* lisp/ecomplete.el: Add completion-table; use lexical-binding and cl-lib
[emacs.git] / src / xdisp.c
blobfd8aad0412122c5ad15678e515fd00212df9fee1
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2018 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 <https://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, Lisp_Object,
880 Lisp_Object, struct text_pos *,
881 ptrdiff_t, int, bool, 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 /* Start from the beginning of the screen line, to make sure we
1199 traverse all of its display elements, and thus capture the
1200 correct metrics. */
1201 move_it_by_lines (&it, 0);
1202 it.vpos = it.current_y = 0;
1203 last_height = 0;
1204 result = make_number (line_bottom_y (&it));
1205 if (old_buffer)
1206 set_buffer_internal_1 (old_buffer);
1208 return result;
1211 /* Return the default pixel height of text lines in window W. The
1212 value is the canonical height of the W frame's default font, plus
1213 any extra space required by the line-spacing variable or frame
1214 parameter.
1216 Implementation note: this ignores any line-spacing text properties
1217 put on the newline characters. This is because those properties
1218 only affect the _screen_ line ending in the newline (i.e., in a
1219 continued line, only the last screen line will be affected), which
1220 means only a small number of lines in a buffer can ever use this
1221 feature. Since this function is used to compute the default pixel
1222 equivalent of text lines in a window, we can safely ignore those
1223 few lines. For the same reasons, we ignore the line-height
1224 properties. */
1226 default_line_pixel_height (struct window *w)
1228 struct frame *f = WINDOW_XFRAME (w);
1229 int height = FRAME_LINE_HEIGHT (f);
1231 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1233 struct buffer *b = XBUFFER (w->contents);
1234 Lisp_Object val = BVAR (b, extra_line_spacing);
1236 if (NILP (val))
1237 val = BVAR (&buffer_defaults, extra_line_spacing);
1238 if (!NILP (val))
1240 if (RANGED_INTEGERP (0, val, INT_MAX))
1241 height += XFASTINT (val);
1242 else if (FLOATP (val))
1244 int addon = XFLOAT_DATA (val) * height + 0.5;
1246 if (addon >= 0)
1247 height += addon;
1250 else
1251 height += f->extra_line_spacing;
1254 return height;
1257 /* Subroutine of pos_visible_p below. Extracts a display string, if
1258 any, from the display spec given as its argument. */
1259 static Lisp_Object
1260 string_from_display_spec (Lisp_Object spec)
1262 if (VECTORP (spec))
1264 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1265 if (STRINGP (AREF (spec, i)))
1266 return AREF (spec, i);
1268 else
1270 for (; CONSP (spec); spec = XCDR (spec))
1271 if (STRINGP (XCAR (spec)))
1272 return XCAR (spec);
1274 return spec;
1278 /* Limit insanely large values of W->hscroll on frame F to the largest
1279 value that will still prevent first_visible_x and last_visible_x of
1280 'struct it' from overflowing an int. */
1281 static int
1282 window_hscroll_limited (struct window *w, struct frame *f)
1284 ptrdiff_t window_hscroll = w->hscroll;
1285 int window_text_width = window_box_width (w, TEXT_AREA);
1286 int colwidth = FRAME_COLUMN_WIDTH (f);
1288 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1289 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1291 return window_hscroll;
1294 /* Return true if position CHARPOS is visible in window W.
1295 CHARPOS < 0 means return info about WINDOW_END position.
1296 If visible, set *X and *Y to pixel coordinates of top left corner.
1297 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1298 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1300 bool
1301 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1302 int *rtop, int *rbot, int *rowh, int *vpos)
1304 struct it it;
1305 void *itdata = bidi_shelve_cache ();
1306 struct text_pos top;
1307 bool visible_p = false;
1308 struct buffer *old_buffer = NULL;
1309 bool r2l = false;
1311 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1312 return visible_p;
1314 if (XBUFFER (w->contents) != current_buffer)
1316 old_buffer = current_buffer;
1317 set_buffer_internal_1 (XBUFFER (w->contents));
1320 SET_TEXT_POS_FROM_MARKER (top, w->start);
1321 /* Scrolling a minibuffer window via scroll bar when the echo area
1322 shows long text sometimes resets the minibuffer contents behind
1323 our backs. Also, someone might narrow-to-region and immediately
1324 call a scroll function. */
1325 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1328 /* If the top of the window is after CHARPOS, the latter is surely
1329 not visible. */
1330 if (charpos >= 0 && CHARPOS (top) > charpos)
1331 return visible_p;
1333 /* Some Lisp hook could call us in the middle of redisplaying this
1334 very window. If, by some bad luck, we are retrying redisplay
1335 because we found that the mode-line height and/or header-line
1336 height needs to be updated, the assignment of mode_line_height
1337 and header_line_height below could disrupt that, due to the
1338 selected/nonselected window dance during mode-line display, and
1339 we could infloop. Avoid that. */
1340 int prev_mode_line_height = w->mode_line_height;
1341 int prev_header_line_height = w->header_line_height;
1342 /* Compute exact mode line heights. */
1343 if (window_wants_mode_line (w))
1345 Lisp_Object window_mode_line_format
1346 = window_parameter (w, Qmode_line_format);
1348 w->mode_line_height
1349 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1350 NILP (window_mode_line_format)
1351 ? BVAR (current_buffer, mode_line_format)
1352 : window_mode_line_format);
1355 if (window_wants_header_line (w))
1357 Lisp_Object window_header_line_format
1358 = window_parameter (w, Qheader_line_format);
1360 w->header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 NILP (window_header_line_format)
1363 ? BVAR (current_buffer, header_line_format)
1364 : window_header_line_format);
1367 start_display (&it, w, top);
1368 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1369 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1371 if (charpos >= 0
1372 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1373 && IT_CHARPOS (it) >= charpos)
1374 /* When scanning backwards under bidi iteration, move_it_to
1375 stops at or _before_ CHARPOS, because it stops at or to
1376 the _right_ of the character at CHARPOS. */
1377 || (it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) <= charpos)))
1380 /* We have reached CHARPOS, or passed it. How the call to
1381 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1382 or covered by a display property, move_it_to stops at the end
1383 of the invisible text, to the right of CHARPOS. (ii) If
1384 CHARPOS is in a display vector, move_it_to stops on its last
1385 glyph. */
1386 int top_x = it.current_x;
1387 int top_y = it.current_y;
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1389 int bottom_y;
1390 struct it save_it;
1391 void *save_it_data = NULL;
1393 /* Calling line_bottom_y may change it.method, it.position, etc. */
1394 SAVE_IT (save_it, it, save_it_data);
1395 last_height = 0;
1396 bottom_y = line_bottom_y (&it);
1397 if (top_y < window_top_y)
1398 visible_p = bottom_y > window_top_y;
1399 else if (top_y < it.last_visible_y)
1400 visible_p = true;
1401 if (bottom_y >= it.last_visible_y
1402 && it.bidi_p && it.bidi_it.scan_dir == -1
1403 && IT_CHARPOS (it) < charpos)
1405 /* When the last line of the window is scanned backwards
1406 under bidi iteration, we could be duped into thinking
1407 that we have passed CHARPOS, when in fact move_it_to
1408 simply stopped short of CHARPOS because it reached
1409 last_visible_y. To see if that's what happened, we call
1410 move_it_to again with a slightly larger vertical limit,
1411 and see if it actually moved vertically; if it did, we
1412 didn't really reach CHARPOS, which is beyond window end. */
1413 /* Why 10? because we don't know how many canonical lines
1414 will the height of the next line(s) be. So we guess. */
1415 int ten_more_lines = 10 * default_line_pixel_height (w);
1417 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1418 MOVE_TO_POS | MOVE_TO_Y);
1419 if (it.current_y > top_y)
1420 visible_p = false;
1423 RESTORE_IT (&it, &save_it, save_it_data);
1424 if (visible_p)
1426 if (it.method == GET_FROM_DISPLAY_VECTOR)
1428 /* We stopped on the last glyph of a display vector.
1429 Try and recompute. Hack alert! */
1430 if (charpos < 2 || top.charpos >= charpos)
1431 top_x = it.glyph_row->x;
1432 else
1434 struct it it2, it2_prev;
1435 /* The idea is to get to the previous buffer
1436 position, consume the character there, and use
1437 the pixel coordinates we get after that. But if
1438 the previous buffer position is also displayed
1439 from a display vector, we need to consume all of
1440 the glyphs from that display vector. */
1441 start_display (&it2, w, top);
1442 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* If we didn't get to CHARPOS - 1, there's some
1444 replacing display property at that position, and
1445 we stopped after it. That is exactly the place
1446 whose coordinates we want. */
1447 if (IT_CHARPOS (it2) != charpos - 1)
1448 it2_prev = it2;
1449 else
1451 /* Iterate until we get out of the display
1452 vector that displays the character at
1453 CHARPOS - 1. */
1454 do {
1455 get_next_display_element (&it2);
1456 PRODUCE_GLYPHS (&it2);
1457 it2_prev = it2;
1458 set_iterator_to_next (&it2, true);
1459 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1460 && IT_CHARPOS (it2) < charpos);
1462 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1463 || it2_prev.current_x > it2_prev.last_visible_x)
1464 top_x = it.glyph_row->x;
1465 else
1467 top_x = it2_prev.current_x;
1468 top_y = it2_prev.current_y;
1472 else if (IT_CHARPOS (it) != charpos)
1474 Lisp_Object cpos = make_number (charpos);
1475 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1476 Lisp_Object string = string_from_display_spec (spec);
1477 struct text_pos tpos;
1478 bool newline_in_string
1479 = (STRINGP (string)
1480 && memchr (SDATA (string), '\n', SBYTES (string)));
1482 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1483 bool replacing_spec_p
1484 = (!NILP (spec)
1485 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1486 charpos, FRAME_WINDOW_P (it.f)));
1487 /* The tricky code below is needed because there's a
1488 discrepancy between move_it_to and how we set cursor
1489 when PT is at the beginning of a portion of text
1490 covered by a display property or an overlay with a
1491 display property, or the display line ends in a
1492 newline from a display string. move_it_to will stop
1493 _after_ such display strings, whereas
1494 set_cursor_from_row conspires with cursor_row_p to
1495 place the cursor on the first glyph produced from the
1496 display string. */
1498 /* We have overshoot PT because it is covered by a
1499 display property that replaces the text it covers.
1500 If the string includes embedded newlines, we are also
1501 in the wrong display line. Backtrack to the correct
1502 line, where the display property begins. */
1503 if (replacing_spec_p)
1505 Lisp_Object startpos, endpos;
1506 EMACS_INT start, end;
1507 struct it it3;
1509 /* Find the first and the last buffer positions
1510 covered by the display string. */
1511 endpos =
1512 Fnext_single_char_property_change (cpos, Qdisplay,
1513 Qnil, Qnil);
1514 startpos =
1515 Fprevious_single_char_property_change (endpos, Qdisplay,
1516 Qnil, Qnil);
1517 start = XFASTINT (startpos);
1518 end = XFASTINT (endpos);
1519 /* Move to the last buffer position before the
1520 display property. */
1521 start_display (&it3, w, top);
1522 if (start > CHARPOS (top))
1523 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1524 /* Move forward one more line if the position before
1525 the display string is a newline or if it is the
1526 rightmost character on a line that is
1527 continued or word-wrapped. */
1528 if (it3.method == GET_FROM_BUFFER
1529 && (it3.c == '\n'
1530 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1531 move_it_by_lines (&it3, 1);
1532 else if (move_it_in_display_line_to (&it3, -1,
1533 it3.current_x
1534 + it3.pixel_width,
1535 MOVE_TO_X)
1536 == MOVE_LINE_CONTINUED)
1538 move_it_by_lines (&it3, 1);
1539 /* When we are under word-wrap, the #$@%!
1540 move_it_by_lines moves 2 lines, so we need to
1541 fix that up. */
1542 if (it3.line_wrap == WORD_WRAP)
1543 move_it_by_lines (&it3, -1);
1546 /* Record the vertical coordinate of the display
1547 line where we wound up. */
1548 top_y = it3.current_y;
1549 if (it3.bidi_p)
1551 /* When characters are reordered for display,
1552 the character displayed to the left of the
1553 display string could be _after_ the display
1554 property in the logical order. Use the
1555 smallest vertical position of these two. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1558 if (it3.current_y < top_y)
1559 top_y = it3.current_y;
1561 /* Move from the top of the window to the beginning
1562 of the display line where the display string
1563 begins. */
1564 start_display (&it3, w, top);
1565 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1566 /* If it3_moved stays false after the 'while' loop
1567 below, that means we already were at a newline
1568 before the loop (e.g., the display string begins
1569 with a newline), so we don't need to (and cannot)
1570 inspect the glyphs of it3.glyph_row, because
1571 PRODUCE_GLYPHS will not produce anything for a
1572 newline, and thus it3.glyph_row stays at its
1573 stale content it got at top of the window. */
1574 bool it3_moved = false;
1575 /* Finally, advance the iterator until we hit the
1576 first display element whose character position is
1577 CHARPOS, or until the first newline from the
1578 display string, which signals the end of the
1579 display line. */
1580 while (get_next_display_element (&it3))
1582 PRODUCE_GLYPHS (&it3);
1583 if (IT_CHARPOS (it3) == charpos
1584 || ITERATOR_AT_END_OF_LINE_P (&it3))
1585 break;
1586 it3_moved = true;
1587 set_iterator_to_next (&it3, false);
1589 top_x = it3.current_x - it3.pixel_width;
1590 /* Normally, we would exit the above loop because we
1591 found the display element whose character
1592 position is CHARPOS. For the contingency that we
1593 didn't, and stopped at the first newline from the
1594 display string, move back over the glyphs
1595 produced from the string, until we find the
1596 rightmost glyph not from the string. */
1597 if (it3_moved
1598 && newline_in_string
1599 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1601 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1602 + it3.glyph_row->used[TEXT_AREA];
1604 while (EQ ((g - 1)->object, string))
1606 --g;
1607 top_x -= g->pixel_width;
1609 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1610 + it3.glyph_row->used[TEXT_AREA]);
1615 *x = top_x;
1616 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1617 *rtop = max (0, window_top_y - top_y);
1618 *rbot = max (0, bottom_y - it.last_visible_y);
1619 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1620 - max (top_y, window_top_y)));
1621 *vpos = it.vpos;
1622 if (it.bidi_it.paragraph_dir == R2L)
1623 r2l = true;
1626 else
1628 /* Either we were asked to provide info about WINDOW_END, or
1629 CHARPOS is in the partially visible glyph row at end of
1630 window. */
1631 struct it it2;
1632 void *it2data = NULL;
1634 SAVE_IT (it2, it, it2data);
1635 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1636 move_it_by_lines (&it, 1);
1637 if (charpos < IT_CHARPOS (it)
1638 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1640 visible_p = true;
1641 RESTORE_IT (&it2, &it2, it2data);
1642 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1643 *x = it2.current_x;
1644 *y = it2.current_y + it2.max_ascent - it2.ascent;
1645 *rtop = max (0, -it2.current_y);
1646 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1647 - it.last_visible_y));
1648 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1649 it.last_visible_y)
1650 - max (it2.current_y,
1651 WINDOW_HEADER_LINE_HEIGHT (w))));
1652 *vpos = it2.vpos;
1653 if (it2.bidi_it.paragraph_dir == R2L)
1654 r2l = true;
1656 else
1657 bidi_unshelve_cache (it2data, true);
1659 bidi_unshelve_cache (itdata, false);
1661 if (old_buffer)
1662 set_buffer_internal_1 (old_buffer);
1664 if (visible_p)
1666 if (w->hscroll > 0)
1667 *x -=
1668 window_hscroll_limited (w, WINDOW_XFRAME (w))
1669 * WINDOW_FRAME_COLUMN_WIDTH (w);
1670 /* For lines in an R2L paragraph, we need to mirror the X pixel
1671 coordinate wrt the text area. For the reasons, see the
1672 commentary in buffer_posn_from_coords and the explanation of
1673 the geometry used by the move_it_* functions at the end of
1674 the large commentary near the beginning of this file. */
1675 if (r2l)
1676 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1679 #if false
1680 /* Debugging code. */
1681 if (visible_p)
1682 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1683 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1684 else
1685 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1686 #endif
1688 /* Restore potentially overwritten values. */
1689 w->mode_line_height = prev_mode_line_height;
1690 w->header_line_height = prev_header_line_height;
1692 return visible_p;
1696 /* Return the next character from STR. Return in *LEN the length of
1697 the character. This is like STRING_CHAR_AND_LENGTH but never
1698 returns an invalid character. If we find one, we return a `?', but
1699 with the length of the invalid character. */
1701 static int
1702 string_char_and_length (const unsigned char *str, int *len)
1704 int c;
1706 c = STRING_CHAR_AND_LENGTH (str, *len);
1707 if (!CHAR_VALID_P (c))
1708 /* We may not change the length here because other places in Emacs
1709 don't use this function, i.e. they silently accept invalid
1710 characters. */
1711 c = '?';
1713 return c;
1718 /* Given a position POS containing a valid character and byte position
1719 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1721 static struct text_pos
1722 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1724 eassert (STRINGP (string) && nchars >= 0);
1726 if (STRING_MULTIBYTE (string))
1728 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1729 int len;
1731 while (nchars--)
1733 string_char_and_length (p, &len);
1734 p += len;
1735 CHARPOS (pos) += 1;
1736 BYTEPOS (pos) += len;
1739 else
1740 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1742 return pos;
1746 /* Value is the text position, i.e. character and byte position,
1747 for character position CHARPOS in STRING. */
1749 static struct text_pos
1750 string_pos (ptrdiff_t charpos, Lisp_Object string)
1752 struct text_pos pos;
1753 eassert (STRINGP (string));
1754 eassert (charpos >= 0);
1755 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1756 return pos;
1760 /* Value is a text position, i.e. character and byte position, for
1761 character position CHARPOS in C string S. MULTIBYTE_P
1762 means recognize multibyte characters. */
1764 static struct text_pos
1765 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1767 struct text_pos pos;
1769 eassert (s != NULL);
1770 eassert (charpos >= 0);
1772 if (multibyte_p)
1774 int len;
1776 SET_TEXT_POS (pos, 0, 0);
1777 while (charpos--)
1779 string_char_and_length ((const unsigned char *) s, &len);
1780 s += len;
1781 CHARPOS (pos) += 1;
1782 BYTEPOS (pos) += len;
1785 else
1786 SET_TEXT_POS (pos, charpos, charpos);
1788 return pos;
1792 /* Value is the number of characters in C string S. MULTIBYTE_P
1793 means recognize multibyte characters. */
1795 static ptrdiff_t
1796 number_of_chars (const char *s, bool multibyte_p)
1798 ptrdiff_t nchars;
1800 if (multibyte_p)
1802 ptrdiff_t rest = strlen (s);
1803 int len;
1804 const unsigned char *p = (const unsigned char *) s;
1806 for (nchars = 0; rest > 0; ++nchars)
1808 string_char_and_length (p, &len);
1809 rest -= len, p += len;
1812 else
1813 nchars = strlen (s);
1815 return nchars;
1819 /* Compute byte position NEWPOS->bytepos corresponding to
1820 NEWPOS->charpos. POS is a known position in string STRING.
1821 NEWPOS->charpos must be >= POS.charpos. */
1823 static void
1824 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1826 eassert (STRINGP (string));
1827 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1829 if (STRING_MULTIBYTE (string))
1830 *newpos = string_pos_nchars_ahead (pos, string,
1831 CHARPOS (*newpos) - CHARPOS (pos));
1832 else
1833 BYTEPOS (*newpos) = CHARPOS (*newpos);
1836 /* EXPORT:
1837 Return an estimation of the pixel height of mode or header lines on
1838 frame F. FACE_ID specifies what line's height to estimate. */
1841 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1843 #ifdef HAVE_WINDOW_SYSTEM
1844 if (FRAME_WINDOW_P (f))
1846 int height = FONT_HEIGHT (FRAME_FONT (f));
1848 /* This function is called so early when Emacs starts that the face
1849 cache and mode line face are not yet initialized. */
1850 if (FRAME_FACE_CACHE (f))
1852 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1853 if (face)
1855 if (face->font)
1856 height = normal_char_height (face->font, -1);
1857 if (face->box_line_width > 0)
1858 height += 2 * face->box_line_width;
1862 return height;
1864 #endif
1866 return 1;
1869 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1870 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1871 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1872 not force the value into range. */
1874 void
1875 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1876 NativeRectangle *bounds, bool noclip)
1879 #ifdef HAVE_WINDOW_SYSTEM
1880 if (FRAME_WINDOW_P (f))
1882 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1883 even for negative values. */
1884 if (pix_x < 0)
1885 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1886 if (pix_y < 0)
1887 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1889 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1890 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1892 if (bounds)
1893 STORE_NATIVE_RECT (*bounds,
1894 FRAME_COL_TO_PIXEL_X (f, pix_x),
1895 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1896 FRAME_COLUMN_WIDTH (f) - 1,
1897 FRAME_LINE_HEIGHT (f) - 1);
1899 /* PXW: Should we clip pixels before converting to columns/lines? */
1900 if (!noclip)
1902 if (pix_x < 0)
1903 pix_x = 0;
1904 else if (pix_x > FRAME_TOTAL_COLS (f))
1905 pix_x = FRAME_TOTAL_COLS (f);
1907 if (pix_y < 0)
1908 pix_y = 0;
1909 else if (pix_y > FRAME_TOTAL_LINES (f))
1910 pix_y = FRAME_TOTAL_LINES (f);
1913 #endif
1915 *x = pix_x;
1916 *y = pix_y;
1920 /* Find the glyph under window-relative coordinates X/Y in window W.
1921 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1922 strings. Return in *HPOS and *VPOS the row and column number of
1923 the glyph found. Return in *AREA the glyph area containing X.
1924 Value is a pointer to the glyph found or null if X/Y is not on
1925 text, or we can't tell because W's current matrix is not up to
1926 date. */
1928 static struct glyph *
1929 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1930 int *dx, int *dy, int *area)
1932 struct glyph *glyph, *end;
1933 struct glyph_row *row = NULL;
1934 int x0, i;
1936 /* Find row containing Y. Give up if some row is not enabled. */
1937 for (i = 0; i < w->current_matrix->nrows; ++i)
1939 row = MATRIX_ROW (w->current_matrix, i);
1940 if (!row->enabled_p)
1941 return NULL;
1942 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1943 break;
1946 *vpos = i;
1947 *hpos = 0;
1949 /* Give up if Y is not in the window. */
1950 if (i == w->current_matrix->nrows)
1951 return NULL;
1953 /* Get the glyph area containing X. */
1954 if (w->pseudo_window_p)
1956 *area = TEXT_AREA;
1957 x0 = 0;
1959 else
1961 if (x < window_box_left_offset (w, TEXT_AREA))
1963 *area = LEFT_MARGIN_AREA;
1964 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1966 else if (x < window_box_right_offset (w, TEXT_AREA))
1968 *area = TEXT_AREA;
1969 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1971 else
1973 *area = RIGHT_MARGIN_AREA;
1974 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1978 /* Find glyph containing X. */
1979 glyph = row->glyphs[*area];
1980 end = glyph + row->used[*area];
1981 x -= x0;
1982 while (glyph < end && x >= glyph->pixel_width)
1984 x -= glyph->pixel_width;
1985 ++glyph;
1988 if (glyph == end)
1989 return NULL;
1991 if (dx)
1993 *dx = x;
1994 *dy = y - (row->y + row->ascent - glyph->ascent);
1997 *hpos = glyph - row->glyphs[*area];
1998 return glyph;
2001 /* Convert frame-relative x/y to coordinates relative to window W.
2002 Takes pseudo-windows into account. */
2004 static void
2005 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2007 if (w->pseudo_window_p)
2009 /* A pseudo-window is always full-width, and starts at the
2010 left edge of the frame, plus a frame border. */
2011 struct frame *f = XFRAME (w->frame);
2012 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2013 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2015 else
2017 *x -= WINDOW_LEFT_EDGE_X (w);
2018 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2022 #ifdef HAVE_WINDOW_SYSTEM
2024 /* EXPORT:
2025 Return in RECTS[] at most N clipping rectangles for glyph string S.
2026 Return the number of stored rectangles. */
2029 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2031 XRectangle r;
2033 if (n <= 0)
2034 return 0;
2036 if (s->row->full_width_p)
2038 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2039 r.x = WINDOW_LEFT_EDGE_X (s->w);
2040 if (s->row->mode_line_p)
2041 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2042 else
2043 r.width = WINDOW_PIXEL_WIDTH (s->w);
2045 /* Unless displaying a mode or menu bar line, which are always
2046 fully visible, clip to the visible part of the row. */
2047 if (s->w->pseudo_window_p)
2048 r.height = s->row->visible_height;
2049 else
2050 r.height = s->height;
2052 else
2054 /* This is a text line that may be partially visible. */
2055 r.x = window_box_left (s->w, s->area);
2056 r.width = window_box_width (s->w, s->area);
2057 r.height = s->row->visible_height;
2060 if (s->clip_head)
2061 if (r.x < s->clip_head->x)
2063 if (r.width >= s->clip_head->x - r.x)
2064 r.width -= s->clip_head->x - r.x;
2065 else
2066 r.width = 0;
2067 r.x = s->clip_head->x;
2069 if (s->clip_tail)
2070 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2072 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2073 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2074 else
2075 r.width = 0;
2078 /* If S draws overlapping rows, it's sufficient to use the top and
2079 bottom of the window for clipping because this glyph string
2080 intentionally draws over other lines. */
2081 if (s->for_overlaps)
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 r.height = window_text_bottom_y (s->w) - r.y;
2086 /* Alas, the above simple strategy does not work for the
2087 environments with anti-aliased text: if the same text is
2088 drawn onto the same place multiple times, it gets thicker.
2089 If the overlap we are processing is for the erased cursor, we
2090 take the intersection with the rectangle of the cursor. */
2091 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2093 XRectangle rc, r_save = r;
2095 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2096 rc.y = s->w->phys_cursor.y;
2097 rc.width = s->w->phys_cursor_width;
2098 rc.height = s->w->phys_cursor_height;
2100 x_intersect_rectangles (&r_save, &rc, &r);
2103 else
2105 /* Don't use S->y for clipping because it doesn't take partially
2106 visible lines into account. For example, it can be negative for
2107 partially visible lines at the top of a window. */
2108 if (!s->row->full_width_p
2109 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2110 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2111 else
2112 r.y = max (0, s->row->y);
2115 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2117 /* If drawing the cursor, don't let glyph draw outside its
2118 advertised boundaries. Cleartype does this under some circumstances. */
2119 if (s->hl == DRAW_CURSOR)
2121 struct glyph *glyph = s->first_glyph;
2122 int height, max_y;
2124 if (s->x > r.x)
2126 if (r.width >= s->x - r.x)
2127 r.width -= s->x - r.x;
2128 else /* R2L hscrolled row with cursor outside text area */
2129 r.width = 0;
2130 r.x = s->x;
2132 r.width = min (r.width, glyph->pixel_width);
2134 /* If r.y is below window bottom, ensure that we still see a cursor. */
2135 height = min (glyph->ascent + glyph->descent,
2136 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2137 max_y = window_text_bottom_y (s->w) - height;
2138 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2139 if (s->ybase - glyph->ascent > max_y)
2141 r.y = max_y;
2142 r.height = height;
2144 else
2146 /* Don't draw cursor glyph taller than our actual glyph. */
2147 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2148 if (height < r.height)
2150 max_y = r.y + r.height;
2151 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2152 r.height = min (max_y - r.y, height);
2157 if (s->row->clip)
2159 XRectangle r_save = r;
2161 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2162 r.width = 0;
2165 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2166 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2168 #ifdef CONVERT_FROM_XRECT
2169 CONVERT_FROM_XRECT (r, *rects);
2170 #else
2171 *rects = r;
2172 #endif
2173 return 1;
2175 else
2177 /* If we are processing overlapping and allowed to return
2178 multiple clipping rectangles, we exclude the row of the glyph
2179 string from the clipping rectangle. This is to avoid drawing
2180 the same text on the environment with anti-aliasing. */
2181 #ifdef CONVERT_FROM_XRECT
2182 XRectangle rs[2];
2183 #else
2184 XRectangle *rs = rects;
2185 #endif
2186 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2188 if (s->for_overlaps & OVERLAPS_PRED)
2190 rs[i] = r;
2191 if (r.y + r.height > row_y)
2193 if (r.y < row_y)
2194 rs[i].height = row_y - r.y;
2195 else
2196 rs[i].height = 0;
2198 i++;
2200 if (s->for_overlaps & OVERLAPS_SUCC)
2202 rs[i] = r;
2203 if (r.y < row_y + s->row->visible_height)
2205 if (r.y + r.height > row_y + s->row->visible_height)
2207 rs[i].y = row_y + s->row->visible_height;
2208 rs[i].height = r.y + r.height - rs[i].y;
2210 else
2211 rs[i].height = 0;
2213 i++;
2216 n = i;
2217 #ifdef CONVERT_FROM_XRECT
2218 for (i = 0; i < n; i++)
2219 CONVERT_FROM_XRECT (rs[i], rects[i]);
2220 #endif
2221 return n;
2225 /* EXPORT:
2226 Return in *NR the clipping rectangle for glyph string S. */
2228 void
2229 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2231 get_glyph_string_clip_rects (s, nr, 1);
2235 /* EXPORT:
2236 Return the position and height of the phys cursor in window W.
2237 Set w->phys_cursor_width to width of phys cursor.
2240 void
2241 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2242 struct glyph *glyph, int *xp, int *yp, int *heightp)
2244 struct frame *f = XFRAME (WINDOW_FRAME (w));
2245 int x, y, wd, h, h0, y0, ascent;
2247 /* Compute the width of the rectangle to draw. If on a stretch
2248 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2249 rectangle as wide as the glyph, but use a canonical character
2250 width instead. */
2251 wd = glyph->pixel_width;
2253 x = w->phys_cursor.x;
2254 if (x < 0)
2256 wd += x;
2257 x = 0;
2260 if (glyph->type == STRETCH_GLYPH
2261 && !x_stretch_cursor_p)
2262 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2263 w->phys_cursor_width = wd;
2265 /* Don't let the hollow cursor glyph descend below the glyph row's
2266 ascent value, lest the hollow cursor looks funny. */
2267 y = w->phys_cursor.y;
2268 ascent = row->ascent;
2269 if (row->ascent < glyph->ascent)
2271 y -= glyph->ascent - row->ascent;
2272 ascent = glyph->ascent;
2275 /* If y is below window bottom, ensure that we still see a cursor. */
2276 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2278 h = max (h0, ascent + glyph->descent);
2279 h0 = min (h0, ascent + glyph->descent);
2281 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2282 if (y < y0)
2284 h = max (h - (y0 - y) + 1, h0);
2285 y = y0 - 1;
2287 else
2289 y0 = window_text_bottom_y (w) - h0;
2290 if (y > y0)
2292 h += y - y0;
2293 y = y0;
2297 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2298 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2299 *heightp = h;
2303 * Remember which glyph the mouse is over.
2306 void
2307 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2309 Lisp_Object window;
2310 struct window *w;
2311 struct glyph_row *r, *gr, *end_row;
2312 enum window_part part;
2313 enum glyph_row_area area;
2314 int x, y, width, height;
2316 /* Try to determine frame pixel position and size of the glyph under
2317 frame pixel coordinates X/Y on frame F. */
2319 if (window_resize_pixelwise)
2321 width = height = 1;
2322 goto virtual_glyph;
2324 else if (!f->glyphs_initialized_p
2325 || (window = window_from_coordinates (f, gx, gy, &part, false),
2326 NILP (window)))
2328 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2329 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2330 goto virtual_glyph;
2333 w = XWINDOW (window);
2334 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2335 height = WINDOW_FRAME_LINE_HEIGHT (w);
2337 x = window_relative_x_coord (w, part, gx);
2338 y = gy - WINDOW_TOP_EDGE_Y (w);
2340 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2341 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2343 if (w->pseudo_window_p)
2345 area = TEXT_AREA;
2346 part = ON_MODE_LINE; /* Don't adjust margin. */
2347 goto text_glyph;
2350 switch (part)
2352 case ON_LEFT_MARGIN:
2353 area = LEFT_MARGIN_AREA;
2354 goto text_glyph;
2356 case ON_RIGHT_MARGIN:
2357 area = RIGHT_MARGIN_AREA;
2358 goto text_glyph;
2360 case ON_HEADER_LINE:
2361 case ON_MODE_LINE:
2362 gr = (part == ON_HEADER_LINE
2363 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2364 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2365 gy = gr->y;
2366 area = TEXT_AREA;
2367 goto text_glyph_row_found;
2369 case ON_TEXT:
2370 area = TEXT_AREA;
2372 text_glyph:
2373 gr = 0; gy = 0;
2374 for (; r <= end_row && r->enabled_p; ++r)
2375 if (r->y + r->height > y)
2377 gr = r; gy = r->y;
2378 break;
2381 text_glyph_row_found:
2382 if (gr && gy <= y)
2384 struct glyph *g = gr->glyphs[area];
2385 struct glyph *end = g + gr->used[area];
2387 height = gr->height;
2388 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2389 if (gx + g->pixel_width > x)
2390 break;
2392 if (g < end)
2394 if (g->type == IMAGE_GLYPH)
2396 /* Don't remember when mouse is over image, as
2397 image may have hot-spots. */
2398 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2399 return;
2401 width = g->pixel_width;
2403 else
2405 /* Use nominal char spacing at end of line. */
2406 x -= gx;
2407 gx += (x / width) * width;
2410 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2412 gx += window_box_left_offset (w, area);
2413 /* Don't expand over the modeline to make sure the vertical
2414 drag cursor is shown early enough. */
2415 height = min (height,
2416 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2419 else
2421 /* Use nominal line height at end of window. */
2422 gx = (x / width) * width;
2423 y -= gy;
2424 gy += (y / height) * height;
2425 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2426 /* See comment above. */
2427 height = min (height,
2428 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2430 break;
2432 case ON_LEFT_FRINGE:
2433 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2434 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2435 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2436 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2437 goto row_glyph;
2439 case ON_RIGHT_FRINGE:
2440 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2441 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2442 : window_box_right_offset (w, TEXT_AREA));
2443 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2444 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2445 && !WINDOW_RIGHTMOST_P (w))
2446 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2447 /* Make sure the vertical border can get her own glyph to the
2448 right of the one we build here. */
2449 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2450 else
2451 width = WINDOW_PIXEL_WIDTH (w) - gx;
2452 else
2453 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2455 goto row_glyph;
2457 case ON_VERTICAL_BORDER:
2458 gx = WINDOW_PIXEL_WIDTH (w) - width;
2459 goto row_glyph;
2461 case ON_VERTICAL_SCROLL_BAR:
2462 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2464 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2465 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2466 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2467 : 0)));
2468 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2470 row_glyph:
2471 gr = 0, gy = 0;
2472 for (; r <= end_row && r->enabled_p; ++r)
2473 if (r->y + r->height > y)
2475 gr = r; gy = r->y;
2476 break;
2479 if (gr && gy <= y)
2480 height = gr->height;
2481 else
2483 /* Use nominal line height at end of window. */
2484 y -= gy;
2485 gy += (y / height) * height;
2487 break;
2489 case ON_RIGHT_DIVIDER:
2490 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2491 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2492 gy = 0;
2493 /* The bottom divider prevails. */
2494 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2495 goto add_edge;
2497 case ON_BOTTOM_DIVIDER:
2498 gx = 0;
2499 width = WINDOW_PIXEL_WIDTH (w);
2500 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2501 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2502 goto add_edge;
2504 default:
2506 virtual_glyph:
2507 /* If there is no glyph under the mouse, then we divide the screen
2508 into a grid of the smallest glyph in the frame, and use that
2509 as our "glyph". */
2511 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2512 round down even for negative values. */
2513 if (gx < 0)
2514 gx -= width - 1;
2515 if (gy < 0)
2516 gy -= height - 1;
2518 gx = (gx / width) * width;
2519 gy = (gy / height) * height;
2521 goto store_rect;
2524 add_edge:
2525 gx += WINDOW_LEFT_EDGE_X (w);
2526 gy += WINDOW_TOP_EDGE_Y (w);
2528 store_rect:
2529 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2531 /* Visible feedback for debugging. */
2532 #if false && defined HAVE_X_WINDOWS
2533 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2534 f->output_data.x->normal_gc,
2535 gx, gy, width, height);
2536 #endif
2540 #endif /* HAVE_WINDOW_SYSTEM */
2542 static void
2543 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2545 eassert (w);
2546 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2547 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2548 w->window_end_vpos
2549 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2552 static bool
2553 hscrolling_current_line_p (struct window *w)
2555 return (!w->suspend_auto_hscroll
2556 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2557 Qcurrent_line));
2560 /***********************************************************************
2561 Lisp form evaluation
2562 ***********************************************************************/
2564 /* Error handler for safe_eval and safe_call. */
2566 static Lisp_Object
2567 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2569 add_to_log ("Error during redisplay: %S signaled %S",
2570 Flist (nargs, args), arg);
2571 return Qnil;
2574 /* Call function FUNC with the rest of NARGS - 1 arguments
2575 following. Return the result, or nil if something went
2576 wrong. Prevent redisplay during the evaluation. */
2578 static Lisp_Object
2579 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2581 Lisp_Object val;
2583 if (inhibit_eval_during_redisplay)
2584 val = Qnil;
2585 else
2587 ptrdiff_t i;
2588 ptrdiff_t count = SPECPDL_INDEX ();
2589 Lisp_Object *args;
2590 USE_SAFE_ALLOCA;
2591 SAFE_ALLOCA_LISP (args, nargs);
2593 args[0] = func;
2594 for (i = 1; i < nargs; i++)
2595 args[i] = va_arg (ap, Lisp_Object);
2597 specbind (Qinhibit_redisplay, Qt);
2598 if (inhibit_quit)
2599 specbind (Qinhibit_quit, Qt);
2600 /* Use Qt to ensure debugger does not run,
2601 so there is no possibility of wanting to redisplay. */
2602 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2603 safe_eval_handler);
2604 SAFE_FREE ();
2605 val = unbind_to (count, val);
2608 return val;
2611 Lisp_Object
2612 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2614 Lisp_Object retval;
2615 va_list ap;
2617 va_start (ap, func);
2618 retval = safe__call (false, nargs, func, ap);
2619 va_end (ap);
2620 return retval;
2623 /* Call function FN with one argument ARG.
2624 Return the result, or nil if something went wrong. */
2626 Lisp_Object
2627 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2629 return safe_call (2, fn, arg);
2632 static Lisp_Object
2633 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2635 Lisp_Object retval;
2636 va_list ap;
2638 va_start (ap, fn);
2639 retval = safe__call (inhibit_quit, 2, fn, ap);
2640 va_end (ap);
2641 return retval;
2644 Lisp_Object
2645 safe_eval (Lisp_Object sexpr)
2647 return safe__call1 (false, Qeval, sexpr);
2650 static Lisp_Object
2651 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2653 return safe__call1 (inhibit_quit, Qeval, sexpr);
2656 /* Call function FN with two arguments ARG1 and ARG2.
2657 Return the result, or nil if something went wrong. */
2659 Lisp_Object
2660 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2662 return safe_call (3, fn, arg1, arg2);
2667 /***********************************************************************
2668 Debugging
2669 ***********************************************************************/
2671 /* Define CHECK_IT to perform sanity checks on iterators.
2672 This is for debugging. It is too slow to do unconditionally. */
2674 static void
2675 CHECK_IT (struct it *it)
2677 #if false
2678 if (it->method == GET_FROM_STRING)
2680 eassert (STRINGP (it->string));
2681 eassert (IT_STRING_CHARPOS (*it) >= 0);
2683 else
2685 eassert (IT_STRING_CHARPOS (*it) < 0);
2686 if (it->method == GET_FROM_BUFFER)
2688 /* Check that character and byte positions agree. */
2689 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2693 if (it->dpvec)
2694 eassert (it->current.dpvec_index >= 0);
2695 else
2696 eassert (it->current.dpvec_index < 0);
2697 #endif
2701 /* Check that the window end of window W is what we expect it
2702 to be---the last row in the current matrix displaying text. */
2704 static void
2705 CHECK_WINDOW_END (struct window *w)
2707 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2708 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2710 struct glyph_row *row;
2711 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2712 !row->enabled_p
2713 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2714 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2716 #endif
2719 /***********************************************************************
2720 Iterator initialization
2721 ***********************************************************************/
2723 /* Initialize IT for displaying current_buffer in window W, starting
2724 at character position CHARPOS. CHARPOS < 0 means that no buffer
2725 position is specified which is useful when the iterator is assigned
2726 a position later. BYTEPOS is the byte position corresponding to
2727 CHARPOS.
2729 If ROW is not null, calls to produce_glyphs with IT as parameter
2730 will produce glyphs in that row.
2732 BASE_FACE_ID is the id of a base face to use. It must be one of
2733 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2735 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2737 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2738 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2739 will be initialized to use the corresponding mode line glyph row of
2740 the desired matrix of W. */
2742 void
2743 init_iterator (struct it *it, struct window *w,
2744 ptrdiff_t charpos, ptrdiff_t bytepos,
2745 struct glyph_row *row, enum face_id base_face_id)
2747 enum face_id remapped_base_face_id = base_face_id;
2749 /* Some precondition checks. */
2750 eassert (w != NULL && it != NULL);
2751 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2752 && charpos <= ZV));
2754 /* If face attributes have been changed since the last redisplay,
2755 free realized faces now because they depend on face definitions
2756 that might have changed. Don't free faces while there might be
2757 desired matrices pending which reference these faces. */
2758 if (!inhibit_free_realized_faces)
2760 if (face_change)
2762 face_change = false;
2763 XFRAME (w->frame)->face_change = 0;
2764 free_all_realized_faces (Qnil);
2766 else if (XFRAME (w->frame)->face_change)
2768 XFRAME (w->frame)->face_change = 0;
2769 free_all_realized_faces (w->frame);
2773 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2774 if (! NILP (Vface_remapping_alist))
2775 remapped_base_face_id
2776 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2778 /* Use one of the mode line rows of W's desired matrix if
2779 appropriate. */
2780 if (row == NULL)
2782 if (base_face_id == MODE_LINE_FACE_ID
2783 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2784 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2785 else if (base_face_id == HEADER_LINE_FACE_ID)
2786 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2789 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2790 Other parts of redisplay rely on that. */
2791 memclear (it, sizeof *it);
2792 it->current.overlay_string_index = -1;
2793 it->current.dpvec_index = -1;
2794 it->base_face_id = remapped_base_face_id;
2795 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2796 it->paragraph_embedding = L2R;
2797 it->bidi_it.w = w;
2799 /* The window in which we iterate over current_buffer: */
2800 XSETWINDOW (it->window, w);
2801 it->w = w;
2802 it->f = XFRAME (w->frame);
2804 it->cmp_it.id = -1;
2806 /* Extra space between lines (on window systems only). */
2807 if (base_face_id == DEFAULT_FACE_ID
2808 && FRAME_WINDOW_P (it->f))
2810 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2811 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2812 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2813 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2814 * FRAME_LINE_HEIGHT (it->f));
2815 else if (it->f->extra_line_spacing > 0)
2816 it->extra_line_spacing = it->f->extra_line_spacing;
2819 /* If realized faces have been removed, e.g. because of face
2820 attribute changes of named faces, recompute them. When running
2821 in batch mode, the face cache of the initial frame is null. If
2822 we happen to get called, make a dummy face cache. */
2823 if (FRAME_FACE_CACHE (it->f) == NULL)
2824 init_frame_faces (it->f);
2825 if (FRAME_FACE_CACHE (it->f)->used == 0)
2826 recompute_basic_faces (it->f);
2828 it->override_ascent = -1;
2830 /* Are control characters displayed as `^C'? */
2831 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2833 /* -1 means everything between a CR and the following line end
2834 is invisible. >0 means lines indented more than this value are
2835 invisible. */
2836 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2837 ? (clip_to_bounds
2838 (-1, XINT (BVAR (current_buffer, selective_display)),
2839 PTRDIFF_MAX))
2840 : (!NILP (BVAR (current_buffer, selective_display))
2841 ? -1 : 0));
2842 it->selective_display_ellipsis_p
2843 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2845 /* Display table to use. */
2846 it->dp = window_display_table (w);
2848 /* Are multibyte characters enabled in current_buffer? */
2849 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2851 /* Get the position at which the redisplay_end_trigger hook should
2852 be run, if it is to be run at all. */
2853 if (MARKERP (w->redisplay_end_trigger)
2854 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2855 it->redisplay_end_trigger_charpos
2856 = marker_position (w->redisplay_end_trigger);
2857 else if (INTEGERP (w->redisplay_end_trigger))
2858 it->redisplay_end_trigger_charpos
2859 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2860 PTRDIFF_MAX);
2862 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2864 /* Are lines in the display truncated? */
2865 if (TRUNCATE != 0)
2866 it->line_wrap = TRUNCATE;
2867 if (base_face_id == DEFAULT_FACE_ID
2868 && !it->w->hscroll
2869 && (WINDOW_FULL_WIDTH_P (it->w)
2870 || NILP (Vtruncate_partial_width_windows)
2871 || (INTEGERP (Vtruncate_partial_width_windows)
2872 /* PXW: Shall we do something about this? */
2873 && (XINT (Vtruncate_partial_width_windows)
2874 <= WINDOW_TOTAL_COLS (it->w))))
2875 && NILP (BVAR (current_buffer, truncate_lines)))
2876 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2877 ? WINDOW_WRAP : WORD_WRAP;
2879 /* Get dimensions of truncation and continuation glyphs. These are
2880 displayed as fringe bitmaps under X, but we need them for such
2881 frames when the fringes are turned off. The no_special_glyphs slot
2882 of the iterator's frame, when set, suppresses their display - by
2883 default for tooltip frames and when set via the 'no-special-glyphs'
2884 frame parameter. */
2885 #ifdef HAVE_WINDOW_SYSTEM
2886 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2887 #endif
2889 if (it->line_wrap == TRUNCATE)
2891 /* We will need the truncation glyph. */
2892 eassert (it->glyph_row == NULL);
2893 produce_special_glyphs (it, IT_TRUNCATION);
2894 it->truncation_pixel_width = it->pixel_width;
2896 else
2898 /* We will need the continuation glyph. */
2899 eassert (it->glyph_row == NULL);
2900 produce_special_glyphs (it, IT_CONTINUATION);
2901 it->continuation_pixel_width = it->pixel_width;
2905 /* Reset these values to zero because the produce_special_glyphs
2906 above has changed them. */
2907 it->pixel_width = it->ascent = it->descent = 0;
2908 it->phys_ascent = it->phys_descent = 0;
2910 /* Set this after getting the dimensions of truncation and
2911 continuation glyphs, so that we don't produce glyphs when calling
2912 produce_special_glyphs, above. */
2913 it->glyph_row = row;
2914 it->area = TEXT_AREA;
2916 /* Get the dimensions of the display area. The display area
2917 consists of the visible window area plus a horizontally scrolled
2918 part to the left of the window. All x-values are relative to the
2919 start of this total display area. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 /* Mode lines, menu bar in terminal frames. */
2923 it->first_visible_x = 0;
2924 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2926 else
2928 /* When hscrolling only the current line, don't apply the
2929 hscroll here, it will be applied by display_line when it gets
2930 to laying out the line showing point. However, if the
2931 window's min_hscroll is positive, the user specified a lower
2932 bound for automatic hscrolling, so they expect the
2933 non-current lines to obey that hscroll amount. */
2934 if (hscrolling_current_line_p (w))
2936 if (w->min_hscroll > 0)
2937 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2938 else
2939 it->first_visible_x = 0;
2941 else
2942 it->first_visible_x =
2943 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2944 it->last_visible_x = (it->first_visible_x
2945 + window_box_width (w, TEXT_AREA));
2947 /* If we truncate lines, leave room for the truncation glyph(s) at
2948 the right margin. Otherwise, leave room for the continuation
2949 glyph(s). Done only if the window has no right fringe. */
2950 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2952 if (it->line_wrap == TRUNCATE)
2953 it->last_visible_x -= it->truncation_pixel_width;
2954 else
2955 it->last_visible_x -= it->continuation_pixel_width;
2958 it->header_line_p = window_wants_header_line (w);
2959 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2962 /* Leave room for a border glyph. */
2963 if (!FRAME_WINDOW_P (it->f)
2964 && !WINDOW_RIGHTMOST_P (it->w))
2965 it->last_visible_x -= 1;
2967 it->last_visible_y = window_text_bottom_y (w);
2969 /* For mode lines and alike, arrange for the first glyph having a
2970 left box line if the face specifies a box. */
2971 if (base_face_id != DEFAULT_FACE_ID)
2973 struct face *face;
2975 it->face_id = remapped_base_face_id;
2977 /* If we have a boxed mode line, make the first character appear
2978 with a left box line. */
2979 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2980 if (face && face->box != FACE_NO_BOX)
2981 it->start_of_box_run_p = true;
2984 /* If a buffer position was specified, set the iterator there,
2985 getting overlays and face properties from that position. */
2986 if (charpos >= BUF_BEG (current_buffer))
2988 it->stop_charpos = charpos;
2989 it->end_charpos = ZV;
2990 eassert (charpos == BYTE_TO_CHAR (bytepos));
2991 IT_CHARPOS (*it) = charpos;
2992 IT_BYTEPOS (*it) = bytepos;
2994 /* We will rely on `reseat' to set this up properly, via
2995 handle_face_prop. */
2996 it->face_id = it->base_face_id;
2998 it->start = it->current;
2999 /* Do we need to reorder bidirectional text? Not if this is a
3000 unibyte buffer: by definition, none of the single-byte
3001 characters are strong R2L, so no reordering is needed. And
3002 bidi.c doesn't support unibyte buffers anyway. Also, don't
3003 reorder while we are loading loadup.el, since the tables of
3004 character properties needed for reordering are not yet
3005 available. */
3006 it->bidi_p =
3007 !redisplay__inhibit_bidi
3008 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3009 && it->multibyte_p;
3011 /* If we are to reorder bidirectional text, init the bidi
3012 iterator. */
3013 if (it->bidi_p)
3015 /* Since we don't know at this point whether there will be
3016 any R2L lines in the window, we reserve space for
3017 truncation/continuation glyphs even if only the left
3018 fringe is absent. */
3019 if (base_face_id == DEFAULT_FACE_ID
3020 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3021 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3023 if (it->line_wrap == TRUNCATE)
3024 it->last_visible_x -= it->truncation_pixel_width;
3025 else
3026 it->last_visible_x -= it->continuation_pixel_width;
3028 /* Note the paragraph direction that this buffer wants to
3029 use. */
3030 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3031 Qleft_to_right))
3032 it->paragraph_embedding = L2R;
3033 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3034 Qright_to_left))
3035 it->paragraph_embedding = R2L;
3036 else
3037 it->paragraph_embedding = NEUTRAL_DIR;
3038 bidi_unshelve_cache (NULL, false);
3039 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3040 &it->bidi_it);
3043 /* Compute faces etc. */
3044 reseat (it, it->current.pos, true);
3047 CHECK_IT (it);
3051 /* Initialize IT for the display of window W with window start POS. */
3053 void
3054 start_display (struct it *it, struct window *w, struct text_pos pos)
3056 struct glyph_row *row;
3057 bool first_vpos = window_wants_header_line (w);
3059 row = w->desired_matrix->rows + first_vpos;
3060 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3061 it->first_vpos = first_vpos;
3063 /* Don't reseat to previous visible line start if current start
3064 position is in a string or image. */
3065 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3067 int first_y = it->current_y;
3069 /* If window start is not at a line start, skip forward to POS to
3070 get the correct continuation lines width. */
3071 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3072 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3073 if (!start_at_line_beg_p)
3075 int new_x;
3077 reseat_at_previous_visible_line_start (it);
3078 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3080 new_x = it->current_x + it->pixel_width;
3082 /* If lines are continued, this line may end in the middle
3083 of a multi-glyph character (e.g. a control character
3084 displayed as \003, or in the middle of an overlay
3085 string). In this case move_it_to above will not have
3086 taken us to the start of the continuation line but to the
3087 end of the continued line. */
3088 if (it->current_x > 0
3089 && it->line_wrap != TRUNCATE /* Lines are continued. */
3090 && (/* And glyph doesn't fit on the line. */
3091 new_x > it->last_visible_x
3092 /* Or it fits exactly and we're on a window
3093 system frame. */
3094 || (new_x == it->last_visible_x
3095 && FRAME_WINDOW_P (it->f)
3096 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3097 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3098 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3100 if ((it->current.dpvec_index >= 0
3101 || it->current.overlay_string_index >= 0)
3102 /* If we are on a newline from a display vector or
3103 overlay string, then we are already at the end of
3104 a screen line; no need to go to the next line in
3105 that case, as this line is not really continued.
3106 (If we do go to the next line, C-e will not DTRT.) */
3107 && it->c != '\n')
3109 set_iterator_to_next (it, true);
3110 move_it_in_display_line_to (it, -1, -1, 0);
3113 it->continuation_lines_width += it->current_x;
3115 /* If the character at POS is displayed via a display
3116 vector, move_it_to above stops at the final glyph of
3117 IT->dpvec. To make the caller redisplay that character
3118 again (a.k.a. start at POS), we need to reset the
3119 dpvec_index to the beginning of IT->dpvec. */
3120 else if (it->current.dpvec_index >= 0)
3121 it->current.dpvec_index = 0;
3123 /* We're starting a new display line, not affected by the
3124 height of the continued line, so clear the appropriate
3125 fields in the iterator structure. */
3126 it->max_ascent = it->max_descent = 0;
3127 it->max_phys_ascent = it->max_phys_descent = 0;
3129 it->current_y = first_y;
3130 it->vpos = 0;
3131 it->current_x = it->hpos = 0;
3137 /* Return true if POS is a position in ellipses displayed for invisible
3138 text. W is the window we display, for text property lookup. */
3140 static bool
3141 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3143 Lisp_Object prop, window;
3144 bool ellipses_p = false;
3145 ptrdiff_t charpos = CHARPOS (pos->pos);
3147 /* If POS specifies a position in a display vector, this might
3148 be for an ellipsis displayed for invisible text. We won't
3149 get the iterator set up for delivering that ellipsis unless
3150 we make sure that it gets aware of the invisible text. */
3151 if (pos->dpvec_index >= 0
3152 && pos->overlay_string_index < 0
3153 && CHARPOS (pos->string_pos) < 0
3154 && charpos > BEGV
3155 && (XSETWINDOW (window, w),
3156 prop = Fget_char_property (make_number (charpos),
3157 Qinvisible, window),
3158 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3160 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3161 window);
3162 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3165 return ellipses_p;
3169 /* Initialize IT for stepping through current_buffer in window W,
3170 starting at position POS that includes overlay string and display
3171 vector/ control character translation position information. Value
3172 is false if there are overlay strings with newlines at POS. */
3174 static bool
3175 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3177 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3178 int i;
3179 bool overlay_strings_with_newlines = false;
3181 /* If POS specifies a position in a display vector, this might
3182 be for an ellipsis displayed for invisible text. We won't
3183 get the iterator set up for delivering that ellipsis unless
3184 we make sure that it gets aware of the invisible text. */
3185 if (in_ellipses_for_invisible_text_p (pos, w))
3187 --charpos;
3188 bytepos = 0;
3191 /* Keep in mind: the call to reseat in init_iterator skips invisible
3192 text, so we might end up at a position different from POS. This
3193 is only a problem when POS is a row start after a newline and an
3194 overlay starts there with an after-string, and the overlay has an
3195 invisible property. Since we don't skip invisible text in
3196 display_line and elsewhere immediately after consuming the
3197 newline before the row start, such a POS will not be in a string,
3198 but the call to init_iterator below will move us to the
3199 after-string. */
3200 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3202 /* This only scans the current chunk -- it should scan all chunks.
3203 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3204 to 16 in 22.1 to make this a lesser problem. */
3205 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3207 const char *s = SSDATA (it->overlay_strings[i]);
3208 const char *e = s + SBYTES (it->overlay_strings[i]);
3210 while (s < e && *s != '\n')
3211 ++s;
3213 if (s < e)
3215 overlay_strings_with_newlines = true;
3216 break;
3220 /* If position is within an overlay string, set up IT to the right
3221 overlay string. */
3222 if (pos->overlay_string_index >= 0)
3224 int relative_index;
3226 /* If the first overlay string happens to have a `display'
3227 property for an image, the iterator will be set up for that
3228 image, and we have to undo that setup first before we can
3229 correct the overlay string index. */
3230 if (it->method == GET_FROM_IMAGE)
3231 pop_it (it);
3233 /* We already have the first chunk of overlay strings in
3234 IT->overlay_strings. Load more until the one for
3235 pos->overlay_string_index is in IT->overlay_strings. */
3236 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3238 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3239 it->current.overlay_string_index = 0;
3240 while (n--)
3242 load_overlay_strings (it, 0);
3243 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3247 it->current.overlay_string_index = pos->overlay_string_index;
3248 relative_index = (it->current.overlay_string_index
3249 % OVERLAY_STRING_CHUNK_SIZE);
3250 it->string = it->overlay_strings[relative_index];
3251 eassert (STRINGP (it->string));
3252 it->current.string_pos = pos->string_pos;
3253 it->method = GET_FROM_STRING;
3254 it->end_charpos = SCHARS (it->string);
3255 /* Set up the bidi iterator for this overlay string. */
3256 if (it->bidi_p)
3258 it->bidi_it.string.lstring = it->string;
3259 it->bidi_it.string.s = NULL;
3260 it->bidi_it.string.schars = SCHARS (it->string);
3261 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3262 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3263 it->bidi_it.string.unibyte = !it->multibyte_p;
3264 it->bidi_it.w = it->w;
3265 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3266 FRAME_WINDOW_P (it->f), &it->bidi_it);
3268 /* Synchronize the state of the bidi iterator with
3269 pos->string_pos. For any string position other than
3270 zero, this will be done automagically when we resume
3271 iteration over the string and get_visually_first_element
3272 is called. But if string_pos is zero, and the string is
3273 to be reordered for display, we need to resync manually,
3274 since it could be that the iteration state recorded in
3275 pos ended at string_pos of 0 moving backwards in string. */
3276 if (CHARPOS (pos->string_pos) == 0)
3278 get_visually_first_element (it);
3279 if (IT_STRING_CHARPOS (*it) != 0)
3280 do {
3281 /* Paranoia. */
3282 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3283 bidi_move_to_visually_next (&it->bidi_it);
3284 } while (it->bidi_it.charpos != 0);
3286 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3287 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3291 if (CHARPOS (pos->string_pos) >= 0)
3293 /* Recorded position is not in an overlay string, but in another
3294 string. This can only be a string from a `display' property.
3295 IT should already be filled with that string. */
3296 it->current.string_pos = pos->string_pos;
3297 eassert (STRINGP (it->string));
3298 if (it->bidi_p)
3299 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3300 FRAME_WINDOW_P (it->f), &it->bidi_it);
3303 /* Restore position in display vector translations, control
3304 character translations or ellipses. */
3305 if (pos->dpvec_index >= 0)
3307 if (it->dpvec == NULL)
3308 get_next_display_element (it);
3309 eassert (it->dpvec && it->current.dpvec_index == 0);
3310 it->current.dpvec_index = pos->dpvec_index;
3313 CHECK_IT (it);
3314 return !overlay_strings_with_newlines;
3318 /* Initialize IT for stepping through current_buffer in window W
3319 starting at ROW->start. */
3321 static void
3322 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3324 init_from_display_pos (it, w, &row->start);
3325 it->start = row->start;
3326 it->continuation_lines_width = row->continuation_lines_width;
3327 CHECK_IT (it);
3331 /* Initialize IT for stepping through current_buffer in window W
3332 starting in the line following ROW, i.e. starting at ROW->end.
3333 Value is false if there are overlay strings with newlines at ROW's
3334 end position. */
3336 static bool
3337 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3339 bool success = false;
3341 if (init_from_display_pos (it, w, &row->end))
3343 if (row->continued_p)
3344 it->continuation_lines_width
3345 = row->continuation_lines_width + row->pixel_width;
3346 CHECK_IT (it);
3347 success = true;
3350 return success;
3356 /***********************************************************************
3357 Text properties
3358 ***********************************************************************/
3360 /* Called when IT reaches IT->stop_charpos. Handle text property and
3361 overlay changes. Set IT->stop_charpos to the next position where
3362 to stop. */
3364 static void
3365 handle_stop (struct it *it)
3367 enum prop_handled handled;
3368 bool handle_overlay_change_p;
3369 struct props *p;
3371 it->dpvec = NULL;
3372 it->current.dpvec_index = -1;
3373 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3374 it->ellipsis_p = false;
3376 /* Use face of preceding text for ellipsis (if invisible) */
3377 if (it->selective_display_ellipsis_p)
3378 it->saved_face_id = it->face_id;
3380 /* Here's the description of the semantics of, and the logic behind,
3381 the various HANDLED_* statuses:
3383 HANDLED_NORMALLY means the handler did its job, and the loop
3384 should proceed to calling the next handler in order.
3386 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3387 change in the properties and overlays at current position, so the
3388 loop should be restarted, to re-invoke the handlers that were
3389 already called. This happens when fontification-functions were
3390 called by handle_fontified_prop, and actually fontified
3391 something. Another case where HANDLED_RECOMPUTE_PROPS is
3392 returned is when we discover overlay strings that need to be
3393 displayed right away. The loop below will continue for as long
3394 as the status is HANDLED_RECOMPUTE_PROPS.
3396 HANDLED_RETURN means return immediately to the caller, to
3397 continue iteration without calling any further handlers. This is
3398 used when we need to act on some property right away, for example
3399 when we need to display the ellipsis or a replacing display
3400 property, such as display string or image.
3402 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3403 consumed, and the handler switched to the next overlay string.
3404 This signals the loop below to refrain from looking for more
3405 overlays before all the overlay strings of the current overlay
3406 are processed.
3408 Some of the handlers called by the loop push the iterator state
3409 onto the stack (see 'push_it'), and arrange for the iteration to
3410 continue with another object, such as an image, a display string,
3411 or an overlay string. In most such cases, it->stop_charpos is
3412 set to the first character of the string, so that when the
3413 iteration resumes, this function will immediately be called
3414 again, to examine the properties at the beginning of the string.
3416 When a display or overlay string is exhausted, the iterator state
3417 is popped (see 'pop_it'), and iteration continues with the
3418 previous object. Again, in many such cases this function is
3419 called again to find the next position where properties might
3420 change. */
3424 handled = HANDLED_NORMALLY;
3426 /* Call text property handlers. */
3427 for (p = it_props; p->handler; ++p)
3429 handled = p->handler (it);
3431 if (handled == HANDLED_RECOMPUTE_PROPS)
3432 break;
3433 else if (handled == HANDLED_RETURN)
3435 /* We still want to show before and after strings from
3436 overlays even if the actual buffer text is replaced. */
3437 if (!handle_overlay_change_p
3438 || it->sp > 1
3439 /* Don't call get_overlay_strings_1 if we already
3440 have overlay strings loaded, because doing so
3441 will load them again and push the iterator state
3442 onto the stack one more time, which is not
3443 expected by the rest of the code that processes
3444 overlay strings. */
3445 || (it->current.overlay_string_index < 0
3446 && !get_overlay_strings_1 (it, 0, false)))
3448 if (it->ellipsis_p)
3449 setup_for_ellipsis (it, 0);
3450 /* When handling a display spec, we might load an
3451 empty string. In that case, discard it here. We
3452 used to discard it in handle_single_display_spec,
3453 but that causes get_overlay_strings_1, above, to
3454 ignore overlay strings that we must check. */
3455 if (STRINGP (it->string) && !SCHARS (it->string))
3456 pop_it (it);
3457 return;
3459 else if (STRINGP (it->string) && !SCHARS (it->string))
3460 pop_it (it);
3461 else
3463 it->string_from_display_prop_p = false;
3464 it->from_disp_prop_p = false;
3465 handle_overlay_change_p = false;
3467 handled = HANDLED_RECOMPUTE_PROPS;
3468 break;
3470 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3471 handle_overlay_change_p = false;
3474 if (handled != HANDLED_RECOMPUTE_PROPS)
3476 /* Don't check for overlay strings below when set to deliver
3477 characters from a display vector. */
3478 if (it->method == GET_FROM_DISPLAY_VECTOR)
3479 handle_overlay_change_p = false;
3481 /* Handle overlay changes.
3482 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3483 if it finds overlays. */
3484 if (handle_overlay_change_p)
3485 handled = handle_overlay_change (it);
3488 if (it->ellipsis_p)
3490 setup_for_ellipsis (it, 0);
3491 break;
3494 while (handled == HANDLED_RECOMPUTE_PROPS);
3496 /* Determine where to stop next. */
3497 if (handled == HANDLED_NORMALLY)
3498 compute_stop_pos (it);
3502 /* Compute IT->stop_charpos from text property and overlay change
3503 information for IT's current position. */
3505 static void
3506 compute_stop_pos (struct it *it)
3508 register INTERVAL iv, next_iv;
3509 Lisp_Object object, limit, position;
3510 ptrdiff_t charpos, bytepos;
3512 if (STRINGP (it->string))
3514 /* Strings are usually short, so don't limit the search for
3515 properties. */
3516 it->stop_charpos = it->end_charpos;
3517 object = it->string;
3518 limit = Qnil;
3519 charpos = IT_STRING_CHARPOS (*it);
3520 bytepos = IT_STRING_BYTEPOS (*it);
3522 else
3524 ptrdiff_t pos;
3526 /* If end_charpos is out of range for some reason, such as a
3527 misbehaving display function, rationalize it (Bug#5984). */
3528 if (it->end_charpos > ZV)
3529 it->end_charpos = ZV;
3530 it->stop_charpos = it->end_charpos;
3532 /* If next overlay change is in front of the current stop pos
3533 (which is IT->end_charpos), stop there. Note: value of
3534 next_overlay_change is point-max if no overlay change
3535 follows. */
3536 charpos = IT_CHARPOS (*it);
3537 bytepos = IT_BYTEPOS (*it);
3538 pos = next_overlay_change (charpos);
3539 if (pos < it->stop_charpos)
3540 it->stop_charpos = pos;
3542 /* Set up variables for computing the stop position from text
3543 property changes. */
3544 XSETBUFFER (object, current_buffer);
3545 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3548 /* Get the interval containing IT's position. Value is a null
3549 interval if there isn't such an interval. */
3550 position = make_number (charpos);
3551 iv = validate_interval_range (object, &position, &position, false);
3552 if (iv)
3554 Lisp_Object values_here[LAST_PROP_IDX];
3555 struct props *p;
3557 /* Get properties here. */
3558 for (p = it_props; p->handler; ++p)
3559 values_here[p->idx] = textget (iv->plist,
3560 builtin_lisp_symbol (p->name));
3562 /* Look for an interval following iv that has different
3563 properties. */
3564 for (next_iv = next_interval (iv);
3565 (next_iv
3566 && (NILP (limit)
3567 || XFASTINT (limit) > next_iv->position));
3568 next_iv = next_interval (next_iv))
3570 for (p = it_props; p->handler; ++p)
3572 Lisp_Object new_value = textget (next_iv->plist,
3573 builtin_lisp_symbol (p->name));
3574 if (!EQ (values_here[p->idx], new_value))
3575 break;
3578 if (p->handler)
3579 break;
3582 if (next_iv)
3584 if (INTEGERP (limit)
3585 && next_iv->position >= XFASTINT (limit))
3586 /* No text property change up to limit. */
3587 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3588 else
3589 /* Text properties change in next_iv. */
3590 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3594 if (it->cmp_it.id < 0)
3596 ptrdiff_t stoppos = it->end_charpos;
3598 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3599 stoppos = -1;
3600 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3601 stoppos, it->string);
3604 eassert (STRINGP (it->string)
3605 || (it->stop_charpos >= BEGV
3606 && it->stop_charpos >= IT_CHARPOS (*it)));
3610 /* Return the position of the next overlay change after POS in
3611 current_buffer. Value is point-max if no overlay change
3612 follows. This is like `next-overlay-change' but doesn't use
3613 xmalloc. */
3615 static ptrdiff_t
3616 next_overlay_change (ptrdiff_t pos)
3618 ptrdiff_t i, noverlays;
3619 ptrdiff_t endpos;
3620 Lisp_Object *overlays;
3621 USE_SAFE_ALLOCA;
3623 /* Get all overlays at the given position. */
3624 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3626 /* If any of these overlays ends before endpos,
3627 use its ending point instead. */
3628 for (i = 0; i < noverlays; ++i)
3630 Lisp_Object oend;
3631 ptrdiff_t oendpos;
3633 oend = OVERLAY_END (overlays[i]);
3634 oendpos = OVERLAY_POSITION (oend);
3635 endpos = min (endpos, oendpos);
3638 SAFE_FREE ();
3639 return endpos;
3642 /* How many characters forward to search for a display property or
3643 display string. Searching too far forward makes the bidi display
3644 sluggish, especially in small windows. */
3645 #define MAX_DISP_SCAN 250
3647 /* Return the character position of a display string at or after
3648 position specified by POSITION. If no display string exists at or
3649 after POSITION, return ZV. A display string is either an overlay
3650 with `display' property whose value is a string, or a `display'
3651 text property whose value is a string. STRING is data about the
3652 string to iterate; if STRING->lstring is nil, we are iterating a
3653 buffer. FRAME_WINDOW_P is true when we are displaying a window
3654 on a GUI frame. DISP_PROP is set to zero if we searched
3655 MAX_DISP_SCAN characters forward without finding any display
3656 strings, non-zero otherwise. It is set to 2 if the display string
3657 uses any kind of `(space ...)' spec that will produce a stretch of
3658 white space in the text area. */
3659 ptrdiff_t
3660 compute_display_string_pos (struct text_pos *position,
3661 struct bidi_string_data *string,
3662 struct window *w,
3663 bool frame_window_p, int *disp_prop)
3665 /* OBJECT = nil means current buffer. */
3666 Lisp_Object object, object1;
3667 Lisp_Object pos, spec, limpos;
3668 bool string_p = string && (STRINGP (string->lstring) || string->s);
3669 ptrdiff_t eob = string_p ? string->schars : ZV;
3670 ptrdiff_t begb = string_p ? 0 : BEGV;
3671 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3672 ptrdiff_t lim =
3673 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3674 struct text_pos tpos;
3675 int rv = 0;
3677 if (string && STRINGP (string->lstring))
3678 object1 = object = string->lstring;
3679 else if (w && !string_p)
3681 XSETWINDOW (object, w);
3682 object1 = Qnil;
3684 else
3685 object1 = object = Qnil;
3687 *disp_prop = 1;
3689 if (charpos >= eob
3690 /* We don't support display properties whose values are strings
3691 that have display string properties. */
3692 || string->from_disp_str
3693 /* C strings cannot have display properties. */
3694 || (string->s && !STRINGP (object)))
3696 *disp_prop = 0;
3697 return eob;
3700 /* If the character at CHARPOS is where the display string begins,
3701 return CHARPOS. */
3702 pos = make_number (charpos);
3703 if (STRINGP (object))
3704 bufpos = string->bufpos;
3705 else
3706 bufpos = charpos;
3707 tpos = *position;
3708 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3709 && (charpos <= begb
3710 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3711 object),
3712 spec))
3713 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3714 frame_window_p)))
3716 if (rv == 2)
3717 *disp_prop = 2;
3718 return charpos;
3721 /* Look forward for the first character with a `display' property
3722 that will replace the underlying text when displayed. */
3723 limpos = make_number (lim);
3724 do {
3725 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3726 CHARPOS (tpos) = XFASTINT (pos);
3727 if (CHARPOS (tpos) >= lim)
3729 *disp_prop = 0;
3730 break;
3732 if (STRINGP (object))
3733 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3734 else
3735 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3736 spec = Fget_char_property (pos, Qdisplay, object);
3737 if (!STRINGP (object))
3738 bufpos = CHARPOS (tpos);
3739 } while (NILP (spec)
3740 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3741 bufpos, frame_window_p)));
3742 if (rv == 2)
3743 *disp_prop = 2;
3745 return CHARPOS (tpos);
3748 /* Return the character position of the end of the display string that
3749 started at CHARPOS. If there's no display string at CHARPOS,
3750 return -1. A display string is either an overlay with `display'
3751 property whose value is a string or a `display' text property whose
3752 value is a string. */
3753 ptrdiff_t
3754 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3756 /* OBJECT = nil means current buffer. */
3757 Lisp_Object object =
3758 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3759 Lisp_Object pos = make_number (charpos);
3760 ptrdiff_t eob =
3761 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3763 if (charpos >= eob || (string->s && !STRINGP (object)))
3764 return eob;
3766 /* It could happen that the display property or overlay was removed
3767 since we found it in compute_display_string_pos above. One way
3768 this can happen is if JIT font-lock was called (through
3769 handle_fontified_prop), and jit-lock-functions remove text
3770 properties or overlays from the portion of buffer that includes
3771 CHARPOS. Muse mode is known to do that, for example. In this
3772 case, we return -1 to the caller, to signal that no display
3773 string is actually present at CHARPOS. See bidi_fetch_char for
3774 how this is handled.
3776 An alternative would be to never look for display properties past
3777 it->stop_charpos. But neither compute_display_string_pos nor
3778 bidi_fetch_char that calls it know or care where the next
3779 stop_charpos is. */
3780 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3781 return -1;
3783 /* Look forward for the first character where the `display' property
3784 changes. */
3785 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3787 return XFASTINT (pos);
3792 /***********************************************************************
3793 Fontification
3794 ***********************************************************************/
3796 /* Handle changes in the `fontified' property of the current buffer by
3797 calling hook functions from Qfontification_functions to fontify
3798 regions of text. */
3800 static enum prop_handled
3801 handle_fontified_prop (struct it *it)
3803 Lisp_Object prop, pos;
3804 enum prop_handled handled = HANDLED_NORMALLY;
3806 if (!NILP (Vmemory_full))
3807 return handled;
3809 /* Get the value of the `fontified' property at IT's current buffer
3810 position. (The `fontified' property doesn't have a special
3811 meaning in strings.) If the value is nil, call functions from
3812 Qfontification_functions. */
3813 if (!STRINGP (it->string)
3814 && it->s == NULL
3815 && !NILP (Vfontification_functions)
3816 && !NILP (Vrun_hooks)
3817 && (pos = make_number (IT_CHARPOS (*it)),
3818 prop = Fget_char_property (pos, Qfontified, Qnil),
3819 /* Ignore the special cased nil value always present at EOB since
3820 no amount of fontifying will be able to change it. */
3821 NILP (prop) && IT_CHARPOS (*it) < Z))
3823 ptrdiff_t count = SPECPDL_INDEX ();
3824 Lisp_Object val;
3825 struct buffer *obuf = current_buffer;
3826 ptrdiff_t begv = BEGV, zv = ZV;
3827 bool old_clip_changed = current_buffer->clip_changed;
3829 val = Vfontification_functions;
3830 specbind (Qfontification_functions, Qnil);
3832 eassert (it->end_charpos == ZV);
3834 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3835 safe_call1 (val, pos);
3836 else
3838 Lisp_Object fns, fn;
3840 fns = Qnil;
3842 for (; CONSP (val); val = XCDR (val))
3844 fn = XCAR (val);
3846 if (EQ (fn, Qt))
3848 /* A value of t indicates this hook has a local
3849 binding; it means to run the global binding too.
3850 In a global value, t should not occur. If it
3851 does, we must ignore it to avoid an endless
3852 loop. */
3853 for (fns = Fdefault_value (Qfontification_functions);
3854 CONSP (fns);
3855 fns = XCDR (fns))
3857 fn = XCAR (fns);
3858 if (!EQ (fn, Qt))
3859 safe_call1 (fn, pos);
3862 else
3863 safe_call1 (fn, pos);
3867 unbind_to (count, Qnil);
3869 /* Fontification functions routinely call `save-restriction'.
3870 Normally, this tags clip_changed, which can confuse redisplay
3871 (see discussion in Bug#6671). Since we don't perform any
3872 special handling of fontification changes in the case where
3873 `save-restriction' isn't called, there's no point doing so in
3874 this case either. So, if the buffer's restrictions are
3875 actually left unchanged, reset clip_changed. */
3876 if (obuf == current_buffer)
3878 if (begv == BEGV && zv == ZV)
3879 current_buffer->clip_changed = old_clip_changed;
3881 /* There isn't much we can reasonably do to protect against
3882 misbehaving fontification, but here's a fig leaf. */
3883 else if (BUFFER_LIVE_P (obuf))
3884 set_buffer_internal_1 (obuf);
3886 /* The fontification code may have added/removed text.
3887 It could do even a lot worse, but let's at least protect against
3888 the most obvious case where only the text past `pos' gets changed',
3889 as is/was done in grep.el where some escapes sequences are turned
3890 into face properties (bug#7876). */
3891 it->end_charpos = ZV;
3893 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3894 something. This avoids an endless loop if they failed to
3895 fontify the text for which reason ever. */
3896 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3897 handled = HANDLED_RECOMPUTE_PROPS;
3900 return handled;
3905 /***********************************************************************
3906 Faces
3907 ***********************************************************************/
3909 /* Set up iterator IT from face properties at its current position.
3910 Called from handle_stop. */
3912 static enum prop_handled
3913 handle_face_prop (struct it *it)
3915 int new_face_id;
3916 ptrdiff_t next_stop;
3918 if (!STRINGP (it->string))
3920 new_face_id
3921 = face_at_buffer_position (it->w,
3922 IT_CHARPOS (*it),
3923 &next_stop,
3924 (IT_CHARPOS (*it)
3925 + TEXT_PROP_DISTANCE_LIMIT),
3926 false, it->base_face_id);
3928 /* Is this a start of a run of characters with box face?
3929 Caveat: this can be called for a freshly initialized
3930 iterator; face_id is -1 in this case. We know that the new
3931 face will not change until limit, i.e. if the new face has a
3932 box, all characters up to limit will have one. But, as
3933 usual, we don't know whether limit is really the end. */
3934 if (new_face_id != it->face_id)
3936 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3937 /* If it->face_id is -1, old_face below will be NULL, see
3938 the definition of FACE_FROM_ID_OR_NULL. This will happen
3939 if this is the initial call that gets the face. */
3940 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3942 /* If the value of face_id of the iterator is -1, we have to
3943 look in front of IT's position and see whether there is a
3944 face there that's different from new_face_id. */
3945 if (!old_face && IT_CHARPOS (*it) > BEG)
3947 int prev_face_id = face_before_it_pos (it);
3949 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3952 /* If the new face has a box, but the old face does not,
3953 this is the start of a run of characters with box face,
3954 i.e. this character has a shadow on the left side. */
3955 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3956 && (old_face == NULL || !old_face->box));
3957 it->face_box_p = new_face->box != FACE_NO_BOX;
3960 else
3962 int base_face_id;
3963 ptrdiff_t bufpos;
3964 int i;
3965 Lisp_Object from_overlay
3966 = (it->current.overlay_string_index >= 0
3967 ? it->string_overlays[it->current.overlay_string_index
3968 % OVERLAY_STRING_CHUNK_SIZE]
3969 : Qnil);
3971 /* See if we got to this string directly or indirectly from
3972 an overlay property. That includes the before-string or
3973 after-string of an overlay, strings in display properties
3974 provided by an overlay, their text properties, etc.
3976 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3977 if (! NILP (from_overlay))
3978 for (i = it->sp - 1; i >= 0; i--)
3980 if (it->stack[i].current.overlay_string_index >= 0)
3981 from_overlay
3982 = it->string_overlays[it->stack[i].current.overlay_string_index
3983 % OVERLAY_STRING_CHUNK_SIZE];
3984 else if (! NILP (it->stack[i].from_overlay))
3985 from_overlay = it->stack[i].from_overlay;
3987 if (!NILP (from_overlay))
3988 break;
3991 if (! NILP (from_overlay))
3993 bufpos = IT_CHARPOS (*it);
3994 /* For a string from an overlay, the base face depends
3995 only on text properties and ignores overlays. */
3996 base_face_id
3997 = face_for_overlay_string (it->w,
3998 IT_CHARPOS (*it),
3999 &next_stop,
4000 (IT_CHARPOS (*it)
4001 + TEXT_PROP_DISTANCE_LIMIT),
4002 false,
4003 from_overlay);
4005 else
4007 bufpos = 0;
4009 /* For strings from a `display' property, use the face at
4010 IT's current buffer position as the base face to merge
4011 with, so that overlay strings appear in the same face as
4012 surrounding text, unless they specify their own faces.
4013 For strings from wrap-prefix and line-prefix properties,
4014 use the default face, possibly remapped via
4015 Vface_remapping_alist. */
4016 /* Note that the fact that we use the face at _buffer_
4017 position means that a 'display' property on an overlay
4018 string will not inherit the face of that overlay string,
4019 but will instead revert to the face of buffer text
4020 covered by the overlay. This is visible, e.g., when the
4021 overlay specifies a box face, but neither the buffer nor
4022 the display string do. This sounds like a design bug,
4023 but Emacs always did that since v21.1, so changing that
4024 might be a big deal. */
4025 base_face_id = it->string_from_prefix_prop_p
4026 ? (!NILP (Vface_remapping_alist)
4027 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4028 : DEFAULT_FACE_ID)
4029 : underlying_face_id (it);
4032 new_face_id = face_at_string_position (it->w,
4033 it->string,
4034 IT_STRING_CHARPOS (*it),
4035 bufpos,
4036 &next_stop,
4037 base_face_id, false);
4039 /* Is this a start of a run of characters with box? Caveat:
4040 this can be called for a freshly allocated iterator; face_id
4041 is -1 is this case. We know that the new face will not
4042 change until the next check pos, i.e. if the new face has a
4043 box, all characters up to that position will have a
4044 box. But, as usual, we don't know whether that position
4045 is really the end. */
4046 if (new_face_id != it->face_id)
4048 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4049 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4051 /* If new face has a box but old face hasn't, this is the
4052 start of a run of characters with box, i.e. it has a
4053 shadow on the left side. */
4054 it->start_of_box_run_p
4055 = new_face->box && (old_face == NULL || !old_face->box);
4056 it->face_box_p = new_face->box != FACE_NO_BOX;
4060 it->face_id = new_face_id;
4061 return HANDLED_NORMALLY;
4065 /* Return the ID of the face ``underlying'' IT's current position,
4066 which is in a string. If the iterator is associated with a
4067 buffer, return the face at IT's current buffer position.
4068 Otherwise, use the iterator's base_face_id. */
4070 static int
4071 underlying_face_id (struct it *it)
4073 int face_id = it->base_face_id, i;
4075 eassert (STRINGP (it->string));
4077 for (i = it->sp - 1; i >= 0; --i)
4078 if (NILP (it->stack[i].string))
4079 face_id = it->stack[i].face_id;
4081 return face_id;
4085 /* Compute the face one character before or after the current position
4086 of IT, in the visual order. BEFORE_P means get the face
4087 in front (to the left in L2R paragraphs, to the right in R2L
4088 paragraphs) of IT's screen position. Value is the ID of the face. */
4090 static int
4091 face_before_or_after_it_pos (struct it *it, bool before_p)
4093 int face_id, limit;
4094 ptrdiff_t next_check_charpos;
4095 struct it it_copy;
4096 void *it_copy_data = NULL;
4098 eassert (it->s == NULL);
4100 if (STRINGP (it->string))
4102 ptrdiff_t bufpos, charpos;
4103 int base_face_id;
4105 /* No face change past the end of the string (for the case
4106 we are padding with spaces). No face change before the
4107 string start. */
4108 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4109 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4110 return it->face_id;
4112 if (!it->bidi_p)
4114 /* Set charpos to the position before or after IT's current
4115 position, in the logical order, which in the non-bidi
4116 case is the same as the visual order. */
4117 if (before_p)
4118 charpos = IT_STRING_CHARPOS (*it) - 1;
4119 else if (it->what == IT_COMPOSITION)
4120 /* For composition, we must check the character after the
4121 composition. */
4122 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4123 else
4124 charpos = IT_STRING_CHARPOS (*it) + 1;
4126 else
4128 if (before_p)
4130 /* With bidi iteration, the character before the current
4131 in the visual order cannot be found by simple
4132 iteration, because "reverse" reordering is not
4133 supported. Instead, we need to start from the string
4134 beginning and go all the way to the current string
4135 position, remembering the previous position. */
4136 /* Ignore face changes before the first visible
4137 character on this display line. */
4138 if (it->current_x <= it->first_visible_x)
4139 return it->face_id;
4140 SAVE_IT (it_copy, *it, it_copy_data);
4141 IT_STRING_CHARPOS (it_copy) = 0;
4142 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4146 charpos = IT_STRING_CHARPOS (it_copy);
4147 if (charpos >= SCHARS (it->string))
4148 break;
4149 bidi_move_to_visually_next (&it_copy.bidi_it);
4151 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4153 RESTORE_IT (it, it, it_copy_data);
4155 else
4157 /* Set charpos to the string position of the character
4158 that comes after IT's current position in the visual
4159 order. */
4160 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4162 it_copy = *it;
4163 while (n--)
4164 bidi_move_to_visually_next (&it_copy.bidi_it);
4166 charpos = it_copy.bidi_it.charpos;
4169 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4171 if (it->current.overlay_string_index >= 0)
4172 bufpos = IT_CHARPOS (*it);
4173 else
4174 bufpos = 0;
4176 base_face_id = underlying_face_id (it);
4178 /* Get the face for ASCII, or unibyte. */
4179 face_id = face_at_string_position (it->w,
4180 it->string,
4181 charpos,
4182 bufpos,
4183 &next_check_charpos,
4184 base_face_id, false);
4186 /* Correct the face for charsets different from ASCII. Do it
4187 for the multibyte case only. The face returned above is
4188 suitable for unibyte text if IT->string is unibyte. */
4189 if (STRING_MULTIBYTE (it->string))
4191 struct text_pos pos1 = string_pos (charpos, it->string);
4192 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4193 int c, len;
4194 struct face *face = FACE_FROM_ID (it->f, face_id);
4196 c = string_char_and_length (p, &len);
4197 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4200 else
4202 struct text_pos pos;
4204 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4205 || (IT_CHARPOS (*it) <= BEGV && before_p))
4206 return it->face_id;
4208 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4209 pos = it->current.pos;
4211 if (!it->bidi_p)
4213 if (before_p)
4214 DEC_TEXT_POS (pos, it->multibyte_p);
4215 else
4217 if (it->what == IT_COMPOSITION)
4219 /* For composition, we must check the position after
4220 the composition. */
4221 pos.charpos += it->cmp_it.nchars;
4222 pos.bytepos += it->len;
4224 else
4225 INC_TEXT_POS (pos, it->multibyte_p);
4228 else
4230 if (before_p)
4232 int current_x;
4234 /* With bidi iteration, the character before the current
4235 in the visual order cannot be found by simple
4236 iteration, because "reverse" reordering is not
4237 supported. Instead, we need to use the move_it_*
4238 family of functions, and move to the previous
4239 character starting from the beginning of the visual
4240 line. */
4241 /* Ignore face changes before the first visible
4242 character on this display line. */
4243 if (it->current_x <= it->first_visible_x)
4244 return it->face_id;
4245 SAVE_IT (it_copy, *it, it_copy_data);
4246 /* Implementation note: Since move_it_in_display_line
4247 works in the iterator geometry, and thinks the first
4248 character is always the leftmost, even in R2L lines,
4249 we don't need to distinguish between the R2L and L2R
4250 cases here. */
4251 current_x = it_copy.current_x;
4252 move_it_vertically_backward (&it_copy, 0);
4253 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4254 pos = it_copy.current.pos;
4255 RESTORE_IT (it, it, it_copy_data);
4257 else
4259 /* Set charpos to the buffer position of the character
4260 that comes after IT's current position in the visual
4261 order. */
4262 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4264 it_copy = *it;
4265 while (n--)
4266 bidi_move_to_visually_next (&it_copy.bidi_it);
4268 SET_TEXT_POS (pos,
4269 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4272 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4274 /* Determine face for CHARSET_ASCII, or unibyte. */
4275 face_id = face_at_buffer_position (it->w,
4276 CHARPOS (pos),
4277 &next_check_charpos,
4278 limit, false, -1);
4280 /* Correct the face for charsets different from ASCII. Do it
4281 for the multibyte case only. The face returned above is
4282 suitable for unibyte text if current_buffer is unibyte. */
4283 if (it->multibyte_p)
4285 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4286 struct face *face = FACE_FROM_ID (it->f, face_id);
4287 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4291 return face_id;
4296 /***********************************************************************
4297 Invisible text
4298 ***********************************************************************/
4300 /* Set up iterator IT from invisible properties at its current
4301 position. Called from handle_stop. */
4303 static enum prop_handled
4304 handle_invisible_prop (struct it *it)
4306 enum prop_handled handled = HANDLED_NORMALLY;
4307 int invis;
4308 Lisp_Object prop;
4310 if (STRINGP (it->string))
4312 Lisp_Object end_charpos, limit;
4314 /* Get the value of the invisible text property at the
4315 current position. Value will be nil if there is no such
4316 property. */
4317 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4318 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4319 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4321 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4323 /* Record whether we have to display an ellipsis for the
4324 invisible text. */
4325 bool display_ellipsis_p = (invis == 2);
4326 ptrdiff_t len, endpos;
4328 handled = HANDLED_RECOMPUTE_PROPS;
4330 /* Get the position at which the next visible text can be
4331 found in IT->string, if any. */
4332 endpos = len = SCHARS (it->string);
4333 XSETINT (limit, len);
4336 end_charpos
4337 = Fnext_single_property_change (end_charpos, Qinvisible,
4338 it->string, limit);
4339 /* Since LIMIT is always an integer, so should be the
4340 value returned by Fnext_single_property_change. */
4341 eassert (INTEGERP (end_charpos));
4342 if (INTEGERP (end_charpos))
4344 endpos = XFASTINT (end_charpos);
4345 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4346 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4347 if (invis == 2)
4348 display_ellipsis_p = true;
4350 else /* Should never happen; but if it does, exit the loop. */
4351 endpos = len;
4353 while (invis != 0 && endpos < len);
4355 if (display_ellipsis_p)
4356 it->ellipsis_p = true;
4358 if (endpos < len)
4360 /* Text at END_CHARPOS is visible. Move IT there. */
4361 struct text_pos old;
4362 ptrdiff_t oldpos;
4364 old = it->current.string_pos;
4365 oldpos = CHARPOS (old);
4366 if (it->bidi_p)
4368 if (it->bidi_it.first_elt
4369 && it->bidi_it.charpos < SCHARS (it->string))
4370 bidi_paragraph_init (it->paragraph_embedding,
4371 &it->bidi_it, true);
4372 /* Bidi-iterate out of the invisible text. */
4375 bidi_move_to_visually_next (&it->bidi_it);
4377 while (oldpos <= it->bidi_it.charpos
4378 && it->bidi_it.charpos < endpos
4379 && it->bidi_it.charpos < it->bidi_it.string.schars);
4381 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4383 if (IT_CHARPOS (*it) >= endpos)
4384 it->prev_stop = endpos;
4386 else
4388 IT_STRING_CHARPOS (*it) = endpos;
4389 compute_string_pos (&it->current.string_pos, old, it->string);
4392 else
4394 /* The rest of the string is invisible. If this is an
4395 overlay string, proceed with the next overlay string
4396 or whatever comes and return a character from there. */
4397 if (it->current.overlay_string_index >= 0
4398 && !display_ellipsis_p)
4400 next_overlay_string (it);
4401 /* Don't check for overlay strings when we just
4402 finished processing them. */
4403 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4405 else
4407 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4408 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4413 else
4415 ptrdiff_t newpos, next_stop, start_charpos, tem;
4416 Lisp_Object pos, overlay;
4418 /* First of all, is there invisible text at this position? */
4419 tem = start_charpos = IT_CHARPOS (*it);
4420 pos = make_number (tem);
4421 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4422 &overlay);
4423 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4425 /* If we are on invisible text, skip over it. */
4426 if (invis != 0 && start_charpos < it->end_charpos)
4428 /* Record whether we have to display an ellipsis for the
4429 invisible text. */
4430 bool display_ellipsis_p = invis == 2;
4432 handled = HANDLED_RECOMPUTE_PROPS;
4434 /* Loop skipping over invisible text. The loop is left at
4435 ZV or with IT on the first char being visible again. */
4438 /* Try to skip some invisible text. Return value is the
4439 position reached which can be equal to where we start
4440 if there is nothing invisible there. This skips both
4441 over invisible text properties and overlays with
4442 invisible property. */
4443 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4445 /* If we skipped nothing at all we weren't at invisible
4446 text in the first place. If everything to the end of
4447 the buffer was skipped, end the loop. */
4448 if (newpos == tem || newpos >= ZV)
4449 invis = 0;
4450 else
4452 /* We skipped some characters but not necessarily
4453 all there are. Check if we ended up on visible
4454 text. Fget_char_property returns the property of
4455 the char before the given position, i.e. if we
4456 get invis = 0, this means that the char at
4457 newpos is visible. */
4458 pos = make_number (newpos);
4459 prop = Fget_char_property (pos, Qinvisible, it->window);
4460 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4463 /* If we ended up on invisible text, proceed to
4464 skip starting with next_stop. */
4465 if (invis != 0)
4466 tem = next_stop;
4468 /* If there are adjacent invisible texts, don't lose the
4469 second one's ellipsis. */
4470 if (invis == 2)
4471 display_ellipsis_p = true;
4473 while (invis != 0);
4475 /* The position newpos is now either ZV or on visible text. */
4476 if (it->bidi_p)
4478 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4479 bool on_newline
4480 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4481 bool after_newline
4482 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4484 /* If the invisible text ends on a newline or on a
4485 character after a newline, we can avoid the costly,
4486 character by character, bidi iteration to NEWPOS, and
4487 instead simply reseat the iterator there. That's
4488 because all bidi reordering information is tossed at
4489 the newline. This is a big win for modes that hide
4490 complete lines, like Outline, Org, etc. */
4491 if (on_newline || after_newline)
4493 struct text_pos tpos;
4494 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4496 SET_TEXT_POS (tpos, newpos, bpos);
4497 reseat_1 (it, tpos, false);
4498 /* If we reseat on a newline/ZV, we need to prep the
4499 bidi iterator for advancing to the next character
4500 after the newline/EOB, keeping the current paragraph
4501 direction (so that PRODUCE_GLYPHS does TRT wrt
4502 prepending/appending glyphs to a glyph row). */
4503 if (on_newline)
4505 it->bidi_it.first_elt = false;
4506 it->bidi_it.paragraph_dir = pdir;
4507 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4508 it->bidi_it.nchars = 1;
4509 it->bidi_it.ch_len = 1;
4512 else /* Must use the slow method. */
4514 /* With bidi iteration, the region of invisible text
4515 could start and/or end in the middle of a
4516 non-base embedding level. Therefore, we need to
4517 skip invisible text using the bidi iterator,
4518 starting at IT's current position, until we find
4519 ourselves outside of the invisible text.
4520 Skipping invisible text _after_ bidi iteration
4521 avoids affecting the visual order of the
4522 displayed text when invisible properties are
4523 added or removed. */
4524 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4526 /* If we were `reseat'ed to a new paragraph,
4527 determine the paragraph base direction. We
4528 need to do it now because
4529 next_element_from_buffer may not have a
4530 chance to do it, if we are going to skip any
4531 text at the beginning, which resets the
4532 FIRST_ELT flag. */
4533 bidi_paragraph_init (it->paragraph_embedding,
4534 &it->bidi_it, true);
4538 bidi_move_to_visually_next (&it->bidi_it);
4540 while (it->stop_charpos <= it->bidi_it.charpos
4541 && it->bidi_it.charpos < newpos);
4542 IT_CHARPOS (*it) = it->bidi_it.charpos;
4543 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4544 /* If we overstepped NEWPOS, record its position in
4545 the iterator, so that we skip invisible text if
4546 later the bidi iteration lands us in the
4547 invisible region again. */
4548 if (IT_CHARPOS (*it) >= newpos)
4549 it->prev_stop = newpos;
4552 else
4554 IT_CHARPOS (*it) = newpos;
4555 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4558 if (display_ellipsis_p)
4560 /* Make sure that the glyphs of the ellipsis will get
4561 correct `charpos' values. If we would not update
4562 it->position here, the glyphs would belong to the
4563 last visible character _before_ the invisible
4564 text, which confuses `set_cursor_from_row'.
4566 We use the last invisible position instead of the
4567 first because this way the cursor is always drawn on
4568 the first "." of the ellipsis, whenever PT is inside
4569 the invisible text. Otherwise the cursor would be
4570 placed _after_ the ellipsis when the point is after the
4571 first invisible character. */
4572 if (!STRINGP (it->object))
4574 it->position.charpos = newpos - 1;
4575 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4579 /* If there are before-strings at the start of invisible
4580 text, and the text is invisible because of a text
4581 property, arrange to show before-strings because 20.x did
4582 it that way. (If the text is invisible because of an
4583 overlay property instead of a text property, this is
4584 already handled in the overlay code.) */
4585 if (NILP (overlay)
4586 && get_overlay_strings (it, it->stop_charpos))
4588 handled = HANDLED_RECOMPUTE_PROPS;
4589 if (it->sp > 0)
4591 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4592 /* The call to get_overlay_strings above recomputes
4593 it->stop_charpos, but it only considers changes
4594 in properties and overlays beyond iterator's
4595 current position. This causes us to miss changes
4596 that happen exactly where the invisible property
4597 ended. So we play it safe here and force the
4598 iterator to check for potential stop positions
4599 immediately after the invisible text. Note that
4600 if get_overlay_strings returns true, it
4601 normally also pushed the iterator stack, so we
4602 need to update the stop position in the slot
4603 below the current one. */
4604 it->stack[it->sp - 1].stop_charpos
4605 = CHARPOS (it->stack[it->sp - 1].current.pos);
4608 else if (display_ellipsis_p)
4610 it->ellipsis_p = true;
4611 /* Let the ellipsis display before
4612 considering any properties of the following char.
4613 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4614 handled = HANDLED_RETURN;
4619 return handled;
4623 /* Make iterator IT return `...' next.
4624 Replaces LEN characters from buffer. */
4626 static void
4627 setup_for_ellipsis (struct it *it, int len)
4629 /* Use the display table definition for `...'. Invalid glyphs
4630 will be handled by the method returning elements from dpvec. */
4631 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4633 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4634 it->dpvec = v->contents;
4635 it->dpend = v->contents + v->header.size;
4637 else
4639 /* Default `...'. */
4640 it->dpvec = default_invis_vector;
4641 it->dpend = default_invis_vector + 3;
4644 it->dpvec_char_len = len;
4645 it->current.dpvec_index = 0;
4646 it->dpvec_face_id = -1;
4648 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4649 face as the preceding text. IT->saved_face_id was set in
4650 handle_stop to the face of the preceding character, and will be
4651 different from IT->face_id only if the invisible text skipped in
4652 handle_invisible_prop has some non-default face on its first
4653 character. We thus ignore the face of the invisible text when we
4654 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4655 if (it->saved_face_id >= 0)
4656 it->face_id = it->saved_face_id;
4658 /* If the ellipsis represents buffer text, it means we advanced in
4659 the buffer, so we should no longer ignore overlay strings. */
4660 if (it->method == GET_FROM_BUFFER)
4661 it->ignore_overlay_strings_at_pos_p = false;
4663 it->method = GET_FROM_DISPLAY_VECTOR;
4664 it->ellipsis_p = true;
4669 /***********************************************************************
4670 'display' property
4671 ***********************************************************************/
4673 /* Set up iterator IT from `display' property at its current position.
4674 Called from handle_stop.
4675 We return HANDLED_RETURN if some part of the display property
4676 overrides the display of the buffer text itself.
4677 Otherwise we return HANDLED_NORMALLY. */
4679 static enum prop_handled
4680 handle_display_prop (struct it *it)
4682 Lisp_Object propval, object, overlay;
4683 struct text_pos *position;
4684 ptrdiff_t bufpos;
4685 /* Nonzero if some property replaces the display of the text itself. */
4686 int display_replaced = 0;
4688 if (STRINGP (it->string))
4690 object = it->string;
4691 position = &it->current.string_pos;
4692 bufpos = CHARPOS (it->current.pos);
4694 else
4696 XSETWINDOW (object, it->w);
4697 position = &it->current.pos;
4698 bufpos = CHARPOS (*position);
4701 /* Reset those iterator values set from display property values. */
4702 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4703 it->space_width = Qnil;
4704 it->font_height = Qnil;
4705 it->voffset = 0;
4707 /* We don't support recursive `display' properties, i.e. string
4708 values that have a string `display' property, that have a string
4709 `display' property etc. */
4710 if (!it->string_from_display_prop_p)
4711 it->area = TEXT_AREA;
4713 propval = get_char_property_and_overlay (make_number (position->charpos),
4714 Qdisplay, object, &overlay);
4715 if (NILP (propval))
4716 return HANDLED_NORMALLY;
4717 /* Now OVERLAY is the overlay that gave us this property, or nil
4718 if it was a text property. */
4720 if (!STRINGP (it->string))
4721 object = it->w->contents;
4723 display_replaced = handle_display_spec (it, propval, object, overlay,
4724 position, bufpos,
4725 FRAME_WINDOW_P (it->f));
4726 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4729 /* Subroutine of handle_display_prop. Returns non-zero if the display
4730 specification in SPEC is a replacing specification, i.e. it would
4731 replace the text covered by `display' property with something else,
4732 such as an image or a display string. If SPEC includes any kind or
4733 `(space ...) specification, the value is 2; this is used by
4734 compute_display_string_pos, which see.
4736 See handle_single_display_spec for documentation of arguments.
4737 FRAME_WINDOW_P is true if the window being redisplayed is on a
4738 GUI frame; this argument is used only if IT is NULL, see below.
4740 IT can be NULL, if this is called by the bidi reordering code
4741 through compute_display_string_pos, which see. In that case, this
4742 function only examines SPEC, but does not otherwise "handle" it, in
4743 the sense that it doesn't set up members of IT from the display
4744 spec. */
4745 static int
4746 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4747 Lisp_Object overlay, struct text_pos *position,
4748 ptrdiff_t bufpos, bool frame_window_p)
4750 int replacing = 0;
4751 bool enable_eval = true;
4753 /* Support (disable-eval PROP) which is used by enriched.el. */
4754 if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
4756 enable_eval = false;
4757 spec = XCAR (XCDR (spec));
4760 if (CONSP (spec)
4761 /* Simple specifications. */
4762 && !EQ (XCAR (spec), Qimage)
4763 #ifdef HAVE_XWIDGETS
4764 && !EQ (XCAR (spec), Qxwidget)
4765 #endif
4766 && !EQ (XCAR (spec), Qspace)
4767 && !EQ (XCAR (spec), Qwhen)
4768 && !EQ (XCAR (spec), Qslice)
4769 && !EQ (XCAR (spec), Qspace_width)
4770 && !EQ (XCAR (spec), Qheight)
4771 && !EQ (XCAR (spec), Qraise)
4772 /* Marginal area specifications. */
4773 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4774 && !EQ (XCAR (spec), Qleft_fringe)
4775 && !EQ (XCAR (spec), Qright_fringe)
4776 && !NILP (XCAR (spec)))
4778 for (; CONSP (spec); spec = XCDR (spec))
4780 int rv = handle_single_display_spec (it, XCAR (spec), object,
4781 overlay, position, bufpos,
4782 replacing, frame_window_p,
4783 enable_eval);
4784 if (rv != 0)
4786 replacing = rv;
4787 /* If some text in a string is replaced, `position' no
4788 longer points to the position of `object'. */
4789 if (!it || STRINGP (object))
4790 break;
4794 else if (VECTORP (spec))
4796 ptrdiff_t i;
4797 for (i = 0; i < ASIZE (spec); ++i)
4799 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4800 overlay, position, bufpos,
4801 replacing, frame_window_p,
4802 enable_eval);
4803 if (rv != 0)
4805 replacing = rv;
4806 /* If some text in a string is replaced, `position' no
4807 longer points to the position of `object'. */
4808 if (!it || STRINGP (object))
4809 break;
4813 else
4814 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4815 bufpos, 0, frame_window_p,
4816 enable_eval);
4817 return replacing;
4820 /* Value is the position of the end of the `display' property starting
4821 at START_POS in OBJECT. */
4823 static struct text_pos
4824 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4826 Lisp_Object end;
4827 struct text_pos end_pos;
4829 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4830 Qdisplay, object, Qnil);
4831 CHARPOS (end_pos) = XFASTINT (end);
4832 if (STRINGP (object))
4833 compute_string_pos (&end_pos, start_pos, it->string);
4834 else
4835 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4837 return end_pos;
4841 /* Set up IT from a single `display' property specification SPEC. OBJECT
4842 is the object in which the `display' property was found. *POSITION
4843 is the position in OBJECT at which the `display' property was found.
4844 BUFPOS is the buffer position of OBJECT (different from POSITION if
4845 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4846 previously saw a display specification which already replaced text
4847 display with something else, for example an image; we ignore such
4848 properties after the first one has been processed.
4850 OVERLAY is the overlay this `display' property came from,
4851 or nil if it was a text property.
4853 If SPEC is a `space' or `image' specification, and in some other
4854 cases too, set *POSITION to the position where the `display'
4855 property ends.
4857 If IT is NULL, only examine the property specification in SPEC, but
4858 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4859 is intended to be displayed in a window on a GUI frame.
4861 Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
4863 Value is non-zero if something was found which replaces the display
4864 of buffer or string text. */
4866 static int
4867 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4868 Lisp_Object overlay, struct text_pos *position,
4869 ptrdiff_t bufpos, int display_replaced,
4870 bool frame_window_p, bool enable_eval_p)
4872 Lisp_Object form;
4873 Lisp_Object location, value;
4874 struct text_pos start_pos = *position;
4876 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4877 If the result is non-nil, use VALUE instead of SPEC. */
4878 form = Qt;
4879 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4881 spec = XCDR (spec);
4882 if (!CONSP (spec))
4883 return 0;
4884 form = XCAR (spec);
4885 spec = XCDR (spec);
4888 if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
4889 form = Qnil;
4890 if (!NILP (form) && !EQ (form, Qt))
4892 ptrdiff_t count = SPECPDL_INDEX ();
4894 /* Bind `object' to the object having the `display' property, a
4895 buffer or string. Bind `position' to the position in the
4896 object where the property was found, and `buffer-position'
4897 to the current position in the buffer. */
4899 if (NILP (object))
4900 XSETBUFFER (object, current_buffer);
4901 specbind (Qobject, object);
4902 specbind (Qposition, make_number (CHARPOS (*position)));
4903 specbind (Qbuffer_position, make_number (bufpos));
4904 form = safe_eval (form);
4905 unbind_to (count, Qnil);
4908 if (NILP (form))
4909 return 0;
4911 /* Handle `(height HEIGHT)' specifications. */
4912 if (CONSP (spec)
4913 && EQ (XCAR (spec), Qheight)
4914 && CONSP (XCDR (spec)))
4916 if (it)
4918 if (!FRAME_WINDOW_P (it->f))
4919 return 0;
4921 it->font_height = XCAR (XCDR (spec));
4922 if (!NILP (it->font_height))
4924 int new_height = -1;
4926 if (CONSP (it->font_height)
4927 && (EQ (XCAR (it->font_height), Qplus)
4928 || EQ (XCAR (it->font_height), Qminus))
4929 && CONSP (XCDR (it->font_height))
4930 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4932 /* `(+ N)' or `(- N)' where N is an integer. */
4933 int steps = XINT (XCAR (XCDR (it->font_height)));
4934 if (EQ (XCAR (it->font_height), Qplus))
4935 steps = - steps;
4936 it->face_id = smaller_face (it->f, it->face_id, steps);
4938 else if (FUNCTIONP (it->font_height) && enable_eval_p)
4940 /* Call function with current height as argument.
4941 Value is the new height. */
4942 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4943 Lisp_Object height;
4944 height = safe_call1 (it->font_height,
4945 face->lface[LFACE_HEIGHT_INDEX]);
4946 if (NUMBERP (height))
4947 new_height = XFLOATINT (height);
4949 else if (NUMBERP (it->font_height))
4951 /* Value is a multiple of the canonical char height. */
4952 struct face *f;
4954 f = FACE_FROM_ID (it->f,
4955 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4956 new_height = (XFLOATINT (it->font_height)
4957 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4959 else if (enable_eval_p)
4961 /* Evaluate IT->font_height with `height' bound to the
4962 current specified height to get the new height. */
4963 ptrdiff_t count = SPECPDL_INDEX ();
4964 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4966 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4967 value = safe_eval (it->font_height);
4968 unbind_to (count, Qnil);
4970 if (NUMBERP (value))
4971 new_height = XFLOATINT (value);
4974 if (new_height > 0)
4975 it->face_id = face_with_height (it->f, it->face_id, new_height);
4979 return 0;
4982 /* Handle `(space-width WIDTH)'. */
4983 if (CONSP (spec)
4984 && EQ (XCAR (spec), Qspace_width)
4985 && CONSP (XCDR (spec)))
4987 if (it)
4989 if (!FRAME_WINDOW_P (it->f))
4990 return 0;
4992 value = XCAR (XCDR (spec));
4993 if (NUMBERP (value) && XFLOATINT (value) > 0)
4994 it->space_width = value;
4997 return 0;
5000 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5001 if (CONSP (spec)
5002 && EQ (XCAR (spec), Qslice))
5004 Lisp_Object tem;
5006 if (it)
5008 if (!FRAME_WINDOW_P (it->f))
5009 return 0;
5011 if (tem = XCDR (spec), CONSP (tem))
5013 it->slice.x = XCAR (tem);
5014 if (tem = XCDR (tem), CONSP (tem))
5016 it->slice.y = XCAR (tem);
5017 if (tem = XCDR (tem), CONSP (tem))
5019 it->slice.width = XCAR (tem);
5020 if (tem = XCDR (tem), CONSP (tem))
5021 it->slice.height = XCAR (tem);
5027 return 0;
5030 /* Handle `(raise FACTOR)'. */
5031 if (CONSP (spec)
5032 && EQ (XCAR (spec), Qraise)
5033 && CONSP (XCDR (spec)))
5035 if (it)
5037 if (!FRAME_WINDOW_P (it->f))
5038 return 0;
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 value = XCAR (XCDR (spec));
5042 if (NUMBERP (value))
5044 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5045 it->voffset = - (XFLOATINT (value)
5046 * (normal_char_height (face->font, -1)));
5048 #endif /* HAVE_WINDOW_SYSTEM */
5051 return 0;
5054 /* Don't handle the other kinds of display specifications
5055 inside a string that we got from a `display' property. */
5056 if (it && it->string_from_display_prop_p)
5057 return 0;
5059 /* Characters having this form of property are not displayed, so
5060 we have to find the end of the property. */
5061 if (it)
5063 start_pos = *position;
5064 *position = display_prop_end (it, object, start_pos);
5065 /* If the display property comes from an overlay, don't consider
5066 any potential stop_charpos values before the end of that
5067 overlay. Since display_prop_end will happily find another
5068 'display' property coming from some other overlay or text
5069 property on buffer positions before this overlay's end, we
5070 need to ignore them, or else we risk displaying this
5071 overlay's display string/image twice. */
5072 if (!NILP (overlay))
5074 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5076 /* Some borderline-sane Lisp might call us with the current
5077 buffer narrowed so that overlay-end is outside the
5078 POINT_MIN..POINT_MAX region, which will then cause
5079 various assertion violations and crashes down the road,
5080 starting with pop_it when it will attempt to use POSITION
5081 set below. Prevent that. */
5082 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5084 if (ovendpos > CHARPOS (*position))
5085 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5088 value = Qnil;
5090 /* Stop the scan at that end position--we assume that all
5091 text properties change there. */
5092 if (it)
5093 it->stop_charpos = position->charpos;
5095 /* Handle `(left-fringe BITMAP [FACE])'
5096 and `(right-fringe BITMAP [FACE])'. */
5097 if (CONSP (spec)
5098 && (EQ (XCAR (spec), Qleft_fringe)
5099 || EQ (XCAR (spec), Qright_fringe))
5100 && CONSP (XCDR (spec)))
5102 if (it)
5104 if (!FRAME_WINDOW_P (it->f))
5105 /* If we return here, POSITION has been advanced
5106 across the text with this property. */
5108 /* Synchronize the bidi iterator with POSITION. This is
5109 needed because we are not going to push the iterator
5110 on behalf of this display property, so there will be
5111 no pop_it call to do this synchronization for us. */
5112 if (it->bidi_p)
5114 it->position = *position;
5115 iterate_out_of_display_property (it);
5116 *position = it->position;
5118 return 1;
5121 else if (!frame_window_p)
5122 return 1;
5124 #ifdef HAVE_WINDOW_SYSTEM
5125 value = XCAR (XCDR (spec));
5126 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5127 if (! fringe_bitmap)
5128 /* If we return here, POSITION has been advanced
5129 across the text with this property. */
5131 if (it && it->bidi_p)
5133 it->position = *position;
5134 iterate_out_of_display_property (it);
5135 *position = it->position;
5137 return 1;
5140 if (it)
5142 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5144 if (CONSP (XCDR (XCDR (spec))))
5146 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5147 int face_id2 = lookup_derived_face (it->f, face_name,
5148 FRINGE_FACE_ID, false);
5149 if (face_id2 >= 0)
5150 face_id = face_id2;
5153 /* Save current settings of IT so that we can restore them
5154 when we are finished with the glyph property value. */
5155 push_it (it, position);
5157 it->area = TEXT_AREA;
5158 it->what = IT_IMAGE;
5159 it->image_id = -1; /* no image */
5160 it->position = start_pos;
5161 it->object = NILP (object) ? it->w->contents : object;
5162 it->method = GET_FROM_IMAGE;
5163 it->from_overlay = Qnil;
5164 it->face_id = face_id;
5165 it->from_disp_prop_p = true;
5167 /* Say that we haven't consumed the characters with
5168 `display' property yet. The call to pop_it in
5169 set_iterator_to_next will clean this up. */
5170 *position = start_pos;
5172 if (EQ (XCAR (spec), Qleft_fringe))
5174 it->left_user_fringe_bitmap = fringe_bitmap;
5175 it->left_user_fringe_face_id = face_id;
5177 else
5179 it->right_user_fringe_bitmap = fringe_bitmap;
5180 it->right_user_fringe_face_id = face_id;
5183 #endif /* HAVE_WINDOW_SYSTEM */
5184 return 1;
5187 /* Prepare to handle `((margin left-margin) ...)',
5188 `((margin right-margin) ...)' and `((margin nil) ...)'
5189 prefixes for display specifications. */
5190 location = Qunbound;
5191 if (CONSP (spec) && CONSP (XCAR (spec)))
5193 Lisp_Object tem;
5195 value = XCDR (spec);
5196 if (CONSP (value))
5197 value = XCAR (value);
5199 tem = XCAR (spec);
5200 if (EQ (XCAR (tem), Qmargin)
5201 && (tem = XCDR (tem),
5202 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5203 (NILP (tem)
5204 || EQ (tem, Qleft_margin)
5205 || EQ (tem, Qright_margin))))
5206 location = tem;
5209 if (EQ (location, Qunbound))
5211 location = Qnil;
5212 value = spec;
5215 /* After this point, VALUE is the property after any
5216 margin prefix has been stripped. It must be a string,
5217 an image specification, or `(space ...)'.
5219 LOCATION specifies where to display: `left-margin',
5220 `right-margin' or nil. */
5222 bool valid_p = (STRINGP (value)
5223 #ifdef HAVE_WINDOW_SYSTEM
5224 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5225 && valid_image_p (value))
5226 #endif /* not HAVE_WINDOW_SYSTEM */
5227 || (CONSP (value) && EQ (XCAR (value), Qspace))
5228 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5229 && valid_xwidget_spec_p (value)));
5231 if (valid_p && display_replaced == 0)
5233 int retval = 1;
5235 if (!it)
5237 /* Callers need to know whether the display spec is any kind
5238 of `(space ...)' spec that is about to affect text-area
5239 display. */
5240 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5241 retval = 2;
5242 return retval;
5245 /* Save current settings of IT so that we can restore them
5246 when we are finished with the glyph property value. */
5247 push_it (it, position);
5248 it->from_overlay = overlay;
5249 it->from_disp_prop_p = true;
5251 if (NILP (location))
5252 it->area = TEXT_AREA;
5253 else if (EQ (location, Qleft_margin))
5254 it->area = LEFT_MARGIN_AREA;
5255 else
5256 it->area = RIGHT_MARGIN_AREA;
5258 if (STRINGP (value))
5260 it->string = value;
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 it->current.overlay_string_index = -1;
5263 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5264 it->end_charpos = it->string_nchars = SCHARS (it->string);
5265 it->method = GET_FROM_STRING;
5266 it->stop_charpos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5269 it->string_from_display_prop_p = true;
5270 it->cmp_it.id = -1;
5271 /* Say that we haven't consumed the characters with
5272 `display' property yet. The call to pop_it in
5273 set_iterator_to_next will clean this up. */
5274 if (BUFFERP (object))
5275 *position = start_pos;
5277 /* Force paragraph direction to be that of the parent
5278 object. If the parent object's paragraph direction is
5279 not yet determined, default to L2R. */
5280 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5281 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5282 else
5283 it->paragraph_embedding = L2R;
5285 /* Set up the bidi iterator for this display string. */
5286 if (it->bidi_p)
5288 it->bidi_it.string.lstring = it->string;
5289 it->bidi_it.string.s = NULL;
5290 it->bidi_it.string.schars = it->end_charpos;
5291 it->bidi_it.string.bufpos = bufpos;
5292 it->bidi_it.string.from_disp_str = true;
5293 it->bidi_it.string.unibyte = !it->multibyte_p;
5294 it->bidi_it.w = it->w;
5295 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5298 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5300 it->method = GET_FROM_STRETCH;
5301 it->object = value;
5302 *position = it->position = start_pos;
5303 retval = 1 + (it->area == TEXT_AREA);
5305 else if (valid_xwidget_spec_p (value))
5307 it->what = IT_XWIDGET;
5308 it->method = GET_FROM_XWIDGET;
5309 it->position = start_pos;
5310 it->object = NILP (object) ? it->w->contents : object;
5311 *position = start_pos;
5312 it->xwidget = lookup_xwidget (value);
5314 #ifdef HAVE_WINDOW_SYSTEM
5315 else
5317 it->what = IT_IMAGE;
5318 it->image_id = lookup_image (it->f, value);
5319 it->position = start_pos;
5320 it->object = NILP (object) ? it->w->contents : object;
5321 it->method = GET_FROM_IMAGE;
5323 /* Say that we haven't consumed the characters with
5324 `display' property yet. The call to pop_it in
5325 set_iterator_to_next will clean this up. */
5326 *position = start_pos;
5328 #endif /* HAVE_WINDOW_SYSTEM */
5330 return retval;
5333 /* Invalid property or property not supported. Restore
5334 POSITION to what it was before. */
5335 *position = start_pos;
5336 return 0;
5339 /* Check if PROP is a display property value whose text should be
5340 treated as intangible. OVERLAY is the overlay from which PROP
5341 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5342 specify the buffer position covered by PROP. */
5344 bool
5345 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5346 ptrdiff_t charpos, ptrdiff_t bytepos)
5348 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5349 struct text_pos position;
5351 SET_TEXT_POS (position, charpos, bytepos);
5352 return (handle_display_spec (NULL, prop, Qnil, overlay,
5353 &position, charpos, frame_window_p)
5354 != 0);
5358 /* Return true if PROP is a display sub-property value containing STRING.
5360 Implementation note: this and the following function are really
5361 special cases of handle_display_spec and
5362 handle_single_display_spec, and should ideally use the same code.
5363 Until they do, these two pairs must be consistent and must be
5364 modified in sync. */
5366 static bool
5367 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5369 if (EQ (string, prop))
5370 return true;
5372 /* Skip over `when FORM'. */
5373 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5375 prop = XCDR (prop);
5376 if (!CONSP (prop))
5377 return false;
5378 /* Actually, the condition following `when' should be eval'ed,
5379 like handle_single_display_spec does, and we should return
5380 false if it evaluates to nil. However, this function is
5381 called only when the buffer was already displayed and some
5382 glyph in the glyph matrix was found to come from a display
5383 string. Therefore, the condition was already evaluated, and
5384 the result was non-nil, otherwise the display string wouldn't
5385 have been displayed and we would have never been called for
5386 this property. Thus, we can skip the evaluation and assume
5387 its result is non-nil. */
5388 prop = XCDR (prop);
5391 if (CONSP (prop))
5392 /* Skip over `margin LOCATION'. */
5393 if (EQ (XCAR (prop), Qmargin))
5395 prop = XCDR (prop);
5396 if (!CONSP (prop))
5397 return false;
5399 prop = XCDR (prop);
5400 if (!CONSP (prop))
5401 return false;
5404 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5408 /* Return true if STRING appears in the `display' property PROP. */
5410 static bool
5411 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5413 if (CONSP (prop)
5414 && !EQ (XCAR (prop), Qwhen)
5415 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5417 /* A list of sub-properties. */
5418 while (CONSP (prop))
5420 if (single_display_spec_string_p (XCAR (prop), string))
5421 return true;
5422 prop = XCDR (prop);
5425 else if (VECTORP (prop))
5427 /* A vector of sub-properties. */
5428 ptrdiff_t i;
5429 for (i = 0; i < ASIZE (prop); ++i)
5430 if (single_display_spec_string_p (AREF (prop, i), string))
5431 return true;
5433 else
5434 return single_display_spec_string_p (prop, string);
5436 return false;
5439 /* Look for STRING in overlays and text properties in the current
5440 buffer, between character positions FROM and TO (excluding TO).
5441 BACK_P means look back (in this case, TO is supposed to be
5442 less than FROM).
5443 Value is the first character position where STRING was found, or
5444 zero if it wasn't found before hitting TO.
5446 This function may only use code that doesn't eval because it is
5447 called asynchronously from note_mouse_highlight. */
5449 static ptrdiff_t
5450 string_buffer_position_lim (Lisp_Object string,
5451 ptrdiff_t from, ptrdiff_t to, bool back_p)
5453 Lisp_Object limit, prop, pos;
5454 bool found = false;
5456 pos = make_number (max (from, BEGV));
5458 if (!back_p) /* looking forward */
5460 limit = make_number (min (to, ZV));
5461 while (!found && !EQ (pos, limit))
5463 prop = Fget_char_property (pos, Qdisplay, Qnil);
5464 if (!NILP (prop) && display_prop_string_p (prop, string))
5465 found = true;
5466 else
5467 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5468 limit);
5471 else /* looking back */
5473 limit = make_number (max (to, BEGV));
5474 while (!found && !EQ (pos, limit))
5476 prop = Fget_char_property (pos, Qdisplay, Qnil);
5477 if (!NILP (prop) && display_prop_string_p (prop, string))
5478 found = true;
5479 else
5480 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5481 limit);
5485 return found ? XINT (pos) : 0;
5488 /* Determine which buffer position in current buffer STRING comes from.
5489 AROUND_CHARPOS is an approximate position where it could come from.
5490 Value is the buffer position or 0 if it couldn't be determined.
5492 This function is necessary because we don't record buffer positions
5493 in glyphs generated from strings (to keep struct glyph small).
5494 This function may only use code that doesn't eval because it is
5495 called asynchronously from note_mouse_highlight. */
5497 static ptrdiff_t
5498 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5500 const int MAX_DISTANCE = 1000;
5501 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5502 around_charpos + MAX_DISTANCE,
5503 false);
5505 if (!found)
5506 found = string_buffer_position_lim (string, around_charpos,
5507 around_charpos - MAX_DISTANCE, true);
5508 return found;
5513 /***********************************************************************
5514 `composition' property
5515 ***********************************************************************/
5517 /* Set up iterator IT from `composition' property at its current
5518 position. Called from handle_stop. */
5520 static enum prop_handled
5521 handle_composition_prop (struct it *it)
5523 Lisp_Object prop, string;
5524 ptrdiff_t pos, pos_byte, start, end;
5526 if (STRINGP (it->string))
5528 unsigned char *s;
5530 pos = IT_STRING_CHARPOS (*it);
5531 pos_byte = IT_STRING_BYTEPOS (*it);
5532 string = it->string;
5533 s = SDATA (string) + pos_byte;
5534 it->c = STRING_CHAR (s);
5536 else
5538 pos = IT_CHARPOS (*it);
5539 pos_byte = IT_BYTEPOS (*it);
5540 string = Qnil;
5541 it->c = FETCH_CHAR (pos_byte);
5544 /* If there's a valid composition and point is not inside of the
5545 composition (in the case that the composition is from the current
5546 buffer), draw a glyph composed from the composition components. */
5547 if (find_composition (pos, -1, &start, &end, &prop, string)
5548 && composition_valid_p (start, end, prop)
5549 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5551 if (start < pos)
5552 /* As we can't handle this situation (perhaps font-lock added
5553 a new composition), we just return here hoping that next
5554 redisplay will detect this composition much earlier. */
5555 return HANDLED_NORMALLY;
5556 if (start != pos)
5558 if (STRINGP (it->string))
5559 pos_byte = string_char_to_byte (it->string, start);
5560 else
5561 pos_byte = CHAR_TO_BYTE (start);
5563 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5564 prop, string);
5566 if (it->cmp_it.id >= 0)
5568 it->cmp_it.ch = -1;
5569 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5570 it->cmp_it.nglyphs = -1;
5574 return HANDLED_NORMALLY;
5579 /***********************************************************************
5580 Overlay strings
5581 ***********************************************************************/
5583 /* The following structure is used to record overlay strings for
5584 later sorting in load_overlay_strings. */
5586 struct overlay_entry
5588 Lisp_Object overlay;
5589 Lisp_Object string;
5590 EMACS_INT priority;
5591 bool after_string_p;
5595 /* Set up iterator IT from overlay strings at its current position.
5596 Called from handle_stop. */
5598 static enum prop_handled
5599 handle_overlay_change (struct it *it)
5601 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5602 return HANDLED_RECOMPUTE_PROPS;
5603 else
5604 return HANDLED_NORMALLY;
5608 /* Set up the next overlay string for delivery by IT, if there is an
5609 overlay string to deliver. Called by set_iterator_to_next when the
5610 end of the current overlay string is reached. If there are more
5611 overlay strings to display, IT->string and
5612 IT->current.overlay_string_index are set appropriately here.
5613 Otherwise IT->string is set to nil. */
5615 static void
5616 next_overlay_string (struct it *it)
5618 ++it->current.overlay_string_index;
5619 if (it->current.overlay_string_index == it->n_overlay_strings)
5621 /* No more overlay strings. Restore IT's settings to what
5622 they were before overlay strings were processed, and
5623 continue to deliver from current_buffer. */
5625 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5626 pop_it (it);
5627 eassert (it->sp > 0
5628 || (NILP (it->string)
5629 && it->method == GET_FROM_BUFFER
5630 && it->stop_charpos >= BEGV
5631 && it->stop_charpos <= it->end_charpos));
5632 it->current.overlay_string_index = -1;
5633 it->n_overlay_strings = 0;
5634 /* If there's an empty display string on the stack, pop the
5635 stack, to resync the bidi iterator with IT's position. Such
5636 empty strings are pushed onto the stack in
5637 get_overlay_strings_1. */
5638 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5639 pop_it (it);
5641 /* Since we've exhausted overlay strings at this buffer
5642 position, set the flag to ignore overlays until we move to
5643 another position. (The flag will be reset in
5644 next_element_from_buffer.) But don't do that if the overlay
5645 strings were loaded at position other than the current one,
5646 which could happen if we called pop_it above, or if the
5647 overlay strings were loaded by handle_invisible_prop at the
5648 beginning of invisible text. */
5649 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5650 it->ignore_overlay_strings_at_pos_p = true;
5652 /* If we're at the end of the buffer, record that we have
5653 processed the overlay strings there already, so that
5654 next_element_from_buffer doesn't try it again. */
5655 if (NILP (it->string)
5656 && IT_CHARPOS (*it) >= it->end_charpos
5657 && it->overlay_strings_charpos >= it->end_charpos)
5658 it->overlay_strings_at_end_processed_p = true;
5659 /* Note: we reset overlay_strings_charpos only here, to make
5660 sure the just-processed overlays were indeed at EOB.
5661 Otherwise, overlays on text with invisible text property,
5662 which are processed with IT's position past the invisible
5663 text, might fool us into thinking the overlays at EOB were
5664 already processed (linum-mode can cause this, for
5665 example). */
5666 it->overlay_strings_charpos = -1;
5668 else
5670 /* There are more overlay strings to process. If
5671 IT->current.overlay_string_index has advanced to a position
5672 where we must load IT->overlay_strings with more strings, do
5673 it. We must load at the IT->overlay_strings_charpos where
5674 IT->n_overlay_strings was originally computed; when invisible
5675 text is present, this might not be IT_CHARPOS (Bug#7016). */
5676 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5678 if (it->current.overlay_string_index && i == 0)
5679 load_overlay_strings (it, it->overlay_strings_charpos);
5681 /* Initialize IT to deliver display elements from the overlay
5682 string. */
5683 it->string = it->overlay_strings[i];
5684 it->multibyte_p = STRING_MULTIBYTE (it->string);
5685 SET_TEXT_POS (it->current.string_pos, 0, 0);
5686 it->method = GET_FROM_STRING;
5687 it->stop_charpos = 0;
5688 it->end_charpos = SCHARS (it->string);
5689 if (it->cmp_it.stop_pos >= 0)
5690 it->cmp_it.stop_pos = 0;
5691 it->prev_stop = 0;
5692 it->base_level_stop = 0;
5694 /* Set up the bidi iterator for this overlay string. */
5695 if (it->bidi_p)
5697 it->bidi_it.string.lstring = it->string;
5698 it->bidi_it.string.s = NULL;
5699 it->bidi_it.string.schars = SCHARS (it->string);
5700 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5701 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5702 it->bidi_it.string.unibyte = !it->multibyte_p;
5703 it->bidi_it.w = it->w;
5704 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5708 CHECK_IT (it);
5712 /* Compare two overlay_entry structures E1 and E2. Used as a
5713 comparison function for qsort in load_overlay_strings. Overlay
5714 strings for the same position are sorted so that
5716 1. All after-strings come in front of before-strings, except
5717 when they come from the same overlay.
5719 2. Within after-strings, strings are sorted so that overlay strings
5720 from overlays with higher priorities come first.
5722 2. Within before-strings, strings are sorted so that overlay
5723 strings from overlays with higher priorities come last.
5725 Value is analogous to strcmp. */
5728 static int
5729 compare_overlay_entries (const void *e1, const void *e2)
5731 struct overlay_entry const *entry1 = e1;
5732 struct overlay_entry const *entry2 = e2;
5733 int result;
5735 if (entry1->after_string_p != entry2->after_string_p)
5737 /* Let after-strings appear in front of before-strings if
5738 they come from different overlays. */
5739 if (EQ (entry1->overlay, entry2->overlay))
5740 result = entry1->after_string_p ? 1 : -1;
5741 else
5742 result = entry1->after_string_p ? -1 : 1;
5744 else if (entry1->priority != entry2->priority)
5746 if (entry1->after_string_p)
5747 /* After-strings sorted in order of decreasing priority. */
5748 result = entry2->priority < entry1->priority ? -1 : 1;
5749 else
5750 /* Before-strings sorted in order of increasing priority. */
5751 result = entry1->priority < entry2->priority ? -1 : 1;
5753 else
5754 result = 0;
5756 return result;
5760 /* Load the vector IT->overlay_strings with overlay strings from IT's
5761 current buffer position, or from CHARPOS if that is > 0. Set
5762 IT->n_overlays to the total number of overlay strings found.
5764 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5765 a time. On entry into load_overlay_strings,
5766 IT->current.overlay_string_index gives the number of overlay
5767 strings that have already been loaded by previous calls to this
5768 function.
5770 IT->add_overlay_start contains an additional overlay start
5771 position to consider for taking overlay strings from, if non-zero.
5772 This position comes into play when the overlay has an `invisible'
5773 property, and both before and after-strings. When we've skipped to
5774 the end of the overlay, because of its `invisible' property, we
5775 nevertheless want its before-string to appear.
5776 IT->add_overlay_start will contain the overlay start position
5777 in this case.
5779 Overlay strings are sorted so that after-string strings come in
5780 front of before-string strings. Within before and after-strings,
5781 strings are sorted by overlay priority. See also function
5782 compare_overlay_entries. */
5784 static void
5785 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5787 Lisp_Object overlay, window, str, invisible;
5788 struct Lisp_Overlay *ov;
5789 ptrdiff_t start, end;
5790 ptrdiff_t n = 0, i, j;
5791 int invis;
5792 struct overlay_entry entriesbuf[20];
5793 ptrdiff_t size = ARRAYELTS (entriesbuf);
5794 struct overlay_entry *entries = entriesbuf;
5795 USE_SAFE_ALLOCA;
5797 if (charpos <= 0)
5798 charpos = IT_CHARPOS (*it);
5800 /* Append the overlay string STRING of overlay OVERLAY to vector
5801 `entries' which has size `size' and currently contains `n'
5802 elements. AFTER_P means STRING is an after-string of
5803 OVERLAY. */
5804 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5805 do \
5807 Lisp_Object priority; \
5809 if (n == size) \
5811 struct overlay_entry *old = entries; \
5812 SAFE_NALLOCA (entries, 2, size); \
5813 memcpy (entries, old, size * sizeof *entries); \
5814 size *= 2; \
5817 entries[n].string = (STRING); \
5818 entries[n].overlay = (OVERLAY); \
5819 priority = Foverlay_get ((OVERLAY), Qpriority); \
5820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5821 entries[n].after_string_p = (AFTER_P); \
5822 ++n; \
5824 while (false)
5826 /* Process overlay before the overlay center. */
5827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5829 XSETMISC (overlay, ov);
5830 eassert (OVERLAYP (overlay));
5831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5834 if (end < charpos)
5835 break;
5837 /* Skip this overlay if it doesn't start or end at IT's current
5838 position. */
5839 if (end != charpos && start != charpos)
5840 continue;
5842 /* Skip this overlay if it doesn't apply to IT->w. */
5843 window = Foverlay_get (overlay, Qwindow);
5844 if (WINDOWP (window) && XWINDOW (window) != it->w)
5845 continue;
5847 /* If the text ``under'' the overlay is invisible, both before-
5848 and after-strings from this overlay are visible; start and
5849 end position are indistinguishable. */
5850 invisible = Foverlay_get (overlay, Qinvisible);
5851 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5853 /* If overlay has a non-empty before-string, record it. */
5854 if ((start == charpos || (end == charpos && invis != 0))
5855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5856 && SCHARS (str))
5857 RECORD_OVERLAY_STRING (overlay, str, false);
5859 /* If overlay has a non-empty after-string, record it. */
5860 if ((end == charpos || (start == charpos && invis != 0))
5861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5862 && SCHARS (str))
5863 RECORD_OVERLAY_STRING (overlay, str, true);
5866 /* Process overlays after the overlay center. */
5867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5869 XSETMISC (overlay, ov);
5870 eassert (OVERLAYP (overlay));
5871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5874 if (start > charpos)
5875 break;
5877 /* Skip this overlay if it doesn't start or end at IT's current
5878 position. */
5879 if (end != charpos && start != charpos)
5880 continue;
5882 /* Skip this overlay if it doesn't apply to IT->w. */
5883 window = Foverlay_get (overlay, Qwindow);
5884 if (WINDOWP (window) && XWINDOW (window) != it->w)
5885 continue;
5887 /* If the text ``under'' the overlay is invisible, it has a zero
5888 dimension, and both before- and after-strings apply. */
5889 invisible = Foverlay_get (overlay, Qinvisible);
5890 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5892 /* If overlay has a non-empty before-string, record it. */
5893 if ((start == charpos || (end == charpos && invis != 0))
5894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5895 && SCHARS (str))
5896 RECORD_OVERLAY_STRING (overlay, str, false);
5898 /* If overlay has a non-empty after-string, record it. */
5899 if ((end == charpos || (start == charpos && invis != 0))
5900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5901 && SCHARS (str))
5902 RECORD_OVERLAY_STRING (overlay, str, true);
5905 #undef RECORD_OVERLAY_STRING
5907 /* Sort entries. */
5908 if (n > 1)
5909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5911 /* Record number of overlay strings, and where we computed it. */
5912 it->n_overlay_strings = n;
5913 it->overlay_strings_charpos = charpos;
5915 /* IT->current.overlay_string_index is the number of overlay strings
5916 that have already been consumed by IT. Copy some of the
5917 remaining overlay strings to IT->overlay_strings. */
5918 i = 0;
5919 j = it->current.overlay_string_index;
5920 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5922 it->overlay_strings[i] = entries[j].string;
5923 it->string_overlays[i++] = entries[j++].overlay;
5926 CHECK_IT (it);
5927 SAFE_FREE ();
5931 /* Get the first chunk of overlay strings at IT's current buffer
5932 position, or at CHARPOS if that is > 0. Value is true if at
5933 least one overlay string was found. */
5935 static bool
5936 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5938 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5939 process. This fills IT->overlay_strings with strings, and sets
5940 IT->n_overlay_strings to the total number of strings to process.
5941 IT->pos.overlay_string_index has to be set temporarily to zero
5942 because load_overlay_strings needs this; it must be set to -1
5943 when no overlay strings are found because a zero value would
5944 indicate a position in the first overlay string. */
5945 it->current.overlay_string_index = 0;
5946 load_overlay_strings (it, charpos);
5948 /* If we found overlay strings, set up IT to deliver display
5949 elements from the first one. Otherwise set up IT to deliver
5950 from current_buffer. */
5951 if (it->n_overlay_strings)
5953 /* Make sure we know settings in current_buffer, so that we can
5954 restore meaningful values when we're done with the overlay
5955 strings. */
5956 if (compute_stop_p)
5957 compute_stop_pos (it);
5958 eassert (it->face_id >= 0);
5960 /* Save IT's settings. They are restored after all overlay
5961 strings have been processed. */
5962 eassert (!compute_stop_p || it->sp == 0);
5964 /* When called from handle_stop, there might be an empty display
5965 string loaded. In that case, don't bother saving it. But
5966 don't use this optimization with the bidi iterator, since we
5967 need the corresponding pop_it call to resync the bidi
5968 iterator's position with IT's position, after we are done
5969 with the overlay strings. (The corresponding call to pop_it
5970 in case of an empty display string is in
5971 next_overlay_string.) */
5972 if (!(!it->bidi_p
5973 && STRINGP (it->string) && !SCHARS (it->string)))
5974 push_it (it, NULL);
5976 /* Set up IT to deliver display elements from the first overlay
5977 string. */
5978 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5979 it->string = it->overlay_strings[0];
5980 it->from_overlay = Qnil;
5981 it->stop_charpos = 0;
5982 eassert (STRINGP (it->string));
5983 it->end_charpos = SCHARS (it->string);
5984 it->prev_stop = 0;
5985 it->base_level_stop = 0;
5986 it->multibyte_p = STRING_MULTIBYTE (it->string);
5987 it->method = GET_FROM_STRING;
5988 it->from_disp_prop_p = 0;
5989 it->cmp_it.id = -1;
5991 /* Force paragraph direction to be that of the parent
5992 buffer. */
5993 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5994 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5995 else
5996 it->paragraph_embedding = L2R;
5998 /* Set up the bidi iterator for this overlay string. */
5999 if (it->bidi_p)
6001 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
6003 it->bidi_it.string.lstring = it->string;
6004 it->bidi_it.string.s = NULL;
6005 it->bidi_it.string.schars = SCHARS (it->string);
6006 it->bidi_it.string.bufpos = pos;
6007 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6008 it->bidi_it.string.unibyte = !it->multibyte_p;
6009 it->bidi_it.w = it->w;
6010 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6012 return true;
6015 it->current.overlay_string_index = -1;
6016 return false;
6019 static bool
6020 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6022 it->string = Qnil;
6023 it->method = GET_FROM_BUFFER;
6025 get_overlay_strings_1 (it, charpos, true);
6027 CHECK_IT (it);
6029 /* Value is true if we found at least one overlay string. */
6030 return STRINGP (it->string);
6035 /***********************************************************************
6036 Saving and restoring state
6037 ***********************************************************************/
6039 /* Save current settings of IT on IT->stack. Called, for example,
6040 before setting up IT for an overlay string, to be able to restore
6041 IT's settings to what they were after the overlay string has been
6042 processed. If POSITION is non-NULL, it is the position to save on
6043 the stack instead of IT->position. */
6045 static void
6046 push_it (struct it *it, struct text_pos *position)
6048 struct iterator_stack_entry *p;
6050 eassert (it->sp < IT_STACK_SIZE);
6051 p = it->stack + it->sp;
6053 p->stop_charpos = it->stop_charpos;
6054 p->prev_stop = it->prev_stop;
6055 p->base_level_stop = it->base_level_stop;
6056 p->cmp_it = it->cmp_it;
6057 eassert (it->face_id >= 0);
6058 p->face_id = it->face_id;
6059 p->string = it->string;
6060 p->method = it->method;
6061 p->from_overlay = it->from_overlay;
6062 switch (p->method)
6064 case GET_FROM_IMAGE:
6065 p->u.image.object = it->object;
6066 p->u.image.image_id = it->image_id;
6067 p->u.image.slice = it->slice;
6068 break;
6069 case GET_FROM_STRETCH:
6070 p->u.stretch.object = it->object;
6071 break;
6072 case GET_FROM_XWIDGET:
6073 p->u.xwidget.object = it->object;
6074 break;
6075 case GET_FROM_BUFFER:
6076 case GET_FROM_DISPLAY_VECTOR:
6077 case GET_FROM_STRING:
6078 case GET_FROM_C_STRING:
6079 break;
6080 default:
6081 emacs_abort ();
6083 p->position = position ? *position : it->position;
6084 p->current = it->current;
6085 p->end_charpos = it->end_charpos;
6086 p->string_nchars = it->string_nchars;
6087 p->area = it->area;
6088 p->multibyte_p = it->multibyte_p;
6089 p->avoid_cursor_p = it->avoid_cursor_p;
6090 p->space_width = it->space_width;
6091 p->font_height = it->font_height;
6092 p->voffset = it->voffset;
6093 p->string_from_display_prop_p = it->string_from_display_prop_p;
6094 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6095 p->display_ellipsis_p = false;
6096 p->line_wrap = it->line_wrap;
6097 p->bidi_p = it->bidi_p;
6098 p->paragraph_embedding = it->paragraph_embedding;
6099 p->from_disp_prop_p = it->from_disp_prop_p;
6100 ++it->sp;
6102 /* Save the state of the bidi iterator as well. */
6103 if (it->bidi_p)
6104 bidi_push_it (&it->bidi_it);
6107 static void
6108 iterate_out_of_display_property (struct it *it)
6110 bool buffer_p = !STRINGP (it->string);
6111 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6112 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6114 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6116 /* Maybe initialize paragraph direction. If we are at the beginning
6117 of a new paragraph, next_element_from_buffer may not have a
6118 chance to do that. */
6119 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6120 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6121 /* prev_stop can be zero, so check against BEGV as well. */
6122 while (it->bidi_it.charpos >= bob
6123 && it->prev_stop <= it->bidi_it.charpos
6124 && it->bidi_it.charpos < CHARPOS (it->position)
6125 && it->bidi_it.charpos < eob)
6126 bidi_move_to_visually_next (&it->bidi_it);
6127 /* Record the stop_pos we just crossed, for when we cross it
6128 back, maybe. */
6129 if (it->bidi_it.charpos > CHARPOS (it->position))
6130 it->prev_stop = CHARPOS (it->position);
6131 /* If we ended up not where pop_it put us, resync IT's
6132 positional members with the bidi iterator. */
6133 if (it->bidi_it.charpos != CHARPOS (it->position))
6134 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6135 if (buffer_p)
6136 it->current.pos = it->position;
6137 else
6138 it->current.string_pos = it->position;
6141 /* Restore IT's settings from IT->stack. Called, for example, when no
6142 more overlay strings must be processed, and we return to delivering
6143 display elements from a buffer, or when the end of a string from a
6144 `display' property is reached and we return to delivering display
6145 elements from an overlay string, or from a buffer. */
6147 static void
6148 pop_it (struct it *it)
6150 struct iterator_stack_entry *p;
6151 bool from_display_prop = it->from_disp_prop_p;
6152 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6154 eassert (it->sp > 0);
6155 --it->sp;
6156 p = it->stack + it->sp;
6157 it->stop_charpos = p->stop_charpos;
6158 it->prev_stop = p->prev_stop;
6159 it->base_level_stop = p->base_level_stop;
6160 it->cmp_it = p->cmp_it;
6161 it->face_id = p->face_id;
6162 it->current = p->current;
6163 it->position = p->position;
6164 it->string = p->string;
6165 it->from_overlay = p->from_overlay;
6166 if (NILP (it->string))
6167 SET_TEXT_POS (it->current.string_pos, -1, -1);
6168 it->method = p->method;
6169 switch (it->method)
6171 case GET_FROM_IMAGE:
6172 it->image_id = p->u.image.image_id;
6173 it->object = p->u.image.object;
6174 it->slice = p->u.image.slice;
6175 break;
6176 case GET_FROM_XWIDGET:
6177 it->object = p->u.xwidget.object;
6178 break;
6179 case GET_FROM_STRETCH:
6180 it->object = p->u.stretch.object;
6181 break;
6182 case GET_FROM_BUFFER:
6183 it->object = it->w->contents;
6184 break;
6185 case GET_FROM_STRING:
6187 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6189 /* Restore the face_box_p flag, since it could have been
6190 overwritten by the face of the object that we just finished
6191 displaying. */
6192 if (face)
6193 it->face_box_p = face->box != FACE_NO_BOX;
6194 it->object = it->string;
6196 break;
6197 case GET_FROM_DISPLAY_VECTOR:
6198 if (it->s)
6199 it->method = GET_FROM_C_STRING;
6200 else if (STRINGP (it->string))
6201 it->method = GET_FROM_STRING;
6202 else
6204 it->method = GET_FROM_BUFFER;
6205 it->object = it->w->contents;
6207 break;
6208 case GET_FROM_C_STRING:
6209 break;
6210 default:
6211 emacs_abort ();
6213 it->end_charpos = p->end_charpos;
6214 it->string_nchars = p->string_nchars;
6215 it->area = p->area;
6216 it->multibyte_p = p->multibyte_p;
6217 it->avoid_cursor_p = p->avoid_cursor_p;
6218 it->space_width = p->space_width;
6219 it->font_height = p->font_height;
6220 it->voffset = p->voffset;
6221 it->string_from_display_prop_p = p->string_from_display_prop_p;
6222 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6223 it->line_wrap = p->line_wrap;
6224 it->bidi_p = p->bidi_p;
6225 it->paragraph_embedding = p->paragraph_embedding;
6226 it->from_disp_prop_p = p->from_disp_prop_p;
6227 if (it->bidi_p)
6229 bidi_pop_it (&it->bidi_it);
6230 /* Bidi-iterate until we get out of the portion of text, if any,
6231 covered by a `display' text property or by an overlay with
6232 `display' property. (We cannot just jump there, because the
6233 internal coherency of the bidi iterator state can not be
6234 preserved across such jumps.) We also must determine the
6235 paragraph base direction if the overlay we just processed is
6236 at the beginning of a new paragraph. */
6237 if (from_display_prop
6238 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6239 iterate_out_of_display_property (it);
6241 eassert ((BUFFERP (it->object)
6242 && IT_CHARPOS (*it) == it->bidi_it.charpos
6243 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6244 || (STRINGP (it->object)
6245 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6246 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6247 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6249 /* If we move the iterator over text covered by a display property
6250 to a new buffer position, any info about previously seen overlays
6251 is no longer valid. */
6252 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6253 it->ignore_overlay_strings_at_pos_p = false;
6258 /***********************************************************************
6259 Moving over lines
6260 ***********************************************************************/
6262 /* Set IT's current position to the previous line start. */
6264 static void
6265 back_to_previous_line_start (struct it *it)
6267 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6269 DEC_BOTH (cp, bp);
6270 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6274 /* Move IT to the next line start.
6276 Value is true if a newline was found. Set *SKIPPED_P to true if
6277 we skipped over part of the text (as opposed to moving the iterator
6278 continuously over the text). Otherwise, don't change the value
6279 of *SKIPPED_P.
6281 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6282 iterator on the newline, if it was found.
6284 Newlines may come from buffer text, overlay strings, or strings
6285 displayed via the `display' property. That's the reason we can't
6286 simply use find_newline_no_quit.
6288 Note that this function may not skip over invisible text that is so
6289 because of text properties and immediately follows a newline. If
6290 it would, function reseat_at_next_visible_line_start, when called
6291 from set_iterator_to_next, would effectively make invisible
6292 characters following a newline part of the wrong glyph row, which
6293 leads to wrong cursor motion. */
6295 static bool
6296 forward_to_next_line_start (struct it *it, bool *skipped_p,
6297 struct bidi_it *bidi_it_prev)
6299 ptrdiff_t old_selective;
6300 bool newline_found_p = false;
6301 int n;
6302 const int MAX_NEWLINE_DISTANCE = 500;
6304 /* If already on a newline, just consume it to avoid unintended
6305 skipping over invisible text below. */
6306 if (it->what == IT_CHARACTER
6307 && it->c == '\n'
6308 && CHARPOS (it->position) == IT_CHARPOS (*it))
6310 if (it->bidi_p && bidi_it_prev)
6311 *bidi_it_prev = it->bidi_it;
6312 set_iterator_to_next (it, false);
6313 it->c = 0;
6314 return true;
6317 /* Don't handle selective display in the following. It's (a)
6318 unnecessary because it's done by the caller, and (b) leads to an
6319 infinite recursion because next_element_from_ellipsis indirectly
6320 calls this function. */
6321 old_selective = it->selective;
6322 it->selective = 0;
6324 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6325 from buffer text. */
6326 for (n = 0;
6327 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6328 n += !STRINGP (it->string))
6330 if (!get_next_display_element (it))
6331 return false;
6332 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6333 if (newline_found_p && it->bidi_p && bidi_it_prev)
6334 *bidi_it_prev = it->bidi_it;
6335 set_iterator_to_next (it, false);
6338 /* If we didn't find a newline near enough, see if we can use a
6339 short-cut. */
6340 if (!newline_found_p)
6342 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6343 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6344 1, &bytepos);
6345 Lisp_Object pos;
6347 eassert (!STRINGP (it->string));
6349 /* If there isn't any `display' property in sight, and no
6350 overlays, we can just use the position of the newline in
6351 buffer text. */
6352 if (it->stop_charpos >= limit
6353 || ((pos = Fnext_single_property_change (make_number (start),
6354 Qdisplay, Qnil,
6355 make_number (limit)),
6356 NILP (pos))
6357 && next_overlay_change (start) == ZV))
6359 if (!it->bidi_p)
6361 IT_CHARPOS (*it) = limit;
6362 IT_BYTEPOS (*it) = bytepos;
6364 else
6366 struct bidi_it bprev;
6368 /* Help bidi.c avoid expensive searches for display
6369 properties and overlays, by telling it that there are
6370 none up to `limit'. */
6371 if (it->bidi_it.disp_pos < limit)
6373 it->bidi_it.disp_pos = limit;
6374 it->bidi_it.disp_prop = 0;
6376 do {
6377 bprev = it->bidi_it;
6378 bidi_move_to_visually_next (&it->bidi_it);
6379 } while (it->bidi_it.charpos != limit);
6380 IT_CHARPOS (*it) = limit;
6381 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6382 if (bidi_it_prev)
6383 *bidi_it_prev = bprev;
6385 *skipped_p = newline_found_p = true;
6387 else
6389 while (!newline_found_p)
6391 if (!get_next_display_element (it))
6392 break;
6393 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6394 if (newline_found_p && it->bidi_p && bidi_it_prev)
6395 *bidi_it_prev = it->bidi_it;
6396 set_iterator_to_next (it, false);
6401 it->selective = old_selective;
6402 return newline_found_p;
6406 /* Set IT's current position to the previous visible line start. Skip
6407 invisible text that is so either due to text properties or due to
6408 selective display. Caution: this does not change IT->current_x and
6409 IT->hpos. */
6411 static void
6412 back_to_previous_visible_line_start (struct it *it)
6414 while (IT_CHARPOS (*it) > BEGV)
6416 back_to_previous_line_start (it);
6418 if (IT_CHARPOS (*it) <= BEGV)
6419 break;
6421 /* If selective > 0, then lines indented more than its value are
6422 invisible. */
6423 if (it->selective > 0
6424 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6425 it->selective))
6426 continue;
6428 /* Check the newline before point for invisibility. */
6430 Lisp_Object prop;
6431 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6432 Qinvisible, it->window);
6433 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6434 continue;
6437 if (IT_CHARPOS (*it) <= BEGV)
6438 break;
6441 struct it it2;
6442 void *it2data = NULL;
6443 ptrdiff_t pos;
6444 ptrdiff_t beg, end;
6445 Lisp_Object val, overlay;
6447 SAVE_IT (it2, *it, it2data);
6449 /* If newline is part of a composition, continue from start of composition */
6450 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6451 && beg < IT_CHARPOS (*it))
6452 goto replaced;
6454 /* If newline is replaced by a display property, find start of overlay
6455 or interval and continue search from that point. */
6456 pos = --IT_CHARPOS (it2);
6457 --IT_BYTEPOS (it2);
6458 it2.sp = 0;
6459 bidi_unshelve_cache (NULL, false);
6460 it2.string_from_display_prop_p = false;
6461 it2.from_disp_prop_p = false;
6462 if (handle_display_prop (&it2) == HANDLED_RETURN
6463 && !NILP (val = get_char_property_and_overlay
6464 (make_number (pos), Qdisplay, Qnil, &overlay))
6465 && (OVERLAYP (overlay)
6466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6469 RESTORE_IT (it, it, it2data);
6470 goto replaced;
6473 /* Newline is not replaced by anything -- so we are done. */
6474 RESTORE_IT (it, it, it2data);
6475 break;
6477 replaced:
6478 if (beg < BEGV)
6479 beg = BEGV;
6480 IT_CHARPOS (*it) = beg;
6481 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6485 it->continuation_lines_width = 0;
6487 eassert (IT_CHARPOS (*it) >= BEGV);
6488 eassert (IT_CHARPOS (*it) == BEGV
6489 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6490 CHECK_IT (it);
6494 /* Reseat iterator IT at the previous visible line start. Skip
6495 invisible text that is so either due to text properties or due to
6496 selective display. At the end, update IT's overlay information,
6497 face information etc. */
6499 void
6500 reseat_at_previous_visible_line_start (struct it *it)
6502 back_to_previous_visible_line_start (it);
6503 reseat (it, it->current.pos, true);
6504 CHECK_IT (it);
6508 /* Reseat iterator IT on the next visible line start in the current
6509 buffer. ON_NEWLINE_P means position IT on the newline
6510 preceding the line start. Skip over invisible text that is so
6511 because of selective display. Compute faces, overlays etc at the
6512 new position. Note that this function does not skip over text that
6513 is invisible because of text properties. */
6515 static void
6516 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6518 bool skipped_p = false;
6519 struct bidi_it bidi_it_prev;
6520 bool newline_found_p
6521 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6523 /* Skip over lines that are invisible because they are indented
6524 more than the value of IT->selective. */
6525 if (it->selective > 0)
6526 while (IT_CHARPOS (*it) < ZV
6527 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6528 it->selective))
6530 eassert (IT_BYTEPOS (*it) == BEGV
6531 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6532 newline_found_p =
6533 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6536 /* Position on the newline if that's what's requested. */
6537 if (on_newline_p && newline_found_p)
6539 if (STRINGP (it->string))
6541 if (IT_STRING_CHARPOS (*it) > 0)
6543 if (!it->bidi_p)
6545 --IT_STRING_CHARPOS (*it);
6546 --IT_STRING_BYTEPOS (*it);
6548 else
6550 /* We need to restore the bidi iterator to the state
6551 it had on the newline, and resync the IT's
6552 position with that. */
6553 it->bidi_it = bidi_it_prev;
6554 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6555 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6559 else if (IT_CHARPOS (*it) > BEGV)
6561 if (!it->bidi_p)
6563 --IT_CHARPOS (*it);
6564 --IT_BYTEPOS (*it);
6566 else
6568 /* We need to restore the bidi iterator to the state it
6569 had on the newline and resync IT with that. */
6570 it->bidi_it = bidi_it_prev;
6571 IT_CHARPOS (*it) = it->bidi_it.charpos;
6572 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6574 reseat (it, it->current.pos, false);
6577 else if (skipped_p)
6578 reseat (it, it->current.pos, false);
6580 CHECK_IT (it);
6585 /***********************************************************************
6586 Changing an iterator's position
6587 ***********************************************************************/
6589 /* Change IT's current position to POS in current_buffer.
6590 If FORCE_P, always check for text properties at the new position.
6591 Otherwise, text properties are only looked up if POS >=
6592 IT->check_charpos of a property. */
6594 static void
6595 reseat (struct it *it, struct text_pos pos, bool force_p)
6597 ptrdiff_t original_pos = IT_CHARPOS (*it);
6599 reseat_1 (it, pos, false);
6601 /* Determine where to check text properties. Avoid doing it
6602 where possible because text property lookup is very expensive. */
6603 if (force_p
6604 || CHARPOS (pos) > it->stop_charpos
6605 || CHARPOS (pos) < original_pos)
6607 if (it->bidi_p)
6609 /* For bidi iteration, we need to prime prev_stop and
6610 base_level_stop with our best estimations. */
6611 /* Implementation note: Of course, POS is not necessarily a
6612 stop position, so assigning prev_pos to it is a lie; we
6613 should have called compute_stop_backwards. However, if
6614 the current buffer does not include any R2L characters,
6615 that call would be a waste of cycles, because the
6616 iterator will never move back, and thus never cross this
6617 "fake" stop position. So we delay that backward search
6618 until the time we really need it, in next_element_from_buffer. */
6619 if (CHARPOS (pos) != it->prev_stop)
6620 it->prev_stop = CHARPOS (pos);
6621 if (CHARPOS (pos) < it->base_level_stop)
6622 it->base_level_stop = 0; /* meaning it's unknown */
6623 handle_stop (it);
6625 else
6627 handle_stop (it);
6628 it->prev_stop = it->base_level_stop = 0;
6633 CHECK_IT (it);
6637 /* Change IT's buffer position to POS. SET_STOP_P means set
6638 IT->stop_pos to POS, also. */
6640 static void
6641 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6643 /* Don't call this function when scanning a C string. */
6644 eassert (it->s == NULL);
6646 /* POS must be a reasonable value. */
6647 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6649 it->current.pos = it->position = pos;
6650 it->end_charpos = ZV;
6651 it->dpvec = NULL;
6652 it->current.dpvec_index = -1;
6653 it->current.overlay_string_index = -1;
6654 IT_STRING_CHARPOS (*it) = -1;
6655 IT_STRING_BYTEPOS (*it) = -1;
6656 it->string = Qnil;
6657 it->method = GET_FROM_BUFFER;
6658 it->object = it->w->contents;
6659 it->area = TEXT_AREA;
6660 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6661 it->sp = 0;
6662 it->string_from_display_prop_p = false;
6663 it->string_from_prefix_prop_p = false;
6665 it->from_disp_prop_p = false;
6666 it->face_before_selective_p = false;
6667 if (it->bidi_p)
6669 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6670 &it->bidi_it);
6671 bidi_unshelve_cache (NULL, false);
6672 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6673 it->bidi_it.string.s = NULL;
6674 it->bidi_it.string.lstring = Qnil;
6675 it->bidi_it.string.bufpos = 0;
6676 it->bidi_it.string.from_disp_str = false;
6677 it->bidi_it.string.unibyte = false;
6678 it->bidi_it.w = it->w;
6681 if (set_stop_p)
6683 it->stop_charpos = CHARPOS (pos);
6684 it->base_level_stop = CHARPOS (pos);
6686 /* This make the information stored in it->cmp_it invalidate. */
6687 it->cmp_it.id = -1;
6691 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6692 If S is non-null, it is a C string to iterate over. Otherwise,
6693 STRING gives a Lisp string to iterate over.
6695 If PRECISION > 0, don't return more then PRECISION number of
6696 characters from the string.
6698 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6699 characters have been returned. FIELD_WIDTH < 0 means an infinite
6700 field width.
6702 MULTIBYTE = 0 means disable processing of multibyte characters,
6703 MULTIBYTE > 0 means enable it,
6704 MULTIBYTE < 0 means use IT->multibyte_p.
6706 IT must be initialized via a prior call to init_iterator before
6707 calling this function. */
6709 static void
6710 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6711 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6712 int multibyte)
6714 /* No text property checks performed by default, but see below. */
6715 it->stop_charpos = -1;
6717 /* Set iterator position and end position. */
6718 memset (&it->current, 0, sizeof it->current);
6719 it->current.overlay_string_index = -1;
6720 it->current.dpvec_index = -1;
6721 eassert (charpos >= 0);
6723 /* If STRING is specified, use its multibyteness, otherwise use the
6724 setting of MULTIBYTE, if specified. */
6725 if (multibyte >= 0)
6726 it->multibyte_p = multibyte > 0;
6728 /* Bidirectional reordering of strings is controlled by the default
6729 value of bidi-display-reordering. Don't try to reorder while
6730 loading loadup.el, as the necessary character property tables are
6731 not yet available. */
6732 it->bidi_p =
6733 !redisplay__inhibit_bidi
6734 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6736 if (s == NULL)
6738 eassert (STRINGP (string));
6739 it->string = string;
6740 it->s = NULL;
6741 it->end_charpos = it->string_nchars = SCHARS (string);
6742 it->method = GET_FROM_STRING;
6743 it->current.string_pos = string_pos (charpos, string);
6745 if (it->bidi_p)
6747 it->bidi_it.string.lstring = string;
6748 it->bidi_it.string.s = NULL;
6749 it->bidi_it.string.schars = it->end_charpos;
6750 it->bidi_it.string.bufpos = 0;
6751 it->bidi_it.string.from_disp_str = false;
6752 it->bidi_it.string.unibyte = !it->multibyte_p;
6753 it->bidi_it.w = it->w;
6754 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6755 FRAME_WINDOW_P (it->f), &it->bidi_it);
6758 else
6760 it->s = (const unsigned char *) s;
6761 it->string = Qnil;
6763 /* Note that we use IT->current.pos, not it->current.string_pos,
6764 for displaying C strings. */
6765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6766 if (it->multibyte_p)
6768 it->current.pos = c_string_pos (charpos, s, true);
6769 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6771 else
6773 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6774 it->end_charpos = it->string_nchars = strlen (s);
6777 if (it->bidi_p)
6779 it->bidi_it.string.lstring = Qnil;
6780 it->bidi_it.string.s = (const unsigned char *) s;
6781 it->bidi_it.string.schars = it->end_charpos;
6782 it->bidi_it.string.bufpos = 0;
6783 it->bidi_it.string.from_disp_str = false;
6784 it->bidi_it.string.unibyte = !it->multibyte_p;
6785 it->bidi_it.w = it->w;
6786 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6787 &it->bidi_it);
6789 it->method = GET_FROM_C_STRING;
6792 /* PRECISION > 0 means don't return more than PRECISION characters
6793 from the string. */
6794 if (precision > 0 && it->end_charpos - charpos > precision)
6796 it->end_charpos = it->string_nchars = charpos + precision;
6797 if (it->bidi_p)
6798 it->bidi_it.string.schars = it->end_charpos;
6801 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6802 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6803 FIELD_WIDTH < 0 means infinite field width. This is useful for
6804 padding with `-' at the end of a mode line. */
6805 if (field_width < 0)
6806 field_width = DISP_INFINITY;
6807 /* Implementation note: We deliberately don't enlarge
6808 it->bidi_it.string.schars here to fit it->end_charpos, because
6809 the bidi iterator cannot produce characters out of thin air. */
6810 if (field_width > it->end_charpos - charpos)
6811 it->end_charpos = charpos + field_width;
6813 /* Use the standard display table for displaying strings. */
6814 if (DISP_TABLE_P (Vstandard_display_table))
6815 it->dp = XCHAR_TABLE (Vstandard_display_table);
6817 it->stop_charpos = charpos;
6818 it->prev_stop = charpos;
6819 it->base_level_stop = 0;
6820 if (it->bidi_p)
6822 it->bidi_it.first_elt = true;
6823 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6824 it->bidi_it.disp_pos = -1;
6826 if (s == NULL && it->multibyte_p)
6828 ptrdiff_t endpos = SCHARS (it->string);
6829 if (endpos > it->end_charpos)
6830 endpos = it->end_charpos;
6831 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6832 it->string);
6834 CHECK_IT (it);
6839 /***********************************************************************
6840 Iteration
6841 ***********************************************************************/
6843 /* Map enum it_method value to corresponding next_element_from_* function. */
6845 typedef bool (*next_element_function) (struct it *);
6847 static next_element_function const get_next_element[NUM_IT_METHODS] =
6849 next_element_from_buffer,
6850 next_element_from_display_vector,
6851 next_element_from_string,
6852 next_element_from_c_string,
6853 next_element_from_image,
6854 next_element_from_stretch,
6855 next_element_from_xwidget,
6858 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6861 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6862 (possibly with the following characters). */
6864 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6865 ((IT)->cmp_it.id >= 0 \
6866 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6867 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6868 END_CHARPOS, (IT)->w, \
6869 FACE_FROM_ID_OR_NULL ((IT)->f, \
6870 (IT)->face_id), \
6871 (IT)->string)))
6874 /* Lookup the char-table Vglyphless_char_display for character C (-1
6875 if we want information for no-font case), and return the display
6876 method symbol. By side-effect, update it->what and
6877 it->glyphless_method. This function is called from
6878 get_next_display_element for each character element, and from
6879 x_produce_glyphs when no suitable font was found. */
6881 Lisp_Object
6882 lookup_glyphless_char_display (int c, struct it *it)
6884 Lisp_Object glyphless_method = Qnil;
6886 if (CHAR_TABLE_P (Vglyphless_char_display)
6887 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6889 if (c >= 0)
6891 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6892 if (CONSP (glyphless_method))
6893 glyphless_method = FRAME_WINDOW_P (it->f)
6894 ? XCAR (glyphless_method)
6895 : XCDR (glyphless_method);
6897 else
6898 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6901 retry:
6902 if (NILP (glyphless_method))
6904 if (c >= 0)
6905 /* The default is to display the character by a proper font. */
6906 return Qnil;
6907 /* The default for the no-font case is to display an empty box. */
6908 glyphless_method = Qempty_box;
6910 if (EQ (glyphless_method, Qzero_width))
6912 if (c >= 0)
6913 return glyphless_method;
6914 /* This method can't be used for the no-font case. */
6915 glyphless_method = Qempty_box;
6917 if (EQ (glyphless_method, Qthin_space))
6918 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6919 else if (EQ (glyphless_method, Qempty_box))
6920 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6921 else if (EQ (glyphless_method, Qhex_code))
6922 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6923 else if (STRINGP (glyphless_method))
6924 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6925 else
6927 /* Invalid value. We use the default method. */
6928 glyphless_method = Qnil;
6929 goto retry;
6931 it->what = IT_GLYPHLESS;
6932 return glyphless_method;
6935 /* Merge escape glyph face and cache the result. */
6937 static struct frame *last_escape_glyph_frame = NULL;
6938 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6939 static int last_escape_glyph_merged_face_id = 0;
6941 static int
6942 merge_escape_glyph_face (struct it *it)
6944 int face_id;
6946 if (it->f == last_escape_glyph_frame
6947 && it->face_id == last_escape_glyph_face_id)
6948 face_id = last_escape_glyph_merged_face_id;
6949 else
6951 /* Merge the `escape-glyph' face into the current face. */
6952 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6953 last_escape_glyph_frame = it->f;
6954 last_escape_glyph_face_id = it->face_id;
6955 last_escape_glyph_merged_face_id = face_id;
6957 return face_id;
6960 /* Likewise for glyphless glyph face. */
6962 static struct frame *last_glyphless_glyph_frame = NULL;
6963 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6964 static int last_glyphless_glyph_merged_face_id = 0;
6967 merge_glyphless_glyph_face (struct it *it)
6969 int face_id;
6971 if (it->f == last_glyphless_glyph_frame
6972 && it->face_id == last_glyphless_glyph_face_id)
6973 face_id = last_glyphless_glyph_merged_face_id;
6974 else
6976 /* Merge the `glyphless-char' face into the current face. */
6977 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6978 last_glyphless_glyph_frame = it->f;
6979 last_glyphless_glyph_face_id = it->face_id;
6980 last_glyphless_glyph_merged_face_id = face_id;
6982 return face_id;
6985 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6986 be called before redisplaying windows, and when the frame's face
6987 cache is freed. */
6988 void
6989 forget_escape_and_glyphless_faces (void)
6991 last_escape_glyph_frame = NULL;
6992 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6993 last_glyphless_glyph_frame = NULL;
6994 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6997 /* Load IT's display element fields with information about the next
6998 display element from the current position of IT. Value is false if
6999 end of buffer (or C string) is reached. */
7001 static bool
7002 get_next_display_element (struct it *it)
7004 /* True means that we found a display element. False means that
7005 we hit the end of what we iterate over. Performance note: the
7006 function pointer `method' used here turns out to be faster than
7007 using a sequence of if-statements. */
7008 bool success_p;
7010 get_next:
7011 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7013 if (it->what == IT_CHARACTER)
7015 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7016 and only if (a) the resolved directionality of that character
7017 is R..." */
7018 /* FIXME: Do we need an exception for characters from display
7019 tables? */
7020 if (it->bidi_p && it->bidi_it.type == STRONG_R
7021 && !inhibit_bidi_mirroring)
7022 it->c = bidi_mirror_char (it->c);
7023 /* Map via display table or translate control characters.
7024 IT->c, IT->len etc. have been set to the next character by
7025 the function call above. If we have a display table, and it
7026 contains an entry for IT->c, translate it. Don't do this if
7027 IT->c itself comes from a display table, otherwise we could
7028 end up in an infinite recursion. (An alternative could be to
7029 count the recursion depth of this function and signal an
7030 error when a certain maximum depth is reached.) Is it worth
7031 it? */
7032 if (success_p && it->dpvec == NULL)
7034 Lisp_Object dv;
7035 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7036 bool nonascii_space_p = false;
7037 bool nonascii_hyphen_p = false;
7038 int c = it->c; /* This is the character to display. */
7040 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7042 eassert (SINGLE_BYTE_CHAR_P (c));
7043 if (unibyte_display_via_language_environment)
7045 c = DECODE_CHAR (unibyte, c);
7046 if (c < 0)
7047 c = BYTE8_TO_CHAR (it->c);
7049 else
7050 c = BYTE8_TO_CHAR (it->c);
7053 if (it->dp
7054 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7055 VECTORP (dv)))
7057 struct Lisp_Vector *v = XVECTOR (dv);
7059 /* Return the first character from the display table
7060 entry, if not empty. If empty, don't display the
7061 current character. */
7062 if (v->header.size)
7064 it->dpvec_char_len = it->len;
7065 it->dpvec = v->contents;
7066 it->dpend = v->contents + v->header.size;
7067 it->current.dpvec_index = 0;
7068 it->dpvec_face_id = -1;
7069 it->saved_face_id = it->face_id;
7070 it->method = GET_FROM_DISPLAY_VECTOR;
7071 it->ellipsis_p = false;
7073 else
7075 set_iterator_to_next (it, false);
7077 goto get_next;
7080 if (! NILP (lookup_glyphless_char_display (c, it)))
7082 if (it->what == IT_GLYPHLESS)
7083 goto done;
7084 /* Don't display this character. */
7085 set_iterator_to_next (it, false);
7086 goto get_next;
7089 /* If `nobreak-char-display' is non-nil, we display
7090 non-ASCII spaces and hyphens specially. */
7091 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7093 if (c == NO_BREAK_SPACE)
7094 nonascii_space_p = true;
7095 else if (c == SOFT_HYPHEN || c == HYPHEN
7096 || c == NON_BREAKING_HYPHEN)
7097 nonascii_hyphen_p = true;
7100 /* Translate control characters into `\003' or `^C' form.
7101 Control characters coming from a display table entry are
7102 currently not translated because we use IT->dpvec to hold
7103 the translation. This could easily be changed but I
7104 don't believe that it is worth doing.
7106 The characters handled by `nobreak-char-display' must be
7107 translated too.
7109 Non-printable characters and raw-byte characters are also
7110 translated to octal or hexadecimal form. */
7111 if (((c < ' ' || c == 127) /* ASCII control chars. */
7112 ? (it->area != TEXT_AREA
7113 /* In mode line, treat \n, \t like other crl chars. */
7114 || (c != '\t'
7115 && it->glyph_row
7116 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7117 || (c != '\n' && c != '\t'))
7118 : (nonascii_space_p
7119 || nonascii_hyphen_p
7120 || CHAR_BYTE8_P (c)
7121 || ! CHAR_PRINTABLE_P (c))))
7123 /* C is a control character, non-ASCII space/hyphen,
7124 raw-byte, or a non-printable character which must be
7125 displayed either as '\003' or as `^C' where the '\\'
7126 and '^' can be defined in the display table. Fill
7127 IT->ctl_chars with glyphs for what we have to
7128 display. Then, set IT->dpvec to these glyphs. */
7129 Lisp_Object gc;
7130 int ctl_len;
7131 int face_id;
7132 int lface_id = 0;
7133 int escape_glyph;
7135 /* Handle control characters with ^. */
7137 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7139 int g;
7141 g = '^'; /* default glyph for Control */
7142 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7143 if (it->dp
7144 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7146 g = GLYPH_CODE_CHAR (gc);
7147 lface_id = GLYPH_CODE_FACE (gc);
7150 face_id = (lface_id
7151 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7152 : merge_escape_glyph_face (it));
7154 XSETINT (it->ctl_chars[0], g);
7155 XSETINT (it->ctl_chars[1], c ^ 0100);
7156 ctl_len = 2;
7157 goto display_control;
7160 /* Handle non-ascii space in the mode where it only gets
7161 highlighting. */
7163 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7165 /* Merge `nobreak-space' into the current face. */
7166 face_id = merge_faces (it->f, Qnobreak_space, 0,
7167 it->face_id);
7168 XSETINT (it->ctl_chars[0], ' ');
7169 ctl_len = 1;
7170 goto display_control;
7173 /* Handle non-ascii hyphens in the mode where it only
7174 gets highlighting. */
7176 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7178 /* Merge `nobreak-space' into the current face. */
7179 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7180 it->face_id);
7181 XSETINT (it->ctl_chars[0], '-');
7182 ctl_len = 1;
7183 goto display_control;
7186 /* Handle sequences that start with the "escape glyph". */
7188 /* the default escape glyph is \. */
7189 escape_glyph = '\\';
7191 if (it->dp
7192 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7194 escape_glyph = GLYPH_CODE_CHAR (gc);
7195 lface_id = GLYPH_CODE_FACE (gc);
7198 face_id = (lface_id
7199 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7200 : merge_escape_glyph_face (it));
7202 /* Draw non-ASCII space/hyphen with escape glyph: */
7204 if (nonascii_space_p || nonascii_hyphen_p)
7206 XSETINT (it->ctl_chars[0], escape_glyph);
7207 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7208 ctl_len = 2;
7209 goto display_control;
7213 char str[10];
7214 int len, i;
7216 if (CHAR_BYTE8_P (c))
7217 /* Display \200 or \x80 instead of \17777600. */
7218 c = CHAR_TO_BYTE8 (c);
7219 const char *format_string = display_raw_bytes_as_hex
7220 ? "x%02x"
7221 : "%03o";
7222 len = sprintf (str, format_string, c + 0u);
7224 XSETINT (it->ctl_chars[0], escape_glyph);
7225 for (i = 0; i < len; i++)
7226 XSETINT (it->ctl_chars[i + 1], str[i]);
7227 ctl_len = len + 1;
7230 display_control:
7231 /* Set up IT->dpvec and return first character from it. */
7232 it->dpvec_char_len = it->len;
7233 it->dpvec = it->ctl_chars;
7234 it->dpend = it->dpvec + ctl_len;
7235 it->current.dpvec_index = 0;
7236 it->dpvec_face_id = face_id;
7237 it->saved_face_id = it->face_id;
7238 it->method = GET_FROM_DISPLAY_VECTOR;
7239 it->ellipsis_p = false;
7240 goto get_next;
7242 it->char_to_display = c;
7244 else if (success_p)
7246 it->char_to_display = it->c;
7250 #ifdef HAVE_WINDOW_SYSTEM
7251 /* Adjust face id for a multibyte character. There are no multibyte
7252 character in unibyte text. */
7253 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7254 && it->multibyte_p
7255 && success_p
7256 && FRAME_WINDOW_P (it->f))
7258 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7260 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7262 /* Automatic composition with glyph-string. */
7263 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7265 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7267 else
7269 ptrdiff_t pos = (it->s ? -1
7270 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7271 : IT_CHARPOS (*it));
7272 int c;
7274 if (it->what == IT_CHARACTER)
7275 c = it->char_to_display;
7276 else
7278 struct composition *cmp = composition_table[it->cmp_it.id];
7279 int i;
7281 c = ' ';
7282 for (i = 0; i < cmp->glyph_len; i++)
7283 /* TAB in a composition means display glyphs with
7284 padding space on the left or right. */
7285 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7286 break;
7288 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7291 #endif /* HAVE_WINDOW_SYSTEM */
7293 done:
7294 /* Is this character the last one of a run of characters with
7295 box? If yes, set IT->end_of_box_run_p to true. */
7296 if (it->face_box_p
7297 && it->s == NULL)
7299 if (it->method == GET_FROM_STRING && it->sp)
7301 int face_id = underlying_face_id (it);
7302 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7304 if (face)
7306 if (face->box == FACE_NO_BOX)
7308 /* If the box comes from face properties in a
7309 display string, check faces in that string. */
7310 int string_face_id = face_after_it_pos (it);
7311 it->end_of_box_run_p
7312 = (FACE_FROM_ID (it->f, string_face_id)->box
7313 == FACE_NO_BOX);
7315 /* Otherwise, the box comes from the underlying face.
7316 If this is the last string character displayed, check
7317 the next buffer location. */
7318 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7319 /* n_overlay_strings is unreliable unless
7320 overlay_string_index is non-negative. */
7321 && ((it->current.overlay_string_index >= 0
7322 && (it->current.overlay_string_index
7323 == it->n_overlay_strings - 1))
7324 /* A string from display property. */
7325 || it->from_disp_prop_p))
7327 ptrdiff_t ignore;
7328 int next_face_id;
7329 bool text_from_string = false;
7330 /* Normally, the next buffer location is stored in
7331 IT->current.pos... */
7332 struct text_pos pos = it->current.pos;
7334 /* ...but for a string from a display property, the
7335 next buffer position is stored in the 'position'
7336 member of the iteration stack slot below the
7337 current one, see handle_single_display_spec. By
7338 contrast, it->current.pos was not yet updated to
7339 point to that buffer position; that will happen
7340 in pop_it, after we finish displaying the current
7341 string. Note that we already checked above that
7342 it->sp is positive, so subtracting one from it is
7343 safe. */
7344 if (it->from_disp_prop_p)
7346 int stackp = it->sp - 1;
7348 /* Find the stack level with data from buffer. */
7349 while (stackp >= 0
7350 && STRINGP ((it->stack + stackp)->string))
7351 stackp--;
7352 if (stackp < 0)
7354 /* If no stack slot was found for iterating
7355 a buffer, we are displaying text from a
7356 string, most probably the mode line or
7357 the header line, and that string has a
7358 display string on some of its
7359 characters. */
7360 text_from_string = true;
7361 pos = it->stack[it->sp - 1].position;
7363 else
7364 pos = (it->stack + stackp)->position;
7366 else
7367 INC_TEXT_POS (pos, it->multibyte_p);
7369 if (text_from_string)
7371 Lisp_Object base_string = it->stack[it->sp - 1].string;
7373 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7374 it->end_of_box_run_p = true;
7375 else
7377 next_face_id
7378 = face_at_string_position (it->w, base_string,
7379 CHARPOS (pos), 0,
7380 &ignore, face_id, false);
7381 it->end_of_box_run_p
7382 = (FACE_FROM_ID (it->f, next_face_id)->box
7383 == FACE_NO_BOX);
7386 else if (CHARPOS (pos) >= ZV)
7387 it->end_of_box_run_p = true;
7388 else
7390 next_face_id =
7391 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7392 CHARPOS (pos)
7393 + TEXT_PROP_DISTANCE_LIMIT,
7394 false, -1);
7395 it->end_of_box_run_p
7396 = (FACE_FROM_ID (it->f, next_face_id)->box
7397 == FACE_NO_BOX);
7402 /* next_element_from_display_vector sets this flag according to
7403 faces of the display vector glyphs, see there. */
7404 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7406 int face_id = face_after_it_pos (it);
7407 it->end_of_box_run_p
7408 = (face_id != it->face_id
7409 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7412 /* If we reached the end of the object we've been iterating (e.g., a
7413 display string or an overlay string), and there's something on
7414 IT->stack, proceed with what's on the stack. It doesn't make
7415 sense to return false if there's unprocessed stuff on the stack,
7416 because otherwise that stuff will never be displayed. */
7417 if (!success_p && it->sp > 0)
7419 set_iterator_to_next (it, false);
7420 success_p = get_next_display_element (it);
7423 /* Value is false if end of buffer or string reached. */
7424 return success_p;
7428 /* Move IT to the next display element.
7430 RESEAT_P means if called on a newline in buffer text,
7431 skip to the next visible line start.
7433 Functions get_next_display_element and set_iterator_to_next are
7434 separate because I find this arrangement easier to handle than a
7435 get_next_display_element function that also increments IT's
7436 position. The way it is we can first look at an iterator's current
7437 display element, decide whether it fits on a line, and if it does,
7438 increment the iterator position. The other way around we probably
7439 would either need a flag indicating whether the iterator has to be
7440 incremented the next time, or we would have to implement a
7441 decrement position function which would not be easy to write. */
7443 void
7444 set_iterator_to_next (struct it *it, bool reseat_p)
7446 /* Reset flags indicating start and end of a sequence of characters
7447 with box. Reset them at the start of this function because
7448 moving the iterator to a new position might set them. */
7449 it->start_of_box_run_p = it->end_of_box_run_p = false;
7451 switch (it->method)
7453 case GET_FROM_BUFFER:
7454 /* The current display element of IT is a character from
7455 current_buffer. Advance in the buffer, and maybe skip over
7456 invisible lines that are so because of selective display. */
7457 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7458 reseat_at_next_visible_line_start (it, false);
7459 else if (it->cmp_it.id >= 0)
7461 /* We are currently getting glyphs from a composition. */
7462 if (! it->bidi_p)
7464 IT_CHARPOS (*it) += it->cmp_it.nchars;
7465 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7467 else
7469 int i;
7471 /* Update IT's char/byte positions to point to the first
7472 character of the next grapheme cluster, or to the
7473 character visually after the current composition. */
7474 for (i = 0; i < it->cmp_it.nchars; i++)
7475 bidi_move_to_visually_next (&it->bidi_it);
7476 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7477 IT_CHARPOS (*it) = it->bidi_it.charpos;
7480 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7481 && it->cmp_it.to < it->cmp_it.nglyphs)
7483 /* Composition created while scanning forward. Proceed
7484 to the next grapheme cluster. */
7485 it->cmp_it.from = it->cmp_it.to;
7487 else if ((it->bidi_p && it->cmp_it.reversed_p)
7488 && it->cmp_it.from > 0)
7490 /* Composition created while scanning backward. Proceed
7491 to the previous grapheme cluster. */
7492 it->cmp_it.to = it->cmp_it.from;
7494 else
7496 /* No more grapheme clusters in this composition.
7497 Find the next stop position. */
7498 ptrdiff_t stop = it->end_charpos;
7500 if (it->bidi_it.scan_dir < 0)
7501 /* Now we are scanning backward and don't know
7502 where to stop. */
7503 stop = -1;
7504 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7505 IT_BYTEPOS (*it), stop, Qnil);
7508 else
7510 eassert (it->len != 0);
7512 if (!it->bidi_p)
7514 IT_BYTEPOS (*it) += it->len;
7515 IT_CHARPOS (*it) += 1;
7517 else
7519 int prev_scan_dir = it->bidi_it.scan_dir;
7520 /* If this is a new paragraph, determine its base
7521 direction (a.k.a. its base embedding level). */
7522 if (it->bidi_it.new_paragraph)
7523 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7524 false);
7525 bidi_move_to_visually_next (&it->bidi_it);
7526 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7527 IT_CHARPOS (*it) = it->bidi_it.charpos;
7528 if (prev_scan_dir != it->bidi_it.scan_dir)
7530 /* As the scan direction was changed, we must
7531 re-compute the stop position for composition. */
7532 ptrdiff_t stop = it->end_charpos;
7533 if (it->bidi_it.scan_dir < 0)
7534 stop = -1;
7535 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7536 IT_BYTEPOS (*it), stop, Qnil);
7539 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7541 break;
7543 case GET_FROM_C_STRING:
7544 /* Current display element of IT is from a C string. */
7545 if (!it->bidi_p
7546 /* If the string position is beyond string's end, it means
7547 next_element_from_c_string is padding the string with
7548 blanks, in which case we bypass the bidi iterator,
7549 because it cannot deal with such virtual characters. */
7550 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7552 IT_BYTEPOS (*it) += it->len;
7553 IT_CHARPOS (*it) += 1;
7555 else
7557 bidi_move_to_visually_next (&it->bidi_it);
7558 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7559 IT_CHARPOS (*it) = it->bidi_it.charpos;
7561 break;
7563 case GET_FROM_DISPLAY_VECTOR:
7564 /* Current display element of IT is from a display table entry.
7565 Advance in the display table definition. Reset it to null if
7566 end reached, and continue with characters from buffers/
7567 strings. */
7568 ++it->current.dpvec_index;
7570 /* Restore face of the iterator to what they were before the
7571 display vector entry (these entries may contain faces). */
7572 it->face_id = it->saved_face_id;
7574 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7576 bool recheck_faces = it->ellipsis_p;
7578 if (it->s)
7579 it->method = GET_FROM_C_STRING;
7580 else if (STRINGP (it->string))
7581 it->method = GET_FROM_STRING;
7582 else
7584 it->method = GET_FROM_BUFFER;
7585 it->object = it->w->contents;
7588 it->dpvec = NULL;
7589 it->current.dpvec_index = -1;
7591 /* Skip over characters which were displayed via IT->dpvec. */
7592 if (it->dpvec_char_len < 0)
7593 reseat_at_next_visible_line_start (it, true);
7594 else if (it->dpvec_char_len > 0)
7596 it->len = it->dpvec_char_len;
7597 set_iterator_to_next (it, reseat_p);
7600 /* Maybe recheck faces after display vector. */
7601 if (recheck_faces)
7603 if (it->method == GET_FROM_STRING)
7604 it->stop_charpos = IT_STRING_CHARPOS (*it);
7605 else
7606 it->stop_charpos = IT_CHARPOS (*it);
7609 break;
7611 case GET_FROM_STRING:
7612 /* Current display element is a character from a Lisp string. */
7613 eassert (it->s == NULL && STRINGP (it->string));
7614 /* Don't advance past string end. These conditions are true
7615 when set_iterator_to_next is called at the end of
7616 get_next_display_element, in which case the Lisp string is
7617 already exhausted, and all we want is pop the iterator
7618 stack. */
7619 if (it->current.overlay_string_index >= 0)
7621 /* This is an overlay string, so there's no padding with
7622 spaces, and the number of characters in the string is
7623 where the string ends. */
7624 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7625 goto consider_string_end;
7627 else
7629 /* Not an overlay string. There could be padding, so test
7630 against it->end_charpos. */
7631 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7632 goto consider_string_end;
7634 if (it->cmp_it.id >= 0)
7636 /* We are delivering display elements from a composition.
7637 Update the string position past the grapheme cluster
7638 we've just processed. */
7639 if (! it->bidi_p)
7641 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7642 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7644 else
7646 int i;
7648 for (i = 0; i < it->cmp_it.nchars; i++)
7649 bidi_move_to_visually_next (&it->bidi_it);
7650 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7651 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7654 /* Did we exhaust all the grapheme clusters of this
7655 composition? */
7656 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7657 && (it->cmp_it.to < it->cmp_it.nglyphs))
7659 /* Not all the grapheme clusters were processed yet;
7660 advance to the next cluster. */
7661 it->cmp_it.from = it->cmp_it.to;
7663 else if ((it->bidi_p && it->cmp_it.reversed_p)
7664 && it->cmp_it.from > 0)
7666 /* Likewise: advance to the next cluster, but going in
7667 the reverse direction. */
7668 it->cmp_it.to = it->cmp_it.from;
7670 else
7672 /* This composition was fully processed; find the next
7673 candidate place for checking for composed
7674 characters. */
7675 /* Always limit string searches to the string length;
7676 any padding spaces are not part of the string, and
7677 there cannot be any compositions in that padding. */
7678 ptrdiff_t stop = SCHARS (it->string);
7680 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7681 stop = -1;
7682 else if (it->end_charpos < stop)
7684 /* Cf. PRECISION in reseat_to_string: we might be
7685 limited in how many of the string characters we
7686 need to deliver. */
7687 stop = it->end_charpos;
7689 composition_compute_stop_pos (&it->cmp_it,
7690 IT_STRING_CHARPOS (*it),
7691 IT_STRING_BYTEPOS (*it), stop,
7692 it->string);
7695 else
7697 if (!it->bidi_p
7698 /* If the string position is beyond string's end, it
7699 means next_element_from_string is padding the string
7700 with blanks, in which case we bypass the bidi
7701 iterator, because it cannot deal with such virtual
7702 characters. */
7703 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7705 IT_STRING_BYTEPOS (*it) += it->len;
7706 IT_STRING_CHARPOS (*it) += 1;
7708 else
7710 int prev_scan_dir = it->bidi_it.scan_dir;
7712 bidi_move_to_visually_next (&it->bidi_it);
7713 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7714 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7715 /* If the scan direction changes, we may need to update
7716 the place where to check for composed characters. */
7717 if (prev_scan_dir != it->bidi_it.scan_dir)
7719 ptrdiff_t stop = SCHARS (it->string);
7721 if (it->bidi_it.scan_dir < 0)
7722 stop = -1;
7723 else if (it->end_charpos < stop)
7724 stop = it->end_charpos;
7726 composition_compute_stop_pos (&it->cmp_it,
7727 IT_STRING_CHARPOS (*it),
7728 IT_STRING_BYTEPOS (*it), stop,
7729 it->string);
7734 consider_string_end:
7736 if (it->current.overlay_string_index >= 0)
7738 /* IT->string is an overlay string. Advance to the
7739 next, if there is one. */
7740 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7742 it->ellipsis_p = false;
7743 next_overlay_string (it);
7744 if (it->ellipsis_p)
7745 setup_for_ellipsis (it, 0);
7748 else
7750 /* IT->string is not an overlay string. If we reached
7751 its end, and there is something on IT->stack, proceed
7752 with what is on the stack. This can be either another
7753 string, this time an overlay string, or a buffer. */
7754 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7755 && it->sp > 0)
7757 pop_it (it);
7758 if (it->method == GET_FROM_STRING)
7759 goto consider_string_end;
7762 break;
7764 case GET_FROM_IMAGE:
7765 case GET_FROM_STRETCH:
7766 case GET_FROM_XWIDGET:
7768 /* The position etc with which we have to proceed are on
7769 the stack. The position may be at the end of a string,
7770 if the `display' property takes up the whole string. */
7771 eassert (it->sp > 0);
7772 pop_it (it);
7773 if (it->method == GET_FROM_STRING)
7774 goto consider_string_end;
7775 break;
7777 default:
7778 /* There are no other methods defined, so this should be a bug. */
7779 emacs_abort ();
7782 eassert (it->method != GET_FROM_STRING
7783 || (STRINGP (it->string)
7784 && IT_STRING_CHARPOS (*it) >= 0));
7787 /* Load IT's display element fields with information about the next
7788 display element which comes from a display table entry or from the
7789 result of translating a control character to one of the forms `^C'
7790 or `\003'.
7792 IT->dpvec holds the glyphs to return as characters.
7793 IT->saved_face_id holds the face id before the display vector--it
7794 is restored into IT->face_id in set_iterator_to_next. */
7796 static bool
7797 next_element_from_display_vector (struct it *it)
7799 Lisp_Object gc;
7800 int prev_face_id = it->face_id;
7801 int next_face_id;
7803 /* Precondition. */
7804 eassert (it->dpvec && it->current.dpvec_index >= 0);
7806 it->face_id = it->saved_face_id;
7808 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7809 That seemed totally bogus - so I changed it... */
7810 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7811 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7813 struct face *this_face, *prev_face, *next_face;
7815 it->c = GLYPH_CODE_CHAR (gc);
7816 it->len = CHAR_BYTES (it->c);
7818 /* The entry may contain a face id to use. Such a face id is
7819 the id of a Lisp face, not a realized face. A face id of
7820 zero means no face is specified. */
7821 if (it->dpvec_face_id >= 0)
7822 it->face_id = it->dpvec_face_id;
7823 else
7825 int lface_id = GLYPH_CODE_FACE (gc);
7826 if (lface_id > 0)
7827 it->face_id = merge_faces (it->f, Qt, lface_id,
7828 it->saved_face_id);
7831 /* Glyphs in the display vector could have the box face, so we
7832 need to set the related flags in the iterator, as
7833 appropriate. */
7834 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7835 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7837 /* Is this character the first character of a box-face run? */
7838 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7839 && (!prev_face
7840 || prev_face->box == FACE_NO_BOX));
7842 /* For the last character of the box-face run, we need to look
7843 either at the next glyph from the display vector, or at the
7844 face we saw before the display vector. */
7845 next_face_id = it->saved_face_id;
7846 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7848 if (it->dpvec_face_id >= 0)
7849 next_face_id = it->dpvec_face_id;
7850 else
7852 int lface_id =
7853 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7855 if (lface_id > 0)
7856 next_face_id = merge_faces (it->f, Qt, lface_id,
7857 it->saved_face_id);
7860 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7861 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7862 && (!next_face
7863 || next_face->box == FACE_NO_BOX));
7864 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7866 else
7867 /* Display table entry is invalid. Return a space. */
7868 it->c = ' ', it->len = 1;
7870 /* Don't change position and object of the iterator here. They are
7871 still the values of the character that had this display table
7872 entry or was translated, and that's what we want. */
7873 it->what = IT_CHARACTER;
7874 return true;
7877 /* Get the first element of string/buffer in the visual order, after
7878 being reseated to a new position in a string or a buffer. */
7879 static void
7880 get_visually_first_element (struct it *it)
7882 bool string_p = STRINGP (it->string) || it->s;
7883 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7884 ptrdiff_t bob = (string_p ? 0 : BEGV);
7886 if (STRINGP (it->string))
7888 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7889 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7891 else
7893 it->bidi_it.charpos = IT_CHARPOS (*it);
7894 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7897 if (it->bidi_it.charpos == eob)
7899 /* Nothing to do, but reset the FIRST_ELT flag, like
7900 bidi_paragraph_init does, because we are not going to
7901 call it. */
7902 it->bidi_it.first_elt = false;
7904 else if (it->bidi_it.charpos == bob
7905 || (!string_p
7906 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7907 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7909 /* If we are at the beginning of a line/string, we can produce
7910 the next element right away. */
7911 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7912 bidi_move_to_visually_next (&it->bidi_it);
7914 else
7916 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7918 /* We need to prime the bidi iterator starting at the line's or
7919 string's beginning, before we will be able to produce the
7920 next element. */
7921 if (string_p)
7922 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7923 else
7924 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7925 IT_BYTEPOS (*it), -1,
7926 &it->bidi_it.bytepos);
7927 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7930 /* Now return to buffer/string position where we were asked
7931 to get the next display element, and produce that. */
7932 bidi_move_to_visually_next (&it->bidi_it);
7934 while (it->bidi_it.bytepos != orig_bytepos
7935 && it->bidi_it.charpos < eob);
7938 /* Adjust IT's position information to where we ended up. */
7939 if (STRINGP (it->string))
7941 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7942 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7944 else
7946 IT_CHARPOS (*it) = it->bidi_it.charpos;
7947 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7950 if (STRINGP (it->string) || !it->s)
7952 ptrdiff_t stop, charpos, bytepos;
7954 if (STRINGP (it->string))
7956 eassert (!it->s);
7957 stop = SCHARS (it->string);
7958 if (stop > it->end_charpos)
7959 stop = it->end_charpos;
7960 charpos = IT_STRING_CHARPOS (*it);
7961 bytepos = IT_STRING_BYTEPOS (*it);
7963 else
7965 stop = it->end_charpos;
7966 charpos = IT_CHARPOS (*it);
7967 bytepos = IT_BYTEPOS (*it);
7969 if (it->bidi_it.scan_dir < 0)
7970 stop = -1;
7971 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7972 it->string);
7976 /* Load IT with the next display element from Lisp string IT->string.
7977 IT->current.string_pos is the current position within the string.
7978 If IT->current.overlay_string_index >= 0, the Lisp string is an
7979 overlay string. */
7981 static bool
7982 next_element_from_string (struct it *it)
7984 struct text_pos position;
7986 eassert (STRINGP (it->string));
7987 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7988 eassert (IT_STRING_CHARPOS (*it) >= 0);
7989 position = it->current.string_pos;
7991 /* With bidi reordering, the character to display might not be the
7992 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7993 that we were reseat()ed to a new string, whose paragraph
7994 direction is not known. */
7995 if (it->bidi_p && it->bidi_it.first_elt)
7997 get_visually_first_element (it);
7998 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
8001 /* Time to check for invisible text? */
8002 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
8004 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
8006 if (!(!it->bidi_p
8007 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8008 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
8010 /* With bidi non-linear iteration, we could find
8011 ourselves far beyond the last computed stop_charpos,
8012 with several other stop positions in between that we
8013 missed. Scan them all now, in buffer's logical
8014 order, until we find and handle the last stop_charpos
8015 that precedes our current position. */
8016 handle_stop_backwards (it, it->stop_charpos);
8017 return GET_NEXT_DISPLAY_ELEMENT (it);
8019 else
8021 if (it->bidi_p)
8023 /* Take note of the stop position we just moved
8024 across, for when we will move back across it. */
8025 it->prev_stop = it->stop_charpos;
8026 /* If we are at base paragraph embedding level, take
8027 note of the last stop position seen at this
8028 level. */
8029 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8030 it->base_level_stop = it->stop_charpos;
8032 handle_stop (it);
8034 /* Since a handler may have changed IT->method, we must
8035 recurse here. */
8036 return GET_NEXT_DISPLAY_ELEMENT (it);
8039 else if (it->bidi_p
8040 /* If we are before prev_stop, we may have overstepped
8041 on our way backwards a stop_pos, and if so, we need
8042 to handle that stop_pos. */
8043 && IT_STRING_CHARPOS (*it) < it->prev_stop
8044 /* We can sometimes back up for reasons that have nothing
8045 to do with bidi reordering. E.g., compositions. The
8046 code below is only needed when we are above the base
8047 embedding level, so test for that explicitly. */
8048 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8050 /* If we lost track of base_level_stop, we have no better
8051 place for handle_stop_backwards to start from than string
8052 beginning. This happens, e.g., when we were reseated to
8053 the previous screenful of text by vertical-motion. */
8054 if (it->base_level_stop <= 0
8055 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8056 it->base_level_stop = 0;
8057 handle_stop_backwards (it, it->base_level_stop);
8058 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 if (it->current.overlay_string_index >= 0)
8064 /* Get the next character from an overlay string. In overlay
8065 strings, there is no field width or padding with spaces to
8066 do. */
8067 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8069 it->what = IT_EOB;
8070 return false;
8072 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8073 IT_STRING_BYTEPOS (*it),
8074 it->bidi_it.scan_dir < 0
8075 ? -1
8076 : SCHARS (it->string))
8077 && next_element_from_composition (it))
8079 return true;
8081 else if (STRING_MULTIBYTE (it->string))
8083 const unsigned char *s = (SDATA (it->string)
8084 + IT_STRING_BYTEPOS (*it));
8085 it->c = string_char_and_length (s, &it->len);
8087 else
8089 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8090 it->len = 1;
8093 else
8095 /* Get the next character from a Lisp string that is not an
8096 overlay string. Such strings come from the mode line, for
8097 example. We may have to pad with spaces, or truncate the
8098 string. See also next_element_from_c_string. */
8099 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8101 it->what = IT_EOB;
8102 return false;
8104 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8106 /* Pad with spaces. */
8107 it->c = ' ', it->len = 1;
8108 CHARPOS (position) = BYTEPOS (position) = -1;
8110 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8111 IT_STRING_BYTEPOS (*it),
8112 it->bidi_it.scan_dir < 0
8113 ? -1
8114 : it->string_nchars)
8115 && next_element_from_composition (it))
8117 return true;
8119 else if (STRING_MULTIBYTE (it->string))
8121 const unsigned char *s = (SDATA (it->string)
8122 + IT_STRING_BYTEPOS (*it));
8123 it->c = string_char_and_length (s, &it->len);
8125 else
8127 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8128 it->len = 1;
8132 /* Record what we have and where it came from. */
8133 it->what = IT_CHARACTER;
8134 it->object = it->string;
8135 it->position = position;
8136 return true;
8140 /* Load IT with next display element from C string IT->s.
8141 IT->string_nchars is the maximum number of characters to return
8142 from the string. IT->end_charpos may be greater than
8143 IT->string_nchars when this function is called, in which case we
8144 may have to return padding spaces. Value is false if end of string
8145 reached, including padding spaces. */
8147 static bool
8148 next_element_from_c_string (struct it *it)
8150 bool success_p = true;
8152 eassert (it->s);
8153 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8154 it->what = IT_CHARACTER;
8155 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8156 it->object = make_number (0);
8158 /* With bidi reordering, the character to display might not be the
8159 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8160 we were reseated to a new string, whose paragraph direction is
8161 not known. */
8162 if (it->bidi_p && it->bidi_it.first_elt)
8163 get_visually_first_element (it);
8165 /* IT's position can be greater than IT->string_nchars in case a
8166 field width or precision has been specified when the iterator was
8167 initialized. */
8168 if (IT_CHARPOS (*it) >= it->end_charpos)
8170 /* End of the game. */
8171 it->what = IT_EOB;
8172 success_p = false;
8174 else if (IT_CHARPOS (*it) >= it->string_nchars)
8176 /* Pad with spaces. */
8177 it->c = ' ', it->len = 1;
8178 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8180 else if (it->multibyte_p)
8181 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8182 else
8183 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8185 return success_p;
8189 /* Set up IT to return characters from an ellipsis, if appropriate.
8190 The definition of the ellipsis glyphs may come from a display table
8191 entry. This function fills IT with the first glyph from the
8192 ellipsis if an ellipsis is to be displayed. */
8194 static bool
8195 next_element_from_ellipsis (struct it *it)
8197 if (it->selective_display_ellipsis_p)
8198 setup_for_ellipsis (it, it->len);
8199 else
8201 /* The face at the current position may be different from the
8202 face we find after the invisible text. Remember what it
8203 was in IT->saved_face_id, and signal that it's there by
8204 setting face_before_selective_p. */
8205 it->saved_face_id = it->face_id;
8206 it->method = GET_FROM_BUFFER;
8207 it->object = it->w->contents;
8208 reseat_at_next_visible_line_start (it, true);
8209 it->face_before_selective_p = true;
8212 return GET_NEXT_DISPLAY_ELEMENT (it);
8216 /* Deliver an image display element. The iterator IT is already
8217 filled with image information (done in handle_display_prop). Value
8218 is always true. */
8221 static bool
8222 next_element_from_image (struct it *it)
8224 it->what = IT_IMAGE;
8225 return true;
8228 static bool
8229 next_element_from_xwidget (struct it *it)
8231 it->what = IT_XWIDGET;
8232 return true;
8236 /* Fill iterator IT with next display element from a stretch glyph
8237 property. IT->object is the value of the text property. Value is
8238 always true. */
8240 static bool
8241 next_element_from_stretch (struct it *it)
8243 it->what = IT_STRETCH;
8244 return true;
8247 /* Scan backwards from IT's current position until we find a stop
8248 position, or until BEGV. This is called when we find ourself
8249 before both the last known prev_stop and base_level_stop while
8250 reordering bidirectional text. */
8252 static void
8253 compute_stop_pos_backwards (struct it *it)
8255 const int SCAN_BACK_LIMIT = 1000;
8256 struct text_pos pos;
8257 struct display_pos save_current = it->current;
8258 struct text_pos save_position = it->position;
8259 ptrdiff_t charpos = IT_CHARPOS (*it);
8260 ptrdiff_t where_we_are = charpos;
8261 ptrdiff_t save_stop_pos = it->stop_charpos;
8262 ptrdiff_t save_end_pos = it->end_charpos;
8264 eassert (NILP (it->string) && !it->s);
8265 eassert (it->bidi_p);
8266 it->bidi_p = false;
8269 it->end_charpos = min (charpos + 1, ZV);
8270 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8271 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8272 reseat_1 (it, pos, false);
8273 compute_stop_pos (it);
8274 /* We must advance forward, right? */
8275 if (it->stop_charpos <= charpos)
8276 emacs_abort ();
8278 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8280 if (it->stop_charpos <= where_we_are)
8281 it->prev_stop = it->stop_charpos;
8282 else
8283 it->prev_stop = BEGV;
8284 it->bidi_p = true;
8285 it->current = save_current;
8286 it->position = save_position;
8287 it->stop_charpos = save_stop_pos;
8288 it->end_charpos = save_end_pos;
8291 /* Scan forward from CHARPOS in the current buffer/string, until we
8292 find a stop position > current IT's position. Then handle the stop
8293 position before that. This is called when we bump into a stop
8294 position while reordering bidirectional text. CHARPOS should be
8295 the last previously processed stop_pos (or BEGV/0, if none were
8296 processed yet) whose position is less that IT's current
8297 position. */
8299 static void
8300 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8302 bool bufp = !STRINGP (it->string);
8303 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8304 struct display_pos save_current = it->current;
8305 struct text_pos save_position = it->position;
8306 struct text_pos pos1;
8307 ptrdiff_t next_stop;
8309 /* Scan in strict logical order. */
8310 eassert (it->bidi_p);
8311 it->bidi_p = false;
8314 it->prev_stop = charpos;
8315 if (bufp)
8317 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8318 reseat_1 (it, pos1, false);
8320 else
8321 it->current.string_pos = string_pos (charpos, it->string);
8322 compute_stop_pos (it);
8323 /* We must advance forward, right? */
8324 if (it->stop_charpos <= it->prev_stop)
8325 emacs_abort ();
8326 charpos = it->stop_charpos;
8328 while (charpos <= where_we_are);
8330 it->bidi_p = true;
8331 it->current = save_current;
8332 it->position = save_position;
8333 next_stop = it->stop_charpos;
8334 it->stop_charpos = it->prev_stop;
8335 handle_stop (it);
8336 it->stop_charpos = next_stop;
8339 /* Load IT with the next display element from current_buffer. Value
8340 is false if end of buffer reached. IT->stop_charpos is the next
8341 position at which to stop and check for text properties or buffer
8342 end. */
8344 static bool
8345 next_element_from_buffer (struct it *it)
8347 bool success_p = true;
8349 eassert (IT_CHARPOS (*it) >= BEGV);
8350 eassert (NILP (it->string) && !it->s);
8351 eassert (!it->bidi_p
8352 || (EQ (it->bidi_it.string.lstring, Qnil)
8353 && it->bidi_it.string.s == NULL));
8355 /* With bidi reordering, the character to display might not be the
8356 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8357 we were reseat()ed to a new buffer position, which is potentially
8358 a different paragraph. */
8359 if (it->bidi_p && it->bidi_it.first_elt)
8361 get_visually_first_element (it);
8362 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8365 if (IT_CHARPOS (*it) >= it->stop_charpos)
8367 if (IT_CHARPOS (*it) >= it->end_charpos)
8369 bool overlay_strings_follow_p;
8371 /* End of the game, except when overlay strings follow that
8372 haven't been returned yet. */
8373 if (it->overlay_strings_at_end_processed_p)
8374 overlay_strings_follow_p = false;
8375 else
8377 it->overlay_strings_at_end_processed_p = true;
8378 overlay_strings_follow_p = get_overlay_strings (it, 0);
8381 if (overlay_strings_follow_p)
8382 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8383 else
8385 it->what = IT_EOB;
8386 it->position = it->current.pos;
8387 success_p = false;
8390 else if (!(!it->bidi_p
8391 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8392 || IT_CHARPOS (*it) == it->stop_charpos))
8394 /* With bidi non-linear iteration, we could find ourselves
8395 far beyond the last computed stop_charpos, with several
8396 other stop positions in between that we missed. Scan
8397 them all now, in buffer's logical order, until we find
8398 and handle the last stop_charpos that precedes our
8399 current position. */
8400 handle_stop_backwards (it, it->stop_charpos);
8401 it->ignore_overlay_strings_at_pos_p = false;
8402 return GET_NEXT_DISPLAY_ELEMENT (it);
8404 else
8406 if (it->bidi_p)
8408 /* Take note of the stop position we just moved across,
8409 for when we will move back across it. */
8410 it->prev_stop = it->stop_charpos;
8411 /* If we are at base paragraph embedding level, take
8412 note of the last stop position seen at this
8413 level. */
8414 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8415 it->base_level_stop = it->stop_charpos;
8417 handle_stop (it);
8418 it->ignore_overlay_strings_at_pos_p = false;
8419 return GET_NEXT_DISPLAY_ELEMENT (it);
8422 else if (it->bidi_p
8423 /* If we are before prev_stop, we may have overstepped on
8424 our way backwards a stop_pos, and if so, we need to
8425 handle that stop_pos. */
8426 && IT_CHARPOS (*it) < it->prev_stop
8427 /* We can sometimes back up for reasons that have nothing
8428 to do with bidi reordering. E.g., compositions. The
8429 code below is only needed when we are above the base
8430 embedding level, so test for that explicitly. */
8431 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8433 if (it->base_level_stop <= 0
8434 || IT_CHARPOS (*it) < it->base_level_stop)
8436 /* If we lost track of base_level_stop, we need to find
8437 prev_stop by looking backwards. This happens, e.g., when
8438 we were reseated to the previous screenful of text by
8439 vertical-motion. */
8440 it->base_level_stop = BEGV;
8441 compute_stop_pos_backwards (it);
8442 handle_stop_backwards (it, it->prev_stop);
8444 else
8445 handle_stop_backwards (it, it->base_level_stop);
8446 it->ignore_overlay_strings_at_pos_p = false;
8447 return GET_NEXT_DISPLAY_ELEMENT (it);
8449 else
8451 /* No face changes, overlays etc. in sight, so just return a
8452 character from current_buffer. */
8453 unsigned char *p;
8454 ptrdiff_t stop;
8456 /* We moved to the next buffer position, so any info about
8457 previously seen overlays is no longer valid. */
8458 it->ignore_overlay_strings_at_pos_p = false;
8460 /* Maybe run the redisplay end trigger hook. Performance note:
8461 This doesn't seem to cost measurable time. */
8462 if (it->redisplay_end_trigger_charpos
8463 && it->glyph_row
8464 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8465 run_redisplay_end_trigger_hook (it);
8467 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8468 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8469 stop)
8470 && next_element_from_composition (it))
8472 return true;
8475 /* Get the next character, maybe multibyte. */
8476 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8477 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8478 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8479 else
8480 it->c = *p, it->len = 1;
8482 /* Record what we have and where it came from. */
8483 it->what = IT_CHARACTER;
8484 it->object = it->w->contents;
8485 it->position = it->current.pos;
8487 /* Normally we return the character found above, except when we
8488 really want to return an ellipsis for selective display. */
8489 if (it->selective)
8491 if (it->c == '\n')
8493 /* A value of selective > 0 means hide lines indented more
8494 than that number of columns. */
8495 if (it->selective > 0
8496 && IT_CHARPOS (*it) + 1 < ZV
8497 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8498 IT_BYTEPOS (*it) + 1,
8499 it->selective))
8501 success_p = next_element_from_ellipsis (it);
8502 it->dpvec_char_len = -1;
8505 else if (it->c == '\r' && it->selective == -1)
8507 /* A value of selective == -1 means that everything from the
8508 CR to the end of the line is invisible, with maybe an
8509 ellipsis displayed for it. */
8510 success_p = next_element_from_ellipsis (it);
8511 it->dpvec_char_len = -1;
8516 /* Value is false if end of buffer reached. */
8517 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8518 return success_p;
8522 /* Run the redisplay end trigger hook for IT. */
8524 static void
8525 run_redisplay_end_trigger_hook (struct it *it)
8527 /* IT->glyph_row should be non-null, i.e. we should be actually
8528 displaying something, or otherwise we should not run the hook. */
8529 eassert (it->glyph_row);
8531 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8532 it->redisplay_end_trigger_charpos = 0;
8534 /* Since we are *trying* to run these functions, don't try to run
8535 them again, even if they get an error. */
8536 wset_redisplay_end_trigger (it->w, Qnil);
8537 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8538 make_number (charpos));
8540 /* Notice if it changed the face of the character we are on. */
8541 handle_face_prop (it);
8545 /* Deliver a composition display element. Unlike the other
8546 next_element_from_XXX, this function is not registered in the array
8547 get_next_element[]. It is called from next_element_from_buffer and
8548 next_element_from_string when necessary. */
8550 static bool
8551 next_element_from_composition (struct it *it)
8553 it->what = IT_COMPOSITION;
8554 it->len = it->cmp_it.nbytes;
8555 if (STRINGP (it->string))
8557 if (it->c < 0)
8559 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8560 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8561 return false;
8563 it->position = it->current.string_pos;
8564 it->object = it->string;
8565 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8566 IT_STRING_BYTEPOS (*it), it->string);
8568 else
8570 if (it->c < 0)
8572 IT_CHARPOS (*it) += it->cmp_it.nchars;
8573 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8574 if (it->bidi_p)
8576 if (it->bidi_it.new_paragraph)
8577 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8578 false);
8579 /* Resync the bidi iterator with IT's new position.
8580 FIXME: this doesn't support bidirectional text. */
8581 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8582 bidi_move_to_visually_next (&it->bidi_it);
8584 return false;
8586 it->position = it->current.pos;
8587 it->object = it->w->contents;
8588 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8589 IT_BYTEPOS (*it), Qnil);
8591 return true;
8596 /***********************************************************************
8597 Moving an iterator without producing glyphs
8598 ***********************************************************************/
8600 /* Check if iterator is at a position corresponding to a valid buffer
8601 position after some move_it_ call. */
8603 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8604 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8607 /* Move iterator IT to a specified buffer or X position within one
8608 line on the display without producing glyphs.
8610 OP should be a bit mask including some or all of these bits:
8611 MOVE_TO_X: Stop upon reaching x-position TO_X.
8612 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8613 Regardless of OP's value, stop upon reaching the end of the display line.
8615 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8616 This means, in particular, that TO_X includes window's horizontal
8617 scroll amount.
8619 The return value has several possible values that
8620 say what condition caused the scan to stop:
8622 MOVE_POS_MATCH_OR_ZV
8623 - when TO_POS or ZV was reached.
8625 MOVE_X_REACHED
8626 -when TO_X was reached before TO_POS or ZV were reached.
8628 MOVE_LINE_CONTINUED
8629 - when we reached the end of the display area and the line must
8630 be continued.
8632 MOVE_LINE_TRUNCATED
8633 - when we reached the end of the display area and the line is
8634 truncated.
8636 MOVE_NEWLINE_OR_CR
8637 - when we stopped at a line end, i.e. a newline or a CR and selective
8638 display is on. */
8640 static enum move_it_result
8641 move_it_in_display_line_to (struct it *it,
8642 ptrdiff_t to_charpos, int to_x,
8643 enum move_operation_enum op)
8645 enum move_it_result result = MOVE_UNDEFINED;
8646 struct glyph_row *saved_glyph_row;
8647 struct it wrap_it, atpos_it, atx_it, ppos_it;
8648 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8649 void *ppos_data = NULL;
8650 bool may_wrap = false;
8651 enum it_method prev_method = it->method;
8652 ptrdiff_t closest_pos UNINIT;
8653 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8654 bool saw_smaller_pos = prev_pos < to_charpos;
8655 bool line_number_pending = false;
8657 /* Don't produce glyphs in produce_glyphs. */
8658 saved_glyph_row = it->glyph_row;
8659 it->glyph_row = NULL;
8661 /* Use wrap_it to save a copy of IT wherever a word wrap could
8662 occur. Use atpos_it to save a copy of IT at the desired buffer
8663 position, if found, so that we can scan ahead and check if the
8664 word later overshoots the window edge. Use atx_it similarly, for
8665 pixel positions. */
8666 wrap_it.sp = -1;
8667 atpos_it.sp = -1;
8668 atx_it.sp = -1;
8670 /* Use ppos_it under bidi reordering to save a copy of IT for the
8671 initial position. We restore that position in IT when we have
8672 scanned the entire display line without finding a match for
8673 TO_CHARPOS and all the character positions are greater than
8674 TO_CHARPOS. We then restart the scan from the initial position,
8675 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8676 the closest to TO_CHARPOS. */
8677 if (it->bidi_p)
8679 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8681 SAVE_IT (ppos_it, *it, ppos_data);
8682 closest_pos = IT_CHARPOS (*it);
8684 else
8685 closest_pos = ZV;
8688 #define BUFFER_POS_REACHED_P() \
8689 ((op & MOVE_TO_POS) != 0 \
8690 && BUFFERP (it->object) \
8691 && (IT_CHARPOS (*it) == to_charpos \
8692 || ((!it->bidi_p \
8693 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8694 && IT_CHARPOS (*it) > to_charpos) \
8695 || (it->what == IT_COMPOSITION \
8696 && ((IT_CHARPOS (*it) > to_charpos \
8697 && to_charpos >= it->cmp_it.charpos) \
8698 || (IT_CHARPOS (*it) < to_charpos \
8699 && to_charpos <= it->cmp_it.charpos)))) \
8700 && (it->method == GET_FROM_BUFFER \
8701 || (it->method == GET_FROM_DISPLAY_VECTOR \
8702 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8704 if (it->hpos == 0)
8706 /* If line numbers are being displayed, produce a line number. */
8707 if (should_produce_line_number (it))
8709 if (it->current_x == it->first_visible_x)
8710 maybe_produce_line_number (it);
8711 else
8712 line_number_pending = true;
8714 /* If there's a line-/wrap-prefix, handle it. */
8715 if (it->method == GET_FROM_BUFFER)
8716 handle_line_prefix (it);
8719 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8720 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8722 while (true)
8724 int x, i, ascent = 0, descent = 0;
8726 /* Utility macro to reset an iterator with x, ascent, and descent. */
8727 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8728 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8729 (IT)->max_descent = descent)
8731 /* Stop if we move beyond TO_CHARPOS (after an image or a
8732 display string or stretch glyph). */
8733 if ((op & MOVE_TO_POS) != 0
8734 && BUFFERP (it->object)
8735 && it->method == GET_FROM_BUFFER
8736 && (((!it->bidi_p
8737 /* When the iterator is at base embedding level, we
8738 are guaranteed that characters are delivered for
8739 display in strictly increasing order of their
8740 buffer positions. */
8741 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8742 && IT_CHARPOS (*it) > to_charpos)
8743 || (it->bidi_p
8744 && (prev_method == GET_FROM_IMAGE
8745 || prev_method == GET_FROM_STRETCH
8746 || prev_method == GET_FROM_STRING)
8747 /* Passed TO_CHARPOS from left to right. */
8748 && ((prev_pos < to_charpos
8749 && IT_CHARPOS (*it) > to_charpos)
8750 /* Passed TO_CHARPOS from right to left. */
8751 || (prev_pos > to_charpos
8752 && IT_CHARPOS (*it) < to_charpos)))))
8754 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8756 result = MOVE_POS_MATCH_OR_ZV;
8757 break;
8759 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8760 /* If wrap_it is valid, the current position might be in a
8761 word that is wrapped. So, save the iterator in
8762 atpos_it and continue to see if wrapping happens. */
8763 SAVE_IT (atpos_it, *it, atpos_data);
8766 /* Stop when ZV reached.
8767 We used to stop here when TO_CHARPOS reached as well, but that is
8768 too soon if this glyph does not fit on this line. So we handle it
8769 explicitly below. */
8770 if (!get_next_display_element (it))
8772 result = MOVE_POS_MATCH_OR_ZV;
8773 break;
8776 if (it->line_wrap == TRUNCATE)
8778 /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
8779 produced something that doesn't consume any screen estate
8780 in the text area, so we don't want to exit the loop at
8781 TO_CHARPOS, before we produce the glyph for that buffer
8782 position. This happens, e.g., when there's an overlay at
8783 TO_CHARPOS that draws a fringe bitmap. */
8784 if (BUFFER_POS_REACHED_P ()
8785 && (it->pixel_width > 0
8786 || IT_CHARPOS (*it) > to_charpos
8787 || it->area != TEXT_AREA))
8789 result = MOVE_POS_MATCH_OR_ZV;
8790 break;
8793 else
8795 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8797 if (IT_DISPLAYING_WHITESPACE (it))
8798 may_wrap = true;
8799 else if (may_wrap)
8801 /* We have reached a glyph that follows one or more
8802 whitespace characters. If the position is
8803 already found, we are done. */
8804 if (atpos_it.sp >= 0)
8806 RESTORE_IT (it, &atpos_it, atpos_data);
8807 result = MOVE_POS_MATCH_OR_ZV;
8808 goto done;
8810 if (atx_it.sp >= 0)
8812 RESTORE_IT (it, &atx_it, atx_data);
8813 result = MOVE_X_REACHED;
8814 goto done;
8816 /* Otherwise, we can wrap here. */
8817 SAVE_IT (wrap_it, *it, wrap_data);
8818 may_wrap = false;
8823 /* Remember the line height for the current line, in case
8824 the next element doesn't fit on the line. */
8825 ascent = it->max_ascent;
8826 descent = it->max_descent;
8828 /* The call to produce_glyphs will get the metrics of the
8829 display element IT is loaded with. Record the x-position
8830 before this display element, in case it doesn't fit on the
8831 line. */
8832 x = it->current_x;
8834 PRODUCE_GLYPHS (it);
8836 if (it->area != TEXT_AREA)
8838 prev_method = it->method;
8839 if (it->method == GET_FROM_BUFFER)
8840 prev_pos = IT_CHARPOS (*it);
8841 set_iterator_to_next (it, true);
8842 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8843 SET_TEXT_POS (this_line_min_pos,
8844 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8845 if (it->bidi_p
8846 && (op & MOVE_TO_POS)
8847 && IT_CHARPOS (*it) > to_charpos
8848 && IT_CHARPOS (*it) < closest_pos)
8849 closest_pos = IT_CHARPOS (*it);
8850 continue;
8853 /* The number of glyphs we get back in IT->nglyphs will normally
8854 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8855 character on a terminal frame, or (iii) a line end. For the
8856 second case, IT->nglyphs - 1 padding glyphs will be present.
8857 (On X frames, there is only one glyph produced for a
8858 composite character.)
8860 The behavior implemented below means, for continuation lines,
8861 that as many spaces of a TAB as fit on the current line are
8862 displayed there. For terminal frames, as many glyphs of a
8863 multi-glyph character are displayed in the current line, too.
8864 This is what the old redisplay code did, and we keep it that
8865 way. Under X, the whole shape of a complex character must
8866 fit on the line or it will be completely displayed in the
8867 next line.
8869 Note that both for tabs and padding glyphs, all glyphs have
8870 the same width. */
8871 if (it->nglyphs)
8873 /* More than one glyph or glyph doesn't fit on line. All
8874 glyphs have the same width. */
8875 int single_glyph_width = it->pixel_width / it->nglyphs;
8876 int new_x;
8877 int x_before_this_char = x;
8878 int hpos_before_this_char = it->hpos;
8880 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8882 new_x = x + single_glyph_width;
8884 /* We want to leave anything reaching TO_X to the caller. */
8885 if ((op & MOVE_TO_X) && new_x > to_x)
8887 if (BUFFER_POS_REACHED_P ())
8889 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8890 goto buffer_pos_reached;
8891 if (atpos_it.sp < 0)
8893 SAVE_IT (atpos_it, *it, atpos_data);
8894 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8897 else
8899 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8901 it->current_x = x;
8902 result = MOVE_X_REACHED;
8903 break;
8905 if (atx_it.sp < 0)
8907 SAVE_IT (atx_it, *it, atx_data);
8908 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8913 if (/* Lines are continued. */
8914 it->line_wrap != TRUNCATE
8915 && (/* And glyph doesn't fit on the line. */
8916 new_x > it->last_visible_x
8917 /* Or it fits exactly and we're on a window
8918 system frame. */
8919 || (new_x == it->last_visible_x
8920 && FRAME_WINDOW_P (it->f)
8921 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8922 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8923 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8925 bool moved_forward = false;
8927 if (/* IT->hpos == 0 means the very first glyph
8928 doesn't fit on the line, e.g. a wide image. */
8929 it->hpos == 0
8930 || (new_x == it->last_visible_x
8931 && FRAME_WINDOW_P (it->f)))
8933 ++it->hpos;
8934 it->current_x = new_x;
8936 /* The character's last glyph just barely fits
8937 in this row. */
8938 if (i == it->nglyphs - 1)
8940 /* If this is the destination position,
8941 return a position *before* it in this row,
8942 now that we know it fits in this row. */
8943 if (BUFFER_POS_REACHED_P ())
8945 bool can_wrap = true;
8947 /* If we are at a whitespace character
8948 that barely fits on this screen line,
8949 but the next character is also
8950 whitespace, we cannot wrap here. */
8951 if (it->line_wrap == WORD_WRAP
8952 && wrap_it.sp >= 0
8953 && may_wrap
8954 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8956 struct it tem_it;
8957 void *tem_data = NULL;
8959 SAVE_IT (tem_it, *it, tem_data);
8960 set_iterator_to_next (it, true);
8961 if (get_next_display_element (it)
8962 && IT_DISPLAYING_WHITESPACE (it))
8963 can_wrap = false;
8964 RESTORE_IT (it, &tem_it, tem_data);
8966 if (it->line_wrap != WORD_WRAP
8967 || wrap_it.sp < 0
8968 /* If we've just found whitespace
8969 where we can wrap, effectively
8970 ignore the previous wrap point --
8971 it is no longer relevant, but we
8972 won't have an opportunity to
8973 update it, since we've reached
8974 the edge of this screen line. */
8975 || (may_wrap && can_wrap
8976 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8978 it->hpos = hpos_before_this_char;
8979 it->current_x = x_before_this_char;
8980 result = MOVE_POS_MATCH_OR_ZV;
8981 break;
8983 if (it->line_wrap == WORD_WRAP
8984 && atpos_it.sp < 0)
8986 SAVE_IT (atpos_it, *it, atpos_data);
8987 atpos_it.current_x = x_before_this_char;
8988 atpos_it.hpos = hpos_before_this_char;
8992 prev_method = it->method;
8993 if (it->method == GET_FROM_BUFFER)
8994 prev_pos = IT_CHARPOS (*it);
8995 set_iterator_to_next (it, true);
8996 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8997 SET_TEXT_POS (this_line_min_pos,
8998 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8999 /* On graphical terminals, newlines may
9000 "overflow" into the fringe if
9001 overflow-newline-into-fringe is non-nil.
9002 On text terminals, and on graphical
9003 terminals with no right margin, newlines
9004 may overflow into the last glyph on the
9005 display line.*/
9006 if (!FRAME_WINDOW_P (it->f)
9007 || ((it->bidi_p
9008 && it->bidi_it.paragraph_dir == R2L)
9009 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9010 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9011 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9013 if (!get_next_display_element (it))
9015 result = MOVE_POS_MATCH_OR_ZV;
9016 break;
9018 moved_forward = true;
9019 if (BUFFER_POS_REACHED_P ())
9021 if (ITERATOR_AT_END_OF_LINE_P (it))
9022 result = MOVE_POS_MATCH_OR_ZV;
9023 else
9024 result = MOVE_LINE_CONTINUED;
9025 break;
9027 if (ITERATOR_AT_END_OF_LINE_P (it)
9028 && (it->line_wrap != WORD_WRAP
9029 || wrap_it.sp < 0
9030 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9032 result = MOVE_NEWLINE_OR_CR;
9033 break;
9038 else
9039 IT_RESET_X_ASCENT_DESCENT (it);
9041 /* If the screen line ends with whitespace, and we
9042 are under word-wrap, don't use wrap_it: it is no
9043 longer relevant, but we won't have an opportunity
9044 to update it, since we are done with this screen
9045 line. */
9046 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9047 /* If the character after the one which set the
9048 may_wrap flag is also whitespace, we can't
9049 wrap here, since the screen line cannot be
9050 wrapped in the middle of whitespace.
9051 Therefore, wrap_it _is_ relevant in that
9052 case. */
9053 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9055 /* If we've found TO_X, go back there, as we now
9056 know the last word fits on this screen line. */
9057 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9058 && atx_it.sp >= 0)
9060 RESTORE_IT (it, &atx_it, atx_data);
9061 atpos_it.sp = -1;
9062 atx_it.sp = -1;
9063 result = MOVE_X_REACHED;
9064 break;
9067 else if (wrap_it.sp >= 0)
9069 RESTORE_IT (it, &wrap_it, wrap_data);
9070 atpos_it.sp = -1;
9071 atx_it.sp = -1;
9074 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9075 IT_CHARPOS (*it)));
9076 result = MOVE_LINE_CONTINUED;
9077 break;
9080 if (BUFFER_POS_REACHED_P ())
9082 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9083 goto buffer_pos_reached;
9084 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9086 SAVE_IT (atpos_it, *it, atpos_data);
9087 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9091 if (new_x > it->first_visible_x)
9093 /* If we have reached the visible portion of the
9094 screen line, produce the line number if needed. */
9095 if (line_number_pending)
9097 line_number_pending = false;
9098 it->current_x = it->first_visible_x;
9099 maybe_produce_line_number (it);
9100 it->current_x += new_x - it->first_visible_x;
9102 /* Glyph is visible. Increment number of glyphs that
9103 would be displayed. */
9104 ++it->hpos;
9108 if (result != MOVE_UNDEFINED)
9109 break;
9111 else if (BUFFER_POS_REACHED_P ())
9113 buffer_pos_reached:
9114 IT_RESET_X_ASCENT_DESCENT (it);
9115 result = MOVE_POS_MATCH_OR_ZV;
9116 break;
9118 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9120 /* Stop when TO_X specified and reached. This check is
9121 necessary here because of lines consisting of a line end,
9122 only. The line end will not produce any glyphs and we
9123 would never get MOVE_X_REACHED. */
9124 eassert (it->nglyphs == 0);
9125 result = MOVE_X_REACHED;
9126 break;
9129 /* Is this a line end? If yes, we're done. */
9130 if (ITERATOR_AT_END_OF_LINE_P (it))
9132 /* If we are past TO_CHARPOS, but never saw any character
9133 positions smaller than TO_CHARPOS, return
9134 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9135 did. */
9136 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9138 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9140 if (closest_pos < ZV)
9142 RESTORE_IT (it, &ppos_it, ppos_data);
9143 /* Don't recurse if closest_pos is equal to
9144 to_charpos, since we have just tried that. */
9145 if (closest_pos != to_charpos)
9146 move_it_in_display_line_to (it, closest_pos, -1,
9147 MOVE_TO_POS);
9148 result = MOVE_POS_MATCH_OR_ZV;
9150 else
9151 goto buffer_pos_reached;
9153 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9154 && IT_CHARPOS (*it) > to_charpos)
9155 goto buffer_pos_reached;
9156 else
9157 result = MOVE_NEWLINE_OR_CR;
9159 else
9160 result = MOVE_NEWLINE_OR_CR;
9161 /* If we've processed the newline, make sure this flag is
9162 reset, as it must only be set when the newline itself is
9163 processed. */
9164 if (result == MOVE_NEWLINE_OR_CR)
9165 it->constrain_row_ascent_descent_p = false;
9166 break;
9169 prev_method = it->method;
9170 if (it->method == GET_FROM_BUFFER)
9171 prev_pos = IT_CHARPOS (*it);
9172 /* The current display element has been consumed. Advance
9173 to the next. */
9174 set_iterator_to_next (it, true);
9175 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9176 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9177 if (IT_CHARPOS (*it) < to_charpos)
9178 saw_smaller_pos = true;
9179 if (it->bidi_p
9180 && (op & MOVE_TO_POS)
9181 && IT_CHARPOS (*it) >= to_charpos
9182 && IT_CHARPOS (*it) < closest_pos)
9183 closest_pos = IT_CHARPOS (*it);
9185 /* Stop if lines are truncated and IT's current x-position is
9186 past the right edge of the window now. */
9187 if (it->line_wrap == TRUNCATE
9188 && it->current_x >= it->last_visible_x)
9190 if (!FRAME_WINDOW_P (it->f)
9191 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9192 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9193 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9194 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9196 bool at_eob_p = false;
9198 if ((at_eob_p = !get_next_display_element (it))
9199 || BUFFER_POS_REACHED_P ()
9200 /* If we are past TO_CHARPOS, but never saw any
9201 character positions smaller than TO_CHARPOS,
9202 return MOVE_POS_MATCH_OR_ZV, like the
9203 unidirectional display did. */
9204 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9205 && !saw_smaller_pos
9206 && IT_CHARPOS (*it) > to_charpos))
9208 if (it->bidi_p
9209 && !BUFFER_POS_REACHED_P ()
9210 && !at_eob_p && closest_pos < ZV)
9212 RESTORE_IT (it, &ppos_it, ppos_data);
9213 if (closest_pos != to_charpos)
9214 move_it_in_display_line_to (it, closest_pos, -1,
9215 MOVE_TO_POS);
9217 result = MOVE_POS_MATCH_OR_ZV;
9218 break;
9220 if (ITERATOR_AT_END_OF_LINE_P (it))
9222 result = MOVE_NEWLINE_OR_CR;
9223 break;
9226 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9227 && !saw_smaller_pos
9228 && IT_CHARPOS (*it) > to_charpos)
9230 if (closest_pos < ZV)
9232 RESTORE_IT (it, &ppos_it, ppos_data);
9233 if (closest_pos != to_charpos)
9234 move_it_in_display_line_to (it, closest_pos, -1,
9235 MOVE_TO_POS);
9237 result = MOVE_POS_MATCH_OR_ZV;
9238 break;
9240 result = MOVE_LINE_TRUNCATED;
9241 break;
9243 #undef IT_RESET_X_ASCENT_DESCENT
9246 #undef BUFFER_POS_REACHED_P
9248 /* If we scanned beyond TO_POS, restore the saved iterator either to
9249 the wrap point (if found), or to atpos/atx location. We decide which
9250 data to use to restore the saved iterator state by their X coordinates,
9251 since buffer positions might increase non-monotonically with screen
9252 coordinates due to bidi reordering. */
9253 if (result == MOVE_LINE_CONTINUED
9254 && it->line_wrap == WORD_WRAP
9255 && wrap_it.sp >= 0
9256 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9257 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9258 RESTORE_IT (it, &wrap_it, wrap_data);
9259 else if (atpos_it.sp >= 0)
9260 RESTORE_IT (it, &atpos_it, atpos_data);
9261 else if (atx_it.sp >= 0)
9262 RESTORE_IT (it, &atx_it, atx_data);
9264 done:
9266 if (atpos_data)
9267 bidi_unshelve_cache (atpos_data, true);
9268 if (atx_data)
9269 bidi_unshelve_cache (atx_data, true);
9270 if (wrap_data)
9271 bidi_unshelve_cache (wrap_data, true);
9272 if (ppos_data)
9273 bidi_unshelve_cache (ppos_data, true);
9275 /* Restore the iterator settings altered at the beginning of this
9276 function. */
9277 it->glyph_row = saved_glyph_row;
9278 return result;
9281 /* For external use. */
9282 void
9283 move_it_in_display_line (struct it *it,
9284 ptrdiff_t to_charpos, int to_x,
9285 enum move_operation_enum op)
9287 if (it->line_wrap == WORD_WRAP
9288 && (op & MOVE_TO_X))
9290 struct it save_it;
9291 void *save_data = NULL;
9292 int skip;
9294 SAVE_IT (save_it, *it, save_data);
9295 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9296 /* When word-wrap is on, TO_X may lie past the end
9297 of a wrapped line. Then it->current is the
9298 character on the next line, so backtrack to the
9299 space before the wrap point. */
9300 if (skip == MOVE_LINE_CONTINUED)
9302 int prev_x = max (it->current_x - 1, 0);
9303 RESTORE_IT (it, &save_it, save_data);
9304 move_it_in_display_line_to
9305 (it, -1, prev_x, MOVE_TO_X);
9307 else
9308 bidi_unshelve_cache (save_data, true);
9310 else
9311 move_it_in_display_line_to (it, to_charpos, to_x, op);
9315 /* Move IT forward until it satisfies one or more of the criteria in
9316 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9318 OP is a bit-mask that specifies where to stop, and in particular,
9319 which of those four position arguments makes a difference. See the
9320 description of enum move_operation_enum.
9322 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9323 screen line, this function will set IT to the next position that is
9324 displayed to the right of TO_CHARPOS on the screen.
9326 Return the maximum pixel length of any line scanned but never more
9327 than it.last_visible_x. */
9330 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9332 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9333 int line_height, line_start_x = 0, reached = 0;
9334 int max_current_x = 0;
9335 void *backup_data = NULL;
9337 for (;;)
9339 if (op & MOVE_TO_VPOS)
9341 /* If no TO_CHARPOS and no TO_X specified, stop at the
9342 start of the line TO_VPOS. */
9343 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9345 if (it->vpos == to_vpos)
9347 reached = 1;
9348 break;
9350 else
9351 skip = move_it_in_display_line_to (it, -1, -1, 0);
9353 else
9355 /* TO_VPOS >= 0 means stop at TO_X in the line at
9356 TO_VPOS, or at TO_POS, whichever comes first. */
9357 if (it->vpos == to_vpos)
9359 reached = 2;
9360 break;
9363 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9365 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9367 reached = 3;
9368 break;
9370 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9372 /* We have reached TO_X but not in the line we want. */
9373 skip = move_it_in_display_line_to (it, to_charpos,
9374 -1, MOVE_TO_POS);
9375 if (skip == MOVE_POS_MATCH_OR_ZV)
9377 reached = 4;
9378 break;
9383 else if (op & MOVE_TO_Y)
9385 struct it it_backup;
9387 if (it->line_wrap == WORD_WRAP)
9388 SAVE_IT (it_backup, *it, backup_data);
9390 /* TO_Y specified means stop at TO_X in the line containing
9391 TO_Y---or at TO_CHARPOS if this is reached first. The
9392 problem is that we can't really tell whether the line
9393 contains TO_Y before we have completely scanned it, and
9394 this may skip past TO_X. What we do is to first scan to
9395 TO_X.
9397 If TO_X is not specified, use a TO_X of zero. The reason
9398 is to make the outcome of this function more predictable.
9399 If we didn't use TO_X == 0, we would stop at the end of
9400 the line which is probably not what a caller would expect
9401 to happen. */
9402 skip = move_it_in_display_line_to
9403 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9404 (MOVE_TO_X | (op & MOVE_TO_POS)));
9406 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9407 if (skip == MOVE_POS_MATCH_OR_ZV)
9408 reached = 5;
9409 else if (skip == MOVE_X_REACHED)
9411 /* If TO_X was reached, we want to know whether TO_Y is
9412 in the line. We know this is the case if the already
9413 scanned glyphs make the line tall enough. Otherwise,
9414 we must check by scanning the rest of the line. */
9415 line_height = it->max_ascent + it->max_descent;
9416 if (to_y >= it->current_y
9417 && to_y < it->current_y + line_height)
9419 reached = 6;
9420 break;
9422 SAVE_IT (it_backup, *it, backup_data);
9423 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9424 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9425 op & MOVE_TO_POS);
9426 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9427 line_height = it->max_ascent + it->max_descent;
9428 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9430 if (to_y >= it->current_y
9431 && to_y < it->current_y + line_height)
9433 /* If TO_Y is in this line and TO_X was reached
9434 above, we scanned too far. We have to restore
9435 IT's settings to the ones before skipping. But
9436 keep the more accurate values of max_ascent and
9437 max_descent we've found while skipping the rest
9438 of the line, for the sake of callers, such as
9439 pos_visible_p, that need to know the line
9440 height. */
9441 int max_ascent = it->max_ascent;
9442 int max_descent = it->max_descent;
9444 RESTORE_IT (it, &it_backup, backup_data);
9445 it->max_ascent = max_ascent;
9446 it->max_descent = max_descent;
9447 reached = 6;
9449 else
9451 skip = skip2;
9452 if (skip == MOVE_POS_MATCH_OR_ZV)
9453 reached = 7;
9456 else
9458 /* Check whether TO_Y is in this line. */
9459 line_height = it->max_ascent + it->max_descent;
9460 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9462 if (to_y >= it->current_y
9463 && to_y < it->current_y + line_height)
9465 if (to_y > it->current_y)
9466 max_current_x = max (it->current_x, max_current_x);
9468 /* When word-wrap is on, TO_X may lie past the end
9469 of a wrapped line. Then it->current is the
9470 character on the next line, so backtrack to the
9471 space before the wrap point. */
9472 if (skip == MOVE_LINE_CONTINUED
9473 && it->line_wrap == WORD_WRAP)
9475 int prev_x = max (it->current_x - 1, 0);
9476 RESTORE_IT (it, &it_backup, backup_data);
9477 skip = move_it_in_display_line_to
9478 (it, -1, prev_x, MOVE_TO_X);
9481 reached = 6;
9485 if (reached)
9487 max_current_x = max (it->current_x, max_current_x);
9488 break;
9491 else if (BUFFERP (it->object)
9492 && (it->method == GET_FROM_BUFFER
9493 || it->method == GET_FROM_STRETCH)
9494 && IT_CHARPOS (*it) >= to_charpos
9495 /* Under bidi iteration, a call to set_iterator_to_next
9496 can scan far beyond to_charpos if the initial
9497 portion of the next line needs to be reordered. In
9498 that case, give move_it_in_display_line_to another
9499 chance below. */
9500 && !(it->bidi_p
9501 && it->bidi_it.scan_dir == -1))
9502 skip = MOVE_POS_MATCH_OR_ZV;
9503 else
9504 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9506 switch (skip)
9508 case MOVE_POS_MATCH_OR_ZV:
9509 max_current_x = max (it->current_x, max_current_x);
9510 reached = 8;
9511 goto out;
9513 case MOVE_NEWLINE_OR_CR:
9514 max_current_x = max (it->current_x, max_current_x);
9515 set_iterator_to_next (it, true);
9516 it->continuation_lines_width = 0;
9517 break;
9519 case MOVE_LINE_TRUNCATED:
9520 max_current_x = it->last_visible_x;
9521 it->continuation_lines_width = 0;
9522 reseat_at_next_visible_line_start (it, false);
9523 if ((op & MOVE_TO_POS) != 0
9524 && IT_CHARPOS (*it) > to_charpos)
9526 reached = 9;
9527 goto out;
9529 break;
9531 case MOVE_LINE_CONTINUED:
9532 max_current_x = it->last_visible_x;
9533 /* For continued lines ending in a tab, some of the glyphs
9534 associated with the tab are displayed on the current
9535 line. Since it->current_x does not include these glyphs,
9536 we use it->last_visible_x instead. */
9537 if (it->c == '\t')
9539 it->continuation_lines_width += it->last_visible_x;
9540 /* When moving by vpos, ensure that the iterator really
9541 advances to the next line (bug#847, bug#969). Fixme:
9542 do we need to do this in other circumstances? */
9543 if (it->current_x != it->last_visible_x
9544 && (op & MOVE_TO_VPOS)
9545 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9547 line_start_x = it->current_x + it->pixel_width
9548 - it->last_visible_x;
9549 if (FRAME_WINDOW_P (it->f))
9551 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9552 struct font *face_font = face->font;
9554 /* When display_line produces a continued line
9555 that ends in a TAB, it skips a tab stop that
9556 is closer than the font's space character
9557 width (see x_produce_glyphs where it produces
9558 the stretch glyph which represents a TAB).
9559 We need to reproduce the same logic here. */
9560 eassert (face_font);
9561 if (face_font)
9563 if (line_start_x < face_font->space_width)
9564 line_start_x
9565 += it->tab_width * face_font->space_width;
9568 set_iterator_to_next (it, false);
9571 else
9572 it->continuation_lines_width += it->current_x;
9573 break;
9575 default:
9576 emacs_abort ();
9579 /* Reset/increment for the next run. */
9580 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9581 it->current_x = line_start_x;
9582 line_start_x = 0;
9583 it->hpos = 0;
9584 it->current_y += it->max_ascent + it->max_descent;
9585 ++it->vpos;
9586 last_height = it->max_ascent + it->max_descent;
9587 it->max_ascent = it->max_descent = 0;
9590 out:
9592 /* On text terminals, we may stop at the end of a line in the middle
9593 of a multi-character glyph. If the glyph itself is continued,
9594 i.e. it is actually displayed on the next line, don't treat this
9595 stopping point as valid; move to the next line instead (unless
9596 that brings us offscreen). */
9597 if (!FRAME_WINDOW_P (it->f)
9598 && op & MOVE_TO_POS
9599 && IT_CHARPOS (*it) == to_charpos
9600 && it->what == IT_CHARACTER
9601 && it->nglyphs > 1
9602 && it->line_wrap == WINDOW_WRAP
9603 && it->current_x == it->last_visible_x - 1
9604 && it->c != '\n'
9605 && it->c != '\t'
9606 && it->w->window_end_valid
9607 && it->vpos < it->w->window_end_vpos)
9609 it->continuation_lines_width += it->current_x;
9610 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9611 it->current_y += it->max_ascent + it->max_descent;
9612 ++it->vpos;
9613 last_height = it->max_ascent + it->max_descent;
9616 if (backup_data)
9617 bidi_unshelve_cache (backup_data, true);
9619 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9621 return max_current_x;
9625 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9627 If DY > 0, move IT backward at least that many pixels. DY = 0
9628 means move IT backward to the preceding line start or BEGV. This
9629 function may move over more than DY pixels if IT->current_y - DY
9630 ends up in the middle of a line; in this case IT->current_y will be
9631 set to the top of the line moved to. */
9633 void
9634 move_it_vertically_backward (struct it *it, int dy)
9636 int nlines, h;
9637 struct it it2, it3;
9638 void *it2data = NULL, *it3data = NULL;
9639 ptrdiff_t start_pos;
9640 int nchars_per_row
9641 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9642 ptrdiff_t pos_limit;
9644 move_further_back:
9645 eassert (dy >= 0);
9647 start_pos = IT_CHARPOS (*it);
9649 /* Estimate how many newlines we must move back. */
9650 nlines = max (1, dy / default_line_pixel_height (it->w));
9651 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9652 pos_limit = BEGV;
9653 else
9654 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9656 /* Set the iterator's position that many lines back. But don't go
9657 back more than NLINES full screen lines -- this wins a day with
9658 buffers which have very long lines. */
9659 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9660 back_to_previous_visible_line_start (it);
9662 /* Reseat the iterator here. When moving backward, we don't want
9663 reseat to skip forward over invisible text, set up the iterator
9664 to deliver from overlay strings at the new position etc. So,
9665 use reseat_1 here. */
9666 reseat_1 (it, it->current.pos, true);
9668 /* We are now surely at a line start. */
9669 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9670 reordering is in effect. */
9671 it->continuation_lines_width = 0;
9673 /* Move forward and see what y-distance we moved. First move to the
9674 start of the next line so that we get its height. We need this
9675 height to be able to tell whether we reached the specified
9676 y-distance. */
9677 SAVE_IT (it2, *it, it2data);
9678 it2.max_ascent = it2.max_descent = 0;
9681 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9682 MOVE_TO_POS | MOVE_TO_VPOS);
9684 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9685 /* If we are in a display string which starts at START_POS,
9686 and that display string includes a newline, and we are
9687 right after that newline (i.e. at the beginning of a
9688 display line), exit the loop, because otherwise we will
9689 infloop, since move_it_to will see that it is already at
9690 START_POS and will not move. */
9691 || (it2.method == GET_FROM_STRING
9692 && IT_CHARPOS (it2) == start_pos
9693 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9694 eassert (IT_CHARPOS (*it) >= BEGV);
9695 SAVE_IT (it3, it2, it3data);
9697 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9698 eassert (IT_CHARPOS (*it) >= BEGV);
9699 /* H is the actual vertical distance from the position in *IT
9700 and the starting position. */
9701 h = it2.current_y - it->current_y;
9702 /* NLINES is the distance in number of lines. */
9703 nlines = it2.vpos - it->vpos;
9705 /* Correct IT's y and vpos position
9706 so that they are relative to the starting point. */
9707 it->vpos -= nlines;
9708 it->current_y -= h;
9710 if (dy == 0)
9712 /* DY == 0 means move to the start of the screen line. The
9713 value of nlines is > 0 if continuation lines were involved,
9714 or if the original IT position was at start of a line. */
9715 RESTORE_IT (it, it, it2data);
9716 if (nlines > 0)
9717 move_it_by_lines (it, nlines);
9718 /* The above code moves us to some position NLINES down,
9719 usually to its first glyph (leftmost in an L2R line), but
9720 that's not necessarily the start of the line, under bidi
9721 reordering. We want to get to the character position
9722 that is immediately after the newline of the previous
9723 line. */
9724 if (it->bidi_p
9725 && !it->continuation_lines_width
9726 && !STRINGP (it->string)
9727 && IT_CHARPOS (*it) > BEGV
9728 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9730 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9732 DEC_BOTH (cp, bp);
9733 cp = find_newline_no_quit (cp, bp, -1, NULL);
9734 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9736 bidi_unshelve_cache (it3data, true);
9738 else
9740 /* The y-position we try to reach, relative to *IT.
9741 Note that H has been subtracted in front of the if-statement. */
9742 int target_y = it->current_y + h - dy;
9743 int y0 = it3.current_y;
9744 int y1;
9745 int line_height;
9747 RESTORE_IT (&it3, &it3, it3data);
9748 y1 = line_bottom_y (&it3);
9749 line_height = y1 - y0;
9750 RESTORE_IT (it, it, it2data);
9751 /* If we did not reach target_y, try to move further backward if
9752 we can. If we moved too far backward, try to move forward. */
9753 if (target_y < it->current_y
9754 /* This is heuristic. In a window that's 3 lines high, with
9755 a line height of 13 pixels each, recentering with point
9756 on the bottom line will try to move -39/2 = 19 pixels
9757 backward. Try to avoid moving into the first line. */
9758 && (it->current_y - target_y
9759 > min (window_box_height (it->w), line_height * 2 / 3))
9760 && IT_CHARPOS (*it) > BEGV)
9762 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9763 target_y - it->current_y));
9764 dy = it->current_y - target_y;
9765 goto move_further_back;
9767 else if (target_y >= it->current_y + line_height
9768 && IT_CHARPOS (*it) < ZV)
9770 /* Should move forward by at least one line, maybe more.
9772 Note: Calling move_it_by_lines can be expensive on
9773 terminal frames, where compute_motion is used (via
9774 vmotion) to do the job, when there are very long lines
9775 and truncate-lines is nil. That's the reason for
9776 treating terminal frames specially here. */
9778 if (!FRAME_WINDOW_P (it->f))
9779 move_it_vertically (it, target_y - it->current_y);
9780 else
9784 move_it_by_lines (it, 1);
9786 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9793 /* Move IT by a specified amount of pixel lines DY. DY negative means
9794 move backwards. DY = 0 means move to start of screen line. At the
9795 end, IT will be on the start of a screen line. */
9797 void
9798 move_it_vertically (struct it *it, int dy)
9800 if (dy <= 0)
9801 move_it_vertically_backward (it, -dy);
9802 else
9804 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9805 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9806 MOVE_TO_POS | MOVE_TO_Y);
9807 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9809 /* If buffer ends in ZV without a newline, move to the start of
9810 the line to satisfy the post-condition. */
9811 if (IT_CHARPOS (*it) == ZV
9812 && ZV > BEGV
9813 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9814 move_it_by_lines (it, 0);
9819 /* Move iterator IT past the end of the text line it is in. */
9821 void
9822 move_it_past_eol (struct it *it)
9824 enum move_it_result rc;
9826 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9827 if (rc == MOVE_NEWLINE_OR_CR)
9828 set_iterator_to_next (it, false);
9832 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9833 negative means move up. DVPOS == 0 means move to the start of the
9834 screen line.
9836 Optimization idea: If we would know that IT->f doesn't use
9837 a face with proportional font, we could be faster for
9838 truncate-lines nil. */
9840 void
9841 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9844 /* The commented-out optimization uses vmotion on terminals. This
9845 gives bad results, because elements like it->what, on which
9846 callers such as pos_visible_p rely, aren't updated. */
9847 /* struct position pos;
9848 if (!FRAME_WINDOW_P (it->f))
9850 struct text_pos textpos;
9852 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9853 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9854 reseat (it, textpos, true);
9855 it->vpos += pos.vpos;
9856 it->current_y += pos.vpos;
9858 else */
9860 if (dvpos == 0)
9862 /* DVPOS == 0 means move to the start of the screen line. */
9863 move_it_vertically_backward (it, 0);
9864 /* Let next call to line_bottom_y calculate real line height. */
9865 last_height = 0;
9867 else if (dvpos > 0)
9869 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9870 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9872 /* Only move to the next buffer position if we ended up in a
9873 string from display property, not in an overlay string
9874 (before-string or after-string). That is because the
9875 latter don't conceal the underlying buffer position, so
9876 we can ask to move the iterator to the exact position we
9877 are interested in. Note that, even if we are already at
9878 IT_CHARPOS (*it), the call below is not a no-op, as it
9879 will detect that we are at the end of the string, pop the
9880 iterator, and compute it->current_x and it->hpos
9881 correctly. */
9882 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9883 -1, -1, -1, MOVE_TO_POS);
9886 else
9888 struct it it2;
9889 void *it2data = NULL;
9890 ptrdiff_t start_charpos, i;
9891 int nchars_per_row
9892 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9893 bool hit_pos_limit = false;
9894 ptrdiff_t pos_limit;
9896 /* Start at the beginning of the screen line containing IT's
9897 position. This may actually move vertically backwards,
9898 in case of overlays, so adjust dvpos accordingly. */
9899 dvpos += it->vpos;
9900 move_it_vertically_backward (it, 0);
9901 dvpos -= it->vpos;
9903 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9904 screen lines, and reseat the iterator there. */
9905 start_charpos = IT_CHARPOS (*it);
9906 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9907 pos_limit = BEGV;
9908 else
9909 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9911 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9912 back_to_previous_visible_line_start (it);
9913 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9914 hit_pos_limit = true;
9915 reseat (it, it->current.pos, true);
9917 /* Move further back if we end up in a string or an image. */
9918 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9920 /* First try to move to start of display line. */
9921 dvpos += it->vpos;
9922 move_it_vertically_backward (it, 0);
9923 dvpos -= it->vpos;
9924 if (IT_POS_VALID_AFTER_MOVE_P (it))
9925 break;
9926 /* If start of line is still in string or image,
9927 move further back. */
9928 back_to_previous_visible_line_start (it);
9929 reseat (it, it->current.pos, true);
9930 dvpos--;
9933 it->current_x = it->hpos = 0;
9935 /* Above call may have moved too far if continuation lines
9936 are involved. Scan forward and see if it did. */
9937 SAVE_IT (it2, *it, it2data);
9938 it2.vpos = it2.current_y = 0;
9939 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9940 it->vpos -= it2.vpos;
9941 it->current_y -= it2.current_y;
9942 it->current_x = it->hpos = 0;
9944 /* If we moved too far back, move IT some lines forward. */
9945 if (it2.vpos > -dvpos)
9947 int delta = it2.vpos + dvpos;
9949 RESTORE_IT (&it2, &it2, it2data);
9950 SAVE_IT (it2, *it, it2data);
9951 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9952 /* Move back again if we got too far ahead. */
9953 if (IT_CHARPOS (*it) >= start_charpos)
9954 RESTORE_IT (it, &it2, it2data);
9955 else
9956 bidi_unshelve_cache (it2data, true);
9958 else if (hit_pos_limit && pos_limit > BEGV
9959 && dvpos < 0 && it2.vpos < -dvpos)
9961 /* If we hit the limit, but still didn't make it far enough
9962 back, that means there's a display string with a newline
9963 covering a large chunk of text, and that caused
9964 back_to_previous_visible_line_start try to go too far.
9965 Punish those who commit such atrocities by going back
9966 until we've reached DVPOS, after lifting the limit, which
9967 could make it slow for very long lines. "If it hurts,
9968 don't do that!" */
9969 dvpos += it2.vpos;
9970 RESTORE_IT (it, it, it2data);
9971 for (i = -dvpos; i > 0; --i)
9973 back_to_previous_visible_line_start (it);
9974 it->vpos--;
9976 reseat_1 (it, it->current.pos, true);
9978 else
9979 RESTORE_IT (it, it, it2data);
9984 partial_line_height (struct it *it_origin)
9986 int partial_height;
9987 void *it_data = NULL;
9988 struct it it;
9989 SAVE_IT (it, *it_origin, it_data);
9990 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9991 MOVE_TO_POS | MOVE_TO_Y);
9992 if (it.what == IT_EOB)
9994 int vis_height = it.last_visible_y - it.current_y;
9995 int height = it.ascent + it.descent;
9996 partial_height = (vis_height < height) ? vis_height : 0;
9998 else
10000 int last_line_y = it.current_y;
10001 move_it_by_lines (&it, 1);
10002 partial_height = (it.current_y > it.last_visible_y)
10003 ? it.last_visible_y - last_line_y : 0;
10005 RESTORE_IT (&it, &it, it_data);
10006 return partial_height;
10009 /* Return true if IT points into the middle of a display vector. */
10011 bool
10012 in_display_vector_p (struct it *it)
10014 return (it->method == GET_FROM_DISPLAY_VECTOR
10015 && it->current.dpvec_index > 0
10016 && it->dpvec + it->current.dpvec_index != it->dpend);
10019 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
10020 doc: /* Return the size of the text of WINDOW's buffer in pixels.
10021 WINDOW must be a live window and defaults to the selected one. The
10022 return value is a cons of the maximum pixel-width of any text line and
10023 the maximum pixel-height of all text lines.
10025 The optional argument FROM, if non-nil, specifies the first text
10026 position and defaults to the minimum accessible position of the buffer.
10027 If FROM is t, use the minimum accessible position that starts a
10028 non-empty line. TO, if non-nil, specifies the last text position and
10029 defaults to the maximum accessible position of the buffer. If TO is t,
10030 use the maximum accessible position that ends a non-empty line.
10032 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10033 width that can be returned. X-LIMIT nil or omitted, means to use the
10034 pixel-width of WINDOW's body; use this if you want to know how high
10035 WINDOW should be become in order to fit all of its buffer's text with
10036 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10037 if you intend to change WINDOW's width. In any case, text whose
10038 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10039 of long lines can take some time, it's always a good idea to make this
10040 argument as small as possible; in particular, if the buffer contains
10041 long lines that shall be truncated anyway.
10043 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10044 height (excluding the height of the mode- or header-line, if any) that
10045 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10046 ignored. Since calculating the text height of a large buffer can take
10047 some time, it makes sense to specify this argument if the size of the
10048 buffer is large or unknown.
10050 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10051 include the height of the mode- or header-line of WINDOW in the return
10052 value. If it is either the symbol `mode-line' or `header-line', include
10053 only the height of that line, if present, in the return value. If t,
10054 include the height of both, if present, in the return value. */)
10055 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10056 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10058 struct window *w = decode_live_window (window);
10059 Lisp_Object buffer = w->contents;
10060 struct buffer *b;
10061 struct it it;
10062 struct buffer *old_b = NULL;
10063 ptrdiff_t start, end, pos;
10064 struct text_pos startp;
10065 void *itdata = NULL;
10066 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10068 CHECK_BUFFER (buffer);
10069 b = XBUFFER (buffer);
10071 if (b != current_buffer)
10073 old_b = current_buffer;
10074 set_buffer_internal (b);
10077 if (NILP (from))
10078 start = BEGV;
10079 else if (EQ (from, Qt))
10081 start = pos = BEGV;
10082 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10083 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10084 start = pos;
10085 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10086 start = pos;
10088 else
10090 CHECK_NUMBER_COERCE_MARKER (from);
10091 start = min (max (XINT (from), BEGV), ZV);
10094 if (NILP (to))
10095 end = ZV;
10096 else if (EQ (to, Qt))
10098 end = pos = ZV;
10099 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10100 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10101 end = pos;
10102 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10103 end = pos;
10105 else
10107 CHECK_NUMBER_COERCE_MARKER (to);
10108 end = max (start, min (XINT (to), ZV));
10111 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10112 max_x = XINT (x_limit);
10114 if (NILP (y_limit))
10115 max_y = INT_MAX;
10116 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10117 max_y = XINT (y_limit);
10119 itdata = bidi_shelve_cache ();
10120 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10121 start_display (&it, w, startp);
10123 if (NILP (x_limit))
10124 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10125 else
10127 it.last_visible_x = max_x;
10128 /* Actually, we never want move_it_to stop at to_x. But to make
10129 sure that move_it_in_display_line_to always moves far enough,
10130 we set it to INT_MAX and specify MOVE_TO_X. */
10131 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10132 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10133 /* Don't return more than X-LIMIT. */
10134 if (x > max_x)
10135 x = max_x;
10138 /* Subtract height of header-line which was counted automatically by
10139 start_display. */
10140 y = it.current_y + it.max_ascent + it.max_descent
10141 - WINDOW_HEADER_LINE_HEIGHT (w);
10142 /* Don't return more than Y-LIMIT. */
10143 if (y > max_y)
10144 y = max_y;
10146 if (EQ (mode_and_header_line, Qheader_line)
10147 || EQ (mode_and_header_line, Qt))
10148 /* Re-add height of header-line as requested. */
10149 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10151 if (EQ (mode_and_header_line, Qmode_line)
10152 || EQ (mode_and_header_line, Qt))
10153 /* Add height of mode-line as requested. */
10154 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10156 bidi_unshelve_cache (itdata, false);
10158 if (old_b)
10159 set_buffer_internal (old_b);
10161 return Fcons (make_number (x), make_number (y));
10164 /***********************************************************************
10165 Messages
10166 ***********************************************************************/
10168 /* Return the number of arguments the format string FORMAT needs. */
10170 static ptrdiff_t
10171 format_nargs (char const *format)
10173 ptrdiff_t nargs = 0;
10174 for (char const *p = format; (p = strchr (p, '%')); p++)
10175 if (p[1] == '%')
10176 p++;
10177 else
10178 nargs++;
10179 return nargs;
10182 /* Add a message with format string FORMAT and formatted arguments
10183 to *Messages*. */
10185 void
10186 add_to_log (const char *format, ...)
10188 va_list ap;
10189 va_start (ap, format);
10190 vadd_to_log (format, ap);
10191 va_end (ap);
10194 void
10195 vadd_to_log (char const *format, va_list ap)
10197 ptrdiff_t form_nargs = format_nargs (format);
10198 ptrdiff_t nargs = 1 + form_nargs;
10199 Lisp_Object args[10];
10200 eassert (nargs <= ARRAYELTS (args));
10201 AUTO_STRING (args0, format);
10202 args[0] = args0;
10203 for (ptrdiff_t i = 1; i <= nargs; i++)
10204 args[i] = va_arg (ap, Lisp_Object);
10205 Lisp_Object msg = Qnil;
10206 msg = Fformat_message (nargs, args);
10208 ptrdiff_t len = SBYTES (msg) + 1;
10209 USE_SAFE_ALLOCA;
10210 char *buffer = SAFE_ALLOCA (len);
10211 memcpy (buffer, SDATA (msg), len);
10213 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10214 SAFE_FREE ();
10218 /* Output a newline in the *Messages* buffer if "needs" one. */
10220 void
10221 message_log_maybe_newline (void)
10223 if (message_log_need_newline)
10224 message_dolog ("", 0, true, false);
10228 /* Add a string M of length NBYTES to the message log, optionally
10229 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10230 true, means interpret the contents of M as multibyte. This
10231 function calls low-level routines in order to bypass text property
10232 hooks, etc. which might not be safe to run.
10234 This may GC (insert may run before/after change hooks),
10235 so the buffer M must NOT point to a Lisp string. */
10237 void
10238 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10240 const unsigned char *msg = (const unsigned char *) m;
10242 if (!NILP (Vmemory_full))
10243 return;
10245 if (!NILP (Vmessage_log_max))
10247 struct buffer *oldbuf;
10248 Lisp_Object oldpoint, oldbegv, oldzv;
10249 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10250 ptrdiff_t point_at_end = 0;
10251 ptrdiff_t zv_at_end = 0;
10252 Lisp_Object old_deactivate_mark;
10254 old_deactivate_mark = Vdeactivate_mark;
10255 oldbuf = current_buffer;
10257 /* Ensure the Messages buffer exists, and switch to it.
10258 If we created it, set the major-mode. */
10259 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10260 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10261 if (newbuffer
10262 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10263 call0 (intern ("messages-buffer-mode"));
10265 bset_undo_list (current_buffer, Qt);
10266 bset_cache_long_scans (current_buffer, Qnil);
10268 oldpoint = message_dolog_marker1;
10269 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10270 oldbegv = message_dolog_marker2;
10271 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10272 oldzv = message_dolog_marker3;
10273 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10275 if (PT == Z)
10276 point_at_end = 1;
10277 if (ZV == Z)
10278 zv_at_end = 1;
10280 BEGV = BEG;
10281 BEGV_BYTE = BEG_BYTE;
10282 ZV = Z;
10283 ZV_BYTE = Z_BYTE;
10284 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10286 /* Insert the string--maybe converting multibyte to single byte
10287 or vice versa, so that all the text fits the buffer. */
10288 if (multibyte
10289 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10291 ptrdiff_t i;
10292 int c, char_bytes;
10293 char work[1];
10295 /* Convert a multibyte string to single-byte
10296 for the *Message* buffer. */
10297 for (i = 0; i < nbytes; i += char_bytes)
10299 c = string_char_and_length (msg + i, &char_bytes);
10300 work[0] = CHAR_TO_BYTE8 (c);
10301 insert_1_both (work, 1, 1, true, false, false);
10304 else if (! multibyte
10305 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10307 ptrdiff_t i;
10308 int c, char_bytes;
10309 unsigned char str[MAX_MULTIBYTE_LENGTH];
10310 /* Convert a single-byte string to multibyte
10311 for the *Message* buffer. */
10312 for (i = 0; i < nbytes; i++)
10314 c = msg[i];
10315 MAKE_CHAR_MULTIBYTE (c);
10316 char_bytes = CHAR_STRING (c, str);
10317 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10320 else if (nbytes)
10321 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10322 true, false, false);
10324 if (nlflag)
10326 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10327 printmax_t dups;
10329 insert_1_both ("\n", 1, 1, true, false, false);
10331 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10332 this_bol = PT;
10333 this_bol_byte = PT_BYTE;
10335 /* See if this line duplicates the previous one.
10336 If so, combine duplicates. */
10337 if (this_bol > BEG)
10339 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10340 prev_bol = PT;
10341 prev_bol_byte = PT_BYTE;
10343 dups = message_log_check_duplicate (prev_bol_byte,
10344 this_bol_byte);
10345 if (dups)
10347 del_range_both (prev_bol, prev_bol_byte,
10348 this_bol, this_bol_byte, false);
10349 if (dups > 1)
10351 char dupstr[sizeof " [ times]"
10352 + INT_STRLEN_BOUND (printmax_t)];
10354 /* If you change this format, don't forget to also
10355 change message_log_check_duplicate. */
10356 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10357 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10358 insert_1_both (dupstr, duplen, duplen,
10359 true, false, true);
10364 /* If we have more than the desired maximum number of lines
10365 in the *Messages* buffer now, delete the oldest ones.
10366 This is safe because we don't have undo in this buffer. */
10368 if (NATNUMP (Vmessage_log_max))
10370 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10371 -XFASTINT (Vmessage_log_max) - 1, false);
10372 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10375 BEGV = marker_position (oldbegv);
10376 BEGV_BYTE = marker_byte_position (oldbegv);
10378 if (zv_at_end)
10380 ZV = Z;
10381 ZV_BYTE = Z_BYTE;
10383 else
10385 ZV = marker_position (oldzv);
10386 ZV_BYTE = marker_byte_position (oldzv);
10389 if (point_at_end)
10390 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10391 else
10392 /* We can't do Fgoto_char (oldpoint) because it will run some
10393 Lisp code. */
10394 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10395 marker_byte_position (oldpoint));
10397 unchain_marker (XMARKER (oldpoint));
10398 unchain_marker (XMARKER (oldbegv));
10399 unchain_marker (XMARKER (oldzv));
10401 /* We called insert_1_both above with its 5th argument (PREPARE)
10402 false, which prevents insert_1_both from calling
10403 prepare_to_modify_buffer, which in turns prevents us from
10404 incrementing windows_or_buffers_changed even if *Messages* is
10405 shown in some window. So we must manually set
10406 windows_or_buffers_changed here to make up for that. */
10407 windows_or_buffers_changed = old_windows_or_buffers_changed;
10408 bset_redisplay (current_buffer);
10410 set_buffer_internal (oldbuf);
10412 message_log_need_newline = !nlflag;
10413 Vdeactivate_mark = old_deactivate_mark;
10418 /* We are at the end of the buffer after just having inserted a newline.
10419 (Note: We depend on the fact we won't be crossing the gap.)
10420 Check to see if the most recent message looks a lot like the previous one.
10421 Return 0 if different, 1 if the new one should just replace it, or a
10422 value N > 1 if we should also append " [N times]". */
10424 static intmax_t
10425 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10427 ptrdiff_t i;
10428 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10429 bool seen_dots = false;
10430 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10431 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10433 for (i = 0; i < len; i++)
10435 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10436 seen_dots = true;
10437 if (p1[i] != p2[i])
10438 return seen_dots;
10440 p1 += len;
10441 if (*p1 == '\n')
10442 return 2;
10443 if (*p1++ == ' ' && *p1++ == '[')
10445 char *pend;
10446 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10447 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10448 return n + 1;
10450 return 0;
10454 /* Display an echo area message M with a specified length of NBYTES
10455 bytes. The string may include null characters. If M is not a
10456 string, clear out any existing message, and let the mini-buffer
10457 text show through.
10459 This function cancels echoing. */
10461 void
10462 message3 (Lisp_Object m)
10464 clear_message (true, true);
10465 cancel_echoing ();
10467 /* First flush out any partial line written with print. */
10468 message_log_maybe_newline ();
10469 if (STRINGP (m))
10471 ptrdiff_t nbytes = SBYTES (m);
10472 bool multibyte = STRING_MULTIBYTE (m);
10473 char *buffer;
10474 USE_SAFE_ALLOCA;
10475 SAFE_ALLOCA_STRING (buffer, m);
10476 message_dolog (buffer, nbytes, true, multibyte);
10477 SAFE_FREE ();
10479 if (! inhibit_message)
10480 message3_nolog (m);
10483 /* Log the message M to stderr. Log an empty line if M is not a string. */
10485 static void
10486 message_to_stderr (Lisp_Object m)
10488 if (noninteractive_need_newline)
10490 noninteractive_need_newline = false;
10491 fputc ('\n', stderr);
10493 if (STRINGP (m))
10495 Lisp_Object coding_system = Vlocale_coding_system;
10496 Lisp_Object s;
10498 if (!NILP (Vcoding_system_for_write))
10499 coding_system = Vcoding_system_for_write;
10500 if (!NILP (coding_system))
10501 s = code_convert_string_norecord (m, coding_system, true);
10502 else
10503 s = m;
10505 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10507 if (!cursor_in_echo_area)
10508 fputc ('\n', stderr);
10509 fflush (stderr);
10512 /* The non-logging version of message3.
10513 This does not cancel echoing, because it is used for echoing.
10514 Perhaps we need to make a separate function for echoing
10515 and make this cancel echoing. */
10517 void
10518 message3_nolog (Lisp_Object m)
10520 struct frame *sf = SELECTED_FRAME ();
10522 if (FRAME_INITIAL_P (sf))
10523 message_to_stderr (m);
10524 /* Error messages get reported properly by cmd_error, so this must be just an
10525 informative message; if the frame hasn't really been initialized yet, just
10526 toss it. */
10527 else if (INTERACTIVE && sf->glyphs_initialized_p)
10529 /* Get the frame containing the mini-buffer
10530 that the selected frame is using. */
10531 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10532 Lisp_Object frame = XWINDOW (mini_window)->frame;
10533 struct frame *f = XFRAME (frame);
10535 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10536 Fmake_frame_visible (frame);
10538 if (STRINGP (m) && SCHARS (m) > 0)
10540 set_message (m);
10541 if (minibuffer_auto_raise)
10542 Fraise_frame (frame);
10543 /* Assume we are not echoing.
10544 (If we are, echo_now will override this.) */
10545 echo_message_buffer = Qnil;
10547 else
10548 clear_message (true, true);
10550 do_pending_window_change (false);
10551 echo_area_display (true);
10552 do_pending_window_change (false);
10553 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10554 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10559 /* Display a null-terminated echo area message M. If M is 0, clear
10560 out any existing message, and let the mini-buffer text show through.
10562 The buffer M must continue to exist until after the echo area gets
10563 cleared or some other message gets displayed there. Do not pass
10564 text that is stored in a Lisp string. Do not pass text in a buffer
10565 that was alloca'd. */
10567 void
10568 message1 (const char *m)
10570 message3 (m ? build_unibyte_string (m) : Qnil);
10574 /* The non-logging counterpart of message1. */
10576 void
10577 message1_nolog (const char *m)
10579 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10582 /* Display a message M which contains a single %s
10583 which gets replaced with STRING. */
10585 void
10586 message_with_string (const char *m, Lisp_Object string, bool log)
10588 CHECK_STRING (string);
10590 bool need_message;
10591 if (noninteractive)
10592 need_message = !!m;
10593 else if (!INTERACTIVE)
10594 need_message = false;
10595 else
10597 /* The frame whose minibuffer we're going to display the message on.
10598 It may be larger than the selected frame, so we need
10599 to use its buffer, not the selected frame's buffer. */
10600 Lisp_Object mini_window;
10601 struct frame *f, *sf = SELECTED_FRAME ();
10603 /* Get the frame containing the minibuffer
10604 that the selected frame is using. */
10605 mini_window = FRAME_MINIBUF_WINDOW (sf);
10606 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10608 /* Error messages get reported properly by cmd_error, so this must be
10609 just an informative message; if the frame hasn't really been
10610 initialized yet, just toss it. */
10611 need_message = f->glyphs_initialized_p;
10614 if (need_message)
10616 AUTO_STRING (fmt, m);
10617 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10619 if (noninteractive)
10620 message_to_stderr (msg);
10621 else
10623 if (log)
10624 message3 (msg);
10625 else
10626 message3_nolog (msg);
10628 /* Print should start at the beginning of the message
10629 buffer next time. */
10630 message_buf_print = false;
10636 /* Dump an informative message to the minibuf. If M is 0, clear out
10637 any existing message, and let the mini-buffer text show through.
10639 The message must be safe ASCII (because when Emacs is
10640 non-interactive the message is sent straight to stderr without
10641 encoding first) and the format must not contain ` or ' (because
10642 this function does not account for `text-quoting-style'). If your
10643 message and format do not fit into this category, convert your
10644 arguments to Lisp objects and use Fmessage instead. */
10646 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10647 vmessage (const char *m, va_list ap)
10649 if (noninteractive)
10651 if (m)
10653 if (noninteractive_need_newline)
10654 putc ('\n', stderr);
10655 noninteractive_need_newline = false;
10656 vfprintf (stderr, m, ap);
10657 if (!cursor_in_echo_area)
10658 fprintf (stderr, "\n");
10659 fflush (stderr);
10662 else if (INTERACTIVE)
10664 /* The frame whose mini-buffer we're going to display the message
10665 on. It may be larger than the selected frame, so we need to
10666 use its buffer, not the selected frame's buffer. */
10667 Lisp_Object mini_window;
10668 struct frame *f, *sf = SELECTED_FRAME ();
10670 /* Get the frame containing the mini-buffer
10671 that the selected frame is using. */
10672 mini_window = FRAME_MINIBUF_WINDOW (sf);
10673 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10675 /* Error messages get reported properly by cmd_error, so this must be
10676 just an informative message; if the frame hasn't really been
10677 initialized yet, just toss it. */
10678 if (f->glyphs_initialized_p)
10680 if (m)
10682 ptrdiff_t len;
10683 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10684 USE_SAFE_ALLOCA;
10685 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10687 len = doprnt (message_buf, maxsize, m, 0, ap);
10689 message3 (make_string (message_buf, len));
10690 SAFE_FREE ();
10692 else
10693 message1 (0);
10695 /* Print should start at the beginning of the message
10696 buffer next time. */
10697 message_buf_print = false;
10702 /* See vmessage for restrictions on the text of the message. */
10703 void
10704 message (const char *m, ...)
10706 va_list ap;
10707 va_start (ap, m);
10708 vmessage (m, ap);
10709 va_end (ap);
10713 /* Display the current message in the current mini-buffer. This is
10714 only called from error handlers in process.c, and is not time
10715 critical. */
10717 void
10718 update_echo_area (void)
10720 if (!NILP (echo_area_buffer[0]))
10722 Lisp_Object string;
10723 string = Fcurrent_message ();
10724 message3 (string);
10729 /* Make sure echo area buffers in `echo_buffers' are live.
10730 If they aren't, make new ones. */
10732 static void
10733 ensure_echo_area_buffers (void)
10735 for (int i = 0; i < 2; i++)
10736 if (!BUFFERP (echo_buffer[i])
10737 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10739 Lisp_Object old_buffer = echo_buffer[i];
10740 static char const name_fmt[] = " *Echo Area %d*";
10741 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10742 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10743 echo_buffer[i] = Fget_buffer_create (lname);
10744 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10745 /* to force word wrap in echo area -
10746 it was decided to postpone this*/
10747 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10749 for (int j = 0; j < 2; j++)
10750 if (EQ (old_buffer, echo_area_buffer[j]))
10751 echo_area_buffer[j] = echo_buffer[i];
10756 /* Call FN with args A1..A2 with either the current or last displayed
10757 echo_area_buffer as current buffer.
10759 WHICH zero means use the current message buffer
10760 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10761 from echo_buffer[] and clear it.
10763 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10764 suitable buffer from echo_buffer[] and clear it.
10766 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10767 that the current message becomes the last displayed one, choose a
10768 suitable buffer for echo_area_buffer[0], and clear it.
10770 Value is what FN returns. */
10772 static bool
10773 with_echo_area_buffer (struct window *w, int which,
10774 bool (*fn) (ptrdiff_t, Lisp_Object),
10775 ptrdiff_t a1, Lisp_Object a2)
10777 Lisp_Object buffer;
10778 bool this_one, the_other, clear_buffer_p, rc;
10779 ptrdiff_t count = SPECPDL_INDEX ();
10781 /* If buffers aren't live, make new ones. */
10782 ensure_echo_area_buffers ();
10784 clear_buffer_p = false;
10786 if (which == 0)
10787 this_one = false, the_other = true;
10788 else if (which > 0)
10789 this_one = true, the_other = false;
10790 else
10792 this_one = false, the_other = true;
10793 clear_buffer_p = true;
10795 /* We need a fresh one in case the current echo buffer equals
10796 the one containing the last displayed echo area message. */
10797 if (!NILP (echo_area_buffer[this_one])
10798 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10799 echo_area_buffer[this_one] = Qnil;
10802 /* Choose a suitable buffer from echo_buffer[] if we don't
10803 have one. */
10804 if (NILP (echo_area_buffer[this_one]))
10806 echo_area_buffer[this_one]
10807 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10808 ? echo_buffer[the_other]
10809 : echo_buffer[this_one]);
10810 clear_buffer_p = true;
10813 buffer = echo_area_buffer[this_one];
10815 /* Don't get confused by reusing the buffer used for echoing
10816 for a different purpose. */
10817 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10818 cancel_echoing ();
10820 record_unwind_protect (unwind_with_echo_area_buffer,
10821 with_echo_area_buffer_unwind_data (w));
10823 /* Make the echo area buffer current. Note that for display
10824 purposes, it is not necessary that the displayed window's buffer
10825 == current_buffer, except for text property lookup. So, let's
10826 only set that buffer temporarily here without doing a full
10827 Fset_window_buffer. We must also change w->pointm, though,
10828 because otherwise an assertions in unshow_buffer fails, and Emacs
10829 aborts. */
10830 set_buffer_internal_1 (XBUFFER (buffer));
10831 if (w)
10833 wset_buffer (w, buffer);
10834 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10835 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10838 bset_undo_list (current_buffer, Qt);
10839 bset_read_only (current_buffer, Qnil);
10840 specbind (Qinhibit_read_only, Qt);
10841 specbind (Qinhibit_modification_hooks, Qt);
10843 if (clear_buffer_p && Z > BEG)
10844 del_range (BEG, Z);
10846 eassert (BEGV >= BEG);
10847 eassert (ZV <= Z && ZV >= BEGV);
10849 rc = fn (a1, a2);
10851 eassert (BEGV >= BEG);
10852 eassert (ZV <= Z && ZV >= BEGV);
10854 unbind_to (count, Qnil);
10855 return rc;
10859 /* Save state that should be preserved around the call to the function
10860 FN called in with_echo_area_buffer. */
10862 static Lisp_Object
10863 with_echo_area_buffer_unwind_data (struct window *w)
10865 int i = 0;
10866 Lisp_Object vector, tmp;
10868 /* Reduce consing by keeping one vector in
10869 Vwith_echo_area_save_vector. */
10870 vector = Vwith_echo_area_save_vector;
10871 Vwith_echo_area_save_vector = Qnil;
10873 if (NILP (vector))
10874 vector = Fmake_vector (make_number (11), Qnil);
10876 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10877 ASET (vector, i, Vdeactivate_mark); ++i;
10878 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10880 if (w)
10882 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10883 ASET (vector, i, w->contents); ++i;
10884 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10885 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10886 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10887 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10888 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10889 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10891 else
10893 int end = i + 8;
10894 for (; i < end; ++i)
10895 ASET (vector, i, Qnil);
10898 eassert (i == ASIZE (vector));
10899 return vector;
10903 /* Restore global state from VECTOR which was created by
10904 with_echo_area_buffer_unwind_data. */
10906 static void
10907 unwind_with_echo_area_buffer (Lisp_Object vector)
10909 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10910 Vdeactivate_mark = AREF (vector, 1);
10911 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10913 if (WINDOWP (AREF (vector, 3)))
10915 struct window *w;
10916 Lisp_Object buffer;
10918 w = XWINDOW (AREF (vector, 3));
10919 buffer = AREF (vector, 4);
10921 wset_buffer (w, buffer);
10922 set_marker_both (w->pointm, buffer,
10923 XFASTINT (AREF (vector, 5)),
10924 XFASTINT (AREF (vector, 6)));
10925 set_marker_both (w->old_pointm, buffer,
10926 XFASTINT (AREF (vector, 7)),
10927 XFASTINT (AREF (vector, 8)));
10928 set_marker_both (w->start, buffer,
10929 XFASTINT (AREF (vector, 9)),
10930 XFASTINT (AREF (vector, 10)));
10933 Vwith_echo_area_save_vector = vector;
10937 /* Set up the echo area for use by print functions. MULTIBYTE_P
10938 means we will print multibyte. */
10940 void
10941 setup_echo_area_for_printing (bool multibyte_p)
10943 /* If we can't find an echo area any more, exit. */
10944 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10945 Fkill_emacs (Qnil);
10947 ensure_echo_area_buffers ();
10949 if (!message_buf_print)
10951 /* A message has been output since the last time we printed.
10952 Choose a fresh echo area buffer. */
10953 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10954 echo_area_buffer[0] = echo_buffer[1];
10955 else
10956 echo_area_buffer[0] = echo_buffer[0];
10958 /* Switch to that buffer and clear it. */
10959 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10960 bset_truncate_lines (current_buffer, Qnil);
10962 if (Z > BEG)
10964 ptrdiff_t count = SPECPDL_INDEX ();
10965 specbind (Qinhibit_read_only, Qt);
10966 /* Note that undo recording is always disabled. */
10967 del_range (BEG, Z);
10968 unbind_to (count, Qnil);
10970 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10972 /* Set up the buffer for the multibyteness we need. */
10973 if (multibyte_p
10974 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10975 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10977 /* Raise the frame containing the echo area. */
10978 if (minibuffer_auto_raise)
10980 struct frame *sf = SELECTED_FRAME ();
10981 Lisp_Object mini_window;
10982 mini_window = FRAME_MINIBUF_WINDOW (sf);
10983 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10986 message_log_maybe_newline ();
10987 message_buf_print = true;
10989 else
10991 if (NILP (echo_area_buffer[0]))
10993 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10994 echo_area_buffer[0] = echo_buffer[1];
10995 else
10996 echo_area_buffer[0] = echo_buffer[0];
10999 if (current_buffer != XBUFFER (echo_area_buffer[0]))
11001 /* Someone switched buffers between print requests. */
11002 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11003 bset_truncate_lines (current_buffer, Qnil);
11009 /* Display an echo area message in window W. Value is true if W's
11010 height is changed. If display_last_displayed_message_p,
11011 display the message that was last displayed, otherwise
11012 display the current message. */
11014 static bool
11015 display_echo_area (struct window *w)
11017 bool no_message_p, window_height_changed_p;
11019 /* Temporarily disable garbage collections while displaying the echo
11020 area. This is done because a GC can print a message itself.
11021 That message would modify the echo area buffer's contents while a
11022 redisplay of the buffer is going on, and seriously confuse
11023 redisplay. */
11024 ptrdiff_t count = inhibit_garbage_collection ();
11026 /* If there is no message, we must call display_echo_area_1
11027 nevertheless because it resizes the window. But we will have to
11028 reset the echo_area_buffer in question to nil at the end because
11029 with_echo_area_buffer will sets it to an empty buffer. */
11030 bool i = display_last_displayed_message_p;
11031 /* According to the C99, C11 and C++11 standards, the integral value
11032 of a "bool" is always 0 or 1, so this array access is safe here,
11033 if oddly typed. */
11034 no_message_p = NILP (echo_area_buffer[i]);
11036 window_height_changed_p
11037 = with_echo_area_buffer (w, display_last_displayed_message_p,
11038 display_echo_area_1,
11039 (intptr_t) w, Qnil);
11041 if (no_message_p)
11042 echo_area_buffer[i] = Qnil;
11044 unbind_to (count, Qnil);
11045 return window_height_changed_p;
11049 /* Helper for display_echo_area. Display the current buffer which
11050 contains the current echo area message in window W, a mini-window,
11051 a pointer to which is passed in A1. A2..A4 are currently not used.
11052 Change the height of W so that all of the message is displayed.
11053 Value is true if height of W was changed. */
11055 static bool
11056 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11058 intptr_t i1 = a1;
11059 struct window *w = (struct window *) i1;
11060 Lisp_Object window;
11061 struct text_pos start;
11063 /* We are about to enter redisplay without going through
11064 redisplay_internal, so we need to forget these faces by hand
11065 here. */
11066 forget_escape_and_glyphless_faces ();
11068 /* Do this before displaying, so that we have a large enough glyph
11069 matrix for the display. If we can't get enough space for the
11070 whole text, display the last N lines. That works by setting w->start. */
11071 bool window_height_changed_p = resize_mini_window (w, false);
11073 /* Use the starting position chosen by resize_mini_window. */
11074 SET_TEXT_POS_FROM_MARKER (start, w->start);
11076 /* Display. */
11077 clear_glyph_matrix (w->desired_matrix);
11078 XSETWINDOW (window, w);
11079 try_window (window, start, 0);
11081 return window_height_changed_p;
11085 /* Resize the echo area window to exactly the size needed for the
11086 currently displayed message, if there is one. If a mini-buffer
11087 is active, don't shrink it. */
11089 void
11090 resize_echo_area_exactly (void)
11092 if (BUFFERP (echo_area_buffer[0])
11093 && WINDOWP (echo_area_window))
11095 struct window *w = XWINDOW (echo_area_window);
11096 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11097 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11098 (intptr_t) w, resize_exactly);
11099 if (resized_p)
11101 windows_or_buffers_changed = 42;
11102 update_mode_lines = 30;
11103 redisplay_internal ();
11109 /* Callback function for with_echo_area_buffer, when used from
11110 resize_echo_area_exactly. A1 contains a pointer to the window to
11111 resize, EXACTLY non-nil means resize the mini-window exactly to the
11112 size of the text displayed. A3 and A4 are not used. Value is what
11113 resize_mini_window returns. */
11115 static bool
11116 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11118 intptr_t i1 = a1;
11119 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11123 /* Resize mini-window W to fit the size of its contents. EXACT_P
11124 means size the window exactly to the size needed. Otherwise, it's
11125 only enlarged until W's buffer is empty.
11127 Set W->start to the right place to begin display. If the whole
11128 contents fit, start at the beginning. Otherwise, start so as
11129 to make the end of the contents appear. This is particularly
11130 important for y-or-n-p, but seems desirable generally.
11132 Value is true if the window height has been changed. */
11134 bool
11135 resize_mini_window (struct window *w, bool exact_p)
11137 struct frame *f = XFRAME (w->frame);
11138 bool window_height_changed_p = false;
11140 eassert (MINI_WINDOW_P (w));
11142 /* By default, start display at the beginning. */
11143 set_marker_both (w->start, w->contents,
11144 BUF_BEGV (XBUFFER (w->contents)),
11145 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11147 /* Don't resize windows while redisplaying a window; it would
11148 confuse redisplay functions when the size of the window they are
11149 displaying changes from under them. Such a resizing can happen,
11150 for instance, when which-func prints a long message while
11151 we are running fontification-functions. We're running these
11152 functions with safe_call which binds inhibit-redisplay to t. */
11153 if (!NILP (Vinhibit_redisplay))
11154 return false;
11156 /* Nil means don't try to resize. */
11157 if (NILP (Vresize_mini_windows)
11158 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11159 return false;
11161 if (!FRAME_MINIBUF_ONLY_P (f))
11163 struct it it;
11164 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11165 + WINDOW_PIXEL_HEIGHT (w));
11166 int unit = FRAME_LINE_HEIGHT (f);
11167 int height, max_height;
11168 struct text_pos start;
11169 struct buffer *old_current_buffer = NULL;
11171 if (current_buffer != XBUFFER (w->contents))
11173 old_current_buffer = current_buffer;
11174 set_buffer_internal (XBUFFER (w->contents));
11177 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11179 /* Compute the max. number of lines specified by the user. */
11180 if (FLOATP (Vmax_mini_window_height))
11181 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11182 else if (INTEGERP (Vmax_mini_window_height))
11183 max_height = XINT (Vmax_mini_window_height) * unit;
11184 else
11185 max_height = total_height / 4;
11187 /* Correct that max. height if it's bogus. */
11188 max_height = clip_to_bounds (unit, max_height, total_height);
11190 /* Find out the height of the text in the window. */
11191 if (it.line_wrap == TRUNCATE)
11192 height = unit;
11193 else
11195 last_height = 0;
11196 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11197 if (it.max_ascent == 0 && it.max_descent == 0)
11198 height = it.current_y + last_height;
11199 else
11200 height = it.current_y + it.max_ascent + it.max_descent;
11201 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11204 /* Compute a suitable window start. */
11205 if (height > max_height)
11207 height = (max_height / unit) * unit;
11208 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11209 move_it_vertically_backward (&it, height - unit);
11210 start = it.current.pos;
11212 else
11213 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11214 SET_MARKER_FROM_TEXT_POS (w->start, start);
11216 if (EQ (Vresize_mini_windows, Qgrow_only))
11218 /* Let it grow only, until we display an empty message, in which
11219 case the window shrinks again. */
11220 if (height > WINDOW_PIXEL_HEIGHT (w))
11222 int old_height = WINDOW_PIXEL_HEIGHT (w);
11224 FRAME_WINDOWS_FROZEN (f) = true;
11225 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11226 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11228 else if (height < WINDOW_PIXEL_HEIGHT (w)
11229 && (exact_p || BEGV == ZV))
11231 int old_height = WINDOW_PIXEL_HEIGHT (w);
11233 FRAME_WINDOWS_FROZEN (f) = false;
11234 shrink_mini_window (w, true);
11235 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11238 else
11240 /* Always resize to exact size needed. */
11241 if (height > WINDOW_PIXEL_HEIGHT (w))
11243 int old_height = WINDOW_PIXEL_HEIGHT (w);
11245 FRAME_WINDOWS_FROZEN (f) = true;
11246 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11247 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11249 else if (height < WINDOW_PIXEL_HEIGHT (w))
11251 int old_height = WINDOW_PIXEL_HEIGHT (w);
11253 FRAME_WINDOWS_FROZEN (f) = false;
11254 shrink_mini_window (w, true);
11256 if (height)
11258 FRAME_WINDOWS_FROZEN (f) = true;
11259 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11262 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11266 if (old_current_buffer)
11267 set_buffer_internal (old_current_buffer);
11270 return window_height_changed_p;
11274 /* Value is the current message, a string, or nil if there is no
11275 current message. */
11277 Lisp_Object
11278 current_message (void)
11280 Lisp_Object msg;
11282 if (!BUFFERP (echo_area_buffer[0]))
11283 msg = Qnil;
11284 else
11286 with_echo_area_buffer (0, 0, current_message_1,
11287 (intptr_t) &msg, Qnil);
11288 if (NILP (msg))
11289 echo_area_buffer[0] = Qnil;
11292 return msg;
11296 static bool
11297 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11299 intptr_t i1 = a1;
11300 Lisp_Object *msg = (Lisp_Object *) i1;
11302 if (Z > BEG)
11303 *msg = make_buffer_string (BEG, Z, true);
11304 else
11305 *msg = Qnil;
11306 return false;
11310 /* Push the current message on Vmessage_stack for later restoration
11311 by restore_message. Value is true if the current message isn't
11312 empty. This is a relatively infrequent operation, so it's not
11313 worth optimizing. */
11315 bool
11316 push_message (void)
11318 Lisp_Object msg = current_message ();
11319 Vmessage_stack = Fcons (msg, Vmessage_stack);
11320 return STRINGP (msg);
11324 /* Restore message display from the top of Vmessage_stack. */
11326 void
11327 restore_message (void)
11329 eassert (CONSP (Vmessage_stack));
11330 message3_nolog (XCAR (Vmessage_stack));
11334 /* Handler for unwind-protect calling pop_message. */
11336 void
11337 pop_message_unwind (void)
11339 /* Pop the top-most entry off Vmessage_stack. */
11340 eassert (CONSP (Vmessage_stack));
11341 Vmessage_stack = XCDR (Vmessage_stack);
11345 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11346 exits. If the stack is not empty, we have a missing pop_message
11347 somewhere. */
11349 void
11350 check_message_stack (void)
11352 if (!NILP (Vmessage_stack))
11353 emacs_abort ();
11357 /* Truncate to NCHARS what will be displayed in the echo area the next
11358 time we display it---but don't redisplay it now. */
11360 void
11361 truncate_echo_area (ptrdiff_t nchars)
11363 if (nchars == 0)
11364 echo_area_buffer[0] = Qnil;
11365 else if (!noninteractive
11366 && INTERACTIVE
11367 && !NILP (echo_area_buffer[0]))
11369 struct frame *sf = SELECTED_FRAME ();
11370 /* Error messages get reported properly by cmd_error, so this must be
11371 just an informative message; if the frame hasn't really been
11372 initialized yet, just toss it. */
11373 if (sf->glyphs_initialized_p)
11374 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11379 /* Helper function for truncate_echo_area. Truncate the current
11380 message to at most NCHARS characters. */
11382 static bool
11383 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11385 if (BEG + nchars < Z)
11386 del_range (BEG + nchars, Z);
11387 if (Z == BEG)
11388 echo_area_buffer[0] = Qnil;
11389 return false;
11392 /* Set the current message to STRING. */
11394 static void
11395 set_message (Lisp_Object string)
11397 eassert (STRINGP (string));
11399 message_enable_multibyte = STRING_MULTIBYTE (string);
11401 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11402 message_buf_print = false;
11403 help_echo_showing_p = false;
11405 if (STRINGP (Vdebug_on_message)
11406 && STRINGP (string)
11407 && fast_string_match (Vdebug_on_message, string) >= 0)
11408 call_debugger (list2 (Qerror, string));
11412 /* Helper function for set_message. First argument is ignored and second
11413 argument has the same meaning as for set_message.
11414 This function is called with the echo area buffer being current. */
11416 static bool
11417 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11419 eassert (STRINGP (string));
11421 /* Change multibyteness of the echo buffer appropriately. */
11422 if (message_enable_multibyte
11423 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11424 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11426 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11427 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11428 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11430 /* Insert new message at BEG. */
11431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11433 /* This function takes care of single/multibyte conversion.
11434 We just have to ensure that the echo area buffer has the right
11435 setting of enable_multibyte_characters. */
11436 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11438 return false;
11442 /* Clear messages. CURRENT_P means clear the current message.
11443 LAST_DISPLAYED_P means clear the message last displayed. */
11445 void
11446 clear_message (bool current_p, bool last_displayed_p)
11448 if (current_p)
11450 echo_area_buffer[0] = Qnil;
11451 message_cleared_p = true;
11454 if (last_displayed_p)
11455 echo_area_buffer[1] = Qnil;
11457 message_buf_print = false;
11460 /* Clear garbaged frames.
11462 This function is used where the old redisplay called
11463 redraw_garbaged_frames which in turn called redraw_frame which in
11464 turn called clear_frame. The call to clear_frame was a source of
11465 flickering. I believe a clear_frame is not necessary. It should
11466 suffice in the new redisplay to invalidate all current matrices,
11467 and ensure a complete redisplay of all windows. */
11469 static void
11470 clear_garbaged_frames (void)
11472 if (frame_garbaged)
11474 Lisp_Object tail, frame;
11475 struct frame *sf = SELECTED_FRAME ();
11477 FOR_EACH_FRAME (tail, frame)
11479 struct frame *f = XFRAME (frame);
11481 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11483 if (f->resized_p
11484 /* It makes no sense to redraw a non-selected TTY
11485 frame, since that will actually clear the
11486 selected frame, and might leave the selected
11487 frame with corrupted display, if it happens not
11488 to be marked garbaged. */
11489 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11490 redraw_frame (f);
11491 else
11492 clear_current_matrices (f);
11494 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11495 x_clear_under_internal_border (f);
11496 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11498 fset_redisplay (f);
11499 f->garbaged = false;
11500 f->resized_p = false;
11504 frame_garbaged = false;
11509 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11510 selected_frame. */
11512 static void
11513 echo_area_display (bool update_frame_p)
11515 Lisp_Object mini_window;
11516 struct window *w;
11517 struct frame *f;
11518 bool window_height_changed_p = false;
11519 struct frame *sf = SELECTED_FRAME ();
11521 mini_window = FRAME_MINIBUF_WINDOW (sf);
11522 if (NILP (mini_window))
11523 return;
11525 w = XWINDOW (mini_window);
11526 f = XFRAME (WINDOW_FRAME (w));
11528 /* Don't display if frame is invisible or not yet initialized. */
11529 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11530 return;
11532 #ifdef HAVE_WINDOW_SYSTEM
11533 /* When Emacs starts, selected_frame may be the initial terminal
11534 frame. If we let this through, a message would be displayed on
11535 the terminal. */
11536 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11537 return;
11538 #endif /* HAVE_WINDOW_SYSTEM */
11540 /* Redraw garbaged frames. */
11541 clear_garbaged_frames ();
11543 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11545 echo_area_window = mini_window;
11546 window_height_changed_p = display_echo_area (w);
11547 w->must_be_updated_p = true;
11549 /* Update the display, unless called from redisplay_internal.
11550 Also don't update the screen during redisplay itself. The
11551 update will happen at the end of redisplay, and an update
11552 here could cause confusion. */
11553 if (update_frame_p && !redisplaying_p)
11555 int n = 0;
11557 /* If the display update has been interrupted by pending
11558 input, update mode lines in the frame. Due to the
11559 pending input, it might have been that redisplay hasn't
11560 been called, so that mode lines above the echo area are
11561 garbaged. This looks odd, so we prevent it here. */
11562 if (!display_completed)
11564 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11566 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11567 x_clear_under_internal_border (f);
11568 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11572 if (window_height_changed_p
11573 /* Don't do this if Emacs is shutting down. Redisplay
11574 needs to run hooks. */
11575 && !NILP (Vrun_hooks))
11577 /* Must update other windows. Likewise as in other
11578 cases, don't let this update be interrupted by
11579 pending input. */
11580 ptrdiff_t count = SPECPDL_INDEX ();
11581 specbind (Qredisplay_dont_pause, Qt);
11582 fset_redisplay (f);
11583 redisplay_internal ();
11584 unbind_to (count, Qnil);
11586 else if (FRAME_WINDOW_P (f) && n == 0)
11588 /* Window configuration is the same as before.
11589 Can do with a display update of the echo area,
11590 unless we displayed some mode lines. */
11591 update_single_window (w);
11592 flush_frame (f);
11594 else
11595 update_frame (f, true, true);
11597 /* If cursor is in the echo area, make sure that the next
11598 redisplay displays the minibuffer, so that the cursor will
11599 be replaced with what the minibuffer wants. */
11600 if (cursor_in_echo_area)
11601 wset_redisplay (XWINDOW (mini_window));
11604 else if (!EQ (mini_window, selected_window))
11605 wset_redisplay (XWINDOW (mini_window));
11607 /* Last displayed message is now the current message. */
11608 echo_area_buffer[1] = echo_area_buffer[0];
11609 /* Inform read_char that we're not echoing. */
11610 echo_message_buffer = Qnil;
11612 /* Prevent redisplay optimization in redisplay_internal by resetting
11613 this_line_start_pos. This is done because the mini-buffer now
11614 displays the message instead of its buffer text. */
11615 if (EQ (mini_window, selected_window))
11616 CHARPOS (this_line_start_pos) = 0;
11618 if (window_height_changed_p)
11620 fset_redisplay (f);
11622 /* If window configuration was changed, frames may have been
11623 marked garbaged. Clear them or we will experience
11624 surprises wrt scrolling.
11625 FIXME: How/why/when? */
11626 clear_garbaged_frames ();
11630 /* True if W's buffer was changed but not saved. */
11632 static bool
11633 window_buffer_changed (struct window *w)
11635 struct buffer *b = XBUFFER (w->contents);
11637 eassert (BUFFER_LIVE_P (b));
11639 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11642 /* True if W has %c or %C in its mode line and mode line should be updated. */
11644 static bool
11645 mode_line_update_needed (struct window *w)
11647 return (w->column_number_displayed != -1
11648 && !(PT == w->last_point && !window_outdated (w))
11649 && (w->column_number_displayed != current_column ()));
11652 /* True if window start of W is frozen and may not be changed during
11653 redisplay. */
11655 static bool
11656 window_frozen_p (struct window *w)
11658 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11660 Lisp_Object window;
11662 XSETWINDOW (window, w);
11663 if (MINI_WINDOW_P (w))
11664 return false;
11665 else if (EQ (window, selected_window))
11666 return false;
11667 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11668 && EQ (window, Vminibuf_scroll_window))
11669 /* This special window can't be frozen too. */
11670 return false;
11671 else
11672 return true;
11674 return false;
11677 /***********************************************************************
11678 Mode Lines and Frame Titles
11679 ***********************************************************************/
11681 /* A buffer for constructing non-propertized mode-line strings and
11682 frame titles in it; allocated from the heap in init_xdisp and
11683 resized as needed in store_mode_line_noprop_char. */
11685 static char *mode_line_noprop_buf;
11687 /* The buffer's end, and a current output position in it. */
11689 static char *mode_line_noprop_buf_end;
11690 static char *mode_line_noprop_ptr;
11692 #define MODE_LINE_NOPROP_LEN(start) \
11693 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11695 static enum {
11696 MODE_LINE_DISPLAY = 0,
11697 MODE_LINE_TITLE,
11698 MODE_LINE_NOPROP,
11699 MODE_LINE_STRING
11700 } mode_line_target;
11702 /* Alist that caches the results of :propertize.
11703 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11704 static Lisp_Object mode_line_proptrans_alist;
11706 /* List of strings making up the mode-line. */
11707 static Lisp_Object mode_line_string_list;
11709 /* Base face property when building propertized mode line string. */
11710 static Lisp_Object mode_line_string_face;
11711 static Lisp_Object mode_line_string_face_prop;
11714 /* Unwind data for mode line strings */
11716 static Lisp_Object Vmode_line_unwind_vector;
11718 static Lisp_Object
11719 format_mode_line_unwind_data (struct frame *target_frame,
11720 struct buffer *obuf,
11721 Lisp_Object owin,
11722 bool save_proptrans)
11724 Lisp_Object vector, tmp;
11726 /* Reduce consing by keeping one vector in
11727 Vwith_echo_area_save_vector. */
11728 vector = Vmode_line_unwind_vector;
11729 Vmode_line_unwind_vector = Qnil;
11731 if (NILP (vector))
11732 vector = Fmake_vector (make_number (10), Qnil);
11734 ASET (vector, 0, make_number (mode_line_target));
11735 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11736 ASET (vector, 2, mode_line_string_list);
11737 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11738 ASET (vector, 4, mode_line_string_face);
11739 ASET (vector, 5, mode_line_string_face_prop);
11741 if (obuf)
11742 XSETBUFFER (tmp, obuf);
11743 else
11744 tmp = Qnil;
11745 ASET (vector, 6, tmp);
11746 ASET (vector, 7, owin);
11747 if (target_frame)
11749 /* Similarly to `with-selected-window', if the operation selects
11750 a window on another frame, we must restore that frame's
11751 selected window, and (for a tty) the top-frame. */
11752 ASET (vector, 8, target_frame->selected_window);
11753 if (FRAME_TERMCAP_P (target_frame))
11754 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11757 return vector;
11760 static void
11761 unwind_format_mode_line (Lisp_Object vector)
11763 Lisp_Object old_window = AREF (vector, 7);
11764 Lisp_Object target_frame_window = AREF (vector, 8);
11765 Lisp_Object old_top_frame = AREF (vector, 9);
11767 mode_line_target = XINT (AREF (vector, 0));
11768 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11769 mode_line_string_list = AREF (vector, 2);
11770 if (! EQ (AREF (vector, 3), Qt))
11771 mode_line_proptrans_alist = AREF (vector, 3);
11772 mode_line_string_face = AREF (vector, 4);
11773 mode_line_string_face_prop = AREF (vector, 5);
11775 /* Select window before buffer, since it may change the buffer. */
11776 if (!NILP (old_window))
11778 /* If the operation that we are unwinding had selected a window
11779 on a different frame, reset its frame-selected-window. For a
11780 text terminal, reset its top-frame if necessary. */
11781 if (!NILP (target_frame_window))
11783 Lisp_Object frame
11784 = WINDOW_FRAME (XWINDOW (target_frame_window));
11786 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11787 Fselect_window (target_frame_window, Qt);
11789 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11790 Fselect_frame (old_top_frame, Qt);
11793 Fselect_window (old_window, Qt);
11796 if (!NILP (AREF (vector, 6)))
11798 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11799 ASET (vector, 6, Qnil);
11802 Vmode_line_unwind_vector = vector;
11806 /* Store a single character C for the frame title in mode_line_noprop_buf.
11807 Re-allocate mode_line_noprop_buf if necessary. */
11809 static void
11810 store_mode_line_noprop_char (char c)
11812 /* If output position has reached the end of the allocated buffer,
11813 increase the buffer's size. */
11814 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11816 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11817 ptrdiff_t size = len;
11818 mode_line_noprop_buf =
11819 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11820 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11821 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11824 *mode_line_noprop_ptr++ = c;
11828 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11829 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11830 characters that yield more columns than PRECISION; PRECISION <= 0
11831 means copy the whole string. Pad with spaces until FIELD_WIDTH
11832 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11833 pad. Called from display_mode_element when it is used to build a
11834 frame title. */
11836 static int
11837 store_mode_line_noprop (const char *string, int field_width, int precision)
11839 const unsigned char *str = (const unsigned char *) string;
11840 int n = 0;
11841 ptrdiff_t dummy, nbytes;
11843 /* Copy at most PRECISION chars from STR. */
11844 nbytes = strlen (string);
11845 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11846 while (nbytes--)
11847 store_mode_line_noprop_char (*str++);
11849 /* Fill up with spaces until FIELD_WIDTH reached. */
11850 while (field_width > 0
11851 && n < field_width)
11853 store_mode_line_noprop_char (' ');
11854 ++n;
11857 return n;
11860 /***********************************************************************
11861 Frame Titles
11862 ***********************************************************************/
11864 #ifdef HAVE_WINDOW_SYSTEM
11866 /* Set the title of FRAME, if it has changed. The title format is
11867 Vicon_title_format if FRAME is iconified, otherwise it is
11868 frame_title_format. */
11870 static void
11871 x_consider_frame_title (Lisp_Object frame)
11873 struct frame *f = XFRAME (frame);
11875 if ((FRAME_WINDOW_P (f)
11876 || FRAME_MINIBUF_ONLY_P (f)
11877 || f->explicit_name)
11878 && !FRAME_TOOLTIP_P (f))
11880 /* Do we have more than one visible frame on this X display? */
11881 Lisp_Object tail, other_frame, fmt;
11882 ptrdiff_t title_start;
11883 char *title;
11884 ptrdiff_t len;
11885 struct it it;
11886 ptrdiff_t count = SPECPDL_INDEX ();
11888 FOR_EACH_FRAME (tail, other_frame)
11890 struct frame *tf = XFRAME (other_frame);
11892 if (tf != f
11893 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11894 && !FRAME_MINIBUF_ONLY_P (tf)
11895 && !FRAME_PARENT_FRAME (tf)
11896 && !FRAME_TOOLTIP_P (tf)
11897 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11898 break;
11901 /* Set global variable indicating that multiple frames exist. */
11902 multiple_frames = CONSP (tail);
11904 /* Switch to the buffer of selected window of the frame. Set up
11905 mode_line_target so that display_mode_element will output into
11906 mode_line_noprop_buf; then display the title. */
11907 record_unwind_protect (unwind_format_mode_line,
11908 format_mode_line_unwind_data
11909 (f, current_buffer, selected_window, false));
11910 /* select-frame calls resize_mini_window, which could resize the
11911 mini-window and by that undo the effect of this redisplay
11912 cycle wrt minibuffer and echo-area display. Binding
11913 inhibit-redisplay to t makes the call to resize_mini_window a
11914 no-op, thus avoiding the adverse side effects. */
11915 specbind (Qinhibit_redisplay, Qt);
11917 Fselect_window (f->selected_window, Qt);
11918 set_buffer_internal_1
11919 (XBUFFER (XWINDOW (f->selected_window)->contents));
11920 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11922 mode_line_target = MODE_LINE_TITLE;
11923 title_start = MODE_LINE_NOPROP_LEN (0);
11924 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11925 NULL, DEFAULT_FACE_ID);
11926 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11927 len = MODE_LINE_NOPROP_LEN (title_start);
11928 title = mode_line_noprop_buf + title_start;
11929 unbind_to (count, Qnil);
11931 /* Set the title only if it's changed. This avoids consing in
11932 the common case where it hasn't. (If it turns out that we've
11933 already wasted too much time by walking through the list with
11934 display_mode_element, then we might need to optimize at a
11935 higher level than this.) */
11936 if (! STRINGP (f->name)
11937 || SBYTES (f->name) != len
11938 || memcmp (title, SDATA (f->name), len) != 0)
11939 x_implicitly_set_name (f, make_string (title, len), Qnil);
11943 #endif /* not HAVE_WINDOW_SYSTEM */
11946 /***********************************************************************
11947 Menu Bars
11948 ***********************************************************************/
11950 /* True if we will not redisplay all visible windows. */
11951 #define REDISPLAY_SOME_P() \
11952 ((windows_or_buffers_changed == 0 \
11953 || windows_or_buffers_changed == REDISPLAY_SOME) \
11954 && (update_mode_lines == 0 \
11955 || update_mode_lines == REDISPLAY_SOME))
11957 /* Prepare for redisplay by updating menu-bar item lists when
11958 appropriate. This can call eval. */
11960 static void
11961 prepare_menu_bars (void)
11963 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11964 bool some_windows = REDISPLAY_SOME_P ();
11966 if (FUNCTIONP (Vpre_redisplay_function))
11968 Lisp_Object windows = all_windows ? Qt : Qnil;
11969 if (all_windows && some_windows)
11971 Lisp_Object ws = window_list ();
11972 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11974 Lisp_Object this = XCAR (ws);
11975 struct window *w = XWINDOW (this);
11976 if (w->redisplay
11977 || XFRAME (w->frame)->redisplay
11978 || XBUFFER (w->contents)->text->redisplay)
11980 windows = Fcons (this, windows);
11984 safe__call1 (true, Vpre_redisplay_function, windows);
11987 /* Update all frame titles based on their buffer names, etc. We do
11988 this before the menu bars so that the buffer-menu will show the
11989 up-to-date frame titles. */
11990 #ifdef HAVE_WINDOW_SYSTEM
11991 if (all_windows)
11993 Lisp_Object tail, frame;
11995 FOR_EACH_FRAME (tail, frame)
11997 struct frame *f = XFRAME (frame);
11998 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11999 if (some_windows
12000 && !f->redisplay
12001 && !w->redisplay
12002 && !XBUFFER (w->contents)->text->redisplay)
12003 continue;
12005 if (!FRAME_TOOLTIP_P (f)
12006 && !FRAME_PARENT_FRAME (f)
12007 && (FRAME_ICONIFIED_P (f)
12008 || FRAME_VISIBLE_P (f) == 1
12009 /* Exclude TTY frames that are obscured because they
12010 are not the top frame on their console. This is
12011 because x_consider_frame_title actually switches
12012 to the frame, which for TTY frames means it is
12013 marked as garbaged, and will be completely
12014 redrawn on the next redisplay cycle. This causes
12015 TTY frames to be completely redrawn, when there
12016 are more than one of them, even though nothing
12017 should be changed on display. */
12018 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12019 x_consider_frame_title (frame);
12022 #endif /* HAVE_WINDOW_SYSTEM */
12024 /* Update the menu bar item lists, if appropriate. This has to be
12025 done before any actual redisplay or generation of display lines. */
12027 if (all_windows)
12029 Lisp_Object tail, frame;
12030 ptrdiff_t count = SPECPDL_INDEX ();
12031 /* True means that update_menu_bar has run its hooks
12032 so any further calls to update_menu_bar shouldn't do so again. */
12033 bool menu_bar_hooks_run = false;
12035 record_unwind_save_match_data ();
12037 FOR_EACH_FRAME (tail, frame)
12039 struct frame *f = XFRAME (frame);
12040 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12042 /* Ignore tooltip frame. */
12043 if (FRAME_TOOLTIP_P (f))
12044 continue;
12046 if (some_windows
12047 && !f->redisplay
12048 && !w->redisplay
12049 && !XBUFFER (w->contents)->text->redisplay)
12050 continue;
12052 run_window_size_change_functions (frame);
12054 if (FRAME_PARENT_FRAME (f))
12055 continue;
12057 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12058 #ifdef HAVE_WINDOW_SYSTEM
12059 update_tool_bar (f, false);
12060 #endif
12063 unbind_to (count, Qnil);
12065 else
12067 struct frame *sf = SELECTED_FRAME ();
12068 update_menu_bar (sf, true, false);
12069 #ifdef HAVE_WINDOW_SYSTEM
12070 update_tool_bar (sf, true);
12071 #endif
12076 /* Update the menu bar item list for frame F. This has to be done
12077 before we start to fill in any display lines, because it can call
12078 eval.
12080 If SAVE_MATCH_DATA, we must save and restore it here.
12082 If HOOKS_RUN, a previous call to update_menu_bar
12083 already ran the menu bar hooks for this redisplay, so there
12084 is no need to run them again. The return value is the
12085 updated value of this flag, to pass to the next call. */
12087 static bool
12088 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12090 Lisp_Object window;
12091 struct window *w;
12093 /* If called recursively during a menu update, do nothing. This can
12094 happen when, for instance, an activate-menubar-hook causes a
12095 redisplay. */
12096 if (inhibit_menubar_update)
12097 return hooks_run;
12099 window = FRAME_SELECTED_WINDOW (f);
12100 w = XWINDOW (window);
12102 if (FRAME_WINDOW_P (f)
12104 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12105 || defined (HAVE_NS) || defined (USE_GTK)
12106 FRAME_EXTERNAL_MENU_BAR (f)
12107 #else
12108 FRAME_MENU_BAR_LINES (f) > 0
12109 #endif
12110 : FRAME_MENU_BAR_LINES (f) > 0)
12112 /* If the user has switched buffers or windows, we need to
12113 recompute to reflect the new bindings. But we'll
12114 recompute when update_mode_lines is set too; that means
12115 that people can use force-mode-line-update to request
12116 that the menu bar be recomputed. The adverse effect on
12117 the rest of the redisplay algorithm is about the same as
12118 windows_or_buffers_changed anyway. */
12119 if (windows_or_buffers_changed
12120 /* This used to test w->update_mode_line, but we believe
12121 there is no need to recompute the menu in that case. */
12122 || update_mode_lines
12123 || window_buffer_changed (w))
12125 struct buffer *prev = current_buffer;
12126 ptrdiff_t count = SPECPDL_INDEX ();
12128 specbind (Qinhibit_menubar_update, Qt);
12130 set_buffer_internal_1 (XBUFFER (w->contents));
12131 if (save_match_data)
12132 record_unwind_save_match_data ();
12133 if (NILP (Voverriding_local_map_menu_flag))
12135 specbind (Qoverriding_terminal_local_map, Qnil);
12136 specbind (Qoverriding_local_map, Qnil);
12139 if (!hooks_run)
12141 /* Run the Lucid hook. */
12142 safe_run_hooks (Qactivate_menubar_hook);
12144 /* If it has changed current-menubar from previous value,
12145 really recompute the menu-bar from the value. */
12146 if (! NILP (Vlucid_menu_bar_dirty_flag))
12147 call0 (Qrecompute_lucid_menubar);
12149 safe_run_hooks (Qmenu_bar_update_hook);
12151 hooks_run = true;
12154 XSETFRAME (Vmenu_updating_frame, f);
12155 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12157 /* Redisplay the menu bar in case we changed it. */
12158 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12159 || defined (HAVE_NS) || defined (USE_GTK)
12160 if (FRAME_WINDOW_P (f))
12162 #if defined (HAVE_NS)
12163 /* All frames on Mac OS share the same menubar. So only
12164 the selected frame should be allowed to set it. */
12165 if (f == SELECTED_FRAME ())
12166 #endif
12167 set_frame_menubar (f, false, false);
12169 else
12170 /* On a terminal screen, the menu bar is an ordinary screen
12171 line, and this makes it get updated. */
12172 w->update_mode_line = true;
12173 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12174 /* In the non-toolkit version, the menu bar is an ordinary screen
12175 line, and this makes it get updated. */
12176 w->update_mode_line = true;
12177 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12179 unbind_to (count, Qnil);
12180 set_buffer_internal_1 (prev);
12184 return hooks_run;
12187 /***********************************************************************
12188 Tool-bars
12189 ***********************************************************************/
12191 #ifdef HAVE_WINDOW_SYSTEM
12193 /* Select `frame' temporarily without running all the code in
12194 do_switch_frame.
12195 FIXME: Maybe do_switch_frame should be trimmed down similarly
12196 when `norecord' is set. */
12197 static void
12198 fast_set_selected_frame (Lisp_Object frame)
12200 if (!EQ (selected_frame, frame))
12202 selected_frame = frame;
12203 selected_window = XFRAME (frame)->selected_window;
12207 /* Update the tool-bar item list for frame F. This has to be done
12208 before we start to fill in any display lines. Called from
12209 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12210 and restore it here. */
12212 static void
12213 update_tool_bar (struct frame *f, bool save_match_data)
12215 #if defined (USE_GTK) || defined (HAVE_NS)
12216 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12217 #else
12218 bool do_update = (WINDOWP (f->tool_bar_window)
12219 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12220 #endif
12222 if (do_update)
12224 Lisp_Object window;
12225 struct window *w;
12227 window = FRAME_SELECTED_WINDOW (f);
12228 w = XWINDOW (window);
12230 /* If the user has switched buffers or windows, we need to
12231 recompute to reflect the new bindings. But we'll
12232 recompute when update_mode_lines is set too; that means
12233 that people can use force-mode-line-update to request
12234 that the menu bar be recomputed. The adverse effect on
12235 the rest of the redisplay algorithm is about the same as
12236 windows_or_buffers_changed anyway. */
12237 if (windows_or_buffers_changed
12238 || w->update_mode_line
12239 || update_mode_lines
12240 || window_buffer_changed (w))
12242 struct buffer *prev = current_buffer;
12243 ptrdiff_t count = SPECPDL_INDEX ();
12244 Lisp_Object frame, new_tool_bar;
12245 int new_n_tool_bar;
12247 /* Set current_buffer to the buffer of the selected
12248 window of the frame, so that we get the right local
12249 keymaps. */
12250 set_buffer_internal_1 (XBUFFER (w->contents));
12252 /* Save match data, if we must. */
12253 if (save_match_data)
12254 record_unwind_save_match_data ();
12256 /* Make sure that we don't accidentally use bogus keymaps. */
12257 if (NILP (Voverriding_local_map_menu_flag))
12259 specbind (Qoverriding_terminal_local_map, Qnil);
12260 specbind (Qoverriding_local_map, Qnil);
12263 /* We must temporarily set the selected frame to this frame
12264 before calling tool_bar_items, because the calculation of
12265 the tool-bar keymap uses the selected frame (see
12266 `tool-bar-make-keymap' in tool-bar.el). */
12267 eassert (EQ (selected_window,
12268 /* Since we only explicitly preserve selected_frame,
12269 check that selected_window would be redundant. */
12270 XFRAME (selected_frame)->selected_window));
12271 record_unwind_protect (fast_set_selected_frame, selected_frame);
12272 XSETFRAME (frame, f);
12273 fast_set_selected_frame (frame);
12275 /* Build desired tool-bar items from keymaps. */
12276 new_tool_bar
12277 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12278 &new_n_tool_bar);
12280 /* Redisplay the tool-bar if we changed it. */
12281 if (new_n_tool_bar != f->n_tool_bar_items
12282 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12284 /* Redisplay that happens asynchronously due to an expose event
12285 may access f->tool_bar_items. Make sure we update both
12286 variables within BLOCK_INPUT so no such event interrupts. */
12287 block_input ();
12288 fset_tool_bar_items (f, new_tool_bar);
12289 f->n_tool_bar_items = new_n_tool_bar;
12290 w->update_mode_line = true;
12291 unblock_input ();
12294 unbind_to (count, Qnil);
12295 set_buffer_internal_1 (prev);
12300 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12302 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12303 F's desired tool-bar contents. F->tool_bar_items must have
12304 been set up previously by calling prepare_menu_bars. */
12306 static void
12307 build_desired_tool_bar_string (struct frame *f)
12309 int i, size, size_needed;
12310 Lisp_Object image, plist;
12312 image = plist = Qnil;
12314 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12315 Otherwise, make a new string. */
12317 /* The size of the string we might be able to reuse. */
12318 size = (STRINGP (f->desired_tool_bar_string)
12319 ? SCHARS (f->desired_tool_bar_string)
12320 : 0);
12322 /* We need one space in the string for each image. */
12323 size_needed = f->n_tool_bar_items;
12325 /* Reuse f->desired_tool_bar_string, if possible. */
12326 if (size < size_needed || NILP (f->desired_tool_bar_string))
12327 fset_desired_tool_bar_string
12328 (f, Fmake_string (make_number (size_needed), make_number (' '), Qnil));
12329 else
12331 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12332 Fremove_text_properties (make_number (0), make_number (size),
12333 props, f->desired_tool_bar_string);
12336 /* Put a `display' property on the string for the images to display,
12337 put a `menu_item' property on tool-bar items with a value that
12338 is the index of the item in F's tool-bar item vector. */
12339 for (i = 0; i < f->n_tool_bar_items; ++i)
12341 #define PROP(IDX) \
12342 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12344 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12345 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12346 int hmargin, vmargin, relief, idx, end;
12348 /* If image is a vector, choose the image according to the
12349 button state. */
12350 image = PROP (TOOL_BAR_ITEM_IMAGES);
12351 if (VECTORP (image))
12353 if (enabled_p)
12354 idx = (selected_p
12355 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12356 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12357 else
12358 idx = (selected_p
12359 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12360 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12362 eassert (ASIZE (image) >= idx);
12363 image = AREF (image, idx);
12365 else
12366 idx = -1;
12368 /* Ignore invalid image specifications. */
12369 if (!valid_image_p (image))
12370 continue;
12372 /* Display the tool-bar button pressed, or depressed. */
12373 plist = Fcopy_sequence (XCDR (image));
12375 /* Compute margin and relief to draw. */
12376 relief = (tool_bar_button_relief >= 0
12377 ? tool_bar_button_relief
12378 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12379 hmargin = vmargin = relief;
12381 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12382 INT_MAX - max (hmargin, vmargin)))
12384 hmargin += XFASTINT (Vtool_bar_button_margin);
12385 vmargin += XFASTINT (Vtool_bar_button_margin);
12387 else if (CONSP (Vtool_bar_button_margin))
12389 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12390 INT_MAX - hmargin))
12391 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12393 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12394 INT_MAX - vmargin))
12395 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12398 if (auto_raise_tool_bar_buttons_p)
12400 /* Add a `:relief' property to the image spec if the item is
12401 selected. */
12402 if (selected_p)
12404 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12405 hmargin -= relief;
12406 vmargin -= relief;
12409 else
12411 /* If image is selected, display it pressed, i.e. with a
12412 negative relief. If it's not selected, display it with a
12413 raised relief. */
12414 plist = Fplist_put (plist, QCrelief,
12415 (selected_p
12416 ? make_number (-relief)
12417 : make_number (relief)));
12418 hmargin -= relief;
12419 vmargin -= relief;
12422 /* Put a margin around the image. */
12423 if (hmargin || vmargin)
12425 if (hmargin == vmargin)
12426 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12427 else
12428 plist = Fplist_put (plist, QCmargin,
12429 Fcons (make_number (hmargin),
12430 make_number (vmargin)));
12433 /* If button is not enabled, and we don't have special images
12434 for the disabled state, make the image appear disabled by
12435 applying an appropriate algorithm to it. */
12436 if (!enabled_p && idx < 0)
12437 plist = Fplist_put (plist, QCconversion, Qdisabled);
12439 /* Put a `display' text property on the string for the image to
12440 display. Put a `menu-item' property on the string that gives
12441 the start of this item's properties in the tool-bar items
12442 vector. */
12443 image = Fcons (Qimage, plist);
12444 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12445 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12447 /* Let the last image hide all remaining spaces in the tool bar
12448 string. The string can be longer than needed when we reuse a
12449 previous string. */
12450 if (i + 1 == f->n_tool_bar_items)
12451 end = SCHARS (f->desired_tool_bar_string);
12452 else
12453 end = i + 1;
12454 Fadd_text_properties (make_number (i), make_number (end),
12455 props, f->desired_tool_bar_string);
12456 #undef PROP
12461 /* Display one line of the tool-bar of frame IT->f.
12463 HEIGHT specifies the desired height of the tool-bar line.
12464 If the actual height of the glyph row is less than HEIGHT, the
12465 row's height is increased to HEIGHT, and the icons are centered
12466 vertically in the new height.
12468 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12469 count a final empty row in case the tool-bar width exactly matches
12470 the window width.
12473 static void
12474 display_tool_bar_line (struct it *it, int height)
12476 struct glyph_row *row = it->glyph_row;
12477 int max_x = it->last_visible_x;
12478 struct glyph *last;
12480 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12481 clear_glyph_row (row);
12482 row->enabled_p = true;
12483 row->y = it->current_y;
12485 /* Note that this isn't made use of if the face hasn't a box,
12486 so there's no need to check the face here. */
12487 it->start_of_box_run_p = true;
12489 while (it->current_x < max_x)
12491 int x, n_glyphs_before, i, nglyphs;
12492 struct it it_before;
12494 /* Get the next display element. */
12495 if (!get_next_display_element (it))
12497 /* Don't count empty row if we are counting needed tool-bar lines. */
12498 if (height < 0 && !it->hpos)
12499 return;
12500 break;
12503 /* Produce glyphs. */
12504 n_glyphs_before = row->used[TEXT_AREA];
12505 it_before = *it;
12507 PRODUCE_GLYPHS (it);
12509 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12510 i = 0;
12511 x = it_before.current_x;
12512 while (i < nglyphs)
12514 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12516 if (x + glyph->pixel_width > max_x)
12518 /* Glyph doesn't fit on line. Backtrack. */
12519 row->used[TEXT_AREA] = n_glyphs_before;
12520 *it = it_before;
12521 /* If this is the only glyph on this line, it will never fit on the
12522 tool-bar, so skip it. But ensure there is at least one glyph,
12523 so we don't accidentally disable the tool-bar. */
12524 if (n_glyphs_before == 0
12525 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12526 break;
12527 goto out;
12530 ++it->hpos;
12531 x += glyph->pixel_width;
12532 ++i;
12535 /* Stop at line end. */
12536 if (ITERATOR_AT_END_OF_LINE_P (it))
12537 break;
12539 set_iterator_to_next (it, true);
12542 out:;
12544 row->displays_text_p = row->used[TEXT_AREA] != 0;
12546 /* Use default face for the border below the tool bar.
12548 FIXME: When auto-resize-tool-bars is grow-only, there is
12549 no additional border below the possibly empty tool-bar lines.
12550 So to make the extra empty lines look "normal", we have to
12551 use the tool-bar face for the border too. */
12552 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12553 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12554 it->face_id = DEFAULT_FACE_ID;
12556 extend_face_to_end_of_line (it);
12557 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12558 last->right_box_line_p = true;
12559 if (last == row->glyphs[TEXT_AREA])
12560 last->left_box_line_p = true;
12562 /* Make line the desired height and center it vertically. */
12563 if ((height -= it->max_ascent + it->max_descent) > 0)
12565 /* Don't add more than one line height. */
12566 height %= FRAME_LINE_HEIGHT (it->f);
12567 it->max_ascent += height / 2;
12568 it->max_descent += (height + 1) / 2;
12571 compute_line_metrics (it);
12573 /* If line is empty, make it occupy the rest of the tool-bar. */
12574 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12576 row->height = row->phys_height = it->last_visible_y - row->y;
12577 row->visible_height = row->height;
12578 row->ascent = row->phys_ascent = 0;
12579 row->extra_line_spacing = 0;
12582 row->full_width_p = true;
12583 row->continued_p = false;
12584 row->truncated_on_left_p = false;
12585 row->truncated_on_right_p = false;
12587 it->current_x = it->hpos = 0;
12588 it->current_y += row->height;
12589 ++it->vpos;
12590 ++it->glyph_row;
12594 /* Value is the number of pixels needed to make all tool-bar items of
12595 frame F visible. The actual number of glyph rows needed is
12596 returned in *N_ROWS if non-NULL. */
12597 static int
12598 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12600 struct window *w = XWINDOW (f->tool_bar_window);
12601 struct it it;
12602 /* tool_bar_height is called from redisplay_tool_bar after building
12603 the desired matrix, so use (unused) mode-line row as temporary row to
12604 avoid destroying the first tool-bar row. */
12605 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12607 /* Initialize an iterator for iteration over
12608 F->desired_tool_bar_string in the tool-bar window of frame F. */
12609 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12610 temp_row->reversed_p = false;
12611 it.first_visible_x = 0;
12612 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12613 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12614 it.paragraph_embedding = L2R;
12616 while (!ITERATOR_AT_END_P (&it))
12618 clear_glyph_row (temp_row);
12619 it.glyph_row = temp_row;
12620 display_tool_bar_line (&it, -1);
12622 clear_glyph_row (temp_row);
12624 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12625 if (n_rows)
12626 *n_rows = it.vpos > 0 ? it.vpos : -1;
12628 if (pixelwise)
12629 return it.current_y;
12630 else
12631 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12634 #endif /* !USE_GTK && !HAVE_NS */
12636 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12637 0, 2, 0,
12638 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12639 If FRAME is nil or omitted, use the selected frame. Optional argument
12640 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12641 (Lisp_Object frame, Lisp_Object pixelwise)
12643 int height = 0;
12645 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12646 struct frame *f = decode_any_frame (frame);
12648 if (WINDOWP (f->tool_bar_window)
12649 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12651 update_tool_bar (f, true);
12652 if (f->n_tool_bar_items)
12654 build_desired_tool_bar_string (f);
12655 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12658 #endif
12660 return make_number (height);
12664 /* Display the tool-bar of frame F. Value is true if tool-bar's
12665 height should be changed. */
12666 static bool
12667 redisplay_tool_bar (struct frame *f)
12669 f->tool_bar_redisplayed = true;
12670 #if defined (USE_GTK) || defined (HAVE_NS)
12672 if (FRAME_EXTERNAL_TOOL_BAR (f))
12673 update_frame_tool_bar (f);
12674 return false;
12676 #else /* !USE_GTK && !HAVE_NS */
12678 struct window *w;
12679 struct it it;
12680 struct glyph_row *row;
12682 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12683 do anything. This means you must start with tool-bar-lines
12684 non-zero to get the auto-sizing effect. Or in other words, you
12685 can turn off tool-bars by specifying tool-bar-lines zero. */
12686 if (!WINDOWP (f->tool_bar_window)
12687 || (w = XWINDOW (f->tool_bar_window),
12688 WINDOW_TOTAL_LINES (w) == 0))
12689 return false;
12691 /* Set up an iterator for the tool-bar window. */
12692 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12693 it.first_visible_x = 0;
12694 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12695 row = it.glyph_row;
12696 row->reversed_p = false;
12698 /* Build a string that represents the contents of the tool-bar. */
12699 build_desired_tool_bar_string (f);
12700 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12701 /* FIXME: This should be controlled by a user option. But it
12702 doesn't make sense to have an R2L tool bar if the menu bar cannot
12703 be drawn also R2L, and making the menu bar R2L is tricky due
12704 toolkit-specific code that implements it. If an R2L tool bar is
12705 ever supported, display_tool_bar_line should also be augmented to
12706 call unproduce_glyphs like display_line and display_string
12707 do. */
12708 it.paragraph_embedding = L2R;
12710 if (f->n_tool_bar_rows == 0)
12712 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12714 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12716 x_change_tool_bar_height (f, new_height);
12717 frame_default_tool_bar_height = new_height;
12718 /* Always do that now. */
12719 clear_glyph_matrix (w->desired_matrix);
12720 f->fonts_changed = true;
12721 return true;
12725 /* Display as many lines as needed to display all tool-bar items. */
12727 if (f->n_tool_bar_rows > 0)
12729 int border, rows, height, extra;
12731 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12732 border = XINT (Vtool_bar_border);
12733 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12734 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12735 else if (EQ (Vtool_bar_border, Qborder_width))
12736 border = f->border_width;
12737 else
12738 border = 0;
12739 if (border < 0)
12740 border = 0;
12742 rows = f->n_tool_bar_rows;
12743 height = max (1, (it.last_visible_y - border) / rows);
12744 extra = it.last_visible_y - border - height * rows;
12746 while (it.current_y < it.last_visible_y)
12748 int h = 0;
12749 if (extra > 0 && rows-- > 0)
12751 h = (extra + rows - 1) / rows;
12752 extra -= h;
12754 display_tool_bar_line (&it, height + h);
12757 else
12759 while (it.current_y < it.last_visible_y)
12760 display_tool_bar_line (&it, 0);
12763 /* It doesn't make much sense to try scrolling in the tool-bar
12764 window, so don't do it. */
12765 w->desired_matrix->no_scrolling_p = true;
12766 w->must_be_updated_p = true;
12768 if (!NILP (Vauto_resize_tool_bars))
12770 bool change_height_p = true;
12772 /* If we couldn't display everything, change the tool-bar's
12773 height if there is room for more. */
12774 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12775 change_height_p = true;
12777 /* We subtract 1 because display_tool_bar_line advances the
12778 glyph_row pointer before returning to its caller. We want to
12779 examine the last glyph row produced by
12780 display_tool_bar_line. */
12781 row = it.glyph_row - 1;
12783 /* If there are blank lines at the end, except for a partially
12784 visible blank line at the end that is smaller than
12785 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12786 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12787 && row->height >= FRAME_LINE_HEIGHT (f))
12788 change_height_p = true;
12790 /* If row displays tool-bar items, but is partially visible,
12791 change the tool-bar's height. */
12792 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12793 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12794 change_height_p = true;
12796 /* Resize windows as needed by changing the `tool-bar-lines'
12797 frame parameter. */
12798 if (change_height_p)
12800 int nrows;
12801 int new_height = tool_bar_height (f, &nrows, true);
12803 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12804 && !f->minimize_tool_bar_window_p)
12805 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12806 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12807 f->minimize_tool_bar_window_p = false;
12809 if (change_height_p)
12811 x_change_tool_bar_height (f, new_height);
12812 frame_default_tool_bar_height = new_height;
12813 clear_glyph_matrix (w->desired_matrix);
12814 f->n_tool_bar_rows = nrows;
12815 f->fonts_changed = true;
12817 return true;
12822 f->minimize_tool_bar_window_p = false;
12823 return false;
12825 #endif /* USE_GTK || HAVE_NS */
12828 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12830 /* Get information about the tool-bar item which is displayed in GLYPH
12831 on frame F. Return in *PROP_IDX the index where tool-bar item
12832 properties start in F->tool_bar_items. Value is false if
12833 GLYPH doesn't display a tool-bar item. */
12835 static bool
12836 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12838 Lisp_Object prop;
12839 int charpos;
12841 /* This function can be called asynchronously, which means we must
12842 exclude any possibility that Fget_text_property signals an
12843 error. */
12844 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12845 charpos = max (0, charpos);
12847 /* Get the text property `menu-item' at pos. The value of that
12848 property is the start index of this item's properties in
12849 F->tool_bar_items. */
12850 prop = Fget_text_property (make_number (charpos),
12851 Qmenu_item, f->current_tool_bar_string);
12852 if (! INTEGERP (prop))
12853 return false;
12854 *prop_idx = XINT (prop);
12855 return true;
12859 /* Get information about the tool-bar item at position X/Y on frame F.
12860 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12861 the current matrix of the tool-bar window of F, or NULL if not
12862 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12863 item in F->tool_bar_items. Value is
12865 -1 if X/Y is not on a tool-bar item
12866 0 if X/Y is on the same item that was highlighted before.
12867 1 otherwise. */
12869 static int
12870 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12871 int *hpos, int *vpos, int *prop_idx)
12873 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12874 struct window *w = XWINDOW (f->tool_bar_window);
12875 int area;
12877 /* Find the glyph under X/Y. */
12878 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12879 if (*glyph == NULL)
12880 return -1;
12882 /* Get the start of this tool-bar item's properties in
12883 f->tool_bar_items. */
12884 if (!tool_bar_item_info (f, *glyph, prop_idx))
12885 return -1;
12887 /* Is mouse on the highlighted item? */
12888 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12889 && *vpos >= hlinfo->mouse_face_beg_row
12890 && *vpos <= hlinfo->mouse_face_end_row
12891 && (*vpos > hlinfo->mouse_face_beg_row
12892 || *hpos >= hlinfo->mouse_face_beg_col)
12893 && (*vpos < hlinfo->mouse_face_end_row
12894 || *hpos < hlinfo->mouse_face_end_col
12895 || hlinfo->mouse_face_past_end))
12896 return 0;
12898 return 1;
12902 /* EXPORT:
12903 Handle mouse button event on the tool-bar of frame F, at
12904 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12905 false for button release. MODIFIERS is event modifiers for button
12906 release. */
12908 void
12909 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12910 int modifiers)
12912 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12913 struct window *w = XWINDOW (f->tool_bar_window);
12914 int hpos, vpos, prop_idx;
12915 struct glyph *glyph;
12916 Lisp_Object enabled_p;
12917 int ts;
12919 /* If not on the highlighted tool-bar item, and mouse-highlight is
12920 non-nil, return. This is so we generate the tool-bar button
12921 click only when the mouse button is released on the same item as
12922 where it was pressed. However, when mouse-highlight is disabled,
12923 generate the click when the button is released regardless of the
12924 highlight, since tool-bar items are not highlighted in that
12925 case. */
12926 frame_to_window_pixel_xy (w, &x, &y);
12927 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12928 if (ts == -1
12929 || (ts != 0 && !NILP (Vmouse_highlight)))
12930 return;
12932 /* When mouse-highlight is off, generate the click for the item
12933 where the button was pressed, disregarding where it was
12934 released. */
12935 if (NILP (Vmouse_highlight) && !down_p)
12936 prop_idx = f->last_tool_bar_item;
12938 /* If item is disabled, do nothing. */
12939 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12940 if (NILP (enabled_p))
12941 return;
12943 if (down_p)
12945 /* Show item in pressed state. */
12946 if (!NILP (Vmouse_highlight))
12947 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12948 f->last_tool_bar_item = prop_idx;
12950 else
12952 Lisp_Object key, frame;
12953 struct input_event event;
12954 EVENT_INIT (event);
12956 /* Show item in released state. */
12957 if (!NILP (Vmouse_highlight))
12958 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12960 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12962 XSETFRAME (frame, f);
12963 event.kind = TOOL_BAR_EVENT;
12964 event.frame_or_window = frame;
12965 event.arg = frame;
12966 kbd_buffer_store_event (&event);
12968 event.kind = TOOL_BAR_EVENT;
12969 event.frame_or_window = frame;
12970 event.arg = key;
12971 event.modifiers = modifiers;
12972 kbd_buffer_store_event (&event);
12973 f->last_tool_bar_item = -1;
12978 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12979 tool-bar window-relative coordinates X/Y. Called from
12980 note_mouse_highlight. */
12982 static void
12983 note_tool_bar_highlight (struct frame *f, int x, int y)
12985 Lisp_Object window = f->tool_bar_window;
12986 struct window *w = XWINDOW (window);
12987 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12988 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12989 int hpos, vpos;
12990 struct glyph *glyph;
12991 struct glyph_row *row;
12992 int i;
12993 Lisp_Object enabled_p;
12994 int prop_idx;
12995 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12996 bool mouse_down_p;
12997 int rc;
12999 /* Function note_mouse_highlight is called with negative X/Y
13000 values when mouse moves outside of the frame. */
13001 if (x <= 0 || y <= 0)
13003 clear_mouse_face (hlinfo);
13004 return;
13007 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13008 if (rc < 0)
13010 /* Not on tool-bar item. */
13011 clear_mouse_face (hlinfo);
13012 return;
13014 else if (rc == 0)
13015 /* On same tool-bar item as before. */
13016 goto set_help_echo;
13018 clear_mouse_face (hlinfo);
13020 /* Mouse is down, but on different tool-bar item? */
13021 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13022 && f == dpyinfo->last_mouse_frame);
13024 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13025 return;
13027 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13029 /* If tool-bar item is not enabled, don't highlight it. */
13030 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13031 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13033 /* Compute the x-position of the glyph. In front and past the
13034 image is a space. We include this in the highlighted area. */
13035 row = MATRIX_ROW (w->current_matrix, vpos);
13036 for (i = x = 0; i < hpos; ++i)
13037 x += row->glyphs[TEXT_AREA][i].pixel_width;
13039 /* Record this as the current active region. */
13040 hlinfo->mouse_face_beg_col = hpos;
13041 hlinfo->mouse_face_beg_row = vpos;
13042 hlinfo->mouse_face_beg_x = x;
13043 hlinfo->mouse_face_past_end = false;
13045 hlinfo->mouse_face_end_col = hpos + 1;
13046 hlinfo->mouse_face_end_row = vpos;
13047 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13048 hlinfo->mouse_face_window = window;
13049 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13051 /* Display it as active. */
13052 show_mouse_face (hlinfo, draw);
13055 set_help_echo:
13057 /* Set help_echo_string to a help string to display for this tool-bar item.
13058 XTread_socket does the rest. */
13059 help_echo_object = help_echo_window = Qnil;
13060 help_echo_pos = -1;
13061 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13062 if (NILP (help_echo_string))
13063 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13066 #endif /* !USE_GTK && !HAVE_NS */
13068 #endif /* HAVE_WINDOW_SYSTEM */
13072 /************************************************************************
13073 Horizontal scrolling
13074 ************************************************************************/
13076 /* For all leaf windows in the window tree rooted at WINDOW, set their
13077 hscroll value so that PT is (i) visible in the window, and (ii) so
13078 that it is not within a certain margin at the window's left and
13079 right border. Value is true if any window's hscroll has been
13080 changed. */
13082 static bool
13083 hscroll_window_tree (Lisp_Object window)
13085 bool hscrolled_p = false;
13086 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13087 int hscroll_step_abs = 0;
13088 double hscroll_step_rel = 0;
13090 if (hscroll_relative_p)
13092 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13093 if (hscroll_step_rel < 0)
13095 hscroll_relative_p = false;
13096 hscroll_step_abs = 0;
13099 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13101 hscroll_step_abs = XINT (Vhscroll_step);
13102 if (hscroll_step_abs < 0)
13103 hscroll_step_abs = 0;
13105 else
13106 hscroll_step_abs = 0;
13108 while (WINDOWP (window))
13110 struct window *w = XWINDOW (window);
13112 if (WINDOWP (w->contents))
13113 hscrolled_p |= hscroll_window_tree (w->contents);
13114 else if (w->cursor.vpos >= 0)
13116 int h_margin;
13117 int text_area_width;
13118 struct glyph_row *cursor_row;
13119 struct glyph_row *bottom_row;
13121 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13122 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13123 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13124 else
13125 cursor_row = bottom_row - 1;
13127 if (!cursor_row->enabled_p)
13129 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13130 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13131 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13132 else
13133 cursor_row = bottom_row - 1;
13135 bool row_r2l_p = cursor_row->reversed_p;
13136 bool hscl = hscrolling_current_line_p (w);
13137 int x_offset = 0;
13138 /* When line numbers are displayed, we need to account for
13139 the horizontal space they consume. */
13140 if (!NILP (Vdisplay_line_numbers))
13142 struct glyph *g;
13143 if (!row_r2l_p)
13145 for (g = cursor_row->glyphs[TEXT_AREA];
13146 g < cursor_row->glyphs[TEXT_AREA]
13147 + cursor_row->used[TEXT_AREA];
13148 g++)
13150 if (!(NILP (g->object) && g->charpos < 0))
13151 break;
13152 x_offset += g->pixel_width;
13155 else
13157 for (g = cursor_row->glyphs[TEXT_AREA]
13158 + cursor_row->used[TEXT_AREA];
13159 g > cursor_row->glyphs[TEXT_AREA];
13160 g--)
13162 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13163 break;
13164 x_offset += (g - 1)->pixel_width;
13168 if (cursor_row->truncated_on_left_p)
13170 /* On TTY frames, don't count the left truncation glyph. */
13171 struct frame *f = XFRAME (WINDOW_FRAME (w));
13172 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13175 text_area_width = window_box_width (w, TEXT_AREA);
13177 /* Scroll when cursor is inside this scroll margin. */
13178 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13180 /* If the position of this window's point has explicitly
13181 changed, no more suspend auto hscrolling. */
13182 if (w->suspend_auto_hscroll
13183 && NILP (Fequal (Fwindow_point (window),
13184 Fwindow_old_point (window))))
13186 w->suspend_auto_hscroll = false;
13187 /* When hscrolling just the current line, and the rest
13188 of lines were temporarily hscrolled, but no longer
13189 are, force thorough redisplay of this window, to show
13190 the effect of disabling hscroll suspension immediately. */
13191 if (w->min_hscroll == 0 && w->hscroll > 0
13192 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
13193 Qcurrent_line))
13194 SET_FRAME_GARBAGED (XFRAME (w->frame));
13197 /* Remember window point. */
13198 Fset_marker (w->old_pointm,
13199 ((w == XWINDOW (selected_window))
13200 ? make_number (BUF_PT (XBUFFER (w->contents)))
13201 : Fmarker_position (w->pointm)),
13202 w->contents);
13204 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13205 && !w->suspend_auto_hscroll
13206 /* In some pathological cases, like restoring a window
13207 configuration into a frame that is much smaller than
13208 the one from which the configuration was saved, we
13209 get glyph rows whose start and end have zero buffer
13210 positions, which we cannot handle below. Just skip
13211 such windows. */
13212 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13213 /* For left-to-right rows, hscroll when cursor is either
13214 (i) inside the right hscroll margin, or (ii) if it is
13215 inside the left margin and the window is already
13216 hscrolled. */
13217 && ((!row_r2l_p
13218 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13219 || (cursor_row->enabled_p
13220 && cursor_row->truncated_on_right_p
13221 && (w->cursor.x >= text_area_width - h_margin))))
13222 /* For right-to-left rows, the logic is similar,
13223 except that rules for scrolling to left and right
13224 are reversed. E.g., if cursor.x <= h_margin, we
13225 need to hscroll "to the right" unconditionally,
13226 and that will scroll the screen to the left so as
13227 to reveal the next portion of the row. */
13228 || (row_r2l_p
13229 && ((cursor_row->enabled_p
13230 /* FIXME: It is confusing to set the
13231 truncated_on_right_p flag when R2L rows
13232 are actually truncated on the left. */
13233 && cursor_row->truncated_on_right_p
13234 && w->cursor.x <= h_margin)
13235 || (w->hscroll
13236 && (w->cursor.x >= (text_area_width - h_margin
13237 - x_offset)))))
13238 /* This last condition is needed when moving
13239 vertically from an hscrolled line to a short line
13240 that doesn't need to be hscrolled. If we omit
13241 this condition, the line from which we move will
13242 remain hscrolled. */
13243 || (hscl
13244 && w->hscroll != w->min_hscroll
13245 && !cursor_row->truncated_on_left_p)))
13247 struct it it;
13248 ptrdiff_t hscroll;
13249 struct buffer *saved_current_buffer;
13250 ptrdiff_t pt;
13251 int wanted_x;
13253 /* Find point in a display of infinite width. */
13254 saved_current_buffer = current_buffer;
13255 current_buffer = XBUFFER (w->contents);
13257 if (w == XWINDOW (selected_window))
13258 pt = PT;
13259 else
13260 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13262 /* Move iterator to pt starting at cursor_row->start in
13263 a line with infinite width. */
13264 init_to_row_start (&it, w, cursor_row);
13265 if (hscl)
13266 it.first_visible_x = window_hscroll_limited (w, it.f)
13267 * FRAME_COLUMN_WIDTH (it.f);
13268 it.last_visible_x = DISP_INFINITY;
13269 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13270 /* If the line ends in an overlay string with a newline,
13271 we might infloop, because displaying the window will
13272 want to put the cursor after the overlay, i.e. at X
13273 coordinate of zero on the next screen line. So we
13274 use the buffer position prior to the overlay string
13275 instead. */
13276 if (it.method == GET_FROM_STRING && pt > 1)
13278 init_to_row_start (&it, w, cursor_row);
13279 if (hscl)
13280 it.first_visible_x = (window_hscroll_limited (w, it.f)
13281 * FRAME_COLUMN_WIDTH (it.f));
13282 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13284 current_buffer = saved_current_buffer;
13286 /* Position cursor in window. */
13287 if (!hscroll_relative_p && hscroll_step_abs == 0)
13288 hscroll = max (0, (it.current_x
13289 - (ITERATOR_AT_END_OF_LINE_P (&it)
13290 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13291 : (text_area_width / 2))))
13292 / FRAME_COLUMN_WIDTH (it.f);
13293 else if ((!row_r2l_p
13294 && w->cursor.x >= text_area_width - h_margin)
13295 || (row_r2l_p && w->cursor.x <= h_margin))
13297 if (hscroll_relative_p)
13298 wanted_x = text_area_width * (1 - hscroll_step_rel)
13299 - h_margin;
13300 else
13301 wanted_x = text_area_width
13302 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13303 - h_margin;
13304 hscroll
13305 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13307 else
13309 if (hscroll_relative_p)
13310 wanted_x = text_area_width * hscroll_step_rel
13311 + h_margin;
13312 else
13313 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13314 + h_margin;
13315 hscroll
13316 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13318 hscroll = max (hscroll, w->min_hscroll);
13320 /* Don't prevent redisplay optimizations if hscroll
13321 hasn't changed, as it will unnecessarily slow down
13322 redisplay. */
13323 if (w->hscroll != hscroll
13324 /* When hscrolling only the current line, we need to
13325 report hscroll even if its value is equal to the
13326 previous one, because the new line might need a
13327 different value. */
13328 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13330 struct buffer *b = XBUFFER (w->contents);
13331 b->prevent_redisplay_optimizations_p = true;
13332 w->hscroll = hscroll;
13333 hscrolled_p = true;
13338 window = w->next;
13341 /* Value is true if hscroll of any leaf window has been changed. */
13342 return hscrolled_p;
13346 /* Set hscroll so that cursor is visible and not inside horizontal
13347 scroll margins for all windows in the tree rooted at WINDOW. See
13348 also hscroll_window_tree above. Value is true if any window's
13349 hscroll has been changed. If it has, desired matrices on the frame
13350 of WINDOW are cleared. */
13352 static bool
13353 hscroll_windows (Lisp_Object window)
13355 bool hscrolled_p = hscroll_window_tree (window);
13356 if (hscrolled_p)
13357 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13358 return hscrolled_p;
13363 /************************************************************************
13364 Redisplay
13365 ************************************************************************/
13367 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13368 This is sometimes handy to have in a debugger session. */
13370 #ifdef GLYPH_DEBUG
13372 /* First and last unchanged row for try_window_id. */
13374 static int debug_first_unchanged_at_end_vpos;
13375 static int debug_last_unchanged_at_beg_vpos;
13377 /* Delta vpos and y. */
13379 static int debug_dvpos, debug_dy;
13381 /* Delta in characters and bytes for try_window_id. */
13383 static ptrdiff_t debug_delta, debug_delta_bytes;
13385 /* Values of window_end_pos and window_end_vpos at the end of
13386 try_window_id. */
13388 static ptrdiff_t debug_end_vpos;
13390 /* Append a string to W->desired_matrix->method. FMT is a printf
13391 format string. If trace_redisplay_p is true also printf the
13392 resulting string to stderr. */
13394 static void debug_method_add (struct window *, char const *, ...)
13395 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13397 static void
13398 debug_method_add (struct window *w, char const *fmt, ...)
13400 void *ptr = w;
13401 char *method = w->desired_matrix->method;
13402 int len = strlen (method);
13403 int size = sizeof w->desired_matrix->method;
13404 int remaining = size - len - 1;
13405 va_list ap;
13407 if (len && remaining)
13409 method[len] = '|';
13410 --remaining, ++len;
13413 va_start (ap, fmt);
13414 vsnprintf (method + len, remaining + 1, fmt, ap);
13415 va_end (ap);
13417 if (trace_redisplay_p)
13418 fprintf (stderr, "%p (%s): %s\n",
13419 ptr,
13420 ((BUFFERP (w->contents)
13421 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13422 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13423 : "no buffer"),
13424 method + len);
13427 #endif /* GLYPH_DEBUG */
13430 /* Value is true if all changes in window W, which displays
13431 current_buffer, are in the text between START and END. START is a
13432 buffer position, END is given as a distance from Z. Used in
13433 redisplay_internal for display optimization. */
13435 static bool
13436 text_outside_line_unchanged_p (struct window *w,
13437 ptrdiff_t start, ptrdiff_t end)
13439 bool unchanged_p = true;
13441 /* If text or overlays have changed, see where. */
13442 if (window_outdated (w))
13444 /* Gap in the line? */
13445 if (GPT < start || Z - GPT < end)
13446 unchanged_p = false;
13448 /* Changes start in front of the line, or end after it? */
13449 if (unchanged_p
13450 && (BEG_UNCHANGED < start - 1
13451 || END_UNCHANGED < end))
13452 unchanged_p = false;
13454 /* If selective display, can't optimize if changes start at the
13455 beginning of the line. */
13456 if (unchanged_p
13457 && INTEGERP (BVAR (current_buffer, selective_display))
13458 && XINT (BVAR (current_buffer, selective_display)) > 0
13459 && (BEG_UNCHANGED < start || GPT <= start))
13460 unchanged_p = false;
13462 /* If there are overlays at the start or end of the line, these
13463 may have overlay strings with newlines in them. A change at
13464 START, for instance, may actually concern the display of such
13465 overlay strings as well, and they are displayed on different
13466 lines. So, quickly rule out this case. (For the future, it
13467 might be desirable to implement something more telling than
13468 just BEG/END_UNCHANGED.) */
13469 if (unchanged_p)
13471 if (BEG + BEG_UNCHANGED == start
13472 && overlay_touches_p (start))
13473 unchanged_p = false;
13474 if (END_UNCHANGED == end
13475 && overlay_touches_p (Z - end))
13476 unchanged_p = false;
13479 /* Under bidi reordering, adding or deleting a character in the
13480 beginning of a paragraph, before the first strong directional
13481 character, can change the base direction of the paragraph (unless
13482 the buffer specifies a fixed paragraph direction), which will
13483 require redisplaying the whole paragraph. It might be worthwhile
13484 to find the paragraph limits and widen the range of redisplayed
13485 lines to that, but for now just give up this optimization. */
13486 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13487 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13488 unchanged_p = false;
13491 return unchanged_p;
13495 /* Do a frame update, taking possible shortcuts into account. This is
13496 the main external entry point for redisplay.
13498 If the last redisplay displayed an echo area message and that message
13499 is no longer requested, we clear the echo area or bring back the
13500 mini-buffer if that is in use. */
13502 void
13503 redisplay (void)
13505 redisplay_internal ();
13509 static Lisp_Object
13510 overlay_arrow_string_or_property (Lisp_Object var)
13512 Lisp_Object val;
13514 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13515 return val;
13517 return Voverlay_arrow_string;
13520 /* Return true if there are any overlay-arrows in current_buffer. */
13521 static bool
13522 overlay_arrow_in_current_buffer_p (void)
13524 Lisp_Object vlist;
13526 for (vlist = Voverlay_arrow_variable_list;
13527 CONSP (vlist);
13528 vlist = XCDR (vlist))
13530 Lisp_Object var = XCAR (vlist);
13531 Lisp_Object val;
13533 if (!SYMBOLP (var))
13534 continue;
13535 val = find_symbol_value (var);
13536 if (MARKERP (val)
13537 && current_buffer == XMARKER (val)->buffer)
13538 return true;
13540 return false;
13544 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13545 has changed.
13546 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13547 buffers that are affected. */
13549 static bool
13550 overlay_arrows_changed_p (bool set_redisplay)
13552 Lisp_Object vlist;
13553 bool changed = false;
13555 for (vlist = Voverlay_arrow_variable_list;
13556 CONSP (vlist);
13557 vlist = XCDR (vlist))
13559 Lisp_Object var = XCAR (vlist);
13560 Lisp_Object val, pstr;
13562 if (!SYMBOLP (var))
13563 continue;
13564 val = find_symbol_value (var);
13565 if (!MARKERP (val))
13566 continue;
13567 if (! EQ (COERCE_MARKER (val),
13568 /* FIXME: Don't we have a problem, using such a global
13569 * "last-position" if the variable is buffer-local? */
13570 Fget (var, Qlast_arrow_position))
13571 || ! (pstr = overlay_arrow_string_or_property (var),
13572 EQ (pstr, Fget (var, Qlast_arrow_string))))
13574 struct buffer *buf = XMARKER (val)->buffer;
13576 if (set_redisplay)
13578 if (buf)
13579 bset_redisplay (buf);
13580 changed = true;
13582 else
13583 return true;
13586 return changed;
13589 /* Mark overlay arrows to be updated on next redisplay. */
13591 static void
13592 update_overlay_arrows (int up_to_date)
13594 Lisp_Object vlist;
13596 for (vlist = Voverlay_arrow_variable_list;
13597 CONSP (vlist);
13598 vlist = XCDR (vlist))
13600 Lisp_Object var = XCAR (vlist);
13602 if (!SYMBOLP (var))
13603 continue;
13605 if (up_to_date > 0)
13607 Lisp_Object val = find_symbol_value (var);
13608 if (!MARKERP (val))
13609 continue;
13610 Fput (var, Qlast_arrow_position,
13611 COERCE_MARKER (val));
13612 Fput (var, Qlast_arrow_string,
13613 overlay_arrow_string_or_property (var));
13615 else if (up_to_date < 0
13616 || !NILP (Fget (var, Qlast_arrow_position)))
13618 Fput (var, Qlast_arrow_position, Qt);
13619 Fput (var, Qlast_arrow_string, Qt);
13625 /* Return overlay arrow string to display at row.
13626 Return integer (bitmap number) for arrow bitmap in left fringe.
13627 Return nil if no overlay arrow. */
13629 static Lisp_Object
13630 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13632 Lisp_Object vlist;
13634 for (vlist = Voverlay_arrow_variable_list;
13635 CONSP (vlist);
13636 vlist = XCDR (vlist))
13638 Lisp_Object var = XCAR (vlist);
13639 Lisp_Object val;
13641 if (!SYMBOLP (var))
13642 continue;
13644 val = find_symbol_value (var);
13646 if (MARKERP (val)
13647 && current_buffer == XMARKER (val)->buffer
13648 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13650 if (FRAME_WINDOW_P (it->f)
13651 /* FIXME: if ROW->reversed_p is set, this should test
13652 the right fringe, not the left one. */
13653 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13655 #ifdef HAVE_WINDOW_SYSTEM
13656 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13658 int fringe_bitmap = lookup_fringe_bitmap (val);
13659 if (fringe_bitmap != 0)
13660 return make_number (fringe_bitmap);
13662 #endif
13663 return make_number (-1); /* Use default arrow bitmap. */
13665 return overlay_arrow_string_or_property (var);
13669 return Qnil;
13672 /* Return true if point moved out of or into a composition. Otherwise
13673 return false. PREV_BUF and PREV_PT are the last point buffer and
13674 position. BUF and PT are the current point buffer and position. */
13676 static bool
13677 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13678 struct buffer *buf, ptrdiff_t pt)
13680 ptrdiff_t start, end;
13681 Lisp_Object prop;
13682 Lisp_Object buffer;
13684 XSETBUFFER (buffer, buf);
13685 /* Check a composition at the last point if point moved within the
13686 same buffer. */
13687 if (prev_buf == buf)
13689 if (prev_pt == pt)
13690 /* Point didn't move. */
13691 return false;
13693 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13694 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13695 && composition_valid_p (start, end, prop)
13696 && start < prev_pt && end > prev_pt)
13697 /* The last point was within the composition. Return true iff
13698 point moved out of the composition. */
13699 return (pt <= start || pt >= end);
13702 /* Check a composition at the current point. */
13703 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13704 && find_composition (pt, -1, &start, &end, &prop, buffer)
13705 && composition_valid_p (start, end, prop)
13706 && start < pt && end > pt);
13709 /* Reconsider the clip changes of buffer which is displayed in W. */
13711 static void
13712 reconsider_clip_changes (struct window *w)
13714 struct buffer *b = XBUFFER (w->contents);
13716 if (b->clip_changed
13717 && w->window_end_valid
13718 && w->current_matrix->buffer == b
13719 && w->current_matrix->zv == BUF_ZV (b)
13720 && w->current_matrix->begv == BUF_BEGV (b))
13721 b->clip_changed = false;
13723 /* If display wasn't paused, and W is not a tool bar window, see if
13724 point has been moved into or out of a composition. In that case,
13725 set b->clip_changed to force updating the screen. If
13726 b->clip_changed has already been set, skip this check. */
13727 if (!b->clip_changed && w->window_end_valid)
13729 ptrdiff_t pt = (w == XWINDOW (selected_window)
13730 ? PT : marker_position (w->pointm));
13732 if ((w->current_matrix->buffer != b || pt != w->last_point)
13733 && check_point_in_composition (w->current_matrix->buffer,
13734 w->last_point, b, pt))
13735 b->clip_changed = true;
13739 static void
13740 propagate_buffer_redisplay (void)
13741 { /* Resetting b->text->redisplay is problematic!
13742 We can't just reset it in the case that some window that displays
13743 it has not been redisplayed; and such a window can stay
13744 unredisplayed for a long time if it's currently invisible.
13745 But we do want to reset it at the end of redisplay otherwise
13746 its displayed windows will keep being redisplayed over and over
13747 again.
13748 So we copy all b->text->redisplay flags up to their windows here,
13749 such that mark_window_display_accurate can safely reset
13750 b->text->redisplay. */
13751 Lisp_Object ws = window_list ();
13752 for (; CONSP (ws); ws = XCDR (ws))
13754 struct window *thisw = XWINDOW (XCAR (ws));
13755 struct buffer *thisb = XBUFFER (thisw->contents);
13756 if (thisb->text->redisplay)
13757 thisw->redisplay = true;
13761 #define STOP_POLLING \
13762 do { if (! polling_stopped_here) stop_polling (); \
13763 polling_stopped_here = true; } while (false)
13765 #define RESUME_POLLING \
13766 do { if (polling_stopped_here) start_polling (); \
13767 polling_stopped_here = false; } while (false)
13770 /* Perhaps in the future avoid recentering windows if it
13771 is not necessary; currently that causes some problems. */
13773 static void
13774 redisplay_internal (void)
13776 struct window *w = XWINDOW (selected_window);
13777 struct window *sw;
13778 struct frame *fr;
13779 bool pending;
13780 bool must_finish = false, match_p;
13781 struct text_pos tlbufpos, tlendpos;
13782 int number_of_visible_frames;
13783 ptrdiff_t count;
13784 struct frame *sf;
13785 bool polling_stopped_here = false;
13786 Lisp_Object tail, frame;
13788 /* Set a limit to the number of retries we perform due to horizontal
13789 scrolling, this avoids getting stuck in an uninterruptible
13790 infinite loop (Bug #24633). */
13791 enum { MAX_HSCROLL_RETRIES = 16 };
13792 int hscroll_retries = 0;
13794 /* Limit the number of retries for when frame(s) become garbaged as
13795 result of redisplaying them. Some packages set various redisplay
13796 hooks, such as window-scroll-functions, to run Lisp that always
13797 calls APIs which cause the frame's garbaged flag to become set,
13798 so we loop indefinitely. */
13799 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13800 int garbaged_frame_retries = 0;
13802 /* True means redisplay has to consider all windows on all
13803 frames. False, only selected_window is considered. */
13804 bool consider_all_windows_p;
13806 /* True means redisplay has to redisplay the miniwindow. */
13807 bool update_miniwindow_p = false;
13809 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13811 /* No redisplay if running in batch mode or frame is not yet fully
13812 initialized, or redisplay is explicitly turned off by setting
13813 Vinhibit_redisplay. */
13814 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13815 || !NILP (Vinhibit_redisplay))
13816 return;
13818 /* Don't examine these until after testing Vinhibit_redisplay.
13819 When Emacs is shutting down, perhaps because its connection to
13820 X has dropped, we should not look at them at all. */
13821 fr = XFRAME (w->frame);
13822 sf = SELECTED_FRAME ();
13824 if (!fr->glyphs_initialized_p)
13825 return;
13827 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13828 if (popup_activated ())
13829 return;
13830 #endif
13832 /* I don't think this happens but let's be paranoid. */
13833 if (redisplaying_p)
13834 return;
13836 /* Record a function that clears redisplaying_p
13837 when we leave this function. */
13838 count = SPECPDL_INDEX ();
13839 record_unwind_protect_void (unwind_redisplay);
13840 redisplaying_p = true;
13841 block_buffer_flips ();
13842 specbind (Qinhibit_free_realized_faces, Qnil);
13844 /* Record this function, so it appears on the profiler's backtraces. */
13845 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13847 FOR_EACH_FRAME (tail, frame)
13848 XFRAME (frame)->already_hscrolled_p = false;
13850 retry:
13851 /* Remember the currently selected window. */
13852 sw = w;
13854 pending = false;
13855 forget_escape_and_glyphless_faces ();
13857 inhibit_free_realized_faces = false;
13859 /* If face_change, init_iterator will free all realized faces, which
13860 includes the faces referenced from current matrices. So, we
13861 can't reuse current matrices in this case. */
13862 if (face_change)
13863 windows_or_buffers_changed = 47;
13865 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13866 && FRAME_TTY (sf)->previous_frame != sf)
13868 /* Since frames on a single ASCII terminal share the same
13869 display area, displaying a different frame means redisplay
13870 the whole thing. */
13871 SET_FRAME_GARBAGED (sf);
13872 #ifndef DOS_NT
13873 set_tty_color_mode (FRAME_TTY (sf), sf);
13874 #endif
13875 FRAME_TTY (sf)->previous_frame = sf;
13878 /* Set the visible flags for all frames. Do this before checking for
13879 resized or garbaged frames; they want to know if their frames are
13880 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13881 number_of_visible_frames = 0;
13883 FOR_EACH_FRAME (tail, frame)
13885 struct frame *f = XFRAME (frame);
13887 if (FRAME_VISIBLE_P (f))
13889 ++number_of_visible_frames;
13890 /* Adjust matrices for visible frames only. */
13891 if (f->fonts_changed)
13893 adjust_frame_glyphs (f);
13894 /* Disable all redisplay optimizations for this frame.
13895 This is because adjust_frame_glyphs resets the
13896 enabled_p flag for all glyph rows of all windows, so
13897 many optimizations will fail anyway, and some might
13898 fail to test that flag and do bogus things as
13899 result. */
13900 SET_FRAME_GARBAGED (f);
13901 f->fonts_changed = false;
13903 /* If cursor type has been changed on the frame
13904 other than selected, consider all frames. */
13905 if (f != sf && f->cursor_type_changed)
13906 fset_redisplay (f);
13908 clear_desired_matrices (f);
13911 /* Notice any pending interrupt request to change frame size. */
13912 do_pending_window_change (true);
13914 /* do_pending_window_change could change the selected_window due to
13915 frame resizing which makes the selected window too small. */
13916 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13917 sw = w;
13919 /* Clear frames marked as garbaged. */
13920 clear_garbaged_frames ();
13922 /* Build menubar and tool-bar items. */
13923 if (NILP (Vmemory_full))
13924 prepare_menu_bars ();
13926 reconsider_clip_changes (w);
13928 /* In most cases selected window displays current buffer. */
13929 match_p = XBUFFER (w->contents) == current_buffer;
13930 if (match_p)
13932 /* Detect case that we need to write or remove a star in the mode line. */
13933 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13934 w->update_mode_line = true;
13936 if (mode_line_update_needed (w))
13937 w->update_mode_line = true;
13939 /* If reconsider_clip_changes above decided that the narrowing
13940 in the current buffer changed, make sure all other windows
13941 showing that buffer will be redisplayed. */
13942 if (current_buffer->clip_changed)
13943 bset_update_mode_line (current_buffer);
13946 /* Normally the message* functions will have already displayed and
13947 updated the echo area, but the frame may have been trashed, or
13948 the update may have been preempted, so display the echo area
13949 again here. Checking message_cleared_p captures the case that
13950 the echo area should be cleared. */
13951 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13952 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13953 || (message_cleared_p
13954 && minibuf_level == 0
13955 /* If the mini-window is currently selected, this means the
13956 echo-area doesn't show through. */
13957 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13959 echo_area_display (false);
13961 /* If echo_area_display resizes the mini-window, the redisplay and
13962 window_sizes_changed flags of the selected frame are set, but
13963 it's too late for the hooks in window-size-change-functions,
13964 which have been examined already in prepare_menu_bars. So in
13965 that case we call the hooks here only for the selected frame. */
13966 if (sf->redisplay)
13968 ptrdiff_t count1 = SPECPDL_INDEX ();
13970 record_unwind_save_match_data ();
13971 run_window_size_change_functions (selected_frame);
13972 unbind_to (count1, Qnil);
13975 if (message_cleared_p)
13976 update_miniwindow_p = true;
13978 must_finish = true;
13980 /* If we don't display the current message, don't clear the
13981 message_cleared_p flag, because, if we did, we wouldn't clear
13982 the echo area in the next redisplay which doesn't preserve
13983 the echo area. */
13984 if (!display_last_displayed_message_p)
13985 message_cleared_p = false;
13987 else if (EQ (selected_window, minibuf_window)
13988 && (current_buffer->clip_changed || window_outdated (w))
13989 && resize_mini_window (w, false))
13991 if (sf->redisplay)
13993 ptrdiff_t count1 = SPECPDL_INDEX ();
13995 record_unwind_save_match_data ();
13996 run_window_size_change_functions (selected_frame);
13997 unbind_to (count1, Qnil);
14000 /* Resized active mini-window to fit the size of what it is
14001 showing if its contents might have changed. */
14002 must_finish = true;
14004 /* If window configuration was changed, frames may have been
14005 marked garbaged. Clear them or we will experience
14006 surprises wrt scrolling. */
14007 clear_garbaged_frames ();
14010 if (windows_or_buffers_changed && !update_mode_lines)
14011 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14012 only the windows's contents needs to be refreshed, or whether the
14013 mode-lines also need a refresh. */
14014 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14015 ? REDISPLAY_SOME : 32);
14017 /* If specs for an arrow have changed, do thorough redisplay
14018 to ensure we remove any arrow that should no longer exist. */
14019 /* Apparently, this is the only case where we update other windows,
14020 without updating other mode-lines. */
14021 overlay_arrows_changed_p (true);
14023 consider_all_windows_p = (update_mode_lines
14024 || windows_or_buffers_changed);
14026 #define AINC(a,i) \
14028 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14029 if (INTEGERP (entry)) \
14030 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14033 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14034 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14036 /* Optimize the case that only the line containing the cursor in the
14037 selected window has changed. Variables starting with this_ are
14038 set in display_line and record information about the line
14039 containing the cursor. */
14040 tlbufpos = this_line_start_pos;
14041 tlendpos = this_line_end_pos;
14042 if (!consider_all_windows_p
14043 && CHARPOS (tlbufpos) > 0
14044 && !w->update_mode_line
14045 && !current_buffer->clip_changed
14046 && !current_buffer->prevent_redisplay_optimizations_p
14047 && FRAME_VISIBLE_P (XFRAME (w->frame))
14048 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14049 && !XFRAME (w->frame)->cursor_type_changed
14050 && !XFRAME (w->frame)->face_change
14051 /* Make sure recorded data applies to current buffer, etc. */
14052 && this_line_buffer == current_buffer
14053 && match_p
14054 && !w->force_start
14055 && !w->optional_new_start
14056 /* Point must be on the line that we have info recorded about. */
14057 && PT >= CHARPOS (tlbufpos)
14058 && PT <= Z - CHARPOS (tlendpos)
14059 /* All text outside that line, including its final newline,
14060 must be unchanged. */
14061 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14062 CHARPOS (tlendpos)))
14064 if (CHARPOS (tlbufpos) > BEGV
14065 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14066 && (CHARPOS (tlbufpos) == ZV
14067 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14068 /* Former continuation line has disappeared by becoming empty. */
14069 goto cancel;
14070 else if (window_outdated (w) || MINI_WINDOW_P (w))
14072 /* We have to handle the case of continuation around a
14073 wide-column character (see the comment in indent.c around
14074 line 1340).
14076 For instance, in the following case:
14078 -------- Insert --------
14079 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14080 J_I_ ==> J_I_ `^^' are cursors.
14081 ^^ ^^
14082 -------- --------
14084 As we have to redraw the line above, we cannot use this
14085 optimization. */
14087 struct it it;
14088 int line_height_before = this_line_pixel_height;
14090 /* Note that start_display will handle the case that the
14091 line starting at tlbufpos is a continuation line. */
14092 start_display (&it, w, tlbufpos);
14094 /* Implementation note: It this still necessary? */
14095 if (it.current_x != this_line_start_x)
14096 goto cancel;
14098 TRACE ((stderr, "trying display optimization 1\n"));
14099 w->cursor.vpos = -1;
14100 overlay_arrow_seen = false;
14101 it.vpos = this_line_vpos;
14102 it.current_y = this_line_y;
14103 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14104 display_line (&it, -1);
14106 /* If line contains point, is not continued,
14107 and ends at same distance from eob as before, we win. */
14108 if (w->cursor.vpos >= 0
14109 /* Line is not continued, otherwise this_line_start_pos
14110 would have been set to 0 in display_line. */
14111 && CHARPOS (this_line_start_pos)
14112 /* Line ends as before. */
14113 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14114 /* Line has same height as before. Otherwise other lines
14115 would have to be shifted up or down. */
14116 && this_line_pixel_height == line_height_before)
14118 /* If this is not the window's last line, we must adjust
14119 the charstarts of the lines below. */
14120 if (it.current_y < it.last_visible_y)
14122 struct glyph_row *row
14123 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14124 ptrdiff_t delta, delta_bytes;
14126 /* We used to distinguish between two cases here,
14127 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14128 when the line ends in a newline or the end of the
14129 buffer's accessible portion. But both cases did
14130 the same, so they were collapsed. */
14131 delta = (Z
14132 - CHARPOS (tlendpos)
14133 - MATRIX_ROW_START_CHARPOS (row));
14134 delta_bytes = (Z_BYTE
14135 - BYTEPOS (tlendpos)
14136 - MATRIX_ROW_START_BYTEPOS (row));
14138 increment_matrix_positions (w->current_matrix,
14139 this_line_vpos + 1,
14140 w->current_matrix->nrows,
14141 delta, delta_bytes);
14144 /* If this row displays text now but previously didn't,
14145 or vice versa, w->window_end_vpos may have to be
14146 adjusted. */
14147 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14149 if (w->window_end_vpos < this_line_vpos)
14150 w->window_end_vpos = this_line_vpos;
14152 else if (w->window_end_vpos == this_line_vpos
14153 && this_line_vpos > 0)
14154 w->window_end_vpos = this_line_vpos - 1;
14155 w->window_end_valid = false;
14157 /* Update hint: No need to try to scroll in update_window. */
14158 w->desired_matrix->no_scrolling_p = true;
14160 #ifdef GLYPH_DEBUG
14161 *w->desired_matrix->method = 0;
14162 debug_method_add (w, "optimization 1");
14163 #endif
14164 #ifdef HAVE_WINDOW_SYSTEM
14165 update_window_fringes (w, false);
14166 #endif
14167 goto update;
14169 else
14170 goto cancel;
14172 else if (/* Cursor position hasn't changed. */
14173 PT == w->last_point
14174 /* Make sure the cursor was last displayed
14175 in this window. Otherwise we have to reposition it. */
14177 /* PXW: Must be converted to pixels, probably. */
14178 && 0 <= w->cursor.vpos
14179 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14181 if (!must_finish)
14183 do_pending_window_change (true);
14184 /* If selected_window changed, redisplay again. */
14185 if (WINDOWP (selected_window)
14186 && (w = XWINDOW (selected_window)) != sw)
14187 goto retry;
14189 /* We used to always goto end_of_redisplay here, but this
14190 isn't enough if we have a blinking cursor. */
14191 if (w->cursor_off_p == w->last_cursor_off_p)
14192 goto end_of_redisplay;
14194 goto update;
14196 /* If highlighting the region, or if the cursor is in the echo area,
14197 then we can't just move the cursor. */
14198 else if (NILP (Vshow_trailing_whitespace)
14199 && !cursor_in_echo_area)
14201 struct it it;
14202 struct glyph_row *row;
14204 /* Skip from tlbufpos to PT and see where it is. Note that
14205 PT may be in invisible text. If so, we will end at the
14206 next visible position. */
14207 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14208 NULL, DEFAULT_FACE_ID);
14209 it.current_x = this_line_start_x;
14210 it.current_y = this_line_y;
14211 it.vpos = this_line_vpos;
14213 /* The call to move_it_to stops in front of PT, but
14214 moves over before-strings. */
14215 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14217 if (it.vpos == this_line_vpos
14218 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14219 row->enabled_p))
14221 eassert (this_line_vpos == it.vpos);
14222 eassert (this_line_y == it.current_y);
14223 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14224 if (cursor_row_fully_visible_p (w, false, true))
14226 #ifdef GLYPH_DEBUG
14227 *w->desired_matrix->method = 0;
14228 debug_method_add (w, "optimization 3");
14229 #endif
14230 goto update;
14232 else
14233 goto cancel;
14235 else
14236 goto cancel;
14239 cancel:
14240 /* Text changed drastically or point moved off of line. */
14241 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14244 CHARPOS (this_line_start_pos) = 0;
14245 ++clear_face_cache_count;
14246 #ifdef HAVE_WINDOW_SYSTEM
14247 ++clear_image_cache_count;
14248 #endif
14250 /* Build desired matrices, and update the display. If
14251 consider_all_windows_p, do it for all windows on all frames that
14252 require redisplay, as specified by their 'redisplay' flag.
14253 Otherwise do it for selected_window, only. */
14255 if (consider_all_windows_p)
14257 FOR_EACH_FRAME (tail, frame)
14258 XFRAME (frame)->updated_p = false;
14260 propagate_buffer_redisplay ();
14262 FOR_EACH_FRAME (tail, frame)
14264 struct frame *f = XFRAME (frame);
14266 /* We don't have to do anything for unselected terminal
14267 frames. */
14268 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14269 && !EQ (FRAME_TTY (f)->top_frame, frame))
14270 continue;
14272 retry_frame:
14273 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14275 bool gcscrollbars
14276 /* Only GC scrollbars when we redisplay the whole frame. */
14277 = f->redisplay || !REDISPLAY_SOME_P ();
14278 bool f_redisplay_flag = f->redisplay;
14279 /* Mark all the scroll bars to be removed; we'll redeem
14280 the ones we want when we redisplay their windows. */
14281 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14282 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14284 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14285 redisplay_windows (FRAME_ROOT_WINDOW (f));
14286 /* Remember that the invisible frames need to be redisplayed next
14287 time they're visible. */
14288 else if (!REDISPLAY_SOME_P ())
14289 f->redisplay = true;
14291 /* The X error handler may have deleted that frame. */
14292 if (!FRAME_LIVE_P (f))
14293 continue;
14295 /* Any scroll bars which redisplay_windows should have
14296 nuked should now go away. */
14297 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14298 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14300 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14302 /* If fonts changed on visible frame, display again. */
14303 if (f->fonts_changed)
14305 adjust_frame_glyphs (f);
14306 /* Disable all redisplay optimizations for this
14307 frame. For the reasons, see the comment near
14308 the previous call to adjust_frame_glyphs above. */
14309 SET_FRAME_GARBAGED (f);
14310 f->fonts_changed = false;
14311 goto retry_frame;
14314 /* See if we have to hscroll. */
14315 if (!f->already_hscrolled_p)
14317 f->already_hscrolled_p = true;
14318 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14319 && hscroll_windows (f->root_window))
14321 hscroll_retries++;
14322 goto retry_frame;
14326 /* If the frame's redisplay flag was not set before
14327 we went about redisplaying its windows, but it is
14328 set now, that means we employed some redisplay
14329 optimizations inside redisplay_windows, and
14330 bypassed producing some screen lines. But if
14331 f->redisplay is now set, it might mean the old
14332 faces are no longer valid (e.g., if redisplaying
14333 some window called some Lisp which defined a new
14334 face or redefined an existing face), so trying to
14335 use them in update_frame will segfault.
14336 Therefore, we must redisplay this frame. */
14337 if (!f_redisplay_flag && f->redisplay)
14338 goto retry_frame;
14339 /* In some case (e.g., window resize), we notice
14340 only during window updating that the window
14341 content changed unpredictably (e.g., a GTK
14342 scrollbar moved, or some Lisp hook that winds up
14343 calling adjust_frame_glyphs) and that our
14344 previous estimation of the frame content was
14345 garbage. We have to start over. These cases
14346 should be rare, so going all the way back to the
14347 top of redisplay should be good enough. */
14348 if (FRAME_GARBAGED_P (f)
14349 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14350 goto retry;
14352 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14353 x_clear_under_internal_border (f);
14354 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14356 /* Prevent various kinds of signals during display
14357 update. stdio is not robust about handling
14358 signals, which can cause an apparent I/O error. */
14359 if (interrupt_input)
14360 unrequest_sigio ();
14361 STOP_POLLING;
14363 pending |= update_frame (f, false, false);
14364 f->cursor_type_changed = false;
14365 f->updated_p = true;
14370 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14372 if (!pending)
14374 /* Do the mark_window_display_accurate after all windows have
14375 been redisplayed because this call resets flags in buffers
14376 which are needed for proper redisplay. */
14377 FOR_EACH_FRAME (tail, frame)
14379 struct frame *f = XFRAME (frame);
14380 if (f->updated_p)
14382 f->redisplay = false;
14383 f->garbaged = false;
14384 mark_window_display_accurate (f->root_window, true);
14385 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14386 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14391 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14393 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14394 /* Use list_of_error, not Qerror, so that
14395 we catch only errors and don't run the debugger. */
14396 internal_condition_case_1 (redisplay_window_1, selected_window,
14397 list_of_error,
14398 redisplay_window_error);
14399 if (update_miniwindow_p)
14400 internal_condition_case_1 (redisplay_window_1,
14401 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14402 redisplay_window_error);
14404 /* Compare desired and current matrices, perform output. */
14406 update:
14407 /* If fonts changed, display again. Likewise if redisplay_window_1
14408 above caused some change (e.g., a change in faces) that requires
14409 considering the entire frame again. */
14410 if (sf->fonts_changed || sf->redisplay)
14412 if (sf->redisplay)
14414 /* Set this to force a more thorough redisplay.
14415 Otherwise, we might immediately loop back to the
14416 above "else-if" clause (since all the conditions that
14417 led here might still be true), and we will then
14418 infloop, because the selected-frame's redisplay flag
14419 is not (and cannot be) reset. */
14420 windows_or_buffers_changed = 50;
14422 goto retry;
14425 /* Prevent freeing of realized faces, since desired matrices are
14426 pending that reference the faces we computed and cached. */
14427 inhibit_free_realized_faces = true;
14429 /* Prevent various kinds of signals during display update.
14430 stdio is not robust about handling signals,
14431 which can cause an apparent I/O error. */
14432 if (interrupt_input)
14433 unrequest_sigio ();
14434 STOP_POLLING;
14436 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14438 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14439 && hscroll_windows (selected_window))
14441 hscroll_retries++;
14442 goto retry;
14445 XWINDOW (selected_window)->must_be_updated_p = true;
14446 pending = update_frame (sf, false, false);
14447 sf->cursor_type_changed = false;
14450 /* We may have called echo_area_display at the top of this
14451 function. If the echo area is on another frame, that may
14452 have put text on a frame other than the selected one, so the
14453 above call to update_frame would not have caught it. Catch
14454 it here. */
14455 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14456 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14458 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14460 XWINDOW (mini_window)->must_be_updated_p = true;
14461 pending |= update_frame (mini_frame, false, false);
14462 mini_frame->cursor_type_changed = false;
14463 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14464 && hscroll_windows (mini_window))
14466 hscroll_retries++;
14467 goto retry;
14472 /* If display was paused because of pending input, make sure we do a
14473 thorough update the next time. */
14474 if (pending)
14476 /* Prevent the optimization at the beginning of
14477 redisplay_internal that tries a single-line update of the
14478 line containing the cursor in the selected window. */
14479 CHARPOS (this_line_start_pos) = 0;
14481 /* Let the overlay arrow be updated the next time. */
14482 update_overlay_arrows (0);
14484 /* If we pause after scrolling, some rows in the current
14485 matrices of some windows are not valid. */
14486 if (!WINDOW_FULL_WIDTH_P (w)
14487 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14488 update_mode_lines = 36;
14490 else
14492 if (!consider_all_windows_p)
14494 /* This has already been done above if
14495 consider_all_windows_p is set. */
14496 if (XBUFFER (w->contents)->text->redisplay
14497 && buffer_window_count (XBUFFER (w->contents)) > 1)
14498 /* This can happen if b->text->redisplay was set during
14499 jit-lock. */
14500 propagate_buffer_redisplay ();
14501 mark_window_display_accurate_1 (w, true);
14503 /* Say overlay arrows are up to date. */
14504 update_overlay_arrows (1);
14506 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14507 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14510 update_mode_lines = 0;
14511 windows_or_buffers_changed = 0;
14514 /* Start SIGIO interrupts coming again. Having them off during the
14515 code above makes it less likely one will discard output, but not
14516 impossible, since there might be stuff in the system buffer here.
14517 But it is much hairier to try to do anything about that. */
14518 if (interrupt_input)
14519 request_sigio ();
14520 RESUME_POLLING;
14522 /* If a frame has become visible which was not before, redisplay
14523 again, so that we display it. Expose events for such a frame
14524 (which it gets when becoming visible) don't call the parts of
14525 redisplay constructing glyphs, so simply exposing a frame won't
14526 display anything in this case. So, we have to display these
14527 frames here explicitly. */
14528 if (!pending)
14530 int new_count = 0;
14532 FOR_EACH_FRAME (tail, frame)
14534 if (XFRAME (frame)->visible)
14535 new_count++;
14538 if (new_count != number_of_visible_frames)
14539 windows_or_buffers_changed = 52;
14542 /* Change frame size now if a change is pending. */
14543 do_pending_window_change (true);
14545 /* If we just did a pending size change, or have additional
14546 visible frames, or selected_window changed, redisplay again. */
14547 if ((windows_or_buffers_changed && !pending)
14548 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14549 goto retry;
14551 /* Clear the face and image caches.
14553 We used to do this only if consider_all_windows_p. But the cache
14554 needs to be cleared if a timer creates images in the current
14555 buffer (e.g. the test case in Bug#6230). */
14557 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14559 clear_face_cache (false);
14560 clear_face_cache_count = 0;
14563 #ifdef HAVE_WINDOW_SYSTEM
14564 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14566 clear_image_caches (Qnil);
14567 clear_image_cache_count = 0;
14569 #endif /* HAVE_WINDOW_SYSTEM */
14571 end_of_redisplay:
14572 #ifdef HAVE_NS
14573 ns_set_doc_edited ();
14574 #endif
14575 if (interrupt_input && interrupts_deferred)
14576 request_sigio ();
14578 unbind_to (count, Qnil);
14579 RESUME_POLLING;
14582 static void
14583 unwind_redisplay_preserve_echo_area (void)
14585 unblock_buffer_flips ();
14588 /* Redisplay, but leave alone any recent echo area message unless
14589 another message has been requested in its place.
14591 This is useful in situations where you need to redisplay but no
14592 user action has occurred, making it inappropriate for the message
14593 area to be cleared. See tracking_off and
14594 wait_reading_process_output for examples of these situations.
14596 FROM_WHERE is an integer saying from where this function was
14597 called. This is useful for debugging. */
14599 void
14600 redisplay_preserve_echo_area (int from_where)
14602 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14604 block_input ();
14605 ptrdiff_t count = SPECPDL_INDEX ();
14606 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14607 block_buffer_flips ();
14608 unblock_input ();
14610 if (!NILP (echo_area_buffer[1]))
14612 /* We have a previously displayed message, but no current
14613 message. Redisplay the previous message. */
14614 display_last_displayed_message_p = true;
14615 redisplay_internal ();
14616 display_last_displayed_message_p = false;
14618 else
14619 redisplay_internal ();
14621 flush_frame (SELECTED_FRAME ());
14622 unbind_to (count, Qnil);
14626 /* Function registered with record_unwind_protect in redisplay_internal. */
14628 static void
14629 unwind_redisplay (void)
14631 redisplaying_p = false;
14632 unblock_buffer_flips ();
14636 /* Mark the display of leaf window W as accurate or inaccurate.
14637 If ACCURATE_P, mark display of W as accurate.
14638 If !ACCURATE_P, arrange for W to be redisplayed the next
14639 time redisplay_internal is called. */
14641 static void
14642 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14644 struct buffer *b = XBUFFER (w->contents);
14646 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14647 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14648 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14650 if (accurate_p)
14652 b->clip_changed = false;
14653 b->prevent_redisplay_optimizations_p = false;
14654 eassert (buffer_window_count (b) > 0);
14655 /* Resetting b->text->redisplay is problematic!
14656 In order to make it safer to do it here, redisplay_internal must
14657 have copied all b->text->redisplay to their respective windows. */
14658 b->text->redisplay = false;
14660 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14661 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14662 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14663 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14665 w->current_matrix->buffer = b;
14666 w->current_matrix->begv = BUF_BEGV (b);
14667 w->current_matrix->zv = BUF_ZV (b);
14669 w->last_cursor_vpos = w->cursor.vpos;
14670 w->last_cursor_off_p = w->cursor_off_p;
14672 if (w == XWINDOW (selected_window))
14673 w->last_point = BUF_PT (b);
14674 else
14675 w->last_point = marker_position (w->pointm);
14677 w->window_end_valid = true;
14678 w->update_mode_line = false;
14681 w->redisplay = !accurate_p;
14685 /* Mark the display of windows in the window tree rooted at WINDOW as
14686 accurate or inaccurate. If ACCURATE_P, mark display of
14687 windows as accurate. If !ACCURATE_P, arrange for windows to
14688 be redisplayed the next time redisplay_internal is called. */
14690 void
14691 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14693 struct window *w;
14695 for (; !NILP (window); window = w->next)
14697 w = XWINDOW (window);
14698 if (WINDOWP (w->contents))
14699 mark_window_display_accurate (w->contents, accurate_p);
14700 else
14701 mark_window_display_accurate_1 (w, accurate_p);
14704 if (accurate_p)
14705 update_overlay_arrows (1);
14706 else
14707 /* Force a thorough redisplay the next time by setting
14708 last_arrow_position and last_arrow_string to t, which is
14709 unequal to any useful value of Voverlay_arrow_... */
14710 update_overlay_arrows (-1);
14714 /* Return value in display table DP (Lisp_Char_Table *) for character
14715 C. Since a display table doesn't have any parent, we don't have to
14716 follow parent. Do not call this function directly but use the
14717 macro DISP_CHAR_VECTOR. */
14719 Lisp_Object
14720 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14722 Lisp_Object val;
14724 if (ASCII_CHAR_P (c))
14726 val = dp->ascii;
14727 if (SUB_CHAR_TABLE_P (val))
14728 val = XSUB_CHAR_TABLE (val)->contents[c];
14730 else
14732 Lisp_Object table;
14734 XSETCHAR_TABLE (table, dp);
14735 val = char_table_ref (table, c);
14737 if (NILP (val))
14738 val = dp->defalt;
14739 return val;
14742 static int buffer_flip_blocked_depth;
14744 static void
14745 block_buffer_flips (void)
14747 eassert (buffer_flip_blocked_depth >= 0);
14748 buffer_flip_blocked_depth++;
14751 static void
14752 unblock_buffer_flips (void)
14754 eassert (buffer_flip_blocked_depth > 0);
14755 if (--buffer_flip_blocked_depth == 0)
14757 Lisp_Object tail, frame;
14758 block_input ();
14759 FOR_EACH_FRAME (tail, frame)
14761 struct frame *f = XFRAME (frame);
14762 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14763 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14765 unblock_input ();
14769 bool
14770 buffer_flipping_blocked_p (void)
14772 return buffer_flip_blocked_depth > 0;
14776 /***********************************************************************
14777 Window Redisplay
14778 ***********************************************************************/
14780 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14782 static void
14783 redisplay_windows (Lisp_Object window)
14785 while (!NILP (window))
14787 struct window *w = XWINDOW (window);
14789 if (WINDOWP (w->contents))
14790 redisplay_windows (w->contents);
14791 else if (BUFFERP (w->contents))
14793 displayed_buffer = XBUFFER (w->contents);
14794 /* Use list_of_error, not Qerror, so that
14795 we catch only errors and don't run the debugger. */
14796 internal_condition_case_1 (redisplay_window_0, window,
14797 list_of_error,
14798 redisplay_window_error);
14801 window = w->next;
14805 static Lisp_Object
14806 redisplay_window_error (Lisp_Object ignore)
14808 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14809 return Qnil;
14812 static Lisp_Object
14813 redisplay_window_0 (Lisp_Object window)
14815 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14816 redisplay_window (window, false);
14817 return Qnil;
14820 static Lisp_Object
14821 redisplay_window_1 (Lisp_Object window)
14823 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14824 redisplay_window (window, true);
14825 return Qnil;
14829 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14830 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14831 which positions recorded in ROW differ from current buffer
14832 positions.
14834 Return true iff cursor is on this row. */
14836 static bool
14837 set_cursor_from_row (struct window *w, struct glyph_row *row,
14838 struct glyph_matrix *matrix,
14839 ptrdiff_t delta, ptrdiff_t delta_bytes,
14840 int dy, int dvpos)
14842 struct glyph *glyph = row->glyphs[TEXT_AREA];
14843 struct glyph *end = glyph + row->used[TEXT_AREA];
14844 struct glyph *cursor = NULL;
14845 /* The last known character position in row. */
14846 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14847 int x = row->x;
14848 ptrdiff_t pt_old = PT - delta;
14849 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14850 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14851 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14852 /* A glyph beyond the edge of TEXT_AREA which we should never
14853 touch. */
14854 struct glyph *glyphs_end = end;
14855 /* True means we've found a match for cursor position, but that
14856 glyph has the avoid_cursor_p flag set. */
14857 bool match_with_avoid_cursor = false;
14858 /* True means we've seen at least one glyph that came from a
14859 display string. */
14860 bool string_seen = false;
14861 /* Largest and smallest buffer positions seen so far during scan of
14862 glyph row. */
14863 ptrdiff_t bpos_max = pos_before;
14864 ptrdiff_t bpos_min = pos_after;
14865 /* Last buffer position covered by an overlay string with an integer
14866 `cursor' property. */
14867 ptrdiff_t bpos_covered = 0;
14868 /* True means the display string on which to display the cursor
14869 comes from a text property, not from an overlay. */
14870 bool string_from_text_prop = false;
14872 /* Don't even try doing anything if called for a mode-line or
14873 header-line row, since the rest of the code isn't prepared to
14874 deal with such calamities. */
14875 eassert (!row->mode_line_p);
14876 if (row->mode_line_p)
14877 return false;
14879 /* Skip over glyphs not having an object at the start and the end of
14880 the row. These are special glyphs like truncation marks on
14881 terminal frames. */
14882 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14884 if (!row->reversed_p)
14886 while (glyph < end
14887 && NILP (glyph->object)
14888 && glyph->charpos < 0)
14890 x += glyph->pixel_width;
14891 ++glyph;
14893 while (end > glyph
14894 && NILP ((end - 1)->object)
14895 /* CHARPOS is zero for blanks and stretch glyphs
14896 inserted by extend_face_to_end_of_line. */
14897 && (end - 1)->charpos <= 0)
14898 --end;
14899 glyph_before = glyph - 1;
14900 glyph_after = end;
14902 else
14904 struct glyph *g;
14906 /* If the glyph row is reversed, we need to process it from back
14907 to front, so swap the edge pointers. */
14908 glyphs_end = end = glyph - 1;
14909 glyph += row->used[TEXT_AREA] - 1;
14911 while (glyph > end + 1
14912 && NILP (glyph->object)
14913 && glyph->charpos < 0)
14914 --glyph;
14915 if (NILP (glyph->object) && glyph->charpos < 0)
14916 --glyph;
14917 /* By default, in reversed rows we put the cursor on the
14918 rightmost (first in the reading order) glyph. */
14919 for (x = 0, g = end + 1; g < glyph; g++)
14920 x += g->pixel_width;
14921 while (end < glyph
14922 && NILP ((end + 1)->object)
14923 && (end + 1)->charpos <= 0)
14924 ++end;
14925 glyph_before = glyph + 1;
14926 glyph_after = end;
14929 else if (row->reversed_p)
14931 /* In R2L rows that don't display text, put the cursor on the
14932 rightmost glyph. Case in point: an empty last line that is
14933 part of an R2L paragraph. */
14934 cursor = end - 1;
14935 /* Avoid placing the cursor on the last glyph of the row, where
14936 on terminal frames we hold the vertical border between
14937 adjacent windows. */
14938 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14939 && !WINDOW_RIGHTMOST_P (w)
14940 && cursor == row->glyphs[LAST_AREA] - 1)
14941 cursor--;
14942 x = -1; /* will be computed below, at label compute_x */
14945 /* Step 1: Try to find the glyph whose character position
14946 corresponds to point. If that's not possible, find 2 glyphs
14947 whose character positions are the closest to point, one before
14948 point, the other after it. */
14949 if (!row->reversed_p)
14950 while (/* not marched to end of glyph row */
14951 glyph < end
14952 /* glyph was not inserted by redisplay for internal purposes */
14953 && !NILP (glyph->object))
14955 if (BUFFERP (glyph->object))
14957 ptrdiff_t dpos = glyph->charpos - pt_old;
14959 if (glyph->charpos > bpos_max)
14960 bpos_max = glyph->charpos;
14961 if (glyph->charpos < bpos_min)
14962 bpos_min = glyph->charpos;
14963 if (!glyph->avoid_cursor_p)
14965 /* If we hit point, we've found the glyph on which to
14966 display the cursor. */
14967 if (dpos == 0)
14969 match_with_avoid_cursor = false;
14970 break;
14972 /* See if we've found a better approximation to
14973 POS_BEFORE or to POS_AFTER. */
14974 if (0 > dpos && dpos > pos_before - pt_old)
14976 pos_before = glyph->charpos;
14977 glyph_before = glyph;
14979 else if (0 < dpos && dpos < pos_after - pt_old)
14981 pos_after = glyph->charpos;
14982 glyph_after = glyph;
14985 else if (dpos == 0)
14986 match_with_avoid_cursor = true;
14988 else if (STRINGP (glyph->object))
14990 Lisp_Object chprop;
14991 ptrdiff_t glyph_pos = glyph->charpos;
14993 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14994 glyph->object);
14995 if (!NILP (chprop))
14997 /* If the string came from a `display' text property,
14998 look up the buffer position of that property and
14999 use that position to update bpos_max, as if we
15000 actually saw such a position in one of the row's
15001 glyphs. This helps with supporting integer values
15002 of `cursor' property on the display string in
15003 situations where most or all of the row's buffer
15004 text is completely covered by display properties,
15005 so that no glyph with valid buffer positions is
15006 ever seen in the row. */
15007 ptrdiff_t prop_pos =
15008 string_buffer_position_lim (glyph->object, pos_before,
15009 pos_after, false);
15011 if (prop_pos >= pos_before)
15012 bpos_max = prop_pos;
15014 if (INTEGERP (chprop))
15016 bpos_covered = bpos_max + XINT (chprop);
15017 /* If the `cursor' property covers buffer positions up
15018 to and including point, we should display cursor on
15019 this glyph. Note that, if a `cursor' property on one
15020 of the string's characters has an integer value, we
15021 will break out of the loop below _before_ we get to
15022 the position match above. IOW, integer values of
15023 the `cursor' property override the "exact match for
15024 point" strategy of positioning the cursor. */
15025 /* Implementation note: bpos_max == pt_old when, e.g.,
15026 we are in an empty line, where bpos_max is set to
15027 MATRIX_ROW_START_CHARPOS, see above. */
15028 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15030 cursor = glyph;
15031 break;
15035 string_seen = true;
15037 x += glyph->pixel_width;
15038 ++glyph;
15040 else if (glyph > end) /* row is reversed */
15041 while (!NILP (glyph->object))
15043 if (BUFFERP (glyph->object))
15045 ptrdiff_t dpos = glyph->charpos - pt_old;
15047 if (glyph->charpos > bpos_max)
15048 bpos_max = glyph->charpos;
15049 if (glyph->charpos < bpos_min)
15050 bpos_min = glyph->charpos;
15051 if (!glyph->avoid_cursor_p)
15053 if (dpos == 0)
15055 match_with_avoid_cursor = false;
15056 break;
15058 if (0 > dpos && dpos > pos_before - pt_old)
15060 pos_before = glyph->charpos;
15061 glyph_before = glyph;
15063 else if (0 < dpos && dpos < pos_after - pt_old)
15065 pos_after = glyph->charpos;
15066 glyph_after = glyph;
15069 else if (dpos == 0)
15070 match_with_avoid_cursor = true;
15072 else if (STRINGP (glyph->object))
15074 Lisp_Object chprop;
15075 ptrdiff_t glyph_pos = glyph->charpos;
15077 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15078 glyph->object);
15079 if (!NILP (chprop))
15081 ptrdiff_t prop_pos =
15082 string_buffer_position_lim (glyph->object, pos_before,
15083 pos_after, false);
15085 if (prop_pos >= pos_before)
15086 bpos_max = prop_pos;
15088 if (INTEGERP (chprop))
15090 bpos_covered = bpos_max + XINT (chprop);
15091 /* If the `cursor' property covers buffer positions up
15092 to and including point, we should display cursor on
15093 this glyph. */
15094 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15096 cursor = glyph;
15097 break;
15100 string_seen = true;
15102 --glyph;
15103 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15105 x--; /* can't use any pixel_width */
15106 break;
15108 x -= glyph->pixel_width;
15111 /* Step 2: If we didn't find an exact match for point, we need to
15112 look for a proper place to put the cursor among glyphs between
15113 GLYPH_BEFORE and GLYPH_AFTER. */
15114 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15115 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15116 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15118 /* An empty line has a single glyph whose OBJECT is nil and
15119 whose CHARPOS is the position of a newline on that line.
15120 Note that on a TTY, there are more glyphs after that, which
15121 were produced by extend_face_to_end_of_line, but their
15122 CHARPOS is zero or negative. */
15123 bool empty_line_p =
15124 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15125 && NILP (glyph->object) && glyph->charpos > 0
15126 /* On a TTY, continued and truncated rows also have a glyph at
15127 their end whose OBJECT is nil and whose CHARPOS is
15128 positive (the continuation and truncation glyphs), but such
15129 rows are obviously not "empty". */
15130 && !(row->continued_p || row->truncated_on_right_p));
15132 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15134 ptrdiff_t ellipsis_pos;
15136 /* Scan back over the ellipsis glyphs. */
15137 if (!row->reversed_p)
15139 ellipsis_pos = (glyph - 1)->charpos;
15140 while (glyph > row->glyphs[TEXT_AREA]
15141 && (glyph - 1)->charpos == ellipsis_pos)
15142 glyph--, x -= glyph->pixel_width;
15143 /* That loop always goes one position too far, including
15144 the glyph before the ellipsis. So scan forward over
15145 that one. */
15146 x += glyph->pixel_width;
15147 glyph++;
15149 else /* row is reversed */
15151 ellipsis_pos = (glyph + 1)->charpos;
15152 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15153 && (glyph + 1)->charpos == ellipsis_pos)
15154 glyph++, x += glyph->pixel_width;
15155 x -= glyph->pixel_width;
15156 glyph--;
15159 else if (match_with_avoid_cursor)
15161 cursor = glyph_after;
15162 x = -1;
15164 else if (string_seen)
15166 int incr = row->reversed_p ? -1 : +1;
15168 /* Need to find the glyph that came out of a string which is
15169 present at point. That glyph is somewhere between
15170 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15171 positioned between POS_BEFORE and POS_AFTER in the
15172 buffer. */
15173 struct glyph *start, *stop;
15174 ptrdiff_t pos = pos_before;
15176 x = -1;
15178 /* If the row ends in a newline from a display string,
15179 reordering could have moved the glyphs belonging to the
15180 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15181 in this case we extend the search to the last glyph in
15182 the row that was not inserted by redisplay. */
15183 if (row->ends_in_newline_from_string_p)
15185 glyph_after = end;
15186 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15189 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15190 correspond to POS_BEFORE and POS_AFTER, respectively. We
15191 need START and STOP in the order that corresponds to the
15192 row's direction as given by its reversed_p flag. If the
15193 directionality of characters between POS_BEFORE and
15194 POS_AFTER is the opposite of the row's base direction,
15195 these characters will have been reordered for display,
15196 and we need to reverse START and STOP. */
15197 if (!row->reversed_p)
15199 start = min (glyph_before, glyph_after);
15200 stop = max (glyph_before, glyph_after);
15202 else
15204 start = max (glyph_before, glyph_after);
15205 stop = min (glyph_before, glyph_after);
15207 for (glyph = start + incr;
15208 row->reversed_p ? glyph > stop : glyph < stop; )
15211 /* Any glyphs that come from the buffer are here because
15212 of bidi reordering. Skip them, and only pay
15213 attention to glyphs that came from some string. */
15214 if (STRINGP (glyph->object))
15216 Lisp_Object str;
15217 ptrdiff_t tem;
15218 /* If the display property covers the newline, we
15219 need to search for it one position farther. */
15220 ptrdiff_t lim = pos_after
15221 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15223 string_from_text_prop = false;
15224 str = glyph->object;
15225 tem = string_buffer_position_lim (str, pos, lim, false);
15226 if (tem == 0 /* from overlay */
15227 || pos <= tem)
15229 /* If the string from which this glyph came is
15230 found in the buffer at point, or at position
15231 that is closer to point than pos_after, then
15232 we've found the glyph we've been looking for.
15233 If it comes from an overlay (tem == 0), and
15234 it has the `cursor' property on one of its
15235 glyphs, record that glyph as a candidate for
15236 displaying the cursor. (As in the
15237 unidirectional version, we will display the
15238 cursor on the last candidate we find.) */
15239 if (tem == 0
15240 || tem == pt_old
15241 || (tem - pt_old > 0 && tem < pos_after))
15243 /* The glyphs from this string could have
15244 been reordered. Find the one with the
15245 smallest string position. Or there could
15246 be a character in the string with the
15247 `cursor' property, which means display
15248 cursor on that character's glyph. */
15249 ptrdiff_t strpos = glyph->charpos;
15251 if (tem)
15253 cursor = glyph;
15254 string_from_text_prop = true;
15256 for ( ;
15257 (row->reversed_p ? glyph > stop : glyph < stop)
15258 && EQ (glyph->object, str);
15259 glyph += incr)
15261 Lisp_Object cprop;
15262 ptrdiff_t gpos = glyph->charpos;
15264 cprop = Fget_char_property (make_number (gpos),
15265 Qcursor,
15266 glyph->object);
15267 if (!NILP (cprop))
15269 cursor = glyph;
15270 break;
15272 if (tem && glyph->charpos < strpos)
15274 strpos = glyph->charpos;
15275 cursor = glyph;
15279 if (tem == pt_old
15280 || (tem - pt_old > 0 && tem < pos_after))
15281 goto compute_x;
15283 if (tem)
15284 pos = tem + 1; /* don't find previous instances */
15286 /* This string is not what we want; skip all of the
15287 glyphs that came from it. */
15288 while ((row->reversed_p ? glyph > stop : glyph < stop)
15289 && EQ (glyph->object, str))
15290 glyph += incr;
15292 else
15293 glyph += incr;
15296 /* If we reached the end of the line, and END was from a string,
15297 the cursor is not on this line. */
15298 if (cursor == NULL
15299 && (row->reversed_p ? glyph <= end : glyph >= end)
15300 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15301 && STRINGP (end->object)
15302 && row->continued_p)
15303 return false;
15305 /* A truncated row may not include PT among its character positions.
15306 Setting the cursor inside the scroll margin will trigger
15307 recalculation of hscroll in hscroll_window_tree. But if a
15308 display string covers point, defer to the string-handling
15309 code below to figure this out. */
15310 else if (row->truncated_on_left_p && pt_old < bpos_min)
15312 cursor = glyph_before;
15313 x = -1;
15315 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15316 /* Zero-width characters produce no glyphs. */
15317 || (!empty_line_p
15318 && (row->reversed_p
15319 ? glyph_after > glyphs_end
15320 : glyph_after < glyphs_end)))
15322 cursor = glyph_after;
15323 x = -1;
15327 compute_x:
15328 if (cursor != NULL)
15329 glyph = cursor;
15330 else if (glyph == glyphs_end
15331 && pos_before == pos_after
15332 && STRINGP ((row->reversed_p
15333 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15334 : row->glyphs[TEXT_AREA])->object))
15336 /* If all the glyphs of this row came from strings, put the
15337 cursor on the first glyph of the row. This avoids having the
15338 cursor outside of the text area in this very rare and hard
15339 use case. */
15340 glyph =
15341 row->reversed_p
15342 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15343 : row->glyphs[TEXT_AREA];
15345 if (x < 0)
15347 struct glyph *g;
15349 /* Need to compute x that corresponds to GLYPH. */
15350 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15352 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15353 emacs_abort ();
15354 x += g->pixel_width;
15358 /* ROW could be part of a continued line, which, under bidi
15359 reordering, might have other rows whose start and end charpos
15360 occlude point. Only set w->cursor if we found a better
15361 approximation to the cursor position than we have from previously
15362 examined candidate rows belonging to the same continued line. */
15363 if (/* We already have a candidate row. */
15364 w->cursor.vpos >= 0
15365 /* That candidate is not the row we are processing. */
15366 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15367 /* Make sure cursor.vpos specifies a row whose start and end
15368 charpos occlude point, and it is valid candidate for being a
15369 cursor-row. This is because some callers of this function
15370 leave cursor.vpos at the row where the cursor was displayed
15371 during the last redisplay cycle. */
15372 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15373 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15374 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15376 struct glyph *g1
15377 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15379 /* Don't consider glyphs that are outside TEXT_AREA. */
15380 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15381 return false;
15382 /* Keep the candidate whose buffer position is the closest to
15383 point or has the `cursor' property. */
15384 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15385 w->cursor.hpos >= 0
15386 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15387 && ((BUFFERP (g1->object)
15388 && (g1->charpos == pt_old /* An exact match always wins. */
15389 || (BUFFERP (glyph->object)
15390 && eabs (g1->charpos - pt_old)
15391 < eabs (glyph->charpos - pt_old))))
15392 /* Previous candidate is a glyph from a string that has
15393 a non-nil `cursor' property. */
15394 || (STRINGP (g1->object)
15395 && (!NILP (Fget_char_property (make_number (g1->charpos),
15396 Qcursor, g1->object))
15397 /* Previous candidate is from the same display
15398 string as this one, and the display string
15399 came from a text property. */
15400 || (EQ (g1->object, glyph->object)
15401 && string_from_text_prop)
15402 /* this candidate is from newline and its
15403 position is not an exact match */
15404 || (NILP (glyph->object)
15405 && glyph->charpos != pt_old)))))
15406 return false;
15407 /* If this candidate gives an exact match, use that. */
15408 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15409 /* If this candidate is a glyph created for the
15410 terminating newline of a line, and point is on that
15411 newline, it wins because it's an exact match. */
15412 || (!row->continued_p
15413 && NILP (glyph->object)
15414 && glyph->charpos == 0
15415 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15416 /* Otherwise, keep the candidate that comes from a row
15417 spanning less buffer positions. This may win when one or
15418 both candidate positions are on glyphs that came from
15419 display strings, for which we cannot compare buffer
15420 positions. */
15421 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15422 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15423 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15424 return false;
15426 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15427 w->cursor.x = x;
15428 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15429 w->cursor.y = row->y + dy;
15431 if (w == XWINDOW (selected_window))
15433 if (!row->continued_p
15434 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15435 && row->x == 0)
15437 this_line_buffer = XBUFFER (w->contents);
15439 CHARPOS (this_line_start_pos)
15440 = MATRIX_ROW_START_CHARPOS (row) + delta;
15441 BYTEPOS (this_line_start_pos)
15442 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15444 CHARPOS (this_line_end_pos)
15445 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15446 BYTEPOS (this_line_end_pos)
15447 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15449 this_line_y = w->cursor.y;
15450 this_line_pixel_height = row->height;
15451 this_line_vpos = w->cursor.vpos;
15452 this_line_start_x = row->x;
15454 else
15455 CHARPOS (this_line_start_pos) = 0;
15458 return true;
15462 /* Run window scroll functions, if any, for WINDOW with new window
15463 start STARTP. Sets the window start of WINDOW to that position.
15465 We assume that the window's buffer is really current. */
15467 static struct text_pos
15468 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15470 struct window *w = XWINDOW (window);
15471 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15473 eassert (current_buffer == XBUFFER (w->contents));
15475 if (!NILP (Vwindow_scroll_functions))
15477 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15478 make_number (CHARPOS (startp)));
15479 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15480 /* In case the hook functions switch buffers. */
15481 set_buffer_internal (XBUFFER (w->contents));
15484 return startp;
15488 /* Make sure the line containing the cursor is fully visible.
15489 A value of true means there is nothing to be done.
15490 (Either the line is fully visible, or it cannot be made so,
15491 or we cannot tell.)
15493 If FORCE_P, return false even if partial visible cursor row
15494 is higher than window.
15496 If CURRENT_MATRIX_P, use the information from the
15497 window's current glyph matrix; otherwise use the desired glyph
15498 matrix.
15500 A value of false means the caller should do scrolling
15501 as if point had gone off the screen. */
15503 static bool
15504 cursor_row_fully_visible_p (struct window *w, bool force_p,
15505 bool current_matrix_p)
15507 struct glyph_matrix *matrix;
15508 struct glyph_row *row;
15509 int window_height;
15511 if (!make_cursor_line_fully_visible_p)
15512 return true;
15514 /* It's not always possible to find the cursor, e.g, when a window
15515 is full of overlay strings. Don't do anything in that case. */
15516 if (w->cursor.vpos < 0)
15517 return true;
15519 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15520 row = MATRIX_ROW (matrix, w->cursor.vpos);
15522 /* If the cursor row is not partially visible, there's nothing to do. */
15523 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15524 return true;
15526 /* If the row the cursor is in is taller than the window's height,
15527 it's not clear what to do, so do nothing. */
15528 window_height = window_box_height (w);
15529 if (row->height >= window_height)
15531 if (!force_p || MINI_WINDOW_P (w)
15532 || w->vscroll || w->cursor.vpos == 0)
15533 return true;
15535 return false;
15539 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15540 means only WINDOW is redisplayed in redisplay_internal.
15541 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15542 in redisplay_window to bring a partially visible line into view in
15543 the case that only the cursor has moved.
15545 LAST_LINE_MISFIT should be true if we're scrolling because the
15546 last screen line's vertical height extends past the end of the screen.
15548 Value is
15550 1 if scrolling succeeded
15552 0 if scrolling didn't find point.
15554 -1 if new fonts have been loaded so that we must interrupt
15555 redisplay, adjust glyph matrices, and try again. */
15557 enum
15559 SCROLLING_SUCCESS,
15560 SCROLLING_FAILED,
15561 SCROLLING_NEED_LARGER_MATRICES
15564 /* If scroll-conservatively is more than this, never recenter.
15566 If you change this, don't forget to update the doc string of
15567 `scroll-conservatively' and the Emacs manual. */
15568 #define SCROLL_LIMIT 100
15570 static int
15571 try_scrolling (Lisp_Object window, bool just_this_one_p,
15572 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15573 bool temp_scroll_step, bool last_line_misfit)
15575 struct window *w = XWINDOW (window);
15576 struct text_pos pos, startp;
15577 struct it it;
15578 int this_scroll_margin, scroll_max, rc, height;
15579 int dy = 0, amount_to_scroll = 0;
15580 bool scroll_down_p = false;
15581 int extra_scroll_margin_lines = last_line_misfit;
15582 Lisp_Object aggressive;
15583 /* We will never try scrolling more than this number of lines. */
15584 int scroll_limit = SCROLL_LIMIT;
15585 int frame_line_height = default_line_pixel_height (w);
15587 #ifdef GLYPH_DEBUG
15588 debug_method_add (w, "try_scrolling");
15589 #endif
15591 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15593 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15595 /* Force arg_scroll_conservatively to have a reasonable value, to
15596 avoid scrolling too far away with slow move_it_* functions. Note
15597 that the user can supply scroll-conservatively equal to
15598 `most-positive-fixnum', which can be larger than INT_MAX. */
15599 if (arg_scroll_conservatively > scroll_limit)
15601 arg_scroll_conservatively = scroll_limit + 1;
15602 scroll_max = scroll_limit * frame_line_height;
15604 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15605 /* Compute how much we should try to scroll maximally to bring
15606 point into view. */
15607 scroll_max = (max (scroll_step,
15608 max (arg_scroll_conservatively, temp_scroll_step))
15609 * frame_line_height);
15610 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15611 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15612 /* We're trying to scroll because of aggressive scrolling but no
15613 scroll_step is set. Choose an arbitrary one. */
15614 scroll_max = 10 * frame_line_height;
15615 else
15616 scroll_max = 0;
15618 too_near_end:
15620 /* Decide whether to scroll down. */
15621 if (PT > CHARPOS (startp))
15623 int scroll_margin_y;
15625 /* Compute the pixel ypos of the scroll margin, then move IT to
15626 either that ypos or PT, whichever comes first. */
15627 start_display (&it, w, startp);
15628 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15629 - this_scroll_margin
15630 - frame_line_height * extra_scroll_margin_lines;
15631 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15632 (MOVE_TO_POS | MOVE_TO_Y));
15634 if (PT > CHARPOS (it.current.pos))
15636 int y0 = line_bottom_y (&it);
15637 /* Compute how many pixels below window bottom to stop searching
15638 for PT. This avoids costly search for PT that is far away if
15639 the user limited scrolling by a small number of lines, but
15640 always finds PT if scroll_conservatively is set to a large
15641 number, such as most-positive-fixnum. */
15642 int slack = max (scroll_max, 10 * frame_line_height);
15643 int y_to_move = it.last_visible_y + slack;
15645 /* Compute the distance from the scroll margin to PT or to
15646 the scroll limit, whichever comes first. This should
15647 include the height of the cursor line, to make that line
15648 fully visible. */
15649 move_it_to (&it, PT, -1, y_to_move,
15650 -1, MOVE_TO_POS | MOVE_TO_Y);
15651 dy = line_bottom_y (&it) - y0;
15653 if (dy > scroll_max)
15654 return SCROLLING_FAILED;
15656 if (dy > 0)
15657 scroll_down_p = true;
15659 else if (PT == IT_CHARPOS (it)
15660 && IT_CHARPOS (it) < ZV
15661 && it.method == GET_FROM_STRING
15662 && arg_scroll_conservatively > scroll_limit
15663 && it.current_x == 0)
15665 enum move_it_result skip;
15666 int y1 = it.current_y;
15667 int vpos;
15669 /* A before-string that includes newlines and is displayed
15670 on the last visible screen line could fail us under
15671 scroll-conservatively > 100, because we will be unable to
15672 position the cursor on that last visible line. Try to
15673 recover by finding the first screen line that has some
15674 glyphs coming from the buffer text. */
15675 do {
15676 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15677 if (skip != MOVE_NEWLINE_OR_CR
15678 || IT_CHARPOS (it) != PT
15679 || it.method == GET_FROM_BUFFER)
15680 break;
15681 vpos = it.vpos;
15682 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15683 } while (it.vpos > vpos);
15685 dy = it.current_y - y1;
15687 if (dy > scroll_max)
15688 return SCROLLING_FAILED;
15690 if (dy > 0)
15691 scroll_down_p = true;
15695 if (scroll_down_p)
15697 /* Point is in or below the bottom scroll margin, so move the
15698 window start down. If scrolling conservatively, move it just
15699 enough down to make point visible. If scroll_step is set,
15700 move it down by scroll_step. */
15701 if (arg_scroll_conservatively)
15702 amount_to_scroll
15703 = min (max (dy, frame_line_height),
15704 frame_line_height * arg_scroll_conservatively);
15705 else if (scroll_step || temp_scroll_step)
15706 amount_to_scroll = scroll_max;
15707 else
15709 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15710 height = WINDOW_BOX_TEXT_HEIGHT (w);
15711 if (NUMBERP (aggressive))
15713 double float_amount = XFLOATINT (aggressive) * height;
15714 int aggressive_scroll = float_amount;
15715 if (aggressive_scroll == 0 && float_amount > 0)
15716 aggressive_scroll = 1;
15717 /* Don't let point enter the scroll margin near top of
15718 the window. This could happen if the value of
15719 scroll_up_aggressively is too large and there are
15720 non-zero margins, because scroll_up_aggressively
15721 means put point that fraction of window height
15722 _from_the_bottom_margin_. */
15723 if (aggressive_scroll + 2 * this_scroll_margin > height)
15724 aggressive_scroll = height - 2 * this_scroll_margin;
15725 amount_to_scroll = dy + aggressive_scroll;
15729 if (amount_to_scroll <= 0)
15730 return SCROLLING_FAILED;
15732 start_display (&it, w, startp);
15733 if (arg_scroll_conservatively <= scroll_limit)
15734 move_it_vertically (&it, amount_to_scroll);
15735 else
15737 /* Extra precision for users who set scroll-conservatively
15738 to a large number: make sure the amount we scroll
15739 the window start is never less than amount_to_scroll,
15740 which was computed as distance from window bottom to
15741 point. This matters when lines at window top and lines
15742 below window bottom have different height. */
15743 struct it it1;
15744 void *it1data = NULL;
15745 /* We use a temporary it1 because line_bottom_y can modify
15746 its argument, if it moves one line down; see there. */
15747 int start_y;
15749 SAVE_IT (it1, it, it1data);
15750 start_y = line_bottom_y (&it1);
15751 do {
15752 RESTORE_IT (&it, &it, it1data);
15753 move_it_by_lines (&it, 1);
15754 SAVE_IT (it1, it, it1data);
15755 } while (IT_CHARPOS (it) < ZV
15756 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15757 bidi_unshelve_cache (it1data, true);
15760 /* If STARTP is unchanged, move it down another screen line. */
15761 if (IT_CHARPOS (it) == CHARPOS (startp))
15762 move_it_by_lines (&it, 1);
15763 startp = it.current.pos;
15765 else
15767 struct text_pos scroll_margin_pos = startp;
15768 int y_offset = 0;
15770 /* See if point is inside the scroll margin at the top of the
15771 window. */
15772 if (this_scroll_margin)
15774 int y_start;
15776 start_display (&it, w, startp);
15777 y_start = it.current_y;
15778 move_it_vertically (&it, this_scroll_margin);
15779 scroll_margin_pos = it.current.pos;
15780 /* If we didn't move enough before hitting ZV, request
15781 additional amount of scroll, to move point out of the
15782 scroll margin. */
15783 if (IT_CHARPOS (it) == ZV
15784 && it.current_y - y_start < this_scroll_margin)
15785 y_offset = this_scroll_margin - (it.current_y - y_start);
15788 if (PT < CHARPOS (scroll_margin_pos))
15790 /* Point is in the scroll margin at the top of the window or
15791 above what is displayed in the window. */
15792 int y0, y_to_move;
15794 /* Compute the vertical distance from PT to the scroll
15795 margin position. Move as far as scroll_max allows, or
15796 one screenful, or 10 screen lines, whichever is largest.
15797 Give up if distance is greater than scroll_max or if we
15798 didn't reach the scroll margin position. */
15799 SET_TEXT_POS (pos, PT, PT_BYTE);
15800 start_display (&it, w, pos);
15801 y0 = it.current_y;
15802 y_to_move = max (it.last_visible_y,
15803 max (scroll_max, 10 * frame_line_height));
15804 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15805 y_to_move, -1,
15806 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15807 dy = it.current_y - y0;
15808 if (dy > scroll_max
15809 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15810 return SCROLLING_FAILED;
15812 /* Additional scroll for when ZV was too close to point. */
15813 dy += y_offset;
15815 /* Compute new window start. */
15816 start_display (&it, w, startp);
15818 if (arg_scroll_conservatively)
15819 amount_to_scroll = max (dy, frame_line_height
15820 * max (scroll_step, temp_scroll_step));
15821 else if (scroll_step || temp_scroll_step)
15822 amount_to_scroll = scroll_max;
15823 else
15825 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15826 height = WINDOW_BOX_TEXT_HEIGHT (w);
15827 if (NUMBERP (aggressive))
15829 double float_amount = XFLOATINT (aggressive) * height;
15830 int aggressive_scroll = float_amount;
15831 if (aggressive_scroll == 0 && float_amount > 0)
15832 aggressive_scroll = 1;
15833 /* Don't let point enter the scroll margin near
15834 bottom of the window, if the value of
15835 scroll_down_aggressively happens to be too
15836 large. */
15837 if (aggressive_scroll + 2 * this_scroll_margin > height)
15838 aggressive_scroll = height - 2 * this_scroll_margin;
15839 amount_to_scroll = dy + aggressive_scroll;
15843 if (amount_to_scroll <= 0)
15844 return SCROLLING_FAILED;
15846 move_it_vertically_backward (&it, amount_to_scroll);
15847 startp = it.current.pos;
15851 /* Run window scroll functions. */
15852 startp = run_window_scroll_functions (window, startp);
15854 /* Display the window. Give up if new fonts are loaded, or if point
15855 doesn't appear. */
15856 if (!try_window (window, startp, 0))
15857 rc = SCROLLING_NEED_LARGER_MATRICES;
15858 else if (w->cursor.vpos < 0)
15860 clear_glyph_matrix (w->desired_matrix);
15861 rc = SCROLLING_FAILED;
15863 else
15865 /* Maybe forget recorded base line for line number display. */
15866 if (!just_this_one_p
15867 || current_buffer->clip_changed
15868 || BEG_UNCHANGED < CHARPOS (startp))
15869 w->base_line_number = 0;
15871 /* If cursor ends up on a partially visible line,
15872 treat that as being off the bottom of the screen. */
15873 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15874 false)
15875 /* It's possible that the cursor is on the first line of the
15876 buffer, which is partially obscured due to a vscroll
15877 (Bug#7537). In that case, avoid looping forever. */
15878 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15880 clear_glyph_matrix (w->desired_matrix);
15881 ++extra_scroll_margin_lines;
15882 goto too_near_end;
15884 rc = SCROLLING_SUCCESS;
15887 return rc;
15891 /* Compute a suitable window start for window W if display of W starts
15892 on a continuation line. Value is true if a new window start
15893 was computed.
15895 The new window start will be computed, based on W's width, starting
15896 from the start of the continued line. It is the start of the
15897 screen line with the minimum distance from the old start W->start,
15898 which is still before point (otherwise point will definitely not
15899 be visible in the window). */
15901 static bool
15902 compute_window_start_on_continuation_line (struct window *w)
15904 struct text_pos pos, start_pos, pos_before_pt;
15905 bool window_start_changed_p = false;
15907 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15909 /* If window start is on a continuation line... Window start may be
15910 < BEGV in case there's invisible text at the start of the
15911 buffer (M-x rmail, for example). */
15912 if (CHARPOS (start_pos) > BEGV
15913 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15915 struct it it;
15916 struct glyph_row *row;
15918 /* Handle the case that the window start is out of range. */
15919 if (CHARPOS (start_pos) < BEGV)
15920 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15921 else if (CHARPOS (start_pos) > ZV)
15922 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15924 /* Find the start of the continued line. This should be fast
15925 because find_newline is fast (newline cache). */
15926 row = w->desired_matrix->rows + window_wants_header_line (w);
15927 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15928 row, DEFAULT_FACE_ID);
15929 reseat_at_previous_visible_line_start (&it);
15931 /* If the line start is "too far" away from the window start,
15932 say it takes too much time to compute a new window start.
15933 Also, give up if the line start is after point, as in that
15934 case point will not be visible with any window start we
15935 compute. */
15936 if (IT_CHARPOS (it) <= PT
15937 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15938 /* PXW: Do we need upper bounds here? */
15939 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15941 int min_distance, distance;
15943 /* Move forward by display lines to find the new window
15944 start. If window width was enlarged, the new start can
15945 be expected to be > the old start. If window width was
15946 decreased, the new window start will be < the old start.
15947 So, we're looking for the display line start with the
15948 minimum distance from the old window start. */
15949 pos_before_pt = pos = it.current.pos;
15950 min_distance = DISP_INFINITY;
15951 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15952 distance < min_distance)
15954 min_distance = distance;
15955 if (CHARPOS (pos) <= PT)
15956 pos_before_pt = pos;
15957 pos = it.current.pos;
15958 if (it.line_wrap == WORD_WRAP)
15960 /* Under WORD_WRAP, move_it_by_lines is likely to
15961 overshoot and stop not at the first, but the
15962 second character from the left margin. So in
15963 that case, we need a more tight control on the X
15964 coordinate of the iterator than move_it_by_lines
15965 promises in its contract. The method is to first
15966 go to the last (rightmost) visible character of a
15967 line, then move to the leftmost character on the
15968 next line in a separate call. */
15969 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15970 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15971 move_it_to (&it, ZV, 0,
15972 it.current_y + it.max_ascent + it.max_descent, -1,
15973 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15975 else
15976 move_it_by_lines (&it, 1);
15979 /* It makes very little sense to make the new window start
15980 after point, as point won't be visible. If that's what
15981 the loop above finds, fall back on the candidate before
15982 or at point that is closest to the old window start. */
15983 if (CHARPOS (pos) > PT)
15984 pos = pos_before_pt;
15986 /* Set the window start there. */
15987 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15988 window_start_changed_p = true;
15992 return window_start_changed_p;
15996 /* Try cursor movement in case text has not changed in window WINDOW,
15997 with window start STARTP. Value is
15999 CURSOR_MOVEMENT_SUCCESS if successful
16001 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16003 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16004 display. *SCROLL_STEP is set to true, under certain circumstances, if
16005 we want to scroll as if scroll-step were set to 1. See the code.
16007 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16008 which case we have to abort this redisplay, and adjust matrices
16009 first. */
16011 enum
16013 CURSOR_MOVEMENT_SUCCESS,
16014 CURSOR_MOVEMENT_CANNOT_BE_USED,
16015 CURSOR_MOVEMENT_MUST_SCROLL,
16016 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16019 static int
16020 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16021 bool *scroll_step)
16023 struct window *w = XWINDOW (window);
16024 struct frame *f = XFRAME (w->frame);
16025 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16027 #ifdef GLYPH_DEBUG
16028 if (inhibit_try_cursor_movement)
16029 return rc;
16030 #endif
16032 /* Previously, there was a check for Lisp integer in the
16033 if-statement below. Now, this field is converted to
16034 ptrdiff_t, thus zero means invalid position in a buffer. */
16035 eassert (w->last_point > 0);
16036 /* Likewise there was a check whether window_end_vpos is nil or larger
16037 than the window. Now window_end_vpos is int and so never nil, but
16038 let's leave eassert to check whether it fits in the window. */
16039 eassert (!w->window_end_valid
16040 || w->window_end_vpos < w->current_matrix->nrows);
16042 /* Handle case where text has not changed, only point, and it has
16043 not moved off the frame. */
16044 if (/* Point may be in this window. */
16045 PT >= CHARPOS (startp)
16046 /* Selective display hasn't changed. */
16047 && !current_buffer->clip_changed
16048 /* Function force-mode-line-update is used to force a thorough
16049 redisplay. It sets either windows_or_buffers_changed or
16050 update_mode_lines. So don't take a shortcut here for these
16051 cases. */
16052 && !update_mode_lines
16053 && !windows_or_buffers_changed
16054 && !f->cursor_type_changed
16055 && NILP (Vshow_trailing_whitespace)
16056 /* When display-line-numbers is in relative mode, moving point
16057 requires to redraw the entire window. */
16058 && !EQ (Vdisplay_line_numbers, Qrelative)
16059 && !EQ (Vdisplay_line_numbers, Qvisual)
16060 /* When the current line number should be displayed in a
16061 distinct face, moving point cannot be handled in optimized
16062 way as below. */
16063 && !(!NILP (Vdisplay_line_numbers)
16064 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16065 Qline_number_current_line,
16066 w->frame)))
16067 /* This code is not used for mini-buffer for the sake of the case
16068 of redisplaying to replace an echo area message; since in
16069 that case the mini-buffer contents per se are usually
16070 unchanged. This code is of no real use in the mini-buffer
16071 since the handling of this_line_start_pos, etc., in redisplay
16072 handles the same cases. */
16073 && !EQ (window, minibuf_window)
16074 /* When overlay arrow is shown in current buffer, point movement
16075 is no longer "simple", as it typically causes the overlay
16076 arrow to move as well. */
16077 && !overlay_arrow_in_current_buffer_p ())
16079 int this_scroll_margin, top_scroll_margin;
16080 struct glyph_row *row = NULL;
16082 #ifdef GLYPH_DEBUG
16083 debug_method_add (w, "cursor movement");
16084 #endif
16086 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16088 top_scroll_margin = this_scroll_margin;
16089 if (window_wants_header_line (w))
16090 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16092 /* Start with the row the cursor was displayed during the last
16093 not paused redisplay. Give up if that row is not valid. */
16094 if (w->last_cursor_vpos < 0
16095 || w->last_cursor_vpos >= w->current_matrix->nrows)
16096 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16097 else
16099 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16100 if (row->mode_line_p)
16101 ++row;
16102 if (!row->enabled_p)
16103 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16106 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16108 bool scroll_p = false, must_scroll = false;
16109 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16111 if (PT > w->last_point)
16113 /* Point has moved forward. */
16114 while (MATRIX_ROW_END_CHARPOS (row) < PT
16115 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16117 eassert (row->enabled_p);
16118 ++row;
16121 /* If the end position of a row equals the start
16122 position of the next row, and PT is at that position,
16123 we would rather display cursor in the next line. */
16124 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16125 && MATRIX_ROW_END_CHARPOS (row) == PT
16126 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16127 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16128 && !cursor_row_p (row))
16129 ++row;
16131 /* If within the scroll margin, scroll. Note that
16132 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16133 the next line would be drawn, and that
16134 this_scroll_margin can be zero. */
16135 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16136 || PT > MATRIX_ROW_END_CHARPOS (row)
16137 /* Line is completely visible last line in window
16138 and PT is to be set in the next line. */
16139 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16140 && PT == MATRIX_ROW_END_CHARPOS (row)
16141 && !row->ends_at_zv_p
16142 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16143 scroll_p = true;
16145 else if (PT < w->last_point)
16147 /* Cursor has to be moved backward. Note that PT >=
16148 CHARPOS (startp) because of the outer if-statement. */
16149 while (!row->mode_line_p
16150 && (MATRIX_ROW_START_CHARPOS (row) > PT
16151 || (MATRIX_ROW_START_CHARPOS (row) == PT
16152 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16153 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16154 row > w->current_matrix->rows
16155 && (row-1)->ends_in_newline_from_string_p))))
16156 && (row->y > top_scroll_margin
16157 || CHARPOS (startp) == BEGV))
16159 eassert (row->enabled_p);
16160 --row;
16163 /* Consider the following case: Window starts at BEGV,
16164 there is invisible, intangible text at BEGV, so that
16165 display starts at some point START > BEGV. It can
16166 happen that we are called with PT somewhere between
16167 BEGV and START. Try to handle that case. */
16168 if (row < w->current_matrix->rows
16169 || row->mode_line_p)
16171 row = w->current_matrix->rows;
16172 if (row->mode_line_p)
16173 ++row;
16176 /* Due to newlines in overlay strings, we may have to
16177 skip forward over overlay strings. */
16178 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16179 && MATRIX_ROW_END_CHARPOS (row) == PT
16180 && !cursor_row_p (row))
16181 ++row;
16183 /* If within the scroll margin, scroll. */
16184 if (row->y < top_scroll_margin
16185 && CHARPOS (startp) != BEGV)
16186 scroll_p = true;
16188 else
16190 /* Cursor did not move. So don't scroll even if cursor line
16191 is partially visible, as it was so before. */
16192 rc = CURSOR_MOVEMENT_SUCCESS;
16195 if (PT < MATRIX_ROW_START_CHARPOS (row)
16196 || PT > MATRIX_ROW_END_CHARPOS (row))
16198 /* if PT is not in the glyph row, give up. */
16199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16200 must_scroll = true;
16202 else if (rc != CURSOR_MOVEMENT_SUCCESS
16203 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16205 struct glyph_row *row1;
16207 /* If rows are bidi-reordered and point moved, back up
16208 until we find a row that does not belong to a
16209 continuation line. This is because we must consider
16210 all rows of a continued line as candidates for the
16211 new cursor positioning, since row start and end
16212 positions change non-linearly with vertical position
16213 in such rows. */
16214 /* FIXME: Revisit this when glyph ``spilling'' in
16215 continuation lines' rows is implemented for
16216 bidi-reordered rows. */
16217 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16218 MATRIX_ROW_CONTINUATION_LINE_P (row);
16219 --row)
16221 /* If we hit the beginning of the displayed portion
16222 without finding the first row of a continued
16223 line, give up. */
16224 if (row <= row1)
16226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16227 break;
16229 eassert (row->enabled_p);
16232 if (must_scroll)
16234 else if (rc != CURSOR_MOVEMENT_SUCCESS
16235 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16236 /* Make sure this isn't a header line by any chance, since
16237 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16238 && !row->mode_line_p
16239 && make_cursor_line_fully_visible_p)
16241 if (PT == MATRIX_ROW_END_CHARPOS (row)
16242 && !row->ends_at_zv_p
16243 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16244 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16245 else if (row->height > window_box_height (w))
16247 /* If we end up in a partially visible line, let's
16248 make it fully visible, except when it's taller
16249 than the window, in which case we can't do much
16250 about it. */
16251 *scroll_step = true;
16252 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16254 else
16256 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16257 if (!cursor_row_fully_visible_p (w, false, true))
16258 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16259 else
16260 rc = CURSOR_MOVEMENT_SUCCESS;
16263 else if (scroll_p)
16264 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16265 else if (rc != CURSOR_MOVEMENT_SUCCESS
16266 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16268 /* With bidi-reordered rows, there could be more than
16269 one candidate row whose start and end positions
16270 occlude point. We need to let set_cursor_from_row
16271 find the best candidate. */
16272 /* FIXME: Revisit this when glyph ``spilling'' in
16273 continuation lines' rows is implemented for
16274 bidi-reordered rows. */
16275 bool rv = false;
16279 bool at_zv_p = false, exact_match_p = false;
16281 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16282 && PT <= MATRIX_ROW_END_CHARPOS (row)
16283 && cursor_row_p (row))
16284 rv |= set_cursor_from_row (w, row, w->current_matrix,
16285 0, 0, 0, 0);
16286 /* As soon as we've found the exact match for point,
16287 or the first suitable row whose ends_at_zv_p flag
16288 is set, we are done. */
16289 if (rv)
16291 at_zv_p = MATRIX_ROW (w->current_matrix,
16292 w->cursor.vpos)->ends_at_zv_p;
16293 if (!at_zv_p
16294 && w->cursor.hpos >= 0
16295 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16296 w->cursor.vpos))
16298 struct glyph_row *candidate =
16299 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16300 struct glyph *g =
16301 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16302 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16304 exact_match_p =
16305 (BUFFERP (g->object) && g->charpos == PT)
16306 || (NILP (g->object)
16307 && (g->charpos == PT
16308 || (g->charpos == 0 && endpos - 1 == PT)));
16310 if (at_zv_p || exact_match_p)
16312 rc = CURSOR_MOVEMENT_SUCCESS;
16313 break;
16316 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16317 break;
16318 ++row;
16320 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16321 || row->continued_p)
16322 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16323 || (MATRIX_ROW_START_CHARPOS (row) == PT
16324 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16325 /* If we didn't find any candidate rows, or exited the
16326 loop before all the candidates were examined, signal
16327 to the caller that this method failed. */
16328 if (rc != CURSOR_MOVEMENT_SUCCESS
16329 && !(rv
16330 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16331 && !row->continued_p))
16332 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16333 else if (rv)
16334 rc = CURSOR_MOVEMENT_SUCCESS;
16336 else
16340 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16342 rc = CURSOR_MOVEMENT_SUCCESS;
16343 break;
16345 ++row;
16347 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16348 && MATRIX_ROW_START_CHARPOS (row) == PT
16349 && cursor_row_p (row));
16354 return rc;
16358 void
16359 set_vertical_scroll_bar (struct window *w)
16361 ptrdiff_t start, end, whole;
16363 /* Calculate the start and end positions for the current window.
16364 At some point, it would be nice to choose between scrollbars
16365 which reflect the whole buffer size, with special markers
16366 indicating narrowing, and scrollbars which reflect only the
16367 visible region.
16369 Note that mini-buffers sometimes aren't displaying any text. */
16370 if (!MINI_WINDOW_P (w)
16371 || (w == XWINDOW (minibuf_window)
16372 && NILP (echo_area_buffer[0])))
16374 struct buffer *buf = XBUFFER (w->contents);
16375 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16376 start = marker_position (w->start) - BUF_BEGV (buf);
16377 /* I don't think this is guaranteed to be right. For the
16378 moment, we'll pretend it is. */
16379 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16381 if (end < start)
16382 end = start;
16383 if (whole < (end - start))
16384 whole = end - start;
16386 else
16387 start = end = whole = 0;
16389 /* Indicate what this scroll bar ought to be displaying now. */
16390 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16391 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16392 (w, end - start, whole, start);
16396 void
16397 set_horizontal_scroll_bar (struct window *w)
16399 int start, end, whole, portion;
16401 if (!MINI_WINDOW_P (w)
16402 || (w == XWINDOW (minibuf_window)
16403 && NILP (echo_area_buffer[0])))
16405 struct buffer *b = XBUFFER (w->contents);
16406 struct buffer *old_buffer = NULL;
16407 struct it it;
16408 struct text_pos startp;
16410 if (b != current_buffer)
16412 old_buffer = current_buffer;
16413 set_buffer_internal (b);
16416 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16417 start_display (&it, w, startp);
16418 it.last_visible_x = INT_MAX;
16419 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16420 MOVE_TO_X | MOVE_TO_Y);
16421 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16422 window_box_height (w), -1,
16423 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16425 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16426 end = start + window_box_width (w, TEXT_AREA);
16427 portion = end - start;
16428 /* After enlarging a horizontally scrolled window such that it
16429 gets at least as wide as the text it contains, make sure that
16430 the thumb doesn't fill the entire scroll bar so we can still
16431 drag it back to see the entire text. */
16432 whole = max (whole, end);
16434 if (it.bidi_p)
16436 Lisp_Object pdir;
16438 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16439 if (EQ (pdir, Qright_to_left))
16441 start = whole - end;
16442 end = start + portion;
16446 if (old_buffer)
16447 set_buffer_internal (old_buffer);
16449 else
16450 start = end = whole = portion = 0;
16452 w->hscroll_whole = whole;
16454 /* Indicate what this scroll bar ought to be displaying now. */
16455 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16456 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16457 (w, portion, whole, start);
16461 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16462 selected_window is redisplayed.
16464 We can return without actually redisplaying the window if fonts has been
16465 changed on window's frame. In that case, redisplay_internal will retry.
16467 As one of the important parts of redisplaying a window, we need to
16468 decide whether the previous window-start position (stored in the
16469 window's w->start marker position) is still valid, and if it isn't,
16470 recompute it. Some details about that:
16472 . The previous window-start could be in a continuation line, in
16473 which case we need to recompute it when the window width
16474 changes. See compute_window_start_on_continuation_line and its
16475 call below.
16477 . The text that changed since last redisplay could include the
16478 previous window-start position. In that case, we try to salvage
16479 what we can from the current glyph matrix by calling
16480 try_scrolling, which see.
16482 . Some Emacs command could force us to use a specific window-start
16483 position by setting the window's force_start flag, or gently
16484 propose doing that by setting the window's optional_new_start
16485 flag. In these cases, we try using the specified start point if
16486 that succeeds (i.e. the window desired matrix is successfully
16487 recomputed, and point location is within the window). In case
16488 of optional_new_start, we first check if the specified start
16489 position is feasible, i.e. if it will allow point to be
16490 displayed in the window. If using the specified start point
16491 fails, e.g., if new fonts are needed to be loaded, we abort the
16492 redisplay cycle and leave it up to the next cycle to figure out
16493 things.
16495 . Note that the window's force_start flag is sometimes set by
16496 redisplay itself, when it decides that the previous window start
16497 point is fine and should be kept. Search for "goto force_start"
16498 below to see the details. Like the values of window-start
16499 specified outside of redisplay, these internally-deduced values
16500 are tested for feasibility, and ignored if found to be
16501 unfeasible.
16503 . Note that the function try_window, used to completely redisplay
16504 a window, accepts the window's start point as its argument.
16505 This is used several times in the redisplay code to control
16506 where the window start will be, according to user options such
16507 as scroll-conservatively, and also to ensure the screen line
16508 showing point will be fully (as opposed to partially) visible on
16509 display. */
16511 static void
16512 redisplay_window (Lisp_Object window, bool just_this_one_p)
16514 struct window *w = XWINDOW (window);
16515 struct frame *f = XFRAME (w->frame);
16516 struct buffer *buffer = XBUFFER (w->contents);
16517 struct buffer *old = current_buffer;
16518 struct text_pos lpoint, opoint, startp;
16519 bool update_mode_line;
16520 int tem;
16521 struct it it;
16522 /* Record it now because it's overwritten. */
16523 bool current_matrix_up_to_date_p = false;
16524 bool used_current_matrix_p = false;
16525 /* This is less strict than current_matrix_up_to_date_p.
16526 It indicates that the buffer contents and narrowing are unchanged. */
16527 bool buffer_unchanged_p = false;
16528 bool temp_scroll_step = false;
16529 ptrdiff_t count = SPECPDL_INDEX ();
16530 int rc;
16531 int centering_position = -1;
16532 bool last_line_misfit = false;
16533 ptrdiff_t beg_unchanged, end_unchanged;
16534 int frame_line_height, margin;
16535 bool use_desired_matrix;
16536 void *itdata = NULL;
16538 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16539 opoint = lpoint;
16541 #ifdef GLYPH_DEBUG
16542 *w->desired_matrix->method = 0;
16543 #endif
16545 if (!just_this_one_p
16546 && REDISPLAY_SOME_P ()
16547 && !w->redisplay
16548 && !w->update_mode_line
16549 && !f->face_change
16550 && !f->redisplay
16551 && !buffer->text->redisplay
16552 && BUF_PT (buffer) == w->last_point)
16553 return;
16555 /* Make sure that both W's markers are valid. */
16556 eassert (XMARKER (w->start)->buffer == buffer);
16557 eassert (XMARKER (w->pointm)->buffer == buffer);
16559 reconsider_clip_changes (w);
16560 frame_line_height = default_line_pixel_height (w);
16561 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16564 /* Has the mode line to be updated? */
16565 update_mode_line = (w->update_mode_line
16566 || update_mode_lines
16567 || buffer->clip_changed
16568 || buffer->prevent_redisplay_optimizations_p);
16570 if (!just_this_one_p)
16571 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16572 cleverly elsewhere. */
16573 w->must_be_updated_p = true;
16575 if (MINI_WINDOW_P (w))
16577 if (w == XWINDOW (echo_area_window)
16578 && !NILP (echo_area_buffer[0]))
16580 if (update_mode_line)
16581 /* We may have to update a tty frame's menu bar or a
16582 tool-bar. Example `M-x C-h C-h C-g'. */
16583 goto finish_menu_bars;
16584 else
16585 /* We've already displayed the echo area glyphs in this window. */
16586 goto finish_scroll_bars;
16588 else if ((w != XWINDOW (minibuf_window)
16589 || minibuf_level == 0)
16590 /* When buffer is nonempty, redisplay window normally. */
16591 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16592 /* Quail displays non-mini buffers in minibuffer window.
16593 In that case, redisplay the window normally. */
16594 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16596 /* W is a mini-buffer window, but it's not active, so clear
16597 it. */
16598 int yb = window_text_bottom_y (w);
16599 struct glyph_row *row;
16600 int y;
16602 for (y = 0, row = w->desired_matrix->rows;
16603 y < yb;
16604 y += row->height, ++row)
16605 blank_row (w, row, y);
16606 goto finish_scroll_bars;
16609 clear_glyph_matrix (w->desired_matrix);
16612 /* Otherwise set up data on this window; select its buffer and point
16613 value. */
16614 /* Really select the buffer, for the sake of buffer-local
16615 variables. */
16616 set_buffer_internal_1 (XBUFFER (w->contents));
16618 current_matrix_up_to_date_p
16619 = (w->window_end_valid
16620 && !current_buffer->clip_changed
16621 && !current_buffer->prevent_redisplay_optimizations_p
16622 && !window_outdated (w)
16623 && !hscrolling_current_line_p (w));
16625 beg_unchanged = BEG_UNCHANGED;
16626 end_unchanged = END_UNCHANGED;
16628 SET_TEXT_POS (opoint, PT, PT_BYTE);
16630 specbind (Qinhibit_point_motion_hooks, Qt);
16632 buffer_unchanged_p
16633 = (w->window_end_valid
16634 && !current_buffer->clip_changed
16635 && !window_outdated (w));
16637 /* When windows_or_buffers_changed is non-zero, we can't rely
16638 on the window end being valid, so set it to zero there. */
16639 if (windows_or_buffers_changed)
16641 /* If window starts on a continuation line, maybe adjust the
16642 window start in case the window's width changed. */
16643 if (XMARKER (w->start)->buffer == current_buffer)
16644 compute_window_start_on_continuation_line (w);
16646 w->window_end_valid = false;
16647 /* If so, we also can't rely on current matrix
16648 and should not fool try_cursor_movement below. */
16649 current_matrix_up_to_date_p = false;
16652 /* Some sanity checks. */
16653 CHECK_WINDOW_END (w);
16654 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16655 emacs_abort ();
16656 if (BYTEPOS (opoint) < CHARPOS (opoint))
16657 emacs_abort ();
16659 if (mode_line_update_needed (w))
16660 update_mode_line = true;
16662 /* Point refers normally to the selected window. For any other
16663 window, set up appropriate value. */
16664 if (!EQ (window, selected_window))
16666 ptrdiff_t new_pt = marker_position (w->pointm);
16667 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16669 if (new_pt < BEGV)
16671 new_pt = BEGV;
16672 new_pt_byte = BEGV_BYTE;
16673 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16675 else if (new_pt > (ZV - 1))
16677 new_pt = ZV;
16678 new_pt_byte = ZV_BYTE;
16679 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16682 /* We don't use SET_PT so that the point-motion hooks don't run. */
16683 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16686 /* If any of the character widths specified in the display table
16687 have changed, invalidate the width run cache. It's true that
16688 this may be a bit late to catch such changes, but the rest of
16689 redisplay goes (non-fatally) haywire when the display table is
16690 changed, so why should we worry about doing any better? */
16691 if (current_buffer->width_run_cache
16692 || (current_buffer->base_buffer
16693 && current_buffer->base_buffer->width_run_cache))
16695 struct Lisp_Char_Table *disptab = buffer_display_table ();
16697 if (! disptab_matches_widthtab
16698 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16700 struct buffer *buf = current_buffer;
16702 if (buf->base_buffer)
16703 buf = buf->base_buffer;
16704 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16705 recompute_width_table (current_buffer, disptab);
16709 /* If window-start is screwed up, choose a new one. */
16710 if (XMARKER (w->start)->buffer != current_buffer)
16711 goto recenter;
16713 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16715 /* If someone specified a new starting point but did not insist,
16716 check whether it can be used. */
16717 if ((w->optional_new_start || window_frozen_p (w))
16718 && CHARPOS (startp) >= BEGV
16719 && CHARPOS (startp) <= ZV)
16721 ptrdiff_t it_charpos;
16723 w->optional_new_start = false;
16724 start_display (&it, w, startp);
16725 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16726 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16727 /* Record IT's position now, since line_bottom_y might change
16728 that. */
16729 it_charpos = IT_CHARPOS (it);
16730 /* Make sure we set the force_start flag only if the cursor row
16731 will be fully visible. Otherwise, the code under force_start
16732 label below will try to move point back into view, which is
16733 not what the code which sets optional_new_start wants. */
16734 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16735 && !w->force_start)
16737 if (it_charpos == PT)
16738 w->force_start = true;
16739 /* IT may overshoot PT if text at PT is invisible. */
16740 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16741 w->force_start = true;
16742 #ifdef GLYPH_DEBUG
16743 if (w->force_start)
16745 if (window_frozen_p (w))
16746 debug_method_add (w, "set force_start from frozen window start");
16747 else
16748 debug_method_add (w, "set force_start from optional_new_start");
16750 #endif
16754 force_start:
16756 /* Handle case where place to start displaying has been specified,
16757 unless the specified location is outside the accessible range. */
16758 if (w->force_start)
16760 /* We set this later on if we have to adjust point. */
16761 int new_vpos = -1;
16763 w->force_start = false;
16764 w->vscroll = 0;
16765 w->window_end_valid = false;
16767 /* Forget any recorded base line for line number display. */
16768 if (!buffer_unchanged_p)
16769 w->base_line_number = 0;
16771 /* Redisplay the mode line. Select the buffer properly for that.
16772 Also, run the hook window-scroll-functions
16773 because we have scrolled. */
16774 /* Note, we do this after clearing force_start because
16775 if there's an error, it is better to forget about force_start
16776 than to get into an infinite loop calling the hook functions
16777 and having them get more errors. */
16778 if (!update_mode_line
16779 || ! NILP (Vwindow_scroll_functions))
16781 update_mode_line = true;
16782 w->update_mode_line = true;
16783 startp = run_window_scroll_functions (window, startp);
16786 if (CHARPOS (startp) < BEGV)
16787 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16788 else if (CHARPOS (startp) > ZV)
16789 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16791 /* Redisplay, then check if cursor has been set during the
16792 redisplay. Give up if new fonts were loaded. */
16793 /* We used to issue a CHECK_MARGINS argument to try_window here,
16794 but this causes scrolling to fail when point begins inside
16795 the scroll margin (bug#148) -- cyd */
16796 if (!try_window (window, startp, 0))
16798 w->force_start = true;
16799 clear_glyph_matrix (w->desired_matrix);
16800 goto need_larger_matrices;
16803 if (w->cursor.vpos < 0)
16805 /* If point does not appear, try to move point so it does
16806 appear. The desired matrix has been built above, so we
16807 can use it here. First see if point is in invisible
16808 text, and if so, move it to the first visible buffer
16809 position past that. */
16810 struct glyph_row *r = NULL;
16811 Lisp_Object invprop =
16812 get_char_property_and_overlay (make_number (PT), Qinvisible,
16813 Qnil, NULL);
16815 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16817 ptrdiff_t alt_pt;
16818 Lisp_Object invprop_end =
16819 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16820 Qnil, Qnil);
16822 if (NATNUMP (invprop_end))
16823 alt_pt = XFASTINT (invprop_end);
16824 else
16825 alt_pt = ZV;
16826 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16827 NULL, 0);
16829 if (r)
16830 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16831 else /* Give up and just move to the middle of the window. */
16832 new_vpos = window_box_height (w) / 2;
16835 if (!cursor_row_fully_visible_p (w, false, false))
16837 /* Point does appear, but on a line partly visible at end of window.
16838 Move it back to a fully-visible line. */
16839 new_vpos = window_box_height (w);
16840 /* But if window_box_height suggests a Y coordinate that is
16841 not less than we already have, that line will clearly not
16842 be fully visible, so give up and scroll the display.
16843 This can happen when the default face uses a font whose
16844 dimensions are different from the frame's default
16845 font. */
16846 if (new_vpos >= w->cursor.y)
16848 w->cursor.vpos = -1;
16849 clear_glyph_matrix (w->desired_matrix);
16850 goto try_to_scroll;
16853 else if (w->cursor.vpos >= 0)
16855 /* Some people insist on not letting point enter the scroll
16856 margin, even though this part handles windows that didn't
16857 scroll at all. */
16858 int pixel_margin = margin * frame_line_height;
16859 bool header_line = window_wants_header_line (w);
16861 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16862 below, which finds the row to move point to, advances by
16863 the Y coordinate of the _next_ row, see the definition of
16864 MATRIX_ROW_BOTTOM_Y. */
16865 if (w->cursor.vpos < margin + header_line)
16867 w->cursor.vpos = -1;
16868 clear_glyph_matrix (w->desired_matrix);
16869 goto try_to_scroll;
16871 else
16873 int window_height = window_box_height (w);
16875 if (header_line)
16876 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16877 if (w->cursor.y >= window_height - pixel_margin)
16879 w->cursor.vpos = -1;
16880 clear_glyph_matrix (w->desired_matrix);
16881 goto try_to_scroll;
16886 /* If we need to move point for either of the above reasons,
16887 now actually do it. */
16888 if (new_vpos >= 0)
16890 struct glyph_row *row;
16892 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16893 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16894 ++row;
16896 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16897 MATRIX_ROW_START_BYTEPOS (row));
16899 if (w != XWINDOW (selected_window))
16900 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16901 else if (current_buffer == old)
16902 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16904 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16906 /* Re-run pre-redisplay-function so it can update the region
16907 according to the new position of point. */
16908 /* Other than the cursor, w's redisplay is done so we can set its
16909 redisplay to false. Also the buffer's redisplay can be set to
16910 false, since propagate_buffer_redisplay should have already
16911 propagated its info to `w' anyway. */
16912 w->redisplay = false;
16913 XBUFFER (w->contents)->text->redisplay = false;
16914 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16916 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16917 || ((EQ (Vdisplay_line_numbers, Qrelative)
16918 || EQ (Vdisplay_line_numbers, Qvisual))
16919 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16921 /* Either pre-redisplay-function made changes (e.g. move
16922 the region), or we moved point in a window that is
16923 under display-line-numbers = relative mode. We need
16924 another round of redisplay. */
16925 clear_glyph_matrix (w->desired_matrix);
16926 if (!try_window (window, startp, 0))
16927 goto need_larger_matrices;
16930 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16932 clear_glyph_matrix (w->desired_matrix);
16933 goto try_to_scroll;
16936 #ifdef GLYPH_DEBUG
16937 debug_method_add (w, "forced window start");
16938 #endif
16939 goto done;
16942 /* Handle case where text has not changed, only point, and it has
16943 not moved off the frame, and we are not retrying after hscroll.
16944 (current_matrix_up_to_date_p is true when retrying.) */
16945 if (current_matrix_up_to_date_p
16946 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16947 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16949 switch (rc)
16951 case CURSOR_MOVEMENT_SUCCESS:
16952 used_current_matrix_p = true;
16953 goto done;
16955 case CURSOR_MOVEMENT_MUST_SCROLL:
16956 goto try_to_scroll;
16958 default:
16959 emacs_abort ();
16962 /* If current starting point was originally the beginning of a line
16963 but no longer is, find a new starting point. */
16964 else if (w->start_at_line_beg
16965 && !(CHARPOS (startp) <= BEGV
16966 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16968 #ifdef GLYPH_DEBUG
16969 debug_method_add (w, "recenter 1");
16970 #endif
16971 goto recenter;
16974 /* Try scrolling with try_window_id. Value is > 0 if update has
16975 been done, it is -1 if we know that the same window start will
16976 not work. It is 0 if unsuccessful for some other reason. */
16977 else if ((tem = try_window_id (w)) != 0)
16979 #ifdef GLYPH_DEBUG
16980 debug_method_add (w, "try_window_id %d", tem);
16981 #endif
16983 if (f->fonts_changed)
16984 goto need_larger_matrices;
16985 if (tem > 0)
16986 goto done;
16988 /* Otherwise try_window_id has returned -1 which means that we
16989 don't want the alternative below this comment to execute. */
16991 else if (CHARPOS (startp) >= BEGV
16992 && CHARPOS (startp) <= ZV
16993 && PT >= CHARPOS (startp)
16994 && (CHARPOS (startp) < ZV
16995 /* Avoid starting at end of buffer. */
16996 || CHARPOS (startp) == BEGV
16997 || !window_outdated (w)))
16999 int d1, d2, d5, d6;
17000 int rtop, rbot;
17002 /* If first window line is a continuation line, and window start
17003 is inside the modified region, but the first change is before
17004 current window start, we must select a new window start.
17006 However, if this is the result of a down-mouse event (e.g. by
17007 extending the mouse-drag-overlay), we don't want to select a
17008 new window start, since that would change the position under
17009 the mouse, resulting in an unwanted mouse-movement rather
17010 than a simple mouse-click. */
17011 if (!w->start_at_line_beg
17012 && NILP (do_mouse_tracking)
17013 && CHARPOS (startp) > BEGV
17014 && CHARPOS (startp) > BEG + beg_unchanged
17015 && CHARPOS (startp) <= Z - end_unchanged
17016 /* Even if w->start_at_line_beg is nil, a new window may
17017 start at a line_beg, since that's how set_buffer_window
17018 sets it. So, we need to check the return value of
17019 compute_window_start_on_continuation_line. (See also
17020 bug#197). */
17021 && XMARKER (w->start)->buffer == current_buffer
17022 && compute_window_start_on_continuation_line (w)
17023 /* It doesn't make sense to force the window start like we
17024 do at label force_start if it is already known that point
17025 will not be fully visible in the resulting window, because
17026 doing so will move point from its correct position
17027 instead of scrolling the window to bring point into view.
17028 See bug#9324. */
17029 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17030 /* A very tall row could need more than the window height,
17031 in which case we accept that it is partially visible. */
17032 && (rtop != 0) == (rbot != 0))
17034 w->force_start = true;
17035 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17036 #ifdef GLYPH_DEBUG
17037 debug_method_add (w, "recomputed window start in continuation line");
17038 #endif
17039 goto force_start;
17042 #ifdef GLYPH_DEBUG
17043 debug_method_add (w, "same window start");
17044 #endif
17046 /* Try to redisplay starting at same place as before.
17047 If point has not moved off frame, accept the results. */
17048 if (!current_matrix_up_to_date_p
17049 /* Don't use try_window_reusing_current_matrix in this case
17050 because a window scroll function can have changed the
17051 buffer. */
17052 || !NILP (Vwindow_scroll_functions)
17053 || MINI_WINDOW_P (w)
17054 || !(used_current_matrix_p
17055 = try_window_reusing_current_matrix (w)))
17057 IF_DEBUG (debug_method_add (w, "1"));
17058 clear_glyph_matrix (w->desired_matrix);
17059 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17060 /* -1 means we need to scroll.
17061 0 means we need new matrices, but fonts_changed
17062 is set in that case, so we will detect it below. */
17063 goto try_to_scroll;
17066 if (f->fonts_changed)
17067 goto need_larger_matrices;
17069 if (w->cursor.vpos >= 0)
17071 if (!just_this_one_p
17072 || current_buffer->clip_changed
17073 || BEG_UNCHANGED < CHARPOS (startp))
17074 /* Forget any recorded base line for line number display. */
17075 w->base_line_number = 0;
17077 if (!cursor_row_fully_visible_p (w, true, false))
17079 clear_glyph_matrix (w->desired_matrix);
17080 last_line_misfit = true;
17082 /* Drop through and scroll. */
17083 else
17084 goto done;
17086 else
17087 clear_glyph_matrix (w->desired_matrix);
17090 try_to_scroll:
17092 /* Redisplay the mode line. Select the buffer properly for that. */
17093 if (!update_mode_line)
17095 update_mode_line = true;
17096 w->update_mode_line = true;
17099 /* Try to scroll by specified few lines. */
17100 if ((scroll_conservatively
17101 || emacs_scroll_step
17102 || temp_scroll_step
17103 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17104 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17105 && CHARPOS (startp) >= BEGV
17106 && CHARPOS (startp) <= ZV)
17108 /* The function returns -1 if new fonts were loaded, 1 if
17109 successful, 0 if not successful. */
17110 int ss = try_scrolling (window, just_this_one_p,
17111 scroll_conservatively,
17112 emacs_scroll_step,
17113 temp_scroll_step, last_line_misfit);
17114 switch (ss)
17116 case SCROLLING_SUCCESS:
17117 goto done;
17119 case SCROLLING_NEED_LARGER_MATRICES:
17120 goto need_larger_matrices;
17122 case SCROLLING_FAILED:
17123 break;
17125 default:
17126 emacs_abort ();
17130 /* Finally, just choose a place to start which positions point
17131 according to user preferences. */
17133 recenter:
17135 #ifdef GLYPH_DEBUG
17136 debug_method_add (w, "recenter");
17137 #endif
17139 /* Forget any previously recorded base line for line number display. */
17140 if (!buffer_unchanged_p)
17141 w->base_line_number = 0;
17143 /* Determine the window start relative to point. */
17144 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17145 it.current_y = it.last_visible_y;
17146 if (centering_position < 0)
17148 ptrdiff_t margin_pos = CHARPOS (startp);
17149 Lisp_Object aggressive;
17150 bool scrolling_up;
17152 /* If there is a scroll margin at the top of the window, find
17153 its character position. */
17154 if (margin
17155 /* Cannot call start_display if startp is not in the
17156 accessible region of the buffer. This can happen when we
17157 have just switched to a different buffer and/or changed
17158 its restriction. In that case, startp is initialized to
17159 the character position 1 (BEGV) because we did not yet
17160 have chance to display the buffer even once. */
17161 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17163 struct it it1;
17164 void *it1data = NULL;
17166 SAVE_IT (it1, it, it1data);
17167 start_display (&it1, w, startp);
17168 move_it_vertically (&it1, margin * frame_line_height);
17169 margin_pos = IT_CHARPOS (it1);
17170 RESTORE_IT (&it, &it, it1data);
17172 scrolling_up = PT > margin_pos;
17173 aggressive =
17174 scrolling_up
17175 ? BVAR (current_buffer, scroll_up_aggressively)
17176 : BVAR (current_buffer, scroll_down_aggressively);
17178 if (!MINI_WINDOW_P (w)
17179 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17181 int pt_offset = 0;
17183 /* Setting scroll-conservatively overrides
17184 scroll-*-aggressively. */
17185 if (!scroll_conservatively && NUMBERP (aggressive))
17187 double float_amount = XFLOATINT (aggressive);
17189 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17190 if (pt_offset == 0 && float_amount > 0)
17191 pt_offset = 1;
17192 if (pt_offset && margin > 0)
17193 margin -= 1;
17195 /* Compute how much to move the window start backward from
17196 point so that point will be displayed where the user
17197 wants it. */
17198 if (scrolling_up)
17200 centering_position = it.last_visible_y;
17201 if (pt_offset)
17202 centering_position -= pt_offset;
17203 centering_position -=
17204 (frame_line_height * (1 + margin + last_line_misfit)
17205 + WINDOW_HEADER_LINE_HEIGHT (w));
17206 /* Don't let point enter the scroll margin near top of
17207 the window. */
17208 if (centering_position < margin * frame_line_height)
17209 centering_position = margin * frame_line_height;
17211 else
17212 centering_position = margin * frame_line_height + pt_offset;
17214 else
17215 /* Set the window start half the height of the window backward
17216 from point. */
17217 centering_position = window_box_height (w) / 2;
17219 move_it_vertically_backward (&it, centering_position);
17221 eassert (IT_CHARPOS (it) >= BEGV);
17223 /* The function move_it_vertically_backward may move over more
17224 than the specified y-distance. If it->w is small, e.g. a
17225 mini-buffer window, we may end up in front of the window's
17226 display area. Start displaying at the start of the line
17227 containing PT in this case. */
17228 if (it.current_y <= 0)
17230 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17231 move_it_vertically_backward (&it, 0);
17232 it.current_y = 0;
17235 it.current_x = it.hpos = 0;
17237 /* Set the window start position here explicitly, to avoid an
17238 infinite loop in case the functions in window-scroll-functions
17239 get errors. */
17240 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17242 /* Run scroll hooks. */
17243 startp = run_window_scroll_functions (window, it.current.pos);
17245 /* We invoke try_window and try_window_reusing_current_matrix below,
17246 and they manipulate the bidi cache. Save and restore the cache
17247 state of our iterator, so we could continue using it after that. */
17248 itdata = bidi_shelve_cache ();
17250 /* Redisplay the window. */
17251 use_desired_matrix = false;
17252 if (!current_matrix_up_to_date_p
17253 || windows_or_buffers_changed
17254 || f->cursor_type_changed
17255 /* Don't use try_window_reusing_current_matrix in this case
17256 because it can have changed the buffer. */
17257 || !NILP (Vwindow_scroll_functions)
17258 || !just_this_one_p
17259 || MINI_WINDOW_P (w)
17260 || !(used_current_matrix_p
17261 = try_window_reusing_current_matrix (w)))
17262 use_desired_matrix = (try_window (window, startp, 0) == 1);
17264 bidi_unshelve_cache (itdata, false);
17266 /* If new fonts have been loaded (due to fontsets), give up. We
17267 have to start a new redisplay since we need to re-adjust glyph
17268 matrices. */
17269 if (f->fonts_changed)
17270 goto need_larger_matrices;
17272 /* If cursor did not appear assume that the middle of the window is
17273 in the first line of the window. Do it again with the next line.
17274 (Imagine a window of height 100, displaying two lines of height
17275 60. Moving back 50 from it->last_visible_y will end in the first
17276 line.) */
17277 if (w->cursor.vpos < 0)
17279 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17281 clear_glyph_matrix (w->desired_matrix);
17282 move_it_by_lines (&it, 1);
17283 try_window (window, it.current.pos, 0);
17285 else if (PT < IT_CHARPOS (it))
17287 clear_glyph_matrix (w->desired_matrix);
17288 move_it_by_lines (&it, -1);
17289 try_window (window, it.current.pos, 0);
17291 else if (scroll_conservatively > SCROLL_LIMIT
17292 && (it.method == GET_FROM_STRING
17293 || overlay_touches_p (IT_CHARPOS (it)))
17294 && IT_CHARPOS (it) < ZV)
17296 /* If the window starts with a before-string that spans more
17297 than one screen line, using that position to display the
17298 window might fail to bring point into the view, because
17299 start_display will always start by displaying the string,
17300 whereas the code above determines where to set w->start
17301 by the buffer position of the place where it takes screen
17302 coordinates. Try to recover by finding the next screen
17303 line that displays buffer text. */
17304 ptrdiff_t pos0 = IT_CHARPOS (it);
17306 clear_glyph_matrix (w->desired_matrix);
17307 do {
17308 move_it_by_lines (&it, 1);
17309 } while (IT_CHARPOS (it) == pos0);
17310 try_window (window, it.current.pos, 0);
17312 else
17314 /* Not much we can do about it. */
17318 /* Consider the following case: Window starts at BEGV, there is
17319 invisible, intangible text at BEGV, so that display starts at
17320 some point START > BEGV. It can happen that we are called with
17321 PT somewhere between BEGV and START. Try to handle that case,
17322 and similar ones. */
17323 if (w->cursor.vpos < 0)
17325 /* Prefer the desired matrix to the current matrix, if possible,
17326 in the fallback calculations below. This is because using
17327 the current matrix might completely goof, e.g. if its first
17328 row is after point. */
17329 struct glyph_matrix *matrix =
17330 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17331 /* First, try locating the proper glyph row for PT. */
17332 struct glyph_row *row =
17333 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17335 /* Sometimes point is at the beginning of invisible text that is
17336 before the 1st character displayed in the row. In that case,
17337 row_containing_pos fails to find the row, because no glyphs
17338 with appropriate buffer positions are present in the row.
17339 Therefore, we next try to find the row which shows the 1st
17340 position after the invisible text. */
17341 if (!row)
17343 Lisp_Object val =
17344 get_char_property_and_overlay (make_number (PT), Qinvisible,
17345 Qnil, NULL);
17347 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17349 ptrdiff_t alt_pos;
17350 Lisp_Object invis_end =
17351 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17352 Qnil, Qnil);
17354 if (NATNUMP (invis_end))
17355 alt_pos = XFASTINT (invis_end);
17356 else
17357 alt_pos = ZV;
17358 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17361 /* Finally, fall back on the first row of the window after the
17362 header line (if any). This is slightly better than not
17363 displaying the cursor at all. */
17364 if (!row)
17366 row = matrix->rows;
17367 if (row->mode_line_p)
17368 ++row;
17370 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17373 if (!cursor_row_fully_visible_p (w, false, false))
17375 /* If vscroll is enabled, disable it and try again. */
17376 if (w->vscroll)
17378 w->vscroll = 0;
17379 clear_glyph_matrix (w->desired_matrix);
17380 goto recenter;
17383 /* Users who set scroll-conservatively to a large number want
17384 point just above/below the scroll margin. If we ended up
17385 with point's row partially visible, move the window start to
17386 make that row fully visible and out of the margin. */
17387 if (scroll_conservatively > SCROLL_LIMIT)
17389 int window_total_lines
17390 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17391 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17393 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17394 clear_glyph_matrix (w->desired_matrix);
17395 if (1 == try_window (window, it.current.pos,
17396 TRY_WINDOW_CHECK_MARGINS))
17397 goto done;
17400 /* If centering point failed to make the whole line visible,
17401 put point at the top instead. That has to make the whole line
17402 visible, if it can be done. */
17403 if (centering_position == 0)
17404 goto done;
17406 clear_glyph_matrix (w->desired_matrix);
17407 centering_position = 0;
17408 goto recenter;
17411 done:
17413 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17414 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17415 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17417 /* Display the mode line, if we must. */
17418 if ((update_mode_line
17419 /* If window not full width, must redo its mode line
17420 if (a) the window to its side is being redone and
17421 (b) we do a frame-based redisplay. This is a consequence
17422 of how inverted lines are drawn in frame-based redisplay. */
17423 || (!just_this_one_p
17424 && !FRAME_WINDOW_P (f)
17425 && !WINDOW_FULL_WIDTH_P (w))
17426 /* Line number to display. */
17427 || w->base_line_pos > 0
17428 /* Column number is displayed and different from the one displayed. */
17429 || (w->column_number_displayed != -1
17430 && (w->column_number_displayed != current_column ())))
17431 /* This means that the window has a mode line. */
17432 && (window_wants_mode_line (w)
17433 || window_wants_header_line (w)))
17436 display_mode_lines (w);
17438 /* If mode line height has changed, arrange for a thorough
17439 immediate redisplay using the correct mode line height. */
17440 if (window_wants_mode_line (w)
17441 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17443 f->fonts_changed = true;
17444 w->mode_line_height = -1;
17445 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17446 = DESIRED_MODE_LINE_HEIGHT (w);
17449 /* If header line height has changed, arrange for a thorough
17450 immediate redisplay using the correct header line height. */
17451 if (window_wants_header_line (w)
17452 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17454 f->fonts_changed = true;
17455 w->header_line_height = -1;
17456 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17457 = DESIRED_HEADER_LINE_HEIGHT (w);
17460 if (f->fonts_changed)
17461 goto need_larger_matrices;
17464 if (!line_number_displayed && w->base_line_pos != -1)
17466 w->base_line_pos = 0;
17467 w->base_line_number = 0;
17470 finish_menu_bars:
17472 /* When we reach a frame's selected window, redo the frame's menu
17473 bar and the frame's title. */
17474 if (update_mode_line
17475 && EQ (FRAME_SELECTED_WINDOW (f), window))
17477 bool redisplay_menu_p;
17479 if (FRAME_WINDOW_P (f))
17481 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17482 || defined (HAVE_NS) || defined (USE_GTK)
17483 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17484 #else
17485 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17486 #endif
17488 else
17489 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17491 if (redisplay_menu_p)
17492 display_menu_bar (w);
17494 #ifdef HAVE_WINDOW_SYSTEM
17495 if (FRAME_WINDOW_P (f))
17497 #if defined (USE_GTK) || defined (HAVE_NS)
17498 if (FRAME_EXTERNAL_TOOL_BAR (f))
17499 redisplay_tool_bar (f);
17500 #else
17501 if (WINDOWP (f->tool_bar_window)
17502 && (FRAME_TOOL_BAR_LINES (f) > 0
17503 || !NILP (Vauto_resize_tool_bars))
17504 && redisplay_tool_bar (f))
17505 ignore_mouse_drag_p = true;
17506 #endif
17508 x_consider_frame_title (w->frame);
17509 #endif
17512 #ifdef HAVE_WINDOW_SYSTEM
17513 if (FRAME_WINDOW_P (f)
17514 && update_window_fringes (w, (just_this_one_p
17515 || (!used_current_matrix_p && !overlay_arrow_seen)
17516 || w->pseudo_window_p)))
17518 update_begin (f);
17519 block_input ();
17520 if (draw_window_fringes (w, true))
17522 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17523 x_draw_right_divider (w);
17524 else
17525 x_draw_vertical_border (w);
17527 unblock_input ();
17528 update_end (f);
17531 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17532 x_draw_bottom_divider (w);
17533 #endif /* HAVE_WINDOW_SYSTEM */
17535 /* We go to this label, with fonts_changed set, if it is
17536 necessary to try again using larger glyph matrices.
17537 We have to redeem the scroll bar even in this case,
17538 because the loop in redisplay_internal expects that. */
17539 need_larger_matrices:
17541 finish_scroll_bars:
17543 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17545 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17546 /* Set the thumb's position and size. */
17547 set_vertical_scroll_bar (w);
17549 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17550 /* Set the thumb's position and size. */
17551 set_horizontal_scroll_bar (w);
17553 /* Note that we actually used the scroll bar attached to this
17554 window, so it shouldn't be deleted at the end of redisplay. */
17555 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17556 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17559 /* Restore current_buffer and value of point in it. The window
17560 update may have changed the buffer, so first make sure `opoint'
17561 is still valid (Bug#6177). */
17562 if (CHARPOS (opoint) < BEGV)
17563 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17564 else if (CHARPOS (opoint) > ZV)
17565 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17566 else
17567 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17569 set_buffer_internal_1 (old);
17570 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17571 shorter. This can be caused by log truncation in *Messages*. */
17572 if (CHARPOS (lpoint) <= ZV)
17573 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17575 unbind_to (count, Qnil);
17579 /* Build the complete desired matrix of WINDOW with a window start
17580 buffer position POS.
17582 Value is 1 if successful. It is zero if fonts were loaded during
17583 redisplay which makes re-adjusting glyph matrices necessary, and -1
17584 if point would appear in the scroll margins.
17585 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17586 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17587 set in FLAGS.) */
17590 try_window (Lisp_Object window, struct text_pos pos, int flags)
17592 struct window *w = XWINDOW (window);
17593 struct it it;
17594 struct glyph_row *last_text_row = NULL;
17595 struct frame *f = XFRAME (w->frame);
17596 int cursor_vpos = w->cursor.vpos;
17598 /* Make POS the new window start. */
17599 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17601 /* Mark cursor position as unknown. No overlay arrow seen. */
17602 w->cursor.vpos = -1;
17603 overlay_arrow_seen = false;
17605 /* Initialize iterator and info to start at POS. */
17606 start_display (&it, w, pos);
17607 it.glyph_row->reversed_p = false;
17609 /* Display all lines of W. */
17610 while (it.current_y < it.last_visible_y)
17612 if (display_line (&it, cursor_vpos))
17613 last_text_row = it.glyph_row - 1;
17614 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17615 return 0;
17618 /* Save the character position of 'it' before we call
17619 'start_display' again. */
17620 ptrdiff_t it_charpos = IT_CHARPOS (it);
17622 /* Don't let the cursor end in the scroll margins. */
17623 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17624 && !MINI_WINDOW_P (w))
17626 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17627 start_display (&it, w, pos);
17629 if ((w->cursor.y >= 0 /* not vscrolled */
17630 && w->cursor.y < this_scroll_margin
17631 && CHARPOS (pos) > BEGV
17632 && it_charpos < ZV)
17633 /* rms: considering make_cursor_line_fully_visible_p here
17634 seems to give wrong results. We don't want to recenter
17635 when the last line is partly visible, we want to allow
17636 that case to be handled in the usual way. */
17637 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17638 - this_scroll_margin - 1))
17640 w->cursor.vpos = -1;
17641 clear_glyph_matrix (w->desired_matrix);
17642 return -1;
17646 /* If bottom moved off end of frame, change mode line percentage. */
17647 if (w->window_end_pos <= 0 && Z != it_charpos)
17648 w->update_mode_line = true;
17650 /* Set window_end_pos to the offset of the last character displayed
17651 on the window from the end of current_buffer. Set
17652 window_end_vpos to its row number. */
17653 if (last_text_row)
17655 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17656 adjust_window_ends (w, last_text_row, false);
17657 eassert
17658 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17659 w->window_end_vpos)));
17661 else
17663 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17664 w->window_end_pos = Z - ZV;
17665 w->window_end_vpos = 0;
17668 /* But that is not valid info until redisplay finishes. */
17669 w->window_end_valid = false;
17670 return 1;
17675 /************************************************************************
17676 Window redisplay reusing current matrix when buffer has not changed
17677 ************************************************************************/
17679 /* Try redisplay of window W showing an unchanged buffer with a
17680 different window start than the last time it was displayed by
17681 reusing its current matrix. Value is true if successful.
17682 W->start is the new window start. */
17684 static bool
17685 try_window_reusing_current_matrix (struct window *w)
17687 struct frame *f = XFRAME (w->frame);
17688 struct glyph_row *bottom_row;
17689 struct it it;
17690 struct run run;
17691 struct text_pos start, new_start;
17692 int nrows_scrolled, i;
17693 struct glyph_row *last_text_row;
17694 struct glyph_row *last_reused_text_row;
17695 struct glyph_row *start_row;
17696 int start_vpos, min_y, max_y;
17698 #ifdef GLYPH_DEBUG
17699 if (inhibit_try_window_reusing)
17700 return false;
17701 #endif
17703 if (/* This function doesn't handle terminal frames. */
17704 !FRAME_WINDOW_P (f)
17705 /* Don't try to reuse the display if windows have been split
17706 or such. */
17707 || windows_or_buffers_changed
17708 || f->cursor_type_changed
17709 /* This function cannot handle buffers where the overlay arrow
17710 is shown on the fringes, because if the arrow position
17711 changes, we cannot just reuse the current matrix. */
17712 || overlay_arrow_in_current_buffer_p ())
17713 return false;
17715 /* Can't do this if showing trailing whitespace. */
17716 if (!NILP (Vshow_trailing_whitespace))
17717 return false;
17719 /* If top-line visibility has changed, give up. */
17720 if (window_wants_header_line (w)
17721 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17722 return false;
17724 /* Give up if old or new display is scrolled vertically. We could
17725 make this function handle this, but right now it doesn't. */
17726 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17727 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17728 return false;
17730 /* Clear the desired matrix for the display below. */
17731 clear_glyph_matrix (w->desired_matrix);
17733 /* Give up if line numbers are being displayed, because reusing the
17734 current matrix might use the wrong width for line-number
17735 display. */
17736 if (!NILP (Vdisplay_line_numbers))
17737 return false;
17739 /* The variable new_start now holds the new window start. The old
17740 start `start' can be determined from the current matrix. */
17741 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17742 start = start_row->minpos;
17743 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17745 if (CHARPOS (new_start) <= CHARPOS (start))
17747 /* Don't use this method if the display starts with an ellipsis
17748 displayed for invisible text. It's not easy to handle that case
17749 below, and it's certainly not worth the effort since this is
17750 not a frequent case. */
17751 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17752 return false;
17754 IF_DEBUG (debug_method_add (w, "twu1"));
17756 /* Display up to a row that can be reused. The variable
17757 last_text_row is set to the last row displayed that displays
17758 text. Note that it.vpos == 0 if or if not there is a
17759 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17760 start_display (&it, w, new_start);
17761 w->cursor.vpos = -1;
17762 last_text_row = last_reused_text_row = NULL;
17764 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17766 /* If we have reached into the characters in the START row,
17767 that means the line boundaries have changed. So we
17768 can't start copying with the row START. Maybe it will
17769 work to start copying with the following row. */
17770 while (IT_CHARPOS (it) > CHARPOS (start))
17772 /* Advance to the next row as the "start". */
17773 start_row++;
17774 start = start_row->minpos;
17775 /* If there are no more rows to try, or just one, give up. */
17776 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17777 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17778 || CHARPOS (start) == ZV)
17780 clear_glyph_matrix (w->desired_matrix);
17781 return false;
17784 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17786 /* If we have reached alignment, we can copy the rest of the
17787 rows. */
17788 if (IT_CHARPOS (it) == CHARPOS (start)
17789 /* Don't accept "alignment" inside a display vector,
17790 since start_row could have started in the middle of
17791 that same display vector (thus their character
17792 positions match), and we have no way of telling if
17793 that is the case. */
17794 && it.current.dpvec_index < 0)
17795 break;
17797 it.glyph_row->reversed_p = false;
17798 if (display_line (&it, -1))
17799 last_text_row = it.glyph_row - 1;
17803 /* A value of current_y < last_visible_y means that we stopped
17804 at the previous window start, which in turn means that we
17805 have at least one reusable row. */
17806 if (it.current_y < it.last_visible_y)
17808 struct glyph_row *row;
17810 /* IT.vpos always starts from 0; it counts text lines. */
17811 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17813 /* Find PT if not already found in the lines displayed. */
17814 if (w->cursor.vpos < 0)
17816 int dy = it.current_y - start_row->y;
17818 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17819 row = row_containing_pos (w, PT, row, NULL, dy);
17820 if (row)
17821 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17822 dy, nrows_scrolled);
17823 else
17825 clear_glyph_matrix (w->desired_matrix);
17826 return false;
17830 /* Scroll the display. Do it before the current matrix is
17831 changed. The problem here is that update has not yet
17832 run, i.e. part of the current matrix is not up to date.
17833 scroll_run_hook will clear the cursor, and use the
17834 current matrix to get the height of the row the cursor is
17835 in. */
17836 run.current_y = start_row->y;
17837 run.desired_y = it.current_y;
17838 run.height = it.last_visible_y - it.current_y;
17840 if (run.height > 0 && run.current_y != run.desired_y)
17842 update_begin (f);
17843 FRAME_RIF (f)->update_window_begin_hook (w);
17844 FRAME_RIF (f)->clear_window_mouse_face (w);
17845 FRAME_RIF (f)->scroll_run_hook (w, &run);
17846 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17847 update_end (f);
17850 /* Shift current matrix down by nrows_scrolled lines. */
17851 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17852 rotate_matrix (w->current_matrix,
17853 start_vpos,
17854 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17855 nrows_scrolled);
17857 /* Disable lines that must be updated. */
17858 for (i = 0; i < nrows_scrolled; ++i)
17859 (start_row + i)->enabled_p = false;
17861 /* Re-compute Y positions. */
17862 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17863 max_y = it.last_visible_y;
17864 for (row = start_row + nrows_scrolled;
17865 row < bottom_row;
17866 ++row)
17868 row->y = it.current_y;
17869 row->visible_height = row->height;
17871 if (row->y < min_y)
17872 row->visible_height -= min_y - row->y;
17873 if (row->y + row->height > max_y)
17874 row->visible_height -= row->y + row->height - max_y;
17875 if (row->fringe_bitmap_periodic_p)
17876 row->redraw_fringe_bitmaps_p = true;
17878 it.current_y += row->height;
17880 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17881 last_reused_text_row = row;
17882 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17883 break;
17886 /* Disable lines in the current matrix which are now
17887 below the window. */
17888 for (++row; row < bottom_row; ++row)
17889 row->enabled_p = row->mode_line_p = false;
17892 /* Update window_end_pos etc.; last_reused_text_row is the last
17893 reused row from the current matrix containing text, if any.
17894 The value of last_text_row is the last displayed line
17895 containing text. */
17896 if (last_reused_text_row)
17897 adjust_window_ends (w, last_reused_text_row, true);
17898 else if (last_text_row)
17899 adjust_window_ends (w, last_text_row, false);
17900 else
17902 /* This window must be completely empty. */
17903 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17904 w->window_end_pos = Z - ZV;
17905 w->window_end_vpos = 0;
17907 w->window_end_valid = false;
17909 /* Update hint: don't try scrolling again in update_window. */
17910 w->desired_matrix->no_scrolling_p = true;
17912 #ifdef GLYPH_DEBUG
17913 debug_method_add (w, "try_window_reusing_current_matrix 1");
17914 #endif
17915 return true;
17917 else if (CHARPOS (new_start) > CHARPOS (start))
17919 struct glyph_row *pt_row, *row;
17920 struct glyph_row *first_reusable_row;
17921 struct glyph_row *first_row_to_display;
17922 int dy;
17923 int yb = window_text_bottom_y (w);
17925 /* Find the row starting at new_start, if there is one. Don't
17926 reuse a partially visible line at the end. */
17927 first_reusable_row = start_row;
17928 while (first_reusable_row->enabled_p
17929 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17930 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17931 < CHARPOS (new_start)))
17932 ++first_reusable_row;
17934 /* Give up if there is no row to reuse. */
17935 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17936 || !first_reusable_row->enabled_p
17937 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17938 != CHARPOS (new_start)))
17939 return false;
17941 /* We can reuse fully visible rows beginning with
17942 first_reusable_row to the end of the window. Set
17943 first_row_to_display to the first row that cannot be reused.
17944 Set pt_row to the row containing point, if there is any. */
17945 pt_row = NULL;
17946 for (first_row_to_display = first_reusable_row;
17947 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17948 ++first_row_to_display)
17950 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17951 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17952 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17953 && first_row_to_display->ends_at_zv_p
17954 && pt_row == NULL)))
17955 pt_row = first_row_to_display;
17958 /* Start displaying at the start of first_row_to_display. */
17959 eassert (first_row_to_display->y < yb);
17960 init_to_row_start (&it, w, first_row_to_display);
17962 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17963 - start_vpos);
17964 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17965 - nrows_scrolled);
17966 it.current_y = (first_row_to_display->y - first_reusable_row->y
17967 + WINDOW_HEADER_LINE_HEIGHT (w));
17969 /* Display lines beginning with first_row_to_display in the
17970 desired matrix. Set last_text_row to the last row displayed
17971 that displays text. */
17972 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17973 if (pt_row == NULL)
17974 w->cursor.vpos = -1;
17975 last_text_row = NULL;
17976 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17977 if (display_line (&it, w->cursor.vpos))
17978 last_text_row = it.glyph_row - 1;
17980 /* If point is in a reused row, adjust y and vpos of the cursor
17981 position. */
17982 if (pt_row)
17984 w->cursor.vpos -= nrows_scrolled;
17985 w->cursor.y -= first_reusable_row->y - start_row->y;
17988 /* Give up if point isn't in a row displayed or reused. (This
17989 also handles the case where w->cursor.vpos < nrows_scrolled
17990 after the calls to display_line, which can happen with scroll
17991 margins. See bug#1295.) */
17992 if (w->cursor.vpos < 0)
17994 clear_glyph_matrix (w->desired_matrix);
17995 return false;
17998 /* Scroll the display. */
17999 run.current_y = first_reusable_row->y;
18000 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
18001 run.height = it.last_visible_y - run.current_y;
18002 dy = run.current_y - run.desired_y;
18004 if (run.height)
18006 update_begin (f);
18007 FRAME_RIF (f)->update_window_begin_hook (w);
18008 FRAME_RIF (f)->clear_window_mouse_face (w);
18009 FRAME_RIF (f)->scroll_run_hook (w, &run);
18010 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18011 update_end (f);
18014 /* Adjust Y positions of reused rows. */
18015 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18016 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18017 max_y = it.last_visible_y;
18018 for (row = first_reusable_row; row < first_row_to_display; ++row)
18020 row->y -= dy;
18021 row->visible_height = row->height;
18022 if (row->y < min_y)
18023 row->visible_height -= min_y - row->y;
18024 if (row->y + row->height > max_y)
18025 row->visible_height -= row->y + row->height - max_y;
18026 if (row->fringe_bitmap_periodic_p)
18027 row->redraw_fringe_bitmaps_p = true;
18030 /* Scroll the current matrix. */
18031 eassert (nrows_scrolled > 0);
18032 rotate_matrix (w->current_matrix,
18033 start_vpos,
18034 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18035 -nrows_scrolled);
18037 /* Disable rows not reused. */
18038 for (row -= nrows_scrolled; row < bottom_row; ++row)
18039 row->enabled_p = false;
18041 /* Point may have moved to a different line, so we cannot assume that
18042 the previous cursor position is valid; locate the correct row. */
18043 if (pt_row)
18045 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18046 row < bottom_row
18047 && PT >= MATRIX_ROW_END_CHARPOS (row)
18048 && !row->ends_at_zv_p;
18049 row++)
18051 w->cursor.vpos++;
18052 w->cursor.y = row->y;
18054 if (row < bottom_row)
18056 /* Can't simply scan the row for point with
18057 bidi-reordered glyph rows. Let set_cursor_from_row
18058 figure out where to put the cursor, and if it fails,
18059 give up. */
18060 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18062 if (!set_cursor_from_row (w, row, w->current_matrix,
18063 0, 0, 0, 0))
18065 clear_glyph_matrix (w->desired_matrix);
18066 return false;
18069 else
18071 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18072 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18074 for (; glyph < end
18075 && (!BUFFERP (glyph->object)
18076 || glyph->charpos < PT);
18077 glyph++)
18079 w->cursor.hpos++;
18080 w->cursor.x += glyph->pixel_width;
18086 /* Adjust window end. A null value of last_text_row means that
18087 the window end is in reused rows which in turn means that
18088 only its vpos can have changed. */
18089 if (last_text_row)
18090 adjust_window_ends (w, last_text_row, false);
18091 else
18092 w->window_end_vpos -= nrows_scrolled;
18094 w->window_end_valid = false;
18095 w->desired_matrix->no_scrolling_p = true;
18097 #ifdef GLYPH_DEBUG
18098 debug_method_add (w, "try_window_reusing_current_matrix 2");
18099 #endif
18100 return true;
18103 return false;
18108 /************************************************************************
18109 Window redisplay reusing current matrix when buffer has changed
18110 ************************************************************************/
18112 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18113 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18114 ptrdiff_t *, ptrdiff_t *);
18115 static struct glyph_row *
18116 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18117 struct glyph_row *);
18120 /* Return the last row in MATRIX displaying text. If row START is
18121 non-null, start searching with that row. IT gives the dimensions
18122 of the display. Value is null if matrix is empty; otherwise it is
18123 a pointer to the row found. */
18125 static struct glyph_row *
18126 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18127 struct glyph_row *start)
18129 struct glyph_row *row, *row_found;
18131 /* Set row_found to the last row in IT->w's current matrix
18132 displaying text. The loop looks funny but think of partially
18133 visible lines. */
18134 row_found = NULL;
18135 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18136 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18138 eassert (row->enabled_p);
18139 row_found = row;
18140 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18141 break;
18142 ++row;
18145 return row_found;
18149 /* Return the last row in the current matrix of W that is not affected
18150 by changes at the start of current_buffer that occurred since W's
18151 current matrix was built. Value is null if no such row exists.
18153 BEG_UNCHANGED us the number of characters unchanged at the start of
18154 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18155 first changed character in current_buffer. Characters at positions <
18156 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18157 when the current matrix was built. */
18159 static struct glyph_row *
18160 find_last_unchanged_at_beg_row (struct window *w)
18162 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18163 struct glyph_row *row;
18164 struct glyph_row *row_found = NULL;
18165 int yb = window_text_bottom_y (w);
18167 /* Find the last row displaying unchanged text. */
18168 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18169 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18170 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18171 ++row)
18173 if (/* If row ends before first_changed_pos, it is unchanged,
18174 except in some case. */
18175 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18176 /* When row ends in ZV and we write at ZV it is not
18177 unchanged. */
18178 && !row->ends_at_zv_p
18179 /* When first_changed_pos is the end of a continued line,
18180 row is not unchanged because it may be no longer
18181 continued. */
18182 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18183 && (row->continued_p
18184 || row->exact_window_width_line_p))
18185 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18186 needs to be recomputed, so don't consider this row as
18187 unchanged. This happens when the last line was
18188 bidi-reordered and was killed immediately before this
18189 redisplay cycle. In that case, ROW->end stores the
18190 buffer position of the first visual-order character of
18191 the killed text, which is now beyond ZV. */
18192 && CHARPOS (row->end.pos) <= ZV)
18193 row_found = row;
18195 /* Stop if last visible row. */
18196 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18197 break;
18200 return row_found;
18204 /* Find the first glyph row in the current matrix of W that is not
18205 affected by changes at the end of current_buffer since the
18206 time W's current matrix was built.
18208 Return in *DELTA the number of chars by which buffer positions in
18209 unchanged text at the end of current_buffer must be adjusted.
18211 Return in *DELTA_BYTES the corresponding number of bytes.
18213 Value is null if no such row exists, i.e. all rows are affected by
18214 changes. */
18216 static struct glyph_row *
18217 find_first_unchanged_at_end_row (struct window *w,
18218 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18220 struct glyph_row *row;
18221 struct glyph_row *row_found = NULL;
18223 *delta = *delta_bytes = 0;
18225 /* Display must not have been paused, otherwise the current matrix
18226 is not up to date. */
18227 eassert (w->window_end_valid);
18229 /* A value of window_end_pos >= END_UNCHANGED means that the window
18230 end is in the range of changed text. If so, there is no
18231 unchanged row at the end of W's current matrix. */
18232 if (w->window_end_pos >= END_UNCHANGED)
18233 return NULL;
18235 /* Set row to the last row in W's current matrix displaying text. */
18236 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18238 /* If matrix is entirely empty, no unchanged row exists. */
18239 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18241 /* The value of row is the last glyph row in the matrix having a
18242 meaningful buffer position in it. The end position of row
18243 corresponds to window_end_pos. This allows us to translate
18244 buffer positions in the current matrix to current buffer
18245 positions for characters not in changed text. */
18246 ptrdiff_t Z_old =
18247 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18248 ptrdiff_t Z_BYTE_old =
18249 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18250 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18251 struct glyph_row *first_text_row
18252 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18254 *delta = Z - Z_old;
18255 *delta_bytes = Z_BYTE - Z_BYTE_old;
18257 /* Set last_unchanged_pos to the buffer position of the last
18258 character in the buffer that has not been changed. Z is the
18259 index + 1 of the last character in current_buffer, i.e. by
18260 subtracting END_UNCHANGED we get the index of the last
18261 unchanged character, and we have to add BEG to get its buffer
18262 position. */
18263 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18264 last_unchanged_pos_old = last_unchanged_pos - *delta;
18266 /* Search backward from ROW for a row displaying a line that
18267 starts at a minimum position >= last_unchanged_pos_old. */
18268 for (; row > first_text_row; --row)
18270 /* This used to abort, but it can happen.
18271 It is ok to just stop the search instead here. KFS. */
18272 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18273 break;
18275 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18276 row_found = row;
18280 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18282 return row_found;
18286 /* Make sure that glyph rows in the current matrix of window W
18287 reference the same glyph memory as corresponding rows in the
18288 frame's frame matrix. This function is called after scrolling W's
18289 current matrix on a terminal frame in try_window_id and
18290 try_window_reusing_current_matrix. */
18292 static void
18293 sync_frame_with_window_matrix_rows (struct window *w)
18295 struct frame *f = XFRAME (w->frame);
18296 struct glyph_row *window_row, *window_row_end, *frame_row;
18298 /* Preconditions: W must be a leaf window and full-width. Its frame
18299 must have a frame matrix. */
18300 eassert (BUFFERP (w->contents));
18301 eassert (WINDOW_FULL_WIDTH_P (w));
18302 eassert (!FRAME_WINDOW_P (f));
18304 /* If W is a full-width window, glyph pointers in W's current matrix
18305 have, by definition, to be the same as glyph pointers in the
18306 corresponding frame matrix. Note that frame matrices have no
18307 marginal areas (see build_frame_matrix). */
18308 window_row = w->current_matrix->rows;
18309 window_row_end = window_row + w->current_matrix->nrows;
18310 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18311 while (window_row < window_row_end)
18313 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18314 struct glyph *end = window_row->glyphs[LAST_AREA];
18316 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18317 frame_row->glyphs[TEXT_AREA] = start;
18318 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18319 frame_row->glyphs[LAST_AREA] = end;
18321 /* Disable frame rows whose corresponding window rows have
18322 been disabled in try_window_id. */
18323 if (!window_row->enabled_p)
18324 frame_row->enabled_p = false;
18326 ++window_row, ++frame_row;
18331 /* Find the glyph row in window W containing CHARPOS. Consider all
18332 rows between START and END (not inclusive). END null means search
18333 all rows to the end of the display area of W. Value is the row
18334 containing CHARPOS or null. */
18336 struct glyph_row *
18337 row_containing_pos (struct window *w, ptrdiff_t charpos,
18338 struct glyph_row *start, struct glyph_row *end, int dy)
18340 struct glyph_row *row = start;
18341 struct glyph_row *best_row = NULL;
18342 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18343 int last_y;
18345 /* If we happen to start on a header-line, skip that. */
18346 if (row->mode_line_p)
18347 ++row;
18349 if ((end && row >= end) || !row->enabled_p)
18350 return NULL;
18352 last_y = window_text_bottom_y (w) - dy;
18354 while (true)
18356 /* Give up if we have gone too far. */
18357 if ((end && row >= end) || !row->enabled_p)
18358 return NULL;
18359 /* This formerly returned if they were equal.
18360 I think that both quantities are of a "last plus one" type;
18361 if so, when they are equal, the row is within the screen. -- rms. */
18362 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18363 return NULL;
18365 /* If it is in this row, return this row. */
18366 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18367 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18368 /* The end position of a row equals the start
18369 position of the next row. If CHARPOS is there, we
18370 would rather consider it displayed in the next
18371 line, except when this line ends in ZV. */
18372 && !row_for_charpos_p (row, charpos)))
18373 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18375 struct glyph *g;
18377 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18378 || (!best_row && !row->continued_p))
18379 return row;
18380 /* In bidi-reordered rows, there could be several rows whose
18381 edges surround CHARPOS, all of these rows belonging to
18382 the same continued line. We need to find the row which
18383 fits CHARPOS the best. */
18384 for (g = row->glyphs[TEXT_AREA];
18385 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18386 g++)
18388 if (!STRINGP (g->object))
18390 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18392 mindif = eabs (g->charpos - charpos);
18393 best_row = row;
18394 /* Exact match always wins. */
18395 if (mindif == 0)
18396 return best_row;
18401 else if (best_row && !row->continued_p)
18402 return best_row;
18403 ++row;
18408 /* Try to redisplay window W by reusing its existing display. W's
18409 current matrix must be up to date when this function is called,
18410 i.e., window_end_valid must be true.
18412 Value is
18414 >= 1 if successful, i.e. display has been updated
18415 specifically:
18416 1 means the changes were in front of a newline that precedes
18417 the window start, and the whole current matrix was reused
18418 2 means the changes were after the last position displayed
18419 in the window, and the whole current matrix was reused
18420 3 means portions of the current matrix were reused, while
18421 some of the screen lines were redrawn
18422 -1 if redisplay with same window start is known not to succeed
18423 0 if otherwise unsuccessful
18425 The following steps are performed:
18427 1. Find the last row in the current matrix of W that is not
18428 affected by changes at the start of current_buffer. If no such row
18429 is found, give up.
18431 2. Find the first row in W's current matrix that is not affected by
18432 changes at the end of current_buffer. Maybe there is no such row.
18434 3. Display lines beginning with the row + 1 found in step 1 to the
18435 row found in step 2 or, if step 2 didn't find a row, to the end of
18436 the window.
18438 4. If cursor is not known to appear on the window, give up.
18440 5. If display stopped at the row found in step 2, scroll the
18441 display and current matrix as needed.
18443 6. Maybe display some lines at the end of W, if we must. This can
18444 happen under various circumstances, like a partially visible line
18445 becoming fully visible, or because newly displayed lines are displayed
18446 in smaller font sizes.
18448 7. Update W's window end information. */
18450 static int
18451 try_window_id (struct window *w)
18453 struct frame *f = XFRAME (w->frame);
18454 struct glyph_matrix *current_matrix = w->current_matrix;
18455 struct glyph_matrix *desired_matrix = w->desired_matrix;
18456 struct glyph_row *last_unchanged_at_beg_row;
18457 struct glyph_row *first_unchanged_at_end_row;
18458 struct glyph_row *row;
18459 struct glyph_row *bottom_row;
18460 int bottom_vpos;
18461 struct it it;
18462 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18463 int dvpos, dy;
18464 struct text_pos start_pos;
18465 struct run run;
18466 int first_unchanged_at_end_vpos = 0;
18467 struct glyph_row *last_text_row, *last_text_row_at_end;
18468 struct text_pos start;
18469 ptrdiff_t first_changed_charpos, last_changed_charpos;
18471 #ifdef GLYPH_DEBUG
18472 if (inhibit_try_window_id)
18473 return 0;
18474 #endif
18476 /* This is handy for debugging. */
18477 #if false
18478 #define GIVE_UP(X) \
18479 do { \
18480 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18481 return 0; \
18482 } while (false)
18483 #else
18484 #define GIVE_UP(X) return 0
18485 #endif
18487 SET_TEXT_POS_FROM_MARKER (start, w->start);
18489 /* Don't use this for mini-windows because these can show
18490 messages and mini-buffers, and we don't handle that here. */
18491 if (MINI_WINDOW_P (w))
18492 GIVE_UP (1);
18494 /* This flag is used to prevent redisplay optimizations. */
18495 if (windows_or_buffers_changed || f->cursor_type_changed)
18496 GIVE_UP (2);
18498 /* This function's optimizations cannot be used if overlays have
18499 changed in the buffer displayed by the window, so give up if they
18500 have. */
18501 if (w->last_overlay_modified != OVERLAY_MODIFF)
18502 GIVE_UP (200);
18504 /* Verify that narrowing has not changed.
18505 Also verify that we were not told to prevent redisplay optimizations.
18506 It would be nice to further
18507 reduce the number of cases where this prevents try_window_id. */
18508 if (current_buffer->clip_changed
18509 || current_buffer->prevent_redisplay_optimizations_p)
18510 GIVE_UP (3);
18512 /* Window must either use window-based redisplay or be full width. */
18513 if (!FRAME_WINDOW_P (f)
18514 && (!FRAME_LINE_INS_DEL_OK (f)
18515 || !WINDOW_FULL_WIDTH_P (w)))
18516 GIVE_UP (4);
18518 /* Give up if point is known NOT to appear in W. */
18519 if (PT < CHARPOS (start))
18520 GIVE_UP (5);
18522 /* Another way to prevent redisplay optimizations. */
18523 if (w->last_modified == 0)
18524 GIVE_UP (6);
18526 /* Verify that window is not hscrolled. */
18527 if (w->hscroll != 0)
18528 GIVE_UP (7);
18530 /* Verify that display wasn't paused. */
18531 if (!w->window_end_valid)
18532 GIVE_UP (8);
18534 /* Likewise if highlighting trailing whitespace. */
18535 if (!NILP (Vshow_trailing_whitespace))
18536 GIVE_UP (11);
18538 /* Can't use this if overlay arrow position and/or string have
18539 changed. */
18540 if (overlay_arrows_changed_p (false))
18541 GIVE_UP (12);
18543 /* When word-wrap is on, adding a space to the first word of a
18544 wrapped line can change the wrap position, altering the line
18545 above it. It might be worthwhile to handle this more
18546 intelligently, but for now just redisplay from scratch. */
18547 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18548 GIVE_UP (21);
18550 /* Under bidi reordering, adding or deleting a character in the
18551 beginning of a paragraph, before the first strong directional
18552 character, can change the base direction of the paragraph (unless
18553 the buffer specifies a fixed paragraph direction), which will
18554 require redisplaying the whole paragraph. It might be worthwhile
18555 to find the paragraph limits and widen the range of redisplayed
18556 lines to that, but for now just give up this optimization and
18557 redisplay from scratch. */
18558 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18559 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18560 GIVE_UP (22);
18562 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18563 to that variable require thorough redisplay. */
18564 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18565 GIVE_UP (23);
18567 /* Give up if display-line-numbers is in relative mode, or when the
18568 current line's number needs to be displayed in a distinct face. */
18569 if (EQ (Vdisplay_line_numbers, Qrelative)
18570 || EQ (Vdisplay_line_numbers, Qvisual)
18571 || (!NILP (Vdisplay_line_numbers)
18572 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18573 Qline_number_current_line,
18574 w->frame))))
18575 GIVE_UP (24);
18577 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18578 only if buffer has really changed. The reason is that the gap is
18579 initially at Z for freshly visited files. The code below would
18580 set end_unchanged to 0 in that case. */
18581 if (MODIFF > SAVE_MODIFF
18582 /* This seems to happen sometimes after saving a buffer. */
18583 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18585 if (GPT - BEG < BEG_UNCHANGED)
18586 BEG_UNCHANGED = GPT - BEG;
18587 if (Z - GPT < END_UNCHANGED)
18588 END_UNCHANGED = Z - GPT;
18591 /* The position of the first and last character that has been changed. */
18592 first_changed_charpos = BEG + BEG_UNCHANGED;
18593 last_changed_charpos = Z - END_UNCHANGED;
18595 /* If window starts after a line end, and the last change is in
18596 front of that newline, then changes don't affect the display.
18597 This case happens with stealth-fontification. Note that although
18598 the display is unchanged, glyph positions in the matrix have to
18599 be adjusted, of course. */
18600 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18601 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18602 && ((last_changed_charpos < CHARPOS (start)
18603 && CHARPOS (start) == BEGV)
18604 || (last_changed_charpos < CHARPOS (start) - 1
18605 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18607 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18608 struct glyph_row *r0;
18610 /* Compute how many chars/bytes have been added to or removed
18611 from the buffer. */
18612 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18613 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18614 Z_delta = Z - Z_old;
18615 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18617 /* Give up if PT is not in the window. Note that it already has
18618 been checked at the start of try_window_id that PT is not in
18619 front of the window start. */
18620 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18621 GIVE_UP (13);
18623 /* If window start is unchanged, we can reuse the whole matrix
18624 as is, after adjusting glyph positions. No need to compute
18625 the window end again, since its offset from Z hasn't changed. */
18626 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18627 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18628 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18629 /* PT must not be in a partially visible line. */
18630 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18631 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18633 /* Adjust positions in the glyph matrix. */
18634 if (Z_delta || Z_delta_bytes)
18636 struct glyph_row *r1
18637 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18638 increment_matrix_positions (w->current_matrix,
18639 MATRIX_ROW_VPOS (r0, current_matrix),
18640 MATRIX_ROW_VPOS (r1, current_matrix),
18641 Z_delta, Z_delta_bytes);
18644 /* Set the cursor. */
18645 row = row_containing_pos (w, PT, r0, NULL, 0);
18646 if (row)
18647 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18648 return 1;
18652 /* Handle the case that changes are all below what is displayed in
18653 the window, and that PT is in the window. This shortcut cannot
18654 be taken if ZV is visible in the window, and text has been added
18655 there that is visible in the window. */
18656 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18657 /* ZV is not visible in the window, or there are no
18658 changes at ZV, actually. */
18659 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18660 || first_changed_charpos == last_changed_charpos))
18662 struct glyph_row *r0;
18664 /* Give up if PT is not in the window. Note that it already has
18665 been checked at the start of try_window_id that PT is not in
18666 front of the window start. */
18667 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18668 GIVE_UP (14);
18670 /* If window start is unchanged, we can reuse the whole matrix
18671 as is, without changing glyph positions since no text has
18672 been added/removed in front of the window end. */
18673 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18674 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18675 /* PT must not be in a partially visible line. */
18676 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18677 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18679 /* We have to compute the window end anew since text
18680 could have been added/removed after it. */
18681 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18682 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18684 /* Set the cursor. */
18685 row = row_containing_pos (w, PT, r0, NULL, 0);
18686 if (row)
18687 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18688 return 2;
18692 /* Give up if window start is in the changed area.
18694 The condition used to read
18696 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18698 but why that was tested escapes me at the moment. */
18699 if (CHARPOS (start) >= first_changed_charpos
18700 && CHARPOS (start) <= last_changed_charpos)
18701 GIVE_UP (15);
18703 /* Check that window start agrees with the start of the first glyph
18704 row in its current matrix. Check this after we know the window
18705 start is not in changed text, otherwise positions would not be
18706 comparable. */
18707 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18708 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18709 GIVE_UP (16);
18711 /* Give up if the window ends in strings. Overlay strings
18712 at the end are difficult to handle, so don't try. */
18713 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18714 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18715 GIVE_UP (20);
18717 /* Compute the position at which we have to start displaying new
18718 lines. Some of the lines at the top of the window might be
18719 reusable because they are not displaying changed text. Find the
18720 last row in W's current matrix not affected by changes at the
18721 start of current_buffer. Value is null if changes start in the
18722 first line of window. */
18723 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18724 if (last_unchanged_at_beg_row)
18726 /* Avoid starting to display in the middle of a character, a TAB
18727 for instance. This is easier than to set up the iterator
18728 exactly, and it's not a frequent case, so the additional
18729 effort wouldn't really pay off. */
18730 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18731 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18732 && last_unchanged_at_beg_row > w->current_matrix->rows)
18733 --last_unchanged_at_beg_row;
18735 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18736 GIVE_UP (17);
18738 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18739 GIVE_UP (18);
18740 start_pos = it.current.pos;
18742 /* Start displaying new lines in the desired matrix at the same
18743 vpos we would use in the current matrix, i.e. below
18744 last_unchanged_at_beg_row. */
18745 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18746 current_matrix);
18747 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18748 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18750 eassert (it.hpos == 0 && it.current_x == 0);
18752 else
18754 /* There are no reusable lines at the start of the window.
18755 Start displaying in the first text line. */
18756 start_display (&it, w, start);
18757 it.vpos = it.first_vpos;
18758 start_pos = it.current.pos;
18761 /* Find the first row that is not affected by changes at the end of
18762 the buffer. Value will be null if there is no unchanged row, in
18763 which case we must redisplay to the end of the window. delta
18764 will be set to the value by which buffer positions beginning with
18765 first_unchanged_at_end_row have to be adjusted due to text
18766 changes. */
18767 first_unchanged_at_end_row
18768 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18769 IF_DEBUG (debug_delta = delta);
18770 IF_DEBUG (debug_delta_bytes = delta_bytes);
18772 /* Set stop_pos to the buffer position up to which we will have to
18773 display new lines. If first_unchanged_at_end_row != NULL, this
18774 is the buffer position of the start of the line displayed in that
18775 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18776 that we don't stop at a buffer position. */
18777 stop_pos = 0;
18778 if (first_unchanged_at_end_row)
18780 eassert (last_unchanged_at_beg_row == NULL
18781 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18783 /* If this is a continuation line, move forward to the next one
18784 that isn't. Changes in lines above affect this line.
18785 Caution: this may move first_unchanged_at_end_row to a row
18786 not displaying text. */
18787 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18788 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18789 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18790 < it.last_visible_y))
18791 ++first_unchanged_at_end_row;
18793 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18794 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18795 >= it.last_visible_y))
18796 first_unchanged_at_end_row = NULL;
18797 else
18799 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18800 + delta);
18801 first_unchanged_at_end_vpos
18802 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18803 eassert (stop_pos >= Z - END_UNCHANGED);
18806 else if (last_unchanged_at_beg_row == NULL)
18807 GIVE_UP (19);
18810 #ifdef GLYPH_DEBUG
18812 /* Either there is no unchanged row at the end, or the one we have
18813 now displays text. This is a necessary condition for the window
18814 end pos calculation at the end of this function. */
18815 eassert (first_unchanged_at_end_row == NULL
18816 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18818 debug_last_unchanged_at_beg_vpos
18819 = (last_unchanged_at_beg_row
18820 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18821 : -1);
18822 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18824 #endif /* GLYPH_DEBUG */
18827 /* Display new lines. Set last_text_row to the last new line
18828 displayed which has text on it, i.e. might end up as being the
18829 line where the window_end_vpos is. */
18830 w->cursor.vpos = -1;
18831 last_text_row = NULL;
18832 overlay_arrow_seen = false;
18833 if (it.current_y < it.last_visible_y
18834 && !f->fonts_changed
18835 && (first_unchanged_at_end_row == NULL
18836 || IT_CHARPOS (it) < stop_pos))
18837 it.glyph_row->reversed_p = false;
18838 while (it.current_y < it.last_visible_y
18839 && !f->fonts_changed
18840 && (first_unchanged_at_end_row == NULL
18841 || IT_CHARPOS (it) < stop_pos))
18843 if (display_line (&it, -1))
18844 last_text_row = it.glyph_row - 1;
18847 if (f->fonts_changed)
18848 return -1;
18850 /* The redisplay iterations in display_line above could have
18851 triggered font-lock, which could have done something that
18852 invalidates IT->w window's end-point information, on which we
18853 rely below. E.g., one package, which will remain unnamed, used
18854 to install a font-lock-fontify-region-function that called
18855 bury-buffer, whose side effect is to switch the buffer displayed
18856 by IT->w, and that predictably resets IT->w's window_end_valid
18857 flag, which we already tested at the entry to this function.
18858 Amply punish such packages/modes by giving up on this
18859 optimization in those cases. */
18860 if (!w->window_end_valid)
18862 clear_glyph_matrix (w->desired_matrix);
18863 return -1;
18866 /* Compute differences in buffer positions, y-positions etc. for
18867 lines reused at the bottom of the window. Compute what we can
18868 scroll. */
18869 if (first_unchanged_at_end_row
18870 /* No lines reused because we displayed everything up to the
18871 bottom of the window. */
18872 && it.current_y < it.last_visible_y)
18874 dvpos = (it.vpos
18875 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18876 current_matrix));
18877 dy = it.current_y - first_unchanged_at_end_row->y;
18878 run.current_y = first_unchanged_at_end_row->y;
18879 run.desired_y = run.current_y + dy;
18880 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18882 else
18884 delta = delta_bytes = dvpos = dy
18885 = run.current_y = run.desired_y = run.height = 0;
18886 first_unchanged_at_end_row = NULL;
18888 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18891 /* Find the cursor if not already found. We have to decide whether
18892 PT will appear on this window (it sometimes doesn't, but this is
18893 not a very frequent case.) This decision has to be made before
18894 the current matrix is altered. A value of cursor.vpos < 0 means
18895 that PT is either in one of the lines beginning at
18896 first_unchanged_at_end_row or below the window. Don't care for
18897 lines that might be displayed later at the window end; as
18898 mentioned, this is not a frequent case. */
18899 if (w->cursor.vpos < 0)
18901 /* Cursor in unchanged rows at the top? */
18902 if (PT < CHARPOS (start_pos)
18903 && last_unchanged_at_beg_row)
18905 row = row_containing_pos (w, PT,
18906 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18907 last_unchanged_at_beg_row + 1, 0);
18908 if (row)
18909 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18912 /* Start from first_unchanged_at_end_row looking for PT. */
18913 else if (first_unchanged_at_end_row)
18915 row = row_containing_pos (w, PT - delta,
18916 first_unchanged_at_end_row, NULL, 0);
18917 if (row)
18918 set_cursor_from_row (w, row, w->current_matrix, delta,
18919 delta_bytes, dy, dvpos);
18922 /* Give up if cursor was not found. */
18923 if (w->cursor.vpos < 0)
18925 clear_glyph_matrix (w->desired_matrix);
18926 return -1;
18930 /* Don't let the cursor end in the scroll margins. */
18932 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18933 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18935 if ((w->cursor.y < this_scroll_margin
18936 && CHARPOS (start) > BEGV)
18937 /* Old redisplay didn't take scroll margin into account at the bottom,
18938 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18939 || (w->cursor.y + (make_cursor_line_fully_visible_p
18940 ? cursor_height + this_scroll_margin
18941 : 1)) > it.last_visible_y)
18943 w->cursor.vpos = -1;
18944 clear_glyph_matrix (w->desired_matrix);
18945 return -1;
18949 /* Scroll the display. Do it before changing the current matrix so
18950 that xterm.c doesn't get confused about where the cursor glyph is
18951 found. */
18952 if (dy && run.height)
18954 update_begin (f);
18956 if (FRAME_WINDOW_P (f))
18958 FRAME_RIF (f)->update_window_begin_hook (w);
18959 FRAME_RIF (f)->clear_window_mouse_face (w);
18960 FRAME_RIF (f)->scroll_run_hook (w, &run);
18961 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18963 else
18965 /* Terminal frame. In this case, dvpos gives the number of
18966 lines to scroll by; dvpos < 0 means scroll up. */
18967 int from_vpos
18968 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18969 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18970 int end = (WINDOW_TOP_EDGE_LINE (w)
18971 + window_wants_header_line (w)
18972 + window_internal_height (w));
18974 #if defined (HAVE_GPM) || defined (MSDOS)
18975 x_clear_window_mouse_face (w);
18976 #endif
18977 /* Perform the operation on the screen. */
18978 if (dvpos > 0)
18980 /* Scroll last_unchanged_at_beg_row to the end of the
18981 window down dvpos lines. */
18982 set_terminal_window (f, end);
18984 /* On dumb terminals delete dvpos lines at the end
18985 before inserting dvpos empty lines. */
18986 if (!FRAME_SCROLL_REGION_OK (f))
18987 ins_del_lines (f, end - dvpos, -dvpos);
18989 /* Insert dvpos empty lines in front of
18990 last_unchanged_at_beg_row. */
18991 ins_del_lines (f, from, dvpos);
18993 else if (dvpos < 0)
18995 /* Scroll up last_unchanged_at_beg_vpos to the end of
18996 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18997 set_terminal_window (f, end);
18999 /* Delete dvpos lines in front of
19000 last_unchanged_at_beg_vpos. ins_del_lines will set
19001 the cursor to the given vpos and emit |dvpos| delete
19002 line sequences. */
19003 ins_del_lines (f, from + dvpos, dvpos);
19005 /* On a dumb terminal insert dvpos empty lines at the
19006 end. */
19007 if (!FRAME_SCROLL_REGION_OK (f))
19008 ins_del_lines (f, end + dvpos, -dvpos);
19011 set_terminal_window (f, 0);
19014 update_end (f);
19017 /* Shift reused rows of the current matrix to the right position.
19018 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19019 text. */
19020 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19021 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19022 if (dvpos < 0)
19024 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19025 bottom_vpos, dvpos);
19026 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19027 bottom_vpos);
19029 else if (dvpos > 0)
19031 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19032 bottom_vpos, dvpos);
19033 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19034 first_unchanged_at_end_vpos + dvpos);
19037 /* For frame-based redisplay, make sure that current frame and window
19038 matrix are in sync with respect to glyph memory. */
19039 if (!FRAME_WINDOW_P (f))
19040 sync_frame_with_window_matrix_rows (w);
19042 /* Adjust buffer positions in reused rows. */
19043 if (delta || delta_bytes)
19044 increment_matrix_positions (current_matrix,
19045 first_unchanged_at_end_vpos + dvpos,
19046 bottom_vpos, delta, delta_bytes);
19048 /* Adjust Y positions. */
19049 if (dy)
19050 shift_glyph_matrix (w, current_matrix,
19051 first_unchanged_at_end_vpos + dvpos,
19052 bottom_vpos, dy);
19054 if (first_unchanged_at_end_row)
19056 first_unchanged_at_end_row += dvpos;
19057 if (first_unchanged_at_end_row->y >= it.last_visible_y
19058 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19059 first_unchanged_at_end_row = NULL;
19062 /* If scrolling up, there may be some lines to display at the end of
19063 the window. */
19064 last_text_row_at_end = NULL;
19065 if (dy < 0)
19067 /* Scrolling up can leave for example a partially visible line
19068 at the end of the window to be redisplayed. */
19069 /* Set last_row to the glyph row in the current matrix where the
19070 window end line is found. It has been moved up or down in
19071 the matrix by dvpos. */
19072 int last_vpos = w->window_end_vpos + dvpos;
19073 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19075 /* If last_row is the window end line, it should display text. */
19076 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19078 /* If window end line was partially visible before, begin
19079 displaying at that line. Otherwise begin displaying with the
19080 line following it. */
19081 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19083 init_to_row_start (&it, w, last_row);
19084 it.vpos = last_vpos;
19085 it.current_y = last_row->y;
19087 else
19089 init_to_row_end (&it, w, last_row);
19090 it.vpos = 1 + last_vpos;
19091 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19092 ++last_row;
19095 /* We may start in a continuation line. If so, we have to
19096 get the right continuation_lines_width and current_x. */
19097 it.continuation_lines_width = last_row->continuation_lines_width;
19098 it.hpos = it.current_x = 0;
19100 /* Display the rest of the lines at the window end. */
19101 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19102 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19104 /* Is it always sure that the display agrees with lines in
19105 the current matrix? I don't think so, so we mark rows
19106 displayed invalid in the current matrix by setting their
19107 enabled_p flag to false. */
19108 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19109 if (display_line (&it, w->cursor.vpos))
19110 last_text_row_at_end = it.glyph_row - 1;
19114 /* Update window_end_pos and window_end_vpos. */
19115 if (first_unchanged_at_end_row && !last_text_row_at_end)
19117 /* Window end line if one of the preserved rows from the current
19118 matrix. Set row to the last row displaying text in current
19119 matrix starting at first_unchanged_at_end_row, after
19120 scrolling. */
19121 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19122 row = find_last_row_displaying_text (w->current_matrix, &it,
19123 first_unchanged_at_end_row);
19124 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19125 adjust_window_ends (w, row, true);
19126 eassert (w->window_end_bytepos >= 0);
19127 IF_DEBUG (debug_method_add (w, "A"));
19129 else if (last_text_row_at_end)
19131 adjust_window_ends (w, last_text_row_at_end, false);
19132 eassert (w->window_end_bytepos >= 0);
19133 IF_DEBUG (debug_method_add (w, "B"));
19135 else if (last_text_row)
19137 /* We have displayed either to the end of the window or at the
19138 end of the window, i.e. the last row with text is to be found
19139 in the desired matrix. */
19140 adjust_window_ends (w, last_text_row, false);
19141 eassert (w->window_end_bytepos >= 0);
19143 else if (first_unchanged_at_end_row == NULL
19144 && last_text_row == NULL
19145 && last_text_row_at_end == NULL)
19147 /* Displayed to end of window, but no line containing text was
19148 displayed. Lines were deleted at the end of the window. */
19149 bool first_vpos = window_wants_header_line (w);
19150 int vpos = w->window_end_vpos;
19151 struct glyph_row *current_row = current_matrix->rows + vpos;
19152 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19154 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19156 eassert (first_vpos <= vpos);
19157 if (desired_row->enabled_p)
19159 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19160 row = desired_row;
19162 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19163 row = current_row;
19166 w->window_end_vpos = vpos + 1;
19167 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19168 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19169 eassert (w->window_end_bytepos >= 0);
19170 IF_DEBUG (debug_method_add (w, "C"));
19172 else
19173 emacs_abort ();
19175 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19176 debug_end_vpos = w->window_end_vpos));
19178 /* Record that display has not been completed. */
19179 w->window_end_valid = false;
19180 w->desired_matrix->no_scrolling_p = true;
19181 return 3;
19183 #undef GIVE_UP
19188 /***********************************************************************
19189 More debugging support
19190 ***********************************************************************/
19192 #ifdef GLYPH_DEBUG
19194 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19195 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19196 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19199 /* Dump the contents of glyph matrix MATRIX on stderr.
19201 GLYPHS 0 means don't show glyph contents.
19202 GLYPHS 1 means show glyphs in short form
19203 GLYPHS > 1 means show glyphs in long form. */
19205 void
19206 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19208 int i;
19209 for (i = 0; i < matrix->nrows; ++i)
19210 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19214 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19215 the glyph row and area where the glyph comes from. */
19217 void
19218 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19220 if (glyph->type == CHAR_GLYPH
19221 || glyph->type == GLYPHLESS_GLYPH)
19223 fprintf (stderr,
19224 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19225 glyph - row->glyphs[TEXT_AREA],
19226 (glyph->type == CHAR_GLYPH
19227 ? 'C'
19228 : 'G'),
19229 glyph->charpos,
19230 (BUFFERP (glyph->object)
19231 ? 'B'
19232 : (STRINGP (glyph->object)
19233 ? 'S'
19234 : (NILP (glyph->object)
19235 ? '0'
19236 : '-'))),
19237 glyph->pixel_width,
19238 glyph->u.ch,
19239 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19240 ? (int) glyph->u.ch
19241 : '.'),
19242 glyph->face_id,
19243 glyph->left_box_line_p,
19244 glyph->right_box_line_p);
19246 else if (glyph->type == STRETCH_GLYPH)
19248 fprintf (stderr,
19249 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19250 glyph - row->glyphs[TEXT_AREA],
19251 'S',
19252 glyph->charpos,
19253 (BUFFERP (glyph->object)
19254 ? 'B'
19255 : (STRINGP (glyph->object)
19256 ? 'S'
19257 : (NILP (glyph->object)
19258 ? '0'
19259 : '-'))),
19260 glyph->pixel_width,
19262 ' ',
19263 glyph->face_id,
19264 glyph->left_box_line_p,
19265 glyph->right_box_line_p);
19267 else if (glyph->type == IMAGE_GLYPH)
19269 fprintf (stderr,
19270 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19271 glyph - row->glyphs[TEXT_AREA],
19272 'I',
19273 glyph->charpos,
19274 (BUFFERP (glyph->object)
19275 ? 'B'
19276 : (STRINGP (glyph->object)
19277 ? 'S'
19278 : (NILP (glyph->object)
19279 ? '0'
19280 : '-'))),
19281 glyph->pixel_width,
19282 (unsigned int) glyph->u.img_id,
19283 '.',
19284 glyph->face_id,
19285 glyph->left_box_line_p,
19286 glyph->right_box_line_p);
19288 else if (glyph->type == COMPOSITE_GLYPH)
19290 fprintf (stderr,
19291 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19292 glyph - row->glyphs[TEXT_AREA],
19293 '+',
19294 glyph->charpos,
19295 (BUFFERP (glyph->object)
19296 ? 'B'
19297 : (STRINGP (glyph->object)
19298 ? 'S'
19299 : (NILP (glyph->object)
19300 ? '0'
19301 : '-'))),
19302 glyph->pixel_width,
19303 (unsigned int) glyph->u.cmp.id);
19304 if (glyph->u.cmp.automatic)
19305 fprintf (stderr,
19306 "[%d-%d]",
19307 glyph->slice.cmp.from, glyph->slice.cmp.to);
19308 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19309 glyph->face_id,
19310 glyph->left_box_line_p,
19311 glyph->right_box_line_p);
19313 else if (glyph->type == XWIDGET_GLYPH)
19315 #ifndef HAVE_XWIDGETS
19316 eassume (false);
19317 #else
19318 fprintf (stderr,
19319 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19320 glyph - row->glyphs[TEXT_AREA],
19321 'X',
19322 glyph->charpos,
19323 (BUFFERP (glyph->object)
19324 ? 'B'
19325 : (STRINGP (glyph->object)
19326 ? 'S'
19327 : '-')),
19328 glyph->pixel_width,
19329 glyph->u.xwidget,
19330 '.',
19331 glyph->face_id,
19332 glyph->left_box_line_p,
19333 glyph->right_box_line_p);
19334 #endif
19339 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19340 GLYPHS 0 means don't show glyph contents.
19341 GLYPHS 1 means show glyphs in short form
19342 GLYPHS > 1 means show glyphs in long form. */
19344 void
19345 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19347 if (glyphs != 1)
19349 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19350 fprintf (stderr, "==============================================================================\n");
19352 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19353 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19354 vpos,
19355 MATRIX_ROW_START_CHARPOS (row),
19356 MATRIX_ROW_END_CHARPOS (row),
19357 row->used[TEXT_AREA],
19358 row->contains_overlapping_glyphs_p,
19359 row->enabled_p,
19360 row->truncated_on_left_p,
19361 row->truncated_on_right_p,
19362 row->continued_p,
19363 MATRIX_ROW_CONTINUATION_LINE_P (row),
19364 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19365 row->ends_at_zv_p,
19366 row->fill_line_p,
19367 row->ends_in_middle_of_char_p,
19368 row->starts_in_middle_of_char_p,
19369 row->mouse_face_p,
19370 row->x,
19371 row->y,
19372 row->pixel_width,
19373 row->height,
19374 row->visible_height,
19375 row->ascent,
19376 row->phys_ascent);
19377 /* The next 3 lines should align to "Start" in the header. */
19378 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19379 row->end.overlay_string_index,
19380 row->continuation_lines_width);
19381 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19382 CHARPOS (row->start.string_pos),
19383 CHARPOS (row->end.string_pos));
19384 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19385 row->end.dpvec_index);
19388 if (glyphs > 1)
19390 int area;
19392 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19394 struct glyph *glyph = row->glyphs[area];
19395 struct glyph *glyph_end = glyph + row->used[area];
19397 /* Glyph for a line end in text. */
19398 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19399 ++glyph_end;
19401 if (glyph < glyph_end)
19402 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19404 for (; glyph < glyph_end; ++glyph)
19405 dump_glyph (row, glyph, area);
19408 else if (glyphs == 1)
19410 int area;
19411 char s[SHRT_MAX + 4];
19413 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19415 int i;
19417 for (i = 0; i < row->used[area]; ++i)
19419 struct glyph *glyph = row->glyphs[area] + i;
19420 if (i == row->used[area] - 1
19421 && area == TEXT_AREA
19422 && NILP (glyph->object)
19423 && glyph->type == CHAR_GLYPH
19424 && glyph->u.ch == ' ')
19426 strcpy (&s[i], "[\\n]");
19427 i += 4;
19429 else if (glyph->type == CHAR_GLYPH
19430 && glyph->u.ch < 0x80
19431 && glyph->u.ch >= ' ')
19432 s[i] = glyph->u.ch;
19433 else
19434 s[i] = '.';
19437 s[i] = '\0';
19438 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19444 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19445 Sdump_glyph_matrix, 0, 1, "p",
19446 doc: /* Dump the current matrix of the selected window to stderr.
19447 Shows contents of glyph row structures. With non-nil
19448 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19449 glyphs in short form, otherwise show glyphs in long form.
19451 Interactively, no argument means show glyphs in short form;
19452 with numeric argument, its value is passed as the GLYPHS flag. */)
19453 (Lisp_Object glyphs)
19455 struct window *w = XWINDOW (selected_window);
19456 struct buffer *buffer = XBUFFER (w->contents);
19458 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19459 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19460 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19461 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19462 fprintf (stderr, "=============================================\n");
19463 dump_glyph_matrix (w->current_matrix,
19464 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19465 return Qnil;
19469 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19470 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19471 Only text-mode frames have frame glyph matrices. */)
19472 (void)
19474 struct frame *f = XFRAME (selected_frame);
19476 if (f->current_matrix)
19477 dump_glyph_matrix (f->current_matrix, 1);
19478 else
19479 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19480 return Qnil;
19484 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19485 doc: /* Dump glyph row ROW to stderr.
19486 Interactively, ROW is the prefix numeric argument and defaults to
19487 the row which displays point.
19488 Optional argument GLYPHS 0 means don't dump glyphs.
19489 GLYPHS 1 means dump glyphs in short form.
19490 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19491 (Lisp_Object row, Lisp_Object glyphs)
19493 struct glyph_matrix *matrix;
19494 EMACS_INT vpos;
19496 if (NILP (row))
19498 int d1, d2, d3, d4, d5, ypos;
19499 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19500 &d1, &d2, &d3, &d4, &d5, &ypos);
19501 if (visible_p)
19502 vpos = ypos;
19503 else
19504 vpos = 0;
19506 else
19508 CHECK_NUMBER (row);
19509 vpos = XINT (row);
19511 matrix = XWINDOW (selected_window)->current_matrix;
19512 if (vpos >= 0 && vpos < matrix->nrows)
19513 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19514 vpos,
19515 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19516 return Qnil;
19520 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19521 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19522 Interactively, ROW is the prefix numeric argument and defaults to zero.
19523 GLYPHS 0 means don't dump glyphs.
19524 GLYPHS 1 means dump glyphs in short form.
19525 GLYPHS > 1 or omitted means dump glyphs in long form.
19527 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19528 do nothing. */)
19529 (Lisp_Object row, Lisp_Object glyphs)
19531 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19532 struct frame *sf = SELECTED_FRAME ();
19533 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19534 EMACS_INT vpos;
19536 if (NILP (row))
19537 vpos = 0;
19538 else
19540 CHECK_NUMBER (row);
19541 vpos = XINT (row);
19543 if (vpos >= 0 && vpos < m->nrows)
19544 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19545 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19546 #endif
19547 return Qnil;
19551 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19552 doc: /* Toggle tracing of redisplay.
19553 With ARG, turn tracing on if and only if ARG is positive. */)
19554 (Lisp_Object arg)
19556 if (NILP (arg))
19557 trace_redisplay_p = !trace_redisplay_p;
19558 else
19560 arg = Fprefix_numeric_value (arg);
19561 trace_redisplay_p = XINT (arg) > 0;
19564 return Qnil;
19568 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19569 doc: /* Like `format', but print result to stderr.
19570 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19571 (ptrdiff_t nargs, Lisp_Object *args)
19573 Lisp_Object s = Fformat (nargs, args);
19574 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19575 return Qnil;
19578 #endif /* GLYPH_DEBUG */
19582 /***********************************************************************
19583 Building Desired Matrix Rows
19584 ***********************************************************************/
19586 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19587 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19589 static struct glyph_row *
19590 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19592 struct frame *f = XFRAME (WINDOW_FRAME (w));
19593 struct buffer *buffer = XBUFFER (w->contents);
19594 struct buffer *old = current_buffer;
19595 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19596 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19597 const unsigned char *arrow_end = arrow_string + arrow_len;
19598 const unsigned char *p;
19599 struct it it;
19600 bool multibyte_p;
19601 int n_glyphs_before;
19603 set_buffer_temp (buffer);
19604 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19605 scratch_glyph_row.reversed_p = false;
19606 it.glyph_row->used[TEXT_AREA] = 0;
19607 SET_TEXT_POS (it.position, 0, 0);
19609 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19610 p = arrow_string;
19611 while (p < arrow_end)
19613 Lisp_Object face, ilisp;
19615 /* Get the next character. */
19616 if (multibyte_p)
19617 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19618 else
19620 it.c = it.char_to_display = *p, it.len = 1;
19621 if (! ASCII_CHAR_P (it.c))
19622 it.char_to_display = BYTE8_TO_CHAR (it.c);
19624 p += it.len;
19626 /* Get its face. */
19627 ilisp = make_number (p - arrow_string);
19628 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19629 it.face_id = compute_char_face (f, it.char_to_display, face);
19631 /* Compute its width, get its glyphs. */
19632 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19633 SET_TEXT_POS (it.position, -1, -1);
19634 PRODUCE_GLYPHS (&it);
19636 /* If this character doesn't fit any more in the line, we have
19637 to remove some glyphs. */
19638 if (it.current_x > it.last_visible_x)
19640 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19641 break;
19645 set_buffer_temp (old);
19646 return it.glyph_row;
19650 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19651 glyphs to insert is determined by produce_special_glyphs. */
19653 static void
19654 insert_left_trunc_glyphs (struct it *it)
19656 struct it truncate_it;
19657 struct glyph *from, *end, *to, *toend;
19659 eassert (!FRAME_WINDOW_P (it->f)
19660 || (!it->glyph_row->reversed_p
19661 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19662 || (it->glyph_row->reversed_p
19663 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19665 /* Get the truncation glyphs. */
19666 truncate_it = *it;
19667 truncate_it.current_x = 0;
19668 truncate_it.face_id = DEFAULT_FACE_ID;
19669 truncate_it.glyph_row = &scratch_glyph_row;
19670 truncate_it.area = TEXT_AREA;
19671 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19672 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19673 truncate_it.object = Qnil;
19674 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19676 /* Overwrite glyphs from IT with truncation glyphs. */
19677 if (!it->glyph_row->reversed_p)
19679 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19681 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19682 end = from + tused;
19683 to = it->glyph_row->glyphs[TEXT_AREA];
19684 toend = to + it->glyph_row->used[TEXT_AREA];
19685 if (FRAME_WINDOW_P (it->f))
19687 /* On GUI frames, when variable-size fonts are displayed,
19688 the truncation glyphs may need more pixels than the row's
19689 glyphs they overwrite. We overwrite more glyphs to free
19690 enough screen real estate, and enlarge the stretch glyph
19691 on the right (see display_line), if there is one, to
19692 preserve the screen position of the truncation glyphs on
19693 the right. */
19694 int w = 0;
19695 struct glyph *g = to;
19696 short used;
19698 /* The first glyph could be partially visible, in which case
19699 it->glyph_row->x will be negative. But we want the left
19700 truncation glyphs to be aligned at the left margin of the
19701 window, so we override the x coordinate at which the row
19702 will begin. */
19703 it->glyph_row->x = 0;
19704 while (g < toend && w < it->truncation_pixel_width)
19706 w += g->pixel_width;
19707 ++g;
19709 if (g - to - tused > 0)
19711 memmove (to + tused, g, (toend - g) * sizeof(*g));
19712 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19714 used = it->glyph_row->used[TEXT_AREA];
19715 if (it->glyph_row->truncated_on_right_p
19716 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19717 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19718 == STRETCH_GLYPH)
19720 int extra = w - it->truncation_pixel_width;
19722 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19726 while (from < end)
19727 *to++ = *from++;
19729 /* There may be padding glyphs left over. Overwrite them too. */
19730 if (!FRAME_WINDOW_P (it->f))
19732 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19734 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19735 while (from < end)
19736 *to++ = *from++;
19740 if (to > toend)
19741 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19743 else
19745 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19747 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19748 that back to front. */
19749 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19750 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19751 toend = it->glyph_row->glyphs[TEXT_AREA];
19752 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19753 if (FRAME_WINDOW_P (it->f))
19755 int w = 0;
19756 struct glyph *g = to;
19758 while (g >= toend && w < it->truncation_pixel_width)
19760 w += g->pixel_width;
19761 --g;
19763 if (to - g - tused > 0)
19764 to = g + tused;
19765 if (it->glyph_row->truncated_on_right_p
19766 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19767 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19769 int extra = w - it->truncation_pixel_width;
19771 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19775 while (from >= end && to >= toend)
19776 *to-- = *from--;
19777 if (!FRAME_WINDOW_P (it->f))
19779 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19781 from =
19782 truncate_it.glyph_row->glyphs[TEXT_AREA]
19783 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19784 while (from >= end && to >= toend)
19785 *to-- = *from--;
19788 if (from >= end)
19790 /* Need to free some room before prepending additional
19791 glyphs. */
19792 int move_by = from - end + 1;
19793 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19794 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19796 for ( ; g >= g0; g--)
19797 g[move_by] = *g;
19798 while (from >= end)
19799 *to-- = *from--;
19800 it->glyph_row->used[TEXT_AREA] += move_by;
19805 /* Compute the hash code for ROW. */
19806 unsigned
19807 row_hash (struct glyph_row *row)
19809 int area, k;
19810 unsigned hashval = 0;
19812 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19813 for (k = 0; k < row->used[area]; ++k)
19814 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19815 + row->glyphs[area][k].u.val
19816 + row->glyphs[area][k].face_id
19817 + row->glyphs[area][k].padding_p
19818 + (row->glyphs[area][k].type << 2));
19820 return hashval;
19823 /* Compute the pixel height and width of IT->glyph_row.
19825 Most of the time, ascent and height of a display line will be equal
19826 to the max_ascent and max_height values of the display iterator
19827 structure. This is not the case if
19829 1. We hit ZV without displaying anything. In this case, max_ascent
19830 and max_height will be zero.
19832 2. We have some glyphs that don't contribute to the line height.
19833 (The glyph row flag contributes_to_line_height_p is for future
19834 pixmap extensions).
19836 The first case is easily covered by using default values because in
19837 these cases, the line height does not really matter, except that it
19838 must not be zero. */
19840 static void
19841 compute_line_metrics (struct it *it)
19843 struct glyph_row *row = it->glyph_row;
19845 if (FRAME_WINDOW_P (it->f))
19847 int i, min_y, max_y;
19849 /* The line may consist of one space only, that was added to
19850 place the cursor on it. If so, the row's height hasn't been
19851 computed yet. */
19852 if (row->height == 0)
19854 if (it->max_ascent + it->max_descent == 0)
19855 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19856 row->ascent = it->max_ascent;
19857 row->height = it->max_ascent + it->max_descent;
19858 row->phys_ascent = it->max_phys_ascent;
19859 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19860 row->extra_line_spacing = it->max_extra_line_spacing;
19863 /* Compute the width of this line. */
19864 row->pixel_width = row->x;
19865 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19866 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19868 eassert (row->pixel_width >= 0);
19869 eassert (row->ascent >= 0 && row->height > 0);
19871 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19872 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19874 /* If first line's physical ascent is larger than its logical
19875 ascent, use the physical ascent, and make the row taller.
19876 This makes accented characters fully visible. */
19877 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19878 && row->phys_ascent > row->ascent)
19880 row->height += row->phys_ascent - row->ascent;
19881 row->ascent = row->phys_ascent;
19884 /* Compute how much of the line is visible. */
19885 row->visible_height = row->height;
19887 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19888 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19890 if (row->y < min_y)
19891 row->visible_height -= min_y - row->y;
19892 if (row->y + row->height > max_y)
19893 row->visible_height -= row->y + row->height - max_y;
19895 else
19897 row->pixel_width = row->used[TEXT_AREA];
19898 if (row->continued_p)
19899 row->pixel_width -= it->continuation_pixel_width;
19900 else if (row->truncated_on_right_p)
19901 row->pixel_width -= it->truncation_pixel_width;
19902 row->ascent = row->phys_ascent = 0;
19903 row->height = row->phys_height = row->visible_height = 1;
19904 row->extra_line_spacing = 0;
19907 /* Compute a hash code for this row. */
19908 row->hash = row_hash (row);
19910 it->max_ascent = it->max_descent = 0;
19911 it->max_phys_ascent = it->max_phys_descent = 0;
19915 /* Append one space to the glyph row of iterator IT if doing a
19916 window-based redisplay. The space has the same face as
19917 IT->face_id. Value is true if a space was added.
19919 This function is called to make sure that there is always one glyph
19920 at the end of a glyph row that the cursor can be set on under
19921 window-systems. (If there weren't such a glyph we would not know
19922 how wide and tall a box cursor should be displayed).
19924 At the same time this space let's a nicely handle clearing to the
19925 end of the line if the row ends in italic text. */
19927 static bool
19928 append_space_for_newline (struct it *it, bool default_face_p)
19930 if (FRAME_WINDOW_P (it->f))
19932 int n = it->glyph_row->used[TEXT_AREA];
19934 if (it->glyph_row->glyphs[TEXT_AREA] + n
19935 < it->glyph_row->glyphs[1 + TEXT_AREA])
19937 /* Save some values that must not be changed.
19938 Must save IT->c and IT->len because otherwise
19939 ITERATOR_AT_END_P wouldn't work anymore after
19940 append_space_for_newline has been called. */
19941 enum display_element_type saved_what = it->what;
19942 int saved_c = it->c, saved_len = it->len;
19943 int saved_char_to_display = it->char_to_display;
19944 int saved_x = it->current_x;
19945 int saved_face_id = it->face_id;
19946 bool saved_box_end = it->end_of_box_run_p;
19947 struct text_pos saved_pos;
19948 Lisp_Object saved_object;
19949 struct face *face;
19951 saved_object = it->object;
19952 saved_pos = it->position;
19954 it->what = IT_CHARACTER;
19955 memset (&it->position, 0, sizeof it->position);
19956 it->object = Qnil;
19957 it->c = it->char_to_display = ' ';
19958 it->len = 1;
19960 /* If the default face was remapped, be sure to use the
19961 remapped face for the appended newline. */
19962 if (default_face_p)
19963 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19964 else if (it->face_before_selective_p)
19965 it->face_id = it->saved_face_id;
19966 face = FACE_FROM_ID (it->f, it->face_id);
19967 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19968 /* In R2L rows, we will prepend a stretch glyph that will
19969 have the end_of_box_run_p flag set for it, so there's no
19970 need for the appended newline glyph to have that flag
19971 set. */
19972 if (it->glyph_row->reversed_p
19973 /* But if the appended newline glyph goes all the way to
19974 the end of the row, there will be no stretch glyph,
19975 so leave the box flag set. */
19976 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19977 it->end_of_box_run_p = false;
19979 PRODUCE_GLYPHS (it);
19981 #ifdef HAVE_WINDOW_SYSTEM
19982 /* Make sure this space glyph has the right ascent and
19983 descent values, or else cursor at end of line will look
19984 funny, and height of empty lines will be incorrect. */
19985 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19986 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19987 if (n == 0)
19989 Lisp_Object height, total_height;
19990 int extra_line_spacing = it->extra_line_spacing;
19991 int boff = font->baseline_offset;
19993 if (font->vertical_centering)
19994 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19996 it->object = saved_object; /* get_it_property needs this */
19997 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19998 /* Must do a subset of line height processing from
19999 x_produce_glyph for newline characters. */
20000 height = get_it_property (it, Qline_height);
20001 if (CONSP (height)
20002 && CONSP (XCDR (height))
20003 && NILP (XCDR (XCDR (height))))
20005 total_height = XCAR (XCDR (height));
20006 height = XCAR (height);
20008 else
20009 total_height = Qnil;
20010 height = calc_line_height_property (it, height, font, boff, true);
20012 if (it->override_ascent >= 0)
20014 it->ascent = it->override_ascent;
20015 it->descent = it->override_descent;
20016 boff = it->override_boff;
20018 if (EQ (height, Qt))
20019 extra_line_spacing = 0;
20020 else
20022 Lisp_Object spacing;
20024 it->phys_ascent = it->ascent;
20025 it->phys_descent = it->descent;
20026 if (!NILP (height)
20027 && XINT (height) > it->ascent + it->descent)
20028 it->ascent = XINT (height) - it->descent;
20030 if (!NILP (total_height))
20031 spacing = calc_line_height_property (it, total_height, font,
20032 boff, false);
20033 else
20035 spacing = get_it_property (it, Qline_spacing);
20036 spacing = calc_line_height_property (it, spacing, font,
20037 boff, false);
20039 if (INTEGERP (spacing))
20041 extra_line_spacing = XINT (spacing);
20042 if (!NILP (total_height))
20043 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20046 if (extra_line_spacing > 0)
20048 it->descent += extra_line_spacing;
20049 if (extra_line_spacing > it->max_extra_line_spacing)
20050 it->max_extra_line_spacing = extra_line_spacing;
20052 it->max_ascent = it->ascent;
20053 it->max_descent = it->descent;
20054 /* Make sure compute_line_metrics recomputes the row height. */
20055 it->glyph_row->height = 0;
20058 g->ascent = it->max_ascent;
20059 g->descent = it->max_descent;
20060 #endif
20062 it->override_ascent = -1;
20063 it->constrain_row_ascent_descent_p = false;
20064 it->current_x = saved_x;
20065 it->object = saved_object;
20066 it->position = saved_pos;
20067 it->what = saved_what;
20068 it->face_id = saved_face_id;
20069 it->len = saved_len;
20070 it->c = saved_c;
20071 it->char_to_display = saved_char_to_display;
20072 it->end_of_box_run_p = saved_box_end;
20073 return true;
20077 return false;
20081 /* Extend the face of the last glyph in the text area of IT->glyph_row
20082 to the end of the display line. Called from display_line. If the
20083 glyph row is empty, add a space glyph to it so that we know the
20084 face to draw. Set the glyph row flag fill_line_p. If the glyph
20085 row is R2L, prepend a stretch glyph to cover the empty space to the
20086 left of the leftmost glyph. */
20088 static void
20089 extend_face_to_end_of_line (struct it *it)
20091 struct face *face, *default_face;
20092 struct frame *f = it->f;
20094 /* If line is already filled, do nothing. Non window-system frames
20095 get a grace of one more ``pixel'' because their characters are
20096 1-``pixel'' wide, so they hit the equality too early. This grace
20097 is needed only for R2L rows that are not continued, to produce
20098 one extra blank where we could display the cursor. */
20099 if ((it->current_x >= it->last_visible_x
20100 + (!FRAME_WINDOW_P (f)
20101 && it->glyph_row->reversed_p
20102 && !it->glyph_row->continued_p))
20103 /* If the window has display margins, we will need to extend
20104 their face even if the text area is filled. */
20105 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20106 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20107 return;
20109 /* The default face, possibly remapped. */
20110 default_face = FACE_FROM_ID_OR_NULL (f,
20111 lookup_basic_face (f, DEFAULT_FACE_ID));
20113 /* Face extension extends the background and box of IT->face_id
20114 to the end of the line. If the background equals the background
20115 of the frame, we don't have to do anything. */
20116 face = FACE_FROM_ID (f, (it->face_before_selective_p
20117 ? it->saved_face_id
20118 : it->face_id));
20120 if (FRAME_WINDOW_P (f)
20121 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20122 && face->box == FACE_NO_BOX
20123 && face->background == FRAME_BACKGROUND_PIXEL (f)
20124 #ifdef HAVE_WINDOW_SYSTEM
20125 && !face->stipple
20126 #endif
20127 && !it->glyph_row->reversed_p)
20128 return;
20130 /* Set the glyph row flag indicating that the face of the last glyph
20131 in the text area has to be drawn to the end of the text area. */
20132 it->glyph_row->fill_line_p = true;
20134 /* If current character of IT is not ASCII, make sure we have the
20135 ASCII face. This will be automatically undone the next time
20136 get_next_display_element returns a multibyte character. Note
20137 that the character will always be single byte in unibyte
20138 text. */
20139 if (!ASCII_CHAR_P (it->c))
20141 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20144 if (FRAME_WINDOW_P (f))
20146 /* If the row is empty, add a space with the current face of IT,
20147 so that we know which face to draw. */
20148 if (it->glyph_row->used[TEXT_AREA] == 0)
20150 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20151 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20152 it->glyph_row->used[TEXT_AREA] = 1;
20154 /* Mode line and the header line don't have margins, and
20155 likewise the frame's tool-bar window, if there is any. */
20156 if (!(it->glyph_row->mode_line_p
20157 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20158 || (WINDOWP (f->tool_bar_window)
20159 && it->w == XWINDOW (f->tool_bar_window))
20160 #endif
20163 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20164 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20166 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20167 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20168 default_face->id;
20169 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20171 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20172 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20174 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20175 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20176 default_face->id;
20177 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20180 #ifdef HAVE_WINDOW_SYSTEM
20181 if (it->glyph_row->reversed_p)
20183 /* Prepend a stretch glyph to the row, such that the
20184 rightmost glyph will be drawn flushed all the way to the
20185 right margin of the window. The stretch glyph that will
20186 occupy the empty space, if any, to the left of the
20187 glyphs. */
20188 struct font *font = face->font ? face->font : FRAME_FONT (f);
20189 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20190 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20191 struct glyph *g;
20192 int row_width, stretch_ascent, stretch_width;
20193 struct text_pos saved_pos;
20194 int saved_face_id;
20195 bool saved_avoid_cursor, saved_box_start;
20197 for (row_width = 0, g = row_start; g < row_end; g++)
20198 row_width += g->pixel_width;
20200 /* FIXME: There are various minor display glitches in R2L
20201 rows when only one of the fringes is missing. The
20202 strange condition below produces the least bad effect. */
20203 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20204 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20205 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20206 stretch_width = window_box_width (it->w, TEXT_AREA);
20207 else
20208 stretch_width = it->last_visible_x - it->first_visible_x;
20209 stretch_width -= row_width;
20211 if (stretch_width > 0)
20213 stretch_ascent =
20214 (((it->ascent + it->descent)
20215 * FONT_BASE (font)) / FONT_HEIGHT (font));
20216 saved_pos = it->position;
20217 memset (&it->position, 0, sizeof it->position);
20218 saved_avoid_cursor = it->avoid_cursor_p;
20219 it->avoid_cursor_p = true;
20220 saved_face_id = it->face_id;
20221 saved_box_start = it->start_of_box_run_p;
20222 /* The last row's stretch glyph should get the default
20223 face, to avoid painting the rest of the window with
20224 the region face, if the region ends at ZV. */
20225 if (it->glyph_row->ends_at_zv_p)
20226 it->face_id = default_face->id;
20227 else
20228 it->face_id = face->id;
20229 it->start_of_box_run_p = false;
20230 append_stretch_glyph (it, Qnil, stretch_width,
20231 it->ascent + it->descent, stretch_ascent);
20232 it->position = saved_pos;
20233 it->avoid_cursor_p = saved_avoid_cursor;
20234 it->face_id = saved_face_id;
20235 it->start_of_box_run_p = saved_box_start;
20237 /* If stretch_width comes out negative, it means that the
20238 last glyph is only partially visible. In R2L rows, we
20239 want the leftmost glyph to be partially visible, so we
20240 need to give the row the corresponding left offset. */
20241 if (stretch_width < 0)
20242 it->glyph_row->x = stretch_width;
20244 #endif /* HAVE_WINDOW_SYSTEM */
20246 else
20248 /* Save some values that must not be changed. */
20249 int saved_x = it->current_x;
20250 struct text_pos saved_pos;
20251 Lisp_Object saved_object;
20252 enum display_element_type saved_what = it->what;
20253 int saved_face_id = it->face_id;
20255 saved_object = it->object;
20256 saved_pos = it->position;
20258 it->what = IT_CHARACTER;
20259 memset (&it->position, 0, sizeof it->position);
20260 it->object = Qnil;
20261 it->c = it->char_to_display = ' ';
20262 it->len = 1;
20264 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20265 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20266 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20267 && !it->glyph_row->mode_line_p
20268 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20270 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20271 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20273 for (it->current_x = 0; g < e; g++)
20274 it->current_x += g->pixel_width;
20276 it->area = LEFT_MARGIN_AREA;
20277 it->face_id = default_face->id;
20278 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20279 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20280 && g < it->glyph_row->glyphs[TEXT_AREA])
20282 PRODUCE_GLYPHS (it);
20283 /* term.c:produce_glyphs advances it->current_x only for
20284 TEXT_AREA. */
20285 it->current_x += it->pixel_width;
20286 g++;
20289 it->current_x = saved_x;
20290 it->area = TEXT_AREA;
20293 /* The last row's blank glyphs should get the default face, to
20294 avoid painting the rest of the window with the region face,
20295 if the region ends at ZV. */
20296 if (it->glyph_row->ends_at_zv_p)
20297 it->face_id = default_face->id;
20298 else
20299 it->face_id = face->id;
20300 PRODUCE_GLYPHS (it);
20302 while (it->current_x <= it->last_visible_x)
20303 PRODUCE_GLYPHS (it);
20305 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20306 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20307 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20308 && !it->glyph_row->mode_line_p
20309 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20311 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20312 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20314 for ( ; g < e; g++)
20315 it->current_x += g->pixel_width;
20317 it->area = RIGHT_MARGIN_AREA;
20318 it->face_id = default_face->id;
20319 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20320 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20321 && g < it->glyph_row->glyphs[LAST_AREA])
20323 PRODUCE_GLYPHS (it);
20324 it->current_x += it->pixel_width;
20325 g++;
20328 it->area = TEXT_AREA;
20331 /* Don't count these blanks really. It would let us insert a left
20332 truncation glyph below and make us set the cursor on them, maybe. */
20333 it->current_x = saved_x;
20334 it->object = saved_object;
20335 it->position = saved_pos;
20336 it->what = saved_what;
20337 it->face_id = saved_face_id;
20342 /* Value is true if text starting at CHARPOS in current_buffer is
20343 trailing whitespace. */
20345 static bool
20346 trailing_whitespace_p (ptrdiff_t charpos)
20348 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20349 int c = 0;
20351 while (bytepos < ZV_BYTE
20352 && (c = FETCH_CHAR (bytepos),
20353 c == ' ' || c == '\t'))
20354 ++bytepos;
20356 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20358 if (bytepos != PT_BYTE)
20359 return true;
20361 return false;
20365 /* Highlight trailing whitespace, if any, in ROW. */
20367 static void
20368 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20370 int used = row->used[TEXT_AREA];
20372 if (used)
20374 struct glyph *start = row->glyphs[TEXT_AREA];
20375 struct glyph *glyph = start + used - 1;
20377 if (row->reversed_p)
20379 /* Right-to-left rows need to be processed in the opposite
20380 direction, so swap the edge pointers. */
20381 glyph = start;
20382 start = row->glyphs[TEXT_AREA] + used - 1;
20385 /* Skip over glyphs inserted to display the cursor at the
20386 end of a line, for extending the face of the last glyph
20387 to the end of the line on terminals, and for truncation
20388 and continuation glyphs. */
20389 if (!row->reversed_p)
20391 while (glyph >= start
20392 && glyph->type == CHAR_GLYPH
20393 && NILP (glyph->object))
20394 --glyph;
20396 else
20398 while (glyph <= start
20399 && glyph->type == CHAR_GLYPH
20400 && NILP (glyph->object))
20401 ++glyph;
20404 /* If last glyph is a space or stretch, and it's trailing
20405 whitespace, set the face of all trailing whitespace glyphs in
20406 IT->glyph_row to `trailing-whitespace'. */
20407 if ((row->reversed_p ? glyph <= start : glyph >= start)
20408 && BUFFERP (glyph->object)
20409 && (glyph->type == STRETCH_GLYPH
20410 || (glyph->type == CHAR_GLYPH
20411 && glyph->u.ch == ' '))
20412 && trailing_whitespace_p (glyph->charpos))
20414 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20415 if (face_id < 0)
20416 return;
20418 if (!row->reversed_p)
20420 while (glyph >= start
20421 && BUFFERP (glyph->object)
20422 && (glyph->type == STRETCH_GLYPH
20423 || (glyph->type == CHAR_GLYPH
20424 && glyph->u.ch == ' ')))
20425 (glyph--)->face_id = face_id;
20427 else
20429 while (glyph <= start
20430 && BUFFERP (glyph->object)
20431 && (glyph->type == STRETCH_GLYPH
20432 || (glyph->type == CHAR_GLYPH
20433 && glyph->u.ch == ' ')))
20434 (glyph++)->face_id = face_id;
20441 /* Value is true if glyph row ROW should be
20442 considered to hold the buffer position CHARPOS. */
20444 static bool
20445 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20447 bool result = true;
20449 if (charpos == CHARPOS (row->end.pos)
20450 || charpos == MATRIX_ROW_END_CHARPOS (row))
20452 /* Suppose the row ends on a string.
20453 Unless the row is continued, that means it ends on a newline
20454 in the string. If it's anything other than a display string
20455 (e.g., a before-string from an overlay), we don't want the
20456 cursor there. (This heuristic seems to give the optimal
20457 behavior for the various types of multi-line strings.)
20458 One exception: if the string has `cursor' property on one of
20459 its characters, we _do_ want the cursor there. */
20460 if (CHARPOS (row->end.string_pos) >= 0)
20462 if (row->continued_p)
20463 result = true;
20464 else
20466 /* Check for `display' property. */
20467 struct glyph *beg = row->glyphs[TEXT_AREA];
20468 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20469 struct glyph *glyph;
20471 result = false;
20472 for (glyph = end; glyph >= beg; --glyph)
20473 if (STRINGP (glyph->object))
20475 Lisp_Object prop
20476 = Fget_char_property (make_number (charpos),
20477 Qdisplay, Qnil);
20478 result =
20479 (!NILP (prop)
20480 && display_prop_string_p (prop, glyph->object));
20481 /* If there's a `cursor' property on one of the
20482 string's characters, this row is a cursor row,
20483 even though this is not a display string. */
20484 if (!result)
20486 Lisp_Object s = glyph->object;
20488 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20490 ptrdiff_t gpos = glyph->charpos;
20492 if (!NILP (Fget_char_property (make_number (gpos),
20493 Qcursor, s)))
20495 result = true;
20496 break;
20500 break;
20504 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20506 /* If the row ends in middle of a real character,
20507 and the line is continued, we want the cursor here.
20508 That's because CHARPOS (ROW->end.pos) would equal
20509 PT if PT is before the character. */
20510 if (!row->ends_in_ellipsis_p)
20511 result = row->continued_p;
20512 else
20513 /* If the row ends in an ellipsis, then
20514 CHARPOS (ROW->end.pos) will equal point after the
20515 invisible text. We want that position to be displayed
20516 after the ellipsis. */
20517 result = false;
20519 /* If the row ends at ZV, display the cursor at the end of that
20520 row instead of at the start of the row below. */
20521 else
20522 result = row->ends_at_zv_p;
20525 return result;
20528 /* Value is true if glyph row ROW should be
20529 used to hold the cursor. */
20531 static bool
20532 cursor_row_p (struct glyph_row *row)
20534 return row_for_charpos_p (row, PT);
20539 /* Push the property PROP so that it will be rendered at the current
20540 position in IT. Return true if PROP was successfully pushed, false
20541 otherwise. Called from handle_line_prefix to handle the
20542 `line-prefix' and `wrap-prefix' properties. */
20544 static bool
20545 push_prefix_prop (struct it *it, Lisp_Object prop)
20547 struct text_pos pos =
20548 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20550 eassert (it->method == GET_FROM_BUFFER
20551 || it->method == GET_FROM_DISPLAY_VECTOR
20552 || it->method == GET_FROM_STRING
20553 || it->method == GET_FROM_IMAGE);
20555 /* We need to save the current buffer/string position, so it will be
20556 restored by pop_it, because iterate_out_of_display_property
20557 depends on that being set correctly, but some situations leave
20558 it->position not yet set when this function is called. */
20559 push_it (it, &pos);
20561 if (STRINGP (prop))
20563 if (SCHARS (prop) == 0)
20565 pop_it (it);
20566 return false;
20569 it->string = prop;
20570 it->string_from_prefix_prop_p = true;
20571 it->multibyte_p = STRING_MULTIBYTE (it->string);
20572 it->current.overlay_string_index = -1;
20573 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20574 it->end_charpos = it->string_nchars = SCHARS (it->string);
20575 it->method = GET_FROM_STRING;
20576 it->stop_charpos = 0;
20577 it->prev_stop = 0;
20578 it->base_level_stop = 0;
20579 it->cmp_it.id = -1;
20581 /* Force paragraph direction to be that of the parent
20582 buffer/string. */
20583 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20584 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20585 else
20586 it->paragraph_embedding = L2R;
20588 /* Set up the bidi iterator for this display string. */
20589 if (it->bidi_p)
20591 it->bidi_it.string.lstring = it->string;
20592 it->bidi_it.string.s = NULL;
20593 it->bidi_it.string.schars = it->end_charpos;
20594 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20595 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20596 it->bidi_it.string.unibyte = !it->multibyte_p;
20597 it->bidi_it.w = it->w;
20598 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20601 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20603 it->method = GET_FROM_STRETCH;
20604 it->object = prop;
20606 #ifdef HAVE_WINDOW_SYSTEM
20607 else if (IMAGEP (prop))
20609 it->what = IT_IMAGE;
20610 it->image_id = lookup_image (it->f, prop);
20611 it->method = GET_FROM_IMAGE;
20613 #endif /* HAVE_WINDOW_SYSTEM */
20614 else
20616 pop_it (it); /* bogus display property, give up */
20617 return false;
20620 return true;
20623 /* Return the character-property PROP at the current position in IT. */
20625 static Lisp_Object
20626 get_it_property (struct it *it, Lisp_Object prop)
20628 Lisp_Object position, object = it->object;
20630 if (STRINGP (object))
20631 position = make_number (IT_STRING_CHARPOS (*it));
20632 else if (BUFFERP (object))
20634 position = make_number (IT_CHARPOS (*it));
20635 object = it->window;
20637 else
20638 return Qnil;
20640 return Fget_char_property (position, prop, object);
20643 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20645 static void
20646 handle_line_prefix (struct it *it)
20648 Lisp_Object prefix;
20650 if (it->continuation_lines_width > 0)
20652 prefix = get_it_property (it, Qwrap_prefix);
20653 if (NILP (prefix))
20654 prefix = Vwrap_prefix;
20656 else
20658 prefix = get_it_property (it, Qline_prefix);
20659 if (NILP (prefix))
20660 prefix = Vline_prefix;
20662 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20664 /* If the prefix is wider than the window, and we try to wrap
20665 it, it would acquire its own wrap prefix, and so on till the
20666 iterator stack overflows. So, don't wrap the prefix. */
20667 it->line_wrap = TRUNCATE;
20668 it->avoid_cursor_p = true;
20674 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20675 only for R2L lines from display_line and display_string, when they
20676 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20677 the line/string needs to be continued on the next glyph row. */
20678 static void
20679 unproduce_glyphs (struct it *it, int n)
20681 struct glyph *glyph, *end;
20683 eassert (it->glyph_row);
20684 eassert (it->glyph_row->reversed_p);
20685 eassert (it->area == TEXT_AREA);
20686 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20688 if (n > it->glyph_row->used[TEXT_AREA])
20689 n = it->glyph_row->used[TEXT_AREA];
20690 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20691 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20692 for ( ; glyph < end; glyph++)
20693 glyph[-n] = *glyph;
20696 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20697 and ROW->maxpos. */
20698 static void
20699 find_row_edges (struct it *it, struct glyph_row *row,
20700 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20701 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20703 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20704 lines' rows is implemented for bidi-reordered rows. */
20706 /* ROW->minpos is the value of min_pos, the minimal buffer position
20707 we have in ROW, or ROW->start.pos if that is smaller. */
20708 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20709 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20710 else
20711 /* We didn't find buffer positions smaller than ROW->start, or
20712 didn't find _any_ valid buffer positions in any of the glyphs,
20713 so we must trust the iterator's computed positions. */
20714 row->minpos = row->start.pos;
20715 if (max_pos <= 0)
20717 max_pos = CHARPOS (it->current.pos);
20718 max_bpos = BYTEPOS (it->current.pos);
20721 /* Here are the various use-cases for ending the row, and the
20722 corresponding values for ROW->maxpos:
20724 Line ends in a newline from buffer eol_pos + 1
20725 Line is continued from buffer max_pos + 1
20726 Line is truncated on right it->current.pos
20727 Line ends in a newline from string max_pos + 1(*)
20728 (*) + 1 only when line ends in a forward scan
20729 Line is continued from string max_pos
20730 Line is continued from display vector max_pos
20731 Line is entirely from a string min_pos == max_pos
20732 Line is entirely from a display vector min_pos == max_pos
20733 Line that ends at ZV ZV
20735 If you discover other use-cases, please add them here as
20736 appropriate. */
20737 if (row->ends_at_zv_p)
20738 row->maxpos = it->current.pos;
20739 else if (row->used[TEXT_AREA])
20741 bool seen_this_string = false;
20742 struct glyph_row *r1 = row - 1;
20744 /* Did we see the same display string on the previous row? */
20745 if (STRINGP (it->object)
20746 /* this is not the first row */
20747 && row > it->w->desired_matrix->rows
20748 /* previous row is not the header line */
20749 && !r1->mode_line_p
20750 /* previous row also ends in a newline from a string */
20751 && r1->ends_in_newline_from_string_p)
20753 struct glyph *start, *end;
20755 /* Search for the last glyph of the previous row that came
20756 from buffer or string. Depending on whether the row is
20757 L2R or R2L, we need to process it front to back or the
20758 other way round. */
20759 if (!r1->reversed_p)
20761 start = r1->glyphs[TEXT_AREA];
20762 end = start + r1->used[TEXT_AREA];
20763 /* Glyphs inserted by redisplay have nil as their object. */
20764 while (end > start
20765 && NILP ((end - 1)->object)
20766 && (end - 1)->charpos <= 0)
20767 --end;
20768 if (end > start)
20770 if (EQ ((end - 1)->object, it->object))
20771 seen_this_string = true;
20773 else
20774 /* If all the glyphs of the previous row were inserted
20775 by redisplay, it means the previous row was
20776 produced from a single newline, which is only
20777 possible if that newline came from the same string
20778 as the one which produced this ROW. */
20779 seen_this_string = true;
20781 else
20783 end = r1->glyphs[TEXT_AREA] - 1;
20784 start = end + r1->used[TEXT_AREA];
20785 while (end < start
20786 && NILP ((end + 1)->object)
20787 && (end + 1)->charpos <= 0)
20788 ++end;
20789 if (end < start)
20791 if (EQ ((end + 1)->object, it->object))
20792 seen_this_string = true;
20794 else
20795 seen_this_string = true;
20798 /* Take note of each display string that covers a newline only
20799 once, the first time we see it. This is for when a display
20800 string includes more than one newline in it. */
20801 if (row->ends_in_newline_from_string_p && !seen_this_string)
20803 /* If we were scanning the buffer forward when we displayed
20804 the string, we want to account for at least one buffer
20805 position that belongs to this row (position covered by
20806 the display string), so that cursor positioning will
20807 consider this row as a candidate when point is at the end
20808 of the visual line represented by this row. This is not
20809 required when scanning back, because max_pos will already
20810 have a much larger value. */
20811 if (CHARPOS (row->end.pos) > max_pos)
20812 INC_BOTH (max_pos, max_bpos);
20813 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20815 else if (CHARPOS (it->eol_pos) > 0)
20816 SET_TEXT_POS (row->maxpos,
20817 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20818 else if (row->continued_p)
20820 /* If max_pos is different from IT's current position, it
20821 means IT->method does not belong to the display element
20822 at max_pos. However, it also means that the display
20823 element at max_pos was displayed in its entirety on this
20824 line, which is equivalent to saying that the next line
20825 starts at the next buffer position. */
20826 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20827 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20828 else
20830 INC_BOTH (max_pos, max_bpos);
20831 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20834 else if (row->truncated_on_right_p)
20835 /* display_line already called reseat_at_next_visible_line_start,
20836 which puts the iterator at the beginning of the next line, in
20837 the logical order. */
20838 row->maxpos = it->current.pos;
20839 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20840 /* A line that is entirely from a string/image/stretch... */
20841 row->maxpos = row->minpos;
20842 else
20843 emacs_abort ();
20845 else
20846 row->maxpos = it->current.pos;
20849 /* Like display_count_lines, but capable of counting outside of the
20850 current narrowed region. */
20851 static ptrdiff_t
20852 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20853 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20855 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20856 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20858 ptrdiff_t val;
20859 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20860 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20861 Fwiden ();
20862 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20863 unbind_to (pdl_count, Qnil);
20864 return val;
20867 /* Count the number of screen lines in window IT->w between character
20868 position IT_CHARPOS(*IT) and the line showing that window's point. */
20869 static ptrdiff_t
20870 display_count_lines_visually (struct it *it)
20872 struct it tem_it;
20873 ptrdiff_t to;
20874 struct text_pos from;
20876 /* If we already calculated a relative line number, use that. This
20877 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20878 are laid out sequentially, one by one, for each sequence of calls
20879 to display_line or other similar function that follows a call to
20880 init_iterator. */
20881 if (it->lnum_bytepos > 0)
20882 return it->lnum + 1;
20883 else
20885 ptrdiff_t count = SPECPDL_INDEX ();
20887 if (IT_CHARPOS (*it) <= PT)
20889 from = it->current.pos;
20890 to = PT;
20892 else
20894 SET_TEXT_POS (from, PT, PT_BYTE);
20895 to = IT_CHARPOS (*it);
20897 start_display (&tem_it, it->w, from);
20898 /* Need to disable visual mode temporarily, since otherwise the
20899 call to move_it_to will cause infinite recursion. */
20900 specbind (Qdisplay_line_numbers, Qrelative);
20901 /* Some redisplay optimizations could invoke us very far from
20902 PT, which will make the caller painfully slow. There should
20903 be no need to go too far beyond the window's bottom, as any
20904 such optimization will fail to show point anyway. */
20905 move_it_to (&tem_it, to, -1,
20906 tem_it.last_visible_y
20907 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20908 -1, MOVE_TO_POS | MOVE_TO_Y);
20909 unbind_to (count, Qnil);
20910 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20914 /* Produce the line-number glyphs for the current glyph_row. If
20915 IT->glyph_row is non-NULL, populate the row with the produced
20916 glyphs. */
20917 static void
20918 maybe_produce_line_number (struct it *it)
20920 ptrdiff_t last_line = it->lnum;
20921 ptrdiff_t start_from, bytepos;
20922 ptrdiff_t this_line;
20923 bool first_time = false;
20924 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20925 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20926 void *itdata = bidi_shelve_cache ();
20928 if (EQ (Vdisplay_line_numbers, Qvisual))
20929 this_line = display_count_lines_visually (it);
20930 else
20932 if (!last_line)
20934 /* If possible, reuse data cached by line-number-mode. */
20935 if (it->w->base_line_number > 0
20936 && it->w->base_line_pos > 0
20937 && it->w->base_line_pos <= IT_CHARPOS (*it)
20938 /* line-number-mode always displays narrowed line
20939 numbers, so we cannot use its data if the user wants
20940 line numbers that disregard narrowing, or if the
20941 buffer's narrowing has just changed. */
20942 && !(display_line_numbers_widen
20943 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
20944 && !current_buffer->clip_changed)
20946 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20947 last_line = it->w->base_line_number - 1;
20949 else
20950 start_from = beg_byte;
20951 if (!it->lnum_bytepos)
20952 first_time = true;
20954 else
20955 start_from = it->lnum_bytepos;
20957 /* Paranoia: what if someone changes the narrowing since the
20958 last time display_line was called? Shouldn't really happen,
20959 but who knows what some crazy Lisp invoked by :eval could do? */
20960 if (!(beg_byte <= start_from && start_from <= z_byte))
20962 last_line = 0;
20963 start_from = beg_byte;
20966 this_line =
20967 last_line + display_count_lines_logically (start_from,
20968 IT_BYTEPOS (*it),
20969 IT_CHARPOS (*it), &bytepos);
20970 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20971 eassert (bytepos == IT_BYTEPOS (*it));
20974 /* Record the line number information. */
20975 if (this_line != last_line || !it->lnum_bytepos)
20977 it->lnum = this_line;
20978 it->lnum_bytepos = IT_BYTEPOS (*it);
20981 /* Produce the glyphs for the line number. */
20982 struct it tem_it;
20983 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20984 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20985 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20986 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20987 int current_lnum_face_id
20988 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20989 /* Compute point's line number if needed. */
20990 if ((EQ (Vdisplay_line_numbers, Qrelative)
20991 || EQ (Vdisplay_line_numbers, Qvisual)
20992 || lnum_face_id != current_lnum_face_id)
20993 && !it->pt_lnum)
20995 ptrdiff_t ignored;
20996 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20997 it->pt_lnum =
20998 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20999 PT, &ignored);
21000 else
21001 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21002 &ignored);
21004 /* Compute the required width if needed. */
21005 if (!it->lnum_width)
21007 if (NATNUMP (Vdisplay_line_numbers_width))
21008 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21010 /* Max line number to be displayed cannot be more than the one
21011 corresponding to the last row of the desired matrix. */
21012 ptrdiff_t max_lnum;
21014 if (NILP (Vdisplay_line_numbers_current_absolute)
21015 && (EQ (Vdisplay_line_numbers, Qrelative)
21016 || EQ (Vdisplay_line_numbers, Qvisual)))
21017 /* We subtract one more because the current line is always
21018 zero in this mode. */
21019 max_lnum = it->w->desired_matrix->nrows - 2;
21020 else if (EQ (Vdisplay_line_numbers, Qvisual))
21021 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21022 else
21023 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21024 max_lnum = max (1, max_lnum);
21025 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21026 eassert (it->lnum_width > 0);
21028 if (EQ (Vdisplay_line_numbers, Qrelative))
21029 lnum_offset = it->pt_lnum;
21030 else if (EQ (Vdisplay_line_numbers, Qvisual))
21031 lnum_offset = 0;
21033 /* Under 'relative', display the absolute line number for the
21034 current line, unless the user requests otherwise. */
21035 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21036 if ((EQ (Vdisplay_line_numbers, Qrelative)
21037 || EQ (Vdisplay_line_numbers, Qvisual))
21038 && lnum_to_display == 0
21039 && !NILP (Vdisplay_line_numbers_current_absolute))
21040 lnum_to_display = it->pt_lnum + 1;
21041 /* In L2R rows we need to append the blank separator, in R2L
21042 rows we need to prepend it. But this function is usually
21043 called when no display elements were produced from the
21044 following line, so the paragraph direction might be unknown.
21045 Therefore we cheat and add 2 blanks, one on either side. */
21046 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21047 strcat (lnum_buf, " ");
21049 /* Setup for producing the glyphs. */
21050 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21051 /* FIXME: Use specialized face. */
21052 DEFAULT_FACE_ID);
21053 scratch_glyph_row.reversed_p = false;
21054 scratch_glyph_row.used[TEXT_AREA] = 0;
21055 SET_TEXT_POS (tem_it.position, 0, 0);
21056 tem_it.avoid_cursor_p = true;
21057 tem_it.bidi_p = true;
21058 tem_it.bidi_it.type = WEAK_EN;
21059 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21060 1 level in R2L paragraphs. Emulate that, assuming we are in
21061 an L2R paragraph. */
21062 tem_it.bidi_it.resolved_level = 2;
21064 /* Produce glyphs for the line number in a scratch glyph_row. */
21065 int n_glyphs_before;
21066 for (const char *p = lnum_buf; *p; p++)
21068 /* For continuation lines and lines after ZV, instead of a line
21069 number, produce a blank prefix of the same width. Use the
21070 default face for the blank field beyond ZV. */
21071 if (beyond_zv)
21072 tem_it.face_id = it->base_face_id;
21073 else if (lnum_face_id != current_lnum_face_id
21074 && (EQ (Vdisplay_line_numbers, Qvisual)
21075 ? this_line == 0
21076 : this_line == it->pt_lnum))
21077 tem_it.face_id = current_lnum_face_id;
21078 else
21079 tem_it.face_id = lnum_face_id;
21080 if (beyond_zv
21081 /* Don't display the same line number more than once. */
21082 || (!EQ (Vdisplay_line_numbers, Qvisual)
21083 && (it->continuation_lines_width > 0
21084 || (this_line == last_line && !first_time))))
21085 tem_it.c = tem_it.char_to_display = ' ';
21086 else
21087 tem_it.c = tem_it.char_to_display = *p;
21088 tem_it.len = 1;
21089 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21090 /* Make sure these glyphs will have a "position" of -1. */
21091 SET_TEXT_POS (tem_it.position, -1, -1);
21092 PRODUCE_GLYPHS (&tem_it);
21094 /* Stop producing glyphs if we don't have enough space on
21095 this line. FIXME: should we refrain from producing the
21096 line number at all in that case? */
21097 if (tem_it.current_x > tem_it.last_visible_x)
21099 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21100 break;
21104 /* Record the width in pixels we need for the line number display. */
21105 it->lnum_pixel_width = tem_it.current_x;
21106 /* Copy the produced glyphs into IT's glyph_row. */
21107 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21108 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21109 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21110 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21112 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21114 for ( ; g < e; g++)
21116 it->current_x += g->pixel_width;
21117 /* The following is important when this function is called
21118 from move_it_in_display_line_to: HPOS is incremented only
21119 when we are in the visible portion of the glyph row. */
21120 if (it->current_x > it->first_visible_x)
21121 it->hpos++;
21122 if (p)
21124 *p++ = *g;
21125 (*u)++;
21129 /* Update IT's metrics due to glyphs produced for line numbers. */
21130 if (it->glyph_row)
21132 struct glyph_row *row = it->glyph_row;
21134 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21135 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21136 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21137 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21138 tem_it.max_phys_descent);
21140 else
21142 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21143 it->max_descent = max (it->max_descent, tem_it.max_descent);
21144 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21145 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21148 bidi_unshelve_cache (itdata, false);
21151 /* Return true if this glyph row needs a line number to be produced
21152 for it. */
21153 static bool
21154 should_produce_line_number (struct it *it)
21156 if (NILP (Vdisplay_line_numbers))
21157 return false;
21159 /* Don't display line numbers in minibuffer windows. */
21160 if (MINI_WINDOW_P (it->w))
21161 return false;
21163 #ifdef HAVE_WINDOW_SYSTEM
21164 /* Don't display line number in tooltip frames. */
21165 if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
21166 return false;
21167 #endif
21169 /* If the character at current position has a non-nil special
21170 property, disable line numbers for this row. This is for
21171 packages such as company-mode, which need this for their tricky
21172 layout, where line numbers get in the way. */
21173 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21174 Qdisplay_line_numbers_disable,
21175 it->window);
21176 /* For ZV, we need to also look in empty overlays at that point,
21177 because get-char-property always returns nil for ZV, except if
21178 the property is in 'default-text-properties'. */
21179 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21180 val = disable_line_numbers_overlay_at_eob ();
21181 return NILP (val) ? true : false;
21184 /* Return true if ROW has no glyphs except those inserted by the
21185 display engine. This is needed for indicate-empty-lines and
21186 similar features when the glyph row starts with glyphs which didn't
21187 come from buffer or string. */
21188 static bool
21189 row_text_area_empty (struct glyph_row *row)
21191 if (!row->reversed_p)
21193 for (struct glyph *g = row->glyphs[TEXT_AREA];
21194 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21195 g++)
21196 if (!NILP (g->object) || g->charpos > 0)
21197 return false;
21199 else
21201 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21202 g > row->glyphs[TEXT_AREA];
21203 g--)
21204 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21205 return false;
21208 return true;
21211 /* Construct the glyph row IT->glyph_row in the desired matrix of
21212 IT->w from text at the current position of IT. See dispextern.h
21213 for an overview of struct it. Value is true if
21214 IT->glyph_row displays text, as opposed to a line displaying ZV
21215 only. CURSOR_VPOS is the window-relative vertical position of
21216 the glyph row displaying the cursor, or -1 if unknown. */
21218 static bool
21219 display_line (struct it *it, int cursor_vpos)
21221 struct glyph_row *row = it->glyph_row;
21222 Lisp_Object overlay_arrow_string;
21223 struct it wrap_it;
21224 void *wrap_data = NULL;
21225 bool may_wrap = false;
21226 int wrap_x UNINIT;
21227 int wrap_row_used = -1;
21228 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21229 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21230 int wrap_row_extra_line_spacing UNINIT;
21231 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21232 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21233 int cvpos;
21234 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21235 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21236 bool pending_handle_line_prefix = false;
21237 int header_line = window_wants_header_line (it->w);
21238 bool hscroll_this_line = (cursor_vpos >= 0
21239 && it->vpos == cursor_vpos - header_line
21240 && hscrolling_current_line_p (it->w));
21241 int first_visible_x = it->first_visible_x;
21242 int last_visible_x = it->last_visible_x;
21243 int x_incr = 0;
21245 /* We always start displaying at hpos zero even if hscrolled. */
21246 eassert (it->hpos == 0 && it->current_x == 0);
21248 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21249 >= it->w->desired_matrix->nrows)
21251 it->w->nrows_scale_factor++;
21252 it->f->fonts_changed = true;
21253 return false;
21256 /* Clear the result glyph row and enable it. */
21257 prepare_desired_row (it->w, row, false);
21259 row->y = it->current_y;
21260 row->start = it->start;
21261 row->continuation_lines_width = it->continuation_lines_width;
21262 row->displays_text_p = true;
21263 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21264 it->starts_in_middle_of_char_p = false;
21266 /* Arrange the overlays nicely for our purposes. Usually, we call
21267 display_line on only one line at a time, in which case this
21268 can't really hurt too much, or we call it on lines which appear
21269 one after another in the buffer, in which case all calls to
21270 recenter_overlay_lists but the first will be pretty cheap. */
21271 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21273 /* If we are going to display the cursor's line, account for the
21274 hscroll of that line. We subtract the window's min_hscroll,
21275 because that was already accounted for in init_iterator. */
21276 if (hscroll_this_line)
21277 x_incr =
21278 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21279 * FRAME_COLUMN_WIDTH (it->f);
21281 bool line_number_needed = should_produce_line_number (it);
21283 /* Move over display elements that are not visible because we are
21284 hscrolled. This may stop at an x-position < first_visible_x
21285 if the first glyph is partially visible or if we hit a line end. */
21286 if (it->current_x < it->first_visible_x + x_incr)
21288 enum move_it_result move_result;
21290 this_line_min_pos = row->start.pos;
21291 if (hscroll_this_line)
21293 it->first_visible_x += x_incr;
21294 it->last_visible_x += x_incr;
21296 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21297 MOVE_TO_POS | MOVE_TO_X);
21298 /* If we are under a large hscroll, move_it_in_display_line_to
21299 could hit the end of the line without reaching
21300 first_visible_x. Pretend that we did reach it. This is
21301 especially important on a TTY, where we will call
21302 extend_face_to_end_of_line, which needs to know how many
21303 blank glyphs to produce. */
21304 if (it->current_x < it->first_visible_x
21305 && (move_result == MOVE_NEWLINE_OR_CR
21306 || move_result == MOVE_POS_MATCH_OR_ZV))
21307 it->current_x = it->first_visible_x;
21309 /* Record the smallest positions seen while we moved over
21310 display elements that are not visible. This is needed by
21311 redisplay_internal for optimizing the case where the cursor
21312 stays inside the same line. The rest of this function only
21313 considers positions that are actually displayed, so
21314 RECORD_MAX_MIN_POS will not otherwise record positions that
21315 are hscrolled to the left of the left edge of the window. */
21316 min_pos = CHARPOS (this_line_min_pos);
21317 min_bpos = BYTEPOS (this_line_min_pos);
21319 /* Produce line number, if needed. */
21320 if (line_number_needed)
21321 maybe_produce_line_number (it);
21323 else if (it->area == TEXT_AREA)
21325 /* Line numbers should precede the line-prefix or wrap-prefix. */
21326 if (line_number_needed)
21327 maybe_produce_line_number (it);
21329 /* We only do this when not calling move_it_in_display_line_to
21330 above, because that function calls itself handle_line_prefix. */
21331 handle_line_prefix (it);
21333 else
21335 /* Line-prefix and wrap-prefix are always displayed in the text
21336 area. But if this is the first call to display_line after
21337 init_iterator, the iterator might have been set up to write
21338 into a marginal area, e.g. if the line begins with some
21339 display property that writes to the margins. So we need to
21340 wait with the call to handle_line_prefix until whatever
21341 writes to the margin has done its job. */
21342 pending_handle_line_prefix = true;
21345 /* Get the initial row height. This is either the height of the
21346 text hscrolled, if there is any, or zero. */
21347 row->ascent = it->max_ascent;
21348 row->height = it->max_ascent + it->max_descent;
21349 row->phys_ascent = it->max_phys_ascent;
21350 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21351 row->extra_line_spacing = it->max_extra_line_spacing;
21353 /* Utility macro to record max and min buffer positions seen until now. */
21354 #define RECORD_MAX_MIN_POS(IT) \
21355 do \
21357 bool composition_p \
21358 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21359 ptrdiff_t current_pos = \
21360 composition_p ? (IT)->cmp_it.charpos \
21361 : IT_CHARPOS (*(IT)); \
21362 ptrdiff_t current_bpos = \
21363 composition_p ? CHAR_TO_BYTE (current_pos) \
21364 : IT_BYTEPOS (*(IT)); \
21365 if (current_pos < min_pos) \
21367 min_pos = current_pos; \
21368 min_bpos = current_bpos; \
21370 if (IT_CHARPOS (*it) > max_pos) \
21372 max_pos = IT_CHARPOS (*it); \
21373 max_bpos = IT_BYTEPOS (*it); \
21376 while (false)
21378 /* Loop generating characters. The loop is left with IT on the next
21379 character to display. */
21380 while (true)
21382 int n_glyphs_before, hpos_before, x_before;
21383 int x, nglyphs;
21384 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21386 /* Retrieve the next thing to display. Value is false if end of
21387 buffer reached. */
21388 if (!get_next_display_element (it))
21390 bool row_has_glyphs = false;
21391 /* Maybe add a space at the end of this line that is used to
21392 display the cursor there under X. Set the charpos of the
21393 first glyph of blank lines not corresponding to any text
21394 to -1. */
21395 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21396 row->exact_window_width_line_p = true;
21397 else if ((append_space_for_newline (it, true)
21398 && row->used[TEXT_AREA] == 1)
21399 || row->used[TEXT_AREA] == 0
21400 || (row_has_glyphs = row_text_area_empty (row)))
21402 row->glyphs[TEXT_AREA]->charpos = -1;
21403 /* Don't reset the displays_text_p flag if we are
21404 displaying line numbers or line-prefix. */
21405 if (!row_has_glyphs)
21406 row->displays_text_p = false;
21408 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21409 && (!MINI_WINDOW_P (it->w)))
21410 row->indicate_empty_line_p = true;
21413 it->continuation_lines_width = 0;
21414 /* Reset those iterator values set from display property
21415 values. This is for the case when the display property
21416 ends at ZV, and is not a replacing property, so pop_it is
21417 not called. */
21418 it->font_height = Qnil;
21419 it->voffset = 0;
21420 row->ends_at_zv_p = true;
21421 /* A row that displays right-to-left text must always have
21422 its last face extended all the way to the end of line,
21423 even if this row ends in ZV, because we still write to
21424 the screen left to right. We also need to extend the
21425 last face if the default face is remapped to some
21426 different face, otherwise the functions that clear
21427 portions of the screen will clear with the default face's
21428 background color. */
21429 if (row->reversed_p
21430 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21431 extend_face_to_end_of_line (it);
21432 break;
21435 /* Now, get the metrics of what we want to display. This also
21436 generates glyphs in `row' (which is IT->glyph_row). */
21437 n_glyphs_before = row->used[TEXT_AREA];
21438 x = it->current_x;
21440 /* Remember the line height so far in case the next element doesn't
21441 fit on the line. */
21442 if (it->line_wrap != TRUNCATE)
21444 ascent = it->max_ascent;
21445 descent = it->max_descent;
21446 phys_ascent = it->max_phys_ascent;
21447 phys_descent = it->max_phys_descent;
21449 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21451 if (IT_DISPLAYING_WHITESPACE (it))
21452 may_wrap = true;
21453 else if (may_wrap)
21455 SAVE_IT (wrap_it, *it, wrap_data);
21456 wrap_x = x;
21457 wrap_row_used = row->used[TEXT_AREA];
21458 wrap_row_ascent = row->ascent;
21459 wrap_row_height = row->height;
21460 wrap_row_phys_ascent = row->phys_ascent;
21461 wrap_row_phys_height = row->phys_height;
21462 wrap_row_extra_line_spacing = row->extra_line_spacing;
21463 wrap_row_min_pos = min_pos;
21464 wrap_row_min_bpos = min_bpos;
21465 wrap_row_max_pos = max_pos;
21466 wrap_row_max_bpos = max_bpos;
21467 may_wrap = false;
21472 PRODUCE_GLYPHS (it);
21474 /* If this display element was in marginal areas, continue with
21475 the next one. */
21476 if (it->area != TEXT_AREA)
21478 row->ascent = max (row->ascent, it->max_ascent);
21479 row->height = max (row->height, it->max_ascent + it->max_descent);
21480 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21481 row->phys_height = max (row->phys_height,
21482 it->max_phys_ascent + it->max_phys_descent);
21483 row->extra_line_spacing = max (row->extra_line_spacing,
21484 it->max_extra_line_spacing);
21485 set_iterator_to_next (it, true);
21486 /* If we didn't handle the line/wrap prefix above, and the
21487 call to set_iterator_to_next just switched to TEXT_AREA,
21488 process the prefix now. */
21489 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21491 /* Line numbers should precede the line-prefix or wrap-prefix. */
21492 if (line_number_needed)
21493 maybe_produce_line_number (it);
21495 pending_handle_line_prefix = false;
21496 handle_line_prefix (it);
21498 continue;
21501 /* Does the display element fit on the line? If we truncate
21502 lines, we should draw past the right edge of the window. If
21503 we don't truncate, we want to stop so that we can display the
21504 continuation glyph before the right margin. If lines are
21505 continued, there are two possible strategies for characters
21506 resulting in more than 1 glyph (e.g. tabs): Display as many
21507 glyphs as possible in this line and leave the rest for the
21508 continuation line, or display the whole element in the next
21509 line. Original redisplay did the former, so we do it also. */
21510 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21511 hpos_before = it->hpos;
21512 x_before = x;
21514 if (/* Not a newline. */
21515 nglyphs > 0
21516 /* Glyphs produced fit entirely in the line. */
21517 && it->current_x < it->last_visible_x)
21519 it->hpos += nglyphs;
21520 row->ascent = max (row->ascent, it->max_ascent);
21521 row->height = max (row->height, it->max_ascent + it->max_descent);
21522 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21523 row->phys_height = max (row->phys_height,
21524 it->max_phys_ascent + it->max_phys_descent);
21525 row->extra_line_spacing = max (row->extra_line_spacing,
21526 it->max_extra_line_spacing);
21527 if (it->current_x - it->pixel_width < it->first_visible_x
21528 /* In R2L rows, we arrange in extend_face_to_end_of_line
21529 to add a right offset to the line, by a suitable
21530 change to the stretch glyph that is the leftmost
21531 glyph of the line. */
21532 && !row->reversed_p)
21533 row->x = x - it->first_visible_x;
21534 /* Record the maximum and minimum buffer positions seen so
21535 far in glyphs that will be displayed by this row. */
21536 if (it->bidi_p)
21537 RECORD_MAX_MIN_POS (it);
21539 else
21541 int i, new_x;
21542 struct glyph *glyph;
21544 for (i = 0; i < nglyphs; ++i, x = new_x)
21546 /* Identify the glyphs added by the last call to
21547 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21548 the previous glyphs. */
21549 if (!row->reversed_p)
21550 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21551 else
21552 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21553 new_x = x + glyph->pixel_width;
21555 if (/* Lines are continued. */
21556 it->line_wrap != TRUNCATE
21557 && (/* Glyph doesn't fit on the line. */
21558 new_x > it->last_visible_x
21559 /* Or it fits exactly on a window system frame. */
21560 || (new_x == it->last_visible_x
21561 && FRAME_WINDOW_P (it->f)
21562 && (row->reversed_p
21563 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21564 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21566 /* End of a continued line. */
21568 if (it->hpos == 0
21569 || (new_x == it->last_visible_x
21570 && FRAME_WINDOW_P (it->f)
21571 && (row->reversed_p
21572 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21573 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21575 /* Current glyph is the only one on the line or
21576 fits exactly on the line. We must continue
21577 the line because we can't draw the cursor
21578 after the glyph. */
21579 row->continued_p = true;
21580 it->current_x = new_x;
21581 it->continuation_lines_width += new_x;
21582 ++it->hpos;
21583 if (i == nglyphs - 1)
21585 /* If line-wrap is on, check if a previous
21586 wrap point was found. */
21587 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21588 && wrap_row_used > 0
21589 /* Even if there is a previous wrap
21590 point, continue the line here as
21591 usual, if (i) the previous character
21592 was a space or tab AND (ii) the
21593 current character is not. */
21594 && (!may_wrap
21595 || IT_DISPLAYING_WHITESPACE (it)))
21596 goto back_to_wrap;
21598 /* Record the maximum and minimum buffer
21599 positions seen so far in glyphs that will be
21600 displayed by this row. */
21601 if (it->bidi_p)
21602 RECORD_MAX_MIN_POS (it);
21603 set_iterator_to_next (it, true);
21604 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21606 if (!get_next_display_element (it))
21608 row->exact_window_width_line_p = true;
21609 it->continuation_lines_width = 0;
21610 it->font_height = Qnil;
21611 it->voffset = 0;
21612 row->continued_p = false;
21613 row->ends_at_zv_p = true;
21615 else if (ITERATOR_AT_END_OF_LINE_P (it))
21617 row->continued_p = false;
21618 row->exact_window_width_line_p = true;
21620 /* If line-wrap is on, check if a
21621 previous wrap point was found. */
21622 else if (wrap_row_used > 0
21623 /* Even if there is a previous wrap
21624 point, continue the line here as
21625 usual, if (i) the previous character
21626 was a space or tab AND (ii) the
21627 current character is not. */
21628 && (!may_wrap
21629 || IT_DISPLAYING_WHITESPACE (it)))
21630 goto back_to_wrap;
21634 else if (it->bidi_p)
21635 RECORD_MAX_MIN_POS (it);
21636 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21637 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21638 extend_face_to_end_of_line (it);
21640 else if (CHAR_GLYPH_PADDING_P (*glyph)
21641 && !FRAME_WINDOW_P (it->f))
21643 /* A padding glyph that doesn't fit on this line.
21644 This means the whole character doesn't fit
21645 on the line. */
21646 if (row->reversed_p)
21647 unproduce_glyphs (it, row->used[TEXT_AREA]
21648 - n_glyphs_before);
21649 row->used[TEXT_AREA] = n_glyphs_before;
21651 /* Fill the rest of the row with continuation
21652 glyphs like in 20.x. */
21653 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21654 < row->glyphs[1 + TEXT_AREA])
21655 produce_special_glyphs (it, IT_CONTINUATION);
21657 row->continued_p = true;
21658 it->current_x = x_before;
21659 it->continuation_lines_width += x_before;
21661 /* Restore the height to what it was before the
21662 element not fitting on the line. */
21663 it->max_ascent = ascent;
21664 it->max_descent = descent;
21665 it->max_phys_ascent = phys_ascent;
21666 it->max_phys_descent = phys_descent;
21667 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21668 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21669 extend_face_to_end_of_line (it);
21671 else if (wrap_row_used > 0)
21673 back_to_wrap:
21674 if (row->reversed_p)
21675 unproduce_glyphs (it,
21676 row->used[TEXT_AREA] - wrap_row_used);
21677 RESTORE_IT (it, &wrap_it, wrap_data);
21678 it->continuation_lines_width += wrap_x;
21679 row->used[TEXT_AREA] = wrap_row_used;
21680 row->ascent = wrap_row_ascent;
21681 row->height = wrap_row_height;
21682 row->phys_ascent = wrap_row_phys_ascent;
21683 row->phys_height = wrap_row_phys_height;
21684 row->extra_line_spacing = wrap_row_extra_line_spacing;
21685 min_pos = wrap_row_min_pos;
21686 min_bpos = wrap_row_min_bpos;
21687 max_pos = wrap_row_max_pos;
21688 max_bpos = wrap_row_max_bpos;
21689 row->continued_p = true;
21690 row->ends_at_zv_p = false;
21691 row->exact_window_width_line_p = false;
21693 /* Make sure that a non-default face is extended
21694 up to the right margin of the window. */
21695 extend_face_to_end_of_line (it);
21697 else if ((it->what == IT_CHARACTER
21698 || it->what == IT_STRETCH
21699 || it->what == IT_COMPOSITION)
21700 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21702 /* A TAB that extends past the right edge of the
21703 window. This produces a single glyph on
21704 window system frames. We leave the glyph in
21705 this row and let it fill the row, but don't
21706 consume the TAB. */
21707 if ((row->reversed_p
21708 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21709 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21710 produce_special_glyphs (it, IT_CONTINUATION);
21711 it->continuation_lines_width += it->last_visible_x;
21712 row->ends_in_middle_of_char_p = true;
21713 row->continued_p = true;
21714 glyph->pixel_width = it->last_visible_x - x;
21715 it->starts_in_middle_of_char_p = true;
21716 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21717 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21718 extend_face_to_end_of_line (it);
21720 else
21722 /* Something other than a TAB that draws past
21723 the right edge of the window. Restore
21724 positions to values before the element. */
21725 if (row->reversed_p)
21726 unproduce_glyphs (it, row->used[TEXT_AREA]
21727 - (n_glyphs_before + i));
21728 row->used[TEXT_AREA] = n_glyphs_before + i;
21730 /* Display continuation glyphs. */
21731 it->current_x = x_before;
21732 it->continuation_lines_width += x;
21733 if (!FRAME_WINDOW_P (it->f)
21734 || (row->reversed_p
21735 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21736 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21737 produce_special_glyphs (it, IT_CONTINUATION);
21738 row->continued_p = true;
21740 extend_face_to_end_of_line (it);
21742 if (nglyphs > 1 && i > 0)
21744 row->ends_in_middle_of_char_p = true;
21745 it->starts_in_middle_of_char_p = true;
21748 /* Restore the height to what it was before the
21749 element not fitting on the line. */
21750 it->max_ascent = ascent;
21751 it->max_descent = descent;
21752 it->max_phys_ascent = phys_ascent;
21753 it->max_phys_descent = phys_descent;
21756 break;
21758 else if (new_x > it->first_visible_x)
21760 /* Increment number of glyphs actually displayed. */
21761 ++it->hpos;
21763 /* Record the maximum and minimum buffer positions
21764 seen so far in glyphs that will be displayed by
21765 this row. */
21766 if (it->bidi_p)
21767 RECORD_MAX_MIN_POS (it);
21769 if (x < it->first_visible_x && !row->reversed_p)
21770 /* Glyph is partially visible, i.e. row starts at
21771 negative X position. Don't do that in R2L
21772 rows, where we arrange to add a right offset to
21773 the line in extend_face_to_end_of_line, by a
21774 suitable change to the stretch glyph that is
21775 the leftmost glyph of the line. */
21776 row->x = x - it->first_visible_x;
21777 /* When the last glyph of an R2L row only fits
21778 partially on the line, we need to set row->x to a
21779 negative offset, so that the leftmost glyph is
21780 the one that is partially visible. But if we are
21781 going to produce the truncation glyph, this will
21782 be taken care of in produce_special_glyphs. */
21783 if (row->reversed_p
21784 && new_x > it->last_visible_x
21785 && !(it->line_wrap == TRUNCATE
21786 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21788 eassert (FRAME_WINDOW_P (it->f));
21789 row->x = it->last_visible_x - new_x;
21792 else
21794 /* Glyph is completely off the left margin of the
21795 window. This should not happen because of the
21796 move_it_in_display_line at the start of this
21797 function, unless the text display area of the
21798 window is empty. */
21799 eassert (it->first_visible_x <= it->last_visible_x);
21802 /* Even if this display element produced no glyphs at all,
21803 we want to record its position. */
21804 if (it->bidi_p && nglyphs == 0)
21805 RECORD_MAX_MIN_POS (it);
21807 row->ascent = max (row->ascent, it->max_ascent);
21808 row->height = max (row->height, it->max_ascent + it->max_descent);
21809 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21810 row->phys_height = max (row->phys_height,
21811 it->max_phys_ascent + it->max_phys_descent);
21812 row->extra_line_spacing = max (row->extra_line_spacing,
21813 it->max_extra_line_spacing);
21815 /* End of this display line if row is continued. */
21816 if (row->continued_p || row->ends_at_zv_p)
21817 break;
21820 at_end_of_line:
21821 /* Is this a line end? If yes, we're also done, after making
21822 sure that a non-default face is extended up to the right
21823 margin of the window. */
21824 if (ITERATOR_AT_END_OF_LINE_P (it))
21826 int used_before = row->used[TEXT_AREA];
21828 row->ends_in_newline_from_string_p = STRINGP (it->object);
21830 /* Add a space at the end of the line that is used to
21831 display the cursor there. */
21832 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21833 append_space_for_newline (it, false);
21835 /* Extend the face to the end of the line. */
21836 extend_face_to_end_of_line (it);
21838 /* Make sure we have the position. */
21839 if (used_before == 0)
21840 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21842 /* Record the position of the newline, for use in
21843 find_row_edges. */
21844 it->eol_pos = it->current.pos;
21846 /* Consume the line end. This skips over invisible lines. */
21847 set_iterator_to_next (it, true);
21848 it->continuation_lines_width = 0;
21849 break;
21852 /* Proceed with next display element. Note that this skips
21853 over lines invisible because of selective display. */
21854 set_iterator_to_next (it, true);
21856 /* If we truncate lines, we are done when the last displayed
21857 glyphs reach past the right margin of the window. */
21858 if (it->line_wrap == TRUNCATE
21859 && ((FRAME_WINDOW_P (it->f)
21860 /* Images are preprocessed in produce_image_glyph such
21861 that they are cropped at the right edge of the
21862 window, so an image glyph will always end exactly at
21863 last_visible_x, even if there's no right fringe. */
21864 && ((row->reversed_p
21865 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21866 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21867 || it->what == IT_IMAGE))
21868 ? (it->current_x >= it->last_visible_x)
21869 : (it->current_x > it->last_visible_x)))
21871 /* Maybe add truncation glyphs. */
21872 if (!FRAME_WINDOW_P (it->f)
21873 || (row->reversed_p
21874 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21875 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21877 int i, n;
21879 if (!row->reversed_p)
21881 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21882 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21883 break;
21885 else
21887 for (i = 0; i < row->used[TEXT_AREA]; i++)
21888 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21889 break;
21890 /* Remove any padding glyphs at the front of ROW, to
21891 make room for the truncation glyphs we will be
21892 adding below. The loop below always inserts at
21893 least one truncation glyph, so also remove the
21894 last glyph added to ROW. */
21895 unproduce_glyphs (it, i + 1);
21896 /* Adjust i for the loop below. */
21897 i = row->used[TEXT_AREA] - (i + 1);
21900 /* produce_special_glyphs overwrites the last glyph, so
21901 we don't want that if we want to keep that last
21902 glyph, which means it's an image. */
21903 if (it->current_x > it->last_visible_x)
21905 it->current_x = x_before;
21906 if (!FRAME_WINDOW_P (it->f))
21908 for (n = row->used[TEXT_AREA]; i < n; ++i)
21910 row->used[TEXT_AREA] = i;
21911 produce_special_glyphs (it, IT_TRUNCATION);
21914 else
21916 row->used[TEXT_AREA] = i;
21917 produce_special_glyphs (it, IT_TRUNCATION);
21919 it->hpos = hpos_before;
21922 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21924 /* Don't truncate if we can overflow newline into fringe. */
21925 if (!get_next_display_element (it))
21927 it->continuation_lines_width = 0;
21928 it->font_height = Qnil;
21929 it->voffset = 0;
21930 row->ends_at_zv_p = true;
21931 row->exact_window_width_line_p = true;
21932 break;
21934 if (ITERATOR_AT_END_OF_LINE_P (it))
21936 row->exact_window_width_line_p = true;
21937 goto at_end_of_line;
21939 it->current_x = x_before;
21940 it->hpos = hpos_before;
21943 row->truncated_on_right_p = true;
21944 it->continuation_lines_width = 0;
21945 reseat_at_next_visible_line_start (it, false);
21946 /* We insist below that IT's position be at ZV because in
21947 bidi-reordered lines the character at visible line start
21948 might not be the character that follows the newline in
21949 the logical order. */
21950 if (IT_BYTEPOS (*it) > BEG_BYTE)
21951 row->ends_at_zv_p =
21952 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21953 else
21954 row->ends_at_zv_p = false;
21955 break;
21959 if (wrap_data)
21960 bidi_unshelve_cache (wrap_data, true);
21962 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21963 at the left window margin. */
21964 if (it->first_visible_x
21965 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21967 if (!FRAME_WINDOW_P (it->f)
21968 || (((row->reversed_p
21969 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21970 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21971 /* Don't let insert_left_trunc_glyphs overwrite the
21972 first glyph of the row if it is an image. */
21973 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21974 insert_left_trunc_glyphs (it);
21975 row->truncated_on_left_p = true;
21978 /* Remember the position at which this line ends.
21980 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21981 cannot be before the call to find_row_edges below, since that is
21982 where these positions are determined. */
21983 row->end = it->current;
21984 if (!it->bidi_p)
21986 row->minpos = row->start.pos;
21987 row->maxpos = row->end.pos;
21989 else
21991 /* ROW->minpos and ROW->maxpos must be the smallest and
21992 `1 + the largest' buffer positions in ROW. But if ROW was
21993 bidi-reordered, these two positions can be anywhere in the
21994 row, so we must determine them now. */
21995 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21998 /* If the start of this line is the overlay arrow-position, then
21999 mark this glyph row as the one containing the overlay arrow.
22000 This is clearly a mess with variable size fonts. It would be
22001 better to let it be displayed like cursors under X. */
22002 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22003 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22004 !NILP (overlay_arrow_string)))
22006 /* Overlay arrow in window redisplay is a fringe bitmap. */
22007 if (STRINGP (overlay_arrow_string))
22009 struct glyph_row *arrow_row
22010 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22011 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22012 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22013 struct glyph *p = row->glyphs[TEXT_AREA];
22014 struct glyph *p2, *end;
22016 /* Copy the arrow glyphs. */
22017 while (glyph < arrow_end)
22018 *p++ = *glyph++;
22020 /* Throw away padding glyphs. */
22021 p2 = p;
22022 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22023 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22024 ++p2;
22025 if (p2 > p)
22027 while (p2 < end)
22028 *p++ = *p2++;
22029 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22032 else
22034 eassert (INTEGERP (overlay_arrow_string));
22035 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22037 overlay_arrow_seen = true;
22040 /* Highlight trailing whitespace. */
22041 if (!NILP (Vshow_trailing_whitespace))
22042 highlight_trailing_whitespace (it->f, it->glyph_row);
22044 /* Compute pixel dimensions of this line. */
22045 compute_line_metrics (it);
22047 /* Implementation note: No changes in the glyphs of ROW or in their
22048 faces can be done past this point, because compute_line_metrics
22049 computes ROW's hash value and stores it within the glyph_row
22050 structure. */
22052 /* Record whether this row ends inside an ellipsis. */
22053 row->ends_in_ellipsis_p
22054 = (it->method == GET_FROM_DISPLAY_VECTOR
22055 && it->ellipsis_p);
22057 /* Save fringe bitmaps in this row. */
22058 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22059 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22060 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22061 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22063 it->left_user_fringe_bitmap = 0;
22064 it->left_user_fringe_face_id = 0;
22065 it->right_user_fringe_bitmap = 0;
22066 it->right_user_fringe_face_id = 0;
22068 /* Maybe set the cursor. */
22069 cvpos = it->w->cursor.vpos;
22070 if ((cvpos < 0
22071 /* In bidi-reordered rows, keep checking for proper cursor
22072 position even if one has been found already, because buffer
22073 positions in such rows change non-linearly with ROW->VPOS,
22074 when a line is continued. One exception: when we are at ZV,
22075 display cursor on the first suitable glyph row, since all
22076 the empty rows after that also have their position set to ZV. */
22077 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22078 lines' rows is implemented for bidi-reordered rows. */
22079 || (it->bidi_p
22080 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22081 && PT >= MATRIX_ROW_START_CHARPOS (row)
22082 && PT <= MATRIX_ROW_END_CHARPOS (row)
22083 && cursor_row_p (row))
22084 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22086 /* Prepare for the next line. This line starts horizontally at (X
22087 HPOS) = (0 0). Vertical positions are incremented. As a
22088 convenience for the caller, IT->glyph_row is set to the next
22089 row to be used. */
22090 it->current_x = it->hpos = 0;
22091 it->current_y += row->height;
22092 /* Restore the first and last visible X if we adjusted them for
22093 current-line hscrolling. */
22094 if (hscroll_this_line)
22096 it->first_visible_x = first_visible_x;
22097 it->last_visible_x = last_visible_x;
22099 SET_TEXT_POS (it->eol_pos, 0, 0);
22100 ++it->vpos;
22101 ++it->glyph_row;
22102 /* The next row should by default use the same value of the
22103 reversed_p flag as this one. set_iterator_to_next decides when
22104 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22105 the flag accordingly. */
22106 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22107 it->glyph_row->reversed_p = row->reversed_p;
22108 it->start = row->end;
22109 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22111 #undef RECORD_MAX_MIN_POS
22114 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22115 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22116 doc: /* Return paragraph direction at point in BUFFER.
22117 Value is either `left-to-right' or `right-to-left'.
22118 If BUFFER is omitted or nil, it defaults to the current buffer.
22120 Paragraph direction determines how the text in the paragraph is displayed.
22121 In left-to-right paragraphs, text begins at the left margin of the window
22122 and the reading direction is generally left to right. In right-to-left
22123 paragraphs, text begins at the right margin and is read from right to left.
22125 See also `bidi-paragraph-direction'. */)
22126 (Lisp_Object buffer)
22128 struct buffer *buf = current_buffer;
22129 struct buffer *old = buf;
22131 if (! NILP (buffer))
22133 CHECK_BUFFER (buffer);
22134 buf = XBUFFER (buffer);
22137 if (NILP (BVAR (buf, bidi_display_reordering))
22138 || NILP (BVAR (buf, enable_multibyte_characters))
22139 /* When we are loading loadup.el, the character property tables
22140 needed for bidi iteration are not yet available. */
22141 || redisplay__inhibit_bidi)
22142 return Qleft_to_right;
22143 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22144 return BVAR (buf, bidi_paragraph_direction);
22145 else
22147 /* Determine the direction from buffer text. We could try to
22148 use current_matrix if it is up to date, but this seems fast
22149 enough as it is. */
22150 struct bidi_it itb;
22151 ptrdiff_t pos = BUF_PT (buf);
22152 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22153 int c;
22154 void *itb_data = bidi_shelve_cache ();
22156 set_buffer_temp (buf);
22157 /* bidi_paragraph_init finds the base direction of the paragraph
22158 by searching forward from paragraph start. We need the base
22159 direction of the current or _previous_ paragraph, so we need
22160 to make sure we are within that paragraph. To that end, find
22161 the previous non-empty line. */
22162 if (pos >= ZV && pos > BEGV)
22163 DEC_BOTH (pos, bytepos);
22164 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22165 if (fast_looking_at (trailing_white_space,
22166 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22168 while ((c = FETCH_BYTE (bytepos)) == '\n'
22169 || c == ' ' || c == '\t' || c == '\f')
22171 if (bytepos <= BEGV_BYTE)
22172 break;
22173 bytepos--;
22174 pos--;
22176 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22177 bytepos--;
22179 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22180 itb.paragraph_dir = NEUTRAL_DIR;
22181 itb.string.s = NULL;
22182 itb.string.lstring = Qnil;
22183 itb.string.bufpos = 0;
22184 itb.string.from_disp_str = false;
22185 itb.string.unibyte = false;
22186 /* We have no window to use here for ignoring window-specific
22187 overlays. Using NULL for window pointer will cause
22188 compute_display_string_pos to use the current buffer. */
22189 itb.w = NULL;
22190 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22191 bidi_unshelve_cache (itb_data, false);
22192 set_buffer_temp (old);
22193 switch (itb.paragraph_dir)
22195 case L2R:
22196 return Qleft_to_right;
22197 break;
22198 case R2L:
22199 return Qright_to_left;
22200 break;
22201 default:
22202 emacs_abort ();
22207 DEFUN ("bidi-find-overridden-directionality",
22208 Fbidi_find_overridden_directionality,
22209 Sbidi_find_overridden_directionality, 2, 3, 0,
22210 doc: /* Return position between FROM and TO where directionality was overridden.
22212 This function returns the first character position in the specified
22213 region of OBJECT where there is a character whose `bidi-class' property
22214 is `L', but which was forced to display as `R' by a directional
22215 override, and likewise with characters whose `bidi-class' is `R'
22216 or `AL' that were forced to display as `L'.
22218 If no such character is found, the function returns nil.
22220 OBJECT is a Lisp string or buffer to search for overridden
22221 directionality, and defaults to the current buffer if nil or omitted.
22222 OBJECT can also be a window, in which case the function will search
22223 the buffer displayed in that window. Passing the window instead of
22224 a buffer is preferable when the buffer is displayed in some window,
22225 because this function will then be able to correctly account for
22226 window-specific overlays, which can affect the results.
22228 Strong directional characters `L', `R', and `AL' can have their
22229 intrinsic directionality overridden by directional override
22230 control characters RLO (u+202e) and LRO (u+202d). See the
22231 function `get-char-code-property' for a way to inquire about
22232 the `bidi-class' property of a character. */)
22233 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22235 struct buffer *buf = current_buffer;
22236 struct buffer *old = buf;
22237 struct window *w = NULL;
22238 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22239 struct bidi_it itb;
22240 ptrdiff_t from_pos, to_pos, from_bpos;
22241 void *itb_data;
22243 if (!NILP (object))
22245 if (BUFFERP (object))
22246 buf = XBUFFER (object);
22247 else if (WINDOWP (object))
22249 w = decode_live_window (object);
22250 buf = XBUFFER (w->contents);
22251 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22253 else
22254 CHECK_STRING (object);
22257 if (STRINGP (object))
22259 /* Characters in unibyte strings are always treated by bidi.c as
22260 strong LTR. */
22261 if (!STRING_MULTIBYTE (object)
22262 /* When we are loading loadup.el, the character property
22263 tables needed for bidi iteration are not yet
22264 available. */
22265 || redisplay__inhibit_bidi)
22266 return Qnil;
22268 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22269 if (from_pos >= SCHARS (object))
22270 return Qnil;
22272 /* Set up the bidi iterator. */
22273 itb_data = bidi_shelve_cache ();
22274 itb.paragraph_dir = NEUTRAL_DIR;
22275 itb.string.lstring = object;
22276 itb.string.s = NULL;
22277 itb.string.schars = SCHARS (object);
22278 itb.string.bufpos = 0;
22279 itb.string.from_disp_str = false;
22280 itb.string.unibyte = false;
22281 itb.w = w;
22282 bidi_init_it (0, 0, frame_window_p, &itb);
22284 else
22286 /* Nothing this fancy can happen in unibyte buffers, or in a
22287 buffer that disabled reordering, or if FROM is at EOB. */
22288 if (NILP (BVAR (buf, bidi_display_reordering))
22289 || NILP (BVAR (buf, enable_multibyte_characters))
22290 /* When we are loading loadup.el, the character property
22291 tables needed for bidi iteration are not yet
22292 available. */
22293 || redisplay__inhibit_bidi)
22294 return Qnil;
22296 set_buffer_temp (buf);
22297 validate_region (&from, &to);
22298 from_pos = XINT (from);
22299 to_pos = XINT (to);
22300 if (from_pos >= ZV)
22301 return Qnil;
22303 /* Set up the bidi iterator. */
22304 itb_data = bidi_shelve_cache ();
22305 from_bpos = CHAR_TO_BYTE (from_pos);
22306 if (from_pos == BEGV)
22308 itb.charpos = BEGV;
22309 itb.bytepos = BEGV_BYTE;
22311 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22313 itb.charpos = from_pos;
22314 itb.bytepos = from_bpos;
22316 else
22317 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22318 -1, &itb.bytepos);
22319 itb.paragraph_dir = NEUTRAL_DIR;
22320 itb.string.s = NULL;
22321 itb.string.lstring = Qnil;
22322 itb.string.bufpos = 0;
22323 itb.string.from_disp_str = false;
22324 itb.string.unibyte = false;
22325 itb.w = w;
22326 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22329 ptrdiff_t found;
22330 do {
22331 /* For the purposes of this function, the actual base direction of
22332 the paragraph doesn't matter, so just set it to L2R. */
22333 bidi_paragraph_init (L2R, &itb, false);
22334 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22336 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22338 bidi_unshelve_cache (itb_data, false);
22339 set_buffer_temp (old);
22341 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22344 DEFUN ("move-point-visually", Fmove_point_visually,
22345 Smove_point_visually, 1, 1, 0,
22346 doc: /* Move point in the visual order in the specified DIRECTION.
22347 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22348 left.
22350 Value is the new character position of point. */)
22351 (Lisp_Object direction)
22353 struct window *w = XWINDOW (selected_window);
22354 struct buffer *b = XBUFFER (w->contents);
22355 struct glyph_row *row;
22356 int dir;
22357 Lisp_Object paragraph_dir;
22359 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22360 (!(ROW)->continued_p \
22361 && NILP ((GLYPH)->object) \
22362 && (GLYPH)->type == CHAR_GLYPH \
22363 && (GLYPH)->u.ch == ' ' \
22364 && (GLYPH)->charpos >= 0 \
22365 && !(GLYPH)->avoid_cursor_p)
22367 CHECK_NUMBER (direction);
22368 dir = XINT (direction);
22369 if (dir > 0)
22370 dir = 1;
22371 else
22372 dir = -1;
22374 /* If current matrix is up-to-date, we can use the information
22375 recorded in the glyphs, at least as long as the goal is on the
22376 screen. */
22377 if (w->window_end_valid
22378 && !windows_or_buffers_changed
22379 && b
22380 && !b->clip_changed
22381 && !b->prevent_redisplay_optimizations_p
22382 && !window_outdated (w)
22383 /* We rely below on the cursor coordinates to be up to date, but
22384 we cannot trust them if some command moved point since the
22385 last complete redisplay. */
22386 && w->last_point == BUF_PT (b)
22387 && w->cursor.vpos >= 0
22388 && w->cursor.vpos < w->current_matrix->nrows
22389 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22391 struct glyph *g = row->glyphs[TEXT_AREA];
22392 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22393 struct glyph *gpt = g + w->cursor.hpos;
22395 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22397 if (BUFFERP (g->object) && g->charpos != PT)
22399 SET_PT (g->charpos);
22400 w->cursor.vpos = -1;
22401 return make_number (PT);
22403 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22405 ptrdiff_t new_pos;
22407 if (BUFFERP (gpt->object))
22409 new_pos = PT;
22410 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22411 new_pos += (row->reversed_p ? -dir : dir);
22412 else
22413 new_pos -= (row->reversed_p ? -dir : dir);
22415 else if (BUFFERP (g->object))
22416 new_pos = g->charpos;
22417 else
22418 break;
22419 SET_PT (new_pos);
22420 w->cursor.vpos = -1;
22421 return make_number (PT);
22423 else if (ROW_GLYPH_NEWLINE_P (row, g))
22425 /* Glyphs inserted at the end of a non-empty line for
22426 positioning the cursor have zero charpos, so we must
22427 deduce the value of point by other means. */
22428 if (g->charpos > 0)
22429 SET_PT (g->charpos);
22430 else if (row->ends_at_zv_p && PT != ZV)
22431 SET_PT (ZV);
22432 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22433 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22434 else
22435 break;
22436 w->cursor.vpos = -1;
22437 return make_number (PT);
22440 if (g == e || NILP (g->object))
22442 if (row->truncated_on_left_p || row->truncated_on_right_p)
22443 goto simulate_display;
22444 if (!row->reversed_p)
22445 row += dir;
22446 else
22447 row -= dir;
22448 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22449 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22450 goto simulate_display;
22452 if (dir > 0)
22454 if (row->reversed_p && !row->continued_p)
22456 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22457 w->cursor.vpos = -1;
22458 return make_number (PT);
22460 g = row->glyphs[TEXT_AREA];
22461 e = g + row->used[TEXT_AREA];
22462 for ( ; g < e; g++)
22464 if (BUFFERP (g->object)
22465 /* Empty lines have only one glyph, which stands
22466 for the newline, and whose charpos is the
22467 buffer position of the newline. */
22468 || ROW_GLYPH_NEWLINE_P (row, g)
22469 /* When the buffer ends in a newline, the line at
22470 EOB also has one glyph, but its charpos is -1. */
22471 || (row->ends_at_zv_p
22472 && !row->reversed_p
22473 && NILP (g->object)
22474 && g->type == CHAR_GLYPH
22475 && g->u.ch == ' '))
22477 if (g->charpos > 0)
22478 SET_PT (g->charpos);
22479 else if (!row->reversed_p
22480 && row->ends_at_zv_p
22481 && PT != ZV)
22482 SET_PT (ZV);
22483 else
22484 continue;
22485 w->cursor.vpos = -1;
22486 return make_number (PT);
22490 else
22492 if (!row->reversed_p && !row->continued_p)
22494 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22495 w->cursor.vpos = -1;
22496 return make_number (PT);
22498 e = row->glyphs[TEXT_AREA];
22499 g = e + row->used[TEXT_AREA] - 1;
22500 for ( ; g >= e; g--)
22502 if (BUFFERP (g->object)
22503 || (ROW_GLYPH_NEWLINE_P (row, g)
22504 && g->charpos > 0)
22505 /* Empty R2L lines on GUI frames have the buffer
22506 position of the newline stored in the stretch
22507 glyph. */
22508 || g->type == STRETCH_GLYPH
22509 || (row->ends_at_zv_p
22510 && row->reversed_p
22511 && NILP (g->object)
22512 && g->type == CHAR_GLYPH
22513 && g->u.ch == ' '))
22515 if (g->charpos > 0)
22516 SET_PT (g->charpos);
22517 else if (row->reversed_p
22518 && row->ends_at_zv_p
22519 && PT != ZV)
22520 SET_PT (ZV);
22521 else
22522 continue;
22523 w->cursor.vpos = -1;
22524 return make_number (PT);
22531 simulate_display:
22533 /* If we wind up here, we failed to move by using the glyphs, so we
22534 need to simulate display instead. */
22536 if (b)
22537 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22538 else
22539 paragraph_dir = Qleft_to_right;
22540 if (EQ (paragraph_dir, Qright_to_left))
22541 dir = -dir;
22542 if (PT <= BEGV && dir < 0)
22543 xsignal0 (Qbeginning_of_buffer);
22544 else if (PT >= ZV && dir > 0)
22545 xsignal0 (Qend_of_buffer);
22546 else
22548 struct text_pos pt;
22549 struct it it;
22550 int pt_x, target_x, pixel_width, pt_vpos;
22551 bool at_eol_p;
22552 bool overshoot_expected = false;
22553 bool target_is_eol_p = false;
22555 /* Setup the arena. */
22556 SET_TEXT_POS (pt, PT, PT_BYTE);
22557 start_display (&it, w, pt);
22558 /* When lines are truncated, we could be called with point
22559 outside of the windows edges, in which case move_it_*
22560 functions either prematurely stop at window's edge or jump to
22561 the next screen line, whereas we rely below on our ability to
22562 reach point, in order to start from its X coordinate. So we
22563 need to disregard the window's horizontal extent in that case. */
22564 if (it.line_wrap == TRUNCATE)
22565 it.last_visible_x = DISP_INFINITY;
22567 if (it.cmp_it.id < 0
22568 && it.method == GET_FROM_STRING
22569 && it.area == TEXT_AREA
22570 && it.string_from_display_prop_p
22571 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22572 overshoot_expected = true;
22574 /* Find the X coordinate of point. We start from the beginning
22575 of this or previous line to make sure we are before point in
22576 the logical order (since the move_it_* functions can only
22577 move forward). */
22578 reseat:
22579 reseat_at_previous_visible_line_start (&it);
22580 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22581 if (IT_CHARPOS (it) != PT)
22583 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22584 -1, -1, -1, MOVE_TO_POS);
22585 /* If we missed point because the character there is
22586 displayed out of a display vector that has more than one
22587 glyph, retry expecting overshoot. */
22588 if (it.method == GET_FROM_DISPLAY_VECTOR
22589 && it.current.dpvec_index > 0
22590 && !overshoot_expected)
22592 overshoot_expected = true;
22593 goto reseat;
22595 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22596 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22598 pt_x = it.current_x;
22599 pt_vpos = it.vpos;
22600 if (dir > 0 || overshoot_expected)
22602 struct glyph_row *row = it.glyph_row;
22604 /* When point is at beginning of line, we don't have
22605 information about the glyph there loaded into struct
22606 it. Calling get_next_display_element fixes that. */
22607 if (pt_x == 0)
22608 get_next_display_element (&it);
22609 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22610 it.glyph_row = NULL;
22611 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22612 it.glyph_row = row;
22613 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22614 it, lest it will become out of sync with it's buffer
22615 position. */
22616 it.current_x = pt_x;
22618 else
22619 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22620 pixel_width = it.pixel_width;
22621 if (overshoot_expected && at_eol_p)
22622 pixel_width = 0;
22623 else if (pixel_width <= 0)
22624 pixel_width = 1;
22626 /* If there's a display string (or something similar) at point,
22627 we are actually at the glyph to the left of point, so we need
22628 to correct the X coordinate. */
22629 if (overshoot_expected)
22631 if (it.bidi_p)
22632 pt_x += pixel_width * it.bidi_it.scan_dir;
22633 else
22634 pt_x += pixel_width;
22637 /* Compute target X coordinate, either to the left or to the
22638 right of point. On TTY frames, all characters have the same
22639 pixel width of 1, so we can use that. On GUI frames we don't
22640 have an easy way of getting at the pixel width of the
22641 character to the left of point, so we use a different method
22642 of getting to that place. */
22643 if (dir > 0)
22644 target_x = pt_x + pixel_width;
22645 else
22646 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22648 /* Target X coordinate could be one line above or below the line
22649 of point, in which case we need to adjust the target X
22650 coordinate. Also, if moving to the left, we need to begin at
22651 the left edge of the point's screen line. */
22652 if (dir < 0)
22654 if (pt_x > 0)
22656 start_display (&it, w, pt);
22657 if (it.line_wrap == TRUNCATE)
22658 it.last_visible_x = DISP_INFINITY;
22659 reseat_at_previous_visible_line_start (&it);
22660 it.current_x = it.current_y = it.hpos = 0;
22661 if (pt_vpos != 0)
22662 move_it_by_lines (&it, pt_vpos);
22664 else
22666 move_it_by_lines (&it, -1);
22667 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22668 target_is_eol_p = true;
22669 /* Under word-wrap, we don't know the x coordinate of
22670 the last character displayed on the previous line,
22671 which immediately precedes the wrap point. To find
22672 out its x coordinate, we try moving to the right
22673 margin of the window, which will stop at the wrap
22674 point, and then reset target_x to point at the
22675 character that precedes the wrap point. This is not
22676 needed on GUI frames, because (see below) there we
22677 move from the left margin one grapheme cluster at a
22678 time, and stop when we hit the wrap point. */
22679 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22681 void *it_data = NULL;
22682 struct it it2;
22684 SAVE_IT (it2, it, it_data);
22685 move_it_in_display_line_to (&it, ZV, target_x,
22686 MOVE_TO_POS | MOVE_TO_X);
22687 /* If we arrived at target_x, that _is_ the last
22688 character on the previous line. */
22689 if (it.current_x != target_x)
22690 target_x = it.current_x - 1;
22691 RESTORE_IT (&it, &it2, it_data);
22695 else
22697 if (at_eol_p
22698 || (target_x >= it.last_visible_x
22699 && it.line_wrap != TRUNCATE))
22701 if (pt_x > 0)
22702 move_it_by_lines (&it, 0);
22703 move_it_by_lines (&it, 1);
22704 target_x = 0;
22708 /* Move to the target X coordinate. */
22709 /* On GUI frames, as we don't know the X coordinate of the
22710 character to the left of point, moving point to the left
22711 requires walking, one grapheme cluster at a time, until we
22712 find ourself at a place immediately to the left of the
22713 character at point. */
22714 if (FRAME_WINDOW_P (it.f) && dir < 0)
22716 struct text_pos new_pos;
22717 enum move_it_result rc = MOVE_X_REACHED;
22719 if (it.current_x == 0)
22720 get_next_display_element (&it);
22721 if (it.what == IT_COMPOSITION)
22723 new_pos.charpos = it.cmp_it.charpos;
22724 new_pos.bytepos = -1;
22726 else
22727 new_pos = it.current.pos;
22729 while (it.current_x + it.pixel_width <= target_x
22730 && (rc == MOVE_X_REACHED
22731 /* Under word-wrap, move_it_in_display_line_to
22732 stops at correct coordinates, but sometimes
22733 returns MOVE_POS_MATCH_OR_ZV. */
22734 || (it.line_wrap == WORD_WRAP
22735 && rc == MOVE_POS_MATCH_OR_ZV)))
22737 int new_x = it.current_x + it.pixel_width;
22739 /* For composed characters, we want the position of the
22740 first character in the grapheme cluster (usually, the
22741 composition's base character), whereas it.current
22742 might give us the position of the _last_ one, e.g. if
22743 the composition is rendered in reverse due to bidi
22744 reordering. */
22745 if (it.what == IT_COMPOSITION)
22747 new_pos.charpos = it.cmp_it.charpos;
22748 new_pos.bytepos = -1;
22750 else
22751 new_pos = it.current.pos;
22752 if (new_x == it.current_x)
22753 new_x++;
22754 rc = move_it_in_display_line_to (&it, ZV, new_x,
22755 MOVE_TO_POS | MOVE_TO_X);
22756 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22757 break;
22759 /* The previous position we saw in the loop is the one we
22760 want. */
22761 if (new_pos.bytepos == -1)
22762 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22763 it.current.pos = new_pos;
22765 else if (it.current_x != target_x)
22766 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22768 /* If we ended up in a display string that covers point, move to
22769 buffer position to the right in the visual order. */
22770 if (dir > 0)
22772 while (IT_CHARPOS (it) == PT)
22774 set_iterator_to_next (&it, false);
22775 if (!get_next_display_element (&it))
22776 break;
22780 /* Move point to that position. */
22781 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22784 return make_number (PT);
22786 #undef ROW_GLYPH_NEWLINE_P
22789 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22790 Sbidi_resolved_levels, 0, 1, 0,
22791 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22793 The resolved levels are produced by the Emacs bidi reordering engine
22794 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22795 read the Unicode Standard Annex 9 (UAX#9) for background information
22796 about these levels.
22798 VPOS is the zero-based number of the current window's screen line
22799 for which to produce the resolved levels. If VPOS is nil or omitted,
22800 it defaults to the screen line of point. If the window displays a
22801 header line, VPOS of zero will report on the header line, and first
22802 line of text in the window will have VPOS of 1.
22804 Value is an array of resolved levels, indexed by glyph number.
22805 Glyphs are numbered from zero starting from the beginning of the
22806 screen line, i.e. the left edge of the window for left-to-right lines
22807 and from the right edge for right-to-left lines. The resolved levels
22808 are produced only for the window's text area; text in display margins
22809 is not included.
22811 If the selected window's display is not up-to-date, or if the specified
22812 screen line does not display text, this function returns nil. It is
22813 highly recommended to bind this function to some simple key, like F8,
22814 in order to avoid these problems.
22816 This function exists mainly for testing the correctness of the
22817 Emacs UBA implementation, in particular with the test suite. */)
22818 (Lisp_Object vpos)
22820 struct window *w = XWINDOW (selected_window);
22821 struct buffer *b = XBUFFER (w->contents);
22822 int nrow;
22823 struct glyph_row *row;
22825 if (NILP (vpos))
22827 int d1, d2, d3, d4, d5;
22829 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22831 else
22833 CHECK_NUMBER_COERCE_MARKER (vpos);
22834 nrow = XINT (vpos);
22837 /* We require up-to-date glyph matrix for this window. */
22838 if (w->window_end_valid
22839 && !windows_or_buffers_changed
22840 && b
22841 && !b->clip_changed
22842 && !b->prevent_redisplay_optimizations_p
22843 && !window_outdated (w)
22844 && nrow >= 0
22845 && nrow < w->current_matrix->nrows
22846 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22847 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22849 struct glyph *g, *e, *g1;
22850 int nglyphs, i;
22851 Lisp_Object levels;
22853 if (!row->reversed_p) /* Left-to-right glyph row. */
22855 g = g1 = row->glyphs[TEXT_AREA];
22856 e = g + row->used[TEXT_AREA];
22858 /* Skip over glyphs at the start of the row that was
22859 generated by redisplay for its own needs. */
22860 while (g < e
22861 && NILP (g->object)
22862 && g->charpos < 0)
22863 g++;
22864 g1 = g;
22866 /* Count the "interesting" glyphs in this row. */
22867 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22868 nglyphs++;
22870 /* Create and fill the array. */
22871 levels = make_uninit_vector (nglyphs);
22872 for (i = 0; g1 < g; i++, g1++)
22873 ASET (levels, i, make_number (g1->resolved_level));
22875 else /* Right-to-left glyph row. */
22877 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22878 e = row->glyphs[TEXT_AREA] - 1;
22879 while (g > e
22880 && NILP (g->object)
22881 && g->charpos < 0)
22882 g--;
22883 g1 = g;
22884 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22885 nglyphs++;
22886 levels = make_uninit_vector (nglyphs);
22887 for (i = 0; g1 > g; i++, g1--)
22888 ASET (levels, i, make_number (g1->resolved_level));
22890 return levels;
22892 else
22893 return Qnil;
22898 /***********************************************************************
22899 Menu Bar
22900 ***********************************************************************/
22902 /* Redisplay the menu bar in the frame for window W.
22904 The menu bar of X frames that don't have X toolkit support is
22905 displayed in a special window W->frame->menu_bar_window.
22907 The menu bar of terminal frames is treated specially as far as
22908 glyph matrices are concerned. Menu bar lines are not part of
22909 windows, so the update is done directly on the frame matrix rows
22910 for the menu bar. */
22912 static void
22913 display_menu_bar (struct window *w)
22915 struct frame *f = XFRAME (WINDOW_FRAME (w));
22916 struct it it;
22917 Lisp_Object items;
22918 int i;
22920 /* Don't do all this for graphical frames. */
22921 #ifdef HAVE_NTGUI
22922 if (FRAME_W32_P (f))
22923 return;
22924 #endif
22925 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22926 if (FRAME_X_P (f))
22927 return;
22928 #endif
22930 #ifdef HAVE_NS
22931 if (FRAME_NS_P (f))
22932 return;
22933 #endif /* HAVE_NS */
22935 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22936 eassert (!FRAME_WINDOW_P (f));
22937 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22938 it.first_visible_x = 0;
22939 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22940 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22941 if (FRAME_WINDOW_P (f))
22943 /* Menu bar lines are displayed in the desired matrix of the
22944 dummy window menu_bar_window. */
22945 struct window *menu_w;
22946 menu_w = XWINDOW (f->menu_bar_window);
22947 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22948 MENU_FACE_ID);
22949 it.first_visible_x = 0;
22950 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22952 else
22953 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22955 /* This is a TTY frame, i.e. character hpos/vpos are used as
22956 pixel x/y. */
22957 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22958 MENU_FACE_ID);
22959 it.first_visible_x = 0;
22960 it.last_visible_x = FRAME_COLS (f);
22963 /* FIXME: This should be controlled by a user option. See the
22964 comments in redisplay_tool_bar and display_mode_line about
22965 this. */
22966 it.paragraph_embedding = L2R;
22968 /* Clear all rows of the menu bar. */
22969 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22971 struct glyph_row *row = it.glyph_row + i;
22972 clear_glyph_row (row);
22973 row->enabled_p = true;
22974 row->full_width_p = true;
22975 row->reversed_p = false;
22978 /* Display all items of the menu bar. */
22979 items = FRAME_MENU_BAR_ITEMS (it.f);
22980 for (i = 0; i < ASIZE (items); i += 4)
22982 Lisp_Object string;
22984 /* Stop at nil string. */
22985 string = AREF (items, i + 1);
22986 if (NILP (string))
22987 break;
22989 /* Remember where item was displayed. */
22990 ASET (items, i + 3, make_number (it.hpos));
22992 /* Display the item, pad with one space. */
22993 if (it.current_x < it.last_visible_x)
22994 display_string (NULL, string, Qnil, 0, 0, &it,
22995 SCHARS (string) + 1, 0, 0, -1);
22998 /* Fill out the line with spaces. */
22999 if (it.current_x < it.last_visible_x)
23000 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23002 /* Compute the total height of the lines. */
23003 compute_line_metrics (&it);
23006 /* Deep copy of a glyph row, including the glyphs. */
23007 static void
23008 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23010 struct glyph *pointers[1 + LAST_AREA];
23011 int to_used = to->used[TEXT_AREA];
23013 /* Save glyph pointers of TO. */
23014 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23016 /* Do a structure assignment. */
23017 *to = *from;
23019 /* Restore original glyph pointers of TO. */
23020 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23022 /* Copy the glyphs. */
23023 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23024 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23026 /* If we filled only part of the TO row, fill the rest with
23027 space_glyph (which will display as empty space). */
23028 if (to_used > from->used[TEXT_AREA])
23029 fill_up_frame_row_with_spaces (to, to_used);
23032 /* Display one menu item on a TTY, by overwriting the glyphs in the
23033 frame F's desired glyph matrix with glyphs produced from the menu
23034 item text. Called from term.c to display TTY drop-down menus one
23035 item at a time.
23037 ITEM_TEXT is the menu item text as a C string.
23039 FACE_ID is the face ID to be used for this menu item. FACE_ID
23040 could specify one of 3 faces: a face for an enabled item, a face
23041 for a disabled item, or a face for a selected item.
23043 X and Y are coordinates of the first glyph in the frame's desired
23044 matrix to be overwritten by the menu item. Since this is a TTY, Y
23045 is the zero-based number of the glyph row and X is the zero-based
23046 glyph number in the row, starting from left, where to start
23047 displaying the item.
23049 SUBMENU means this menu item drops down a submenu, which
23050 should be indicated by displaying a proper visual cue after the
23051 item text. */
23053 void
23054 display_tty_menu_item (const char *item_text, int width, int face_id,
23055 int x, int y, bool submenu)
23057 struct it it;
23058 struct frame *f = SELECTED_FRAME ();
23059 struct window *w = XWINDOW (f->selected_window);
23060 struct glyph_row *row;
23061 size_t item_len = strlen (item_text);
23063 eassert (FRAME_TERMCAP_P (f));
23065 /* Don't write beyond the matrix's last row. This can happen for
23066 TTY screens that are not high enough to show the entire menu.
23067 (This is actually a bit of defensive programming, as
23068 tty_menu_display already limits the number of menu items to one
23069 less than the number of screen lines.) */
23070 if (y >= f->desired_matrix->nrows)
23071 return;
23073 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23074 it.first_visible_x = 0;
23075 it.last_visible_x = FRAME_COLS (f) - 1;
23076 row = it.glyph_row;
23077 /* Start with the row contents from the current matrix. */
23078 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23079 bool saved_width = row->full_width_p;
23080 row->full_width_p = true;
23081 bool saved_reversed = row->reversed_p;
23082 row->reversed_p = false;
23083 row->enabled_p = true;
23085 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23086 desired face. */
23087 eassert (x < f->desired_matrix->matrix_w);
23088 it.current_x = it.hpos = x;
23089 it.current_y = it.vpos = y;
23090 int saved_used = row->used[TEXT_AREA];
23091 bool saved_truncated = row->truncated_on_right_p;
23092 row->used[TEXT_AREA] = x;
23093 it.face_id = face_id;
23094 it.line_wrap = TRUNCATE;
23096 /* FIXME: This should be controlled by a user option. See the
23097 comments in redisplay_tool_bar and display_mode_line about this.
23098 Also, if paragraph_embedding could ever be R2L, changes will be
23099 needed to avoid shifting to the right the row characters in
23100 term.c:append_glyph. */
23101 it.paragraph_embedding = L2R;
23103 /* Pad with a space on the left. */
23104 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23105 width--;
23106 /* Display the menu item, pad with spaces to WIDTH. */
23107 if (submenu)
23109 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23110 item_len, 0, FRAME_COLS (f) - 1, -1);
23111 width -= item_len;
23112 /* Indicate with " >" that there's a submenu. */
23113 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23114 FRAME_COLS (f) - 1, -1);
23116 else
23117 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23118 width, 0, FRAME_COLS (f) - 1, -1);
23120 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23121 row->truncated_on_right_p = saved_truncated;
23122 row->hash = row_hash (row);
23123 row->full_width_p = saved_width;
23124 row->reversed_p = saved_reversed;
23127 /***********************************************************************
23128 Mode Line
23129 ***********************************************************************/
23131 /* Redisplay mode lines in the window tree whose root is WINDOW.
23132 If FORCE, redisplay mode lines unconditionally.
23133 Otherwise, redisplay only mode lines that are garbaged. Value is
23134 the number of windows whose mode lines were redisplayed. */
23136 static int
23137 redisplay_mode_lines (Lisp_Object window, bool force)
23139 int nwindows = 0;
23141 while (!NILP (window))
23143 struct window *w = XWINDOW (window);
23145 if (WINDOWP (w->contents))
23146 nwindows += redisplay_mode_lines (w->contents, force);
23147 else if (force
23148 || FRAME_GARBAGED_P (XFRAME (w->frame))
23149 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23151 struct text_pos lpoint;
23152 struct buffer *old = current_buffer;
23154 /* Set the window's buffer for the mode line display. */
23155 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23156 set_buffer_internal_1 (XBUFFER (w->contents));
23158 /* Point refers normally to the selected window. For any
23159 other window, set up appropriate value. */
23160 if (!EQ (window, selected_window))
23162 struct text_pos pt;
23164 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23165 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23168 /* Display mode lines. */
23169 clear_glyph_matrix (w->desired_matrix);
23170 if (display_mode_lines (w))
23171 ++nwindows;
23173 /* Restore old settings. */
23174 set_buffer_internal_1 (old);
23175 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23178 window = w->next;
23181 return nwindows;
23185 /* Display the mode and/or header line of window W. Value is the
23186 sum number of mode lines and header lines displayed. */
23188 static int
23189 display_mode_lines (struct window *w)
23191 Lisp_Object old_selected_window = selected_window;
23192 Lisp_Object old_selected_frame = selected_frame;
23193 Lisp_Object new_frame = w->frame;
23194 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23195 int n = 0;
23197 selected_frame = new_frame;
23198 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23199 or window's point, then we'd need select_window_1 here as well. */
23200 XSETWINDOW (selected_window, w);
23201 XFRAME (new_frame)->selected_window = selected_window;
23203 /* These will be set while the mode line specs are processed. */
23204 line_number_displayed = false;
23205 w->column_number_displayed = -1;
23207 if (window_wants_mode_line (w))
23209 Lisp_Object window_mode_line_format
23210 = window_parameter (w, Qmode_line_format);
23212 struct window *sel_w = XWINDOW (old_selected_window);
23214 /* Select mode line face based on the real selected window. */
23215 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23216 NILP (window_mode_line_format)
23217 ? BVAR (current_buffer, mode_line_format)
23218 : window_mode_line_format);
23219 ++n;
23222 if (window_wants_header_line (w))
23224 Lisp_Object window_header_line_format
23225 = window_parameter (w, Qheader_line_format);
23227 display_mode_line (w, HEADER_LINE_FACE_ID,
23228 NILP (window_header_line_format)
23229 ? BVAR (current_buffer, header_line_format)
23230 : window_header_line_format);
23231 ++n;
23234 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23235 selected_frame = old_selected_frame;
23236 selected_window = old_selected_window;
23237 if (n > 0)
23238 w->must_be_updated_p = true;
23239 return n;
23243 /* Display mode or header line of window W. FACE_ID specifies which
23244 line to display; it is either MODE_LINE_FACE_ID or
23245 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23246 display. Value is the pixel height of the mode/header line
23247 displayed. */
23249 static int
23250 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23252 struct it it;
23253 struct face *face;
23254 ptrdiff_t count = SPECPDL_INDEX ();
23256 init_iterator (&it, w, -1, -1, NULL, face_id);
23257 /* Don't extend on a previously drawn mode-line.
23258 This may happen if called from pos_visible_p. */
23259 it.glyph_row->enabled_p = false;
23260 prepare_desired_row (w, it.glyph_row, true);
23262 it.glyph_row->mode_line_p = true;
23264 /* FIXME: This should be controlled by a user option. But
23265 supporting such an option is not trivial, since the mode line is
23266 made up of many separate strings. */
23267 it.paragraph_embedding = L2R;
23269 record_unwind_protect (unwind_format_mode_line,
23270 format_mode_line_unwind_data (NULL, NULL,
23271 Qnil, false));
23273 mode_line_target = MODE_LINE_DISPLAY;
23275 /* Temporarily make frame's keyboard the current kboard so that
23276 kboard-local variables in the mode_line_format will get the right
23277 values. */
23278 push_kboard (FRAME_KBOARD (it.f));
23279 record_unwind_save_match_data ();
23280 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23281 pop_kboard ();
23283 unbind_to (count, Qnil);
23285 /* Fill up with spaces. */
23286 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23288 compute_line_metrics (&it);
23289 it.glyph_row->full_width_p = true;
23290 it.glyph_row->continued_p = false;
23291 it.glyph_row->truncated_on_left_p = false;
23292 it.glyph_row->truncated_on_right_p = false;
23294 /* Make a 3D mode-line have a shadow at its right end. */
23295 face = FACE_FROM_ID (it.f, face_id);
23296 extend_face_to_end_of_line (&it);
23297 if (face->box != FACE_NO_BOX)
23299 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23300 + it.glyph_row->used[TEXT_AREA] - 1);
23301 last->right_box_line_p = true;
23304 return it.glyph_row->height;
23307 /* Move element ELT in LIST to the front of LIST.
23308 Return the updated list. */
23310 static Lisp_Object
23311 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23313 register Lisp_Object tail, prev;
23314 register Lisp_Object tem;
23316 tail = list;
23317 prev = Qnil;
23318 while (CONSP (tail))
23320 tem = XCAR (tail);
23322 if (EQ (elt, tem))
23324 /* Splice out the link TAIL. */
23325 if (NILP (prev))
23326 list = XCDR (tail);
23327 else
23328 Fsetcdr (prev, XCDR (tail));
23330 /* Now make it the first. */
23331 Fsetcdr (tail, list);
23332 return tail;
23334 else
23335 prev = tail;
23336 tail = XCDR (tail);
23337 maybe_quit ();
23340 /* Not found--return unchanged LIST. */
23341 return list;
23344 /* Contribute ELT to the mode line for window IT->w. How it
23345 translates into text depends on its data type.
23347 IT describes the display environment in which we display, as usual.
23349 DEPTH is the depth in recursion. It is used to prevent
23350 infinite recursion here.
23352 FIELD_WIDTH is the number of characters the display of ELT should
23353 occupy in the mode line, and PRECISION is the maximum number of
23354 characters to display from ELT's representation. See
23355 display_string for details.
23357 Returns the hpos of the end of the text generated by ELT.
23359 PROPS is a property list to add to any string we encounter.
23361 If RISKY, remove (disregard) any properties in any string
23362 we encounter, and ignore :eval and :propertize.
23364 The global variable `mode_line_target' determines whether the
23365 output is passed to `store_mode_line_noprop',
23366 `store_mode_line_string', or `display_string'. */
23368 static int
23369 display_mode_element (struct it *it, int depth, int field_width, int precision,
23370 Lisp_Object elt, Lisp_Object props, bool risky)
23372 int n = 0, field, prec;
23373 bool literal = false;
23375 tail_recurse:
23376 if (depth > 100)
23377 elt = build_string ("*too-deep*");
23379 depth++;
23381 switch (XTYPE (elt))
23383 case Lisp_String:
23385 /* A string: output it and check for %-constructs within it. */
23386 unsigned char c;
23387 ptrdiff_t offset = 0;
23389 if (SCHARS (elt) > 0
23390 && (!NILP (props) || risky))
23392 Lisp_Object oprops, aelt;
23393 oprops = Ftext_properties_at (make_number (0), elt);
23395 /* If the starting string's properties are not what
23396 we want, translate the string. Also, if the string
23397 is risky, do that anyway. */
23399 if (NILP (Fequal (props, oprops)) || risky)
23401 /* If the starting string has properties,
23402 merge the specified ones onto the existing ones. */
23403 if (! NILP (oprops) && !risky)
23405 Lisp_Object tem;
23407 oprops = Fcopy_sequence (oprops);
23408 tem = props;
23409 while (CONSP (tem))
23411 oprops = Fplist_put (oprops, XCAR (tem),
23412 XCAR (XCDR (tem)));
23413 tem = XCDR (XCDR (tem));
23415 props = oprops;
23418 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23419 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23421 /* AELT is what we want. Move it to the front
23422 without consing. */
23423 elt = XCAR (aelt);
23424 mode_line_proptrans_alist
23425 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23427 else
23429 Lisp_Object tem;
23431 /* If AELT has the wrong props, it is useless.
23432 so get rid of it. */
23433 if (! NILP (aelt))
23434 mode_line_proptrans_alist
23435 = Fdelq (aelt, mode_line_proptrans_alist);
23437 elt = Fcopy_sequence (elt);
23438 Fset_text_properties (make_number (0), Flength (elt),
23439 props, elt);
23440 /* Add this item to mode_line_proptrans_alist. */
23441 mode_line_proptrans_alist
23442 = Fcons (Fcons (elt, props),
23443 mode_line_proptrans_alist);
23444 /* Truncate mode_line_proptrans_alist
23445 to at most 50 elements. */
23446 tem = Fnthcdr (make_number (50),
23447 mode_line_proptrans_alist);
23448 if (! NILP (tem))
23449 XSETCDR (tem, Qnil);
23454 offset = 0;
23456 if (literal)
23458 prec = precision - n;
23459 switch (mode_line_target)
23461 case MODE_LINE_NOPROP:
23462 case MODE_LINE_TITLE:
23463 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23464 break;
23465 case MODE_LINE_STRING:
23466 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23467 break;
23468 case MODE_LINE_DISPLAY:
23469 n += display_string (NULL, elt, Qnil, 0, 0, it,
23470 0, prec, 0, STRING_MULTIBYTE (elt));
23471 break;
23474 break;
23477 /* Handle the non-literal case. */
23479 while ((precision <= 0 || n < precision)
23480 && SREF (elt, offset) != 0
23481 && (mode_line_target != MODE_LINE_DISPLAY
23482 || it->current_x < it->last_visible_x))
23484 ptrdiff_t last_offset = offset;
23486 /* Advance to end of string or next format specifier. */
23487 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23490 if (offset - 1 != last_offset)
23492 ptrdiff_t nchars, nbytes;
23494 /* Output to end of string or up to '%'. Field width
23495 is length of string. Don't output more than
23496 PRECISION allows us. */
23497 offset--;
23499 prec = c_string_width (SDATA (elt) + last_offset,
23500 offset - last_offset, precision - n,
23501 &nchars, &nbytes);
23503 switch (mode_line_target)
23505 case MODE_LINE_NOPROP:
23506 case MODE_LINE_TITLE:
23507 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23508 break;
23509 case MODE_LINE_STRING:
23511 ptrdiff_t bytepos = last_offset;
23512 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23513 ptrdiff_t endpos = (precision <= 0
23514 ? string_byte_to_char (elt, offset)
23515 : charpos + nchars);
23516 Lisp_Object mode_string
23517 = Fsubstring (elt, make_number (charpos),
23518 make_number (endpos));
23519 n += store_mode_line_string (NULL, mode_string, false,
23520 0, 0, Qnil);
23522 break;
23523 case MODE_LINE_DISPLAY:
23525 ptrdiff_t bytepos = last_offset;
23526 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23528 if (precision <= 0)
23529 nchars = string_byte_to_char (elt, offset) - charpos;
23530 n += display_string (NULL, elt, Qnil, 0, charpos,
23531 it, 0, nchars, 0,
23532 STRING_MULTIBYTE (elt));
23534 break;
23537 else /* c == '%' */
23539 ptrdiff_t percent_position = offset;
23541 /* Get the specified minimum width. Zero means
23542 don't pad. */
23543 field = 0;
23544 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23545 field = field * 10 + c - '0';
23547 /* Don't pad beyond the total padding allowed. */
23548 if (field_width - n > 0 && field > field_width - n)
23549 field = field_width - n;
23551 /* Note that either PRECISION <= 0 or N < PRECISION. */
23552 prec = precision - n;
23554 if (c == 'M')
23555 n += display_mode_element (it, depth, field, prec,
23556 Vglobal_mode_string, props,
23557 risky);
23558 else if (c != 0)
23560 bool multibyte;
23561 ptrdiff_t bytepos, charpos;
23562 const char *spec;
23563 Lisp_Object string;
23565 bytepos = percent_position;
23566 charpos = (STRING_MULTIBYTE (elt)
23567 ? string_byte_to_char (elt, bytepos)
23568 : bytepos);
23569 spec = decode_mode_spec (it->w, c, field, &string);
23570 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23572 switch (mode_line_target)
23574 case MODE_LINE_NOPROP:
23575 case MODE_LINE_TITLE:
23576 n += store_mode_line_noprop (spec, field, prec);
23577 break;
23578 case MODE_LINE_STRING:
23580 Lisp_Object tem = build_string (spec);
23581 props = Ftext_properties_at (make_number (charpos), elt);
23582 /* Should only keep face property in props */
23583 n += store_mode_line_string (NULL, tem, false,
23584 field, prec, props);
23586 break;
23587 case MODE_LINE_DISPLAY:
23589 int nglyphs_before, nwritten;
23591 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23592 nwritten = display_string (spec, string, elt,
23593 charpos, 0, it,
23594 field, prec, 0,
23595 multibyte);
23597 /* Assign to the glyphs written above the
23598 string where the `%x' came from, position
23599 of the `%'. */
23600 if (nwritten > 0)
23602 struct glyph *glyph
23603 = (it->glyph_row->glyphs[TEXT_AREA]
23604 + nglyphs_before);
23605 int i;
23607 for (i = 0; i < nwritten; ++i)
23609 glyph[i].object = elt;
23610 glyph[i].charpos = charpos;
23613 n += nwritten;
23616 break;
23619 else /* c == 0 */
23620 break;
23624 break;
23626 case Lisp_Symbol:
23627 /* A symbol: process the value of the symbol recursively
23628 as if it appeared here directly. Avoid error if symbol void.
23629 Special case: if value of symbol is a string, output the string
23630 literally. */
23632 register Lisp_Object tem;
23634 /* If the variable is not marked as risky to set
23635 then its contents are risky to use. */
23636 if (NILP (Fget (elt, Qrisky_local_variable)))
23637 risky = true;
23639 tem = Fboundp (elt);
23640 if (!NILP (tem))
23642 tem = Fsymbol_value (elt);
23643 /* If value is a string, output that string literally:
23644 don't check for % within it. */
23645 if (STRINGP (tem))
23646 literal = true;
23648 if (!EQ (tem, elt))
23650 /* Give up right away for nil or t. */
23651 elt = tem;
23652 goto tail_recurse;
23656 break;
23658 case Lisp_Cons:
23660 register Lisp_Object car, tem;
23662 /* A cons cell: five distinct cases.
23663 If first element is :eval or :propertize, do something special.
23664 If first element is a string or a cons, process all the elements
23665 and effectively concatenate them.
23666 If first element is a negative number, truncate displaying cdr to
23667 at most that many characters. If positive, pad (with spaces)
23668 to at least that many characters.
23669 If first element is a symbol, process the cadr or caddr recursively
23670 according to whether the symbol's value is non-nil or nil. */
23671 car = XCAR (elt);
23672 if (EQ (car, QCeval))
23674 /* An element of the form (:eval FORM) means evaluate FORM
23675 and use the result as mode line elements. */
23677 if (risky)
23678 break;
23680 if (CONSP (XCDR (elt)))
23682 Lisp_Object spec;
23683 spec = safe__eval (true, XCAR (XCDR (elt)));
23684 /* The :eval form could delete the frame stored in the
23685 iterator, which will cause a crash if we try to
23686 access faces and other fields (e.g., FRAME_KBOARD)
23687 on that frame. This is a nonsensical thing to do,
23688 and signaling an error from redisplay might be
23689 dangerous, but we cannot continue with an invalid frame. */
23690 if (!FRAME_LIVE_P (it->f))
23691 signal_error (":eval deleted the frame being displayed", elt);
23692 n += display_mode_element (it, depth, field_width - n,
23693 precision - n, spec, props,
23694 risky);
23697 else if (EQ (car, QCpropertize))
23699 /* An element of the form (:propertize ELT PROPS...)
23700 means display ELT but applying properties PROPS. */
23702 if (risky)
23703 break;
23705 if (CONSP (XCDR (elt)))
23706 n += display_mode_element (it, depth, field_width - n,
23707 precision - n, XCAR (XCDR (elt)),
23708 XCDR (XCDR (elt)), risky);
23710 else if (SYMBOLP (car))
23712 tem = Fboundp (car);
23713 elt = XCDR (elt);
23714 if (!CONSP (elt))
23715 goto invalid;
23716 /* elt is now the cdr, and we know it is a cons cell.
23717 Use its car if CAR has a non-nil value. */
23718 if (!NILP (tem))
23720 tem = Fsymbol_value (car);
23721 if (!NILP (tem))
23723 elt = XCAR (elt);
23724 goto tail_recurse;
23727 /* Symbol's value is nil (or symbol is unbound)
23728 Get the cddr of the original list
23729 and if possible find the caddr and use that. */
23730 elt = XCDR (elt);
23731 if (NILP (elt))
23732 break;
23733 else if (!CONSP (elt))
23734 goto invalid;
23735 elt = XCAR (elt);
23736 goto tail_recurse;
23738 else if (INTEGERP (car))
23740 register int lim = XINT (car);
23741 elt = XCDR (elt);
23742 if (lim < 0)
23744 /* Negative int means reduce maximum width. */
23745 if (precision <= 0)
23746 precision = -lim;
23747 else
23748 precision = min (precision, -lim);
23750 else if (lim > 0)
23752 /* Padding specified. Don't let it be more than
23753 current maximum. */
23754 if (precision > 0)
23755 lim = min (precision, lim);
23757 /* If that's more padding than already wanted, queue it.
23758 But don't reduce padding already specified even if
23759 that is beyond the current truncation point. */
23760 field_width = max (lim, field_width);
23762 goto tail_recurse;
23764 else if (STRINGP (car) || CONSP (car))
23765 FOR_EACH_TAIL_SAFE (elt)
23767 if (0 < precision && precision <= n)
23768 break;
23769 n += display_mode_element (it, depth,
23770 /* Pad after only the last
23771 list element. */
23772 (! CONSP (XCDR (elt))
23773 ? field_width - n
23774 : 0),
23775 precision - n, XCAR (elt),
23776 props, risky);
23779 break;
23781 default:
23782 invalid:
23783 elt = build_string ("*invalid*");
23784 goto tail_recurse;
23787 /* Pad to FIELD_WIDTH. */
23788 if (field_width > 0 && n < field_width)
23790 switch (mode_line_target)
23792 case MODE_LINE_NOPROP:
23793 case MODE_LINE_TITLE:
23794 n += store_mode_line_noprop ("", field_width - n, 0);
23795 break;
23796 case MODE_LINE_STRING:
23797 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23798 Qnil);
23799 break;
23800 case MODE_LINE_DISPLAY:
23801 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23802 0, 0, 0);
23803 break;
23807 return n;
23810 /* Store a mode-line string element in mode_line_string_list.
23812 If STRING is non-null, display that C string. Otherwise, the Lisp
23813 string LISP_STRING is displayed.
23815 FIELD_WIDTH is the minimum number of output glyphs to produce.
23816 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23817 with spaces. FIELD_WIDTH <= 0 means don't pad.
23819 PRECISION is the maximum number of characters to output from
23820 STRING. PRECISION <= 0 means don't truncate the string.
23822 If COPY_STRING, make a copy of LISP_STRING before adding
23823 properties to the string.
23825 PROPS are the properties to add to the string.
23826 The mode_line_string_face face property is always added to the string.
23829 static int
23830 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23831 bool copy_string,
23832 int field_width, int precision, Lisp_Object props)
23834 ptrdiff_t len;
23835 int n = 0;
23837 if (string != NULL)
23839 len = strlen (string);
23840 if (precision > 0 && len > precision)
23841 len = precision;
23842 lisp_string = make_string (string, len);
23843 if (NILP (props))
23844 props = mode_line_string_face_prop;
23845 else if (!NILP (mode_line_string_face))
23847 Lisp_Object face = Fplist_get (props, Qface);
23848 props = Fcopy_sequence (props);
23849 if (NILP (face))
23850 face = mode_line_string_face;
23851 else
23852 face = list2 (face, mode_line_string_face);
23853 props = Fplist_put (props, Qface, face);
23855 Fadd_text_properties (make_number (0), make_number (len),
23856 props, lisp_string);
23858 else
23860 len = XFASTINT (Flength (lisp_string));
23861 if (precision > 0 && len > precision)
23863 len = precision;
23864 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23865 precision = -1;
23867 if (!NILP (mode_line_string_face))
23869 Lisp_Object face;
23870 if (NILP (props))
23871 props = Ftext_properties_at (make_number (0), lisp_string);
23872 face = Fplist_get (props, Qface);
23873 if (NILP (face))
23874 face = mode_line_string_face;
23875 else
23876 face = list2 (face, mode_line_string_face);
23877 props = list2 (Qface, face);
23878 if (copy_string)
23879 lisp_string = Fcopy_sequence (lisp_string);
23881 if (!NILP (props))
23882 Fadd_text_properties (make_number (0), make_number (len),
23883 props, lisp_string);
23886 if (len > 0)
23888 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23889 n += len;
23892 if (field_width > len)
23894 field_width -= len;
23895 lisp_string = Fmake_string (make_number (field_width), make_number (' '),
23896 Qnil);
23897 if (!NILP (props))
23898 Fadd_text_properties (make_number (0), make_number (field_width),
23899 props, lisp_string);
23900 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23901 n += field_width;
23904 return n;
23908 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23909 1, 4, 0,
23910 doc: /* Format a string out of a mode line format specification.
23911 First arg FORMAT specifies the mode line format (see `mode-line-format'
23912 for details) to use.
23914 By default, the format is evaluated for the currently selected window.
23916 Optional second arg FACE specifies the face property to put on all
23917 characters for which no face is specified. The value nil means the
23918 default face. The value t means whatever face the window's mode line
23919 currently uses (either `mode-line' or `mode-line-inactive',
23920 depending on whether the window is the selected window or not).
23921 An integer value means the value string has no text
23922 properties.
23924 Optional third and fourth args WINDOW and BUFFER specify the window
23925 and buffer to use as the context for the formatting (defaults
23926 are the selected window and the WINDOW's buffer). */)
23927 (Lisp_Object format, Lisp_Object face,
23928 Lisp_Object window, Lisp_Object buffer)
23930 struct it it;
23931 int len;
23932 struct window *w;
23933 struct buffer *old_buffer = NULL;
23934 int face_id;
23935 bool no_props = INTEGERP (face);
23936 ptrdiff_t count = SPECPDL_INDEX ();
23937 Lisp_Object str;
23938 int string_start = 0;
23940 w = decode_any_window (window);
23941 XSETWINDOW (window, w);
23943 if (NILP (buffer))
23944 buffer = w->contents;
23945 CHECK_BUFFER (buffer);
23947 /* Make formatting the modeline a non-op when noninteractive, otherwise
23948 there will be problems later caused by a partially initialized frame. */
23949 if (NILP (format) || noninteractive)
23950 return empty_unibyte_string;
23952 if (no_props)
23953 face = Qnil;
23955 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23956 : EQ (face, Qt) ? (EQ (window, selected_window)
23957 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23958 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23959 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23960 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23961 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23962 : DEFAULT_FACE_ID;
23964 old_buffer = current_buffer;
23966 /* Save things including mode_line_proptrans_alist,
23967 and set that to nil so that we don't alter the outer value. */
23968 record_unwind_protect (unwind_format_mode_line,
23969 format_mode_line_unwind_data
23970 (XFRAME (WINDOW_FRAME (w)),
23971 old_buffer, selected_window, true));
23972 mode_line_proptrans_alist = Qnil;
23974 Fselect_window (window, Qt);
23975 set_buffer_internal_1 (XBUFFER (buffer));
23977 init_iterator (&it, w, -1, -1, NULL, face_id);
23979 if (no_props)
23981 mode_line_target = MODE_LINE_NOPROP;
23982 mode_line_string_face_prop = Qnil;
23983 mode_line_string_list = Qnil;
23984 string_start = MODE_LINE_NOPROP_LEN (0);
23986 else
23988 mode_line_target = MODE_LINE_STRING;
23989 mode_line_string_list = Qnil;
23990 mode_line_string_face = face;
23991 mode_line_string_face_prop
23992 = NILP (face) ? Qnil : list2 (Qface, face);
23995 push_kboard (FRAME_KBOARD (it.f));
23996 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23997 pop_kboard ();
23999 if (no_props)
24001 len = MODE_LINE_NOPROP_LEN (string_start);
24002 str = make_string (mode_line_noprop_buf + string_start, len);
24004 else
24006 mode_line_string_list = Fnreverse (mode_line_string_list);
24007 str = Fmapconcat (Qidentity, mode_line_string_list,
24008 empty_unibyte_string);
24011 unbind_to (count, Qnil);
24012 return str;
24015 /* Write a null-terminated, right justified decimal representation of
24016 the positive integer D to BUF using a minimal field width WIDTH. */
24018 static void
24019 pint2str (register char *buf, register int width, register ptrdiff_t d)
24021 register char *p = buf;
24023 if (d <= 0)
24024 *p++ = '0';
24025 else
24027 while (d > 0)
24029 *p++ = d % 10 + '0';
24030 d /= 10;
24034 for (width -= (int) (p - buf); width > 0; --width)
24035 *p++ = ' ';
24036 *p-- = '\0';
24037 while (p > buf)
24039 d = *buf;
24040 *buf++ = *p;
24041 *p-- = d;
24045 /* Write a null-terminated, right justified decimal and "human
24046 readable" representation of the nonnegative integer D to BUF using
24047 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24049 static const char power_letter[] =
24051 0, /* no letter */
24052 'k', /* kilo */
24053 'M', /* mega */
24054 'G', /* giga */
24055 'T', /* tera */
24056 'P', /* peta */
24057 'E', /* exa */
24058 'Z', /* zetta */
24059 'Y' /* yotta */
24062 static void
24063 pint2hrstr (char *buf, int width, ptrdiff_t d)
24065 /* We aim to represent the nonnegative integer D as
24066 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24067 ptrdiff_t quotient = d;
24068 int remainder = 0;
24069 /* -1 means: do not use TENTHS. */
24070 int tenths = -1;
24071 int exponent = 0;
24073 /* Length of QUOTIENT.TENTHS as a string. */
24074 int length;
24076 char * psuffix;
24077 char * p;
24079 if (quotient >= 1000)
24081 /* Scale to the appropriate EXPONENT. */
24084 remainder = quotient % 1000;
24085 quotient /= 1000;
24086 exponent++;
24088 while (quotient >= 1000);
24090 /* Round to nearest and decide whether to use TENTHS or not. */
24091 if (quotient <= 9)
24093 tenths = remainder / 100;
24094 if (remainder % 100 >= 50)
24096 if (tenths < 9)
24097 tenths++;
24098 else
24100 quotient++;
24101 if (quotient == 10)
24102 tenths = -1;
24103 else
24104 tenths = 0;
24108 else
24109 if (remainder >= 500)
24111 if (quotient < 999)
24112 quotient++;
24113 else
24115 quotient = 1;
24116 exponent++;
24117 tenths = 0;
24122 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24123 if (tenths == -1 && quotient <= 99)
24124 if (quotient <= 9)
24125 length = 1;
24126 else
24127 length = 2;
24128 else
24129 length = 3;
24130 p = psuffix = buf + max (width, length);
24132 /* Print EXPONENT. */
24133 *psuffix++ = power_letter[exponent];
24134 *psuffix = '\0';
24136 /* Print TENTHS. */
24137 if (tenths >= 0)
24139 *--p = '0' + tenths;
24140 *--p = '.';
24143 /* Print QUOTIENT. */
24146 int digit = quotient % 10;
24147 *--p = '0' + digit;
24149 while ((quotient /= 10) != 0);
24151 /* Print leading spaces. */
24152 while (buf < p)
24153 *--p = ' ';
24156 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24157 If EOL_FLAG, set also a mnemonic character for end-of-line
24158 type of CODING_SYSTEM. Return updated pointer into BUF. */
24160 static unsigned char invalid_eol_type[] = "(*invalid*)";
24162 static char *
24163 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24165 Lisp_Object val;
24166 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24167 const unsigned char *eol_str;
24168 int eol_str_len;
24169 /* The EOL conversion we are using. */
24170 Lisp_Object eoltype;
24172 val = CODING_SYSTEM_SPEC (coding_system);
24173 eoltype = Qnil;
24175 if (!VECTORP (val)) /* Not yet decided. */
24177 *buf++ = multibyte ? '-' : ' ';
24178 if (eol_flag)
24179 eoltype = eol_mnemonic_undecided;
24180 /* Don't mention EOL conversion if it isn't decided. */
24182 else
24184 Lisp_Object attrs;
24185 Lisp_Object eolvalue;
24187 attrs = AREF (val, 0);
24188 eolvalue = AREF (val, 2);
24190 *buf++ = multibyte
24191 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24192 : ' ';
24194 if (eol_flag)
24196 /* The EOL conversion that is normal on this system. */
24198 if (NILP (eolvalue)) /* Not yet decided. */
24199 eoltype = eol_mnemonic_undecided;
24200 else if (VECTORP (eolvalue)) /* Not yet decided. */
24201 eoltype = eol_mnemonic_undecided;
24202 else /* eolvalue is Qunix, Qdos, or Qmac. */
24203 eoltype = (EQ (eolvalue, Qunix)
24204 ? eol_mnemonic_unix
24205 : EQ (eolvalue, Qdos)
24206 ? eol_mnemonic_dos : eol_mnemonic_mac);
24210 if (eol_flag)
24212 /* Mention the EOL conversion if it is not the usual one. */
24213 if (STRINGP (eoltype))
24215 eol_str = SDATA (eoltype);
24216 eol_str_len = SBYTES (eoltype);
24218 else if (CHARACTERP (eoltype))
24220 int c = XFASTINT (eoltype);
24221 return buf + CHAR_STRING (c, (unsigned char *) buf);
24223 else
24225 eol_str = invalid_eol_type;
24226 eol_str_len = sizeof (invalid_eol_type) - 1;
24228 memcpy (buf, eol_str, eol_str_len);
24229 buf += eol_str_len;
24232 return buf;
24235 /* Return the approximate percentage N is of D (rounding upward), or 99,
24236 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24238 static int
24239 percent99 (ptrdiff_t n, ptrdiff_t d)
24241 int percent = (d - 1 + 100.0 * n) / d;
24242 return min (percent, 99);
24245 /* Return a string for the output of a mode line %-spec for window W,
24246 generated by character C. FIELD_WIDTH > 0 means pad the string
24247 returned with spaces to that value. Return a Lisp string in
24248 *STRING if the resulting string is taken from that Lisp string.
24250 Note we operate on the current buffer for most purposes. */
24252 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24254 static const char *
24255 decode_mode_spec (struct window *w, register int c, int field_width,
24256 Lisp_Object *string)
24258 Lisp_Object obj;
24259 struct frame *f = XFRAME (WINDOW_FRAME (w));
24260 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24261 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24262 produce strings from numerical values, so limit preposterously
24263 large values of FIELD_WIDTH to avoid overrunning the buffer's
24264 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24265 bytes plus the terminating null. */
24266 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24267 struct buffer *b = current_buffer;
24269 obj = Qnil;
24270 *string = Qnil;
24272 switch (c)
24274 case '*':
24275 if (!NILP (BVAR (b, read_only)))
24276 return "%";
24277 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24278 return "*";
24279 return "-";
24281 case '+':
24282 /* This differs from %* only for a modified read-only buffer. */
24283 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24284 return "*";
24285 if (!NILP (BVAR (b, read_only)))
24286 return "%";
24287 return "-";
24289 case '&':
24290 /* This differs from %* in ignoring read-only-ness. */
24291 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24292 return "*";
24293 return "-";
24295 case '%':
24296 return "%";
24298 case '[':
24300 int i;
24301 char *p;
24303 if (command_loop_level > 5)
24304 return "[[[... ";
24305 p = decode_mode_spec_buf;
24306 for (i = 0; i < command_loop_level; i++)
24307 *p++ = '[';
24308 *p = 0;
24309 return decode_mode_spec_buf;
24312 case ']':
24314 int i;
24315 char *p;
24317 if (command_loop_level > 5)
24318 return " ...]]]";
24319 p = decode_mode_spec_buf;
24320 for (i = 0; i < command_loop_level; i++)
24321 *p++ = ']';
24322 *p = 0;
24323 return decode_mode_spec_buf;
24326 case '-':
24328 register int i;
24330 /* Let lots_of_dashes be a string of infinite length. */
24331 if (mode_line_target == MODE_LINE_NOPROP
24332 || mode_line_target == MODE_LINE_STRING)
24333 return "--";
24334 if (field_width <= 0
24335 || field_width > sizeof (lots_of_dashes))
24337 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24338 decode_mode_spec_buf[i] = '-';
24339 decode_mode_spec_buf[i] = '\0';
24340 return decode_mode_spec_buf;
24342 else
24343 return lots_of_dashes;
24346 case 'b':
24347 obj = BVAR (b, name);
24348 break;
24350 case 'c':
24351 case 'C':
24352 /* %c, %C, and %l are ignored in `frame-title-format'.
24353 (In redisplay_internal, the frame title is drawn _before_ the
24354 windows are updated, so the stuff which depends on actual
24355 window contents (such as %l) may fail to render properly, or
24356 even crash emacs.) */
24357 if (mode_line_target == MODE_LINE_TITLE)
24358 return "";
24359 else
24361 ptrdiff_t col = current_column ();
24362 int disp_col = (c == 'C') ? col + 1 : col;
24363 w->column_number_displayed = col;
24364 pint2str (decode_mode_spec_buf, width, disp_col);
24365 return decode_mode_spec_buf;
24368 case 'e':
24369 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24371 if (NILP (Vmemory_full))
24372 return "";
24373 else
24374 return "!MEM FULL! ";
24376 #else
24377 return "";
24378 #endif
24380 case 'F':
24381 /* %F displays the frame name. */
24382 if (!NILP (f->title))
24383 return SSDATA (f->title);
24384 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24385 return SSDATA (f->name);
24386 return "Emacs";
24388 case 'f':
24389 obj = BVAR (b, filename);
24390 break;
24392 case 'i':
24394 ptrdiff_t size = ZV - BEGV;
24395 pint2str (decode_mode_spec_buf, width, size);
24396 return decode_mode_spec_buf;
24399 case 'I':
24401 ptrdiff_t size = ZV - BEGV;
24402 pint2hrstr (decode_mode_spec_buf, width, size);
24403 return decode_mode_spec_buf;
24406 case 'l':
24408 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24409 ptrdiff_t topline, nlines, height;
24410 ptrdiff_t junk;
24412 /* %c, %C, and %l are ignored in `frame-title-format'. */
24413 if (mode_line_target == MODE_LINE_TITLE)
24414 return "";
24416 startpos = marker_position (w->start);
24417 startpos_byte = marker_byte_position (w->start);
24418 height = WINDOW_TOTAL_LINES (w);
24420 /* If we decided that this buffer isn't suitable for line numbers,
24421 don't forget that too fast. */
24422 if (w->base_line_pos == -1)
24423 goto no_value;
24425 /* If the buffer is very big, don't waste time. */
24426 if (INTEGERP (Vline_number_display_limit)
24427 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24429 w->base_line_pos = 0;
24430 w->base_line_number = 0;
24431 goto no_value;
24434 if (w->base_line_number > 0
24435 && w->base_line_pos > 0
24436 && w->base_line_pos <= startpos)
24438 line = w->base_line_number;
24439 linepos = w->base_line_pos;
24440 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24442 else
24444 line = 1;
24445 linepos = BUF_BEGV (b);
24446 linepos_byte = BUF_BEGV_BYTE (b);
24449 /* Count lines from base line to window start position. */
24450 nlines = display_count_lines (linepos_byte,
24451 startpos_byte,
24452 startpos, &junk);
24454 topline = nlines + line;
24456 /* Determine a new base line, if the old one is too close
24457 or too far away, or if we did not have one.
24458 "Too close" means it's plausible a scroll-down would
24459 go back past it. */
24460 if (startpos == BUF_BEGV (b))
24462 w->base_line_number = topline;
24463 w->base_line_pos = BUF_BEGV (b);
24465 else if (nlines < height + 25 || nlines > height * 3 + 50
24466 || linepos == BUF_BEGV (b))
24468 ptrdiff_t limit = BUF_BEGV (b);
24469 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24470 ptrdiff_t position;
24471 ptrdiff_t distance =
24472 (height * 2 + 30) * line_number_display_limit_width;
24474 if (startpos - distance > limit)
24476 limit = startpos - distance;
24477 limit_byte = CHAR_TO_BYTE (limit);
24480 nlines = display_count_lines (startpos_byte,
24481 limit_byte,
24482 - (height * 2 + 30),
24483 &position);
24484 /* If we couldn't find the lines we wanted within
24485 line_number_display_limit_width chars per line,
24486 give up on line numbers for this window. */
24487 if (position == limit_byte && limit == startpos - distance)
24489 w->base_line_pos = -1;
24490 w->base_line_number = 0;
24491 goto no_value;
24494 w->base_line_number = topline - nlines;
24495 w->base_line_pos = BYTE_TO_CHAR (position);
24498 /* Now count lines from the start pos to point. */
24499 nlines = display_count_lines (startpos_byte,
24500 PT_BYTE, PT, &junk);
24502 /* Record that we did display the line number. */
24503 line_number_displayed = true;
24505 /* Make the string to show. */
24506 pint2str (decode_mode_spec_buf, width, topline + nlines);
24507 return decode_mode_spec_buf;
24508 no_value:
24510 char *p = decode_mode_spec_buf;
24511 int pad = width - 2;
24512 while (pad-- > 0)
24513 *p++ = ' ';
24514 *p++ = '?';
24515 *p++ = '?';
24516 *p = '\0';
24517 return decode_mode_spec_buf;
24520 break;
24522 case 'm':
24523 obj = BVAR (b, mode_name);
24524 break;
24526 case 'n':
24527 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24528 return " Narrow";
24529 break;
24531 /* Display the "degree of travel" of the window through the buffer. */
24532 case 'o':
24534 ptrdiff_t toppos = marker_position (w->start);
24535 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24536 ptrdiff_t begv = BUF_BEGV (b);
24537 ptrdiff_t zv = BUF_ZV (b);
24539 if (zv <= botpos)
24540 return toppos <= begv ? "All" : "Bottom";
24541 else if (toppos <= begv)
24542 return "Top";
24543 else
24545 sprintf (decode_mode_spec_buf, "%2d%%",
24546 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24547 return decode_mode_spec_buf;
24551 /* Display percentage of buffer above the top of the screen. */
24552 case 'p':
24554 ptrdiff_t pos = marker_position (w->start);
24555 ptrdiff_t begv = BUF_BEGV (b);
24556 ptrdiff_t zv = BUF_ZV (b);
24558 if (w->window_end_pos <= BUF_Z (b) - zv)
24559 return pos <= begv ? "All" : "Bottom";
24560 else if (pos <= begv)
24561 return "Top";
24562 else
24564 sprintf (decode_mode_spec_buf, "%2d%%",
24565 percent99 (pos - begv, zv - begv));
24566 return decode_mode_spec_buf;
24570 /* Display percentage of size above the bottom of the screen. */
24571 case 'P':
24573 ptrdiff_t toppos = marker_position (w->start);
24574 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24575 ptrdiff_t begv = BUF_BEGV (b);
24576 ptrdiff_t zv = BUF_ZV (b);
24578 if (zv <= botpos)
24579 return toppos <= begv ? "All" : "Bottom";
24580 else
24582 sprintf (decode_mode_spec_buf,
24583 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24584 percent99 (botpos - begv, zv - begv));
24585 return decode_mode_spec_buf;
24589 /* Display percentage offsets of top and bottom of the window,
24590 using "All" (but not "Top" or "Bottom") where appropriate. */
24591 case 'q':
24593 ptrdiff_t toppos = marker_position (w->start);
24594 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24595 ptrdiff_t begv = BUF_BEGV (b);
24596 ptrdiff_t zv = BUF_ZV (b);
24597 int top_perc, bot_perc;
24599 if ((toppos <= begv) && (zv <= botpos))
24600 return "All ";
24602 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24603 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24605 if (top_perc == bot_perc)
24606 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24607 else
24608 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24610 return decode_mode_spec_buf;
24613 case 's':
24614 /* status of process */
24615 obj = Fget_buffer_process (Fcurrent_buffer ());
24616 if (NILP (obj))
24617 return "no process";
24618 #ifndef MSDOS
24619 obj = Fsymbol_name (Fprocess_status (obj));
24620 #endif
24621 break;
24623 case '@':
24625 ptrdiff_t count = inhibit_garbage_collection ();
24626 Lisp_Object curdir = BVAR (current_buffer, directory);
24627 Lisp_Object val = Qnil;
24629 if (STRINGP (curdir))
24630 val = call1 (intern ("file-remote-p"), curdir);
24632 unbind_to (count, Qnil);
24634 if (NILP (val))
24635 return "-";
24636 else
24637 return "@";
24640 case 'z':
24641 /* coding-system (not including end-of-line format) */
24642 case 'Z':
24643 /* coding-system (including end-of-line type) */
24645 bool eol_flag = (c == 'Z');
24646 char *p = decode_mode_spec_buf;
24648 if (! FRAME_WINDOW_P (f))
24650 /* No need to mention EOL here--the terminal never needs
24651 to do EOL conversion. */
24652 p = decode_mode_spec_coding (CODING_ID_NAME
24653 (FRAME_KEYBOARD_CODING (f)->id),
24654 p, false);
24655 p = decode_mode_spec_coding (CODING_ID_NAME
24656 (FRAME_TERMINAL_CODING (f)->id),
24657 p, false);
24659 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24660 p, eol_flag);
24662 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24663 #ifdef subprocesses
24664 obj = Fget_buffer_process (Fcurrent_buffer ());
24665 if (PROCESSP (obj))
24667 p = decode_mode_spec_coding
24668 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24669 p = decode_mode_spec_coding
24670 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24672 #endif /* subprocesses */
24673 #endif /* false */
24674 *p = 0;
24675 return decode_mode_spec_buf;
24679 if (STRINGP (obj))
24681 *string = obj;
24682 return SSDATA (obj);
24684 else
24685 return "";
24689 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24690 means count lines back from START_BYTE. But don't go beyond
24691 LIMIT_BYTE. Return the number of lines thus found (always
24692 nonnegative).
24694 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24695 either the position COUNT lines after/before START_BYTE, if we
24696 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24697 COUNT lines. */
24699 static ptrdiff_t
24700 display_count_lines (ptrdiff_t start_byte,
24701 ptrdiff_t limit_byte, ptrdiff_t count,
24702 ptrdiff_t *byte_pos_ptr)
24704 register unsigned char *cursor;
24705 unsigned char *base;
24707 register ptrdiff_t ceiling;
24708 register unsigned char *ceiling_addr;
24709 ptrdiff_t orig_count = count;
24711 /* If we are not in selective display mode,
24712 check only for newlines. */
24713 bool selective_display
24714 = (!NILP (BVAR (current_buffer, selective_display))
24715 && !INTEGERP (BVAR (current_buffer, selective_display)));
24717 if (count > 0)
24719 while (start_byte < limit_byte)
24721 ceiling = BUFFER_CEILING_OF (start_byte);
24722 ceiling = min (limit_byte - 1, ceiling);
24723 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24724 base = (cursor = BYTE_POS_ADDR (start_byte));
24728 if (selective_display)
24730 while (*cursor != '\n' && *cursor != 015
24731 && ++cursor != ceiling_addr)
24732 continue;
24733 if (cursor == ceiling_addr)
24734 break;
24736 else
24738 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24739 if (! cursor)
24740 break;
24743 cursor++;
24745 if (--count == 0)
24747 start_byte += cursor - base;
24748 *byte_pos_ptr = start_byte;
24749 return orig_count;
24752 while (cursor < ceiling_addr);
24754 start_byte += ceiling_addr - base;
24757 else
24759 while (start_byte > limit_byte)
24761 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24762 ceiling = max (limit_byte, ceiling);
24763 ceiling_addr = BYTE_POS_ADDR (ceiling);
24764 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24765 while (true)
24767 if (selective_display)
24769 while (--cursor >= ceiling_addr
24770 && *cursor != '\n' && *cursor != 015)
24771 continue;
24772 if (cursor < ceiling_addr)
24773 break;
24775 else
24777 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24778 if (! cursor)
24779 break;
24782 if (++count == 0)
24784 start_byte += cursor - base + 1;
24785 *byte_pos_ptr = start_byte;
24786 /* When scanning backwards, we should
24787 not count the newline posterior to which we stop. */
24788 return - orig_count - 1;
24791 start_byte += ceiling_addr - base;
24795 *byte_pos_ptr = limit_byte;
24797 if (count < 0)
24798 return - orig_count + count;
24799 return orig_count - count;
24805 /***********************************************************************
24806 Displaying strings
24807 ***********************************************************************/
24809 /* Display a NUL-terminated string, starting with index START.
24811 If STRING is non-null, display that C string. Otherwise, the Lisp
24812 string LISP_STRING is displayed. There's a case that STRING is
24813 non-null and LISP_STRING is not nil. It means STRING is a string
24814 data of LISP_STRING. In that case, we display LISP_STRING while
24815 ignoring its text properties.
24817 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24818 FACE_STRING. Display STRING or LISP_STRING with the face at
24819 FACE_STRING_POS in FACE_STRING:
24821 Display the string in the environment given by IT, but use the
24822 standard display table, temporarily.
24824 FIELD_WIDTH is the minimum number of output glyphs to produce.
24825 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24826 with spaces. If STRING has more characters, more than FIELD_WIDTH
24827 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24829 PRECISION is the maximum number of characters to output from
24830 STRING. PRECISION < 0 means don't truncate the string.
24832 This is roughly equivalent to printf format specifiers:
24834 FIELD_WIDTH PRECISION PRINTF
24835 ----------------------------------------
24836 -1 -1 %s
24837 -1 10 %.10s
24838 10 -1 %10s
24839 20 10 %20.10s
24841 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24842 display them, and < 0 means obey the current buffer's value of
24843 enable_multibyte_characters.
24845 Value is the number of columns displayed. */
24847 static int
24848 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24849 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24850 int field_width, int precision, int max_x, int multibyte)
24852 int hpos_at_start = it->hpos;
24853 int saved_face_id = it->face_id;
24854 struct glyph_row *row = it->glyph_row;
24855 ptrdiff_t it_charpos;
24857 /* Initialize the iterator IT for iteration over STRING beginning
24858 with index START. */
24859 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24860 precision, field_width, multibyte);
24861 if (string && STRINGP (lisp_string))
24862 /* LISP_STRING is the one returned by decode_mode_spec. We should
24863 ignore its text properties. */
24864 it->stop_charpos = it->end_charpos;
24866 /* If displaying STRING, set up the face of the iterator from
24867 FACE_STRING, if that's given. */
24868 if (STRINGP (face_string))
24870 ptrdiff_t endptr;
24871 struct face *face;
24873 it->face_id
24874 = face_at_string_position (it->w, face_string, face_string_pos,
24875 0, &endptr, it->base_face_id, false);
24876 face = FACE_FROM_ID (it->f, it->face_id);
24877 it->face_box_p = face->box != FACE_NO_BOX;
24880 /* Set max_x to the maximum allowed X position. Don't let it go
24881 beyond the right edge of the window. */
24882 if (max_x <= 0)
24883 max_x = it->last_visible_x;
24884 else
24885 max_x = min (max_x, it->last_visible_x);
24887 /* Skip over display elements that are not visible. because IT->w is
24888 hscrolled. */
24889 if (it->current_x < it->first_visible_x)
24890 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24891 MOVE_TO_POS | MOVE_TO_X);
24893 row->ascent = it->max_ascent;
24894 row->height = it->max_ascent + it->max_descent;
24895 row->phys_ascent = it->max_phys_ascent;
24896 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24897 row->extra_line_spacing = it->max_extra_line_spacing;
24899 if (STRINGP (it->string))
24900 it_charpos = IT_STRING_CHARPOS (*it);
24901 else
24902 it_charpos = IT_CHARPOS (*it);
24904 /* This condition is for the case that we are called with current_x
24905 past last_visible_x. */
24906 while (it->current_x < max_x)
24908 int x_before, x, n_glyphs_before, i, nglyphs;
24910 /* Get the next display element. */
24911 if (!get_next_display_element (it))
24912 break;
24914 /* Produce glyphs. */
24915 x_before = it->current_x;
24916 n_glyphs_before = row->used[TEXT_AREA];
24917 PRODUCE_GLYPHS (it);
24919 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24920 i = 0;
24921 x = x_before;
24922 while (i < nglyphs)
24924 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24926 if (it->line_wrap != TRUNCATE
24927 && x + glyph->pixel_width > max_x)
24929 /* End of continued line or max_x reached. */
24930 if (CHAR_GLYPH_PADDING_P (*glyph))
24932 /* A wide character is unbreakable. */
24933 if (row->reversed_p)
24934 unproduce_glyphs (it, row->used[TEXT_AREA]
24935 - n_glyphs_before);
24936 row->used[TEXT_AREA] = n_glyphs_before;
24937 it->current_x = x_before;
24939 else
24941 if (row->reversed_p)
24942 unproduce_glyphs (it, row->used[TEXT_AREA]
24943 - (n_glyphs_before + i));
24944 row->used[TEXT_AREA] = n_glyphs_before + i;
24945 it->current_x = x;
24947 break;
24949 else if (x + glyph->pixel_width >= it->first_visible_x)
24951 /* Glyph is at least partially visible. */
24952 ++it->hpos;
24953 if (x < it->first_visible_x)
24954 row->x = x - it->first_visible_x;
24956 else
24958 /* Glyph is off the left margin of the display area.
24959 Should not happen. */
24960 emacs_abort ();
24963 row->ascent = max (row->ascent, it->max_ascent);
24964 row->height = max (row->height, it->max_ascent + it->max_descent);
24965 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24966 row->phys_height = max (row->phys_height,
24967 it->max_phys_ascent + it->max_phys_descent);
24968 row->extra_line_spacing = max (row->extra_line_spacing,
24969 it->max_extra_line_spacing);
24970 x += glyph->pixel_width;
24971 ++i;
24974 /* Stop if max_x reached. */
24975 if (i < nglyphs)
24976 break;
24978 /* Stop at line ends. */
24979 if (ITERATOR_AT_END_OF_LINE_P (it))
24981 it->continuation_lines_width = 0;
24982 break;
24985 set_iterator_to_next (it, true);
24986 if (STRINGP (it->string))
24987 it_charpos = IT_STRING_CHARPOS (*it);
24988 else
24989 it_charpos = IT_CHARPOS (*it);
24991 /* Stop if truncating at the right edge. */
24992 if (it->line_wrap == TRUNCATE
24993 && it->current_x >= it->last_visible_x)
24995 /* Add truncation mark, but don't do it if the line is
24996 truncated at a padding space. */
24997 if (it_charpos < it->string_nchars)
24999 if (!FRAME_WINDOW_P (it->f))
25001 int ii, n;
25003 if (it->current_x > it->last_visible_x)
25005 if (!row->reversed_p)
25007 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25008 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25009 break;
25011 else
25013 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25014 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25015 break;
25016 unproduce_glyphs (it, ii + 1);
25017 ii = row->used[TEXT_AREA] - (ii + 1);
25019 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25021 row->used[TEXT_AREA] = ii;
25022 produce_special_glyphs (it, IT_TRUNCATION);
25025 produce_special_glyphs (it, IT_TRUNCATION);
25027 row->truncated_on_right_p = true;
25029 break;
25033 /* Maybe insert a truncation at the left. */
25034 if (it->first_visible_x
25035 && it_charpos > 0)
25037 if (!FRAME_WINDOW_P (it->f)
25038 || (row->reversed_p
25039 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25040 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25041 insert_left_trunc_glyphs (it);
25042 row->truncated_on_left_p = true;
25045 it->face_id = saved_face_id;
25047 /* Value is number of columns displayed. */
25048 return it->hpos - hpos_at_start;
25053 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25054 appears as an element of LIST or as the car of an element of LIST.
25055 If PROPVAL is a list, compare each element against LIST in that
25056 way, and return 1/2 if any element of PROPVAL is found in LIST.
25057 Otherwise return 0. This function cannot quit.
25058 The return value is 2 if the text is invisible but with an ellipsis
25059 and 1 if it's invisible and without an ellipsis. */
25062 invisible_prop (Lisp_Object propval, Lisp_Object list)
25064 Lisp_Object tail, proptail;
25066 for (tail = list; CONSP (tail); tail = XCDR (tail))
25068 register Lisp_Object tem;
25069 tem = XCAR (tail);
25070 if (EQ (propval, tem))
25071 return 1;
25072 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25073 return NILP (XCDR (tem)) ? 1 : 2;
25076 if (CONSP (propval))
25078 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25080 Lisp_Object propelt;
25081 propelt = XCAR (proptail);
25082 for (tail = list; CONSP (tail); tail = XCDR (tail))
25084 register Lisp_Object tem;
25085 tem = XCAR (tail);
25086 if (EQ (propelt, tem))
25087 return 1;
25088 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25089 return NILP (XCDR (tem)) ? 1 : 2;
25094 return 0;
25097 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25098 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25099 POS should be a marker or a buffer position; the value of the `invisible'
25100 property at that position in the current buffer is examined.
25101 POS can also be the actual value of the `invisible' text or overlay
25102 property of the text of interest, in which case the value itself is
25103 examined.
25105 The non-nil value returned can be t for currently invisible text that is
25106 entirely hidden on display, or some other non-nil, non-t value if the
25107 text is replaced by an ellipsis.
25109 Note that whether text with `invisible' property is actually hidden on
25110 display may depend on `buffer-invisibility-spec', which see. */)
25111 (Lisp_Object pos)
25113 Lisp_Object prop
25114 = (NATNUMP (pos) || MARKERP (pos)
25115 ? Fget_char_property (pos, Qinvisible, Qnil)
25116 : pos);
25117 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25118 return (invis == 0 ? Qnil
25119 : invis == 1 ? Qt
25120 : make_number (invis));
25123 /* Calculate a width or height in pixels from a specification using
25124 the following elements:
25126 SPEC ::=
25127 NUM - a (fractional) multiple of the default font width/height
25128 (NUM) - specifies exactly NUM pixels
25129 UNIT - a fixed number of pixels, see below.
25130 ELEMENT - size of a display element in pixels, see below.
25131 (NUM . SPEC) - equals NUM * SPEC
25132 (+ SPEC SPEC ...) - add pixel values
25133 (- SPEC SPEC ...) - subtract pixel values
25134 (- SPEC) - negate pixel value
25136 NUM ::=
25137 INT or FLOAT - a number constant
25138 SYMBOL - use symbol's (buffer local) variable binding.
25140 UNIT ::=
25141 in - pixels per inch *)
25142 mm - pixels per 1/1000 meter *)
25143 cm - pixels per 1/100 meter *)
25144 width - width of current font in pixels.
25145 height - height of current font in pixels.
25147 *) using the ratio(s) defined in display-pixels-per-inch.
25149 ELEMENT ::=
25151 left-fringe - left fringe width in pixels
25152 right-fringe - right fringe width in pixels
25154 left-margin - left margin width in pixels
25155 right-margin - right margin width in pixels
25157 scroll-bar - scroll-bar area width in pixels
25159 Examples:
25161 Pixels corresponding to 5 inches:
25162 (5 . in)
25164 Total width of non-text areas on left side of window (if scroll-bar is on left):
25165 '(space :width (+ left-fringe left-margin scroll-bar))
25167 Align to first text column (in header line):
25168 '(space :align-to 0)
25170 Align to middle of text area minus half the width of variable `my-image'
25171 containing a loaded image:
25172 '(space :align-to (0.5 . (- text my-image)))
25174 Width of left margin minus width of 1 character in the default font:
25175 '(space :width (- left-margin 1))
25177 Width of left margin minus width of 2 characters in the current font:
25178 '(space :width (- left-margin (2 . width)))
25180 Center 1 character over left-margin (in header line):
25181 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25183 Different ways to express width of left fringe plus left margin minus one pixel:
25184 '(space :width (- (+ left-fringe left-margin) (1)))
25185 '(space :width (+ left-fringe left-margin (- (1))))
25186 '(space :width (+ left-fringe left-margin (-1)))
25188 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25189 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25190 coordinate, and *RES is the additional pixel width from that point
25191 till the end of the stretch glyph.
25193 WIDTH_P non-zero means take the width dimension or X coordinate of
25194 the object specified by PROP, WIDTH_P zero means take the height
25195 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25196 non-NULL, WIDTH_P should be non-zero.)
25198 FONT is the font of the face of the surrounding text.
25200 The return value is non-zero if width or height were successfully
25201 calculated, i.e. if PROP is a valid spec. */
25203 static bool
25204 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25205 struct font *font, bool width_p, int *align_to)
25207 double pixels;
25209 # define OK_PIXELS(val) (*res = (val), true)
25210 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25212 if (NILP (prop))
25213 return OK_PIXELS (0);
25215 eassert (FRAME_LIVE_P (it->f));
25217 if (SYMBOLP (prop))
25219 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25221 char *unit = SSDATA (SYMBOL_NAME (prop));
25223 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25224 if (unit[0] == 'i' && unit[1] == 'n')
25225 pixels = 1.0;
25226 else if (unit[0] == 'm' && unit[1] == 'm')
25227 pixels = 25.4;
25228 else if (unit[0] == 'c' && unit[1] == 'm')
25229 pixels = 2.54;
25230 else
25231 pixels = 0;
25232 if (pixels > 0)
25234 double ppi = (width_p ? FRAME_RES_X (it->f)
25235 : FRAME_RES_Y (it->f));
25237 if (ppi > 0)
25238 return OK_PIXELS (ppi / pixels);
25239 return false;
25243 #ifdef HAVE_WINDOW_SYSTEM
25244 /* 'height': the height of FONT. */
25245 if (EQ (prop, Qheight))
25246 return OK_PIXELS (font
25247 ? normal_char_height (font, -1)
25248 : FRAME_LINE_HEIGHT (it->f));
25249 /* 'width': the width of FONT. */
25250 if (EQ (prop, Qwidth))
25251 return OK_PIXELS (font
25252 ? FONT_WIDTH (font)
25253 : FRAME_COLUMN_WIDTH (it->f));
25254 #else
25255 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25256 return OK_PIXELS (1);
25257 #endif
25259 /* 'text': the width or height of the text area. */
25260 if (EQ (prop, Qtext))
25261 return OK_PIXELS (width_p
25262 ? (window_box_width (it->w, TEXT_AREA)
25263 - it->lnum_pixel_width)
25264 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25266 /* ':align_to'. First time we compute the value, window
25267 elements are interpreted as the position of the element's
25268 left edge. */
25269 if (align_to && *align_to < 0)
25271 *res = 0;
25272 /* 'left': left edge of the text area. */
25273 if (EQ (prop, Qleft))
25274 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25275 + it->lnum_pixel_width);
25276 /* 'right': right edge of the text area. */
25277 if (EQ (prop, Qright))
25278 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25279 /* 'center': the center of the text area. */
25280 if (EQ (prop, Qcenter))
25281 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25282 + it->lnum_pixel_width
25283 + window_box_width (it->w, TEXT_AREA) / 2);
25284 /* 'left-fringe': left edge of the left fringe. */
25285 if (EQ (prop, Qleft_fringe))
25286 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25287 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25288 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25289 /* 'right-fringe': left edge of the right fringe. */
25290 if (EQ (prop, Qright_fringe))
25291 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25292 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25293 : window_box_right_offset (it->w, TEXT_AREA));
25294 /* 'left-margin': left edge of the left display margin. */
25295 if (EQ (prop, Qleft_margin))
25296 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25297 /* 'right-margin': left edge of the right display margin. */
25298 if (EQ (prop, Qright_margin))
25299 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25300 /* 'scroll-bar': left edge of the vertical scroll bar. */
25301 if (EQ (prop, Qscroll_bar))
25302 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25304 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25305 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25306 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25307 : 0)));
25309 else
25311 /* Otherwise, the elements stand for their width. */
25312 if (EQ (prop, Qleft_fringe))
25313 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25314 if (EQ (prop, Qright_fringe))
25315 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25316 if (EQ (prop, Qleft_margin))
25317 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25318 if (EQ (prop, Qright_margin))
25319 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25320 if (EQ (prop, Qscroll_bar))
25321 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25324 prop = buffer_local_value (prop, it->w->contents);
25325 if (EQ (prop, Qunbound))
25326 prop = Qnil;
25329 if (NUMBERP (prop))
25331 int base_unit = (width_p
25332 ? FRAME_COLUMN_WIDTH (it->f)
25333 : FRAME_LINE_HEIGHT (it->f));
25334 if (width_p && align_to && *align_to < 0)
25335 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25336 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25339 if (CONSP (prop))
25341 Lisp_Object car = XCAR (prop);
25342 Lisp_Object cdr = XCDR (prop);
25344 if (SYMBOLP (car))
25346 #ifdef HAVE_WINDOW_SYSTEM
25347 /* '(image PROPS...)': width or height of the specified image. */
25348 if (FRAME_WINDOW_P (it->f)
25349 && valid_image_p (prop))
25351 ptrdiff_t id = lookup_image (it->f, prop);
25352 struct image *img = IMAGE_FROM_ID (it->f, id);
25354 return OK_PIXELS (width_p ? img->width : img->height);
25356 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25357 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25359 /* TODO: Don't return dummy size. */
25360 return OK_PIXELS (100);
25362 #endif
25363 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25364 recursively calculated values. */
25365 if (EQ (car, Qplus) || EQ (car, Qminus))
25367 bool first = true;
25368 double px;
25370 pixels = 0;
25371 while (CONSP (cdr))
25373 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25374 font, width_p, align_to))
25375 return false;
25376 if (first)
25377 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25378 else
25379 pixels += px;
25380 cdr = XCDR (cdr);
25382 if (EQ (car, Qminus))
25383 pixels = -pixels;
25384 return OK_PIXELS (pixels);
25387 car = buffer_local_value (car, it->w->contents);
25388 if (EQ (car, Qunbound))
25389 car = Qnil;
25392 /* '(NUM)': absolute number of pixels. */
25393 if (NUMBERP (car))
25395 double fact;
25396 int offset =
25397 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25398 pixels = XFLOATINT (car);
25399 if (NILP (cdr))
25400 return OK_PIXELS (pixels + offset);
25401 if (calc_pixel_width_or_height (&fact, it, cdr,
25402 font, width_p, align_to))
25403 return OK_PIXELS (pixels * fact + offset);
25404 return false;
25407 return false;
25410 return false;
25413 void
25414 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25416 #ifdef HAVE_WINDOW_SYSTEM
25417 normal_char_ascent_descent (font, -1, ascent, descent);
25418 #else
25419 *ascent = 1;
25420 *descent = 0;
25421 #endif
25425 /***********************************************************************
25426 Glyph Display
25427 ***********************************************************************/
25429 #ifdef HAVE_WINDOW_SYSTEM
25431 #ifdef GLYPH_DEBUG
25433 void
25434 dump_glyph_string (struct glyph_string *s)
25436 fprintf (stderr, "glyph string\n");
25437 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25438 s->x, s->y, s->width, s->height);
25439 fprintf (stderr, " ybase = %d\n", s->ybase);
25440 fprintf (stderr, " hl = %u\n", s->hl);
25441 fprintf (stderr, " left overhang = %d, right = %d\n",
25442 s->left_overhang, s->right_overhang);
25443 fprintf (stderr, " nchars = %d\n", s->nchars);
25444 fprintf (stderr, " extends to end of line = %d\n",
25445 s->extends_to_end_of_line_p);
25446 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25447 fprintf (stderr, " bg width = %d\n", s->background_width);
25450 #endif /* GLYPH_DEBUG */
25452 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25453 of XChar2b structures for S; it can't be allocated in
25454 init_glyph_string because it must be allocated via `alloca'. W
25455 is the window on which S is drawn. ROW and AREA are the glyph row
25456 and area within the row from which S is constructed. START is the
25457 index of the first glyph structure covered by S. HL is a
25458 face-override for drawing S. */
25460 #ifdef HAVE_NTGUI
25461 #define OPTIONAL_HDC(hdc) HDC hdc,
25462 #define DECLARE_HDC(hdc) HDC hdc;
25463 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25464 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25465 #endif
25467 #ifndef OPTIONAL_HDC
25468 #define OPTIONAL_HDC(hdc)
25469 #define DECLARE_HDC(hdc)
25470 #define ALLOCATE_HDC(hdc, f)
25471 #define RELEASE_HDC(hdc, f)
25472 #endif
25474 static void
25475 init_glyph_string (struct glyph_string *s,
25476 OPTIONAL_HDC (hdc)
25477 XChar2b *char2b, struct window *w, struct glyph_row *row,
25478 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25480 memset (s, 0, sizeof *s);
25481 s->w = w;
25482 s->f = XFRAME (w->frame);
25483 #ifdef HAVE_NTGUI
25484 s->hdc = hdc;
25485 #endif
25486 s->display = FRAME_X_DISPLAY (s->f);
25487 s->char2b = char2b;
25488 s->hl = hl;
25489 s->row = row;
25490 s->area = area;
25491 s->first_glyph = row->glyphs[area] + start;
25492 s->height = row->height;
25493 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25494 s->ybase = s->y + row->ascent;
25498 /* Append the list of glyph strings with head H and tail T to the list
25499 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25501 static void
25502 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25503 struct glyph_string *h, struct glyph_string *t)
25505 if (h)
25507 if (*head)
25508 (*tail)->next = h;
25509 else
25510 *head = h;
25511 h->prev = *tail;
25512 *tail = t;
25517 /* Prepend the list of glyph strings with head H and tail T to the
25518 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25519 result. */
25521 static void
25522 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25523 struct glyph_string *h, struct glyph_string *t)
25525 if (h)
25527 if (*head)
25528 (*head)->prev = t;
25529 else
25530 *tail = t;
25531 t->next = *head;
25532 *head = h;
25537 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25538 Set *HEAD and *TAIL to the resulting list. */
25540 static void
25541 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25542 struct glyph_string *s)
25544 s->next = s->prev = NULL;
25545 append_glyph_string_lists (head, tail, s, s);
25549 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25550 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25551 make sure that X resources for the face returned are allocated.
25552 Value is a pointer to a realized face that is ready for display if
25553 DISPLAY_P. */
25555 static struct face *
25556 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25557 XChar2b *char2b, bool display_p)
25559 struct face *face = FACE_FROM_ID (f, face_id);
25560 unsigned code = 0;
25562 if (face->font)
25564 code = face->font->driver->encode_char (face->font, c);
25566 if (code == FONT_INVALID_CODE)
25567 code = 0;
25569 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25571 /* Make sure X resources of the face are allocated. */
25572 #ifdef HAVE_X_WINDOWS
25573 if (display_p)
25574 #endif
25576 eassert (face != NULL);
25577 prepare_face_for_display (f, face);
25580 return face;
25584 /* Get face and two-byte form of character glyph GLYPH on frame F.
25585 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25586 a pointer to a realized face that is ready for display. */
25588 static struct face *
25589 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25590 XChar2b *char2b)
25592 struct face *face;
25593 unsigned code = 0;
25595 eassert (glyph->type == CHAR_GLYPH);
25596 face = FACE_FROM_ID (f, glyph->face_id);
25598 /* Make sure X resources of the face are allocated. */
25599 prepare_face_for_display (f, face);
25601 if (face->font)
25603 if (CHAR_BYTE8_P (glyph->u.ch))
25604 code = CHAR_TO_BYTE8 (glyph->u.ch);
25605 else
25606 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25608 if (code == FONT_INVALID_CODE)
25609 code = 0;
25612 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25613 return face;
25617 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25618 Return true iff FONT has a glyph for C. */
25620 static bool
25621 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25623 unsigned code;
25625 if (CHAR_BYTE8_P (c))
25626 code = CHAR_TO_BYTE8 (c);
25627 else
25628 code = font->driver->encode_char (font, c);
25630 if (code == FONT_INVALID_CODE)
25631 return false;
25632 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25633 return true;
25637 /* Fill glyph string S with composition components specified by S->cmp.
25639 BASE_FACE is the base face of the composition.
25640 S->cmp_from is the index of the first component for S.
25642 OVERLAPS non-zero means S should draw the foreground only, and use
25643 its physical height for clipping. See also draw_glyphs.
25645 Value is the index of a component not in S. */
25647 static int
25648 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25649 int overlaps)
25651 int i;
25652 /* For all glyphs of this composition, starting at the offset
25653 S->cmp_from, until we reach the end of the definition or encounter a
25654 glyph that requires the different face, add it to S. */
25655 struct face *face;
25657 eassert (s);
25659 s->for_overlaps = overlaps;
25660 s->face = NULL;
25661 s->font = NULL;
25662 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25664 int c = COMPOSITION_GLYPH (s->cmp, i);
25666 /* TAB in a composition means display glyphs with padding space
25667 on the left or right. */
25668 if (c != '\t')
25670 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25671 -1, Qnil);
25673 face = get_char_face_and_encoding (s->f, c, face_id,
25674 s->char2b + i, true);
25675 if (face)
25677 if (! s->face)
25679 s->face = face;
25680 s->font = s->face->font;
25682 else if (s->face != face)
25683 break;
25686 ++s->nchars;
25688 s->cmp_to = i;
25690 if (s->face == NULL)
25692 s->face = base_face->ascii_face;
25693 s->font = s->face->font;
25696 /* All glyph strings for the same composition has the same width,
25697 i.e. the width set for the first component of the composition. */
25698 s->width = s->first_glyph->pixel_width;
25700 /* If the specified font could not be loaded, use the frame's
25701 default font, but record the fact that we couldn't load it in
25702 the glyph string so that we can draw rectangles for the
25703 characters of the glyph string. */
25704 if (s->font == NULL)
25706 s->font_not_found_p = true;
25707 s->font = FRAME_FONT (s->f);
25710 /* Adjust base line for subscript/superscript text. */
25711 s->ybase += s->first_glyph->voffset;
25713 return s->cmp_to;
25716 static int
25717 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25718 int start, int end, int overlaps)
25720 struct glyph *glyph, *last;
25721 Lisp_Object lgstring;
25722 int i;
25724 s->for_overlaps = overlaps;
25725 glyph = s->row->glyphs[s->area] + start;
25726 last = s->row->glyphs[s->area] + end;
25727 s->cmp_id = glyph->u.cmp.id;
25728 s->cmp_from = glyph->slice.cmp.from;
25729 s->cmp_to = glyph->slice.cmp.to + 1;
25730 s->face = FACE_FROM_ID (s->f, face_id);
25731 lgstring = composition_gstring_from_id (s->cmp_id);
25732 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25733 glyph++;
25734 while (glyph < last
25735 && glyph->u.cmp.automatic
25736 && glyph->u.cmp.id == s->cmp_id
25737 && s->cmp_to == glyph->slice.cmp.from)
25738 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25740 for (i = s->cmp_from; i < s->cmp_to; i++)
25742 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25743 unsigned code = LGLYPH_CODE (lglyph);
25745 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25747 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25748 return glyph - s->row->glyphs[s->area];
25752 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25753 See the comment of fill_glyph_string for arguments.
25754 Value is the index of the first glyph not in S. */
25757 static int
25758 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25759 int start, int end, int overlaps)
25761 struct glyph *glyph, *last;
25762 int voffset;
25764 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25765 s->for_overlaps = overlaps;
25766 glyph = s->row->glyphs[s->area] + start;
25767 last = s->row->glyphs[s->area] + end;
25768 voffset = glyph->voffset;
25769 s->face = FACE_FROM_ID (s->f, face_id);
25770 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25771 s->nchars = 1;
25772 s->width = glyph->pixel_width;
25773 glyph++;
25774 while (glyph < last
25775 && glyph->type == GLYPHLESS_GLYPH
25776 && glyph->voffset == voffset
25777 && glyph->face_id == face_id)
25779 s->nchars++;
25780 s->width += glyph->pixel_width;
25781 glyph++;
25783 s->ybase += voffset;
25784 return glyph - s->row->glyphs[s->area];
25788 /* Fill glyph string S from a sequence of character glyphs.
25790 FACE_ID is the face id of the string. START is the index of the
25791 first glyph to consider, END is the index of the last + 1.
25792 OVERLAPS non-zero means S should draw the foreground only, and use
25793 its physical height for clipping. See also draw_glyphs.
25795 Value is the index of the first glyph not in S. */
25797 static int
25798 fill_glyph_string (struct glyph_string *s, int face_id,
25799 int start, int end, int overlaps)
25801 struct glyph *glyph, *last;
25802 int voffset;
25803 bool glyph_not_available_p;
25805 eassert (s->f == XFRAME (s->w->frame));
25806 eassert (s->nchars == 0);
25807 eassert (start >= 0 && end > start);
25809 s->for_overlaps = overlaps;
25810 glyph = s->row->glyphs[s->area] + start;
25811 last = s->row->glyphs[s->area] + end;
25812 voffset = glyph->voffset;
25813 s->padding_p = glyph->padding_p;
25814 glyph_not_available_p = glyph->glyph_not_available_p;
25816 while (glyph < last
25817 && glyph->type == CHAR_GLYPH
25818 && glyph->voffset == voffset
25819 /* Same face id implies same font, nowadays. */
25820 && glyph->face_id == face_id
25821 && glyph->glyph_not_available_p == glyph_not_available_p)
25823 s->face = get_glyph_face_and_encoding (s->f, glyph,
25824 s->char2b + s->nchars);
25825 ++s->nchars;
25826 eassert (s->nchars <= end - start);
25827 s->width += glyph->pixel_width;
25828 if (glyph++->padding_p != s->padding_p)
25829 break;
25832 s->font = s->face->font;
25834 /* If the specified font could not be loaded, use the frame's font,
25835 but record the fact that we couldn't load it in
25836 S->font_not_found_p so that we can draw rectangles for the
25837 characters of the glyph string. */
25838 if (s->font == NULL || glyph_not_available_p)
25840 s->font_not_found_p = true;
25841 s->font = FRAME_FONT (s->f);
25844 /* Adjust base line for subscript/superscript text. */
25845 s->ybase += voffset;
25847 eassert (s->face && s->face->gc);
25848 return glyph - s->row->glyphs[s->area];
25852 /* Fill glyph string S from image glyph S->first_glyph. */
25854 static void
25855 fill_image_glyph_string (struct glyph_string *s)
25857 eassert (s->first_glyph->type == IMAGE_GLYPH);
25858 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25859 eassert (s->img);
25860 s->slice = s->first_glyph->slice.img;
25861 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25862 s->font = s->face->font;
25863 s->width = s->first_glyph->pixel_width;
25865 /* Adjust base line for subscript/superscript text. */
25866 s->ybase += s->first_glyph->voffset;
25870 #ifdef HAVE_XWIDGETS
25871 static void
25872 fill_xwidget_glyph_string (struct glyph_string *s)
25874 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25875 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25876 s->font = s->face->font;
25877 s->width = s->first_glyph->pixel_width;
25878 s->ybase += s->first_glyph->voffset;
25879 s->xwidget = s->first_glyph->u.xwidget;
25881 #endif
25882 /* Fill glyph string S from a sequence of stretch glyphs.
25884 START is the index of the first glyph to consider,
25885 END is the index of the last + 1.
25887 Value is the index of the first glyph not in S. */
25889 static int
25890 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25892 struct glyph *glyph, *last;
25893 int voffset, face_id;
25895 eassert (s->first_glyph->type == STRETCH_GLYPH);
25897 glyph = s->row->glyphs[s->area] + start;
25898 last = s->row->glyphs[s->area] + end;
25899 face_id = glyph->face_id;
25900 s->face = FACE_FROM_ID (s->f, face_id);
25901 s->font = s->face->font;
25902 s->width = glyph->pixel_width;
25903 s->nchars = 1;
25904 voffset = glyph->voffset;
25906 for (++glyph;
25907 (glyph < last
25908 && glyph->type == STRETCH_GLYPH
25909 && glyph->voffset == voffset
25910 && glyph->face_id == face_id);
25911 ++glyph)
25912 s->width += glyph->pixel_width;
25914 /* Adjust base line for subscript/superscript text. */
25915 s->ybase += voffset;
25917 /* The case that face->gc == 0 is handled when drawing the glyph
25918 string by calling prepare_face_for_display. */
25919 eassert (s->face);
25920 return glyph - s->row->glyphs[s->area];
25923 static struct font_metrics *
25924 get_per_char_metric (struct font *font, XChar2b *char2b)
25926 static struct font_metrics metrics;
25927 unsigned code;
25929 if (! font)
25930 return NULL;
25931 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25932 if (code == FONT_INVALID_CODE)
25933 return NULL;
25934 font->driver->text_extents (font, &code, 1, &metrics);
25935 return &metrics;
25938 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25939 for FONT. Values are taken from font-global ones, except for fonts
25940 that claim preposterously large values, but whose glyphs actually
25941 have reasonable dimensions. C is the character to use for metrics
25942 if the font-global values are too large; if C is negative, the
25943 function selects a default character. */
25944 static void
25945 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25947 *ascent = FONT_BASE (font);
25948 *descent = FONT_DESCENT (font);
25950 if (FONT_TOO_HIGH (font))
25952 XChar2b char2b;
25954 /* Get metrics of C, defaulting to a reasonably sized ASCII
25955 character. */
25956 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25958 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25960 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25962 /* We add 1 pixel to character dimensions as heuristics
25963 that produces nicer display, e.g. when the face has
25964 the box attribute. */
25965 *ascent = pcm->ascent + 1;
25966 *descent = pcm->descent + 1;
25972 /* A subroutine that computes a reasonable "normal character height"
25973 for fonts that claim preposterously large vertical dimensions, but
25974 whose glyphs are actually reasonably sized. C is the character
25975 whose metrics to use for those fonts, or -1 for default
25976 character. */
25977 static int
25978 normal_char_height (struct font *font, int c)
25980 int ascent, descent;
25982 normal_char_ascent_descent (font, c, &ascent, &descent);
25984 return ascent + descent;
25987 /* EXPORT for RIF:
25988 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25989 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25990 assumed to be zero. */
25992 void
25993 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25995 *left = *right = 0;
25997 if (glyph->type == CHAR_GLYPH)
25999 XChar2b char2b;
26000 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26001 if (face->font)
26003 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26004 if (pcm)
26006 if (pcm->rbearing > pcm->width)
26007 *right = pcm->rbearing - pcm->width;
26008 if (pcm->lbearing < 0)
26009 *left = -pcm->lbearing;
26013 else if (glyph->type == COMPOSITE_GLYPH)
26015 if (! glyph->u.cmp.automatic)
26017 struct composition *cmp = composition_table[glyph->u.cmp.id];
26019 if (cmp->rbearing > cmp->pixel_width)
26020 *right = cmp->rbearing - cmp->pixel_width;
26021 if (cmp->lbearing < 0)
26022 *left = - cmp->lbearing;
26024 else
26026 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26027 struct font_metrics metrics;
26029 composition_gstring_width (gstring, glyph->slice.cmp.from,
26030 glyph->slice.cmp.to + 1, &metrics);
26031 if (metrics.rbearing > metrics.width)
26032 *right = metrics.rbearing - metrics.width;
26033 if (metrics.lbearing < 0)
26034 *left = - metrics.lbearing;
26040 /* Return the index of the first glyph preceding glyph string S that
26041 is overwritten by S because of S's left overhang. Value is -1
26042 if no glyphs are overwritten. */
26044 static int
26045 left_overwritten (struct glyph_string *s)
26047 int k;
26049 if (s->left_overhang)
26051 int x = 0, i;
26052 struct glyph *glyphs = s->row->glyphs[s->area];
26053 int first = s->first_glyph - glyphs;
26055 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26056 x -= glyphs[i].pixel_width;
26058 k = i + 1;
26060 else
26061 k = -1;
26063 return k;
26067 /* Return the index of the first glyph preceding glyph string S that
26068 is overwriting S because of its right overhang. Value is -1 if no
26069 glyph in front of S overwrites S. */
26071 static int
26072 left_overwriting (struct glyph_string *s)
26074 int i, k, x;
26075 struct glyph *glyphs = s->row->glyphs[s->area];
26076 int first = s->first_glyph - glyphs;
26078 k = -1;
26079 x = 0;
26080 for (i = first - 1; i >= 0; --i)
26082 int left, right;
26083 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26084 if (x + right > 0)
26085 k = i;
26086 x -= glyphs[i].pixel_width;
26089 return k;
26093 /* Return the index of the last glyph following glyph string S that is
26094 overwritten by S because of S's right overhang. Value is -1 if
26095 no such glyph is found. */
26097 static int
26098 right_overwritten (struct glyph_string *s)
26100 int k = -1;
26102 if (s->right_overhang)
26104 int x = 0, i;
26105 struct glyph *glyphs = s->row->glyphs[s->area];
26106 int first = (s->first_glyph - glyphs
26107 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26108 int end = s->row->used[s->area];
26110 for (i = first; i < end && s->right_overhang > x; ++i)
26111 x += glyphs[i].pixel_width;
26113 k = i;
26116 return k;
26120 /* Return the index of the last glyph following glyph string S that
26121 overwrites S because of its left overhang. Value is negative
26122 if no such glyph is found. */
26124 static int
26125 right_overwriting (struct glyph_string *s)
26127 int i, k, x;
26128 int end = s->row->used[s->area];
26129 struct glyph *glyphs = s->row->glyphs[s->area];
26130 int first = (s->first_glyph - glyphs
26131 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26133 k = -1;
26134 x = 0;
26135 for (i = first; i < end; ++i)
26137 int left, right;
26138 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26139 if (x - left < 0)
26140 k = i;
26141 x += glyphs[i].pixel_width;
26144 return k;
26148 /* Set background width of glyph string S. START is the index of the
26149 first glyph following S. LAST_X is the right-most x-position + 1
26150 in the drawing area. */
26152 static void
26153 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26155 /* If the face of this glyph string has to be drawn to the end of
26156 the drawing area, set S->extends_to_end_of_line_p. */
26158 if (start == s->row->used[s->area]
26159 && ((s->row->fill_line_p
26160 && (s->hl == DRAW_NORMAL_TEXT
26161 || s->hl == DRAW_IMAGE_RAISED
26162 || s->hl == DRAW_IMAGE_SUNKEN))
26163 || s->hl == DRAW_MOUSE_FACE))
26164 s->extends_to_end_of_line_p = true;
26166 /* If S extends its face to the end of the line, set its
26167 background_width to the distance to the right edge of the drawing
26168 area. */
26169 if (s->extends_to_end_of_line_p)
26170 s->background_width = last_x - s->x + 1;
26171 else
26172 s->background_width = s->width;
26176 /* Return glyph string that shares background with glyph string S and
26177 whose `background_width' member has been set. */
26179 static struct glyph_string *
26180 glyph_string_containing_background_width (struct glyph_string *s)
26182 if (s->cmp)
26183 while (s->cmp_from)
26184 s = s->prev;
26186 return s;
26190 /* Compute overhangs and x-positions for glyph string S and its
26191 predecessors, or successors. X is the starting x-position for S.
26192 BACKWARD_P means process predecessors. */
26194 static void
26195 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26197 if (backward_p)
26199 while (s)
26201 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26202 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26203 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26204 x -= s->width;
26205 s->x = x;
26206 s = s->prev;
26209 else
26211 while (s)
26213 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26214 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26215 s->x = x;
26216 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26217 x += s->width;
26218 s = s->next;
26225 /* The following macros are only called from draw_glyphs below.
26226 They reference the following parameters of that function directly:
26227 `w', `row', `area', and `overlap_p'
26228 as well as the following local variables:
26229 `s', `f', and `hdc' (in W32) */
26231 #ifdef HAVE_NTGUI
26232 /* On W32, silently add local `hdc' variable to argument list of
26233 init_glyph_string. */
26234 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26235 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26236 #else
26237 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26238 init_glyph_string (s, char2b, w, row, area, start, hl)
26239 #endif
26241 /* Add a glyph string for a stretch glyph to the list of strings
26242 between HEAD and TAIL. START is the index of the stretch glyph in
26243 row area AREA of glyph row ROW. END is the index of the last glyph
26244 in that glyph row area. X is the current output position assigned
26245 to the new glyph string constructed. HL overrides that face of the
26246 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26247 is the right-most x-position of the drawing area. */
26249 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26250 and below -- keep them on one line. */
26251 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26252 do \
26254 s = alloca (sizeof *s); \
26255 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26256 START = fill_stretch_glyph_string (s, START, END); \
26257 append_glyph_string (&HEAD, &TAIL, s); \
26258 s->x = (X); \
26260 while (false)
26263 /* Add a glyph string for an image glyph to the list of strings
26264 between HEAD and TAIL. START is the index of the image glyph in
26265 row area AREA of glyph row ROW. END is the index of the last glyph
26266 in that glyph row area. X is the current output position assigned
26267 to the new glyph string constructed. HL overrides that face of the
26268 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26269 is the right-most x-position of the drawing area. */
26271 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26272 do \
26274 s = alloca (sizeof *s); \
26275 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26276 fill_image_glyph_string (s); \
26277 append_glyph_string (&HEAD, &TAIL, s); \
26278 ++START; \
26279 s->x = (X); \
26281 while (false)
26283 #ifndef HAVE_XWIDGETS
26284 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26285 eassume (false)
26286 #else
26287 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26288 do \
26290 s = alloca (sizeof *s); \
26291 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26292 fill_xwidget_glyph_string (s); \
26293 append_glyph_string (&(HEAD), &(TAIL), s); \
26294 ++(START); \
26295 s->x = (X); \
26297 while (false)
26298 #endif
26300 /* Add a glyph string for a sequence of character glyphs to the list
26301 of strings between HEAD and TAIL. START is the index of the first
26302 glyph in row area AREA of glyph row ROW that is part of the new
26303 glyph string. END is the index of the last glyph in that glyph row
26304 area. X is the current output position assigned to the new glyph
26305 string constructed. HL overrides that face of the glyph; e.g. it
26306 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26307 right-most x-position of the drawing area. */
26309 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26310 do \
26312 int face_id; \
26313 XChar2b *char2b; \
26315 face_id = (row)->glyphs[area][START].face_id; \
26317 s = alloca (sizeof *s); \
26318 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26319 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26320 append_glyph_string (&HEAD, &TAIL, s); \
26321 s->x = (X); \
26322 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26324 while (false)
26327 /* Add a glyph string for a composite sequence to the list of strings
26328 between HEAD and TAIL. START is the index of the first glyph in
26329 row area AREA of glyph row ROW that is part of the new glyph
26330 string. END is the index of the last glyph in that glyph row area.
26331 X is the current output position assigned to the new glyph string
26332 constructed. HL overrides that face of the glyph; e.g. it is
26333 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26334 x-position of the drawing area. */
26336 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26337 do { \
26338 int face_id = (row)->glyphs[area][START].face_id; \
26339 struct face *base_face = FACE_FROM_ID (f, face_id); \
26340 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26341 struct composition *cmp = composition_table[cmp_id]; \
26342 XChar2b *char2b; \
26343 struct glyph_string *first_s = NULL; \
26344 int n; \
26346 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26348 /* Make glyph_strings for each glyph sequence that is drawable by \
26349 the same face, and append them to HEAD/TAIL. */ \
26350 for (n = 0; n < cmp->glyph_len;) \
26352 s = alloca (sizeof *s); \
26353 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26354 append_glyph_string (&(HEAD), &(TAIL), s); \
26355 s->cmp = cmp; \
26356 s->cmp_from = n; \
26357 s->x = (X); \
26358 if (n == 0) \
26359 first_s = s; \
26360 n = fill_composite_glyph_string (s, base_face, overlaps); \
26363 ++START; \
26364 s = first_s; \
26365 } while (false)
26368 /* Add a glyph string for a glyph-string sequence to the list of strings
26369 between HEAD and TAIL. */
26371 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26372 do { \
26373 int face_id; \
26374 XChar2b *char2b; \
26375 Lisp_Object gstring; \
26377 face_id = (row)->glyphs[area][START].face_id; \
26378 gstring = (composition_gstring_from_id \
26379 ((row)->glyphs[area][START].u.cmp.id)); \
26380 s = alloca (sizeof *s); \
26381 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26382 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26383 append_glyph_string (&(HEAD), &(TAIL), s); \
26384 s->x = (X); \
26385 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26386 } while (false)
26389 /* Add a glyph string for a sequence of glyphless character's glyphs
26390 to the list of strings between HEAD and TAIL. The meanings of
26391 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26393 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26394 do \
26396 int face_id; \
26398 face_id = (row)->glyphs[area][START].face_id; \
26400 s = alloca (sizeof *s); \
26401 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26402 append_glyph_string (&HEAD, &TAIL, s); \
26403 s->x = (X); \
26404 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26405 overlaps); \
26407 while (false)
26410 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26411 of AREA of glyph row ROW on window W between indices START and END.
26412 HL overrides the face for drawing glyph strings, e.g. it is
26413 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26414 x-positions of the drawing area.
26416 This is an ugly monster macro construct because we must use alloca
26417 to allocate glyph strings (because draw_glyphs can be called
26418 asynchronously). */
26420 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26421 do \
26423 HEAD = TAIL = NULL; \
26424 while (START < END) \
26426 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26427 switch (first_glyph->type) \
26429 case CHAR_GLYPH: \
26430 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26431 HL, X, LAST_X); \
26432 break; \
26434 case COMPOSITE_GLYPH: \
26435 if (first_glyph->u.cmp.automatic) \
26436 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26437 HL, X, LAST_X); \
26438 else \
26439 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26440 HL, X, LAST_X); \
26441 break; \
26443 case STRETCH_GLYPH: \
26444 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26445 HL, X, LAST_X); \
26446 break; \
26448 case IMAGE_GLYPH: \
26449 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26450 HL, X, LAST_X); \
26451 break;
26453 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26454 case XWIDGET_GLYPH: \
26455 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26456 HL, X, LAST_X); \
26457 break;
26459 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26460 case GLYPHLESS_GLYPH: \
26461 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26462 HL, X, LAST_X); \
26463 break; \
26465 default: \
26466 emacs_abort (); \
26469 if (s) \
26471 set_glyph_string_background_width (s, START, LAST_X); \
26472 (X) += s->width; \
26475 } while (false)
26478 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26479 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26480 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26481 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26484 /* Draw glyphs between START and END in AREA of ROW on window W,
26485 starting at x-position X. X is relative to AREA in W. HL is a
26486 face-override with the following meaning:
26488 DRAW_NORMAL_TEXT draw normally
26489 DRAW_CURSOR draw in cursor face
26490 DRAW_MOUSE_FACE draw in mouse face.
26491 DRAW_INVERSE_VIDEO draw in mode line face
26492 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26493 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26495 If OVERLAPS is non-zero, draw only the foreground of characters and
26496 clip to the physical height of ROW. Non-zero value also defines
26497 the overlapping part to be drawn:
26499 OVERLAPS_PRED overlap with preceding rows
26500 OVERLAPS_SUCC overlap with succeeding rows
26501 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26502 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26504 Value is the x-position reached, relative to AREA of W. */
26506 static int
26507 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26508 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26509 enum draw_glyphs_face hl, int overlaps)
26511 struct glyph_string *head, *tail;
26512 struct glyph_string *s;
26513 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26514 int i, j, x_reached, last_x, area_left = 0;
26515 struct frame *f = XFRAME (WINDOW_FRAME (w));
26516 DECLARE_HDC (hdc);
26518 ALLOCATE_HDC (hdc, f);
26520 /* Let's rather be paranoid than getting a SEGV. */
26521 end = min (end, row->used[area]);
26522 start = clip_to_bounds (0, start, end);
26524 /* Translate X to frame coordinates. Set last_x to the right
26525 end of the drawing area. */
26526 if (row->full_width_p)
26528 /* X is relative to the left edge of W, without scroll bars
26529 or fringes. */
26530 area_left = WINDOW_LEFT_EDGE_X (w);
26531 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26532 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26534 else
26536 area_left = window_box_left (w, area);
26537 last_x = area_left + window_box_width (w, area);
26539 x += area_left;
26541 /* Build a doubly-linked list of glyph_string structures between
26542 head and tail from what we have to draw. Note that the macro
26543 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26544 the reason we use a separate variable `i'. */
26545 i = start;
26546 USE_SAFE_ALLOCA;
26547 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26548 if (tail)
26550 s = glyph_string_containing_background_width (tail);
26551 x_reached = s->x + s->background_width;
26553 else
26554 x_reached = x;
26556 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26557 the row, redraw some glyphs in front or following the glyph
26558 strings built above. */
26559 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26561 struct glyph_string *h, *t;
26562 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26563 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26564 bool check_mouse_face = false;
26565 int dummy_x = 0;
26567 /* If mouse highlighting is on, we may need to draw adjacent
26568 glyphs using mouse-face highlighting. */
26569 if (area == TEXT_AREA && row->mouse_face_p
26570 && hlinfo->mouse_face_beg_row >= 0
26571 && hlinfo->mouse_face_end_row >= 0)
26573 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26575 if (row_vpos >= hlinfo->mouse_face_beg_row
26576 && row_vpos <= hlinfo->mouse_face_end_row)
26578 check_mouse_face = true;
26579 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26580 ? hlinfo->mouse_face_beg_col : 0;
26581 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26582 ? hlinfo->mouse_face_end_col
26583 : row->used[TEXT_AREA];
26587 /* Compute overhangs for all glyph strings. */
26588 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26589 for (s = head; s; s = s->next)
26590 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26592 /* Prepend glyph strings for glyphs in front of the first glyph
26593 string that are overwritten because of the first glyph
26594 string's left overhang. The background of all strings
26595 prepended must be drawn because the first glyph string
26596 draws over it. */
26597 i = left_overwritten (head);
26598 if (i >= 0)
26600 enum draw_glyphs_face overlap_hl;
26602 /* If this row contains mouse highlighting, attempt to draw
26603 the overlapped glyphs with the correct highlight. This
26604 code fails if the overlap encompasses more than one glyph
26605 and mouse-highlight spans only some of these glyphs.
26606 However, making it work perfectly involves a lot more
26607 code, and I don't know if the pathological case occurs in
26608 practice, so we'll stick to this for now. --- cyd */
26609 if (check_mouse_face
26610 && mouse_beg_col < start && mouse_end_col > i)
26611 overlap_hl = DRAW_MOUSE_FACE;
26612 else
26613 overlap_hl = DRAW_NORMAL_TEXT;
26615 if (hl != overlap_hl)
26616 clip_head = head;
26617 j = i;
26618 BUILD_GLYPH_STRINGS (j, start, h, t,
26619 overlap_hl, dummy_x, last_x);
26620 start = i;
26621 compute_overhangs_and_x (t, head->x, true);
26622 prepend_glyph_string_lists (&head, &tail, h, t);
26623 if (clip_head == NULL)
26624 clip_head = head;
26627 /* Prepend glyph strings for glyphs in front of the first glyph
26628 string that overwrite that glyph string because of their
26629 right overhang. For these strings, only the foreground must
26630 be drawn, because it draws over the glyph string at `head'.
26631 The background must not be drawn because this would overwrite
26632 right overhangs of preceding glyphs for which no glyph
26633 strings exist. */
26634 i = left_overwriting (head);
26635 if (i >= 0)
26637 enum draw_glyphs_face overlap_hl;
26639 if (check_mouse_face
26640 && mouse_beg_col < start && mouse_end_col > i)
26641 overlap_hl = DRAW_MOUSE_FACE;
26642 else
26643 overlap_hl = DRAW_NORMAL_TEXT;
26645 if (hl == overlap_hl || clip_head == NULL)
26646 clip_head = head;
26647 BUILD_GLYPH_STRINGS (i, start, h, t,
26648 overlap_hl, dummy_x, last_x);
26649 for (s = h; s; s = s->next)
26650 s->background_filled_p = true;
26651 compute_overhangs_and_x (t, head->x, true);
26652 prepend_glyph_string_lists (&head, &tail, h, t);
26655 /* Append glyphs strings for glyphs following the last glyph
26656 string tail that are overwritten by tail. The background of
26657 these strings has to be drawn because tail's foreground draws
26658 over it. */
26659 i = right_overwritten (tail);
26660 if (i >= 0)
26662 enum draw_glyphs_face overlap_hl;
26664 if (check_mouse_face
26665 && mouse_beg_col < i && mouse_end_col > end)
26666 overlap_hl = DRAW_MOUSE_FACE;
26667 else
26668 overlap_hl = DRAW_NORMAL_TEXT;
26670 if (hl != overlap_hl)
26671 clip_tail = tail;
26672 BUILD_GLYPH_STRINGS (end, i, h, t,
26673 overlap_hl, x, last_x);
26674 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26675 we don't have `end = i;' here. */
26676 compute_overhangs_and_x (h, tail->x + tail->width, false);
26677 append_glyph_string_lists (&head, &tail, h, t);
26678 if (clip_tail == NULL)
26679 clip_tail = tail;
26682 /* Append glyph strings for glyphs following the last glyph
26683 string tail that overwrite tail. The foreground of such
26684 glyphs has to be drawn because it writes into the background
26685 of tail. The background must not be drawn because it could
26686 paint over the foreground of following glyphs. */
26687 i = right_overwriting (tail);
26688 if (i >= 0)
26690 enum draw_glyphs_face overlap_hl;
26691 if (check_mouse_face
26692 && mouse_beg_col < i && mouse_end_col > end)
26693 overlap_hl = DRAW_MOUSE_FACE;
26694 else
26695 overlap_hl = DRAW_NORMAL_TEXT;
26697 if (hl == overlap_hl || clip_tail == NULL)
26698 clip_tail = tail;
26699 i++; /* We must include the Ith glyph. */
26700 BUILD_GLYPH_STRINGS (end, i, h, t,
26701 overlap_hl, x, last_x);
26702 for (s = h; s; s = s->next)
26703 s->background_filled_p = true;
26704 compute_overhangs_and_x (h, tail->x + tail->width, false);
26705 append_glyph_string_lists (&head, &tail, h, t);
26707 tail = glyph_string_containing_background_width (tail);
26708 if (clip_tail)
26709 clip_tail = glyph_string_containing_background_width (clip_tail);
26710 if (clip_head || clip_tail)
26711 for (s = head; s; s = s->next)
26713 s->clip_head = clip_head;
26714 s->clip_tail = clip_tail;
26718 /* Draw all strings. */
26719 for (s = head; s; s = s->next)
26720 FRAME_RIF (f)->draw_glyph_string (s);
26722 #ifndef HAVE_NS
26723 /* When focus a sole frame and move horizontally, this clears on_p
26724 causing a failure to erase prev cursor position. */
26725 if (area == TEXT_AREA
26726 && !row->full_width_p
26727 /* When drawing overlapping rows, only the glyph strings'
26728 foreground is drawn, which doesn't erase a cursor
26729 completely. */
26730 && !overlaps)
26732 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26733 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26734 : (tail ? tail->x + tail->background_width : x));
26735 x0 -= area_left;
26736 x1 -= area_left;
26738 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26739 row->y, MATRIX_ROW_BOTTOM_Y (row));
26741 #endif
26743 /* Value is the x-position up to which drawn, relative to AREA of W.
26744 This doesn't include parts drawn because of overhangs. */
26745 if (row->full_width_p)
26746 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26747 else
26748 x_reached -= area_left;
26750 RELEASE_HDC (hdc, f);
26752 SAFE_FREE ();
26753 return x_reached;
26756 /* Find the first glyph in the run of underlined glyphs preceding the
26757 beginning of glyph string S, and return its font (which could be
26758 NULL). This is needed because that font determines the underline
26759 position and thickness for the entire run of the underlined glyphs.
26760 This function is called from the draw_glyph_string method of GUI
26761 frame's redisplay interface (RIF) when it needs to draw in an
26762 underlined face. */
26763 struct font *
26764 font_for_underline_metrics (struct glyph_string *s)
26766 struct glyph *g0 = s->row->glyphs[s->area], *g;
26768 for (g = s->first_glyph - 1; g >= g0; g--)
26770 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26771 if (!(prev_face && prev_face->underline_p))
26772 break;
26775 /* If preceding glyphs are not underlined, use the font of S. */
26776 if (g == s->first_glyph - 1)
26777 return s->font;
26778 else
26780 /* Otherwise use the font of the last glyph we saw in the above
26781 loop whose face had the underline_p flag set. */
26782 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26786 /* Expand row matrix if too narrow. Don't expand if area
26787 is not present. */
26789 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26791 if (!it->f->fonts_changed \
26792 && (it->glyph_row->glyphs[area] \
26793 < it->glyph_row->glyphs[area + 1])) \
26795 it->w->ncols_scale_factor++; \
26796 it->f->fonts_changed = true; \
26800 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26801 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26803 static void
26804 append_glyph (struct it *it)
26806 struct glyph *glyph;
26807 enum glyph_row_area area = it->area;
26809 eassert (it->glyph_row);
26810 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26812 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26813 if (glyph < it->glyph_row->glyphs[area + 1])
26815 /* If the glyph row is reversed, we need to prepend the glyph
26816 rather than append it. */
26817 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26819 struct glyph *g;
26821 /* Make room for the additional glyph. */
26822 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26823 g[1] = *g;
26824 glyph = it->glyph_row->glyphs[area];
26826 glyph->charpos = CHARPOS (it->position);
26827 glyph->object = it->object;
26828 if (it->pixel_width > 0)
26830 eassert (it->pixel_width <= SHRT_MAX);
26831 glyph->pixel_width = it->pixel_width;
26832 glyph->padding_p = false;
26834 else
26836 /* Assure at least 1-pixel width. Otherwise, cursor can't
26837 be displayed correctly. */
26838 glyph->pixel_width = 1;
26839 glyph->padding_p = true;
26841 glyph->ascent = it->ascent;
26842 glyph->descent = it->descent;
26843 glyph->voffset = it->voffset;
26844 glyph->type = CHAR_GLYPH;
26845 glyph->avoid_cursor_p = it->avoid_cursor_p;
26846 glyph->multibyte_p = it->multibyte_p;
26847 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26849 /* In R2L rows, the left and the right box edges need to be
26850 drawn in reverse direction. */
26851 glyph->right_box_line_p = it->start_of_box_run_p;
26852 glyph->left_box_line_p = it->end_of_box_run_p;
26854 else
26856 glyph->left_box_line_p = it->start_of_box_run_p;
26857 glyph->right_box_line_p = it->end_of_box_run_p;
26859 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26860 || it->phys_descent > it->descent);
26861 glyph->glyph_not_available_p = it->glyph_not_available_p;
26862 glyph->face_id = it->face_id;
26863 glyph->u.ch = it->char_to_display;
26864 glyph->slice.img = null_glyph_slice;
26865 glyph->font_type = FONT_TYPE_UNKNOWN;
26866 if (it->bidi_p)
26868 glyph->resolved_level = it->bidi_it.resolved_level;
26869 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26870 glyph->bidi_type = it->bidi_it.type;
26872 else
26874 glyph->resolved_level = 0;
26875 glyph->bidi_type = UNKNOWN_BT;
26877 ++it->glyph_row->used[area];
26879 else
26880 IT_EXPAND_MATRIX_WIDTH (it, area);
26883 /* Store one glyph for the composition IT->cmp_it.id in
26884 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26885 non-null. */
26887 static void
26888 append_composite_glyph (struct it *it)
26890 struct glyph *glyph;
26891 enum glyph_row_area area = it->area;
26893 eassert (it->glyph_row);
26895 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26896 if (glyph < it->glyph_row->glyphs[area + 1])
26898 /* If the glyph row is reversed, we need to prepend the glyph
26899 rather than append it. */
26900 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26902 struct glyph *g;
26904 /* Make room for the new glyph. */
26905 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26906 g[1] = *g;
26907 glyph = it->glyph_row->glyphs[it->area];
26909 glyph->charpos = it->cmp_it.charpos;
26910 glyph->object = it->object;
26911 eassert (it->pixel_width <= SHRT_MAX);
26912 glyph->pixel_width = it->pixel_width;
26913 glyph->ascent = it->ascent;
26914 glyph->descent = it->descent;
26915 glyph->voffset = it->voffset;
26916 glyph->type = COMPOSITE_GLYPH;
26917 if (it->cmp_it.ch < 0)
26919 glyph->u.cmp.automatic = false;
26920 glyph->u.cmp.id = it->cmp_it.id;
26921 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26923 else
26925 glyph->u.cmp.automatic = true;
26926 glyph->u.cmp.id = it->cmp_it.id;
26927 glyph->slice.cmp.from = it->cmp_it.from;
26928 glyph->slice.cmp.to = it->cmp_it.to - 1;
26930 glyph->avoid_cursor_p = it->avoid_cursor_p;
26931 glyph->multibyte_p = it->multibyte_p;
26932 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26934 /* In R2L rows, the left and the right box edges need to be
26935 drawn in reverse direction. */
26936 glyph->right_box_line_p = it->start_of_box_run_p;
26937 glyph->left_box_line_p = it->end_of_box_run_p;
26939 else
26941 glyph->left_box_line_p = it->start_of_box_run_p;
26942 glyph->right_box_line_p = it->end_of_box_run_p;
26944 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26945 || it->phys_descent > it->descent);
26946 glyph->padding_p = false;
26947 glyph->glyph_not_available_p = false;
26948 glyph->face_id = it->face_id;
26949 glyph->font_type = FONT_TYPE_UNKNOWN;
26950 if (it->bidi_p)
26952 glyph->resolved_level = it->bidi_it.resolved_level;
26953 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26954 glyph->bidi_type = it->bidi_it.type;
26956 ++it->glyph_row->used[area];
26958 else
26959 IT_EXPAND_MATRIX_WIDTH (it, area);
26963 /* Change IT->ascent and IT->height according to the setting of
26964 IT->voffset. */
26966 static void
26967 take_vertical_position_into_account (struct it *it)
26969 if (it->voffset)
26971 if (it->voffset < 0)
26972 /* Increase the ascent so that we can display the text higher
26973 in the line. */
26974 it->ascent -= it->voffset;
26975 else
26976 /* Increase the descent so that we can display the text lower
26977 in the line. */
26978 it->descent += it->voffset;
26983 /* Produce glyphs/get display metrics for the image IT is loaded with.
26984 See the description of struct display_iterator in dispextern.h for
26985 an overview of struct display_iterator. */
26987 static void
26988 produce_image_glyph (struct it *it)
26990 struct image *img;
26991 struct face *face;
26992 int glyph_ascent, crop;
26993 struct glyph_slice slice;
26995 eassert (it->what == IT_IMAGE);
26997 face = FACE_FROM_ID (it->f, it->face_id);
26998 /* Make sure X resources of the face is loaded. */
26999 prepare_face_for_display (it->f, face);
27001 if (it->image_id < 0)
27003 /* Fringe bitmap. */
27004 it->ascent = it->phys_ascent = 0;
27005 it->descent = it->phys_descent = 0;
27006 it->pixel_width = 0;
27007 it->nglyphs = 0;
27008 return;
27011 img = IMAGE_FROM_ID (it->f, it->image_id);
27012 /* Make sure X resources of the image is loaded. */
27013 prepare_image_for_display (it->f, img);
27015 slice.x = slice.y = 0;
27016 slice.width = img->width;
27017 slice.height = img->height;
27019 if (INTEGERP (it->slice.x))
27020 slice.x = XINT (it->slice.x);
27021 else if (FLOATP (it->slice.x))
27022 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27024 if (INTEGERP (it->slice.y))
27025 slice.y = XINT (it->slice.y);
27026 else if (FLOATP (it->slice.y))
27027 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27029 if (INTEGERP (it->slice.width))
27030 slice.width = XINT (it->slice.width);
27031 else if (FLOATP (it->slice.width))
27032 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27034 if (INTEGERP (it->slice.height))
27035 slice.height = XINT (it->slice.height);
27036 else if (FLOATP (it->slice.height))
27037 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27039 if (slice.x >= img->width)
27040 slice.x = img->width;
27041 if (slice.y >= img->height)
27042 slice.y = img->height;
27043 if (slice.x + slice.width >= img->width)
27044 slice.width = img->width - slice.x;
27045 if (slice.y + slice.height > img->height)
27046 slice.height = img->height - slice.y;
27048 if (slice.width == 0 || slice.height == 0)
27049 return;
27051 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27053 it->descent = slice.height - glyph_ascent;
27054 if (slice.y == 0)
27055 it->descent += img->vmargin;
27056 if (slice.y + slice.height == img->height)
27057 it->descent += img->vmargin;
27058 it->phys_descent = it->descent;
27060 it->pixel_width = slice.width;
27061 if (slice.x == 0)
27062 it->pixel_width += img->hmargin;
27063 if (slice.x + slice.width == img->width)
27064 it->pixel_width += img->hmargin;
27066 /* It's quite possible for images to have an ascent greater than
27067 their height, so don't get confused in that case. */
27068 if (it->descent < 0)
27069 it->descent = 0;
27071 it->nglyphs = 1;
27073 if (face->box != FACE_NO_BOX)
27075 if (face->box_line_width > 0)
27077 if (slice.y == 0)
27078 it->ascent += face->box_line_width;
27079 if (slice.y + slice.height == img->height)
27080 it->descent += face->box_line_width;
27083 if (it->start_of_box_run_p && slice.x == 0)
27084 it->pixel_width += eabs (face->box_line_width);
27085 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27086 it->pixel_width += eabs (face->box_line_width);
27089 take_vertical_position_into_account (it);
27091 /* Automatically crop wide image glyphs at right edge so we can
27092 draw the cursor on same display row. */
27093 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27094 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27096 it->pixel_width -= crop;
27097 slice.width -= crop;
27100 if (it->glyph_row)
27102 struct glyph *glyph;
27103 enum glyph_row_area area = it->area;
27105 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27106 if (it->glyph_row->reversed_p)
27108 struct glyph *g;
27110 /* Make room for the new glyph. */
27111 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27112 g[1] = *g;
27113 glyph = it->glyph_row->glyphs[it->area];
27115 if (glyph < it->glyph_row->glyphs[area + 1])
27117 glyph->charpos = CHARPOS (it->position);
27118 glyph->object = it->object;
27119 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27120 glyph->ascent = glyph_ascent;
27121 glyph->descent = it->descent;
27122 glyph->voffset = it->voffset;
27123 glyph->type = IMAGE_GLYPH;
27124 glyph->avoid_cursor_p = it->avoid_cursor_p;
27125 glyph->multibyte_p = it->multibyte_p;
27126 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27128 /* In R2L rows, the left and the right box edges need to be
27129 drawn in reverse direction. */
27130 glyph->right_box_line_p = it->start_of_box_run_p;
27131 glyph->left_box_line_p = it->end_of_box_run_p;
27133 else
27135 glyph->left_box_line_p = it->start_of_box_run_p;
27136 glyph->right_box_line_p = it->end_of_box_run_p;
27138 glyph->overlaps_vertically_p = false;
27139 glyph->padding_p = false;
27140 glyph->glyph_not_available_p = false;
27141 glyph->face_id = it->face_id;
27142 glyph->u.img_id = img->id;
27143 glyph->slice.img = slice;
27144 glyph->font_type = FONT_TYPE_UNKNOWN;
27145 if (it->bidi_p)
27147 glyph->resolved_level = it->bidi_it.resolved_level;
27148 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27149 glyph->bidi_type = it->bidi_it.type;
27151 ++it->glyph_row->used[area];
27153 else
27154 IT_EXPAND_MATRIX_WIDTH (it, area);
27158 static void
27159 produce_xwidget_glyph (struct it *it)
27161 #ifdef HAVE_XWIDGETS
27162 struct xwidget *xw;
27163 int glyph_ascent, crop;
27164 eassert (it->what == IT_XWIDGET);
27166 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27167 /* Make sure X resources of the face is loaded. */
27168 prepare_face_for_display (it->f, face);
27170 xw = it->xwidget;
27171 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27172 it->descent = xw->height/2;
27173 it->phys_descent = it->descent;
27174 it->pixel_width = xw->width;
27175 /* It's quite possible for images to have an ascent greater than
27176 their height, so don't get confused in that case. */
27177 if (it->descent < 0)
27178 it->descent = 0;
27180 it->nglyphs = 1;
27182 if (face->box != FACE_NO_BOX)
27184 if (face->box_line_width > 0)
27186 it->ascent += face->box_line_width;
27187 it->descent += face->box_line_width;
27190 if (it->start_of_box_run_p)
27191 it->pixel_width += eabs (face->box_line_width);
27192 it->pixel_width += eabs (face->box_line_width);
27195 take_vertical_position_into_account (it);
27197 /* Automatically crop wide image glyphs at right edge so we can
27198 draw the cursor on same display row. */
27199 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27200 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27201 it->pixel_width -= crop;
27203 if (it->glyph_row)
27205 enum glyph_row_area area = it->area;
27206 struct glyph *glyph
27207 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27209 if (it->glyph_row->reversed_p)
27211 struct glyph *g;
27213 /* Make room for the new glyph. */
27214 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27215 g[1] = *g;
27216 glyph = it->glyph_row->glyphs[it->area];
27218 if (glyph < it->glyph_row->glyphs[area + 1])
27220 glyph->charpos = CHARPOS (it->position);
27221 glyph->object = it->object;
27222 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27223 glyph->ascent = glyph_ascent;
27224 glyph->descent = it->descent;
27225 glyph->voffset = it->voffset;
27226 glyph->type = XWIDGET_GLYPH;
27227 glyph->avoid_cursor_p = it->avoid_cursor_p;
27228 glyph->multibyte_p = it->multibyte_p;
27229 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27231 /* In R2L rows, the left and the right box edges need to be
27232 drawn in reverse direction. */
27233 glyph->right_box_line_p = it->start_of_box_run_p;
27234 glyph->left_box_line_p = it->end_of_box_run_p;
27236 else
27238 glyph->left_box_line_p = it->start_of_box_run_p;
27239 glyph->right_box_line_p = it->end_of_box_run_p;
27241 glyph->overlaps_vertically_p = 0;
27242 glyph->padding_p = 0;
27243 glyph->glyph_not_available_p = 0;
27244 glyph->face_id = it->face_id;
27245 glyph->u.xwidget = it->xwidget;
27246 glyph->font_type = FONT_TYPE_UNKNOWN;
27247 if (it->bidi_p)
27249 glyph->resolved_level = it->bidi_it.resolved_level;
27250 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27251 glyph->bidi_type = it->bidi_it.type;
27253 ++it->glyph_row->used[area];
27255 else
27256 IT_EXPAND_MATRIX_WIDTH (it, area);
27258 #endif
27261 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27262 of the glyph, WIDTH and HEIGHT are the width and height of the
27263 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27265 static void
27266 append_stretch_glyph (struct it *it, Lisp_Object object,
27267 int width, int height, int ascent)
27269 struct glyph *glyph;
27270 enum glyph_row_area area = it->area;
27272 eassert (ascent >= 0 && ascent <= height);
27274 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27275 if (glyph < it->glyph_row->glyphs[area + 1])
27277 /* If the glyph row is reversed, we need to prepend the glyph
27278 rather than append it. */
27279 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27281 struct glyph *g;
27283 /* Make room for the additional glyph. */
27284 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27285 g[1] = *g;
27286 glyph = it->glyph_row->glyphs[area];
27288 /* Decrease the width of the first glyph of the row that
27289 begins before first_visible_x (e.g., due to hscroll).
27290 This is so the overall width of the row becomes smaller
27291 by the scroll amount, and the stretch glyph appended by
27292 extend_face_to_end_of_line will be wider, to shift the
27293 row glyphs to the right. (In L2R rows, the corresponding
27294 left-shift effect is accomplished by setting row->x to a
27295 negative value, which won't work with R2L rows.)
27297 This must leave us with a positive value of WIDTH, since
27298 otherwise the call to move_it_in_display_line_to at the
27299 beginning of display_line would have got past the entire
27300 first glyph, and then it->current_x would have been
27301 greater or equal to it->first_visible_x. */
27302 if (it->current_x < it->first_visible_x)
27303 width -= it->first_visible_x - it->current_x;
27304 eassert (width > 0);
27306 glyph->charpos = CHARPOS (it->position);
27307 glyph->object = object;
27308 /* FIXME: It would be better to use TYPE_MAX here, but
27309 __typeof__ is not portable enough... */
27310 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27311 glyph->ascent = ascent;
27312 glyph->descent = height - ascent;
27313 glyph->voffset = it->voffset;
27314 glyph->type = STRETCH_GLYPH;
27315 glyph->avoid_cursor_p = it->avoid_cursor_p;
27316 glyph->multibyte_p = it->multibyte_p;
27317 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27319 /* In R2L rows, the left and the right box edges need to be
27320 drawn in reverse direction. */
27321 glyph->right_box_line_p = it->start_of_box_run_p;
27322 glyph->left_box_line_p = it->end_of_box_run_p;
27324 else
27326 glyph->left_box_line_p = it->start_of_box_run_p;
27327 glyph->right_box_line_p = it->end_of_box_run_p;
27329 glyph->overlaps_vertically_p = false;
27330 glyph->padding_p = false;
27331 glyph->glyph_not_available_p = false;
27332 glyph->face_id = it->face_id;
27333 glyph->u.stretch.ascent = ascent;
27334 glyph->u.stretch.height = height;
27335 glyph->slice.img = null_glyph_slice;
27336 glyph->font_type = FONT_TYPE_UNKNOWN;
27337 if (it->bidi_p)
27339 glyph->resolved_level = it->bidi_it.resolved_level;
27340 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27341 glyph->bidi_type = it->bidi_it.type;
27343 else
27345 glyph->resolved_level = 0;
27346 glyph->bidi_type = UNKNOWN_BT;
27348 ++it->glyph_row->used[area];
27350 else
27351 IT_EXPAND_MATRIX_WIDTH (it, area);
27354 #endif /* HAVE_WINDOW_SYSTEM */
27356 /* Produce a stretch glyph for iterator IT. IT->object is the value
27357 of the glyph property displayed. The value must be a list
27358 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27359 being recognized:
27361 1. `:width WIDTH' specifies that the space should be WIDTH *
27362 canonical char width wide. WIDTH may be an integer or floating
27363 point number.
27365 2. `:relative-width FACTOR' specifies that the width of the stretch
27366 should be computed from the width of the first character having the
27367 `glyph' property, and should be FACTOR times that width.
27369 3. `:align-to HPOS' specifies that the space should be wide enough
27370 to reach HPOS, a value in canonical character units.
27372 Exactly one of the above pairs must be present.
27374 4. `:height HEIGHT' specifies that the height of the stretch produced
27375 should be HEIGHT, measured in canonical character units.
27377 5. `:relative-height FACTOR' specifies that the height of the
27378 stretch should be FACTOR times the height of the characters having
27379 the glyph property.
27381 Either none or exactly one of 4 or 5 must be present.
27383 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27384 of the stretch should be used for the ascent of the stretch.
27385 ASCENT must be in the range 0 <= ASCENT <= 100. */
27387 void
27388 produce_stretch_glyph (struct it *it)
27390 /* (space :width WIDTH :height HEIGHT ...) */
27391 Lisp_Object prop, plist;
27392 int width = 0, height = 0, align_to = -1;
27393 bool zero_width_ok_p = false;
27394 double tem;
27395 struct font *font = NULL;
27397 #ifdef HAVE_WINDOW_SYSTEM
27398 int ascent = 0;
27399 bool zero_height_ok_p = false;
27401 if (FRAME_WINDOW_P (it->f))
27403 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27404 font = face->font ? face->font : FRAME_FONT (it->f);
27405 prepare_face_for_display (it->f, face);
27407 #endif
27409 /* List should start with `space'. */
27410 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27411 plist = XCDR (it->object);
27413 /* Compute the width of the stretch. */
27414 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27415 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27417 /* Absolute width `:width WIDTH' specified and valid. */
27418 zero_width_ok_p = true;
27419 width = (int)tem;
27421 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27423 /* Relative width `:relative-width FACTOR' specified and valid.
27424 Compute the width of the characters having the `glyph'
27425 property. */
27426 struct it it2;
27427 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27429 it2 = *it;
27430 if (it->multibyte_p)
27431 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27432 else
27434 it2.c = it2.char_to_display = *p, it2.len = 1;
27435 if (! ASCII_CHAR_P (it2.c))
27436 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27439 it2.glyph_row = NULL;
27440 it2.what = IT_CHARACTER;
27441 PRODUCE_GLYPHS (&it2);
27442 width = NUMVAL (prop) * it2.pixel_width;
27444 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27445 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27446 &align_to))
27448 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27449 align_to = (align_to < 0
27451 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27452 else if (align_to < 0)
27453 align_to = window_box_left_offset (it->w, TEXT_AREA);
27454 width = max (0, (int)tem + align_to - it->current_x);
27455 zero_width_ok_p = true;
27457 else
27458 /* Nothing specified -> width defaults to canonical char width. */
27459 width = FRAME_COLUMN_WIDTH (it->f);
27461 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27462 width = 1;
27464 #ifdef HAVE_WINDOW_SYSTEM
27465 /* Compute height. */
27466 if (FRAME_WINDOW_P (it->f))
27468 int default_height = normal_char_height (font, ' ');
27470 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27471 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27473 height = (int)tem;
27474 zero_height_ok_p = true;
27476 else if (prop = Fplist_get (plist, QCrelative_height),
27477 NUMVAL (prop) > 0)
27478 height = default_height * NUMVAL (prop);
27479 else
27480 height = default_height;
27482 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27483 height = 1;
27485 /* Compute percentage of height used for ascent. If
27486 `:ascent ASCENT' is present and valid, use that. Otherwise,
27487 derive the ascent from the font in use. */
27488 if (prop = Fplist_get (plist, QCascent),
27489 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27490 ascent = height * NUMVAL (prop) / 100.0;
27491 else if (!NILP (prop)
27492 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27493 ascent = min (max (0, (int)tem), height);
27494 else
27495 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27497 else
27498 #endif /* HAVE_WINDOW_SYSTEM */
27499 height = 1;
27501 if (width > 0 && it->line_wrap != TRUNCATE
27502 && it->current_x + width > it->last_visible_x)
27504 width = it->last_visible_x - it->current_x;
27505 #ifdef HAVE_WINDOW_SYSTEM
27506 /* Subtract one more pixel from the stretch width, but only on
27507 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27508 width -= FRAME_WINDOW_P (it->f);
27509 #endif
27512 if (width > 0 && height > 0 && it->glyph_row)
27514 Lisp_Object o_object = it->object;
27515 Lisp_Object object = it->stack[it->sp - 1].string;
27516 int n = width;
27518 if (!STRINGP (object))
27519 object = it->w->contents;
27520 #ifdef HAVE_WINDOW_SYSTEM
27521 if (FRAME_WINDOW_P (it->f))
27522 append_stretch_glyph (it, object, width, height, ascent);
27523 else
27524 #endif
27526 it->object = object;
27527 it->char_to_display = ' ';
27528 it->pixel_width = it->len = 1;
27529 while (n--)
27530 tty_append_glyph (it);
27531 it->object = o_object;
27535 it->pixel_width = width;
27536 #ifdef HAVE_WINDOW_SYSTEM
27537 if (FRAME_WINDOW_P (it->f))
27539 it->ascent = it->phys_ascent = ascent;
27540 it->descent = it->phys_descent = height - it->ascent;
27541 it->nglyphs = width > 0 && height > 0;
27542 take_vertical_position_into_account (it);
27544 else
27545 #endif
27546 it->nglyphs = width;
27549 /* Get information about special display element WHAT in an
27550 environment described by IT. WHAT is one of IT_TRUNCATION or
27551 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27552 non-null glyph_row member. This function ensures that fields like
27553 face_id, c, len of IT are left untouched. */
27555 static void
27556 produce_special_glyphs (struct it *it, enum display_element_type what)
27558 struct it temp_it;
27559 Lisp_Object gc;
27560 GLYPH glyph;
27562 temp_it = *it;
27563 temp_it.object = Qnil;
27564 memset (&temp_it.current, 0, sizeof temp_it.current);
27566 if (what == IT_CONTINUATION)
27568 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27569 if (it->bidi_it.paragraph_dir == R2L)
27570 SET_GLYPH_FROM_CHAR (glyph, '/');
27571 else
27572 SET_GLYPH_FROM_CHAR (glyph, '\\');
27573 if (it->dp
27574 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27576 /* FIXME: Should we mirror GC for R2L lines? */
27577 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27578 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27581 else if (what == IT_TRUNCATION)
27583 /* Truncation glyph. */
27584 SET_GLYPH_FROM_CHAR (glyph, '$');
27585 if (it->dp
27586 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27588 /* FIXME: Should we mirror GC for R2L lines? */
27589 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27590 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27593 else
27594 emacs_abort ();
27596 #ifdef HAVE_WINDOW_SYSTEM
27597 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27598 is turned off, we precede the truncation/continuation glyphs by a
27599 stretch glyph whose width is computed such that these special
27600 glyphs are aligned at the window margin, even when very different
27601 fonts are used in different glyph rows. */
27602 if (FRAME_WINDOW_P (temp_it.f)
27603 /* init_iterator calls this with it->glyph_row == NULL, and it
27604 wants only the pixel width of the truncation/continuation
27605 glyphs. */
27606 && temp_it.glyph_row
27607 /* insert_left_trunc_glyphs calls us at the beginning of the
27608 row, and it has its own calculation of the stretch glyph
27609 width. */
27610 && temp_it.glyph_row->used[TEXT_AREA] > 0
27611 && (temp_it.glyph_row->reversed_p
27612 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27613 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27615 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27617 if (stretch_width > 0)
27619 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27620 struct font *font =
27621 face->font ? face->font : FRAME_FONT (temp_it.f);
27622 int stretch_ascent =
27623 (((temp_it.ascent + temp_it.descent)
27624 * FONT_BASE (font)) / FONT_HEIGHT (font));
27626 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27627 temp_it.ascent + temp_it.descent,
27628 stretch_ascent);
27631 #endif
27633 temp_it.dp = NULL;
27634 temp_it.what = IT_CHARACTER;
27635 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27636 temp_it.face_id = GLYPH_FACE (glyph);
27637 temp_it.len = CHAR_BYTES (temp_it.c);
27639 PRODUCE_GLYPHS (&temp_it);
27640 it->pixel_width = temp_it.pixel_width;
27641 it->nglyphs = temp_it.nglyphs;
27644 #ifdef HAVE_WINDOW_SYSTEM
27646 /* Calculate line-height and line-spacing properties.
27647 An integer value specifies explicit pixel value.
27648 A float value specifies relative value to current face height.
27649 A cons (float . face-name) specifies relative value to
27650 height of specified face font.
27652 Returns height in pixels, or nil. */
27654 static Lisp_Object
27655 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27656 int boff, bool override)
27658 Lisp_Object face_name = Qnil;
27659 int ascent, descent, height;
27661 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27662 return val;
27664 if (CONSP (val))
27666 face_name = XCAR (val);
27667 val = XCDR (val);
27668 if (!NUMBERP (val))
27669 val = make_number (1);
27670 if (NILP (face_name))
27672 height = it->ascent + it->descent;
27673 goto scale;
27677 if (NILP (face_name))
27679 font = FRAME_FONT (it->f);
27680 boff = FRAME_BASELINE_OFFSET (it->f);
27682 else if (EQ (face_name, Qt))
27684 override = false;
27686 else
27688 int face_id;
27689 struct face *face;
27691 face_id = lookup_named_face (it->f, face_name, false);
27692 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27693 if (face == NULL || ((font = face->font) == NULL))
27694 return make_number (-1);
27695 boff = font->baseline_offset;
27696 if (font->vertical_centering)
27697 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27700 normal_char_ascent_descent (font, -1, &ascent, &descent);
27702 if (override)
27704 it->override_ascent = ascent;
27705 it->override_descent = descent;
27706 it->override_boff = boff;
27709 height = ascent + descent;
27711 scale:
27712 if (FLOATP (val))
27713 height = (int)(XFLOAT_DATA (val) * height);
27714 else if (INTEGERP (val))
27715 height *= XINT (val);
27717 return make_number (height);
27721 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27722 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27723 and only if this is for a character for which no font was found.
27725 If the display method (it->glyphless_method) is
27726 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27727 length of the acronym or the hexadecimal string, UPPER_XOFF and
27728 UPPER_YOFF are pixel offsets for the upper part of the string,
27729 LOWER_XOFF and LOWER_YOFF are for the lower part.
27731 For the other display methods, LEN through LOWER_YOFF are zero. */
27733 static void
27734 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27735 short upper_xoff, short upper_yoff,
27736 short lower_xoff, short lower_yoff)
27738 struct glyph *glyph;
27739 enum glyph_row_area area = it->area;
27741 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27742 if (glyph < it->glyph_row->glyphs[area + 1])
27744 /* If the glyph row is reversed, we need to prepend the glyph
27745 rather than append it. */
27746 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27748 struct glyph *g;
27750 /* Make room for the additional glyph. */
27751 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27752 g[1] = *g;
27753 glyph = it->glyph_row->glyphs[area];
27755 glyph->charpos = CHARPOS (it->position);
27756 glyph->object = it->object;
27757 eassert (it->pixel_width <= SHRT_MAX);
27758 glyph->pixel_width = it->pixel_width;
27759 glyph->ascent = it->ascent;
27760 glyph->descent = it->descent;
27761 glyph->voffset = it->voffset;
27762 glyph->type = GLYPHLESS_GLYPH;
27763 glyph->u.glyphless.method = it->glyphless_method;
27764 glyph->u.glyphless.for_no_font = for_no_font;
27765 glyph->u.glyphless.len = len;
27766 glyph->u.glyphless.ch = it->c;
27767 glyph->slice.glyphless.upper_xoff = upper_xoff;
27768 glyph->slice.glyphless.upper_yoff = upper_yoff;
27769 glyph->slice.glyphless.lower_xoff = lower_xoff;
27770 glyph->slice.glyphless.lower_yoff = lower_yoff;
27771 glyph->avoid_cursor_p = it->avoid_cursor_p;
27772 glyph->multibyte_p = it->multibyte_p;
27773 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27775 /* In R2L rows, the left and the right box edges need to be
27776 drawn in reverse direction. */
27777 glyph->right_box_line_p = it->start_of_box_run_p;
27778 glyph->left_box_line_p = it->end_of_box_run_p;
27780 else
27782 glyph->left_box_line_p = it->start_of_box_run_p;
27783 glyph->right_box_line_p = it->end_of_box_run_p;
27785 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27786 || it->phys_descent > it->descent);
27787 glyph->padding_p = false;
27788 glyph->glyph_not_available_p = false;
27789 glyph->face_id = face_id;
27790 glyph->font_type = FONT_TYPE_UNKNOWN;
27791 if (it->bidi_p)
27793 glyph->resolved_level = it->bidi_it.resolved_level;
27794 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27795 glyph->bidi_type = it->bidi_it.type;
27797 ++it->glyph_row->used[area];
27799 else
27800 IT_EXPAND_MATRIX_WIDTH (it, area);
27804 /* Produce a glyph for a glyphless character for iterator IT.
27805 IT->glyphless_method specifies which method to use for displaying
27806 the character. See the description of enum
27807 glyphless_display_method in dispextern.h for the detail.
27809 FOR_NO_FONT is true if and only if this is for a character for
27810 which no font was found. ACRONYM, if non-nil, is an acronym string
27811 for the character. */
27813 static void
27814 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27816 int face_id;
27817 struct face *face;
27818 struct font *font;
27819 int base_width, base_height, width, height;
27820 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27821 int len;
27823 /* Get the metrics of the base font. We always refer to the current
27824 ASCII face. */
27825 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27826 font = face->font ? face->font : FRAME_FONT (it->f);
27827 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27828 it->ascent += font->baseline_offset;
27829 it->descent -= font->baseline_offset;
27830 base_height = it->ascent + it->descent;
27831 base_width = font->average_width;
27833 face_id = merge_glyphless_glyph_face (it);
27835 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27837 it->pixel_width = THIN_SPACE_WIDTH;
27838 len = 0;
27839 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27841 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27843 width = CHARACTER_WIDTH (it->c);
27844 if (width == 0)
27845 width = 1;
27846 else if (width > 4)
27847 width = 4;
27848 it->pixel_width = base_width * width;
27849 len = 0;
27850 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27852 else
27854 char buf[7];
27855 const char *str;
27856 unsigned int code[6];
27857 int upper_len;
27858 int ascent, descent;
27859 struct font_metrics metrics_upper, metrics_lower;
27861 face = FACE_FROM_ID (it->f, face_id);
27862 font = face->font ? face->font : FRAME_FONT (it->f);
27863 prepare_face_for_display (it->f, face);
27865 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27867 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27868 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27869 if (CONSP (acronym))
27870 acronym = XCAR (acronym);
27871 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27873 else
27875 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27876 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27877 str = buf;
27879 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27880 code[len] = font->driver->encode_char (font, str[len]);
27881 upper_len = (len + 1) / 2;
27882 font->driver->text_extents (font, code, upper_len,
27883 &metrics_upper);
27884 font->driver->text_extents (font, code + upper_len, len - upper_len,
27885 &metrics_lower);
27889 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27890 width = max (metrics_upper.width, metrics_lower.width) + 4;
27891 upper_xoff = upper_yoff = 2; /* the typical case */
27892 if (base_width >= width)
27894 /* Align the upper to the left, the lower to the right. */
27895 it->pixel_width = base_width;
27896 lower_xoff = base_width - 2 - metrics_lower.width;
27898 else
27900 /* Center the shorter one. */
27901 it->pixel_width = width;
27902 if (metrics_upper.width >= metrics_lower.width)
27903 lower_xoff = (width - metrics_lower.width) / 2;
27904 else
27906 /* FIXME: This code doesn't look right. It formerly was
27907 missing the "lower_xoff = 0;", which couldn't have
27908 been right since it left lower_xoff uninitialized. */
27909 lower_xoff = 0;
27910 upper_xoff = (width - metrics_upper.width) / 2;
27914 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27915 top, bottom, and between upper and lower strings. */
27916 height = (metrics_upper.ascent + metrics_upper.descent
27917 + metrics_lower.ascent + metrics_lower.descent) + 5;
27918 /* Center vertically.
27919 H:base_height, D:base_descent
27920 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27922 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27923 descent = D - H/2 + h/2;
27924 lower_yoff = descent - 2 - ld;
27925 upper_yoff = lower_yoff - la - 1 - ud; */
27926 ascent = - (it->descent - (base_height + height + 1) / 2);
27927 descent = it->descent - (base_height - height) / 2;
27928 lower_yoff = descent - 2 - metrics_lower.descent;
27929 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27930 - metrics_upper.descent);
27931 /* Don't make the height shorter than the base height. */
27932 if (height > base_height)
27934 it->ascent = ascent;
27935 it->descent = descent;
27939 it->phys_ascent = it->ascent;
27940 it->phys_descent = it->descent;
27941 if (it->glyph_row)
27942 append_glyphless_glyph (it, face_id, for_no_font, len,
27943 upper_xoff, upper_yoff,
27944 lower_xoff, lower_yoff);
27945 it->nglyphs = 1;
27946 take_vertical_position_into_account (it);
27950 /* RIF:
27951 Produce glyphs/get display metrics for the display element IT is
27952 loaded with. See the description of struct it in dispextern.h
27953 for an overview of struct it. */
27955 void
27956 x_produce_glyphs (struct it *it)
27958 int extra_line_spacing = it->extra_line_spacing;
27960 it->glyph_not_available_p = false;
27962 if (it->what == IT_CHARACTER)
27964 XChar2b char2b;
27965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27966 struct font *font = face->font;
27967 struct font_metrics *pcm = NULL;
27968 int boff; /* Baseline offset. */
27970 if (font == NULL)
27972 /* When no suitable font is found, display this character by
27973 the method specified in the first extra slot of
27974 Vglyphless_char_display. */
27975 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27977 eassert (it->what == IT_GLYPHLESS);
27978 produce_glyphless_glyph (it, true,
27979 STRINGP (acronym) ? acronym : Qnil);
27980 goto done;
27983 boff = font->baseline_offset;
27984 if (font->vertical_centering)
27985 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27987 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27989 it->nglyphs = 1;
27991 if (it->override_ascent >= 0)
27993 it->ascent = it->override_ascent;
27994 it->descent = it->override_descent;
27995 boff = it->override_boff;
27997 else
27999 it->ascent = FONT_BASE (font) + boff;
28000 it->descent = FONT_DESCENT (font) - boff;
28003 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28005 pcm = get_per_char_metric (font, &char2b);
28006 if (pcm->width == 0
28007 && pcm->rbearing == 0 && pcm->lbearing == 0)
28008 pcm = NULL;
28011 if (pcm)
28013 it->phys_ascent = pcm->ascent + boff;
28014 it->phys_descent = pcm->descent - boff;
28015 it->pixel_width = pcm->width;
28016 /* Don't use font-global values for ascent and descent
28017 if they result in an exceedingly large line height. */
28018 if (it->override_ascent < 0)
28020 if (FONT_TOO_HIGH (font))
28022 it->ascent = it->phys_ascent;
28023 it->descent = it->phys_descent;
28024 /* These limitations are enforced by an
28025 assertion near the end of this function. */
28026 if (it->ascent < 0)
28027 it->ascent = 0;
28028 if (it->descent < 0)
28029 it->descent = 0;
28033 else
28035 it->glyph_not_available_p = true;
28036 it->phys_ascent = it->ascent;
28037 it->phys_descent = it->descent;
28038 it->pixel_width = font->space_width;
28041 if (it->constrain_row_ascent_descent_p)
28043 if (it->descent > it->max_descent)
28045 it->ascent += it->descent - it->max_descent;
28046 it->descent = it->max_descent;
28048 if (it->ascent > it->max_ascent)
28050 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28051 it->ascent = it->max_ascent;
28053 it->phys_ascent = min (it->phys_ascent, it->ascent);
28054 it->phys_descent = min (it->phys_descent, it->descent);
28055 extra_line_spacing = 0;
28058 /* If this is a space inside a region of text with
28059 `space-width' property, change its width. */
28060 bool stretched_p
28061 = it->char_to_display == ' ' && !NILP (it->space_width);
28062 if (stretched_p)
28063 it->pixel_width *= XFLOATINT (it->space_width);
28065 /* If face has a box, add the box thickness to the character
28066 height. If character has a box line to the left and/or
28067 right, add the box line width to the character's width. */
28068 if (face->box != FACE_NO_BOX)
28070 int thick = face->box_line_width;
28072 if (thick > 0)
28074 it->ascent += thick;
28075 it->descent += thick;
28077 else
28078 thick = -thick;
28080 if (it->start_of_box_run_p)
28081 it->pixel_width += thick;
28082 if (it->end_of_box_run_p)
28083 it->pixel_width += thick;
28086 /* If face has an overline, add the height of the overline
28087 (1 pixel) and a 1 pixel margin to the character height. */
28088 if (face->overline_p)
28089 it->ascent += overline_margin;
28091 if (it->constrain_row_ascent_descent_p)
28093 if (it->ascent > it->max_ascent)
28094 it->ascent = it->max_ascent;
28095 if (it->descent > it->max_descent)
28096 it->descent = it->max_descent;
28099 take_vertical_position_into_account (it);
28101 /* If we have to actually produce glyphs, do it. */
28102 if (it->glyph_row)
28104 if (stretched_p)
28106 /* Translate a space with a `space-width' property
28107 into a stretch glyph. */
28108 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28109 / FONT_HEIGHT (font));
28110 append_stretch_glyph (it, it->object, it->pixel_width,
28111 it->ascent + it->descent, ascent);
28113 else
28114 append_glyph (it);
28116 /* If characters with lbearing or rbearing are displayed
28117 in this line, record that fact in a flag of the
28118 glyph row. This is used to optimize X output code. */
28119 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28120 it->glyph_row->contains_overlapping_glyphs_p = true;
28122 if (! stretched_p && it->pixel_width == 0)
28123 /* We assure that all visible glyphs have at least 1-pixel
28124 width. */
28125 it->pixel_width = 1;
28127 else if (it->char_to_display == '\n')
28129 /* A newline has no width, but we need the height of the
28130 line. But if previous part of the line sets a height,
28131 don't increase that height. */
28133 Lisp_Object height;
28134 Lisp_Object total_height = Qnil;
28136 it->override_ascent = -1;
28137 it->pixel_width = 0;
28138 it->nglyphs = 0;
28140 height = get_it_property (it, Qline_height);
28141 /* Split (line-height total-height) list. */
28142 if (CONSP (height)
28143 && CONSP (XCDR (height))
28144 && NILP (XCDR (XCDR (height))))
28146 total_height = XCAR (XCDR (height));
28147 height = XCAR (height);
28149 height = calc_line_height_property (it, height, font, boff, true);
28151 if (it->override_ascent >= 0)
28153 it->ascent = it->override_ascent;
28154 it->descent = it->override_descent;
28155 boff = it->override_boff;
28157 else
28159 if (FONT_TOO_HIGH (font))
28161 it->ascent = font->pixel_size + boff - 1;
28162 it->descent = -boff + 1;
28163 if (it->descent < 0)
28164 it->descent = 0;
28166 else
28168 it->ascent = FONT_BASE (font) + boff;
28169 it->descent = FONT_DESCENT (font) - boff;
28173 if (EQ (height, Qt))
28175 if (it->descent > it->max_descent)
28177 it->ascent += it->descent - it->max_descent;
28178 it->descent = it->max_descent;
28180 if (it->ascent > it->max_ascent)
28182 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28183 it->ascent = it->max_ascent;
28185 it->phys_ascent = min (it->phys_ascent, it->ascent);
28186 it->phys_descent = min (it->phys_descent, it->descent);
28187 it->constrain_row_ascent_descent_p = true;
28188 extra_line_spacing = 0;
28190 else
28192 Lisp_Object spacing;
28194 it->phys_ascent = it->ascent;
28195 it->phys_descent = it->descent;
28197 if ((it->max_ascent > 0 || it->max_descent > 0)
28198 && face->box != FACE_NO_BOX
28199 && face->box_line_width > 0)
28201 it->ascent += face->box_line_width;
28202 it->descent += face->box_line_width;
28204 if (!NILP (height)
28205 && XINT (height) > it->ascent + it->descent)
28206 it->ascent = XINT (height) - it->descent;
28208 if (!NILP (total_height))
28209 spacing = calc_line_height_property (it, total_height, font,
28210 boff, false);
28211 else
28213 spacing = get_it_property (it, Qline_spacing);
28214 spacing = calc_line_height_property (it, spacing, font,
28215 boff, false);
28217 if (INTEGERP (spacing))
28219 extra_line_spacing = XINT (spacing);
28220 if (!NILP (total_height))
28221 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28225 else /* i.e. (it->char_to_display == '\t') */
28227 if (font->space_width > 0)
28229 int tab_width = it->tab_width * font->space_width;
28230 int x = it->current_x + it->continuation_lines_width;
28231 int x0 = x;
28232 /* Adjust for line numbers, if needed. */
28233 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28234 x -= it->lnum_pixel_width;
28235 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28237 /* If the distance from the current position to the next tab
28238 stop is less than a space character width, use the
28239 tab stop after that. */
28240 if (next_tab_x - x < font->space_width)
28241 next_tab_x += tab_width;
28242 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28243 next_tab_x += (it->lnum_pixel_width
28244 - ((it->w->hscroll * font->space_width)
28245 % tab_width));
28247 it->pixel_width = next_tab_x - x0;
28248 it->nglyphs = 1;
28249 if (FONT_TOO_HIGH (font))
28251 if (get_char_glyph_code (' ', font, &char2b))
28253 pcm = get_per_char_metric (font, &char2b);
28254 if (pcm->width == 0
28255 && pcm->rbearing == 0 && pcm->lbearing == 0)
28256 pcm = NULL;
28259 if (pcm)
28261 it->ascent = pcm->ascent + boff;
28262 it->descent = pcm->descent - boff;
28264 else
28266 it->ascent = font->pixel_size + boff - 1;
28267 it->descent = -boff + 1;
28269 if (it->ascent < 0)
28270 it->ascent = 0;
28271 if (it->descent < 0)
28272 it->descent = 0;
28274 else
28276 it->ascent = FONT_BASE (font) + boff;
28277 it->descent = FONT_DESCENT (font) - boff;
28279 it->phys_ascent = it->ascent;
28280 it->phys_descent = it->descent;
28282 if (it->glyph_row)
28284 append_stretch_glyph (it, it->object, it->pixel_width,
28285 it->ascent + it->descent, it->ascent);
28288 else
28290 it->pixel_width = 0;
28291 it->nglyphs = 1;
28295 if (FONT_TOO_HIGH (font))
28297 int font_ascent, font_descent;
28299 /* For very large fonts, where we ignore the declared font
28300 dimensions, and go by per-character metrics instead,
28301 don't let the row ascent and descent values (and the row
28302 height computed from them) be smaller than the "normal"
28303 character metrics. This avoids unpleasant effects
28304 whereby lines on display would change their height
28305 depending on which characters are shown. */
28306 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28307 it->max_ascent = max (it->max_ascent, font_ascent);
28308 it->max_descent = max (it->max_descent, font_descent);
28311 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28313 /* A static composition.
28315 Note: A composition is represented as one glyph in the
28316 glyph matrix. There are no padding glyphs.
28318 Important note: pixel_width, ascent, and descent are the
28319 values of what is drawn by draw_glyphs (i.e. the values of
28320 the overall glyphs composed). */
28321 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28322 int boff; /* baseline offset */
28323 struct composition *cmp = composition_table[it->cmp_it.id];
28324 int glyph_len = cmp->glyph_len;
28325 struct font *font = face->font;
28327 it->nglyphs = 1;
28329 /* If we have not yet calculated pixel size data of glyphs of
28330 the composition for the current face font, calculate them
28331 now. Theoretically, we have to check all fonts for the
28332 glyphs, but that requires much time and memory space. So,
28333 here we check only the font of the first glyph. This may
28334 lead to incorrect display, but it's very rare, and C-l
28335 (recenter-top-bottom) can correct the display anyway. */
28336 if (! cmp->font || cmp->font != font)
28338 /* Ascent and descent of the font of the first character
28339 of this composition (adjusted by baseline offset).
28340 Ascent and descent of overall glyphs should not be less
28341 than these, respectively. */
28342 int font_ascent, font_descent, font_height;
28343 /* Bounding box of the overall glyphs. */
28344 int leftmost, rightmost, lowest, highest;
28345 int lbearing, rbearing;
28346 int i, width, ascent, descent;
28347 int c;
28348 XChar2b char2b;
28349 struct font_metrics *pcm;
28350 ptrdiff_t pos;
28352 eassume (0 < glyph_len); /* See Bug#8512. */
28354 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28355 while (c == '\t' && 0 < --glyph_len);
28357 bool right_padded = glyph_len < cmp->glyph_len;
28358 for (i = 0; i < glyph_len; i++)
28360 c = COMPOSITION_GLYPH (cmp, i);
28361 if (c != '\t')
28362 break;
28363 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28365 bool left_padded = i > 0;
28367 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28368 : IT_CHARPOS (*it));
28369 /* If no suitable font is found, use the default font. */
28370 bool font_not_found_p = font == NULL;
28371 if (font_not_found_p)
28373 face = face->ascii_face;
28374 font = face->font;
28376 boff = font->baseline_offset;
28377 if (font->vertical_centering)
28378 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28379 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28380 font_ascent += boff;
28381 font_descent -= boff;
28382 font_height = font_ascent + font_descent;
28384 cmp->font = font;
28386 pcm = NULL;
28387 if (! font_not_found_p)
28389 get_char_face_and_encoding (it->f, c, it->face_id,
28390 &char2b, false);
28391 pcm = get_per_char_metric (font, &char2b);
28394 /* Initialize the bounding box. */
28395 if (pcm)
28397 width = cmp->glyph_len > 0 ? pcm->width : 0;
28398 ascent = pcm->ascent;
28399 descent = pcm->descent;
28400 lbearing = pcm->lbearing;
28401 rbearing = pcm->rbearing;
28403 else
28405 width = cmp->glyph_len > 0 ? font->space_width : 0;
28406 ascent = FONT_BASE (font);
28407 descent = FONT_DESCENT (font);
28408 lbearing = 0;
28409 rbearing = width;
28412 rightmost = width;
28413 leftmost = 0;
28414 lowest = - descent + boff;
28415 highest = ascent + boff;
28417 if (! font_not_found_p
28418 && font->default_ascent
28419 && CHAR_TABLE_P (Vuse_default_ascent)
28420 && !NILP (Faref (Vuse_default_ascent,
28421 make_number (it->char_to_display))))
28422 highest = font->default_ascent + boff;
28424 /* Draw the first glyph at the normal position. It may be
28425 shifted to right later if some other glyphs are drawn
28426 at the left. */
28427 cmp->offsets[i * 2] = 0;
28428 cmp->offsets[i * 2 + 1] = boff;
28429 cmp->lbearing = lbearing;
28430 cmp->rbearing = rbearing;
28432 /* Set cmp->offsets for the remaining glyphs. */
28433 for (i++; i < glyph_len; i++)
28435 int left, right, btm, top;
28436 int ch = COMPOSITION_GLYPH (cmp, i);
28437 int face_id;
28438 struct face *this_face;
28440 if (ch == '\t')
28441 ch = ' ';
28442 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28443 this_face = FACE_FROM_ID (it->f, face_id);
28444 font = this_face->font;
28446 if (font == NULL)
28447 pcm = NULL;
28448 else
28450 get_char_face_and_encoding (it->f, ch, face_id,
28451 &char2b, false);
28452 pcm = get_per_char_metric (font, &char2b);
28454 if (! pcm)
28455 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28456 else
28458 width = pcm->width;
28459 ascent = pcm->ascent;
28460 descent = pcm->descent;
28461 lbearing = pcm->lbearing;
28462 rbearing = pcm->rbearing;
28463 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28465 /* Relative composition with or without
28466 alternate chars. */
28467 left = (leftmost + rightmost - width) / 2;
28468 btm = - descent + boff;
28469 if (font->relative_compose
28470 && (! CHAR_TABLE_P (Vignore_relative_composition)
28471 || NILP (Faref (Vignore_relative_composition,
28472 make_number (ch)))))
28475 if (- descent >= font->relative_compose)
28476 /* One extra pixel between two glyphs. */
28477 btm = highest + 1;
28478 else if (ascent <= 0)
28479 /* One extra pixel between two glyphs. */
28480 btm = lowest - 1 - ascent - descent;
28483 else
28485 /* A composition rule is specified by an integer
28486 value that encodes global and new reference
28487 points (GREF and NREF). GREF and NREF are
28488 specified by numbers as below:
28490 0---1---2 -- ascent
28494 9--10--11 -- center
28496 ---3---4---5--- baseline
28498 6---7---8 -- descent
28500 int rule = COMPOSITION_RULE (cmp, i);
28501 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28503 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28504 grefx = gref % 3, nrefx = nref % 3;
28505 grefy = gref / 3, nrefy = nref / 3;
28506 if (xoff)
28507 xoff = font_height * (xoff - 128) / 256;
28508 if (yoff)
28509 yoff = font_height * (yoff - 128) / 256;
28511 left = (leftmost
28512 + grefx * (rightmost - leftmost) / 2
28513 - nrefx * width / 2
28514 + xoff);
28516 btm = ((grefy == 0 ? highest
28517 : grefy == 1 ? 0
28518 : grefy == 2 ? lowest
28519 : (highest + lowest) / 2)
28520 - (nrefy == 0 ? ascent + descent
28521 : nrefy == 1 ? descent - boff
28522 : nrefy == 2 ? 0
28523 : (ascent + descent) / 2)
28524 + yoff);
28527 cmp->offsets[i * 2] = left;
28528 cmp->offsets[i * 2 + 1] = btm + descent;
28530 /* Update the bounding box of the overall glyphs. */
28531 if (width > 0)
28533 right = left + width;
28534 if (left < leftmost)
28535 leftmost = left;
28536 if (right > rightmost)
28537 rightmost = right;
28539 top = btm + descent + ascent;
28540 if (top > highest)
28541 highest = top;
28542 if (btm < lowest)
28543 lowest = btm;
28545 if (cmp->lbearing > left + lbearing)
28546 cmp->lbearing = left + lbearing;
28547 if (cmp->rbearing < left + rbearing)
28548 cmp->rbearing = left + rbearing;
28552 /* If there are glyphs whose x-offsets are negative,
28553 shift all glyphs to the right and make all x-offsets
28554 non-negative. */
28555 if (leftmost < 0)
28557 for (i = 0; i < cmp->glyph_len; i++)
28558 cmp->offsets[i * 2] -= leftmost;
28559 rightmost -= leftmost;
28560 cmp->lbearing -= leftmost;
28561 cmp->rbearing -= leftmost;
28564 if (left_padded && cmp->lbearing < 0)
28566 for (i = 0; i < cmp->glyph_len; i++)
28567 cmp->offsets[i * 2] -= cmp->lbearing;
28568 rightmost -= cmp->lbearing;
28569 cmp->rbearing -= cmp->lbearing;
28570 cmp->lbearing = 0;
28572 if (right_padded && rightmost < cmp->rbearing)
28574 rightmost = cmp->rbearing;
28577 cmp->pixel_width = rightmost;
28578 cmp->ascent = highest;
28579 cmp->descent = - lowest;
28580 if (cmp->ascent < font_ascent)
28581 cmp->ascent = font_ascent;
28582 if (cmp->descent < font_descent)
28583 cmp->descent = font_descent;
28586 if (it->glyph_row
28587 && (cmp->lbearing < 0
28588 || cmp->rbearing > cmp->pixel_width))
28589 it->glyph_row->contains_overlapping_glyphs_p = true;
28591 it->pixel_width = cmp->pixel_width;
28592 it->ascent = it->phys_ascent = cmp->ascent;
28593 it->descent = it->phys_descent = cmp->descent;
28594 if (face->box != FACE_NO_BOX)
28596 int thick = face->box_line_width;
28598 if (thick > 0)
28600 it->ascent += thick;
28601 it->descent += thick;
28603 else
28604 thick = - thick;
28606 if (it->start_of_box_run_p)
28607 it->pixel_width += thick;
28608 if (it->end_of_box_run_p)
28609 it->pixel_width += thick;
28612 /* If face has an overline, add the height of the overline
28613 (1 pixel) and a 1 pixel margin to the character height. */
28614 if (face->overline_p)
28615 it->ascent += overline_margin;
28617 take_vertical_position_into_account (it);
28618 if (it->ascent < 0)
28619 it->ascent = 0;
28620 if (it->descent < 0)
28621 it->descent = 0;
28623 if (it->glyph_row && cmp->glyph_len > 0)
28624 append_composite_glyph (it);
28626 else if (it->what == IT_COMPOSITION)
28628 /* A dynamic (automatic) composition. */
28629 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28630 Lisp_Object gstring;
28631 struct font_metrics metrics;
28633 it->nglyphs = 1;
28635 gstring = composition_gstring_from_id (it->cmp_it.id);
28636 it->pixel_width
28637 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28638 &metrics);
28639 if (it->glyph_row
28640 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28641 it->glyph_row->contains_overlapping_glyphs_p = true;
28642 it->ascent = it->phys_ascent = metrics.ascent;
28643 it->descent = it->phys_descent = metrics.descent;
28644 if (face->box != FACE_NO_BOX)
28646 int thick = face->box_line_width;
28648 if (thick > 0)
28650 it->ascent += thick;
28651 it->descent += thick;
28653 else
28654 thick = - thick;
28656 if (it->start_of_box_run_p)
28657 it->pixel_width += thick;
28658 if (it->end_of_box_run_p)
28659 it->pixel_width += thick;
28661 /* If face has an overline, add the height of the overline
28662 (1 pixel) and a 1 pixel margin to the character height. */
28663 if (face->overline_p)
28664 it->ascent += overline_margin;
28665 take_vertical_position_into_account (it);
28666 if (it->ascent < 0)
28667 it->ascent = 0;
28668 if (it->descent < 0)
28669 it->descent = 0;
28671 if (it->glyph_row)
28672 append_composite_glyph (it);
28674 else if (it->what == IT_GLYPHLESS)
28675 produce_glyphless_glyph (it, false, Qnil);
28676 else if (it->what == IT_IMAGE)
28677 produce_image_glyph (it);
28678 else if (it->what == IT_STRETCH)
28679 produce_stretch_glyph (it);
28680 else if (it->what == IT_XWIDGET)
28681 produce_xwidget_glyph (it);
28683 done:
28684 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28685 because this isn't true for images with `:ascent 100'. */
28686 eassert (it->ascent >= 0 && it->descent >= 0);
28687 if (it->area == TEXT_AREA)
28688 it->current_x += it->pixel_width;
28690 if (extra_line_spacing > 0)
28692 it->descent += extra_line_spacing;
28693 if (extra_line_spacing > it->max_extra_line_spacing)
28694 it->max_extra_line_spacing = extra_line_spacing;
28697 it->max_ascent = max (it->max_ascent, it->ascent);
28698 it->max_descent = max (it->max_descent, it->descent);
28699 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28700 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28703 /* EXPORT for RIF:
28704 Output LEN glyphs starting at START at the nominal cursor position.
28705 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28706 being updated, and UPDATED_AREA is the area of that row being updated. */
28708 void
28709 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28710 struct glyph *start, enum glyph_row_area updated_area, int len)
28712 int x, hpos, chpos = w->phys_cursor.hpos;
28714 eassert (updated_row);
28715 /* When the window is hscrolled, cursor hpos can legitimately be out
28716 of bounds, but we draw the cursor at the corresponding window
28717 margin in that case. */
28718 if (!updated_row->reversed_p && chpos < 0)
28719 chpos = 0;
28720 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28721 chpos = updated_row->used[TEXT_AREA] - 1;
28723 block_input ();
28725 /* Write glyphs. */
28727 hpos = start - updated_row->glyphs[updated_area];
28728 x = draw_glyphs (w, w->output_cursor.x,
28729 updated_row, updated_area,
28730 hpos, hpos + len,
28731 DRAW_NORMAL_TEXT, 0);
28733 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28734 if (updated_area == TEXT_AREA
28735 && w->phys_cursor_on_p
28736 && w->phys_cursor.vpos == w->output_cursor.vpos
28737 && chpos >= hpos
28738 && chpos < hpos + len)
28739 w->phys_cursor_on_p = false;
28741 unblock_input ();
28743 /* Advance the output cursor. */
28744 w->output_cursor.hpos += len;
28745 w->output_cursor.x = x;
28749 /* EXPORT for RIF:
28750 Insert LEN glyphs from START at the nominal cursor position. */
28752 void
28753 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28754 struct glyph *start, enum glyph_row_area updated_area, int len)
28756 struct frame *f;
28757 int line_height, shift_by_width, shifted_region_width;
28758 struct glyph_row *row;
28759 struct glyph *glyph;
28760 int frame_x, frame_y;
28761 ptrdiff_t hpos;
28763 eassert (updated_row);
28764 block_input ();
28765 f = XFRAME (WINDOW_FRAME (w));
28767 /* Get the height of the line we are in. */
28768 row = updated_row;
28769 line_height = row->height;
28771 /* Get the width of the glyphs to insert. */
28772 shift_by_width = 0;
28773 for (glyph = start; glyph < start + len; ++glyph)
28774 shift_by_width += glyph->pixel_width;
28776 /* Get the width of the region to shift right. */
28777 shifted_region_width = (window_box_width (w, updated_area)
28778 - w->output_cursor.x
28779 - shift_by_width);
28781 /* Shift right. */
28782 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28783 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28785 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28786 line_height, shift_by_width);
28788 /* Write the glyphs. */
28789 hpos = start - row->glyphs[updated_area];
28790 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28791 hpos, hpos + len,
28792 DRAW_NORMAL_TEXT, 0);
28794 /* Advance the output cursor. */
28795 w->output_cursor.hpos += len;
28796 w->output_cursor.x += shift_by_width;
28797 unblock_input ();
28801 /* EXPORT for RIF:
28802 Erase the current text line from the nominal cursor position
28803 (inclusive) to pixel column TO_X (exclusive). The idea is that
28804 everything from TO_X onward is already erased.
28806 TO_X is a pixel position relative to UPDATED_AREA of currently
28807 updated window W. TO_X == -1 means clear to the end of this area. */
28809 void
28810 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28811 enum glyph_row_area updated_area, int to_x)
28813 struct frame *f;
28814 int max_x, min_y, max_y;
28815 int from_x, from_y, to_y;
28817 eassert (updated_row);
28818 f = XFRAME (w->frame);
28820 if (updated_row->full_width_p)
28821 max_x = (WINDOW_PIXEL_WIDTH (w)
28822 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28823 else
28824 max_x = window_box_width (w, updated_area);
28825 max_y = window_text_bottom_y (w);
28827 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28828 of window. For TO_X > 0, truncate to end of drawing area. */
28829 if (to_x == 0)
28830 return;
28831 else if (to_x < 0)
28832 to_x = max_x;
28833 else
28834 to_x = min (to_x, max_x);
28836 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28838 /* Notice if the cursor will be cleared by this operation. */
28839 if (!updated_row->full_width_p)
28840 notice_overwritten_cursor (w, updated_area,
28841 w->output_cursor.x, -1,
28842 updated_row->y,
28843 MATRIX_ROW_BOTTOM_Y (updated_row));
28845 from_x = w->output_cursor.x;
28847 /* Translate to frame coordinates. */
28848 if (updated_row->full_width_p)
28850 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28851 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28853 else
28855 int area_left = window_box_left (w, updated_area);
28856 from_x += area_left;
28857 to_x += area_left;
28860 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28861 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28862 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28864 /* Prevent inadvertently clearing to end of the X window. */
28865 if (to_x > from_x && to_y > from_y)
28867 block_input ();
28868 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28869 to_x - from_x, to_y - from_y);
28870 unblock_input ();
28874 #endif /* HAVE_WINDOW_SYSTEM */
28878 /***********************************************************************
28879 Cursor types
28880 ***********************************************************************/
28882 /* Value is the internal representation of the specified cursor type
28883 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28884 of the bar cursor. */
28886 static enum text_cursor_kinds
28887 get_specified_cursor_type (Lisp_Object arg, int *width)
28889 enum text_cursor_kinds type;
28891 if (NILP (arg))
28892 return NO_CURSOR;
28894 if (EQ (arg, Qbox))
28895 return FILLED_BOX_CURSOR;
28897 if (EQ (arg, Qhollow))
28898 return HOLLOW_BOX_CURSOR;
28900 if (EQ (arg, Qbar))
28902 *width = 2;
28903 return BAR_CURSOR;
28906 if (CONSP (arg)
28907 && EQ (XCAR (arg), Qbar)
28908 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28910 *width = XINT (XCDR (arg));
28911 return BAR_CURSOR;
28914 if (EQ (arg, Qhbar))
28916 *width = 2;
28917 return HBAR_CURSOR;
28920 if (CONSP (arg)
28921 && EQ (XCAR (arg), Qhbar)
28922 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28924 *width = XINT (XCDR (arg));
28925 return HBAR_CURSOR;
28928 /* Treat anything unknown as "hollow box cursor".
28929 It was bad to signal an error; people have trouble fixing
28930 .Xdefaults with Emacs, when it has something bad in it. */
28931 type = HOLLOW_BOX_CURSOR;
28933 return type;
28936 /* Set the default cursor types for specified frame. */
28937 void
28938 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28940 int width = 1;
28941 Lisp_Object tem;
28943 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28944 FRAME_CURSOR_WIDTH (f) = width;
28946 /* By default, set up the blink-off state depending on the on-state. */
28948 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28949 if (!NILP (tem))
28951 FRAME_BLINK_OFF_CURSOR (f)
28952 = get_specified_cursor_type (XCDR (tem), &width);
28953 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28955 else
28956 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28958 /* Make sure the cursor gets redrawn. */
28959 f->cursor_type_changed = true;
28963 #ifdef HAVE_WINDOW_SYSTEM
28965 /* Return the cursor we want to be displayed in window W. Return
28966 width of bar/hbar cursor through WIDTH arg. Return with
28967 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28968 (i.e. if the `system caret' should track this cursor).
28970 In a mini-buffer window, we want the cursor only to appear if we
28971 are reading input from this window. For the selected window, we
28972 want the cursor type given by the frame parameter or buffer local
28973 setting of cursor-type. If explicitly marked off, draw no cursor.
28974 In all other cases, we want a hollow box cursor. */
28976 static enum text_cursor_kinds
28977 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28978 bool *active_cursor)
28980 struct frame *f = XFRAME (w->frame);
28981 struct buffer *b = XBUFFER (w->contents);
28982 int cursor_type = DEFAULT_CURSOR;
28983 Lisp_Object alt_cursor;
28984 bool non_selected = false;
28986 *active_cursor = true;
28988 /* Echo area */
28989 if (cursor_in_echo_area
28990 && FRAME_HAS_MINIBUF_P (f)
28991 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28993 if (w == XWINDOW (echo_area_window))
28995 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28997 *width = FRAME_CURSOR_WIDTH (f);
28998 return FRAME_DESIRED_CURSOR (f);
29000 else
29001 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29004 *active_cursor = false;
29005 non_selected = true;
29008 /* Detect a nonselected window or nonselected frame. */
29009 else if (w != XWINDOW (f->selected_window)
29010 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29012 *active_cursor = false;
29014 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29015 return NO_CURSOR;
29017 non_selected = true;
29020 /* Never display a cursor in a window in which cursor-type is nil. */
29021 if (NILP (BVAR (b, cursor_type)))
29022 return NO_CURSOR;
29024 /* Get the normal cursor type for this window. */
29025 if (EQ (BVAR (b, cursor_type), Qt))
29027 cursor_type = FRAME_DESIRED_CURSOR (f);
29028 *width = FRAME_CURSOR_WIDTH (f);
29030 else
29031 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29033 /* Use cursor-in-non-selected-windows instead
29034 for non-selected window or frame. */
29035 if (non_selected)
29037 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29038 if (!EQ (Qt, alt_cursor))
29039 return get_specified_cursor_type (alt_cursor, width);
29040 /* t means modify the normal cursor type. */
29041 if (cursor_type == FILLED_BOX_CURSOR)
29042 cursor_type = HOLLOW_BOX_CURSOR;
29043 else if (cursor_type == BAR_CURSOR && *width > 1)
29044 --*width;
29045 return cursor_type;
29048 /* Use normal cursor if not blinked off. */
29049 if (!w->cursor_off_p)
29051 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29052 return NO_CURSOR;
29053 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29055 if (cursor_type == FILLED_BOX_CURSOR)
29057 /* Using a block cursor on large images can be very annoying.
29058 So use a hollow cursor for "large" images.
29059 If image is not transparent (no mask), also use hollow cursor. */
29060 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29061 if (img != NULL && IMAGEP (img->spec))
29063 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29064 where N = size of default frame font size.
29065 This should cover most of the "tiny" icons people may use. */
29066 if (!img->mask
29067 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29068 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29069 cursor_type = HOLLOW_BOX_CURSOR;
29072 else if (cursor_type != NO_CURSOR)
29074 /* Display current only supports BOX and HOLLOW cursors for images.
29075 So for now, unconditionally use a HOLLOW cursor when cursor is
29076 not a solid box cursor. */
29077 cursor_type = HOLLOW_BOX_CURSOR;
29080 return cursor_type;
29083 /* Cursor is blinked off, so determine how to "toggle" it. */
29085 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29086 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29087 return get_specified_cursor_type (XCDR (alt_cursor), width);
29089 /* Then see if frame has specified a specific blink off cursor type. */
29090 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29092 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29093 return FRAME_BLINK_OFF_CURSOR (f);
29096 #if false
29097 /* Some people liked having a permanently visible blinking cursor,
29098 while others had very strong opinions against it. So it was
29099 decided to remove it. KFS 2003-09-03 */
29101 /* Finally perform built-in cursor blinking:
29102 filled box <-> hollow box
29103 wide [h]bar <-> narrow [h]bar
29104 narrow [h]bar <-> no cursor
29105 other type <-> no cursor */
29107 if (cursor_type == FILLED_BOX_CURSOR)
29108 return HOLLOW_BOX_CURSOR;
29110 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29112 *width = 1;
29113 return cursor_type;
29115 #endif
29117 return NO_CURSOR;
29121 /* Notice when the text cursor of window W has been completely
29122 overwritten by a drawing operation that outputs glyphs in AREA
29123 starting at X0 and ending at X1 in the line starting at Y0 and
29124 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29125 the rest of the line after X0 has been written. Y coordinates
29126 are window-relative. */
29128 static void
29129 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29130 int x0, int x1, int y0, int y1)
29132 int cx0, cx1, cy0, cy1;
29133 struct glyph_row *row;
29135 if (!w->phys_cursor_on_p)
29136 return;
29137 if (area != TEXT_AREA)
29138 return;
29140 if (w->phys_cursor.vpos < 0
29141 || w->phys_cursor.vpos >= w->current_matrix->nrows
29142 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29143 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29144 return;
29146 if (row->cursor_in_fringe_p)
29148 row->cursor_in_fringe_p = false;
29149 draw_fringe_bitmap (w, row, row->reversed_p);
29150 w->phys_cursor_on_p = false;
29151 return;
29154 cx0 = w->phys_cursor.x;
29155 cx1 = cx0 + w->phys_cursor_width;
29156 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29157 return;
29159 /* The cursor image will be completely removed from the
29160 screen if the output area intersects the cursor area in
29161 y-direction. When we draw in [y0 y1[, and some part of
29162 the cursor is at y < y0, that part must have been drawn
29163 before. When scrolling, the cursor is erased before
29164 actually scrolling, so we don't come here. When not
29165 scrolling, the rows above the old cursor row must have
29166 changed, and in this case these rows must have written
29167 over the cursor image.
29169 Likewise if part of the cursor is below y1, with the
29170 exception of the cursor being in the first blank row at
29171 the buffer and window end because update_text_area
29172 doesn't draw that row. (Except when it does, but
29173 that's handled in update_text_area.) */
29175 cy0 = w->phys_cursor.y;
29176 cy1 = cy0 + w->phys_cursor_height;
29177 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29178 return;
29180 w->phys_cursor_on_p = false;
29183 #endif /* HAVE_WINDOW_SYSTEM */
29186 /************************************************************************
29187 Mouse Face
29188 ************************************************************************/
29190 #ifdef HAVE_WINDOW_SYSTEM
29192 /* EXPORT for RIF:
29193 Fix the display of area AREA of overlapping row ROW in window W
29194 with respect to the overlapping part OVERLAPS. */
29196 void
29197 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29198 enum glyph_row_area area, int overlaps)
29200 int i, x;
29202 block_input ();
29204 x = 0;
29205 for (i = 0; i < row->used[area];)
29207 if (row->glyphs[area][i].overlaps_vertically_p)
29209 int start = i, start_x = x;
29213 x += row->glyphs[area][i].pixel_width;
29214 ++i;
29216 while (i < row->used[area]
29217 && row->glyphs[area][i].overlaps_vertically_p);
29219 draw_glyphs (w, start_x, row, area,
29220 start, i,
29221 DRAW_NORMAL_TEXT, overlaps);
29223 else
29225 x += row->glyphs[area][i].pixel_width;
29226 ++i;
29230 unblock_input ();
29234 /* EXPORT:
29235 Draw the cursor glyph of window W in glyph row ROW. See the
29236 comment of draw_glyphs for the meaning of HL. */
29238 void
29239 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29240 enum draw_glyphs_face hl)
29242 /* If cursor hpos is out of bounds, don't draw garbage. This can
29243 happen in mini-buffer windows when switching between echo area
29244 glyphs and mini-buffer. */
29245 if ((row->reversed_p
29246 ? (w->phys_cursor.hpos >= 0)
29247 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29249 bool on_p = w->phys_cursor_on_p;
29250 int x1;
29251 int hpos = w->phys_cursor.hpos;
29253 /* When the window is hscrolled, cursor hpos can legitimately be
29254 out of bounds, but we draw the cursor at the corresponding
29255 window margin in that case. */
29256 if (!row->reversed_p && hpos < 0)
29257 hpos = 0;
29258 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29259 hpos = row->used[TEXT_AREA] - 1;
29261 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29262 hl, 0);
29263 w->phys_cursor_on_p = on_p;
29265 if (hl == DRAW_CURSOR)
29266 w->phys_cursor_width = x1 - w->phys_cursor.x;
29267 /* When we erase the cursor, and ROW is overlapped by other
29268 rows, make sure that these overlapping parts of other rows
29269 are redrawn. */
29270 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29272 w->phys_cursor_width = x1 - w->phys_cursor.x;
29274 if (row > w->current_matrix->rows
29275 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29276 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29277 OVERLAPS_ERASED_CURSOR);
29279 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29280 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29281 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29282 OVERLAPS_ERASED_CURSOR);
29288 /* Erase the image of a cursor of window W from the screen. */
29290 void
29291 erase_phys_cursor (struct window *w)
29293 struct frame *f = XFRAME (w->frame);
29294 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29295 int hpos = w->phys_cursor.hpos;
29296 int vpos = w->phys_cursor.vpos;
29297 bool mouse_face_here_p = false;
29298 struct glyph_matrix *active_glyphs = w->current_matrix;
29299 struct glyph_row *cursor_row;
29300 struct glyph *cursor_glyph;
29301 enum draw_glyphs_face hl;
29303 /* No cursor displayed or row invalidated => nothing to do on the
29304 screen. */
29305 if (w->phys_cursor_type == NO_CURSOR)
29306 goto mark_cursor_off;
29308 /* VPOS >= active_glyphs->nrows means that window has been resized.
29309 Don't bother to erase the cursor. */
29310 if (vpos >= active_glyphs->nrows)
29311 goto mark_cursor_off;
29313 /* If row containing cursor is marked invalid, there is nothing we
29314 can do. */
29315 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29316 if (!cursor_row->enabled_p)
29317 goto mark_cursor_off;
29319 /* If line spacing is > 0, old cursor may only be partially visible in
29320 window after split-window. So adjust visible height. */
29321 cursor_row->visible_height = min (cursor_row->visible_height,
29322 window_text_bottom_y (w) - cursor_row->y);
29324 /* If row is completely invisible, don't attempt to delete a cursor which
29325 isn't there. This can happen if cursor is at top of a window, and
29326 we switch to a buffer with a header line in that window. */
29327 if (cursor_row->visible_height <= 0)
29328 goto mark_cursor_off;
29330 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29331 if (cursor_row->cursor_in_fringe_p)
29333 cursor_row->cursor_in_fringe_p = false;
29334 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29335 goto mark_cursor_off;
29338 /* This can happen when the new row is shorter than the old one.
29339 In this case, either draw_glyphs or clear_end_of_line
29340 should have cleared the cursor. Note that we wouldn't be
29341 able to erase the cursor in this case because we don't have a
29342 cursor glyph at hand. */
29343 if ((cursor_row->reversed_p
29344 ? (w->phys_cursor.hpos < 0)
29345 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29346 goto mark_cursor_off;
29348 /* When the window is hscrolled, cursor hpos can legitimately be out
29349 of bounds, but we draw the cursor at the corresponding window
29350 margin in that case. */
29351 if (!cursor_row->reversed_p && hpos < 0)
29352 hpos = 0;
29353 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29354 hpos = cursor_row->used[TEXT_AREA] - 1;
29356 /* If the cursor is in the mouse face area, redisplay that when
29357 we clear the cursor. */
29358 if (! NILP (hlinfo->mouse_face_window)
29359 && coords_in_mouse_face_p (w, hpos, vpos)
29360 /* Don't redraw the cursor's spot in mouse face if it is at the
29361 end of a line (on a newline). The cursor appears there, but
29362 mouse highlighting does not. */
29363 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29364 mouse_face_here_p = true;
29366 /* Maybe clear the display under the cursor. */
29367 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29369 int x, y;
29370 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29371 int width;
29373 cursor_glyph = get_phys_cursor_glyph (w);
29374 if (cursor_glyph == NULL)
29375 goto mark_cursor_off;
29377 width = cursor_glyph->pixel_width;
29378 x = w->phys_cursor.x;
29379 if (x < 0)
29381 width += x;
29382 x = 0;
29384 width = min (width, window_box_width (w, TEXT_AREA) - x);
29385 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29386 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29388 if (width > 0)
29389 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29392 /* Erase the cursor by redrawing the character underneath it. */
29393 if (mouse_face_here_p)
29394 hl = DRAW_MOUSE_FACE;
29395 else
29396 hl = DRAW_NORMAL_TEXT;
29397 draw_phys_cursor_glyph (w, cursor_row, hl);
29399 mark_cursor_off:
29400 w->phys_cursor_on_p = false;
29401 w->phys_cursor_type = NO_CURSOR;
29405 /* Display or clear cursor of window W. If !ON, clear the cursor.
29406 If ON, display the cursor; where to put the cursor is specified by
29407 HPOS, VPOS, X and Y. */
29409 void
29410 display_and_set_cursor (struct window *w, bool on,
29411 int hpos, int vpos, int x, int y)
29413 struct frame *f = XFRAME (w->frame);
29414 int new_cursor_type;
29415 int new_cursor_width;
29416 bool active_cursor;
29417 struct glyph_row *glyph_row;
29418 struct glyph *glyph;
29420 /* This is pointless on invisible frames, and dangerous on garbaged
29421 windows and frames; in the latter case, the frame or window may
29422 be in the midst of changing its size, and x and y may be off the
29423 window. */
29424 if (! FRAME_VISIBLE_P (f)
29425 || vpos >= w->current_matrix->nrows
29426 || hpos >= w->current_matrix->matrix_w)
29427 return;
29429 /* If cursor is off and we want it off, return quickly. */
29430 if (!on && !w->phys_cursor_on_p)
29431 return;
29433 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29434 /* If cursor row is not enabled, we don't really know where to
29435 display the cursor. */
29436 if (!glyph_row->enabled_p)
29438 w->phys_cursor_on_p = false;
29439 return;
29442 /* A frame might be marked garbaged even though its cursor position
29443 is correct, and will not change upon subsequent redisplay. This
29444 happens in some rare situations, like toggling the sort order in
29445 Dired windows. We've already established that VPOS is valid, so
29446 it shouldn't do any harm to record the cursor position, as we are
29447 going to return without acting on it anyway. Otherwise, expose
29448 events might come in and call update_window_cursor, which will
29449 blindly use outdated values in w->phys_cursor. */
29450 if (FRAME_GARBAGED_P (f))
29452 if (on)
29454 w->phys_cursor.x = x;
29455 w->phys_cursor.y = glyph_row->y;
29456 w->phys_cursor.hpos = hpos;
29457 w->phys_cursor.vpos = vpos;
29459 return;
29462 glyph = NULL;
29463 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29464 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29466 eassert (input_blocked_p ());
29468 /* Set new_cursor_type to the cursor we want to be displayed. */
29469 new_cursor_type = get_window_cursor_type (w, glyph,
29470 &new_cursor_width, &active_cursor);
29472 /* If cursor is currently being shown and we don't want it to be or
29473 it is in the wrong place, or the cursor type is not what we want,
29474 erase it. */
29475 if (w->phys_cursor_on_p
29476 && (!on
29477 || w->phys_cursor.x != x
29478 || w->phys_cursor.y != y
29479 /* HPOS can be negative in R2L rows whose
29480 exact_window_width_line_p flag is set (i.e. their newline
29481 would "overflow into the fringe"). */
29482 || hpos < 0
29483 || new_cursor_type != w->phys_cursor_type
29484 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29485 && new_cursor_width != w->phys_cursor_width)))
29486 erase_phys_cursor (w);
29488 /* Don't check phys_cursor_on_p here because that flag is only set
29489 to false in some cases where we know that the cursor has been
29490 completely erased, to avoid the extra work of erasing the cursor
29491 twice. In other words, phys_cursor_on_p can be true and the cursor
29492 still not be visible, or it has only been partly erased. */
29493 if (on)
29495 w->phys_cursor_ascent = glyph_row->ascent;
29496 w->phys_cursor_height = glyph_row->height;
29498 /* Set phys_cursor_.* before x_draw_.* is called because some
29499 of them may need the information. */
29500 w->phys_cursor.x = x;
29501 w->phys_cursor.y = glyph_row->y;
29502 w->phys_cursor.hpos = hpos;
29503 w->phys_cursor.vpos = vpos;
29506 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29507 new_cursor_type, new_cursor_width,
29508 on, active_cursor);
29512 /* Switch the display of W's cursor on or off, according to the value
29513 of ON. */
29515 static void
29516 update_window_cursor (struct window *w, bool on)
29518 /* Don't update cursor in windows whose frame is in the process
29519 of being deleted. */
29520 if (w->current_matrix)
29522 int hpos = w->phys_cursor.hpos;
29523 int vpos = w->phys_cursor.vpos;
29524 struct glyph_row *row;
29526 if (vpos >= w->current_matrix->nrows
29527 || hpos >= w->current_matrix->matrix_w)
29528 return;
29530 row = MATRIX_ROW (w->current_matrix, vpos);
29532 /* When the window is hscrolled, cursor hpos can legitimately be
29533 out of bounds, but we draw the cursor at the corresponding
29534 window margin in that case. */
29535 if (!row->reversed_p && hpos < 0)
29536 hpos = 0;
29537 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29538 hpos = row->used[TEXT_AREA] - 1;
29540 block_input ();
29541 display_and_set_cursor (w, on, hpos, vpos,
29542 w->phys_cursor.x, w->phys_cursor.y);
29543 unblock_input ();
29548 /* Call update_window_cursor with parameter ON_P on all leaf windows
29549 in the window tree rooted at W. */
29551 static void
29552 update_cursor_in_window_tree (struct window *w, bool on_p)
29554 while (w)
29556 if (WINDOWP (w->contents))
29557 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29558 else
29559 update_window_cursor (w, on_p);
29561 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29566 /* EXPORT:
29567 Display the cursor on window W, or clear it, according to ON_P.
29568 Don't change the cursor's position. */
29570 void
29571 x_update_cursor (struct frame *f, bool on_p)
29573 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29577 /* EXPORT:
29578 Clear the cursor of window W to background color, and mark the
29579 cursor as not shown. This is used when the text where the cursor
29580 is about to be rewritten. */
29582 void
29583 x_clear_cursor (struct window *w)
29585 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29586 update_window_cursor (w, false);
29589 #endif /* HAVE_WINDOW_SYSTEM */
29591 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29592 and MSDOS. */
29593 static void
29594 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29595 int start_hpos, int end_hpos,
29596 enum draw_glyphs_face draw)
29598 #ifdef HAVE_WINDOW_SYSTEM
29599 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29601 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29602 return;
29604 #endif
29605 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29606 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29607 #endif
29610 /* Display the active region described by mouse_face_* according to DRAW. */
29612 static void
29613 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29615 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29616 struct frame *f = XFRAME (WINDOW_FRAME (w));
29618 if (/* If window is in the process of being destroyed, don't bother
29619 to do anything. */
29620 w->current_matrix != NULL
29621 /* Don't update mouse highlight if hidden. */
29622 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29623 /* Recognize when we are called to operate on rows that don't exist
29624 anymore. This can happen when a window is split. */
29625 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29627 bool phys_cursor_on_p = w->phys_cursor_on_p;
29628 struct glyph_row *row, *first, *last;
29630 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29631 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29633 for (row = first; row <= last && row->enabled_p; ++row)
29635 int start_hpos, end_hpos, start_x;
29637 /* For all but the first row, the highlight starts at column 0. */
29638 if (row == first)
29640 /* R2L rows have BEG and END in reversed order, but the
29641 screen drawing geometry is always left to right. So
29642 we need to mirror the beginning and end of the
29643 highlighted area in R2L rows. */
29644 if (!row->reversed_p)
29646 start_hpos = hlinfo->mouse_face_beg_col;
29647 start_x = hlinfo->mouse_face_beg_x;
29649 else if (row == last)
29651 start_hpos = hlinfo->mouse_face_end_col;
29652 start_x = hlinfo->mouse_face_end_x;
29654 else
29656 start_hpos = 0;
29657 start_x = 0;
29660 else if (row->reversed_p && row == last)
29662 start_hpos = hlinfo->mouse_face_end_col;
29663 start_x = hlinfo->mouse_face_end_x;
29665 else
29667 start_hpos = 0;
29668 start_x = 0;
29671 if (row == last)
29673 if (!row->reversed_p)
29674 end_hpos = hlinfo->mouse_face_end_col;
29675 else if (row == first)
29676 end_hpos = hlinfo->mouse_face_beg_col;
29677 else
29679 end_hpos = row->used[TEXT_AREA];
29680 if (draw == DRAW_NORMAL_TEXT)
29681 row->fill_line_p = true; /* Clear to end of line. */
29684 else if (row->reversed_p && row == first)
29685 end_hpos = hlinfo->mouse_face_beg_col;
29686 else
29688 end_hpos = row->used[TEXT_AREA];
29689 if (draw == DRAW_NORMAL_TEXT)
29690 row->fill_line_p = true; /* Clear to end of line. */
29693 if (end_hpos > start_hpos)
29695 draw_row_with_mouse_face (w, start_x, row,
29696 start_hpos, end_hpos, draw);
29698 row->mouse_face_p
29699 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29703 /* When we've written over the cursor, arrange for it to
29704 be displayed again. */
29705 if (FRAME_WINDOW_P (f)
29706 && phys_cursor_on_p && !w->phys_cursor_on_p)
29708 #ifdef HAVE_WINDOW_SYSTEM
29709 int hpos = w->phys_cursor.hpos;
29711 /* When the window is hscrolled, cursor hpos can legitimately be
29712 out of bounds, but we draw the cursor at the corresponding
29713 window margin in that case. */
29714 if (!row->reversed_p && hpos < 0)
29715 hpos = 0;
29716 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29717 hpos = row->used[TEXT_AREA] - 1;
29719 block_input ();
29720 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29721 w->phys_cursor.x, w->phys_cursor.y);
29722 unblock_input ();
29723 #endif /* HAVE_WINDOW_SYSTEM */
29727 #ifdef HAVE_WINDOW_SYSTEM
29728 /* Change the mouse cursor. */
29729 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29731 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29732 if (draw == DRAW_NORMAL_TEXT
29733 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29734 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29735 else
29736 #endif
29737 if (draw == DRAW_MOUSE_FACE)
29738 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29739 else
29740 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29742 #endif /* HAVE_WINDOW_SYSTEM */
29745 /* EXPORT:
29746 Clear out the mouse-highlighted active region.
29747 Redraw it un-highlighted first. Value is true if mouse
29748 face was actually drawn unhighlighted. */
29750 bool
29751 clear_mouse_face (Mouse_HLInfo *hlinfo)
29753 bool cleared
29754 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29755 if (cleared)
29756 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29757 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29758 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29759 hlinfo->mouse_face_window = Qnil;
29760 hlinfo->mouse_face_overlay = Qnil;
29761 return cleared;
29764 /* Return true if the coordinates HPOS and VPOS on windows W are
29765 within the mouse face on that window. */
29766 static bool
29767 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29769 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29771 /* Quickly resolve the easy cases. */
29772 if (!(WINDOWP (hlinfo->mouse_face_window)
29773 && XWINDOW (hlinfo->mouse_face_window) == w))
29774 return false;
29775 if (vpos < hlinfo->mouse_face_beg_row
29776 || vpos > hlinfo->mouse_face_end_row)
29777 return false;
29778 if (vpos > hlinfo->mouse_face_beg_row
29779 && vpos < hlinfo->mouse_face_end_row)
29780 return true;
29782 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29784 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29786 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29787 return true;
29789 else if ((vpos == hlinfo->mouse_face_beg_row
29790 && hpos >= hlinfo->mouse_face_beg_col)
29791 || (vpos == hlinfo->mouse_face_end_row
29792 && hpos < hlinfo->mouse_face_end_col))
29793 return true;
29795 else
29797 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29799 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29800 return true;
29802 else if ((vpos == hlinfo->mouse_face_beg_row
29803 && hpos <= hlinfo->mouse_face_beg_col)
29804 || (vpos == hlinfo->mouse_face_end_row
29805 && hpos > hlinfo->mouse_face_end_col))
29806 return true;
29808 return false;
29812 /* EXPORT:
29813 True if physical cursor of window W is within mouse face. */
29815 bool
29816 cursor_in_mouse_face_p (struct window *w)
29818 int hpos = w->phys_cursor.hpos;
29819 int vpos = w->phys_cursor.vpos;
29820 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29822 /* When the window is hscrolled, cursor hpos can legitimately be out
29823 of bounds, but we draw the cursor at the corresponding window
29824 margin in that case. */
29825 if (!row->reversed_p && hpos < 0)
29826 hpos = 0;
29827 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29828 hpos = row->used[TEXT_AREA] - 1;
29830 return coords_in_mouse_face_p (w, hpos, vpos);
29835 /* Find the glyph rows START_ROW and END_ROW of window W that display
29836 characters between buffer positions START_CHARPOS and END_CHARPOS
29837 (excluding END_CHARPOS). DISP_STRING is a display string that
29838 covers these buffer positions. This is similar to
29839 row_containing_pos, but is more accurate when bidi reordering makes
29840 buffer positions change non-linearly with glyph rows. */
29841 static void
29842 rows_from_pos_range (struct window *w,
29843 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29844 Lisp_Object disp_string,
29845 struct glyph_row **start, struct glyph_row **end)
29847 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29848 int last_y = window_text_bottom_y (w);
29849 struct glyph_row *row;
29851 *start = NULL;
29852 *end = NULL;
29854 while (!first->enabled_p
29855 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29856 first++;
29858 /* Find the START row. */
29859 for (row = first;
29860 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29861 row++)
29863 /* A row can potentially be the START row if the range of the
29864 characters it displays intersects the range
29865 [START_CHARPOS..END_CHARPOS). */
29866 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29867 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29868 /* See the commentary in row_containing_pos, for the
29869 explanation of the complicated way to check whether
29870 some position is beyond the end of the characters
29871 displayed by a row. */
29872 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29873 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29874 && !row->ends_at_zv_p
29875 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29876 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29877 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29878 && !row->ends_at_zv_p
29879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29881 /* Found a candidate row. Now make sure at least one of the
29882 glyphs it displays has a charpos from the range
29883 [START_CHARPOS..END_CHARPOS).
29885 This is not obvious because bidi reordering could make
29886 buffer positions of a row be 1,2,3,102,101,100, and if we
29887 want to highlight characters in [50..60), we don't want
29888 this row, even though [50..60) does intersect [1..103),
29889 the range of character positions given by the row's start
29890 and end positions. */
29891 struct glyph *g = row->glyphs[TEXT_AREA];
29892 struct glyph *e = g + row->used[TEXT_AREA];
29894 while (g < e)
29896 if (((BUFFERP (g->object) || NILP (g->object))
29897 && start_charpos <= g->charpos && g->charpos < end_charpos)
29898 /* A glyph that comes from DISP_STRING is by
29899 definition to be highlighted. */
29900 || EQ (g->object, disp_string))
29901 *start = row;
29902 g++;
29904 if (*start)
29905 break;
29909 /* Find the END row. */
29910 if (!*start
29911 /* If the last row is partially visible, start looking for END
29912 from that row, instead of starting from FIRST. */
29913 && !(row->enabled_p
29914 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29915 row = first;
29916 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29918 struct glyph_row *next = row + 1;
29919 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29921 if (!next->enabled_p
29922 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29923 /* The first row >= START whose range of displayed characters
29924 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29925 is the row END + 1. */
29926 || (start_charpos < next_start
29927 && end_charpos < next_start)
29928 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29929 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29930 && !next->ends_at_zv_p
29931 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29932 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29933 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29934 && !next->ends_at_zv_p
29935 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29937 *end = row;
29938 break;
29940 else
29942 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29943 but none of the characters it displays are in the range, it is
29944 also END + 1. */
29945 struct glyph *g = next->glyphs[TEXT_AREA];
29946 struct glyph *s = g;
29947 struct glyph *e = g + next->used[TEXT_AREA];
29949 while (g < e)
29951 if (((BUFFERP (g->object) || NILP (g->object))
29952 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29953 /* If the buffer position of the first glyph in
29954 the row is equal to END_CHARPOS, it means
29955 the last character to be highlighted is the
29956 newline of ROW, and we must consider NEXT as
29957 END, not END+1. */
29958 || (((!next->reversed_p && g == s)
29959 || (next->reversed_p && g == e - 1))
29960 && (g->charpos == end_charpos
29961 /* Special case for when NEXT is an
29962 empty line at ZV. */
29963 || (g->charpos == -1
29964 && !row->ends_at_zv_p
29965 && next_start == end_charpos)))))
29966 /* A glyph that comes from DISP_STRING is by
29967 definition to be highlighted. */
29968 || EQ (g->object, disp_string))
29969 break;
29970 g++;
29972 if (g == e)
29974 *end = row;
29975 break;
29977 /* The first row that ends at ZV must be the last to be
29978 highlighted. */
29979 else if (next->ends_at_zv_p)
29981 *end = next;
29982 break;
29988 /* This function sets the mouse_face_* elements of HLINFO, assuming
29989 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29990 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29991 for the overlay or run of text properties specifying the mouse
29992 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29993 before-string and after-string that must also be highlighted.
29994 DISP_STRING, if non-nil, is a display string that may cover some
29995 or all of the highlighted text. */
29997 static void
29998 mouse_face_from_buffer_pos (Lisp_Object window,
29999 Mouse_HLInfo *hlinfo,
30000 ptrdiff_t mouse_charpos,
30001 ptrdiff_t start_charpos,
30002 ptrdiff_t end_charpos,
30003 Lisp_Object before_string,
30004 Lisp_Object after_string,
30005 Lisp_Object disp_string)
30007 struct window *w = XWINDOW (window);
30008 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30009 struct glyph_row *r1, *r2;
30010 struct glyph *glyph, *end;
30011 ptrdiff_t ignore, pos;
30012 int x;
30014 eassert (NILP (disp_string) || STRINGP (disp_string));
30015 eassert (NILP (before_string) || STRINGP (before_string));
30016 eassert (NILP (after_string) || STRINGP (after_string));
30018 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30019 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30020 if (r1 == NULL)
30021 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30022 /* If the before-string or display-string contains newlines,
30023 rows_from_pos_range skips to its last row. Move back. */
30024 if (!NILP (before_string) || !NILP (disp_string))
30026 struct glyph_row *prev;
30027 while ((prev = r1 - 1, prev >= first)
30028 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30029 && prev->used[TEXT_AREA] > 0)
30031 struct glyph *beg = prev->glyphs[TEXT_AREA];
30032 glyph = beg + prev->used[TEXT_AREA];
30033 while (--glyph >= beg && NILP (glyph->object));
30034 if (glyph < beg
30035 || !(EQ (glyph->object, before_string)
30036 || EQ (glyph->object, disp_string)))
30037 break;
30038 r1 = prev;
30041 if (r2 == NULL)
30043 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30044 hlinfo->mouse_face_past_end = true;
30046 else if (!NILP (after_string))
30048 /* If the after-string has newlines, advance to its last row. */
30049 struct glyph_row *next;
30050 struct glyph_row *last
30051 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30053 for (next = r2 + 1;
30054 next <= last
30055 && next->used[TEXT_AREA] > 0
30056 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30057 ++next)
30058 r2 = next;
30060 /* The rest of the display engine assumes that mouse_face_beg_row is
30061 either above mouse_face_end_row or identical to it. But with
30062 bidi-reordered continued lines, the row for START_CHARPOS could
30063 be below the row for END_CHARPOS. If so, swap the rows and store
30064 them in correct order. */
30065 if (r1->y > r2->y)
30067 struct glyph_row *tem = r2;
30069 r2 = r1;
30070 r1 = tem;
30073 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30074 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30076 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30077 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30078 could be anywhere in the row and in any order. The strategy
30079 below is to find the leftmost and the rightmost glyph that
30080 belongs to either of these 3 strings, or whose position is
30081 between START_CHARPOS and END_CHARPOS, and highlight all the
30082 glyphs between those two. This may cover more than just the text
30083 between START_CHARPOS and END_CHARPOS if the range of characters
30084 strides the bidi level boundary, e.g. if the beginning is in R2L
30085 text while the end is in L2R text or vice versa. */
30086 if (!r1->reversed_p)
30088 /* This row is in a left to right paragraph. Scan it left to
30089 right. */
30090 glyph = r1->glyphs[TEXT_AREA];
30091 end = glyph + r1->used[TEXT_AREA];
30092 x = r1->x;
30094 /* Skip truncation glyphs at the start of the glyph row. */
30095 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30096 for (; glyph < end
30097 && NILP (glyph->object)
30098 && glyph->charpos < 0;
30099 ++glyph)
30100 x += glyph->pixel_width;
30102 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30103 or DISP_STRING, and the first glyph from buffer whose
30104 position is between START_CHARPOS and END_CHARPOS. */
30105 for (; glyph < end
30106 && !NILP (glyph->object)
30107 && !EQ (glyph->object, disp_string)
30108 && !(BUFFERP (glyph->object)
30109 && (glyph->charpos >= start_charpos
30110 && glyph->charpos < end_charpos));
30111 ++glyph)
30113 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30114 are present at buffer positions between START_CHARPOS and
30115 END_CHARPOS, or if they come from an overlay. */
30116 if (EQ (glyph->object, before_string))
30118 pos = string_buffer_position (before_string,
30119 start_charpos);
30120 /* If pos == 0, it means before_string came from an
30121 overlay, not from a buffer position. */
30122 if (!pos || (pos >= start_charpos && pos < end_charpos))
30123 break;
30125 else if (EQ (glyph->object, after_string))
30127 pos = string_buffer_position (after_string, end_charpos);
30128 if (!pos || (pos >= start_charpos && pos < end_charpos))
30129 break;
30131 x += glyph->pixel_width;
30133 hlinfo->mouse_face_beg_x = x;
30134 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30136 else
30138 /* This row is in a right to left paragraph. Scan it right to
30139 left. */
30140 struct glyph *g;
30142 end = r1->glyphs[TEXT_AREA] - 1;
30143 glyph = end + r1->used[TEXT_AREA];
30145 /* Skip truncation glyphs at the start of the glyph row. */
30146 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30147 for (; glyph > end
30148 && NILP (glyph->object)
30149 && glyph->charpos < 0;
30150 --glyph)
30153 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30154 or DISP_STRING, and the first glyph from buffer whose
30155 position is between START_CHARPOS and END_CHARPOS. */
30156 for (; glyph > end
30157 && !NILP (glyph->object)
30158 && !EQ (glyph->object, disp_string)
30159 && !(BUFFERP (glyph->object)
30160 && (glyph->charpos >= start_charpos
30161 && glyph->charpos < end_charpos));
30162 --glyph)
30164 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30165 are present at buffer positions between START_CHARPOS and
30166 END_CHARPOS, or if they come from an overlay. */
30167 if (EQ (glyph->object, before_string))
30169 pos = string_buffer_position (before_string, start_charpos);
30170 /* If pos == 0, it means before_string came from an
30171 overlay, not from a buffer position. */
30172 if (!pos || (pos >= start_charpos && pos < end_charpos))
30173 break;
30175 else if (EQ (glyph->object, after_string))
30177 pos = string_buffer_position (after_string, end_charpos);
30178 if (!pos || (pos >= start_charpos && pos < end_charpos))
30179 break;
30183 glyph++; /* first glyph to the right of the highlighted area */
30184 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30185 x += g->pixel_width;
30186 hlinfo->mouse_face_beg_x = x;
30187 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30190 /* If the highlight ends in a different row, compute GLYPH and END
30191 for the end row. Otherwise, reuse the values computed above for
30192 the row where the highlight begins. */
30193 if (r2 != r1)
30195 if (!r2->reversed_p)
30197 glyph = r2->glyphs[TEXT_AREA];
30198 end = glyph + r2->used[TEXT_AREA];
30199 x = r2->x;
30201 else
30203 end = r2->glyphs[TEXT_AREA] - 1;
30204 glyph = end + r2->used[TEXT_AREA];
30208 if (!r2->reversed_p)
30210 /* Skip truncation and continuation glyphs near the end of the
30211 row, and also blanks and stretch glyphs inserted by
30212 extend_face_to_end_of_line. */
30213 while (end > glyph
30214 && NILP ((end - 1)->object))
30215 --end;
30216 /* Scan the rest of the glyph row from the end, looking for the
30217 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30218 DISP_STRING, or whose position is between START_CHARPOS
30219 and END_CHARPOS */
30220 for (--end;
30221 end > glyph
30222 && !NILP (end->object)
30223 && !EQ (end->object, disp_string)
30224 && !(BUFFERP (end->object)
30225 && (end->charpos >= start_charpos
30226 && end->charpos < end_charpos));
30227 --end)
30229 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30230 are present at buffer positions between START_CHARPOS and
30231 END_CHARPOS, or if they come from an overlay. */
30232 if (EQ (end->object, before_string))
30234 pos = string_buffer_position (before_string, start_charpos);
30235 if (!pos || (pos >= start_charpos && pos < end_charpos))
30236 break;
30238 else if (EQ (end->object, after_string))
30240 pos = string_buffer_position (after_string, end_charpos);
30241 if (!pos || (pos >= start_charpos && pos < end_charpos))
30242 break;
30245 /* Find the X coordinate of the last glyph to be highlighted. */
30246 for (; glyph <= end; ++glyph)
30247 x += glyph->pixel_width;
30249 hlinfo->mouse_face_end_x = x;
30250 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30252 else
30254 /* Skip truncation and continuation glyphs near the end of the
30255 row, and also blanks and stretch glyphs inserted by
30256 extend_face_to_end_of_line. */
30257 x = r2->x;
30258 end++;
30259 while (end < glyph
30260 && NILP (end->object))
30262 x += end->pixel_width;
30263 ++end;
30265 /* Scan the rest of the glyph row from the end, looking for the
30266 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30267 DISP_STRING, or whose position is between START_CHARPOS
30268 and END_CHARPOS */
30269 for ( ;
30270 end < glyph
30271 && !NILP (end->object)
30272 && !EQ (end->object, disp_string)
30273 && !(BUFFERP (end->object)
30274 && (end->charpos >= start_charpos
30275 && end->charpos < end_charpos));
30276 ++end)
30278 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30279 are present at buffer positions between START_CHARPOS and
30280 END_CHARPOS, or if they come from an overlay. */
30281 if (EQ (end->object, before_string))
30283 pos = string_buffer_position (before_string, start_charpos);
30284 if (!pos || (pos >= start_charpos && pos < end_charpos))
30285 break;
30287 else if (EQ (end->object, after_string))
30289 pos = string_buffer_position (after_string, end_charpos);
30290 if (!pos || (pos >= start_charpos && pos < end_charpos))
30291 break;
30293 x += end->pixel_width;
30295 /* If we exited the above loop because we arrived at the last
30296 glyph of the row, and its buffer position is still not in
30297 range, it means the last character in range is the preceding
30298 newline. Bump the end column and x values to get past the
30299 last glyph. */
30300 if (end == glyph
30301 && BUFFERP (end->object)
30302 && (end->charpos < start_charpos
30303 || end->charpos >= end_charpos))
30305 x += end->pixel_width;
30306 ++end;
30308 hlinfo->mouse_face_end_x = x;
30309 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30312 hlinfo->mouse_face_window = window;
30313 hlinfo->mouse_face_face_id
30314 = face_at_buffer_position (w, mouse_charpos, &ignore,
30315 mouse_charpos + 1,
30316 !hlinfo->mouse_face_hidden, -1);
30317 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30320 /* The following function is not used anymore (replaced with
30321 mouse_face_from_string_pos), but I leave it here for the time
30322 being, in case someone would. */
30324 #if false /* not used */
30326 /* Find the position of the glyph for position POS in OBJECT in
30327 window W's current matrix, and return in *X, *Y the pixel
30328 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30330 RIGHT_P means return the position of the right edge of the glyph.
30331 !RIGHT_P means return the left edge position.
30333 If no glyph for POS exists in the matrix, return the position of
30334 the glyph with the next smaller position that is in the matrix, if
30335 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30336 exists in the matrix, return the position of the glyph with the
30337 next larger position in OBJECT.
30339 Value is true if a glyph was found. */
30341 static bool
30342 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30343 int *hpos, int *vpos, int *x, int *y, bool right_p)
30345 int yb = window_text_bottom_y (w);
30346 struct glyph_row *r;
30347 struct glyph *best_glyph = NULL;
30348 struct glyph_row *best_row = NULL;
30349 int best_x = 0;
30351 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30352 r->enabled_p && r->y < yb;
30353 ++r)
30355 struct glyph *g = r->glyphs[TEXT_AREA];
30356 struct glyph *e = g + r->used[TEXT_AREA];
30357 int gx;
30359 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30360 if (EQ (g->object, object))
30362 if (g->charpos == pos)
30364 best_glyph = g;
30365 best_x = gx;
30366 best_row = r;
30367 goto found;
30369 else if (best_glyph == NULL
30370 || ((eabs (g->charpos - pos)
30371 < eabs (best_glyph->charpos - pos))
30372 && (right_p
30373 ? g->charpos < pos
30374 : g->charpos > pos)))
30376 best_glyph = g;
30377 best_x = gx;
30378 best_row = r;
30383 found:
30385 if (best_glyph)
30387 *x = best_x;
30388 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30390 if (right_p)
30392 *x += best_glyph->pixel_width;
30393 ++*hpos;
30396 *y = best_row->y;
30397 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30400 return best_glyph != NULL;
30402 #endif /* not used */
30404 /* Find the positions of the first and the last glyphs in window W's
30405 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30406 (assumed to be a string), and return in HLINFO's mouse_face_*
30407 members the pixel and column/row coordinates of those glyphs. */
30409 static void
30410 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30411 Lisp_Object object,
30412 ptrdiff_t startpos, ptrdiff_t endpos)
30414 int yb = window_text_bottom_y (w);
30415 struct glyph_row *r;
30416 struct glyph *g, *e;
30417 int gx;
30418 bool found = false;
30420 /* Find the glyph row with at least one position in the range
30421 [STARTPOS..ENDPOS), and the first glyph in that row whose
30422 position belongs to that range. */
30423 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30424 r->enabled_p && r->y < yb;
30425 ++r)
30427 if (!r->reversed_p)
30429 g = r->glyphs[TEXT_AREA];
30430 e = g + r->used[TEXT_AREA];
30431 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30432 if (EQ (g->object, object)
30433 && startpos <= g->charpos && g->charpos < endpos)
30435 hlinfo->mouse_face_beg_row
30436 = MATRIX_ROW_VPOS (r, w->current_matrix);
30437 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30438 hlinfo->mouse_face_beg_x = gx;
30439 found = true;
30440 break;
30443 else
30445 struct glyph *g1;
30447 e = r->glyphs[TEXT_AREA];
30448 g = e + r->used[TEXT_AREA];
30449 for ( ; g > e; --g)
30450 if (EQ ((g-1)->object, object)
30451 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30453 hlinfo->mouse_face_beg_row
30454 = MATRIX_ROW_VPOS (r, w->current_matrix);
30455 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30456 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30457 gx += g1->pixel_width;
30458 hlinfo->mouse_face_beg_x = gx;
30459 found = true;
30460 break;
30463 if (found)
30464 break;
30467 if (!found)
30468 return;
30470 /* Starting with the next row, look for the first row which does NOT
30471 include any glyphs whose positions are in the range. */
30472 for (++r; r->enabled_p && r->y < yb; ++r)
30474 g = r->glyphs[TEXT_AREA];
30475 e = g + r->used[TEXT_AREA];
30476 found = false;
30477 for ( ; g < e; ++g)
30478 if (EQ (g->object, object)
30479 && startpos <= g->charpos && g->charpos < endpos)
30481 found = true;
30482 break;
30484 if (!found)
30485 break;
30488 /* The highlighted region ends on the previous row. */
30489 r--;
30491 /* Set the end row. */
30492 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30494 /* Compute and set the end column and the end column's horizontal
30495 pixel coordinate. */
30496 if (!r->reversed_p)
30498 g = r->glyphs[TEXT_AREA];
30499 e = g + r->used[TEXT_AREA];
30500 for ( ; e > g; --e)
30501 if (EQ ((e-1)->object, object)
30502 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30503 break;
30504 hlinfo->mouse_face_end_col = e - g;
30506 for (gx = r->x; g < e; ++g)
30507 gx += g->pixel_width;
30508 hlinfo->mouse_face_end_x = gx;
30510 else
30512 e = r->glyphs[TEXT_AREA];
30513 g = e + r->used[TEXT_AREA];
30514 for (gx = r->x ; e < g; ++e)
30516 if (EQ (e->object, object)
30517 && startpos <= e->charpos && e->charpos < endpos)
30518 break;
30519 gx += e->pixel_width;
30521 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30522 hlinfo->mouse_face_end_x = gx;
30526 #ifdef HAVE_WINDOW_SYSTEM
30528 /* See if position X, Y is within a hot-spot of an image. */
30530 static bool
30531 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30533 if (!CONSP (hot_spot))
30534 return false;
30536 if (EQ (XCAR (hot_spot), Qrect))
30538 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30539 Lisp_Object rect = XCDR (hot_spot);
30540 Lisp_Object tem;
30541 if (!CONSP (rect))
30542 return false;
30543 if (!CONSP (XCAR (rect)))
30544 return false;
30545 if (!CONSP (XCDR (rect)))
30546 return false;
30547 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30548 return false;
30549 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30550 return false;
30551 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30552 return false;
30553 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30554 return false;
30555 return true;
30557 else if (EQ (XCAR (hot_spot), Qcircle))
30559 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30560 Lisp_Object circ = XCDR (hot_spot);
30561 Lisp_Object lr, lx0, ly0;
30562 if (CONSP (circ)
30563 && CONSP (XCAR (circ))
30564 && (lr = XCDR (circ), NUMBERP (lr))
30565 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30566 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30568 double r = XFLOATINT (lr);
30569 double dx = XINT (lx0) - x;
30570 double dy = XINT (ly0) - y;
30571 return (dx * dx + dy * dy <= r * r);
30574 else if (EQ (XCAR (hot_spot), Qpoly))
30576 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30577 if (VECTORP (XCDR (hot_spot)))
30579 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30580 Lisp_Object *poly = v->contents;
30581 ptrdiff_t n = v->header.size;
30582 ptrdiff_t i;
30583 bool inside = false;
30584 Lisp_Object lx, ly;
30585 int x0, y0;
30587 /* Need an even number of coordinates, and at least 3 edges. */
30588 if (n < 6 || n & 1)
30589 return false;
30591 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30592 If count is odd, we are inside polygon. Pixels on edges
30593 may or may not be included depending on actual geometry of the
30594 polygon. */
30595 if ((lx = poly[n-2], !INTEGERP (lx))
30596 || (ly = poly[n-1], !INTEGERP (lx)))
30597 return false;
30598 x0 = XINT (lx), y0 = XINT (ly);
30599 for (i = 0; i < n; i += 2)
30601 int x1 = x0, y1 = y0;
30602 if ((lx = poly[i], !INTEGERP (lx))
30603 || (ly = poly[i+1], !INTEGERP (ly)))
30604 return false;
30605 x0 = XINT (lx), y0 = XINT (ly);
30607 /* Does this segment cross the X line? */
30608 if (x0 >= x)
30610 if (x1 >= x)
30611 continue;
30613 else if (x1 < x)
30614 continue;
30615 if (y > y0 && y > y1)
30616 continue;
30617 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30618 inside = !inside;
30620 return inside;
30623 return false;
30626 Lisp_Object
30627 find_hot_spot (Lisp_Object map, int x, int y)
30629 while (CONSP (map))
30631 if (CONSP (XCAR (map))
30632 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30633 return XCAR (map);
30634 map = XCDR (map);
30637 return Qnil;
30640 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30641 3, 3, 0,
30642 doc: /* Lookup in image map MAP coordinates X and Y.
30643 An image map is an alist where each element has the format (AREA ID PLIST).
30644 An AREA is specified as either a rectangle, a circle, or a polygon:
30645 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30646 pixel coordinates of the upper left and bottom right corners.
30647 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30648 and the radius of the circle; r may be a float or integer.
30649 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30650 vector describes one corner in the polygon.
30651 Returns the alist element for the first matching AREA in MAP. */)
30652 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30654 if (NILP (map))
30655 return Qnil;
30657 CHECK_NUMBER (x);
30658 CHECK_NUMBER (y);
30660 return find_hot_spot (map,
30661 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30662 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30664 #endif /* HAVE_WINDOW_SYSTEM */
30667 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30668 static void
30669 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30671 #ifdef HAVE_WINDOW_SYSTEM
30672 if (!FRAME_WINDOW_P (f))
30673 return;
30675 /* Do not change cursor shape while dragging mouse. */
30676 if (EQ (do_mouse_tracking, Qdragging))
30677 return;
30679 if (!NILP (pointer))
30681 if (EQ (pointer, Qarrow))
30682 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30683 else if (EQ (pointer, Qhand))
30684 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30685 else if (EQ (pointer, Qtext))
30686 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30687 else if (EQ (pointer, intern ("hdrag")))
30688 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30689 else if (EQ (pointer, intern ("nhdrag")))
30690 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30691 # ifdef HAVE_X_WINDOWS
30692 else if (EQ (pointer, intern ("vdrag")))
30693 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30694 # endif
30695 else if (EQ (pointer, intern ("hourglass")))
30696 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30697 else if (EQ (pointer, Qmodeline))
30698 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30699 else
30700 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30703 if (cursor != No_Cursor)
30704 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30705 #endif
30708 /* Take proper action when mouse has moved to the mode or header line
30709 or marginal area AREA of window W, x-position X and y-position Y.
30710 X is relative to the start of the text display area of W, so the
30711 width of bitmap areas and scroll bars must be subtracted to get a
30712 position relative to the start of the mode line. */
30714 static void
30715 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30716 enum window_part area)
30718 struct window *w = XWINDOW (window);
30719 struct frame *f = XFRAME (w->frame);
30720 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30721 #ifdef HAVE_WINDOW_SYSTEM
30722 Display_Info *dpyinfo;
30723 #endif
30724 Cursor cursor = No_Cursor;
30725 Lisp_Object pointer = Qnil;
30726 int dx, dy, width, height;
30727 ptrdiff_t charpos;
30728 Lisp_Object string, object = Qnil;
30729 Lisp_Object pos UNINIT;
30730 Lisp_Object mouse_face;
30731 int original_x_pixel = x;
30732 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30733 struct glyph_row *row UNINIT;
30735 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30737 int x0;
30738 struct glyph *end;
30740 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30741 returns them in row/column units! */
30742 string = mode_line_string (w, area, &x, &y, &charpos,
30743 &object, &dx, &dy, &width, &height);
30745 row = (area == ON_MODE_LINE
30746 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30747 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30749 /* Find the glyph under the mouse pointer. */
30750 if (row->mode_line_p && row->enabled_p)
30752 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30753 end = glyph + row->used[TEXT_AREA];
30755 for (x0 = original_x_pixel;
30756 glyph < end && x0 >= glyph->pixel_width;
30757 ++glyph)
30758 x0 -= glyph->pixel_width;
30760 if (glyph >= end)
30761 glyph = NULL;
30764 else
30766 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30767 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30768 returns them in row/column units! */
30769 string = marginal_area_string (w, area, &x, &y, &charpos,
30770 &object, &dx, &dy, &width, &height);
30773 Lisp_Object help = Qnil;
30775 #ifdef HAVE_WINDOW_SYSTEM
30776 if (IMAGEP (object))
30778 Lisp_Object image_map, hotspot;
30779 if ((image_map = Fplist_get (XCDR (object), QCmap),
30780 !NILP (image_map))
30781 && (hotspot = find_hot_spot (image_map, dx, dy),
30782 CONSP (hotspot))
30783 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30785 Lisp_Object plist;
30787 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30788 If so, we could look for mouse-enter, mouse-leave
30789 properties in PLIST (and do something...). */
30790 hotspot = XCDR (hotspot);
30791 if (CONSP (hotspot)
30792 && (plist = XCAR (hotspot), CONSP (plist)))
30794 pointer = Fplist_get (plist, Qpointer);
30795 if (NILP (pointer))
30796 pointer = Qhand;
30797 help = Fplist_get (plist, Qhelp_echo);
30798 if (!NILP (help))
30800 help_echo_string = help;
30801 XSETWINDOW (help_echo_window, w);
30802 help_echo_object = w->contents;
30803 help_echo_pos = charpos;
30807 if (NILP (pointer))
30808 pointer = Fplist_get (XCDR (object), QCpointer);
30810 #endif /* HAVE_WINDOW_SYSTEM */
30812 if (STRINGP (string))
30813 pos = make_number (charpos);
30815 /* Set the help text and mouse pointer. If the mouse is on a part
30816 of the mode line without any text (e.g. past the right edge of
30817 the mode line text), use the default help text and pointer. */
30818 if (STRINGP (string) || area == ON_MODE_LINE)
30820 /* Arrange to display the help by setting the global variables
30821 help_echo_string, help_echo_object, and help_echo_pos. */
30822 if (NILP (help))
30824 if (STRINGP (string))
30825 help = Fget_text_property (pos, Qhelp_echo, string);
30827 if (!NILP (help))
30829 help_echo_string = help;
30830 XSETWINDOW (help_echo_window, w);
30831 help_echo_object = string;
30832 help_echo_pos = charpos;
30834 else if (area == ON_MODE_LINE)
30836 Lisp_Object default_help
30837 = buffer_local_value (Qmode_line_default_help_echo,
30838 w->contents);
30840 if (FUNCTIONP (default_help) || STRINGP (default_help))
30842 help_echo_string = (FUNCTIONP (default_help)
30843 ? safe_call1 (default_help, window)
30844 : default_help);
30845 XSETWINDOW (help_echo_window, w);
30846 help_echo_object = Qnil;
30847 help_echo_pos = -1;
30852 #ifdef HAVE_WINDOW_SYSTEM
30853 /* Change the mouse pointer according to what is under it. */
30854 if (FRAME_WINDOW_P (f))
30856 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30857 || minibuf_level
30858 || NILP (Vresize_mini_windows));
30860 dpyinfo = FRAME_DISPLAY_INFO (f);
30861 if (STRINGP (string))
30863 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30865 if (NILP (pointer))
30866 pointer = Fget_text_property (pos, Qpointer, string);
30868 /* Change the mouse pointer according to what is under X/Y. */
30869 if (NILP (pointer)
30870 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30872 Lisp_Object map;
30873 map = Fget_text_property (pos, Qlocal_map, string);
30874 if (!KEYMAPP (map))
30875 map = Fget_text_property (pos, Qkeymap, string);
30876 if (!KEYMAPP (map) && draggable)
30877 cursor = dpyinfo->vertical_scroll_bar_cursor;
30880 else if (draggable)
30881 /* Default mode-line pointer. */
30882 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30884 #endif
30887 /* Change the mouse face according to what is under X/Y. */
30888 bool mouse_face_shown = false;
30889 if (STRINGP (string))
30891 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30892 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30893 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30894 && glyph)
30896 Lisp_Object b, e;
30898 struct glyph * tmp_glyph;
30900 int gpos;
30901 int gseq_length;
30902 int total_pixel_width;
30903 ptrdiff_t begpos, endpos, ignore;
30905 int vpos, hpos;
30907 b = Fprevious_single_property_change (make_number (charpos + 1),
30908 Qmouse_face, string, Qnil);
30909 if (NILP (b))
30910 begpos = 0;
30911 else
30912 begpos = XINT (b);
30914 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30915 if (NILP (e))
30916 endpos = SCHARS (string);
30917 else
30918 endpos = XINT (e);
30920 /* Calculate the glyph position GPOS of GLYPH in the
30921 displayed string, relative to the beginning of the
30922 highlighted part of the string.
30924 Note: GPOS is different from CHARPOS. CHARPOS is the
30925 position of GLYPH in the internal string object. A mode
30926 line string format has structures which are converted to
30927 a flattened string by the Emacs Lisp interpreter. The
30928 internal string is an element of those structures. The
30929 displayed string is the flattened string. */
30930 tmp_glyph = row_start_glyph;
30931 while (tmp_glyph < glyph
30932 && (!(EQ (tmp_glyph->object, glyph->object)
30933 && begpos <= tmp_glyph->charpos
30934 && tmp_glyph->charpos < endpos)))
30935 tmp_glyph++;
30936 gpos = glyph - tmp_glyph;
30938 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30939 the highlighted part of the displayed string to which
30940 GLYPH belongs. Note: GSEQ_LENGTH is different from
30941 SCHARS (STRING), because the latter returns the length of
30942 the internal string. */
30943 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30944 tmp_glyph > glyph
30945 && (!(EQ (tmp_glyph->object, glyph->object)
30946 && begpos <= tmp_glyph->charpos
30947 && tmp_glyph->charpos < endpos));
30948 tmp_glyph--)
30950 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30952 /* Calculate the total pixel width of all the glyphs between
30953 the beginning of the highlighted area and GLYPH. */
30954 total_pixel_width = 0;
30955 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30956 total_pixel_width += tmp_glyph->pixel_width;
30958 /* Pre calculation of re-rendering position. Note: X is in
30959 column units here, after the call to mode_line_string or
30960 marginal_area_string. */
30961 hpos = x - gpos;
30962 vpos = (area == ON_MODE_LINE
30963 ? (w->current_matrix)->nrows - 1
30964 : 0);
30966 /* If GLYPH's position is included in the region that is
30967 already drawn in mouse face, we have nothing to do. */
30968 if ( EQ (window, hlinfo->mouse_face_window)
30969 && (!row->reversed_p
30970 ? (hlinfo->mouse_face_beg_col <= hpos
30971 && hpos < hlinfo->mouse_face_end_col)
30972 /* In R2L rows we swap BEG and END, see below. */
30973 : (hlinfo->mouse_face_end_col <= hpos
30974 && hpos < hlinfo->mouse_face_beg_col))
30975 && hlinfo->mouse_face_beg_row == vpos )
30976 return;
30978 if (clear_mouse_face (hlinfo))
30979 cursor = No_Cursor;
30981 if (!row->reversed_p)
30983 hlinfo->mouse_face_beg_col = hpos;
30984 hlinfo->mouse_face_beg_x = original_x_pixel
30985 - (total_pixel_width + dx);
30986 hlinfo->mouse_face_end_col = hpos + gseq_length;
30987 hlinfo->mouse_face_end_x = 0;
30989 else
30991 /* In R2L rows, show_mouse_face expects BEG and END
30992 coordinates to be swapped. */
30993 hlinfo->mouse_face_end_col = hpos;
30994 hlinfo->mouse_face_end_x = original_x_pixel
30995 - (total_pixel_width + dx);
30996 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30997 hlinfo->mouse_face_beg_x = 0;
31000 hlinfo->mouse_face_beg_row = vpos;
31001 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31002 hlinfo->mouse_face_past_end = false;
31003 hlinfo->mouse_face_window = window;
31005 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31006 charpos,
31007 0, &ignore,
31008 glyph->face_id,
31009 true);
31010 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31011 mouse_face_shown = true;
31013 if (NILP (pointer))
31014 pointer = Qhand;
31018 /* If mouse-face doesn't need to be shown, clear any existing
31019 mouse-face. */
31020 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31021 clear_mouse_face (hlinfo);
31023 define_frame_cursor1 (f, cursor, pointer);
31027 /* EXPORT:
31028 Take proper action when the mouse has moved to position X, Y on
31029 frame F with regards to highlighting portions of display that have
31030 mouse-face properties. Also de-highlight portions of display where
31031 the mouse was before, set the mouse pointer shape as appropriate
31032 for the mouse coordinates, and activate help echo (tooltips).
31033 X and Y can be negative or out of range. */
31035 void
31036 note_mouse_highlight (struct frame *f, int x, int y)
31038 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31039 enum window_part part = ON_NOTHING;
31040 Lisp_Object window;
31041 struct window *w;
31042 Cursor cursor = No_Cursor;
31043 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31044 struct buffer *b;
31046 /* When a menu is active, don't highlight because this looks odd. */
31047 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31048 if (popup_activated ())
31049 return;
31050 #endif
31052 if (!f->glyphs_initialized_p
31053 || f->pointer_invisible)
31054 return;
31056 hlinfo->mouse_face_mouse_x = x;
31057 hlinfo->mouse_face_mouse_y = y;
31058 hlinfo->mouse_face_mouse_frame = f;
31060 if (hlinfo->mouse_face_defer)
31061 return;
31063 /* Which window is that in? */
31064 window = window_from_coordinates (f, x, y, &part, true);
31066 /* If displaying active text in another window, clear that. */
31067 if (! EQ (window, hlinfo->mouse_face_window)
31068 /* Also clear if we move out of text area in same window. */
31069 || (!NILP (hlinfo->mouse_face_window)
31070 && !NILP (window)
31071 && part != ON_TEXT
31072 && part != ON_MODE_LINE
31073 && part != ON_HEADER_LINE))
31074 clear_mouse_face (hlinfo);
31076 /* Reset help_echo_string. It will get recomputed below. */
31077 help_echo_string = Qnil;
31079 #ifdef HAVE_WINDOW_SYSTEM
31080 /* If the cursor is on the internal border of FRAME and FRAME's
31081 internal border is draggable, provide some visual feedback. */
31082 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31083 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31085 enum internal_border_part part = frame_internal_border_part (f, x, y);
31087 switch (part)
31089 case INTERNAL_BORDER_NONE:
31090 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31091 /* Reset cursor. */
31092 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31093 break;
31094 case INTERNAL_BORDER_LEFT_EDGE:
31095 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31096 break;
31097 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31098 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31099 break;
31100 case INTERNAL_BORDER_TOP_EDGE:
31101 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31102 break;
31103 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31104 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31105 break;
31106 case INTERNAL_BORDER_RIGHT_EDGE:
31107 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31108 break;
31109 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31110 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31111 break;
31112 case INTERNAL_BORDER_BOTTOM_EDGE:
31113 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31114 break;
31115 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31116 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31117 break;
31118 default:
31119 /* This should not happen. */
31120 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31121 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31124 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31126 /* Do we really want a help echo here? */
31127 help_echo_string = build_string ("drag-mouse-1: resize frame");
31128 goto set_cursor;
31131 #endif /* HAVE_WINDOW_SYSTEM */
31133 /* Not on a window -> return. */
31134 if (!WINDOWP (window))
31135 return;
31137 /* Convert to window-relative pixel coordinates. */
31138 w = XWINDOW (window);
31139 frame_to_window_pixel_xy (w, &x, &y);
31141 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31142 /* Handle tool-bar window differently since it doesn't display a
31143 buffer. */
31144 if (EQ (window, f->tool_bar_window))
31146 note_tool_bar_highlight (f, x, y);
31147 return;
31149 #endif
31151 /* Mouse is on the mode, header line or margin? */
31152 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31153 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31155 note_mode_line_or_margin_highlight (window, x, y, part);
31157 #ifdef HAVE_WINDOW_SYSTEM
31158 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31160 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31161 /* Show non-text cursor (Bug#16647). */
31162 goto set_cursor;
31164 else
31165 #endif
31166 return;
31169 #ifdef HAVE_WINDOW_SYSTEM
31170 if (part == ON_VERTICAL_BORDER)
31172 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31173 help_echo_string = build_string ("drag-mouse-1: resize");
31174 goto set_cursor;
31176 else if (part == ON_RIGHT_DIVIDER)
31178 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31179 help_echo_string = build_string ("drag-mouse-1: resize");
31180 goto set_cursor;
31182 else if (part == ON_BOTTOM_DIVIDER)
31183 if (! WINDOW_BOTTOMMOST_P (w)
31184 || minibuf_level
31185 || NILP (Vresize_mini_windows))
31187 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31188 help_echo_string = build_string ("drag-mouse-1: resize");
31189 goto set_cursor;
31191 else
31192 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31193 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31194 || part == ON_VERTICAL_SCROLL_BAR
31195 || part == ON_HORIZONTAL_SCROLL_BAR)
31196 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31197 else
31198 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31199 #endif
31201 /* Are we in a window whose display is up to date?
31202 And verify the buffer's text has not changed. */
31203 b = XBUFFER (w->contents);
31204 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31206 int hpos, vpos, dx, dy, area = LAST_AREA;
31207 ptrdiff_t pos;
31208 struct glyph *glyph;
31209 Lisp_Object object;
31210 Lisp_Object mouse_face = Qnil, position;
31211 Lisp_Object *overlay_vec = NULL;
31212 ptrdiff_t i, noverlays;
31213 struct buffer *obuf;
31214 ptrdiff_t obegv, ozv;
31215 bool same_region;
31217 /* Find the glyph under X/Y. */
31218 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31220 #ifdef HAVE_WINDOW_SYSTEM
31221 /* Look for :pointer property on image. */
31222 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31224 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31225 if (img != NULL && IMAGEP (img->spec))
31227 Lisp_Object image_map, hotspot;
31228 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31229 !NILP (image_map))
31230 && (hotspot = find_hot_spot (image_map,
31231 glyph->slice.img.x + dx,
31232 glyph->slice.img.y + dy),
31233 CONSP (hotspot))
31234 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31236 Lisp_Object plist;
31238 /* Could check XCAR (hotspot) to see if we enter/leave
31239 this hot-spot.
31240 If so, we could look for mouse-enter, mouse-leave
31241 properties in PLIST (and do something...). */
31242 hotspot = XCDR (hotspot);
31243 if (CONSP (hotspot)
31244 && (plist = XCAR (hotspot), CONSP (plist)))
31246 pointer = Fplist_get (plist, Qpointer);
31247 if (NILP (pointer))
31248 pointer = Qhand;
31249 help_echo_string = Fplist_get (plist, Qhelp_echo);
31250 if (!NILP (help_echo_string))
31252 help_echo_window = window;
31253 help_echo_object = glyph->object;
31254 help_echo_pos = glyph->charpos;
31258 if (NILP (pointer))
31259 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31262 #endif /* HAVE_WINDOW_SYSTEM */
31264 /* Clear mouse face if X/Y not over text. */
31265 if (glyph == NULL
31266 || area != TEXT_AREA
31267 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31268 /* Glyph's OBJECT is nil for glyphs inserted by the
31269 display engine for its internal purposes, like truncation
31270 and continuation glyphs and blanks beyond the end of
31271 line's text on text terminals. If we are over such a
31272 glyph, we are not over any text. */
31273 || NILP (glyph->object)
31274 /* R2L rows have a stretch glyph at their front, which
31275 stands for no text, whereas L2R rows have no glyphs at
31276 all beyond the end of text. Treat such stretch glyphs
31277 like we do with NULL glyphs in L2R rows. */
31278 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31279 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31280 && glyph->type == STRETCH_GLYPH
31281 && glyph->avoid_cursor_p))
31283 if (clear_mouse_face (hlinfo))
31284 cursor = No_Cursor;
31285 if (FRAME_WINDOW_P (f) && NILP (pointer))
31287 #ifdef HAVE_WINDOW_SYSTEM
31288 if (area != TEXT_AREA)
31289 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31290 else
31291 pointer = Vvoid_text_area_pointer;
31292 #endif
31294 goto set_cursor;
31297 pos = glyph->charpos;
31298 object = glyph->object;
31299 if (!STRINGP (object) && !BUFFERP (object))
31300 goto set_cursor;
31302 /* If we get an out-of-range value, return now; avoid an error. */
31303 if (BUFFERP (object) && pos > BUF_Z (b))
31304 goto set_cursor;
31306 /* Make the window's buffer temporarily current for
31307 overlays_at and compute_char_face. */
31308 obuf = current_buffer;
31309 current_buffer = b;
31310 obegv = BEGV;
31311 ozv = ZV;
31312 BEGV = BEG;
31313 ZV = Z;
31315 /* Is this char mouse-active or does it have help-echo? */
31316 position = make_number (pos);
31318 USE_SAFE_ALLOCA;
31320 if (BUFFERP (object))
31322 /* Put all the overlays we want in a vector in overlay_vec. */
31323 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31324 /* Sort overlays into increasing priority order. */
31325 noverlays = sort_overlays (overlay_vec, noverlays, w);
31327 else
31328 noverlays = 0;
31330 if (NILP (Vmouse_highlight))
31332 clear_mouse_face (hlinfo);
31333 goto check_help_echo;
31336 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31338 if (same_region)
31339 cursor = No_Cursor;
31341 /* Check mouse-face highlighting. */
31342 if (! same_region
31343 /* If there exists an overlay with mouse-face overlapping
31344 the one we are currently highlighting, we have to
31345 check if we enter the overlapping overlay, and then
31346 highlight only that. */
31347 || (OVERLAYP (hlinfo->mouse_face_overlay)
31348 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31350 /* Find the highest priority overlay with a mouse-face. */
31351 Lisp_Object overlay = Qnil;
31352 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31354 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31355 if (!NILP (mouse_face))
31356 overlay = overlay_vec[i];
31359 /* If we're highlighting the same overlay as before, there's
31360 no need to do that again. */
31361 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31362 goto check_help_echo;
31364 /* Clear the display of the old active region, if any. */
31365 if (clear_mouse_face (hlinfo))
31366 cursor = No_Cursor;
31368 /* Record the overlay, if any, to be highlighted. */
31369 hlinfo->mouse_face_overlay = overlay;
31371 /* If no overlay applies, get a text property. */
31372 if (NILP (overlay))
31373 mouse_face = Fget_text_property (position, Qmouse_face, object);
31375 /* Next, compute the bounds of the mouse highlighting and
31376 display it. */
31377 if (!NILP (mouse_face) && STRINGP (object))
31379 /* The mouse-highlighting comes from a display string
31380 with a mouse-face. */
31381 Lisp_Object s, e;
31382 ptrdiff_t ignore;
31384 s = Fprevious_single_property_change
31385 (make_number (pos + 1), Qmouse_face, object, Qnil);
31386 e = Fnext_single_property_change
31387 (position, Qmouse_face, object, Qnil);
31388 if (NILP (s))
31389 s = make_number (0);
31390 if (NILP (e))
31391 e = make_number (SCHARS (object));
31392 mouse_face_from_string_pos (w, hlinfo, object,
31393 XINT (s), XINT (e));
31394 hlinfo->mouse_face_past_end = false;
31395 hlinfo->mouse_face_window = window;
31396 hlinfo->mouse_face_face_id
31397 = face_at_string_position (w, object, pos, 0, &ignore,
31398 glyph->face_id, true);
31399 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31400 cursor = No_Cursor;
31402 else
31404 /* The mouse-highlighting, if any, comes from an overlay
31405 or text property in the buffer. */
31406 Lisp_Object buffer UNINIT;
31407 Lisp_Object disp_string UNINIT;
31409 if (STRINGP (object))
31411 /* If we are on a display string with no mouse-face,
31412 check if the text under it has one. */
31413 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31414 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31415 pos = string_buffer_position (object, start);
31416 if (pos > 0)
31418 mouse_face = get_char_property_and_overlay
31419 (make_number (pos), Qmouse_face, w->contents, &overlay);
31420 buffer = w->contents;
31421 disp_string = object;
31424 else
31426 buffer = object;
31427 disp_string = Qnil;
31430 if (!NILP (mouse_face))
31432 Lisp_Object before, after;
31433 Lisp_Object before_string, after_string;
31434 /* To correctly find the limits of mouse highlight
31435 in a bidi-reordered buffer, we must not use the
31436 optimization of limiting the search in
31437 previous-single-property-change and
31438 next-single-property-change, because
31439 rows_from_pos_range needs the real start and end
31440 positions to DTRT in this case. That's because
31441 the first row visible in a window does not
31442 necessarily display the character whose position
31443 is the smallest. */
31444 Lisp_Object lim1
31445 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31446 ? Fmarker_position (w->start)
31447 : Qnil;
31448 Lisp_Object lim2
31449 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31450 ? make_number (BUF_Z (XBUFFER (buffer))
31451 - w->window_end_pos)
31452 : Qnil;
31454 if (NILP (overlay))
31456 /* Handle the text property case. */
31457 before = Fprevious_single_property_change
31458 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31459 after = Fnext_single_property_change
31460 (make_number (pos), Qmouse_face, buffer, lim2);
31461 before_string = after_string = Qnil;
31463 else
31465 /* Handle the overlay case. */
31466 before = Foverlay_start (overlay);
31467 after = Foverlay_end (overlay);
31468 before_string = Foverlay_get (overlay, Qbefore_string);
31469 after_string = Foverlay_get (overlay, Qafter_string);
31471 if (!STRINGP (before_string)) before_string = Qnil;
31472 if (!STRINGP (after_string)) after_string = Qnil;
31475 mouse_face_from_buffer_pos (window, hlinfo, pos,
31476 NILP (before)
31478 : XFASTINT (before),
31479 NILP (after)
31480 ? BUF_Z (XBUFFER (buffer))
31481 : XFASTINT (after),
31482 before_string, after_string,
31483 disp_string);
31484 cursor = No_Cursor;
31489 check_help_echo:
31491 /* Look for a `help-echo' property. */
31492 if (NILP (help_echo_string)) {
31493 Lisp_Object help, overlay;
31495 /* Check overlays first. */
31496 help = overlay = Qnil;
31497 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31499 overlay = overlay_vec[i];
31500 help = Foverlay_get (overlay, Qhelp_echo);
31503 if (!NILP (help))
31505 help_echo_string = help;
31506 help_echo_window = window;
31507 help_echo_object = overlay;
31508 help_echo_pos = pos;
31510 else
31512 Lisp_Object obj = glyph->object;
31513 ptrdiff_t charpos = glyph->charpos;
31515 /* Try text properties. */
31516 if (STRINGP (obj)
31517 && charpos >= 0
31518 && charpos < SCHARS (obj))
31520 help = Fget_text_property (make_number (charpos),
31521 Qhelp_echo, obj);
31522 if (NILP (help))
31524 /* If the string itself doesn't specify a help-echo,
31525 see if the buffer text ``under'' it does. */
31526 struct glyph_row *r
31527 = MATRIX_ROW (w->current_matrix, vpos);
31528 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31529 ptrdiff_t p = string_buffer_position (obj, start);
31530 if (p > 0)
31532 help = Fget_char_property (make_number (p),
31533 Qhelp_echo, w->contents);
31534 if (!NILP (help))
31536 charpos = p;
31537 obj = w->contents;
31542 else if (BUFFERP (obj)
31543 && charpos >= BEGV
31544 && charpos < ZV)
31545 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31546 obj);
31548 if (!NILP (help))
31550 help_echo_string = help;
31551 help_echo_window = window;
31552 help_echo_object = obj;
31553 help_echo_pos = charpos;
31558 #ifdef HAVE_WINDOW_SYSTEM
31559 /* Look for a `pointer' property. */
31560 if (FRAME_WINDOW_P (f) && NILP (pointer))
31562 /* Check overlays first. */
31563 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31564 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31566 if (NILP (pointer))
31568 Lisp_Object obj = glyph->object;
31569 ptrdiff_t charpos = glyph->charpos;
31571 /* Try text properties. */
31572 if (STRINGP (obj)
31573 && charpos >= 0
31574 && charpos < SCHARS (obj))
31576 pointer = Fget_text_property (make_number (charpos),
31577 Qpointer, obj);
31578 if (NILP (pointer))
31580 /* If the string itself doesn't specify a pointer,
31581 see if the buffer text ``under'' it does. */
31582 struct glyph_row *r
31583 = MATRIX_ROW (w->current_matrix, vpos);
31584 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31585 ptrdiff_t p = string_buffer_position (obj, start);
31586 if (p > 0)
31587 pointer = Fget_char_property (make_number (p),
31588 Qpointer, w->contents);
31591 else if (BUFFERP (obj)
31592 && charpos >= BEGV
31593 && charpos < ZV)
31594 pointer = Fget_text_property (make_number (charpos),
31595 Qpointer, obj);
31598 #endif /* HAVE_WINDOW_SYSTEM */
31600 BEGV = obegv;
31601 ZV = ozv;
31602 current_buffer = obuf;
31603 SAFE_FREE ();
31606 set_cursor:
31607 define_frame_cursor1 (f, cursor, pointer);
31611 /* EXPORT for RIF:
31612 Clear any mouse-face on window W. This function is part of the
31613 redisplay interface, and is called from try_window_id and similar
31614 functions to ensure the mouse-highlight is off. */
31616 void
31617 x_clear_window_mouse_face (struct window *w)
31619 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31620 Lisp_Object window;
31622 block_input ();
31623 XSETWINDOW (window, w);
31624 if (EQ (window, hlinfo->mouse_face_window))
31625 clear_mouse_face (hlinfo);
31626 unblock_input ();
31630 /* EXPORT:
31631 Just discard the mouse face information for frame F, if any.
31632 This is used when the size of F is changed. */
31634 void
31635 cancel_mouse_face (struct frame *f)
31637 Lisp_Object window;
31638 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31640 window = hlinfo->mouse_face_window;
31641 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31642 reset_mouse_highlight (hlinfo);
31647 /***********************************************************************
31648 Exposure Events
31649 ***********************************************************************/
31651 #ifdef HAVE_WINDOW_SYSTEM
31653 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31654 which intersects rectangle R. R is in window-relative coordinates. */
31656 static void
31657 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31658 enum glyph_row_area area)
31660 struct glyph *first = row->glyphs[area];
31661 struct glyph *end = row->glyphs[area] + row->used[area];
31662 struct glyph *last;
31663 int first_x, start_x, x;
31665 if (area == TEXT_AREA && row->fill_line_p)
31666 /* If row extends face to end of line write the whole line. */
31667 draw_glyphs (w, 0, row, area,
31668 0, row->used[area],
31669 DRAW_NORMAL_TEXT, 0);
31670 else
31672 /* Set START_X to the window-relative start position for drawing glyphs of
31673 AREA. The first glyph of the text area can be partially visible.
31674 The first glyphs of other areas cannot. */
31675 start_x = window_box_left_offset (w, area);
31676 x = start_x;
31677 if (area == TEXT_AREA)
31678 x += row->x;
31680 /* Find the first glyph that must be redrawn. */
31681 while (first < end
31682 && x + first->pixel_width < r->x)
31684 x += first->pixel_width;
31685 ++first;
31688 /* Find the last one. */
31689 last = first;
31690 first_x = x;
31691 /* Use a signed int intermediate value to avoid catastrophic
31692 failures due to comparison between signed and unsigned, when
31693 x is negative (can happen for wide images that are hscrolled). */
31694 int r_end = r->x + r->width;
31695 while (last < end && x < r_end)
31697 x += last->pixel_width;
31698 ++last;
31701 /* Repaint. */
31702 if (last > first)
31703 draw_glyphs (w, first_x - start_x, row, area,
31704 first - row->glyphs[area], last - row->glyphs[area],
31705 DRAW_NORMAL_TEXT, 0);
31710 /* Redraw the parts of the glyph row ROW on window W intersecting
31711 rectangle R. R is in window-relative coordinates. Value is
31712 true if mouse-face was overwritten. */
31714 static bool
31715 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31717 eassert (row->enabled_p);
31719 if (row->mode_line_p || w->pseudo_window_p)
31720 draw_glyphs (w, 0, row, TEXT_AREA,
31721 0, row->used[TEXT_AREA],
31722 DRAW_NORMAL_TEXT, 0);
31723 else
31725 if (row->used[LEFT_MARGIN_AREA])
31726 expose_area (w, row, r, LEFT_MARGIN_AREA);
31727 if (row->used[TEXT_AREA])
31728 expose_area (w, row, r, TEXT_AREA);
31729 if (row->used[RIGHT_MARGIN_AREA])
31730 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31731 draw_row_fringe_bitmaps (w, row);
31734 return row->mouse_face_p;
31738 /* Redraw those parts of glyphs rows during expose event handling that
31739 overlap other rows. Redrawing of an exposed line writes over parts
31740 of lines overlapping that exposed line; this function fixes that.
31742 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31743 row in W's current matrix that is exposed and overlaps other rows.
31744 LAST_OVERLAPPING_ROW is the last such row. */
31746 static void
31747 expose_overlaps (struct window *w,
31748 struct glyph_row *first_overlapping_row,
31749 struct glyph_row *last_overlapping_row,
31750 XRectangle *r)
31752 struct glyph_row *row;
31754 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31755 if (row->overlapping_p)
31757 eassert (row->enabled_p && !row->mode_line_p);
31759 row->clip = r;
31760 if (row->used[LEFT_MARGIN_AREA])
31761 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31763 if (row->used[TEXT_AREA])
31764 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31766 if (row->used[RIGHT_MARGIN_AREA])
31767 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31768 row->clip = NULL;
31773 /* Return true if W's cursor intersects rectangle R. */
31775 static bool
31776 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31778 XRectangle cr, result;
31779 struct glyph *cursor_glyph;
31780 struct glyph_row *row;
31782 if (w->phys_cursor.vpos >= 0
31783 && w->phys_cursor.vpos < w->current_matrix->nrows
31784 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31785 row->enabled_p)
31786 && row->cursor_in_fringe_p)
31788 /* Cursor is in the fringe. */
31789 cr.x = window_box_right_offset (w,
31790 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31791 ? RIGHT_MARGIN_AREA
31792 : TEXT_AREA));
31793 cr.y = row->y;
31794 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31795 cr.height = row->height;
31796 return x_intersect_rectangles (&cr, r, &result);
31799 cursor_glyph = get_phys_cursor_glyph (w);
31800 if (cursor_glyph)
31802 /* r is relative to W's box, but w->phys_cursor.x is relative
31803 to left edge of W's TEXT area. Adjust it. */
31804 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31805 cr.y = w->phys_cursor.y;
31806 cr.width = cursor_glyph->pixel_width;
31807 cr.height = w->phys_cursor_height;
31808 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31809 I assume the effect is the same -- and this is portable. */
31810 return x_intersect_rectangles (&cr, r, &result);
31812 /* If we don't understand the format, pretend we're not in the hot-spot. */
31813 return false;
31817 /* EXPORT:
31818 Draw a vertical window border to the right of window W if W doesn't
31819 have vertical scroll bars. */
31821 void
31822 x_draw_vertical_border (struct window *w)
31824 struct frame *f = XFRAME (WINDOW_FRAME (w));
31826 /* We could do better, if we knew what type of scroll-bar the adjacent
31827 windows (on either side) have... But we don't :-(
31828 However, I think this works ok. ++KFS 2003-04-25 */
31830 /* Redraw borders between horizontally adjacent windows. Don't
31831 do it for frames with vertical scroll bars because either the
31832 right scroll bar of a window, or the left scroll bar of its
31833 neighbor will suffice as a border. */
31834 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31835 return;
31837 /* Note: It is necessary to redraw both the left and the right
31838 borders, for when only this single window W is being
31839 redisplayed. */
31840 if (!WINDOW_RIGHTMOST_P (w)
31841 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31843 int x0, x1, y0, y1;
31845 window_box_edges (w, &x0, &y0, &x1, &y1);
31846 y1 -= 1;
31848 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31849 x1 -= 1;
31851 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31854 if (!WINDOW_LEFTMOST_P (w)
31855 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31857 int x0, x1, y0, y1;
31859 window_box_edges (w, &x0, &y0, &x1, &y1);
31860 y1 -= 1;
31862 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31863 x0 -= 1;
31865 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31870 /* Draw window dividers for window W. */
31872 void
31873 x_draw_right_divider (struct window *w)
31875 struct frame *f = WINDOW_XFRAME (w);
31877 if (w->mini || w->pseudo_window_p)
31878 return;
31879 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31881 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31882 int x1 = WINDOW_RIGHT_EDGE_X (w);
31883 int y0 = WINDOW_TOP_EDGE_Y (w);
31884 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31886 /* If W is horizontally combined and has a right sibling, don't
31887 draw over any bottom divider. */
31888 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31889 && !NILP (w->parent)
31890 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31891 && !NILP (w->next))
31892 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31894 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31898 static void
31899 x_draw_bottom_divider (struct window *w)
31901 struct frame *f = XFRAME (WINDOW_FRAME (w));
31903 if (w->mini || w->pseudo_window_p)
31904 return;
31905 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31907 int x0 = WINDOW_LEFT_EDGE_X (w);
31908 int x1 = WINDOW_RIGHT_EDGE_X (w);
31909 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31910 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31911 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
31913 /* If W is vertically combined and has a sibling below, don't draw
31914 over any right divider. */
31915 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31916 && p
31917 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31918 && !NILP (w->next))
31919 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31920 && NILP (w->next)
31921 && !NILP (p->parent)
31922 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31923 && !NILP (XWINDOW (p->parent)->next))))
31924 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31926 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31930 /* Redraw the part of window W intersection rectangle FR. Pixel
31931 coordinates in FR are frame-relative. Call this function with
31932 input blocked. Value is true if the exposure overwrites
31933 mouse-face. */
31935 static bool
31936 expose_window (struct window *w, XRectangle *fr)
31938 struct frame *f = XFRAME (w->frame);
31939 XRectangle wr, r;
31940 bool mouse_face_overwritten_p = false;
31942 /* If window is not yet fully initialized, do nothing. This can
31943 happen when toolkit scroll bars are used and a window is split.
31944 Reconfiguring the scroll bar will generate an expose for a newly
31945 created window. */
31946 if (w->current_matrix == NULL)
31947 return false;
31949 /* When we're currently updating the window, display and current
31950 matrix usually don't agree. Arrange for a thorough display
31951 later. */
31952 if (w->must_be_updated_p)
31954 SET_FRAME_GARBAGED (f);
31955 return false;
31958 /* Frame-relative pixel rectangle of W. */
31959 wr.x = WINDOW_LEFT_EDGE_X (w);
31960 wr.y = WINDOW_TOP_EDGE_Y (w);
31961 wr.width = WINDOW_PIXEL_WIDTH (w);
31962 wr.height = WINDOW_PIXEL_HEIGHT (w);
31964 if (x_intersect_rectangles (fr, &wr, &r))
31966 int yb = window_text_bottom_y (w);
31967 struct glyph_row *row;
31968 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31970 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31971 r.x, r.y, r.width, r.height));
31973 /* Convert to window coordinates. */
31974 r.x -= WINDOW_LEFT_EDGE_X (w);
31975 r.y -= WINDOW_TOP_EDGE_Y (w);
31977 /* Turn off the cursor. */
31978 bool cursor_cleared_p = (!w->pseudo_window_p
31979 && phys_cursor_in_rect_p (w, &r));
31980 if (cursor_cleared_p)
31981 x_clear_cursor (w);
31983 /* If the row containing the cursor extends face to end of line,
31984 then expose_area might overwrite the cursor outside the
31985 rectangle and thus notice_overwritten_cursor might clear
31986 w->phys_cursor_on_p. We remember the original value and
31987 check later if it is changed. */
31988 bool phys_cursor_on_p = w->phys_cursor_on_p;
31990 /* Use a signed int intermediate value to avoid catastrophic
31991 failures due to comparison between signed and unsigned, when
31992 y0 or y1 is negative (can happen for tall images). */
31993 int r_bottom = r.y + r.height;
31995 /* Update lines intersecting rectangle R. */
31996 first_overlapping_row = last_overlapping_row = NULL;
31997 for (row = w->current_matrix->rows;
31998 row->enabled_p;
31999 ++row)
32001 int y0 = row->y;
32002 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32004 if ((y0 >= r.y && y0 < r_bottom)
32005 || (y1 > r.y && y1 < r_bottom)
32006 || (r.y >= y0 && r.y < y1)
32007 || (r_bottom > y0 && r_bottom < y1))
32009 /* A header line may be overlapping, but there is no need
32010 to fix overlapping areas for them. KFS 2005-02-12 */
32011 if (row->overlapping_p && !row->mode_line_p)
32013 if (first_overlapping_row == NULL)
32014 first_overlapping_row = row;
32015 last_overlapping_row = row;
32018 row->clip = fr;
32019 if (expose_line (w, row, &r))
32020 mouse_face_overwritten_p = true;
32021 row->clip = NULL;
32023 else if (row->overlapping_p)
32025 /* We must redraw a row overlapping the exposed area. */
32026 if (y0 < r.y
32027 ? y0 + row->phys_height > r.y
32028 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32030 if (first_overlapping_row == NULL)
32031 first_overlapping_row = row;
32032 last_overlapping_row = row;
32036 if (y1 >= yb)
32037 break;
32040 /* Display the mode line if there is one. */
32041 if (window_wants_mode_line (w)
32042 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32043 row->enabled_p)
32044 && row->y < r_bottom)
32046 if (expose_line (w, row, &r))
32047 mouse_face_overwritten_p = true;
32050 if (!w->pseudo_window_p)
32052 /* Fix the display of overlapping rows. */
32053 if (first_overlapping_row)
32054 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32055 fr);
32057 /* Draw border between windows. */
32058 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32059 x_draw_right_divider (w);
32060 else
32061 x_draw_vertical_border (w);
32063 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32064 x_draw_bottom_divider (w);
32066 /* Turn the cursor on again. */
32067 if (cursor_cleared_p
32068 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32069 update_window_cursor (w, true);
32073 return mouse_face_overwritten_p;
32078 /* Redraw (parts) of all windows in the window tree rooted at W that
32079 intersect R. R contains frame pixel coordinates. Value is
32080 true if the exposure overwrites mouse-face. */
32082 static bool
32083 expose_window_tree (struct window *w, XRectangle *r)
32085 struct frame *f = XFRAME (w->frame);
32086 bool mouse_face_overwritten_p = false;
32088 while (w && !FRAME_GARBAGED_P (f))
32090 mouse_face_overwritten_p
32091 |= (WINDOWP (w->contents)
32092 ? expose_window_tree (XWINDOW (w->contents), r)
32093 : expose_window (w, r));
32095 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32098 return mouse_face_overwritten_p;
32102 /* EXPORT:
32103 Redisplay an exposed area of frame F. X and Y are the upper-left
32104 corner of the exposed rectangle. W and H are width and height of
32105 the exposed area. All are pixel values. W or H zero means redraw
32106 the entire frame. */
32108 void
32109 expose_frame (struct frame *f, int x, int y, int w, int h)
32111 XRectangle r;
32112 bool mouse_face_overwritten_p = false;
32114 TRACE ((stderr, "expose_frame "));
32116 /* No need to redraw if frame will be redrawn soon. */
32117 if (FRAME_GARBAGED_P (f))
32119 TRACE ((stderr, " garbaged\n"));
32120 return;
32123 /* If basic faces haven't been realized yet, there is no point in
32124 trying to redraw anything. This can happen when we get an expose
32125 event while Emacs is starting, e.g. by moving another window. */
32126 if (FRAME_FACE_CACHE (f) == NULL
32127 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32129 TRACE ((stderr, " no faces\n"));
32130 return;
32133 if (w == 0 || h == 0)
32135 r.x = r.y = 0;
32136 r.width = FRAME_TEXT_WIDTH (f);
32137 r.height = FRAME_TEXT_HEIGHT (f);
32139 else
32141 r.x = x;
32142 r.y = y;
32143 r.width = w;
32144 r.height = h;
32147 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32148 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32150 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32151 if (WINDOWP (f->tool_bar_window))
32152 mouse_face_overwritten_p
32153 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32154 #endif
32156 #ifdef HAVE_X_WINDOWS
32157 #ifndef MSDOS
32158 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32159 if (WINDOWP (f->menu_bar_window))
32160 mouse_face_overwritten_p
32161 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32162 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32163 #endif
32164 #endif
32166 /* Some window managers support a focus-follows-mouse style with
32167 delayed raising of frames. Imagine a partially obscured frame,
32168 and moving the mouse into partially obscured mouse-face on that
32169 frame. The visible part of the mouse-face will be highlighted,
32170 then the WM raises the obscured frame. With at least one WM, KDE
32171 2.1, Emacs is not getting any event for the raising of the frame
32172 (even tried with SubstructureRedirectMask), only Expose events.
32173 These expose events will draw text normally, i.e. not
32174 highlighted. Which means we must redo the highlight here.
32175 Subsume it under ``we love X''. --gerd 2001-08-15 */
32176 /* Included in Windows version because Windows most likely does not
32177 do the right thing if any third party tool offers
32178 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32179 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32182 if (f == hlinfo->mouse_face_mouse_frame)
32184 int mouse_x = hlinfo->mouse_face_mouse_x;
32185 int mouse_y = hlinfo->mouse_face_mouse_y;
32186 clear_mouse_face (hlinfo);
32187 note_mouse_highlight (f, mouse_x, mouse_y);
32193 /* EXPORT:
32194 Determine the intersection of two rectangles R1 and R2. Return
32195 the intersection in *RESULT. Value is true if RESULT is not
32196 empty. */
32198 bool
32199 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32201 XRectangle *left, *right;
32202 XRectangle *upper, *lower;
32203 bool intersection_p = false;
32205 /* Rearrange so that R1 is the left-most rectangle. */
32206 if (r1->x < r2->x)
32207 left = r1, right = r2;
32208 else
32209 left = r2, right = r1;
32211 /* X0 of the intersection is right.x0, if this is inside R1,
32212 otherwise there is no intersection. */
32213 if (right->x <= left->x + left->width)
32215 result->x = right->x;
32217 /* The right end of the intersection is the minimum of
32218 the right ends of left and right. */
32219 result->width = (min (left->x + left->width, right->x + right->width)
32220 - result->x);
32222 /* Same game for Y. */
32223 if (r1->y < r2->y)
32224 upper = r1, lower = r2;
32225 else
32226 upper = r2, lower = r1;
32228 /* The upper end of the intersection is lower.y0, if this is inside
32229 of upper. Otherwise, there is no intersection. */
32230 if (lower->y <= upper->y + upper->height)
32232 result->y = lower->y;
32234 /* The lower end of the intersection is the minimum of the lower
32235 ends of upper and lower. */
32236 result->height = (min (lower->y + lower->height,
32237 upper->y + upper->height)
32238 - result->y);
32239 intersection_p = true;
32243 return intersection_p;
32246 #endif /* HAVE_WINDOW_SYSTEM */
32249 /***********************************************************************
32250 Initialization
32251 ***********************************************************************/
32253 void
32254 syms_of_xdisp (void)
32256 Vwith_echo_area_save_vector = Qnil;
32257 staticpro (&Vwith_echo_area_save_vector);
32259 Vmessage_stack = Qnil;
32260 staticpro (&Vmessage_stack);
32262 /* Non-nil means don't actually do any redisplay. */
32263 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32265 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32267 DEFVAR_BOOL("inhibit-message", inhibit_message,
32268 doc: /* Non-nil means calls to `message' are not displayed.
32269 They are still logged to the *Messages* buffer. */);
32270 inhibit_message = 0;
32272 message_dolog_marker1 = Fmake_marker ();
32273 staticpro (&message_dolog_marker1);
32274 message_dolog_marker2 = Fmake_marker ();
32275 staticpro (&message_dolog_marker2);
32276 message_dolog_marker3 = Fmake_marker ();
32277 staticpro (&message_dolog_marker3);
32279 defsubr (&Sset_buffer_redisplay);
32280 #ifdef GLYPH_DEBUG
32281 defsubr (&Sdump_frame_glyph_matrix);
32282 defsubr (&Sdump_glyph_matrix);
32283 defsubr (&Sdump_glyph_row);
32284 defsubr (&Sdump_tool_bar_row);
32285 defsubr (&Strace_redisplay);
32286 defsubr (&Strace_to_stderr);
32287 #endif
32288 #ifdef HAVE_WINDOW_SYSTEM
32289 defsubr (&Stool_bar_height);
32290 defsubr (&Slookup_image_map);
32291 #endif
32292 defsubr (&Sline_pixel_height);
32293 defsubr (&Sformat_mode_line);
32294 defsubr (&Sinvisible_p);
32295 defsubr (&Scurrent_bidi_paragraph_direction);
32296 defsubr (&Swindow_text_pixel_size);
32297 defsubr (&Smove_point_visually);
32298 defsubr (&Sbidi_find_overridden_directionality);
32300 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32301 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32302 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32303 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32304 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32305 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32306 DEFSYM (Qeval, "eval");
32307 DEFSYM (QCdata, ":data");
32309 /* Names of text properties relevant for redisplay. */
32310 DEFSYM (Qdisplay, "display");
32311 DEFSYM (Qspace_width, "space-width");
32312 DEFSYM (Qraise, "raise");
32313 DEFSYM (Qslice, "slice");
32314 DEFSYM (Qspace, "space");
32315 DEFSYM (Qmargin, "margin");
32316 DEFSYM (Qpointer, "pointer");
32317 DEFSYM (Qleft_margin, "left-margin");
32318 DEFSYM (Qright_margin, "right-margin");
32319 DEFSYM (Qcenter, "center");
32320 DEFSYM (Qline_height, "line-height");
32321 DEFSYM (QCalign_to, ":align-to");
32322 DEFSYM (QCrelative_width, ":relative-width");
32323 DEFSYM (QCrelative_height, ":relative-height");
32324 DEFSYM (QCeval, ":eval");
32325 DEFSYM (QCpropertize, ":propertize");
32326 DEFSYM (QCfile, ":file");
32327 DEFSYM (Qfontified, "fontified");
32328 DEFSYM (Qfontification_functions, "fontification-functions");
32330 /* Name of the symbol which disables Lisp evaluation in 'display'
32331 properties. This is used by enriched.el. */
32332 DEFSYM (Qdisable_eval, "disable-eval");
32334 /* Name of the face used to highlight trailing whitespace. */
32335 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32337 /* Names of the faces used to display line numbers. */
32338 DEFSYM (Qline_number, "line-number");
32339 DEFSYM (Qline_number_current_line, "line-number-current-line");
32340 /* Name of a text property which disables line-number display. */
32341 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32343 /* Name and number of the face used to highlight escape glyphs. */
32344 DEFSYM (Qescape_glyph, "escape-glyph");
32346 /* Name and number of the face used to highlight non-breaking
32347 spaces/hyphens. */
32348 DEFSYM (Qnobreak_space, "nobreak-space");
32349 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32351 /* The symbol 'image' which is the car of the lists used to represent
32352 images in Lisp. Also a tool bar style. */
32353 DEFSYM (Qimage, "image");
32355 /* Tool bar styles. */
32356 DEFSYM (Qtext, "text");
32357 DEFSYM (Qboth, "both");
32358 DEFSYM (Qboth_horiz, "both-horiz");
32359 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32361 /* The image map types. */
32362 DEFSYM (QCmap, ":map");
32363 DEFSYM (QCpointer, ":pointer");
32364 DEFSYM (Qrect, "rect");
32365 DEFSYM (Qcircle, "circle");
32366 DEFSYM (Qpoly, "poly");
32368 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32370 DEFSYM (Qgrow_only, "grow-only");
32371 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32372 DEFSYM (Qposition, "position");
32373 DEFSYM (Qbuffer_position, "buffer-position");
32374 DEFSYM (Qobject, "object");
32376 /* Cursor shapes. */
32377 DEFSYM (Qbar, "bar");
32378 DEFSYM (Qhbar, "hbar");
32379 DEFSYM (Qbox, "box");
32380 DEFSYM (Qhollow, "hollow");
32382 /* Pointer shapes. */
32383 DEFSYM (Qhand, "hand");
32384 DEFSYM (Qarrow, "arrow");
32385 /* also Qtext */
32387 DEFSYM (Qdragging, "dragging");
32389 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32391 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32392 staticpro (&list_of_error);
32394 /* Values of those variables at last redisplay are stored as
32395 properties on 'overlay-arrow-position' symbol. However, if
32396 Voverlay_arrow_position is a marker, last-arrow-position is its
32397 numerical position. */
32398 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32399 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32401 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32402 properties on a symbol in overlay-arrow-variable-list. */
32403 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32404 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32406 echo_buffer[0] = echo_buffer[1] = Qnil;
32407 staticpro (&echo_buffer[0]);
32408 staticpro (&echo_buffer[1]);
32410 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32411 staticpro (&echo_area_buffer[0]);
32412 staticpro (&echo_area_buffer[1]);
32414 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32415 staticpro (&Vmessages_buffer_name);
32417 mode_line_proptrans_alist = Qnil;
32418 staticpro (&mode_line_proptrans_alist);
32419 mode_line_string_list = Qnil;
32420 staticpro (&mode_line_string_list);
32421 mode_line_string_face = Qnil;
32422 staticpro (&mode_line_string_face);
32423 mode_line_string_face_prop = Qnil;
32424 staticpro (&mode_line_string_face_prop);
32425 Vmode_line_unwind_vector = Qnil;
32426 staticpro (&Vmode_line_unwind_vector);
32428 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32430 help_echo_string = Qnil;
32431 staticpro (&help_echo_string);
32432 help_echo_object = Qnil;
32433 staticpro (&help_echo_object);
32434 help_echo_window = Qnil;
32435 staticpro (&help_echo_window);
32436 previous_help_echo_string = Qnil;
32437 staticpro (&previous_help_echo_string);
32438 help_echo_pos = -1;
32440 DEFSYM (Qright_to_left, "right-to-left");
32441 DEFSYM (Qleft_to_right, "left-to-right");
32442 defsubr (&Sbidi_resolved_levels);
32444 #ifdef HAVE_WINDOW_SYSTEM
32445 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32446 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32447 For example, if a block cursor is over a tab, it will be drawn as
32448 wide as that tab on the display. */);
32449 x_stretch_cursor_p = 0;
32450 #endif
32452 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32453 doc: /* Non-nil means highlight trailing whitespace.
32454 The face used for trailing whitespace is `trailing-whitespace'. */);
32455 Vshow_trailing_whitespace = Qnil;
32457 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32458 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32459 If the value is t, Emacs highlights non-ASCII chars which have the
32460 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32461 or `nobreak-hyphen' face respectively.
32463 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32464 U+2011 (non-breaking hyphen) are affected.
32466 Any other non-nil value means to display these characters as a escape
32467 glyph followed by an ordinary space or hyphen.
32469 A value of nil means no special handling of these characters. */);
32470 Vnobreak_char_display = Qt;
32472 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32473 doc: /* The pointer shape to show in void text areas.
32474 A value of nil means to show the text pointer. Other options are
32475 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32476 `hourglass'. */);
32477 Vvoid_text_area_pointer = Qarrow;
32479 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32480 doc: /* Non-nil means don't actually do any redisplay.
32481 This is used for internal purposes. */);
32482 Vinhibit_redisplay = Qnil;
32484 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32485 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32486 Vglobal_mode_string = Qnil;
32488 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32489 doc: /* Marker for where to display an arrow on top of the buffer text.
32490 This must be the beginning of a line in order to work.
32491 See also `overlay-arrow-string'. */);
32492 Voverlay_arrow_position = Qnil;
32494 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32495 doc: /* String to display as an arrow in non-window frames.
32496 See also `overlay-arrow-position'. */);
32497 Voverlay_arrow_string = build_pure_c_string ("=>");
32499 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32500 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32501 The symbols on this list are examined during redisplay to determine
32502 where to display overlay arrows. */);
32503 Voverlay_arrow_variable_list
32504 = list1 (intern_c_string ("overlay-arrow-position"));
32506 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32507 doc: /* The number of lines to try scrolling a window by when point moves out.
32508 If that fails to bring point back on frame, point is centered instead.
32509 If this is zero, point is always centered after it moves off frame.
32510 If you want scrolling to always be a line at a time, you should set
32511 `scroll-conservatively' to a large value rather than set this to 1. */);
32513 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32514 doc: /* Scroll up to this many lines, to bring point back on screen.
32515 If point moves off-screen, redisplay will scroll by up to
32516 `scroll-conservatively' lines in order to bring point just barely
32517 onto the screen again. If that cannot be done, then redisplay
32518 recenters point as usual.
32520 If the value is greater than 100, redisplay will never recenter point,
32521 but will always scroll just enough text to bring point into view, even
32522 if you move far away.
32524 A value of zero means always recenter point if it moves off screen. */);
32525 scroll_conservatively = 0;
32527 DEFVAR_INT ("scroll-margin", scroll_margin,
32528 doc: /* Number of lines of margin at the top and bottom of a window.
32529 Recenter the window whenever point gets within this many lines
32530 of the top or bottom of the window. */);
32531 scroll_margin = 0;
32533 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32534 doc: /* Maximum effective value of `scroll-margin'.
32535 Given as a fraction of the current window's lines. The value should
32536 be a floating point number between 0.0 and 0.5. The effective maximum
32537 is limited to (/ (1- window-lines) 2). Non-float values for this
32538 variable are ignored and the default 0.25 is used instead. */);
32539 Vmaximum_scroll_margin = make_float (0.25);
32541 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32542 doc: /* Pixels per inch value for non-window system displays.
32543 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32544 Vdisplay_pixels_per_inch = make_float (72.0);
32546 #ifdef GLYPH_DEBUG
32547 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32548 #endif
32550 DEFVAR_LISP ("truncate-partial-width-windows",
32551 Vtruncate_partial_width_windows,
32552 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32553 For an integer value, truncate lines in each window narrower than the
32554 full frame width, provided the total window width in column units is less
32555 than that integer; otherwise, respect the value of `truncate-lines'.
32556 The total width of the window is as returned by `window-total-width', it
32557 includes the fringes, the continuation and truncation glyphs, the
32558 display margins (if any), and the scroll bar
32560 For any other non-nil value, truncate lines in all windows that do
32561 not span the full frame width.
32563 A value of nil means to respect the value of `truncate-lines'.
32565 If `word-wrap' is enabled, you might want to reduce this. */);
32566 Vtruncate_partial_width_windows = make_number (50);
32568 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32569 doc: /* Maximum buffer size for which line number should be displayed.
32570 If the buffer is bigger than this, the line number does not appear
32571 in the mode line. A value of nil means no limit. */);
32572 Vline_number_display_limit = Qnil;
32574 DEFVAR_INT ("line-number-display-limit-width",
32575 line_number_display_limit_width,
32576 doc: /* Maximum line width (in characters) for line number display.
32577 If the average length of the lines near point is bigger than this, then the
32578 line number may be omitted from the mode line. */);
32579 line_number_display_limit_width = 200;
32581 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32582 doc: /* Non-nil means highlight region even in nonselected windows. */);
32583 highlight_nonselected_windows = false;
32585 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32586 doc: /* Non-nil if more than one frame is visible on this display.
32587 Minibuffer-only frames don't count, but iconified frames do.
32588 This variable is not guaranteed to be accurate except while processing
32589 `frame-title-format' and `icon-title-format'. */);
32591 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32592 doc: /* Template for displaying the title bar of visible frames.
32593 \(Assuming the window manager supports this feature.)
32595 This variable has the same structure as `mode-line-format', except that
32596 the %c, %C, and %l constructs are ignored. It is used only on frames for
32597 which no explicit name has been set (see `modify-frame-parameters'). */);
32599 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32600 doc: /* Template for displaying the title bar of an iconified frame.
32601 \(Assuming the window manager supports this feature.)
32602 This variable has the same structure as `mode-line-format' (which see),
32603 and is used only on frames for which no explicit name has been set
32604 \(see `modify-frame-parameters'). */);
32605 Vicon_title_format
32606 = Vframe_title_format
32607 = listn (CONSTYPE_PURE, 3,
32608 intern_c_string ("multiple-frames"),
32609 build_pure_c_string ("%b"),
32610 listn (CONSTYPE_PURE, 4,
32611 empty_unibyte_string,
32612 intern_c_string ("invocation-name"),
32613 build_pure_c_string ("@"),
32614 intern_c_string ("system-name")));
32616 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32617 doc: /* Maximum number of lines to keep in the message log buffer.
32618 If nil, disable message logging. If t, log messages but don't truncate
32619 the buffer when it becomes large. */);
32620 Vmessage_log_max = make_number (1000);
32622 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32623 doc: /* List of functions to call before redisplaying a window with scrolling.
32624 Each function is called with two arguments, the window and its new
32625 display-start position.
32626 These functions are called whenever the `window-start' marker is modified,
32627 either to point into another buffer (e.g. via `set-window-buffer') or another
32628 place in the same buffer.
32629 When each function is called, the `window-start' marker of its window
32630 argument has been already set to the new value, and the buffer which that
32631 window will display is set to be the current buffer.
32632 Note that the value of `window-end' is not valid when these functions are
32633 called.
32635 Warning: Do not use this feature to alter the way the window
32636 is scrolled. It is not designed for that, and such use probably won't
32637 work. */);
32638 Vwindow_scroll_functions = Qnil;
32640 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32641 doc: /* Functions called when redisplay of a window reaches the end trigger.
32642 Each function is called with two arguments, the window and the end trigger value.
32643 See `set-window-redisplay-end-trigger'. */);
32644 Vredisplay_end_trigger_functions = Qnil;
32646 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32647 doc: /* Non-nil means autoselect window with mouse pointer.
32648 If nil, do not autoselect windows.
32649 A positive number means delay autoselection by that many seconds: a
32650 window is autoselected only after the mouse has remained in that
32651 window for the duration of the delay.
32652 A negative number has a similar effect, but causes windows to be
32653 autoselected only after the mouse has stopped moving. (Because of
32654 the way Emacs compares mouse events, you will occasionally wait twice
32655 that time before the window gets selected.)
32656 Any other value means to autoselect window instantaneously when the
32657 mouse pointer enters it.
32659 Autoselection selects the minibuffer only if it is active, and never
32660 unselects the minibuffer if it is active.
32662 When customizing this variable make sure that the actual value of
32663 `focus-follows-mouse' matches the behavior of your window manager. */);
32664 Vmouse_autoselect_window = Qnil;
32666 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32667 doc: /* Non-nil means automatically resize tool-bars.
32668 This dynamically changes the tool-bar's height to the minimum height
32669 that is needed to make all tool-bar items visible.
32670 If value is `grow-only', the tool-bar's height is only increased
32671 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32672 Vauto_resize_tool_bars = Qt;
32674 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32675 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32676 auto_raise_tool_bar_buttons_p = true;
32678 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32679 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32680 make_cursor_line_fully_visible_p = true;
32682 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32683 doc: /* Border below tool-bar in pixels.
32684 If an integer, use it as the height of the border.
32685 If it is one of `internal-border-width' or `border-width', use the
32686 value of the corresponding frame parameter.
32687 Otherwise, no border is added below the tool-bar. */);
32688 Vtool_bar_border = Qinternal_border_width;
32690 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32691 doc: /* Margin around tool-bar buttons in pixels.
32692 If an integer, use that for both horizontal and vertical margins.
32693 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32694 HORZ specifying the horizontal margin, and VERT specifying the
32695 vertical margin. */);
32696 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32698 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32699 doc: /* Relief thickness of tool-bar buttons. */);
32700 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32702 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32703 doc: /* Tool bar style to use.
32704 It can be one of
32705 image - show images only
32706 text - show text only
32707 both - show both, text below image
32708 both-horiz - show text to the right of the image
32709 text-image-horiz - show text to the left of the image
32710 any other - use system default or image if no system default.
32712 This variable only affects the GTK+ toolkit version of Emacs. */);
32713 Vtool_bar_style = Qnil;
32715 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32716 doc: /* Maximum number of characters a label can have to be shown.
32717 The tool bar style must also show labels for this to have any effect, see
32718 `tool-bar-style'. */);
32719 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32721 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32722 doc: /* List of functions to call to fontify regions of text.
32723 Each function is called with one argument POS. Functions must
32724 fontify a region starting at POS in the current buffer, and give
32725 fontified regions the property `fontified'. */);
32726 Vfontification_functions = Qnil;
32727 Fmake_variable_buffer_local (Qfontification_functions);
32729 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32730 unibyte_display_via_language_environment,
32731 doc: /* Non-nil means display unibyte text according to language environment.
32732 Specifically, this means that raw bytes in the range 160-255 decimal
32733 are displayed by converting them to the equivalent multibyte characters
32734 according to the current language environment. As a result, they are
32735 displayed according to the current fontset.
32737 Note that this variable affects only how these bytes are displayed,
32738 but does not change the fact they are interpreted as raw bytes. */);
32739 unibyte_display_via_language_environment = false;
32741 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32742 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32743 If a float, it specifies a fraction of the mini-window frame's height.
32744 If an integer, it specifies a number of lines. */);
32745 Vmax_mini_window_height = make_float (0.25);
32747 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32748 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32749 A value of nil means don't automatically resize mini-windows.
32750 A value of t means resize them to fit the text displayed in them.
32751 A value of `grow-only', the default, means let mini-windows grow only;
32752 they return to their normal size when the minibuffer is closed, or the
32753 echo area becomes empty. */);
32754 /* Contrary to the doc string, we initialize this to nil, so that
32755 loading loadup.el won't try to resize windows before loading
32756 window.el, where some functions we need to call for this live.
32757 We assign the 'grow-only' value right after loading window.el
32758 during loadup. */
32759 Vresize_mini_windows = Qnil;
32761 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32762 doc: /* Alist specifying how to blink the cursor off.
32763 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32764 `cursor-type' frame-parameter or variable equals ON-STATE,
32765 comparing using `equal', Emacs uses OFF-STATE to specify
32766 how to blink it off. ON-STATE and OFF-STATE are values for
32767 the `cursor-type' frame parameter.
32769 If a frame's ON-STATE has no entry in this list,
32770 the frame's other specifications determine how to blink the cursor off. */);
32771 Vblink_cursor_alist = Qnil;
32773 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32774 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32775 The value `current-line' means the line displaying point in each window
32776 is automatically scrolled horizontally to make point visible.
32777 Any other non-nil value means all the lines in a window are automatically
32778 scrolled horizontally to make point visible. */);
32779 automatic_hscrolling = Qt;
32780 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32781 DEFSYM (Qcurrent_line, "current-line");
32783 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32784 doc: /* How many columns away from the window edge point is allowed to get
32785 before automatic hscrolling will horizontally scroll the window. */);
32786 hscroll_margin = 5;
32788 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32789 doc: /* How many columns to scroll the window when point gets too close to the edge.
32790 When point is less than `hscroll-margin' columns from the window
32791 edge, automatic hscrolling will scroll the window by the amount of columns
32792 determined by this variable. If its value is a positive integer, scroll that
32793 many columns. If it's a positive floating-point number, it specifies the
32794 fraction of the window's width to scroll. If it's nil or zero, point will be
32795 centered horizontally after the scroll. Any other value, including negative
32796 numbers, are treated as if the value were zero.
32798 Automatic hscrolling always moves point outside the scroll margin, so if
32799 point was more than scroll step columns inside the margin, the window will
32800 scroll more than the value given by the scroll step.
32802 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32803 and `scroll-right' overrides this variable's effect. */);
32804 Vhscroll_step = make_number (0);
32806 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32807 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32808 Bind this around calls to `message' to let it take effect. */);
32809 message_truncate_lines = false;
32811 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32812 doc: /* Normal hook run to update the menu bar definitions.
32813 Redisplay runs this hook before it redisplays the menu bar.
32814 This is used to update menus such as Buffers, whose contents depend on
32815 various data. */);
32816 Vmenu_bar_update_hook = Qnil;
32818 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32819 doc: /* Frame for which we are updating a menu.
32820 The enable predicate for a menu binding should check this variable. */);
32821 Vmenu_updating_frame = Qnil;
32823 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32824 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32825 inhibit_menubar_update = false;
32827 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32828 doc: /* Prefix prepended to all continuation lines at display time.
32829 The value may be a string, an image, or a stretch-glyph; it is
32830 interpreted in the same way as the value of a `display' text property.
32832 This variable is overridden by any `wrap-prefix' text or overlay
32833 property.
32835 To add a prefix to non-continuation lines, use `line-prefix'. */);
32836 Vwrap_prefix = Qnil;
32837 DEFSYM (Qwrap_prefix, "wrap-prefix");
32838 Fmake_variable_buffer_local (Qwrap_prefix);
32840 DEFVAR_LISP ("line-prefix", Vline_prefix,
32841 doc: /* Prefix prepended to all non-continuation lines at display time.
32842 The value may be a string, an image, or a stretch-glyph; it is
32843 interpreted in the same way as the value of a `display' text property.
32845 This variable is overridden by any `line-prefix' text or overlay
32846 property.
32848 To add a prefix to continuation lines, use `wrap-prefix'. */);
32849 Vline_prefix = Qnil;
32850 DEFSYM (Qline_prefix, "line-prefix");
32851 Fmake_variable_buffer_local (Qline_prefix);
32853 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32854 doc: /* Non-nil means display line numbers.
32855 If the value is t, display the absolute number of each line of a buffer
32856 shown in a window. Absolute line numbers count from the beginning of
32857 the current narrowing, or from buffer beginning. If the value is
32858 `relative', display for each line not containing the window's point its
32859 relative number instead, i.e. the number of the line relative to the
32860 line showing the window's point.
32862 In either case, line numbers are displayed at the beginning of each
32863 non-continuation line that displays buffer text, i.e. after each newline
32864 character that comes from the buffer. The value `visual' is like
32865 `relative' but counts screen lines instead of buffer lines. In practice
32866 this means that continuation lines count as well when calculating the
32867 relative number of a line.
32869 Lisp programs can disable display of a line number of a particular
32870 buffer line by putting the `display-line-numbers-disable' text property
32871 or overlay property on the first visible character of that line. */);
32872 Vdisplay_line_numbers = Qnil;
32873 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32874 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32875 DEFSYM (Qrelative, "relative");
32876 DEFSYM (Qvisual, "visual");
32878 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32879 doc: /* Minimum width of space reserved for line number display.
32880 A positive number means reserve that many columns for line numbers,
32881 even if the actual number needs less space.
32882 The default value of nil means compute the space dynamically.
32883 Any other value is treated as nil. */);
32884 Vdisplay_line_numbers_width = Qnil;
32885 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32886 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32888 DEFVAR_LISP ("display-line-numbers-current-absolute",
32889 Vdisplay_line_numbers_current_absolute,
32890 doc: /* Non-nil means display absolute number of current line.
32891 This variable has effect only when `display-line-numbers' is
32892 either `relative' or `visual'. */);
32893 Vdisplay_line_numbers_current_absolute = Qt;
32895 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32896 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32897 display_line_numbers_widen = false;
32898 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32899 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32901 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32902 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32903 inhibit_eval_during_redisplay = false;
32905 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32906 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32907 inhibit_free_realized_faces = false;
32909 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32910 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32911 Intended for use during debugging and for testing bidi display;
32912 see biditest.el in the test suite. */);
32913 inhibit_bidi_mirroring = false;
32915 #ifdef GLYPH_DEBUG
32916 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32917 doc: /* Inhibit try_window_id display optimization. */);
32918 inhibit_try_window_id = false;
32920 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32921 doc: /* Inhibit try_window_reusing display optimization. */);
32922 inhibit_try_window_reusing = false;
32924 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32925 doc: /* Inhibit try_cursor_movement display optimization. */);
32926 inhibit_try_cursor_movement = false;
32927 #endif /* GLYPH_DEBUG */
32929 DEFVAR_INT ("overline-margin", overline_margin,
32930 doc: /* Space between overline and text, in pixels.
32931 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32932 margin to the character height. */);
32933 overline_margin = 2;
32935 DEFVAR_INT ("underline-minimum-offset",
32936 underline_minimum_offset,
32937 doc: /* Minimum distance between baseline and underline.
32938 This can improve legibility of underlined text at small font sizes,
32939 particularly when using variable `x-use-underline-position-properties'
32940 with fonts that specify an UNDERLINE_POSITION relatively close to the
32941 baseline. The default value is 1. */);
32942 underline_minimum_offset = 1;
32944 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32945 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32946 This feature only works when on a window system that can change
32947 cursor shapes. */);
32948 display_hourglass_p = true;
32950 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32951 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32952 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32954 #ifdef HAVE_WINDOW_SYSTEM
32955 hourglass_atimer = NULL;
32956 hourglass_shown_p = false;
32957 #endif /* HAVE_WINDOW_SYSTEM */
32959 /* Name of the face used to display glyphless characters. */
32960 DEFSYM (Qglyphless_char, "glyphless-char");
32962 /* Method symbols for Vglyphless_char_display. */
32963 DEFSYM (Qhex_code, "hex-code");
32964 DEFSYM (Qempty_box, "empty-box");
32965 DEFSYM (Qthin_space, "thin-space");
32966 DEFSYM (Qzero_width, "zero-width");
32968 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32969 doc: /* Function run just before redisplay.
32970 It is called with one argument, which is the set of windows that are to
32971 be redisplayed. This set can be nil (meaning, only the selected window),
32972 or t (meaning all windows). */);
32973 Vpre_redisplay_function = intern ("ignore");
32975 /* Symbol for the purpose of Vglyphless_char_display. */
32976 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32977 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32979 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32980 doc: /* Char-table defining glyphless characters.
32981 Each element, if non-nil, should be one of the following:
32982 an ASCII acronym string: display this string in a box
32983 `hex-code': display the hexadecimal code of a character in a box
32984 `empty-box': display as an empty box
32985 `thin-space': display as 1-pixel width space
32986 `zero-width': don't display
32987 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32988 display method for graphical terminals and text terminals respectively.
32989 GRAPHICAL and TEXT should each have one of the values listed above.
32991 The char-table has one extra slot to control the display of a character for
32992 which no font is found. This slot only takes effect on graphical terminals.
32993 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32994 `thin-space'. The default is `empty-box'.
32996 If a character has a non-nil entry in an active display table, the
32997 display table takes effect; in this case, Emacs does not consult
32998 `glyphless-char-display' at all. */);
32999 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33000 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33001 Qempty_box);
33003 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33004 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33005 Vdebug_on_message = Qnil;
33007 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33008 doc: /* */);
33009 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33011 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33012 doc: /* */);
33013 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33015 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33016 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33017 /* Initialize to t, since we need to disable reordering until
33018 loadup.el successfully loads charprop.el. */
33019 redisplay__inhibit_bidi = true;
33021 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33022 doc: /* Non-nil means display raw bytes in hexadecimal format.
33023 The default is to use octal format (\200) whereas hexadecimal (\x80)
33024 may be more familiar to users. */);
33025 display_raw_bytes_as_hex = false;
33030 /* Initialize this module when Emacs starts. */
33032 void
33033 init_xdisp (void)
33035 CHARPOS (this_line_start_pos) = 0;
33037 if (!noninteractive)
33039 struct window *m = XWINDOW (minibuf_window);
33040 Lisp_Object frame = m->frame;
33041 struct frame *f = XFRAME (frame);
33042 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33043 struct window *r = XWINDOW (root);
33044 int i;
33046 echo_area_window = minibuf_window;
33048 r->top_line = FRAME_TOP_MARGIN (f);
33049 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33050 r->total_cols = FRAME_COLS (f);
33051 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33052 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33053 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33055 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33056 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33057 m->total_cols = FRAME_COLS (f);
33058 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33059 m->total_lines = 1;
33060 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33062 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33063 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33064 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33066 /* The default ellipsis glyphs `...'. */
33067 for (i = 0; i < 3; ++i)
33068 default_invis_vector[i] = make_number ('.');
33072 /* Allocate the buffer for frame titles.
33073 Also used for `format-mode-line'. */
33074 int size = 100;
33075 mode_line_noprop_buf = xmalloc (size);
33076 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33077 mode_line_noprop_ptr = mode_line_noprop_buf;
33078 mode_line_target = MODE_LINE_DISPLAY;
33081 help_echo_showing_p = false;
33084 #ifdef HAVE_WINDOW_SYSTEM
33086 /* Platform-independent portion of hourglass implementation. */
33088 /* Timer function of hourglass_atimer. */
33090 static void
33091 show_hourglass (struct atimer *timer)
33093 /* The timer implementation will cancel this timer automatically
33094 after this function has run. Set hourglass_atimer to null
33095 so that we know the timer doesn't have to be canceled. */
33096 hourglass_atimer = NULL;
33098 if (!hourglass_shown_p)
33100 Lisp_Object tail, frame;
33102 block_input ();
33104 FOR_EACH_FRAME (tail, frame)
33106 struct frame *f = XFRAME (frame);
33108 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33109 && FRAME_RIF (f)->show_hourglass)
33110 FRAME_RIF (f)->show_hourglass (f);
33113 hourglass_shown_p = true;
33114 unblock_input ();
33118 /* Cancel a currently active hourglass timer, and start a new one. */
33120 void
33121 start_hourglass (void)
33123 struct timespec delay;
33125 cancel_hourglass ();
33127 if (INTEGERP (Vhourglass_delay)
33128 && XINT (Vhourglass_delay) > 0)
33129 delay = make_timespec (min (XINT (Vhourglass_delay),
33130 TYPE_MAXIMUM (time_t)),
33132 else if (FLOATP (Vhourglass_delay)
33133 && XFLOAT_DATA (Vhourglass_delay) > 0)
33134 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33135 else
33136 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33138 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33139 show_hourglass, NULL);
33142 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33143 shown. */
33145 void
33146 cancel_hourglass (void)
33148 if (hourglass_atimer)
33150 cancel_atimer (hourglass_atimer);
33151 hourglass_atimer = NULL;
33154 if (hourglass_shown_p)
33156 Lisp_Object tail, frame;
33158 block_input ();
33160 FOR_EACH_FRAME (tail, frame)
33162 struct frame *f = XFRAME (frame);
33164 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33165 && FRAME_RIF (f)->hide_hourglass)
33166 FRAME_RIF (f)->hide_hourglass (f);
33167 #ifdef HAVE_NTGUI
33168 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33169 else if (!FRAME_W32_P (f))
33170 w32_arrow_cursor ();
33171 #endif
33174 hourglass_shown_p = false;
33175 unblock_input ();
33179 #endif /* HAVE_WINDOW_SYSTEM */