Prevent infloops in redisplay due to truncate-lines and overlays
[emacs.git] / src / xdisp.c
blob5de5ecaef0d50247b2330956d52f48b29cc3ffa4
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
626 Sset_buffer_redisplay, 4, 4, 0,
627 doc: /* Mark the current buffer for redisplay.
628 This function may be passed to `add-variable-watcher'. */)
629 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
633 return Qnil;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void block_buffer_flips (void);
822 static void unblock_buffer_flips (void);
823 static void redisplay_windows (Lisp_Object);
824 static void redisplay_window (Lisp_Object, bool);
825 static Lisp_Object redisplay_window_error (Lisp_Object);
826 static Lisp_Object redisplay_window_0 (Lisp_Object);
827 static Lisp_Object redisplay_window_1 (Lisp_Object);
828 static bool set_cursor_from_row (struct window *, struct glyph_row *,
829 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
830 int, int);
831 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
832 static bool update_menu_bar (struct frame *, bool, bool);
833 static bool try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static bool display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
839 Lisp_Object, bool);
840 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
841 Lisp_Object);
842 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
843 static void display_menu_bar (struct window *);
844 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
845 ptrdiff_t *);
846 static int display_string (const char *, Lisp_Object, Lisp_Object,
847 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
848 static void compute_line_metrics (struct it *);
849 static void run_redisplay_end_trigger_hook (struct it *);
850 static bool get_overlay_strings (struct it *, ptrdiff_t);
851 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
852 static void next_overlay_string (struct it *);
853 static void reseat (struct it *, struct text_pos, bool);
854 static void reseat_1 (struct it *, struct text_pos, bool);
855 static bool next_element_from_display_vector (struct it *);
856 static bool next_element_from_string (struct it *);
857 static bool next_element_from_c_string (struct it *);
858 static bool next_element_from_buffer (struct it *);
859 static bool next_element_from_composition (struct it *);
860 static bool next_element_from_image (struct it *);
861 static bool next_element_from_stretch (struct it *);
862 static bool next_element_from_xwidget (struct it *);
863 static void load_overlay_strings (struct it *, ptrdiff_t);
864 static bool get_next_display_element (struct it *);
865 static enum move_it_result
866 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
867 enum move_operation_enum);
868 static void get_visually_first_element (struct it *);
869 static void compute_stop_pos (struct it *);
870 static int face_before_or_after_it_pos (struct it *, bool);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, bool);
877 static int underlying_face_id (struct it *);
879 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
880 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
882 #ifdef HAVE_WINDOW_SYSTEM
884 static void update_tool_bar (struct frame *, bool);
885 static void x_draw_bottom_divider (struct window *w);
886 static void notice_overwritten_cursor (struct window *,
887 enum glyph_row_area,
888 int, int, int, int);
889 static int normal_char_height (struct font *, int);
890 static void normal_char_ascent_descent (struct font *, int, int *, int *);
892 static void append_stretch_glyph (struct it *, Lisp_Object,
893 int, int, int);
895 static Lisp_Object get_it_property (struct it *, Lisp_Object);
896 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
897 struct font *, int, bool);
899 #endif /* HAVE_WINDOW_SYSTEM */
901 static void produce_special_glyphs (struct it *, enum display_element_type);
902 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
903 static bool coords_in_mouse_face_p (struct window *, int, int);
907 /***********************************************************************
908 Window display dimensions
909 ***********************************************************************/
911 /* Return the bottom boundary y-position for text lines in window W.
912 This is the first y position at which a line cannot start.
913 It is relative to the top of the window.
915 This is the height of W minus the height of a mode line, if any. */
918 window_text_bottom_y (struct window *w)
920 int height = WINDOW_PIXEL_HEIGHT (w);
922 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
927 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
929 return height;
932 /* Return the pixel width of display area AREA of window W.
933 ANY_AREA means return the total width of W, not including
934 fringes to the left and right of the window. */
937 window_box_width (struct window *w, enum glyph_row_area area)
939 int width = w->pixel_width;
941 if (!w->pseudo_window_p)
943 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
944 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
946 if (area == TEXT_AREA)
947 width -= (WINDOW_MARGINS_WIDTH (w)
948 + WINDOW_FRINGES_WIDTH (w));
949 else if (area == LEFT_MARGIN_AREA)
950 width = WINDOW_LEFT_MARGIN_WIDTH (w);
951 else if (area == RIGHT_MARGIN_AREA)
952 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
955 /* With wide margins, fringes, etc. we might end up with a negative
956 width, correct that here. */
957 return max (0, width);
961 /* Return the pixel height of the display area of window W, not
962 including mode lines of W, if any. */
965 window_box_height (struct window *w)
967 struct frame *f = XFRAME (w->frame);
968 int height = WINDOW_PIXEL_HEIGHT (w);
970 eassert (height >= 0);
972 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
973 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
975 /* Note: the code below that determines the mode-line/header-line
976 height is essentially the same as that contained in the macro
977 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
978 the appropriate glyph row has its `mode_line_p' flag set,
979 and if it doesn't, uses estimate_mode_line_height instead. */
981 if (WINDOW_WANTS_MODELINE_P (w))
983 struct glyph_row *ml_row
984 = (w->current_matrix && w->current_matrix->rows
985 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
986 : 0);
987 if (ml_row && ml_row->mode_line_p)
988 height -= ml_row->height;
989 else
990 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
993 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 struct glyph_row *hl_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
998 : 0);
999 if (hl_row && hl_row->mode_line_p)
1000 height -= hl_row->height;
1001 else
1002 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1005 /* With a very small font and a mode-line that's taller than
1006 default, we might end up with a negative height. */
1007 return max (0, height);
1010 /* Return the window-relative coordinate of the left edge of display
1011 area AREA of window W. ANY_AREA means return the left edge of the
1012 whole window, to the right of the left fringe of W. */
1015 window_box_left_offset (struct window *w, enum glyph_row_area area)
1017 int x;
1019 if (w->pseudo_window_p)
1020 return 0;
1022 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1024 if (area == TEXT_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA));
1027 else if (area == RIGHT_MARGIN_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA)
1030 + window_box_width (w, TEXT_AREA)
1031 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1033 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1034 else if (area == LEFT_MARGIN_AREA
1035 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1038 /* Don't return more than the window's pixel width. */
1039 return min (x, w->pixel_width);
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. ANY_AREA means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1047 static int
1048 window_box_right_offset (struct window *w, enum glyph_row_area area)
1050 /* Don't return more than the window's pixel width. */
1051 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1052 w->pixel_width);
1055 /* Return the frame-relative coordinate of the left edge of display
1056 area AREA of window W. ANY_AREA means return the left edge of the
1057 whole window, to the right of the left fringe of W. */
1060 window_box_left (struct window *w, enum glyph_row_area area)
1062 struct frame *f = XFRAME (w->frame);
1063 int x;
1065 if (w->pseudo_window_p)
1066 return FRAME_INTERNAL_BORDER_WIDTH (f);
1068 x = (WINDOW_LEFT_EDGE_X (w)
1069 + window_box_left_offset (w, area));
1071 return x;
1075 /* Return the frame-relative coordinate of the right edge of display
1076 area AREA of window W. ANY_AREA means return the right edge of the
1077 whole window, to the left of the right fringe of W. */
1080 window_box_right (struct window *w, enum glyph_row_area area)
1082 return window_box_left (w, area) + window_box_width (w, area);
1085 /* Get the bounding box of the display area AREA of window W, without
1086 mode lines, in frame-relative coordinates. ANY_AREA means the
1087 whole window, not including the left and right fringes of
1088 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1089 coordinates of the upper-left corner of the box. Return in
1090 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1092 void
1093 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1094 int *box_y, int *box_width, int *box_height)
1096 if (box_width)
1097 *box_width = window_box_width (w, area);
1098 if (box_height)
1099 *box_height = window_box_height (w);
1100 if (box_x)
1101 *box_x = window_box_left (w, area);
1102 if (box_y)
1104 *box_y = WINDOW_TOP_EDGE_Y (w);
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1110 #ifdef HAVE_WINDOW_SYSTEM
1112 /* Get the bounding box of the display area AREA of window W, without
1113 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1114 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1115 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1116 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1117 box. */
1119 static void
1120 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1121 int *bottom_right_x, int *bottom_right_y)
1123 window_box (w, ANY_AREA, top_left_x, top_left_y,
1124 bottom_right_x, bottom_right_y);
1125 *bottom_right_x += *top_left_x;
1126 *bottom_right_y += *top_left_y;
1129 #endif /* HAVE_WINDOW_SYSTEM */
1131 /***********************************************************************
1132 Utilities
1133 ***********************************************************************/
1135 /* Return the bottom y-position of the line the iterator IT is in.
1136 This can modify IT's settings. */
1139 line_bottom_y (struct it *it)
1141 int line_height = it->max_ascent + it->max_descent;
1142 int line_top_y = it->current_y;
1144 if (line_height == 0)
1146 if (last_height)
1147 line_height = last_height;
1148 else if (IT_CHARPOS (*it) < ZV)
1150 move_it_by_lines (it, 1);
1151 line_height = (it->max_ascent || it->max_descent
1152 ? it->max_ascent + it->max_descent
1153 : last_height);
1155 else
1157 struct glyph_row *row = it->glyph_row;
1159 /* Use the default character height. */
1160 it->glyph_row = NULL;
1161 it->what = IT_CHARACTER;
1162 it->c = ' ';
1163 it->len = 1;
1164 PRODUCE_GLYPHS (it);
1165 line_height = it->ascent + it->descent;
1166 it->glyph_row = row;
1170 return line_top_y + line_height;
1173 DEFUN ("line-pixel-height", Fline_pixel_height,
1174 Sline_pixel_height, 0, 0, 0,
1175 doc: /* Return height in pixels of text line in the selected window.
1177 Value is the height in pixels of the line at point. */)
1178 (void)
1180 struct it it;
1181 struct text_pos pt;
1182 struct window *w = XWINDOW (selected_window);
1183 struct buffer *old_buffer = NULL;
1184 Lisp_Object result;
1186 if (XBUFFER (w->contents) != current_buffer)
1188 old_buffer = current_buffer;
1189 set_buffer_internal_1 (XBUFFER (w->contents));
1191 SET_TEXT_POS (pt, PT, PT_BYTE);
1192 start_display (&it, w, pt);
1193 it.vpos = it.current_y = 0;
1194 last_height = 0;
1195 result = make_number (line_bottom_y (&it));
1196 if (old_buffer)
1197 set_buffer_internal_1 (old_buffer);
1199 return result;
1202 /* Return the default pixel height of text lines in window W. The
1203 value is the canonical height of the W frame's default font, plus
1204 any extra space required by the line-spacing variable or frame
1205 parameter.
1207 Implementation note: this ignores any line-spacing text properties
1208 put on the newline characters. This is because those properties
1209 only affect the _screen_ line ending in the newline (i.e., in a
1210 continued line, only the last screen line will be affected), which
1211 means only a small number of lines in a buffer can ever use this
1212 feature. Since this function is used to compute the default pixel
1213 equivalent of text lines in a window, we can safely ignore those
1214 few lines. For the same reasons, we ignore the line-height
1215 properties. */
1217 default_line_pixel_height (struct window *w)
1219 struct frame *f = WINDOW_XFRAME (w);
1220 int height = FRAME_LINE_HEIGHT (f);
1222 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1224 struct buffer *b = XBUFFER (w->contents);
1225 Lisp_Object val = BVAR (b, extra_line_spacing);
1227 if (NILP (val))
1228 val = BVAR (&buffer_defaults, extra_line_spacing);
1229 if (!NILP (val))
1231 if (RANGED_INTEGERP (0, val, INT_MAX))
1232 height += XFASTINT (val);
1233 else if (FLOATP (val))
1235 int addon = XFLOAT_DATA (val) * height + 0.5;
1237 if (addon >= 0)
1238 height += addon;
1241 else
1242 height += f->extra_line_spacing;
1245 return height;
1248 /* Subroutine of pos_visible_p below. Extracts a display string, if
1249 any, from the display spec given as its argument. */
1250 static Lisp_Object
1251 string_from_display_spec (Lisp_Object spec)
1253 if (CONSP (spec))
1255 while (CONSP (spec))
1257 if (STRINGP (XCAR (spec)))
1258 return XCAR (spec);
1259 spec = XCDR (spec);
1262 else if (VECTORP (spec))
1264 ptrdiff_t i;
1266 for (i = 0; i < ASIZE (spec); i++)
1268 if (STRINGP (AREF (spec, i)))
1269 return AREF (spec, i);
1271 return Qnil;
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 /* Compute exact mode line heights. */
1334 if (WINDOW_WANTS_MODELINE_P (w))
1335 w->mode_line_height
1336 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1337 BVAR (current_buffer, mode_line_format));
1339 if (WINDOW_WANTS_HEADER_LINE_P (w))
1340 w->header_line_height
1341 = display_mode_line (w, HEADER_LINE_FACE_ID,
1342 BVAR (current_buffer, header_line_format));
1344 start_display (&it, w, top);
1345 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1346 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1348 if (charpos >= 0
1349 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1350 && IT_CHARPOS (it) >= charpos)
1351 /* When scanning backwards under bidi iteration, move_it_to
1352 stops at or _before_ CHARPOS, because it stops at or to
1353 the _right_ of the character at CHARPOS. */
1354 || (it.bidi_p && it.bidi_it.scan_dir == -1
1355 && IT_CHARPOS (it) <= charpos)))
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1359 or covered by a display property, move_it_to stops at the end
1360 of the invisible text, to the right of CHARPOS. (ii) If
1361 CHARPOS is in a display vector, move_it_to stops on its last
1362 glyph. */
1363 int top_x = it.current_x;
1364 int top_y = it.current_y;
1365 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1366 int bottom_y;
1367 struct it save_it;
1368 void *save_it_data = NULL;
1370 /* Calling line_bottom_y may change it.method, it.position, etc. */
1371 SAVE_IT (save_it, it, save_it_data);
1372 last_height = 0;
1373 bottom_y = line_bottom_y (&it);
1374 if (top_y < window_top_y)
1375 visible_p = bottom_y > window_top_y;
1376 else if (top_y < it.last_visible_y)
1377 visible_p = true;
1378 if (bottom_y >= it.last_visible_y
1379 && it.bidi_p && it.bidi_it.scan_dir == -1
1380 && IT_CHARPOS (it) < charpos)
1382 /* When the last line of the window is scanned backwards
1383 under bidi iteration, we could be duped into thinking
1384 that we have passed CHARPOS, when in fact move_it_to
1385 simply stopped short of CHARPOS because it reached
1386 last_visible_y. To see if that's what happened, we call
1387 move_it_to again with a slightly larger vertical limit,
1388 and see if it actually moved vertically; if it did, we
1389 didn't really reach CHARPOS, which is beyond window end. */
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = false;
1400 RESTORE_IT (&it, &save_it, save_it_data);
1401 if (visible_p)
1403 if (it.method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, true);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1449 else if (IT_CHARPOS (it) != charpos)
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 bool newline_in_string
1456 = (STRINGP (string)
1457 && memchr (SDATA (string), '\n', SBYTES (string)));
1459 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1460 bool replacing_spec_p
1461 = (!NILP (spec)
1462 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1463 charpos, FRAME_WINDOW_P (it.f)));
1464 /* The tricky code below is needed because there's a
1465 discrepancy between move_it_to and how we set cursor
1466 when PT is at the beginning of a portion of text
1467 covered by a display property or an overlay with a
1468 display property, or the display line ends in a
1469 newline from a display string. move_it_to will stop
1470 _after_ such display strings, whereas
1471 set_cursor_from_row conspires with cursor_row_p to
1472 place the cursor on the first glyph produced from the
1473 display string. */
1475 /* We have overshoot PT because it is covered by a
1476 display property that replaces the text it covers.
1477 If the string includes embedded newlines, we are also
1478 in the wrong display line. Backtrack to the correct
1479 line, where the display property begins. */
1480 if (replacing_spec_p)
1482 Lisp_Object startpos, endpos;
1483 EMACS_INT start, end;
1484 struct it it3;
1486 /* Find the first and the last buffer positions
1487 covered by the display string. */
1488 endpos =
1489 Fnext_single_char_property_change (cpos, Qdisplay,
1490 Qnil, Qnil);
1491 startpos =
1492 Fprevious_single_char_property_change (endpos, Qdisplay,
1493 Qnil, Qnil);
1494 start = XFASTINT (startpos);
1495 end = XFASTINT (endpos);
1496 /* Move to the last buffer position before the
1497 display property. */
1498 start_display (&it3, w, top);
1499 if (start > CHARPOS (top))
1500 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1501 /* Move forward one more line if the position before
1502 the display string is a newline or if it is the
1503 rightmost character on a line that is
1504 continued or word-wrapped. */
1505 if (it3.method == GET_FROM_BUFFER
1506 && (it3.c == '\n'
1507 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1508 move_it_by_lines (&it3, 1);
1509 else if (move_it_in_display_line_to (&it3, -1,
1510 it3.current_x
1511 + it3.pixel_width,
1512 MOVE_TO_X)
1513 == MOVE_LINE_CONTINUED)
1515 move_it_by_lines (&it3, 1);
1516 /* When we are under word-wrap, the #$@%!
1517 move_it_by_lines moves 2 lines, so we need to
1518 fix that up. */
1519 if (it3.line_wrap == WORD_WRAP)
1520 move_it_by_lines (&it3, -1);
1523 /* Record the vertical coordinate of the display
1524 line where we wound up. */
1525 top_y = it3.current_y;
1526 if (it3.bidi_p)
1528 /* When characters are reordered for display,
1529 the character displayed to the left of the
1530 display string could be _after_ the display
1531 property in the logical order. Use the
1532 smallest vertical position of these two. */
1533 start_display (&it3, w, top);
1534 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1535 if (it3.current_y < top_y)
1536 top_y = it3.current_y;
1538 /* Move from the top of the window to the beginning
1539 of the display line where the display string
1540 begins. */
1541 start_display (&it3, w, top);
1542 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1543 /* If it3_moved stays false after the 'while' loop
1544 below, that means we already were at a newline
1545 before the loop (e.g., the display string begins
1546 with a newline), so we don't need to (and cannot)
1547 inspect the glyphs of it3.glyph_row, because
1548 PRODUCE_GLYPHS will not produce anything for a
1549 newline, and thus it3.glyph_row stays at its
1550 stale content it got at top of the window. */
1551 bool it3_moved = false;
1552 /* Finally, advance the iterator until we hit the
1553 first display element whose character position is
1554 CHARPOS, or until the first newline from the
1555 display string, which signals the end of the
1556 display line. */
1557 while (get_next_display_element (&it3))
1559 PRODUCE_GLYPHS (&it3);
1560 if (IT_CHARPOS (it3) == charpos
1561 || ITERATOR_AT_END_OF_LINE_P (&it3))
1562 break;
1563 it3_moved = true;
1564 set_iterator_to_next (&it3, false);
1566 top_x = it3.current_x - it3.pixel_width;
1567 /* Normally, we would exit the above loop because we
1568 found the display element whose character
1569 position is CHARPOS. For the contingency that we
1570 didn't, and stopped at the first newline from the
1571 display string, move back over the glyphs
1572 produced from the string, until we find the
1573 rightmost glyph not from the string. */
1574 if (it3_moved
1575 && newline_in_string
1576 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1578 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1579 + it3.glyph_row->used[TEXT_AREA];
1581 while (EQ ((g - 1)->object, string))
1583 --g;
1584 top_x -= g->pixel_width;
1586 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1587 + it3.glyph_row->used[TEXT_AREA]);
1592 *x = top_x;
1593 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1594 *rtop = max (0, window_top_y - top_y);
1595 *rbot = max (0, bottom_y - it.last_visible_y);
1596 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1597 - max (top_y, window_top_y)));
1598 *vpos = it.vpos;
1599 if (it.bidi_it.paragraph_dir == R2L)
1600 r2l = true;
1603 else
1605 /* Either we were asked to provide info about WINDOW_END, or
1606 CHARPOS is in the partially visible glyph row at end of
1607 window. */
1608 struct it it2;
1609 void *it2data = NULL;
1611 SAVE_IT (it2, it, it2data);
1612 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1613 move_it_by_lines (&it, 1);
1614 if (charpos < IT_CHARPOS (it)
1615 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1617 visible_p = true;
1618 RESTORE_IT (&it2, &it2, it2data);
1619 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1620 *x = it2.current_x;
1621 *y = it2.current_y + it2.max_ascent - it2.ascent;
1622 *rtop = max (0, -it2.current_y);
1623 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1624 - it.last_visible_y));
1625 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1626 it.last_visible_y)
1627 - max (it2.current_y,
1628 WINDOW_HEADER_LINE_HEIGHT (w))));
1629 *vpos = it2.vpos;
1630 if (it2.bidi_it.paragraph_dir == R2L)
1631 r2l = true;
1633 else
1634 bidi_unshelve_cache (it2data, true);
1636 bidi_unshelve_cache (itdata, false);
1638 if (old_buffer)
1639 set_buffer_internal_1 (old_buffer);
1641 if (visible_p)
1643 if (w->hscroll > 0)
1644 *x -=
1645 window_hscroll_limited (w, WINDOW_XFRAME (w))
1646 * WINDOW_FRAME_COLUMN_WIDTH (w);
1647 /* For lines in an R2L paragraph, we need to mirror the X pixel
1648 coordinate wrt the text area. For the reasons, see the
1649 commentary in buffer_posn_from_coords and the explanation of
1650 the geometry used by the move_it_* functions at the end of
1651 the large commentary near the beginning of this file. */
1652 if (r2l)
1653 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1656 #if false
1657 /* Debugging code. */
1658 if (visible_p)
1659 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1660 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1661 else
1662 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1663 #endif
1665 return visible_p;
1669 /* Return the next character from STR. Return in *LEN the length of
1670 the character. This is like STRING_CHAR_AND_LENGTH but never
1671 returns an invalid character. If we find one, we return a `?', but
1672 with the length of the invalid character. */
1674 static int
1675 string_char_and_length (const unsigned char *str, int *len)
1677 int c;
1679 c = STRING_CHAR_AND_LENGTH (str, *len);
1680 if (!CHAR_VALID_P (c))
1681 /* We may not change the length here because other places in Emacs
1682 don't use this function, i.e. they silently accept invalid
1683 characters. */
1684 c = '?';
1686 return c;
1691 /* Given a position POS containing a valid character and byte position
1692 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1694 static struct text_pos
1695 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1697 eassert (STRINGP (string) && nchars >= 0);
1699 if (STRING_MULTIBYTE (string))
1701 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1702 int len;
1704 while (nchars--)
1706 string_char_and_length (p, &len);
1707 p += len;
1708 CHARPOS (pos) += 1;
1709 BYTEPOS (pos) += len;
1712 else
1713 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1715 return pos;
1719 /* Value is the text position, i.e. character and byte position,
1720 for character position CHARPOS in STRING. */
1722 static struct text_pos
1723 string_pos (ptrdiff_t charpos, Lisp_Object string)
1725 struct text_pos pos;
1726 eassert (STRINGP (string));
1727 eassert (charpos >= 0);
1728 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1729 return pos;
1733 /* Value is a text position, i.e. character and byte position, for
1734 character position CHARPOS in C string S. MULTIBYTE_P
1735 means recognize multibyte characters. */
1737 static struct text_pos
1738 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1740 struct text_pos pos;
1742 eassert (s != NULL);
1743 eassert (charpos >= 0);
1745 if (multibyte_p)
1747 int len;
1749 SET_TEXT_POS (pos, 0, 0);
1750 while (charpos--)
1752 string_char_and_length ((const unsigned char *) s, &len);
1753 s += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1758 else
1759 SET_TEXT_POS (pos, charpos, charpos);
1761 return pos;
1765 /* Value is the number of characters in C string S. MULTIBYTE_P
1766 means recognize multibyte characters. */
1768 static ptrdiff_t
1769 number_of_chars (const char *s, bool multibyte_p)
1771 ptrdiff_t nchars;
1773 if (multibyte_p)
1775 ptrdiff_t rest = strlen (s);
1776 int len;
1777 const unsigned char *p = (const unsigned char *) s;
1779 for (nchars = 0; rest > 0; ++nchars)
1781 string_char_and_length (p, &len);
1782 rest -= len, p += len;
1785 else
1786 nchars = strlen (s);
1788 return nchars;
1792 /* Compute byte position NEWPOS->bytepos corresponding to
1793 NEWPOS->charpos. POS is a known position in string STRING.
1794 NEWPOS->charpos must be >= POS.charpos. */
1796 static void
1797 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1799 eassert (STRINGP (string));
1800 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1802 if (STRING_MULTIBYTE (string))
1803 *newpos = string_pos_nchars_ahead (pos, string,
1804 CHARPOS (*newpos) - CHARPOS (pos));
1805 else
1806 BYTEPOS (*newpos) = CHARPOS (*newpos);
1809 /* EXPORT:
1810 Return an estimation of the pixel height of mode or header lines on
1811 frame F. FACE_ID specifies what line's height to estimate. */
1814 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1816 #ifdef HAVE_WINDOW_SYSTEM
1817 if (FRAME_WINDOW_P (f))
1819 int height = FONT_HEIGHT (FRAME_FONT (f));
1821 /* This function is called so early when Emacs starts that the face
1822 cache and mode line face are not yet initialized. */
1823 if (FRAME_FACE_CACHE (f))
1825 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1826 if (face)
1828 if (face->font)
1829 height = normal_char_height (face->font, -1);
1830 if (face->box_line_width > 0)
1831 height += 2 * face->box_line_width;
1835 return height;
1837 #endif
1839 return 1;
1842 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1843 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1844 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1845 not force the value into range. */
1847 void
1848 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1849 NativeRectangle *bounds, bool noclip)
1852 #ifdef HAVE_WINDOW_SYSTEM
1853 if (FRAME_WINDOW_P (f))
1855 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1856 even for negative values. */
1857 if (pix_x < 0)
1858 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1859 if (pix_y < 0)
1860 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1862 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1863 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1865 if (bounds)
1866 STORE_NATIVE_RECT (*bounds,
1867 FRAME_COL_TO_PIXEL_X (f, pix_x),
1868 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1869 FRAME_COLUMN_WIDTH (f) - 1,
1870 FRAME_LINE_HEIGHT (f) - 1);
1872 /* PXW: Should we clip pixels before converting to columns/lines? */
1873 if (!noclip)
1875 if (pix_x < 0)
1876 pix_x = 0;
1877 else if (pix_x > FRAME_TOTAL_COLS (f))
1878 pix_x = FRAME_TOTAL_COLS (f);
1880 if (pix_y < 0)
1881 pix_y = 0;
1882 else if (pix_y > FRAME_TOTAL_LINES (f))
1883 pix_y = FRAME_TOTAL_LINES (f);
1886 #endif
1888 *x = pix_x;
1889 *y = pix_y;
1893 /* Find the glyph under window-relative coordinates X/Y in window W.
1894 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1895 strings. Return in *HPOS and *VPOS the row and column number of
1896 the glyph found. Return in *AREA the glyph area containing X.
1897 Value is a pointer to the glyph found or null if X/Y is not on
1898 text, or we can't tell because W's current matrix is not up to
1899 date. */
1901 static struct glyph *
1902 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1903 int *dx, int *dy, int *area)
1905 struct glyph *glyph, *end;
1906 struct glyph_row *row = NULL;
1907 int x0, i;
1909 /* Find row containing Y. Give up if some row is not enabled. */
1910 for (i = 0; i < w->current_matrix->nrows; ++i)
1912 row = MATRIX_ROW (w->current_matrix, i);
1913 if (!row->enabled_p)
1914 return NULL;
1915 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1916 break;
1919 *vpos = i;
1920 *hpos = 0;
1922 /* Give up if Y is not in the window. */
1923 if (i == w->current_matrix->nrows)
1924 return NULL;
1926 /* Get the glyph area containing X. */
1927 if (w->pseudo_window_p)
1929 *area = TEXT_AREA;
1930 x0 = 0;
1932 else
1934 if (x < window_box_left_offset (w, TEXT_AREA))
1936 *area = LEFT_MARGIN_AREA;
1937 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1939 else if (x < window_box_right_offset (w, TEXT_AREA))
1941 *area = TEXT_AREA;
1942 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1944 else
1946 *area = RIGHT_MARGIN_AREA;
1947 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1951 /* Find glyph containing X. */
1952 glyph = row->glyphs[*area];
1953 end = glyph + row->used[*area];
1954 x -= x0;
1955 while (glyph < end && x >= glyph->pixel_width)
1957 x -= glyph->pixel_width;
1958 ++glyph;
1961 if (glyph == end)
1962 return NULL;
1964 if (dx)
1966 *dx = x;
1967 *dy = y - (row->y + row->ascent - glyph->ascent);
1970 *hpos = glyph - row->glyphs[*area];
1971 return glyph;
1974 /* Convert frame-relative x/y to coordinates relative to window W.
1975 Takes pseudo-windows into account. */
1977 static void
1978 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1980 if (w->pseudo_window_p)
1982 /* A pseudo-window is always full-width, and starts at the
1983 left edge of the frame, plus a frame border. */
1984 struct frame *f = XFRAME (w->frame);
1985 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1986 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1988 else
1990 *x -= WINDOW_LEFT_EDGE_X (w);
1991 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1995 #ifdef HAVE_WINDOW_SYSTEM
1997 /* EXPORT:
1998 Return in RECTS[] at most N clipping rectangles for glyph string S.
1999 Return the number of stored rectangles. */
2002 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2004 XRectangle r;
2006 if (n <= 0)
2007 return 0;
2009 if (s->row->full_width_p)
2011 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2012 r.x = WINDOW_LEFT_EDGE_X (s->w);
2013 if (s->row->mode_line_p)
2014 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2015 else
2016 r.width = WINDOW_PIXEL_WIDTH (s->w);
2018 /* Unless displaying a mode or menu bar line, which are always
2019 fully visible, clip to the visible part of the row. */
2020 if (s->w->pseudo_window_p)
2021 r.height = s->row->visible_height;
2022 else
2023 r.height = s->height;
2025 else
2027 /* This is a text line that may be partially visible. */
2028 r.x = window_box_left (s->w, s->area);
2029 r.width = window_box_width (s->w, s->area);
2030 r.height = s->row->visible_height;
2033 if (s->clip_head)
2034 if (r.x < s->clip_head->x)
2036 if (r.width >= s->clip_head->x - r.x)
2037 r.width -= s->clip_head->x - r.x;
2038 else
2039 r.width = 0;
2040 r.x = s->clip_head->x;
2042 if (s->clip_tail)
2043 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2045 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2046 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2047 else
2048 r.width = 0;
2051 /* If S draws overlapping rows, it's sufficient to use the top and
2052 bottom of the window for clipping because this glyph string
2053 intentionally draws over other lines. */
2054 if (s->for_overlaps)
2056 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2057 r.height = window_text_bottom_y (s->w) - r.y;
2059 /* Alas, the above simple strategy does not work for the
2060 environments with anti-aliased text: if the same text is
2061 drawn onto the same place multiple times, it gets thicker.
2062 If the overlap we are processing is for the erased cursor, we
2063 take the intersection with the rectangle of the cursor. */
2064 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2066 XRectangle rc, r_save = r;
2068 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2069 rc.y = s->w->phys_cursor.y;
2070 rc.width = s->w->phys_cursor_width;
2071 rc.height = s->w->phys_cursor_height;
2073 x_intersect_rectangles (&r_save, &rc, &r);
2076 else
2078 /* Don't use S->y for clipping because it doesn't take partially
2079 visible lines into account. For example, it can be negative for
2080 partially visible lines at the top of a window. */
2081 if (!s->row->full_width_p
2082 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 else
2085 r.y = max (0, s->row->y);
2088 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2090 /* If drawing the cursor, don't let glyph draw outside its
2091 advertised boundaries. Cleartype does this under some circumstances. */
2092 if (s->hl == DRAW_CURSOR)
2094 struct glyph *glyph = s->first_glyph;
2095 int height, max_y;
2097 if (s->x > r.x)
2099 if (r.width >= s->x - r.x)
2100 r.width -= s->x - r.x;
2101 else /* R2L hscrolled row with cursor outside text area */
2102 r.width = 0;
2103 r.x = s->x;
2105 r.width = min (r.width, glyph->pixel_width);
2107 /* If r.y is below window bottom, ensure that we still see a cursor. */
2108 height = min (glyph->ascent + glyph->descent,
2109 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2110 max_y = window_text_bottom_y (s->w) - height;
2111 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2112 if (s->ybase - glyph->ascent > max_y)
2114 r.y = max_y;
2115 r.height = height;
2117 else
2119 /* Don't draw cursor glyph taller than our actual glyph. */
2120 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2121 if (height < r.height)
2123 max_y = r.y + r.height;
2124 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2125 r.height = min (max_y - r.y, height);
2130 if (s->row->clip)
2132 XRectangle r_save = r;
2134 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2135 r.width = 0;
2138 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2139 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2141 #ifdef CONVERT_FROM_XRECT
2142 CONVERT_FROM_XRECT (r, *rects);
2143 #else
2144 *rects = r;
2145 #endif
2146 return 1;
2148 else
2150 /* If we are processing overlapping and allowed to return
2151 multiple clipping rectangles, we exclude the row of the glyph
2152 string from the clipping rectangle. This is to avoid drawing
2153 the same text on the environment with anti-aliasing. */
2154 #ifdef CONVERT_FROM_XRECT
2155 XRectangle rs[2];
2156 #else
2157 XRectangle *rs = rects;
2158 #endif
2159 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2161 if (s->for_overlaps & OVERLAPS_PRED)
2163 rs[i] = r;
2164 if (r.y + r.height > row_y)
2166 if (r.y < row_y)
2167 rs[i].height = row_y - r.y;
2168 else
2169 rs[i].height = 0;
2171 i++;
2173 if (s->for_overlaps & OVERLAPS_SUCC)
2175 rs[i] = r;
2176 if (r.y < row_y + s->row->visible_height)
2178 if (r.y + r.height > row_y + s->row->visible_height)
2180 rs[i].y = row_y + s->row->visible_height;
2181 rs[i].height = r.y + r.height - rs[i].y;
2183 else
2184 rs[i].height = 0;
2186 i++;
2189 n = i;
2190 #ifdef CONVERT_FROM_XRECT
2191 for (i = 0; i < n; i++)
2192 CONVERT_FROM_XRECT (rs[i], rects[i]);
2193 #endif
2194 return n;
2198 /* EXPORT:
2199 Return in *NR the clipping rectangle for glyph string S. */
2201 void
2202 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2204 get_glyph_string_clip_rects (s, nr, 1);
2208 /* EXPORT:
2209 Return the position and height of the phys cursor in window W.
2210 Set w->phys_cursor_width to width of phys cursor.
2213 void
2214 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2215 struct glyph *glyph, int *xp, int *yp, int *heightp)
2217 struct frame *f = XFRAME (WINDOW_FRAME (w));
2218 int x, y, wd, h, h0, y0, ascent;
2220 /* Compute the width of the rectangle to draw. If on a stretch
2221 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2222 rectangle as wide as the glyph, but use a canonical character
2223 width instead. */
2224 wd = glyph->pixel_width;
2226 x = w->phys_cursor.x;
2227 if (x < 0)
2229 wd += x;
2230 x = 0;
2233 if (glyph->type == STRETCH_GLYPH
2234 && !x_stretch_cursor_p)
2235 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2236 w->phys_cursor_width = wd;
2238 /* Don't let the hollow cursor glyph descend below the glyph row's
2239 ascent value, lest the hollow cursor looks funny. */
2240 y = w->phys_cursor.y;
2241 ascent = row->ascent;
2242 if (row->ascent < glyph->ascent)
2244 y -= glyph->ascent - row->ascent;
2245 ascent = glyph->ascent;
2248 /* If y is below window bottom, ensure that we still see a cursor. */
2249 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2251 h = max (h0, ascent + glyph->descent);
2252 h0 = min (h0, ascent + glyph->descent);
2254 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2255 if (y < y0)
2257 h = max (h - (y0 - y) + 1, h0);
2258 y = y0 - 1;
2260 else
2262 y0 = window_text_bottom_y (w) - h0;
2263 if (y > y0)
2265 h += y - y0;
2266 y = y0;
2270 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2271 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2272 *heightp = h;
2276 * Remember which glyph the mouse is over.
2279 void
2280 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2282 Lisp_Object window;
2283 struct window *w;
2284 struct glyph_row *r, *gr, *end_row;
2285 enum window_part part;
2286 enum glyph_row_area area;
2287 int x, y, width, height;
2289 /* Try to determine frame pixel position and size of the glyph under
2290 frame pixel coordinates X/Y on frame F. */
2292 if (window_resize_pixelwise)
2294 width = height = 1;
2295 goto virtual_glyph;
2297 else if (!f->glyphs_initialized_p
2298 || (window = window_from_coordinates (f, gx, gy, &part, false),
2299 NILP (window)))
2301 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2302 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2303 goto virtual_glyph;
2306 w = XWINDOW (window);
2307 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2308 height = WINDOW_FRAME_LINE_HEIGHT (w);
2310 x = window_relative_x_coord (w, part, gx);
2311 y = gy - WINDOW_TOP_EDGE_Y (w);
2313 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2314 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2316 if (w->pseudo_window_p)
2318 area = TEXT_AREA;
2319 part = ON_MODE_LINE; /* Don't adjust margin. */
2320 goto text_glyph;
2323 switch (part)
2325 case ON_LEFT_MARGIN:
2326 area = LEFT_MARGIN_AREA;
2327 goto text_glyph;
2329 case ON_RIGHT_MARGIN:
2330 area = RIGHT_MARGIN_AREA;
2331 goto text_glyph;
2333 case ON_HEADER_LINE:
2334 case ON_MODE_LINE:
2335 gr = (part == ON_HEADER_LINE
2336 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2337 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2338 gy = gr->y;
2339 area = TEXT_AREA;
2340 goto text_glyph_row_found;
2342 case ON_TEXT:
2343 area = TEXT_AREA;
2345 text_glyph:
2346 gr = 0; gy = 0;
2347 for (; r <= end_row && r->enabled_p; ++r)
2348 if (r->y + r->height > y)
2350 gr = r; gy = r->y;
2351 break;
2354 text_glyph_row_found:
2355 if (gr && gy <= y)
2357 struct glyph *g = gr->glyphs[area];
2358 struct glyph *end = g + gr->used[area];
2360 height = gr->height;
2361 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2362 if (gx + g->pixel_width > x)
2363 break;
2365 if (g < end)
2367 if (g->type == IMAGE_GLYPH)
2369 /* Don't remember when mouse is over image, as
2370 image may have hot-spots. */
2371 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2372 return;
2374 width = g->pixel_width;
2376 else
2378 /* Use nominal char spacing at end of line. */
2379 x -= gx;
2380 gx += (x / width) * width;
2383 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 gx += window_box_left_offset (w, area);
2386 /* Don't expand over the modeline to make sure the vertical
2387 drag cursor is shown early enough. */
2388 height = min (height,
2389 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2392 else
2394 /* Use nominal line height at end of window. */
2395 gx = (x / width) * width;
2396 y -= gy;
2397 gy += (y / height) * height;
2398 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2399 /* See comment above. */
2400 height = min (height,
2401 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2403 break;
2405 case ON_LEFT_FRINGE:
2406 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2407 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2408 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2409 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2410 goto row_glyph;
2412 case ON_RIGHT_FRINGE:
2413 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2414 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2415 : window_box_right_offset (w, TEXT_AREA));
2416 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2417 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2418 && !WINDOW_RIGHTMOST_P (w))
2419 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2420 /* Make sure the vertical border can get her own glyph to the
2421 right of the one we build here. */
2422 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2423 else
2424 width = WINDOW_PIXEL_WIDTH (w) - gx;
2425 else
2426 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2428 goto row_glyph;
2430 case ON_VERTICAL_BORDER:
2431 gx = WINDOW_PIXEL_WIDTH (w) - width;
2432 goto row_glyph;
2434 case ON_VERTICAL_SCROLL_BAR:
2435 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2437 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2439 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2440 : 0)));
2441 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2443 row_glyph:
2444 gr = 0, gy = 0;
2445 for (; r <= end_row && r->enabled_p; ++r)
2446 if (r->y + r->height > y)
2448 gr = r; gy = r->y;
2449 break;
2452 if (gr && gy <= y)
2453 height = gr->height;
2454 else
2456 /* Use nominal line height at end of window. */
2457 y -= gy;
2458 gy += (y / height) * height;
2460 break;
2462 case ON_RIGHT_DIVIDER:
2463 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2464 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2465 gy = 0;
2466 /* The bottom divider prevails. */
2467 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2468 goto add_edge;
2470 case ON_BOTTOM_DIVIDER:
2471 gx = 0;
2472 width = WINDOW_PIXEL_WIDTH (w);
2473 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2474 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2475 goto add_edge;
2477 default:
2479 virtual_glyph:
2480 /* If there is no glyph under the mouse, then we divide the screen
2481 into a grid of the smallest glyph in the frame, and use that
2482 as our "glyph". */
2484 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2485 round down even for negative values. */
2486 if (gx < 0)
2487 gx -= width - 1;
2488 if (gy < 0)
2489 gy -= height - 1;
2491 gx = (gx / width) * width;
2492 gy = (gy / height) * height;
2494 goto store_rect;
2497 add_edge:
2498 gx += WINDOW_LEFT_EDGE_X (w);
2499 gy += WINDOW_TOP_EDGE_Y (w);
2501 store_rect:
2502 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2504 /* Visible feedback for debugging. */
2505 #if false && defined HAVE_X_WINDOWS
2506 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2507 f->output_data.x->normal_gc,
2508 gx, gy, width, height);
2509 #endif
2513 #endif /* HAVE_WINDOW_SYSTEM */
2515 static void
2516 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2518 eassert (w);
2519 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2520 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2521 w->window_end_vpos
2522 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2525 /***********************************************************************
2526 Lisp form evaluation
2527 ***********************************************************************/
2529 /* Error handler for safe_eval and safe_call. */
2531 static Lisp_Object
2532 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2534 add_to_log ("Error during redisplay: %S signaled %S",
2535 Flist (nargs, args), arg);
2536 return Qnil;
2539 /* Call function FUNC with the rest of NARGS - 1 arguments
2540 following. Return the result, or nil if something went
2541 wrong. Prevent redisplay during the evaluation. */
2543 static Lisp_Object
2544 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2546 Lisp_Object val;
2548 if (inhibit_eval_during_redisplay)
2549 val = Qnil;
2550 else
2552 ptrdiff_t i;
2553 ptrdiff_t count = SPECPDL_INDEX ();
2554 Lisp_Object *args;
2555 USE_SAFE_ALLOCA;
2556 SAFE_ALLOCA_LISP (args, nargs);
2558 args[0] = func;
2559 for (i = 1; i < nargs; i++)
2560 args[i] = va_arg (ap, Lisp_Object);
2562 specbind (Qinhibit_redisplay, Qt);
2563 if (inhibit_quit)
2564 specbind (Qinhibit_quit, Qt);
2565 /* Use Qt to ensure debugger does not run,
2566 so there is no possibility of wanting to redisplay. */
2567 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2568 safe_eval_handler);
2569 SAFE_FREE ();
2570 val = unbind_to (count, val);
2573 return val;
2576 Lisp_Object
2577 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2579 Lisp_Object retval;
2580 va_list ap;
2582 va_start (ap, func);
2583 retval = safe__call (false, nargs, func, ap);
2584 va_end (ap);
2585 return retval;
2588 /* Call function FN with one argument ARG.
2589 Return the result, or nil if something went wrong. */
2591 Lisp_Object
2592 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2594 return safe_call (2, fn, arg);
2597 static Lisp_Object
2598 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2600 Lisp_Object retval;
2601 va_list ap;
2603 va_start (ap, fn);
2604 retval = safe__call (inhibit_quit, 2, fn, ap);
2605 va_end (ap);
2606 return retval;
2609 Lisp_Object
2610 safe_eval (Lisp_Object sexpr)
2612 return safe__call1 (false, Qeval, sexpr);
2615 static Lisp_Object
2616 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2618 return safe__call1 (inhibit_quit, Qeval, sexpr);
2621 /* Call function FN with two arguments ARG1 and ARG2.
2622 Return the result, or nil if something went wrong. */
2624 Lisp_Object
2625 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2627 return safe_call (3, fn, arg1, arg2);
2632 /***********************************************************************
2633 Debugging
2634 ***********************************************************************/
2636 /* Define CHECK_IT to perform sanity checks on iterators.
2637 This is for debugging. It is too slow to do unconditionally. */
2639 static void
2640 CHECK_IT (struct it *it)
2642 #if false
2643 if (it->method == GET_FROM_STRING)
2645 eassert (STRINGP (it->string));
2646 eassert (IT_STRING_CHARPOS (*it) >= 0);
2648 else
2650 eassert (IT_STRING_CHARPOS (*it) < 0);
2651 if (it->method == GET_FROM_BUFFER)
2653 /* Check that character and byte positions agree. */
2654 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2658 if (it->dpvec)
2659 eassert (it->current.dpvec_index >= 0);
2660 else
2661 eassert (it->current.dpvec_index < 0);
2662 #endif
2666 /* Check that the window end of window W is what we expect it
2667 to be---the last row in the current matrix displaying text. */
2669 static void
2670 CHECK_WINDOW_END (struct window *w)
2672 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2673 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2675 struct glyph_row *row;
2676 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2677 !row->enabled_p
2678 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2679 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2681 #endif
2684 /***********************************************************************
2685 Iterator initialization
2686 ***********************************************************************/
2688 /* Initialize IT for displaying current_buffer in window W, starting
2689 at character position CHARPOS. CHARPOS < 0 means that no buffer
2690 position is specified which is useful when the iterator is assigned
2691 a position later. BYTEPOS is the byte position corresponding to
2692 CHARPOS.
2694 If ROW is not null, calls to produce_glyphs with IT as parameter
2695 will produce glyphs in that row.
2697 BASE_FACE_ID is the id of a base face to use. It must be one of
2698 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2699 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2700 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2702 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2704 will be initialized to use the corresponding mode line glyph row of
2705 the desired matrix of W. */
2707 void
2708 init_iterator (struct it *it, struct window *w,
2709 ptrdiff_t charpos, ptrdiff_t bytepos,
2710 struct glyph_row *row, enum face_id base_face_id)
2712 enum face_id remapped_base_face_id = base_face_id;
2714 /* Some precondition checks. */
2715 eassert (w != NULL && it != NULL);
2716 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2717 && charpos <= ZV));
2719 /* If face attributes have been changed since the last redisplay,
2720 free realized faces now because they depend on face definitions
2721 that might have changed. Don't free faces while there might be
2722 desired matrices pending which reference these faces. */
2723 if (!inhibit_free_realized_faces)
2725 if (face_change)
2727 face_change = false;
2728 free_all_realized_faces (Qnil);
2730 else if (XFRAME (w->frame)->face_change)
2732 XFRAME (w->frame)->face_change = 0;
2733 free_all_realized_faces (w->frame);
2737 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2738 if (! NILP (Vface_remapping_alist))
2739 remapped_base_face_id
2740 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2742 /* Use one of the mode line rows of W's desired matrix if
2743 appropriate. */
2744 if (row == NULL)
2746 if (base_face_id == MODE_LINE_FACE_ID
2747 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2748 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2749 else if (base_face_id == HEADER_LINE_FACE_ID)
2750 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2753 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2754 Other parts of redisplay rely on that. */
2755 memclear (it, sizeof *it);
2756 it->current.overlay_string_index = -1;
2757 it->current.dpvec_index = -1;
2758 it->base_face_id = remapped_base_face_id;
2759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2760 it->paragraph_embedding = L2R;
2761 it->bidi_it.w = w;
2763 /* The window in which we iterate over current_buffer: */
2764 XSETWINDOW (it->window, w);
2765 it->w = w;
2766 it->f = XFRAME (w->frame);
2768 it->cmp_it.id = -1;
2770 /* Extra space between lines (on window systems only). */
2771 if (base_face_id == DEFAULT_FACE_ID
2772 && FRAME_WINDOW_P (it->f))
2774 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2776 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2777 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2778 * FRAME_LINE_HEIGHT (it->f));
2779 else if (it->f->extra_line_spacing > 0)
2780 it->extra_line_spacing = it->f->extra_line_spacing;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 it->override_ascent = -1;
2794 /* Are control characters displayed as `^C'? */
2795 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2797 /* -1 means everything between a CR and the following line end
2798 is invisible. >0 means lines indented more than this value are
2799 invisible. */
2800 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2801 ? (clip_to_bounds
2802 (-1, XINT (BVAR (current_buffer, selective_display)),
2803 PTRDIFF_MAX))
2804 : (!NILP (BVAR (current_buffer, selective_display))
2805 ? -1 : 0));
2806 it->selective_display_ellipsis_p
2807 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2809 /* Display table to use. */
2810 it->dp = window_display_table (w);
2812 /* Are multibyte characters enabled in current_buffer? */
2813 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2815 /* Get the position at which the redisplay_end_trigger hook should
2816 be run, if it is to be run at all. */
2817 if (MARKERP (w->redisplay_end_trigger)
2818 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2819 it->redisplay_end_trigger_charpos
2820 = marker_position (w->redisplay_end_trigger);
2821 else if (INTEGERP (w->redisplay_end_trigger))
2822 it->redisplay_end_trigger_charpos
2823 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2824 PTRDIFF_MAX);
2826 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2828 /* Are lines in the display truncated? */
2829 if (TRUNCATE != 0)
2830 it->line_wrap = TRUNCATE;
2831 if (base_face_id == DEFAULT_FACE_ID
2832 && !it->w->hscroll
2833 && (WINDOW_FULL_WIDTH_P (it->w)
2834 || NILP (Vtruncate_partial_width_windows)
2835 || (INTEGERP (Vtruncate_partial_width_windows)
2836 /* PXW: Shall we do something about this? */
2837 && (XINT (Vtruncate_partial_width_windows)
2838 <= WINDOW_TOTAL_COLS (it->w))))
2839 && NILP (BVAR (current_buffer, truncate_lines)))
2840 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2841 ? WINDOW_WRAP : WORD_WRAP;
2843 /* Get dimensions of truncation and continuation glyphs. These are
2844 displayed as fringe bitmaps under X, but we need them for such
2845 frames when the fringes are turned off. But leave the dimensions
2846 zero for tooltip frames, as these glyphs look ugly there and also
2847 sabotage calculations of tooltip dimensions in x-show-tip. */
2848 #ifdef HAVE_WINDOW_SYSTEM
2849 if (!(FRAME_WINDOW_P (it->f)
2850 && FRAMEP (tip_frame)
2851 && it->f == XFRAME (tip_frame)))
2852 #endif
2854 if (it->line_wrap == TRUNCATE)
2856 /* We will need the truncation glyph. */
2857 eassert (it->glyph_row == NULL);
2858 produce_special_glyphs (it, IT_TRUNCATION);
2859 it->truncation_pixel_width = it->pixel_width;
2861 else
2863 /* We will need the continuation glyph. */
2864 eassert (it->glyph_row == NULL);
2865 produce_special_glyphs (it, IT_CONTINUATION);
2866 it->continuation_pixel_width = it->pixel_width;
2870 /* Reset these values to zero because the produce_special_glyphs
2871 above has changed them. */
2872 it->pixel_width = it->ascent = it->descent = 0;
2873 it->phys_ascent = it->phys_descent = 0;
2875 /* Set this after getting the dimensions of truncation and
2876 continuation glyphs, so that we don't produce glyphs when calling
2877 produce_special_glyphs, above. */
2878 it->glyph_row = row;
2879 it->area = TEXT_AREA;
2881 /* Get the dimensions of the display area. The display area
2882 consists of the visible window area plus a horizontally scrolled
2883 part to the left of the window. All x-values are relative to the
2884 start of this total display area. */
2885 if (base_face_id != DEFAULT_FACE_ID)
2887 /* Mode lines, menu bar in terminal frames. */
2888 it->first_visible_x = 0;
2889 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2891 else
2893 it->first_visible_x
2894 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2895 it->last_visible_x = (it->first_visible_x
2896 + window_box_width (w, TEXT_AREA));
2898 /* If we truncate lines, leave room for the truncation glyph(s) at
2899 the right margin. Otherwise, leave room for the continuation
2900 glyph(s). Done only if the window has no right fringe. */
2901 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2903 if (it->line_wrap == TRUNCATE)
2904 it->last_visible_x -= it->truncation_pixel_width;
2905 else
2906 it->last_visible_x -= it->continuation_pixel_width;
2909 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2910 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2913 /* Leave room for a border glyph. */
2914 if (!FRAME_WINDOW_P (it->f)
2915 && !WINDOW_RIGHTMOST_P (it->w))
2916 it->last_visible_x -= 1;
2918 it->last_visible_y = window_text_bottom_y (w);
2920 /* For mode lines and alike, arrange for the first glyph having a
2921 left box line if the face specifies a box. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2924 struct face *face;
2926 it->face_id = remapped_base_face_id;
2928 /* If we have a boxed mode line, make the first character appear
2929 with a left box line. */
2930 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2931 if (face && face->box != FACE_NO_BOX)
2932 it->start_of_box_run_p = true;
2935 /* If a buffer position was specified, set the iterator there,
2936 getting overlays and face properties from that position. */
2937 if (charpos >= BUF_BEG (current_buffer))
2939 it->stop_charpos = charpos;
2940 it->end_charpos = ZV;
2941 eassert (charpos == BYTE_TO_CHAR (bytepos));
2942 IT_CHARPOS (*it) = charpos;
2943 IT_BYTEPOS (*it) = bytepos;
2945 /* We will rely on `reseat' to set this up properly, via
2946 handle_face_prop. */
2947 it->face_id = it->base_face_id;
2949 it->start = it->current;
2950 /* Do we need to reorder bidirectional text? Not if this is a
2951 unibyte buffer: by definition, none of the single-byte
2952 characters are strong R2L, so no reordering is needed. And
2953 bidi.c doesn't support unibyte buffers anyway. Also, don't
2954 reorder while we are loading loadup.el, since the tables of
2955 character properties needed for reordering are not yet
2956 available. */
2957 it->bidi_p =
2958 !redisplay__inhibit_bidi
2959 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2960 && it->multibyte_p;
2962 /* If we are to reorder bidirectional text, init the bidi
2963 iterator. */
2964 if (it->bidi_p)
2966 /* Since we don't know at this point whether there will be
2967 any R2L lines in the window, we reserve space for
2968 truncation/continuation glyphs even if only the left
2969 fringe is absent. */
2970 if (base_face_id == DEFAULT_FACE_ID
2971 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2972 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2974 if (it->line_wrap == TRUNCATE)
2975 it->last_visible_x -= it->truncation_pixel_width;
2976 else
2977 it->last_visible_x -= it->continuation_pixel_width;
2979 /* Note the paragraph direction that this buffer wants to
2980 use. */
2981 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2982 Qleft_to_right))
2983 it->paragraph_embedding = L2R;
2984 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2985 Qright_to_left))
2986 it->paragraph_embedding = R2L;
2987 else
2988 it->paragraph_embedding = NEUTRAL_DIR;
2989 bidi_unshelve_cache (NULL, false);
2990 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2991 &it->bidi_it);
2994 /* Compute faces etc. */
2995 reseat (it, it->current.pos, true);
2998 CHECK_IT (it);
3002 /* Initialize IT for the display of window W with window start POS. */
3004 void
3005 start_display (struct it *it, struct window *w, struct text_pos pos)
3007 struct glyph_row *row;
3008 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3010 row = w->desired_matrix->rows + first_vpos;
3011 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3012 it->first_vpos = first_vpos;
3014 /* Don't reseat to previous visible line start if current start
3015 position is in a string or image. */
3016 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3018 int first_y = it->current_y;
3020 /* If window start is not at a line start, skip forward to POS to
3021 get the correct continuation lines width. */
3022 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3023 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3024 if (!start_at_line_beg_p)
3026 int new_x;
3028 reseat_at_previous_visible_line_start (it);
3029 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3031 new_x = it->current_x + it->pixel_width;
3033 /* If lines are continued, this line may end in the middle
3034 of a multi-glyph character (e.g. a control character
3035 displayed as \003, or in the middle of an overlay
3036 string). In this case move_it_to above will not have
3037 taken us to the start of the continuation line but to the
3038 end of the continued line. */
3039 if (it->current_x > 0
3040 && it->line_wrap != TRUNCATE /* Lines are continued. */
3041 && (/* And glyph doesn't fit on the line. */
3042 new_x > it->last_visible_x
3043 /* Or it fits exactly and we're on a window
3044 system frame. */
3045 || (new_x == it->last_visible_x
3046 && FRAME_WINDOW_P (it->f)
3047 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3048 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3049 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3051 if ((it->current.dpvec_index >= 0
3052 || it->current.overlay_string_index >= 0)
3053 /* If we are on a newline from a display vector or
3054 overlay string, then we are already at the end of
3055 a screen line; no need to go to the next line in
3056 that case, as this line is not really continued.
3057 (If we do go to the next line, C-e will not DTRT.) */
3058 && it->c != '\n')
3060 set_iterator_to_next (it, true);
3061 move_it_in_display_line_to (it, -1, -1, 0);
3064 it->continuation_lines_width += it->current_x;
3066 /* If the character at POS is displayed via a display
3067 vector, move_it_to above stops at the final glyph of
3068 IT->dpvec. To make the caller redisplay that character
3069 again (a.k.a. start at POS), we need to reset the
3070 dpvec_index to the beginning of IT->dpvec. */
3071 else if (it->current.dpvec_index >= 0)
3072 it->current.dpvec_index = 0;
3074 /* We're starting a new display line, not affected by the
3075 height of the continued line, so clear the appropriate
3076 fields in the iterator structure. */
3077 it->max_ascent = it->max_descent = 0;
3078 it->max_phys_ascent = it->max_phys_descent = 0;
3080 it->current_y = first_y;
3081 it->vpos = 0;
3082 it->current_x = it->hpos = 0;
3088 /* Return true if POS is a position in ellipses displayed for invisible
3089 text. W is the window we display, for text property lookup. */
3091 static bool
3092 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3094 Lisp_Object prop, window;
3095 bool ellipses_p = false;
3096 ptrdiff_t charpos = CHARPOS (pos->pos);
3098 /* If POS specifies a position in a display vector, this might
3099 be for an ellipsis displayed for invisible text. We won't
3100 get the iterator set up for delivering that ellipsis unless
3101 we make sure that it gets aware of the invisible text. */
3102 if (pos->dpvec_index >= 0
3103 && pos->overlay_string_index < 0
3104 && CHARPOS (pos->string_pos) < 0
3105 && charpos > BEGV
3106 && (XSETWINDOW (window, w),
3107 prop = Fget_char_property (make_number (charpos),
3108 Qinvisible, window),
3109 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3111 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3112 window);
3113 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3116 return ellipses_p;
3120 /* Initialize IT for stepping through current_buffer in window W,
3121 starting at position POS that includes overlay string and display
3122 vector/ control character translation position information. Value
3123 is false if there are overlay strings with newlines at POS. */
3125 static bool
3126 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3128 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3129 int i;
3130 bool overlay_strings_with_newlines = false;
3132 /* If POS specifies a position in a display vector, this might
3133 be for an ellipsis displayed for invisible text. We won't
3134 get the iterator set up for delivering that ellipsis unless
3135 we make sure that it gets aware of the invisible text. */
3136 if (in_ellipses_for_invisible_text_p (pos, w))
3138 --charpos;
3139 bytepos = 0;
3142 /* Keep in mind: the call to reseat in init_iterator skips invisible
3143 text, so we might end up at a position different from POS. This
3144 is only a problem when POS is a row start after a newline and an
3145 overlay starts there with an after-string, and the overlay has an
3146 invisible property. Since we don't skip invisible text in
3147 display_line and elsewhere immediately after consuming the
3148 newline before the row start, such a POS will not be in a string,
3149 but the call to init_iterator below will move us to the
3150 after-string. */
3151 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3153 /* This only scans the current chunk -- it should scan all chunks.
3154 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3155 to 16 in 22.1 to make this a lesser problem. */
3156 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3158 const char *s = SSDATA (it->overlay_strings[i]);
3159 const char *e = s + SBYTES (it->overlay_strings[i]);
3161 while (s < e && *s != '\n')
3162 ++s;
3164 if (s < e)
3166 overlay_strings_with_newlines = true;
3167 break;
3171 /* If position is within an overlay string, set up IT to the right
3172 overlay string. */
3173 if (pos->overlay_string_index >= 0)
3175 int relative_index;
3177 /* If the first overlay string happens to have a `display'
3178 property for an image, the iterator will be set up for that
3179 image, and we have to undo that setup first before we can
3180 correct the overlay string index. */
3181 if (it->method == GET_FROM_IMAGE)
3182 pop_it (it);
3184 /* We already have the first chunk of overlay strings in
3185 IT->overlay_strings. Load more until the one for
3186 pos->overlay_string_index is in IT->overlay_strings. */
3187 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3189 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3190 it->current.overlay_string_index = 0;
3191 while (n--)
3193 load_overlay_strings (it, 0);
3194 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3198 it->current.overlay_string_index = pos->overlay_string_index;
3199 relative_index = (it->current.overlay_string_index
3200 % OVERLAY_STRING_CHUNK_SIZE);
3201 it->string = it->overlay_strings[relative_index];
3202 eassert (STRINGP (it->string));
3203 it->current.string_pos = pos->string_pos;
3204 it->method = GET_FROM_STRING;
3205 it->end_charpos = SCHARS (it->string);
3206 /* Set up the bidi iterator for this overlay string. */
3207 if (it->bidi_p)
3209 it->bidi_it.string.lstring = it->string;
3210 it->bidi_it.string.s = NULL;
3211 it->bidi_it.string.schars = SCHARS (it->string);
3212 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3213 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3214 it->bidi_it.string.unibyte = !it->multibyte_p;
3215 it->bidi_it.w = it->w;
3216 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3217 FRAME_WINDOW_P (it->f), &it->bidi_it);
3219 /* Synchronize the state of the bidi iterator with
3220 pos->string_pos. For any string position other than
3221 zero, this will be done automagically when we resume
3222 iteration over the string and get_visually_first_element
3223 is called. But if string_pos is zero, and the string is
3224 to be reordered for display, we need to resync manually,
3225 since it could be that the iteration state recorded in
3226 pos ended at string_pos of 0 moving backwards in string. */
3227 if (CHARPOS (pos->string_pos) == 0)
3229 get_visually_first_element (it);
3230 if (IT_STRING_CHARPOS (*it) != 0)
3231 do {
3232 /* Paranoia. */
3233 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3234 bidi_move_to_visually_next (&it->bidi_it);
3235 } while (it->bidi_it.charpos != 0);
3237 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3238 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3242 if (CHARPOS (pos->string_pos) >= 0)
3244 /* Recorded position is not in an overlay string, but in another
3245 string. This can only be a string from a `display' property.
3246 IT should already be filled with that string. */
3247 it->current.string_pos = pos->string_pos;
3248 eassert (STRINGP (it->string));
3249 if (it->bidi_p)
3250 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3251 FRAME_WINDOW_P (it->f), &it->bidi_it);
3254 /* Restore position in display vector translations, control
3255 character translations or ellipses. */
3256 if (pos->dpvec_index >= 0)
3258 if (it->dpvec == NULL)
3259 get_next_display_element (it);
3260 eassert (it->dpvec && it->current.dpvec_index == 0);
3261 it->current.dpvec_index = pos->dpvec_index;
3264 CHECK_IT (it);
3265 return !overlay_strings_with_newlines;
3269 /* Initialize IT for stepping through current_buffer in window W
3270 starting at ROW->start. */
3272 static void
3273 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3275 init_from_display_pos (it, w, &row->start);
3276 it->start = row->start;
3277 it->continuation_lines_width = row->continuation_lines_width;
3278 CHECK_IT (it);
3282 /* Initialize IT for stepping through current_buffer in window W
3283 starting in the line following ROW, i.e. starting at ROW->end.
3284 Value is false if there are overlay strings with newlines at ROW's
3285 end position. */
3287 static bool
3288 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3290 bool success = false;
3292 if (init_from_display_pos (it, w, &row->end))
3294 if (row->continued_p)
3295 it->continuation_lines_width
3296 = row->continuation_lines_width + row->pixel_width;
3297 CHECK_IT (it);
3298 success = true;
3301 return success;
3307 /***********************************************************************
3308 Text properties
3309 ***********************************************************************/
3311 /* Called when IT reaches IT->stop_charpos. Handle text property and
3312 overlay changes. Set IT->stop_charpos to the next position where
3313 to stop. */
3315 static void
3316 handle_stop (struct it *it)
3318 enum prop_handled handled;
3319 bool handle_overlay_change_p;
3320 struct props *p;
3322 it->dpvec = NULL;
3323 it->current.dpvec_index = -1;
3324 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3325 it->ellipsis_p = false;
3327 /* Use face of preceding text for ellipsis (if invisible) */
3328 if (it->selective_display_ellipsis_p)
3329 it->saved_face_id = it->face_id;
3331 /* Here's the description of the semantics of, and the logic behind,
3332 the various HANDLED_* statuses:
3334 HANDLED_NORMALLY means the handler did its job, and the loop
3335 should proceed to calling the next handler in order.
3337 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3338 change in the properties and overlays at current position, so the
3339 loop should be restarted, to re-invoke the handlers that were
3340 already called. This happens when fontification-functions were
3341 called by handle_fontified_prop, and actually fontified
3342 something. Another case where HANDLED_RECOMPUTE_PROPS is
3343 returned is when we discover overlay strings that need to be
3344 displayed right away. The loop below will continue for as long
3345 as the status is HANDLED_RECOMPUTE_PROPS.
3347 HANDLED_RETURN means return immediately to the caller, to
3348 continue iteration without calling any further handlers. This is
3349 used when we need to act on some property right away, for example
3350 when we need to display the ellipsis or a replacing display
3351 property, such as display string or image.
3353 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3354 consumed, and the handler switched to the next overlay string.
3355 This signals the loop below to refrain from looking for more
3356 overlays before all the overlay strings of the current overlay
3357 are processed.
3359 Some of the handlers called by the loop push the iterator state
3360 onto the stack (see 'push_it'), and arrange for the iteration to
3361 continue with another object, such as an image, a display string,
3362 or an overlay string. In most such cases, it->stop_charpos is
3363 set to the first character of the string, so that when the
3364 iteration resumes, this function will immediately be called
3365 again, to examine the properties at the beginning of the string.
3367 When a display or overlay string is exhausted, the iterator state
3368 is popped (see 'pop_it'), and iteration continues with the
3369 previous object. Again, in many such cases this function is
3370 called again to find the next position where properties might
3371 change. */
3375 handled = HANDLED_NORMALLY;
3377 /* Call text property handlers. */
3378 for (p = it_props; p->handler; ++p)
3380 handled = p->handler (it);
3382 if (handled == HANDLED_RECOMPUTE_PROPS)
3383 break;
3384 else if (handled == HANDLED_RETURN)
3386 /* We still want to show before and after strings from
3387 overlays even if the actual buffer text is replaced. */
3388 if (!handle_overlay_change_p
3389 || it->sp > 1
3390 /* Don't call get_overlay_strings_1 if we already
3391 have overlay strings loaded, because doing so
3392 will load them again and push the iterator state
3393 onto the stack one more time, which is not
3394 expected by the rest of the code that processes
3395 overlay strings. */
3396 || (it->current.overlay_string_index < 0
3397 && !get_overlay_strings_1 (it, 0, false)))
3399 if (it->ellipsis_p)
3400 setup_for_ellipsis (it, 0);
3401 /* When handling a display spec, we might load an
3402 empty string. In that case, discard it here. We
3403 used to discard it in handle_single_display_spec,
3404 but that causes get_overlay_strings_1, above, to
3405 ignore overlay strings that we must check. */
3406 if (STRINGP (it->string) && !SCHARS (it->string))
3407 pop_it (it);
3408 return;
3410 else if (STRINGP (it->string) && !SCHARS (it->string))
3411 pop_it (it);
3412 else
3414 it->string_from_display_prop_p = false;
3415 it->from_disp_prop_p = false;
3416 handle_overlay_change_p = false;
3418 handled = HANDLED_RECOMPUTE_PROPS;
3419 break;
3421 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3422 handle_overlay_change_p = false;
3425 if (handled != HANDLED_RECOMPUTE_PROPS)
3427 /* Don't check for overlay strings below when set to deliver
3428 characters from a display vector. */
3429 if (it->method == GET_FROM_DISPLAY_VECTOR)
3430 handle_overlay_change_p = false;
3432 /* Handle overlay changes.
3433 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3434 if it finds overlays. */
3435 if (handle_overlay_change_p)
3436 handled = handle_overlay_change (it);
3439 if (it->ellipsis_p)
3441 setup_for_ellipsis (it, 0);
3442 break;
3445 while (handled == HANDLED_RECOMPUTE_PROPS);
3447 /* Determine where to stop next. */
3448 if (handled == HANDLED_NORMALLY)
3449 compute_stop_pos (it);
3453 /* Compute IT->stop_charpos from text property and overlay change
3454 information for IT's current position. */
3456 static void
3457 compute_stop_pos (struct it *it)
3459 register INTERVAL iv, next_iv;
3460 Lisp_Object object, limit, position;
3461 ptrdiff_t charpos, bytepos;
3463 if (STRINGP (it->string))
3465 /* Strings are usually short, so don't limit the search for
3466 properties. */
3467 it->stop_charpos = it->end_charpos;
3468 object = it->string;
3469 limit = Qnil;
3470 charpos = IT_STRING_CHARPOS (*it);
3471 bytepos = IT_STRING_BYTEPOS (*it);
3473 else
3475 ptrdiff_t pos;
3477 /* If end_charpos is out of range for some reason, such as a
3478 misbehaving display function, rationalize it (Bug#5984). */
3479 if (it->end_charpos > ZV)
3480 it->end_charpos = ZV;
3481 it->stop_charpos = it->end_charpos;
3483 /* If next overlay change is in front of the current stop pos
3484 (which is IT->end_charpos), stop there. Note: value of
3485 next_overlay_change is point-max if no overlay change
3486 follows. */
3487 charpos = IT_CHARPOS (*it);
3488 bytepos = IT_BYTEPOS (*it);
3489 pos = next_overlay_change (charpos);
3490 if (pos < it->stop_charpos)
3491 it->stop_charpos = pos;
3493 /* Set up variables for computing the stop position from text
3494 property changes. */
3495 XSETBUFFER (object, current_buffer);
3496 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3499 /* Get the interval containing IT's position. Value is a null
3500 interval if there isn't such an interval. */
3501 position = make_number (charpos);
3502 iv = validate_interval_range (object, &position, &position, false);
3503 if (iv)
3505 Lisp_Object values_here[LAST_PROP_IDX];
3506 struct props *p;
3508 /* Get properties here. */
3509 for (p = it_props; p->handler; ++p)
3510 values_here[p->idx] = textget (iv->plist,
3511 builtin_lisp_symbol (p->name));
3513 /* Look for an interval following iv that has different
3514 properties. */
3515 for (next_iv = next_interval (iv);
3516 (next_iv
3517 && (NILP (limit)
3518 || XFASTINT (limit) > next_iv->position));
3519 next_iv = next_interval (next_iv))
3521 for (p = it_props; p->handler; ++p)
3523 Lisp_Object new_value = textget (next_iv->plist,
3524 builtin_lisp_symbol (p->name));
3525 if (!EQ (values_here[p->idx], new_value))
3526 break;
3529 if (p->handler)
3530 break;
3533 if (next_iv)
3535 if (INTEGERP (limit)
3536 && next_iv->position >= XFASTINT (limit))
3537 /* No text property change up to limit. */
3538 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3539 else
3540 /* Text properties change in next_iv. */
3541 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3545 if (it->cmp_it.id < 0)
3547 ptrdiff_t stoppos = it->end_charpos;
3549 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3550 stoppos = -1;
3551 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3552 stoppos, it->string);
3555 eassert (STRINGP (it->string)
3556 || (it->stop_charpos >= BEGV
3557 && it->stop_charpos >= IT_CHARPOS (*it)));
3561 /* Return the position of the next overlay change after POS in
3562 current_buffer. Value is point-max if no overlay change
3563 follows. This is like `next-overlay-change' but doesn't use
3564 xmalloc. */
3566 static ptrdiff_t
3567 next_overlay_change (ptrdiff_t pos)
3569 ptrdiff_t i, noverlays;
3570 ptrdiff_t endpos;
3571 Lisp_Object *overlays;
3572 USE_SAFE_ALLOCA;
3574 /* Get all overlays at the given position. */
3575 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3577 /* If any of these overlays ends before endpos,
3578 use its ending point instead. */
3579 for (i = 0; i < noverlays; ++i)
3581 Lisp_Object oend;
3582 ptrdiff_t oendpos;
3584 oend = OVERLAY_END (overlays[i]);
3585 oendpos = OVERLAY_POSITION (oend);
3586 endpos = min (endpos, oendpos);
3589 SAFE_FREE ();
3590 return endpos;
3593 /* How many characters forward to search for a display property or
3594 display string. Searching too far forward makes the bidi display
3595 sluggish, especially in small windows. */
3596 #define MAX_DISP_SCAN 250
3598 /* Return the character position of a display string at or after
3599 position specified by POSITION. If no display string exists at or
3600 after POSITION, return ZV. A display string is either an overlay
3601 with `display' property whose value is a string, or a `display'
3602 text property whose value is a string. STRING is data about the
3603 string to iterate; if STRING->lstring is nil, we are iterating a
3604 buffer. FRAME_WINDOW_P is true when we are displaying a window
3605 on a GUI frame. DISP_PROP is set to zero if we searched
3606 MAX_DISP_SCAN characters forward without finding any display
3607 strings, non-zero otherwise. It is set to 2 if the display string
3608 uses any kind of `(space ...)' spec that will produce a stretch of
3609 white space in the text area. */
3610 ptrdiff_t
3611 compute_display_string_pos (struct text_pos *position,
3612 struct bidi_string_data *string,
3613 struct window *w,
3614 bool frame_window_p, int *disp_prop)
3616 /* OBJECT = nil means current buffer. */
3617 Lisp_Object object, object1;
3618 Lisp_Object pos, spec, limpos;
3619 bool string_p = string && (STRINGP (string->lstring) || string->s);
3620 ptrdiff_t eob = string_p ? string->schars : ZV;
3621 ptrdiff_t begb = string_p ? 0 : BEGV;
3622 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3623 ptrdiff_t lim =
3624 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3625 struct text_pos tpos;
3626 int rv = 0;
3628 if (string && STRINGP (string->lstring))
3629 object1 = object = string->lstring;
3630 else if (w && !string_p)
3632 XSETWINDOW (object, w);
3633 object1 = Qnil;
3635 else
3636 object1 = object = Qnil;
3638 *disp_prop = 1;
3640 if (charpos >= eob
3641 /* We don't support display properties whose values are strings
3642 that have display string properties. */
3643 || string->from_disp_str
3644 /* C strings cannot have display properties. */
3645 || (string->s && !STRINGP (object)))
3647 *disp_prop = 0;
3648 return eob;
3651 /* If the character at CHARPOS is where the display string begins,
3652 return CHARPOS. */
3653 pos = make_number (charpos);
3654 if (STRINGP (object))
3655 bufpos = string->bufpos;
3656 else
3657 bufpos = charpos;
3658 tpos = *position;
3659 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3660 && (charpos <= begb
3661 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3662 object),
3663 spec))
3664 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3665 frame_window_p)))
3667 if (rv == 2)
3668 *disp_prop = 2;
3669 return charpos;
3672 /* Look forward for the first character with a `display' property
3673 that will replace the underlying text when displayed. */
3674 limpos = make_number (lim);
3675 do {
3676 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3677 CHARPOS (tpos) = XFASTINT (pos);
3678 if (CHARPOS (tpos) >= lim)
3680 *disp_prop = 0;
3681 break;
3683 if (STRINGP (object))
3684 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3685 else
3686 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3687 spec = Fget_char_property (pos, Qdisplay, object);
3688 if (!STRINGP (object))
3689 bufpos = CHARPOS (tpos);
3690 } while (NILP (spec)
3691 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3692 bufpos, frame_window_p)));
3693 if (rv == 2)
3694 *disp_prop = 2;
3696 return CHARPOS (tpos);
3699 /* Return the character position of the end of the display string that
3700 started at CHARPOS. If there's no display string at CHARPOS,
3701 return -1. A display string is either an overlay with `display'
3702 property whose value is a string or a `display' text property whose
3703 value is a string. */
3704 ptrdiff_t
3705 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3707 /* OBJECT = nil means current buffer. */
3708 Lisp_Object object =
3709 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3710 Lisp_Object pos = make_number (charpos);
3711 ptrdiff_t eob =
3712 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3714 if (charpos >= eob || (string->s && !STRINGP (object)))
3715 return eob;
3717 /* It could happen that the display property or overlay was removed
3718 since we found it in compute_display_string_pos above. One way
3719 this can happen is if JIT font-lock was called (through
3720 handle_fontified_prop), and jit-lock-functions remove text
3721 properties or overlays from the portion of buffer that includes
3722 CHARPOS. Muse mode is known to do that, for example. In this
3723 case, we return -1 to the caller, to signal that no display
3724 string is actually present at CHARPOS. See bidi_fetch_char for
3725 how this is handled.
3727 An alternative would be to never look for display properties past
3728 it->stop_charpos. But neither compute_display_string_pos nor
3729 bidi_fetch_char that calls it know or care where the next
3730 stop_charpos is. */
3731 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3732 return -1;
3734 /* Look forward for the first character where the `display' property
3735 changes. */
3736 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3738 return XFASTINT (pos);
3743 /***********************************************************************
3744 Fontification
3745 ***********************************************************************/
3747 /* Handle changes in the `fontified' property of the current buffer by
3748 calling hook functions from Qfontification_functions to fontify
3749 regions of text. */
3751 static enum prop_handled
3752 handle_fontified_prop (struct it *it)
3754 Lisp_Object prop, pos;
3755 enum prop_handled handled = HANDLED_NORMALLY;
3757 if (!NILP (Vmemory_full))
3758 return handled;
3760 /* Get the value of the `fontified' property at IT's current buffer
3761 position. (The `fontified' property doesn't have a special
3762 meaning in strings.) If the value is nil, call functions from
3763 Qfontification_functions. */
3764 if (!STRINGP (it->string)
3765 && it->s == NULL
3766 && !NILP (Vfontification_functions)
3767 && !NILP (Vrun_hooks)
3768 && (pos = make_number (IT_CHARPOS (*it)),
3769 prop = Fget_char_property (pos, Qfontified, Qnil),
3770 /* Ignore the special cased nil value always present at EOB since
3771 no amount of fontifying will be able to change it. */
3772 NILP (prop) && IT_CHARPOS (*it) < Z))
3774 ptrdiff_t count = SPECPDL_INDEX ();
3775 Lisp_Object val;
3776 struct buffer *obuf = current_buffer;
3777 ptrdiff_t begv = BEGV, zv = ZV;
3778 bool old_clip_changed = current_buffer->clip_changed;
3780 val = Vfontification_functions;
3781 specbind (Qfontification_functions, Qnil);
3783 eassert (it->end_charpos == ZV);
3785 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3786 safe_call1 (val, pos);
3787 else
3789 Lisp_Object fns, fn;
3791 fns = Qnil;
3793 for (; CONSP (val); val = XCDR (val))
3795 fn = XCAR (val);
3797 if (EQ (fn, Qt))
3799 /* A value of t indicates this hook has a local
3800 binding; it means to run the global binding too.
3801 In a global value, t should not occur. If it
3802 does, we must ignore it to avoid an endless
3803 loop. */
3804 for (fns = Fdefault_value (Qfontification_functions);
3805 CONSP (fns);
3806 fns = XCDR (fns))
3808 fn = XCAR (fns);
3809 if (!EQ (fn, Qt))
3810 safe_call1 (fn, pos);
3813 else
3814 safe_call1 (fn, pos);
3818 unbind_to (count, Qnil);
3820 /* Fontification functions routinely call `save-restriction'.
3821 Normally, this tags clip_changed, which can confuse redisplay
3822 (see discussion in Bug#6671). Since we don't perform any
3823 special handling of fontification changes in the case where
3824 `save-restriction' isn't called, there's no point doing so in
3825 this case either. So, if the buffer's restrictions are
3826 actually left unchanged, reset clip_changed. */
3827 if (obuf == current_buffer)
3829 if (begv == BEGV && zv == ZV)
3830 current_buffer->clip_changed = old_clip_changed;
3832 /* There isn't much we can reasonably do to protect against
3833 misbehaving fontification, but here's a fig leaf. */
3834 else if (BUFFER_LIVE_P (obuf))
3835 set_buffer_internal_1 (obuf);
3837 /* The fontification code may have added/removed text.
3838 It could do even a lot worse, but let's at least protect against
3839 the most obvious case where only the text past `pos' gets changed',
3840 as is/was done in grep.el where some escapes sequences are turned
3841 into face properties (bug#7876). */
3842 it->end_charpos = ZV;
3844 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3845 something. This avoids an endless loop if they failed to
3846 fontify the text for which reason ever. */
3847 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3848 handled = HANDLED_RECOMPUTE_PROPS;
3851 return handled;
3856 /***********************************************************************
3857 Faces
3858 ***********************************************************************/
3860 /* Set up iterator IT from face properties at its current position.
3861 Called from handle_stop. */
3863 static enum prop_handled
3864 handle_face_prop (struct it *it)
3866 int new_face_id;
3867 ptrdiff_t next_stop;
3869 if (!STRINGP (it->string))
3871 new_face_id
3872 = face_at_buffer_position (it->w,
3873 IT_CHARPOS (*it),
3874 &next_stop,
3875 (IT_CHARPOS (*it)
3876 + TEXT_PROP_DISTANCE_LIMIT),
3877 false, it->base_face_id);
3879 /* Is this a start of a run of characters with box face?
3880 Caveat: this can be called for a freshly initialized
3881 iterator; face_id is -1 in this case. We know that the new
3882 face will not change until limit, i.e. if the new face has a
3883 box, all characters up to limit will have one. But, as
3884 usual, we don't know whether limit is really the end. */
3885 if (new_face_id != it->face_id)
3887 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3888 /* If it->face_id is -1, old_face below will be NULL, see
3889 the definition of FACE_FROM_ID_OR_NULL. This will happen
3890 if this is the initial call that gets the face. */
3891 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3893 /* If the value of face_id of the iterator is -1, we have to
3894 look in front of IT's position and see whether there is a
3895 face there that's different from new_face_id. */
3896 if (!old_face && IT_CHARPOS (*it) > BEG)
3898 int prev_face_id = face_before_it_pos (it);
3900 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3903 /* If the new face has a box, but the old face does not,
3904 this is the start of a run of characters with box face,
3905 i.e. this character has a shadow on the left side. */
3906 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3907 && (old_face == NULL || !old_face->box));
3908 it->face_box_p = new_face->box != FACE_NO_BOX;
3911 else
3913 int base_face_id;
3914 ptrdiff_t bufpos;
3915 int i;
3916 Lisp_Object from_overlay
3917 = (it->current.overlay_string_index >= 0
3918 ? it->string_overlays[it->current.overlay_string_index
3919 % OVERLAY_STRING_CHUNK_SIZE]
3920 : Qnil);
3922 /* See if we got to this string directly or indirectly from
3923 an overlay property. That includes the before-string or
3924 after-string of an overlay, strings in display properties
3925 provided by an overlay, their text properties, etc.
3927 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3928 if (! NILP (from_overlay))
3929 for (i = it->sp - 1; i >= 0; i--)
3931 if (it->stack[i].current.overlay_string_index >= 0)
3932 from_overlay
3933 = it->string_overlays[it->stack[i].current.overlay_string_index
3934 % OVERLAY_STRING_CHUNK_SIZE];
3935 else if (! NILP (it->stack[i].from_overlay))
3936 from_overlay = it->stack[i].from_overlay;
3938 if (!NILP (from_overlay))
3939 break;
3942 if (! NILP (from_overlay))
3944 bufpos = IT_CHARPOS (*it);
3945 /* For a string from an overlay, the base face depends
3946 only on text properties and ignores overlays. */
3947 base_face_id
3948 = face_for_overlay_string (it->w,
3949 IT_CHARPOS (*it),
3950 &next_stop,
3951 (IT_CHARPOS (*it)
3952 + TEXT_PROP_DISTANCE_LIMIT),
3953 false,
3954 from_overlay);
3956 else
3958 bufpos = 0;
3960 /* For strings from a `display' property, use the face at
3961 IT's current buffer position as the base face to merge
3962 with, so that overlay strings appear in the same face as
3963 surrounding text, unless they specify their own faces.
3964 For strings from wrap-prefix and line-prefix properties,
3965 use the default face, possibly remapped via
3966 Vface_remapping_alist. */
3967 /* Note that the fact that we use the face at _buffer_
3968 position means that a 'display' property on an overlay
3969 string will not inherit the face of that overlay string,
3970 but will instead revert to the face of buffer text
3971 covered by the overlay. This is visible, e.g., when the
3972 overlay specifies a box face, but neither the buffer nor
3973 the display string do. This sounds like a design bug,
3974 but Emacs always did that since v21.1, so changing that
3975 might be a big deal. */
3976 base_face_id = it->string_from_prefix_prop_p
3977 ? (!NILP (Vface_remapping_alist)
3978 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3979 : DEFAULT_FACE_ID)
3980 : underlying_face_id (it);
3983 new_face_id = face_at_string_position (it->w,
3984 it->string,
3985 IT_STRING_CHARPOS (*it),
3986 bufpos,
3987 &next_stop,
3988 base_face_id, false);
3990 /* Is this a start of a run of characters with box? Caveat:
3991 this can be called for a freshly allocated iterator; face_id
3992 is -1 is this case. We know that the new face will not
3993 change until the next check pos, i.e. if the new face has a
3994 box, all characters up to that position will have a
3995 box. But, as usual, we don't know whether that position
3996 is really the end. */
3997 if (new_face_id != it->face_id)
3999 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4000 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4002 /* If new face has a box but old face hasn't, this is the
4003 start of a run of characters with box, i.e. it has a
4004 shadow on the left side. */
4005 it->start_of_box_run_p
4006 = new_face->box && (old_face == NULL || !old_face->box);
4007 it->face_box_p = new_face->box != FACE_NO_BOX;
4011 it->face_id = new_face_id;
4012 return HANDLED_NORMALLY;
4016 /* Return the ID of the face ``underlying'' IT's current position,
4017 which is in a string. If the iterator is associated with a
4018 buffer, return the face at IT's current buffer position.
4019 Otherwise, use the iterator's base_face_id. */
4021 static int
4022 underlying_face_id (struct it *it)
4024 int face_id = it->base_face_id, i;
4026 eassert (STRINGP (it->string));
4028 for (i = it->sp - 1; i >= 0; --i)
4029 if (NILP (it->stack[i].string))
4030 face_id = it->stack[i].face_id;
4032 return face_id;
4036 /* Compute the face one character before or after the current position
4037 of IT, in the visual order. BEFORE_P means get the face
4038 in front (to the left in L2R paragraphs, to the right in R2L
4039 paragraphs) of IT's screen position. Value is the ID of the face. */
4041 static int
4042 face_before_or_after_it_pos (struct it *it, bool before_p)
4044 int face_id, limit;
4045 ptrdiff_t next_check_charpos;
4046 struct it it_copy;
4047 void *it_copy_data = NULL;
4049 eassert (it->s == NULL);
4051 if (STRINGP (it->string))
4053 ptrdiff_t bufpos, charpos;
4054 int base_face_id;
4056 /* No face change past the end of the string (for the case
4057 we are padding with spaces). No face change before the
4058 string start. */
4059 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4060 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4061 return it->face_id;
4063 if (!it->bidi_p)
4065 /* Set charpos to the position before or after IT's current
4066 position, in the logical order, which in the non-bidi
4067 case is the same as the visual order. */
4068 if (before_p)
4069 charpos = IT_STRING_CHARPOS (*it) - 1;
4070 else if (it->what == IT_COMPOSITION)
4071 /* For composition, we must check the character after the
4072 composition. */
4073 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4074 else
4075 charpos = IT_STRING_CHARPOS (*it) + 1;
4077 else
4079 if (before_p)
4081 /* With bidi iteration, the character before the current
4082 in the visual order cannot be found by simple
4083 iteration, because "reverse" reordering is not
4084 supported. Instead, we need to start from the string
4085 beginning and go all the way to the current string
4086 position, remembering the previous position. */
4087 /* Ignore face changes before the first visible
4088 character on this display line. */
4089 if (it->current_x <= it->first_visible_x)
4090 return it->face_id;
4091 SAVE_IT (it_copy, *it, it_copy_data);
4092 IT_STRING_CHARPOS (it_copy) = 0;
4093 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4097 charpos = IT_STRING_CHARPOS (it_copy);
4098 if (charpos >= SCHARS (it->string))
4099 break;
4100 bidi_move_to_visually_next (&it_copy.bidi_it);
4102 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4104 RESTORE_IT (it, it, it_copy_data);
4106 else
4108 /* Set charpos to the string position of the character
4109 that comes after IT's current position in the visual
4110 order. */
4111 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4113 it_copy = *it;
4114 while (n--)
4115 bidi_move_to_visually_next (&it_copy.bidi_it);
4117 charpos = it_copy.bidi_it.charpos;
4120 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4122 if (it->current.overlay_string_index >= 0)
4123 bufpos = IT_CHARPOS (*it);
4124 else
4125 bufpos = 0;
4127 base_face_id = underlying_face_id (it);
4129 /* Get the face for ASCII, or unibyte. */
4130 face_id = face_at_string_position (it->w,
4131 it->string,
4132 charpos,
4133 bufpos,
4134 &next_check_charpos,
4135 base_face_id, false);
4137 /* Correct the face for charsets different from ASCII. Do it
4138 for the multibyte case only. The face returned above is
4139 suitable for unibyte text if IT->string is unibyte. */
4140 if (STRING_MULTIBYTE (it->string))
4142 struct text_pos pos1 = string_pos (charpos, it->string);
4143 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4144 int c, len;
4145 struct face *face = FACE_FROM_ID (it->f, face_id);
4147 c = string_char_and_length (p, &len);
4148 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4151 else
4153 struct text_pos pos;
4155 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4156 || (IT_CHARPOS (*it) <= BEGV && before_p))
4157 return it->face_id;
4159 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4160 pos = it->current.pos;
4162 if (!it->bidi_p)
4164 if (before_p)
4165 DEC_TEXT_POS (pos, it->multibyte_p);
4166 else
4168 if (it->what == IT_COMPOSITION)
4170 /* For composition, we must check the position after
4171 the composition. */
4172 pos.charpos += it->cmp_it.nchars;
4173 pos.bytepos += it->len;
4175 else
4176 INC_TEXT_POS (pos, it->multibyte_p);
4179 else
4181 if (before_p)
4183 int current_x;
4185 /* With bidi iteration, the character before the current
4186 in the visual order cannot be found by simple
4187 iteration, because "reverse" reordering is not
4188 supported. Instead, we need to use the move_it_*
4189 family of functions, and move to the previous
4190 character starting from the beginning of the visual
4191 line. */
4192 /* Ignore face changes before the first visible
4193 character on this display line. */
4194 if (it->current_x <= it->first_visible_x)
4195 return it->face_id;
4196 SAVE_IT (it_copy, *it, it_copy_data);
4197 /* Implementation note: Since move_it_in_display_line
4198 works in the iterator geometry, and thinks the first
4199 character is always the leftmost, even in R2L lines,
4200 we don't need to distinguish between the R2L and L2R
4201 cases here. */
4202 current_x = it_copy.current_x;
4203 move_it_vertically_backward (&it_copy, 0);
4204 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4205 pos = it_copy.current.pos;
4206 RESTORE_IT (it, it, it_copy_data);
4208 else
4210 /* Set charpos to the buffer position of the character
4211 that comes after IT's current position in the visual
4212 order. */
4213 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4215 it_copy = *it;
4216 while (n--)
4217 bidi_move_to_visually_next (&it_copy.bidi_it);
4219 SET_TEXT_POS (pos,
4220 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4223 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4225 /* Determine face for CHARSET_ASCII, or unibyte. */
4226 face_id = face_at_buffer_position (it->w,
4227 CHARPOS (pos),
4228 &next_check_charpos,
4229 limit, false, -1);
4231 /* Correct the face for charsets different from ASCII. Do it
4232 for the multibyte case only. The face returned above is
4233 suitable for unibyte text if current_buffer is unibyte. */
4234 if (it->multibyte_p)
4236 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4237 struct face *face = FACE_FROM_ID (it->f, face_id);
4238 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4242 return face_id;
4247 /***********************************************************************
4248 Invisible text
4249 ***********************************************************************/
4251 /* Set up iterator IT from invisible properties at its current
4252 position. Called from handle_stop. */
4254 static enum prop_handled
4255 handle_invisible_prop (struct it *it)
4257 enum prop_handled handled = HANDLED_NORMALLY;
4258 int invis;
4259 Lisp_Object prop;
4261 if (STRINGP (it->string))
4263 Lisp_Object end_charpos, limit;
4265 /* Get the value of the invisible text property at the
4266 current position. Value will be nil if there is no such
4267 property. */
4268 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4269 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4270 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4272 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4274 /* Record whether we have to display an ellipsis for the
4275 invisible text. */
4276 bool display_ellipsis_p = (invis == 2);
4277 ptrdiff_t len, endpos;
4279 handled = HANDLED_RECOMPUTE_PROPS;
4281 /* Get the position at which the next visible text can be
4282 found in IT->string, if any. */
4283 endpos = len = SCHARS (it->string);
4284 XSETINT (limit, len);
4287 end_charpos
4288 = Fnext_single_property_change (end_charpos, Qinvisible,
4289 it->string, limit);
4290 /* Since LIMIT is always an integer, so should be the
4291 value returned by Fnext_single_property_change. */
4292 eassert (INTEGERP (end_charpos));
4293 if (INTEGERP (end_charpos))
4295 endpos = XFASTINT (end_charpos);
4296 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4297 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4298 if (invis == 2)
4299 display_ellipsis_p = true;
4301 else /* Should never happen; but if it does, exit the loop. */
4302 endpos = len;
4304 while (invis != 0 && endpos < len);
4306 if (display_ellipsis_p)
4307 it->ellipsis_p = true;
4309 if (endpos < len)
4311 /* Text at END_CHARPOS is visible. Move IT there. */
4312 struct text_pos old;
4313 ptrdiff_t oldpos;
4315 old = it->current.string_pos;
4316 oldpos = CHARPOS (old);
4317 if (it->bidi_p)
4319 if (it->bidi_it.first_elt
4320 && it->bidi_it.charpos < SCHARS (it->string))
4321 bidi_paragraph_init (it->paragraph_embedding,
4322 &it->bidi_it, true);
4323 /* Bidi-iterate out of the invisible text. */
4326 bidi_move_to_visually_next (&it->bidi_it);
4328 while (oldpos <= it->bidi_it.charpos
4329 && it->bidi_it.charpos < endpos);
4331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4332 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4333 if (IT_CHARPOS (*it) >= endpos)
4334 it->prev_stop = endpos;
4336 else
4338 IT_STRING_CHARPOS (*it) = endpos;
4339 compute_string_pos (&it->current.string_pos, old, it->string);
4342 else
4344 /* The rest of the string is invisible. If this is an
4345 overlay string, proceed with the next overlay string
4346 or whatever comes and return a character from there. */
4347 if (it->current.overlay_string_index >= 0
4348 && !display_ellipsis_p)
4350 next_overlay_string (it);
4351 /* Don't check for overlay strings when we just
4352 finished processing them. */
4353 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4355 else
4357 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4358 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4363 else
4365 ptrdiff_t newpos, next_stop, start_charpos, tem;
4366 Lisp_Object pos, overlay;
4368 /* First of all, is there invisible text at this position? */
4369 tem = start_charpos = IT_CHARPOS (*it);
4370 pos = make_number (tem);
4371 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4372 &overlay);
4373 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4375 /* If we are on invisible text, skip over it. */
4376 if (invis != 0 && start_charpos < it->end_charpos)
4378 /* Record whether we have to display an ellipsis for the
4379 invisible text. */
4380 bool display_ellipsis_p = invis == 2;
4382 handled = HANDLED_RECOMPUTE_PROPS;
4384 /* Loop skipping over invisible text. The loop is left at
4385 ZV or with IT on the first char being visible again. */
4388 /* Try to skip some invisible text. Return value is the
4389 position reached which can be equal to where we start
4390 if there is nothing invisible there. This skips both
4391 over invisible text properties and overlays with
4392 invisible property. */
4393 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4395 /* If we skipped nothing at all we weren't at invisible
4396 text in the first place. If everything to the end of
4397 the buffer was skipped, end the loop. */
4398 if (newpos == tem || newpos >= ZV)
4399 invis = 0;
4400 else
4402 /* We skipped some characters but not necessarily
4403 all there are. Check if we ended up on visible
4404 text. Fget_char_property returns the property of
4405 the char before the given position, i.e. if we
4406 get invis = 0, this means that the char at
4407 newpos is visible. */
4408 pos = make_number (newpos);
4409 prop = Fget_char_property (pos, Qinvisible, it->window);
4410 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4413 /* If we ended up on invisible text, proceed to
4414 skip starting with next_stop. */
4415 if (invis != 0)
4416 tem = next_stop;
4418 /* If there are adjacent invisible texts, don't lose the
4419 second one's ellipsis. */
4420 if (invis == 2)
4421 display_ellipsis_p = true;
4423 while (invis != 0);
4425 /* The position newpos is now either ZV or on visible text. */
4426 if (it->bidi_p)
4428 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4429 bool on_newline
4430 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4431 bool after_newline
4432 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4434 /* If the invisible text ends on a newline or on a
4435 character after a newline, we can avoid the costly,
4436 character by character, bidi iteration to NEWPOS, and
4437 instead simply reseat the iterator there. That's
4438 because all bidi reordering information is tossed at
4439 the newline. This is a big win for modes that hide
4440 complete lines, like Outline, Org, etc. */
4441 if (on_newline || after_newline)
4443 struct text_pos tpos;
4444 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4446 SET_TEXT_POS (tpos, newpos, bpos);
4447 reseat_1 (it, tpos, false);
4448 /* If we reseat on a newline/ZV, we need to prep the
4449 bidi iterator for advancing to the next character
4450 after the newline/EOB, keeping the current paragraph
4451 direction (so that PRODUCE_GLYPHS does TRT wrt
4452 prepending/appending glyphs to a glyph row). */
4453 if (on_newline)
4455 it->bidi_it.first_elt = false;
4456 it->bidi_it.paragraph_dir = pdir;
4457 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4458 it->bidi_it.nchars = 1;
4459 it->bidi_it.ch_len = 1;
4462 else /* Must use the slow method. */
4464 /* With bidi iteration, the region of invisible text
4465 could start and/or end in the middle of a
4466 non-base embedding level. Therefore, we need to
4467 skip invisible text using the bidi iterator,
4468 starting at IT's current position, until we find
4469 ourselves outside of the invisible text.
4470 Skipping invisible text _after_ bidi iteration
4471 avoids affecting the visual order of the
4472 displayed text when invisible properties are
4473 added or removed. */
4474 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4476 /* If we were `reseat'ed to a new paragraph,
4477 determine the paragraph base direction. We
4478 need to do it now because
4479 next_element_from_buffer may not have a
4480 chance to do it, if we are going to skip any
4481 text at the beginning, which resets the
4482 FIRST_ELT flag. */
4483 bidi_paragraph_init (it->paragraph_embedding,
4484 &it->bidi_it, true);
4488 bidi_move_to_visually_next (&it->bidi_it);
4490 while (it->stop_charpos <= it->bidi_it.charpos
4491 && it->bidi_it.charpos < newpos);
4492 IT_CHARPOS (*it) = it->bidi_it.charpos;
4493 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4494 /* If we overstepped NEWPOS, record its position in
4495 the iterator, so that we skip invisible text if
4496 later the bidi iteration lands us in the
4497 invisible region again. */
4498 if (IT_CHARPOS (*it) >= newpos)
4499 it->prev_stop = newpos;
4502 else
4504 IT_CHARPOS (*it) = newpos;
4505 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4508 if (display_ellipsis_p)
4510 /* Make sure that the glyphs of the ellipsis will get
4511 correct `charpos' values. If we would not update
4512 it->position here, the glyphs would belong to the
4513 last visible character _before_ the invisible
4514 text, which confuses `set_cursor_from_row'.
4516 We use the last invisible position instead of the
4517 first because this way the cursor is always drawn on
4518 the first "." of the ellipsis, whenever PT is inside
4519 the invisible text. Otherwise the cursor would be
4520 placed _after_ the ellipsis when the point is after the
4521 first invisible character. */
4522 if (!STRINGP (it->object))
4524 it->position.charpos = newpos - 1;
4525 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4529 /* If there are before-strings at the start of invisible
4530 text, and the text is invisible because of a text
4531 property, arrange to show before-strings because 20.x did
4532 it that way. (If the text is invisible because of an
4533 overlay property instead of a text property, this is
4534 already handled in the overlay code.) */
4535 if (NILP (overlay)
4536 && get_overlay_strings (it, it->stop_charpos))
4538 handled = HANDLED_RECOMPUTE_PROPS;
4539 if (it->sp > 0)
4541 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4542 /* The call to get_overlay_strings above recomputes
4543 it->stop_charpos, but it only considers changes
4544 in properties and overlays beyond iterator's
4545 current position. This causes us to miss changes
4546 that happen exactly where the invisible property
4547 ended. So we play it safe here and force the
4548 iterator to check for potential stop positions
4549 immediately after the invisible text. Note that
4550 if get_overlay_strings returns true, it
4551 normally also pushed the iterator stack, so we
4552 need to update the stop position in the slot
4553 below the current one. */
4554 it->stack[it->sp - 1].stop_charpos
4555 = CHARPOS (it->stack[it->sp - 1].current.pos);
4558 else if (display_ellipsis_p)
4560 it->ellipsis_p = true;
4561 /* Let the ellipsis display before
4562 considering any properties of the following char.
4563 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4564 handled = HANDLED_RETURN;
4569 return handled;
4573 /* Make iterator IT return `...' next.
4574 Replaces LEN characters from buffer. */
4576 static void
4577 setup_for_ellipsis (struct it *it, int len)
4579 /* Use the display table definition for `...'. Invalid glyphs
4580 will be handled by the method returning elements from dpvec. */
4581 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4583 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4584 it->dpvec = v->contents;
4585 it->dpend = v->contents + v->header.size;
4587 else
4589 /* Default `...'. */
4590 it->dpvec = default_invis_vector;
4591 it->dpend = default_invis_vector + 3;
4594 it->dpvec_char_len = len;
4595 it->current.dpvec_index = 0;
4596 it->dpvec_face_id = -1;
4598 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4599 face as the preceding text. IT->saved_face_id was set in
4600 handle_stop to the face of the preceding character, and will be
4601 different from IT->face_id only if the invisible text skipped in
4602 handle_invisible_prop has some non-default face on its first
4603 character. We thus ignore the face of the invisible text when we
4604 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4605 if (it->saved_face_id >= 0)
4606 it->face_id = it->saved_face_id;
4608 /* If the ellipsis represents buffer text, it means we advanced in
4609 the buffer, so we should no longer ignore overlay strings. */
4610 if (it->method == GET_FROM_BUFFER)
4611 it->ignore_overlay_strings_at_pos_p = false;
4613 it->method = GET_FROM_DISPLAY_VECTOR;
4614 it->ellipsis_p = true;
4619 /***********************************************************************
4620 'display' property
4621 ***********************************************************************/
4623 /* Set up iterator IT from `display' property at its current position.
4624 Called from handle_stop.
4625 We return HANDLED_RETURN if some part of the display property
4626 overrides the display of the buffer text itself.
4627 Otherwise we return HANDLED_NORMALLY. */
4629 static enum prop_handled
4630 handle_display_prop (struct it *it)
4632 Lisp_Object propval, object, overlay;
4633 struct text_pos *position;
4634 ptrdiff_t bufpos;
4635 /* Nonzero if some property replaces the display of the text itself. */
4636 int display_replaced = 0;
4638 if (STRINGP (it->string))
4640 object = it->string;
4641 position = &it->current.string_pos;
4642 bufpos = CHARPOS (it->current.pos);
4644 else
4646 XSETWINDOW (object, it->w);
4647 position = &it->current.pos;
4648 bufpos = CHARPOS (*position);
4651 /* Reset those iterator values set from display property values. */
4652 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4653 it->space_width = Qnil;
4654 it->font_height = Qnil;
4655 it->voffset = 0;
4657 /* We don't support recursive `display' properties, i.e. string
4658 values that have a string `display' property, that have a string
4659 `display' property etc. */
4660 if (!it->string_from_display_prop_p)
4661 it->area = TEXT_AREA;
4663 propval = get_char_property_and_overlay (make_number (position->charpos),
4664 Qdisplay, object, &overlay);
4665 if (NILP (propval))
4666 return HANDLED_NORMALLY;
4667 /* Now OVERLAY is the overlay that gave us this property, or nil
4668 if it was a text property. */
4670 if (!STRINGP (it->string))
4671 object = it->w->contents;
4673 display_replaced = handle_display_spec (it, propval, object, overlay,
4674 position, bufpos,
4675 FRAME_WINDOW_P (it->f));
4676 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4679 /* Subroutine of handle_display_prop. Returns non-zero if the display
4680 specification in SPEC is a replacing specification, i.e. it would
4681 replace the text covered by `display' property with something else,
4682 such as an image or a display string. If SPEC includes any kind or
4683 `(space ...) specification, the value is 2; this is used by
4684 compute_display_string_pos, which see.
4686 See handle_single_display_spec for documentation of arguments.
4687 FRAME_WINDOW_P is true if the window being redisplayed is on a
4688 GUI frame; this argument is used only if IT is NULL, see below.
4690 IT can be NULL, if this is called by the bidi reordering code
4691 through compute_display_string_pos, which see. In that case, this
4692 function only examines SPEC, but does not otherwise "handle" it, in
4693 the sense that it doesn't set up members of IT from the display
4694 spec. */
4695 static int
4696 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4697 Lisp_Object overlay, struct text_pos *position,
4698 ptrdiff_t bufpos, bool frame_window_p)
4700 int replacing = 0;
4702 if (CONSP (spec)
4703 /* Simple specifications. */
4704 && !EQ (XCAR (spec), Qimage)
4705 #ifdef HAVE_XWIDGETS
4706 && !EQ (XCAR (spec), Qxwidget)
4707 #endif
4708 && !EQ (XCAR (spec), Qspace)
4709 && !EQ (XCAR (spec), Qwhen)
4710 && !EQ (XCAR (spec), Qslice)
4711 && !EQ (XCAR (spec), Qspace_width)
4712 && !EQ (XCAR (spec), Qheight)
4713 && !EQ (XCAR (spec), Qraise)
4714 /* Marginal area specifications. */
4715 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4716 && !EQ (XCAR (spec), Qleft_fringe)
4717 && !EQ (XCAR (spec), Qright_fringe)
4718 && !NILP (XCAR (spec)))
4720 for (; CONSP (spec); spec = XCDR (spec))
4722 int rv = handle_single_display_spec (it, XCAR (spec), object,
4723 overlay, position, bufpos,
4724 replacing, frame_window_p);
4725 if (rv != 0)
4727 replacing = rv;
4728 /* If some text in a string is replaced, `position' no
4729 longer points to the position of `object'. */
4730 if (!it || STRINGP (object))
4731 break;
4735 else if (VECTORP (spec))
4737 ptrdiff_t i;
4738 for (i = 0; i < ASIZE (spec); ++i)
4740 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4741 overlay, position, bufpos,
4742 replacing, frame_window_p);
4743 if (rv != 0)
4745 replacing = rv;
4746 /* If some text in a string is replaced, `position' no
4747 longer points to the position of `object'. */
4748 if (!it || STRINGP (object))
4749 break;
4753 else
4754 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4755 bufpos, 0, frame_window_p);
4756 return replacing;
4759 /* Value is the position of the end of the `display' property starting
4760 at START_POS in OBJECT. */
4762 static struct text_pos
4763 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4765 Lisp_Object end;
4766 struct text_pos end_pos;
4768 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4769 Qdisplay, object, Qnil);
4770 CHARPOS (end_pos) = XFASTINT (end);
4771 if (STRINGP (object))
4772 compute_string_pos (&end_pos, start_pos, it->string);
4773 else
4774 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4776 return end_pos;
4780 /* Set up IT from a single `display' property specification SPEC. OBJECT
4781 is the object in which the `display' property was found. *POSITION
4782 is the position in OBJECT at which the `display' property was found.
4783 BUFPOS is the buffer position of OBJECT (different from POSITION if
4784 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4785 previously saw a display specification which already replaced text
4786 display with something else, for example an image; we ignore such
4787 properties after the first one has been processed.
4789 OVERLAY is the overlay this `display' property came from,
4790 or nil if it was a text property.
4792 If SPEC is a `space' or `image' specification, and in some other
4793 cases too, set *POSITION to the position where the `display'
4794 property ends.
4796 If IT is NULL, only examine the property specification in SPEC, but
4797 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4798 is intended to be displayed in a window on a GUI frame.
4800 Value is non-zero if something was found which replaces the display
4801 of buffer or string text. */
4803 static int
4804 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4805 Lisp_Object overlay, struct text_pos *position,
4806 ptrdiff_t bufpos, int display_replaced,
4807 bool frame_window_p)
4809 Lisp_Object form;
4810 Lisp_Object location, value;
4811 struct text_pos start_pos = *position;
4813 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4814 If the result is non-nil, use VALUE instead of SPEC. */
4815 form = Qt;
4816 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4818 spec = XCDR (spec);
4819 if (!CONSP (spec))
4820 return 0;
4821 form = XCAR (spec);
4822 spec = XCDR (spec);
4825 if (!NILP (form) && !EQ (form, Qt))
4827 ptrdiff_t count = SPECPDL_INDEX ();
4829 /* Bind `object' to the object having the `display' property, a
4830 buffer or string. Bind `position' to the position in the
4831 object where the property was found, and `buffer-position'
4832 to the current position in the buffer. */
4834 if (NILP (object))
4835 XSETBUFFER (object, current_buffer);
4836 specbind (Qobject, object);
4837 specbind (Qposition, make_number (CHARPOS (*position)));
4838 specbind (Qbuffer_position, make_number (bufpos));
4839 form = safe_eval (form);
4840 unbind_to (count, Qnil);
4843 if (NILP (form))
4844 return 0;
4846 /* Handle `(height HEIGHT)' specifications. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qheight)
4849 && CONSP (XCDR (spec)))
4851 if (it)
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4856 it->font_height = XCAR (XCDR (spec));
4857 if (!NILP (it->font_height))
4859 int new_height = -1;
4861 if (CONSP (it->font_height)
4862 && (EQ (XCAR (it->font_height), Qplus)
4863 || EQ (XCAR (it->font_height), Qminus))
4864 && CONSP (XCDR (it->font_height))
4865 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4867 /* `(+ N)' or `(- N)' where N is an integer. */
4868 int steps = XINT (XCAR (XCDR (it->font_height)));
4869 if (EQ (XCAR (it->font_height), Qplus))
4870 steps = - steps;
4871 it->face_id = smaller_face (it->f, it->face_id, steps);
4873 else if (FUNCTIONP (it->font_height))
4875 /* Call function with current height as argument.
4876 Value is the new height. */
4877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4878 Lisp_Object height;
4879 height = safe_call1 (it->font_height,
4880 face->lface[LFACE_HEIGHT_INDEX]);
4881 if (NUMBERP (height))
4882 new_height = XFLOATINT (height);
4884 else if (NUMBERP (it->font_height))
4886 /* Value is a multiple of the canonical char height. */
4887 struct face *f;
4889 f = FACE_FROM_ID (it->f,
4890 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4891 new_height = (XFLOATINT (it->font_height)
4892 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4894 else
4896 /* Evaluate IT->font_height with `height' bound to the
4897 current specified height to get the new height. */
4898 ptrdiff_t count = SPECPDL_INDEX ();
4899 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4901 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4902 value = safe_eval (it->font_height);
4903 unbind_to (count, Qnil);
4905 if (NUMBERP (value))
4906 new_height = XFLOATINT (value);
4909 if (new_height > 0)
4910 it->face_id = face_with_height (it->f, it->face_id, new_height);
4914 return 0;
4917 /* Handle `(space-width WIDTH)'. */
4918 if (CONSP (spec)
4919 && EQ (XCAR (spec), Qspace_width)
4920 && CONSP (XCDR (spec)))
4922 if (it)
4924 if (!FRAME_WINDOW_P (it->f))
4925 return 0;
4927 value = XCAR (XCDR (spec));
4928 if (NUMBERP (value) && XFLOATINT (value) > 0)
4929 it->space_width = value;
4932 return 0;
4935 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4936 if (CONSP (spec)
4937 && EQ (XCAR (spec), Qslice))
4939 Lisp_Object tem;
4941 if (it)
4943 if (!FRAME_WINDOW_P (it->f))
4944 return 0;
4946 if (tem = XCDR (spec), CONSP (tem))
4948 it->slice.x = XCAR (tem);
4949 if (tem = XCDR (tem), CONSP (tem))
4951 it->slice.y = XCAR (tem);
4952 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.width = XCAR (tem);
4955 if (tem = XCDR (tem), CONSP (tem))
4956 it->slice.height = XCAR (tem);
4962 return 0;
4965 /* Handle `(raise FACTOR)'. */
4966 if (CONSP (spec)
4967 && EQ (XCAR (spec), Qraise)
4968 && CONSP (XCDR (spec)))
4970 if (it)
4972 if (!FRAME_WINDOW_P (it->f))
4973 return 0;
4975 #ifdef HAVE_WINDOW_SYSTEM
4976 value = XCAR (XCDR (spec));
4977 if (NUMBERP (value))
4979 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4980 it->voffset = - (XFLOATINT (value)
4981 * (normal_char_height (face->font, -1)));
4983 #endif /* HAVE_WINDOW_SYSTEM */
4986 return 0;
4989 /* Don't handle the other kinds of display specifications
4990 inside a string that we got from a `display' property. */
4991 if (it && it->string_from_display_prop_p)
4992 return 0;
4994 /* Characters having this form of property are not displayed, so
4995 we have to find the end of the property. */
4996 if (it)
4998 start_pos = *position;
4999 *position = display_prop_end (it, object, start_pos);
5000 /* If the display property comes from an overlay, don't consider
5001 any potential stop_charpos values before the end of that
5002 overlay. Since display_prop_end will happily find another
5003 'display' property coming from some other overlay or text
5004 property on buffer positions before this overlay's end, we
5005 need to ignore them, or else we risk displaying this
5006 overlay's display string/image twice. */
5007 if (!NILP (overlay))
5009 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5011 if (ovendpos > CHARPOS (*position))
5012 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5015 value = Qnil;
5017 /* Stop the scan at that end position--we assume that all
5018 text properties change there. */
5019 if (it)
5020 it->stop_charpos = position->charpos;
5022 /* Handle `(left-fringe BITMAP [FACE])'
5023 and `(right-fringe BITMAP [FACE])'. */
5024 if (CONSP (spec)
5025 && (EQ (XCAR (spec), Qleft_fringe)
5026 || EQ (XCAR (spec), Qright_fringe))
5027 && CONSP (XCDR (spec)))
5029 if (it)
5031 if (!FRAME_WINDOW_P (it->f))
5032 /* If we return here, POSITION has been advanced
5033 across the text with this property. */
5035 /* Synchronize the bidi iterator with POSITION. This is
5036 needed because we are not going to push the iterator
5037 on behalf of this display property, so there will be
5038 no pop_it call to do this synchronization for us. */
5039 if (it->bidi_p)
5041 it->position = *position;
5042 iterate_out_of_display_property (it);
5043 *position = it->position;
5045 return 1;
5048 else if (!frame_window_p)
5049 return 1;
5051 #ifdef HAVE_WINDOW_SYSTEM
5052 value = XCAR (XCDR (spec));
5053 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5054 if (! fringe_bitmap)
5055 /* If we return here, POSITION has been advanced
5056 across the text with this property. */
5058 if (it && it->bidi_p)
5060 it->position = *position;
5061 iterate_out_of_display_property (it);
5062 *position = it->position;
5064 return 1;
5067 if (it)
5069 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5071 if (CONSP (XCDR (XCDR (spec))))
5073 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5074 int face_id2 = lookup_derived_face (it->f, face_name,
5075 FRINGE_FACE_ID, false);
5076 if (face_id2 >= 0)
5077 face_id = face_id2;
5080 /* Save current settings of IT so that we can restore them
5081 when we are finished with the glyph property value. */
5082 push_it (it, position);
5084 it->area = TEXT_AREA;
5085 it->what = IT_IMAGE;
5086 it->image_id = -1; /* no image */
5087 it->position = start_pos;
5088 it->object = NILP (object) ? it->w->contents : object;
5089 it->method = GET_FROM_IMAGE;
5090 it->from_overlay = Qnil;
5091 it->face_id = face_id;
5092 it->from_disp_prop_p = true;
5094 /* Say that we haven't consumed the characters with
5095 `display' property yet. The call to pop_it in
5096 set_iterator_to_next will clean this up. */
5097 *position = start_pos;
5099 if (EQ (XCAR (spec), Qleft_fringe))
5101 it->left_user_fringe_bitmap = fringe_bitmap;
5102 it->left_user_fringe_face_id = face_id;
5104 else
5106 it->right_user_fringe_bitmap = fringe_bitmap;
5107 it->right_user_fringe_face_id = face_id;
5110 #endif /* HAVE_WINDOW_SYSTEM */
5111 return 1;
5114 /* Prepare to handle `((margin left-margin) ...)',
5115 `((margin right-margin) ...)' and `((margin nil) ...)'
5116 prefixes for display specifications. */
5117 location = Qunbound;
5118 if (CONSP (spec) && CONSP (XCAR (spec)))
5120 Lisp_Object tem;
5122 value = XCDR (spec);
5123 if (CONSP (value))
5124 value = XCAR (value);
5126 tem = XCAR (spec);
5127 if (EQ (XCAR (tem), Qmargin)
5128 && (tem = XCDR (tem),
5129 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5130 (NILP (tem)
5131 || EQ (tem, Qleft_margin)
5132 || EQ (tem, Qright_margin))))
5133 location = tem;
5136 if (EQ (location, Qunbound))
5138 location = Qnil;
5139 value = spec;
5142 /* After this point, VALUE is the property after any
5143 margin prefix has been stripped. It must be a string,
5144 an image specification, or `(space ...)'.
5146 LOCATION specifies where to display: `left-margin',
5147 `right-margin' or nil. */
5149 bool valid_p = (STRINGP (value)
5150 #ifdef HAVE_WINDOW_SYSTEM
5151 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5152 && valid_image_p (value))
5153 #endif /* not HAVE_WINDOW_SYSTEM */
5154 || (CONSP (value) && EQ (XCAR (value), Qspace))
5155 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5156 && valid_xwidget_spec_p (value)));
5158 if (valid_p && display_replaced == 0)
5160 int retval = 1;
5162 if (!it)
5164 /* Callers need to know whether the display spec is any kind
5165 of `(space ...)' spec that is about to affect text-area
5166 display. */
5167 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5168 retval = 2;
5169 return retval;
5172 /* Save current settings of IT so that we can restore them
5173 when we are finished with the glyph property value. */
5174 push_it (it, position);
5175 it->from_overlay = overlay;
5176 it->from_disp_prop_p = true;
5178 if (NILP (location))
5179 it->area = TEXT_AREA;
5180 else if (EQ (location, Qleft_margin))
5181 it->area = LEFT_MARGIN_AREA;
5182 else
5183 it->area = RIGHT_MARGIN_AREA;
5185 if (STRINGP (value))
5187 it->string = value;
5188 it->multibyte_p = STRING_MULTIBYTE (it->string);
5189 it->current.overlay_string_index = -1;
5190 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5191 it->end_charpos = it->string_nchars = SCHARS (it->string);
5192 it->method = GET_FROM_STRING;
5193 it->stop_charpos = 0;
5194 it->prev_stop = 0;
5195 it->base_level_stop = 0;
5196 it->string_from_display_prop_p = true;
5197 /* Say that we haven't consumed the characters with
5198 `display' property yet. The call to pop_it in
5199 set_iterator_to_next will clean this up. */
5200 if (BUFFERP (object))
5201 *position = start_pos;
5203 /* Force paragraph direction to be that of the parent
5204 object. If the parent object's paragraph direction is
5205 not yet determined, default to L2R. */
5206 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5207 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5208 else
5209 it->paragraph_embedding = L2R;
5211 /* Set up the bidi iterator for this display string. */
5212 if (it->bidi_p)
5214 it->bidi_it.string.lstring = it->string;
5215 it->bidi_it.string.s = NULL;
5216 it->bidi_it.string.schars = it->end_charpos;
5217 it->bidi_it.string.bufpos = bufpos;
5218 it->bidi_it.string.from_disp_str = true;
5219 it->bidi_it.string.unibyte = !it->multibyte_p;
5220 it->bidi_it.w = it->w;
5221 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5224 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5226 it->method = GET_FROM_STRETCH;
5227 it->object = value;
5228 *position = it->position = start_pos;
5229 retval = 1 + (it->area == TEXT_AREA);
5231 else if (valid_xwidget_spec_p (value))
5233 it->what = IT_XWIDGET;
5234 it->method = GET_FROM_XWIDGET;
5235 it->position = start_pos;
5236 it->object = NILP (object) ? it->w->contents : object;
5237 *position = start_pos;
5238 it->xwidget = lookup_xwidget (value);
5240 #ifdef HAVE_WINDOW_SYSTEM
5241 else
5243 it->what = IT_IMAGE;
5244 it->image_id = lookup_image (it->f, value);
5245 it->position = start_pos;
5246 it->object = NILP (object) ? it->w->contents : object;
5247 it->method = GET_FROM_IMAGE;
5249 /* Say that we haven't consumed the characters with
5250 `display' property yet. The call to pop_it in
5251 set_iterator_to_next will clean this up. */
5252 *position = start_pos;
5254 #endif /* HAVE_WINDOW_SYSTEM */
5256 return retval;
5259 /* Invalid property or property not supported. Restore
5260 POSITION to what it was before. */
5261 *position = start_pos;
5262 return 0;
5265 /* Check if PROP is a display property value whose text should be
5266 treated as intangible. OVERLAY is the overlay from which PROP
5267 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5268 specify the buffer position covered by PROP. */
5270 bool
5271 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5272 ptrdiff_t charpos, ptrdiff_t bytepos)
5274 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5275 struct text_pos position;
5277 SET_TEXT_POS (position, charpos, bytepos);
5278 return (handle_display_spec (NULL, prop, Qnil, overlay,
5279 &position, charpos, frame_window_p)
5280 != 0);
5284 /* Return true if PROP is a display sub-property value containing STRING.
5286 Implementation note: this and the following function are really
5287 special cases of handle_display_spec and
5288 handle_single_display_spec, and should ideally use the same code.
5289 Until they do, these two pairs must be consistent and must be
5290 modified in sync. */
5292 static bool
5293 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5295 if (EQ (string, prop))
5296 return true;
5298 /* Skip over `when FORM'. */
5299 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5301 prop = XCDR (prop);
5302 if (!CONSP (prop))
5303 return false;
5304 /* Actually, the condition following `when' should be eval'ed,
5305 like handle_single_display_spec does, and we should return
5306 false if it evaluates to nil. However, this function is
5307 called only when the buffer was already displayed and some
5308 glyph in the glyph matrix was found to come from a display
5309 string. Therefore, the condition was already evaluated, and
5310 the result was non-nil, otherwise the display string wouldn't
5311 have been displayed and we would have never been called for
5312 this property. Thus, we can skip the evaluation and assume
5313 its result is non-nil. */
5314 prop = XCDR (prop);
5317 if (CONSP (prop))
5318 /* Skip over `margin LOCATION'. */
5319 if (EQ (XCAR (prop), Qmargin))
5321 prop = XCDR (prop);
5322 if (!CONSP (prop))
5323 return false;
5325 prop = XCDR (prop);
5326 if (!CONSP (prop))
5327 return false;
5330 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5334 /* Return true if STRING appears in the `display' property PROP. */
5336 static bool
5337 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5339 if (CONSP (prop)
5340 && !EQ (XCAR (prop), Qwhen)
5341 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5343 /* A list of sub-properties. */
5344 while (CONSP (prop))
5346 if (single_display_spec_string_p (XCAR (prop), string))
5347 return true;
5348 prop = XCDR (prop);
5351 else if (VECTORP (prop))
5353 /* A vector of sub-properties. */
5354 ptrdiff_t i;
5355 for (i = 0; i < ASIZE (prop); ++i)
5356 if (single_display_spec_string_p (AREF (prop, i), string))
5357 return true;
5359 else
5360 return single_display_spec_string_p (prop, string);
5362 return false;
5365 /* Look for STRING in overlays and text properties in the current
5366 buffer, between character positions FROM and TO (excluding TO).
5367 BACK_P means look back (in this case, TO is supposed to be
5368 less than FROM).
5369 Value is the first character position where STRING was found, or
5370 zero if it wasn't found before hitting TO.
5372 This function may only use code that doesn't eval because it is
5373 called asynchronously from note_mouse_highlight. */
5375 static ptrdiff_t
5376 string_buffer_position_lim (Lisp_Object string,
5377 ptrdiff_t from, ptrdiff_t to, bool back_p)
5379 Lisp_Object limit, prop, pos;
5380 bool found = false;
5382 pos = make_number (max (from, BEGV));
5384 if (!back_p) /* looking forward */
5386 limit = make_number (min (to, ZV));
5387 while (!found && !EQ (pos, limit))
5389 prop = Fget_char_property (pos, Qdisplay, Qnil);
5390 if (!NILP (prop) && display_prop_string_p (prop, string))
5391 found = true;
5392 else
5393 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5394 limit);
5397 else /* looking back */
5399 limit = make_number (max (to, BEGV));
5400 while (!found && !EQ (pos, limit))
5402 prop = Fget_char_property (pos, Qdisplay, Qnil);
5403 if (!NILP (prop) && display_prop_string_p (prop, string))
5404 found = true;
5405 else
5406 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5407 limit);
5411 return found ? XINT (pos) : 0;
5414 /* Determine which buffer position in current buffer STRING comes from.
5415 AROUND_CHARPOS is an approximate position where it could come from.
5416 Value is the buffer position or 0 if it couldn't be determined.
5418 This function is necessary because we don't record buffer positions
5419 in glyphs generated from strings (to keep struct glyph small).
5420 This function may only use code that doesn't eval because it is
5421 called asynchronously from note_mouse_highlight. */
5423 static ptrdiff_t
5424 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5426 const int MAX_DISTANCE = 1000;
5427 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5428 around_charpos + MAX_DISTANCE,
5429 false);
5431 if (!found)
5432 found = string_buffer_position_lim (string, around_charpos,
5433 around_charpos - MAX_DISTANCE, true);
5434 return found;
5439 /***********************************************************************
5440 `composition' property
5441 ***********************************************************************/
5443 /* Set up iterator IT from `composition' property at its current
5444 position. Called from handle_stop. */
5446 static enum prop_handled
5447 handle_composition_prop (struct it *it)
5449 Lisp_Object prop, string;
5450 ptrdiff_t pos, pos_byte, start, end;
5452 if (STRINGP (it->string))
5454 unsigned char *s;
5456 pos = IT_STRING_CHARPOS (*it);
5457 pos_byte = IT_STRING_BYTEPOS (*it);
5458 string = it->string;
5459 s = SDATA (string) + pos_byte;
5460 it->c = STRING_CHAR (s);
5462 else
5464 pos = IT_CHARPOS (*it);
5465 pos_byte = IT_BYTEPOS (*it);
5466 string = Qnil;
5467 it->c = FETCH_CHAR (pos_byte);
5470 /* If there's a valid composition and point is not inside of the
5471 composition (in the case that the composition is from the current
5472 buffer), draw a glyph composed from the composition components. */
5473 if (find_composition (pos, -1, &start, &end, &prop, string)
5474 && composition_valid_p (start, end, prop)
5475 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5477 if (start < pos)
5478 /* As we can't handle this situation (perhaps font-lock added
5479 a new composition), we just return here hoping that next
5480 redisplay will detect this composition much earlier. */
5481 return HANDLED_NORMALLY;
5482 if (start != pos)
5484 if (STRINGP (it->string))
5485 pos_byte = string_char_to_byte (it->string, start);
5486 else
5487 pos_byte = CHAR_TO_BYTE (start);
5489 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5490 prop, string);
5492 if (it->cmp_it.id >= 0)
5494 it->cmp_it.ch = -1;
5495 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5496 it->cmp_it.nglyphs = -1;
5500 return HANDLED_NORMALLY;
5505 /***********************************************************************
5506 Overlay strings
5507 ***********************************************************************/
5509 /* The following structure is used to record overlay strings for
5510 later sorting in load_overlay_strings. */
5512 struct overlay_entry
5514 Lisp_Object overlay;
5515 Lisp_Object string;
5516 EMACS_INT priority;
5517 bool after_string_p;
5521 /* Set up iterator IT from overlay strings at its current position.
5522 Called from handle_stop. */
5524 static enum prop_handled
5525 handle_overlay_change (struct it *it)
5527 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5528 return HANDLED_RECOMPUTE_PROPS;
5529 else
5530 return HANDLED_NORMALLY;
5534 /* Set up the next overlay string for delivery by IT, if there is an
5535 overlay string to deliver. Called by set_iterator_to_next when the
5536 end of the current overlay string is reached. If there are more
5537 overlay strings to display, IT->string and
5538 IT->current.overlay_string_index are set appropriately here.
5539 Otherwise IT->string is set to nil. */
5541 static void
5542 next_overlay_string (struct it *it)
5544 ++it->current.overlay_string_index;
5545 if (it->current.overlay_string_index == it->n_overlay_strings)
5547 /* No more overlay strings. Restore IT's settings to what
5548 they were before overlay strings were processed, and
5549 continue to deliver from current_buffer. */
5551 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5552 pop_it (it);
5553 eassert (it->sp > 0
5554 || (NILP (it->string)
5555 && it->method == GET_FROM_BUFFER
5556 && it->stop_charpos >= BEGV
5557 && it->stop_charpos <= it->end_charpos));
5558 it->current.overlay_string_index = -1;
5559 it->n_overlay_strings = 0;
5560 /* If there's an empty display string on the stack, pop the
5561 stack, to resync the bidi iterator with IT's position. Such
5562 empty strings are pushed onto the stack in
5563 get_overlay_strings_1. */
5564 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5565 pop_it (it);
5567 /* Since we've exhausted overlay strings at this buffer
5568 position, set the flag to ignore overlays until we move to
5569 another position. The flag is reset in
5570 next_element_from_buffer. */
5571 it->ignore_overlay_strings_at_pos_p = true;
5573 /* If we're at the end of the buffer, record that we have
5574 processed the overlay strings there already, so that
5575 next_element_from_buffer doesn't try it again. */
5576 if (NILP (it->string)
5577 && IT_CHARPOS (*it) >= it->end_charpos
5578 && it->overlay_strings_charpos >= it->end_charpos)
5579 it->overlay_strings_at_end_processed_p = true;
5580 /* Note: we reset overlay_strings_charpos only here, to make
5581 sure the just-processed overlays were indeed at EOB.
5582 Otherwise, overlays on text with invisible text property,
5583 which are processed with IT's position past the invisible
5584 text, might fool us into thinking the overlays at EOB were
5585 already processed (linum-mode can cause this, for
5586 example). */
5587 it->overlay_strings_charpos = -1;
5589 else
5591 /* There are more overlay strings to process. If
5592 IT->current.overlay_string_index has advanced to a position
5593 where we must load IT->overlay_strings with more strings, do
5594 it. We must load at the IT->overlay_strings_charpos where
5595 IT->n_overlay_strings was originally computed; when invisible
5596 text is present, this might not be IT_CHARPOS (Bug#7016). */
5597 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5599 if (it->current.overlay_string_index && i == 0)
5600 load_overlay_strings (it, it->overlay_strings_charpos);
5602 /* Initialize IT to deliver display elements from the overlay
5603 string. */
5604 it->string = it->overlay_strings[i];
5605 it->multibyte_p = STRING_MULTIBYTE (it->string);
5606 SET_TEXT_POS (it->current.string_pos, 0, 0);
5607 it->method = GET_FROM_STRING;
5608 it->stop_charpos = 0;
5609 it->end_charpos = SCHARS (it->string);
5610 if (it->cmp_it.stop_pos >= 0)
5611 it->cmp_it.stop_pos = 0;
5612 it->prev_stop = 0;
5613 it->base_level_stop = 0;
5615 /* Set up the bidi iterator for this overlay string. */
5616 if (it->bidi_p)
5618 it->bidi_it.string.lstring = it->string;
5619 it->bidi_it.string.s = NULL;
5620 it->bidi_it.string.schars = SCHARS (it->string);
5621 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5622 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5623 it->bidi_it.string.unibyte = !it->multibyte_p;
5624 it->bidi_it.w = it->w;
5625 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5629 CHECK_IT (it);
5633 /* Compare two overlay_entry structures E1 and E2. Used as a
5634 comparison function for qsort in load_overlay_strings. Overlay
5635 strings for the same position are sorted so that
5637 1. All after-strings come in front of before-strings, except
5638 when they come from the same overlay.
5640 2. Within after-strings, strings are sorted so that overlay strings
5641 from overlays with higher priorities come first.
5643 2. Within before-strings, strings are sorted so that overlay
5644 strings from overlays with higher priorities come last.
5646 Value is analogous to strcmp. */
5649 static int
5650 compare_overlay_entries (const void *e1, const void *e2)
5652 struct overlay_entry const *entry1 = e1;
5653 struct overlay_entry const *entry2 = e2;
5654 int result;
5656 if (entry1->after_string_p != entry2->after_string_p)
5658 /* Let after-strings appear in front of before-strings if
5659 they come from different overlays. */
5660 if (EQ (entry1->overlay, entry2->overlay))
5661 result = entry1->after_string_p ? 1 : -1;
5662 else
5663 result = entry1->after_string_p ? -1 : 1;
5665 else if (entry1->priority != entry2->priority)
5667 if (entry1->after_string_p)
5668 /* After-strings sorted in order of decreasing priority. */
5669 result = entry2->priority < entry1->priority ? -1 : 1;
5670 else
5671 /* Before-strings sorted in order of increasing priority. */
5672 result = entry1->priority < entry2->priority ? -1 : 1;
5674 else
5675 result = 0;
5677 return result;
5681 /* Load the vector IT->overlay_strings with overlay strings from IT's
5682 current buffer position, or from CHARPOS if that is > 0. Set
5683 IT->n_overlays to the total number of overlay strings found.
5685 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5686 a time. On entry into load_overlay_strings,
5687 IT->current.overlay_string_index gives the number of overlay
5688 strings that have already been loaded by previous calls to this
5689 function.
5691 IT->add_overlay_start contains an additional overlay start
5692 position to consider for taking overlay strings from, if non-zero.
5693 This position comes into play when the overlay has an `invisible'
5694 property, and both before and after-strings. When we've skipped to
5695 the end of the overlay, because of its `invisible' property, we
5696 nevertheless want its before-string to appear.
5697 IT->add_overlay_start will contain the overlay start position
5698 in this case.
5700 Overlay strings are sorted so that after-string strings come in
5701 front of before-string strings. Within before and after-strings,
5702 strings are sorted by overlay priority. See also function
5703 compare_overlay_entries. */
5705 static void
5706 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5708 Lisp_Object overlay, window, str, invisible;
5709 struct Lisp_Overlay *ov;
5710 ptrdiff_t start, end;
5711 ptrdiff_t n = 0, i, j;
5712 int invis;
5713 struct overlay_entry entriesbuf[20];
5714 ptrdiff_t size = ARRAYELTS (entriesbuf);
5715 struct overlay_entry *entries = entriesbuf;
5716 USE_SAFE_ALLOCA;
5718 if (charpos <= 0)
5719 charpos = IT_CHARPOS (*it);
5721 /* Append the overlay string STRING of overlay OVERLAY to vector
5722 `entries' which has size `size' and currently contains `n'
5723 elements. AFTER_P means STRING is an after-string of
5724 OVERLAY. */
5725 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5726 do \
5728 Lisp_Object priority; \
5730 if (n == size) \
5732 struct overlay_entry *old = entries; \
5733 SAFE_NALLOCA (entries, 2, size); \
5734 memcpy (entries, old, size * sizeof *entries); \
5735 size *= 2; \
5738 entries[n].string = (STRING); \
5739 entries[n].overlay = (OVERLAY); \
5740 priority = Foverlay_get ((OVERLAY), Qpriority); \
5741 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5742 entries[n].after_string_p = (AFTER_P); \
5743 ++n; \
5745 while (false)
5747 /* Process overlay before the overlay center. */
5748 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5750 XSETMISC (overlay, ov);
5751 eassert (OVERLAYP (overlay));
5752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5755 if (end < charpos)
5756 break;
5758 /* Skip this overlay if it doesn't start or end at IT's current
5759 position. */
5760 if (end != charpos && start != charpos)
5761 continue;
5763 /* Skip this overlay if it doesn't apply to IT->w. */
5764 window = Foverlay_get (overlay, Qwindow);
5765 if (WINDOWP (window) && XWINDOW (window) != it->w)
5766 continue;
5768 /* If the text ``under'' the overlay is invisible, both before-
5769 and after-strings from this overlay are visible; start and
5770 end position are indistinguishable. */
5771 invisible = Foverlay_get (overlay, Qinvisible);
5772 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5774 /* If overlay has a non-empty before-string, record it. */
5775 if ((start == charpos || (end == charpos && invis != 0))
5776 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5777 && SCHARS (str))
5778 RECORD_OVERLAY_STRING (overlay, str, false);
5780 /* If overlay has a non-empty after-string, record it. */
5781 if ((end == charpos || (start == charpos && invis != 0))
5782 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5783 && SCHARS (str))
5784 RECORD_OVERLAY_STRING (overlay, str, true);
5787 /* Process overlays after the overlay center. */
5788 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5790 XSETMISC (overlay, ov);
5791 eassert (OVERLAYP (overlay));
5792 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5793 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5795 if (start > charpos)
5796 break;
5798 /* Skip this overlay if it doesn't start or end at IT's current
5799 position. */
5800 if (end != charpos && start != charpos)
5801 continue;
5803 /* Skip this overlay if it doesn't apply to IT->w. */
5804 window = Foverlay_get (overlay, Qwindow);
5805 if (WINDOWP (window) && XWINDOW (window) != it->w)
5806 continue;
5808 /* If the text ``under'' the overlay is invisible, it has a zero
5809 dimension, and both before- and after-strings apply. */
5810 invisible = Foverlay_get (overlay, Qinvisible);
5811 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5813 /* If overlay has a non-empty before-string, record it. */
5814 if ((start == charpos || (end == charpos && invis != 0))
5815 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5816 && SCHARS (str))
5817 RECORD_OVERLAY_STRING (overlay, str, false);
5819 /* If overlay has a non-empty after-string, record it. */
5820 if ((end == charpos || (start == charpos && invis != 0))
5821 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5822 && SCHARS (str))
5823 RECORD_OVERLAY_STRING (overlay, str, true);
5826 #undef RECORD_OVERLAY_STRING
5828 /* Sort entries. */
5829 if (n > 1)
5830 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5832 /* Record number of overlay strings, and where we computed it. */
5833 it->n_overlay_strings = n;
5834 it->overlay_strings_charpos = charpos;
5836 /* IT->current.overlay_string_index is the number of overlay strings
5837 that have already been consumed by IT. Copy some of the
5838 remaining overlay strings to IT->overlay_strings. */
5839 i = 0;
5840 j = it->current.overlay_string_index;
5841 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5843 it->overlay_strings[i] = entries[j].string;
5844 it->string_overlays[i++] = entries[j++].overlay;
5847 CHECK_IT (it);
5848 SAFE_FREE ();
5852 /* Get the first chunk of overlay strings at IT's current buffer
5853 position, or at CHARPOS if that is > 0. Value is true if at
5854 least one overlay string was found. */
5856 static bool
5857 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5859 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5860 process. This fills IT->overlay_strings with strings, and sets
5861 IT->n_overlay_strings to the total number of strings to process.
5862 IT->pos.overlay_string_index has to be set temporarily to zero
5863 because load_overlay_strings needs this; it must be set to -1
5864 when no overlay strings are found because a zero value would
5865 indicate a position in the first overlay string. */
5866 it->current.overlay_string_index = 0;
5867 load_overlay_strings (it, charpos);
5869 /* If we found overlay strings, set up IT to deliver display
5870 elements from the first one. Otherwise set up IT to deliver
5871 from current_buffer. */
5872 if (it->n_overlay_strings)
5874 /* Make sure we know settings in current_buffer, so that we can
5875 restore meaningful values when we're done with the overlay
5876 strings. */
5877 if (compute_stop_p)
5878 compute_stop_pos (it);
5879 eassert (it->face_id >= 0);
5881 /* Save IT's settings. They are restored after all overlay
5882 strings have been processed. */
5883 eassert (!compute_stop_p || it->sp == 0);
5885 /* When called from handle_stop, there might be an empty display
5886 string loaded. In that case, don't bother saving it. But
5887 don't use this optimization with the bidi iterator, since we
5888 need the corresponding pop_it call to resync the bidi
5889 iterator's position with IT's position, after we are done
5890 with the overlay strings. (The corresponding call to pop_it
5891 in case of an empty display string is in
5892 next_overlay_string.) */
5893 if (!(!it->bidi_p
5894 && STRINGP (it->string) && !SCHARS (it->string)))
5895 push_it (it, NULL);
5897 /* Set up IT to deliver display elements from the first overlay
5898 string. */
5899 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5900 it->string = it->overlay_strings[0];
5901 it->from_overlay = Qnil;
5902 it->stop_charpos = 0;
5903 eassert (STRINGP (it->string));
5904 it->end_charpos = SCHARS (it->string);
5905 it->prev_stop = 0;
5906 it->base_level_stop = 0;
5907 it->multibyte_p = STRING_MULTIBYTE (it->string);
5908 it->method = GET_FROM_STRING;
5909 it->from_disp_prop_p = 0;
5911 /* Force paragraph direction to be that of the parent
5912 buffer. */
5913 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5914 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5915 else
5916 it->paragraph_embedding = L2R;
5918 /* Set up the bidi iterator for this overlay string. */
5919 if (it->bidi_p)
5921 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5923 it->bidi_it.string.lstring = it->string;
5924 it->bidi_it.string.s = NULL;
5925 it->bidi_it.string.schars = SCHARS (it->string);
5926 it->bidi_it.string.bufpos = pos;
5927 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5928 it->bidi_it.string.unibyte = !it->multibyte_p;
5929 it->bidi_it.w = it->w;
5930 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5932 return true;
5935 it->current.overlay_string_index = -1;
5936 return false;
5939 static bool
5940 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5942 it->string = Qnil;
5943 it->method = GET_FROM_BUFFER;
5945 get_overlay_strings_1 (it, charpos, true);
5947 CHECK_IT (it);
5949 /* Value is true if we found at least one overlay string. */
5950 return STRINGP (it->string);
5955 /***********************************************************************
5956 Saving and restoring state
5957 ***********************************************************************/
5959 /* Save current settings of IT on IT->stack. Called, for example,
5960 before setting up IT for an overlay string, to be able to restore
5961 IT's settings to what they were after the overlay string has been
5962 processed. If POSITION is non-NULL, it is the position to save on
5963 the stack instead of IT->position. */
5965 static void
5966 push_it (struct it *it, struct text_pos *position)
5968 struct iterator_stack_entry *p;
5970 eassert (it->sp < IT_STACK_SIZE);
5971 p = it->stack + it->sp;
5973 p->stop_charpos = it->stop_charpos;
5974 p->prev_stop = it->prev_stop;
5975 p->base_level_stop = it->base_level_stop;
5976 p->cmp_it = it->cmp_it;
5977 eassert (it->face_id >= 0);
5978 p->face_id = it->face_id;
5979 p->string = it->string;
5980 p->method = it->method;
5981 p->from_overlay = it->from_overlay;
5982 switch (p->method)
5984 case GET_FROM_IMAGE:
5985 p->u.image.object = it->object;
5986 p->u.image.image_id = it->image_id;
5987 p->u.image.slice = it->slice;
5988 break;
5989 case GET_FROM_STRETCH:
5990 p->u.stretch.object = it->object;
5991 break;
5992 case GET_FROM_XWIDGET:
5993 p->u.xwidget.object = it->object;
5994 break;
5995 case GET_FROM_BUFFER:
5996 case GET_FROM_DISPLAY_VECTOR:
5997 case GET_FROM_STRING:
5998 case GET_FROM_C_STRING:
5999 break;
6000 default:
6001 emacs_abort ();
6003 p->position = position ? *position : it->position;
6004 p->current = it->current;
6005 p->end_charpos = it->end_charpos;
6006 p->string_nchars = it->string_nchars;
6007 p->area = it->area;
6008 p->multibyte_p = it->multibyte_p;
6009 p->avoid_cursor_p = it->avoid_cursor_p;
6010 p->space_width = it->space_width;
6011 p->font_height = it->font_height;
6012 p->voffset = it->voffset;
6013 p->string_from_display_prop_p = it->string_from_display_prop_p;
6014 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6015 p->display_ellipsis_p = false;
6016 p->line_wrap = it->line_wrap;
6017 p->bidi_p = it->bidi_p;
6018 p->paragraph_embedding = it->paragraph_embedding;
6019 p->from_disp_prop_p = it->from_disp_prop_p;
6020 ++it->sp;
6022 /* Save the state of the bidi iterator as well. */
6023 if (it->bidi_p)
6024 bidi_push_it (&it->bidi_it);
6027 static void
6028 iterate_out_of_display_property (struct it *it)
6030 bool buffer_p = !STRINGP (it->string);
6031 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6032 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6034 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6036 /* Maybe initialize paragraph direction. If we are at the beginning
6037 of a new paragraph, next_element_from_buffer may not have a
6038 chance to do that. */
6039 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6040 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6041 /* prev_stop can be zero, so check against BEGV as well. */
6042 while (it->bidi_it.charpos >= bob
6043 && it->prev_stop <= it->bidi_it.charpos
6044 && it->bidi_it.charpos < CHARPOS (it->position)
6045 && it->bidi_it.charpos < eob)
6046 bidi_move_to_visually_next (&it->bidi_it);
6047 /* Record the stop_pos we just crossed, for when we cross it
6048 back, maybe. */
6049 if (it->bidi_it.charpos > CHARPOS (it->position))
6050 it->prev_stop = CHARPOS (it->position);
6051 /* If we ended up not where pop_it put us, resync IT's
6052 positional members with the bidi iterator. */
6053 if (it->bidi_it.charpos != CHARPOS (it->position))
6054 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6055 if (buffer_p)
6056 it->current.pos = it->position;
6057 else
6058 it->current.string_pos = it->position;
6061 /* Restore IT's settings from IT->stack. Called, for example, when no
6062 more overlay strings must be processed, and we return to delivering
6063 display elements from a buffer, or when the end of a string from a
6064 `display' property is reached and we return to delivering display
6065 elements from an overlay string, or from a buffer. */
6067 static void
6068 pop_it (struct it *it)
6070 struct iterator_stack_entry *p;
6071 bool from_display_prop = it->from_disp_prop_p;
6072 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6074 eassert (it->sp > 0);
6075 --it->sp;
6076 p = it->stack + it->sp;
6077 it->stop_charpos = p->stop_charpos;
6078 it->prev_stop = p->prev_stop;
6079 it->base_level_stop = p->base_level_stop;
6080 it->cmp_it = p->cmp_it;
6081 it->face_id = p->face_id;
6082 it->current = p->current;
6083 it->position = p->position;
6084 it->string = p->string;
6085 it->from_overlay = p->from_overlay;
6086 if (NILP (it->string))
6087 SET_TEXT_POS (it->current.string_pos, -1, -1);
6088 it->method = p->method;
6089 switch (it->method)
6091 case GET_FROM_IMAGE:
6092 it->image_id = p->u.image.image_id;
6093 it->object = p->u.image.object;
6094 it->slice = p->u.image.slice;
6095 break;
6096 case GET_FROM_XWIDGET:
6097 it->object = p->u.xwidget.object;
6098 break;
6099 case GET_FROM_STRETCH:
6100 it->object = p->u.stretch.object;
6101 break;
6102 case GET_FROM_BUFFER:
6103 it->object = it->w->contents;
6104 break;
6105 case GET_FROM_STRING:
6107 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6109 /* Restore the face_box_p flag, since it could have been
6110 overwritten by the face of the object that we just finished
6111 displaying. */
6112 if (face)
6113 it->face_box_p = face->box != FACE_NO_BOX;
6114 it->object = it->string;
6116 break;
6117 case GET_FROM_DISPLAY_VECTOR:
6118 if (it->s)
6119 it->method = GET_FROM_C_STRING;
6120 else if (STRINGP (it->string))
6121 it->method = GET_FROM_STRING;
6122 else
6124 it->method = GET_FROM_BUFFER;
6125 it->object = it->w->contents;
6127 break;
6128 case GET_FROM_C_STRING:
6129 break;
6130 default:
6131 emacs_abort ();
6133 it->end_charpos = p->end_charpos;
6134 it->string_nchars = p->string_nchars;
6135 it->area = p->area;
6136 it->multibyte_p = p->multibyte_p;
6137 it->avoid_cursor_p = p->avoid_cursor_p;
6138 it->space_width = p->space_width;
6139 it->font_height = p->font_height;
6140 it->voffset = p->voffset;
6141 it->string_from_display_prop_p = p->string_from_display_prop_p;
6142 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6143 it->line_wrap = p->line_wrap;
6144 it->bidi_p = p->bidi_p;
6145 it->paragraph_embedding = p->paragraph_embedding;
6146 it->from_disp_prop_p = p->from_disp_prop_p;
6147 if (it->bidi_p)
6149 bidi_pop_it (&it->bidi_it);
6150 /* Bidi-iterate until we get out of the portion of text, if any,
6151 covered by a `display' text property or by an overlay with
6152 `display' property. (We cannot just jump there, because the
6153 internal coherency of the bidi iterator state can not be
6154 preserved across such jumps.) We also must determine the
6155 paragraph base direction if the overlay we just processed is
6156 at the beginning of a new paragraph. */
6157 if (from_display_prop
6158 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6159 iterate_out_of_display_property (it);
6161 eassert ((BUFFERP (it->object)
6162 && IT_CHARPOS (*it) == it->bidi_it.charpos
6163 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6164 || (STRINGP (it->object)
6165 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6166 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6167 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6169 /* If we move the iterator over text covered by a display property
6170 to a new buffer position, any info about previously seen overlays
6171 is no longer valid. */
6172 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6173 it->ignore_overlay_strings_at_pos_p = false;
6178 /***********************************************************************
6179 Moving over lines
6180 ***********************************************************************/
6182 /* Set IT's current position to the previous line start. */
6184 static void
6185 back_to_previous_line_start (struct it *it)
6187 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6189 DEC_BOTH (cp, bp);
6190 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6194 /* Move IT to the next line start.
6196 Value is true if a newline was found. Set *SKIPPED_P to true if
6197 we skipped over part of the text (as opposed to moving the iterator
6198 continuously over the text). Otherwise, don't change the value
6199 of *SKIPPED_P.
6201 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6202 iterator on the newline, if it was found.
6204 Newlines may come from buffer text, overlay strings, or strings
6205 displayed via the `display' property. That's the reason we can't
6206 simply use find_newline_no_quit.
6208 Note that this function may not skip over invisible text that is so
6209 because of text properties and immediately follows a newline. If
6210 it would, function reseat_at_next_visible_line_start, when called
6211 from set_iterator_to_next, would effectively make invisible
6212 characters following a newline part of the wrong glyph row, which
6213 leads to wrong cursor motion. */
6215 static bool
6216 forward_to_next_line_start (struct it *it, bool *skipped_p,
6217 struct bidi_it *bidi_it_prev)
6219 ptrdiff_t old_selective;
6220 bool newline_found_p = false;
6221 int n;
6222 const int MAX_NEWLINE_DISTANCE = 500;
6224 /* If already on a newline, just consume it to avoid unintended
6225 skipping over invisible text below. */
6226 if (it->what == IT_CHARACTER
6227 && it->c == '\n'
6228 && CHARPOS (it->position) == IT_CHARPOS (*it))
6230 if (it->bidi_p && bidi_it_prev)
6231 *bidi_it_prev = it->bidi_it;
6232 set_iterator_to_next (it, false);
6233 it->c = 0;
6234 return true;
6237 /* Don't handle selective display in the following. It's (a)
6238 unnecessary because it's done by the caller, and (b) leads to an
6239 infinite recursion because next_element_from_ellipsis indirectly
6240 calls this function. */
6241 old_selective = it->selective;
6242 it->selective = 0;
6244 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6245 from buffer text. */
6246 for (n = 0;
6247 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6248 n += !STRINGP (it->string))
6250 if (!get_next_display_element (it))
6251 return false;
6252 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6253 if (newline_found_p && it->bidi_p && bidi_it_prev)
6254 *bidi_it_prev = it->bidi_it;
6255 set_iterator_to_next (it, false);
6258 /* If we didn't find a newline near enough, see if we can use a
6259 short-cut. */
6260 if (!newline_found_p)
6262 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6263 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6264 1, &bytepos);
6265 Lisp_Object pos;
6267 eassert (!STRINGP (it->string));
6269 /* If there isn't any `display' property in sight, and no
6270 overlays, we can just use the position of the newline in
6271 buffer text. */
6272 if (it->stop_charpos >= limit
6273 || ((pos = Fnext_single_property_change (make_number (start),
6274 Qdisplay, Qnil,
6275 make_number (limit)),
6276 NILP (pos))
6277 && next_overlay_change (start) == ZV))
6279 if (!it->bidi_p)
6281 IT_CHARPOS (*it) = limit;
6282 IT_BYTEPOS (*it) = bytepos;
6284 else
6286 struct bidi_it bprev;
6288 /* Help bidi.c avoid expensive searches for display
6289 properties and overlays, by telling it that there are
6290 none up to `limit'. */
6291 if (it->bidi_it.disp_pos < limit)
6293 it->bidi_it.disp_pos = limit;
6294 it->bidi_it.disp_prop = 0;
6296 do {
6297 bprev = it->bidi_it;
6298 bidi_move_to_visually_next (&it->bidi_it);
6299 } while (it->bidi_it.charpos != limit);
6300 IT_CHARPOS (*it) = limit;
6301 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6302 if (bidi_it_prev)
6303 *bidi_it_prev = bprev;
6305 *skipped_p = newline_found_p = true;
6307 else
6309 while (!newline_found_p)
6311 if (!get_next_display_element (it))
6312 break;
6313 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6314 if (newline_found_p && it->bidi_p && bidi_it_prev)
6315 *bidi_it_prev = it->bidi_it;
6316 set_iterator_to_next (it, false);
6321 it->selective = old_selective;
6322 return newline_found_p;
6326 /* Set IT's current position to the previous visible line start. Skip
6327 invisible text that is so either due to text properties or due to
6328 selective display. Caution: this does not change IT->current_x and
6329 IT->hpos. */
6331 static void
6332 back_to_previous_visible_line_start (struct it *it)
6334 while (IT_CHARPOS (*it) > BEGV)
6336 back_to_previous_line_start (it);
6338 if (IT_CHARPOS (*it) <= BEGV)
6339 break;
6341 /* If selective > 0, then lines indented more than its value are
6342 invisible. */
6343 if (it->selective > 0
6344 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6345 it->selective))
6346 continue;
6348 /* Check the newline before point for invisibility. */
6350 Lisp_Object prop;
6351 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6352 Qinvisible, it->window);
6353 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6354 continue;
6357 if (IT_CHARPOS (*it) <= BEGV)
6358 break;
6361 struct it it2;
6362 void *it2data = NULL;
6363 ptrdiff_t pos;
6364 ptrdiff_t beg, end;
6365 Lisp_Object val, overlay;
6367 SAVE_IT (it2, *it, it2data);
6369 /* If newline is part of a composition, continue from start of composition */
6370 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6371 && beg < IT_CHARPOS (*it))
6372 goto replaced;
6374 /* If newline is replaced by a display property, find start of overlay
6375 or interval and continue search from that point. */
6376 pos = --IT_CHARPOS (it2);
6377 --IT_BYTEPOS (it2);
6378 it2.sp = 0;
6379 bidi_unshelve_cache (NULL, false);
6380 it2.string_from_display_prop_p = false;
6381 it2.from_disp_prop_p = false;
6382 if (handle_display_prop (&it2) == HANDLED_RETURN
6383 && !NILP (val = get_char_property_and_overlay
6384 (make_number (pos), Qdisplay, Qnil, &overlay))
6385 && (OVERLAYP (overlay)
6386 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6387 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6389 RESTORE_IT (it, it, it2data);
6390 goto replaced;
6393 /* Newline is not replaced by anything -- so we are done. */
6394 RESTORE_IT (it, it, it2data);
6395 break;
6397 replaced:
6398 if (beg < BEGV)
6399 beg = BEGV;
6400 IT_CHARPOS (*it) = beg;
6401 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6405 it->continuation_lines_width = 0;
6407 eassert (IT_CHARPOS (*it) >= BEGV);
6408 eassert (IT_CHARPOS (*it) == BEGV
6409 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6410 CHECK_IT (it);
6414 /* Reseat iterator IT at the previous visible line start. Skip
6415 invisible text that is so either due to text properties or due to
6416 selective display. At the end, update IT's overlay information,
6417 face information etc. */
6419 void
6420 reseat_at_previous_visible_line_start (struct it *it)
6422 back_to_previous_visible_line_start (it);
6423 reseat (it, it->current.pos, true);
6424 CHECK_IT (it);
6428 /* Reseat iterator IT on the next visible line start in the current
6429 buffer. ON_NEWLINE_P means position IT on the newline
6430 preceding the line start. Skip over invisible text that is so
6431 because of selective display. Compute faces, overlays etc at the
6432 new position. Note that this function does not skip over text that
6433 is invisible because of text properties. */
6435 static void
6436 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6438 bool skipped_p = false;
6439 struct bidi_it bidi_it_prev;
6440 bool newline_found_p
6441 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6443 /* Skip over lines that are invisible because they are indented
6444 more than the value of IT->selective. */
6445 if (it->selective > 0)
6446 while (IT_CHARPOS (*it) < ZV
6447 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6448 it->selective))
6450 eassert (IT_BYTEPOS (*it) == BEGV
6451 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6452 newline_found_p =
6453 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6456 /* Position on the newline if that's what's requested. */
6457 if (on_newline_p && newline_found_p)
6459 if (STRINGP (it->string))
6461 if (IT_STRING_CHARPOS (*it) > 0)
6463 if (!it->bidi_p)
6465 --IT_STRING_CHARPOS (*it);
6466 --IT_STRING_BYTEPOS (*it);
6468 else
6470 /* We need to restore the bidi iterator to the state
6471 it had on the newline, and resync the IT's
6472 position with that. */
6473 it->bidi_it = bidi_it_prev;
6474 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6475 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6479 else if (IT_CHARPOS (*it) > BEGV)
6481 if (!it->bidi_p)
6483 --IT_CHARPOS (*it);
6484 --IT_BYTEPOS (*it);
6486 else
6488 /* We need to restore the bidi iterator to the state it
6489 had on the newline and resync IT with that. */
6490 it->bidi_it = bidi_it_prev;
6491 IT_CHARPOS (*it) = it->bidi_it.charpos;
6492 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6494 reseat (it, it->current.pos, false);
6497 else if (skipped_p)
6498 reseat (it, it->current.pos, false);
6500 CHECK_IT (it);
6505 /***********************************************************************
6506 Changing an iterator's position
6507 ***********************************************************************/
6509 /* Change IT's current position to POS in current_buffer.
6510 If FORCE_P, always check for text properties at the new position.
6511 Otherwise, text properties are only looked up if POS >=
6512 IT->check_charpos of a property. */
6514 static void
6515 reseat (struct it *it, struct text_pos pos, bool force_p)
6517 ptrdiff_t original_pos = IT_CHARPOS (*it);
6519 reseat_1 (it, pos, false);
6521 /* Determine where to check text properties. Avoid doing it
6522 where possible because text property lookup is very expensive. */
6523 if (force_p
6524 || CHARPOS (pos) > it->stop_charpos
6525 || CHARPOS (pos) < original_pos)
6527 if (it->bidi_p)
6529 /* For bidi iteration, we need to prime prev_stop and
6530 base_level_stop with our best estimations. */
6531 /* Implementation note: Of course, POS is not necessarily a
6532 stop position, so assigning prev_pos to it is a lie; we
6533 should have called compute_stop_backwards. However, if
6534 the current buffer does not include any R2L characters,
6535 that call would be a waste of cycles, because the
6536 iterator will never move back, and thus never cross this
6537 "fake" stop position. So we delay that backward search
6538 until the time we really need it, in next_element_from_buffer. */
6539 if (CHARPOS (pos) != it->prev_stop)
6540 it->prev_stop = CHARPOS (pos);
6541 if (CHARPOS (pos) < it->base_level_stop)
6542 it->base_level_stop = 0; /* meaning it's unknown */
6543 handle_stop (it);
6545 else
6547 handle_stop (it);
6548 it->prev_stop = it->base_level_stop = 0;
6553 CHECK_IT (it);
6557 /* Change IT's buffer position to POS. SET_STOP_P means set
6558 IT->stop_pos to POS, also. */
6560 static void
6561 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6563 /* Don't call this function when scanning a C string. */
6564 eassert (it->s == NULL);
6566 /* POS must be a reasonable value. */
6567 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6569 it->current.pos = it->position = pos;
6570 it->end_charpos = ZV;
6571 it->dpvec = NULL;
6572 it->current.dpvec_index = -1;
6573 it->current.overlay_string_index = -1;
6574 IT_STRING_CHARPOS (*it) = -1;
6575 IT_STRING_BYTEPOS (*it) = -1;
6576 it->string = Qnil;
6577 it->method = GET_FROM_BUFFER;
6578 it->object = it->w->contents;
6579 it->area = TEXT_AREA;
6580 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6581 it->sp = 0;
6582 it->string_from_display_prop_p = false;
6583 it->string_from_prefix_prop_p = false;
6585 it->from_disp_prop_p = false;
6586 it->face_before_selective_p = false;
6587 if (it->bidi_p)
6589 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6590 &it->bidi_it);
6591 bidi_unshelve_cache (NULL, false);
6592 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6593 it->bidi_it.string.s = NULL;
6594 it->bidi_it.string.lstring = Qnil;
6595 it->bidi_it.string.bufpos = 0;
6596 it->bidi_it.string.from_disp_str = false;
6597 it->bidi_it.string.unibyte = false;
6598 it->bidi_it.w = it->w;
6601 if (set_stop_p)
6603 it->stop_charpos = CHARPOS (pos);
6604 it->base_level_stop = CHARPOS (pos);
6606 /* This make the information stored in it->cmp_it invalidate. */
6607 it->cmp_it.id = -1;
6611 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6612 If S is non-null, it is a C string to iterate over. Otherwise,
6613 STRING gives a Lisp string to iterate over.
6615 If PRECISION > 0, don't return more then PRECISION number of
6616 characters from the string.
6618 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6619 characters have been returned. FIELD_WIDTH < 0 means an infinite
6620 field width.
6622 MULTIBYTE = 0 means disable processing of multibyte characters,
6623 MULTIBYTE > 0 means enable it,
6624 MULTIBYTE < 0 means use IT->multibyte_p.
6626 IT must be initialized via a prior call to init_iterator before
6627 calling this function. */
6629 static void
6630 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6631 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6632 int multibyte)
6634 /* No text property checks performed by default, but see below. */
6635 it->stop_charpos = -1;
6637 /* Set iterator position and end position. */
6638 memset (&it->current, 0, sizeof it->current);
6639 it->current.overlay_string_index = -1;
6640 it->current.dpvec_index = -1;
6641 eassert (charpos >= 0);
6643 /* If STRING is specified, use its multibyteness, otherwise use the
6644 setting of MULTIBYTE, if specified. */
6645 if (multibyte >= 0)
6646 it->multibyte_p = multibyte > 0;
6648 /* Bidirectional reordering of strings is controlled by the default
6649 value of bidi-display-reordering. Don't try to reorder while
6650 loading loadup.el, as the necessary character property tables are
6651 not yet available. */
6652 it->bidi_p =
6653 !redisplay__inhibit_bidi
6654 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6656 if (s == NULL)
6658 eassert (STRINGP (string));
6659 it->string = string;
6660 it->s = NULL;
6661 it->end_charpos = it->string_nchars = SCHARS (string);
6662 it->method = GET_FROM_STRING;
6663 it->current.string_pos = string_pos (charpos, string);
6665 if (it->bidi_p)
6667 it->bidi_it.string.lstring = string;
6668 it->bidi_it.string.s = NULL;
6669 it->bidi_it.string.schars = it->end_charpos;
6670 it->bidi_it.string.bufpos = 0;
6671 it->bidi_it.string.from_disp_str = false;
6672 it->bidi_it.string.unibyte = !it->multibyte_p;
6673 it->bidi_it.w = it->w;
6674 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6675 FRAME_WINDOW_P (it->f), &it->bidi_it);
6678 else
6680 it->s = (const unsigned char *) s;
6681 it->string = Qnil;
6683 /* Note that we use IT->current.pos, not it->current.string_pos,
6684 for displaying C strings. */
6685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6686 if (it->multibyte_p)
6688 it->current.pos = c_string_pos (charpos, s, true);
6689 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6691 else
6693 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6694 it->end_charpos = it->string_nchars = strlen (s);
6697 if (it->bidi_p)
6699 it->bidi_it.string.lstring = Qnil;
6700 it->bidi_it.string.s = (const unsigned char *) s;
6701 it->bidi_it.string.schars = it->end_charpos;
6702 it->bidi_it.string.bufpos = 0;
6703 it->bidi_it.string.from_disp_str = false;
6704 it->bidi_it.string.unibyte = !it->multibyte_p;
6705 it->bidi_it.w = it->w;
6706 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6707 &it->bidi_it);
6709 it->method = GET_FROM_C_STRING;
6712 /* PRECISION > 0 means don't return more than PRECISION characters
6713 from the string. */
6714 if (precision > 0 && it->end_charpos - charpos > precision)
6716 it->end_charpos = it->string_nchars = charpos + precision;
6717 if (it->bidi_p)
6718 it->bidi_it.string.schars = it->end_charpos;
6721 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6722 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6723 FIELD_WIDTH < 0 means infinite field width. This is useful for
6724 padding with `-' at the end of a mode line. */
6725 if (field_width < 0)
6726 field_width = INFINITY;
6727 /* Implementation note: We deliberately don't enlarge
6728 it->bidi_it.string.schars here to fit it->end_charpos, because
6729 the bidi iterator cannot produce characters out of thin air. */
6730 if (field_width > it->end_charpos - charpos)
6731 it->end_charpos = charpos + field_width;
6733 /* Use the standard display table for displaying strings. */
6734 if (DISP_TABLE_P (Vstandard_display_table))
6735 it->dp = XCHAR_TABLE (Vstandard_display_table);
6737 it->stop_charpos = charpos;
6738 it->prev_stop = charpos;
6739 it->base_level_stop = 0;
6740 if (it->bidi_p)
6742 it->bidi_it.first_elt = true;
6743 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6744 it->bidi_it.disp_pos = -1;
6746 if (s == NULL && it->multibyte_p)
6748 ptrdiff_t endpos = SCHARS (it->string);
6749 if (endpos > it->end_charpos)
6750 endpos = it->end_charpos;
6751 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6752 it->string);
6754 CHECK_IT (it);
6759 /***********************************************************************
6760 Iteration
6761 ***********************************************************************/
6763 /* Map enum it_method value to corresponding next_element_from_* function. */
6765 typedef bool (*next_element_function) (struct it *);
6767 static next_element_function const get_next_element[NUM_IT_METHODS] =
6769 next_element_from_buffer,
6770 next_element_from_display_vector,
6771 next_element_from_string,
6772 next_element_from_c_string,
6773 next_element_from_image,
6774 next_element_from_stretch,
6775 next_element_from_xwidget,
6778 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6781 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6782 (possibly with the following characters). */
6784 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6785 ((IT)->cmp_it.id >= 0 \
6786 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6787 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6788 END_CHARPOS, (IT)->w, \
6789 FACE_FROM_ID_OR_NULL ((IT)->f, \
6790 (IT)->face_id), \
6791 (IT)->string)))
6794 /* Lookup the char-table Vglyphless_char_display for character C (-1
6795 if we want information for no-font case), and return the display
6796 method symbol. By side-effect, update it->what and
6797 it->glyphless_method. This function is called from
6798 get_next_display_element for each character element, and from
6799 x_produce_glyphs when no suitable font was found. */
6801 Lisp_Object
6802 lookup_glyphless_char_display (int c, struct it *it)
6804 Lisp_Object glyphless_method = Qnil;
6806 if (CHAR_TABLE_P (Vglyphless_char_display)
6807 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6809 if (c >= 0)
6811 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6812 if (CONSP (glyphless_method))
6813 glyphless_method = FRAME_WINDOW_P (it->f)
6814 ? XCAR (glyphless_method)
6815 : XCDR (glyphless_method);
6817 else
6818 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6821 retry:
6822 if (NILP (glyphless_method))
6824 if (c >= 0)
6825 /* The default is to display the character by a proper font. */
6826 return Qnil;
6827 /* The default for the no-font case is to display an empty box. */
6828 glyphless_method = Qempty_box;
6830 if (EQ (glyphless_method, Qzero_width))
6832 if (c >= 0)
6833 return glyphless_method;
6834 /* This method can't be used for the no-font case. */
6835 glyphless_method = Qempty_box;
6837 if (EQ (glyphless_method, Qthin_space))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6839 else if (EQ (glyphless_method, Qempty_box))
6840 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6841 else if (EQ (glyphless_method, Qhex_code))
6842 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6843 else if (STRINGP (glyphless_method))
6844 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6845 else
6847 /* Invalid value. We use the default method. */
6848 glyphless_method = Qnil;
6849 goto retry;
6851 it->what = IT_GLYPHLESS;
6852 return glyphless_method;
6855 /* Merge escape glyph face and cache the result. */
6857 static struct frame *last_escape_glyph_frame = NULL;
6858 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6859 static int last_escape_glyph_merged_face_id = 0;
6861 static int
6862 merge_escape_glyph_face (struct it *it)
6864 int face_id;
6866 if (it->f == last_escape_glyph_frame
6867 && it->face_id == last_escape_glyph_face_id)
6868 face_id = last_escape_glyph_merged_face_id;
6869 else
6871 /* Merge the `escape-glyph' face into the current face. */
6872 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6873 last_escape_glyph_frame = it->f;
6874 last_escape_glyph_face_id = it->face_id;
6875 last_escape_glyph_merged_face_id = face_id;
6877 return face_id;
6880 /* Likewise for glyphless glyph face. */
6882 static struct frame *last_glyphless_glyph_frame = NULL;
6883 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6884 static int last_glyphless_glyph_merged_face_id = 0;
6887 merge_glyphless_glyph_face (struct it *it)
6889 int face_id;
6891 if (it->f == last_glyphless_glyph_frame
6892 && it->face_id == last_glyphless_glyph_face_id)
6893 face_id = last_glyphless_glyph_merged_face_id;
6894 else
6896 /* Merge the `glyphless-char' face into the current face. */
6897 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6898 last_glyphless_glyph_frame = it->f;
6899 last_glyphless_glyph_face_id = it->face_id;
6900 last_glyphless_glyph_merged_face_id = face_id;
6902 return face_id;
6905 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6906 be called before redisplaying windows, and when the frame's face
6907 cache is freed. */
6908 void
6909 forget_escape_and_glyphless_faces (void)
6911 last_escape_glyph_frame = NULL;
6912 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6913 last_glyphless_glyph_frame = NULL;
6914 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6917 /* Load IT's display element fields with information about the next
6918 display element from the current position of IT. Value is false if
6919 end of buffer (or C string) is reached. */
6921 static bool
6922 get_next_display_element (struct it *it)
6924 /* True means that we found a display element. False means that
6925 we hit the end of what we iterate over. Performance note: the
6926 function pointer `method' used here turns out to be faster than
6927 using a sequence of if-statements. */
6928 bool success_p;
6930 get_next:
6931 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6933 if (it->what == IT_CHARACTER)
6935 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6936 and only if (a) the resolved directionality of that character
6937 is R..." */
6938 /* FIXME: Do we need an exception for characters from display
6939 tables? */
6940 if (it->bidi_p && it->bidi_it.type == STRONG_R
6941 && !inhibit_bidi_mirroring)
6942 it->c = bidi_mirror_char (it->c);
6943 /* Map via display table or translate control characters.
6944 IT->c, IT->len etc. have been set to the next character by
6945 the function call above. If we have a display table, and it
6946 contains an entry for IT->c, translate it. Don't do this if
6947 IT->c itself comes from a display table, otherwise we could
6948 end up in an infinite recursion. (An alternative could be to
6949 count the recursion depth of this function and signal an
6950 error when a certain maximum depth is reached.) Is it worth
6951 it? */
6952 if (success_p && it->dpvec == NULL)
6954 Lisp_Object dv;
6955 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6956 bool nonascii_space_p = false;
6957 bool nonascii_hyphen_p = false;
6958 int c = it->c; /* This is the character to display. */
6960 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6962 eassert (SINGLE_BYTE_CHAR_P (c));
6963 if (unibyte_display_via_language_environment)
6965 c = DECODE_CHAR (unibyte, c);
6966 if (c < 0)
6967 c = BYTE8_TO_CHAR (it->c);
6969 else
6970 c = BYTE8_TO_CHAR (it->c);
6973 if (it->dp
6974 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6975 VECTORP (dv)))
6977 struct Lisp_Vector *v = XVECTOR (dv);
6979 /* Return the first character from the display table
6980 entry, if not empty. If empty, don't display the
6981 current character. */
6982 if (v->header.size)
6984 it->dpvec_char_len = it->len;
6985 it->dpvec = v->contents;
6986 it->dpend = v->contents + v->header.size;
6987 it->current.dpvec_index = 0;
6988 it->dpvec_face_id = -1;
6989 it->saved_face_id = it->face_id;
6990 it->method = GET_FROM_DISPLAY_VECTOR;
6991 it->ellipsis_p = false;
6993 else
6995 set_iterator_to_next (it, false);
6997 goto get_next;
7000 if (! NILP (lookup_glyphless_char_display (c, it)))
7002 if (it->what == IT_GLYPHLESS)
7003 goto done;
7004 /* Don't display this character. */
7005 set_iterator_to_next (it, false);
7006 goto get_next;
7009 /* If `nobreak-char-display' is non-nil, we display
7010 non-ASCII spaces and hyphens specially. */
7011 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7013 if (c == NO_BREAK_SPACE)
7014 nonascii_space_p = true;
7015 else if (c == SOFT_HYPHEN || c == HYPHEN
7016 || c == NON_BREAKING_HYPHEN)
7017 nonascii_hyphen_p = true;
7020 /* Translate control characters into `\003' or `^C' form.
7021 Control characters coming from a display table entry are
7022 currently not translated because we use IT->dpvec to hold
7023 the translation. This could easily be changed but I
7024 don't believe that it is worth doing.
7026 The characters handled by `nobreak-char-display' must be
7027 translated too.
7029 Non-printable characters and raw-byte characters are also
7030 translated to octal form. */
7031 if (((c < ' ' || c == 127) /* ASCII control chars. */
7032 ? (it->area != TEXT_AREA
7033 /* In mode line, treat \n, \t like other crl chars. */
7034 || (c != '\t'
7035 && it->glyph_row
7036 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7037 || (c != '\n' && c != '\t'))
7038 : (nonascii_space_p
7039 || nonascii_hyphen_p
7040 || CHAR_BYTE8_P (c)
7041 || ! CHAR_PRINTABLE_P (c))))
7043 /* C is a control character, non-ASCII space/hyphen,
7044 raw-byte, or a non-printable character which must be
7045 displayed either as '\003' or as `^C' where the '\\'
7046 and '^' can be defined in the display table. Fill
7047 IT->ctl_chars with glyphs for what we have to
7048 display. Then, set IT->dpvec to these glyphs. */
7049 Lisp_Object gc;
7050 int ctl_len;
7051 int face_id;
7052 int lface_id = 0;
7053 int escape_glyph;
7055 /* Handle control characters with ^. */
7057 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7059 int g;
7061 g = '^'; /* default glyph for Control */
7062 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7063 if (it->dp
7064 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7066 g = GLYPH_CODE_CHAR (gc);
7067 lface_id = GLYPH_CODE_FACE (gc);
7070 face_id = (lface_id
7071 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7072 : merge_escape_glyph_face (it));
7074 XSETINT (it->ctl_chars[0], g);
7075 XSETINT (it->ctl_chars[1], c ^ 0100);
7076 ctl_len = 2;
7077 goto display_control;
7080 /* Handle non-ascii space in the mode where it only gets
7081 highlighting. */
7083 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7085 /* Merge `nobreak-space' into the current face. */
7086 face_id = merge_faces (it->f, Qnobreak_space, 0,
7087 it->face_id);
7088 XSETINT (it->ctl_chars[0], ' ');
7089 ctl_len = 1;
7090 goto display_control;
7093 /* Handle non-ascii hyphens in the mode where it only
7094 gets highlighting. */
7096 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7098 /* Merge `nobreak-space' into the current face. */
7099 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7100 it->face_id);
7101 XSETINT (it->ctl_chars[0], '-');
7102 ctl_len = 1;
7103 goto display_control;
7106 /* Handle sequences that start with the "escape glyph". */
7108 /* the default escape glyph is \. */
7109 escape_glyph = '\\';
7111 if (it->dp
7112 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7114 escape_glyph = GLYPH_CODE_CHAR (gc);
7115 lface_id = GLYPH_CODE_FACE (gc);
7118 face_id = (lface_id
7119 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7120 : merge_escape_glyph_face (it));
7122 /* Draw non-ASCII space/hyphen with escape glyph: */
7124 if (nonascii_space_p || nonascii_hyphen_p)
7126 XSETINT (it->ctl_chars[0], escape_glyph);
7127 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7128 ctl_len = 2;
7129 goto display_control;
7133 char str[10];
7134 int len, i;
7136 if (CHAR_BYTE8_P (c))
7137 /* Display \200 instead of \17777600. */
7138 c = CHAR_TO_BYTE8 (c);
7139 len = sprintf (str, "%03o", c + 0u);
7141 XSETINT (it->ctl_chars[0], escape_glyph);
7142 for (i = 0; i < len; i++)
7143 XSETINT (it->ctl_chars[i + 1], str[i]);
7144 ctl_len = len + 1;
7147 display_control:
7148 /* Set up IT->dpvec and return first character from it. */
7149 it->dpvec_char_len = it->len;
7150 it->dpvec = it->ctl_chars;
7151 it->dpend = it->dpvec + ctl_len;
7152 it->current.dpvec_index = 0;
7153 it->dpvec_face_id = face_id;
7154 it->saved_face_id = it->face_id;
7155 it->method = GET_FROM_DISPLAY_VECTOR;
7156 it->ellipsis_p = false;
7157 goto get_next;
7159 it->char_to_display = c;
7161 else if (success_p)
7163 it->char_to_display = it->c;
7167 #ifdef HAVE_WINDOW_SYSTEM
7168 /* Adjust face id for a multibyte character. There are no multibyte
7169 character in unibyte text. */
7170 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7171 && it->multibyte_p
7172 && success_p
7173 && FRAME_WINDOW_P (it->f))
7175 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7177 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7179 /* Automatic composition with glyph-string. */
7180 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7182 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7184 else
7186 ptrdiff_t pos = (it->s ? -1
7187 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7188 : IT_CHARPOS (*it));
7189 int c;
7191 if (it->what == IT_CHARACTER)
7192 c = it->char_to_display;
7193 else
7195 struct composition *cmp = composition_table[it->cmp_it.id];
7196 int i;
7198 c = ' ';
7199 for (i = 0; i < cmp->glyph_len; i++)
7200 /* TAB in a composition means display glyphs with
7201 padding space on the left or right. */
7202 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7203 break;
7205 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7208 #endif /* HAVE_WINDOW_SYSTEM */
7210 done:
7211 /* Is this character the last one of a run of characters with
7212 box? If yes, set IT->end_of_box_run_p to true. */
7213 if (it->face_box_p
7214 && it->s == NULL)
7216 if (it->method == GET_FROM_STRING && it->sp)
7218 int face_id = underlying_face_id (it);
7219 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7221 if (face)
7223 if (face->box == FACE_NO_BOX)
7225 /* If the box comes from face properties in a
7226 display string, check faces in that string. */
7227 int string_face_id = face_after_it_pos (it);
7228 it->end_of_box_run_p
7229 = (FACE_FROM_ID (it->f, string_face_id)->box
7230 == FACE_NO_BOX);
7232 /* Otherwise, the box comes from the underlying face.
7233 If this is the last string character displayed, check
7234 the next buffer location. */
7235 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7236 /* n_overlay_strings is unreliable unless
7237 overlay_string_index is non-negative. */
7238 && ((it->current.overlay_string_index >= 0
7239 && (it->current.overlay_string_index
7240 == it->n_overlay_strings - 1))
7241 /* A string from display property. */
7242 || it->from_disp_prop_p))
7244 ptrdiff_t ignore;
7245 int next_face_id;
7246 bool text_from_string = false;
7247 /* Normally, the next buffer location is stored in
7248 IT->current.pos... */
7249 struct text_pos pos = it->current.pos;
7251 /* ...but for a string from a display property, the
7252 next buffer position is stored in the 'position'
7253 member of the iteration stack slot below the
7254 current one, see handle_single_display_spec. By
7255 contrast, it->current.pos was not yet updated to
7256 point to that buffer position; that will happen
7257 in pop_it, after we finish displaying the current
7258 string. Note that we already checked above that
7259 it->sp is positive, so subtracting one from it is
7260 safe. */
7261 if (it->from_disp_prop_p)
7263 int stackp = it->sp - 1;
7265 /* Find the stack level with data from buffer. */
7266 while (stackp >= 0
7267 && STRINGP ((it->stack + stackp)->string))
7268 stackp--;
7269 if (stackp < 0)
7271 /* If no stack slot was found for iterating
7272 a buffer, we are displaying text from a
7273 string, most probably the mode line or
7274 the header line, and that string has a
7275 display string on some of its
7276 characters. */
7277 text_from_string = true;
7278 pos = it->stack[it->sp - 1].position;
7280 else
7281 pos = (it->stack + stackp)->position;
7283 else
7284 INC_TEXT_POS (pos, it->multibyte_p);
7286 if (text_from_string)
7288 Lisp_Object base_string = it->stack[it->sp - 1].string;
7290 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7291 it->end_of_box_run_p = true;
7292 else
7294 next_face_id
7295 = face_at_string_position (it->w, base_string,
7296 CHARPOS (pos), 0,
7297 &ignore, face_id, false);
7298 it->end_of_box_run_p
7299 = (FACE_FROM_ID (it->f, next_face_id)->box
7300 == FACE_NO_BOX);
7303 else if (CHARPOS (pos) >= ZV)
7304 it->end_of_box_run_p = true;
7305 else
7307 next_face_id =
7308 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7309 CHARPOS (pos)
7310 + TEXT_PROP_DISTANCE_LIMIT,
7311 false, -1);
7312 it->end_of_box_run_p
7313 = (FACE_FROM_ID (it->f, next_face_id)->box
7314 == FACE_NO_BOX);
7319 /* next_element_from_display_vector sets this flag according to
7320 faces of the display vector glyphs, see there. */
7321 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7323 int face_id = face_after_it_pos (it);
7324 it->end_of_box_run_p
7325 = (face_id != it->face_id
7326 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7329 /* If we reached the end of the object we've been iterating (e.g., a
7330 display string or an overlay string), and there's something on
7331 IT->stack, proceed with what's on the stack. It doesn't make
7332 sense to return false if there's unprocessed stuff on the stack,
7333 because otherwise that stuff will never be displayed. */
7334 if (!success_p && it->sp > 0)
7336 set_iterator_to_next (it, false);
7337 success_p = get_next_display_element (it);
7340 /* Value is false if end of buffer or string reached. */
7341 return success_p;
7345 /* Move IT to the next display element.
7347 RESEAT_P means if called on a newline in buffer text,
7348 skip to the next visible line start.
7350 Functions get_next_display_element and set_iterator_to_next are
7351 separate because I find this arrangement easier to handle than a
7352 get_next_display_element function that also increments IT's
7353 position. The way it is we can first look at an iterator's current
7354 display element, decide whether it fits on a line, and if it does,
7355 increment the iterator position. The other way around we probably
7356 would either need a flag indicating whether the iterator has to be
7357 incremented the next time, or we would have to implement a
7358 decrement position function which would not be easy to write. */
7360 void
7361 set_iterator_to_next (struct it *it, bool reseat_p)
7363 /* Reset flags indicating start and end of a sequence of characters
7364 with box. Reset them at the start of this function because
7365 moving the iterator to a new position might set them. */
7366 it->start_of_box_run_p = it->end_of_box_run_p = false;
7368 switch (it->method)
7370 case GET_FROM_BUFFER:
7371 /* The current display element of IT is a character from
7372 current_buffer. Advance in the buffer, and maybe skip over
7373 invisible lines that are so because of selective display. */
7374 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7375 reseat_at_next_visible_line_start (it, false);
7376 else if (it->cmp_it.id >= 0)
7378 /* We are currently getting glyphs from a composition. */
7379 if (! it->bidi_p)
7381 IT_CHARPOS (*it) += it->cmp_it.nchars;
7382 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7384 else
7386 int i;
7388 /* Update IT's char/byte positions to point to the first
7389 character of the next grapheme cluster, or to the
7390 character visually after the current composition. */
7391 for (i = 0; i < it->cmp_it.nchars; i++)
7392 bidi_move_to_visually_next (&it->bidi_it);
7393 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7394 IT_CHARPOS (*it) = it->bidi_it.charpos;
7397 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7398 && it->cmp_it.to < it->cmp_it.nglyphs)
7400 /* Composition created while scanning forward. Proceed
7401 to the next grapheme cluster. */
7402 it->cmp_it.from = it->cmp_it.to;
7404 else if ((it->bidi_p && it->cmp_it.reversed_p)
7405 && it->cmp_it.from > 0)
7407 /* Composition created while scanning backward. Proceed
7408 to the previous grapheme cluster. */
7409 it->cmp_it.to = it->cmp_it.from;
7411 else
7413 /* No more grapheme clusters in this composition.
7414 Find the next stop position. */
7415 ptrdiff_t stop = it->end_charpos;
7417 if (it->bidi_it.scan_dir < 0)
7418 /* Now we are scanning backward and don't know
7419 where to stop. */
7420 stop = -1;
7421 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7422 IT_BYTEPOS (*it), stop, Qnil);
7425 else
7427 eassert (it->len != 0);
7429 if (!it->bidi_p)
7431 IT_BYTEPOS (*it) += it->len;
7432 IT_CHARPOS (*it) += 1;
7434 else
7436 int prev_scan_dir = it->bidi_it.scan_dir;
7437 /* If this is a new paragraph, determine its base
7438 direction (a.k.a. its base embedding level). */
7439 if (it->bidi_it.new_paragraph)
7440 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7441 false);
7442 bidi_move_to_visually_next (&it->bidi_it);
7443 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7444 IT_CHARPOS (*it) = it->bidi_it.charpos;
7445 if (prev_scan_dir != it->bidi_it.scan_dir)
7447 /* As the scan direction was changed, we must
7448 re-compute the stop position for composition. */
7449 ptrdiff_t stop = it->end_charpos;
7450 if (it->bidi_it.scan_dir < 0)
7451 stop = -1;
7452 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7453 IT_BYTEPOS (*it), stop, Qnil);
7456 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7458 break;
7460 case GET_FROM_C_STRING:
7461 /* Current display element of IT is from a C string. */
7462 if (!it->bidi_p
7463 /* If the string position is beyond string's end, it means
7464 next_element_from_c_string is padding the string with
7465 blanks, in which case we bypass the bidi iterator,
7466 because it cannot deal with such virtual characters. */
7467 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7469 IT_BYTEPOS (*it) += it->len;
7470 IT_CHARPOS (*it) += 1;
7472 else
7474 bidi_move_to_visually_next (&it->bidi_it);
7475 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7476 IT_CHARPOS (*it) = it->bidi_it.charpos;
7478 break;
7480 case GET_FROM_DISPLAY_VECTOR:
7481 /* Current display element of IT is from a display table entry.
7482 Advance in the display table definition. Reset it to null if
7483 end reached, and continue with characters from buffers/
7484 strings. */
7485 ++it->current.dpvec_index;
7487 /* Restore face of the iterator to what they were before the
7488 display vector entry (these entries may contain faces). */
7489 it->face_id = it->saved_face_id;
7491 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7493 bool recheck_faces = it->ellipsis_p;
7495 if (it->s)
7496 it->method = GET_FROM_C_STRING;
7497 else if (STRINGP (it->string))
7498 it->method = GET_FROM_STRING;
7499 else
7501 it->method = GET_FROM_BUFFER;
7502 it->object = it->w->contents;
7505 it->dpvec = NULL;
7506 it->current.dpvec_index = -1;
7508 /* Skip over characters which were displayed via IT->dpvec. */
7509 if (it->dpvec_char_len < 0)
7510 reseat_at_next_visible_line_start (it, true);
7511 else if (it->dpvec_char_len > 0)
7513 it->len = it->dpvec_char_len;
7514 set_iterator_to_next (it, reseat_p);
7517 /* Maybe recheck faces after display vector. */
7518 if (recheck_faces)
7520 if (it->method == GET_FROM_STRING)
7521 it->stop_charpos = IT_STRING_CHARPOS (*it);
7522 else
7523 it->stop_charpos = IT_CHARPOS (*it);
7526 break;
7528 case GET_FROM_STRING:
7529 /* Current display element is a character from a Lisp string. */
7530 eassert (it->s == NULL && STRINGP (it->string));
7531 /* Don't advance past string end. These conditions are true
7532 when set_iterator_to_next is called at the end of
7533 get_next_display_element, in which case the Lisp string is
7534 already exhausted, and all we want is pop the iterator
7535 stack. */
7536 if (it->current.overlay_string_index >= 0)
7538 /* This is an overlay string, so there's no padding with
7539 spaces, and the number of characters in the string is
7540 where the string ends. */
7541 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7542 goto consider_string_end;
7544 else
7546 /* Not an overlay string. There could be padding, so test
7547 against it->end_charpos. */
7548 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7549 goto consider_string_end;
7551 if (it->cmp_it.id >= 0)
7553 /* We are delivering display elements from a composition.
7554 Update the string position past the grapheme cluster
7555 we've just processed. */
7556 if (! it->bidi_p)
7558 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7559 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7561 else
7563 int i;
7565 for (i = 0; i < it->cmp_it.nchars; i++)
7566 bidi_move_to_visually_next (&it->bidi_it);
7567 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7568 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7571 /* Did we exhaust all the grapheme clusters of this
7572 composition? */
7573 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7574 && (it->cmp_it.to < it->cmp_it.nglyphs))
7576 /* Not all the grapheme clusters were processed yet;
7577 advance to the next cluster. */
7578 it->cmp_it.from = it->cmp_it.to;
7580 else if ((it->bidi_p && it->cmp_it.reversed_p)
7581 && it->cmp_it.from > 0)
7583 /* Likewise: advance to the next cluster, but going in
7584 the reverse direction. */
7585 it->cmp_it.to = it->cmp_it.from;
7587 else
7589 /* This composition was fully processed; find the next
7590 candidate place for checking for composed
7591 characters. */
7592 /* Always limit string searches to the string length;
7593 any padding spaces are not part of the string, and
7594 there cannot be any compositions in that padding. */
7595 ptrdiff_t stop = SCHARS (it->string);
7597 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7598 stop = -1;
7599 else if (it->end_charpos < stop)
7601 /* Cf. PRECISION in reseat_to_string: we might be
7602 limited in how many of the string characters we
7603 need to deliver. */
7604 stop = it->end_charpos;
7606 composition_compute_stop_pos (&it->cmp_it,
7607 IT_STRING_CHARPOS (*it),
7608 IT_STRING_BYTEPOS (*it), stop,
7609 it->string);
7612 else
7614 if (!it->bidi_p
7615 /* If the string position is beyond string's end, it
7616 means next_element_from_string is padding the string
7617 with blanks, in which case we bypass the bidi
7618 iterator, because it cannot deal with such virtual
7619 characters. */
7620 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7622 IT_STRING_BYTEPOS (*it) += it->len;
7623 IT_STRING_CHARPOS (*it) += 1;
7625 else
7627 int prev_scan_dir = it->bidi_it.scan_dir;
7629 bidi_move_to_visually_next (&it->bidi_it);
7630 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7631 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7632 /* If the scan direction changes, we may need to update
7633 the place where to check for composed characters. */
7634 if (prev_scan_dir != it->bidi_it.scan_dir)
7636 ptrdiff_t stop = SCHARS (it->string);
7638 if (it->bidi_it.scan_dir < 0)
7639 stop = -1;
7640 else if (it->end_charpos < stop)
7641 stop = it->end_charpos;
7643 composition_compute_stop_pos (&it->cmp_it,
7644 IT_STRING_CHARPOS (*it),
7645 IT_STRING_BYTEPOS (*it), stop,
7646 it->string);
7651 consider_string_end:
7653 if (it->current.overlay_string_index >= 0)
7655 /* IT->string is an overlay string. Advance to the
7656 next, if there is one. */
7657 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7659 it->ellipsis_p = false;
7660 next_overlay_string (it);
7661 if (it->ellipsis_p)
7662 setup_for_ellipsis (it, 0);
7665 else
7667 /* IT->string is not an overlay string. If we reached
7668 its end, and there is something on IT->stack, proceed
7669 with what is on the stack. This can be either another
7670 string, this time an overlay string, or a buffer. */
7671 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7672 && it->sp > 0)
7674 pop_it (it);
7675 if (it->method == GET_FROM_STRING)
7676 goto consider_string_end;
7679 break;
7681 case GET_FROM_IMAGE:
7682 case GET_FROM_STRETCH:
7683 case GET_FROM_XWIDGET:
7685 /* The position etc with which we have to proceed are on
7686 the stack. The position may be at the end of a string,
7687 if the `display' property takes up the whole string. */
7688 eassert (it->sp > 0);
7689 pop_it (it);
7690 if (it->method == GET_FROM_STRING)
7691 goto consider_string_end;
7692 break;
7694 default:
7695 /* There are no other methods defined, so this should be a bug. */
7696 emacs_abort ();
7699 eassert (it->method != GET_FROM_STRING
7700 || (STRINGP (it->string)
7701 && IT_STRING_CHARPOS (*it) >= 0));
7704 /* Load IT's display element fields with information about the next
7705 display element which comes from a display table entry or from the
7706 result of translating a control character to one of the forms `^C'
7707 or `\003'.
7709 IT->dpvec holds the glyphs to return as characters.
7710 IT->saved_face_id holds the face id before the display vector--it
7711 is restored into IT->face_id in set_iterator_to_next. */
7713 static bool
7714 next_element_from_display_vector (struct it *it)
7716 Lisp_Object gc;
7717 int prev_face_id = it->face_id;
7718 int next_face_id;
7720 /* Precondition. */
7721 eassert (it->dpvec && it->current.dpvec_index >= 0);
7723 it->face_id = it->saved_face_id;
7725 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7726 That seemed totally bogus - so I changed it... */
7727 gc = it->dpvec[it->current.dpvec_index];
7729 if (GLYPH_CODE_P (gc))
7731 struct face *this_face, *prev_face, *next_face;
7733 it->c = GLYPH_CODE_CHAR (gc);
7734 it->len = CHAR_BYTES (it->c);
7736 /* The entry may contain a face id to use. Such a face id is
7737 the id of a Lisp face, not a realized face. A face id of
7738 zero means no face is specified. */
7739 if (it->dpvec_face_id >= 0)
7740 it->face_id = it->dpvec_face_id;
7741 else
7743 int lface_id = GLYPH_CODE_FACE (gc);
7744 if (lface_id > 0)
7745 it->face_id = merge_faces (it->f, Qt, lface_id,
7746 it->saved_face_id);
7749 /* Glyphs in the display vector could have the box face, so we
7750 need to set the related flags in the iterator, as
7751 appropriate. */
7752 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7753 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7755 /* Is this character the first character of a box-face run? */
7756 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7757 && (!prev_face
7758 || prev_face->box == FACE_NO_BOX));
7760 /* For the last character of the box-face run, we need to look
7761 either at the next glyph from the display vector, or at the
7762 face we saw before the display vector. */
7763 next_face_id = it->saved_face_id;
7764 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7766 if (it->dpvec_face_id >= 0)
7767 next_face_id = it->dpvec_face_id;
7768 else
7770 int lface_id =
7771 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7773 if (lface_id > 0)
7774 next_face_id = merge_faces (it->f, Qt, lface_id,
7775 it->saved_face_id);
7778 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7779 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7780 && (!next_face
7781 || next_face->box == FACE_NO_BOX));
7782 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7784 else
7785 /* Display table entry is invalid. Return a space. */
7786 it->c = ' ', it->len = 1;
7788 /* Don't change position and object of the iterator here. They are
7789 still the values of the character that had this display table
7790 entry or was translated, and that's what we want. */
7791 it->what = IT_CHARACTER;
7792 return true;
7795 /* Get the first element of string/buffer in the visual order, after
7796 being reseated to a new position in a string or a buffer. */
7797 static void
7798 get_visually_first_element (struct it *it)
7800 bool string_p = STRINGP (it->string) || it->s;
7801 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7802 ptrdiff_t bob = (string_p ? 0 : BEGV);
7804 if (STRINGP (it->string))
7806 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7807 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7809 else
7811 it->bidi_it.charpos = IT_CHARPOS (*it);
7812 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7815 if (it->bidi_it.charpos == eob)
7817 /* Nothing to do, but reset the FIRST_ELT flag, like
7818 bidi_paragraph_init does, because we are not going to
7819 call it. */
7820 it->bidi_it.first_elt = false;
7822 else if (it->bidi_it.charpos == bob
7823 || (!string_p
7824 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7825 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7827 /* If we are at the beginning of a line/string, we can produce
7828 the next element right away. */
7829 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7830 bidi_move_to_visually_next (&it->bidi_it);
7832 else
7834 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7836 /* We need to prime the bidi iterator starting at the line's or
7837 string's beginning, before we will be able to produce the
7838 next element. */
7839 if (string_p)
7840 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7841 else
7842 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7843 IT_BYTEPOS (*it), -1,
7844 &it->bidi_it.bytepos);
7845 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7848 /* Now return to buffer/string position where we were asked
7849 to get the next display element, and produce that. */
7850 bidi_move_to_visually_next (&it->bidi_it);
7852 while (it->bidi_it.bytepos != orig_bytepos
7853 && it->bidi_it.charpos < eob);
7856 /* Adjust IT's position information to where we ended up. */
7857 if (STRINGP (it->string))
7859 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7860 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7862 else
7864 IT_CHARPOS (*it) = it->bidi_it.charpos;
7865 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7868 if (STRINGP (it->string) || !it->s)
7870 ptrdiff_t stop, charpos, bytepos;
7872 if (STRINGP (it->string))
7874 eassert (!it->s);
7875 stop = SCHARS (it->string);
7876 if (stop > it->end_charpos)
7877 stop = it->end_charpos;
7878 charpos = IT_STRING_CHARPOS (*it);
7879 bytepos = IT_STRING_BYTEPOS (*it);
7881 else
7883 stop = it->end_charpos;
7884 charpos = IT_CHARPOS (*it);
7885 bytepos = IT_BYTEPOS (*it);
7887 if (it->bidi_it.scan_dir < 0)
7888 stop = -1;
7889 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7890 it->string);
7894 /* Load IT with the next display element from Lisp string IT->string.
7895 IT->current.string_pos is the current position within the string.
7896 If IT->current.overlay_string_index >= 0, the Lisp string is an
7897 overlay string. */
7899 static bool
7900 next_element_from_string (struct it *it)
7902 struct text_pos position;
7904 eassert (STRINGP (it->string));
7905 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7906 eassert (IT_STRING_CHARPOS (*it) >= 0);
7907 position = it->current.string_pos;
7909 /* With bidi reordering, the character to display might not be the
7910 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7911 that we were reseat()ed to a new string, whose paragraph
7912 direction is not known. */
7913 if (it->bidi_p && it->bidi_it.first_elt)
7915 get_visually_first_element (it);
7916 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7919 /* Time to check for invisible text? */
7920 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7922 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7924 if (!(!it->bidi_p
7925 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7926 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7928 /* With bidi non-linear iteration, we could find
7929 ourselves far beyond the last computed stop_charpos,
7930 with several other stop positions in between that we
7931 missed. Scan them all now, in buffer's logical
7932 order, until we find and handle the last stop_charpos
7933 that precedes our current position. */
7934 handle_stop_backwards (it, it->stop_charpos);
7935 return GET_NEXT_DISPLAY_ELEMENT (it);
7937 else
7939 if (it->bidi_p)
7941 /* Take note of the stop position we just moved
7942 across, for when we will move back across it. */
7943 it->prev_stop = it->stop_charpos;
7944 /* If we are at base paragraph embedding level, take
7945 note of the last stop position seen at this
7946 level. */
7947 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7948 it->base_level_stop = it->stop_charpos;
7950 handle_stop (it);
7952 /* Since a handler may have changed IT->method, we must
7953 recurse here. */
7954 return GET_NEXT_DISPLAY_ELEMENT (it);
7957 else if (it->bidi_p
7958 /* If we are before prev_stop, we may have overstepped
7959 on our way backwards a stop_pos, and if so, we need
7960 to handle that stop_pos. */
7961 && IT_STRING_CHARPOS (*it) < it->prev_stop
7962 /* We can sometimes back up for reasons that have nothing
7963 to do with bidi reordering. E.g., compositions. The
7964 code below is only needed when we are above the base
7965 embedding level, so test for that explicitly. */
7966 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7968 /* If we lost track of base_level_stop, we have no better
7969 place for handle_stop_backwards to start from than string
7970 beginning. This happens, e.g., when we were reseated to
7971 the previous screenful of text by vertical-motion. */
7972 if (it->base_level_stop <= 0
7973 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7974 it->base_level_stop = 0;
7975 handle_stop_backwards (it, it->base_level_stop);
7976 return GET_NEXT_DISPLAY_ELEMENT (it);
7980 if (it->current.overlay_string_index >= 0)
7982 /* Get the next character from an overlay string. In overlay
7983 strings, there is no field width or padding with spaces to
7984 do. */
7985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7987 it->what = IT_EOB;
7988 return false;
7990 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7991 IT_STRING_BYTEPOS (*it),
7992 it->bidi_it.scan_dir < 0
7993 ? -1
7994 : SCHARS (it->string))
7995 && next_element_from_composition (it))
7997 return true;
7999 else if (STRING_MULTIBYTE (it->string))
8001 const unsigned char *s = (SDATA (it->string)
8002 + IT_STRING_BYTEPOS (*it));
8003 it->c = string_char_and_length (s, &it->len);
8005 else
8007 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8008 it->len = 1;
8011 else
8013 /* Get the next character from a Lisp string that is not an
8014 overlay string. Such strings come from the mode line, for
8015 example. We may have to pad with spaces, or truncate the
8016 string. See also next_element_from_c_string. */
8017 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8019 it->what = IT_EOB;
8020 return false;
8022 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8024 /* Pad with spaces. */
8025 it->c = ' ', it->len = 1;
8026 CHARPOS (position) = BYTEPOS (position) = -1;
8028 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8029 IT_STRING_BYTEPOS (*it),
8030 it->bidi_it.scan_dir < 0
8031 ? -1
8032 : it->string_nchars)
8033 && next_element_from_composition (it))
8035 return true;
8037 else if (STRING_MULTIBYTE (it->string))
8039 const unsigned char *s = (SDATA (it->string)
8040 + IT_STRING_BYTEPOS (*it));
8041 it->c = string_char_and_length (s, &it->len);
8043 else
8045 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8046 it->len = 1;
8050 /* Record what we have and where it came from. */
8051 it->what = IT_CHARACTER;
8052 it->object = it->string;
8053 it->position = position;
8054 return true;
8058 /* Load IT with next display element from C string IT->s.
8059 IT->string_nchars is the maximum number of characters to return
8060 from the string. IT->end_charpos may be greater than
8061 IT->string_nchars when this function is called, in which case we
8062 may have to return padding spaces. Value is false if end of string
8063 reached, including padding spaces. */
8065 static bool
8066 next_element_from_c_string (struct it *it)
8068 bool success_p = true;
8070 eassert (it->s);
8071 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8072 it->what = IT_CHARACTER;
8073 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8074 it->object = make_number (0);
8076 /* With bidi reordering, the character to display might not be the
8077 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8078 we were reseated to a new string, whose paragraph direction is
8079 not known. */
8080 if (it->bidi_p && it->bidi_it.first_elt)
8081 get_visually_first_element (it);
8083 /* IT's position can be greater than IT->string_nchars in case a
8084 field width or precision has been specified when the iterator was
8085 initialized. */
8086 if (IT_CHARPOS (*it) >= it->end_charpos)
8088 /* End of the game. */
8089 it->what = IT_EOB;
8090 success_p = false;
8092 else if (IT_CHARPOS (*it) >= it->string_nchars)
8094 /* Pad with spaces. */
8095 it->c = ' ', it->len = 1;
8096 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8098 else if (it->multibyte_p)
8099 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8100 else
8101 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8103 return success_p;
8107 /* Set up IT to return characters from an ellipsis, if appropriate.
8108 The definition of the ellipsis glyphs may come from a display table
8109 entry. This function fills IT with the first glyph from the
8110 ellipsis if an ellipsis is to be displayed. */
8112 static bool
8113 next_element_from_ellipsis (struct it *it)
8115 if (it->selective_display_ellipsis_p)
8116 setup_for_ellipsis (it, it->len);
8117 else
8119 /* The face at the current position may be different from the
8120 face we find after the invisible text. Remember what it
8121 was in IT->saved_face_id, and signal that it's there by
8122 setting face_before_selective_p. */
8123 it->saved_face_id = it->face_id;
8124 it->method = GET_FROM_BUFFER;
8125 it->object = it->w->contents;
8126 reseat_at_next_visible_line_start (it, true);
8127 it->face_before_selective_p = true;
8130 return GET_NEXT_DISPLAY_ELEMENT (it);
8134 /* Deliver an image display element. The iterator IT is already
8135 filled with image information (done in handle_display_prop). Value
8136 is always true. */
8139 static bool
8140 next_element_from_image (struct it *it)
8142 it->what = IT_IMAGE;
8143 return true;
8146 static bool
8147 next_element_from_xwidget (struct it *it)
8149 it->what = IT_XWIDGET;
8150 return true;
8154 /* Fill iterator IT with next display element from a stretch glyph
8155 property. IT->object is the value of the text property. Value is
8156 always true. */
8158 static bool
8159 next_element_from_stretch (struct it *it)
8161 it->what = IT_STRETCH;
8162 return true;
8165 /* Scan backwards from IT's current position until we find a stop
8166 position, or until BEGV. This is called when we find ourself
8167 before both the last known prev_stop and base_level_stop while
8168 reordering bidirectional text. */
8170 static void
8171 compute_stop_pos_backwards (struct it *it)
8173 const int SCAN_BACK_LIMIT = 1000;
8174 struct text_pos pos;
8175 struct display_pos save_current = it->current;
8176 struct text_pos save_position = it->position;
8177 ptrdiff_t charpos = IT_CHARPOS (*it);
8178 ptrdiff_t where_we_are = charpos;
8179 ptrdiff_t save_stop_pos = it->stop_charpos;
8180 ptrdiff_t save_end_pos = it->end_charpos;
8182 eassert (NILP (it->string) && !it->s);
8183 eassert (it->bidi_p);
8184 it->bidi_p = false;
8187 it->end_charpos = min (charpos + 1, ZV);
8188 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8189 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8190 reseat_1 (it, pos, false);
8191 compute_stop_pos (it);
8192 /* We must advance forward, right? */
8193 if (it->stop_charpos <= charpos)
8194 emacs_abort ();
8196 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8198 if (it->stop_charpos <= where_we_are)
8199 it->prev_stop = it->stop_charpos;
8200 else
8201 it->prev_stop = BEGV;
8202 it->bidi_p = true;
8203 it->current = save_current;
8204 it->position = save_position;
8205 it->stop_charpos = save_stop_pos;
8206 it->end_charpos = save_end_pos;
8209 /* Scan forward from CHARPOS in the current buffer/string, until we
8210 find a stop position > current IT's position. Then handle the stop
8211 position before that. This is called when we bump into a stop
8212 position while reordering bidirectional text. CHARPOS should be
8213 the last previously processed stop_pos (or BEGV/0, if none were
8214 processed yet) whose position is less that IT's current
8215 position. */
8217 static void
8218 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8220 bool bufp = !STRINGP (it->string);
8221 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8222 struct display_pos save_current = it->current;
8223 struct text_pos save_position = it->position;
8224 struct text_pos pos1;
8225 ptrdiff_t next_stop;
8227 /* Scan in strict logical order. */
8228 eassert (it->bidi_p);
8229 it->bidi_p = false;
8232 it->prev_stop = charpos;
8233 if (bufp)
8235 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8236 reseat_1 (it, pos1, false);
8238 else
8239 it->current.string_pos = string_pos (charpos, it->string);
8240 compute_stop_pos (it);
8241 /* We must advance forward, right? */
8242 if (it->stop_charpos <= it->prev_stop)
8243 emacs_abort ();
8244 charpos = it->stop_charpos;
8246 while (charpos <= where_we_are);
8248 it->bidi_p = true;
8249 it->current = save_current;
8250 it->position = save_position;
8251 next_stop = it->stop_charpos;
8252 it->stop_charpos = it->prev_stop;
8253 handle_stop (it);
8254 it->stop_charpos = next_stop;
8257 /* Load IT with the next display element from current_buffer. Value
8258 is false if end of buffer reached. IT->stop_charpos is the next
8259 position at which to stop and check for text properties or buffer
8260 end. */
8262 static bool
8263 next_element_from_buffer (struct it *it)
8265 bool success_p = true;
8267 eassert (IT_CHARPOS (*it) >= BEGV);
8268 eassert (NILP (it->string) && !it->s);
8269 eassert (!it->bidi_p
8270 || (EQ (it->bidi_it.string.lstring, Qnil)
8271 && it->bidi_it.string.s == NULL));
8273 /* With bidi reordering, the character to display might not be the
8274 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8275 we were reseat()ed to a new buffer position, which is potentially
8276 a different paragraph. */
8277 if (it->bidi_p && it->bidi_it.first_elt)
8279 get_visually_first_element (it);
8280 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8283 if (IT_CHARPOS (*it) >= it->stop_charpos)
8285 if (IT_CHARPOS (*it) >= it->end_charpos)
8287 bool overlay_strings_follow_p;
8289 /* End of the game, except when overlay strings follow that
8290 haven't been returned yet. */
8291 if (it->overlay_strings_at_end_processed_p)
8292 overlay_strings_follow_p = false;
8293 else
8295 it->overlay_strings_at_end_processed_p = true;
8296 overlay_strings_follow_p = get_overlay_strings (it, 0);
8299 if (overlay_strings_follow_p)
8300 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8301 else
8303 it->what = IT_EOB;
8304 it->position = it->current.pos;
8305 success_p = false;
8308 else if (!(!it->bidi_p
8309 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8310 || IT_CHARPOS (*it) == it->stop_charpos))
8312 /* With bidi non-linear iteration, we could find ourselves
8313 far beyond the last computed stop_charpos, with several
8314 other stop positions in between that we missed. Scan
8315 them all now, in buffer's logical order, until we find
8316 and handle the last stop_charpos that precedes our
8317 current position. */
8318 handle_stop_backwards (it, it->stop_charpos);
8319 it->ignore_overlay_strings_at_pos_p = false;
8320 return GET_NEXT_DISPLAY_ELEMENT (it);
8322 else
8324 if (it->bidi_p)
8326 /* Take note of the stop position we just moved across,
8327 for when we will move back across it. */
8328 it->prev_stop = it->stop_charpos;
8329 /* If we are at base paragraph embedding level, take
8330 note of the last stop position seen at this
8331 level. */
8332 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8333 it->base_level_stop = it->stop_charpos;
8335 handle_stop (it);
8336 it->ignore_overlay_strings_at_pos_p = false;
8337 return GET_NEXT_DISPLAY_ELEMENT (it);
8340 else if (it->bidi_p
8341 /* If we are before prev_stop, we may have overstepped on
8342 our way backwards a stop_pos, and if so, we need to
8343 handle that stop_pos. */
8344 && IT_CHARPOS (*it) < it->prev_stop
8345 /* We can sometimes back up for reasons that have nothing
8346 to do with bidi reordering. E.g., compositions. The
8347 code below is only needed when we are above the base
8348 embedding level, so test for that explicitly. */
8349 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8351 if (it->base_level_stop <= 0
8352 || IT_CHARPOS (*it) < it->base_level_stop)
8354 /* If we lost track of base_level_stop, we need to find
8355 prev_stop by looking backwards. This happens, e.g., when
8356 we were reseated to the previous screenful of text by
8357 vertical-motion. */
8358 it->base_level_stop = BEGV;
8359 compute_stop_pos_backwards (it);
8360 handle_stop_backwards (it, it->prev_stop);
8362 else
8363 handle_stop_backwards (it, it->base_level_stop);
8364 it->ignore_overlay_strings_at_pos_p = false;
8365 return GET_NEXT_DISPLAY_ELEMENT (it);
8367 else
8369 /* No face changes, overlays etc. in sight, so just return a
8370 character from current_buffer. */
8371 unsigned char *p;
8372 ptrdiff_t stop;
8374 /* We moved to the next buffer position, so any info about
8375 previously seen overlays is no longer valid. */
8376 it->ignore_overlay_strings_at_pos_p = false;
8378 /* Maybe run the redisplay end trigger hook. Performance note:
8379 This doesn't seem to cost measurable time. */
8380 if (it->redisplay_end_trigger_charpos
8381 && it->glyph_row
8382 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8383 run_redisplay_end_trigger_hook (it);
8385 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8386 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8387 stop)
8388 && next_element_from_composition (it))
8390 return true;
8393 /* Get the next character, maybe multibyte. */
8394 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8395 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8396 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8397 else
8398 it->c = *p, it->len = 1;
8400 /* Record what we have and where it came from. */
8401 it->what = IT_CHARACTER;
8402 it->object = it->w->contents;
8403 it->position = it->current.pos;
8405 /* Normally we return the character found above, except when we
8406 really want to return an ellipsis for selective display. */
8407 if (it->selective)
8409 if (it->c == '\n')
8411 /* A value of selective > 0 means hide lines indented more
8412 than that number of columns. */
8413 if (it->selective > 0
8414 && IT_CHARPOS (*it) + 1 < ZV
8415 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8416 IT_BYTEPOS (*it) + 1,
8417 it->selective))
8419 success_p = next_element_from_ellipsis (it);
8420 it->dpvec_char_len = -1;
8423 else if (it->c == '\r' && it->selective == -1)
8425 /* A value of selective == -1 means that everything from the
8426 CR to the end of the line is invisible, with maybe an
8427 ellipsis displayed for it. */
8428 success_p = next_element_from_ellipsis (it);
8429 it->dpvec_char_len = -1;
8434 /* Value is false if end of buffer reached. */
8435 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8436 return success_p;
8440 /* Run the redisplay end trigger hook for IT. */
8442 static void
8443 run_redisplay_end_trigger_hook (struct it *it)
8445 /* IT->glyph_row should be non-null, i.e. we should be actually
8446 displaying something, or otherwise we should not run the hook. */
8447 eassert (it->glyph_row);
8449 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8450 it->redisplay_end_trigger_charpos = 0;
8452 /* Since we are *trying* to run these functions, don't try to run
8453 them again, even if they get an error. */
8454 wset_redisplay_end_trigger (it->w, Qnil);
8455 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8456 make_number (charpos));
8458 /* Notice if it changed the face of the character we are on. */
8459 handle_face_prop (it);
8463 /* Deliver a composition display element. Unlike the other
8464 next_element_from_XXX, this function is not registered in the array
8465 get_next_element[]. It is called from next_element_from_buffer and
8466 next_element_from_string when necessary. */
8468 static bool
8469 next_element_from_composition (struct it *it)
8471 it->what = IT_COMPOSITION;
8472 it->len = it->cmp_it.nbytes;
8473 if (STRINGP (it->string))
8475 if (it->c < 0)
8477 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8478 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8479 return false;
8481 it->position = it->current.string_pos;
8482 it->object = it->string;
8483 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8484 IT_STRING_BYTEPOS (*it), it->string);
8486 else
8488 if (it->c < 0)
8490 IT_CHARPOS (*it) += it->cmp_it.nchars;
8491 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8492 if (it->bidi_p)
8494 if (it->bidi_it.new_paragraph)
8495 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8496 false);
8497 /* Resync the bidi iterator with IT's new position.
8498 FIXME: this doesn't support bidirectional text. */
8499 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8500 bidi_move_to_visually_next (&it->bidi_it);
8502 return false;
8504 it->position = it->current.pos;
8505 it->object = it->w->contents;
8506 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8507 IT_BYTEPOS (*it), Qnil);
8509 return true;
8514 /***********************************************************************
8515 Moving an iterator without producing glyphs
8516 ***********************************************************************/
8518 /* Check if iterator is at a position corresponding to a valid buffer
8519 position after some move_it_ call. */
8521 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8522 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8525 /* Move iterator IT to a specified buffer or X position within one
8526 line on the display without producing glyphs.
8528 OP should be a bit mask including some or all of these bits:
8529 MOVE_TO_X: Stop upon reaching x-position TO_X.
8530 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8531 Regardless of OP's value, stop upon reaching the end of the display line.
8533 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8534 This means, in particular, that TO_X includes window's horizontal
8535 scroll amount.
8537 The return value has several possible values that
8538 say what condition caused the scan to stop:
8540 MOVE_POS_MATCH_OR_ZV
8541 - when TO_POS or ZV was reached.
8543 MOVE_X_REACHED
8544 -when TO_X was reached before TO_POS or ZV were reached.
8546 MOVE_LINE_CONTINUED
8547 - when we reached the end of the display area and the line must
8548 be continued.
8550 MOVE_LINE_TRUNCATED
8551 - when we reached the end of the display area and the line is
8552 truncated.
8554 MOVE_NEWLINE_OR_CR
8555 - when we stopped at a line end, i.e. a newline or a CR and selective
8556 display is on. */
8558 static enum move_it_result
8559 move_it_in_display_line_to (struct it *it,
8560 ptrdiff_t to_charpos, int to_x,
8561 enum move_operation_enum op)
8563 enum move_it_result result = MOVE_UNDEFINED;
8564 struct glyph_row *saved_glyph_row;
8565 struct it wrap_it, atpos_it, atx_it, ppos_it;
8566 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8567 void *ppos_data = NULL;
8568 bool may_wrap = false;
8569 enum it_method prev_method = it->method;
8570 ptrdiff_t closest_pos UNINIT;
8571 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8572 bool saw_smaller_pos = prev_pos < to_charpos;
8574 /* Don't produce glyphs in produce_glyphs. */
8575 saved_glyph_row = it->glyph_row;
8576 it->glyph_row = NULL;
8578 /* Use wrap_it to save a copy of IT wherever a word wrap could
8579 occur. Use atpos_it to save a copy of IT at the desired buffer
8580 position, if found, so that we can scan ahead and check if the
8581 word later overshoots the window edge. Use atx_it similarly, for
8582 pixel positions. */
8583 wrap_it.sp = -1;
8584 atpos_it.sp = -1;
8585 atx_it.sp = -1;
8587 /* Use ppos_it under bidi reordering to save a copy of IT for the
8588 initial position. We restore that position in IT when we have
8589 scanned the entire display line without finding a match for
8590 TO_CHARPOS and all the character positions are greater than
8591 TO_CHARPOS. We then restart the scan from the initial position,
8592 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8593 the closest to TO_CHARPOS. */
8594 if (it->bidi_p)
8596 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8598 SAVE_IT (ppos_it, *it, ppos_data);
8599 closest_pos = IT_CHARPOS (*it);
8601 else
8602 closest_pos = ZV;
8605 #define BUFFER_POS_REACHED_P() \
8606 ((op & MOVE_TO_POS) != 0 \
8607 && BUFFERP (it->object) \
8608 && (IT_CHARPOS (*it) == to_charpos \
8609 || ((!it->bidi_p \
8610 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8611 && IT_CHARPOS (*it) > to_charpos) \
8612 || (it->what == IT_COMPOSITION \
8613 && ((IT_CHARPOS (*it) > to_charpos \
8614 && to_charpos >= it->cmp_it.charpos) \
8615 || (IT_CHARPOS (*it) < to_charpos \
8616 && to_charpos <= it->cmp_it.charpos)))) \
8617 && (it->method == GET_FROM_BUFFER \
8618 || (it->method == GET_FROM_DISPLAY_VECTOR \
8619 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8621 /* If there's a line-/wrap-prefix, handle it. */
8622 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8623 handle_line_prefix (it);
8625 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8626 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8628 while (true)
8630 int x, i, ascent = 0, descent = 0;
8632 /* Utility macro to reset an iterator with x, ascent, and descent. */
8633 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8634 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8635 (IT)->max_descent = descent)
8637 /* Stop if we move beyond TO_CHARPOS (after an image or a
8638 display string or stretch glyph). */
8639 if ((op & MOVE_TO_POS) != 0
8640 && BUFFERP (it->object)
8641 && it->method == GET_FROM_BUFFER
8642 && (((!it->bidi_p
8643 /* When the iterator is at base embedding level, we
8644 are guaranteed that characters are delivered for
8645 display in strictly increasing order of their
8646 buffer positions. */
8647 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8648 && IT_CHARPOS (*it) > to_charpos)
8649 || (it->bidi_p
8650 && (prev_method == GET_FROM_IMAGE
8651 || prev_method == GET_FROM_STRETCH
8652 || prev_method == GET_FROM_STRING)
8653 /* Passed TO_CHARPOS from left to right. */
8654 && ((prev_pos < to_charpos
8655 && IT_CHARPOS (*it) > to_charpos)
8656 /* Passed TO_CHARPOS from right to left. */
8657 || (prev_pos > to_charpos
8658 && IT_CHARPOS (*it) < to_charpos)))))
8660 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8662 result = MOVE_POS_MATCH_OR_ZV;
8663 break;
8665 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8666 /* If wrap_it is valid, the current position might be in a
8667 word that is wrapped. So, save the iterator in
8668 atpos_it and continue to see if wrapping happens. */
8669 SAVE_IT (atpos_it, *it, atpos_data);
8672 /* Stop when ZV reached.
8673 We used to stop here when TO_CHARPOS reached as well, but that is
8674 too soon if this glyph does not fit on this line. So we handle it
8675 explicitly below. */
8676 if (!get_next_display_element (it))
8678 result = MOVE_POS_MATCH_OR_ZV;
8679 break;
8682 if (it->line_wrap == TRUNCATE)
8684 if (BUFFER_POS_REACHED_P ())
8686 result = MOVE_POS_MATCH_OR_ZV;
8687 break;
8690 else
8692 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8694 if (IT_DISPLAYING_WHITESPACE (it))
8695 may_wrap = true;
8696 else if (may_wrap)
8698 /* We have reached a glyph that follows one or more
8699 whitespace characters. If the position is
8700 already found, we are done. */
8701 if (atpos_it.sp >= 0)
8703 RESTORE_IT (it, &atpos_it, atpos_data);
8704 result = MOVE_POS_MATCH_OR_ZV;
8705 goto done;
8707 if (atx_it.sp >= 0)
8709 RESTORE_IT (it, &atx_it, atx_data);
8710 result = MOVE_X_REACHED;
8711 goto done;
8713 /* Otherwise, we can wrap here. */
8714 SAVE_IT (wrap_it, *it, wrap_data);
8715 may_wrap = false;
8720 /* Remember the line height for the current line, in case
8721 the next element doesn't fit on the line. */
8722 ascent = it->max_ascent;
8723 descent = it->max_descent;
8725 /* The call to produce_glyphs will get the metrics of the
8726 display element IT is loaded with. Record the x-position
8727 before this display element, in case it doesn't fit on the
8728 line. */
8729 x = it->current_x;
8731 PRODUCE_GLYPHS (it);
8733 if (it->area != TEXT_AREA)
8735 prev_method = it->method;
8736 if (it->method == GET_FROM_BUFFER)
8737 prev_pos = IT_CHARPOS (*it);
8738 set_iterator_to_next (it, true);
8739 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8740 SET_TEXT_POS (this_line_min_pos,
8741 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8742 if (it->bidi_p
8743 && (op & MOVE_TO_POS)
8744 && IT_CHARPOS (*it) > to_charpos
8745 && IT_CHARPOS (*it) < closest_pos)
8746 closest_pos = IT_CHARPOS (*it);
8747 continue;
8750 /* The number of glyphs we get back in IT->nglyphs will normally
8751 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8752 character on a terminal frame, or (iii) a line end. For the
8753 second case, IT->nglyphs - 1 padding glyphs will be present.
8754 (On X frames, there is only one glyph produced for a
8755 composite character.)
8757 The behavior implemented below means, for continuation lines,
8758 that as many spaces of a TAB as fit on the current line are
8759 displayed there. For terminal frames, as many glyphs of a
8760 multi-glyph character are displayed in the current line, too.
8761 This is what the old redisplay code did, and we keep it that
8762 way. Under X, the whole shape of a complex character must
8763 fit on the line or it will be completely displayed in the
8764 next line.
8766 Note that both for tabs and padding glyphs, all glyphs have
8767 the same width. */
8768 if (it->nglyphs)
8770 /* More than one glyph or glyph doesn't fit on line. All
8771 glyphs have the same width. */
8772 int single_glyph_width = it->pixel_width / it->nglyphs;
8773 int new_x;
8774 int x_before_this_char = x;
8775 int hpos_before_this_char = it->hpos;
8777 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8779 new_x = x + single_glyph_width;
8781 /* We want to leave anything reaching TO_X to the caller. */
8782 if ((op & MOVE_TO_X) && new_x > to_x)
8784 if (BUFFER_POS_REACHED_P ())
8786 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8787 goto buffer_pos_reached;
8788 if (atpos_it.sp < 0)
8790 SAVE_IT (atpos_it, *it, atpos_data);
8791 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8794 else
8796 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8798 it->current_x = x;
8799 result = MOVE_X_REACHED;
8800 break;
8802 if (atx_it.sp < 0)
8804 SAVE_IT (atx_it, *it, atx_data);
8805 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8810 if (/* Lines are continued. */
8811 it->line_wrap != TRUNCATE
8812 && (/* And glyph doesn't fit on the line. */
8813 new_x > it->last_visible_x
8814 /* Or it fits exactly and we're on a window
8815 system frame. */
8816 || (new_x == it->last_visible_x
8817 && FRAME_WINDOW_P (it->f)
8818 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8819 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8820 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8822 bool moved_forward = false;
8824 if (/* IT->hpos == 0 means the very first glyph
8825 doesn't fit on the line, e.g. a wide image. */
8826 it->hpos == 0
8827 || (new_x == it->last_visible_x
8828 && FRAME_WINDOW_P (it->f)))
8830 ++it->hpos;
8831 it->current_x = new_x;
8833 /* The character's last glyph just barely fits
8834 in this row. */
8835 if (i == it->nglyphs - 1)
8837 /* If this is the destination position,
8838 return a position *before* it in this row,
8839 now that we know it fits in this row. */
8840 if (BUFFER_POS_REACHED_P ())
8842 bool can_wrap = true;
8844 /* If we are at a whitespace character
8845 that barely fits on this screen line,
8846 but the next character is also
8847 whitespace, we cannot wrap here. */
8848 if (it->line_wrap == WORD_WRAP
8849 && wrap_it.sp >= 0
8850 && may_wrap
8851 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8853 struct it tem_it;
8854 void *tem_data = NULL;
8856 SAVE_IT (tem_it, *it, tem_data);
8857 set_iterator_to_next (it, true);
8858 if (get_next_display_element (it)
8859 && IT_DISPLAYING_WHITESPACE (it))
8860 can_wrap = false;
8861 RESTORE_IT (it, &tem_it, tem_data);
8863 if (it->line_wrap != WORD_WRAP
8864 || wrap_it.sp < 0
8865 /* If we've just found whitespace
8866 where we can wrap, effectively
8867 ignore the previous wrap point --
8868 it is no longer relevant, but we
8869 won't have an opportunity to
8870 update it, since we've reached
8871 the edge of this screen line. */
8872 || (may_wrap && can_wrap
8873 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8875 it->hpos = hpos_before_this_char;
8876 it->current_x = x_before_this_char;
8877 result = MOVE_POS_MATCH_OR_ZV;
8878 break;
8880 if (it->line_wrap == WORD_WRAP
8881 && atpos_it.sp < 0)
8883 SAVE_IT (atpos_it, *it, atpos_data);
8884 atpos_it.current_x = x_before_this_char;
8885 atpos_it.hpos = hpos_before_this_char;
8889 prev_method = it->method;
8890 if (it->method == GET_FROM_BUFFER)
8891 prev_pos = IT_CHARPOS (*it);
8892 set_iterator_to_next (it, true);
8893 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8894 SET_TEXT_POS (this_line_min_pos,
8895 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8896 /* On graphical terminals, newlines may
8897 "overflow" into the fringe if
8898 overflow-newline-into-fringe is non-nil.
8899 On text terminals, and on graphical
8900 terminals with no right margin, newlines
8901 may overflow into the last glyph on the
8902 display line.*/
8903 if (!FRAME_WINDOW_P (it->f)
8904 || ((it->bidi_p
8905 && it->bidi_it.paragraph_dir == R2L)
8906 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8907 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8908 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8910 if (!get_next_display_element (it))
8912 result = MOVE_POS_MATCH_OR_ZV;
8913 break;
8915 moved_forward = true;
8916 if (BUFFER_POS_REACHED_P ())
8918 if (ITERATOR_AT_END_OF_LINE_P (it))
8919 result = MOVE_POS_MATCH_OR_ZV;
8920 else
8921 result = MOVE_LINE_CONTINUED;
8922 break;
8924 if (ITERATOR_AT_END_OF_LINE_P (it)
8925 && (it->line_wrap != WORD_WRAP
8926 || wrap_it.sp < 0
8927 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8929 result = MOVE_NEWLINE_OR_CR;
8930 break;
8935 else
8936 IT_RESET_X_ASCENT_DESCENT (it);
8938 /* If the screen line ends with whitespace, and we
8939 are under word-wrap, don't use wrap_it: it is no
8940 longer relevant, but we won't have an opportunity
8941 to update it, since we are done with this screen
8942 line. */
8943 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8944 /* If the character after the one which set the
8945 may_wrap flag is also whitespace, we can't
8946 wrap here, since the screen line cannot be
8947 wrapped in the middle of whitespace.
8948 Therefore, wrap_it _is_ relevant in that
8949 case. */
8950 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8952 /* If we've found TO_X, go back there, as we now
8953 know the last word fits on this screen line. */
8954 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8955 && atx_it.sp >= 0)
8957 RESTORE_IT (it, &atx_it, atx_data);
8958 atpos_it.sp = -1;
8959 atx_it.sp = -1;
8960 result = MOVE_X_REACHED;
8961 break;
8964 else if (wrap_it.sp >= 0)
8966 RESTORE_IT (it, &wrap_it, wrap_data);
8967 atpos_it.sp = -1;
8968 atx_it.sp = -1;
8971 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8972 IT_CHARPOS (*it)));
8973 result = MOVE_LINE_CONTINUED;
8974 break;
8977 if (BUFFER_POS_REACHED_P ())
8979 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8980 goto buffer_pos_reached;
8981 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8983 SAVE_IT (atpos_it, *it, atpos_data);
8984 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8988 if (new_x > it->first_visible_x)
8990 /* Glyph is visible. Increment number of glyphs that
8991 would be displayed. */
8992 ++it->hpos;
8996 if (result != MOVE_UNDEFINED)
8997 break;
8999 else if (BUFFER_POS_REACHED_P ())
9001 buffer_pos_reached:
9002 IT_RESET_X_ASCENT_DESCENT (it);
9003 result = MOVE_POS_MATCH_OR_ZV;
9004 break;
9006 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9008 /* Stop when TO_X specified and reached. This check is
9009 necessary here because of lines consisting of a line end,
9010 only. The line end will not produce any glyphs and we
9011 would never get MOVE_X_REACHED. */
9012 eassert (it->nglyphs == 0);
9013 result = MOVE_X_REACHED;
9014 break;
9017 /* Is this a line end? If yes, we're done. */
9018 if (ITERATOR_AT_END_OF_LINE_P (it))
9020 /* If we are past TO_CHARPOS, but never saw any character
9021 positions smaller than TO_CHARPOS, return
9022 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9023 did. */
9024 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9026 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9028 if (closest_pos < ZV)
9030 RESTORE_IT (it, &ppos_it, ppos_data);
9031 /* Don't recurse if closest_pos is equal to
9032 to_charpos, since we have just tried that. */
9033 if (closest_pos != to_charpos)
9034 move_it_in_display_line_to (it, closest_pos, -1,
9035 MOVE_TO_POS);
9036 result = MOVE_POS_MATCH_OR_ZV;
9038 else
9039 goto buffer_pos_reached;
9041 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9042 && IT_CHARPOS (*it) > to_charpos)
9043 goto buffer_pos_reached;
9044 else
9045 result = MOVE_NEWLINE_OR_CR;
9047 else
9048 result = MOVE_NEWLINE_OR_CR;
9049 /* If we've processed the newline, make sure this flag is
9050 reset, as it must only be set when the newline itself is
9051 processed. */
9052 if (result == MOVE_NEWLINE_OR_CR)
9053 it->constrain_row_ascent_descent_p = false;
9054 break;
9057 prev_method = it->method;
9058 if (it->method == GET_FROM_BUFFER)
9059 prev_pos = IT_CHARPOS (*it);
9060 /* The current display element has been consumed. Advance
9061 to the next. */
9062 set_iterator_to_next (it, true);
9063 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9064 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9065 if (IT_CHARPOS (*it) < to_charpos)
9066 saw_smaller_pos = true;
9067 if (it->bidi_p
9068 && (op & MOVE_TO_POS)
9069 && IT_CHARPOS (*it) >= to_charpos
9070 && IT_CHARPOS (*it) < closest_pos)
9071 closest_pos = IT_CHARPOS (*it);
9073 /* Stop if lines are truncated and IT's current x-position is
9074 past the right edge of the window now. */
9075 if (it->line_wrap == TRUNCATE
9076 && it->current_x >= it->last_visible_x)
9078 if (!FRAME_WINDOW_P (it->f)
9079 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9080 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9081 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9082 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9084 bool at_eob_p = false;
9086 if ((at_eob_p = !get_next_display_element (it))
9087 || BUFFER_POS_REACHED_P ()
9088 /* If we are past TO_CHARPOS, but never saw any
9089 character positions smaller than TO_CHARPOS,
9090 return MOVE_POS_MATCH_OR_ZV, like the
9091 unidirectional display did. */
9092 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9093 && !saw_smaller_pos
9094 && IT_CHARPOS (*it) > to_charpos))
9096 if (it->bidi_p
9097 && !BUFFER_POS_REACHED_P ()
9098 && !at_eob_p && closest_pos < ZV)
9100 RESTORE_IT (it, &ppos_it, ppos_data);
9101 if (closest_pos != to_charpos)
9102 move_it_in_display_line_to (it, closest_pos, -1,
9103 MOVE_TO_POS);
9105 result = MOVE_POS_MATCH_OR_ZV;
9106 break;
9108 if (ITERATOR_AT_END_OF_LINE_P (it))
9110 result = MOVE_NEWLINE_OR_CR;
9111 break;
9114 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9115 && !saw_smaller_pos
9116 && IT_CHARPOS (*it) > to_charpos)
9118 if (closest_pos < ZV)
9120 RESTORE_IT (it, &ppos_it, ppos_data);
9121 if (closest_pos != to_charpos)
9122 move_it_in_display_line_to (it, closest_pos, -1,
9123 MOVE_TO_POS);
9125 result = MOVE_POS_MATCH_OR_ZV;
9126 break;
9128 result = MOVE_LINE_TRUNCATED;
9129 break;
9131 #undef IT_RESET_X_ASCENT_DESCENT
9134 #undef BUFFER_POS_REACHED_P
9136 /* If we scanned beyond TO_POS, restore the saved iterator either to
9137 the wrap point (if found), or to atpos/atx location. We decide which
9138 data to use to restore the saved iterator state by their X coordinates,
9139 since buffer positions might increase non-monotonically with screen
9140 coordinates due to bidi reordering. */
9141 if (result == MOVE_LINE_CONTINUED
9142 && it->line_wrap == WORD_WRAP
9143 && wrap_it.sp >= 0
9144 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9145 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9146 RESTORE_IT (it, &wrap_it, wrap_data);
9147 else if (atpos_it.sp >= 0)
9148 RESTORE_IT (it, &atpos_it, atpos_data);
9149 else if (atx_it.sp >= 0)
9150 RESTORE_IT (it, &atx_it, atx_data);
9152 done:
9154 if (atpos_data)
9155 bidi_unshelve_cache (atpos_data, true);
9156 if (atx_data)
9157 bidi_unshelve_cache (atx_data, true);
9158 if (wrap_data)
9159 bidi_unshelve_cache (wrap_data, true);
9160 if (ppos_data)
9161 bidi_unshelve_cache (ppos_data, true);
9163 /* Restore the iterator settings altered at the beginning of this
9164 function. */
9165 it->glyph_row = saved_glyph_row;
9166 return result;
9169 /* For external use. */
9170 void
9171 move_it_in_display_line (struct it *it,
9172 ptrdiff_t to_charpos, int to_x,
9173 enum move_operation_enum op)
9175 if (it->line_wrap == WORD_WRAP
9176 && (op & MOVE_TO_X))
9178 struct it save_it;
9179 void *save_data = NULL;
9180 int skip;
9182 SAVE_IT (save_it, *it, save_data);
9183 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9184 /* When word-wrap is on, TO_X may lie past the end
9185 of a wrapped line. Then it->current is the
9186 character on the next line, so backtrack to the
9187 space before the wrap point. */
9188 if (skip == MOVE_LINE_CONTINUED)
9190 int prev_x = max (it->current_x - 1, 0);
9191 RESTORE_IT (it, &save_it, save_data);
9192 move_it_in_display_line_to
9193 (it, -1, prev_x, MOVE_TO_X);
9195 else
9196 bidi_unshelve_cache (save_data, true);
9198 else
9199 move_it_in_display_line_to (it, to_charpos, to_x, op);
9203 /* Move IT forward until it satisfies one or more of the criteria in
9204 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9206 OP is a bit-mask that specifies where to stop, and in particular,
9207 which of those four position arguments makes a difference. See the
9208 description of enum move_operation_enum.
9210 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9211 screen line, this function will set IT to the next position that is
9212 displayed to the right of TO_CHARPOS on the screen.
9214 Return the maximum pixel length of any line scanned but never more
9215 than it.last_visible_x. */
9218 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9220 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9221 int line_height, line_start_x = 0, reached = 0;
9222 int max_current_x = 0;
9223 void *backup_data = NULL;
9225 for (;;)
9227 if (op & MOVE_TO_VPOS)
9229 /* If no TO_CHARPOS and no TO_X specified, stop at the
9230 start of the line TO_VPOS. */
9231 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9233 if (it->vpos == to_vpos)
9235 reached = 1;
9236 break;
9238 else
9239 skip = move_it_in_display_line_to (it, -1, -1, 0);
9241 else
9243 /* TO_VPOS >= 0 means stop at TO_X in the line at
9244 TO_VPOS, or at TO_POS, whichever comes first. */
9245 if (it->vpos == to_vpos)
9247 reached = 2;
9248 break;
9251 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9253 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9255 reached = 3;
9256 break;
9258 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9260 /* We have reached TO_X but not in the line we want. */
9261 skip = move_it_in_display_line_to (it, to_charpos,
9262 -1, MOVE_TO_POS);
9263 if (skip == MOVE_POS_MATCH_OR_ZV)
9265 reached = 4;
9266 break;
9271 else if (op & MOVE_TO_Y)
9273 struct it it_backup;
9275 if (it->line_wrap == WORD_WRAP)
9276 SAVE_IT (it_backup, *it, backup_data);
9278 /* TO_Y specified means stop at TO_X in the line containing
9279 TO_Y---or at TO_CHARPOS if this is reached first. The
9280 problem is that we can't really tell whether the line
9281 contains TO_Y before we have completely scanned it, and
9282 this may skip past TO_X. What we do is to first scan to
9283 TO_X.
9285 If TO_X is not specified, use a TO_X of zero. The reason
9286 is to make the outcome of this function more predictable.
9287 If we didn't use TO_X == 0, we would stop at the end of
9288 the line which is probably not what a caller would expect
9289 to happen. */
9290 skip = move_it_in_display_line_to
9291 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9292 (MOVE_TO_X | (op & MOVE_TO_POS)));
9294 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9295 if (skip == MOVE_POS_MATCH_OR_ZV)
9296 reached = 5;
9297 else if (skip == MOVE_X_REACHED)
9299 /* If TO_X was reached, we want to know whether TO_Y is
9300 in the line. We know this is the case if the already
9301 scanned glyphs make the line tall enough. Otherwise,
9302 we must check by scanning the rest of the line. */
9303 line_height = it->max_ascent + it->max_descent;
9304 if (to_y >= it->current_y
9305 && to_y < it->current_y + line_height)
9307 reached = 6;
9308 break;
9310 SAVE_IT (it_backup, *it, backup_data);
9311 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9312 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9313 op & MOVE_TO_POS);
9314 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9315 line_height = it->max_ascent + it->max_descent;
9316 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9318 if (to_y >= it->current_y
9319 && to_y < it->current_y + line_height)
9321 /* If TO_Y is in this line and TO_X was reached
9322 above, we scanned too far. We have to restore
9323 IT's settings to the ones before skipping. But
9324 keep the more accurate values of max_ascent and
9325 max_descent we've found while skipping the rest
9326 of the line, for the sake of callers, such as
9327 pos_visible_p, that need to know the line
9328 height. */
9329 int max_ascent = it->max_ascent;
9330 int max_descent = it->max_descent;
9332 RESTORE_IT (it, &it_backup, backup_data);
9333 it->max_ascent = max_ascent;
9334 it->max_descent = max_descent;
9335 reached = 6;
9337 else
9339 skip = skip2;
9340 if (skip == MOVE_POS_MATCH_OR_ZV)
9341 reached = 7;
9344 else
9346 /* Check whether TO_Y is in this line. */
9347 line_height = it->max_ascent + it->max_descent;
9348 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9350 if (to_y >= it->current_y
9351 && to_y < it->current_y + line_height)
9353 if (to_y > it->current_y)
9354 max_current_x = max (it->current_x, max_current_x);
9356 /* When word-wrap is on, TO_X may lie past the end
9357 of a wrapped line. Then it->current is the
9358 character on the next line, so backtrack to the
9359 space before the wrap point. */
9360 if (skip == MOVE_LINE_CONTINUED
9361 && it->line_wrap == WORD_WRAP)
9363 int prev_x = max (it->current_x - 1, 0);
9364 RESTORE_IT (it, &it_backup, backup_data);
9365 skip = move_it_in_display_line_to
9366 (it, -1, prev_x, MOVE_TO_X);
9369 reached = 6;
9373 if (reached)
9375 max_current_x = max (it->current_x, max_current_x);
9376 break;
9379 else if (BUFFERP (it->object)
9380 && (it->method == GET_FROM_BUFFER
9381 || it->method == GET_FROM_STRETCH)
9382 && IT_CHARPOS (*it) >= to_charpos
9383 /* Under bidi iteration, a call to set_iterator_to_next
9384 can scan far beyond to_charpos if the initial
9385 portion of the next line needs to be reordered. In
9386 that case, give move_it_in_display_line_to another
9387 chance below. */
9388 && !(it->bidi_p
9389 && it->bidi_it.scan_dir == -1))
9390 skip = MOVE_POS_MATCH_OR_ZV;
9391 else
9392 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9394 switch (skip)
9396 case MOVE_POS_MATCH_OR_ZV:
9397 max_current_x = max (it->current_x, max_current_x);
9398 reached = 8;
9399 goto out;
9401 case MOVE_NEWLINE_OR_CR:
9402 max_current_x = max (it->current_x, max_current_x);
9403 set_iterator_to_next (it, true);
9404 it->continuation_lines_width = 0;
9405 break;
9407 case MOVE_LINE_TRUNCATED:
9408 max_current_x = it->last_visible_x;
9409 it->continuation_lines_width = 0;
9410 reseat_at_next_visible_line_start (it, false);
9411 if ((op & MOVE_TO_POS) != 0
9412 && IT_CHARPOS (*it) > to_charpos)
9414 reached = 9;
9415 goto out;
9417 break;
9419 case MOVE_LINE_CONTINUED:
9420 max_current_x = it->last_visible_x;
9421 /* For continued lines ending in a tab, some of the glyphs
9422 associated with the tab are displayed on the current
9423 line. Since it->current_x does not include these glyphs,
9424 we use it->last_visible_x instead. */
9425 if (it->c == '\t')
9427 it->continuation_lines_width += it->last_visible_x;
9428 /* When moving by vpos, ensure that the iterator really
9429 advances to the next line (bug#847, bug#969). Fixme:
9430 do we need to do this in other circumstances? */
9431 if (it->current_x != it->last_visible_x
9432 && (op & MOVE_TO_VPOS)
9433 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9435 line_start_x = it->current_x + it->pixel_width
9436 - it->last_visible_x;
9437 if (FRAME_WINDOW_P (it->f))
9439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9440 struct font *face_font = face->font;
9442 /* When display_line produces a continued line
9443 that ends in a TAB, it skips a tab stop that
9444 is closer than the font's space character
9445 width (see x_produce_glyphs where it produces
9446 the stretch glyph which represents a TAB).
9447 We need to reproduce the same logic here. */
9448 eassert (face_font);
9449 if (face_font)
9451 if (line_start_x < face_font->space_width)
9452 line_start_x
9453 += it->tab_width * face_font->space_width;
9456 set_iterator_to_next (it, false);
9459 else
9460 it->continuation_lines_width += it->current_x;
9461 break;
9463 default:
9464 emacs_abort ();
9467 /* Reset/increment for the next run. */
9468 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9469 it->current_x = line_start_x;
9470 line_start_x = 0;
9471 it->hpos = 0;
9472 it->current_y += it->max_ascent + it->max_descent;
9473 ++it->vpos;
9474 last_height = it->max_ascent + it->max_descent;
9475 it->max_ascent = it->max_descent = 0;
9478 out:
9480 /* On text terminals, we may stop at the end of a line in the middle
9481 of a multi-character glyph. If the glyph itself is continued,
9482 i.e. it is actually displayed on the next line, don't treat this
9483 stopping point as valid; move to the next line instead (unless
9484 that brings us offscreen). */
9485 if (!FRAME_WINDOW_P (it->f)
9486 && op & MOVE_TO_POS
9487 && IT_CHARPOS (*it) == to_charpos
9488 && it->what == IT_CHARACTER
9489 && it->nglyphs > 1
9490 && it->line_wrap == WINDOW_WRAP
9491 && it->current_x == it->last_visible_x - 1
9492 && it->c != '\n'
9493 && it->c != '\t'
9494 && it->w->window_end_valid
9495 && it->vpos < it->w->window_end_vpos)
9497 it->continuation_lines_width += it->current_x;
9498 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9499 it->current_y += it->max_ascent + it->max_descent;
9500 ++it->vpos;
9501 last_height = it->max_ascent + it->max_descent;
9504 if (backup_data)
9505 bidi_unshelve_cache (backup_data, true);
9507 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9509 return max_current_x;
9513 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9515 If DY > 0, move IT backward at least that many pixels. DY = 0
9516 means move IT backward to the preceding line start or BEGV. This
9517 function may move over more than DY pixels if IT->current_y - DY
9518 ends up in the middle of a line; in this case IT->current_y will be
9519 set to the top of the line moved to. */
9521 void
9522 move_it_vertically_backward (struct it *it, int dy)
9524 int nlines, h;
9525 struct it it2, it3;
9526 void *it2data = NULL, *it3data = NULL;
9527 ptrdiff_t start_pos;
9528 int nchars_per_row
9529 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9530 ptrdiff_t pos_limit;
9532 move_further_back:
9533 eassert (dy >= 0);
9535 start_pos = IT_CHARPOS (*it);
9537 /* Estimate how many newlines we must move back. */
9538 nlines = max (1, dy / default_line_pixel_height (it->w));
9539 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9540 pos_limit = BEGV;
9541 else
9542 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9544 /* Set the iterator's position that many lines back. But don't go
9545 back more than NLINES full screen lines -- this wins a day with
9546 buffers which have very long lines. */
9547 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9548 back_to_previous_visible_line_start (it);
9550 /* Reseat the iterator here. When moving backward, we don't want
9551 reseat to skip forward over invisible text, set up the iterator
9552 to deliver from overlay strings at the new position etc. So,
9553 use reseat_1 here. */
9554 reseat_1 (it, it->current.pos, true);
9556 /* We are now surely at a line start. */
9557 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9558 reordering is in effect. */
9559 it->continuation_lines_width = 0;
9561 /* Move forward and see what y-distance we moved. First move to the
9562 start of the next line so that we get its height. We need this
9563 height to be able to tell whether we reached the specified
9564 y-distance. */
9565 SAVE_IT (it2, *it, it2data);
9566 it2.max_ascent = it2.max_descent = 0;
9569 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9570 MOVE_TO_POS | MOVE_TO_VPOS);
9572 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9573 /* If we are in a display string which starts at START_POS,
9574 and that display string includes a newline, and we are
9575 right after that newline (i.e. at the beginning of a
9576 display line), exit the loop, because otherwise we will
9577 infloop, since move_it_to will see that it is already at
9578 START_POS and will not move. */
9579 || (it2.method == GET_FROM_STRING
9580 && IT_CHARPOS (it2) == start_pos
9581 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9582 eassert (IT_CHARPOS (*it) >= BEGV);
9583 SAVE_IT (it3, it2, it3data);
9585 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9586 eassert (IT_CHARPOS (*it) >= BEGV);
9587 /* H is the actual vertical distance from the position in *IT
9588 and the starting position. */
9589 h = it2.current_y - it->current_y;
9590 /* NLINES is the distance in number of lines. */
9591 nlines = it2.vpos - it->vpos;
9593 /* Correct IT's y and vpos position
9594 so that they are relative to the starting point. */
9595 it->vpos -= nlines;
9596 it->current_y -= h;
9598 if (dy == 0)
9600 /* DY == 0 means move to the start of the screen line. The
9601 value of nlines is > 0 if continuation lines were involved,
9602 or if the original IT position was at start of a line. */
9603 RESTORE_IT (it, it, it2data);
9604 if (nlines > 0)
9605 move_it_by_lines (it, nlines);
9606 /* The above code moves us to some position NLINES down,
9607 usually to its first glyph (leftmost in an L2R line), but
9608 that's not necessarily the start of the line, under bidi
9609 reordering. We want to get to the character position
9610 that is immediately after the newline of the previous
9611 line. */
9612 if (it->bidi_p
9613 && !it->continuation_lines_width
9614 && !STRINGP (it->string)
9615 && IT_CHARPOS (*it) > BEGV
9616 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9618 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9620 DEC_BOTH (cp, bp);
9621 cp = find_newline_no_quit (cp, bp, -1, NULL);
9622 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9624 bidi_unshelve_cache (it3data, true);
9626 else
9628 /* The y-position we try to reach, relative to *IT.
9629 Note that H has been subtracted in front of the if-statement. */
9630 int target_y = it->current_y + h - dy;
9631 int y0 = it3.current_y;
9632 int y1;
9633 int line_height;
9635 RESTORE_IT (&it3, &it3, it3data);
9636 y1 = line_bottom_y (&it3);
9637 line_height = y1 - y0;
9638 RESTORE_IT (it, it, it2data);
9639 /* If we did not reach target_y, try to move further backward if
9640 we can. If we moved too far backward, try to move forward. */
9641 if (target_y < it->current_y
9642 /* This is heuristic. In a window that's 3 lines high, with
9643 a line height of 13 pixels each, recentering with point
9644 on the bottom line will try to move -39/2 = 19 pixels
9645 backward. Try to avoid moving into the first line. */
9646 && (it->current_y - target_y
9647 > min (window_box_height (it->w), line_height * 2 / 3))
9648 && IT_CHARPOS (*it) > BEGV)
9650 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9651 target_y - it->current_y));
9652 dy = it->current_y - target_y;
9653 goto move_further_back;
9655 else if (target_y >= it->current_y + line_height
9656 && IT_CHARPOS (*it) < ZV)
9658 /* Should move forward by at least one line, maybe more.
9660 Note: Calling move_it_by_lines can be expensive on
9661 terminal frames, where compute_motion is used (via
9662 vmotion) to do the job, when there are very long lines
9663 and truncate-lines is nil. That's the reason for
9664 treating terminal frames specially here. */
9666 if (!FRAME_WINDOW_P (it->f))
9667 move_it_vertically (it, target_y - it->current_y);
9668 else
9672 move_it_by_lines (it, 1);
9674 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9681 /* Move IT by a specified amount of pixel lines DY. DY negative means
9682 move backwards. DY = 0 means move to start of screen line. At the
9683 end, IT will be on the start of a screen line. */
9685 void
9686 move_it_vertically (struct it *it, int dy)
9688 if (dy <= 0)
9689 move_it_vertically_backward (it, -dy);
9690 else
9692 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9693 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9694 MOVE_TO_POS | MOVE_TO_Y);
9695 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9697 /* If buffer ends in ZV without a newline, move to the start of
9698 the line to satisfy the post-condition. */
9699 if (IT_CHARPOS (*it) == ZV
9700 && ZV > BEGV
9701 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9702 move_it_by_lines (it, 0);
9707 /* Move iterator IT past the end of the text line it is in. */
9709 void
9710 move_it_past_eol (struct it *it)
9712 enum move_it_result rc;
9714 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9715 if (rc == MOVE_NEWLINE_OR_CR)
9716 set_iterator_to_next (it, false);
9720 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9721 negative means move up. DVPOS == 0 means move to the start of the
9722 screen line.
9724 Optimization idea: If we would know that IT->f doesn't use
9725 a face with proportional font, we could be faster for
9726 truncate-lines nil. */
9728 void
9729 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9732 /* The commented-out optimization uses vmotion on terminals. This
9733 gives bad results, because elements like it->what, on which
9734 callers such as pos_visible_p rely, aren't updated. */
9735 /* struct position pos;
9736 if (!FRAME_WINDOW_P (it->f))
9738 struct text_pos textpos;
9740 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9741 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9742 reseat (it, textpos, true);
9743 it->vpos += pos.vpos;
9744 it->current_y += pos.vpos;
9746 else */
9748 if (dvpos == 0)
9750 /* DVPOS == 0 means move to the start of the screen line. */
9751 move_it_vertically_backward (it, 0);
9752 /* Let next call to line_bottom_y calculate real line height. */
9753 last_height = 0;
9755 else if (dvpos > 0)
9757 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9758 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9760 /* Only move to the next buffer position if we ended up in a
9761 string from display property, not in an overlay string
9762 (before-string or after-string). That is because the
9763 latter don't conceal the underlying buffer position, so
9764 we can ask to move the iterator to the exact position we
9765 are interested in. Note that, even if we are already at
9766 IT_CHARPOS (*it), the call below is not a no-op, as it
9767 will detect that we are at the end of the string, pop the
9768 iterator, and compute it->current_x and it->hpos
9769 correctly. */
9770 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9771 -1, -1, -1, MOVE_TO_POS);
9774 else
9776 struct it it2;
9777 void *it2data = NULL;
9778 ptrdiff_t start_charpos, i;
9779 int nchars_per_row
9780 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9781 bool hit_pos_limit = false;
9782 ptrdiff_t pos_limit;
9784 /* Start at the beginning of the screen line containing IT's
9785 position. This may actually move vertically backwards,
9786 in case of overlays, so adjust dvpos accordingly. */
9787 dvpos += it->vpos;
9788 move_it_vertically_backward (it, 0);
9789 dvpos -= it->vpos;
9791 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9792 screen lines, and reseat the iterator there. */
9793 start_charpos = IT_CHARPOS (*it);
9794 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9795 pos_limit = BEGV;
9796 else
9797 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9799 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9800 back_to_previous_visible_line_start (it);
9801 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9802 hit_pos_limit = true;
9803 reseat (it, it->current.pos, true);
9805 /* Move further back if we end up in a string or an image. */
9806 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9808 /* First try to move to start of display line. */
9809 dvpos += it->vpos;
9810 move_it_vertically_backward (it, 0);
9811 dvpos -= it->vpos;
9812 if (IT_POS_VALID_AFTER_MOVE_P (it))
9813 break;
9814 /* If start of line is still in string or image,
9815 move further back. */
9816 back_to_previous_visible_line_start (it);
9817 reseat (it, it->current.pos, true);
9818 dvpos--;
9821 it->current_x = it->hpos = 0;
9823 /* Above call may have moved too far if continuation lines
9824 are involved. Scan forward and see if it did. */
9825 SAVE_IT (it2, *it, it2data);
9826 it2.vpos = it2.current_y = 0;
9827 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9828 it->vpos -= it2.vpos;
9829 it->current_y -= it2.current_y;
9830 it->current_x = it->hpos = 0;
9832 /* If we moved too far back, move IT some lines forward. */
9833 if (it2.vpos > -dvpos)
9835 int delta = it2.vpos + dvpos;
9837 RESTORE_IT (&it2, &it2, it2data);
9838 SAVE_IT (it2, *it, it2data);
9839 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9840 /* Move back again if we got too far ahead. */
9841 if (IT_CHARPOS (*it) >= start_charpos)
9842 RESTORE_IT (it, &it2, it2data);
9843 else
9844 bidi_unshelve_cache (it2data, true);
9846 else if (hit_pos_limit && pos_limit > BEGV
9847 && dvpos < 0 && it2.vpos < -dvpos)
9849 /* If we hit the limit, but still didn't make it far enough
9850 back, that means there's a display string with a newline
9851 covering a large chunk of text, and that caused
9852 back_to_previous_visible_line_start try to go too far.
9853 Punish those who commit such atrocities by going back
9854 until we've reached DVPOS, after lifting the limit, which
9855 could make it slow for very long lines. "If it hurts,
9856 don't do that!" */
9857 dvpos += it2.vpos;
9858 RESTORE_IT (it, it, it2data);
9859 for (i = -dvpos; i > 0; --i)
9861 back_to_previous_visible_line_start (it);
9862 it->vpos--;
9864 reseat_1 (it, it->current.pos, true);
9866 else
9867 RESTORE_IT (it, it, it2data);
9871 /* Return true if IT points into the middle of a display vector. */
9873 bool
9874 in_display_vector_p (struct it *it)
9876 return (it->method == GET_FROM_DISPLAY_VECTOR
9877 && it->current.dpvec_index > 0
9878 && it->dpvec + it->current.dpvec_index != it->dpend);
9881 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9882 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9883 WINDOW must be a live window and defaults to the selected one. The
9884 return value is a cons of the maximum pixel-width of any text line and
9885 the maximum pixel-height of all text lines.
9887 The optional argument FROM, if non-nil, specifies the first text
9888 position and defaults to the minimum accessible position of the buffer.
9889 If FROM is t, use the minimum accessible position that starts a
9890 non-empty line. TO, if non-nil, specifies the last text position and
9891 defaults to the maximum accessible position of the buffer. If TO is t,
9892 use the maximum accessible position that ends a non-empty line.
9894 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9895 width that can be returned. X-LIMIT nil or omitted, means to use the
9896 pixel-width of WINDOW's body; use this if you want to know how high
9897 WINDOW should be become in order to fit all of its buffer's text with
9898 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9899 if you intend to change WINDOW's width. In any case, text whose
9900 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9901 of long lines can take some time, it's always a good idea to make this
9902 argument as small as possible; in particular, if the buffer contains
9903 long lines that shall be truncated anyway.
9905 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9906 height (excluding the height of the mode- or header-line, if any) that
9907 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9908 ignored. Since calculating the text height of a large buffer can take
9909 some time, it makes sense to specify this argument if the size of the
9910 buffer is large or unknown.
9912 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9913 include the height of the mode- or header-line of WINDOW in the return
9914 value. If it is either the symbol `mode-line' or `header-line', include
9915 only the height of that line, if present, in the return value. If t,
9916 include the height of both, if present, in the return value. */)
9917 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9918 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9920 struct window *w = decode_live_window (window);
9921 Lisp_Object buffer = w->contents;
9922 struct buffer *b;
9923 struct it it;
9924 struct buffer *old_b = NULL;
9925 ptrdiff_t start, end, pos;
9926 struct text_pos startp;
9927 void *itdata = NULL;
9928 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9930 CHECK_BUFFER (buffer);
9931 b = XBUFFER (buffer);
9933 if (b != current_buffer)
9935 old_b = current_buffer;
9936 set_buffer_internal (b);
9939 if (NILP (from))
9940 start = BEGV;
9941 else if (EQ (from, Qt))
9943 start = pos = BEGV;
9944 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9945 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9946 start = pos;
9947 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9948 start = pos;
9950 else
9952 CHECK_NUMBER_COERCE_MARKER (from);
9953 start = min (max (XINT (from), BEGV), ZV);
9956 if (NILP (to))
9957 end = ZV;
9958 else if (EQ (to, Qt))
9960 end = pos = ZV;
9961 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9962 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9963 end = pos;
9964 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9965 end = pos;
9967 else
9969 CHECK_NUMBER_COERCE_MARKER (to);
9970 end = max (start, min (XINT (to), ZV));
9973 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9974 max_x = XINT (x_limit);
9976 if (NILP (y_limit))
9977 max_y = INT_MAX;
9978 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9979 max_y = XINT (y_limit);
9981 itdata = bidi_shelve_cache ();
9982 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9983 start_display (&it, w, startp);
9985 if (NILP (x_limit))
9986 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9987 else
9989 it.last_visible_x = max_x;
9990 /* Actually, we never want move_it_to stop at to_x. But to make
9991 sure that move_it_in_display_line_to always moves far enough,
9992 we set it to INT_MAX and specify MOVE_TO_X. */
9993 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9994 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9995 /* Don't return more than X-LIMIT. */
9996 if (x > max_x)
9997 x = max_x;
10000 /* Subtract height of header-line which was counted automatically by
10001 start_display. */
10002 y = it.current_y + it.max_ascent + it.max_descent
10003 - WINDOW_HEADER_LINE_HEIGHT (w);
10004 /* Don't return more than Y-LIMIT. */
10005 if (y > max_y)
10006 y = max_y;
10008 if (EQ (mode_and_header_line, Qheader_line)
10009 || EQ (mode_and_header_line, Qt))
10010 /* Re-add height of header-line as requested. */
10011 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10013 if (EQ (mode_and_header_line, Qmode_line)
10014 || EQ (mode_and_header_line, Qt))
10015 /* Add height of mode-line as requested. */
10016 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10018 bidi_unshelve_cache (itdata, false);
10020 if (old_b)
10021 set_buffer_internal (old_b);
10023 return Fcons (make_number (x), make_number (y));
10026 /***********************************************************************
10027 Messages
10028 ***********************************************************************/
10030 /* Return the number of arguments the format string FORMAT needs. */
10032 static ptrdiff_t
10033 format_nargs (char const *format)
10035 ptrdiff_t nargs = 0;
10036 for (char const *p = format; (p = strchr (p, '%')); p++)
10037 if (p[1] == '%')
10038 p++;
10039 else
10040 nargs++;
10041 return nargs;
10044 /* Add a message with format string FORMAT and formatted arguments
10045 to *Messages*. */
10047 void
10048 add_to_log (const char *format, ...)
10050 va_list ap;
10051 va_start (ap, format);
10052 vadd_to_log (format, ap);
10053 va_end (ap);
10056 void
10057 vadd_to_log (char const *format, va_list ap)
10059 ptrdiff_t form_nargs = format_nargs (format);
10060 ptrdiff_t nargs = 1 + form_nargs;
10061 Lisp_Object args[10];
10062 eassert (nargs <= ARRAYELTS (args));
10063 AUTO_STRING (args0, format);
10064 args[0] = args0;
10065 for (ptrdiff_t i = 1; i <= nargs; i++)
10066 args[i] = va_arg (ap, Lisp_Object);
10067 Lisp_Object msg = Qnil;
10068 msg = Fformat_message (nargs, args);
10070 ptrdiff_t len = SBYTES (msg) + 1;
10071 USE_SAFE_ALLOCA;
10072 char *buffer = SAFE_ALLOCA (len);
10073 memcpy (buffer, SDATA (msg), len);
10075 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10076 SAFE_FREE ();
10080 /* Output a newline in the *Messages* buffer if "needs" one. */
10082 void
10083 message_log_maybe_newline (void)
10085 if (message_log_need_newline)
10086 message_dolog ("", 0, true, false);
10090 /* Add a string M of length NBYTES to the message log, optionally
10091 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10092 true, means interpret the contents of M as multibyte. This
10093 function calls low-level routines in order to bypass text property
10094 hooks, etc. which might not be safe to run.
10096 This may GC (insert may run before/after change hooks),
10097 so the buffer M must NOT point to a Lisp string. */
10099 void
10100 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10102 const unsigned char *msg = (const unsigned char *) m;
10104 if (!NILP (Vmemory_full))
10105 return;
10107 if (!NILP (Vmessage_log_max))
10109 struct buffer *oldbuf;
10110 Lisp_Object oldpoint, oldbegv, oldzv;
10111 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10112 ptrdiff_t point_at_end = 0;
10113 ptrdiff_t zv_at_end = 0;
10114 Lisp_Object old_deactivate_mark;
10116 old_deactivate_mark = Vdeactivate_mark;
10117 oldbuf = current_buffer;
10119 /* Ensure the Messages buffer exists, and switch to it.
10120 If we created it, set the major-mode. */
10121 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10122 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10123 if (newbuffer
10124 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10125 call0 (intern ("messages-buffer-mode"));
10127 bset_undo_list (current_buffer, Qt);
10128 bset_cache_long_scans (current_buffer, Qnil);
10130 oldpoint = message_dolog_marker1;
10131 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10132 oldbegv = message_dolog_marker2;
10133 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10134 oldzv = message_dolog_marker3;
10135 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10137 if (PT == Z)
10138 point_at_end = 1;
10139 if (ZV == Z)
10140 zv_at_end = 1;
10142 BEGV = BEG;
10143 BEGV_BYTE = BEG_BYTE;
10144 ZV = Z;
10145 ZV_BYTE = Z_BYTE;
10146 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10148 /* Insert the string--maybe converting multibyte to single byte
10149 or vice versa, so that all the text fits the buffer. */
10150 if (multibyte
10151 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10153 ptrdiff_t i;
10154 int c, char_bytes;
10155 char work[1];
10157 /* Convert a multibyte string to single-byte
10158 for the *Message* buffer. */
10159 for (i = 0; i < nbytes; i += char_bytes)
10161 c = string_char_and_length (msg + i, &char_bytes);
10162 work[0] = CHAR_TO_BYTE8 (c);
10163 insert_1_both (work, 1, 1, true, false, false);
10166 else if (! multibyte
10167 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10169 ptrdiff_t i;
10170 int c, char_bytes;
10171 unsigned char str[MAX_MULTIBYTE_LENGTH];
10172 /* Convert a single-byte string to multibyte
10173 for the *Message* buffer. */
10174 for (i = 0; i < nbytes; i++)
10176 c = msg[i];
10177 MAKE_CHAR_MULTIBYTE (c);
10178 char_bytes = CHAR_STRING (c, str);
10179 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10182 else if (nbytes)
10183 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10184 true, false, false);
10186 if (nlflag)
10188 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10189 printmax_t dups;
10191 insert_1_both ("\n", 1, 1, true, false, false);
10193 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10194 this_bol = PT;
10195 this_bol_byte = PT_BYTE;
10197 /* See if this line duplicates the previous one.
10198 If so, combine duplicates. */
10199 if (this_bol > BEG)
10201 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10202 prev_bol = PT;
10203 prev_bol_byte = PT_BYTE;
10205 dups = message_log_check_duplicate (prev_bol_byte,
10206 this_bol_byte);
10207 if (dups)
10209 del_range_both (prev_bol, prev_bol_byte,
10210 this_bol, this_bol_byte, false);
10211 if (dups > 1)
10213 char dupstr[sizeof " [ times]"
10214 + INT_STRLEN_BOUND (printmax_t)];
10216 /* If you change this format, don't forget to also
10217 change message_log_check_duplicate. */
10218 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10220 insert_1_both (dupstr, duplen, duplen,
10221 true, false, true);
10226 /* If we have more than the desired maximum number of lines
10227 in the *Messages* buffer now, delete the oldest ones.
10228 This is safe because we don't have undo in this buffer. */
10230 if (NATNUMP (Vmessage_log_max))
10232 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10233 -XFASTINT (Vmessage_log_max) - 1, false);
10234 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10237 BEGV = marker_position (oldbegv);
10238 BEGV_BYTE = marker_byte_position (oldbegv);
10240 if (zv_at_end)
10242 ZV = Z;
10243 ZV_BYTE = Z_BYTE;
10245 else
10247 ZV = marker_position (oldzv);
10248 ZV_BYTE = marker_byte_position (oldzv);
10251 if (point_at_end)
10252 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10253 else
10254 /* We can't do Fgoto_char (oldpoint) because it will run some
10255 Lisp code. */
10256 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10257 marker_byte_position (oldpoint));
10259 unchain_marker (XMARKER (oldpoint));
10260 unchain_marker (XMARKER (oldbegv));
10261 unchain_marker (XMARKER (oldzv));
10263 /* We called insert_1_both above with its 5th argument (PREPARE)
10264 false, which prevents insert_1_both from calling
10265 prepare_to_modify_buffer, which in turns prevents us from
10266 incrementing windows_or_buffers_changed even if *Messages* is
10267 shown in some window. So we must manually set
10268 windows_or_buffers_changed here to make up for that. */
10269 windows_or_buffers_changed = old_windows_or_buffers_changed;
10270 bset_redisplay (current_buffer);
10272 set_buffer_internal (oldbuf);
10274 message_log_need_newline = !nlflag;
10275 Vdeactivate_mark = old_deactivate_mark;
10280 /* We are at the end of the buffer after just having inserted a newline.
10281 (Note: We depend on the fact we won't be crossing the gap.)
10282 Check to see if the most recent message looks a lot like the previous one.
10283 Return 0 if different, 1 if the new one should just replace it, or a
10284 value N > 1 if we should also append " [N times]". */
10286 static intmax_t
10287 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10289 ptrdiff_t i;
10290 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10291 bool seen_dots = false;
10292 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10293 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10295 for (i = 0; i < len; i++)
10297 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10298 seen_dots = true;
10299 if (p1[i] != p2[i])
10300 return seen_dots;
10302 p1 += len;
10303 if (*p1 == '\n')
10304 return 2;
10305 if (*p1++ == ' ' && *p1++ == '[')
10307 char *pend;
10308 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10309 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10310 return n + 1;
10312 return 0;
10316 /* Display an echo area message M with a specified length of NBYTES
10317 bytes. The string may include null characters. If M is not a
10318 string, clear out any existing message, and let the mini-buffer
10319 text show through.
10321 This function cancels echoing. */
10323 void
10324 message3 (Lisp_Object m)
10326 clear_message (true, true);
10327 cancel_echoing ();
10329 /* First flush out any partial line written with print. */
10330 message_log_maybe_newline ();
10331 if (STRINGP (m))
10333 ptrdiff_t nbytes = SBYTES (m);
10334 bool multibyte = STRING_MULTIBYTE (m);
10335 char *buffer;
10336 USE_SAFE_ALLOCA;
10337 SAFE_ALLOCA_STRING (buffer, m);
10338 message_dolog (buffer, nbytes, true, multibyte);
10339 SAFE_FREE ();
10341 if (! inhibit_message)
10342 message3_nolog (m);
10345 /* Log the message M to stderr. Log an empty line if M is not a string. */
10347 static void
10348 message_to_stderr (Lisp_Object m)
10350 if (noninteractive_need_newline)
10352 noninteractive_need_newline = false;
10353 fputc ('\n', stderr);
10355 if (STRINGP (m))
10357 Lisp_Object coding_system = Vlocale_coding_system;
10358 Lisp_Object s;
10360 if (!NILP (Vcoding_system_for_write))
10361 coding_system = Vcoding_system_for_write;
10362 if (!NILP (coding_system))
10363 s = code_convert_string_norecord (m, coding_system, true);
10364 else
10365 s = m;
10367 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10369 if (!cursor_in_echo_area)
10370 fputc ('\n', stderr);
10371 fflush (stderr);
10374 /* The non-logging version of message3.
10375 This does not cancel echoing, because it is used for echoing.
10376 Perhaps we need to make a separate function for echoing
10377 and make this cancel echoing. */
10379 void
10380 message3_nolog (Lisp_Object m)
10382 struct frame *sf = SELECTED_FRAME ();
10384 if (FRAME_INITIAL_P (sf))
10385 message_to_stderr (m);
10386 /* Error messages get reported properly by cmd_error, so this must be just an
10387 informative message; if the frame hasn't really been initialized yet, just
10388 toss it. */
10389 else if (INTERACTIVE && sf->glyphs_initialized_p)
10391 /* Get the frame containing the mini-buffer
10392 that the selected frame is using. */
10393 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10394 Lisp_Object frame = XWINDOW (mini_window)->frame;
10395 struct frame *f = XFRAME (frame);
10397 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10398 Fmake_frame_visible (frame);
10400 if (STRINGP (m) && SCHARS (m) > 0)
10402 set_message (m);
10403 if (minibuffer_auto_raise)
10404 Fraise_frame (frame);
10405 /* Assume we are not echoing.
10406 (If we are, echo_now will override this.) */
10407 echo_message_buffer = Qnil;
10409 else
10410 clear_message (true, true);
10412 do_pending_window_change (false);
10413 echo_area_display (true);
10414 do_pending_window_change (false);
10415 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10416 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10421 /* Display a null-terminated echo area message M. If M is 0, clear
10422 out any existing message, and let the mini-buffer text show through.
10424 The buffer M must continue to exist until after the echo area gets
10425 cleared or some other message gets displayed there. Do not pass
10426 text that is stored in a Lisp string. Do not pass text in a buffer
10427 that was alloca'd. */
10429 void
10430 message1 (const char *m)
10432 message3 (m ? build_unibyte_string (m) : Qnil);
10436 /* The non-logging counterpart of message1. */
10438 void
10439 message1_nolog (const char *m)
10441 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10444 /* Display a message M which contains a single %s
10445 which gets replaced with STRING. */
10447 void
10448 message_with_string (const char *m, Lisp_Object string, bool log)
10450 CHECK_STRING (string);
10452 bool need_message;
10453 if (noninteractive)
10454 need_message = !!m;
10455 else if (!INTERACTIVE)
10456 need_message = false;
10457 else
10459 /* The frame whose minibuffer we're going to display the message on.
10460 It may be larger than the selected frame, so we need
10461 to use its buffer, not the selected frame's buffer. */
10462 Lisp_Object mini_window;
10463 struct frame *f, *sf = SELECTED_FRAME ();
10465 /* Get the frame containing the minibuffer
10466 that the selected frame is using. */
10467 mini_window = FRAME_MINIBUF_WINDOW (sf);
10468 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10470 /* Error messages get reported properly by cmd_error, so this must be
10471 just an informative message; if the frame hasn't really been
10472 initialized yet, just toss it. */
10473 need_message = f->glyphs_initialized_p;
10476 if (need_message)
10478 AUTO_STRING (fmt, m);
10479 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10481 if (noninteractive)
10482 message_to_stderr (msg);
10483 else
10485 if (log)
10486 message3 (msg);
10487 else
10488 message3_nolog (msg);
10490 /* Print should start at the beginning of the message
10491 buffer next time. */
10492 message_buf_print = false;
10498 /* Dump an informative message to the minibuf. If M is 0, clear out
10499 any existing message, and let the mini-buffer text show through.
10501 The message must be safe ASCII and the format must not contain ` or
10502 '. If your message and format do not fit into this category,
10503 convert your arguments to Lisp objects and use Fmessage instead. */
10505 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10506 vmessage (const char *m, va_list ap)
10508 if (noninteractive)
10510 if (m)
10512 if (noninteractive_need_newline)
10513 putc ('\n', stderr);
10514 noninteractive_need_newline = false;
10515 vfprintf (stderr, m, ap);
10516 if (!cursor_in_echo_area)
10517 fprintf (stderr, "\n");
10518 fflush (stderr);
10521 else if (INTERACTIVE)
10523 /* The frame whose mini-buffer we're going to display the message
10524 on. It may be larger than the selected frame, so we need to
10525 use its buffer, not the selected frame's buffer. */
10526 Lisp_Object mini_window;
10527 struct frame *f, *sf = SELECTED_FRAME ();
10529 /* Get the frame containing the mini-buffer
10530 that the selected frame is using. */
10531 mini_window = FRAME_MINIBUF_WINDOW (sf);
10532 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10534 /* Error messages get reported properly by cmd_error, so this must be
10535 just an informative message; if the frame hasn't really been
10536 initialized yet, just toss it. */
10537 if (f->glyphs_initialized_p)
10539 if (m)
10541 ptrdiff_t len;
10542 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10543 USE_SAFE_ALLOCA;
10544 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10546 len = doprnt (message_buf, maxsize, m, 0, ap);
10548 message3 (make_string (message_buf, len));
10549 SAFE_FREE ();
10551 else
10552 message1 (0);
10554 /* Print should start at the beginning of the message
10555 buffer next time. */
10556 message_buf_print = false;
10561 void
10562 message (const char *m, ...)
10564 va_list ap;
10565 va_start (ap, m);
10566 vmessage (m, ap);
10567 va_end (ap);
10571 /* Display the current message in the current mini-buffer. This is
10572 only called from error handlers in process.c, and is not time
10573 critical. */
10575 void
10576 update_echo_area (void)
10578 if (!NILP (echo_area_buffer[0]))
10580 Lisp_Object string;
10581 string = Fcurrent_message ();
10582 message3 (string);
10587 /* Make sure echo area buffers in `echo_buffers' are live.
10588 If they aren't, make new ones. */
10590 static void
10591 ensure_echo_area_buffers (void)
10593 for (int i = 0; i < 2; i++)
10594 if (!BUFFERP (echo_buffer[i])
10595 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10597 Lisp_Object old_buffer = echo_buffer[i];
10598 static char const name_fmt[] = " *Echo Area %d*";
10599 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10600 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10601 echo_buffer[i] = Fget_buffer_create (lname);
10602 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10603 /* to force word wrap in echo area -
10604 it was decided to postpone this*/
10605 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10607 for (int j = 0; j < 2; j++)
10608 if (EQ (old_buffer, echo_area_buffer[j]))
10609 echo_area_buffer[j] = echo_buffer[i];
10614 /* Call FN with args A1..A2 with either the current or last displayed
10615 echo_area_buffer as current buffer.
10617 WHICH zero means use the current message buffer
10618 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10619 from echo_buffer[] and clear it.
10621 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10622 suitable buffer from echo_buffer[] and clear it.
10624 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10625 that the current message becomes the last displayed one, choose a
10626 suitable buffer for echo_area_buffer[0], and clear it.
10628 Value is what FN returns. */
10630 static bool
10631 with_echo_area_buffer (struct window *w, int which,
10632 bool (*fn) (ptrdiff_t, Lisp_Object),
10633 ptrdiff_t a1, Lisp_Object a2)
10635 Lisp_Object buffer;
10636 bool this_one, the_other, clear_buffer_p, rc;
10637 ptrdiff_t count = SPECPDL_INDEX ();
10639 /* If buffers aren't live, make new ones. */
10640 ensure_echo_area_buffers ();
10642 clear_buffer_p = false;
10644 if (which == 0)
10645 this_one = false, the_other = true;
10646 else if (which > 0)
10647 this_one = true, the_other = false;
10648 else
10650 this_one = false, the_other = true;
10651 clear_buffer_p = true;
10653 /* We need a fresh one in case the current echo buffer equals
10654 the one containing the last displayed echo area message. */
10655 if (!NILP (echo_area_buffer[this_one])
10656 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10657 echo_area_buffer[this_one] = Qnil;
10660 /* Choose a suitable buffer from echo_buffer[] if we don't
10661 have one. */
10662 if (NILP (echo_area_buffer[this_one]))
10664 echo_area_buffer[this_one]
10665 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10666 ? echo_buffer[the_other]
10667 : echo_buffer[this_one]);
10668 clear_buffer_p = true;
10671 buffer = echo_area_buffer[this_one];
10673 /* Don't get confused by reusing the buffer used for echoing
10674 for a different purpose. */
10675 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10676 cancel_echoing ();
10678 record_unwind_protect (unwind_with_echo_area_buffer,
10679 with_echo_area_buffer_unwind_data (w));
10681 /* Make the echo area buffer current. Note that for display
10682 purposes, it is not necessary that the displayed window's buffer
10683 == current_buffer, except for text property lookup. So, let's
10684 only set that buffer temporarily here without doing a full
10685 Fset_window_buffer. We must also change w->pointm, though,
10686 because otherwise an assertions in unshow_buffer fails, and Emacs
10687 aborts. */
10688 set_buffer_internal_1 (XBUFFER (buffer));
10689 if (w)
10691 wset_buffer (w, buffer);
10692 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10693 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10696 bset_undo_list (current_buffer, Qt);
10697 bset_read_only (current_buffer, Qnil);
10698 specbind (Qinhibit_read_only, Qt);
10699 specbind (Qinhibit_modification_hooks, Qt);
10701 if (clear_buffer_p && Z > BEG)
10702 del_range (BEG, Z);
10704 eassert (BEGV >= BEG);
10705 eassert (ZV <= Z && ZV >= BEGV);
10707 rc = fn (a1, a2);
10709 eassert (BEGV >= BEG);
10710 eassert (ZV <= Z && ZV >= BEGV);
10712 unbind_to (count, Qnil);
10713 return rc;
10717 /* Save state that should be preserved around the call to the function
10718 FN called in with_echo_area_buffer. */
10720 static Lisp_Object
10721 with_echo_area_buffer_unwind_data (struct window *w)
10723 int i = 0;
10724 Lisp_Object vector, tmp;
10726 /* Reduce consing by keeping one vector in
10727 Vwith_echo_area_save_vector. */
10728 vector = Vwith_echo_area_save_vector;
10729 Vwith_echo_area_save_vector = Qnil;
10731 if (NILP (vector))
10732 vector = Fmake_vector (make_number (11), Qnil);
10734 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10735 ASET (vector, i, Vdeactivate_mark); ++i;
10736 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10738 if (w)
10740 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10741 ASET (vector, i, w->contents); ++i;
10742 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10743 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10744 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10745 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10746 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10747 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10749 else
10751 int end = i + 8;
10752 for (; i < end; ++i)
10753 ASET (vector, i, Qnil);
10756 eassert (i == ASIZE (vector));
10757 return vector;
10761 /* Restore global state from VECTOR which was created by
10762 with_echo_area_buffer_unwind_data. */
10764 static void
10765 unwind_with_echo_area_buffer (Lisp_Object vector)
10767 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10768 Vdeactivate_mark = AREF (vector, 1);
10769 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10771 if (WINDOWP (AREF (vector, 3)))
10773 struct window *w;
10774 Lisp_Object buffer;
10776 w = XWINDOW (AREF (vector, 3));
10777 buffer = AREF (vector, 4);
10779 wset_buffer (w, buffer);
10780 set_marker_both (w->pointm, buffer,
10781 XFASTINT (AREF (vector, 5)),
10782 XFASTINT (AREF (vector, 6)));
10783 set_marker_both (w->old_pointm, buffer,
10784 XFASTINT (AREF (vector, 7)),
10785 XFASTINT (AREF (vector, 8)));
10786 set_marker_both (w->start, buffer,
10787 XFASTINT (AREF (vector, 9)),
10788 XFASTINT (AREF (vector, 10)));
10791 Vwith_echo_area_save_vector = vector;
10795 /* Set up the echo area for use by print functions. MULTIBYTE_P
10796 means we will print multibyte. */
10798 void
10799 setup_echo_area_for_printing (bool multibyte_p)
10801 /* If we can't find an echo area any more, exit. */
10802 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10803 Fkill_emacs (Qnil);
10805 ensure_echo_area_buffers ();
10807 if (!message_buf_print)
10809 /* A message has been output since the last time we printed.
10810 Choose a fresh echo area buffer. */
10811 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10812 echo_area_buffer[0] = echo_buffer[1];
10813 else
10814 echo_area_buffer[0] = echo_buffer[0];
10816 /* Switch to that buffer and clear it. */
10817 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10818 bset_truncate_lines (current_buffer, Qnil);
10820 if (Z > BEG)
10822 ptrdiff_t count = SPECPDL_INDEX ();
10823 specbind (Qinhibit_read_only, Qt);
10824 /* Note that undo recording is always disabled. */
10825 del_range (BEG, Z);
10826 unbind_to (count, Qnil);
10828 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10830 /* Set up the buffer for the multibyteness we need. */
10831 if (multibyte_p
10832 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10833 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10835 /* Raise the frame containing the echo area. */
10836 if (minibuffer_auto_raise)
10838 struct frame *sf = SELECTED_FRAME ();
10839 Lisp_Object mini_window;
10840 mini_window = FRAME_MINIBUF_WINDOW (sf);
10841 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10844 message_log_maybe_newline ();
10845 message_buf_print = true;
10847 else
10849 if (NILP (echo_area_buffer[0]))
10851 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10852 echo_area_buffer[0] = echo_buffer[1];
10853 else
10854 echo_area_buffer[0] = echo_buffer[0];
10857 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10859 /* Someone switched buffers between print requests. */
10860 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10861 bset_truncate_lines (current_buffer, Qnil);
10867 /* Display an echo area message in window W. Value is true if W's
10868 height is changed. If display_last_displayed_message_p,
10869 display the message that was last displayed, otherwise
10870 display the current message. */
10872 static bool
10873 display_echo_area (struct window *w)
10875 bool no_message_p, window_height_changed_p;
10877 /* Temporarily disable garbage collections while displaying the echo
10878 area. This is done because a GC can print a message itself.
10879 That message would modify the echo area buffer's contents while a
10880 redisplay of the buffer is going on, and seriously confuse
10881 redisplay. */
10882 ptrdiff_t count = inhibit_garbage_collection ();
10884 /* If there is no message, we must call display_echo_area_1
10885 nevertheless because it resizes the window. But we will have to
10886 reset the echo_area_buffer in question to nil at the end because
10887 with_echo_area_buffer will sets it to an empty buffer. */
10888 bool i = display_last_displayed_message_p;
10889 /* According to the C99, C11 and C++11 standards, the integral value
10890 of a "bool" is always 0 or 1, so this array access is safe here,
10891 if oddly typed. */
10892 no_message_p = NILP (echo_area_buffer[i]);
10894 window_height_changed_p
10895 = with_echo_area_buffer (w, display_last_displayed_message_p,
10896 display_echo_area_1,
10897 (intptr_t) w, Qnil);
10899 if (no_message_p)
10900 echo_area_buffer[i] = Qnil;
10902 unbind_to (count, Qnil);
10903 return window_height_changed_p;
10907 /* Helper for display_echo_area. Display the current buffer which
10908 contains the current echo area message in window W, a mini-window,
10909 a pointer to which is passed in A1. A2..A4 are currently not used.
10910 Change the height of W so that all of the message is displayed.
10911 Value is true if height of W was changed. */
10913 static bool
10914 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10916 intptr_t i1 = a1;
10917 struct window *w = (struct window *) i1;
10918 Lisp_Object window;
10919 struct text_pos start;
10921 /* We are about to enter redisplay without going through
10922 redisplay_internal, so we need to forget these faces by hand
10923 here. */
10924 forget_escape_and_glyphless_faces ();
10926 /* Do this before displaying, so that we have a large enough glyph
10927 matrix for the display. If we can't get enough space for the
10928 whole text, display the last N lines. That works by setting w->start. */
10929 bool window_height_changed_p = resize_mini_window (w, false);
10931 /* Use the starting position chosen by resize_mini_window. */
10932 SET_TEXT_POS_FROM_MARKER (start, w->start);
10934 /* Display. */
10935 clear_glyph_matrix (w->desired_matrix);
10936 XSETWINDOW (window, w);
10937 try_window (window, start, 0);
10939 return window_height_changed_p;
10943 /* Resize the echo area window to exactly the size needed for the
10944 currently displayed message, if there is one. If a mini-buffer
10945 is active, don't shrink it. */
10947 void
10948 resize_echo_area_exactly (void)
10950 if (BUFFERP (echo_area_buffer[0])
10951 && WINDOWP (echo_area_window))
10953 struct window *w = XWINDOW (echo_area_window);
10954 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10955 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10956 (intptr_t) w, resize_exactly);
10957 if (resized_p)
10959 windows_or_buffers_changed = 42;
10960 update_mode_lines = 30;
10961 redisplay_internal ();
10967 /* Callback function for with_echo_area_buffer, when used from
10968 resize_echo_area_exactly. A1 contains a pointer to the window to
10969 resize, EXACTLY non-nil means resize the mini-window exactly to the
10970 size of the text displayed. A3 and A4 are not used. Value is what
10971 resize_mini_window returns. */
10973 static bool
10974 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10976 intptr_t i1 = a1;
10977 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10981 /* Resize mini-window W to fit the size of its contents. EXACT_P
10982 means size the window exactly to the size needed. Otherwise, it's
10983 only enlarged until W's buffer is empty.
10985 Set W->start to the right place to begin display. If the whole
10986 contents fit, start at the beginning. Otherwise, start so as
10987 to make the end of the contents appear. This is particularly
10988 important for y-or-n-p, but seems desirable generally.
10990 Value is true if the window height has been changed. */
10992 bool
10993 resize_mini_window (struct window *w, bool exact_p)
10995 struct frame *f = XFRAME (w->frame);
10996 bool window_height_changed_p = false;
10998 eassert (MINI_WINDOW_P (w));
11000 /* By default, start display at the beginning. */
11001 set_marker_both (w->start, w->contents,
11002 BUF_BEGV (XBUFFER (w->contents)),
11003 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11005 /* Don't resize windows while redisplaying a window; it would
11006 confuse redisplay functions when the size of the window they are
11007 displaying changes from under them. Such a resizing can happen,
11008 for instance, when which-func prints a long message while
11009 we are running fontification-functions. We're running these
11010 functions with safe_call which binds inhibit-redisplay to t. */
11011 if (!NILP (Vinhibit_redisplay))
11012 return false;
11014 /* Nil means don't try to resize. */
11015 if (NILP (Vresize_mini_windows)
11016 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11017 return false;
11019 if (!FRAME_MINIBUF_ONLY_P (f))
11021 struct it it;
11022 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11023 + WINDOW_PIXEL_HEIGHT (w));
11024 int unit = FRAME_LINE_HEIGHT (f);
11025 int height, max_height;
11026 struct text_pos start;
11027 struct buffer *old_current_buffer = NULL;
11029 if (current_buffer != XBUFFER (w->contents))
11031 old_current_buffer = current_buffer;
11032 set_buffer_internal (XBUFFER (w->contents));
11035 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11037 /* Compute the max. number of lines specified by the user. */
11038 if (FLOATP (Vmax_mini_window_height))
11039 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11040 else if (INTEGERP (Vmax_mini_window_height))
11041 max_height = XINT (Vmax_mini_window_height) * unit;
11042 else
11043 max_height = total_height / 4;
11045 /* Correct that max. height if it's bogus. */
11046 max_height = clip_to_bounds (unit, max_height, total_height);
11048 /* Find out the height of the text in the window. */
11049 if (it.line_wrap == TRUNCATE)
11050 height = unit;
11051 else
11053 last_height = 0;
11054 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11055 if (it.max_ascent == 0 && it.max_descent == 0)
11056 height = it.current_y + last_height;
11057 else
11058 height = it.current_y + it.max_ascent + it.max_descent;
11059 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11062 /* Compute a suitable window start. */
11063 if (height > max_height)
11065 height = (max_height / unit) * unit;
11066 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11067 move_it_vertically_backward (&it, height - unit);
11068 start = it.current.pos;
11070 else
11071 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11072 SET_MARKER_FROM_TEXT_POS (w->start, start);
11074 if (EQ (Vresize_mini_windows, Qgrow_only))
11076 /* Let it grow only, until we display an empty message, in which
11077 case the window shrinks again. */
11078 if (height > WINDOW_PIXEL_HEIGHT (w))
11080 int old_height = WINDOW_PIXEL_HEIGHT (w);
11082 FRAME_WINDOWS_FROZEN (f) = true;
11083 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11084 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11086 else if (height < WINDOW_PIXEL_HEIGHT (w)
11087 && (exact_p || BEGV == ZV))
11089 int old_height = WINDOW_PIXEL_HEIGHT (w);
11091 FRAME_WINDOWS_FROZEN (f) = false;
11092 shrink_mini_window (w, true);
11093 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11096 else
11098 /* Always resize to exact size needed. */
11099 if (height > WINDOW_PIXEL_HEIGHT (w))
11101 int old_height = WINDOW_PIXEL_HEIGHT (w);
11103 FRAME_WINDOWS_FROZEN (f) = true;
11104 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11105 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11107 else if (height < WINDOW_PIXEL_HEIGHT (w))
11109 int old_height = WINDOW_PIXEL_HEIGHT (w);
11111 FRAME_WINDOWS_FROZEN (f) = false;
11112 shrink_mini_window (w, true);
11114 if (height)
11116 FRAME_WINDOWS_FROZEN (f) = true;
11117 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11120 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11124 if (old_current_buffer)
11125 set_buffer_internal (old_current_buffer);
11128 return window_height_changed_p;
11132 /* Value is the current message, a string, or nil if there is no
11133 current message. */
11135 Lisp_Object
11136 current_message (void)
11138 Lisp_Object msg;
11140 if (!BUFFERP (echo_area_buffer[0]))
11141 msg = Qnil;
11142 else
11144 with_echo_area_buffer (0, 0, current_message_1,
11145 (intptr_t) &msg, Qnil);
11146 if (NILP (msg))
11147 echo_area_buffer[0] = Qnil;
11150 return msg;
11154 static bool
11155 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11157 intptr_t i1 = a1;
11158 Lisp_Object *msg = (Lisp_Object *) i1;
11160 if (Z > BEG)
11161 *msg = make_buffer_string (BEG, Z, true);
11162 else
11163 *msg = Qnil;
11164 return false;
11168 /* Push the current message on Vmessage_stack for later restoration
11169 by restore_message. Value is true if the current message isn't
11170 empty. This is a relatively infrequent operation, so it's not
11171 worth optimizing. */
11173 bool
11174 push_message (void)
11176 Lisp_Object msg = current_message ();
11177 Vmessage_stack = Fcons (msg, Vmessage_stack);
11178 return STRINGP (msg);
11182 /* Restore message display from the top of Vmessage_stack. */
11184 void
11185 restore_message (void)
11187 eassert (CONSP (Vmessage_stack));
11188 message3_nolog (XCAR (Vmessage_stack));
11192 /* Handler for unwind-protect calling pop_message. */
11194 void
11195 pop_message_unwind (void)
11197 /* Pop the top-most entry off Vmessage_stack. */
11198 eassert (CONSP (Vmessage_stack));
11199 Vmessage_stack = XCDR (Vmessage_stack);
11203 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11204 exits. If the stack is not empty, we have a missing pop_message
11205 somewhere. */
11207 void
11208 check_message_stack (void)
11210 if (!NILP (Vmessage_stack))
11211 emacs_abort ();
11215 /* Truncate to NCHARS what will be displayed in the echo area the next
11216 time we display it---but don't redisplay it now. */
11218 void
11219 truncate_echo_area (ptrdiff_t nchars)
11221 if (nchars == 0)
11222 echo_area_buffer[0] = Qnil;
11223 else if (!noninteractive
11224 && INTERACTIVE
11225 && !NILP (echo_area_buffer[0]))
11227 struct frame *sf = SELECTED_FRAME ();
11228 /* Error messages get reported properly by cmd_error, so this must be
11229 just an informative message; if the frame hasn't really been
11230 initialized yet, just toss it. */
11231 if (sf->glyphs_initialized_p)
11232 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11237 /* Helper function for truncate_echo_area. Truncate the current
11238 message to at most NCHARS characters. */
11240 static bool
11241 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11243 if (BEG + nchars < Z)
11244 del_range (BEG + nchars, Z);
11245 if (Z == BEG)
11246 echo_area_buffer[0] = Qnil;
11247 return false;
11250 /* Set the current message to STRING. */
11252 static void
11253 set_message (Lisp_Object string)
11255 eassert (STRINGP (string));
11257 message_enable_multibyte = STRING_MULTIBYTE (string);
11259 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11260 message_buf_print = false;
11261 help_echo_showing_p = false;
11263 if (STRINGP (Vdebug_on_message)
11264 && STRINGP (string)
11265 && fast_string_match (Vdebug_on_message, string) >= 0)
11266 call_debugger (list2 (Qerror, string));
11270 /* Helper function for set_message. First argument is ignored and second
11271 argument has the same meaning as for set_message.
11272 This function is called with the echo area buffer being current. */
11274 static bool
11275 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11277 eassert (STRINGP (string));
11279 /* Change multibyteness of the echo buffer appropriately. */
11280 if (message_enable_multibyte
11281 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11282 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11284 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11285 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11286 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11288 /* Insert new message at BEG. */
11289 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11291 /* This function takes care of single/multibyte conversion.
11292 We just have to ensure that the echo area buffer has the right
11293 setting of enable_multibyte_characters. */
11294 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11296 return false;
11300 /* Clear messages. CURRENT_P means clear the current message.
11301 LAST_DISPLAYED_P means clear the message last displayed. */
11303 void
11304 clear_message (bool current_p, bool last_displayed_p)
11306 if (current_p)
11308 echo_area_buffer[0] = Qnil;
11309 message_cleared_p = true;
11312 if (last_displayed_p)
11313 echo_area_buffer[1] = Qnil;
11315 message_buf_print = false;
11318 /* Clear garbaged frames.
11320 This function is used where the old redisplay called
11321 redraw_garbaged_frames which in turn called redraw_frame which in
11322 turn called clear_frame. The call to clear_frame was a source of
11323 flickering. I believe a clear_frame is not necessary. It should
11324 suffice in the new redisplay to invalidate all current matrices,
11325 and ensure a complete redisplay of all windows. */
11327 static void
11328 clear_garbaged_frames (void)
11330 if (frame_garbaged)
11332 Lisp_Object tail, frame;
11333 struct frame *sf = SELECTED_FRAME ();
11335 FOR_EACH_FRAME (tail, frame)
11337 struct frame *f = XFRAME (frame);
11339 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11341 if (f->resized_p
11342 /* It makes no sense to redraw a non-selected TTY
11343 frame, since that will actually clear the
11344 selected frame, and might leave the selected
11345 frame with corrupted display, if it happens not
11346 to be marked garbaged. */
11347 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11348 redraw_frame (f);
11349 else
11350 clear_current_matrices (f);
11351 fset_redisplay (f);
11352 f->garbaged = false;
11353 f->resized_p = false;
11357 frame_garbaged = false;
11362 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11363 selected_frame. */
11365 static void
11366 echo_area_display (bool update_frame_p)
11368 Lisp_Object mini_window;
11369 struct window *w;
11370 struct frame *f;
11371 bool window_height_changed_p = false;
11372 struct frame *sf = SELECTED_FRAME ();
11374 mini_window = FRAME_MINIBUF_WINDOW (sf);
11375 w = XWINDOW (mini_window);
11376 f = XFRAME (WINDOW_FRAME (w));
11378 /* Don't display if frame is invisible or not yet initialized. */
11379 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11380 return;
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 /* When Emacs starts, selected_frame may be the initial terminal
11384 frame. If we let this through, a message would be displayed on
11385 the terminal. */
11386 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11387 return;
11388 #endif /* HAVE_WINDOW_SYSTEM */
11390 /* Redraw garbaged frames. */
11391 clear_garbaged_frames ();
11393 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11395 echo_area_window = mini_window;
11396 window_height_changed_p = display_echo_area (w);
11397 w->must_be_updated_p = true;
11399 /* Update the display, unless called from redisplay_internal.
11400 Also don't update the screen during redisplay itself. The
11401 update will happen at the end of redisplay, and an update
11402 here could cause confusion. */
11403 if (update_frame_p && !redisplaying_p)
11405 int n = 0;
11407 /* If the display update has been interrupted by pending
11408 input, update mode lines in the frame. Due to the
11409 pending input, it might have been that redisplay hasn't
11410 been called, so that mode lines above the echo area are
11411 garbaged. This looks odd, so we prevent it here. */
11412 if (!display_completed)
11413 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11415 if (window_height_changed_p
11416 /* Don't do this if Emacs is shutting down. Redisplay
11417 needs to run hooks. */
11418 && !NILP (Vrun_hooks))
11420 /* Must update other windows. Likewise as in other
11421 cases, don't let this update be interrupted by
11422 pending input. */
11423 ptrdiff_t count = SPECPDL_INDEX ();
11424 specbind (Qredisplay_dont_pause, Qt);
11425 fset_redisplay (f);
11426 redisplay_internal ();
11427 unbind_to (count, Qnil);
11429 else if (FRAME_WINDOW_P (f) && n == 0)
11431 /* Window configuration is the same as before.
11432 Can do with a display update of the echo area,
11433 unless we displayed some mode lines. */
11434 update_single_window (w);
11435 flush_frame (f);
11437 else
11438 update_frame (f, true, true);
11440 /* If cursor is in the echo area, make sure that the next
11441 redisplay displays the minibuffer, so that the cursor will
11442 be replaced with what the minibuffer wants. */
11443 if (cursor_in_echo_area)
11444 wset_redisplay (XWINDOW (mini_window));
11447 else if (!EQ (mini_window, selected_window))
11448 wset_redisplay (XWINDOW (mini_window));
11450 /* Last displayed message is now the current message. */
11451 echo_area_buffer[1] = echo_area_buffer[0];
11452 /* Inform read_char that we're not echoing. */
11453 echo_message_buffer = Qnil;
11455 /* Prevent redisplay optimization in redisplay_internal by resetting
11456 this_line_start_pos. This is done because the mini-buffer now
11457 displays the message instead of its buffer text. */
11458 if (EQ (mini_window, selected_window))
11459 CHARPOS (this_line_start_pos) = 0;
11461 if (window_height_changed_p)
11463 fset_redisplay (f);
11465 /* If window configuration was changed, frames may have been
11466 marked garbaged. Clear them or we will experience
11467 surprises wrt scrolling.
11468 FIXME: How/why/when? */
11469 clear_garbaged_frames ();
11473 /* True if W's buffer was changed but not saved. */
11475 static bool
11476 window_buffer_changed (struct window *w)
11478 struct buffer *b = XBUFFER (w->contents);
11480 eassert (BUFFER_LIVE_P (b));
11482 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11485 /* True if W has %c in its mode line and mode line should be updated. */
11487 static bool
11488 mode_line_update_needed (struct window *w)
11490 return (w->column_number_displayed != -1
11491 && !(PT == w->last_point && !window_outdated (w))
11492 && (w->column_number_displayed != current_column ()));
11495 /* True if window start of W is frozen and may not be changed during
11496 redisplay. */
11498 static bool
11499 window_frozen_p (struct window *w)
11501 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11503 Lisp_Object window;
11505 XSETWINDOW (window, w);
11506 if (MINI_WINDOW_P (w))
11507 return false;
11508 else if (EQ (window, selected_window))
11509 return false;
11510 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11511 && EQ (window, Vminibuf_scroll_window))
11512 /* This special window can't be frozen too. */
11513 return false;
11514 else
11515 return true;
11517 return false;
11520 /***********************************************************************
11521 Mode Lines and Frame Titles
11522 ***********************************************************************/
11524 /* A buffer for constructing non-propertized mode-line strings and
11525 frame titles in it; allocated from the heap in init_xdisp and
11526 resized as needed in store_mode_line_noprop_char. */
11528 static char *mode_line_noprop_buf;
11530 /* The buffer's end, and a current output position in it. */
11532 static char *mode_line_noprop_buf_end;
11533 static char *mode_line_noprop_ptr;
11535 #define MODE_LINE_NOPROP_LEN(start) \
11536 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11538 static enum {
11539 MODE_LINE_DISPLAY = 0,
11540 MODE_LINE_TITLE,
11541 MODE_LINE_NOPROP,
11542 MODE_LINE_STRING
11543 } mode_line_target;
11545 /* Alist that caches the results of :propertize.
11546 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11547 static Lisp_Object mode_line_proptrans_alist;
11549 /* List of strings making up the mode-line. */
11550 static Lisp_Object mode_line_string_list;
11552 /* Base face property when building propertized mode line string. */
11553 static Lisp_Object mode_line_string_face;
11554 static Lisp_Object mode_line_string_face_prop;
11557 /* Unwind data for mode line strings */
11559 static Lisp_Object Vmode_line_unwind_vector;
11561 static Lisp_Object
11562 format_mode_line_unwind_data (struct frame *target_frame,
11563 struct buffer *obuf,
11564 Lisp_Object owin,
11565 bool save_proptrans)
11567 Lisp_Object vector, tmp;
11569 /* Reduce consing by keeping one vector in
11570 Vwith_echo_area_save_vector. */
11571 vector = Vmode_line_unwind_vector;
11572 Vmode_line_unwind_vector = Qnil;
11574 if (NILP (vector))
11575 vector = Fmake_vector (make_number (10), Qnil);
11577 ASET (vector, 0, make_number (mode_line_target));
11578 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11579 ASET (vector, 2, mode_line_string_list);
11580 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11581 ASET (vector, 4, mode_line_string_face);
11582 ASET (vector, 5, mode_line_string_face_prop);
11584 if (obuf)
11585 XSETBUFFER (tmp, obuf);
11586 else
11587 tmp = Qnil;
11588 ASET (vector, 6, tmp);
11589 ASET (vector, 7, owin);
11590 if (target_frame)
11592 /* Similarly to `with-selected-window', if the operation selects
11593 a window on another frame, we must restore that frame's
11594 selected window, and (for a tty) the top-frame. */
11595 ASET (vector, 8, target_frame->selected_window);
11596 if (FRAME_TERMCAP_P (target_frame))
11597 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11600 return vector;
11603 static void
11604 unwind_format_mode_line (Lisp_Object vector)
11606 Lisp_Object old_window = AREF (vector, 7);
11607 Lisp_Object target_frame_window = AREF (vector, 8);
11608 Lisp_Object old_top_frame = AREF (vector, 9);
11610 mode_line_target = XINT (AREF (vector, 0));
11611 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11612 mode_line_string_list = AREF (vector, 2);
11613 if (! EQ (AREF (vector, 3), Qt))
11614 mode_line_proptrans_alist = AREF (vector, 3);
11615 mode_line_string_face = AREF (vector, 4);
11616 mode_line_string_face_prop = AREF (vector, 5);
11618 /* Select window before buffer, since it may change the buffer. */
11619 if (!NILP (old_window))
11621 /* If the operation that we are unwinding had selected a window
11622 on a different frame, reset its frame-selected-window. For a
11623 text terminal, reset its top-frame if necessary. */
11624 if (!NILP (target_frame_window))
11626 Lisp_Object frame
11627 = WINDOW_FRAME (XWINDOW (target_frame_window));
11629 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11630 Fselect_window (target_frame_window, Qt);
11632 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11633 Fselect_frame (old_top_frame, Qt);
11636 Fselect_window (old_window, Qt);
11639 if (!NILP (AREF (vector, 6)))
11641 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11642 ASET (vector, 6, Qnil);
11645 Vmode_line_unwind_vector = vector;
11649 /* Store a single character C for the frame title in mode_line_noprop_buf.
11650 Re-allocate mode_line_noprop_buf if necessary. */
11652 static void
11653 store_mode_line_noprop_char (char c)
11655 /* If output position has reached the end of the allocated buffer,
11656 increase the buffer's size. */
11657 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11659 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11660 ptrdiff_t size = len;
11661 mode_line_noprop_buf =
11662 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11663 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11664 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11667 *mode_line_noprop_ptr++ = c;
11671 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11672 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11673 characters that yield more columns than PRECISION; PRECISION <= 0
11674 means copy the whole string. Pad with spaces until FIELD_WIDTH
11675 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11676 pad. Called from display_mode_element when it is used to build a
11677 frame title. */
11679 static int
11680 store_mode_line_noprop (const char *string, int field_width, int precision)
11682 const unsigned char *str = (const unsigned char *) string;
11683 int n = 0;
11684 ptrdiff_t dummy, nbytes;
11686 /* Copy at most PRECISION chars from STR. */
11687 nbytes = strlen (string);
11688 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11689 while (nbytes--)
11690 store_mode_line_noprop_char (*str++);
11692 /* Fill up with spaces until FIELD_WIDTH reached. */
11693 while (field_width > 0
11694 && n < field_width)
11696 store_mode_line_noprop_char (' ');
11697 ++n;
11700 return n;
11703 /***********************************************************************
11704 Frame Titles
11705 ***********************************************************************/
11707 #ifdef HAVE_WINDOW_SYSTEM
11709 /* Set the title of FRAME, if it has changed. The title format is
11710 Vicon_title_format if FRAME is iconified, otherwise it is
11711 frame_title_format. */
11713 static void
11714 x_consider_frame_title (Lisp_Object frame)
11716 struct frame *f = XFRAME (frame);
11718 if ((FRAME_WINDOW_P (f)
11719 || FRAME_MINIBUF_ONLY_P (f)
11720 || f->explicit_name)
11721 && NILP (Fframe_parameter (frame, Qtooltip)))
11723 /* Do we have more than one visible frame on this X display? */
11724 Lisp_Object tail, other_frame, fmt;
11725 ptrdiff_t title_start;
11726 char *title;
11727 ptrdiff_t len;
11728 struct it it;
11729 ptrdiff_t count = SPECPDL_INDEX ();
11731 FOR_EACH_FRAME (tail, other_frame)
11733 struct frame *tf = XFRAME (other_frame);
11735 if (tf != f
11736 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11737 && !FRAME_MINIBUF_ONLY_P (tf)
11738 && !EQ (other_frame, tip_frame)
11739 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11740 break;
11743 /* Set global variable indicating that multiple frames exist. */
11744 multiple_frames = CONSP (tail);
11746 /* Switch to the buffer of selected window of the frame. Set up
11747 mode_line_target so that display_mode_element will output into
11748 mode_line_noprop_buf; then display the title. */
11749 record_unwind_protect (unwind_format_mode_line,
11750 format_mode_line_unwind_data
11751 (f, current_buffer, selected_window, false));
11752 /* select-frame calls resize_mini_window, which could resize the
11753 mini-window and by that undo the effect of this redisplay
11754 cycle wrt minibuffer and echo-area display. Binding
11755 inhibit-redisplay to t makes the call to resize_mini_window a
11756 no-op, thus avoiding the adverse side effects. */
11757 specbind (Qinhibit_redisplay, Qt);
11759 Fselect_window (f->selected_window, Qt);
11760 set_buffer_internal_1
11761 (XBUFFER (XWINDOW (f->selected_window)->contents));
11762 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11764 mode_line_target = MODE_LINE_TITLE;
11765 title_start = MODE_LINE_NOPROP_LEN (0);
11766 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11767 NULL, DEFAULT_FACE_ID);
11768 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11769 len = MODE_LINE_NOPROP_LEN (title_start);
11770 title = mode_line_noprop_buf + title_start;
11771 unbind_to (count, Qnil);
11773 /* Set the title only if it's changed. This avoids consing in
11774 the common case where it hasn't. (If it turns out that we've
11775 already wasted too much time by walking through the list with
11776 display_mode_element, then we might need to optimize at a
11777 higher level than this.) */
11778 if (! STRINGP (f->name)
11779 || SBYTES (f->name) != len
11780 || memcmp (title, SDATA (f->name), len) != 0)
11781 x_implicitly_set_name (f, make_string (title, len), Qnil);
11785 #endif /* not HAVE_WINDOW_SYSTEM */
11788 /***********************************************************************
11789 Menu Bars
11790 ***********************************************************************/
11792 /* True if we will not redisplay all visible windows. */
11793 #define REDISPLAY_SOME_P() \
11794 ((windows_or_buffers_changed == 0 \
11795 || windows_or_buffers_changed == REDISPLAY_SOME) \
11796 && (update_mode_lines == 0 \
11797 || update_mode_lines == REDISPLAY_SOME))
11799 /* Prepare for redisplay by updating menu-bar item lists when
11800 appropriate. This can call eval. */
11802 static void
11803 prepare_menu_bars (void)
11805 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11806 bool some_windows = REDISPLAY_SOME_P ();
11807 Lisp_Object tooltip_frame;
11809 #ifdef HAVE_WINDOW_SYSTEM
11810 tooltip_frame = tip_frame;
11811 #else
11812 tooltip_frame = Qnil;
11813 #endif
11815 if (FUNCTIONP (Vpre_redisplay_function))
11817 Lisp_Object windows = all_windows ? Qt : Qnil;
11818 if (all_windows && some_windows)
11820 Lisp_Object ws = window_list ();
11821 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11823 Lisp_Object this = XCAR (ws);
11824 struct window *w = XWINDOW (this);
11825 if (w->redisplay
11826 || XFRAME (w->frame)->redisplay
11827 || XBUFFER (w->contents)->text->redisplay)
11829 windows = Fcons (this, windows);
11833 safe__call1 (true, Vpre_redisplay_function, windows);
11836 /* Update all frame titles based on their buffer names, etc. We do
11837 this before the menu bars so that the buffer-menu will show the
11838 up-to-date frame titles. */
11839 #ifdef HAVE_WINDOW_SYSTEM
11840 if (all_windows)
11842 Lisp_Object tail, frame;
11844 FOR_EACH_FRAME (tail, frame)
11846 struct frame *f = XFRAME (frame);
11847 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11848 if (some_windows
11849 && !f->redisplay
11850 && !w->redisplay
11851 && !XBUFFER (w->contents)->text->redisplay)
11852 continue;
11854 if (!EQ (frame, tooltip_frame)
11855 && (FRAME_ICONIFIED_P (f)
11856 || FRAME_VISIBLE_P (f) == 1
11857 /* Exclude TTY frames that are obscured because they
11858 are not the top frame on their console. This is
11859 because x_consider_frame_title actually switches
11860 to the frame, which for TTY frames means it is
11861 marked as garbaged, and will be completely
11862 redrawn on the next redisplay cycle. This causes
11863 TTY frames to be completely redrawn, when there
11864 are more than one of them, even though nothing
11865 should be changed on display. */
11866 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11867 x_consider_frame_title (frame);
11870 #endif /* HAVE_WINDOW_SYSTEM */
11872 /* Update the menu bar item lists, if appropriate. This has to be
11873 done before any actual redisplay or generation of display lines. */
11875 if (all_windows)
11877 Lisp_Object tail, frame;
11878 ptrdiff_t count = SPECPDL_INDEX ();
11879 /* True means that update_menu_bar has run its hooks
11880 so any further calls to update_menu_bar shouldn't do so again. */
11881 bool menu_bar_hooks_run = false;
11883 record_unwind_save_match_data ();
11885 FOR_EACH_FRAME (tail, frame)
11887 struct frame *f = XFRAME (frame);
11888 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11890 /* Ignore tooltip frame. */
11891 if (EQ (frame, tooltip_frame))
11892 continue;
11894 if (some_windows
11895 && !f->redisplay
11896 && !w->redisplay
11897 && !XBUFFER (w->contents)->text->redisplay)
11898 continue;
11900 run_window_size_change_functions (frame);
11901 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11902 #ifdef HAVE_WINDOW_SYSTEM
11903 update_tool_bar (f, false);
11904 #endif
11907 unbind_to (count, Qnil);
11909 else
11911 struct frame *sf = SELECTED_FRAME ();
11912 update_menu_bar (sf, true, false);
11913 #ifdef HAVE_WINDOW_SYSTEM
11914 update_tool_bar (sf, true);
11915 #endif
11920 /* Update the menu bar item list for frame F. This has to be done
11921 before we start to fill in any display lines, because it can call
11922 eval.
11924 If SAVE_MATCH_DATA, we must save and restore it here.
11926 If HOOKS_RUN, a previous call to update_menu_bar
11927 already ran the menu bar hooks for this redisplay, so there
11928 is no need to run them again. The return value is the
11929 updated value of this flag, to pass to the next call. */
11931 static bool
11932 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11934 Lisp_Object window;
11935 struct window *w;
11937 /* If called recursively during a menu update, do nothing. This can
11938 happen when, for instance, an activate-menubar-hook causes a
11939 redisplay. */
11940 if (inhibit_menubar_update)
11941 return hooks_run;
11943 window = FRAME_SELECTED_WINDOW (f);
11944 w = XWINDOW (window);
11946 if (FRAME_WINDOW_P (f)
11948 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11949 || defined (HAVE_NS) || defined (USE_GTK)
11950 FRAME_EXTERNAL_MENU_BAR (f)
11951 #else
11952 FRAME_MENU_BAR_LINES (f) > 0
11953 #endif
11954 : FRAME_MENU_BAR_LINES (f) > 0)
11956 /* If the user has switched buffers or windows, we need to
11957 recompute to reflect the new bindings. But we'll
11958 recompute when update_mode_lines is set too; that means
11959 that people can use force-mode-line-update to request
11960 that the menu bar be recomputed. The adverse effect on
11961 the rest of the redisplay algorithm is about the same as
11962 windows_or_buffers_changed anyway. */
11963 if (windows_or_buffers_changed
11964 /* This used to test w->update_mode_line, but we believe
11965 there is no need to recompute the menu in that case. */
11966 || update_mode_lines
11967 || window_buffer_changed (w))
11969 struct buffer *prev = current_buffer;
11970 ptrdiff_t count = SPECPDL_INDEX ();
11972 specbind (Qinhibit_menubar_update, Qt);
11974 set_buffer_internal_1 (XBUFFER (w->contents));
11975 if (save_match_data)
11976 record_unwind_save_match_data ();
11977 if (NILP (Voverriding_local_map_menu_flag))
11979 specbind (Qoverriding_terminal_local_map, Qnil);
11980 specbind (Qoverriding_local_map, Qnil);
11983 if (!hooks_run)
11985 /* Run the Lucid hook. */
11986 safe_run_hooks (Qactivate_menubar_hook);
11988 /* If it has changed current-menubar from previous value,
11989 really recompute the menu-bar from the value. */
11990 if (! NILP (Vlucid_menu_bar_dirty_flag))
11991 call0 (Qrecompute_lucid_menubar);
11993 safe_run_hooks (Qmenu_bar_update_hook);
11995 hooks_run = true;
11998 XSETFRAME (Vmenu_updating_frame, f);
11999 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12001 /* Redisplay the menu bar in case we changed it. */
12002 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12003 || defined (HAVE_NS) || defined (USE_GTK)
12004 if (FRAME_WINDOW_P (f))
12006 #if defined (HAVE_NS)
12007 /* All frames on Mac OS share the same menubar. So only
12008 the selected frame should be allowed to set it. */
12009 if (f == SELECTED_FRAME ())
12010 #endif
12011 set_frame_menubar (f, false, false);
12013 else
12014 /* On a terminal screen, the menu bar is an ordinary screen
12015 line, and this makes it get updated. */
12016 w->update_mode_line = true;
12017 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12018 /* In the non-toolkit version, the menu bar is an ordinary screen
12019 line, and this makes it get updated. */
12020 w->update_mode_line = true;
12021 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12023 unbind_to (count, Qnil);
12024 set_buffer_internal_1 (prev);
12028 return hooks_run;
12031 /***********************************************************************
12032 Tool-bars
12033 ***********************************************************************/
12035 #ifdef HAVE_WINDOW_SYSTEM
12037 /* Select `frame' temporarily without running all the code in
12038 do_switch_frame.
12039 FIXME: Maybe do_switch_frame should be trimmed down similarly
12040 when `norecord' is set. */
12041 static void
12042 fast_set_selected_frame (Lisp_Object frame)
12044 if (!EQ (selected_frame, frame))
12046 selected_frame = frame;
12047 selected_window = XFRAME (frame)->selected_window;
12051 /* Update the tool-bar item list for frame F. This has to be done
12052 before we start to fill in any display lines. Called from
12053 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12054 and restore it here. */
12056 static void
12057 update_tool_bar (struct frame *f, bool save_match_data)
12059 #if defined (USE_GTK) || defined (HAVE_NS)
12060 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12061 #else
12062 bool do_update = (WINDOWP (f->tool_bar_window)
12063 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12064 #endif
12066 if (do_update)
12068 Lisp_Object window;
12069 struct window *w;
12071 window = FRAME_SELECTED_WINDOW (f);
12072 w = XWINDOW (window);
12074 /* If the user has switched buffers or windows, we need to
12075 recompute to reflect the new bindings. But we'll
12076 recompute when update_mode_lines is set too; that means
12077 that people can use force-mode-line-update to request
12078 that the menu bar be recomputed. The adverse effect on
12079 the rest of the redisplay algorithm is about the same as
12080 windows_or_buffers_changed anyway. */
12081 if (windows_or_buffers_changed
12082 || w->update_mode_line
12083 || update_mode_lines
12084 || window_buffer_changed (w))
12086 struct buffer *prev = current_buffer;
12087 ptrdiff_t count = SPECPDL_INDEX ();
12088 Lisp_Object frame, new_tool_bar;
12089 int new_n_tool_bar;
12091 /* Set current_buffer to the buffer of the selected
12092 window of the frame, so that we get the right local
12093 keymaps. */
12094 set_buffer_internal_1 (XBUFFER (w->contents));
12096 /* Save match data, if we must. */
12097 if (save_match_data)
12098 record_unwind_save_match_data ();
12100 /* Make sure that we don't accidentally use bogus keymaps. */
12101 if (NILP (Voverriding_local_map_menu_flag))
12103 specbind (Qoverriding_terminal_local_map, Qnil);
12104 specbind (Qoverriding_local_map, Qnil);
12107 /* We must temporarily set the selected frame to this frame
12108 before calling tool_bar_items, because the calculation of
12109 the tool-bar keymap uses the selected frame (see
12110 `tool-bar-make-keymap' in tool-bar.el). */
12111 eassert (EQ (selected_window,
12112 /* Since we only explicitly preserve selected_frame,
12113 check that selected_window would be redundant. */
12114 XFRAME (selected_frame)->selected_window));
12115 record_unwind_protect (fast_set_selected_frame, selected_frame);
12116 XSETFRAME (frame, f);
12117 fast_set_selected_frame (frame);
12119 /* Build desired tool-bar items from keymaps. */
12120 new_tool_bar
12121 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12122 &new_n_tool_bar);
12124 /* Redisplay the tool-bar if we changed it. */
12125 if (new_n_tool_bar != f->n_tool_bar_items
12126 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12128 /* Redisplay that happens asynchronously due to an expose event
12129 may access f->tool_bar_items. Make sure we update both
12130 variables within BLOCK_INPUT so no such event interrupts. */
12131 block_input ();
12132 fset_tool_bar_items (f, new_tool_bar);
12133 f->n_tool_bar_items = new_n_tool_bar;
12134 w->update_mode_line = true;
12135 unblock_input ();
12138 unbind_to (count, Qnil);
12139 set_buffer_internal_1 (prev);
12144 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12146 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12147 F's desired tool-bar contents. F->tool_bar_items must have
12148 been set up previously by calling prepare_menu_bars. */
12150 static void
12151 build_desired_tool_bar_string (struct frame *f)
12153 int i, size, size_needed;
12154 Lisp_Object image, plist;
12156 image = plist = Qnil;
12158 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12159 Otherwise, make a new string. */
12161 /* The size of the string we might be able to reuse. */
12162 size = (STRINGP (f->desired_tool_bar_string)
12163 ? SCHARS (f->desired_tool_bar_string)
12164 : 0);
12166 /* We need one space in the string for each image. */
12167 size_needed = f->n_tool_bar_items;
12169 /* Reuse f->desired_tool_bar_string, if possible. */
12170 if (size < size_needed || NILP (f->desired_tool_bar_string))
12171 fset_desired_tool_bar_string
12172 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12173 else
12175 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12176 Fremove_text_properties (make_number (0), make_number (size),
12177 props, f->desired_tool_bar_string);
12180 /* Put a `display' property on the string for the images to display,
12181 put a `menu_item' property on tool-bar items with a value that
12182 is the index of the item in F's tool-bar item vector. */
12183 for (i = 0; i < f->n_tool_bar_items; ++i)
12185 #define PROP(IDX) \
12186 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12188 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12189 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12190 int hmargin, vmargin, relief, idx, end;
12192 /* If image is a vector, choose the image according to the
12193 button state. */
12194 image = PROP (TOOL_BAR_ITEM_IMAGES);
12195 if (VECTORP (image))
12197 if (enabled_p)
12198 idx = (selected_p
12199 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12200 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12201 else
12202 idx = (selected_p
12203 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12204 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12206 eassert (ASIZE (image) >= idx);
12207 image = AREF (image, idx);
12209 else
12210 idx = -1;
12212 /* Ignore invalid image specifications. */
12213 if (!valid_image_p (image))
12214 continue;
12216 /* Display the tool-bar button pressed, or depressed. */
12217 plist = Fcopy_sequence (XCDR (image));
12219 /* Compute margin and relief to draw. */
12220 relief = (tool_bar_button_relief >= 0
12221 ? tool_bar_button_relief
12222 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12223 hmargin = vmargin = relief;
12225 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12226 INT_MAX - max (hmargin, vmargin)))
12228 hmargin += XFASTINT (Vtool_bar_button_margin);
12229 vmargin += XFASTINT (Vtool_bar_button_margin);
12231 else if (CONSP (Vtool_bar_button_margin))
12233 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12234 INT_MAX - hmargin))
12235 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12237 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12238 INT_MAX - vmargin))
12239 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12242 if (auto_raise_tool_bar_buttons_p)
12244 /* Add a `:relief' property to the image spec if the item is
12245 selected. */
12246 if (selected_p)
12248 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12249 hmargin -= relief;
12250 vmargin -= relief;
12253 else
12255 /* If image is selected, display it pressed, i.e. with a
12256 negative relief. If it's not selected, display it with a
12257 raised relief. */
12258 plist = Fplist_put (plist, QCrelief,
12259 (selected_p
12260 ? make_number (-relief)
12261 : make_number (relief)));
12262 hmargin -= relief;
12263 vmargin -= relief;
12266 /* Put a margin around the image. */
12267 if (hmargin || vmargin)
12269 if (hmargin == vmargin)
12270 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12271 else
12272 plist = Fplist_put (plist, QCmargin,
12273 Fcons (make_number (hmargin),
12274 make_number (vmargin)));
12277 /* If button is not enabled, and we don't have special images
12278 for the disabled state, make the image appear disabled by
12279 applying an appropriate algorithm to it. */
12280 if (!enabled_p && idx < 0)
12281 plist = Fplist_put (plist, QCconversion, Qdisabled);
12283 /* Put a `display' text property on the string for the image to
12284 display. Put a `menu-item' property on the string that gives
12285 the start of this item's properties in the tool-bar items
12286 vector. */
12287 image = Fcons (Qimage, plist);
12288 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12289 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12291 /* Let the last image hide all remaining spaces in the tool bar
12292 string. The string can be longer than needed when we reuse a
12293 previous string. */
12294 if (i + 1 == f->n_tool_bar_items)
12295 end = SCHARS (f->desired_tool_bar_string);
12296 else
12297 end = i + 1;
12298 Fadd_text_properties (make_number (i), make_number (end),
12299 props, f->desired_tool_bar_string);
12300 #undef PROP
12305 /* Display one line of the tool-bar of frame IT->f.
12307 HEIGHT specifies the desired height of the tool-bar line.
12308 If the actual height of the glyph row is less than HEIGHT, the
12309 row's height is increased to HEIGHT, and the icons are centered
12310 vertically in the new height.
12312 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12313 count a final empty row in case the tool-bar width exactly matches
12314 the window width.
12317 static void
12318 display_tool_bar_line (struct it *it, int height)
12320 struct glyph_row *row = it->glyph_row;
12321 int max_x = it->last_visible_x;
12322 struct glyph *last;
12324 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12325 clear_glyph_row (row);
12326 row->enabled_p = true;
12327 row->y = it->current_y;
12329 /* Note that this isn't made use of if the face hasn't a box,
12330 so there's no need to check the face here. */
12331 it->start_of_box_run_p = true;
12333 while (it->current_x < max_x)
12335 int x, n_glyphs_before, i, nglyphs;
12336 struct it it_before;
12338 /* Get the next display element. */
12339 if (!get_next_display_element (it))
12341 /* Don't count empty row if we are counting needed tool-bar lines. */
12342 if (height < 0 && !it->hpos)
12343 return;
12344 break;
12347 /* Produce glyphs. */
12348 n_glyphs_before = row->used[TEXT_AREA];
12349 it_before = *it;
12351 PRODUCE_GLYPHS (it);
12353 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12354 i = 0;
12355 x = it_before.current_x;
12356 while (i < nglyphs)
12358 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12360 if (x + glyph->pixel_width > max_x)
12362 /* Glyph doesn't fit on line. Backtrack. */
12363 row->used[TEXT_AREA] = n_glyphs_before;
12364 *it = it_before;
12365 /* If this is the only glyph on this line, it will never fit on the
12366 tool-bar, so skip it. But ensure there is at least one glyph,
12367 so we don't accidentally disable the tool-bar. */
12368 if (n_glyphs_before == 0
12369 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12370 break;
12371 goto out;
12374 ++it->hpos;
12375 x += glyph->pixel_width;
12376 ++i;
12379 /* Stop at line end. */
12380 if (ITERATOR_AT_END_OF_LINE_P (it))
12381 break;
12383 set_iterator_to_next (it, true);
12386 out:;
12388 row->displays_text_p = row->used[TEXT_AREA] != 0;
12390 /* Use default face for the border below the tool bar.
12392 FIXME: When auto-resize-tool-bars is grow-only, there is
12393 no additional border below the possibly empty tool-bar lines.
12394 So to make the extra empty lines look "normal", we have to
12395 use the tool-bar face for the border too. */
12396 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12397 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12398 it->face_id = DEFAULT_FACE_ID;
12400 extend_face_to_end_of_line (it);
12401 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12402 last->right_box_line_p = true;
12403 if (last == row->glyphs[TEXT_AREA])
12404 last->left_box_line_p = true;
12406 /* Make line the desired height and center it vertically. */
12407 if ((height -= it->max_ascent + it->max_descent) > 0)
12409 /* Don't add more than one line height. */
12410 height %= FRAME_LINE_HEIGHT (it->f);
12411 it->max_ascent += height / 2;
12412 it->max_descent += (height + 1) / 2;
12415 compute_line_metrics (it);
12417 /* If line is empty, make it occupy the rest of the tool-bar. */
12418 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12420 row->height = row->phys_height = it->last_visible_y - row->y;
12421 row->visible_height = row->height;
12422 row->ascent = row->phys_ascent = 0;
12423 row->extra_line_spacing = 0;
12426 row->full_width_p = true;
12427 row->continued_p = false;
12428 row->truncated_on_left_p = false;
12429 row->truncated_on_right_p = false;
12431 it->current_x = it->hpos = 0;
12432 it->current_y += row->height;
12433 ++it->vpos;
12434 ++it->glyph_row;
12438 /* Value is the number of pixels needed to make all tool-bar items of
12439 frame F visible. The actual number of glyph rows needed is
12440 returned in *N_ROWS if non-NULL. */
12441 static int
12442 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12444 struct window *w = XWINDOW (f->tool_bar_window);
12445 struct it it;
12446 /* tool_bar_height is called from redisplay_tool_bar after building
12447 the desired matrix, so use (unused) mode-line row as temporary row to
12448 avoid destroying the first tool-bar row. */
12449 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12451 /* Initialize an iterator for iteration over
12452 F->desired_tool_bar_string in the tool-bar window of frame F. */
12453 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12454 temp_row->reversed_p = false;
12455 it.first_visible_x = 0;
12456 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12457 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12458 it.paragraph_embedding = L2R;
12460 while (!ITERATOR_AT_END_P (&it))
12462 clear_glyph_row (temp_row);
12463 it.glyph_row = temp_row;
12464 display_tool_bar_line (&it, -1);
12466 clear_glyph_row (temp_row);
12468 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12469 if (n_rows)
12470 *n_rows = it.vpos > 0 ? it.vpos : -1;
12472 if (pixelwise)
12473 return it.current_y;
12474 else
12475 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12478 #endif /* !USE_GTK && !HAVE_NS */
12480 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12481 0, 2, 0,
12482 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12483 If FRAME is nil or omitted, use the selected frame. Optional argument
12484 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12485 (Lisp_Object frame, Lisp_Object pixelwise)
12487 int height = 0;
12489 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12490 struct frame *f = decode_any_frame (frame);
12492 if (WINDOWP (f->tool_bar_window)
12493 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12495 update_tool_bar (f, true);
12496 if (f->n_tool_bar_items)
12498 build_desired_tool_bar_string (f);
12499 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12502 #endif
12504 return make_number (height);
12508 /* Display the tool-bar of frame F. Value is true if tool-bar's
12509 height should be changed. */
12510 static bool
12511 redisplay_tool_bar (struct frame *f)
12513 f->tool_bar_redisplayed = true;
12514 #if defined (USE_GTK) || defined (HAVE_NS)
12516 if (FRAME_EXTERNAL_TOOL_BAR (f))
12517 update_frame_tool_bar (f);
12518 return false;
12520 #else /* !USE_GTK && !HAVE_NS */
12522 struct window *w;
12523 struct it it;
12524 struct glyph_row *row;
12526 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12527 do anything. This means you must start with tool-bar-lines
12528 non-zero to get the auto-sizing effect. Or in other words, you
12529 can turn off tool-bars by specifying tool-bar-lines zero. */
12530 if (!WINDOWP (f->tool_bar_window)
12531 || (w = XWINDOW (f->tool_bar_window),
12532 WINDOW_TOTAL_LINES (w) == 0))
12533 return false;
12535 /* Set up an iterator for the tool-bar window. */
12536 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12537 it.first_visible_x = 0;
12538 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12539 row = it.glyph_row;
12540 row->reversed_p = false;
12542 /* Build a string that represents the contents of the tool-bar. */
12543 build_desired_tool_bar_string (f);
12544 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12545 /* FIXME: This should be controlled by a user option. But it
12546 doesn't make sense to have an R2L tool bar if the menu bar cannot
12547 be drawn also R2L, and making the menu bar R2L is tricky due
12548 toolkit-specific code that implements it. If an R2L tool bar is
12549 ever supported, display_tool_bar_line should also be augmented to
12550 call unproduce_glyphs like display_line and display_string
12551 do. */
12552 it.paragraph_embedding = L2R;
12554 if (f->n_tool_bar_rows == 0)
12556 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12558 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12560 x_change_tool_bar_height (f, new_height);
12561 frame_default_tool_bar_height = new_height;
12562 /* Always do that now. */
12563 clear_glyph_matrix (w->desired_matrix);
12564 f->fonts_changed = true;
12565 return true;
12569 /* Display as many lines as needed to display all tool-bar items. */
12571 if (f->n_tool_bar_rows > 0)
12573 int border, rows, height, extra;
12575 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12576 border = XINT (Vtool_bar_border);
12577 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12578 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12579 else if (EQ (Vtool_bar_border, Qborder_width))
12580 border = f->border_width;
12581 else
12582 border = 0;
12583 if (border < 0)
12584 border = 0;
12586 rows = f->n_tool_bar_rows;
12587 height = max (1, (it.last_visible_y - border) / rows);
12588 extra = it.last_visible_y - border - height * rows;
12590 while (it.current_y < it.last_visible_y)
12592 int h = 0;
12593 if (extra > 0 && rows-- > 0)
12595 h = (extra + rows - 1) / rows;
12596 extra -= h;
12598 display_tool_bar_line (&it, height + h);
12601 else
12603 while (it.current_y < it.last_visible_y)
12604 display_tool_bar_line (&it, 0);
12607 /* It doesn't make much sense to try scrolling in the tool-bar
12608 window, so don't do it. */
12609 w->desired_matrix->no_scrolling_p = true;
12610 w->must_be_updated_p = true;
12612 if (!NILP (Vauto_resize_tool_bars))
12614 bool change_height_p = true;
12616 /* If we couldn't display everything, change the tool-bar's
12617 height if there is room for more. */
12618 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12619 change_height_p = true;
12621 /* We subtract 1 because display_tool_bar_line advances the
12622 glyph_row pointer before returning to its caller. We want to
12623 examine the last glyph row produced by
12624 display_tool_bar_line. */
12625 row = it.glyph_row - 1;
12627 /* If there are blank lines at the end, except for a partially
12628 visible blank line at the end that is smaller than
12629 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12630 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12631 && row->height >= FRAME_LINE_HEIGHT (f))
12632 change_height_p = true;
12634 /* If row displays tool-bar items, but is partially visible,
12635 change the tool-bar's height. */
12636 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12637 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12638 change_height_p = true;
12640 /* Resize windows as needed by changing the `tool-bar-lines'
12641 frame parameter. */
12642 if (change_height_p)
12644 int nrows;
12645 int new_height = tool_bar_height (f, &nrows, true);
12647 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12648 && !f->minimize_tool_bar_window_p)
12649 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12650 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12651 f->minimize_tool_bar_window_p = false;
12653 if (change_height_p)
12655 x_change_tool_bar_height (f, new_height);
12656 frame_default_tool_bar_height = new_height;
12657 clear_glyph_matrix (w->desired_matrix);
12658 f->n_tool_bar_rows = nrows;
12659 f->fonts_changed = true;
12661 return true;
12666 f->minimize_tool_bar_window_p = false;
12667 return false;
12669 #endif /* USE_GTK || HAVE_NS */
12672 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12674 /* Get information about the tool-bar item which is displayed in GLYPH
12675 on frame F. Return in *PROP_IDX the index where tool-bar item
12676 properties start in F->tool_bar_items. Value is false if
12677 GLYPH doesn't display a tool-bar item. */
12679 static bool
12680 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12682 Lisp_Object prop;
12683 int charpos;
12685 /* This function can be called asynchronously, which means we must
12686 exclude any possibility that Fget_text_property signals an
12687 error. */
12688 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12689 charpos = max (0, charpos);
12691 /* Get the text property `menu-item' at pos. The value of that
12692 property is the start index of this item's properties in
12693 F->tool_bar_items. */
12694 prop = Fget_text_property (make_number (charpos),
12695 Qmenu_item, f->current_tool_bar_string);
12696 if (! INTEGERP (prop))
12697 return false;
12698 *prop_idx = XINT (prop);
12699 return true;
12703 /* Get information about the tool-bar item at position X/Y on frame F.
12704 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12705 the current matrix of the tool-bar window of F, or NULL if not
12706 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12707 item in F->tool_bar_items. Value is
12709 -1 if X/Y is not on a tool-bar item
12710 0 if X/Y is on the same item that was highlighted before.
12711 1 otherwise. */
12713 static int
12714 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12715 int *hpos, int *vpos, int *prop_idx)
12717 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12718 struct window *w = XWINDOW (f->tool_bar_window);
12719 int area;
12721 /* Find the glyph under X/Y. */
12722 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12723 if (*glyph == NULL)
12724 return -1;
12726 /* Get the start of this tool-bar item's properties in
12727 f->tool_bar_items. */
12728 if (!tool_bar_item_info (f, *glyph, prop_idx))
12729 return -1;
12731 /* Is mouse on the highlighted item? */
12732 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12733 && *vpos >= hlinfo->mouse_face_beg_row
12734 && *vpos <= hlinfo->mouse_face_end_row
12735 && (*vpos > hlinfo->mouse_face_beg_row
12736 || *hpos >= hlinfo->mouse_face_beg_col)
12737 && (*vpos < hlinfo->mouse_face_end_row
12738 || *hpos < hlinfo->mouse_face_end_col
12739 || hlinfo->mouse_face_past_end))
12740 return 0;
12742 return 1;
12746 /* EXPORT:
12747 Handle mouse button event on the tool-bar of frame F, at
12748 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12749 false for button release. MODIFIERS is event modifiers for button
12750 release. */
12752 void
12753 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12754 int modifiers)
12756 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12757 struct window *w = XWINDOW (f->tool_bar_window);
12758 int hpos, vpos, prop_idx;
12759 struct glyph *glyph;
12760 Lisp_Object enabled_p;
12761 int ts;
12763 /* If not on the highlighted tool-bar item, and mouse-highlight is
12764 non-nil, return. This is so we generate the tool-bar button
12765 click only when the mouse button is released on the same item as
12766 where it was pressed. However, when mouse-highlight is disabled,
12767 generate the click when the button is released regardless of the
12768 highlight, since tool-bar items are not highlighted in that
12769 case. */
12770 frame_to_window_pixel_xy (w, &x, &y);
12771 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12772 if (ts == -1
12773 || (ts != 0 && !NILP (Vmouse_highlight)))
12774 return;
12776 /* When mouse-highlight is off, generate the click for the item
12777 where the button was pressed, disregarding where it was
12778 released. */
12779 if (NILP (Vmouse_highlight) && !down_p)
12780 prop_idx = f->last_tool_bar_item;
12782 /* If item is disabled, do nothing. */
12783 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12784 if (NILP (enabled_p))
12785 return;
12787 if (down_p)
12789 /* Show item in pressed state. */
12790 if (!NILP (Vmouse_highlight))
12791 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12792 f->last_tool_bar_item = prop_idx;
12794 else
12796 Lisp_Object key, frame;
12797 struct input_event event;
12798 EVENT_INIT (event);
12800 /* Show item in released state. */
12801 if (!NILP (Vmouse_highlight))
12802 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12804 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12806 XSETFRAME (frame, f);
12807 event.kind = TOOL_BAR_EVENT;
12808 event.frame_or_window = frame;
12809 event.arg = frame;
12810 kbd_buffer_store_event (&event);
12812 event.kind = TOOL_BAR_EVENT;
12813 event.frame_or_window = frame;
12814 event.arg = key;
12815 event.modifiers = modifiers;
12816 kbd_buffer_store_event (&event);
12817 f->last_tool_bar_item = -1;
12822 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12823 tool-bar window-relative coordinates X/Y. Called from
12824 note_mouse_highlight. */
12826 static void
12827 note_tool_bar_highlight (struct frame *f, int x, int y)
12829 Lisp_Object window = f->tool_bar_window;
12830 struct window *w = XWINDOW (window);
12831 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12832 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12833 int hpos, vpos;
12834 struct glyph *glyph;
12835 struct glyph_row *row;
12836 int i;
12837 Lisp_Object enabled_p;
12838 int prop_idx;
12839 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12840 bool mouse_down_p;
12841 int rc;
12843 /* Function note_mouse_highlight is called with negative X/Y
12844 values when mouse moves outside of the frame. */
12845 if (x <= 0 || y <= 0)
12847 clear_mouse_face (hlinfo);
12848 return;
12851 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12852 if (rc < 0)
12854 /* Not on tool-bar item. */
12855 clear_mouse_face (hlinfo);
12856 return;
12858 else if (rc == 0)
12859 /* On same tool-bar item as before. */
12860 goto set_help_echo;
12862 clear_mouse_face (hlinfo);
12864 /* Mouse is down, but on different tool-bar item? */
12865 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12866 && f == dpyinfo->last_mouse_frame);
12868 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12869 return;
12871 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12873 /* If tool-bar item is not enabled, don't highlight it. */
12874 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12875 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12877 /* Compute the x-position of the glyph. In front and past the
12878 image is a space. We include this in the highlighted area. */
12879 row = MATRIX_ROW (w->current_matrix, vpos);
12880 for (i = x = 0; i < hpos; ++i)
12881 x += row->glyphs[TEXT_AREA][i].pixel_width;
12883 /* Record this as the current active region. */
12884 hlinfo->mouse_face_beg_col = hpos;
12885 hlinfo->mouse_face_beg_row = vpos;
12886 hlinfo->mouse_face_beg_x = x;
12887 hlinfo->mouse_face_past_end = false;
12889 hlinfo->mouse_face_end_col = hpos + 1;
12890 hlinfo->mouse_face_end_row = vpos;
12891 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12892 hlinfo->mouse_face_window = window;
12893 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12895 /* Display it as active. */
12896 show_mouse_face (hlinfo, draw);
12899 set_help_echo:
12901 /* Set help_echo_string to a help string to display for this tool-bar item.
12902 XTread_socket does the rest. */
12903 help_echo_object = help_echo_window = Qnil;
12904 help_echo_pos = -1;
12905 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12906 if (NILP (help_echo_string))
12907 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12910 #endif /* !USE_GTK && !HAVE_NS */
12912 #endif /* HAVE_WINDOW_SYSTEM */
12916 /************************************************************************
12917 Horizontal scrolling
12918 ************************************************************************/
12920 /* For all leaf windows in the window tree rooted at WINDOW, set their
12921 hscroll value so that PT is (i) visible in the window, and (ii) so
12922 that it is not within a certain margin at the window's left and
12923 right border. Value is true if any window's hscroll has been
12924 changed. */
12926 static bool
12927 hscroll_window_tree (Lisp_Object window)
12929 bool hscrolled_p = false;
12930 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12931 int hscroll_step_abs = 0;
12932 double hscroll_step_rel = 0;
12934 if (hscroll_relative_p)
12936 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12937 if (hscroll_step_rel < 0)
12939 hscroll_relative_p = false;
12940 hscroll_step_abs = 0;
12943 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12945 hscroll_step_abs = XINT (Vhscroll_step);
12946 if (hscroll_step_abs < 0)
12947 hscroll_step_abs = 0;
12949 else
12950 hscroll_step_abs = 0;
12952 while (WINDOWP (window))
12954 struct window *w = XWINDOW (window);
12956 if (WINDOWP (w->contents))
12957 hscrolled_p |= hscroll_window_tree (w->contents);
12958 else if (w->cursor.vpos >= 0)
12960 int h_margin;
12961 int text_area_width;
12962 struct glyph_row *cursor_row;
12963 struct glyph_row *bottom_row;
12965 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12966 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12967 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12968 else
12969 cursor_row = bottom_row - 1;
12971 if (!cursor_row->enabled_p)
12973 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12974 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12975 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12976 else
12977 cursor_row = bottom_row - 1;
12979 bool row_r2l_p = cursor_row->reversed_p;
12981 text_area_width = window_box_width (w, TEXT_AREA);
12983 /* Scroll when cursor is inside this scroll margin. */
12984 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12986 /* If the position of this window's point has explicitly
12987 changed, no more suspend auto hscrolling. */
12988 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12989 w->suspend_auto_hscroll = false;
12991 /* Remember window point. */
12992 Fset_marker (w->old_pointm,
12993 ((w == XWINDOW (selected_window))
12994 ? make_number (BUF_PT (XBUFFER (w->contents)))
12995 : Fmarker_position (w->pointm)),
12996 w->contents);
12998 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12999 && !w->suspend_auto_hscroll
13000 /* In some pathological cases, like restoring a window
13001 configuration into a frame that is much smaller than
13002 the one from which the configuration was saved, we
13003 get glyph rows whose start and end have zero buffer
13004 positions, which we cannot handle below. Just skip
13005 such windows. */
13006 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13007 /* For left-to-right rows, hscroll when cursor is either
13008 (i) inside the right hscroll margin, or (ii) if it is
13009 inside the left margin and the window is already
13010 hscrolled. */
13011 && ((!row_r2l_p
13012 && ((w->hscroll && w->cursor.x <= h_margin)
13013 || (cursor_row->enabled_p
13014 && cursor_row->truncated_on_right_p
13015 && (w->cursor.x >= text_area_width - h_margin))))
13016 /* For right-to-left rows, the logic is similar,
13017 except that rules for scrolling to left and right
13018 are reversed. E.g., if cursor.x <= h_margin, we
13019 need to hscroll "to the right" unconditionally,
13020 and that will scroll the screen to the left so as
13021 to reveal the next portion of the row. */
13022 || (row_r2l_p
13023 && ((cursor_row->enabled_p
13024 /* FIXME: It is confusing to set the
13025 truncated_on_right_p flag when R2L rows
13026 are actually truncated on the left. */
13027 && cursor_row->truncated_on_right_p
13028 && w->cursor.x <= h_margin)
13029 || (w->hscroll
13030 && (w->cursor.x >= text_area_width - h_margin))))))
13032 struct it it;
13033 ptrdiff_t hscroll;
13034 struct buffer *saved_current_buffer;
13035 ptrdiff_t pt;
13036 int wanted_x;
13038 /* Find point in a display of infinite width. */
13039 saved_current_buffer = current_buffer;
13040 current_buffer = XBUFFER (w->contents);
13042 if (w == XWINDOW (selected_window))
13043 pt = PT;
13044 else
13045 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13047 /* Move iterator to pt starting at cursor_row->start in
13048 a line with infinite width. */
13049 init_to_row_start (&it, w, cursor_row);
13050 it.last_visible_x = INFINITY;
13051 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13052 /* If the line ends in an overlay string with a newline,
13053 we might infloop, because displaying the window will
13054 want to put the cursor after the overlay, i.e. at X
13055 coordinate of zero on the next screen line. So we
13056 use the buffer position prior to the overlay string
13057 instead. */
13058 if (it.method == GET_FROM_STRING && pt > 1)
13060 init_to_row_start (&it, w, cursor_row);
13061 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13063 current_buffer = saved_current_buffer;
13065 /* Position cursor in window. */
13066 if (!hscroll_relative_p && hscroll_step_abs == 0)
13067 hscroll = max (0, (it.current_x
13068 - (ITERATOR_AT_END_OF_LINE_P (&it)
13069 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13070 : (text_area_width / 2))))
13071 / FRAME_COLUMN_WIDTH (it.f);
13072 else if ((!row_r2l_p
13073 && w->cursor.x >= text_area_width - h_margin)
13074 || (row_r2l_p && w->cursor.x <= h_margin))
13076 if (hscroll_relative_p)
13077 wanted_x = text_area_width * (1 - hscroll_step_rel)
13078 - h_margin;
13079 else
13080 wanted_x = text_area_width
13081 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13082 - h_margin;
13083 hscroll
13084 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13086 else
13088 if (hscroll_relative_p)
13089 wanted_x = text_area_width * hscroll_step_rel
13090 + h_margin;
13091 else
13092 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13093 + h_margin;
13094 hscroll
13095 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13097 hscroll = max (hscroll, w->min_hscroll);
13099 /* Don't prevent redisplay optimizations if hscroll
13100 hasn't changed, as it will unnecessarily slow down
13101 redisplay. */
13102 if (w->hscroll != hscroll)
13104 struct buffer *b = XBUFFER (w->contents);
13105 b->prevent_redisplay_optimizations_p = true;
13106 w->hscroll = hscroll;
13107 hscrolled_p = true;
13112 window = w->next;
13115 /* Value is true if hscroll of any leaf window has been changed. */
13116 return hscrolled_p;
13120 /* Set hscroll so that cursor is visible and not inside horizontal
13121 scroll margins for all windows in the tree rooted at WINDOW. See
13122 also hscroll_window_tree above. Value is true if any window's
13123 hscroll has been changed. If it has, desired matrices on the frame
13124 of WINDOW are cleared. */
13126 static bool
13127 hscroll_windows (Lisp_Object window)
13129 bool hscrolled_p = hscroll_window_tree (window);
13130 if (hscrolled_p)
13131 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13132 return hscrolled_p;
13137 /************************************************************************
13138 Redisplay
13139 ************************************************************************/
13141 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13142 This is sometimes handy to have in a debugger session. */
13144 #ifdef GLYPH_DEBUG
13146 /* First and last unchanged row for try_window_id. */
13148 static int debug_first_unchanged_at_end_vpos;
13149 static int debug_last_unchanged_at_beg_vpos;
13151 /* Delta vpos and y. */
13153 static int debug_dvpos, debug_dy;
13155 /* Delta in characters and bytes for try_window_id. */
13157 static ptrdiff_t debug_delta, debug_delta_bytes;
13159 /* Values of window_end_pos and window_end_vpos at the end of
13160 try_window_id. */
13162 static ptrdiff_t debug_end_vpos;
13164 /* Append a string to W->desired_matrix->method. FMT is a printf
13165 format string. If trace_redisplay_p is true also printf the
13166 resulting string to stderr. */
13168 static void debug_method_add (struct window *, char const *, ...)
13169 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13171 static void
13172 debug_method_add (struct window *w, char const *fmt, ...)
13174 void *ptr = w;
13175 char *method = w->desired_matrix->method;
13176 int len = strlen (method);
13177 int size = sizeof w->desired_matrix->method;
13178 int remaining = size - len - 1;
13179 va_list ap;
13181 if (len && remaining)
13183 method[len] = '|';
13184 --remaining, ++len;
13187 va_start (ap, fmt);
13188 vsnprintf (method + len, remaining + 1, fmt, ap);
13189 va_end (ap);
13191 if (trace_redisplay_p)
13192 fprintf (stderr, "%p (%s): %s\n",
13193 ptr,
13194 ((BUFFERP (w->contents)
13195 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13196 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13197 : "no buffer"),
13198 method + len);
13201 #endif /* GLYPH_DEBUG */
13204 /* Value is true if all changes in window W, which displays
13205 current_buffer, are in the text between START and END. START is a
13206 buffer position, END is given as a distance from Z. Used in
13207 redisplay_internal for display optimization. */
13209 static bool
13210 text_outside_line_unchanged_p (struct window *w,
13211 ptrdiff_t start, ptrdiff_t end)
13213 bool unchanged_p = true;
13215 /* If text or overlays have changed, see where. */
13216 if (window_outdated (w))
13218 /* Gap in the line? */
13219 if (GPT < start || Z - GPT < end)
13220 unchanged_p = false;
13222 /* Changes start in front of the line, or end after it? */
13223 if (unchanged_p
13224 && (BEG_UNCHANGED < start - 1
13225 || END_UNCHANGED < end))
13226 unchanged_p = false;
13228 /* If selective display, can't optimize if changes start at the
13229 beginning of the line. */
13230 if (unchanged_p
13231 && INTEGERP (BVAR (current_buffer, selective_display))
13232 && XINT (BVAR (current_buffer, selective_display)) > 0
13233 && (BEG_UNCHANGED < start || GPT <= start))
13234 unchanged_p = false;
13236 /* If there are overlays at the start or end of the line, these
13237 may have overlay strings with newlines in them. A change at
13238 START, for instance, may actually concern the display of such
13239 overlay strings as well, and they are displayed on different
13240 lines. So, quickly rule out this case. (For the future, it
13241 might be desirable to implement something more telling than
13242 just BEG/END_UNCHANGED.) */
13243 if (unchanged_p)
13245 if (BEG + BEG_UNCHANGED == start
13246 && overlay_touches_p (start))
13247 unchanged_p = false;
13248 if (END_UNCHANGED == end
13249 && overlay_touches_p (Z - end))
13250 unchanged_p = false;
13253 /* Under bidi reordering, adding or deleting a character in the
13254 beginning of a paragraph, before the first strong directional
13255 character, can change the base direction of the paragraph (unless
13256 the buffer specifies a fixed paragraph direction), which will
13257 require redisplaying the whole paragraph. It might be worthwhile
13258 to find the paragraph limits and widen the range of redisplayed
13259 lines to that, but for now just give up this optimization. */
13260 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13261 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13262 unchanged_p = false;
13265 return unchanged_p;
13269 /* Do a frame update, taking possible shortcuts into account. This is
13270 the main external entry point for redisplay.
13272 If the last redisplay displayed an echo area message and that message
13273 is no longer requested, we clear the echo area or bring back the
13274 mini-buffer if that is in use. */
13276 void
13277 redisplay (void)
13279 redisplay_internal ();
13283 static Lisp_Object
13284 overlay_arrow_string_or_property (Lisp_Object var)
13286 Lisp_Object val;
13288 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13289 return val;
13291 return Voverlay_arrow_string;
13294 /* Return true if there are any overlay-arrows in current_buffer. */
13295 static bool
13296 overlay_arrow_in_current_buffer_p (void)
13298 Lisp_Object vlist;
13300 for (vlist = Voverlay_arrow_variable_list;
13301 CONSP (vlist);
13302 vlist = XCDR (vlist))
13304 Lisp_Object var = XCAR (vlist);
13305 Lisp_Object val;
13307 if (!SYMBOLP (var))
13308 continue;
13309 val = find_symbol_value (var);
13310 if (MARKERP (val)
13311 && current_buffer == XMARKER (val)->buffer)
13312 return true;
13314 return false;
13318 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13319 has changed. */
13321 static bool
13322 overlay_arrows_changed_p (void)
13324 Lisp_Object vlist;
13326 for (vlist = Voverlay_arrow_variable_list;
13327 CONSP (vlist);
13328 vlist = XCDR (vlist))
13330 Lisp_Object var = XCAR (vlist);
13331 Lisp_Object val, pstr;
13333 if (!SYMBOLP (var))
13334 continue;
13335 val = find_symbol_value (var);
13336 if (!MARKERP (val))
13337 continue;
13338 if (! EQ (COERCE_MARKER (val),
13339 Fget (var, Qlast_arrow_position))
13340 || ! (pstr = overlay_arrow_string_or_property (var),
13341 EQ (pstr, Fget (var, Qlast_arrow_string))))
13342 return true;
13344 return false;
13347 /* Mark overlay arrows to be updated on next redisplay. */
13349 static void
13350 update_overlay_arrows (int up_to_date)
13352 Lisp_Object vlist;
13354 for (vlist = Voverlay_arrow_variable_list;
13355 CONSP (vlist);
13356 vlist = XCDR (vlist))
13358 Lisp_Object var = XCAR (vlist);
13360 if (!SYMBOLP (var))
13361 continue;
13363 if (up_to_date > 0)
13365 Lisp_Object val = find_symbol_value (var);
13366 Fput (var, Qlast_arrow_position,
13367 COERCE_MARKER (val));
13368 Fput (var, Qlast_arrow_string,
13369 overlay_arrow_string_or_property (var));
13371 else if (up_to_date < 0
13372 || !NILP (Fget (var, Qlast_arrow_position)))
13374 Fput (var, Qlast_arrow_position, Qt);
13375 Fput (var, Qlast_arrow_string, Qt);
13381 /* Return overlay arrow string to display at row.
13382 Return integer (bitmap number) for arrow bitmap in left fringe.
13383 Return nil if no overlay arrow. */
13385 static Lisp_Object
13386 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13388 Lisp_Object vlist;
13390 for (vlist = Voverlay_arrow_variable_list;
13391 CONSP (vlist);
13392 vlist = XCDR (vlist))
13394 Lisp_Object var = XCAR (vlist);
13395 Lisp_Object val;
13397 if (!SYMBOLP (var))
13398 continue;
13400 val = find_symbol_value (var);
13402 if (MARKERP (val)
13403 && current_buffer == XMARKER (val)->buffer
13404 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13406 if (FRAME_WINDOW_P (it->f)
13407 /* FIXME: if ROW->reversed_p is set, this should test
13408 the right fringe, not the left one. */
13409 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13411 #ifdef HAVE_WINDOW_SYSTEM
13412 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13414 int fringe_bitmap = lookup_fringe_bitmap (val);
13415 if (fringe_bitmap != 0)
13416 return make_number (fringe_bitmap);
13418 #endif
13419 return make_number (-1); /* Use default arrow bitmap. */
13421 return overlay_arrow_string_or_property (var);
13425 return Qnil;
13428 /* Return true if point moved out of or into a composition. Otherwise
13429 return false. PREV_BUF and PREV_PT are the last point buffer and
13430 position. BUF and PT are the current point buffer and position. */
13432 static bool
13433 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13434 struct buffer *buf, ptrdiff_t pt)
13436 ptrdiff_t start, end;
13437 Lisp_Object prop;
13438 Lisp_Object buffer;
13440 XSETBUFFER (buffer, buf);
13441 /* Check a composition at the last point if point moved within the
13442 same buffer. */
13443 if (prev_buf == buf)
13445 if (prev_pt == pt)
13446 /* Point didn't move. */
13447 return false;
13449 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13450 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13451 && composition_valid_p (start, end, prop)
13452 && start < prev_pt && end > prev_pt)
13453 /* The last point was within the composition. Return true iff
13454 point moved out of the composition. */
13455 return (pt <= start || pt >= end);
13458 /* Check a composition at the current point. */
13459 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13460 && find_composition (pt, -1, &start, &end, &prop, buffer)
13461 && composition_valid_p (start, end, prop)
13462 && start < pt && end > pt);
13465 /* Reconsider the clip changes of buffer which is displayed in W. */
13467 static void
13468 reconsider_clip_changes (struct window *w)
13470 struct buffer *b = XBUFFER (w->contents);
13472 if (b->clip_changed
13473 && w->window_end_valid
13474 && w->current_matrix->buffer == b
13475 && w->current_matrix->zv == BUF_ZV (b)
13476 && w->current_matrix->begv == BUF_BEGV (b))
13477 b->clip_changed = false;
13479 /* If display wasn't paused, and W is not a tool bar window, see if
13480 point has been moved into or out of a composition. In that case,
13481 set b->clip_changed to force updating the screen. If
13482 b->clip_changed has already been set, skip this check. */
13483 if (!b->clip_changed && w->window_end_valid)
13485 ptrdiff_t pt = (w == XWINDOW (selected_window)
13486 ? PT : marker_position (w->pointm));
13488 if ((w->current_matrix->buffer != b || pt != w->last_point)
13489 && check_point_in_composition (w->current_matrix->buffer,
13490 w->last_point, b, pt))
13491 b->clip_changed = true;
13495 static void
13496 propagate_buffer_redisplay (void)
13497 { /* Resetting b->text->redisplay is problematic!
13498 We can't just reset it in the case that some window that displays
13499 it has not been redisplayed; and such a window can stay
13500 unredisplayed for a long time if it's currently invisible.
13501 But we do want to reset it at the end of redisplay otherwise
13502 its displayed windows will keep being redisplayed over and over
13503 again.
13504 So we copy all b->text->redisplay flags up to their windows here,
13505 such that mark_window_display_accurate can safely reset
13506 b->text->redisplay. */
13507 Lisp_Object ws = window_list ();
13508 for (; CONSP (ws); ws = XCDR (ws))
13510 struct window *thisw = XWINDOW (XCAR (ws));
13511 struct buffer *thisb = XBUFFER (thisw->contents);
13512 if (thisb->text->redisplay)
13513 thisw->redisplay = true;
13517 #define STOP_POLLING \
13518 do { if (! polling_stopped_here) stop_polling (); \
13519 polling_stopped_here = true; } while (false)
13521 #define RESUME_POLLING \
13522 do { if (polling_stopped_here) start_polling (); \
13523 polling_stopped_here = false; } while (false)
13526 /* Perhaps in the future avoid recentering windows if it
13527 is not necessary; currently that causes some problems. */
13529 static void
13530 redisplay_internal (void)
13532 struct window *w = XWINDOW (selected_window);
13533 struct window *sw;
13534 struct frame *fr;
13535 bool pending;
13536 bool must_finish = false, match_p;
13537 struct text_pos tlbufpos, tlendpos;
13538 int number_of_visible_frames;
13539 ptrdiff_t count;
13540 struct frame *sf;
13541 bool polling_stopped_here = false;
13542 Lisp_Object tail, frame;
13544 /* Set a limit to the number of retries we perform due to horizontal
13545 scrolling, this avoids getting stuck in an uninterruptible
13546 infinite loop (Bug #24633). */
13547 enum { MAX_HSCROLL_RETRIES = 16 };
13548 int hscroll_retries = 0;
13550 /* True means redisplay has to consider all windows on all
13551 frames. False, only selected_window is considered. */
13552 bool consider_all_windows_p;
13554 /* True means redisplay has to redisplay the miniwindow. */
13555 bool update_miniwindow_p = false;
13557 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13559 /* No redisplay if running in batch mode or frame is not yet fully
13560 initialized, or redisplay is explicitly turned off by setting
13561 Vinhibit_redisplay. */
13562 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13563 || !NILP (Vinhibit_redisplay))
13564 return;
13566 /* Don't examine these until after testing Vinhibit_redisplay.
13567 When Emacs is shutting down, perhaps because its connection to
13568 X has dropped, we should not look at them at all. */
13569 fr = XFRAME (w->frame);
13570 sf = SELECTED_FRAME ();
13572 if (!fr->glyphs_initialized_p)
13573 return;
13575 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13576 if (popup_activated ())
13577 return;
13578 #endif
13580 /* I don't think this happens but let's be paranoid. */
13581 if (redisplaying_p)
13582 return;
13584 /* Record a function that clears redisplaying_p
13585 when we leave this function. */
13586 count = SPECPDL_INDEX ();
13587 record_unwind_protect_void (unwind_redisplay);
13588 redisplaying_p = true;
13589 block_buffer_flips ();
13590 specbind (Qinhibit_free_realized_faces, Qnil);
13592 /* Record this function, so it appears on the profiler's backtraces. */
13593 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13595 FOR_EACH_FRAME (tail, frame)
13596 XFRAME (frame)->already_hscrolled_p = false;
13598 retry:
13599 /* Remember the currently selected window. */
13600 sw = w;
13602 pending = false;
13603 forget_escape_and_glyphless_faces ();
13605 inhibit_free_realized_faces = false;
13607 /* If face_change, init_iterator will free all realized faces, which
13608 includes the faces referenced from current matrices. So, we
13609 can't reuse current matrices in this case. */
13610 if (face_change)
13611 windows_or_buffers_changed = 47;
13613 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13614 && FRAME_TTY (sf)->previous_frame != sf)
13616 /* Since frames on a single ASCII terminal share the same
13617 display area, displaying a different frame means redisplay
13618 the whole thing. */
13619 SET_FRAME_GARBAGED (sf);
13620 #ifndef DOS_NT
13621 set_tty_color_mode (FRAME_TTY (sf), sf);
13622 #endif
13623 FRAME_TTY (sf)->previous_frame = sf;
13626 /* Set the visible flags for all frames. Do this before checking for
13627 resized or garbaged frames; they want to know if their frames are
13628 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13629 number_of_visible_frames = 0;
13631 FOR_EACH_FRAME (tail, frame)
13633 struct frame *f = XFRAME (frame);
13635 if (FRAME_VISIBLE_P (f))
13637 ++number_of_visible_frames;
13638 /* Adjust matrices for visible frames only. */
13639 if (f->fonts_changed)
13641 adjust_frame_glyphs (f);
13642 /* Disable all redisplay optimizations for this frame.
13643 This is because adjust_frame_glyphs resets the
13644 enabled_p flag for all glyph rows of all windows, so
13645 many optimizations will fail anyway, and some might
13646 fail to test that flag and do bogus things as
13647 result. */
13648 SET_FRAME_GARBAGED (f);
13649 f->fonts_changed = false;
13651 /* If cursor type has been changed on the frame
13652 other than selected, consider all frames. */
13653 if (f != sf && f->cursor_type_changed)
13654 fset_redisplay (f);
13656 clear_desired_matrices (f);
13659 /* Notice any pending interrupt request to change frame size. */
13660 do_pending_window_change (true);
13662 /* do_pending_window_change could change the selected_window due to
13663 frame resizing which makes the selected window too small. */
13664 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13665 sw = w;
13667 /* Clear frames marked as garbaged. */
13668 clear_garbaged_frames ();
13670 /* Build menubar and tool-bar items. */
13671 if (NILP (Vmemory_full))
13672 prepare_menu_bars ();
13674 reconsider_clip_changes (w);
13676 /* In most cases selected window displays current buffer. */
13677 match_p = XBUFFER (w->contents) == current_buffer;
13678 if (match_p)
13680 /* Detect case that we need to write or remove a star in the mode line. */
13681 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13682 w->update_mode_line = true;
13684 if (mode_line_update_needed (w))
13685 w->update_mode_line = true;
13687 /* If reconsider_clip_changes above decided that the narrowing
13688 in the current buffer changed, make sure all other windows
13689 showing that buffer will be redisplayed. */
13690 if (current_buffer->clip_changed)
13691 bset_update_mode_line (current_buffer);
13694 /* Normally the message* functions will have already displayed and
13695 updated the echo area, but the frame may have been trashed, or
13696 the update may have been preempted, so display the echo area
13697 again here. Checking message_cleared_p captures the case that
13698 the echo area should be cleared. */
13699 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13700 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13701 || (message_cleared_p
13702 && minibuf_level == 0
13703 /* If the mini-window is currently selected, this means the
13704 echo-area doesn't show through. */
13705 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13707 echo_area_display (false);
13709 /* If echo_area_display resizes the mini-window, the redisplay and
13710 window_sizes_changed flags of the selected frame are set, but
13711 it's too late for the hooks in window-size-change-functions,
13712 which have been examined already in prepare_menu_bars. So in
13713 that case we call the hooks here only for the selected frame. */
13714 if (sf->redisplay)
13716 ptrdiff_t count1 = SPECPDL_INDEX ();
13718 record_unwind_save_match_data ();
13719 run_window_size_change_functions (selected_frame);
13720 unbind_to (count1, Qnil);
13723 if (message_cleared_p)
13724 update_miniwindow_p = true;
13726 must_finish = true;
13728 /* If we don't display the current message, don't clear the
13729 message_cleared_p flag, because, if we did, we wouldn't clear
13730 the echo area in the next redisplay which doesn't preserve
13731 the echo area. */
13732 if (!display_last_displayed_message_p)
13733 message_cleared_p = false;
13735 else if (EQ (selected_window, minibuf_window)
13736 && (current_buffer->clip_changed || window_outdated (w))
13737 && resize_mini_window (w, false))
13739 if (sf->redisplay)
13741 ptrdiff_t count1 = SPECPDL_INDEX ();
13743 record_unwind_save_match_data ();
13744 run_window_size_change_functions (selected_frame);
13745 unbind_to (count1, Qnil);
13748 /* Resized active mini-window to fit the size of what it is
13749 showing if its contents might have changed. */
13750 must_finish = true;
13752 /* If window configuration was changed, frames may have been
13753 marked garbaged. Clear them or we will experience
13754 surprises wrt scrolling. */
13755 clear_garbaged_frames ();
13758 if (windows_or_buffers_changed && !update_mode_lines)
13759 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13760 only the windows's contents needs to be refreshed, or whether the
13761 mode-lines also need a refresh. */
13762 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13763 ? REDISPLAY_SOME : 32);
13765 /* If specs for an arrow have changed, do thorough redisplay
13766 to ensure we remove any arrow that should no longer exist. */
13767 if (overlay_arrows_changed_p ())
13768 /* Apparently, this is the only case where we update other windows,
13769 without updating other mode-lines. */
13770 windows_or_buffers_changed = 49;
13772 consider_all_windows_p = (update_mode_lines
13773 || windows_or_buffers_changed);
13775 #define AINC(a,i) \
13777 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13778 if (INTEGERP (entry)) \
13779 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13782 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13783 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13785 /* Optimize the case that only the line containing the cursor in the
13786 selected window has changed. Variables starting with this_ are
13787 set in display_line and record information about the line
13788 containing the cursor. */
13789 tlbufpos = this_line_start_pos;
13790 tlendpos = this_line_end_pos;
13791 if (!consider_all_windows_p
13792 && CHARPOS (tlbufpos) > 0
13793 && !w->update_mode_line
13794 && !current_buffer->clip_changed
13795 && !current_buffer->prevent_redisplay_optimizations_p
13796 && FRAME_VISIBLE_P (XFRAME (w->frame))
13797 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13798 && !XFRAME (w->frame)->cursor_type_changed
13799 && !XFRAME (w->frame)->face_change
13800 /* Make sure recorded data applies to current buffer, etc. */
13801 && this_line_buffer == current_buffer
13802 && match_p
13803 && !w->force_start
13804 && !w->optional_new_start
13805 /* Point must be on the line that we have info recorded about. */
13806 && PT >= CHARPOS (tlbufpos)
13807 && PT <= Z - CHARPOS (tlendpos)
13808 /* All text outside that line, including its final newline,
13809 must be unchanged. */
13810 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13811 CHARPOS (tlendpos)))
13813 if (CHARPOS (tlbufpos) > BEGV
13814 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13815 && (CHARPOS (tlbufpos) == ZV
13816 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13817 /* Former continuation line has disappeared by becoming empty. */
13818 goto cancel;
13819 else if (window_outdated (w) || MINI_WINDOW_P (w))
13821 /* We have to handle the case of continuation around a
13822 wide-column character (see the comment in indent.c around
13823 line 1340).
13825 For instance, in the following case:
13827 -------- Insert --------
13828 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13829 J_I_ ==> J_I_ `^^' are cursors.
13830 ^^ ^^
13831 -------- --------
13833 As we have to redraw the line above, we cannot use this
13834 optimization. */
13836 struct it it;
13837 int line_height_before = this_line_pixel_height;
13839 /* Note that start_display will handle the case that the
13840 line starting at tlbufpos is a continuation line. */
13841 start_display (&it, w, tlbufpos);
13843 /* Implementation note: It this still necessary? */
13844 if (it.current_x != this_line_start_x)
13845 goto cancel;
13847 TRACE ((stderr, "trying display optimization 1\n"));
13848 w->cursor.vpos = -1;
13849 overlay_arrow_seen = false;
13850 it.vpos = this_line_vpos;
13851 it.current_y = this_line_y;
13852 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13853 display_line (&it);
13855 /* If line contains point, is not continued,
13856 and ends at same distance from eob as before, we win. */
13857 if (w->cursor.vpos >= 0
13858 /* Line is not continued, otherwise this_line_start_pos
13859 would have been set to 0 in display_line. */
13860 && CHARPOS (this_line_start_pos)
13861 /* Line ends as before. */
13862 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13863 /* Line has same height as before. Otherwise other lines
13864 would have to be shifted up or down. */
13865 && this_line_pixel_height == line_height_before)
13867 /* If this is not the window's last line, we must adjust
13868 the charstarts of the lines below. */
13869 if (it.current_y < it.last_visible_y)
13871 struct glyph_row *row
13872 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13873 ptrdiff_t delta, delta_bytes;
13875 /* We used to distinguish between two cases here,
13876 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13877 when the line ends in a newline or the end of the
13878 buffer's accessible portion. But both cases did
13879 the same, so they were collapsed. */
13880 delta = (Z
13881 - CHARPOS (tlendpos)
13882 - MATRIX_ROW_START_CHARPOS (row));
13883 delta_bytes = (Z_BYTE
13884 - BYTEPOS (tlendpos)
13885 - MATRIX_ROW_START_BYTEPOS (row));
13887 increment_matrix_positions (w->current_matrix,
13888 this_line_vpos + 1,
13889 w->current_matrix->nrows,
13890 delta, delta_bytes);
13893 /* If this row displays text now but previously didn't,
13894 or vice versa, w->window_end_vpos may have to be
13895 adjusted. */
13896 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13898 if (w->window_end_vpos < this_line_vpos)
13899 w->window_end_vpos = this_line_vpos;
13901 else if (w->window_end_vpos == this_line_vpos
13902 && this_line_vpos > 0)
13903 w->window_end_vpos = this_line_vpos - 1;
13904 w->window_end_valid = false;
13906 /* Update hint: No need to try to scroll in update_window. */
13907 w->desired_matrix->no_scrolling_p = true;
13909 #ifdef GLYPH_DEBUG
13910 *w->desired_matrix->method = 0;
13911 debug_method_add (w, "optimization 1");
13912 #endif
13913 #ifdef HAVE_WINDOW_SYSTEM
13914 update_window_fringes (w, false);
13915 #endif
13916 goto update;
13918 else
13919 goto cancel;
13921 else if (/* Cursor position hasn't changed. */
13922 PT == w->last_point
13923 /* Make sure the cursor was last displayed
13924 in this window. Otherwise we have to reposition it. */
13926 /* PXW: Must be converted to pixels, probably. */
13927 && 0 <= w->cursor.vpos
13928 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13930 if (!must_finish)
13932 do_pending_window_change (true);
13933 /* If selected_window changed, redisplay again. */
13934 if (WINDOWP (selected_window)
13935 && (w = XWINDOW (selected_window)) != sw)
13936 goto retry;
13938 /* We used to always goto end_of_redisplay here, but this
13939 isn't enough if we have a blinking cursor. */
13940 if (w->cursor_off_p == w->last_cursor_off_p)
13941 goto end_of_redisplay;
13943 goto update;
13945 /* If highlighting the region, or if the cursor is in the echo area,
13946 then we can't just move the cursor. */
13947 else if (NILP (Vshow_trailing_whitespace)
13948 && !cursor_in_echo_area)
13950 struct it it;
13951 struct glyph_row *row;
13953 /* Skip from tlbufpos to PT and see where it is. Note that
13954 PT may be in invisible text. If so, we will end at the
13955 next visible position. */
13956 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13957 NULL, DEFAULT_FACE_ID);
13958 it.current_x = this_line_start_x;
13959 it.current_y = this_line_y;
13960 it.vpos = this_line_vpos;
13962 /* The call to move_it_to stops in front of PT, but
13963 moves over before-strings. */
13964 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13966 if (it.vpos == this_line_vpos
13967 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13968 row->enabled_p))
13970 eassert (this_line_vpos == it.vpos);
13971 eassert (this_line_y == it.current_y);
13972 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13973 if (cursor_row_fully_visible_p (w, false, true))
13975 #ifdef GLYPH_DEBUG
13976 *w->desired_matrix->method = 0;
13977 debug_method_add (w, "optimization 3");
13978 #endif
13979 goto update;
13981 else
13982 goto cancel;
13984 else
13985 goto cancel;
13988 cancel:
13989 /* Text changed drastically or point moved off of line. */
13990 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13993 CHARPOS (this_line_start_pos) = 0;
13994 ++clear_face_cache_count;
13995 #ifdef HAVE_WINDOW_SYSTEM
13996 ++clear_image_cache_count;
13997 #endif
13999 /* Build desired matrices, and update the display. If
14000 consider_all_windows_p, do it for all windows on all frames that
14001 require redisplay, as specified by their 'redisplay' flag.
14002 Otherwise do it for selected_window, only. */
14004 if (consider_all_windows_p)
14006 FOR_EACH_FRAME (tail, frame)
14007 XFRAME (frame)->updated_p = false;
14009 propagate_buffer_redisplay ();
14011 FOR_EACH_FRAME (tail, frame)
14013 struct frame *f = XFRAME (frame);
14015 /* We don't have to do anything for unselected terminal
14016 frames. */
14017 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14018 && !EQ (FRAME_TTY (f)->top_frame, frame))
14019 continue;
14021 retry_frame:
14022 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14024 bool gcscrollbars
14025 /* Only GC scrollbars when we redisplay the whole frame. */
14026 = f->redisplay || !REDISPLAY_SOME_P ();
14027 bool f_redisplay_flag = f->redisplay;
14028 /* Mark all the scroll bars to be removed; we'll redeem
14029 the ones we want when we redisplay their windows. */
14030 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14031 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14033 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14034 redisplay_windows (FRAME_ROOT_WINDOW (f));
14035 /* Remember that the invisible frames need to be redisplayed next
14036 time they're visible. */
14037 else if (!REDISPLAY_SOME_P ())
14038 f->redisplay = true;
14040 /* The X error handler may have deleted that frame. */
14041 if (!FRAME_LIVE_P (f))
14042 continue;
14044 /* Any scroll bars which redisplay_windows should have
14045 nuked should now go away. */
14046 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14047 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14049 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14051 /* If fonts changed on visible frame, display again. */
14052 if (f->fonts_changed)
14054 adjust_frame_glyphs (f);
14055 /* Disable all redisplay optimizations for this
14056 frame. For the reasons, see the comment near
14057 the previous call to adjust_frame_glyphs above. */
14058 SET_FRAME_GARBAGED (f);
14059 f->fonts_changed = false;
14060 goto retry_frame;
14063 /* See if we have to hscroll. */
14064 if (!f->already_hscrolled_p)
14066 f->already_hscrolled_p = true;
14067 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14068 && hscroll_windows (f->root_window))
14070 hscroll_retries++;
14071 goto retry_frame;
14075 /* If the frame's redisplay flag was not set before
14076 we went about redisplaying its windows, but it is
14077 set now, that means we employed some redisplay
14078 optimizations inside redisplay_windows, and
14079 bypassed producing some screen lines. But if
14080 f->redisplay is now set, it might mean the old
14081 faces are no longer valid (e.g., if redisplaying
14082 some window called some Lisp which defined a new
14083 face or redefined an existing face), so trying to
14084 use them in update_frame will segfault.
14085 Therefore, we must redisplay this frame. */
14086 if (!f_redisplay_flag && f->redisplay)
14087 goto retry_frame;
14089 /* In some case (e.g., window resize), we notice
14090 only during window updating that the window
14091 content changed unpredictably (e.g., a GTK
14092 scrollbar moved) and that our previous estimation
14093 of the frame content was garbage. We have to
14094 start over. These cases should be rare, so going
14095 all the way back to the top of redisplay should
14096 be good enough.
14098 Why FRAME_WINDOW_P? See
14099 https://lists.gnu.org/archive/html/emacs-devel/2016-10/msg00957.html
14102 if (FRAME_GARBAGED_P (f) && FRAME_WINDOW_P (f))
14103 goto retry;
14105 /* Prevent various kinds of signals during display
14106 update. stdio is not robust about handling
14107 signals, which can cause an apparent I/O error. */
14108 if (interrupt_input)
14109 unrequest_sigio ();
14110 STOP_POLLING;
14112 pending |= update_frame (f, false, false);
14113 f->cursor_type_changed = false;
14114 f->updated_p = true;
14119 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14121 if (!pending)
14123 /* Do the mark_window_display_accurate after all windows have
14124 been redisplayed because this call resets flags in buffers
14125 which are needed for proper redisplay. */
14126 FOR_EACH_FRAME (tail, frame)
14128 struct frame *f = XFRAME (frame);
14129 if (f->updated_p)
14131 f->redisplay = false;
14132 f->garbaged = false;
14133 mark_window_display_accurate (f->root_window, true);
14134 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14135 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14140 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14142 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14143 /* Use list_of_error, not Qerror, so that
14144 we catch only errors and don't run the debugger. */
14145 internal_condition_case_1 (redisplay_window_1, selected_window,
14146 list_of_error,
14147 redisplay_window_error);
14148 if (update_miniwindow_p)
14149 internal_condition_case_1 (redisplay_window_1,
14150 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14151 redisplay_window_error);
14153 /* Compare desired and current matrices, perform output. */
14155 update:
14156 /* If fonts changed, display again. Likewise if redisplay_window_1
14157 above caused some change (e.g., a change in faces) that requires
14158 considering the entire frame again. */
14159 if (sf->fonts_changed || sf->redisplay)
14161 if (sf->redisplay)
14163 /* Set this to force a more thorough redisplay.
14164 Otherwise, we might immediately loop back to the
14165 above "else-if" clause (since all the conditions that
14166 led here might still be true), and we will then
14167 infloop, because the selected-frame's redisplay flag
14168 is not (and cannot be) reset. */
14169 windows_or_buffers_changed = 50;
14171 goto retry;
14174 /* Prevent freeing of realized faces, since desired matrices are
14175 pending that reference the faces we computed and cached. */
14176 inhibit_free_realized_faces = true;
14178 /* Prevent various kinds of signals during display update.
14179 stdio is not robust about handling signals,
14180 which can cause an apparent I/O error. */
14181 if (interrupt_input)
14182 unrequest_sigio ();
14183 STOP_POLLING;
14185 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14187 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14188 && hscroll_windows (selected_window))
14190 hscroll_retries++;
14191 goto retry;
14194 XWINDOW (selected_window)->must_be_updated_p = true;
14195 pending = update_frame (sf, false, false);
14196 sf->cursor_type_changed = false;
14199 /* We may have called echo_area_display at the top of this
14200 function. If the echo area is on another frame, that may
14201 have put text on a frame other than the selected one, so the
14202 above call to update_frame would not have caught it. Catch
14203 it here. */
14204 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14205 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14207 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14209 XWINDOW (mini_window)->must_be_updated_p = true;
14210 pending |= update_frame (mini_frame, false, false);
14211 mini_frame->cursor_type_changed = false;
14212 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14213 && hscroll_windows (mini_window))
14215 hscroll_retries++;
14216 goto retry;
14221 /* If display was paused because of pending input, make sure we do a
14222 thorough update the next time. */
14223 if (pending)
14225 /* Prevent the optimization at the beginning of
14226 redisplay_internal that tries a single-line update of the
14227 line containing the cursor in the selected window. */
14228 CHARPOS (this_line_start_pos) = 0;
14230 /* Let the overlay arrow be updated the next time. */
14231 update_overlay_arrows (0);
14233 /* If we pause after scrolling, some rows in the current
14234 matrices of some windows are not valid. */
14235 if (!WINDOW_FULL_WIDTH_P (w)
14236 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14237 update_mode_lines = 36;
14239 else
14241 if (!consider_all_windows_p)
14243 /* This has already been done above if
14244 consider_all_windows_p is set. */
14245 if (XBUFFER (w->contents)->text->redisplay
14246 && buffer_window_count (XBUFFER (w->contents)) > 1)
14247 /* This can happen if b->text->redisplay was set during
14248 jit-lock. */
14249 propagate_buffer_redisplay ();
14250 mark_window_display_accurate_1 (w, true);
14252 /* Say overlay arrows are up to date. */
14253 update_overlay_arrows (1);
14255 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14256 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14259 update_mode_lines = 0;
14260 windows_or_buffers_changed = 0;
14263 /* Start SIGIO interrupts coming again. Having them off during the
14264 code above makes it less likely one will discard output, but not
14265 impossible, since there might be stuff in the system buffer here.
14266 But it is much hairier to try to do anything about that. */
14267 if (interrupt_input)
14268 request_sigio ();
14269 RESUME_POLLING;
14271 /* If a frame has become visible which was not before, redisplay
14272 again, so that we display it. Expose events for such a frame
14273 (which it gets when becoming visible) don't call the parts of
14274 redisplay constructing glyphs, so simply exposing a frame won't
14275 display anything in this case. So, we have to display these
14276 frames here explicitly. */
14277 if (!pending)
14279 int new_count = 0;
14281 FOR_EACH_FRAME (tail, frame)
14283 if (XFRAME (frame)->visible)
14284 new_count++;
14287 if (new_count != number_of_visible_frames)
14288 windows_or_buffers_changed = 52;
14291 /* Change frame size now if a change is pending. */
14292 do_pending_window_change (true);
14294 /* If we just did a pending size change, or have additional
14295 visible frames, or selected_window changed, redisplay again. */
14296 if ((windows_or_buffers_changed && !pending)
14297 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14298 goto retry;
14300 /* Clear the face and image caches.
14302 We used to do this only if consider_all_windows_p. But the cache
14303 needs to be cleared if a timer creates images in the current
14304 buffer (e.g. the test case in Bug#6230). */
14306 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14308 clear_face_cache (false);
14309 clear_face_cache_count = 0;
14312 #ifdef HAVE_WINDOW_SYSTEM
14313 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14315 clear_image_caches (Qnil);
14316 clear_image_cache_count = 0;
14318 #endif /* HAVE_WINDOW_SYSTEM */
14320 end_of_redisplay:
14321 #ifdef HAVE_NS
14322 ns_set_doc_edited ();
14323 #endif
14324 if (interrupt_input && interrupts_deferred)
14325 request_sigio ();
14327 unbind_to (count, Qnil);
14328 RESUME_POLLING;
14331 static void
14332 unwind_redisplay_preserve_echo_area (void)
14334 unblock_buffer_flips ();
14337 /* Redisplay, but leave alone any recent echo area message unless
14338 another message has been requested in its place.
14340 This is useful in situations where you need to redisplay but no
14341 user action has occurred, making it inappropriate for the message
14342 area to be cleared. See tracking_off and
14343 wait_reading_process_output for examples of these situations.
14345 FROM_WHERE is an integer saying from where this function was
14346 called. This is useful for debugging. */
14348 void
14349 redisplay_preserve_echo_area (int from_where)
14351 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14353 block_input ();
14354 ptrdiff_t count = SPECPDL_INDEX ();
14355 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14356 block_buffer_flips ();
14357 unblock_input ();
14359 if (!NILP (echo_area_buffer[1]))
14361 /* We have a previously displayed message, but no current
14362 message. Redisplay the previous message. */
14363 display_last_displayed_message_p = true;
14364 redisplay_internal ();
14365 display_last_displayed_message_p = false;
14367 else
14368 redisplay_internal ();
14370 flush_frame (SELECTED_FRAME ());
14371 unbind_to (count, Qnil);
14375 /* Function registered with record_unwind_protect in redisplay_internal. */
14377 static void
14378 unwind_redisplay (void)
14380 redisplaying_p = false;
14381 unblock_buffer_flips ();
14385 /* Mark the display of leaf window W as accurate or inaccurate.
14386 If ACCURATE_P, mark display of W as accurate.
14387 If !ACCURATE_P, arrange for W to be redisplayed the next
14388 time redisplay_internal is called. */
14390 static void
14391 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14393 struct buffer *b = XBUFFER (w->contents);
14395 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14396 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14397 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14399 if (accurate_p)
14401 b->clip_changed = false;
14402 b->prevent_redisplay_optimizations_p = false;
14403 eassert (buffer_window_count (b) > 0);
14404 /* Resetting b->text->redisplay is problematic!
14405 In order to make it safer to do it here, redisplay_internal must
14406 have copied all b->text->redisplay to their respective windows. */
14407 b->text->redisplay = false;
14409 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14410 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14411 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14412 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14414 w->current_matrix->buffer = b;
14415 w->current_matrix->begv = BUF_BEGV (b);
14416 w->current_matrix->zv = BUF_ZV (b);
14418 w->last_cursor_vpos = w->cursor.vpos;
14419 w->last_cursor_off_p = w->cursor_off_p;
14421 if (w == XWINDOW (selected_window))
14422 w->last_point = BUF_PT (b);
14423 else
14424 w->last_point = marker_position (w->pointm);
14426 w->window_end_valid = true;
14427 w->update_mode_line = false;
14430 w->redisplay = !accurate_p;
14434 /* Mark the display of windows in the window tree rooted at WINDOW as
14435 accurate or inaccurate. If ACCURATE_P, mark display of
14436 windows as accurate. If !ACCURATE_P, arrange for windows to
14437 be redisplayed the next time redisplay_internal is called. */
14439 void
14440 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14442 struct window *w;
14444 for (; !NILP (window); window = w->next)
14446 w = XWINDOW (window);
14447 if (WINDOWP (w->contents))
14448 mark_window_display_accurate (w->contents, accurate_p);
14449 else
14450 mark_window_display_accurate_1 (w, accurate_p);
14453 if (accurate_p)
14454 update_overlay_arrows (1);
14455 else
14456 /* Force a thorough redisplay the next time by setting
14457 last_arrow_position and last_arrow_string to t, which is
14458 unequal to any useful value of Voverlay_arrow_... */
14459 update_overlay_arrows (-1);
14463 /* Return value in display table DP (Lisp_Char_Table *) for character
14464 C. Since a display table doesn't have any parent, we don't have to
14465 follow parent. Do not call this function directly but use the
14466 macro DISP_CHAR_VECTOR. */
14468 Lisp_Object
14469 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14471 Lisp_Object val;
14473 if (ASCII_CHAR_P (c))
14475 val = dp->ascii;
14476 if (SUB_CHAR_TABLE_P (val))
14477 val = XSUB_CHAR_TABLE (val)->contents[c];
14479 else
14481 Lisp_Object table;
14483 XSETCHAR_TABLE (table, dp);
14484 val = char_table_ref (table, c);
14486 if (NILP (val))
14487 val = dp->defalt;
14488 return val;
14491 static int buffer_flip_blocked_depth;
14493 static void
14494 block_buffer_flips (void)
14496 eassert (buffer_flip_blocked_depth >= 0);
14497 buffer_flip_blocked_depth++;
14500 static void
14501 unblock_buffer_flips (void)
14503 eassert (buffer_flip_blocked_depth > 0);
14504 if (--buffer_flip_blocked_depth == 0)
14506 Lisp_Object tail, frame;
14507 block_input ();
14508 FOR_EACH_FRAME (tail, frame)
14510 struct frame *f = XFRAME (frame);
14511 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14512 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14514 unblock_input ();
14518 bool
14519 buffer_flipping_blocked_p (void)
14521 return buffer_flip_blocked_depth > 0;
14525 /***********************************************************************
14526 Window Redisplay
14527 ***********************************************************************/
14529 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14531 static void
14532 redisplay_windows (Lisp_Object window)
14534 while (!NILP (window))
14536 struct window *w = XWINDOW (window);
14538 if (WINDOWP (w->contents))
14539 redisplay_windows (w->contents);
14540 else if (BUFFERP (w->contents))
14542 displayed_buffer = XBUFFER (w->contents);
14543 /* Use list_of_error, not Qerror, so that
14544 we catch only errors and don't run the debugger. */
14545 internal_condition_case_1 (redisplay_window_0, window,
14546 list_of_error,
14547 redisplay_window_error);
14550 window = w->next;
14554 static Lisp_Object
14555 redisplay_window_error (Lisp_Object ignore)
14557 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14558 return Qnil;
14561 static Lisp_Object
14562 redisplay_window_0 (Lisp_Object window)
14564 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14565 redisplay_window (window, false);
14566 return Qnil;
14569 static Lisp_Object
14570 redisplay_window_1 (Lisp_Object window)
14572 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14573 redisplay_window (window, true);
14574 return Qnil;
14578 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14579 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14580 which positions recorded in ROW differ from current buffer
14581 positions.
14583 Return true iff cursor is on this row. */
14585 static bool
14586 set_cursor_from_row (struct window *w, struct glyph_row *row,
14587 struct glyph_matrix *matrix,
14588 ptrdiff_t delta, ptrdiff_t delta_bytes,
14589 int dy, int dvpos)
14591 struct glyph *glyph = row->glyphs[TEXT_AREA];
14592 struct glyph *end = glyph + row->used[TEXT_AREA];
14593 struct glyph *cursor = NULL;
14594 /* The last known character position in row. */
14595 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14596 int x = row->x;
14597 ptrdiff_t pt_old = PT - delta;
14598 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14599 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14600 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14601 /* A glyph beyond the edge of TEXT_AREA which we should never
14602 touch. */
14603 struct glyph *glyphs_end = end;
14604 /* True means we've found a match for cursor position, but that
14605 glyph has the avoid_cursor_p flag set. */
14606 bool match_with_avoid_cursor = false;
14607 /* True means we've seen at least one glyph that came from a
14608 display string. */
14609 bool string_seen = false;
14610 /* Largest and smallest buffer positions seen so far during scan of
14611 glyph row. */
14612 ptrdiff_t bpos_max = pos_before;
14613 ptrdiff_t bpos_min = pos_after;
14614 /* Last buffer position covered by an overlay string with an integer
14615 `cursor' property. */
14616 ptrdiff_t bpos_covered = 0;
14617 /* True means the display string on which to display the cursor
14618 comes from a text property, not from an overlay. */
14619 bool string_from_text_prop = false;
14621 /* Don't even try doing anything if called for a mode-line or
14622 header-line row, since the rest of the code isn't prepared to
14623 deal with such calamities. */
14624 eassert (!row->mode_line_p);
14625 if (row->mode_line_p)
14626 return false;
14628 /* Skip over glyphs not having an object at the start and the end of
14629 the row. These are special glyphs like truncation marks on
14630 terminal frames. */
14631 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14633 if (!row->reversed_p)
14635 while (glyph < end
14636 && NILP (glyph->object)
14637 && glyph->charpos < 0)
14639 x += glyph->pixel_width;
14640 ++glyph;
14642 while (end > glyph
14643 && NILP ((end - 1)->object)
14644 /* CHARPOS is zero for blanks and stretch glyphs
14645 inserted by extend_face_to_end_of_line. */
14646 && (end - 1)->charpos <= 0)
14647 --end;
14648 glyph_before = glyph - 1;
14649 glyph_after = end;
14651 else
14653 struct glyph *g;
14655 /* If the glyph row is reversed, we need to process it from back
14656 to front, so swap the edge pointers. */
14657 glyphs_end = end = glyph - 1;
14658 glyph += row->used[TEXT_AREA] - 1;
14660 while (glyph > end + 1
14661 && NILP (glyph->object)
14662 && glyph->charpos < 0)
14664 --glyph;
14665 x -= glyph->pixel_width;
14667 if (NILP (glyph->object) && glyph->charpos < 0)
14668 --glyph;
14669 /* By default, in reversed rows we put the cursor on the
14670 rightmost (first in the reading order) glyph. */
14671 for (g = end + 1; g < glyph; g++)
14672 x += g->pixel_width;
14673 while (end < glyph
14674 && NILP ((end + 1)->object)
14675 && (end + 1)->charpos <= 0)
14676 ++end;
14677 glyph_before = glyph + 1;
14678 glyph_after = end;
14681 else if (row->reversed_p)
14683 /* In R2L rows that don't display text, put the cursor on the
14684 rightmost glyph. Case in point: an empty last line that is
14685 part of an R2L paragraph. */
14686 cursor = end - 1;
14687 /* Avoid placing the cursor on the last glyph of the row, where
14688 on terminal frames we hold the vertical border between
14689 adjacent windows. */
14690 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14691 && !WINDOW_RIGHTMOST_P (w)
14692 && cursor == row->glyphs[LAST_AREA] - 1)
14693 cursor--;
14694 x = -1; /* will be computed below, at label compute_x */
14697 /* Step 1: Try to find the glyph whose character position
14698 corresponds to point. If that's not possible, find 2 glyphs
14699 whose character positions are the closest to point, one before
14700 point, the other after it. */
14701 if (!row->reversed_p)
14702 while (/* not marched to end of glyph row */
14703 glyph < end
14704 /* glyph was not inserted by redisplay for internal purposes */
14705 && !NILP (glyph->object))
14707 if (BUFFERP (glyph->object))
14709 ptrdiff_t dpos = glyph->charpos - pt_old;
14711 if (glyph->charpos > bpos_max)
14712 bpos_max = glyph->charpos;
14713 if (glyph->charpos < bpos_min)
14714 bpos_min = glyph->charpos;
14715 if (!glyph->avoid_cursor_p)
14717 /* If we hit point, we've found the glyph on which to
14718 display the cursor. */
14719 if (dpos == 0)
14721 match_with_avoid_cursor = false;
14722 break;
14724 /* See if we've found a better approximation to
14725 POS_BEFORE or to POS_AFTER. */
14726 if (0 > dpos && dpos > pos_before - pt_old)
14728 pos_before = glyph->charpos;
14729 glyph_before = glyph;
14731 else if (0 < dpos && dpos < pos_after - pt_old)
14733 pos_after = glyph->charpos;
14734 glyph_after = glyph;
14737 else if (dpos == 0)
14738 match_with_avoid_cursor = true;
14740 else if (STRINGP (glyph->object))
14742 Lisp_Object chprop;
14743 ptrdiff_t glyph_pos = glyph->charpos;
14745 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14746 glyph->object);
14747 if (!NILP (chprop))
14749 /* If the string came from a `display' text property,
14750 look up the buffer position of that property and
14751 use that position to update bpos_max, as if we
14752 actually saw such a position in one of the row's
14753 glyphs. This helps with supporting integer values
14754 of `cursor' property on the display string in
14755 situations where most or all of the row's buffer
14756 text is completely covered by display properties,
14757 so that no glyph with valid buffer positions is
14758 ever seen in the row. */
14759 ptrdiff_t prop_pos =
14760 string_buffer_position_lim (glyph->object, pos_before,
14761 pos_after, false);
14763 if (prop_pos >= pos_before)
14764 bpos_max = prop_pos;
14766 if (INTEGERP (chprop))
14768 bpos_covered = bpos_max + XINT (chprop);
14769 /* If the `cursor' property covers buffer positions up
14770 to and including point, we should display cursor on
14771 this glyph. Note that, if a `cursor' property on one
14772 of the string's characters has an integer value, we
14773 will break out of the loop below _before_ we get to
14774 the position match above. IOW, integer values of
14775 the `cursor' property override the "exact match for
14776 point" strategy of positioning the cursor. */
14777 /* Implementation note: bpos_max == pt_old when, e.g.,
14778 we are in an empty line, where bpos_max is set to
14779 MATRIX_ROW_START_CHARPOS, see above. */
14780 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14782 cursor = glyph;
14783 break;
14787 string_seen = true;
14789 x += glyph->pixel_width;
14790 ++glyph;
14792 else if (glyph > end) /* row is reversed */
14793 while (!NILP (glyph->object))
14795 if (BUFFERP (glyph->object))
14797 ptrdiff_t dpos = glyph->charpos - pt_old;
14799 if (glyph->charpos > bpos_max)
14800 bpos_max = glyph->charpos;
14801 if (glyph->charpos < bpos_min)
14802 bpos_min = glyph->charpos;
14803 if (!glyph->avoid_cursor_p)
14805 if (dpos == 0)
14807 match_with_avoid_cursor = false;
14808 break;
14810 if (0 > dpos && dpos > pos_before - pt_old)
14812 pos_before = glyph->charpos;
14813 glyph_before = glyph;
14815 else if (0 < dpos && dpos < pos_after - pt_old)
14817 pos_after = glyph->charpos;
14818 glyph_after = glyph;
14821 else if (dpos == 0)
14822 match_with_avoid_cursor = true;
14824 else if (STRINGP (glyph->object))
14826 Lisp_Object chprop;
14827 ptrdiff_t glyph_pos = glyph->charpos;
14829 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14830 glyph->object);
14831 if (!NILP (chprop))
14833 ptrdiff_t prop_pos =
14834 string_buffer_position_lim (glyph->object, pos_before,
14835 pos_after, false);
14837 if (prop_pos >= pos_before)
14838 bpos_max = prop_pos;
14840 if (INTEGERP (chprop))
14842 bpos_covered = bpos_max + XINT (chprop);
14843 /* If the `cursor' property covers buffer positions up
14844 to and including point, we should display cursor on
14845 this glyph. */
14846 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14848 cursor = glyph;
14849 break;
14852 string_seen = true;
14854 --glyph;
14855 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14857 x--; /* can't use any pixel_width */
14858 break;
14860 x -= glyph->pixel_width;
14863 /* Step 2: If we didn't find an exact match for point, we need to
14864 look for a proper place to put the cursor among glyphs between
14865 GLYPH_BEFORE and GLYPH_AFTER. */
14866 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14867 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14868 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14870 /* An empty line has a single glyph whose OBJECT is nil and
14871 whose CHARPOS is the position of a newline on that line.
14872 Note that on a TTY, there are more glyphs after that, which
14873 were produced by extend_face_to_end_of_line, but their
14874 CHARPOS is zero or negative. */
14875 bool empty_line_p =
14876 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14877 && NILP (glyph->object) && glyph->charpos > 0
14878 /* On a TTY, continued and truncated rows also have a glyph at
14879 their end whose OBJECT is nil and whose CHARPOS is
14880 positive (the continuation and truncation glyphs), but such
14881 rows are obviously not "empty". */
14882 && !(row->continued_p || row->truncated_on_right_p));
14884 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14886 ptrdiff_t ellipsis_pos;
14888 /* Scan back over the ellipsis glyphs. */
14889 if (!row->reversed_p)
14891 ellipsis_pos = (glyph - 1)->charpos;
14892 while (glyph > row->glyphs[TEXT_AREA]
14893 && (glyph - 1)->charpos == ellipsis_pos)
14894 glyph--, x -= glyph->pixel_width;
14895 /* That loop always goes one position too far, including
14896 the glyph before the ellipsis. So scan forward over
14897 that one. */
14898 x += glyph->pixel_width;
14899 glyph++;
14901 else /* row is reversed */
14903 ellipsis_pos = (glyph + 1)->charpos;
14904 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14905 && (glyph + 1)->charpos == ellipsis_pos)
14906 glyph++, x += glyph->pixel_width;
14907 x -= glyph->pixel_width;
14908 glyph--;
14911 else if (match_with_avoid_cursor)
14913 cursor = glyph_after;
14914 x = -1;
14916 else if (string_seen)
14918 int incr = row->reversed_p ? -1 : +1;
14920 /* Need to find the glyph that came out of a string which is
14921 present at point. That glyph is somewhere between
14922 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14923 positioned between POS_BEFORE and POS_AFTER in the
14924 buffer. */
14925 struct glyph *start, *stop;
14926 ptrdiff_t pos = pos_before;
14928 x = -1;
14930 /* If the row ends in a newline from a display string,
14931 reordering could have moved the glyphs belonging to the
14932 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14933 in this case we extend the search to the last glyph in
14934 the row that was not inserted by redisplay. */
14935 if (row->ends_in_newline_from_string_p)
14937 glyph_after = end;
14938 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14941 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14942 correspond to POS_BEFORE and POS_AFTER, respectively. We
14943 need START and STOP in the order that corresponds to the
14944 row's direction as given by its reversed_p flag. If the
14945 directionality of characters between POS_BEFORE and
14946 POS_AFTER is the opposite of the row's base direction,
14947 these characters will have been reordered for display,
14948 and we need to reverse START and STOP. */
14949 if (!row->reversed_p)
14951 start = min (glyph_before, glyph_after);
14952 stop = max (glyph_before, glyph_after);
14954 else
14956 start = max (glyph_before, glyph_after);
14957 stop = min (glyph_before, glyph_after);
14959 for (glyph = start + incr;
14960 row->reversed_p ? glyph > stop : glyph < stop; )
14963 /* Any glyphs that come from the buffer are here because
14964 of bidi reordering. Skip them, and only pay
14965 attention to glyphs that came from some string. */
14966 if (STRINGP (glyph->object))
14968 Lisp_Object str;
14969 ptrdiff_t tem;
14970 /* If the display property covers the newline, we
14971 need to search for it one position farther. */
14972 ptrdiff_t lim = pos_after
14973 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14975 string_from_text_prop = false;
14976 str = glyph->object;
14977 tem = string_buffer_position_lim (str, pos, lim, false);
14978 if (tem == 0 /* from overlay */
14979 || pos <= tem)
14981 /* If the string from which this glyph came is
14982 found in the buffer at point, or at position
14983 that is closer to point than pos_after, then
14984 we've found the glyph we've been looking for.
14985 If it comes from an overlay (tem == 0), and
14986 it has the `cursor' property on one of its
14987 glyphs, record that glyph as a candidate for
14988 displaying the cursor. (As in the
14989 unidirectional version, we will display the
14990 cursor on the last candidate we find.) */
14991 if (tem == 0
14992 || tem == pt_old
14993 || (tem - pt_old > 0 && tem < pos_after))
14995 /* The glyphs from this string could have
14996 been reordered. Find the one with the
14997 smallest string position. Or there could
14998 be a character in the string with the
14999 `cursor' property, which means display
15000 cursor on that character's glyph. */
15001 ptrdiff_t strpos = glyph->charpos;
15003 if (tem)
15005 cursor = glyph;
15006 string_from_text_prop = true;
15008 for ( ;
15009 (row->reversed_p ? glyph > stop : glyph < stop)
15010 && EQ (glyph->object, str);
15011 glyph += incr)
15013 Lisp_Object cprop;
15014 ptrdiff_t gpos = glyph->charpos;
15016 cprop = Fget_char_property (make_number (gpos),
15017 Qcursor,
15018 glyph->object);
15019 if (!NILP (cprop))
15021 cursor = glyph;
15022 break;
15024 if (tem && glyph->charpos < strpos)
15026 strpos = glyph->charpos;
15027 cursor = glyph;
15031 if (tem == pt_old
15032 || (tem - pt_old > 0 && tem < pos_after))
15033 goto compute_x;
15035 if (tem)
15036 pos = tem + 1; /* don't find previous instances */
15038 /* This string is not what we want; skip all of the
15039 glyphs that came from it. */
15040 while ((row->reversed_p ? glyph > stop : glyph < stop)
15041 && EQ (glyph->object, str))
15042 glyph += incr;
15044 else
15045 glyph += incr;
15048 /* If we reached the end of the line, and END was from a string,
15049 the cursor is not on this line. */
15050 if (cursor == NULL
15051 && (row->reversed_p ? glyph <= end : glyph >= end)
15052 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15053 && STRINGP (end->object)
15054 && row->continued_p)
15055 return false;
15057 /* A truncated row may not include PT among its character positions.
15058 Setting the cursor inside the scroll margin will trigger
15059 recalculation of hscroll in hscroll_window_tree. But if a
15060 display string covers point, defer to the string-handling
15061 code below to figure this out. */
15062 else if (row->truncated_on_left_p && pt_old < bpos_min)
15064 cursor = glyph_before;
15065 x = -1;
15067 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15068 /* Zero-width characters produce no glyphs. */
15069 || (!empty_line_p
15070 && (row->reversed_p
15071 ? glyph_after > glyphs_end
15072 : glyph_after < glyphs_end)))
15074 cursor = glyph_after;
15075 x = -1;
15079 compute_x:
15080 if (cursor != NULL)
15081 glyph = cursor;
15082 else if (glyph == glyphs_end
15083 && pos_before == pos_after
15084 && STRINGP ((row->reversed_p
15085 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15086 : row->glyphs[TEXT_AREA])->object))
15088 /* If all the glyphs of this row came from strings, put the
15089 cursor on the first glyph of the row. This avoids having the
15090 cursor outside of the text area in this very rare and hard
15091 use case. */
15092 glyph =
15093 row->reversed_p
15094 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15095 : row->glyphs[TEXT_AREA];
15097 if (x < 0)
15099 struct glyph *g;
15101 /* Need to compute x that corresponds to GLYPH. */
15102 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15104 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15105 emacs_abort ();
15106 x += g->pixel_width;
15110 /* ROW could be part of a continued line, which, under bidi
15111 reordering, might have other rows whose start and end charpos
15112 occlude point. Only set w->cursor if we found a better
15113 approximation to the cursor position than we have from previously
15114 examined candidate rows belonging to the same continued line. */
15115 if (/* We already have a candidate row. */
15116 w->cursor.vpos >= 0
15117 /* That candidate is not the row we are processing. */
15118 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15119 /* Make sure cursor.vpos specifies a row whose start and end
15120 charpos occlude point, and it is valid candidate for being a
15121 cursor-row. This is because some callers of this function
15122 leave cursor.vpos at the row where the cursor was displayed
15123 during the last redisplay cycle. */
15124 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15125 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15126 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15128 struct glyph *g1
15129 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15131 /* Don't consider glyphs that are outside TEXT_AREA. */
15132 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15133 return false;
15134 /* Keep the candidate whose buffer position is the closest to
15135 point or has the `cursor' property. */
15136 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15137 w->cursor.hpos >= 0
15138 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15139 && ((BUFFERP (g1->object)
15140 && (g1->charpos == pt_old /* An exact match always wins. */
15141 || (BUFFERP (glyph->object)
15142 && eabs (g1->charpos - pt_old)
15143 < eabs (glyph->charpos - pt_old))))
15144 /* Previous candidate is a glyph from a string that has
15145 a non-nil `cursor' property. */
15146 || (STRINGP (g1->object)
15147 && (!NILP (Fget_char_property (make_number (g1->charpos),
15148 Qcursor, g1->object))
15149 /* Previous candidate is from the same display
15150 string as this one, and the display string
15151 came from a text property. */
15152 || (EQ (g1->object, glyph->object)
15153 && string_from_text_prop)
15154 /* this candidate is from newline and its
15155 position is not an exact match */
15156 || (NILP (glyph->object)
15157 && glyph->charpos != pt_old)))))
15158 return false;
15159 /* If this candidate gives an exact match, use that. */
15160 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15161 /* If this candidate is a glyph created for the
15162 terminating newline of a line, and point is on that
15163 newline, it wins because it's an exact match. */
15164 || (!row->continued_p
15165 && NILP (glyph->object)
15166 && glyph->charpos == 0
15167 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15168 /* Otherwise, keep the candidate that comes from a row
15169 spanning less buffer positions. This may win when one or
15170 both candidate positions are on glyphs that came from
15171 display strings, for which we cannot compare buffer
15172 positions. */
15173 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15174 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15175 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15176 return false;
15178 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15179 w->cursor.x = x;
15180 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15181 w->cursor.y = row->y + dy;
15183 if (w == XWINDOW (selected_window))
15185 if (!row->continued_p
15186 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15187 && row->x == 0)
15189 this_line_buffer = XBUFFER (w->contents);
15191 CHARPOS (this_line_start_pos)
15192 = MATRIX_ROW_START_CHARPOS (row) + delta;
15193 BYTEPOS (this_line_start_pos)
15194 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15196 CHARPOS (this_line_end_pos)
15197 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15198 BYTEPOS (this_line_end_pos)
15199 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15201 this_line_y = w->cursor.y;
15202 this_line_pixel_height = row->height;
15203 this_line_vpos = w->cursor.vpos;
15204 this_line_start_x = row->x;
15206 else
15207 CHARPOS (this_line_start_pos) = 0;
15210 return true;
15214 /* Run window scroll functions, if any, for WINDOW with new window
15215 start STARTP. Sets the window start of WINDOW to that position.
15217 We assume that the window's buffer is really current. */
15219 static struct text_pos
15220 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15222 struct window *w = XWINDOW (window);
15223 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15225 eassert (current_buffer == XBUFFER (w->contents));
15227 if (!NILP (Vwindow_scroll_functions))
15229 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15230 make_number (CHARPOS (startp)));
15231 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15232 /* In case the hook functions switch buffers. */
15233 set_buffer_internal (XBUFFER (w->contents));
15236 return startp;
15240 /* Make sure the line containing the cursor is fully visible.
15241 A value of true means there is nothing to be done.
15242 (Either the line is fully visible, or it cannot be made so,
15243 or we cannot tell.)
15245 If FORCE_P, return false even if partial visible cursor row
15246 is higher than window.
15248 If CURRENT_MATRIX_P, use the information from the
15249 window's current glyph matrix; otherwise use the desired glyph
15250 matrix.
15252 A value of false means the caller should do scrolling
15253 as if point had gone off the screen. */
15255 static bool
15256 cursor_row_fully_visible_p (struct window *w, bool force_p,
15257 bool current_matrix_p)
15259 struct glyph_matrix *matrix;
15260 struct glyph_row *row;
15261 int window_height;
15263 if (!make_cursor_line_fully_visible_p)
15264 return true;
15266 /* It's not always possible to find the cursor, e.g, when a window
15267 is full of overlay strings. Don't do anything in that case. */
15268 if (w->cursor.vpos < 0)
15269 return true;
15271 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15272 row = MATRIX_ROW (matrix, w->cursor.vpos);
15274 /* If the cursor row is not partially visible, there's nothing to do. */
15275 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15276 return true;
15278 /* If the row the cursor is in is taller than the window's height,
15279 it's not clear what to do, so do nothing. */
15280 window_height = window_box_height (w);
15281 if (row->height >= window_height)
15283 if (!force_p || MINI_WINDOW_P (w)
15284 || w->vscroll || w->cursor.vpos == 0)
15285 return true;
15287 return false;
15291 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15292 means only WINDOW is redisplayed in redisplay_internal.
15293 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15294 in redisplay_window to bring a partially visible line into view in
15295 the case that only the cursor has moved.
15297 LAST_LINE_MISFIT should be true if we're scrolling because the
15298 last screen line's vertical height extends past the end of the screen.
15300 Value is
15302 1 if scrolling succeeded
15304 0 if scrolling didn't find point.
15306 -1 if new fonts have been loaded so that we must interrupt
15307 redisplay, adjust glyph matrices, and try again. */
15309 enum
15311 SCROLLING_SUCCESS,
15312 SCROLLING_FAILED,
15313 SCROLLING_NEED_LARGER_MATRICES
15316 /* If scroll-conservatively is more than this, never recenter.
15318 If you change this, don't forget to update the doc string of
15319 `scroll-conservatively' and the Emacs manual. */
15320 #define SCROLL_LIMIT 100
15322 static int
15323 try_scrolling (Lisp_Object window, bool just_this_one_p,
15324 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15325 bool temp_scroll_step, bool last_line_misfit)
15327 struct window *w = XWINDOW (window);
15328 struct frame *f = XFRAME (w->frame);
15329 struct text_pos pos, startp;
15330 struct it it;
15331 int this_scroll_margin, scroll_max, rc, height;
15332 int dy = 0, amount_to_scroll = 0;
15333 bool scroll_down_p = false;
15334 int extra_scroll_margin_lines = last_line_misfit;
15335 Lisp_Object aggressive;
15336 /* We will never try scrolling more than this number of lines. */
15337 int scroll_limit = SCROLL_LIMIT;
15338 int frame_line_height = default_line_pixel_height (w);
15339 int window_total_lines
15340 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15342 #ifdef GLYPH_DEBUG
15343 debug_method_add (w, "try_scrolling");
15344 #endif
15346 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15348 /* Compute scroll margin height in pixels. We scroll when point is
15349 within this distance from the top or bottom of the window. */
15350 if (scroll_margin > 0)
15351 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15352 * frame_line_height;
15353 else
15354 this_scroll_margin = 0;
15356 /* Force arg_scroll_conservatively to have a reasonable value, to
15357 avoid scrolling too far away with slow move_it_* functions. Note
15358 that the user can supply scroll-conservatively equal to
15359 `most-positive-fixnum', which can be larger than INT_MAX. */
15360 if (arg_scroll_conservatively > scroll_limit)
15362 arg_scroll_conservatively = scroll_limit + 1;
15363 scroll_max = scroll_limit * frame_line_height;
15365 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15366 /* Compute how much we should try to scroll maximally to bring
15367 point into view. */
15368 scroll_max = (max (scroll_step,
15369 max (arg_scroll_conservatively, temp_scroll_step))
15370 * frame_line_height);
15371 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15372 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15373 /* We're trying to scroll because of aggressive scrolling but no
15374 scroll_step is set. Choose an arbitrary one. */
15375 scroll_max = 10 * frame_line_height;
15376 else
15377 scroll_max = 0;
15379 too_near_end:
15381 /* Decide whether to scroll down. */
15382 if (PT > CHARPOS (startp))
15384 int scroll_margin_y;
15386 /* Compute the pixel ypos of the scroll margin, then move IT to
15387 either that ypos or PT, whichever comes first. */
15388 start_display (&it, w, startp);
15389 scroll_margin_y = it.last_visible_y - this_scroll_margin
15390 - frame_line_height * extra_scroll_margin_lines;
15391 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15392 (MOVE_TO_POS | MOVE_TO_Y));
15394 if (PT > CHARPOS (it.current.pos))
15396 int y0 = line_bottom_y (&it);
15397 /* Compute how many pixels below window bottom to stop searching
15398 for PT. This avoids costly search for PT that is far away if
15399 the user limited scrolling by a small number of lines, but
15400 always finds PT if scroll_conservatively is set to a large
15401 number, such as most-positive-fixnum. */
15402 int slack = max (scroll_max, 10 * frame_line_height);
15403 int y_to_move = it.last_visible_y + slack;
15405 /* Compute the distance from the scroll margin to PT or to
15406 the scroll limit, whichever comes first. This should
15407 include the height of the cursor line, to make that line
15408 fully visible. */
15409 move_it_to (&it, PT, -1, y_to_move,
15410 -1, MOVE_TO_POS | MOVE_TO_Y);
15411 dy = line_bottom_y (&it) - y0;
15413 if (dy > scroll_max)
15414 return SCROLLING_FAILED;
15416 if (dy > 0)
15417 scroll_down_p = true;
15419 else if (PT == IT_CHARPOS (it)
15420 && IT_CHARPOS (it) < ZV
15421 && it.method == GET_FROM_STRING
15422 && arg_scroll_conservatively > scroll_limit
15423 && it.current_x == 0)
15425 enum move_it_result skip;
15426 int y1 = it.current_y;
15427 int vpos;
15429 /* A before-string that includes newlines and is displayed
15430 on the last visible screen line could fail us under
15431 scroll-conservatively > 100, because we will be unable to
15432 position the cursor on that last visible line. Try to
15433 recover by finding the first screen line that has some
15434 glyphs coming from the buffer text. */
15435 do {
15436 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15437 if (skip != MOVE_NEWLINE_OR_CR
15438 || IT_CHARPOS (it) != PT
15439 || it.method == GET_FROM_BUFFER)
15440 break;
15441 vpos = it.vpos;
15442 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15443 } while (it.vpos > vpos);
15445 dy = it.current_y - y1;
15447 if (dy > scroll_max)
15448 return SCROLLING_FAILED;
15450 if (dy > 0)
15451 scroll_down_p = true;
15455 if (scroll_down_p)
15457 /* Point is in or below the bottom scroll margin, so move the
15458 window start down. If scrolling conservatively, move it just
15459 enough down to make point visible. If scroll_step is set,
15460 move it down by scroll_step. */
15461 if (arg_scroll_conservatively)
15462 amount_to_scroll
15463 = min (max (dy, frame_line_height),
15464 frame_line_height * arg_scroll_conservatively);
15465 else if (scroll_step || temp_scroll_step)
15466 amount_to_scroll = scroll_max;
15467 else
15469 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15470 height = WINDOW_BOX_TEXT_HEIGHT (w);
15471 if (NUMBERP (aggressive))
15473 double float_amount = XFLOATINT (aggressive) * height;
15474 int aggressive_scroll = float_amount;
15475 if (aggressive_scroll == 0 && float_amount > 0)
15476 aggressive_scroll = 1;
15477 /* Don't let point enter the scroll margin near top of
15478 the window. This could happen if the value of
15479 scroll_up_aggressively is too large and there are
15480 non-zero margins, because scroll_up_aggressively
15481 means put point that fraction of window height
15482 _from_the_bottom_margin_. */
15483 if (aggressive_scroll + 2 * this_scroll_margin > height)
15484 aggressive_scroll = height - 2 * this_scroll_margin;
15485 amount_to_scroll = dy + aggressive_scroll;
15489 if (amount_to_scroll <= 0)
15490 return SCROLLING_FAILED;
15492 start_display (&it, w, startp);
15493 if (arg_scroll_conservatively <= scroll_limit)
15494 move_it_vertically (&it, amount_to_scroll);
15495 else
15497 /* Extra precision for users who set scroll-conservatively
15498 to a large number: make sure the amount we scroll
15499 the window start is never less than amount_to_scroll,
15500 which was computed as distance from window bottom to
15501 point. This matters when lines at window top and lines
15502 below window bottom have different height. */
15503 struct it it1;
15504 void *it1data = NULL;
15505 /* We use a temporary it1 because line_bottom_y can modify
15506 its argument, if it moves one line down; see there. */
15507 int start_y;
15509 SAVE_IT (it1, it, it1data);
15510 start_y = line_bottom_y (&it1);
15511 do {
15512 RESTORE_IT (&it, &it, it1data);
15513 move_it_by_lines (&it, 1);
15514 SAVE_IT (it1, it, it1data);
15515 } while (IT_CHARPOS (it) < ZV
15516 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15517 bidi_unshelve_cache (it1data, true);
15520 /* If STARTP is unchanged, move it down another screen line. */
15521 if (IT_CHARPOS (it) == CHARPOS (startp))
15522 move_it_by_lines (&it, 1);
15523 startp = it.current.pos;
15525 else
15527 struct text_pos scroll_margin_pos = startp;
15528 int y_offset = 0;
15530 /* See if point is inside the scroll margin at the top of the
15531 window. */
15532 if (this_scroll_margin)
15534 int y_start;
15536 start_display (&it, w, startp);
15537 y_start = it.current_y;
15538 move_it_vertically (&it, this_scroll_margin);
15539 scroll_margin_pos = it.current.pos;
15540 /* If we didn't move enough before hitting ZV, request
15541 additional amount of scroll, to move point out of the
15542 scroll margin. */
15543 if (IT_CHARPOS (it) == ZV
15544 && it.current_y - y_start < this_scroll_margin)
15545 y_offset = this_scroll_margin - (it.current_y - y_start);
15548 if (PT < CHARPOS (scroll_margin_pos))
15550 /* Point is in the scroll margin at the top of the window or
15551 above what is displayed in the window. */
15552 int y0, y_to_move;
15554 /* Compute the vertical distance from PT to the scroll
15555 margin position. Move as far as scroll_max allows, or
15556 one screenful, or 10 screen lines, whichever is largest.
15557 Give up if distance is greater than scroll_max or if we
15558 didn't reach the scroll margin position. */
15559 SET_TEXT_POS (pos, PT, PT_BYTE);
15560 start_display (&it, w, pos);
15561 y0 = it.current_y;
15562 y_to_move = max (it.last_visible_y,
15563 max (scroll_max, 10 * frame_line_height));
15564 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15565 y_to_move, -1,
15566 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15567 dy = it.current_y - y0;
15568 if (dy > scroll_max
15569 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15570 return SCROLLING_FAILED;
15572 /* Additional scroll for when ZV was too close to point. */
15573 dy += y_offset;
15575 /* Compute new window start. */
15576 start_display (&it, w, startp);
15578 if (arg_scroll_conservatively)
15579 amount_to_scroll = max (dy, frame_line_height
15580 * max (scroll_step, temp_scroll_step));
15581 else if (scroll_step || temp_scroll_step)
15582 amount_to_scroll = scroll_max;
15583 else
15585 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15586 height = WINDOW_BOX_TEXT_HEIGHT (w);
15587 if (NUMBERP (aggressive))
15589 double float_amount = XFLOATINT (aggressive) * height;
15590 int aggressive_scroll = float_amount;
15591 if (aggressive_scroll == 0 && float_amount > 0)
15592 aggressive_scroll = 1;
15593 /* Don't let point enter the scroll margin near
15594 bottom of the window, if the value of
15595 scroll_down_aggressively happens to be too
15596 large. */
15597 if (aggressive_scroll + 2 * this_scroll_margin > height)
15598 aggressive_scroll = height - 2 * this_scroll_margin;
15599 amount_to_scroll = dy + aggressive_scroll;
15603 if (amount_to_scroll <= 0)
15604 return SCROLLING_FAILED;
15606 move_it_vertically_backward (&it, amount_to_scroll);
15607 startp = it.current.pos;
15611 /* Run window scroll functions. */
15612 startp = run_window_scroll_functions (window, startp);
15614 /* Display the window. Give up if new fonts are loaded, or if point
15615 doesn't appear. */
15616 if (!try_window (window, startp, 0))
15617 rc = SCROLLING_NEED_LARGER_MATRICES;
15618 else if (w->cursor.vpos < 0)
15620 clear_glyph_matrix (w->desired_matrix);
15621 rc = SCROLLING_FAILED;
15623 else
15625 /* Maybe forget recorded base line for line number display. */
15626 if (!just_this_one_p
15627 || current_buffer->clip_changed
15628 || BEG_UNCHANGED < CHARPOS (startp))
15629 w->base_line_number = 0;
15631 /* If cursor ends up on a partially visible line,
15632 treat that as being off the bottom of the screen. */
15633 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15634 false)
15635 /* It's possible that the cursor is on the first line of the
15636 buffer, which is partially obscured due to a vscroll
15637 (Bug#7537). In that case, avoid looping forever. */
15638 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15640 clear_glyph_matrix (w->desired_matrix);
15641 ++extra_scroll_margin_lines;
15642 goto too_near_end;
15644 rc = SCROLLING_SUCCESS;
15647 return rc;
15651 /* Compute a suitable window start for window W if display of W starts
15652 on a continuation line. Value is true if a new window start
15653 was computed.
15655 The new window start will be computed, based on W's width, starting
15656 from the start of the continued line. It is the start of the
15657 screen line with the minimum distance from the old start W->start,
15658 which is still before point (otherwise point will definitely not
15659 be visible in the window). */
15661 static bool
15662 compute_window_start_on_continuation_line (struct window *w)
15664 struct text_pos pos, start_pos, pos_before_pt;
15665 bool window_start_changed_p = false;
15667 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15669 /* If window start is on a continuation line... Window start may be
15670 < BEGV in case there's invisible text at the start of the
15671 buffer (M-x rmail, for example). */
15672 if (CHARPOS (start_pos) > BEGV
15673 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15675 struct it it;
15676 struct glyph_row *row;
15678 /* Handle the case that the window start is out of range. */
15679 if (CHARPOS (start_pos) < BEGV)
15680 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15681 else if (CHARPOS (start_pos) > ZV)
15682 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15684 /* Find the start of the continued line. This should be fast
15685 because find_newline is fast (newline cache). */
15686 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15687 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15688 row, DEFAULT_FACE_ID);
15689 reseat_at_previous_visible_line_start (&it);
15691 /* If the line start is "too far" away from the window start,
15692 say it takes too much time to compute a new window start.
15693 Also, give up if the line start is after point, as in that
15694 case point will not be visible with any window start we
15695 compute. */
15696 if (IT_CHARPOS (it) <= PT
15697 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15698 /* PXW: Do we need upper bounds here? */
15699 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15701 int min_distance, distance;
15703 /* Move forward by display lines to find the new window
15704 start. If window width was enlarged, the new start can
15705 be expected to be > the old start. If window width was
15706 decreased, the new window start will be < the old start.
15707 So, we're looking for the display line start with the
15708 minimum distance from the old window start. */
15709 pos_before_pt = pos = it.current.pos;
15710 min_distance = INFINITY;
15711 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15712 distance < min_distance)
15714 min_distance = distance;
15715 if (CHARPOS (pos) <= PT)
15716 pos_before_pt = pos;
15717 pos = it.current.pos;
15718 if (it.line_wrap == WORD_WRAP)
15720 /* Under WORD_WRAP, move_it_by_lines is likely to
15721 overshoot and stop not at the first, but the
15722 second character from the left margin. So in
15723 that case, we need a more tight control on the X
15724 coordinate of the iterator than move_it_by_lines
15725 promises in its contract. The method is to first
15726 go to the last (rightmost) visible character of a
15727 line, then move to the leftmost character on the
15728 next line in a separate call. */
15729 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15730 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15731 move_it_to (&it, ZV, 0,
15732 it.current_y + it.max_ascent + it.max_descent, -1,
15733 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15735 else
15736 move_it_by_lines (&it, 1);
15739 /* It makes very little sense to make the new window start
15740 after point, as point won't be visible. If that's what
15741 the loop above finds, fall back on the candidate before
15742 or at point that is closest to the old window start. */
15743 if (CHARPOS (pos) > PT)
15744 pos = pos_before_pt;
15746 /* Set the window start there. */
15747 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15748 window_start_changed_p = true;
15752 return window_start_changed_p;
15756 /* Try cursor movement in case text has not changed in window WINDOW,
15757 with window start STARTP. Value is
15759 CURSOR_MOVEMENT_SUCCESS if successful
15761 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15763 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15764 display. *SCROLL_STEP is set to true, under certain circumstances, if
15765 we want to scroll as if scroll-step were set to 1. See the code.
15767 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15768 which case we have to abort this redisplay, and adjust matrices
15769 first. */
15771 enum
15773 CURSOR_MOVEMENT_SUCCESS,
15774 CURSOR_MOVEMENT_CANNOT_BE_USED,
15775 CURSOR_MOVEMENT_MUST_SCROLL,
15776 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15779 static int
15780 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15781 bool *scroll_step)
15783 struct window *w = XWINDOW (window);
15784 struct frame *f = XFRAME (w->frame);
15785 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15787 #ifdef GLYPH_DEBUG
15788 if (inhibit_try_cursor_movement)
15789 return rc;
15790 #endif
15792 /* Previously, there was a check for Lisp integer in the
15793 if-statement below. Now, this field is converted to
15794 ptrdiff_t, thus zero means invalid position in a buffer. */
15795 eassert (w->last_point > 0);
15796 /* Likewise there was a check whether window_end_vpos is nil or larger
15797 than the window. Now window_end_vpos is int and so never nil, but
15798 let's leave eassert to check whether it fits in the window. */
15799 eassert (!w->window_end_valid
15800 || w->window_end_vpos < w->current_matrix->nrows);
15802 /* Handle case where text has not changed, only point, and it has
15803 not moved off the frame. */
15804 if (/* Point may be in this window. */
15805 PT >= CHARPOS (startp)
15806 /* Selective display hasn't changed. */
15807 && !current_buffer->clip_changed
15808 /* Function force-mode-line-update is used to force a thorough
15809 redisplay. It sets either windows_or_buffers_changed or
15810 update_mode_lines. So don't take a shortcut here for these
15811 cases. */
15812 && !update_mode_lines
15813 && !windows_or_buffers_changed
15814 && !f->cursor_type_changed
15815 && NILP (Vshow_trailing_whitespace)
15816 /* This code is not used for mini-buffer for the sake of the case
15817 of redisplaying to replace an echo area message; since in
15818 that case the mini-buffer contents per se are usually
15819 unchanged. This code is of no real use in the mini-buffer
15820 since the handling of this_line_start_pos, etc., in redisplay
15821 handles the same cases. */
15822 && !EQ (window, minibuf_window)
15823 && (FRAME_WINDOW_P (f)
15824 || !overlay_arrow_in_current_buffer_p ()))
15826 int this_scroll_margin, top_scroll_margin;
15827 struct glyph_row *row = NULL;
15828 int frame_line_height = default_line_pixel_height (w);
15829 int window_total_lines
15830 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15832 #ifdef GLYPH_DEBUG
15833 debug_method_add (w, "cursor movement");
15834 #endif
15836 /* Scroll if point within this distance from the top or bottom
15837 of the window. This is a pixel value. */
15838 if (scroll_margin > 0)
15840 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15841 this_scroll_margin *= frame_line_height;
15843 else
15844 this_scroll_margin = 0;
15846 top_scroll_margin = this_scroll_margin;
15847 if (WINDOW_WANTS_HEADER_LINE_P (w))
15848 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15850 /* Start with the row the cursor was displayed during the last
15851 not paused redisplay. Give up if that row is not valid. */
15852 if (w->last_cursor_vpos < 0
15853 || w->last_cursor_vpos >= w->current_matrix->nrows)
15854 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15855 else
15857 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15858 if (row->mode_line_p)
15859 ++row;
15860 if (!row->enabled_p)
15861 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15864 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15866 bool scroll_p = false, must_scroll = false;
15867 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15869 if (PT > w->last_point)
15871 /* Point has moved forward. */
15872 while (MATRIX_ROW_END_CHARPOS (row) < PT
15873 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15875 eassert (row->enabled_p);
15876 ++row;
15879 /* If the end position of a row equals the start
15880 position of the next row, and PT is at that position,
15881 we would rather display cursor in the next line. */
15882 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15883 && MATRIX_ROW_END_CHARPOS (row) == PT
15884 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15885 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15886 && !cursor_row_p (row))
15887 ++row;
15889 /* If within the scroll margin, scroll. Note that
15890 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15891 the next line would be drawn, and that
15892 this_scroll_margin can be zero. */
15893 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15894 || PT > MATRIX_ROW_END_CHARPOS (row)
15895 /* Line is completely visible last line in window
15896 and PT is to be set in the next line. */
15897 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15898 && PT == MATRIX_ROW_END_CHARPOS (row)
15899 && !row->ends_at_zv_p
15900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15901 scroll_p = true;
15903 else if (PT < w->last_point)
15905 /* Cursor has to be moved backward. Note that PT >=
15906 CHARPOS (startp) because of the outer if-statement. */
15907 while (!row->mode_line_p
15908 && (MATRIX_ROW_START_CHARPOS (row) > PT
15909 || (MATRIX_ROW_START_CHARPOS (row) == PT
15910 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15911 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15912 row > w->current_matrix->rows
15913 && (row-1)->ends_in_newline_from_string_p))))
15914 && (row->y > top_scroll_margin
15915 || CHARPOS (startp) == BEGV))
15917 eassert (row->enabled_p);
15918 --row;
15921 /* Consider the following case: Window starts at BEGV,
15922 there is invisible, intangible text at BEGV, so that
15923 display starts at some point START > BEGV. It can
15924 happen that we are called with PT somewhere between
15925 BEGV and START. Try to handle that case. */
15926 if (row < w->current_matrix->rows
15927 || row->mode_line_p)
15929 row = w->current_matrix->rows;
15930 if (row->mode_line_p)
15931 ++row;
15934 /* Due to newlines in overlay strings, we may have to
15935 skip forward over overlay strings. */
15936 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15937 && MATRIX_ROW_END_CHARPOS (row) == PT
15938 && !cursor_row_p (row))
15939 ++row;
15941 /* If within the scroll margin, scroll. */
15942 if (row->y < top_scroll_margin
15943 && CHARPOS (startp) != BEGV)
15944 scroll_p = true;
15946 else
15948 /* Cursor did not move. So don't scroll even if cursor line
15949 is partially visible, as it was so before. */
15950 rc = CURSOR_MOVEMENT_SUCCESS;
15953 if (PT < MATRIX_ROW_START_CHARPOS (row)
15954 || PT > MATRIX_ROW_END_CHARPOS (row))
15956 /* if PT is not in the glyph row, give up. */
15957 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15958 must_scroll = true;
15960 else if (rc != CURSOR_MOVEMENT_SUCCESS
15961 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15963 struct glyph_row *row1;
15965 /* If rows are bidi-reordered and point moved, back up
15966 until we find a row that does not belong to a
15967 continuation line. This is because we must consider
15968 all rows of a continued line as candidates for the
15969 new cursor positioning, since row start and end
15970 positions change non-linearly with vertical position
15971 in such rows. */
15972 /* FIXME: Revisit this when glyph ``spilling'' in
15973 continuation lines' rows is implemented for
15974 bidi-reordered rows. */
15975 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15976 MATRIX_ROW_CONTINUATION_LINE_P (row);
15977 --row)
15979 /* If we hit the beginning of the displayed portion
15980 without finding the first row of a continued
15981 line, give up. */
15982 if (row <= row1)
15984 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15985 break;
15987 eassert (row->enabled_p);
15990 if (must_scroll)
15992 else if (rc != CURSOR_MOVEMENT_SUCCESS
15993 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15994 /* Make sure this isn't a header line by any chance, since
15995 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15996 && !row->mode_line_p
15997 && make_cursor_line_fully_visible_p)
15999 if (PT == MATRIX_ROW_END_CHARPOS (row)
16000 && !row->ends_at_zv_p
16001 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16002 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16003 else if (row->height > window_box_height (w))
16005 /* If we end up in a partially visible line, let's
16006 make it fully visible, except when it's taller
16007 than the window, in which case we can't do much
16008 about it. */
16009 *scroll_step = true;
16010 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16012 else
16014 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16015 if (!cursor_row_fully_visible_p (w, false, true))
16016 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16017 else
16018 rc = CURSOR_MOVEMENT_SUCCESS;
16021 else if (scroll_p)
16022 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16023 else if (rc != CURSOR_MOVEMENT_SUCCESS
16024 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16026 /* With bidi-reordered rows, there could be more than
16027 one candidate row whose start and end positions
16028 occlude point. We need to let set_cursor_from_row
16029 find the best candidate. */
16030 /* FIXME: Revisit this when glyph ``spilling'' in
16031 continuation lines' rows is implemented for
16032 bidi-reordered rows. */
16033 bool rv = false;
16037 bool at_zv_p = false, exact_match_p = false;
16039 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16040 && PT <= MATRIX_ROW_END_CHARPOS (row)
16041 && cursor_row_p (row))
16042 rv |= set_cursor_from_row (w, row, w->current_matrix,
16043 0, 0, 0, 0);
16044 /* As soon as we've found the exact match for point,
16045 or the first suitable row whose ends_at_zv_p flag
16046 is set, we are done. */
16047 if (rv)
16049 at_zv_p = MATRIX_ROW (w->current_matrix,
16050 w->cursor.vpos)->ends_at_zv_p;
16051 if (!at_zv_p
16052 && w->cursor.hpos >= 0
16053 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16054 w->cursor.vpos))
16056 struct glyph_row *candidate =
16057 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16058 struct glyph *g =
16059 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16060 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16062 exact_match_p =
16063 (BUFFERP (g->object) && g->charpos == PT)
16064 || (NILP (g->object)
16065 && (g->charpos == PT
16066 || (g->charpos == 0 && endpos - 1 == PT)));
16068 if (at_zv_p || exact_match_p)
16070 rc = CURSOR_MOVEMENT_SUCCESS;
16071 break;
16074 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16075 break;
16076 ++row;
16078 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16079 || row->continued_p)
16080 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16081 || (MATRIX_ROW_START_CHARPOS (row) == PT
16082 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16083 /* If we didn't find any candidate rows, or exited the
16084 loop before all the candidates were examined, signal
16085 to the caller that this method failed. */
16086 if (rc != CURSOR_MOVEMENT_SUCCESS
16087 && !(rv
16088 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16089 && !row->continued_p))
16090 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16091 else if (rv)
16092 rc = CURSOR_MOVEMENT_SUCCESS;
16094 else
16098 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16100 rc = CURSOR_MOVEMENT_SUCCESS;
16101 break;
16103 ++row;
16105 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16106 && MATRIX_ROW_START_CHARPOS (row) == PT
16107 && cursor_row_p (row));
16112 return rc;
16116 void
16117 set_vertical_scroll_bar (struct window *w)
16119 ptrdiff_t start, end, whole;
16121 /* Calculate the start and end positions for the current window.
16122 At some point, it would be nice to choose between scrollbars
16123 which reflect the whole buffer size, with special markers
16124 indicating narrowing, and scrollbars which reflect only the
16125 visible region.
16127 Note that mini-buffers sometimes aren't displaying any text. */
16128 if (!MINI_WINDOW_P (w)
16129 || (w == XWINDOW (minibuf_window)
16130 && NILP (echo_area_buffer[0])))
16132 struct buffer *buf = XBUFFER (w->contents);
16133 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16134 start = marker_position (w->start) - BUF_BEGV (buf);
16135 /* I don't think this is guaranteed to be right. For the
16136 moment, we'll pretend it is. */
16137 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16139 if (end < start)
16140 end = start;
16141 if (whole < (end - start))
16142 whole = end - start;
16144 else
16145 start = end = whole = 0;
16147 /* Indicate what this scroll bar ought to be displaying now. */
16148 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16149 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16150 (w, end - start, whole, start);
16154 void
16155 set_horizontal_scroll_bar (struct window *w)
16157 int start, end, whole, portion;
16159 if (!MINI_WINDOW_P (w)
16160 || (w == XWINDOW (minibuf_window)
16161 && NILP (echo_area_buffer[0])))
16163 struct buffer *b = XBUFFER (w->contents);
16164 struct buffer *old_buffer = NULL;
16165 struct it it;
16166 struct text_pos startp;
16168 if (b != current_buffer)
16170 old_buffer = current_buffer;
16171 set_buffer_internal (b);
16174 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16175 start_display (&it, w, startp);
16176 it.last_visible_x = INT_MAX;
16177 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16178 MOVE_TO_X | MOVE_TO_Y);
16179 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16180 window_box_height (w), -1,
16181 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16183 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16184 end = start + window_box_width (w, TEXT_AREA);
16185 portion = end - start;
16186 /* After enlarging a horizontally scrolled window such that it
16187 gets at least as wide as the text it contains, make sure that
16188 the thumb doesn't fill the entire scroll bar so we can still
16189 drag it back to see the entire text. */
16190 whole = max (whole, end);
16192 if (it.bidi_p)
16194 Lisp_Object pdir;
16196 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16197 if (EQ (pdir, Qright_to_left))
16199 start = whole - end;
16200 end = start + portion;
16204 if (old_buffer)
16205 set_buffer_internal (old_buffer);
16207 else
16208 start = end = whole = portion = 0;
16210 w->hscroll_whole = whole;
16212 /* Indicate what this scroll bar ought to be displaying now. */
16213 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16214 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16215 (w, portion, whole, start);
16219 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16220 selected_window is redisplayed.
16222 We can return without actually redisplaying the window if fonts has been
16223 changed on window's frame. In that case, redisplay_internal will retry.
16225 As one of the important parts of redisplaying a window, we need to
16226 decide whether the previous window-start position (stored in the
16227 window's w->start marker position) is still valid, and if it isn't,
16228 recompute it. Some details about that:
16230 . The previous window-start could be in a continuation line, in
16231 which case we need to recompute it when the window width
16232 changes. See compute_window_start_on_continuation_line and its
16233 call below.
16235 . The text that changed since last redisplay could include the
16236 previous window-start position. In that case, we try to salvage
16237 what we can from the current glyph matrix by calling
16238 try_scrolling, which see.
16240 . Some Emacs command could force us to use a specific window-start
16241 position by setting the window's force_start flag, or gently
16242 propose doing that by setting the window's optional_new_start
16243 flag. In these cases, we try using the specified start point if
16244 that succeeds (i.e. the window desired matrix is successfully
16245 recomputed, and point location is within the window). In case
16246 of optional_new_start, we first check if the specified start
16247 position is feasible, i.e. if it will allow point to be
16248 displayed in the window. If using the specified start point
16249 fails, e.g., if new fonts are needed to be loaded, we abort the
16250 redisplay cycle and leave it up to the next cycle to figure out
16251 things.
16253 . Note that the window's force_start flag is sometimes set by
16254 redisplay itself, when it decides that the previous window start
16255 point is fine and should be kept. Search for "goto force_start"
16256 below to see the details. Like the values of window-start
16257 specified outside of redisplay, these internally-deduced values
16258 are tested for feasibility, and ignored if found to be
16259 unfeasible.
16261 . Note that the function try_window, used to completely redisplay
16262 a window, accepts the window's start point as its argument.
16263 This is used several times in the redisplay code to control
16264 where the window start will be, according to user options such
16265 as scroll-conservatively, and also to ensure the screen line
16266 showing point will be fully (as opposed to partially) visible on
16267 display. */
16269 static void
16270 redisplay_window (Lisp_Object window, bool just_this_one_p)
16272 struct window *w = XWINDOW (window);
16273 struct frame *f = XFRAME (w->frame);
16274 struct buffer *buffer = XBUFFER (w->contents);
16275 struct buffer *old = current_buffer;
16276 struct text_pos lpoint, opoint, startp;
16277 bool update_mode_line;
16278 int tem;
16279 struct it it;
16280 /* Record it now because it's overwritten. */
16281 bool current_matrix_up_to_date_p = false;
16282 bool used_current_matrix_p = false;
16283 /* This is less strict than current_matrix_up_to_date_p.
16284 It indicates that the buffer contents and narrowing are unchanged. */
16285 bool buffer_unchanged_p = false;
16286 bool temp_scroll_step = false;
16287 ptrdiff_t count = SPECPDL_INDEX ();
16288 int rc;
16289 int centering_position = -1;
16290 bool last_line_misfit = false;
16291 ptrdiff_t beg_unchanged, end_unchanged;
16292 int frame_line_height;
16293 bool use_desired_matrix;
16294 void *itdata = NULL;
16296 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16297 opoint = lpoint;
16299 #ifdef GLYPH_DEBUG
16300 *w->desired_matrix->method = 0;
16301 #endif
16303 if (!just_this_one_p
16304 && REDISPLAY_SOME_P ()
16305 && !w->redisplay
16306 && !w->update_mode_line
16307 && !f->face_change
16308 && !f->redisplay
16309 && !buffer->text->redisplay
16310 && BUF_PT (buffer) == w->last_point)
16311 return;
16313 /* Make sure that both W's markers are valid. */
16314 eassert (XMARKER (w->start)->buffer == buffer);
16315 eassert (XMARKER (w->pointm)->buffer == buffer);
16317 /* We come here again if we need to run window-text-change-functions
16318 below. */
16319 restart:
16320 reconsider_clip_changes (w);
16321 frame_line_height = default_line_pixel_height (w);
16323 /* Has the mode line to be updated? */
16324 update_mode_line = (w->update_mode_line
16325 || update_mode_lines
16326 || buffer->clip_changed
16327 || buffer->prevent_redisplay_optimizations_p);
16329 if (!just_this_one_p)
16330 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16331 cleverly elsewhere. */
16332 w->must_be_updated_p = true;
16334 if (MINI_WINDOW_P (w))
16336 if (w == XWINDOW (echo_area_window)
16337 && !NILP (echo_area_buffer[0]))
16339 if (update_mode_line)
16340 /* We may have to update a tty frame's menu bar or a
16341 tool-bar. Example `M-x C-h C-h C-g'. */
16342 goto finish_menu_bars;
16343 else
16344 /* We've already displayed the echo area glyphs in this window. */
16345 goto finish_scroll_bars;
16347 else if ((w != XWINDOW (minibuf_window)
16348 || minibuf_level == 0)
16349 /* When buffer is nonempty, redisplay window normally. */
16350 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16351 /* Quail displays non-mini buffers in minibuffer window.
16352 In that case, redisplay the window normally. */
16353 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16355 /* W is a mini-buffer window, but it's not active, so clear
16356 it. */
16357 int yb = window_text_bottom_y (w);
16358 struct glyph_row *row;
16359 int y;
16361 for (y = 0, row = w->desired_matrix->rows;
16362 y < yb;
16363 y += row->height, ++row)
16364 blank_row (w, row, y);
16365 goto finish_scroll_bars;
16368 clear_glyph_matrix (w->desired_matrix);
16371 /* Otherwise set up data on this window; select its buffer and point
16372 value. */
16373 /* Really select the buffer, for the sake of buffer-local
16374 variables. */
16375 set_buffer_internal_1 (XBUFFER (w->contents));
16377 current_matrix_up_to_date_p
16378 = (w->window_end_valid
16379 && !current_buffer->clip_changed
16380 && !current_buffer->prevent_redisplay_optimizations_p
16381 && !window_outdated (w));
16383 /* Run the window-text-change-functions
16384 if it is possible that the text on the screen has changed
16385 (either due to modification of the text, or any other reason). */
16386 if (!current_matrix_up_to_date_p
16387 && !NILP (Vwindow_text_change_functions))
16389 safe_run_hooks (Qwindow_text_change_functions);
16390 goto restart;
16393 beg_unchanged = BEG_UNCHANGED;
16394 end_unchanged = END_UNCHANGED;
16396 SET_TEXT_POS (opoint, PT, PT_BYTE);
16398 specbind (Qinhibit_point_motion_hooks, Qt);
16400 buffer_unchanged_p
16401 = (w->window_end_valid
16402 && !current_buffer->clip_changed
16403 && !window_outdated (w));
16405 /* When windows_or_buffers_changed is non-zero, we can't rely
16406 on the window end being valid, so set it to zero there. */
16407 if (windows_or_buffers_changed)
16409 /* If window starts on a continuation line, maybe adjust the
16410 window start in case the window's width changed. */
16411 if (XMARKER (w->start)->buffer == current_buffer)
16412 compute_window_start_on_continuation_line (w);
16414 w->window_end_valid = false;
16415 /* If so, we also can't rely on current matrix
16416 and should not fool try_cursor_movement below. */
16417 current_matrix_up_to_date_p = false;
16420 /* Some sanity checks. */
16421 CHECK_WINDOW_END (w);
16422 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16423 emacs_abort ();
16424 if (BYTEPOS (opoint) < CHARPOS (opoint))
16425 emacs_abort ();
16427 if (mode_line_update_needed (w))
16428 update_mode_line = true;
16430 /* Point refers normally to the selected window. For any other
16431 window, set up appropriate value. */
16432 if (!EQ (window, selected_window))
16434 ptrdiff_t new_pt = marker_position (w->pointm);
16435 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16437 if (new_pt < BEGV)
16439 new_pt = BEGV;
16440 new_pt_byte = BEGV_BYTE;
16441 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16443 else if (new_pt > (ZV - 1))
16445 new_pt = ZV;
16446 new_pt_byte = ZV_BYTE;
16447 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16450 /* We don't use SET_PT so that the point-motion hooks don't run. */
16451 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16454 /* If any of the character widths specified in the display table
16455 have changed, invalidate the width run cache. It's true that
16456 this may be a bit late to catch such changes, but the rest of
16457 redisplay goes (non-fatally) haywire when the display table is
16458 changed, so why should we worry about doing any better? */
16459 if (current_buffer->width_run_cache
16460 || (current_buffer->base_buffer
16461 && current_buffer->base_buffer->width_run_cache))
16463 struct Lisp_Char_Table *disptab = buffer_display_table ();
16465 if (! disptab_matches_widthtab
16466 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16468 struct buffer *buf = current_buffer;
16470 if (buf->base_buffer)
16471 buf = buf->base_buffer;
16472 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16473 recompute_width_table (current_buffer, disptab);
16477 /* If window-start is screwed up, choose a new one. */
16478 if (XMARKER (w->start)->buffer != current_buffer)
16479 goto recenter;
16481 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16483 /* If someone specified a new starting point but did not insist,
16484 check whether it can be used. */
16485 if ((w->optional_new_start || window_frozen_p (w))
16486 && CHARPOS (startp) >= BEGV
16487 && CHARPOS (startp) <= ZV)
16489 ptrdiff_t it_charpos;
16491 w->optional_new_start = false;
16492 start_display (&it, w, startp);
16493 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16494 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16495 /* Record IT's position now, since line_bottom_y might change
16496 that. */
16497 it_charpos = IT_CHARPOS (it);
16498 /* Make sure we set the force_start flag only if the cursor row
16499 will be fully visible. Otherwise, the code under force_start
16500 label below will try to move point back into view, which is
16501 not what the code which sets optional_new_start wants. */
16502 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16503 && !w->force_start)
16505 if (it_charpos == PT)
16506 w->force_start = true;
16507 /* IT may overshoot PT if text at PT is invisible. */
16508 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16509 w->force_start = true;
16510 #ifdef GLYPH_DEBUG
16511 if (w->force_start)
16513 if (window_frozen_p (w))
16514 debug_method_add (w, "set force_start from frozen window start");
16515 else
16516 debug_method_add (w, "set force_start from optional_new_start");
16518 #endif
16522 force_start:
16524 /* Handle case where place to start displaying has been specified,
16525 unless the specified location is outside the accessible range. */
16526 if (w->force_start)
16528 /* We set this later on if we have to adjust point. */
16529 int new_vpos = -1;
16531 w->force_start = false;
16532 w->vscroll = 0;
16533 w->window_end_valid = false;
16535 /* Forget any recorded base line for line number display. */
16536 if (!buffer_unchanged_p)
16537 w->base_line_number = 0;
16539 /* Redisplay the mode line. Select the buffer properly for that.
16540 Also, run the hook window-scroll-functions
16541 because we have scrolled. */
16542 /* Note, we do this after clearing force_start because
16543 if there's an error, it is better to forget about force_start
16544 than to get into an infinite loop calling the hook functions
16545 and having them get more errors. */
16546 if (!update_mode_line
16547 || ! NILP (Vwindow_scroll_functions))
16549 update_mode_line = true;
16550 w->update_mode_line = true;
16551 startp = run_window_scroll_functions (window, startp);
16554 if (CHARPOS (startp) < BEGV)
16555 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16556 else if (CHARPOS (startp) > ZV)
16557 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16559 /* Redisplay, then check if cursor has been set during the
16560 redisplay. Give up if new fonts were loaded. */
16561 /* We used to issue a CHECK_MARGINS argument to try_window here,
16562 but this causes scrolling to fail when point begins inside
16563 the scroll margin (bug#148) -- cyd */
16564 if (!try_window (window, startp, 0))
16566 w->force_start = true;
16567 clear_glyph_matrix (w->desired_matrix);
16568 goto need_larger_matrices;
16571 if (w->cursor.vpos < 0)
16573 /* If point does not appear, try to move point so it does
16574 appear. The desired matrix has been built above, so we
16575 can use it here. First see if point is in invisible
16576 text, and if so, move it to the first visible buffer
16577 position past that. */
16578 struct glyph_row *r = NULL;
16579 Lisp_Object invprop =
16580 get_char_property_and_overlay (make_number (PT), Qinvisible,
16581 Qnil, NULL);
16583 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16585 ptrdiff_t alt_pt;
16586 Lisp_Object invprop_end =
16587 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16588 Qnil, Qnil);
16590 if (NATNUMP (invprop_end))
16591 alt_pt = XFASTINT (invprop_end);
16592 else
16593 alt_pt = ZV;
16594 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16595 NULL, 0);
16597 if (r)
16598 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16599 else /* Give up and just move to the middle of the window. */
16600 new_vpos = window_box_height (w) / 2;
16603 if (!cursor_row_fully_visible_p (w, false, false))
16605 /* Point does appear, but on a line partly visible at end of window.
16606 Move it back to a fully-visible line. */
16607 new_vpos = window_box_height (w);
16608 /* But if window_box_height suggests a Y coordinate that is
16609 not less than we already have, that line will clearly not
16610 be fully visible, so give up and scroll the display.
16611 This can happen when the default face uses a font whose
16612 dimensions are different from the frame's default
16613 font. */
16614 if (new_vpos >= w->cursor.y)
16616 w->cursor.vpos = -1;
16617 clear_glyph_matrix (w->desired_matrix);
16618 goto try_to_scroll;
16621 else if (w->cursor.vpos >= 0)
16623 /* Some people insist on not letting point enter the scroll
16624 margin, even though this part handles windows that didn't
16625 scroll at all. */
16626 int window_total_lines
16627 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16628 int margin = min (scroll_margin, window_total_lines / 4);
16629 int pixel_margin = margin * frame_line_height;
16630 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16632 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16633 below, which finds the row to move point to, advances by
16634 the Y coordinate of the _next_ row, see the definition of
16635 MATRIX_ROW_BOTTOM_Y. */
16636 if (w->cursor.vpos < margin + header_line)
16638 w->cursor.vpos = -1;
16639 clear_glyph_matrix (w->desired_matrix);
16640 goto try_to_scroll;
16642 else
16644 int window_height = window_box_height (w);
16646 if (header_line)
16647 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16648 if (w->cursor.y >= window_height - pixel_margin)
16650 w->cursor.vpos = -1;
16651 clear_glyph_matrix (w->desired_matrix);
16652 goto try_to_scroll;
16657 /* If we need to move point for either of the above reasons,
16658 now actually do it. */
16659 if (new_vpos >= 0)
16661 struct glyph_row *row;
16663 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16664 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16665 ++row;
16667 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16668 MATRIX_ROW_START_BYTEPOS (row));
16670 if (w != XWINDOW (selected_window))
16671 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16672 else if (current_buffer == old)
16673 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16675 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16677 /* Re-run pre-redisplay-function so it can update the region
16678 according to the new position of point. */
16679 /* Other than the cursor, w's redisplay is done so we can set its
16680 redisplay to false. Also the buffer's redisplay can be set to
16681 false, since propagate_buffer_redisplay should have already
16682 propagated its info to `w' anyway. */
16683 w->redisplay = false;
16684 XBUFFER (w->contents)->text->redisplay = false;
16685 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16687 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16689 /* pre-redisplay-function made changes (e.g. move the region)
16690 that require another round of redisplay. */
16691 clear_glyph_matrix (w->desired_matrix);
16692 if (!try_window (window, startp, 0))
16693 goto need_larger_matrices;
16696 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16698 clear_glyph_matrix (w->desired_matrix);
16699 goto try_to_scroll;
16702 #ifdef GLYPH_DEBUG
16703 debug_method_add (w, "forced window start");
16704 #endif
16705 goto done;
16708 /* Handle case where text has not changed, only point, and it has
16709 not moved off the frame, and we are not retrying after hscroll.
16710 (current_matrix_up_to_date_p is true when retrying.) */
16711 if (current_matrix_up_to_date_p
16712 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16713 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16715 switch (rc)
16717 case CURSOR_MOVEMENT_SUCCESS:
16718 used_current_matrix_p = true;
16719 goto done;
16721 case CURSOR_MOVEMENT_MUST_SCROLL:
16722 goto try_to_scroll;
16724 default:
16725 emacs_abort ();
16728 /* If current starting point was originally the beginning of a line
16729 but no longer is, find a new starting point. */
16730 else if (w->start_at_line_beg
16731 && !(CHARPOS (startp) <= BEGV
16732 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16734 #ifdef GLYPH_DEBUG
16735 debug_method_add (w, "recenter 1");
16736 #endif
16737 goto recenter;
16740 /* Try scrolling with try_window_id. Value is > 0 if update has
16741 been done, it is -1 if we know that the same window start will
16742 not work. It is 0 if unsuccessful for some other reason. */
16743 else if ((tem = try_window_id (w)) != 0)
16745 #ifdef GLYPH_DEBUG
16746 debug_method_add (w, "try_window_id %d", tem);
16747 #endif
16749 if (f->fonts_changed)
16750 goto need_larger_matrices;
16751 if (tem > 0)
16752 goto done;
16754 /* Otherwise try_window_id has returned -1 which means that we
16755 don't want the alternative below this comment to execute. */
16757 else if (CHARPOS (startp) >= BEGV
16758 && CHARPOS (startp) <= ZV
16759 && PT >= CHARPOS (startp)
16760 && (CHARPOS (startp) < ZV
16761 /* Avoid starting at end of buffer. */
16762 || CHARPOS (startp) == BEGV
16763 || !window_outdated (w)))
16765 int d1, d2, d5, d6;
16766 int rtop, rbot;
16768 /* If first window line is a continuation line, and window start
16769 is inside the modified region, but the first change is before
16770 current window start, we must select a new window start.
16772 However, if this is the result of a down-mouse event (e.g. by
16773 extending the mouse-drag-overlay), we don't want to select a
16774 new window start, since that would change the position under
16775 the mouse, resulting in an unwanted mouse-movement rather
16776 than a simple mouse-click. */
16777 if (!w->start_at_line_beg
16778 && NILP (do_mouse_tracking)
16779 && CHARPOS (startp) > BEGV
16780 && CHARPOS (startp) > BEG + beg_unchanged
16781 && CHARPOS (startp) <= Z - end_unchanged
16782 /* Even if w->start_at_line_beg is nil, a new window may
16783 start at a line_beg, since that's how set_buffer_window
16784 sets it. So, we need to check the return value of
16785 compute_window_start_on_continuation_line. (See also
16786 bug#197). */
16787 && XMARKER (w->start)->buffer == current_buffer
16788 && compute_window_start_on_continuation_line (w)
16789 /* It doesn't make sense to force the window start like we
16790 do at label force_start if it is already known that point
16791 will not be fully visible in the resulting window, because
16792 doing so will move point from its correct position
16793 instead of scrolling the window to bring point into view.
16794 See bug#9324. */
16795 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16796 /* A very tall row could need more than the window height,
16797 in which case we accept that it is partially visible. */
16798 && (rtop != 0) == (rbot != 0))
16800 w->force_start = true;
16801 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16802 #ifdef GLYPH_DEBUG
16803 debug_method_add (w, "recomputed window start in continuation line");
16804 #endif
16805 goto force_start;
16808 #ifdef GLYPH_DEBUG
16809 debug_method_add (w, "same window start");
16810 #endif
16812 /* Try to redisplay starting at same place as before.
16813 If point has not moved off frame, accept the results. */
16814 if (!current_matrix_up_to_date_p
16815 /* Don't use try_window_reusing_current_matrix in this case
16816 because a window scroll function can have changed the
16817 buffer. */
16818 || !NILP (Vwindow_scroll_functions)
16819 || MINI_WINDOW_P (w)
16820 || !(used_current_matrix_p
16821 = try_window_reusing_current_matrix (w)))
16823 IF_DEBUG (debug_method_add (w, "1"));
16824 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16825 /* -1 means we need to scroll.
16826 0 means we need new matrices, but fonts_changed
16827 is set in that case, so we will detect it below. */
16828 goto try_to_scroll;
16831 if (f->fonts_changed)
16832 goto need_larger_matrices;
16834 if (w->cursor.vpos >= 0)
16836 if (!just_this_one_p
16837 || current_buffer->clip_changed
16838 || BEG_UNCHANGED < CHARPOS (startp))
16839 /* Forget any recorded base line for line number display. */
16840 w->base_line_number = 0;
16842 if (!cursor_row_fully_visible_p (w, true, false))
16844 clear_glyph_matrix (w->desired_matrix);
16845 last_line_misfit = true;
16847 /* Drop through and scroll. */
16848 else
16849 goto done;
16851 else
16852 clear_glyph_matrix (w->desired_matrix);
16855 try_to_scroll:
16857 /* Redisplay the mode line. Select the buffer properly for that. */
16858 if (!update_mode_line)
16860 update_mode_line = true;
16861 w->update_mode_line = true;
16864 /* Try to scroll by specified few lines. */
16865 if ((scroll_conservatively
16866 || emacs_scroll_step
16867 || temp_scroll_step
16868 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16869 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16870 && CHARPOS (startp) >= BEGV
16871 && CHARPOS (startp) <= ZV)
16873 /* The function returns -1 if new fonts were loaded, 1 if
16874 successful, 0 if not successful. */
16875 int ss = try_scrolling (window, just_this_one_p,
16876 scroll_conservatively,
16877 emacs_scroll_step,
16878 temp_scroll_step, last_line_misfit);
16879 switch (ss)
16881 case SCROLLING_SUCCESS:
16882 goto done;
16884 case SCROLLING_NEED_LARGER_MATRICES:
16885 goto need_larger_matrices;
16887 case SCROLLING_FAILED:
16888 break;
16890 default:
16891 emacs_abort ();
16895 /* Finally, just choose a place to start which positions point
16896 according to user preferences. */
16898 recenter:
16900 #ifdef GLYPH_DEBUG
16901 debug_method_add (w, "recenter");
16902 #endif
16904 /* Forget any previously recorded base line for line number display. */
16905 if (!buffer_unchanged_p)
16906 w->base_line_number = 0;
16908 /* Determine the window start relative to point. */
16909 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16910 it.current_y = it.last_visible_y;
16911 if (centering_position < 0)
16913 int window_total_lines
16914 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16915 int margin
16916 = scroll_margin > 0
16917 ? min (scroll_margin, window_total_lines / 4)
16918 : 0;
16919 ptrdiff_t margin_pos = CHARPOS (startp);
16920 Lisp_Object aggressive;
16921 bool scrolling_up;
16923 /* If there is a scroll margin at the top of the window, find
16924 its character position. */
16925 if (margin
16926 /* Cannot call start_display if startp is not in the
16927 accessible region of the buffer. This can happen when we
16928 have just switched to a different buffer and/or changed
16929 its restriction. In that case, startp is initialized to
16930 the character position 1 (BEGV) because we did not yet
16931 have chance to display the buffer even once. */
16932 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16934 struct it it1;
16935 void *it1data = NULL;
16937 SAVE_IT (it1, it, it1data);
16938 start_display (&it1, w, startp);
16939 move_it_vertically (&it1, margin * frame_line_height);
16940 margin_pos = IT_CHARPOS (it1);
16941 RESTORE_IT (&it, &it, it1data);
16943 scrolling_up = PT > margin_pos;
16944 aggressive =
16945 scrolling_up
16946 ? BVAR (current_buffer, scroll_up_aggressively)
16947 : BVAR (current_buffer, scroll_down_aggressively);
16949 if (!MINI_WINDOW_P (w)
16950 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16952 int pt_offset = 0;
16954 /* Setting scroll-conservatively overrides
16955 scroll-*-aggressively. */
16956 if (!scroll_conservatively && NUMBERP (aggressive))
16958 double float_amount = XFLOATINT (aggressive);
16960 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16961 if (pt_offset == 0 && float_amount > 0)
16962 pt_offset = 1;
16963 if (pt_offset && margin > 0)
16964 margin -= 1;
16966 /* Compute how much to move the window start backward from
16967 point so that point will be displayed where the user
16968 wants it. */
16969 if (scrolling_up)
16971 centering_position = it.last_visible_y;
16972 if (pt_offset)
16973 centering_position -= pt_offset;
16974 centering_position -=
16975 (frame_line_height * (1 + margin + last_line_misfit)
16976 + WINDOW_HEADER_LINE_HEIGHT (w));
16977 /* Don't let point enter the scroll margin near top of
16978 the window. */
16979 if (centering_position < margin * frame_line_height)
16980 centering_position = margin * frame_line_height;
16982 else
16983 centering_position = margin * frame_line_height + pt_offset;
16985 else
16986 /* Set the window start half the height of the window backward
16987 from point. */
16988 centering_position = window_box_height (w) / 2;
16990 move_it_vertically_backward (&it, centering_position);
16992 eassert (IT_CHARPOS (it) >= BEGV);
16994 /* The function move_it_vertically_backward may move over more
16995 than the specified y-distance. If it->w is small, e.g. a
16996 mini-buffer window, we may end up in front of the window's
16997 display area. Start displaying at the start of the line
16998 containing PT in this case. */
16999 if (it.current_y <= 0)
17001 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17002 move_it_vertically_backward (&it, 0);
17003 it.current_y = 0;
17006 it.current_x = it.hpos = 0;
17008 /* Set the window start position here explicitly, to avoid an
17009 infinite loop in case the functions in window-scroll-functions
17010 get errors. */
17011 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17013 /* Run scroll hooks. */
17014 startp = run_window_scroll_functions (window, it.current.pos);
17016 /* We invoke try_window and try_window_reusing_current_matrix below,
17017 and they manipulate the bidi cache. Save and restore the cache
17018 state of our iterator, so we could continue using it after that. */
17019 itdata = bidi_shelve_cache ();
17021 /* Redisplay the window. */
17022 use_desired_matrix = false;
17023 if (!current_matrix_up_to_date_p
17024 || windows_or_buffers_changed
17025 || f->cursor_type_changed
17026 /* Don't use try_window_reusing_current_matrix in this case
17027 because it can have changed the buffer. */
17028 || !NILP (Vwindow_scroll_functions)
17029 || !just_this_one_p
17030 || MINI_WINDOW_P (w)
17031 || !(used_current_matrix_p
17032 = try_window_reusing_current_matrix (w)))
17033 use_desired_matrix = (try_window (window, startp, 0) == 1);
17035 bidi_unshelve_cache (itdata, false);
17037 /* If new fonts have been loaded (due to fontsets), give up. We
17038 have to start a new redisplay since we need to re-adjust glyph
17039 matrices. */
17040 if (f->fonts_changed)
17041 goto need_larger_matrices;
17043 /* If cursor did not appear assume that the middle of the window is
17044 in the first line of the window. Do it again with the next line.
17045 (Imagine a window of height 100, displaying two lines of height
17046 60. Moving back 50 from it->last_visible_y will end in the first
17047 line.) */
17048 if (w->cursor.vpos < 0)
17050 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17052 clear_glyph_matrix (w->desired_matrix);
17053 move_it_by_lines (&it, 1);
17054 try_window (window, it.current.pos, 0);
17056 else if (PT < IT_CHARPOS (it))
17058 clear_glyph_matrix (w->desired_matrix);
17059 move_it_by_lines (&it, -1);
17060 try_window (window, it.current.pos, 0);
17062 else if (scroll_conservatively > SCROLL_LIMIT
17063 && (it.method == GET_FROM_STRING
17064 || overlay_touches_p (IT_CHARPOS (it)))
17065 && IT_CHARPOS (it) < ZV)
17067 /* If the window starts with a before-string that spans more
17068 than one screen line, using that position to display the
17069 window might fail to bring point into the view, because
17070 start_display will always start by displaying the string,
17071 whereas the code above determines where to set w->start
17072 by the buffer position of the place where it takes screen
17073 coordinates. Try to recover by finding the next screen
17074 line that displays buffer text. */
17075 ptrdiff_t pos0 = IT_CHARPOS (it);
17077 clear_glyph_matrix (w->desired_matrix);
17078 do {
17079 move_it_by_lines (&it, 1);
17080 } while (IT_CHARPOS (it) == pos0);
17081 try_window (window, it.current.pos, 0);
17083 else
17085 /* Not much we can do about it. */
17089 /* Consider the following case: Window starts at BEGV, there is
17090 invisible, intangible text at BEGV, so that display starts at
17091 some point START > BEGV. It can happen that we are called with
17092 PT somewhere between BEGV and START. Try to handle that case,
17093 and similar ones. */
17094 if (w->cursor.vpos < 0)
17096 /* Prefer the desired matrix to the current matrix, if possible,
17097 in the fallback calculations below. This is because using
17098 the current matrix might completely goof, e.g. if its first
17099 row is after point. */
17100 struct glyph_matrix *matrix =
17101 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17102 /* First, try locating the proper glyph row for PT. */
17103 struct glyph_row *row =
17104 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17106 /* Sometimes point is at the beginning of invisible text that is
17107 before the 1st character displayed in the row. In that case,
17108 row_containing_pos fails to find the row, because no glyphs
17109 with appropriate buffer positions are present in the row.
17110 Therefore, we next try to find the row which shows the 1st
17111 position after the invisible text. */
17112 if (!row)
17114 Lisp_Object val =
17115 get_char_property_and_overlay (make_number (PT), Qinvisible,
17116 Qnil, NULL);
17118 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17120 ptrdiff_t alt_pos;
17121 Lisp_Object invis_end =
17122 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17123 Qnil, Qnil);
17125 if (NATNUMP (invis_end))
17126 alt_pos = XFASTINT (invis_end);
17127 else
17128 alt_pos = ZV;
17129 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17132 /* Finally, fall back on the first row of the window after the
17133 header line (if any). This is slightly better than not
17134 displaying the cursor at all. */
17135 if (!row)
17137 row = matrix->rows;
17138 if (row->mode_line_p)
17139 ++row;
17141 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17144 if (!cursor_row_fully_visible_p (w, false, false))
17146 /* If vscroll is enabled, disable it and try again. */
17147 if (w->vscroll)
17149 w->vscroll = 0;
17150 clear_glyph_matrix (w->desired_matrix);
17151 goto recenter;
17154 /* Users who set scroll-conservatively to a large number want
17155 point just above/below the scroll margin. If we ended up
17156 with point's row partially visible, move the window start to
17157 make that row fully visible and out of the margin. */
17158 if (scroll_conservatively > SCROLL_LIMIT)
17160 int window_total_lines
17161 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17162 int margin =
17163 scroll_margin > 0
17164 ? min (scroll_margin, window_total_lines / 4)
17165 : 0;
17166 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17168 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17169 clear_glyph_matrix (w->desired_matrix);
17170 if (1 == try_window (window, it.current.pos,
17171 TRY_WINDOW_CHECK_MARGINS))
17172 goto done;
17175 /* If centering point failed to make the whole line visible,
17176 put point at the top instead. That has to make the whole line
17177 visible, if it can be done. */
17178 if (centering_position == 0)
17179 goto done;
17181 clear_glyph_matrix (w->desired_matrix);
17182 centering_position = 0;
17183 goto recenter;
17186 done:
17188 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17189 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17190 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17192 /* Display the mode line, if we must. */
17193 if ((update_mode_line
17194 /* If window not full width, must redo its mode line
17195 if (a) the window to its side is being redone and
17196 (b) we do a frame-based redisplay. This is a consequence
17197 of how inverted lines are drawn in frame-based redisplay. */
17198 || (!just_this_one_p
17199 && !FRAME_WINDOW_P (f)
17200 && !WINDOW_FULL_WIDTH_P (w))
17201 /* Line number to display. */
17202 || w->base_line_pos > 0
17203 /* Column number is displayed and different from the one displayed. */
17204 || (w->column_number_displayed != -1
17205 && (w->column_number_displayed != current_column ())))
17206 /* This means that the window has a mode line. */
17207 && (WINDOW_WANTS_MODELINE_P (w)
17208 || WINDOW_WANTS_HEADER_LINE_P (w)))
17211 display_mode_lines (w);
17213 /* If mode line height has changed, arrange for a thorough
17214 immediate redisplay using the correct mode line height. */
17215 if (WINDOW_WANTS_MODELINE_P (w)
17216 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17218 f->fonts_changed = true;
17219 w->mode_line_height = -1;
17220 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17221 = DESIRED_MODE_LINE_HEIGHT (w);
17224 /* If header line height has changed, arrange for a thorough
17225 immediate redisplay using the correct header line height. */
17226 if (WINDOW_WANTS_HEADER_LINE_P (w)
17227 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17229 f->fonts_changed = true;
17230 w->header_line_height = -1;
17231 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17232 = DESIRED_HEADER_LINE_HEIGHT (w);
17235 if (f->fonts_changed)
17236 goto need_larger_matrices;
17239 if (!line_number_displayed && w->base_line_pos != -1)
17241 w->base_line_pos = 0;
17242 w->base_line_number = 0;
17245 finish_menu_bars:
17247 /* When we reach a frame's selected window, redo the frame's menu
17248 bar and the frame's title. */
17249 if (update_mode_line
17250 && EQ (FRAME_SELECTED_WINDOW (f), window))
17252 bool redisplay_menu_p;
17254 if (FRAME_WINDOW_P (f))
17256 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17257 || defined (HAVE_NS) || defined (USE_GTK)
17258 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17259 #else
17260 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17261 #endif
17263 else
17264 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17266 if (redisplay_menu_p)
17267 display_menu_bar (w);
17269 #ifdef HAVE_WINDOW_SYSTEM
17270 if (FRAME_WINDOW_P (f))
17272 #if defined (USE_GTK) || defined (HAVE_NS)
17273 if (FRAME_EXTERNAL_TOOL_BAR (f))
17274 redisplay_tool_bar (f);
17275 #else
17276 if (WINDOWP (f->tool_bar_window)
17277 && (FRAME_TOOL_BAR_LINES (f) > 0
17278 || !NILP (Vauto_resize_tool_bars))
17279 && redisplay_tool_bar (f))
17280 ignore_mouse_drag_p = true;
17281 #endif
17283 x_consider_frame_title (w->frame);
17284 #endif
17287 #ifdef HAVE_WINDOW_SYSTEM
17288 if (FRAME_WINDOW_P (f)
17289 && update_window_fringes (w, (just_this_one_p
17290 || (!used_current_matrix_p && !overlay_arrow_seen)
17291 || w->pseudo_window_p)))
17293 update_begin (f);
17294 block_input ();
17295 if (draw_window_fringes (w, true))
17297 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17298 x_draw_right_divider (w);
17299 else
17300 x_draw_vertical_border (w);
17302 unblock_input ();
17303 update_end (f);
17306 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17307 x_draw_bottom_divider (w);
17308 #endif /* HAVE_WINDOW_SYSTEM */
17310 /* We go to this label, with fonts_changed set, if it is
17311 necessary to try again using larger glyph matrices.
17312 We have to redeem the scroll bar even in this case,
17313 because the loop in redisplay_internal expects that. */
17314 need_larger_matrices:
17316 finish_scroll_bars:
17318 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17320 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17321 /* Set the thumb's position and size. */
17322 set_vertical_scroll_bar (w);
17324 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17325 /* Set the thumb's position and size. */
17326 set_horizontal_scroll_bar (w);
17328 /* Note that we actually used the scroll bar attached to this
17329 window, so it shouldn't be deleted at the end of redisplay. */
17330 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17331 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17334 /* Restore current_buffer and value of point in it. The window
17335 update may have changed the buffer, so first make sure `opoint'
17336 is still valid (Bug#6177). */
17337 if (CHARPOS (opoint) < BEGV)
17338 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17339 else if (CHARPOS (opoint) > ZV)
17340 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17341 else
17342 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17344 set_buffer_internal_1 (old);
17345 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17346 shorter. This can be caused by log truncation in *Messages*. */
17347 if (CHARPOS (lpoint) <= ZV)
17348 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17350 unbind_to (count, Qnil);
17354 /* Build the complete desired matrix of WINDOW with a window start
17355 buffer position POS.
17357 Value is 1 if successful. It is zero if fonts were loaded during
17358 redisplay which makes re-adjusting glyph matrices necessary, and -1
17359 if point would appear in the scroll margins.
17360 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17361 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17362 set in FLAGS.) */
17365 try_window (Lisp_Object window, struct text_pos pos, int flags)
17367 struct window *w = XWINDOW (window);
17368 struct it it;
17369 struct glyph_row *last_text_row = NULL;
17370 struct frame *f = XFRAME (w->frame);
17371 int frame_line_height = default_line_pixel_height (w);
17373 /* Make POS the new window start. */
17374 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17376 /* Mark cursor position as unknown. No overlay arrow seen. */
17377 w->cursor.vpos = -1;
17378 overlay_arrow_seen = false;
17380 /* Initialize iterator and info to start at POS. */
17381 start_display (&it, w, pos);
17382 it.glyph_row->reversed_p = false;
17384 /* Display all lines of W. */
17385 while (it.current_y < it.last_visible_y)
17387 if (display_line (&it))
17388 last_text_row = it.glyph_row - 1;
17389 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17390 return 0;
17393 /* Don't let the cursor end in the scroll margins. */
17394 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17395 && !MINI_WINDOW_P (w))
17397 int this_scroll_margin;
17398 int window_total_lines
17399 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17401 if (scroll_margin > 0)
17403 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17404 this_scroll_margin *= frame_line_height;
17406 else
17407 this_scroll_margin = 0;
17409 if ((w->cursor.y >= 0 /* not vscrolled */
17410 && w->cursor.y < this_scroll_margin
17411 && CHARPOS (pos) > BEGV
17412 && IT_CHARPOS (it) < ZV)
17413 /* rms: considering make_cursor_line_fully_visible_p here
17414 seems to give wrong results. We don't want to recenter
17415 when the last line is partly visible, we want to allow
17416 that case to be handled in the usual way. */
17417 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17419 w->cursor.vpos = -1;
17420 clear_glyph_matrix (w->desired_matrix);
17421 return -1;
17425 /* If bottom moved off end of frame, change mode line percentage. */
17426 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17427 w->update_mode_line = true;
17429 /* Set window_end_pos to the offset of the last character displayed
17430 on the window from the end of current_buffer. Set
17431 window_end_vpos to its row number. */
17432 if (last_text_row)
17434 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17435 adjust_window_ends (w, last_text_row, false);
17436 eassert
17437 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17438 w->window_end_vpos)));
17440 else
17442 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17443 w->window_end_pos = Z - ZV;
17444 w->window_end_vpos = 0;
17447 /* But that is not valid info until redisplay finishes. */
17448 w->window_end_valid = false;
17449 return 1;
17454 /************************************************************************
17455 Window redisplay reusing current matrix when buffer has not changed
17456 ************************************************************************/
17458 /* Try redisplay of window W showing an unchanged buffer with a
17459 different window start than the last time it was displayed by
17460 reusing its current matrix. Value is true if successful.
17461 W->start is the new window start. */
17463 static bool
17464 try_window_reusing_current_matrix (struct window *w)
17466 struct frame *f = XFRAME (w->frame);
17467 struct glyph_row *bottom_row;
17468 struct it it;
17469 struct run run;
17470 struct text_pos start, new_start;
17471 int nrows_scrolled, i;
17472 struct glyph_row *last_text_row;
17473 struct glyph_row *last_reused_text_row;
17474 struct glyph_row *start_row;
17475 int start_vpos, min_y, max_y;
17477 #ifdef GLYPH_DEBUG
17478 if (inhibit_try_window_reusing)
17479 return false;
17480 #endif
17482 if (/* This function doesn't handle terminal frames. */
17483 !FRAME_WINDOW_P (f)
17484 /* Don't try to reuse the display if windows have been split
17485 or such. */
17486 || windows_or_buffers_changed
17487 || f->cursor_type_changed)
17488 return false;
17490 /* Can't do this if showing trailing whitespace. */
17491 if (!NILP (Vshow_trailing_whitespace))
17492 return false;
17494 /* If top-line visibility has changed, give up. */
17495 if (WINDOW_WANTS_HEADER_LINE_P (w)
17496 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17497 return false;
17499 /* Give up if old or new display is scrolled vertically. We could
17500 make this function handle this, but right now it doesn't. */
17501 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17502 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17503 return false;
17505 /* The variable new_start now holds the new window start. The old
17506 start `start' can be determined from the current matrix. */
17507 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17508 start = start_row->minpos;
17509 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17511 /* Clear the desired matrix for the display below. */
17512 clear_glyph_matrix (w->desired_matrix);
17514 if (CHARPOS (new_start) <= CHARPOS (start))
17516 /* Don't use this method if the display starts with an ellipsis
17517 displayed for invisible text. It's not easy to handle that case
17518 below, and it's certainly not worth the effort since this is
17519 not a frequent case. */
17520 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17521 return false;
17523 IF_DEBUG (debug_method_add (w, "twu1"));
17525 /* Display up to a row that can be reused. The variable
17526 last_text_row is set to the last row displayed that displays
17527 text. Note that it.vpos == 0 if or if not there is a
17528 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17529 start_display (&it, w, new_start);
17530 w->cursor.vpos = -1;
17531 last_text_row = last_reused_text_row = NULL;
17533 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17535 /* If we have reached into the characters in the START row,
17536 that means the line boundaries have changed. So we
17537 can't start copying with the row START. Maybe it will
17538 work to start copying with the following row. */
17539 while (IT_CHARPOS (it) > CHARPOS (start))
17541 /* Advance to the next row as the "start". */
17542 start_row++;
17543 start = start_row->minpos;
17544 /* If there are no more rows to try, or just one, give up. */
17545 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17546 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17547 || CHARPOS (start) == ZV)
17549 clear_glyph_matrix (w->desired_matrix);
17550 return false;
17553 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17555 /* If we have reached alignment, we can copy the rest of the
17556 rows. */
17557 if (IT_CHARPOS (it) == CHARPOS (start)
17558 /* Don't accept "alignment" inside a display vector,
17559 since start_row could have started in the middle of
17560 that same display vector (thus their character
17561 positions match), and we have no way of telling if
17562 that is the case. */
17563 && it.current.dpvec_index < 0)
17564 break;
17566 it.glyph_row->reversed_p = false;
17567 if (display_line (&it))
17568 last_text_row = it.glyph_row - 1;
17572 /* A value of current_y < last_visible_y means that we stopped
17573 at the previous window start, which in turn means that we
17574 have at least one reusable row. */
17575 if (it.current_y < it.last_visible_y)
17577 struct glyph_row *row;
17579 /* IT.vpos always starts from 0; it counts text lines. */
17580 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17582 /* Find PT if not already found in the lines displayed. */
17583 if (w->cursor.vpos < 0)
17585 int dy = it.current_y - start_row->y;
17587 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17588 row = row_containing_pos (w, PT, row, NULL, dy);
17589 if (row)
17590 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17591 dy, nrows_scrolled);
17592 else
17594 clear_glyph_matrix (w->desired_matrix);
17595 return false;
17599 /* Scroll the display. Do it before the current matrix is
17600 changed. The problem here is that update has not yet
17601 run, i.e. part of the current matrix is not up to date.
17602 scroll_run_hook will clear the cursor, and use the
17603 current matrix to get the height of the row the cursor is
17604 in. */
17605 run.current_y = start_row->y;
17606 run.desired_y = it.current_y;
17607 run.height = it.last_visible_y - it.current_y;
17609 if (run.height > 0 && run.current_y != run.desired_y)
17611 update_begin (f);
17612 FRAME_RIF (f)->update_window_begin_hook (w);
17613 FRAME_RIF (f)->clear_window_mouse_face (w);
17614 FRAME_RIF (f)->scroll_run_hook (w, &run);
17615 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17616 update_end (f);
17619 /* Shift current matrix down by nrows_scrolled lines. */
17620 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17621 rotate_matrix (w->current_matrix,
17622 start_vpos,
17623 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17624 nrows_scrolled);
17626 /* Disable lines that must be updated. */
17627 for (i = 0; i < nrows_scrolled; ++i)
17628 (start_row + i)->enabled_p = false;
17630 /* Re-compute Y positions. */
17631 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17632 max_y = it.last_visible_y;
17633 for (row = start_row + nrows_scrolled;
17634 row < bottom_row;
17635 ++row)
17637 row->y = it.current_y;
17638 row->visible_height = row->height;
17640 if (row->y < min_y)
17641 row->visible_height -= min_y - row->y;
17642 if (row->y + row->height > max_y)
17643 row->visible_height -= row->y + row->height - max_y;
17644 if (row->fringe_bitmap_periodic_p)
17645 row->redraw_fringe_bitmaps_p = true;
17647 it.current_y += row->height;
17649 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17650 last_reused_text_row = row;
17651 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17652 break;
17655 /* Disable lines in the current matrix which are now
17656 below the window. */
17657 for (++row; row < bottom_row; ++row)
17658 row->enabled_p = row->mode_line_p = false;
17661 /* Update window_end_pos etc.; last_reused_text_row is the last
17662 reused row from the current matrix containing text, if any.
17663 The value of last_text_row is the last displayed line
17664 containing text. */
17665 if (last_reused_text_row)
17666 adjust_window_ends (w, last_reused_text_row, true);
17667 else if (last_text_row)
17668 adjust_window_ends (w, last_text_row, false);
17669 else
17671 /* This window must be completely empty. */
17672 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17673 w->window_end_pos = Z - ZV;
17674 w->window_end_vpos = 0;
17676 w->window_end_valid = false;
17678 /* Update hint: don't try scrolling again in update_window. */
17679 w->desired_matrix->no_scrolling_p = true;
17681 #ifdef GLYPH_DEBUG
17682 debug_method_add (w, "try_window_reusing_current_matrix 1");
17683 #endif
17684 return true;
17686 else if (CHARPOS (new_start) > CHARPOS (start))
17688 struct glyph_row *pt_row, *row;
17689 struct glyph_row *first_reusable_row;
17690 struct glyph_row *first_row_to_display;
17691 int dy;
17692 int yb = window_text_bottom_y (w);
17694 /* Find the row starting at new_start, if there is one. Don't
17695 reuse a partially visible line at the end. */
17696 first_reusable_row = start_row;
17697 while (first_reusable_row->enabled_p
17698 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17699 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17700 < CHARPOS (new_start)))
17701 ++first_reusable_row;
17703 /* Give up if there is no row to reuse. */
17704 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17705 || !first_reusable_row->enabled_p
17706 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17707 != CHARPOS (new_start)))
17708 return false;
17710 /* We can reuse fully visible rows beginning with
17711 first_reusable_row to the end of the window. Set
17712 first_row_to_display to the first row that cannot be reused.
17713 Set pt_row to the row containing point, if there is any. */
17714 pt_row = NULL;
17715 for (first_row_to_display = first_reusable_row;
17716 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17717 ++first_row_to_display)
17719 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17720 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17721 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17722 && first_row_to_display->ends_at_zv_p
17723 && pt_row == NULL)))
17724 pt_row = first_row_to_display;
17727 /* Start displaying at the start of first_row_to_display. */
17728 eassert (first_row_to_display->y < yb);
17729 init_to_row_start (&it, w, first_row_to_display);
17731 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17732 - start_vpos);
17733 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17734 - nrows_scrolled);
17735 it.current_y = (first_row_to_display->y - first_reusable_row->y
17736 + WINDOW_HEADER_LINE_HEIGHT (w));
17738 /* Display lines beginning with first_row_to_display in the
17739 desired matrix. Set last_text_row to the last row displayed
17740 that displays text. */
17741 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17742 if (pt_row == NULL)
17743 w->cursor.vpos = -1;
17744 last_text_row = NULL;
17745 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17746 if (display_line (&it))
17747 last_text_row = it.glyph_row - 1;
17749 /* If point is in a reused row, adjust y and vpos of the cursor
17750 position. */
17751 if (pt_row)
17753 w->cursor.vpos -= nrows_scrolled;
17754 w->cursor.y -= first_reusable_row->y - start_row->y;
17757 /* Give up if point isn't in a row displayed or reused. (This
17758 also handles the case where w->cursor.vpos < nrows_scrolled
17759 after the calls to display_line, which can happen with scroll
17760 margins. See bug#1295.) */
17761 if (w->cursor.vpos < 0)
17763 clear_glyph_matrix (w->desired_matrix);
17764 return false;
17767 /* Scroll the display. */
17768 run.current_y = first_reusable_row->y;
17769 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17770 run.height = it.last_visible_y - run.current_y;
17771 dy = run.current_y - run.desired_y;
17773 if (run.height)
17775 update_begin (f);
17776 FRAME_RIF (f)->update_window_begin_hook (w);
17777 FRAME_RIF (f)->clear_window_mouse_face (w);
17778 FRAME_RIF (f)->scroll_run_hook (w, &run);
17779 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17780 update_end (f);
17783 /* Adjust Y positions of reused rows. */
17784 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17785 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17786 max_y = it.last_visible_y;
17787 for (row = first_reusable_row; row < first_row_to_display; ++row)
17789 row->y -= dy;
17790 row->visible_height = row->height;
17791 if (row->y < min_y)
17792 row->visible_height -= min_y - row->y;
17793 if (row->y + row->height > max_y)
17794 row->visible_height -= row->y + row->height - max_y;
17795 if (row->fringe_bitmap_periodic_p)
17796 row->redraw_fringe_bitmaps_p = true;
17799 /* Scroll the current matrix. */
17800 eassert (nrows_scrolled > 0);
17801 rotate_matrix (w->current_matrix,
17802 start_vpos,
17803 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17804 -nrows_scrolled);
17806 /* Disable rows not reused. */
17807 for (row -= nrows_scrolled; row < bottom_row; ++row)
17808 row->enabled_p = false;
17810 /* Point may have moved to a different line, so we cannot assume that
17811 the previous cursor position is valid; locate the correct row. */
17812 if (pt_row)
17814 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17815 row < bottom_row
17816 && PT >= MATRIX_ROW_END_CHARPOS (row)
17817 && !row->ends_at_zv_p;
17818 row++)
17820 w->cursor.vpos++;
17821 w->cursor.y = row->y;
17823 if (row < bottom_row)
17825 /* Can't simply scan the row for point with
17826 bidi-reordered glyph rows. Let set_cursor_from_row
17827 figure out where to put the cursor, and if it fails,
17828 give up. */
17829 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17831 if (!set_cursor_from_row (w, row, w->current_matrix,
17832 0, 0, 0, 0))
17834 clear_glyph_matrix (w->desired_matrix);
17835 return false;
17838 else
17840 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17841 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17843 for (; glyph < end
17844 && (!BUFFERP (glyph->object)
17845 || glyph->charpos < PT);
17846 glyph++)
17848 w->cursor.hpos++;
17849 w->cursor.x += glyph->pixel_width;
17855 /* Adjust window end. A null value of last_text_row means that
17856 the window end is in reused rows which in turn means that
17857 only its vpos can have changed. */
17858 if (last_text_row)
17859 adjust_window_ends (w, last_text_row, false);
17860 else
17861 w->window_end_vpos -= nrows_scrolled;
17863 w->window_end_valid = false;
17864 w->desired_matrix->no_scrolling_p = true;
17866 #ifdef GLYPH_DEBUG
17867 debug_method_add (w, "try_window_reusing_current_matrix 2");
17868 #endif
17869 return true;
17872 return false;
17877 /************************************************************************
17878 Window redisplay reusing current matrix when buffer has changed
17879 ************************************************************************/
17881 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17882 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17883 ptrdiff_t *, ptrdiff_t *);
17884 static struct glyph_row *
17885 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17886 struct glyph_row *);
17889 /* Return the last row in MATRIX displaying text. If row START is
17890 non-null, start searching with that row. IT gives the dimensions
17891 of the display. Value is null if matrix is empty; otherwise it is
17892 a pointer to the row found. */
17894 static struct glyph_row *
17895 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17896 struct glyph_row *start)
17898 struct glyph_row *row, *row_found;
17900 /* Set row_found to the last row in IT->w's current matrix
17901 displaying text. The loop looks funny but think of partially
17902 visible lines. */
17903 row_found = NULL;
17904 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17905 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17907 eassert (row->enabled_p);
17908 row_found = row;
17909 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17910 break;
17911 ++row;
17914 return row_found;
17918 /* Return the last row in the current matrix of W that is not affected
17919 by changes at the start of current_buffer that occurred since W's
17920 current matrix was built. Value is null if no such row exists.
17922 BEG_UNCHANGED us the number of characters unchanged at the start of
17923 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17924 first changed character in current_buffer. Characters at positions <
17925 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17926 when the current matrix was built. */
17928 static struct glyph_row *
17929 find_last_unchanged_at_beg_row (struct window *w)
17931 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17932 struct glyph_row *row;
17933 struct glyph_row *row_found = NULL;
17934 int yb = window_text_bottom_y (w);
17936 /* Find the last row displaying unchanged text. */
17937 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17938 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17939 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17940 ++row)
17942 if (/* If row ends before first_changed_pos, it is unchanged,
17943 except in some case. */
17944 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17945 /* When row ends in ZV and we write at ZV it is not
17946 unchanged. */
17947 && !row->ends_at_zv_p
17948 /* When first_changed_pos is the end of a continued line,
17949 row is not unchanged because it may be no longer
17950 continued. */
17951 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17952 && (row->continued_p
17953 || row->exact_window_width_line_p))
17954 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17955 needs to be recomputed, so don't consider this row as
17956 unchanged. This happens when the last line was
17957 bidi-reordered and was killed immediately before this
17958 redisplay cycle. In that case, ROW->end stores the
17959 buffer position of the first visual-order character of
17960 the killed text, which is now beyond ZV. */
17961 && CHARPOS (row->end.pos) <= ZV)
17962 row_found = row;
17964 /* Stop if last visible row. */
17965 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17966 break;
17969 return row_found;
17973 /* Find the first glyph row in the current matrix of W that is not
17974 affected by changes at the end of current_buffer since the
17975 time W's current matrix was built.
17977 Return in *DELTA the number of chars by which buffer positions in
17978 unchanged text at the end of current_buffer must be adjusted.
17980 Return in *DELTA_BYTES the corresponding number of bytes.
17982 Value is null if no such row exists, i.e. all rows are affected by
17983 changes. */
17985 static struct glyph_row *
17986 find_first_unchanged_at_end_row (struct window *w,
17987 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17989 struct glyph_row *row;
17990 struct glyph_row *row_found = NULL;
17992 *delta = *delta_bytes = 0;
17994 /* Display must not have been paused, otherwise the current matrix
17995 is not up to date. */
17996 eassert (w->window_end_valid);
17998 /* A value of window_end_pos >= END_UNCHANGED means that the window
17999 end is in the range of changed text. If so, there is no
18000 unchanged row at the end of W's current matrix. */
18001 if (w->window_end_pos >= END_UNCHANGED)
18002 return NULL;
18004 /* Set row to the last row in W's current matrix displaying text. */
18005 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18007 /* If matrix is entirely empty, no unchanged row exists. */
18008 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18010 /* The value of row is the last glyph row in the matrix having a
18011 meaningful buffer position in it. The end position of row
18012 corresponds to window_end_pos. This allows us to translate
18013 buffer positions in the current matrix to current buffer
18014 positions for characters not in changed text. */
18015 ptrdiff_t Z_old =
18016 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18017 ptrdiff_t Z_BYTE_old =
18018 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18019 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18020 struct glyph_row *first_text_row
18021 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18023 *delta = Z - Z_old;
18024 *delta_bytes = Z_BYTE - Z_BYTE_old;
18026 /* Set last_unchanged_pos to the buffer position of the last
18027 character in the buffer that has not been changed. Z is the
18028 index + 1 of the last character in current_buffer, i.e. by
18029 subtracting END_UNCHANGED we get the index of the last
18030 unchanged character, and we have to add BEG to get its buffer
18031 position. */
18032 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18033 last_unchanged_pos_old = last_unchanged_pos - *delta;
18035 /* Search backward from ROW for a row displaying a line that
18036 starts at a minimum position >= last_unchanged_pos_old. */
18037 for (; row > first_text_row; --row)
18039 /* This used to abort, but it can happen.
18040 It is ok to just stop the search instead here. KFS. */
18041 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18042 break;
18044 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18045 row_found = row;
18049 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18051 return row_found;
18055 /* Make sure that glyph rows in the current matrix of window W
18056 reference the same glyph memory as corresponding rows in the
18057 frame's frame matrix. This function is called after scrolling W's
18058 current matrix on a terminal frame in try_window_id and
18059 try_window_reusing_current_matrix. */
18061 static void
18062 sync_frame_with_window_matrix_rows (struct window *w)
18064 struct frame *f = XFRAME (w->frame);
18065 struct glyph_row *window_row, *window_row_end, *frame_row;
18067 /* Preconditions: W must be a leaf window and full-width. Its frame
18068 must have a frame matrix. */
18069 eassert (BUFFERP (w->contents));
18070 eassert (WINDOW_FULL_WIDTH_P (w));
18071 eassert (!FRAME_WINDOW_P (f));
18073 /* If W is a full-width window, glyph pointers in W's current matrix
18074 have, by definition, to be the same as glyph pointers in the
18075 corresponding frame matrix. Note that frame matrices have no
18076 marginal areas (see build_frame_matrix). */
18077 window_row = w->current_matrix->rows;
18078 window_row_end = window_row + w->current_matrix->nrows;
18079 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18080 while (window_row < window_row_end)
18082 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18083 struct glyph *end = window_row->glyphs[LAST_AREA];
18085 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18086 frame_row->glyphs[TEXT_AREA] = start;
18087 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18088 frame_row->glyphs[LAST_AREA] = end;
18090 /* Disable frame rows whose corresponding window rows have
18091 been disabled in try_window_id. */
18092 if (!window_row->enabled_p)
18093 frame_row->enabled_p = false;
18095 ++window_row, ++frame_row;
18100 /* Find the glyph row in window W containing CHARPOS. Consider all
18101 rows between START and END (not inclusive). END null means search
18102 all rows to the end of the display area of W. Value is the row
18103 containing CHARPOS or null. */
18105 struct glyph_row *
18106 row_containing_pos (struct window *w, ptrdiff_t charpos,
18107 struct glyph_row *start, struct glyph_row *end, int dy)
18109 struct glyph_row *row = start;
18110 struct glyph_row *best_row = NULL;
18111 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18112 int last_y;
18114 /* If we happen to start on a header-line, skip that. */
18115 if (row->mode_line_p)
18116 ++row;
18118 if ((end && row >= end) || !row->enabled_p)
18119 return NULL;
18121 last_y = window_text_bottom_y (w) - dy;
18123 while (true)
18125 /* Give up if we have gone too far. */
18126 if ((end && row >= end) || !row->enabled_p)
18127 return NULL;
18128 /* This formerly returned if they were equal.
18129 I think that both quantities are of a "last plus one" type;
18130 if so, when they are equal, the row is within the screen. -- rms. */
18131 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18132 return NULL;
18134 /* If it is in this row, return this row. */
18135 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18136 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18137 /* The end position of a row equals the start
18138 position of the next row. If CHARPOS is there, we
18139 would rather consider it displayed in the next
18140 line, except when this line ends in ZV. */
18141 && !row_for_charpos_p (row, charpos)))
18142 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18144 struct glyph *g;
18146 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18147 || (!best_row && !row->continued_p))
18148 return row;
18149 /* In bidi-reordered rows, there could be several rows whose
18150 edges surround CHARPOS, all of these rows belonging to
18151 the same continued line. We need to find the row which
18152 fits CHARPOS the best. */
18153 for (g = row->glyphs[TEXT_AREA];
18154 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18155 g++)
18157 if (!STRINGP (g->object))
18159 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18161 mindif = eabs (g->charpos - charpos);
18162 best_row = row;
18163 /* Exact match always wins. */
18164 if (mindif == 0)
18165 return best_row;
18170 else if (best_row && !row->continued_p)
18171 return best_row;
18172 ++row;
18177 /* Try to redisplay window W by reusing its existing display. W's
18178 current matrix must be up to date when this function is called,
18179 i.e., window_end_valid must be true.
18181 Value is
18183 >= 1 if successful, i.e. display has been updated
18184 specifically:
18185 1 means the changes were in front of a newline that precedes
18186 the window start, and the whole current matrix was reused
18187 2 means the changes were after the last position displayed
18188 in the window, and the whole current matrix was reused
18189 3 means portions of the current matrix were reused, while
18190 some of the screen lines were redrawn
18191 -1 if redisplay with same window start is known not to succeed
18192 0 if otherwise unsuccessful
18194 The following steps are performed:
18196 1. Find the last row in the current matrix of W that is not
18197 affected by changes at the start of current_buffer. If no such row
18198 is found, give up.
18200 2. Find the first row in W's current matrix that is not affected by
18201 changes at the end of current_buffer. Maybe there is no such row.
18203 3. Display lines beginning with the row + 1 found in step 1 to the
18204 row found in step 2 or, if step 2 didn't find a row, to the end of
18205 the window.
18207 4. If cursor is not known to appear on the window, give up.
18209 5. If display stopped at the row found in step 2, scroll the
18210 display and current matrix as needed.
18212 6. Maybe display some lines at the end of W, if we must. This can
18213 happen under various circumstances, like a partially visible line
18214 becoming fully visible, or because newly displayed lines are displayed
18215 in smaller font sizes.
18217 7. Update W's window end information. */
18219 static int
18220 try_window_id (struct window *w)
18222 struct frame *f = XFRAME (w->frame);
18223 struct glyph_matrix *current_matrix = w->current_matrix;
18224 struct glyph_matrix *desired_matrix = w->desired_matrix;
18225 struct glyph_row *last_unchanged_at_beg_row;
18226 struct glyph_row *first_unchanged_at_end_row;
18227 struct glyph_row *row;
18228 struct glyph_row *bottom_row;
18229 int bottom_vpos;
18230 struct it it;
18231 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18232 int dvpos, dy;
18233 struct text_pos start_pos;
18234 struct run run;
18235 int first_unchanged_at_end_vpos = 0;
18236 struct glyph_row *last_text_row, *last_text_row_at_end;
18237 struct text_pos start;
18238 ptrdiff_t first_changed_charpos, last_changed_charpos;
18240 #ifdef GLYPH_DEBUG
18241 if (inhibit_try_window_id)
18242 return 0;
18243 #endif
18245 /* This is handy for debugging. */
18246 #if false
18247 #define GIVE_UP(X) \
18248 do { \
18249 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18250 return 0; \
18251 } while (false)
18252 #else
18253 #define GIVE_UP(X) return 0
18254 #endif
18256 SET_TEXT_POS_FROM_MARKER (start, w->start);
18258 /* Don't use this for mini-windows because these can show
18259 messages and mini-buffers, and we don't handle that here. */
18260 if (MINI_WINDOW_P (w))
18261 GIVE_UP (1);
18263 /* This flag is used to prevent redisplay optimizations. */
18264 if (windows_or_buffers_changed || f->cursor_type_changed)
18265 GIVE_UP (2);
18267 /* This function's optimizations cannot be used if overlays have
18268 changed in the buffer displayed by the window, so give up if they
18269 have. */
18270 if (w->last_overlay_modified != OVERLAY_MODIFF)
18271 GIVE_UP (200);
18273 /* Verify that narrowing has not changed.
18274 Also verify that we were not told to prevent redisplay optimizations.
18275 It would be nice to further
18276 reduce the number of cases where this prevents try_window_id. */
18277 if (current_buffer->clip_changed
18278 || current_buffer->prevent_redisplay_optimizations_p)
18279 GIVE_UP (3);
18281 /* Window must either use window-based redisplay or be full width. */
18282 if (!FRAME_WINDOW_P (f)
18283 && (!FRAME_LINE_INS_DEL_OK (f)
18284 || !WINDOW_FULL_WIDTH_P (w)))
18285 GIVE_UP (4);
18287 /* Give up if point is known NOT to appear in W. */
18288 if (PT < CHARPOS (start))
18289 GIVE_UP (5);
18291 /* Another way to prevent redisplay optimizations. */
18292 if (w->last_modified == 0)
18293 GIVE_UP (6);
18295 /* Verify that window is not hscrolled. */
18296 if (w->hscroll != 0)
18297 GIVE_UP (7);
18299 /* Verify that display wasn't paused. */
18300 if (!w->window_end_valid)
18301 GIVE_UP (8);
18303 /* Likewise if highlighting trailing whitespace. */
18304 if (!NILP (Vshow_trailing_whitespace))
18305 GIVE_UP (11);
18307 /* Can't use this if overlay arrow position and/or string have
18308 changed. */
18309 if (overlay_arrows_changed_p ())
18310 GIVE_UP (12);
18312 /* When word-wrap is on, adding a space to the first word of a
18313 wrapped line can change the wrap position, altering the line
18314 above it. It might be worthwhile to handle this more
18315 intelligently, but for now just redisplay from scratch. */
18316 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18317 GIVE_UP (21);
18319 /* Under bidi reordering, adding or deleting a character in the
18320 beginning of a paragraph, before the first strong directional
18321 character, can change the base direction of the paragraph (unless
18322 the buffer specifies a fixed paragraph direction), which will
18323 require redisplaying the whole paragraph. It might be worthwhile
18324 to find the paragraph limits and widen the range of redisplayed
18325 lines to that, but for now just give up this optimization and
18326 redisplay from scratch. */
18327 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18328 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18329 GIVE_UP (22);
18331 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18332 to that variable require thorough redisplay. */
18333 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18334 GIVE_UP (23);
18336 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18337 only if buffer has really changed. The reason is that the gap is
18338 initially at Z for freshly visited files. The code below would
18339 set end_unchanged to 0 in that case. */
18340 if (MODIFF > SAVE_MODIFF
18341 /* This seems to happen sometimes after saving a buffer. */
18342 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18344 if (GPT - BEG < BEG_UNCHANGED)
18345 BEG_UNCHANGED = GPT - BEG;
18346 if (Z - GPT < END_UNCHANGED)
18347 END_UNCHANGED = Z - GPT;
18350 /* The position of the first and last character that has been changed. */
18351 first_changed_charpos = BEG + BEG_UNCHANGED;
18352 last_changed_charpos = Z - END_UNCHANGED;
18354 /* If window starts after a line end, and the last change is in
18355 front of that newline, then changes don't affect the display.
18356 This case happens with stealth-fontification. Note that although
18357 the display is unchanged, glyph positions in the matrix have to
18358 be adjusted, of course. */
18359 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18360 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18361 && ((last_changed_charpos < CHARPOS (start)
18362 && CHARPOS (start) == BEGV)
18363 || (last_changed_charpos < CHARPOS (start) - 1
18364 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18366 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18367 struct glyph_row *r0;
18369 /* Compute how many chars/bytes have been added to or removed
18370 from the buffer. */
18371 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18372 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18373 Z_delta = Z - Z_old;
18374 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18376 /* Give up if PT is not in the window. Note that it already has
18377 been checked at the start of try_window_id that PT is not in
18378 front of the window start. */
18379 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18380 GIVE_UP (13);
18382 /* If window start is unchanged, we can reuse the whole matrix
18383 as is, after adjusting glyph positions. No need to compute
18384 the window end again, since its offset from Z hasn't changed. */
18385 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18386 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18387 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18388 /* PT must not be in a partially visible line. */
18389 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18390 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18392 /* Adjust positions in the glyph matrix. */
18393 if (Z_delta || Z_delta_bytes)
18395 struct glyph_row *r1
18396 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18397 increment_matrix_positions (w->current_matrix,
18398 MATRIX_ROW_VPOS (r0, current_matrix),
18399 MATRIX_ROW_VPOS (r1, current_matrix),
18400 Z_delta, Z_delta_bytes);
18403 /* Set the cursor. */
18404 row = row_containing_pos (w, PT, r0, NULL, 0);
18405 if (row)
18406 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18407 return 1;
18411 /* Handle the case that changes are all below what is displayed in
18412 the window, and that PT is in the window. This shortcut cannot
18413 be taken if ZV is visible in the window, and text has been added
18414 there that is visible in the window. */
18415 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18416 /* ZV is not visible in the window, or there are no
18417 changes at ZV, actually. */
18418 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18419 || first_changed_charpos == last_changed_charpos))
18421 struct glyph_row *r0;
18423 /* Give up if PT is not in the window. Note that it already has
18424 been checked at the start of try_window_id that PT is not in
18425 front of the window start. */
18426 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18427 GIVE_UP (14);
18429 /* If window start is unchanged, we can reuse the whole matrix
18430 as is, without changing glyph positions since no text has
18431 been added/removed in front of the window end. */
18432 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18433 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18434 /* PT must not be in a partially visible line. */
18435 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18436 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18438 /* We have to compute the window end anew since text
18439 could have been added/removed after it. */
18440 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18441 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18443 /* Set the cursor. */
18444 row = row_containing_pos (w, PT, r0, NULL, 0);
18445 if (row)
18446 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18447 return 2;
18451 /* Give up if window start is in the changed area.
18453 The condition used to read
18455 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18457 but why that was tested escapes me at the moment. */
18458 if (CHARPOS (start) >= first_changed_charpos
18459 && CHARPOS (start) <= last_changed_charpos)
18460 GIVE_UP (15);
18462 /* Check that window start agrees with the start of the first glyph
18463 row in its current matrix. Check this after we know the window
18464 start is not in changed text, otherwise positions would not be
18465 comparable. */
18466 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18467 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18468 GIVE_UP (16);
18470 /* Give up if the window ends in strings. Overlay strings
18471 at the end are difficult to handle, so don't try. */
18472 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18473 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18474 GIVE_UP (20);
18476 /* Compute the position at which we have to start displaying new
18477 lines. Some of the lines at the top of the window might be
18478 reusable because they are not displaying changed text. Find the
18479 last row in W's current matrix not affected by changes at the
18480 start of current_buffer. Value is null if changes start in the
18481 first line of window. */
18482 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18483 if (last_unchanged_at_beg_row)
18485 /* Avoid starting to display in the middle of a character, a TAB
18486 for instance. This is easier than to set up the iterator
18487 exactly, and it's not a frequent case, so the additional
18488 effort wouldn't really pay off. */
18489 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18490 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18491 && last_unchanged_at_beg_row > w->current_matrix->rows)
18492 --last_unchanged_at_beg_row;
18494 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18495 GIVE_UP (17);
18497 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18498 GIVE_UP (18);
18499 start_pos = it.current.pos;
18501 /* Start displaying new lines in the desired matrix at the same
18502 vpos we would use in the current matrix, i.e. below
18503 last_unchanged_at_beg_row. */
18504 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18505 current_matrix);
18506 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18507 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18509 eassert (it.hpos == 0 && it.current_x == 0);
18511 else
18513 /* There are no reusable lines at the start of the window.
18514 Start displaying in the first text line. */
18515 start_display (&it, w, start);
18516 it.vpos = it.first_vpos;
18517 start_pos = it.current.pos;
18520 /* Find the first row that is not affected by changes at the end of
18521 the buffer. Value will be null if there is no unchanged row, in
18522 which case we must redisplay to the end of the window. delta
18523 will be set to the value by which buffer positions beginning with
18524 first_unchanged_at_end_row have to be adjusted due to text
18525 changes. */
18526 first_unchanged_at_end_row
18527 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18528 IF_DEBUG (debug_delta = delta);
18529 IF_DEBUG (debug_delta_bytes = delta_bytes);
18531 /* Set stop_pos to the buffer position up to which we will have to
18532 display new lines. If first_unchanged_at_end_row != NULL, this
18533 is the buffer position of the start of the line displayed in that
18534 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18535 that we don't stop at a buffer position. */
18536 stop_pos = 0;
18537 if (first_unchanged_at_end_row)
18539 eassert (last_unchanged_at_beg_row == NULL
18540 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18542 /* If this is a continuation line, move forward to the next one
18543 that isn't. Changes in lines above affect this line.
18544 Caution: this may move first_unchanged_at_end_row to a row
18545 not displaying text. */
18546 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18547 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18548 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18549 < it.last_visible_y))
18550 ++first_unchanged_at_end_row;
18552 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18553 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18554 >= it.last_visible_y))
18555 first_unchanged_at_end_row = NULL;
18556 else
18558 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18559 + delta);
18560 first_unchanged_at_end_vpos
18561 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18562 eassert (stop_pos >= Z - END_UNCHANGED);
18565 else if (last_unchanged_at_beg_row == NULL)
18566 GIVE_UP (19);
18569 #ifdef GLYPH_DEBUG
18571 /* Either there is no unchanged row at the end, or the one we have
18572 now displays text. This is a necessary condition for the window
18573 end pos calculation at the end of this function. */
18574 eassert (first_unchanged_at_end_row == NULL
18575 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18577 debug_last_unchanged_at_beg_vpos
18578 = (last_unchanged_at_beg_row
18579 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18580 : -1);
18581 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18583 #endif /* GLYPH_DEBUG */
18586 /* Display new lines. Set last_text_row to the last new line
18587 displayed which has text on it, i.e. might end up as being the
18588 line where the window_end_vpos is. */
18589 w->cursor.vpos = -1;
18590 last_text_row = NULL;
18591 overlay_arrow_seen = false;
18592 if (it.current_y < it.last_visible_y
18593 && !f->fonts_changed
18594 && (first_unchanged_at_end_row == NULL
18595 || IT_CHARPOS (it) < stop_pos))
18596 it.glyph_row->reversed_p = false;
18597 while (it.current_y < it.last_visible_y
18598 && !f->fonts_changed
18599 && (first_unchanged_at_end_row == NULL
18600 || IT_CHARPOS (it) < stop_pos))
18602 if (display_line (&it))
18603 last_text_row = it.glyph_row - 1;
18606 if (f->fonts_changed)
18607 return -1;
18609 /* The redisplay iterations in display_line above could have
18610 triggered font-lock, which could have done something that
18611 invalidates IT->w window's end-point information, on which we
18612 rely below. E.g., one package, which will remain unnamed, used
18613 to install a font-lock-fontify-region-function that called
18614 bury-buffer, whose side effect is to switch the buffer displayed
18615 by IT->w, and that predictably resets IT->w's window_end_valid
18616 flag, which we already tested at the entry to this function.
18617 Amply punish such packages/modes by giving up on this
18618 optimization in those cases. */
18619 if (!w->window_end_valid)
18621 clear_glyph_matrix (w->desired_matrix);
18622 return -1;
18625 /* Compute differences in buffer positions, y-positions etc. for
18626 lines reused at the bottom of the window. Compute what we can
18627 scroll. */
18628 if (first_unchanged_at_end_row
18629 /* No lines reused because we displayed everything up to the
18630 bottom of the window. */
18631 && it.current_y < it.last_visible_y)
18633 dvpos = (it.vpos
18634 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18635 current_matrix));
18636 dy = it.current_y - first_unchanged_at_end_row->y;
18637 run.current_y = first_unchanged_at_end_row->y;
18638 run.desired_y = run.current_y + dy;
18639 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18641 else
18643 delta = delta_bytes = dvpos = dy
18644 = run.current_y = run.desired_y = run.height = 0;
18645 first_unchanged_at_end_row = NULL;
18647 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18650 /* Find the cursor if not already found. We have to decide whether
18651 PT will appear on this window (it sometimes doesn't, but this is
18652 not a very frequent case.) This decision has to be made before
18653 the current matrix is altered. A value of cursor.vpos < 0 means
18654 that PT is either in one of the lines beginning at
18655 first_unchanged_at_end_row or below the window. Don't care for
18656 lines that might be displayed later at the window end; as
18657 mentioned, this is not a frequent case. */
18658 if (w->cursor.vpos < 0)
18660 /* Cursor in unchanged rows at the top? */
18661 if (PT < CHARPOS (start_pos)
18662 && last_unchanged_at_beg_row)
18664 row = row_containing_pos (w, PT,
18665 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18666 last_unchanged_at_beg_row + 1, 0);
18667 if (row)
18668 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18671 /* Start from first_unchanged_at_end_row looking for PT. */
18672 else if (first_unchanged_at_end_row)
18674 row = row_containing_pos (w, PT - delta,
18675 first_unchanged_at_end_row, NULL, 0);
18676 if (row)
18677 set_cursor_from_row (w, row, w->current_matrix, delta,
18678 delta_bytes, dy, dvpos);
18681 /* Give up if cursor was not found. */
18682 if (w->cursor.vpos < 0)
18684 clear_glyph_matrix (w->desired_matrix);
18685 return -1;
18689 /* Don't let the cursor end in the scroll margins. */
18691 int this_scroll_margin, cursor_height;
18692 int frame_line_height = default_line_pixel_height (w);
18693 int window_total_lines
18694 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18696 this_scroll_margin =
18697 max (0, min (scroll_margin, window_total_lines / 4));
18698 this_scroll_margin *= frame_line_height;
18699 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18701 if ((w->cursor.y < this_scroll_margin
18702 && CHARPOS (start) > BEGV)
18703 /* Old redisplay didn't take scroll margin into account at the bottom,
18704 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18705 || (w->cursor.y + (make_cursor_line_fully_visible_p
18706 ? cursor_height + this_scroll_margin
18707 : 1)) > it.last_visible_y)
18709 w->cursor.vpos = -1;
18710 clear_glyph_matrix (w->desired_matrix);
18711 return -1;
18715 /* Scroll the display. Do it before changing the current matrix so
18716 that xterm.c doesn't get confused about where the cursor glyph is
18717 found. */
18718 if (dy && run.height)
18720 update_begin (f);
18722 if (FRAME_WINDOW_P (f))
18724 FRAME_RIF (f)->update_window_begin_hook (w);
18725 FRAME_RIF (f)->clear_window_mouse_face (w);
18726 FRAME_RIF (f)->scroll_run_hook (w, &run);
18727 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18729 else
18731 /* Terminal frame. In this case, dvpos gives the number of
18732 lines to scroll by; dvpos < 0 means scroll up. */
18733 int from_vpos
18734 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18735 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18736 int end = (WINDOW_TOP_EDGE_LINE (w)
18737 + WINDOW_WANTS_HEADER_LINE_P (w)
18738 + window_internal_height (w));
18740 #if defined (HAVE_GPM) || defined (MSDOS)
18741 x_clear_window_mouse_face (w);
18742 #endif
18743 /* Perform the operation on the screen. */
18744 if (dvpos > 0)
18746 /* Scroll last_unchanged_at_beg_row to the end of the
18747 window down dvpos lines. */
18748 set_terminal_window (f, end);
18750 /* On dumb terminals delete dvpos lines at the end
18751 before inserting dvpos empty lines. */
18752 if (!FRAME_SCROLL_REGION_OK (f))
18753 ins_del_lines (f, end - dvpos, -dvpos);
18755 /* Insert dvpos empty lines in front of
18756 last_unchanged_at_beg_row. */
18757 ins_del_lines (f, from, dvpos);
18759 else if (dvpos < 0)
18761 /* Scroll up last_unchanged_at_beg_vpos to the end of
18762 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18763 set_terminal_window (f, end);
18765 /* Delete dvpos lines in front of
18766 last_unchanged_at_beg_vpos. ins_del_lines will set
18767 the cursor to the given vpos and emit |dvpos| delete
18768 line sequences. */
18769 ins_del_lines (f, from + dvpos, dvpos);
18771 /* On a dumb terminal insert dvpos empty lines at the
18772 end. */
18773 if (!FRAME_SCROLL_REGION_OK (f))
18774 ins_del_lines (f, end + dvpos, -dvpos);
18777 set_terminal_window (f, 0);
18780 update_end (f);
18783 /* Shift reused rows of the current matrix to the right position.
18784 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18785 text. */
18786 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18787 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18788 if (dvpos < 0)
18790 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18791 bottom_vpos, dvpos);
18792 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18793 bottom_vpos);
18795 else if (dvpos > 0)
18797 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18798 bottom_vpos, dvpos);
18799 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18800 first_unchanged_at_end_vpos + dvpos);
18803 /* For frame-based redisplay, make sure that current frame and window
18804 matrix are in sync with respect to glyph memory. */
18805 if (!FRAME_WINDOW_P (f))
18806 sync_frame_with_window_matrix_rows (w);
18808 /* Adjust buffer positions in reused rows. */
18809 if (delta || delta_bytes)
18810 increment_matrix_positions (current_matrix,
18811 first_unchanged_at_end_vpos + dvpos,
18812 bottom_vpos, delta, delta_bytes);
18814 /* Adjust Y positions. */
18815 if (dy)
18816 shift_glyph_matrix (w, current_matrix,
18817 first_unchanged_at_end_vpos + dvpos,
18818 bottom_vpos, dy);
18820 if (first_unchanged_at_end_row)
18822 first_unchanged_at_end_row += dvpos;
18823 if (first_unchanged_at_end_row->y >= it.last_visible_y
18824 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18825 first_unchanged_at_end_row = NULL;
18828 /* If scrolling up, there may be some lines to display at the end of
18829 the window. */
18830 last_text_row_at_end = NULL;
18831 if (dy < 0)
18833 /* Scrolling up can leave for example a partially visible line
18834 at the end of the window to be redisplayed. */
18835 /* Set last_row to the glyph row in the current matrix where the
18836 window end line is found. It has been moved up or down in
18837 the matrix by dvpos. */
18838 int last_vpos = w->window_end_vpos + dvpos;
18839 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18841 /* If last_row is the window end line, it should display text. */
18842 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18844 /* If window end line was partially visible before, begin
18845 displaying at that line. Otherwise begin displaying with the
18846 line following it. */
18847 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18849 init_to_row_start (&it, w, last_row);
18850 it.vpos = last_vpos;
18851 it.current_y = last_row->y;
18853 else
18855 init_to_row_end (&it, w, last_row);
18856 it.vpos = 1 + last_vpos;
18857 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18858 ++last_row;
18861 /* We may start in a continuation line. If so, we have to
18862 get the right continuation_lines_width and current_x. */
18863 it.continuation_lines_width = last_row->continuation_lines_width;
18864 it.hpos = it.current_x = 0;
18866 /* Display the rest of the lines at the window end. */
18867 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18868 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18870 /* Is it always sure that the display agrees with lines in
18871 the current matrix? I don't think so, so we mark rows
18872 displayed invalid in the current matrix by setting their
18873 enabled_p flag to false. */
18874 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18875 if (display_line (&it))
18876 last_text_row_at_end = it.glyph_row - 1;
18880 /* Update window_end_pos and window_end_vpos. */
18881 if (first_unchanged_at_end_row && !last_text_row_at_end)
18883 /* Window end line if one of the preserved rows from the current
18884 matrix. Set row to the last row displaying text in current
18885 matrix starting at first_unchanged_at_end_row, after
18886 scrolling. */
18887 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18888 row = find_last_row_displaying_text (w->current_matrix, &it,
18889 first_unchanged_at_end_row);
18890 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18891 adjust_window_ends (w, row, true);
18892 eassert (w->window_end_bytepos >= 0);
18893 IF_DEBUG (debug_method_add (w, "A"));
18895 else if (last_text_row_at_end)
18897 adjust_window_ends (w, last_text_row_at_end, false);
18898 eassert (w->window_end_bytepos >= 0);
18899 IF_DEBUG (debug_method_add (w, "B"));
18901 else if (last_text_row)
18903 /* We have displayed either to the end of the window or at the
18904 end of the window, i.e. the last row with text is to be found
18905 in the desired matrix. */
18906 adjust_window_ends (w, last_text_row, false);
18907 eassert (w->window_end_bytepos >= 0);
18909 else if (first_unchanged_at_end_row == NULL
18910 && last_text_row == NULL
18911 && last_text_row_at_end == NULL)
18913 /* Displayed to end of window, but no line containing text was
18914 displayed. Lines were deleted at the end of the window. */
18915 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18916 int vpos = w->window_end_vpos;
18917 struct glyph_row *current_row = current_matrix->rows + vpos;
18918 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18920 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18922 eassert (first_vpos <= vpos);
18923 if (desired_row->enabled_p)
18925 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18926 row = desired_row;
18928 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18929 row = current_row;
18932 w->window_end_vpos = vpos + 1;
18933 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18934 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18935 eassert (w->window_end_bytepos >= 0);
18936 IF_DEBUG (debug_method_add (w, "C"));
18938 else
18939 emacs_abort ();
18941 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18942 debug_end_vpos = w->window_end_vpos));
18944 /* Record that display has not been completed. */
18945 w->window_end_valid = false;
18946 w->desired_matrix->no_scrolling_p = true;
18947 return 3;
18949 #undef GIVE_UP
18954 /***********************************************************************
18955 More debugging support
18956 ***********************************************************************/
18958 #ifdef GLYPH_DEBUG
18960 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18961 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18962 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18965 /* Dump the contents of glyph matrix MATRIX on stderr.
18967 GLYPHS 0 means don't show glyph contents.
18968 GLYPHS 1 means show glyphs in short form
18969 GLYPHS > 1 means show glyphs in long form. */
18971 void
18972 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18974 int i;
18975 for (i = 0; i < matrix->nrows; ++i)
18976 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18980 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18981 the glyph row and area where the glyph comes from. */
18983 void
18984 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18986 if (glyph->type == CHAR_GLYPH
18987 || glyph->type == GLYPHLESS_GLYPH)
18989 fprintf (stderr,
18990 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18991 glyph - row->glyphs[TEXT_AREA],
18992 (glyph->type == CHAR_GLYPH
18993 ? 'C'
18994 : 'G'),
18995 glyph->charpos,
18996 (BUFFERP (glyph->object)
18997 ? 'B'
18998 : (STRINGP (glyph->object)
18999 ? 'S'
19000 : (NILP (glyph->object)
19001 ? '0'
19002 : '-'))),
19003 glyph->pixel_width,
19004 glyph->u.ch,
19005 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19006 ? glyph->u.ch
19007 : '.'),
19008 glyph->face_id,
19009 glyph->left_box_line_p,
19010 glyph->right_box_line_p);
19012 else if (glyph->type == STRETCH_GLYPH)
19014 fprintf (stderr,
19015 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19016 glyph - row->glyphs[TEXT_AREA],
19017 'S',
19018 glyph->charpos,
19019 (BUFFERP (glyph->object)
19020 ? 'B'
19021 : (STRINGP (glyph->object)
19022 ? 'S'
19023 : (NILP (glyph->object)
19024 ? '0'
19025 : '-'))),
19026 glyph->pixel_width,
19028 ' ',
19029 glyph->face_id,
19030 glyph->left_box_line_p,
19031 glyph->right_box_line_p);
19033 else if (glyph->type == IMAGE_GLYPH)
19035 fprintf (stderr,
19036 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19037 glyph - row->glyphs[TEXT_AREA],
19038 'I',
19039 glyph->charpos,
19040 (BUFFERP (glyph->object)
19041 ? 'B'
19042 : (STRINGP (glyph->object)
19043 ? 'S'
19044 : (NILP (glyph->object)
19045 ? '0'
19046 : '-'))),
19047 glyph->pixel_width,
19048 glyph->u.img_id,
19049 '.',
19050 glyph->face_id,
19051 glyph->left_box_line_p,
19052 glyph->right_box_line_p);
19054 else if (glyph->type == COMPOSITE_GLYPH)
19056 fprintf (stderr,
19057 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19058 glyph - row->glyphs[TEXT_AREA],
19059 '+',
19060 glyph->charpos,
19061 (BUFFERP (glyph->object)
19062 ? 'B'
19063 : (STRINGP (glyph->object)
19064 ? 'S'
19065 : (NILP (glyph->object)
19066 ? '0'
19067 : '-'))),
19068 glyph->pixel_width,
19069 glyph->u.cmp.id);
19070 if (glyph->u.cmp.automatic)
19071 fprintf (stderr,
19072 "[%d-%d]",
19073 glyph->slice.cmp.from, glyph->slice.cmp.to);
19074 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19075 glyph->face_id,
19076 glyph->left_box_line_p,
19077 glyph->right_box_line_p);
19079 else if (glyph->type == XWIDGET_GLYPH)
19081 #ifndef HAVE_XWIDGETS
19082 eassume (false);
19083 #else
19084 fprintf (stderr,
19085 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19086 glyph - row->glyphs[TEXT_AREA],
19087 'X',
19088 glyph->charpos,
19089 (BUFFERP (glyph->object)
19090 ? 'B'
19091 : (STRINGP (glyph->object)
19092 ? 'S'
19093 : '-')),
19094 glyph->pixel_width,
19095 glyph->u.xwidget,
19096 '.',
19097 glyph->face_id,
19098 glyph->left_box_line_p,
19099 glyph->right_box_line_p);
19100 #endif
19105 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19106 GLYPHS 0 means don't show glyph contents.
19107 GLYPHS 1 means show glyphs in short form
19108 GLYPHS > 1 means show glyphs in long form. */
19110 void
19111 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19113 if (glyphs != 1)
19115 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19116 fprintf (stderr, "==============================================================================\n");
19118 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19119 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19120 vpos,
19121 MATRIX_ROW_START_CHARPOS (row),
19122 MATRIX_ROW_END_CHARPOS (row),
19123 row->used[TEXT_AREA],
19124 row->contains_overlapping_glyphs_p,
19125 row->enabled_p,
19126 row->truncated_on_left_p,
19127 row->truncated_on_right_p,
19128 row->continued_p,
19129 MATRIX_ROW_CONTINUATION_LINE_P (row),
19130 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19131 row->ends_at_zv_p,
19132 row->fill_line_p,
19133 row->ends_in_middle_of_char_p,
19134 row->starts_in_middle_of_char_p,
19135 row->mouse_face_p,
19136 row->x,
19137 row->y,
19138 row->pixel_width,
19139 row->height,
19140 row->visible_height,
19141 row->ascent,
19142 row->phys_ascent);
19143 /* The next 3 lines should align to "Start" in the header. */
19144 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19145 row->end.overlay_string_index,
19146 row->continuation_lines_width);
19147 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19148 CHARPOS (row->start.string_pos),
19149 CHARPOS (row->end.string_pos));
19150 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19151 row->end.dpvec_index);
19154 if (glyphs > 1)
19156 int area;
19158 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19160 struct glyph *glyph = row->glyphs[area];
19161 struct glyph *glyph_end = glyph + row->used[area];
19163 /* Glyph for a line end in text. */
19164 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19165 ++glyph_end;
19167 if (glyph < glyph_end)
19168 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19170 for (; glyph < glyph_end; ++glyph)
19171 dump_glyph (row, glyph, area);
19174 else if (glyphs == 1)
19176 int area;
19177 char s[SHRT_MAX + 4];
19179 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19181 int i;
19183 for (i = 0; i < row->used[area]; ++i)
19185 struct glyph *glyph = row->glyphs[area] + i;
19186 if (i == row->used[area] - 1
19187 && area == TEXT_AREA
19188 && NILP (glyph->object)
19189 && glyph->type == CHAR_GLYPH
19190 && glyph->u.ch == ' ')
19192 strcpy (&s[i], "[\\n]");
19193 i += 4;
19195 else if (glyph->type == CHAR_GLYPH
19196 && glyph->u.ch < 0x80
19197 && glyph->u.ch >= ' ')
19198 s[i] = glyph->u.ch;
19199 else
19200 s[i] = '.';
19203 s[i] = '\0';
19204 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19210 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19211 Sdump_glyph_matrix, 0, 1, "p",
19212 doc: /* Dump the current matrix of the selected window to stderr.
19213 Shows contents of glyph row structures. With non-nil
19214 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19215 glyphs in short form, otherwise show glyphs in long form.
19217 Interactively, no argument means show glyphs in short form;
19218 with numeric argument, its value is passed as the GLYPHS flag. */)
19219 (Lisp_Object glyphs)
19221 struct window *w = XWINDOW (selected_window);
19222 struct buffer *buffer = XBUFFER (w->contents);
19224 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19225 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19226 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19227 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19228 fprintf (stderr, "=============================================\n");
19229 dump_glyph_matrix (w->current_matrix,
19230 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19231 return Qnil;
19235 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19236 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19237 Only text-mode frames have frame glyph matrices. */)
19238 (void)
19240 struct frame *f = XFRAME (selected_frame);
19242 if (f->current_matrix)
19243 dump_glyph_matrix (f->current_matrix, 1);
19244 else
19245 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19246 return Qnil;
19250 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19251 doc: /* Dump glyph row ROW to stderr.
19252 GLYPH 0 means don't dump glyphs.
19253 GLYPH 1 means dump glyphs in short form.
19254 GLYPH > 1 or omitted means dump glyphs in long form. */)
19255 (Lisp_Object row, Lisp_Object glyphs)
19257 struct glyph_matrix *matrix;
19258 EMACS_INT vpos;
19260 CHECK_NUMBER (row);
19261 matrix = XWINDOW (selected_window)->current_matrix;
19262 vpos = XINT (row);
19263 if (vpos >= 0 && vpos < matrix->nrows)
19264 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19265 vpos,
19266 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19267 return Qnil;
19271 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19272 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19273 GLYPH 0 means don't dump glyphs.
19274 GLYPH 1 means dump glyphs in short form.
19275 GLYPH > 1 or omitted means dump glyphs in long form.
19277 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19278 do nothing. */)
19279 (Lisp_Object row, Lisp_Object glyphs)
19281 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19282 struct frame *sf = SELECTED_FRAME ();
19283 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19284 EMACS_INT vpos;
19286 CHECK_NUMBER (row);
19287 vpos = XINT (row);
19288 if (vpos >= 0 && vpos < m->nrows)
19289 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19290 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19291 #endif
19292 return Qnil;
19296 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19297 doc: /* Toggle tracing of redisplay.
19298 With ARG, turn tracing on if and only if ARG is positive. */)
19299 (Lisp_Object arg)
19301 if (NILP (arg))
19302 trace_redisplay_p = !trace_redisplay_p;
19303 else
19305 arg = Fprefix_numeric_value (arg);
19306 trace_redisplay_p = XINT (arg) > 0;
19309 return Qnil;
19313 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19314 doc: /* Like `format', but print result to stderr.
19315 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19316 (ptrdiff_t nargs, Lisp_Object *args)
19318 Lisp_Object s = Fformat (nargs, args);
19319 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19320 return Qnil;
19323 #endif /* GLYPH_DEBUG */
19327 /***********************************************************************
19328 Building Desired Matrix Rows
19329 ***********************************************************************/
19331 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19332 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19334 static struct glyph_row *
19335 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19337 struct frame *f = XFRAME (WINDOW_FRAME (w));
19338 struct buffer *buffer = XBUFFER (w->contents);
19339 struct buffer *old = current_buffer;
19340 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19341 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19342 const unsigned char *arrow_end = arrow_string + arrow_len;
19343 const unsigned char *p;
19344 struct it it;
19345 bool multibyte_p;
19346 int n_glyphs_before;
19348 set_buffer_temp (buffer);
19349 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19350 scratch_glyph_row.reversed_p = false;
19351 it.glyph_row->used[TEXT_AREA] = 0;
19352 SET_TEXT_POS (it.position, 0, 0);
19354 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19355 p = arrow_string;
19356 while (p < arrow_end)
19358 Lisp_Object face, ilisp;
19360 /* Get the next character. */
19361 if (multibyte_p)
19362 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19363 else
19365 it.c = it.char_to_display = *p, it.len = 1;
19366 if (! ASCII_CHAR_P (it.c))
19367 it.char_to_display = BYTE8_TO_CHAR (it.c);
19369 p += it.len;
19371 /* Get its face. */
19372 ilisp = make_number (p - arrow_string);
19373 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19374 it.face_id = compute_char_face (f, it.char_to_display, face);
19376 /* Compute its width, get its glyphs. */
19377 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19378 SET_TEXT_POS (it.position, -1, -1);
19379 PRODUCE_GLYPHS (&it);
19381 /* If this character doesn't fit any more in the line, we have
19382 to remove some glyphs. */
19383 if (it.current_x > it.last_visible_x)
19385 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19386 break;
19390 set_buffer_temp (old);
19391 return it.glyph_row;
19395 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19396 glyphs to insert is determined by produce_special_glyphs. */
19398 static void
19399 insert_left_trunc_glyphs (struct it *it)
19401 struct it truncate_it;
19402 struct glyph *from, *end, *to, *toend;
19404 eassert (!FRAME_WINDOW_P (it->f)
19405 || (!it->glyph_row->reversed_p
19406 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19407 || (it->glyph_row->reversed_p
19408 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19410 /* Get the truncation glyphs. */
19411 truncate_it = *it;
19412 truncate_it.current_x = 0;
19413 truncate_it.face_id = DEFAULT_FACE_ID;
19414 truncate_it.glyph_row = &scratch_glyph_row;
19415 truncate_it.area = TEXT_AREA;
19416 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19417 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19418 truncate_it.object = Qnil;
19419 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19421 /* Overwrite glyphs from IT with truncation glyphs. */
19422 if (!it->glyph_row->reversed_p)
19424 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19426 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19427 end = from + tused;
19428 to = it->glyph_row->glyphs[TEXT_AREA];
19429 toend = to + it->glyph_row->used[TEXT_AREA];
19430 if (FRAME_WINDOW_P (it->f))
19432 /* On GUI frames, when variable-size fonts are displayed,
19433 the truncation glyphs may need more pixels than the row's
19434 glyphs they overwrite. We overwrite more glyphs to free
19435 enough screen real estate, and enlarge the stretch glyph
19436 on the right (see display_line), if there is one, to
19437 preserve the screen position of the truncation glyphs on
19438 the right. */
19439 int w = 0;
19440 struct glyph *g = to;
19441 short used;
19443 /* The first glyph could be partially visible, in which case
19444 it->glyph_row->x will be negative. But we want the left
19445 truncation glyphs to be aligned at the left margin of the
19446 window, so we override the x coordinate at which the row
19447 will begin. */
19448 it->glyph_row->x = 0;
19449 while (g < toend && w < it->truncation_pixel_width)
19451 w += g->pixel_width;
19452 ++g;
19454 if (g - to - tused > 0)
19456 memmove (to + tused, g, (toend - g) * sizeof(*g));
19457 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19459 used = it->glyph_row->used[TEXT_AREA];
19460 if (it->glyph_row->truncated_on_right_p
19461 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19462 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19463 == STRETCH_GLYPH)
19465 int extra = w - it->truncation_pixel_width;
19467 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19471 while (from < end)
19472 *to++ = *from++;
19474 /* There may be padding glyphs left over. Overwrite them too. */
19475 if (!FRAME_WINDOW_P (it->f))
19477 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19479 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19480 while (from < end)
19481 *to++ = *from++;
19485 if (to > toend)
19486 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19488 else
19490 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19492 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19493 that back to front. */
19494 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19495 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19496 toend = it->glyph_row->glyphs[TEXT_AREA];
19497 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19498 if (FRAME_WINDOW_P (it->f))
19500 int w = 0;
19501 struct glyph *g = to;
19503 while (g >= toend && w < it->truncation_pixel_width)
19505 w += g->pixel_width;
19506 --g;
19508 if (to - g - tused > 0)
19509 to = g + tused;
19510 if (it->glyph_row->truncated_on_right_p
19511 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19512 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19514 int extra = w - it->truncation_pixel_width;
19516 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19520 while (from >= end && to >= toend)
19521 *to-- = *from--;
19522 if (!FRAME_WINDOW_P (it->f))
19524 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19526 from =
19527 truncate_it.glyph_row->glyphs[TEXT_AREA]
19528 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19529 while (from >= end && to >= toend)
19530 *to-- = *from--;
19533 if (from >= end)
19535 /* Need to free some room before prepending additional
19536 glyphs. */
19537 int move_by = from - end + 1;
19538 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19539 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19541 for ( ; g >= g0; g--)
19542 g[move_by] = *g;
19543 while (from >= end)
19544 *to-- = *from--;
19545 it->glyph_row->used[TEXT_AREA] += move_by;
19550 /* Compute the hash code for ROW. */
19551 unsigned
19552 row_hash (struct glyph_row *row)
19554 int area, k;
19555 unsigned hashval = 0;
19557 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19558 for (k = 0; k < row->used[area]; ++k)
19559 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19560 + row->glyphs[area][k].u.val
19561 + row->glyphs[area][k].face_id
19562 + row->glyphs[area][k].padding_p
19563 + (row->glyphs[area][k].type << 2));
19565 return hashval;
19568 /* Compute the pixel height and width of IT->glyph_row.
19570 Most of the time, ascent and height of a display line will be equal
19571 to the max_ascent and max_height values of the display iterator
19572 structure. This is not the case if
19574 1. We hit ZV without displaying anything. In this case, max_ascent
19575 and max_height will be zero.
19577 2. We have some glyphs that don't contribute to the line height.
19578 (The glyph row flag contributes_to_line_height_p is for future
19579 pixmap extensions).
19581 The first case is easily covered by using default values because in
19582 these cases, the line height does not really matter, except that it
19583 must not be zero. */
19585 static void
19586 compute_line_metrics (struct it *it)
19588 struct glyph_row *row = it->glyph_row;
19590 if (FRAME_WINDOW_P (it->f))
19592 int i, min_y, max_y;
19594 /* The line may consist of one space only, that was added to
19595 place the cursor on it. If so, the row's height hasn't been
19596 computed yet. */
19597 if (row->height == 0)
19599 if (it->max_ascent + it->max_descent == 0)
19600 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19601 row->ascent = it->max_ascent;
19602 row->height = it->max_ascent + it->max_descent;
19603 row->phys_ascent = it->max_phys_ascent;
19604 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19605 row->extra_line_spacing = it->max_extra_line_spacing;
19608 /* Compute the width of this line. */
19609 row->pixel_width = row->x;
19610 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19611 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19613 eassert (row->pixel_width >= 0);
19614 eassert (row->ascent >= 0 && row->height > 0);
19616 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19617 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19619 /* If first line's physical ascent is larger than its logical
19620 ascent, use the physical ascent, and make the row taller.
19621 This makes accented characters fully visible. */
19622 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19623 && row->phys_ascent > row->ascent)
19625 row->height += row->phys_ascent - row->ascent;
19626 row->ascent = row->phys_ascent;
19629 /* Compute how much of the line is visible. */
19630 row->visible_height = row->height;
19632 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19633 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19635 if (row->y < min_y)
19636 row->visible_height -= min_y - row->y;
19637 if (row->y + row->height > max_y)
19638 row->visible_height -= row->y + row->height - max_y;
19640 else
19642 row->pixel_width = row->used[TEXT_AREA];
19643 if (row->continued_p)
19644 row->pixel_width -= it->continuation_pixel_width;
19645 else if (row->truncated_on_right_p)
19646 row->pixel_width -= it->truncation_pixel_width;
19647 row->ascent = row->phys_ascent = 0;
19648 row->height = row->phys_height = row->visible_height = 1;
19649 row->extra_line_spacing = 0;
19652 /* Compute a hash code for this row. */
19653 row->hash = row_hash (row);
19655 it->max_ascent = it->max_descent = 0;
19656 it->max_phys_ascent = it->max_phys_descent = 0;
19660 /* Append one space to the glyph row of iterator IT if doing a
19661 window-based redisplay. The space has the same face as
19662 IT->face_id. Value is true if a space was added.
19664 This function is called to make sure that there is always one glyph
19665 at the end of a glyph row that the cursor can be set on under
19666 window-systems. (If there weren't such a glyph we would not know
19667 how wide and tall a box cursor should be displayed).
19669 At the same time this space let's a nicely handle clearing to the
19670 end of the line if the row ends in italic text. */
19672 static bool
19673 append_space_for_newline (struct it *it, bool default_face_p)
19675 if (FRAME_WINDOW_P (it->f))
19677 int n = it->glyph_row->used[TEXT_AREA];
19679 if (it->glyph_row->glyphs[TEXT_AREA] + n
19680 < it->glyph_row->glyphs[1 + TEXT_AREA])
19682 /* Save some values that must not be changed.
19683 Must save IT->c and IT->len because otherwise
19684 ITERATOR_AT_END_P wouldn't work anymore after
19685 append_space_for_newline has been called. */
19686 enum display_element_type saved_what = it->what;
19687 int saved_c = it->c, saved_len = it->len;
19688 int saved_char_to_display = it->char_to_display;
19689 int saved_x = it->current_x;
19690 int saved_face_id = it->face_id;
19691 bool saved_box_end = it->end_of_box_run_p;
19692 struct text_pos saved_pos;
19693 Lisp_Object saved_object;
19694 struct face *face;
19696 saved_object = it->object;
19697 saved_pos = it->position;
19699 it->what = IT_CHARACTER;
19700 memset (&it->position, 0, sizeof it->position);
19701 it->object = Qnil;
19702 it->c = it->char_to_display = ' ';
19703 it->len = 1;
19705 /* If the default face was remapped, be sure to use the
19706 remapped face for the appended newline. */
19707 if (default_face_p)
19708 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19709 else if (it->face_before_selective_p)
19710 it->face_id = it->saved_face_id;
19711 face = FACE_FROM_ID (it->f, it->face_id);
19712 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19713 /* In R2L rows, we will prepend a stretch glyph that will
19714 have the end_of_box_run_p flag set for it, so there's no
19715 need for the appended newline glyph to have that flag
19716 set. */
19717 if (it->glyph_row->reversed_p
19718 /* But if the appended newline glyph goes all the way to
19719 the end of the row, there will be no stretch glyph,
19720 so leave the box flag set. */
19721 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19722 it->end_of_box_run_p = false;
19724 PRODUCE_GLYPHS (it);
19726 #ifdef HAVE_WINDOW_SYSTEM
19727 /* Make sure this space glyph has the right ascent and
19728 descent values, or else cursor at end of line will look
19729 funny, and height of empty lines will be incorrect. */
19730 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19731 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19732 if (n == 0)
19734 Lisp_Object height, total_height;
19735 int extra_line_spacing = it->extra_line_spacing;
19736 int boff = font->baseline_offset;
19738 if (font->vertical_centering)
19739 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19741 it->object = saved_object; /* get_it_property needs this */
19742 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19743 /* Must do a subset of line height processing from
19744 x_produce_glyph for newline characters. */
19745 height = get_it_property (it, Qline_height);
19746 if (CONSP (height)
19747 && CONSP (XCDR (height))
19748 && NILP (XCDR (XCDR (height))))
19750 total_height = XCAR (XCDR (height));
19751 height = XCAR (height);
19753 else
19754 total_height = Qnil;
19755 height = calc_line_height_property (it, height, font, boff, true);
19757 if (it->override_ascent >= 0)
19759 it->ascent = it->override_ascent;
19760 it->descent = it->override_descent;
19761 boff = it->override_boff;
19763 if (EQ (height, Qt))
19764 extra_line_spacing = 0;
19765 else
19767 Lisp_Object spacing;
19769 it->phys_ascent = it->ascent;
19770 it->phys_descent = it->descent;
19771 if (!NILP (height)
19772 && XINT (height) > it->ascent + it->descent)
19773 it->ascent = XINT (height) - it->descent;
19775 if (!NILP (total_height))
19776 spacing = calc_line_height_property (it, total_height, font,
19777 boff, false);
19778 else
19780 spacing = get_it_property (it, Qline_spacing);
19781 spacing = calc_line_height_property (it, spacing, font,
19782 boff, false);
19784 if (INTEGERP (spacing))
19786 extra_line_spacing = XINT (spacing);
19787 if (!NILP (total_height))
19788 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19791 if (extra_line_spacing > 0)
19793 it->descent += extra_line_spacing;
19794 if (extra_line_spacing > it->max_extra_line_spacing)
19795 it->max_extra_line_spacing = extra_line_spacing;
19797 it->max_ascent = it->ascent;
19798 it->max_descent = it->descent;
19799 /* Make sure compute_line_metrics recomputes the row height. */
19800 it->glyph_row->height = 0;
19803 g->ascent = it->max_ascent;
19804 g->descent = it->max_descent;
19805 #endif
19807 it->override_ascent = -1;
19808 it->constrain_row_ascent_descent_p = false;
19809 it->current_x = saved_x;
19810 it->object = saved_object;
19811 it->position = saved_pos;
19812 it->what = saved_what;
19813 it->face_id = saved_face_id;
19814 it->len = saved_len;
19815 it->c = saved_c;
19816 it->char_to_display = saved_char_to_display;
19817 it->end_of_box_run_p = saved_box_end;
19818 return true;
19822 return false;
19826 /* Extend the face of the last glyph in the text area of IT->glyph_row
19827 to the end of the display line. Called from display_line. If the
19828 glyph row is empty, add a space glyph to it so that we know the
19829 face to draw. Set the glyph row flag fill_line_p. If the glyph
19830 row is R2L, prepend a stretch glyph to cover the empty space to the
19831 left of the leftmost glyph. */
19833 static void
19834 extend_face_to_end_of_line (struct it *it)
19836 struct face *face, *default_face;
19837 struct frame *f = it->f;
19839 /* If line is already filled, do nothing. Non window-system frames
19840 get a grace of one more ``pixel'' because their characters are
19841 1-``pixel'' wide, so they hit the equality too early. This grace
19842 is needed only for R2L rows that are not continued, to produce
19843 one extra blank where we could display the cursor. */
19844 if ((it->current_x >= it->last_visible_x
19845 + (!FRAME_WINDOW_P (f)
19846 && it->glyph_row->reversed_p
19847 && !it->glyph_row->continued_p))
19848 /* If the window has display margins, we will need to extend
19849 their face even if the text area is filled. */
19850 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19851 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19852 return;
19854 /* The default face, possibly remapped. */
19855 default_face = FACE_FROM_ID_OR_NULL (f,
19856 lookup_basic_face (f, DEFAULT_FACE_ID));
19858 /* Face extension extends the background and box of IT->face_id
19859 to the end of the line. If the background equals the background
19860 of the frame, we don't have to do anything. */
19861 face = FACE_FROM_ID (f, (it->face_before_selective_p
19862 ? it->saved_face_id
19863 : it->face_id));
19865 if (FRAME_WINDOW_P (f)
19866 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19867 && face->box == FACE_NO_BOX
19868 && face->background == FRAME_BACKGROUND_PIXEL (f)
19869 #ifdef HAVE_WINDOW_SYSTEM
19870 && !face->stipple
19871 #endif
19872 && !it->glyph_row->reversed_p)
19873 return;
19875 /* Set the glyph row flag indicating that the face of the last glyph
19876 in the text area has to be drawn to the end of the text area. */
19877 it->glyph_row->fill_line_p = true;
19879 /* If current character of IT is not ASCII, make sure we have the
19880 ASCII face. This will be automatically undone the next time
19881 get_next_display_element returns a multibyte character. Note
19882 that the character will always be single byte in unibyte
19883 text. */
19884 if (!ASCII_CHAR_P (it->c))
19886 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19889 if (FRAME_WINDOW_P (f))
19891 /* If the row is empty, add a space with the current face of IT,
19892 so that we know which face to draw. */
19893 if (it->glyph_row->used[TEXT_AREA] == 0)
19895 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19896 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19897 it->glyph_row->used[TEXT_AREA] = 1;
19899 /* Mode line and the header line don't have margins, and
19900 likewise the frame's tool-bar window, if there is any. */
19901 if (!(it->glyph_row->mode_line_p
19902 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19903 || (WINDOWP (f->tool_bar_window)
19904 && it->w == XWINDOW (f->tool_bar_window))
19905 #endif
19908 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19909 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19911 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19912 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19913 default_face->id;
19914 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19916 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19917 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19919 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19920 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19921 default_face->id;
19922 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19925 #ifdef HAVE_WINDOW_SYSTEM
19926 if (it->glyph_row->reversed_p)
19928 /* Prepend a stretch glyph to the row, such that the
19929 rightmost glyph will be drawn flushed all the way to the
19930 right margin of the window. The stretch glyph that will
19931 occupy the empty space, if any, to the left of the
19932 glyphs. */
19933 struct font *font = face->font ? face->font : FRAME_FONT (f);
19934 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19935 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19936 struct glyph *g;
19937 int row_width, stretch_ascent, stretch_width;
19938 struct text_pos saved_pos;
19939 int saved_face_id;
19940 bool saved_avoid_cursor, saved_box_start;
19942 for (row_width = 0, g = row_start; g < row_end; g++)
19943 row_width += g->pixel_width;
19945 /* FIXME: There are various minor display glitches in R2L
19946 rows when only one of the fringes is missing. The
19947 strange condition below produces the least bad effect. */
19948 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19949 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19950 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19951 stretch_width = window_box_width (it->w, TEXT_AREA);
19952 else
19953 stretch_width = it->last_visible_x - it->first_visible_x;
19954 stretch_width -= row_width;
19956 if (stretch_width > 0)
19958 stretch_ascent =
19959 (((it->ascent + it->descent)
19960 * FONT_BASE (font)) / FONT_HEIGHT (font));
19961 saved_pos = it->position;
19962 memset (&it->position, 0, sizeof it->position);
19963 saved_avoid_cursor = it->avoid_cursor_p;
19964 it->avoid_cursor_p = true;
19965 saved_face_id = it->face_id;
19966 saved_box_start = it->start_of_box_run_p;
19967 /* The last row's stretch glyph should get the default
19968 face, to avoid painting the rest of the window with
19969 the region face, if the region ends at ZV. */
19970 if (it->glyph_row->ends_at_zv_p)
19971 it->face_id = default_face->id;
19972 else
19973 it->face_id = face->id;
19974 it->start_of_box_run_p = false;
19975 append_stretch_glyph (it, Qnil, stretch_width,
19976 it->ascent + it->descent, stretch_ascent);
19977 it->position = saved_pos;
19978 it->avoid_cursor_p = saved_avoid_cursor;
19979 it->face_id = saved_face_id;
19980 it->start_of_box_run_p = saved_box_start;
19982 /* If stretch_width comes out negative, it means that the
19983 last glyph is only partially visible. In R2L rows, we
19984 want the leftmost glyph to be partially visible, so we
19985 need to give the row the corresponding left offset. */
19986 if (stretch_width < 0)
19987 it->glyph_row->x = stretch_width;
19989 #endif /* HAVE_WINDOW_SYSTEM */
19991 else
19993 /* Save some values that must not be changed. */
19994 int saved_x = it->current_x;
19995 struct text_pos saved_pos;
19996 Lisp_Object saved_object;
19997 enum display_element_type saved_what = it->what;
19998 int saved_face_id = it->face_id;
20000 saved_object = it->object;
20001 saved_pos = it->position;
20003 it->what = IT_CHARACTER;
20004 memset (&it->position, 0, sizeof it->position);
20005 it->object = Qnil;
20006 it->c = it->char_to_display = ' ';
20007 it->len = 1;
20009 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20010 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20011 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20012 && !it->glyph_row->mode_line_p
20013 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20015 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20016 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20018 for (it->current_x = 0; g < e; g++)
20019 it->current_x += g->pixel_width;
20021 it->area = LEFT_MARGIN_AREA;
20022 it->face_id = default_face->id;
20023 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20024 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20026 PRODUCE_GLYPHS (it);
20027 /* term.c:produce_glyphs advances it->current_x only for
20028 TEXT_AREA. */
20029 it->current_x += it->pixel_width;
20032 it->current_x = saved_x;
20033 it->area = TEXT_AREA;
20036 /* The last row's blank glyphs should get the default face, to
20037 avoid painting the rest of the window with the region face,
20038 if the region ends at ZV. */
20039 if (it->glyph_row->ends_at_zv_p)
20040 it->face_id = default_face->id;
20041 else
20042 it->face_id = face->id;
20043 PRODUCE_GLYPHS (it);
20045 while (it->current_x <= it->last_visible_x)
20046 PRODUCE_GLYPHS (it);
20048 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20049 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20050 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20051 && !it->glyph_row->mode_line_p
20052 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20054 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20055 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20057 for ( ; g < e; g++)
20058 it->current_x += g->pixel_width;
20060 it->area = RIGHT_MARGIN_AREA;
20061 it->face_id = default_face->id;
20062 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20063 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20065 PRODUCE_GLYPHS (it);
20066 it->current_x += it->pixel_width;
20069 it->area = TEXT_AREA;
20072 /* Don't count these blanks really. It would let us insert a left
20073 truncation glyph below and make us set the cursor on them, maybe. */
20074 it->current_x = saved_x;
20075 it->object = saved_object;
20076 it->position = saved_pos;
20077 it->what = saved_what;
20078 it->face_id = saved_face_id;
20083 /* Value is true if text starting at CHARPOS in current_buffer is
20084 trailing whitespace. */
20086 static bool
20087 trailing_whitespace_p (ptrdiff_t charpos)
20089 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20090 int c = 0;
20092 while (bytepos < ZV_BYTE
20093 && (c = FETCH_CHAR (bytepos),
20094 c == ' ' || c == '\t'))
20095 ++bytepos;
20097 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20099 if (bytepos != PT_BYTE)
20100 return true;
20102 return false;
20106 /* Highlight trailing whitespace, if any, in ROW. */
20108 static void
20109 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20111 int used = row->used[TEXT_AREA];
20113 if (used)
20115 struct glyph *start = row->glyphs[TEXT_AREA];
20116 struct glyph *glyph = start + used - 1;
20118 if (row->reversed_p)
20120 /* Right-to-left rows need to be processed in the opposite
20121 direction, so swap the edge pointers. */
20122 glyph = start;
20123 start = row->glyphs[TEXT_AREA] + used - 1;
20126 /* Skip over glyphs inserted to display the cursor at the
20127 end of a line, for extending the face of the last glyph
20128 to the end of the line on terminals, and for truncation
20129 and continuation glyphs. */
20130 if (!row->reversed_p)
20132 while (glyph >= start
20133 && glyph->type == CHAR_GLYPH
20134 && NILP (glyph->object))
20135 --glyph;
20137 else
20139 while (glyph <= start
20140 && glyph->type == CHAR_GLYPH
20141 && NILP (glyph->object))
20142 ++glyph;
20145 /* If last glyph is a space or stretch, and it's trailing
20146 whitespace, set the face of all trailing whitespace glyphs in
20147 IT->glyph_row to `trailing-whitespace'. */
20148 if ((row->reversed_p ? glyph <= start : glyph >= start)
20149 && BUFFERP (glyph->object)
20150 && (glyph->type == STRETCH_GLYPH
20151 || (glyph->type == CHAR_GLYPH
20152 && glyph->u.ch == ' '))
20153 && trailing_whitespace_p (glyph->charpos))
20155 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20156 if (face_id < 0)
20157 return;
20159 if (!row->reversed_p)
20161 while (glyph >= start
20162 && BUFFERP (glyph->object)
20163 && (glyph->type == STRETCH_GLYPH
20164 || (glyph->type == CHAR_GLYPH
20165 && glyph->u.ch == ' ')))
20166 (glyph--)->face_id = face_id;
20168 else
20170 while (glyph <= start
20171 && BUFFERP (glyph->object)
20172 && (glyph->type == STRETCH_GLYPH
20173 || (glyph->type == CHAR_GLYPH
20174 && glyph->u.ch == ' ')))
20175 (glyph++)->face_id = face_id;
20182 /* Value is true if glyph row ROW should be
20183 considered to hold the buffer position CHARPOS. */
20185 static bool
20186 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20188 bool result = true;
20190 if (charpos == CHARPOS (row->end.pos)
20191 || charpos == MATRIX_ROW_END_CHARPOS (row))
20193 /* Suppose the row ends on a string.
20194 Unless the row is continued, that means it ends on a newline
20195 in the string. If it's anything other than a display string
20196 (e.g., a before-string from an overlay), we don't want the
20197 cursor there. (This heuristic seems to give the optimal
20198 behavior for the various types of multi-line strings.)
20199 One exception: if the string has `cursor' property on one of
20200 its characters, we _do_ want the cursor there. */
20201 if (CHARPOS (row->end.string_pos) >= 0)
20203 if (row->continued_p)
20204 result = true;
20205 else
20207 /* Check for `display' property. */
20208 struct glyph *beg = row->glyphs[TEXT_AREA];
20209 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20210 struct glyph *glyph;
20212 result = false;
20213 for (glyph = end; glyph >= beg; --glyph)
20214 if (STRINGP (glyph->object))
20216 Lisp_Object prop
20217 = Fget_char_property (make_number (charpos),
20218 Qdisplay, Qnil);
20219 result =
20220 (!NILP (prop)
20221 && display_prop_string_p (prop, glyph->object));
20222 /* If there's a `cursor' property on one of the
20223 string's characters, this row is a cursor row,
20224 even though this is not a display string. */
20225 if (!result)
20227 Lisp_Object s = glyph->object;
20229 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20231 ptrdiff_t gpos = glyph->charpos;
20233 if (!NILP (Fget_char_property (make_number (gpos),
20234 Qcursor, s)))
20236 result = true;
20237 break;
20241 break;
20245 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20247 /* If the row ends in middle of a real character,
20248 and the line is continued, we want the cursor here.
20249 That's because CHARPOS (ROW->end.pos) would equal
20250 PT if PT is before the character. */
20251 if (!row->ends_in_ellipsis_p)
20252 result = row->continued_p;
20253 else
20254 /* If the row ends in an ellipsis, then
20255 CHARPOS (ROW->end.pos) will equal point after the
20256 invisible text. We want that position to be displayed
20257 after the ellipsis. */
20258 result = false;
20260 /* If the row ends at ZV, display the cursor at the end of that
20261 row instead of at the start of the row below. */
20262 else
20263 result = row->ends_at_zv_p;
20266 return result;
20269 /* Value is true if glyph row ROW should be
20270 used to hold the cursor. */
20272 static bool
20273 cursor_row_p (struct glyph_row *row)
20275 return row_for_charpos_p (row, PT);
20280 /* Push the property PROP so that it will be rendered at the current
20281 position in IT. Return true if PROP was successfully pushed, false
20282 otherwise. Called from handle_line_prefix to handle the
20283 `line-prefix' and `wrap-prefix' properties. */
20285 static bool
20286 push_prefix_prop (struct it *it, Lisp_Object prop)
20288 struct text_pos pos =
20289 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20291 eassert (it->method == GET_FROM_BUFFER
20292 || it->method == GET_FROM_DISPLAY_VECTOR
20293 || it->method == GET_FROM_STRING
20294 || it->method == GET_FROM_IMAGE);
20296 /* We need to save the current buffer/string position, so it will be
20297 restored by pop_it, because iterate_out_of_display_property
20298 depends on that being set correctly, but some situations leave
20299 it->position not yet set when this function is called. */
20300 push_it (it, &pos);
20302 if (STRINGP (prop))
20304 if (SCHARS (prop) == 0)
20306 pop_it (it);
20307 return false;
20310 it->string = prop;
20311 it->string_from_prefix_prop_p = true;
20312 it->multibyte_p = STRING_MULTIBYTE (it->string);
20313 it->current.overlay_string_index = -1;
20314 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20315 it->end_charpos = it->string_nchars = SCHARS (it->string);
20316 it->method = GET_FROM_STRING;
20317 it->stop_charpos = 0;
20318 it->prev_stop = 0;
20319 it->base_level_stop = 0;
20321 /* Force paragraph direction to be that of the parent
20322 buffer/string. */
20323 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20324 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20325 else
20326 it->paragraph_embedding = L2R;
20328 /* Set up the bidi iterator for this display string. */
20329 if (it->bidi_p)
20331 it->bidi_it.string.lstring = it->string;
20332 it->bidi_it.string.s = NULL;
20333 it->bidi_it.string.schars = it->end_charpos;
20334 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20335 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20336 it->bidi_it.string.unibyte = !it->multibyte_p;
20337 it->bidi_it.w = it->w;
20338 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20341 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20343 it->method = GET_FROM_STRETCH;
20344 it->object = prop;
20346 #ifdef HAVE_WINDOW_SYSTEM
20347 else if (IMAGEP (prop))
20349 it->what = IT_IMAGE;
20350 it->image_id = lookup_image (it->f, prop);
20351 it->method = GET_FROM_IMAGE;
20353 #endif /* HAVE_WINDOW_SYSTEM */
20354 else
20356 pop_it (it); /* bogus display property, give up */
20357 return false;
20360 return true;
20363 /* Return the character-property PROP at the current position in IT. */
20365 static Lisp_Object
20366 get_it_property (struct it *it, Lisp_Object prop)
20368 Lisp_Object position, object = it->object;
20370 if (STRINGP (object))
20371 position = make_number (IT_STRING_CHARPOS (*it));
20372 else if (BUFFERP (object))
20374 position = make_number (IT_CHARPOS (*it));
20375 object = it->window;
20377 else
20378 return Qnil;
20380 return Fget_char_property (position, prop, object);
20383 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20385 static void
20386 handle_line_prefix (struct it *it)
20388 Lisp_Object prefix;
20390 if (it->continuation_lines_width > 0)
20392 prefix = get_it_property (it, Qwrap_prefix);
20393 if (NILP (prefix))
20394 prefix = Vwrap_prefix;
20396 else
20398 prefix = get_it_property (it, Qline_prefix);
20399 if (NILP (prefix))
20400 prefix = Vline_prefix;
20402 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20404 /* If the prefix is wider than the window, and we try to wrap
20405 it, it would acquire its own wrap prefix, and so on till the
20406 iterator stack overflows. So, don't wrap the prefix. */
20407 it->line_wrap = TRUNCATE;
20408 it->avoid_cursor_p = true;
20414 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20415 only for R2L lines from display_line and display_string, when they
20416 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20417 the line/string needs to be continued on the next glyph row. */
20418 static void
20419 unproduce_glyphs (struct it *it, int n)
20421 struct glyph *glyph, *end;
20423 eassert (it->glyph_row);
20424 eassert (it->glyph_row->reversed_p);
20425 eassert (it->area == TEXT_AREA);
20426 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20428 if (n > it->glyph_row->used[TEXT_AREA])
20429 n = it->glyph_row->used[TEXT_AREA];
20430 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20431 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20432 for ( ; glyph < end; glyph++)
20433 glyph[-n] = *glyph;
20436 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20437 and ROW->maxpos. */
20438 static void
20439 find_row_edges (struct it *it, struct glyph_row *row,
20440 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20441 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20443 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20444 lines' rows is implemented for bidi-reordered rows. */
20446 /* ROW->minpos is the value of min_pos, the minimal buffer position
20447 we have in ROW, or ROW->start.pos if that is smaller. */
20448 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20449 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20450 else
20451 /* We didn't find buffer positions smaller than ROW->start, or
20452 didn't find _any_ valid buffer positions in any of the glyphs,
20453 so we must trust the iterator's computed positions. */
20454 row->minpos = row->start.pos;
20455 if (max_pos <= 0)
20457 max_pos = CHARPOS (it->current.pos);
20458 max_bpos = BYTEPOS (it->current.pos);
20461 /* Here are the various use-cases for ending the row, and the
20462 corresponding values for ROW->maxpos:
20464 Line ends in a newline from buffer eol_pos + 1
20465 Line is continued from buffer max_pos + 1
20466 Line is truncated on right it->current.pos
20467 Line ends in a newline from string max_pos + 1(*)
20468 (*) + 1 only when line ends in a forward scan
20469 Line is continued from string max_pos
20470 Line is continued from display vector max_pos
20471 Line is entirely from a string min_pos == max_pos
20472 Line is entirely from a display vector min_pos == max_pos
20473 Line that ends at ZV ZV
20475 If you discover other use-cases, please add them here as
20476 appropriate. */
20477 if (row->ends_at_zv_p)
20478 row->maxpos = it->current.pos;
20479 else if (row->used[TEXT_AREA])
20481 bool seen_this_string = false;
20482 struct glyph_row *r1 = row - 1;
20484 /* Did we see the same display string on the previous row? */
20485 if (STRINGP (it->object)
20486 /* this is not the first row */
20487 && row > it->w->desired_matrix->rows
20488 /* previous row is not the header line */
20489 && !r1->mode_line_p
20490 /* previous row also ends in a newline from a string */
20491 && r1->ends_in_newline_from_string_p)
20493 struct glyph *start, *end;
20495 /* Search for the last glyph of the previous row that came
20496 from buffer or string. Depending on whether the row is
20497 L2R or R2L, we need to process it front to back or the
20498 other way round. */
20499 if (!r1->reversed_p)
20501 start = r1->glyphs[TEXT_AREA];
20502 end = start + r1->used[TEXT_AREA];
20503 /* Glyphs inserted by redisplay have nil as their object. */
20504 while (end > start
20505 && NILP ((end - 1)->object)
20506 && (end - 1)->charpos <= 0)
20507 --end;
20508 if (end > start)
20510 if (EQ ((end - 1)->object, it->object))
20511 seen_this_string = true;
20513 else
20514 /* If all the glyphs of the previous row were inserted
20515 by redisplay, it means the previous row was
20516 produced from a single newline, which is only
20517 possible if that newline came from the same string
20518 as the one which produced this ROW. */
20519 seen_this_string = true;
20521 else
20523 end = r1->glyphs[TEXT_AREA] - 1;
20524 start = end + r1->used[TEXT_AREA];
20525 while (end < start
20526 && NILP ((end + 1)->object)
20527 && (end + 1)->charpos <= 0)
20528 ++end;
20529 if (end < start)
20531 if (EQ ((end + 1)->object, it->object))
20532 seen_this_string = true;
20534 else
20535 seen_this_string = true;
20538 /* Take note of each display string that covers a newline only
20539 once, the first time we see it. This is for when a display
20540 string includes more than one newline in it. */
20541 if (row->ends_in_newline_from_string_p && !seen_this_string)
20543 /* If we were scanning the buffer forward when we displayed
20544 the string, we want to account for at least one buffer
20545 position that belongs to this row (position covered by
20546 the display string), so that cursor positioning will
20547 consider this row as a candidate when point is at the end
20548 of the visual line represented by this row. This is not
20549 required when scanning back, because max_pos will already
20550 have a much larger value. */
20551 if (CHARPOS (row->end.pos) > max_pos)
20552 INC_BOTH (max_pos, max_bpos);
20553 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20555 else if (CHARPOS (it->eol_pos) > 0)
20556 SET_TEXT_POS (row->maxpos,
20557 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20558 else if (row->continued_p)
20560 /* If max_pos is different from IT's current position, it
20561 means IT->method does not belong to the display element
20562 at max_pos. However, it also means that the display
20563 element at max_pos was displayed in its entirety on this
20564 line, which is equivalent to saying that the next line
20565 starts at the next buffer position. */
20566 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20567 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20568 else
20570 INC_BOTH (max_pos, max_bpos);
20571 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20574 else if (row->truncated_on_right_p)
20575 /* display_line already called reseat_at_next_visible_line_start,
20576 which puts the iterator at the beginning of the next line, in
20577 the logical order. */
20578 row->maxpos = it->current.pos;
20579 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20580 /* A line that is entirely from a string/image/stretch... */
20581 row->maxpos = row->minpos;
20582 else
20583 emacs_abort ();
20585 else
20586 row->maxpos = it->current.pos;
20589 /* Construct the glyph row IT->glyph_row in the desired matrix of
20590 IT->w from text at the current position of IT. See dispextern.h
20591 for an overview of struct it. Value is true if
20592 IT->glyph_row displays text, as opposed to a line displaying ZV
20593 only. */
20595 static bool
20596 display_line (struct it *it)
20598 struct glyph_row *row = it->glyph_row;
20599 Lisp_Object overlay_arrow_string;
20600 struct it wrap_it;
20601 void *wrap_data = NULL;
20602 bool may_wrap = false;
20603 int wrap_x UNINIT;
20604 int wrap_row_used = -1;
20605 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20606 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20607 int wrap_row_extra_line_spacing UNINIT;
20608 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20609 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20610 int cvpos;
20611 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20612 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20613 bool pending_handle_line_prefix = false;
20615 /* We always start displaying at hpos zero even if hscrolled. */
20616 eassert (it->hpos == 0 && it->current_x == 0);
20618 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20619 >= it->w->desired_matrix->nrows)
20621 it->w->nrows_scale_factor++;
20622 it->f->fonts_changed = true;
20623 return false;
20626 /* Clear the result glyph row and enable it. */
20627 prepare_desired_row (it->w, row, false);
20629 row->y = it->current_y;
20630 row->start = it->start;
20631 row->continuation_lines_width = it->continuation_lines_width;
20632 row->displays_text_p = true;
20633 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20634 it->starts_in_middle_of_char_p = false;
20636 /* Arrange the overlays nicely for our purposes. Usually, we call
20637 display_line on only one line at a time, in which case this
20638 can't really hurt too much, or we call it on lines which appear
20639 one after another in the buffer, in which case all calls to
20640 recenter_overlay_lists but the first will be pretty cheap. */
20641 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20643 /* Move over display elements that are not visible because we are
20644 hscrolled. This may stop at an x-position < IT->first_visible_x
20645 if the first glyph is partially visible or if we hit a line end. */
20646 if (it->current_x < it->first_visible_x)
20648 enum move_it_result move_result;
20650 this_line_min_pos = row->start.pos;
20651 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20652 MOVE_TO_POS | MOVE_TO_X);
20653 /* If we are under a large hscroll, move_it_in_display_line_to
20654 could hit the end of the line without reaching
20655 it->first_visible_x. Pretend that we did reach it. This is
20656 especially important on a TTY, where we will call
20657 extend_face_to_end_of_line, which needs to know how many
20658 blank glyphs to produce. */
20659 if (it->current_x < it->first_visible_x
20660 && (move_result == MOVE_NEWLINE_OR_CR
20661 || move_result == MOVE_POS_MATCH_OR_ZV))
20662 it->current_x = it->first_visible_x;
20664 /* Record the smallest positions seen while we moved over
20665 display elements that are not visible. This is needed by
20666 redisplay_internal for optimizing the case where the cursor
20667 stays inside the same line. The rest of this function only
20668 considers positions that are actually displayed, so
20669 RECORD_MAX_MIN_POS will not otherwise record positions that
20670 are hscrolled to the left of the left edge of the window. */
20671 min_pos = CHARPOS (this_line_min_pos);
20672 min_bpos = BYTEPOS (this_line_min_pos);
20674 else if (it->area == TEXT_AREA)
20676 /* We only do this when not calling move_it_in_display_line_to
20677 above, because that function calls itself handle_line_prefix. */
20678 handle_line_prefix (it);
20680 else
20682 /* Line-prefix and wrap-prefix are always displayed in the text
20683 area. But if this is the first call to display_line after
20684 init_iterator, the iterator might have been set up to write
20685 into a marginal area, e.g. if the line begins with some
20686 display property that writes to the margins. So we need to
20687 wait with the call to handle_line_prefix until whatever
20688 writes to the margin has done its job. */
20689 pending_handle_line_prefix = true;
20692 /* Get the initial row height. This is either the height of the
20693 text hscrolled, if there is any, or zero. */
20694 row->ascent = it->max_ascent;
20695 row->height = it->max_ascent + it->max_descent;
20696 row->phys_ascent = it->max_phys_ascent;
20697 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20698 row->extra_line_spacing = it->max_extra_line_spacing;
20700 /* Utility macro to record max and min buffer positions seen until now. */
20701 #define RECORD_MAX_MIN_POS(IT) \
20702 do \
20704 bool composition_p \
20705 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20706 ptrdiff_t current_pos = \
20707 composition_p ? (IT)->cmp_it.charpos \
20708 : IT_CHARPOS (*(IT)); \
20709 ptrdiff_t current_bpos = \
20710 composition_p ? CHAR_TO_BYTE (current_pos) \
20711 : IT_BYTEPOS (*(IT)); \
20712 if (current_pos < min_pos) \
20714 min_pos = current_pos; \
20715 min_bpos = current_bpos; \
20717 if (IT_CHARPOS (*it) > max_pos) \
20719 max_pos = IT_CHARPOS (*it); \
20720 max_bpos = IT_BYTEPOS (*it); \
20723 while (false)
20725 /* Loop generating characters. The loop is left with IT on the next
20726 character to display. */
20727 while (true)
20729 int n_glyphs_before, hpos_before, x_before;
20730 int x, nglyphs;
20731 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20733 /* Retrieve the next thing to display. Value is false if end of
20734 buffer reached. */
20735 if (!get_next_display_element (it))
20737 /* Maybe add a space at the end of this line that is used to
20738 display the cursor there under X. Set the charpos of the
20739 first glyph of blank lines not corresponding to any text
20740 to -1. */
20741 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20742 row->exact_window_width_line_p = true;
20743 else if ((append_space_for_newline (it, true)
20744 && row->used[TEXT_AREA] == 1)
20745 || row->used[TEXT_AREA] == 0)
20747 row->glyphs[TEXT_AREA]->charpos = -1;
20748 row->displays_text_p = false;
20750 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20751 && (!MINI_WINDOW_P (it->w)
20752 || (minibuf_level && EQ (it->window, minibuf_window))))
20753 row->indicate_empty_line_p = true;
20756 it->continuation_lines_width = 0;
20757 row->ends_at_zv_p = true;
20758 /* A row that displays right-to-left text must always have
20759 its last face extended all the way to the end of line,
20760 even if this row ends in ZV, because we still write to
20761 the screen left to right. We also need to extend the
20762 last face if the default face is remapped to some
20763 different face, otherwise the functions that clear
20764 portions of the screen will clear with the default face's
20765 background color. */
20766 if (row->reversed_p
20767 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20768 extend_face_to_end_of_line (it);
20769 break;
20772 /* Now, get the metrics of what we want to display. This also
20773 generates glyphs in `row' (which is IT->glyph_row). */
20774 n_glyphs_before = row->used[TEXT_AREA];
20775 x = it->current_x;
20777 /* Remember the line height so far in case the next element doesn't
20778 fit on the line. */
20779 if (it->line_wrap != TRUNCATE)
20781 ascent = it->max_ascent;
20782 descent = it->max_descent;
20783 phys_ascent = it->max_phys_ascent;
20784 phys_descent = it->max_phys_descent;
20786 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20788 if (IT_DISPLAYING_WHITESPACE (it))
20789 may_wrap = true;
20790 else if (may_wrap)
20792 SAVE_IT (wrap_it, *it, wrap_data);
20793 wrap_x = x;
20794 wrap_row_used = row->used[TEXT_AREA];
20795 wrap_row_ascent = row->ascent;
20796 wrap_row_height = row->height;
20797 wrap_row_phys_ascent = row->phys_ascent;
20798 wrap_row_phys_height = row->phys_height;
20799 wrap_row_extra_line_spacing = row->extra_line_spacing;
20800 wrap_row_min_pos = min_pos;
20801 wrap_row_min_bpos = min_bpos;
20802 wrap_row_max_pos = max_pos;
20803 wrap_row_max_bpos = max_bpos;
20804 may_wrap = false;
20809 PRODUCE_GLYPHS (it);
20811 /* If this display element was in marginal areas, continue with
20812 the next one. */
20813 if (it->area != TEXT_AREA)
20815 row->ascent = max (row->ascent, it->max_ascent);
20816 row->height = max (row->height, it->max_ascent + it->max_descent);
20817 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20818 row->phys_height = max (row->phys_height,
20819 it->max_phys_ascent + it->max_phys_descent);
20820 row->extra_line_spacing = max (row->extra_line_spacing,
20821 it->max_extra_line_spacing);
20822 set_iterator_to_next (it, true);
20823 /* If we didn't handle the line/wrap prefix above, and the
20824 call to set_iterator_to_next just switched to TEXT_AREA,
20825 process the prefix now. */
20826 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20828 pending_handle_line_prefix = false;
20829 handle_line_prefix (it);
20831 continue;
20834 /* Does the display element fit on the line? If we truncate
20835 lines, we should draw past the right edge of the window. If
20836 we don't truncate, we want to stop so that we can display the
20837 continuation glyph before the right margin. If lines are
20838 continued, there are two possible strategies for characters
20839 resulting in more than 1 glyph (e.g. tabs): Display as many
20840 glyphs as possible in this line and leave the rest for the
20841 continuation line, or display the whole element in the next
20842 line. Original redisplay did the former, so we do it also. */
20843 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20844 hpos_before = it->hpos;
20845 x_before = x;
20847 if (/* Not a newline. */
20848 nglyphs > 0
20849 /* Glyphs produced fit entirely in the line. */
20850 && it->current_x < it->last_visible_x)
20852 it->hpos += nglyphs;
20853 row->ascent = max (row->ascent, it->max_ascent);
20854 row->height = max (row->height, it->max_ascent + it->max_descent);
20855 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20856 row->phys_height = max (row->phys_height,
20857 it->max_phys_ascent + it->max_phys_descent);
20858 row->extra_line_spacing = max (row->extra_line_spacing,
20859 it->max_extra_line_spacing);
20860 if (it->current_x - it->pixel_width < it->first_visible_x
20861 /* In R2L rows, we arrange in extend_face_to_end_of_line
20862 to add a right offset to the line, by a suitable
20863 change to the stretch glyph that is the leftmost
20864 glyph of the line. */
20865 && !row->reversed_p)
20866 row->x = x - it->first_visible_x;
20867 /* Record the maximum and minimum buffer positions seen so
20868 far in glyphs that will be displayed by this row. */
20869 if (it->bidi_p)
20870 RECORD_MAX_MIN_POS (it);
20872 else
20874 int i, new_x;
20875 struct glyph *glyph;
20877 for (i = 0; i < nglyphs; ++i, x = new_x)
20879 /* Identify the glyphs added by the last call to
20880 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20881 the previous glyphs. */
20882 if (!row->reversed_p)
20883 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20884 else
20885 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20886 new_x = x + glyph->pixel_width;
20888 if (/* Lines are continued. */
20889 it->line_wrap != TRUNCATE
20890 && (/* Glyph doesn't fit on the line. */
20891 new_x > it->last_visible_x
20892 /* Or it fits exactly on a window system frame. */
20893 || (new_x == it->last_visible_x
20894 && FRAME_WINDOW_P (it->f)
20895 && (row->reversed_p
20896 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20897 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20899 /* End of a continued line. */
20901 if (it->hpos == 0
20902 || (new_x == it->last_visible_x
20903 && FRAME_WINDOW_P (it->f)
20904 && (row->reversed_p
20905 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20906 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20908 /* Current glyph is the only one on the line or
20909 fits exactly on the line. We must continue
20910 the line because we can't draw the cursor
20911 after the glyph. */
20912 row->continued_p = true;
20913 it->current_x = new_x;
20914 it->continuation_lines_width += new_x;
20915 ++it->hpos;
20916 if (i == nglyphs - 1)
20918 /* If line-wrap is on, check if a previous
20919 wrap point was found. */
20920 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20921 && wrap_row_used > 0
20922 /* Even if there is a previous wrap
20923 point, continue the line here as
20924 usual, if (i) the previous character
20925 was a space or tab AND (ii) the
20926 current character is not. */
20927 && (!may_wrap
20928 || IT_DISPLAYING_WHITESPACE (it)))
20929 goto back_to_wrap;
20931 /* Record the maximum and minimum buffer
20932 positions seen so far in glyphs that will be
20933 displayed by this row. */
20934 if (it->bidi_p)
20935 RECORD_MAX_MIN_POS (it);
20936 set_iterator_to_next (it, true);
20937 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20939 if (!get_next_display_element (it))
20941 row->exact_window_width_line_p = true;
20942 it->continuation_lines_width = 0;
20943 row->continued_p = false;
20944 row->ends_at_zv_p = true;
20946 else if (ITERATOR_AT_END_OF_LINE_P (it))
20948 row->continued_p = false;
20949 row->exact_window_width_line_p = true;
20951 /* If line-wrap is on, check if a
20952 previous wrap point was found. */
20953 else if (wrap_row_used > 0
20954 /* Even if there is a previous wrap
20955 point, continue the line here as
20956 usual, if (i) the previous character
20957 was a space or tab AND (ii) the
20958 current character is not. */
20959 && (!may_wrap
20960 || IT_DISPLAYING_WHITESPACE (it)))
20961 goto back_to_wrap;
20965 else if (it->bidi_p)
20966 RECORD_MAX_MIN_POS (it);
20967 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20968 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20969 extend_face_to_end_of_line (it);
20971 else if (CHAR_GLYPH_PADDING_P (*glyph)
20972 && !FRAME_WINDOW_P (it->f))
20974 /* A padding glyph that doesn't fit on this line.
20975 This means the whole character doesn't fit
20976 on the line. */
20977 if (row->reversed_p)
20978 unproduce_glyphs (it, row->used[TEXT_AREA]
20979 - n_glyphs_before);
20980 row->used[TEXT_AREA] = n_glyphs_before;
20982 /* Fill the rest of the row with continuation
20983 glyphs like in 20.x. */
20984 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20985 < row->glyphs[1 + TEXT_AREA])
20986 produce_special_glyphs (it, IT_CONTINUATION);
20988 row->continued_p = true;
20989 it->current_x = x_before;
20990 it->continuation_lines_width += x_before;
20992 /* Restore the height to what it was before the
20993 element not fitting on the line. */
20994 it->max_ascent = ascent;
20995 it->max_descent = descent;
20996 it->max_phys_ascent = phys_ascent;
20997 it->max_phys_descent = phys_descent;
20998 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20999 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21000 extend_face_to_end_of_line (it);
21002 else if (wrap_row_used > 0)
21004 back_to_wrap:
21005 if (row->reversed_p)
21006 unproduce_glyphs (it,
21007 row->used[TEXT_AREA] - wrap_row_used);
21008 RESTORE_IT (it, &wrap_it, wrap_data);
21009 it->continuation_lines_width += wrap_x;
21010 row->used[TEXT_AREA] = wrap_row_used;
21011 row->ascent = wrap_row_ascent;
21012 row->height = wrap_row_height;
21013 row->phys_ascent = wrap_row_phys_ascent;
21014 row->phys_height = wrap_row_phys_height;
21015 row->extra_line_spacing = wrap_row_extra_line_spacing;
21016 min_pos = wrap_row_min_pos;
21017 min_bpos = wrap_row_min_bpos;
21018 max_pos = wrap_row_max_pos;
21019 max_bpos = wrap_row_max_bpos;
21020 row->continued_p = true;
21021 row->ends_at_zv_p = false;
21022 row->exact_window_width_line_p = false;
21023 it->continuation_lines_width += x;
21025 /* Make sure that a non-default face is extended
21026 up to the right margin of the window. */
21027 extend_face_to_end_of_line (it);
21029 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
21031 /* A TAB that extends past the right edge of the
21032 window. This produces a single glyph on
21033 window system frames. We leave the glyph in
21034 this row and let it fill the row, but don't
21035 consume the TAB. */
21036 if ((row->reversed_p
21037 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21038 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21039 produce_special_glyphs (it, IT_CONTINUATION);
21040 it->continuation_lines_width += it->last_visible_x;
21041 row->ends_in_middle_of_char_p = true;
21042 row->continued_p = true;
21043 glyph->pixel_width = it->last_visible_x - x;
21044 it->starts_in_middle_of_char_p = true;
21045 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21046 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21047 extend_face_to_end_of_line (it);
21049 else
21051 /* Something other than a TAB that draws past
21052 the right edge of the window. Restore
21053 positions to values before the element. */
21054 if (row->reversed_p)
21055 unproduce_glyphs (it, row->used[TEXT_AREA]
21056 - (n_glyphs_before + i));
21057 row->used[TEXT_AREA] = n_glyphs_before + i;
21059 /* Display continuation glyphs. */
21060 it->current_x = x_before;
21061 it->continuation_lines_width += x;
21062 if (!FRAME_WINDOW_P (it->f)
21063 || (row->reversed_p
21064 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21065 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21066 produce_special_glyphs (it, IT_CONTINUATION);
21067 row->continued_p = true;
21069 extend_face_to_end_of_line (it);
21071 if (nglyphs > 1 && i > 0)
21073 row->ends_in_middle_of_char_p = true;
21074 it->starts_in_middle_of_char_p = true;
21077 /* Restore the height to what it was before the
21078 element not fitting on the line. */
21079 it->max_ascent = ascent;
21080 it->max_descent = descent;
21081 it->max_phys_ascent = phys_ascent;
21082 it->max_phys_descent = phys_descent;
21085 break;
21087 else if (new_x > it->first_visible_x)
21089 /* Increment number of glyphs actually displayed. */
21090 ++it->hpos;
21092 /* Record the maximum and minimum buffer positions
21093 seen so far in glyphs that will be displayed by
21094 this row. */
21095 if (it->bidi_p)
21096 RECORD_MAX_MIN_POS (it);
21098 if (x < it->first_visible_x && !row->reversed_p)
21099 /* Glyph is partially visible, i.e. row starts at
21100 negative X position. Don't do that in R2L
21101 rows, where we arrange to add a right offset to
21102 the line in extend_face_to_end_of_line, by a
21103 suitable change to the stretch glyph that is
21104 the leftmost glyph of the line. */
21105 row->x = x - it->first_visible_x;
21106 /* When the last glyph of an R2L row only fits
21107 partially on the line, we need to set row->x to a
21108 negative offset, so that the leftmost glyph is
21109 the one that is partially visible. But if we are
21110 going to produce the truncation glyph, this will
21111 be taken care of in produce_special_glyphs. */
21112 if (row->reversed_p
21113 && new_x > it->last_visible_x
21114 && !(it->line_wrap == TRUNCATE
21115 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21117 eassert (FRAME_WINDOW_P (it->f));
21118 row->x = it->last_visible_x - new_x;
21121 else
21123 /* Glyph is completely off the left margin of the
21124 window. This should not happen because of the
21125 move_it_in_display_line at the start of this
21126 function, unless the text display area of the
21127 window is empty. */
21128 eassert (it->first_visible_x <= it->last_visible_x);
21131 /* Even if this display element produced no glyphs at all,
21132 we want to record its position. */
21133 if (it->bidi_p && nglyphs == 0)
21134 RECORD_MAX_MIN_POS (it);
21136 row->ascent = max (row->ascent, it->max_ascent);
21137 row->height = max (row->height, it->max_ascent + it->max_descent);
21138 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21139 row->phys_height = max (row->phys_height,
21140 it->max_phys_ascent + it->max_phys_descent);
21141 row->extra_line_spacing = max (row->extra_line_spacing,
21142 it->max_extra_line_spacing);
21144 /* End of this display line if row is continued. */
21145 if (row->continued_p || row->ends_at_zv_p)
21146 break;
21149 at_end_of_line:
21150 /* Is this a line end? If yes, we're also done, after making
21151 sure that a non-default face is extended up to the right
21152 margin of the window. */
21153 if (ITERATOR_AT_END_OF_LINE_P (it))
21155 int used_before = row->used[TEXT_AREA];
21157 row->ends_in_newline_from_string_p = STRINGP (it->object);
21159 /* Add a space at the end of the line that is used to
21160 display the cursor there. */
21161 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21162 append_space_for_newline (it, false);
21164 /* Extend the face to the end of the line. */
21165 extend_face_to_end_of_line (it);
21167 /* Make sure we have the position. */
21168 if (used_before == 0)
21169 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21171 /* Record the position of the newline, for use in
21172 find_row_edges. */
21173 it->eol_pos = it->current.pos;
21175 /* Consume the line end. This skips over invisible lines. */
21176 set_iterator_to_next (it, true);
21177 it->continuation_lines_width = 0;
21178 break;
21181 /* Proceed with next display element. Note that this skips
21182 over lines invisible because of selective display. */
21183 set_iterator_to_next (it, true);
21185 /* If we truncate lines, we are done when the last displayed
21186 glyphs reach past the right margin of the window. */
21187 if (it->line_wrap == TRUNCATE
21188 && ((FRAME_WINDOW_P (it->f)
21189 /* Images are preprocessed in produce_image_glyph such
21190 that they are cropped at the right edge of the
21191 window, so an image glyph will always end exactly at
21192 last_visible_x, even if there's no right fringe. */
21193 && ((row->reversed_p
21194 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21195 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21196 || it->what == IT_IMAGE))
21197 ? (it->current_x >= it->last_visible_x)
21198 : (it->current_x > it->last_visible_x)))
21200 /* Maybe add truncation glyphs. */
21201 if (!FRAME_WINDOW_P (it->f)
21202 || (row->reversed_p
21203 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21204 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21206 int i, n;
21208 if (!row->reversed_p)
21210 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21211 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21212 break;
21214 else
21216 for (i = 0; i < row->used[TEXT_AREA]; i++)
21217 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21218 break;
21219 /* Remove any padding glyphs at the front of ROW, to
21220 make room for the truncation glyphs we will be
21221 adding below. The loop below always inserts at
21222 least one truncation glyph, so also remove the
21223 last glyph added to ROW. */
21224 unproduce_glyphs (it, i + 1);
21225 /* Adjust i for the loop below. */
21226 i = row->used[TEXT_AREA] - (i + 1);
21229 /* produce_special_glyphs overwrites the last glyph, so
21230 we don't want that if we want to keep that last
21231 glyph, which means it's an image. */
21232 if (it->current_x > it->last_visible_x)
21234 it->current_x = x_before;
21235 if (!FRAME_WINDOW_P (it->f))
21237 for (n = row->used[TEXT_AREA]; i < n; ++i)
21239 row->used[TEXT_AREA] = i;
21240 produce_special_glyphs (it, IT_TRUNCATION);
21243 else
21245 row->used[TEXT_AREA] = i;
21246 produce_special_glyphs (it, IT_TRUNCATION);
21248 it->hpos = hpos_before;
21251 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21253 /* Don't truncate if we can overflow newline into fringe. */
21254 if (!get_next_display_element (it))
21256 it->continuation_lines_width = 0;
21257 row->ends_at_zv_p = true;
21258 row->exact_window_width_line_p = true;
21259 break;
21261 if (ITERATOR_AT_END_OF_LINE_P (it))
21263 row->exact_window_width_line_p = true;
21264 goto at_end_of_line;
21266 it->current_x = x_before;
21267 it->hpos = hpos_before;
21270 row->truncated_on_right_p = true;
21271 it->continuation_lines_width = 0;
21272 reseat_at_next_visible_line_start (it, false);
21273 /* We insist below that IT's position be at ZV because in
21274 bidi-reordered lines the character at visible line start
21275 might not be the character that follows the newline in
21276 the logical order. */
21277 if (IT_BYTEPOS (*it) > BEG_BYTE)
21278 row->ends_at_zv_p =
21279 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21280 else
21281 row->ends_at_zv_p = false;
21282 break;
21286 if (wrap_data)
21287 bidi_unshelve_cache (wrap_data, true);
21289 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21290 at the left window margin. */
21291 if (it->first_visible_x
21292 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21294 if (!FRAME_WINDOW_P (it->f)
21295 || (((row->reversed_p
21296 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21297 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21298 /* Don't let insert_left_trunc_glyphs overwrite the
21299 first glyph of the row if it is an image. */
21300 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21301 insert_left_trunc_glyphs (it);
21302 row->truncated_on_left_p = true;
21305 /* Remember the position at which this line ends.
21307 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21308 cannot be before the call to find_row_edges below, since that is
21309 where these positions are determined. */
21310 row->end = it->current;
21311 if (!it->bidi_p)
21313 row->minpos = row->start.pos;
21314 row->maxpos = row->end.pos;
21316 else
21318 /* ROW->minpos and ROW->maxpos must be the smallest and
21319 `1 + the largest' buffer positions in ROW. But if ROW was
21320 bidi-reordered, these two positions can be anywhere in the
21321 row, so we must determine them now. */
21322 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21325 /* If the start of this line is the overlay arrow-position, then
21326 mark this glyph row as the one containing the overlay arrow.
21327 This is clearly a mess with variable size fonts. It would be
21328 better to let it be displayed like cursors under X. */
21329 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21330 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21331 !NILP (overlay_arrow_string)))
21333 /* Overlay arrow in window redisplay is a fringe bitmap. */
21334 if (STRINGP (overlay_arrow_string))
21336 struct glyph_row *arrow_row
21337 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21338 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21339 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21340 struct glyph *p = row->glyphs[TEXT_AREA];
21341 struct glyph *p2, *end;
21343 /* Copy the arrow glyphs. */
21344 while (glyph < arrow_end)
21345 *p++ = *glyph++;
21347 /* Throw away padding glyphs. */
21348 p2 = p;
21349 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21350 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21351 ++p2;
21352 if (p2 > p)
21354 while (p2 < end)
21355 *p++ = *p2++;
21356 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21359 else
21361 eassert (INTEGERP (overlay_arrow_string));
21362 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21364 overlay_arrow_seen = true;
21367 /* Highlight trailing whitespace. */
21368 if (!NILP (Vshow_trailing_whitespace))
21369 highlight_trailing_whitespace (it->f, it->glyph_row);
21371 /* Compute pixel dimensions of this line. */
21372 compute_line_metrics (it);
21374 /* Implementation note: No changes in the glyphs of ROW or in their
21375 faces can be done past this point, because compute_line_metrics
21376 computes ROW's hash value and stores it within the glyph_row
21377 structure. */
21379 /* Record whether this row ends inside an ellipsis. */
21380 row->ends_in_ellipsis_p
21381 = (it->method == GET_FROM_DISPLAY_VECTOR
21382 && it->ellipsis_p);
21384 /* Save fringe bitmaps in this row. */
21385 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21386 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21387 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21388 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21390 it->left_user_fringe_bitmap = 0;
21391 it->left_user_fringe_face_id = 0;
21392 it->right_user_fringe_bitmap = 0;
21393 it->right_user_fringe_face_id = 0;
21395 /* Maybe set the cursor. */
21396 cvpos = it->w->cursor.vpos;
21397 if ((cvpos < 0
21398 /* In bidi-reordered rows, keep checking for proper cursor
21399 position even if one has been found already, because buffer
21400 positions in such rows change non-linearly with ROW->VPOS,
21401 when a line is continued. One exception: when we are at ZV,
21402 display cursor on the first suitable glyph row, since all
21403 the empty rows after that also have their position set to ZV. */
21404 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21405 lines' rows is implemented for bidi-reordered rows. */
21406 || (it->bidi_p
21407 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21408 && PT >= MATRIX_ROW_START_CHARPOS (row)
21409 && PT <= MATRIX_ROW_END_CHARPOS (row)
21410 && cursor_row_p (row))
21411 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21413 /* Prepare for the next line. This line starts horizontally at (X
21414 HPOS) = (0 0). Vertical positions are incremented. As a
21415 convenience for the caller, IT->glyph_row is set to the next
21416 row to be used. */
21417 it->current_x = it->hpos = 0;
21418 it->current_y += row->height;
21419 SET_TEXT_POS (it->eol_pos, 0, 0);
21420 ++it->vpos;
21421 ++it->glyph_row;
21422 /* The next row should by default use the same value of the
21423 reversed_p flag as this one. set_iterator_to_next decides when
21424 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21425 the flag accordingly. */
21426 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21427 it->glyph_row->reversed_p = row->reversed_p;
21428 it->start = row->end;
21429 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21431 #undef RECORD_MAX_MIN_POS
21434 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21435 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21436 doc: /* Return paragraph direction at point in BUFFER.
21437 Value is either `left-to-right' or `right-to-left'.
21438 If BUFFER is omitted or nil, it defaults to the current buffer.
21440 Paragraph direction determines how the text in the paragraph is displayed.
21441 In left-to-right paragraphs, text begins at the left margin of the window
21442 and the reading direction is generally left to right. In right-to-left
21443 paragraphs, text begins at the right margin and is read from right to left.
21445 See also `bidi-paragraph-direction'. */)
21446 (Lisp_Object buffer)
21448 struct buffer *buf = current_buffer;
21449 struct buffer *old = buf;
21451 if (! NILP (buffer))
21453 CHECK_BUFFER (buffer);
21454 buf = XBUFFER (buffer);
21457 if (NILP (BVAR (buf, bidi_display_reordering))
21458 || NILP (BVAR (buf, enable_multibyte_characters))
21459 /* When we are loading loadup.el, the character property tables
21460 needed for bidi iteration are not yet available. */
21461 || redisplay__inhibit_bidi)
21462 return Qleft_to_right;
21463 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21464 return BVAR (buf, bidi_paragraph_direction);
21465 else
21467 /* Determine the direction from buffer text. We could try to
21468 use current_matrix if it is up to date, but this seems fast
21469 enough as it is. */
21470 struct bidi_it itb;
21471 ptrdiff_t pos = BUF_PT (buf);
21472 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21473 int c;
21474 void *itb_data = bidi_shelve_cache ();
21476 set_buffer_temp (buf);
21477 /* bidi_paragraph_init finds the base direction of the paragraph
21478 by searching forward from paragraph start. We need the base
21479 direction of the current or _previous_ paragraph, so we need
21480 to make sure we are within that paragraph. To that end, find
21481 the previous non-empty line. */
21482 if (pos >= ZV && pos > BEGV)
21483 DEC_BOTH (pos, bytepos);
21484 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21485 if (fast_looking_at (trailing_white_space,
21486 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21488 while ((c = FETCH_BYTE (bytepos)) == '\n'
21489 || c == ' ' || c == '\t' || c == '\f')
21491 if (bytepos <= BEGV_BYTE)
21492 break;
21493 bytepos--;
21494 pos--;
21496 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21497 bytepos--;
21499 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21500 itb.paragraph_dir = NEUTRAL_DIR;
21501 itb.string.s = NULL;
21502 itb.string.lstring = Qnil;
21503 itb.string.bufpos = 0;
21504 itb.string.from_disp_str = false;
21505 itb.string.unibyte = false;
21506 /* We have no window to use here for ignoring window-specific
21507 overlays. Using NULL for window pointer will cause
21508 compute_display_string_pos to use the current buffer. */
21509 itb.w = NULL;
21510 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21511 bidi_unshelve_cache (itb_data, false);
21512 set_buffer_temp (old);
21513 switch (itb.paragraph_dir)
21515 case L2R:
21516 return Qleft_to_right;
21517 break;
21518 case R2L:
21519 return Qright_to_left;
21520 break;
21521 default:
21522 emacs_abort ();
21527 DEFUN ("bidi-find-overridden-directionality",
21528 Fbidi_find_overridden_directionality,
21529 Sbidi_find_overridden_directionality, 2, 3, 0,
21530 doc: /* Return position between FROM and TO where directionality was overridden.
21532 This function returns the first character position in the specified
21533 region of OBJECT where there is a character whose `bidi-class' property
21534 is `L', but which was forced to display as `R' by a directional
21535 override, and likewise with characters whose `bidi-class' is `R'
21536 or `AL' that were forced to display as `L'.
21538 If no such character is found, the function returns nil.
21540 OBJECT is a Lisp string or buffer to search for overridden
21541 directionality, and defaults to the current buffer if nil or omitted.
21542 OBJECT can also be a window, in which case the function will search
21543 the buffer displayed in that window. Passing the window instead of
21544 a buffer is preferable when the buffer is displayed in some window,
21545 because this function will then be able to correctly account for
21546 window-specific overlays, which can affect the results.
21548 Strong directional characters `L', `R', and `AL' can have their
21549 intrinsic directionality overridden by directional override
21550 control characters RLO (u+202e) and LRO (u+202d). See the
21551 function `get-char-code-property' for a way to inquire about
21552 the `bidi-class' property of a character. */)
21553 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21555 struct buffer *buf = current_buffer;
21556 struct buffer *old = buf;
21557 struct window *w = NULL;
21558 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21559 struct bidi_it itb;
21560 ptrdiff_t from_pos, to_pos, from_bpos;
21561 void *itb_data;
21563 if (!NILP (object))
21565 if (BUFFERP (object))
21566 buf = XBUFFER (object);
21567 else if (WINDOWP (object))
21569 w = decode_live_window (object);
21570 buf = XBUFFER (w->contents);
21571 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21573 else
21574 CHECK_STRING (object);
21577 if (STRINGP (object))
21579 /* Characters in unibyte strings are always treated by bidi.c as
21580 strong LTR. */
21581 if (!STRING_MULTIBYTE (object)
21582 /* When we are loading loadup.el, the character property
21583 tables needed for bidi iteration are not yet
21584 available. */
21585 || redisplay__inhibit_bidi)
21586 return Qnil;
21588 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21589 if (from_pos >= SCHARS (object))
21590 return Qnil;
21592 /* Set up the bidi iterator. */
21593 itb_data = bidi_shelve_cache ();
21594 itb.paragraph_dir = NEUTRAL_DIR;
21595 itb.string.lstring = object;
21596 itb.string.s = NULL;
21597 itb.string.schars = SCHARS (object);
21598 itb.string.bufpos = 0;
21599 itb.string.from_disp_str = false;
21600 itb.string.unibyte = false;
21601 itb.w = w;
21602 bidi_init_it (0, 0, frame_window_p, &itb);
21604 else
21606 /* Nothing this fancy can happen in unibyte buffers, or in a
21607 buffer that disabled reordering, or if FROM is at EOB. */
21608 if (NILP (BVAR (buf, bidi_display_reordering))
21609 || NILP (BVAR (buf, enable_multibyte_characters))
21610 /* When we are loading loadup.el, the character property
21611 tables needed for bidi iteration are not yet
21612 available. */
21613 || redisplay__inhibit_bidi)
21614 return Qnil;
21616 set_buffer_temp (buf);
21617 validate_region (&from, &to);
21618 from_pos = XINT (from);
21619 to_pos = XINT (to);
21620 if (from_pos >= ZV)
21621 return Qnil;
21623 /* Set up the bidi iterator. */
21624 itb_data = bidi_shelve_cache ();
21625 from_bpos = CHAR_TO_BYTE (from_pos);
21626 if (from_pos == BEGV)
21628 itb.charpos = BEGV;
21629 itb.bytepos = BEGV_BYTE;
21631 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21633 itb.charpos = from_pos;
21634 itb.bytepos = from_bpos;
21636 else
21637 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21638 -1, &itb.bytepos);
21639 itb.paragraph_dir = NEUTRAL_DIR;
21640 itb.string.s = NULL;
21641 itb.string.lstring = Qnil;
21642 itb.string.bufpos = 0;
21643 itb.string.from_disp_str = false;
21644 itb.string.unibyte = false;
21645 itb.w = w;
21646 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21649 ptrdiff_t found;
21650 do {
21651 /* For the purposes of this function, the actual base direction of
21652 the paragraph doesn't matter, so just set it to L2R. */
21653 bidi_paragraph_init (L2R, &itb, false);
21654 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21656 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21658 bidi_unshelve_cache (itb_data, false);
21659 set_buffer_temp (old);
21661 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21664 DEFUN ("move-point-visually", Fmove_point_visually,
21665 Smove_point_visually, 1, 1, 0,
21666 doc: /* Move point in the visual order in the specified DIRECTION.
21667 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21668 left.
21670 Value is the new character position of point. */)
21671 (Lisp_Object direction)
21673 struct window *w = XWINDOW (selected_window);
21674 struct buffer *b = XBUFFER (w->contents);
21675 struct glyph_row *row;
21676 int dir;
21677 Lisp_Object paragraph_dir;
21679 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21680 (!(ROW)->continued_p \
21681 && NILP ((GLYPH)->object) \
21682 && (GLYPH)->type == CHAR_GLYPH \
21683 && (GLYPH)->u.ch == ' ' \
21684 && (GLYPH)->charpos >= 0 \
21685 && !(GLYPH)->avoid_cursor_p)
21687 CHECK_NUMBER (direction);
21688 dir = XINT (direction);
21689 if (dir > 0)
21690 dir = 1;
21691 else
21692 dir = -1;
21694 /* If current matrix is up-to-date, we can use the information
21695 recorded in the glyphs, at least as long as the goal is on the
21696 screen. */
21697 if (w->window_end_valid
21698 && !windows_or_buffers_changed
21699 && b
21700 && !b->clip_changed
21701 && !b->prevent_redisplay_optimizations_p
21702 && !window_outdated (w)
21703 /* We rely below on the cursor coordinates to be up to date, but
21704 we cannot trust them if some command moved point since the
21705 last complete redisplay. */
21706 && w->last_point == BUF_PT (b)
21707 && w->cursor.vpos >= 0
21708 && w->cursor.vpos < w->current_matrix->nrows
21709 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21711 struct glyph *g = row->glyphs[TEXT_AREA];
21712 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21713 struct glyph *gpt = g + w->cursor.hpos;
21715 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21717 if (BUFFERP (g->object) && g->charpos != PT)
21719 SET_PT (g->charpos);
21720 w->cursor.vpos = -1;
21721 return make_number (PT);
21723 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21725 ptrdiff_t new_pos;
21727 if (BUFFERP (gpt->object))
21729 new_pos = PT;
21730 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21731 new_pos += (row->reversed_p ? -dir : dir);
21732 else
21733 new_pos -= (row->reversed_p ? -dir : dir);
21735 else if (BUFFERP (g->object))
21736 new_pos = g->charpos;
21737 else
21738 break;
21739 SET_PT (new_pos);
21740 w->cursor.vpos = -1;
21741 return make_number (PT);
21743 else if (ROW_GLYPH_NEWLINE_P (row, g))
21745 /* Glyphs inserted at the end of a non-empty line for
21746 positioning the cursor have zero charpos, so we must
21747 deduce the value of point by other means. */
21748 if (g->charpos > 0)
21749 SET_PT (g->charpos);
21750 else if (row->ends_at_zv_p && PT != ZV)
21751 SET_PT (ZV);
21752 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21753 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21754 else
21755 break;
21756 w->cursor.vpos = -1;
21757 return make_number (PT);
21760 if (g == e || NILP (g->object))
21762 if (row->truncated_on_left_p || row->truncated_on_right_p)
21763 goto simulate_display;
21764 if (!row->reversed_p)
21765 row += dir;
21766 else
21767 row -= dir;
21768 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21769 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21770 goto simulate_display;
21772 if (dir > 0)
21774 if (row->reversed_p && !row->continued_p)
21776 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21777 w->cursor.vpos = -1;
21778 return make_number (PT);
21780 g = row->glyphs[TEXT_AREA];
21781 e = g + row->used[TEXT_AREA];
21782 for ( ; g < e; g++)
21784 if (BUFFERP (g->object)
21785 /* Empty lines have only one glyph, which stands
21786 for the newline, and whose charpos is the
21787 buffer position of the newline. */
21788 || ROW_GLYPH_NEWLINE_P (row, g)
21789 /* When the buffer ends in a newline, the line at
21790 EOB also has one glyph, but its charpos is -1. */
21791 || (row->ends_at_zv_p
21792 && !row->reversed_p
21793 && NILP (g->object)
21794 && g->type == CHAR_GLYPH
21795 && g->u.ch == ' '))
21797 if (g->charpos > 0)
21798 SET_PT (g->charpos);
21799 else if (!row->reversed_p
21800 && row->ends_at_zv_p
21801 && PT != ZV)
21802 SET_PT (ZV);
21803 else
21804 continue;
21805 w->cursor.vpos = -1;
21806 return make_number (PT);
21810 else
21812 if (!row->reversed_p && !row->continued_p)
21814 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21815 w->cursor.vpos = -1;
21816 return make_number (PT);
21818 e = row->glyphs[TEXT_AREA];
21819 g = e + row->used[TEXT_AREA] - 1;
21820 for ( ; g >= e; g--)
21822 if (BUFFERP (g->object)
21823 || (ROW_GLYPH_NEWLINE_P (row, g)
21824 && g->charpos > 0)
21825 /* Empty R2L lines on GUI frames have the buffer
21826 position of the newline stored in the stretch
21827 glyph. */
21828 || g->type == STRETCH_GLYPH
21829 || (row->ends_at_zv_p
21830 && row->reversed_p
21831 && NILP (g->object)
21832 && g->type == CHAR_GLYPH
21833 && g->u.ch == ' '))
21835 if (g->charpos > 0)
21836 SET_PT (g->charpos);
21837 else if (row->reversed_p
21838 && row->ends_at_zv_p
21839 && PT != ZV)
21840 SET_PT (ZV);
21841 else
21842 continue;
21843 w->cursor.vpos = -1;
21844 return make_number (PT);
21851 simulate_display:
21853 /* If we wind up here, we failed to move by using the glyphs, so we
21854 need to simulate display instead. */
21856 if (b)
21857 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21858 else
21859 paragraph_dir = Qleft_to_right;
21860 if (EQ (paragraph_dir, Qright_to_left))
21861 dir = -dir;
21862 if (PT <= BEGV && dir < 0)
21863 xsignal0 (Qbeginning_of_buffer);
21864 else if (PT >= ZV && dir > 0)
21865 xsignal0 (Qend_of_buffer);
21866 else
21868 struct text_pos pt;
21869 struct it it;
21870 int pt_x, target_x, pixel_width, pt_vpos;
21871 bool at_eol_p;
21872 bool overshoot_expected = false;
21873 bool target_is_eol_p = false;
21875 /* Setup the arena. */
21876 SET_TEXT_POS (pt, PT, PT_BYTE);
21877 start_display (&it, w, pt);
21878 /* When lines are truncated, we could be called with point
21879 outside of the windows edges, in which case move_it_*
21880 functions either prematurely stop at window's edge or jump to
21881 the next screen line, whereas we rely below on our ability to
21882 reach point, in order to start from its X coordinate. So we
21883 need to disregard the window's horizontal extent in that case. */
21884 if (it.line_wrap == TRUNCATE)
21885 it.last_visible_x = INFINITY;
21887 if (it.cmp_it.id < 0
21888 && it.method == GET_FROM_STRING
21889 && it.area == TEXT_AREA
21890 && it.string_from_display_prop_p
21891 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21892 overshoot_expected = true;
21894 /* Find the X coordinate of point. We start from the beginning
21895 of this or previous line to make sure we are before point in
21896 the logical order (since the move_it_* functions can only
21897 move forward). */
21898 reseat:
21899 reseat_at_previous_visible_line_start (&it);
21900 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21901 if (IT_CHARPOS (it) != PT)
21903 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21904 -1, -1, -1, MOVE_TO_POS);
21905 /* If we missed point because the character there is
21906 displayed out of a display vector that has more than one
21907 glyph, retry expecting overshoot. */
21908 if (it.method == GET_FROM_DISPLAY_VECTOR
21909 && it.current.dpvec_index > 0
21910 && !overshoot_expected)
21912 overshoot_expected = true;
21913 goto reseat;
21915 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21916 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21918 pt_x = it.current_x;
21919 pt_vpos = it.vpos;
21920 if (dir > 0 || overshoot_expected)
21922 struct glyph_row *row = it.glyph_row;
21924 /* When point is at beginning of line, we don't have
21925 information about the glyph there loaded into struct
21926 it. Calling get_next_display_element fixes that. */
21927 if (pt_x == 0)
21928 get_next_display_element (&it);
21929 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21930 it.glyph_row = NULL;
21931 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21932 it.glyph_row = row;
21933 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21934 it, lest it will become out of sync with it's buffer
21935 position. */
21936 it.current_x = pt_x;
21938 else
21939 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21940 pixel_width = it.pixel_width;
21941 if (overshoot_expected && at_eol_p)
21942 pixel_width = 0;
21943 else if (pixel_width <= 0)
21944 pixel_width = 1;
21946 /* If there's a display string (or something similar) at point,
21947 we are actually at the glyph to the left of point, so we need
21948 to correct the X coordinate. */
21949 if (overshoot_expected)
21951 if (it.bidi_p)
21952 pt_x += pixel_width * it.bidi_it.scan_dir;
21953 else
21954 pt_x += pixel_width;
21957 /* Compute target X coordinate, either to the left or to the
21958 right of point. On TTY frames, all characters have the same
21959 pixel width of 1, so we can use that. On GUI frames we don't
21960 have an easy way of getting at the pixel width of the
21961 character to the left of point, so we use a different method
21962 of getting to that place. */
21963 if (dir > 0)
21964 target_x = pt_x + pixel_width;
21965 else
21966 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21968 /* Target X coordinate could be one line above or below the line
21969 of point, in which case we need to adjust the target X
21970 coordinate. Also, if moving to the left, we need to begin at
21971 the left edge of the point's screen line. */
21972 if (dir < 0)
21974 if (pt_x > 0)
21976 start_display (&it, w, pt);
21977 if (it.line_wrap == TRUNCATE)
21978 it.last_visible_x = INFINITY;
21979 reseat_at_previous_visible_line_start (&it);
21980 it.current_x = it.current_y = it.hpos = 0;
21981 if (pt_vpos != 0)
21982 move_it_by_lines (&it, pt_vpos);
21984 else
21986 move_it_by_lines (&it, -1);
21987 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21988 target_is_eol_p = true;
21989 /* Under word-wrap, we don't know the x coordinate of
21990 the last character displayed on the previous line,
21991 which immediately precedes the wrap point. To find
21992 out its x coordinate, we try moving to the right
21993 margin of the window, which will stop at the wrap
21994 point, and then reset target_x to point at the
21995 character that precedes the wrap point. This is not
21996 needed on GUI frames, because (see below) there we
21997 move from the left margin one grapheme cluster at a
21998 time, and stop when we hit the wrap point. */
21999 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22001 void *it_data = NULL;
22002 struct it it2;
22004 SAVE_IT (it2, it, it_data);
22005 move_it_in_display_line_to (&it, ZV, target_x,
22006 MOVE_TO_POS | MOVE_TO_X);
22007 /* If we arrived at target_x, that _is_ the last
22008 character on the previous line. */
22009 if (it.current_x != target_x)
22010 target_x = it.current_x - 1;
22011 RESTORE_IT (&it, &it2, it_data);
22015 else
22017 if (at_eol_p
22018 || (target_x >= it.last_visible_x
22019 && it.line_wrap != TRUNCATE))
22021 if (pt_x > 0)
22022 move_it_by_lines (&it, 0);
22023 move_it_by_lines (&it, 1);
22024 target_x = 0;
22028 /* Move to the target X coordinate. */
22029 /* On GUI frames, as we don't know the X coordinate of the
22030 character to the left of point, moving point to the left
22031 requires walking, one grapheme cluster at a time, until we
22032 find ourself at a place immediately to the left of the
22033 character at point. */
22034 if (FRAME_WINDOW_P (it.f) && dir < 0)
22036 struct text_pos new_pos;
22037 enum move_it_result rc = MOVE_X_REACHED;
22039 if (it.current_x == 0)
22040 get_next_display_element (&it);
22041 if (it.what == IT_COMPOSITION)
22043 new_pos.charpos = it.cmp_it.charpos;
22044 new_pos.bytepos = -1;
22046 else
22047 new_pos = it.current.pos;
22049 while (it.current_x + it.pixel_width <= target_x
22050 && (rc == MOVE_X_REACHED
22051 /* Under word-wrap, move_it_in_display_line_to
22052 stops at correct coordinates, but sometimes
22053 returns MOVE_POS_MATCH_OR_ZV. */
22054 || (it.line_wrap == WORD_WRAP
22055 && rc == MOVE_POS_MATCH_OR_ZV)))
22057 int new_x = it.current_x + it.pixel_width;
22059 /* For composed characters, we want the position of the
22060 first character in the grapheme cluster (usually, the
22061 composition's base character), whereas it.current
22062 might give us the position of the _last_ one, e.g. if
22063 the composition is rendered in reverse due to bidi
22064 reordering. */
22065 if (it.what == IT_COMPOSITION)
22067 new_pos.charpos = it.cmp_it.charpos;
22068 new_pos.bytepos = -1;
22070 else
22071 new_pos = it.current.pos;
22072 if (new_x == it.current_x)
22073 new_x++;
22074 rc = move_it_in_display_line_to (&it, ZV, new_x,
22075 MOVE_TO_POS | MOVE_TO_X);
22076 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22077 break;
22079 /* The previous position we saw in the loop is the one we
22080 want. */
22081 if (new_pos.bytepos == -1)
22082 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22083 it.current.pos = new_pos;
22085 else if (it.current_x != target_x)
22086 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22088 /* If we ended up in a display string that covers point, move to
22089 buffer position to the right in the visual order. */
22090 if (dir > 0)
22092 while (IT_CHARPOS (it) == PT)
22094 set_iterator_to_next (&it, false);
22095 if (!get_next_display_element (&it))
22096 break;
22100 /* Move point to that position. */
22101 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22104 return make_number (PT);
22106 #undef ROW_GLYPH_NEWLINE_P
22109 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22110 Sbidi_resolved_levels, 0, 1, 0,
22111 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22113 The resolved levels are produced by the Emacs bidi reordering engine
22114 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22115 read the Unicode Standard Annex 9 (UAX#9) for background information
22116 about these levels.
22118 VPOS is the zero-based number of the current window's screen line
22119 for which to produce the resolved levels. If VPOS is nil or omitted,
22120 it defaults to the screen line of point. If the window displays a
22121 header line, VPOS of zero will report on the header line, and first
22122 line of text in the window will have VPOS of 1.
22124 Value is an array of resolved levels, indexed by glyph number.
22125 Glyphs are numbered from zero starting from the beginning of the
22126 screen line, i.e. the left edge of the window for left-to-right lines
22127 and from the right edge for right-to-left lines. The resolved levels
22128 are produced only for the window's text area; text in display margins
22129 is not included.
22131 If the selected window's display is not up-to-date, or if the specified
22132 screen line does not display text, this function returns nil. It is
22133 highly recommended to bind this function to some simple key, like F8,
22134 in order to avoid these problems.
22136 This function exists mainly for testing the correctness of the
22137 Emacs UBA implementation, in particular with the test suite. */)
22138 (Lisp_Object vpos)
22140 struct window *w = XWINDOW (selected_window);
22141 struct buffer *b = XBUFFER (w->contents);
22142 int nrow;
22143 struct glyph_row *row;
22145 if (NILP (vpos))
22147 int d1, d2, d3, d4, d5;
22149 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22151 else
22153 CHECK_NUMBER_COERCE_MARKER (vpos);
22154 nrow = XINT (vpos);
22157 /* We require up-to-date glyph matrix for this window. */
22158 if (w->window_end_valid
22159 && !windows_or_buffers_changed
22160 && b
22161 && !b->clip_changed
22162 && !b->prevent_redisplay_optimizations_p
22163 && !window_outdated (w)
22164 && nrow >= 0
22165 && nrow < w->current_matrix->nrows
22166 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22167 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22169 struct glyph *g, *e, *g1;
22170 int nglyphs, i;
22171 Lisp_Object levels;
22173 if (!row->reversed_p) /* Left-to-right glyph row. */
22175 g = g1 = row->glyphs[TEXT_AREA];
22176 e = g + row->used[TEXT_AREA];
22178 /* Skip over glyphs at the start of the row that was
22179 generated by redisplay for its own needs. */
22180 while (g < e
22181 && NILP (g->object)
22182 && g->charpos < 0)
22183 g++;
22184 g1 = g;
22186 /* Count the "interesting" glyphs in this row. */
22187 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22188 nglyphs++;
22190 /* Create and fill the array. */
22191 levels = make_uninit_vector (nglyphs);
22192 for (i = 0; g1 < g; i++, g1++)
22193 ASET (levels, i, make_number (g1->resolved_level));
22195 else /* Right-to-left glyph row. */
22197 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22198 e = row->glyphs[TEXT_AREA] - 1;
22199 while (g > e
22200 && NILP (g->object)
22201 && g->charpos < 0)
22202 g--;
22203 g1 = g;
22204 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22205 nglyphs++;
22206 levels = make_uninit_vector (nglyphs);
22207 for (i = 0; g1 > g; i++, g1--)
22208 ASET (levels, i, make_number (g1->resolved_level));
22210 return levels;
22212 else
22213 return Qnil;
22218 /***********************************************************************
22219 Menu Bar
22220 ***********************************************************************/
22222 /* Redisplay the menu bar in the frame for window W.
22224 The menu bar of X frames that don't have X toolkit support is
22225 displayed in a special window W->frame->menu_bar_window.
22227 The menu bar of terminal frames is treated specially as far as
22228 glyph matrices are concerned. Menu bar lines are not part of
22229 windows, so the update is done directly on the frame matrix rows
22230 for the menu bar. */
22232 static void
22233 display_menu_bar (struct window *w)
22235 struct frame *f = XFRAME (WINDOW_FRAME (w));
22236 struct it it;
22237 Lisp_Object items;
22238 int i;
22240 /* Don't do all this for graphical frames. */
22241 #ifdef HAVE_NTGUI
22242 if (FRAME_W32_P (f))
22243 return;
22244 #endif
22245 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22246 if (FRAME_X_P (f))
22247 return;
22248 #endif
22250 #ifdef HAVE_NS
22251 if (FRAME_NS_P (f))
22252 return;
22253 #endif /* HAVE_NS */
22255 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22256 eassert (!FRAME_WINDOW_P (f));
22257 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22258 it.first_visible_x = 0;
22259 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22260 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22261 if (FRAME_WINDOW_P (f))
22263 /* Menu bar lines are displayed in the desired matrix of the
22264 dummy window menu_bar_window. */
22265 struct window *menu_w;
22266 menu_w = XWINDOW (f->menu_bar_window);
22267 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22268 MENU_FACE_ID);
22269 it.first_visible_x = 0;
22270 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22272 else
22273 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22275 /* This is a TTY frame, i.e. character hpos/vpos are used as
22276 pixel x/y. */
22277 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22278 MENU_FACE_ID);
22279 it.first_visible_x = 0;
22280 it.last_visible_x = FRAME_COLS (f);
22283 /* FIXME: This should be controlled by a user option. See the
22284 comments in redisplay_tool_bar and display_mode_line about
22285 this. */
22286 it.paragraph_embedding = L2R;
22288 /* Clear all rows of the menu bar. */
22289 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22291 struct glyph_row *row = it.glyph_row + i;
22292 clear_glyph_row (row);
22293 row->enabled_p = true;
22294 row->full_width_p = true;
22295 row->reversed_p = false;
22298 /* Display all items of the menu bar. */
22299 items = FRAME_MENU_BAR_ITEMS (it.f);
22300 for (i = 0; i < ASIZE (items); i += 4)
22302 Lisp_Object string;
22304 /* Stop at nil string. */
22305 string = AREF (items, i + 1);
22306 if (NILP (string))
22307 break;
22309 /* Remember where item was displayed. */
22310 ASET (items, i + 3, make_number (it.hpos));
22312 /* Display the item, pad with one space. */
22313 if (it.current_x < it.last_visible_x)
22314 display_string (NULL, string, Qnil, 0, 0, &it,
22315 SCHARS (string) + 1, 0, 0, -1);
22318 /* Fill out the line with spaces. */
22319 if (it.current_x < it.last_visible_x)
22320 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22322 /* Compute the total height of the lines. */
22323 compute_line_metrics (&it);
22326 /* Deep copy of a glyph row, including the glyphs. */
22327 static void
22328 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22330 struct glyph *pointers[1 + LAST_AREA];
22331 int to_used = to->used[TEXT_AREA];
22333 /* Save glyph pointers of TO. */
22334 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22336 /* Do a structure assignment. */
22337 *to = *from;
22339 /* Restore original glyph pointers of TO. */
22340 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22342 /* Copy the glyphs. */
22343 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22344 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22346 /* If we filled only part of the TO row, fill the rest with
22347 space_glyph (which will display as empty space). */
22348 if (to_used > from->used[TEXT_AREA])
22349 fill_up_frame_row_with_spaces (to, to_used);
22352 /* Display one menu item on a TTY, by overwriting the glyphs in the
22353 frame F's desired glyph matrix with glyphs produced from the menu
22354 item text. Called from term.c to display TTY drop-down menus one
22355 item at a time.
22357 ITEM_TEXT is the menu item text as a C string.
22359 FACE_ID is the face ID to be used for this menu item. FACE_ID
22360 could specify one of 3 faces: a face for an enabled item, a face
22361 for a disabled item, or a face for a selected item.
22363 X and Y are coordinates of the first glyph in the frame's desired
22364 matrix to be overwritten by the menu item. Since this is a TTY, Y
22365 is the zero-based number of the glyph row and X is the zero-based
22366 glyph number in the row, starting from left, where to start
22367 displaying the item.
22369 SUBMENU means this menu item drops down a submenu, which
22370 should be indicated by displaying a proper visual cue after the
22371 item text. */
22373 void
22374 display_tty_menu_item (const char *item_text, int width, int face_id,
22375 int x, int y, bool submenu)
22377 struct it it;
22378 struct frame *f = SELECTED_FRAME ();
22379 struct window *w = XWINDOW (f->selected_window);
22380 struct glyph_row *row;
22381 size_t item_len = strlen (item_text);
22383 eassert (FRAME_TERMCAP_P (f));
22385 /* Don't write beyond the matrix's last row. This can happen for
22386 TTY screens that are not high enough to show the entire menu.
22387 (This is actually a bit of defensive programming, as
22388 tty_menu_display already limits the number of menu items to one
22389 less than the number of screen lines.) */
22390 if (y >= f->desired_matrix->nrows)
22391 return;
22393 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22394 it.first_visible_x = 0;
22395 it.last_visible_x = FRAME_COLS (f) - 1;
22396 row = it.glyph_row;
22397 /* Start with the row contents from the current matrix. */
22398 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22399 bool saved_width = row->full_width_p;
22400 row->full_width_p = true;
22401 bool saved_reversed = row->reversed_p;
22402 row->reversed_p = false;
22403 row->enabled_p = true;
22405 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22406 desired face. */
22407 eassert (x < f->desired_matrix->matrix_w);
22408 it.current_x = it.hpos = x;
22409 it.current_y = it.vpos = y;
22410 int saved_used = row->used[TEXT_AREA];
22411 bool saved_truncated = row->truncated_on_right_p;
22412 row->used[TEXT_AREA] = x;
22413 it.face_id = face_id;
22414 it.line_wrap = TRUNCATE;
22416 /* FIXME: This should be controlled by a user option. See the
22417 comments in redisplay_tool_bar and display_mode_line about this.
22418 Also, if paragraph_embedding could ever be R2L, changes will be
22419 needed to avoid shifting to the right the row characters in
22420 term.c:append_glyph. */
22421 it.paragraph_embedding = L2R;
22423 /* Pad with a space on the left. */
22424 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22425 width--;
22426 /* Display the menu item, pad with spaces to WIDTH. */
22427 if (submenu)
22429 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22430 item_len, 0, FRAME_COLS (f) - 1, -1);
22431 width -= item_len;
22432 /* Indicate with " >" that there's a submenu. */
22433 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22434 FRAME_COLS (f) - 1, -1);
22436 else
22437 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22438 width, 0, FRAME_COLS (f) - 1, -1);
22440 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22441 row->truncated_on_right_p = saved_truncated;
22442 row->hash = row_hash (row);
22443 row->full_width_p = saved_width;
22444 row->reversed_p = saved_reversed;
22447 /***********************************************************************
22448 Mode Line
22449 ***********************************************************************/
22451 /* Redisplay mode lines in the window tree whose root is WINDOW.
22452 If FORCE, redisplay mode lines unconditionally.
22453 Otherwise, redisplay only mode lines that are garbaged. Value is
22454 the number of windows whose mode lines were redisplayed. */
22456 static int
22457 redisplay_mode_lines (Lisp_Object window, bool force)
22459 int nwindows = 0;
22461 while (!NILP (window))
22463 struct window *w = XWINDOW (window);
22465 if (WINDOWP (w->contents))
22466 nwindows += redisplay_mode_lines (w->contents, force);
22467 else if (force
22468 || FRAME_GARBAGED_P (XFRAME (w->frame))
22469 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22471 struct text_pos lpoint;
22472 struct buffer *old = current_buffer;
22474 /* Set the window's buffer for the mode line display. */
22475 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22476 set_buffer_internal_1 (XBUFFER (w->contents));
22478 /* Point refers normally to the selected window. For any
22479 other window, set up appropriate value. */
22480 if (!EQ (window, selected_window))
22482 struct text_pos pt;
22484 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22485 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22488 /* Display mode lines. */
22489 clear_glyph_matrix (w->desired_matrix);
22490 if (display_mode_lines (w))
22491 ++nwindows;
22493 /* Restore old settings. */
22494 set_buffer_internal_1 (old);
22495 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22498 window = w->next;
22501 return nwindows;
22505 /* Display the mode and/or header line of window W. Value is the
22506 sum number of mode lines and header lines displayed. */
22508 static int
22509 display_mode_lines (struct window *w)
22511 Lisp_Object old_selected_window = selected_window;
22512 Lisp_Object old_selected_frame = selected_frame;
22513 Lisp_Object new_frame = w->frame;
22514 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22515 int n = 0;
22517 selected_frame = new_frame;
22518 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22519 or window's point, then we'd need select_window_1 here as well. */
22520 XSETWINDOW (selected_window, w);
22521 XFRAME (new_frame)->selected_window = selected_window;
22523 /* These will be set while the mode line specs are processed. */
22524 line_number_displayed = false;
22525 w->column_number_displayed = -1;
22527 if (WINDOW_WANTS_MODELINE_P (w))
22529 struct window *sel_w = XWINDOW (old_selected_window);
22531 /* Select mode line face based on the real selected window. */
22532 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22533 BVAR (current_buffer, mode_line_format));
22534 ++n;
22537 if (WINDOW_WANTS_HEADER_LINE_P (w))
22539 display_mode_line (w, HEADER_LINE_FACE_ID,
22540 BVAR (current_buffer, header_line_format));
22541 ++n;
22544 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22545 selected_frame = old_selected_frame;
22546 selected_window = old_selected_window;
22547 if (n > 0)
22548 w->must_be_updated_p = true;
22549 return n;
22553 /* Display mode or header line of window W. FACE_ID specifies which
22554 line to display; it is either MODE_LINE_FACE_ID or
22555 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22556 display. Value is the pixel height of the mode/header line
22557 displayed. */
22559 static int
22560 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22562 struct it it;
22563 struct face *face;
22564 ptrdiff_t count = SPECPDL_INDEX ();
22566 init_iterator (&it, w, -1, -1, NULL, face_id);
22567 /* Don't extend on a previously drawn mode-line.
22568 This may happen if called from pos_visible_p. */
22569 it.glyph_row->enabled_p = false;
22570 prepare_desired_row (w, it.glyph_row, true);
22572 it.glyph_row->mode_line_p = true;
22574 /* FIXME: This should be controlled by a user option. But
22575 supporting such an option is not trivial, since the mode line is
22576 made up of many separate strings. */
22577 it.paragraph_embedding = L2R;
22579 record_unwind_protect (unwind_format_mode_line,
22580 format_mode_line_unwind_data (NULL, NULL,
22581 Qnil, false));
22583 mode_line_target = MODE_LINE_DISPLAY;
22585 /* Temporarily make frame's keyboard the current kboard so that
22586 kboard-local variables in the mode_line_format will get the right
22587 values. */
22588 push_kboard (FRAME_KBOARD (it.f));
22589 record_unwind_save_match_data ();
22590 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22591 pop_kboard ();
22593 unbind_to (count, Qnil);
22595 /* Fill up with spaces. */
22596 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22598 compute_line_metrics (&it);
22599 it.glyph_row->full_width_p = true;
22600 it.glyph_row->continued_p = false;
22601 it.glyph_row->truncated_on_left_p = false;
22602 it.glyph_row->truncated_on_right_p = false;
22604 /* Make a 3D mode-line have a shadow at its right end. */
22605 face = FACE_FROM_ID (it.f, face_id);
22606 extend_face_to_end_of_line (&it);
22607 if (face->box != FACE_NO_BOX)
22609 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22610 + it.glyph_row->used[TEXT_AREA] - 1);
22611 last->right_box_line_p = true;
22614 return it.glyph_row->height;
22617 /* Move element ELT in LIST to the front of LIST.
22618 Return the updated list. */
22620 static Lisp_Object
22621 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22623 register Lisp_Object tail, prev;
22624 register Lisp_Object tem;
22626 tail = list;
22627 prev = Qnil;
22628 while (CONSP (tail))
22630 tem = XCAR (tail);
22632 if (EQ (elt, tem))
22634 /* Splice out the link TAIL. */
22635 if (NILP (prev))
22636 list = XCDR (tail);
22637 else
22638 Fsetcdr (prev, XCDR (tail));
22640 /* Now make it the first. */
22641 Fsetcdr (tail, list);
22642 return tail;
22644 else
22645 prev = tail;
22646 tail = XCDR (tail);
22647 QUIT;
22650 /* Not found--return unchanged LIST. */
22651 return list;
22654 /* Contribute ELT to the mode line for window IT->w. How it
22655 translates into text depends on its data type.
22657 IT describes the display environment in which we display, as usual.
22659 DEPTH is the depth in recursion. It is used to prevent
22660 infinite recursion here.
22662 FIELD_WIDTH is the number of characters the display of ELT should
22663 occupy in the mode line, and PRECISION is the maximum number of
22664 characters to display from ELT's representation. See
22665 display_string for details.
22667 Returns the hpos of the end of the text generated by ELT.
22669 PROPS is a property list to add to any string we encounter.
22671 If RISKY, remove (disregard) any properties in any string
22672 we encounter, and ignore :eval and :propertize.
22674 The global variable `mode_line_target' determines whether the
22675 output is passed to `store_mode_line_noprop',
22676 `store_mode_line_string', or `display_string'. */
22678 static int
22679 display_mode_element (struct it *it, int depth, int field_width, int precision,
22680 Lisp_Object elt, Lisp_Object props, bool risky)
22682 int n = 0, field, prec;
22683 bool literal = false;
22685 tail_recurse:
22686 if (depth > 100)
22687 elt = build_string ("*too-deep*");
22689 depth++;
22691 switch (XTYPE (elt))
22693 case Lisp_String:
22695 /* A string: output it and check for %-constructs within it. */
22696 unsigned char c;
22697 ptrdiff_t offset = 0;
22699 if (SCHARS (elt) > 0
22700 && (!NILP (props) || risky))
22702 Lisp_Object oprops, aelt;
22703 oprops = Ftext_properties_at (make_number (0), elt);
22705 /* If the starting string's properties are not what
22706 we want, translate the string. Also, if the string
22707 is risky, do that anyway. */
22709 if (NILP (Fequal (props, oprops)) || risky)
22711 /* If the starting string has properties,
22712 merge the specified ones onto the existing ones. */
22713 if (! NILP (oprops) && !risky)
22715 Lisp_Object tem;
22717 oprops = Fcopy_sequence (oprops);
22718 tem = props;
22719 while (CONSP (tem))
22721 oprops = Fplist_put (oprops, XCAR (tem),
22722 XCAR (XCDR (tem)));
22723 tem = XCDR (XCDR (tem));
22725 props = oprops;
22728 aelt = Fassoc (elt, mode_line_proptrans_alist);
22729 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22731 /* AELT is what we want. Move it to the front
22732 without consing. */
22733 elt = XCAR (aelt);
22734 mode_line_proptrans_alist
22735 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22737 else
22739 Lisp_Object tem;
22741 /* If AELT has the wrong props, it is useless.
22742 so get rid of it. */
22743 if (! NILP (aelt))
22744 mode_line_proptrans_alist
22745 = Fdelq (aelt, mode_line_proptrans_alist);
22747 elt = Fcopy_sequence (elt);
22748 Fset_text_properties (make_number (0), Flength (elt),
22749 props, elt);
22750 /* Add this item to mode_line_proptrans_alist. */
22751 mode_line_proptrans_alist
22752 = Fcons (Fcons (elt, props),
22753 mode_line_proptrans_alist);
22754 /* Truncate mode_line_proptrans_alist
22755 to at most 50 elements. */
22756 tem = Fnthcdr (make_number (50),
22757 mode_line_proptrans_alist);
22758 if (! NILP (tem))
22759 XSETCDR (tem, Qnil);
22764 offset = 0;
22766 if (literal)
22768 prec = precision - n;
22769 switch (mode_line_target)
22771 case MODE_LINE_NOPROP:
22772 case MODE_LINE_TITLE:
22773 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22774 break;
22775 case MODE_LINE_STRING:
22776 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22777 break;
22778 case MODE_LINE_DISPLAY:
22779 n += display_string (NULL, elt, Qnil, 0, 0, it,
22780 0, prec, 0, STRING_MULTIBYTE (elt));
22781 break;
22784 break;
22787 /* Handle the non-literal case. */
22789 while ((precision <= 0 || n < precision)
22790 && SREF (elt, offset) != 0
22791 && (mode_line_target != MODE_LINE_DISPLAY
22792 || it->current_x < it->last_visible_x))
22794 ptrdiff_t last_offset = offset;
22796 /* Advance to end of string or next format specifier. */
22797 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22800 if (offset - 1 != last_offset)
22802 ptrdiff_t nchars, nbytes;
22804 /* Output to end of string or up to '%'. Field width
22805 is length of string. Don't output more than
22806 PRECISION allows us. */
22807 offset--;
22809 prec = c_string_width (SDATA (elt) + last_offset,
22810 offset - last_offset, precision - n,
22811 &nchars, &nbytes);
22813 switch (mode_line_target)
22815 case MODE_LINE_NOPROP:
22816 case MODE_LINE_TITLE:
22817 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22818 break;
22819 case MODE_LINE_STRING:
22821 ptrdiff_t bytepos = last_offset;
22822 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22823 ptrdiff_t endpos = (precision <= 0
22824 ? string_byte_to_char (elt, offset)
22825 : charpos + nchars);
22826 Lisp_Object mode_string
22827 = Fsubstring (elt, make_number (charpos),
22828 make_number (endpos));
22829 n += store_mode_line_string (NULL, mode_string, false,
22830 0, 0, Qnil);
22832 break;
22833 case MODE_LINE_DISPLAY:
22835 ptrdiff_t bytepos = last_offset;
22836 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22838 if (precision <= 0)
22839 nchars = string_byte_to_char (elt, offset) - charpos;
22840 n += display_string (NULL, elt, Qnil, 0, charpos,
22841 it, 0, nchars, 0,
22842 STRING_MULTIBYTE (elt));
22844 break;
22847 else /* c == '%' */
22849 ptrdiff_t percent_position = offset;
22851 /* Get the specified minimum width. Zero means
22852 don't pad. */
22853 field = 0;
22854 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22855 field = field * 10 + c - '0';
22857 /* Don't pad beyond the total padding allowed. */
22858 if (field_width - n > 0 && field > field_width - n)
22859 field = field_width - n;
22861 /* Note that either PRECISION <= 0 or N < PRECISION. */
22862 prec = precision - n;
22864 if (c == 'M')
22865 n += display_mode_element (it, depth, field, prec,
22866 Vglobal_mode_string, props,
22867 risky);
22868 else if (c != 0)
22870 bool multibyte;
22871 ptrdiff_t bytepos, charpos;
22872 const char *spec;
22873 Lisp_Object string;
22875 bytepos = percent_position;
22876 charpos = (STRING_MULTIBYTE (elt)
22877 ? string_byte_to_char (elt, bytepos)
22878 : bytepos);
22879 spec = decode_mode_spec (it->w, c, field, &string);
22880 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22882 switch (mode_line_target)
22884 case MODE_LINE_NOPROP:
22885 case MODE_LINE_TITLE:
22886 n += store_mode_line_noprop (spec, field, prec);
22887 break;
22888 case MODE_LINE_STRING:
22890 Lisp_Object tem = build_string (spec);
22891 props = Ftext_properties_at (make_number (charpos), elt);
22892 /* Should only keep face property in props */
22893 n += store_mode_line_string (NULL, tem, false,
22894 field, prec, props);
22896 break;
22897 case MODE_LINE_DISPLAY:
22899 int nglyphs_before, nwritten;
22901 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22902 nwritten = display_string (spec, string, elt,
22903 charpos, 0, it,
22904 field, prec, 0,
22905 multibyte);
22907 /* Assign to the glyphs written above the
22908 string where the `%x' came from, position
22909 of the `%'. */
22910 if (nwritten > 0)
22912 struct glyph *glyph
22913 = (it->glyph_row->glyphs[TEXT_AREA]
22914 + nglyphs_before);
22915 int i;
22917 for (i = 0; i < nwritten; ++i)
22919 glyph[i].object = elt;
22920 glyph[i].charpos = charpos;
22923 n += nwritten;
22926 break;
22929 else /* c == 0 */
22930 break;
22934 break;
22936 case Lisp_Symbol:
22937 /* A symbol: process the value of the symbol recursively
22938 as if it appeared here directly. Avoid error if symbol void.
22939 Special case: if value of symbol is a string, output the string
22940 literally. */
22942 register Lisp_Object tem;
22944 /* If the variable is not marked as risky to set
22945 then its contents are risky to use. */
22946 if (NILP (Fget (elt, Qrisky_local_variable)))
22947 risky = true;
22949 tem = Fboundp (elt);
22950 if (!NILP (tem))
22952 tem = Fsymbol_value (elt);
22953 /* If value is a string, output that string literally:
22954 don't check for % within it. */
22955 if (STRINGP (tem))
22956 literal = true;
22958 if (!EQ (tem, elt))
22960 /* Give up right away for nil or t. */
22961 elt = tem;
22962 goto tail_recurse;
22966 break;
22968 case Lisp_Cons:
22970 register Lisp_Object car, tem;
22972 /* A cons cell: five distinct cases.
22973 If first element is :eval or :propertize, do something special.
22974 If first element is a string or a cons, process all the elements
22975 and effectively concatenate them.
22976 If first element is a negative number, truncate displaying cdr to
22977 at most that many characters. If positive, pad (with spaces)
22978 to at least that many characters.
22979 If first element is a symbol, process the cadr or caddr recursively
22980 according to whether the symbol's value is non-nil or nil. */
22981 car = XCAR (elt);
22982 if (EQ (car, QCeval))
22984 /* An element of the form (:eval FORM) means evaluate FORM
22985 and use the result as mode line elements. */
22987 if (risky)
22988 break;
22990 if (CONSP (XCDR (elt)))
22992 Lisp_Object spec;
22993 spec = safe__eval (true, XCAR (XCDR (elt)));
22994 n += display_mode_element (it, depth, field_width - n,
22995 precision - n, spec, props,
22996 risky);
22999 else if (EQ (car, QCpropertize))
23001 /* An element of the form (:propertize ELT PROPS...)
23002 means display ELT but applying properties PROPS. */
23004 if (risky)
23005 break;
23007 if (CONSP (XCDR (elt)))
23008 n += display_mode_element (it, depth, field_width - n,
23009 precision - n, XCAR (XCDR (elt)),
23010 XCDR (XCDR (elt)), risky);
23012 else if (SYMBOLP (car))
23014 tem = Fboundp (car);
23015 elt = XCDR (elt);
23016 if (!CONSP (elt))
23017 goto invalid;
23018 /* elt is now the cdr, and we know it is a cons cell.
23019 Use its car if CAR has a non-nil value. */
23020 if (!NILP (tem))
23022 tem = Fsymbol_value (car);
23023 if (!NILP (tem))
23025 elt = XCAR (elt);
23026 goto tail_recurse;
23029 /* Symbol's value is nil (or symbol is unbound)
23030 Get the cddr of the original list
23031 and if possible find the caddr and use that. */
23032 elt = XCDR (elt);
23033 if (NILP (elt))
23034 break;
23035 else if (!CONSP (elt))
23036 goto invalid;
23037 elt = XCAR (elt);
23038 goto tail_recurse;
23040 else if (INTEGERP (car))
23042 register int lim = XINT (car);
23043 elt = XCDR (elt);
23044 if (lim < 0)
23046 /* Negative int means reduce maximum width. */
23047 if (precision <= 0)
23048 precision = -lim;
23049 else
23050 precision = min (precision, -lim);
23052 else if (lim > 0)
23054 /* Padding specified. Don't let it be more than
23055 current maximum. */
23056 if (precision > 0)
23057 lim = min (precision, lim);
23059 /* If that's more padding than already wanted, queue it.
23060 But don't reduce padding already specified even if
23061 that is beyond the current truncation point. */
23062 field_width = max (lim, field_width);
23064 goto tail_recurse;
23066 else if (STRINGP (car) || CONSP (car))
23068 Lisp_Object halftail = elt;
23069 int len = 0;
23071 while (CONSP (elt)
23072 && (precision <= 0 || n < precision))
23074 n += display_mode_element (it, depth,
23075 /* Do padding only after the last
23076 element in the list. */
23077 (! CONSP (XCDR (elt))
23078 ? field_width - n
23079 : 0),
23080 precision - n, XCAR (elt),
23081 props, risky);
23082 elt = XCDR (elt);
23083 len++;
23084 if ((len & 1) == 0)
23085 halftail = XCDR (halftail);
23086 /* Check for cycle. */
23087 if (EQ (halftail, elt))
23088 break;
23092 break;
23094 default:
23095 invalid:
23096 elt = build_string ("*invalid*");
23097 goto tail_recurse;
23100 /* Pad to FIELD_WIDTH. */
23101 if (field_width > 0 && n < field_width)
23103 switch (mode_line_target)
23105 case MODE_LINE_NOPROP:
23106 case MODE_LINE_TITLE:
23107 n += store_mode_line_noprop ("", field_width - n, 0);
23108 break;
23109 case MODE_LINE_STRING:
23110 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23111 Qnil);
23112 break;
23113 case MODE_LINE_DISPLAY:
23114 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23115 0, 0, 0);
23116 break;
23120 return n;
23123 /* Store a mode-line string element in mode_line_string_list.
23125 If STRING is non-null, display that C string. Otherwise, the Lisp
23126 string LISP_STRING is displayed.
23128 FIELD_WIDTH is the minimum number of output glyphs to produce.
23129 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23130 with spaces. FIELD_WIDTH <= 0 means don't pad.
23132 PRECISION is the maximum number of characters to output from
23133 STRING. PRECISION <= 0 means don't truncate the string.
23135 If COPY_STRING, make a copy of LISP_STRING before adding
23136 properties to the string.
23138 PROPS are the properties to add to the string.
23139 The mode_line_string_face face property is always added to the string.
23142 static int
23143 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23144 bool copy_string,
23145 int field_width, int precision, Lisp_Object props)
23147 ptrdiff_t len;
23148 int n = 0;
23150 if (string != NULL)
23152 len = strlen (string);
23153 if (precision > 0 && len > precision)
23154 len = precision;
23155 lisp_string = make_string (string, len);
23156 if (NILP (props))
23157 props = mode_line_string_face_prop;
23158 else if (!NILP (mode_line_string_face))
23160 Lisp_Object face = Fplist_get (props, Qface);
23161 props = Fcopy_sequence (props);
23162 if (NILP (face))
23163 face = mode_line_string_face;
23164 else
23165 face = list2 (face, mode_line_string_face);
23166 props = Fplist_put (props, Qface, face);
23168 Fadd_text_properties (make_number (0), make_number (len),
23169 props, lisp_string);
23171 else
23173 len = XFASTINT (Flength (lisp_string));
23174 if (precision > 0 && len > precision)
23176 len = precision;
23177 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23178 precision = -1;
23180 if (!NILP (mode_line_string_face))
23182 Lisp_Object face;
23183 if (NILP (props))
23184 props = Ftext_properties_at (make_number (0), lisp_string);
23185 face = Fplist_get (props, Qface);
23186 if (NILP (face))
23187 face = mode_line_string_face;
23188 else
23189 face = list2 (face, mode_line_string_face);
23190 props = list2 (Qface, face);
23191 if (copy_string)
23192 lisp_string = Fcopy_sequence (lisp_string);
23194 if (!NILP (props))
23195 Fadd_text_properties (make_number (0), make_number (len),
23196 props, lisp_string);
23199 if (len > 0)
23201 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23202 n += len;
23205 if (field_width > len)
23207 field_width -= len;
23208 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23209 if (!NILP (props))
23210 Fadd_text_properties (make_number (0), make_number (field_width),
23211 props, lisp_string);
23212 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23213 n += field_width;
23216 return n;
23220 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23221 1, 4, 0,
23222 doc: /* Format a string out of a mode line format specification.
23223 First arg FORMAT specifies the mode line format (see `mode-line-format'
23224 for details) to use.
23226 By default, the format is evaluated for the currently selected window.
23228 Optional second arg FACE specifies the face property to put on all
23229 characters for which no face is specified. The value nil means the
23230 default face. The value t means whatever face the window's mode line
23231 currently uses (either `mode-line' or `mode-line-inactive',
23232 depending on whether the window is the selected window or not).
23233 An integer value means the value string has no text
23234 properties.
23236 Optional third and fourth args WINDOW and BUFFER specify the window
23237 and buffer to use as the context for the formatting (defaults
23238 are the selected window and the WINDOW's buffer). */)
23239 (Lisp_Object format, Lisp_Object face,
23240 Lisp_Object window, Lisp_Object buffer)
23242 struct it it;
23243 int len;
23244 struct window *w;
23245 struct buffer *old_buffer = NULL;
23246 int face_id;
23247 bool no_props = INTEGERP (face);
23248 ptrdiff_t count = SPECPDL_INDEX ();
23249 Lisp_Object str;
23250 int string_start = 0;
23252 w = decode_any_window (window);
23253 XSETWINDOW (window, w);
23255 if (NILP (buffer))
23256 buffer = w->contents;
23257 CHECK_BUFFER (buffer);
23259 /* Make formatting the modeline a non-op when noninteractive, otherwise
23260 there will be problems later caused by a partially initialized frame. */
23261 if (NILP (format) || noninteractive)
23262 return empty_unibyte_string;
23264 if (no_props)
23265 face = Qnil;
23267 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23268 : EQ (face, Qt) ? (EQ (window, selected_window)
23269 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23270 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23271 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23272 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23273 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23274 : DEFAULT_FACE_ID;
23276 old_buffer = current_buffer;
23278 /* Save things including mode_line_proptrans_alist,
23279 and set that to nil so that we don't alter the outer value. */
23280 record_unwind_protect (unwind_format_mode_line,
23281 format_mode_line_unwind_data
23282 (XFRAME (WINDOW_FRAME (w)),
23283 old_buffer, selected_window, true));
23284 mode_line_proptrans_alist = Qnil;
23286 Fselect_window (window, Qt);
23287 set_buffer_internal_1 (XBUFFER (buffer));
23289 init_iterator (&it, w, -1, -1, NULL, face_id);
23291 if (no_props)
23293 mode_line_target = MODE_LINE_NOPROP;
23294 mode_line_string_face_prop = Qnil;
23295 mode_line_string_list = Qnil;
23296 string_start = MODE_LINE_NOPROP_LEN (0);
23298 else
23300 mode_line_target = MODE_LINE_STRING;
23301 mode_line_string_list = Qnil;
23302 mode_line_string_face = face;
23303 mode_line_string_face_prop
23304 = NILP (face) ? Qnil : list2 (Qface, face);
23307 push_kboard (FRAME_KBOARD (it.f));
23308 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23309 pop_kboard ();
23311 if (no_props)
23313 len = MODE_LINE_NOPROP_LEN (string_start);
23314 str = make_string (mode_line_noprop_buf + string_start, len);
23316 else
23318 mode_line_string_list = Fnreverse (mode_line_string_list);
23319 str = Fmapconcat (Qidentity, mode_line_string_list,
23320 empty_unibyte_string);
23323 unbind_to (count, Qnil);
23324 return str;
23327 /* Write a null-terminated, right justified decimal representation of
23328 the positive integer D to BUF using a minimal field width WIDTH. */
23330 static void
23331 pint2str (register char *buf, register int width, register ptrdiff_t d)
23333 register char *p = buf;
23335 if (d <= 0)
23336 *p++ = '0';
23337 else
23339 while (d > 0)
23341 *p++ = d % 10 + '0';
23342 d /= 10;
23346 for (width -= (int) (p - buf); width > 0; --width)
23347 *p++ = ' ';
23348 *p-- = '\0';
23349 while (p > buf)
23351 d = *buf;
23352 *buf++ = *p;
23353 *p-- = d;
23357 /* Write a null-terminated, right justified decimal and "human
23358 readable" representation of the nonnegative integer D to BUF using
23359 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23361 static const char power_letter[] =
23363 0, /* no letter */
23364 'k', /* kilo */
23365 'M', /* mega */
23366 'G', /* giga */
23367 'T', /* tera */
23368 'P', /* peta */
23369 'E', /* exa */
23370 'Z', /* zetta */
23371 'Y' /* yotta */
23374 static void
23375 pint2hrstr (char *buf, int width, ptrdiff_t d)
23377 /* We aim to represent the nonnegative integer D as
23378 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23379 ptrdiff_t quotient = d;
23380 int remainder = 0;
23381 /* -1 means: do not use TENTHS. */
23382 int tenths = -1;
23383 int exponent = 0;
23385 /* Length of QUOTIENT.TENTHS as a string. */
23386 int length;
23388 char * psuffix;
23389 char * p;
23391 if (quotient >= 1000)
23393 /* Scale to the appropriate EXPONENT. */
23396 remainder = quotient % 1000;
23397 quotient /= 1000;
23398 exponent++;
23400 while (quotient >= 1000);
23402 /* Round to nearest and decide whether to use TENTHS or not. */
23403 if (quotient <= 9)
23405 tenths = remainder / 100;
23406 if (remainder % 100 >= 50)
23408 if (tenths < 9)
23409 tenths++;
23410 else
23412 quotient++;
23413 if (quotient == 10)
23414 tenths = -1;
23415 else
23416 tenths = 0;
23420 else
23421 if (remainder >= 500)
23423 if (quotient < 999)
23424 quotient++;
23425 else
23427 quotient = 1;
23428 exponent++;
23429 tenths = 0;
23434 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23435 if (tenths == -1 && quotient <= 99)
23436 if (quotient <= 9)
23437 length = 1;
23438 else
23439 length = 2;
23440 else
23441 length = 3;
23442 p = psuffix = buf + max (width, length);
23444 /* Print EXPONENT. */
23445 *psuffix++ = power_letter[exponent];
23446 *psuffix = '\0';
23448 /* Print TENTHS. */
23449 if (tenths >= 0)
23451 *--p = '0' + tenths;
23452 *--p = '.';
23455 /* Print QUOTIENT. */
23458 int digit = quotient % 10;
23459 *--p = '0' + digit;
23461 while ((quotient /= 10) != 0);
23463 /* Print leading spaces. */
23464 while (buf < p)
23465 *--p = ' ';
23468 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23469 If EOL_FLAG, set also a mnemonic character for end-of-line
23470 type of CODING_SYSTEM. Return updated pointer into BUF. */
23472 static unsigned char invalid_eol_type[] = "(*invalid*)";
23474 static char *
23475 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23477 Lisp_Object val;
23478 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23479 const unsigned char *eol_str;
23480 int eol_str_len;
23481 /* The EOL conversion we are using. */
23482 Lisp_Object eoltype;
23484 val = CODING_SYSTEM_SPEC (coding_system);
23485 eoltype = Qnil;
23487 if (!VECTORP (val)) /* Not yet decided. */
23489 *buf++ = multibyte ? '-' : ' ';
23490 if (eol_flag)
23491 eoltype = eol_mnemonic_undecided;
23492 /* Don't mention EOL conversion if it isn't decided. */
23494 else
23496 Lisp_Object attrs;
23497 Lisp_Object eolvalue;
23499 attrs = AREF (val, 0);
23500 eolvalue = AREF (val, 2);
23502 *buf++ = multibyte
23503 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23504 : ' ';
23506 if (eol_flag)
23508 /* The EOL conversion that is normal on this system. */
23510 if (NILP (eolvalue)) /* Not yet decided. */
23511 eoltype = eol_mnemonic_undecided;
23512 else if (VECTORP (eolvalue)) /* Not yet decided. */
23513 eoltype = eol_mnemonic_undecided;
23514 else /* eolvalue is Qunix, Qdos, or Qmac. */
23515 eoltype = (EQ (eolvalue, Qunix)
23516 ? eol_mnemonic_unix
23517 : EQ (eolvalue, Qdos)
23518 ? eol_mnemonic_dos : eol_mnemonic_mac);
23522 if (eol_flag)
23524 /* Mention the EOL conversion if it is not the usual one. */
23525 if (STRINGP (eoltype))
23527 eol_str = SDATA (eoltype);
23528 eol_str_len = SBYTES (eoltype);
23530 else if (CHARACTERP (eoltype))
23532 int c = XFASTINT (eoltype);
23533 return buf + CHAR_STRING (c, (unsigned char *) buf);
23535 else
23537 eol_str = invalid_eol_type;
23538 eol_str_len = sizeof (invalid_eol_type) - 1;
23540 memcpy (buf, eol_str, eol_str_len);
23541 buf += eol_str_len;
23544 return buf;
23547 /* Return the approximate percentage N is of D (rounding upward), or 99,
23548 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23550 static int
23551 percent99 (ptrdiff_t n, ptrdiff_t d)
23553 int percent = (d - 1 + 100.0 * n) / d;
23554 return min (percent, 99);
23557 /* Return a string for the output of a mode line %-spec for window W,
23558 generated by character C. FIELD_WIDTH > 0 means pad the string
23559 returned with spaces to that value. Return a Lisp string in
23560 *STRING if the resulting string is taken from that Lisp string.
23562 Note we operate on the current buffer for most purposes. */
23564 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23566 static const char *
23567 decode_mode_spec (struct window *w, register int c, int field_width,
23568 Lisp_Object *string)
23570 Lisp_Object obj;
23571 struct frame *f = XFRAME (WINDOW_FRAME (w));
23572 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23573 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23574 produce strings from numerical values, so limit preposterously
23575 large values of FIELD_WIDTH to avoid overrunning the buffer's
23576 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23577 bytes plus the terminating null. */
23578 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23579 struct buffer *b = current_buffer;
23581 obj = Qnil;
23582 *string = Qnil;
23584 switch (c)
23586 case '*':
23587 if (!NILP (BVAR (b, read_only)))
23588 return "%";
23589 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23590 return "*";
23591 return "-";
23593 case '+':
23594 /* This differs from %* only for a modified read-only buffer. */
23595 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23596 return "*";
23597 if (!NILP (BVAR (b, read_only)))
23598 return "%";
23599 return "-";
23601 case '&':
23602 /* This differs from %* in ignoring read-only-ness. */
23603 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23604 return "*";
23605 return "-";
23607 case '%':
23608 return "%";
23610 case '[':
23612 int i;
23613 char *p;
23615 if (command_loop_level > 5)
23616 return "[[[... ";
23617 p = decode_mode_spec_buf;
23618 for (i = 0; i < command_loop_level; i++)
23619 *p++ = '[';
23620 *p = 0;
23621 return decode_mode_spec_buf;
23624 case ']':
23626 int i;
23627 char *p;
23629 if (command_loop_level > 5)
23630 return " ...]]]";
23631 p = decode_mode_spec_buf;
23632 for (i = 0; i < command_loop_level; i++)
23633 *p++ = ']';
23634 *p = 0;
23635 return decode_mode_spec_buf;
23638 case '-':
23640 register int i;
23642 /* Let lots_of_dashes be a string of infinite length. */
23643 if (mode_line_target == MODE_LINE_NOPROP
23644 || mode_line_target == MODE_LINE_STRING)
23645 return "--";
23646 if (field_width <= 0
23647 || field_width > sizeof (lots_of_dashes))
23649 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23650 decode_mode_spec_buf[i] = '-';
23651 decode_mode_spec_buf[i] = '\0';
23652 return decode_mode_spec_buf;
23654 else
23655 return lots_of_dashes;
23658 case 'b':
23659 obj = BVAR (b, name);
23660 break;
23662 case 'c':
23663 /* %c and %l are ignored in `frame-title-format'.
23664 (In redisplay_internal, the frame title is drawn _before_ the
23665 windows are updated, so the stuff which depends on actual
23666 window contents (such as %l) may fail to render properly, or
23667 even crash emacs.) */
23668 if (mode_line_target == MODE_LINE_TITLE)
23669 return "";
23670 else
23672 ptrdiff_t col = current_column ();
23673 w->column_number_displayed = col;
23674 pint2str (decode_mode_spec_buf, width, col);
23675 return decode_mode_spec_buf;
23678 case 'e':
23679 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23681 if (NILP (Vmemory_full))
23682 return "";
23683 else
23684 return "!MEM FULL! ";
23686 #else
23687 return "";
23688 #endif
23690 case 'F':
23691 /* %F displays the frame name. */
23692 if (!NILP (f->title))
23693 return SSDATA (f->title);
23694 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23695 return SSDATA (f->name);
23696 return "Emacs";
23698 case 'f':
23699 obj = BVAR (b, filename);
23700 break;
23702 case 'i':
23704 ptrdiff_t size = ZV - BEGV;
23705 pint2str (decode_mode_spec_buf, width, size);
23706 return decode_mode_spec_buf;
23709 case 'I':
23711 ptrdiff_t size = ZV - BEGV;
23712 pint2hrstr (decode_mode_spec_buf, width, size);
23713 return decode_mode_spec_buf;
23716 case 'l':
23718 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23719 ptrdiff_t topline, nlines, height;
23720 ptrdiff_t junk;
23722 /* %c and %l are ignored in `frame-title-format'. */
23723 if (mode_line_target == MODE_LINE_TITLE)
23724 return "";
23726 startpos = marker_position (w->start);
23727 startpos_byte = marker_byte_position (w->start);
23728 height = WINDOW_TOTAL_LINES (w);
23730 /* If we decided that this buffer isn't suitable for line numbers,
23731 don't forget that too fast. */
23732 if (w->base_line_pos == -1)
23733 goto no_value;
23735 /* If the buffer is very big, don't waste time. */
23736 if (INTEGERP (Vline_number_display_limit)
23737 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23739 w->base_line_pos = 0;
23740 w->base_line_number = 0;
23741 goto no_value;
23744 if (w->base_line_number > 0
23745 && w->base_line_pos > 0
23746 && w->base_line_pos <= startpos)
23748 line = w->base_line_number;
23749 linepos = w->base_line_pos;
23750 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23752 else
23754 line = 1;
23755 linepos = BUF_BEGV (b);
23756 linepos_byte = BUF_BEGV_BYTE (b);
23759 /* Count lines from base line to window start position. */
23760 nlines = display_count_lines (linepos_byte,
23761 startpos_byte,
23762 startpos, &junk);
23764 topline = nlines + line;
23766 /* Determine a new base line, if the old one is too close
23767 or too far away, or if we did not have one.
23768 "Too close" means it's plausible a scroll-down would
23769 go back past it. */
23770 if (startpos == BUF_BEGV (b))
23772 w->base_line_number = topline;
23773 w->base_line_pos = BUF_BEGV (b);
23775 else if (nlines < height + 25 || nlines > height * 3 + 50
23776 || linepos == BUF_BEGV (b))
23778 ptrdiff_t limit = BUF_BEGV (b);
23779 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23780 ptrdiff_t position;
23781 ptrdiff_t distance =
23782 (height * 2 + 30) * line_number_display_limit_width;
23784 if (startpos - distance > limit)
23786 limit = startpos - distance;
23787 limit_byte = CHAR_TO_BYTE (limit);
23790 nlines = display_count_lines (startpos_byte,
23791 limit_byte,
23792 - (height * 2 + 30),
23793 &position);
23794 /* If we couldn't find the lines we wanted within
23795 line_number_display_limit_width chars per line,
23796 give up on line numbers for this window. */
23797 if (position == limit_byte && limit == startpos - distance)
23799 w->base_line_pos = -1;
23800 w->base_line_number = 0;
23801 goto no_value;
23804 w->base_line_number = topline - nlines;
23805 w->base_line_pos = BYTE_TO_CHAR (position);
23808 /* Now count lines from the start pos to point. */
23809 nlines = display_count_lines (startpos_byte,
23810 PT_BYTE, PT, &junk);
23812 /* Record that we did display the line number. */
23813 line_number_displayed = true;
23815 /* Make the string to show. */
23816 pint2str (decode_mode_spec_buf, width, topline + nlines);
23817 return decode_mode_spec_buf;
23818 no_value:
23820 char *p = decode_mode_spec_buf;
23821 int pad = width - 2;
23822 while (pad-- > 0)
23823 *p++ = ' ';
23824 *p++ = '?';
23825 *p++ = '?';
23826 *p = '\0';
23827 return decode_mode_spec_buf;
23830 break;
23832 case 'm':
23833 obj = BVAR (b, mode_name);
23834 break;
23836 case 'n':
23837 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23838 return " Narrow";
23839 break;
23841 case 'p':
23843 ptrdiff_t pos = marker_position (w->start);
23844 ptrdiff_t begv = BUF_BEGV (b);
23845 ptrdiff_t zv = BUF_ZV (b);
23847 if (w->window_end_pos <= BUF_Z (b) - zv)
23848 return pos <= begv ? "All" : "Bottom";
23849 else if (pos <= begv)
23850 return "Top";
23851 else
23853 sprintf (decode_mode_spec_buf, "%2d%%",
23854 percent99 (pos - begv, zv - begv));
23855 return decode_mode_spec_buf;
23859 /* Display percentage of size above the bottom of the screen. */
23860 case 'P':
23862 ptrdiff_t toppos = marker_position (w->start);
23863 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23864 ptrdiff_t begv = BUF_BEGV (b);
23865 ptrdiff_t zv = BUF_ZV (b);
23867 if (zv <= botpos)
23868 return toppos <= begv ? "All" : "Bottom";
23869 else
23871 sprintf (decode_mode_spec_buf,
23872 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23873 percent99 (botpos - begv, zv - begv));
23874 return decode_mode_spec_buf;
23878 case 's':
23879 /* status of process */
23880 obj = Fget_buffer_process (Fcurrent_buffer ());
23881 if (NILP (obj))
23882 return "no process";
23883 #ifndef MSDOS
23884 obj = Fsymbol_name (Fprocess_status (obj));
23885 #endif
23886 break;
23888 case '@':
23890 ptrdiff_t count = inhibit_garbage_collection ();
23891 Lisp_Object curdir = BVAR (current_buffer, directory);
23892 Lisp_Object val = Qnil;
23894 if (STRINGP (curdir))
23895 val = call1 (intern ("file-remote-p"), curdir);
23897 unbind_to (count, Qnil);
23899 if (NILP (val))
23900 return "-";
23901 else
23902 return "@";
23905 case 'z':
23906 /* coding-system (not including end-of-line format) */
23907 case 'Z':
23908 /* coding-system (including end-of-line type) */
23910 bool eol_flag = (c == 'Z');
23911 char *p = decode_mode_spec_buf;
23913 if (! FRAME_WINDOW_P (f))
23915 /* No need to mention EOL here--the terminal never needs
23916 to do EOL conversion. */
23917 p = decode_mode_spec_coding (CODING_ID_NAME
23918 (FRAME_KEYBOARD_CODING (f)->id),
23919 p, false);
23920 p = decode_mode_spec_coding (CODING_ID_NAME
23921 (FRAME_TERMINAL_CODING (f)->id),
23922 p, false);
23924 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23925 p, eol_flag);
23927 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23928 #ifdef subprocesses
23929 obj = Fget_buffer_process (Fcurrent_buffer ());
23930 if (PROCESSP (obj))
23932 p = decode_mode_spec_coding
23933 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23934 p = decode_mode_spec_coding
23935 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23937 #endif /* subprocesses */
23938 #endif /* false */
23939 *p = 0;
23940 return decode_mode_spec_buf;
23944 if (STRINGP (obj))
23946 *string = obj;
23947 return SSDATA (obj);
23949 else
23950 return "";
23954 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23955 means count lines back from START_BYTE. But don't go beyond
23956 LIMIT_BYTE. Return the number of lines thus found (always
23957 nonnegative).
23959 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23960 either the position COUNT lines after/before START_BYTE, if we
23961 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23962 COUNT lines. */
23964 static ptrdiff_t
23965 display_count_lines (ptrdiff_t start_byte,
23966 ptrdiff_t limit_byte, ptrdiff_t count,
23967 ptrdiff_t *byte_pos_ptr)
23969 register unsigned char *cursor;
23970 unsigned char *base;
23972 register ptrdiff_t ceiling;
23973 register unsigned char *ceiling_addr;
23974 ptrdiff_t orig_count = count;
23976 /* If we are not in selective display mode,
23977 check only for newlines. */
23978 bool selective_display
23979 = (!NILP (BVAR (current_buffer, selective_display))
23980 && !INTEGERP (BVAR (current_buffer, selective_display)));
23982 if (count > 0)
23984 while (start_byte < limit_byte)
23986 ceiling = BUFFER_CEILING_OF (start_byte);
23987 ceiling = min (limit_byte - 1, ceiling);
23988 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23989 base = (cursor = BYTE_POS_ADDR (start_byte));
23993 if (selective_display)
23995 while (*cursor != '\n' && *cursor != 015
23996 && ++cursor != ceiling_addr)
23997 continue;
23998 if (cursor == ceiling_addr)
23999 break;
24001 else
24003 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24004 if (! cursor)
24005 break;
24008 cursor++;
24010 if (--count == 0)
24012 start_byte += cursor - base;
24013 *byte_pos_ptr = start_byte;
24014 return orig_count;
24017 while (cursor < ceiling_addr);
24019 start_byte += ceiling_addr - base;
24022 else
24024 while (start_byte > limit_byte)
24026 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24027 ceiling = max (limit_byte, ceiling);
24028 ceiling_addr = BYTE_POS_ADDR (ceiling);
24029 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24030 while (true)
24032 if (selective_display)
24034 while (--cursor >= ceiling_addr
24035 && *cursor != '\n' && *cursor != 015)
24036 continue;
24037 if (cursor < ceiling_addr)
24038 break;
24040 else
24042 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24043 if (! cursor)
24044 break;
24047 if (++count == 0)
24049 start_byte += cursor - base + 1;
24050 *byte_pos_ptr = start_byte;
24051 /* When scanning backwards, we should
24052 not count the newline posterior to which we stop. */
24053 return - orig_count - 1;
24056 start_byte += ceiling_addr - base;
24060 *byte_pos_ptr = limit_byte;
24062 if (count < 0)
24063 return - orig_count + count;
24064 return orig_count - count;
24070 /***********************************************************************
24071 Displaying strings
24072 ***********************************************************************/
24074 /* Display a NUL-terminated string, starting with index START.
24076 If STRING is non-null, display that C string. Otherwise, the Lisp
24077 string LISP_STRING is displayed. There's a case that STRING is
24078 non-null and LISP_STRING is not nil. It means STRING is a string
24079 data of LISP_STRING. In that case, we display LISP_STRING while
24080 ignoring its text properties.
24082 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24083 FACE_STRING. Display STRING or LISP_STRING with the face at
24084 FACE_STRING_POS in FACE_STRING:
24086 Display the string in the environment given by IT, but use the
24087 standard display table, temporarily.
24089 FIELD_WIDTH is the minimum number of output glyphs to produce.
24090 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24091 with spaces. If STRING has more characters, more than FIELD_WIDTH
24092 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24094 PRECISION is the maximum number of characters to output from
24095 STRING. PRECISION < 0 means don't truncate the string.
24097 This is roughly equivalent to printf format specifiers:
24099 FIELD_WIDTH PRECISION PRINTF
24100 ----------------------------------------
24101 -1 -1 %s
24102 -1 10 %.10s
24103 10 -1 %10s
24104 20 10 %20.10s
24106 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24107 display them, and < 0 means obey the current buffer's value of
24108 enable_multibyte_characters.
24110 Value is the number of columns displayed. */
24112 static int
24113 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24114 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24115 int field_width, int precision, int max_x, int multibyte)
24117 int hpos_at_start = it->hpos;
24118 int saved_face_id = it->face_id;
24119 struct glyph_row *row = it->glyph_row;
24120 ptrdiff_t it_charpos;
24122 /* Initialize the iterator IT for iteration over STRING beginning
24123 with index START. */
24124 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24125 precision, field_width, multibyte);
24126 if (string && STRINGP (lisp_string))
24127 /* LISP_STRING is the one returned by decode_mode_spec. We should
24128 ignore its text properties. */
24129 it->stop_charpos = it->end_charpos;
24131 /* If displaying STRING, set up the face of the iterator from
24132 FACE_STRING, if that's given. */
24133 if (STRINGP (face_string))
24135 ptrdiff_t endptr;
24136 struct face *face;
24138 it->face_id
24139 = face_at_string_position (it->w, face_string, face_string_pos,
24140 0, &endptr, it->base_face_id, false);
24141 face = FACE_FROM_ID (it->f, it->face_id);
24142 it->face_box_p = face->box != FACE_NO_BOX;
24145 /* Set max_x to the maximum allowed X position. Don't let it go
24146 beyond the right edge of the window. */
24147 if (max_x <= 0)
24148 max_x = it->last_visible_x;
24149 else
24150 max_x = min (max_x, it->last_visible_x);
24152 /* Skip over display elements that are not visible. because IT->w is
24153 hscrolled. */
24154 if (it->current_x < it->first_visible_x)
24155 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24156 MOVE_TO_POS | MOVE_TO_X);
24158 row->ascent = it->max_ascent;
24159 row->height = it->max_ascent + it->max_descent;
24160 row->phys_ascent = it->max_phys_ascent;
24161 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24162 row->extra_line_spacing = it->max_extra_line_spacing;
24164 if (STRINGP (it->string))
24165 it_charpos = IT_STRING_CHARPOS (*it);
24166 else
24167 it_charpos = IT_CHARPOS (*it);
24169 /* This condition is for the case that we are called with current_x
24170 past last_visible_x. */
24171 while (it->current_x < max_x)
24173 int x_before, x, n_glyphs_before, i, nglyphs;
24175 /* Get the next display element. */
24176 if (!get_next_display_element (it))
24177 break;
24179 /* Produce glyphs. */
24180 x_before = it->current_x;
24181 n_glyphs_before = row->used[TEXT_AREA];
24182 PRODUCE_GLYPHS (it);
24184 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24185 i = 0;
24186 x = x_before;
24187 while (i < nglyphs)
24189 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24191 if (it->line_wrap != TRUNCATE
24192 && x + glyph->pixel_width > max_x)
24194 /* End of continued line or max_x reached. */
24195 if (CHAR_GLYPH_PADDING_P (*glyph))
24197 /* A wide character is unbreakable. */
24198 if (row->reversed_p)
24199 unproduce_glyphs (it, row->used[TEXT_AREA]
24200 - n_glyphs_before);
24201 row->used[TEXT_AREA] = n_glyphs_before;
24202 it->current_x = x_before;
24204 else
24206 if (row->reversed_p)
24207 unproduce_glyphs (it, row->used[TEXT_AREA]
24208 - (n_glyphs_before + i));
24209 row->used[TEXT_AREA] = n_glyphs_before + i;
24210 it->current_x = x;
24212 break;
24214 else if (x + glyph->pixel_width >= it->first_visible_x)
24216 /* Glyph is at least partially visible. */
24217 ++it->hpos;
24218 if (x < it->first_visible_x)
24219 row->x = x - it->first_visible_x;
24221 else
24223 /* Glyph is off the left margin of the display area.
24224 Should not happen. */
24225 emacs_abort ();
24228 row->ascent = max (row->ascent, it->max_ascent);
24229 row->height = max (row->height, it->max_ascent + it->max_descent);
24230 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24231 row->phys_height = max (row->phys_height,
24232 it->max_phys_ascent + it->max_phys_descent);
24233 row->extra_line_spacing = max (row->extra_line_spacing,
24234 it->max_extra_line_spacing);
24235 x += glyph->pixel_width;
24236 ++i;
24239 /* Stop if max_x reached. */
24240 if (i < nglyphs)
24241 break;
24243 /* Stop at line ends. */
24244 if (ITERATOR_AT_END_OF_LINE_P (it))
24246 it->continuation_lines_width = 0;
24247 break;
24250 set_iterator_to_next (it, true);
24251 if (STRINGP (it->string))
24252 it_charpos = IT_STRING_CHARPOS (*it);
24253 else
24254 it_charpos = IT_CHARPOS (*it);
24256 /* Stop if truncating at the right edge. */
24257 if (it->line_wrap == TRUNCATE
24258 && it->current_x >= it->last_visible_x)
24260 /* Add truncation mark, but don't do it if the line is
24261 truncated at a padding space. */
24262 if (it_charpos < it->string_nchars)
24264 if (!FRAME_WINDOW_P (it->f))
24266 int ii, n;
24268 if (it->current_x > it->last_visible_x)
24270 if (!row->reversed_p)
24272 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24273 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24274 break;
24276 else
24278 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24279 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24280 break;
24281 unproduce_glyphs (it, ii + 1);
24282 ii = row->used[TEXT_AREA] - (ii + 1);
24284 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24286 row->used[TEXT_AREA] = ii;
24287 produce_special_glyphs (it, IT_TRUNCATION);
24290 produce_special_glyphs (it, IT_TRUNCATION);
24292 row->truncated_on_right_p = true;
24294 break;
24298 /* Maybe insert a truncation at the left. */
24299 if (it->first_visible_x
24300 && it_charpos > 0)
24302 if (!FRAME_WINDOW_P (it->f)
24303 || (row->reversed_p
24304 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24305 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24306 insert_left_trunc_glyphs (it);
24307 row->truncated_on_left_p = true;
24310 it->face_id = saved_face_id;
24312 /* Value is number of columns displayed. */
24313 return it->hpos - hpos_at_start;
24318 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24319 appears as an element of LIST or as the car of an element of LIST.
24320 If PROPVAL is a list, compare each element against LIST in that
24321 way, and return 1/2 if any element of PROPVAL is found in LIST.
24322 Otherwise return 0. This function cannot quit.
24323 The return value is 2 if the text is invisible but with an ellipsis
24324 and 1 if it's invisible and without an ellipsis. */
24327 invisible_prop (Lisp_Object propval, Lisp_Object list)
24329 Lisp_Object tail, proptail;
24331 for (tail = list; CONSP (tail); tail = XCDR (tail))
24333 register Lisp_Object tem;
24334 tem = XCAR (tail);
24335 if (EQ (propval, tem))
24336 return 1;
24337 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24338 return NILP (XCDR (tem)) ? 1 : 2;
24341 if (CONSP (propval))
24343 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24345 Lisp_Object propelt;
24346 propelt = XCAR (proptail);
24347 for (tail = list; CONSP (tail); tail = XCDR (tail))
24349 register Lisp_Object tem;
24350 tem = XCAR (tail);
24351 if (EQ (propelt, tem))
24352 return 1;
24353 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24354 return NILP (XCDR (tem)) ? 1 : 2;
24359 return 0;
24362 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24363 doc: /* Non-nil if the property makes the text invisible.
24364 POS-OR-PROP can be a marker or number, in which case it is taken to be
24365 a position in the current buffer and the value of the `invisible' property
24366 is checked; or it can be some other value, which is then presumed to be the
24367 value of the `invisible' property of the text of interest.
24368 The non-nil value returned can be t for truly invisible text or something
24369 else if the text is replaced by an ellipsis. */)
24370 (Lisp_Object pos_or_prop)
24372 Lisp_Object prop
24373 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24374 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24375 : pos_or_prop);
24376 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24377 return (invis == 0 ? Qnil
24378 : invis == 1 ? Qt
24379 : make_number (invis));
24382 /* Calculate a width or height in pixels from a specification using
24383 the following elements:
24385 SPEC ::=
24386 NUM - a (fractional) multiple of the default font width/height
24387 (NUM) - specifies exactly NUM pixels
24388 UNIT - a fixed number of pixels, see below.
24389 ELEMENT - size of a display element in pixels, see below.
24390 (NUM . SPEC) - equals NUM * SPEC
24391 (+ SPEC SPEC ...) - add pixel values
24392 (- SPEC SPEC ...) - subtract pixel values
24393 (- SPEC) - negate pixel value
24395 NUM ::=
24396 INT or FLOAT - a number constant
24397 SYMBOL - use symbol's (buffer local) variable binding.
24399 UNIT ::=
24400 in - pixels per inch *)
24401 mm - pixels per 1/1000 meter *)
24402 cm - pixels per 1/100 meter *)
24403 width - width of current font in pixels.
24404 height - height of current font in pixels.
24406 *) using the ratio(s) defined in display-pixels-per-inch.
24408 ELEMENT ::=
24410 left-fringe - left fringe width in pixels
24411 right-fringe - right fringe width in pixels
24413 left-margin - left margin width in pixels
24414 right-margin - right margin width in pixels
24416 scroll-bar - scroll-bar area width in pixels
24418 Examples:
24420 Pixels corresponding to 5 inches:
24421 (5 . in)
24423 Total width of non-text areas on left side of window (if scroll-bar is on left):
24424 '(space :width (+ left-fringe left-margin scroll-bar))
24426 Align to first text column (in header line):
24427 '(space :align-to 0)
24429 Align to middle of text area minus half the width of variable `my-image'
24430 containing a loaded image:
24431 '(space :align-to (0.5 . (- text my-image)))
24433 Width of left margin minus width of 1 character in the default font:
24434 '(space :width (- left-margin 1))
24436 Width of left margin minus width of 2 characters in the current font:
24437 '(space :width (- left-margin (2 . width)))
24439 Center 1 character over left-margin (in header line):
24440 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24442 Different ways to express width of left fringe plus left margin minus one pixel:
24443 '(space :width (- (+ left-fringe left-margin) (1)))
24444 '(space :width (+ left-fringe left-margin (- (1))))
24445 '(space :width (+ left-fringe left-margin (-1)))
24449 static bool
24450 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24451 struct font *font, bool width_p, int *align_to)
24453 double pixels;
24455 # define OK_PIXELS(val) (*res = (val), true)
24456 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24458 if (NILP (prop))
24459 return OK_PIXELS (0);
24461 eassert (FRAME_LIVE_P (it->f));
24463 if (SYMBOLP (prop))
24465 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24467 char *unit = SSDATA (SYMBOL_NAME (prop));
24469 if (unit[0] == 'i' && unit[1] == 'n')
24470 pixels = 1.0;
24471 else if (unit[0] == 'm' && unit[1] == 'm')
24472 pixels = 25.4;
24473 else if (unit[0] == 'c' && unit[1] == 'm')
24474 pixels = 2.54;
24475 else
24476 pixels = 0;
24477 if (pixels > 0)
24479 double ppi = (width_p ? FRAME_RES_X (it->f)
24480 : FRAME_RES_Y (it->f));
24482 if (ppi > 0)
24483 return OK_PIXELS (ppi / pixels);
24484 return false;
24488 #ifdef HAVE_WINDOW_SYSTEM
24489 if (EQ (prop, Qheight))
24490 return OK_PIXELS (font
24491 ? normal_char_height (font, -1)
24492 : FRAME_LINE_HEIGHT (it->f));
24493 if (EQ (prop, Qwidth))
24494 return OK_PIXELS (font
24495 ? FONT_WIDTH (font)
24496 : FRAME_COLUMN_WIDTH (it->f));
24497 #else
24498 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24499 return OK_PIXELS (1);
24500 #endif
24502 if (EQ (prop, Qtext))
24503 return OK_PIXELS (width_p
24504 ? window_box_width (it->w, TEXT_AREA)
24505 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24507 if (align_to && *align_to < 0)
24509 *res = 0;
24510 if (EQ (prop, Qleft))
24511 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24512 if (EQ (prop, Qright))
24513 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24514 if (EQ (prop, Qcenter))
24515 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24516 + window_box_width (it->w, TEXT_AREA) / 2);
24517 if (EQ (prop, Qleft_fringe))
24518 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24519 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24520 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24521 if (EQ (prop, Qright_fringe))
24522 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24523 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24524 : window_box_right_offset (it->w, TEXT_AREA));
24525 if (EQ (prop, Qleft_margin))
24526 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24527 if (EQ (prop, Qright_margin))
24528 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24529 if (EQ (prop, Qscroll_bar))
24530 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24532 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24533 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24534 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24535 : 0)));
24537 else
24539 if (EQ (prop, Qleft_fringe))
24540 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24541 if (EQ (prop, Qright_fringe))
24542 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24543 if (EQ (prop, Qleft_margin))
24544 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24545 if (EQ (prop, Qright_margin))
24546 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24547 if (EQ (prop, Qscroll_bar))
24548 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24551 prop = buffer_local_value (prop, it->w->contents);
24552 if (EQ (prop, Qunbound))
24553 prop = Qnil;
24556 if (NUMBERP (prop))
24558 int base_unit = (width_p
24559 ? FRAME_COLUMN_WIDTH (it->f)
24560 : FRAME_LINE_HEIGHT (it->f));
24561 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24564 if (CONSP (prop))
24566 Lisp_Object car = XCAR (prop);
24567 Lisp_Object cdr = XCDR (prop);
24569 if (SYMBOLP (car))
24571 #ifdef HAVE_WINDOW_SYSTEM
24572 if (FRAME_WINDOW_P (it->f)
24573 && valid_image_p (prop))
24575 ptrdiff_t id = lookup_image (it->f, prop);
24576 struct image *img = IMAGE_FROM_ID (it->f, id);
24578 return OK_PIXELS (width_p ? img->width : img->height);
24580 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24582 /* TODO: Don't return dummy size. */
24583 return OK_PIXELS (100);
24585 #endif
24586 if (EQ (car, Qplus) || EQ (car, Qminus))
24588 bool first = true;
24589 double px;
24591 pixels = 0;
24592 while (CONSP (cdr))
24594 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24595 font, width_p, align_to))
24596 return false;
24597 if (first)
24598 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24599 else
24600 pixels += px;
24601 cdr = XCDR (cdr);
24603 if (EQ (car, Qminus))
24604 pixels = -pixels;
24605 return OK_PIXELS (pixels);
24608 car = buffer_local_value (car, it->w->contents);
24609 if (EQ (car, Qunbound))
24610 car = Qnil;
24613 if (NUMBERP (car))
24615 double fact;
24616 pixels = XFLOATINT (car);
24617 if (NILP (cdr))
24618 return OK_PIXELS (pixels);
24619 if (calc_pixel_width_or_height (&fact, it, cdr,
24620 font, width_p, align_to))
24621 return OK_PIXELS (pixels * fact);
24622 return false;
24625 return false;
24628 return false;
24631 void
24632 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24634 #ifdef HAVE_WINDOW_SYSTEM
24635 normal_char_ascent_descent (font, -1, ascent, descent);
24636 #else
24637 *ascent = 1;
24638 *descent = 0;
24639 #endif
24643 /***********************************************************************
24644 Glyph Display
24645 ***********************************************************************/
24647 #ifdef HAVE_WINDOW_SYSTEM
24649 #ifdef GLYPH_DEBUG
24651 void
24652 dump_glyph_string (struct glyph_string *s)
24654 fprintf (stderr, "glyph string\n");
24655 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24656 s->x, s->y, s->width, s->height);
24657 fprintf (stderr, " ybase = %d\n", s->ybase);
24658 fprintf (stderr, " hl = %d\n", s->hl);
24659 fprintf (stderr, " left overhang = %d, right = %d\n",
24660 s->left_overhang, s->right_overhang);
24661 fprintf (stderr, " nchars = %d\n", s->nchars);
24662 fprintf (stderr, " extends to end of line = %d\n",
24663 s->extends_to_end_of_line_p);
24664 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24665 fprintf (stderr, " bg width = %d\n", s->background_width);
24668 #endif /* GLYPH_DEBUG */
24670 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24671 of XChar2b structures for S; it can't be allocated in
24672 init_glyph_string because it must be allocated via `alloca'. W
24673 is the window on which S is drawn. ROW and AREA are the glyph row
24674 and area within the row from which S is constructed. START is the
24675 index of the first glyph structure covered by S. HL is a
24676 face-override for drawing S. */
24678 #ifdef HAVE_NTGUI
24679 #define OPTIONAL_HDC(hdc) HDC hdc,
24680 #define DECLARE_HDC(hdc) HDC hdc;
24681 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24682 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24683 #endif
24685 #ifndef OPTIONAL_HDC
24686 #define OPTIONAL_HDC(hdc)
24687 #define DECLARE_HDC(hdc)
24688 #define ALLOCATE_HDC(hdc, f)
24689 #define RELEASE_HDC(hdc, f)
24690 #endif
24692 static void
24693 init_glyph_string (struct glyph_string *s,
24694 OPTIONAL_HDC (hdc)
24695 XChar2b *char2b, struct window *w, struct glyph_row *row,
24696 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24698 memset (s, 0, sizeof *s);
24699 s->w = w;
24700 s->f = XFRAME (w->frame);
24701 #ifdef HAVE_NTGUI
24702 s->hdc = hdc;
24703 #endif
24704 s->display = FRAME_X_DISPLAY (s->f);
24705 s->char2b = char2b;
24706 s->hl = hl;
24707 s->row = row;
24708 s->area = area;
24709 s->first_glyph = row->glyphs[area] + start;
24710 s->height = row->height;
24711 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24712 s->ybase = s->y + row->ascent;
24716 /* Append the list of glyph strings with head H and tail T to the list
24717 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24719 static void
24720 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24721 struct glyph_string *h, struct glyph_string *t)
24723 if (h)
24725 if (*head)
24726 (*tail)->next = h;
24727 else
24728 *head = h;
24729 h->prev = *tail;
24730 *tail = t;
24735 /* Prepend the list of glyph strings with head H and tail T to the
24736 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24737 result. */
24739 static void
24740 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24741 struct glyph_string *h, struct glyph_string *t)
24743 if (h)
24745 if (*head)
24746 (*head)->prev = t;
24747 else
24748 *tail = t;
24749 t->next = *head;
24750 *head = h;
24755 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24756 Set *HEAD and *TAIL to the resulting list. */
24758 static void
24759 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24760 struct glyph_string *s)
24762 s->next = s->prev = NULL;
24763 append_glyph_string_lists (head, tail, s, s);
24767 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24768 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24769 make sure that X resources for the face returned are allocated.
24770 Value is a pointer to a realized face that is ready for display if
24771 DISPLAY_P. */
24773 static struct face *
24774 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24775 XChar2b *char2b, bool display_p)
24777 struct face *face = FACE_FROM_ID (f, face_id);
24778 unsigned code = 0;
24780 if (face->font)
24782 code = face->font->driver->encode_char (face->font, c);
24784 if (code == FONT_INVALID_CODE)
24785 code = 0;
24787 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24789 /* Make sure X resources of the face are allocated. */
24790 #ifdef HAVE_X_WINDOWS
24791 if (display_p)
24792 #endif
24794 eassert (face != NULL);
24795 prepare_face_for_display (f, face);
24798 return face;
24802 /* Get face and two-byte form of character glyph GLYPH on frame F.
24803 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24804 a pointer to a realized face that is ready for display. */
24806 static struct face *
24807 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24808 XChar2b *char2b)
24810 struct face *face;
24811 unsigned code = 0;
24813 eassert (glyph->type == CHAR_GLYPH);
24814 face = FACE_FROM_ID (f, glyph->face_id);
24816 /* Make sure X resources of the face are allocated. */
24817 prepare_face_for_display (f, face);
24819 if (face->font)
24821 if (CHAR_BYTE8_P (glyph->u.ch))
24822 code = CHAR_TO_BYTE8 (glyph->u.ch);
24823 else
24824 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24826 if (code == FONT_INVALID_CODE)
24827 code = 0;
24830 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24831 return face;
24835 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24836 Return true iff FONT has a glyph for C. */
24838 static bool
24839 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24841 unsigned code;
24843 if (CHAR_BYTE8_P (c))
24844 code = CHAR_TO_BYTE8 (c);
24845 else
24846 code = font->driver->encode_char (font, c);
24848 if (code == FONT_INVALID_CODE)
24849 return false;
24850 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24851 return true;
24855 /* Fill glyph string S with composition components specified by S->cmp.
24857 BASE_FACE is the base face of the composition.
24858 S->cmp_from is the index of the first component for S.
24860 OVERLAPS non-zero means S should draw the foreground only, and use
24861 its physical height for clipping. See also draw_glyphs.
24863 Value is the index of a component not in S. */
24865 static int
24866 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24867 int overlaps)
24869 int i;
24870 /* For all glyphs of this composition, starting at the offset
24871 S->cmp_from, until we reach the end of the definition or encounter a
24872 glyph that requires the different face, add it to S. */
24873 struct face *face;
24875 eassert (s);
24877 s->for_overlaps = overlaps;
24878 s->face = NULL;
24879 s->font = NULL;
24880 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24882 int c = COMPOSITION_GLYPH (s->cmp, i);
24884 /* TAB in a composition means display glyphs with padding space
24885 on the left or right. */
24886 if (c != '\t')
24888 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24889 -1, Qnil);
24891 face = get_char_face_and_encoding (s->f, c, face_id,
24892 s->char2b + i, true);
24893 if (face)
24895 if (! s->face)
24897 s->face = face;
24898 s->font = s->face->font;
24900 else if (s->face != face)
24901 break;
24904 ++s->nchars;
24906 s->cmp_to = i;
24908 if (s->face == NULL)
24910 s->face = base_face->ascii_face;
24911 s->font = s->face->font;
24914 /* All glyph strings for the same composition has the same width,
24915 i.e. the width set for the first component of the composition. */
24916 s->width = s->first_glyph->pixel_width;
24918 /* If the specified font could not be loaded, use the frame's
24919 default font, but record the fact that we couldn't load it in
24920 the glyph string so that we can draw rectangles for the
24921 characters of the glyph string. */
24922 if (s->font == NULL)
24924 s->font_not_found_p = true;
24925 s->font = FRAME_FONT (s->f);
24928 /* Adjust base line for subscript/superscript text. */
24929 s->ybase += s->first_glyph->voffset;
24931 return s->cmp_to;
24934 static int
24935 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24936 int start, int end, int overlaps)
24938 struct glyph *glyph, *last;
24939 Lisp_Object lgstring;
24940 int i;
24942 s->for_overlaps = overlaps;
24943 glyph = s->row->glyphs[s->area] + start;
24944 last = s->row->glyphs[s->area] + end;
24945 s->cmp_id = glyph->u.cmp.id;
24946 s->cmp_from = glyph->slice.cmp.from;
24947 s->cmp_to = glyph->slice.cmp.to + 1;
24948 s->face = FACE_FROM_ID (s->f, face_id);
24949 lgstring = composition_gstring_from_id (s->cmp_id);
24950 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24951 glyph++;
24952 while (glyph < last
24953 && glyph->u.cmp.automatic
24954 && glyph->u.cmp.id == s->cmp_id
24955 && s->cmp_to == glyph->slice.cmp.from)
24956 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24958 for (i = s->cmp_from; i < s->cmp_to; i++)
24960 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24961 unsigned code = LGLYPH_CODE (lglyph);
24963 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24965 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24966 return glyph - s->row->glyphs[s->area];
24970 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24971 See the comment of fill_glyph_string for arguments.
24972 Value is the index of the first glyph not in S. */
24975 static int
24976 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24977 int start, int end, int overlaps)
24979 struct glyph *glyph, *last;
24980 int voffset;
24982 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24983 s->for_overlaps = overlaps;
24984 glyph = s->row->glyphs[s->area] + start;
24985 last = s->row->glyphs[s->area] + end;
24986 voffset = glyph->voffset;
24987 s->face = FACE_FROM_ID (s->f, face_id);
24988 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24989 s->nchars = 1;
24990 s->width = glyph->pixel_width;
24991 glyph++;
24992 while (glyph < last
24993 && glyph->type == GLYPHLESS_GLYPH
24994 && glyph->voffset == voffset
24995 && glyph->face_id == face_id)
24997 s->nchars++;
24998 s->width += glyph->pixel_width;
24999 glyph++;
25001 s->ybase += voffset;
25002 return glyph - s->row->glyphs[s->area];
25006 /* Fill glyph string S from a sequence of character glyphs.
25008 FACE_ID is the face id of the string. START is the index of the
25009 first glyph to consider, END is the index of the last + 1.
25010 OVERLAPS non-zero means S should draw the foreground only, and use
25011 its physical height for clipping. See also draw_glyphs.
25013 Value is the index of the first glyph not in S. */
25015 static int
25016 fill_glyph_string (struct glyph_string *s, int face_id,
25017 int start, int end, int overlaps)
25019 struct glyph *glyph, *last;
25020 int voffset;
25021 bool glyph_not_available_p;
25023 eassert (s->f == XFRAME (s->w->frame));
25024 eassert (s->nchars == 0);
25025 eassert (start >= 0 && end > start);
25027 s->for_overlaps = overlaps;
25028 glyph = s->row->glyphs[s->area] + start;
25029 last = s->row->glyphs[s->area] + end;
25030 voffset = glyph->voffset;
25031 s->padding_p = glyph->padding_p;
25032 glyph_not_available_p = glyph->glyph_not_available_p;
25034 while (glyph < last
25035 && glyph->type == CHAR_GLYPH
25036 && glyph->voffset == voffset
25037 /* Same face id implies same font, nowadays. */
25038 && glyph->face_id == face_id
25039 && glyph->glyph_not_available_p == glyph_not_available_p)
25041 s->face = get_glyph_face_and_encoding (s->f, glyph,
25042 s->char2b + s->nchars);
25043 ++s->nchars;
25044 eassert (s->nchars <= end - start);
25045 s->width += glyph->pixel_width;
25046 if (glyph++->padding_p != s->padding_p)
25047 break;
25050 s->font = s->face->font;
25052 /* If the specified font could not be loaded, use the frame's font,
25053 but record the fact that we couldn't load it in
25054 S->font_not_found_p so that we can draw rectangles for the
25055 characters of the glyph string. */
25056 if (s->font == NULL || glyph_not_available_p)
25058 s->font_not_found_p = true;
25059 s->font = FRAME_FONT (s->f);
25062 /* Adjust base line for subscript/superscript text. */
25063 s->ybase += voffset;
25065 eassert (s->face && s->face->gc);
25066 return glyph - s->row->glyphs[s->area];
25070 /* Fill glyph string S from image glyph S->first_glyph. */
25072 static void
25073 fill_image_glyph_string (struct glyph_string *s)
25075 eassert (s->first_glyph->type == IMAGE_GLYPH);
25076 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25077 eassert (s->img);
25078 s->slice = s->first_glyph->slice.img;
25079 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25080 s->font = s->face->font;
25081 s->width = s->first_glyph->pixel_width;
25083 /* Adjust base line for subscript/superscript text. */
25084 s->ybase += s->first_glyph->voffset;
25088 #ifdef HAVE_XWIDGETS
25089 static void
25090 fill_xwidget_glyph_string (struct glyph_string *s)
25092 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25093 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25094 s->font = s->face->font;
25095 s->width = s->first_glyph->pixel_width;
25096 s->ybase += s->first_glyph->voffset;
25097 s->xwidget = s->first_glyph->u.xwidget;
25099 #endif
25100 /* Fill glyph string S from a sequence of stretch glyphs.
25102 START is the index of the first glyph to consider,
25103 END is the index of the last + 1.
25105 Value is the index of the first glyph not in S. */
25107 static int
25108 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25110 struct glyph *glyph, *last;
25111 int voffset, face_id;
25113 eassert (s->first_glyph->type == STRETCH_GLYPH);
25115 glyph = s->row->glyphs[s->area] + start;
25116 last = s->row->glyphs[s->area] + end;
25117 face_id = glyph->face_id;
25118 s->face = FACE_FROM_ID (s->f, face_id);
25119 s->font = s->face->font;
25120 s->width = glyph->pixel_width;
25121 s->nchars = 1;
25122 voffset = glyph->voffset;
25124 for (++glyph;
25125 (glyph < last
25126 && glyph->type == STRETCH_GLYPH
25127 && glyph->voffset == voffset
25128 && glyph->face_id == face_id);
25129 ++glyph)
25130 s->width += glyph->pixel_width;
25132 /* Adjust base line for subscript/superscript text. */
25133 s->ybase += voffset;
25135 /* The case that face->gc == 0 is handled when drawing the glyph
25136 string by calling prepare_face_for_display. */
25137 eassert (s->face);
25138 return glyph - s->row->glyphs[s->area];
25141 static struct font_metrics *
25142 get_per_char_metric (struct font *font, XChar2b *char2b)
25144 static struct font_metrics metrics;
25145 unsigned code;
25147 if (! font)
25148 return NULL;
25149 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25150 if (code == FONT_INVALID_CODE)
25151 return NULL;
25152 font->driver->text_extents (font, &code, 1, &metrics);
25153 return &metrics;
25156 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25157 for FONT. Values are taken from font-global ones, except for fonts
25158 that claim preposterously large values, but whose glyphs actually
25159 have reasonable dimensions. C is the character to use for metrics
25160 if the font-global values are too large; if C is negative, the
25161 function selects a default character. */
25162 static void
25163 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25165 *ascent = FONT_BASE (font);
25166 *descent = FONT_DESCENT (font);
25168 if (FONT_TOO_HIGH (font))
25170 XChar2b char2b;
25172 /* Get metrics of C, defaulting to a reasonably sized ASCII
25173 character. */
25174 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25176 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25178 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25180 /* We add 1 pixel to character dimensions as heuristics
25181 that produces nicer display, e.g. when the face has
25182 the box attribute. */
25183 *ascent = pcm->ascent + 1;
25184 *descent = pcm->descent + 1;
25190 /* A subroutine that computes a reasonable "normal character height"
25191 for fonts that claim preposterously large vertical dimensions, but
25192 whose glyphs are actually reasonably sized. C is the character
25193 whose metrics to use for those fonts, or -1 for default
25194 character. */
25195 static int
25196 normal_char_height (struct font *font, int c)
25198 int ascent, descent;
25200 normal_char_ascent_descent (font, c, &ascent, &descent);
25202 return ascent + descent;
25205 /* EXPORT for RIF:
25206 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25207 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25208 assumed to be zero. */
25210 void
25211 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25213 *left = *right = 0;
25215 if (glyph->type == CHAR_GLYPH)
25217 XChar2b char2b;
25218 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25219 if (face->font)
25221 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25222 if (pcm)
25224 if (pcm->rbearing > pcm->width)
25225 *right = pcm->rbearing - pcm->width;
25226 if (pcm->lbearing < 0)
25227 *left = -pcm->lbearing;
25231 else if (glyph->type == COMPOSITE_GLYPH)
25233 if (! glyph->u.cmp.automatic)
25235 struct composition *cmp = composition_table[glyph->u.cmp.id];
25237 if (cmp->rbearing > cmp->pixel_width)
25238 *right = cmp->rbearing - cmp->pixel_width;
25239 if (cmp->lbearing < 0)
25240 *left = - cmp->lbearing;
25242 else
25244 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25245 struct font_metrics metrics;
25247 composition_gstring_width (gstring, glyph->slice.cmp.from,
25248 glyph->slice.cmp.to + 1, &metrics);
25249 if (metrics.rbearing > metrics.width)
25250 *right = metrics.rbearing - metrics.width;
25251 if (metrics.lbearing < 0)
25252 *left = - metrics.lbearing;
25258 /* Return the index of the first glyph preceding glyph string S that
25259 is overwritten by S because of S's left overhang. Value is -1
25260 if no glyphs are overwritten. */
25262 static int
25263 left_overwritten (struct glyph_string *s)
25265 int k;
25267 if (s->left_overhang)
25269 int x = 0, i;
25270 struct glyph *glyphs = s->row->glyphs[s->area];
25271 int first = s->first_glyph - glyphs;
25273 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25274 x -= glyphs[i].pixel_width;
25276 k = i + 1;
25278 else
25279 k = -1;
25281 return k;
25285 /* Return the index of the first glyph preceding glyph string S that
25286 is overwriting S because of its right overhang. Value is -1 if no
25287 glyph in front of S overwrites S. */
25289 static int
25290 left_overwriting (struct glyph_string *s)
25292 int i, k, x;
25293 struct glyph *glyphs = s->row->glyphs[s->area];
25294 int first = s->first_glyph - glyphs;
25296 k = -1;
25297 x = 0;
25298 for (i = first - 1; i >= 0; --i)
25300 int left, right;
25301 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25302 if (x + right > 0)
25303 k = i;
25304 x -= glyphs[i].pixel_width;
25307 return k;
25311 /* Return the index of the last glyph following glyph string S that is
25312 overwritten by S because of S's right overhang. Value is -1 if
25313 no such glyph is found. */
25315 static int
25316 right_overwritten (struct glyph_string *s)
25318 int k = -1;
25320 if (s->right_overhang)
25322 int x = 0, i;
25323 struct glyph *glyphs = s->row->glyphs[s->area];
25324 int first = (s->first_glyph - glyphs
25325 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25326 int end = s->row->used[s->area];
25328 for (i = first; i < end && s->right_overhang > x; ++i)
25329 x += glyphs[i].pixel_width;
25331 k = i;
25334 return k;
25338 /* Return the index of the last glyph following glyph string S that
25339 overwrites S because of its left overhang. Value is negative
25340 if no such glyph is found. */
25342 static int
25343 right_overwriting (struct glyph_string *s)
25345 int i, k, x;
25346 int end = s->row->used[s->area];
25347 struct glyph *glyphs = s->row->glyphs[s->area];
25348 int first = (s->first_glyph - glyphs
25349 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25351 k = -1;
25352 x = 0;
25353 for (i = first; i < end; ++i)
25355 int left, right;
25356 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25357 if (x - left < 0)
25358 k = i;
25359 x += glyphs[i].pixel_width;
25362 return k;
25366 /* Set background width of glyph string S. START is the index of the
25367 first glyph following S. LAST_X is the right-most x-position + 1
25368 in the drawing area. */
25370 static void
25371 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25373 /* If the face of this glyph string has to be drawn to the end of
25374 the drawing area, set S->extends_to_end_of_line_p. */
25376 if (start == s->row->used[s->area]
25377 && ((s->row->fill_line_p
25378 && (s->hl == DRAW_NORMAL_TEXT
25379 || s->hl == DRAW_IMAGE_RAISED
25380 || s->hl == DRAW_IMAGE_SUNKEN))
25381 || s->hl == DRAW_MOUSE_FACE))
25382 s->extends_to_end_of_line_p = true;
25384 /* If S extends its face to the end of the line, set its
25385 background_width to the distance to the right edge of the drawing
25386 area. */
25387 if (s->extends_to_end_of_line_p)
25388 s->background_width = last_x - s->x + 1;
25389 else
25390 s->background_width = s->width;
25394 /* Compute overhangs and x-positions for glyph string S and its
25395 predecessors, or successors. X is the starting x-position for S.
25396 BACKWARD_P means process predecessors. */
25398 static void
25399 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25401 if (backward_p)
25403 while (s)
25405 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25406 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25407 x -= s->width;
25408 s->x = x;
25409 s = s->prev;
25412 else
25414 while (s)
25416 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25417 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25418 s->x = x;
25419 x += s->width;
25420 s = s->next;
25427 /* The following macros are only called from draw_glyphs below.
25428 They reference the following parameters of that function directly:
25429 `w', `row', `area', and `overlap_p'
25430 as well as the following local variables:
25431 `s', `f', and `hdc' (in W32) */
25433 #ifdef HAVE_NTGUI
25434 /* On W32, silently add local `hdc' variable to argument list of
25435 init_glyph_string. */
25436 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25437 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25438 #else
25439 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25440 init_glyph_string (s, char2b, w, row, area, start, hl)
25441 #endif
25443 /* Add a glyph string for a stretch glyph to the list of strings
25444 between HEAD and TAIL. START is the index of the stretch glyph in
25445 row area AREA of glyph row ROW. END is the index of the last glyph
25446 in that glyph row area. X is the current output position assigned
25447 to the new glyph string constructed. HL overrides that face of the
25448 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25449 is the right-most x-position of the drawing area. */
25451 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25452 and below -- keep them on one line. */
25453 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25454 do \
25456 s = alloca (sizeof *s); \
25457 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25458 START = fill_stretch_glyph_string (s, START, END); \
25459 append_glyph_string (&HEAD, &TAIL, s); \
25460 s->x = (X); \
25462 while (false)
25465 /* Add a glyph string for an image glyph to the list of strings
25466 between HEAD and TAIL. START is the index of the image glyph in
25467 row area AREA of glyph row ROW. END is the index of the last glyph
25468 in that glyph row area. X is the current output position assigned
25469 to the new glyph string constructed. HL overrides that face of the
25470 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25471 is the right-most x-position of the drawing area. */
25473 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25474 do \
25476 s = alloca (sizeof *s); \
25477 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25478 fill_image_glyph_string (s); \
25479 append_glyph_string (&HEAD, &TAIL, s); \
25480 ++START; \
25481 s->x = (X); \
25483 while (false)
25485 #ifndef HAVE_XWIDGETS
25486 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25487 eassume (false)
25488 #else
25489 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25490 do \
25492 s = alloca (sizeof *s); \
25493 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25494 fill_xwidget_glyph_string (s); \
25495 append_glyph_string (&(HEAD), &(TAIL), s); \
25496 ++(START); \
25497 s->x = (X); \
25499 while (false)
25500 #endif
25502 /* Add a glyph string for a sequence of character glyphs to the list
25503 of strings between HEAD and TAIL. START is the index of the first
25504 glyph in row area AREA of glyph row ROW that is part of the new
25505 glyph string. END is the index of the last glyph in that glyph row
25506 area. X is the current output position assigned to the new glyph
25507 string constructed. HL overrides that face of the glyph; e.g. it
25508 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25509 right-most x-position of the drawing area. */
25511 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25512 do \
25514 int face_id; \
25515 XChar2b *char2b; \
25517 face_id = (row)->glyphs[area][START].face_id; \
25519 s = alloca (sizeof *s); \
25520 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25521 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25522 append_glyph_string (&HEAD, &TAIL, s); \
25523 s->x = (X); \
25524 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25526 while (false)
25529 /* Add a glyph string for a composite sequence to the list of strings
25530 between HEAD and TAIL. START is the index of the first glyph in
25531 row area AREA of glyph row ROW that is part of the new glyph
25532 string. END is the index of the last glyph in that glyph row area.
25533 X is the current output position assigned to the new glyph string
25534 constructed. HL overrides that face of the glyph; e.g. it is
25535 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25536 x-position of the drawing area. */
25538 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25539 do { \
25540 int face_id = (row)->glyphs[area][START].face_id; \
25541 struct face *base_face = FACE_FROM_ID (f, face_id); \
25542 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25543 struct composition *cmp = composition_table[cmp_id]; \
25544 XChar2b *char2b; \
25545 struct glyph_string *first_s = NULL; \
25546 int n; \
25548 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25550 /* Make glyph_strings for each glyph sequence that is drawable by \
25551 the same face, and append them to HEAD/TAIL. */ \
25552 for (n = 0; n < cmp->glyph_len;) \
25554 s = alloca (sizeof *s); \
25555 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25556 append_glyph_string (&(HEAD), &(TAIL), s); \
25557 s->cmp = cmp; \
25558 s->cmp_from = n; \
25559 s->x = (X); \
25560 if (n == 0) \
25561 first_s = s; \
25562 n = fill_composite_glyph_string (s, base_face, overlaps); \
25565 ++START; \
25566 s = first_s; \
25567 } while (false)
25570 /* Add a glyph string for a glyph-string sequence to the list of strings
25571 between HEAD and TAIL. */
25573 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25574 do { \
25575 int face_id; \
25576 XChar2b *char2b; \
25577 Lisp_Object gstring; \
25579 face_id = (row)->glyphs[area][START].face_id; \
25580 gstring = (composition_gstring_from_id \
25581 ((row)->glyphs[area][START].u.cmp.id)); \
25582 s = alloca (sizeof *s); \
25583 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25584 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25585 append_glyph_string (&(HEAD), &(TAIL), s); \
25586 s->x = (X); \
25587 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25588 } while (false)
25591 /* Add a glyph string for a sequence of glyphless character's glyphs
25592 to the list of strings between HEAD and TAIL. The meanings of
25593 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25595 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25596 do \
25598 int face_id; \
25600 face_id = (row)->glyphs[area][START].face_id; \
25602 s = alloca (sizeof *s); \
25603 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25604 append_glyph_string (&HEAD, &TAIL, s); \
25605 s->x = (X); \
25606 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25607 overlaps); \
25609 while (false)
25612 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25613 of AREA of glyph row ROW on window W between indices START and END.
25614 HL overrides the face for drawing glyph strings, e.g. it is
25615 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25616 x-positions of the drawing area.
25618 This is an ugly monster macro construct because we must use alloca
25619 to allocate glyph strings (because draw_glyphs can be called
25620 asynchronously). */
25622 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25623 do \
25625 HEAD = TAIL = NULL; \
25626 while (START < END) \
25628 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25629 switch (first_glyph->type) \
25631 case CHAR_GLYPH: \
25632 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25633 HL, X, LAST_X); \
25634 break; \
25636 case COMPOSITE_GLYPH: \
25637 if (first_glyph->u.cmp.automatic) \
25638 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25639 HL, X, LAST_X); \
25640 else \
25641 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25642 HL, X, LAST_X); \
25643 break; \
25645 case STRETCH_GLYPH: \
25646 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25647 HL, X, LAST_X); \
25648 break; \
25650 case IMAGE_GLYPH: \
25651 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25652 HL, X, LAST_X); \
25653 break;
25655 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25656 case XWIDGET_GLYPH: \
25657 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25658 HL, X, LAST_X); \
25659 break;
25661 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25662 case GLYPHLESS_GLYPH: \
25663 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25664 HL, X, LAST_X); \
25665 break; \
25667 default: \
25668 emacs_abort (); \
25671 if (s) \
25673 set_glyph_string_background_width (s, START, LAST_X); \
25674 (X) += s->width; \
25677 } while (false)
25680 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25681 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25682 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25683 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25686 /* Draw glyphs between START and END in AREA of ROW on window W,
25687 starting at x-position X. X is relative to AREA in W. HL is a
25688 face-override with the following meaning:
25690 DRAW_NORMAL_TEXT draw normally
25691 DRAW_CURSOR draw in cursor face
25692 DRAW_MOUSE_FACE draw in mouse face.
25693 DRAW_INVERSE_VIDEO draw in mode line face
25694 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25695 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25697 If OVERLAPS is non-zero, draw only the foreground of characters and
25698 clip to the physical height of ROW. Non-zero value also defines
25699 the overlapping part to be drawn:
25701 OVERLAPS_PRED overlap with preceding rows
25702 OVERLAPS_SUCC overlap with succeeding rows
25703 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25704 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25706 Value is the x-position reached, relative to AREA of W. */
25708 static int
25709 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25710 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25711 enum draw_glyphs_face hl, int overlaps)
25713 struct glyph_string *head, *tail;
25714 struct glyph_string *s;
25715 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25716 int i, j, x_reached, last_x, area_left = 0;
25717 struct frame *f = XFRAME (WINDOW_FRAME (w));
25718 DECLARE_HDC (hdc);
25720 ALLOCATE_HDC (hdc, f);
25722 /* Let's rather be paranoid than getting a SEGV. */
25723 end = min (end, row->used[area]);
25724 start = clip_to_bounds (0, start, end);
25726 /* Translate X to frame coordinates. Set last_x to the right
25727 end of the drawing area. */
25728 if (row->full_width_p)
25730 /* X is relative to the left edge of W, without scroll bars
25731 or fringes. */
25732 area_left = WINDOW_LEFT_EDGE_X (w);
25733 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25734 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25736 else
25738 area_left = window_box_left (w, area);
25739 last_x = area_left + window_box_width (w, area);
25741 x += area_left;
25743 /* Build a doubly-linked list of glyph_string structures between
25744 head and tail from what we have to draw. Note that the macro
25745 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25746 the reason we use a separate variable `i'. */
25747 i = start;
25748 USE_SAFE_ALLOCA;
25749 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25750 if (tail)
25751 x_reached = tail->x + tail->background_width;
25752 else
25753 x_reached = x;
25755 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25756 the row, redraw some glyphs in front or following the glyph
25757 strings built above. */
25758 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25760 struct glyph_string *h, *t;
25761 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25762 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25763 bool check_mouse_face = false;
25764 int dummy_x = 0;
25766 /* If mouse highlighting is on, we may need to draw adjacent
25767 glyphs using mouse-face highlighting. */
25768 if (area == TEXT_AREA && row->mouse_face_p
25769 && hlinfo->mouse_face_beg_row >= 0
25770 && hlinfo->mouse_face_end_row >= 0)
25772 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25774 if (row_vpos >= hlinfo->mouse_face_beg_row
25775 && row_vpos <= hlinfo->mouse_face_end_row)
25777 check_mouse_face = true;
25778 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25779 ? hlinfo->mouse_face_beg_col : 0;
25780 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25781 ? hlinfo->mouse_face_end_col
25782 : row->used[TEXT_AREA];
25786 /* Compute overhangs for all glyph strings. */
25787 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25788 for (s = head; s; s = s->next)
25789 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25791 /* Prepend glyph strings for glyphs in front of the first glyph
25792 string that are overwritten because of the first glyph
25793 string's left overhang. The background of all strings
25794 prepended must be drawn because the first glyph string
25795 draws over it. */
25796 i = left_overwritten (head);
25797 if (i >= 0)
25799 enum draw_glyphs_face overlap_hl;
25801 /* If this row contains mouse highlighting, attempt to draw
25802 the overlapped glyphs with the correct highlight. This
25803 code fails if the overlap encompasses more than one glyph
25804 and mouse-highlight spans only some of these glyphs.
25805 However, making it work perfectly involves a lot more
25806 code, and I don't know if the pathological case occurs in
25807 practice, so we'll stick to this for now. --- cyd */
25808 if (check_mouse_face
25809 && mouse_beg_col < start && mouse_end_col > i)
25810 overlap_hl = DRAW_MOUSE_FACE;
25811 else
25812 overlap_hl = DRAW_NORMAL_TEXT;
25814 if (hl != overlap_hl)
25815 clip_head = head;
25816 j = i;
25817 BUILD_GLYPH_STRINGS (j, start, h, t,
25818 overlap_hl, dummy_x, last_x);
25819 start = i;
25820 compute_overhangs_and_x (t, head->x, true);
25821 prepend_glyph_string_lists (&head, &tail, h, t);
25822 if (clip_head == NULL)
25823 clip_head = head;
25826 /* Prepend glyph strings for glyphs in front of the first glyph
25827 string that overwrite that glyph string because of their
25828 right overhang. For these strings, only the foreground must
25829 be drawn, because it draws over the glyph string at `head'.
25830 The background must not be drawn because this would overwrite
25831 right overhangs of preceding glyphs for which no glyph
25832 strings exist. */
25833 i = left_overwriting (head);
25834 if (i >= 0)
25836 enum draw_glyphs_face overlap_hl;
25838 if (check_mouse_face
25839 && mouse_beg_col < start && mouse_end_col > i)
25840 overlap_hl = DRAW_MOUSE_FACE;
25841 else
25842 overlap_hl = DRAW_NORMAL_TEXT;
25844 if (hl == overlap_hl || clip_head == NULL)
25845 clip_head = head;
25846 BUILD_GLYPH_STRINGS (i, start, h, t,
25847 overlap_hl, dummy_x, last_x);
25848 for (s = h; s; s = s->next)
25849 s->background_filled_p = true;
25850 compute_overhangs_and_x (t, head->x, true);
25851 prepend_glyph_string_lists (&head, &tail, h, t);
25854 /* Append glyphs strings for glyphs following the last glyph
25855 string tail that are overwritten by tail. The background of
25856 these strings has to be drawn because tail's foreground draws
25857 over it. */
25858 i = right_overwritten (tail);
25859 if (i >= 0)
25861 enum draw_glyphs_face overlap_hl;
25863 if (check_mouse_face
25864 && mouse_beg_col < i && mouse_end_col > end)
25865 overlap_hl = DRAW_MOUSE_FACE;
25866 else
25867 overlap_hl = DRAW_NORMAL_TEXT;
25869 if (hl != overlap_hl)
25870 clip_tail = tail;
25871 BUILD_GLYPH_STRINGS (end, i, h, t,
25872 overlap_hl, x, last_x);
25873 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25874 we don't have `end = i;' here. */
25875 compute_overhangs_and_x (h, tail->x + tail->width, false);
25876 append_glyph_string_lists (&head, &tail, h, t);
25877 if (clip_tail == NULL)
25878 clip_tail = tail;
25881 /* Append glyph strings for glyphs following the last glyph
25882 string tail that overwrite tail. The foreground of such
25883 glyphs has to be drawn because it writes into the background
25884 of tail. The background must not be drawn because it could
25885 paint over the foreground of following glyphs. */
25886 i = right_overwriting (tail);
25887 if (i >= 0)
25889 enum draw_glyphs_face overlap_hl;
25890 if (check_mouse_face
25891 && mouse_beg_col < i && mouse_end_col > end)
25892 overlap_hl = DRAW_MOUSE_FACE;
25893 else
25894 overlap_hl = DRAW_NORMAL_TEXT;
25896 if (hl == overlap_hl || clip_tail == NULL)
25897 clip_tail = tail;
25898 i++; /* We must include the Ith glyph. */
25899 BUILD_GLYPH_STRINGS (end, i, h, t,
25900 overlap_hl, x, last_x);
25901 for (s = h; s; s = s->next)
25902 s->background_filled_p = true;
25903 compute_overhangs_and_x (h, tail->x + tail->width, false);
25904 append_glyph_string_lists (&head, &tail, h, t);
25906 if (clip_head || clip_tail)
25907 for (s = head; s; s = s->next)
25909 s->clip_head = clip_head;
25910 s->clip_tail = clip_tail;
25914 /* Draw all strings. */
25915 for (s = head; s; s = s->next)
25916 FRAME_RIF (f)->draw_glyph_string (s);
25918 #ifndef HAVE_NS
25919 /* When focus a sole frame and move horizontally, this clears on_p
25920 causing a failure to erase prev cursor position. */
25921 if (area == TEXT_AREA
25922 && !row->full_width_p
25923 /* When drawing overlapping rows, only the glyph strings'
25924 foreground is drawn, which doesn't erase a cursor
25925 completely. */
25926 && !overlaps)
25928 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25929 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25930 : (tail ? tail->x + tail->background_width : x));
25931 x0 -= area_left;
25932 x1 -= area_left;
25934 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25935 row->y, MATRIX_ROW_BOTTOM_Y (row));
25937 #endif
25939 /* Value is the x-position up to which drawn, relative to AREA of W.
25940 This doesn't include parts drawn because of overhangs. */
25941 if (row->full_width_p)
25942 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25943 else
25944 x_reached -= area_left;
25946 RELEASE_HDC (hdc, f);
25948 SAFE_FREE ();
25949 return x_reached;
25952 /* Expand row matrix if too narrow. Don't expand if area
25953 is not present. */
25955 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25957 if (!it->f->fonts_changed \
25958 && (it->glyph_row->glyphs[area] \
25959 < it->glyph_row->glyphs[area + 1])) \
25961 it->w->ncols_scale_factor++; \
25962 it->f->fonts_changed = true; \
25966 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25967 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25969 static void
25970 append_glyph (struct it *it)
25972 struct glyph *glyph;
25973 enum glyph_row_area area = it->area;
25975 eassert (it->glyph_row);
25976 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25978 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25979 if (glyph < it->glyph_row->glyphs[area + 1])
25981 /* If the glyph row is reversed, we need to prepend the glyph
25982 rather than append it. */
25983 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25985 struct glyph *g;
25987 /* Make room for the additional glyph. */
25988 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25989 g[1] = *g;
25990 glyph = it->glyph_row->glyphs[area];
25992 glyph->charpos = CHARPOS (it->position);
25993 glyph->object = it->object;
25994 if (it->pixel_width > 0)
25996 eassert (it->pixel_width <= SHRT_MAX);
25997 glyph->pixel_width = it->pixel_width;
25998 glyph->padding_p = false;
26000 else
26002 /* Assure at least 1-pixel width. Otherwise, cursor can't
26003 be displayed correctly. */
26004 glyph->pixel_width = 1;
26005 glyph->padding_p = true;
26007 glyph->ascent = it->ascent;
26008 glyph->descent = it->descent;
26009 glyph->voffset = it->voffset;
26010 glyph->type = CHAR_GLYPH;
26011 glyph->avoid_cursor_p = it->avoid_cursor_p;
26012 glyph->multibyte_p = it->multibyte_p;
26013 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26015 /* In R2L rows, the left and the right box edges need to be
26016 drawn in reverse direction. */
26017 glyph->right_box_line_p = it->start_of_box_run_p;
26018 glyph->left_box_line_p = it->end_of_box_run_p;
26020 else
26022 glyph->left_box_line_p = it->start_of_box_run_p;
26023 glyph->right_box_line_p = it->end_of_box_run_p;
26025 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26026 || it->phys_descent > it->descent);
26027 glyph->glyph_not_available_p = it->glyph_not_available_p;
26028 glyph->face_id = it->face_id;
26029 glyph->u.ch = it->char_to_display;
26030 glyph->slice.img = null_glyph_slice;
26031 glyph->font_type = FONT_TYPE_UNKNOWN;
26032 if (it->bidi_p)
26034 glyph->resolved_level = it->bidi_it.resolved_level;
26035 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26036 glyph->bidi_type = it->bidi_it.type;
26038 else
26040 glyph->resolved_level = 0;
26041 glyph->bidi_type = UNKNOWN_BT;
26043 ++it->glyph_row->used[area];
26045 else
26046 IT_EXPAND_MATRIX_WIDTH (it, area);
26049 /* Store one glyph for the composition IT->cmp_it.id in
26050 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26051 non-null. */
26053 static void
26054 append_composite_glyph (struct it *it)
26056 struct glyph *glyph;
26057 enum glyph_row_area area = it->area;
26059 eassert (it->glyph_row);
26061 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26062 if (glyph < it->glyph_row->glyphs[area + 1])
26064 /* If the glyph row is reversed, we need to prepend the glyph
26065 rather than append it. */
26066 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26068 struct glyph *g;
26070 /* Make room for the new glyph. */
26071 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26072 g[1] = *g;
26073 glyph = it->glyph_row->glyphs[it->area];
26075 glyph->charpos = it->cmp_it.charpos;
26076 glyph->object = it->object;
26077 eassert (it->pixel_width <= SHRT_MAX);
26078 glyph->pixel_width = it->pixel_width;
26079 glyph->ascent = it->ascent;
26080 glyph->descent = it->descent;
26081 glyph->voffset = it->voffset;
26082 glyph->type = COMPOSITE_GLYPH;
26083 if (it->cmp_it.ch < 0)
26085 glyph->u.cmp.automatic = false;
26086 glyph->u.cmp.id = it->cmp_it.id;
26087 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26089 else
26091 glyph->u.cmp.automatic = true;
26092 glyph->u.cmp.id = it->cmp_it.id;
26093 glyph->slice.cmp.from = it->cmp_it.from;
26094 glyph->slice.cmp.to = it->cmp_it.to - 1;
26096 glyph->avoid_cursor_p = it->avoid_cursor_p;
26097 glyph->multibyte_p = it->multibyte_p;
26098 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26100 /* In R2L rows, the left and the right box edges need to be
26101 drawn in reverse direction. */
26102 glyph->right_box_line_p = it->start_of_box_run_p;
26103 glyph->left_box_line_p = it->end_of_box_run_p;
26105 else
26107 glyph->left_box_line_p = it->start_of_box_run_p;
26108 glyph->right_box_line_p = it->end_of_box_run_p;
26110 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26111 || it->phys_descent > it->descent);
26112 glyph->padding_p = false;
26113 glyph->glyph_not_available_p = false;
26114 glyph->face_id = it->face_id;
26115 glyph->font_type = FONT_TYPE_UNKNOWN;
26116 if (it->bidi_p)
26118 glyph->resolved_level = it->bidi_it.resolved_level;
26119 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26120 glyph->bidi_type = it->bidi_it.type;
26122 ++it->glyph_row->used[area];
26124 else
26125 IT_EXPAND_MATRIX_WIDTH (it, area);
26129 /* Change IT->ascent and IT->height according to the setting of
26130 IT->voffset. */
26132 static void
26133 take_vertical_position_into_account (struct it *it)
26135 if (it->voffset)
26137 if (it->voffset < 0)
26138 /* Increase the ascent so that we can display the text higher
26139 in the line. */
26140 it->ascent -= it->voffset;
26141 else
26142 /* Increase the descent so that we can display the text lower
26143 in the line. */
26144 it->descent += it->voffset;
26149 /* Produce glyphs/get display metrics for the image IT is loaded with.
26150 See the description of struct display_iterator in dispextern.h for
26151 an overview of struct display_iterator. */
26153 static void
26154 produce_image_glyph (struct it *it)
26156 struct image *img;
26157 struct face *face;
26158 int glyph_ascent, crop;
26159 struct glyph_slice slice;
26161 eassert (it->what == IT_IMAGE);
26163 face = FACE_FROM_ID (it->f, it->face_id);
26164 /* Make sure X resources of the face is loaded. */
26165 prepare_face_for_display (it->f, face);
26167 if (it->image_id < 0)
26169 /* Fringe bitmap. */
26170 it->ascent = it->phys_ascent = 0;
26171 it->descent = it->phys_descent = 0;
26172 it->pixel_width = 0;
26173 it->nglyphs = 0;
26174 return;
26177 img = IMAGE_FROM_ID (it->f, it->image_id);
26178 /* Make sure X resources of the image is loaded. */
26179 prepare_image_for_display (it->f, img);
26181 slice.x = slice.y = 0;
26182 slice.width = img->width;
26183 slice.height = img->height;
26185 if (INTEGERP (it->slice.x))
26186 slice.x = XINT (it->slice.x);
26187 else if (FLOATP (it->slice.x))
26188 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26190 if (INTEGERP (it->slice.y))
26191 slice.y = XINT (it->slice.y);
26192 else if (FLOATP (it->slice.y))
26193 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26195 if (INTEGERP (it->slice.width))
26196 slice.width = XINT (it->slice.width);
26197 else if (FLOATP (it->slice.width))
26198 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26200 if (INTEGERP (it->slice.height))
26201 slice.height = XINT (it->slice.height);
26202 else if (FLOATP (it->slice.height))
26203 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26205 if (slice.x >= img->width)
26206 slice.x = img->width;
26207 if (slice.y >= img->height)
26208 slice.y = img->height;
26209 if (slice.x + slice.width >= img->width)
26210 slice.width = img->width - slice.x;
26211 if (slice.y + slice.height > img->height)
26212 slice.height = img->height - slice.y;
26214 if (slice.width == 0 || slice.height == 0)
26215 return;
26217 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26219 it->descent = slice.height - glyph_ascent;
26220 if (slice.y == 0)
26221 it->descent += img->vmargin;
26222 if (slice.y + slice.height == img->height)
26223 it->descent += img->vmargin;
26224 it->phys_descent = it->descent;
26226 it->pixel_width = slice.width;
26227 if (slice.x == 0)
26228 it->pixel_width += img->hmargin;
26229 if (slice.x + slice.width == img->width)
26230 it->pixel_width += img->hmargin;
26232 /* It's quite possible for images to have an ascent greater than
26233 their height, so don't get confused in that case. */
26234 if (it->descent < 0)
26235 it->descent = 0;
26237 it->nglyphs = 1;
26239 if (face->box != FACE_NO_BOX)
26241 if (face->box_line_width > 0)
26243 if (slice.y == 0)
26244 it->ascent += face->box_line_width;
26245 if (slice.y + slice.height == img->height)
26246 it->descent += face->box_line_width;
26249 if (it->start_of_box_run_p && slice.x == 0)
26250 it->pixel_width += eabs (face->box_line_width);
26251 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26252 it->pixel_width += eabs (face->box_line_width);
26255 take_vertical_position_into_account (it);
26257 /* Automatically crop wide image glyphs at right edge so we can
26258 draw the cursor on same display row. */
26259 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26260 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26262 it->pixel_width -= crop;
26263 slice.width -= crop;
26266 if (it->glyph_row)
26268 struct glyph *glyph;
26269 enum glyph_row_area area = it->area;
26271 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26272 if (it->glyph_row->reversed_p)
26274 struct glyph *g;
26276 /* Make room for the new glyph. */
26277 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26278 g[1] = *g;
26279 glyph = it->glyph_row->glyphs[it->area];
26281 if (glyph < it->glyph_row->glyphs[area + 1])
26283 glyph->charpos = CHARPOS (it->position);
26284 glyph->object = it->object;
26285 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26286 glyph->ascent = glyph_ascent;
26287 glyph->descent = it->descent;
26288 glyph->voffset = it->voffset;
26289 glyph->type = IMAGE_GLYPH;
26290 glyph->avoid_cursor_p = it->avoid_cursor_p;
26291 glyph->multibyte_p = it->multibyte_p;
26292 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26294 /* In R2L rows, the left and the right box edges need to be
26295 drawn in reverse direction. */
26296 glyph->right_box_line_p = it->start_of_box_run_p;
26297 glyph->left_box_line_p = it->end_of_box_run_p;
26299 else
26301 glyph->left_box_line_p = it->start_of_box_run_p;
26302 glyph->right_box_line_p = it->end_of_box_run_p;
26304 glyph->overlaps_vertically_p = false;
26305 glyph->padding_p = false;
26306 glyph->glyph_not_available_p = false;
26307 glyph->face_id = it->face_id;
26308 glyph->u.img_id = img->id;
26309 glyph->slice.img = slice;
26310 glyph->font_type = FONT_TYPE_UNKNOWN;
26311 if (it->bidi_p)
26313 glyph->resolved_level = it->bidi_it.resolved_level;
26314 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26315 glyph->bidi_type = it->bidi_it.type;
26317 ++it->glyph_row->used[area];
26319 else
26320 IT_EXPAND_MATRIX_WIDTH (it, area);
26324 static void
26325 produce_xwidget_glyph (struct it *it)
26327 #ifdef HAVE_XWIDGETS
26328 struct xwidget *xw;
26329 int glyph_ascent, crop;
26330 eassert (it->what == IT_XWIDGET);
26332 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26333 /* Make sure X resources of the face is loaded. */
26334 prepare_face_for_display (it->f, face);
26336 xw = it->xwidget;
26337 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26338 it->descent = xw->height/2;
26339 it->phys_descent = it->descent;
26340 it->pixel_width = xw->width;
26341 /* It's quite possible for images to have an ascent greater than
26342 their height, so don't get confused in that case. */
26343 if (it->descent < 0)
26344 it->descent = 0;
26346 it->nglyphs = 1;
26348 if (face->box != FACE_NO_BOX)
26350 if (face->box_line_width > 0)
26352 it->ascent += face->box_line_width;
26353 it->descent += face->box_line_width;
26356 if (it->start_of_box_run_p)
26357 it->pixel_width += eabs (face->box_line_width);
26358 it->pixel_width += eabs (face->box_line_width);
26361 take_vertical_position_into_account (it);
26363 /* Automatically crop wide image glyphs at right edge so we can
26364 draw the cursor on same display row. */
26365 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26366 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26367 it->pixel_width -= crop;
26369 if (it->glyph_row)
26371 enum glyph_row_area area = it->area;
26372 struct glyph *glyph
26373 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26375 if (it->glyph_row->reversed_p)
26377 struct glyph *g;
26379 /* Make room for the new glyph. */
26380 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26381 g[1] = *g;
26382 glyph = it->glyph_row->glyphs[it->area];
26384 if (glyph < it->glyph_row->glyphs[area + 1])
26386 glyph->charpos = CHARPOS (it->position);
26387 glyph->object = it->object;
26388 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26389 glyph->ascent = glyph_ascent;
26390 glyph->descent = it->descent;
26391 glyph->voffset = it->voffset;
26392 glyph->type = XWIDGET_GLYPH;
26393 glyph->avoid_cursor_p = it->avoid_cursor_p;
26394 glyph->multibyte_p = it->multibyte_p;
26395 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26397 /* In R2L rows, the left and the right box edges need to be
26398 drawn in reverse direction. */
26399 glyph->right_box_line_p = it->start_of_box_run_p;
26400 glyph->left_box_line_p = it->end_of_box_run_p;
26402 else
26404 glyph->left_box_line_p = it->start_of_box_run_p;
26405 glyph->right_box_line_p = it->end_of_box_run_p;
26407 glyph->overlaps_vertically_p = 0;
26408 glyph->padding_p = 0;
26409 glyph->glyph_not_available_p = 0;
26410 glyph->face_id = it->face_id;
26411 glyph->u.xwidget = it->xwidget;
26412 glyph->font_type = FONT_TYPE_UNKNOWN;
26413 if (it->bidi_p)
26415 glyph->resolved_level = it->bidi_it.resolved_level;
26416 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26417 glyph->bidi_type = it->bidi_it.type;
26419 ++it->glyph_row->used[area];
26421 else
26422 IT_EXPAND_MATRIX_WIDTH (it, area);
26424 #endif
26427 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26428 of the glyph, WIDTH and HEIGHT are the width and height of the
26429 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26431 static void
26432 append_stretch_glyph (struct it *it, Lisp_Object object,
26433 int width, int height, int ascent)
26435 struct glyph *glyph;
26436 enum glyph_row_area area = it->area;
26438 eassert (ascent >= 0 && ascent <= height);
26440 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26441 if (glyph < it->glyph_row->glyphs[area + 1])
26443 /* If the glyph row is reversed, we need to prepend the glyph
26444 rather than append it. */
26445 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26447 struct glyph *g;
26449 /* Make room for the additional glyph. */
26450 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26451 g[1] = *g;
26452 glyph = it->glyph_row->glyphs[area];
26454 /* Decrease the width of the first glyph of the row that
26455 begins before first_visible_x (e.g., due to hscroll).
26456 This is so the overall width of the row becomes smaller
26457 by the scroll amount, and the stretch glyph appended by
26458 extend_face_to_end_of_line will be wider, to shift the
26459 row glyphs to the right. (In L2R rows, the corresponding
26460 left-shift effect is accomplished by setting row->x to a
26461 negative value, which won't work with R2L rows.)
26463 This must leave us with a positive value of WIDTH, since
26464 otherwise the call to move_it_in_display_line_to at the
26465 beginning of display_line would have got past the entire
26466 first glyph, and then it->current_x would have been
26467 greater or equal to it->first_visible_x. */
26468 if (it->current_x < it->first_visible_x)
26469 width -= it->first_visible_x - it->current_x;
26470 eassert (width > 0);
26472 glyph->charpos = CHARPOS (it->position);
26473 glyph->object = object;
26474 /* FIXME: It would be better to use TYPE_MAX here, but
26475 __typeof__ is not portable enough... */
26476 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26477 glyph->ascent = ascent;
26478 glyph->descent = height - ascent;
26479 glyph->voffset = it->voffset;
26480 glyph->type = STRETCH_GLYPH;
26481 glyph->avoid_cursor_p = it->avoid_cursor_p;
26482 glyph->multibyte_p = it->multibyte_p;
26483 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26485 /* In R2L rows, the left and the right box edges need to be
26486 drawn in reverse direction. */
26487 glyph->right_box_line_p = it->start_of_box_run_p;
26488 glyph->left_box_line_p = it->end_of_box_run_p;
26490 else
26492 glyph->left_box_line_p = it->start_of_box_run_p;
26493 glyph->right_box_line_p = it->end_of_box_run_p;
26495 glyph->overlaps_vertically_p = false;
26496 glyph->padding_p = false;
26497 glyph->glyph_not_available_p = false;
26498 glyph->face_id = it->face_id;
26499 glyph->u.stretch.ascent = ascent;
26500 glyph->u.stretch.height = height;
26501 glyph->slice.img = null_glyph_slice;
26502 glyph->font_type = FONT_TYPE_UNKNOWN;
26503 if (it->bidi_p)
26505 glyph->resolved_level = it->bidi_it.resolved_level;
26506 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26507 glyph->bidi_type = it->bidi_it.type;
26509 else
26511 glyph->resolved_level = 0;
26512 glyph->bidi_type = UNKNOWN_BT;
26514 ++it->glyph_row->used[area];
26516 else
26517 IT_EXPAND_MATRIX_WIDTH (it, area);
26520 #endif /* HAVE_WINDOW_SYSTEM */
26522 /* Produce a stretch glyph for iterator IT. IT->object is the value
26523 of the glyph property displayed. The value must be a list
26524 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26525 being recognized:
26527 1. `:width WIDTH' specifies that the space should be WIDTH *
26528 canonical char width wide. WIDTH may be an integer or floating
26529 point number.
26531 2. `:relative-width FACTOR' specifies that the width of the stretch
26532 should be computed from the width of the first character having the
26533 `glyph' property, and should be FACTOR times that width.
26535 3. `:align-to HPOS' specifies that the space should be wide enough
26536 to reach HPOS, a value in canonical character units.
26538 Exactly one of the above pairs must be present.
26540 4. `:height HEIGHT' specifies that the height of the stretch produced
26541 should be HEIGHT, measured in canonical character units.
26543 5. `:relative-height FACTOR' specifies that the height of the
26544 stretch should be FACTOR times the height of the characters having
26545 the glyph property.
26547 Either none or exactly one of 4 or 5 must be present.
26549 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26550 of the stretch should be used for the ascent of the stretch.
26551 ASCENT must be in the range 0 <= ASCENT <= 100. */
26553 void
26554 produce_stretch_glyph (struct it *it)
26556 /* (space :width WIDTH :height HEIGHT ...) */
26557 Lisp_Object prop, plist;
26558 int width = 0, height = 0, align_to = -1;
26559 bool zero_width_ok_p = false;
26560 double tem;
26561 struct font *font = NULL;
26563 #ifdef HAVE_WINDOW_SYSTEM
26564 int ascent = 0;
26565 bool zero_height_ok_p = false;
26567 if (FRAME_WINDOW_P (it->f))
26569 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26570 font = face->font ? face->font : FRAME_FONT (it->f);
26571 prepare_face_for_display (it->f, face);
26573 #endif
26575 /* List should start with `space'. */
26576 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26577 plist = XCDR (it->object);
26579 /* Compute the width of the stretch. */
26580 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26581 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26583 /* Absolute width `:width WIDTH' specified and valid. */
26584 zero_width_ok_p = true;
26585 width = (int)tem;
26587 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26589 /* Relative width `:relative-width FACTOR' specified and valid.
26590 Compute the width of the characters having the `glyph'
26591 property. */
26592 struct it it2;
26593 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26595 it2 = *it;
26596 if (it->multibyte_p)
26597 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26598 else
26600 it2.c = it2.char_to_display = *p, it2.len = 1;
26601 if (! ASCII_CHAR_P (it2.c))
26602 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26605 it2.glyph_row = NULL;
26606 it2.what = IT_CHARACTER;
26607 PRODUCE_GLYPHS (&it2);
26608 width = NUMVAL (prop) * it2.pixel_width;
26610 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26611 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26612 &align_to))
26614 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26615 align_to = (align_to < 0
26617 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26618 else if (align_to < 0)
26619 align_to = window_box_left_offset (it->w, TEXT_AREA);
26620 width = max (0, (int)tem + align_to - it->current_x);
26621 zero_width_ok_p = true;
26623 else
26624 /* Nothing specified -> width defaults to canonical char width. */
26625 width = FRAME_COLUMN_WIDTH (it->f);
26627 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26628 width = 1;
26630 #ifdef HAVE_WINDOW_SYSTEM
26631 /* Compute height. */
26632 if (FRAME_WINDOW_P (it->f))
26634 int default_height = normal_char_height (font, ' ');
26636 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26637 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26639 height = (int)tem;
26640 zero_height_ok_p = true;
26642 else if (prop = Fplist_get (plist, QCrelative_height),
26643 NUMVAL (prop) > 0)
26644 height = default_height * NUMVAL (prop);
26645 else
26646 height = default_height;
26648 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26649 height = 1;
26651 /* Compute percentage of height used for ascent. If
26652 `:ascent ASCENT' is present and valid, use that. Otherwise,
26653 derive the ascent from the font in use. */
26654 if (prop = Fplist_get (plist, QCascent),
26655 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26656 ascent = height * NUMVAL (prop) / 100.0;
26657 else if (!NILP (prop)
26658 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26659 ascent = min (max (0, (int)tem), height);
26660 else
26661 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26663 else
26664 #endif /* HAVE_WINDOW_SYSTEM */
26665 height = 1;
26667 if (width > 0 && it->line_wrap != TRUNCATE
26668 && it->current_x + width > it->last_visible_x)
26670 width = it->last_visible_x - it->current_x;
26671 #ifdef HAVE_WINDOW_SYSTEM
26672 /* Subtract one more pixel from the stretch width, but only on
26673 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26674 width -= FRAME_WINDOW_P (it->f);
26675 #endif
26678 if (width > 0 && height > 0 && it->glyph_row)
26680 Lisp_Object o_object = it->object;
26681 Lisp_Object object = it->stack[it->sp - 1].string;
26682 int n = width;
26684 if (!STRINGP (object))
26685 object = it->w->contents;
26686 #ifdef HAVE_WINDOW_SYSTEM
26687 if (FRAME_WINDOW_P (it->f))
26688 append_stretch_glyph (it, object, width, height, ascent);
26689 else
26690 #endif
26692 it->object = object;
26693 it->char_to_display = ' ';
26694 it->pixel_width = it->len = 1;
26695 while (n--)
26696 tty_append_glyph (it);
26697 it->object = o_object;
26701 it->pixel_width = width;
26702 #ifdef HAVE_WINDOW_SYSTEM
26703 if (FRAME_WINDOW_P (it->f))
26705 it->ascent = it->phys_ascent = ascent;
26706 it->descent = it->phys_descent = height - it->ascent;
26707 it->nglyphs = width > 0 && height > 0;
26708 take_vertical_position_into_account (it);
26710 else
26711 #endif
26712 it->nglyphs = width;
26715 /* Get information about special display element WHAT in an
26716 environment described by IT. WHAT is one of IT_TRUNCATION or
26717 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26718 non-null glyph_row member. This function ensures that fields like
26719 face_id, c, len of IT are left untouched. */
26721 static void
26722 produce_special_glyphs (struct it *it, enum display_element_type what)
26724 struct it temp_it;
26725 Lisp_Object gc;
26726 GLYPH glyph;
26728 temp_it = *it;
26729 temp_it.object = Qnil;
26730 memset (&temp_it.current, 0, sizeof temp_it.current);
26732 if (what == IT_CONTINUATION)
26734 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26735 if (it->bidi_it.paragraph_dir == R2L)
26736 SET_GLYPH_FROM_CHAR (glyph, '/');
26737 else
26738 SET_GLYPH_FROM_CHAR (glyph, '\\');
26739 if (it->dp
26740 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26742 /* FIXME: Should we mirror GC for R2L lines? */
26743 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26744 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26747 else if (what == IT_TRUNCATION)
26749 /* Truncation glyph. */
26750 SET_GLYPH_FROM_CHAR (glyph, '$');
26751 if (it->dp
26752 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26754 /* FIXME: Should we mirror GC for R2L lines? */
26755 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26756 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26759 else
26760 emacs_abort ();
26762 #ifdef HAVE_WINDOW_SYSTEM
26763 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26764 is turned off, we precede the truncation/continuation glyphs by a
26765 stretch glyph whose width is computed such that these special
26766 glyphs are aligned at the window margin, even when very different
26767 fonts are used in different glyph rows. */
26768 if (FRAME_WINDOW_P (temp_it.f)
26769 /* init_iterator calls this with it->glyph_row == NULL, and it
26770 wants only the pixel width of the truncation/continuation
26771 glyphs. */
26772 && temp_it.glyph_row
26773 /* insert_left_trunc_glyphs calls us at the beginning of the
26774 row, and it has its own calculation of the stretch glyph
26775 width. */
26776 && temp_it.glyph_row->used[TEXT_AREA] > 0
26777 && (temp_it.glyph_row->reversed_p
26778 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26779 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26781 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26783 if (stretch_width > 0)
26785 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26786 struct font *font =
26787 face->font ? face->font : FRAME_FONT (temp_it.f);
26788 int stretch_ascent =
26789 (((temp_it.ascent + temp_it.descent)
26790 * FONT_BASE (font)) / FONT_HEIGHT (font));
26792 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26793 temp_it.ascent + temp_it.descent,
26794 stretch_ascent);
26797 #endif
26799 temp_it.dp = NULL;
26800 temp_it.what = IT_CHARACTER;
26801 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26802 temp_it.face_id = GLYPH_FACE (glyph);
26803 temp_it.len = CHAR_BYTES (temp_it.c);
26805 PRODUCE_GLYPHS (&temp_it);
26806 it->pixel_width = temp_it.pixel_width;
26807 it->nglyphs = temp_it.nglyphs;
26810 #ifdef HAVE_WINDOW_SYSTEM
26812 /* Calculate line-height and line-spacing properties.
26813 An integer value specifies explicit pixel value.
26814 A float value specifies relative value to current face height.
26815 A cons (float . face-name) specifies relative value to
26816 height of specified face font.
26818 Returns height in pixels, or nil. */
26820 static Lisp_Object
26821 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26822 int boff, bool override)
26824 Lisp_Object face_name = Qnil;
26825 int ascent, descent, height;
26827 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26828 return val;
26830 if (CONSP (val))
26832 face_name = XCAR (val);
26833 val = XCDR (val);
26834 if (!NUMBERP (val))
26835 val = make_number (1);
26836 if (NILP (face_name))
26838 height = it->ascent + it->descent;
26839 goto scale;
26843 if (NILP (face_name))
26845 font = FRAME_FONT (it->f);
26846 boff = FRAME_BASELINE_OFFSET (it->f);
26848 else if (EQ (face_name, Qt))
26850 override = false;
26852 else
26854 int face_id;
26855 struct face *face;
26857 face_id = lookup_named_face (it->f, face_name, false);
26858 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26859 if (face == NULL || ((font = face->font) == NULL))
26860 return make_number (-1);
26861 boff = font->baseline_offset;
26862 if (font->vertical_centering)
26863 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26866 normal_char_ascent_descent (font, -1, &ascent, &descent);
26868 if (override)
26870 it->override_ascent = ascent;
26871 it->override_descent = descent;
26872 it->override_boff = boff;
26875 height = ascent + descent;
26877 scale:
26878 if (FLOATP (val))
26879 height = (int)(XFLOAT_DATA (val) * height);
26880 else if (INTEGERP (val))
26881 height *= XINT (val);
26883 return make_number (height);
26887 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26888 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26889 and only if this is for a character for which no font was found.
26891 If the display method (it->glyphless_method) is
26892 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26893 length of the acronym or the hexadecimal string, UPPER_XOFF and
26894 UPPER_YOFF are pixel offsets for the upper part of the string,
26895 LOWER_XOFF and LOWER_YOFF are for the lower part.
26897 For the other display methods, LEN through LOWER_YOFF are zero. */
26899 static void
26900 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26901 short upper_xoff, short upper_yoff,
26902 short lower_xoff, short lower_yoff)
26904 struct glyph *glyph;
26905 enum glyph_row_area area = it->area;
26907 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26908 if (glyph < it->glyph_row->glyphs[area + 1])
26910 /* If the glyph row is reversed, we need to prepend the glyph
26911 rather than append it. */
26912 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26914 struct glyph *g;
26916 /* Make room for the additional glyph. */
26917 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26918 g[1] = *g;
26919 glyph = it->glyph_row->glyphs[area];
26921 glyph->charpos = CHARPOS (it->position);
26922 glyph->object = it->object;
26923 eassert (it->pixel_width <= SHRT_MAX);
26924 glyph->pixel_width = it->pixel_width;
26925 glyph->ascent = it->ascent;
26926 glyph->descent = it->descent;
26927 glyph->voffset = it->voffset;
26928 glyph->type = GLYPHLESS_GLYPH;
26929 glyph->u.glyphless.method = it->glyphless_method;
26930 glyph->u.glyphless.for_no_font = for_no_font;
26931 glyph->u.glyphless.len = len;
26932 glyph->u.glyphless.ch = it->c;
26933 glyph->slice.glyphless.upper_xoff = upper_xoff;
26934 glyph->slice.glyphless.upper_yoff = upper_yoff;
26935 glyph->slice.glyphless.lower_xoff = lower_xoff;
26936 glyph->slice.glyphless.lower_yoff = lower_yoff;
26937 glyph->avoid_cursor_p = it->avoid_cursor_p;
26938 glyph->multibyte_p = it->multibyte_p;
26939 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26941 /* In R2L rows, the left and the right box edges need to be
26942 drawn in reverse direction. */
26943 glyph->right_box_line_p = it->start_of_box_run_p;
26944 glyph->left_box_line_p = it->end_of_box_run_p;
26946 else
26948 glyph->left_box_line_p = it->start_of_box_run_p;
26949 glyph->right_box_line_p = it->end_of_box_run_p;
26951 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26952 || it->phys_descent > it->descent);
26953 glyph->padding_p = false;
26954 glyph->glyph_not_available_p = false;
26955 glyph->face_id = face_id;
26956 glyph->font_type = FONT_TYPE_UNKNOWN;
26957 if (it->bidi_p)
26959 glyph->resolved_level = it->bidi_it.resolved_level;
26960 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26961 glyph->bidi_type = it->bidi_it.type;
26963 ++it->glyph_row->used[area];
26965 else
26966 IT_EXPAND_MATRIX_WIDTH (it, area);
26970 /* Produce a glyph for a glyphless character for iterator IT.
26971 IT->glyphless_method specifies which method to use for displaying
26972 the character. See the description of enum
26973 glyphless_display_method in dispextern.h for the detail.
26975 FOR_NO_FONT is true if and only if this is for a character for
26976 which no font was found. ACRONYM, if non-nil, is an acronym string
26977 for the character. */
26979 static void
26980 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26982 int face_id;
26983 struct face *face;
26984 struct font *font;
26985 int base_width, base_height, width, height;
26986 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26987 int len;
26989 /* Get the metrics of the base font. We always refer to the current
26990 ASCII face. */
26991 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26992 font = face->font ? face->font : FRAME_FONT (it->f);
26993 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26994 it->ascent += font->baseline_offset;
26995 it->descent -= font->baseline_offset;
26996 base_height = it->ascent + it->descent;
26997 base_width = font->average_width;
26999 face_id = merge_glyphless_glyph_face (it);
27001 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27003 it->pixel_width = THIN_SPACE_WIDTH;
27004 len = 0;
27005 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27007 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27009 width = CHARACTER_WIDTH (it->c);
27010 if (width == 0)
27011 width = 1;
27012 else if (width > 4)
27013 width = 4;
27014 it->pixel_width = base_width * width;
27015 len = 0;
27016 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27018 else
27020 char buf[7];
27021 const char *str;
27022 unsigned int code[6];
27023 int upper_len;
27024 int ascent, descent;
27025 struct font_metrics metrics_upper, metrics_lower;
27027 face = FACE_FROM_ID (it->f, face_id);
27028 font = face->font ? face->font : FRAME_FONT (it->f);
27029 prepare_face_for_display (it->f, face);
27031 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27033 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27034 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27035 if (CONSP (acronym))
27036 acronym = XCAR (acronym);
27037 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27039 else
27041 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27042 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27043 str = buf;
27045 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27046 code[len] = font->driver->encode_char (font, str[len]);
27047 upper_len = (len + 1) / 2;
27048 font->driver->text_extents (font, code, upper_len,
27049 &metrics_upper);
27050 font->driver->text_extents (font, code + upper_len, len - upper_len,
27051 &metrics_lower);
27055 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27056 width = max (metrics_upper.width, metrics_lower.width) + 4;
27057 upper_xoff = upper_yoff = 2; /* the typical case */
27058 if (base_width >= width)
27060 /* Align the upper to the left, the lower to the right. */
27061 it->pixel_width = base_width;
27062 lower_xoff = base_width - 2 - metrics_lower.width;
27064 else
27066 /* Center the shorter one. */
27067 it->pixel_width = width;
27068 if (metrics_upper.width >= metrics_lower.width)
27069 lower_xoff = (width - metrics_lower.width) / 2;
27070 else
27072 /* FIXME: This code doesn't look right. It formerly was
27073 missing the "lower_xoff = 0;", which couldn't have
27074 been right since it left lower_xoff uninitialized. */
27075 lower_xoff = 0;
27076 upper_xoff = (width - metrics_upper.width) / 2;
27080 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27081 top, bottom, and between upper and lower strings. */
27082 height = (metrics_upper.ascent + metrics_upper.descent
27083 + metrics_lower.ascent + metrics_lower.descent) + 5;
27084 /* Center vertically.
27085 H:base_height, D:base_descent
27086 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27088 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27089 descent = D - H/2 + h/2;
27090 lower_yoff = descent - 2 - ld;
27091 upper_yoff = lower_yoff - la - 1 - ud; */
27092 ascent = - (it->descent - (base_height + height + 1) / 2);
27093 descent = it->descent - (base_height - height) / 2;
27094 lower_yoff = descent - 2 - metrics_lower.descent;
27095 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27096 - metrics_upper.descent);
27097 /* Don't make the height shorter than the base height. */
27098 if (height > base_height)
27100 it->ascent = ascent;
27101 it->descent = descent;
27105 it->phys_ascent = it->ascent;
27106 it->phys_descent = it->descent;
27107 if (it->glyph_row)
27108 append_glyphless_glyph (it, face_id, for_no_font, len,
27109 upper_xoff, upper_yoff,
27110 lower_xoff, lower_yoff);
27111 it->nglyphs = 1;
27112 take_vertical_position_into_account (it);
27116 /* RIF:
27117 Produce glyphs/get display metrics for the display element IT is
27118 loaded with. See the description of struct it in dispextern.h
27119 for an overview of struct it. */
27121 void
27122 x_produce_glyphs (struct it *it)
27124 int extra_line_spacing = it->extra_line_spacing;
27126 it->glyph_not_available_p = false;
27128 if (it->what == IT_CHARACTER)
27130 XChar2b char2b;
27131 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27132 struct font *font = face->font;
27133 struct font_metrics *pcm = NULL;
27134 int boff; /* Baseline offset. */
27136 if (font == NULL)
27138 /* When no suitable font is found, display this character by
27139 the method specified in the first extra slot of
27140 Vglyphless_char_display. */
27141 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27143 eassert (it->what == IT_GLYPHLESS);
27144 produce_glyphless_glyph (it, true,
27145 STRINGP (acronym) ? acronym : Qnil);
27146 goto done;
27149 boff = font->baseline_offset;
27150 if (font->vertical_centering)
27151 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27153 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27155 it->nglyphs = 1;
27157 if (it->override_ascent >= 0)
27159 it->ascent = it->override_ascent;
27160 it->descent = it->override_descent;
27161 boff = it->override_boff;
27163 else
27165 it->ascent = FONT_BASE (font) + boff;
27166 it->descent = FONT_DESCENT (font) - boff;
27169 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27171 pcm = get_per_char_metric (font, &char2b);
27172 if (pcm->width == 0
27173 && pcm->rbearing == 0 && pcm->lbearing == 0)
27174 pcm = NULL;
27177 if (pcm)
27179 it->phys_ascent = pcm->ascent + boff;
27180 it->phys_descent = pcm->descent - boff;
27181 it->pixel_width = pcm->width;
27182 /* Don't use font-global values for ascent and descent
27183 if they result in an exceedingly large line height. */
27184 if (it->override_ascent < 0)
27186 if (FONT_TOO_HIGH (font))
27188 it->ascent = it->phys_ascent;
27189 it->descent = it->phys_descent;
27190 /* These limitations are enforced by an
27191 assertion near the end of this function. */
27192 if (it->ascent < 0)
27193 it->ascent = 0;
27194 if (it->descent < 0)
27195 it->descent = 0;
27199 else
27201 it->glyph_not_available_p = true;
27202 it->phys_ascent = it->ascent;
27203 it->phys_descent = it->descent;
27204 it->pixel_width = font->space_width;
27207 if (it->constrain_row_ascent_descent_p)
27209 if (it->descent > it->max_descent)
27211 it->ascent += it->descent - it->max_descent;
27212 it->descent = it->max_descent;
27214 if (it->ascent > it->max_ascent)
27216 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27217 it->ascent = it->max_ascent;
27219 it->phys_ascent = min (it->phys_ascent, it->ascent);
27220 it->phys_descent = min (it->phys_descent, it->descent);
27221 extra_line_spacing = 0;
27224 /* If this is a space inside a region of text with
27225 `space-width' property, change its width. */
27226 bool stretched_p
27227 = it->char_to_display == ' ' && !NILP (it->space_width);
27228 if (stretched_p)
27229 it->pixel_width *= XFLOATINT (it->space_width);
27231 /* If face has a box, add the box thickness to the character
27232 height. If character has a box line to the left and/or
27233 right, add the box line width to the character's width. */
27234 if (face->box != FACE_NO_BOX)
27236 int thick = face->box_line_width;
27238 if (thick > 0)
27240 it->ascent += thick;
27241 it->descent += thick;
27243 else
27244 thick = -thick;
27246 if (it->start_of_box_run_p)
27247 it->pixel_width += thick;
27248 if (it->end_of_box_run_p)
27249 it->pixel_width += thick;
27252 /* If face has an overline, add the height of the overline
27253 (1 pixel) and a 1 pixel margin to the character height. */
27254 if (face->overline_p)
27255 it->ascent += overline_margin;
27257 if (it->constrain_row_ascent_descent_p)
27259 if (it->ascent > it->max_ascent)
27260 it->ascent = it->max_ascent;
27261 if (it->descent > it->max_descent)
27262 it->descent = it->max_descent;
27265 take_vertical_position_into_account (it);
27267 /* If we have to actually produce glyphs, do it. */
27268 if (it->glyph_row)
27270 if (stretched_p)
27272 /* Translate a space with a `space-width' property
27273 into a stretch glyph. */
27274 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27275 / FONT_HEIGHT (font));
27276 append_stretch_glyph (it, it->object, it->pixel_width,
27277 it->ascent + it->descent, ascent);
27279 else
27280 append_glyph (it);
27282 /* If characters with lbearing or rbearing are displayed
27283 in this line, record that fact in a flag of the
27284 glyph row. This is used to optimize X output code. */
27285 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27286 it->glyph_row->contains_overlapping_glyphs_p = true;
27288 if (! stretched_p && it->pixel_width == 0)
27289 /* We assure that all visible glyphs have at least 1-pixel
27290 width. */
27291 it->pixel_width = 1;
27293 else if (it->char_to_display == '\n')
27295 /* A newline has no width, but we need the height of the
27296 line. But if previous part of the line sets a height,
27297 don't increase that height. */
27299 Lisp_Object height;
27300 Lisp_Object total_height = Qnil;
27302 it->override_ascent = -1;
27303 it->pixel_width = 0;
27304 it->nglyphs = 0;
27306 height = get_it_property (it, Qline_height);
27307 /* Split (line-height total-height) list. */
27308 if (CONSP (height)
27309 && CONSP (XCDR (height))
27310 && NILP (XCDR (XCDR (height))))
27312 total_height = XCAR (XCDR (height));
27313 height = XCAR (height);
27315 height = calc_line_height_property (it, height, font, boff, true);
27317 if (it->override_ascent >= 0)
27319 it->ascent = it->override_ascent;
27320 it->descent = it->override_descent;
27321 boff = it->override_boff;
27323 else
27325 if (FONT_TOO_HIGH (font))
27327 it->ascent = font->pixel_size + boff - 1;
27328 it->descent = -boff + 1;
27329 if (it->descent < 0)
27330 it->descent = 0;
27332 else
27334 it->ascent = FONT_BASE (font) + boff;
27335 it->descent = FONT_DESCENT (font) - boff;
27339 if (EQ (height, Qt))
27341 if (it->descent > it->max_descent)
27343 it->ascent += it->descent - it->max_descent;
27344 it->descent = it->max_descent;
27346 if (it->ascent > it->max_ascent)
27348 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27349 it->ascent = it->max_ascent;
27351 it->phys_ascent = min (it->phys_ascent, it->ascent);
27352 it->phys_descent = min (it->phys_descent, it->descent);
27353 it->constrain_row_ascent_descent_p = true;
27354 extra_line_spacing = 0;
27356 else
27358 Lisp_Object spacing;
27360 it->phys_ascent = it->ascent;
27361 it->phys_descent = it->descent;
27363 if ((it->max_ascent > 0 || it->max_descent > 0)
27364 && face->box != FACE_NO_BOX
27365 && face->box_line_width > 0)
27367 it->ascent += face->box_line_width;
27368 it->descent += face->box_line_width;
27370 if (!NILP (height)
27371 && XINT (height) > it->ascent + it->descent)
27372 it->ascent = XINT (height) - it->descent;
27374 if (!NILP (total_height))
27375 spacing = calc_line_height_property (it, total_height, font,
27376 boff, false);
27377 else
27379 spacing = get_it_property (it, Qline_spacing);
27380 spacing = calc_line_height_property (it, spacing, font,
27381 boff, false);
27383 if (INTEGERP (spacing))
27385 extra_line_spacing = XINT (spacing);
27386 if (!NILP (total_height))
27387 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27391 else /* i.e. (it->char_to_display == '\t') */
27393 if (font->space_width > 0)
27395 int tab_width = it->tab_width * font->space_width;
27396 int x = it->current_x + it->continuation_lines_width;
27397 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27399 /* If the distance from the current position to the next tab
27400 stop is less than a space character width, use the
27401 tab stop after that. */
27402 if (next_tab_x - x < font->space_width)
27403 next_tab_x += tab_width;
27405 it->pixel_width = next_tab_x - x;
27406 it->nglyphs = 1;
27407 if (FONT_TOO_HIGH (font))
27409 if (get_char_glyph_code (' ', font, &char2b))
27411 pcm = get_per_char_metric (font, &char2b);
27412 if (pcm->width == 0
27413 && pcm->rbearing == 0 && pcm->lbearing == 0)
27414 pcm = NULL;
27417 if (pcm)
27419 it->ascent = pcm->ascent + boff;
27420 it->descent = pcm->descent - boff;
27422 else
27424 it->ascent = font->pixel_size + boff - 1;
27425 it->descent = -boff + 1;
27427 if (it->ascent < 0)
27428 it->ascent = 0;
27429 if (it->descent < 0)
27430 it->descent = 0;
27432 else
27434 it->ascent = FONT_BASE (font) + boff;
27435 it->descent = FONT_DESCENT (font) - boff;
27437 it->phys_ascent = it->ascent;
27438 it->phys_descent = it->descent;
27440 if (it->glyph_row)
27442 append_stretch_glyph (it, it->object, it->pixel_width,
27443 it->ascent + it->descent, it->ascent);
27446 else
27448 it->pixel_width = 0;
27449 it->nglyphs = 1;
27453 if (FONT_TOO_HIGH (font))
27455 int font_ascent, font_descent;
27457 /* For very large fonts, where we ignore the declared font
27458 dimensions, and go by per-character metrics instead,
27459 don't let the row ascent and descent values (and the row
27460 height computed from them) be smaller than the "normal"
27461 character metrics. This avoids unpleasant effects
27462 whereby lines on display would change their height
27463 depending on which characters are shown. */
27464 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27465 it->max_ascent = max (it->max_ascent, font_ascent);
27466 it->max_descent = max (it->max_descent, font_descent);
27469 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27471 /* A static composition.
27473 Note: A composition is represented as one glyph in the
27474 glyph matrix. There are no padding glyphs.
27476 Important note: pixel_width, ascent, and descent are the
27477 values of what is drawn by draw_glyphs (i.e. the values of
27478 the overall glyphs composed). */
27479 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27480 int boff; /* baseline offset */
27481 struct composition *cmp = composition_table[it->cmp_it.id];
27482 int glyph_len = cmp->glyph_len;
27483 struct font *font = face->font;
27485 it->nglyphs = 1;
27487 /* If we have not yet calculated pixel size data of glyphs of
27488 the composition for the current face font, calculate them
27489 now. Theoretically, we have to check all fonts for the
27490 glyphs, but that requires much time and memory space. So,
27491 here we check only the font of the first glyph. This may
27492 lead to incorrect display, but it's very rare, and C-l
27493 (recenter-top-bottom) can correct the display anyway. */
27494 if (! cmp->font || cmp->font != font)
27496 /* Ascent and descent of the font of the first character
27497 of this composition (adjusted by baseline offset).
27498 Ascent and descent of overall glyphs should not be less
27499 than these, respectively. */
27500 int font_ascent, font_descent, font_height;
27501 /* Bounding box of the overall glyphs. */
27502 int leftmost, rightmost, lowest, highest;
27503 int lbearing, rbearing;
27504 int i, width, ascent, descent;
27505 int c;
27506 XChar2b char2b;
27507 struct font_metrics *pcm;
27508 ptrdiff_t pos;
27510 eassume (0 < glyph_len); /* See Bug#8512. */
27512 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27513 while (c == '\t' && 0 < --glyph_len);
27515 bool right_padded = glyph_len < cmp->glyph_len;
27516 for (i = 0; i < glyph_len; i++)
27518 c = COMPOSITION_GLYPH (cmp, i);
27519 if (c != '\t')
27520 break;
27521 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27523 bool left_padded = i > 0;
27525 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27526 : IT_CHARPOS (*it));
27527 /* If no suitable font is found, use the default font. */
27528 bool font_not_found_p = font == NULL;
27529 if (font_not_found_p)
27531 face = face->ascii_face;
27532 font = face->font;
27534 boff = font->baseline_offset;
27535 if (font->vertical_centering)
27536 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27537 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27538 font_ascent += boff;
27539 font_descent -= boff;
27540 font_height = font_ascent + font_descent;
27542 cmp->font = font;
27544 pcm = NULL;
27545 if (! font_not_found_p)
27547 get_char_face_and_encoding (it->f, c, it->face_id,
27548 &char2b, false);
27549 pcm = get_per_char_metric (font, &char2b);
27552 /* Initialize the bounding box. */
27553 if (pcm)
27555 width = cmp->glyph_len > 0 ? pcm->width : 0;
27556 ascent = pcm->ascent;
27557 descent = pcm->descent;
27558 lbearing = pcm->lbearing;
27559 rbearing = pcm->rbearing;
27561 else
27563 width = cmp->glyph_len > 0 ? font->space_width : 0;
27564 ascent = FONT_BASE (font);
27565 descent = FONT_DESCENT (font);
27566 lbearing = 0;
27567 rbearing = width;
27570 rightmost = width;
27571 leftmost = 0;
27572 lowest = - descent + boff;
27573 highest = ascent + boff;
27575 if (! font_not_found_p
27576 && font->default_ascent
27577 && CHAR_TABLE_P (Vuse_default_ascent)
27578 && !NILP (Faref (Vuse_default_ascent,
27579 make_number (it->char_to_display))))
27580 highest = font->default_ascent + boff;
27582 /* Draw the first glyph at the normal position. It may be
27583 shifted to right later if some other glyphs are drawn
27584 at the left. */
27585 cmp->offsets[i * 2] = 0;
27586 cmp->offsets[i * 2 + 1] = boff;
27587 cmp->lbearing = lbearing;
27588 cmp->rbearing = rbearing;
27590 /* Set cmp->offsets for the remaining glyphs. */
27591 for (i++; i < glyph_len; i++)
27593 int left, right, btm, top;
27594 int ch = COMPOSITION_GLYPH (cmp, i);
27595 int face_id;
27596 struct face *this_face;
27598 if (ch == '\t')
27599 ch = ' ';
27600 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27601 this_face = FACE_FROM_ID (it->f, face_id);
27602 font = this_face->font;
27604 if (font == NULL)
27605 pcm = NULL;
27606 else
27608 get_char_face_and_encoding (it->f, ch, face_id,
27609 &char2b, false);
27610 pcm = get_per_char_metric (font, &char2b);
27612 if (! pcm)
27613 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27614 else
27616 width = pcm->width;
27617 ascent = pcm->ascent;
27618 descent = pcm->descent;
27619 lbearing = pcm->lbearing;
27620 rbearing = pcm->rbearing;
27621 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27623 /* Relative composition with or without
27624 alternate chars. */
27625 left = (leftmost + rightmost - width) / 2;
27626 btm = - descent + boff;
27627 if (font->relative_compose
27628 && (! CHAR_TABLE_P (Vignore_relative_composition)
27629 || NILP (Faref (Vignore_relative_composition,
27630 make_number (ch)))))
27633 if (- descent >= font->relative_compose)
27634 /* One extra pixel between two glyphs. */
27635 btm = highest + 1;
27636 else if (ascent <= 0)
27637 /* One extra pixel between two glyphs. */
27638 btm = lowest - 1 - ascent - descent;
27641 else
27643 /* A composition rule is specified by an integer
27644 value that encodes global and new reference
27645 points (GREF and NREF). GREF and NREF are
27646 specified by numbers as below:
27648 0---1---2 -- ascent
27652 9--10--11 -- center
27654 ---3---4---5--- baseline
27656 6---7---8 -- descent
27658 int rule = COMPOSITION_RULE (cmp, i);
27659 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27661 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27662 grefx = gref % 3, nrefx = nref % 3;
27663 grefy = gref / 3, nrefy = nref / 3;
27664 if (xoff)
27665 xoff = font_height * (xoff - 128) / 256;
27666 if (yoff)
27667 yoff = font_height * (yoff - 128) / 256;
27669 left = (leftmost
27670 + grefx * (rightmost - leftmost) / 2
27671 - nrefx * width / 2
27672 + xoff);
27674 btm = ((grefy == 0 ? highest
27675 : grefy == 1 ? 0
27676 : grefy == 2 ? lowest
27677 : (highest + lowest) / 2)
27678 - (nrefy == 0 ? ascent + descent
27679 : nrefy == 1 ? descent - boff
27680 : nrefy == 2 ? 0
27681 : (ascent + descent) / 2)
27682 + yoff);
27685 cmp->offsets[i * 2] = left;
27686 cmp->offsets[i * 2 + 1] = btm + descent;
27688 /* Update the bounding box of the overall glyphs. */
27689 if (width > 0)
27691 right = left + width;
27692 if (left < leftmost)
27693 leftmost = left;
27694 if (right > rightmost)
27695 rightmost = right;
27697 top = btm + descent + ascent;
27698 if (top > highest)
27699 highest = top;
27700 if (btm < lowest)
27701 lowest = btm;
27703 if (cmp->lbearing > left + lbearing)
27704 cmp->lbearing = left + lbearing;
27705 if (cmp->rbearing < left + rbearing)
27706 cmp->rbearing = left + rbearing;
27710 /* If there are glyphs whose x-offsets are negative,
27711 shift all glyphs to the right and make all x-offsets
27712 non-negative. */
27713 if (leftmost < 0)
27715 for (i = 0; i < cmp->glyph_len; i++)
27716 cmp->offsets[i * 2] -= leftmost;
27717 rightmost -= leftmost;
27718 cmp->lbearing -= leftmost;
27719 cmp->rbearing -= leftmost;
27722 if (left_padded && cmp->lbearing < 0)
27724 for (i = 0; i < cmp->glyph_len; i++)
27725 cmp->offsets[i * 2] -= cmp->lbearing;
27726 rightmost -= cmp->lbearing;
27727 cmp->rbearing -= cmp->lbearing;
27728 cmp->lbearing = 0;
27730 if (right_padded && rightmost < cmp->rbearing)
27732 rightmost = cmp->rbearing;
27735 cmp->pixel_width = rightmost;
27736 cmp->ascent = highest;
27737 cmp->descent = - lowest;
27738 if (cmp->ascent < font_ascent)
27739 cmp->ascent = font_ascent;
27740 if (cmp->descent < font_descent)
27741 cmp->descent = font_descent;
27744 if (it->glyph_row
27745 && (cmp->lbearing < 0
27746 || cmp->rbearing > cmp->pixel_width))
27747 it->glyph_row->contains_overlapping_glyphs_p = true;
27749 it->pixel_width = cmp->pixel_width;
27750 it->ascent = it->phys_ascent = cmp->ascent;
27751 it->descent = it->phys_descent = cmp->descent;
27752 if (face->box != FACE_NO_BOX)
27754 int thick = face->box_line_width;
27756 if (thick > 0)
27758 it->ascent += thick;
27759 it->descent += thick;
27761 else
27762 thick = - thick;
27764 if (it->start_of_box_run_p)
27765 it->pixel_width += thick;
27766 if (it->end_of_box_run_p)
27767 it->pixel_width += thick;
27770 /* If face has an overline, add the height of the overline
27771 (1 pixel) and a 1 pixel margin to the character height. */
27772 if (face->overline_p)
27773 it->ascent += overline_margin;
27775 take_vertical_position_into_account (it);
27776 if (it->ascent < 0)
27777 it->ascent = 0;
27778 if (it->descent < 0)
27779 it->descent = 0;
27781 if (it->glyph_row && cmp->glyph_len > 0)
27782 append_composite_glyph (it);
27784 else if (it->what == IT_COMPOSITION)
27786 /* A dynamic (automatic) composition. */
27787 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27788 Lisp_Object gstring;
27789 struct font_metrics metrics;
27791 it->nglyphs = 1;
27793 gstring = composition_gstring_from_id (it->cmp_it.id);
27794 it->pixel_width
27795 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27796 &metrics);
27797 if (it->glyph_row
27798 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27799 it->glyph_row->contains_overlapping_glyphs_p = true;
27800 it->ascent = it->phys_ascent = metrics.ascent;
27801 it->descent = it->phys_descent = metrics.descent;
27802 if (face->box != FACE_NO_BOX)
27804 int thick = face->box_line_width;
27806 if (thick > 0)
27808 it->ascent += thick;
27809 it->descent += thick;
27811 else
27812 thick = - thick;
27814 if (it->start_of_box_run_p)
27815 it->pixel_width += thick;
27816 if (it->end_of_box_run_p)
27817 it->pixel_width += thick;
27819 /* If face has an overline, add the height of the overline
27820 (1 pixel) and a 1 pixel margin to the character height. */
27821 if (face->overline_p)
27822 it->ascent += overline_margin;
27823 take_vertical_position_into_account (it);
27824 if (it->ascent < 0)
27825 it->ascent = 0;
27826 if (it->descent < 0)
27827 it->descent = 0;
27829 if (it->glyph_row)
27830 append_composite_glyph (it);
27832 else if (it->what == IT_GLYPHLESS)
27833 produce_glyphless_glyph (it, false, Qnil);
27834 else if (it->what == IT_IMAGE)
27835 produce_image_glyph (it);
27836 else if (it->what == IT_STRETCH)
27837 produce_stretch_glyph (it);
27838 else if (it->what == IT_XWIDGET)
27839 produce_xwidget_glyph (it);
27841 done:
27842 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27843 because this isn't true for images with `:ascent 100'. */
27844 eassert (it->ascent >= 0 && it->descent >= 0);
27845 if (it->area == TEXT_AREA)
27846 it->current_x += it->pixel_width;
27848 if (extra_line_spacing > 0)
27850 it->descent += extra_line_spacing;
27851 if (extra_line_spacing > it->max_extra_line_spacing)
27852 it->max_extra_line_spacing = extra_line_spacing;
27855 it->max_ascent = max (it->max_ascent, it->ascent);
27856 it->max_descent = max (it->max_descent, it->descent);
27857 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27858 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27861 /* EXPORT for RIF:
27862 Output LEN glyphs starting at START at the nominal cursor position.
27863 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27864 being updated, and UPDATED_AREA is the area of that row being updated. */
27866 void
27867 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27868 struct glyph *start, enum glyph_row_area updated_area, int len)
27870 int x, hpos, chpos = w->phys_cursor.hpos;
27872 eassert (updated_row);
27873 /* When the window is hscrolled, cursor hpos can legitimately be out
27874 of bounds, but we draw the cursor at the corresponding window
27875 margin in that case. */
27876 if (!updated_row->reversed_p && chpos < 0)
27877 chpos = 0;
27878 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27879 chpos = updated_row->used[TEXT_AREA] - 1;
27881 block_input ();
27883 /* Write glyphs. */
27885 hpos = start - updated_row->glyphs[updated_area];
27886 x = draw_glyphs (w, w->output_cursor.x,
27887 updated_row, updated_area,
27888 hpos, hpos + len,
27889 DRAW_NORMAL_TEXT, 0);
27891 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27892 if (updated_area == TEXT_AREA
27893 && w->phys_cursor_on_p
27894 && w->phys_cursor.vpos == w->output_cursor.vpos
27895 && chpos >= hpos
27896 && chpos < hpos + len)
27897 w->phys_cursor_on_p = false;
27899 unblock_input ();
27901 /* Advance the output cursor. */
27902 w->output_cursor.hpos += len;
27903 w->output_cursor.x = x;
27907 /* EXPORT for RIF:
27908 Insert LEN glyphs from START at the nominal cursor position. */
27910 void
27911 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27912 struct glyph *start, enum glyph_row_area updated_area, int len)
27914 struct frame *f;
27915 int line_height, shift_by_width, shifted_region_width;
27916 struct glyph_row *row;
27917 struct glyph *glyph;
27918 int frame_x, frame_y;
27919 ptrdiff_t hpos;
27921 eassert (updated_row);
27922 block_input ();
27923 f = XFRAME (WINDOW_FRAME (w));
27925 /* Get the height of the line we are in. */
27926 row = updated_row;
27927 line_height = row->height;
27929 /* Get the width of the glyphs to insert. */
27930 shift_by_width = 0;
27931 for (glyph = start; glyph < start + len; ++glyph)
27932 shift_by_width += glyph->pixel_width;
27934 /* Get the width of the region to shift right. */
27935 shifted_region_width = (window_box_width (w, updated_area)
27936 - w->output_cursor.x
27937 - shift_by_width);
27939 /* Shift right. */
27940 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27941 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27943 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27944 line_height, shift_by_width);
27946 /* Write the glyphs. */
27947 hpos = start - row->glyphs[updated_area];
27948 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27949 hpos, hpos + len,
27950 DRAW_NORMAL_TEXT, 0);
27952 /* Advance the output cursor. */
27953 w->output_cursor.hpos += len;
27954 w->output_cursor.x += shift_by_width;
27955 unblock_input ();
27959 /* EXPORT for RIF:
27960 Erase the current text line from the nominal cursor position
27961 (inclusive) to pixel column TO_X (exclusive). The idea is that
27962 everything from TO_X onward is already erased.
27964 TO_X is a pixel position relative to UPDATED_AREA of currently
27965 updated window W. TO_X == -1 means clear to the end of this area. */
27967 void
27968 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27969 enum glyph_row_area updated_area, int to_x)
27971 struct frame *f;
27972 int max_x, min_y, max_y;
27973 int from_x, from_y, to_y;
27975 eassert (updated_row);
27976 f = XFRAME (w->frame);
27978 if (updated_row->full_width_p)
27979 max_x = (WINDOW_PIXEL_WIDTH (w)
27980 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27981 else
27982 max_x = window_box_width (w, updated_area);
27983 max_y = window_text_bottom_y (w);
27985 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27986 of window. For TO_X > 0, truncate to end of drawing area. */
27987 if (to_x == 0)
27988 return;
27989 else if (to_x < 0)
27990 to_x = max_x;
27991 else
27992 to_x = min (to_x, max_x);
27994 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27996 /* Notice if the cursor will be cleared by this operation. */
27997 if (!updated_row->full_width_p)
27998 notice_overwritten_cursor (w, updated_area,
27999 w->output_cursor.x, -1,
28000 updated_row->y,
28001 MATRIX_ROW_BOTTOM_Y (updated_row));
28003 from_x = w->output_cursor.x;
28005 /* Translate to frame coordinates. */
28006 if (updated_row->full_width_p)
28008 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28009 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28011 else
28013 int area_left = window_box_left (w, updated_area);
28014 from_x += area_left;
28015 to_x += area_left;
28018 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28019 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28020 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28022 /* Prevent inadvertently clearing to end of the X window. */
28023 if (to_x > from_x && to_y > from_y)
28025 block_input ();
28026 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28027 to_x - from_x, to_y - from_y);
28028 unblock_input ();
28032 #endif /* HAVE_WINDOW_SYSTEM */
28036 /***********************************************************************
28037 Cursor types
28038 ***********************************************************************/
28040 /* Value is the internal representation of the specified cursor type
28041 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28042 of the bar cursor. */
28044 static enum text_cursor_kinds
28045 get_specified_cursor_type (Lisp_Object arg, int *width)
28047 enum text_cursor_kinds type;
28049 if (NILP (arg))
28050 return NO_CURSOR;
28052 if (EQ (arg, Qbox))
28053 return FILLED_BOX_CURSOR;
28055 if (EQ (arg, Qhollow))
28056 return HOLLOW_BOX_CURSOR;
28058 if (EQ (arg, Qbar))
28060 *width = 2;
28061 return BAR_CURSOR;
28064 if (CONSP (arg)
28065 && EQ (XCAR (arg), Qbar)
28066 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28068 *width = XINT (XCDR (arg));
28069 return BAR_CURSOR;
28072 if (EQ (arg, Qhbar))
28074 *width = 2;
28075 return HBAR_CURSOR;
28078 if (CONSP (arg)
28079 && EQ (XCAR (arg), Qhbar)
28080 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28082 *width = XINT (XCDR (arg));
28083 return HBAR_CURSOR;
28086 /* Treat anything unknown as "hollow box cursor".
28087 It was bad to signal an error; people have trouble fixing
28088 .Xdefaults with Emacs, when it has something bad in it. */
28089 type = HOLLOW_BOX_CURSOR;
28091 return type;
28094 /* Set the default cursor types for specified frame. */
28095 void
28096 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28098 int width = 1;
28099 Lisp_Object tem;
28101 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28102 FRAME_CURSOR_WIDTH (f) = width;
28104 /* By default, set up the blink-off state depending on the on-state. */
28106 tem = Fassoc (arg, Vblink_cursor_alist);
28107 if (!NILP (tem))
28109 FRAME_BLINK_OFF_CURSOR (f)
28110 = get_specified_cursor_type (XCDR (tem), &width);
28111 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28113 else
28114 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28116 /* Make sure the cursor gets redrawn. */
28117 f->cursor_type_changed = true;
28121 #ifdef HAVE_WINDOW_SYSTEM
28123 /* Return the cursor we want to be displayed in window W. Return
28124 width of bar/hbar cursor through WIDTH arg. Return with
28125 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28126 (i.e. if the `system caret' should track this cursor).
28128 In a mini-buffer window, we want the cursor only to appear if we
28129 are reading input from this window. For the selected window, we
28130 want the cursor type given by the frame parameter or buffer local
28131 setting of cursor-type. If explicitly marked off, draw no cursor.
28132 In all other cases, we want a hollow box cursor. */
28134 static enum text_cursor_kinds
28135 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28136 bool *active_cursor)
28138 struct frame *f = XFRAME (w->frame);
28139 struct buffer *b = XBUFFER (w->contents);
28140 int cursor_type = DEFAULT_CURSOR;
28141 Lisp_Object alt_cursor;
28142 bool non_selected = false;
28144 *active_cursor = true;
28146 /* Echo area */
28147 if (cursor_in_echo_area
28148 && FRAME_HAS_MINIBUF_P (f)
28149 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28151 if (w == XWINDOW (echo_area_window))
28153 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28155 *width = FRAME_CURSOR_WIDTH (f);
28156 return FRAME_DESIRED_CURSOR (f);
28158 else
28159 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28162 *active_cursor = false;
28163 non_selected = true;
28166 /* Detect a nonselected window or nonselected frame. */
28167 else if (w != XWINDOW (f->selected_window)
28168 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28170 *active_cursor = false;
28172 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28173 return NO_CURSOR;
28175 non_selected = true;
28178 /* Never display a cursor in a window in which cursor-type is nil. */
28179 if (NILP (BVAR (b, cursor_type)))
28180 return NO_CURSOR;
28182 /* Get the normal cursor type for this window. */
28183 if (EQ (BVAR (b, cursor_type), Qt))
28185 cursor_type = FRAME_DESIRED_CURSOR (f);
28186 *width = FRAME_CURSOR_WIDTH (f);
28188 else
28189 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28191 /* Use cursor-in-non-selected-windows instead
28192 for non-selected window or frame. */
28193 if (non_selected)
28195 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28196 if (!EQ (Qt, alt_cursor))
28197 return get_specified_cursor_type (alt_cursor, width);
28198 /* t means modify the normal cursor type. */
28199 if (cursor_type == FILLED_BOX_CURSOR)
28200 cursor_type = HOLLOW_BOX_CURSOR;
28201 else if (cursor_type == BAR_CURSOR && *width > 1)
28202 --*width;
28203 return cursor_type;
28206 /* Use normal cursor if not blinked off. */
28207 if (!w->cursor_off_p)
28209 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28210 return NO_CURSOR;
28211 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28213 if (cursor_type == FILLED_BOX_CURSOR)
28215 /* Using a block cursor on large images can be very annoying.
28216 So use a hollow cursor for "large" images.
28217 If image is not transparent (no mask), also use hollow cursor. */
28218 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28219 if (img != NULL && IMAGEP (img->spec))
28221 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28222 where N = size of default frame font size.
28223 This should cover most of the "tiny" icons people may use. */
28224 if (!img->mask
28225 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28226 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28227 cursor_type = HOLLOW_BOX_CURSOR;
28230 else if (cursor_type != NO_CURSOR)
28232 /* Display current only supports BOX and HOLLOW cursors for images.
28233 So for now, unconditionally use a HOLLOW cursor when cursor is
28234 not a solid box cursor. */
28235 cursor_type = HOLLOW_BOX_CURSOR;
28238 return cursor_type;
28241 /* Cursor is blinked off, so determine how to "toggle" it. */
28243 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28244 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28245 return get_specified_cursor_type (XCDR (alt_cursor), width);
28247 /* Then see if frame has specified a specific blink off cursor type. */
28248 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28250 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28251 return FRAME_BLINK_OFF_CURSOR (f);
28254 #if false
28255 /* Some people liked having a permanently visible blinking cursor,
28256 while others had very strong opinions against it. So it was
28257 decided to remove it. KFS 2003-09-03 */
28259 /* Finally perform built-in cursor blinking:
28260 filled box <-> hollow box
28261 wide [h]bar <-> narrow [h]bar
28262 narrow [h]bar <-> no cursor
28263 other type <-> no cursor */
28265 if (cursor_type == FILLED_BOX_CURSOR)
28266 return HOLLOW_BOX_CURSOR;
28268 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28270 *width = 1;
28271 return cursor_type;
28273 #endif
28275 return NO_CURSOR;
28279 /* Notice when the text cursor of window W has been completely
28280 overwritten by a drawing operation that outputs glyphs in AREA
28281 starting at X0 and ending at X1 in the line starting at Y0 and
28282 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28283 the rest of the line after X0 has been written. Y coordinates
28284 are window-relative. */
28286 static void
28287 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28288 int x0, int x1, int y0, int y1)
28290 int cx0, cx1, cy0, cy1;
28291 struct glyph_row *row;
28293 if (!w->phys_cursor_on_p)
28294 return;
28295 if (area != TEXT_AREA)
28296 return;
28298 if (w->phys_cursor.vpos < 0
28299 || w->phys_cursor.vpos >= w->current_matrix->nrows
28300 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28301 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28302 return;
28304 if (row->cursor_in_fringe_p)
28306 row->cursor_in_fringe_p = false;
28307 draw_fringe_bitmap (w, row, row->reversed_p);
28308 w->phys_cursor_on_p = false;
28309 return;
28312 cx0 = w->phys_cursor.x;
28313 cx1 = cx0 + w->phys_cursor_width;
28314 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28315 return;
28317 /* The cursor image will be completely removed from the
28318 screen if the output area intersects the cursor area in
28319 y-direction. When we draw in [y0 y1[, and some part of
28320 the cursor is at y < y0, that part must have been drawn
28321 before. When scrolling, the cursor is erased before
28322 actually scrolling, so we don't come here. When not
28323 scrolling, the rows above the old cursor row must have
28324 changed, and in this case these rows must have written
28325 over the cursor image.
28327 Likewise if part of the cursor is below y1, with the
28328 exception of the cursor being in the first blank row at
28329 the buffer and window end because update_text_area
28330 doesn't draw that row. (Except when it does, but
28331 that's handled in update_text_area.) */
28333 cy0 = w->phys_cursor.y;
28334 cy1 = cy0 + w->phys_cursor_height;
28335 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28336 return;
28338 w->phys_cursor_on_p = false;
28341 #endif /* HAVE_WINDOW_SYSTEM */
28344 /************************************************************************
28345 Mouse Face
28346 ************************************************************************/
28348 #ifdef HAVE_WINDOW_SYSTEM
28350 /* EXPORT for RIF:
28351 Fix the display of area AREA of overlapping row ROW in window W
28352 with respect to the overlapping part OVERLAPS. */
28354 void
28355 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28356 enum glyph_row_area area, int overlaps)
28358 int i, x;
28360 block_input ();
28362 x = 0;
28363 for (i = 0; i < row->used[area];)
28365 if (row->glyphs[area][i].overlaps_vertically_p)
28367 int start = i, start_x = x;
28371 x += row->glyphs[area][i].pixel_width;
28372 ++i;
28374 while (i < row->used[area]
28375 && row->glyphs[area][i].overlaps_vertically_p);
28377 draw_glyphs (w, start_x, row, area,
28378 start, i,
28379 DRAW_NORMAL_TEXT, overlaps);
28381 else
28383 x += row->glyphs[area][i].pixel_width;
28384 ++i;
28388 unblock_input ();
28392 /* EXPORT:
28393 Draw the cursor glyph of window W in glyph row ROW. See the
28394 comment of draw_glyphs for the meaning of HL. */
28396 void
28397 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28398 enum draw_glyphs_face hl)
28400 /* If cursor hpos is out of bounds, don't draw garbage. This can
28401 happen in mini-buffer windows when switching between echo area
28402 glyphs and mini-buffer. */
28403 if ((row->reversed_p
28404 ? (w->phys_cursor.hpos >= 0)
28405 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28407 bool on_p = w->phys_cursor_on_p;
28408 int x1;
28409 int hpos = w->phys_cursor.hpos;
28411 /* When the window is hscrolled, cursor hpos can legitimately be
28412 out of bounds, but we draw the cursor at the corresponding
28413 window margin in that case. */
28414 if (!row->reversed_p && hpos < 0)
28415 hpos = 0;
28416 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28417 hpos = row->used[TEXT_AREA] - 1;
28419 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28420 hl, 0);
28421 w->phys_cursor_on_p = on_p;
28423 if (hl == DRAW_CURSOR)
28424 w->phys_cursor_width = x1 - w->phys_cursor.x;
28425 /* When we erase the cursor, and ROW is overlapped by other
28426 rows, make sure that these overlapping parts of other rows
28427 are redrawn. */
28428 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28430 w->phys_cursor_width = x1 - w->phys_cursor.x;
28432 if (row > w->current_matrix->rows
28433 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28434 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28435 OVERLAPS_ERASED_CURSOR);
28437 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28438 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28439 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28440 OVERLAPS_ERASED_CURSOR);
28446 /* Erase the image of a cursor of window W from the screen. */
28448 void
28449 erase_phys_cursor (struct window *w)
28451 struct frame *f = XFRAME (w->frame);
28452 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28453 int hpos = w->phys_cursor.hpos;
28454 int vpos = w->phys_cursor.vpos;
28455 bool mouse_face_here_p = false;
28456 struct glyph_matrix *active_glyphs = w->current_matrix;
28457 struct glyph_row *cursor_row;
28458 struct glyph *cursor_glyph;
28459 enum draw_glyphs_face hl;
28461 /* No cursor displayed or row invalidated => nothing to do on the
28462 screen. */
28463 if (w->phys_cursor_type == NO_CURSOR)
28464 goto mark_cursor_off;
28466 /* VPOS >= active_glyphs->nrows means that window has been resized.
28467 Don't bother to erase the cursor. */
28468 if (vpos >= active_glyphs->nrows)
28469 goto mark_cursor_off;
28471 /* If row containing cursor is marked invalid, there is nothing we
28472 can do. */
28473 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28474 if (!cursor_row->enabled_p)
28475 goto mark_cursor_off;
28477 /* If line spacing is > 0, old cursor may only be partially visible in
28478 window after split-window. So adjust visible height. */
28479 cursor_row->visible_height = min (cursor_row->visible_height,
28480 window_text_bottom_y (w) - cursor_row->y);
28482 /* If row is completely invisible, don't attempt to delete a cursor which
28483 isn't there. This can happen if cursor is at top of a window, and
28484 we switch to a buffer with a header line in that window. */
28485 if (cursor_row->visible_height <= 0)
28486 goto mark_cursor_off;
28488 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28489 if (cursor_row->cursor_in_fringe_p)
28491 cursor_row->cursor_in_fringe_p = false;
28492 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28493 goto mark_cursor_off;
28496 /* This can happen when the new row is shorter than the old one.
28497 In this case, either draw_glyphs or clear_end_of_line
28498 should have cleared the cursor. Note that we wouldn't be
28499 able to erase the cursor in this case because we don't have a
28500 cursor glyph at hand. */
28501 if ((cursor_row->reversed_p
28502 ? (w->phys_cursor.hpos < 0)
28503 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28504 goto mark_cursor_off;
28506 /* When the window is hscrolled, cursor hpos can legitimately be out
28507 of bounds, but we draw the cursor at the corresponding window
28508 margin in that case. */
28509 if (!cursor_row->reversed_p && hpos < 0)
28510 hpos = 0;
28511 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28512 hpos = cursor_row->used[TEXT_AREA] - 1;
28514 /* If the cursor is in the mouse face area, redisplay that when
28515 we clear the cursor. */
28516 if (! NILP (hlinfo->mouse_face_window)
28517 && coords_in_mouse_face_p (w, hpos, vpos)
28518 /* Don't redraw the cursor's spot in mouse face if it is at the
28519 end of a line (on a newline). The cursor appears there, but
28520 mouse highlighting does not. */
28521 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28522 mouse_face_here_p = true;
28524 /* Maybe clear the display under the cursor. */
28525 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28527 int x, y;
28528 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28529 int width;
28531 cursor_glyph = get_phys_cursor_glyph (w);
28532 if (cursor_glyph == NULL)
28533 goto mark_cursor_off;
28535 width = cursor_glyph->pixel_width;
28536 x = w->phys_cursor.x;
28537 if (x < 0)
28539 width += x;
28540 x = 0;
28542 width = min (width, window_box_width (w, TEXT_AREA) - x);
28543 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28544 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28546 if (width > 0)
28547 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28550 /* Erase the cursor by redrawing the character underneath it. */
28551 if (mouse_face_here_p)
28552 hl = DRAW_MOUSE_FACE;
28553 else
28554 hl = DRAW_NORMAL_TEXT;
28555 draw_phys_cursor_glyph (w, cursor_row, hl);
28557 mark_cursor_off:
28558 w->phys_cursor_on_p = false;
28559 w->phys_cursor_type = NO_CURSOR;
28563 /* Display or clear cursor of window W. If !ON, clear the cursor.
28564 If ON, display the cursor; where to put the cursor is specified by
28565 HPOS, VPOS, X and Y. */
28567 void
28568 display_and_set_cursor (struct window *w, bool on,
28569 int hpos, int vpos, int x, int y)
28571 struct frame *f = XFRAME (w->frame);
28572 int new_cursor_type;
28573 int new_cursor_width;
28574 bool active_cursor;
28575 struct glyph_row *glyph_row;
28576 struct glyph *glyph;
28578 /* This is pointless on invisible frames, and dangerous on garbaged
28579 windows and frames; in the latter case, the frame or window may
28580 be in the midst of changing its size, and x and y may be off the
28581 window. */
28582 if (! FRAME_VISIBLE_P (f)
28583 || FRAME_GARBAGED_P (f)
28584 || vpos >= w->current_matrix->nrows
28585 || hpos >= w->current_matrix->matrix_w)
28586 return;
28588 /* If cursor is off and we want it off, return quickly. */
28589 if (!on && !w->phys_cursor_on_p)
28590 return;
28592 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28593 /* If cursor row is not enabled, we don't really know where to
28594 display the cursor. */
28595 if (!glyph_row->enabled_p)
28597 w->phys_cursor_on_p = false;
28598 return;
28601 glyph = NULL;
28602 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28603 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28605 eassert (input_blocked_p ());
28607 /* Set new_cursor_type to the cursor we want to be displayed. */
28608 new_cursor_type = get_window_cursor_type (w, glyph,
28609 &new_cursor_width, &active_cursor);
28611 /* If cursor is currently being shown and we don't want it to be or
28612 it is in the wrong place, or the cursor type is not what we want,
28613 erase it. */
28614 if (w->phys_cursor_on_p
28615 && (!on
28616 || w->phys_cursor.x != x
28617 || w->phys_cursor.y != y
28618 /* HPOS can be negative in R2L rows whose
28619 exact_window_width_line_p flag is set (i.e. their newline
28620 would "overflow into the fringe"). */
28621 || hpos < 0
28622 || new_cursor_type != w->phys_cursor_type
28623 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28624 && new_cursor_width != w->phys_cursor_width)))
28625 erase_phys_cursor (w);
28627 /* Don't check phys_cursor_on_p here because that flag is only set
28628 to false in some cases where we know that the cursor has been
28629 completely erased, to avoid the extra work of erasing the cursor
28630 twice. In other words, phys_cursor_on_p can be true and the cursor
28631 still not be visible, or it has only been partly erased. */
28632 if (on)
28634 w->phys_cursor_ascent = glyph_row->ascent;
28635 w->phys_cursor_height = glyph_row->height;
28637 /* Set phys_cursor_.* before x_draw_.* is called because some
28638 of them may need the information. */
28639 w->phys_cursor.x = x;
28640 w->phys_cursor.y = glyph_row->y;
28641 w->phys_cursor.hpos = hpos;
28642 w->phys_cursor.vpos = vpos;
28645 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28646 new_cursor_type, new_cursor_width,
28647 on, active_cursor);
28651 /* Switch the display of W's cursor on or off, according to the value
28652 of ON. */
28654 static void
28655 update_window_cursor (struct window *w, bool on)
28657 /* Don't update cursor in windows whose frame is in the process
28658 of being deleted. */
28659 if (w->current_matrix)
28661 int hpos = w->phys_cursor.hpos;
28662 int vpos = w->phys_cursor.vpos;
28663 struct glyph_row *row;
28665 if (vpos >= w->current_matrix->nrows
28666 || hpos >= w->current_matrix->matrix_w)
28667 return;
28669 row = MATRIX_ROW (w->current_matrix, vpos);
28671 /* When the window is hscrolled, cursor hpos can legitimately be
28672 out of bounds, but we draw the cursor at the corresponding
28673 window margin in that case. */
28674 if (!row->reversed_p && hpos < 0)
28675 hpos = 0;
28676 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28677 hpos = row->used[TEXT_AREA] - 1;
28679 block_input ();
28680 display_and_set_cursor (w, on, hpos, vpos,
28681 w->phys_cursor.x, w->phys_cursor.y);
28682 unblock_input ();
28687 /* Call update_window_cursor with parameter ON_P on all leaf windows
28688 in the window tree rooted at W. */
28690 static void
28691 update_cursor_in_window_tree (struct window *w, bool on_p)
28693 while (w)
28695 if (WINDOWP (w->contents))
28696 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28697 else
28698 update_window_cursor (w, on_p);
28700 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28705 /* EXPORT:
28706 Display the cursor on window W, or clear it, according to ON_P.
28707 Don't change the cursor's position. */
28709 void
28710 x_update_cursor (struct frame *f, bool on_p)
28712 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28716 /* EXPORT:
28717 Clear the cursor of window W to background color, and mark the
28718 cursor as not shown. This is used when the text where the cursor
28719 is about to be rewritten. */
28721 void
28722 x_clear_cursor (struct window *w)
28724 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28725 update_window_cursor (w, false);
28728 #endif /* HAVE_WINDOW_SYSTEM */
28730 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28731 and MSDOS. */
28732 static void
28733 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28734 int start_hpos, int end_hpos,
28735 enum draw_glyphs_face draw)
28737 #ifdef HAVE_WINDOW_SYSTEM
28738 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28740 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28741 return;
28743 #endif
28744 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28745 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28746 #endif
28749 /* Display the active region described by mouse_face_* according to DRAW. */
28751 static void
28752 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28754 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28755 struct frame *f = XFRAME (WINDOW_FRAME (w));
28757 if (/* If window is in the process of being destroyed, don't bother
28758 to do anything. */
28759 w->current_matrix != NULL
28760 /* Don't update mouse highlight if hidden. */
28761 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28762 /* Recognize when we are called to operate on rows that don't exist
28763 anymore. This can happen when a window is split. */
28764 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28766 bool phys_cursor_on_p = w->phys_cursor_on_p;
28767 struct glyph_row *row, *first, *last;
28769 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28770 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28772 for (row = first; row <= last && row->enabled_p; ++row)
28774 int start_hpos, end_hpos, start_x;
28776 /* For all but the first row, the highlight starts at column 0. */
28777 if (row == first)
28779 /* R2L rows have BEG and END in reversed order, but the
28780 screen drawing geometry is always left to right. So
28781 we need to mirror the beginning and end of the
28782 highlighted area in R2L rows. */
28783 if (!row->reversed_p)
28785 start_hpos = hlinfo->mouse_face_beg_col;
28786 start_x = hlinfo->mouse_face_beg_x;
28788 else if (row == last)
28790 start_hpos = hlinfo->mouse_face_end_col;
28791 start_x = hlinfo->mouse_face_end_x;
28793 else
28795 start_hpos = 0;
28796 start_x = 0;
28799 else if (row->reversed_p && row == last)
28801 start_hpos = hlinfo->mouse_face_end_col;
28802 start_x = hlinfo->mouse_face_end_x;
28804 else
28806 start_hpos = 0;
28807 start_x = 0;
28810 if (row == last)
28812 if (!row->reversed_p)
28813 end_hpos = hlinfo->mouse_face_end_col;
28814 else if (row == first)
28815 end_hpos = hlinfo->mouse_face_beg_col;
28816 else
28818 end_hpos = row->used[TEXT_AREA];
28819 if (draw == DRAW_NORMAL_TEXT)
28820 row->fill_line_p = true; /* Clear to end of line. */
28823 else if (row->reversed_p && row == first)
28824 end_hpos = hlinfo->mouse_face_beg_col;
28825 else
28827 end_hpos = row->used[TEXT_AREA];
28828 if (draw == DRAW_NORMAL_TEXT)
28829 row->fill_line_p = true; /* Clear to end of line. */
28832 if (end_hpos > start_hpos)
28834 draw_row_with_mouse_face (w, start_x, row,
28835 start_hpos, end_hpos, draw);
28837 row->mouse_face_p
28838 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28842 /* When we've written over the cursor, arrange for it to
28843 be displayed again. */
28844 if (FRAME_WINDOW_P (f)
28845 && phys_cursor_on_p && !w->phys_cursor_on_p)
28847 #ifdef HAVE_WINDOW_SYSTEM
28848 int hpos = w->phys_cursor.hpos;
28850 /* When the window is hscrolled, cursor hpos can legitimately be
28851 out of bounds, but we draw the cursor at the corresponding
28852 window margin in that case. */
28853 if (!row->reversed_p && hpos < 0)
28854 hpos = 0;
28855 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28856 hpos = row->used[TEXT_AREA] - 1;
28858 block_input ();
28859 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28860 w->phys_cursor.x, w->phys_cursor.y);
28861 unblock_input ();
28862 #endif /* HAVE_WINDOW_SYSTEM */
28866 #ifdef HAVE_WINDOW_SYSTEM
28867 /* Change the mouse cursor. */
28868 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28870 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28871 if (draw == DRAW_NORMAL_TEXT
28872 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28873 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28874 else
28875 #endif
28876 if (draw == DRAW_MOUSE_FACE)
28877 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28878 else
28879 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28881 #endif /* HAVE_WINDOW_SYSTEM */
28884 /* EXPORT:
28885 Clear out the mouse-highlighted active region.
28886 Redraw it un-highlighted first. Value is true if mouse
28887 face was actually drawn unhighlighted. */
28889 bool
28890 clear_mouse_face (Mouse_HLInfo *hlinfo)
28892 bool cleared
28893 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28894 if (cleared)
28895 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28896 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28897 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28898 hlinfo->mouse_face_window = Qnil;
28899 hlinfo->mouse_face_overlay = Qnil;
28900 return cleared;
28903 /* Return true if the coordinates HPOS and VPOS on windows W are
28904 within the mouse face on that window. */
28905 static bool
28906 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28910 /* Quickly resolve the easy cases. */
28911 if (!(WINDOWP (hlinfo->mouse_face_window)
28912 && XWINDOW (hlinfo->mouse_face_window) == w))
28913 return false;
28914 if (vpos < hlinfo->mouse_face_beg_row
28915 || vpos > hlinfo->mouse_face_end_row)
28916 return false;
28917 if (vpos > hlinfo->mouse_face_beg_row
28918 && vpos < hlinfo->mouse_face_end_row)
28919 return true;
28921 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28923 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28925 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28926 return true;
28928 else if ((vpos == hlinfo->mouse_face_beg_row
28929 && hpos >= hlinfo->mouse_face_beg_col)
28930 || (vpos == hlinfo->mouse_face_end_row
28931 && hpos < hlinfo->mouse_face_end_col))
28932 return true;
28934 else
28936 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28938 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28939 return true;
28941 else if ((vpos == hlinfo->mouse_face_beg_row
28942 && hpos <= hlinfo->mouse_face_beg_col)
28943 || (vpos == hlinfo->mouse_face_end_row
28944 && hpos > hlinfo->mouse_face_end_col))
28945 return true;
28947 return false;
28951 /* EXPORT:
28952 True if physical cursor of window W is within mouse face. */
28954 bool
28955 cursor_in_mouse_face_p (struct window *w)
28957 int hpos = w->phys_cursor.hpos;
28958 int vpos = w->phys_cursor.vpos;
28959 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28961 /* When the window is hscrolled, cursor hpos can legitimately be out
28962 of bounds, but we draw the cursor at the corresponding window
28963 margin in that case. */
28964 if (!row->reversed_p && hpos < 0)
28965 hpos = 0;
28966 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28967 hpos = row->used[TEXT_AREA] - 1;
28969 return coords_in_mouse_face_p (w, hpos, vpos);
28974 /* Find the glyph rows START_ROW and END_ROW of window W that display
28975 characters between buffer positions START_CHARPOS and END_CHARPOS
28976 (excluding END_CHARPOS). DISP_STRING is a display string that
28977 covers these buffer positions. This is similar to
28978 row_containing_pos, but is more accurate when bidi reordering makes
28979 buffer positions change non-linearly with glyph rows. */
28980 static void
28981 rows_from_pos_range (struct window *w,
28982 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28983 Lisp_Object disp_string,
28984 struct glyph_row **start, struct glyph_row **end)
28986 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28987 int last_y = window_text_bottom_y (w);
28988 struct glyph_row *row;
28990 *start = NULL;
28991 *end = NULL;
28993 while (!first->enabled_p
28994 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28995 first++;
28997 /* Find the START row. */
28998 for (row = first;
28999 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29000 row++)
29002 /* A row can potentially be the START row if the range of the
29003 characters it displays intersects the range
29004 [START_CHARPOS..END_CHARPOS). */
29005 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29006 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29007 /* See the commentary in row_containing_pos, for the
29008 explanation of the complicated way to check whether
29009 some position is beyond the end of the characters
29010 displayed by a row. */
29011 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29012 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29013 && !row->ends_at_zv_p
29014 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29015 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29016 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29017 && !row->ends_at_zv_p
29018 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29020 /* Found a candidate row. Now make sure at least one of the
29021 glyphs it displays has a charpos from the range
29022 [START_CHARPOS..END_CHARPOS).
29024 This is not obvious because bidi reordering could make
29025 buffer positions of a row be 1,2,3,102,101,100, and if we
29026 want to highlight characters in [50..60), we don't want
29027 this row, even though [50..60) does intersect [1..103),
29028 the range of character positions given by the row's start
29029 and end positions. */
29030 struct glyph *g = row->glyphs[TEXT_AREA];
29031 struct glyph *e = g + row->used[TEXT_AREA];
29033 while (g < e)
29035 if (((BUFFERP (g->object) || NILP (g->object))
29036 && start_charpos <= g->charpos && g->charpos < end_charpos)
29037 /* A glyph that comes from DISP_STRING is by
29038 definition to be highlighted. */
29039 || EQ (g->object, disp_string))
29040 *start = row;
29041 g++;
29043 if (*start)
29044 break;
29048 /* Find the END row. */
29049 if (!*start
29050 /* If the last row is partially visible, start looking for END
29051 from that row, instead of starting from FIRST. */
29052 && !(row->enabled_p
29053 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29054 row = first;
29055 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29057 struct glyph_row *next = row + 1;
29058 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29060 if (!next->enabled_p
29061 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29062 /* The first row >= START whose range of displayed characters
29063 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29064 is the row END + 1. */
29065 || (start_charpos < next_start
29066 && end_charpos < next_start)
29067 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29068 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29069 && !next->ends_at_zv_p
29070 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29071 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29072 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29073 && !next->ends_at_zv_p
29074 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29076 *end = row;
29077 break;
29079 else
29081 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29082 but none of the characters it displays are in the range, it is
29083 also END + 1. */
29084 struct glyph *g = next->glyphs[TEXT_AREA];
29085 struct glyph *s = g;
29086 struct glyph *e = g + next->used[TEXT_AREA];
29088 while (g < e)
29090 if (((BUFFERP (g->object) || NILP (g->object))
29091 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29092 /* If the buffer position of the first glyph in
29093 the row is equal to END_CHARPOS, it means
29094 the last character to be highlighted is the
29095 newline of ROW, and we must consider NEXT as
29096 END, not END+1. */
29097 || (((!next->reversed_p && g == s)
29098 || (next->reversed_p && g == e - 1))
29099 && (g->charpos == end_charpos
29100 /* Special case for when NEXT is an
29101 empty line at ZV. */
29102 || (g->charpos == -1
29103 && !row->ends_at_zv_p
29104 && next_start == end_charpos)))))
29105 /* A glyph that comes from DISP_STRING is by
29106 definition to be highlighted. */
29107 || EQ (g->object, disp_string))
29108 break;
29109 g++;
29111 if (g == e)
29113 *end = row;
29114 break;
29116 /* The first row that ends at ZV must be the last to be
29117 highlighted. */
29118 else if (next->ends_at_zv_p)
29120 *end = next;
29121 break;
29127 /* This function sets the mouse_face_* elements of HLINFO, assuming
29128 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29129 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29130 for the overlay or run of text properties specifying the mouse
29131 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29132 before-string and after-string that must also be highlighted.
29133 DISP_STRING, if non-nil, is a display string that may cover some
29134 or all of the highlighted text. */
29136 static void
29137 mouse_face_from_buffer_pos (Lisp_Object window,
29138 Mouse_HLInfo *hlinfo,
29139 ptrdiff_t mouse_charpos,
29140 ptrdiff_t start_charpos,
29141 ptrdiff_t end_charpos,
29142 Lisp_Object before_string,
29143 Lisp_Object after_string,
29144 Lisp_Object disp_string)
29146 struct window *w = XWINDOW (window);
29147 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29148 struct glyph_row *r1, *r2;
29149 struct glyph *glyph, *end;
29150 ptrdiff_t ignore, pos;
29151 int x;
29153 eassert (NILP (disp_string) || STRINGP (disp_string));
29154 eassert (NILP (before_string) || STRINGP (before_string));
29155 eassert (NILP (after_string) || STRINGP (after_string));
29157 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29158 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29159 if (r1 == NULL)
29160 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29161 /* If the before-string or display-string contains newlines,
29162 rows_from_pos_range skips to its last row. Move back. */
29163 if (!NILP (before_string) || !NILP (disp_string))
29165 struct glyph_row *prev;
29166 while ((prev = r1 - 1, prev >= first)
29167 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29168 && prev->used[TEXT_AREA] > 0)
29170 struct glyph *beg = prev->glyphs[TEXT_AREA];
29171 glyph = beg + prev->used[TEXT_AREA];
29172 while (--glyph >= beg && NILP (glyph->object));
29173 if (glyph < beg
29174 || !(EQ (glyph->object, before_string)
29175 || EQ (glyph->object, disp_string)))
29176 break;
29177 r1 = prev;
29180 if (r2 == NULL)
29182 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29183 hlinfo->mouse_face_past_end = true;
29185 else if (!NILP (after_string))
29187 /* If the after-string has newlines, advance to its last row. */
29188 struct glyph_row *next;
29189 struct glyph_row *last
29190 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29192 for (next = r2 + 1;
29193 next <= last
29194 && next->used[TEXT_AREA] > 0
29195 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29196 ++next)
29197 r2 = next;
29199 /* The rest of the display engine assumes that mouse_face_beg_row is
29200 either above mouse_face_end_row or identical to it. But with
29201 bidi-reordered continued lines, the row for START_CHARPOS could
29202 be below the row for END_CHARPOS. If so, swap the rows and store
29203 them in correct order. */
29204 if (r1->y > r2->y)
29206 struct glyph_row *tem = r2;
29208 r2 = r1;
29209 r1 = tem;
29212 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29213 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29215 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29216 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29217 could be anywhere in the row and in any order. The strategy
29218 below is to find the leftmost and the rightmost glyph that
29219 belongs to either of these 3 strings, or whose position is
29220 between START_CHARPOS and END_CHARPOS, and highlight all the
29221 glyphs between those two. This may cover more than just the text
29222 between START_CHARPOS and END_CHARPOS if the range of characters
29223 strides the bidi level boundary, e.g. if the beginning is in R2L
29224 text while the end is in L2R text or vice versa. */
29225 if (!r1->reversed_p)
29227 /* This row is in a left to right paragraph. Scan it left to
29228 right. */
29229 glyph = r1->glyphs[TEXT_AREA];
29230 end = glyph + r1->used[TEXT_AREA];
29231 x = r1->x;
29233 /* Skip truncation glyphs at the start of the glyph row. */
29234 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29235 for (; glyph < end
29236 && NILP (glyph->object)
29237 && glyph->charpos < 0;
29238 ++glyph)
29239 x += glyph->pixel_width;
29241 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29242 or DISP_STRING, and the first glyph from buffer whose
29243 position is between START_CHARPOS and END_CHARPOS. */
29244 for (; glyph < end
29245 && !NILP (glyph->object)
29246 && !EQ (glyph->object, disp_string)
29247 && !(BUFFERP (glyph->object)
29248 && (glyph->charpos >= start_charpos
29249 && glyph->charpos < end_charpos));
29250 ++glyph)
29252 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29253 are present at buffer positions between START_CHARPOS and
29254 END_CHARPOS, or if they come from an overlay. */
29255 if (EQ (glyph->object, before_string))
29257 pos = string_buffer_position (before_string,
29258 start_charpos);
29259 /* If pos == 0, it means before_string came from an
29260 overlay, not from a buffer position. */
29261 if (!pos || (pos >= start_charpos && pos < end_charpos))
29262 break;
29264 else if (EQ (glyph->object, after_string))
29266 pos = string_buffer_position (after_string, end_charpos);
29267 if (!pos || (pos >= start_charpos && pos < end_charpos))
29268 break;
29270 x += glyph->pixel_width;
29272 hlinfo->mouse_face_beg_x = x;
29273 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29275 else
29277 /* This row is in a right to left paragraph. Scan it right to
29278 left. */
29279 struct glyph *g;
29281 end = r1->glyphs[TEXT_AREA] - 1;
29282 glyph = end + r1->used[TEXT_AREA];
29284 /* Skip truncation glyphs at the start of the glyph row. */
29285 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29286 for (; glyph > end
29287 && NILP (glyph->object)
29288 && glyph->charpos < 0;
29289 --glyph)
29292 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29293 or DISP_STRING, and the first glyph from buffer whose
29294 position is between START_CHARPOS and END_CHARPOS. */
29295 for (; glyph > end
29296 && !NILP (glyph->object)
29297 && !EQ (glyph->object, disp_string)
29298 && !(BUFFERP (glyph->object)
29299 && (glyph->charpos >= start_charpos
29300 && glyph->charpos < end_charpos));
29301 --glyph)
29303 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29304 are present at buffer positions between START_CHARPOS and
29305 END_CHARPOS, or if they come from an overlay. */
29306 if (EQ (glyph->object, before_string))
29308 pos = string_buffer_position (before_string, start_charpos);
29309 /* If pos == 0, it means before_string came from an
29310 overlay, not from a buffer position. */
29311 if (!pos || (pos >= start_charpos && pos < end_charpos))
29312 break;
29314 else if (EQ (glyph->object, after_string))
29316 pos = string_buffer_position (after_string, end_charpos);
29317 if (!pos || (pos >= start_charpos && pos < end_charpos))
29318 break;
29322 glyph++; /* first glyph to the right of the highlighted area */
29323 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29324 x += g->pixel_width;
29325 hlinfo->mouse_face_beg_x = x;
29326 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29329 /* If the highlight ends in a different row, compute GLYPH and END
29330 for the end row. Otherwise, reuse the values computed above for
29331 the row where the highlight begins. */
29332 if (r2 != r1)
29334 if (!r2->reversed_p)
29336 glyph = r2->glyphs[TEXT_AREA];
29337 end = glyph + r2->used[TEXT_AREA];
29338 x = r2->x;
29340 else
29342 end = r2->glyphs[TEXT_AREA] - 1;
29343 glyph = end + r2->used[TEXT_AREA];
29347 if (!r2->reversed_p)
29349 /* Skip truncation and continuation glyphs near the end of the
29350 row, and also blanks and stretch glyphs inserted by
29351 extend_face_to_end_of_line. */
29352 while (end > glyph
29353 && NILP ((end - 1)->object))
29354 --end;
29355 /* Scan the rest of the glyph row from the end, looking for the
29356 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29357 DISP_STRING, or whose position is between START_CHARPOS
29358 and END_CHARPOS */
29359 for (--end;
29360 end > glyph
29361 && !NILP (end->object)
29362 && !EQ (end->object, disp_string)
29363 && !(BUFFERP (end->object)
29364 && (end->charpos >= start_charpos
29365 && end->charpos < end_charpos));
29366 --end)
29368 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29369 are present at buffer positions between START_CHARPOS and
29370 END_CHARPOS, or if they come from an overlay. */
29371 if (EQ (end->object, before_string))
29373 pos = string_buffer_position (before_string, start_charpos);
29374 if (!pos || (pos >= start_charpos && pos < end_charpos))
29375 break;
29377 else if (EQ (end->object, after_string))
29379 pos = string_buffer_position (after_string, end_charpos);
29380 if (!pos || (pos >= start_charpos && pos < end_charpos))
29381 break;
29384 /* Find the X coordinate of the last glyph to be highlighted. */
29385 for (; glyph <= end; ++glyph)
29386 x += glyph->pixel_width;
29388 hlinfo->mouse_face_end_x = x;
29389 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29391 else
29393 /* Skip truncation and continuation glyphs near the end of the
29394 row, and also blanks and stretch glyphs inserted by
29395 extend_face_to_end_of_line. */
29396 x = r2->x;
29397 end++;
29398 while (end < glyph
29399 && NILP (end->object))
29401 x += end->pixel_width;
29402 ++end;
29404 /* Scan the rest of the glyph row from the end, looking for the
29405 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29406 DISP_STRING, or whose position is between START_CHARPOS
29407 and END_CHARPOS */
29408 for ( ;
29409 end < glyph
29410 && !NILP (end->object)
29411 && !EQ (end->object, disp_string)
29412 && !(BUFFERP (end->object)
29413 && (end->charpos >= start_charpos
29414 && end->charpos < end_charpos));
29415 ++end)
29417 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29418 are present at buffer positions between START_CHARPOS and
29419 END_CHARPOS, or if they come from an overlay. */
29420 if (EQ (end->object, before_string))
29422 pos = string_buffer_position (before_string, start_charpos);
29423 if (!pos || (pos >= start_charpos && pos < end_charpos))
29424 break;
29426 else if (EQ (end->object, after_string))
29428 pos = string_buffer_position (after_string, end_charpos);
29429 if (!pos || (pos >= start_charpos && pos < end_charpos))
29430 break;
29432 x += end->pixel_width;
29434 /* If we exited the above loop because we arrived at the last
29435 glyph of the row, and its buffer position is still not in
29436 range, it means the last character in range is the preceding
29437 newline. Bump the end column and x values to get past the
29438 last glyph. */
29439 if (end == glyph
29440 && BUFFERP (end->object)
29441 && (end->charpos < start_charpos
29442 || end->charpos >= end_charpos))
29444 x += end->pixel_width;
29445 ++end;
29447 hlinfo->mouse_face_end_x = x;
29448 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29451 hlinfo->mouse_face_window = window;
29452 hlinfo->mouse_face_face_id
29453 = face_at_buffer_position (w, mouse_charpos, &ignore,
29454 mouse_charpos + 1,
29455 !hlinfo->mouse_face_hidden, -1);
29456 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29459 /* The following function is not used anymore (replaced with
29460 mouse_face_from_string_pos), but I leave it here for the time
29461 being, in case someone would. */
29463 #if false /* not used */
29465 /* Find the position of the glyph for position POS in OBJECT in
29466 window W's current matrix, and return in *X, *Y the pixel
29467 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29469 RIGHT_P means return the position of the right edge of the glyph.
29470 !RIGHT_P means return the left edge position.
29472 If no glyph for POS exists in the matrix, return the position of
29473 the glyph with the next smaller position that is in the matrix, if
29474 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29475 exists in the matrix, return the position of the glyph with the
29476 next larger position in OBJECT.
29478 Value is true if a glyph was found. */
29480 static bool
29481 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29482 int *hpos, int *vpos, int *x, int *y, bool right_p)
29484 int yb = window_text_bottom_y (w);
29485 struct glyph_row *r;
29486 struct glyph *best_glyph = NULL;
29487 struct glyph_row *best_row = NULL;
29488 int best_x = 0;
29490 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29491 r->enabled_p && r->y < yb;
29492 ++r)
29494 struct glyph *g = r->glyphs[TEXT_AREA];
29495 struct glyph *e = g + r->used[TEXT_AREA];
29496 int gx;
29498 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29499 if (EQ (g->object, object))
29501 if (g->charpos == pos)
29503 best_glyph = g;
29504 best_x = gx;
29505 best_row = r;
29506 goto found;
29508 else if (best_glyph == NULL
29509 || ((eabs (g->charpos - pos)
29510 < eabs (best_glyph->charpos - pos))
29511 && (right_p
29512 ? g->charpos < pos
29513 : g->charpos > pos)))
29515 best_glyph = g;
29516 best_x = gx;
29517 best_row = r;
29522 found:
29524 if (best_glyph)
29526 *x = best_x;
29527 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29529 if (right_p)
29531 *x += best_glyph->pixel_width;
29532 ++*hpos;
29535 *y = best_row->y;
29536 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29539 return best_glyph != NULL;
29541 #endif /* not used */
29543 /* Find the positions of the first and the last glyphs in window W's
29544 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29545 (assumed to be a string), and return in HLINFO's mouse_face_*
29546 members the pixel and column/row coordinates of those glyphs. */
29548 static void
29549 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29550 Lisp_Object object,
29551 ptrdiff_t startpos, ptrdiff_t endpos)
29553 int yb = window_text_bottom_y (w);
29554 struct glyph_row *r;
29555 struct glyph *g, *e;
29556 int gx;
29557 bool found = false;
29559 /* Find the glyph row with at least one position in the range
29560 [STARTPOS..ENDPOS), and the first glyph in that row whose
29561 position belongs to that range. */
29562 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29563 r->enabled_p && r->y < yb;
29564 ++r)
29566 if (!r->reversed_p)
29568 g = r->glyphs[TEXT_AREA];
29569 e = g + r->used[TEXT_AREA];
29570 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29571 if (EQ (g->object, object)
29572 && startpos <= g->charpos && g->charpos < endpos)
29574 hlinfo->mouse_face_beg_row
29575 = MATRIX_ROW_VPOS (r, w->current_matrix);
29576 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29577 hlinfo->mouse_face_beg_x = gx;
29578 found = true;
29579 break;
29582 else
29584 struct glyph *g1;
29586 e = r->glyphs[TEXT_AREA];
29587 g = e + r->used[TEXT_AREA];
29588 for ( ; g > e; --g)
29589 if (EQ ((g-1)->object, object)
29590 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29592 hlinfo->mouse_face_beg_row
29593 = MATRIX_ROW_VPOS (r, w->current_matrix);
29594 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29595 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29596 gx += g1->pixel_width;
29597 hlinfo->mouse_face_beg_x = gx;
29598 found = true;
29599 break;
29602 if (found)
29603 break;
29606 if (!found)
29607 return;
29609 /* Starting with the next row, look for the first row which does NOT
29610 include any glyphs whose positions are in the range. */
29611 for (++r; r->enabled_p && r->y < yb; ++r)
29613 g = r->glyphs[TEXT_AREA];
29614 e = g + r->used[TEXT_AREA];
29615 found = false;
29616 for ( ; g < e; ++g)
29617 if (EQ (g->object, object)
29618 && startpos <= g->charpos && g->charpos < endpos)
29620 found = true;
29621 break;
29623 if (!found)
29624 break;
29627 /* The highlighted region ends on the previous row. */
29628 r--;
29630 /* Set the end row. */
29631 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29633 /* Compute and set the end column and the end column's horizontal
29634 pixel coordinate. */
29635 if (!r->reversed_p)
29637 g = r->glyphs[TEXT_AREA];
29638 e = g + r->used[TEXT_AREA];
29639 for ( ; e > g; --e)
29640 if (EQ ((e-1)->object, object)
29641 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29642 break;
29643 hlinfo->mouse_face_end_col = e - g;
29645 for (gx = r->x; g < e; ++g)
29646 gx += g->pixel_width;
29647 hlinfo->mouse_face_end_x = gx;
29649 else
29651 e = r->glyphs[TEXT_AREA];
29652 g = e + r->used[TEXT_AREA];
29653 for (gx = r->x ; e < g; ++e)
29655 if (EQ (e->object, object)
29656 && startpos <= e->charpos && e->charpos < endpos)
29657 break;
29658 gx += e->pixel_width;
29660 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29661 hlinfo->mouse_face_end_x = gx;
29665 #ifdef HAVE_WINDOW_SYSTEM
29667 /* See if position X, Y is within a hot-spot of an image. */
29669 static bool
29670 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29672 if (!CONSP (hot_spot))
29673 return false;
29675 if (EQ (XCAR (hot_spot), Qrect))
29677 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29678 Lisp_Object rect = XCDR (hot_spot);
29679 Lisp_Object tem;
29680 if (!CONSP (rect))
29681 return false;
29682 if (!CONSP (XCAR (rect)))
29683 return false;
29684 if (!CONSP (XCDR (rect)))
29685 return false;
29686 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29687 return false;
29688 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29689 return false;
29690 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29691 return false;
29692 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29693 return false;
29694 return true;
29696 else if (EQ (XCAR (hot_spot), Qcircle))
29698 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29699 Lisp_Object circ = XCDR (hot_spot);
29700 Lisp_Object lr, lx0, ly0;
29701 if (CONSP (circ)
29702 && CONSP (XCAR (circ))
29703 && (lr = XCDR (circ), NUMBERP (lr))
29704 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29705 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29707 double r = XFLOATINT (lr);
29708 double dx = XINT (lx0) - x;
29709 double dy = XINT (ly0) - y;
29710 return (dx * dx + dy * dy <= r * r);
29713 else if (EQ (XCAR (hot_spot), Qpoly))
29715 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29716 if (VECTORP (XCDR (hot_spot)))
29718 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29719 Lisp_Object *poly = v->contents;
29720 ptrdiff_t n = v->header.size;
29721 ptrdiff_t i;
29722 bool inside = false;
29723 Lisp_Object lx, ly;
29724 int x0, y0;
29726 /* Need an even number of coordinates, and at least 3 edges. */
29727 if (n < 6 || n & 1)
29728 return false;
29730 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29731 If count is odd, we are inside polygon. Pixels on edges
29732 may or may not be included depending on actual geometry of the
29733 polygon. */
29734 if ((lx = poly[n-2], !INTEGERP (lx))
29735 || (ly = poly[n-1], !INTEGERP (lx)))
29736 return false;
29737 x0 = XINT (lx), y0 = XINT (ly);
29738 for (i = 0; i < n; i += 2)
29740 int x1 = x0, y1 = y0;
29741 if ((lx = poly[i], !INTEGERP (lx))
29742 || (ly = poly[i+1], !INTEGERP (ly)))
29743 return false;
29744 x0 = XINT (lx), y0 = XINT (ly);
29746 /* Does this segment cross the X line? */
29747 if (x0 >= x)
29749 if (x1 >= x)
29750 continue;
29752 else if (x1 < x)
29753 continue;
29754 if (y > y0 && y > y1)
29755 continue;
29756 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29757 inside = !inside;
29759 return inside;
29762 return false;
29765 Lisp_Object
29766 find_hot_spot (Lisp_Object map, int x, int y)
29768 while (CONSP (map))
29770 if (CONSP (XCAR (map))
29771 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29772 return XCAR (map);
29773 map = XCDR (map);
29776 return Qnil;
29779 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29780 3, 3, 0,
29781 doc: /* Lookup in image map MAP coordinates X and Y.
29782 An image map is an alist where each element has the format (AREA ID PLIST).
29783 An AREA is specified as either a rectangle, a circle, or a polygon:
29784 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29785 pixel coordinates of the upper left and bottom right corners.
29786 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29787 and the radius of the circle; r may be a float or integer.
29788 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29789 vector describes one corner in the polygon.
29790 Returns the alist element for the first matching AREA in MAP. */)
29791 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29793 if (NILP (map))
29794 return Qnil;
29796 CHECK_NUMBER (x);
29797 CHECK_NUMBER (y);
29799 return find_hot_spot (map,
29800 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29801 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29803 #endif /* HAVE_WINDOW_SYSTEM */
29806 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29807 static void
29808 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29810 #ifdef HAVE_WINDOW_SYSTEM
29811 if (!FRAME_WINDOW_P (f))
29812 return;
29814 /* Do not change cursor shape while dragging mouse. */
29815 if (EQ (do_mouse_tracking, Qdragging))
29816 return;
29818 if (!NILP (pointer))
29820 if (EQ (pointer, Qarrow))
29821 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29822 else if (EQ (pointer, Qhand))
29823 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29824 else if (EQ (pointer, Qtext))
29825 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29826 else if (EQ (pointer, intern ("hdrag")))
29827 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29828 else if (EQ (pointer, intern ("nhdrag")))
29829 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29830 # ifdef HAVE_X_WINDOWS
29831 else if (EQ (pointer, intern ("vdrag")))
29832 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29833 # endif
29834 else if (EQ (pointer, intern ("hourglass")))
29835 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29836 else if (EQ (pointer, Qmodeline))
29837 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29838 else
29839 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29842 if (cursor != No_Cursor)
29843 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29844 #endif
29847 /* Take proper action when mouse has moved to the mode or header line
29848 or marginal area AREA of window W, x-position X and y-position Y.
29849 X is relative to the start of the text display area of W, so the
29850 width of bitmap areas and scroll bars must be subtracted to get a
29851 position relative to the start of the mode line. */
29853 static void
29854 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29855 enum window_part area)
29857 struct window *w = XWINDOW (window);
29858 struct frame *f = XFRAME (w->frame);
29859 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29860 #ifdef HAVE_WINDOW_SYSTEM
29861 Display_Info *dpyinfo;
29862 #endif
29863 Cursor cursor = No_Cursor;
29864 Lisp_Object pointer = Qnil;
29865 int dx, dy, width, height;
29866 ptrdiff_t charpos;
29867 Lisp_Object string, object = Qnil;
29868 Lisp_Object pos UNINIT;
29869 Lisp_Object mouse_face;
29870 int original_x_pixel = x;
29871 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29872 struct glyph_row *row UNINIT;
29874 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29876 int x0;
29877 struct glyph *end;
29879 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29880 returns them in row/column units! */
29881 string = mode_line_string (w, area, &x, &y, &charpos,
29882 &object, &dx, &dy, &width, &height);
29884 row = (area == ON_MODE_LINE
29885 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29886 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29888 /* Find the glyph under the mouse pointer. */
29889 if (row->mode_line_p && row->enabled_p)
29891 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29892 end = glyph + row->used[TEXT_AREA];
29894 for (x0 = original_x_pixel;
29895 glyph < end && x0 >= glyph->pixel_width;
29896 ++glyph)
29897 x0 -= glyph->pixel_width;
29899 if (glyph >= end)
29900 glyph = NULL;
29903 else
29905 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29906 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29907 returns them in row/column units! */
29908 string = marginal_area_string (w, area, &x, &y, &charpos,
29909 &object, &dx, &dy, &width, &height);
29912 Lisp_Object help = Qnil;
29914 #ifdef HAVE_WINDOW_SYSTEM
29915 if (IMAGEP (object))
29917 Lisp_Object image_map, hotspot;
29918 if ((image_map = Fplist_get (XCDR (object), QCmap),
29919 !NILP (image_map))
29920 && (hotspot = find_hot_spot (image_map, dx, dy),
29921 CONSP (hotspot))
29922 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29924 Lisp_Object plist;
29926 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29927 If so, we could look for mouse-enter, mouse-leave
29928 properties in PLIST (and do something...). */
29929 hotspot = XCDR (hotspot);
29930 if (CONSP (hotspot)
29931 && (plist = XCAR (hotspot), CONSP (plist)))
29933 pointer = Fplist_get (plist, Qpointer);
29934 if (NILP (pointer))
29935 pointer = Qhand;
29936 help = Fplist_get (plist, Qhelp_echo);
29937 if (!NILP (help))
29939 help_echo_string = help;
29940 XSETWINDOW (help_echo_window, w);
29941 help_echo_object = w->contents;
29942 help_echo_pos = charpos;
29946 if (NILP (pointer))
29947 pointer = Fplist_get (XCDR (object), QCpointer);
29949 #endif /* HAVE_WINDOW_SYSTEM */
29951 if (STRINGP (string))
29952 pos = make_number (charpos);
29954 /* Set the help text and mouse pointer. If the mouse is on a part
29955 of the mode line without any text (e.g. past the right edge of
29956 the mode line text), use the default help text and pointer. */
29957 if (STRINGP (string) || area == ON_MODE_LINE)
29959 /* Arrange to display the help by setting the global variables
29960 help_echo_string, help_echo_object, and help_echo_pos. */
29961 if (NILP (help))
29963 if (STRINGP (string))
29964 help = Fget_text_property (pos, Qhelp_echo, string);
29966 if (!NILP (help))
29968 help_echo_string = help;
29969 XSETWINDOW (help_echo_window, w);
29970 help_echo_object = string;
29971 help_echo_pos = charpos;
29973 else if (area == ON_MODE_LINE)
29975 Lisp_Object default_help
29976 = buffer_local_value (Qmode_line_default_help_echo,
29977 w->contents);
29979 if (STRINGP (default_help))
29981 help_echo_string = default_help;
29982 XSETWINDOW (help_echo_window, w);
29983 help_echo_object = Qnil;
29984 help_echo_pos = -1;
29989 #ifdef HAVE_WINDOW_SYSTEM
29990 /* Change the mouse pointer according to what is under it. */
29991 if (FRAME_WINDOW_P (f))
29993 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29994 || minibuf_level
29995 || NILP (Vresize_mini_windows));
29997 dpyinfo = FRAME_DISPLAY_INFO (f);
29998 if (STRINGP (string))
30000 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30002 if (NILP (pointer))
30003 pointer = Fget_text_property (pos, Qpointer, string);
30005 /* Change the mouse pointer according to what is under X/Y. */
30006 if (NILP (pointer)
30007 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30009 Lisp_Object map;
30010 map = Fget_text_property (pos, Qlocal_map, string);
30011 if (!KEYMAPP (map))
30012 map = Fget_text_property (pos, Qkeymap, string);
30013 if (!KEYMAPP (map) && draggable)
30014 cursor = dpyinfo->vertical_scroll_bar_cursor;
30017 else if (draggable)
30018 /* Default mode-line pointer. */
30019 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30021 #endif
30024 /* Change the mouse face according to what is under X/Y. */
30025 bool mouse_face_shown = false;
30026 if (STRINGP (string))
30028 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30029 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30030 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30031 && glyph)
30033 Lisp_Object b, e;
30035 struct glyph * tmp_glyph;
30037 int gpos;
30038 int gseq_length;
30039 int total_pixel_width;
30040 ptrdiff_t begpos, endpos, ignore;
30042 int vpos, hpos;
30044 b = Fprevious_single_property_change (make_number (charpos + 1),
30045 Qmouse_face, string, Qnil);
30046 if (NILP (b))
30047 begpos = 0;
30048 else
30049 begpos = XINT (b);
30051 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30052 if (NILP (e))
30053 endpos = SCHARS (string);
30054 else
30055 endpos = XINT (e);
30057 /* Calculate the glyph position GPOS of GLYPH in the
30058 displayed string, relative to the beginning of the
30059 highlighted part of the string.
30061 Note: GPOS is different from CHARPOS. CHARPOS is the
30062 position of GLYPH in the internal string object. A mode
30063 line string format has structures which are converted to
30064 a flattened string by the Emacs Lisp interpreter. The
30065 internal string is an element of those structures. The
30066 displayed string is the flattened string. */
30067 tmp_glyph = row_start_glyph;
30068 while (tmp_glyph < glyph
30069 && (!(EQ (tmp_glyph->object, glyph->object)
30070 && begpos <= tmp_glyph->charpos
30071 && tmp_glyph->charpos < endpos)))
30072 tmp_glyph++;
30073 gpos = glyph - tmp_glyph;
30075 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30076 the highlighted part of the displayed string to which
30077 GLYPH belongs. Note: GSEQ_LENGTH is different from
30078 SCHARS (STRING), because the latter returns the length of
30079 the internal string. */
30080 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30081 tmp_glyph > glyph
30082 && (!(EQ (tmp_glyph->object, glyph->object)
30083 && begpos <= tmp_glyph->charpos
30084 && tmp_glyph->charpos < endpos));
30085 tmp_glyph--)
30087 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30089 /* Calculate the total pixel width of all the glyphs between
30090 the beginning of the highlighted area and GLYPH. */
30091 total_pixel_width = 0;
30092 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30093 total_pixel_width += tmp_glyph->pixel_width;
30095 /* Pre calculation of re-rendering position. Note: X is in
30096 column units here, after the call to mode_line_string or
30097 marginal_area_string. */
30098 hpos = x - gpos;
30099 vpos = (area == ON_MODE_LINE
30100 ? (w->current_matrix)->nrows - 1
30101 : 0);
30103 /* If GLYPH's position is included in the region that is
30104 already drawn in mouse face, we have nothing to do. */
30105 if ( EQ (window, hlinfo->mouse_face_window)
30106 && (!row->reversed_p
30107 ? (hlinfo->mouse_face_beg_col <= hpos
30108 && hpos < hlinfo->mouse_face_end_col)
30109 /* In R2L rows we swap BEG and END, see below. */
30110 : (hlinfo->mouse_face_end_col <= hpos
30111 && hpos < hlinfo->mouse_face_beg_col))
30112 && hlinfo->mouse_face_beg_row == vpos )
30113 return;
30115 if (clear_mouse_face (hlinfo))
30116 cursor = No_Cursor;
30118 if (!row->reversed_p)
30120 hlinfo->mouse_face_beg_col = hpos;
30121 hlinfo->mouse_face_beg_x = original_x_pixel
30122 - (total_pixel_width + dx);
30123 hlinfo->mouse_face_end_col = hpos + gseq_length;
30124 hlinfo->mouse_face_end_x = 0;
30126 else
30128 /* In R2L rows, show_mouse_face expects BEG and END
30129 coordinates to be swapped. */
30130 hlinfo->mouse_face_end_col = hpos;
30131 hlinfo->mouse_face_end_x = original_x_pixel
30132 - (total_pixel_width + dx);
30133 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30134 hlinfo->mouse_face_beg_x = 0;
30137 hlinfo->mouse_face_beg_row = vpos;
30138 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30139 hlinfo->mouse_face_past_end = false;
30140 hlinfo->mouse_face_window = window;
30142 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30143 charpos,
30144 0, &ignore,
30145 glyph->face_id,
30146 true);
30147 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30148 mouse_face_shown = true;
30150 if (NILP (pointer))
30151 pointer = Qhand;
30155 /* If mouse-face doesn't need to be shown, clear any existing
30156 mouse-face. */
30157 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30158 clear_mouse_face (hlinfo);
30160 define_frame_cursor1 (f, cursor, pointer);
30164 /* EXPORT:
30165 Take proper action when the mouse has moved to position X, Y on
30166 frame F with regards to highlighting portions of display that have
30167 mouse-face properties. Also de-highlight portions of display where
30168 the mouse was before, set the mouse pointer shape as appropriate
30169 for the mouse coordinates, and activate help echo (tooltips).
30170 X and Y can be negative or out of range. */
30172 void
30173 note_mouse_highlight (struct frame *f, int x, int y)
30175 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30176 enum window_part part = ON_NOTHING;
30177 Lisp_Object window;
30178 struct window *w;
30179 Cursor cursor = No_Cursor;
30180 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30181 struct buffer *b;
30183 /* When a menu is active, don't highlight because this looks odd. */
30184 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30185 if (popup_activated ())
30186 return;
30187 #endif
30189 if (!f->glyphs_initialized_p
30190 || f->pointer_invisible)
30191 return;
30193 hlinfo->mouse_face_mouse_x = x;
30194 hlinfo->mouse_face_mouse_y = y;
30195 hlinfo->mouse_face_mouse_frame = f;
30197 if (hlinfo->mouse_face_defer)
30198 return;
30200 /* Which window is that in? */
30201 window = window_from_coordinates (f, x, y, &part, true);
30203 /* If displaying active text in another window, clear that. */
30204 if (! EQ (window, hlinfo->mouse_face_window)
30205 /* Also clear if we move out of text area in same window. */
30206 || (!NILP (hlinfo->mouse_face_window)
30207 && !NILP (window)
30208 && part != ON_TEXT
30209 && part != ON_MODE_LINE
30210 && part != ON_HEADER_LINE))
30211 clear_mouse_face (hlinfo);
30213 /* Not on a window -> return. */
30214 if (!WINDOWP (window))
30215 return;
30217 /* Reset help_echo_string. It will get recomputed below. */
30218 help_echo_string = Qnil;
30220 /* Convert to window-relative pixel coordinates. */
30221 w = XWINDOW (window);
30222 frame_to_window_pixel_xy (w, &x, &y);
30224 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30225 /* Handle tool-bar window differently since it doesn't display a
30226 buffer. */
30227 if (EQ (window, f->tool_bar_window))
30229 note_tool_bar_highlight (f, x, y);
30230 return;
30232 #endif
30234 /* Mouse is on the mode, header line or margin? */
30235 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30236 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30238 note_mode_line_or_margin_highlight (window, x, y, part);
30240 #ifdef HAVE_WINDOW_SYSTEM
30241 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30243 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30244 /* Show non-text cursor (Bug#16647). */
30245 goto set_cursor;
30247 else
30248 #endif
30249 return;
30252 #ifdef HAVE_WINDOW_SYSTEM
30253 if (part == ON_VERTICAL_BORDER)
30255 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30256 help_echo_string = build_string ("drag-mouse-1: resize");
30258 else if (part == ON_RIGHT_DIVIDER)
30260 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30261 help_echo_string = build_string ("drag-mouse-1: resize");
30263 else if (part == ON_BOTTOM_DIVIDER)
30264 if (! WINDOW_BOTTOMMOST_P (w)
30265 || minibuf_level
30266 || NILP (Vresize_mini_windows))
30268 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30269 help_echo_string = build_string ("drag-mouse-1: resize");
30271 else
30272 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30273 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30274 || part == ON_VERTICAL_SCROLL_BAR
30275 || part == ON_HORIZONTAL_SCROLL_BAR)
30276 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30277 else
30278 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30279 #endif
30281 /* Are we in a window whose display is up to date?
30282 And verify the buffer's text has not changed. */
30283 b = XBUFFER (w->contents);
30284 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30286 int hpos, vpos, dx, dy, area = LAST_AREA;
30287 ptrdiff_t pos;
30288 struct glyph *glyph;
30289 Lisp_Object object;
30290 Lisp_Object mouse_face = Qnil, position;
30291 Lisp_Object *overlay_vec = NULL;
30292 ptrdiff_t i, noverlays;
30293 struct buffer *obuf;
30294 ptrdiff_t obegv, ozv;
30295 bool same_region;
30297 /* Find the glyph under X/Y. */
30298 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30300 #ifdef HAVE_WINDOW_SYSTEM
30301 /* Look for :pointer property on image. */
30302 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30304 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30305 if (img != NULL && IMAGEP (img->spec))
30307 Lisp_Object image_map, hotspot;
30308 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30309 !NILP (image_map))
30310 && (hotspot = find_hot_spot (image_map,
30311 glyph->slice.img.x + dx,
30312 glyph->slice.img.y + dy),
30313 CONSP (hotspot))
30314 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30316 Lisp_Object plist;
30318 /* Could check XCAR (hotspot) to see if we enter/leave
30319 this hot-spot.
30320 If so, we could look for mouse-enter, mouse-leave
30321 properties in PLIST (and do something...). */
30322 hotspot = XCDR (hotspot);
30323 if (CONSP (hotspot)
30324 && (plist = XCAR (hotspot), CONSP (plist)))
30326 pointer = Fplist_get (plist, Qpointer);
30327 if (NILP (pointer))
30328 pointer = Qhand;
30329 help_echo_string = Fplist_get (plist, Qhelp_echo);
30330 if (!NILP (help_echo_string))
30332 help_echo_window = window;
30333 help_echo_object = glyph->object;
30334 help_echo_pos = glyph->charpos;
30338 if (NILP (pointer))
30339 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30342 #endif /* HAVE_WINDOW_SYSTEM */
30344 /* Clear mouse face if X/Y not over text. */
30345 if (glyph == NULL
30346 || area != TEXT_AREA
30347 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30348 /* Glyph's OBJECT is nil for glyphs inserted by the
30349 display engine for its internal purposes, like truncation
30350 and continuation glyphs and blanks beyond the end of
30351 line's text on text terminals. If we are over such a
30352 glyph, we are not over any text. */
30353 || NILP (glyph->object)
30354 /* R2L rows have a stretch glyph at their front, which
30355 stands for no text, whereas L2R rows have no glyphs at
30356 all beyond the end of text. Treat such stretch glyphs
30357 like we do with NULL glyphs in L2R rows. */
30358 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30359 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30360 && glyph->type == STRETCH_GLYPH
30361 && glyph->avoid_cursor_p))
30363 if (clear_mouse_face (hlinfo))
30364 cursor = No_Cursor;
30365 if (FRAME_WINDOW_P (f) && NILP (pointer))
30367 #ifdef HAVE_WINDOW_SYSTEM
30368 if (area != TEXT_AREA)
30369 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30370 else
30371 pointer = Vvoid_text_area_pointer;
30372 #endif
30374 goto set_cursor;
30377 pos = glyph->charpos;
30378 object = glyph->object;
30379 if (!STRINGP (object) && !BUFFERP (object))
30380 goto set_cursor;
30382 /* If we get an out-of-range value, return now; avoid an error. */
30383 if (BUFFERP (object) && pos > BUF_Z (b))
30384 goto set_cursor;
30386 /* Make the window's buffer temporarily current for
30387 overlays_at and compute_char_face. */
30388 obuf = current_buffer;
30389 current_buffer = b;
30390 obegv = BEGV;
30391 ozv = ZV;
30392 BEGV = BEG;
30393 ZV = Z;
30395 /* Is this char mouse-active or does it have help-echo? */
30396 position = make_number (pos);
30398 USE_SAFE_ALLOCA;
30400 if (BUFFERP (object))
30402 /* Put all the overlays we want in a vector in overlay_vec. */
30403 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30404 /* Sort overlays into increasing priority order. */
30405 noverlays = sort_overlays (overlay_vec, noverlays, w);
30407 else
30408 noverlays = 0;
30410 if (NILP (Vmouse_highlight))
30412 clear_mouse_face (hlinfo);
30413 goto check_help_echo;
30416 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30418 if (same_region)
30419 cursor = No_Cursor;
30421 /* Check mouse-face highlighting. */
30422 if (! same_region
30423 /* If there exists an overlay with mouse-face overlapping
30424 the one we are currently highlighting, we have to
30425 check if we enter the overlapping overlay, and then
30426 highlight only that. */
30427 || (OVERLAYP (hlinfo->mouse_face_overlay)
30428 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30430 /* Find the highest priority overlay with a mouse-face. */
30431 Lisp_Object overlay = Qnil;
30432 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30434 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30435 if (!NILP (mouse_face))
30436 overlay = overlay_vec[i];
30439 /* If we're highlighting the same overlay as before, there's
30440 no need to do that again. */
30441 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30442 goto check_help_echo;
30443 hlinfo->mouse_face_overlay = overlay;
30445 /* Clear the display of the old active region, if any. */
30446 if (clear_mouse_face (hlinfo))
30447 cursor = No_Cursor;
30449 /* If no overlay applies, get a text property. */
30450 if (NILP (overlay))
30451 mouse_face = Fget_text_property (position, Qmouse_face, object);
30453 /* Next, compute the bounds of the mouse highlighting and
30454 display it. */
30455 if (!NILP (mouse_face) && STRINGP (object))
30457 /* The mouse-highlighting comes from a display string
30458 with a mouse-face. */
30459 Lisp_Object s, e;
30460 ptrdiff_t ignore;
30462 s = Fprevious_single_property_change
30463 (make_number (pos + 1), Qmouse_face, object, Qnil);
30464 e = Fnext_single_property_change
30465 (position, Qmouse_face, object, Qnil);
30466 if (NILP (s))
30467 s = make_number (0);
30468 if (NILP (e))
30469 e = make_number (SCHARS (object));
30470 mouse_face_from_string_pos (w, hlinfo, object,
30471 XINT (s), XINT (e));
30472 hlinfo->mouse_face_past_end = false;
30473 hlinfo->mouse_face_window = window;
30474 hlinfo->mouse_face_face_id
30475 = face_at_string_position (w, object, pos, 0, &ignore,
30476 glyph->face_id, true);
30477 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30478 cursor = No_Cursor;
30480 else
30482 /* The mouse-highlighting, if any, comes from an overlay
30483 or text property in the buffer. */
30484 Lisp_Object buffer UNINIT;
30485 Lisp_Object disp_string UNINIT;
30487 if (STRINGP (object))
30489 /* If we are on a display string with no mouse-face,
30490 check if the text under it has one. */
30491 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30492 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30493 pos = string_buffer_position (object, start);
30494 if (pos > 0)
30496 mouse_face = get_char_property_and_overlay
30497 (make_number (pos), Qmouse_face, w->contents, &overlay);
30498 buffer = w->contents;
30499 disp_string = object;
30502 else
30504 buffer = object;
30505 disp_string = Qnil;
30508 if (!NILP (mouse_face))
30510 Lisp_Object before, after;
30511 Lisp_Object before_string, after_string;
30512 /* To correctly find the limits of mouse highlight
30513 in a bidi-reordered buffer, we must not use the
30514 optimization of limiting the search in
30515 previous-single-property-change and
30516 next-single-property-change, because
30517 rows_from_pos_range needs the real start and end
30518 positions to DTRT in this case. That's because
30519 the first row visible in a window does not
30520 necessarily display the character whose position
30521 is the smallest. */
30522 Lisp_Object lim1
30523 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30524 ? Fmarker_position (w->start)
30525 : Qnil;
30526 Lisp_Object lim2
30527 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30528 ? make_number (BUF_Z (XBUFFER (buffer))
30529 - w->window_end_pos)
30530 : Qnil;
30532 if (NILP (overlay))
30534 /* Handle the text property case. */
30535 before = Fprevious_single_property_change
30536 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30537 after = Fnext_single_property_change
30538 (make_number (pos), Qmouse_face, buffer, lim2);
30539 before_string = after_string = Qnil;
30541 else
30543 /* Handle the overlay case. */
30544 before = Foverlay_start (overlay);
30545 after = Foverlay_end (overlay);
30546 before_string = Foverlay_get (overlay, Qbefore_string);
30547 after_string = Foverlay_get (overlay, Qafter_string);
30549 if (!STRINGP (before_string)) before_string = Qnil;
30550 if (!STRINGP (after_string)) after_string = Qnil;
30553 mouse_face_from_buffer_pos (window, hlinfo, pos,
30554 NILP (before)
30556 : XFASTINT (before),
30557 NILP (after)
30558 ? BUF_Z (XBUFFER (buffer))
30559 : XFASTINT (after),
30560 before_string, after_string,
30561 disp_string);
30562 cursor = No_Cursor;
30567 check_help_echo:
30569 /* Look for a `help-echo' property. */
30570 if (NILP (help_echo_string)) {
30571 Lisp_Object help, overlay;
30573 /* Check overlays first. */
30574 help = overlay = Qnil;
30575 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30577 overlay = overlay_vec[i];
30578 help = Foverlay_get (overlay, Qhelp_echo);
30581 if (!NILP (help))
30583 help_echo_string = help;
30584 help_echo_window = window;
30585 help_echo_object = overlay;
30586 help_echo_pos = pos;
30588 else
30590 Lisp_Object obj = glyph->object;
30591 ptrdiff_t charpos = glyph->charpos;
30593 /* Try text properties. */
30594 if (STRINGP (obj)
30595 && charpos >= 0
30596 && charpos < SCHARS (obj))
30598 help = Fget_text_property (make_number (charpos),
30599 Qhelp_echo, obj);
30600 if (NILP (help))
30602 /* If the string itself doesn't specify a help-echo,
30603 see if the buffer text ``under'' it does. */
30604 struct glyph_row *r
30605 = MATRIX_ROW (w->current_matrix, vpos);
30606 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30607 ptrdiff_t p = string_buffer_position (obj, start);
30608 if (p > 0)
30610 help = Fget_char_property (make_number (p),
30611 Qhelp_echo, w->contents);
30612 if (!NILP (help))
30614 charpos = p;
30615 obj = w->contents;
30620 else if (BUFFERP (obj)
30621 && charpos >= BEGV
30622 && charpos < ZV)
30623 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30624 obj);
30626 if (!NILP (help))
30628 help_echo_string = help;
30629 help_echo_window = window;
30630 help_echo_object = obj;
30631 help_echo_pos = charpos;
30636 #ifdef HAVE_WINDOW_SYSTEM
30637 /* Look for a `pointer' property. */
30638 if (FRAME_WINDOW_P (f) && NILP (pointer))
30640 /* Check overlays first. */
30641 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30642 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30644 if (NILP (pointer))
30646 Lisp_Object obj = glyph->object;
30647 ptrdiff_t charpos = glyph->charpos;
30649 /* Try text properties. */
30650 if (STRINGP (obj)
30651 && charpos >= 0
30652 && charpos < SCHARS (obj))
30654 pointer = Fget_text_property (make_number (charpos),
30655 Qpointer, obj);
30656 if (NILP (pointer))
30658 /* If the string itself doesn't specify a pointer,
30659 see if the buffer text ``under'' it does. */
30660 struct glyph_row *r
30661 = MATRIX_ROW (w->current_matrix, vpos);
30662 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30663 ptrdiff_t p = string_buffer_position (obj, start);
30664 if (p > 0)
30665 pointer = Fget_char_property (make_number (p),
30666 Qpointer, w->contents);
30669 else if (BUFFERP (obj)
30670 && charpos >= BEGV
30671 && charpos < ZV)
30672 pointer = Fget_text_property (make_number (charpos),
30673 Qpointer, obj);
30676 #endif /* HAVE_WINDOW_SYSTEM */
30678 BEGV = obegv;
30679 ZV = ozv;
30680 current_buffer = obuf;
30681 SAFE_FREE ();
30684 set_cursor:
30685 define_frame_cursor1 (f, cursor, pointer);
30689 /* EXPORT for RIF:
30690 Clear any mouse-face on window W. This function is part of the
30691 redisplay interface, and is called from try_window_id and similar
30692 functions to ensure the mouse-highlight is off. */
30694 void
30695 x_clear_window_mouse_face (struct window *w)
30697 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30698 Lisp_Object window;
30700 block_input ();
30701 XSETWINDOW (window, w);
30702 if (EQ (window, hlinfo->mouse_face_window))
30703 clear_mouse_face (hlinfo);
30704 unblock_input ();
30708 /* EXPORT:
30709 Just discard the mouse face information for frame F, if any.
30710 This is used when the size of F is changed. */
30712 void
30713 cancel_mouse_face (struct frame *f)
30715 Lisp_Object window;
30716 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30718 window = hlinfo->mouse_face_window;
30719 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30720 reset_mouse_highlight (hlinfo);
30725 /***********************************************************************
30726 Exposure Events
30727 ***********************************************************************/
30729 #ifdef HAVE_WINDOW_SYSTEM
30731 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30732 which intersects rectangle R. R is in window-relative coordinates. */
30734 static void
30735 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30736 enum glyph_row_area area)
30738 struct glyph *first = row->glyphs[area];
30739 struct glyph *end = row->glyphs[area] + row->used[area];
30740 struct glyph *last;
30741 int first_x, start_x, x;
30743 if (area == TEXT_AREA && row->fill_line_p)
30744 /* If row extends face to end of line write the whole line. */
30745 draw_glyphs (w, 0, row, area,
30746 0, row->used[area],
30747 DRAW_NORMAL_TEXT, 0);
30748 else
30750 /* Set START_X to the window-relative start position for drawing glyphs of
30751 AREA. The first glyph of the text area can be partially visible.
30752 The first glyphs of other areas cannot. */
30753 start_x = window_box_left_offset (w, area);
30754 x = start_x;
30755 if (area == TEXT_AREA)
30756 x += row->x;
30758 /* Find the first glyph that must be redrawn. */
30759 while (first < end
30760 && x + first->pixel_width < r->x)
30762 x += first->pixel_width;
30763 ++first;
30766 /* Find the last one. */
30767 last = first;
30768 first_x = x;
30769 /* Use a signed int intermediate value to avoid catastrophic
30770 failures due to comparison between signed and unsigned, when
30771 x is negative (can happen for wide images that are hscrolled). */
30772 int r_end = r->x + r->width;
30773 while (last < end && x < r_end)
30775 x += last->pixel_width;
30776 ++last;
30779 /* Repaint. */
30780 if (last > first)
30781 draw_glyphs (w, first_x - start_x, row, area,
30782 first - row->glyphs[area], last - row->glyphs[area],
30783 DRAW_NORMAL_TEXT, 0);
30788 /* Redraw the parts of the glyph row ROW on window W intersecting
30789 rectangle R. R is in window-relative coordinates. Value is
30790 true if mouse-face was overwritten. */
30792 static bool
30793 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30795 eassert (row->enabled_p);
30797 if (row->mode_line_p || w->pseudo_window_p)
30798 draw_glyphs (w, 0, row, TEXT_AREA,
30799 0, row->used[TEXT_AREA],
30800 DRAW_NORMAL_TEXT, 0);
30801 else
30803 if (row->used[LEFT_MARGIN_AREA])
30804 expose_area (w, row, r, LEFT_MARGIN_AREA);
30805 if (row->used[TEXT_AREA])
30806 expose_area (w, row, r, TEXT_AREA);
30807 if (row->used[RIGHT_MARGIN_AREA])
30808 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30809 draw_row_fringe_bitmaps (w, row);
30812 return row->mouse_face_p;
30816 /* Redraw those parts of glyphs rows during expose event handling that
30817 overlap other rows. Redrawing of an exposed line writes over parts
30818 of lines overlapping that exposed line; this function fixes that.
30820 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30821 row in W's current matrix that is exposed and overlaps other rows.
30822 LAST_OVERLAPPING_ROW is the last such row. */
30824 static void
30825 expose_overlaps (struct window *w,
30826 struct glyph_row *first_overlapping_row,
30827 struct glyph_row *last_overlapping_row,
30828 XRectangle *r)
30830 struct glyph_row *row;
30832 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30833 if (row->overlapping_p)
30835 eassert (row->enabled_p && !row->mode_line_p);
30837 row->clip = r;
30838 if (row->used[LEFT_MARGIN_AREA])
30839 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30841 if (row->used[TEXT_AREA])
30842 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30844 if (row->used[RIGHT_MARGIN_AREA])
30845 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30846 row->clip = NULL;
30851 /* Return true if W's cursor intersects rectangle R. */
30853 static bool
30854 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30856 XRectangle cr, result;
30857 struct glyph *cursor_glyph;
30858 struct glyph_row *row;
30860 if (w->phys_cursor.vpos >= 0
30861 && w->phys_cursor.vpos < w->current_matrix->nrows
30862 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30863 row->enabled_p)
30864 && row->cursor_in_fringe_p)
30866 /* Cursor is in the fringe. */
30867 cr.x = window_box_right_offset (w,
30868 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30869 ? RIGHT_MARGIN_AREA
30870 : TEXT_AREA));
30871 cr.y = row->y;
30872 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30873 cr.height = row->height;
30874 return x_intersect_rectangles (&cr, r, &result);
30877 cursor_glyph = get_phys_cursor_glyph (w);
30878 if (cursor_glyph)
30880 /* r is relative to W's box, but w->phys_cursor.x is relative
30881 to left edge of W's TEXT area. Adjust it. */
30882 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30883 cr.y = w->phys_cursor.y;
30884 cr.width = cursor_glyph->pixel_width;
30885 cr.height = w->phys_cursor_height;
30886 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30887 I assume the effect is the same -- and this is portable. */
30888 return x_intersect_rectangles (&cr, r, &result);
30890 /* If we don't understand the format, pretend we're not in the hot-spot. */
30891 return false;
30895 /* EXPORT:
30896 Draw a vertical window border to the right of window W if W doesn't
30897 have vertical scroll bars. */
30899 void
30900 x_draw_vertical_border (struct window *w)
30902 struct frame *f = XFRAME (WINDOW_FRAME (w));
30904 /* We could do better, if we knew what type of scroll-bar the adjacent
30905 windows (on either side) have... But we don't :-(
30906 However, I think this works ok. ++KFS 2003-04-25 */
30908 /* Redraw borders between horizontally adjacent windows. Don't
30909 do it for frames with vertical scroll bars because either the
30910 right scroll bar of a window, or the left scroll bar of its
30911 neighbor will suffice as a border. */
30912 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30913 return;
30915 /* Note: It is necessary to redraw both the left and the right
30916 borders, for when only this single window W is being
30917 redisplayed. */
30918 if (!WINDOW_RIGHTMOST_P (w)
30919 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30921 int x0, x1, y0, y1;
30923 window_box_edges (w, &x0, &y0, &x1, &y1);
30924 y1 -= 1;
30926 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30927 x1 -= 1;
30929 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30932 if (!WINDOW_LEFTMOST_P (w)
30933 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30935 int x0, x1, y0, y1;
30937 window_box_edges (w, &x0, &y0, &x1, &y1);
30938 y1 -= 1;
30940 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30941 x0 -= 1;
30943 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30948 /* Draw window dividers for window W. */
30950 void
30951 x_draw_right_divider (struct window *w)
30953 struct frame *f = WINDOW_XFRAME (w);
30955 if (w->mini || w->pseudo_window_p)
30956 return;
30957 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30959 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30960 int x1 = WINDOW_RIGHT_EDGE_X (w);
30961 int y0 = WINDOW_TOP_EDGE_Y (w);
30962 /* The bottom divider prevails. */
30963 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30965 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30969 static void
30970 x_draw_bottom_divider (struct window *w)
30972 struct frame *f = XFRAME (WINDOW_FRAME (w));
30974 if (w->mini || w->pseudo_window_p)
30975 return;
30976 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30978 int x0 = WINDOW_LEFT_EDGE_X (w);
30979 int x1 = WINDOW_RIGHT_EDGE_X (w);
30980 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30981 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30983 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30987 /* Redraw the part of window W intersection rectangle FR. Pixel
30988 coordinates in FR are frame-relative. Call this function with
30989 input blocked. Value is true if the exposure overwrites
30990 mouse-face. */
30992 static bool
30993 expose_window (struct window *w, XRectangle *fr)
30995 struct frame *f = XFRAME (w->frame);
30996 XRectangle wr, r;
30997 bool mouse_face_overwritten_p = false;
30999 /* If window is not yet fully initialized, do nothing. This can
31000 happen when toolkit scroll bars are used and a window is split.
31001 Reconfiguring the scroll bar will generate an expose for a newly
31002 created window. */
31003 if (w->current_matrix == NULL)
31004 return false;
31006 /* When we're currently updating the window, display and current
31007 matrix usually don't agree. Arrange for a thorough display
31008 later. */
31009 if (w->must_be_updated_p)
31011 SET_FRAME_GARBAGED (f);
31012 return false;
31015 /* Frame-relative pixel rectangle of W. */
31016 wr.x = WINDOW_LEFT_EDGE_X (w);
31017 wr.y = WINDOW_TOP_EDGE_Y (w);
31018 wr.width = WINDOW_PIXEL_WIDTH (w);
31019 wr.height = WINDOW_PIXEL_HEIGHT (w);
31021 if (x_intersect_rectangles (fr, &wr, &r))
31023 int yb = window_text_bottom_y (w);
31024 struct glyph_row *row;
31025 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31027 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31028 r.x, r.y, r.width, r.height));
31030 /* Convert to window coordinates. */
31031 r.x -= WINDOW_LEFT_EDGE_X (w);
31032 r.y -= WINDOW_TOP_EDGE_Y (w);
31034 /* Turn off the cursor. */
31035 bool cursor_cleared_p = (!w->pseudo_window_p
31036 && phys_cursor_in_rect_p (w, &r));
31037 if (cursor_cleared_p)
31038 x_clear_cursor (w);
31040 /* If the row containing the cursor extends face to end of line,
31041 then expose_area might overwrite the cursor outside the
31042 rectangle and thus notice_overwritten_cursor might clear
31043 w->phys_cursor_on_p. We remember the original value and
31044 check later if it is changed. */
31045 bool phys_cursor_on_p = w->phys_cursor_on_p;
31047 /* Use a signed int intermediate value to avoid catastrophic
31048 failures due to comparison between signed and unsigned, when
31049 y0 or y1 is negative (can happen for tall images). */
31050 int r_bottom = r.y + r.height;
31052 /* Update lines intersecting rectangle R. */
31053 first_overlapping_row = last_overlapping_row = NULL;
31054 for (row = w->current_matrix->rows;
31055 row->enabled_p;
31056 ++row)
31058 int y0 = row->y;
31059 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31061 if ((y0 >= r.y && y0 < r_bottom)
31062 || (y1 > r.y && y1 < r_bottom)
31063 || (r.y >= y0 && r.y < y1)
31064 || (r_bottom > y0 && r_bottom < y1))
31066 /* A header line may be overlapping, but there is no need
31067 to fix overlapping areas for them. KFS 2005-02-12 */
31068 if (row->overlapping_p && !row->mode_line_p)
31070 if (first_overlapping_row == NULL)
31071 first_overlapping_row = row;
31072 last_overlapping_row = row;
31075 row->clip = fr;
31076 if (expose_line (w, row, &r))
31077 mouse_face_overwritten_p = true;
31078 row->clip = NULL;
31080 else if (row->overlapping_p)
31082 /* We must redraw a row overlapping the exposed area. */
31083 if (y0 < r.y
31084 ? y0 + row->phys_height > r.y
31085 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31087 if (first_overlapping_row == NULL)
31088 first_overlapping_row = row;
31089 last_overlapping_row = row;
31093 if (y1 >= yb)
31094 break;
31097 /* Display the mode line if there is one. */
31098 if (WINDOW_WANTS_MODELINE_P (w)
31099 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31100 row->enabled_p)
31101 && row->y < r_bottom)
31103 if (expose_line (w, row, &r))
31104 mouse_face_overwritten_p = true;
31107 if (!w->pseudo_window_p)
31109 /* Fix the display of overlapping rows. */
31110 if (first_overlapping_row)
31111 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31112 fr);
31114 /* Draw border between windows. */
31115 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31116 x_draw_right_divider (w);
31117 else
31118 x_draw_vertical_border (w);
31120 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31121 x_draw_bottom_divider (w);
31123 /* Turn the cursor on again. */
31124 if (cursor_cleared_p
31125 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31126 update_window_cursor (w, true);
31130 return mouse_face_overwritten_p;
31135 /* Redraw (parts) of all windows in the window tree rooted at W that
31136 intersect R. R contains frame pixel coordinates. Value is
31137 true if the exposure overwrites mouse-face. */
31139 static bool
31140 expose_window_tree (struct window *w, XRectangle *r)
31142 struct frame *f = XFRAME (w->frame);
31143 bool mouse_face_overwritten_p = false;
31145 while (w && !FRAME_GARBAGED_P (f))
31147 mouse_face_overwritten_p
31148 |= (WINDOWP (w->contents)
31149 ? expose_window_tree (XWINDOW (w->contents), r)
31150 : expose_window (w, r));
31152 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31155 return mouse_face_overwritten_p;
31159 /* EXPORT:
31160 Redisplay an exposed area of frame F. X and Y are the upper-left
31161 corner of the exposed rectangle. W and H are width and height of
31162 the exposed area. All are pixel values. W or H zero means redraw
31163 the entire frame. */
31165 void
31166 expose_frame (struct frame *f, int x, int y, int w, int h)
31168 XRectangle r;
31169 bool mouse_face_overwritten_p = false;
31171 TRACE ((stderr, "expose_frame "));
31173 /* No need to redraw if frame will be redrawn soon. */
31174 if (FRAME_GARBAGED_P (f))
31176 TRACE ((stderr, " garbaged\n"));
31177 return;
31180 /* If basic faces haven't been realized yet, there is no point in
31181 trying to redraw anything. This can happen when we get an expose
31182 event while Emacs is starting, e.g. by moving another window. */
31183 if (FRAME_FACE_CACHE (f) == NULL
31184 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31186 TRACE ((stderr, " no faces\n"));
31187 return;
31190 if (w == 0 || h == 0)
31192 r.x = r.y = 0;
31193 r.width = FRAME_TEXT_WIDTH (f);
31194 r.height = FRAME_TEXT_HEIGHT (f);
31196 else
31198 r.x = x;
31199 r.y = y;
31200 r.width = w;
31201 r.height = h;
31204 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31205 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31207 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31208 if (WINDOWP (f->tool_bar_window))
31209 mouse_face_overwritten_p
31210 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31211 #endif
31213 #ifdef HAVE_X_WINDOWS
31214 #ifndef MSDOS
31215 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31216 if (WINDOWP (f->menu_bar_window))
31217 mouse_face_overwritten_p
31218 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31219 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31220 #endif
31221 #endif
31223 /* Some window managers support a focus-follows-mouse style with
31224 delayed raising of frames. Imagine a partially obscured frame,
31225 and moving the mouse into partially obscured mouse-face on that
31226 frame. The visible part of the mouse-face will be highlighted,
31227 then the WM raises the obscured frame. With at least one WM, KDE
31228 2.1, Emacs is not getting any event for the raising of the frame
31229 (even tried with SubstructureRedirectMask), only Expose events.
31230 These expose events will draw text normally, i.e. not
31231 highlighted. Which means we must redo the highlight here.
31232 Subsume it under ``we love X''. --gerd 2001-08-15 */
31233 /* Included in Windows version because Windows most likely does not
31234 do the right thing if any third party tool offers
31235 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31236 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31238 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31239 if (f == hlinfo->mouse_face_mouse_frame)
31241 int mouse_x = hlinfo->mouse_face_mouse_x;
31242 int mouse_y = hlinfo->mouse_face_mouse_y;
31243 clear_mouse_face (hlinfo);
31244 note_mouse_highlight (f, mouse_x, mouse_y);
31250 /* EXPORT:
31251 Determine the intersection of two rectangles R1 and R2. Return
31252 the intersection in *RESULT. Value is true if RESULT is not
31253 empty. */
31255 bool
31256 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31258 XRectangle *left, *right;
31259 XRectangle *upper, *lower;
31260 bool intersection_p = false;
31262 /* Rearrange so that R1 is the left-most rectangle. */
31263 if (r1->x < r2->x)
31264 left = r1, right = r2;
31265 else
31266 left = r2, right = r1;
31268 /* X0 of the intersection is right.x0, if this is inside R1,
31269 otherwise there is no intersection. */
31270 if (right->x <= left->x + left->width)
31272 result->x = right->x;
31274 /* The right end of the intersection is the minimum of
31275 the right ends of left and right. */
31276 result->width = (min (left->x + left->width, right->x + right->width)
31277 - result->x);
31279 /* Same game for Y. */
31280 if (r1->y < r2->y)
31281 upper = r1, lower = r2;
31282 else
31283 upper = r2, lower = r1;
31285 /* The upper end of the intersection is lower.y0, if this is inside
31286 of upper. Otherwise, there is no intersection. */
31287 if (lower->y <= upper->y + upper->height)
31289 result->y = lower->y;
31291 /* The lower end of the intersection is the minimum of the lower
31292 ends of upper and lower. */
31293 result->height = (min (lower->y + lower->height,
31294 upper->y + upper->height)
31295 - result->y);
31296 intersection_p = true;
31300 return intersection_p;
31303 #endif /* HAVE_WINDOW_SYSTEM */
31306 /***********************************************************************
31307 Initialization
31308 ***********************************************************************/
31310 void
31311 syms_of_xdisp (void)
31313 Vwith_echo_area_save_vector = Qnil;
31314 staticpro (&Vwith_echo_area_save_vector);
31316 Vmessage_stack = Qnil;
31317 staticpro (&Vmessage_stack);
31319 /* Non-nil means don't actually do any redisplay. */
31320 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31322 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31324 DEFVAR_BOOL("inhibit-message", inhibit_message,
31325 doc: /* Non-nil means calls to `message' are not displayed.
31326 They are still logged to the *Messages* buffer. */);
31327 inhibit_message = 0;
31329 message_dolog_marker1 = Fmake_marker ();
31330 staticpro (&message_dolog_marker1);
31331 message_dolog_marker2 = Fmake_marker ();
31332 staticpro (&message_dolog_marker2);
31333 message_dolog_marker3 = Fmake_marker ();
31334 staticpro (&message_dolog_marker3);
31336 defsubr (&Sset_buffer_redisplay);
31337 #ifdef GLYPH_DEBUG
31338 defsubr (&Sdump_frame_glyph_matrix);
31339 defsubr (&Sdump_glyph_matrix);
31340 defsubr (&Sdump_glyph_row);
31341 defsubr (&Sdump_tool_bar_row);
31342 defsubr (&Strace_redisplay);
31343 defsubr (&Strace_to_stderr);
31344 #endif
31345 #ifdef HAVE_WINDOW_SYSTEM
31346 defsubr (&Stool_bar_height);
31347 defsubr (&Slookup_image_map);
31348 #endif
31349 defsubr (&Sline_pixel_height);
31350 defsubr (&Sformat_mode_line);
31351 defsubr (&Sinvisible_p);
31352 defsubr (&Scurrent_bidi_paragraph_direction);
31353 defsubr (&Swindow_text_pixel_size);
31354 defsubr (&Smove_point_visually);
31355 defsubr (&Sbidi_find_overridden_directionality);
31357 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31358 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31359 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31360 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31361 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31362 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31363 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31364 DEFSYM (Qeval, "eval");
31365 DEFSYM (QCdata, ":data");
31367 /* Names of text properties relevant for redisplay. */
31368 DEFSYM (Qdisplay, "display");
31369 DEFSYM (Qspace_width, "space-width");
31370 DEFSYM (Qraise, "raise");
31371 DEFSYM (Qslice, "slice");
31372 DEFSYM (Qspace, "space");
31373 DEFSYM (Qmargin, "margin");
31374 DEFSYM (Qpointer, "pointer");
31375 DEFSYM (Qleft_margin, "left-margin");
31376 DEFSYM (Qright_margin, "right-margin");
31377 DEFSYM (Qcenter, "center");
31378 DEFSYM (Qline_height, "line-height");
31379 DEFSYM (QCalign_to, ":align-to");
31380 DEFSYM (QCrelative_width, ":relative-width");
31381 DEFSYM (QCrelative_height, ":relative-height");
31382 DEFSYM (QCeval, ":eval");
31383 DEFSYM (QCpropertize, ":propertize");
31384 DEFSYM (QCfile, ":file");
31385 DEFSYM (Qfontified, "fontified");
31386 DEFSYM (Qfontification_functions, "fontification-functions");
31388 /* Name of the face used to highlight trailing whitespace. */
31389 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31391 /* Name and number of the face used to highlight escape glyphs. */
31392 DEFSYM (Qescape_glyph, "escape-glyph");
31394 /* Name and number of the face used to highlight non-breaking
31395 spaces/hyphens. */
31396 DEFSYM (Qnobreak_space, "nobreak-space");
31397 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31399 /* The symbol 'image' which is the car of the lists used to represent
31400 images in Lisp. Also a tool bar style. */
31401 DEFSYM (Qimage, "image");
31403 /* Tool bar styles. */
31404 DEFSYM (Qtext, "text");
31405 DEFSYM (Qboth, "both");
31406 DEFSYM (Qboth_horiz, "both-horiz");
31407 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31409 /* The image map types. */
31410 DEFSYM (QCmap, ":map");
31411 DEFSYM (QCpointer, ":pointer");
31412 DEFSYM (Qrect, "rect");
31413 DEFSYM (Qcircle, "circle");
31414 DEFSYM (Qpoly, "poly");
31416 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31418 DEFSYM (Qgrow_only, "grow-only");
31419 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31420 DEFSYM (Qposition, "position");
31421 DEFSYM (Qbuffer_position, "buffer-position");
31422 DEFSYM (Qobject, "object");
31424 /* Cursor shapes. */
31425 DEFSYM (Qbar, "bar");
31426 DEFSYM (Qhbar, "hbar");
31427 DEFSYM (Qbox, "box");
31428 DEFSYM (Qhollow, "hollow");
31430 /* Pointer shapes. */
31431 DEFSYM (Qhand, "hand");
31432 DEFSYM (Qarrow, "arrow");
31433 /* also Qtext */
31435 DEFSYM (Qdragging, "dragging");
31437 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31439 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31440 staticpro (&list_of_error);
31442 /* Values of those variables at last redisplay are stored as
31443 properties on 'overlay-arrow-position' symbol. However, if
31444 Voverlay_arrow_position is a marker, last-arrow-position is its
31445 numerical position. */
31446 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31447 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31450 properties on a symbol in overlay-arrow-variable-list. */
31451 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31452 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31454 echo_buffer[0] = echo_buffer[1] = Qnil;
31455 staticpro (&echo_buffer[0]);
31456 staticpro (&echo_buffer[1]);
31458 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31459 staticpro (&echo_area_buffer[0]);
31460 staticpro (&echo_area_buffer[1]);
31462 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31463 staticpro (&Vmessages_buffer_name);
31465 mode_line_proptrans_alist = Qnil;
31466 staticpro (&mode_line_proptrans_alist);
31467 mode_line_string_list = Qnil;
31468 staticpro (&mode_line_string_list);
31469 mode_line_string_face = Qnil;
31470 staticpro (&mode_line_string_face);
31471 mode_line_string_face_prop = Qnil;
31472 staticpro (&mode_line_string_face_prop);
31473 Vmode_line_unwind_vector = Qnil;
31474 staticpro (&Vmode_line_unwind_vector);
31476 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31478 help_echo_string = Qnil;
31479 staticpro (&help_echo_string);
31480 help_echo_object = Qnil;
31481 staticpro (&help_echo_object);
31482 help_echo_window = Qnil;
31483 staticpro (&help_echo_window);
31484 previous_help_echo_string = Qnil;
31485 staticpro (&previous_help_echo_string);
31486 help_echo_pos = -1;
31488 DEFSYM (Qright_to_left, "right-to-left");
31489 DEFSYM (Qleft_to_right, "left-to-right");
31490 defsubr (&Sbidi_resolved_levels);
31492 #ifdef HAVE_WINDOW_SYSTEM
31493 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31494 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31495 For example, if a block cursor is over a tab, it will be drawn as
31496 wide as that tab on the display. */);
31497 x_stretch_cursor_p = 0;
31498 #endif
31500 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31501 doc: /* Non-nil means highlight trailing whitespace.
31502 The face used for trailing whitespace is `trailing-whitespace'. */);
31503 Vshow_trailing_whitespace = Qnil;
31505 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31506 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31507 If the value is t, Emacs highlights non-ASCII chars which have the
31508 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31509 or `nobreak-hyphen' face respectively.
31511 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31512 U+2011 (non-breaking hyphen) are affected.
31514 Any other non-nil value means to display these characters as a escape
31515 glyph followed by an ordinary space or hyphen.
31517 A value of nil means no special handling of these characters. */);
31518 Vnobreak_char_display = Qt;
31520 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31521 doc: /* The pointer shape to show in void text areas.
31522 A value of nil means to show the text pointer. Other options are
31523 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31524 `hourglass'. */);
31525 Vvoid_text_area_pointer = Qarrow;
31527 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31528 doc: /* Non-nil means don't actually do any redisplay.
31529 This is used for internal purposes. */);
31530 Vinhibit_redisplay = Qnil;
31532 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31533 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31534 Vglobal_mode_string = Qnil;
31536 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31537 doc: /* Marker for where to display an arrow on top of the buffer text.
31538 This must be the beginning of a line in order to work.
31539 See also `overlay-arrow-string'. */);
31540 Voverlay_arrow_position = Qnil;
31542 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31543 doc: /* String to display as an arrow in non-window frames.
31544 See also `overlay-arrow-position'. */);
31545 Voverlay_arrow_string = build_pure_c_string ("=>");
31547 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31548 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31549 The symbols on this list are examined during redisplay to determine
31550 where to display overlay arrows. */);
31551 Voverlay_arrow_variable_list
31552 = list1 (intern_c_string ("overlay-arrow-position"));
31554 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31555 doc: /* The number of lines to try scrolling a window by when point moves out.
31556 If that fails to bring point back on frame, point is centered instead.
31557 If this is zero, point is always centered after it moves off frame.
31558 If you want scrolling to always be a line at a time, you should set
31559 `scroll-conservatively' to a large value rather than set this to 1. */);
31561 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31562 doc: /* Scroll up to this many lines, to bring point back on screen.
31563 If point moves off-screen, redisplay will scroll by up to
31564 `scroll-conservatively' lines in order to bring point just barely
31565 onto the screen again. If that cannot be done, then redisplay
31566 recenters point as usual.
31568 If the value is greater than 100, redisplay will never recenter point,
31569 but will always scroll just enough text to bring point into view, even
31570 if you move far away.
31572 A value of zero means always recenter point if it moves off screen. */);
31573 scroll_conservatively = 0;
31575 DEFVAR_INT ("scroll-margin", scroll_margin,
31576 doc: /* Number of lines of margin at the top and bottom of a window.
31577 Recenter the window whenever point gets within this many lines
31578 of the top or bottom of the window. */);
31579 scroll_margin = 0;
31581 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31582 doc: /* Pixels per inch value for non-window system displays.
31583 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31584 Vdisplay_pixels_per_inch = make_float (72.0);
31586 #ifdef GLYPH_DEBUG
31587 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31588 #endif
31590 DEFVAR_LISP ("truncate-partial-width-windows",
31591 Vtruncate_partial_width_windows,
31592 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31593 For an integer value, truncate lines in each window narrower than the
31594 full frame width, provided the total window width in column units is less
31595 than that integer; otherwise, respect the value of `truncate-lines'.
31596 The total width of the window is as returned by `window-total-width', it
31597 includes the fringes, the continuation and truncation glyphs, the
31598 display margins (if any), and the scroll bar
31600 For any other non-nil value, truncate lines in all windows that do
31601 not span the full frame width.
31603 A value of nil means to respect the value of `truncate-lines'.
31605 If `word-wrap' is enabled, you might want to reduce this. */);
31606 Vtruncate_partial_width_windows = make_number (50);
31608 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31609 doc: /* Maximum buffer size for which line number should be displayed.
31610 If the buffer is bigger than this, the line number does not appear
31611 in the mode line. A value of nil means no limit. */);
31612 Vline_number_display_limit = Qnil;
31614 DEFVAR_INT ("line-number-display-limit-width",
31615 line_number_display_limit_width,
31616 doc: /* Maximum line width (in characters) for line number display.
31617 If the average length of the lines near point is bigger than this, then the
31618 line number may be omitted from the mode line. */);
31619 line_number_display_limit_width = 200;
31621 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31622 doc: /* Non-nil means highlight region even in nonselected windows. */);
31623 highlight_nonselected_windows = false;
31625 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31626 doc: /* Non-nil if more than one frame is visible on this display.
31627 Minibuffer-only frames don't count, but iconified frames do.
31628 This variable is not guaranteed to be accurate except while processing
31629 `frame-title-format' and `icon-title-format'. */);
31631 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31632 doc: /* Template for displaying the title bar of visible frames.
31633 \(Assuming the window manager supports this feature.)
31635 This variable has the same structure as `mode-line-format', except that
31636 the %c and %l constructs are ignored. It is used only on frames for
31637 which no explicit name has been set (see `modify-frame-parameters'). */);
31639 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31640 doc: /* Template for displaying the title bar of an iconified frame.
31641 \(Assuming the window manager supports this feature.)
31642 This variable has the same structure as `mode-line-format' (which see),
31643 and is used only on frames for which no explicit name has been set
31644 \(see `modify-frame-parameters'). */);
31645 Vicon_title_format
31646 = Vframe_title_format
31647 = listn (CONSTYPE_PURE, 3,
31648 intern_c_string ("multiple-frames"),
31649 build_pure_c_string ("%b"),
31650 listn (CONSTYPE_PURE, 4,
31651 empty_unibyte_string,
31652 intern_c_string ("invocation-name"),
31653 build_pure_c_string ("@"),
31654 intern_c_string ("system-name")));
31656 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31657 doc: /* Maximum number of lines to keep in the message log buffer.
31658 If nil, disable message logging. If t, log messages but don't truncate
31659 the buffer when it becomes large. */);
31660 Vmessage_log_max = make_number (1000);
31662 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31663 doc: /* List of functions to call before redisplaying a window with scrolling.
31664 Each function is called with two arguments, the window and its new
31665 display-start position.
31666 These functions are called whenever the `window-start' marker is modified,
31667 either to point into another buffer (e.g. via `set-window-buffer') or another
31668 place in the same buffer.
31669 Note that the value of `window-end' is not valid when these functions are
31670 called.
31672 Warning: Do not use this feature to alter the way the window
31673 is scrolled. It is not designed for that, and such use probably won't
31674 work. */);
31675 Vwindow_scroll_functions = Qnil;
31677 DEFVAR_LISP ("window-text-change-functions",
31678 Vwindow_text_change_functions,
31679 doc: /* Functions to call in redisplay when text in the window might change. */);
31680 Vwindow_text_change_functions = Qnil;
31682 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31683 doc: /* Functions called when redisplay of a window reaches the end trigger.
31684 Each function is called with two arguments, the window and the end trigger value.
31685 See `set-window-redisplay-end-trigger'. */);
31686 Vredisplay_end_trigger_functions = Qnil;
31688 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31689 doc: /* Non-nil means autoselect window with mouse pointer.
31690 If nil, do not autoselect windows.
31691 A positive number means delay autoselection by that many seconds: a
31692 window is autoselected only after the mouse has remained in that
31693 window for the duration of the delay.
31694 A negative number has a similar effect, but causes windows to be
31695 autoselected only after the mouse has stopped moving. (Because of
31696 the way Emacs compares mouse events, you will occasionally wait twice
31697 that time before the window gets selected.)
31698 Any other value means to autoselect window instantaneously when the
31699 mouse pointer enters it.
31701 Autoselection selects the minibuffer only if it is active, and never
31702 unselects the minibuffer if it is active.
31704 When customizing this variable make sure that the actual value of
31705 `focus-follows-mouse' matches the behavior of your window manager. */);
31706 Vmouse_autoselect_window = Qnil;
31708 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31709 doc: /* Non-nil means automatically resize tool-bars.
31710 This dynamically changes the tool-bar's height to the minimum height
31711 that is needed to make all tool-bar items visible.
31712 If value is `grow-only', the tool-bar's height is only increased
31713 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31714 Vauto_resize_tool_bars = Qt;
31716 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31717 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31718 auto_raise_tool_bar_buttons_p = true;
31720 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31721 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31722 make_cursor_line_fully_visible_p = true;
31724 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31725 doc: /* Border below tool-bar in pixels.
31726 If an integer, use it as the height of the border.
31727 If it is one of `internal-border-width' or `border-width', use the
31728 value of the corresponding frame parameter.
31729 Otherwise, no border is added below the tool-bar. */);
31730 Vtool_bar_border = Qinternal_border_width;
31732 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31733 doc: /* Margin around tool-bar buttons in pixels.
31734 If an integer, use that for both horizontal and vertical margins.
31735 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31736 HORZ specifying the horizontal margin, and VERT specifying the
31737 vertical margin. */);
31738 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31740 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31741 doc: /* Relief thickness of tool-bar buttons. */);
31742 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31744 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31745 doc: /* Tool bar style to use.
31746 It can be one of
31747 image - show images only
31748 text - show text only
31749 both - show both, text below image
31750 both-horiz - show text to the right of the image
31751 text-image-horiz - show text to the left of the image
31752 any other - use system default or image if no system default.
31754 This variable only affects the GTK+ toolkit version of Emacs. */);
31755 Vtool_bar_style = Qnil;
31757 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31758 doc: /* Maximum number of characters a label can have to be shown.
31759 The tool bar style must also show labels for this to have any effect, see
31760 `tool-bar-style'. */);
31761 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31763 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31764 doc: /* List of functions to call to fontify regions of text.
31765 Each function is called with one argument POS. Functions must
31766 fontify a region starting at POS in the current buffer, and give
31767 fontified regions the property `fontified'. */);
31768 Vfontification_functions = Qnil;
31769 Fmake_variable_buffer_local (Qfontification_functions);
31771 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31772 unibyte_display_via_language_environment,
31773 doc: /* Non-nil means display unibyte text according to language environment.
31774 Specifically, this means that raw bytes in the range 160-255 decimal
31775 are displayed by converting them to the equivalent multibyte characters
31776 according to the current language environment. As a result, they are
31777 displayed according to the current fontset.
31779 Note that this variable affects only how these bytes are displayed,
31780 but does not change the fact they are interpreted as raw bytes. */);
31781 unibyte_display_via_language_environment = false;
31783 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31784 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31785 If a float, it specifies a fraction of the mini-window frame's height.
31786 If an integer, it specifies a number of lines. */);
31787 Vmax_mini_window_height = make_float (0.25);
31789 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31790 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31791 A value of nil means don't automatically resize mini-windows.
31792 A value of t means resize them to fit the text displayed in them.
31793 A value of `grow-only', the default, means let mini-windows grow only;
31794 they return to their normal size when the minibuffer is closed, or the
31795 echo area becomes empty. */);
31796 /* Contrary to the doc string, we initialize this to nil, so that
31797 loading loadup.el won't try to resize windows before loading
31798 window.el, where some functions we need to call for this live.
31799 We assign the 'grow-only' value right after loading window.el
31800 during loadup. */
31801 Vresize_mini_windows = Qnil;
31803 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31804 doc: /* Alist specifying how to blink the cursor off.
31805 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31806 `cursor-type' frame-parameter or variable equals ON-STATE,
31807 comparing using `equal', Emacs uses OFF-STATE to specify
31808 how to blink it off. ON-STATE and OFF-STATE are values for
31809 the `cursor-type' frame parameter.
31811 If a frame's ON-STATE has no entry in this list,
31812 the frame's other specifications determine how to blink the cursor off. */);
31813 Vblink_cursor_alist = Qnil;
31815 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31816 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31817 If non-nil, windows are automatically scrolled horizontally to make
31818 point visible. */);
31819 automatic_hscrolling_p = true;
31820 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31822 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31823 doc: /* How many columns away from the window edge point is allowed to get
31824 before automatic hscrolling will horizontally scroll the window. */);
31825 hscroll_margin = 5;
31827 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31828 doc: /* How many columns to scroll the window when point gets too close to the edge.
31829 When point is less than `hscroll-margin' columns from the window
31830 edge, automatic hscrolling will scroll the window by the amount of columns
31831 determined by this variable. If its value is a positive integer, scroll that
31832 many columns. If it's a positive floating-point number, it specifies the
31833 fraction of the window's width to scroll. If it's nil or zero, point will be
31834 centered horizontally after the scroll. Any other value, including negative
31835 numbers, are treated as if the value were zero.
31837 Automatic hscrolling always moves point outside the scroll margin, so if
31838 point was more than scroll step columns inside the margin, the window will
31839 scroll more than the value given by the scroll step.
31841 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31842 and `scroll-right' overrides this variable's effect. */);
31843 Vhscroll_step = make_number (0);
31845 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31846 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31847 Bind this around calls to `message' to let it take effect. */);
31848 message_truncate_lines = false;
31850 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31851 doc: /* Normal hook run to update the menu bar definitions.
31852 Redisplay runs this hook before it redisplays the menu bar.
31853 This is used to update menus such as Buffers, whose contents depend on
31854 various data. */);
31855 Vmenu_bar_update_hook = Qnil;
31857 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31858 doc: /* Frame for which we are updating a menu.
31859 The enable predicate for a menu binding should check this variable. */);
31860 Vmenu_updating_frame = Qnil;
31862 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31863 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31864 inhibit_menubar_update = false;
31866 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31867 doc: /* Prefix prepended to all continuation lines at display time.
31868 The value may be a string, an image, or a stretch-glyph; it is
31869 interpreted in the same way as the value of a `display' text property.
31871 This variable is overridden by any `wrap-prefix' text or overlay
31872 property.
31874 To add a prefix to non-continuation lines, use `line-prefix'. */);
31875 Vwrap_prefix = Qnil;
31876 DEFSYM (Qwrap_prefix, "wrap-prefix");
31877 Fmake_variable_buffer_local (Qwrap_prefix);
31879 DEFVAR_LISP ("line-prefix", Vline_prefix,
31880 doc: /* Prefix prepended to all non-continuation lines at display time.
31881 The value may be a string, an image, or a stretch-glyph; it is
31882 interpreted in the same way as the value of a `display' text property.
31884 This variable is overridden by any `line-prefix' text or overlay
31885 property.
31887 To add a prefix to continuation lines, use `wrap-prefix'. */);
31888 Vline_prefix = Qnil;
31889 DEFSYM (Qline_prefix, "line-prefix");
31890 Fmake_variable_buffer_local (Qline_prefix);
31892 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31893 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31894 inhibit_eval_during_redisplay = false;
31896 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31897 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31898 inhibit_free_realized_faces = false;
31900 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31901 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31902 Intended for use during debugging and for testing bidi display;
31903 see biditest.el in the test suite. */);
31904 inhibit_bidi_mirroring = false;
31906 #ifdef GLYPH_DEBUG
31907 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31908 doc: /* Inhibit try_window_id display optimization. */);
31909 inhibit_try_window_id = false;
31911 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31912 doc: /* Inhibit try_window_reusing display optimization. */);
31913 inhibit_try_window_reusing = false;
31915 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31916 doc: /* Inhibit try_cursor_movement display optimization. */);
31917 inhibit_try_cursor_movement = false;
31918 #endif /* GLYPH_DEBUG */
31920 DEFVAR_INT ("overline-margin", overline_margin,
31921 doc: /* Space between overline and text, in pixels.
31922 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31923 margin to the character height. */);
31924 overline_margin = 2;
31926 DEFVAR_INT ("underline-minimum-offset",
31927 underline_minimum_offset,
31928 doc: /* Minimum distance between baseline and underline.
31929 This can improve legibility of underlined text at small font sizes,
31930 particularly when using variable `x-use-underline-position-properties'
31931 with fonts that specify an UNDERLINE_POSITION relatively close to the
31932 baseline. The default value is 1. */);
31933 underline_minimum_offset = 1;
31935 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31936 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31937 This feature only works when on a window system that can change
31938 cursor shapes. */);
31939 display_hourglass_p = true;
31941 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31942 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31943 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31945 #ifdef HAVE_WINDOW_SYSTEM
31946 hourglass_atimer = NULL;
31947 hourglass_shown_p = false;
31948 #endif /* HAVE_WINDOW_SYSTEM */
31950 /* Name of the face used to display glyphless characters. */
31951 DEFSYM (Qglyphless_char, "glyphless-char");
31953 /* Method symbols for Vglyphless_char_display. */
31954 DEFSYM (Qhex_code, "hex-code");
31955 DEFSYM (Qempty_box, "empty-box");
31956 DEFSYM (Qthin_space, "thin-space");
31957 DEFSYM (Qzero_width, "zero-width");
31959 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31960 doc: /* Function run just before redisplay.
31961 It is called with one argument, which is the set of windows that are to
31962 be redisplayed. This set can be nil (meaning, only the selected window),
31963 or t (meaning all windows). */);
31964 Vpre_redisplay_function = intern ("ignore");
31966 /* Symbol for the purpose of Vglyphless_char_display. */
31967 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31968 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31970 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31971 doc: /* Char-table defining glyphless characters.
31972 Each element, if non-nil, should be one of the following:
31973 an ASCII acronym string: display this string in a box
31974 `hex-code': display the hexadecimal code of a character in a box
31975 `empty-box': display as an empty box
31976 `thin-space': display as 1-pixel width space
31977 `zero-width': don't display
31978 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31979 display method for graphical terminals and text terminals respectively.
31980 GRAPHICAL and TEXT should each have one of the values listed above.
31982 The char-table has one extra slot to control the display of a character for
31983 which no font is found. This slot only takes effect on graphical terminals.
31984 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31985 `thin-space'. The default is `empty-box'.
31987 If a character has a non-nil entry in an active display table, the
31988 display table takes effect; in this case, Emacs does not consult
31989 `glyphless-char-display' at all. */);
31990 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31991 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31992 Qempty_box);
31994 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31995 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31996 Vdebug_on_message = Qnil;
31998 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31999 doc: /* */);
32000 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32002 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32003 doc: /* */);
32004 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32006 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32007 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32008 /* Initialize to t, since we need to disable reordering until
32009 loadup.el successfully loads charprop.el. */
32010 redisplay__inhibit_bidi = true;
32014 /* Initialize this module when Emacs starts. */
32016 void
32017 init_xdisp (void)
32019 CHARPOS (this_line_start_pos) = 0;
32021 if (!noninteractive)
32023 struct window *m = XWINDOW (minibuf_window);
32024 Lisp_Object frame = m->frame;
32025 struct frame *f = XFRAME (frame);
32026 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32027 struct window *r = XWINDOW (root);
32028 int i;
32030 echo_area_window = minibuf_window;
32032 r->top_line = FRAME_TOP_MARGIN (f);
32033 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32034 r->total_cols = FRAME_COLS (f);
32035 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32036 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32037 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32039 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32040 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32041 m->total_cols = FRAME_COLS (f);
32042 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32043 m->total_lines = 1;
32044 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32046 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32047 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32048 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32050 /* The default ellipsis glyphs `...'. */
32051 for (i = 0; i < 3; ++i)
32052 default_invis_vector[i] = make_number ('.');
32056 /* Allocate the buffer for frame titles.
32057 Also used for `format-mode-line'. */
32058 int size = 100;
32059 mode_line_noprop_buf = xmalloc (size);
32060 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32061 mode_line_noprop_ptr = mode_line_noprop_buf;
32062 mode_line_target = MODE_LINE_DISPLAY;
32065 help_echo_showing_p = false;
32068 #ifdef HAVE_WINDOW_SYSTEM
32070 /* Platform-independent portion of hourglass implementation. */
32072 /* Timer function of hourglass_atimer. */
32074 static void
32075 show_hourglass (struct atimer *timer)
32077 /* The timer implementation will cancel this timer automatically
32078 after this function has run. Set hourglass_atimer to null
32079 so that we know the timer doesn't have to be canceled. */
32080 hourglass_atimer = NULL;
32082 if (!hourglass_shown_p)
32084 Lisp_Object tail, frame;
32086 block_input ();
32088 FOR_EACH_FRAME (tail, frame)
32090 struct frame *f = XFRAME (frame);
32092 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32093 && FRAME_RIF (f)->show_hourglass)
32094 FRAME_RIF (f)->show_hourglass (f);
32097 hourglass_shown_p = true;
32098 unblock_input ();
32102 /* Cancel a currently active hourglass timer, and start a new one. */
32104 void
32105 start_hourglass (void)
32107 struct timespec delay;
32109 cancel_hourglass ();
32111 if (INTEGERP (Vhourglass_delay)
32112 && XINT (Vhourglass_delay) > 0)
32113 delay = make_timespec (min (XINT (Vhourglass_delay),
32114 TYPE_MAXIMUM (time_t)),
32116 else if (FLOATP (Vhourglass_delay)
32117 && XFLOAT_DATA (Vhourglass_delay) > 0)
32118 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32119 else
32120 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32122 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32123 show_hourglass, NULL);
32126 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32127 shown. */
32129 void
32130 cancel_hourglass (void)
32132 if (hourglass_atimer)
32134 cancel_atimer (hourglass_atimer);
32135 hourglass_atimer = NULL;
32138 if (hourglass_shown_p)
32140 Lisp_Object tail, frame;
32142 block_input ();
32144 FOR_EACH_FRAME (tail, frame)
32146 struct frame *f = XFRAME (frame);
32148 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32149 && FRAME_RIF (f)->hide_hourglass)
32150 FRAME_RIF (f)->hide_hourglass (f);
32151 #ifdef HAVE_NTGUI
32152 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32153 else if (!FRAME_W32_P (f))
32154 w32_arrow_cursor ();
32155 #endif
32158 hourglass_shown_p = false;
32159 unblock_input ();
32163 #endif /* HAVE_WINDOW_SYSTEM */