Avoid crashes due to invalid error forms from sentinels/filters
[emacs.git] / src / xdisp.c
blob8ca9037a00d4cab2affc41c856d9f54f8b199758
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2017 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <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,
880 Lisp_Object, Lisp_Object,
881 struct text_pos *, ptrdiff_t, int, bool);
882 static int underlying_face_id (struct it *);
884 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
885 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
887 #ifdef HAVE_WINDOW_SYSTEM
889 static void update_tool_bar (struct frame *, bool);
890 static void x_draw_bottom_divider (struct window *w);
891 static void notice_overwritten_cursor (struct window *,
892 enum glyph_row_area,
893 int, int, int, int);
894 static int normal_char_height (struct font *, int);
895 static void normal_char_ascent_descent (struct font *, int, int *, int *);
897 static void append_stretch_glyph (struct it *, Lisp_Object,
898 int, int, int);
900 static Lisp_Object get_it_property (struct it *, Lisp_Object);
901 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
902 struct font *, int, bool);
904 #endif /* HAVE_WINDOW_SYSTEM */
906 static void produce_special_glyphs (struct it *, enum display_element_type);
907 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
908 static bool coords_in_mouse_face_p (struct window *, int, int);
912 /***********************************************************************
913 Window display dimensions
914 ***********************************************************************/
916 /* Return the bottom boundary y-position for text lines in window W.
917 This is the first y position at which a line cannot start.
918 It is relative to the top of the window.
920 This is the height of W minus the height of a mode line, if any. */
923 window_text_bottom_y (struct window *w)
925 int height = WINDOW_PIXEL_HEIGHT (w);
927 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
929 if (window_wants_mode_line (w))
930 height -= CURRENT_MODE_LINE_HEIGHT (w);
932 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
934 return height;
937 /* Return the pixel width of display area AREA of window W.
938 ANY_AREA means return the total width of W, not including
939 fringes to the left and right of the window. */
942 window_box_width (struct window *w, enum glyph_row_area area)
944 int width = w->pixel_width;
946 if (!w->pseudo_window_p)
948 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
949 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
951 if (area == TEXT_AREA)
952 width -= (WINDOW_MARGINS_WIDTH (w)
953 + WINDOW_FRINGES_WIDTH (w));
954 else if (area == LEFT_MARGIN_AREA)
955 width = WINDOW_LEFT_MARGIN_WIDTH (w);
956 else if (area == RIGHT_MARGIN_AREA)
957 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
960 /* With wide margins, fringes, etc. we might end up with a negative
961 width, correct that here. */
962 return max (0, width);
966 /* Return the pixel height of the display area of window W, not
967 including mode lines of W, if any. */
970 window_box_height (struct window *w)
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_PIXEL_HEIGHT (w);
975 eassert (height >= 0);
977 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
978 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
980 /* Note: the code below that determines the mode-line/header-line
981 height is essentially the same as that contained in the macro
982 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
983 the appropriate glyph row has its `mode_line_p' flag set,
984 and if it doesn't, uses estimate_mode_line_height instead. */
986 if (window_wants_mode_line (w))
988 struct glyph_row *ml_row
989 = (w->current_matrix && w->current_matrix->rows
990 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
991 : 0);
992 if (ml_row && ml_row->mode_line_p)
993 height -= ml_row->height;
994 else
995 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
998 if (window_wants_header_line (w))
1000 struct glyph_row *hl_row
1001 = (w->current_matrix && w->current_matrix->rows
1002 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1003 : 0);
1004 if (hl_row && hl_row->mode_line_p)
1005 height -= hl_row->height;
1006 else
1007 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1010 /* With a very small font and a mode-line that's taller than
1011 default, we might end up with a negative height. */
1012 return max (0, height);
1015 /* Return the window-relative coordinate of the left edge of display
1016 area AREA of window W. ANY_AREA means return the left edge of the
1017 whole window, to the right of the left fringe of W. */
1020 window_box_left_offset (struct window *w, enum glyph_row_area area)
1022 int x;
1024 if (w->pseudo_window_p)
1025 return 0;
1027 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1031 + window_box_width (w, LEFT_MARGIN_AREA));
1032 else if (area == RIGHT_MARGIN_AREA)
1033 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1034 + window_box_width (w, LEFT_MARGIN_AREA)
1035 + window_box_width (w, TEXT_AREA)
1036 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1038 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1039 else if (area == LEFT_MARGIN_AREA
1040 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1041 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1043 /* Don't return more than the window's pixel width. */
1044 return min (x, w->pixel_width);
1048 /* Return the window-relative coordinate of the right edge of display
1049 area AREA of window W. ANY_AREA means return the right edge of the
1050 whole window, to the left of the right fringe of W. */
1052 static int
1053 window_box_right_offset (struct window *w, enum glyph_row_area area)
1055 /* Don't return more than the window's pixel width. */
1056 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1057 w->pixel_width);
1060 /* Return the frame-relative coordinate of the left edge of display
1061 area AREA of window W. ANY_AREA means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1065 window_box_left (struct window *w, enum glyph_row_area area)
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1076 return x;
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. ANY_AREA means return the right edge of the
1082 whole window, to the left of the right fringe of W. */
1085 window_box_right (struct window *w, enum glyph_row_area area)
1087 return window_box_left (w, area) + window_box_width (w, area);
1090 /* Get the bounding box of the display area AREA of window W, without
1091 mode lines, in frame-relative coordinates. ANY_AREA means the
1092 whole window, not including the left and right fringes of
1093 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1094 coordinates of the upper-left corner of the box. Return in
1095 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1097 void
1098 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1099 int *box_y, int *box_width, int *box_height)
1101 if (box_width)
1102 *box_width = window_box_width (w, area);
1103 if (box_height)
1104 *box_height = window_box_height (w);
1105 if (box_x)
1106 *box_x = window_box_left (w, area);
1107 if (box_y)
1109 *box_y = WINDOW_TOP_EDGE_Y (w);
1110 if (window_wants_header_line (w))
1111 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1115 #ifdef HAVE_WINDOW_SYSTEM
1117 /* Get the bounding box of the display area AREA of window W, without
1118 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1119 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1120 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1121 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1122 box. */
1124 static void
1125 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1126 int *bottom_right_x, int *bottom_right_y)
1128 window_box (w, ANY_AREA, top_left_x, top_left_y,
1129 bottom_right_x, bottom_right_y);
1130 *bottom_right_x += *top_left_x;
1131 *bottom_right_y += *top_left_y;
1134 #endif /* HAVE_WINDOW_SYSTEM */
1136 /***********************************************************************
1137 Utilities
1138 ***********************************************************************/
1140 /* Return the bottom y-position of the line the iterator IT is in.
1141 This can modify IT's settings. */
1144 line_bottom_y (struct it *it)
1146 int line_height = it->max_ascent + it->max_descent;
1147 int line_top_y = it->current_y;
1149 if (line_height == 0)
1151 if (last_height)
1152 line_height = last_height;
1153 else if (IT_CHARPOS (*it) < ZV)
1155 move_it_by_lines (it, 1);
1156 line_height = (it->max_ascent || it->max_descent
1157 ? it->max_ascent + it->max_descent
1158 : last_height);
1160 else
1162 struct glyph_row *row = it->glyph_row;
1164 /* Use the default character height. */
1165 it->glyph_row = NULL;
1166 it->what = IT_CHARACTER;
1167 it->c = ' ';
1168 it->len = 1;
1169 PRODUCE_GLYPHS (it);
1170 line_height = it->ascent + it->descent;
1171 it->glyph_row = row;
1175 return line_top_y + line_height;
1178 DEFUN ("line-pixel-height", Fline_pixel_height,
1179 Sline_pixel_height, 0, 0, 0,
1180 doc: /* Return height in pixels of text line in the selected window.
1182 Value is the height in pixels of the line at point. */)
1183 (void)
1185 struct it it;
1186 struct text_pos pt;
1187 struct window *w = XWINDOW (selected_window);
1188 struct buffer *old_buffer = NULL;
1189 Lisp_Object result;
1191 if (XBUFFER (w->contents) != current_buffer)
1193 old_buffer = current_buffer;
1194 set_buffer_internal_1 (XBUFFER (w->contents));
1196 SET_TEXT_POS (pt, PT, PT_BYTE);
1197 start_display (&it, w, pt);
1198 /* 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;
4752 if (CONSP (spec)
4753 /* Simple specifications. */
4754 && !EQ (XCAR (spec), Qimage)
4755 #ifdef HAVE_XWIDGETS
4756 && !EQ (XCAR (spec), Qxwidget)
4757 #endif
4758 && !EQ (XCAR (spec), Qspace)
4759 && !EQ (XCAR (spec), Qwhen)
4760 && !EQ (XCAR (spec), Qslice)
4761 && !EQ (XCAR (spec), Qspace_width)
4762 && !EQ (XCAR (spec), Qheight)
4763 && !EQ (XCAR (spec), Qraise)
4764 /* Marginal area specifications. */
4765 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4766 && !EQ (XCAR (spec), Qleft_fringe)
4767 && !EQ (XCAR (spec), Qright_fringe)
4768 && !NILP (XCAR (spec)))
4770 for (; CONSP (spec); spec = XCDR (spec))
4772 int rv = handle_single_display_spec (it, XCAR (spec), object,
4773 overlay, position, bufpos,
4774 replacing, frame_window_p);
4775 if (rv != 0)
4777 replacing = rv;
4778 /* If some text in a string is replaced, `position' no
4779 longer points to the position of `object'. */
4780 if (!it || STRINGP (object))
4781 break;
4785 else if (VECTORP (spec))
4787 ptrdiff_t i;
4788 for (i = 0; i < ASIZE (spec); ++i)
4790 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4791 overlay, position, bufpos,
4792 replacing, frame_window_p);
4793 if (rv != 0)
4795 replacing = rv;
4796 /* If some text in a string is replaced, `position' no
4797 longer points to the position of `object'. */
4798 if (!it || STRINGP (object))
4799 break;
4803 else
4804 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4805 bufpos, 0, frame_window_p);
4806 return replacing;
4809 /* Value is the position of the end of the `display' property starting
4810 at START_POS in OBJECT. */
4812 static struct text_pos
4813 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4815 Lisp_Object end;
4816 struct text_pos end_pos;
4818 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4819 Qdisplay, object, Qnil);
4820 CHARPOS (end_pos) = XFASTINT (end);
4821 if (STRINGP (object))
4822 compute_string_pos (&end_pos, start_pos, it->string);
4823 else
4824 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4826 return end_pos;
4830 /* Set up IT from a single `display' property specification SPEC. OBJECT
4831 is the object in which the `display' property was found. *POSITION
4832 is the position in OBJECT at which the `display' property was found.
4833 BUFPOS is the buffer position of OBJECT (different from POSITION if
4834 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4835 previously saw a display specification which already replaced text
4836 display with something else, for example an image; we ignore such
4837 properties after the first one has been processed.
4839 OVERLAY is the overlay this `display' property came from,
4840 or nil if it was a text property.
4842 If SPEC is a `space' or `image' specification, and in some other
4843 cases too, set *POSITION to the position where the `display'
4844 property ends.
4846 If IT is NULL, only examine the property specification in SPEC, but
4847 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4848 is intended to be displayed in a window on a GUI frame.
4850 Value is non-zero if something was found which replaces the display
4851 of buffer or string text. */
4853 static int
4854 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4855 Lisp_Object overlay, struct text_pos *position,
4856 ptrdiff_t bufpos, int display_replaced,
4857 bool frame_window_p)
4859 Lisp_Object form;
4860 Lisp_Object location, value;
4861 struct text_pos start_pos = *position;
4863 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4864 If the result is non-nil, use VALUE instead of SPEC. */
4865 form = Qt;
4866 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4868 spec = XCDR (spec);
4869 if (!CONSP (spec))
4870 return 0;
4871 form = XCAR (spec);
4872 spec = XCDR (spec);
4875 if (!NILP (form) && !EQ (form, Qt))
4877 ptrdiff_t count = SPECPDL_INDEX ();
4879 /* Bind `object' to the object having the `display' property, a
4880 buffer or string. Bind `position' to the position in the
4881 object where the property was found, and `buffer-position'
4882 to the current position in the buffer. */
4884 if (NILP (object))
4885 XSETBUFFER (object, current_buffer);
4886 specbind (Qobject, object);
4887 specbind (Qposition, make_number (CHARPOS (*position)));
4888 specbind (Qbuffer_position, make_number (bufpos));
4889 form = safe_eval (form);
4890 unbind_to (count, Qnil);
4893 if (NILP (form))
4894 return 0;
4896 /* Handle `(height HEIGHT)' specifications. */
4897 if (CONSP (spec)
4898 && EQ (XCAR (spec), Qheight)
4899 && CONSP (XCDR (spec)))
4901 if (it)
4903 if (!FRAME_WINDOW_P (it->f))
4904 return 0;
4906 it->font_height = XCAR (XCDR (spec));
4907 if (!NILP (it->font_height))
4909 int new_height = -1;
4911 if (CONSP (it->font_height)
4912 && (EQ (XCAR (it->font_height), Qplus)
4913 || EQ (XCAR (it->font_height), Qminus))
4914 && CONSP (XCDR (it->font_height))
4915 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4917 /* `(+ N)' or `(- N)' where N is an integer. */
4918 int steps = XINT (XCAR (XCDR (it->font_height)));
4919 if (EQ (XCAR (it->font_height), Qplus))
4920 steps = - steps;
4921 it->face_id = smaller_face (it->f, it->face_id, steps);
4923 else if (FUNCTIONP (it->font_height))
4925 /* Call function with current height as argument.
4926 Value is the new height. */
4927 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4928 Lisp_Object height;
4929 height = safe_call1 (it->font_height,
4930 face->lface[LFACE_HEIGHT_INDEX]);
4931 if (NUMBERP (height))
4932 new_height = XFLOATINT (height);
4934 else if (NUMBERP (it->font_height))
4936 /* Value is a multiple of the canonical char height. */
4937 struct face *f;
4939 f = FACE_FROM_ID (it->f,
4940 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4941 new_height = (XFLOATINT (it->font_height)
4942 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4944 else
4946 /* Evaluate IT->font_height with `height' bound to the
4947 current specified height to get the new height. */
4948 ptrdiff_t count = SPECPDL_INDEX ();
4949 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4951 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4952 value = safe_eval (it->font_height);
4953 unbind_to (count, Qnil);
4955 if (NUMBERP (value))
4956 new_height = XFLOATINT (value);
4959 if (new_height > 0)
4960 it->face_id = face_with_height (it->f, it->face_id, new_height);
4964 return 0;
4967 /* Handle `(space-width WIDTH)'. */
4968 if (CONSP (spec)
4969 && EQ (XCAR (spec), Qspace_width)
4970 && CONSP (XCDR (spec)))
4972 if (it)
4974 if (!FRAME_WINDOW_P (it->f))
4975 return 0;
4977 value = XCAR (XCDR (spec));
4978 if (NUMBERP (value) && XFLOATINT (value) > 0)
4979 it->space_width = value;
4982 return 0;
4985 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4986 if (CONSP (spec)
4987 && EQ (XCAR (spec), Qslice))
4989 Lisp_Object tem;
4991 if (it)
4993 if (!FRAME_WINDOW_P (it->f))
4994 return 0;
4996 if (tem = XCDR (spec), CONSP (tem))
4998 it->slice.x = XCAR (tem);
4999 if (tem = XCDR (tem), CONSP (tem))
5001 it->slice.y = XCAR (tem);
5002 if (tem = XCDR (tem), CONSP (tem))
5004 it->slice.width = XCAR (tem);
5005 if (tem = XCDR (tem), CONSP (tem))
5006 it->slice.height = XCAR (tem);
5012 return 0;
5015 /* Handle `(raise FACTOR)'. */
5016 if (CONSP (spec)
5017 && EQ (XCAR (spec), Qraise)
5018 && CONSP (XCDR (spec)))
5020 if (it)
5022 if (!FRAME_WINDOW_P (it->f))
5023 return 0;
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 value = XCAR (XCDR (spec));
5027 if (NUMBERP (value))
5029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5030 it->voffset = - (XFLOATINT (value)
5031 * (normal_char_height (face->font, -1)));
5033 #endif /* HAVE_WINDOW_SYSTEM */
5036 return 0;
5039 /* Don't handle the other kinds of display specifications
5040 inside a string that we got from a `display' property. */
5041 if (it && it->string_from_display_prop_p)
5042 return 0;
5044 /* Characters having this form of property are not displayed, so
5045 we have to find the end of the property. */
5046 if (it)
5048 start_pos = *position;
5049 *position = display_prop_end (it, object, start_pos);
5050 /* If the display property comes from an overlay, don't consider
5051 any potential stop_charpos values before the end of that
5052 overlay. Since display_prop_end will happily find another
5053 'display' property coming from some other overlay or text
5054 property on buffer positions before this overlay's end, we
5055 need to ignore them, or else we risk displaying this
5056 overlay's display string/image twice. */
5057 if (!NILP (overlay))
5059 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5061 /* Some borderline-sane Lisp might call us with the current
5062 buffer narrowed so that overlay-end is outside the
5063 POINT_MIN..POINT_MAX region, which will then cause
5064 various assertion violations and crashes down the road,
5065 starting with pop_it when it will attempt to use POSITION
5066 set below. Prevent that. */
5067 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5069 if (ovendpos > CHARPOS (*position))
5070 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5073 value = Qnil;
5075 /* Stop the scan at that end position--we assume that all
5076 text properties change there. */
5077 if (it)
5078 it->stop_charpos = position->charpos;
5080 /* Handle `(left-fringe BITMAP [FACE])'
5081 and `(right-fringe BITMAP [FACE])'. */
5082 if (CONSP (spec)
5083 && (EQ (XCAR (spec), Qleft_fringe)
5084 || EQ (XCAR (spec), Qright_fringe))
5085 && CONSP (XCDR (spec)))
5087 if (it)
5089 if (!FRAME_WINDOW_P (it->f))
5090 /* If we return here, POSITION has been advanced
5091 across the text with this property. */
5093 /* Synchronize the bidi iterator with POSITION. This is
5094 needed because we are not going to push the iterator
5095 on behalf of this display property, so there will be
5096 no pop_it call to do this synchronization for us. */
5097 if (it->bidi_p)
5099 it->position = *position;
5100 iterate_out_of_display_property (it);
5101 *position = it->position;
5103 return 1;
5106 else if (!frame_window_p)
5107 return 1;
5109 #ifdef HAVE_WINDOW_SYSTEM
5110 value = XCAR (XCDR (spec));
5111 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5112 if (! fringe_bitmap)
5113 /* If we return here, POSITION has been advanced
5114 across the text with this property. */
5116 if (it && it->bidi_p)
5118 it->position = *position;
5119 iterate_out_of_display_property (it);
5120 *position = it->position;
5122 return 1;
5125 if (it)
5127 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5129 if (CONSP (XCDR (XCDR (spec))))
5131 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5132 int face_id2 = lookup_derived_face (it->f, face_name,
5133 FRINGE_FACE_ID, false);
5134 if (face_id2 >= 0)
5135 face_id = face_id2;
5138 /* Save current settings of IT so that we can restore them
5139 when we are finished with the glyph property value. */
5140 push_it (it, position);
5142 it->area = TEXT_AREA;
5143 it->what = IT_IMAGE;
5144 it->image_id = -1; /* no image */
5145 it->position = start_pos;
5146 it->object = NILP (object) ? it->w->contents : object;
5147 it->method = GET_FROM_IMAGE;
5148 it->from_overlay = Qnil;
5149 it->face_id = face_id;
5150 it->from_disp_prop_p = true;
5152 /* Say that we haven't consumed the characters with
5153 `display' property yet. The call to pop_it in
5154 set_iterator_to_next will clean this up. */
5155 *position = start_pos;
5157 if (EQ (XCAR (spec), Qleft_fringe))
5159 it->left_user_fringe_bitmap = fringe_bitmap;
5160 it->left_user_fringe_face_id = face_id;
5162 else
5164 it->right_user_fringe_bitmap = fringe_bitmap;
5165 it->right_user_fringe_face_id = face_id;
5168 #endif /* HAVE_WINDOW_SYSTEM */
5169 return 1;
5172 /* Prepare to handle `((margin left-margin) ...)',
5173 `((margin right-margin) ...)' and `((margin nil) ...)'
5174 prefixes for display specifications. */
5175 location = Qunbound;
5176 if (CONSP (spec) && CONSP (XCAR (spec)))
5178 Lisp_Object tem;
5180 value = XCDR (spec);
5181 if (CONSP (value))
5182 value = XCAR (value);
5184 tem = XCAR (spec);
5185 if (EQ (XCAR (tem), Qmargin)
5186 && (tem = XCDR (tem),
5187 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5188 (NILP (tem)
5189 || EQ (tem, Qleft_margin)
5190 || EQ (tem, Qright_margin))))
5191 location = tem;
5194 if (EQ (location, Qunbound))
5196 location = Qnil;
5197 value = spec;
5200 /* After this point, VALUE is the property after any
5201 margin prefix has been stripped. It must be a string,
5202 an image specification, or `(space ...)'.
5204 LOCATION specifies where to display: `left-margin',
5205 `right-margin' or nil. */
5207 bool valid_p = (STRINGP (value)
5208 #ifdef HAVE_WINDOW_SYSTEM
5209 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5210 && valid_image_p (value))
5211 #endif /* not HAVE_WINDOW_SYSTEM */
5212 || (CONSP (value) && EQ (XCAR (value), Qspace))
5213 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5214 && valid_xwidget_spec_p (value)));
5216 if (valid_p && display_replaced == 0)
5218 int retval = 1;
5220 if (!it)
5222 /* Callers need to know whether the display spec is any kind
5223 of `(space ...)' spec that is about to affect text-area
5224 display. */
5225 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5226 retval = 2;
5227 return retval;
5230 /* Save current settings of IT so that we can restore them
5231 when we are finished with the glyph property value. */
5232 push_it (it, position);
5233 it->from_overlay = overlay;
5234 it->from_disp_prop_p = true;
5236 if (NILP (location))
5237 it->area = TEXT_AREA;
5238 else if (EQ (location, Qleft_margin))
5239 it->area = LEFT_MARGIN_AREA;
5240 else
5241 it->area = RIGHT_MARGIN_AREA;
5243 if (STRINGP (value))
5245 it->string = value;
5246 it->multibyte_p = STRING_MULTIBYTE (it->string);
5247 it->current.overlay_string_index = -1;
5248 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5249 it->end_charpos = it->string_nchars = SCHARS (it->string);
5250 it->method = GET_FROM_STRING;
5251 it->stop_charpos = 0;
5252 it->prev_stop = 0;
5253 it->base_level_stop = 0;
5254 it->string_from_display_prop_p = true;
5255 it->cmp_it.id = -1;
5256 /* Say that we haven't consumed the characters with
5257 `display' property yet. The call to pop_it in
5258 set_iterator_to_next will clean this up. */
5259 if (BUFFERP (object))
5260 *position = start_pos;
5262 /* Force paragraph direction to be that of the parent
5263 object. If the parent object's paragraph direction is
5264 not yet determined, default to L2R. */
5265 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5266 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5267 else
5268 it->paragraph_embedding = L2R;
5270 /* Set up the bidi iterator for this display string. */
5271 if (it->bidi_p)
5273 it->bidi_it.string.lstring = it->string;
5274 it->bidi_it.string.s = NULL;
5275 it->bidi_it.string.schars = it->end_charpos;
5276 it->bidi_it.string.bufpos = bufpos;
5277 it->bidi_it.string.from_disp_str = true;
5278 it->bidi_it.string.unibyte = !it->multibyte_p;
5279 it->bidi_it.w = it->w;
5280 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5283 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5285 it->method = GET_FROM_STRETCH;
5286 it->object = value;
5287 *position = it->position = start_pos;
5288 retval = 1 + (it->area == TEXT_AREA);
5290 else if (valid_xwidget_spec_p (value))
5292 it->what = IT_XWIDGET;
5293 it->method = GET_FROM_XWIDGET;
5294 it->position = start_pos;
5295 it->object = NILP (object) ? it->w->contents : object;
5296 *position = start_pos;
5297 it->xwidget = lookup_xwidget (value);
5299 #ifdef HAVE_WINDOW_SYSTEM
5300 else
5302 it->what = IT_IMAGE;
5303 it->image_id = lookup_image (it->f, value);
5304 it->position = start_pos;
5305 it->object = NILP (object) ? it->w->contents : object;
5306 it->method = GET_FROM_IMAGE;
5308 /* Say that we haven't consumed the characters with
5309 `display' property yet. The call to pop_it in
5310 set_iterator_to_next will clean this up. */
5311 *position = start_pos;
5313 #endif /* HAVE_WINDOW_SYSTEM */
5315 return retval;
5318 /* Invalid property or property not supported. Restore
5319 POSITION to what it was before. */
5320 *position = start_pos;
5321 return 0;
5324 /* Check if PROP is a display property value whose text should be
5325 treated as intangible. OVERLAY is the overlay from which PROP
5326 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5327 specify the buffer position covered by PROP. */
5329 bool
5330 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5331 ptrdiff_t charpos, ptrdiff_t bytepos)
5333 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5334 struct text_pos position;
5336 SET_TEXT_POS (position, charpos, bytepos);
5337 return (handle_display_spec (NULL, prop, Qnil, overlay,
5338 &position, charpos, frame_window_p)
5339 != 0);
5343 /* Return true if PROP is a display sub-property value containing STRING.
5345 Implementation note: this and the following function are really
5346 special cases of handle_display_spec and
5347 handle_single_display_spec, and should ideally use the same code.
5348 Until they do, these two pairs must be consistent and must be
5349 modified in sync. */
5351 static bool
5352 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5354 if (EQ (string, prop))
5355 return true;
5357 /* Skip over `when FORM'. */
5358 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5360 prop = XCDR (prop);
5361 if (!CONSP (prop))
5362 return false;
5363 /* Actually, the condition following `when' should be eval'ed,
5364 like handle_single_display_spec does, and we should return
5365 false if it evaluates to nil. However, this function is
5366 called only when the buffer was already displayed and some
5367 glyph in the glyph matrix was found to come from a display
5368 string. Therefore, the condition was already evaluated, and
5369 the result was non-nil, otherwise the display string wouldn't
5370 have been displayed and we would have never been called for
5371 this property. Thus, we can skip the evaluation and assume
5372 its result is non-nil. */
5373 prop = XCDR (prop);
5376 if (CONSP (prop))
5377 /* Skip over `margin LOCATION'. */
5378 if (EQ (XCAR (prop), Qmargin))
5380 prop = XCDR (prop);
5381 if (!CONSP (prop))
5382 return false;
5384 prop = XCDR (prop);
5385 if (!CONSP (prop))
5386 return false;
5389 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5393 /* Return true if STRING appears in the `display' property PROP. */
5395 static bool
5396 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5398 if (CONSP (prop)
5399 && !EQ (XCAR (prop), Qwhen)
5400 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5402 /* A list of sub-properties. */
5403 while (CONSP (prop))
5405 if (single_display_spec_string_p (XCAR (prop), string))
5406 return true;
5407 prop = XCDR (prop);
5410 else if (VECTORP (prop))
5412 /* A vector of sub-properties. */
5413 ptrdiff_t i;
5414 for (i = 0; i < ASIZE (prop); ++i)
5415 if (single_display_spec_string_p (AREF (prop, i), string))
5416 return true;
5418 else
5419 return single_display_spec_string_p (prop, string);
5421 return false;
5424 /* Look for STRING in overlays and text properties in the current
5425 buffer, between character positions FROM and TO (excluding TO).
5426 BACK_P means look back (in this case, TO is supposed to be
5427 less than FROM).
5428 Value is the first character position where STRING was found, or
5429 zero if it wasn't found before hitting TO.
5431 This function may only use code that doesn't eval because it is
5432 called asynchronously from note_mouse_highlight. */
5434 static ptrdiff_t
5435 string_buffer_position_lim (Lisp_Object string,
5436 ptrdiff_t from, ptrdiff_t to, bool back_p)
5438 Lisp_Object limit, prop, pos;
5439 bool found = false;
5441 pos = make_number (max (from, BEGV));
5443 if (!back_p) /* looking forward */
5445 limit = make_number (min (to, ZV));
5446 while (!found && !EQ (pos, limit))
5448 prop = Fget_char_property (pos, Qdisplay, Qnil);
5449 if (!NILP (prop) && display_prop_string_p (prop, string))
5450 found = true;
5451 else
5452 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5453 limit);
5456 else /* looking back */
5458 limit = make_number (max (to, BEGV));
5459 while (!found && !EQ (pos, limit))
5461 prop = Fget_char_property (pos, Qdisplay, Qnil);
5462 if (!NILP (prop) && display_prop_string_p (prop, string))
5463 found = true;
5464 else
5465 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5466 limit);
5470 return found ? XINT (pos) : 0;
5473 /* Determine which buffer position in current buffer STRING comes from.
5474 AROUND_CHARPOS is an approximate position where it could come from.
5475 Value is the buffer position or 0 if it couldn't be determined.
5477 This function is necessary because we don't record buffer positions
5478 in glyphs generated from strings (to keep struct glyph small).
5479 This function may only use code that doesn't eval because it is
5480 called asynchronously from note_mouse_highlight. */
5482 static ptrdiff_t
5483 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5485 const int MAX_DISTANCE = 1000;
5486 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5487 around_charpos + MAX_DISTANCE,
5488 false);
5490 if (!found)
5491 found = string_buffer_position_lim (string, around_charpos,
5492 around_charpos - MAX_DISTANCE, true);
5493 return found;
5498 /***********************************************************************
5499 `composition' property
5500 ***********************************************************************/
5502 /* Set up iterator IT from `composition' property at its current
5503 position. Called from handle_stop. */
5505 static enum prop_handled
5506 handle_composition_prop (struct it *it)
5508 Lisp_Object prop, string;
5509 ptrdiff_t pos, pos_byte, start, end;
5511 if (STRINGP (it->string))
5513 unsigned char *s;
5515 pos = IT_STRING_CHARPOS (*it);
5516 pos_byte = IT_STRING_BYTEPOS (*it);
5517 string = it->string;
5518 s = SDATA (string) + pos_byte;
5519 it->c = STRING_CHAR (s);
5521 else
5523 pos = IT_CHARPOS (*it);
5524 pos_byte = IT_BYTEPOS (*it);
5525 string = Qnil;
5526 it->c = FETCH_CHAR (pos_byte);
5529 /* If there's a valid composition and point is not inside of the
5530 composition (in the case that the composition is from the current
5531 buffer), draw a glyph composed from the composition components. */
5532 if (find_composition (pos, -1, &start, &end, &prop, string)
5533 && composition_valid_p (start, end, prop)
5534 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5536 if (start < pos)
5537 /* As we can't handle this situation (perhaps font-lock added
5538 a new composition), we just return here hoping that next
5539 redisplay will detect this composition much earlier. */
5540 return HANDLED_NORMALLY;
5541 if (start != pos)
5543 if (STRINGP (it->string))
5544 pos_byte = string_char_to_byte (it->string, start);
5545 else
5546 pos_byte = CHAR_TO_BYTE (start);
5548 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5549 prop, string);
5551 if (it->cmp_it.id >= 0)
5553 it->cmp_it.ch = -1;
5554 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5555 it->cmp_it.nglyphs = -1;
5559 return HANDLED_NORMALLY;
5564 /***********************************************************************
5565 Overlay strings
5566 ***********************************************************************/
5568 /* The following structure is used to record overlay strings for
5569 later sorting in load_overlay_strings. */
5571 struct overlay_entry
5573 Lisp_Object overlay;
5574 Lisp_Object string;
5575 EMACS_INT priority;
5576 bool after_string_p;
5580 /* Set up iterator IT from overlay strings at its current position.
5581 Called from handle_stop. */
5583 static enum prop_handled
5584 handle_overlay_change (struct it *it)
5586 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5587 return HANDLED_RECOMPUTE_PROPS;
5588 else
5589 return HANDLED_NORMALLY;
5593 /* Set up the next overlay string for delivery by IT, if there is an
5594 overlay string to deliver. Called by set_iterator_to_next when the
5595 end of the current overlay string is reached. If there are more
5596 overlay strings to display, IT->string and
5597 IT->current.overlay_string_index are set appropriately here.
5598 Otherwise IT->string is set to nil. */
5600 static void
5601 next_overlay_string (struct it *it)
5603 ++it->current.overlay_string_index;
5604 if (it->current.overlay_string_index == it->n_overlay_strings)
5606 /* No more overlay strings. Restore IT's settings to what
5607 they were before overlay strings were processed, and
5608 continue to deliver from current_buffer. */
5610 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5611 pop_it (it);
5612 eassert (it->sp > 0
5613 || (NILP (it->string)
5614 && it->method == GET_FROM_BUFFER
5615 && it->stop_charpos >= BEGV
5616 && it->stop_charpos <= it->end_charpos));
5617 it->current.overlay_string_index = -1;
5618 it->n_overlay_strings = 0;
5619 /* If there's an empty display string on the stack, pop the
5620 stack, to resync the bidi iterator with IT's position. Such
5621 empty strings are pushed onto the stack in
5622 get_overlay_strings_1. */
5623 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5624 pop_it (it);
5626 /* Since we've exhausted overlay strings at this buffer
5627 position, set the flag to ignore overlays until we move to
5628 another position. (The flag will be reset in
5629 next_element_from_buffer.) But don't do that if the overlay
5630 strings were loaded at position other than the current one,
5631 which could happen if we called pop_it above, or if the
5632 overlay strings were loaded by handle_invisible_prop at the
5633 beginning of invisible text. */
5634 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5635 it->ignore_overlay_strings_at_pos_p = true;
5637 /* If we're at the end of the buffer, record that we have
5638 processed the overlay strings there already, so that
5639 next_element_from_buffer doesn't try it again. */
5640 if (NILP (it->string)
5641 && IT_CHARPOS (*it) >= it->end_charpos
5642 && it->overlay_strings_charpos >= it->end_charpos)
5643 it->overlay_strings_at_end_processed_p = true;
5644 /* Note: we reset overlay_strings_charpos only here, to make
5645 sure the just-processed overlays were indeed at EOB.
5646 Otherwise, overlays on text with invisible text property,
5647 which are processed with IT's position past the invisible
5648 text, might fool us into thinking the overlays at EOB were
5649 already processed (linum-mode can cause this, for
5650 example). */
5651 it->overlay_strings_charpos = -1;
5653 else
5655 /* There are more overlay strings to process. If
5656 IT->current.overlay_string_index has advanced to a position
5657 where we must load IT->overlay_strings with more strings, do
5658 it. We must load at the IT->overlay_strings_charpos where
5659 IT->n_overlay_strings was originally computed; when invisible
5660 text is present, this might not be IT_CHARPOS (Bug#7016). */
5661 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5663 if (it->current.overlay_string_index && i == 0)
5664 load_overlay_strings (it, it->overlay_strings_charpos);
5666 /* Initialize IT to deliver display elements from the overlay
5667 string. */
5668 it->string = it->overlay_strings[i];
5669 it->multibyte_p = STRING_MULTIBYTE (it->string);
5670 SET_TEXT_POS (it->current.string_pos, 0, 0);
5671 it->method = GET_FROM_STRING;
5672 it->stop_charpos = 0;
5673 it->end_charpos = SCHARS (it->string);
5674 if (it->cmp_it.stop_pos >= 0)
5675 it->cmp_it.stop_pos = 0;
5676 it->prev_stop = 0;
5677 it->base_level_stop = 0;
5679 /* Set up the bidi iterator for this overlay string. */
5680 if (it->bidi_p)
5682 it->bidi_it.string.lstring = it->string;
5683 it->bidi_it.string.s = NULL;
5684 it->bidi_it.string.schars = SCHARS (it->string);
5685 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5686 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5687 it->bidi_it.string.unibyte = !it->multibyte_p;
5688 it->bidi_it.w = it->w;
5689 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5693 CHECK_IT (it);
5697 /* Compare two overlay_entry structures E1 and E2. Used as a
5698 comparison function for qsort in load_overlay_strings. Overlay
5699 strings for the same position are sorted so that
5701 1. All after-strings come in front of before-strings, except
5702 when they come from the same overlay.
5704 2. Within after-strings, strings are sorted so that overlay strings
5705 from overlays with higher priorities come first.
5707 2. Within before-strings, strings are sorted so that overlay
5708 strings from overlays with higher priorities come last.
5710 Value is analogous to strcmp. */
5713 static int
5714 compare_overlay_entries (const void *e1, const void *e2)
5716 struct overlay_entry const *entry1 = e1;
5717 struct overlay_entry const *entry2 = e2;
5718 int result;
5720 if (entry1->after_string_p != entry2->after_string_p)
5722 /* Let after-strings appear in front of before-strings if
5723 they come from different overlays. */
5724 if (EQ (entry1->overlay, entry2->overlay))
5725 result = entry1->after_string_p ? 1 : -1;
5726 else
5727 result = entry1->after_string_p ? -1 : 1;
5729 else if (entry1->priority != entry2->priority)
5731 if (entry1->after_string_p)
5732 /* After-strings sorted in order of decreasing priority. */
5733 result = entry2->priority < entry1->priority ? -1 : 1;
5734 else
5735 /* Before-strings sorted in order of increasing priority. */
5736 result = entry1->priority < entry2->priority ? -1 : 1;
5738 else
5739 result = 0;
5741 return result;
5745 /* Load the vector IT->overlay_strings with overlay strings from IT's
5746 current buffer position, or from CHARPOS if that is > 0. Set
5747 IT->n_overlays to the total number of overlay strings found.
5749 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5750 a time. On entry into load_overlay_strings,
5751 IT->current.overlay_string_index gives the number of overlay
5752 strings that have already been loaded by previous calls to this
5753 function.
5755 IT->add_overlay_start contains an additional overlay start
5756 position to consider for taking overlay strings from, if non-zero.
5757 This position comes into play when the overlay has an `invisible'
5758 property, and both before and after-strings. When we've skipped to
5759 the end of the overlay, because of its `invisible' property, we
5760 nevertheless want its before-string to appear.
5761 IT->add_overlay_start will contain the overlay start position
5762 in this case.
5764 Overlay strings are sorted so that after-string strings come in
5765 front of before-string strings. Within before and after-strings,
5766 strings are sorted by overlay priority. See also function
5767 compare_overlay_entries. */
5769 static void
5770 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5772 Lisp_Object overlay, window, str, invisible;
5773 struct Lisp_Overlay *ov;
5774 ptrdiff_t start, end;
5775 ptrdiff_t n = 0, i, j;
5776 int invis;
5777 struct overlay_entry entriesbuf[20];
5778 ptrdiff_t size = ARRAYELTS (entriesbuf);
5779 struct overlay_entry *entries = entriesbuf;
5780 USE_SAFE_ALLOCA;
5782 if (charpos <= 0)
5783 charpos = IT_CHARPOS (*it);
5785 /* Append the overlay string STRING of overlay OVERLAY to vector
5786 `entries' which has size `size' and currently contains `n'
5787 elements. AFTER_P means STRING is an after-string of
5788 OVERLAY. */
5789 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5790 do \
5792 Lisp_Object priority; \
5794 if (n == size) \
5796 struct overlay_entry *old = entries; \
5797 SAFE_NALLOCA (entries, 2, size); \
5798 memcpy (entries, old, size * sizeof *entries); \
5799 size *= 2; \
5802 entries[n].string = (STRING); \
5803 entries[n].overlay = (OVERLAY); \
5804 priority = Foverlay_get ((OVERLAY), Qpriority); \
5805 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5806 entries[n].after_string_p = (AFTER_P); \
5807 ++n; \
5809 while (false)
5811 /* Process overlay before the overlay center. */
5812 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5814 XSETMISC (overlay, ov);
5815 eassert (OVERLAYP (overlay));
5816 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5817 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5819 if (end < charpos)
5820 break;
5822 /* Skip this overlay if it doesn't start or end at IT's current
5823 position. */
5824 if (end != charpos && start != charpos)
5825 continue;
5827 /* Skip this overlay if it doesn't apply to IT->w. */
5828 window = Foverlay_get (overlay, Qwindow);
5829 if (WINDOWP (window) && XWINDOW (window) != it->w)
5830 continue;
5832 /* If the text ``under'' the overlay is invisible, both before-
5833 and after-strings from this overlay are visible; start and
5834 end position are indistinguishable. */
5835 invisible = Foverlay_get (overlay, Qinvisible);
5836 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5838 /* If overlay has a non-empty before-string, record it. */
5839 if ((start == charpos || (end == charpos && invis != 0))
5840 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5841 && SCHARS (str))
5842 RECORD_OVERLAY_STRING (overlay, str, false);
5844 /* If overlay has a non-empty after-string, record it. */
5845 if ((end == charpos || (start == charpos && invis != 0))
5846 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5847 && SCHARS (str))
5848 RECORD_OVERLAY_STRING (overlay, str, true);
5851 /* Process overlays after the overlay center. */
5852 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5854 XSETMISC (overlay, ov);
5855 eassert (OVERLAYP (overlay));
5856 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5857 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5859 if (start > charpos)
5860 break;
5862 /* Skip this overlay if it doesn't start or end at IT's current
5863 position. */
5864 if (end != charpos && start != charpos)
5865 continue;
5867 /* Skip this overlay if it doesn't apply to IT->w. */
5868 window = Foverlay_get (overlay, Qwindow);
5869 if (WINDOWP (window) && XWINDOW (window) != it->w)
5870 continue;
5872 /* If the text ``under'' the overlay is invisible, it has a zero
5873 dimension, and both before- and after-strings apply. */
5874 invisible = Foverlay_get (overlay, Qinvisible);
5875 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5877 /* If overlay has a non-empty before-string, record it. */
5878 if ((start == charpos || (end == charpos && invis != 0))
5879 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5880 && SCHARS (str))
5881 RECORD_OVERLAY_STRING (overlay, str, false);
5883 /* If overlay has a non-empty after-string, record it. */
5884 if ((end == charpos || (start == charpos && invis != 0))
5885 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5886 && SCHARS (str))
5887 RECORD_OVERLAY_STRING (overlay, str, true);
5890 #undef RECORD_OVERLAY_STRING
5892 /* Sort entries. */
5893 if (n > 1)
5894 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5896 /* Record number of overlay strings, and where we computed it. */
5897 it->n_overlay_strings = n;
5898 it->overlay_strings_charpos = charpos;
5900 /* IT->current.overlay_string_index is the number of overlay strings
5901 that have already been consumed by IT. Copy some of the
5902 remaining overlay strings to IT->overlay_strings. */
5903 i = 0;
5904 j = it->current.overlay_string_index;
5905 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5907 it->overlay_strings[i] = entries[j].string;
5908 it->string_overlays[i++] = entries[j++].overlay;
5911 CHECK_IT (it);
5912 SAFE_FREE ();
5916 /* Get the first chunk of overlay strings at IT's current buffer
5917 position, or at CHARPOS if that is > 0. Value is true if at
5918 least one overlay string was found. */
5920 static bool
5921 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5923 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5924 process. This fills IT->overlay_strings with strings, and sets
5925 IT->n_overlay_strings to the total number of strings to process.
5926 IT->pos.overlay_string_index has to be set temporarily to zero
5927 because load_overlay_strings needs this; it must be set to -1
5928 when no overlay strings are found because a zero value would
5929 indicate a position in the first overlay string. */
5930 it->current.overlay_string_index = 0;
5931 load_overlay_strings (it, charpos);
5933 /* If we found overlay strings, set up IT to deliver display
5934 elements from the first one. Otherwise set up IT to deliver
5935 from current_buffer. */
5936 if (it->n_overlay_strings)
5938 /* Make sure we know settings in current_buffer, so that we can
5939 restore meaningful values when we're done with the overlay
5940 strings. */
5941 if (compute_stop_p)
5942 compute_stop_pos (it);
5943 eassert (it->face_id >= 0);
5945 /* Save IT's settings. They are restored after all overlay
5946 strings have been processed. */
5947 eassert (!compute_stop_p || it->sp == 0);
5949 /* When called from handle_stop, there might be an empty display
5950 string loaded. In that case, don't bother saving it. But
5951 don't use this optimization with the bidi iterator, since we
5952 need the corresponding pop_it call to resync the bidi
5953 iterator's position with IT's position, after we are done
5954 with the overlay strings. (The corresponding call to pop_it
5955 in case of an empty display string is in
5956 next_overlay_string.) */
5957 if (!(!it->bidi_p
5958 && STRINGP (it->string) && !SCHARS (it->string)))
5959 push_it (it, NULL);
5961 /* Set up IT to deliver display elements from the first overlay
5962 string. */
5963 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5964 it->string = it->overlay_strings[0];
5965 it->from_overlay = Qnil;
5966 it->stop_charpos = 0;
5967 eassert (STRINGP (it->string));
5968 it->end_charpos = SCHARS (it->string);
5969 it->prev_stop = 0;
5970 it->base_level_stop = 0;
5971 it->multibyte_p = STRING_MULTIBYTE (it->string);
5972 it->method = GET_FROM_STRING;
5973 it->from_disp_prop_p = 0;
5974 it->cmp_it.id = -1;
5976 /* Force paragraph direction to be that of the parent
5977 buffer. */
5978 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5979 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5980 else
5981 it->paragraph_embedding = L2R;
5983 /* Set up the bidi iterator for this overlay string. */
5984 if (it->bidi_p)
5986 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5988 it->bidi_it.string.lstring = it->string;
5989 it->bidi_it.string.s = NULL;
5990 it->bidi_it.string.schars = SCHARS (it->string);
5991 it->bidi_it.string.bufpos = pos;
5992 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5993 it->bidi_it.string.unibyte = !it->multibyte_p;
5994 it->bidi_it.w = it->w;
5995 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5997 return true;
6000 it->current.overlay_string_index = -1;
6001 return false;
6004 static bool
6005 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6007 it->string = Qnil;
6008 it->method = GET_FROM_BUFFER;
6010 get_overlay_strings_1 (it, charpos, true);
6012 CHECK_IT (it);
6014 /* Value is true if we found at least one overlay string. */
6015 return STRINGP (it->string);
6020 /***********************************************************************
6021 Saving and restoring state
6022 ***********************************************************************/
6024 /* Save current settings of IT on IT->stack. Called, for example,
6025 before setting up IT for an overlay string, to be able to restore
6026 IT's settings to what they were after the overlay string has been
6027 processed. If POSITION is non-NULL, it is the position to save on
6028 the stack instead of IT->position. */
6030 static void
6031 push_it (struct it *it, struct text_pos *position)
6033 struct iterator_stack_entry *p;
6035 eassert (it->sp < IT_STACK_SIZE);
6036 p = it->stack + it->sp;
6038 p->stop_charpos = it->stop_charpos;
6039 p->prev_stop = it->prev_stop;
6040 p->base_level_stop = it->base_level_stop;
6041 p->cmp_it = it->cmp_it;
6042 eassert (it->face_id >= 0);
6043 p->face_id = it->face_id;
6044 p->string = it->string;
6045 p->method = it->method;
6046 p->from_overlay = it->from_overlay;
6047 switch (p->method)
6049 case GET_FROM_IMAGE:
6050 p->u.image.object = it->object;
6051 p->u.image.image_id = it->image_id;
6052 p->u.image.slice = it->slice;
6053 break;
6054 case GET_FROM_STRETCH:
6055 p->u.stretch.object = it->object;
6056 break;
6057 case GET_FROM_XWIDGET:
6058 p->u.xwidget.object = it->object;
6059 break;
6060 case GET_FROM_BUFFER:
6061 case GET_FROM_DISPLAY_VECTOR:
6062 case GET_FROM_STRING:
6063 case GET_FROM_C_STRING:
6064 break;
6065 default:
6066 emacs_abort ();
6068 p->position = position ? *position : it->position;
6069 p->current = it->current;
6070 p->end_charpos = it->end_charpos;
6071 p->string_nchars = it->string_nchars;
6072 p->area = it->area;
6073 p->multibyte_p = it->multibyte_p;
6074 p->avoid_cursor_p = it->avoid_cursor_p;
6075 p->space_width = it->space_width;
6076 p->font_height = it->font_height;
6077 p->voffset = it->voffset;
6078 p->string_from_display_prop_p = it->string_from_display_prop_p;
6079 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6080 p->display_ellipsis_p = false;
6081 p->line_wrap = it->line_wrap;
6082 p->bidi_p = it->bidi_p;
6083 p->paragraph_embedding = it->paragraph_embedding;
6084 p->from_disp_prop_p = it->from_disp_prop_p;
6085 ++it->sp;
6087 /* Save the state of the bidi iterator as well. */
6088 if (it->bidi_p)
6089 bidi_push_it (&it->bidi_it);
6092 static void
6093 iterate_out_of_display_property (struct it *it)
6095 bool buffer_p = !STRINGP (it->string);
6096 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6097 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6099 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6101 /* Maybe initialize paragraph direction. If we are at the beginning
6102 of a new paragraph, next_element_from_buffer may not have a
6103 chance to do that. */
6104 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6105 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6106 /* prev_stop can be zero, so check against BEGV as well. */
6107 while (it->bidi_it.charpos >= bob
6108 && it->prev_stop <= it->bidi_it.charpos
6109 && it->bidi_it.charpos < CHARPOS (it->position)
6110 && it->bidi_it.charpos < eob)
6111 bidi_move_to_visually_next (&it->bidi_it);
6112 /* Record the stop_pos we just crossed, for when we cross it
6113 back, maybe. */
6114 if (it->bidi_it.charpos > CHARPOS (it->position))
6115 it->prev_stop = CHARPOS (it->position);
6116 /* If we ended up not where pop_it put us, resync IT's
6117 positional members with the bidi iterator. */
6118 if (it->bidi_it.charpos != CHARPOS (it->position))
6119 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6120 if (buffer_p)
6121 it->current.pos = it->position;
6122 else
6123 it->current.string_pos = it->position;
6126 /* Restore IT's settings from IT->stack. Called, for example, when no
6127 more overlay strings must be processed, and we return to delivering
6128 display elements from a buffer, or when the end of a string from a
6129 `display' property is reached and we return to delivering display
6130 elements from an overlay string, or from a buffer. */
6132 static void
6133 pop_it (struct it *it)
6135 struct iterator_stack_entry *p;
6136 bool from_display_prop = it->from_disp_prop_p;
6137 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6139 eassert (it->sp > 0);
6140 --it->sp;
6141 p = it->stack + it->sp;
6142 it->stop_charpos = p->stop_charpos;
6143 it->prev_stop = p->prev_stop;
6144 it->base_level_stop = p->base_level_stop;
6145 it->cmp_it = p->cmp_it;
6146 it->face_id = p->face_id;
6147 it->current = p->current;
6148 it->position = p->position;
6149 it->string = p->string;
6150 it->from_overlay = p->from_overlay;
6151 if (NILP (it->string))
6152 SET_TEXT_POS (it->current.string_pos, -1, -1);
6153 it->method = p->method;
6154 switch (it->method)
6156 case GET_FROM_IMAGE:
6157 it->image_id = p->u.image.image_id;
6158 it->object = p->u.image.object;
6159 it->slice = p->u.image.slice;
6160 break;
6161 case GET_FROM_XWIDGET:
6162 it->object = p->u.xwidget.object;
6163 break;
6164 case GET_FROM_STRETCH:
6165 it->object = p->u.stretch.object;
6166 break;
6167 case GET_FROM_BUFFER:
6168 it->object = it->w->contents;
6169 break;
6170 case GET_FROM_STRING:
6172 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6174 /* Restore the face_box_p flag, since it could have been
6175 overwritten by the face of the object that we just finished
6176 displaying. */
6177 if (face)
6178 it->face_box_p = face->box != FACE_NO_BOX;
6179 it->object = it->string;
6181 break;
6182 case GET_FROM_DISPLAY_VECTOR:
6183 if (it->s)
6184 it->method = GET_FROM_C_STRING;
6185 else if (STRINGP (it->string))
6186 it->method = GET_FROM_STRING;
6187 else
6189 it->method = GET_FROM_BUFFER;
6190 it->object = it->w->contents;
6192 break;
6193 case GET_FROM_C_STRING:
6194 break;
6195 default:
6196 emacs_abort ();
6198 it->end_charpos = p->end_charpos;
6199 it->string_nchars = p->string_nchars;
6200 it->area = p->area;
6201 it->multibyte_p = p->multibyte_p;
6202 it->avoid_cursor_p = p->avoid_cursor_p;
6203 it->space_width = p->space_width;
6204 it->font_height = p->font_height;
6205 it->voffset = p->voffset;
6206 it->string_from_display_prop_p = p->string_from_display_prop_p;
6207 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6208 it->line_wrap = p->line_wrap;
6209 it->bidi_p = p->bidi_p;
6210 it->paragraph_embedding = p->paragraph_embedding;
6211 it->from_disp_prop_p = p->from_disp_prop_p;
6212 if (it->bidi_p)
6214 bidi_pop_it (&it->bidi_it);
6215 /* Bidi-iterate until we get out of the portion of text, if any,
6216 covered by a `display' text property or by an overlay with
6217 `display' property. (We cannot just jump there, because the
6218 internal coherency of the bidi iterator state can not be
6219 preserved across such jumps.) We also must determine the
6220 paragraph base direction if the overlay we just processed is
6221 at the beginning of a new paragraph. */
6222 if (from_display_prop
6223 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6224 iterate_out_of_display_property (it);
6226 eassert ((BUFFERP (it->object)
6227 && IT_CHARPOS (*it) == it->bidi_it.charpos
6228 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6229 || (STRINGP (it->object)
6230 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6231 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6232 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6234 /* If we move the iterator over text covered by a display property
6235 to a new buffer position, any info about previously seen overlays
6236 is no longer valid. */
6237 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6238 it->ignore_overlay_strings_at_pos_p = false;
6243 /***********************************************************************
6244 Moving over lines
6245 ***********************************************************************/
6247 /* Set IT's current position to the previous line start. */
6249 static void
6250 back_to_previous_line_start (struct it *it)
6252 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6254 DEC_BOTH (cp, bp);
6255 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6259 /* Move IT to the next line start.
6261 Value is true if a newline was found. Set *SKIPPED_P to true if
6262 we skipped over part of the text (as opposed to moving the iterator
6263 continuously over the text). Otherwise, don't change the value
6264 of *SKIPPED_P.
6266 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6267 iterator on the newline, if it was found.
6269 Newlines may come from buffer text, overlay strings, or strings
6270 displayed via the `display' property. That's the reason we can't
6271 simply use find_newline_no_quit.
6273 Note that this function may not skip over invisible text that is so
6274 because of text properties and immediately follows a newline. If
6275 it would, function reseat_at_next_visible_line_start, when called
6276 from set_iterator_to_next, would effectively make invisible
6277 characters following a newline part of the wrong glyph row, which
6278 leads to wrong cursor motion. */
6280 static bool
6281 forward_to_next_line_start (struct it *it, bool *skipped_p,
6282 struct bidi_it *bidi_it_prev)
6284 ptrdiff_t old_selective;
6285 bool newline_found_p = false;
6286 int n;
6287 const int MAX_NEWLINE_DISTANCE = 500;
6289 /* If already on a newline, just consume it to avoid unintended
6290 skipping over invisible text below. */
6291 if (it->what == IT_CHARACTER
6292 && it->c == '\n'
6293 && CHARPOS (it->position) == IT_CHARPOS (*it))
6295 if (it->bidi_p && bidi_it_prev)
6296 *bidi_it_prev = it->bidi_it;
6297 set_iterator_to_next (it, false);
6298 it->c = 0;
6299 return true;
6302 /* Don't handle selective display in the following. It's (a)
6303 unnecessary because it's done by the caller, and (b) leads to an
6304 infinite recursion because next_element_from_ellipsis indirectly
6305 calls this function. */
6306 old_selective = it->selective;
6307 it->selective = 0;
6309 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6310 from buffer text. */
6311 for (n = 0;
6312 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6313 n += !STRINGP (it->string))
6315 if (!get_next_display_element (it))
6316 return false;
6317 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6318 if (newline_found_p && it->bidi_p && bidi_it_prev)
6319 *bidi_it_prev = it->bidi_it;
6320 set_iterator_to_next (it, false);
6323 /* If we didn't find a newline near enough, see if we can use a
6324 short-cut. */
6325 if (!newline_found_p)
6327 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6328 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6329 1, &bytepos);
6330 Lisp_Object pos;
6332 eassert (!STRINGP (it->string));
6334 /* If there isn't any `display' property in sight, and no
6335 overlays, we can just use the position of the newline in
6336 buffer text. */
6337 if (it->stop_charpos >= limit
6338 || ((pos = Fnext_single_property_change (make_number (start),
6339 Qdisplay, Qnil,
6340 make_number (limit)),
6341 NILP (pos))
6342 && next_overlay_change (start) == ZV))
6344 if (!it->bidi_p)
6346 IT_CHARPOS (*it) = limit;
6347 IT_BYTEPOS (*it) = bytepos;
6349 else
6351 struct bidi_it bprev;
6353 /* Help bidi.c avoid expensive searches for display
6354 properties and overlays, by telling it that there are
6355 none up to `limit'. */
6356 if (it->bidi_it.disp_pos < limit)
6358 it->bidi_it.disp_pos = limit;
6359 it->bidi_it.disp_prop = 0;
6361 do {
6362 bprev = it->bidi_it;
6363 bidi_move_to_visually_next (&it->bidi_it);
6364 } while (it->bidi_it.charpos != limit);
6365 IT_CHARPOS (*it) = limit;
6366 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6367 if (bidi_it_prev)
6368 *bidi_it_prev = bprev;
6370 *skipped_p = newline_found_p = true;
6372 else
6374 while (!newline_found_p)
6376 if (!get_next_display_element (it))
6377 break;
6378 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6379 if (newline_found_p && it->bidi_p && bidi_it_prev)
6380 *bidi_it_prev = it->bidi_it;
6381 set_iterator_to_next (it, false);
6386 it->selective = old_selective;
6387 return newline_found_p;
6391 /* Set IT's current position to the previous visible line start. Skip
6392 invisible text that is so either due to text properties or due to
6393 selective display. Caution: this does not change IT->current_x and
6394 IT->hpos. */
6396 static void
6397 back_to_previous_visible_line_start (struct it *it)
6399 while (IT_CHARPOS (*it) > BEGV)
6401 back_to_previous_line_start (it);
6403 if (IT_CHARPOS (*it) <= BEGV)
6404 break;
6406 /* If selective > 0, then lines indented more than its value are
6407 invisible. */
6408 if (it->selective > 0
6409 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6410 it->selective))
6411 continue;
6413 /* Check the newline before point for invisibility. */
6415 Lisp_Object prop;
6416 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6417 Qinvisible, it->window);
6418 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6419 continue;
6422 if (IT_CHARPOS (*it) <= BEGV)
6423 break;
6426 struct it it2;
6427 void *it2data = NULL;
6428 ptrdiff_t pos;
6429 ptrdiff_t beg, end;
6430 Lisp_Object val, overlay;
6432 SAVE_IT (it2, *it, it2data);
6434 /* If newline is part of a composition, continue from start of composition */
6435 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6436 && beg < IT_CHARPOS (*it))
6437 goto replaced;
6439 /* If newline is replaced by a display property, find start of overlay
6440 or interval and continue search from that point. */
6441 pos = --IT_CHARPOS (it2);
6442 --IT_BYTEPOS (it2);
6443 it2.sp = 0;
6444 bidi_unshelve_cache (NULL, false);
6445 it2.string_from_display_prop_p = false;
6446 it2.from_disp_prop_p = false;
6447 if (handle_display_prop (&it2) == HANDLED_RETURN
6448 && !NILP (val = get_char_property_and_overlay
6449 (make_number (pos), Qdisplay, Qnil, &overlay))
6450 && (OVERLAYP (overlay)
6451 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6452 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6454 RESTORE_IT (it, it, it2data);
6455 goto replaced;
6458 /* Newline is not replaced by anything -- so we are done. */
6459 RESTORE_IT (it, it, it2data);
6460 break;
6462 replaced:
6463 if (beg < BEGV)
6464 beg = BEGV;
6465 IT_CHARPOS (*it) = beg;
6466 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6470 it->continuation_lines_width = 0;
6472 eassert (IT_CHARPOS (*it) >= BEGV);
6473 eassert (IT_CHARPOS (*it) == BEGV
6474 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6475 CHECK_IT (it);
6479 /* Reseat iterator IT at the previous visible line start. Skip
6480 invisible text that is so either due to text properties or due to
6481 selective display. At the end, update IT's overlay information,
6482 face information etc. */
6484 void
6485 reseat_at_previous_visible_line_start (struct it *it)
6487 back_to_previous_visible_line_start (it);
6488 reseat (it, it->current.pos, true);
6489 CHECK_IT (it);
6493 /* Reseat iterator IT on the next visible line start in the current
6494 buffer. ON_NEWLINE_P means position IT on the newline
6495 preceding the line start. Skip over invisible text that is so
6496 because of selective display. Compute faces, overlays etc at the
6497 new position. Note that this function does not skip over text that
6498 is invisible because of text properties. */
6500 static void
6501 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6503 bool skipped_p = false;
6504 struct bidi_it bidi_it_prev;
6505 bool newline_found_p
6506 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6508 /* Skip over lines that are invisible because they are indented
6509 more than the value of IT->selective. */
6510 if (it->selective > 0)
6511 while (IT_CHARPOS (*it) < ZV
6512 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6513 it->selective))
6515 eassert (IT_BYTEPOS (*it) == BEGV
6516 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6517 newline_found_p =
6518 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6521 /* Position on the newline if that's what's requested. */
6522 if (on_newline_p && newline_found_p)
6524 if (STRINGP (it->string))
6526 if (IT_STRING_CHARPOS (*it) > 0)
6528 if (!it->bidi_p)
6530 --IT_STRING_CHARPOS (*it);
6531 --IT_STRING_BYTEPOS (*it);
6533 else
6535 /* We need to restore the bidi iterator to the state
6536 it had on the newline, and resync the IT's
6537 position with that. */
6538 it->bidi_it = bidi_it_prev;
6539 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6540 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6544 else if (IT_CHARPOS (*it) > BEGV)
6546 if (!it->bidi_p)
6548 --IT_CHARPOS (*it);
6549 --IT_BYTEPOS (*it);
6551 else
6553 /* We need to restore the bidi iterator to the state it
6554 had on the newline and resync IT with that. */
6555 it->bidi_it = bidi_it_prev;
6556 IT_CHARPOS (*it) = it->bidi_it.charpos;
6557 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6559 reseat (it, it->current.pos, false);
6562 else if (skipped_p)
6563 reseat (it, it->current.pos, false);
6565 CHECK_IT (it);
6570 /***********************************************************************
6571 Changing an iterator's position
6572 ***********************************************************************/
6574 /* Change IT's current position to POS in current_buffer.
6575 If FORCE_P, always check for text properties at the new position.
6576 Otherwise, text properties are only looked up if POS >=
6577 IT->check_charpos of a property. */
6579 static void
6580 reseat (struct it *it, struct text_pos pos, bool force_p)
6582 ptrdiff_t original_pos = IT_CHARPOS (*it);
6584 reseat_1 (it, pos, false);
6586 /* Determine where to check text properties. Avoid doing it
6587 where possible because text property lookup is very expensive. */
6588 if (force_p
6589 || CHARPOS (pos) > it->stop_charpos
6590 || CHARPOS (pos) < original_pos)
6592 if (it->bidi_p)
6594 /* For bidi iteration, we need to prime prev_stop and
6595 base_level_stop with our best estimations. */
6596 /* Implementation note: Of course, POS is not necessarily a
6597 stop position, so assigning prev_pos to it is a lie; we
6598 should have called compute_stop_backwards. However, if
6599 the current buffer does not include any R2L characters,
6600 that call would be a waste of cycles, because the
6601 iterator will never move back, and thus never cross this
6602 "fake" stop position. So we delay that backward search
6603 until the time we really need it, in next_element_from_buffer. */
6604 if (CHARPOS (pos) != it->prev_stop)
6605 it->prev_stop = CHARPOS (pos);
6606 if (CHARPOS (pos) < it->base_level_stop)
6607 it->base_level_stop = 0; /* meaning it's unknown */
6608 handle_stop (it);
6610 else
6612 handle_stop (it);
6613 it->prev_stop = it->base_level_stop = 0;
6618 CHECK_IT (it);
6622 /* Change IT's buffer position to POS. SET_STOP_P means set
6623 IT->stop_pos to POS, also. */
6625 static void
6626 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6628 /* Don't call this function when scanning a C string. */
6629 eassert (it->s == NULL);
6631 /* POS must be a reasonable value. */
6632 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6634 it->current.pos = it->position = pos;
6635 it->end_charpos = ZV;
6636 it->dpvec = NULL;
6637 it->current.dpvec_index = -1;
6638 it->current.overlay_string_index = -1;
6639 IT_STRING_CHARPOS (*it) = -1;
6640 IT_STRING_BYTEPOS (*it) = -1;
6641 it->string = Qnil;
6642 it->method = GET_FROM_BUFFER;
6643 it->object = it->w->contents;
6644 it->area = TEXT_AREA;
6645 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6646 it->sp = 0;
6647 it->string_from_display_prop_p = false;
6648 it->string_from_prefix_prop_p = false;
6650 it->from_disp_prop_p = false;
6651 it->face_before_selective_p = false;
6652 if (it->bidi_p)
6654 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6655 &it->bidi_it);
6656 bidi_unshelve_cache (NULL, false);
6657 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6658 it->bidi_it.string.s = NULL;
6659 it->bidi_it.string.lstring = Qnil;
6660 it->bidi_it.string.bufpos = 0;
6661 it->bidi_it.string.from_disp_str = false;
6662 it->bidi_it.string.unibyte = false;
6663 it->bidi_it.w = it->w;
6666 if (set_stop_p)
6668 it->stop_charpos = CHARPOS (pos);
6669 it->base_level_stop = CHARPOS (pos);
6671 /* This make the information stored in it->cmp_it invalidate. */
6672 it->cmp_it.id = -1;
6676 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6677 If S is non-null, it is a C string to iterate over. Otherwise,
6678 STRING gives a Lisp string to iterate over.
6680 If PRECISION > 0, don't return more then PRECISION number of
6681 characters from the string.
6683 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6684 characters have been returned. FIELD_WIDTH < 0 means an infinite
6685 field width.
6687 MULTIBYTE = 0 means disable processing of multibyte characters,
6688 MULTIBYTE > 0 means enable it,
6689 MULTIBYTE < 0 means use IT->multibyte_p.
6691 IT must be initialized via a prior call to init_iterator before
6692 calling this function. */
6694 static void
6695 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6696 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6697 int multibyte)
6699 /* No text property checks performed by default, but see below. */
6700 it->stop_charpos = -1;
6702 /* Set iterator position and end position. */
6703 memset (&it->current, 0, sizeof it->current);
6704 it->current.overlay_string_index = -1;
6705 it->current.dpvec_index = -1;
6706 eassert (charpos >= 0);
6708 /* If STRING is specified, use its multibyteness, otherwise use the
6709 setting of MULTIBYTE, if specified. */
6710 if (multibyte >= 0)
6711 it->multibyte_p = multibyte > 0;
6713 /* Bidirectional reordering of strings is controlled by the default
6714 value of bidi-display-reordering. Don't try to reorder while
6715 loading loadup.el, as the necessary character property tables are
6716 not yet available. */
6717 it->bidi_p =
6718 !redisplay__inhibit_bidi
6719 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6721 if (s == NULL)
6723 eassert (STRINGP (string));
6724 it->string = string;
6725 it->s = NULL;
6726 it->end_charpos = it->string_nchars = SCHARS (string);
6727 it->method = GET_FROM_STRING;
6728 it->current.string_pos = string_pos (charpos, string);
6730 if (it->bidi_p)
6732 it->bidi_it.string.lstring = string;
6733 it->bidi_it.string.s = NULL;
6734 it->bidi_it.string.schars = it->end_charpos;
6735 it->bidi_it.string.bufpos = 0;
6736 it->bidi_it.string.from_disp_str = false;
6737 it->bidi_it.string.unibyte = !it->multibyte_p;
6738 it->bidi_it.w = it->w;
6739 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6740 FRAME_WINDOW_P (it->f), &it->bidi_it);
6743 else
6745 it->s = (const unsigned char *) s;
6746 it->string = Qnil;
6748 /* Note that we use IT->current.pos, not it->current.string_pos,
6749 for displaying C strings. */
6750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6751 if (it->multibyte_p)
6753 it->current.pos = c_string_pos (charpos, s, true);
6754 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6756 else
6758 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6759 it->end_charpos = it->string_nchars = strlen (s);
6762 if (it->bidi_p)
6764 it->bidi_it.string.lstring = Qnil;
6765 it->bidi_it.string.s = (const unsigned char *) s;
6766 it->bidi_it.string.schars = it->end_charpos;
6767 it->bidi_it.string.bufpos = 0;
6768 it->bidi_it.string.from_disp_str = false;
6769 it->bidi_it.string.unibyte = !it->multibyte_p;
6770 it->bidi_it.w = it->w;
6771 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6772 &it->bidi_it);
6774 it->method = GET_FROM_C_STRING;
6777 /* PRECISION > 0 means don't return more than PRECISION characters
6778 from the string. */
6779 if (precision > 0 && it->end_charpos - charpos > precision)
6781 it->end_charpos = it->string_nchars = charpos + precision;
6782 if (it->bidi_p)
6783 it->bidi_it.string.schars = it->end_charpos;
6786 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6787 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6788 FIELD_WIDTH < 0 means infinite field width. This is useful for
6789 padding with `-' at the end of a mode line. */
6790 if (field_width < 0)
6791 field_width = DISP_INFINITY;
6792 /* Implementation note: We deliberately don't enlarge
6793 it->bidi_it.string.schars here to fit it->end_charpos, because
6794 the bidi iterator cannot produce characters out of thin air. */
6795 if (field_width > it->end_charpos - charpos)
6796 it->end_charpos = charpos + field_width;
6798 /* Use the standard display table for displaying strings. */
6799 if (DISP_TABLE_P (Vstandard_display_table))
6800 it->dp = XCHAR_TABLE (Vstandard_display_table);
6802 it->stop_charpos = charpos;
6803 it->prev_stop = charpos;
6804 it->base_level_stop = 0;
6805 if (it->bidi_p)
6807 it->bidi_it.first_elt = true;
6808 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6809 it->bidi_it.disp_pos = -1;
6811 if (s == NULL && it->multibyte_p)
6813 ptrdiff_t endpos = SCHARS (it->string);
6814 if (endpos > it->end_charpos)
6815 endpos = it->end_charpos;
6816 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6817 it->string);
6819 CHECK_IT (it);
6824 /***********************************************************************
6825 Iteration
6826 ***********************************************************************/
6828 /* Map enum it_method value to corresponding next_element_from_* function. */
6830 typedef bool (*next_element_function) (struct it *);
6832 static next_element_function const get_next_element[NUM_IT_METHODS] =
6834 next_element_from_buffer,
6835 next_element_from_display_vector,
6836 next_element_from_string,
6837 next_element_from_c_string,
6838 next_element_from_image,
6839 next_element_from_stretch,
6840 next_element_from_xwidget,
6843 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6846 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6847 (possibly with the following characters). */
6849 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6850 ((IT)->cmp_it.id >= 0 \
6851 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6852 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6853 END_CHARPOS, (IT)->w, \
6854 FACE_FROM_ID_OR_NULL ((IT)->f, \
6855 (IT)->face_id), \
6856 (IT)->string)))
6859 /* Lookup the char-table Vglyphless_char_display for character C (-1
6860 if we want information for no-font case), and return the display
6861 method symbol. By side-effect, update it->what and
6862 it->glyphless_method. This function is called from
6863 get_next_display_element for each character element, and from
6864 x_produce_glyphs when no suitable font was found. */
6866 Lisp_Object
6867 lookup_glyphless_char_display (int c, struct it *it)
6869 Lisp_Object glyphless_method = Qnil;
6871 if (CHAR_TABLE_P (Vglyphless_char_display)
6872 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6874 if (c >= 0)
6876 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6877 if (CONSP (glyphless_method))
6878 glyphless_method = FRAME_WINDOW_P (it->f)
6879 ? XCAR (glyphless_method)
6880 : XCDR (glyphless_method);
6882 else
6883 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6886 retry:
6887 if (NILP (glyphless_method))
6889 if (c >= 0)
6890 /* The default is to display the character by a proper font. */
6891 return Qnil;
6892 /* The default for the no-font case is to display an empty box. */
6893 glyphless_method = Qempty_box;
6895 if (EQ (glyphless_method, Qzero_width))
6897 if (c >= 0)
6898 return glyphless_method;
6899 /* This method can't be used for the no-font case. */
6900 glyphless_method = Qempty_box;
6902 if (EQ (glyphless_method, Qthin_space))
6903 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6904 else if (EQ (glyphless_method, Qempty_box))
6905 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6906 else if (EQ (glyphless_method, Qhex_code))
6907 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6908 else if (STRINGP (glyphless_method))
6909 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6910 else
6912 /* Invalid value. We use the default method. */
6913 glyphless_method = Qnil;
6914 goto retry;
6916 it->what = IT_GLYPHLESS;
6917 return glyphless_method;
6920 /* Merge escape glyph face and cache the result. */
6922 static struct frame *last_escape_glyph_frame = NULL;
6923 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6924 static int last_escape_glyph_merged_face_id = 0;
6926 static int
6927 merge_escape_glyph_face (struct it *it)
6929 int face_id;
6931 if (it->f == last_escape_glyph_frame
6932 && it->face_id == last_escape_glyph_face_id)
6933 face_id = last_escape_glyph_merged_face_id;
6934 else
6936 /* Merge the `escape-glyph' face into the current face. */
6937 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6938 last_escape_glyph_frame = it->f;
6939 last_escape_glyph_face_id = it->face_id;
6940 last_escape_glyph_merged_face_id = face_id;
6942 return face_id;
6945 /* Likewise for glyphless glyph face. */
6947 static struct frame *last_glyphless_glyph_frame = NULL;
6948 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6949 static int last_glyphless_glyph_merged_face_id = 0;
6952 merge_glyphless_glyph_face (struct it *it)
6954 int face_id;
6956 if (it->f == last_glyphless_glyph_frame
6957 && it->face_id == last_glyphless_glyph_face_id)
6958 face_id = last_glyphless_glyph_merged_face_id;
6959 else
6961 /* Merge the `glyphless-char' face into the current face. */
6962 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6963 last_glyphless_glyph_frame = it->f;
6964 last_glyphless_glyph_face_id = it->face_id;
6965 last_glyphless_glyph_merged_face_id = face_id;
6967 return face_id;
6970 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6971 be called before redisplaying windows, and when the frame's face
6972 cache is freed. */
6973 void
6974 forget_escape_and_glyphless_faces (void)
6976 last_escape_glyph_frame = NULL;
6977 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6978 last_glyphless_glyph_frame = NULL;
6979 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6982 /* Load IT's display element fields with information about the next
6983 display element from the current position of IT. Value is false if
6984 end of buffer (or C string) is reached. */
6986 static bool
6987 get_next_display_element (struct it *it)
6989 /* True means that we found a display element. False means that
6990 we hit the end of what we iterate over. Performance note: the
6991 function pointer `method' used here turns out to be faster than
6992 using a sequence of if-statements. */
6993 bool success_p;
6995 get_next:
6996 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6998 if (it->what == IT_CHARACTER)
7000 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7001 and only if (a) the resolved directionality of that character
7002 is R..." */
7003 /* FIXME: Do we need an exception for characters from display
7004 tables? */
7005 if (it->bidi_p && it->bidi_it.type == STRONG_R
7006 && !inhibit_bidi_mirroring)
7007 it->c = bidi_mirror_char (it->c);
7008 /* Map via display table or translate control characters.
7009 IT->c, IT->len etc. have been set to the next character by
7010 the function call above. If we have a display table, and it
7011 contains an entry for IT->c, translate it. Don't do this if
7012 IT->c itself comes from a display table, otherwise we could
7013 end up in an infinite recursion. (An alternative could be to
7014 count the recursion depth of this function and signal an
7015 error when a certain maximum depth is reached.) Is it worth
7016 it? */
7017 if (success_p && it->dpvec == NULL)
7019 Lisp_Object dv;
7020 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7021 bool nonascii_space_p = false;
7022 bool nonascii_hyphen_p = false;
7023 int c = it->c; /* This is the character to display. */
7025 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7027 eassert (SINGLE_BYTE_CHAR_P (c));
7028 if (unibyte_display_via_language_environment)
7030 c = DECODE_CHAR (unibyte, c);
7031 if (c < 0)
7032 c = BYTE8_TO_CHAR (it->c);
7034 else
7035 c = BYTE8_TO_CHAR (it->c);
7038 if (it->dp
7039 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7040 VECTORP (dv)))
7042 struct Lisp_Vector *v = XVECTOR (dv);
7044 /* Return the first character from the display table
7045 entry, if not empty. If empty, don't display the
7046 current character. */
7047 if (v->header.size)
7049 it->dpvec_char_len = it->len;
7050 it->dpvec = v->contents;
7051 it->dpend = v->contents + v->header.size;
7052 it->current.dpvec_index = 0;
7053 it->dpvec_face_id = -1;
7054 it->saved_face_id = it->face_id;
7055 it->method = GET_FROM_DISPLAY_VECTOR;
7056 it->ellipsis_p = false;
7058 else
7060 set_iterator_to_next (it, false);
7062 goto get_next;
7065 if (! NILP (lookup_glyphless_char_display (c, it)))
7067 if (it->what == IT_GLYPHLESS)
7068 goto done;
7069 /* Don't display this character. */
7070 set_iterator_to_next (it, false);
7071 goto get_next;
7074 /* If `nobreak-char-display' is non-nil, we display
7075 non-ASCII spaces and hyphens specially. */
7076 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7078 if (c == NO_BREAK_SPACE)
7079 nonascii_space_p = true;
7080 else if (c == SOFT_HYPHEN || c == HYPHEN
7081 || c == NON_BREAKING_HYPHEN)
7082 nonascii_hyphen_p = true;
7085 /* Translate control characters into `\003' or `^C' form.
7086 Control characters coming from a display table entry are
7087 currently not translated because we use IT->dpvec to hold
7088 the translation. This could easily be changed but I
7089 don't believe that it is worth doing.
7091 The characters handled by `nobreak-char-display' must be
7092 translated too.
7094 Non-printable characters and raw-byte characters are also
7095 translated to octal or hexadecimal form. */
7096 if (((c < ' ' || c == 127) /* ASCII control chars. */
7097 ? (it->area != TEXT_AREA
7098 /* In mode line, treat \n, \t like other crl chars. */
7099 || (c != '\t'
7100 && it->glyph_row
7101 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7102 || (c != '\n' && c != '\t'))
7103 : (nonascii_space_p
7104 || nonascii_hyphen_p
7105 || CHAR_BYTE8_P (c)
7106 || ! CHAR_PRINTABLE_P (c))))
7108 /* C is a control character, non-ASCII space/hyphen,
7109 raw-byte, or a non-printable character which must be
7110 displayed either as '\003' or as `^C' where the '\\'
7111 and '^' can be defined in the display table. Fill
7112 IT->ctl_chars with glyphs for what we have to
7113 display. Then, set IT->dpvec to these glyphs. */
7114 Lisp_Object gc;
7115 int ctl_len;
7116 int face_id;
7117 int lface_id = 0;
7118 int escape_glyph;
7120 /* Handle control characters with ^. */
7122 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7124 int g;
7126 g = '^'; /* default glyph for Control */
7127 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7128 if (it->dp
7129 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7131 g = GLYPH_CODE_CHAR (gc);
7132 lface_id = GLYPH_CODE_FACE (gc);
7135 face_id = (lface_id
7136 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7137 : merge_escape_glyph_face (it));
7139 XSETINT (it->ctl_chars[0], g);
7140 XSETINT (it->ctl_chars[1], c ^ 0100);
7141 ctl_len = 2;
7142 goto display_control;
7145 /* Handle non-ascii space in the mode where it only gets
7146 highlighting. */
7148 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7150 /* Merge `nobreak-space' into the current face. */
7151 face_id = merge_faces (it->f, Qnobreak_space, 0,
7152 it->face_id);
7153 XSETINT (it->ctl_chars[0], ' ');
7154 ctl_len = 1;
7155 goto display_control;
7158 /* Handle non-ascii hyphens in the mode where it only
7159 gets highlighting. */
7161 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7163 /* Merge `nobreak-space' into the current face. */
7164 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7165 it->face_id);
7166 XSETINT (it->ctl_chars[0], '-');
7167 ctl_len = 1;
7168 goto display_control;
7171 /* Handle sequences that start with the "escape glyph". */
7173 /* the default escape glyph is \. */
7174 escape_glyph = '\\';
7176 if (it->dp
7177 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7179 escape_glyph = GLYPH_CODE_CHAR (gc);
7180 lface_id = GLYPH_CODE_FACE (gc);
7183 face_id = (lface_id
7184 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7185 : merge_escape_glyph_face (it));
7187 /* Draw non-ASCII space/hyphen with escape glyph: */
7189 if (nonascii_space_p || nonascii_hyphen_p)
7191 XSETINT (it->ctl_chars[0], escape_glyph);
7192 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7193 ctl_len = 2;
7194 goto display_control;
7198 char str[10];
7199 int len, i;
7201 if (CHAR_BYTE8_P (c))
7202 /* Display \200 or \x80 instead of \17777600. */
7203 c = CHAR_TO_BYTE8 (c);
7204 const char *format_string = display_raw_bytes_as_hex
7205 ? "x%02x"
7206 : "%03o";
7207 len = sprintf (str, format_string, c + 0u);
7209 XSETINT (it->ctl_chars[0], escape_glyph);
7210 for (i = 0; i < len; i++)
7211 XSETINT (it->ctl_chars[i + 1], str[i]);
7212 ctl_len = len + 1;
7215 display_control:
7216 /* Set up IT->dpvec and return first character from it. */
7217 it->dpvec_char_len = it->len;
7218 it->dpvec = it->ctl_chars;
7219 it->dpend = it->dpvec + ctl_len;
7220 it->current.dpvec_index = 0;
7221 it->dpvec_face_id = face_id;
7222 it->saved_face_id = it->face_id;
7223 it->method = GET_FROM_DISPLAY_VECTOR;
7224 it->ellipsis_p = false;
7225 goto get_next;
7227 it->char_to_display = c;
7229 else if (success_p)
7231 it->char_to_display = it->c;
7235 #ifdef HAVE_WINDOW_SYSTEM
7236 /* Adjust face id for a multibyte character. There are no multibyte
7237 character in unibyte text. */
7238 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7239 && it->multibyte_p
7240 && success_p
7241 && FRAME_WINDOW_P (it->f))
7243 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7245 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7247 /* Automatic composition with glyph-string. */
7248 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7250 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7252 else
7254 ptrdiff_t pos = (it->s ? -1
7255 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7256 : IT_CHARPOS (*it));
7257 int c;
7259 if (it->what == IT_CHARACTER)
7260 c = it->char_to_display;
7261 else
7263 struct composition *cmp = composition_table[it->cmp_it.id];
7264 int i;
7266 c = ' ';
7267 for (i = 0; i < cmp->glyph_len; i++)
7268 /* TAB in a composition means display glyphs with
7269 padding space on the left or right. */
7270 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7271 break;
7273 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7276 #endif /* HAVE_WINDOW_SYSTEM */
7278 done:
7279 /* Is this character the last one of a run of characters with
7280 box? If yes, set IT->end_of_box_run_p to true. */
7281 if (it->face_box_p
7282 && it->s == NULL)
7284 if (it->method == GET_FROM_STRING && it->sp)
7286 int face_id = underlying_face_id (it);
7287 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7289 if (face)
7291 if (face->box == FACE_NO_BOX)
7293 /* If the box comes from face properties in a
7294 display string, check faces in that string. */
7295 int string_face_id = face_after_it_pos (it);
7296 it->end_of_box_run_p
7297 = (FACE_FROM_ID (it->f, string_face_id)->box
7298 == FACE_NO_BOX);
7300 /* Otherwise, the box comes from the underlying face.
7301 If this is the last string character displayed, check
7302 the next buffer location. */
7303 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7304 /* n_overlay_strings is unreliable unless
7305 overlay_string_index is non-negative. */
7306 && ((it->current.overlay_string_index >= 0
7307 && (it->current.overlay_string_index
7308 == it->n_overlay_strings - 1))
7309 /* A string from display property. */
7310 || it->from_disp_prop_p))
7312 ptrdiff_t ignore;
7313 int next_face_id;
7314 bool text_from_string = false;
7315 /* Normally, the next buffer location is stored in
7316 IT->current.pos... */
7317 struct text_pos pos = it->current.pos;
7319 /* ...but for a string from a display property, the
7320 next buffer position is stored in the 'position'
7321 member of the iteration stack slot below the
7322 current one, see handle_single_display_spec. By
7323 contrast, it->current.pos was not yet updated to
7324 point to that buffer position; that will happen
7325 in pop_it, after we finish displaying the current
7326 string. Note that we already checked above that
7327 it->sp is positive, so subtracting one from it is
7328 safe. */
7329 if (it->from_disp_prop_p)
7331 int stackp = it->sp - 1;
7333 /* Find the stack level with data from buffer. */
7334 while (stackp >= 0
7335 && STRINGP ((it->stack + stackp)->string))
7336 stackp--;
7337 if (stackp < 0)
7339 /* If no stack slot was found for iterating
7340 a buffer, we are displaying text from a
7341 string, most probably the mode line or
7342 the header line, and that string has a
7343 display string on some of its
7344 characters. */
7345 text_from_string = true;
7346 pos = it->stack[it->sp - 1].position;
7348 else
7349 pos = (it->stack + stackp)->position;
7351 else
7352 INC_TEXT_POS (pos, it->multibyte_p);
7354 if (text_from_string)
7356 Lisp_Object base_string = it->stack[it->sp - 1].string;
7358 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7359 it->end_of_box_run_p = true;
7360 else
7362 next_face_id
7363 = face_at_string_position (it->w, base_string,
7364 CHARPOS (pos), 0,
7365 &ignore, face_id, false);
7366 it->end_of_box_run_p
7367 = (FACE_FROM_ID (it->f, next_face_id)->box
7368 == FACE_NO_BOX);
7371 else if (CHARPOS (pos) >= ZV)
7372 it->end_of_box_run_p = true;
7373 else
7375 next_face_id =
7376 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7377 CHARPOS (pos)
7378 + TEXT_PROP_DISTANCE_LIMIT,
7379 false, -1);
7380 it->end_of_box_run_p
7381 = (FACE_FROM_ID (it->f, next_face_id)->box
7382 == FACE_NO_BOX);
7387 /* next_element_from_display_vector sets this flag according to
7388 faces of the display vector glyphs, see there. */
7389 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7391 int face_id = face_after_it_pos (it);
7392 it->end_of_box_run_p
7393 = (face_id != it->face_id
7394 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7397 /* If we reached the end of the object we've been iterating (e.g., a
7398 display string or an overlay string), and there's something on
7399 IT->stack, proceed with what's on the stack. It doesn't make
7400 sense to return false if there's unprocessed stuff on the stack,
7401 because otherwise that stuff will never be displayed. */
7402 if (!success_p && it->sp > 0)
7404 set_iterator_to_next (it, false);
7405 success_p = get_next_display_element (it);
7408 /* Value is false if end of buffer or string reached. */
7409 return success_p;
7413 /* Move IT to the next display element.
7415 RESEAT_P means if called on a newline in buffer text,
7416 skip to the next visible line start.
7418 Functions get_next_display_element and set_iterator_to_next are
7419 separate because I find this arrangement easier to handle than a
7420 get_next_display_element function that also increments IT's
7421 position. The way it is we can first look at an iterator's current
7422 display element, decide whether it fits on a line, and if it does,
7423 increment the iterator position. The other way around we probably
7424 would either need a flag indicating whether the iterator has to be
7425 incremented the next time, or we would have to implement a
7426 decrement position function which would not be easy to write. */
7428 void
7429 set_iterator_to_next (struct it *it, bool reseat_p)
7431 /* Reset flags indicating start and end of a sequence of characters
7432 with box. Reset them at the start of this function because
7433 moving the iterator to a new position might set them. */
7434 it->start_of_box_run_p = it->end_of_box_run_p = false;
7436 switch (it->method)
7438 case GET_FROM_BUFFER:
7439 /* The current display element of IT is a character from
7440 current_buffer. Advance in the buffer, and maybe skip over
7441 invisible lines that are so because of selective display. */
7442 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7443 reseat_at_next_visible_line_start (it, false);
7444 else if (it->cmp_it.id >= 0)
7446 /* We are currently getting glyphs from a composition. */
7447 if (! it->bidi_p)
7449 IT_CHARPOS (*it) += it->cmp_it.nchars;
7450 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7452 else
7454 int i;
7456 /* Update IT's char/byte positions to point to the first
7457 character of the next grapheme cluster, or to the
7458 character visually after the current composition. */
7459 for (i = 0; i < it->cmp_it.nchars; i++)
7460 bidi_move_to_visually_next (&it->bidi_it);
7461 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7462 IT_CHARPOS (*it) = it->bidi_it.charpos;
7465 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7466 && it->cmp_it.to < it->cmp_it.nglyphs)
7468 /* Composition created while scanning forward. Proceed
7469 to the next grapheme cluster. */
7470 it->cmp_it.from = it->cmp_it.to;
7472 else if ((it->bidi_p && it->cmp_it.reversed_p)
7473 && it->cmp_it.from > 0)
7475 /* Composition created while scanning backward. Proceed
7476 to the previous grapheme cluster. */
7477 it->cmp_it.to = it->cmp_it.from;
7479 else
7481 /* No more grapheme clusters in this composition.
7482 Find the next stop position. */
7483 ptrdiff_t stop = it->end_charpos;
7485 if (it->bidi_it.scan_dir < 0)
7486 /* Now we are scanning backward and don't know
7487 where to stop. */
7488 stop = -1;
7489 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7490 IT_BYTEPOS (*it), stop, Qnil);
7493 else
7495 eassert (it->len != 0);
7497 if (!it->bidi_p)
7499 IT_BYTEPOS (*it) += it->len;
7500 IT_CHARPOS (*it) += 1;
7502 else
7504 int prev_scan_dir = it->bidi_it.scan_dir;
7505 /* If this is a new paragraph, determine its base
7506 direction (a.k.a. its base embedding level). */
7507 if (it->bidi_it.new_paragraph)
7508 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7509 false);
7510 bidi_move_to_visually_next (&it->bidi_it);
7511 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7512 IT_CHARPOS (*it) = it->bidi_it.charpos;
7513 if (prev_scan_dir != it->bidi_it.scan_dir)
7515 /* As the scan direction was changed, we must
7516 re-compute the stop position for composition. */
7517 ptrdiff_t stop = it->end_charpos;
7518 if (it->bidi_it.scan_dir < 0)
7519 stop = -1;
7520 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7521 IT_BYTEPOS (*it), stop, Qnil);
7524 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7526 break;
7528 case GET_FROM_C_STRING:
7529 /* Current display element of IT is from a C string. */
7530 if (!it->bidi_p
7531 /* If the string position is beyond string's end, it means
7532 next_element_from_c_string is padding the string with
7533 blanks, in which case we bypass the bidi iterator,
7534 because it cannot deal with such virtual characters. */
7535 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7537 IT_BYTEPOS (*it) += it->len;
7538 IT_CHARPOS (*it) += 1;
7540 else
7542 bidi_move_to_visually_next (&it->bidi_it);
7543 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7544 IT_CHARPOS (*it) = it->bidi_it.charpos;
7546 break;
7548 case GET_FROM_DISPLAY_VECTOR:
7549 /* Current display element of IT is from a display table entry.
7550 Advance in the display table definition. Reset it to null if
7551 end reached, and continue with characters from buffers/
7552 strings. */
7553 ++it->current.dpvec_index;
7555 /* Restore face of the iterator to what they were before the
7556 display vector entry (these entries may contain faces). */
7557 it->face_id = it->saved_face_id;
7559 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7561 bool recheck_faces = it->ellipsis_p;
7563 if (it->s)
7564 it->method = GET_FROM_C_STRING;
7565 else if (STRINGP (it->string))
7566 it->method = GET_FROM_STRING;
7567 else
7569 it->method = GET_FROM_BUFFER;
7570 it->object = it->w->contents;
7573 it->dpvec = NULL;
7574 it->current.dpvec_index = -1;
7576 /* Skip over characters which were displayed via IT->dpvec. */
7577 if (it->dpvec_char_len < 0)
7578 reseat_at_next_visible_line_start (it, true);
7579 else if (it->dpvec_char_len > 0)
7581 it->len = it->dpvec_char_len;
7582 set_iterator_to_next (it, reseat_p);
7585 /* Maybe recheck faces after display vector. */
7586 if (recheck_faces)
7588 if (it->method == GET_FROM_STRING)
7589 it->stop_charpos = IT_STRING_CHARPOS (*it);
7590 else
7591 it->stop_charpos = IT_CHARPOS (*it);
7594 break;
7596 case GET_FROM_STRING:
7597 /* Current display element is a character from a Lisp string. */
7598 eassert (it->s == NULL && STRINGP (it->string));
7599 /* Don't advance past string end. These conditions are true
7600 when set_iterator_to_next is called at the end of
7601 get_next_display_element, in which case the Lisp string is
7602 already exhausted, and all we want is pop the iterator
7603 stack. */
7604 if (it->current.overlay_string_index >= 0)
7606 /* This is an overlay string, so there's no padding with
7607 spaces, and the number of characters in the string is
7608 where the string ends. */
7609 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7610 goto consider_string_end;
7612 else
7614 /* Not an overlay string. There could be padding, so test
7615 against it->end_charpos. */
7616 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7617 goto consider_string_end;
7619 if (it->cmp_it.id >= 0)
7621 /* We are delivering display elements from a composition.
7622 Update the string position past the grapheme cluster
7623 we've just processed. */
7624 if (! it->bidi_p)
7626 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7627 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7629 else
7631 int i;
7633 for (i = 0; i < it->cmp_it.nchars; i++)
7634 bidi_move_to_visually_next (&it->bidi_it);
7635 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7636 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7639 /* Did we exhaust all the grapheme clusters of this
7640 composition? */
7641 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7642 && (it->cmp_it.to < it->cmp_it.nglyphs))
7644 /* Not all the grapheme clusters were processed yet;
7645 advance to the next cluster. */
7646 it->cmp_it.from = it->cmp_it.to;
7648 else if ((it->bidi_p && it->cmp_it.reversed_p)
7649 && it->cmp_it.from > 0)
7651 /* Likewise: advance to the next cluster, but going in
7652 the reverse direction. */
7653 it->cmp_it.to = it->cmp_it.from;
7655 else
7657 /* This composition was fully processed; find the next
7658 candidate place for checking for composed
7659 characters. */
7660 /* Always limit string searches to the string length;
7661 any padding spaces are not part of the string, and
7662 there cannot be any compositions in that padding. */
7663 ptrdiff_t stop = SCHARS (it->string);
7665 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7666 stop = -1;
7667 else if (it->end_charpos < stop)
7669 /* Cf. PRECISION in reseat_to_string: we might be
7670 limited in how many of the string characters we
7671 need to deliver. */
7672 stop = it->end_charpos;
7674 composition_compute_stop_pos (&it->cmp_it,
7675 IT_STRING_CHARPOS (*it),
7676 IT_STRING_BYTEPOS (*it), stop,
7677 it->string);
7680 else
7682 if (!it->bidi_p
7683 /* If the string position is beyond string's end, it
7684 means next_element_from_string is padding the string
7685 with blanks, in which case we bypass the bidi
7686 iterator, because it cannot deal with such virtual
7687 characters. */
7688 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7690 IT_STRING_BYTEPOS (*it) += it->len;
7691 IT_STRING_CHARPOS (*it) += 1;
7693 else
7695 int prev_scan_dir = it->bidi_it.scan_dir;
7697 bidi_move_to_visually_next (&it->bidi_it);
7698 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7699 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7700 /* If the scan direction changes, we may need to update
7701 the place where to check for composed characters. */
7702 if (prev_scan_dir != it->bidi_it.scan_dir)
7704 ptrdiff_t stop = SCHARS (it->string);
7706 if (it->bidi_it.scan_dir < 0)
7707 stop = -1;
7708 else if (it->end_charpos < stop)
7709 stop = it->end_charpos;
7711 composition_compute_stop_pos (&it->cmp_it,
7712 IT_STRING_CHARPOS (*it),
7713 IT_STRING_BYTEPOS (*it), stop,
7714 it->string);
7719 consider_string_end:
7721 if (it->current.overlay_string_index >= 0)
7723 /* IT->string is an overlay string. Advance to the
7724 next, if there is one. */
7725 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7727 it->ellipsis_p = false;
7728 next_overlay_string (it);
7729 if (it->ellipsis_p)
7730 setup_for_ellipsis (it, 0);
7733 else
7735 /* IT->string is not an overlay string. If we reached
7736 its end, and there is something on IT->stack, proceed
7737 with what is on the stack. This can be either another
7738 string, this time an overlay string, or a buffer. */
7739 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7740 && it->sp > 0)
7742 pop_it (it);
7743 if (it->method == GET_FROM_STRING)
7744 goto consider_string_end;
7747 break;
7749 case GET_FROM_IMAGE:
7750 case GET_FROM_STRETCH:
7751 case GET_FROM_XWIDGET:
7753 /* The position etc with which we have to proceed are on
7754 the stack. The position may be at the end of a string,
7755 if the `display' property takes up the whole string. */
7756 eassert (it->sp > 0);
7757 pop_it (it);
7758 if (it->method == GET_FROM_STRING)
7759 goto consider_string_end;
7760 break;
7762 default:
7763 /* There are no other methods defined, so this should be a bug. */
7764 emacs_abort ();
7767 eassert (it->method != GET_FROM_STRING
7768 || (STRINGP (it->string)
7769 && IT_STRING_CHARPOS (*it) >= 0));
7772 /* Load IT's display element fields with information about the next
7773 display element which comes from a display table entry or from the
7774 result of translating a control character to one of the forms `^C'
7775 or `\003'.
7777 IT->dpvec holds the glyphs to return as characters.
7778 IT->saved_face_id holds the face id before the display vector--it
7779 is restored into IT->face_id in set_iterator_to_next. */
7781 static bool
7782 next_element_from_display_vector (struct it *it)
7784 Lisp_Object gc;
7785 int prev_face_id = it->face_id;
7786 int next_face_id;
7788 /* Precondition. */
7789 eassert (it->dpvec && it->current.dpvec_index >= 0);
7791 it->face_id = it->saved_face_id;
7793 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7794 That seemed totally bogus - so I changed it... */
7795 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7796 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7798 struct face *this_face, *prev_face, *next_face;
7800 it->c = GLYPH_CODE_CHAR (gc);
7801 it->len = CHAR_BYTES (it->c);
7803 /* The entry may contain a face id to use. Such a face id is
7804 the id of a Lisp face, not a realized face. A face id of
7805 zero means no face is specified. */
7806 if (it->dpvec_face_id >= 0)
7807 it->face_id = it->dpvec_face_id;
7808 else
7810 int lface_id = GLYPH_CODE_FACE (gc);
7811 if (lface_id > 0)
7812 it->face_id = merge_faces (it->f, Qt, lface_id,
7813 it->saved_face_id);
7816 /* Glyphs in the display vector could have the box face, so we
7817 need to set the related flags in the iterator, as
7818 appropriate. */
7819 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7820 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7822 /* Is this character the first character of a box-face run? */
7823 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7824 && (!prev_face
7825 || prev_face->box == FACE_NO_BOX));
7827 /* For the last character of the box-face run, we need to look
7828 either at the next glyph from the display vector, or at the
7829 face we saw before the display vector. */
7830 next_face_id = it->saved_face_id;
7831 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7833 if (it->dpvec_face_id >= 0)
7834 next_face_id = it->dpvec_face_id;
7835 else
7837 int lface_id =
7838 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7840 if (lface_id > 0)
7841 next_face_id = merge_faces (it->f, Qt, lface_id,
7842 it->saved_face_id);
7845 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7846 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7847 && (!next_face
7848 || next_face->box == FACE_NO_BOX));
7849 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7851 else
7852 /* Display table entry is invalid. Return a space. */
7853 it->c = ' ', it->len = 1;
7855 /* Don't change position and object of the iterator here. They are
7856 still the values of the character that had this display table
7857 entry or was translated, and that's what we want. */
7858 it->what = IT_CHARACTER;
7859 return true;
7862 /* Get the first element of string/buffer in the visual order, after
7863 being reseated to a new position in a string or a buffer. */
7864 static void
7865 get_visually_first_element (struct it *it)
7867 bool string_p = STRINGP (it->string) || it->s;
7868 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7869 ptrdiff_t bob = (string_p ? 0 : BEGV);
7871 if (STRINGP (it->string))
7873 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7874 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7876 else
7878 it->bidi_it.charpos = IT_CHARPOS (*it);
7879 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7882 if (it->bidi_it.charpos == eob)
7884 /* Nothing to do, but reset the FIRST_ELT flag, like
7885 bidi_paragraph_init does, because we are not going to
7886 call it. */
7887 it->bidi_it.first_elt = false;
7889 else if (it->bidi_it.charpos == bob
7890 || (!string_p
7891 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7892 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7894 /* If we are at the beginning of a line/string, we can produce
7895 the next element right away. */
7896 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7897 bidi_move_to_visually_next (&it->bidi_it);
7899 else
7901 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7903 /* We need to prime the bidi iterator starting at the line's or
7904 string's beginning, before we will be able to produce the
7905 next element. */
7906 if (string_p)
7907 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7908 else
7909 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7910 IT_BYTEPOS (*it), -1,
7911 &it->bidi_it.bytepos);
7912 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7915 /* Now return to buffer/string position where we were asked
7916 to get the next display element, and produce that. */
7917 bidi_move_to_visually_next (&it->bidi_it);
7919 while (it->bidi_it.bytepos != orig_bytepos
7920 && it->bidi_it.charpos < eob);
7923 /* Adjust IT's position information to where we ended up. */
7924 if (STRINGP (it->string))
7926 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7927 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7929 else
7931 IT_CHARPOS (*it) = it->bidi_it.charpos;
7932 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7935 if (STRINGP (it->string) || !it->s)
7937 ptrdiff_t stop, charpos, bytepos;
7939 if (STRINGP (it->string))
7941 eassert (!it->s);
7942 stop = SCHARS (it->string);
7943 if (stop > it->end_charpos)
7944 stop = it->end_charpos;
7945 charpos = IT_STRING_CHARPOS (*it);
7946 bytepos = IT_STRING_BYTEPOS (*it);
7948 else
7950 stop = it->end_charpos;
7951 charpos = IT_CHARPOS (*it);
7952 bytepos = IT_BYTEPOS (*it);
7954 if (it->bidi_it.scan_dir < 0)
7955 stop = -1;
7956 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7957 it->string);
7961 /* Load IT with the next display element from Lisp string IT->string.
7962 IT->current.string_pos is the current position within the string.
7963 If IT->current.overlay_string_index >= 0, the Lisp string is an
7964 overlay string. */
7966 static bool
7967 next_element_from_string (struct it *it)
7969 struct text_pos position;
7971 eassert (STRINGP (it->string));
7972 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7973 eassert (IT_STRING_CHARPOS (*it) >= 0);
7974 position = it->current.string_pos;
7976 /* With bidi reordering, the character to display might not be the
7977 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7978 that we were reseat()ed to a new string, whose paragraph
7979 direction is not known. */
7980 if (it->bidi_p && it->bidi_it.first_elt)
7982 get_visually_first_element (it);
7983 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7986 /* Time to check for invisible text? */
7987 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7989 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7991 if (!(!it->bidi_p
7992 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7993 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7995 /* With bidi non-linear iteration, we could find
7996 ourselves far beyond the last computed stop_charpos,
7997 with several other stop positions in between that we
7998 missed. Scan them all now, in buffer's logical
7999 order, until we find and handle the last stop_charpos
8000 that precedes our current position. */
8001 handle_stop_backwards (it, it->stop_charpos);
8002 return GET_NEXT_DISPLAY_ELEMENT (it);
8004 else
8006 if (it->bidi_p)
8008 /* Take note of the stop position we just moved
8009 across, for when we will move back across it. */
8010 it->prev_stop = it->stop_charpos;
8011 /* If we are at base paragraph embedding level, take
8012 note of the last stop position seen at this
8013 level. */
8014 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8015 it->base_level_stop = it->stop_charpos;
8017 handle_stop (it);
8019 /* Since a handler may have changed IT->method, we must
8020 recurse here. */
8021 return GET_NEXT_DISPLAY_ELEMENT (it);
8024 else if (it->bidi_p
8025 /* If we are before prev_stop, we may have overstepped
8026 on our way backwards a stop_pos, and if so, we need
8027 to handle that stop_pos. */
8028 && IT_STRING_CHARPOS (*it) < it->prev_stop
8029 /* We can sometimes back up for reasons that have nothing
8030 to do with bidi reordering. E.g., compositions. The
8031 code below is only needed when we are above the base
8032 embedding level, so test for that explicitly. */
8033 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8035 /* If we lost track of base_level_stop, we have no better
8036 place for handle_stop_backwards to start from than string
8037 beginning. This happens, e.g., when we were reseated to
8038 the previous screenful of text by vertical-motion. */
8039 if (it->base_level_stop <= 0
8040 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8041 it->base_level_stop = 0;
8042 handle_stop_backwards (it, it->base_level_stop);
8043 return GET_NEXT_DISPLAY_ELEMENT (it);
8047 if (it->current.overlay_string_index >= 0)
8049 /* Get the next character from an overlay string. In overlay
8050 strings, there is no field width or padding with spaces to
8051 do. */
8052 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8054 it->what = IT_EOB;
8055 return false;
8057 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8058 IT_STRING_BYTEPOS (*it),
8059 it->bidi_it.scan_dir < 0
8060 ? -1
8061 : SCHARS (it->string))
8062 && next_element_from_composition (it))
8064 return true;
8066 else if (STRING_MULTIBYTE (it->string))
8068 const unsigned char *s = (SDATA (it->string)
8069 + IT_STRING_BYTEPOS (*it));
8070 it->c = string_char_and_length (s, &it->len);
8072 else
8074 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8075 it->len = 1;
8078 else
8080 /* Get the next character from a Lisp string that is not an
8081 overlay string. Such strings come from the mode line, for
8082 example. We may have to pad with spaces, or truncate the
8083 string. See also next_element_from_c_string. */
8084 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8086 it->what = IT_EOB;
8087 return false;
8089 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8091 /* Pad with spaces. */
8092 it->c = ' ', it->len = 1;
8093 CHARPOS (position) = BYTEPOS (position) = -1;
8095 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8096 IT_STRING_BYTEPOS (*it),
8097 it->bidi_it.scan_dir < 0
8098 ? -1
8099 : it->string_nchars)
8100 && next_element_from_composition (it))
8102 return true;
8104 else if (STRING_MULTIBYTE (it->string))
8106 const unsigned char *s = (SDATA (it->string)
8107 + IT_STRING_BYTEPOS (*it));
8108 it->c = string_char_and_length (s, &it->len);
8110 else
8112 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8113 it->len = 1;
8117 /* Record what we have and where it came from. */
8118 it->what = IT_CHARACTER;
8119 it->object = it->string;
8120 it->position = position;
8121 return true;
8125 /* Load IT with next display element from C string IT->s.
8126 IT->string_nchars is the maximum number of characters to return
8127 from the string. IT->end_charpos may be greater than
8128 IT->string_nchars when this function is called, in which case we
8129 may have to return padding spaces. Value is false if end of string
8130 reached, including padding spaces. */
8132 static bool
8133 next_element_from_c_string (struct it *it)
8135 bool success_p = true;
8137 eassert (it->s);
8138 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8139 it->what = IT_CHARACTER;
8140 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8141 it->object = make_number (0);
8143 /* With bidi reordering, the character to display might not be the
8144 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8145 we were reseated to a new string, whose paragraph direction is
8146 not known. */
8147 if (it->bidi_p && it->bidi_it.first_elt)
8148 get_visually_first_element (it);
8150 /* IT's position can be greater than IT->string_nchars in case a
8151 field width or precision has been specified when the iterator was
8152 initialized. */
8153 if (IT_CHARPOS (*it) >= it->end_charpos)
8155 /* End of the game. */
8156 it->what = IT_EOB;
8157 success_p = false;
8159 else if (IT_CHARPOS (*it) >= it->string_nchars)
8161 /* Pad with spaces. */
8162 it->c = ' ', it->len = 1;
8163 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8165 else if (it->multibyte_p)
8166 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8167 else
8168 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8170 return success_p;
8174 /* Set up IT to return characters from an ellipsis, if appropriate.
8175 The definition of the ellipsis glyphs may come from a display table
8176 entry. This function fills IT with the first glyph from the
8177 ellipsis if an ellipsis is to be displayed. */
8179 static bool
8180 next_element_from_ellipsis (struct it *it)
8182 if (it->selective_display_ellipsis_p)
8183 setup_for_ellipsis (it, it->len);
8184 else
8186 /* The face at the current position may be different from the
8187 face we find after the invisible text. Remember what it
8188 was in IT->saved_face_id, and signal that it's there by
8189 setting face_before_selective_p. */
8190 it->saved_face_id = it->face_id;
8191 it->method = GET_FROM_BUFFER;
8192 it->object = it->w->contents;
8193 reseat_at_next_visible_line_start (it, true);
8194 it->face_before_selective_p = true;
8197 return GET_NEXT_DISPLAY_ELEMENT (it);
8201 /* Deliver an image display element. The iterator IT is already
8202 filled with image information (done in handle_display_prop). Value
8203 is always true. */
8206 static bool
8207 next_element_from_image (struct it *it)
8209 it->what = IT_IMAGE;
8210 return true;
8213 static bool
8214 next_element_from_xwidget (struct it *it)
8216 it->what = IT_XWIDGET;
8217 return true;
8221 /* Fill iterator IT with next display element from a stretch glyph
8222 property. IT->object is the value of the text property. Value is
8223 always true. */
8225 static bool
8226 next_element_from_stretch (struct it *it)
8228 it->what = IT_STRETCH;
8229 return true;
8232 /* Scan backwards from IT's current position until we find a stop
8233 position, or until BEGV. This is called when we find ourself
8234 before both the last known prev_stop and base_level_stop while
8235 reordering bidirectional text. */
8237 static void
8238 compute_stop_pos_backwards (struct it *it)
8240 const int SCAN_BACK_LIMIT = 1000;
8241 struct text_pos pos;
8242 struct display_pos save_current = it->current;
8243 struct text_pos save_position = it->position;
8244 ptrdiff_t charpos = IT_CHARPOS (*it);
8245 ptrdiff_t where_we_are = charpos;
8246 ptrdiff_t save_stop_pos = it->stop_charpos;
8247 ptrdiff_t save_end_pos = it->end_charpos;
8249 eassert (NILP (it->string) && !it->s);
8250 eassert (it->bidi_p);
8251 it->bidi_p = false;
8254 it->end_charpos = min (charpos + 1, ZV);
8255 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8256 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8257 reseat_1 (it, pos, false);
8258 compute_stop_pos (it);
8259 /* We must advance forward, right? */
8260 if (it->stop_charpos <= charpos)
8261 emacs_abort ();
8263 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8265 if (it->stop_charpos <= where_we_are)
8266 it->prev_stop = it->stop_charpos;
8267 else
8268 it->prev_stop = BEGV;
8269 it->bidi_p = true;
8270 it->current = save_current;
8271 it->position = save_position;
8272 it->stop_charpos = save_stop_pos;
8273 it->end_charpos = save_end_pos;
8276 /* Scan forward from CHARPOS in the current buffer/string, until we
8277 find a stop position > current IT's position. Then handle the stop
8278 position before that. This is called when we bump into a stop
8279 position while reordering bidirectional text. CHARPOS should be
8280 the last previously processed stop_pos (or BEGV/0, if none were
8281 processed yet) whose position is less that IT's current
8282 position. */
8284 static void
8285 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8287 bool bufp = !STRINGP (it->string);
8288 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8289 struct display_pos save_current = it->current;
8290 struct text_pos save_position = it->position;
8291 struct text_pos pos1;
8292 ptrdiff_t next_stop;
8294 /* Scan in strict logical order. */
8295 eassert (it->bidi_p);
8296 it->bidi_p = false;
8299 it->prev_stop = charpos;
8300 if (bufp)
8302 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8303 reseat_1 (it, pos1, false);
8305 else
8306 it->current.string_pos = string_pos (charpos, it->string);
8307 compute_stop_pos (it);
8308 /* We must advance forward, right? */
8309 if (it->stop_charpos <= it->prev_stop)
8310 emacs_abort ();
8311 charpos = it->stop_charpos;
8313 while (charpos <= where_we_are);
8315 it->bidi_p = true;
8316 it->current = save_current;
8317 it->position = save_position;
8318 next_stop = it->stop_charpos;
8319 it->stop_charpos = it->prev_stop;
8320 handle_stop (it);
8321 it->stop_charpos = next_stop;
8324 /* Load IT with the next display element from current_buffer. Value
8325 is false if end of buffer reached. IT->stop_charpos is the next
8326 position at which to stop and check for text properties or buffer
8327 end. */
8329 static bool
8330 next_element_from_buffer (struct it *it)
8332 bool success_p = true;
8334 eassert (IT_CHARPOS (*it) >= BEGV);
8335 eassert (NILP (it->string) && !it->s);
8336 eassert (!it->bidi_p
8337 || (EQ (it->bidi_it.string.lstring, Qnil)
8338 && it->bidi_it.string.s == NULL));
8340 /* With bidi reordering, the character to display might not be the
8341 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8342 we were reseat()ed to a new buffer position, which is potentially
8343 a different paragraph. */
8344 if (it->bidi_p && it->bidi_it.first_elt)
8346 get_visually_first_element (it);
8347 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8350 if (IT_CHARPOS (*it) >= it->stop_charpos)
8352 if (IT_CHARPOS (*it) >= it->end_charpos)
8354 bool overlay_strings_follow_p;
8356 /* End of the game, except when overlay strings follow that
8357 haven't been returned yet. */
8358 if (it->overlay_strings_at_end_processed_p)
8359 overlay_strings_follow_p = false;
8360 else
8362 it->overlay_strings_at_end_processed_p = true;
8363 overlay_strings_follow_p = get_overlay_strings (it, 0);
8366 if (overlay_strings_follow_p)
8367 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8368 else
8370 it->what = IT_EOB;
8371 it->position = it->current.pos;
8372 success_p = false;
8375 else if (!(!it->bidi_p
8376 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8377 || IT_CHARPOS (*it) == it->stop_charpos))
8379 /* With bidi non-linear iteration, we could find ourselves
8380 far beyond the last computed stop_charpos, with several
8381 other stop positions in between that we missed. Scan
8382 them all now, in buffer's logical order, until we find
8383 and handle the last stop_charpos that precedes our
8384 current position. */
8385 handle_stop_backwards (it, it->stop_charpos);
8386 it->ignore_overlay_strings_at_pos_p = false;
8387 return GET_NEXT_DISPLAY_ELEMENT (it);
8389 else
8391 if (it->bidi_p)
8393 /* Take note of the stop position we just moved across,
8394 for when we will move back across it. */
8395 it->prev_stop = it->stop_charpos;
8396 /* If we are at base paragraph embedding level, take
8397 note of the last stop position seen at this
8398 level. */
8399 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8400 it->base_level_stop = it->stop_charpos;
8402 handle_stop (it);
8403 it->ignore_overlay_strings_at_pos_p = false;
8404 return GET_NEXT_DISPLAY_ELEMENT (it);
8407 else if (it->bidi_p
8408 /* If we are before prev_stop, we may have overstepped on
8409 our way backwards a stop_pos, and if so, we need to
8410 handle that stop_pos. */
8411 && IT_CHARPOS (*it) < it->prev_stop
8412 /* We can sometimes back up for reasons that have nothing
8413 to do with bidi reordering. E.g., compositions. The
8414 code below is only needed when we are above the base
8415 embedding level, so test for that explicitly. */
8416 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8418 if (it->base_level_stop <= 0
8419 || IT_CHARPOS (*it) < it->base_level_stop)
8421 /* If we lost track of base_level_stop, we need to find
8422 prev_stop by looking backwards. This happens, e.g., when
8423 we were reseated to the previous screenful of text by
8424 vertical-motion. */
8425 it->base_level_stop = BEGV;
8426 compute_stop_pos_backwards (it);
8427 handle_stop_backwards (it, it->prev_stop);
8429 else
8430 handle_stop_backwards (it, it->base_level_stop);
8431 it->ignore_overlay_strings_at_pos_p = false;
8432 return GET_NEXT_DISPLAY_ELEMENT (it);
8434 else
8436 /* No face changes, overlays etc. in sight, so just return a
8437 character from current_buffer. */
8438 unsigned char *p;
8439 ptrdiff_t stop;
8441 /* We moved to the next buffer position, so any info about
8442 previously seen overlays is no longer valid. */
8443 it->ignore_overlay_strings_at_pos_p = false;
8445 /* Maybe run the redisplay end trigger hook. Performance note:
8446 This doesn't seem to cost measurable time. */
8447 if (it->redisplay_end_trigger_charpos
8448 && it->glyph_row
8449 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8450 run_redisplay_end_trigger_hook (it);
8452 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8453 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8454 stop)
8455 && next_element_from_composition (it))
8457 return true;
8460 /* Get the next character, maybe multibyte. */
8461 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8462 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8463 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8464 else
8465 it->c = *p, it->len = 1;
8467 /* Record what we have and where it came from. */
8468 it->what = IT_CHARACTER;
8469 it->object = it->w->contents;
8470 it->position = it->current.pos;
8472 /* Normally we return the character found above, except when we
8473 really want to return an ellipsis for selective display. */
8474 if (it->selective)
8476 if (it->c == '\n')
8478 /* A value of selective > 0 means hide lines indented more
8479 than that number of columns. */
8480 if (it->selective > 0
8481 && IT_CHARPOS (*it) + 1 < ZV
8482 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8483 IT_BYTEPOS (*it) + 1,
8484 it->selective))
8486 success_p = next_element_from_ellipsis (it);
8487 it->dpvec_char_len = -1;
8490 else if (it->c == '\r' && it->selective == -1)
8492 /* A value of selective == -1 means that everything from the
8493 CR to the end of the line is invisible, with maybe an
8494 ellipsis displayed for it. */
8495 success_p = next_element_from_ellipsis (it);
8496 it->dpvec_char_len = -1;
8501 /* Value is false if end of buffer reached. */
8502 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8503 return success_p;
8507 /* Run the redisplay end trigger hook for IT. */
8509 static void
8510 run_redisplay_end_trigger_hook (struct it *it)
8512 /* IT->glyph_row should be non-null, i.e. we should be actually
8513 displaying something, or otherwise we should not run the hook. */
8514 eassert (it->glyph_row);
8516 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8517 it->redisplay_end_trigger_charpos = 0;
8519 /* Since we are *trying* to run these functions, don't try to run
8520 them again, even if they get an error. */
8521 wset_redisplay_end_trigger (it->w, Qnil);
8522 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8523 make_number (charpos));
8525 /* Notice if it changed the face of the character we are on. */
8526 handle_face_prop (it);
8530 /* Deliver a composition display element. Unlike the other
8531 next_element_from_XXX, this function is not registered in the array
8532 get_next_element[]. It is called from next_element_from_buffer and
8533 next_element_from_string when necessary. */
8535 static bool
8536 next_element_from_composition (struct it *it)
8538 it->what = IT_COMPOSITION;
8539 it->len = it->cmp_it.nbytes;
8540 if (STRINGP (it->string))
8542 if (it->c < 0)
8544 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8545 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8546 return false;
8548 it->position = it->current.string_pos;
8549 it->object = it->string;
8550 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8551 IT_STRING_BYTEPOS (*it), it->string);
8553 else
8555 if (it->c < 0)
8557 IT_CHARPOS (*it) += it->cmp_it.nchars;
8558 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8559 if (it->bidi_p)
8561 if (it->bidi_it.new_paragraph)
8562 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8563 false);
8564 /* Resync the bidi iterator with IT's new position.
8565 FIXME: this doesn't support bidirectional text. */
8566 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8567 bidi_move_to_visually_next (&it->bidi_it);
8569 return false;
8571 it->position = it->current.pos;
8572 it->object = it->w->contents;
8573 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8574 IT_BYTEPOS (*it), Qnil);
8576 return true;
8581 /***********************************************************************
8582 Moving an iterator without producing glyphs
8583 ***********************************************************************/
8585 /* Check if iterator is at a position corresponding to a valid buffer
8586 position after some move_it_ call. */
8588 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8589 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8592 /* Move iterator IT to a specified buffer or X position within one
8593 line on the display without producing glyphs.
8595 OP should be a bit mask including some or all of these bits:
8596 MOVE_TO_X: Stop upon reaching x-position TO_X.
8597 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8598 Regardless of OP's value, stop upon reaching the end of the display line.
8600 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8601 This means, in particular, that TO_X includes window's horizontal
8602 scroll amount.
8604 The return value has several possible values that
8605 say what condition caused the scan to stop:
8607 MOVE_POS_MATCH_OR_ZV
8608 - when TO_POS or ZV was reached.
8610 MOVE_X_REACHED
8611 -when TO_X was reached before TO_POS or ZV were reached.
8613 MOVE_LINE_CONTINUED
8614 - when we reached the end of the display area and the line must
8615 be continued.
8617 MOVE_LINE_TRUNCATED
8618 - when we reached the end of the display area and the line is
8619 truncated.
8621 MOVE_NEWLINE_OR_CR
8622 - when we stopped at a line end, i.e. a newline or a CR and selective
8623 display is on. */
8625 static enum move_it_result
8626 move_it_in_display_line_to (struct it *it,
8627 ptrdiff_t to_charpos, int to_x,
8628 enum move_operation_enum op)
8630 enum move_it_result result = MOVE_UNDEFINED;
8631 struct glyph_row *saved_glyph_row;
8632 struct it wrap_it, atpos_it, atx_it, ppos_it;
8633 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8634 void *ppos_data = NULL;
8635 bool may_wrap = false;
8636 enum it_method prev_method = it->method;
8637 ptrdiff_t closest_pos UNINIT;
8638 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8639 bool saw_smaller_pos = prev_pos < to_charpos;
8640 bool line_number_pending = false;
8642 /* Don't produce glyphs in produce_glyphs. */
8643 saved_glyph_row = it->glyph_row;
8644 it->glyph_row = NULL;
8646 /* Use wrap_it to save a copy of IT wherever a word wrap could
8647 occur. Use atpos_it to save a copy of IT at the desired buffer
8648 position, if found, so that we can scan ahead and check if the
8649 word later overshoots the window edge. Use atx_it similarly, for
8650 pixel positions. */
8651 wrap_it.sp = -1;
8652 atpos_it.sp = -1;
8653 atx_it.sp = -1;
8655 /* Use ppos_it under bidi reordering to save a copy of IT for the
8656 initial position. We restore that position in IT when we have
8657 scanned the entire display line without finding a match for
8658 TO_CHARPOS and all the character positions are greater than
8659 TO_CHARPOS. We then restart the scan from the initial position,
8660 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8661 the closest to TO_CHARPOS. */
8662 if (it->bidi_p)
8664 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8666 SAVE_IT (ppos_it, *it, ppos_data);
8667 closest_pos = IT_CHARPOS (*it);
8669 else
8670 closest_pos = ZV;
8673 #define BUFFER_POS_REACHED_P() \
8674 ((op & MOVE_TO_POS) != 0 \
8675 && BUFFERP (it->object) \
8676 && (IT_CHARPOS (*it) == to_charpos \
8677 || ((!it->bidi_p \
8678 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8679 && IT_CHARPOS (*it) > to_charpos) \
8680 || (it->what == IT_COMPOSITION \
8681 && ((IT_CHARPOS (*it) > to_charpos \
8682 && to_charpos >= it->cmp_it.charpos) \
8683 || (IT_CHARPOS (*it) < to_charpos \
8684 && to_charpos <= it->cmp_it.charpos)))) \
8685 && (it->method == GET_FROM_BUFFER \
8686 || (it->method == GET_FROM_DISPLAY_VECTOR \
8687 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8689 if (it->hpos == 0)
8691 /* If line numbers are being displayed, produce a line number. */
8692 if (should_produce_line_number (it))
8694 if (it->current_x == it->first_visible_x)
8695 maybe_produce_line_number (it);
8696 else
8697 line_number_pending = true;
8699 /* If there's a line-/wrap-prefix, handle it. */
8700 if (it->method == GET_FROM_BUFFER)
8701 handle_line_prefix (it);
8704 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8705 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8707 while (true)
8709 int x, i, ascent = 0, descent = 0;
8711 /* Utility macro to reset an iterator with x, ascent, and descent. */
8712 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8713 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8714 (IT)->max_descent = descent)
8716 /* Stop if we move beyond TO_CHARPOS (after an image or a
8717 display string or stretch glyph). */
8718 if ((op & MOVE_TO_POS) != 0
8719 && BUFFERP (it->object)
8720 && it->method == GET_FROM_BUFFER
8721 && (((!it->bidi_p
8722 /* When the iterator is at base embedding level, we
8723 are guaranteed that characters are delivered for
8724 display in strictly increasing order of their
8725 buffer positions. */
8726 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8727 && IT_CHARPOS (*it) > to_charpos)
8728 || (it->bidi_p
8729 && (prev_method == GET_FROM_IMAGE
8730 || prev_method == GET_FROM_STRETCH
8731 || prev_method == GET_FROM_STRING)
8732 /* Passed TO_CHARPOS from left to right. */
8733 && ((prev_pos < to_charpos
8734 && IT_CHARPOS (*it) > to_charpos)
8735 /* Passed TO_CHARPOS from right to left. */
8736 || (prev_pos > to_charpos
8737 && IT_CHARPOS (*it) < to_charpos)))))
8739 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8741 result = MOVE_POS_MATCH_OR_ZV;
8742 break;
8744 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8745 /* If wrap_it is valid, the current position might be in a
8746 word that is wrapped. So, save the iterator in
8747 atpos_it and continue to see if wrapping happens. */
8748 SAVE_IT (atpos_it, *it, atpos_data);
8751 /* Stop when ZV reached.
8752 We used to stop here when TO_CHARPOS reached as well, but that is
8753 too soon if this glyph does not fit on this line. So we handle it
8754 explicitly below. */
8755 if (!get_next_display_element (it))
8757 result = MOVE_POS_MATCH_OR_ZV;
8758 break;
8761 if (it->line_wrap == TRUNCATE)
8763 if (BUFFER_POS_REACHED_P ())
8765 result = MOVE_POS_MATCH_OR_ZV;
8766 break;
8769 else
8771 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8773 if (IT_DISPLAYING_WHITESPACE (it))
8774 may_wrap = true;
8775 else if (may_wrap)
8777 /* We have reached a glyph that follows one or more
8778 whitespace characters. If the position is
8779 already found, we are done. */
8780 if (atpos_it.sp >= 0)
8782 RESTORE_IT (it, &atpos_it, atpos_data);
8783 result = MOVE_POS_MATCH_OR_ZV;
8784 goto done;
8786 if (atx_it.sp >= 0)
8788 RESTORE_IT (it, &atx_it, atx_data);
8789 result = MOVE_X_REACHED;
8790 goto done;
8792 /* Otherwise, we can wrap here. */
8793 SAVE_IT (wrap_it, *it, wrap_data);
8794 may_wrap = false;
8799 /* Remember the line height for the current line, in case
8800 the next element doesn't fit on the line. */
8801 ascent = it->max_ascent;
8802 descent = it->max_descent;
8804 /* The call to produce_glyphs will get the metrics of the
8805 display element IT is loaded with. Record the x-position
8806 before this display element, in case it doesn't fit on the
8807 line. */
8808 x = it->current_x;
8810 PRODUCE_GLYPHS (it);
8812 if (it->area != TEXT_AREA)
8814 prev_method = it->method;
8815 if (it->method == GET_FROM_BUFFER)
8816 prev_pos = IT_CHARPOS (*it);
8817 set_iterator_to_next (it, true);
8818 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8819 SET_TEXT_POS (this_line_min_pos,
8820 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8821 if (it->bidi_p
8822 && (op & MOVE_TO_POS)
8823 && IT_CHARPOS (*it) > to_charpos
8824 && IT_CHARPOS (*it) < closest_pos)
8825 closest_pos = IT_CHARPOS (*it);
8826 continue;
8829 /* The number of glyphs we get back in IT->nglyphs will normally
8830 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8831 character on a terminal frame, or (iii) a line end. For the
8832 second case, IT->nglyphs - 1 padding glyphs will be present.
8833 (On X frames, there is only one glyph produced for a
8834 composite character.)
8836 The behavior implemented below means, for continuation lines,
8837 that as many spaces of a TAB as fit on the current line are
8838 displayed there. For terminal frames, as many glyphs of a
8839 multi-glyph character are displayed in the current line, too.
8840 This is what the old redisplay code did, and we keep it that
8841 way. Under X, the whole shape of a complex character must
8842 fit on the line or it will be completely displayed in the
8843 next line.
8845 Note that both for tabs and padding glyphs, all glyphs have
8846 the same width. */
8847 if (it->nglyphs)
8849 /* More than one glyph or glyph doesn't fit on line. All
8850 glyphs have the same width. */
8851 int single_glyph_width = it->pixel_width / it->nglyphs;
8852 int new_x;
8853 int x_before_this_char = x;
8854 int hpos_before_this_char = it->hpos;
8856 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8858 new_x = x + single_glyph_width;
8860 /* We want to leave anything reaching TO_X to the caller. */
8861 if ((op & MOVE_TO_X) && new_x > to_x)
8863 if (BUFFER_POS_REACHED_P ())
8865 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8866 goto buffer_pos_reached;
8867 if (atpos_it.sp < 0)
8869 SAVE_IT (atpos_it, *it, atpos_data);
8870 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8873 else
8875 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8877 it->current_x = x;
8878 result = MOVE_X_REACHED;
8879 break;
8881 if (atx_it.sp < 0)
8883 SAVE_IT (atx_it, *it, atx_data);
8884 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8889 if (/* Lines are continued. */
8890 it->line_wrap != TRUNCATE
8891 && (/* And glyph doesn't fit on the line. */
8892 new_x > it->last_visible_x
8893 /* Or it fits exactly and we're on a window
8894 system frame. */
8895 || (new_x == it->last_visible_x
8896 && FRAME_WINDOW_P (it->f)
8897 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8898 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8899 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8901 bool moved_forward = false;
8903 if (/* IT->hpos == 0 means the very first glyph
8904 doesn't fit on the line, e.g. a wide image. */
8905 it->hpos == 0
8906 || (new_x == it->last_visible_x
8907 && FRAME_WINDOW_P (it->f)))
8909 ++it->hpos;
8910 it->current_x = new_x;
8912 /* The character's last glyph just barely fits
8913 in this row. */
8914 if (i == it->nglyphs - 1)
8916 /* If this is the destination position,
8917 return a position *before* it in this row,
8918 now that we know it fits in this row. */
8919 if (BUFFER_POS_REACHED_P ())
8921 bool can_wrap = true;
8923 /* If we are at a whitespace character
8924 that barely fits on this screen line,
8925 but the next character is also
8926 whitespace, we cannot wrap here. */
8927 if (it->line_wrap == WORD_WRAP
8928 && wrap_it.sp >= 0
8929 && may_wrap
8930 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8932 struct it tem_it;
8933 void *tem_data = NULL;
8935 SAVE_IT (tem_it, *it, tem_data);
8936 set_iterator_to_next (it, true);
8937 if (get_next_display_element (it)
8938 && IT_DISPLAYING_WHITESPACE (it))
8939 can_wrap = false;
8940 RESTORE_IT (it, &tem_it, tem_data);
8942 if (it->line_wrap != WORD_WRAP
8943 || wrap_it.sp < 0
8944 /* If we've just found whitespace
8945 where we can wrap, effectively
8946 ignore the previous wrap point --
8947 it is no longer relevant, but we
8948 won't have an opportunity to
8949 update it, since we've reached
8950 the edge of this screen line. */
8951 || (may_wrap && can_wrap
8952 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8954 it->hpos = hpos_before_this_char;
8955 it->current_x = x_before_this_char;
8956 result = MOVE_POS_MATCH_OR_ZV;
8957 break;
8959 if (it->line_wrap == WORD_WRAP
8960 && atpos_it.sp < 0)
8962 SAVE_IT (atpos_it, *it, atpos_data);
8963 atpos_it.current_x = x_before_this_char;
8964 atpos_it.hpos = hpos_before_this_char;
8968 prev_method = it->method;
8969 if (it->method == GET_FROM_BUFFER)
8970 prev_pos = IT_CHARPOS (*it);
8971 set_iterator_to_next (it, true);
8972 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8973 SET_TEXT_POS (this_line_min_pos,
8974 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8975 /* On graphical terminals, newlines may
8976 "overflow" into the fringe if
8977 overflow-newline-into-fringe is non-nil.
8978 On text terminals, and on graphical
8979 terminals with no right margin, newlines
8980 may overflow into the last glyph on the
8981 display line.*/
8982 if (!FRAME_WINDOW_P (it->f)
8983 || ((it->bidi_p
8984 && it->bidi_it.paragraph_dir == R2L)
8985 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8986 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8987 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8989 if (!get_next_display_element (it))
8991 result = MOVE_POS_MATCH_OR_ZV;
8992 break;
8994 moved_forward = true;
8995 if (BUFFER_POS_REACHED_P ())
8997 if (ITERATOR_AT_END_OF_LINE_P (it))
8998 result = MOVE_POS_MATCH_OR_ZV;
8999 else
9000 result = MOVE_LINE_CONTINUED;
9001 break;
9003 if (ITERATOR_AT_END_OF_LINE_P (it)
9004 && (it->line_wrap != WORD_WRAP
9005 || wrap_it.sp < 0
9006 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9008 result = MOVE_NEWLINE_OR_CR;
9009 break;
9014 else
9015 IT_RESET_X_ASCENT_DESCENT (it);
9017 /* If the screen line ends with whitespace, and we
9018 are under word-wrap, don't use wrap_it: it is no
9019 longer relevant, but we won't have an opportunity
9020 to update it, since we are done with this screen
9021 line. */
9022 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9023 /* If the character after the one which set the
9024 may_wrap flag is also whitespace, we can't
9025 wrap here, since the screen line cannot be
9026 wrapped in the middle of whitespace.
9027 Therefore, wrap_it _is_ relevant in that
9028 case. */
9029 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9031 /* If we've found TO_X, go back there, as we now
9032 know the last word fits on this screen line. */
9033 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9034 && atx_it.sp >= 0)
9036 RESTORE_IT (it, &atx_it, atx_data);
9037 atpos_it.sp = -1;
9038 atx_it.sp = -1;
9039 result = MOVE_X_REACHED;
9040 break;
9043 else if (wrap_it.sp >= 0)
9045 RESTORE_IT (it, &wrap_it, wrap_data);
9046 atpos_it.sp = -1;
9047 atx_it.sp = -1;
9050 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9051 IT_CHARPOS (*it)));
9052 result = MOVE_LINE_CONTINUED;
9053 break;
9056 if (BUFFER_POS_REACHED_P ())
9058 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9059 goto buffer_pos_reached;
9060 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9062 SAVE_IT (atpos_it, *it, atpos_data);
9063 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9067 if (new_x > it->first_visible_x)
9069 /* If we have reached the visible portion of the
9070 screen line, produce the line number if needed. */
9071 if (line_number_pending)
9073 line_number_pending = false;
9074 it->current_x = it->first_visible_x;
9075 maybe_produce_line_number (it);
9076 it->current_x += new_x - it->first_visible_x;
9078 /* Glyph is visible. Increment number of glyphs that
9079 would be displayed. */
9080 ++it->hpos;
9084 if (result != MOVE_UNDEFINED)
9085 break;
9087 else if (BUFFER_POS_REACHED_P ())
9089 buffer_pos_reached:
9090 IT_RESET_X_ASCENT_DESCENT (it);
9091 result = MOVE_POS_MATCH_OR_ZV;
9092 break;
9094 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9096 /* Stop when TO_X specified and reached. This check is
9097 necessary here because of lines consisting of a line end,
9098 only. The line end will not produce any glyphs and we
9099 would never get MOVE_X_REACHED. */
9100 eassert (it->nglyphs == 0);
9101 result = MOVE_X_REACHED;
9102 break;
9105 /* Is this a line end? If yes, we're done. */
9106 if (ITERATOR_AT_END_OF_LINE_P (it))
9108 /* If we are past TO_CHARPOS, but never saw any character
9109 positions smaller than TO_CHARPOS, return
9110 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9111 did. */
9112 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9114 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9116 if (closest_pos < ZV)
9118 RESTORE_IT (it, &ppos_it, ppos_data);
9119 /* Don't recurse if closest_pos is equal to
9120 to_charpos, since we have just tried that. */
9121 if (closest_pos != to_charpos)
9122 move_it_in_display_line_to (it, closest_pos, -1,
9123 MOVE_TO_POS);
9124 result = MOVE_POS_MATCH_OR_ZV;
9126 else
9127 goto buffer_pos_reached;
9129 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9130 && IT_CHARPOS (*it) > to_charpos)
9131 goto buffer_pos_reached;
9132 else
9133 result = MOVE_NEWLINE_OR_CR;
9135 else
9136 result = MOVE_NEWLINE_OR_CR;
9137 /* If we've processed the newline, make sure this flag is
9138 reset, as it must only be set when the newline itself is
9139 processed. */
9140 if (result == MOVE_NEWLINE_OR_CR)
9141 it->constrain_row_ascent_descent_p = false;
9142 break;
9145 prev_method = it->method;
9146 if (it->method == GET_FROM_BUFFER)
9147 prev_pos = IT_CHARPOS (*it);
9148 /* The current display element has been consumed. Advance
9149 to the next. */
9150 set_iterator_to_next (it, true);
9151 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9152 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9153 if (IT_CHARPOS (*it) < to_charpos)
9154 saw_smaller_pos = true;
9155 if (it->bidi_p
9156 && (op & MOVE_TO_POS)
9157 && IT_CHARPOS (*it) >= to_charpos
9158 && IT_CHARPOS (*it) < closest_pos)
9159 closest_pos = IT_CHARPOS (*it);
9161 /* Stop if lines are truncated and IT's current x-position is
9162 past the right edge of the window now. */
9163 if (it->line_wrap == TRUNCATE
9164 && it->current_x >= it->last_visible_x)
9166 if (!FRAME_WINDOW_P (it->f)
9167 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9168 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9169 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9170 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9172 bool at_eob_p = false;
9174 if ((at_eob_p = !get_next_display_element (it))
9175 || BUFFER_POS_REACHED_P ()
9176 /* If we are past TO_CHARPOS, but never saw any
9177 character positions smaller than TO_CHARPOS,
9178 return MOVE_POS_MATCH_OR_ZV, like the
9179 unidirectional display did. */
9180 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9181 && !saw_smaller_pos
9182 && IT_CHARPOS (*it) > to_charpos))
9184 if (it->bidi_p
9185 && !BUFFER_POS_REACHED_P ()
9186 && !at_eob_p && closest_pos < ZV)
9188 RESTORE_IT (it, &ppos_it, ppos_data);
9189 if (closest_pos != to_charpos)
9190 move_it_in_display_line_to (it, closest_pos, -1,
9191 MOVE_TO_POS);
9193 result = MOVE_POS_MATCH_OR_ZV;
9194 break;
9196 if (ITERATOR_AT_END_OF_LINE_P (it))
9198 result = MOVE_NEWLINE_OR_CR;
9199 break;
9202 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9203 && !saw_smaller_pos
9204 && IT_CHARPOS (*it) > to_charpos)
9206 if (closest_pos < ZV)
9208 RESTORE_IT (it, &ppos_it, ppos_data);
9209 if (closest_pos != to_charpos)
9210 move_it_in_display_line_to (it, closest_pos, -1,
9211 MOVE_TO_POS);
9213 result = MOVE_POS_MATCH_OR_ZV;
9214 break;
9216 result = MOVE_LINE_TRUNCATED;
9217 break;
9219 #undef IT_RESET_X_ASCENT_DESCENT
9222 #undef BUFFER_POS_REACHED_P
9224 /* If we scanned beyond TO_POS, restore the saved iterator either to
9225 the wrap point (if found), or to atpos/atx location. We decide which
9226 data to use to restore the saved iterator state by their X coordinates,
9227 since buffer positions might increase non-monotonically with screen
9228 coordinates due to bidi reordering. */
9229 if (result == MOVE_LINE_CONTINUED
9230 && it->line_wrap == WORD_WRAP
9231 && wrap_it.sp >= 0
9232 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9233 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9234 RESTORE_IT (it, &wrap_it, wrap_data);
9235 else if (atpos_it.sp >= 0)
9236 RESTORE_IT (it, &atpos_it, atpos_data);
9237 else if (atx_it.sp >= 0)
9238 RESTORE_IT (it, &atx_it, atx_data);
9240 done:
9242 if (atpos_data)
9243 bidi_unshelve_cache (atpos_data, true);
9244 if (atx_data)
9245 bidi_unshelve_cache (atx_data, true);
9246 if (wrap_data)
9247 bidi_unshelve_cache (wrap_data, true);
9248 if (ppos_data)
9249 bidi_unshelve_cache (ppos_data, true);
9251 /* Restore the iterator settings altered at the beginning of this
9252 function. */
9253 it->glyph_row = saved_glyph_row;
9254 return result;
9257 /* For external use. */
9258 void
9259 move_it_in_display_line (struct it *it,
9260 ptrdiff_t to_charpos, int to_x,
9261 enum move_operation_enum op)
9263 if (it->line_wrap == WORD_WRAP
9264 && (op & MOVE_TO_X))
9266 struct it save_it;
9267 void *save_data = NULL;
9268 int skip;
9270 SAVE_IT (save_it, *it, save_data);
9271 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9272 /* When word-wrap is on, TO_X may lie past the end
9273 of a wrapped line. Then it->current is the
9274 character on the next line, so backtrack to the
9275 space before the wrap point. */
9276 if (skip == MOVE_LINE_CONTINUED)
9278 int prev_x = max (it->current_x - 1, 0);
9279 RESTORE_IT (it, &save_it, save_data);
9280 move_it_in_display_line_to
9281 (it, -1, prev_x, MOVE_TO_X);
9283 else
9284 bidi_unshelve_cache (save_data, true);
9286 else
9287 move_it_in_display_line_to (it, to_charpos, to_x, op);
9291 /* Move IT forward until it satisfies one or more of the criteria in
9292 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9294 OP is a bit-mask that specifies where to stop, and in particular,
9295 which of those four position arguments makes a difference. See the
9296 description of enum move_operation_enum.
9298 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9299 screen line, this function will set IT to the next position that is
9300 displayed to the right of TO_CHARPOS on the screen.
9302 Return the maximum pixel length of any line scanned but never more
9303 than it.last_visible_x. */
9306 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9308 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9309 int line_height, line_start_x = 0, reached = 0;
9310 int max_current_x = 0;
9311 void *backup_data = NULL;
9313 for (;;)
9315 if (op & MOVE_TO_VPOS)
9317 /* If no TO_CHARPOS and no TO_X specified, stop at the
9318 start of the line TO_VPOS. */
9319 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9321 if (it->vpos == to_vpos)
9323 reached = 1;
9324 break;
9326 else
9327 skip = move_it_in_display_line_to (it, -1, -1, 0);
9329 else
9331 /* TO_VPOS >= 0 means stop at TO_X in the line at
9332 TO_VPOS, or at TO_POS, whichever comes first. */
9333 if (it->vpos == to_vpos)
9335 reached = 2;
9336 break;
9339 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9341 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9343 reached = 3;
9344 break;
9346 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9348 /* We have reached TO_X but not in the line we want. */
9349 skip = move_it_in_display_line_to (it, to_charpos,
9350 -1, MOVE_TO_POS);
9351 if (skip == MOVE_POS_MATCH_OR_ZV)
9353 reached = 4;
9354 break;
9359 else if (op & MOVE_TO_Y)
9361 struct it it_backup;
9363 if (it->line_wrap == WORD_WRAP)
9364 SAVE_IT (it_backup, *it, backup_data);
9366 /* TO_Y specified means stop at TO_X in the line containing
9367 TO_Y---or at TO_CHARPOS if this is reached first. The
9368 problem is that we can't really tell whether the line
9369 contains TO_Y before we have completely scanned it, and
9370 this may skip past TO_X. What we do is to first scan to
9371 TO_X.
9373 If TO_X is not specified, use a TO_X of zero. The reason
9374 is to make the outcome of this function more predictable.
9375 If we didn't use TO_X == 0, we would stop at the end of
9376 the line which is probably not what a caller would expect
9377 to happen. */
9378 skip = move_it_in_display_line_to
9379 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9380 (MOVE_TO_X | (op & MOVE_TO_POS)));
9382 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9383 if (skip == MOVE_POS_MATCH_OR_ZV)
9384 reached = 5;
9385 else if (skip == MOVE_X_REACHED)
9387 /* If TO_X was reached, we want to know whether TO_Y is
9388 in the line. We know this is the case if the already
9389 scanned glyphs make the line tall enough. Otherwise,
9390 we must check by scanning the rest of the line. */
9391 line_height = it->max_ascent + it->max_descent;
9392 if (to_y >= it->current_y
9393 && to_y < it->current_y + line_height)
9395 reached = 6;
9396 break;
9398 SAVE_IT (it_backup, *it, backup_data);
9399 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9400 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9401 op & MOVE_TO_POS);
9402 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9403 line_height = it->max_ascent + it->max_descent;
9404 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9406 if (to_y >= it->current_y
9407 && to_y < it->current_y + line_height)
9409 /* If TO_Y is in this line and TO_X was reached
9410 above, we scanned too far. We have to restore
9411 IT's settings to the ones before skipping. But
9412 keep the more accurate values of max_ascent and
9413 max_descent we've found while skipping the rest
9414 of the line, for the sake of callers, such as
9415 pos_visible_p, that need to know the line
9416 height. */
9417 int max_ascent = it->max_ascent;
9418 int max_descent = it->max_descent;
9420 RESTORE_IT (it, &it_backup, backup_data);
9421 it->max_ascent = max_ascent;
9422 it->max_descent = max_descent;
9423 reached = 6;
9425 else
9427 skip = skip2;
9428 if (skip == MOVE_POS_MATCH_OR_ZV)
9429 reached = 7;
9432 else
9434 /* Check whether TO_Y is in this line. */
9435 line_height = it->max_ascent + it->max_descent;
9436 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9438 if (to_y >= it->current_y
9439 && to_y < it->current_y + line_height)
9441 if (to_y > it->current_y)
9442 max_current_x = max (it->current_x, max_current_x);
9444 /* When word-wrap is on, TO_X may lie past the end
9445 of a wrapped line. Then it->current is the
9446 character on the next line, so backtrack to the
9447 space before the wrap point. */
9448 if (skip == MOVE_LINE_CONTINUED
9449 && it->line_wrap == WORD_WRAP)
9451 int prev_x = max (it->current_x - 1, 0);
9452 RESTORE_IT (it, &it_backup, backup_data);
9453 skip = move_it_in_display_line_to
9454 (it, -1, prev_x, MOVE_TO_X);
9457 reached = 6;
9461 if (reached)
9463 max_current_x = max (it->current_x, max_current_x);
9464 break;
9467 else if (BUFFERP (it->object)
9468 && (it->method == GET_FROM_BUFFER
9469 || it->method == GET_FROM_STRETCH)
9470 && IT_CHARPOS (*it) >= to_charpos
9471 /* Under bidi iteration, a call to set_iterator_to_next
9472 can scan far beyond to_charpos if the initial
9473 portion of the next line needs to be reordered. In
9474 that case, give move_it_in_display_line_to another
9475 chance below. */
9476 && !(it->bidi_p
9477 && it->bidi_it.scan_dir == -1))
9478 skip = MOVE_POS_MATCH_OR_ZV;
9479 else
9480 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9482 switch (skip)
9484 case MOVE_POS_MATCH_OR_ZV:
9485 max_current_x = max (it->current_x, max_current_x);
9486 reached = 8;
9487 goto out;
9489 case MOVE_NEWLINE_OR_CR:
9490 max_current_x = max (it->current_x, max_current_x);
9491 set_iterator_to_next (it, true);
9492 it->continuation_lines_width = 0;
9493 break;
9495 case MOVE_LINE_TRUNCATED:
9496 max_current_x = it->last_visible_x;
9497 it->continuation_lines_width = 0;
9498 reseat_at_next_visible_line_start (it, false);
9499 if ((op & MOVE_TO_POS) != 0
9500 && IT_CHARPOS (*it) > to_charpos)
9502 reached = 9;
9503 goto out;
9505 break;
9507 case MOVE_LINE_CONTINUED:
9508 max_current_x = it->last_visible_x;
9509 /* For continued lines ending in a tab, some of the glyphs
9510 associated with the tab are displayed on the current
9511 line. Since it->current_x does not include these glyphs,
9512 we use it->last_visible_x instead. */
9513 if (it->c == '\t')
9515 it->continuation_lines_width += it->last_visible_x;
9516 /* When moving by vpos, ensure that the iterator really
9517 advances to the next line (bug#847, bug#969). Fixme:
9518 do we need to do this in other circumstances? */
9519 if (it->current_x != it->last_visible_x
9520 && (op & MOVE_TO_VPOS)
9521 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9523 line_start_x = it->current_x + it->pixel_width
9524 - it->last_visible_x;
9525 if (FRAME_WINDOW_P (it->f))
9527 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9528 struct font *face_font = face->font;
9530 /* When display_line produces a continued line
9531 that ends in a TAB, it skips a tab stop that
9532 is closer than the font's space character
9533 width (see x_produce_glyphs where it produces
9534 the stretch glyph which represents a TAB).
9535 We need to reproduce the same logic here. */
9536 eassert (face_font);
9537 if (face_font)
9539 if (line_start_x < face_font->space_width)
9540 line_start_x
9541 += it->tab_width * face_font->space_width;
9544 set_iterator_to_next (it, false);
9547 else
9548 it->continuation_lines_width += it->current_x;
9549 break;
9551 default:
9552 emacs_abort ();
9555 /* Reset/increment for the next run. */
9556 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9557 it->current_x = line_start_x;
9558 line_start_x = 0;
9559 it->hpos = 0;
9560 it->current_y += it->max_ascent + it->max_descent;
9561 ++it->vpos;
9562 last_height = it->max_ascent + it->max_descent;
9563 it->max_ascent = it->max_descent = 0;
9566 out:
9568 /* On text terminals, we may stop at the end of a line in the middle
9569 of a multi-character glyph. If the glyph itself is continued,
9570 i.e. it is actually displayed on the next line, don't treat this
9571 stopping point as valid; move to the next line instead (unless
9572 that brings us offscreen). */
9573 if (!FRAME_WINDOW_P (it->f)
9574 && op & MOVE_TO_POS
9575 && IT_CHARPOS (*it) == to_charpos
9576 && it->what == IT_CHARACTER
9577 && it->nglyphs > 1
9578 && it->line_wrap == WINDOW_WRAP
9579 && it->current_x == it->last_visible_x - 1
9580 && it->c != '\n'
9581 && it->c != '\t'
9582 && it->w->window_end_valid
9583 && it->vpos < it->w->window_end_vpos)
9585 it->continuation_lines_width += it->current_x;
9586 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9587 it->current_y += it->max_ascent + it->max_descent;
9588 ++it->vpos;
9589 last_height = it->max_ascent + it->max_descent;
9592 if (backup_data)
9593 bidi_unshelve_cache (backup_data, true);
9595 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9597 return max_current_x;
9601 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9603 If DY > 0, move IT backward at least that many pixels. DY = 0
9604 means move IT backward to the preceding line start or BEGV. This
9605 function may move over more than DY pixels if IT->current_y - DY
9606 ends up in the middle of a line; in this case IT->current_y will be
9607 set to the top of the line moved to. */
9609 void
9610 move_it_vertically_backward (struct it *it, int dy)
9612 int nlines, h;
9613 struct it it2, it3;
9614 void *it2data = NULL, *it3data = NULL;
9615 ptrdiff_t start_pos;
9616 int nchars_per_row
9617 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9618 ptrdiff_t pos_limit;
9620 move_further_back:
9621 eassert (dy >= 0);
9623 start_pos = IT_CHARPOS (*it);
9625 /* Estimate how many newlines we must move back. */
9626 nlines = max (1, dy / default_line_pixel_height (it->w));
9627 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9628 pos_limit = BEGV;
9629 else
9630 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9632 /* Set the iterator's position that many lines back. But don't go
9633 back more than NLINES full screen lines -- this wins a day with
9634 buffers which have very long lines. */
9635 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9636 back_to_previous_visible_line_start (it);
9638 /* Reseat the iterator here. When moving backward, we don't want
9639 reseat to skip forward over invisible text, set up the iterator
9640 to deliver from overlay strings at the new position etc. So,
9641 use reseat_1 here. */
9642 reseat_1 (it, it->current.pos, true);
9644 /* We are now surely at a line start. */
9645 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9646 reordering is in effect. */
9647 it->continuation_lines_width = 0;
9649 /* Move forward and see what y-distance we moved. First move to the
9650 start of the next line so that we get its height. We need this
9651 height to be able to tell whether we reached the specified
9652 y-distance. */
9653 SAVE_IT (it2, *it, it2data);
9654 it2.max_ascent = it2.max_descent = 0;
9657 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9658 MOVE_TO_POS | MOVE_TO_VPOS);
9660 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9661 /* If we are in a display string which starts at START_POS,
9662 and that display string includes a newline, and we are
9663 right after that newline (i.e. at the beginning of a
9664 display line), exit the loop, because otherwise we will
9665 infloop, since move_it_to will see that it is already at
9666 START_POS and will not move. */
9667 || (it2.method == GET_FROM_STRING
9668 && IT_CHARPOS (it2) == start_pos
9669 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9670 eassert (IT_CHARPOS (*it) >= BEGV);
9671 SAVE_IT (it3, it2, it3data);
9673 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9674 eassert (IT_CHARPOS (*it) >= BEGV);
9675 /* H is the actual vertical distance from the position in *IT
9676 and the starting position. */
9677 h = it2.current_y - it->current_y;
9678 /* NLINES is the distance in number of lines. */
9679 nlines = it2.vpos - it->vpos;
9681 /* Correct IT's y and vpos position
9682 so that they are relative to the starting point. */
9683 it->vpos -= nlines;
9684 it->current_y -= h;
9686 if (dy == 0)
9688 /* DY == 0 means move to the start of the screen line. The
9689 value of nlines is > 0 if continuation lines were involved,
9690 or if the original IT position was at start of a line. */
9691 RESTORE_IT (it, it, it2data);
9692 if (nlines > 0)
9693 move_it_by_lines (it, nlines);
9694 /* The above code moves us to some position NLINES down,
9695 usually to its first glyph (leftmost in an L2R line), but
9696 that's not necessarily the start of the line, under bidi
9697 reordering. We want to get to the character position
9698 that is immediately after the newline of the previous
9699 line. */
9700 if (it->bidi_p
9701 && !it->continuation_lines_width
9702 && !STRINGP (it->string)
9703 && IT_CHARPOS (*it) > BEGV
9704 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9706 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9708 DEC_BOTH (cp, bp);
9709 cp = find_newline_no_quit (cp, bp, -1, NULL);
9710 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9712 bidi_unshelve_cache (it3data, true);
9714 else
9716 /* The y-position we try to reach, relative to *IT.
9717 Note that H has been subtracted in front of the if-statement. */
9718 int target_y = it->current_y + h - dy;
9719 int y0 = it3.current_y;
9720 int y1;
9721 int line_height;
9723 RESTORE_IT (&it3, &it3, it3data);
9724 y1 = line_bottom_y (&it3);
9725 line_height = y1 - y0;
9726 RESTORE_IT (it, it, it2data);
9727 /* If we did not reach target_y, try to move further backward if
9728 we can. If we moved too far backward, try to move forward. */
9729 if (target_y < it->current_y
9730 /* This is heuristic. In a window that's 3 lines high, with
9731 a line height of 13 pixels each, recentering with point
9732 on the bottom line will try to move -39/2 = 19 pixels
9733 backward. Try to avoid moving into the first line. */
9734 && (it->current_y - target_y
9735 > min (window_box_height (it->w), line_height * 2 / 3))
9736 && IT_CHARPOS (*it) > BEGV)
9738 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9739 target_y - it->current_y));
9740 dy = it->current_y - target_y;
9741 goto move_further_back;
9743 else if (target_y >= it->current_y + line_height
9744 && IT_CHARPOS (*it) < ZV)
9746 /* Should move forward by at least one line, maybe more.
9748 Note: Calling move_it_by_lines can be expensive on
9749 terminal frames, where compute_motion is used (via
9750 vmotion) to do the job, when there are very long lines
9751 and truncate-lines is nil. That's the reason for
9752 treating terminal frames specially here. */
9754 if (!FRAME_WINDOW_P (it->f))
9755 move_it_vertically (it, target_y - it->current_y);
9756 else
9760 move_it_by_lines (it, 1);
9762 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9769 /* Move IT by a specified amount of pixel lines DY. DY negative means
9770 move backwards. DY = 0 means move to start of screen line. At the
9771 end, IT will be on the start of a screen line. */
9773 void
9774 move_it_vertically (struct it *it, int dy)
9776 if (dy <= 0)
9777 move_it_vertically_backward (it, -dy);
9778 else
9780 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9781 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9782 MOVE_TO_POS | MOVE_TO_Y);
9783 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9785 /* If buffer ends in ZV without a newline, move to the start of
9786 the line to satisfy the post-condition. */
9787 if (IT_CHARPOS (*it) == ZV
9788 && ZV > BEGV
9789 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9790 move_it_by_lines (it, 0);
9795 /* Move iterator IT past the end of the text line it is in. */
9797 void
9798 move_it_past_eol (struct it *it)
9800 enum move_it_result rc;
9802 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9803 if (rc == MOVE_NEWLINE_OR_CR)
9804 set_iterator_to_next (it, false);
9808 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9809 negative means move up. DVPOS == 0 means move to the start of the
9810 screen line.
9812 Optimization idea: If we would know that IT->f doesn't use
9813 a face with proportional font, we could be faster for
9814 truncate-lines nil. */
9816 void
9817 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9820 /* The commented-out optimization uses vmotion on terminals. This
9821 gives bad results, because elements like it->what, on which
9822 callers such as pos_visible_p rely, aren't updated. */
9823 /* struct position pos;
9824 if (!FRAME_WINDOW_P (it->f))
9826 struct text_pos textpos;
9828 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9829 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9830 reseat (it, textpos, true);
9831 it->vpos += pos.vpos;
9832 it->current_y += pos.vpos;
9834 else */
9836 if (dvpos == 0)
9838 /* DVPOS == 0 means move to the start of the screen line. */
9839 move_it_vertically_backward (it, 0);
9840 /* Let next call to line_bottom_y calculate real line height. */
9841 last_height = 0;
9843 else if (dvpos > 0)
9845 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9846 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9848 /* Only move to the next buffer position if we ended up in a
9849 string from display property, not in an overlay string
9850 (before-string or after-string). That is because the
9851 latter don't conceal the underlying buffer position, so
9852 we can ask to move the iterator to the exact position we
9853 are interested in. Note that, even if we are already at
9854 IT_CHARPOS (*it), the call below is not a no-op, as it
9855 will detect that we are at the end of the string, pop the
9856 iterator, and compute it->current_x and it->hpos
9857 correctly. */
9858 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9859 -1, -1, -1, MOVE_TO_POS);
9862 else
9864 struct it it2;
9865 void *it2data = NULL;
9866 ptrdiff_t start_charpos, i;
9867 int nchars_per_row
9868 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9869 bool hit_pos_limit = false;
9870 ptrdiff_t pos_limit;
9872 /* Start at the beginning of the screen line containing IT's
9873 position. This may actually move vertically backwards,
9874 in case of overlays, so adjust dvpos accordingly. */
9875 dvpos += it->vpos;
9876 move_it_vertically_backward (it, 0);
9877 dvpos -= it->vpos;
9879 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9880 screen lines, and reseat the iterator there. */
9881 start_charpos = IT_CHARPOS (*it);
9882 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9883 pos_limit = BEGV;
9884 else
9885 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9887 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9888 back_to_previous_visible_line_start (it);
9889 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9890 hit_pos_limit = true;
9891 reseat (it, it->current.pos, true);
9893 /* Move further back if we end up in a string or an image. */
9894 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9896 /* First try to move to start of display line. */
9897 dvpos += it->vpos;
9898 move_it_vertically_backward (it, 0);
9899 dvpos -= it->vpos;
9900 if (IT_POS_VALID_AFTER_MOVE_P (it))
9901 break;
9902 /* If start of line is still in string or image,
9903 move further back. */
9904 back_to_previous_visible_line_start (it);
9905 reseat (it, it->current.pos, true);
9906 dvpos--;
9909 it->current_x = it->hpos = 0;
9911 /* Above call may have moved too far if continuation lines
9912 are involved. Scan forward and see if it did. */
9913 SAVE_IT (it2, *it, it2data);
9914 it2.vpos = it2.current_y = 0;
9915 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9916 it->vpos -= it2.vpos;
9917 it->current_y -= it2.current_y;
9918 it->current_x = it->hpos = 0;
9920 /* If we moved too far back, move IT some lines forward. */
9921 if (it2.vpos > -dvpos)
9923 int delta = it2.vpos + dvpos;
9925 RESTORE_IT (&it2, &it2, it2data);
9926 SAVE_IT (it2, *it, it2data);
9927 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9928 /* Move back again if we got too far ahead. */
9929 if (IT_CHARPOS (*it) >= start_charpos)
9930 RESTORE_IT (it, &it2, it2data);
9931 else
9932 bidi_unshelve_cache (it2data, true);
9934 else if (hit_pos_limit && pos_limit > BEGV
9935 && dvpos < 0 && it2.vpos < -dvpos)
9937 /* If we hit the limit, but still didn't make it far enough
9938 back, that means there's a display string with a newline
9939 covering a large chunk of text, and that caused
9940 back_to_previous_visible_line_start try to go too far.
9941 Punish those who commit such atrocities by going back
9942 until we've reached DVPOS, after lifting the limit, which
9943 could make it slow for very long lines. "If it hurts,
9944 don't do that!" */
9945 dvpos += it2.vpos;
9946 RESTORE_IT (it, it, it2data);
9947 for (i = -dvpos; i > 0; --i)
9949 back_to_previous_visible_line_start (it);
9950 it->vpos--;
9952 reseat_1 (it, it->current.pos, true);
9954 else
9955 RESTORE_IT (it, it, it2data);
9960 partial_line_height (struct it *it_origin)
9962 int partial_height;
9963 void *it_data = NULL;
9964 struct it it;
9965 SAVE_IT (it, *it_origin, it_data);
9966 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9967 MOVE_TO_POS | MOVE_TO_Y);
9968 if (it.what == IT_EOB)
9970 int vis_height = it.last_visible_y - it.current_y;
9971 int height = it.ascent + it.descent;
9972 partial_height = (vis_height < height) ? vis_height : 0;
9974 else
9976 int last_line_y = it.current_y;
9977 move_it_by_lines (&it, 1);
9978 partial_height = (it.current_y > it.last_visible_y)
9979 ? it.last_visible_y - last_line_y : 0;
9981 RESTORE_IT (&it, &it, it_data);
9982 return partial_height;
9985 /* Return true if IT points into the middle of a display vector. */
9987 bool
9988 in_display_vector_p (struct it *it)
9990 return (it->method == GET_FROM_DISPLAY_VECTOR
9991 && it->current.dpvec_index > 0
9992 && it->dpvec + it->current.dpvec_index != it->dpend);
9995 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9996 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9997 WINDOW must be a live window and defaults to the selected one. The
9998 return value is a cons of the maximum pixel-width of any text line and
9999 the maximum pixel-height of all text lines.
10001 The optional argument FROM, if non-nil, specifies the first text
10002 position and defaults to the minimum accessible position of the buffer.
10003 If FROM is t, use the minimum accessible position that starts a
10004 non-empty line. TO, if non-nil, specifies the last text position and
10005 defaults to the maximum accessible position of the buffer. If TO is t,
10006 use the maximum accessible position that ends a non-empty line.
10008 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10009 width that can be returned. X-LIMIT nil or omitted, means to use the
10010 pixel-width of WINDOW's body; use this if you want to know how high
10011 WINDOW should be become in order to fit all of its buffer's text with
10012 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10013 if you intend to change WINDOW's width. In any case, text whose
10014 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10015 of long lines can take some time, it's always a good idea to make this
10016 argument as small as possible; in particular, if the buffer contains
10017 long lines that shall be truncated anyway.
10019 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10020 height (excluding the height of the mode- or header-line, if any) that
10021 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10022 ignored. Since calculating the text height of a large buffer can take
10023 some time, it makes sense to specify this argument if the size of the
10024 buffer is large or unknown.
10026 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10027 include the height of the mode- or header-line of WINDOW in the return
10028 value. If it is either the symbol `mode-line' or `header-line', include
10029 only the height of that line, if present, in the return value. If t,
10030 include the height of both, if present, in the return value. */)
10031 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10032 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10034 struct window *w = decode_live_window (window);
10035 Lisp_Object buffer = w->contents;
10036 struct buffer *b;
10037 struct it it;
10038 struct buffer *old_b = NULL;
10039 ptrdiff_t start, end, pos;
10040 struct text_pos startp;
10041 void *itdata = NULL;
10042 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10044 CHECK_BUFFER (buffer);
10045 b = XBUFFER (buffer);
10047 if (b != current_buffer)
10049 old_b = current_buffer;
10050 set_buffer_internal (b);
10053 if (NILP (from))
10054 start = BEGV;
10055 else if (EQ (from, Qt))
10057 start = pos = BEGV;
10058 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10059 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10060 start = pos;
10061 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10062 start = pos;
10064 else
10066 CHECK_NUMBER_COERCE_MARKER (from);
10067 start = min (max (XINT (from), BEGV), ZV);
10070 if (NILP (to))
10071 end = ZV;
10072 else if (EQ (to, Qt))
10074 end = pos = ZV;
10075 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10076 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10077 end = pos;
10078 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10079 end = pos;
10081 else
10083 CHECK_NUMBER_COERCE_MARKER (to);
10084 end = max (start, min (XINT (to), ZV));
10087 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10088 max_x = XINT (x_limit);
10090 if (NILP (y_limit))
10091 max_y = INT_MAX;
10092 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10093 max_y = XINT (y_limit);
10095 itdata = bidi_shelve_cache ();
10096 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10097 start_display (&it, w, startp);
10099 if (NILP (x_limit))
10100 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10101 else
10103 it.last_visible_x = max_x;
10104 /* Actually, we never want move_it_to stop at to_x. But to make
10105 sure that move_it_in_display_line_to always moves far enough,
10106 we set it to INT_MAX and specify MOVE_TO_X. */
10107 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10108 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10109 /* Don't return more than X-LIMIT. */
10110 if (x > max_x)
10111 x = max_x;
10114 /* Subtract height of header-line which was counted automatically by
10115 start_display. */
10116 y = it.current_y + it.max_ascent + it.max_descent
10117 - WINDOW_HEADER_LINE_HEIGHT (w);
10118 /* Don't return more than Y-LIMIT. */
10119 if (y > max_y)
10120 y = max_y;
10122 if (EQ (mode_and_header_line, Qheader_line)
10123 || EQ (mode_and_header_line, Qt))
10124 /* Re-add height of header-line as requested. */
10125 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10127 if (EQ (mode_and_header_line, Qmode_line)
10128 || EQ (mode_and_header_line, Qt))
10129 /* Add height of mode-line as requested. */
10130 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10132 bidi_unshelve_cache (itdata, false);
10134 if (old_b)
10135 set_buffer_internal (old_b);
10137 return Fcons (make_number (x), make_number (y));
10140 /***********************************************************************
10141 Messages
10142 ***********************************************************************/
10144 /* Return the number of arguments the format string FORMAT needs. */
10146 static ptrdiff_t
10147 format_nargs (char const *format)
10149 ptrdiff_t nargs = 0;
10150 for (char const *p = format; (p = strchr (p, '%')); p++)
10151 if (p[1] == '%')
10152 p++;
10153 else
10154 nargs++;
10155 return nargs;
10158 /* Add a message with format string FORMAT and formatted arguments
10159 to *Messages*. */
10161 void
10162 add_to_log (const char *format, ...)
10164 va_list ap;
10165 va_start (ap, format);
10166 vadd_to_log (format, ap);
10167 va_end (ap);
10170 void
10171 vadd_to_log (char const *format, va_list ap)
10173 ptrdiff_t form_nargs = format_nargs (format);
10174 ptrdiff_t nargs = 1 + form_nargs;
10175 Lisp_Object args[10];
10176 eassert (nargs <= ARRAYELTS (args));
10177 AUTO_STRING (args0, format);
10178 args[0] = args0;
10179 for (ptrdiff_t i = 1; i <= nargs; i++)
10180 args[i] = va_arg (ap, Lisp_Object);
10181 Lisp_Object msg = Qnil;
10182 msg = Fformat_message (nargs, args);
10184 ptrdiff_t len = SBYTES (msg) + 1;
10185 USE_SAFE_ALLOCA;
10186 char *buffer = SAFE_ALLOCA (len);
10187 memcpy (buffer, SDATA (msg), len);
10189 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10190 SAFE_FREE ();
10194 /* Output a newline in the *Messages* buffer if "needs" one. */
10196 void
10197 message_log_maybe_newline (void)
10199 if (message_log_need_newline)
10200 message_dolog ("", 0, true, false);
10204 /* Add a string M of length NBYTES to the message log, optionally
10205 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10206 true, means interpret the contents of M as multibyte. This
10207 function calls low-level routines in order to bypass text property
10208 hooks, etc. which might not be safe to run.
10210 This may GC (insert may run before/after change hooks),
10211 so the buffer M must NOT point to a Lisp string. */
10213 void
10214 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10216 const unsigned char *msg = (const unsigned char *) m;
10218 if (!NILP (Vmemory_full))
10219 return;
10221 if (!NILP (Vmessage_log_max))
10223 struct buffer *oldbuf;
10224 Lisp_Object oldpoint, oldbegv, oldzv;
10225 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10226 ptrdiff_t point_at_end = 0;
10227 ptrdiff_t zv_at_end = 0;
10228 Lisp_Object old_deactivate_mark;
10230 old_deactivate_mark = Vdeactivate_mark;
10231 oldbuf = current_buffer;
10233 /* Ensure the Messages buffer exists, and switch to it.
10234 If we created it, set the major-mode. */
10235 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10236 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10237 if (newbuffer
10238 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10239 call0 (intern ("messages-buffer-mode"));
10241 bset_undo_list (current_buffer, Qt);
10242 bset_cache_long_scans (current_buffer, Qnil);
10244 oldpoint = message_dolog_marker1;
10245 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10246 oldbegv = message_dolog_marker2;
10247 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10248 oldzv = message_dolog_marker3;
10249 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10251 if (PT == Z)
10252 point_at_end = 1;
10253 if (ZV == Z)
10254 zv_at_end = 1;
10256 BEGV = BEG;
10257 BEGV_BYTE = BEG_BYTE;
10258 ZV = Z;
10259 ZV_BYTE = Z_BYTE;
10260 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10262 /* Insert the string--maybe converting multibyte to single byte
10263 or vice versa, so that all the text fits the buffer. */
10264 if (multibyte
10265 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10267 ptrdiff_t i;
10268 int c, char_bytes;
10269 char work[1];
10271 /* Convert a multibyte string to single-byte
10272 for the *Message* buffer. */
10273 for (i = 0; i < nbytes; i += char_bytes)
10275 c = string_char_and_length (msg + i, &char_bytes);
10276 work[0] = CHAR_TO_BYTE8 (c);
10277 insert_1_both (work, 1, 1, true, false, false);
10280 else if (! multibyte
10281 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10283 ptrdiff_t i;
10284 int c, char_bytes;
10285 unsigned char str[MAX_MULTIBYTE_LENGTH];
10286 /* Convert a single-byte string to multibyte
10287 for the *Message* buffer. */
10288 for (i = 0; i < nbytes; i++)
10290 c = msg[i];
10291 MAKE_CHAR_MULTIBYTE (c);
10292 char_bytes = CHAR_STRING (c, str);
10293 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10296 else if (nbytes)
10297 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10298 true, false, false);
10300 if (nlflag)
10302 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10303 printmax_t dups;
10305 insert_1_both ("\n", 1, 1, true, false, false);
10307 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10308 this_bol = PT;
10309 this_bol_byte = PT_BYTE;
10311 /* See if this line duplicates the previous one.
10312 If so, combine duplicates. */
10313 if (this_bol > BEG)
10315 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10316 prev_bol = PT;
10317 prev_bol_byte = PT_BYTE;
10319 dups = message_log_check_duplicate (prev_bol_byte,
10320 this_bol_byte);
10321 if (dups)
10323 del_range_both (prev_bol, prev_bol_byte,
10324 this_bol, this_bol_byte, false);
10325 if (dups > 1)
10327 char dupstr[sizeof " [ times]"
10328 + INT_STRLEN_BOUND (printmax_t)];
10330 /* If you change this format, don't forget to also
10331 change message_log_check_duplicate. */
10332 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10333 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10334 insert_1_both (dupstr, duplen, duplen,
10335 true, false, true);
10340 /* If we have more than the desired maximum number of lines
10341 in the *Messages* buffer now, delete the oldest ones.
10342 This is safe because we don't have undo in this buffer. */
10344 if (NATNUMP (Vmessage_log_max))
10346 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10347 -XFASTINT (Vmessage_log_max) - 1, false);
10348 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10351 BEGV = marker_position (oldbegv);
10352 BEGV_BYTE = marker_byte_position (oldbegv);
10354 if (zv_at_end)
10356 ZV = Z;
10357 ZV_BYTE = Z_BYTE;
10359 else
10361 ZV = marker_position (oldzv);
10362 ZV_BYTE = marker_byte_position (oldzv);
10365 if (point_at_end)
10366 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10367 else
10368 /* We can't do Fgoto_char (oldpoint) because it will run some
10369 Lisp code. */
10370 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10371 marker_byte_position (oldpoint));
10373 unchain_marker (XMARKER (oldpoint));
10374 unchain_marker (XMARKER (oldbegv));
10375 unchain_marker (XMARKER (oldzv));
10377 /* We called insert_1_both above with its 5th argument (PREPARE)
10378 false, which prevents insert_1_both from calling
10379 prepare_to_modify_buffer, which in turns prevents us from
10380 incrementing windows_or_buffers_changed even if *Messages* is
10381 shown in some window. So we must manually set
10382 windows_or_buffers_changed here to make up for that. */
10383 windows_or_buffers_changed = old_windows_or_buffers_changed;
10384 bset_redisplay (current_buffer);
10386 set_buffer_internal (oldbuf);
10388 message_log_need_newline = !nlflag;
10389 Vdeactivate_mark = old_deactivate_mark;
10394 /* We are at the end of the buffer after just having inserted a newline.
10395 (Note: We depend on the fact we won't be crossing the gap.)
10396 Check to see if the most recent message looks a lot like the previous one.
10397 Return 0 if different, 1 if the new one should just replace it, or a
10398 value N > 1 if we should also append " [N times]". */
10400 static intmax_t
10401 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10403 ptrdiff_t i;
10404 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10405 bool seen_dots = false;
10406 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10407 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10409 for (i = 0; i < len; i++)
10411 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10412 seen_dots = true;
10413 if (p1[i] != p2[i])
10414 return seen_dots;
10416 p1 += len;
10417 if (*p1 == '\n')
10418 return 2;
10419 if (*p1++ == ' ' && *p1++ == '[')
10421 char *pend;
10422 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10423 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10424 return n + 1;
10426 return 0;
10430 /* Display an echo area message M with a specified length of NBYTES
10431 bytes. The string may include null characters. If M is not a
10432 string, clear out any existing message, and let the mini-buffer
10433 text show through.
10435 This function cancels echoing. */
10437 void
10438 message3 (Lisp_Object m)
10440 clear_message (true, true);
10441 cancel_echoing ();
10443 /* First flush out any partial line written with print. */
10444 message_log_maybe_newline ();
10445 if (STRINGP (m))
10447 ptrdiff_t nbytes = SBYTES (m);
10448 bool multibyte = STRING_MULTIBYTE (m);
10449 char *buffer;
10450 USE_SAFE_ALLOCA;
10451 SAFE_ALLOCA_STRING (buffer, m);
10452 message_dolog (buffer, nbytes, true, multibyte);
10453 SAFE_FREE ();
10455 if (! inhibit_message)
10456 message3_nolog (m);
10459 /* Log the message M to stderr. Log an empty line if M is not a string. */
10461 static void
10462 message_to_stderr (Lisp_Object m)
10464 if (noninteractive_need_newline)
10466 noninteractive_need_newline = false;
10467 fputc ('\n', stderr);
10469 if (STRINGP (m))
10471 Lisp_Object coding_system = Vlocale_coding_system;
10472 Lisp_Object s;
10474 if (!NILP (Vcoding_system_for_write))
10475 coding_system = Vcoding_system_for_write;
10476 if (!NILP (coding_system))
10477 s = code_convert_string_norecord (m, coding_system, true);
10478 else
10479 s = m;
10481 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10483 if (!cursor_in_echo_area)
10484 fputc ('\n', stderr);
10485 fflush (stderr);
10488 /* The non-logging version of message3.
10489 This does not cancel echoing, because it is used for echoing.
10490 Perhaps we need to make a separate function for echoing
10491 and make this cancel echoing. */
10493 void
10494 message3_nolog (Lisp_Object m)
10496 struct frame *sf = SELECTED_FRAME ();
10498 if (FRAME_INITIAL_P (sf))
10499 message_to_stderr (m);
10500 /* Error messages get reported properly by cmd_error, so this must be just an
10501 informative message; if the frame hasn't really been initialized yet, just
10502 toss it. */
10503 else if (INTERACTIVE && sf->glyphs_initialized_p)
10505 /* Get the frame containing the mini-buffer
10506 that the selected frame is using. */
10507 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10508 Lisp_Object frame = XWINDOW (mini_window)->frame;
10509 struct frame *f = XFRAME (frame);
10511 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10512 Fmake_frame_visible (frame);
10514 if (STRINGP (m) && SCHARS (m) > 0)
10516 set_message (m);
10517 if (minibuffer_auto_raise)
10518 Fraise_frame (frame);
10519 /* Assume we are not echoing.
10520 (If we are, echo_now will override this.) */
10521 echo_message_buffer = Qnil;
10523 else
10524 clear_message (true, true);
10526 do_pending_window_change (false);
10527 echo_area_display (true);
10528 do_pending_window_change (false);
10529 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10530 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10535 /* Display a null-terminated echo area message M. If M is 0, clear
10536 out any existing message, and let the mini-buffer text show through.
10538 The buffer M must continue to exist until after the echo area gets
10539 cleared or some other message gets displayed there. Do not pass
10540 text that is stored in a Lisp string. Do not pass text in a buffer
10541 that was alloca'd. */
10543 void
10544 message1 (const char *m)
10546 message3 (m ? build_unibyte_string (m) : Qnil);
10550 /* The non-logging counterpart of message1. */
10552 void
10553 message1_nolog (const char *m)
10555 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10558 /* Display a message M which contains a single %s
10559 which gets replaced with STRING. */
10561 void
10562 message_with_string (const char *m, Lisp_Object string, bool log)
10564 CHECK_STRING (string);
10566 bool need_message;
10567 if (noninteractive)
10568 need_message = !!m;
10569 else if (!INTERACTIVE)
10570 need_message = false;
10571 else
10573 /* The frame whose minibuffer we're going to display the message on.
10574 It may be larger than the selected frame, so we need
10575 to use its buffer, not the selected frame's buffer. */
10576 Lisp_Object mini_window;
10577 struct frame *f, *sf = SELECTED_FRAME ();
10579 /* Get the frame containing the minibuffer
10580 that the selected frame is using. */
10581 mini_window = FRAME_MINIBUF_WINDOW (sf);
10582 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10584 /* Error messages get reported properly by cmd_error, so this must be
10585 just an informative message; if the frame hasn't really been
10586 initialized yet, just toss it. */
10587 need_message = f->glyphs_initialized_p;
10590 if (need_message)
10592 AUTO_STRING (fmt, m);
10593 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10595 if (noninteractive)
10596 message_to_stderr (msg);
10597 else
10599 if (log)
10600 message3 (msg);
10601 else
10602 message3_nolog (msg);
10604 /* Print should start at the beginning of the message
10605 buffer next time. */
10606 message_buf_print = false;
10612 /* Dump an informative message to the minibuf. If M is 0, clear out
10613 any existing message, and let the mini-buffer text show through.
10615 The message must be safe ASCII (because when Emacs is
10616 non-interactive the message is sent straight to stderr without
10617 encoding first) and the format must not contain ` or ' (because
10618 this function does not account for `text-quoting-style'). If your
10619 message and format do not fit into this category, convert your
10620 arguments to Lisp objects and use Fmessage instead. */
10622 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10623 vmessage (const char *m, va_list ap)
10625 if (noninteractive)
10627 if (m)
10629 if (noninteractive_need_newline)
10630 putc ('\n', stderr);
10631 noninteractive_need_newline = false;
10632 vfprintf (stderr, m, ap);
10633 if (!cursor_in_echo_area)
10634 fprintf (stderr, "\n");
10635 fflush (stderr);
10638 else if (INTERACTIVE)
10640 /* The frame whose mini-buffer we're going to display the message
10641 on. It may be larger than the selected frame, so we need to
10642 use its buffer, not the selected frame's buffer. */
10643 Lisp_Object mini_window;
10644 struct frame *f, *sf = SELECTED_FRAME ();
10646 /* Get the frame containing the mini-buffer
10647 that the selected frame is using. */
10648 mini_window = FRAME_MINIBUF_WINDOW (sf);
10649 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10651 /* Error messages get reported properly by cmd_error, so this must be
10652 just an informative message; if the frame hasn't really been
10653 initialized yet, just toss it. */
10654 if (f->glyphs_initialized_p)
10656 if (m)
10658 ptrdiff_t len;
10659 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10660 USE_SAFE_ALLOCA;
10661 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10663 len = doprnt (message_buf, maxsize, m, 0, ap);
10665 message3 (make_string (message_buf, len));
10666 SAFE_FREE ();
10668 else
10669 message1 (0);
10671 /* Print should start at the beginning of the message
10672 buffer next time. */
10673 message_buf_print = false;
10678 /* See vmessage for restrictions on the text of the message. */
10679 void
10680 message (const char *m, ...)
10682 va_list ap;
10683 va_start (ap, m);
10684 vmessage (m, ap);
10685 va_end (ap);
10689 /* Display the current message in the current mini-buffer. This is
10690 only called from error handlers in process.c, and is not time
10691 critical. */
10693 void
10694 update_echo_area (void)
10696 if (!NILP (echo_area_buffer[0]))
10698 Lisp_Object string;
10699 string = Fcurrent_message ();
10700 message3 (string);
10705 /* Make sure echo area buffers in `echo_buffers' are live.
10706 If they aren't, make new ones. */
10708 static void
10709 ensure_echo_area_buffers (void)
10711 for (int i = 0; i < 2; i++)
10712 if (!BUFFERP (echo_buffer[i])
10713 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10715 Lisp_Object old_buffer = echo_buffer[i];
10716 static char const name_fmt[] = " *Echo Area %d*";
10717 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10718 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10719 echo_buffer[i] = Fget_buffer_create (lname);
10720 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10721 /* to force word wrap in echo area -
10722 it was decided to postpone this*/
10723 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10725 for (int j = 0; j < 2; j++)
10726 if (EQ (old_buffer, echo_area_buffer[j]))
10727 echo_area_buffer[j] = echo_buffer[i];
10732 /* Call FN with args A1..A2 with either the current or last displayed
10733 echo_area_buffer as current buffer.
10735 WHICH zero means use the current message buffer
10736 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10737 from echo_buffer[] and clear it.
10739 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10740 suitable buffer from echo_buffer[] and clear it.
10742 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10743 that the current message becomes the last displayed one, choose a
10744 suitable buffer for echo_area_buffer[0], and clear it.
10746 Value is what FN returns. */
10748 static bool
10749 with_echo_area_buffer (struct window *w, int which,
10750 bool (*fn) (ptrdiff_t, Lisp_Object),
10751 ptrdiff_t a1, Lisp_Object a2)
10753 Lisp_Object buffer;
10754 bool this_one, the_other, clear_buffer_p, rc;
10755 ptrdiff_t count = SPECPDL_INDEX ();
10757 /* If buffers aren't live, make new ones. */
10758 ensure_echo_area_buffers ();
10760 clear_buffer_p = false;
10762 if (which == 0)
10763 this_one = false, the_other = true;
10764 else if (which > 0)
10765 this_one = true, the_other = false;
10766 else
10768 this_one = false, the_other = true;
10769 clear_buffer_p = true;
10771 /* We need a fresh one in case the current echo buffer equals
10772 the one containing the last displayed echo area message. */
10773 if (!NILP (echo_area_buffer[this_one])
10774 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10775 echo_area_buffer[this_one] = Qnil;
10778 /* Choose a suitable buffer from echo_buffer[] if we don't
10779 have one. */
10780 if (NILP (echo_area_buffer[this_one]))
10782 echo_area_buffer[this_one]
10783 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10784 ? echo_buffer[the_other]
10785 : echo_buffer[this_one]);
10786 clear_buffer_p = true;
10789 buffer = echo_area_buffer[this_one];
10791 /* Don't get confused by reusing the buffer used for echoing
10792 for a different purpose. */
10793 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10794 cancel_echoing ();
10796 record_unwind_protect (unwind_with_echo_area_buffer,
10797 with_echo_area_buffer_unwind_data (w));
10799 /* Make the echo area buffer current. Note that for display
10800 purposes, it is not necessary that the displayed window's buffer
10801 == current_buffer, except for text property lookup. So, let's
10802 only set that buffer temporarily here without doing a full
10803 Fset_window_buffer. We must also change w->pointm, though,
10804 because otherwise an assertions in unshow_buffer fails, and Emacs
10805 aborts. */
10806 set_buffer_internal_1 (XBUFFER (buffer));
10807 if (w)
10809 wset_buffer (w, buffer);
10810 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10811 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10814 bset_undo_list (current_buffer, Qt);
10815 bset_read_only (current_buffer, Qnil);
10816 specbind (Qinhibit_read_only, Qt);
10817 specbind (Qinhibit_modification_hooks, Qt);
10819 if (clear_buffer_p && Z > BEG)
10820 del_range (BEG, Z);
10822 eassert (BEGV >= BEG);
10823 eassert (ZV <= Z && ZV >= BEGV);
10825 rc = fn (a1, a2);
10827 eassert (BEGV >= BEG);
10828 eassert (ZV <= Z && ZV >= BEGV);
10830 unbind_to (count, Qnil);
10831 return rc;
10835 /* Save state that should be preserved around the call to the function
10836 FN called in with_echo_area_buffer. */
10838 static Lisp_Object
10839 with_echo_area_buffer_unwind_data (struct window *w)
10841 int i = 0;
10842 Lisp_Object vector, tmp;
10844 /* Reduce consing by keeping one vector in
10845 Vwith_echo_area_save_vector. */
10846 vector = Vwith_echo_area_save_vector;
10847 Vwith_echo_area_save_vector = Qnil;
10849 if (NILP (vector))
10850 vector = Fmake_vector (make_number (11), Qnil);
10852 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10853 ASET (vector, i, Vdeactivate_mark); ++i;
10854 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10856 if (w)
10858 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10859 ASET (vector, i, w->contents); ++i;
10860 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10861 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10862 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10863 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10864 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10865 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10867 else
10869 int end = i + 8;
10870 for (; i < end; ++i)
10871 ASET (vector, i, Qnil);
10874 eassert (i == ASIZE (vector));
10875 return vector;
10879 /* Restore global state from VECTOR which was created by
10880 with_echo_area_buffer_unwind_data. */
10882 static void
10883 unwind_with_echo_area_buffer (Lisp_Object vector)
10885 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10886 Vdeactivate_mark = AREF (vector, 1);
10887 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10889 if (WINDOWP (AREF (vector, 3)))
10891 struct window *w;
10892 Lisp_Object buffer;
10894 w = XWINDOW (AREF (vector, 3));
10895 buffer = AREF (vector, 4);
10897 wset_buffer (w, buffer);
10898 set_marker_both (w->pointm, buffer,
10899 XFASTINT (AREF (vector, 5)),
10900 XFASTINT (AREF (vector, 6)));
10901 set_marker_both (w->old_pointm, buffer,
10902 XFASTINT (AREF (vector, 7)),
10903 XFASTINT (AREF (vector, 8)));
10904 set_marker_both (w->start, buffer,
10905 XFASTINT (AREF (vector, 9)),
10906 XFASTINT (AREF (vector, 10)));
10909 Vwith_echo_area_save_vector = vector;
10913 /* Set up the echo area for use by print functions. MULTIBYTE_P
10914 means we will print multibyte. */
10916 void
10917 setup_echo_area_for_printing (bool multibyte_p)
10919 /* If we can't find an echo area any more, exit. */
10920 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10921 Fkill_emacs (Qnil);
10923 ensure_echo_area_buffers ();
10925 if (!message_buf_print)
10927 /* A message has been output since the last time we printed.
10928 Choose a fresh echo area buffer. */
10929 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10930 echo_area_buffer[0] = echo_buffer[1];
10931 else
10932 echo_area_buffer[0] = echo_buffer[0];
10934 /* Switch to that buffer and clear it. */
10935 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10936 bset_truncate_lines (current_buffer, Qnil);
10938 if (Z > BEG)
10940 ptrdiff_t count = SPECPDL_INDEX ();
10941 specbind (Qinhibit_read_only, Qt);
10942 /* Note that undo recording is always disabled. */
10943 del_range (BEG, Z);
10944 unbind_to (count, Qnil);
10946 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10948 /* Set up the buffer for the multibyteness we need. */
10949 if (multibyte_p
10950 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10951 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10953 /* Raise the frame containing the echo area. */
10954 if (minibuffer_auto_raise)
10956 struct frame *sf = SELECTED_FRAME ();
10957 Lisp_Object mini_window;
10958 mini_window = FRAME_MINIBUF_WINDOW (sf);
10959 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10962 message_log_maybe_newline ();
10963 message_buf_print = true;
10965 else
10967 if (NILP (echo_area_buffer[0]))
10969 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10970 echo_area_buffer[0] = echo_buffer[1];
10971 else
10972 echo_area_buffer[0] = echo_buffer[0];
10975 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10977 /* Someone switched buffers between print requests. */
10978 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10979 bset_truncate_lines (current_buffer, Qnil);
10985 /* Display an echo area message in window W. Value is true if W's
10986 height is changed. If display_last_displayed_message_p,
10987 display the message that was last displayed, otherwise
10988 display the current message. */
10990 static bool
10991 display_echo_area (struct window *w)
10993 bool no_message_p, window_height_changed_p;
10995 /* Temporarily disable garbage collections while displaying the echo
10996 area. This is done because a GC can print a message itself.
10997 That message would modify the echo area buffer's contents while a
10998 redisplay of the buffer is going on, and seriously confuse
10999 redisplay. */
11000 ptrdiff_t count = inhibit_garbage_collection ();
11002 /* If there is no message, we must call display_echo_area_1
11003 nevertheless because it resizes the window. But we will have to
11004 reset the echo_area_buffer in question to nil at the end because
11005 with_echo_area_buffer will sets it to an empty buffer. */
11006 bool i = display_last_displayed_message_p;
11007 /* According to the C99, C11 and C++11 standards, the integral value
11008 of a "bool" is always 0 or 1, so this array access is safe here,
11009 if oddly typed. */
11010 no_message_p = NILP (echo_area_buffer[i]);
11012 window_height_changed_p
11013 = with_echo_area_buffer (w, display_last_displayed_message_p,
11014 display_echo_area_1,
11015 (intptr_t) w, Qnil);
11017 if (no_message_p)
11018 echo_area_buffer[i] = Qnil;
11020 unbind_to (count, Qnil);
11021 return window_height_changed_p;
11025 /* Helper for display_echo_area. Display the current buffer which
11026 contains the current echo area message in window W, a mini-window,
11027 a pointer to which is passed in A1. A2..A4 are currently not used.
11028 Change the height of W so that all of the message is displayed.
11029 Value is true if height of W was changed. */
11031 static bool
11032 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11034 intptr_t i1 = a1;
11035 struct window *w = (struct window *) i1;
11036 Lisp_Object window;
11037 struct text_pos start;
11039 /* We are about to enter redisplay without going through
11040 redisplay_internal, so we need to forget these faces by hand
11041 here. */
11042 forget_escape_and_glyphless_faces ();
11044 /* Do this before displaying, so that we have a large enough glyph
11045 matrix for the display. If we can't get enough space for the
11046 whole text, display the last N lines. That works by setting w->start. */
11047 bool window_height_changed_p = resize_mini_window (w, false);
11049 /* Use the starting position chosen by resize_mini_window. */
11050 SET_TEXT_POS_FROM_MARKER (start, w->start);
11052 /* Display. */
11053 clear_glyph_matrix (w->desired_matrix);
11054 XSETWINDOW (window, w);
11055 try_window (window, start, 0);
11057 return window_height_changed_p;
11061 /* Resize the echo area window to exactly the size needed for the
11062 currently displayed message, if there is one. If a mini-buffer
11063 is active, don't shrink it. */
11065 void
11066 resize_echo_area_exactly (void)
11068 if (BUFFERP (echo_area_buffer[0])
11069 && WINDOWP (echo_area_window))
11071 struct window *w = XWINDOW (echo_area_window);
11072 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11073 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11074 (intptr_t) w, resize_exactly);
11075 if (resized_p)
11077 windows_or_buffers_changed = 42;
11078 update_mode_lines = 30;
11079 redisplay_internal ();
11085 /* Callback function for with_echo_area_buffer, when used from
11086 resize_echo_area_exactly. A1 contains a pointer to the window to
11087 resize, EXACTLY non-nil means resize the mini-window exactly to the
11088 size of the text displayed. A3 and A4 are not used. Value is what
11089 resize_mini_window returns. */
11091 static bool
11092 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11094 intptr_t i1 = a1;
11095 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11099 /* Resize mini-window W to fit the size of its contents. EXACT_P
11100 means size the window exactly to the size needed. Otherwise, it's
11101 only enlarged until W's buffer is empty.
11103 Set W->start to the right place to begin display. If the whole
11104 contents fit, start at the beginning. Otherwise, start so as
11105 to make the end of the contents appear. This is particularly
11106 important for y-or-n-p, but seems desirable generally.
11108 Value is true if the window height has been changed. */
11110 bool
11111 resize_mini_window (struct window *w, bool exact_p)
11113 struct frame *f = XFRAME (w->frame);
11114 bool window_height_changed_p = false;
11116 eassert (MINI_WINDOW_P (w));
11118 /* By default, start display at the beginning. */
11119 set_marker_both (w->start, w->contents,
11120 BUF_BEGV (XBUFFER (w->contents)),
11121 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11123 /* Don't resize windows while redisplaying a window; it would
11124 confuse redisplay functions when the size of the window they are
11125 displaying changes from under them. Such a resizing can happen,
11126 for instance, when which-func prints a long message while
11127 we are running fontification-functions. We're running these
11128 functions with safe_call which binds inhibit-redisplay to t. */
11129 if (!NILP (Vinhibit_redisplay))
11130 return false;
11132 /* Nil means don't try to resize. */
11133 if (NILP (Vresize_mini_windows)
11134 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11135 return false;
11137 if (!FRAME_MINIBUF_ONLY_P (f))
11139 struct it it;
11140 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11141 + WINDOW_PIXEL_HEIGHT (w));
11142 int unit = FRAME_LINE_HEIGHT (f);
11143 int height, max_height;
11144 struct text_pos start;
11145 struct buffer *old_current_buffer = NULL;
11147 if (current_buffer != XBUFFER (w->contents))
11149 old_current_buffer = current_buffer;
11150 set_buffer_internal (XBUFFER (w->contents));
11153 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11155 /* Compute the max. number of lines specified by the user. */
11156 if (FLOATP (Vmax_mini_window_height))
11157 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11158 else if (INTEGERP (Vmax_mini_window_height))
11159 max_height = XINT (Vmax_mini_window_height) * unit;
11160 else
11161 max_height = total_height / 4;
11163 /* Correct that max. height if it's bogus. */
11164 max_height = clip_to_bounds (unit, max_height, total_height);
11166 /* Find out the height of the text in the window. */
11167 if (it.line_wrap == TRUNCATE)
11168 height = unit;
11169 else
11171 last_height = 0;
11172 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11173 if (it.max_ascent == 0 && it.max_descent == 0)
11174 height = it.current_y + last_height;
11175 else
11176 height = it.current_y + it.max_ascent + it.max_descent;
11177 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11180 /* Compute a suitable window start. */
11181 if (height > max_height)
11183 height = (max_height / unit) * unit;
11184 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11185 move_it_vertically_backward (&it, height - unit);
11186 start = it.current.pos;
11188 else
11189 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11190 SET_MARKER_FROM_TEXT_POS (w->start, start);
11192 if (EQ (Vresize_mini_windows, Qgrow_only))
11194 /* Let it grow only, until we display an empty message, in which
11195 case the window shrinks again. */
11196 if (height > WINDOW_PIXEL_HEIGHT (w))
11198 int old_height = WINDOW_PIXEL_HEIGHT (w);
11200 FRAME_WINDOWS_FROZEN (f) = true;
11201 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11202 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11204 else if (height < WINDOW_PIXEL_HEIGHT (w)
11205 && (exact_p || BEGV == ZV))
11207 int old_height = WINDOW_PIXEL_HEIGHT (w);
11209 FRAME_WINDOWS_FROZEN (f) = false;
11210 shrink_mini_window (w, true);
11211 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11214 else
11216 /* Always resize to exact size needed. */
11217 if (height > WINDOW_PIXEL_HEIGHT (w))
11219 int old_height = WINDOW_PIXEL_HEIGHT (w);
11221 FRAME_WINDOWS_FROZEN (f) = true;
11222 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11223 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11225 else if (height < WINDOW_PIXEL_HEIGHT (w))
11227 int old_height = WINDOW_PIXEL_HEIGHT (w);
11229 FRAME_WINDOWS_FROZEN (f) = false;
11230 shrink_mini_window (w, true);
11232 if (height)
11234 FRAME_WINDOWS_FROZEN (f) = true;
11235 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11238 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11242 if (old_current_buffer)
11243 set_buffer_internal (old_current_buffer);
11246 return window_height_changed_p;
11250 /* Value is the current message, a string, or nil if there is no
11251 current message. */
11253 Lisp_Object
11254 current_message (void)
11256 Lisp_Object msg;
11258 if (!BUFFERP (echo_area_buffer[0]))
11259 msg = Qnil;
11260 else
11262 with_echo_area_buffer (0, 0, current_message_1,
11263 (intptr_t) &msg, Qnil);
11264 if (NILP (msg))
11265 echo_area_buffer[0] = Qnil;
11268 return msg;
11272 static bool
11273 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11275 intptr_t i1 = a1;
11276 Lisp_Object *msg = (Lisp_Object *) i1;
11278 if (Z > BEG)
11279 *msg = make_buffer_string (BEG, Z, true);
11280 else
11281 *msg = Qnil;
11282 return false;
11286 /* Push the current message on Vmessage_stack for later restoration
11287 by restore_message. Value is true if the current message isn't
11288 empty. This is a relatively infrequent operation, so it's not
11289 worth optimizing. */
11291 bool
11292 push_message (void)
11294 Lisp_Object msg = current_message ();
11295 Vmessage_stack = Fcons (msg, Vmessage_stack);
11296 return STRINGP (msg);
11300 /* Restore message display from the top of Vmessage_stack. */
11302 void
11303 restore_message (void)
11305 eassert (CONSP (Vmessage_stack));
11306 message3_nolog (XCAR (Vmessage_stack));
11310 /* Handler for unwind-protect calling pop_message. */
11312 void
11313 pop_message_unwind (void)
11315 /* Pop the top-most entry off Vmessage_stack. */
11316 eassert (CONSP (Vmessage_stack));
11317 Vmessage_stack = XCDR (Vmessage_stack);
11321 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11322 exits. If the stack is not empty, we have a missing pop_message
11323 somewhere. */
11325 void
11326 check_message_stack (void)
11328 if (!NILP (Vmessage_stack))
11329 emacs_abort ();
11333 /* Truncate to NCHARS what will be displayed in the echo area the next
11334 time we display it---but don't redisplay it now. */
11336 void
11337 truncate_echo_area (ptrdiff_t nchars)
11339 if (nchars == 0)
11340 echo_area_buffer[0] = Qnil;
11341 else if (!noninteractive
11342 && INTERACTIVE
11343 && !NILP (echo_area_buffer[0]))
11345 struct frame *sf = SELECTED_FRAME ();
11346 /* Error messages get reported properly by cmd_error, so this must be
11347 just an informative message; if the frame hasn't really been
11348 initialized yet, just toss it. */
11349 if (sf->glyphs_initialized_p)
11350 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11355 /* Helper function for truncate_echo_area. Truncate the current
11356 message to at most NCHARS characters. */
11358 static bool
11359 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11361 if (BEG + nchars < Z)
11362 del_range (BEG + nchars, Z);
11363 if (Z == BEG)
11364 echo_area_buffer[0] = Qnil;
11365 return false;
11368 /* Set the current message to STRING. */
11370 static void
11371 set_message (Lisp_Object string)
11373 eassert (STRINGP (string));
11375 message_enable_multibyte = STRING_MULTIBYTE (string);
11377 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11378 message_buf_print = false;
11379 help_echo_showing_p = false;
11381 if (STRINGP (Vdebug_on_message)
11382 && STRINGP (string)
11383 && fast_string_match (Vdebug_on_message, string) >= 0)
11384 call_debugger (list2 (Qerror, string));
11388 /* Helper function for set_message. First argument is ignored and second
11389 argument has the same meaning as for set_message.
11390 This function is called with the echo area buffer being current. */
11392 static bool
11393 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11395 eassert (STRINGP (string));
11397 /* Change multibyteness of the echo buffer appropriately. */
11398 if (message_enable_multibyte
11399 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11400 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11402 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11403 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11404 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11406 /* Insert new message at BEG. */
11407 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11409 /* This function takes care of single/multibyte conversion.
11410 We just have to ensure that the echo area buffer has the right
11411 setting of enable_multibyte_characters. */
11412 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11414 return false;
11418 /* Clear messages. CURRENT_P means clear the current message.
11419 LAST_DISPLAYED_P means clear the message last displayed. */
11421 void
11422 clear_message (bool current_p, bool last_displayed_p)
11424 if (current_p)
11426 echo_area_buffer[0] = Qnil;
11427 message_cleared_p = true;
11430 if (last_displayed_p)
11431 echo_area_buffer[1] = Qnil;
11433 message_buf_print = false;
11436 /* Clear garbaged frames.
11438 This function is used where the old redisplay called
11439 redraw_garbaged_frames which in turn called redraw_frame which in
11440 turn called clear_frame. The call to clear_frame was a source of
11441 flickering. I believe a clear_frame is not necessary. It should
11442 suffice in the new redisplay to invalidate all current matrices,
11443 and ensure a complete redisplay of all windows. */
11445 static void
11446 clear_garbaged_frames (void)
11448 if (frame_garbaged)
11450 Lisp_Object tail, frame;
11451 struct frame *sf = SELECTED_FRAME ();
11453 FOR_EACH_FRAME (tail, frame)
11455 struct frame *f = XFRAME (frame);
11457 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11459 if (f->resized_p
11460 /* It makes no sense to redraw a non-selected TTY
11461 frame, since that will actually clear the
11462 selected frame, and might leave the selected
11463 frame with corrupted display, if it happens not
11464 to be marked garbaged. */
11465 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11466 redraw_frame (f);
11467 else
11468 clear_current_matrices (f);
11470 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11471 x_clear_under_internal_border (f);
11472 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11474 fset_redisplay (f);
11475 f->garbaged = false;
11476 f->resized_p = false;
11480 frame_garbaged = false;
11485 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11486 selected_frame. */
11488 static void
11489 echo_area_display (bool update_frame_p)
11491 Lisp_Object mini_window;
11492 struct window *w;
11493 struct frame *f;
11494 bool window_height_changed_p = false;
11495 struct frame *sf = SELECTED_FRAME ();
11497 mini_window = FRAME_MINIBUF_WINDOW (sf);
11498 w = XWINDOW (mini_window);
11499 f = XFRAME (WINDOW_FRAME (w));
11501 /* Don't display if frame is invisible or not yet initialized. */
11502 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11503 return;
11505 #ifdef HAVE_WINDOW_SYSTEM
11506 /* When Emacs starts, selected_frame may be the initial terminal
11507 frame. If we let this through, a message would be displayed on
11508 the terminal. */
11509 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11510 return;
11511 #endif /* HAVE_WINDOW_SYSTEM */
11513 /* Redraw garbaged frames. */
11514 clear_garbaged_frames ();
11516 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11518 echo_area_window = mini_window;
11519 window_height_changed_p = display_echo_area (w);
11520 w->must_be_updated_p = true;
11522 /* Update the display, unless called from redisplay_internal.
11523 Also don't update the screen during redisplay itself. The
11524 update will happen at the end of redisplay, and an update
11525 here could cause confusion. */
11526 if (update_frame_p && !redisplaying_p)
11528 int n = 0;
11530 /* If the display update has been interrupted by pending
11531 input, update mode lines in the frame. Due to the
11532 pending input, it might have been that redisplay hasn't
11533 been called, so that mode lines above the echo area are
11534 garbaged. This looks odd, so we prevent it here. */
11535 if (!display_completed)
11537 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11539 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11540 x_clear_under_internal_border (f);
11541 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11545 if (window_height_changed_p
11546 /* Don't do this if Emacs is shutting down. Redisplay
11547 needs to run hooks. */
11548 && !NILP (Vrun_hooks))
11550 /* Must update other windows. Likewise as in other
11551 cases, don't let this update be interrupted by
11552 pending input. */
11553 ptrdiff_t count = SPECPDL_INDEX ();
11554 specbind (Qredisplay_dont_pause, Qt);
11555 fset_redisplay (f);
11556 redisplay_internal ();
11557 unbind_to (count, Qnil);
11559 else if (FRAME_WINDOW_P (f) && n == 0)
11561 /* Window configuration is the same as before.
11562 Can do with a display update of the echo area,
11563 unless we displayed some mode lines. */
11564 update_single_window (w);
11565 flush_frame (f);
11567 else
11568 update_frame (f, true, true);
11570 /* If cursor is in the echo area, make sure that the next
11571 redisplay displays the minibuffer, so that the cursor will
11572 be replaced with what the minibuffer wants. */
11573 if (cursor_in_echo_area)
11574 wset_redisplay (XWINDOW (mini_window));
11577 else if (!EQ (mini_window, selected_window))
11578 wset_redisplay (XWINDOW (mini_window));
11580 /* Last displayed message is now the current message. */
11581 echo_area_buffer[1] = echo_area_buffer[0];
11582 /* Inform read_char that we're not echoing. */
11583 echo_message_buffer = Qnil;
11585 /* Prevent redisplay optimization in redisplay_internal by resetting
11586 this_line_start_pos. This is done because the mini-buffer now
11587 displays the message instead of its buffer text. */
11588 if (EQ (mini_window, selected_window))
11589 CHARPOS (this_line_start_pos) = 0;
11591 if (window_height_changed_p)
11593 fset_redisplay (f);
11595 /* If window configuration was changed, frames may have been
11596 marked garbaged. Clear them or we will experience
11597 surprises wrt scrolling.
11598 FIXME: How/why/when? */
11599 clear_garbaged_frames ();
11603 /* True if W's buffer was changed but not saved. */
11605 static bool
11606 window_buffer_changed (struct window *w)
11608 struct buffer *b = XBUFFER (w->contents);
11610 eassert (BUFFER_LIVE_P (b));
11612 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11615 /* True if W has %c or %C in its mode line and mode line should be updated. */
11617 static bool
11618 mode_line_update_needed (struct window *w)
11620 return (w->column_number_displayed != -1
11621 && !(PT == w->last_point && !window_outdated (w))
11622 && (w->column_number_displayed != current_column ()));
11625 /* True if window start of W is frozen and may not be changed during
11626 redisplay. */
11628 static bool
11629 window_frozen_p (struct window *w)
11631 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11633 Lisp_Object window;
11635 XSETWINDOW (window, w);
11636 if (MINI_WINDOW_P (w))
11637 return false;
11638 else if (EQ (window, selected_window))
11639 return false;
11640 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11641 && EQ (window, Vminibuf_scroll_window))
11642 /* This special window can't be frozen too. */
11643 return false;
11644 else
11645 return true;
11647 return false;
11650 /***********************************************************************
11651 Mode Lines and Frame Titles
11652 ***********************************************************************/
11654 /* A buffer for constructing non-propertized mode-line strings and
11655 frame titles in it; allocated from the heap in init_xdisp and
11656 resized as needed in store_mode_line_noprop_char. */
11658 static char *mode_line_noprop_buf;
11660 /* The buffer's end, and a current output position in it. */
11662 static char *mode_line_noprop_buf_end;
11663 static char *mode_line_noprop_ptr;
11665 #define MODE_LINE_NOPROP_LEN(start) \
11666 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11668 static enum {
11669 MODE_LINE_DISPLAY = 0,
11670 MODE_LINE_TITLE,
11671 MODE_LINE_NOPROP,
11672 MODE_LINE_STRING
11673 } mode_line_target;
11675 /* Alist that caches the results of :propertize.
11676 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11677 static Lisp_Object mode_line_proptrans_alist;
11679 /* List of strings making up the mode-line. */
11680 static Lisp_Object mode_line_string_list;
11682 /* Base face property when building propertized mode line string. */
11683 static Lisp_Object mode_line_string_face;
11684 static Lisp_Object mode_line_string_face_prop;
11687 /* Unwind data for mode line strings */
11689 static Lisp_Object Vmode_line_unwind_vector;
11691 static Lisp_Object
11692 format_mode_line_unwind_data (struct frame *target_frame,
11693 struct buffer *obuf,
11694 Lisp_Object owin,
11695 bool save_proptrans)
11697 Lisp_Object vector, tmp;
11699 /* Reduce consing by keeping one vector in
11700 Vwith_echo_area_save_vector. */
11701 vector = Vmode_line_unwind_vector;
11702 Vmode_line_unwind_vector = Qnil;
11704 if (NILP (vector))
11705 vector = Fmake_vector (make_number (10), Qnil);
11707 ASET (vector, 0, make_number (mode_line_target));
11708 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11709 ASET (vector, 2, mode_line_string_list);
11710 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11711 ASET (vector, 4, mode_line_string_face);
11712 ASET (vector, 5, mode_line_string_face_prop);
11714 if (obuf)
11715 XSETBUFFER (tmp, obuf);
11716 else
11717 tmp = Qnil;
11718 ASET (vector, 6, tmp);
11719 ASET (vector, 7, owin);
11720 if (target_frame)
11722 /* Similarly to `with-selected-window', if the operation selects
11723 a window on another frame, we must restore that frame's
11724 selected window, and (for a tty) the top-frame. */
11725 ASET (vector, 8, target_frame->selected_window);
11726 if (FRAME_TERMCAP_P (target_frame))
11727 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11730 return vector;
11733 static void
11734 unwind_format_mode_line (Lisp_Object vector)
11736 Lisp_Object old_window = AREF (vector, 7);
11737 Lisp_Object target_frame_window = AREF (vector, 8);
11738 Lisp_Object old_top_frame = AREF (vector, 9);
11740 mode_line_target = XINT (AREF (vector, 0));
11741 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11742 mode_line_string_list = AREF (vector, 2);
11743 if (! EQ (AREF (vector, 3), Qt))
11744 mode_line_proptrans_alist = AREF (vector, 3);
11745 mode_line_string_face = AREF (vector, 4);
11746 mode_line_string_face_prop = AREF (vector, 5);
11748 /* Select window before buffer, since it may change the buffer. */
11749 if (!NILP (old_window))
11751 /* If the operation that we are unwinding had selected a window
11752 on a different frame, reset its frame-selected-window. For a
11753 text terminal, reset its top-frame if necessary. */
11754 if (!NILP (target_frame_window))
11756 Lisp_Object frame
11757 = WINDOW_FRAME (XWINDOW (target_frame_window));
11759 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11760 Fselect_window (target_frame_window, Qt);
11762 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11763 Fselect_frame (old_top_frame, Qt);
11766 Fselect_window (old_window, Qt);
11769 if (!NILP (AREF (vector, 6)))
11771 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11772 ASET (vector, 6, Qnil);
11775 Vmode_line_unwind_vector = vector;
11779 /* Store a single character C for the frame title in mode_line_noprop_buf.
11780 Re-allocate mode_line_noprop_buf if necessary. */
11782 static void
11783 store_mode_line_noprop_char (char c)
11785 /* If output position has reached the end of the allocated buffer,
11786 increase the buffer's size. */
11787 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11789 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11790 ptrdiff_t size = len;
11791 mode_line_noprop_buf =
11792 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11793 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11794 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11797 *mode_line_noprop_ptr++ = c;
11801 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11802 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11803 characters that yield more columns than PRECISION; PRECISION <= 0
11804 means copy the whole string. Pad with spaces until FIELD_WIDTH
11805 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11806 pad. Called from display_mode_element when it is used to build a
11807 frame title. */
11809 static int
11810 store_mode_line_noprop (const char *string, int field_width, int precision)
11812 const unsigned char *str = (const unsigned char *) string;
11813 int n = 0;
11814 ptrdiff_t dummy, nbytes;
11816 /* Copy at most PRECISION chars from STR. */
11817 nbytes = strlen (string);
11818 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11819 while (nbytes--)
11820 store_mode_line_noprop_char (*str++);
11822 /* Fill up with spaces until FIELD_WIDTH reached. */
11823 while (field_width > 0
11824 && n < field_width)
11826 store_mode_line_noprop_char (' ');
11827 ++n;
11830 return n;
11833 /***********************************************************************
11834 Frame Titles
11835 ***********************************************************************/
11837 #ifdef HAVE_WINDOW_SYSTEM
11839 /* Set the title of FRAME, if it has changed. The title format is
11840 Vicon_title_format if FRAME is iconified, otherwise it is
11841 frame_title_format. */
11843 static void
11844 x_consider_frame_title (Lisp_Object frame)
11846 struct frame *f = XFRAME (frame);
11848 if ((FRAME_WINDOW_P (f)
11849 || FRAME_MINIBUF_ONLY_P (f)
11850 || f->explicit_name)
11851 && NILP (Fframe_parameter (frame, Qtooltip)))
11853 /* Do we have more than one visible frame on this X display? */
11854 Lisp_Object tail, other_frame, fmt;
11855 ptrdiff_t title_start;
11856 char *title;
11857 ptrdiff_t len;
11858 struct it it;
11859 ptrdiff_t count = SPECPDL_INDEX ();
11861 FOR_EACH_FRAME (tail, other_frame)
11863 struct frame *tf = XFRAME (other_frame);
11865 if (tf != f
11866 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11867 && !FRAME_MINIBUF_ONLY_P (tf)
11868 && !EQ (other_frame, tip_frame)
11869 && !FRAME_PARENT_FRAME (tf)
11870 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11871 break;
11874 /* Set global variable indicating that multiple frames exist. */
11875 multiple_frames = CONSP (tail);
11877 /* Switch to the buffer of selected window of the frame. Set up
11878 mode_line_target so that display_mode_element will output into
11879 mode_line_noprop_buf; then display the title. */
11880 record_unwind_protect (unwind_format_mode_line,
11881 format_mode_line_unwind_data
11882 (f, current_buffer, selected_window, false));
11883 /* select-frame calls resize_mini_window, which could resize the
11884 mini-window and by that undo the effect of this redisplay
11885 cycle wrt minibuffer and echo-area display. Binding
11886 inhibit-redisplay to t makes the call to resize_mini_window a
11887 no-op, thus avoiding the adverse side effects. */
11888 specbind (Qinhibit_redisplay, Qt);
11890 Fselect_window (f->selected_window, Qt);
11891 set_buffer_internal_1
11892 (XBUFFER (XWINDOW (f->selected_window)->contents));
11893 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11895 mode_line_target = MODE_LINE_TITLE;
11896 title_start = MODE_LINE_NOPROP_LEN (0);
11897 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11898 NULL, DEFAULT_FACE_ID);
11899 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11900 len = MODE_LINE_NOPROP_LEN (title_start);
11901 title = mode_line_noprop_buf + title_start;
11902 unbind_to (count, Qnil);
11904 /* Set the title only if it's changed. This avoids consing in
11905 the common case where it hasn't. (If it turns out that we've
11906 already wasted too much time by walking through the list with
11907 display_mode_element, then we might need to optimize at a
11908 higher level than this.) */
11909 if (! STRINGP (f->name)
11910 || SBYTES (f->name) != len
11911 || memcmp (title, SDATA (f->name), len) != 0)
11912 x_implicitly_set_name (f, make_string (title, len), Qnil);
11916 #endif /* not HAVE_WINDOW_SYSTEM */
11919 /***********************************************************************
11920 Menu Bars
11921 ***********************************************************************/
11923 /* True if we will not redisplay all visible windows. */
11924 #define REDISPLAY_SOME_P() \
11925 ((windows_or_buffers_changed == 0 \
11926 || windows_or_buffers_changed == REDISPLAY_SOME) \
11927 && (update_mode_lines == 0 \
11928 || update_mode_lines == REDISPLAY_SOME))
11930 /* Prepare for redisplay by updating menu-bar item lists when
11931 appropriate. This can call eval. */
11933 static void
11934 prepare_menu_bars (void)
11936 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11937 bool some_windows = REDISPLAY_SOME_P ();
11938 Lisp_Object tooltip_frame;
11940 #ifdef HAVE_WINDOW_SYSTEM
11941 tooltip_frame = tip_frame;
11942 #else
11943 tooltip_frame = Qnil;
11944 #endif
11946 if (FUNCTIONP (Vpre_redisplay_function))
11948 Lisp_Object windows = all_windows ? Qt : Qnil;
11949 if (all_windows && some_windows)
11951 Lisp_Object ws = window_list ();
11952 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11954 Lisp_Object this = XCAR (ws);
11955 struct window *w = XWINDOW (this);
11956 if (w->redisplay
11957 || XFRAME (w->frame)->redisplay
11958 || XBUFFER (w->contents)->text->redisplay)
11960 windows = Fcons (this, windows);
11964 safe__call1 (true, Vpre_redisplay_function, windows);
11967 /* Update all frame titles based on their buffer names, etc. We do
11968 this before the menu bars so that the buffer-menu will show the
11969 up-to-date frame titles. */
11970 #ifdef HAVE_WINDOW_SYSTEM
11971 if (all_windows)
11973 Lisp_Object tail, frame;
11975 FOR_EACH_FRAME (tail, frame)
11977 struct frame *f = XFRAME (frame);
11978 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11979 if (some_windows
11980 && !f->redisplay
11981 && !w->redisplay
11982 && !XBUFFER (w->contents)->text->redisplay)
11983 continue;
11985 if (!EQ (frame, tooltip_frame)
11986 && !FRAME_PARENT_FRAME (f)
11987 && (FRAME_ICONIFIED_P (f)
11988 || FRAME_VISIBLE_P (f) == 1
11989 /* Exclude TTY frames that are obscured because they
11990 are not the top frame on their console. This is
11991 because x_consider_frame_title actually switches
11992 to the frame, which for TTY frames means it is
11993 marked as garbaged, and will be completely
11994 redrawn on the next redisplay cycle. This causes
11995 TTY frames to be completely redrawn, when there
11996 are more than one of them, even though nothing
11997 should be changed on display. */
11998 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11999 x_consider_frame_title (frame);
12002 #endif /* HAVE_WINDOW_SYSTEM */
12004 /* Update the menu bar item lists, if appropriate. This has to be
12005 done before any actual redisplay or generation of display lines. */
12007 if (all_windows)
12009 Lisp_Object tail, frame;
12010 ptrdiff_t count = SPECPDL_INDEX ();
12011 /* True means that update_menu_bar has run its hooks
12012 so any further calls to update_menu_bar shouldn't do so again. */
12013 bool menu_bar_hooks_run = false;
12015 record_unwind_save_match_data ();
12017 FOR_EACH_FRAME (tail, frame)
12019 struct frame *f = XFRAME (frame);
12020 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12022 /* Ignore tooltip frame. */
12023 if (EQ (frame, tooltip_frame))
12024 continue;
12026 if (some_windows
12027 && !f->redisplay
12028 && !w->redisplay
12029 && !XBUFFER (w->contents)->text->redisplay)
12030 continue;
12032 run_window_size_change_functions (frame);
12034 if (FRAME_PARENT_FRAME (f))
12035 continue;
12037 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12038 #ifdef HAVE_WINDOW_SYSTEM
12039 update_tool_bar (f, false);
12040 #endif
12043 unbind_to (count, Qnil);
12045 else
12047 struct frame *sf = SELECTED_FRAME ();
12048 update_menu_bar (sf, true, false);
12049 #ifdef HAVE_WINDOW_SYSTEM
12050 update_tool_bar (sf, true);
12051 #endif
12056 /* Update the menu bar item list for frame F. This has to be done
12057 before we start to fill in any display lines, because it can call
12058 eval.
12060 If SAVE_MATCH_DATA, we must save and restore it here.
12062 If HOOKS_RUN, a previous call to update_menu_bar
12063 already ran the menu bar hooks for this redisplay, so there
12064 is no need to run them again. The return value is the
12065 updated value of this flag, to pass to the next call. */
12067 static bool
12068 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12070 Lisp_Object window;
12071 struct window *w;
12073 /* If called recursively during a menu update, do nothing. This can
12074 happen when, for instance, an activate-menubar-hook causes a
12075 redisplay. */
12076 if (inhibit_menubar_update)
12077 return hooks_run;
12079 window = FRAME_SELECTED_WINDOW (f);
12080 w = XWINDOW (window);
12082 if (FRAME_WINDOW_P (f)
12084 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12085 || defined (HAVE_NS) || defined (USE_GTK)
12086 FRAME_EXTERNAL_MENU_BAR (f)
12087 #else
12088 FRAME_MENU_BAR_LINES (f) > 0
12089 #endif
12090 : FRAME_MENU_BAR_LINES (f) > 0)
12092 /* If the user has switched buffers or windows, we need to
12093 recompute to reflect the new bindings. But we'll
12094 recompute when update_mode_lines is set too; that means
12095 that people can use force-mode-line-update to request
12096 that the menu bar be recomputed. The adverse effect on
12097 the rest of the redisplay algorithm is about the same as
12098 windows_or_buffers_changed anyway. */
12099 if (windows_or_buffers_changed
12100 /* This used to test w->update_mode_line, but we believe
12101 there is no need to recompute the menu in that case. */
12102 || update_mode_lines
12103 || window_buffer_changed (w))
12105 struct buffer *prev = current_buffer;
12106 ptrdiff_t count = SPECPDL_INDEX ();
12108 specbind (Qinhibit_menubar_update, Qt);
12110 set_buffer_internal_1 (XBUFFER (w->contents));
12111 if (save_match_data)
12112 record_unwind_save_match_data ();
12113 if (NILP (Voverriding_local_map_menu_flag))
12115 specbind (Qoverriding_terminal_local_map, Qnil);
12116 specbind (Qoverriding_local_map, Qnil);
12119 if (!hooks_run)
12121 /* Run the Lucid hook. */
12122 safe_run_hooks (Qactivate_menubar_hook);
12124 /* If it has changed current-menubar from previous value,
12125 really recompute the menu-bar from the value. */
12126 if (! NILP (Vlucid_menu_bar_dirty_flag))
12127 call0 (Qrecompute_lucid_menubar);
12129 safe_run_hooks (Qmenu_bar_update_hook);
12131 hooks_run = true;
12134 XSETFRAME (Vmenu_updating_frame, f);
12135 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12137 /* Redisplay the menu bar in case we changed it. */
12138 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12139 || defined (HAVE_NS) || defined (USE_GTK)
12140 if (FRAME_WINDOW_P (f))
12142 #if defined (HAVE_NS)
12143 /* All frames on Mac OS share the same menubar. So only
12144 the selected frame should be allowed to set it. */
12145 if (f == SELECTED_FRAME ())
12146 #endif
12147 set_frame_menubar (f, false, false);
12149 else
12150 /* On a terminal screen, the menu bar is an ordinary screen
12151 line, and this makes it get updated. */
12152 w->update_mode_line = true;
12153 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12154 /* In the non-toolkit version, the menu bar is an ordinary screen
12155 line, and this makes it get updated. */
12156 w->update_mode_line = true;
12157 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12159 unbind_to (count, Qnil);
12160 set_buffer_internal_1 (prev);
12164 return hooks_run;
12167 /***********************************************************************
12168 Tool-bars
12169 ***********************************************************************/
12171 #ifdef HAVE_WINDOW_SYSTEM
12173 /* Select `frame' temporarily without running all the code in
12174 do_switch_frame.
12175 FIXME: Maybe do_switch_frame should be trimmed down similarly
12176 when `norecord' is set. */
12177 static void
12178 fast_set_selected_frame (Lisp_Object frame)
12180 if (!EQ (selected_frame, frame))
12182 selected_frame = frame;
12183 selected_window = XFRAME (frame)->selected_window;
12187 /* Update the tool-bar item list for frame F. This has to be done
12188 before we start to fill in any display lines. Called from
12189 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12190 and restore it here. */
12192 static void
12193 update_tool_bar (struct frame *f, bool save_match_data)
12195 #if defined (USE_GTK) || defined (HAVE_NS)
12196 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12197 #else
12198 bool do_update = (WINDOWP (f->tool_bar_window)
12199 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12200 #endif
12202 if (do_update)
12204 Lisp_Object window;
12205 struct window *w;
12207 window = FRAME_SELECTED_WINDOW (f);
12208 w = XWINDOW (window);
12210 /* If the user has switched buffers or windows, we need to
12211 recompute to reflect the new bindings. But we'll
12212 recompute when update_mode_lines is set too; that means
12213 that people can use force-mode-line-update to request
12214 that the menu bar be recomputed. The adverse effect on
12215 the rest of the redisplay algorithm is about the same as
12216 windows_or_buffers_changed anyway. */
12217 if (windows_or_buffers_changed
12218 || w->update_mode_line
12219 || update_mode_lines
12220 || window_buffer_changed (w))
12222 struct buffer *prev = current_buffer;
12223 ptrdiff_t count = SPECPDL_INDEX ();
12224 Lisp_Object frame, new_tool_bar;
12225 int new_n_tool_bar;
12227 /* Set current_buffer to the buffer of the selected
12228 window of the frame, so that we get the right local
12229 keymaps. */
12230 set_buffer_internal_1 (XBUFFER (w->contents));
12232 /* Save match data, if we must. */
12233 if (save_match_data)
12234 record_unwind_save_match_data ();
12236 /* Make sure that we don't accidentally use bogus keymaps. */
12237 if (NILP (Voverriding_local_map_menu_flag))
12239 specbind (Qoverriding_terminal_local_map, Qnil);
12240 specbind (Qoverriding_local_map, Qnil);
12243 /* We must temporarily set the selected frame to this frame
12244 before calling tool_bar_items, because the calculation of
12245 the tool-bar keymap uses the selected frame (see
12246 `tool-bar-make-keymap' in tool-bar.el). */
12247 eassert (EQ (selected_window,
12248 /* Since we only explicitly preserve selected_frame,
12249 check that selected_window would be redundant. */
12250 XFRAME (selected_frame)->selected_window));
12251 record_unwind_protect (fast_set_selected_frame, selected_frame);
12252 XSETFRAME (frame, f);
12253 fast_set_selected_frame (frame);
12255 /* Build desired tool-bar items from keymaps. */
12256 new_tool_bar
12257 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12258 &new_n_tool_bar);
12260 /* Redisplay the tool-bar if we changed it. */
12261 if (new_n_tool_bar != f->n_tool_bar_items
12262 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12264 /* Redisplay that happens asynchronously due to an expose event
12265 may access f->tool_bar_items. Make sure we update both
12266 variables within BLOCK_INPUT so no such event interrupts. */
12267 block_input ();
12268 fset_tool_bar_items (f, new_tool_bar);
12269 f->n_tool_bar_items = new_n_tool_bar;
12270 w->update_mode_line = true;
12271 unblock_input ();
12274 unbind_to (count, Qnil);
12275 set_buffer_internal_1 (prev);
12280 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12282 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12283 F's desired tool-bar contents. F->tool_bar_items must have
12284 been set up previously by calling prepare_menu_bars. */
12286 static void
12287 build_desired_tool_bar_string (struct frame *f)
12289 int i, size, size_needed;
12290 Lisp_Object image, plist;
12292 image = plist = Qnil;
12294 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12295 Otherwise, make a new string. */
12297 /* The size of the string we might be able to reuse. */
12298 size = (STRINGP (f->desired_tool_bar_string)
12299 ? SCHARS (f->desired_tool_bar_string)
12300 : 0);
12302 /* We need one space in the string for each image. */
12303 size_needed = f->n_tool_bar_items;
12305 /* Reuse f->desired_tool_bar_string, if possible. */
12306 if (size < size_needed || NILP (f->desired_tool_bar_string))
12307 fset_desired_tool_bar_string
12308 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12309 else
12311 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12312 Fremove_text_properties (make_number (0), make_number (size),
12313 props, f->desired_tool_bar_string);
12316 /* Put a `display' property on the string for the images to display,
12317 put a `menu_item' property on tool-bar items with a value that
12318 is the index of the item in F's tool-bar item vector. */
12319 for (i = 0; i < f->n_tool_bar_items; ++i)
12321 #define PROP(IDX) \
12322 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12324 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12325 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12326 int hmargin, vmargin, relief, idx, end;
12328 /* If image is a vector, choose the image according to the
12329 button state. */
12330 image = PROP (TOOL_BAR_ITEM_IMAGES);
12331 if (VECTORP (image))
12333 if (enabled_p)
12334 idx = (selected_p
12335 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12336 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12337 else
12338 idx = (selected_p
12339 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12340 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12342 eassert (ASIZE (image) >= idx);
12343 image = AREF (image, idx);
12345 else
12346 idx = -1;
12348 /* Ignore invalid image specifications. */
12349 if (!valid_image_p (image))
12350 continue;
12352 /* Display the tool-bar button pressed, or depressed. */
12353 plist = Fcopy_sequence (XCDR (image));
12355 /* Compute margin and relief to draw. */
12356 relief = (tool_bar_button_relief >= 0
12357 ? tool_bar_button_relief
12358 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12359 hmargin = vmargin = relief;
12361 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12362 INT_MAX - max (hmargin, vmargin)))
12364 hmargin += XFASTINT (Vtool_bar_button_margin);
12365 vmargin += XFASTINT (Vtool_bar_button_margin);
12367 else if (CONSP (Vtool_bar_button_margin))
12369 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12370 INT_MAX - hmargin))
12371 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12373 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12374 INT_MAX - vmargin))
12375 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12378 if (auto_raise_tool_bar_buttons_p)
12380 /* Add a `:relief' property to the image spec if the item is
12381 selected. */
12382 if (selected_p)
12384 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12385 hmargin -= relief;
12386 vmargin -= relief;
12389 else
12391 /* If image is selected, display it pressed, i.e. with a
12392 negative relief. If it's not selected, display it with a
12393 raised relief. */
12394 plist = Fplist_put (plist, QCrelief,
12395 (selected_p
12396 ? make_number (-relief)
12397 : make_number (relief)));
12398 hmargin -= relief;
12399 vmargin -= relief;
12402 /* Put a margin around the image. */
12403 if (hmargin || vmargin)
12405 if (hmargin == vmargin)
12406 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12407 else
12408 plist = Fplist_put (plist, QCmargin,
12409 Fcons (make_number (hmargin),
12410 make_number (vmargin)));
12413 /* If button is not enabled, and we don't have special images
12414 for the disabled state, make the image appear disabled by
12415 applying an appropriate algorithm to it. */
12416 if (!enabled_p && idx < 0)
12417 plist = Fplist_put (plist, QCconversion, Qdisabled);
12419 /* Put a `display' text property on the string for the image to
12420 display. Put a `menu-item' property on the string that gives
12421 the start of this item's properties in the tool-bar items
12422 vector. */
12423 image = Fcons (Qimage, plist);
12424 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12425 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12427 /* Let the last image hide all remaining spaces in the tool bar
12428 string. The string can be longer than needed when we reuse a
12429 previous string. */
12430 if (i + 1 == f->n_tool_bar_items)
12431 end = SCHARS (f->desired_tool_bar_string);
12432 else
12433 end = i + 1;
12434 Fadd_text_properties (make_number (i), make_number (end),
12435 props, f->desired_tool_bar_string);
12436 #undef PROP
12441 /* Display one line of the tool-bar of frame IT->f.
12443 HEIGHT specifies the desired height of the tool-bar line.
12444 If the actual height of the glyph row is less than HEIGHT, the
12445 row's height is increased to HEIGHT, and the icons are centered
12446 vertically in the new height.
12448 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12449 count a final empty row in case the tool-bar width exactly matches
12450 the window width.
12453 static void
12454 display_tool_bar_line (struct it *it, int height)
12456 struct glyph_row *row = it->glyph_row;
12457 int max_x = it->last_visible_x;
12458 struct glyph *last;
12460 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12461 clear_glyph_row (row);
12462 row->enabled_p = true;
12463 row->y = it->current_y;
12465 /* Note that this isn't made use of if the face hasn't a box,
12466 so there's no need to check the face here. */
12467 it->start_of_box_run_p = true;
12469 while (it->current_x < max_x)
12471 int x, n_glyphs_before, i, nglyphs;
12472 struct it it_before;
12474 /* Get the next display element. */
12475 if (!get_next_display_element (it))
12477 /* Don't count empty row if we are counting needed tool-bar lines. */
12478 if (height < 0 && !it->hpos)
12479 return;
12480 break;
12483 /* Produce glyphs. */
12484 n_glyphs_before = row->used[TEXT_AREA];
12485 it_before = *it;
12487 PRODUCE_GLYPHS (it);
12489 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12490 i = 0;
12491 x = it_before.current_x;
12492 while (i < nglyphs)
12494 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12496 if (x + glyph->pixel_width > max_x)
12498 /* Glyph doesn't fit on line. Backtrack. */
12499 row->used[TEXT_AREA] = n_glyphs_before;
12500 *it = it_before;
12501 /* If this is the only glyph on this line, it will never fit on the
12502 tool-bar, so skip it. But ensure there is at least one glyph,
12503 so we don't accidentally disable the tool-bar. */
12504 if (n_glyphs_before == 0
12505 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12506 break;
12507 goto out;
12510 ++it->hpos;
12511 x += glyph->pixel_width;
12512 ++i;
12515 /* Stop at line end. */
12516 if (ITERATOR_AT_END_OF_LINE_P (it))
12517 break;
12519 set_iterator_to_next (it, true);
12522 out:;
12524 row->displays_text_p = row->used[TEXT_AREA] != 0;
12526 /* Use default face for the border below the tool bar.
12528 FIXME: When auto-resize-tool-bars is grow-only, there is
12529 no additional border below the possibly empty tool-bar lines.
12530 So to make the extra empty lines look "normal", we have to
12531 use the tool-bar face for the border too. */
12532 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12533 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12534 it->face_id = DEFAULT_FACE_ID;
12536 extend_face_to_end_of_line (it);
12537 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12538 last->right_box_line_p = true;
12539 if (last == row->glyphs[TEXT_AREA])
12540 last->left_box_line_p = true;
12542 /* Make line the desired height and center it vertically. */
12543 if ((height -= it->max_ascent + it->max_descent) > 0)
12545 /* Don't add more than one line height. */
12546 height %= FRAME_LINE_HEIGHT (it->f);
12547 it->max_ascent += height / 2;
12548 it->max_descent += (height + 1) / 2;
12551 compute_line_metrics (it);
12553 /* If line is empty, make it occupy the rest of the tool-bar. */
12554 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12556 row->height = row->phys_height = it->last_visible_y - row->y;
12557 row->visible_height = row->height;
12558 row->ascent = row->phys_ascent = 0;
12559 row->extra_line_spacing = 0;
12562 row->full_width_p = true;
12563 row->continued_p = false;
12564 row->truncated_on_left_p = false;
12565 row->truncated_on_right_p = false;
12567 it->current_x = it->hpos = 0;
12568 it->current_y += row->height;
12569 ++it->vpos;
12570 ++it->glyph_row;
12574 /* Value is the number of pixels needed to make all tool-bar items of
12575 frame F visible. The actual number of glyph rows needed is
12576 returned in *N_ROWS if non-NULL. */
12577 static int
12578 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12580 struct window *w = XWINDOW (f->tool_bar_window);
12581 struct it it;
12582 /* tool_bar_height is called from redisplay_tool_bar after building
12583 the desired matrix, so use (unused) mode-line row as temporary row to
12584 avoid destroying the first tool-bar row. */
12585 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12587 /* Initialize an iterator for iteration over
12588 F->desired_tool_bar_string in the tool-bar window of frame F. */
12589 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12590 temp_row->reversed_p = false;
12591 it.first_visible_x = 0;
12592 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12593 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12594 it.paragraph_embedding = L2R;
12596 while (!ITERATOR_AT_END_P (&it))
12598 clear_glyph_row (temp_row);
12599 it.glyph_row = temp_row;
12600 display_tool_bar_line (&it, -1);
12602 clear_glyph_row (temp_row);
12604 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12605 if (n_rows)
12606 *n_rows = it.vpos > 0 ? it.vpos : -1;
12608 if (pixelwise)
12609 return it.current_y;
12610 else
12611 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12614 #endif /* !USE_GTK && !HAVE_NS */
12616 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12617 0, 2, 0,
12618 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12619 If FRAME is nil or omitted, use the selected frame. Optional argument
12620 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12621 (Lisp_Object frame, Lisp_Object pixelwise)
12623 int height = 0;
12625 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12626 struct frame *f = decode_any_frame (frame);
12628 if (WINDOWP (f->tool_bar_window)
12629 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12631 update_tool_bar (f, true);
12632 if (f->n_tool_bar_items)
12634 build_desired_tool_bar_string (f);
12635 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12638 #endif
12640 return make_number (height);
12644 /* Display the tool-bar of frame F. Value is true if tool-bar's
12645 height should be changed. */
12646 static bool
12647 redisplay_tool_bar (struct frame *f)
12649 f->tool_bar_redisplayed = true;
12650 #if defined (USE_GTK) || defined (HAVE_NS)
12652 if (FRAME_EXTERNAL_TOOL_BAR (f))
12653 update_frame_tool_bar (f);
12654 return false;
12656 #else /* !USE_GTK && !HAVE_NS */
12658 struct window *w;
12659 struct it it;
12660 struct glyph_row *row;
12662 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12663 do anything. This means you must start with tool-bar-lines
12664 non-zero to get the auto-sizing effect. Or in other words, you
12665 can turn off tool-bars by specifying tool-bar-lines zero. */
12666 if (!WINDOWP (f->tool_bar_window)
12667 || (w = XWINDOW (f->tool_bar_window),
12668 WINDOW_TOTAL_LINES (w) == 0))
12669 return false;
12671 /* Set up an iterator for the tool-bar window. */
12672 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12673 it.first_visible_x = 0;
12674 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12675 row = it.glyph_row;
12676 row->reversed_p = false;
12678 /* Build a string that represents the contents of the tool-bar. */
12679 build_desired_tool_bar_string (f);
12680 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12681 /* FIXME: This should be controlled by a user option. But it
12682 doesn't make sense to have an R2L tool bar if the menu bar cannot
12683 be drawn also R2L, and making the menu bar R2L is tricky due
12684 toolkit-specific code that implements it. If an R2L tool bar is
12685 ever supported, display_tool_bar_line should also be augmented to
12686 call unproduce_glyphs like display_line and display_string
12687 do. */
12688 it.paragraph_embedding = L2R;
12690 if (f->n_tool_bar_rows == 0)
12692 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12694 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12696 x_change_tool_bar_height (f, new_height);
12697 frame_default_tool_bar_height = new_height;
12698 /* Always do that now. */
12699 clear_glyph_matrix (w->desired_matrix);
12700 f->fonts_changed = true;
12701 return true;
12705 /* Display as many lines as needed to display all tool-bar items. */
12707 if (f->n_tool_bar_rows > 0)
12709 int border, rows, height, extra;
12711 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12712 border = XINT (Vtool_bar_border);
12713 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12714 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12715 else if (EQ (Vtool_bar_border, Qborder_width))
12716 border = f->border_width;
12717 else
12718 border = 0;
12719 if (border < 0)
12720 border = 0;
12722 rows = f->n_tool_bar_rows;
12723 height = max (1, (it.last_visible_y - border) / rows);
12724 extra = it.last_visible_y - border - height * rows;
12726 while (it.current_y < it.last_visible_y)
12728 int h = 0;
12729 if (extra > 0 && rows-- > 0)
12731 h = (extra + rows - 1) / rows;
12732 extra -= h;
12734 display_tool_bar_line (&it, height + h);
12737 else
12739 while (it.current_y < it.last_visible_y)
12740 display_tool_bar_line (&it, 0);
12743 /* It doesn't make much sense to try scrolling in the tool-bar
12744 window, so don't do it. */
12745 w->desired_matrix->no_scrolling_p = true;
12746 w->must_be_updated_p = true;
12748 if (!NILP (Vauto_resize_tool_bars))
12750 bool change_height_p = true;
12752 /* If we couldn't display everything, change the tool-bar's
12753 height if there is room for more. */
12754 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12755 change_height_p = true;
12757 /* We subtract 1 because display_tool_bar_line advances the
12758 glyph_row pointer before returning to its caller. We want to
12759 examine the last glyph row produced by
12760 display_tool_bar_line. */
12761 row = it.glyph_row - 1;
12763 /* If there are blank lines at the end, except for a partially
12764 visible blank line at the end that is smaller than
12765 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12766 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12767 && row->height >= FRAME_LINE_HEIGHT (f))
12768 change_height_p = true;
12770 /* If row displays tool-bar items, but is partially visible,
12771 change the tool-bar's height. */
12772 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12773 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12774 change_height_p = true;
12776 /* Resize windows as needed by changing the `tool-bar-lines'
12777 frame parameter. */
12778 if (change_height_p)
12780 int nrows;
12781 int new_height = tool_bar_height (f, &nrows, true);
12783 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12784 && !f->minimize_tool_bar_window_p)
12785 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12786 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12787 f->minimize_tool_bar_window_p = false;
12789 if (change_height_p)
12791 x_change_tool_bar_height (f, new_height);
12792 frame_default_tool_bar_height = new_height;
12793 clear_glyph_matrix (w->desired_matrix);
12794 f->n_tool_bar_rows = nrows;
12795 f->fonts_changed = true;
12797 return true;
12802 f->minimize_tool_bar_window_p = false;
12803 return false;
12805 #endif /* USE_GTK || HAVE_NS */
12808 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12810 /* Get information about the tool-bar item which is displayed in GLYPH
12811 on frame F. Return in *PROP_IDX the index where tool-bar item
12812 properties start in F->tool_bar_items. Value is false if
12813 GLYPH doesn't display a tool-bar item. */
12815 static bool
12816 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12818 Lisp_Object prop;
12819 int charpos;
12821 /* This function can be called asynchronously, which means we must
12822 exclude any possibility that Fget_text_property signals an
12823 error. */
12824 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12825 charpos = max (0, charpos);
12827 /* Get the text property `menu-item' at pos. The value of that
12828 property is the start index of this item's properties in
12829 F->tool_bar_items. */
12830 prop = Fget_text_property (make_number (charpos),
12831 Qmenu_item, f->current_tool_bar_string);
12832 if (! INTEGERP (prop))
12833 return false;
12834 *prop_idx = XINT (prop);
12835 return true;
12839 /* Get information about the tool-bar item at position X/Y on frame F.
12840 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12841 the current matrix of the tool-bar window of F, or NULL if not
12842 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12843 item in F->tool_bar_items. Value is
12845 -1 if X/Y is not on a tool-bar item
12846 0 if X/Y is on the same item that was highlighted before.
12847 1 otherwise. */
12849 static int
12850 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12851 int *hpos, int *vpos, int *prop_idx)
12853 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12854 struct window *w = XWINDOW (f->tool_bar_window);
12855 int area;
12857 /* Find the glyph under X/Y. */
12858 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12859 if (*glyph == NULL)
12860 return -1;
12862 /* Get the start of this tool-bar item's properties in
12863 f->tool_bar_items. */
12864 if (!tool_bar_item_info (f, *glyph, prop_idx))
12865 return -1;
12867 /* Is mouse on the highlighted item? */
12868 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12869 && *vpos >= hlinfo->mouse_face_beg_row
12870 && *vpos <= hlinfo->mouse_face_end_row
12871 && (*vpos > hlinfo->mouse_face_beg_row
12872 || *hpos >= hlinfo->mouse_face_beg_col)
12873 && (*vpos < hlinfo->mouse_face_end_row
12874 || *hpos < hlinfo->mouse_face_end_col
12875 || hlinfo->mouse_face_past_end))
12876 return 0;
12878 return 1;
12882 /* EXPORT:
12883 Handle mouse button event on the tool-bar of frame F, at
12884 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12885 false for button release. MODIFIERS is event modifiers for button
12886 release. */
12888 void
12889 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12890 int modifiers)
12892 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12893 struct window *w = XWINDOW (f->tool_bar_window);
12894 int hpos, vpos, prop_idx;
12895 struct glyph *glyph;
12896 Lisp_Object enabled_p;
12897 int ts;
12899 /* If not on the highlighted tool-bar item, and mouse-highlight is
12900 non-nil, return. This is so we generate the tool-bar button
12901 click only when the mouse button is released on the same item as
12902 where it was pressed. However, when mouse-highlight is disabled,
12903 generate the click when the button is released regardless of the
12904 highlight, since tool-bar items are not highlighted in that
12905 case. */
12906 frame_to_window_pixel_xy (w, &x, &y);
12907 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12908 if (ts == -1
12909 || (ts != 0 && !NILP (Vmouse_highlight)))
12910 return;
12912 /* When mouse-highlight is off, generate the click for the item
12913 where the button was pressed, disregarding where it was
12914 released. */
12915 if (NILP (Vmouse_highlight) && !down_p)
12916 prop_idx = f->last_tool_bar_item;
12918 /* If item is disabled, do nothing. */
12919 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12920 if (NILP (enabled_p))
12921 return;
12923 if (down_p)
12925 /* Show item in pressed state. */
12926 if (!NILP (Vmouse_highlight))
12927 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12928 f->last_tool_bar_item = prop_idx;
12930 else
12932 Lisp_Object key, frame;
12933 struct input_event event;
12934 EVENT_INIT (event);
12936 /* Show item in released state. */
12937 if (!NILP (Vmouse_highlight))
12938 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12940 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12942 XSETFRAME (frame, f);
12943 event.kind = TOOL_BAR_EVENT;
12944 event.frame_or_window = frame;
12945 event.arg = frame;
12946 kbd_buffer_store_event (&event);
12948 event.kind = TOOL_BAR_EVENT;
12949 event.frame_or_window = frame;
12950 event.arg = key;
12951 event.modifiers = modifiers;
12952 kbd_buffer_store_event (&event);
12953 f->last_tool_bar_item = -1;
12958 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12959 tool-bar window-relative coordinates X/Y. Called from
12960 note_mouse_highlight. */
12962 static void
12963 note_tool_bar_highlight (struct frame *f, int x, int y)
12965 Lisp_Object window = f->tool_bar_window;
12966 struct window *w = XWINDOW (window);
12967 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12968 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12969 int hpos, vpos;
12970 struct glyph *glyph;
12971 struct glyph_row *row;
12972 int i;
12973 Lisp_Object enabled_p;
12974 int prop_idx;
12975 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12976 bool mouse_down_p;
12977 int rc;
12979 /* Function note_mouse_highlight is called with negative X/Y
12980 values when mouse moves outside of the frame. */
12981 if (x <= 0 || y <= 0)
12983 clear_mouse_face (hlinfo);
12984 return;
12987 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12988 if (rc < 0)
12990 /* Not on tool-bar item. */
12991 clear_mouse_face (hlinfo);
12992 return;
12994 else if (rc == 0)
12995 /* On same tool-bar item as before. */
12996 goto set_help_echo;
12998 clear_mouse_face (hlinfo);
13000 /* Mouse is down, but on different tool-bar item? */
13001 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13002 && f == dpyinfo->last_mouse_frame);
13004 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13005 return;
13007 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13009 /* If tool-bar item is not enabled, don't highlight it. */
13010 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13011 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13013 /* Compute the x-position of the glyph. In front and past the
13014 image is a space. We include this in the highlighted area. */
13015 row = MATRIX_ROW (w->current_matrix, vpos);
13016 for (i = x = 0; i < hpos; ++i)
13017 x += row->glyphs[TEXT_AREA][i].pixel_width;
13019 /* Record this as the current active region. */
13020 hlinfo->mouse_face_beg_col = hpos;
13021 hlinfo->mouse_face_beg_row = vpos;
13022 hlinfo->mouse_face_beg_x = x;
13023 hlinfo->mouse_face_past_end = false;
13025 hlinfo->mouse_face_end_col = hpos + 1;
13026 hlinfo->mouse_face_end_row = vpos;
13027 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13028 hlinfo->mouse_face_window = window;
13029 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13031 /* Display it as active. */
13032 show_mouse_face (hlinfo, draw);
13035 set_help_echo:
13037 /* Set help_echo_string to a help string to display for this tool-bar item.
13038 XTread_socket does the rest. */
13039 help_echo_object = help_echo_window = Qnil;
13040 help_echo_pos = -1;
13041 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13042 if (NILP (help_echo_string))
13043 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13046 #endif /* !USE_GTK && !HAVE_NS */
13048 #endif /* HAVE_WINDOW_SYSTEM */
13052 /************************************************************************
13053 Horizontal scrolling
13054 ************************************************************************/
13056 /* For all leaf windows in the window tree rooted at WINDOW, set their
13057 hscroll value so that PT is (i) visible in the window, and (ii) so
13058 that it is not within a certain margin at the window's left and
13059 right border. Value is true if any window's hscroll has been
13060 changed. */
13062 static bool
13063 hscroll_window_tree (Lisp_Object window)
13065 bool hscrolled_p = false;
13066 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13067 int hscroll_step_abs = 0;
13068 double hscroll_step_rel = 0;
13070 if (hscroll_relative_p)
13072 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13073 if (hscroll_step_rel < 0)
13075 hscroll_relative_p = false;
13076 hscroll_step_abs = 0;
13079 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13081 hscroll_step_abs = XINT (Vhscroll_step);
13082 if (hscroll_step_abs < 0)
13083 hscroll_step_abs = 0;
13085 else
13086 hscroll_step_abs = 0;
13088 while (WINDOWP (window))
13090 struct window *w = XWINDOW (window);
13092 if (WINDOWP (w->contents))
13093 hscrolled_p |= hscroll_window_tree (w->contents);
13094 else if (w->cursor.vpos >= 0)
13096 int h_margin;
13097 int text_area_width;
13098 struct glyph_row *cursor_row;
13099 struct glyph_row *bottom_row;
13101 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13102 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13103 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13104 else
13105 cursor_row = bottom_row - 1;
13107 if (!cursor_row->enabled_p)
13109 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13110 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13111 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13112 else
13113 cursor_row = bottom_row - 1;
13115 bool row_r2l_p = cursor_row->reversed_p;
13116 bool hscl = hscrolling_current_line_p (w);
13117 int x_offset = 0;
13118 /* When line numbers are displayed, we need to account for
13119 the horizontal space they consume. */
13120 if (!NILP (Vdisplay_line_numbers))
13122 struct glyph *g;
13123 if (!row_r2l_p)
13125 for (g = cursor_row->glyphs[TEXT_AREA];
13126 g < cursor_row->glyphs[TEXT_AREA]
13127 + cursor_row->used[TEXT_AREA];
13128 g++)
13130 if (!(NILP (g->object) && g->charpos < 0))
13131 break;
13132 x_offset += g->pixel_width;
13135 else
13137 for (g = cursor_row->glyphs[TEXT_AREA]
13138 + cursor_row->used[TEXT_AREA];
13139 g > cursor_row->glyphs[TEXT_AREA];
13140 g--)
13142 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13143 break;
13144 x_offset += (g - 1)->pixel_width;
13148 if (cursor_row->truncated_on_left_p)
13150 /* On TTY frames, don't count the left truncation glyph. */
13151 struct frame *f = XFRAME (WINDOW_FRAME (w));
13152 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13155 text_area_width = window_box_width (w, TEXT_AREA);
13157 /* Scroll when cursor is inside this scroll margin. */
13158 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13160 /* If the position of this window's point has explicitly
13161 changed, no more suspend auto hscrolling. */
13162 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13163 w->suspend_auto_hscroll = false;
13165 /* Remember window point. */
13166 Fset_marker (w->old_pointm,
13167 ((w == XWINDOW (selected_window))
13168 ? make_number (BUF_PT (XBUFFER (w->contents)))
13169 : Fmarker_position (w->pointm)),
13170 w->contents);
13172 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13173 && !w->suspend_auto_hscroll
13174 /* In some pathological cases, like restoring a window
13175 configuration into a frame that is much smaller than
13176 the one from which the configuration was saved, we
13177 get glyph rows whose start and end have zero buffer
13178 positions, which we cannot handle below. Just skip
13179 such windows. */
13180 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13181 /* For left-to-right rows, hscroll when cursor is either
13182 (i) inside the right hscroll margin, or (ii) if it is
13183 inside the left margin and the window is already
13184 hscrolled. */
13185 && ((!row_r2l_p
13186 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13187 || (cursor_row->enabled_p
13188 && cursor_row->truncated_on_right_p
13189 && (w->cursor.x >= text_area_width - h_margin))))
13190 /* For right-to-left rows, the logic is similar,
13191 except that rules for scrolling to left and right
13192 are reversed. E.g., if cursor.x <= h_margin, we
13193 need to hscroll "to the right" unconditionally,
13194 and that will scroll the screen to the left so as
13195 to reveal the next portion of the row. */
13196 || (row_r2l_p
13197 && ((cursor_row->enabled_p
13198 /* FIXME: It is confusing to set the
13199 truncated_on_right_p flag when R2L rows
13200 are actually truncated on the left. */
13201 && cursor_row->truncated_on_right_p
13202 && w->cursor.x <= h_margin)
13203 || (w->hscroll
13204 && (w->cursor.x >= (text_area_width - h_margin
13205 - x_offset)))))
13206 /* This last condition is needed when moving
13207 vertically from an hscrolled line to a short line
13208 that doesn't need to be hscrolled. If we omit
13209 this condition, the line from which we move will
13210 remain hscrolled. */
13211 || (hscl
13212 && w->hscroll != w->min_hscroll
13213 && !cursor_row->truncated_on_left_p)))
13215 struct it it;
13216 ptrdiff_t hscroll;
13217 struct buffer *saved_current_buffer;
13218 ptrdiff_t pt;
13219 int wanted_x;
13221 /* Find point in a display of infinite width. */
13222 saved_current_buffer = current_buffer;
13223 current_buffer = XBUFFER (w->contents);
13225 if (w == XWINDOW (selected_window))
13226 pt = PT;
13227 else
13228 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13230 /* Move iterator to pt starting at cursor_row->start in
13231 a line with infinite width. */
13232 init_to_row_start (&it, w, cursor_row);
13233 if (hscl)
13234 it.first_visible_x = window_hscroll_limited (w, it.f)
13235 * FRAME_COLUMN_WIDTH (it.f);
13236 it.last_visible_x = DISP_INFINITY;
13237 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13238 /* If the line ends in an overlay string with a newline,
13239 we might infloop, because displaying the window will
13240 want to put the cursor after the overlay, i.e. at X
13241 coordinate of zero on the next screen line. So we
13242 use the buffer position prior to the overlay string
13243 instead. */
13244 if (it.method == GET_FROM_STRING && pt > 1)
13246 init_to_row_start (&it, w, cursor_row);
13247 if (hscl)
13248 it.first_visible_x = (window_hscroll_limited (w, it.f)
13249 * FRAME_COLUMN_WIDTH (it.f));
13250 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13252 current_buffer = saved_current_buffer;
13254 /* Position cursor in window. */
13255 if (!hscroll_relative_p && hscroll_step_abs == 0)
13256 hscroll = max (0, (it.current_x
13257 - (ITERATOR_AT_END_OF_LINE_P (&it)
13258 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13259 : (text_area_width / 2))))
13260 / FRAME_COLUMN_WIDTH (it.f);
13261 else if ((!row_r2l_p
13262 && w->cursor.x >= text_area_width - h_margin)
13263 || (row_r2l_p && w->cursor.x <= h_margin))
13265 if (hscroll_relative_p)
13266 wanted_x = text_area_width * (1 - hscroll_step_rel)
13267 - h_margin;
13268 else
13269 wanted_x = text_area_width
13270 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13271 - h_margin;
13272 hscroll
13273 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13275 else
13277 if (hscroll_relative_p)
13278 wanted_x = text_area_width * hscroll_step_rel
13279 + h_margin;
13280 else
13281 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13282 + h_margin;
13283 hscroll
13284 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13286 hscroll = max (hscroll, w->min_hscroll);
13288 /* Don't prevent redisplay optimizations if hscroll
13289 hasn't changed, as it will unnecessarily slow down
13290 redisplay. */
13291 if (w->hscroll != hscroll
13292 /* When hscrolling only the current line, we need to
13293 report hscroll even if its value is equal to the
13294 previous one, because the new line might need a
13295 different value. */
13296 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13298 struct buffer *b = XBUFFER (w->contents);
13299 b->prevent_redisplay_optimizations_p = true;
13300 w->hscroll = hscroll;
13301 hscrolled_p = true;
13306 window = w->next;
13309 /* Value is true if hscroll of any leaf window has been changed. */
13310 return hscrolled_p;
13314 /* Set hscroll so that cursor is visible and not inside horizontal
13315 scroll margins for all windows in the tree rooted at WINDOW. See
13316 also hscroll_window_tree above. Value is true if any window's
13317 hscroll has been changed. If it has, desired matrices on the frame
13318 of WINDOW are cleared. */
13320 static bool
13321 hscroll_windows (Lisp_Object window)
13323 bool hscrolled_p = hscroll_window_tree (window);
13324 if (hscrolled_p)
13325 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13326 return hscrolled_p;
13331 /************************************************************************
13332 Redisplay
13333 ************************************************************************/
13335 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13336 This is sometimes handy to have in a debugger session. */
13338 #ifdef GLYPH_DEBUG
13340 /* First and last unchanged row for try_window_id. */
13342 static int debug_first_unchanged_at_end_vpos;
13343 static int debug_last_unchanged_at_beg_vpos;
13345 /* Delta vpos and y. */
13347 static int debug_dvpos, debug_dy;
13349 /* Delta in characters and bytes for try_window_id. */
13351 static ptrdiff_t debug_delta, debug_delta_bytes;
13353 /* Values of window_end_pos and window_end_vpos at the end of
13354 try_window_id. */
13356 static ptrdiff_t debug_end_vpos;
13358 /* Append a string to W->desired_matrix->method. FMT is a printf
13359 format string. If trace_redisplay_p is true also printf the
13360 resulting string to stderr. */
13362 static void debug_method_add (struct window *, char const *, ...)
13363 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13365 static void
13366 debug_method_add (struct window *w, char const *fmt, ...)
13368 void *ptr = w;
13369 char *method = w->desired_matrix->method;
13370 int len = strlen (method);
13371 int size = sizeof w->desired_matrix->method;
13372 int remaining = size - len - 1;
13373 va_list ap;
13375 if (len && remaining)
13377 method[len] = '|';
13378 --remaining, ++len;
13381 va_start (ap, fmt);
13382 vsnprintf (method + len, remaining + 1, fmt, ap);
13383 va_end (ap);
13385 if (trace_redisplay_p)
13386 fprintf (stderr, "%p (%s): %s\n",
13387 ptr,
13388 ((BUFFERP (w->contents)
13389 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13390 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13391 : "no buffer"),
13392 method + len);
13395 #endif /* GLYPH_DEBUG */
13398 /* Value is true if all changes in window W, which displays
13399 current_buffer, are in the text between START and END. START is a
13400 buffer position, END is given as a distance from Z. Used in
13401 redisplay_internal for display optimization. */
13403 static bool
13404 text_outside_line_unchanged_p (struct window *w,
13405 ptrdiff_t start, ptrdiff_t end)
13407 bool unchanged_p = true;
13409 /* If text or overlays have changed, see where. */
13410 if (window_outdated (w))
13412 /* Gap in the line? */
13413 if (GPT < start || Z - GPT < end)
13414 unchanged_p = false;
13416 /* Changes start in front of the line, or end after it? */
13417 if (unchanged_p
13418 && (BEG_UNCHANGED < start - 1
13419 || END_UNCHANGED < end))
13420 unchanged_p = false;
13422 /* If selective display, can't optimize if changes start at the
13423 beginning of the line. */
13424 if (unchanged_p
13425 && INTEGERP (BVAR (current_buffer, selective_display))
13426 && XINT (BVAR (current_buffer, selective_display)) > 0
13427 && (BEG_UNCHANGED < start || GPT <= start))
13428 unchanged_p = false;
13430 /* If there are overlays at the start or end of the line, these
13431 may have overlay strings with newlines in them. A change at
13432 START, for instance, may actually concern the display of such
13433 overlay strings as well, and they are displayed on different
13434 lines. So, quickly rule out this case. (For the future, it
13435 might be desirable to implement something more telling than
13436 just BEG/END_UNCHANGED.) */
13437 if (unchanged_p)
13439 if (BEG + BEG_UNCHANGED == start
13440 && overlay_touches_p (start))
13441 unchanged_p = false;
13442 if (END_UNCHANGED == end
13443 && overlay_touches_p (Z - end))
13444 unchanged_p = false;
13447 /* Under bidi reordering, adding or deleting a character in the
13448 beginning of a paragraph, before the first strong directional
13449 character, can change the base direction of the paragraph (unless
13450 the buffer specifies a fixed paragraph direction), which will
13451 require redisplaying the whole paragraph. It might be worthwhile
13452 to find the paragraph limits and widen the range of redisplayed
13453 lines to that, but for now just give up this optimization. */
13454 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13455 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13456 unchanged_p = false;
13459 return unchanged_p;
13463 /* Do a frame update, taking possible shortcuts into account. This is
13464 the main external entry point for redisplay.
13466 If the last redisplay displayed an echo area message and that message
13467 is no longer requested, we clear the echo area or bring back the
13468 mini-buffer if that is in use. */
13470 void
13471 redisplay (void)
13473 redisplay_internal ();
13477 static Lisp_Object
13478 overlay_arrow_string_or_property (Lisp_Object var)
13480 Lisp_Object val;
13482 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13483 return val;
13485 return Voverlay_arrow_string;
13488 /* Return true if there are any overlay-arrows in current_buffer. */
13489 static bool
13490 overlay_arrow_in_current_buffer_p (void)
13492 Lisp_Object vlist;
13494 for (vlist = Voverlay_arrow_variable_list;
13495 CONSP (vlist);
13496 vlist = XCDR (vlist))
13498 Lisp_Object var = XCAR (vlist);
13499 Lisp_Object val;
13501 if (!SYMBOLP (var))
13502 continue;
13503 val = find_symbol_value (var);
13504 if (MARKERP (val)
13505 && current_buffer == XMARKER (val)->buffer)
13506 return true;
13508 return false;
13512 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13513 has changed.
13514 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13515 buffers that are affected. */
13517 static bool
13518 overlay_arrows_changed_p (bool set_redisplay)
13520 Lisp_Object vlist;
13521 bool changed = false;
13523 for (vlist = Voverlay_arrow_variable_list;
13524 CONSP (vlist);
13525 vlist = XCDR (vlist))
13527 Lisp_Object var = XCAR (vlist);
13528 Lisp_Object val, pstr;
13530 if (!SYMBOLP (var))
13531 continue;
13532 val = find_symbol_value (var);
13533 if (!MARKERP (val))
13534 continue;
13535 if (! EQ (COERCE_MARKER (val),
13536 /* FIXME: Don't we have a problem, using such a global
13537 * "last-position" if the variable is buffer-local? */
13538 Fget (var, Qlast_arrow_position))
13539 || ! (pstr = overlay_arrow_string_or_property (var),
13540 EQ (pstr, Fget (var, Qlast_arrow_string))))
13542 struct buffer *buf = XMARKER (val)->buffer;
13544 if (set_redisplay)
13546 if (buf)
13547 bset_redisplay (buf);
13548 changed = true;
13550 else
13551 return true;
13554 return changed;
13557 /* Mark overlay arrows to be updated on next redisplay. */
13559 static void
13560 update_overlay_arrows (int up_to_date)
13562 Lisp_Object vlist;
13564 for (vlist = Voverlay_arrow_variable_list;
13565 CONSP (vlist);
13566 vlist = XCDR (vlist))
13568 Lisp_Object var = XCAR (vlist);
13570 if (!SYMBOLP (var))
13571 continue;
13573 if (up_to_date > 0)
13575 Lisp_Object val = find_symbol_value (var);
13576 if (!MARKERP (val))
13577 continue;
13578 Fput (var, Qlast_arrow_position,
13579 COERCE_MARKER (val));
13580 Fput (var, Qlast_arrow_string,
13581 overlay_arrow_string_or_property (var));
13583 else if (up_to_date < 0
13584 || !NILP (Fget (var, Qlast_arrow_position)))
13586 Fput (var, Qlast_arrow_position, Qt);
13587 Fput (var, Qlast_arrow_string, Qt);
13593 /* Return overlay arrow string to display at row.
13594 Return integer (bitmap number) for arrow bitmap in left fringe.
13595 Return nil if no overlay arrow. */
13597 static Lisp_Object
13598 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13600 Lisp_Object vlist;
13602 for (vlist = Voverlay_arrow_variable_list;
13603 CONSP (vlist);
13604 vlist = XCDR (vlist))
13606 Lisp_Object var = XCAR (vlist);
13607 Lisp_Object val;
13609 if (!SYMBOLP (var))
13610 continue;
13612 val = find_symbol_value (var);
13614 if (MARKERP (val)
13615 && current_buffer == XMARKER (val)->buffer
13616 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13618 if (FRAME_WINDOW_P (it->f)
13619 /* FIXME: if ROW->reversed_p is set, this should test
13620 the right fringe, not the left one. */
13621 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13623 #ifdef HAVE_WINDOW_SYSTEM
13624 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13626 int fringe_bitmap = lookup_fringe_bitmap (val);
13627 if (fringe_bitmap != 0)
13628 return make_number (fringe_bitmap);
13630 #endif
13631 return make_number (-1); /* Use default arrow bitmap. */
13633 return overlay_arrow_string_or_property (var);
13637 return Qnil;
13640 /* Return true if point moved out of or into a composition. Otherwise
13641 return false. PREV_BUF and PREV_PT are the last point buffer and
13642 position. BUF and PT are the current point buffer and position. */
13644 static bool
13645 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13646 struct buffer *buf, ptrdiff_t pt)
13648 ptrdiff_t start, end;
13649 Lisp_Object prop;
13650 Lisp_Object buffer;
13652 XSETBUFFER (buffer, buf);
13653 /* Check a composition at the last point if point moved within the
13654 same buffer. */
13655 if (prev_buf == buf)
13657 if (prev_pt == pt)
13658 /* Point didn't move. */
13659 return false;
13661 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13662 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13663 && composition_valid_p (start, end, prop)
13664 && start < prev_pt && end > prev_pt)
13665 /* The last point was within the composition. Return true iff
13666 point moved out of the composition. */
13667 return (pt <= start || pt >= end);
13670 /* Check a composition at the current point. */
13671 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13672 && find_composition (pt, -1, &start, &end, &prop, buffer)
13673 && composition_valid_p (start, end, prop)
13674 && start < pt && end > pt);
13677 /* Reconsider the clip changes of buffer which is displayed in W. */
13679 static void
13680 reconsider_clip_changes (struct window *w)
13682 struct buffer *b = XBUFFER (w->contents);
13684 if (b->clip_changed
13685 && w->window_end_valid
13686 && w->current_matrix->buffer == b
13687 && w->current_matrix->zv == BUF_ZV (b)
13688 && w->current_matrix->begv == BUF_BEGV (b))
13689 b->clip_changed = false;
13691 /* If display wasn't paused, and W is not a tool bar window, see if
13692 point has been moved into or out of a composition. In that case,
13693 set b->clip_changed to force updating the screen. If
13694 b->clip_changed has already been set, skip this check. */
13695 if (!b->clip_changed && w->window_end_valid)
13697 ptrdiff_t pt = (w == XWINDOW (selected_window)
13698 ? PT : marker_position (w->pointm));
13700 if ((w->current_matrix->buffer != b || pt != w->last_point)
13701 && check_point_in_composition (w->current_matrix->buffer,
13702 w->last_point, b, pt))
13703 b->clip_changed = true;
13707 static void
13708 propagate_buffer_redisplay (void)
13709 { /* Resetting b->text->redisplay is problematic!
13710 We can't just reset it in the case that some window that displays
13711 it has not been redisplayed; and such a window can stay
13712 unredisplayed for a long time if it's currently invisible.
13713 But we do want to reset it at the end of redisplay otherwise
13714 its displayed windows will keep being redisplayed over and over
13715 again.
13716 So we copy all b->text->redisplay flags up to their windows here,
13717 such that mark_window_display_accurate can safely reset
13718 b->text->redisplay. */
13719 Lisp_Object ws = window_list ();
13720 for (; CONSP (ws); ws = XCDR (ws))
13722 struct window *thisw = XWINDOW (XCAR (ws));
13723 struct buffer *thisb = XBUFFER (thisw->contents);
13724 if (thisb->text->redisplay)
13725 thisw->redisplay = true;
13729 #define STOP_POLLING \
13730 do { if (! polling_stopped_here) stop_polling (); \
13731 polling_stopped_here = true; } while (false)
13733 #define RESUME_POLLING \
13734 do { if (polling_stopped_here) start_polling (); \
13735 polling_stopped_here = false; } while (false)
13738 /* Perhaps in the future avoid recentering windows if it
13739 is not necessary; currently that causes some problems. */
13741 static void
13742 redisplay_internal (void)
13744 struct window *w = XWINDOW (selected_window);
13745 struct window *sw;
13746 struct frame *fr;
13747 bool pending;
13748 bool must_finish = false, match_p;
13749 struct text_pos tlbufpos, tlendpos;
13750 int number_of_visible_frames;
13751 ptrdiff_t count;
13752 struct frame *sf;
13753 bool polling_stopped_here = false;
13754 Lisp_Object tail, frame;
13756 /* Set a limit to the number of retries we perform due to horizontal
13757 scrolling, this avoids getting stuck in an uninterruptible
13758 infinite loop (Bug #24633). */
13759 enum { MAX_HSCROLL_RETRIES = 16 };
13760 int hscroll_retries = 0;
13762 /* Limit the number of retries for when frame(s) become garbaged as
13763 result of redisplaying them. Some packages set various redisplay
13764 hooks, such as window-scroll-functions, to run Lisp that always
13765 calls APIs which cause the frame's garbaged flag to become set,
13766 so we loop indefinitely. */
13767 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13768 int garbaged_frame_retries = 0;
13770 /* True means redisplay has to consider all windows on all
13771 frames. False, only selected_window is considered. */
13772 bool consider_all_windows_p;
13774 /* True means redisplay has to redisplay the miniwindow. */
13775 bool update_miniwindow_p = false;
13777 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13779 /* No redisplay if running in batch mode or frame is not yet fully
13780 initialized, or redisplay is explicitly turned off by setting
13781 Vinhibit_redisplay. */
13782 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13783 || !NILP (Vinhibit_redisplay))
13784 return;
13786 /* Don't examine these until after testing Vinhibit_redisplay.
13787 When Emacs is shutting down, perhaps because its connection to
13788 X has dropped, we should not look at them at all. */
13789 fr = XFRAME (w->frame);
13790 sf = SELECTED_FRAME ();
13792 if (!fr->glyphs_initialized_p)
13793 return;
13795 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13796 if (popup_activated ())
13797 return;
13798 #endif
13800 /* I don't think this happens but let's be paranoid. */
13801 if (redisplaying_p)
13802 return;
13804 /* Record a function that clears redisplaying_p
13805 when we leave this function. */
13806 count = SPECPDL_INDEX ();
13807 record_unwind_protect_void (unwind_redisplay);
13808 redisplaying_p = true;
13809 block_buffer_flips ();
13810 specbind (Qinhibit_free_realized_faces, Qnil);
13812 /* Record this function, so it appears on the profiler's backtraces. */
13813 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13815 FOR_EACH_FRAME (tail, frame)
13816 XFRAME (frame)->already_hscrolled_p = false;
13818 retry:
13819 /* Remember the currently selected window. */
13820 sw = w;
13822 pending = false;
13823 forget_escape_and_glyphless_faces ();
13825 inhibit_free_realized_faces = false;
13827 /* If face_change, init_iterator will free all realized faces, which
13828 includes the faces referenced from current matrices. So, we
13829 can't reuse current matrices in this case. */
13830 if (face_change)
13831 windows_or_buffers_changed = 47;
13833 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13834 && FRAME_TTY (sf)->previous_frame != sf)
13836 /* Since frames on a single ASCII terminal share the same
13837 display area, displaying a different frame means redisplay
13838 the whole thing. */
13839 SET_FRAME_GARBAGED (sf);
13840 #ifndef DOS_NT
13841 set_tty_color_mode (FRAME_TTY (sf), sf);
13842 #endif
13843 FRAME_TTY (sf)->previous_frame = sf;
13846 /* Set the visible flags for all frames. Do this before checking for
13847 resized or garbaged frames; they want to know if their frames are
13848 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13849 number_of_visible_frames = 0;
13851 FOR_EACH_FRAME (tail, frame)
13853 struct frame *f = XFRAME (frame);
13855 if (FRAME_VISIBLE_P (f))
13857 ++number_of_visible_frames;
13858 /* Adjust matrices for visible frames only. */
13859 if (f->fonts_changed)
13861 adjust_frame_glyphs (f);
13862 /* Disable all redisplay optimizations for this frame.
13863 This is because adjust_frame_glyphs resets the
13864 enabled_p flag for all glyph rows of all windows, so
13865 many optimizations will fail anyway, and some might
13866 fail to test that flag and do bogus things as
13867 result. */
13868 SET_FRAME_GARBAGED (f);
13869 f->fonts_changed = false;
13871 /* If cursor type has been changed on the frame
13872 other than selected, consider all frames. */
13873 if (f != sf && f->cursor_type_changed)
13874 fset_redisplay (f);
13876 clear_desired_matrices (f);
13879 /* Notice any pending interrupt request to change frame size. */
13880 do_pending_window_change (true);
13882 /* do_pending_window_change could change the selected_window due to
13883 frame resizing which makes the selected window too small. */
13884 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13885 sw = w;
13887 /* Clear frames marked as garbaged. */
13888 clear_garbaged_frames ();
13890 /* Build menubar and tool-bar items. */
13891 if (NILP (Vmemory_full))
13892 prepare_menu_bars ();
13894 reconsider_clip_changes (w);
13896 /* In most cases selected window displays current buffer. */
13897 match_p = XBUFFER (w->contents) == current_buffer;
13898 if (match_p)
13900 /* Detect case that we need to write or remove a star in the mode line. */
13901 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13902 w->update_mode_line = true;
13904 if (mode_line_update_needed (w))
13905 w->update_mode_line = true;
13907 /* If reconsider_clip_changes above decided that the narrowing
13908 in the current buffer changed, make sure all other windows
13909 showing that buffer will be redisplayed. */
13910 if (current_buffer->clip_changed)
13911 bset_update_mode_line (current_buffer);
13914 /* Normally the message* functions will have already displayed and
13915 updated the echo area, but the frame may have been trashed, or
13916 the update may have been preempted, so display the echo area
13917 again here. Checking message_cleared_p captures the case that
13918 the echo area should be cleared. */
13919 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13920 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13921 || (message_cleared_p
13922 && minibuf_level == 0
13923 /* If the mini-window is currently selected, this means the
13924 echo-area doesn't show through. */
13925 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13927 echo_area_display (false);
13929 /* If echo_area_display resizes the mini-window, the redisplay and
13930 window_sizes_changed flags of the selected frame are set, but
13931 it's too late for the hooks in window-size-change-functions,
13932 which have been examined already in prepare_menu_bars. So in
13933 that case we call the hooks here only for the selected frame. */
13934 if (sf->redisplay)
13936 ptrdiff_t count1 = SPECPDL_INDEX ();
13938 record_unwind_save_match_data ();
13939 run_window_size_change_functions (selected_frame);
13940 unbind_to (count1, Qnil);
13943 if (message_cleared_p)
13944 update_miniwindow_p = true;
13946 must_finish = true;
13948 /* If we don't display the current message, don't clear the
13949 message_cleared_p flag, because, if we did, we wouldn't clear
13950 the echo area in the next redisplay which doesn't preserve
13951 the echo area. */
13952 if (!display_last_displayed_message_p)
13953 message_cleared_p = false;
13955 else if (EQ (selected_window, minibuf_window)
13956 && (current_buffer->clip_changed || window_outdated (w))
13957 && resize_mini_window (w, false))
13959 if (sf->redisplay)
13961 ptrdiff_t count1 = SPECPDL_INDEX ();
13963 record_unwind_save_match_data ();
13964 run_window_size_change_functions (selected_frame);
13965 unbind_to (count1, Qnil);
13968 /* Resized active mini-window to fit the size of what it is
13969 showing if its contents might have changed. */
13970 must_finish = true;
13972 /* If window configuration was changed, frames may have been
13973 marked garbaged. Clear them or we will experience
13974 surprises wrt scrolling. */
13975 clear_garbaged_frames ();
13978 if (windows_or_buffers_changed && !update_mode_lines)
13979 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13980 only the windows's contents needs to be refreshed, or whether the
13981 mode-lines also need a refresh. */
13982 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13983 ? REDISPLAY_SOME : 32);
13985 /* If specs for an arrow have changed, do thorough redisplay
13986 to ensure we remove any arrow that should no longer exist. */
13987 /* Apparently, this is the only case where we update other windows,
13988 without updating other mode-lines. */
13989 overlay_arrows_changed_p (true);
13991 consider_all_windows_p = (update_mode_lines
13992 || windows_or_buffers_changed);
13994 #define AINC(a,i) \
13996 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13997 if (INTEGERP (entry)) \
13998 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14001 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14002 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14004 /* Optimize the case that only the line containing the cursor in the
14005 selected window has changed. Variables starting with this_ are
14006 set in display_line and record information about the line
14007 containing the cursor. */
14008 tlbufpos = this_line_start_pos;
14009 tlendpos = this_line_end_pos;
14010 if (!consider_all_windows_p
14011 && CHARPOS (tlbufpos) > 0
14012 && !w->update_mode_line
14013 && !current_buffer->clip_changed
14014 && !current_buffer->prevent_redisplay_optimizations_p
14015 && FRAME_VISIBLE_P (XFRAME (w->frame))
14016 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14017 && !XFRAME (w->frame)->cursor_type_changed
14018 && !XFRAME (w->frame)->face_change
14019 /* Make sure recorded data applies to current buffer, etc. */
14020 && this_line_buffer == current_buffer
14021 && match_p
14022 && !w->force_start
14023 && !w->optional_new_start
14024 /* Point must be on the line that we have info recorded about. */
14025 && PT >= CHARPOS (tlbufpos)
14026 && PT <= Z - CHARPOS (tlendpos)
14027 /* All text outside that line, including its final newline,
14028 must be unchanged. */
14029 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14030 CHARPOS (tlendpos)))
14032 if (CHARPOS (tlbufpos) > BEGV
14033 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14034 && (CHARPOS (tlbufpos) == ZV
14035 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14036 /* Former continuation line has disappeared by becoming empty. */
14037 goto cancel;
14038 else if (window_outdated (w) || MINI_WINDOW_P (w))
14040 /* We have to handle the case of continuation around a
14041 wide-column character (see the comment in indent.c around
14042 line 1340).
14044 For instance, in the following case:
14046 -------- Insert --------
14047 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14048 J_I_ ==> J_I_ `^^' are cursors.
14049 ^^ ^^
14050 -------- --------
14052 As we have to redraw the line above, we cannot use this
14053 optimization. */
14055 struct it it;
14056 int line_height_before = this_line_pixel_height;
14058 /* Note that start_display will handle the case that the
14059 line starting at tlbufpos is a continuation line. */
14060 start_display (&it, w, tlbufpos);
14062 /* Implementation note: It this still necessary? */
14063 if (it.current_x != this_line_start_x)
14064 goto cancel;
14066 TRACE ((stderr, "trying display optimization 1\n"));
14067 w->cursor.vpos = -1;
14068 overlay_arrow_seen = false;
14069 it.vpos = this_line_vpos;
14070 it.current_y = this_line_y;
14071 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14072 display_line (&it, -1);
14074 /* If line contains point, is not continued,
14075 and ends at same distance from eob as before, we win. */
14076 if (w->cursor.vpos >= 0
14077 /* Line is not continued, otherwise this_line_start_pos
14078 would have been set to 0 in display_line. */
14079 && CHARPOS (this_line_start_pos)
14080 /* Line ends as before. */
14081 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14082 /* Line has same height as before. Otherwise other lines
14083 would have to be shifted up or down. */
14084 && this_line_pixel_height == line_height_before)
14086 /* If this is not the window's last line, we must adjust
14087 the charstarts of the lines below. */
14088 if (it.current_y < it.last_visible_y)
14090 struct glyph_row *row
14091 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14092 ptrdiff_t delta, delta_bytes;
14094 /* We used to distinguish between two cases here,
14095 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14096 when the line ends in a newline or the end of the
14097 buffer's accessible portion. But both cases did
14098 the same, so they were collapsed. */
14099 delta = (Z
14100 - CHARPOS (tlendpos)
14101 - MATRIX_ROW_START_CHARPOS (row));
14102 delta_bytes = (Z_BYTE
14103 - BYTEPOS (tlendpos)
14104 - MATRIX_ROW_START_BYTEPOS (row));
14106 increment_matrix_positions (w->current_matrix,
14107 this_line_vpos + 1,
14108 w->current_matrix->nrows,
14109 delta, delta_bytes);
14112 /* If this row displays text now but previously didn't,
14113 or vice versa, w->window_end_vpos may have to be
14114 adjusted. */
14115 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14117 if (w->window_end_vpos < this_line_vpos)
14118 w->window_end_vpos = this_line_vpos;
14120 else if (w->window_end_vpos == this_line_vpos
14121 && this_line_vpos > 0)
14122 w->window_end_vpos = this_line_vpos - 1;
14123 w->window_end_valid = false;
14125 /* Update hint: No need to try to scroll in update_window. */
14126 w->desired_matrix->no_scrolling_p = true;
14128 #ifdef GLYPH_DEBUG
14129 *w->desired_matrix->method = 0;
14130 debug_method_add (w, "optimization 1");
14131 #endif
14132 #ifdef HAVE_WINDOW_SYSTEM
14133 update_window_fringes (w, false);
14134 #endif
14135 goto update;
14137 else
14138 goto cancel;
14140 else if (/* Cursor position hasn't changed. */
14141 PT == w->last_point
14142 /* Make sure the cursor was last displayed
14143 in this window. Otherwise we have to reposition it. */
14145 /* PXW: Must be converted to pixels, probably. */
14146 && 0 <= w->cursor.vpos
14147 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14149 if (!must_finish)
14151 do_pending_window_change (true);
14152 /* If selected_window changed, redisplay again. */
14153 if (WINDOWP (selected_window)
14154 && (w = XWINDOW (selected_window)) != sw)
14155 goto retry;
14157 /* We used to always goto end_of_redisplay here, but this
14158 isn't enough if we have a blinking cursor. */
14159 if (w->cursor_off_p == w->last_cursor_off_p)
14160 goto end_of_redisplay;
14162 goto update;
14164 /* If highlighting the region, or if the cursor is in the echo area,
14165 then we can't just move the cursor. */
14166 else if (NILP (Vshow_trailing_whitespace)
14167 && !cursor_in_echo_area)
14169 struct it it;
14170 struct glyph_row *row;
14172 /* Skip from tlbufpos to PT and see where it is. Note that
14173 PT may be in invisible text. If so, we will end at the
14174 next visible position. */
14175 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14176 NULL, DEFAULT_FACE_ID);
14177 it.current_x = this_line_start_x;
14178 it.current_y = this_line_y;
14179 it.vpos = this_line_vpos;
14181 /* The call to move_it_to stops in front of PT, but
14182 moves over before-strings. */
14183 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14185 if (it.vpos == this_line_vpos
14186 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14187 row->enabled_p))
14189 eassert (this_line_vpos == it.vpos);
14190 eassert (this_line_y == it.current_y);
14191 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14192 if (cursor_row_fully_visible_p (w, false, true))
14194 #ifdef GLYPH_DEBUG
14195 *w->desired_matrix->method = 0;
14196 debug_method_add (w, "optimization 3");
14197 #endif
14198 goto update;
14200 else
14201 goto cancel;
14203 else
14204 goto cancel;
14207 cancel:
14208 /* Text changed drastically or point moved off of line. */
14209 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14212 CHARPOS (this_line_start_pos) = 0;
14213 ++clear_face_cache_count;
14214 #ifdef HAVE_WINDOW_SYSTEM
14215 ++clear_image_cache_count;
14216 #endif
14218 /* Build desired matrices, and update the display. If
14219 consider_all_windows_p, do it for all windows on all frames that
14220 require redisplay, as specified by their 'redisplay' flag.
14221 Otherwise do it for selected_window, only. */
14223 if (consider_all_windows_p)
14225 FOR_EACH_FRAME (tail, frame)
14226 XFRAME (frame)->updated_p = false;
14228 propagate_buffer_redisplay ();
14230 FOR_EACH_FRAME (tail, frame)
14232 struct frame *f = XFRAME (frame);
14234 /* We don't have to do anything for unselected terminal
14235 frames. */
14236 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14237 && !EQ (FRAME_TTY (f)->top_frame, frame))
14238 continue;
14240 retry_frame:
14241 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14243 bool gcscrollbars
14244 /* Only GC scrollbars when we redisplay the whole frame. */
14245 = f->redisplay || !REDISPLAY_SOME_P ();
14246 bool f_redisplay_flag = f->redisplay;
14247 /* Mark all the scroll bars to be removed; we'll redeem
14248 the ones we want when we redisplay their windows. */
14249 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14250 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14252 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14253 redisplay_windows (FRAME_ROOT_WINDOW (f));
14254 /* Remember that the invisible frames need to be redisplayed next
14255 time they're visible. */
14256 else if (!REDISPLAY_SOME_P ())
14257 f->redisplay = true;
14259 /* The X error handler may have deleted that frame. */
14260 if (!FRAME_LIVE_P (f))
14261 continue;
14263 /* Any scroll bars which redisplay_windows should have
14264 nuked should now go away. */
14265 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14266 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14268 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14270 /* If fonts changed on visible frame, display again. */
14271 if (f->fonts_changed)
14273 adjust_frame_glyphs (f);
14274 /* Disable all redisplay optimizations for this
14275 frame. For the reasons, see the comment near
14276 the previous call to adjust_frame_glyphs above. */
14277 SET_FRAME_GARBAGED (f);
14278 f->fonts_changed = false;
14279 goto retry_frame;
14282 /* See if we have to hscroll. */
14283 if (!f->already_hscrolled_p)
14285 f->already_hscrolled_p = true;
14286 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14287 && hscroll_windows (f->root_window))
14289 hscroll_retries++;
14290 goto retry_frame;
14294 /* If the frame's redisplay flag was not set before
14295 we went about redisplaying its windows, but it is
14296 set now, that means we employed some redisplay
14297 optimizations inside redisplay_windows, and
14298 bypassed producing some screen lines. But if
14299 f->redisplay is now set, it might mean the old
14300 faces are no longer valid (e.g., if redisplaying
14301 some window called some Lisp which defined a new
14302 face or redefined an existing face), so trying to
14303 use them in update_frame will segfault.
14304 Therefore, we must redisplay this frame. */
14305 if (!f_redisplay_flag && f->redisplay)
14306 goto retry_frame;
14307 /* In some case (e.g., window resize), we notice
14308 only during window updating that the window
14309 content changed unpredictably (e.g., a GTK
14310 scrollbar moved, or some Lisp hook that winds up
14311 calling adjust_frame_glyphs) and that our
14312 previous estimation of the frame content was
14313 garbage. We have to start over. These cases
14314 should be rare, so going all the way back to the
14315 top of redisplay should be good enough. */
14316 if (FRAME_GARBAGED_P (f)
14317 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14318 goto retry;
14320 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14321 x_clear_under_internal_border (f);
14322 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14324 /* Prevent various kinds of signals during display
14325 update. stdio is not robust about handling
14326 signals, which can cause an apparent I/O error. */
14327 if (interrupt_input)
14328 unrequest_sigio ();
14329 STOP_POLLING;
14331 pending |= update_frame (f, false, false);
14332 f->cursor_type_changed = false;
14333 f->updated_p = true;
14338 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14340 if (!pending)
14342 /* Do the mark_window_display_accurate after all windows have
14343 been redisplayed because this call resets flags in buffers
14344 which are needed for proper redisplay. */
14345 FOR_EACH_FRAME (tail, frame)
14347 struct frame *f = XFRAME (frame);
14348 if (f->updated_p)
14350 f->redisplay = false;
14351 f->garbaged = false;
14352 mark_window_display_accurate (f->root_window, true);
14353 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14354 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14359 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14361 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14362 /* Use list_of_error, not Qerror, so that
14363 we catch only errors and don't run the debugger. */
14364 internal_condition_case_1 (redisplay_window_1, selected_window,
14365 list_of_error,
14366 redisplay_window_error);
14367 if (update_miniwindow_p)
14368 internal_condition_case_1 (redisplay_window_1,
14369 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14370 redisplay_window_error);
14372 /* Compare desired and current matrices, perform output. */
14374 update:
14375 /* If fonts changed, display again. Likewise if redisplay_window_1
14376 above caused some change (e.g., a change in faces) that requires
14377 considering the entire frame again. */
14378 if (sf->fonts_changed || sf->redisplay)
14380 if (sf->redisplay)
14382 /* Set this to force a more thorough redisplay.
14383 Otherwise, we might immediately loop back to the
14384 above "else-if" clause (since all the conditions that
14385 led here might still be true), and we will then
14386 infloop, because the selected-frame's redisplay flag
14387 is not (and cannot be) reset. */
14388 windows_or_buffers_changed = 50;
14390 goto retry;
14393 /* Prevent freeing of realized faces, since desired matrices are
14394 pending that reference the faces we computed and cached. */
14395 inhibit_free_realized_faces = true;
14397 /* Prevent various kinds of signals during display update.
14398 stdio is not robust about handling signals,
14399 which can cause an apparent I/O error. */
14400 if (interrupt_input)
14401 unrequest_sigio ();
14402 STOP_POLLING;
14404 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14406 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14407 && hscroll_windows (selected_window))
14409 hscroll_retries++;
14410 goto retry;
14413 XWINDOW (selected_window)->must_be_updated_p = true;
14414 pending = update_frame (sf, false, false);
14415 sf->cursor_type_changed = false;
14418 /* We may have called echo_area_display at the top of this
14419 function. If the echo area is on another frame, that may
14420 have put text on a frame other than the selected one, so the
14421 above call to update_frame would not have caught it. Catch
14422 it here. */
14423 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14424 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14426 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14428 XWINDOW (mini_window)->must_be_updated_p = true;
14429 pending |= update_frame (mini_frame, false, false);
14430 mini_frame->cursor_type_changed = false;
14431 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14432 && hscroll_windows (mini_window))
14434 hscroll_retries++;
14435 goto retry;
14440 /* If display was paused because of pending input, make sure we do a
14441 thorough update the next time. */
14442 if (pending)
14444 /* Prevent the optimization at the beginning of
14445 redisplay_internal that tries a single-line update of the
14446 line containing the cursor in the selected window. */
14447 CHARPOS (this_line_start_pos) = 0;
14449 /* Let the overlay arrow be updated the next time. */
14450 update_overlay_arrows (0);
14452 /* If we pause after scrolling, some rows in the current
14453 matrices of some windows are not valid. */
14454 if (!WINDOW_FULL_WIDTH_P (w)
14455 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14456 update_mode_lines = 36;
14458 else
14460 if (!consider_all_windows_p)
14462 /* This has already been done above if
14463 consider_all_windows_p is set. */
14464 if (XBUFFER (w->contents)->text->redisplay
14465 && buffer_window_count (XBUFFER (w->contents)) > 1)
14466 /* This can happen if b->text->redisplay was set during
14467 jit-lock. */
14468 propagate_buffer_redisplay ();
14469 mark_window_display_accurate_1 (w, true);
14471 /* Say overlay arrows are up to date. */
14472 update_overlay_arrows (1);
14474 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14475 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14478 update_mode_lines = 0;
14479 windows_or_buffers_changed = 0;
14482 /* Start SIGIO interrupts coming again. Having them off during the
14483 code above makes it less likely one will discard output, but not
14484 impossible, since there might be stuff in the system buffer here.
14485 But it is much hairier to try to do anything about that. */
14486 if (interrupt_input)
14487 request_sigio ();
14488 RESUME_POLLING;
14490 /* If a frame has become visible which was not before, redisplay
14491 again, so that we display it. Expose events for such a frame
14492 (which it gets when becoming visible) don't call the parts of
14493 redisplay constructing glyphs, so simply exposing a frame won't
14494 display anything in this case. So, we have to display these
14495 frames here explicitly. */
14496 if (!pending)
14498 int new_count = 0;
14500 FOR_EACH_FRAME (tail, frame)
14502 if (XFRAME (frame)->visible)
14503 new_count++;
14506 if (new_count != number_of_visible_frames)
14507 windows_or_buffers_changed = 52;
14510 /* Change frame size now if a change is pending. */
14511 do_pending_window_change (true);
14513 /* If we just did a pending size change, or have additional
14514 visible frames, or selected_window changed, redisplay again. */
14515 if ((windows_or_buffers_changed && !pending)
14516 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14517 goto retry;
14519 /* Clear the face and image caches.
14521 We used to do this only if consider_all_windows_p. But the cache
14522 needs to be cleared if a timer creates images in the current
14523 buffer (e.g. the test case in Bug#6230). */
14525 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14527 clear_face_cache (false);
14528 clear_face_cache_count = 0;
14531 #ifdef HAVE_WINDOW_SYSTEM
14532 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14534 clear_image_caches (Qnil);
14535 clear_image_cache_count = 0;
14537 #endif /* HAVE_WINDOW_SYSTEM */
14539 end_of_redisplay:
14540 #ifdef HAVE_NS
14541 ns_set_doc_edited ();
14542 #endif
14543 if (interrupt_input && interrupts_deferred)
14544 request_sigio ();
14546 unbind_to (count, Qnil);
14547 RESUME_POLLING;
14550 static void
14551 unwind_redisplay_preserve_echo_area (void)
14553 unblock_buffer_flips ();
14556 /* Redisplay, but leave alone any recent echo area message unless
14557 another message has been requested in its place.
14559 This is useful in situations where you need to redisplay but no
14560 user action has occurred, making it inappropriate for the message
14561 area to be cleared. See tracking_off and
14562 wait_reading_process_output for examples of these situations.
14564 FROM_WHERE is an integer saying from where this function was
14565 called. This is useful for debugging. */
14567 void
14568 redisplay_preserve_echo_area (int from_where)
14570 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14572 block_input ();
14573 ptrdiff_t count = SPECPDL_INDEX ();
14574 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14575 block_buffer_flips ();
14576 unblock_input ();
14578 if (!NILP (echo_area_buffer[1]))
14580 /* We have a previously displayed message, but no current
14581 message. Redisplay the previous message. */
14582 display_last_displayed_message_p = true;
14583 redisplay_internal ();
14584 display_last_displayed_message_p = false;
14586 else
14587 redisplay_internal ();
14589 flush_frame (SELECTED_FRAME ());
14590 unbind_to (count, Qnil);
14594 /* Function registered with record_unwind_protect in redisplay_internal. */
14596 static void
14597 unwind_redisplay (void)
14599 redisplaying_p = false;
14600 unblock_buffer_flips ();
14604 /* Mark the display of leaf window W as accurate or inaccurate.
14605 If ACCURATE_P, mark display of W as accurate.
14606 If !ACCURATE_P, arrange for W to be redisplayed the next
14607 time redisplay_internal is called. */
14609 static void
14610 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14612 struct buffer *b = XBUFFER (w->contents);
14614 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14615 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14616 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14618 if (accurate_p)
14620 b->clip_changed = false;
14621 b->prevent_redisplay_optimizations_p = false;
14622 eassert (buffer_window_count (b) > 0);
14623 /* Resetting b->text->redisplay is problematic!
14624 In order to make it safer to do it here, redisplay_internal must
14625 have copied all b->text->redisplay to their respective windows. */
14626 b->text->redisplay = false;
14628 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14629 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14630 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14631 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14633 w->current_matrix->buffer = b;
14634 w->current_matrix->begv = BUF_BEGV (b);
14635 w->current_matrix->zv = BUF_ZV (b);
14637 w->last_cursor_vpos = w->cursor.vpos;
14638 w->last_cursor_off_p = w->cursor_off_p;
14640 if (w == XWINDOW (selected_window))
14641 w->last_point = BUF_PT (b);
14642 else
14643 w->last_point = marker_position (w->pointm);
14645 w->window_end_valid = true;
14646 w->update_mode_line = false;
14649 w->redisplay = !accurate_p;
14653 /* Mark the display of windows in the window tree rooted at WINDOW as
14654 accurate or inaccurate. If ACCURATE_P, mark display of
14655 windows as accurate. If !ACCURATE_P, arrange for windows to
14656 be redisplayed the next time redisplay_internal is called. */
14658 void
14659 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14661 struct window *w;
14663 for (; !NILP (window); window = w->next)
14665 w = XWINDOW (window);
14666 if (WINDOWP (w->contents))
14667 mark_window_display_accurate (w->contents, accurate_p);
14668 else
14669 mark_window_display_accurate_1 (w, accurate_p);
14672 if (accurate_p)
14673 update_overlay_arrows (1);
14674 else
14675 /* Force a thorough redisplay the next time by setting
14676 last_arrow_position and last_arrow_string to t, which is
14677 unequal to any useful value of Voverlay_arrow_... */
14678 update_overlay_arrows (-1);
14682 /* Return value in display table DP (Lisp_Char_Table *) for character
14683 C. Since a display table doesn't have any parent, we don't have to
14684 follow parent. Do not call this function directly but use the
14685 macro DISP_CHAR_VECTOR. */
14687 Lisp_Object
14688 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14690 Lisp_Object val;
14692 if (ASCII_CHAR_P (c))
14694 val = dp->ascii;
14695 if (SUB_CHAR_TABLE_P (val))
14696 val = XSUB_CHAR_TABLE (val)->contents[c];
14698 else
14700 Lisp_Object table;
14702 XSETCHAR_TABLE (table, dp);
14703 val = char_table_ref (table, c);
14705 if (NILP (val))
14706 val = dp->defalt;
14707 return val;
14710 static int buffer_flip_blocked_depth;
14712 static void
14713 block_buffer_flips (void)
14715 eassert (buffer_flip_blocked_depth >= 0);
14716 buffer_flip_blocked_depth++;
14719 static void
14720 unblock_buffer_flips (void)
14722 eassert (buffer_flip_blocked_depth > 0);
14723 if (--buffer_flip_blocked_depth == 0)
14725 Lisp_Object tail, frame;
14726 block_input ();
14727 FOR_EACH_FRAME (tail, frame)
14729 struct frame *f = XFRAME (frame);
14730 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14731 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14733 unblock_input ();
14737 bool
14738 buffer_flipping_blocked_p (void)
14740 return buffer_flip_blocked_depth > 0;
14744 /***********************************************************************
14745 Window Redisplay
14746 ***********************************************************************/
14748 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14750 static void
14751 redisplay_windows (Lisp_Object window)
14753 while (!NILP (window))
14755 struct window *w = XWINDOW (window);
14757 if (WINDOWP (w->contents))
14758 redisplay_windows (w->contents);
14759 else if (BUFFERP (w->contents))
14761 displayed_buffer = XBUFFER (w->contents);
14762 /* Use list_of_error, not Qerror, so that
14763 we catch only errors and don't run the debugger. */
14764 internal_condition_case_1 (redisplay_window_0, window,
14765 list_of_error,
14766 redisplay_window_error);
14769 window = w->next;
14773 static Lisp_Object
14774 redisplay_window_error (Lisp_Object ignore)
14776 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14777 return Qnil;
14780 static Lisp_Object
14781 redisplay_window_0 (Lisp_Object window)
14783 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14784 redisplay_window (window, false);
14785 return Qnil;
14788 static Lisp_Object
14789 redisplay_window_1 (Lisp_Object window)
14791 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14792 redisplay_window (window, true);
14793 return Qnil;
14797 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14798 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14799 which positions recorded in ROW differ from current buffer
14800 positions.
14802 Return true iff cursor is on this row. */
14804 static bool
14805 set_cursor_from_row (struct window *w, struct glyph_row *row,
14806 struct glyph_matrix *matrix,
14807 ptrdiff_t delta, ptrdiff_t delta_bytes,
14808 int dy, int dvpos)
14810 struct glyph *glyph = row->glyphs[TEXT_AREA];
14811 struct glyph *end = glyph + row->used[TEXT_AREA];
14812 struct glyph *cursor = NULL;
14813 /* The last known character position in row. */
14814 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14815 int x = row->x;
14816 ptrdiff_t pt_old = PT - delta;
14817 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14818 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14819 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14820 /* A glyph beyond the edge of TEXT_AREA which we should never
14821 touch. */
14822 struct glyph *glyphs_end = end;
14823 /* True means we've found a match for cursor position, but that
14824 glyph has the avoid_cursor_p flag set. */
14825 bool match_with_avoid_cursor = false;
14826 /* True means we've seen at least one glyph that came from a
14827 display string. */
14828 bool string_seen = false;
14829 /* Largest and smallest buffer positions seen so far during scan of
14830 glyph row. */
14831 ptrdiff_t bpos_max = pos_before;
14832 ptrdiff_t bpos_min = pos_after;
14833 /* Last buffer position covered by an overlay string with an integer
14834 `cursor' property. */
14835 ptrdiff_t bpos_covered = 0;
14836 /* True means the display string on which to display the cursor
14837 comes from a text property, not from an overlay. */
14838 bool string_from_text_prop = false;
14840 /* Don't even try doing anything if called for a mode-line or
14841 header-line row, since the rest of the code isn't prepared to
14842 deal with such calamities. */
14843 eassert (!row->mode_line_p);
14844 if (row->mode_line_p)
14845 return false;
14847 /* Skip over glyphs not having an object at the start and the end of
14848 the row. These are special glyphs like truncation marks on
14849 terminal frames. */
14850 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14852 if (!row->reversed_p)
14854 while (glyph < end
14855 && NILP (glyph->object)
14856 && glyph->charpos < 0)
14858 x += glyph->pixel_width;
14859 ++glyph;
14861 while (end > glyph
14862 && NILP ((end - 1)->object)
14863 /* CHARPOS is zero for blanks and stretch glyphs
14864 inserted by extend_face_to_end_of_line. */
14865 && (end - 1)->charpos <= 0)
14866 --end;
14867 glyph_before = glyph - 1;
14868 glyph_after = end;
14870 else
14872 struct glyph *g;
14874 /* If the glyph row is reversed, we need to process it from back
14875 to front, so swap the edge pointers. */
14876 glyphs_end = end = glyph - 1;
14877 glyph += row->used[TEXT_AREA] - 1;
14879 while (glyph > end + 1
14880 && NILP (glyph->object)
14881 && glyph->charpos < 0)
14882 --glyph;
14883 if (NILP (glyph->object) && glyph->charpos < 0)
14884 --glyph;
14885 /* By default, in reversed rows we put the cursor on the
14886 rightmost (first in the reading order) glyph. */
14887 for (x = 0, g = end + 1; g < glyph; g++)
14888 x += g->pixel_width;
14889 while (end < glyph
14890 && NILP ((end + 1)->object)
14891 && (end + 1)->charpos <= 0)
14892 ++end;
14893 glyph_before = glyph + 1;
14894 glyph_after = end;
14897 else if (row->reversed_p)
14899 /* In R2L rows that don't display text, put the cursor on the
14900 rightmost glyph. Case in point: an empty last line that is
14901 part of an R2L paragraph. */
14902 cursor = end - 1;
14903 /* Avoid placing the cursor on the last glyph of the row, where
14904 on terminal frames we hold the vertical border between
14905 adjacent windows. */
14906 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14907 && !WINDOW_RIGHTMOST_P (w)
14908 && cursor == row->glyphs[LAST_AREA] - 1)
14909 cursor--;
14910 x = -1; /* will be computed below, at label compute_x */
14913 /* Step 1: Try to find the glyph whose character position
14914 corresponds to point. If that's not possible, find 2 glyphs
14915 whose character positions are the closest to point, one before
14916 point, the other after it. */
14917 if (!row->reversed_p)
14918 while (/* not marched to end of glyph row */
14919 glyph < end
14920 /* glyph was not inserted by redisplay for internal purposes */
14921 && !NILP (glyph->object))
14923 if (BUFFERP (glyph->object))
14925 ptrdiff_t dpos = glyph->charpos - pt_old;
14927 if (glyph->charpos > bpos_max)
14928 bpos_max = glyph->charpos;
14929 if (glyph->charpos < bpos_min)
14930 bpos_min = glyph->charpos;
14931 if (!glyph->avoid_cursor_p)
14933 /* If we hit point, we've found the glyph on which to
14934 display the cursor. */
14935 if (dpos == 0)
14937 match_with_avoid_cursor = false;
14938 break;
14940 /* See if we've found a better approximation to
14941 POS_BEFORE or to POS_AFTER. */
14942 if (0 > dpos && dpos > pos_before - pt_old)
14944 pos_before = glyph->charpos;
14945 glyph_before = glyph;
14947 else if (0 < dpos && dpos < pos_after - pt_old)
14949 pos_after = glyph->charpos;
14950 glyph_after = glyph;
14953 else if (dpos == 0)
14954 match_with_avoid_cursor = true;
14956 else if (STRINGP (glyph->object))
14958 Lisp_Object chprop;
14959 ptrdiff_t glyph_pos = glyph->charpos;
14961 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14962 glyph->object);
14963 if (!NILP (chprop))
14965 /* If the string came from a `display' text property,
14966 look up the buffer position of that property and
14967 use that position to update bpos_max, as if we
14968 actually saw such a position in one of the row's
14969 glyphs. This helps with supporting integer values
14970 of `cursor' property on the display string in
14971 situations where most or all of the row's buffer
14972 text is completely covered by display properties,
14973 so that no glyph with valid buffer positions is
14974 ever seen in the row. */
14975 ptrdiff_t prop_pos =
14976 string_buffer_position_lim (glyph->object, pos_before,
14977 pos_after, false);
14979 if (prop_pos >= pos_before)
14980 bpos_max = prop_pos;
14982 if (INTEGERP (chprop))
14984 bpos_covered = bpos_max + XINT (chprop);
14985 /* If the `cursor' property covers buffer positions up
14986 to and including point, we should display cursor on
14987 this glyph. Note that, if a `cursor' property on one
14988 of the string's characters has an integer value, we
14989 will break out of the loop below _before_ we get to
14990 the position match above. IOW, integer values of
14991 the `cursor' property override the "exact match for
14992 point" strategy of positioning the cursor. */
14993 /* Implementation note: bpos_max == pt_old when, e.g.,
14994 we are in an empty line, where bpos_max is set to
14995 MATRIX_ROW_START_CHARPOS, see above. */
14996 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14998 cursor = glyph;
14999 break;
15003 string_seen = true;
15005 x += glyph->pixel_width;
15006 ++glyph;
15008 else if (glyph > end) /* row is reversed */
15009 while (!NILP (glyph->object))
15011 if (BUFFERP (glyph->object))
15013 ptrdiff_t dpos = glyph->charpos - pt_old;
15015 if (glyph->charpos > bpos_max)
15016 bpos_max = glyph->charpos;
15017 if (glyph->charpos < bpos_min)
15018 bpos_min = glyph->charpos;
15019 if (!glyph->avoid_cursor_p)
15021 if (dpos == 0)
15023 match_with_avoid_cursor = false;
15024 break;
15026 if (0 > dpos && dpos > pos_before - pt_old)
15028 pos_before = glyph->charpos;
15029 glyph_before = glyph;
15031 else if (0 < dpos && dpos < pos_after - pt_old)
15033 pos_after = glyph->charpos;
15034 glyph_after = glyph;
15037 else if (dpos == 0)
15038 match_with_avoid_cursor = true;
15040 else if (STRINGP (glyph->object))
15042 Lisp_Object chprop;
15043 ptrdiff_t glyph_pos = glyph->charpos;
15045 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15046 glyph->object);
15047 if (!NILP (chprop))
15049 ptrdiff_t prop_pos =
15050 string_buffer_position_lim (glyph->object, pos_before,
15051 pos_after, false);
15053 if (prop_pos >= pos_before)
15054 bpos_max = prop_pos;
15056 if (INTEGERP (chprop))
15058 bpos_covered = bpos_max + XINT (chprop);
15059 /* If the `cursor' property covers buffer positions up
15060 to and including point, we should display cursor on
15061 this glyph. */
15062 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15064 cursor = glyph;
15065 break;
15068 string_seen = true;
15070 --glyph;
15071 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15073 x--; /* can't use any pixel_width */
15074 break;
15076 x -= glyph->pixel_width;
15079 /* Step 2: If we didn't find an exact match for point, we need to
15080 look for a proper place to put the cursor among glyphs between
15081 GLYPH_BEFORE and GLYPH_AFTER. */
15082 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15083 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15084 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15086 /* An empty line has a single glyph whose OBJECT is nil and
15087 whose CHARPOS is the position of a newline on that line.
15088 Note that on a TTY, there are more glyphs after that, which
15089 were produced by extend_face_to_end_of_line, but their
15090 CHARPOS is zero or negative. */
15091 bool empty_line_p =
15092 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15093 && NILP (glyph->object) && glyph->charpos > 0
15094 /* On a TTY, continued and truncated rows also have a glyph at
15095 their end whose OBJECT is nil and whose CHARPOS is
15096 positive (the continuation and truncation glyphs), but such
15097 rows are obviously not "empty". */
15098 && !(row->continued_p || row->truncated_on_right_p));
15100 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15102 ptrdiff_t ellipsis_pos;
15104 /* Scan back over the ellipsis glyphs. */
15105 if (!row->reversed_p)
15107 ellipsis_pos = (glyph - 1)->charpos;
15108 while (glyph > row->glyphs[TEXT_AREA]
15109 && (glyph - 1)->charpos == ellipsis_pos)
15110 glyph--, x -= glyph->pixel_width;
15111 /* That loop always goes one position too far, including
15112 the glyph before the ellipsis. So scan forward over
15113 that one. */
15114 x += glyph->pixel_width;
15115 glyph++;
15117 else /* row is reversed */
15119 ellipsis_pos = (glyph + 1)->charpos;
15120 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15121 && (glyph + 1)->charpos == ellipsis_pos)
15122 glyph++, x += glyph->pixel_width;
15123 x -= glyph->pixel_width;
15124 glyph--;
15127 else if (match_with_avoid_cursor)
15129 cursor = glyph_after;
15130 x = -1;
15132 else if (string_seen)
15134 int incr = row->reversed_p ? -1 : +1;
15136 /* Need to find the glyph that came out of a string which is
15137 present at point. That glyph is somewhere between
15138 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15139 positioned between POS_BEFORE and POS_AFTER in the
15140 buffer. */
15141 struct glyph *start, *stop;
15142 ptrdiff_t pos = pos_before;
15144 x = -1;
15146 /* If the row ends in a newline from a display string,
15147 reordering could have moved the glyphs belonging to the
15148 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15149 in this case we extend the search to the last glyph in
15150 the row that was not inserted by redisplay. */
15151 if (row->ends_in_newline_from_string_p)
15153 glyph_after = end;
15154 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15157 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15158 correspond to POS_BEFORE and POS_AFTER, respectively. We
15159 need START and STOP in the order that corresponds to the
15160 row's direction as given by its reversed_p flag. If the
15161 directionality of characters between POS_BEFORE and
15162 POS_AFTER is the opposite of the row's base direction,
15163 these characters will have been reordered for display,
15164 and we need to reverse START and STOP. */
15165 if (!row->reversed_p)
15167 start = min (glyph_before, glyph_after);
15168 stop = max (glyph_before, glyph_after);
15170 else
15172 start = max (glyph_before, glyph_after);
15173 stop = min (glyph_before, glyph_after);
15175 for (glyph = start + incr;
15176 row->reversed_p ? glyph > stop : glyph < stop; )
15179 /* Any glyphs that come from the buffer are here because
15180 of bidi reordering. Skip them, and only pay
15181 attention to glyphs that came from some string. */
15182 if (STRINGP (glyph->object))
15184 Lisp_Object str;
15185 ptrdiff_t tem;
15186 /* If the display property covers the newline, we
15187 need to search for it one position farther. */
15188 ptrdiff_t lim = pos_after
15189 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15191 string_from_text_prop = false;
15192 str = glyph->object;
15193 tem = string_buffer_position_lim (str, pos, lim, false);
15194 if (tem == 0 /* from overlay */
15195 || pos <= tem)
15197 /* If the string from which this glyph came is
15198 found in the buffer at point, or at position
15199 that is closer to point than pos_after, then
15200 we've found the glyph we've been looking for.
15201 If it comes from an overlay (tem == 0), and
15202 it has the `cursor' property on one of its
15203 glyphs, record that glyph as a candidate for
15204 displaying the cursor. (As in the
15205 unidirectional version, we will display the
15206 cursor on the last candidate we find.) */
15207 if (tem == 0
15208 || tem == pt_old
15209 || (tem - pt_old > 0 && tem < pos_after))
15211 /* The glyphs from this string could have
15212 been reordered. Find the one with the
15213 smallest string position. Or there could
15214 be a character in the string with the
15215 `cursor' property, which means display
15216 cursor on that character's glyph. */
15217 ptrdiff_t strpos = glyph->charpos;
15219 if (tem)
15221 cursor = glyph;
15222 string_from_text_prop = true;
15224 for ( ;
15225 (row->reversed_p ? glyph > stop : glyph < stop)
15226 && EQ (glyph->object, str);
15227 glyph += incr)
15229 Lisp_Object cprop;
15230 ptrdiff_t gpos = glyph->charpos;
15232 cprop = Fget_char_property (make_number (gpos),
15233 Qcursor,
15234 glyph->object);
15235 if (!NILP (cprop))
15237 cursor = glyph;
15238 break;
15240 if (tem && glyph->charpos < strpos)
15242 strpos = glyph->charpos;
15243 cursor = glyph;
15247 if (tem == pt_old
15248 || (tem - pt_old > 0 && tem < pos_after))
15249 goto compute_x;
15251 if (tem)
15252 pos = tem + 1; /* don't find previous instances */
15254 /* This string is not what we want; skip all of the
15255 glyphs that came from it. */
15256 while ((row->reversed_p ? glyph > stop : glyph < stop)
15257 && EQ (glyph->object, str))
15258 glyph += incr;
15260 else
15261 glyph += incr;
15264 /* If we reached the end of the line, and END was from a string,
15265 the cursor is not on this line. */
15266 if (cursor == NULL
15267 && (row->reversed_p ? glyph <= end : glyph >= end)
15268 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15269 && STRINGP (end->object)
15270 && row->continued_p)
15271 return false;
15273 /* A truncated row may not include PT among its character positions.
15274 Setting the cursor inside the scroll margin will trigger
15275 recalculation of hscroll in hscroll_window_tree. But if a
15276 display string covers point, defer to the string-handling
15277 code below to figure this out. */
15278 else if (row->truncated_on_left_p && pt_old < bpos_min)
15280 cursor = glyph_before;
15281 x = -1;
15283 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15284 /* Zero-width characters produce no glyphs. */
15285 || (!empty_line_p
15286 && (row->reversed_p
15287 ? glyph_after > glyphs_end
15288 : glyph_after < glyphs_end)))
15290 cursor = glyph_after;
15291 x = -1;
15295 compute_x:
15296 if (cursor != NULL)
15297 glyph = cursor;
15298 else if (glyph == glyphs_end
15299 && pos_before == pos_after
15300 && STRINGP ((row->reversed_p
15301 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15302 : row->glyphs[TEXT_AREA])->object))
15304 /* If all the glyphs of this row came from strings, put the
15305 cursor on the first glyph of the row. This avoids having the
15306 cursor outside of the text area in this very rare and hard
15307 use case. */
15308 glyph =
15309 row->reversed_p
15310 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15311 : row->glyphs[TEXT_AREA];
15313 if (x < 0)
15315 struct glyph *g;
15317 /* Need to compute x that corresponds to GLYPH. */
15318 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15320 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15321 emacs_abort ();
15322 x += g->pixel_width;
15326 /* ROW could be part of a continued line, which, under bidi
15327 reordering, might have other rows whose start and end charpos
15328 occlude point. Only set w->cursor if we found a better
15329 approximation to the cursor position than we have from previously
15330 examined candidate rows belonging to the same continued line. */
15331 if (/* We already have a candidate row. */
15332 w->cursor.vpos >= 0
15333 /* That candidate is not the row we are processing. */
15334 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15335 /* Make sure cursor.vpos specifies a row whose start and end
15336 charpos occlude point, and it is valid candidate for being a
15337 cursor-row. This is because some callers of this function
15338 leave cursor.vpos at the row where the cursor was displayed
15339 during the last redisplay cycle. */
15340 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15341 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15342 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15344 struct glyph *g1
15345 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15347 /* Don't consider glyphs that are outside TEXT_AREA. */
15348 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15349 return false;
15350 /* Keep the candidate whose buffer position is the closest to
15351 point or has the `cursor' property. */
15352 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15353 w->cursor.hpos >= 0
15354 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15355 && ((BUFFERP (g1->object)
15356 && (g1->charpos == pt_old /* An exact match always wins. */
15357 || (BUFFERP (glyph->object)
15358 && eabs (g1->charpos - pt_old)
15359 < eabs (glyph->charpos - pt_old))))
15360 /* Previous candidate is a glyph from a string that has
15361 a non-nil `cursor' property. */
15362 || (STRINGP (g1->object)
15363 && (!NILP (Fget_char_property (make_number (g1->charpos),
15364 Qcursor, g1->object))
15365 /* Previous candidate is from the same display
15366 string as this one, and the display string
15367 came from a text property. */
15368 || (EQ (g1->object, glyph->object)
15369 && string_from_text_prop)
15370 /* this candidate is from newline and its
15371 position is not an exact match */
15372 || (NILP (glyph->object)
15373 && glyph->charpos != pt_old)))))
15374 return false;
15375 /* If this candidate gives an exact match, use that. */
15376 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15377 /* If this candidate is a glyph created for the
15378 terminating newline of a line, and point is on that
15379 newline, it wins because it's an exact match. */
15380 || (!row->continued_p
15381 && NILP (glyph->object)
15382 && glyph->charpos == 0
15383 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15384 /* Otherwise, keep the candidate that comes from a row
15385 spanning less buffer positions. This may win when one or
15386 both candidate positions are on glyphs that came from
15387 display strings, for which we cannot compare buffer
15388 positions. */
15389 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15390 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15391 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15392 return false;
15394 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15395 w->cursor.x = x;
15396 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15397 w->cursor.y = row->y + dy;
15399 if (w == XWINDOW (selected_window))
15401 if (!row->continued_p
15402 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15403 && row->x == 0)
15405 this_line_buffer = XBUFFER (w->contents);
15407 CHARPOS (this_line_start_pos)
15408 = MATRIX_ROW_START_CHARPOS (row) + delta;
15409 BYTEPOS (this_line_start_pos)
15410 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15412 CHARPOS (this_line_end_pos)
15413 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15414 BYTEPOS (this_line_end_pos)
15415 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15417 this_line_y = w->cursor.y;
15418 this_line_pixel_height = row->height;
15419 this_line_vpos = w->cursor.vpos;
15420 this_line_start_x = row->x;
15422 else
15423 CHARPOS (this_line_start_pos) = 0;
15426 return true;
15430 /* Run window scroll functions, if any, for WINDOW with new window
15431 start STARTP. Sets the window start of WINDOW to that position.
15433 We assume that the window's buffer is really current. */
15435 static struct text_pos
15436 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15438 struct window *w = XWINDOW (window);
15439 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15441 eassert (current_buffer == XBUFFER (w->contents));
15443 if (!NILP (Vwindow_scroll_functions))
15445 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15446 make_number (CHARPOS (startp)));
15447 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15448 /* In case the hook functions switch buffers. */
15449 set_buffer_internal (XBUFFER (w->contents));
15452 return startp;
15456 /* Make sure the line containing the cursor is fully visible.
15457 A value of true means there is nothing to be done.
15458 (Either the line is fully visible, or it cannot be made so,
15459 or we cannot tell.)
15461 If FORCE_P, return false even if partial visible cursor row
15462 is higher than window.
15464 If CURRENT_MATRIX_P, use the information from the
15465 window's current glyph matrix; otherwise use the desired glyph
15466 matrix.
15468 A value of false means the caller should do scrolling
15469 as if point had gone off the screen. */
15471 static bool
15472 cursor_row_fully_visible_p (struct window *w, bool force_p,
15473 bool current_matrix_p)
15475 struct glyph_matrix *matrix;
15476 struct glyph_row *row;
15477 int window_height;
15479 if (!make_cursor_line_fully_visible_p)
15480 return true;
15482 /* It's not always possible to find the cursor, e.g, when a window
15483 is full of overlay strings. Don't do anything in that case. */
15484 if (w->cursor.vpos < 0)
15485 return true;
15487 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15488 row = MATRIX_ROW (matrix, w->cursor.vpos);
15490 /* If the cursor row is not partially visible, there's nothing to do. */
15491 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15492 return true;
15494 /* If the row the cursor is in is taller than the window's height,
15495 it's not clear what to do, so do nothing. */
15496 window_height = window_box_height (w);
15497 if (row->height >= window_height)
15499 if (!force_p || MINI_WINDOW_P (w)
15500 || w->vscroll || w->cursor.vpos == 0)
15501 return true;
15503 return false;
15507 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15508 means only WINDOW is redisplayed in redisplay_internal.
15509 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15510 in redisplay_window to bring a partially visible line into view in
15511 the case that only the cursor has moved.
15513 LAST_LINE_MISFIT should be true if we're scrolling because the
15514 last screen line's vertical height extends past the end of the screen.
15516 Value is
15518 1 if scrolling succeeded
15520 0 if scrolling didn't find point.
15522 -1 if new fonts have been loaded so that we must interrupt
15523 redisplay, adjust glyph matrices, and try again. */
15525 enum
15527 SCROLLING_SUCCESS,
15528 SCROLLING_FAILED,
15529 SCROLLING_NEED_LARGER_MATRICES
15532 /* If scroll-conservatively is more than this, never recenter.
15534 If you change this, don't forget to update the doc string of
15535 `scroll-conservatively' and the Emacs manual. */
15536 #define SCROLL_LIMIT 100
15538 static int
15539 try_scrolling (Lisp_Object window, bool just_this_one_p,
15540 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15541 bool temp_scroll_step, bool last_line_misfit)
15543 struct window *w = XWINDOW (window);
15544 struct text_pos pos, startp;
15545 struct it it;
15546 int this_scroll_margin, scroll_max, rc, height;
15547 int dy = 0, amount_to_scroll = 0;
15548 bool scroll_down_p = false;
15549 int extra_scroll_margin_lines = last_line_misfit;
15550 Lisp_Object aggressive;
15551 /* We will never try scrolling more than this number of lines. */
15552 int scroll_limit = SCROLL_LIMIT;
15553 int frame_line_height = default_line_pixel_height (w);
15555 #ifdef GLYPH_DEBUG
15556 debug_method_add (w, "try_scrolling");
15557 #endif
15559 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15561 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15563 /* Force arg_scroll_conservatively to have a reasonable value, to
15564 avoid scrolling too far away with slow move_it_* functions. Note
15565 that the user can supply scroll-conservatively equal to
15566 `most-positive-fixnum', which can be larger than INT_MAX. */
15567 if (arg_scroll_conservatively > scroll_limit)
15569 arg_scroll_conservatively = scroll_limit + 1;
15570 scroll_max = scroll_limit * frame_line_height;
15572 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15573 /* Compute how much we should try to scroll maximally to bring
15574 point into view. */
15575 scroll_max = (max (scroll_step,
15576 max (arg_scroll_conservatively, temp_scroll_step))
15577 * frame_line_height);
15578 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15579 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15580 /* We're trying to scroll because of aggressive scrolling but no
15581 scroll_step is set. Choose an arbitrary one. */
15582 scroll_max = 10 * frame_line_height;
15583 else
15584 scroll_max = 0;
15586 too_near_end:
15588 /* Decide whether to scroll down. */
15589 if (PT > CHARPOS (startp))
15591 int scroll_margin_y;
15593 /* Compute the pixel ypos of the scroll margin, then move IT to
15594 either that ypos or PT, whichever comes first. */
15595 start_display (&it, w, startp);
15596 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15597 - this_scroll_margin
15598 - frame_line_height * extra_scroll_margin_lines;
15599 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15600 (MOVE_TO_POS | MOVE_TO_Y));
15602 if (PT > CHARPOS (it.current.pos))
15604 int y0 = line_bottom_y (&it);
15605 /* Compute how many pixels below window bottom to stop searching
15606 for PT. This avoids costly search for PT that is far away if
15607 the user limited scrolling by a small number of lines, but
15608 always finds PT if scroll_conservatively is set to a large
15609 number, such as most-positive-fixnum. */
15610 int slack = max (scroll_max, 10 * frame_line_height);
15611 int y_to_move = it.last_visible_y + slack;
15613 /* Compute the distance from the scroll margin to PT or to
15614 the scroll limit, whichever comes first. This should
15615 include the height of the cursor line, to make that line
15616 fully visible. */
15617 move_it_to (&it, PT, -1, y_to_move,
15618 -1, MOVE_TO_POS | MOVE_TO_Y);
15619 dy = line_bottom_y (&it) - y0;
15621 if (dy > scroll_max)
15622 return SCROLLING_FAILED;
15624 if (dy > 0)
15625 scroll_down_p = true;
15627 else if (PT == IT_CHARPOS (it)
15628 && IT_CHARPOS (it) < ZV
15629 && it.method == GET_FROM_STRING
15630 && arg_scroll_conservatively > scroll_limit
15631 && it.current_x == 0)
15633 enum move_it_result skip;
15634 int y1 = it.current_y;
15635 int vpos;
15637 /* A before-string that includes newlines and is displayed
15638 on the last visible screen line could fail us under
15639 scroll-conservatively > 100, because we will be unable to
15640 position the cursor on that last visible line. Try to
15641 recover by finding the first screen line that has some
15642 glyphs coming from the buffer text. */
15643 do {
15644 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15645 if (skip != MOVE_NEWLINE_OR_CR
15646 || IT_CHARPOS (it) != PT
15647 || it.method == GET_FROM_BUFFER)
15648 break;
15649 vpos = it.vpos;
15650 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15651 } while (it.vpos > vpos);
15653 dy = it.current_y - y1;
15655 if (dy > scroll_max)
15656 return SCROLLING_FAILED;
15658 if (dy > 0)
15659 scroll_down_p = true;
15663 if (scroll_down_p)
15665 /* Point is in or below the bottom scroll margin, so move the
15666 window start down. If scrolling conservatively, move it just
15667 enough down to make point visible. If scroll_step is set,
15668 move it down by scroll_step. */
15669 if (arg_scroll_conservatively)
15670 amount_to_scroll
15671 = min (max (dy, frame_line_height),
15672 frame_line_height * arg_scroll_conservatively);
15673 else if (scroll_step || temp_scroll_step)
15674 amount_to_scroll = scroll_max;
15675 else
15677 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15678 height = WINDOW_BOX_TEXT_HEIGHT (w);
15679 if (NUMBERP (aggressive))
15681 double float_amount = XFLOATINT (aggressive) * height;
15682 int aggressive_scroll = float_amount;
15683 if (aggressive_scroll == 0 && float_amount > 0)
15684 aggressive_scroll = 1;
15685 /* Don't let point enter the scroll margin near top of
15686 the window. This could happen if the value of
15687 scroll_up_aggressively is too large and there are
15688 non-zero margins, because scroll_up_aggressively
15689 means put point that fraction of window height
15690 _from_the_bottom_margin_. */
15691 if (aggressive_scroll + 2 * this_scroll_margin > height)
15692 aggressive_scroll = height - 2 * this_scroll_margin;
15693 amount_to_scroll = dy + aggressive_scroll;
15697 if (amount_to_scroll <= 0)
15698 return SCROLLING_FAILED;
15700 start_display (&it, w, startp);
15701 if (arg_scroll_conservatively <= scroll_limit)
15702 move_it_vertically (&it, amount_to_scroll);
15703 else
15705 /* Extra precision for users who set scroll-conservatively
15706 to a large number: make sure the amount we scroll
15707 the window start is never less than amount_to_scroll,
15708 which was computed as distance from window bottom to
15709 point. This matters when lines at window top and lines
15710 below window bottom have different height. */
15711 struct it it1;
15712 void *it1data = NULL;
15713 /* We use a temporary it1 because line_bottom_y can modify
15714 its argument, if it moves one line down; see there. */
15715 int start_y;
15717 SAVE_IT (it1, it, it1data);
15718 start_y = line_bottom_y (&it1);
15719 do {
15720 RESTORE_IT (&it, &it, it1data);
15721 move_it_by_lines (&it, 1);
15722 SAVE_IT (it1, it, it1data);
15723 } while (IT_CHARPOS (it) < ZV
15724 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15725 bidi_unshelve_cache (it1data, true);
15728 /* If STARTP is unchanged, move it down another screen line. */
15729 if (IT_CHARPOS (it) == CHARPOS (startp))
15730 move_it_by_lines (&it, 1);
15731 startp = it.current.pos;
15733 else
15735 struct text_pos scroll_margin_pos = startp;
15736 int y_offset = 0;
15738 /* See if point is inside the scroll margin at the top of the
15739 window. */
15740 if (this_scroll_margin)
15742 int y_start;
15744 start_display (&it, w, startp);
15745 y_start = it.current_y;
15746 move_it_vertically (&it, this_scroll_margin);
15747 scroll_margin_pos = it.current.pos;
15748 /* If we didn't move enough before hitting ZV, request
15749 additional amount of scroll, to move point out of the
15750 scroll margin. */
15751 if (IT_CHARPOS (it) == ZV
15752 && it.current_y - y_start < this_scroll_margin)
15753 y_offset = this_scroll_margin - (it.current_y - y_start);
15756 if (PT < CHARPOS (scroll_margin_pos))
15758 /* Point is in the scroll margin at the top of the window or
15759 above what is displayed in the window. */
15760 int y0, y_to_move;
15762 /* Compute the vertical distance from PT to the scroll
15763 margin position. Move as far as scroll_max allows, or
15764 one screenful, or 10 screen lines, whichever is largest.
15765 Give up if distance is greater than scroll_max or if we
15766 didn't reach the scroll margin position. */
15767 SET_TEXT_POS (pos, PT, PT_BYTE);
15768 start_display (&it, w, pos);
15769 y0 = it.current_y;
15770 y_to_move = max (it.last_visible_y,
15771 max (scroll_max, 10 * frame_line_height));
15772 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15773 y_to_move, -1,
15774 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15775 dy = it.current_y - y0;
15776 if (dy > scroll_max
15777 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15778 return SCROLLING_FAILED;
15780 /* Additional scroll for when ZV was too close to point. */
15781 dy += y_offset;
15783 /* Compute new window start. */
15784 start_display (&it, w, startp);
15786 if (arg_scroll_conservatively)
15787 amount_to_scroll = max (dy, frame_line_height
15788 * max (scroll_step, temp_scroll_step));
15789 else if (scroll_step || temp_scroll_step)
15790 amount_to_scroll = scroll_max;
15791 else
15793 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15794 height = WINDOW_BOX_TEXT_HEIGHT (w);
15795 if (NUMBERP (aggressive))
15797 double float_amount = XFLOATINT (aggressive) * height;
15798 int aggressive_scroll = float_amount;
15799 if (aggressive_scroll == 0 && float_amount > 0)
15800 aggressive_scroll = 1;
15801 /* Don't let point enter the scroll margin near
15802 bottom of the window, if the value of
15803 scroll_down_aggressively happens to be too
15804 large. */
15805 if (aggressive_scroll + 2 * this_scroll_margin > height)
15806 aggressive_scroll = height - 2 * this_scroll_margin;
15807 amount_to_scroll = dy + aggressive_scroll;
15811 if (amount_to_scroll <= 0)
15812 return SCROLLING_FAILED;
15814 move_it_vertically_backward (&it, amount_to_scroll);
15815 startp = it.current.pos;
15819 /* Run window scroll functions. */
15820 startp = run_window_scroll_functions (window, startp);
15822 /* Display the window. Give up if new fonts are loaded, or if point
15823 doesn't appear. */
15824 if (!try_window (window, startp, 0))
15825 rc = SCROLLING_NEED_LARGER_MATRICES;
15826 else if (w->cursor.vpos < 0)
15828 clear_glyph_matrix (w->desired_matrix);
15829 rc = SCROLLING_FAILED;
15831 else
15833 /* Maybe forget recorded base line for line number display. */
15834 if (!just_this_one_p
15835 || current_buffer->clip_changed
15836 || BEG_UNCHANGED < CHARPOS (startp))
15837 w->base_line_number = 0;
15839 /* If cursor ends up on a partially visible line,
15840 treat that as being off the bottom of the screen. */
15841 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15842 false)
15843 /* It's possible that the cursor is on the first line of the
15844 buffer, which is partially obscured due to a vscroll
15845 (Bug#7537). In that case, avoid looping forever. */
15846 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15848 clear_glyph_matrix (w->desired_matrix);
15849 ++extra_scroll_margin_lines;
15850 goto too_near_end;
15852 rc = SCROLLING_SUCCESS;
15855 return rc;
15859 /* Compute a suitable window start for window W if display of W starts
15860 on a continuation line. Value is true if a new window start
15861 was computed.
15863 The new window start will be computed, based on W's width, starting
15864 from the start of the continued line. It is the start of the
15865 screen line with the minimum distance from the old start W->start,
15866 which is still before point (otherwise point will definitely not
15867 be visible in the window). */
15869 static bool
15870 compute_window_start_on_continuation_line (struct window *w)
15872 struct text_pos pos, start_pos, pos_before_pt;
15873 bool window_start_changed_p = false;
15875 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15877 /* If window start is on a continuation line... Window start may be
15878 < BEGV in case there's invisible text at the start of the
15879 buffer (M-x rmail, for example). */
15880 if (CHARPOS (start_pos) > BEGV
15881 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15883 struct it it;
15884 struct glyph_row *row;
15886 /* Handle the case that the window start is out of range. */
15887 if (CHARPOS (start_pos) < BEGV)
15888 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15889 else if (CHARPOS (start_pos) > ZV)
15890 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15892 /* Find the start of the continued line. This should be fast
15893 because find_newline is fast (newline cache). */
15894 row = w->desired_matrix->rows + window_wants_header_line (w);
15895 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15896 row, DEFAULT_FACE_ID);
15897 reseat_at_previous_visible_line_start (&it);
15899 /* If the line start is "too far" away from the window start,
15900 say it takes too much time to compute a new window start.
15901 Also, give up if the line start is after point, as in that
15902 case point will not be visible with any window start we
15903 compute. */
15904 if (IT_CHARPOS (it) <= PT
15905 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15906 /* PXW: Do we need upper bounds here? */
15907 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15909 int min_distance, distance;
15911 /* Move forward by display lines to find the new window
15912 start. If window width was enlarged, the new start can
15913 be expected to be > the old start. If window width was
15914 decreased, the new window start will be < the old start.
15915 So, we're looking for the display line start with the
15916 minimum distance from the old window start. */
15917 pos_before_pt = pos = it.current.pos;
15918 min_distance = DISP_INFINITY;
15919 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15920 distance < min_distance)
15922 min_distance = distance;
15923 if (CHARPOS (pos) <= PT)
15924 pos_before_pt = pos;
15925 pos = it.current.pos;
15926 if (it.line_wrap == WORD_WRAP)
15928 /* Under WORD_WRAP, move_it_by_lines is likely to
15929 overshoot and stop not at the first, but the
15930 second character from the left margin. So in
15931 that case, we need a more tight control on the X
15932 coordinate of the iterator than move_it_by_lines
15933 promises in its contract. The method is to first
15934 go to the last (rightmost) visible character of a
15935 line, then move to the leftmost character on the
15936 next line in a separate call. */
15937 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15938 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15939 move_it_to (&it, ZV, 0,
15940 it.current_y + it.max_ascent + it.max_descent, -1,
15941 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15943 else
15944 move_it_by_lines (&it, 1);
15947 /* It makes very little sense to make the new window start
15948 after point, as point won't be visible. If that's what
15949 the loop above finds, fall back on the candidate before
15950 or at point that is closest to the old window start. */
15951 if (CHARPOS (pos) > PT)
15952 pos = pos_before_pt;
15954 /* Set the window start there. */
15955 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15956 window_start_changed_p = true;
15960 return window_start_changed_p;
15964 /* Try cursor movement in case text has not changed in window WINDOW,
15965 with window start STARTP. Value is
15967 CURSOR_MOVEMENT_SUCCESS if successful
15969 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15971 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15972 display. *SCROLL_STEP is set to true, under certain circumstances, if
15973 we want to scroll as if scroll-step were set to 1. See the code.
15975 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15976 which case we have to abort this redisplay, and adjust matrices
15977 first. */
15979 enum
15981 CURSOR_MOVEMENT_SUCCESS,
15982 CURSOR_MOVEMENT_CANNOT_BE_USED,
15983 CURSOR_MOVEMENT_MUST_SCROLL,
15984 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15987 static int
15988 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15989 bool *scroll_step)
15991 struct window *w = XWINDOW (window);
15992 struct frame *f = XFRAME (w->frame);
15993 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15995 #ifdef GLYPH_DEBUG
15996 if (inhibit_try_cursor_movement)
15997 return rc;
15998 #endif
16000 /* Previously, there was a check for Lisp integer in the
16001 if-statement below. Now, this field is converted to
16002 ptrdiff_t, thus zero means invalid position in a buffer. */
16003 eassert (w->last_point > 0);
16004 /* Likewise there was a check whether window_end_vpos is nil or larger
16005 than the window. Now window_end_vpos is int and so never nil, but
16006 let's leave eassert to check whether it fits in the window. */
16007 eassert (!w->window_end_valid
16008 || w->window_end_vpos < w->current_matrix->nrows);
16010 /* Handle case where text has not changed, only point, and it has
16011 not moved off the frame. */
16012 if (/* Point may be in this window. */
16013 PT >= CHARPOS (startp)
16014 /* Selective display hasn't changed. */
16015 && !current_buffer->clip_changed
16016 /* Function force-mode-line-update is used to force a thorough
16017 redisplay. It sets either windows_or_buffers_changed or
16018 update_mode_lines. So don't take a shortcut here for these
16019 cases. */
16020 && !update_mode_lines
16021 && !windows_or_buffers_changed
16022 && !f->cursor_type_changed
16023 && NILP (Vshow_trailing_whitespace)
16024 /* When display-line-numbers is in relative mode, moving point
16025 requires to redraw the entire window. */
16026 && !EQ (Vdisplay_line_numbers, Qrelative)
16027 && !EQ (Vdisplay_line_numbers, Qvisual)
16028 /* When the current line number should be displayed in a
16029 distinct face, moving point cannot be handled in optimized
16030 way as below. */
16031 && !(!NILP (Vdisplay_line_numbers)
16032 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16033 Qline_number_current_line,
16034 w->frame)))
16035 /* This code is not used for mini-buffer for the sake of the case
16036 of redisplaying to replace an echo area message; since in
16037 that case the mini-buffer contents per se are usually
16038 unchanged. This code is of no real use in the mini-buffer
16039 since the handling of this_line_start_pos, etc., in redisplay
16040 handles the same cases. */
16041 && !EQ (window, minibuf_window)
16042 && (FRAME_WINDOW_P (f)
16043 || !overlay_arrow_in_current_buffer_p ()))
16045 int this_scroll_margin, top_scroll_margin;
16046 struct glyph_row *row = NULL;
16048 #ifdef GLYPH_DEBUG
16049 debug_method_add (w, "cursor movement");
16050 #endif
16052 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16054 top_scroll_margin = this_scroll_margin;
16055 if (window_wants_header_line (w))
16056 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16058 /* Start with the row the cursor was displayed during the last
16059 not paused redisplay. Give up if that row is not valid. */
16060 if (w->last_cursor_vpos < 0
16061 || w->last_cursor_vpos >= w->current_matrix->nrows)
16062 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16063 else
16065 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16066 if (row->mode_line_p)
16067 ++row;
16068 if (!row->enabled_p)
16069 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16072 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16074 bool scroll_p = false, must_scroll = false;
16075 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16077 if (PT > w->last_point)
16079 /* Point has moved forward. */
16080 while (MATRIX_ROW_END_CHARPOS (row) < PT
16081 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16083 eassert (row->enabled_p);
16084 ++row;
16087 /* If the end position of a row equals the start
16088 position of the next row, and PT is at that position,
16089 we would rather display cursor in the next line. */
16090 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16091 && MATRIX_ROW_END_CHARPOS (row) == PT
16092 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16093 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16094 && !cursor_row_p (row))
16095 ++row;
16097 /* If within the scroll margin, scroll. Note that
16098 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16099 the next line would be drawn, and that
16100 this_scroll_margin can be zero. */
16101 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16102 || PT > MATRIX_ROW_END_CHARPOS (row)
16103 /* Line is completely visible last line in window
16104 and PT is to be set in the next line. */
16105 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16106 && PT == MATRIX_ROW_END_CHARPOS (row)
16107 && !row->ends_at_zv_p
16108 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16109 scroll_p = true;
16111 else if (PT < w->last_point)
16113 /* Cursor has to be moved backward. Note that PT >=
16114 CHARPOS (startp) because of the outer if-statement. */
16115 while (!row->mode_line_p
16116 && (MATRIX_ROW_START_CHARPOS (row) > PT
16117 || (MATRIX_ROW_START_CHARPOS (row) == PT
16118 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16119 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16120 row > w->current_matrix->rows
16121 && (row-1)->ends_in_newline_from_string_p))))
16122 && (row->y > top_scroll_margin
16123 || CHARPOS (startp) == BEGV))
16125 eassert (row->enabled_p);
16126 --row;
16129 /* Consider the following case: Window starts at BEGV,
16130 there is invisible, intangible text at BEGV, so that
16131 display starts at some point START > BEGV. It can
16132 happen that we are called with PT somewhere between
16133 BEGV and START. Try to handle that case. */
16134 if (row < w->current_matrix->rows
16135 || row->mode_line_p)
16137 row = w->current_matrix->rows;
16138 if (row->mode_line_p)
16139 ++row;
16142 /* Due to newlines in overlay strings, we may have to
16143 skip forward over overlay strings. */
16144 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16145 && MATRIX_ROW_END_CHARPOS (row) == PT
16146 && !cursor_row_p (row))
16147 ++row;
16149 /* If within the scroll margin, scroll. */
16150 if (row->y < top_scroll_margin
16151 && CHARPOS (startp) != BEGV)
16152 scroll_p = true;
16154 else
16156 /* Cursor did not move. So don't scroll even if cursor line
16157 is partially visible, as it was so before. */
16158 rc = CURSOR_MOVEMENT_SUCCESS;
16161 if (PT < MATRIX_ROW_START_CHARPOS (row)
16162 || PT > MATRIX_ROW_END_CHARPOS (row))
16164 /* if PT is not in the glyph row, give up. */
16165 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16166 must_scroll = true;
16168 else if (rc != CURSOR_MOVEMENT_SUCCESS
16169 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16171 struct glyph_row *row1;
16173 /* If rows are bidi-reordered and point moved, back up
16174 until we find a row that does not belong to a
16175 continuation line. This is because we must consider
16176 all rows of a continued line as candidates for the
16177 new cursor positioning, since row start and end
16178 positions change non-linearly with vertical position
16179 in such rows. */
16180 /* FIXME: Revisit this when glyph ``spilling'' in
16181 continuation lines' rows is implemented for
16182 bidi-reordered rows. */
16183 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16184 MATRIX_ROW_CONTINUATION_LINE_P (row);
16185 --row)
16187 /* If we hit the beginning of the displayed portion
16188 without finding the first row of a continued
16189 line, give up. */
16190 if (row <= row1)
16192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16193 break;
16195 eassert (row->enabled_p);
16198 if (must_scroll)
16200 else if (rc != CURSOR_MOVEMENT_SUCCESS
16201 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16202 /* Make sure this isn't a header line by any chance, since
16203 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16204 && !row->mode_line_p
16205 && make_cursor_line_fully_visible_p)
16207 if (PT == MATRIX_ROW_END_CHARPOS (row)
16208 && !row->ends_at_zv_p
16209 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16210 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16211 else if (row->height > window_box_height (w))
16213 /* If we end up in a partially visible line, let's
16214 make it fully visible, except when it's taller
16215 than the window, in which case we can't do much
16216 about it. */
16217 *scroll_step = true;
16218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16220 else
16222 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16223 if (!cursor_row_fully_visible_p (w, false, true))
16224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16225 else
16226 rc = CURSOR_MOVEMENT_SUCCESS;
16229 else if (scroll_p)
16230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16231 else if (rc != CURSOR_MOVEMENT_SUCCESS
16232 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16234 /* With bidi-reordered rows, there could be more than
16235 one candidate row whose start and end positions
16236 occlude point. We need to let set_cursor_from_row
16237 find the best candidate. */
16238 /* FIXME: Revisit this when glyph ``spilling'' in
16239 continuation lines' rows is implemented for
16240 bidi-reordered rows. */
16241 bool rv = false;
16245 bool at_zv_p = false, exact_match_p = false;
16247 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16248 && PT <= MATRIX_ROW_END_CHARPOS (row)
16249 && cursor_row_p (row))
16250 rv |= set_cursor_from_row (w, row, w->current_matrix,
16251 0, 0, 0, 0);
16252 /* As soon as we've found the exact match for point,
16253 or the first suitable row whose ends_at_zv_p flag
16254 is set, we are done. */
16255 if (rv)
16257 at_zv_p = MATRIX_ROW (w->current_matrix,
16258 w->cursor.vpos)->ends_at_zv_p;
16259 if (!at_zv_p
16260 && w->cursor.hpos >= 0
16261 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16262 w->cursor.vpos))
16264 struct glyph_row *candidate =
16265 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16266 struct glyph *g =
16267 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16268 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16270 exact_match_p =
16271 (BUFFERP (g->object) && g->charpos == PT)
16272 || (NILP (g->object)
16273 && (g->charpos == PT
16274 || (g->charpos == 0 && endpos - 1 == PT)));
16276 if (at_zv_p || exact_match_p)
16278 rc = CURSOR_MOVEMENT_SUCCESS;
16279 break;
16282 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16283 break;
16284 ++row;
16286 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16287 || row->continued_p)
16288 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16289 || (MATRIX_ROW_START_CHARPOS (row) == PT
16290 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16291 /* If we didn't find any candidate rows, or exited the
16292 loop before all the candidates were examined, signal
16293 to the caller that this method failed. */
16294 if (rc != CURSOR_MOVEMENT_SUCCESS
16295 && !(rv
16296 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16297 && !row->continued_p))
16298 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16299 else if (rv)
16300 rc = CURSOR_MOVEMENT_SUCCESS;
16302 else
16306 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16308 rc = CURSOR_MOVEMENT_SUCCESS;
16309 break;
16311 ++row;
16313 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16314 && MATRIX_ROW_START_CHARPOS (row) == PT
16315 && cursor_row_p (row));
16320 return rc;
16324 void
16325 set_vertical_scroll_bar (struct window *w)
16327 ptrdiff_t start, end, whole;
16329 /* Calculate the start and end positions for the current window.
16330 At some point, it would be nice to choose between scrollbars
16331 which reflect the whole buffer size, with special markers
16332 indicating narrowing, and scrollbars which reflect only the
16333 visible region.
16335 Note that mini-buffers sometimes aren't displaying any text. */
16336 if (!MINI_WINDOW_P (w)
16337 || (w == XWINDOW (minibuf_window)
16338 && NILP (echo_area_buffer[0])))
16340 struct buffer *buf = XBUFFER (w->contents);
16341 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16342 start = marker_position (w->start) - BUF_BEGV (buf);
16343 /* I don't think this is guaranteed to be right. For the
16344 moment, we'll pretend it is. */
16345 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16347 if (end < start)
16348 end = start;
16349 if (whole < (end - start))
16350 whole = end - start;
16352 else
16353 start = end = whole = 0;
16355 /* Indicate what this scroll bar ought to be displaying now. */
16356 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16357 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16358 (w, end - start, whole, start);
16362 void
16363 set_horizontal_scroll_bar (struct window *w)
16365 int start, end, whole, portion;
16367 if (!MINI_WINDOW_P (w)
16368 || (w == XWINDOW (minibuf_window)
16369 && NILP (echo_area_buffer[0])))
16371 struct buffer *b = XBUFFER (w->contents);
16372 struct buffer *old_buffer = NULL;
16373 struct it it;
16374 struct text_pos startp;
16376 if (b != current_buffer)
16378 old_buffer = current_buffer;
16379 set_buffer_internal (b);
16382 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16383 start_display (&it, w, startp);
16384 it.last_visible_x = INT_MAX;
16385 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16386 MOVE_TO_X | MOVE_TO_Y);
16387 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16388 window_box_height (w), -1,
16389 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16391 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16392 end = start + window_box_width (w, TEXT_AREA);
16393 portion = end - start;
16394 /* After enlarging a horizontally scrolled window such that it
16395 gets at least as wide as the text it contains, make sure that
16396 the thumb doesn't fill the entire scroll bar so we can still
16397 drag it back to see the entire text. */
16398 whole = max (whole, end);
16400 if (it.bidi_p)
16402 Lisp_Object pdir;
16404 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16405 if (EQ (pdir, Qright_to_left))
16407 start = whole - end;
16408 end = start + portion;
16412 if (old_buffer)
16413 set_buffer_internal (old_buffer);
16415 else
16416 start = end = whole = portion = 0;
16418 w->hscroll_whole = whole;
16420 /* Indicate what this scroll bar ought to be displaying now. */
16421 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16422 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16423 (w, portion, whole, start);
16427 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16428 selected_window is redisplayed.
16430 We can return without actually redisplaying the window if fonts has been
16431 changed on window's frame. In that case, redisplay_internal will retry.
16433 As one of the important parts of redisplaying a window, we need to
16434 decide whether the previous window-start position (stored in the
16435 window's w->start marker position) is still valid, and if it isn't,
16436 recompute it. Some details about that:
16438 . The previous window-start could be in a continuation line, in
16439 which case we need to recompute it when the window width
16440 changes. See compute_window_start_on_continuation_line and its
16441 call below.
16443 . The text that changed since last redisplay could include the
16444 previous window-start position. In that case, we try to salvage
16445 what we can from the current glyph matrix by calling
16446 try_scrolling, which see.
16448 . Some Emacs command could force us to use a specific window-start
16449 position by setting the window's force_start flag, or gently
16450 propose doing that by setting the window's optional_new_start
16451 flag. In these cases, we try using the specified start point if
16452 that succeeds (i.e. the window desired matrix is successfully
16453 recomputed, and point location is within the window). In case
16454 of optional_new_start, we first check if the specified start
16455 position is feasible, i.e. if it will allow point to be
16456 displayed in the window. If using the specified start point
16457 fails, e.g., if new fonts are needed to be loaded, we abort the
16458 redisplay cycle and leave it up to the next cycle to figure out
16459 things.
16461 . Note that the window's force_start flag is sometimes set by
16462 redisplay itself, when it decides that the previous window start
16463 point is fine and should be kept. Search for "goto force_start"
16464 below to see the details. Like the values of window-start
16465 specified outside of redisplay, these internally-deduced values
16466 are tested for feasibility, and ignored if found to be
16467 unfeasible.
16469 . Note that the function try_window, used to completely redisplay
16470 a window, accepts the window's start point as its argument.
16471 This is used several times in the redisplay code to control
16472 where the window start will be, according to user options such
16473 as scroll-conservatively, and also to ensure the screen line
16474 showing point will be fully (as opposed to partially) visible on
16475 display. */
16477 static void
16478 redisplay_window (Lisp_Object window, bool just_this_one_p)
16480 struct window *w = XWINDOW (window);
16481 struct frame *f = XFRAME (w->frame);
16482 struct buffer *buffer = XBUFFER (w->contents);
16483 struct buffer *old = current_buffer;
16484 struct text_pos lpoint, opoint, startp;
16485 bool update_mode_line;
16486 int tem;
16487 struct it it;
16488 /* Record it now because it's overwritten. */
16489 bool current_matrix_up_to_date_p = false;
16490 bool used_current_matrix_p = false;
16491 /* This is less strict than current_matrix_up_to_date_p.
16492 It indicates that the buffer contents and narrowing are unchanged. */
16493 bool buffer_unchanged_p = false;
16494 bool temp_scroll_step = false;
16495 ptrdiff_t count = SPECPDL_INDEX ();
16496 int rc;
16497 int centering_position = -1;
16498 bool last_line_misfit = false;
16499 ptrdiff_t beg_unchanged, end_unchanged;
16500 int frame_line_height, margin;
16501 bool use_desired_matrix;
16502 void *itdata = NULL;
16504 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16505 opoint = lpoint;
16507 #ifdef GLYPH_DEBUG
16508 *w->desired_matrix->method = 0;
16509 #endif
16511 if (!just_this_one_p
16512 && REDISPLAY_SOME_P ()
16513 && !w->redisplay
16514 && !w->update_mode_line
16515 && !f->face_change
16516 && !f->redisplay
16517 && !buffer->text->redisplay
16518 && BUF_PT (buffer) == w->last_point)
16519 return;
16521 /* Make sure that both W's markers are valid. */
16522 eassert (XMARKER (w->start)->buffer == buffer);
16523 eassert (XMARKER (w->pointm)->buffer == buffer);
16525 reconsider_clip_changes (w);
16526 frame_line_height = default_line_pixel_height (w);
16527 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16530 /* Has the mode line to be updated? */
16531 update_mode_line = (w->update_mode_line
16532 || update_mode_lines
16533 || buffer->clip_changed
16534 || buffer->prevent_redisplay_optimizations_p);
16536 if (!just_this_one_p)
16537 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16538 cleverly elsewhere. */
16539 w->must_be_updated_p = true;
16541 if (MINI_WINDOW_P (w))
16543 if (w == XWINDOW (echo_area_window)
16544 && !NILP (echo_area_buffer[0]))
16546 if (update_mode_line)
16547 /* We may have to update a tty frame's menu bar or a
16548 tool-bar. Example `M-x C-h C-h C-g'. */
16549 goto finish_menu_bars;
16550 else
16551 /* We've already displayed the echo area glyphs in this window. */
16552 goto finish_scroll_bars;
16554 else if ((w != XWINDOW (minibuf_window)
16555 || minibuf_level == 0)
16556 /* When buffer is nonempty, redisplay window normally. */
16557 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16558 /* Quail displays non-mini buffers in minibuffer window.
16559 In that case, redisplay the window normally. */
16560 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16562 /* W is a mini-buffer window, but it's not active, so clear
16563 it. */
16564 int yb = window_text_bottom_y (w);
16565 struct glyph_row *row;
16566 int y;
16568 for (y = 0, row = w->desired_matrix->rows;
16569 y < yb;
16570 y += row->height, ++row)
16571 blank_row (w, row, y);
16572 goto finish_scroll_bars;
16575 clear_glyph_matrix (w->desired_matrix);
16578 /* Otherwise set up data on this window; select its buffer and point
16579 value. */
16580 /* Really select the buffer, for the sake of buffer-local
16581 variables. */
16582 set_buffer_internal_1 (XBUFFER (w->contents));
16584 current_matrix_up_to_date_p
16585 = (w->window_end_valid
16586 && !current_buffer->clip_changed
16587 && !current_buffer->prevent_redisplay_optimizations_p
16588 && !window_outdated (w)
16589 && !hscrolling_current_line_p (w));
16591 beg_unchanged = BEG_UNCHANGED;
16592 end_unchanged = END_UNCHANGED;
16594 SET_TEXT_POS (opoint, PT, PT_BYTE);
16596 specbind (Qinhibit_point_motion_hooks, Qt);
16598 buffer_unchanged_p
16599 = (w->window_end_valid
16600 && !current_buffer->clip_changed
16601 && !window_outdated (w));
16603 /* When windows_or_buffers_changed is non-zero, we can't rely
16604 on the window end being valid, so set it to zero there. */
16605 if (windows_or_buffers_changed)
16607 /* If window starts on a continuation line, maybe adjust the
16608 window start in case the window's width changed. */
16609 if (XMARKER (w->start)->buffer == current_buffer)
16610 compute_window_start_on_continuation_line (w);
16612 w->window_end_valid = false;
16613 /* If so, we also can't rely on current matrix
16614 and should not fool try_cursor_movement below. */
16615 current_matrix_up_to_date_p = false;
16618 /* Some sanity checks. */
16619 CHECK_WINDOW_END (w);
16620 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16621 emacs_abort ();
16622 if (BYTEPOS (opoint) < CHARPOS (opoint))
16623 emacs_abort ();
16625 if (mode_line_update_needed (w))
16626 update_mode_line = true;
16628 /* Point refers normally to the selected window. For any other
16629 window, set up appropriate value. */
16630 if (!EQ (window, selected_window))
16632 ptrdiff_t new_pt = marker_position (w->pointm);
16633 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16635 if (new_pt < BEGV)
16637 new_pt = BEGV;
16638 new_pt_byte = BEGV_BYTE;
16639 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16641 else if (new_pt > (ZV - 1))
16643 new_pt = ZV;
16644 new_pt_byte = ZV_BYTE;
16645 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16648 /* We don't use SET_PT so that the point-motion hooks don't run. */
16649 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16652 /* If any of the character widths specified in the display table
16653 have changed, invalidate the width run cache. It's true that
16654 this may be a bit late to catch such changes, but the rest of
16655 redisplay goes (non-fatally) haywire when the display table is
16656 changed, so why should we worry about doing any better? */
16657 if (current_buffer->width_run_cache
16658 || (current_buffer->base_buffer
16659 && current_buffer->base_buffer->width_run_cache))
16661 struct Lisp_Char_Table *disptab = buffer_display_table ();
16663 if (! disptab_matches_widthtab
16664 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16666 struct buffer *buf = current_buffer;
16668 if (buf->base_buffer)
16669 buf = buf->base_buffer;
16670 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16671 recompute_width_table (current_buffer, disptab);
16675 /* If window-start is screwed up, choose a new one. */
16676 if (XMARKER (w->start)->buffer != current_buffer)
16677 goto recenter;
16679 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16681 /* If someone specified a new starting point but did not insist,
16682 check whether it can be used. */
16683 if ((w->optional_new_start || window_frozen_p (w))
16684 && CHARPOS (startp) >= BEGV
16685 && CHARPOS (startp) <= ZV)
16687 ptrdiff_t it_charpos;
16689 w->optional_new_start = false;
16690 start_display (&it, w, startp);
16691 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16692 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16693 /* Record IT's position now, since line_bottom_y might change
16694 that. */
16695 it_charpos = IT_CHARPOS (it);
16696 /* Make sure we set the force_start flag only if the cursor row
16697 will be fully visible. Otherwise, the code under force_start
16698 label below will try to move point back into view, which is
16699 not what the code which sets optional_new_start wants. */
16700 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16701 && !w->force_start)
16703 if (it_charpos == PT)
16704 w->force_start = true;
16705 /* IT may overshoot PT if text at PT is invisible. */
16706 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16707 w->force_start = true;
16708 #ifdef GLYPH_DEBUG
16709 if (w->force_start)
16711 if (window_frozen_p (w))
16712 debug_method_add (w, "set force_start from frozen window start");
16713 else
16714 debug_method_add (w, "set force_start from optional_new_start");
16716 #endif
16720 force_start:
16722 /* Handle case where place to start displaying has been specified,
16723 unless the specified location is outside the accessible range. */
16724 if (w->force_start)
16726 /* We set this later on if we have to adjust point. */
16727 int new_vpos = -1;
16729 w->force_start = false;
16730 w->vscroll = 0;
16731 w->window_end_valid = false;
16733 /* Forget any recorded base line for line number display. */
16734 if (!buffer_unchanged_p)
16735 w->base_line_number = 0;
16737 /* Redisplay the mode line. Select the buffer properly for that.
16738 Also, run the hook window-scroll-functions
16739 because we have scrolled. */
16740 /* Note, we do this after clearing force_start because
16741 if there's an error, it is better to forget about force_start
16742 than to get into an infinite loop calling the hook functions
16743 and having them get more errors. */
16744 if (!update_mode_line
16745 || ! NILP (Vwindow_scroll_functions))
16747 update_mode_line = true;
16748 w->update_mode_line = true;
16749 startp = run_window_scroll_functions (window, startp);
16752 if (CHARPOS (startp) < BEGV)
16753 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16754 else if (CHARPOS (startp) > ZV)
16755 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16757 /* Redisplay, then check if cursor has been set during the
16758 redisplay. Give up if new fonts were loaded. */
16759 /* We used to issue a CHECK_MARGINS argument to try_window here,
16760 but this causes scrolling to fail when point begins inside
16761 the scroll margin (bug#148) -- cyd */
16762 if (!try_window (window, startp, 0))
16764 w->force_start = true;
16765 clear_glyph_matrix (w->desired_matrix);
16766 goto need_larger_matrices;
16769 if (w->cursor.vpos < 0)
16771 /* If point does not appear, try to move point so it does
16772 appear. The desired matrix has been built above, so we
16773 can use it here. First see if point is in invisible
16774 text, and if so, move it to the first visible buffer
16775 position past that. */
16776 struct glyph_row *r = NULL;
16777 Lisp_Object invprop =
16778 get_char_property_and_overlay (make_number (PT), Qinvisible,
16779 Qnil, NULL);
16781 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16783 ptrdiff_t alt_pt;
16784 Lisp_Object invprop_end =
16785 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16786 Qnil, Qnil);
16788 if (NATNUMP (invprop_end))
16789 alt_pt = XFASTINT (invprop_end);
16790 else
16791 alt_pt = ZV;
16792 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16793 NULL, 0);
16795 if (r)
16796 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16797 else /* Give up and just move to the middle of the window. */
16798 new_vpos = window_box_height (w) / 2;
16801 if (!cursor_row_fully_visible_p (w, false, false))
16803 /* Point does appear, but on a line partly visible at end of window.
16804 Move it back to a fully-visible line. */
16805 new_vpos = window_box_height (w);
16806 /* But if window_box_height suggests a Y coordinate that is
16807 not less than we already have, that line will clearly not
16808 be fully visible, so give up and scroll the display.
16809 This can happen when the default face uses a font whose
16810 dimensions are different from the frame's default
16811 font. */
16812 if (new_vpos >= w->cursor.y)
16814 w->cursor.vpos = -1;
16815 clear_glyph_matrix (w->desired_matrix);
16816 goto try_to_scroll;
16819 else if (w->cursor.vpos >= 0)
16821 /* Some people insist on not letting point enter the scroll
16822 margin, even though this part handles windows that didn't
16823 scroll at all. */
16824 int pixel_margin = margin * frame_line_height;
16825 bool header_line = window_wants_header_line (w);
16827 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16828 below, which finds the row to move point to, advances by
16829 the Y coordinate of the _next_ row, see the definition of
16830 MATRIX_ROW_BOTTOM_Y. */
16831 if (w->cursor.vpos < margin + header_line)
16833 w->cursor.vpos = -1;
16834 clear_glyph_matrix (w->desired_matrix);
16835 goto try_to_scroll;
16837 else
16839 int window_height = window_box_height (w);
16841 if (header_line)
16842 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16843 if (w->cursor.y >= window_height - pixel_margin)
16845 w->cursor.vpos = -1;
16846 clear_glyph_matrix (w->desired_matrix);
16847 goto try_to_scroll;
16852 /* If we need to move point for either of the above reasons,
16853 now actually do it. */
16854 if (new_vpos >= 0)
16856 struct glyph_row *row;
16858 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16859 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16860 ++row;
16862 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16863 MATRIX_ROW_START_BYTEPOS (row));
16865 if (w != XWINDOW (selected_window))
16866 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16867 else if (current_buffer == old)
16868 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16870 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16872 /* Re-run pre-redisplay-function so it can update the region
16873 according to the new position of point. */
16874 /* Other than the cursor, w's redisplay is done so we can set its
16875 redisplay to false. Also the buffer's redisplay can be set to
16876 false, since propagate_buffer_redisplay should have already
16877 propagated its info to `w' anyway. */
16878 w->redisplay = false;
16879 XBUFFER (w->contents)->text->redisplay = false;
16880 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16882 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16883 || ((EQ (Vdisplay_line_numbers, Qrelative)
16884 || EQ (Vdisplay_line_numbers, Qvisual))
16885 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16887 /* Either pre-redisplay-function made changes (e.g. move
16888 the region), or we moved point in a window that is
16889 under display-line-numbers = relative mode. We need
16890 another round of redisplay. */
16891 clear_glyph_matrix (w->desired_matrix);
16892 if (!try_window (window, startp, 0))
16893 goto need_larger_matrices;
16896 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16898 clear_glyph_matrix (w->desired_matrix);
16899 goto try_to_scroll;
16902 #ifdef GLYPH_DEBUG
16903 debug_method_add (w, "forced window start");
16904 #endif
16905 goto done;
16908 /* Handle case where text has not changed, only point, and it has
16909 not moved off the frame, and we are not retrying after hscroll.
16910 (current_matrix_up_to_date_p is true when retrying.) */
16911 if (current_matrix_up_to_date_p
16912 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16913 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16915 switch (rc)
16917 case CURSOR_MOVEMENT_SUCCESS:
16918 used_current_matrix_p = true;
16919 goto done;
16921 case CURSOR_MOVEMENT_MUST_SCROLL:
16922 goto try_to_scroll;
16924 default:
16925 emacs_abort ();
16928 /* If current starting point was originally the beginning of a line
16929 but no longer is, find a new starting point. */
16930 else if (w->start_at_line_beg
16931 && !(CHARPOS (startp) <= BEGV
16932 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16934 #ifdef GLYPH_DEBUG
16935 debug_method_add (w, "recenter 1");
16936 #endif
16937 goto recenter;
16940 /* Try scrolling with try_window_id. Value is > 0 if update has
16941 been done, it is -1 if we know that the same window start will
16942 not work. It is 0 if unsuccessful for some other reason. */
16943 else if ((tem = try_window_id (w)) != 0)
16945 #ifdef GLYPH_DEBUG
16946 debug_method_add (w, "try_window_id %d", tem);
16947 #endif
16949 if (f->fonts_changed)
16950 goto need_larger_matrices;
16951 if (tem > 0)
16952 goto done;
16954 /* Otherwise try_window_id has returned -1 which means that we
16955 don't want the alternative below this comment to execute. */
16957 else if (CHARPOS (startp) >= BEGV
16958 && CHARPOS (startp) <= ZV
16959 && PT >= CHARPOS (startp)
16960 && (CHARPOS (startp) < ZV
16961 /* Avoid starting at end of buffer. */
16962 || CHARPOS (startp) == BEGV
16963 || !window_outdated (w)))
16965 int d1, d2, d5, d6;
16966 int rtop, rbot;
16968 /* If first window line is a continuation line, and window start
16969 is inside the modified region, but the first change is before
16970 current window start, we must select a new window start.
16972 However, if this is the result of a down-mouse event (e.g. by
16973 extending the mouse-drag-overlay), we don't want to select a
16974 new window start, since that would change the position under
16975 the mouse, resulting in an unwanted mouse-movement rather
16976 than a simple mouse-click. */
16977 if (!w->start_at_line_beg
16978 && NILP (do_mouse_tracking)
16979 && CHARPOS (startp) > BEGV
16980 && CHARPOS (startp) > BEG + beg_unchanged
16981 && CHARPOS (startp) <= Z - end_unchanged
16982 /* Even if w->start_at_line_beg is nil, a new window may
16983 start at a line_beg, since that's how set_buffer_window
16984 sets it. So, we need to check the return value of
16985 compute_window_start_on_continuation_line. (See also
16986 bug#197). */
16987 && XMARKER (w->start)->buffer == current_buffer
16988 && compute_window_start_on_continuation_line (w)
16989 /* It doesn't make sense to force the window start like we
16990 do at label force_start if it is already known that point
16991 will not be fully visible in the resulting window, because
16992 doing so will move point from its correct position
16993 instead of scrolling the window to bring point into view.
16994 See bug#9324. */
16995 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16996 /* A very tall row could need more than the window height,
16997 in which case we accept that it is partially visible. */
16998 && (rtop != 0) == (rbot != 0))
17000 w->force_start = true;
17001 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17002 #ifdef GLYPH_DEBUG
17003 debug_method_add (w, "recomputed window start in continuation line");
17004 #endif
17005 goto force_start;
17008 #ifdef GLYPH_DEBUG
17009 debug_method_add (w, "same window start");
17010 #endif
17012 /* Try to redisplay starting at same place as before.
17013 If point has not moved off frame, accept the results. */
17014 if (!current_matrix_up_to_date_p
17015 /* Don't use try_window_reusing_current_matrix in this case
17016 because a window scroll function can have changed the
17017 buffer. */
17018 || !NILP (Vwindow_scroll_functions)
17019 || MINI_WINDOW_P (w)
17020 || !(used_current_matrix_p
17021 = try_window_reusing_current_matrix (w)))
17023 IF_DEBUG (debug_method_add (w, "1"));
17024 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17025 /* -1 means we need to scroll.
17026 0 means we need new matrices, but fonts_changed
17027 is set in that case, so we will detect it below. */
17028 goto try_to_scroll;
17031 if (f->fonts_changed)
17032 goto need_larger_matrices;
17034 if (w->cursor.vpos >= 0)
17036 if (!just_this_one_p
17037 || current_buffer->clip_changed
17038 || BEG_UNCHANGED < CHARPOS (startp))
17039 /* Forget any recorded base line for line number display. */
17040 w->base_line_number = 0;
17042 if (!cursor_row_fully_visible_p (w, true, false))
17044 clear_glyph_matrix (w->desired_matrix);
17045 last_line_misfit = true;
17047 /* Drop through and scroll. */
17048 else
17049 goto done;
17051 else
17052 clear_glyph_matrix (w->desired_matrix);
17055 try_to_scroll:
17057 /* Redisplay the mode line. Select the buffer properly for that. */
17058 if (!update_mode_line)
17060 update_mode_line = true;
17061 w->update_mode_line = true;
17064 /* Try to scroll by specified few lines. */
17065 if ((scroll_conservatively
17066 || emacs_scroll_step
17067 || temp_scroll_step
17068 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17069 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17070 && CHARPOS (startp) >= BEGV
17071 && CHARPOS (startp) <= ZV)
17073 /* The function returns -1 if new fonts were loaded, 1 if
17074 successful, 0 if not successful. */
17075 int ss = try_scrolling (window, just_this_one_p,
17076 scroll_conservatively,
17077 emacs_scroll_step,
17078 temp_scroll_step, last_line_misfit);
17079 switch (ss)
17081 case SCROLLING_SUCCESS:
17082 goto done;
17084 case SCROLLING_NEED_LARGER_MATRICES:
17085 goto need_larger_matrices;
17087 case SCROLLING_FAILED:
17088 break;
17090 default:
17091 emacs_abort ();
17095 /* Finally, just choose a place to start which positions point
17096 according to user preferences. */
17098 recenter:
17100 #ifdef GLYPH_DEBUG
17101 debug_method_add (w, "recenter");
17102 #endif
17104 /* Forget any previously recorded base line for line number display. */
17105 if (!buffer_unchanged_p)
17106 w->base_line_number = 0;
17108 /* Determine the window start relative to point. */
17109 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17110 it.current_y = it.last_visible_y;
17111 if (centering_position < 0)
17113 ptrdiff_t margin_pos = CHARPOS (startp);
17114 Lisp_Object aggressive;
17115 bool scrolling_up;
17117 /* If there is a scroll margin at the top of the window, find
17118 its character position. */
17119 if (margin
17120 /* Cannot call start_display if startp is not in the
17121 accessible region of the buffer. This can happen when we
17122 have just switched to a different buffer and/or changed
17123 its restriction. In that case, startp is initialized to
17124 the character position 1 (BEGV) because we did not yet
17125 have chance to display the buffer even once. */
17126 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17128 struct it it1;
17129 void *it1data = NULL;
17131 SAVE_IT (it1, it, it1data);
17132 start_display (&it1, w, startp);
17133 move_it_vertically (&it1, margin * frame_line_height);
17134 margin_pos = IT_CHARPOS (it1);
17135 RESTORE_IT (&it, &it, it1data);
17137 scrolling_up = PT > margin_pos;
17138 aggressive =
17139 scrolling_up
17140 ? BVAR (current_buffer, scroll_up_aggressively)
17141 : BVAR (current_buffer, scroll_down_aggressively);
17143 if (!MINI_WINDOW_P (w)
17144 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17146 int pt_offset = 0;
17148 /* Setting scroll-conservatively overrides
17149 scroll-*-aggressively. */
17150 if (!scroll_conservatively && NUMBERP (aggressive))
17152 double float_amount = XFLOATINT (aggressive);
17154 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17155 if (pt_offset == 0 && float_amount > 0)
17156 pt_offset = 1;
17157 if (pt_offset && margin > 0)
17158 margin -= 1;
17160 /* Compute how much to move the window start backward from
17161 point so that point will be displayed where the user
17162 wants it. */
17163 if (scrolling_up)
17165 centering_position = it.last_visible_y;
17166 if (pt_offset)
17167 centering_position -= pt_offset;
17168 centering_position -=
17169 (frame_line_height * (1 + margin + last_line_misfit)
17170 + WINDOW_HEADER_LINE_HEIGHT (w));
17171 /* Don't let point enter the scroll margin near top of
17172 the window. */
17173 if (centering_position < margin * frame_line_height)
17174 centering_position = margin * frame_line_height;
17176 else
17177 centering_position = margin * frame_line_height + pt_offset;
17179 else
17180 /* Set the window start half the height of the window backward
17181 from point. */
17182 centering_position = window_box_height (w) / 2;
17184 move_it_vertically_backward (&it, centering_position);
17186 eassert (IT_CHARPOS (it) >= BEGV);
17188 /* The function move_it_vertically_backward may move over more
17189 than the specified y-distance. If it->w is small, e.g. a
17190 mini-buffer window, we may end up in front of the window's
17191 display area. Start displaying at the start of the line
17192 containing PT in this case. */
17193 if (it.current_y <= 0)
17195 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17196 move_it_vertically_backward (&it, 0);
17197 it.current_y = 0;
17200 it.current_x = it.hpos = 0;
17202 /* Set the window start position here explicitly, to avoid an
17203 infinite loop in case the functions in window-scroll-functions
17204 get errors. */
17205 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17207 /* Run scroll hooks. */
17208 startp = run_window_scroll_functions (window, it.current.pos);
17210 /* We invoke try_window and try_window_reusing_current_matrix below,
17211 and they manipulate the bidi cache. Save and restore the cache
17212 state of our iterator, so we could continue using it after that. */
17213 itdata = bidi_shelve_cache ();
17215 /* Redisplay the window. */
17216 use_desired_matrix = false;
17217 if (!current_matrix_up_to_date_p
17218 || windows_or_buffers_changed
17219 || f->cursor_type_changed
17220 /* Don't use try_window_reusing_current_matrix in this case
17221 because it can have changed the buffer. */
17222 || !NILP (Vwindow_scroll_functions)
17223 || !just_this_one_p
17224 || MINI_WINDOW_P (w)
17225 || !(used_current_matrix_p
17226 = try_window_reusing_current_matrix (w)))
17227 use_desired_matrix = (try_window (window, startp, 0) == 1);
17229 bidi_unshelve_cache (itdata, false);
17231 /* If new fonts have been loaded (due to fontsets), give up. We
17232 have to start a new redisplay since we need to re-adjust glyph
17233 matrices. */
17234 if (f->fonts_changed)
17235 goto need_larger_matrices;
17237 /* If cursor did not appear assume that the middle of the window is
17238 in the first line of the window. Do it again with the next line.
17239 (Imagine a window of height 100, displaying two lines of height
17240 60. Moving back 50 from it->last_visible_y will end in the first
17241 line.) */
17242 if (w->cursor.vpos < 0)
17244 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17246 clear_glyph_matrix (w->desired_matrix);
17247 move_it_by_lines (&it, 1);
17248 try_window (window, it.current.pos, 0);
17250 else if (PT < IT_CHARPOS (it))
17252 clear_glyph_matrix (w->desired_matrix);
17253 move_it_by_lines (&it, -1);
17254 try_window (window, it.current.pos, 0);
17256 else if (scroll_conservatively > SCROLL_LIMIT
17257 && (it.method == GET_FROM_STRING
17258 || overlay_touches_p (IT_CHARPOS (it)))
17259 && IT_CHARPOS (it) < ZV)
17261 /* If the window starts with a before-string that spans more
17262 than one screen line, using that position to display the
17263 window might fail to bring point into the view, because
17264 start_display will always start by displaying the string,
17265 whereas the code above determines where to set w->start
17266 by the buffer position of the place where it takes screen
17267 coordinates. Try to recover by finding the next screen
17268 line that displays buffer text. */
17269 ptrdiff_t pos0 = IT_CHARPOS (it);
17271 clear_glyph_matrix (w->desired_matrix);
17272 do {
17273 move_it_by_lines (&it, 1);
17274 } while (IT_CHARPOS (it) == pos0);
17275 try_window (window, it.current.pos, 0);
17277 else
17279 /* Not much we can do about it. */
17283 /* Consider the following case: Window starts at BEGV, there is
17284 invisible, intangible text at BEGV, so that display starts at
17285 some point START > BEGV. It can happen that we are called with
17286 PT somewhere between BEGV and START. Try to handle that case,
17287 and similar ones. */
17288 if (w->cursor.vpos < 0)
17290 /* Prefer the desired matrix to the current matrix, if possible,
17291 in the fallback calculations below. This is because using
17292 the current matrix might completely goof, e.g. if its first
17293 row is after point. */
17294 struct glyph_matrix *matrix =
17295 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17296 /* First, try locating the proper glyph row for PT. */
17297 struct glyph_row *row =
17298 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17300 /* Sometimes point is at the beginning of invisible text that is
17301 before the 1st character displayed in the row. In that case,
17302 row_containing_pos fails to find the row, because no glyphs
17303 with appropriate buffer positions are present in the row.
17304 Therefore, we next try to find the row which shows the 1st
17305 position after the invisible text. */
17306 if (!row)
17308 Lisp_Object val =
17309 get_char_property_and_overlay (make_number (PT), Qinvisible,
17310 Qnil, NULL);
17312 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17314 ptrdiff_t alt_pos;
17315 Lisp_Object invis_end =
17316 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17317 Qnil, Qnil);
17319 if (NATNUMP (invis_end))
17320 alt_pos = XFASTINT (invis_end);
17321 else
17322 alt_pos = ZV;
17323 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17326 /* Finally, fall back on the first row of the window after the
17327 header line (if any). This is slightly better than not
17328 displaying the cursor at all. */
17329 if (!row)
17331 row = matrix->rows;
17332 if (row->mode_line_p)
17333 ++row;
17335 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17338 if (!cursor_row_fully_visible_p (w, false, false))
17340 /* If vscroll is enabled, disable it and try again. */
17341 if (w->vscroll)
17343 w->vscroll = 0;
17344 clear_glyph_matrix (w->desired_matrix);
17345 goto recenter;
17348 /* Users who set scroll-conservatively to a large number want
17349 point just above/below the scroll margin. If we ended up
17350 with point's row partially visible, move the window start to
17351 make that row fully visible and out of the margin. */
17352 if (scroll_conservatively > SCROLL_LIMIT)
17354 int window_total_lines
17355 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17356 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17358 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17359 clear_glyph_matrix (w->desired_matrix);
17360 if (1 == try_window (window, it.current.pos,
17361 TRY_WINDOW_CHECK_MARGINS))
17362 goto done;
17365 /* If centering point failed to make the whole line visible,
17366 put point at the top instead. That has to make the whole line
17367 visible, if it can be done. */
17368 if (centering_position == 0)
17369 goto done;
17371 clear_glyph_matrix (w->desired_matrix);
17372 centering_position = 0;
17373 goto recenter;
17376 done:
17378 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17379 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17380 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17382 /* Display the mode line, if we must. */
17383 if ((update_mode_line
17384 /* If window not full width, must redo its mode line
17385 if (a) the window to its side is being redone and
17386 (b) we do a frame-based redisplay. This is a consequence
17387 of how inverted lines are drawn in frame-based redisplay. */
17388 || (!just_this_one_p
17389 && !FRAME_WINDOW_P (f)
17390 && !WINDOW_FULL_WIDTH_P (w))
17391 /* Line number to display. */
17392 || w->base_line_pos > 0
17393 /* Column number is displayed and different from the one displayed. */
17394 || (w->column_number_displayed != -1
17395 && (w->column_number_displayed != current_column ())))
17396 /* This means that the window has a mode line. */
17397 && (window_wants_mode_line (w)
17398 || window_wants_header_line (w)))
17401 display_mode_lines (w);
17403 /* If mode line height has changed, arrange for a thorough
17404 immediate redisplay using the correct mode line height. */
17405 if (window_wants_mode_line (w)
17406 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17408 f->fonts_changed = true;
17409 w->mode_line_height = -1;
17410 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17411 = DESIRED_MODE_LINE_HEIGHT (w);
17414 /* If header line height has changed, arrange for a thorough
17415 immediate redisplay using the correct header line height. */
17416 if (window_wants_header_line (w)
17417 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17419 f->fonts_changed = true;
17420 w->header_line_height = -1;
17421 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17422 = DESIRED_HEADER_LINE_HEIGHT (w);
17425 if (f->fonts_changed)
17426 goto need_larger_matrices;
17429 if (!line_number_displayed && w->base_line_pos != -1)
17431 w->base_line_pos = 0;
17432 w->base_line_number = 0;
17435 finish_menu_bars:
17437 /* When we reach a frame's selected window, redo the frame's menu
17438 bar and the frame's title. */
17439 if (update_mode_line
17440 && EQ (FRAME_SELECTED_WINDOW (f), window))
17442 bool redisplay_menu_p;
17444 if (FRAME_WINDOW_P (f))
17446 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17447 || defined (HAVE_NS) || defined (USE_GTK)
17448 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17449 #else
17450 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17451 #endif
17453 else
17454 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17456 if (redisplay_menu_p)
17457 display_menu_bar (w);
17459 #ifdef HAVE_WINDOW_SYSTEM
17460 if (FRAME_WINDOW_P (f))
17462 #if defined (USE_GTK) || defined (HAVE_NS)
17463 if (FRAME_EXTERNAL_TOOL_BAR (f))
17464 redisplay_tool_bar (f);
17465 #else
17466 if (WINDOWP (f->tool_bar_window)
17467 && (FRAME_TOOL_BAR_LINES (f) > 0
17468 || !NILP (Vauto_resize_tool_bars))
17469 && redisplay_tool_bar (f))
17470 ignore_mouse_drag_p = true;
17471 #endif
17473 x_consider_frame_title (w->frame);
17474 #endif
17477 #ifdef HAVE_WINDOW_SYSTEM
17478 if (FRAME_WINDOW_P (f)
17479 && update_window_fringes (w, (just_this_one_p
17480 || (!used_current_matrix_p && !overlay_arrow_seen)
17481 || w->pseudo_window_p)))
17483 update_begin (f);
17484 block_input ();
17485 if (draw_window_fringes (w, true))
17487 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17488 x_draw_right_divider (w);
17489 else
17490 x_draw_vertical_border (w);
17492 unblock_input ();
17493 update_end (f);
17496 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17497 x_draw_bottom_divider (w);
17498 #endif /* HAVE_WINDOW_SYSTEM */
17500 /* We go to this label, with fonts_changed set, if it is
17501 necessary to try again using larger glyph matrices.
17502 We have to redeem the scroll bar even in this case,
17503 because the loop in redisplay_internal expects that. */
17504 need_larger_matrices:
17506 finish_scroll_bars:
17508 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17510 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17511 /* Set the thumb's position and size. */
17512 set_vertical_scroll_bar (w);
17514 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17515 /* Set the thumb's position and size. */
17516 set_horizontal_scroll_bar (w);
17518 /* Note that we actually used the scroll bar attached to this
17519 window, so it shouldn't be deleted at the end of redisplay. */
17520 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17521 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17524 /* Restore current_buffer and value of point in it. The window
17525 update may have changed the buffer, so first make sure `opoint'
17526 is still valid (Bug#6177). */
17527 if (CHARPOS (opoint) < BEGV)
17528 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17529 else if (CHARPOS (opoint) > ZV)
17530 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17531 else
17532 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17534 set_buffer_internal_1 (old);
17535 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17536 shorter. This can be caused by log truncation in *Messages*. */
17537 if (CHARPOS (lpoint) <= ZV)
17538 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17540 unbind_to (count, Qnil);
17544 /* Build the complete desired matrix of WINDOW with a window start
17545 buffer position POS.
17547 Value is 1 if successful. It is zero if fonts were loaded during
17548 redisplay which makes re-adjusting glyph matrices necessary, and -1
17549 if point would appear in the scroll margins.
17550 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17551 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17552 set in FLAGS.) */
17555 try_window (Lisp_Object window, struct text_pos pos, int flags)
17557 struct window *w = XWINDOW (window);
17558 struct it it;
17559 struct glyph_row *last_text_row = NULL;
17560 struct frame *f = XFRAME (w->frame);
17561 int cursor_vpos = w->cursor.vpos;
17563 /* Make POS the new window start. */
17564 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17566 /* Mark cursor position as unknown. No overlay arrow seen. */
17567 w->cursor.vpos = -1;
17568 overlay_arrow_seen = false;
17570 /* Initialize iterator and info to start at POS. */
17571 start_display (&it, w, pos);
17572 it.glyph_row->reversed_p = false;
17574 /* Display all lines of W. */
17575 while (it.current_y < it.last_visible_y)
17577 if (display_line (&it, cursor_vpos))
17578 last_text_row = it.glyph_row - 1;
17579 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17580 return 0;
17583 /* Save the character position of 'it' before we call
17584 'start_display' again. */
17585 ptrdiff_t it_charpos = IT_CHARPOS (it);
17587 /* Don't let the cursor end in the scroll margins. */
17588 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17589 && !MINI_WINDOW_P (w))
17591 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17592 start_display (&it, w, pos);
17594 if ((w->cursor.y >= 0 /* not vscrolled */
17595 && w->cursor.y < this_scroll_margin
17596 && CHARPOS (pos) > BEGV
17597 && it_charpos < ZV)
17598 /* rms: considering make_cursor_line_fully_visible_p here
17599 seems to give wrong results. We don't want to recenter
17600 when the last line is partly visible, we want to allow
17601 that case to be handled in the usual way. */
17602 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17603 - this_scroll_margin - 1))
17605 w->cursor.vpos = -1;
17606 clear_glyph_matrix (w->desired_matrix);
17607 return -1;
17611 /* If bottom moved off end of frame, change mode line percentage. */
17612 if (w->window_end_pos <= 0 && Z != it_charpos)
17613 w->update_mode_line = true;
17615 /* Set window_end_pos to the offset of the last character displayed
17616 on the window from the end of current_buffer. Set
17617 window_end_vpos to its row number. */
17618 if (last_text_row)
17620 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17621 adjust_window_ends (w, last_text_row, false);
17622 eassert
17623 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17624 w->window_end_vpos)));
17626 else
17628 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17629 w->window_end_pos = Z - ZV;
17630 w->window_end_vpos = 0;
17633 /* But that is not valid info until redisplay finishes. */
17634 w->window_end_valid = false;
17635 return 1;
17640 /************************************************************************
17641 Window redisplay reusing current matrix when buffer has not changed
17642 ************************************************************************/
17644 /* Try redisplay of window W showing an unchanged buffer with a
17645 different window start than the last time it was displayed by
17646 reusing its current matrix. Value is true if successful.
17647 W->start is the new window start. */
17649 static bool
17650 try_window_reusing_current_matrix (struct window *w)
17652 struct frame *f = XFRAME (w->frame);
17653 struct glyph_row *bottom_row;
17654 struct it it;
17655 struct run run;
17656 struct text_pos start, new_start;
17657 int nrows_scrolled, i;
17658 struct glyph_row *last_text_row;
17659 struct glyph_row *last_reused_text_row;
17660 struct glyph_row *start_row;
17661 int start_vpos, min_y, max_y;
17663 #ifdef GLYPH_DEBUG
17664 if (inhibit_try_window_reusing)
17665 return false;
17666 #endif
17668 if (/* This function doesn't handle terminal frames. */
17669 !FRAME_WINDOW_P (f)
17670 /* Don't try to reuse the display if windows have been split
17671 or such. */
17672 || windows_or_buffers_changed
17673 || f->cursor_type_changed)
17674 return false;
17676 /* Can't do this if showing trailing whitespace. */
17677 if (!NILP (Vshow_trailing_whitespace))
17678 return false;
17680 /* If top-line visibility has changed, give up. */
17681 if (window_wants_header_line (w)
17682 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17683 return false;
17685 /* Give up if old or new display is scrolled vertically. We could
17686 make this function handle this, but right now it doesn't. */
17687 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17688 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17689 return false;
17691 /* Clear the desired matrix for the display below. */
17692 clear_glyph_matrix (w->desired_matrix);
17694 /* Give up if line numbers are being displayed, because reusing the
17695 current matrix might use the wrong width for line-number
17696 display. */
17697 if (!NILP (Vdisplay_line_numbers))
17698 return false;
17700 /* The variable new_start now holds the new window start. The old
17701 start `start' can be determined from the current matrix. */
17702 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17703 start = start_row->minpos;
17704 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17706 if (CHARPOS (new_start) <= CHARPOS (start))
17708 /* Don't use this method if the display starts with an ellipsis
17709 displayed for invisible text. It's not easy to handle that case
17710 below, and it's certainly not worth the effort since this is
17711 not a frequent case. */
17712 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17713 return false;
17715 IF_DEBUG (debug_method_add (w, "twu1"));
17717 /* Display up to a row that can be reused. The variable
17718 last_text_row is set to the last row displayed that displays
17719 text. Note that it.vpos == 0 if or if not there is a
17720 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17721 start_display (&it, w, new_start);
17722 w->cursor.vpos = -1;
17723 last_text_row = last_reused_text_row = NULL;
17725 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17727 /* If we have reached into the characters in the START row,
17728 that means the line boundaries have changed. So we
17729 can't start copying with the row START. Maybe it will
17730 work to start copying with the following row. */
17731 while (IT_CHARPOS (it) > CHARPOS (start))
17733 /* Advance to the next row as the "start". */
17734 start_row++;
17735 start = start_row->minpos;
17736 /* If there are no more rows to try, or just one, give up. */
17737 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17738 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17739 || CHARPOS (start) == ZV)
17741 clear_glyph_matrix (w->desired_matrix);
17742 return false;
17745 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17747 /* If we have reached alignment, we can copy the rest of the
17748 rows. */
17749 if (IT_CHARPOS (it) == CHARPOS (start)
17750 /* Don't accept "alignment" inside a display vector,
17751 since start_row could have started in the middle of
17752 that same display vector (thus their character
17753 positions match), and we have no way of telling if
17754 that is the case. */
17755 && it.current.dpvec_index < 0)
17756 break;
17758 it.glyph_row->reversed_p = false;
17759 if (display_line (&it, -1))
17760 last_text_row = it.glyph_row - 1;
17764 /* A value of current_y < last_visible_y means that we stopped
17765 at the previous window start, which in turn means that we
17766 have at least one reusable row. */
17767 if (it.current_y < it.last_visible_y)
17769 struct glyph_row *row;
17771 /* IT.vpos always starts from 0; it counts text lines. */
17772 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17774 /* Find PT if not already found in the lines displayed. */
17775 if (w->cursor.vpos < 0)
17777 int dy = it.current_y - start_row->y;
17779 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17780 row = row_containing_pos (w, PT, row, NULL, dy);
17781 if (row)
17782 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17783 dy, nrows_scrolled);
17784 else
17786 clear_glyph_matrix (w->desired_matrix);
17787 return false;
17791 /* Scroll the display. Do it before the current matrix is
17792 changed. The problem here is that update has not yet
17793 run, i.e. part of the current matrix is not up to date.
17794 scroll_run_hook will clear the cursor, and use the
17795 current matrix to get the height of the row the cursor is
17796 in. */
17797 run.current_y = start_row->y;
17798 run.desired_y = it.current_y;
17799 run.height = it.last_visible_y - it.current_y;
17801 if (run.height > 0 && run.current_y != run.desired_y)
17803 update_begin (f);
17804 FRAME_RIF (f)->update_window_begin_hook (w);
17805 FRAME_RIF (f)->clear_window_mouse_face (w);
17806 FRAME_RIF (f)->scroll_run_hook (w, &run);
17807 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17808 update_end (f);
17811 /* Shift current matrix down by nrows_scrolled lines. */
17812 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17813 rotate_matrix (w->current_matrix,
17814 start_vpos,
17815 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17816 nrows_scrolled);
17818 /* Disable lines that must be updated. */
17819 for (i = 0; i < nrows_scrolled; ++i)
17820 (start_row + i)->enabled_p = false;
17822 /* Re-compute Y positions. */
17823 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17824 max_y = it.last_visible_y;
17825 for (row = start_row + nrows_scrolled;
17826 row < bottom_row;
17827 ++row)
17829 row->y = it.current_y;
17830 row->visible_height = row->height;
17832 if (row->y < min_y)
17833 row->visible_height -= min_y - row->y;
17834 if (row->y + row->height > max_y)
17835 row->visible_height -= row->y + row->height - max_y;
17836 if (row->fringe_bitmap_periodic_p)
17837 row->redraw_fringe_bitmaps_p = true;
17839 it.current_y += row->height;
17841 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17842 last_reused_text_row = row;
17843 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17844 break;
17847 /* Disable lines in the current matrix which are now
17848 below the window. */
17849 for (++row; row < bottom_row; ++row)
17850 row->enabled_p = row->mode_line_p = false;
17853 /* Update window_end_pos etc.; last_reused_text_row is the last
17854 reused row from the current matrix containing text, if any.
17855 The value of last_text_row is the last displayed line
17856 containing text. */
17857 if (last_reused_text_row)
17858 adjust_window_ends (w, last_reused_text_row, true);
17859 else if (last_text_row)
17860 adjust_window_ends (w, last_text_row, false);
17861 else
17863 /* This window must be completely empty. */
17864 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17865 w->window_end_pos = Z - ZV;
17866 w->window_end_vpos = 0;
17868 w->window_end_valid = false;
17870 /* Update hint: don't try scrolling again in update_window. */
17871 w->desired_matrix->no_scrolling_p = true;
17873 #ifdef GLYPH_DEBUG
17874 debug_method_add (w, "try_window_reusing_current_matrix 1");
17875 #endif
17876 return true;
17878 else if (CHARPOS (new_start) > CHARPOS (start))
17880 struct glyph_row *pt_row, *row;
17881 struct glyph_row *first_reusable_row;
17882 struct glyph_row *first_row_to_display;
17883 int dy;
17884 int yb = window_text_bottom_y (w);
17886 /* Find the row starting at new_start, if there is one. Don't
17887 reuse a partially visible line at the end. */
17888 first_reusable_row = start_row;
17889 while (first_reusable_row->enabled_p
17890 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17891 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17892 < CHARPOS (new_start)))
17893 ++first_reusable_row;
17895 /* Give up if there is no row to reuse. */
17896 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17897 || !first_reusable_row->enabled_p
17898 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17899 != CHARPOS (new_start)))
17900 return false;
17902 /* We can reuse fully visible rows beginning with
17903 first_reusable_row to the end of the window. Set
17904 first_row_to_display to the first row that cannot be reused.
17905 Set pt_row to the row containing point, if there is any. */
17906 pt_row = NULL;
17907 for (first_row_to_display = first_reusable_row;
17908 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17909 ++first_row_to_display)
17911 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17912 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17913 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17914 && first_row_to_display->ends_at_zv_p
17915 && pt_row == NULL)))
17916 pt_row = first_row_to_display;
17919 /* Start displaying at the start of first_row_to_display. */
17920 eassert (first_row_to_display->y < yb);
17921 init_to_row_start (&it, w, first_row_to_display);
17923 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17924 - start_vpos);
17925 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17926 - nrows_scrolled);
17927 it.current_y = (first_row_to_display->y - first_reusable_row->y
17928 + WINDOW_HEADER_LINE_HEIGHT (w));
17930 /* Display lines beginning with first_row_to_display in the
17931 desired matrix. Set last_text_row to the last row displayed
17932 that displays text. */
17933 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17934 if (pt_row == NULL)
17935 w->cursor.vpos = -1;
17936 last_text_row = NULL;
17937 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17938 if (display_line (&it, w->cursor.vpos))
17939 last_text_row = it.glyph_row - 1;
17941 /* If point is in a reused row, adjust y and vpos of the cursor
17942 position. */
17943 if (pt_row)
17945 w->cursor.vpos -= nrows_scrolled;
17946 w->cursor.y -= first_reusable_row->y - start_row->y;
17949 /* Give up if point isn't in a row displayed or reused. (This
17950 also handles the case where w->cursor.vpos < nrows_scrolled
17951 after the calls to display_line, which can happen with scroll
17952 margins. See bug#1295.) */
17953 if (w->cursor.vpos < 0)
17955 clear_glyph_matrix (w->desired_matrix);
17956 return false;
17959 /* Scroll the display. */
17960 run.current_y = first_reusable_row->y;
17961 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17962 run.height = it.last_visible_y - run.current_y;
17963 dy = run.current_y - run.desired_y;
17965 if (run.height)
17967 update_begin (f);
17968 FRAME_RIF (f)->update_window_begin_hook (w);
17969 FRAME_RIF (f)->clear_window_mouse_face (w);
17970 FRAME_RIF (f)->scroll_run_hook (w, &run);
17971 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17972 update_end (f);
17975 /* Adjust Y positions of reused rows. */
17976 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17977 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17978 max_y = it.last_visible_y;
17979 for (row = first_reusable_row; row < first_row_to_display; ++row)
17981 row->y -= dy;
17982 row->visible_height = row->height;
17983 if (row->y < min_y)
17984 row->visible_height -= min_y - row->y;
17985 if (row->y + row->height > max_y)
17986 row->visible_height -= row->y + row->height - max_y;
17987 if (row->fringe_bitmap_periodic_p)
17988 row->redraw_fringe_bitmaps_p = true;
17991 /* Scroll the current matrix. */
17992 eassert (nrows_scrolled > 0);
17993 rotate_matrix (w->current_matrix,
17994 start_vpos,
17995 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17996 -nrows_scrolled);
17998 /* Disable rows not reused. */
17999 for (row -= nrows_scrolled; row < bottom_row; ++row)
18000 row->enabled_p = false;
18002 /* Point may have moved to a different line, so we cannot assume that
18003 the previous cursor position is valid; locate the correct row. */
18004 if (pt_row)
18006 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18007 row < bottom_row
18008 && PT >= MATRIX_ROW_END_CHARPOS (row)
18009 && !row->ends_at_zv_p;
18010 row++)
18012 w->cursor.vpos++;
18013 w->cursor.y = row->y;
18015 if (row < bottom_row)
18017 /* Can't simply scan the row for point with
18018 bidi-reordered glyph rows. Let set_cursor_from_row
18019 figure out where to put the cursor, and if it fails,
18020 give up. */
18021 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18023 if (!set_cursor_from_row (w, row, w->current_matrix,
18024 0, 0, 0, 0))
18026 clear_glyph_matrix (w->desired_matrix);
18027 return false;
18030 else
18032 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18033 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18035 for (; glyph < end
18036 && (!BUFFERP (glyph->object)
18037 || glyph->charpos < PT);
18038 glyph++)
18040 w->cursor.hpos++;
18041 w->cursor.x += glyph->pixel_width;
18047 /* Adjust window end. A null value of last_text_row means that
18048 the window end is in reused rows which in turn means that
18049 only its vpos can have changed. */
18050 if (last_text_row)
18051 adjust_window_ends (w, last_text_row, false);
18052 else
18053 w->window_end_vpos -= nrows_scrolled;
18055 w->window_end_valid = false;
18056 w->desired_matrix->no_scrolling_p = true;
18058 #ifdef GLYPH_DEBUG
18059 debug_method_add (w, "try_window_reusing_current_matrix 2");
18060 #endif
18061 return true;
18064 return false;
18069 /************************************************************************
18070 Window redisplay reusing current matrix when buffer has changed
18071 ************************************************************************/
18073 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18074 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18075 ptrdiff_t *, ptrdiff_t *);
18076 static struct glyph_row *
18077 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18078 struct glyph_row *);
18081 /* Return the last row in MATRIX displaying text. If row START is
18082 non-null, start searching with that row. IT gives the dimensions
18083 of the display. Value is null if matrix is empty; otherwise it is
18084 a pointer to the row found. */
18086 static struct glyph_row *
18087 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18088 struct glyph_row *start)
18090 struct glyph_row *row, *row_found;
18092 /* Set row_found to the last row in IT->w's current matrix
18093 displaying text. The loop looks funny but think of partially
18094 visible lines. */
18095 row_found = NULL;
18096 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18097 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18099 eassert (row->enabled_p);
18100 row_found = row;
18101 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18102 break;
18103 ++row;
18106 return row_found;
18110 /* Return the last row in the current matrix of W that is not affected
18111 by changes at the start of current_buffer that occurred since W's
18112 current matrix was built. Value is null if no such row exists.
18114 BEG_UNCHANGED us the number of characters unchanged at the start of
18115 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18116 first changed character in current_buffer. Characters at positions <
18117 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18118 when the current matrix was built. */
18120 static struct glyph_row *
18121 find_last_unchanged_at_beg_row (struct window *w)
18123 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18124 struct glyph_row *row;
18125 struct glyph_row *row_found = NULL;
18126 int yb = window_text_bottom_y (w);
18128 /* Find the last row displaying unchanged text. */
18129 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18130 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18131 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18132 ++row)
18134 if (/* If row ends before first_changed_pos, it is unchanged,
18135 except in some case. */
18136 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18137 /* When row ends in ZV and we write at ZV it is not
18138 unchanged. */
18139 && !row->ends_at_zv_p
18140 /* When first_changed_pos is the end of a continued line,
18141 row is not unchanged because it may be no longer
18142 continued. */
18143 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18144 && (row->continued_p
18145 || row->exact_window_width_line_p))
18146 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18147 needs to be recomputed, so don't consider this row as
18148 unchanged. This happens when the last line was
18149 bidi-reordered and was killed immediately before this
18150 redisplay cycle. In that case, ROW->end stores the
18151 buffer position of the first visual-order character of
18152 the killed text, which is now beyond ZV. */
18153 && CHARPOS (row->end.pos) <= ZV)
18154 row_found = row;
18156 /* Stop if last visible row. */
18157 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18158 break;
18161 return row_found;
18165 /* Find the first glyph row in the current matrix of W that is not
18166 affected by changes at the end of current_buffer since the
18167 time W's current matrix was built.
18169 Return in *DELTA the number of chars by which buffer positions in
18170 unchanged text at the end of current_buffer must be adjusted.
18172 Return in *DELTA_BYTES the corresponding number of bytes.
18174 Value is null if no such row exists, i.e. all rows are affected by
18175 changes. */
18177 static struct glyph_row *
18178 find_first_unchanged_at_end_row (struct window *w,
18179 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18181 struct glyph_row *row;
18182 struct glyph_row *row_found = NULL;
18184 *delta = *delta_bytes = 0;
18186 /* Display must not have been paused, otherwise the current matrix
18187 is not up to date. */
18188 eassert (w->window_end_valid);
18190 /* A value of window_end_pos >= END_UNCHANGED means that the window
18191 end is in the range of changed text. If so, there is no
18192 unchanged row at the end of W's current matrix. */
18193 if (w->window_end_pos >= END_UNCHANGED)
18194 return NULL;
18196 /* Set row to the last row in W's current matrix displaying text. */
18197 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18199 /* If matrix is entirely empty, no unchanged row exists. */
18200 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18202 /* The value of row is the last glyph row in the matrix having a
18203 meaningful buffer position in it. The end position of row
18204 corresponds to window_end_pos. This allows us to translate
18205 buffer positions in the current matrix to current buffer
18206 positions for characters not in changed text. */
18207 ptrdiff_t Z_old =
18208 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18209 ptrdiff_t Z_BYTE_old =
18210 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18211 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18212 struct glyph_row *first_text_row
18213 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18215 *delta = Z - Z_old;
18216 *delta_bytes = Z_BYTE - Z_BYTE_old;
18218 /* Set last_unchanged_pos to the buffer position of the last
18219 character in the buffer that has not been changed. Z is the
18220 index + 1 of the last character in current_buffer, i.e. by
18221 subtracting END_UNCHANGED we get the index of the last
18222 unchanged character, and we have to add BEG to get its buffer
18223 position. */
18224 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18225 last_unchanged_pos_old = last_unchanged_pos - *delta;
18227 /* Search backward from ROW for a row displaying a line that
18228 starts at a minimum position >= last_unchanged_pos_old. */
18229 for (; row > first_text_row; --row)
18231 /* This used to abort, but it can happen.
18232 It is ok to just stop the search instead here. KFS. */
18233 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18234 break;
18236 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18237 row_found = row;
18241 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18243 return row_found;
18247 /* Make sure that glyph rows in the current matrix of window W
18248 reference the same glyph memory as corresponding rows in the
18249 frame's frame matrix. This function is called after scrolling W's
18250 current matrix on a terminal frame in try_window_id and
18251 try_window_reusing_current_matrix. */
18253 static void
18254 sync_frame_with_window_matrix_rows (struct window *w)
18256 struct frame *f = XFRAME (w->frame);
18257 struct glyph_row *window_row, *window_row_end, *frame_row;
18259 /* Preconditions: W must be a leaf window and full-width. Its frame
18260 must have a frame matrix. */
18261 eassert (BUFFERP (w->contents));
18262 eassert (WINDOW_FULL_WIDTH_P (w));
18263 eassert (!FRAME_WINDOW_P (f));
18265 /* If W is a full-width window, glyph pointers in W's current matrix
18266 have, by definition, to be the same as glyph pointers in the
18267 corresponding frame matrix. Note that frame matrices have no
18268 marginal areas (see build_frame_matrix). */
18269 window_row = w->current_matrix->rows;
18270 window_row_end = window_row + w->current_matrix->nrows;
18271 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18272 while (window_row < window_row_end)
18274 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18275 struct glyph *end = window_row->glyphs[LAST_AREA];
18277 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18278 frame_row->glyphs[TEXT_AREA] = start;
18279 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18280 frame_row->glyphs[LAST_AREA] = end;
18282 /* Disable frame rows whose corresponding window rows have
18283 been disabled in try_window_id. */
18284 if (!window_row->enabled_p)
18285 frame_row->enabled_p = false;
18287 ++window_row, ++frame_row;
18292 /* Find the glyph row in window W containing CHARPOS. Consider all
18293 rows between START and END (not inclusive). END null means search
18294 all rows to the end of the display area of W. Value is the row
18295 containing CHARPOS or null. */
18297 struct glyph_row *
18298 row_containing_pos (struct window *w, ptrdiff_t charpos,
18299 struct glyph_row *start, struct glyph_row *end, int dy)
18301 struct glyph_row *row = start;
18302 struct glyph_row *best_row = NULL;
18303 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18304 int last_y;
18306 /* If we happen to start on a header-line, skip that. */
18307 if (row->mode_line_p)
18308 ++row;
18310 if ((end && row >= end) || !row->enabled_p)
18311 return NULL;
18313 last_y = window_text_bottom_y (w) - dy;
18315 while (true)
18317 /* Give up if we have gone too far. */
18318 if ((end && row >= end) || !row->enabled_p)
18319 return NULL;
18320 /* This formerly returned if they were equal.
18321 I think that both quantities are of a "last plus one" type;
18322 if so, when they are equal, the row is within the screen. -- rms. */
18323 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18324 return NULL;
18326 /* If it is in this row, return this row. */
18327 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18328 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18329 /* The end position of a row equals the start
18330 position of the next row. If CHARPOS is there, we
18331 would rather consider it displayed in the next
18332 line, except when this line ends in ZV. */
18333 && !row_for_charpos_p (row, charpos)))
18334 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18336 struct glyph *g;
18338 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18339 || (!best_row && !row->continued_p))
18340 return row;
18341 /* In bidi-reordered rows, there could be several rows whose
18342 edges surround CHARPOS, all of these rows belonging to
18343 the same continued line. We need to find the row which
18344 fits CHARPOS the best. */
18345 for (g = row->glyphs[TEXT_AREA];
18346 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18347 g++)
18349 if (!STRINGP (g->object))
18351 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18353 mindif = eabs (g->charpos - charpos);
18354 best_row = row;
18355 /* Exact match always wins. */
18356 if (mindif == 0)
18357 return best_row;
18362 else if (best_row && !row->continued_p)
18363 return best_row;
18364 ++row;
18369 /* Try to redisplay window W by reusing its existing display. W's
18370 current matrix must be up to date when this function is called,
18371 i.e., window_end_valid must be true.
18373 Value is
18375 >= 1 if successful, i.e. display has been updated
18376 specifically:
18377 1 means the changes were in front of a newline that precedes
18378 the window start, and the whole current matrix was reused
18379 2 means the changes were after the last position displayed
18380 in the window, and the whole current matrix was reused
18381 3 means portions of the current matrix were reused, while
18382 some of the screen lines were redrawn
18383 -1 if redisplay with same window start is known not to succeed
18384 0 if otherwise unsuccessful
18386 The following steps are performed:
18388 1. Find the last row in the current matrix of W that is not
18389 affected by changes at the start of current_buffer. If no such row
18390 is found, give up.
18392 2. Find the first row in W's current matrix that is not affected by
18393 changes at the end of current_buffer. Maybe there is no such row.
18395 3. Display lines beginning with the row + 1 found in step 1 to the
18396 row found in step 2 or, if step 2 didn't find a row, to the end of
18397 the window.
18399 4. If cursor is not known to appear on the window, give up.
18401 5. If display stopped at the row found in step 2, scroll the
18402 display and current matrix as needed.
18404 6. Maybe display some lines at the end of W, if we must. This can
18405 happen under various circumstances, like a partially visible line
18406 becoming fully visible, or because newly displayed lines are displayed
18407 in smaller font sizes.
18409 7. Update W's window end information. */
18411 static int
18412 try_window_id (struct window *w)
18414 struct frame *f = XFRAME (w->frame);
18415 struct glyph_matrix *current_matrix = w->current_matrix;
18416 struct glyph_matrix *desired_matrix = w->desired_matrix;
18417 struct glyph_row *last_unchanged_at_beg_row;
18418 struct glyph_row *first_unchanged_at_end_row;
18419 struct glyph_row *row;
18420 struct glyph_row *bottom_row;
18421 int bottom_vpos;
18422 struct it it;
18423 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18424 int dvpos, dy;
18425 struct text_pos start_pos;
18426 struct run run;
18427 int first_unchanged_at_end_vpos = 0;
18428 struct glyph_row *last_text_row, *last_text_row_at_end;
18429 struct text_pos start;
18430 ptrdiff_t first_changed_charpos, last_changed_charpos;
18432 #ifdef GLYPH_DEBUG
18433 if (inhibit_try_window_id)
18434 return 0;
18435 #endif
18437 /* This is handy for debugging. */
18438 #if false
18439 #define GIVE_UP(X) \
18440 do { \
18441 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18442 return 0; \
18443 } while (false)
18444 #else
18445 #define GIVE_UP(X) return 0
18446 #endif
18448 SET_TEXT_POS_FROM_MARKER (start, w->start);
18450 /* Don't use this for mini-windows because these can show
18451 messages and mini-buffers, and we don't handle that here. */
18452 if (MINI_WINDOW_P (w))
18453 GIVE_UP (1);
18455 /* This flag is used to prevent redisplay optimizations. */
18456 if (windows_or_buffers_changed || f->cursor_type_changed)
18457 GIVE_UP (2);
18459 /* This function's optimizations cannot be used if overlays have
18460 changed in the buffer displayed by the window, so give up if they
18461 have. */
18462 if (w->last_overlay_modified != OVERLAY_MODIFF)
18463 GIVE_UP (200);
18465 /* Verify that narrowing has not changed.
18466 Also verify that we were not told to prevent redisplay optimizations.
18467 It would be nice to further
18468 reduce the number of cases where this prevents try_window_id. */
18469 if (current_buffer->clip_changed
18470 || current_buffer->prevent_redisplay_optimizations_p)
18471 GIVE_UP (3);
18473 /* Window must either use window-based redisplay or be full width. */
18474 if (!FRAME_WINDOW_P (f)
18475 && (!FRAME_LINE_INS_DEL_OK (f)
18476 || !WINDOW_FULL_WIDTH_P (w)))
18477 GIVE_UP (4);
18479 /* Give up if point is known NOT to appear in W. */
18480 if (PT < CHARPOS (start))
18481 GIVE_UP (5);
18483 /* Another way to prevent redisplay optimizations. */
18484 if (w->last_modified == 0)
18485 GIVE_UP (6);
18487 /* Verify that window is not hscrolled. */
18488 if (w->hscroll != 0)
18489 GIVE_UP (7);
18491 /* Verify that display wasn't paused. */
18492 if (!w->window_end_valid)
18493 GIVE_UP (8);
18495 /* Likewise if highlighting trailing whitespace. */
18496 if (!NILP (Vshow_trailing_whitespace))
18497 GIVE_UP (11);
18499 /* Can't use this if overlay arrow position and/or string have
18500 changed. */
18501 if (overlay_arrows_changed_p (false))
18502 GIVE_UP (12);
18504 /* When word-wrap is on, adding a space to the first word of a
18505 wrapped line can change the wrap position, altering the line
18506 above it. It might be worthwhile to handle this more
18507 intelligently, but for now just redisplay from scratch. */
18508 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18509 GIVE_UP (21);
18511 /* Under bidi reordering, adding or deleting a character in the
18512 beginning of a paragraph, before the first strong directional
18513 character, can change the base direction of the paragraph (unless
18514 the buffer specifies a fixed paragraph direction), which will
18515 require redisplaying the whole paragraph. It might be worthwhile
18516 to find the paragraph limits and widen the range of redisplayed
18517 lines to that, but for now just give up this optimization and
18518 redisplay from scratch. */
18519 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18520 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18521 GIVE_UP (22);
18523 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18524 to that variable require thorough redisplay. */
18525 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18526 GIVE_UP (23);
18528 /* Give up if display-line-numbers is in relative mode, or when the
18529 current line's number needs to be displayed in a distinct face. */
18530 if (EQ (Vdisplay_line_numbers, Qrelative)
18531 || EQ (Vdisplay_line_numbers, Qvisual)
18532 || (!NILP (Vdisplay_line_numbers)
18533 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18534 Qline_number_current_line,
18535 w->frame))))
18536 GIVE_UP (24);
18538 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18539 only if buffer has really changed. The reason is that the gap is
18540 initially at Z for freshly visited files. The code below would
18541 set end_unchanged to 0 in that case. */
18542 if (MODIFF > SAVE_MODIFF
18543 /* This seems to happen sometimes after saving a buffer. */
18544 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18546 if (GPT - BEG < BEG_UNCHANGED)
18547 BEG_UNCHANGED = GPT - BEG;
18548 if (Z - GPT < END_UNCHANGED)
18549 END_UNCHANGED = Z - GPT;
18552 /* The position of the first and last character that has been changed. */
18553 first_changed_charpos = BEG + BEG_UNCHANGED;
18554 last_changed_charpos = Z - END_UNCHANGED;
18556 /* If window starts after a line end, and the last change is in
18557 front of that newline, then changes don't affect the display.
18558 This case happens with stealth-fontification. Note that although
18559 the display is unchanged, glyph positions in the matrix have to
18560 be adjusted, of course. */
18561 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18562 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18563 && ((last_changed_charpos < CHARPOS (start)
18564 && CHARPOS (start) == BEGV)
18565 || (last_changed_charpos < CHARPOS (start) - 1
18566 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18568 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18569 struct glyph_row *r0;
18571 /* Compute how many chars/bytes have been added to or removed
18572 from the buffer. */
18573 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18574 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18575 Z_delta = Z - Z_old;
18576 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18578 /* Give up if PT is not in the window. Note that it already has
18579 been checked at the start of try_window_id that PT is not in
18580 front of the window start. */
18581 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18582 GIVE_UP (13);
18584 /* If window start is unchanged, we can reuse the whole matrix
18585 as is, after adjusting glyph positions. No need to compute
18586 the window end again, since its offset from Z hasn't changed. */
18587 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18588 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18589 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18590 /* PT must not be in a partially visible line. */
18591 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18592 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18594 /* Adjust positions in the glyph matrix. */
18595 if (Z_delta || Z_delta_bytes)
18597 struct glyph_row *r1
18598 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18599 increment_matrix_positions (w->current_matrix,
18600 MATRIX_ROW_VPOS (r0, current_matrix),
18601 MATRIX_ROW_VPOS (r1, current_matrix),
18602 Z_delta, Z_delta_bytes);
18605 /* Set the cursor. */
18606 row = row_containing_pos (w, PT, r0, NULL, 0);
18607 if (row)
18608 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18609 return 1;
18613 /* Handle the case that changes are all below what is displayed in
18614 the window, and that PT is in the window. This shortcut cannot
18615 be taken if ZV is visible in the window, and text has been added
18616 there that is visible in the window. */
18617 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18618 /* ZV is not visible in the window, or there are no
18619 changes at ZV, actually. */
18620 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18621 || first_changed_charpos == last_changed_charpos))
18623 struct glyph_row *r0;
18625 /* Give up if PT is not in the window. Note that it already has
18626 been checked at the start of try_window_id that PT is not in
18627 front of the window start. */
18628 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18629 GIVE_UP (14);
18631 /* If window start is unchanged, we can reuse the whole matrix
18632 as is, without changing glyph positions since no text has
18633 been added/removed in front of the window end. */
18634 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18635 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18636 /* PT must not be in a partially visible line. */
18637 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18638 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18640 /* We have to compute the window end anew since text
18641 could have been added/removed after it. */
18642 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18643 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18645 /* Set the cursor. */
18646 row = row_containing_pos (w, PT, r0, NULL, 0);
18647 if (row)
18648 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18649 return 2;
18653 /* Give up if window start is in the changed area.
18655 The condition used to read
18657 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18659 but why that was tested escapes me at the moment. */
18660 if (CHARPOS (start) >= first_changed_charpos
18661 && CHARPOS (start) <= last_changed_charpos)
18662 GIVE_UP (15);
18664 /* Check that window start agrees with the start of the first glyph
18665 row in its current matrix. Check this after we know the window
18666 start is not in changed text, otherwise positions would not be
18667 comparable. */
18668 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18669 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18670 GIVE_UP (16);
18672 /* Give up if the window ends in strings. Overlay strings
18673 at the end are difficult to handle, so don't try. */
18674 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18675 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18676 GIVE_UP (20);
18678 /* Compute the position at which we have to start displaying new
18679 lines. Some of the lines at the top of the window might be
18680 reusable because they are not displaying changed text. Find the
18681 last row in W's current matrix not affected by changes at the
18682 start of current_buffer. Value is null if changes start in the
18683 first line of window. */
18684 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18685 if (last_unchanged_at_beg_row)
18687 /* Avoid starting to display in the middle of a character, a TAB
18688 for instance. This is easier than to set up the iterator
18689 exactly, and it's not a frequent case, so the additional
18690 effort wouldn't really pay off. */
18691 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18692 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18693 && last_unchanged_at_beg_row > w->current_matrix->rows)
18694 --last_unchanged_at_beg_row;
18696 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18697 GIVE_UP (17);
18699 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18700 GIVE_UP (18);
18701 start_pos = it.current.pos;
18703 /* Start displaying new lines in the desired matrix at the same
18704 vpos we would use in the current matrix, i.e. below
18705 last_unchanged_at_beg_row. */
18706 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18707 current_matrix);
18708 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18709 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18711 eassert (it.hpos == 0 && it.current_x == 0);
18713 else
18715 /* There are no reusable lines at the start of the window.
18716 Start displaying in the first text line. */
18717 start_display (&it, w, start);
18718 it.vpos = it.first_vpos;
18719 start_pos = it.current.pos;
18722 /* Find the first row that is not affected by changes at the end of
18723 the buffer. Value will be null if there is no unchanged row, in
18724 which case we must redisplay to the end of the window. delta
18725 will be set to the value by which buffer positions beginning with
18726 first_unchanged_at_end_row have to be adjusted due to text
18727 changes. */
18728 first_unchanged_at_end_row
18729 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18730 IF_DEBUG (debug_delta = delta);
18731 IF_DEBUG (debug_delta_bytes = delta_bytes);
18733 /* Set stop_pos to the buffer position up to which we will have to
18734 display new lines. If first_unchanged_at_end_row != NULL, this
18735 is the buffer position of the start of the line displayed in that
18736 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18737 that we don't stop at a buffer position. */
18738 stop_pos = 0;
18739 if (first_unchanged_at_end_row)
18741 eassert (last_unchanged_at_beg_row == NULL
18742 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18744 /* If this is a continuation line, move forward to the next one
18745 that isn't. Changes in lines above affect this line.
18746 Caution: this may move first_unchanged_at_end_row to a row
18747 not displaying text. */
18748 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18749 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18750 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18751 < it.last_visible_y))
18752 ++first_unchanged_at_end_row;
18754 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18755 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18756 >= it.last_visible_y))
18757 first_unchanged_at_end_row = NULL;
18758 else
18760 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18761 + delta);
18762 first_unchanged_at_end_vpos
18763 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18764 eassert (stop_pos >= Z - END_UNCHANGED);
18767 else if (last_unchanged_at_beg_row == NULL)
18768 GIVE_UP (19);
18771 #ifdef GLYPH_DEBUG
18773 /* Either there is no unchanged row at the end, or the one we have
18774 now displays text. This is a necessary condition for the window
18775 end pos calculation at the end of this function. */
18776 eassert (first_unchanged_at_end_row == NULL
18777 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18779 debug_last_unchanged_at_beg_vpos
18780 = (last_unchanged_at_beg_row
18781 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18782 : -1);
18783 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18785 #endif /* GLYPH_DEBUG */
18788 /* Display new lines. Set last_text_row to the last new line
18789 displayed which has text on it, i.e. might end up as being the
18790 line where the window_end_vpos is. */
18791 w->cursor.vpos = -1;
18792 last_text_row = NULL;
18793 overlay_arrow_seen = false;
18794 if (it.current_y < it.last_visible_y
18795 && !f->fonts_changed
18796 && (first_unchanged_at_end_row == NULL
18797 || IT_CHARPOS (it) < stop_pos))
18798 it.glyph_row->reversed_p = false;
18799 while (it.current_y < it.last_visible_y
18800 && !f->fonts_changed
18801 && (first_unchanged_at_end_row == NULL
18802 || IT_CHARPOS (it) < stop_pos))
18804 if (display_line (&it, -1))
18805 last_text_row = it.glyph_row - 1;
18808 if (f->fonts_changed)
18809 return -1;
18811 /* The redisplay iterations in display_line above could have
18812 triggered font-lock, which could have done something that
18813 invalidates IT->w window's end-point information, on which we
18814 rely below. E.g., one package, which will remain unnamed, used
18815 to install a font-lock-fontify-region-function that called
18816 bury-buffer, whose side effect is to switch the buffer displayed
18817 by IT->w, and that predictably resets IT->w's window_end_valid
18818 flag, which we already tested at the entry to this function.
18819 Amply punish such packages/modes by giving up on this
18820 optimization in those cases. */
18821 if (!w->window_end_valid)
18823 clear_glyph_matrix (w->desired_matrix);
18824 return -1;
18827 /* Compute differences in buffer positions, y-positions etc. for
18828 lines reused at the bottom of the window. Compute what we can
18829 scroll. */
18830 if (first_unchanged_at_end_row
18831 /* No lines reused because we displayed everything up to the
18832 bottom of the window. */
18833 && it.current_y < it.last_visible_y)
18835 dvpos = (it.vpos
18836 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18837 current_matrix));
18838 dy = it.current_y - first_unchanged_at_end_row->y;
18839 run.current_y = first_unchanged_at_end_row->y;
18840 run.desired_y = run.current_y + dy;
18841 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18843 else
18845 delta = delta_bytes = dvpos = dy
18846 = run.current_y = run.desired_y = run.height = 0;
18847 first_unchanged_at_end_row = NULL;
18849 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18852 /* Find the cursor if not already found. We have to decide whether
18853 PT will appear on this window (it sometimes doesn't, but this is
18854 not a very frequent case.) This decision has to be made before
18855 the current matrix is altered. A value of cursor.vpos < 0 means
18856 that PT is either in one of the lines beginning at
18857 first_unchanged_at_end_row or below the window. Don't care for
18858 lines that might be displayed later at the window end; as
18859 mentioned, this is not a frequent case. */
18860 if (w->cursor.vpos < 0)
18862 /* Cursor in unchanged rows at the top? */
18863 if (PT < CHARPOS (start_pos)
18864 && last_unchanged_at_beg_row)
18866 row = row_containing_pos (w, PT,
18867 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18868 last_unchanged_at_beg_row + 1, 0);
18869 if (row)
18870 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18873 /* Start from first_unchanged_at_end_row looking for PT. */
18874 else if (first_unchanged_at_end_row)
18876 row = row_containing_pos (w, PT - delta,
18877 first_unchanged_at_end_row, NULL, 0);
18878 if (row)
18879 set_cursor_from_row (w, row, w->current_matrix, delta,
18880 delta_bytes, dy, dvpos);
18883 /* Give up if cursor was not found. */
18884 if (w->cursor.vpos < 0)
18886 clear_glyph_matrix (w->desired_matrix);
18887 return -1;
18891 /* Don't let the cursor end in the scroll margins. */
18893 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18894 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18896 if ((w->cursor.y < this_scroll_margin
18897 && CHARPOS (start) > BEGV)
18898 /* Old redisplay didn't take scroll margin into account at the bottom,
18899 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18900 || (w->cursor.y + (make_cursor_line_fully_visible_p
18901 ? cursor_height + this_scroll_margin
18902 : 1)) > it.last_visible_y)
18904 w->cursor.vpos = -1;
18905 clear_glyph_matrix (w->desired_matrix);
18906 return -1;
18910 /* Scroll the display. Do it before changing the current matrix so
18911 that xterm.c doesn't get confused about where the cursor glyph is
18912 found. */
18913 if (dy && run.height)
18915 update_begin (f);
18917 if (FRAME_WINDOW_P (f))
18919 FRAME_RIF (f)->update_window_begin_hook (w);
18920 FRAME_RIF (f)->clear_window_mouse_face (w);
18921 FRAME_RIF (f)->scroll_run_hook (w, &run);
18922 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18924 else
18926 /* Terminal frame. In this case, dvpos gives the number of
18927 lines to scroll by; dvpos < 0 means scroll up. */
18928 int from_vpos
18929 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18930 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18931 int end = (WINDOW_TOP_EDGE_LINE (w)
18932 + window_wants_header_line (w)
18933 + window_internal_height (w));
18935 #if defined (HAVE_GPM) || defined (MSDOS)
18936 x_clear_window_mouse_face (w);
18937 #endif
18938 /* Perform the operation on the screen. */
18939 if (dvpos > 0)
18941 /* Scroll last_unchanged_at_beg_row to the end of the
18942 window down dvpos lines. */
18943 set_terminal_window (f, end);
18945 /* On dumb terminals delete dvpos lines at the end
18946 before inserting dvpos empty lines. */
18947 if (!FRAME_SCROLL_REGION_OK (f))
18948 ins_del_lines (f, end - dvpos, -dvpos);
18950 /* Insert dvpos empty lines in front of
18951 last_unchanged_at_beg_row. */
18952 ins_del_lines (f, from, dvpos);
18954 else if (dvpos < 0)
18956 /* Scroll up last_unchanged_at_beg_vpos to the end of
18957 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18958 set_terminal_window (f, end);
18960 /* Delete dvpos lines in front of
18961 last_unchanged_at_beg_vpos. ins_del_lines will set
18962 the cursor to the given vpos and emit |dvpos| delete
18963 line sequences. */
18964 ins_del_lines (f, from + dvpos, dvpos);
18966 /* On a dumb terminal insert dvpos empty lines at the
18967 end. */
18968 if (!FRAME_SCROLL_REGION_OK (f))
18969 ins_del_lines (f, end + dvpos, -dvpos);
18972 set_terminal_window (f, 0);
18975 update_end (f);
18978 /* Shift reused rows of the current matrix to the right position.
18979 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18980 text. */
18981 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18982 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18983 if (dvpos < 0)
18985 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18986 bottom_vpos, dvpos);
18987 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18988 bottom_vpos);
18990 else if (dvpos > 0)
18992 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18993 bottom_vpos, dvpos);
18994 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18995 first_unchanged_at_end_vpos + dvpos);
18998 /* For frame-based redisplay, make sure that current frame and window
18999 matrix are in sync with respect to glyph memory. */
19000 if (!FRAME_WINDOW_P (f))
19001 sync_frame_with_window_matrix_rows (w);
19003 /* Adjust buffer positions in reused rows. */
19004 if (delta || delta_bytes)
19005 increment_matrix_positions (current_matrix,
19006 first_unchanged_at_end_vpos + dvpos,
19007 bottom_vpos, delta, delta_bytes);
19009 /* Adjust Y positions. */
19010 if (dy)
19011 shift_glyph_matrix (w, current_matrix,
19012 first_unchanged_at_end_vpos + dvpos,
19013 bottom_vpos, dy);
19015 if (first_unchanged_at_end_row)
19017 first_unchanged_at_end_row += dvpos;
19018 if (first_unchanged_at_end_row->y >= it.last_visible_y
19019 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19020 first_unchanged_at_end_row = NULL;
19023 /* If scrolling up, there may be some lines to display at the end of
19024 the window. */
19025 last_text_row_at_end = NULL;
19026 if (dy < 0)
19028 /* Scrolling up can leave for example a partially visible line
19029 at the end of the window to be redisplayed. */
19030 /* Set last_row to the glyph row in the current matrix where the
19031 window end line is found. It has been moved up or down in
19032 the matrix by dvpos. */
19033 int last_vpos = w->window_end_vpos + dvpos;
19034 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19036 /* If last_row is the window end line, it should display text. */
19037 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19039 /* If window end line was partially visible before, begin
19040 displaying at that line. Otherwise begin displaying with the
19041 line following it. */
19042 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19044 init_to_row_start (&it, w, last_row);
19045 it.vpos = last_vpos;
19046 it.current_y = last_row->y;
19048 else
19050 init_to_row_end (&it, w, last_row);
19051 it.vpos = 1 + last_vpos;
19052 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19053 ++last_row;
19056 /* We may start in a continuation line. If so, we have to
19057 get the right continuation_lines_width and current_x. */
19058 it.continuation_lines_width = last_row->continuation_lines_width;
19059 it.hpos = it.current_x = 0;
19061 /* Display the rest of the lines at the window end. */
19062 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19063 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19065 /* Is it always sure that the display agrees with lines in
19066 the current matrix? I don't think so, so we mark rows
19067 displayed invalid in the current matrix by setting their
19068 enabled_p flag to false. */
19069 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19070 if (display_line (&it, w->cursor.vpos))
19071 last_text_row_at_end = it.glyph_row - 1;
19075 /* Update window_end_pos and window_end_vpos. */
19076 if (first_unchanged_at_end_row && !last_text_row_at_end)
19078 /* Window end line if one of the preserved rows from the current
19079 matrix. Set row to the last row displaying text in current
19080 matrix starting at first_unchanged_at_end_row, after
19081 scrolling. */
19082 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19083 row = find_last_row_displaying_text (w->current_matrix, &it,
19084 first_unchanged_at_end_row);
19085 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19086 adjust_window_ends (w, row, true);
19087 eassert (w->window_end_bytepos >= 0);
19088 IF_DEBUG (debug_method_add (w, "A"));
19090 else if (last_text_row_at_end)
19092 adjust_window_ends (w, last_text_row_at_end, false);
19093 eassert (w->window_end_bytepos >= 0);
19094 IF_DEBUG (debug_method_add (w, "B"));
19096 else if (last_text_row)
19098 /* We have displayed either to the end of the window or at the
19099 end of the window, i.e. the last row with text is to be found
19100 in the desired matrix. */
19101 adjust_window_ends (w, last_text_row, false);
19102 eassert (w->window_end_bytepos >= 0);
19104 else if (first_unchanged_at_end_row == NULL
19105 && last_text_row == NULL
19106 && last_text_row_at_end == NULL)
19108 /* Displayed to end of window, but no line containing text was
19109 displayed. Lines were deleted at the end of the window. */
19110 bool first_vpos = window_wants_header_line (w);
19111 int vpos = w->window_end_vpos;
19112 struct glyph_row *current_row = current_matrix->rows + vpos;
19113 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19115 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19117 eassert (first_vpos <= vpos);
19118 if (desired_row->enabled_p)
19120 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19121 row = desired_row;
19123 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19124 row = current_row;
19127 w->window_end_vpos = vpos + 1;
19128 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19129 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19130 eassert (w->window_end_bytepos >= 0);
19131 IF_DEBUG (debug_method_add (w, "C"));
19133 else
19134 emacs_abort ();
19136 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19137 debug_end_vpos = w->window_end_vpos));
19139 /* Record that display has not been completed. */
19140 w->window_end_valid = false;
19141 w->desired_matrix->no_scrolling_p = true;
19142 return 3;
19144 #undef GIVE_UP
19149 /***********************************************************************
19150 More debugging support
19151 ***********************************************************************/
19153 #ifdef GLYPH_DEBUG
19155 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19156 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19157 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19160 /* Dump the contents of glyph matrix MATRIX on stderr.
19162 GLYPHS 0 means don't show glyph contents.
19163 GLYPHS 1 means show glyphs in short form
19164 GLYPHS > 1 means show glyphs in long form. */
19166 void
19167 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19169 int i;
19170 for (i = 0; i < matrix->nrows; ++i)
19171 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19175 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19176 the glyph row and area where the glyph comes from. */
19178 void
19179 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19181 if (glyph->type == CHAR_GLYPH
19182 || glyph->type == GLYPHLESS_GLYPH)
19184 fprintf (stderr,
19185 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19186 glyph - row->glyphs[TEXT_AREA],
19187 (glyph->type == CHAR_GLYPH
19188 ? 'C'
19189 : 'G'),
19190 glyph->charpos,
19191 (BUFFERP (glyph->object)
19192 ? 'B'
19193 : (STRINGP (glyph->object)
19194 ? 'S'
19195 : (NILP (glyph->object)
19196 ? '0'
19197 : '-'))),
19198 glyph->pixel_width,
19199 glyph->u.ch,
19200 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19201 ? (int) glyph->u.ch
19202 : '.'),
19203 glyph->face_id,
19204 glyph->left_box_line_p,
19205 glyph->right_box_line_p);
19207 else if (glyph->type == STRETCH_GLYPH)
19209 fprintf (stderr,
19210 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19211 glyph - row->glyphs[TEXT_AREA],
19212 'S',
19213 glyph->charpos,
19214 (BUFFERP (glyph->object)
19215 ? 'B'
19216 : (STRINGP (glyph->object)
19217 ? 'S'
19218 : (NILP (glyph->object)
19219 ? '0'
19220 : '-'))),
19221 glyph->pixel_width,
19223 ' ',
19224 glyph->face_id,
19225 glyph->left_box_line_p,
19226 glyph->right_box_line_p);
19228 else if (glyph->type == IMAGE_GLYPH)
19230 fprintf (stderr,
19231 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19232 glyph - row->glyphs[TEXT_AREA],
19233 'I',
19234 glyph->charpos,
19235 (BUFFERP (glyph->object)
19236 ? 'B'
19237 : (STRINGP (glyph->object)
19238 ? 'S'
19239 : (NILP (glyph->object)
19240 ? '0'
19241 : '-'))),
19242 glyph->pixel_width,
19243 (unsigned int) glyph->u.img_id,
19244 '.',
19245 glyph->face_id,
19246 glyph->left_box_line_p,
19247 glyph->right_box_line_p);
19249 else if (glyph->type == COMPOSITE_GLYPH)
19251 fprintf (stderr,
19252 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19253 glyph - row->glyphs[TEXT_AREA],
19254 '+',
19255 glyph->charpos,
19256 (BUFFERP (glyph->object)
19257 ? 'B'
19258 : (STRINGP (glyph->object)
19259 ? 'S'
19260 : (NILP (glyph->object)
19261 ? '0'
19262 : '-'))),
19263 glyph->pixel_width,
19264 (unsigned int) glyph->u.cmp.id);
19265 if (glyph->u.cmp.automatic)
19266 fprintf (stderr,
19267 "[%d-%d]",
19268 glyph->slice.cmp.from, glyph->slice.cmp.to);
19269 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19270 glyph->face_id,
19271 glyph->left_box_line_p,
19272 glyph->right_box_line_p);
19274 else if (glyph->type == XWIDGET_GLYPH)
19276 #ifndef HAVE_XWIDGETS
19277 eassume (false);
19278 #else
19279 fprintf (stderr,
19280 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19281 glyph - row->glyphs[TEXT_AREA],
19282 'X',
19283 glyph->charpos,
19284 (BUFFERP (glyph->object)
19285 ? 'B'
19286 : (STRINGP (glyph->object)
19287 ? 'S'
19288 : '-')),
19289 glyph->pixel_width,
19290 glyph->u.xwidget,
19291 '.',
19292 glyph->face_id,
19293 glyph->left_box_line_p,
19294 glyph->right_box_line_p);
19295 #endif
19300 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19301 GLYPHS 0 means don't show glyph contents.
19302 GLYPHS 1 means show glyphs in short form
19303 GLYPHS > 1 means show glyphs in long form. */
19305 void
19306 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19308 if (glyphs != 1)
19310 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19311 fprintf (stderr, "==============================================================================\n");
19313 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19314 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19315 vpos,
19316 MATRIX_ROW_START_CHARPOS (row),
19317 MATRIX_ROW_END_CHARPOS (row),
19318 row->used[TEXT_AREA],
19319 row->contains_overlapping_glyphs_p,
19320 row->enabled_p,
19321 row->truncated_on_left_p,
19322 row->truncated_on_right_p,
19323 row->continued_p,
19324 MATRIX_ROW_CONTINUATION_LINE_P (row),
19325 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19326 row->ends_at_zv_p,
19327 row->fill_line_p,
19328 row->ends_in_middle_of_char_p,
19329 row->starts_in_middle_of_char_p,
19330 row->mouse_face_p,
19331 row->x,
19332 row->y,
19333 row->pixel_width,
19334 row->height,
19335 row->visible_height,
19336 row->ascent,
19337 row->phys_ascent);
19338 /* The next 3 lines should align to "Start" in the header. */
19339 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19340 row->end.overlay_string_index,
19341 row->continuation_lines_width);
19342 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19343 CHARPOS (row->start.string_pos),
19344 CHARPOS (row->end.string_pos));
19345 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19346 row->end.dpvec_index);
19349 if (glyphs > 1)
19351 int area;
19353 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19355 struct glyph *glyph = row->glyphs[area];
19356 struct glyph *glyph_end = glyph + row->used[area];
19358 /* Glyph for a line end in text. */
19359 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19360 ++glyph_end;
19362 if (glyph < glyph_end)
19363 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19365 for (; glyph < glyph_end; ++glyph)
19366 dump_glyph (row, glyph, area);
19369 else if (glyphs == 1)
19371 int area;
19372 char s[SHRT_MAX + 4];
19374 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19376 int i;
19378 for (i = 0; i < row->used[area]; ++i)
19380 struct glyph *glyph = row->glyphs[area] + i;
19381 if (i == row->used[area] - 1
19382 && area == TEXT_AREA
19383 && NILP (glyph->object)
19384 && glyph->type == CHAR_GLYPH
19385 && glyph->u.ch == ' ')
19387 strcpy (&s[i], "[\\n]");
19388 i += 4;
19390 else if (glyph->type == CHAR_GLYPH
19391 && glyph->u.ch < 0x80
19392 && glyph->u.ch >= ' ')
19393 s[i] = glyph->u.ch;
19394 else
19395 s[i] = '.';
19398 s[i] = '\0';
19399 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19405 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19406 Sdump_glyph_matrix, 0, 1, "p",
19407 doc: /* Dump the current matrix of the selected window to stderr.
19408 Shows contents of glyph row structures. With non-nil
19409 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19410 glyphs in short form, otherwise show glyphs in long form.
19412 Interactively, no argument means show glyphs in short form;
19413 with numeric argument, its value is passed as the GLYPHS flag. */)
19414 (Lisp_Object glyphs)
19416 struct window *w = XWINDOW (selected_window);
19417 struct buffer *buffer = XBUFFER (w->contents);
19419 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19420 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19421 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19422 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19423 fprintf (stderr, "=============================================\n");
19424 dump_glyph_matrix (w->current_matrix,
19425 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19426 return Qnil;
19430 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19431 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19432 Only text-mode frames have frame glyph matrices. */)
19433 (void)
19435 struct frame *f = XFRAME (selected_frame);
19437 if (f->current_matrix)
19438 dump_glyph_matrix (f->current_matrix, 1);
19439 else
19440 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19441 return Qnil;
19445 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19446 doc: /* Dump glyph row ROW to stderr.
19447 GLYPH 0 means don't dump glyphs.
19448 GLYPH 1 means dump glyphs in short form.
19449 GLYPH > 1 or omitted means dump glyphs in long form. */)
19450 (Lisp_Object row, Lisp_Object glyphs)
19452 struct glyph_matrix *matrix;
19453 EMACS_INT vpos;
19455 CHECK_NUMBER (row);
19456 matrix = XWINDOW (selected_window)->current_matrix;
19457 vpos = XINT (row);
19458 if (vpos >= 0 && vpos < matrix->nrows)
19459 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19460 vpos,
19461 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19462 return Qnil;
19466 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19467 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19468 GLYPH 0 means don't dump glyphs.
19469 GLYPH 1 means dump glyphs in short form.
19470 GLYPH > 1 or omitted means dump glyphs in long form.
19472 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19473 do nothing. */)
19474 (Lisp_Object row, Lisp_Object glyphs)
19476 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19477 struct frame *sf = SELECTED_FRAME ();
19478 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19479 EMACS_INT vpos;
19481 CHECK_NUMBER (row);
19482 vpos = XINT (row);
19483 if (vpos >= 0 && vpos < m->nrows)
19484 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19485 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19486 #endif
19487 return Qnil;
19491 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19492 doc: /* Toggle tracing of redisplay.
19493 With ARG, turn tracing on if and only if ARG is positive. */)
19494 (Lisp_Object arg)
19496 if (NILP (arg))
19497 trace_redisplay_p = !trace_redisplay_p;
19498 else
19500 arg = Fprefix_numeric_value (arg);
19501 trace_redisplay_p = XINT (arg) > 0;
19504 return Qnil;
19508 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19509 doc: /* Like `format', but print result to stderr.
19510 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19511 (ptrdiff_t nargs, Lisp_Object *args)
19513 Lisp_Object s = Fformat (nargs, args);
19514 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19515 return Qnil;
19518 #endif /* GLYPH_DEBUG */
19522 /***********************************************************************
19523 Building Desired Matrix Rows
19524 ***********************************************************************/
19526 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19527 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19529 static struct glyph_row *
19530 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19532 struct frame *f = XFRAME (WINDOW_FRAME (w));
19533 struct buffer *buffer = XBUFFER (w->contents);
19534 struct buffer *old = current_buffer;
19535 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19536 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19537 const unsigned char *arrow_end = arrow_string + arrow_len;
19538 const unsigned char *p;
19539 struct it it;
19540 bool multibyte_p;
19541 int n_glyphs_before;
19543 set_buffer_temp (buffer);
19544 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19545 scratch_glyph_row.reversed_p = false;
19546 it.glyph_row->used[TEXT_AREA] = 0;
19547 SET_TEXT_POS (it.position, 0, 0);
19549 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19550 p = arrow_string;
19551 while (p < arrow_end)
19553 Lisp_Object face, ilisp;
19555 /* Get the next character. */
19556 if (multibyte_p)
19557 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19558 else
19560 it.c = it.char_to_display = *p, it.len = 1;
19561 if (! ASCII_CHAR_P (it.c))
19562 it.char_to_display = BYTE8_TO_CHAR (it.c);
19564 p += it.len;
19566 /* Get its face. */
19567 ilisp = make_number (p - arrow_string);
19568 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19569 it.face_id = compute_char_face (f, it.char_to_display, face);
19571 /* Compute its width, get its glyphs. */
19572 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19573 SET_TEXT_POS (it.position, -1, -1);
19574 PRODUCE_GLYPHS (&it);
19576 /* If this character doesn't fit any more in the line, we have
19577 to remove some glyphs. */
19578 if (it.current_x > it.last_visible_x)
19580 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19581 break;
19585 set_buffer_temp (old);
19586 return it.glyph_row;
19590 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19591 glyphs to insert is determined by produce_special_glyphs. */
19593 static void
19594 insert_left_trunc_glyphs (struct it *it)
19596 struct it truncate_it;
19597 struct glyph *from, *end, *to, *toend;
19599 eassert (!FRAME_WINDOW_P (it->f)
19600 || (!it->glyph_row->reversed_p
19601 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19602 || (it->glyph_row->reversed_p
19603 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19605 /* Get the truncation glyphs. */
19606 truncate_it = *it;
19607 truncate_it.current_x = 0;
19608 truncate_it.face_id = DEFAULT_FACE_ID;
19609 truncate_it.glyph_row = &scratch_glyph_row;
19610 truncate_it.area = TEXT_AREA;
19611 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19612 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19613 truncate_it.object = Qnil;
19614 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19616 /* Overwrite glyphs from IT with truncation glyphs. */
19617 if (!it->glyph_row->reversed_p)
19619 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19621 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19622 end = from + tused;
19623 to = it->glyph_row->glyphs[TEXT_AREA];
19624 toend = to + it->glyph_row->used[TEXT_AREA];
19625 if (FRAME_WINDOW_P (it->f))
19627 /* On GUI frames, when variable-size fonts are displayed,
19628 the truncation glyphs may need more pixels than the row's
19629 glyphs they overwrite. We overwrite more glyphs to free
19630 enough screen real estate, and enlarge the stretch glyph
19631 on the right (see display_line), if there is one, to
19632 preserve the screen position of the truncation glyphs on
19633 the right. */
19634 int w = 0;
19635 struct glyph *g = to;
19636 short used;
19638 /* The first glyph could be partially visible, in which case
19639 it->glyph_row->x will be negative. But we want the left
19640 truncation glyphs to be aligned at the left margin of the
19641 window, so we override the x coordinate at which the row
19642 will begin. */
19643 it->glyph_row->x = 0;
19644 while (g < toend && w < it->truncation_pixel_width)
19646 w += g->pixel_width;
19647 ++g;
19649 if (g - to - tused > 0)
19651 memmove (to + tused, g, (toend - g) * sizeof(*g));
19652 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19654 used = it->glyph_row->used[TEXT_AREA];
19655 if (it->glyph_row->truncated_on_right_p
19656 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19657 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19658 == STRETCH_GLYPH)
19660 int extra = w - it->truncation_pixel_width;
19662 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19666 while (from < end)
19667 *to++ = *from++;
19669 /* There may be padding glyphs left over. Overwrite them too. */
19670 if (!FRAME_WINDOW_P (it->f))
19672 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19674 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19675 while (from < end)
19676 *to++ = *from++;
19680 if (to > toend)
19681 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19683 else
19685 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19687 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19688 that back to front. */
19689 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19690 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19691 toend = it->glyph_row->glyphs[TEXT_AREA];
19692 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19693 if (FRAME_WINDOW_P (it->f))
19695 int w = 0;
19696 struct glyph *g = to;
19698 while (g >= toend && w < it->truncation_pixel_width)
19700 w += g->pixel_width;
19701 --g;
19703 if (to - g - tused > 0)
19704 to = g + tused;
19705 if (it->glyph_row->truncated_on_right_p
19706 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19707 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19709 int extra = w - it->truncation_pixel_width;
19711 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19715 while (from >= end && to >= toend)
19716 *to-- = *from--;
19717 if (!FRAME_WINDOW_P (it->f))
19719 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19721 from =
19722 truncate_it.glyph_row->glyphs[TEXT_AREA]
19723 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19724 while (from >= end && to >= toend)
19725 *to-- = *from--;
19728 if (from >= end)
19730 /* Need to free some room before prepending additional
19731 glyphs. */
19732 int move_by = from - end + 1;
19733 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19734 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19736 for ( ; g >= g0; g--)
19737 g[move_by] = *g;
19738 while (from >= end)
19739 *to-- = *from--;
19740 it->glyph_row->used[TEXT_AREA] += move_by;
19745 /* Compute the hash code for ROW. */
19746 unsigned
19747 row_hash (struct glyph_row *row)
19749 int area, k;
19750 unsigned hashval = 0;
19752 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19753 for (k = 0; k < row->used[area]; ++k)
19754 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19755 + row->glyphs[area][k].u.val
19756 + row->glyphs[area][k].face_id
19757 + row->glyphs[area][k].padding_p
19758 + (row->glyphs[area][k].type << 2));
19760 return hashval;
19763 /* Compute the pixel height and width of IT->glyph_row.
19765 Most of the time, ascent and height of a display line will be equal
19766 to the max_ascent and max_height values of the display iterator
19767 structure. This is not the case if
19769 1. We hit ZV without displaying anything. In this case, max_ascent
19770 and max_height will be zero.
19772 2. We have some glyphs that don't contribute to the line height.
19773 (The glyph row flag contributes_to_line_height_p is for future
19774 pixmap extensions).
19776 The first case is easily covered by using default values because in
19777 these cases, the line height does not really matter, except that it
19778 must not be zero. */
19780 static void
19781 compute_line_metrics (struct it *it)
19783 struct glyph_row *row = it->glyph_row;
19785 if (FRAME_WINDOW_P (it->f))
19787 int i, min_y, max_y;
19789 /* The line may consist of one space only, that was added to
19790 place the cursor on it. If so, the row's height hasn't been
19791 computed yet. */
19792 if (row->height == 0)
19794 if (it->max_ascent + it->max_descent == 0)
19795 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19796 row->ascent = it->max_ascent;
19797 row->height = it->max_ascent + it->max_descent;
19798 row->phys_ascent = it->max_phys_ascent;
19799 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19800 row->extra_line_spacing = it->max_extra_line_spacing;
19803 /* Compute the width of this line. */
19804 row->pixel_width = row->x;
19805 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19806 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19808 eassert (row->pixel_width >= 0);
19809 eassert (row->ascent >= 0 && row->height > 0);
19811 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19812 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19814 /* If first line's physical ascent is larger than its logical
19815 ascent, use the physical ascent, and make the row taller.
19816 This makes accented characters fully visible. */
19817 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19818 && row->phys_ascent > row->ascent)
19820 row->height += row->phys_ascent - row->ascent;
19821 row->ascent = row->phys_ascent;
19824 /* Compute how much of the line is visible. */
19825 row->visible_height = row->height;
19827 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19828 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19830 if (row->y < min_y)
19831 row->visible_height -= min_y - row->y;
19832 if (row->y + row->height > max_y)
19833 row->visible_height -= row->y + row->height - max_y;
19835 else
19837 row->pixel_width = row->used[TEXT_AREA];
19838 if (row->continued_p)
19839 row->pixel_width -= it->continuation_pixel_width;
19840 else if (row->truncated_on_right_p)
19841 row->pixel_width -= it->truncation_pixel_width;
19842 row->ascent = row->phys_ascent = 0;
19843 row->height = row->phys_height = row->visible_height = 1;
19844 row->extra_line_spacing = 0;
19847 /* Compute a hash code for this row. */
19848 row->hash = row_hash (row);
19850 it->max_ascent = it->max_descent = 0;
19851 it->max_phys_ascent = it->max_phys_descent = 0;
19855 /* Append one space to the glyph row of iterator IT if doing a
19856 window-based redisplay. The space has the same face as
19857 IT->face_id. Value is true if a space was added.
19859 This function is called to make sure that there is always one glyph
19860 at the end of a glyph row that the cursor can be set on under
19861 window-systems. (If there weren't such a glyph we would not know
19862 how wide and tall a box cursor should be displayed).
19864 At the same time this space let's a nicely handle clearing to the
19865 end of the line if the row ends in italic text. */
19867 static bool
19868 append_space_for_newline (struct it *it, bool default_face_p)
19870 if (FRAME_WINDOW_P (it->f))
19872 int n = it->glyph_row->used[TEXT_AREA];
19874 if (it->glyph_row->glyphs[TEXT_AREA] + n
19875 < it->glyph_row->glyphs[1 + TEXT_AREA])
19877 /* Save some values that must not be changed.
19878 Must save IT->c and IT->len because otherwise
19879 ITERATOR_AT_END_P wouldn't work anymore after
19880 append_space_for_newline has been called. */
19881 enum display_element_type saved_what = it->what;
19882 int saved_c = it->c, saved_len = it->len;
19883 int saved_char_to_display = it->char_to_display;
19884 int saved_x = it->current_x;
19885 int saved_face_id = it->face_id;
19886 bool saved_box_end = it->end_of_box_run_p;
19887 struct text_pos saved_pos;
19888 Lisp_Object saved_object;
19889 struct face *face;
19891 saved_object = it->object;
19892 saved_pos = it->position;
19894 it->what = IT_CHARACTER;
19895 memset (&it->position, 0, sizeof it->position);
19896 it->object = Qnil;
19897 it->c = it->char_to_display = ' ';
19898 it->len = 1;
19900 /* If the default face was remapped, be sure to use the
19901 remapped face for the appended newline. */
19902 if (default_face_p)
19903 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19904 else if (it->face_before_selective_p)
19905 it->face_id = it->saved_face_id;
19906 face = FACE_FROM_ID (it->f, it->face_id);
19907 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19908 /* In R2L rows, we will prepend a stretch glyph that will
19909 have the end_of_box_run_p flag set for it, so there's no
19910 need for the appended newline glyph to have that flag
19911 set. */
19912 if (it->glyph_row->reversed_p
19913 /* But if the appended newline glyph goes all the way to
19914 the end of the row, there will be no stretch glyph,
19915 so leave the box flag set. */
19916 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19917 it->end_of_box_run_p = false;
19919 PRODUCE_GLYPHS (it);
19921 #ifdef HAVE_WINDOW_SYSTEM
19922 /* Make sure this space glyph has the right ascent and
19923 descent values, or else cursor at end of line will look
19924 funny, and height of empty lines will be incorrect. */
19925 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19926 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19927 if (n == 0)
19929 Lisp_Object height, total_height;
19930 int extra_line_spacing = it->extra_line_spacing;
19931 int boff = font->baseline_offset;
19933 if (font->vertical_centering)
19934 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19936 it->object = saved_object; /* get_it_property needs this */
19937 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19938 /* Must do a subset of line height processing from
19939 x_produce_glyph for newline characters. */
19940 height = get_it_property (it, Qline_height);
19941 if (CONSP (height)
19942 && CONSP (XCDR (height))
19943 && NILP (XCDR (XCDR (height))))
19945 total_height = XCAR (XCDR (height));
19946 height = XCAR (height);
19948 else
19949 total_height = Qnil;
19950 height = calc_line_height_property (it, height, font, boff, true);
19952 if (it->override_ascent >= 0)
19954 it->ascent = it->override_ascent;
19955 it->descent = it->override_descent;
19956 boff = it->override_boff;
19958 if (EQ (height, Qt))
19959 extra_line_spacing = 0;
19960 else
19962 Lisp_Object spacing;
19964 it->phys_ascent = it->ascent;
19965 it->phys_descent = it->descent;
19966 if (!NILP (height)
19967 && XINT (height) > it->ascent + it->descent)
19968 it->ascent = XINT (height) - it->descent;
19970 if (!NILP (total_height))
19971 spacing = calc_line_height_property (it, total_height, font,
19972 boff, false);
19973 else
19975 spacing = get_it_property (it, Qline_spacing);
19976 spacing = calc_line_height_property (it, spacing, font,
19977 boff, false);
19979 if (INTEGERP (spacing))
19981 extra_line_spacing = XINT (spacing);
19982 if (!NILP (total_height))
19983 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19986 if (extra_line_spacing > 0)
19988 it->descent += extra_line_spacing;
19989 if (extra_line_spacing > it->max_extra_line_spacing)
19990 it->max_extra_line_spacing = extra_line_spacing;
19992 it->max_ascent = it->ascent;
19993 it->max_descent = it->descent;
19994 /* Make sure compute_line_metrics recomputes the row height. */
19995 it->glyph_row->height = 0;
19998 g->ascent = it->max_ascent;
19999 g->descent = it->max_descent;
20000 #endif
20002 it->override_ascent = -1;
20003 it->constrain_row_ascent_descent_p = false;
20004 it->current_x = saved_x;
20005 it->object = saved_object;
20006 it->position = saved_pos;
20007 it->what = saved_what;
20008 it->face_id = saved_face_id;
20009 it->len = saved_len;
20010 it->c = saved_c;
20011 it->char_to_display = saved_char_to_display;
20012 it->end_of_box_run_p = saved_box_end;
20013 return true;
20017 return false;
20021 /* Extend the face of the last glyph in the text area of IT->glyph_row
20022 to the end of the display line. Called from display_line. If the
20023 glyph row is empty, add a space glyph to it so that we know the
20024 face to draw. Set the glyph row flag fill_line_p. If the glyph
20025 row is R2L, prepend a stretch glyph to cover the empty space to the
20026 left of the leftmost glyph. */
20028 static void
20029 extend_face_to_end_of_line (struct it *it)
20031 struct face *face, *default_face;
20032 struct frame *f = it->f;
20034 /* If line is already filled, do nothing. Non window-system frames
20035 get a grace of one more ``pixel'' because their characters are
20036 1-``pixel'' wide, so they hit the equality too early. This grace
20037 is needed only for R2L rows that are not continued, to produce
20038 one extra blank where we could display the cursor. */
20039 if ((it->current_x >= it->last_visible_x
20040 + (!FRAME_WINDOW_P (f)
20041 && it->glyph_row->reversed_p
20042 && !it->glyph_row->continued_p))
20043 /* If the window has display margins, we will need to extend
20044 their face even if the text area is filled. */
20045 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20046 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20047 return;
20049 /* The default face, possibly remapped. */
20050 default_face = FACE_FROM_ID_OR_NULL (f,
20051 lookup_basic_face (f, DEFAULT_FACE_ID));
20053 /* Face extension extends the background and box of IT->face_id
20054 to the end of the line. If the background equals the background
20055 of the frame, we don't have to do anything. */
20056 face = FACE_FROM_ID (f, (it->face_before_selective_p
20057 ? it->saved_face_id
20058 : it->face_id));
20060 if (FRAME_WINDOW_P (f)
20061 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20062 && face->box == FACE_NO_BOX
20063 && face->background == FRAME_BACKGROUND_PIXEL (f)
20064 #ifdef HAVE_WINDOW_SYSTEM
20065 && !face->stipple
20066 #endif
20067 && !it->glyph_row->reversed_p)
20068 return;
20070 /* Set the glyph row flag indicating that the face of the last glyph
20071 in the text area has to be drawn to the end of the text area. */
20072 it->glyph_row->fill_line_p = true;
20074 /* If current character of IT is not ASCII, make sure we have the
20075 ASCII face. This will be automatically undone the next time
20076 get_next_display_element returns a multibyte character. Note
20077 that the character will always be single byte in unibyte
20078 text. */
20079 if (!ASCII_CHAR_P (it->c))
20081 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20084 if (FRAME_WINDOW_P (f))
20086 /* If the row is empty, add a space with the current face of IT,
20087 so that we know which face to draw. */
20088 if (it->glyph_row->used[TEXT_AREA] == 0)
20090 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20091 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20092 it->glyph_row->used[TEXT_AREA] = 1;
20094 /* Mode line and the header line don't have margins, and
20095 likewise the frame's tool-bar window, if there is any. */
20096 if (!(it->glyph_row->mode_line_p
20097 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20098 || (WINDOWP (f->tool_bar_window)
20099 && it->w == XWINDOW (f->tool_bar_window))
20100 #endif
20103 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20104 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20106 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20107 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20108 default_face->id;
20109 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20111 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20112 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20114 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20115 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20116 default_face->id;
20117 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20120 #ifdef HAVE_WINDOW_SYSTEM
20121 if (it->glyph_row->reversed_p)
20123 /* Prepend a stretch glyph to the row, such that the
20124 rightmost glyph will be drawn flushed all the way to the
20125 right margin of the window. The stretch glyph that will
20126 occupy the empty space, if any, to the left of the
20127 glyphs. */
20128 struct font *font = face->font ? face->font : FRAME_FONT (f);
20129 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20130 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20131 struct glyph *g;
20132 int row_width, stretch_ascent, stretch_width;
20133 struct text_pos saved_pos;
20134 int saved_face_id;
20135 bool saved_avoid_cursor, saved_box_start;
20137 for (row_width = 0, g = row_start; g < row_end; g++)
20138 row_width += g->pixel_width;
20140 /* FIXME: There are various minor display glitches in R2L
20141 rows when only one of the fringes is missing. The
20142 strange condition below produces the least bad effect. */
20143 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20144 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20145 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20146 stretch_width = window_box_width (it->w, TEXT_AREA);
20147 else
20148 stretch_width = it->last_visible_x - it->first_visible_x;
20149 stretch_width -= row_width;
20151 if (stretch_width > 0)
20153 stretch_ascent =
20154 (((it->ascent + it->descent)
20155 * FONT_BASE (font)) / FONT_HEIGHT (font));
20156 saved_pos = it->position;
20157 memset (&it->position, 0, sizeof it->position);
20158 saved_avoid_cursor = it->avoid_cursor_p;
20159 it->avoid_cursor_p = true;
20160 saved_face_id = it->face_id;
20161 saved_box_start = it->start_of_box_run_p;
20162 /* The last row's stretch glyph should get the default
20163 face, to avoid painting the rest of the window with
20164 the region face, if the region ends at ZV. */
20165 if (it->glyph_row->ends_at_zv_p)
20166 it->face_id = default_face->id;
20167 else
20168 it->face_id = face->id;
20169 it->start_of_box_run_p = false;
20170 append_stretch_glyph (it, Qnil, stretch_width,
20171 it->ascent + it->descent, stretch_ascent);
20172 it->position = saved_pos;
20173 it->avoid_cursor_p = saved_avoid_cursor;
20174 it->face_id = saved_face_id;
20175 it->start_of_box_run_p = saved_box_start;
20177 /* If stretch_width comes out negative, it means that the
20178 last glyph is only partially visible. In R2L rows, we
20179 want the leftmost glyph to be partially visible, so we
20180 need to give the row the corresponding left offset. */
20181 if (stretch_width < 0)
20182 it->glyph_row->x = stretch_width;
20184 #endif /* HAVE_WINDOW_SYSTEM */
20186 else
20188 /* Save some values that must not be changed. */
20189 int saved_x = it->current_x;
20190 struct text_pos saved_pos;
20191 Lisp_Object saved_object;
20192 enum display_element_type saved_what = it->what;
20193 int saved_face_id = it->face_id;
20195 saved_object = it->object;
20196 saved_pos = it->position;
20198 it->what = IT_CHARACTER;
20199 memset (&it->position, 0, sizeof it->position);
20200 it->object = Qnil;
20201 it->c = it->char_to_display = ' ';
20202 it->len = 1;
20204 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20205 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20206 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20207 && !it->glyph_row->mode_line_p
20208 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20210 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20211 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20213 for (it->current_x = 0; g < e; g++)
20214 it->current_x += g->pixel_width;
20216 it->area = LEFT_MARGIN_AREA;
20217 it->face_id = default_face->id;
20218 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20219 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20221 PRODUCE_GLYPHS (it);
20222 /* term.c:produce_glyphs advances it->current_x only for
20223 TEXT_AREA. */
20224 it->current_x += it->pixel_width;
20227 it->current_x = saved_x;
20228 it->area = TEXT_AREA;
20231 /* The last row's blank glyphs should get the default face, to
20232 avoid painting the rest of the window with the region face,
20233 if the region ends at ZV. */
20234 if (it->glyph_row->ends_at_zv_p)
20235 it->face_id = default_face->id;
20236 else
20237 it->face_id = face->id;
20238 PRODUCE_GLYPHS (it);
20240 while (it->current_x <= it->last_visible_x)
20241 PRODUCE_GLYPHS (it);
20243 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20244 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20245 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20246 && !it->glyph_row->mode_line_p
20247 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20249 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20250 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20252 for ( ; g < e; g++)
20253 it->current_x += g->pixel_width;
20255 it->area = RIGHT_MARGIN_AREA;
20256 it->face_id = default_face->id;
20257 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20258 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20260 PRODUCE_GLYPHS (it);
20261 it->current_x += it->pixel_width;
20264 it->area = TEXT_AREA;
20267 /* Don't count these blanks really. It would let us insert a left
20268 truncation glyph below and make us set the cursor on them, maybe. */
20269 it->current_x = saved_x;
20270 it->object = saved_object;
20271 it->position = saved_pos;
20272 it->what = saved_what;
20273 it->face_id = saved_face_id;
20278 /* Value is true if text starting at CHARPOS in current_buffer is
20279 trailing whitespace. */
20281 static bool
20282 trailing_whitespace_p (ptrdiff_t charpos)
20284 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20285 int c = 0;
20287 while (bytepos < ZV_BYTE
20288 && (c = FETCH_CHAR (bytepos),
20289 c == ' ' || c == '\t'))
20290 ++bytepos;
20292 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20294 if (bytepos != PT_BYTE)
20295 return true;
20297 return false;
20301 /* Highlight trailing whitespace, if any, in ROW. */
20303 static void
20304 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20306 int used = row->used[TEXT_AREA];
20308 if (used)
20310 struct glyph *start = row->glyphs[TEXT_AREA];
20311 struct glyph *glyph = start + used - 1;
20313 if (row->reversed_p)
20315 /* Right-to-left rows need to be processed in the opposite
20316 direction, so swap the edge pointers. */
20317 glyph = start;
20318 start = row->glyphs[TEXT_AREA] + used - 1;
20321 /* Skip over glyphs inserted to display the cursor at the
20322 end of a line, for extending the face of the last glyph
20323 to the end of the line on terminals, and for truncation
20324 and continuation glyphs. */
20325 if (!row->reversed_p)
20327 while (glyph >= start
20328 && glyph->type == CHAR_GLYPH
20329 && NILP (glyph->object))
20330 --glyph;
20332 else
20334 while (glyph <= start
20335 && glyph->type == CHAR_GLYPH
20336 && NILP (glyph->object))
20337 ++glyph;
20340 /* If last glyph is a space or stretch, and it's trailing
20341 whitespace, set the face of all trailing whitespace glyphs in
20342 IT->glyph_row to `trailing-whitespace'. */
20343 if ((row->reversed_p ? glyph <= start : glyph >= start)
20344 && BUFFERP (glyph->object)
20345 && (glyph->type == STRETCH_GLYPH
20346 || (glyph->type == CHAR_GLYPH
20347 && glyph->u.ch == ' '))
20348 && trailing_whitespace_p (glyph->charpos))
20350 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20351 if (face_id < 0)
20352 return;
20354 if (!row->reversed_p)
20356 while (glyph >= start
20357 && BUFFERP (glyph->object)
20358 && (glyph->type == STRETCH_GLYPH
20359 || (glyph->type == CHAR_GLYPH
20360 && glyph->u.ch == ' ')))
20361 (glyph--)->face_id = face_id;
20363 else
20365 while (glyph <= start
20366 && BUFFERP (glyph->object)
20367 && (glyph->type == STRETCH_GLYPH
20368 || (glyph->type == CHAR_GLYPH
20369 && glyph->u.ch == ' ')))
20370 (glyph++)->face_id = face_id;
20377 /* Value is true if glyph row ROW should be
20378 considered to hold the buffer position CHARPOS. */
20380 static bool
20381 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20383 bool result = true;
20385 if (charpos == CHARPOS (row->end.pos)
20386 || charpos == MATRIX_ROW_END_CHARPOS (row))
20388 /* Suppose the row ends on a string.
20389 Unless the row is continued, that means it ends on a newline
20390 in the string. If it's anything other than a display string
20391 (e.g., a before-string from an overlay), we don't want the
20392 cursor there. (This heuristic seems to give the optimal
20393 behavior for the various types of multi-line strings.)
20394 One exception: if the string has `cursor' property on one of
20395 its characters, we _do_ want the cursor there. */
20396 if (CHARPOS (row->end.string_pos) >= 0)
20398 if (row->continued_p)
20399 result = true;
20400 else
20402 /* Check for `display' property. */
20403 struct glyph *beg = row->glyphs[TEXT_AREA];
20404 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20405 struct glyph *glyph;
20407 result = false;
20408 for (glyph = end; glyph >= beg; --glyph)
20409 if (STRINGP (glyph->object))
20411 Lisp_Object prop
20412 = Fget_char_property (make_number (charpos),
20413 Qdisplay, Qnil);
20414 result =
20415 (!NILP (prop)
20416 && display_prop_string_p (prop, glyph->object));
20417 /* If there's a `cursor' property on one of the
20418 string's characters, this row is a cursor row,
20419 even though this is not a display string. */
20420 if (!result)
20422 Lisp_Object s = glyph->object;
20424 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20426 ptrdiff_t gpos = glyph->charpos;
20428 if (!NILP (Fget_char_property (make_number (gpos),
20429 Qcursor, s)))
20431 result = true;
20432 break;
20436 break;
20440 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20442 /* If the row ends in middle of a real character,
20443 and the line is continued, we want the cursor here.
20444 That's because CHARPOS (ROW->end.pos) would equal
20445 PT if PT is before the character. */
20446 if (!row->ends_in_ellipsis_p)
20447 result = row->continued_p;
20448 else
20449 /* If the row ends in an ellipsis, then
20450 CHARPOS (ROW->end.pos) will equal point after the
20451 invisible text. We want that position to be displayed
20452 after the ellipsis. */
20453 result = false;
20455 /* If the row ends at ZV, display the cursor at the end of that
20456 row instead of at the start of the row below. */
20457 else
20458 result = row->ends_at_zv_p;
20461 return result;
20464 /* Value is true if glyph row ROW should be
20465 used to hold the cursor. */
20467 static bool
20468 cursor_row_p (struct glyph_row *row)
20470 return row_for_charpos_p (row, PT);
20475 /* Push the property PROP so that it will be rendered at the current
20476 position in IT. Return true if PROP was successfully pushed, false
20477 otherwise. Called from handle_line_prefix to handle the
20478 `line-prefix' and `wrap-prefix' properties. */
20480 static bool
20481 push_prefix_prop (struct it *it, Lisp_Object prop)
20483 struct text_pos pos =
20484 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20486 eassert (it->method == GET_FROM_BUFFER
20487 || it->method == GET_FROM_DISPLAY_VECTOR
20488 || it->method == GET_FROM_STRING
20489 || it->method == GET_FROM_IMAGE);
20491 /* We need to save the current buffer/string position, so it will be
20492 restored by pop_it, because iterate_out_of_display_property
20493 depends on that being set correctly, but some situations leave
20494 it->position not yet set when this function is called. */
20495 push_it (it, &pos);
20497 if (STRINGP (prop))
20499 if (SCHARS (prop) == 0)
20501 pop_it (it);
20502 return false;
20505 it->string = prop;
20506 it->string_from_prefix_prop_p = true;
20507 it->multibyte_p = STRING_MULTIBYTE (it->string);
20508 it->current.overlay_string_index = -1;
20509 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20510 it->end_charpos = it->string_nchars = SCHARS (it->string);
20511 it->method = GET_FROM_STRING;
20512 it->stop_charpos = 0;
20513 it->prev_stop = 0;
20514 it->base_level_stop = 0;
20515 it->cmp_it.id = -1;
20517 /* Force paragraph direction to be that of the parent
20518 buffer/string. */
20519 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20520 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20521 else
20522 it->paragraph_embedding = L2R;
20524 /* Set up the bidi iterator for this display string. */
20525 if (it->bidi_p)
20527 it->bidi_it.string.lstring = it->string;
20528 it->bidi_it.string.s = NULL;
20529 it->bidi_it.string.schars = it->end_charpos;
20530 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20531 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20532 it->bidi_it.string.unibyte = !it->multibyte_p;
20533 it->bidi_it.w = it->w;
20534 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20537 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20539 it->method = GET_FROM_STRETCH;
20540 it->object = prop;
20542 #ifdef HAVE_WINDOW_SYSTEM
20543 else if (IMAGEP (prop))
20545 it->what = IT_IMAGE;
20546 it->image_id = lookup_image (it->f, prop);
20547 it->method = GET_FROM_IMAGE;
20549 #endif /* HAVE_WINDOW_SYSTEM */
20550 else
20552 pop_it (it); /* bogus display property, give up */
20553 return false;
20556 return true;
20559 /* Return the character-property PROP at the current position in IT. */
20561 static Lisp_Object
20562 get_it_property (struct it *it, Lisp_Object prop)
20564 Lisp_Object position, object = it->object;
20566 if (STRINGP (object))
20567 position = make_number (IT_STRING_CHARPOS (*it));
20568 else if (BUFFERP (object))
20570 position = make_number (IT_CHARPOS (*it));
20571 object = it->window;
20573 else
20574 return Qnil;
20576 return Fget_char_property (position, prop, object);
20579 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20581 static void
20582 handle_line_prefix (struct it *it)
20584 Lisp_Object prefix;
20586 if (it->continuation_lines_width > 0)
20588 prefix = get_it_property (it, Qwrap_prefix);
20589 if (NILP (prefix))
20590 prefix = Vwrap_prefix;
20592 else
20594 prefix = get_it_property (it, Qline_prefix);
20595 if (NILP (prefix))
20596 prefix = Vline_prefix;
20598 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20600 /* If the prefix is wider than the window, and we try to wrap
20601 it, it would acquire its own wrap prefix, and so on till the
20602 iterator stack overflows. So, don't wrap the prefix. */
20603 it->line_wrap = TRUNCATE;
20604 it->avoid_cursor_p = true;
20610 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20611 only for R2L lines from display_line and display_string, when they
20612 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20613 the line/string needs to be continued on the next glyph row. */
20614 static void
20615 unproduce_glyphs (struct it *it, int n)
20617 struct glyph *glyph, *end;
20619 eassert (it->glyph_row);
20620 eassert (it->glyph_row->reversed_p);
20621 eassert (it->area == TEXT_AREA);
20622 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20624 if (n > it->glyph_row->used[TEXT_AREA])
20625 n = it->glyph_row->used[TEXT_AREA];
20626 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20627 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20628 for ( ; glyph < end; glyph++)
20629 glyph[-n] = *glyph;
20632 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20633 and ROW->maxpos. */
20634 static void
20635 find_row_edges (struct it *it, struct glyph_row *row,
20636 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20637 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20639 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20640 lines' rows is implemented for bidi-reordered rows. */
20642 /* ROW->minpos is the value of min_pos, the minimal buffer position
20643 we have in ROW, or ROW->start.pos if that is smaller. */
20644 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20645 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20646 else
20647 /* We didn't find buffer positions smaller than ROW->start, or
20648 didn't find _any_ valid buffer positions in any of the glyphs,
20649 so we must trust the iterator's computed positions. */
20650 row->minpos = row->start.pos;
20651 if (max_pos <= 0)
20653 max_pos = CHARPOS (it->current.pos);
20654 max_bpos = BYTEPOS (it->current.pos);
20657 /* Here are the various use-cases for ending the row, and the
20658 corresponding values for ROW->maxpos:
20660 Line ends in a newline from buffer eol_pos + 1
20661 Line is continued from buffer max_pos + 1
20662 Line is truncated on right it->current.pos
20663 Line ends in a newline from string max_pos + 1(*)
20664 (*) + 1 only when line ends in a forward scan
20665 Line is continued from string max_pos
20666 Line is continued from display vector max_pos
20667 Line is entirely from a string min_pos == max_pos
20668 Line is entirely from a display vector min_pos == max_pos
20669 Line that ends at ZV ZV
20671 If you discover other use-cases, please add them here as
20672 appropriate. */
20673 if (row->ends_at_zv_p)
20674 row->maxpos = it->current.pos;
20675 else if (row->used[TEXT_AREA])
20677 bool seen_this_string = false;
20678 struct glyph_row *r1 = row - 1;
20680 /* Did we see the same display string on the previous row? */
20681 if (STRINGP (it->object)
20682 /* this is not the first row */
20683 && row > it->w->desired_matrix->rows
20684 /* previous row is not the header line */
20685 && !r1->mode_line_p
20686 /* previous row also ends in a newline from a string */
20687 && r1->ends_in_newline_from_string_p)
20689 struct glyph *start, *end;
20691 /* Search for the last glyph of the previous row that came
20692 from buffer or string. Depending on whether the row is
20693 L2R or R2L, we need to process it front to back or the
20694 other way round. */
20695 if (!r1->reversed_p)
20697 start = r1->glyphs[TEXT_AREA];
20698 end = start + r1->used[TEXT_AREA];
20699 /* Glyphs inserted by redisplay have nil as their object. */
20700 while (end > start
20701 && NILP ((end - 1)->object)
20702 && (end - 1)->charpos <= 0)
20703 --end;
20704 if (end > start)
20706 if (EQ ((end - 1)->object, it->object))
20707 seen_this_string = true;
20709 else
20710 /* If all the glyphs of the previous row were inserted
20711 by redisplay, it means the previous row was
20712 produced from a single newline, which is only
20713 possible if that newline came from the same string
20714 as the one which produced this ROW. */
20715 seen_this_string = true;
20717 else
20719 end = r1->glyphs[TEXT_AREA] - 1;
20720 start = end + r1->used[TEXT_AREA];
20721 while (end < start
20722 && NILP ((end + 1)->object)
20723 && (end + 1)->charpos <= 0)
20724 ++end;
20725 if (end < start)
20727 if (EQ ((end + 1)->object, it->object))
20728 seen_this_string = true;
20730 else
20731 seen_this_string = true;
20734 /* Take note of each display string that covers a newline only
20735 once, the first time we see it. This is for when a display
20736 string includes more than one newline in it. */
20737 if (row->ends_in_newline_from_string_p && !seen_this_string)
20739 /* If we were scanning the buffer forward when we displayed
20740 the string, we want to account for at least one buffer
20741 position that belongs to this row (position covered by
20742 the display string), so that cursor positioning will
20743 consider this row as a candidate when point is at the end
20744 of the visual line represented by this row. This is not
20745 required when scanning back, because max_pos will already
20746 have a much larger value. */
20747 if (CHARPOS (row->end.pos) > max_pos)
20748 INC_BOTH (max_pos, max_bpos);
20749 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20751 else if (CHARPOS (it->eol_pos) > 0)
20752 SET_TEXT_POS (row->maxpos,
20753 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20754 else if (row->continued_p)
20756 /* If max_pos is different from IT's current position, it
20757 means IT->method does not belong to the display element
20758 at max_pos. However, it also means that the display
20759 element at max_pos was displayed in its entirety on this
20760 line, which is equivalent to saying that the next line
20761 starts at the next buffer position. */
20762 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20763 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20764 else
20766 INC_BOTH (max_pos, max_bpos);
20767 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20770 else if (row->truncated_on_right_p)
20771 /* display_line already called reseat_at_next_visible_line_start,
20772 which puts the iterator at the beginning of the next line, in
20773 the logical order. */
20774 row->maxpos = it->current.pos;
20775 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20776 /* A line that is entirely from a string/image/stretch... */
20777 row->maxpos = row->minpos;
20778 else
20779 emacs_abort ();
20781 else
20782 row->maxpos = it->current.pos;
20785 /* Like display_count_lines, but capable of counting outside of the
20786 current narrowed region. */
20787 static ptrdiff_t
20788 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20789 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20791 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20792 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20794 ptrdiff_t val;
20795 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20796 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20797 Fwiden ();
20798 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20799 unbind_to (pdl_count, Qnil);
20800 return val;
20803 /* Count the number of screen lines in window IT->w between character
20804 position IT_CHARPOS(*IT) and the line showing that window's point. */
20805 static ptrdiff_t
20806 display_count_lines_visually (struct it *it)
20808 struct it tem_it;
20809 ptrdiff_t to;
20810 struct text_pos from;
20812 /* If we already calculated a relative line number, use that. This
20813 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20814 are laid out sequentially, one by one, for each sequence of calls
20815 to display_line or other similar function that follows a call to
20816 init_iterator. */
20817 if (it->lnum_bytepos > 0)
20818 return it->lnum + 1;
20819 else
20821 ptrdiff_t count = SPECPDL_INDEX ();
20823 if (IT_CHARPOS (*it) <= PT)
20825 from = it->current.pos;
20826 to = PT;
20828 else
20830 SET_TEXT_POS (from, PT, PT_BYTE);
20831 to = IT_CHARPOS (*it);
20833 start_display (&tem_it, it->w, from);
20834 /* Need to disable visual mode temporarily, since otherwise the
20835 call to move_it_to will cause infinite recursion. */
20836 specbind (Qdisplay_line_numbers, Qrelative);
20837 /* Some redisplay optimizations could invoke us very far from
20838 PT, which will make the caller painfully slow. There should
20839 be no need to go too far beyond the window's bottom, as any
20840 such optimization will fail to show point anyway. */
20841 move_it_to (&tem_it, to, -1,
20842 tem_it.last_visible_y
20843 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20844 -1, MOVE_TO_POS | MOVE_TO_Y);
20845 unbind_to (count, Qnil);
20846 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20850 /* Produce the line-number glyphs for the current glyph_row. If
20851 IT->glyph_row is non-NULL, populate the row with the produced
20852 glyphs. */
20853 static void
20854 maybe_produce_line_number (struct it *it)
20856 ptrdiff_t last_line = it->lnum;
20857 ptrdiff_t start_from, bytepos;
20858 ptrdiff_t this_line;
20859 bool first_time = false;
20860 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20861 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20862 void *itdata = bidi_shelve_cache ();
20864 if (EQ (Vdisplay_line_numbers, Qvisual))
20865 this_line = display_count_lines_visually (it);
20866 else
20868 if (!last_line)
20870 /* If possible, reuse data cached by line-number-mode. */
20871 if (it->w->base_line_number > 0
20872 && it->w->base_line_pos > 0
20873 && it->w->base_line_pos <= IT_CHARPOS (*it)
20874 /* line-number-mode always displays narrowed line
20875 numbers, so we cannot use its data if the user wants
20876 line numbers that disregard narrowing. */
20877 && !(display_line_numbers_widen
20878 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE)))
20880 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20881 last_line = it->w->base_line_number - 1;
20883 else
20884 start_from = beg_byte;
20885 if (!it->lnum_bytepos)
20886 first_time = true;
20888 else
20889 start_from = it->lnum_bytepos;
20891 /* Paranoia: what if someone changes the narrowing since the
20892 last time display_line was called? Shouldn't really happen,
20893 but who knows what some crazy Lisp invoked by :eval could do? */
20894 if (!(beg_byte <= start_from && start_from <= z_byte))
20896 last_line = 0;
20897 start_from = beg_byte;
20900 this_line =
20901 last_line + display_count_lines_logically (start_from,
20902 IT_BYTEPOS (*it),
20903 IT_CHARPOS (*it), &bytepos);
20904 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20905 eassert (bytepos == IT_BYTEPOS (*it));
20908 /* Record the line number information. */
20909 if (this_line != last_line || !it->lnum_bytepos)
20911 it->lnum = this_line;
20912 it->lnum_bytepos = IT_BYTEPOS (*it);
20915 /* Produce the glyphs for the line number. */
20916 struct it tem_it;
20917 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20918 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20919 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20920 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
20921 int current_lnum_face_id
20922 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
20923 /* Compute point's line number if needed. */
20924 if ((EQ (Vdisplay_line_numbers, Qrelative)
20925 || EQ (Vdisplay_line_numbers, Qvisual)
20926 || lnum_face_id != current_lnum_face_id)
20927 && !it->pt_lnum)
20929 ptrdiff_t ignored;
20930 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
20931 it->pt_lnum =
20932 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
20933 PT, &ignored);
20934 else
20935 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
20936 &ignored);
20938 /* Compute the required width if needed. */
20939 if (!it->lnum_width)
20941 if (NATNUMP (Vdisplay_line_numbers_width))
20942 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
20944 /* Max line number to be displayed cannot be more than the one
20945 corresponding to the last row of the desired matrix. */
20946 ptrdiff_t max_lnum;
20948 if (NILP (Vdisplay_line_numbers_current_absolute)
20949 && (EQ (Vdisplay_line_numbers, Qrelative)
20950 || EQ (Vdisplay_line_numbers, Qvisual)))
20951 /* We subtract one more because the current line is always
20952 zero in this mode. */
20953 max_lnum = it->w->desired_matrix->nrows - 2;
20954 else if (EQ (Vdisplay_line_numbers, Qvisual))
20955 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
20956 else
20957 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
20958 max_lnum = max (1, max_lnum);
20959 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
20960 eassert (it->lnum_width > 0);
20962 if (EQ (Vdisplay_line_numbers, Qrelative))
20963 lnum_offset = it->pt_lnum;
20964 else if (EQ (Vdisplay_line_numbers, Qvisual))
20965 lnum_offset = 0;
20967 /* Under 'relative', display the absolute line number for the
20968 current line, unless the user requests otherwise. */
20969 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
20970 if ((EQ (Vdisplay_line_numbers, Qrelative)
20971 || EQ (Vdisplay_line_numbers, Qvisual))
20972 && lnum_to_display == 0
20973 && !NILP (Vdisplay_line_numbers_current_absolute))
20974 lnum_to_display = it->pt_lnum + 1;
20975 /* In L2R rows we need to append the blank separator, in R2L
20976 rows we need to prepend it. But this function is usually
20977 called when no display elements were produced from the
20978 following line, so the paragraph direction might be unknown.
20979 Therefore we cheat and add 2 blanks, one on either side. */
20980 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
20981 strcat (lnum_buf, " ");
20983 /* Setup for producing the glyphs. */
20984 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
20985 /* FIXME: Use specialized face. */
20986 DEFAULT_FACE_ID);
20987 scratch_glyph_row.reversed_p = false;
20988 scratch_glyph_row.used[TEXT_AREA] = 0;
20989 SET_TEXT_POS (tem_it.position, 0, 0);
20990 tem_it.avoid_cursor_p = true;
20991 tem_it.bidi_p = true;
20992 tem_it.bidi_it.type = WEAK_EN;
20993 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
20994 1 level in R2L paragraphs. Emulate that, assuming we are in
20995 an L2R paragraph. */
20996 tem_it.bidi_it.resolved_level = 2;
20998 /* Produce glyphs for the line number in a scratch glyph_row. */
20999 int n_glyphs_before;
21000 for (const char *p = lnum_buf; *p; p++)
21002 /* For continuation lines and lines after ZV, instead of a line
21003 number, produce a blank prefix of the same width. Use the
21004 default face for the blank field beyond ZV. */
21005 if (beyond_zv)
21006 tem_it.face_id = it->base_face_id;
21007 else if (lnum_face_id != current_lnum_face_id
21008 && (EQ (Vdisplay_line_numbers, Qvisual)
21009 ? this_line == 0
21010 : this_line == it->pt_lnum))
21011 tem_it.face_id = current_lnum_face_id;
21012 else
21013 tem_it.face_id = lnum_face_id;
21014 if (beyond_zv
21015 /* Don't display the same line number more than once. */
21016 || (!EQ (Vdisplay_line_numbers, Qvisual)
21017 && (it->continuation_lines_width > 0
21018 || (this_line == last_line && !first_time))))
21019 tem_it.c = tem_it.char_to_display = ' ';
21020 else
21021 tem_it.c = tem_it.char_to_display = *p;
21022 tem_it.len = 1;
21023 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21024 /* Make sure these glyphs will have a "position" of -1. */
21025 SET_TEXT_POS (tem_it.position, -1, -1);
21026 PRODUCE_GLYPHS (&tem_it);
21028 /* Stop producing glyphs if we don't have enough space on
21029 this line. FIXME: should we refrain from producing the
21030 line number at all in that case? */
21031 if (tem_it.current_x > tem_it.last_visible_x)
21033 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21034 break;
21038 /* Record the width in pixels we need for the line number display. */
21039 it->lnum_pixel_width = tem_it.current_x;
21040 /* Copy the produced glyphs into IT's glyph_row. */
21041 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21042 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21043 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21044 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21046 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21048 for ( ; g < e; g++)
21050 it->current_x += g->pixel_width;
21051 /* The following is important when this function is called
21052 from move_it_in_display_line_to: HPOS is incremented only
21053 when we are in the visible portion of the glyph row. */
21054 if (it->current_x > it->first_visible_x)
21055 it->hpos++;
21056 if (p)
21058 *p++ = *g;
21059 (*u)++;
21063 /* Update IT's metrics due to glyphs produced for line numbers. */
21064 if (it->glyph_row)
21066 struct glyph_row *row = it->glyph_row;
21068 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21069 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21070 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21071 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21072 tem_it.max_phys_descent);
21074 else
21076 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21077 it->max_descent = max (it->max_descent, tem_it.max_descent);
21078 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21079 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21082 bidi_unshelve_cache (itdata, false);
21085 /* Return true if this glyph row needs a line number to be produced
21086 for it. */
21087 static bool
21088 should_produce_line_number (struct it *it)
21090 if (NILP (Vdisplay_line_numbers))
21091 return false;
21093 /* Don't display line numbers in minibuffer windows. */
21094 if (MINI_WINDOW_P (it->w))
21095 return false;
21097 #ifdef HAVE_WINDOW_SYSTEM
21098 /* Don't display line number in tooltip frames. */
21099 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame))
21100 return false;
21101 #endif
21103 /* If the character at current position has a non-nil special
21104 property, disable line numbers for this row. This is for
21105 packages such as company-mode, which need this for their tricky
21106 layout, where line numbers get in the way. */
21107 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21108 Qdisplay_line_numbers_disable,
21109 it->window);
21110 /* For ZV, we need to also look in empty overlays at that point,
21111 because get-char-property always returns nil for ZV, except if
21112 the property is in 'default-text-properties'. */
21113 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21114 val = disable_line_numbers_overlay_at_eob ();
21115 return NILP (val) ? true : false;
21118 /* Return true if ROW has no glyphs except those inserted by the
21119 display engine. This is needed for indicate-empty-lines and
21120 similar features when the glyph row starts with glyphs which didn't
21121 come from buffer or string. */
21122 static bool
21123 row_text_area_empty (struct glyph_row *row)
21125 if (!row->reversed_p)
21127 for (struct glyph *g = row->glyphs[TEXT_AREA];
21128 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21129 g++)
21130 if (!NILP (g->object) || g->charpos > 0)
21131 return false;
21133 else
21135 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21136 g > row->glyphs[TEXT_AREA];
21137 g--)
21138 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21139 return false;
21142 return true;
21145 /* Construct the glyph row IT->glyph_row in the desired matrix of
21146 IT->w from text at the current position of IT. See dispextern.h
21147 for an overview of struct it. Value is true if
21148 IT->glyph_row displays text, as opposed to a line displaying ZV
21149 only. CURSOR_VPOS is the window-relative vertical position of
21150 the glyph row displaying the cursor, or -1 if unknown. */
21152 static bool
21153 display_line (struct it *it, int cursor_vpos)
21155 struct glyph_row *row = it->glyph_row;
21156 Lisp_Object overlay_arrow_string;
21157 struct it wrap_it;
21158 void *wrap_data = NULL;
21159 bool may_wrap = false;
21160 int wrap_x UNINIT;
21161 int wrap_row_used = -1;
21162 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21163 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21164 int wrap_row_extra_line_spacing UNINIT;
21165 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21166 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21167 int cvpos;
21168 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21169 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21170 bool pending_handle_line_prefix = false;
21171 int header_line = window_wants_header_line (it->w);
21172 bool hscroll_this_line = (cursor_vpos >= 0
21173 && it->vpos == cursor_vpos - header_line
21174 && hscrolling_current_line_p (it->w));
21175 int first_visible_x = it->first_visible_x;
21176 int last_visible_x = it->last_visible_x;
21177 int x_incr = 0;
21179 /* We always start displaying at hpos zero even if hscrolled. */
21180 eassert (it->hpos == 0 && it->current_x == 0);
21182 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21183 >= it->w->desired_matrix->nrows)
21185 it->w->nrows_scale_factor++;
21186 it->f->fonts_changed = true;
21187 return false;
21190 /* Clear the result glyph row and enable it. */
21191 prepare_desired_row (it->w, row, false);
21193 row->y = it->current_y;
21194 row->start = it->start;
21195 row->continuation_lines_width = it->continuation_lines_width;
21196 row->displays_text_p = true;
21197 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21198 it->starts_in_middle_of_char_p = false;
21200 /* Arrange the overlays nicely for our purposes. Usually, we call
21201 display_line on only one line at a time, in which case this
21202 can't really hurt too much, or we call it on lines which appear
21203 one after another in the buffer, in which case all calls to
21204 recenter_overlay_lists but the first will be pretty cheap. */
21205 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21207 /* If we are going to display the cursor's line, account for the
21208 hscroll of that line. We subtract the window's min_hscroll,
21209 because that was already accounted for in init_iterator. */
21210 if (hscroll_this_line)
21211 x_incr =
21212 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21213 * FRAME_COLUMN_WIDTH (it->f);
21215 bool line_number_needed = should_produce_line_number (it);
21217 /* Move over display elements that are not visible because we are
21218 hscrolled. This may stop at an x-position < first_visible_x
21219 if the first glyph is partially visible or if we hit a line end. */
21220 if (it->current_x < it->first_visible_x + x_incr)
21222 enum move_it_result move_result;
21224 this_line_min_pos = row->start.pos;
21225 if (hscroll_this_line)
21227 it->first_visible_x += x_incr;
21228 it->last_visible_x += x_incr;
21230 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21231 MOVE_TO_POS | MOVE_TO_X);
21232 /* If we are under a large hscroll, move_it_in_display_line_to
21233 could hit the end of the line without reaching
21234 first_visible_x. Pretend that we did reach it. This is
21235 especially important on a TTY, where we will call
21236 extend_face_to_end_of_line, which needs to know how many
21237 blank glyphs to produce. */
21238 if (it->current_x < it->first_visible_x
21239 && (move_result == MOVE_NEWLINE_OR_CR
21240 || move_result == MOVE_POS_MATCH_OR_ZV))
21241 it->current_x = it->first_visible_x;
21243 /* Record the smallest positions seen while we moved over
21244 display elements that are not visible. This is needed by
21245 redisplay_internal for optimizing the case where the cursor
21246 stays inside the same line. The rest of this function only
21247 considers positions that are actually displayed, so
21248 RECORD_MAX_MIN_POS will not otherwise record positions that
21249 are hscrolled to the left of the left edge of the window. */
21250 min_pos = CHARPOS (this_line_min_pos);
21251 min_bpos = BYTEPOS (this_line_min_pos);
21253 /* Produce line number, if needed. */
21254 if (line_number_needed)
21255 maybe_produce_line_number (it);
21257 else if (it->area == TEXT_AREA)
21259 /* Line numbers should precede the line-prefix or wrap-prefix. */
21260 if (line_number_needed)
21261 maybe_produce_line_number (it);
21263 /* We only do this when not calling move_it_in_display_line_to
21264 above, because that function calls itself handle_line_prefix. */
21265 handle_line_prefix (it);
21267 else
21269 /* Line-prefix and wrap-prefix are always displayed in the text
21270 area. But if this is the first call to display_line after
21271 init_iterator, the iterator might have been set up to write
21272 into a marginal area, e.g. if the line begins with some
21273 display property that writes to the margins. So we need to
21274 wait with the call to handle_line_prefix until whatever
21275 writes to the margin has done its job. */
21276 pending_handle_line_prefix = true;
21279 /* Get the initial row height. This is either the height of the
21280 text hscrolled, if there is any, or zero. */
21281 row->ascent = it->max_ascent;
21282 row->height = it->max_ascent + it->max_descent;
21283 row->phys_ascent = it->max_phys_ascent;
21284 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21285 row->extra_line_spacing = it->max_extra_line_spacing;
21287 /* Utility macro to record max and min buffer positions seen until now. */
21288 #define RECORD_MAX_MIN_POS(IT) \
21289 do \
21291 bool composition_p \
21292 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21293 ptrdiff_t current_pos = \
21294 composition_p ? (IT)->cmp_it.charpos \
21295 : IT_CHARPOS (*(IT)); \
21296 ptrdiff_t current_bpos = \
21297 composition_p ? CHAR_TO_BYTE (current_pos) \
21298 : IT_BYTEPOS (*(IT)); \
21299 if (current_pos < min_pos) \
21301 min_pos = current_pos; \
21302 min_bpos = current_bpos; \
21304 if (IT_CHARPOS (*it) > max_pos) \
21306 max_pos = IT_CHARPOS (*it); \
21307 max_bpos = IT_BYTEPOS (*it); \
21310 while (false)
21312 /* Loop generating characters. The loop is left with IT on the next
21313 character to display. */
21314 while (true)
21316 int n_glyphs_before, hpos_before, x_before;
21317 int x, nglyphs;
21318 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21320 /* Retrieve the next thing to display. Value is false if end of
21321 buffer reached. */
21322 if (!get_next_display_element (it))
21324 bool row_has_glyphs = false;
21325 /* Maybe add a space at the end of this line that is used to
21326 display the cursor there under X. Set the charpos of the
21327 first glyph of blank lines not corresponding to any text
21328 to -1. */
21329 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21330 row->exact_window_width_line_p = true;
21331 else if ((append_space_for_newline (it, true)
21332 && row->used[TEXT_AREA] == 1)
21333 || row->used[TEXT_AREA] == 0
21334 || (row_has_glyphs = row_text_area_empty (row)))
21336 row->glyphs[TEXT_AREA]->charpos = -1;
21337 /* Don't reset the displays_text_p flag if we are
21338 displaying line numbers or line-prefix. */
21339 if (!row_has_glyphs)
21340 row->displays_text_p = false;
21342 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21343 && (!MINI_WINDOW_P (it->w)))
21344 row->indicate_empty_line_p = true;
21347 it->continuation_lines_width = 0;
21348 /* Reset those iterator values set from display property
21349 values. This is for the case when the display property
21350 ends at ZV, and is not a replacing property, so pop_it is
21351 not called. */
21352 it->font_height = Qnil;
21353 it->voffset = 0;
21354 row->ends_at_zv_p = true;
21355 /* A row that displays right-to-left text must always have
21356 its last face extended all the way to the end of line,
21357 even if this row ends in ZV, because we still write to
21358 the screen left to right. We also need to extend the
21359 last face if the default face is remapped to some
21360 different face, otherwise the functions that clear
21361 portions of the screen will clear with the default face's
21362 background color. */
21363 if (row->reversed_p
21364 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21365 extend_face_to_end_of_line (it);
21366 break;
21369 /* Now, get the metrics of what we want to display. This also
21370 generates glyphs in `row' (which is IT->glyph_row). */
21371 n_glyphs_before = row->used[TEXT_AREA];
21372 x = it->current_x;
21374 /* Remember the line height so far in case the next element doesn't
21375 fit on the line. */
21376 if (it->line_wrap != TRUNCATE)
21378 ascent = it->max_ascent;
21379 descent = it->max_descent;
21380 phys_ascent = it->max_phys_ascent;
21381 phys_descent = it->max_phys_descent;
21383 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21385 if (IT_DISPLAYING_WHITESPACE (it))
21386 may_wrap = true;
21387 else if (may_wrap)
21389 SAVE_IT (wrap_it, *it, wrap_data);
21390 wrap_x = x;
21391 wrap_row_used = row->used[TEXT_AREA];
21392 wrap_row_ascent = row->ascent;
21393 wrap_row_height = row->height;
21394 wrap_row_phys_ascent = row->phys_ascent;
21395 wrap_row_phys_height = row->phys_height;
21396 wrap_row_extra_line_spacing = row->extra_line_spacing;
21397 wrap_row_min_pos = min_pos;
21398 wrap_row_min_bpos = min_bpos;
21399 wrap_row_max_pos = max_pos;
21400 wrap_row_max_bpos = max_bpos;
21401 may_wrap = false;
21406 PRODUCE_GLYPHS (it);
21408 /* If this display element was in marginal areas, continue with
21409 the next one. */
21410 if (it->area != TEXT_AREA)
21412 row->ascent = max (row->ascent, it->max_ascent);
21413 row->height = max (row->height, it->max_ascent + it->max_descent);
21414 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21415 row->phys_height = max (row->phys_height,
21416 it->max_phys_ascent + it->max_phys_descent);
21417 row->extra_line_spacing = max (row->extra_line_spacing,
21418 it->max_extra_line_spacing);
21419 set_iterator_to_next (it, true);
21420 /* If we didn't handle the line/wrap prefix above, and the
21421 call to set_iterator_to_next just switched to TEXT_AREA,
21422 process the prefix now. */
21423 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21425 /* Line numbers should precede the line-prefix or wrap-prefix. */
21426 if (line_number_needed)
21427 maybe_produce_line_number (it);
21429 pending_handle_line_prefix = false;
21430 handle_line_prefix (it);
21432 continue;
21435 /* Does the display element fit on the line? If we truncate
21436 lines, we should draw past the right edge of the window. If
21437 we don't truncate, we want to stop so that we can display the
21438 continuation glyph before the right margin. If lines are
21439 continued, there are two possible strategies for characters
21440 resulting in more than 1 glyph (e.g. tabs): Display as many
21441 glyphs as possible in this line and leave the rest for the
21442 continuation line, or display the whole element in the next
21443 line. Original redisplay did the former, so we do it also. */
21444 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21445 hpos_before = it->hpos;
21446 x_before = x;
21448 if (/* Not a newline. */
21449 nglyphs > 0
21450 /* Glyphs produced fit entirely in the line. */
21451 && it->current_x < it->last_visible_x)
21453 it->hpos += nglyphs;
21454 row->ascent = max (row->ascent, it->max_ascent);
21455 row->height = max (row->height, it->max_ascent + it->max_descent);
21456 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21457 row->phys_height = max (row->phys_height,
21458 it->max_phys_ascent + it->max_phys_descent);
21459 row->extra_line_spacing = max (row->extra_line_spacing,
21460 it->max_extra_line_spacing);
21461 if (it->current_x - it->pixel_width < it->first_visible_x
21462 /* In R2L rows, we arrange in extend_face_to_end_of_line
21463 to add a right offset to the line, by a suitable
21464 change to the stretch glyph that is the leftmost
21465 glyph of the line. */
21466 && !row->reversed_p)
21467 row->x = x - it->first_visible_x;
21468 /* Record the maximum and minimum buffer positions seen so
21469 far in glyphs that will be displayed by this row. */
21470 if (it->bidi_p)
21471 RECORD_MAX_MIN_POS (it);
21473 else
21475 int i, new_x;
21476 struct glyph *glyph;
21478 for (i = 0; i < nglyphs; ++i, x = new_x)
21480 /* Identify the glyphs added by the last call to
21481 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21482 the previous glyphs. */
21483 if (!row->reversed_p)
21484 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21485 else
21486 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21487 new_x = x + glyph->pixel_width;
21489 if (/* Lines are continued. */
21490 it->line_wrap != TRUNCATE
21491 && (/* Glyph doesn't fit on the line. */
21492 new_x > it->last_visible_x
21493 /* Or it fits exactly on a window system frame. */
21494 || (new_x == it->last_visible_x
21495 && FRAME_WINDOW_P (it->f)
21496 && (row->reversed_p
21497 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21498 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21500 /* End of a continued line. */
21502 if (it->hpos == 0
21503 || (new_x == it->last_visible_x
21504 && FRAME_WINDOW_P (it->f)
21505 && (row->reversed_p
21506 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21507 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21509 /* Current glyph is the only one on the line or
21510 fits exactly on the line. We must continue
21511 the line because we can't draw the cursor
21512 after the glyph. */
21513 row->continued_p = true;
21514 it->current_x = new_x;
21515 it->continuation_lines_width += new_x;
21516 ++it->hpos;
21517 if (i == nglyphs - 1)
21519 /* If line-wrap is on, check if a previous
21520 wrap point was found. */
21521 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21522 && wrap_row_used > 0
21523 /* Even if there is a previous wrap
21524 point, continue the line here as
21525 usual, if (i) the previous character
21526 was a space or tab AND (ii) the
21527 current character is not. */
21528 && (!may_wrap
21529 || IT_DISPLAYING_WHITESPACE (it)))
21530 goto back_to_wrap;
21532 /* Record the maximum and minimum buffer
21533 positions seen so far in glyphs that will be
21534 displayed by this row. */
21535 if (it->bidi_p)
21536 RECORD_MAX_MIN_POS (it);
21537 set_iterator_to_next (it, true);
21538 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21540 if (!get_next_display_element (it))
21542 row->exact_window_width_line_p = true;
21543 it->continuation_lines_width = 0;
21544 it->font_height = Qnil;
21545 it->voffset = 0;
21546 row->continued_p = false;
21547 row->ends_at_zv_p = true;
21549 else if (ITERATOR_AT_END_OF_LINE_P (it))
21551 row->continued_p = false;
21552 row->exact_window_width_line_p = true;
21554 /* If line-wrap is on, check if a
21555 previous wrap point was found. */
21556 else if (wrap_row_used > 0
21557 /* Even if there is a previous wrap
21558 point, continue the line here as
21559 usual, if (i) the previous character
21560 was a space or tab AND (ii) the
21561 current character is not. */
21562 && (!may_wrap
21563 || IT_DISPLAYING_WHITESPACE (it)))
21564 goto back_to_wrap;
21568 else if (it->bidi_p)
21569 RECORD_MAX_MIN_POS (it);
21570 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21571 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21572 extend_face_to_end_of_line (it);
21574 else if (CHAR_GLYPH_PADDING_P (*glyph)
21575 && !FRAME_WINDOW_P (it->f))
21577 /* A padding glyph that doesn't fit on this line.
21578 This means the whole character doesn't fit
21579 on the line. */
21580 if (row->reversed_p)
21581 unproduce_glyphs (it, row->used[TEXT_AREA]
21582 - n_glyphs_before);
21583 row->used[TEXT_AREA] = n_glyphs_before;
21585 /* Fill the rest of the row with continuation
21586 glyphs like in 20.x. */
21587 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21588 < row->glyphs[1 + TEXT_AREA])
21589 produce_special_glyphs (it, IT_CONTINUATION);
21591 row->continued_p = true;
21592 it->current_x = x_before;
21593 it->continuation_lines_width += x_before;
21595 /* Restore the height to what it was before the
21596 element not fitting on the line. */
21597 it->max_ascent = ascent;
21598 it->max_descent = descent;
21599 it->max_phys_ascent = phys_ascent;
21600 it->max_phys_descent = phys_descent;
21601 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21602 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21603 extend_face_to_end_of_line (it);
21605 else if (wrap_row_used > 0)
21607 back_to_wrap:
21608 if (row->reversed_p)
21609 unproduce_glyphs (it,
21610 row->used[TEXT_AREA] - wrap_row_used);
21611 RESTORE_IT (it, &wrap_it, wrap_data);
21612 it->continuation_lines_width += wrap_x;
21613 row->used[TEXT_AREA] = wrap_row_used;
21614 row->ascent = wrap_row_ascent;
21615 row->height = wrap_row_height;
21616 row->phys_ascent = wrap_row_phys_ascent;
21617 row->phys_height = wrap_row_phys_height;
21618 row->extra_line_spacing = wrap_row_extra_line_spacing;
21619 min_pos = wrap_row_min_pos;
21620 min_bpos = wrap_row_min_bpos;
21621 max_pos = wrap_row_max_pos;
21622 max_bpos = wrap_row_max_bpos;
21623 row->continued_p = true;
21624 row->ends_at_zv_p = false;
21625 row->exact_window_width_line_p = false;
21626 it->continuation_lines_width += x;
21628 /* Make sure that a non-default face is extended
21629 up to the right margin of the window. */
21630 extend_face_to_end_of_line (it);
21632 else if ((it->what == IT_CHARACTER
21633 || it->what == IT_STRETCH
21634 || it->what == IT_COMPOSITION)
21635 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21637 /* A TAB that extends past the right edge of the
21638 window. This produces a single glyph on
21639 window system frames. We leave the glyph in
21640 this row and let it fill the row, but don't
21641 consume the TAB. */
21642 if ((row->reversed_p
21643 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21644 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21645 produce_special_glyphs (it, IT_CONTINUATION);
21646 it->continuation_lines_width += it->last_visible_x;
21647 row->ends_in_middle_of_char_p = true;
21648 row->continued_p = true;
21649 glyph->pixel_width = it->last_visible_x - x;
21650 it->starts_in_middle_of_char_p = true;
21651 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21652 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21653 extend_face_to_end_of_line (it);
21655 else
21657 /* Something other than a TAB that draws past
21658 the right edge of the window. Restore
21659 positions to values before the element. */
21660 if (row->reversed_p)
21661 unproduce_glyphs (it, row->used[TEXT_AREA]
21662 - (n_glyphs_before + i));
21663 row->used[TEXT_AREA] = n_glyphs_before + i;
21665 /* Display continuation glyphs. */
21666 it->current_x = x_before;
21667 it->continuation_lines_width += x;
21668 if (!FRAME_WINDOW_P (it->f)
21669 || (row->reversed_p
21670 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21671 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21672 produce_special_glyphs (it, IT_CONTINUATION);
21673 row->continued_p = true;
21675 extend_face_to_end_of_line (it);
21677 if (nglyphs > 1 && i > 0)
21679 row->ends_in_middle_of_char_p = true;
21680 it->starts_in_middle_of_char_p = true;
21683 /* Restore the height to what it was before the
21684 element not fitting on the line. */
21685 it->max_ascent = ascent;
21686 it->max_descent = descent;
21687 it->max_phys_ascent = phys_ascent;
21688 it->max_phys_descent = phys_descent;
21691 break;
21693 else if (new_x > it->first_visible_x)
21695 /* Increment number of glyphs actually displayed. */
21696 ++it->hpos;
21698 /* Record the maximum and minimum buffer positions
21699 seen so far in glyphs that will be displayed by
21700 this row. */
21701 if (it->bidi_p)
21702 RECORD_MAX_MIN_POS (it);
21704 if (x < it->first_visible_x && !row->reversed_p)
21705 /* Glyph is partially visible, i.e. row starts at
21706 negative X position. Don't do that in R2L
21707 rows, where we arrange to add a right offset to
21708 the line in extend_face_to_end_of_line, by a
21709 suitable change to the stretch glyph that is
21710 the leftmost glyph of the line. */
21711 row->x = x - it->first_visible_x;
21712 /* When the last glyph of an R2L row only fits
21713 partially on the line, we need to set row->x to a
21714 negative offset, so that the leftmost glyph is
21715 the one that is partially visible. But if we are
21716 going to produce the truncation glyph, this will
21717 be taken care of in produce_special_glyphs. */
21718 if (row->reversed_p
21719 && new_x > it->last_visible_x
21720 && !(it->line_wrap == TRUNCATE
21721 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21723 eassert (FRAME_WINDOW_P (it->f));
21724 row->x = it->last_visible_x - new_x;
21727 else
21729 /* Glyph is completely off the left margin of the
21730 window. This should not happen because of the
21731 move_it_in_display_line at the start of this
21732 function, unless the text display area of the
21733 window is empty. */
21734 eassert (it->first_visible_x <= it->last_visible_x);
21737 /* Even if this display element produced no glyphs at all,
21738 we want to record its position. */
21739 if (it->bidi_p && nglyphs == 0)
21740 RECORD_MAX_MIN_POS (it);
21742 row->ascent = max (row->ascent, it->max_ascent);
21743 row->height = max (row->height, it->max_ascent + it->max_descent);
21744 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21745 row->phys_height = max (row->phys_height,
21746 it->max_phys_ascent + it->max_phys_descent);
21747 row->extra_line_spacing = max (row->extra_line_spacing,
21748 it->max_extra_line_spacing);
21750 /* End of this display line if row is continued. */
21751 if (row->continued_p || row->ends_at_zv_p)
21752 break;
21755 at_end_of_line:
21756 /* Is this a line end? If yes, we're also done, after making
21757 sure that a non-default face is extended up to the right
21758 margin of the window. */
21759 if (ITERATOR_AT_END_OF_LINE_P (it))
21761 int used_before = row->used[TEXT_AREA];
21763 row->ends_in_newline_from_string_p = STRINGP (it->object);
21765 /* Add a space at the end of the line that is used to
21766 display the cursor there. */
21767 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21768 append_space_for_newline (it, false);
21770 /* Extend the face to the end of the line. */
21771 extend_face_to_end_of_line (it);
21773 /* Make sure we have the position. */
21774 if (used_before == 0)
21775 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21777 /* Record the position of the newline, for use in
21778 find_row_edges. */
21779 it->eol_pos = it->current.pos;
21781 /* Consume the line end. This skips over invisible lines. */
21782 set_iterator_to_next (it, true);
21783 it->continuation_lines_width = 0;
21784 break;
21787 /* Proceed with next display element. Note that this skips
21788 over lines invisible because of selective display. */
21789 set_iterator_to_next (it, true);
21791 /* If we truncate lines, we are done when the last displayed
21792 glyphs reach past the right margin of the window. */
21793 if (it->line_wrap == TRUNCATE
21794 && ((FRAME_WINDOW_P (it->f)
21795 /* Images are preprocessed in produce_image_glyph such
21796 that they are cropped at the right edge of the
21797 window, so an image glyph will always end exactly at
21798 last_visible_x, even if there's no right fringe. */
21799 && ((row->reversed_p
21800 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21801 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21802 || it->what == IT_IMAGE))
21803 ? (it->current_x >= it->last_visible_x)
21804 : (it->current_x > it->last_visible_x)))
21806 /* Maybe add truncation glyphs. */
21807 if (!FRAME_WINDOW_P (it->f)
21808 || (row->reversed_p
21809 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21810 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21812 int i, n;
21814 if (!row->reversed_p)
21816 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21817 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21818 break;
21820 else
21822 for (i = 0; i < row->used[TEXT_AREA]; i++)
21823 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21824 break;
21825 /* Remove any padding glyphs at the front of ROW, to
21826 make room for the truncation glyphs we will be
21827 adding below. The loop below always inserts at
21828 least one truncation glyph, so also remove the
21829 last glyph added to ROW. */
21830 unproduce_glyphs (it, i + 1);
21831 /* Adjust i for the loop below. */
21832 i = row->used[TEXT_AREA] - (i + 1);
21835 /* produce_special_glyphs overwrites the last glyph, so
21836 we don't want that if we want to keep that last
21837 glyph, which means it's an image. */
21838 if (it->current_x > it->last_visible_x)
21840 it->current_x = x_before;
21841 if (!FRAME_WINDOW_P (it->f))
21843 for (n = row->used[TEXT_AREA]; i < n; ++i)
21845 row->used[TEXT_AREA] = i;
21846 produce_special_glyphs (it, IT_TRUNCATION);
21849 else
21851 row->used[TEXT_AREA] = i;
21852 produce_special_glyphs (it, IT_TRUNCATION);
21854 it->hpos = hpos_before;
21857 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21859 /* Don't truncate if we can overflow newline into fringe. */
21860 if (!get_next_display_element (it))
21862 it->continuation_lines_width = 0;
21863 it->font_height = Qnil;
21864 it->voffset = 0;
21865 row->ends_at_zv_p = true;
21866 row->exact_window_width_line_p = true;
21867 break;
21869 if (ITERATOR_AT_END_OF_LINE_P (it))
21871 row->exact_window_width_line_p = true;
21872 goto at_end_of_line;
21874 it->current_x = x_before;
21875 it->hpos = hpos_before;
21878 row->truncated_on_right_p = true;
21879 it->continuation_lines_width = 0;
21880 reseat_at_next_visible_line_start (it, false);
21881 /* We insist below that IT's position be at ZV because in
21882 bidi-reordered lines the character at visible line start
21883 might not be the character that follows the newline in
21884 the logical order. */
21885 if (IT_BYTEPOS (*it) > BEG_BYTE)
21886 row->ends_at_zv_p =
21887 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21888 else
21889 row->ends_at_zv_p = false;
21890 break;
21894 if (wrap_data)
21895 bidi_unshelve_cache (wrap_data, true);
21897 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21898 at the left window margin. */
21899 if (it->first_visible_x
21900 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21902 if (!FRAME_WINDOW_P (it->f)
21903 || (((row->reversed_p
21904 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21905 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21906 /* Don't let insert_left_trunc_glyphs overwrite the
21907 first glyph of the row if it is an image. */
21908 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21909 insert_left_trunc_glyphs (it);
21910 row->truncated_on_left_p = true;
21913 /* Remember the position at which this line ends.
21915 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21916 cannot be before the call to find_row_edges below, since that is
21917 where these positions are determined. */
21918 row->end = it->current;
21919 if (!it->bidi_p)
21921 row->minpos = row->start.pos;
21922 row->maxpos = row->end.pos;
21924 else
21926 /* ROW->minpos and ROW->maxpos must be the smallest and
21927 `1 + the largest' buffer positions in ROW. But if ROW was
21928 bidi-reordered, these two positions can be anywhere in the
21929 row, so we must determine them now. */
21930 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21933 /* If the start of this line is the overlay arrow-position, then
21934 mark this glyph row as the one containing the overlay arrow.
21935 This is clearly a mess with variable size fonts. It would be
21936 better to let it be displayed like cursors under X. */
21937 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21938 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21939 !NILP (overlay_arrow_string)))
21941 /* Overlay arrow in window redisplay is a fringe bitmap. */
21942 if (STRINGP (overlay_arrow_string))
21944 struct glyph_row *arrow_row
21945 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21946 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21947 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21948 struct glyph *p = row->glyphs[TEXT_AREA];
21949 struct glyph *p2, *end;
21951 /* Copy the arrow glyphs. */
21952 while (glyph < arrow_end)
21953 *p++ = *glyph++;
21955 /* Throw away padding glyphs. */
21956 p2 = p;
21957 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21958 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21959 ++p2;
21960 if (p2 > p)
21962 while (p2 < end)
21963 *p++ = *p2++;
21964 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21967 else
21969 eassert (INTEGERP (overlay_arrow_string));
21970 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21972 overlay_arrow_seen = true;
21975 /* Highlight trailing whitespace. */
21976 if (!NILP (Vshow_trailing_whitespace))
21977 highlight_trailing_whitespace (it->f, it->glyph_row);
21979 /* Compute pixel dimensions of this line. */
21980 compute_line_metrics (it);
21982 /* Implementation note: No changes in the glyphs of ROW or in their
21983 faces can be done past this point, because compute_line_metrics
21984 computes ROW's hash value and stores it within the glyph_row
21985 structure. */
21987 /* Record whether this row ends inside an ellipsis. */
21988 row->ends_in_ellipsis_p
21989 = (it->method == GET_FROM_DISPLAY_VECTOR
21990 && it->ellipsis_p);
21992 /* Save fringe bitmaps in this row. */
21993 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21994 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21995 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21996 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21998 it->left_user_fringe_bitmap = 0;
21999 it->left_user_fringe_face_id = 0;
22000 it->right_user_fringe_bitmap = 0;
22001 it->right_user_fringe_face_id = 0;
22003 /* Maybe set the cursor. */
22004 cvpos = it->w->cursor.vpos;
22005 if ((cvpos < 0
22006 /* In bidi-reordered rows, keep checking for proper cursor
22007 position even if one has been found already, because buffer
22008 positions in such rows change non-linearly with ROW->VPOS,
22009 when a line is continued. One exception: when we are at ZV,
22010 display cursor on the first suitable glyph row, since all
22011 the empty rows after that also have their position set to ZV. */
22012 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22013 lines' rows is implemented for bidi-reordered rows. */
22014 || (it->bidi_p
22015 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22016 && PT >= MATRIX_ROW_START_CHARPOS (row)
22017 && PT <= MATRIX_ROW_END_CHARPOS (row)
22018 && cursor_row_p (row))
22019 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22021 /* Prepare for the next line. This line starts horizontally at (X
22022 HPOS) = (0 0). Vertical positions are incremented. As a
22023 convenience for the caller, IT->glyph_row is set to the next
22024 row to be used. */
22025 it->current_x = it->hpos = 0;
22026 it->current_y += row->height;
22027 /* Restore the first and last visible X if we adjusted them for
22028 current-line hscrolling. */
22029 if (hscroll_this_line)
22031 it->first_visible_x = first_visible_x;
22032 it->last_visible_x = last_visible_x;
22034 SET_TEXT_POS (it->eol_pos, 0, 0);
22035 ++it->vpos;
22036 ++it->glyph_row;
22037 /* The next row should by default use the same value of the
22038 reversed_p flag as this one. set_iterator_to_next decides when
22039 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22040 the flag accordingly. */
22041 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22042 it->glyph_row->reversed_p = row->reversed_p;
22043 it->start = row->end;
22044 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22046 #undef RECORD_MAX_MIN_POS
22049 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22050 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22051 doc: /* Return paragraph direction at point in BUFFER.
22052 Value is either `left-to-right' or `right-to-left'.
22053 If BUFFER is omitted or nil, it defaults to the current buffer.
22055 Paragraph direction determines how the text in the paragraph is displayed.
22056 In left-to-right paragraphs, text begins at the left margin of the window
22057 and the reading direction is generally left to right. In right-to-left
22058 paragraphs, text begins at the right margin and is read from right to left.
22060 See also `bidi-paragraph-direction'. */)
22061 (Lisp_Object buffer)
22063 struct buffer *buf = current_buffer;
22064 struct buffer *old = buf;
22066 if (! NILP (buffer))
22068 CHECK_BUFFER (buffer);
22069 buf = XBUFFER (buffer);
22072 if (NILP (BVAR (buf, bidi_display_reordering))
22073 || NILP (BVAR (buf, enable_multibyte_characters))
22074 /* When we are loading loadup.el, the character property tables
22075 needed for bidi iteration are not yet available. */
22076 || redisplay__inhibit_bidi)
22077 return Qleft_to_right;
22078 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22079 return BVAR (buf, bidi_paragraph_direction);
22080 else
22082 /* Determine the direction from buffer text. We could try to
22083 use current_matrix if it is up to date, but this seems fast
22084 enough as it is. */
22085 struct bidi_it itb;
22086 ptrdiff_t pos = BUF_PT (buf);
22087 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22088 int c;
22089 void *itb_data = bidi_shelve_cache ();
22091 set_buffer_temp (buf);
22092 /* bidi_paragraph_init finds the base direction of the paragraph
22093 by searching forward from paragraph start. We need the base
22094 direction of the current or _previous_ paragraph, so we need
22095 to make sure we are within that paragraph. To that end, find
22096 the previous non-empty line. */
22097 if (pos >= ZV && pos > BEGV)
22098 DEC_BOTH (pos, bytepos);
22099 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22100 if (fast_looking_at (trailing_white_space,
22101 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22103 while ((c = FETCH_BYTE (bytepos)) == '\n'
22104 || c == ' ' || c == '\t' || c == '\f')
22106 if (bytepos <= BEGV_BYTE)
22107 break;
22108 bytepos--;
22109 pos--;
22111 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22112 bytepos--;
22114 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22115 itb.paragraph_dir = NEUTRAL_DIR;
22116 itb.string.s = NULL;
22117 itb.string.lstring = Qnil;
22118 itb.string.bufpos = 0;
22119 itb.string.from_disp_str = false;
22120 itb.string.unibyte = false;
22121 /* We have no window to use here for ignoring window-specific
22122 overlays. Using NULL for window pointer will cause
22123 compute_display_string_pos to use the current buffer. */
22124 itb.w = NULL;
22125 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22126 bidi_unshelve_cache (itb_data, false);
22127 set_buffer_temp (old);
22128 switch (itb.paragraph_dir)
22130 case L2R:
22131 return Qleft_to_right;
22132 break;
22133 case R2L:
22134 return Qright_to_left;
22135 break;
22136 default:
22137 emacs_abort ();
22142 DEFUN ("bidi-find-overridden-directionality",
22143 Fbidi_find_overridden_directionality,
22144 Sbidi_find_overridden_directionality, 2, 3, 0,
22145 doc: /* Return position between FROM and TO where directionality was overridden.
22147 This function returns the first character position in the specified
22148 region of OBJECT where there is a character whose `bidi-class' property
22149 is `L', but which was forced to display as `R' by a directional
22150 override, and likewise with characters whose `bidi-class' is `R'
22151 or `AL' that were forced to display as `L'.
22153 If no such character is found, the function returns nil.
22155 OBJECT is a Lisp string or buffer to search for overridden
22156 directionality, and defaults to the current buffer if nil or omitted.
22157 OBJECT can also be a window, in which case the function will search
22158 the buffer displayed in that window. Passing the window instead of
22159 a buffer is preferable when the buffer is displayed in some window,
22160 because this function will then be able to correctly account for
22161 window-specific overlays, which can affect the results.
22163 Strong directional characters `L', `R', and `AL' can have their
22164 intrinsic directionality overridden by directional override
22165 control characters RLO (u+202e) and LRO (u+202d). See the
22166 function `get-char-code-property' for a way to inquire about
22167 the `bidi-class' property of a character. */)
22168 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22170 struct buffer *buf = current_buffer;
22171 struct buffer *old = buf;
22172 struct window *w = NULL;
22173 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22174 struct bidi_it itb;
22175 ptrdiff_t from_pos, to_pos, from_bpos;
22176 void *itb_data;
22178 if (!NILP (object))
22180 if (BUFFERP (object))
22181 buf = XBUFFER (object);
22182 else if (WINDOWP (object))
22184 w = decode_live_window (object);
22185 buf = XBUFFER (w->contents);
22186 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22188 else
22189 CHECK_STRING (object);
22192 if (STRINGP (object))
22194 /* Characters in unibyte strings are always treated by bidi.c as
22195 strong LTR. */
22196 if (!STRING_MULTIBYTE (object)
22197 /* When we are loading loadup.el, the character property
22198 tables needed for bidi iteration are not yet
22199 available. */
22200 || redisplay__inhibit_bidi)
22201 return Qnil;
22203 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22204 if (from_pos >= SCHARS (object))
22205 return Qnil;
22207 /* Set up the bidi iterator. */
22208 itb_data = bidi_shelve_cache ();
22209 itb.paragraph_dir = NEUTRAL_DIR;
22210 itb.string.lstring = object;
22211 itb.string.s = NULL;
22212 itb.string.schars = SCHARS (object);
22213 itb.string.bufpos = 0;
22214 itb.string.from_disp_str = false;
22215 itb.string.unibyte = false;
22216 itb.w = w;
22217 bidi_init_it (0, 0, frame_window_p, &itb);
22219 else
22221 /* Nothing this fancy can happen in unibyte buffers, or in a
22222 buffer that disabled reordering, or if FROM is at EOB. */
22223 if (NILP (BVAR (buf, bidi_display_reordering))
22224 || NILP (BVAR (buf, enable_multibyte_characters))
22225 /* When we are loading loadup.el, the character property
22226 tables needed for bidi iteration are not yet
22227 available. */
22228 || redisplay__inhibit_bidi)
22229 return Qnil;
22231 set_buffer_temp (buf);
22232 validate_region (&from, &to);
22233 from_pos = XINT (from);
22234 to_pos = XINT (to);
22235 if (from_pos >= ZV)
22236 return Qnil;
22238 /* Set up the bidi iterator. */
22239 itb_data = bidi_shelve_cache ();
22240 from_bpos = CHAR_TO_BYTE (from_pos);
22241 if (from_pos == BEGV)
22243 itb.charpos = BEGV;
22244 itb.bytepos = BEGV_BYTE;
22246 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22248 itb.charpos = from_pos;
22249 itb.bytepos = from_bpos;
22251 else
22252 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22253 -1, &itb.bytepos);
22254 itb.paragraph_dir = NEUTRAL_DIR;
22255 itb.string.s = NULL;
22256 itb.string.lstring = Qnil;
22257 itb.string.bufpos = 0;
22258 itb.string.from_disp_str = false;
22259 itb.string.unibyte = false;
22260 itb.w = w;
22261 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22264 ptrdiff_t found;
22265 do {
22266 /* For the purposes of this function, the actual base direction of
22267 the paragraph doesn't matter, so just set it to L2R. */
22268 bidi_paragraph_init (L2R, &itb, false);
22269 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22271 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22273 bidi_unshelve_cache (itb_data, false);
22274 set_buffer_temp (old);
22276 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22279 DEFUN ("move-point-visually", Fmove_point_visually,
22280 Smove_point_visually, 1, 1, 0,
22281 doc: /* Move point in the visual order in the specified DIRECTION.
22282 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22283 left.
22285 Value is the new character position of point. */)
22286 (Lisp_Object direction)
22288 struct window *w = XWINDOW (selected_window);
22289 struct buffer *b = XBUFFER (w->contents);
22290 struct glyph_row *row;
22291 int dir;
22292 Lisp_Object paragraph_dir;
22294 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22295 (!(ROW)->continued_p \
22296 && NILP ((GLYPH)->object) \
22297 && (GLYPH)->type == CHAR_GLYPH \
22298 && (GLYPH)->u.ch == ' ' \
22299 && (GLYPH)->charpos >= 0 \
22300 && !(GLYPH)->avoid_cursor_p)
22302 CHECK_NUMBER (direction);
22303 dir = XINT (direction);
22304 if (dir > 0)
22305 dir = 1;
22306 else
22307 dir = -1;
22309 /* If current matrix is up-to-date, we can use the information
22310 recorded in the glyphs, at least as long as the goal is on the
22311 screen. */
22312 if (w->window_end_valid
22313 && !windows_or_buffers_changed
22314 && b
22315 && !b->clip_changed
22316 && !b->prevent_redisplay_optimizations_p
22317 && !window_outdated (w)
22318 /* We rely below on the cursor coordinates to be up to date, but
22319 we cannot trust them if some command moved point since the
22320 last complete redisplay. */
22321 && w->last_point == BUF_PT (b)
22322 && w->cursor.vpos >= 0
22323 && w->cursor.vpos < w->current_matrix->nrows
22324 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22326 struct glyph *g = row->glyphs[TEXT_AREA];
22327 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22328 struct glyph *gpt = g + w->cursor.hpos;
22330 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22332 if (BUFFERP (g->object) && g->charpos != PT)
22334 SET_PT (g->charpos);
22335 w->cursor.vpos = -1;
22336 return make_number (PT);
22338 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22340 ptrdiff_t new_pos;
22342 if (BUFFERP (gpt->object))
22344 new_pos = PT;
22345 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22346 new_pos += (row->reversed_p ? -dir : dir);
22347 else
22348 new_pos -= (row->reversed_p ? -dir : dir);
22350 else if (BUFFERP (g->object))
22351 new_pos = g->charpos;
22352 else
22353 break;
22354 SET_PT (new_pos);
22355 w->cursor.vpos = -1;
22356 return make_number (PT);
22358 else if (ROW_GLYPH_NEWLINE_P (row, g))
22360 /* Glyphs inserted at the end of a non-empty line for
22361 positioning the cursor have zero charpos, so we must
22362 deduce the value of point by other means. */
22363 if (g->charpos > 0)
22364 SET_PT (g->charpos);
22365 else if (row->ends_at_zv_p && PT != ZV)
22366 SET_PT (ZV);
22367 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22368 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22369 else
22370 break;
22371 w->cursor.vpos = -1;
22372 return make_number (PT);
22375 if (g == e || NILP (g->object))
22377 if (row->truncated_on_left_p || row->truncated_on_right_p)
22378 goto simulate_display;
22379 if (!row->reversed_p)
22380 row += dir;
22381 else
22382 row -= dir;
22383 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
22384 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
22385 goto simulate_display;
22387 if (dir > 0)
22389 if (row->reversed_p && !row->continued_p)
22391 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22392 w->cursor.vpos = -1;
22393 return make_number (PT);
22395 g = row->glyphs[TEXT_AREA];
22396 e = g + row->used[TEXT_AREA];
22397 for ( ; g < e; g++)
22399 if (BUFFERP (g->object)
22400 /* Empty lines have only one glyph, which stands
22401 for the newline, and whose charpos is the
22402 buffer position of the newline. */
22403 || ROW_GLYPH_NEWLINE_P (row, g)
22404 /* When the buffer ends in a newline, the line at
22405 EOB also has one glyph, but its charpos is -1. */
22406 || (row->ends_at_zv_p
22407 && !row->reversed_p
22408 && NILP (g->object)
22409 && g->type == CHAR_GLYPH
22410 && g->u.ch == ' '))
22412 if (g->charpos > 0)
22413 SET_PT (g->charpos);
22414 else if (!row->reversed_p
22415 && row->ends_at_zv_p
22416 && PT != ZV)
22417 SET_PT (ZV);
22418 else
22419 continue;
22420 w->cursor.vpos = -1;
22421 return make_number (PT);
22425 else
22427 if (!row->reversed_p && !row->continued_p)
22429 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22430 w->cursor.vpos = -1;
22431 return make_number (PT);
22433 e = row->glyphs[TEXT_AREA];
22434 g = e + row->used[TEXT_AREA] - 1;
22435 for ( ; g >= e; g--)
22437 if (BUFFERP (g->object)
22438 || (ROW_GLYPH_NEWLINE_P (row, g)
22439 && g->charpos > 0)
22440 /* Empty R2L lines on GUI frames have the buffer
22441 position of the newline stored in the stretch
22442 glyph. */
22443 || g->type == STRETCH_GLYPH
22444 || (row->ends_at_zv_p
22445 && row->reversed_p
22446 && NILP (g->object)
22447 && g->type == CHAR_GLYPH
22448 && g->u.ch == ' '))
22450 if (g->charpos > 0)
22451 SET_PT (g->charpos);
22452 else if (row->reversed_p
22453 && row->ends_at_zv_p
22454 && PT != ZV)
22455 SET_PT (ZV);
22456 else
22457 continue;
22458 w->cursor.vpos = -1;
22459 return make_number (PT);
22466 simulate_display:
22468 /* If we wind up here, we failed to move by using the glyphs, so we
22469 need to simulate display instead. */
22471 if (b)
22472 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22473 else
22474 paragraph_dir = Qleft_to_right;
22475 if (EQ (paragraph_dir, Qright_to_left))
22476 dir = -dir;
22477 if (PT <= BEGV && dir < 0)
22478 xsignal0 (Qbeginning_of_buffer);
22479 else if (PT >= ZV && dir > 0)
22480 xsignal0 (Qend_of_buffer);
22481 else
22483 struct text_pos pt;
22484 struct it it;
22485 int pt_x, target_x, pixel_width, pt_vpos;
22486 bool at_eol_p;
22487 bool overshoot_expected = false;
22488 bool target_is_eol_p = false;
22490 /* Setup the arena. */
22491 SET_TEXT_POS (pt, PT, PT_BYTE);
22492 start_display (&it, w, pt);
22493 /* When lines are truncated, we could be called with point
22494 outside of the windows edges, in which case move_it_*
22495 functions either prematurely stop at window's edge or jump to
22496 the next screen line, whereas we rely below on our ability to
22497 reach point, in order to start from its X coordinate. So we
22498 need to disregard the window's horizontal extent in that case. */
22499 if (it.line_wrap == TRUNCATE)
22500 it.last_visible_x = DISP_INFINITY;
22502 if (it.cmp_it.id < 0
22503 && it.method == GET_FROM_STRING
22504 && it.area == TEXT_AREA
22505 && it.string_from_display_prop_p
22506 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22507 overshoot_expected = true;
22509 /* Find the X coordinate of point. We start from the beginning
22510 of this or previous line to make sure we are before point in
22511 the logical order (since the move_it_* functions can only
22512 move forward). */
22513 reseat:
22514 reseat_at_previous_visible_line_start (&it);
22515 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22516 if (IT_CHARPOS (it) != PT)
22518 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22519 -1, -1, -1, MOVE_TO_POS);
22520 /* If we missed point because the character there is
22521 displayed out of a display vector that has more than one
22522 glyph, retry expecting overshoot. */
22523 if (it.method == GET_FROM_DISPLAY_VECTOR
22524 && it.current.dpvec_index > 0
22525 && !overshoot_expected)
22527 overshoot_expected = true;
22528 goto reseat;
22530 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22531 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22533 pt_x = it.current_x;
22534 pt_vpos = it.vpos;
22535 if (dir > 0 || overshoot_expected)
22537 struct glyph_row *row = it.glyph_row;
22539 /* When point is at beginning of line, we don't have
22540 information about the glyph there loaded into struct
22541 it. Calling get_next_display_element fixes that. */
22542 if (pt_x == 0)
22543 get_next_display_element (&it);
22544 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22545 it.glyph_row = NULL;
22546 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22547 it.glyph_row = row;
22548 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22549 it, lest it will become out of sync with it's buffer
22550 position. */
22551 it.current_x = pt_x;
22553 else
22554 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22555 pixel_width = it.pixel_width;
22556 if (overshoot_expected && at_eol_p)
22557 pixel_width = 0;
22558 else if (pixel_width <= 0)
22559 pixel_width = 1;
22561 /* If there's a display string (or something similar) at point,
22562 we are actually at the glyph to the left of point, so we need
22563 to correct the X coordinate. */
22564 if (overshoot_expected)
22566 if (it.bidi_p)
22567 pt_x += pixel_width * it.bidi_it.scan_dir;
22568 else
22569 pt_x += pixel_width;
22572 /* Compute target X coordinate, either to the left or to the
22573 right of point. On TTY frames, all characters have the same
22574 pixel width of 1, so we can use that. On GUI frames we don't
22575 have an easy way of getting at the pixel width of the
22576 character to the left of point, so we use a different method
22577 of getting to that place. */
22578 if (dir > 0)
22579 target_x = pt_x + pixel_width;
22580 else
22581 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22583 /* Target X coordinate could be one line above or below the line
22584 of point, in which case we need to adjust the target X
22585 coordinate. Also, if moving to the left, we need to begin at
22586 the left edge of the point's screen line. */
22587 if (dir < 0)
22589 if (pt_x > 0)
22591 start_display (&it, w, pt);
22592 if (it.line_wrap == TRUNCATE)
22593 it.last_visible_x = DISP_INFINITY;
22594 reseat_at_previous_visible_line_start (&it);
22595 it.current_x = it.current_y = it.hpos = 0;
22596 if (pt_vpos != 0)
22597 move_it_by_lines (&it, pt_vpos);
22599 else
22601 move_it_by_lines (&it, -1);
22602 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22603 target_is_eol_p = true;
22604 /* Under word-wrap, we don't know the x coordinate of
22605 the last character displayed on the previous line,
22606 which immediately precedes the wrap point. To find
22607 out its x coordinate, we try moving to the right
22608 margin of the window, which will stop at the wrap
22609 point, and then reset target_x to point at the
22610 character that precedes the wrap point. This is not
22611 needed on GUI frames, because (see below) there we
22612 move from the left margin one grapheme cluster at a
22613 time, and stop when we hit the wrap point. */
22614 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22616 void *it_data = NULL;
22617 struct it it2;
22619 SAVE_IT (it2, it, it_data);
22620 move_it_in_display_line_to (&it, ZV, target_x,
22621 MOVE_TO_POS | MOVE_TO_X);
22622 /* If we arrived at target_x, that _is_ the last
22623 character on the previous line. */
22624 if (it.current_x != target_x)
22625 target_x = it.current_x - 1;
22626 RESTORE_IT (&it, &it2, it_data);
22630 else
22632 if (at_eol_p
22633 || (target_x >= it.last_visible_x
22634 && it.line_wrap != TRUNCATE))
22636 if (pt_x > 0)
22637 move_it_by_lines (&it, 0);
22638 move_it_by_lines (&it, 1);
22639 target_x = 0;
22643 /* Move to the target X coordinate. */
22644 /* On GUI frames, as we don't know the X coordinate of the
22645 character to the left of point, moving point to the left
22646 requires walking, one grapheme cluster at a time, until we
22647 find ourself at a place immediately to the left of the
22648 character at point. */
22649 if (FRAME_WINDOW_P (it.f) && dir < 0)
22651 struct text_pos new_pos;
22652 enum move_it_result rc = MOVE_X_REACHED;
22654 if (it.current_x == 0)
22655 get_next_display_element (&it);
22656 if (it.what == IT_COMPOSITION)
22658 new_pos.charpos = it.cmp_it.charpos;
22659 new_pos.bytepos = -1;
22661 else
22662 new_pos = it.current.pos;
22664 while (it.current_x + it.pixel_width <= target_x
22665 && (rc == MOVE_X_REACHED
22666 /* Under word-wrap, move_it_in_display_line_to
22667 stops at correct coordinates, but sometimes
22668 returns MOVE_POS_MATCH_OR_ZV. */
22669 || (it.line_wrap == WORD_WRAP
22670 && rc == MOVE_POS_MATCH_OR_ZV)))
22672 int new_x = it.current_x + it.pixel_width;
22674 /* For composed characters, we want the position of the
22675 first character in the grapheme cluster (usually, the
22676 composition's base character), whereas it.current
22677 might give us the position of the _last_ one, e.g. if
22678 the composition is rendered in reverse due to bidi
22679 reordering. */
22680 if (it.what == IT_COMPOSITION)
22682 new_pos.charpos = it.cmp_it.charpos;
22683 new_pos.bytepos = -1;
22685 else
22686 new_pos = it.current.pos;
22687 if (new_x == it.current_x)
22688 new_x++;
22689 rc = move_it_in_display_line_to (&it, ZV, new_x,
22690 MOVE_TO_POS | MOVE_TO_X);
22691 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22692 break;
22694 /* The previous position we saw in the loop is the one we
22695 want. */
22696 if (new_pos.bytepos == -1)
22697 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22698 it.current.pos = new_pos;
22700 else if (it.current_x != target_x)
22701 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22703 /* If we ended up in a display string that covers point, move to
22704 buffer position to the right in the visual order. */
22705 if (dir > 0)
22707 while (IT_CHARPOS (it) == PT)
22709 set_iterator_to_next (&it, false);
22710 if (!get_next_display_element (&it))
22711 break;
22715 /* Move point to that position. */
22716 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22719 return make_number (PT);
22721 #undef ROW_GLYPH_NEWLINE_P
22724 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22725 Sbidi_resolved_levels, 0, 1, 0,
22726 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22728 The resolved levels are produced by the Emacs bidi reordering engine
22729 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22730 read the Unicode Standard Annex 9 (UAX#9) for background information
22731 about these levels.
22733 VPOS is the zero-based number of the current window's screen line
22734 for which to produce the resolved levels. If VPOS is nil or omitted,
22735 it defaults to the screen line of point. If the window displays a
22736 header line, VPOS of zero will report on the header line, and first
22737 line of text in the window will have VPOS of 1.
22739 Value is an array of resolved levels, indexed by glyph number.
22740 Glyphs are numbered from zero starting from the beginning of the
22741 screen line, i.e. the left edge of the window for left-to-right lines
22742 and from the right edge for right-to-left lines. The resolved levels
22743 are produced only for the window's text area; text in display margins
22744 is not included.
22746 If the selected window's display is not up-to-date, or if the specified
22747 screen line does not display text, this function returns nil. It is
22748 highly recommended to bind this function to some simple key, like F8,
22749 in order to avoid these problems.
22751 This function exists mainly for testing the correctness of the
22752 Emacs UBA implementation, in particular with the test suite. */)
22753 (Lisp_Object vpos)
22755 struct window *w = XWINDOW (selected_window);
22756 struct buffer *b = XBUFFER (w->contents);
22757 int nrow;
22758 struct glyph_row *row;
22760 if (NILP (vpos))
22762 int d1, d2, d3, d4, d5;
22764 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22766 else
22768 CHECK_NUMBER_COERCE_MARKER (vpos);
22769 nrow = XINT (vpos);
22772 /* We require up-to-date glyph matrix for this window. */
22773 if (w->window_end_valid
22774 && !windows_or_buffers_changed
22775 && b
22776 && !b->clip_changed
22777 && !b->prevent_redisplay_optimizations_p
22778 && !window_outdated (w)
22779 && nrow >= 0
22780 && nrow < w->current_matrix->nrows
22781 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22782 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22784 struct glyph *g, *e, *g1;
22785 int nglyphs, i;
22786 Lisp_Object levels;
22788 if (!row->reversed_p) /* Left-to-right glyph row. */
22790 g = g1 = row->glyphs[TEXT_AREA];
22791 e = g + row->used[TEXT_AREA];
22793 /* Skip over glyphs at the start of the row that was
22794 generated by redisplay for its own needs. */
22795 while (g < e
22796 && NILP (g->object)
22797 && g->charpos < 0)
22798 g++;
22799 g1 = g;
22801 /* Count the "interesting" glyphs in this row. */
22802 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22803 nglyphs++;
22805 /* Create and fill the array. */
22806 levels = make_uninit_vector (nglyphs);
22807 for (i = 0; g1 < g; i++, g1++)
22808 ASET (levels, i, make_number (g1->resolved_level));
22810 else /* Right-to-left glyph row. */
22812 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22813 e = row->glyphs[TEXT_AREA] - 1;
22814 while (g > e
22815 && NILP (g->object)
22816 && g->charpos < 0)
22817 g--;
22818 g1 = g;
22819 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22820 nglyphs++;
22821 levels = make_uninit_vector (nglyphs);
22822 for (i = 0; g1 > g; i++, g1--)
22823 ASET (levels, i, make_number (g1->resolved_level));
22825 return levels;
22827 else
22828 return Qnil;
22833 /***********************************************************************
22834 Menu Bar
22835 ***********************************************************************/
22837 /* Redisplay the menu bar in the frame for window W.
22839 The menu bar of X frames that don't have X toolkit support is
22840 displayed in a special window W->frame->menu_bar_window.
22842 The menu bar of terminal frames is treated specially as far as
22843 glyph matrices are concerned. Menu bar lines are not part of
22844 windows, so the update is done directly on the frame matrix rows
22845 for the menu bar. */
22847 static void
22848 display_menu_bar (struct window *w)
22850 struct frame *f = XFRAME (WINDOW_FRAME (w));
22851 struct it it;
22852 Lisp_Object items;
22853 int i;
22855 /* Don't do all this for graphical frames. */
22856 #ifdef HAVE_NTGUI
22857 if (FRAME_W32_P (f))
22858 return;
22859 #endif
22860 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22861 if (FRAME_X_P (f))
22862 return;
22863 #endif
22865 #ifdef HAVE_NS
22866 if (FRAME_NS_P (f))
22867 return;
22868 #endif /* HAVE_NS */
22870 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22871 eassert (!FRAME_WINDOW_P (f));
22872 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22873 it.first_visible_x = 0;
22874 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22875 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22876 if (FRAME_WINDOW_P (f))
22878 /* Menu bar lines are displayed in the desired matrix of the
22879 dummy window menu_bar_window. */
22880 struct window *menu_w;
22881 menu_w = XWINDOW (f->menu_bar_window);
22882 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22883 MENU_FACE_ID);
22884 it.first_visible_x = 0;
22885 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22887 else
22888 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22890 /* This is a TTY frame, i.e. character hpos/vpos are used as
22891 pixel x/y. */
22892 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22893 MENU_FACE_ID);
22894 it.first_visible_x = 0;
22895 it.last_visible_x = FRAME_COLS (f);
22898 /* FIXME: This should be controlled by a user option. See the
22899 comments in redisplay_tool_bar and display_mode_line about
22900 this. */
22901 it.paragraph_embedding = L2R;
22903 /* Clear all rows of the menu bar. */
22904 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22906 struct glyph_row *row = it.glyph_row + i;
22907 clear_glyph_row (row);
22908 row->enabled_p = true;
22909 row->full_width_p = true;
22910 row->reversed_p = false;
22913 /* Display all items of the menu bar. */
22914 items = FRAME_MENU_BAR_ITEMS (it.f);
22915 for (i = 0; i < ASIZE (items); i += 4)
22917 Lisp_Object string;
22919 /* Stop at nil string. */
22920 string = AREF (items, i + 1);
22921 if (NILP (string))
22922 break;
22924 /* Remember where item was displayed. */
22925 ASET (items, i + 3, make_number (it.hpos));
22927 /* Display the item, pad with one space. */
22928 if (it.current_x < it.last_visible_x)
22929 display_string (NULL, string, Qnil, 0, 0, &it,
22930 SCHARS (string) + 1, 0, 0, -1);
22933 /* Fill out the line with spaces. */
22934 if (it.current_x < it.last_visible_x)
22935 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22937 /* Compute the total height of the lines. */
22938 compute_line_metrics (&it);
22941 /* Deep copy of a glyph row, including the glyphs. */
22942 static void
22943 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22945 struct glyph *pointers[1 + LAST_AREA];
22946 int to_used = to->used[TEXT_AREA];
22948 /* Save glyph pointers of TO. */
22949 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22951 /* Do a structure assignment. */
22952 *to = *from;
22954 /* Restore original glyph pointers of TO. */
22955 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22957 /* Copy the glyphs. */
22958 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22959 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22961 /* If we filled only part of the TO row, fill the rest with
22962 space_glyph (which will display as empty space). */
22963 if (to_used > from->used[TEXT_AREA])
22964 fill_up_frame_row_with_spaces (to, to_used);
22967 /* Display one menu item on a TTY, by overwriting the glyphs in the
22968 frame F's desired glyph matrix with glyphs produced from the menu
22969 item text. Called from term.c to display TTY drop-down menus one
22970 item at a time.
22972 ITEM_TEXT is the menu item text as a C string.
22974 FACE_ID is the face ID to be used for this menu item. FACE_ID
22975 could specify one of 3 faces: a face for an enabled item, a face
22976 for a disabled item, or a face for a selected item.
22978 X and Y are coordinates of the first glyph in the frame's desired
22979 matrix to be overwritten by the menu item. Since this is a TTY, Y
22980 is the zero-based number of the glyph row and X is the zero-based
22981 glyph number in the row, starting from left, where to start
22982 displaying the item.
22984 SUBMENU means this menu item drops down a submenu, which
22985 should be indicated by displaying a proper visual cue after the
22986 item text. */
22988 void
22989 display_tty_menu_item (const char *item_text, int width, int face_id,
22990 int x, int y, bool submenu)
22992 struct it it;
22993 struct frame *f = SELECTED_FRAME ();
22994 struct window *w = XWINDOW (f->selected_window);
22995 struct glyph_row *row;
22996 size_t item_len = strlen (item_text);
22998 eassert (FRAME_TERMCAP_P (f));
23000 /* Don't write beyond the matrix's last row. This can happen for
23001 TTY screens that are not high enough to show the entire menu.
23002 (This is actually a bit of defensive programming, as
23003 tty_menu_display already limits the number of menu items to one
23004 less than the number of screen lines.) */
23005 if (y >= f->desired_matrix->nrows)
23006 return;
23008 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23009 it.first_visible_x = 0;
23010 it.last_visible_x = FRAME_COLS (f) - 1;
23011 row = it.glyph_row;
23012 /* Start with the row contents from the current matrix. */
23013 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23014 bool saved_width = row->full_width_p;
23015 row->full_width_p = true;
23016 bool saved_reversed = row->reversed_p;
23017 row->reversed_p = false;
23018 row->enabled_p = true;
23020 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23021 desired face. */
23022 eassert (x < f->desired_matrix->matrix_w);
23023 it.current_x = it.hpos = x;
23024 it.current_y = it.vpos = y;
23025 int saved_used = row->used[TEXT_AREA];
23026 bool saved_truncated = row->truncated_on_right_p;
23027 row->used[TEXT_AREA] = x;
23028 it.face_id = face_id;
23029 it.line_wrap = TRUNCATE;
23031 /* FIXME: This should be controlled by a user option. See the
23032 comments in redisplay_tool_bar and display_mode_line about this.
23033 Also, if paragraph_embedding could ever be R2L, changes will be
23034 needed to avoid shifting to the right the row characters in
23035 term.c:append_glyph. */
23036 it.paragraph_embedding = L2R;
23038 /* Pad with a space on the left. */
23039 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23040 width--;
23041 /* Display the menu item, pad with spaces to WIDTH. */
23042 if (submenu)
23044 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23045 item_len, 0, FRAME_COLS (f) - 1, -1);
23046 width -= item_len;
23047 /* Indicate with " >" that there's a submenu. */
23048 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23049 FRAME_COLS (f) - 1, -1);
23051 else
23052 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23053 width, 0, FRAME_COLS (f) - 1, -1);
23055 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23056 row->truncated_on_right_p = saved_truncated;
23057 row->hash = row_hash (row);
23058 row->full_width_p = saved_width;
23059 row->reversed_p = saved_reversed;
23062 /***********************************************************************
23063 Mode Line
23064 ***********************************************************************/
23066 /* Redisplay mode lines in the window tree whose root is WINDOW.
23067 If FORCE, redisplay mode lines unconditionally.
23068 Otherwise, redisplay only mode lines that are garbaged. Value is
23069 the number of windows whose mode lines were redisplayed. */
23071 static int
23072 redisplay_mode_lines (Lisp_Object window, bool force)
23074 int nwindows = 0;
23076 while (!NILP (window))
23078 struct window *w = XWINDOW (window);
23080 if (WINDOWP (w->contents))
23081 nwindows += redisplay_mode_lines (w->contents, force);
23082 else if (force
23083 || FRAME_GARBAGED_P (XFRAME (w->frame))
23084 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23086 struct text_pos lpoint;
23087 struct buffer *old = current_buffer;
23089 /* Set the window's buffer for the mode line display. */
23090 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23091 set_buffer_internal_1 (XBUFFER (w->contents));
23093 /* Point refers normally to the selected window. For any
23094 other window, set up appropriate value. */
23095 if (!EQ (window, selected_window))
23097 struct text_pos pt;
23099 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23100 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23103 /* Display mode lines. */
23104 clear_glyph_matrix (w->desired_matrix);
23105 if (display_mode_lines (w))
23106 ++nwindows;
23108 /* Restore old settings. */
23109 set_buffer_internal_1 (old);
23110 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23113 window = w->next;
23116 return nwindows;
23120 /* Display the mode and/or header line of window W. Value is the
23121 sum number of mode lines and header lines displayed. */
23123 static int
23124 display_mode_lines (struct window *w)
23126 Lisp_Object old_selected_window = selected_window;
23127 Lisp_Object old_selected_frame = selected_frame;
23128 Lisp_Object new_frame = w->frame;
23129 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23130 int n = 0;
23132 selected_frame = new_frame;
23133 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23134 or window's point, then we'd need select_window_1 here as well. */
23135 XSETWINDOW (selected_window, w);
23136 XFRAME (new_frame)->selected_window = selected_window;
23138 /* These will be set while the mode line specs are processed. */
23139 line_number_displayed = false;
23140 w->column_number_displayed = -1;
23142 if (window_wants_mode_line (w))
23144 Lisp_Object window_mode_line_format
23145 = window_parameter (w, Qmode_line_format);
23147 struct window *sel_w = XWINDOW (old_selected_window);
23149 /* Select mode line face based on the real selected window. */
23150 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23151 NILP (window_mode_line_format)
23152 ? BVAR (current_buffer, mode_line_format)
23153 : window_mode_line_format);
23154 ++n;
23157 if (window_wants_header_line (w))
23159 Lisp_Object window_header_line_format
23160 = window_parameter (w, Qheader_line_format);
23162 display_mode_line (w, HEADER_LINE_FACE_ID,
23163 NILP (window_header_line_format)
23164 ? BVAR (current_buffer, header_line_format)
23165 : window_header_line_format);
23166 ++n;
23169 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23170 selected_frame = old_selected_frame;
23171 selected_window = old_selected_window;
23172 if (n > 0)
23173 w->must_be_updated_p = true;
23174 return n;
23178 /* Display mode or header line of window W. FACE_ID specifies which
23179 line to display; it is either MODE_LINE_FACE_ID or
23180 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23181 display. Value is the pixel height of the mode/header line
23182 displayed. */
23184 static int
23185 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23187 struct it it;
23188 struct face *face;
23189 ptrdiff_t count = SPECPDL_INDEX ();
23191 init_iterator (&it, w, -1, -1, NULL, face_id);
23192 /* Don't extend on a previously drawn mode-line.
23193 This may happen if called from pos_visible_p. */
23194 it.glyph_row->enabled_p = false;
23195 prepare_desired_row (w, it.glyph_row, true);
23197 it.glyph_row->mode_line_p = true;
23199 /* FIXME: This should be controlled by a user option. But
23200 supporting such an option is not trivial, since the mode line is
23201 made up of many separate strings. */
23202 it.paragraph_embedding = L2R;
23204 record_unwind_protect (unwind_format_mode_line,
23205 format_mode_line_unwind_data (NULL, NULL,
23206 Qnil, false));
23208 mode_line_target = MODE_LINE_DISPLAY;
23210 /* Temporarily make frame's keyboard the current kboard so that
23211 kboard-local variables in the mode_line_format will get the right
23212 values. */
23213 push_kboard (FRAME_KBOARD (it.f));
23214 record_unwind_save_match_data ();
23215 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23216 pop_kboard ();
23218 unbind_to (count, Qnil);
23220 /* Fill up with spaces. */
23221 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23223 compute_line_metrics (&it);
23224 it.glyph_row->full_width_p = true;
23225 it.glyph_row->continued_p = false;
23226 it.glyph_row->truncated_on_left_p = false;
23227 it.glyph_row->truncated_on_right_p = false;
23229 /* Make a 3D mode-line have a shadow at its right end. */
23230 face = FACE_FROM_ID (it.f, face_id);
23231 extend_face_to_end_of_line (&it);
23232 if (face->box != FACE_NO_BOX)
23234 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23235 + it.glyph_row->used[TEXT_AREA] - 1);
23236 last->right_box_line_p = true;
23239 return it.glyph_row->height;
23242 /* Move element ELT in LIST to the front of LIST.
23243 Return the updated list. */
23245 static Lisp_Object
23246 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23248 register Lisp_Object tail, prev;
23249 register Lisp_Object tem;
23251 tail = list;
23252 prev = Qnil;
23253 while (CONSP (tail))
23255 tem = XCAR (tail);
23257 if (EQ (elt, tem))
23259 /* Splice out the link TAIL. */
23260 if (NILP (prev))
23261 list = XCDR (tail);
23262 else
23263 Fsetcdr (prev, XCDR (tail));
23265 /* Now make it the first. */
23266 Fsetcdr (tail, list);
23267 return tail;
23269 else
23270 prev = tail;
23271 tail = XCDR (tail);
23272 maybe_quit ();
23275 /* Not found--return unchanged LIST. */
23276 return list;
23279 /* Contribute ELT to the mode line for window IT->w. How it
23280 translates into text depends on its data type.
23282 IT describes the display environment in which we display, as usual.
23284 DEPTH is the depth in recursion. It is used to prevent
23285 infinite recursion here.
23287 FIELD_WIDTH is the number of characters the display of ELT should
23288 occupy in the mode line, and PRECISION is the maximum number of
23289 characters to display from ELT's representation. See
23290 display_string for details.
23292 Returns the hpos of the end of the text generated by ELT.
23294 PROPS is a property list to add to any string we encounter.
23296 If RISKY, remove (disregard) any properties in any string
23297 we encounter, and ignore :eval and :propertize.
23299 The global variable `mode_line_target' determines whether the
23300 output is passed to `store_mode_line_noprop',
23301 `store_mode_line_string', or `display_string'. */
23303 static int
23304 display_mode_element (struct it *it, int depth, int field_width, int precision,
23305 Lisp_Object elt, Lisp_Object props, bool risky)
23307 int n = 0, field, prec;
23308 bool literal = false;
23310 tail_recurse:
23311 if (depth > 100)
23312 elt = build_string ("*too-deep*");
23314 depth++;
23316 switch (XTYPE (elt))
23318 case Lisp_String:
23320 /* A string: output it and check for %-constructs within it. */
23321 unsigned char c;
23322 ptrdiff_t offset = 0;
23324 if (SCHARS (elt) > 0
23325 && (!NILP (props) || risky))
23327 Lisp_Object oprops, aelt;
23328 oprops = Ftext_properties_at (make_number (0), elt);
23330 /* If the starting string's properties are not what
23331 we want, translate the string. Also, if the string
23332 is risky, do that anyway. */
23334 if (NILP (Fequal (props, oprops)) || risky)
23336 /* If the starting string has properties,
23337 merge the specified ones onto the existing ones. */
23338 if (! NILP (oprops) && !risky)
23340 Lisp_Object tem;
23342 oprops = Fcopy_sequence (oprops);
23343 tem = props;
23344 while (CONSP (tem))
23346 oprops = Fplist_put (oprops, XCAR (tem),
23347 XCAR (XCDR (tem)));
23348 tem = XCDR (XCDR (tem));
23350 props = oprops;
23353 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23354 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23356 /* AELT is what we want. Move it to the front
23357 without consing. */
23358 elt = XCAR (aelt);
23359 mode_line_proptrans_alist
23360 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23362 else
23364 Lisp_Object tem;
23366 /* If AELT has the wrong props, it is useless.
23367 so get rid of it. */
23368 if (! NILP (aelt))
23369 mode_line_proptrans_alist
23370 = Fdelq (aelt, mode_line_proptrans_alist);
23372 elt = Fcopy_sequence (elt);
23373 Fset_text_properties (make_number (0), Flength (elt),
23374 props, elt);
23375 /* Add this item to mode_line_proptrans_alist. */
23376 mode_line_proptrans_alist
23377 = Fcons (Fcons (elt, props),
23378 mode_line_proptrans_alist);
23379 /* Truncate mode_line_proptrans_alist
23380 to at most 50 elements. */
23381 tem = Fnthcdr (make_number (50),
23382 mode_line_proptrans_alist);
23383 if (! NILP (tem))
23384 XSETCDR (tem, Qnil);
23389 offset = 0;
23391 if (literal)
23393 prec = precision - n;
23394 switch (mode_line_target)
23396 case MODE_LINE_NOPROP:
23397 case MODE_LINE_TITLE:
23398 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23399 break;
23400 case MODE_LINE_STRING:
23401 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23402 break;
23403 case MODE_LINE_DISPLAY:
23404 n += display_string (NULL, elt, Qnil, 0, 0, it,
23405 0, prec, 0, STRING_MULTIBYTE (elt));
23406 break;
23409 break;
23412 /* Handle the non-literal case. */
23414 while ((precision <= 0 || n < precision)
23415 && SREF (elt, offset) != 0
23416 && (mode_line_target != MODE_LINE_DISPLAY
23417 || it->current_x < it->last_visible_x))
23419 ptrdiff_t last_offset = offset;
23421 /* Advance to end of string or next format specifier. */
23422 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23425 if (offset - 1 != last_offset)
23427 ptrdiff_t nchars, nbytes;
23429 /* Output to end of string or up to '%'. Field width
23430 is length of string. Don't output more than
23431 PRECISION allows us. */
23432 offset--;
23434 prec = c_string_width (SDATA (elt) + last_offset,
23435 offset - last_offset, precision - n,
23436 &nchars, &nbytes);
23438 switch (mode_line_target)
23440 case MODE_LINE_NOPROP:
23441 case MODE_LINE_TITLE:
23442 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23443 break;
23444 case MODE_LINE_STRING:
23446 ptrdiff_t bytepos = last_offset;
23447 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23448 ptrdiff_t endpos = (precision <= 0
23449 ? string_byte_to_char (elt, offset)
23450 : charpos + nchars);
23451 Lisp_Object mode_string
23452 = Fsubstring (elt, make_number (charpos),
23453 make_number (endpos));
23454 n += store_mode_line_string (NULL, mode_string, false,
23455 0, 0, Qnil);
23457 break;
23458 case MODE_LINE_DISPLAY:
23460 ptrdiff_t bytepos = last_offset;
23461 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23463 if (precision <= 0)
23464 nchars = string_byte_to_char (elt, offset) - charpos;
23465 n += display_string (NULL, elt, Qnil, 0, charpos,
23466 it, 0, nchars, 0,
23467 STRING_MULTIBYTE (elt));
23469 break;
23472 else /* c == '%' */
23474 ptrdiff_t percent_position = offset;
23476 /* Get the specified minimum width. Zero means
23477 don't pad. */
23478 field = 0;
23479 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23480 field = field * 10 + c - '0';
23482 /* Don't pad beyond the total padding allowed. */
23483 if (field_width - n > 0 && field > field_width - n)
23484 field = field_width - n;
23486 /* Note that either PRECISION <= 0 or N < PRECISION. */
23487 prec = precision - n;
23489 if (c == 'M')
23490 n += display_mode_element (it, depth, field, prec,
23491 Vglobal_mode_string, props,
23492 risky);
23493 else if (c != 0)
23495 bool multibyte;
23496 ptrdiff_t bytepos, charpos;
23497 const char *spec;
23498 Lisp_Object string;
23500 bytepos = percent_position;
23501 charpos = (STRING_MULTIBYTE (elt)
23502 ? string_byte_to_char (elt, bytepos)
23503 : bytepos);
23504 spec = decode_mode_spec (it->w, c, field, &string);
23505 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23507 switch (mode_line_target)
23509 case MODE_LINE_NOPROP:
23510 case MODE_LINE_TITLE:
23511 n += store_mode_line_noprop (spec, field, prec);
23512 break;
23513 case MODE_LINE_STRING:
23515 Lisp_Object tem = build_string (spec);
23516 props = Ftext_properties_at (make_number (charpos), elt);
23517 /* Should only keep face property in props */
23518 n += store_mode_line_string (NULL, tem, false,
23519 field, prec, props);
23521 break;
23522 case MODE_LINE_DISPLAY:
23524 int nglyphs_before, nwritten;
23526 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23527 nwritten = display_string (spec, string, elt,
23528 charpos, 0, it,
23529 field, prec, 0,
23530 multibyte);
23532 /* Assign to the glyphs written above the
23533 string where the `%x' came from, position
23534 of the `%'. */
23535 if (nwritten > 0)
23537 struct glyph *glyph
23538 = (it->glyph_row->glyphs[TEXT_AREA]
23539 + nglyphs_before);
23540 int i;
23542 for (i = 0; i < nwritten; ++i)
23544 glyph[i].object = elt;
23545 glyph[i].charpos = charpos;
23548 n += nwritten;
23551 break;
23554 else /* c == 0 */
23555 break;
23559 break;
23561 case Lisp_Symbol:
23562 /* A symbol: process the value of the symbol recursively
23563 as if it appeared here directly. Avoid error if symbol void.
23564 Special case: if value of symbol is a string, output the string
23565 literally. */
23567 register Lisp_Object tem;
23569 /* If the variable is not marked as risky to set
23570 then its contents are risky to use. */
23571 if (NILP (Fget (elt, Qrisky_local_variable)))
23572 risky = true;
23574 tem = Fboundp (elt);
23575 if (!NILP (tem))
23577 tem = Fsymbol_value (elt);
23578 /* If value is a string, output that string literally:
23579 don't check for % within it. */
23580 if (STRINGP (tem))
23581 literal = true;
23583 if (!EQ (tem, elt))
23585 /* Give up right away for nil or t. */
23586 elt = tem;
23587 goto tail_recurse;
23591 break;
23593 case Lisp_Cons:
23595 register Lisp_Object car, tem;
23597 /* A cons cell: five distinct cases.
23598 If first element is :eval or :propertize, do something special.
23599 If first element is a string or a cons, process all the elements
23600 and effectively concatenate them.
23601 If first element is a negative number, truncate displaying cdr to
23602 at most that many characters. If positive, pad (with spaces)
23603 to at least that many characters.
23604 If first element is a symbol, process the cadr or caddr recursively
23605 according to whether the symbol's value is non-nil or nil. */
23606 car = XCAR (elt);
23607 if (EQ (car, QCeval))
23609 /* An element of the form (:eval FORM) means evaluate FORM
23610 and use the result as mode line elements. */
23612 if (risky)
23613 break;
23615 if (CONSP (XCDR (elt)))
23617 Lisp_Object spec;
23618 spec = safe__eval (true, XCAR (XCDR (elt)));
23619 n += display_mode_element (it, depth, field_width - n,
23620 precision - n, spec, props,
23621 risky);
23624 else if (EQ (car, QCpropertize))
23626 /* An element of the form (:propertize ELT PROPS...)
23627 means display ELT but applying properties PROPS. */
23629 if (risky)
23630 break;
23632 if (CONSP (XCDR (elt)))
23633 n += display_mode_element (it, depth, field_width - n,
23634 precision - n, XCAR (XCDR (elt)),
23635 XCDR (XCDR (elt)), risky);
23637 else if (SYMBOLP (car))
23639 tem = Fboundp (car);
23640 elt = XCDR (elt);
23641 if (!CONSP (elt))
23642 goto invalid;
23643 /* elt is now the cdr, and we know it is a cons cell.
23644 Use its car if CAR has a non-nil value. */
23645 if (!NILP (tem))
23647 tem = Fsymbol_value (car);
23648 if (!NILP (tem))
23650 elt = XCAR (elt);
23651 goto tail_recurse;
23654 /* Symbol's value is nil (or symbol is unbound)
23655 Get the cddr of the original list
23656 and if possible find the caddr and use that. */
23657 elt = XCDR (elt);
23658 if (NILP (elt))
23659 break;
23660 else if (!CONSP (elt))
23661 goto invalid;
23662 elt = XCAR (elt);
23663 goto tail_recurse;
23665 else if (INTEGERP (car))
23667 register int lim = XINT (car);
23668 elt = XCDR (elt);
23669 if (lim < 0)
23671 /* Negative int means reduce maximum width. */
23672 if (precision <= 0)
23673 precision = -lim;
23674 else
23675 precision = min (precision, -lim);
23677 else if (lim > 0)
23679 /* Padding specified. Don't let it be more than
23680 current maximum. */
23681 if (precision > 0)
23682 lim = min (precision, lim);
23684 /* If that's more padding than already wanted, queue it.
23685 But don't reduce padding already specified even if
23686 that is beyond the current truncation point. */
23687 field_width = max (lim, field_width);
23689 goto tail_recurse;
23691 else if (STRINGP (car) || CONSP (car))
23692 FOR_EACH_TAIL_SAFE (elt)
23694 if (0 < precision && precision <= n)
23695 break;
23696 n += display_mode_element (it, depth,
23697 /* Pad after only the last
23698 list element. */
23699 (! CONSP (XCDR (elt))
23700 ? field_width - n
23701 : 0),
23702 precision - n, XCAR (elt),
23703 props, risky);
23706 break;
23708 default:
23709 invalid:
23710 elt = build_string ("*invalid*");
23711 goto tail_recurse;
23714 /* Pad to FIELD_WIDTH. */
23715 if (field_width > 0 && n < field_width)
23717 switch (mode_line_target)
23719 case MODE_LINE_NOPROP:
23720 case MODE_LINE_TITLE:
23721 n += store_mode_line_noprop ("", field_width - n, 0);
23722 break;
23723 case MODE_LINE_STRING:
23724 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23725 Qnil);
23726 break;
23727 case MODE_LINE_DISPLAY:
23728 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23729 0, 0, 0);
23730 break;
23734 return n;
23737 /* Store a mode-line string element in mode_line_string_list.
23739 If STRING is non-null, display that C string. Otherwise, the Lisp
23740 string LISP_STRING is displayed.
23742 FIELD_WIDTH is the minimum number of output glyphs to produce.
23743 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23744 with spaces. FIELD_WIDTH <= 0 means don't pad.
23746 PRECISION is the maximum number of characters to output from
23747 STRING. PRECISION <= 0 means don't truncate the string.
23749 If COPY_STRING, make a copy of LISP_STRING before adding
23750 properties to the string.
23752 PROPS are the properties to add to the string.
23753 The mode_line_string_face face property is always added to the string.
23756 static int
23757 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23758 bool copy_string,
23759 int field_width, int precision, Lisp_Object props)
23761 ptrdiff_t len;
23762 int n = 0;
23764 if (string != NULL)
23766 len = strlen (string);
23767 if (precision > 0 && len > precision)
23768 len = precision;
23769 lisp_string = make_string (string, len);
23770 if (NILP (props))
23771 props = mode_line_string_face_prop;
23772 else if (!NILP (mode_line_string_face))
23774 Lisp_Object face = Fplist_get (props, Qface);
23775 props = Fcopy_sequence (props);
23776 if (NILP (face))
23777 face = mode_line_string_face;
23778 else
23779 face = list2 (face, mode_line_string_face);
23780 props = Fplist_put (props, Qface, face);
23782 Fadd_text_properties (make_number (0), make_number (len),
23783 props, lisp_string);
23785 else
23787 len = XFASTINT (Flength (lisp_string));
23788 if (precision > 0 && len > precision)
23790 len = precision;
23791 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23792 precision = -1;
23794 if (!NILP (mode_line_string_face))
23796 Lisp_Object face;
23797 if (NILP (props))
23798 props = Ftext_properties_at (make_number (0), lisp_string);
23799 face = Fplist_get (props, Qface);
23800 if (NILP (face))
23801 face = mode_line_string_face;
23802 else
23803 face = list2 (face, mode_line_string_face);
23804 props = list2 (Qface, face);
23805 if (copy_string)
23806 lisp_string = Fcopy_sequence (lisp_string);
23808 if (!NILP (props))
23809 Fadd_text_properties (make_number (0), make_number (len),
23810 props, lisp_string);
23813 if (len > 0)
23815 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23816 n += len;
23819 if (field_width > len)
23821 field_width -= len;
23822 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23823 if (!NILP (props))
23824 Fadd_text_properties (make_number (0), make_number (field_width),
23825 props, lisp_string);
23826 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23827 n += field_width;
23830 return n;
23834 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23835 1, 4, 0,
23836 doc: /* Format a string out of a mode line format specification.
23837 First arg FORMAT specifies the mode line format (see `mode-line-format'
23838 for details) to use.
23840 By default, the format is evaluated for the currently selected window.
23842 Optional second arg FACE specifies the face property to put on all
23843 characters for which no face is specified. The value nil means the
23844 default face. The value t means whatever face the window's mode line
23845 currently uses (either `mode-line' or `mode-line-inactive',
23846 depending on whether the window is the selected window or not).
23847 An integer value means the value string has no text
23848 properties.
23850 Optional third and fourth args WINDOW and BUFFER specify the window
23851 and buffer to use as the context for the formatting (defaults
23852 are the selected window and the WINDOW's buffer). */)
23853 (Lisp_Object format, Lisp_Object face,
23854 Lisp_Object window, Lisp_Object buffer)
23856 struct it it;
23857 int len;
23858 struct window *w;
23859 struct buffer *old_buffer = NULL;
23860 int face_id;
23861 bool no_props = INTEGERP (face);
23862 ptrdiff_t count = SPECPDL_INDEX ();
23863 Lisp_Object str;
23864 int string_start = 0;
23866 w = decode_any_window (window);
23867 XSETWINDOW (window, w);
23869 if (NILP (buffer))
23870 buffer = w->contents;
23871 CHECK_BUFFER (buffer);
23873 /* Make formatting the modeline a non-op when noninteractive, otherwise
23874 there will be problems later caused by a partially initialized frame. */
23875 if (NILP (format) || noninteractive)
23876 return empty_unibyte_string;
23878 if (no_props)
23879 face = Qnil;
23881 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23882 : EQ (face, Qt) ? (EQ (window, selected_window)
23883 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23884 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23885 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23886 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23887 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23888 : DEFAULT_FACE_ID;
23890 old_buffer = current_buffer;
23892 /* Save things including mode_line_proptrans_alist,
23893 and set that to nil so that we don't alter the outer value. */
23894 record_unwind_protect (unwind_format_mode_line,
23895 format_mode_line_unwind_data
23896 (XFRAME (WINDOW_FRAME (w)),
23897 old_buffer, selected_window, true));
23898 mode_line_proptrans_alist = Qnil;
23900 Fselect_window (window, Qt);
23901 set_buffer_internal_1 (XBUFFER (buffer));
23903 init_iterator (&it, w, -1, -1, NULL, face_id);
23905 if (no_props)
23907 mode_line_target = MODE_LINE_NOPROP;
23908 mode_line_string_face_prop = Qnil;
23909 mode_line_string_list = Qnil;
23910 string_start = MODE_LINE_NOPROP_LEN (0);
23912 else
23914 mode_line_target = MODE_LINE_STRING;
23915 mode_line_string_list = Qnil;
23916 mode_line_string_face = face;
23917 mode_line_string_face_prop
23918 = NILP (face) ? Qnil : list2 (Qface, face);
23921 push_kboard (FRAME_KBOARD (it.f));
23922 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23923 pop_kboard ();
23925 if (no_props)
23927 len = MODE_LINE_NOPROP_LEN (string_start);
23928 str = make_string (mode_line_noprop_buf + string_start, len);
23930 else
23932 mode_line_string_list = Fnreverse (mode_line_string_list);
23933 str = Fmapconcat (Qidentity, mode_line_string_list,
23934 empty_unibyte_string);
23937 unbind_to (count, Qnil);
23938 return str;
23941 /* Write a null-terminated, right justified decimal representation of
23942 the positive integer D to BUF using a minimal field width WIDTH. */
23944 static void
23945 pint2str (register char *buf, register int width, register ptrdiff_t d)
23947 register char *p = buf;
23949 if (d <= 0)
23950 *p++ = '0';
23951 else
23953 while (d > 0)
23955 *p++ = d % 10 + '0';
23956 d /= 10;
23960 for (width -= (int) (p - buf); width > 0; --width)
23961 *p++ = ' ';
23962 *p-- = '\0';
23963 while (p > buf)
23965 d = *buf;
23966 *buf++ = *p;
23967 *p-- = d;
23971 /* Write a null-terminated, right justified decimal and "human
23972 readable" representation of the nonnegative integer D to BUF using
23973 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23975 static const char power_letter[] =
23977 0, /* no letter */
23978 'k', /* kilo */
23979 'M', /* mega */
23980 'G', /* giga */
23981 'T', /* tera */
23982 'P', /* peta */
23983 'E', /* exa */
23984 'Z', /* zetta */
23985 'Y' /* yotta */
23988 static void
23989 pint2hrstr (char *buf, int width, ptrdiff_t d)
23991 /* We aim to represent the nonnegative integer D as
23992 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23993 ptrdiff_t quotient = d;
23994 int remainder = 0;
23995 /* -1 means: do not use TENTHS. */
23996 int tenths = -1;
23997 int exponent = 0;
23999 /* Length of QUOTIENT.TENTHS as a string. */
24000 int length;
24002 char * psuffix;
24003 char * p;
24005 if (quotient >= 1000)
24007 /* Scale to the appropriate EXPONENT. */
24010 remainder = quotient % 1000;
24011 quotient /= 1000;
24012 exponent++;
24014 while (quotient >= 1000);
24016 /* Round to nearest and decide whether to use TENTHS or not. */
24017 if (quotient <= 9)
24019 tenths = remainder / 100;
24020 if (remainder % 100 >= 50)
24022 if (tenths < 9)
24023 tenths++;
24024 else
24026 quotient++;
24027 if (quotient == 10)
24028 tenths = -1;
24029 else
24030 tenths = 0;
24034 else
24035 if (remainder >= 500)
24037 if (quotient < 999)
24038 quotient++;
24039 else
24041 quotient = 1;
24042 exponent++;
24043 tenths = 0;
24048 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24049 if (tenths == -1 && quotient <= 99)
24050 if (quotient <= 9)
24051 length = 1;
24052 else
24053 length = 2;
24054 else
24055 length = 3;
24056 p = psuffix = buf + max (width, length);
24058 /* Print EXPONENT. */
24059 *psuffix++ = power_letter[exponent];
24060 *psuffix = '\0';
24062 /* Print TENTHS. */
24063 if (tenths >= 0)
24065 *--p = '0' + tenths;
24066 *--p = '.';
24069 /* Print QUOTIENT. */
24072 int digit = quotient % 10;
24073 *--p = '0' + digit;
24075 while ((quotient /= 10) != 0);
24077 /* Print leading spaces. */
24078 while (buf < p)
24079 *--p = ' ';
24082 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24083 If EOL_FLAG, set also a mnemonic character for end-of-line
24084 type of CODING_SYSTEM. Return updated pointer into BUF. */
24086 static unsigned char invalid_eol_type[] = "(*invalid*)";
24088 static char *
24089 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24091 Lisp_Object val;
24092 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24093 const unsigned char *eol_str;
24094 int eol_str_len;
24095 /* The EOL conversion we are using. */
24096 Lisp_Object eoltype;
24098 val = CODING_SYSTEM_SPEC (coding_system);
24099 eoltype = Qnil;
24101 if (!VECTORP (val)) /* Not yet decided. */
24103 *buf++ = multibyte ? '-' : ' ';
24104 if (eol_flag)
24105 eoltype = eol_mnemonic_undecided;
24106 /* Don't mention EOL conversion if it isn't decided. */
24108 else
24110 Lisp_Object attrs;
24111 Lisp_Object eolvalue;
24113 attrs = AREF (val, 0);
24114 eolvalue = AREF (val, 2);
24116 *buf++ = multibyte
24117 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24118 : ' ';
24120 if (eol_flag)
24122 /* The EOL conversion that is normal on this system. */
24124 if (NILP (eolvalue)) /* Not yet decided. */
24125 eoltype = eol_mnemonic_undecided;
24126 else if (VECTORP (eolvalue)) /* Not yet decided. */
24127 eoltype = eol_mnemonic_undecided;
24128 else /* eolvalue is Qunix, Qdos, or Qmac. */
24129 eoltype = (EQ (eolvalue, Qunix)
24130 ? eol_mnemonic_unix
24131 : EQ (eolvalue, Qdos)
24132 ? eol_mnemonic_dos : eol_mnemonic_mac);
24136 if (eol_flag)
24138 /* Mention the EOL conversion if it is not the usual one. */
24139 if (STRINGP (eoltype))
24141 eol_str = SDATA (eoltype);
24142 eol_str_len = SBYTES (eoltype);
24144 else if (CHARACTERP (eoltype))
24146 int c = XFASTINT (eoltype);
24147 return buf + CHAR_STRING (c, (unsigned char *) buf);
24149 else
24151 eol_str = invalid_eol_type;
24152 eol_str_len = sizeof (invalid_eol_type) - 1;
24154 memcpy (buf, eol_str, eol_str_len);
24155 buf += eol_str_len;
24158 return buf;
24161 /* Return the approximate percentage N is of D (rounding upward), or 99,
24162 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24164 static int
24165 percent99 (ptrdiff_t n, ptrdiff_t d)
24167 int percent = (d - 1 + 100.0 * n) / d;
24168 return min (percent, 99);
24171 /* Return a string for the output of a mode line %-spec for window W,
24172 generated by character C. FIELD_WIDTH > 0 means pad the string
24173 returned with spaces to that value. Return a Lisp string in
24174 *STRING if the resulting string is taken from that Lisp string.
24176 Note we operate on the current buffer for most purposes. */
24178 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24180 static const char *
24181 decode_mode_spec (struct window *w, register int c, int field_width,
24182 Lisp_Object *string)
24184 Lisp_Object obj;
24185 struct frame *f = XFRAME (WINDOW_FRAME (w));
24186 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24187 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24188 produce strings from numerical values, so limit preposterously
24189 large values of FIELD_WIDTH to avoid overrunning the buffer's
24190 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24191 bytes plus the terminating null. */
24192 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24193 struct buffer *b = current_buffer;
24195 obj = Qnil;
24196 *string = Qnil;
24198 switch (c)
24200 case '*':
24201 if (!NILP (BVAR (b, read_only)))
24202 return "%";
24203 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24204 return "*";
24205 return "-";
24207 case '+':
24208 /* This differs from %* only for a modified read-only buffer. */
24209 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24210 return "*";
24211 if (!NILP (BVAR (b, read_only)))
24212 return "%";
24213 return "-";
24215 case '&':
24216 /* This differs from %* in ignoring read-only-ness. */
24217 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24218 return "*";
24219 return "-";
24221 case '%':
24222 return "%";
24224 case '[':
24226 int i;
24227 char *p;
24229 if (command_loop_level > 5)
24230 return "[[[... ";
24231 p = decode_mode_spec_buf;
24232 for (i = 0; i < command_loop_level; i++)
24233 *p++ = '[';
24234 *p = 0;
24235 return decode_mode_spec_buf;
24238 case ']':
24240 int i;
24241 char *p;
24243 if (command_loop_level > 5)
24244 return " ...]]]";
24245 p = decode_mode_spec_buf;
24246 for (i = 0; i < command_loop_level; i++)
24247 *p++ = ']';
24248 *p = 0;
24249 return decode_mode_spec_buf;
24252 case '-':
24254 register int i;
24256 /* Let lots_of_dashes be a string of infinite length. */
24257 if (mode_line_target == MODE_LINE_NOPROP
24258 || mode_line_target == MODE_LINE_STRING)
24259 return "--";
24260 if (field_width <= 0
24261 || field_width > sizeof (lots_of_dashes))
24263 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24264 decode_mode_spec_buf[i] = '-';
24265 decode_mode_spec_buf[i] = '\0';
24266 return decode_mode_spec_buf;
24268 else
24269 return lots_of_dashes;
24272 case 'b':
24273 obj = BVAR (b, name);
24274 break;
24276 case 'c':
24277 case 'C':
24278 /* %c, %C, and %l are ignored in `frame-title-format'.
24279 (In redisplay_internal, the frame title is drawn _before_ the
24280 windows are updated, so the stuff which depends on actual
24281 window contents (such as %l) may fail to render properly, or
24282 even crash emacs.) */
24283 if (mode_line_target == MODE_LINE_TITLE)
24284 return "";
24285 else
24287 ptrdiff_t col = current_column ();
24288 int disp_col = (c == 'C') ? col + 1 : col;
24289 w->column_number_displayed = col;
24290 pint2str (decode_mode_spec_buf, width, disp_col);
24291 return decode_mode_spec_buf;
24294 case 'e':
24295 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24297 if (NILP (Vmemory_full))
24298 return "";
24299 else
24300 return "!MEM FULL! ";
24302 #else
24303 return "";
24304 #endif
24306 case 'F':
24307 /* %F displays the frame name. */
24308 if (!NILP (f->title))
24309 return SSDATA (f->title);
24310 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24311 return SSDATA (f->name);
24312 return "Emacs";
24314 case 'f':
24315 obj = BVAR (b, filename);
24316 break;
24318 case 'i':
24320 ptrdiff_t size = ZV - BEGV;
24321 pint2str (decode_mode_spec_buf, width, size);
24322 return decode_mode_spec_buf;
24325 case 'I':
24327 ptrdiff_t size = ZV - BEGV;
24328 pint2hrstr (decode_mode_spec_buf, width, size);
24329 return decode_mode_spec_buf;
24332 case 'l':
24334 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24335 ptrdiff_t topline, nlines, height;
24336 ptrdiff_t junk;
24338 /* %c, %C, and %l are ignored in `frame-title-format'. */
24339 if (mode_line_target == MODE_LINE_TITLE)
24340 return "";
24342 startpos = marker_position (w->start);
24343 startpos_byte = marker_byte_position (w->start);
24344 height = WINDOW_TOTAL_LINES (w);
24346 /* If we decided that this buffer isn't suitable for line numbers,
24347 don't forget that too fast. */
24348 if (w->base_line_pos == -1)
24349 goto no_value;
24351 /* If the buffer is very big, don't waste time. */
24352 if (INTEGERP (Vline_number_display_limit)
24353 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24355 w->base_line_pos = 0;
24356 w->base_line_number = 0;
24357 goto no_value;
24360 if (w->base_line_number > 0
24361 && w->base_line_pos > 0
24362 && w->base_line_pos <= startpos)
24364 line = w->base_line_number;
24365 linepos = w->base_line_pos;
24366 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24368 else
24370 line = 1;
24371 linepos = BUF_BEGV (b);
24372 linepos_byte = BUF_BEGV_BYTE (b);
24375 /* Count lines from base line to window start position. */
24376 nlines = display_count_lines (linepos_byte,
24377 startpos_byte,
24378 startpos, &junk);
24380 topline = nlines + line;
24382 /* Determine a new base line, if the old one is too close
24383 or too far away, or if we did not have one.
24384 "Too close" means it's plausible a scroll-down would
24385 go back past it. */
24386 if (startpos == BUF_BEGV (b))
24388 w->base_line_number = topline;
24389 w->base_line_pos = BUF_BEGV (b);
24391 else if (nlines < height + 25 || nlines > height * 3 + 50
24392 || linepos == BUF_BEGV (b))
24394 ptrdiff_t limit = BUF_BEGV (b);
24395 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24396 ptrdiff_t position;
24397 ptrdiff_t distance =
24398 (height * 2 + 30) * line_number_display_limit_width;
24400 if (startpos - distance > limit)
24402 limit = startpos - distance;
24403 limit_byte = CHAR_TO_BYTE (limit);
24406 nlines = display_count_lines (startpos_byte,
24407 limit_byte,
24408 - (height * 2 + 30),
24409 &position);
24410 /* If we couldn't find the lines we wanted within
24411 line_number_display_limit_width chars per line,
24412 give up on line numbers for this window. */
24413 if (position == limit_byte && limit == startpos - distance)
24415 w->base_line_pos = -1;
24416 w->base_line_number = 0;
24417 goto no_value;
24420 w->base_line_number = topline - nlines;
24421 w->base_line_pos = BYTE_TO_CHAR (position);
24424 /* Now count lines from the start pos to point. */
24425 nlines = display_count_lines (startpos_byte,
24426 PT_BYTE, PT, &junk);
24428 /* Record that we did display the line number. */
24429 line_number_displayed = true;
24431 /* Make the string to show. */
24432 pint2str (decode_mode_spec_buf, width, topline + nlines);
24433 return decode_mode_spec_buf;
24434 no_value:
24436 char *p = decode_mode_spec_buf;
24437 int pad = width - 2;
24438 while (pad-- > 0)
24439 *p++ = ' ';
24440 *p++ = '?';
24441 *p++ = '?';
24442 *p = '\0';
24443 return decode_mode_spec_buf;
24446 break;
24448 case 'm':
24449 obj = BVAR (b, mode_name);
24450 break;
24452 case 'n':
24453 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24454 return " Narrow";
24455 break;
24457 /* Display the "degree of travel" of the window through the buffer. */
24458 case 'o':
24460 ptrdiff_t toppos = marker_position (w->start);
24461 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24462 ptrdiff_t begv = BUF_BEGV (b);
24463 ptrdiff_t zv = BUF_ZV (b);
24465 if (zv <= botpos)
24466 return toppos <= begv ? "All" : "Bottom";
24467 else if (toppos <= begv)
24468 return "Top";
24469 else
24471 sprintf (decode_mode_spec_buf, "%2d%%",
24472 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24473 return decode_mode_spec_buf;
24477 /* Display percentage of buffer above the top of the screen. */
24478 case 'p':
24480 ptrdiff_t pos = marker_position (w->start);
24481 ptrdiff_t begv = BUF_BEGV (b);
24482 ptrdiff_t zv = BUF_ZV (b);
24484 if (w->window_end_pos <= BUF_Z (b) - zv)
24485 return pos <= begv ? "All" : "Bottom";
24486 else if (pos <= begv)
24487 return "Top";
24488 else
24490 sprintf (decode_mode_spec_buf, "%2d%%",
24491 percent99 (pos - begv, zv - begv));
24492 return decode_mode_spec_buf;
24496 /* Display percentage of size above the bottom of the screen. */
24497 case 'P':
24499 ptrdiff_t toppos = marker_position (w->start);
24500 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24501 ptrdiff_t begv = BUF_BEGV (b);
24502 ptrdiff_t zv = BUF_ZV (b);
24504 if (zv <= botpos)
24505 return toppos <= begv ? "All" : "Bottom";
24506 else
24508 sprintf (decode_mode_spec_buf,
24509 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24510 percent99 (botpos - begv, zv - begv));
24511 return decode_mode_spec_buf;
24515 /* Display percentage offsets of top and bottom of the window,
24516 using "All" (but not "Top" or "Bottom") where appropriate. */
24517 case 'q':
24519 ptrdiff_t toppos = marker_position (w->start);
24520 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24521 ptrdiff_t begv = BUF_BEGV (b);
24522 ptrdiff_t zv = BUF_ZV (b);
24523 int top_perc, bot_perc;
24525 if ((toppos <= begv) && (zv <= botpos))
24526 return "All ";
24528 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24529 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24531 if (top_perc == bot_perc)
24532 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24533 else
24534 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24536 return decode_mode_spec_buf;
24539 case 's':
24540 /* status of process */
24541 obj = Fget_buffer_process (Fcurrent_buffer ());
24542 if (NILP (obj))
24543 return "no process";
24544 #ifndef MSDOS
24545 obj = Fsymbol_name (Fprocess_status (obj));
24546 #endif
24547 break;
24549 case '@':
24551 ptrdiff_t count = inhibit_garbage_collection ();
24552 Lisp_Object curdir = BVAR (current_buffer, directory);
24553 Lisp_Object val = Qnil;
24555 if (STRINGP (curdir))
24556 val = call1 (intern ("file-remote-p"), curdir);
24558 unbind_to (count, Qnil);
24560 if (NILP (val))
24561 return "-";
24562 else
24563 return "@";
24566 case 'z':
24567 /* coding-system (not including end-of-line format) */
24568 case 'Z':
24569 /* coding-system (including end-of-line type) */
24571 bool eol_flag = (c == 'Z');
24572 char *p = decode_mode_spec_buf;
24574 if (! FRAME_WINDOW_P (f))
24576 /* No need to mention EOL here--the terminal never needs
24577 to do EOL conversion. */
24578 p = decode_mode_spec_coding (CODING_ID_NAME
24579 (FRAME_KEYBOARD_CODING (f)->id),
24580 p, false);
24581 p = decode_mode_spec_coding (CODING_ID_NAME
24582 (FRAME_TERMINAL_CODING (f)->id),
24583 p, false);
24585 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24586 p, eol_flag);
24588 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24589 #ifdef subprocesses
24590 obj = Fget_buffer_process (Fcurrent_buffer ());
24591 if (PROCESSP (obj))
24593 p = decode_mode_spec_coding
24594 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24595 p = decode_mode_spec_coding
24596 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24598 #endif /* subprocesses */
24599 #endif /* false */
24600 *p = 0;
24601 return decode_mode_spec_buf;
24605 if (STRINGP (obj))
24607 *string = obj;
24608 return SSDATA (obj);
24610 else
24611 return "";
24615 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24616 means count lines back from START_BYTE. But don't go beyond
24617 LIMIT_BYTE. Return the number of lines thus found (always
24618 nonnegative).
24620 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24621 either the position COUNT lines after/before START_BYTE, if we
24622 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24623 COUNT lines. */
24625 static ptrdiff_t
24626 display_count_lines (ptrdiff_t start_byte,
24627 ptrdiff_t limit_byte, ptrdiff_t count,
24628 ptrdiff_t *byte_pos_ptr)
24630 register unsigned char *cursor;
24631 unsigned char *base;
24633 register ptrdiff_t ceiling;
24634 register unsigned char *ceiling_addr;
24635 ptrdiff_t orig_count = count;
24637 /* If we are not in selective display mode,
24638 check only for newlines. */
24639 bool selective_display
24640 = (!NILP (BVAR (current_buffer, selective_display))
24641 && !INTEGERP (BVAR (current_buffer, selective_display)));
24643 if (count > 0)
24645 while (start_byte < limit_byte)
24647 ceiling = BUFFER_CEILING_OF (start_byte);
24648 ceiling = min (limit_byte - 1, ceiling);
24649 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24650 base = (cursor = BYTE_POS_ADDR (start_byte));
24654 if (selective_display)
24656 while (*cursor != '\n' && *cursor != 015
24657 && ++cursor != ceiling_addr)
24658 continue;
24659 if (cursor == ceiling_addr)
24660 break;
24662 else
24664 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24665 if (! cursor)
24666 break;
24669 cursor++;
24671 if (--count == 0)
24673 start_byte += cursor - base;
24674 *byte_pos_ptr = start_byte;
24675 return orig_count;
24678 while (cursor < ceiling_addr);
24680 start_byte += ceiling_addr - base;
24683 else
24685 while (start_byte > limit_byte)
24687 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24688 ceiling = max (limit_byte, ceiling);
24689 ceiling_addr = BYTE_POS_ADDR (ceiling);
24690 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24691 while (true)
24693 if (selective_display)
24695 while (--cursor >= ceiling_addr
24696 && *cursor != '\n' && *cursor != 015)
24697 continue;
24698 if (cursor < ceiling_addr)
24699 break;
24701 else
24703 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24704 if (! cursor)
24705 break;
24708 if (++count == 0)
24710 start_byte += cursor - base + 1;
24711 *byte_pos_ptr = start_byte;
24712 /* When scanning backwards, we should
24713 not count the newline posterior to which we stop. */
24714 return - orig_count - 1;
24717 start_byte += ceiling_addr - base;
24721 *byte_pos_ptr = limit_byte;
24723 if (count < 0)
24724 return - orig_count + count;
24725 return orig_count - count;
24731 /***********************************************************************
24732 Displaying strings
24733 ***********************************************************************/
24735 /* Display a NUL-terminated string, starting with index START.
24737 If STRING is non-null, display that C string. Otherwise, the Lisp
24738 string LISP_STRING is displayed. There's a case that STRING is
24739 non-null and LISP_STRING is not nil. It means STRING is a string
24740 data of LISP_STRING. In that case, we display LISP_STRING while
24741 ignoring its text properties.
24743 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24744 FACE_STRING. Display STRING or LISP_STRING with the face at
24745 FACE_STRING_POS in FACE_STRING:
24747 Display the string in the environment given by IT, but use the
24748 standard display table, temporarily.
24750 FIELD_WIDTH is the minimum number of output glyphs to produce.
24751 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24752 with spaces. If STRING has more characters, more than FIELD_WIDTH
24753 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24755 PRECISION is the maximum number of characters to output from
24756 STRING. PRECISION < 0 means don't truncate the string.
24758 This is roughly equivalent to printf format specifiers:
24760 FIELD_WIDTH PRECISION PRINTF
24761 ----------------------------------------
24762 -1 -1 %s
24763 -1 10 %.10s
24764 10 -1 %10s
24765 20 10 %20.10s
24767 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24768 display them, and < 0 means obey the current buffer's value of
24769 enable_multibyte_characters.
24771 Value is the number of columns displayed. */
24773 static int
24774 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24775 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24776 int field_width, int precision, int max_x, int multibyte)
24778 int hpos_at_start = it->hpos;
24779 int saved_face_id = it->face_id;
24780 struct glyph_row *row = it->glyph_row;
24781 ptrdiff_t it_charpos;
24783 /* Initialize the iterator IT for iteration over STRING beginning
24784 with index START. */
24785 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24786 precision, field_width, multibyte);
24787 if (string && STRINGP (lisp_string))
24788 /* LISP_STRING is the one returned by decode_mode_spec. We should
24789 ignore its text properties. */
24790 it->stop_charpos = it->end_charpos;
24792 /* If displaying STRING, set up the face of the iterator from
24793 FACE_STRING, if that's given. */
24794 if (STRINGP (face_string))
24796 ptrdiff_t endptr;
24797 struct face *face;
24799 it->face_id
24800 = face_at_string_position (it->w, face_string, face_string_pos,
24801 0, &endptr, it->base_face_id, false);
24802 face = FACE_FROM_ID (it->f, it->face_id);
24803 it->face_box_p = face->box != FACE_NO_BOX;
24806 /* Set max_x to the maximum allowed X position. Don't let it go
24807 beyond the right edge of the window. */
24808 if (max_x <= 0)
24809 max_x = it->last_visible_x;
24810 else
24811 max_x = min (max_x, it->last_visible_x);
24813 /* Skip over display elements that are not visible. because IT->w is
24814 hscrolled. */
24815 if (it->current_x < it->first_visible_x)
24816 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24817 MOVE_TO_POS | MOVE_TO_X);
24819 row->ascent = it->max_ascent;
24820 row->height = it->max_ascent + it->max_descent;
24821 row->phys_ascent = it->max_phys_ascent;
24822 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24823 row->extra_line_spacing = it->max_extra_line_spacing;
24825 if (STRINGP (it->string))
24826 it_charpos = IT_STRING_CHARPOS (*it);
24827 else
24828 it_charpos = IT_CHARPOS (*it);
24830 /* This condition is for the case that we are called with current_x
24831 past last_visible_x. */
24832 while (it->current_x < max_x)
24834 int x_before, x, n_glyphs_before, i, nglyphs;
24836 /* Get the next display element. */
24837 if (!get_next_display_element (it))
24838 break;
24840 /* Produce glyphs. */
24841 x_before = it->current_x;
24842 n_glyphs_before = row->used[TEXT_AREA];
24843 PRODUCE_GLYPHS (it);
24845 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24846 i = 0;
24847 x = x_before;
24848 while (i < nglyphs)
24850 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24852 if (it->line_wrap != TRUNCATE
24853 && x + glyph->pixel_width > max_x)
24855 /* End of continued line or max_x reached. */
24856 if (CHAR_GLYPH_PADDING_P (*glyph))
24858 /* A wide character is unbreakable. */
24859 if (row->reversed_p)
24860 unproduce_glyphs (it, row->used[TEXT_AREA]
24861 - n_glyphs_before);
24862 row->used[TEXT_AREA] = n_glyphs_before;
24863 it->current_x = x_before;
24865 else
24867 if (row->reversed_p)
24868 unproduce_glyphs (it, row->used[TEXT_AREA]
24869 - (n_glyphs_before + i));
24870 row->used[TEXT_AREA] = n_glyphs_before + i;
24871 it->current_x = x;
24873 break;
24875 else if (x + glyph->pixel_width >= it->first_visible_x)
24877 /* Glyph is at least partially visible. */
24878 ++it->hpos;
24879 if (x < it->first_visible_x)
24880 row->x = x - it->first_visible_x;
24882 else
24884 /* Glyph is off the left margin of the display area.
24885 Should not happen. */
24886 emacs_abort ();
24889 row->ascent = max (row->ascent, it->max_ascent);
24890 row->height = max (row->height, it->max_ascent + it->max_descent);
24891 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24892 row->phys_height = max (row->phys_height,
24893 it->max_phys_ascent + it->max_phys_descent);
24894 row->extra_line_spacing = max (row->extra_line_spacing,
24895 it->max_extra_line_spacing);
24896 x += glyph->pixel_width;
24897 ++i;
24900 /* Stop if max_x reached. */
24901 if (i < nglyphs)
24902 break;
24904 /* Stop at line ends. */
24905 if (ITERATOR_AT_END_OF_LINE_P (it))
24907 it->continuation_lines_width = 0;
24908 break;
24911 set_iterator_to_next (it, true);
24912 if (STRINGP (it->string))
24913 it_charpos = IT_STRING_CHARPOS (*it);
24914 else
24915 it_charpos = IT_CHARPOS (*it);
24917 /* Stop if truncating at the right edge. */
24918 if (it->line_wrap == TRUNCATE
24919 && it->current_x >= it->last_visible_x)
24921 /* Add truncation mark, but don't do it if the line is
24922 truncated at a padding space. */
24923 if (it_charpos < it->string_nchars)
24925 if (!FRAME_WINDOW_P (it->f))
24927 int ii, n;
24929 if (it->current_x > it->last_visible_x)
24931 if (!row->reversed_p)
24933 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24934 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24935 break;
24937 else
24939 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24940 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24941 break;
24942 unproduce_glyphs (it, ii + 1);
24943 ii = row->used[TEXT_AREA] - (ii + 1);
24945 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24947 row->used[TEXT_AREA] = ii;
24948 produce_special_glyphs (it, IT_TRUNCATION);
24951 produce_special_glyphs (it, IT_TRUNCATION);
24953 row->truncated_on_right_p = true;
24955 break;
24959 /* Maybe insert a truncation at the left. */
24960 if (it->first_visible_x
24961 && it_charpos > 0)
24963 if (!FRAME_WINDOW_P (it->f)
24964 || (row->reversed_p
24965 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24966 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24967 insert_left_trunc_glyphs (it);
24968 row->truncated_on_left_p = true;
24971 it->face_id = saved_face_id;
24973 /* Value is number of columns displayed. */
24974 return it->hpos - hpos_at_start;
24979 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24980 appears as an element of LIST or as the car of an element of LIST.
24981 If PROPVAL is a list, compare each element against LIST in that
24982 way, and return 1/2 if any element of PROPVAL is found in LIST.
24983 Otherwise return 0. This function cannot quit.
24984 The return value is 2 if the text is invisible but with an ellipsis
24985 and 1 if it's invisible and without an ellipsis. */
24988 invisible_prop (Lisp_Object propval, Lisp_Object list)
24990 Lisp_Object tail, proptail;
24992 for (tail = list; CONSP (tail); tail = XCDR (tail))
24994 register Lisp_Object tem;
24995 tem = XCAR (tail);
24996 if (EQ (propval, tem))
24997 return 1;
24998 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24999 return NILP (XCDR (tem)) ? 1 : 2;
25002 if (CONSP (propval))
25004 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25006 Lisp_Object propelt;
25007 propelt = XCAR (proptail);
25008 for (tail = list; CONSP (tail); tail = XCDR (tail))
25010 register Lisp_Object tem;
25011 tem = XCAR (tail);
25012 if (EQ (propelt, tem))
25013 return 1;
25014 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25015 return NILP (XCDR (tem)) ? 1 : 2;
25020 return 0;
25023 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25024 doc: /* Non-nil if the property makes the text invisible.
25025 POS-OR-PROP can be a marker or number, in which case it is taken to be
25026 a position in the current buffer and the value of the `invisible' property
25027 is checked; or it can be some other value, which is then presumed to be the
25028 value of the `invisible' property of the text of interest.
25029 The non-nil value returned can be t for truly invisible text or something
25030 else if the text is replaced by an ellipsis. */)
25031 (Lisp_Object pos_or_prop)
25033 Lisp_Object prop
25034 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
25035 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
25036 : pos_or_prop);
25037 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25038 return (invis == 0 ? Qnil
25039 : invis == 1 ? Qt
25040 : make_number (invis));
25043 /* Calculate a width or height in pixels from a specification using
25044 the following elements:
25046 SPEC ::=
25047 NUM - a (fractional) multiple of the default font width/height
25048 (NUM) - specifies exactly NUM pixels
25049 UNIT - a fixed number of pixels, see below.
25050 ELEMENT - size of a display element in pixels, see below.
25051 (NUM . SPEC) - equals NUM * SPEC
25052 (+ SPEC SPEC ...) - add pixel values
25053 (- SPEC SPEC ...) - subtract pixel values
25054 (- SPEC) - negate pixel value
25056 NUM ::=
25057 INT or FLOAT - a number constant
25058 SYMBOL - use symbol's (buffer local) variable binding.
25060 UNIT ::=
25061 in - pixels per inch *)
25062 mm - pixels per 1/1000 meter *)
25063 cm - pixels per 1/100 meter *)
25064 width - width of current font in pixels.
25065 height - height of current font in pixels.
25067 *) using the ratio(s) defined in display-pixels-per-inch.
25069 ELEMENT ::=
25071 left-fringe - left fringe width in pixels
25072 right-fringe - right fringe width in pixels
25074 left-margin - left margin width in pixels
25075 right-margin - right margin width in pixels
25077 scroll-bar - scroll-bar area width in pixels
25079 Examples:
25081 Pixels corresponding to 5 inches:
25082 (5 . in)
25084 Total width of non-text areas on left side of window (if scroll-bar is on left):
25085 '(space :width (+ left-fringe left-margin scroll-bar))
25087 Align to first text column (in header line):
25088 '(space :align-to 0)
25090 Align to middle of text area minus half the width of variable `my-image'
25091 containing a loaded image:
25092 '(space :align-to (0.5 . (- text my-image)))
25094 Width of left margin minus width of 1 character in the default font:
25095 '(space :width (- left-margin 1))
25097 Width of left margin minus width of 2 characters in the current font:
25098 '(space :width (- left-margin (2 . width)))
25100 Center 1 character over left-margin (in header line):
25101 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25103 Different ways to express width of left fringe plus left margin minus one pixel:
25104 '(space :width (- (+ left-fringe left-margin) (1)))
25105 '(space :width (+ left-fringe left-margin (- (1))))
25106 '(space :width (+ left-fringe left-margin (-1)))
25110 static bool
25111 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25112 struct font *font, bool width_p, int *align_to)
25114 double pixels;
25116 # define OK_PIXELS(val) (*res = (val), true)
25117 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25119 if (NILP (prop))
25120 return OK_PIXELS (0);
25122 eassert (FRAME_LIVE_P (it->f));
25124 if (SYMBOLP (prop))
25126 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25128 char *unit = SSDATA (SYMBOL_NAME (prop));
25130 if (unit[0] == 'i' && unit[1] == 'n')
25131 pixels = 1.0;
25132 else if (unit[0] == 'm' && unit[1] == 'm')
25133 pixels = 25.4;
25134 else if (unit[0] == 'c' && unit[1] == 'm')
25135 pixels = 2.54;
25136 else
25137 pixels = 0;
25138 if (pixels > 0)
25140 double ppi = (width_p ? FRAME_RES_X (it->f)
25141 : FRAME_RES_Y (it->f));
25143 if (ppi > 0)
25144 return OK_PIXELS (ppi / pixels);
25145 return false;
25149 #ifdef HAVE_WINDOW_SYSTEM
25150 if (EQ (prop, Qheight))
25151 return OK_PIXELS (font
25152 ? normal_char_height (font, -1)
25153 : FRAME_LINE_HEIGHT (it->f));
25154 if (EQ (prop, Qwidth))
25155 return OK_PIXELS (font
25156 ? FONT_WIDTH (font)
25157 : FRAME_COLUMN_WIDTH (it->f));
25158 #else
25159 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25160 return OK_PIXELS (1);
25161 #endif
25163 if (EQ (prop, Qtext))
25164 return OK_PIXELS (width_p
25165 ? window_box_width (it->w, TEXT_AREA)
25166 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25168 if (align_to && *align_to < 0)
25170 *res = 0;
25171 if (EQ (prop, Qleft))
25172 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
25173 if (EQ (prop, Qright))
25174 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25175 if (EQ (prop, Qcenter))
25176 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25177 + window_box_width (it->w, TEXT_AREA) / 2);
25178 if (EQ (prop, Qleft_fringe))
25179 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25180 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25181 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25182 if (EQ (prop, Qright_fringe))
25183 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25184 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25185 : window_box_right_offset (it->w, TEXT_AREA));
25186 if (EQ (prop, Qleft_margin))
25187 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25188 if (EQ (prop, Qright_margin))
25189 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25190 if (EQ (prop, Qscroll_bar))
25191 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25193 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25194 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25195 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25196 : 0)));
25198 else
25200 if (EQ (prop, Qleft_fringe))
25201 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25202 if (EQ (prop, Qright_fringe))
25203 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25204 if (EQ (prop, Qleft_margin))
25205 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25206 if (EQ (prop, Qright_margin))
25207 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25208 if (EQ (prop, Qscroll_bar))
25209 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25212 prop = buffer_local_value (prop, it->w->contents);
25213 if (EQ (prop, Qunbound))
25214 prop = Qnil;
25217 if (NUMBERP (prop))
25219 int base_unit = (width_p
25220 ? FRAME_COLUMN_WIDTH (it->f)
25221 : FRAME_LINE_HEIGHT (it->f));
25222 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25225 if (CONSP (prop))
25227 Lisp_Object car = XCAR (prop);
25228 Lisp_Object cdr = XCDR (prop);
25230 if (SYMBOLP (car))
25232 #ifdef HAVE_WINDOW_SYSTEM
25233 if (FRAME_WINDOW_P (it->f)
25234 && valid_image_p (prop))
25236 ptrdiff_t id = lookup_image (it->f, prop);
25237 struct image *img = IMAGE_FROM_ID (it->f, id);
25239 return OK_PIXELS (width_p ? img->width : img->height);
25241 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25243 /* TODO: Don't return dummy size. */
25244 return OK_PIXELS (100);
25246 #endif
25247 if (EQ (car, Qplus) || EQ (car, Qminus))
25249 bool first = true;
25250 double px;
25252 pixels = 0;
25253 while (CONSP (cdr))
25255 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25256 font, width_p, align_to))
25257 return false;
25258 if (first)
25259 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25260 else
25261 pixels += px;
25262 cdr = XCDR (cdr);
25264 if (EQ (car, Qminus))
25265 pixels = -pixels;
25266 return OK_PIXELS (pixels);
25269 car = buffer_local_value (car, it->w->contents);
25270 if (EQ (car, Qunbound))
25271 car = Qnil;
25274 if (NUMBERP (car))
25276 double fact;
25277 pixels = XFLOATINT (car);
25278 if (NILP (cdr))
25279 return OK_PIXELS (pixels);
25280 if (calc_pixel_width_or_height (&fact, it, cdr,
25281 font, width_p, align_to))
25282 return OK_PIXELS (pixels * fact);
25283 return false;
25286 return false;
25289 return false;
25292 void
25293 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25295 #ifdef HAVE_WINDOW_SYSTEM
25296 normal_char_ascent_descent (font, -1, ascent, descent);
25297 #else
25298 *ascent = 1;
25299 *descent = 0;
25300 #endif
25304 /***********************************************************************
25305 Glyph Display
25306 ***********************************************************************/
25308 #ifdef HAVE_WINDOW_SYSTEM
25310 #ifdef GLYPH_DEBUG
25312 void
25313 dump_glyph_string (struct glyph_string *s)
25315 fprintf (stderr, "glyph string\n");
25316 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25317 s->x, s->y, s->width, s->height);
25318 fprintf (stderr, " ybase = %d\n", s->ybase);
25319 fprintf (stderr, " hl = %u\n", s->hl);
25320 fprintf (stderr, " left overhang = %d, right = %d\n",
25321 s->left_overhang, s->right_overhang);
25322 fprintf (stderr, " nchars = %d\n", s->nchars);
25323 fprintf (stderr, " extends to end of line = %d\n",
25324 s->extends_to_end_of_line_p);
25325 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25326 fprintf (stderr, " bg width = %d\n", s->background_width);
25329 #endif /* GLYPH_DEBUG */
25331 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25332 of XChar2b structures for S; it can't be allocated in
25333 init_glyph_string because it must be allocated via `alloca'. W
25334 is the window on which S is drawn. ROW and AREA are the glyph row
25335 and area within the row from which S is constructed. START is the
25336 index of the first glyph structure covered by S. HL is a
25337 face-override for drawing S. */
25339 #ifdef HAVE_NTGUI
25340 #define OPTIONAL_HDC(hdc) HDC hdc,
25341 #define DECLARE_HDC(hdc) HDC hdc;
25342 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25343 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25344 #endif
25346 #ifndef OPTIONAL_HDC
25347 #define OPTIONAL_HDC(hdc)
25348 #define DECLARE_HDC(hdc)
25349 #define ALLOCATE_HDC(hdc, f)
25350 #define RELEASE_HDC(hdc, f)
25351 #endif
25353 static void
25354 init_glyph_string (struct glyph_string *s,
25355 OPTIONAL_HDC (hdc)
25356 XChar2b *char2b, struct window *w, struct glyph_row *row,
25357 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25359 memset (s, 0, sizeof *s);
25360 s->w = w;
25361 s->f = XFRAME (w->frame);
25362 #ifdef HAVE_NTGUI
25363 s->hdc = hdc;
25364 #endif
25365 s->display = FRAME_X_DISPLAY (s->f);
25366 s->char2b = char2b;
25367 s->hl = hl;
25368 s->row = row;
25369 s->area = area;
25370 s->first_glyph = row->glyphs[area] + start;
25371 s->height = row->height;
25372 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25373 s->ybase = s->y + row->ascent;
25377 /* Append the list of glyph strings with head H and tail T to the list
25378 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25380 static void
25381 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25382 struct glyph_string *h, struct glyph_string *t)
25384 if (h)
25386 if (*head)
25387 (*tail)->next = h;
25388 else
25389 *head = h;
25390 h->prev = *tail;
25391 *tail = t;
25396 /* Prepend the list of glyph strings with head H and tail T to the
25397 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25398 result. */
25400 static void
25401 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25402 struct glyph_string *h, struct glyph_string *t)
25404 if (h)
25406 if (*head)
25407 (*head)->prev = t;
25408 else
25409 *tail = t;
25410 t->next = *head;
25411 *head = h;
25416 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25417 Set *HEAD and *TAIL to the resulting list. */
25419 static void
25420 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25421 struct glyph_string *s)
25423 s->next = s->prev = NULL;
25424 append_glyph_string_lists (head, tail, s, s);
25428 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25429 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25430 make sure that X resources for the face returned are allocated.
25431 Value is a pointer to a realized face that is ready for display if
25432 DISPLAY_P. */
25434 static struct face *
25435 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25436 XChar2b *char2b, bool display_p)
25438 struct face *face = FACE_FROM_ID (f, face_id);
25439 unsigned code = 0;
25441 if (face->font)
25443 code = face->font->driver->encode_char (face->font, c);
25445 if (code == FONT_INVALID_CODE)
25446 code = 0;
25448 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25450 /* Make sure X resources of the face are allocated. */
25451 #ifdef HAVE_X_WINDOWS
25452 if (display_p)
25453 #endif
25455 eassert (face != NULL);
25456 prepare_face_for_display (f, face);
25459 return face;
25463 /* Get face and two-byte form of character glyph GLYPH on frame F.
25464 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25465 a pointer to a realized face that is ready for display. */
25467 static struct face *
25468 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25469 XChar2b *char2b)
25471 struct face *face;
25472 unsigned code = 0;
25474 eassert (glyph->type == CHAR_GLYPH);
25475 face = FACE_FROM_ID (f, glyph->face_id);
25477 /* Make sure X resources of the face are allocated. */
25478 prepare_face_for_display (f, face);
25480 if (face->font)
25482 if (CHAR_BYTE8_P (glyph->u.ch))
25483 code = CHAR_TO_BYTE8 (glyph->u.ch);
25484 else
25485 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25487 if (code == FONT_INVALID_CODE)
25488 code = 0;
25491 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25492 return face;
25496 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25497 Return true iff FONT has a glyph for C. */
25499 static bool
25500 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25502 unsigned code;
25504 if (CHAR_BYTE8_P (c))
25505 code = CHAR_TO_BYTE8 (c);
25506 else
25507 code = font->driver->encode_char (font, c);
25509 if (code == FONT_INVALID_CODE)
25510 return false;
25511 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25512 return true;
25516 /* Fill glyph string S with composition components specified by S->cmp.
25518 BASE_FACE is the base face of the composition.
25519 S->cmp_from is the index of the first component for S.
25521 OVERLAPS non-zero means S should draw the foreground only, and use
25522 its physical height for clipping. See also draw_glyphs.
25524 Value is the index of a component not in S. */
25526 static int
25527 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25528 int overlaps)
25530 int i;
25531 /* For all glyphs of this composition, starting at the offset
25532 S->cmp_from, until we reach the end of the definition or encounter a
25533 glyph that requires the different face, add it to S. */
25534 struct face *face;
25536 eassert (s);
25538 s->for_overlaps = overlaps;
25539 s->face = NULL;
25540 s->font = NULL;
25541 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25543 int c = COMPOSITION_GLYPH (s->cmp, i);
25545 /* TAB in a composition means display glyphs with padding space
25546 on the left or right. */
25547 if (c != '\t')
25549 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25550 -1, Qnil);
25552 face = get_char_face_and_encoding (s->f, c, face_id,
25553 s->char2b + i, true);
25554 if (face)
25556 if (! s->face)
25558 s->face = face;
25559 s->font = s->face->font;
25561 else if (s->face != face)
25562 break;
25565 ++s->nchars;
25567 s->cmp_to = i;
25569 if (s->face == NULL)
25571 s->face = base_face->ascii_face;
25572 s->font = s->face->font;
25575 /* All glyph strings for the same composition has the same width,
25576 i.e. the width set for the first component of the composition. */
25577 s->width = s->first_glyph->pixel_width;
25579 /* If the specified font could not be loaded, use the frame's
25580 default font, but record the fact that we couldn't load it in
25581 the glyph string so that we can draw rectangles for the
25582 characters of the glyph string. */
25583 if (s->font == NULL)
25585 s->font_not_found_p = true;
25586 s->font = FRAME_FONT (s->f);
25589 /* Adjust base line for subscript/superscript text. */
25590 s->ybase += s->first_glyph->voffset;
25592 return s->cmp_to;
25595 static int
25596 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25597 int start, int end, int overlaps)
25599 struct glyph *glyph, *last;
25600 Lisp_Object lgstring;
25601 int i;
25603 s->for_overlaps = overlaps;
25604 glyph = s->row->glyphs[s->area] + start;
25605 last = s->row->glyphs[s->area] + end;
25606 s->cmp_id = glyph->u.cmp.id;
25607 s->cmp_from = glyph->slice.cmp.from;
25608 s->cmp_to = glyph->slice.cmp.to + 1;
25609 s->face = FACE_FROM_ID (s->f, face_id);
25610 lgstring = composition_gstring_from_id (s->cmp_id);
25611 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25612 glyph++;
25613 while (glyph < last
25614 && glyph->u.cmp.automatic
25615 && glyph->u.cmp.id == s->cmp_id
25616 && s->cmp_to == glyph->slice.cmp.from)
25617 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25619 for (i = s->cmp_from; i < s->cmp_to; i++)
25621 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25622 unsigned code = LGLYPH_CODE (lglyph);
25624 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25626 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25627 return glyph - s->row->glyphs[s->area];
25631 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25632 See the comment of fill_glyph_string for arguments.
25633 Value is the index of the first glyph not in S. */
25636 static int
25637 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25638 int start, int end, int overlaps)
25640 struct glyph *glyph, *last;
25641 int voffset;
25643 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25644 s->for_overlaps = overlaps;
25645 glyph = s->row->glyphs[s->area] + start;
25646 last = s->row->glyphs[s->area] + end;
25647 voffset = glyph->voffset;
25648 s->face = FACE_FROM_ID (s->f, face_id);
25649 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25650 s->nchars = 1;
25651 s->width = glyph->pixel_width;
25652 glyph++;
25653 while (glyph < last
25654 && glyph->type == GLYPHLESS_GLYPH
25655 && glyph->voffset == voffset
25656 && glyph->face_id == face_id)
25658 s->nchars++;
25659 s->width += glyph->pixel_width;
25660 glyph++;
25662 s->ybase += voffset;
25663 return glyph - s->row->glyphs[s->area];
25667 /* Fill glyph string S from a sequence of character glyphs.
25669 FACE_ID is the face id of the string. START is the index of the
25670 first glyph to consider, END is the index of the last + 1.
25671 OVERLAPS non-zero means S should draw the foreground only, and use
25672 its physical height for clipping. See also draw_glyphs.
25674 Value is the index of the first glyph not in S. */
25676 static int
25677 fill_glyph_string (struct glyph_string *s, int face_id,
25678 int start, int end, int overlaps)
25680 struct glyph *glyph, *last;
25681 int voffset;
25682 bool glyph_not_available_p;
25684 eassert (s->f == XFRAME (s->w->frame));
25685 eassert (s->nchars == 0);
25686 eassert (start >= 0 && end > start);
25688 s->for_overlaps = overlaps;
25689 glyph = s->row->glyphs[s->area] + start;
25690 last = s->row->glyphs[s->area] + end;
25691 voffset = glyph->voffset;
25692 s->padding_p = glyph->padding_p;
25693 glyph_not_available_p = glyph->glyph_not_available_p;
25695 while (glyph < last
25696 && glyph->type == CHAR_GLYPH
25697 && glyph->voffset == voffset
25698 /* Same face id implies same font, nowadays. */
25699 && glyph->face_id == face_id
25700 && glyph->glyph_not_available_p == glyph_not_available_p)
25702 s->face = get_glyph_face_and_encoding (s->f, glyph,
25703 s->char2b + s->nchars);
25704 ++s->nchars;
25705 eassert (s->nchars <= end - start);
25706 s->width += glyph->pixel_width;
25707 if (glyph++->padding_p != s->padding_p)
25708 break;
25711 s->font = s->face->font;
25713 /* If the specified font could not be loaded, use the frame's font,
25714 but record the fact that we couldn't load it in
25715 S->font_not_found_p so that we can draw rectangles for the
25716 characters of the glyph string. */
25717 if (s->font == NULL || glyph_not_available_p)
25719 s->font_not_found_p = true;
25720 s->font = FRAME_FONT (s->f);
25723 /* Adjust base line for subscript/superscript text. */
25724 s->ybase += voffset;
25726 eassert (s->face && s->face->gc);
25727 return glyph - s->row->glyphs[s->area];
25731 /* Fill glyph string S from image glyph S->first_glyph. */
25733 static void
25734 fill_image_glyph_string (struct glyph_string *s)
25736 eassert (s->first_glyph->type == IMAGE_GLYPH);
25737 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25738 eassert (s->img);
25739 s->slice = s->first_glyph->slice.img;
25740 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25741 s->font = s->face->font;
25742 s->width = s->first_glyph->pixel_width;
25744 /* Adjust base line for subscript/superscript text. */
25745 s->ybase += s->first_glyph->voffset;
25749 #ifdef HAVE_XWIDGETS
25750 static void
25751 fill_xwidget_glyph_string (struct glyph_string *s)
25753 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25754 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25755 s->font = s->face->font;
25756 s->width = s->first_glyph->pixel_width;
25757 s->ybase += s->first_glyph->voffset;
25758 s->xwidget = s->first_glyph->u.xwidget;
25760 #endif
25761 /* Fill glyph string S from a sequence of stretch glyphs.
25763 START is the index of the first glyph to consider,
25764 END is the index of the last + 1.
25766 Value is the index of the first glyph not in S. */
25768 static int
25769 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25771 struct glyph *glyph, *last;
25772 int voffset, face_id;
25774 eassert (s->first_glyph->type == STRETCH_GLYPH);
25776 glyph = s->row->glyphs[s->area] + start;
25777 last = s->row->glyphs[s->area] + end;
25778 face_id = glyph->face_id;
25779 s->face = FACE_FROM_ID (s->f, face_id);
25780 s->font = s->face->font;
25781 s->width = glyph->pixel_width;
25782 s->nchars = 1;
25783 voffset = glyph->voffset;
25785 for (++glyph;
25786 (glyph < last
25787 && glyph->type == STRETCH_GLYPH
25788 && glyph->voffset == voffset
25789 && glyph->face_id == face_id);
25790 ++glyph)
25791 s->width += glyph->pixel_width;
25793 /* Adjust base line for subscript/superscript text. */
25794 s->ybase += voffset;
25796 /* The case that face->gc == 0 is handled when drawing the glyph
25797 string by calling prepare_face_for_display. */
25798 eassert (s->face);
25799 return glyph - s->row->glyphs[s->area];
25802 static struct font_metrics *
25803 get_per_char_metric (struct font *font, XChar2b *char2b)
25805 static struct font_metrics metrics;
25806 unsigned code;
25808 if (! font)
25809 return NULL;
25810 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25811 if (code == FONT_INVALID_CODE)
25812 return NULL;
25813 font->driver->text_extents (font, &code, 1, &metrics);
25814 return &metrics;
25817 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25818 for FONT. Values are taken from font-global ones, except for fonts
25819 that claim preposterously large values, but whose glyphs actually
25820 have reasonable dimensions. C is the character to use for metrics
25821 if the font-global values are too large; if C is negative, the
25822 function selects a default character. */
25823 static void
25824 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25826 *ascent = FONT_BASE (font);
25827 *descent = FONT_DESCENT (font);
25829 if (FONT_TOO_HIGH (font))
25831 XChar2b char2b;
25833 /* Get metrics of C, defaulting to a reasonably sized ASCII
25834 character. */
25835 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25837 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25839 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25841 /* We add 1 pixel to character dimensions as heuristics
25842 that produces nicer display, e.g. when the face has
25843 the box attribute. */
25844 *ascent = pcm->ascent + 1;
25845 *descent = pcm->descent + 1;
25851 /* A subroutine that computes a reasonable "normal character height"
25852 for fonts that claim preposterously large vertical dimensions, but
25853 whose glyphs are actually reasonably sized. C is the character
25854 whose metrics to use for those fonts, or -1 for default
25855 character. */
25856 static int
25857 normal_char_height (struct font *font, int c)
25859 int ascent, descent;
25861 normal_char_ascent_descent (font, c, &ascent, &descent);
25863 return ascent + descent;
25866 /* EXPORT for RIF:
25867 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25868 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25869 assumed to be zero. */
25871 void
25872 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25874 *left = *right = 0;
25876 if (glyph->type == CHAR_GLYPH)
25878 XChar2b char2b;
25879 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25880 if (face->font)
25882 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25883 if (pcm)
25885 if (pcm->rbearing > pcm->width)
25886 *right = pcm->rbearing - pcm->width;
25887 if (pcm->lbearing < 0)
25888 *left = -pcm->lbearing;
25892 else if (glyph->type == COMPOSITE_GLYPH)
25894 if (! glyph->u.cmp.automatic)
25896 struct composition *cmp = composition_table[glyph->u.cmp.id];
25898 if (cmp->rbearing > cmp->pixel_width)
25899 *right = cmp->rbearing - cmp->pixel_width;
25900 if (cmp->lbearing < 0)
25901 *left = - cmp->lbearing;
25903 else
25905 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25906 struct font_metrics metrics;
25908 composition_gstring_width (gstring, glyph->slice.cmp.from,
25909 glyph->slice.cmp.to + 1, &metrics);
25910 if (metrics.rbearing > metrics.width)
25911 *right = metrics.rbearing - metrics.width;
25912 if (metrics.lbearing < 0)
25913 *left = - metrics.lbearing;
25919 /* Return the index of the first glyph preceding glyph string S that
25920 is overwritten by S because of S's left overhang. Value is -1
25921 if no glyphs are overwritten. */
25923 static int
25924 left_overwritten (struct glyph_string *s)
25926 int k;
25928 if (s->left_overhang)
25930 int x = 0, i;
25931 struct glyph *glyphs = s->row->glyphs[s->area];
25932 int first = s->first_glyph - glyphs;
25934 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25935 x -= glyphs[i].pixel_width;
25937 k = i + 1;
25939 else
25940 k = -1;
25942 return k;
25946 /* Return the index of the first glyph preceding glyph string S that
25947 is overwriting S because of its right overhang. Value is -1 if no
25948 glyph in front of S overwrites S. */
25950 static int
25951 left_overwriting (struct glyph_string *s)
25953 int i, k, x;
25954 struct glyph *glyphs = s->row->glyphs[s->area];
25955 int first = s->first_glyph - glyphs;
25957 k = -1;
25958 x = 0;
25959 for (i = first - 1; i >= 0; --i)
25961 int left, right;
25962 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25963 if (x + right > 0)
25964 k = i;
25965 x -= glyphs[i].pixel_width;
25968 return k;
25972 /* Return the index of the last glyph following glyph string S that is
25973 overwritten by S because of S's right overhang. Value is -1 if
25974 no such glyph is found. */
25976 static int
25977 right_overwritten (struct glyph_string *s)
25979 int k = -1;
25981 if (s->right_overhang)
25983 int x = 0, i;
25984 struct glyph *glyphs = s->row->glyphs[s->area];
25985 int first = (s->first_glyph - glyphs
25986 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25987 int end = s->row->used[s->area];
25989 for (i = first; i < end && s->right_overhang > x; ++i)
25990 x += glyphs[i].pixel_width;
25992 k = i;
25995 return k;
25999 /* Return the index of the last glyph following glyph string S that
26000 overwrites S because of its left overhang. Value is negative
26001 if no such glyph is found. */
26003 static int
26004 right_overwriting (struct glyph_string *s)
26006 int i, k, x;
26007 int end = s->row->used[s->area];
26008 struct glyph *glyphs = s->row->glyphs[s->area];
26009 int first = (s->first_glyph - glyphs
26010 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26012 k = -1;
26013 x = 0;
26014 for (i = first; i < end; ++i)
26016 int left, right;
26017 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26018 if (x - left < 0)
26019 k = i;
26020 x += glyphs[i].pixel_width;
26023 return k;
26027 /* Set background width of glyph string S. START is the index of the
26028 first glyph following S. LAST_X is the right-most x-position + 1
26029 in the drawing area. */
26031 static void
26032 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26034 /* If the face of this glyph string has to be drawn to the end of
26035 the drawing area, set S->extends_to_end_of_line_p. */
26037 if (start == s->row->used[s->area]
26038 && ((s->row->fill_line_p
26039 && (s->hl == DRAW_NORMAL_TEXT
26040 || s->hl == DRAW_IMAGE_RAISED
26041 || s->hl == DRAW_IMAGE_SUNKEN))
26042 || s->hl == DRAW_MOUSE_FACE))
26043 s->extends_to_end_of_line_p = true;
26045 /* If S extends its face to the end of the line, set its
26046 background_width to the distance to the right edge of the drawing
26047 area. */
26048 if (s->extends_to_end_of_line_p)
26049 s->background_width = last_x - s->x + 1;
26050 else
26051 s->background_width = s->width;
26055 /* Return glyph string that shares background with glyph string S and
26056 whose `background_width' member has been set. */
26058 static struct glyph_string *
26059 glyph_string_containing_background_width (struct glyph_string *s)
26061 if (s->cmp)
26062 while (s->cmp_from)
26063 s = s->prev;
26065 return s;
26069 /* Compute overhangs and x-positions for glyph string S and its
26070 predecessors, or successors. X is the starting x-position for S.
26071 BACKWARD_P means process predecessors. */
26073 static void
26074 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26076 if (backward_p)
26078 while (s)
26080 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26081 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26082 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26083 x -= s->width;
26084 s->x = x;
26085 s = s->prev;
26088 else
26090 while (s)
26092 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26093 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26094 s->x = x;
26095 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26096 x += s->width;
26097 s = s->next;
26104 /* The following macros are only called from draw_glyphs below.
26105 They reference the following parameters of that function directly:
26106 `w', `row', `area', and `overlap_p'
26107 as well as the following local variables:
26108 `s', `f', and `hdc' (in W32) */
26110 #ifdef HAVE_NTGUI
26111 /* On W32, silently add local `hdc' variable to argument list of
26112 init_glyph_string. */
26113 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26114 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26115 #else
26116 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26117 init_glyph_string (s, char2b, w, row, area, start, hl)
26118 #endif
26120 /* Add a glyph string for a stretch glyph to the list of strings
26121 between HEAD and TAIL. START is the index of the stretch glyph in
26122 row area AREA of glyph row ROW. END is the index of the last glyph
26123 in that glyph row area. X is the current output position assigned
26124 to the new glyph string constructed. HL overrides that face of the
26125 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26126 is the right-most x-position of the drawing area. */
26128 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26129 and below -- keep them on one line. */
26130 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26131 do \
26133 s = alloca (sizeof *s); \
26134 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26135 START = fill_stretch_glyph_string (s, START, END); \
26136 append_glyph_string (&HEAD, &TAIL, s); \
26137 s->x = (X); \
26139 while (false)
26142 /* Add a glyph string for an image glyph to the list of strings
26143 between HEAD and TAIL. START is the index of the image glyph in
26144 row area AREA of glyph row ROW. END is the index of the last glyph
26145 in that glyph row area. X is the current output position assigned
26146 to the new glyph string constructed. HL overrides that face of the
26147 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26148 is the right-most x-position of the drawing area. */
26150 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26151 do \
26153 s = alloca (sizeof *s); \
26154 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26155 fill_image_glyph_string (s); \
26156 append_glyph_string (&HEAD, &TAIL, s); \
26157 ++START; \
26158 s->x = (X); \
26160 while (false)
26162 #ifndef HAVE_XWIDGETS
26163 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26164 eassume (false)
26165 #else
26166 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26167 do \
26169 s = alloca (sizeof *s); \
26170 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26171 fill_xwidget_glyph_string (s); \
26172 append_glyph_string (&(HEAD), &(TAIL), s); \
26173 ++(START); \
26174 s->x = (X); \
26176 while (false)
26177 #endif
26179 /* Add a glyph string for a sequence of character glyphs to the list
26180 of strings between HEAD and TAIL. START is the index of the first
26181 glyph in row area AREA of glyph row ROW that is part of the new
26182 glyph string. END is the index of the last glyph in that glyph row
26183 area. X is the current output position assigned to the new glyph
26184 string constructed. HL overrides that face of the glyph; e.g. it
26185 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26186 right-most x-position of the drawing area. */
26188 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26189 do \
26191 int face_id; \
26192 XChar2b *char2b; \
26194 face_id = (row)->glyphs[area][START].face_id; \
26196 s = alloca (sizeof *s); \
26197 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26198 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26199 append_glyph_string (&HEAD, &TAIL, s); \
26200 s->x = (X); \
26201 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26203 while (false)
26206 /* Add a glyph string for a composite sequence to the list of strings
26207 between HEAD and TAIL. START is the index of the first glyph in
26208 row area AREA of glyph row ROW that is part of the new glyph
26209 string. END is the index of the last glyph in that glyph row area.
26210 X is the current output position assigned to the new glyph string
26211 constructed. HL overrides that face of the glyph; e.g. it is
26212 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26213 x-position of the drawing area. */
26215 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26216 do { \
26217 int face_id = (row)->glyphs[area][START].face_id; \
26218 struct face *base_face = FACE_FROM_ID (f, face_id); \
26219 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26220 struct composition *cmp = composition_table[cmp_id]; \
26221 XChar2b *char2b; \
26222 struct glyph_string *first_s = NULL; \
26223 int n; \
26225 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26227 /* Make glyph_strings for each glyph sequence that is drawable by \
26228 the same face, and append them to HEAD/TAIL. */ \
26229 for (n = 0; n < cmp->glyph_len;) \
26231 s = alloca (sizeof *s); \
26232 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26233 append_glyph_string (&(HEAD), &(TAIL), s); \
26234 s->cmp = cmp; \
26235 s->cmp_from = n; \
26236 s->x = (X); \
26237 if (n == 0) \
26238 first_s = s; \
26239 n = fill_composite_glyph_string (s, base_face, overlaps); \
26242 ++START; \
26243 s = first_s; \
26244 } while (false)
26247 /* Add a glyph string for a glyph-string sequence to the list of strings
26248 between HEAD and TAIL. */
26250 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26251 do { \
26252 int face_id; \
26253 XChar2b *char2b; \
26254 Lisp_Object gstring; \
26256 face_id = (row)->glyphs[area][START].face_id; \
26257 gstring = (composition_gstring_from_id \
26258 ((row)->glyphs[area][START].u.cmp.id)); \
26259 s = alloca (sizeof *s); \
26260 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26261 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26262 append_glyph_string (&(HEAD), &(TAIL), s); \
26263 s->x = (X); \
26264 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26265 } while (false)
26268 /* Add a glyph string for a sequence of glyphless character's glyphs
26269 to the list of strings between HEAD and TAIL. The meanings of
26270 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26272 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26273 do \
26275 int face_id; \
26277 face_id = (row)->glyphs[area][START].face_id; \
26279 s = alloca (sizeof *s); \
26280 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26281 append_glyph_string (&HEAD, &TAIL, s); \
26282 s->x = (X); \
26283 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26284 overlaps); \
26286 while (false)
26289 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26290 of AREA of glyph row ROW on window W between indices START and END.
26291 HL overrides the face for drawing glyph strings, e.g. it is
26292 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26293 x-positions of the drawing area.
26295 This is an ugly monster macro construct because we must use alloca
26296 to allocate glyph strings (because draw_glyphs can be called
26297 asynchronously). */
26299 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26300 do \
26302 HEAD = TAIL = NULL; \
26303 while (START < END) \
26305 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26306 switch (first_glyph->type) \
26308 case CHAR_GLYPH: \
26309 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26310 HL, X, LAST_X); \
26311 break; \
26313 case COMPOSITE_GLYPH: \
26314 if (first_glyph->u.cmp.automatic) \
26315 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26316 HL, X, LAST_X); \
26317 else \
26318 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26319 HL, X, LAST_X); \
26320 break; \
26322 case STRETCH_GLYPH: \
26323 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26324 HL, X, LAST_X); \
26325 break; \
26327 case IMAGE_GLYPH: \
26328 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26329 HL, X, LAST_X); \
26330 break;
26332 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26333 case XWIDGET_GLYPH: \
26334 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26335 HL, X, LAST_X); \
26336 break;
26338 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26339 case GLYPHLESS_GLYPH: \
26340 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26341 HL, X, LAST_X); \
26342 break; \
26344 default: \
26345 emacs_abort (); \
26348 if (s) \
26350 set_glyph_string_background_width (s, START, LAST_X); \
26351 (X) += s->width; \
26354 } while (false)
26357 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26358 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26359 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26360 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26363 /* Draw glyphs between START and END in AREA of ROW on window W,
26364 starting at x-position X. X is relative to AREA in W. HL is a
26365 face-override with the following meaning:
26367 DRAW_NORMAL_TEXT draw normally
26368 DRAW_CURSOR draw in cursor face
26369 DRAW_MOUSE_FACE draw in mouse face.
26370 DRAW_INVERSE_VIDEO draw in mode line face
26371 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26372 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26374 If OVERLAPS is non-zero, draw only the foreground of characters and
26375 clip to the physical height of ROW. Non-zero value also defines
26376 the overlapping part to be drawn:
26378 OVERLAPS_PRED overlap with preceding rows
26379 OVERLAPS_SUCC overlap with succeeding rows
26380 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26381 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26383 Value is the x-position reached, relative to AREA of W. */
26385 static int
26386 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26387 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26388 enum draw_glyphs_face hl, int overlaps)
26390 struct glyph_string *head, *tail;
26391 struct glyph_string *s;
26392 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26393 int i, j, x_reached, last_x, area_left = 0;
26394 struct frame *f = XFRAME (WINDOW_FRAME (w));
26395 DECLARE_HDC (hdc);
26397 ALLOCATE_HDC (hdc, f);
26399 /* Let's rather be paranoid than getting a SEGV. */
26400 end = min (end, row->used[area]);
26401 start = clip_to_bounds (0, start, end);
26403 /* Translate X to frame coordinates. Set last_x to the right
26404 end of the drawing area. */
26405 if (row->full_width_p)
26407 /* X is relative to the left edge of W, without scroll bars
26408 or fringes. */
26409 area_left = WINDOW_LEFT_EDGE_X (w);
26410 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26411 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26413 else
26415 area_left = window_box_left (w, area);
26416 last_x = area_left + window_box_width (w, area);
26418 x += area_left;
26420 /* Build a doubly-linked list of glyph_string structures between
26421 head and tail from what we have to draw. Note that the macro
26422 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26423 the reason we use a separate variable `i'. */
26424 i = start;
26425 USE_SAFE_ALLOCA;
26426 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26427 if (tail)
26429 s = glyph_string_containing_background_width (tail);
26430 x_reached = s->x + s->background_width;
26432 else
26433 x_reached = x;
26435 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26436 the row, redraw some glyphs in front or following the glyph
26437 strings built above. */
26438 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26440 struct glyph_string *h, *t;
26441 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26442 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26443 bool check_mouse_face = false;
26444 int dummy_x = 0;
26446 /* If mouse highlighting is on, we may need to draw adjacent
26447 glyphs using mouse-face highlighting. */
26448 if (area == TEXT_AREA && row->mouse_face_p
26449 && hlinfo->mouse_face_beg_row >= 0
26450 && hlinfo->mouse_face_end_row >= 0)
26452 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26454 if (row_vpos >= hlinfo->mouse_face_beg_row
26455 && row_vpos <= hlinfo->mouse_face_end_row)
26457 check_mouse_face = true;
26458 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26459 ? hlinfo->mouse_face_beg_col : 0;
26460 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26461 ? hlinfo->mouse_face_end_col
26462 : row->used[TEXT_AREA];
26466 /* Compute overhangs for all glyph strings. */
26467 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26468 for (s = head; s; s = s->next)
26469 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26471 /* Prepend glyph strings for glyphs in front of the first glyph
26472 string that are overwritten because of the first glyph
26473 string's left overhang. The background of all strings
26474 prepended must be drawn because the first glyph string
26475 draws over it. */
26476 i = left_overwritten (head);
26477 if (i >= 0)
26479 enum draw_glyphs_face overlap_hl;
26481 /* If this row contains mouse highlighting, attempt to draw
26482 the overlapped glyphs with the correct highlight. This
26483 code fails if the overlap encompasses more than one glyph
26484 and mouse-highlight spans only some of these glyphs.
26485 However, making it work perfectly involves a lot more
26486 code, and I don't know if the pathological case occurs in
26487 practice, so we'll stick to this for now. --- cyd */
26488 if (check_mouse_face
26489 && mouse_beg_col < start && mouse_end_col > i)
26490 overlap_hl = DRAW_MOUSE_FACE;
26491 else
26492 overlap_hl = DRAW_NORMAL_TEXT;
26494 if (hl != overlap_hl)
26495 clip_head = head;
26496 j = i;
26497 BUILD_GLYPH_STRINGS (j, start, h, t,
26498 overlap_hl, dummy_x, last_x);
26499 start = i;
26500 compute_overhangs_and_x (t, head->x, true);
26501 prepend_glyph_string_lists (&head, &tail, h, t);
26502 if (clip_head == NULL)
26503 clip_head = head;
26506 /* Prepend glyph strings for glyphs in front of the first glyph
26507 string that overwrite that glyph string because of their
26508 right overhang. For these strings, only the foreground must
26509 be drawn, because it draws over the glyph string at `head'.
26510 The background must not be drawn because this would overwrite
26511 right overhangs of preceding glyphs for which no glyph
26512 strings exist. */
26513 i = left_overwriting (head);
26514 if (i >= 0)
26516 enum draw_glyphs_face overlap_hl;
26518 if (check_mouse_face
26519 && mouse_beg_col < start && mouse_end_col > i)
26520 overlap_hl = DRAW_MOUSE_FACE;
26521 else
26522 overlap_hl = DRAW_NORMAL_TEXT;
26524 if (hl == overlap_hl || clip_head == NULL)
26525 clip_head = head;
26526 BUILD_GLYPH_STRINGS (i, start, h, t,
26527 overlap_hl, dummy_x, last_x);
26528 for (s = h; s; s = s->next)
26529 s->background_filled_p = true;
26530 compute_overhangs_and_x (t, head->x, true);
26531 prepend_glyph_string_lists (&head, &tail, h, t);
26534 /* Append glyphs strings for glyphs following the last glyph
26535 string tail that are overwritten by tail. The background of
26536 these strings has to be drawn because tail's foreground draws
26537 over it. */
26538 i = right_overwritten (tail);
26539 if (i >= 0)
26541 enum draw_glyphs_face overlap_hl;
26543 if (check_mouse_face
26544 && mouse_beg_col < i && mouse_end_col > end)
26545 overlap_hl = DRAW_MOUSE_FACE;
26546 else
26547 overlap_hl = DRAW_NORMAL_TEXT;
26549 if (hl != overlap_hl)
26550 clip_tail = tail;
26551 BUILD_GLYPH_STRINGS (end, i, h, t,
26552 overlap_hl, x, last_x);
26553 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26554 we don't have `end = i;' here. */
26555 compute_overhangs_and_x (h, tail->x + tail->width, false);
26556 append_glyph_string_lists (&head, &tail, h, t);
26557 if (clip_tail == NULL)
26558 clip_tail = tail;
26561 /* Append glyph strings for glyphs following the last glyph
26562 string tail that overwrite tail. The foreground of such
26563 glyphs has to be drawn because it writes into the background
26564 of tail. The background must not be drawn because it could
26565 paint over the foreground of following glyphs. */
26566 i = right_overwriting (tail);
26567 if (i >= 0)
26569 enum draw_glyphs_face overlap_hl;
26570 if (check_mouse_face
26571 && mouse_beg_col < i && mouse_end_col > end)
26572 overlap_hl = DRAW_MOUSE_FACE;
26573 else
26574 overlap_hl = DRAW_NORMAL_TEXT;
26576 if (hl == overlap_hl || clip_tail == NULL)
26577 clip_tail = tail;
26578 i++; /* We must include the Ith glyph. */
26579 BUILD_GLYPH_STRINGS (end, i, h, t,
26580 overlap_hl, x, last_x);
26581 for (s = h; s; s = s->next)
26582 s->background_filled_p = true;
26583 compute_overhangs_and_x (h, tail->x + tail->width, false);
26584 append_glyph_string_lists (&head, &tail, h, t);
26586 tail = glyph_string_containing_background_width (tail);
26587 if (clip_tail)
26588 clip_tail = glyph_string_containing_background_width (clip_tail);
26589 if (clip_head || clip_tail)
26590 for (s = head; s; s = s->next)
26592 s->clip_head = clip_head;
26593 s->clip_tail = clip_tail;
26597 /* Draw all strings. */
26598 for (s = head; s; s = s->next)
26599 FRAME_RIF (f)->draw_glyph_string (s);
26601 #ifndef HAVE_NS
26602 /* When focus a sole frame and move horizontally, this clears on_p
26603 causing a failure to erase prev cursor position. */
26604 if (area == TEXT_AREA
26605 && !row->full_width_p
26606 /* When drawing overlapping rows, only the glyph strings'
26607 foreground is drawn, which doesn't erase a cursor
26608 completely. */
26609 && !overlaps)
26611 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26612 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26613 : (tail ? tail->x + tail->background_width : x));
26614 x0 -= area_left;
26615 x1 -= area_left;
26617 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26618 row->y, MATRIX_ROW_BOTTOM_Y (row));
26620 #endif
26622 /* Value is the x-position up to which drawn, relative to AREA of W.
26623 This doesn't include parts drawn because of overhangs. */
26624 if (row->full_width_p)
26625 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26626 else
26627 x_reached -= area_left;
26629 RELEASE_HDC (hdc, f);
26631 SAFE_FREE ();
26632 return x_reached;
26635 /* Find the first glyph in the run of underlined glyphs preceding the
26636 beginning of glyph string S, and return its font (which could be
26637 NULL). This is needed because that font determines the underline
26638 position and thickness for the entire run of the underlined glyphs.
26639 This function is called from the draw_glyph_string method of GUI
26640 frame's redisplay interface (RIF) when it needs to draw in an
26641 underlined face. */
26642 struct font *
26643 font_for_underline_metrics (struct glyph_string *s)
26645 struct glyph *g0 = s->row->glyphs[s->area], *g;
26647 for (g = s->first_glyph - 1; g >= g0; g--)
26649 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26650 if (!(prev_face && prev_face->underline_p))
26651 break;
26654 /* If preceding glyphs are not underlined, use the font of S. */
26655 if (g == s->first_glyph - 1)
26656 return s->font;
26657 else
26659 /* Otherwise use the font of the last glyph we saw in the above
26660 loop whose face had the underline_p flag set. */
26661 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26665 /* Expand row matrix if too narrow. Don't expand if area
26666 is not present. */
26668 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26670 if (!it->f->fonts_changed \
26671 && (it->glyph_row->glyphs[area] \
26672 < it->glyph_row->glyphs[area + 1])) \
26674 it->w->ncols_scale_factor++; \
26675 it->f->fonts_changed = true; \
26679 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26680 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26682 static void
26683 append_glyph (struct it *it)
26685 struct glyph *glyph;
26686 enum glyph_row_area area = it->area;
26688 eassert (it->glyph_row);
26689 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26691 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26692 if (glyph < it->glyph_row->glyphs[area + 1])
26694 /* If the glyph row is reversed, we need to prepend the glyph
26695 rather than append it. */
26696 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26698 struct glyph *g;
26700 /* Make room for the additional glyph. */
26701 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26702 g[1] = *g;
26703 glyph = it->glyph_row->glyphs[area];
26705 glyph->charpos = CHARPOS (it->position);
26706 glyph->object = it->object;
26707 if (it->pixel_width > 0)
26709 eassert (it->pixel_width <= SHRT_MAX);
26710 glyph->pixel_width = it->pixel_width;
26711 glyph->padding_p = false;
26713 else
26715 /* Assure at least 1-pixel width. Otherwise, cursor can't
26716 be displayed correctly. */
26717 glyph->pixel_width = 1;
26718 glyph->padding_p = true;
26720 glyph->ascent = it->ascent;
26721 glyph->descent = it->descent;
26722 glyph->voffset = it->voffset;
26723 glyph->type = CHAR_GLYPH;
26724 glyph->avoid_cursor_p = it->avoid_cursor_p;
26725 glyph->multibyte_p = it->multibyte_p;
26726 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26728 /* In R2L rows, the left and the right box edges need to be
26729 drawn in reverse direction. */
26730 glyph->right_box_line_p = it->start_of_box_run_p;
26731 glyph->left_box_line_p = it->end_of_box_run_p;
26733 else
26735 glyph->left_box_line_p = it->start_of_box_run_p;
26736 glyph->right_box_line_p = it->end_of_box_run_p;
26738 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26739 || it->phys_descent > it->descent);
26740 glyph->glyph_not_available_p = it->glyph_not_available_p;
26741 glyph->face_id = it->face_id;
26742 glyph->u.ch = it->char_to_display;
26743 glyph->slice.img = null_glyph_slice;
26744 glyph->font_type = FONT_TYPE_UNKNOWN;
26745 if (it->bidi_p)
26747 glyph->resolved_level = it->bidi_it.resolved_level;
26748 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26749 glyph->bidi_type = it->bidi_it.type;
26751 else
26753 glyph->resolved_level = 0;
26754 glyph->bidi_type = UNKNOWN_BT;
26756 ++it->glyph_row->used[area];
26758 else
26759 IT_EXPAND_MATRIX_WIDTH (it, area);
26762 /* Store one glyph for the composition IT->cmp_it.id in
26763 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26764 non-null. */
26766 static void
26767 append_composite_glyph (struct it *it)
26769 struct glyph *glyph;
26770 enum glyph_row_area area = it->area;
26772 eassert (it->glyph_row);
26774 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26775 if (glyph < it->glyph_row->glyphs[area + 1])
26777 /* If the glyph row is reversed, we need to prepend the glyph
26778 rather than append it. */
26779 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26781 struct glyph *g;
26783 /* Make room for the new glyph. */
26784 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26785 g[1] = *g;
26786 glyph = it->glyph_row->glyphs[it->area];
26788 glyph->charpos = it->cmp_it.charpos;
26789 glyph->object = it->object;
26790 eassert (it->pixel_width <= SHRT_MAX);
26791 glyph->pixel_width = it->pixel_width;
26792 glyph->ascent = it->ascent;
26793 glyph->descent = it->descent;
26794 glyph->voffset = it->voffset;
26795 glyph->type = COMPOSITE_GLYPH;
26796 if (it->cmp_it.ch < 0)
26798 glyph->u.cmp.automatic = false;
26799 glyph->u.cmp.id = it->cmp_it.id;
26800 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26802 else
26804 glyph->u.cmp.automatic = true;
26805 glyph->u.cmp.id = it->cmp_it.id;
26806 glyph->slice.cmp.from = it->cmp_it.from;
26807 glyph->slice.cmp.to = it->cmp_it.to - 1;
26809 glyph->avoid_cursor_p = it->avoid_cursor_p;
26810 glyph->multibyte_p = it->multibyte_p;
26811 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26813 /* In R2L rows, the left and the right box edges need to be
26814 drawn in reverse direction. */
26815 glyph->right_box_line_p = it->start_of_box_run_p;
26816 glyph->left_box_line_p = it->end_of_box_run_p;
26818 else
26820 glyph->left_box_line_p = it->start_of_box_run_p;
26821 glyph->right_box_line_p = it->end_of_box_run_p;
26823 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26824 || it->phys_descent > it->descent);
26825 glyph->padding_p = false;
26826 glyph->glyph_not_available_p = false;
26827 glyph->face_id = it->face_id;
26828 glyph->font_type = FONT_TYPE_UNKNOWN;
26829 if (it->bidi_p)
26831 glyph->resolved_level = it->bidi_it.resolved_level;
26832 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26833 glyph->bidi_type = it->bidi_it.type;
26835 ++it->glyph_row->used[area];
26837 else
26838 IT_EXPAND_MATRIX_WIDTH (it, area);
26842 /* Change IT->ascent and IT->height according to the setting of
26843 IT->voffset. */
26845 static void
26846 take_vertical_position_into_account (struct it *it)
26848 if (it->voffset)
26850 if (it->voffset < 0)
26851 /* Increase the ascent so that we can display the text higher
26852 in the line. */
26853 it->ascent -= it->voffset;
26854 else
26855 /* Increase the descent so that we can display the text lower
26856 in the line. */
26857 it->descent += it->voffset;
26862 /* Produce glyphs/get display metrics for the image IT is loaded with.
26863 See the description of struct display_iterator in dispextern.h for
26864 an overview of struct display_iterator. */
26866 static void
26867 produce_image_glyph (struct it *it)
26869 struct image *img;
26870 struct face *face;
26871 int glyph_ascent, crop;
26872 struct glyph_slice slice;
26874 eassert (it->what == IT_IMAGE);
26876 face = FACE_FROM_ID (it->f, it->face_id);
26877 /* Make sure X resources of the face is loaded. */
26878 prepare_face_for_display (it->f, face);
26880 if (it->image_id < 0)
26882 /* Fringe bitmap. */
26883 it->ascent = it->phys_ascent = 0;
26884 it->descent = it->phys_descent = 0;
26885 it->pixel_width = 0;
26886 it->nglyphs = 0;
26887 return;
26890 img = IMAGE_FROM_ID (it->f, it->image_id);
26891 /* Make sure X resources of the image is loaded. */
26892 prepare_image_for_display (it->f, img);
26894 slice.x = slice.y = 0;
26895 slice.width = img->width;
26896 slice.height = img->height;
26898 if (INTEGERP (it->slice.x))
26899 slice.x = XINT (it->slice.x);
26900 else if (FLOATP (it->slice.x))
26901 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26903 if (INTEGERP (it->slice.y))
26904 slice.y = XINT (it->slice.y);
26905 else if (FLOATP (it->slice.y))
26906 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26908 if (INTEGERP (it->slice.width))
26909 slice.width = XINT (it->slice.width);
26910 else if (FLOATP (it->slice.width))
26911 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26913 if (INTEGERP (it->slice.height))
26914 slice.height = XINT (it->slice.height);
26915 else if (FLOATP (it->slice.height))
26916 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26918 if (slice.x >= img->width)
26919 slice.x = img->width;
26920 if (slice.y >= img->height)
26921 slice.y = img->height;
26922 if (slice.x + slice.width >= img->width)
26923 slice.width = img->width - slice.x;
26924 if (slice.y + slice.height > img->height)
26925 slice.height = img->height - slice.y;
26927 if (slice.width == 0 || slice.height == 0)
26928 return;
26930 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26932 it->descent = slice.height - glyph_ascent;
26933 if (slice.y == 0)
26934 it->descent += img->vmargin;
26935 if (slice.y + slice.height == img->height)
26936 it->descent += img->vmargin;
26937 it->phys_descent = it->descent;
26939 it->pixel_width = slice.width;
26940 if (slice.x == 0)
26941 it->pixel_width += img->hmargin;
26942 if (slice.x + slice.width == img->width)
26943 it->pixel_width += img->hmargin;
26945 /* It's quite possible for images to have an ascent greater than
26946 their height, so don't get confused in that case. */
26947 if (it->descent < 0)
26948 it->descent = 0;
26950 it->nglyphs = 1;
26952 if (face->box != FACE_NO_BOX)
26954 if (face->box_line_width > 0)
26956 if (slice.y == 0)
26957 it->ascent += face->box_line_width;
26958 if (slice.y + slice.height == img->height)
26959 it->descent += face->box_line_width;
26962 if (it->start_of_box_run_p && slice.x == 0)
26963 it->pixel_width += eabs (face->box_line_width);
26964 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26965 it->pixel_width += eabs (face->box_line_width);
26968 take_vertical_position_into_account (it);
26970 /* Automatically crop wide image glyphs at right edge so we can
26971 draw the cursor on same display row. */
26972 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26973 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26975 it->pixel_width -= crop;
26976 slice.width -= crop;
26979 if (it->glyph_row)
26981 struct glyph *glyph;
26982 enum glyph_row_area area = it->area;
26984 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26985 if (it->glyph_row->reversed_p)
26987 struct glyph *g;
26989 /* Make room for the new glyph. */
26990 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26991 g[1] = *g;
26992 glyph = it->glyph_row->glyphs[it->area];
26994 if (glyph < it->glyph_row->glyphs[area + 1])
26996 glyph->charpos = CHARPOS (it->position);
26997 glyph->object = it->object;
26998 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26999 glyph->ascent = glyph_ascent;
27000 glyph->descent = it->descent;
27001 glyph->voffset = it->voffset;
27002 glyph->type = IMAGE_GLYPH;
27003 glyph->avoid_cursor_p = it->avoid_cursor_p;
27004 glyph->multibyte_p = it->multibyte_p;
27005 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27007 /* In R2L rows, the left and the right box edges need to be
27008 drawn in reverse direction. */
27009 glyph->right_box_line_p = it->start_of_box_run_p;
27010 glyph->left_box_line_p = it->end_of_box_run_p;
27012 else
27014 glyph->left_box_line_p = it->start_of_box_run_p;
27015 glyph->right_box_line_p = it->end_of_box_run_p;
27017 glyph->overlaps_vertically_p = false;
27018 glyph->padding_p = false;
27019 glyph->glyph_not_available_p = false;
27020 glyph->face_id = it->face_id;
27021 glyph->u.img_id = img->id;
27022 glyph->slice.img = slice;
27023 glyph->font_type = FONT_TYPE_UNKNOWN;
27024 if (it->bidi_p)
27026 glyph->resolved_level = it->bidi_it.resolved_level;
27027 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27028 glyph->bidi_type = it->bidi_it.type;
27030 ++it->glyph_row->used[area];
27032 else
27033 IT_EXPAND_MATRIX_WIDTH (it, area);
27037 static void
27038 produce_xwidget_glyph (struct it *it)
27040 #ifdef HAVE_XWIDGETS
27041 struct xwidget *xw;
27042 int glyph_ascent, crop;
27043 eassert (it->what == IT_XWIDGET);
27045 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27046 /* Make sure X resources of the face is loaded. */
27047 prepare_face_for_display (it->f, face);
27049 xw = it->xwidget;
27050 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27051 it->descent = xw->height/2;
27052 it->phys_descent = it->descent;
27053 it->pixel_width = xw->width;
27054 /* It's quite possible for images to have an ascent greater than
27055 their height, so don't get confused in that case. */
27056 if (it->descent < 0)
27057 it->descent = 0;
27059 it->nglyphs = 1;
27061 if (face->box != FACE_NO_BOX)
27063 if (face->box_line_width > 0)
27065 it->ascent += face->box_line_width;
27066 it->descent += face->box_line_width;
27069 if (it->start_of_box_run_p)
27070 it->pixel_width += eabs (face->box_line_width);
27071 it->pixel_width += eabs (face->box_line_width);
27074 take_vertical_position_into_account (it);
27076 /* Automatically crop wide image glyphs at right edge so we can
27077 draw the cursor on same display row. */
27078 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27079 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27080 it->pixel_width -= crop;
27082 if (it->glyph_row)
27084 enum glyph_row_area area = it->area;
27085 struct glyph *glyph
27086 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27088 if (it->glyph_row->reversed_p)
27090 struct glyph *g;
27092 /* Make room for the new glyph. */
27093 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27094 g[1] = *g;
27095 glyph = it->glyph_row->glyphs[it->area];
27097 if (glyph < it->glyph_row->glyphs[area + 1])
27099 glyph->charpos = CHARPOS (it->position);
27100 glyph->object = it->object;
27101 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27102 glyph->ascent = glyph_ascent;
27103 glyph->descent = it->descent;
27104 glyph->voffset = it->voffset;
27105 glyph->type = XWIDGET_GLYPH;
27106 glyph->avoid_cursor_p = it->avoid_cursor_p;
27107 glyph->multibyte_p = it->multibyte_p;
27108 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27110 /* In R2L rows, the left and the right box edges need to be
27111 drawn in reverse direction. */
27112 glyph->right_box_line_p = it->start_of_box_run_p;
27113 glyph->left_box_line_p = it->end_of_box_run_p;
27115 else
27117 glyph->left_box_line_p = it->start_of_box_run_p;
27118 glyph->right_box_line_p = it->end_of_box_run_p;
27120 glyph->overlaps_vertically_p = 0;
27121 glyph->padding_p = 0;
27122 glyph->glyph_not_available_p = 0;
27123 glyph->face_id = it->face_id;
27124 glyph->u.xwidget = it->xwidget;
27125 glyph->font_type = FONT_TYPE_UNKNOWN;
27126 if (it->bidi_p)
27128 glyph->resolved_level = it->bidi_it.resolved_level;
27129 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27130 glyph->bidi_type = it->bidi_it.type;
27132 ++it->glyph_row->used[area];
27134 else
27135 IT_EXPAND_MATRIX_WIDTH (it, area);
27137 #endif
27140 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27141 of the glyph, WIDTH and HEIGHT are the width and height of the
27142 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27144 static void
27145 append_stretch_glyph (struct it *it, Lisp_Object object,
27146 int width, int height, int ascent)
27148 struct glyph *glyph;
27149 enum glyph_row_area area = it->area;
27151 eassert (ascent >= 0 && ascent <= height);
27153 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27154 if (glyph < it->glyph_row->glyphs[area + 1])
27156 /* If the glyph row is reversed, we need to prepend the glyph
27157 rather than append it. */
27158 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27160 struct glyph *g;
27162 /* Make room for the additional glyph. */
27163 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27164 g[1] = *g;
27165 glyph = it->glyph_row->glyphs[area];
27167 /* Decrease the width of the first glyph of the row that
27168 begins before first_visible_x (e.g., due to hscroll).
27169 This is so the overall width of the row becomes smaller
27170 by the scroll amount, and the stretch glyph appended by
27171 extend_face_to_end_of_line will be wider, to shift the
27172 row glyphs to the right. (In L2R rows, the corresponding
27173 left-shift effect is accomplished by setting row->x to a
27174 negative value, which won't work with R2L rows.)
27176 This must leave us with a positive value of WIDTH, since
27177 otherwise the call to move_it_in_display_line_to at the
27178 beginning of display_line would have got past the entire
27179 first glyph, and then it->current_x would have been
27180 greater or equal to it->first_visible_x. */
27181 if (it->current_x < it->first_visible_x)
27182 width -= it->first_visible_x - it->current_x;
27183 eassert (width > 0);
27185 glyph->charpos = CHARPOS (it->position);
27186 glyph->object = object;
27187 /* FIXME: It would be better to use TYPE_MAX here, but
27188 __typeof__ is not portable enough... */
27189 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27190 glyph->ascent = ascent;
27191 glyph->descent = height - ascent;
27192 glyph->voffset = it->voffset;
27193 glyph->type = STRETCH_GLYPH;
27194 glyph->avoid_cursor_p = it->avoid_cursor_p;
27195 glyph->multibyte_p = it->multibyte_p;
27196 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27198 /* In R2L rows, the left and the right box edges need to be
27199 drawn in reverse direction. */
27200 glyph->right_box_line_p = it->start_of_box_run_p;
27201 glyph->left_box_line_p = it->end_of_box_run_p;
27203 else
27205 glyph->left_box_line_p = it->start_of_box_run_p;
27206 glyph->right_box_line_p = it->end_of_box_run_p;
27208 glyph->overlaps_vertically_p = false;
27209 glyph->padding_p = false;
27210 glyph->glyph_not_available_p = false;
27211 glyph->face_id = it->face_id;
27212 glyph->u.stretch.ascent = ascent;
27213 glyph->u.stretch.height = height;
27214 glyph->slice.img = null_glyph_slice;
27215 glyph->font_type = FONT_TYPE_UNKNOWN;
27216 if (it->bidi_p)
27218 glyph->resolved_level = it->bidi_it.resolved_level;
27219 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27220 glyph->bidi_type = it->bidi_it.type;
27222 else
27224 glyph->resolved_level = 0;
27225 glyph->bidi_type = UNKNOWN_BT;
27227 ++it->glyph_row->used[area];
27229 else
27230 IT_EXPAND_MATRIX_WIDTH (it, area);
27233 #endif /* HAVE_WINDOW_SYSTEM */
27235 /* Produce a stretch glyph for iterator IT. IT->object is the value
27236 of the glyph property displayed. The value must be a list
27237 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27238 being recognized:
27240 1. `:width WIDTH' specifies that the space should be WIDTH *
27241 canonical char width wide. WIDTH may be an integer or floating
27242 point number.
27244 2. `:relative-width FACTOR' specifies that the width of the stretch
27245 should be computed from the width of the first character having the
27246 `glyph' property, and should be FACTOR times that width.
27248 3. `:align-to HPOS' specifies that the space should be wide enough
27249 to reach HPOS, a value in canonical character units.
27251 Exactly one of the above pairs must be present.
27253 4. `:height HEIGHT' specifies that the height of the stretch produced
27254 should be HEIGHT, measured in canonical character units.
27256 5. `:relative-height FACTOR' specifies that the height of the
27257 stretch should be FACTOR times the height of the characters having
27258 the glyph property.
27260 Either none or exactly one of 4 or 5 must be present.
27262 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27263 of the stretch should be used for the ascent of the stretch.
27264 ASCENT must be in the range 0 <= ASCENT <= 100. */
27266 void
27267 produce_stretch_glyph (struct it *it)
27269 /* (space :width WIDTH :height HEIGHT ...) */
27270 Lisp_Object prop, plist;
27271 int width = 0, height = 0, align_to = -1;
27272 bool zero_width_ok_p = false;
27273 double tem;
27274 struct font *font = NULL;
27276 #ifdef HAVE_WINDOW_SYSTEM
27277 int ascent = 0;
27278 bool zero_height_ok_p = false;
27280 if (FRAME_WINDOW_P (it->f))
27282 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27283 font = face->font ? face->font : FRAME_FONT (it->f);
27284 prepare_face_for_display (it->f, face);
27286 #endif
27288 /* List should start with `space'. */
27289 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27290 plist = XCDR (it->object);
27292 /* Compute the width of the stretch. */
27293 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27294 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27296 /* Absolute width `:width WIDTH' specified and valid. */
27297 zero_width_ok_p = true;
27298 width = (int)tem;
27300 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27302 /* Relative width `:relative-width FACTOR' specified and valid.
27303 Compute the width of the characters having the `glyph'
27304 property. */
27305 struct it it2;
27306 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27308 it2 = *it;
27309 if (it->multibyte_p)
27310 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27311 else
27313 it2.c = it2.char_to_display = *p, it2.len = 1;
27314 if (! ASCII_CHAR_P (it2.c))
27315 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27318 it2.glyph_row = NULL;
27319 it2.what = IT_CHARACTER;
27320 PRODUCE_GLYPHS (&it2);
27321 width = NUMVAL (prop) * it2.pixel_width;
27323 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27324 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27325 &align_to))
27327 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27328 align_to = (align_to < 0
27330 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27331 else if (align_to < 0)
27332 align_to = window_box_left_offset (it->w, TEXT_AREA);
27333 width = max (0, (int)tem + align_to - it->current_x);
27334 zero_width_ok_p = true;
27336 else
27337 /* Nothing specified -> width defaults to canonical char width. */
27338 width = FRAME_COLUMN_WIDTH (it->f);
27340 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27341 width = 1;
27343 #ifdef HAVE_WINDOW_SYSTEM
27344 /* Compute height. */
27345 if (FRAME_WINDOW_P (it->f))
27347 int default_height = normal_char_height (font, ' ');
27349 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27350 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27352 height = (int)tem;
27353 zero_height_ok_p = true;
27355 else if (prop = Fplist_get (plist, QCrelative_height),
27356 NUMVAL (prop) > 0)
27357 height = default_height * NUMVAL (prop);
27358 else
27359 height = default_height;
27361 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27362 height = 1;
27364 /* Compute percentage of height used for ascent. If
27365 `:ascent ASCENT' is present and valid, use that. Otherwise,
27366 derive the ascent from the font in use. */
27367 if (prop = Fplist_get (plist, QCascent),
27368 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27369 ascent = height * NUMVAL (prop) / 100.0;
27370 else if (!NILP (prop)
27371 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27372 ascent = min (max (0, (int)tem), height);
27373 else
27374 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27376 else
27377 #endif /* HAVE_WINDOW_SYSTEM */
27378 height = 1;
27380 if (width > 0 && it->line_wrap != TRUNCATE
27381 && it->current_x + width > it->last_visible_x)
27383 width = it->last_visible_x - it->current_x;
27384 #ifdef HAVE_WINDOW_SYSTEM
27385 /* Subtract one more pixel from the stretch width, but only on
27386 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27387 width -= FRAME_WINDOW_P (it->f);
27388 #endif
27391 if (width > 0 && height > 0 && it->glyph_row)
27393 Lisp_Object o_object = it->object;
27394 Lisp_Object object = it->stack[it->sp - 1].string;
27395 int n = width;
27397 if (!STRINGP (object))
27398 object = it->w->contents;
27399 #ifdef HAVE_WINDOW_SYSTEM
27400 if (FRAME_WINDOW_P (it->f))
27401 append_stretch_glyph (it, object, width, height, ascent);
27402 else
27403 #endif
27405 it->object = object;
27406 it->char_to_display = ' ';
27407 it->pixel_width = it->len = 1;
27408 while (n--)
27409 tty_append_glyph (it);
27410 it->object = o_object;
27414 it->pixel_width = width;
27415 #ifdef HAVE_WINDOW_SYSTEM
27416 if (FRAME_WINDOW_P (it->f))
27418 it->ascent = it->phys_ascent = ascent;
27419 it->descent = it->phys_descent = height - it->ascent;
27420 it->nglyphs = width > 0 && height > 0;
27421 take_vertical_position_into_account (it);
27423 else
27424 #endif
27425 it->nglyphs = width;
27428 /* Get information about special display element WHAT in an
27429 environment described by IT. WHAT is one of IT_TRUNCATION or
27430 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27431 non-null glyph_row member. This function ensures that fields like
27432 face_id, c, len of IT are left untouched. */
27434 static void
27435 produce_special_glyphs (struct it *it, enum display_element_type what)
27437 struct it temp_it;
27438 Lisp_Object gc;
27439 GLYPH glyph;
27441 temp_it = *it;
27442 temp_it.object = Qnil;
27443 memset (&temp_it.current, 0, sizeof temp_it.current);
27445 if (what == IT_CONTINUATION)
27447 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27448 if (it->bidi_it.paragraph_dir == R2L)
27449 SET_GLYPH_FROM_CHAR (glyph, '/');
27450 else
27451 SET_GLYPH_FROM_CHAR (glyph, '\\');
27452 if (it->dp
27453 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27455 /* FIXME: Should we mirror GC for R2L lines? */
27456 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27457 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27460 else if (what == IT_TRUNCATION)
27462 /* Truncation glyph. */
27463 SET_GLYPH_FROM_CHAR (glyph, '$');
27464 if (it->dp
27465 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27467 /* FIXME: Should we mirror GC for R2L lines? */
27468 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27469 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27472 else
27473 emacs_abort ();
27475 #ifdef HAVE_WINDOW_SYSTEM
27476 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27477 is turned off, we precede the truncation/continuation glyphs by a
27478 stretch glyph whose width is computed such that these special
27479 glyphs are aligned at the window margin, even when very different
27480 fonts are used in different glyph rows. */
27481 if (FRAME_WINDOW_P (temp_it.f)
27482 /* init_iterator calls this with it->glyph_row == NULL, and it
27483 wants only the pixel width of the truncation/continuation
27484 glyphs. */
27485 && temp_it.glyph_row
27486 /* insert_left_trunc_glyphs calls us at the beginning of the
27487 row, and it has its own calculation of the stretch glyph
27488 width. */
27489 && temp_it.glyph_row->used[TEXT_AREA] > 0
27490 && (temp_it.glyph_row->reversed_p
27491 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27492 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27494 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27496 if (stretch_width > 0)
27498 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27499 struct font *font =
27500 face->font ? face->font : FRAME_FONT (temp_it.f);
27501 int stretch_ascent =
27502 (((temp_it.ascent + temp_it.descent)
27503 * FONT_BASE (font)) / FONT_HEIGHT (font));
27505 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27506 temp_it.ascent + temp_it.descent,
27507 stretch_ascent);
27510 #endif
27512 temp_it.dp = NULL;
27513 temp_it.what = IT_CHARACTER;
27514 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27515 temp_it.face_id = GLYPH_FACE (glyph);
27516 temp_it.len = CHAR_BYTES (temp_it.c);
27518 PRODUCE_GLYPHS (&temp_it);
27519 it->pixel_width = temp_it.pixel_width;
27520 it->nglyphs = temp_it.nglyphs;
27523 #ifdef HAVE_WINDOW_SYSTEM
27525 /* Calculate line-height and line-spacing properties.
27526 An integer value specifies explicit pixel value.
27527 A float value specifies relative value to current face height.
27528 A cons (float . face-name) specifies relative value to
27529 height of specified face font.
27531 Returns height in pixels, or nil. */
27533 static Lisp_Object
27534 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27535 int boff, bool override)
27537 Lisp_Object face_name = Qnil;
27538 int ascent, descent, height;
27540 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27541 return val;
27543 if (CONSP (val))
27545 face_name = XCAR (val);
27546 val = XCDR (val);
27547 if (!NUMBERP (val))
27548 val = make_number (1);
27549 if (NILP (face_name))
27551 height = it->ascent + it->descent;
27552 goto scale;
27556 if (NILP (face_name))
27558 font = FRAME_FONT (it->f);
27559 boff = FRAME_BASELINE_OFFSET (it->f);
27561 else if (EQ (face_name, Qt))
27563 override = false;
27565 else
27567 int face_id;
27568 struct face *face;
27570 face_id = lookup_named_face (it->f, face_name, false);
27571 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27572 if (face == NULL || ((font = face->font) == NULL))
27573 return make_number (-1);
27574 boff = font->baseline_offset;
27575 if (font->vertical_centering)
27576 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27579 normal_char_ascent_descent (font, -1, &ascent, &descent);
27581 if (override)
27583 it->override_ascent = ascent;
27584 it->override_descent = descent;
27585 it->override_boff = boff;
27588 height = ascent + descent;
27590 scale:
27591 if (FLOATP (val))
27592 height = (int)(XFLOAT_DATA (val) * height);
27593 else if (INTEGERP (val))
27594 height *= XINT (val);
27596 return make_number (height);
27600 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27601 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27602 and only if this is for a character for which no font was found.
27604 If the display method (it->glyphless_method) is
27605 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27606 length of the acronym or the hexadecimal string, UPPER_XOFF and
27607 UPPER_YOFF are pixel offsets for the upper part of the string,
27608 LOWER_XOFF and LOWER_YOFF are for the lower part.
27610 For the other display methods, LEN through LOWER_YOFF are zero. */
27612 static void
27613 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27614 short upper_xoff, short upper_yoff,
27615 short lower_xoff, short lower_yoff)
27617 struct glyph *glyph;
27618 enum glyph_row_area area = it->area;
27620 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27621 if (glyph < it->glyph_row->glyphs[area + 1])
27623 /* If the glyph row is reversed, we need to prepend the glyph
27624 rather than append it. */
27625 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27627 struct glyph *g;
27629 /* Make room for the additional glyph. */
27630 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27631 g[1] = *g;
27632 glyph = it->glyph_row->glyphs[area];
27634 glyph->charpos = CHARPOS (it->position);
27635 glyph->object = it->object;
27636 eassert (it->pixel_width <= SHRT_MAX);
27637 glyph->pixel_width = it->pixel_width;
27638 glyph->ascent = it->ascent;
27639 glyph->descent = it->descent;
27640 glyph->voffset = it->voffset;
27641 glyph->type = GLYPHLESS_GLYPH;
27642 glyph->u.glyphless.method = it->glyphless_method;
27643 glyph->u.glyphless.for_no_font = for_no_font;
27644 glyph->u.glyphless.len = len;
27645 glyph->u.glyphless.ch = it->c;
27646 glyph->slice.glyphless.upper_xoff = upper_xoff;
27647 glyph->slice.glyphless.upper_yoff = upper_yoff;
27648 glyph->slice.glyphless.lower_xoff = lower_xoff;
27649 glyph->slice.glyphless.lower_yoff = lower_yoff;
27650 glyph->avoid_cursor_p = it->avoid_cursor_p;
27651 glyph->multibyte_p = it->multibyte_p;
27652 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27654 /* In R2L rows, the left and the right box edges need to be
27655 drawn in reverse direction. */
27656 glyph->right_box_line_p = it->start_of_box_run_p;
27657 glyph->left_box_line_p = it->end_of_box_run_p;
27659 else
27661 glyph->left_box_line_p = it->start_of_box_run_p;
27662 glyph->right_box_line_p = it->end_of_box_run_p;
27664 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27665 || it->phys_descent > it->descent);
27666 glyph->padding_p = false;
27667 glyph->glyph_not_available_p = false;
27668 glyph->face_id = face_id;
27669 glyph->font_type = FONT_TYPE_UNKNOWN;
27670 if (it->bidi_p)
27672 glyph->resolved_level = it->bidi_it.resolved_level;
27673 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27674 glyph->bidi_type = it->bidi_it.type;
27676 ++it->glyph_row->used[area];
27678 else
27679 IT_EXPAND_MATRIX_WIDTH (it, area);
27683 /* Produce a glyph for a glyphless character for iterator IT.
27684 IT->glyphless_method specifies which method to use for displaying
27685 the character. See the description of enum
27686 glyphless_display_method in dispextern.h for the detail.
27688 FOR_NO_FONT is true if and only if this is for a character for
27689 which no font was found. ACRONYM, if non-nil, is an acronym string
27690 for the character. */
27692 static void
27693 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27695 int face_id;
27696 struct face *face;
27697 struct font *font;
27698 int base_width, base_height, width, height;
27699 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27700 int len;
27702 /* Get the metrics of the base font. We always refer to the current
27703 ASCII face. */
27704 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27705 font = face->font ? face->font : FRAME_FONT (it->f);
27706 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27707 it->ascent += font->baseline_offset;
27708 it->descent -= font->baseline_offset;
27709 base_height = it->ascent + it->descent;
27710 base_width = font->average_width;
27712 face_id = merge_glyphless_glyph_face (it);
27714 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27716 it->pixel_width = THIN_SPACE_WIDTH;
27717 len = 0;
27718 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27720 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27722 width = CHARACTER_WIDTH (it->c);
27723 if (width == 0)
27724 width = 1;
27725 else if (width > 4)
27726 width = 4;
27727 it->pixel_width = base_width * width;
27728 len = 0;
27729 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27731 else
27733 char buf[7];
27734 const char *str;
27735 unsigned int code[6];
27736 int upper_len;
27737 int ascent, descent;
27738 struct font_metrics metrics_upper, metrics_lower;
27740 face = FACE_FROM_ID (it->f, face_id);
27741 font = face->font ? face->font : FRAME_FONT (it->f);
27742 prepare_face_for_display (it->f, face);
27744 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27746 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27747 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27748 if (CONSP (acronym))
27749 acronym = XCAR (acronym);
27750 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27752 else
27754 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27755 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27756 str = buf;
27758 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27759 code[len] = font->driver->encode_char (font, str[len]);
27760 upper_len = (len + 1) / 2;
27761 font->driver->text_extents (font, code, upper_len,
27762 &metrics_upper);
27763 font->driver->text_extents (font, code + upper_len, len - upper_len,
27764 &metrics_lower);
27768 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27769 width = max (metrics_upper.width, metrics_lower.width) + 4;
27770 upper_xoff = upper_yoff = 2; /* the typical case */
27771 if (base_width >= width)
27773 /* Align the upper to the left, the lower to the right. */
27774 it->pixel_width = base_width;
27775 lower_xoff = base_width - 2 - metrics_lower.width;
27777 else
27779 /* Center the shorter one. */
27780 it->pixel_width = width;
27781 if (metrics_upper.width >= metrics_lower.width)
27782 lower_xoff = (width - metrics_lower.width) / 2;
27783 else
27785 /* FIXME: This code doesn't look right. It formerly was
27786 missing the "lower_xoff = 0;", which couldn't have
27787 been right since it left lower_xoff uninitialized. */
27788 lower_xoff = 0;
27789 upper_xoff = (width - metrics_upper.width) / 2;
27793 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27794 top, bottom, and between upper and lower strings. */
27795 height = (metrics_upper.ascent + metrics_upper.descent
27796 + metrics_lower.ascent + metrics_lower.descent) + 5;
27797 /* Center vertically.
27798 H:base_height, D:base_descent
27799 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27801 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27802 descent = D - H/2 + h/2;
27803 lower_yoff = descent - 2 - ld;
27804 upper_yoff = lower_yoff - la - 1 - ud; */
27805 ascent = - (it->descent - (base_height + height + 1) / 2);
27806 descent = it->descent - (base_height - height) / 2;
27807 lower_yoff = descent - 2 - metrics_lower.descent;
27808 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27809 - metrics_upper.descent);
27810 /* Don't make the height shorter than the base height. */
27811 if (height > base_height)
27813 it->ascent = ascent;
27814 it->descent = descent;
27818 it->phys_ascent = it->ascent;
27819 it->phys_descent = it->descent;
27820 if (it->glyph_row)
27821 append_glyphless_glyph (it, face_id, for_no_font, len,
27822 upper_xoff, upper_yoff,
27823 lower_xoff, lower_yoff);
27824 it->nglyphs = 1;
27825 take_vertical_position_into_account (it);
27829 /* RIF:
27830 Produce glyphs/get display metrics for the display element IT is
27831 loaded with. See the description of struct it in dispextern.h
27832 for an overview of struct it. */
27834 void
27835 x_produce_glyphs (struct it *it)
27837 int extra_line_spacing = it->extra_line_spacing;
27839 it->glyph_not_available_p = false;
27841 if (it->what == IT_CHARACTER)
27843 XChar2b char2b;
27844 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27845 struct font *font = face->font;
27846 struct font_metrics *pcm = NULL;
27847 int boff; /* Baseline offset. */
27849 if (font == NULL)
27851 /* When no suitable font is found, display this character by
27852 the method specified in the first extra slot of
27853 Vglyphless_char_display. */
27854 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27856 eassert (it->what == IT_GLYPHLESS);
27857 produce_glyphless_glyph (it, true,
27858 STRINGP (acronym) ? acronym : Qnil);
27859 goto done;
27862 boff = font->baseline_offset;
27863 if (font->vertical_centering)
27864 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27866 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27868 it->nglyphs = 1;
27870 if (it->override_ascent >= 0)
27872 it->ascent = it->override_ascent;
27873 it->descent = it->override_descent;
27874 boff = it->override_boff;
27876 else
27878 it->ascent = FONT_BASE (font) + boff;
27879 it->descent = FONT_DESCENT (font) - boff;
27882 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27884 pcm = get_per_char_metric (font, &char2b);
27885 if (pcm->width == 0
27886 && pcm->rbearing == 0 && pcm->lbearing == 0)
27887 pcm = NULL;
27890 if (pcm)
27892 it->phys_ascent = pcm->ascent + boff;
27893 it->phys_descent = pcm->descent - boff;
27894 it->pixel_width = pcm->width;
27895 /* Don't use font-global values for ascent and descent
27896 if they result in an exceedingly large line height. */
27897 if (it->override_ascent < 0)
27899 if (FONT_TOO_HIGH (font))
27901 it->ascent = it->phys_ascent;
27902 it->descent = it->phys_descent;
27903 /* These limitations are enforced by an
27904 assertion near the end of this function. */
27905 if (it->ascent < 0)
27906 it->ascent = 0;
27907 if (it->descent < 0)
27908 it->descent = 0;
27912 else
27914 it->glyph_not_available_p = true;
27915 it->phys_ascent = it->ascent;
27916 it->phys_descent = it->descent;
27917 it->pixel_width = font->space_width;
27920 if (it->constrain_row_ascent_descent_p)
27922 if (it->descent > it->max_descent)
27924 it->ascent += it->descent - it->max_descent;
27925 it->descent = it->max_descent;
27927 if (it->ascent > it->max_ascent)
27929 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27930 it->ascent = it->max_ascent;
27932 it->phys_ascent = min (it->phys_ascent, it->ascent);
27933 it->phys_descent = min (it->phys_descent, it->descent);
27934 extra_line_spacing = 0;
27937 /* If this is a space inside a region of text with
27938 `space-width' property, change its width. */
27939 bool stretched_p
27940 = it->char_to_display == ' ' && !NILP (it->space_width);
27941 if (stretched_p)
27942 it->pixel_width *= XFLOATINT (it->space_width);
27944 /* If face has a box, add the box thickness to the character
27945 height. If character has a box line to the left and/or
27946 right, add the box line width to the character's width. */
27947 if (face->box != FACE_NO_BOX)
27949 int thick = face->box_line_width;
27951 if (thick > 0)
27953 it->ascent += thick;
27954 it->descent += thick;
27956 else
27957 thick = -thick;
27959 if (it->start_of_box_run_p)
27960 it->pixel_width += thick;
27961 if (it->end_of_box_run_p)
27962 it->pixel_width += thick;
27965 /* If face has an overline, add the height of the overline
27966 (1 pixel) and a 1 pixel margin to the character height. */
27967 if (face->overline_p)
27968 it->ascent += overline_margin;
27970 if (it->constrain_row_ascent_descent_p)
27972 if (it->ascent > it->max_ascent)
27973 it->ascent = it->max_ascent;
27974 if (it->descent > it->max_descent)
27975 it->descent = it->max_descent;
27978 take_vertical_position_into_account (it);
27980 /* If we have to actually produce glyphs, do it. */
27981 if (it->glyph_row)
27983 if (stretched_p)
27985 /* Translate a space with a `space-width' property
27986 into a stretch glyph. */
27987 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27988 / FONT_HEIGHT (font));
27989 append_stretch_glyph (it, it->object, it->pixel_width,
27990 it->ascent + it->descent, ascent);
27992 else
27993 append_glyph (it);
27995 /* If characters with lbearing or rbearing are displayed
27996 in this line, record that fact in a flag of the
27997 glyph row. This is used to optimize X output code. */
27998 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27999 it->glyph_row->contains_overlapping_glyphs_p = true;
28001 if (! stretched_p && it->pixel_width == 0)
28002 /* We assure that all visible glyphs have at least 1-pixel
28003 width. */
28004 it->pixel_width = 1;
28006 else if (it->char_to_display == '\n')
28008 /* A newline has no width, but we need the height of the
28009 line. But if previous part of the line sets a height,
28010 don't increase that height. */
28012 Lisp_Object height;
28013 Lisp_Object total_height = Qnil;
28015 it->override_ascent = -1;
28016 it->pixel_width = 0;
28017 it->nglyphs = 0;
28019 height = get_it_property (it, Qline_height);
28020 /* Split (line-height total-height) list. */
28021 if (CONSP (height)
28022 && CONSP (XCDR (height))
28023 && NILP (XCDR (XCDR (height))))
28025 total_height = XCAR (XCDR (height));
28026 height = XCAR (height);
28028 height = calc_line_height_property (it, height, font, boff, true);
28030 if (it->override_ascent >= 0)
28032 it->ascent = it->override_ascent;
28033 it->descent = it->override_descent;
28034 boff = it->override_boff;
28036 else
28038 if (FONT_TOO_HIGH (font))
28040 it->ascent = font->pixel_size + boff - 1;
28041 it->descent = -boff + 1;
28042 if (it->descent < 0)
28043 it->descent = 0;
28045 else
28047 it->ascent = FONT_BASE (font) + boff;
28048 it->descent = FONT_DESCENT (font) - boff;
28052 if (EQ (height, Qt))
28054 if (it->descent > it->max_descent)
28056 it->ascent += it->descent - it->max_descent;
28057 it->descent = it->max_descent;
28059 if (it->ascent > it->max_ascent)
28061 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28062 it->ascent = it->max_ascent;
28064 it->phys_ascent = min (it->phys_ascent, it->ascent);
28065 it->phys_descent = min (it->phys_descent, it->descent);
28066 it->constrain_row_ascent_descent_p = true;
28067 extra_line_spacing = 0;
28069 else
28071 Lisp_Object spacing;
28073 it->phys_ascent = it->ascent;
28074 it->phys_descent = it->descent;
28076 if ((it->max_ascent > 0 || it->max_descent > 0)
28077 && face->box != FACE_NO_BOX
28078 && face->box_line_width > 0)
28080 it->ascent += face->box_line_width;
28081 it->descent += face->box_line_width;
28083 if (!NILP (height)
28084 && XINT (height) > it->ascent + it->descent)
28085 it->ascent = XINT (height) - it->descent;
28087 if (!NILP (total_height))
28088 spacing = calc_line_height_property (it, total_height, font,
28089 boff, false);
28090 else
28092 spacing = get_it_property (it, Qline_spacing);
28093 spacing = calc_line_height_property (it, spacing, font,
28094 boff, false);
28096 if (INTEGERP (spacing))
28098 extra_line_spacing = XINT (spacing);
28099 if (!NILP (total_height))
28100 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28104 else /* i.e. (it->char_to_display == '\t') */
28106 if (font->space_width > 0)
28108 int tab_width = it->tab_width * font->space_width;
28109 int x = it->current_x + it->continuation_lines_width;
28110 int x0 = x;
28111 /* Adjust for line numbers, if needed. */
28112 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28113 x -= it->lnum_pixel_width;
28114 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28116 /* If the distance from the current position to the next tab
28117 stop is less than a space character width, use the
28118 tab stop after that. */
28119 if (next_tab_x - x < font->space_width)
28120 next_tab_x += tab_width;
28121 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28122 next_tab_x += (it->lnum_pixel_width
28123 - ((it->w->hscroll * font->space_width)
28124 % tab_width));
28126 it->pixel_width = next_tab_x - x0;
28127 it->nglyphs = 1;
28128 if (FONT_TOO_HIGH (font))
28130 if (get_char_glyph_code (' ', font, &char2b))
28132 pcm = get_per_char_metric (font, &char2b);
28133 if (pcm->width == 0
28134 && pcm->rbearing == 0 && pcm->lbearing == 0)
28135 pcm = NULL;
28138 if (pcm)
28140 it->ascent = pcm->ascent + boff;
28141 it->descent = pcm->descent - boff;
28143 else
28145 it->ascent = font->pixel_size + boff - 1;
28146 it->descent = -boff + 1;
28148 if (it->ascent < 0)
28149 it->ascent = 0;
28150 if (it->descent < 0)
28151 it->descent = 0;
28153 else
28155 it->ascent = FONT_BASE (font) + boff;
28156 it->descent = FONT_DESCENT (font) - boff;
28158 it->phys_ascent = it->ascent;
28159 it->phys_descent = it->descent;
28161 if (it->glyph_row)
28163 append_stretch_glyph (it, it->object, it->pixel_width,
28164 it->ascent + it->descent, it->ascent);
28167 else
28169 it->pixel_width = 0;
28170 it->nglyphs = 1;
28174 if (FONT_TOO_HIGH (font))
28176 int font_ascent, font_descent;
28178 /* For very large fonts, where we ignore the declared font
28179 dimensions, and go by per-character metrics instead,
28180 don't let the row ascent and descent values (and the row
28181 height computed from them) be smaller than the "normal"
28182 character metrics. This avoids unpleasant effects
28183 whereby lines on display would change their height
28184 depending on which characters are shown. */
28185 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28186 it->max_ascent = max (it->max_ascent, font_ascent);
28187 it->max_descent = max (it->max_descent, font_descent);
28190 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28192 /* A static composition.
28194 Note: A composition is represented as one glyph in the
28195 glyph matrix. There are no padding glyphs.
28197 Important note: pixel_width, ascent, and descent are the
28198 values of what is drawn by draw_glyphs (i.e. the values of
28199 the overall glyphs composed). */
28200 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28201 int boff; /* baseline offset */
28202 struct composition *cmp = composition_table[it->cmp_it.id];
28203 int glyph_len = cmp->glyph_len;
28204 struct font *font = face->font;
28206 it->nglyphs = 1;
28208 /* If we have not yet calculated pixel size data of glyphs of
28209 the composition for the current face font, calculate them
28210 now. Theoretically, we have to check all fonts for the
28211 glyphs, but that requires much time and memory space. So,
28212 here we check only the font of the first glyph. This may
28213 lead to incorrect display, but it's very rare, and C-l
28214 (recenter-top-bottom) can correct the display anyway. */
28215 if (! cmp->font || cmp->font != font)
28217 /* Ascent and descent of the font of the first character
28218 of this composition (adjusted by baseline offset).
28219 Ascent and descent of overall glyphs should not be less
28220 than these, respectively. */
28221 int font_ascent, font_descent, font_height;
28222 /* Bounding box of the overall glyphs. */
28223 int leftmost, rightmost, lowest, highest;
28224 int lbearing, rbearing;
28225 int i, width, ascent, descent;
28226 int c;
28227 XChar2b char2b;
28228 struct font_metrics *pcm;
28229 ptrdiff_t pos;
28231 eassume (0 < glyph_len); /* See Bug#8512. */
28233 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28234 while (c == '\t' && 0 < --glyph_len);
28236 bool right_padded = glyph_len < cmp->glyph_len;
28237 for (i = 0; i < glyph_len; i++)
28239 c = COMPOSITION_GLYPH (cmp, i);
28240 if (c != '\t')
28241 break;
28242 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28244 bool left_padded = i > 0;
28246 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28247 : IT_CHARPOS (*it));
28248 /* If no suitable font is found, use the default font. */
28249 bool font_not_found_p = font == NULL;
28250 if (font_not_found_p)
28252 face = face->ascii_face;
28253 font = face->font;
28255 boff = font->baseline_offset;
28256 if (font->vertical_centering)
28257 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28258 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28259 font_ascent += boff;
28260 font_descent -= boff;
28261 font_height = font_ascent + font_descent;
28263 cmp->font = font;
28265 pcm = NULL;
28266 if (! font_not_found_p)
28268 get_char_face_and_encoding (it->f, c, it->face_id,
28269 &char2b, false);
28270 pcm = get_per_char_metric (font, &char2b);
28273 /* Initialize the bounding box. */
28274 if (pcm)
28276 width = cmp->glyph_len > 0 ? pcm->width : 0;
28277 ascent = pcm->ascent;
28278 descent = pcm->descent;
28279 lbearing = pcm->lbearing;
28280 rbearing = pcm->rbearing;
28282 else
28284 width = cmp->glyph_len > 0 ? font->space_width : 0;
28285 ascent = FONT_BASE (font);
28286 descent = FONT_DESCENT (font);
28287 lbearing = 0;
28288 rbearing = width;
28291 rightmost = width;
28292 leftmost = 0;
28293 lowest = - descent + boff;
28294 highest = ascent + boff;
28296 if (! font_not_found_p
28297 && font->default_ascent
28298 && CHAR_TABLE_P (Vuse_default_ascent)
28299 && !NILP (Faref (Vuse_default_ascent,
28300 make_number (it->char_to_display))))
28301 highest = font->default_ascent + boff;
28303 /* Draw the first glyph at the normal position. It may be
28304 shifted to right later if some other glyphs are drawn
28305 at the left. */
28306 cmp->offsets[i * 2] = 0;
28307 cmp->offsets[i * 2 + 1] = boff;
28308 cmp->lbearing = lbearing;
28309 cmp->rbearing = rbearing;
28311 /* Set cmp->offsets for the remaining glyphs. */
28312 for (i++; i < glyph_len; i++)
28314 int left, right, btm, top;
28315 int ch = COMPOSITION_GLYPH (cmp, i);
28316 int face_id;
28317 struct face *this_face;
28319 if (ch == '\t')
28320 ch = ' ';
28321 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28322 this_face = FACE_FROM_ID (it->f, face_id);
28323 font = this_face->font;
28325 if (font == NULL)
28326 pcm = NULL;
28327 else
28329 get_char_face_and_encoding (it->f, ch, face_id,
28330 &char2b, false);
28331 pcm = get_per_char_metric (font, &char2b);
28333 if (! pcm)
28334 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28335 else
28337 width = pcm->width;
28338 ascent = pcm->ascent;
28339 descent = pcm->descent;
28340 lbearing = pcm->lbearing;
28341 rbearing = pcm->rbearing;
28342 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28344 /* Relative composition with or without
28345 alternate chars. */
28346 left = (leftmost + rightmost - width) / 2;
28347 btm = - descent + boff;
28348 if (font->relative_compose
28349 && (! CHAR_TABLE_P (Vignore_relative_composition)
28350 || NILP (Faref (Vignore_relative_composition,
28351 make_number (ch)))))
28354 if (- descent >= font->relative_compose)
28355 /* One extra pixel between two glyphs. */
28356 btm = highest + 1;
28357 else if (ascent <= 0)
28358 /* One extra pixel between two glyphs. */
28359 btm = lowest - 1 - ascent - descent;
28362 else
28364 /* A composition rule is specified by an integer
28365 value that encodes global and new reference
28366 points (GREF and NREF). GREF and NREF are
28367 specified by numbers as below:
28369 0---1---2 -- ascent
28373 9--10--11 -- center
28375 ---3---4---5--- baseline
28377 6---7---8 -- descent
28379 int rule = COMPOSITION_RULE (cmp, i);
28380 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28382 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28383 grefx = gref % 3, nrefx = nref % 3;
28384 grefy = gref / 3, nrefy = nref / 3;
28385 if (xoff)
28386 xoff = font_height * (xoff - 128) / 256;
28387 if (yoff)
28388 yoff = font_height * (yoff - 128) / 256;
28390 left = (leftmost
28391 + grefx * (rightmost - leftmost) / 2
28392 - nrefx * width / 2
28393 + xoff);
28395 btm = ((grefy == 0 ? highest
28396 : grefy == 1 ? 0
28397 : grefy == 2 ? lowest
28398 : (highest + lowest) / 2)
28399 - (nrefy == 0 ? ascent + descent
28400 : nrefy == 1 ? descent - boff
28401 : nrefy == 2 ? 0
28402 : (ascent + descent) / 2)
28403 + yoff);
28406 cmp->offsets[i * 2] = left;
28407 cmp->offsets[i * 2 + 1] = btm + descent;
28409 /* Update the bounding box of the overall glyphs. */
28410 if (width > 0)
28412 right = left + width;
28413 if (left < leftmost)
28414 leftmost = left;
28415 if (right > rightmost)
28416 rightmost = right;
28418 top = btm + descent + ascent;
28419 if (top > highest)
28420 highest = top;
28421 if (btm < lowest)
28422 lowest = btm;
28424 if (cmp->lbearing > left + lbearing)
28425 cmp->lbearing = left + lbearing;
28426 if (cmp->rbearing < left + rbearing)
28427 cmp->rbearing = left + rbearing;
28431 /* If there are glyphs whose x-offsets are negative,
28432 shift all glyphs to the right and make all x-offsets
28433 non-negative. */
28434 if (leftmost < 0)
28436 for (i = 0; i < cmp->glyph_len; i++)
28437 cmp->offsets[i * 2] -= leftmost;
28438 rightmost -= leftmost;
28439 cmp->lbearing -= leftmost;
28440 cmp->rbearing -= leftmost;
28443 if (left_padded && cmp->lbearing < 0)
28445 for (i = 0; i < cmp->glyph_len; i++)
28446 cmp->offsets[i * 2] -= cmp->lbearing;
28447 rightmost -= cmp->lbearing;
28448 cmp->rbearing -= cmp->lbearing;
28449 cmp->lbearing = 0;
28451 if (right_padded && rightmost < cmp->rbearing)
28453 rightmost = cmp->rbearing;
28456 cmp->pixel_width = rightmost;
28457 cmp->ascent = highest;
28458 cmp->descent = - lowest;
28459 if (cmp->ascent < font_ascent)
28460 cmp->ascent = font_ascent;
28461 if (cmp->descent < font_descent)
28462 cmp->descent = font_descent;
28465 if (it->glyph_row
28466 && (cmp->lbearing < 0
28467 || cmp->rbearing > cmp->pixel_width))
28468 it->glyph_row->contains_overlapping_glyphs_p = true;
28470 it->pixel_width = cmp->pixel_width;
28471 it->ascent = it->phys_ascent = cmp->ascent;
28472 it->descent = it->phys_descent = cmp->descent;
28473 if (face->box != FACE_NO_BOX)
28475 int thick = face->box_line_width;
28477 if (thick > 0)
28479 it->ascent += thick;
28480 it->descent += thick;
28482 else
28483 thick = - thick;
28485 if (it->start_of_box_run_p)
28486 it->pixel_width += thick;
28487 if (it->end_of_box_run_p)
28488 it->pixel_width += thick;
28491 /* If face has an overline, add the height of the overline
28492 (1 pixel) and a 1 pixel margin to the character height. */
28493 if (face->overline_p)
28494 it->ascent += overline_margin;
28496 take_vertical_position_into_account (it);
28497 if (it->ascent < 0)
28498 it->ascent = 0;
28499 if (it->descent < 0)
28500 it->descent = 0;
28502 if (it->glyph_row && cmp->glyph_len > 0)
28503 append_composite_glyph (it);
28505 else if (it->what == IT_COMPOSITION)
28507 /* A dynamic (automatic) composition. */
28508 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28509 Lisp_Object gstring;
28510 struct font_metrics metrics;
28512 it->nglyphs = 1;
28514 gstring = composition_gstring_from_id (it->cmp_it.id);
28515 it->pixel_width
28516 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28517 &metrics);
28518 if (it->glyph_row
28519 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28520 it->glyph_row->contains_overlapping_glyphs_p = true;
28521 it->ascent = it->phys_ascent = metrics.ascent;
28522 it->descent = it->phys_descent = metrics.descent;
28523 if (face->box != FACE_NO_BOX)
28525 int thick = face->box_line_width;
28527 if (thick > 0)
28529 it->ascent += thick;
28530 it->descent += thick;
28532 else
28533 thick = - thick;
28535 if (it->start_of_box_run_p)
28536 it->pixel_width += thick;
28537 if (it->end_of_box_run_p)
28538 it->pixel_width += thick;
28540 /* If face has an overline, add the height of the overline
28541 (1 pixel) and a 1 pixel margin to the character height. */
28542 if (face->overline_p)
28543 it->ascent += overline_margin;
28544 take_vertical_position_into_account (it);
28545 if (it->ascent < 0)
28546 it->ascent = 0;
28547 if (it->descent < 0)
28548 it->descent = 0;
28550 if (it->glyph_row)
28551 append_composite_glyph (it);
28553 else if (it->what == IT_GLYPHLESS)
28554 produce_glyphless_glyph (it, false, Qnil);
28555 else if (it->what == IT_IMAGE)
28556 produce_image_glyph (it);
28557 else if (it->what == IT_STRETCH)
28558 produce_stretch_glyph (it);
28559 else if (it->what == IT_XWIDGET)
28560 produce_xwidget_glyph (it);
28562 done:
28563 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28564 because this isn't true for images with `:ascent 100'. */
28565 eassert (it->ascent >= 0 && it->descent >= 0);
28566 if (it->area == TEXT_AREA)
28567 it->current_x += it->pixel_width;
28569 if (extra_line_spacing > 0)
28571 it->descent += extra_line_spacing;
28572 if (extra_line_spacing > it->max_extra_line_spacing)
28573 it->max_extra_line_spacing = extra_line_spacing;
28576 it->max_ascent = max (it->max_ascent, it->ascent);
28577 it->max_descent = max (it->max_descent, it->descent);
28578 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28579 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28582 /* EXPORT for RIF:
28583 Output LEN glyphs starting at START at the nominal cursor position.
28584 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28585 being updated, and UPDATED_AREA is the area of that row being updated. */
28587 void
28588 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28589 struct glyph *start, enum glyph_row_area updated_area, int len)
28591 int x, hpos, chpos = w->phys_cursor.hpos;
28593 eassert (updated_row);
28594 /* When the window is hscrolled, cursor hpos can legitimately be out
28595 of bounds, but we draw the cursor at the corresponding window
28596 margin in that case. */
28597 if (!updated_row->reversed_p && chpos < 0)
28598 chpos = 0;
28599 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28600 chpos = updated_row->used[TEXT_AREA] - 1;
28602 block_input ();
28604 /* Write glyphs. */
28606 hpos = start - updated_row->glyphs[updated_area];
28607 x = draw_glyphs (w, w->output_cursor.x,
28608 updated_row, updated_area,
28609 hpos, hpos + len,
28610 DRAW_NORMAL_TEXT, 0);
28612 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28613 if (updated_area == TEXT_AREA
28614 && w->phys_cursor_on_p
28615 && w->phys_cursor.vpos == w->output_cursor.vpos
28616 && chpos >= hpos
28617 && chpos < hpos + len)
28618 w->phys_cursor_on_p = false;
28620 unblock_input ();
28622 /* Advance the output cursor. */
28623 w->output_cursor.hpos += len;
28624 w->output_cursor.x = x;
28628 /* EXPORT for RIF:
28629 Insert LEN glyphs from START at the nominal cursor position. */
28631 void
28632 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28633 struct glyph *start, enum glyph_row_area updated_area, int len)
28635 struct frame *f;
28636 int line_height, shift_by_width, shifted_region_width;
28637 struct glyph_row *row;
28638 struct glyph *glyph;
28639 int frame_x, frame_y;
28640 ptrdiff_t hpos;
28642 eassert (updated_row);
28643 block_input ();
28644 f = XFRAME (WINDOW_FRAME (w));
28646 /* Get the height of the line we are in. */
28647 row = updated_row;
28648 line_height = row->height;
28650 /* Get the width of the glyphs to insert. */
28651 shift_by_width = 0;
28652 for (glyph = start; glyph < start + len; ++glyph)
28653 shift_by_width += glyph->pixel_width;
28655 /* Get the width of the region to shift right. */
28656 shifted_region_width = (window_box_width (w, updated_area)
28657 - w->output_cursor.x
28658 - shift_by_width);
28660 /* Shift right. */
28661 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28662 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28664 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28665 line_height, shift_by_width);
28667 /* Write the glyphs. */
28668 hpos = start - row->glyphs[updated_area];
28669 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28670 hpos, hpos + len,
28671 DRAW_NORMAL_TEXT, 0);
28673 /* Advance the output cursor. */
28674 w->output_cursor.hpos += len;
28675 w->output_cursor.x += shift_by_width;
28676 unblock_input ();
28680 /* EXPORT for RIF:
28681 Erase the current text line from the nominal cursor position
28682 (inclusive) to pixel column TO_X (exclusive). The idea is that
28683 everything from TO_X onward is already erased.
28685 TO_X is a pixel position relative to UPDATED_AREA of currently
28686 updated window W. TO_X == -1 means clear to the end of this area. */
28688 void
28689 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28690 enum glyph_row_area updated_area, int to_x)
28692 struct frame *f;
28693 int max_x, min_y, max_y;
28694 int from_x, from_y, to_y;
28696 eassert (updated_row);
28697 f = XFRAME (w->frame);
28699 if (updated_row->full_width_p)
28700 max_x = (WINDOW_PIXEL_WIDTH (w)
28701 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28702 else
28703 max_x = window_box_width (w, updated_area);
28704 max_y = window_text_bottom_y (w);
28706 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28707 of window. For TO_X > 0, truncate to end of drawing area. */
28708 if (to_x == 0)
28709 return;
28710 else if (to_x < 0)
28711 to_x = max_x;
28712 else
28713 to_x = min (to_x, max_x);
28715 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28717 /* Notice if the cursor will be cleared by this operation. */
28718 if (!updated_row->full_width_p)
28719 notice_overwritten_cursor (w, updated_area,
28720 w->output_cursor.x, -1,
28721 updated_row->y,
28722 MATRIX_ROW_BOTTOM_Y (updated_row));
28724 from_x = w->output_cursor.x;
28726 /* Translate to frame coordinates. */
28727 if (updated_row->full_width_p)
28729 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28730 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28732 else
28734 int area_left = window_box_left (w, updated_area);
28735 from_x += area_left;
28736 to_x += area_left;
28739 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28740 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28741 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28743 /* Prevent inadvertently clearing to end of the X window. */
28744 if (to_x > from_x && to_y > from_y)
28746 block_input ();
28747 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28748 to_x - from_x, to_y - from_y);
28749 unblock_input ();
28753 #endif /* HAVE_WINDOW_SYSTEM */
28757 /***********************************************************************
28758 Cursor types
28759 ***********************************************************************/
28761 /* Value is the internal representation of the specified cursor type
28762 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28763 of the bar cursor. */
28765 static enum text_cursor_kinds
28766 get_specified_cursor_type (Lisp_Object arg, int *width)
28768 enum text_cursor_kinds type;
28770 if (NILP (arg))
28771 return NO_CURSOR;
28773 if (EQ (arg, Qbox))
28774 return FILLED_BOX_CURSOR;
28776 if (EQ (arg, Qhollow))
28777 return HOLLOW_BOX_CURSOR;
28779 if (EQ (arg, Qbar))
28781 *width = 2;
28782 return BAR_CURSOR;
28785 if (CONSP (arg)
28786 && EQ (XCAR (arg), Qbar)
28787 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28789 *width = XINT (XCDR (arg));
28790 return BAR_CURSOR;
28793 if (EQ (arg, Qhbar))
28795 *width = 2;
28796 return HBAR_CURSOR;
28799 if (CONSP (arg)
28800 && EQ (XCAR (arg), Qhbar)
28801 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28803 *width = XINT (XCDR (arg));
28804 return HBAR_CURSOR;
28807 /* Treat anything unknown as "hollow box cursor".
28808 It was bad to signal an error; people have trouble fixing
28809 .Xdefaults with Emacs, when it has something bad in it. */
28810 type = HOLLOW_BOX_CURSOR;
28812 return type;
28815 /* Set the default cursor types for specified frame. */
28816 void
28817 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28819 int width = 1;
28820 Lisp_Object tem;
28822 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28823 FRAME_CURSOR_WIDTH (f) = width;
28825 /* By default, set up the blink-off state depending on the on-state. */
28827 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28828 if (!NILP (tem))
28830 FRAME_BLINK_OFF_CURSOR (f)
28831 = get_specified_cursor_type (XCDR (tem), &width);
28832 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28834 else
28835 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28837 /* Make sure the cursor gets redrawn. */
28838 f->cursor_type_changed = true;
28842 #ifdef HAVE_WINDOW_SYSTEM
28844 /* Return the cursor we want to be displayed in window W. Return
28845 width of bar/hbar cursor through WIDTH arg. Return with
28846 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28847 (i.e. if the `system caret' should track this cursor).
28849 In a mini-buffer window, we want the cursor only to appear if we
28850 are reading input from this window. For the selected window, we
28851 want the cursor type given by the frame parameter or buffer local
28852 setting of cursor-type. If explicitly marked off, draw no cursor.
28853 In all other cases, we want a hollow box cursor. */
28855 static enum text_cursor_kinds
28856 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28857 bool *active_cursor)
28859 struct frame *f = XFRAME (w->frame);
28860 struct buffer *b = XBUFFER (w->contents);
28861 int cursor_type = DEFAULT_CURSOR;
28862 Lisp_Object alt_cursor;
28863 bool non_selected = false;
28865 *active_cursor = true;
28867 /* Echo area */
28868 if (cursor_in_echo_area
28869 && FRAME_HAS_MINIBUF_P (f)
28870 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28872 if (w == XWINDOW (echo_area_window))
28874 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28876 *width = FRAME_CURSOR_WIDTH (f);
28877 return FRAME_DESIRED_CURSOR (f);
28879 else
28880 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28883 *active_cursor = false;
28884 non_selected = true;
28887 /* Detect a nonselected window or nonselected frame. */
28888 else if (w != XWINDOW (f->selected_window)
28889 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28891 *active_cursor = false;
28893 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28894 return NO_CURSOR;
28896 non_selected = true;
28899 /* Never display a cursor in a window in which cursor-type is nil. */
28900 if (NILP (BVAR (b, cursor_type)))
28901 return NO_CURSOR;
28903 /* Get the normal cursor type for this window. */
28904 if (EQ (BVAR (b, cursor_type), Qt))
28906 cursor_type = FRAME_DESIRED_CURSOR (f);
28907 *width = FRAME_CURSOR_WIDTH (f);
28909 else
28910 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28912 /* Use cursor-in-non-selected-windows instead
28913 for non-selected window or frame. */
28914 if (non_selected)
28916 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28917 if (!EQ (Qt, alt_cursor))
28918 return get_specified_cursor_type (alt_cursor, width);
28919 /* t means modify the normal cursor type. */
28920 if (cursor_type == FILLED_BOX_CURSOR)
28921 cursor_type = HOLLOW_BOX_CURSOR;
28922 else if (cursor_type == BAR_CURSOR && *width > 1)
28923 --*width;
28924 return cursor_type;
28927 /* Use normal cursor if not blinked off. */
28928 if (!w->cursor_off_p)
28930 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28931 return NO_CURSOR;
28932 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28934 if (cursor_type == FILLED_BOX_CURSOR)
28936 /* Using a block cursor on large images can be very annoying.
28937 So use a hollow cursor for "large" images.
28938 If image is not transparent (no mask), also use hollow cursor. */
28939 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28940 if (img != NULL && IMAGEP (img->spec))
28942 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28943 where N = size of default frame font size.
28944 This should cover most of the "tiny" icons people may use. */
28945 if (!img->mask
28946 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28947 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28948 cursor_type = HOLLOW_BOX_CURSOR;
28951 else if (cursor_type != NO_CURSOR)
28953 /* Display current only supports BOX and HOLLOW cursors for images.
28954 So for now, unconditionally use a HOLLOW cursor when cursor is
28955 not a solid box cursor. */
28956 cursor_type = HOLLOW_BOX_CURSOR;
28959 return cursor_type;
28962 /* Cursor is blinked off, so determine how to "toggle" it. */
28964 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28965 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
28966 return get_specified_cursor_type (XCDR (alt_cursor), width);
28968 /* Then see if frame has specified a specific blink off cursor type. */
28969 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28971 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28972 return FRAME_BLINK_OFF_CURSOR (f);
28975 #if false
28976 /* Some people liked having a permanently visible blinking cursor,
28977 while others had very strong opinions against it. So it was
28978 decided to remove it. KFS 2003-09-03 */
28980 /* Finally perform built-in cursor blinking:
28981 filled box <-> hollow box
28982 wide [h]bar <-> narrow [h]bar
28983 narrow [h]bar <-> no cursor
28984 other type <-> no cursor */
28986 if (cursor_type == FILLED_BOX_CURSOR)
28987 return HOLLOW_BOX_CURSOR;
28989 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28991 *width = 1;
28992 return cursor_type;
28994 #endif
28996 return NO_CURSOR;
29000 /* Notice when the text cursor of window W has been completely
29001 overwritten by a drawing operation that outputs glyphs in AREA
29002 starting at X0 and ending at X1 in the line starting at Y0 and
29003 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29004 the rest of the line after X0 has been written. Y coordinates
29005 are window-relative. */
29007 static void
29008 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29009 int x0, int x1, int y0, int y1)
29011 int cx0, cx1, cy0, cy1;
29012 struct glyph_row *row;
29014 if (!w->phys_cursor_on_p)
29015 return;
29016 if (area != TEXT_AREA)
29017 return;
29019 if (w->phys_cursor.vpos < 0
29020 || w->phys_cursor.vpos >= w->current_matrix->nrows
29021 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29022 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29023 return;
29025 if (row->cursor_in_fringe_p)
29027 row->cursor_in_fringe_p = false;
29028 draw_fringe_bitmap (w, row, row->reversed_p);
29029 w->phys_cursor_on_p = false;
29030 return;
29033 cx0 = w->phys_cursor.x;
29034 cx1 = cx0 + w->phys_cursor_width;
29035 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29036 return;
29038 /* The cursor image will be completely removed from the
29039 screen if the output area intersects the cursor area in
29040 y-direction. When we draw in [y0 y1[, and some part of
29041 the cursor is at y < y0, that part must have been drawn
29042 before. When scrolling, the cursor is erased before
29043 actually scrolling, so we don't come here. When not
29044 scrolling, the rows above the old cursor row must have
29045 changed, and in this case these rows must have written
29046 over the cursor image.
29048 Likewise if part of the cursor is below y1, with the
29049 exception of the cursor being in the first blank row at
29050 the buffer and window end because update_text_area
29051 doesn't draw that row. (Except when it does, but
29052 that's handled in update_text_area.) */
29054 cy0 = w->phys_cursor.y;
29055 cy1 = cy0 + w->phys_cursor_height;
29056 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29057 return;
29059 w->phys_cursor_on_p = false;
29062 #endif /* HAVE_WINDOW_SYSTEM */
29065 /************************************************************************
29066 Mouse Face
29067 ************************************************************************/
29069 #ifdef HAVE_WINDOW_SYSTEM
29071 /* EXPORT for RIF:
29072 Fix the display of area AREA of overlapping row ROW in window W
29073 with respect to the overlapping part OVERLAPS. */
29075 void
29076 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29077 enum glyph_row_area area, int overlaps)
29079 int i, x;
29081 block_input ();
29083 x = 0;
29084 for (i = 0; i < row->used[area];)
29086 if (row->glyphs[area][i].overlaps_vertically_p)
29088 int start = i, start_x = x;
29092 x += row->glyphs[area][i].pixel_width;
29093 ++i;
29095 while (i < row->used[area]
29096 && row->glyphs[area][i].overlaps_vertically_p);
29098 draw_glyphs (w, start_x, row, area,
29099 start, i,
29100 DRAW_NORMAL_TEXT, overlaps);
29102 else
29104 x += row->glyphs[area][i].pixel_width;
29105 ++i;
29109 unblock_input ();
29113 /* EXPORT:
29114 Draw the cursor glyph of window W in glyph row ROW. See the
29115 comment of draw_glyphs for the meaning of HL. */
29117 void
29118 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29119 enum draw_glyphs_face hl)
29121 /* If cursor hpos is out of bounds, don't draw garbage. This can
29122 happen in mini-buffer windows when switching between echo area
29123 glyphs and mini-buffer. */
29124 if ((row->reversed_p
29125 ? (w->phys_cursor.hpos >= 0)
29126 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29128 bool on_p = w->phys_cursor_on_p;
29129 int x1;
29130 int hpos = w->phys_cursor.hpos;
29132 /* When the window is hscrolled, cursor hpos can legitimately be
29133 out of bounds, but we draw the cursor at the corresponding
29134 window margin in that case. */
29135 if (!row->reversed_p && hpos < 0)
29136 hpos = 0;
29137 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29138 hpos = row->used[TEXT_AREA] - 1;
29140 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29141 hl, 0);
29142 w->phys_cursor_on_p = on_p;
29144 if (hl == DRAW_CURSOR)
29145 w->phys_cursor_width = x1 - w->phys_cursor.x;
29146 /* When we erase the cursor, and ROW is overlapped by other
29147 rows, make sure that these overlapping parts of other rows
29148 are redrawn. */
29149 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29151 w->phys_cursor_width = x1 - w->phys_cursor.x;
29153 if (row > w->current_matrix->rows
29154 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29155 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29156 OVERLAPS_ERASED_CURSOR);
29158 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29159 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29160 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29161 OVERLAPS_ERASED_CURSOR);
29167 /* Erase the image of a cursor of window W from the screen. */
29169 void
29170 erase_phys_cursor (struct window *w)
29172 struct frame *f = XFRAME (w->frame);
29173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29174 int hpos = w->phys_cursor.hpos;
29175 int vpos = w->phys_cursor.vpos;
29176 bool mouse_face_here_p = false;
29177 struct glyph_matrix *active_glyphs = w->current_matrix;
29178 struct glyph_row *cursor_row;
29179 struct glyph *cursor_glyph;
29180 enum draw_glyphs_face hl;
29182 /* No cursor displayed or row invalidated => nothing to do on the
29183 screen. */
29184 if (w->phys_cursor_type == NO_CURSOR)
29185 goto mark_cursor_off;
29187 /* VPOS >= active_glyphs->nrows means that window has been resized.
29188 Don't bother to erase the cursor. */
29189 if (vpos >= active_glyphs->nrows)
29190 goto mark_cursor_off;
29192 /* If row containing cursor is marked invalid, there is nothing we
29193 can do. */
29194 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29195 if (!cursor_row->enabled_p)
29196 goto mark_cursor_off;
29198 /* If line spacing is > 0, old cursor may only be partially visible in
29199 window after split-window. So adjust visible height. */
29200 cursor_row->visible_height = min (cursor_row->visible_height,
29201 window_text_bottom_y (w) - cursor_row->y);
29203 /* If row is completely invisible, don't attempt to delete a cursor which
29204 isn't there. This can happen if cursor is at top of a window, and
29205 we switch to a buffer with a header line in that window. */
29206 if (cursor_row->visible_height <= 0)
29207 goto mark_cursor_off;
29209 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29210 if (cursor_row->cursor_in_fringe_p)
29212 cursor_row->cursor_in_fringe_p = false;
29213 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29214 goto mark_cursor_off;
29217 /* This can happen when the new row is shorter than the old one.
29218 In this case, either draw_glyphs or clear_end_of_line
29219 should have cleared the cursor. Note that we wouldn't be
29220 able to erase the cursor in this case because we don't have a
29221 cursor glyph at hand. */
29222 if ((cursor_row->reversed_p
29223 ? (w->phys_cursor.hpos < 0)
29224 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29225 goto mark_cursor_off;
29227 /* When the window is hscrolled, cursor hpos can legitimately be out
29228 of bounds, but we draw the cursor at the corresponding window
29229 margin in that case. */
29230 if (!cursor_row->reversed_p && hpos < 0)
29231 hpos = 0;
29232 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29233 hpos = cursor_row->used[TEXT_AREA] - 1;
29235 /* If the cursor is in the mouse face area, redisplay that when
29236 we clear the cursor. */
29237 if (! NILP (hlinfo->mouse_face_window)
29238 && coords_in_mouse_face_p (w, hpos, vpos)
29239 /* Don't redraw the cursor's spot in mouse face if it is at the
29240 end of a line (on a newline). The cursor appears there, but
29241 mouse highlighting does not. */
29242 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29243 mouse_face_here_p = true;
29245 /* Maybe clear the display under the cursor. */
29246 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29248 int x, y;
29249 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29250 int width;
29252 cursor_glyph = get_phys_cursor_glyph (w);
29253 if (cursor_glyph == NULL)
29254 goto mark_cursor_off;
29256 width = cursor_glyph->pixel_width;
29257 x = w->phys_cursor.x;
29258 if (x < 0)
29260 width += x;
29261 x = 0;
29263 width = min (width, window_box_width (w, TEXT_AREA) - x);
29264 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29265 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29267 if (width > 0)
29268 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29271 /* Erase the cursor by redrawing the character underneath it. */
29272 if (mouse_face_here_p)
29273 hl = DRAW_MOUSE_FACE;
29274 else
29275 hl = DRAW_NORMAL_TEXT;
29276 draw_phys_cursor_glyph (w, cursor_row, hl);
29278 mark_cursor_off:
29279 w->phys_cursor_on_p = false;
29280 w->phys_cursor_type = NO_CURSOR;
29284 /* Display or clear cursor of window W. If !ON, clear the cursor.
29285 If ON, display the cursor; where to put the cursor is specified by
29286 HPOS, VPOS, X and Y. */
29288 void
29289 display_and_set_cursor (struct window *w, bool on,
29290 int hpos, int vpos, int x, int y)
29292 struct frame *f = XFRAME (w->frame);
29293 int new_cursor_type;
29294 int new_cursor_width;
29295 bool active_cursor;
29296 struct glyph_row *glyph_row;
29297 struct glyph *glyph;
29299 /* This is pointless on invisible frames, and dangerous on garbaged
29300 windows and frames; in the latter case, the frame or window may
29301 be in the midst of changing its size, and x and y may be off the
29302 window. */
29303 if (! FRAME_VISIBLE_P (f)
29304 || vpos >= w->current_matrix->nrows
29305 || hpos >= w->current_matrix->matrix_w)
29306 return;
29308 /* If cursor is off and we want it off, return quickly. */
29309 if (!on && !w->phys_cursor_on_p)
29310 return;
29312 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29313 /* If cursor row is not enabled, we don't really know where to
29314 display the cursor. */
29315 if (!glyph_row->enabled_p)
29317 w->phys_cursor_on_p = false;
29318 return;
29321 /* A frame might be marked garbaged even though its cursor position
29322 is correct, and will not change upon subsequent redisplay. This
29323 happens in some rare situations, like toggling the sort order in
29324 Dired windows. We've already established that VPOS is valid, so
29325 it shouldn't do any harm to record the cursor position, as we are
29326 going to return without acting on it anyway. Otherwise, expose
29327 events might come in and call update_window_cursor, which will
29328 blindly use outdated values in w->phys_cursor. */
29329 if (FRAME_GARBAGED_P (f))
29331 if (on)
29333 w->phys_cursor.x = x;
29334 w->phys_cursor.y = glyph_row->y;
29335 w->phys_cursor.hpos = hpos;
29336 w->phys_cursor.vpos = vpos;
29338 return;
29341 glyph = NULL;
29342 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29343 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29345 eassert (input_blocked_p ());
29347 /* Set new_cursor_type to the cursor we want to be displayed. */
29348 new_cursor_type = get_window_cursor_type (w, glyph,
29349 &new_cursor_width, &active_cursor);
29351 /* If cursor is currently being shown and we don't want it to be or
29352 it is in the wrong place, or the cursor type is not what we want,
29353 erase it. */
29354 if (w->phys_cursor_on_p
29355 && (!on
29356 || w->phys_cursor.x != x
29357 || w->phys_cursor.y != y
29358 /* HPOS can be negative in R2L rows whose
29359 exact_window_width_line_p flag is set (i.e. their newline
29360 would "overflow into the fringe"). */
29361 || hpos < 0
29362 || new_cursor_type != w->phys_cursor_type
29363 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29364 && new_cursor_width != w->phys_cursor_width)))
29365 erase_phys_cursor (w);
29367 /* Don't check phys_cursor_on_p here because that flag is only set
29368 to false in some cases where we know that the cursor has been
29369 completely erased, to avoid the extra work of erasing the cursor
29370 twice. In other words, phys_cursor_on_p can be true and the cursor
29371 still not be visible, or it has only been partly erased. */
29372 if (on)
29374 w->phys_cursor_ascent = glyph_row->ascent;
29375 w->phys_cursor_height = glyph_row->height;
29377 /* Set phys_cursor_.* before x_draw_.* is called because some
29378 of them may need the information. */
29379 w->phys_cursor.x = x;
29380 w->phys_cursor.y = glyph_row->y;
29381 w->phys_cursor.hpos = hpos;
29382 w->phys_cursor.vpos = vpos;
29385 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29386 new_cursor_type, new_cursor_width,
29387 on, active_cursor);
29391 /* Switch the display of W's cursor on or off, according to the value
29392 of ON. */
29394 static void
29395 update_window_cursor (struct window *w, bool on)
29397 /* Don't update cursor in windows whose frame is in the process
29398 of being deleted. */
29399 if (w->current_matrix)
29401 int hpos = w->phys_cursor.hpos;
29402 int vpos = w->phys_cursor.vpos;
29403 struct glyph_row *row;
29405 if (vpos >= w->current_matrix->nrows
29406 || hpos >= w->current_matrix->matrix_w)
29407 return;
29409 row = MATRIX_ROW (w->current_matrix, vpos);
29411 /* When the window is hscrolled, cursor hpos can legitimately be
29412 out of bounds, but we draw the cursor at the corresponding
29413 window margin in that case. */
29414 if (!row->reversed_p && hpos < 0)
29415 hpos = 0;
29416 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29417 hpos = row->used[TEXT_AREA] - 1;
29419 block_input ();
29420 display_and_set_cursor (w, on, hpos, vpos,
29421 w->phys_cursor.x, w->phys_cursor.y);
29422 unblock_input ();
29427 /* Call update_window_cursor with parameter ON_P on all leaf windows
29428 in the window tree rooted at W. */
29430 static void
29431 update_cursor_in_window_tree (struct window *w, bool on_p)
29433 while (w)
29435 if (WINDOWP (w->contents))
29436 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29437 else
29438 update_window_cursor (w, on_p);
29440 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29445 /* EXPORT:
29446 Display the cursor on window W, or clear it, according to ON_P.
29447 Don't change the cursor's position. */
29449 void
29450 x_update_cursor (struct frame *f, bool on_p)
29452 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29456 /* EXPORT:
29457 Clear the cursor of window W to background color, and mark the
29458 cursor as not shown. This is used when the text where the cursor
29459 is about to be rewritten. */
29461 void
29462 x_clear_cursor (struct window *w)
29464 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29465 update_window_cursor (w, false);
29468 #endif /* HAVE_WINDOW_SYSTEM */
29470 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29471 and MSDOS. */
29472 static void
29473 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29474 int start_hpos, int end_hpos,
29475 enum draw_glyphs_face draw)
29477 #ifdef HAVE_WINDOW_SYSTEM
29478 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29480 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29481 return;
29483 #endif
29484 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29485 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29486 #endif
29489 /* Display the active region described by mouse_face_* according to DRAW. */
29491 static void
29492 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29494 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29495 struct frame *f = XFRAME (WINDOW_FRAME (w));
29497 if (/* If window is in the process of being destroyed, don't bother
29498 to do anything. */
29499 w->current_matrix != NULL
29500 /* Don't update mouse highlight if hidden. */
29501 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29502 /* Recognize when we are called to operate on rows that don't exist
29503 anymore. This can happen when a window is split. */
29504 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29506 bool phys_cursor_on_p = w->phys_cursor_on_p;
29507 struct glyph_row *row, *first, *last;
29509 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29510 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29512 for (row = first; row <= last && row->enabled_p; ++row)
29514 int start_hpos, end_hpos, start_x;
29516 /* For all but the first row, the highlight starts at column 0. */
29517 if (row == first)
29519 /* R2L rows have BEG and END in reversed order, but the
29520 screen drawing geometry is always left to right. So
29521 we need to mirror the beginning and end of the
29522 highlighted area in R2L rows. */
29523 if (!row->reversed_p)
29525 start_hpos = hlinfo->mouse_face_beg_col;
29526 start_x = hlinfo->mouse_face_beg_x;
29528 else if (row == last)
29530 start_hpos = hlinfo->mouse_face_end_col;
29531 start_x = hlinfo->mouse_face_end_x;
29533 else
29535 start_hpos = 0;
29536 start_x = 0;
29539 else if (row->reversed_p && row == last)
29541 start_hpos = hlinfo->mouse_face_end_col;
29542 start_x = hlinfo->mouse_face_end_x;
29544 else
29546 start_hpos = 0;
29547 start_x = 0;
29550 if (row == last)
29552 if (!row->reversed_p)
29553 end_hpos = hlinfo->mouse_face_end_col;
29554 else if (row == first)
29555 end_hpos = hlinfo->mouse_face_beg_col;
29556 else
29558 end_hpos = row->used[TEXT_AREA];
29559 if (draw == DRAW_NORMAL_TEXT)
29560 row->fill_line_p = true; /* Clear to end of line. */
29563 else if (row->reversed_p && row == first)
29564 end_hpos = hlinfo->mouse_face_beg_col;
29565 else
29567 end_hpos = row->used[TEXT_AREA];
29568 if (draw == DRAW_NORMAL_TEXT)
29569 row->fill_line_p = true; /* Clear to end of line. */
29572 if (end_hpos > start_hpos)
29574 draw_row_with_mouse_face (w, start_x, row,
29575 start_hpos, end_hpos, draw);
29577 row->mouse_face_p
29578 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29582 /* When we've written over the cursor, arrange for it to
29583 be displayed again. */
29584 if (FRAME_WINDOW_P (f)
29585 && phys_cursor_on_p && !w->phys_cursor_on_p)
29587 #ifdef HAVE_WINDOW_SYSTEM
29588 int hpos = w->phys_cursor.hpos;
29590 /* When the window is hscrolled, cursor hpos can legitimately be
29591 out of bounds, but we draw the cursor at the corresponding
29592 window margin in that case. */
29593 if (!row->reversed_p && hpos < 0)
29594 hpos = 0;
29595 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29596 hpos = row->used[TEXT_AREA] - 1;
29598 block_input ();
29599 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29600 w->phys_cursor.x, w->phys_cursor.y);
29601 unblock_input ();
29602 #endif /* HAVE_WINDOW_SYSTEM */
29606 #ifdef HAVE_WINDOW_SYSTEM
29607 /* Change the mouse cursor. */
29608 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29610 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29611 if (draw == DRAW_NORMAL_TEXT
29612 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29613 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29614 else
29615 #endif
29616 if (draw == DRAW_MOUSE_FACE)
29617 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29618 else
29619 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29621 #endif /* HAVE_WINDOW_SYSTEM */
29624 /* EXPORT:
29625 Clear out the mouse-highlighted active region.
29626 Redraw it un-highlighted first. Value is true if mouse
29627 face was actually drawn unhighlighted. */
29629 bool
29630 clear_mouse_face (Mouse_HLInfo *hlinfo)
29632 bool cleared
29633 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29634 if (cleared)
29635 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29636 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29637 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29638 hlinfo->mouse_face_window = Qnil;
29639 hlinfo->mouse_face_overlay = Qnil;
29640 return cleared;
29643 /* Return true if the coordinates HPOS and VPOS on windows W are
29644 within the mouse face on that window. */
29645 static bool
29646 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29648 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29650 /* Quickly resolve the easy cases. */
29651 if (!(WINDOWP (hlinfo->mouse_face_window)
29652 && XWINDOW (hlinfo->mouse_face_window) == w))
29653 return false;
29654 if (vpos < hlinfo->mouse_face_beg_row
29655 || vpos > hlinfo->mouse_face_end_row)
29656 return false;
29657 if (vpos > hlinfo->mouse_face_beg_row
29658 && vpos < hlinfo->mouse_face_end_row)
29659 return true;
29661 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29663 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29665 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29666 return true;
29668 else if ((vpos == hlinfo->mouse_face_beg_row
29669 && hpos >= hlinfo->mouse_face_beg_col)
29670 || (vpos == hlinfo->mouse_face_end_row
29671 && hpos < hlinfo->mouse_face_end_col))
29672 return true;
29674 else
29676 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29678 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29679 return true;
29681 else if ((vpos == hlinfo->mouse_face_beg_row
29682 && hpos <= hlinfo->mouse_face_beg_col)
29683 || (vpos == hlinfo->mouse_face_end_row
29684 && hpos > hlinfo->mouse_face_end_col))
29685 return true;
29687 return false;
29691 /* EXPORT:
29692 True if physical cursor of window W is within mouse face. */
29694 bool
29695 cursor_in_mouse_face_p (struct window *w)
29697 int hpos = w->phys_cursor.hpos;
29698 int vpos = w->phys_cursor.vpos;
29699 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29701 /* When the window is hscrolled, cursor hpos can legitimately be out
29702 of bounds, but we draw the cursor at the corresponding window
29703 margin in that case. */
29704 if (!row->reversed_p && hpos < 0)
29705 hpos = 0;
29706 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29707 hpos = row->used[TEXT_AREA] - 1;
29709 return coords_in_mouse_face_p (w, hpos, vpos);
29714 /* Find the glyph rows START_ROW and END_ROW of window W that display
29715 characters between buffer positions START_CHARPOS and END_CHARPOS
29716 (excluding END_CHARPOS). DISP_STRING is a display string that
29717 covers these buffer positions. This is similar to
29718 row_containing_pos, but is more accurate when bidi reordering makes
29719 buffer positions change non-linearly with glyph rows. */
29720 static void
29721 rows_from_pos_range (struct window *w,
29722 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29723 Lisp_Object disp_string,
29724 struct glyph_row **start, struct glyph_row **end)
29726 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29727 int last_y = window_text_bottom_y (w);
29728 struct glyph_row *row;
29730 *start = NULL;
29731 *end = NULL;
29733 while (!first->enabled_p
29734 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29735 first++;
29737 /* Find the START row. */
29738 for (row = first;
29739 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29740 row++)
29742 /* A row can potentially be the START row if the range of the
29743 characters it displays intersects the range
29744 [START_CHARPOS..END_CHARPOS). */
29745 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29746 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29747 /* See the commentary in row_containing_pos, for the
29748 explanation of the complicated way to check whether
29749 some position is beyond the end of the characters
29750 displayed by a row. */
29751 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29752 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29753 && !row->ends_at_zv_p
29754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29755 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29756 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29757 && !row->ends_at_zv_p
29758 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29760 /* Found a candidate row. Now make sure at least one of the
29761 glyphs it displays has a charpos from the range
29762 [START_CHARPOS..END_CHARPOS).
29764 This is not obvious because bidi reordering could make
29765 buffer positions of a row be 1,2,3,102,101,100, and if we
29766 want to highlight characters in [50..60), we don't want
29767 this row, even though [50..60) does intersect [1..103),
29768 the range of character positions given by the row's start
29769 and end positions. */
29770 struct glyph *g = row->glyphs[TEXT_AREA];
29771 struct glyph *e = g + row->used[TEXT_AREA];
29773 while (g < e)
29775 if (((BUFFERP (g->object) || NILP (g->object))
29776 && start_charpos <= g->charpos && g->charpos < end_charpos)
29777 /* A glyph that comes from DISP_STRING is by
29778 definition to be highlighted. */
29779 || EQ (g->object, disp_string))
29780 *start = row;
29781 g++;
29783 if (*start)
29784 break;
29788 /* Find the END row. */
29789 if (!*start
29790 /* If the last row is partially visible, start looking for END
29791 from that row, instead of starting from FIRST. */
29792 && !(row->enabled_p
29793 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29794 row = first;
29795 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29797 struct glyph_row *next = row + 1;
29798 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29800 if (!next->enabled_p
29801 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29802 /* The first row >= START whose range of displayed characters
29803 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29804 is the row END + 1. */
29805 || (start_charpos < next_start
29806 && end_charpos < next_start)
29807 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29808 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29809 && !next->ends_at_zv_p
29810 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29811 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29812 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29813 && !next->ends_at_zv_p
29814 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29816 *end = row;
29817 break;
29819 else
29821 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29822 but none of the characters it displays are in the range, it is
29823 also END + 1. */
29824 struct glyph *g = next->glyphs[TEXT_AREA];
29825 struct glyph *s = g;
29826 struct glyph *e = g + next->used[TEXT_AREA];
29828 while (g < e)
29830 if (((BUFFERP (g->object) || NILP (g->object))
29831 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29832 /* If the buffer position of the first glyph in
29833 the row is equal to END_CHARPOS, it means
29834 the last character to be highlighted is the
29835 newline of ROW, and we must consider NEXT as
29836 END, not END+1. */
29837 || (((!next->reversed_p && g == s)
29838 || (next->reversed_p && g == e - 1))
29839 && (g->charpos == end_charpos
29840 /* Special case for when NEXT is an
29841 empty line at ZV. */
29842 || (g->charpos == -1
29843 && !row->ends_at_zv_p
29844 && next_start == end_charpos)))))
29845 /* A glyph that comes from DISP_STRING is by
29846 definition to be highlighted. */
29847 || EQ (g->object, disp_string))
29848 break;
29849 g++;
29851 if (g == e)
29853 *end = row;
29854 break;
29856 /* The first row that ends at ZV must be the last to be
29857 highlighted. */
29858 else if (next->ends_at_zv_p)
29860 *end = next;
29861 break;
29867 /* This function sets the mouse_face_* elements of HLINFO, assuming
29868 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29869 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29870 for the overlay or run of text properties specifying the mouse
29871 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29872 before-string and after-string that must also be highlighted.
29873 DISP_STRING, if non-nil, is a display string that may cover some
29874 or all of the highlighted text. */
29876 static void
29877 mouse_face_from_buffer_pos (Lisp_Object window,
29878 Mouse_HLInfo *hlinfo,
29879 ptrdiff_t mouse_charpos,
29880 ptrdiff_t start_charpos,
29881 ptrdiff_t end_charpos,
29882 Lisp_Object before_string,
29883 Lisp_Object after_string,
29884 Lisp_Object disp_string)
29886 struct window *w = XWINDOW (window);
29887 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29888 struct glyph_row *r1, *r2;
29889 struct glyph *glyph, *end;
29890 ptrdiff_t ignore, pos;
29891 int x;
29893 eassert (NILP (disp_string) || STRINGP (disp_string));
29894 eassert (NILP (before_string) || STRINGP (before_string));
29895 eassert (NILP (after_string) || STRINGP (after_string));
29897 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29898 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29899 if (r1 == NULL)
29900 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29901 /* If the before-string or display-string contains newlines,
29902 rows_from_pos_range skips to its last row. Move back. */
29903 if (!NILP (before_string) || !NILP (disp_string))
29905 struct glyph_row *prev;
29906 while ((prev = r1 - 1, prev >= first)
29907 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29908 && prev->used[TEXT_AREA] > 0)
29910 struct glyph *beg = prev->glyphs[TEXT_AREA];
29911 glyph = beg + prev->used[TEXT_AREA];
29912 while (--glyph >= beg && NILP (glyph->object));
29913 if (glyph < beg
29914 || !(EQ (glyph->object, before_string)
29915 || EQ (glyph->object, disp_string)))
29916 break;
29917 r1 = prev;
29920 if (r2 == NULL)
29922 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29923 hlinfo->mouse_face_past_end = true;
29925 else if (!NILP (after_string))
29927 /* If the after-string has newlines, advance to its last row. */
29928 struct glyph_row *next;
29929 struct glyph_row *last
29930 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29932 for (next = r2 + 1;
29933 next <= last
29934 && next->used[TEXT_AREA] > 0
29935 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29936 ++next)
29937 r2 = next;
29939 /* The rest of the display engine assumes that mouse_face_beg_row is
29940 either above mouse_face_end_row or identical to it. But with
29941 bidi-reordered continued lines, the row for START_CHARPOS could
29942 be below the row for END_CHARPOS. If so, swap the rows and store
29943 them in correct order. */
29944 if (r1->y > r2->y)
29946 struct glyph_row *tem = r2;
29948 r2 = r1;
29949 r1 = tem;
29952 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29953 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29955 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29956 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29957 could be anywhere in the row and in any order. The strategy
29958 below is to find the leftmost and the rightmost glyph that
29959 belongs to either of these 3 strings, or whose position is
29960 between START_CHARPOS and END_CHARPOS, and highlight all the
29961 glyphs between those two. This may cover more than just the text
29962 between START_CHARPOS and END_CHARPOS if the range of characters
29963 strides the bidi level boundary, e.g. if the beginning is in R2L
29964 text while the end is in L2R text or vice versa. */
29965 if (!r1->reversed_p)
29967 /* This row is in a left to right paragraph. Scan it left to
29968 right. */
29969 glyph = r1->glyphs[TEXT_AREA];
29970 end = glyph + r1->used[TEXT_AREA];
29971 x = r1->x;
29973 /* Skip truncation glyphs at the start of the glyph row. */
29974 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29975 for (; glyph < end
29976 && NILP (glyph->object)
29977 && glyph->charpos < 0;
29978 ++glyph)
29979 x += glyph->pixel_width;
29981 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29982 or DISP_STRING, and the first glyph from buffer whose
29983 position is between START_CHARPOS and END_CHARPOS. */
29984 for (; glyph < end
29985 && !NILP (glyph->object)
29986 && !EQ (glyph->object, disp_string)
29987 && !(BUFFERP (glyph->object)
29988 && (glyph->charpos >= start_charpos
29989 && glyph->charpos < end_charpos));
29990 ++glyph)
29992 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29993 are present at buffer positions between START_CHARPOS and
29994 END_CHARPOS, or if they come from an overlay. */
29995 if (EQ (glyph->object, before_string))
29997 pos = string_buffer_position (before_string,
29998 start_charpos);
29999 /* If pos == 0, it means before_string came from an
30000 overlay, not from a buffer position. */
30001 if (!pos || (pos >= start_charpos && pos < end_charpos))
30002 break;
30004 else if (EQ (glyph->object, after_string))
30006 pos = string_buffer_position (after_string, end_charpos);
30007 if (!pos || (pos >= start_charpos && pos < end_charpos))
30008 break;
30010 x += glyph->pixel_width;
30012 hlinfo->mouse_face_beg_x = x;
30013 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30015 else
30017 /* This row is in a right to left paragraph. Scan it right to
30018 left. */
30019 struct glyph *g;
30021 end = r1->glyphs[TEXT_AREA] - 1;
30022 glyph = end + r1->used[TEXT_AREA];
30024 /* Skip truncation glyphs at the start of the glyph row. */
30025 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30026 for (; glyph > end
30027 && NILP (glyph->object)
30028 && glyph->charpos < 0;
30029 --glyph)
30032 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30033 or DISP_STRING, and the first glyph from buffer whose
30034 position is between START_CHARPOS and END_CHARPOS. */
30035 for (; glyph > end
30036 && !NILP (glyph->object)
30037 && !EQ (glyph->object, disp_string)
30038 && !(BUFFERP (glyph->object)
30039 && (glyph->charpos >= start_charpos
30040 && glyph->charpos < end_charpos));
30041 --glyph)
30043 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30044 are present at buffer positions between START_CHARPOS and
30045 END_CHARPOS, or if they come from an overlay. */
30046 if (EQ (glyph->object, before_string))
30048 pos = string_buffer_position (before_string, start_charpos);
30049 /* If pos == 0, it means before_string came from an
30050 overlay, not from a buffer position. */
30051 if (!pos || (pos >= start_charpos && pos < end_charpos))
30052 break;
30054 else if (EQ (glyph->object, after_string))
30056 pos = string_buffer_position (after_string, end_charpos);
30057 if (!pos || (pos >= start_charpos && pos < end_charpos))
30058 break;
30062 glyph++; /* first glyph to the right of the highlighted area */
30063 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30064 x += g->pixel_width;
30065 hlinfo->mouse_face_beg_x = x;
30066 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30069 /* If the highlight ends in a different row, compute GLYPH and END
30070 for the end row. Otherwise, reuse the values computed above for
30071 the row where the highlight begins. */
30072 if (r2 != r1)
30074 if (!r2->reversed_p)
30076 glyph = r2->glyphs[TEXT_AREA];
30077 end = glyph + r2->used[TEXT_AREA];
30078 x = r2->x;
30080 else
30082 end = r2->glyphs[TEXT_AREA] - 1;
30083 glyph = end + r2->used[TEXT_AREA];
30087 if (!r2->reversed_p)
30089 /* Skip truncation and continuation glyphs near the end of the
30090 row, and also blanks and stretch glyphs inserted by
30091 extend_face_to_end_of_line. */
30092 while (end > glyph
30093 && NILP ((end - 1)->object))
30094 --end;
30095 /* Scan the rest of the glyph row from the end, looking for the
30096 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30097 DISP_STRING, or whose position is between START_CHARPOS
30098 and END_CHARPOS */
30099 for (--end;
30100 end > glyph
30101 && !NILP (end->object)
30102 && !EQ (end->object, disp_string)
30103 && !(BUFFERP (end->object)
30104 && (end->charpos >= start_charpos
30105 && end->charpos < end_charpos));
30106 --end)
30108 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30109 are present at buffer positions between START_CHARPOS and
30110 END_CHARPOS, or if they come from an overlay. */
30111 if (EQ (end->object, before_string))
30113 pos = string_buffer_position (before_string, start_charpos);
30114 if (!pos || (pos >= start_charpos && pos < end_charpos))
30115 break;
30117 else if (EQ (end->object, after_string))
30119 pos = string_buffer_position (after_string, end_charpos);
30120 if (!pos || (pos >= start_charpos && pos < end_charpos))
30121 break;
30124 /* Find the X coordinate of the last glyph to be highlighted. */
30125 for (; glyph <= end; ++glyph)
30126 x += glyph->pixel_width;
30128 hlinfo->mouse_face_end_x = x;
30129 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30131 else
30133 /* Skip truncation and continuation glyphs near the end of the
30134 row, and also blanks and stretch glyphs inserted by
30135 extend_face_to_end_of_line. */
30136 x = r2->x;
30137 end++;
30138 while (end < glyph
30139 && NILP (end->object))
30141 x += end->pixel_width;
30142 ++end;
30144 /* Scan the rest of the glyph row from the end, looking for the
30145 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30146 DISP_STRING, or whose position is between START_CHARPOS
30147 and END_CHARPOS */
30148 for ( ;
30149 end < glyph
30150 && !NILP (end->object)
30151 && !EQ (end->object, disp_string)
30152 && !(BUFFERP (end->object)
30153 && (end->charpos >= start_charpos
30154 && end->charpos < end_charpos));
30155 ++end)
30157 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30158 are present at buffer positions between START_CHARPOS and
30159 END_CHARPOS, or if they come from an overlay. */
30160 if (EQ (end->object, before_string))
30162 pos = string_buffer_position (before_string, start_charpos);
30163 if (!pos || (pos >= start_charpos && pos < end_charpos))
30164 break;
30166 else if (EQ (end->object, after_string))
30168 pos = string_buffer_position (after_string, end_charpos);
30169 if (!pos || (pos >= start_charpos && pos < end_charpos))
30170 break;
30172 x += end->pixel_width;
30174 /* If we exited the above loop because we arrived at the last
30175 glyph of the row, and its buffer position is still not in
30176 range, it means the last character in range is the preceding
30177 newline. Bump the end column and x values to get past the
30178 last glyph. */
30179 if (end == glyph
30180 && BUFFERP (end->object)
30181 && (end->charpos < start_charpos
30182 || end->charpos >= end_charpos))
30184 x += end->pixel_width;
30185 ++end;
30187 hlinfo->mouse_face_end_x = x;
30188 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30191 hlinfo->mouse_face_window = window;
30192 hlinfo->mouse_face_face_id
30193 = face_at_buffer_position (w, mouse_charpos, &ignore,
30194 mouse_charpos + 1,
30195 !hlinfo->mouse_face_hidden, -1);
30196 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30199 /* The following function is not used anymore (replaced with
30200 mouse_face_from_string_pos), but I leave it here for the time
30201 being, in case someone would. */
30203 #if false /* not used */
30205 /* Find the position of the glyph for position POS in OBJECT in
30206 window W's current matrix, and return in *X, *Y the pixel
30207 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30209 RIGHT_P means return the position of the right edge of the glyph.
30210 !RIGHT_P means return the left edge position.
30212 If no glyph for POS exists in the matrix, return the position of
30213 the glyph with the next smaller position that is in the matrix, if
30214 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30215 exists in the matrix, return the position of the glyph with the
30216 next larger position in OBJECT.
30218 Value is true if a glyph was found. */
30220 static bool
30221 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30222 int *hpos, int *vpos, int *x, int *y, bool right_p)
30224 int yb = window_text_bottom_y (w);
30225 struct glyph_row *r;
30226 struct glyph *best_glyph = NULL;
30227 struct glyph_row *best_row = NULL;
30228 int best_x = 0;
30230 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30231 r->enabled_p && r->y < yb;
30232 ++r)
30234 struct glyph *g = r->glyphs[TEXT_AREA];
30235 struct glyph *e = g + r->used[TEXT_AREA];
30236 int gx;
30238 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30239 if (EQ (g->object, object))
30241 if (g->charpos == pos)
30243 best_glyph = g;
30244 best_x = gx;
30245 best_row = r;
30246 goto found;
30248 else if (best_glyph == NULL
30249 || ((eabs (g->charpos - pos)
30250 < eabs (best_glyph->charpos - pos))
30251 && (right_p
30252 ? g->charpos < pos
30253 : g->charpos > pos)))
30255 best_glyph = g;
30256 best_x = gx;
30257 best_row = r;
30262 found:
30264 if (best_glyph)
30266 *x = best_x;
30267 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30269 if (right_p)
30271 *x += best_glyph->pixel_width;
30272 ++*hpos;
30275 *y = best_row->y;
30276 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30279 return best_glyph != NULL;
30281 #endif /* not used */
30283 /* Find the positions of the first and the last glyphs in window W's
30284 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30285 (assumed to be a string), and return in HLINFO's mouse_face_*
30286 members the pixel and column/row coordinates of those glyphs. */
30288 static void
30289 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30290 Lisp_Object object,
30291 ptrdiff_t startpos, ptrdiff_t endpos)
30293 int yb = window_text_bottom_y (w);
30294 struct glyph_row *r;
30295 struct glyph *g, *e;
30296 int gx;
30297 bool found = false;
30299 /* Find the glyph row with at least one position in the range
30300 [STARTPOS..ENDPOS), and the first glyph in that row whose
30301 position belongs to that range. */
30302 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30303 r->enabled_p && r->y < yb;
30304 ++r)
30306 if (!r->reversed_p)
30308 g = r->glyphs[TEXT_AREA];
30309 e = g + r->used[TEXT_AREA];
30310 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30311 if (EQ (g->object, object)
30312 && startpos <= g->charpos && g->charpos < endpos)
30314 hlinfo->mouse_face_beg_row
30315 = MATRIX_ROW_VPOS (r, w->current_matrix);
30316 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30317 hlinfo->mouse_face_beg_x = gx;
30318 found = true;
30319 break;
30322 else
30324 struct glyph *g1;
30326 e = r->glyphs[TEXT_AREA];
30327 g = e + r->used[TEXT_AREA];
30328 for ( ; g > e; --g)
30329 if (EQ ((g-1)->object, object)
30330 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30332 hlinfo->mouse_face_beg_row
30333 = MATRIX_ROW_VPOS (r, w->current_matrix);
30334 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30335 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30336 gx += g1->pixel_width;
30337 hlinfo->mouse_face_beg_x = gx;
30338 found = true;
30339 break;
30342 if (found)
30343 break;
30346 if (!found)
30347 return;
30349 /* Starting with the next row, look for the first row which does NOT
30350 include any glyphs whose positions are in the range. */
30351 for (++r; r->enabled_p && r->y < yb; ++r)
30353 g = r->glyphs[TEXT_AREA];
30354 e = g + r->used[TEXT_AREA];
30355 found = false;
30356 for ( ; g < e; ++g)
30357 if (EQ (g->object, object)
30358 && startpos <= g->charpos && g->charpos < endpos)
30360 found = true;
30361 break;
30363 if (!found)
30364 break;
30367 /* The highlighted region ends on the previous row. */
30368 r--;
30370 /* Set the end row. */
30371 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30373 /* Compute and set the end column and the end column's horizontal
30374 pixel coordinate. */
30375 if (!r->reversed_p)
30377 g = r->glyphs[TEXT_AREA];
30378 e = g + r->used[TEXT_AREA];
30379 for ( ; e > g; --e)
30380 if (EQ ((e-1)->object, object)
30381 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30382 break;
30383 hlinfo->mouse_face_end_col = e - g;
30385 for (gx = r->x; g < e; ++g)
30386 gx += g->pixel_width;
30387 hlinfo->mouse_face_end_x = gx;
30389 else
30391 e = r->glyphs[TEXT_AREA];
30392 g = e + r->used[TEXT_AREA];
30393 for (gx = r->x ; e < g; ++e)
30395 if (EQ (e->object, object)
30396 && startpos <= e->charpos && e->charpos < endpos)
30397 break;
30398 gx += e->pixel_width;
30400 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30401 hlinfo->mouse_face_end_x = gx;
30405 #ifdef HAVE_WINDOW_SYSTEM
30407 /* See if position X, Y is within a hot-spot of an image. */
30409 static bool
30410 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30412 if (!CONSP (hot_spot))
30413 return false;
30415 if (EQ (XCAR (hot_spot), Qrect))
30417 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30418 Lisp_Object rect = XCDR (hot_spot);
30419 Lisp_Object tem;
30420 if (!CONSP (rect))
30421 return false;
30422 if (!CONSP (XCAR (rect)))
30423 return false;
30424 if (!CONSP (XCDR (rect)))
30425 return false;
30426 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30427 return false;
30428 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30429 return false;
30430 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30431 return false;
30432 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30433 return false;
30434 return true;
30436 else if (EQ (XCAR (hot_spot), Qcircle))
30438 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30439 Lisp_Object circ = XCDR (hot_spot);
30440 Lisp_Object lr, lx0, ly0;
30441 if (CONSP (circ)
30442 && CONSP (XCAR (circ))
30443 && (lr = XCDR (circ), NUMBERP (lr))
30444 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30445 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30447 double r = XFLOATINT (lr);
30448 double dx = XINT (lx0) - x;
30449 double dy = XINT (ly0) - y;
30450 return (dx * dx + dy * dy <= r * r);
30453 else if (EQ (XCAR (hot_spot), Qpoly))
30455 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30456 if (VECTORP (XCDR (hot_spot)))
30458 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30459 Lisp_Object *poly = v->contents;
30460 ptrdiff_t n = v->header.size;
30461 ptrdiff_t i;
30462 bool inside = false;
30463 Lisp_Object lx, ly;
30464 int x0, y0;
30466 /* Need an even number of coordinates, and at least 3 edges. */
30467 if (n < 6 || n & 1)
30468 return false;
30470 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30471 If count is odd, we are inside polygon. Pixels on edges
30472 may or may not be included depending on actual geometry of the
30473 polygon. */
30474 if ((lx = poly[n-2], !INTEGERP (lx))
30475 || (ly = poly[n-1], !INTEGERP (lx)))
30476 return false;
30477 x0 = XINT (lx), y0 = XINT (ly);
30478 for (i = 0; i < n; i += 2)
30480 int x1 = x0, y1 = y0;
30481 if ((lx = poly[i], !INTEGERP (lx))
30482 || (ly = poly[i+1], !INTEGERP (ly)))
30483 return false;
30484 x0 = XINT (lx), y0 = XINT (ly);
30486 /* Does this segment cross the X line? */
30487 if (x0 >= x)
30489 if (x1 >= x)
30490 continue;
30492 else if (x1 < x)
30493 continue;
30494 if (y > y0 && y > y1)
30495 continue;
30496 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30497 inside = !inside;
30499 return inside;
30502 return false;
30505 Lisp_Object
30506 find_hot_spot (Lisp_Object map, int x, int y)
30508 while (CONSP (map))
30510 if (CONSP (XCAR (map))
30511 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30512 return XCAR (map);
30513 map = XCDR (map);
30516 return Qnil;
30519 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30520 3, 3, 0,
30521 doc: /* Lookup in image map MAP coordinates X and Y.
30522 An image map is an alist where each element has the format (AREA ID PLIST).
30523 An AREA is specified as either a rectangle, a circle, or a polygon:
30524 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30525 pixel coordinates of the upper left and bottom right corners.
30526 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30527 and the radius of the circle; r may be a float or integer.
30528 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30529 vector describes one corner in the polygon.
30530 Returns the alist element for the first matching AREA in MAP. */)
30531 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30533 if (NILP (map))
30534 return Qnil;
30536 CHECK_NUMBER (x);
30537 CHECK_NUMBER (y);
30539 return find_hot_spot (map,
30540 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30541 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30543 #endif /* HAVE_WINDOW_SYSTEM */
30546 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30547 static void
30548 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30550 #ifdef HAVE_WINDOW_SYSTEM
30551 if (!FRAME_WINDOW_P (f))
30552 return;
30554 /* Do not change cursor shape while dragging mouse. */
30555 if (EQ (do_mouse_tracking, Qdragging))
30556 return;
30558 if (!NILP (pointer))
30560 if (EQ (pointer, Qarrow))
30561 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30562 else if (EQ (pointer, Qhand))
30563 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30564 else if (EQ (pointer, Qtext))
30565 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30566 else if (EQ (pointer, intern ("hdrag")))
30567 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30568 else if (EQ (pointer, intern ("nhdrag")))
30569 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30570 # ifdef HAVE_X_WINDOWS
30571 else if (EQ (pointer, intern ("vdrag")))
30572 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30573 # endif
30574 else if (EQ (pointer, intern ("hourglass")))
30575 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30576 else if (EQ (pointer, Qmodeline))
30577 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30578 else
30579 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30582 if (cursor != No_Cursor)
30583 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30584 #endif
30587 /* Take proper action when mouse has moved to the mode or header line
30588 or marginal area AREA of window W, x-position X and y-position Y.
30589 X is relative to the start of the text display area of W, so the
30590 width of bitmap areas and scroll bars must be subtracted to get a
30591 position relative to the start of the mode line. */
30593 static void
30594 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30595 enum window_part area)
30597 struct window *w = XWINDOW (window);
30598 struct frame *f = XFRAME (w->frame);
30599 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30600 #ifdef HAVE_WINDOW_SYSTEM
30601 Display_Info *dpyinfo;
30602 #endif
30603 Cursor cursor = No_Cursor;
30604 Lisp_Object pointer = Qnil;
30605 int dx, dy, width, height;
30606 ptrdiff_t charpos;
30607 Lisp_Object string, object = Qnil;
30608 Lisp_Object pos UNINIT;
30609 Lisp_Object mouse_face;
30610 int original_x_pixel = x;
30611 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30612 struct glyph_row *row UNINIT;
30614 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30616 int x0;
30617 struct glyph *end;
30619 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30620 returns them in row/column units! */
30621 string = mode_line_string (w, area, &x, &y, &charpos,
30622 &object, &dx, &dy, &width, &height);
30624 row = (area == ON_MODE_LINE
30625 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30626 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30628 /* Find the glyph under the mouse pointer. */
30629 if (row->mode_line_p && row->enabled_p)
30631 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30632 end = glyph + row->used[TEXT_AREA];
30634 for (x0 = original_x_pixel;
30635 glyph < end && x0 >= glyph->pixel_width;
30636 ++glyph)
30637 x0 -= glyph->pixel_width;
30639 if (glyph >= end)
30640 glyph = NULL;
30643 else
30645 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30646 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30647 returns them in row/column units! */
30648 string = marginal_area_string (w, area, &x, &y, &charpos,
30649 &object, &dx, &dy, &width, &height);
30652 Lisp_Object help = Qnil;
30654 #ifdef HAVE_WINDOW_SYSTEM
30655 if (IMAGEP (object))
30657 Lisp_Object image_map, hotspot;
30658 if ((image_map = Fplist_get (XCDR (object), QCmap),
30659 !NILP (image_map))
30660 && (hotspot = find_hot_spot (image_map, dx, dy),
30661 CONSP (hotspot))
30662 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30664 Lisp_Object plist;
30666 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30667 If so, we could look for mouse-enter, mouse-leave
30668 properties in PLIST (and do something...). */
30669 hotspot = XCDR (hotspot);
30670 if (CONSP (hotspot)
30671 && (plist = XCAR (hotspot), CONSP (plist)))
30673 pointer = Fplist_get (plist, Qpointer);
30674 if (NILP (pointer))
30675 pointer = Qhand;
30676 help = Fplist_get (plist, Qhelp_echo);
30677 if (!NILP (help))
30679 help_echo_string = help;
30680 XSETWINDOW (help_echo_window, w);
30681 help_echo_object = w->contents;
30682 help_echo_pos = charpos;
30686 if (NILP (pointer))
30687 pointer = Fplist_get (XCDR (object), QCpointer);
30689 #endif /* HAVE_WINDOW_SYSTEM */
30691 if (STRINGP (string))
30692 pos = make_number (charpos);
30694 /* Set the help text and mouse pointer. If the mouse is on a part
30695 of the mode line without any text (e.g. past the right edge of
30696 the mode line text), use the default help text and pointer. */
30697 if (STRINGP (string) || area == ON_MODE_LINE)
30699 /* Arrange to display the help by setting the global variables
30700 help_echo_string, help_echo_object, and help_echo_pos. */
30701 if (NILP (help))
30703 if (STRINGP (string))
30704 help = Fget_text_property (pos, Qhelp_echo, string);
30706 if (!NILP (help))
30708 help_echo_string = help;
30709 XSETWINDOW (help_echo_window, w);
30710 help_echo_object = string;
30711 help_echo_pos = charpos;
30713 else if (area == ON_MODE_LINE)
30715 Lisp_Object default_help
30716 = buffer_local_value (Qmode_line_default_help_echo,
30717 w->contents);
30719 if (STRINGP (default_help))
30721 help_echo_string = default_help;
30722 XSETWINDOW (help_echo_window, w);
30723 help_echo_object = Qnil;
30724 help_echo_pos = -1;
30729 #ifdef HAVE_WINDOW_SYSTEM
30730 /* Change the mouse pointer according to what is under it. */
30731 if (FRAME_WINDOW_P (f))
30733 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30734 || minibuf_level
30735 || NILP (Vresize_mini_windows));
30737 dpyinfo = FRAME_DISPLAY_INFO (f);
30738 if (STRINGP (string))
30740 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30742 if (NILP (pointer))
30743 pointer = Fget_text_property (pos, Qpointer, string);
30745 /* Change the mouse pointer according to what is under X/Y. */
30746 if (NILP (pointer)
30747 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30749 Lisp_Object map;
30750 map = Fget_text_property (pos, Qlocal_map, string);
30751 if (!KEYMAPP (map))
30752 map = Fget_text_property (pos, Qkeymap, string);
30753 if (!KEYMAPP (map) && draggable)
30754 cursor = dpyinfo->vertical_scroll_bar_cursor;
30757 else if (draggable)
30758 /* Default mode-line pointer. */
30759 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30761 #endif
30764 /* Change the mouse face according to what is under X/Y. */
30765 bool mouse_face_shown = false;
30766 if (STRINGP (string))
30768 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30769 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30770 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30771 && glyph)
30773 Lisp_Object b, e;
30775 struct glyph * tmp_glyph;
30777 int gpos;
30778 int gseq_length;
30779 int total_pixel_width;
30780 ptrdiff_t begpos, endpos, ignore;
30782 int vpos, hpos;
30784 b = Fprevious_single_property_change (make_number (charpos + 1),
30785 Qmouse_face, string, Qnil);
30786 if (NILP (b))
30787 begpos = 0;
30788 else
30789 begpos = XINT (b);
30791 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30792 if (NILP (e))
30793 endpos = SCHARS (string);
30794 else
30795 endpos = XINT (e);
30797 /* Calculate the glyph position GPOS of GLYPH in the
30798 displayed string, relative to the beginning of the
30799 highlighted part of the string.
30801 Note: GPOS is different from CHARPOS. CHARPOS is the
30802 position of GLYPH in the internal string object. A mode
30803 line string format has structures which are converted to
30804 a flattened string by the Emacs Lisp interpreter. The
30805 internal string is an element of those structures. The
30806 displayed string is the flattened string. */
30807 tmp_glyph = row_start_glyph;
30808 while (tmp_glyph < glyph
30809 && (!(EQ (tmp_glyph->object, glyph->object)
30810 && begpos <= tmp_glyph->charpos
30811 && tmp_glyph->charpos < endpos)))
30812 tmp_glyph++;
30813 gpos = glyph - tmp_glyph;
30815 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30816 the highlighted part of the displayed string to which
30817 GLYPH belongs. Note: GSEQ_LENGTH is different from
30818 SCHARS (STRING), because the latter returns the length of
30819 the internal string. */
30820 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30821 tmp_glyph > glyph
30822 && (!(EQ (tmp_glyph->object, glyph->object)
30823 && begpos <= tmp_glyph->charpos
30824 && tmp_glyph->charpos < endpos));
30825 tmp_glyph--)
30827 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30829 /* Calculate the total pixel width of all the glyphs between
30830 the beginning of the highlighted area and GLYPH. */
30831 total_pixel_width = 0;
30832 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30833 total_pixel_width += tmp_glyph->pixel_width;
30835 /* Pre calculation of re-rendering position. Note: X is in
30836 column units here, after the call to mode_line_string or
30837 marginal_area_string. */
30838 hpos = x - gpos;
30839 vpos = (area == ON_MODE_LINE
30840 ? (w->current_matrix)->nrows - 1
30841 : 0);
30843 /* If GLYPH's position is included in the region that is
30844 already drawn in mouse face, we have nothing to do. */
30845 if ( EQ (window, hlinfo->mouse_face_window)
30846 && (!row->reversed_p
30847 ? (hlinfo->mouse_face_beg_col <= hpos
30848 && hpos < hlinfo->mouse_face_end_col)
30849 /* In R2L rows we swap BEG and END, see below. */
30850 : (hlinfo->mouse_face_end_col <= hpos
30851 && hpos < hlinfo->mouse_face_beg_col))
30852 && hlinfo->mouse_face_beg_row == vpos )
30853 return;
30855 if (clear_mouse_face (hlinfo))
30856 cursor = No_Cursor;
30858 if (!row->reversed_p)
30860 hlinfo->mouse_face_beg_col = hpos;
30861 hlinfo->mouse_face_beg_x = original_x_pixel
30862 - (total_pixel_width + dx);
30863 hlinfo->mouse_face_end_col = hpos + gseq_length;
30864 hlinfo->mouse_face_end_x = 0;
30866 else
30868 /* In R2L rows, show_mouse_face expects BEG and END
30869 coordinates to be swapped. */
30870 hlinfo->mouse_face_end_col = hpos;
30871 hlinfo->mouse_face_end_x = original_x_pixel
30872 - (total_pixel_width + dx);
30873 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30874 hlinfo->mouse_face_beg_x = 0;
30877 hlinfo->mouse_face_beg_row = vpos;
30878 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30879 hlinfo->mouse_face_past_end = false;
30880 hlinfo->mouse_face_window = window;
30882 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30883 charpos,
30884 0, &ignore,
30885 glyph->face_id,
30886 true);
30887 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30888 mouse_face_shown = true;
30890 if (NILP (pointer))
30891 pointer = Qhand;
30895 /* If mouse-face doesn't need to be shown, clear any existing
30896 mouse-face. */
30897 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30898 clear_mouse_face (hlinfo);
30900 define_frame_cursor1 (f, cursor, pointer);
30904 /* EXPORT:
30905 Take proper action when the mouse has moved to position X, Y on
30906 frame F with regards to highlighting portions of display that have
30907 mouse-face properties. Also de-highlight portions of display where
30908 the mouse was before, set the mouse pointer shape as appropriate
30909 for the mouse coordinates, and activate help echo (tooltips).
30910 X and Y can be negative or out of range. */
30912 void
30913 note_mouse_highlight (struct frame *f, int x, int y)
30915 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30916 enum window_part part = ON_NOTHING;
30917 Lisp_Object window;
30918 struct window *w;
30919 Cursor cursor = No_Cursor;
30920 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30921 struct buffer *b;
30923 /* When a menu is active, don't highlight because this looks odd. */
30924 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30925 if (popup_activated ())
30926 return;
30927 #endif
30929 if (!f->glyphs_initialized_p
30930 || f->pointer_invisible)
30931 return;
30933 hlinfo->mouse_face_mouse_x = x;
30934 hlinfo->mouse_face_mouse_y = y;
30935 hlinfo->mouse_face_mouse_frame = f;
30937 if (hlinfo->mouse_face_defer)
30938 return;
30940 /* Which window is that in? */
30941 window = window_from_coordinates (f, x, y, &part, true);
30943 /* If displaying active text in another window, clear that. */
30944 if (! EQ (window, hlinfo->mouse_face_window)
30945 /* Also clear if we move out of text area in same window. */
30946 || (!NILP (hlinfo->mouse_face_window)
30947 && !NILP (window)
30948 && part != ON_TEXT
30949 && part != ON_MODE_LINE
30950 && part != ON_HEADER_LINE))
30951 clear_mouse_face (hlinfo);
30953 /* Reset help_echo_string. It will get recomputed below. */
30954 help_echo_string = Qnil;
30956 #ifdef HAVE_WINDOW_SYSTEM
30957 /* If the cursor is on the internal border of FRAME and FRAME's
30958 internal border is draggable, provide some visual feedback. */
30959 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
30960 && !NILP (get_frame_param (f, Qdrag_internal_border)))
30962 enum internal_border_part part = frame_internal_border_part (f, x, y);
30964 switch (part)
30966 case INTERNAL_BORDER_NONE:
30967 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30968 /* Reset cursor. */
30969 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30970 break;
30971 case INTERNAL_BORDER_LEFT_EDGE:
30972 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
30973 break;
30974 case INTERNAL_BORDER_TOP_LEFT_CORNER:
30975 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
30976 break;
30977 case INTERNAL_BORDER_TOP_EDGE:
30978 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
30979 break;
30980 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
30981 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
30982 break;
30983 case INTERNAL_BORDER_RIGHT_EDGE:
30984 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
30985 break;
30986 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
30987 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
30988 break;
30989 case INTERNAL_BORDER_BOTTOM_EDGE:
30990 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
30991 break;
30992 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
30993 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
30994 break;
30995 default:
30996 /* This should not happen. */
30997 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
30998 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31001 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31003 /* Do we really want a help echo here? */
31004 help_echo_string = build_string ("drag-mouse-1: resize frame");
31005 goto set_cursor;
31008 #endif /* HAVE_WINDOW_SYSTEM */
31010 /* Not on a window -> return. */
31011 if (!WINDOWP (window))
31012 return;
31014 /* Convert to window-relative pixel coordinates. */
31015 w = XWINDOW (window);
31016 frame_to_window_pixel_xy (w, &x, &y);
31018 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31019 /* Handle tool-bar window differently since it doesn't display a
31020 buffer. */
31021 if (EQ (window, f->tool_bar_window))
31023 note_tool_bar_highlight (f, x, y);
31024 return;
31026 #endif
31028 /* Mouse is on the mode, header line or margin? */
31029 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31030 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31032 note_mode_line_or_margin_highlight (window, x, y, part);
31034 #ifdef HAVE_WINDOW_SYSTEM
31035 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31037 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31038 /* Show non-text cursor (Bug#16647). */
31039 goto set_cursor;
31041 else
31042 #endif
31043 return;
31046 #ifdef HAVE_WINDOW_SYSTEM
31047 if (part == ON_VERTICAL_BORDER)
31049 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31050 help_echo_string = build_string ("drag-mouse-1: resize");
31051 goto set_cursor;
31053 else if (part == ON_RIGHT_DIVIDER)
31055 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31056 help_echo_string = build_string ("drag-mouse-1: resize");
31057 goto set_cursor;
31059 else if (part == ON_BOTTOM_DIVIDER)
31060 if (! WINDOW_BOTTOMMOST_P (w)
31061 || minibuf_level
31062 || NILP (Vresize_mini_windows))
31064 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31065 help_echo_string = build_string ("drag-mouse-1: resize");
31066 goto set_cursor;
31068 else
31069 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31070 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31071 || part == ON_VERTICAL_SCROLL_BAR
31072 || part == ON_HORIZONTAL_SCROLL_BAR)
31073 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31074 else
31075 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31076 #endif
31078 /* Are we in a window whose display is up to date?
31079 And verify the buffer's text has not changed. */
31080 b = XBUFFER (w->contents);
31081 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31083 int hpos, vpos, dx, dy, area = LAST_AREA;
31084 ptrdiff_t pos;
31085 struct glyph *glyph;
31086 Lisp_Object object;
31087 Lisp_Object mouse_face = Qnil, position;
31088 Lisp_Object *overlay_vec = NULL;
31089 ptrdiff_t i, noverlays;
31090 struct buffer *obuf;
31091 ptrdiff_t obegv, ozv;
31092 bool same_region;
31094 /* Find the glyph under X/Y. */
31095 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31097 #ifdef HAVE_WINDOW_SYSTEM
31098 /* Look for :pointer property on image. */
31099 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31101 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31102 if (img != NULL && IMAGEP (img->spec))
31104 Lisp_Object image_map, hotspot;
31105 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31106 !NILP (image_map))
31107 && (hotspot = find_hot_spot (image_map,
31108 glyph->slice.img.x + dx,
31109 glyph->slice.img.y + dy),
31110 CONSP (hotspot))
31111 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31113 Lisp_Object plist;
31115 /* Could check XCAR (hotspot) to see if we enter/leave
31116 this hot-spot.
31117 If so, we could look for mouse-enter, mouse-leave
31118 properties in PLIST (and do something...). */
31119 hotspot = XCDR (hotspot);
31120 if (CONSP (hotspot)
31121 && (plist = XCAR (hotspot), CONSP (plist)))
31123 pointer = Fplist_get (plist, Qpointer);
31124 if (NILP (pointer))
31125 pointer = Qhand;
31126 help_echo_string = Fplist_get (plist, Qhelp_echo);
31127 if (!NILP (help_echo_string))
31129 help_echo_window = window;
31130 help_echo_object = glyph->object;
31131 help_echo_pos = glyph->charpos;
31135 if (NILP (pointer))
31136 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31139 #endif /* HAVE_WINDOW_SYSTEM */
31141 /* Clear mouse face if X/Y not over text. */
31142 if (glyph == NULL
31143 || area != TEXT_AREA
31144 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31145 /* Glyph's OBJECT is nil for glyphs inserted by the
31146 display engine for its internal purposes, like truncation
31147 and continuation glyphs and blanks beyond the end of
31148 line's text on text terminals. If we are over such a
31149 glyph, we are not over any text. */
31150 || NILP (glyph->object)
31151 /* R2L rows have a stretch glyph at their front, which
31152 stands for no text, whereas L2R rows have no glyphs at
31153 all beyond the end of text. Treat such stretch glyphs
31154 like we do with NULL glyphs in L2R rows. */
31155 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31156 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31157 && glyph->type == STRETCH_GLYPH
31158 && glyph->avoid_cursor_p))
31160 if (clear_mouse_face (hlinfo))
31161 cursor = No_Cursor;
31162 if (FRAME_WINDOW_P (f) && NILP (pointer))
31164 #ifdef HAVE_WINDOW_SYSTEM
31165 if (area != TEXT_AREA)
31166 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31167 else
31168 pointer = Vvoid_text_area_pointer;
31169 #endif
31171 goto set_cursor;
31174 pos = glyph->charpos;
31175 object = glyph->object;
31176 if (!STRINGP (object) && !BUFFERP (object))
31177 goto set_cursor;
31179 /* If we get an out-of-range value, return now; avoid an error. */
31180 if (BUFFERP (object) && pos > BUF_Z (b))
31181 goto set_cursor;
31183 /* Make the window's buffer temporarily current for
31184 overlays_at and compute_char_face. */
31185 obuf = current_buffer;
31186 current_buffer = b;
31187 obegv = BEGV;
31188 ozv = ZV;
31189 BEGV = BEG;
31190 ZV = Z;
31192 /* Is this char mouse-active or does it have help-echo? */
31193 position = make_number (pos);
31195 USE_SAFE_ALLOCA;
31197 if (BUFFERP (object))
31199 /* Put all the overlays we want in a vector in overlay_vec. */
31200 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31201 /* Sort overlays into increasing priority order. */
31202 noverlays = sort_overlays (overlay_vec, noverlays, w);
31204 else
31205 noverlays = 0;
31207 if (NILP (Vmouse_highlight))
31209 clear_mouse_face (hlinfo);
31210 goto check_help_echo;
31213 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31215 if (same_region)
31216 cursor = No_Cursor;
31218 /* Check mouse-face highlighting. */
31219 if (! same_region
31220 /* If there exists an overlay with mouse-face overlapping
31221 the one we are currently highlighting, we have to
31222 check if we enter the overlapping overlay, and then
31223 highlight only that. */
31224 || (OVERLAYP (hlinfo->mouse_face_overlay)
31225 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31227 /* Find the highest priority overlay with a mouse-face. */
31228 Lisp_Object overlay = Qnil;
31229 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31231 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31232 if (!NILP (mouse_face))
31233 overlay = overlay_vec[i];
31236 /* If we're highlighting the same overlay as before, there's
31237 no need to do that again. */
31238 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31239 goto check_help_echo;
31241 /* Clear the display of the old active region, if any. */
31242 if (clear_mouse_face (hlinfo))
31243 cursor = No_Cursor;
31245 /* Record the overlay, if any, to be highlighted. */
31246 hlinfo->mouse_face_overlay = overlay;
31248 /* If no overlay applies, get a text property. */
31249 if (NILP (overlay))
31250 mouse_face = Fget_text_property (position, Qmouse_face, object);
31252 /* Next, compute the bounds of the mouse highlighting and
31253 display it. */
31254 if (!NILP (mouse_face) && STRINGP (object))
31256 /* The mouse-highlighting comes from a display string
31257 with a mouse-face. */
31258 Lisp_Object s, e;
31259 ptrdiff_t ignore;
31261 s = Fprevious_single_property_change
31262 (make_number (pos + 1), Qmouse_face, object, Qnil);
31263 e = Fnext_single_property_change
31264 (position, Qmouse_face, object, Qnil);
31265 if (NILP (s))
31266 s = make_number (0);
31267 if (NILP (e))
31268 e = make_number (SCHARS (object));
31269 mouse_face_from_string_pos (w, hlinfo, object,
31270 XINT (s), XINT (e));
31271 hlinfo->mouse_face_past_end = false;
31272 hlinfo->mouse_face_window = window;
31273 hlinfo->mouse_face_face_id
31274 = face_at_string_position (w, object, pos, 0, &ignore,
31275 glyph->face_id, true);
31276 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31277 cursor = No_Cursor;
31279 else
31281 /* The mouse-highlighting, if any, comes from an overlay
31282 or text property in the buffer. */
31283 Lisp_Object buffer UNINIT;
31284 Lisp_Object disp_string UNINIT;
31286 if (STRINGP (object))
31288 /* If we are on a display string with no mouse-face,
31289 check if the text under it has one. */
31290 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31291 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31292 pos = string_buffer_position (object, start);
31293 if (pos > 0)
31295 mouse_face = get_char_property_and_overlay
31296 (make_number (pos), Qmouse_face, w->contents, &overlay);
31297 buffer = w->contents;
31298 disp_string = object;
31301 else
31303 buffer = object;
31304 disp_string = Qnil;
31307 if (!NILP (mouse_face))
31309 Lisp_Object before, after;
31310 Lisp_Object before_string, after_string;
31311 /* To correctly find the limits of mouse highlight
31312 in a bidi-reordered buffer, we must not use the
31313 optimization of limiting the search in
31314 previous-single-property-change and
31315 next-single-property-change, because
31316 rows_from_pos_range needs the real start and end
31317 positions to DTRT in this case. That's because
31318 the first row visible in a window does not
31319 necessarily display the character whose position
31320 is the smallest. */
31321 Lisp_Object lim1
31322 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31323 ? Fmarker_position (w->start)
31324 : Qnil;
31325 Lisp_Object lim2
31326 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31327 ? make_number (BUF_Z (XBUFFER (buffer))
31328 - w->window_end_pos)
31329 : Qnil;
31331 if (NILP (overlay))
31333 /* Handle the text property case. */
31334 before = Fprevious_single_property_change
31335 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31336 after = Fnext_single_property_change
31337 (make_number (pos), Qmouse_face, buffer, lim2);
31338 before_string = after_string = Qnil;
31340 else
31342 /* Handle the overlay case. */
31343 before = Foverlay_start (overlay);
31344 after = Foverlay_end (overlay);
31345 before_string = Foverlay_get (overlay, Qbefore_string);
31346 after_string = Foverlay_get (overlay, Qafter_string);
31348 if (!STRINGP (before_string)) before_string = Qnil;
31349 if (!STRINGP (after_string)) after_string = Qnil;
31352 mouse_face_from_buffer_pos (window, hlinfo, pos,
31353 NILP (before)
31355 : XFASTINT (before),
31356 NILP (after)
31357 ? BUF_Z (XBUFFER (buffer))
31358 : XFASTINT (after),
31359 before_string, after_string,
31360 disp_string);
31361 cursor = No_Cursor;
31366 check_help_echo:
31368 /* Look for a `help-echo' property. */
31369 if (NILP (help_echo_string)) {
31370 Lisp_Object help, overlay;
31372 /* Check overlays first. */
31373 help = overlay = Qnil;
31374 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31376 overlay = overlay_vec[i];
31377 help = Foverlay_get (overlay, Qhelp_echo);
31380 if (!NILP (help))
31382 help_echo_string = help;
31383 help_echo_window = window;
31384 help_echo_object = overlay;
31385 help_echo_pos = pos;
31387 else
31389 Lisp_Object obj = glyph->object;
31390 ptrdiff_t charpos = glyph->charpos;
31392 /* Try text properties. */
31393 if (STRINGP (obj)
31394 && charpos >= 0
31395 && charpos < SCHARS (obj))
31397 help = Fget_text_property (make_number (charpos),
31398 Qhelp_echo, obj);
31399 if (NILP (help))
31401 /* If the string itself doesn't specify a help-echo,
31402 see if the buffer text ``under'' it does. */
31403 struct glyph_row *r
31404 = MATRIX_ROW (w->current_matrix, vpos);
31405 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31406 ptrdiff_t p = string_buffer_position (obj, start);
31407 if (p > 0)
31409 help = Fget_char_property (make_number (p),
31410 Qhelp_echo, w->contents);
31411 if (!NILP (help))
31413 charpos = p;
31414 obj = w->contents;
31419 else if (BUFFERP (obj)
31420 && charpos >= BEGV
31421 && charpos < ZV)
31422 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31423 obj);
31425 if (!NILP (help))
31427 help_echo_string = help;
31428 help_echo_window = window;
31429 help_echo_object = obj;
31430 help_echo_pos = charpos;
31435 #ifdef HAVE_WINDOW_SYSTEM
31436 /* Look for a `pointer' property. */
31437 if (FRAME_WINDOW_P (f) && NILP (pointer))
31439 /* Check overlays first. */
31440 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31441 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31443 if (NILP (pointer))
31445 Lisp_Object obj = glyph->object;
31446 ptrdiff_t charpos = glyph->charpos;
31448 /* Try text properties. */
31449 if (STRINGP (obj)
31450 && charpos >= 0
31451 && charpos < SCHARS (obj))
31453 pointer = Fget_text_property (make_number (charpos),
31454 Qpointer, obj);
31455 if (NILP (pointer))
31457 /* If the string itself doesn't specify a pointer,
31458 see if the buffer text ``under'' it does. */
31459 struct glyph_row *r
31460 = MATRIX_ROW (w->current_matrix, vpos);
31461 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31462 ptrdiff_t p = string_buffer_position (obj, start);
31463 if (p > 0)
31464 pointer = Fget_char_property (make_number (p),
31465 Qpointer, w->contents);
31468 else if (BUFFERP (obj)
31469 && charpos >= BEGV
31470 && charpos < ZV)
31471 pointer = Fget_text_property (make_number (charpos),
31472 Qpointer, obj);
31475 #endif /* HAVE_WINDOW_SYSTEM */
31477 BEGV = obegv;
31478 ZV = ozv;
31479 current_buffer = obuf;
31480 SAFE_FREE ();
31483 set_cursor:
31484 define_frame_cursor1 (f, cursor, pointer);
31488 /* EXPORT for RIF:
31489 Clear any mouse-face on window W. This function is part of the
31490 redisplay interface, and is called from try_window_id and similar
31491 functions to ensure the mouse-highlight is off. */
31493 void
31494 x_clear_window_mouse_face (struct window *w)
31496 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31497 Lisp_Object window;
31499 block_input ();
31500 XSETWINDOW (window, w);
31501 if (EQ (window, hlinfo->mouse_face_window))
31502 clear_mouse_face (hlinfo);
31503 unblock_input ();
31507 /* EXPORT:
31508 Just discard the mouse face information for frame F, if any.
31509 This is used when the size of F is changed. */
31511 void
31512 cancel_mouse_face (struct frame *f)
31514 Lisp_Object window;
31515 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31517 window = hlinfo->mouse_face_window;
31518 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31519 reset_mouse_highlight (hlinfo);
31524 /***********************************************************************
31525 Exposure Events
31526 ***********************************************************************/
31528 #ifdef HAVE_WINDOW_SYSTEM
31530 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31531 which intersects rectangle R. R is in window-relative coordinates. */
31533 static void
31534 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31535 enum glyph_row_area area)
31537 struct glyph *first = row->glyphs[area];
31538 struct glyph *end = row->glyphs[area] + row->used[area];
31539 struct glyph *last;
31540 int first_x, start_x, x;
31542 if (area == TEXT_AREA && row->fill_line_p)
31543 /* If row extends face to end of line write the whole line. */
31544 draw_glyphs (w, 0, row, area,
31545 0, row->used[area],
31546 DRAW_NORMAL_TEXT, 0);
31547 else
31549 /* Set START_X to the window-relative start position for drawing glyphs of
31550 AREA. The first glyph of the text area can be partially visible.
31551 The first glyphs of other areas cannot. */
31552 start_x = window_box_left_offset (w, area);
31553 x = start_x;
31554 if (area == TEXT_AREA)
31555 x += row->x;
31557 /* Find the first glyph that must be redrawn. */
31558 while (first < end
31559 && x + first->pixel_width < r->x)
31561 x += first->pixel_width;
31562 ++first;
31565 /* Find the last one. */
31566 last = first;
31567 first_x = x;
31568 /* Use a signed int intermediate value to avoid catastrophic
31569 failures due to comparison between signed and unsigned, when
31570 x is negative (can happen for wide images that are hscrolled). */
31571 int r_end = r->x + r->width;
31572 while (last < end && x < r_end)
31574 x += last->pixel_width;
31575 ++last;
31578 /* Repaint. */
31579 if (last > first)
31580 draw_glyphs (w, first_x - start_x, row, area,
31581 first - row->glyphs[area], last - row->glyphs[area],
31582 DRAW_NORMAL_TEXT, 0);
31587 /* Redraw the parts of the glyph row ROW on window W intersecting
31588 rectangle R. R is in window-relative coordinates. Value is
31589 true if mouse-face was overwritten. */
31591 static bool
31592 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31594 eassert (row->enabled_p);
31596 if (row->mode_line_p || w->pseudo_window_p)
31597 draw_glyphs (w, 0, row, TEXT_AREA,
31598 0, row->used[TEXT_AREA],
31599 DRAW_NORMAL_TEXT, 0);
31600 else
31602 if (row->used[LEFT_MARGIN_AREA])
31603 expose_area (w, row, r, LEFT_MARGIN_AREA);
31604 if (row->used[TEXT_AREA])
31605 expose_area (w, row, r, TEXT_AREA);
31606 if (row->used[RIGHT_MARGIN_AREA])
31607 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31608 draw_row_fringe_bitmaps (w, row);
31611 return row->mouse_face_p;
31615 /* Redraw those parts of glyphs rows during expose event handling that
31616 overlap other rows. Redrawing of an exposed line writes over parts
31617 of lines overlapping that exposed line; this function fixes that.
31619 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31620 row in W's current matrix that is exposed and overlaps other rows.
31621 LAST_OVERLAPPING_ROW is the last such row. */
31623 static void
31624 expose_overlaps (struct window *w,
31625 struct glyph_row *first_overlapping_row,
31626 struct glyph_row *last_overlapping_row,
31627 XRectangle *r)
31629 struct glyph_row *row;
31631 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31632 if (row->overlapping_p)
31634 eassert (row->enabled_p && !row->mode_line_p);
31636 row->clip = r;
31637 if (row->used[LEFT_MARGIN_AREA])
31638 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31640 if (row->used[TEXT_AREA])
31641 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31643 if (row->used[RIGHT_MARGIN_AREA])
31644 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31645 row->clip = NULL;
31650 /* Return true if W's cursor intersects rectangle R. */
31652 static bool
31653 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31655 XRectangle cr, result;
31656 struct glyph *cursor_glyph;
31657 struct glyph_row *row;
31659 if (w->phys_cursor.vpos >= 0
31660 && w->phys_cursor.vpos < w->current_matrix->nrows
31661 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31662 row->enabled_p)
31663 && row->cursor_in_fringe_p)
31665 /* Cursor is in the fringe. */
31666 cr.x = window_box_right_offset (w,
31667 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31668 ? RIGHT_MARGIN_AREA
31669 : TEXT_AREA));
31670 cr.y = row->y;
31671 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31672 cr.height = row->height;
31673 return x_intersect_rectangles (&cr, r, &result);
31676 cursor_glyph = get_phys_cursor_glyph (w);
31677 if (cursor_glyph)
31679 /* r is relative to W's box, but w->phys_cursor.x is relative
31680 to left edge of W's TEXT area. Adjust it. */
31681 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31682 cr.y = w->phys_cursor.y;
31683 cr.width = cursor_glyph->pixel_width;
31684 cr.height = w->phys_cursor_height;
31685 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31686 I assume the effect is the same -- and this is portable. */
31687 return x_intersect_rectangles (&cr, r, &result);
31689 /* If we don't understand the format, pretend we're not in the hot-spot. */
31690 return false;
31694 /* EXPORT:
31695 Draw a vertical window border to the right of window W if W doesn't
31696 have vertical scroll bars. */
31698 void
31699 x_draw_vertical_border (struct window *w)
31701 struct frame *f = XFRAME (WINDOW_FRAME (w));
31703 /* We could do better, if we knew what type of scroll-bar the adjacent
31704 windows (on either side) have... But we don't :-(
31705 However, I think this works ok. ++KFS 2003-04-25 */
31707 /* Redraw borders between horizontally adjacent windows. Don't
31708 do it for frames with vertical scroll bars because either the
31709 right scroll bar of a window, or the left scroll bar of its
31710 neighbor will suffice as a border. */
31711 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31712 return;
31714 /* Note: It is necessary to redraw both the left and the right
31715 borders, for when only this single window W is being
31716 redisplayed. */
31717 if (!WINDOW_RIGHTMOST_P (w)
31718 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31720 int x0, x1, y0, y1;
31722 window_box_edges (w, &x0, &y0, &x1, &y1);
31723 y1 -= 1;
31725 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31726 x1 -= 1;
31728 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31731 if (!WINDOW_LEFTMOST_P (w)
31732 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31734 int x0, x1, y0, y1;
31736 window_box_edges (w, &x0, &y0, &x1, &y1);
31737 y1 -= 1;
31739 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31740 x0 -= 1;
31742 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31747 /* Draw window dividers for window W. */
31749 void
31750 x_draw_right_divider (struct window *w)
31752 struct frame *f = WINDOW_XFRAME (w);
31754 if (w->mini || w->pseudo_window_p)
31755 return;
31756 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31758 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31759 int x1 = WINDOW_RIGHT_EDGE_X (w);
31760 int y0 = WINDOW_TOP_EDGE_Y (w);
31761 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31763 /* If W is horizontally combined and has a right sibling, don't
31764 draw over any bottom divider. */
31765 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31766 && !NILP (w->parent)
31767 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31768 && !NILP (w->next))
31769 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31771 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31775 static void
31776 x_draw_bottom_divider (struct window *w)
31778 struct frame *f = XFRAME (WINDOW_FRAME (w));
31780 if (w->mini || w->pseudo_window_p)
31781 return;
31782 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31784 int x0 = WINDOW_LEFT_EDGE_X (w);
31785 int x1 = WINDOW_RIGHT_EDGE_X (w);
31786 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31787 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31788 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31790 /* If W is vertically combined and has a sibling below, don't draw
31791 over any right divider. */
31792 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31793 && p
31794 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31795 && !NILP (w->next))
31796 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31797 && NILP (w->next)
31798 && !NILP (p->parent)
31799 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31800 && !NILP (XWINDOW (p->parent)->next))))
31801 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31803 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31807 /* Redraw the part of window W intersection rectangle FR. Pixel
31808 coordinates in FR are frame-relative. Call this function with
31809 input blocked. Value is true if the exposure overwrites
31810 mouse-face. */
31812 static bool
31813 expose_window (struct window *w, XRectangle *fr)
31815 struct frame *f = XFRAME (w->frame);
31816 XRectangle wr, r;
31817 bool mouse_face_overwritten_p = false;
31819 /* If window is not yet fully initialized, do nothing. This can
31820 happen when toolkit scroll bars are used and a window is split.
31821 Reconfiguring the scroll bar will generate an expose for a newly
31822 created window. */
31823 if (w->current_matrix == NULL)
31824 return false;
31826 /* When we're currently updating the window, display and current
31827 matrix usually don't agree. Arrange for a thorough display
31828 later. */
31829 if (w->must_be_updated_p)
31831 SET_FRAME_GARBAGED (f);
31832 return false;
31835 /* Frame-relative pixel rectangle of W. */
31836 wr.x = WINDOW_LEFT_EDGE_X (w);
31837 wr.y = WINDOW_TOP_EDGE_Y (w);
31838 wr.width = WINDOW_PIXEL_WIDTH (w);
31839 wr.height = WINDOW_PIXEL_HEIGHT (w);
31841 if (x_intersect_rectangles (fr, &wr, &r))
31843 int yb = window_text_bottom_y (w);
31844 struct glyph_row *row;
31845 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31847 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31848 r.x, r.y, r.width, r.height));
31850 /* Convert to window coordinates. */
31851 r.x -= WINDOW_LEFT_EDGE_X (w);
31852 r.y -= WINDOW_TOP_EDGE_Y (w);
31854 /* Turn off the cursor. */
31855 bool cursor_cleared_p = (!w->pseudo_window_p
31856 && phys_cursor_in_rect_p (w, &r));
31857 if (cursor_cleared_p)
31858 x_clear_cursor (w);
31860 /* If the row containing the cursor extends face to end of line,
31861 then expose_area might overwrite the cursor outside the
31862 rectangle and thus notice_overwritten_cursor might clear
31863 w->phys_cursor_on_p. We remember the original value and
31864 check later if it is changed. */
31865 bool phys_cursor_on_p = w->phys_cursor_on_p;
31867 /* Use a signed int intermediate value to avoid catastrophic
31868 failures due to comparison between signed and unsigned, when
31869 y0 or y1 is negative (can happen for tall images). */
31870 int r_bottom = r.y + r.height;
31872 /* Update lines intersecting rectangle R. */
31873 first_overlapping_row = last_overlapping_row = NULL;
31874 for (row = w->current_matrix->rows;
31875 row->enabled_p;
31876 ++row)
31878 int y0 = row->y;
31879 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31881 if ((y0 >= r.y && y0 < r_bottom)
31882 || (y1 > r.y && y1 < r_bottom)
31883 || (r.y >= y0 && r.y < y1)
31884 || (r_bottom > y0 && r_bottom < y1))
31886 /* A header line may be overlapping, but there is no need
31887 to fix overlapping areas for them. KFS 2005-02-12 */
31888 if (row->overlapping_p && !row->mode_line_p)
31890 if (first_overlapping_row == NULL)
31891 first_overlapping_row = row;
31892 last_overlapping_row = row;
31895 row->clip = fr;
31896 if (expose_line (w, row, &r))
31897 mouse_face_overwritten_p = true;
31898 row->clip = NULL;
31900 else if (row->overlapping_p)
31902 /* We must redraw a row overlapping the exposed area. */
31903 if (y0 < r.y
31904 ? y0 + row->phys_height > r.y
31905 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31907 if (first_overlapping_row == NULL)
31908 first_overlapping_row = row;
31909 last_overlapping_row = row;
31913 if (y1 >= yb)
31914 break;
31917 /* Display the mode line if there is one. */
31918 if (window_wants_mode_line (w)
31919 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31920 row->enabled_p)
31921 && row->y < r_bottom)
31923 if (expose_line (w, row, &r))
31924 mouse_face_overwritten_p = true;
31927 if (!w->pseudo_window_p)
31929 /* Fix the display of overlapping rows. */
31930 if (first_overlapping_row)
31931 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31932 fr);
31934 /* Draw border between windows. */
31935 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31936 x_draw_right_divider (w);
31937 else
31938 x_draw_vertical_border (w);
31940 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31941 x_draw_bottom_divider (w);
31943 /* Turn the cursor on again. */
31944 if (cursor_cleared_p
31945 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31946 update_window_cursor (w, true);
31950 return mouse_face_overwritten_p;
31955 /* Redraw (parts) of all windows in the window tree rooted at W that
31956 intersect R. R contains frame pixel coordinates. Value is
31957 true if the exposure overwrites mouse-face. */
31959 static bool
31960 expose_window_tree (struct window *w, XRectangle *r)
31962 struct frame *f = XFRAME (w->frame);
31963 bool mouse_face_overwritten_p = false;
31965 while (w && !FRAME_GARBAGED_P (f))
31967 mouse_face_overwritten_p
31968 |= (WINDOWP (w->contents)
31969 ? expose_window_tree (XWINDOW (w->contents), r)
31970 : expose_window (w, r));
31972 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31975 return mouse_face_overwritten_p;
31979 /* EXPORT:
31980 Redisplay an exposed area of frame F. X and Y are the upper-left
31981 corner of the exposed rectangle. W and H are width and height of
31982 the exposed area. All are pixel values. W or H zero means redraw
31983 the entire frame. */
31985 void
31986 expose_frame (struct frame *f, int x, int y, int w, int h)
31988 XRectangle r;
31989 bool mouse_face_overwritten_p = false;
31991 TRACE ((stderr, "expose_frame "));
31993 /* No need to redraw if frame will be redrawn soon. */
31994 if (FRAME_GARBAGED_P (f))
31996 TRACE ((stderr, " garbaged\n"));
31997 return;
32000 /* If basic faces haven't been realized yet, there is no point in
32001 trying to redraw anything. This can happen when we get an expose
32002 event while Emacs is starting, e.g. by moving another window. */
32003 if (FRAME_FACE_CACHE (f) == NULL
32004 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32006 TRACE ((stderr, " no faces\n"));
32007 return;
32010 if (w == 0 || h == 0)
32012 r.x = r.y = 0;
32013 r.width = FRAME_TEXT_WIDTH (f);
32014 r.height = FRAME_TEXT_HEIGHT (f);
32016 else
32018 r.x = x;
32019 r.y = y;
32020 r.width = w;
32021 r.height = h;
32024 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32025 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32027 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32028 if (WINDOWP (f->tool_bar_window))
32029 mouse_face_overwritten_p
32030 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32031 #endif
32033 #ifdef HAVE_X_WINDOWS
32034 #ifndef MSDOS
32035 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32036 if (WINDOWP (f->menu_bar_window))
32037 mouse_face_overwritten_p
32038 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32039 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32040 #endif
32041 #endif
32043 /* Some window managers support a focus-follows-mouse style with
32044 delayed raising of frames. Imagine a partially obscured frame,
32045 and moving the mouse into partially obscured mouse-face on that
32046 frame. The visible part of the mouse-face will be highlighted,
32047 then the WM raises the obscured frame. With at least one WM, KDE
32048 2.1, Emacs is not getting any event for the raising of the frame
32049 (even tried with SubstructureRedirectMask), only Expose events.
32050 These expose events will draw text normally, i.e. not
32051 highlighted. Which means we must redo the highlight here.
32052 Subsume it under ``we love X''. --gerd 2001-08-15 */
32053 /* Included in Windows version because Windows most likely does not
32054 do the right thing if any third party tool offers
32055 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32056 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32058 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32059 if (f == hlinfo->mouse_face_mouse_frame)
32061 int mouse_x = hlinfo->mouse_face_mouse_x;
32062 int mouse_y = hlinfo->mouse_face_mouse_y;
32063 clear_mouse_face (hlinfo);
32064 note_mouse_highlight (f, mouse_x, mouse_y);
32070 /* EXPORT:
32071 Determine the intersection of two rectangles R1 and R2. Return
32072 the intersection in *RESULT. Value is true if RESULT is not
32073 empty. */
32075 bool
32076 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32078 XRectangle *left, *right;
32079 XRectangle *upper, *lower;
32080 bool intersection_p = false;
32082 /* Rearrange so that R1 is the left-most rectangle. */
32083 if (r1->x < r2->x)
32084 left = r1, right = r2;
32085 else
32086 left = r2, right = r1;
32088 /* X0 of the intersection is right.x0, if this is inside R1,
32089 otherwise there is no intersection. */
32090 if (right->x <= left->x + left->width)
32092 result->x = right->x;
32094 /* The right end of the intersection is the minimum of
32095 the right ends of left and right. */
32096 result->width = (min (left->x + left->width, right->x + right->width)
32097 - result->x);
32099 /* Same game for Y. */
32100 if (r1->y < r2->y)
32101 upper = r1, lower = r2;
32102 else
32103 upper = r2, lower = r1;
32105 /* The upper end of the intersection is lower.y0, if this is inside
32106 of upper. Otherwise, there is no intersection. */
32107 if (lower->y <= upper->y + upper->height)
32109 result->y = lower->y;
32111 /* The lower end of the intersection is the minimum of the lower
32112 ends of upper and lower. */
32113 result->height = (min (lower->y + lower->height,
32114 upper->y + upper->height)
32115 - result->y);
32116 intersection_p = true;
32120 return intersection_p;
32123 #endif /* HAVE_WINDOW_SYSTEM */
32126 /***********************************************************************
32127 Initialization
32128 ***********************************************************************/
32130 void
32131 syms_of_xdisp (void)
32133 Vwith_echo_area_save_vector = Qnil;
32134 staticpro (&Vwith_echo_area_save_vector);
32136 Vmessage_stack = Qnil;
32137 staticpro (&Vmessage_stack);
32139 /* Non-nil means don't actually do any redisplay. */
32140 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32142 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32144 DEFVAR_BOOL("inhibit-message", inhibit_message,
32145 doc: /* Non-nil means calls to `message' are not displayed.
32146 They are still logged to the *Messages* buffer. */);
32147 inhibit_message = 0;
32149 message_dolog_marker1 = Fmake_marker ();
32150 staticpro (&message_dolog_marker1);
32151 message_dolog_marker2 = Fmake_marker ();
32152 staticpro (&message_dolog_marker2);
32153 message_dolog_marker3 = Fmake_marker ();
32154 staticpro (&message_dolog_marker3);
32156 defsubr (&Sset_buffer_redisplay);
32157 #ifdef GLYPH_DEBUG
32158 defsubr (&Sdump_frame_glyph_matrix);
32159 defsubr (&Sdump_glyph_matrix);
32160 defsubr (&Sdump_glyph_row);
32161 defsubr (&Sdump_tool_bar_row);
32162 defsubr (&Strace_redisplay);
32163 defsubr (&Strace_to_stderr);
32164 #endif
32165 #ifdef HAVE_WINDOW_SYSTEM
32166 defsubr (&Stool_bar_height);
32167 defsubr (&Slookup_image_map);
32168 #endif
32169 defsubr (&Sline_pixel_height);
32170 defsubr (&Sformat_mode_line);
32171 defsubr (&Sinvisible_p);
32172 defsubr (&Scurrent_bidi_paragraph_direction);
32173 defsubr (&Swindow_text_pixel_size);
32174 defsubr (&Smove_point_visually);
32175 defsubr (&Sbidi_find_overridden_directionality);
32177 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32178 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32179 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32180 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32181 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32182 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32183 DEFSYM (Qeval, "eval");
32184 DEFSYM (QCdata, ":data");
32186 /* Names of text properties relevant for redisplay. */
32187 DEFSYM (Qdisplay, "display");
32188 DEFSYM (Qspace_width, "space-width");
32189 DEFSYM (Qraise, "raise");
32190 DEFSYM (Qslice, "slice");
32191 DEFSYM (Qspace, "space");
32192 DEFSYM (Qmargin, "margin");
32193 DEFSYM (Qpointer, "pointer");
32194 DEFSYM (Qleft_margin, "left-margin");
32195 DEFSYM (Qright_margin, "right-margin");
32196 DEFSYM (Qcenter, "center");
32197 DEFSYM (Qline_height, "line-height");
32198 DEFSYM (QCalign_to, ":align-to");
32199 DEFSYM (QCrelative_width, ":relative-width");
32200 DEFSYM (QCrelative_height, ":relative-height");
32201 DEFSYM (QCeval, ":eval");
32202 DEFSYM (QCpropertize, ":propertize");
32203 DEFSYM (QCfile, ":file");
32204 DEFSYM (Qfontified, "fontified");
32205 DEFSYM (Qfontification_functions, "fontification-functions");
32207 /* Name of the face used to highlight trailing whitespace. */
32208 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32210 /* Names of the faces used to display line numbers. */
32211 DEFSYM (Qline_number, "line-number");
32212 DEFSYM (Qline_number_current_line, "line-number-current-line");
32213 /* Name of a text property which disables line-number display. */
32214 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32216 /* Name and number of the face used to highlight escape glyphs. */
32217 DEFSYM (Qescape_glyph, "escape-glyph");
32219 /* Name and number of the face used to highlight non-breaking
32220 spaces/hyphens. */
32221 DEFSYM (Qnobreak_space, "nobreak-space");
32222 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32224 /* The symbol 'image' which is the car of the lists used to represent
32225 images in Lisp. Also a tool bar style. */
32226 DEFSYM (Qimage, "image");
32228 /* Tool bar styles. */
32229 DEFSYM (Qtext, "text");
32230 DEFSYM (Qboth, "both");
32231 DEFSYM (Qboth_horiz, "both-horiz");
32232 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32234 /* The image map types. */
32235 DEFSYM (QCmap, ":map");
32236 DEFSYM (QCpointer, ":pointer");
32237 DEFSYM (Qrect, "rect");
32238 DEFSYM (Qcircle, "circle");
32239 DEFSYM (Qpoly, "poly");
32241 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32243 DEFSYM (Qgrow_only, "grow-only");
32244 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32245 DEFSYM (Qposition, "position");
32246 DEFSYM (Qbuffer_position, "buffer-position");
32247 DEFSYM (Qobject, "object");
32249 /* Cursor shapes. */
32250 DEFSYM (Qbar, "bar");
32251 DEFSYM (Qhbar, "hbar");
32252 DEFSYM (Qbox, "box");
32253 DEFSYM (Qhollow, "hollow");
32255 /* Pointer shapes. */
32256 DEFSYM (Qhand, "hand");
32257 DEFSYM (Qarrow, "arrow");
32258 /* also Qtext */
32260 DEFSYM (Qdragging, "dragging");
32262 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32264 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32265 staticpro (&list_of_error);
32267 /* Values of those variables at last redisplay are stored as
32268 properties on 'overlay-arrow-position' symbol. However, if
32269 Voverlay_arrow_position is a marker, last-arrow-position is its
32270 numerical position. */
32271 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32272 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32274 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32275 properties on a symbol in overlay-arrow-variable-list. */
32276 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32277 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32279 echo_buffer[0] = echo_buffer[1] = Qnil;
32280 staticpro (&echo_buffer[0]);
32281 staticpro (&echo_buffer[1]);
32283 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32284 staticpro (&echo_area_buffer[0]);
32285 staticpro (&echo_area_buffer[1]);
32287 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32288 staticpro (&Vmessages_buffer_name);
32290 mode_line_proptrans_alist = Qnil;
32291 staticpro (&mode_line_proptrans_alist);
32292 mode_line_string_list = Qnil;
32293 staticpro (&mode_line_string_list);
32294 mode_line_string_face = Qnil;
32295 staticpro (&mode_line_string_face);
32296 mode_line_string_face_prop = Qnil;
32297 staticpro (&mode_line_string_face_prop);
32298 Vmode_line_unwind_vector = Qnil;
32299 staticpro (&Vmode_line_unwind_vector);
32301 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32303 help_echo_string = Qnil;
32304 staticpro (&help_echo_string);
32305 help_echo_object = Qnil;
32306 staticpro (&help_echo_object);
32307 help_echo_window = Qnil;
32308 staticpro (&help_echo_window);
32309 previous_help_echo_string = Qnil;
32310 staticpro (&previous_help_echo_string);
32311 help_echo_pos = -1;
32313 DEFSYM (Qright_to_left, "right-to-left");
32314 DEFSYM (Qleft_to_right, "left-to-right");
32315 defsubr (&Sbidi_resolved_levels);
32317 #ifdef HAVE_WINDOW_SYSTEM
32318 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32319 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32320 For example, if a block cursor is over a tab, it will be drawn as
32321 wide as that tab on the display. */);
32322 x_stretch_cursor_p = 0;
32323 #endif
32325 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32326 doc: /* Non-nil means highlight trailing whitespace.
32327 The face used for trailing whitespace is `trailing-whitespace'. */);
32328 Vshow_trailing_whitespace = Qnil;
32330 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32331 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32332 If the value is t, Emacs highlights non-ASCII chars which have the
32333 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32334 or `nobreak-hyphen' face respectively.
32336 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32337 U+2011 (non-breaking hyphen) are affected.
32339 Any other non-nil value means to display these characters as a escape
32340 glyph followed by an ordinary space or hyphen.
32342 A value of nil means no special handling of these characters. */);
32343 Vnobreak_char_display = Qt;
32345 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32346 doc: /* The pointer shape to show in void text areas.
32347 A value of nil means to show the text pointer. Other options are
32348 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32349 `hourglass'. */);
32350 Vvoid_text_area_pointer = Qarrow;
32352 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32353 doc: /* Non-nil means don't actually do any redisplay.
32354 This is used for internal purposes. */);
32355 Vinhibit_redisplay = Qnil;
32357 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32358 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32359 Vglobal_mode_string = Qnil;
32361 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32362 doc: /* Marker for where to display an arrow on top of the buffer text.
32363 This must be the beginning of a line in order to work.
32364 See also `overlay-arrow-string'. */);
32365 Voverlay_arrow_position = Qnil;
32367 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32368 doc: /* String to display as an arrow in non-window frames.
32369 See also `overlay-arrow-position'. */);
32370 Voverlay_arrow_string = build_pure_c_string ("=>");
32372 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32373 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32374 The symbols on this list are examined during redisplay to determine
32375 where to display overlay arrows. */);
32376 Voverlay_arrow_variable_list
32377 = list1 (intern_c_string ("overlay-arrow-position"));
32379 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32380 doc: /* The number of lines to try scrolling a window by when point moves out.
32381 If that fails to bring point back on frame, point is centered instead.
32382 If this is zero, point is always centered after it moves off frame.
32383 If you want scrolling to always be a line at a time, you should set
32384 `scroll-conservatively' to a large value rather than set this to 1. */);
32386 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32387 doc: /* Scroll up to this many lines, to bring point back on screen.
32388 If point moves off-screen, redisplay will scroll by up to
32389 `scroll-conservatively' lines in order to bring point just barely
32390 onto the screen again. If that cannot be done, then redisplay
32391 recenters point as usual.
32393 If the value is greater than 100, redisplay will never recenter point,
32394 but will always scroll just enough text to bring point into view, even
32395 if you move far away.
32397 A value of zero means always recenter point if it moves off screen. */);
32398 scroll_conservatively = 0;
32400 DEFVAR_INT ("scroll-margin", scroll_margin,
32401 doc: /* Number of lines of margin at the top and bottom of a window.
32402 Recenter the window whenever point gets within this many lines
32403 of the top or bottom of the window. */);
32404 scroll_margin = 0;
32406 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32407 doc: /* Maximum effective value of `scroll-margin'.
32408 Given as a fraction of the current window's lines. The value should
32409 be a floating point number between 0.0 and 0.5. The effective maximum
32410 is limited to (/ (1- window-lines) 2). Non-float values for this
32411 variable are ignored and the default 0.25 is used instead. */);
32412 Vmaximum_scroll_margin = make_float (0.25);
32414 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32415 doc: /* Pixels per inch value for non-window system displays.
32416 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32417 Vdisplay_pixels_per_inch = make_float (72.0);
32419 #ifdef GLYPH_DEBUG
32420 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32421 #endif
32423 DEFVAR_LISP ("truncate-partial-width-windows",
32424 Vtruncate_partial_width_windows,
32425 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32426 For an integer value, truncate lines in each window narrower than the
32427 full frame width, provided the total window width in column units is less
32428 than that integer; otherwise, respect the value of `truncate-lines'.
32429 The total width of the window is as returned by `window-total-width', it
32430 includes the fringes, the continuation and truncation glyphs, the
32431 display margins (if any), and the scroll bar
32433 For any other non-nil value, truncate lines in all windows that do
32434 not span the full frame width.
32436 A value of nil means to respect the value of `truncate-lines'.
32438 If `word-wrap' is enabled, you might want to reduce this. */);
32439 Vtruncate_partial_width_windows = make_number (50);
32441 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32442 doc: /* Maximum buffer size for which line number should be displayed.
32443 If the buffer is bigger than this, the line number does not appear
32444 in the mode line. A value of nil means no limit. */);
32445 Vline_number_display_limit = Qnil;
32447 DEFVAR_INT ("line-number-display-limit-width",
32448 line_number_display_limit_width,
32449 doc: /* Maximum line width (in characters) for line number display.
32450 If the average length of the lines near point is bigger than this, then the
32451 line number may be omitted from the mode line. */);
32452 line_number_display_limit_width = 200;
32454 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32455 doc: /* Non-nil means highlight region even in nonselected windows. */);
32456 highlight_nonselected_windows = false;
32458 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32459 doc: /* Non-nil if more than one frame is visible on this display.
32460 Minibuffer-only frames don't count, but iconified frames do.
32461 This variable is not guaranteed to be accurate except while processing
32462 `frame-title-format' and `icon-title-format'. */);
32464 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32465 doc: /* Template for displaying the title bar of visible frames.
32466 \(Assuming the window manager supports this feature.)
32468 This variable has the same structure as `mode-line-format', except that
32469 the %c, %C, and %l constructs are ignored. It is used only on frames for
32470 which no explicit name has been set (see `modify-frame-parameters'). */);
32472 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32473 doc: /* Template for displaying the title bar of an iconified frame.
32474 \(Assuming the window manager supports this feature.)
32475 This variable has the same structure as `mode-line-format' (which see),
32476 and is used only on frames for which no explicit name has been set
32477 \(see `modify-frame-parameters'). */);
32478 Vicon_title_format
32479 = Vframe_title_format
32480 = listn (CONSTYPE_PURE, 3,
32481 intern_c_string ("multiple-frames"),
32482 build_pure_c_string ("%b"),
32483 listn (CONSTYPE_PURE, 4,
32484 empty_unibyte_string,
32485 intern_c_string ("invocation-name"),
32486 build_pure_c_string ("@"),
32487 intern_c_string ("system-name")));
32489 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32490 doc: /* Maximum number of lines to keep in the message log buffer.
32491 If nil, disable message logging. If t, log messages but don't truncate
32492 the buffer when it becomes large. */);
32493 Vmessage_log_max = make_number (1000);
32495 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32496 doc: /* List of functions to call before redisplaying a window with scrolling.
32497 Each function is called with two arguments, the window and its new
32498 display-start position.
32499 These functions are called whenever the `window-start' marker is modified,
32500 either to point into another buffer (e.g. via `set-window-buffer') or another
32501 place in the same buffer.
32502 Note that the value of `window-end' is not valid when these functions are
32503 called.
32505 Warning: Do not use this feature to alter the way the window
32506 is scrolled. It is not designed for that, and such use probably won't
32507 work. */);
32508 Vwindow_scroll_functions = Qnil;
32510 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32511 doc: /* Functions called when redisplay of a window reaches the end trigger.
32512 Each function is called with two arguments, the window and the end trigger value.
32513 See `set-window-redisplay-end-trigger'. */);
32514 Vredisplay_end_trigger_functions = Qnil;
32516 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32517 doc: /* Non-nil means autoselect window with mouse pointer.
32518 If nil, do not autoselect windows.
32519 A positive number means delay autoselection by that many seconds: a
32520 window is autoselected only after the mouse has remained in that
32521 window for the duration of the delay.
32522 A negative number has a similar effect, but causes windows to be
32523 autoselected only after the mouse has stopped moving. (Because of
32524 the way Emacs compares mouse events, you will occasionally wait twice
32525 that time before the window gets selected.)
32526 Any other value means to autoselect window instantaneously when the
32527 mouse pointer enters it.
32529 Autoselection selects the minibuffer only if it is active, and never
32530 unselects the minibuffer if it is active.
32532 When customizing this variable make sure that the actual value of
32533 `focus-follows-mouse' matches the behavior of your window manager. */);
32534 Vmouse_autoselect_window = Qnil;
32536 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32537 doc: /* Non-nil means automatically resize tool-bars.
32538 This dynamically changes the tool-bar's height to the minimum height
32539 that is needed to make all tool-bar items visible.
32540 If value is `grow-only', the tool-bar's height is only increased
32541 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32542 Vauto_resize_tool_bars = Qt;
32544 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32545 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32546 auto_raise_tool_bar_buttons_p = true;
32548 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32549 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32550 make_cursor_line_fully_visible_p = true;
32552 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32553 doc: /* Border below tool-bar in pixels.
32554 If an integer, use it as the height of the border.
32555 If it is one of `internal-border-width' or `border-width', use the
32556 value of the corresponding frame parameter.
32557 Otherwise, no border is added below the tool-bar. */);
32558 Vtool_bar_border = Qinternal_border_width;
32560 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32561 doc: /* Margin around tool-bar buttons in pixels.
32562 If an integer, use that for both horizontal and vertical margins.
32563 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32564 HORZ specifying the horizontal margin, and VERT specifying the
32565 vertical margin. */);
32566 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32568 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32569 doc: /* Relief thickness of tool-bar buttons. */);
32570 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32572 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32573 doc: /* Tool bar style to use.
32574 It can be one of
32575 image - show images only
32576 text - show text only
32577 both - show both, text below image
32578 both-horiz - show text to the right of the image
32579 text-image-horiz - show text to the left of the image
32580 any other - use system default or image if no system default.
32582 This variable only affects the GTK+ toolkit version of Emacs. */);
32583 Vtool_bar_style = Qnil;
32585 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32586 doc: /* Maximum number of characters a label can have to be shown.
32587 The tool bar style must also show labels for this to have any effect, see
32588 `tool-bar-style'. */);
32589 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32591 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32592 doc: /* List of functions to call to fontify regions of text.
32593 Each function is called with one argument POS. Functions must
32594 fontify a region starting at POS in the current buffer, and give
32595 fontified regions the property `fontified'. */);
32596 Vfontification_functions = Qnil;
32597 Fmake_variable_buffer_local (Qfontification_functions);
32599 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32600 unibyte_display_via_language_environment,
32601 doc: /* Non-nil means display unibyte text according to language environment.
32602 Specifically, this means that raw bytes in the range 160-255 decimal
32603 are displayed by converting them to the equivalent multibyte characters
32604 according to the current language environment. As a result, they are
32605 displayed according to the current fontset.
32607 Note that this variable affects only how these bytes are displayed,
32608 but does not change the fact they are interpreted as raw bytes. */);
32609 unibyte_display_via_language_environment = false;
32611 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32612 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32613 If a float, it specifies a fraction of the mini-window frame's height.
32614 If an integer, it specifies a number of lines. */);
32615 Vmax_mini_window_height = make_float (0.25);
32617 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32618 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32619 A value of nil means don't automatically resize mini-windows.
32620 A value of t means resize them to fit the text displayed in them.
32621 A value of `grow-only', the default, means let mini-windows grow only;
32622 they return to their normal size when the minibuffer is closed, or the
32623 echo area becomes empty. */);
32624 /* Contrary to the doc string, we initialize this to nil, so that
32625 loading loadup.el won't try to resize windows before loading
32626 window.el, where some functions we need to call for this live.
32627 We assign the 'grow-only' value right after loading window.el
32628 during loadup. */
32629 Vresize_mini_windows = Qnil;
32631 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32632 doc: /* Alist specifying how to blink the cursor off.
32633 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32634 `cursor-type' frame-parameter or variable equals ON-STATE,
32635 comparing using `equal', Emacs uses OFF-STATE to specify
32636 how to blink it off. ON-STATE and OFF-STATE are values for
32637 the `cursor-type' frame parameter.
32639 If a frame's ON-STATE has no entry in this list,
32640 the frame's other specifications determine how to blink the cursor off. */);
32641 Vblink_cursor_alist = Qnil;
32643 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32644 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32645 The value `current-line' means the line displaying point in each window
32646 is automatically scrolled horizontally to make point visible.
32647 Any other non-nil value means all the lines in a window are automatically
32648 scrolled horizontally to make point visible. */);
32649 automatic_hscrolling = Qt;
32650 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32651 DEFSYM (Qcurrent_line, "current-line");
32653 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32654 doc: /* How many columns away from the window edge point is allowed to get
32655 before automatic hscrolling will horizontally scroll the window. */);
32656 hscroll_margin = 5;
32658 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32659 doc: /* How many columns to scroll the window when point gets too close to the edge.
32660 When point is less than `hscroll-margin' columns from the window
32661 edge, automatic hscrolling will scroll the window by the amount of columns
32662 determined by this variable. If its value is a positive integer, scroll that
32663 many columns. If it's a positive floating-point number, it specifies the
32664 fraction of the window's width to scroll. If it's nil or zero, point will be
32665 centered horizontally after the scroll. Any other value, including negative
32666 numbers, are treated as if the value were zero.
32668 Automatic hscrolling always moves point outside the scroll margin, so if
32669 point was more than scroll step columns inside the margin, the window will
32670 scroll more than the value given by the scroll step.
32672 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32673 and `scroll-right' overrides this variable's effect. */);
32674 Vhscroll_step = make_number (0);
32676 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32677 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32678 Bind this around calls to `message' to let it take effect. */);
32679 message_truncate_lines = false;
32681 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32682 doc: /* Normal hook run to update the menu bar definitions.
32683 Redisplay runs this hook before it redisplays the menu bar.
32684 This is used to update menus such as Buffers, whose contents depend on
32685 various data. */);
32686 Vmenu_bar_update_hook = Qnil;
32688 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32689 doc: /* Frame for which we are updating a menu.
32690 The enable predicate for a menu binding should check this variable. */);
32691 Vmenu_updating_frame = Qnil;
32693 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32694 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32695 inhibit_menubar_update = false;
32697 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32698 doc: /* Prefix prepended to all continuation lines at display time.
32699 The value may be a string, an image, or a stretch-glyph; it is
32700 interpreted in the same way as the value of a `display' text property.
32702 This variable is overridden by any `wrap-prefix' text or overlay
32703 property.
32705 To add a prefix to non-continuation lines, use `line-prefix'. */);
32706 Vwrap_prefix = Qnil;
32707 DEFSYM (Qwrap_prefix, "wrap-prefix");
32708 Fmake_variable_buffer_local (Qwrap_prefix);
32710 DEFVAR_LISP ("line-prefix", Vline_prefix,
32711 doc: /* Prefix prepended to all non-continuation lines at display time.
32712 The value may be a string, an image, or a stretch-glyph; it is
32713 interpreted in the same way as the value of a `display' text property.
32715 This variable is overridden by any `line-prefix' text or overlay
32716 property.
32718 To add a prefix to continuation lines, use `wrap-prefix'. */);
32719 Vline_prefix = Qnil;
32720 DEFSYM (Qline_prefix, "line-prefix");
32721 Fmake_variable_buffer_local (Qline_prefix);
32723 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32724 doc: /* Non-nil means display line numbers.
32725 If the value is t, display the absolute number of each line of a buffer
32726 shown in a window. Absolute line numbers count from the beginning of
32727 the current narrowing, or from buffer beginning. If the value is
32728 `relative', display for each line not containing the window's point its
32729 relative number instead, i.e. the number of the line relative to the
32730 line showing the window's point.
32732 In either case, line numbers are displayed at the beginning of each
32733 non-continuation line that displays buffer text, i.e. after each newline
32734 character that comes from the buffer. The value `visual' is like
32735 `relative' but counts screen lines instead of buffer lines. In practice
32736 this means that continuation lines count as well when calculating the
32737 relative number of a line.
32739 Lisp programs can disable display of a line number of a particular
32740 buffer line by putting the `display-line-numbers-disable' text property
32741 or overlay property on the first visible character of that line. */);
32742 Vdisplay_line_numbers = Qnil;
32743 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32744 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32745 DEFSYM (Qrelative, "relative");
32746 DEFSYM (Qvisual, "visual");
32748 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32749 doc: /* Minimum width of space reserved for line number display.
32750 A positive number means reserve that many columns for line numbers,
32751 even if the actual number needs less space.
32752 The default value of nil means compute the space dynamically.
32753 Any other value is treated as nil. */);
32754 Vdisplay_line_numbers_width = Qnil;
32755 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32756 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32758 DEFVAR_LISP ("display-line-numbers-current-absolute",
32759 Vdisplay_line_numbers_current_absolute,
32760 doc: /* Non-nil means display absolute number of current line.
32761 This variable has effect only when `display-line-numbers' is
32762 either `relative' or `visual'. */);
32763 Vdisplay_line_numbers_current_absolute = Qt;
32765 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32766 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32767 display_line_numbers_widen = false;
32768 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32769 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32771 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32772 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32773 inhibit_eval_during_redisplay = false;
32775 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32776 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32777 inhibit_free_realized_faces = false;
32779 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32780 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32781 Intended for use during debugging and for testing bidi display;
32782 see biditest.el in the test suite. */);
32783 inhibit_bidi_mirroring = false;
32785 #ifdef GLYPH_DEBUG
32786 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32787 doc: /* Inhibit try_window_id display optimization. */);
32788 inhibit_try_window_id = false;
32790 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32791 doc: /* Inhibit try_window_reusing display optimization. */);
32792 inhibit_try_window_reusing = false;
32794 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32795 doc: /* Inhibit try_cursor_movement display optimization. */);
32796 inhibit_try_cursor_movement = false;
32797 #endif /* GLYPH_DEBUG */
32799 DEFVAR_INT ("overline-margin", overline_margin,
32800 doc: /* Space between overline and text, in pixels.
32801 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32802 margin to the character height. */);
32803 overline_margin = 2;
32805 DEFVAR_INT ("underline-minimum-offset",
32806 underline_minimum_offset,
32807 doc: /* Minimum distance between baseline and underline.
32808 This can improve legibility of underlined text at small font sizes,
32809 particularly when using variable `x-use-underline-position-properties'
32810 with fonts that specify an UNDERLINE_POSITION relatively close to the
32811 baseline. The default value is 1. */);
32812 underline_minimum_offset = 1;
32814 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32815 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32816 This feature only works when on a window system that can change
32817 cursor shapes. */);
32818 display_hourglass_p = true;
32820 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32821 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32822 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32824 #ifdef HAVE_WINDOW_SYSTEM
32825 hourglass_atimer = NULL;
32826 hourglass_shown_p = false;
32827 #endif /* HAVE_WINDOW_SYSTEM */
32829 /* Name of the face used to display glyphless characters. */
32830 DEFSYM (Qglyphless_char, "glyphless-char");
32832 /* Method symbols for Vglyphless_char_display. */
32833 DEFSYM (Qhex_code, "hex-code");
32834 DEFSYM (Qempty_box, "empty-box");
32835 DEFSYM (Qthin_space, "thin-space");
32836 DEFSYM (Qzero_width, "zero-width");
32838 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32839 doc: /* Function run just before redisplay.
32840 It is called with one argument, which is the set of windows that are to
32841 be redisplayed. This set can be nil (meaning, only the selected window),
32842 or t (meaning all windows). */);
32843 Vpre_redisplay_function = intern ("ignore");
32845 /* Symbol for the purpose of Vglyphless_char_display. */
32846 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32847 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32849 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32850 doc: /* Char-table defining glyphless characters.
32851 Each element, if non-nil, should be one of the following:
32852 an ASCII acronym string: display this string in a box
32853 `hex-code': display the hexadecimal code of a character in a box
32854 `empty-box': display as an empty box
32855 `thin-space': display as 1-pixel width space
32856 `zero-width': don't display
32857 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32858 display method for graphical terminals and text terminals respectively.
32859 GRAPHICAL and TEXT should each have one of the values listed above.
32861 The char-table has one extra slot to control the display of a character for
32862 which no font is found. This slot only takes effect on graphical terminals.
32863 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32864 `thin-space'. The default is `empty-box'.
32866 If a character has a non-nil entry in an active display table, the
32867 display table takes effect; in this case, Emacs does not consult
32868 `glyphless-char-display' at all. */);
32869 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32870 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32871 Qempty_box);
32873 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32874 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32875 Vdebug_on_message = Qnil;
32877 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32878 doc: /* */);
32879 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32881 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32882 doc: /* */);
32883 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32885 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32886 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32887 /* Initialize to t, since we need to disable reordering until
32888 loadup.el successfully loads charprop.el. */
32889 redisplay__inhibit_bidi = true;
32891 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32892 doc: /* Non-nil means display raw bytes in hexadecimal format.
32893 The default is to use octal format (\200) whereas hexadecimal (\x80)
32894 may be more familiar to users. */);
32895 display_raw_bytes_as_hex = false;
32900 /* Initialize this module when Emacs starts. */
32902 void
32903 init_xdisp (void)
32905 CHARPOS (this_line_start_pos) = 0;
32907 if (!noninteractive)
32909 struct window *m = XWINDOW (minibuf_window);
32910 Lisp_Object frame = m->frame;
32911 struct frame *f = XFRAME (frame);
32912 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32913 struct window *r = XWINDOW (root);
32914 int i;
32916 echo_area_window = minibuf_window;
32918 r->top_line = FRAME_TOP_MARGIN (f);
32919 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32920 r->total_cols = FRAME_COLS (f);
32921 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32922 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32923 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32925 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32926 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32927 m->total_cols = FRAME_COLS (f);
32928 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32929 m->total_lines = 1;
32930 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32932 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32933 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32934 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32936 /* The default ellipsis glyphs `...'. */
32937 for (i = 0; i < 3; ++i)
32938 default_invis_vector[i] = make_number ('.');
32942 /* Allocate the buffer for frame titles.
32943 Also used for `format-mode-line'. */
32944 int size = 100;
32945 mode_line_noprop_buf = xmalloc (size);
32946 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32947 mode_line_noprop_ptr = mode_line_noprop_buf;
32948 mode_line_target = MODE_LINE_DISPLAY;
32951 help_echo_showing_p = false;
32954 #ifdef HAVE_WINDOW_SYSTEM
32956 /* Platform-independent portion of hourglass implementation. */
32958 /* Timer function of hourglass_atimer. */
32960 static void
32961 show_hourglass (struct atimer *timer)
32963 /* The timer implementation will cancel this timer automatically
32964 after this function has run. Set hourglass_atimer to null
32965 so that we know the timer doesn't have to be canceled. */
32966 hourglass_atimer = NULL;
32968 if (!hourglass_shown_p)
32970 Lisp_Object tail, frame;
32972 block_input ();
32974 FOR_EACH_FRAME (tail, frame)
32976 struct frame *f = XFRAME (frame);
32978 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32979 && FRAME_RIF (f)->show_hourglass)
32980 FRAME_RIF (f)->show_hourglass (f);
32983 hourglass_shown_p = true;
32984 unblock_input ();
32988 /* Cancel a currently active hourglass timer, and start a new one. */
32990 void
32991 start_hourglass (void)
32993 struct timespec delay;
32995 cancel_hourglass ();
32997 if (INTEGERP (Vhourglass_delay)
32998 && XINT (Vhourglass_delay) > 0)
32999 delay = make_timespec (min (XINT (Vhourglass_delay),
33000 TYPE_MAXIMUM (time_t)),
33002 else if (FLOATP (Vhourglass_delay)
33003 && XFLOAT_DATA (Vhourglass_delay) > 0)
33004 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33005 else
33006 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33008 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33009 show_hourglass, NULL);
33012 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33013 shown. */
33015 void
33016 cancel_hourglass (void)
33018 if (hourglass_atimer)
33020 cancel_atimer (hourglass_atimer);
33021 hourglass_atimer = NULL;
33024 if (hourglass_shown_p)
33026 Lisp_Object tail, frame;
33028 block_input ();
33030 FOR_EACH_FRAME (tail, frame)
33032 struct frame *f = XFRAME (frame);
33034 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33035 && FRAME_RIF (f)->hide_hourglass)
33036 FRAME_RIF (f)->hide_hourglass (f);
33037 #ifdef HAVE_NTGUI
33038 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33039 else if (!FRAME_W32_P (f))
33040 w32_arrow_cursor ();
33041 #endif
33044 hourglass_shown_p = false;
33045 unblock_input ();
33049 #endif /* HAVE_WINDOW_SYSTEM */