Stick with debbugs-supported tags in triage
[emacs.git] / src / xdisp.c
blob13af87f953c6cad2fbb83b99f95fc4c733726825
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 <limits.h>
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #include "xwidget.h"
318 #ifdef HAVE_WINDOW_SYSTEM
319 #include TERM_HEADER
320 #endif /* HAVE_WINDOW_SYSTEM */
322 #ifndef FRAME_X_OUTPUT
323 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
324 #endif
326 #define INFINITY 10000000
328 /* Holds the list (error). */
329 static Lisp_Object list_of_error;
331 #ifdef HAVE_WINDOW_SYSTEM
333 /* Test if overflow newline into fringe. Called with iterator IT
334 at or past right window margin, and with IT->current_x set. */
336 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
337 (!NILP (Voverflow_newline_into_fringe) \
338 && FRAME_WINDOW_P ((IT)->f) \
339 && ((IT)->bidi_it.paragraph_dir == R2L \
340 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
341 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
342 && (IT)->current_x == (IT)->last_visible_x)
344 #else /* !HAVE_WINDOW_SYSTEM */
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
346 #endif /* HAVE_WINDOW_SYSTEM */
348 /* Test if the display element loaded in IT, or the underlying buffer
349 or string character, is a space or a TAB character. This is used
350 to determine where word wrapping can occur. */
352 #define IT_DISPLAYING_WHITESPACE(it) \
353 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
354 || ((STRINGP (it->string) \
355 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
356 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
357 || (it->s \
358 && (it->s[IT_BYTEPOS (*it)] == ' ' \
359 || it->s[IT_BYTEPOS (*it)] == '\t')) \
360 || (IT_BYTEPOS (*it) < ZV_BYTE \
361 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
362 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
364 /* True means print newline to stdout before next mini-buffer message. */
366 bool noninteractive_need_newline;
368 /* True means print newline to message log before next message. */
370 static bool message_log_need_newline;
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
384 static struct text_pos this_line_start_pos;
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
389 static struct text_pos this_line_end_pos;
391 /* The vertical positions and the height of this line. */
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
400 static int this_line_start_x;
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
406 static struct text_pos this_line_min_pos;
408 /* Buffer that this_line_.* variables are referring to. */
410 static struct buffer *this_line_buffer;
412 /* True if an overlay arrow has been displayed in this window. */
414 static bool overlay_arrow_seen;
416 /* Vector containing glyphs for an ellipsis `...'. */
418 static Lisp_Object default_invis_vector[3];
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
424 Lisp_Object echo_area_window;
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
431 static Lisp_Object Vmessage_stack;
433 /* True means multibyte characters were enabled when the echo area
434 message was specified. */
436 static bool message_enable_multibyte;
438 /* At each redisplay cycle, we should refresh everything there is to refresh.
439 To do that efficiently, we use many optimizations that try to make sure we
440 don't waste too much time updating things that haven't changed.
441 The coarsest such optimization is that, in the most common cases, we only
442 look at the selected-window.
444 To know whether other windows should be considered for redisplay, we use the
445 variable windows_or_buffers_changed: as long as it is 0, it means that we
446 have not noticed anything that should require updating anything else than
447 the selected-window. If it is set to REDISPLAY_SOME, it means that since
448 last redisplay, some changes have been made which could impact other
449 windows. To know which ones need redisplay, every buffer, window, and frame
450 has a `redisplay' bit, which (if true) means that this object needs to be
451 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
452 looking for those `redisplay' bits (actually, there might be some such bits
453 set, but then only on objects which aren't displayed anyway).
455 OTOH if it's non-zero we wil have to loop through all windows and then check
456 the `redisplay' bit of the corresponding window, frame, and buffer, in order
457 to decide whether that window needs attention or not. Note that we can't
458 just look at the frame's redisplay bit to decide that the whole frame can be
459 skipped, since even if the frame's redisplay bit is unset, some of its
460 windows's redisplay bits may be set.
462 Mostly for historical reasons, windows_or_buffers_changed can also take
463 other non-zero values. In that case, the precise value doesn't matter (it
464 encodes the cause of the setting but is only used for debugging purposes),
465 and what it means is that we shouldn't pay attention to any `redisplay' bits
466 and we should simply try and redisplay every window out there. */
468 int windows_or_buffers_changed;
470 /* Nonzero if we should redraw the mode lines on the next redisplay.
471 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
472 then only redisplay the mode lines in those buffers/windows/frames where the
473 `redisplay' bit has been set.
474 For any other value, redisplay all mode lines (the number used is then only
475 used to track down the cause for this full-redisplay).
477 Since the frame title uses the same %-constructs as the mode line
478 (except %c and %l), if this variable is non-zero, we also consider
479 redisplaying the title of each frame, see x_consider_frame_title.
481 The `redisplay' bits are the same as those used for
482 windows_or_buffers_changed, and setting windows_or_buffers_changed also
483 causes recomputation of the mode lines of all those windows. IOW this
484 variable only has an effect if windows_or_buffers_changed is zero, in which
485 case we should only need to redisplay the mode-line of those objects with
486 a `redisplay' bit set but not the window's text content (tho we may still
487 need to refresh the text content of the selected-window). */
489 int update_mode_lines;
491 /* True after display_mode_line if %l was used and it displayed a
492 line number. */
494 static bool line_number_displayed;
496 /* The name of the *Messages* buffer, a string. */
498 static Lisp_Object Vmessages_buffer_name;
500 /* Current, index 0, and last displayed echo area message. Either
501 buffers from echo_buffers, or nil to indicate no message. */
503 Lisp_Object echo_area_buffer[2];
505 /* The buffers referenced from echo_area_buffer. */
507 static Lisp_Object echo_buffer[2];
509 /* A vector saved used in with_area_buffer to reduce consing. */
511 static Lisp_Object Vwith_echo_area_save_vector;
513 /* True means display_echo_area should display the last echo area
514 message again. Set by redisplay_preserve_echo_area. */
516 static bool display_last_displayed_message_p;
518 /* True if echo area is being used by print; false if being used by
519 message. */
521 static bool message_buf_print;
523 /* Set to true in clear_message to make redisplay_internal aware
524 of an emptied echo area. */
526 static bool message_cleared_p;
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
531 #define MAX_SCRATCH_GLYPHS 100
532 static struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
535 /* Ascent and height of the last line processed by move_it_to. */
537 static int last_height;
539 /* True if there's a help-echo in the echo area. */
541 bool help_echo_showing_p;
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
549 #define TEXT_PROP_DISTANCE_LIMIT 100
551 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
552 iterator state and later restore it. This is needed because the
553 bidi iterator on bidi.c keeps a stacked cache of its states, which
554 is really a singleton. When we use scratch iterator objects to
555 move around the buffer, we can cause the bidi cache to be pushed or
556 popped, and therefore we need to restore the cache state when we
557 return to the original iterator. */
558 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
559 do { \
560 if (CACHE) \
561 bidi_unshelve_cache (CACHE, true); \
562 ITCOPY = ITORIG; \
563 CACHE = bidi_shelve_cache (); \
564 } while (false)
566 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
567 do { \
568 if (pITORIG != pITCOPY) \
569 *(pITORIG) = *(pITCOPY); \
570 bidi_unshelve_cache (CACHE, false); \
571 CACHE = NULL; \
572 } while (false)
574 /* Functions to mark elements as needing redisplay. */
575 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
577 void
578 redisplay_other_windows (void)
580 if (!windows_or_buffers_changed)
581 windows_or_buffers_changed = REDISPLAY_SOME;
584 void
585 wset_redisplay (struct window *w)
587 /* Beware: selected_window can be nil during early stages. */
588 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
589 redisplay_other_windows ();
590 w->redisplay = true;
593 void
594 fset_redisplay (struct frame *f)
596 redisplay_other_windows ();
597 f->redisplay = true;
600 void
601 bset_redisplay (struct buffer *b)
603 int count = buffer_window_count (b);
604 if (count > 0)
606 /* ... it's visible in other window than selected, */
607 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
608 redisplay_other_windows ();
609 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
610 so that if we later set windows_or_buffers_changed, this buffer will
611 not be omitted. */
612 b->text->redisplay = true;
616 void
617 bset_update_mode_line (struct buffer *b)
619 if (!update_mode_lines)
620 update_mode_lines = REDISPLAY_SOME;
621 b->text->redisplay = true;
624 void
625 maybe_set_redisplay (Lisp_Object symbol)
627 if (HASH_TABLE_P (Vredisplay__variables)
628 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
630 bset_update_mode_line (current_buffer);
631 current_buffer->prevent_redisplay_optimizations_p = true;
635 #ifdef GLYPH_DEBUG
637 /* True means print traces of redisplay if compiled with
638 GLYPH_DEBUG defined. */
640 bool trace_redisplay_p;
642 #endif /* GLYPH_DEBUG */
644 #ifdef DEBUG_TRACE_MOVE
645 /* True means trace with TRACE_MOVE to stderr. */
646 static bool trace_move;
648 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
649 #else
650 #define TRACE_MOVE(x) (void) 0
651 #endif
653 /* Buffer being redisplayed -- for redisplay_window_error. */
655 static struct buffer *displayed_buffer;
657 /* Value returned from text property handlers (see below). */
659 enum prop_handled
661 HANDLED_NORMALLY,
662 HANDLED_RECOMPUTE_PROPS,
663 HANDLED_OVERLAY_STRING_CONSUMED,
664 HANDLED_RETURN
667 /* A description of text properties that redisplay is interested
668 in. */
670 struct props
672 /* The symbol index of the name of the property. */
673 short name;
675 /* A unique index for the property. */
676 enum prop_idx idx;
678 /* A handler function called to set up iterator IT from the property
679 at IT's current position. Value is used to steer handle_stop. */
680 enum prop_handled (*handler) (struct it *it);
683 static enum prop_handled handle_face_prop (struct it *);
684 static enum prop_handled handle_invisible_prop (struct it *);
685 static enum prop_handled handle_display_prop (struct it *);
686 static enum prop_handled handle_composition_prop (struct it *);
687 static enum prop_handled handle_overlay_change (struct it *);
688 static enum prop_handled handle_fontified_prop (struct it *);
690 /* Properties handled by iterators. */
692 static struct props it_props[] =
694 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
695 /* Handle `face' before `display' because some sub-properties of
696 `display' need to know the face. */
697 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
698 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
699 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
700 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
701 {0, 0, NULL}
704 /* Value is the position described by X. If X is a marker, value is
705 the marker_position of X. Otherwise, value is X. */
707 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
709 /* Enumeration returned by some move_it_.* functions internally. */
711 enum move_it_result
713 /* Not used. Undefined value. */
714 MOVE_UNDEFINED,
716 /* Move ended at the requested buffer position or ZV. */
717 MOVE_POS_MATCH_OR_ZV,
719 /* Move ended at the requested X pixel position. */
720 MOVE_X_REACHED,
722 /* Move within a line ended at the end of a line that must be
723 continued. */
724 MOVE_LINE_CONTINUED,
726 /* Move within a line ended at the end of a line that would
727 be displayed truncated. */
728 MOVE_LINE_TRUNCATED,
730 /* Move within a line ended at a line end. */
731 MOVE_NEWLINE_OR_CR
734 /* This counter is used to clear the face cache every once in a while
735 in redisplay_internal. It is incremented for each redisplay.
736 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
737 cleared. */
739 #define CLEAR_FACE_CACHE_COUNT 500
740 static int clear_face_cache_count;
742 /* Similarly for the image cache. */
744 #ifdef HAVE_WINDOW_SYSTEM
745 #define CLEAR_IMAGE_CACHE_COUNT 101
746 static int clear_image_cache_count;
748 /* Null glyph slice */
749 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
750 #endif
752 /* True while redisplay_internal is in progress. */
754 bool redisplaying_p;
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
764 /* Temporary variable for XTread_socket. */
766 Lisp_Object previous_help_echo_string;
768 /* Platform-independent portion of hourglass implementation. */
770 #ifdef HAVE_WINDOW_SYSTEM
772 /* True means an hourglass cursor is currently shown. */
773 static bool hourglass_shown_p;
775 /* If non-null, an asynchronous timer that, when it expires, displays
776 an hourglass cursor on all frames. */
777 static struct atimer *hourglass_atimer;
779 #endif /* HAVE_WINDOW_SYSTEM */
781 /* Default number of seconds to wait before displaying an hourglass
782 cursor. */
783 #define DEFAULT_HOURGLASS_DELAY 1
785 #ifdef HAVE_WINDOW_SYSTEM
787 /* Default pixel width of `thin-space' display method. */
788 #define THIN_SPACE_WIDTH 1
790 #endif /* HAVE_WINDOW_SYSTEM */
792 /* Function prototypes. */
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, bool);
796 static void mark_window_display_accurate_1 (struct window *, bool);
797 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static bool cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, bool);
801 static void handle_line_prefix (struct it *);
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void unwind_with_echo_area_buffer (Lisp_Object);
805 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
806 static bool current_message_1 (ptrdiff_t, Lisp_Object);
807 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
808 static void set_message (Lisp_Object);
809 static bool set_message_1 (ptrdiff_t, Lisp_Object);
810 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
811 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
812 static void unwind_redisplay (void);
813 static void extend_face_to_end_of_line (struct it *);
814 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
815 static void push_it (struct it *, struct text_pos *);
816 static void iterate_out_of_display_property (struct it *);
817 static void pop_it (struct it *);
818 static void redisplay_internal (void);
819 static void echo_area_display (bool);
820 static void redisplay_windows (Lisp_Object);
821 static void redisplay_window (Lisp_Object, bool);
822 static Lisp_Object redisplay_window_error (Lisp_Object);
823 static Lisp_Object redisplay_window_0 (Lisp_Object);
824 static Lisp_Object redisplay_window_1 (Lisp_Object);
825 static bool set_cursor_from_row (struct window *, struct glyph_row *,
826 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
827 int, int);
828 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
829 static bool update_menu_bar (struct frame *, bool, bool);
830 static bool try_window_reusing_current_matrix (struct window *);
831 static int try_window_id (struct window *);
832 static bool display_line (struct it *);
833 static int display_mode_lines (struct window *);
834 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
835 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
836 Lisp_Object, bool);
837 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
838 Lisp_Object);
839 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
840 static void display_menu_bar (struct window *);
841 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
842 ptrdiff_t *);
843 static int display_string (const char *, Lisp_Object, Lisp_Object,
844 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
845 static void compute_line_metrics (struct it *);
846 static void run_redisplay_end_trigger_hook (struct it *);
847 static bool get_overlay_strings (struct it *, ptrdiff_t);
848 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
849 static void next_overlay_string (struct it *);
850 static void reseat (struct it *, struct text_pos, bool);
851 static void reseat_1 (struct it *, struct text_pos, bool);
852 static bool next_element_from_display_vector (struct it *);
853 static bool next_element_from_string (struct it *);
854 static bool next_element_from_c_string (struct it *);
855 static bool next_element_from_buffer (struct it *);
856 static bool next_element_from_composition (struct it *);
857 static bool next_element_from_image (struct it *);
858 static bool next_element_from_stretch (struct it *);
859 static bool next_element_from_xwidget (struct it *);
860 static void load_overlay_strings (struct it *, ptrdiff_t);
861 static bool get_next_display_element (struct it *);
862 static enum move_it_result
863 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
864 enum move_operation_enum);
865 static void get_visually_first_element (struct it *);
866 static void compute_stop_pos (struct it *);
867 static int face_before_or_after_it_pos (struct it *, bool);
868 static ptrdiff_t next_overlay_change (ptrdiff_t);
869 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
870 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
871 static int handle_single_display_spec (struct it *, Lisp_Object,
872 Lisp_Object, Lisp_Object,
873 struct text_pos *, ptrdiff_t, int, bool);
874 static int underlying_face_id (struct it *);
876 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
877 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
879 #ifdef HAVE_WINDOW_SYSTEM
881 static void update_tool_bar (struct frame *, bool);
882 static void x_draw_bottom_divider (struct window *w);
883 static void notice_overwritten_cursor (struct window *,
884 enum glyph_row_area,
885 int, int, int, int);
886 static int normal_char_height (struct font *, int);
887 static void normal_char_ascent_descent (struct font *, int, int *, int *);
889 static void append_stretch_glyph (struct it *, Lisp_Object,
890 int, int, int);
892 static Lisp_Object get_it_property (struct it *, Lisp_Object);
893 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
894 struct font *, int, bool);
896 #endif /* HAVE_WINDOW_SYSTEM */
898 static void produce_special_glyphs (struct it *, enum display_element_type);
899 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
900 static bool coords_in_mouse_face_p (struct window *, int, int);
904 /***********************************************************************
905 Window display dimensions
906 ***********************************************************************/
908 /* Return the bottom boundary y-position for text lines in window W.
909 This is the first y position at which a line cannot start.
910 It is relative to the top of the window.
912 This is the height of W minus the height of a mode line, if any. */
915 window_text_bottom_y (struct window *w)
917 int height = WINDOW_PIXEL_HEIGHT (w);
919 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
921 if (WINDOW_WANTS_MODELINE_P (w))
922 height -= CURRENT_MODE_LINE_HEIGHT (w);
924 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
926 return height;
929 /* Return the pixel width of display area AREA of window W.
930 ANY_AREA means return the total width of W, not including
931 fringes to the left and right of the window. */
934 window_box_width (struct window *w, enum glyph_row_area area)
936 int width = w->pixel_width;
938 if (!w->pseudo_window_p)
940 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
941 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
943 if (area == TEXT_AREA)
944 width -= (WINDOW_MARGINS_WIDTH (w)
945 + WINDOW_FRINGES_WIDTH (w));
946 else if (area == LEFT_MARGIN_AREA)
947 width = WINDOW_LEFT_MARGIN_WIDTH (w);
948 else if (area == RIGHT_MARGIN_AREA)
949 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
952 /* With wide margins, fringes, etc. we might end up with a negative
953 width, correct that here. */
954 return max (0, width);
958 /* Return the pixel height of the display area of window W, not
959 including mode lines of W, if any. */
962 window_box_height (struct window *w)
964 struct frame *f = XFRAME (w->frame);
965 int height = WINDOW_PIXEL_HEIGHT (w);
967 eassert (height >= 0);
969 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
970 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
972 /* Note: the code below that determines the mode-line/header-line
973 height is essentially the same as that contained in the macro
974 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
975 the appropriate glyph row has its `mode_line_p' flag set,
976 and if it doesn't, uses estimate_mode_line_height instead. */
978 if (WINDOW_WANTS_MODELINE_P (w))
980 struct glyph_row *ml_row
981 = (w->current_matrix && w->current_matrix->rows
982 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
983 : 0);
984 if (ml_row && ml_row->mode_line_p)
985 height -= ml_row->height;
986 else
987 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
990 if (WINDOW_WANTS_HEADER_LINE_P (w))
992 struct glyph_row *hl_row
993 = (w->current_matrix && w->current_matrix->rows
994 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
995 : 0);
996 if (hl_row && hl_row->mode_line_p)
997 height -= hl_row->height;
998 else
999 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1002 /* With a very small font and a mode-line that's taller than
1003 default, we might end up with a negative height. */
1004 return max (0, height);
1007 /* Return the window-relative coordinate of the left edge of display
1008 area AREA of window W. ANY_AREA means return the left edge of the
1009 whole window, to the right of the left fringe of W. */
1012 window_box_left_offset (struct window *w, enum glyph_row_area area)
1014 int x;
1016 if (w->pseudo_window_p)
1017 return 0;
1019 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1021 if (area == TEXT_AREA)
1022 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1023 + window_box_width (w, LEFT_MARGIN_AREA));
1024 else if (area == RIGHT_MARGIN_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA)
1027 + window_box_width (w, TEXT_AREA)
1028 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1030 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1031 else if (area == LEFT_MARGIN_AREA
1032 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1033 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1035 /* Don't return more than the window's pixel width. */
1036 return min (x, w->pixel_width);
1040 /* Return the window-relative coordinate of the right edge of display
1041 area AREA of window W. ANY_AREA means return the right edge of the
1042 whole window, to the left of the right fringe of W. */
1044 static int
1045 window_box_right_offset (struct window *w, enum glyph_row_area area)
1047 /* Don't return more than the window's pixel width. */
1048 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1049 w->pixel_width);
1052 /* Return the frame-relative coordinate of the left edge of display
1053 area AREA of window W. ANY_AREA means return the left edge of the
1054 whole window, to the right of the left fringe of W. */
1057 window_box_left (struct window *w, enum glyph_row_area area)
1059 struct frame *f = XFRAME (w->frame);
1060 int x;
1062 if (w->pseudo_window_p)
1063 return FRAME_INTERNAL_BORDER_WIDTH (f);
1065 x = (WINDOW_LEFT_EDGE_X (w)
1066 + window_box_left_offset (w, area));
1068 return x;
1072 /* Return the frame-relative coordinate of the right edge of display
1073 area AREA of window W. ANY_AREA means return the right edge of the
1074 whole window, to the left of the right fringe of W. */
1077 window_box_right (struct window *w, enum glyph_row_area area)
1079 return window_box_left (w, area) + window_box_width (w, area);
1082 /* Get the bounding box of the display area AREA of window W, without
1083 mode lines, in frame-relative coordinates. ANY_AREA means the
1084 whole window, not including the left and right fringes of
1085 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1086 coordinates of the upper-left corner of the box. Return in
1087 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1089 void
1090 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1091 int *box_y, int *box_width, int *box_height)
1093 if (box_width)
1094 *box_width = window_box_width (w, area);
1095 if (box_height)
1096 *box_height = window_box_height (w);
1097 if (box_x)
1098 *box_x = window_box_left (w, area);
1099 if (box_y)
1101 *box_y = WINDOW_TOP_EDGE_Y (w);
1102 if (WINDOW_WANTS_HEADER_LINE_P (w))
1103 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1107 #ifdef HAVE_WINDOW_SYSTEM
1109 /* Get the bounding box of the display area AREA of window W, without
1110 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1111 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1112 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1113 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1114 box. */
1116 static void
1117 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1118 int *bottom_right_x, int *bottom_right_y)
1120 window_box (w, ANY_AREA, top_left_x, top_left_y,
1121 bottom_right_x, bottom_right_y);
1122 *bottom_right_x += *top_left_x;
1123 *bottom_right_y += *top_left_y;
1126 #endif /* HAVE_WINDOW_SYSTEM */
1128 /***********************************************************************
1129 Utilities
1130 ***********************************************************************/
1132 /* Return the bottom y-position of the line the iterator IT is in.
1133 This can modify IT's settings. */
1136 line_bottom_y (struct it *it)
1138 int line_height = it->max_ascent + it->max_descent;
1139 int line_top_y = it->current_y;
1141 if (line_height == 0)
1143 if (last_height)
1144 line_height = last_height;
1145 else if (IT_CHARPOS (*it) < ZV)
1147 move_it_by_lines (it, 1);
1148 line_height = (it->max_ascent || it->max_descent
1149 ? it->max_ascent + it->max_descent
1150 : last_height);
1152 else
1154 struct glyph_row *row = it->glyph_row;
1156 /* Use the default character height. */
1157 it->glyph_row = NULL;
1158 it->what = IT_CHARACTER;
1159 it->c = ' ';
1160 it->len = 1;
1161 PRODUCE_GLYPHS (it);
1162 line_height = it->ascent + it->descent;
1163 it->glyph_row = row;
1167 return line_top_y + line_height;
1170 DEFUN ("line-pixel-height", Fline_pixel_height,
1171 Sline_pixel_height, 0, 0, 0,
1172 doc: /* Return height in pixels of text line in the selected window.
1174 Value is the height in pixels of the line at point. */)
1175 (void)
1177 struct it it;
1178 struct text_pos pt;
1179 struct window *w = XWINDOW (selected_window);
1180 struct buffer *old_buffer = NULL;
1181 Lisp_Object result;
1183 if (XBUFFER (w->contents) != current_buffer)
1185 old_buffer = current_buffer;
1186 set_buffer_internal_1 (XBUFFER (w->contents));
1188 SET_TEXT_POS (pt, PT, PT_BYTE);
1189 start_display (&it, w, pt);
1190 it.vpos = it.current_y = 0;
1191 last_height = 0;
1192 result = make_number (line_bottom_y (&it));
1193 if (old_buffer)
1194 set_buffer_internal_1 (old_buffer);
1196 return result;
1199 /* Return the default pixel height of text lines in window W. The
1200 value is the canonical height of the W frame's default font, plus
1201 any extra space required by the line-spacing variable or frame
1202 parameter.
1204 Implementation note: this ignores any line-spacing text properties
1205 put on the newline characters. This is because those properties
1206 only affect the _screen_ line ending in the newline (i.e., in a
1207 continued line, only the last screen line will be affected), which
1208 means only a small number of lines in a buffer can ever use this
1209 feature. Since this function is used to compute the default pixel
1210 equivalent of text lines in a window, we can safely ignore those
1211 few lines. For the same reasons, we ignore the line-height
1212 properties. */
1214 default_line_pixel_height (struct window *w)
1216 struct frame *f = WINDOW_XFRAME (w);
1217 int height = FRAME_LINE_HEIGHT (f);
1219 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1221 struct buffer *b = XBUFFER (w->contents);
1222 Lisp_Object val = BVAR (b, extra_line_spacing);
1224 if (NILP (val))
1225 val = BVAR (&buffer_defaults, extra_line_spacing);
1226 if (!NILP (val))
1228 if (RANGED_INTEGERP (0, val, INT_MAX))
1229 height += XFASTINT (val);
1230 else if (FLOATP (val))
1232 int addon = XFLOAT_DATA (val) * height + 0.5;
1234 if (addon >= 0)
1235 height += addon;
1238 else
1239 height += f->extra_line_spacing;
1242 return height;
1245 /* Subroutine of pos_visible_p below. Extracts a display string, if
1246 any, from the display spec given as its argument. */
1247 static Lisp_Object
1248 string_from_display_spec (Lisp_Object spec)
1250 if (CONSP (spec))
1252 while (CONSP (spec))
1254 if (STRINGP (XCAR (spec)))
1255 return XCAR (spec);
1256 spec = XCDR (spec);
1259 else if (VECTORP (spec))
1261 ptrdiff_t i;
1263 for (i = 0; i < ASIZE (spec); i++)
1265 if (STRINGP (AREF (spec, i)))
1266 return AREF (spec, i);
1268 return Qnil;
1271 return spec;
1275 /* Limit insanely large values of W->hscroll on frame F to the largest
1276 value that will still prevent first_visible_x and last_visible_x of
1277 'struct it' from overflowing an int. */
1278 static int
1279 window_hscroll_limited (struct window *w, struct frame *f)
1281 ptrdiff_t window_hscroll = w->hscroll;
1282 int window_text_width = window_box_width (w, TEXT_AREA);
1283 int colwidth = FRAME_COLUMN_WIDTH (f);
1285 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1286 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1288 return window_hscroll;
1291 /* Return true if position CHARPOS is visible in window W.
1292 CHARPOS < 0 means return info about WINDOW_END position.
1293 If visible, set *X and *Y to pixel coordinates of top left corner.
1294 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1295 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1297 bool
1298 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1299 int *rtop, int *rbot, int *rowh, int *vpos)
1301 struct it it;
1302 void *itdata = bidi_shelve_cache ();
1303 struct text_pos top;
1304 bool visible_p = false;
1305 struct buffer *old_buffer = NULL;
1306 bool r2l = false;
1308 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1309 return visible_p;
1311 if (XBUFFER (w->contents) != current_buffer)
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->contents));
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318 /* Scrolling a minibuffer window via scroll bar when the echo area
1319 shows long text sometimes resets the minibuffer contents behind
1320 our backs. Also, someone might narrow-to-region and immediately
1321 call a scroll function. */
1322 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1325 /* If the top of the window is after CHARPOS, the latter is surely
1326 not visible. */
1327 if (charpos >= 0 && CHARPOS (top) > charpos)
1328 return visible_p;
1330 /* Compute exact mode line heights. */
1331 if (WINDOW_WANTS_MODELINE_P (w))
1332 w->mode_line_height
1333 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1334 BVAR (current_buffer, mode_line_format));
1336 if (WINDOW_WANTS_HEADER_LINE_P (w))
1337 w->header_line_height
1338 = display_mode_line (w, HEADER_LINE_FACE_ID,
1339 BVAR (current_buffer, header_line_format));
1341 start_display (&it, w, top);
1342 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1343 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1345 if (charpos >= 0
1346 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1347 && IT_CHARPOS (it) >= charpos)
1348 /* When scanning backwards under bidi iteration, move_it_to
1349 stops at or _before_ CHARPOS, because it stops at or to
1350 the _right_ of the character at CHARPOS. */
1351 || (it.bidi_p && it.bidi_it.scan_dir == -1
1352 && IT_CHARPOS (it) <= charpos)))
1354 /* We have reached CHARPOS, or passed it. How the call to
1355 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1356 or covered by a display property, move_it_to stops at the end
1357 of the invisible text, to the right of CHARPOS. (ii) If
1358 CHARPOS is in a display vector, move_it_to stops on its last
1359 glyph. */
1360 int top_x = it.current_x;
1361 int top_y = it.current_y;
1362 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1363 int bottom_y;
1364 struct it save_it;
1365 void *save_it_data = NULL;
1367 /* Calling line_bottom_y may change it.method, it.position, etc. */
1368 SAVE_IT (save_it, it, save_it_data);
1369 last_height = 0;
1370 bottom_y = line_bottom_y (&it);
1371 if (top_y < window_top_y)
1372 visible_p = bottom_y > window_top_y;
1373 else if (top_y < it.last_visible_y)
1374 visible_p = true;
1375 if (bottom_y >= it.last_visible_y
1376 && it.bidi_p && it.bidi_it.scan_dir == -1
1377 && IT_CHARPOS (it) < charpos)
1379 /* When the last line of the window is scanned backwards
1380 under bidi iteration, we could be duped into thinking
1381 that we have passed CHARPOS, when in fact move_it_to
1382 simply stopped short of CHARPOS because it reached
1383 last_visible_y. To see if that's what happened, we call
1384 move_it_to again with a slightly larger vertical limit,
1385 and see if it actually moved vertically; if it did, we
1386 didn't really reach CHARPOS, which is beyond window end. */
1387 /* Why 10? because we don't know how many canonical lines
1388 will the height of the next line(s) be. So we guess. */
1389 int ten_more_lines = 10 * default_line_pixel_height (w);
1391 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1392 MOVE_TO_POS | MOVE_TO_Y);
1393 if (it.current_y > top_y)
1394 visible_p = false;
1397 RESTORE_IT (&it, &save_it, save_it_data);
1398 if (visible_p)
1400 if (it.method == GET_FROM_DISPLAY_VECTOR)
1402 /* We stopped on the last glyph of a display vector.
1403 Try and recompute. Hack alert! */
1404 if (charpos < 2 || top.charpos >= charpos)
1405 top_x = it.glyph_row->x;
1406 else
1408 struct it it2, it2_prev;
1409 /* The idea is to get to the previous buffer
1410 position, consume the character there, and use
1411 the pixel coordinates we get after that. But if
1412 the previous buffer position is also displayed
1413 from a display vector, we need to consume all of
1414 the glyphs from that display vector. */
1415 start_display (&it2, w, top);
1416 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1417 /* If we didn't get to CHARPOS - 1, there's some
1418 replacing display property at that position, and
1419 we stopped after it. That is exactly the place
1420 whose coordinates we want. */
1421 if (IT_CHARPOS (it2) != charpos - 1)
1422 it2_prev = it2;
1423 else
1425 /* Iterate until we get out of the display
1426 vector that displays the character at
1427 CHARPOS - 1. */
1428 do {
1429 get_next_display_element (&it2);
1430 PRODUCE_GLYPHS (&it2);
1431 it2_prev = it2;
1432 set_iterator_to_next (&it2, true);
1433 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1434 && IT_CHARPOS (it2) < charpos);
1436 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1437 || it2_prev.current_x > it2_prev.last_visible_x)
1438 top_x = it.glyph_row->x;
1439 else
1441 top_x = it2_prev.current_x;
1442 top_y = it2_prev.current_y;
1446 else if (IT_CHARPOS (it) != charpos)
1448 Lisp_Object cpos = make_number (charpos);
1449 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1450 Lisp_Object string = string_from_display_spec (spec);
1451 struct text_pos tpos;
1452 bool newline_in_string
1453 = (STRINGP (string)
1454 && memchr (SDATA (string), '\n', SBYTES (string)));
1456 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1457 bool replacing_spec_p
1458 = (!NILP (spec)
1459 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1460 charpos, FRAME_WINDOW_P (it.f)));
1461 /* The tricky code below is needed because there's a
1462 discrepancy between move_it_to and how we set cursor
1463 when PT is at the beginning of a portion of text
1464 covered by a display property or an overlay with a
1465 display property, or the display line ends in a
1466 newline from a display string. move_it_to will stop
1467 _after_ such display strings, whereas
1468 set_cursor_from_row conspires with cursor_row_p to
1469 place the cursor on the first glyph produced from the
1470 display string. */
1472 /* We have overshoot PT because it is covered by a
1473 display property that replaces the text it covers.
1474 If the string includes embedded newlines, we are also
1475 in the wrong display line. Backtrack to the correct
1476 line, where the display property begins. */
1477 if (replacing_spec_p)
1479 Lisp_Object startpos, endpos;
1480 EMACS_INT start, end;
1481 struct it it3;
1483 /* Find the first and the last buffer positions
1484 covered by the display string. */
1485 endpos =
1486 Fnext_single_char_property_change (cpos, Qdisplay,
1487 Qnil, Qnil);
1488 startpos =
1489 Fprevious_single_char_property_change (endpos, Qdisplay,
1490 Qnil, Qnil);
1491 start = XFASTINT (startpos);
1492 end = XFASTINT (endpos);
1493 /* Move to the last buffer position before the
1494 display property. */
1495 start_display (&it3, w, top);
1496 if (start > CHARPOS (top))
1497 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1498 /* Move forward one more line if the position before
1499 the display string is a newline or if it is the
1500 rightmost character on a line that is
1501 continued or word-wrapped. */
1502 if (it3.method == GET_FROM_BUFFER
1503 && (it3.c == '\n'
1504 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1505 move_it_by_lines (&it3, 1);
1506 else if (move_it_in_display_line_to (&it3, -1,
1507 it3.current_x
1508 + it3.pixel_width,
1509 MOVE_TO_X)
1510 == MOVE_LINE_CONTINUED)
1512 move_it_by_lines (&it3, 1);
1513 /* When we are under word-wrap, the #$@%!
1514 move_it_by_lines moves 2 lines, so we need to
1515 fix that up. */
1516 if (it3.line_wrap == WORD_WRAP)
1517 move_it_by_lines (&it3, -1);
1520 /* Record the vertical coordinate of the display
1521 line where we wound up. */
1522 top_y = it3.current_y;
1523 if (it3.bidi_p)
1525 /* When characters are reordered for display,
1526 the character displayed to the left of the
1527 display string could be _after_ the display
1528 property in the logical order. Use the
1529 smallest vertical position of these two. */
1530 start_display (&it3, w, top);
1531 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1532 if (it3.current_y < top_y)
1533 top_y = it3.current_y;
1535 /* Move from the top of the window to the beginning
1536 of the display line where the display string
1537 begins. */
1538 start_display (&it3, w, top);
1539 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1540 /* If it3_moved stays false after the 'while' loop
1541 below, that means we already were at a newline
1542 before the loop (e.g., the display string begins
1543 with a newline), so we don't need to (and cannot)
1544 inspect the glyphs of it3.glyph_row, because
1545 PRODUCE_GLYPHS will not produce anything for a
1546 newline, and thus it3.glyph_row stays at its
1547 stale content it got at top of the window. */
1548 bool it3_moved = false;
1549 /* Finally, advance the iterator until we hit the
1550 first display element whose character position is
1551 CHARPOS, or until the first newline from the
1552 display string, which signals the end of the
1553 display line. */
1554 while (get_next_display_element (&it3))
1556 PRODUCE_GLYPHS (&it3);
1557 if (IT_CHARPOS (it3) == charpos
1558 || ITERATOR_AT_END_OF_LINE_P (&it3))
1559 break;
1560 it3_moved = true;
1561 set_iterator_to_next (&it3, false);
1563 top_x = it3.current_x - it3.pixel_width;
1564 /* Normally, we would exit the above loop because we
1565 found the display element whose character
1566 position is CHARPOS. For the contingency that we
1567 didn't, and stopped at the first newline from the
1568 display string, move back over the glyphs
1569 produced from the string, until we find the
1570 rightmost glyph not from the string. */
1571 if (it3_moved
1572 && newline_in_string
1573 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1575 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1576 + it3.glyph_row->used[TEXT_AREA];
1578 while (EQ ((g - 1)->object, string))
1580 --g;
1581 top_x -= g->pixel_width;
1583 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1584 + it3.glyph_row->used[TEXT_AREA]);
1589 *x = top_x;
1590 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1591 *rtop = max (0, window_top_y - top_y);
1592 *rbot = max (0, bottom_y - it.last_visible_y);
1593 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1594 - max (top_y, window_top_y)));
1595 *vpos = it.vpos;
1596 if (it.bidi_it.paragraph_dir == R2L)
1597 r2l = true;
1600 else
1602 /* Either we were asked to provide info about WINDOW_END, or
1603 CHARPOS is in the partially visible glyph row at end of
1604 window. */
1605 struct it it2;
1606 void *it2data = NULL;
1608 SAVE_IT (it2, it, it2data);
1609 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1610 move_it_by_lines (&it, 1);
1611 if (charpos < IT_CHARPOS (it)
1612 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1614 visible_p = true;
1615 RESTORE_IT (&it2, &it2, it2data);
1616 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1617 *x = it2.current_x;
1618 *y = it2.current_y + it2.max_ascent - it2.ascent;
1619 *rtop = max (0, -it2.current_y);
1620 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1621 - it.last_visible_y));
1622 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1623 it.last_visible_y)
1624 - max (it2.current_y,
1625 WINDOW_HEADER_LINE_HEIGHT (w))));
1626 *vpos = it2.vpos;
1627 if (it2.bidi_it.paragraph_dir == R2L)
1628 r2l = true;
1630 else
1631 bidi_unshelve_cache (it2data, true);
1633 bidi_unshelve_cache (itdata, false);
1635 if (old_buffer)
1636 set_buffer_internal_1 (old_buffer);
1638 if (visible_p)
1640 if (w->hscroll > 0)
1641 *x -=
1642 window_hscroll_limited (w, WINDOW_XFRAME (w))
1643 * WINDOW_FRAME_COLUMN_WIDTH (w);
1644 /* For lines in an R2L paragraph, we need to mirror the X pixel
1645 coordinate wrt the text area. For the reasons, see the
1646 commentary in buffer_posn_from_coords and the explanation of
1647 the geometry used by the move_it_* functions at the end of
1648 the large commentary near the beginning of this file. */
1649 if (r2l)
1650 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1653 #if false
1654 /* Debugging code. */
1655 if (visible_p)
1656 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1657 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1658 else
1659 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1660 #endif
1662 return visible_p;
1666 /* Return the next character from STR. Return in *LEN the length of
1667 the character. This is like STRING_CHAR_AND_LENGTH but never
1668 returns an invalid character. If we find one, we return a `?', but
1669 with the length of the invalid character. */
1671 static int
1672 string_char_and_length (const unsigned char *str, int *len)
1674 int c;
1676 c = STRING_CHAR_AND_LENGTH (str, *len);
1677 if (!CHAR_VALID_P (c))
1678 /* We may not change the length here because other places in Emacs
1679 don't use this function, i.e. they silently accept invalid
1680 characters. */
1681 c = '?';
1683 return c;
1688 /* Given a position POS containing a valid character and byte position
1689 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1691 static struct text_pos
1692 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1694 eassert (STRINGP (string) && nchars >= 0);
1696 if (STRING_MULTIBYTE (string))
1698 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1699 int len;
1701 while (nchars--)
1703 string_char_and_length (p, &len);
1704 p += len;
1705 CHARPOS (pos) += 1;
1706 BYTEPOS (pos) += len;
1709 else
1710 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1712 return pos;
1716 /* Value is the text position, i.e. character and byte position,
1717 for character position CHARPOS in STRING. */
1719 static struct text_pos
1720 string_pos (ptrdiff_t charpos, Lisp_Object string)
1722 struct text_pos pos;
1723 eassert (STRINGP (string));
1724 eassert (charpos >= 0);
1725 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1726 return pos;
1730 /* Value is a text position, i.e. character and byte position, for
1731 character position CHARPOS in C string S. MULTIBYTE_P
1732 means recognize multibyte characters. */
1734 static struct text_pos
1735 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1737 struct text_pos pos;
1739 eassert (s != NULL);
1740 eassert (charpos >= 0);
1742 if (multibyte_p)
1744 int len;
1746 SET_TEXT_POS (pos, 0, 0);
1747 while (charpos--)
1749 string_char_and_length ((const unsigned char *) s, &len);
1750 s += len;
1751 CHARPOS (pos) += 1;
1752 BYTEPOS (pos) += len;
1755 else
1756 SET_TEXT_POS (pos, charpos, charpos);
1758 return pos;
1762 /* Value is the number of characters in C string S. MULTIBYTE_P
1763 means recognize multibyte characters. */
1765 static ptrdiff_t
1766 number_of_chars (const char *s, bool multibyte_p)
1768 ptrdiff_t nchars;
1770 if (multibyte_p)
1772 ptrdiff_t rest = strlen (s);
1773 int len;
1774 const unsigned char *p = (const unsigned char *) s;
1776 for (nchars = 0; rest > 0; ++nchars)
1778 string_char_and_length (p, &len);
1779 rest -= len, p += len;
1782 else
1783 nchars = strlen (s);
1785 return nchars;
1789 /* Compute byte position NEWPOS->bytepos corresponding to
1790 NEWPOS->charpos. POS is a known position in string STRING.
1791 NEWPOS->charpos must be >= POS.charpos. */
1793 static void
1794 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1796 eassert (STRINGP (string));
1797 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1799 if (STRING_MULTIBYTE (string))
1800 *newpos = string_pos_nchars_ahead (pos, string,
1801 CHARPOS (*newpos) - CHARPOS (pos));
1802 else
1803 BYTEPOS (*newpos) = CHARPOS (*newpos);
1806 /* EXPORT:
1807 Return an estimation of the pixel height of mode or header lines on
1808 frame F. FACE_ID specifies what line's height to estimate. */
1811 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1813 #ifdef HAVE_WINDOW_SYSTEM
1814 if (FRAME_WINDOW_P (f))
1816 int height = FONT_HEIGHT (FRAME_FONT (f));
1818 /* This function is called so early when Emacs starts that the face
1819 cache and mode line face are not yet initialized. */
1820 if (FRAME_FACE_CACHE (f))
1822 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1823 if (face)
1825 if (face->font)
1826 height = normal_char_height (face->font, -1);
1827 if (face->box_line_width > 0)
1828 height += 2 * face->box_line_width;
1832 return height;
1834 #endif
1836 return 1;
1839 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1840 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1841 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1842 not force the value into range. */
1844 void
1845 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1846 NativeRectangle *bounds, bool noclip)
1849 #ifdef HAVE_WINDOW_SYSTEM
1850 if (FRAME_WINDOW_P (f))
1852 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1853 even for negative values. */
1854 if (pix_x < 0)
1855 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1856 if (pix_y < 0)
1857 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1859 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1860 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1862 if (bounds)
1863 STORE_NATIVE_RECT (*bounds,
1864 FRAME_COL_TO_PIXEL_X (f, pix_x),
1865 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1866 FRAME_COLUMN_WIDTH (f) - 1,
1867 FRAME_LINE_HEIGHT (f) - 1);
1869 /* PXW: Should we clip pixels before converting to columns/lines? */
1870 if (!noclip)
1872 if (pix_x < 0)
1873 pix_x = 0;
1874 else if (pix_x > FRAME_TOTAL_COLS (f))
1875 pix_x = FRAME_TOTAL_COLS (f);
1877 if (pix_y < 0)
1878 pix_y = 0;
1879 else if (pix_y > FRAME_TOTAL_LINES (f))
1880 pix_y = FRAME_TOTAL_LINES (f);
1883 #endif
1885 *x = pix_x;
1886 *y = pix_y;
1890 /* Find the glyph under window-relative coordinates X/Y in window W.
1891 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1892 strings. Return in *HPOS and *VPOS the row and column number of
1893 the glyph found. Return in *AREA the glyph area containing X.
1894 Value is a pointer to the glyph found or null if X/Y is not on
1895 text, or we can't tell because W's current matrix is not up to
1896 date. */
1898 static struct glyph *
1899 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1900 int *dx, int *dy, int *area)
1902 struct glyph *glyph, *end;
1903 struct glyph_row *row = NULL;
1904 int x0, i;
1906 /* Find row containing Y. Give up if some row is not enabled. */
1907 for (i = 0; i < w->current_matrix->nrows; ++i)
1909 row = MATRIX_ROW (w->current_matrix, i);
1910 if (!row->enabled_p)
1911 return NULL;
1912 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1913 break;
1916 *vpos = i;
1917 *hpos = 0;
1919 /* Give up if Y is not in the window. */
1920 if (i == w->current_matrix->nrows)
1921 return NULL;
1923 /* Get the glyph area containing X. */
1924 if (w->pseudo_window_p)
1926 *area = TEXT_AREA;
1927 x0 = 0;
1929 else
1931 if (x < window_box_left_offset (w, TEXT_AREA))
1933 *area = LEFT_MARGIN_AREA;
1934 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1936 else if (x < window_box_right_offset (w, TEXT_AREA))
1938 *area = TEXT_AREA;
1939 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1941 else
1943 *area = RIGHT_MARGIN_AREA;
1944 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1948 /* Find glyph containing X. */
1949 glyph = row->glyphs[*area];
1950 end = glyph + row->used[*area];
1951 x -= x0;
1952 while (glyph < end && x >= glyph->pixel_width)
1954 x -= glyph->pixel_width;
1955 ++glyph;
1958 if (glyph == end)
1959 return NULL;
1961 if (dx)
1963 *dx = x;
1964 *dy = y - (row->y + row->ascent - glyph->ascent);
1967 *hpos = glyph - row->glyphs[*area];
1968 return glyph;
1971 /* Convert frame-relative x/y to coordinates relative to window W.
1972 Takes pseudo-windows into account. */
1974 static void
1975 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1977 if (w->pseudo_window_p)
1979 /* A pseudo-window is always full-width, and starts at the
1980 left edge of the frame, plus a frame border. */
1981 struct frame *f = XFRAME (w->frame);
1982 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1983 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1985 else
1987 *x -= WINDOW_LEFT_EDGE_X (w);
1988 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1992 #ifdef HAVE_WINDOW_SYSTEM
1994 /* EXPORT:
1995 Return in RECTS[] at most N clipping rectangles for glyph string S.
1996 Return the number of stored rectangles. */
1999 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2001 XRectangle r;
2003 if (n <= 0)
2004 return 0;
2006 if (s->row->full_width_p)
2008 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2009 r.x = WINDOW_LEFT_EDGE_X (s->w);
2010 if (s->row->mode_line_p)
2011 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2012 else
2013 r.width = WINDOW_PIXEL_WIDTH (s->w);
2015 /* Unless displaying a mode or menu bar line, which are always
2016 fully visible, clip to the visible part of the row. */
2017 if (s->w->pseudo_window_p)
2018 r.height = s->row->visible_height;
2019 else
2020 r.height = s->height;
2022 else
2024 /* This is a text line that may be partially visible. */
2025 r.x = window_box_left (s->w, s->area);
2026 r.width = window_box_width (s->w, s->area);
2027 r.height = s->row->visible_height;
2030 if (s->clip_head)
2031 if (r.x < s->clip_head->x)
2033 if (r.width >= s->clip_head->x - r.x)
2034 r.width -= s->clip_head->x - r.x;
2035 else
2036 r.width = 0;
2037 r.x = s->clip_head->x;
2039 if (s->clip_tail)
2040 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2042 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2043 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2044 else
2045 r.width = 0;
2048 /* If S draws overlapping rows, it's sufficient to use the top and
2049 bottom of the window for clipping because this glyph string
2050 intentionally draws over other lines. */
2051 if (s->for_overlaps)
2053 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2054 r.height = window_text_bottom_y (s->w) - r.y;
2056 /* Alas, the above simple strategy does not work for the
2057 environments with anti-aliased text: if the same text is
2058 drawn onto the same place multiple times, it gets thicker.
2059 If the overlap we are processing is for the erased cursor, we
2060 take the intersection with the rectangle of the cursor. */
2061 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2063 XRectangle rc, r_save = r;
2065 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2066 rc.y = s->w->phys_cursor.y;
2067 rc.width = s->w->phys_cursor_width;
2068 rc.height = s->w->phys_cursor_height;
2070 x_intersect_rectangles (&r_save, &rc, &r);
2073 else
2075 /* Don't use S->y for clipping because it doesn't take partially
2076 visible lines into account. For example, it can be negative for
2077 partially visible lines at the top of a window. */
2078 if (!s->row->full_width_p
2079 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2080 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2081 else
2082 r.y = max (0, s->row->y);
2085 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2087 /* If drawing the cursor, don't let glyph draw outside its
2088 advertised boundaries. Cleartype does this under some circumstances. */
2089 if (s->hl == DRAW_CURSOR)
2091 struct glyph *glyph = s->first_glyph;
2092 int height, max_y;
2094 if (s->x > r.x)
2096 if (r.width >= s->x - r.x)
2097 r.width -= s->x - r.x;
2098 else /* R2L hscrolled row with cursor outside text area */
2099 r.width = 0;
2100 r.x = s->x;
2102 r.width = min (r.width, glyph->pixel_width);
2104 /* If r.y is below window bottom, ensure that we still see a cursor. */
2105 height = min (glyph->ascent + glyph->descent,
2106 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2107 max_y = window_text_bottom_y (s->w) - height;
2108 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2109 if (s->ybase - glyph->ascent > max_y)
2111 r.y = max_y;
2112 r.height = height;
2114 else
2116 /* Don't draw cursor glyph taller than our actual glyph. */
2117 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2118 if (height < r.height)
2120 max_y = r.y + r.height;
2121 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2122 r.height = min (max_y - r.y, height);
2127 if (s->row->clip)
2129 XRectangle r_save = r;
2131 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2132 r.width = 0;
2135 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2136 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2138 #ifdef CONVERT_FROM_XRECT
2139 CONVERT_FROM_XRECT (r, *rects);
2140 #else
2141 *rects = r;
2142 #endif
2143 return 1;
2145 else
2147 /* If we are processing overlapping and allowed to return
2148 multiple clipping rectangles, we exclude the row of the glyph
2149 string from the clipping rectangle. This is to avoid drawing
2150 the same text on the environment with anti-aliasing. */
2151 #ifdef CONVERT_FROM_XRECT
2152 XRectangle rs[2];
2153 #else
2154 XRectangle *rs = rects;
2155 #endif
2156 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2158 if (s->for_overlaps & OVERLAPS_PRED)
2160 rs[i] = r;
2161 if (r.y + r.height > row_y)
2163 if (r.y < row_y)
2164 rs[i].height = row_y - r.y;
2165 else
2166 rs[i].height = 0;
2168 i++;
2170 if (s->for_overlaps & OVERLAPS_SUCC)
2172 rs[i] = r;
2173 if (r.y < row_y + s->row->visible_height)
2175 if (r.y + r.height > row_y + s->row->visible_height)
2177 rs[i].y = row_y + s->row->visible_height;
2178 rs[i].height = r.y + r.height - rs[i].y;
2180 else
2181 rs[i].height = 0;
2183 i++;
2186 n = i;
2187 #ifdef CONVERT_FROM_XRECT
2188 for (i = 0; i < n; i++)
2189 CONVERT_FROM_XRECT (rs[i], rects[i]);
2190 #endif
2191 return n;
2195 /* EXPORT:
2196 Return in *NR the clipping rectangle for glyph string S. */
2198 void
2199 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2201 get_glyph_string_clip_rects (s, nr, 1);
2205 /* EXPORT:
2206 Return the position and height of the phys cursor in window W.
2207 Set w->phys_cursor_width to width of phys cursor.
2210 void
2211 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2212 struct glyph *glyph, int *xp, int *yp, int *heightp)
2214 struct frame *f = XFRAME (WINDOW_FRAME (w));
2215 int x, y, wd, h, h0, y0, ascent;
2217 /* Compute the width of the rectangle to draw. If on a stretch
2218 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2219 rectangle as wide as the glyph, but use a canonical character
2220 width instead. */
2221 wd = glyph->pixel_width;
2223 x = w->phys_cursor.x;
2224 if (x < 0)
2226 wd += x;
2227 x = 0;
2230 if (glyph->type == STRETCH_GLYPH
2231 && !x_stretch_cursor_p)
2232 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2233 w->phys_cursor_width = wd;
2235 /* Don't let the hollow cursor glyph descend below the glyph row's
2236 ascent value, lest the hollow cursor looks funny. */
2237 y = w->phys_cursor.y;
2238 ascent = row->ascent;
2239 if (row->ascent < glyph->ascent)
2241 y -= glyph->ascent - row->ascent;
2242 ascent = glyph->ascent;
2245 /* If y is below window bottom, ensure that we still see a cursor. */
2246 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2248 h = max (h0, ascent + glyph->descent);
2249 h0 = min (h0, ascent + glyph->descent);
2251 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2252 if (y < y0)
2254 h = max (h - (y0 - y) + 1, h0);
2255 y = y0 - 1;
2257 else
2259 y0 = window_text_bottom_y (w) - h0;
2260 if (y > y0)
2262 h += y - y0;
2263 y = y0;
2267 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2268 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2269 *heightp = h;
2273 * Remember which glyph the mouse is over.
2276 void
2277 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2279 Lisp_Object window;
2280 struct window *w;
2281 struct glyph_row *r, *gr, *end_row;
2282 enum window_part part;
2283 enum glyph_row_area area;
2284 int x, y, width, height;
2286 /* Try to determine frame pixel position and size of the glyph under
2287 frame pixel coordinates X/Y on frame F. */
2289 if (window_resize_pixelwise)
2291 width = height = 1;
2292 goto virtual_glyph;
2294 else if (!f->glyphs_initialized_p
2295 || (window = window_from_coordinates (f, gx, gy, &part, false),
2296 NILP (window)))
2298 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2299 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2300 goto virtual_glyph;
2303 w = XWINDOW (window);
2304 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2305 height = WINDOW_FRAME_LINE_HEIGHT (w);
2307 x = window_relative_x_coord (w, part, gx);
2308 y = gy - WINDOW_TOP_EDGE_Y (w);
2310 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2311 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2313 if (w->pseudo_window_p)
2315 area = TEXT_AREA;
2316 part = ON_MODE_LINE; /* Don't adjust margin. */
2317 goto text_glyph;
2320 switch (part)
2322 case ON_LEFT_MARGIN:
2323 area = LEFT_MARGIN_AREA;
2324 goto text_glyph;
2326 case ON_RIGHT_MARGIN:
2327 area = RIGHT_MARGIN_AREA;
2328 goto text_glyph;
2330 case ON_HEADER_LINE:
2331 case ON_MODE_LINE:
2332 gr = (part == ON_HEADER_LINE
2333 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2334 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2335 gy = gr->y;
2336 area = TEXT_AREA;
2337 goto text_glyph_row_found;
2339 case ON_TEXT:
2340 area = TEXT_AREA;
2342 text_glyph:
2343 gr = 0; gy = 0;
2344 for (; r <= end_row && r->enabled_p; ++r)
2345 if (r->y + r->height > y)
2347 gr = r; gy = r->y;
2348 break;
2351 text_glyph_row_found:
2352 if (gr && gy <= y)
2354 struct glyph *g = gr->glyphs[area];
2355 struct glyph *end = g + gr->used[area];
2357 height = gr->height;
2358 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2359 if (gx + g->pixel_width > x)
2360 break;
2362 if (g < end)
2364 if (g->type == IMAGE_GLYPH)
2366 /* Don't remember when mouse is over image, as
2367 image may have hot-spots. */
2368 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2369 return;
2371 width = g->pixel_width;
2373 else
2375 /* Use nominal char spacing at end of line. */
2376 x -= gx;
2377 gx += (x / width) * width;
2380 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2382 gx += window_box_left_offset (w, area);
2383 /* Don't expand over the modeline to make sure the vertical
2384 drag cursor is shown early enough. */
2385 height = min (height,
2386 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2389 else
2391 /* Use nominal line height at end of window. */
2392 gx = (x / width) * width;
2393 y -= gy;
2394 gy += (y / height) * height;
2395 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2396 /* See comment above. */
2397 height = min (height,
2398 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2400 break;
2402 case ON_LEFT_FRINGE:
2403 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2404 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2405 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2406 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2407 goto row_glyph;
2409 case ON_RIGHT_FRINGE:
2410 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2411 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2412 : window_box_right_offset (w, TEXT_AREA));
2413 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2414 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2415 && !WINDOW_RIGHTMOST_P (w))
2416 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2417 /* Make sure the vertical border can get her own glyph to the
2418 right of the one we build here. */
2419 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2420 else
2421 width = WINDOW_PIXEL_WIDTH (w) - gx;
2422 else
2423 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2425 goto row_glyph;
2427 case ON_VERTICAL_BORDER:
2428 gx = WINDOW_PIXEL_WIDTH (w) - width;
2429 goto row_glyph;
2431 case ON_VERTICAL_SCROLL_BAR:
2432 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2434 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2435 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2436 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2437 : 0)));
2438 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2440 row_glyph:
2441 gr = 0, gy = 0;
2442 for (; r <= end_row && r->enabled_p; ++r)
2443 if (r->y + r->height > y)
2445 gr = r; gy = r->y;
2446 break;
2449 if (gr && gy <= y)
2450 height = gr->height;
2451 else
2453 /* Use nominal line height at end of window. */
2454 y -= gy;
2455 gy += (y / height) * height;
2457 break;
2459 case ON_RIGHT_DIVIDER:
2460 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2461 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2462 gy = 0;
2463 /* The bottom divider prevails. */
2464 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2465 goto add_edge;
2467 case ON_BOTTOM_DIVIDER:
2468 gx = 0;
2469 width = WINDOW_PIXEL_WIDTH (w);
2470 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2471 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2472 goto add_edge;
2474 default:
2476 virtual_glyph:
2477 /* If there is no glyph under the mouse, then we divide the screen
2478 into a grid of the smallest glyph in the frame, and use that
2479 as our "glyph". */
2481 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2482 round down even for negative values. */
2483 if (gx < 0)
2484 gx -= width - 1;
2485 if (gy < 0)
2486 gy -= height - 1;
2488 gx = (gx / width) * width;
2489 gy = (gy / height) * height;
2491 goto store_rect;
2494 add_edge:
2495 gx += WINDOW_LEFT_EDGE_X (w);
2496 gy += WINDOW_TOP_EDGE_Y (w);
2498 store_rect:
2499 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2501 /* Visible feedback for debugging. */
2502 #if false && defined HAVE_X_WINDOWS
2503 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2504 f->output_data.x->normal_gc,
2505 gx, gy, width, height);
2506 #endif
2510 #endif /* HAVE_WINDOW_SYSTEM */
2512 static void
2513 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2515 eassert (w);
2516 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2517 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2518 w->window_end_vpos
2519 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2522 /***********************************************************************
2523 Lisp form evaluation
2524 ***********************************************************************/
2526 /* Error handler for safe_eval and safe_call. */
2528 static Lisp_Object
2529 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2531 add_to_log ("Error during redisplay: %S signaled %S",
2532 Flist (nargs, args), arg);
2533 return Qnil;
2536 /* Call function FUNC with the rest of NARGS - 1 arguments
2537 following. Return the result, or nil if something went
2538 wrong. Prevent redisplay during the evaluation. */
2540 static Lisp_Object
2541 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2543 Lisp_Object val;
2545 if (inhibit_eval_during_redisplay)
2546 val = Qnil;
2547 else
2549 ptrdiff_t i;
2550 ptrdiff_t count = SPECPDL_INDEX ();
2551 Lisp_Object *args;
2552 USE_SAFE_ALLOCA;
2553 SAFE_ALLOCA_LISP (args, nargs);
2555 args[0] = func;
2556 for (i = 1; i < nargs; i++)
2557 args[i] = va_arg (ap, Lisp_Object);
2559 specbind (Qinhibit_redisplay, Qt);
2560 if (inhibit_quit)
2561 specbind (Qinhibit_quit, Qt);
2562 /* Use Qt to ensure debugger does not run,
2563 so there is no possibility of wanting to redisplay. */
2564 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2565 safe_eval_handler);
2566 SAFE_FREE ();
2567 val = unbind_to (count, val);
2570 return val;
2573 Lisp_Object
2574 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2576 Lisp_Object retval;
2577 va_list ap;
2579 va_start (ap, func);
2580 retval = safe__call (false, nargs, func, ap);
2581 va_end (ap);
2582 return retval;
2585 /* Call function FN with one argument ARG.
2586 Return the result, or nil if something went wrong. */
2588 Lisp_Object
2589 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2591 return safe_call (2, fn, arg);
2594 static Lisp_Object
2595 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2597 Lisp_Object retval;
2598 va_list ap;
2600 va_start (ap, fn);
2601 retval = safe__call (inhibit_quit, 2, fn, ap);
2602 va_end (ap);
2603 return retval;
2606 Lisp_Object
2607 safe_eval (Lisp_Object sexpr)
2609 return safe__call1 (false, Qeval, sexpr);
2612 static Lisp_Object
2613 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2615 return safe__call1 (inhibit_quit, Qeval, sexpr);
2618 /* Call function FN with two arguments ARG1 and ARG2.
2619 Return the result, or nil if something went wrong. */
2621 Lisp_Object
2622 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2624 return safe_call (3, fn, arg1, arg2);
2629 /***********************************************************************
2630 Debugging
2631 ***********************************************************************/
2633 /* Define CHECK_IT to perform sanity checks on iterators.
2634 This is for debugging. It is too slow to do unconditionally. */
2636 static void
2637 CHECK_IT (struct it *it)
2639 #if false
2640 if (it->method == GET_FROM_STRING)
2642 eassert (STRINGP (it->string));
2643 eassert (IT_STRING_CHARPOS (*it) >= 0);
2645 else
2647 eassert (IT_STRING_CHARPOS (*it) < 0);
2648 if (it->method == GET_FROM_BUFFER)
2650 /* Check that character and byte positions agree. */
2651 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2655 if (it->dpvec)
2656 eassert (it->current.dpvec_index >= 0);
2657 else
2658 eassert (it->current.dpvec_index < 0);
2659 #endif
2663 /* Check that the window end of window W is what we expect it
2664 to be---the last row in the current matrix displaying text. */
2666 static void
2667 CHECK_WINDOW_END (struct window *w)
2669 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2678 #endif
2681 /***********************************************************************
2682 Iterator initialization
2683 ***********************************************************************/
2685 /* Initialize IT for displaying current_buffer in window W, starting
2686 at character position CHARPOS. CHARPOS < 0 means that no buffer
2687 position is specified which is useful when the iterator is assigned
2688 a position later. BYTEPOS is the byte position corresponding to
2689 CHARPOS.
2691 If ROW is not null, calls to produce_glyphs with IT as parameter
2692 will produce glyphs in that row.
2694 BASE_FACE_ID is the id of a base face to use. It must be one of
2695 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2696 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2697 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2699 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2700 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2701 will be initialized to use the corresponding mode line glyph row of
2702 the desired matrix of W. */
2704 void
2705 init_iterator (struct it *it, struct window *w,
2706 ptrdiff_t charpos, ptrdiff_t bytepos,
2707 struct glyph_row *row, enum face_id base_face_id)
2709 enum face_id remapped_base_face_id = base_face_id;
2711 /* Some precondition checks. */
2712 eassert (w != NULL && it != NULL);
2713 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2714 && charpos <= ZV));
2716 /* If face attributes have been changed since the last redisplay,
2717 free realized faces now because they depend on face definitions
2718 that might have changed. Don't free faces while there might be
2719 desired matrices pending which reference these faces. */
2720 if (!inhibit_free_realized_faces)
2722 if (face_change)
2724 face_change = false;
2725 free_all_realized_faces (Qnil);
2727 else if (XFRAME (w->frame)->face_change)
2729 XFRAME (w->frame)->face_change = 0;
2730 free_all_realized_faces (w->frame);
2734 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2735 if (! NILP (Vface_remapping_alist))
2736 remapped_base_face_id
2737 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2739 /* Use one of the mode line rows of W's desired matrix if
2740 appropriate. */
2741 if (row == NULL)
2743 if (base_face_id == MODE_LINE_FACE_ID
2744 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2745 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2746 else if (base_face_id == HEADER_LINE_FACE_ID)
2747 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2750 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2751 Other parts of redisplay rely on that. */
2752 memclear (it, sizeof *it);
2753 it->current.overlay_string_index = -1;
2754 it->current.dpvec_index = -1;
2755 it->base_face_id = remapped_base_face_id;
2756 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2757 it->paragraph_embedding = L2R;
2758 it->bidi_it.w = w;
2760 /* The window in which we iterate over current_buffer: */
2761 XSETWINDOW (it->window, w);
2762 it->w = w;
2763 it->f = XFRAME (w->frame);
2765 it->cmp_it.id = -1;
2767 /* Extra space between lines (on window systems only). */
2768 if (base_face_id == DEFAULT_FACE_ID
2769 && FRAME_WINDOW_P (it->f))
2771 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2772 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2773 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2774 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2775 * FRAME_LINE_HEIGHT (it->f));
2776 else if (it->f->extra_line_spacing > 0)
2777 it->extra_line_spacing = it->f->extra_line_spacing;
2780 /* If realized faces have been removed, e.g. because of face
2781 attribute changes of named faces, recompute them. When running
2782 in batch mode, the face cache of the initial frame is null. If
2783 we happen to get called, make a dummy face cache. */
2784 if (FRAME_FACE_CACHE (it->f) == NULL)
2785 init_frame_faces (it->f);
2786 if (FRAME_FACE_CACHE (it->f)->used == 0)
2787 recompute_basic_faces (it->f);
2789 it->override_ascent = -1;
2791 /* Are control characters displayed as `^C'? */
2792 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2794 /* -1 means everything between a CR and the following line end
2795 is invisible. >0 means lines indented more than this value are
2796 invisible. */
2797 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2798 ? (clip_to_bounds
2799 (-1, XINT (BVAR (current_buffer, selective_display)),
2800 PTRDIFF_MAX))
2801 : (!NILP (BVAR (current_buffer, selective_display))
2802 ? -1 : 0));
2803 it->selective_display_ellipsis_p
2804 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2806 /* Display table to use. */
2807 it->dp = window_display_table (w);
2809 /* Are multibyte characters enabled in current_buffer? */
2810 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2812 /* Get the position at which the redisplay_end_trigger hook should
2813 be run, if it is to be run at all. */
2814 if (MARKERP (w->redisplay_end_trigger)
2815 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2816 it->redisplay_end_trigger_charpos
2817 = marker_position (w->redisplay_end_trigger);
2818 else if (INTEGERP (w->redisplay_end_trigger))
2819 it->redisplay_end_trigger_charpos
2820 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2821 PTRDIFF_MAX);
2823 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2825 /* Are lines in the display truncated? */
2826 if (TRUNCATE != 0)
2827 it->line_wrap = TRUNCATE;
2828 if (base_face_id == DEFAULT_FACE_ID
2829 && !it->w->hscroll
2830 && (WINDOW_FULL_WIDTH_P (it->w)
2831 || NILP (Vtruncate_partial_width_windows)
2832 || (INTEGERP (Vtruncate_partial_width_windows)
2833 /* PXW: Shall we do something about this? */
2834 && (XINT (Vtruncate_partial_width_windows)
2835 <= WINDOW_TOTAL_COLS (it->w))))
2836 && NILP (BVAR (current_buffer, truncate_lines)))
2837 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2838 ? WINDOW_WRAP : WORD_WRAP;
2840 /* Get dimensions of truncation and continuation glyphs. These are
2841 displayed as fringe bitmaps under X, but we need them for such
2842 frames when the fringes are turned off. But leave the dimensions
2843 zero for tooltip frames, as these glyphs look ugly there and also
2844 sabotage calculations of tooltip dimensions in x-show-tip. */
2845 #ifdef HAVE_WINDOW_SYSTEM
2846 if (!(FRAME_WINDOW_P (it->f)
2847 && FRAMEP (tip_frame)
2848 && it->f == XFRAME (tip_frame)))
2849 #endif
2851 if (it->line_wrap == TRUNCATE)
2853 /* We will need the truncation glyph. */
2854 eassert (it->glyph_row == NULL);
2855 produce_special_glyphs (it, IT_TRUNCATION);
2856 it->truncation_pixel_width = it->pixel_width;
2858 else
2860 /* We will need the continuation glyph. */
2861 eassert (it->glyph_row == NULL);
2862 produce_special_glyphs (it, IT_CONTINUATION);
2863 it->continuation_pixel_width = it->pixel_width;
2867 /* Reset these values to zero because the produce_special_glyphs
2868 above has changed them. */
2869 it->pixel_width = it->ascent = it->descent = 0;
2870 it->phys_ascent = it->phys_descent = 0;
2872 /* Set this after getting the dimensions of truncation and
2873 continuation glyphs, so that we don't produce glyphs when calling
2874 produce_special_glyphs, above. */
2875 it->glyph_row = row;
2876 it->area = TEXT_AREA;
2878 /* Get the dimensions of the display area. The display area
2879 consists of the visible window area plus a horizontally scrolled
2880 part to the left of the window. All x-values are relative to the
2881 start of this total display area. */
2882 if (base_face_id != DEFAULT_FACE_ID)
2884 /* Mode lines, menu bar in terminal frames. */
2885 it->first_visible_x = 0;
2886 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2888 else
2890 it->first_visible_x
2891 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2892 it->last_visible_x = (it->first_visible_x
2893 + window_box_width (w, TEXT_AREA));
2895 /* If we truncate lines, leave room for the truncation glyph(s) at
2896 the right margin. Otherwise, leave room for the continuation
2897 glyph(s). Done only if the window has no right fringe. */
2898 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2900 if (it->line_wrap == TRUNCATE)
2901 it->last_visible_x -= it->truncation_pixel_width;
2902 else
2903 it->last_visible_x -= it->continuation_pixel_width;
2906 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2907 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2910 /* Leave room for a border glyph. */
2911 if (!FRAME_WINDOW_P (it->f)
2912 && !WINDOW_RIGHTMOST_P (it->w))
2913 it->last_visible_x -= 1;
2915 it->last_visible_y = window_text_bottom_y (w);
2917 /* For mode lines and alike, arrange for the first glyph having a
2918 left box line if the face specifies a box. */
2919 if (base_face_id != DEFAULT_FACE_ID)
2921 struct face *face;
2923 it->face_id = remapped_base_face_id;
2925 /* If we have a boxed mode line, make the first character appear
2926 with a left box line. */
2927 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2928 if (face && face->box != FACE_NO_BOX)
2929 it->start_of_box_run_p = true;
2932 /* If a buffer position was specified, set the iterator there,
2933 getting overlays and face properties from that position. */
2934 if (charpos >= BUF_BEG (current_buffer))
2936 it->stop_charpos = charpos;
2937 it->end_charpos = ZV;
2938 eassert (charpos == BYTE_TO_CHAR (bytepos));
2939 IT_CHARPOS (*it) = charpos;
2940 IT_BYTEPOS (*it) = bytepos;
2942 /* We will rely on `reseat' to set this up properly, via
2943 handle_face_prop. */
2944 it->face_id = it->base_face_id;
2946 it->start = it->current;
2947 /* Do we need to reorder bidirectional text? Not if this is a
2948 unibyte buffer: by definition, none of the single-byte
2949 characters are strong R2L, so no reordering is needed. And
2950 bidi.c doesn't support unibyte buffers anyway. Also, don't
2951 reorder while we are loading loadup.el, since the tables of
2952 character properties needed for reordering are not yet
2953 available. */
2954 it->bidi_p =
2955 !redisplay__inhibit_bidi
2956 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2957 && it->multibyte_p;
2959 /* If we are to reorder bidirectional text, init the bidi
2960 iterator. */
2961 if (it->bidi_p)
2963 /* Since we don't know at this point whether there will be
2964 any R2L lines in the window, we reserve space for
2965 truncation/continuation glyphs even if only the left
2966 fringe is absent. */
2967 if (base_face_id == DEFAULT_FACE_ID
2968 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2969 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2971 if (it->line_wrap == TRUNCATE)
2972 it->last_visible_x -= it->truncation_pixel_width;
2973 else
2974 it->last_visible_x -= it->continuation_pixel_width;
2976 /* Note the paragraph direction that this buffer wants to
2977 use. */
2978 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2979 Qleft_to_right))
2980 it->paragraph_embedding = L2R;
2981 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2982 Qright_to_left))
2983 it->paragraph_embedding = R2L;
2984 else
2985 it->paragraph_embedding = NEUTRAL_DIR;
2986 bidi_unshelve_cache (NULL, false);
2987 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2988 &it->bidi_it);
2991 /* Compute faces etc. */
2992 reseat (it, it->current.pos, true);
2995 CHECK_IT (it);
2999 /* Initialize IT for the display of window W with window start POS. */
3001 void
3002 start_display (struct it *it, struct window *w, struct text_pos pos)
3004 struct glyph_row *row;
3005 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3007 row = w->desired_matrix->rows + first_vpos;
3008 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3009 it->first_vpos = first_vpos;
3011 /* Don't reseat to previous visible line start if current start
3012 position is in a string or image. */
3013 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3015 int first_y = it->current_y;
3017 /* If window start is not at a line start, skip forward to POS to
3018 get the correct continuation lines width. */
3019 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3020 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3021 if (!start_at_line_beg_p)
3023 int new_x;
3025 reseat_at_previous_visible_line_start (it);
3026 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3028 new_x = it->current_x + it->pixel_width;
3030 /* If lines are continued, this line may end in the middle
3031 of a multi-glyph character (e.g. a control character
3032 displayed as \003, or in the middle of an overlay
3033 string). In this case move_it_to above will not have
3034 taken us to the start of the continuation line but to the
3035 end of the continued line. */
3036 if (it->current_x > 0
3037 && it->line_wrap != TRUNCATE /* Lines are continued. */
3038 && (/* And glyph doesn't fit on the line. */
3039 new_x > it->last_visible_x
3040 /* Or it fits exactly and we're on a window
3041 system frame. */
3042 || (new_x == it->last_visible_x
3043 && FRAME_WINDOW_P (it->f)
3044 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3045 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3046 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3048 if ((it->current.dpvec_index >= 0
3049 || it->current.overlay_string_index >= 0)
3050 /* If we are on a newline from a display vector or
3051 overlay string, then we are already at the end of
3052 a screen line; no need to go to the next line in
3053 that case, as this line is not really continued.
3054 (If we do go to the next line, C-e will not DTRT.) */
3055 && it->c != '\n')
3057 set_iterator_to_next (it, true);
3058 move_it_in_display_line_to (it, -1, -1, 0);
3061 it->continuation_lines_width += it->current_x;
3063 /* If the character at POS is displayed via a display
3064 vector, move_it_to above stops at the final glyph of
3065 IT->dpvec. To make the caller redisplay that character
3066 again (a.k.a. start at POS), we need to reset the
3067 dpvec_index to the beginning of IT->dpvec. */
3068 else if (it->current.dpvec_index >= 0)
3069 it->current.dpvec_index = 0;
3071 /* We're starting a new display line, not affected by the
3072 height of the continued line, so clear the appropriate
3073 fields in the iterator structure. */
3074 it->max_ascent = it->max_descent = 0;
3075 it->max_phys_ascent = it->max_phys_descent = 0;
3077 it->current_y = first_y;
3078 it->vpos = 0;
3079 it->current_x = it->hpos = 0;
3085 /* Return true if POS is a position in ellipses displayed for invisible
3086 text. W is the window we display, for text property lookup. */
3088 static bool
3089 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3091 Lisp_Object prop, window;
3092 bool ellipses_p = false;
3093 ptrdiff_t charpos = CHARPOS (pos->pos);
3095 /* If POS specifies a position in a display vector, this might
3096 be for an ellipsis displayed for invisible text. We won't
3097 get the iterator set up for delivering that ellipsis unless
3098 we make sure that it gets aware of the invisible text. */
3099 if (pos->dpvec_index >= 0
3100 && pos->overlay_string_index < 0
3101 && CHARPOS (pos->string_pos) < 0
3102 && charpos > BEGV
3103 && (XSETWINDOW (window, w),
3104 prop = Fget_char_property (make_number (charpos),
3105 Qinvisible, window),
3106 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3108 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3109 window);
3110 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3113 return ellipses_p;
3117 /* Initialize IT for stepping through current_buffer in window W,
3118 starting at position POS that includes overlay string and display
3119 vector/ control character translation position information. Value
3120 is false if there are overlay strings with newlines at POS. */
3122 static bool
3123 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3125 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3126 int i;
3127 bool overlay_strings_with_newlines = false;
3129 /* If POS specifies a position in a display vector, this might
3130 be for an ellipsis displayed for invisible text. We won't
3131 get the iterator set up for delivering that ellipsis unless
3132 we make sure that it gets aware of the invisible text. */
3133 if (in_ellipses_for_invisible_text_p (pos, w))
3135 --charpos;
3136 bytepos = 0;
3139 /* Keep in mind: the call to reseat in init_iterator skips invisible
3140 text, so we might end up at a position different from POS. This
3141 is only a problem when POS is a row start after a newline and an
3142 overlay starts there with an after-string, and the overlay has an
3143 invisible property. Since we don't skip invisible text in
3144 display_line and elsewhere immediately after consuming the
3145 newline before the row start, such a POS will not be in a string,
3146 but the call to init_iterator below will move us to the
3147 after-string. */
3148 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3150 /* This only scans the current chunk -- it should scan all chunks.
3151 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3152 to 16 in 22.1 to make this a lesser problem. */
3153 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3155 const char *s = SSDATA (it->overlay_strings[i]);
3156 const char *e = s + SBYTES (it->overlay_strings[i]);
3158 while (s < e && *s != '\n')
3159 ++s;
3161 if (s < e)
3163 overlay_strings_with_newlines = true;
3164 break;
3168 /* If position is within an overlay string, set up IT to the right
3169 overlay string. */
3170 if (pos->overlay_string_index >= 0)
3172 int relative_index;
3174 /* If the first overlay string happens to have a `display'
3175 property for an image, the iterator will be set up for that
3176 image, and we have to undo that setup first before we can
3177 correct the overlay string index. */
3178 if (it->method == GET_FROM_IMAGE)
3179 pop_it (it);
3181 /* We already have the first chunk of overlay strings in
3182 IT->overlay_strings. Load more until the one for
3183 pos->overlay_string_index is in IT->overlay_strings. */
3184 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3186 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3187 it->current.overlay_string_index = 0;
3188 while (n--)
3190 load_overlay_strings (it, 0);
3191 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3195 it->current.overlay_string_index = pos->overlay_string_index;
3196 relative_index = (it->current.overlay_string_index
3197 % OVERLAY_STRING_CHUNK_SIZE);
3198 it->string = it->overlay_strings[relative_index];
3199 eassert (STRINGP (it->string));
3200 it->current.string_pos = pos->string_pos;
3201 it->method = GET_FROM_STRING;
3202 it->end_charpos = SCHARS (it->string);
3203 /* Set up the bidi iterator for this overlay string. */
3204 if (it->bidi_p)
3206 it->bidi_it.string.lstring = it->string;
3207 it->bidi_it.string.s = NULL;
3208 it->bidi_it.string.schars = SCHARS (it->string);
3209 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3210 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3211 it->bidi_it.string.unibyte = !it->multibyte_p;
3212 it->bidi_it.w = it->w;
3213 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3214 FRAME_WINDOW_P (it->f), &it->bidi_it);
3216 /* Synchronize the state of the bidi iterator with
3217 pos->string_pos. For any string position other than
3218 zero, this will be done automagically when we resume
3219 iteration over the string and get_visually_first_element
3220 is called. But if string_pos is zero, and the string is
3221 to be reordered for display, we need to resync manually,
3222 since it could be that the iteration state recorded in
3223 pos ended at string_pos of 0 moving backwards in string. */
3224 if (CHARPOS (pos->string_pos) == 0)
3226 get_visually_first_element (it);
3227 if (IT_STRING_CHARPOS (*it) != 0)
3228 do {
3229 /* Paranoia. */
3230 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3231 bidi_move_to_visually_next (&it->bidi_it);
3232 } while (it->bidi_it.charpos != 0);
3234 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3235 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3239 if (CHARPOS (pos->string_pos) >= 0)
3241 /* Recorded position is not in an overlay string, but in another
3242 string. This can only be a string from a `display' property.
3243 IT should already be filled with that string. */
3244 it->current.string_pos = pos->string_pos;
3245 eassert (STRINGP (it->string));
3246 if (it->bidi_p)
3247 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3248 FRAME_WINDOW_P (it->f), &it->bidi_it);
3251 /* Restore position in display vector translations, control
3252 character translations or ellipses. */
3253 if (pos->dpvec_index >= 0)
3255 if (it->dpvec == NULL)
3256 get_next_display_element (it);
3257 eassert (it->dpvec && it->current.dpvec_index == 0);
3258 it->current.dpvec_index = pos->dpvec_index;
3261 CHECK_IT (it);
3262 return !overlay_strings_with_newlines;
3266 /* Initialize IT for stepping through current_buffer in window W
3267 starting at ROW->start. */
3269 static void
3270 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3272 init_from_display_pos (it, w, &row->start);
3273 it->start = row->start;
3274 it->continuation_lines_width = row->continuation_lines_width;
3275 CHECK_IT (it);
3279 /* Initialize IT for stepping through current_buffer in window W
3280 starting in the line following ROW, i.e. starting at ROW->end.
3281 Value is false if there are overlay strings with newlines at ROW's
3282 end position. */
3284 static bool
3285 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3287 bool success = false;
3289 if (init_from_display_pos (it, w, &row->end))
3291 if (row->continued_p)
3292 it->continuation_lines_width
3293 = row->continuation_lines_width + row->pixel_width;
3294 CHECK_IT (it);
3295 success = true;
3298 return success;
3304 /***********************************************************************
3305 Text properties
3306 ***********************************************************************/
3308 /* Called when IT reaches IT->stop_charpos. Handle text property and
3309 overlay changes. Set IT->stop_charpos to the next position where
3310 to stop. */
3312 static void
3313 handle_stop (struct it *it)
3315 enum prop_handled handled;
3316 bool handle_overlay_change_p;
3317 struct props *p;
3319 it->dpvec = NULL;
3320 it->current.dpvec_index = -1;
3321 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3322 it->ellipsis_p = false;
3324 /* Use face of preceding text for ellipsis (if invisible) */
3325 if (it->selective_display_ellipsis_p)
3326 it->saved_face_id = it->face_id;
3328 /* Here's the description of the semantics of, and the logic behind,
3329 the various HANDLED_* statuses:
3331 HANDLED_NORMALLY means the handler did its job, and the loop
3332 should proceed to calling the next handler in order.
3334 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3335 change in the properties and overlays at current position, so the
3336 loop should be restarted, to re-invoke the handlers that were
3337 already called. This happens when fontification-functions were
3338 called by handle_fontified_prop, and actually fontified
3339 something. Another case where HANDLED_RECOMPUTE_PROPS is
3340 returned is when we discover overlay strings that need to be
3341 displayed right away. The loop below will continue for as long
3342 as the status is HANDLED_RECOMPUTE_PROPS.
3344 HANDLED_RETURN means return immediately to the caller, to
3345 continue iteration without calling any further handlers. This is
3346 used when we need to act on some property right away, for example
3347 when we need to display the ellipsis or a replacing display
3348 property, such as display string or image.
3350 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3351 consumed, and the handler switched to the next overlay string.
3352 This signals the loop below to refrain from looking for more
3353 overlays before all the overlay strings of the current overlay
3354 are processed.
3356 Some of the handlers called by the loop push the iterator state
3357 onto the stack (see 'push_it'), and arrange for the iteration to
3358 continue with another object, such as an image, a display string,
3359 or an overlay string. In most such cases, it->stop_charpos is
3360 set to the first character of the string, so that when the
3361 iteration resumes, this function will immediately be called
3362 again, to examine the properties at the beginning of the string.
3364 When a display or overlay string is exhausted, the iterator state
3365 is popped (see 'pop_it'), and iteration continues with the
3366 previous object. Again, in many such cases this function is
3367 called again to find the next position where properties might
3368 change. */
3372 handled = HANDLED_NORMALLY;
3374 /* Call text property handlers. */
3375 for (p = it_props; p->handler; ++p)
3377 handled = p->handler (it);
3379 if (handled == HANDLED_RECOMPUTE_PROPS)
3380 break;
3381 else if (handled == HANDLED_RETURN)
3383 /* We still want to show before and after strings from
3384 overlays even if the actual buffer text is replaced. */
3385 if (!handle_overlay_change_p
3386 || it->sp > 1
3387 /* Don't call get_overlay_strings_1 if we already
3388 have overlay strings loaded, because doing so
3389 will load them again and push the iterator state
3390 onto the stack one more time, which is not
3391 expected by the rest of the code that processes
3392 overlay strings. */
3393 || (it->current.overlay_string_index < 0
3394 && !get_overlay_strings_1 (it, 0, false)))
3396 if (it->ellipsis_p)
3397 setup_for_ellipsis (it, 0);
3398 /* When handling a display spec, we might load an
3399 empty string. In that case, discard it here. We
3400 used to discard it in handle_single_display_spec,
3401 but that causes get_overlay_strings_1, above, to
3402 ignore overlay strings that we must check. */
3403 if (STRINGP (it->string) && !SCHARS (it->string))
3404 pop_it (it);
3405 return;
3407 else if (STRINGP (it->string) && !SCHARS (it->string))
3408 pop_it (it);
3409 else
3411 it->string_from_display_prop_p = false;
3412 it->from_disp_prop_p = false;
3413 handle_overlay_change_p = false;
3415 handled = HANDLED_RECOMPUTE_PROPS;
3416 break;
3418 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3419 handle_overlay_change_p = false;
3422 if (handled != HANDLED_RECOMPUTE_PROPS)
3424 /* Don't check for overlay strings below when set to deliver
3425 characters from a display vector. */
3426 if (it->method == GET_FROM_DISPLAY_VECTOR)
3427 handle_overlay_change_p = false;
3429 /* Handle overlay changes.
3430 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3431 if it finds overlays. */
3432 if (handle_overlay_change_p)
3433 handled = handle_overlay_change (it);
3436 if (it->ellipsis_p)
3438 setup_for_ellipsis (it, 0);
3439 break;
3442 while (handled == HANDLED_RECOMPUTE_PROPS);
3444 /* Determine where to stop next. */
3445 if (handled == HANDLED_NORMALLY)
3446 compute_stop_pos (it);
3450 /* Compute IT->stop_charpos from text property and overlay change
3451 information for IT's current position. */
3453 static void
3454 compute_stop_pos (struct it *it)
3456 register INTERVAL iv, next_iv;
3457 Lisp_Object object, limit, position;
3458 ptrdiff_t charpos, bytepos;
3460 if (STRINGP (it->string))
3462 /* Strings are usually short, so don't limit the search for
3463 properties. */
3464 it->stop_charpos = it->end_charpos;
3465 object = it->string;
3466 limit = Qnil;
3467 charpos = IT_STRING_CHARPOS (*it);
3468 bytepos = IT_STRING_BYTEPOS (*it);
3470 else
3472 ptrdiff_t pos;
3474 /* If end_charpos is out of range for some reason, such as a
3475 misbehaving display function, rationalize it (Bug#5984). */
3476 if (it->end_charpos > ZV)
3477 it->end_charpos = ZV;
3478 it->stop_charpos = it->end_charpos;
3480 /* If next overlay change is in front of the current stop pos
3481 (which is IT->end_charpos), stop there. Note: value of
3482 next_overlay_change is point-max if no overlay change
3483 follows. */
3484 charpos = IT_CHARPOS (*it);
3485 bytepos = IT_BYTEPOS (*it);
3486 pos = next_overlay_change (charpos);
3487 if (pos < it->stop_charpos)
3488 it->stop_charpos = pos;
3490 /* Set up variables for computing the stop position from text
3491 property changes. */
3492 XSETBUFFER (object, current_buffer);
3493 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3496 /* Get the interval containing IT's position. Value is a null
3497 interval if there isn't such an interval. */
3498 position = make_number (charpos);
3499 iv = validate_interval_range (object, &position, &position, false);
3500 if (iv)
3502 Lisp_Object values_here[LAST_PROP_IDX];
3503 struct props *p;
3505 /* Get properties here. */
3506 for (p = it_props; p->handler; ++p)
3507 values_here[p->idx] = textget (iv->plist,
3508 builtin_lisp_symbol (p->name));
3510 /* Look for an interval following iv that has different
3511 properties. */
3512 for (next_iv = next_interval (iv);
3513 (next_iv
3514 && (NILP (limit)
3515 || XFASTINT (limit) > next_iv->position));
3516 next_iv = next_interval (next_iv))
3518 for (p = it_props; p->handler; ++p)
3520 Lisp_Object new_value = textget (next_iv->plist,
3521 builtin_lisp_symbol (p->name));
3522 if (!EQ (values_here[p->idx], new_value))
3523 break;
3526 if (p->handler)
3527 break;
3530 if (next_iv)
3532 if (INTEGERP (limit)
3533 && next_iv->position >= XFASTINT (limit))
3534 /* No text property change up to limit. */
3535 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3536 else
3537 /* Text properties change in next_iv. */
3538 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3542 if (it->cmp_it.id < 0)
3544 ptrdiff_t stoppos = it->end_charpos;
3546 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3547 stoppos = -1;
3548 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3549 stoppos, it->string);
3552 eassert (STRINGP (it->string)
3553 || (it->stop_charpos >= BEGV
3554 && it->stop_charpos >= IT_CHARPOS (*it)));
3558 /* Return the position of the next overlay change after POS in
3559 current_buffer. Value is point-max if no overlay change
3560 follows. This is like `next-overlay-change' but doesn't use
3561 xmalloc. */
3563 static ptrdiff_t
3564 next_overlay_change (ptrdiff_t pos)
3566 ptrdiff_t i, noverlays;
3567 ptrdiff_t endpos;
3568 Lisp_Object *overlays;
3569 USE_SAFE_ALLOCA;
3571 /* Get all overlays at the given position. */
3572 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3574 /* If any of these overlays ends before endpos,
3575 use its ending point instead. */
3576 for (i = 0; i < noverlays; ++i)
3578 Lisp_Object oend;
3579 ptrdiff_t oendpos;
3581 oend = OVERLAY_END (overlays[i]);
3582 oendpos = OVERLAY_POSITION (oend);
3583 endpos = min (endpos, oendpos);
3586 SAFE_FREE ();
3587 return endpos;
3590 /* How many characters forward to search for a display property or
3591 display string. Searching too far forward makes the bidi display
3592 sluggish, especially in small windows. */
3593 #define MAX_DISP_SCAN 250
3595 /* Return the character position of a display string at or after
3596 position specified by POSITION. If no display string exists at or
3597 after POSITION, return ZV. A display string is either an overlay
3598 with `display' property whose value is a string, or a `display'
3599 text property whose value is a string. STRING is data about the
3600 string to iterate; if STRING->lstring is nil, we are iterating a
3601 buffer. FRAME_WINDOW_P is true when we are displaying a window
3602 on a GUI frame. DISP_PROP is set to zero if we searched
3603 MAX_DISP_SCAN characters forward without finding any display
3604 strings, non-zero otherwise. It is set to 2 if the display string
3605 uses any kind of `(space ...)' spec that will produce a stretch of
3606 white space in the text area. */
3607 ptrdiff_t
3608 compute_display_string_pos (struct text_pos *position,
3609 struct bidi_string_data *string,
3610 struct window *w,
3611 bool frame_window_p, int *disp_prop)
3613 /* OBJECT = nil means current buffer. */
3614 Lisp_Object object, object1;
3615 Lisp_Object pos, spec, limpos;
3616 bool string_p = string && (STRINGP (string->lstring) || string->s);
3617 ptrdiff_t eob = string_p ? string->schars : ZV;
3618 ptrdiff_t begb = string_p ? 0 : BEGV;
3619 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3620 ptrdiff_t lim =
3621 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3622 struct text_pos tpos;
3623 int rv = 0;
3625 if (string && STRINGP (string->lstring))
3626 object1 = object = string->lstring;
3627 else if (w && !string_p)
3629 XSETWINDOW (object, w);
3630 object1 = Qnil;
3632 else
3633 object1 = object = Qnil;
3635 *disp_prop = 1;
3637 if (charpos >= eob
3638 /* We don't support display properties whose values are strings
3639 that have display string properties. */
3640 || string->from_disp_str
3641 /* C strings cannot have display properties. */
3642 || (string->s && !STRINGP (object)))
3644 *disp_prop = 0;
3645 return eob;
3648 /* If the character at CHARPOS is where the display string begins,
3649 return CHARPOS. */
3650 pos = make_number (charpos);
3651 if (STRINGP (object))
3652 bufpos = string->bufpos;
3653 else
3654 bufpos = charpos;
3655 tpos = *position;
3656 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3657 && (charpos <= begb
3658 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3659 object),
3660 spec))
3661 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3662 frame_window_p)))
3664 if (rv == 2)
3665 *disp_prop = 2;
3666 return charpos;
3669 /* Look forward for the first character with a `display' property
3670 that will replace the underlying text when displayed. */
3671 limpos = make_number (lim);
3672 do {
3673 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3674 CHARPOS (tpos) = XFASTINT (pos);
3675 if (CHARPOS (tpos) >= lim)
3677 *disp_prop = 0;
3678 break;
3680 if (STRINGP (object))
3681 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3682 else
3683 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3684 spec = Fget_char_property (pos, Qdisplay, object);
3685 if (!STRINGP (object))
3686 bufpos = CHARPOS (tpos);
3687 } while (NILP (spec)
3688 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3689 bufpos, frame_window_p)));
3690 if (rv == 2)
3691 *disp_prop = 2;
3693 return CHARPOS (tpos);
3696 /* Return the character position of the end of the display string that
3697 started at CHARPOS. If there's no display string at CHARPOS,
3698 return -1. A display string is either an overlay with `display'
3699 property whose value is a string or a `display' text property whose
3700 value is a string. */
3701 ptrdiff_t
3702 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3704 /* OBJECT = nil means current buffer. */
3705 Lisp_Object object =
3706 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3707 Lisp_Object pos = make_number (charpos);
3708 ptrdiff_t eob =
3709 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3711 if (charpos >= eob || (string->s && !STRINGP (object)))
3712 return eob;
3714 /* It could happen that the display property or overlay was removed
3715 since we found it in compute_display_string_pos above. One way
3716 this can happen is if JIT font-lock was called (through
3717 handle_fontified_prop), and jit-lock-functions remove text
3718 properties or overlays from the portion of buffer that includes
3719 CHARPOS. Muse mode is known to do that, for example. In this
3720 case, we return -1 to the caller, to signal that no display
3721 string is actually present at CHARPOS. See bidi_fetch_char for
3722 how this is handled.
3724 An alternative would be to never look for display properties past
3725 it->stop_charpos. But neither compute_display_string_pos nor
3726 bidi_fetch_char that calls it know or care where the next
3727 stop_charpos is. */
3728 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3729 return -1;
3731 /* Look forward for the first character where the `display' property
3732 changes. */
3733 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3735 return XFASTINT (pos);
3740 /***********************************************************************
3741 Fontification
3742 ***********************************************************************/
3744 /* Handle changes in the `fontified' property of the current buffer by
3745 calling hook functions from Qfontification_functions to fontify
3746 regions of text. */
3748 static enum prop_handled
3749 handle_fontified_prop (struct it *it)
3751 Lisp_Object prop, pos;
3752 enum prop_handled handled = HANDLED_NORMALLY;
3754 if (!NILP (Vmemory_full))
3755 return handled;
3757 /* Get the value of the `fontified' property at IT's current buffer
3758 position. (The `fontified' property doesn't have a special
3759 meaning in strings.) If the value is nil, call functions from
3760 Qfontification_functions. */
3761 if (!STRINGP (it->string)
3762 && it->s == NULL
3763 && !NILP (Vfontification_functions)
3764 && !NILP (Vrun_hooks)
3765 && (pos = make_number (IT_CHARPOS (*it)),
3766 prop = Fget_char_property (pos, Qfontified, Qnil),
3767 /* Ignore the special cased nil value always present at EOB since
3768 no amount of fontifying will be able to change it. */
3769 NILP (prop) && IT_CHARPOS (*it) < Z))
3771 ptrdiff_t count = SPECPDL_INDEX ();
3772 Lisp_Object val;
3773 struct buffer *obuf = current_buffer;
3774 ptrdiff_t begv = BEGV, zv = ZV;
3775 bool old_clip_changed = current_buffer->clip_changed;
3777 val = Vfontification_functions;
3778 specbind (Qfontification_functions, Qnil);
3780 eassert (it->end_charpos == ZV);
3782 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3783 safe_call1 (val, pos);
3784 else
3786 Lisp_Object fns, fn;
3788 fns = Qnil;
3790 for (; CONSP (val); val = XCDR (val))
3792 fn = XCAR (val);
3794 if (EQ (fn, Qt))
3796 /* A value of t indicates this hook has a local
3797 binding; it means to run the global binding too.
3798 In a global value, t should not occur. If it
3799 does, we must ignore it to avoid an endless
3800 loop. */
3801 for (fns = Fdefault_value (Qfontification_functions);
3802 CONSP (fns);
3803 fns = XCDR (fns))
3805 fn = XCAR (fns);
3806 if (!EQ (fn, Qt))
3807 safe_call1 (fn, pos);
3810 else
3811 safe_call1 (fn, pos);
3815 unbind_to (count, Qnil);
3817 /* Fontification functions routinely call `save-restriction'.
3818 Normally, this tags clip_changed, which can confuse redisplay
3819 (see discussion in Bug#6671). Since we don't perform any
3820 special handling of fontification changes in the case where
3821 `save-restriction' isn't called, there's no point doing so in
3822 this case either. So, if the buffer's restrictions are
3823 actually left unchanged, reset clip_changed. */
3824 if (obuf == current_buffer)
3826 if (begv == BEGV && zv == ZV)
3827 current_buffer->clip_changed = old_clip_changed;
3829 /* There isn't much we can reasonably do to protect against
3830 misbehaving fontification, but here's a fig leaf. */
3831 else if (BUFFER_LIVE_P (obuf))
3832 set_buffer_internal_1 (obuf);
3834 /* The fontification code may have added/removed text.
3835 It could do even a lot worse, but let's at least protect against
3836 the most obvious case where only the text past `pos' gets changed',
3837 as is/was done in grep.el where some escapes sequences are turned
3838 into face properties (bug#7876). */
3839 it->end_charpos = ZV;
3841 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3842 something. This avoids an endless loop if they failed to
3843 fontify the text for which reason ever. */
3844 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3845 handled = HANDLED_RECOMPUTE_PROPS;
3848 return handled;
3853 /***********************************************************************
3854 Faces
3855 ***********************************************************************/
3857 /* Set up iterator IT from face properties at its current position.
3858 Called from handle_stop. */
3860 static enum prop_handled
3861 handle_face_prop (struct it *it)
3863 int new_face_id;
3864 ptrdiff_t next_stop;
3866 if (!STRINGP (it->string))
3868 new_face_id
3869 = face_at_buffer_position (it->w,
3870 IT_CHARPOS (*it),
3871 &next_stop,
3872 (IT_CHARPOS (*it)
3873 + TEXT_PROP_DISTANCE_LIMIT),
3874 false, it->base_face_id);
3876 /* Is this a start of a run of characters with box face?
3877 Caveat: this can be called for a freshly initialized
3878 iterator; face_id is -1 in this case. We know that the new
3879 face will not change until limit, i.e. if the new face has a
3880 box, all characters up to limit will have one. But, as
3881 usual, we don't know whether limit is really the end. */
3882 if (new_face_id != it->face_id)
3884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3885 /* If it->face_id is -1, old_face below will be NULL, see
3886 the definition of FACE_FROM_ID_OR_NULL. This will happen
3887 if this is the initial call that gets the face. */
3888 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3890 /* If the value of face_id of the iterator is -1, we have to
3891 look in front of IT's position and see whether there is a
3892 face there that's different from new_face_id. */
3893 if (!old_face && IT_CHARPOS (*it) > BEG)
3895 int prev_face_id = face_before_it_pos (it);
3897 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3900 /* If the new face has a box, but the old face does not,
3901 this is the start of a run of characters with box face,
3902 i.e. this character has a shadow on the left side. */
3903 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3904 && (old_face == NULL || !old_face->box));
3905 it->face_box_p = new_face->box != FACE_NO_BOX;
3908 else
3910 int base_face_id;
3911 ptrdiff_t bufpos;
3912 int i;
3913 Lisp_Object from_overlay
3914 = (it->current.overlay_string_index >= 0
3915 ? it->string_overlays[it->current.overlay_string_index
3916 % OVERLAY_STRING_CHUNK_SIZE]
3917 : Qnil);
3919 /* See if we got to this string directly or indirectly from
3920 an overlay property. That includes the before-string or
3921 after-string of an overlay, strings in display properties
3922 provided by an overlay, their text properties, etc.
3924 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3925 if (! NILP (from_overlay))
3926 for (i = it->sp - 1; i >= 0; i--)
3928 if (it->stack[i].current.overlay_string_index >= 0)
3929 from_overlay
3930 = it->string_overlays[it->stack[i].current.overlay_string_index
3931 % OVERLAY_STRING_CHUNK_SIZE];
3932 else if (! NILP (it->stack[i].from_overlay))
3933 from_overlay = it->stack[i].from_overlay;
3935 if (!NILP (from_overlay))
3936 break;
3939 if (! NILP (from_overlay))
3941 bufpos = IT_CHARPOS (*it);
3942 /* For a string from an overlay, the base face depends
3943 only on text properties and ignores overlays. */
3944 base_face_id
3945 = face_for_overlay_string (it->w,
3946 IT_CHARPOS (*it),
3947 &next_stop,
3948 (IT_CHARPOS (*it)
3949 + TEXT_PROP_DISTANCE_LIMIT),
3950 false,
3951 from_overlay);
3953 else
3955 bufpos = 0;
3957 /* For strings from a `display' property, use the face at
3958 IT's current buffer position as the base face to merge
3959 with, so that overlay strings appear in the same face as
3960 surrounding text, unless they specify their own faces.
3961 For strings from wrap-prefix and line-prefix properties,
3962 use the default face, possibly remapped via
3963 Vface_remapping_alist. */
3964 /* Note that the fact that we use the face at _buffer_
3965 position means that a 'display' property on an overlay
3966 string will not inherit the face of that overlay string,
3967 but will instead revert to the face of buffer text
3968 covered by the overlay. This is visible, e.g., when the
3969 overlay specifies a box face, but neither the buffer nor
3970 the display string do. This sounds like a design bug,
3971 but Emacs always did that since v21.1, so changing that
3972 might be a big deal. */
3973 base_face_id = it->string_from_prefix_prop_p
3974 ? (!NILP (Vface_remapping_alist)
3975 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3976 : DEFAULT_FACE_ID)
3977 : underlying_face_id (it);
3980 new_face_id = face_at_string_position (it->w,
3981 it->string,
3982 IT_STRING_CHARPOS (*it),
3983 bufpos,
3984 &next_stop,
3985 base_face_id, false);
3987 /* Is this a start of a run of characters with box? Caveat:
3988 this can be called for a freshly allocated iterator; face_id
3989 is -1 is this case. We know that the new face will not
3990 change until the next check pos, i.e. if the new face has a
3991 box, all characters up to that position will have a
3992 box. But, as usual, we don't know whether that position
3993 is really the end. */
3994 if (new_face_id != it->face_id)
3996 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3997 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3999 /* If new face has a box but old face hasn't, this is the
4000 start of a run of characters with box, i.e. it has a
4001 shadow on the left side. */
4002 it->start_of_box_run_p
4003 = new_face->box && (old_face == NULL || !old_face->box);
4004 it->face_box_p = new_face->box != FACE_NO_BOX;
4008 it->face_id = new_face_id;
4009 return HANDLED_NORMALLY;
4013 /* Return the ID of the face ``underlying'' IT's current position,
4014 which is in a string. If the iterator is associated with a
4015 buffer, return the face at IT's current buffer position.
4016 Otherwise, use the iterator's base_face_id. */
4018 static int
4019 underlying_face_id (struct it *it)
4021 int face_id = it->base_face_id, i;
4023 eassert (STRINGP (it->string));
4025 for (i = it->sp - 1; i >= 0; --i)
4026 if (NILP (it->stack[i].string))
4027 face_id = it->stack[i].face_id;
4029 return face_id;
4033 /* Compute the face one character before or after the current position
4034 of IT, in the visual order. BEFORE_P means get the face
4035 in front (to the left in L2R paragraphs, to the right in R2L
4036 paragraphs) of IT's screen position. Value is the ID of the face. */
4038 static int
4039 face_before_or_after_it_pos (struct it *it, bool before_p)
4041 int face_id, limit;
4042 ptrdiff_t next_check_charpos;
4043 struct it it_copy;
4044 void *it_copy_data = NULL;
4046 eassert (it->s == NULL);
4048 if (STRINGP (it->string))
4050 ptrdiff_t bufpos, charpos;
4051 int base_face_id;
4053 /* No face change past the end of the string (for the case
4054 we are padding with spaces). No face change before the
4055 string start. */
4056 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4057 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4058 return it->face_id;
4060 if (!it->bidi_p)
4062 /* Set charpos to the position before or after IT's current
4063 position, in the logical order, which in the non-bidi
4064 case is the same as the visual order. */
4065 if (before_p)
4066 charpos = IT_STRING_CHARPOS (*it) - 1;
4067 else if (it->what == IT_COMPOSITION)
4068 /* For composition, we must check the character after the
4069 composition. */
4070 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4071 else
4072 charpos = IT_STRING_CHARPOS (*it) + 1;
4074 else
4076 if (before_p)
4078 /* With bidi iteration, the character before the current
4079 in the visual order cannot be found by simple
4080 iteration, because "reverse" reordering is not
4081 supported. Instead, we need to start from the string
4082 beginning and go all the way to the current string
4083 position, remembering the previous position. */
4084 /* Ignore face changes before the first visible
4085 character on this display line. */
4086 if (it->current_x <= it->first_visible_x)
4087 return it->face_id;
4088 SAVE_IT (it_copy, *it, it_copy_data);
4089 IT_STRING_CHARPOS (it_copy) = 0;
4090 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4094 charpos = IT_STRING_CHARPOS (it_copy);
4095 if (charpos >= SCHARS (it->string))
4096 break;
4097 bidi_move_to_visually_next (&it_copy.bidi_it);
4099 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4101 RESTORE_IT (it, it, it_copy_data);
4103 else
4105 /* Set charpos to the string position of the character
4106 that comes after IT's current position in the visual
4107 order. */
4108 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4110 it_copy = *it;
4111 while (n--)
4112 bidi_move_to_visually_next (&it_copy.bidi_it);
4114 charpos = it_copy.bidi_it.charpos;
4117 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4119 if (it->current.overlay_string_index >= 0)
4120 bufpos = IT_CHARPOS (*it);
4121 else
4122 bufpos = 0;
4124 base_face_id = underlying_face_id (it);
4126 /* Get the face for ASCII, or unibyte. */
4127 face_id = face_at_string_position (it->w,
4128 it->string,
4129 charpos,
4130 bufpos,
4131 &next_check_charpos,
4132 base_face_id, false);
4134 /* Correct the face for charsets different from ASCII. Do it
4135 for the multibyte case only. The face returned above is
4136 suitable for unibyte text if IT->string is unibyte. */
4137 if (STRING_MULTIBYTE (it->string))
4139 struct text_pos pos1 = string_pos (charpos, it->string);
4140 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4141 int c, len;
4142 struct face *face = FACE_FROM_ID (it->f, face_id);
4144 c = string_char_and_length (p, &len);
4145 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4148 else
4150 struct text_pos pos;
4152 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4153 || (IT_CHARPOS (*it) <= BEGV && before_p))
4154 return it->face_id;
4156 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4157 pos = it->current.pos;
4159 if (!it->bidi_p)
4161 if (before_p)
4162 DEC_TEXT_POS (pos, it->multibyte_p);
4163 else
4165 if (it->what == IT_COMPOSITION)
4167 /* For composition, we must check the position after
4168 the composition. */
4169 pos.charpos += it->cmp_it.nchars;
4170 pos.bytepos += it->len;
4172 else
4173 INC_TEXT_POS (pos, it->multibyte_p);
4176 else
4178 if (before_p)
4180 int current_x;
4182 /* With bidi iteration, the character before the current
4183 in the visual order cannot be found by simple
4184 iteration, because "reverse" reordering is not
4185 supported. Instead, we need to use the move_it_*
4186 family of functions, and move to the previous
4187 character starting from the beginning of the visual
4188 line. */
4189 /* Ignore face changes before the first visible
4190 character on this display line. */
4191 if (it->current_x <= it->first_visible_x)
4192 return it->face_id;
4193 SAVE_IT (it_copy, *it, it_copy_data);
4194 /* Implementation note: Since move_it_in_display_line
4195 works in the iterator geometry, and thinks the first
4196 character is always the leftmost, even in R2L lines,
4197 we don't need to distinguish between the R2L and L2R
4198 cases here. */
4199 current_x = it_copy.current_x;
4200 move_it_vertically_backward (&it_copy, 0);
4201 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4202 pos = it_copy.current.pos;
4203 RESTORE_IT (it, it, it_copy_data);
4205 else
4207 /* Set charpos to the buffer position of the character
4208 that comes after IT's current position in the visual
4209 order. */
4210 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4212 it_copy = *it;
4213 while (n--)
4214 bidi_move_to_visually_next (&it_copy.bidi_it);
4216 SET_TEXT_POS (pos,
4217 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4220 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4222 /* Determine face for CHARSET_ASCII, or unibyte. */
4223 face_id = face_at_buffer_position (it->w,
4224 CHARPOS (pos),
4225 &next_check_charpos,
4226 limit, false, -1);
4228 /* Correct the face for charsets different from ASCII. Do it
4229 for the multibyte case only. The face returned above is
4230 suitable for unibyte text if current_buffer is unibyte. */
4231 if (it->multibyte_p)
4233 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4234 struct face *face = FACE_FROM_ID (it->f, face_id);
4235 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4239 return face_id;
4244 /***********************************************************************
4245 Invisible text
4246 ***********************************************************************/
4248 /* Set up iterator IT from invisible properties at its current
4249 position. Called from handle_stop. */
4251 static enum prop_handled
4252 handle_invisible_prop (struct it *it)
4254 enum prop_handled handled = HANDLED_NORMALLY;
4255 int invis;
4256 Lisp_Object prop;
4258 if (STRINGP (it->string))
4260 Lisp_Object end_charpos, limit;
4262 /* Get the value of the invisible text property at the
4263 current position. Value will be nil if there is no such
4264 property. */
4265 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4266 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4267 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4269 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4271 /* Record whether we have to display an ellipsis for the
4272 invisible text. */
4273 bool display_ellipsis_p = (invis == 2);
4274 ptrdiff_t len, endpos;
4276 handled = HANDLED_RECOMPUTE_PROPS;
4278 /* Get the position at which the next visible text can be
4279 found in IT->string, if any. */
4280 endpos = len = SCHARS (it->string);
4281 XSETINT (limit, len);
4284 end_charpos
4285 = Fnext_single_property_change (end_charpos, Qinvisible,
4286 it->string, limit);
4287 /* Since LIMIT is always an integer, so should be the
4288 value returned by Fnext_single_property_change. */
4289 eassert (INTEGERP (end_charpos));
4290 if (INTEGERP (end_charpos))
4292 endpos = XFASTINT (end_charpos);
4293 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4294 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4295 if (invis == 2)
4296 display_ellipsis_p = true;
4298 else /* Should never happen; but if it does, exit the loop. */
4299 endpos = len;
4301 while (invis != 0 && endpos < len);
4303 if (display_ellipsis_p)
4304 it->ellipsis_p = true;
4306 if (endpos < len)
4308 /* Text at END_CHARPOS is visible. Move IT there. */
4309 struct text_pos old;
4310 ptrdiff_t oldpos;
4312 old = it->current.string_pos;
4313 oldpos = CHARPOS (old);
4314 if (it->bidi_p)
4316 if (it->bidi_it.first_elt
4317 && it->bidi_it.charpos < SCHARS (it->string))
4318 bidi_paragraph_init (it->paragraph_embedding,
4319 &it->bidi_it, true);
4320 /* Bidi-iterate out of the invisible text. */
4323 bidi_move_to_visually_next (&it->bidi_it);
4325 while (oldpos <= it->bidi_it.charpos
4326 && it->bidi_it.charpos < endpos);
4328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4330 if (IT_CHARPOS (*it) >= endpos)
4331 it->prev_stop = endpos;
4333 else
4335 IT_STRING_CHARPOS (*it) = endpos;
4336 compute_string_pos (&it->current.string_pos, old, it->string);
4339 else
4341 /* The rest of the string is invisible. If this is an
4342 overlay string, proceed with the next overlay string
4343 or whatever comes and return a character from there. */
4344 if (it->current.overlay_string_index >= 0
4345 && !display_ellipsis_p)
4347 next_overlay_string (it);
4348 /* Don't check for overlay strings when we just
4349 finished processing them. */
4350 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4352 else
4354 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4355 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4360 else
4362 ptrdiff_t newpos, next_stop, start_charpos, tem;
4363 Lisp_Object pos, overlay;
4365 /* First of all, is there invisible text at this position? */
4366 tem = start_charpos = IT_CHARPOS (*it);
4367 pos = make_number (tem);
4368 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4369 &overlay);
4370 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4372 /* If we are on invisible text, skip over it. */
4373 if (invis != 0 && start_charpos < it->end_charpos)
4375 /* Record whether we have to display an ellipsis for the
4376 invisible text. */
4377 bool display_ellipsis_p = invis == 2;
4379 handled = HANDLED_RECOMPUTE_PROPS;
4381 /* Loop skipping over invisible text. The loop is left at
4382 ZV or with IT on the first char being visible again. */
4385 /* Try to skip some invisible text. Return value is the
4386 position reached which can be equal to where we start
4387 if there is nothing invisible there. This skips both
4388 over invisible text properties and overlays with
4389 invisible property. */
4390 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4392 /* If we skipped nothing at all we weren't at invisible
4393 text in the first place. If everything to the end of
4394 the buffer was skipped, end the loop. */
4395 if (newpos == tem || newpos >= ZV)
4396 invis = 0;
4397 else
4399 /* We skipped some characters but not necessarily
4400 all there are. Check if we ended up on visible
4401 text. Fget_char_property returns the property of
4402 the char before the given position, i.e. if we
4403 get invis = 0, this means that the char at
4404 newpos is visible. */
4405 pos = make_number (newpos);
4406 prop = Fget_char_property (pos, Qinvisible, it->window);
4407 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4410 /* If we ended up on invisible text, proceed to
4411 skip starting with next_stop. */
4412 if (invis != 0)
4413 tem = next_stop;
4415 /* If there are adjacent invisible texts, don't lose the
4416 second one's ellipsis. */
4417 if (invis == 2)
4418 display_ellipsis_p = true;
4420 while (invis != 0);
4422 /* The position newpos is now either ZV or on visible text. */
4423 if (it->bidi_p)
4425 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4426 bool on_newline
4427 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4428 bool after_newline
4429 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4431 /* If the invisible text ends on a newline or on a
4432 character after a newline, we can avoid the costly,
4433 character by character, bidi iteration to NEWPOS, and
4434 instead simply reseat the iterator there. That's
4435 because all bidi reordering information is tossed at
4436 the newline. This is a big win for modes that hide
4437 complete lines, like Outline, Org, etc. */
4438 if (on_newline || after_newline)
4440 struct text_pos tpos;
4441 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4443 SET_TEXT_POS (tpos, newpos, bpos);
4444 reseat_1 (it, tpos, false);
4445 /* If we reseat on a newline/ZV, we need to prep the
4446 bidi iterator for advancing to the next character
4447 after the newline/EOB, keeping the current paragraph
4448 direction (so that PRODUCE_GLYPHS does TRT wrt
4449 prepending/appending glyphs to a glyph row). */
4450 if (on_newline)
4452 it->bidi_it.first_elt = false;
4453 it->bidi_it.paragraph_dir = pdir;
4454 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4455 it->bidi_it.nchars = 1;
4456 it->bidi_it.ch_len = 1;
4459 else /* Must use the slow method. */
4461 /* With bidi iteration, the region of invisible text
4462 could start and/or end in the middle of a
4463 non-base embedding level. Therefore, we need to
4464 skip invisible text using the bidi iterator,
4465 starting at IT's current position, until we find
4466 ourselves outside of the invisible text.
4467 Skipping invisible text _after_ bidi iteration
4468 avoids affecting the visual order of the
4469 displayed text when invisible properties are
4470 added or removed. */
4471 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4473 /* If we were `reseat'ed to a new paragraph,
4474 determine the paragraph base direction. We
4475 need to do it now because
4476 next_element_from_buffer may not have a
4477 chance to do it, if we are going to skip any
4478 text at the beginning, which resets the
4479 FIRST_ELT flag. */
4480 bidi_paragraph_init (it->paragraph_embedding,
4481 &it->bidi_it, true);
4485 bidi_move_to_visually_next (&it->bidi_it);
4487 while (it->stop_charpos <= it->bidi_it.charpos
4488 && it->bidi_it.charpos < newpos);
4489 IT_CHARPOS (*it) = it->bidi_it.charpos;
4490 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4491 /* If we overstepped NEWPOS, record its position in
4492 the iterator, so that we skip invisible text if
4493 later the bidi iteration lands us in the
4494 invisible region again. */
4495 if (IT_CHARPOS (*it) >= newpos)
4496 it->prev_stop = newpos;
4499 else
4501 IT_CHARPOS (*it) = newpos;
4502 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4505 if (display_ellipsis_p)
4507 /* Make sure that the glyphs of the ellipsis will get
4508 correct `charpos' values. If we would not update
4509 it->position here, the glyphs would belong to the
4510 last visible character _before_ the invisible
4511 text, which confuses `set_cursor_from_row'.
4513 We use the last invisible position instead of the
4514 first because this way the cursor is always drawn on
4515 the first "." of the ellipsis, whenever PT is inside
4516 the invisible text. Otherwise the cursor would be
4517 placed _after_ the ellipsis when the point is after the
4518 first invisible character. */
4519 if (!STRINGP (it->object))
4521 it->position.charpos = newpos - 1;
4522 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4526 /* If there are before-strings at the start of invisible
4527 text, and the text is invisible because of a text
4528 property, arrange to show before-strings because 20.x did
4529 it that way. (If the text is invisible because of an
4530 overlay property instead of a text property, this is
4531 already handled in the overlay code.) */
4532 if (NILP (overlay)
4533 && get_overlay_strings (it, it->stop_charpos))
4535 handled = HANDLED_RECOMPUTE_PROPS;
4536 if (it->sp > 0)
4538 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4539 /* The call to get_overlay_strings above recomputes
4540 it->stop_charpos, but it only considers changes
4541 in properties and overlays beyond iterator's
4542 current position. This causes us to miss changes
4543 that happen exactly where the invisible property
4544 ended. So we play it safe here and force the
4545 iterator to check for potential stop positions
4546 immediately after the invisible text. Note that
4547 if get_overlay_strings returns true, it
4548 normally also pushed the iterator stack, so we
4549 need to update the stop position in the slot
4550 below the current one. */
4551 it->stack[it->sp - 1].stop_charpos
4552 = CHARPOS (it->stack[it->sp - 1].current.pos);
4555 else if (display_ellipsis_p)
4557 it->ellipsis_p = true;
4558 /* Let the ellipsis display before
4559 considering any properties of the following char.
4560 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4561 handled = HANDLED_RETURN;
4566 return handled;
4570 /* Make iterator IT return `...' next.
4571 Replaces LEN characters from buffer. */
4573 static void
4574 setup_for_ellipsis (struct it *it, int len)
4576 /* Use the display table definition for `...'. Invalid glyphs
4577 will be handled by the method returning elements from dpvec. */
4578 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4580 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4581 it->dpvec = v->contents;
4582 it->dpend = v->contents + v->header.size;
4584 else
4586 /* Default `...'. */
4587 it->dpvec = default_invis_vector;
4588 it->dpend = default_invis_vector + 3;
4591 it->dpvec_char_len = len;
4592 it->current.dpvec_index = 0;
4593 it->dpvec_face_id = -1;
4595 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4596 face as the preceding text. IT->saved_face_id was set in
4597 handle_stop to the face of the preceding character, and will be
4598 different from IT->face_id only if the invisible text skipped in
4599 handle_invisible_prop has some non-default face on its first
4600 character. We thus ignore the face of the invisible text when we
4601 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4602 if (it->saved_face_id >= 0)
4603 it->face_id = it->saved_face_id;
4605 /* If the ellipsis represents buffer text, it means we advanced in
4606 the buffer, so we should no longer ignore overlay strings. */
4607 if (it->method == GET_FROM_BUFFER)
4608 it->ignore_overlay_strings_at_pos_p = false;
4610 it->method = GET_FROM_DISPLAY_VECTOR;
4611 it->ellipsis_p = true;
4616 /***********************************************************************
4617 'display' property
4618 ***********************************************************************/
4620 /* Set up iterator IT from `display' property at its current position.
4621 Called from handle_stop.
4622 We return HANDLED_RETURN if some part of the display property
4623 overrides the display of the buffer text itself.
4624 Otherwise we return HANDLED_NORMALLY. */
4626 static enum prop_handled
4627 handle_display_prop (struct it *it)
4629 Lisp_Object propval, object, overlay;
4630 struct text_pos *position;
4631 ptrdiff_t bufpos;
4632 /* Nonzero if some property replaces the display of the text itself. */
4633 int display_replaced = 0;
4635 if (STRINGP (it->string))
4637 object = it->string;
4638 position = &it->current.string_pos;
4639 bufpos = CHARPOS (it->current.pos);
4641 else
4643 XSETWINDOW (object, it->w);
4644 position = &it->current.pos;
4645 bufpos = CHARPOS (*position);
4648 /* Reset those iterator values set from display property values. */
4649 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4650 it->space_width = Qnil;
4651 it->font_height = Qnil;
4652 it->voffset = 0;
4654 /* We don't support recursive `display' properties, i.e. string
4655 values that have a string `display' property, that have a string
4656 `display' property etc. */
4657 if (!it->string_from_display_prop_p)
4658 it->area = TEXT_AREA;
4660 propval = get_char_property_and_overlay (make_number (position->charpos),
4661 Qdisplay, object, &overlay);
4662 if (NILP (propval))
4663 return HANDLED_NORMALLY;
4664 /* Now OVERLAY is the overlay that gave us this property, or nil
4665 if it was a text property. */
4667 if (!STRINGP (it->string))
4668 object = it->w->contents;
4670 display_replaced = handle_display_spec (it, propval, object, overlay,
4671 position, bufpos,
4672 FRAME_WINDOW_P (it->f));
4673 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4676 /* Subroutine of handle_display_prop. Returns non-zero if the display
4677 specification in SPEC is a replacing specification, i.e. it would
4678 replace the text covered by `display' property with something else,
4679 such as an image or a display string. If SPEC includes any kind or
4680 `(space ...) specification, the value is 2; this is used by
4681 compute_display_string_pos, which see.
4683 See handle_single_display_spec for documentation of arguments.
4684 FRAME_WINDOW_P is true if the window being redisplayed is on a
4685 GUI frame; this argument is used only if IT is NULL, see below.
4687 IT can be NULL, if this is called by the bidi reordering code
4688 through compute_display_string_pos, which see. In that case, this
4689 function only examines SPEC, but does not otherwise "handle" it, in
4690 the sense that it doesn't set up members of IT from the display
4691 spec. */
4692 static int
4693 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4694 Lisp_Object overlay, struct text_pos *position,
4695 ptrdiff_t bufpos, bool frame_window_p)
4697 int replacing = 0;
4699 if (CONSP (spec)
4700 /* Simple specifications. */
4701 && !EQ (XCAR (spec), Qimage)
4702 #ifdef HAVE_XWIDGETS
4703 && !EQ (XCAR (spec), Qxwidget)
4704 #endif
4705 && !EQ (XCAR (spec), Qspace)
4706 && !EQ (XCAR (spec), Qwhen)
4707 && !EQ (XCAR (spec), Qslice)
4708 && !EQ (XCAR (spec), Qspace_width)
4709 && !EQ (XCAR (spec), Qheight)
4710 && !EQ (XCAR (spec), Qraise)
4711 /* Marginal area specifications. */
4712 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4713 && !EQ (XCAR (spec), Qleft_fringe)
4714 && !EQ (XCAR (spec), Qright_fringe)
4715 && !NILP (XCAR (spec)))
4717 for (; CONSP (spec); spec = XCDR (spec))
4719 int rv = handle_single_display_spec (it, XCAR (spec), object,
4720 overlay, position, bufpos,
4721 replacing, frame_window_p);
4722 if (rv != 0)
4724 replacing = rv;
4725 /* If some text in a string is replaced, `position' no
4726 longer points to the position of `object'. */
4727 if (!it || STRINGP (object))
4728 break;
4732 else if (VECTORP (spec))
4734 ptrdiff_t i;
4735 for (i = 0; i < ASIZE (spec); ++i)
4737 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4738 overlay, position, bufpos,
4739 replacing, frame_window_p);
4740 if (rv != 0)
4742 replacing = rv;
4743 /* If some text in a string is replaced, `position' no
4744 longer points to the position of `object'. */
4745 if (!it || STRINGP (object))
4746 break;
4750 else
4751 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4752 bufpos, 0, frame_window_p);
4753 return replacing;
4756 /* Value is the position of the end of the `display' property starting
4757 at START_POS in OBJECT. */
4759 static struct text_pos
4760 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4762 Lisp_Object end;
4763 struct text_pos end_pos;
4765 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4766 Qdisplay, object, Qnil);
4767 CHARPOS (end_pos) = XFASTINT (end);
4768 if (STRINGP (object))
4769 compute_string_pos (&end_pos, start_pos, it->string);
4770 else
4771 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4773 return end_pos;
4777 /* Set up IT from a single `display' property specification SPEC. OBJECT
4778 is the object in which the `display' property was found. *POSITION
4779 is the position in OBJECT at which the `display' property was found.
4780 BUFPOS is the buffer position of OBJECT (different from POSITION if
4781 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4782 previously saw a display specification which already replaced text
4783 display with something else, for example an image; we ignore such
4784 properties after the first one has been processed.
4786 OVERLAY is the overlay this `display' property came from,
4787 or nil if it was a text property.
4789 If SPEC is a `space' or `image' specification, and in some other
4790 cases too, set *POSITION to the position where the `display'
4791 property ends.
4793 If IT is NULL, only examine the property specification in SPEC, but
4794 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4795 is intended to be displayed in a window on a GUI frame.
4797 Value is non-zero if something was found which replaces the display
4798 of buffer or string text. */
4800 static int
4801 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4802 Lisp_Object overlay, struct text_pos *position,
4803 ptrdiff_t bufpos, int display_replaced,
4804 bool frame_window_p)
4806 Lisp_Object form;
4807 Lisp_Object location, value;
4808 struct text_pos start_pos = *position;
4810 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4811 If the result is non-nil, use VALUE instead of SPEC. */
4812 form = Qt;
4813 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4815 spec = XCDR (spec);
4816 if (!CONSP (spec))
4817 return 0;
4818 form = XCAR (spec);
4819 spec = XCDR (spec);
4822 if (!NILP (form) && !EQ (form, Qt))
4824 ptrdiff_t count = SPECPDL_INDEX ();
4826 /* Bind `object' to the object having the `display' property, a
4827 buffer or string. Bind `position' to the position in the
4828 object where the property was found, and `buffer-position'
4829 to the current position in the buffer. */
4831 if (NILP (object))
4832 XSETBUFFER (object, current_buffer);
4833 specbind (Qobject, object);
4834 specbind (Qposition, make_number (CHARPOS (*position)));
4835 specbind (Qbuffer_position, make_number (bufpos));
4836 form = safe_eval (form);
4837 unbind_to (count, Qnil);
4840 if (NILP (form))
4841 return 0;
4843 /* Handle `(height HEIGHT)' specifications. */
4844 if (CONSP (spec)
4845 && EQ (XCAR (spec), Qheight)
4846 && CONSP (XCDR (spec)))
4848 if (it)
4850 if (!FRAME_WINDOW_P (it->f))
4851 return 0;
4853 it->font_height = XCAR (XCDR (spec));
4854 if (!NILP (it->font_height))
4856 int new_height = -1;
4858 if (CONSP (it->font_height)
4859 && (EQ (XCAR (it->font_height), Qplus)
4860 || EQ (XCAR (it->font_height), Qminus))
4861 && CONSP (XCDR (it->font_height))
4862 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4864 /* `(+ N)' or `(- N)' where N is an integer. */
4865 int steps = XINT (XCAR (XCDR (it->font_height)));
4866 if (EQ (XCAR (it->font_height), Qplus))
4867 steps = - steps;
4868 it->face_id = smaller_face (it->f, it->face_id, steps);
4870 else if (FUNCTIONP (it->font_height))
4872 /* Call function with current height as argument.
4873 Value is the new height. */
4874 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4875 Lisp_Object height;
4876 height = safe_call1 (it->font_height,
4877 face->lface[LFACE_HEIGHT_INDEX]);
4878 if (NUMBERP (height))
4879 new_height = XFLOATINT (height);
4881 else if (NUMBERP (it->font_height))
4883 /* Value is a multiple of the canonical char height. */
4884 struct face *f;
4886 f = FACE_FROM_ID (it->f,
4887 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4888 new_height = (XFLOATINT (it->font_height)
4889 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4891 else
4893 /* Evaluate IT->font_height with `height' bound to the
4894 current specified height to get the new height. */
4895 ptrdiff_t count = SPECPDL_INDEX ();
4896 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4898 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4899 value = safe_eval (it->font_height);
4900 unbind_to (count, Qnil);
4902 if (NUMBERP (value))
4903 new_height = XFLOATINT (value);
4906 if (new_height > 0)
4907 it->face_id = face_with_height (it->f, it->face_id, new_height);
4911 return 0;
4914 /* Handle `(space-width WIDTH)'. */
4915 if (CONSP (spec)
4916 && EQ (XCAR (spec), Qspace_width)
4917 && CONSP (XCDR (spec)))
4919 if (it)
4921 if (!FRAME_WINDOW_P (it->f))
4922 return 0;
4924 value = XCAR (XCDR (spec));
4925 if (NUMBERP (value) && XFLOATINT (value) > 0)
4926 it->space_width = value;
4929 return 0;
4932 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4933 if (CONSP (spec)
4934 && EQ (XCAR (spec), Qslice))
4936 Lisp_Object tem;
4938 if (it)
4940 if (!FRAME_WINDOW_P (it->f))
4941 return 0;
4943 if (tem = XCDR (spec), CONSP (tem))
4945 it->slice.x = XCAR (tem);
4946 if (tem = XCDR (tem), CONSP (tem))
4948 it->slice.y = XCAR (tem);
4949 if (tem = XCDR (tem), CONSP (tem))
4951 it->slice.width = XCAR (tem);
4952 if (tem = XCDR (tem), CONSP (tem))
4953 it->slice.height = XCAR (tem);
4959 return 0;
4962 /* Handle `(raise FACTOR)'. */
4963 if (CONSP (spec)
4964 && EQ (XCAR (spec), Qraise)
4965 && CONSP (XCDR (spec)))
4967 if (it)
4969 if (!FRAME_WINDOW_P (it->f))
4970 return 0;
4972 #ifdef HAVE_WINDOW_SYSTEM
4973 value = XCAR (XCDR (spec));
4974 if (NUMBERP (value))
4976 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4977 it->voffset = - (XFLOATINT (value)
4978 * (normal_char_height (face->font, -1)));
4980 #endif /* HAVE_WINDOW_SYSTEM */
4983 return 0;
4986 /* Don't handle the other kinds of display specifications
4987 inside a string that we got from a `display' property. */
4988 if (it && it->string_from_display_prop_p)
4989 return 0;
4991 /* Characters having this form of property are not displayed, so
4992 we have to find the end of the property. */
4993 if (it)
4995 start_pos = *position;
4996 *position = display_prop_end (it, object, start_pos);
4997 /* If the display property comes from an overlay, don't consider
4998 any potential stop_charpos values before the end of that
4999 overlay. Since display_prop_end will happily find another
5000 'display' property coming from some other overlay or text
5001 property on buffer positions before this overlay's end, we
5002 need to ignore them, or else we risk displaying this
5003 overlay's display string/image twice. */
5004 if (!NILP (overlay))
5006 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5008 if (ovendpos > CHARPOS (*position))
5009 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5012 value = Qnil;
5014 /* Stop the scan at that end position--we assume that all
5015 text properties change there. */
5016 if (it)
5017 it->stop_charpos = position->charpos;
5019 /* Handle `(left-fringe BITMAP [FACE])'
5020 and `(right-fringe BITMAP [FACE])'. */
5021 if (CONSP (spec)
5022 && (EQ (XCAR (spec), Qleft_fringe)
5023 || EQ (XCAR (spec), Qright_fringe))
5024 && CONSP (XCDR (spec)))
5026 if (it)
5028 if (!FRAME_WINDOW_P (it->f))
5029 /* If we return here, POSITION has been advanced
5030 across the text with this property. */
5032 /* Synchronize the bidi iterator with POSITION. This is
5033 needed because we are not going to push the iterator
5034 on behalf of this display property, so there will be
5035 no pop_it call to do this synchronization for us. */
5036 if (it->bidi_p)
5038 it->position = *position;
5039 iterate_out_of_display_property (it);
5040 *position = it->position;
5042 return 1;
5045 else if (!frame_window_p)
5046 return 1;
5048 #ifdef HAVE_WINDOW_SYSTEM
5049 value = XCAR (XCDR (spec));
5050 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5051 if (! fringe_bitmap)
5052 /* If we return here, POSITION has been advanced
5053 across the text with this property. */
5055 if (it && it->bidi_p)
5057 it->position = *position;
5058 iterate_out_of_display_property (it);
5059 *position = it->position;
5061 return 1;
5064 if (it)
5066 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5068 if (CONSP (XCDR (XCDR (spec))))
5070 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5071 int face_id2 = lookup_derived_face (it->f, face_name,
5072 FRINGE_FACE_ID, false);
5073 if (face_id2 >= 0)
5074 face_id = face_id2;
5077 /* Save current settings of IT so that we can restore them
5078 when we are finished with the glyph property value. */
5079 push_it (it, position);
5081 it->area = TEXT_AREA;
5082 it->what = IT_IMAGE;
5083 it->image_id = -1; /* no image */
5084 it->position = start_pos;
5085 it->object = NILP (object) ? it->w->contents : object;
5086 it->method = GET_FROM_IMAGE;
5087 it->from_overlay = Qnil;
5088 it->face_id = face_id;
5089 it->from_disp_prop_p = true;
5091 /* Say that we haven't consumed the characters with
5092 `display' property yet. The call to pop_it in
5093 set_iterator_to_next will clean this up. */
5094 *position = start_pos;
5096 if (EQ (XCAR (spec), Qleft_fringe))
5098 it->left_user_fringe_bitmap = fringe_bitmap;
5099 it->left_user_fringe_face_id = face_id;
5101 else
5103 it->right_user_fringe_bitmap = fringe_bitmap;
5104 it->right_user_fringe_face_id = face_id;
5107 #endif /* HAVE_WINDOW_SYSTEM */
5108 return 1;
5111 /* Prepare to handle `((margin left-margin) ...)',
5112 `((margin right-margin) ...)' and `((margin nil) ...)'
5113 prefixes for display specifications. */
5114 location = Qunbound;
5115 if (CONSP (spec) && CONSP (XCAR (spec)))
5117 Lisp_Object tem;
5119 value = XCDR (spec);
5120 if (CONSP (value))
5121 value = XCAR (value);
5123 tem = XCAR (spec);
5124 if (EQ (XCAR (tem), Qmargin)
5125 && (tem = XCDR (tem),
5126 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5127 (NILP (tem)
5128 || EQ (tem, Qleft_margin)
5129 || EQ (tem, Qright_margin))))
5130 location = tem;
5133 if (EQ (location, Qunbound))
5135 location = Qnil;
5136 value = spec;
5139 /* After this point, VALUE is the property after any
5140 margin prefix has been stripped. It must be a string,
5141 an image specification, or `(space ...)'.
5143 LOCATION specifies where to display: `left-margin',
5144 `right-margin' or nil. */
5146 bool valid_p = (STRINGP (value)
5147 #ifdef HAVE_WINDOW_SYSTEM
5148 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5149 && valid_image_p (value))
5150 #endif /* not HAVE_WINDOW_SYSTEM */
5151 || (CONSP (value) && EQ (XCAR (value), Qspace))
5152 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5153 && valid_xwidget_spec_p (value)));
5155 if (valid_p && display_replaced == 0)
5157 int retval = 1;
5159 if (!it)
5161 /* Callers need to know whether the display spec is any kind
5162 of `(space ...)' spec that is about to affect text-area
5163 display. */
5164 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5165 retval = 2;
5166 return retval;
5169 /* Save current settings of IT so that we can restore them
5170 when we are finished with the glyph property value. */
5171 push_it (it, position);
5172 it->from_overlay = overlay;
5173 it->from_disp_prop_p = true;
5175 if (NILP (location))
5176 it->area = TEXT_AREA;
5177 else if (EQ (location, Qleft_margin))
5178 it->area = LEFT_MARGIN_AREA;
5179 else
5180 it->area = RIGHT_MARGIN_AREA;
5182 if (STRINGP (value))
5184 it->string = value;
5185 it->multibyte_p = STRING_MULTIBYTE (it->string);
5186 it->current.overlay_string_index = -1;
5187 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5188 it->end_charpos = it->string_nchars = SCHARS (it->string);
5189 it->method = GET_FROM_STRING;
5190 it->stop_charpos = 0;
5191 it->prev_stop = 0;
5192 it->base_level_stop = 0;
5193 it->string_from_display_prop_p = true;
5194 /* Say that we haven't consumed the characters with
5195 `display' property yet. The call to pop_it in
5196 set_iterator_to_next will clean this up. */
5197 if (BUFFERP (object))
5198 *position = start_pos;
5200 /* Force paragraph direction to be that of the parent
5201 object. If the parent object's paragraph direction is
5202 not yet determined, default to L2R. */
5203 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5204 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5205 else
5206 it->paragraph_embedding = L2R;
5208 /* Set up the bidi iterator for this display string. */
5209 if (it->bidi_p)
5211 it->bidi_it.string.lstring = it->string;
5212 it->bidi_it.string.s = NULL;
5213 it->bidi_it.string.schars = it->end_charpos;
5214 it->bidi_it.string.bufpos = bufpos;
5215 it->bidi_it.string.from_disp_str = true;
5216 it->bidi_it.string.unibyte = !it->multibyte_p;
5217 it->bidi_it.w = it->w;
5218 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5221 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5223 it->method = GET_FROM_STRETCH;
5224 it->object = value;
5225 *position = it->position = start_pos;
5226 retval = 1 + (it->area == TEXT_AREA);
5228 else if (valid_xwidget_spec_p (value))
5230 it->what = IT_XWIDGET;
5231 it->method = GET_FROM_XWIDGET;
5232 it->position = start_pos;
5233 it->object = NILP (object) ? it->w->contents : object;
5234 *position = start_pos;
5235 it->xwidget = lookup_xwidget (value);
5237 #ifdef HAVE_WINDOW_SYSTEM
5238 else
5240 it->what = IT_IMAGE;
5241 it->image_id = lookup_image (it->f, value);
5242 it->position = start_pos;
5243 it->object = NILP (object) ? it->w->contents : object;
5244 it->method = GET_FROM_IMAGE;
5246 /* Say that we haven't consumed the characters with
5247 `display' property yet. The call to pop_it in
5248 set_iterator_to_next will clean this up. */
5249 *position = start_pos;
5251 #endif /* HAVE_WINDOW_SYSTEM */
5253 return retval;
5256 /* Invalid property or property not supported. Restore
5257 POSITION to what it was before. */
5258 *position = start_pos;
5259 return 0;
5262 /* Check if PROP is a display property value whose text should be
5263 treated as intangible. OVERLAY is the overlay from which PROP
5264 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5265 specify the buffer position covered by PROP. */
5267 bool
5268 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5269 ptrdiff_t charpos, ptrdiff_t bytepos)
5271 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5272 struct text_pos position;
5274 SET_TEXT_POS (position, charpos, bytepos);
5275 return (handle_display_spec (NULL, prop, Qnil, overlay,
5276 &position, charpos, frame_window_p)
5277 != 0);
5281 /* Return true if PROP is a display sub-property value containing STRING.
5283 Implementation note: this and the following function are really
5284 special cases of handle_display_spec and
5285 handle_single_display_spec, and should ideally use the same code.
5286 Until they do, these two pairs must be consistent and must be
5287 modified in sync. */
5289 static bool
5290 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5292 if (EQ (string, prop))
5293 return true;
5295 /* Skip over `when FORM'. */
5296 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5298 prop = XCDR (prop);
5299 if (!CONSP (prop))
5300 return false;
5301 /* Actually, the condition following `when' should be eval'ed,
5302 like handle_single_display_spec does, and we should return
5303 false if it evaluates to nil. However, this function is
5304 called only when the buffer was already displayed and some
5305 glyph in the glyph matrix was found to come from a display
5306 string. Therefore, the condition was already evaluated, and
5307 the result was non-nil, otherwise the display string wouldn't
5308 have been displayed and we would have never been called for
5309 this property. Thus, we can skip the evaluation and assume
5310 its result is non-nil. */
5311 prop = XCDR (prop);
5314 if (CONSP (prop))
5315 /* Skip over `margin LOCATION'. */
5316 if (EQ (XCAR (prop), Qmargin))
5318 prop = XCDR (prop);
5319 if (!CONSP (prop))
5320 return false;
5322 prop = XCDR (prop);
5323 if (!CONSP (prop))
5324 return false;
5327 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5331 /* Return true if STRING appears in the `display' property PROP. */
5333 static bool
5334 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5336 if (CONSP (prop)
5337 && !EQ (XCAR (prop), Qwhen)
5338 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5340 /* A list of sub-properties. */
5341 while (CONSP (prop))
5343 if (single_display_spec_string_p (XCAR (prop), string))
5344 return true;
5345 prop = XCDR (prop);
5348 else if (VECTORP (prop))
5350 /* A vector of sub-properties. */
5351 ptrdiff_t i;
5352 for (i = 0; i < ASIZE (prop); ++i)
5353 if (single_display_spec_string_p (AREF (prop, i), string))
5354 return true;
5356 else
5357 return single_display_spec_string_p (prop, string);
5359 return false;
5362 /* Look for STRING in overlays and text properties in the current
5363 buffer, between character positions FROM and TO (excluding TO).
5364 BACK_P means look back (in this case, TO is supposed to be
5365 less than FROM).
5366 Value is the first character position where STRING was found, or
5367 zero if it wasn't found before hitting TO.
5369 This function may only use code that doesn't eval because it is
5370 called asynchronously from note_mouse_highlight. */
5372 static ptrdiff_t
5373 string_buffer_position_lim (Lisp_Object string,
5374 ptrdiff_t from, ptrdiff_t to, bool back_p)
5376 Lisp_Object limit, prop, pos;
5377 bool found = false;
5379 pos = make_number (max (from, BEGV));
5381 if (!back_p) /* looking forward */
5383 limit = make_number (min (to, ZV));
5384 while (!found && !EQ (pos, limit))
5386 prop = Fget_char_property (pos, Qdisplay, Qnil);
5387 if (!NILP (prop) && display_prop_string_p (prop, string))
5388 found = true;
5389 else
5390 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5391 limit);
5394 else /* looking back */
5396 limit = make_number (max (to, BEGV));
5397 while (!found && !EQ (pos, limit))
5399 prop = Fget_char_property (pos, Qdisplay, Qnil);
5400 if (!NILP (prop) && display_prop_string_p (prop, string))
5401 found = true;
5402 else
5403 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5404 limit);
5408 return found ? XINT (pos) : 0;
5411 /* Determine which buffer position in current buffer STRING comes from.
5412 AROUND_CHARPOS is an approximate position where it could come from.
5413 Value is the buffer position or 0 if it couldn't be determined.
5415 This function is necessary because we don't record buffer positions
5416 in glyphs generated from strings (to keep struct glyph small).
5417 This function may only use code that doesn't eval because it is
5418 called asynchronously from note_mouse_highlight. */
5420 static ptrdiff_t
5421 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5423 const int MAX_DISTANCE = 1000;
5424 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5425 around_charpos + MAX_DISTANCE,
5426 false);
5428 if (!found)
5429 found = string_buffer_position_lim (string, around_charpos,
5430 around_charpos - MAX_DISTANCE, true);
5431 return found;
5436 /***********************************************************************
5437 `composition' property
5438 ***********************************************************************/
5440 /* Set up iterator IT from `composition' property at its current
5441 position. Called from handle_stop. */
5443 static enum prop_handled
5444 handle_composition_prop (struct it *it)
5446 Lisp_Object prop, string;
5447 ptrdiff_t pos, pos_byte, start, end;
5449 if (STRINGP (it->string))
5451 unsigned char *s;
5453 pos = IT_STRING_CHARPOS (*it);
5454 pos_byte = IT_STRING_BYTEPOS (*it);
5455 string = it->string;
5456 s = SDATA (string) + pos_byte;
5457 it->c = STRING_CHAR (s);
5459 else
5461 pos = IT_CHARPOS (*it);
5462 pos_byte = IT_BYTEPOS (*it);
5463 string = Qnil;
5464 it->c = FETCH_CHAR (pos_byte);
5467 /* If there's a valid composition and point is not inside of the
5468 composition (in the case that the composition is from the current
5469 buffer), draw a glyph composed from the composition components. */
5470 if (find_composition (pos, -1, &start, &end, &prop, string)
5471 && composition_valid_p (start, end, prop)
5472 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5474 if (start < pos)
5475 /* As we can't handle this situation (perhaps font-lock added
5476 a new composition), we just return here hoping that next
5477 redisplay will detect this composition much earlier. */
5478 return HANDLED_NORMALLY;
5479 if (start != pos)
5481 if (STRINGP (it->string))
5482 pos_byte = string_char_to_byte (it->string, start);
5483 else
5484 pos_byte = CHAR_TO_BYTE (start);
5486 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5487 prop, string);
5489 if (it->cmp_it.id >= 0)
5491 it->cmp_it.ch = -1;
5492 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5493 it->cmp_it.nglyphs = -1;
5497 return HANDLED_NORMALLY;
5502 /***********************************************************************
5503 Overlay strings
5504 ***********************************************************************/
5506 /* The following structure is used to record overlay strings for
5507 later sorting in load_overlay_strings. */
5509 struct overlay_entry
5511 Lisp_Object overlay;
5512 Lisp_Object string;
5513 EMACS_INT priority;
5514 bool after_string_p;
5518 /* Set up iterator IT from overlay strings at its current position.
5519 Called from handle_stop. */
5521 static enum prop_handled
5522 handle_overlay_change (struct it *it)
5524 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5525 return HANDLED_RECOMPUTE_PROPS;
5526 else
5527 return HANDLED_NORMALLY;
5531 /* Set up the next overlay string for delivery by IT, if there is an
5532 overlay string to deliver. Called by set_iterator_to_next when the
5533 end of the current overlay string is reached. If there are more
5534 overlay strings to display, IT->string and
5535 IT->current.overlay_string_index are set appropriately here.
5536 Otherwise IT->string is set to nil. */
5538 static void
5539 next_overlay_string (struct it *it)
5541 ++it->current.overlay_string_index;
5542 if (it->current.overlay_string_index == it->n_overlay_strings)
5544 /* No more overlay strings. Restore IT's settings to what
5545 they were before overlay strings were processed, and
5546 continue to deliver from current_buffer. */
5548 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5549 pop_it (it);
5550 eassert (it->sp > 0
5551 || (NILP (it->string)
5552 && it->method == GET_FROM_BUFFER
5553 && it->stop_charpos >= BEGV
5554 && it->stop_charpos <= it->end_charpos));
5555 it->current.overlay_string_index = -1;
5556 it->n_overlay_strings = 0;
5557 /* If there's an empty display string on the stack, pop the
5558 stack, to resync the bidi iterator with IT's position. Such
5559 empty strings are pushed onto the stack in
5560 get_overlay_strings_1. */
5561 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5562 pop_it (it);
5564 /* Since we've exhausted overlay strings at this buffer
5565 position, set the flag to ignore overlays until we move to
5566 another position. The flag is reset in
5567 next_element_from_buffer. */
5568 it->ignore_overlay_strings_at_pos_p = true;
5570 /* If we're at the end of the buffer, record that we have
5571 processed the overlay strings there already, so that
5572 next_element_from_buffer doesn't try it again. */
5573 if (NILP (it->string)
5574 && IT_CHARPOS (*it) >= it->end_charpos
5575 && it->overlay_strings_charpos >= it->end_charpos)
5576 it->overlay_strings_at_end_processed_p = true;
5577 /* Note: we reset overlay_strings_charpos only here, to make
5578 sure the just-processed overlays were indeed at EOB.
5579 Otherwise, overlays on text with invisible text property,
5580 which are processed with IT's position past the invisible
5581 text, might fool us into thinking the overlays at EOB were
5582 already processed (linum-mode can cause this, for
5583 example). */
5584 it->overlay_strings_charpos = -1;
5586 else
5588 /* There are more overlay strings to process. If
5589 IT->current.overlay_string_index has advanced to a position
5590 where we must load IT->overlay_strings with more strings, do
5591 it. We must load at the IT->overlay_strings_charpos where
5592 IT->n_overlay_strings was originally computed; when invisible
5593 text is present, this might not be IT_CHARPOS (Bug#7016). */
5594 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5596 if (it->current.overlay_string_index && i == 0)
5597 load_overlay_strings (it, it->overlay_strings_charpos);
5599 /* Initialize IT to deliver display elements from the overlay
5600 string. */
5601 it->string = it->overlay_strings[i];
5602 it->multibyte_p = STRING_MULTIBYTE (it->string);
5603 SET_TEXT_POS (it->current.string_pos, 0, 0);
5604 it->method = GET_FROM_STRING;
5605 it->stop_charpos = 0;
5606 it->end_charpos = SCHARS (it->string);
5607 if (it->cmp_it.stop_pos >= 0)
5608 it->cmp_it.stop_pos = 0;
5609 it->prev_stop = 0;
5610 it->base_level_stop = 0;
5612 /* Set up the bidi iterator for this overlay string. */
5613 if (it->bidi_p)
5615 it->bidi_it.string.lstring = it->string;
5616 it->bidi_it.string.s = NULL;
5617 it->bidi_it.string.schars = SCHARS (it->string);
5618 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5619 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5620 it->bidi_it.string.unibyte = !it->multibyte_p;
5621 it->bidi_it.w = it->w;
5622 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5626 CHECK_IT (it);
5630 /* Compare two overlay_entry structures E1 and E2. Used as a
5631 comparison function for qsort in load_overlay_strings. Overlay
5632 strings for the same position are sorted so that
5634 1. All after-strings come in front of before-strings, except
5635 when they come from the same overlay.
5637 2. Within after-strings, strings are sorted so that overlay strings
5638 from overlays with higher priorities come first.
5640 2. Within before-strings, strings are sorted so that overlay
5641 strings from overlays with higher priorities come last.
5643 Value is analogous to strcmp. */
5646 static int
5647 compare_overlay_entries (const void *e1, const void *e2)
5649 struct overlay_entry const *entry1 = e1;
5650 struct overlay_entry const *entry2 = e2;
5651 int result;
5653 if (entry1->after_string_p != entry2->after_string_p)
5655 /* Let after-strings appear in front of before-strings if
5656 they come from different overlays. */
5657 if (EQ (entry1->overlay, entry2->overlay))
5658 result = entry1->after_string_p ? 1 : -1;
5659 else
5660 result = entry1->after_string_p ? -1 : 1;
5662 else if (entry1->priority != entry2->priority)
5664 if (entry1->after_string_p)
5665 /* After-strings sorted in order of decreasing priority. */
5666 result = entry2->priority < entry1->priority ? -1 : 1;
5667 else
5668 /* Before-strings sorted in order of increasing priority. */
5669 result = entry1->priority < entry2->priority ? -1 : 1;
5671 else
5672 result = 0;
5674 return result;
5678 /* Load the vector IT->overlay_strings with overlay strings from IT's
5679 current buffer position, or from CHARPOS if that is > 0. Set
5680 IT->n_overlays to the total number of overlay strings found.
5682 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5683 a time. On entry into load_overlay_strings,
5684 IT->current.overlay_string_index gives the number of overlay
5685 strings that have already been loaded by previous calls to this
5686 function.
5688 IT->add_overlay_start contains an additional overlay start
5689 position to consider for taking overlay strings from, if non-zero.
5690 This position comes into play when the overlay has an `invisible'
5691 property, and both before and after-strings. When we've skipped to
5692 the end of the overlay, because of its `invisible' property, we
5693 nevertheless want its before-string to appear.
5694 IT->add_overlay_start will contain the overlay start position
5695 in this case.
5697 Overlay strings are sorted so that after-string strings come in
5698 front of before-string strings. Within before and after-strings,
5699 strings are sorted by overlay priority. See also function
5700 compare_overlay_entries. */
5702 static void
5703 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5705 Lisp_Object overlay, window, str, invisible;
5706 struct Lisp_Overlay *ov;
5707 ptrdiff_t start, end;
5708 ptrdiff_t n = 0, i, j;
5709 int invis;
5710 struct overlay_entry entriesbuf[20];
5711 ptrdiff_t size = ARRAYELTS (entriesbuf);
5712 struct overlay_entry *entries = entriesbuf;
5713 USE_SAFE_ALLOCA;
5715 if (charpos <= 0)
5716 charpos = IT_CHARPOS (*it);
5718 /* Append the overlay string STRING of overlay OVERLAY to vector
5719 `entries' which has size `size' and currently contains `n'
5720 elements. AFTER_P means STRING is an after-string of
5721 OVERLAY. */
5722 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5723 do \
5725 Lisp_Object priority; \
5727 if (n == size) \
5729 struct overlay_entry *old = entries; \
5730 SAFE_NALLOCA (entries, 2, size); \
5731 memcpy (entries, old, size * sizeof *entries); \
5732 size *= 2; \
5735 entries[n].string = (STRING); \
5736 entries[n].overlay = (OVERLAY); \
5737 priority = Foverlay_get ((OVERLAY), Qpriority); \
5738 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5739 entries[n].after_string_p = (AFTER_P); \
5740 ++n; \
5742 while (false)
5744 /* Process overlay before the overlay center. */
5745 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5747 XSETMISC (overlay, ov);
5748 eassert (OVERLAYP (overlay));
5749 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5750 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5752 if (end < charpos)
5753 break;
5755 /* Skip this overlay if it doesn't start or end at IT's current
5756 position. */
5757 if (end != charpos && start != charpos)
5758 continue;
5760 /* Skip this overlay if it doesn't apply to IT->w. */
5761 window = Foverlay_get (overlay, Qwindow);
5762 if (WINDOWP (window) && XWINDOW (window) != it->w)
5763 continue;
5765 /* If the text ``under'' the overlay is invisible, both before-
5766 and after-strings from this overlay are visible; start and
5767 end position are indistinguishable. */
5768 invisible = Foverlay_get (overlay, Qinvisible);
5769 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5771 /* If overlay has a non-empty before-string, record it. */
5772 if ((start == charpos || (end == charpos && invis != 0))
5773 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5774 && SCHARS (str))
5775 RECORD_OVERLAY_STRING (overlay, str, false);
5777 /* If overlay has a non-empty after-string, record it. */
5778 if ((end == charpos || (start == charpos && invis != 0))
5779 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5780 && SCHARS (str))
5781 RECORD_OVERLAY_STRING (overlay, str, true);
5784 /* Process overlays after the overlay center. */
5785 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5787 XSETMISC (overlay, ov);
5788 eassert (OVERLAYP (overlay));
5789 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5790 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5792 if (start > charpos)
5793 break;
5795 /* Skip this overlay if it doesn't start or end at IT's current
5796 position. */
5797 if (end != charpos && start != charpos)
5798 continue;
5800 /* Skip this overlay if it doesn't apply to IT->w. */
5801 window = Foverlay_get (overlay, Qwindow);
5802 if (WINDOWP (window) && XWINDOW (window) != it->w)
5803 continue;
5805 /* If the text ``under'' the overlay is invisible, it has a zero
5806 dimension, and both before- and after-strings apply. */
5807 invisible = Foverlay_get (overlay, Qinvisible);
5808 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5810 /* If overlay has a non-empty before-string, record it. */
5811 if ((start == charpos || (end == charpos && invis != 0))
5812 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5813 && SCHARS (str))
5814 RECORD_OVERLAY_STRING (overlay, str, false);
5816 /* If overlay has a non-empty after-string, record it. */
5817 if ((end == charpos || (start == charpos && invis != 0))
5818 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5819 && SCHARS (str))
5820 RECORD_OVERLAY_STRING (overlay, str, true);
5823 #undef RECORD_OVERLAY_STRING
5825 /* Sort entries. */
5826 if (n > 1)
5827 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5829 /* Record number of overlay strings, and where we computed it. */
5830 it->n_overlay_strings = n;
5831 it->overlay_strings_charpos = charpos;
5833 /* IT->current.overlay_string_index is the number of overlay strings
5834 that have already been consumed by IT. Copy some of the
5835 remaining overlay strings to IT->overlay_strings. */
5836 i = 0;
5837 j = it->current.overlay_string_index;
5838 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5840 it->overlay_strings[i] = entries[j].string;
5841 it->string_overlays[i++] = entries[j++].overlay;
5844 CHECK_IT (it);
5845 SAFE_FREE ();
5849 /* Get the first chunk of overlay strings at IT's current buffer
5850 position, or at CHARPOS if that is > 0. Value is true if at
5851 least one overlay string was found. */
5853 static bool
5854 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5856 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5857 process. This fills IT->overlay_strings with strings, and sets
5858 IT->n_overlay_strings to the total number of strings to process.
5859 IT->pos.overlay_string_index has to be set temporarily to zero
5860 because load_overlay_strings needs this; it must be set to -1
5861 when no overlay strings are found because a zero value would
5862 indicate a position in the first overlay string. */
5863 it->current.overlay_string_index = 0;
5864 load_overlay_strings (it, charpos);
5866 /* If we found overlay strings, set up IT to deliver display
5867 elements from the first one. Otherwise set up IT to deliver
5868 from current_buffer. */
5869 if (it->n_overlay_strings)
5871 /* Make sure we know settings in current_buffer, so that we can
5872 restore meaningful values when we're done with the overlay
5873 strings. */
5874 if (compute_stop_p)
5875 compute_stop_pos (it);
5876 eassert (it->face_id >= 0);
5878 /* Save IT's settings. They are restored after all overlay
5879 strings have been processed. */
5880 eassert (!compute_stop_p || it->sp == 0);
5882 /* When called from handle_stop, there might be an empty display
5883 string loaded. In that case, don't bother saving it. But
5884 don't use this optimization with the bidi iterator, since we
5885 need the corresponding pop_it call to resync the bidi
5886 iterator's position with IT's position, after we are done
5887 with the overlay strings. (The corresponding call to pop_it
5888 in case of an empty display string is in
5889 next_overlay_string.) */
5890 if (!(!it->bidi_p
5891 && STRINGP (it->string) && !SCHARS (it->string)))
5892 push_it (it, NULL);
5894 /* Set up IT to deliver display elements from the first overlay
5895 string. */
5896 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5897 it->string = it->overlay_strings[0];
5898 it->from_overlay = Qnil;
5899 it->stop_charpos = 0;
5900 eassert (STRINGP (it->string));
5901 it->end_charpos = SCHARS (it->string);
5902 it->prev_stop = 0;
5903 it->base_level_stop = 0;
5904 it->multibyte_p = STRING_MULTIBYTE (it->string);
5905 it->method = GET_FROM_STRING;
5906 it->from_disp_prop_p = 0;
5908 /* Force paragraph direction to be that of the parent
5909 buffer. */
5910 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5911 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5912 else
5913 it->paragraph_embedding = L2R;
5915 /* Set up the bidi iterator for this overlay string. */
5916 if (it->bidi_p)
5918 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5920 it->bidi_it.string.lstring = it->string;
5921 it->bidi_it.string.s = NULL;
5922 it->bidi_it.string.schars = SCHARS (it->string);
5923 it->bidi_it.string.bufpos = pos;
5924 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5925 it->bidi_it.string.unibyte = !it->multibyte_p;
5926 it->bidi_it.w = it->w;
5927 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5929 return true;
5932 it->current.overlay_string_index = -1;
5933 return false;
5936 static bool
5937 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5939 it->string = Qnil;
5940 it->method = GET_FROM_BUFFER;
5942 get_overlay_strings_1 (it, charpos, true);
5944 CHECK_IT (it);
5946 /* Value is true if we found at least one overlay string. */
5947 return STRINGP (it->string);
5952 /***********************************************************************
5953 Saving and restoring state
5954 ***********************************************************************/
5956 /* Save current settings of IT on IT->stack. Called, for example,
5957 before setting up IT for an overlay string, to be able to restore
5958 IT's settings to what they were after the overlay string has been
5959 processed. If POSITION is non-NULL, it is the position to save on
5960 the stack instead of IT->position. */
5962 static void
5963 push_it (struct it *it, struct text_pos *position)
5965 struct iterator_stack_entry *p;
5967 eassert (it->sp < IT_STACK_SIZE);
5968 p = it->stack + it->sp;
5970 p->stop_charpos = it->stop_charpos;
5971 p->prev_stop = it->prev_stop;
5972 p->base_level_stop = it->base_level_stop;
5973 p->cmp_it = it->cmp_it;
5974 eassert (it->face_id >= 0);
5975 p->face_id = it->face_id;
5976 p->string = it->string;
5977 p->method = it->method;
5978 p->from_overlay = it->from_overlay;
5979 switch (p->method)
5981 case GET_FROM_IMAGE:
5982 p->u.image.object = it->object;
5983 p->u.image.image_id = it->image_id;
5984 p->u.image.slice = it->slice;
5985 break;
5986 case GET_FROM_STRETCH:
5987 p->u.stretch.object = it->object;
5988 break;
5989 case GET_FROM_XWIDGET:
5990 p->u.xwidget.object = it->object;
5991 break;
5992 case GET_FROM_BUFFER:
5993 case GET_FROM_DISPLAY_VECTOR:
5994 case GET_FROM_STRING:
5995 case GET_FROM_C_STRING:
5996 break;
5997 default:
5998 emacs_abort ();
6000 p->position = position ? *position : it->position;
6001 p->current = it->current;
6002 p->end_charpos = it->end_charpos;
6003 p->string_nchars = it->string_nchars;
6004 p->area = it->area;
6005 p->multibyte_p = it->multibyte_p;
6006 p->avoid_cursor_p = it->avoid_cursor_p;
6007 p->space_width = it->space_width;
6008 p->font_height = it->font_height;
6009 p->voffset = it->voffset;
6010 p->string_from_display_prop_p = it->string_from_display_prop_p;
6011 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6012 p->display_ellipsis_p = false;
6013 p->line_wrap = it->line_wrap;
6014 p->bidi_p = it->bidi_p;
6015 p->paragraph_embedding = it->paragraph_embedding;
6016 p->from_disp_prop_p = it->from_disp_prop_p;
6017 ++it->sp;
6019 /* Save the state of the bidi iterator as well. */
6020 if (it->bidi_p)
6021 bidi_push_it (&it->bidi_it);
6024 static void
6025 iterate_out_of_display_property (struct it *it)
6027 bool buffer_p = !STRINGP (it->string);
6028 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6029 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6031 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6033 /* Maybe initialize paragraph direction. If we are at the beginning
6034 of a new paragraph, next_element_from_buffer may not have a
6035 chance to do that. */
6036 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6037 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6038 /* prev_stop can be zero, so check against BEGV as well. */
6039 while (it->bidi_it.charpos >= bob
6040 && it->prev_stop <= it->bidi_it.charpos
6041 && it->bidi_it.charpos < CHARPOS (it->position)
6042 && it->bidi_it.charpos < eob)
6043 bidi_move_to_visually_next (&it->bidi_it);
6044 /* Record the stop_pos we just crossed, for when we cross it
6045 back, maybe. */
6046 if (it->bidi_it.charpos > CHARPOS (it->position))
6047 it->prev_stop = CHARPOS (it->position);
6048 /* If we ended up not where pop_it put us, resync IT's
6049 positional members with the bidi iterator. */
6050 if (it->bidi_it.charpos != CHARPOS (it->position))
6051 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6052 if (buffer_p)
6053 it->current.pos = it->position;
6054 else
6055 it->current.string_pos = it->position;
6058 /* Restore IT's settings from IT->stack. Called, for example, when no
6059 more overlay strings must be processed, and we return to delivering
6060 display elements from a buffer, or when the end of a string from a
6061 `display' property is reached and we return to delivering display
6062 elements from an overlay string, or from a buffer. */
6064 static void
6065 pop_it (struct it *it)
6067 struct iterator_stack_entry *p;
6068 bool from_display_prop = it->from_disp_prop_p;
6069 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6071 eassert (it->sp > 0);
6072 --it->sp;
6073 p = it->stack + it->sp;
6074 it->stop_charpos = p->stop_charpos;
6075 it->prev_stop = p->prev_stop;
6076 it->base_level_stop = p->base_level_stop;
6077 it->cmp_it = p->cmp_it;
6078 it->face_id = p->face_id;
6079 it->current = p->current;
6080 it->position = p->position;
6081 it->string = p->string;
6082 it->from_overlay = p->from_overlay;
6083 if (NILP (it->string))
6084 SET_TEXT_POS (it->current.string_pos, -1, -1);
6085 it->method = p->method;
6086 switch (it->method)
6088 case GET_FROM_IMAGE:
6089 it->image_id = p->u.image.image_id;
6090 it->object = p->u.image.object;
6091 it->slice = p->u.image.slice;
6092 break;
6093 case GET_FROM_XWIDGET:
6094 it->object = p->u.xwidget.object;
6095 break;
6096 case GET_FROM_STRETCH:
6097 it->object = p->u.stretch.object;
6098 break;
6099 case GET_FROM_BUFFER:
6100 it->object = it->w->contents;
6101 break;
6102 case GET_FROM_STRING:
6104 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6106 /* Restore the face_box_p flag, since it could have been
6107 overwritten by the face of the object that we just finished
6108 displaying. */
6109 if (face)
6110 it->face_box_p = face->box != FACE_NO_BOX;
6111 it->object = it->string;
6113 break;
6114 case GET_FROM_DISPLAY_VECTOR:
6115 if (it->s)
6116 it->method = GET_FROM_C_STRING;
6117 else if (STRINGP (it->string))
6118 it->method = GET_FROM_STRING;
6119 else
6121 it->method = GET_FROM_BUFFER;
6122 it->object = it->w->contents;
6124 break;
6125 case GET_FROM_C_STRING:
6126 break;
6127 default:
6128 emacs_abort ();
6130 it->end_charpos = p->end_charpos;
6131 it->string_nchars = p->string_nchars;
6132 it->area = p->area;
6133 it->multibyte_p = p->multibyte_p;
6134 it->avoid_cursor_p = p->avoid_cursor_p;
6135 it->space_width = p->space_width;
6136 it->font_height = p->font_height;
6137 it->voffset = p->voffset;
6138 it->string_from_display_prop_p = p->string_from_display_prop_p;
6139 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6140 it->line_wrap = p->line_wrap;
6141 it->bidi_p = p->bidi_p;
6142 it->paragraph_embedding = p->paragraph_embedding;
6143 it->from_disp_prop_p = p->from_disp_prop_p;
6144 if (it->bidi_p)
6146 bidi_pop_it (&it->bidi_it);
6147 /* Bidi-iterate until we get out of the portion of text, if any,
6148 covered by a `display' text property or by an overlay with
6149 `display' property. (We cannot just jump there, because the
6150 internal coherency of the bidi iterator state can not be
6151 preserved across such jumps.) We also must determine the
6152 paragraph base direction if the overlay we just processed is
6153 at the beginning of a new paragraph. */
6154 if (from_display_prop
6155 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6156 iterate_out_of_display_property (it);
6158 eassert ((BUFFERP (it->object)
6159 && IT_CHARPOS (*it) == it->bidi_it.charpos
6160 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6161 || (STRINGP (it->object)
6162 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6163 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6164 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6166 /* If we move the iterator over text covered by a display property
6167 to a new buffer position, any info about previously seen overlays
6168 is no longer valid. */
6169 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6170 it->ignore_overlay_strings_at_pos_p = false;
6175 /***********************************************************************
6176 Moving over lines
6177 ***********************************************************************/
6179 /* Set IT's current position to the previous line start. */
6181 static void
6182 back_to_previous_line_start (struct it *it)
6184 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6186 DEC_BOTH (cp, bp);
6187 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6191 /* Move IT to the next line start.
6193 Value is true if a newline was found. Set *SKIPPED_P to true if
6194 we skipped over part of the text (as opposed to moving the iterator
6195 continuously over the text). Otherwise, don't change the value
6196 of *SKIPPED_P.
6198 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6199 iterator on the newline, if it was found.
6201 Newlines may come from buffer text, overlay strings, or strings
6202 displayed via the `display' property. That's the reason we can't
6203 simply use find_newline_no_quit.
6205 Note that this function may not skip over invisible text that is so
6206 because of text properties and immediately follows a newline. If
6207 it would, function reseat_at_next_visible_line_start, when called
6208 from set_iterator_to_next, would effectively make invisible
6209 characters following a newline part of the wrong glyph row, which
6210 leads to wrong cursor motion. */
6212 static bool
6213 forward_to_next_line_start (struct it *it, bool *skipped_p,
6214 struct bidi_it *bidi_it_prev)
6216 ptrdiff_t old_selective;
6217 bool newline_found_p = false;
6218 int n;
6219 const int MAX_NEWLINE_DISTANCE = 500;
6221 /* If already on a newline, just consume it to avoid unintended
6222 skipping over invisible text below. */
6223 if (it->what == IT_CHARACTER
6224 && it->c == '\n'
6225 && CHARPOS (it->position) == IT_CHARPOS (*it))
6227 if (it->bidi_p && bidi_it_prev)
6228 *bidi_it_prev = it->bidi_it;
6229 set_iterator_to_next (it, false);
6230 it->c = 0;
6231 return true;
6234 /* Don't handle selective display in the following. It's (a)
6235 unnecessary because it's done by the caller, and (b) leads to an
6236 infinite recursion because next_element_from_ellipsis indirectly
6237 calls this function. */
6238 old_selective = it->selective;
6239 it->selective = 0;
6241 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6242 from buffer text. */
6243 for (n = 0;
6244 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6245 n += !STRINGP (it->string))
6247 if (!get_next_display_element (it))
6248 return false;
6249 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6250 if (newline_found_p && it->bidi_p && bidi_it_prev)
6251 *bidi_it_prev = it->bidi_it;
6252 set_iterator_to_next (it, false);
6255 /* If we didn't find a newline near enough, see if we can use a
6256 short-cut. */
6257 if (!newline_found_p)
6259 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6260 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6261 1, &bytepos);
6262 Lisp_Object pos;
6264 eassert (!STRINGP (it->string));
6266 /* If there isn't any `display' property in sight, and no
6267 overlays, we can just use the position of the newline in
6268 buffer text. */
6269 if (it->stop_charpos >= limit
6270 || ((pos = Fnext_single_property_change (make_number (start),
6271 Qdisplay, Qnil,
6272 make_number (limit)),
6273 NILP (pos))
6274 && next_overlay_change (start) == ZV))
6276 if (!it->bidi_p)
6278 IT_CHARPOS (*it) = limit;
6279 IT_BYTEPOS (*it) = bytepos;
6281 else
6283 struct bidi_it bprev;
6285 /* Help bidi.c avoid expensive searches for display
6286 properties and overlays, by telling it that there are
6287 none up to `limit'. */
6288 if (it->bidi_it.disp_pos < limit)
6290 it->bidi_it.disp_pos = limit;
6291 it->bidi_it.disp_prop = 0;
6293 do {
6294 bprev = it->bidi_it;
6295 bidi_move_to_visually_next (&it->bidi_it);
6296 } while (it->bidi_it.charpos != limit);
6297 IT_CHARPOS (*it) = limit;
6298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6299 if (bidi_it_prev)
6300 *bidi_it_prev = bprev;
6302 *skipped_p = newline_found_p = true;
6304 else
6306 while (get_next_display_element (it)
6307 && !newline_found_p)
6309 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6310 if (newline_found_p && it->bidi_p && bidi_it_prev)
6311 *bidi_it_prev = it->bidi_it;
6312 set_iterator_to_next (it, false);
6317 it->selective = old_selective;
6318 return newline_found_p;
6322 /* Set IT's current position to the previous visible line start. Skip
6323 invisible text that is so either due to text properties or due to
6324 selective display. Caution: this does not change IT->current_x and
6325 IT->hpos. */
6327 static void
6328 back_to_previous_visible_line_start (struct it *it)
6330 while (IT_CHARPOS (*it) > BEGV)
6332 back_to_previous_line_start (it);
6334 if (IT_CHARPOS (*it) <= BEGV)
6335 break;
6337 /* If selective > 0, then lines indented more than its value are
6338 invisible. */
6339 if (it->selective > 0
6340 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6341 it->selective))
6342 continue;
6344 /* Check the newline before point for invisibility. */
6346 Lisp_Object prop;
6347 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6348 Qinvisible, it->window);
6349 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6350 continue;
6353 if (IT_CHARPOS (*it) <= BEGV)
6354 break;
6357 struct it it2;
6358 void *it2data = NULL;
6359 ptrdiff_t pos;
6360 ptrdiff_t beg, end;
6361 Lisp_Object val, overlay;
6363 SAVE_IT (it2, *it, it2data);
6365 /* If newline is part of a composition, continue from start of composition */
6366 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6367 && beg < IT_CHARPOS (*it))
6368 goto replaced;
6370 /* If newline is replaced by a display property, find start of overlay
6371 or interval and continue search from that point. */
6372 pos = --IT_CHARPOS (it2);
6373 --IT_BYTEPOS (it2);
6374 it2.sp = 0;
6375 bidi_unshelve_cache (NULL, false);
6376 it2.string_from_display_prop_p = false;
6377 it2.from_disp_prop_p = false;
6378 if (handle_display_prop (&it2) == HANDLED_RETURN
6379 && !NILP (val = get_char_property_and_overlay
6380 (make_number (pos), Qdisplay, Qnil, &overlay))
6381 && (OVERLAYP (overlay)
6382 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6383 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6385 RESTORE_IT (it, it, it2data);
6386 goto replaced;
6389 /* Newline is not replaced by anything -- so we are done. */
6390 RESTORE_IT (it, it, it2data);
6391 break;
6393 replaced:
6394 if (beg < BEGV)
6395 beg = BEGV;
6396 IT_CHARPOS (*it) = beg;
6397 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6401 it->continuation_lines_width = 0;
6403 eassert (IT_CHARPOS (*it) >= BEGV);
6404 eassert (IT_CHARPOS (*it) == BEGV
6405 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6406 CHECK_IT (it);
6410 /* Reseat iterator IT at the previous visible line start. Skip
6411 invisible text that is so either due to text properties or due to
6412 selective display. At the end, update IT's overlay information,
6413 face information etc. */
6415 void
6416 reseat_at_previous_visible_line_start (struct it *it)
6418 back_to_previous_visible_line_start (it);
6419 reseat (it, it->current.pos, true);
6420 CHECK_IT (it);
6424 /* Reseat iterator IT on the next visible line start in the current
6425 buffer. ON_NEWLINE_P means position IT on the newline
6426 preceding the line start. Skip over invisible text that is so
6427 because of selective display. Compute faces, overlays etc at the
6428 new position. Note that this function does not skip over text that
6429 is invisible because of text properties. */
6431 static void
6432 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6434 bool skipped_p = false;
6435 struct bidi_it bidi_it_prev;
6436 bool newline_found_p
6437 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6439 /* Skip over lines that are invisible because they are indented
6440 more than the value of IT->selective. */
6441 if (it->selective > 0)
6442 while (IT_CHARPOS (*it) < ZV
6443 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6444 it->selective))
6446 eassert (IT_BYTEPOS (*it) == BEGV
6447 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6448 newline_found_p =
6449 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6452 /* Position on the newline if that's what's requested. */
6453 if (on_newline_p && newline_found_p)
6455 if (STRINGP (it->string))
6457 if (IT_STRING_CHARPOS (*it) > 0)
6459 if (!it->bidi_p)
6461 --IT_STRING_CHARPOS (*it);
6462 --IT_STRING_BYTEPOS (*it);
6464 else
6466 /* We need to restore the bidi iterator to the state
6467 it had on the newline, and resync the IT's
6468 position with that. */
6469 it->bidi_it = bidi_it_prev;
6470 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6471 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6475 else if (IT_CHARPOS (*it) > BEGV)
6477 if (!it->bidi_p)
6479 --IT_CHARPOS (*it);
6480 --IT_BYTEPOS (*it);
6482 else
6484 /* We need to restore the bidi iterator to the state it
6485 had on the newline and resync IT with that. */
6486 it->bidi_it = bidi_it_prev;
6487 IT_CHARPOS (*it) = it->bidi_it.charpos;
6488 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6490 reseat (it, it->current.pos, false);
6493 else if (skipped_p)
6494 reseat (it, it->current.pos, false);
6496 CHECK_IT (it);
6501 /***********************************************************************
6502 Changing an iterator's position
6503 ***********************************************************************/
6505 /* Change IT's current position to POS in current_buffer.
6506 If FORCE_P, always check for text properties at the new position.
6507 Otherwise, text properties are only looked up if POS >=
6508 IT->check_charpos of a property. */
6510 static void
6511 reseat (struct it *it, struct text_pos pos, bool force_p)
6513 ptrdiff_t original_pos = IT_CHARPOS (*it);
6515 reseat_1 (it, pos, false);
6517 /* Determine where to check text properties. Avoid doing it
6518 where possible because text property lookup is very expensive. */
6519 if (force_p
6520 || CHARPOS (pos) > it->stop_charpos
6521 || CHARPOS (pos) < original_pos)
6523 if (it->bidi_p)
6525 /* For bidi iteration, we need to prime prev_stop and
6526 base_level_stop with our best estimations. */
6527 /* Implementation note: Of course, POS is not necessarily a
6528 stop position, so assigning prev_pos to it is a lie; we
6529 should have called compute_stop_backwards. However, if
6530 the current buffer does not include any R2L characters,
6531 that call would be a waste of cycles, because the
6532 iterator will never move back, and thus never cross this
6533 "fake" stop position. So we delay that backward search
6534 until the time we really need it, in next_element_from_buffer. */
6535 if (CHARPOS (pos) != it->prev_stop)
6536 it->prev_stop = CHARPOS (pos);
6537 if (CHARPOS (pos) < it->base_level_stop)
6538 it->base_level_stop = 0; /* meaning it's unknown */
6539 handle_stop (it);
6541 else
6543 handle_stop (it);
6544 it->prev_stop = it->base_level_stop = 0;
6549 CHECK_IT (it);
6553 /* Change IT's buffer position to POS. SET_STOP_P means set
6554 IT->stop_pos to POS, also. */
6556 static void
6557 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6559 /* Don't call this function when scanning a C string. */
6560 eassert (it->s == NULL);
6562 /* POS must be a reasonable value. */
6563 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6565 it->current.pos = it->position = pos;
6566 it->end_charpos = ZV;
6567 it->dpvec = NULL;
6568 it->current.dpvec_index = -1;
6569 it->current.overlay_string_index = -1;
6570 IT_STRING_CHARPOS (*it) = -1;
6571 IT_STRING_BYTEPOS (*it) = -1;
6572 it->string = Qnil;
6573 it->method = GET_FROM_BUFFER;
6574 it->object = it->w->contents;
6575 it->area = TEXT_AREA;
6576 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6577 it->sp = 0;
6578 it->string_from_display_prop_p = false;
6579 it->string_from_prefix_prop_p = false;
6581 it->from_disp_prop_p = false;
6582 it->face_before_selective_p = false;
6583 if (it->bidi_p)
6585 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6586 &it->bidi_it);
6587 bidi_unshelve_cache (NULL, false);
6588 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6589 it->bidi_it.string.s = NULL;
6590 it->bidi_it.string.lstring = Qnil;
6591 it->bidi_it.string.bufpos = 0;
6592 it->bidi_it.string.from_disp_str = false;
6593 it->bidi_it.string.unibyte = false;
6594 it->bidi_it.w = it->w;
6597 if (set_stop_p)
6599 it->stop_charpos = CHARPOS (pos);
6600 it->base_level_stop = CHARPOS (pos);
6602 /* This make the information stored in it->cmp_it invalidate. */
6603 it->cmp_it.id = -1;
6607 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6608 If S is non-null, it is a C string to iterate over. Otherwise,
6609 STRING gives a Lisp string to iterate over.
6611 If PRECISION > 0, don't return more then PRECISION number of
6612 characters from the string.
6614 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6615 characters have been returned. FIELD_WIDTH < 0 means an infinite
6616 field width.
6618 MULTIBYTE = 0 means disable processing of multibyte characters,
6619 MULTIBYTE > 0 means enable it,
6620 MULTIBYTE < 0 means use IT->multibyte_p.
6622 IT must be initialized via a prior call to init_iterator before
6623 calling this function. */
6625 static void
6626 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6627 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6628 int multibyte)
6630 /* No text property checks performed by default, but see below. */
6631 it->stop_charpos = -1;
6633 /* Set iterator position and end position. */
6634 memset (&it->current, 0, sizeof it->current);
6635 it->current.overlay_string_index = -1;
6636 it->current.dpvec_index = -1;
6637 eassert (charpos >= 0);
6639 /* If STRING is specified, use its multibyteness, otherwise use the
6640 setting of MULTIBYTE, if specified. */
6641 if (multibyte >= 0)
6642 it->multibyte_p = multibyte > 0;
6644 /* Bidirectional reordering of strings is controlled by the default
6645 value of bidi-display-reordering. Don't try to reorder while
6646 loading loadup.el, as the necessary character property tables are
6647 not yet available. */
6648 it->bidi_p =
6649 !redisplay__inhibit_bidi
6650 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6652 if (s == NULL)
6654 eassert (STRINGP (string));
6655 it->string = string;
6656 it->s = NULL;
6657 it->end_charpos = it->string_nchars = SCHARS (string);
6658 it->method = GET_FROM_STRING;
6659 it->current.string_pos = string_pos (charpos, string);
6661 if (it->bidi_p)
6663 it->bidi_it.string.lstring = string;
6664 it->bidi_it.string.s = NULL;
6665 it->bidi_it.string.schars = it->end_charpos;
6666 it->bidi_it.string.bufpos = 0;
6667 it->bidi_it.string.from_disp_str = false;
6668 it->bidi_it.string.unibyte = !it->multibyte_p;
6669 it->bidi_it.w = it->w;
6670 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6671 FRAME_WINDOW_P (it->f), &it->bidi_it);
6674 else
6676 it->s = (const unsigned char *) s;
6677 it->string = Qnil;
6679 /* Note that we use IT->current.pos, not it->current.string_pos,
6680 for displaying C strings. */
6681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6682 if (it->multibyte_p)
6684 it->current.pos = c_string_pos (charpos, s, true);
6685 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6687 else
6689 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6690 it->end_charpos = it->string_nchars = strlen (s);
6693 if (it->bidi_p)
6695 it->bidi_it.string.lstring = Qnil;
6696 it->bidi_it.string.s = (const unsigned char *) s;
6697 it->bidi_it.string.schars = it->end_charpos;
6698 it->bidi_it.string.bufpos = 0;
6699 it->bidi_it.string.from_disp_str = false;
6700 it->bidi_it.string.unibyte = !it->multibyte_p;
6701 it->bidi_it.w = it->w;
6702 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6703 &it->bidi_it);
6705 it->method = GET_FROM_C_STRING;
6708 /* PRECISION > 0 means don't return more than PRECISION characters
6709 from the string. */
6710 if (precision > 0 && it->end_charpos - charpos > precision)
6712 it->end_charpos = it->string_nchars = charpos + precision;
6713 if (it->bidi_p)
6714 it->bidi_it.string.schars = it->end_charpos;
6717 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6718 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6719 FIELD_WIDTH < 0 means infinite field width. This is useful for
6720 padding with `-' at the end of a mode line. */
6721 if (field_width < 0)
6722 field_width = INFINITY;
6723 /* Implementation note: We deliberately don't enlarge
6724 it->bidi_it.string.schars here to fit it->end_charpos, because
6725 the bidi iterator cannot produce characters out of thin air. */
6726 if (field_width > it->end_charpos - charpos)
6727 it->end_charpos = charpos + field_width;
6729 /* Use the standard display table for displaying strings. */
6730 if (DISP_TABLE_P (Vstandard_display_table))
6731 it->dp = XCHAR_TABLE (Vstandard_display_table);
6733 it->stop_charpos = charpos;
6734 it->prev_stop = charpos;
6735 it->base_level_stop = 0;
6736 if (it->bidi_p)
6738 it->bidi_it.first_elt = true;
6739 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6740 it->bidi_it.disp_pos = -1;
6742 if (s == NULL && it->multibyte_p)
6744 ptrdiff_t endpos = SCHARS (it->string);
6745 if (endpos > it->end_charpos)
6746 endpos = it->end_charpos;
6747 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6748 it->string);
6750 CHECK_IT (it);
6755 /***********************************************************************
6756 Iteration
6757 ***********************************************************************/
6759 /* Map enum it_method value to corresponding next_element_from_* function. */
6761 typedef bool (*next_element_function) (struct it *);
6763 static next_element_function const get_next_element[NUM_IT_METHODS] =
6765 next_element_from_buffer,
6766 next_element_from_display_vector,
6767 next_element_from_string,
6768 next_element_from_c_string,
6769 next_element_from_image,
6770 next_element_from_stretch,
6771 next_element_from_xwidget,
6774 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6777 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6778 (possibly with the following characters). */
6780 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6781 ((IT)->cmp_it.id >= 0 \
6782 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6783 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6784 END_CHARPOS, (IT)->w, \
6785 FACE_FROM_ID_OR_NULL ((IT)->f, \
6786 (IT)->face_id), \
6787 (IT)->string)))
6790 /* Lookup the char-table Vglyphless_char_display for character C (-1
6791 if we want information for no-font case), and return the display
6792 method symbol. By side-effect, update it->what and
6793 it->glyphless_method. This function is called from
6794 get_next_display_element for each character element, and from
6795 x_produce_glyphs when no suitable font was found. */
6797 Lisp_Object
6798 lookup_glyphless_char_display (int c, struct it *it)
6800 Lisp_Object glyphless_method = Qnil;
6802 if (CHAR_TABLE_P (Vglyphless_char_display)
6803 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6805 if (c >= 0)
6807 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6808 if (CONSP (glyphless_method))
6809 glyphless_method = FRAME_WINDOW_P (it->f)
6810 ? XCAR (glyphless_method)
6811 : XCDR (glyphless_method);
6813 else
6814 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6817 retry:
6818 if (NILP (glyphless_method))
6820 if (c >= 0)
6821 /* The default is to display the character by a proper font. */
6822 return Qnil;
6823 /* The default for the no-font case is to display an empty box. */
6824 glyphless_method = Qempty_box;
6826 if (EQ (glyphless_method, Qzero_width))
6828 if (c >= 0)
6829 return glyphless_method;
6830 /* This method can't be used for the no-font case. */
6831 glyphless_method = Qempty_box;
6833 if (EQ (glyphless_method, Qthin_space))
6834 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6835 else if (EQ (glyphless_method, Qempty_box))
6836 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6837 else if (EQ (glyphless_method, Qhex_code))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6839 else if (STRINGP (glyphless_method))
6840 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6841 else
6843 /* Invalid value. We use the default method. */
6844 glyphless_method = Qnil;
6845 goto retry;
6847 it->what = IT_GLYPHLESS;
6848 return glyphless_method;
6851 /* Merge escape glyph face and cache the result. */
6853 static struct frame *last_escape_glyph_frame = NULL;
6854 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6855 static int last_escape_glyph_merged_face_id = 0;
6857 static int
6858 merge_escape_glyph_face (struct it *it)
6860 int face_id;
6862 if (it->f == last_escape_glyph_frame
6863 && it->face_id == last_escape_glyph_face_id)
6864 face_id = last_escape_glyph_merged_face_id;
6865 else
6867 /* Merge the `escape-glyph' face into the current face. */
6868 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6869 last_escape_glyph_frame = it->f;
6870 last_escape_glyph_face_id = it->face_id;
6871 last_escape_glyph_merged_face_id = face_id;
6873 return face_id;
6876 /* Likewise for glyphless glyph face. */
6878 static struct frame *last_glyphless_glyph_frame = NULL;
6879 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6880 static int last_glyphless_glyph_merged_face_id = 0;
6883 merge_glyphless_glyph_face (struct it *it)
6885 int face_id;
6887 if (it->f == last_glyphless_glyph_frame
6888 && it->face_id == last_glyphless_glyph_face_id)
6889 face_id = last_glyphless_glyph_merged_face_id;
6890 else
6892 /* Merge the `glyphless-char' face into the current face. */
6893 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6894 last_glyphless_glyph_frame = it->f;
6895 last_glyphless_glyph_face_id = it->face_id;
6896 last_glyphless_glyph_merged_face_id = face_id;
6898 return face_id;
6901 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6902 be called before redisplaying windows, and when the frame's face
6903 cache is freed. */
6904 void
6905 forget_escape_and_glyphless_faces (void)
6907 last_escape_glyph_frame = NULL;
6908 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6909 last_glyphless_glyph_frame = NULL;
6910 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6913 /* Load IT's display element fields with information about the next
6914 display element from the current position of IT. Value is false if
6915 end of buffer (or C string) is reached. */
6917 static bool
6918 get_next_display_element (struct it *it)
6920 /* True means that we found a display element. False means that
6921 we hit the end of what we iterate over. Performance note: the
6922 function pointer `method' used here turns out to be faster than
6923 using a sequence of if-statements. */
6924 bool success_p;
6926 get_next:
6927 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6929 if (it->what == IT_CHARACTER)
6931 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6932 and only if (a) the resolved directionality of that character
6933 is R..." */
6934 /* FIXME: Do we need an exception for characters from display
6935 tables? */
6936 if (it->bidi_p && it->bidi_it.type == STRONG_R
6937 && !inhibit_bidi_mirroring)
6938 it->c = bidi_mirror_char (it->c);
6939 /* Map via display table or translate control characters.
6940 IT->c, IT->len etc. have been set to the next character by
6941 the function call above. If we have a display table, and it
6942 contains an entry for IT->c, translate it. Don't do this if
6943 IT->c itself comes from a display table, otherwise we could
6944 end up in an infinite recursion. (An alternative could be to
6945 count the recursion depth of this function and signal an
6946 error when a certain maximum depth is reached.) Is it worth
6947 it? */
6948 if (success_p && it->dpvec == NULL)
6950 Lisp_Object dv;
6951 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6952 bool nonascii_space_p = false;
6953 bool nonascii_hyphen_p = false;
6954 int c = it->c; /* This is the character to display. */
6956 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6958 eassert (SINGLE_BYTE_CHAR_P (c));
6959 if (unibyte_display_via_language_environment)
6961 c = DECODE_CHAR (unibyte, c);
6962 if (c < 0)
6963 c = BYTE8_TO_CHAR (it->c);
6965 else
6966 c = BYTE8_TO_CHAR (it->c);
6969 if (it->dp
6970 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6971 VECTORP (dv)))
6973 struct Lisp_Vector *v = XVECTOR (dv);
6975 /* Return the first character from the display table
6976 entry, if not empty. If empty, don't display the
6977 current character. */
6978 if (v->header.size)
6980 it->dpvec_char_len = it->len;
6981 it->dpvec = v->contents;
6982 it->dpend = v->contents + v->header.size;
6983 it->current.dpvec_index = 0;
6984 it->dpvec_face_id = -1;
6985 it->saved_face_id = it->face_id;
6986 it->method = GET_FROM_DISPLAY_VECTOR;
6987 it->ellipsis_p = false;
6989 else
6991 set_iterator_to_next (it, false);
6993 goto get_next;
6996 if (! NILP (lookup_glyphless_char_display (c, it)))
6998 if (it->what == IT_GLYPHLESS)
6999 goto done;
7000 /* Don't display this character. */
7001 set_iterator_to_next (it, false);
7002 goto get_next;
7005 /* If `nobreak-char-display' is non-nil, we display
7006 non-ASCII spaces and hyphens specially. */
7007 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7009 if (c == NO_BREAK_SPACE)
7010 nonascii_space_p = true;
7011 else if (c == SOFT_HYPHEN || c == HYPHEN
7012 || c == NON_BREAKING_HYPHEN)
7013 nonascii_hyphen_p = true;
7016 /* Translate control characters into `\003' or `^C' form.
7017 Control characters coming from a display table entry are
7018 currently not translated because we use IT->dpvec to hold
7019 the translation. This could easily be changed but I
7020 don't believe that it is worth doing.
7022 The characters handled by `nobreak-char-display' must be
7023 translated too.
7025 Non-printable characters and raw-byte characters are also
7026 translated to octal form. */
7027 if (((c < ' ' || c == 127) /* ASCII control chars. */
7028 ? (it->area != TEXT_AREA
7029 /* In mode line, treat \n, \t like other crl chars. */
7030 || (c != '\t'
7031 && it->glyph_row
7032 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7033 || (c != '\n' && c != '\t'))
7034 : (nonascii_space_p
7035 || nonascii_hyphen_p
7036 || CHAR_BYTE8_P (c)
7037 || ! CHAR_PRINTABLE_P (c))))
7039 /* C is a control character, non-ASCII space/hyphen,
7040 raw-byte, or a non-printable character which must be
7041 displayed either as '\003' or as `^C' where the '\\'
7042 and '^' can be defined in the display table. Fill
7043 IT->ctl_chars with glyphs for what we have to
7044 display. Then, set IT->dpvec to these glyphs. */
7045 Lisp_Object gc;
7046 int ctl_len;
7047 int face_id;
7048 int lface_id = 0;
7049 int escape_glyph;
7051 /* Handle control characters with ^. */
7053 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7055 int g;
7057 g = '^'; /* default glyph for Control */
7058 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7059 if (it->dp
7060 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7062 g = GLYPH_CODE_CHAR (gc);
7063 lface_id = GLYPH_CODE_FACE (gc);
7066 face_id = (lface_id
7067 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7068 : merge_escape_glyph_face (it));
7070 XSETINT (it->ctl_chars[0], g);
7071 XSETINT (it->ctl_chars[1], c ^ 0100);
7072 ctl_len = 2;
7073 goto display_control;
7076 /* Handle non-ascii space in the mode where it only gets
7077 highlighting. */
7079 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7081 /* Merge `nobreak-space' into the current face. */
7082 face_id = merge_faces (it->f, Qnobreak_space, 0,
7083 it->face_id);
7084 XSETINT (it->ctl_chars[0], ' ');
7085 ctl_len = 1;
7086 goto display_control;
7089 /* Handle non-ascii hyphens in the mode where it only
7090 gets highlighting. */
7092 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7094 /* Merge `nobreak-space' into the current face. */
7095 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7096 it->face_id);
7097 XSETINT (it->ctl_chars[0], '-');
7098 ctl_len = 1;
7099 goto display_control;
7102 /* Handle sequences that start with the "escape glyph". */
7104 /* the default escape glyph is \. */
7105 escape_glyph = '\\';
7107 if (it->dp
7108 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7110 escape_glyph = GLYPH_CODE_CHAR (gc);
7111 lface_id = GLYPH_CODE_FACE (gc);
7114 face_id = (lface_id
7115 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7116 : merge_escape_glyph_face (it));
7118 /* Draw non-ASCII space/hyphen with escape glyph: */
7120 if (nonascii_space_p || nonascii_hyphen_p)
7122 XSETINT (it->ctl_chars[0], escape_glyph);
7123 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7124 ctl_len = 2;
7125 goto display_control;
7129 char str[10];
7130 int len, i;
7132 if (CHAR_BYTE8_P (c))
7133 /* Display \200 instead of \17777600. */
7134 c = CHAR_TO_BYTE8 (c);
7135 len = sprintf (str, "%03o", c + 0u);
7137 XSETINT (it->ctl_chars[0], escape_glyph);
7138 for (i = 0; i < len; i++)
7139 XSETINT (it->ctl_chars[i + 1], str[i]);
7140 ctl_len = len + 1;
7143 display_control:
7144 /* Set up IT->dpvec and return first character from it. */
7145 it->dpvec_char_len = it->len;
7146 it->dpvec = it->ctl_chars;
7147 it->dpend = it->dpvec + ctl_len;
7148 it->current.dpvec_index = 0;
7149 it->dpvec_face_id = face_id;
7150 it->saved_face_id = it->face_id;
7151 it->method = GET_FROM_DISPLAY_VECTOR;
7152 it->ellipsis_p = false;
7153 goto get_next;
7155 it->char_to_display = c;
7157 else if (success_p)
7159 it->char_to_display = it->c;
7163 #ifdef HAVE_WINDOW_SYSTEM
7164 /* Adjust face id for a multibyte character. There are no multibyte
7165 character in unibyte text. */
7166 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7167 && it->multibyte_p
7168 && success_p
7169 && FRAME_WINDOW_P (it->f))
7171 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7173 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7175 /* Automatic composition with glyph-string. */
7176 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7178 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7180 else
7182 ptrdiff_t pos = (it->s ? -1
7183 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7184 : IT_CHARPOS (*it));
7185 int c;
7187 if (it->what == IT_CHARACTER)
7188 c = it->char_to_display;
7189 else
7191 struct composition *cmp = composition_table[it->cmp_it.id];
7192 int i;
7194 c = ' ';
7195 for (i = 0; i < cmp->glyph_len; i++)
7196 /* TAB in a composition means display glyphs with
7197 padding space on the left or right. */
7198 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7199 break;
7201 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7204 #endif /* HAVE_WINDOW_SYSTEM */
7206 done:
7207 /* Is this character the last one of a run of characters with
7208 box? If yes, set IT->end_of_box_run_p to true. */
7209 if (it->face_box_p
7210 && it->s == NULL)
7212 if (it->method == GET_FROM_STRING && it->sp)
7214 int face_id = underlying_face_id (it);
7215 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7217 if (face)
7219 if (face->box == FACE_NO_BOX)
7221 /* If the box comes from face properties in a
7222 display string, check faces in that string. */
7223 int string_face_id = face_after_it_pos (it);
7224 it->end_of_box_run_p
7225 = (FACE_FROM_ID (it->f, string_face_id)->box
7226 == FACE_NO_BOX);
7228 /* Otherwise, the box comes from the underlying face.
7229 If this is the last string character displayed, check
7230 the next buffer location. */
7231 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7232 /* n_overlay_strings is unreliable unless
7233 overlay_string_index is non-negative. */
7234 && ((it->current.overlay_string_index >= 0
7235 && (it->current.overlay_string_index
7236 == it->n_overlay_strings - 1))
7237 /* A string from display property. */
7238 || it->from_disp_prop_p))
7240 ptrdiff_t ignore;
7241 int next_face_id;
7242 bool text_from_string = false;
7243 /* Normally, the next buffer location is stored in
7244 IT->current.pos... */
7245 struct text_pos pos = it->current.pos;
7247 /* ...but for a string from a display property, the
7248 next buffer position is stored in the 'position'
7249 member of the iteration stack slot below the
7250 current one, see handle_single_display_spec. By
7251 contrast, it->current.pos was not yet updated to
7252 point to that buffer position; that will happen
7253 in pop_it, after we finish displaying the current
7254 string. Note that we already checked above that
7255 it->sp is positive, so subtracting one from it is
7256 safe. */
7257 if (it->from_disp_prop_p)
7259 int stackp = it->sp - 1;
7261 /* Find the stack level with data from buffer. */
7262 while (stackp >= 0
7263 && STRINGP ((it->stack + stackp)->string))
7264 stackp--;
7265 if (stackp < 0)
7267 /* If no stack slot was found for iterating
7268 a buffer, we are displaying text from a
7269 string, most probably the mode line or
7270 the header line, and that string has a
7271 display string on some of its
7272 characters. */
7273 text_from_string = true;
7274 pos = it->stack[it->sp - 1].position;
7276 else
7277 pos = (it->stack + stackp)->position;
7279 else
7280 INC_TEXT_POS (pos, it->multibyte_p);
7282 if (text_from_string)
7284 Lisp_Object base_string = it->stack[it->sp - 1].string;
7286 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7287 it->end_of_box_run_p = true;
7288 else
7290 next_face_id
7291 = face_at_string_position (it->w, base_string,
7292 CHARPOS (pos), 0,
7293 &ignore, face_id, false);
7294 it->end_of_box_run_p
7295 = (FACE_FROM_ID (it->f, next_face_id)->box
7296 == FACE_NO_BOX);
7299 else if (CHARPOS (pos) >= ZV)
7300 it->end_of_box_run_p = true;
7301 else
7303 next_face_id =
7304 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7305 CHARPOS (pos)
7306 + TEXT_PROP_DISTANCE_LIMIT,
7307 false, -1);
7308 it->end_of_box_run_p
7309 = (FACE_FROM_ID (it->f, next_face_id)->box
7310 == FACE_NO_BOX);
7315 /* next_element_from_display_vector sets this flag according to
7316 faces of the display vector glyphs, see there. */
7317 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7319 int face_id = face_after_it_pos (it);
7320 it->end_of_box_run_p
7321 = (face_id != it->face_id
7322 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7325 /* If we reached the end of the object we've been iterating (e.g., a
7326 display string or an overlay string), and there's something on
7327 IT->stack, proceed with what's on the stack. It doesn't make
7328 sense to return false if there's unprocessed stuff on the stack,
7329 because otherwise that stuff will never be displayed. */
7330 if (!success_p && it->sp > 0)
7332 set_iterator_to_next (it, false);
7333 success_p = get_next_display_element (it);
7336 /* Value is false if end of buffer or string reached. */
7337 return success_p;
7341 /* Move IT to the next display element.
7343 RESEAT_P means if called on a newline in buffer text,
7344 skip to the next visible line start.
7346 Functions get_next_display_element and set_iterator_to_next are
7347 separate because I find this arrangement easier to handle than a
7348 get_next_display_element function that also increments IT's
7349 position. The way it is we can first look at an iterator's current
7350 display element, decide whether it fits on a line, and if it does,
7351 increment the iterator position. The other way around we probably
7352 would either need a flag indicating whether the iterator has to be
7353 incremented the next time, or we would have to implement a
7354 decrement position function which would not be easy to write. */
7356 void
7357 set_iterator_to_next (struct it *it, bool reseat_p)
7359 /* Reset flags indicating start and end of a sequence of characters
7360 with box. Reset them at the start of this function because
7361 moving the iterator to a new position might set them. */
7362 it->start_of_box_run_p = it->end_of_box_run_p = false;
7364 switch (it->method)
7366 case GET_FROM_BUFFER:
7367 /* The current display element of IT is a character from
7368 current_buffer. Advance in the buffer, and maybe skip over
7369 invisible lines that are so because of selective display. */
7370 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7371 reseat_at_next_visible_line_start (it, false);
7372 else if (it->cmp_it.id >= 0)
7374 /* We are currently getting glyphs from a composition. */
7375 if (! it->bidi_p)
7377 IT_CHARPOS (*it) += it->cmp_it.nchars;
7378 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7380 else
7382 int i;
7384 /* Update IT's char/byte positions to point to the first
7385 character of the next grapheme cluster, or to the
7386 character visually after the current composition. */
7387 for (i = 0; i < it->cmp_it.nchars; i++)
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7390 IT_CHARPOS (*it) = it->bidi_it.charpos;
7393 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7394 && it->cmp_it.to < it->cmp_it.nglyphs)
7396 /* Composition created while scanning forward. Proceed
7397 to the next grapheme cluster. */
7398 it->cmp_it.from = it->cmp_it.to;
7400 else if ((it->bidi_p && it->cmp_it.reversed_p)
7401 && it->cmp_it.from > 0)
7403 /* Composition created while scanning backward. Proceed
7404 to the previous grapheme cluster. */
7405 it->cmp_it.to = it->cmp_it.from;
7407 else
7409 /* No more grapheme clusters in this composition.
7410 Find the next stop position. */
7411 ptrdiff_t stop = it->end_charpos;
7413 if (it->bidi_it.scan_dir < 0)
7414 /* Now we are scanning backward and don't know
7415 where to stop. */
7416 stop = -1;
7417 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7418 IT_BYTEPOS (*it), stop, Qnil);
7421 else
7423 eassert (it->len != 0);
7425 if (!it->bidi_p)
7427 IT_BYTEPOS (*it) += it->len;
7428 IT_CHARPOS (*it) += 1;
7430 else
7432 int prev_scan_dir = it->bidi_it.scan_dir;
7433 /* If this is a new paragraph, determine its base
7434 direction (a.k.a. its base embedding level). */
7435 if (it->bidi_it.new_paragraph)
7436 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7437 false);
7438 bidi_move_to_visually_next (&it->bidi_it);
7439 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7440 IT_CHARPOS (*it) = it->bidi_it.charpos;
7441 if (prev_scan_dir != it->bidi_it.scan_dir)
7443 /* As the scan direction was changed, we must
7444 re-compute the stop position for composition. */
7445 ptrdiff_t stop = it->end_charpos;
7446 if (it->bidi_it.scan_dir < 0)
7447 stop = -1;
7448 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7449 IT_BYTEPOS (*it), stop, Qnil);
7452 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7454 break;
7456 case GET_FROM_C_STRING:
7457 /* Current display element of IT is from a C string. */
7458 if (!it->bidi_p
7459 /* If the string position is beyond string's end, it means
7460 next_element_from_c_string is padding the string with
7461 blanks, in which case we bypass the bidi iterator,
7462 because it cannot deal with such virtual characters. */
7463 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7465 IT_BYTEPOS (*it) += it->len;
7466 IT_CHARPOS (*it) += 1;
7468 else
7470 bidi_move_to_visually_next (&it->bidi_it);
7471 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7472 IT_CHARPOS (*it) = it->bidi_it.charpos;
7474 break;
7476 case GET_FROM_DISPLAY_VECTOR:
7477 /* Current display element of IT is from a display table entry.
7478 Advance in the display table definition. Reset it to null if
7479 end reached, and continue with characters from buffers/
7480 strings. */
7481 ++it->current.dpvec_index;
7483 /* Restore face of the iterator to what they were before the
7484 display vector entry (these entries may contain faces). */
7485 it->face_id = it->saved_face_id;
7487 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7489 bool recheck_faces = it->ellipsis_p;
7491 if (it->s)
7492 it->method = GET_FROM_C_STRING;
7493 else if (STRINGP (it->string))
7494 it->method = GET_FROM_STRING;
7495 else
7497 it->method = GET_FROM_BUFFER;
7498 it->object = it->w->contents;
7501 it->dpvec = NULL;
7502 it->current.dpvec_index = -1;
7504 /* Skip over characters which were displayed via IT->dpvec. */
7505 if (it->dpvec_char_len < 0)
7506 reseat_at_next_visible_line_start (it, true);
7507 else if (it->dpvec_char_len > 0)
7509 it->len = it->dpvec_char_len;
7510 set_iterator_to_next (it, reseat_p);
7513 /* Maybe recheck faces after display vector. */
7514 if (recheck_faces)
7516 if (it->method == GET_FROM_STRING)
7517 it->stop_charpos = IT_STRING_CHARPOS (*it);
7518 else
7519 it->stop_charpos = IT_CHARPOS (*it);
7522 break;
7524 case GET_FROM_STRING:
7525 /* Current display element is a character from a Lisp string. */
7526 eassert (it->s == NULL && STRINGP (it->string));
7527 /* Don't advance past string end. These conditions are true
7528 when set_iterator_to_next is called at the end of
7529 get_next_display_element, in which case the Lisp string is
7530 already exhausted, and all we want is pop the iterator
7531 stack. */
7532 if (it->current.overlay_string_index >= 0)
7534 /* This is an overlay string, so there's no padding with
7535 spaces, and the number of characters in the string is
7536 where the string ends. */
7537 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7538 goto consider_string_end;
7540 else
7542 /* Not an overlay string. There could be padding, so test
7543 against it->end_charpos. */
7544 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7545 goto consider_string_end;
7547 if (it->cmp_it.id >= 0)
7549 /* We are delivering display elements from a composition.
7550 Update the string position past the grapheme cluster
7551 we've just processed. */
7552 if (! it->bidi_p)
7554 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7555 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7557 else
7559 int i;
7561 for (i = 0; i < it->cmp_it.nchars; i++)
7562 bidi_move_to_visually_next (&it->bidi_it);
7563 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7564 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7567 /* Did we exhaust all the grapheme clusters of this
7568 composition? */
7569 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7570 && (it->cmp_it.to < it->cmp_it.nglyphs))
7572 /* Not all the grapheme clusters were processed yet;
7573 advance to the next cluster. */
7574 it->cmp_it.from = it->cmp_it.to;
7576 else if ((it->bidi_p && it->cmp_it.reversed_p)
7577 && it->cmp_it.from > 0)
7579 /* Likewise: advance to the next cluster, but going in
7580 the reverse direction. */
7581 it->cmp_it.to = it->cmp_it.from;
7583 else
7585 /* This composition was fully processed; find the next
7586 candidate place for checking for composed
7587 characters. */
7588 /* Always limit string searches to the string length;
7589 any padding spaces are not part of the string, and
7590 there cannot be any compositions in that padding. */
7591 ptrdiff_t stop = SCHARS (it->string);
7593 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7594 stop = -1;
7595 else if (it->end_charpos < stop)
7597 /* Cf. PRECISION in reseat_to_string: we might be
7598 limited in how many of the string characters we
7599 need to deliver. */
7600 stop = it->end_charpos;
7602 composition_compute_stop_pos (&it->cmp_it,
7603 IT_STRING_CHARPOS (*it),
7604 IT_STRING_BYTEPOS (*it), stop,
7605 it->string);
7608 else
7610 if (!it->bidi_p
7611 /* If the string position is beyond string's end, it
7612 means next_element_from_string is padding the string
7613 with blanks, in which case we bypass the bidi
7614 iterator, because it cannot deal with such virtual
7615 characters. */
7616 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7618 IT_STRING_BYTEPOS (*it) += it->len;
7619 IT_STRING_CHARPOS (*it) += 1;
7621 else
7623 int prev_scan_dir = it->bidi_it.scan_dir;
7625 bidi_move_to_visually_next (&it->bidi_it);
7626 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7627 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7628 /* If the scan direction changes, we may need to update
7629 the place where to check for composed characters. */
7630 if (prev_scan_dir != it->bidi_it.scan_dir)
7632 ptrdiff_t stop = SCHARS (it->string);
7634 if (it->bidi_it.scan_dir < 0)
7635 stop = -1;
7636 else if (it->end_charpos < stop)
7637 stop = it->end_charpos;
7639 composition_compute_stop_pos (&it->cmp_it,
7640 IT_STRING_CHARPOS (*it),
7641 IT_STRING_BYTEPOS (*it), stop,
7642 it->string);
7647 consider_string_end:
7649 if (it->current.overlay_string_index >= 0)
7651 /* IT->string is an overlay string. Advance to the
7652 next, if there is one. */
7653 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7655 it->ellipsis_p = false;
7656 next_overlay_string (it);
7657 if (it->ellipsis_p)
7658 setup_for_ellipsis (it, 0);
7661 else
7663 /* IT->string is not an overlay string. If we reached
7664 its end, and there is something on IT->stack, proceed
7665 with what is on the stack. This can be either another
7666 string, this time an overlay string, or a buffer. */
7667 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7668 && it->sp > 0)
7670 pop_it (it);
7671 if (it->method == GET_FROM_STRING)
7672 goto consider_string_end;
7675 break;
7677 case GET_FROM_IMAGE:
7678 case GET_FROM_STRETCH:
7679 case GET_FROM_XWIDGET:
7681 /* The position etc with which we have to proceed are on
7682 the stack. The position may be at the end of a string,
7683 if the `display' property takes up the whole string. */
7684 eassert (it->sp > 0);
7685 pop_it (it);
7686 if (it->method == GET_FROM_STRING)
7687 goto consider_string_end;
7688 break;
7690 default:
7691 /* There are no other methods defined, so this should be a bug. */
7692 emacs_abort ();
7695 eassert (it->method != GET_FROM_STRING
7696 || (STRINGP (it->string)
7697 && IT_STRING_CHARPOS (*it) >= 0));
7700 /* Load IT's display element fields with information about the next
7701 display element which comes from a display table entry or from the
7702 result of translating a control character to one of the forms `^C'
7703 or `\003'.
7705 IT->dpvec holds the glyphs to return as characters.
7706 IT->saved_face_id holds the face id before the display vector--it
7707 is restored into IT->face_id in set_iterator_to_next. */
7709 static bool
7710 next_element_from_display_vector (struct it *it)
7712 Lisp_Object gc;
7713 int prev_face_id = it->face_id;
7714 int next_face_id;
7716 /* Precondition. */
7717 eassert (it->dpvec && it->current.dpvec_index >= 0);
7719 it->face_id = it->saved_face_id;
7721 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7722 That seemed totally bogus - so I changed it... */
7723 gc = it->dpvec[it->current.dpvec_index];
7725 if (GLYPH_CODE_P (gc))
7727 struct face *this_face, *prev_face, *next_face;
7729 it->c = GLYPH_CODE_CHAR (gc);
7730 it->len = CHAR_BYTES (it->c);
7732 /* The entry may contain a face id to use. Such a face id is
7733 the id of a Lisp face, not a realized face. A face id of
7734 zero means no face is specified. */
7735 if (it->dpvec_face_id >= 0)
7736 it->face_id = it->dpvec_face_id;
7737 else
7739 int lface_id = GLYPH_CODE_FACE (gc);
7740 if (lface_id > 0)
7741 it->face_id = merge_faces (it->f, Qt, lface_id,
7742 it->saved_face_id);
7745 /* Glyphs in the display vector could have the box face, so we
7746 need to set the related flags in the iterator, as
7747 appropriate. */
7748 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7749 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7751 /* Is this character the first character of a box-face run? */
7752 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7753 && (!prev_face
7754 || prev_face->box == FACE_NO_BOX));
7756 /* For the last character of the box-face run, we need to look
7757 either at the next glyph from the display vector, or at the
7758 face we saw before the display vector. */
7759 next_face_id = it->saved_face_id;
7760 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7762 if (it->dpvec_face_id >= 0)
7763 next_face_id = it->dpvec_face_id;
7764 else
7766 int lface_id =
7767 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7769 if (lface_id > 0)
7770 next_face_id = merge_faces (it->f, Qt, lface_id,
7771 it->saved_face_id);
7774 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7775 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7776 && (!next_face
7777 || next_face->box == FACE_NO_BOX));
7778 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7780 else
7781 /* Display table entry is invalid. Return a space. */
7782 it->c = ' ', it->len = 1;
7784 /* Don't change position and object of the iterator here. They are
7785 still the values of the character that had this display table
7786 entry or was translated, and that's what we want. */
7787 it->what = IT_CHARACTER;
7788 return true;
7791 /* Get the first element of string/buffer in the visual order, after
7792 being reseated to a new position in a string or a buffer. */
7793 static void
7794 get_visually_first_element (struct it *it)
7796 bool string_p = STRINGP (it->string) || it->s;
7797 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7798 ptrdiff_t bob = (string_p ? 0 : BEGV);
7800 if (STRINGP (it->string))
7802 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7803 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7805 else
7807 it->bidi_it.charpos = IT_CHARPOS (*it);
7808 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7811 if (it->bidi_it.charpos == eob)
7813 /* Nothing to do, but reset the FIRST_ELT flag, like
7814 bidi_paragraph_init does, because we are not going to
7815 call it. */
7816 it->bidi_it.first_elt = false;
7818 else if (it->bidi_it.charpos == bob
7819 || (!string_p
7820 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7821 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7823 /* If we are at the beginning of a line/string, we can produce
7824 the next element right away. */
7825 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7826 bidi_move_to_visually_next (&it->bidi_it);
7828 else
7830 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7832 /* We need to prime the bidi iterator starting at the line's or
7833 string's beginning, before we will be able to produce the
7834 next element. */
7835 if (string_p)
7836 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7837 else
7838 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7839 IT_BYTEPOS (*it), -1,
7840 &it->bidi_it.bytepos);
7841 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7844 /* Now return to buffer/string position where we were asked
7845 to get the next display element, and produce that. */
7846 bidi_move_to_visually_next (&it->bidi_it);
7848 while (it->bidi_it.bytepos != orig_bytepos
7849 && it->bidi_it.charpos < eob);
7852 /* Adjust IT's position information to where we ended up. */
7853 if (STRINGP (it->string))
7855 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7856 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7858 else
7860 IT_CHARPOS (*it) = it->bidi_it.charpos;
7861 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7864 if (STRINGP (it->string) || !it->s)
7866 ptrdiff_t stop, charpos, bytepos;
7868 if (STRINGP (it->string))
7870 eassert (!it->s);
7871 stop = SCHARS (it->string);
7872 if (stop > it->end_charpos)
7873 stop = it->end_charpos;
7874 charpos = IT_STRING_CHARPOS (*it);
7875 bytepos = IT_STRING_BYTEPOS (*it);
7877 else
7879 stop = it->end_charpos;
7880 charpos = IT_CHARPOS (*it);
7881 bytepos = IT_BYTEPOS (*it);
7883 if (it->bidi_it.scan_dir < 0)
7884 stop = -1;
7885 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7886 it->string);
7890 /* Load IT with the next display element from Lisp string IT->string.
7891 IT->current.string_pos is the current position within the string.
7892 If IT->current.overlay_string_index >= 0, the Lisp string is an
7893 overlay string. */
7895 static bool
7896 next_element_from_string (struct it *it)
7898 struct text_pos position;
7900 eassert (STRINGP (it->string));
7901 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7902 eassert (IT_STRING_CHARPOS (*it) >= 0);
7903 position = it->current.string_pos;
7905 /* With bidi reordering, the character to display might not be the
7906 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7907 that we were reseat()ed to a new string, whose paragraph
7908 direction is not known. */
7909 if (it->bidi_p && it->bidi_it.first_elt)
7911 get_visually_first_element (it);
7912 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7915 /* Time to check for invisible text? */
7916 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7918 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7920 if (!(!it->bidi_p
7921 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7922 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7924 /* With bidi non-linear iteration, we could find
7925 ourselves far beyond the last computed stop_charpos,
7926 with several other stop positions in between that we
7927 missed. Scan them all now, in buffer's logical
7928 order, until we find and handle the last stop_charpos
7929 that precedes our current position. */
7930 handle_stop_backwards (it, it->stop_charpos);
7931 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 else
7935 if (it->bidi_p)
7937 /* Take note of the stop position we just moved
7938 across, for when we will move back across it. */
7939 it->prev_stop = it->stop_charpos;
7940 /* If we are at base paragraph embedding level, take
7941 note of the last stop position seen at this
7942 level. */
7943 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7944 it->base_level_stop = it->stop_charpos;
7946 handle_stop (it);
7948 /* Since a handler may have changed IT->method, we must
7949 recurse here. */
7950 return GET_NEXT_DISPLAY_ELEMENT (it);
7953 else if (it->bidi_p
7954 /* If we are before prev_stop, we may have overstepped
7955 on our way backwards a stop_pos, and if so, we need
7956 to handle that stop_pos. */
7957 && IT_STRING_CHARPOS (*it) < it->prev_stop
7958 /* We can sometimes back up for reasons that have nothing
7959 to do with bidi reordering. E.g., compositions. The
7960 code below is only needed when we are above the base
7961 embedding level, so test for that explicitly. */
7962 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7964 /* If we lost track of base_level_stop, we have no better
7965 place for handle_stop_backwards to start from than string
7966 beginning. This happens, e.g., when we were reseated to
7967 the previous screenful of text by vertical-motion. */
7968 if (it->base_level_stop <= 0
7969 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7970 it->base_level_stop = 0;
7971 handle_stop_backwards (it, it->base_level_stop);
7972 return GET_NEXT_DISPLAY_ELEMENT (it);
7976 if (it->current.overlay_string_index >= 0)
7978 /* Get the next character from an overlay string. In overlay
7979 strings, there is no field width or padding with spaces to
7980 do. */
7981 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7983 it->what = IT_EOB;
7984 return false;
7986 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7987 IT_STRING_BYTEPOS (*it),
7988 it->bidi_it.scan_dir < 0
7989 ? -1
7990 : SCHARS (it->string))
7991 && next_element_from_composition (it))
7993 return true;
7995 else if (STRING_MULTIBYTE (it->string))
7997 const unsigned char *s = (SDATA (it->string)
7998 + IT_STRING_BYTEPOS (*it));
7999 it->c = string_char_and_length (s, &it->len);
8001 else
8003 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8004 it->len = 1;
8007 else
8009 /* Get the next character from a Lisp string that is not an
8010 overlay string. Such strings come from the mode line, for
8011 example. We may have to pad with spaces, or truncate the
8012 string. See also next_element_from_c_string. */
8013 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8015 it->what = IT_EOB;
8016 return false;
8018 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8020 /* Pad with spaces. */
8021 it->c = ' ', it->len = 1;
8022 CHARPOS (position) = BYTEPOS (position) = -1;
8024 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8025 IT_STRING_BYTEPOS (*it),
8026 it->bidi_it.scan_dir < 0
8027 ? -1
8028 : it->string_nchars)
8029 && next_element_from_composition (it))
8031 return true;
8033 else if (STRING_MULTIBYTE (it->string))
8035 const unsigned char *s = (SDATA (it->string)
8036 + IT_STRING_BYTEPOS (*it));
8037 it->c = string_char_and_length (s, &it->len);
8039 else
8041 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8042 it->len = 1;
8046 /* Record what we have and where it came from. */
8047 it->what = IT_CHARACTER;
8048 it->object = it->string;
8049 it->position = position;
8050 return true;
8054 /* Load IT with next display element from C string IT->s.
8055 IT->string_nchars is the maximum number of characters to return
8056 from the string. IT->end_charpos may be greater than
8057 IT->string_nchars when this function is called, in which case we
8058 may have to return padding spaces. Value is false if end of string
8059 reached, including padding spaces. */
8061 static bool
8062 next_element_from_c_string (struct it *it)
8064 bool success_p = true;
8066 eassert (it->s);
8067 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8068 it->what = IT_CHARACTER;
8069 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8070 it->object = make_number (0);
8072 /* With bidi reordering, the character to display might not be the
8073 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8074 we were reseated to a new string, whose paragraph direction is
8075 not known. */
8076 if (it->bidi_p && it->bidi_it.first_elt)
8077 get_visually_first_element (it);
8079 /* IT's position can be greater than IT->string_nchars in case a
8080 field width or precision has been specified when the iterator was
8081 initialized. */
8082 if (IT_CHARPOS (*it) >= it->end_charpos)
8084 /* End of the game. */
8085 it->what = IT_EOB;
8086 success_p = false;
8088 else if (IT_CHARPOS (*it) >= it->string_nchars)
8090 /* Pad with spaces. */
8091 it->c = ' ', it->len = 1;
8092 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8094 else if (it->multibyte_p)
8095 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8096 else
8097 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8099 return success_p;
8103 /* Set up IT to return characters from an ellipsis, if appropriate.
8104 The definition of the ellipsis glyphs may come from a display table
8105 entry. This function fills IT with the first glyph from the
8106 ellipsis if an ellipsis is to be displayed. */
8108 static bool
8109 next_element_from_ellipsis (struct it *it)
8111 if (it->selective_display_ellipsis_p)
8112 setup_for_ellipsis (it, it->len);
8113 else
8115 /* The face at the current position may be different from the
8116 face we find after the invisible text. Remember what it
8117 was in IT->saved_face_id, and signal that it's there by
8118 setting face_before_selective_p. */
8119 it->saved_face_id = it->face_id;
8120 it->method = GET_FROM_BUFFER;
8121 it->object = it->w->contents;
8122 reseat_at_next_visible_line_start (it, true);
8123 it->face_before_selective_p = true;
8126 return GET_NEXT_DISPLAY_ELEMENT (it);
8130 /* Deliver an image display element. The iterator IT is already
8131 filled with image information (done in handle_display_prop). Value
8132 is always true. */
8135 static bool
8136 next_element_from_image (struct it *it)
8138 it->what = IT_IMAGE;
8139 return true;
8142 static bool
8143 next_element_from_xwidget (struct it *it)
8145 it->what = IT_XWIDGET;
8146 return true;
8150 /* Fill iterator IT with next display element from a stretch glyph
8151 property. IT->object is the value of the text property. Value is
8152 always true. */
8154 static bool
8155 next_element_from_stretch (struct it *it)
8157 it->what = IT_STRETCH;
8158 return true;
8161 /* Scan backwards from IT's current position until we find a stop
8162 position, or until BEGV. This is called when we find ourself
8163 before both the last known prev_stop and base_level_stop while
8164 reordering bidirectional text. */
8166 static void
8167 compute_stop_pos_backwards (struct it *it)
8169 const int SCAN_BACK_LIMIT = 1000;
8170 struct text_pos pos;
8171 struct display_pos save_current = it->current;
8172 struct text_pos save_position = it->position;
8173 ptrdiff_t charpos = IT_CHARPOS (*it);
8174 ptrdiff_t where_we_are = charpos;
8175 ptrdiff_t save_stop_pos = it->stop_charpos;
8176 ptrdiff_t save_end_pos = it->end_charpos;
8178 eassert (NILP (it->string) && !it->s);
8179 eassert (it->bidi_p);
8180 it->bidi_p = false;
8183 it->end_charpos = min (charpos + 1, ZV);
8184 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8185 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8186 reseat_1 (it, pos, false);
8187 compute_stop_pos (it);
8188 /* We must advance forward, right? */
8189 if (it->stop_charpos <= charpos)
8190 emacs_abort ();
8192 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8194 if (it->stop_charpos <= where_we_are)
8195 it->prev_stop = it->stop_charpos;
8196 else
8197 it->prev_stop = BEGV;
8198 it->bidi_p = true;
8199 it->current = save_current;
8200 it->position = save_position;
8201 it->stop_charpos = save_stop_pos;
8202 it->end_charpos = save_end_pos;
8205 /* Scan forward from CHARPOS in the current buffer/string, until we
8206 find a stop position > current IT's position. Then handle the stop
8207 position before that. This is called when we bump into a stop
8208 position while reordering bidirectional text. CHARPOS should be
8209 the last previously processed stop_pos (or BEGV/0, if none were
8210 processed yet) whose position is less that IT's current
8211 position. */
8213 static void
8214 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8216 bool bufp = !STRINGP (it->string);
8217 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8218 struct display_pos save_current = it->current;
8219 struct text_pos save_position = it->position;
8220 struct text_pos pos1;
8221 ptrdiff_t next_stop;
8223 /* Scan in strict logical order. */
8224 eassert (it->bidi_p);
8225 it->bidi_p = false;
8228 it->prev_stop = charpos;
8229 if (bufp)
8231 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8232 reseat_1 (it, pos1, false);
8234 else
8235 it->current.string_pos = string_pos (charpos, it->string);
8236 compute_stop_pos (it);
8237 /* We must advance forward, right? */
8238 if (it->stop_charpos <= it->prev_stop)
8239 emacs_abort ();
8240 charpos = it->stop_charpos;
8242 while (charpos <= where_we_are);
8244 it->bidi_p = true;
8245 it->current = save_current;
8246 it->position = save_position;
8247 next_stop = it->stop_charpos;
8248 it->stop_charpos = it->prev_stop;
8249 handle_stop (it);
8250 it->stop_charpos = next_stop;
8253 /* Load IT with the next display element from current_buffer. Value
8254 is false if end of buffer reached. IT->stop_charpos is the next
8255 position at which to stop and check for text properties or buffer
8256 end. */
8258 static bool
8259 next_element_from_buffer (struct it *it)
8261 bool success_p = true;
8263 eassert (IT_CHARPOS (*it) >= BEGV);
8264 eassert (NILP (it->string) && !it->s);
8265 eassert (!it->bidi_p
8266 || (EQ (it->bidi_it.string.lstring, Qnil)
8267 && it->bidi_it.string.s == NULL));
8269 /* With bidi reordering, the character to display might not be the
8270 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8271 we were reseat()ed to a new buffer position, which is potentially
8272 a different paragraph. */
8273 if (it->bidi_p && it->bidi_it.first_elt)
8275 get_visually_first_element (it);
8276 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8279 if (IT_CHARPOS (*it) >= it->stop_charpos)
8281 if (IT_CHARPOS (*it) >= it->end_charpos)
8283 bool overlay_strings_follow_p;
8285 /* End of the game, except when overlay strings follow that
8286 haven't been returned yet. */
8287 if (it->overlay_strings_at_end_processed_p)
8288 overlay_strings_follow_p = false;
8289 else
8291 it->overlay_strings_at_end_processed_p = true;
8292 overlay_strings_follow_p = get_overlay_strings (it, 0);
8295 if (overlay_strings_follow_p)
8296 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8297 else
8299 it->what = IT_EOB;
8300 it->position = it->current.pos;
8301 success_p = false;
8304 else if (!(!it->bidi_p
8305 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8306 || IT_CHARPOS (*it) == it->stop_charpos))
8308 /* With bidi non-linear iteration, we could find ourselves
8309 far beyond the last computed stop_charpos, with several
8310 other stop positions in between that we missed. Scan
8311 them all now, in buffer's logical order, until we find
8312 and handle the last stop_charpos that precedes our
8313 current position. */
8314 handle_stop_backwards (it, it->stop_charpos);
8315 it->ignore_overlay_strings_at_pos_p = false;
8316 return GET_NEXT_DISPLAY_ELEMENT (it);
8318 else
8320 if (it->bidi_p)
8322 /* Take note of the stop position we just moved across,
8323 for when we will move back across it. */
8324 it->prev_stop = it->stop_charpos;
8325 /* If we are at base paragraph embedding level, take
8326 note of the last stop position seen at this
8327 level. */
8328 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8329 it->base_level_stop = it->stop_charpos;
8331 handle_stop (it);
8332 it->ignore_overlay_strings_at_pos_p = false;
8333 return GET_NEXT_DISPLAY_ELEMENT (it);
8336 else if (it->bidi_p
8337 /* If we are before prev_stop, we may have overstepped on
8338 our way backwards a stop_pos, and if so, we need to
8339 handle that stop_pos. */
8340 && IT_CHARPOS (*it) < it->prev_stop
8341 /* We can sometimes back up for reasons that have nothing
8342 to do with bidi reordering. E.g., compositions. The
8343 code below is only needed when we are above the base
8344 embedding level, so test for that explicitly. */
8345 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8347 if (it->base_level_stop <= 0
8348 || IT_CHARPOS (*it) < it->base_level_stop)
8350 /* If we lost track of base_level_stop, we need to find
8351 prev_stop by looking backwards. This happens, e.g., when
8352 we were reseated to the previous screenful of text by
8353 vertical-motion. */
8354 it->base_level_stop = BEGV;
8355 compute_stop_pos_backwards (it);
8356 handle_stop_backwards (it, it->prev_stop);
8358 else
8359 handle_stop_backwards (it, it->base_level_stop);
8360 it->ignore_overlay_strings_at_pos_p = false;
8361 return GET_NEXT_DISPLAY_ELEMENT (it);
8363 else
8365 /* No face changes, overlays etc. in sight, so just return a
8366 character from current_buffer. */
8367 unsigned char *p;
8368 ptrdiff_t stop;
8370 /* We moved to the next buffer position, so any info about
8371 previously seen overlays is no longer valid. */
8372 it->ignore_overlay_strings_at_pos_p = false;
8374 /* Maybe run the redisplay end trigger hook. Performance note:
8375 This doesn't seem to cost measurable time. */
8376 if (it->redisplay_end_trigger_charpos
8377 && it->glyph_row
8378 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8379 run_redisplay_end_trigger_hook (it);
8381 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8382 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8383 stop)
8384 && next_element_from_composition (it))
8386 return true;
8389 /* Get the next character, maybe multibyte. */
8390 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8391 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8392 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8393 else
8394 it->c = *p, it->len = 1;
8396 /* Record what we have and where it came from. */
8397 it->what = IT_CHARACTER;
8398 it->object = it->w->contents;
8399 it->position = it->current.pos;
8401 /* Normally we return the character found above, except when we
8402 really want to return an ellipsis for selective display. */
8403 if (it->selective)
8405 if (it->c == '\n')
8407 /* A value of selective > 0 means hide lines indented more
8408 than that number of columns. */
8409 if (it->selective > 0
8410 && IT_CHARPOS (*it) + 1 < ZV
8411 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8412 IT_BYTEPOS (*it) + 1,
8413 it->selective))
8415 success_p = next_element_from_ellipsis (it);
8416 it->dpvec_char_len = -1;
8419 else if (it->c == '\r' && it->selective == -1)
8421 /* A value of selective == -1 means that everything from the
8422 CR to the end of the line is invisible, with maybe an
8423 ellipsis displayed for it. */
8424 success_p = next_element_from_ellipsis (it);
8425 it->dpvec_char_len = -1;
8430 /* Value is false if end of buffer reached. */
8431 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8432 return success_p;
8436 /* Run the redisplay end trigger hook for IT. */
8438 static void
8439 run_redisplay_end_trigger_hook (struct it *it)
8441 /* IT->glyph_row should be non-null, i.e. we should be actually
8442 displaying something, or otherwise we should not run the hook. */
8443 eassert (it->glyph_row);
8445 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8446 it->redisplay_end_trigger_charpos = 0;
8448 /* Since we are *trying* to run these functions, don't try to run
8449 them again, even if they get an error. */
8450 wset_redisplay_end_trigger (it->w, Qnil);
8451 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8452 make_number (charpos));
8454 /* Notice if it changed the face of the character we are on. */
8455 handle_face_prop (it);
8459 /* Deliver a composition display element. Unlike the other
8460 next_element_from_XXX, this function is not registered in the array
8461 get_next_element[]. It is called from next_element_from_buffer and
8462 next_element_from_string when necessary. */
8464 static bool
8465 next_element_from_composition (struct it *it)
8467 it->what = IT_COMPOSITION;
8468 it->len = it->cmp_it.nbytes;
8469 if (STRINGP (it->string))
8471 if (it->c < 0)
8473 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8474 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8475 return false;
8477 it->position = it->current.string_pos;
8478 it->object = it->string;
8479 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8480 IT_STRING_BYTEPOS (*it), it->string);
8482 else
8484 if (it->c < 0)
8486 IT_CHARPOS (*it) += it->cmp_it.nchars;
8487 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8488 if (it->bidi_p)
8490 if (it->bidi_it.new_paragraph)
8491 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8492 false);
8493 /* Resync the bidi iterator with IT's new position.
8494 FIXME: this doesn't support bidirectional text. */
8495 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8496 bidi_move_to_visually_next (&it->bidi_it);
8498 return false;
8500 it->position = it->current.pos;
8501 it->object = it->w->contents;
8502 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8503 IT_BYTEPOS (*it), Qnil);
8505 return true;
8510 /***********************************************************************
8511 Moving an iterator without producing glyphs
8512 ***********************************************************************/
8514 /* Check if iterator is at a position corresponding to a valid buffer
8515 position after some move_it_ call. */
8517 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8518 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8521 /* Move iterator IT to a specified buffer or X position within one
8522 line on the display without producing glyphs.
8524 OP should be a bit mask including some or all of these bits:
8525 MOVE_TO_X: Stop upon reaching x-position TO_X.
8526 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8527 Regardless of OP's value, stop upon reaching the end of the display line.
8529 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8530 This means, in particular, that TO_X includes window's horizontal
8531 scroll amount.
8533 The return value has several possible values that
8534 say what condition caused the scan to stop:
8536 MOVE_POS_MATCH_OR_ZV
8537 - when TO_POS or ZV was reached.
8539 MOVE_X_REACHED
8540 -when TO_X was reached before TO_POS or ZV were reached.
8542 MOVE_LINE_CONTINUED
8543 - when we reached the end of the display area and the line must
8544 be continued.
8546 MOVE_LINE_TRUNCATED
8547 - when we reached the end of the display area and the line is
8548 truncated.
8550 MOVE_NEWLINE_OR_CR
8551 - when we stopped at a line end, i.e. a newline or a CR and selective
8552 display is on. */
8554 static enum move_it_result
8555 move_it_in_display_line_to (struct it *it,
8556 ptrdiff_t to_charpos, int to_x,
8557 enum move_operation_enum op)
8559 enum move_it_result result = MOVE_UNDEFINED;
8560 struct glyph_row *saved_glyph_row;
8561 struct it wrap_it, atpos_it, atx_it, ppos_it;
8562 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8563 void *ppos_data = NULL;
8564 bool may_wrap = false;
8565 enum it_method prev_method = it->method;
8566 ptrdiff_t closest_pos UNINIT;
8567 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8568 bool saw_smaller_pos = prev_pos < to_charpos;
8570 /* Don't produce glyphs in produce_glyphs. */
8571 saved_glyph_row = it->glyph_row;
8572 it->glyph_row = NULL;
8574 /* Use wrap_it to save a copy of IT wherever a word wrap could
8575 occur. Use atpos_it to save a copy of IT at the desired buffer
8576 position, if found, so that we can scan ahead and check if the
8577 word later overshoots the window edge. Use atx_it similarly, for
8578 pixel positions. */
8579 wrap_it.sp = -1;
8580 atpos_it.sp = -1;
8581 atx_it.sp = -1;
8583 /* Use ppos_it under bidi reordering to save a copy of IT for the
8584 initial position. We restore that position in IT when we have
8585 scanned the entire display line without finding a match for
8586 TO_CHARPOS and all the character positions are greater than
8587 TO_CHARPOS. We then restart the scan from the initial position,
8588 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8589 the closest to TO_CHARPOS. */
8590 if (it->bidi_p)
8592 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8594 SAVE_IT (ppos_it, *it, ppos_data);
8595 closest_pos = IT_CHARPOS (*it);
8597 else
8598 closest_pos = ZV;
8601 #define BUFFER_POS_REACHED_P() \
8602 ((op & MOVE_TO_POS) != 0 \
8603 && BUFFERP (it->object) \
8604 && (IT_CHARPOS (*it) == to_charpos \
8605 || ((!it->bidi_p \
8606 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8607 && IT_CHARPOS (*it) > to_charpos) \
8608 || (it->what == IT_COMPOSITION \
8609 && ((IT_CHARPOS (*it) > to_charpos \
8610 && to_charpos >= it->cmp_it.charpos) \
8611 || (IT_CHARPOS (*it) < to_charpos \
8612 && to_charpos <= it->cmp_it.charpos)))) \
8613 && (it->method == GET_FROM_BUFFER \
8614 || (it->method == GET_FROM_DISPLAY_VECTOR \
8615 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8617 /* If there's a line-/wrap-prefix, handle it. */
8618 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8619 handle_line_prefix (it);
8621 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8622 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8624 while (true)
8626 int x, i, ascent = 0, descent = 0;
8628 /* Utility macro to reset an iterator with x, ascent, and descent. */
8629 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8630 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8631 (IT)->max_descent = descent)
8633 /* Stop if we move beyond TO_CHARPOS (after an image or a
8634 display string or stretch glyph). */
8635 if ((op & MOVE_TO_POS) != 0
8636 && BUFFERP (it->object)
8637 && it->method == GET_FROM_BUFFER
8638 && (((!it->bidi_p
8639 /* When the iterator is at base embedding level, we
8640 are guaranteed that characters are delivered for
8641 display in strictly increasing order of their
8642 buffer positions. */
8643 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8644 && IT_CHARPOS (*it) > to_charpos)
8645 || (it->bidi_p
8646 && (prev_method == GET_FROM_IMAGE
8647 || prev_method == GET_FROM_STRETCH
8648 || prev_method == GET_FROM_STRING)
8649 /* Passed TO_CHARPOS from left to right. */
8650 && ((prev_pos < to_charpos
8651 && IT_CHARPOS (*it) > to_charpos)
8652 /* Passed TO_CHARPOS from right to left. */
8653 || (prev_pos > to_charpos
8654 && IT_CHARPOS (*it) < to_charpos)))))
8656 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8658 result = MOVE_POS_MATCH_OR_ZV;
8659 break;
8661 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8662 /* If wrap_it is valid, the current position might be in a
8663 word that is wrapped. So, save the iterator in
8664 atpos_it and continue to see if wrapping happens. */
8665 SAVE_IT (atpos_it, *it, atpos_data);
8668 /* Stop when ZV reached.
8669 We used to stop here when TO_CHARPOS reached as well, but that is
8670 too soon if this glyph does not fit on this line. So we handle it
8671 explicitly below. */
8672 if (!get_next_display_element (it))
8674 result = MOVE_POS_MATCH_OR_ZV;
8675 break;
8678 if (it->line_wrap == TRUNCATE)
8680 if (BUFFER_POS_REACHED_P ())
8682 result = MOVE_POS_MATCH_OR_ZV;
8683 break;
8686 else
8688 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8690 if (IT_DISPLAYING_WHITESPACE (it))
8691 may_wrap = true;
8692 else if (may_wrap)
8694 /* We have reached a glyph that follows one or more
8695 whitespace characters. If the position is
8696 already found, we are done. */
8697 if (atpos_it.sp >= 0)
8699 RESTORE_IT (it, &atpos_it, atpos_data);
8700 result = MOVE_POS_MATCH_OR_ZV;
8701 goto done;
8703 if (atx_it.sp >= 0)
8705 RESTORE_IT (it, &atx_it, atx_data);
8706 result = MOVE_X_REACHED;
8707 goto done;
8709 /* Otherwise, we can wrap here. */
8710 SAVE_IT (wrap_it, *it, wrap_data);
8711 may_wrap = false;
8716 /* Remember the line height for the current line, in case
8717 the next element doesn't fit on the line. */
8718 ascent = it->max_ascent;
8719 descent = it->max_descent;
8721 /* The call to produce_glyphs will get the metrics of the
8722 display element IT is loaded with. Record the x-position
8723 before this display element, in case it doesn't fit on the
8724 line. */
8725 x = it->current_x;
8727 PRODUCE_GLYPHS (it);
8729 if (it->area != TEXT_AREA)
8731 prev_method = it->method;
8732 if (it->method == GET_FROM_BUFFER)
8733 prev_pos = IT_CHARPOS (*it);
8734 set_iterator_to_next (it, true);
8735 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8736 SET_TEXT_POS (this_line_min_pos,
8737 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8738 if (it->bidi_p
8739 && (op & MOVE_TO_POS)
8740 && IT_CHARPOS (*it) > to_charpos
8741 && IT_CHARPOS (*it) < closest_pos)
8742 closest_pos = IT_CHARPOS (*it);
8743 continue;
8746 /* The number of glyphs we get back in IT->nglyphs will normally
8747 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8748 character on a terminal frame, or (iii) a line end. For the
8749 second case, IT->nglyphs - 1 padding glyphs will be present.
8750 (On X frames, there is only one glyph produced for a
8751 composite character.)
8753 The behavior implemented below means, for continuation lines,
8754 that as many spaces of a TAB as fit on the current line are
8755 displayed there. For terminal frames, as many glyphs of a
8756 multi-glyph character are displayed in the current line, too.
8757 This is what the old redisplay code did, and we keep it that
8758 way. Under X, the whole shape of a complex character must
8759 fit on the line or it will be completely displayed in the
8760 next line.
8762 Note that both for tabs and padding glyphs, all glyphs have
8763 the same width. */
8764 if (it->nglyphs)
8766 /* More than one glyph or glyph doesn't fit on line. All
8767 glyphs have the same width. */
8768 int single_glyph_width = it->pixel_width / it->nglyphs;
8769 int new_x;
8770 int x_before_this_char = x;
8771 int hpos_before_this_char = it->hpos;
8773 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8775 new_x = x + single_glyph_width;
8777 /* We want to leave anything reaching TO_X to the caller. */
8778 if ((op & MOVE_TO_X) && new_x > to_x)
8780 if (BUFFER_POS_REACHED_P ())
8782 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8783 goto buffer_pos_reached;
8784 if (atpos_it.sp < 0)
8786 SAVE_IT (atpos_it, *it, atpos_data);
8787 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8790 else
8792 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8794 it->current_x = x;
8795 result = MOVE_X_REACHED;
8796 break;
8798 if (atx_it.sp < 0)
8800 SAVE_IT (atx_it, *it, atx_data);
8801 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8806 if (/* Lines are continued. */
8807 it->line_wrap != TRUNCATE
8808 && (/* And glyph doesn't fit on the line. */
8809 new_x > it->last_visible_x
8810 /* Or it fits exactly and we're on a window
8811 system frame. */
8812 || (new_x == it->last_visible_x
8813 && FRAME_WINDOW_P (it->f)
8814 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8815 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8816 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8818 bool moved_forward = false;
8820 if (/* IT->hpos == 0 means the very first glyph
8821 doesn't fit on the line, e.g. a wide image. */
8822 it->hpos == 0
8823 || (new_x == it->last_visible_x
8824 && FRAME_WINDOW_P (it->f)))
8826 ++it->hpos;
8827 it->current_x = new_x;
8829 /* The character's last glyph just barely fits
8830 in this row. */
8831 if (i == it->nglyphs - 1)
8833 /* If this is the destination position,
8834 return a position *before* it in this row,
8835 now that we know it fits in this row. */
8836 if (BUFFER_POS_REACHED_P ())
8838 bool can_wrap = true;
8840 /* If we are at a whitespace character
8841 that barely fits on this screen line,
8842 but the next character is also
8843 whitespace, we cannot wrap here. */
8844 if (it->line_wrap == WORD_WRAP
8845 && wrap_it.sp >= 0
8846 && may_wrap
8847 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8849 struct it tem_it;
8850 void *tem_data = NULL;
8852 SAVE_IT (tem_it, *it, tem_data);
8853 set_iterator_to_next (it, true);
8854 if (get_next_display_element (it)
8855 && IT_DISPLAYING_WHITESPACE (it))
8856 can_wrap = false;
8857 RESTORE_IT (it, &tem_it, tem_data);
8859 if (it->line_wrap != WORD_WRAP
8860 || wrap_it.sp < 0
8861 /* If we've just found whitespace
8862 where we can wrap, effectively
8863 ignore the previous wrap point --
8864 it is no longer relevant, but we
8865 won't have an opportunity to
8866 update it, since we've reached
8867 the edge of this screen line. */
8868 || (may_wrap && can_wrap
8869 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8871 it->hpos = hpos_before_this_char;
8872 it->current_x = x_before_this_char;
8873 result = MOVE_POS_MATCH_OR_ZV;
8874 break;
8876 if (it->line_wrap == WORD_WRAP
8877 && atpos_it.sp < 0)
8879 SAVE_IT (atpos_it, *it, atpos_data);
8880 atpos_it.current_x = x_before_this_char;
8881 atpos_it.hpos = hpos_before_this_char;
8885 prev_method = it->method;
8886 if (it->method == GET_FROM_BUFFER)
8887 prev_pos = IT_CHARPOS (*it);
8888 set_iterator_to_next (it, true);
8889 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8890 SET_TEXT_POS (this_line_min_pos,
8891 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8892 /* On graphical terminals, newlines may
8893 "overflow" into the fringe if
8894 overflow-newline-into-fringe is non-nil.
8895 On text terminals, and on graphical
8896 terminals with no right margin, newlines
8897 may overflow into the last glyph on the
8898 display line.*/
8899 if (!FRAME_WINDOW_P (it->f)
8900 || ((it->bidi_p
8901 && it->bidi_it.paragraph_dir == R2L)
8902 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8903 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8904 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8906 if (!get_next_display_element (it))
8908 result = MOVE_POS_MATCH_OR_ZV;
8909 break;
8911 moved_forward = true;
8912 if (BUFFER_POS_REACHED_P ())
8914 if (ITERATOR_AT_END_OF_LINE_P (it))
8915 result = MOVE_POS_MATCH_OR_ZV;
8916 else
8917 result = MOVE_LINE_CONTINUED;
8918 break;
8920 if (ITERATOR_AT_END_OF_LINE_P (it)
8921 && (it->line_wrap != WORD_WRAP
8922 || wrap_it.sp < 0
8923 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8925 result = MOVE_NEWLINE_OR_CR;
8926 break;
8931 else
8932 IT_RESET_X_ASCENT_DESCENT (it);
8934 /* If the screen line ends with whitespace, and we
8935 are under word-wrap, don't use wrap_it: it is no
8936 longer relevant, but we won't have an opportunity
8937 to update it, since we are done with this screen
8938 line. */
8939 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8940 /* If the character after the one which set the
8941 may_wrap flag is also whitespace, we can't
8942 wrap here, since the screen line cannot be
8943 wrapped in the middle of whitespace.
8944 Therefore, wrap_it _is_ relevant in that
8945 case. */
8946 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8948 /* If we've found TO_X, go back there, as we now
8949 know the last word fits on this screen line. */
8950 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8951 && atx_it.sp >= 0)
8953 RESTORE_IT (it, &atx_it, atx_data);
8954 atpos_it.sp = -1;
8955 atx_it.sp = -1;
8956 result = MOVE_X_REACHED;
8957 break;
8960 else if (wrap_it.sp >= 0)
8962 RESTORE_IT (it, &wrap_it, wrap_data);
8963 atpos_it.sp = -1;
8964 atx_it.sp = -1;
8967 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8968 IT_CHARPOS (*it)));
8969 result = MOVE_LINE_CONTINUED;
8970 break;
8973 if (BUFFER_POS_REACHED_P ())
8975 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8976 goto buffer_pos_reached;
8977 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8979 SAVE_IT (atpos_it, *it, atpos_data);
8980 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8984 if (new_x > it->first_visible_x)
8986 /* Glyph is visible. Increment number of glyphs that
8987 would be displayed. */
8988 ++it->hpos;
8992 if (result != MOVE_UNDEFINED)
8993 break;
8995 else if (BUFFER_POS_REACHED_P ())
8997 buffer_pos_reached:
8998 IT_RESET_X_ASCENT_DESCENT (it);
8999 result = MOVE_POS_MATCH_OR_ZV;
9000 break;
9002 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9004 /* Stop when TO_X specified and reached. This check is
9005 necessary here because of lines consisting of a line end,
9006 only. The line end will not produce any glyphs and we
9007 would never get MOVE_X_REACHED. */
9008 eassert (it->nglyphs == 0);
9009 result = MOVE_X_REACHED;
9010 break;
9013 /* Is this a line end? If yes, we're done. */
9014 if (ITERATOR_AT_END_OF_LINE_P (it))
9016 /* If we are past TO_CHARPOS, but never saw any character
9017 positions smaller than TO_CHARPOS, return
9018 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9019 did. */
9020 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9022 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9024 if (closest_pos < ZV)
9026 RESTORE_IT (it, &ppos_it, ppos_data);
9027 /* Don't recurse if closest_pos is equal to
9028 to_charpos, since we have just tried that. */
9029 if (closest_pos != to_charpos)
9030 move_it_in_display_line_to (it, closest_pos, -1,
9031 MOVE_TO_POS);
9032 result = MOVE_POS_MATCH_OR_ZV;
9034 else
9035 goto buffer_pos_reached;
9037 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9038 && IT_CHARPOS (*it) > to_charpos)
9039 goto buffer_pos_reached;
9040 else
9041 result = MOVE_NEWLINE_OR_CR;
9043 else
9044 result = MOVE_NEWLINE_OR_CR;
9045 /* If we've processed the newline, make sure this flag is
9046 reset, as it must only be set when the newline itself is
9047 processed. */
9048 if (result == MOVE_NEWLINE_OR_CR)
9049 it->constrain_row_ascent_descent_p = false;
9050 break;
9053 prev_method = it->method;
9054 if (it->method == GET_FROM_BUFFER)
9055 prev_pos = IT_CHARPOS (*it);
9056 /* The current display element has been consumed. Advance
9057 to the next. */
9058 set_iterator_to_next (it, true);
9059 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9060 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9061 if (IT_CHARPOS (*it) < to_charpos)
9062 saw_smaller_pos = true;
9063 if (it->bidi_p
9064 && (op & MOVE_TO_POS)
9065 && IT_CHARPOS (*it) >= to_charpos
9066 && IT_CHARPOS (*it) < closest_pos)
9067 closest_pos = IT_CHARPOS (*it);
9069 /* Stop if lines are truncated and IT's current x-position is
9070 past the right edge of the window now. */
9071 if (it->line_wrap == TRUNCATE
9072 && it->current_x >= it->last_visible_x)
9074 if (!FRAME_WINDOW_P (it->f)
9075 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9076 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9077 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9078 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9080 bool at_eob_p = false;
9082 if ((at_eob_p = !get_next_display_element (it))
9083 || BUFFER_POS_REACHED_P ()
9084 /* If we are past TO_CHARPOS, but never saw any
9085 character positions smaller than TO_CHARPOS,
9086 return MOVE_POS_MATCH_OR_ZV, like the
9087 unidirectional display did. */
9088 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9089 && !saw_smaller_pos
9090 && IT_CHARPOS (*it) > to_charpos))
9092 if (it->bidi_p
9093 && !BUFFER_POS_REACHED_P ()
9094 && !at_eob_p && closest_pos < ZV)
9096 RESTORE_IT (it, &ppos_it, ppos_data);
9097 if (closest_pos != to_charpos)
9098 move_it_in_display_line_to (it, closest_pos, -1,
9099 MOVE_TO_POS);
9101 result = MOVE_POS_MATCH_OR_ZV;
9102 break;
9104 if (ITERATOR_AT_END_OF_LINE_P (it))
9106 result = MOVE_NEWLINE_OR_CR;
9107 break;
9110 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9111 && !saw_smaller_pos
9112 && IT_CHARPOS (*it) > to_charpos)
9114 if (closest_pos < ZV)
9116 RESTORE_IT (it, &ppos_it, ppos_data);
9117 if (closest_pos != to_charpos)
9118 move_it_in_display_line_to (it, closest_pos, -1,
9119 MOVE_TO_POS);
9121 result = MOVE_POS_MATCH_OR_ZV;
9122 break;
9124 result = MOVE_LINE_TRUNCATED;
9125 break;
9127 #undef IT_RESET_X_ASCENT_DESCENT
9130 #undef BUFFER_POS_REACHED_P
9132 /* If we scanned beyond TO_POS, restore the saved iterator either to
9133 the wrap point (if found), or to atpos/atx location. We decide which
9134 data to use to restore the saved iterator state by their X coordinates,
9135 since buffer positions might increase non-monotonically with screen
9136 coordinates due to bidi reordering. */
9137 if (result == MOVE_LINE_CONTINUED
9138 && it->line_wrap == WORD_WRAP
9139 && wrap_it.sp >= 0
9140 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9141 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9142 RESTORE_IT (it, &wrap_it, wrap_data);
9143 else if (atpos_it.sp >= 0)
9144 RESTORE_IT (it, &atpos_it, atpos_data);
9145 else if (atx_it.sp >= 0)
9146 RESTORE_IT (it, &atx_it, atx_data);
9148 done:
9150 if (atpos_data)
9151 bidi_unshelve_cache (atpos_data, true);
9152 if (atx_data)
9153 bidi_unshelve_cache (atx_data, true);
9154 if (wrap_data)
9155 bidi_unshelve_cache (wrap_data, true);
9156 if (ppos_data)
9157 bidi_unshelve_cache (ppos_data, true);
9159 /* Restore the iterator settings altered at the beginning of this
9160 function. */
9161 it->glyph_row = saved_glyph_row;
9162 return result;
9165 /* For external use. */
9166 void
9167 move_it_in_display_line (struct it *it,
9168 ptrdiff_t to_charpos, int to_x,
9169 enum move_operation_enum op)
9171 if (it->line_wrap == WORD_WRAP
9172 && (op & MOVE_TO_X))
9174 struct it save_it;
9175 void *save_data = NULL;
9176 int skip;
9178 SAVE_IT (save_it, *it, save_data);
9179 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9180 /* When word-wrap is on, TO_X may lie past the end
9181 of a wrapped line. Then it->current is the
9182 character on the next line, so backtrack to the
9183 space before the wrap point. */
9184 if (skip == MOVE_LINE_CONTINUED)
9186 int prev_x = max (it->current_x - 1, 0);
9187 RESTORE_IT (it, &save_it, save_data);
9188 move_it_in_display_line_to
9189 (it, -1, prev_x, MOVE_TO_X);
9191 else
9192 bidi_unshelve_cache (save_data, true);
9194 else
9195 move_it_in_display_line_to (it, to_charpos, to_x, op);
9199 /* Move IT forward until it satisfies one or more of the criteria in
9200 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9202 OP is a bit-mask that specifies where to stop, and in particular,
9203 which of those four position arguments makes a difference. See the
9204 description of enum move_operation_enum.
9206 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9207 screen line, this function will set IT to the next position that is
9208 displayed to the right of TO_CHARPOS on the screen.
9210 Return the maximum pixel length of any line scanned but never more
9211 than it.last_visible_x. */
9214 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9216 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9217 int line_height, line_start_x = 0, reached = 0;
9218 int max_current_x = 0;
9219 void *backup_data = NULL;
9221 for (;;)
9223 if (op & MOVE_TO_VPOS)
9225 /* If no TO_CHARPOS and no TO_X specified, stop at the
9226 start of the line TO_VPOS. */
9227 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9229 if (it->vpos == to_vpos)
9231 reached = 1;
9232 break;
9234 else
9235 skip = move_it_in_display_line_to (it, -1, -1, 0);
9237 else
9239 /* TO_VPOS >= 0 means stop at TO_X in the line at
9240 TO_VPOS, or at TO_POS, whichever comes first. */
9241 if (it->vpos == to_vpos)
9243 reached = 2;
9244 break;
9247 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9249 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9251 reached = 3;
9252 break;
9254 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9256 /* We have reached TO_X but not in the line we want. */
9257 skip = move_it_in_display_line_to (it, to_charpos,
9258 -1, MOVE_TO_POS);
9259 if (skip == MOVE_POS_MATCH_OR_ZV)
9261 reached = 4;
9262 break;
9267 else if (op & MOVE_TO_Y)
9269 struct it it_backup;
9271 if (it->line_wrap == WORD_WRAP)
9272 SAVE_IT (it_backup, *it, backup_data);
9274 /* TO_Y specified means stop at TO_X in the line containing
9275 TO_Y---or at TO_CHARPOS if this is reached first. The
9276 problem is that we can't really tell whether the line
9277 contains TO_Y before we have completely scanned it, and
9278 this may skip past TO_X. What we do is to first scan to
9279 TO_X.
9281 If TO_X is not specified, use a TO_X of zero. The reason
9282 is to make the outcome of this function more predictable.
9283 If we didn't use TO_X == 0, we would stop at the end of
9284 the line which is probably not what a caller would expect
9285 to happen. */
9286 skip = move_it_in_display_line_to
9287 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9288 (MOVE_TO_X | (op & MOVE_TO_POS)));
9290 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9291 if (skip == MOVE_POS_MATCH_OR_ZV)
9292 reached = 5;
9293 else if (skip == MOVE_X_REACHED)
9295 /* If TO_X was reached, we want to know whether TO_Y is
9296 in the line. We know this is the case if the already
9297 scanned glyphs make the line tall enough. Otherwise,
9298 we must check by scanning the rest of the line. */
9299 line_height = it->max_ascent + it->max_descent;
9300 if (to_y >= it->current_y
9301 && to_y < it->current_y + line_height)
9303 reached = 6;
9304 break;
9306 SAVE_IT (it_backup, *it, backup_data);
9307 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9308 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9309 op & MOVE_TO_POS);
9310 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9311 line_height = it->max_ascent + it->max_descent;
9312 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9314 if (to_y >= it->current_y
9315 && to_y < it->current_y + line_height)
9317 /* If TO_Y is in this line and TO_X was reached
9318 above, we scanned too far. We have to restore
9319 IT's settings to the ones before skipping. But
9320 keep the more accurate values of max_ascent and
9321 max_descent we've found while skipping the rest
9322 of the line, for the sake of callers, such as
9323 pos_visible_p, that need to know the line
9324 height. */
9325 int max_ascent = it->max_ascent;
9326 int max_descent = it->max_descent;
9328 RESTORE_IT (it, &it_backup, backup_data);
9329 it->max_ascent = max_ascent;
9330 it->max_descent = max_descent;
9331 reached = 6;
9333 else
9335 skip = skip2;
9336 if (skip == MOVE_POS_MATCH_OR_ZV)
9337 reached = 7;
9340 else
9342 /* Check whether TO_Y is in this line. */
9343 line_height = it->max_ascent + it->max_descent;
9344 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9346 if (to_y >= it->current_y
9347 && to_y < it->current_y + line_height)
9349 if (to_y > it->current_y)
9350 max_current_x = max (it->current_x, max_current_x);
9352 /* When word-wrap is on, TO_X may lie past the end
9353 of a wrapped line. Then it->current is the
9354 character on the next line, so backtrack to the
9355 space before the wrap point. */
9356 if (skip == MOVE_LINE_CONTINUED
9357 && it->line_wrap == WORD_WRAP)
9359 int prev_x = max (it->current_x - 1, 0);
9360 RESTORE_IT (it, &it_backup, backup_data);
9361 skip = move_it_in_display_line_to
9362 (it, -1, prev_x, MOVE_TO_X);
9365 reached = 6;
9369 if (reached)
9371 max_current_x = max (it->current_x, max_current_x);
9372 break;
9375 else if (BUFFERP (it->object)
9376 && (it->method == GET_FROM_BUFFER
9377 || it->method == GET_FROM_STRETCH)
9378 && IT_CHARPOS (*it) >= to_charpos
9379 /* Under bidi iteration, a call to set_iterator_to_next
9380 can scan far beyond to_charpos if the initial
9381 portion of the next line needs to be reordered. In
9382 that case, give move_it_in_display_line_to another
9383 chance below. */
9384 && !(it->bidi_p
9385 && it->bidi_it.scan_dir == -1))
9386 skip = MOVE_POS_MATCH_OR_ZV;
9387 else
9388 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9390 switch (skip)
9392 case MOVE_POS_MATCH_OR_ZV:
9393 max_current_x = max (it->current_x, max_current_x);
9394 reached = 8;
9395 goto out;
9397 case MOVE_NEWLINE_OR_CR:
9398 max_current_x = max (it->current_x, max_current_x);
9399 set_iterator_to_next (it, true);
9400 it->continuation_lines_width = 0;
9401 break;
9403 case MOVE_LINE_TRUNCATED:
9404 max_current_x = it->last_visible_x;
9405 it->continuation_lines_width = 0;
9406 reseat_at_next_visible_line_start (it, false);
9407 if ((op & MOVE_TO_POS) != 0
9408 && IT_CHARPOS (*it) > to_charpos)
9410 reached = 9;
9411 goto out;
9413 break;
9415 case MOVE_LINE_CONTINUED:
9416 max_current_x = it->last_visible_x;
9417 /* For continued lines ending in a tab, some of the glyphs
9418 associated with the tab are displayed on the current
9419 line. Since it->current_x does not include these glyphs,
9420 we use it->last_visible_x instead. */
9421 if (it->c == '\t')
9423 it->continuation_lines_width += it->last_visible_x;
9424 /* When moving by vpos, ensure that the iterator really
9425 advances to the next line (bug#847, bug#969). Fixme:
9426 do we need to do this in other circumstances? */
9427 if (it->current_x != it->last_visible_x
9428 && (op & MOVE_TO_VPOS)
9429 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9431 line_start_x = it->current_x + it->pixel_width
9432 - it->last_visible_x;
9433 if (FRAME_WINDOW_P (it->f))
9435 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9436 struct font *face_font = face->font;
9438 /* When display_line produces a continued line
9439 that ends in a TAB, it skips a tab stop that
9440 is closer than the font's space character
9441 width (see x_produce_glyphs where it produces
9442 the stretch glyph which represents a TAB).
9443 We need to reproduce the same logic here. */
9444 eassert (face_font);
9445 if (face_font)
9447 if (line_start_x < face_font->space_width)
9448 line_start_x
9449 += it->tab_width * face_font->space_width;
9452 set_iterator_to_next (it, false);
9455 else
9456 it->continuation_lines_width += it->current_x;
9457 break;
9459 default:
9460 emacs_abort ();
9463 /* Reset/increment for the next run. */
9464 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9465 it->current_x = line_start_x;
9466 line_start_x = 0;
9467 it->hpos = 0;
9468 it->current_y += it->max_ascent + it->max_descent;
9469 ++it->vpos;
9470 last_height = it->max_ascent + it->max_descent;
9471 it->max_ascent = it->max_descent = 0;
9474 out:
9476 /* On text terminals, we may stop at the end of a line in the middle
9477 of a multi-character glyph. If the glyph itself is continued,
9478 i.e. it is actually displayed on the next line, don't treat this
9479 stopping point as valid; move to the next line instead (unless
9480 that brings us offscreen). */
9481 if (!FRAME_WINDOW_P (it->f)
9482 && op & MOVE_TO_POS
9483 && IT_CHARPOS (*it) == to_charpos
9484 && it->what == IT_CHARACTER
9485 && it->nglyphs > 1
9486 && it->line_wrap == WINDOW_WRAP
9487 && it->current_x == it->last_visible_x - 1
9488 && it->c != '\n'
9489 && it->c != '\t'
9490 && it->w->window_end_valid
9491 && it->vpos < it->w->window_end_vpos)
9493 it->continuation_lines_width += it->current_x;
9494 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9495 it->current_y += it->max_ascent + it->max_descent;
9496 ++it->vpos;
9497 last_height = it->max_ascent + it->max_descent;
9500 if (backup_data)
9501 bidi_unshelve_cache (backup_data, true);
9503 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9505 return max_current_x;
9509 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9511 If DY > 0, move IT backward at least that many pixels. DY = 0
9512 means move IT backward to the preceding line start or BEGV. This
9513 function may move over more than DY pixels if IT->current_y - DY
9514 ends up in the middle of a line; in this case IT->current_y will be
9515 set to the top of the line moved to. */
9517 void
9518 move_it_vertically_backward (struct it *it, int dy)
9520 int nlines, h;
9521 struct it it2, it3;
9522 void *it2data = NULL, *it3data = NULL;
9523 ptrdiff_t start_pos;
9524 int nchars_per_row
9525 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9526 ptrdiff_t pos_limit;
9528 move_further_back:
9529 eassert (dy >= 0);
9531 start_pos = IT_CHARPOS (*it);
9533 /* Estimate how many newlines we must move back. */
9534 nlines = max (1, dy / default_line_pixel_height (it->w));
9535 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9536 pos_limit = BEGV;
9537 else
9538 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9540 /* Set the iterator's position that many lines back. But don't go
9541 back more than NLINES full screen lines -- this wins a day with
9542 buffers which have very long lines. */
9543 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9544 back_to_previous_visible_line_start (it);
9546 /* Reseat the iterator here. When moving backward, we don't want
9547 reseat to skip forward over invisible text, set up the iterator
9548 to deliver from overlay strings at the new position etc. So,
9549 use reseat_1 here. */
9550 reseat_1 (it, it->current.pos, true);
9552 /* We are now surely at a line start. */
9553 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9554 reordering is in effect. */
9555 it->continuation_lines_width = 0;
9557 /* Move forward and see what y-distance we moved. First move to the
9558 start of the next line so that we get its height. We need this
9559 height to be able to tell whether we reached the specified
9560 y-distance. */
9561 SAVE_IT (it2, *it, it2data);
9562 it2.max_ascent = it2.max_descent = 0;
9565 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9566 MOVE_TO_POS | MOVE_TO_VPOS);
9568 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9569 /* If we are in a display string which starts at START_POS,
9570 and that display string includes a newline, and we are
9571 right after that newline (i.e. at the beginning of a
9572 display line), exit the loop, because otherwise we will
9573 infloop, since move_it_to will see that it is already at
9574 START_POS and will not move. */
9575 || (it2.method == GET_FROM_STRING
9576 && IT_CHARPOS (it2) == start_pos
9577 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9578 eassert (IT_CHARPOS (*it) >= BEGV);
9579 SAVE_IT (it3, it2, it3data);
9581 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9582 eassert (IT_CHARPOS (*it) >= BEGV);
9583 /* H is the actual vertical distance from the position in *IT
9584 and the starting position. */
9585 h = it2.current_y - it->current_y;
9586 /* NLINES is the distance in number of lines. */
9587 nlines = it2.vpos - it->vpos;
9589 /* Correct IT's y and vpos position
9590 so that they are relative to the starting point. */
9591 it->vpos -= nlines;
9592 it->current_y -= h;
9594 if (dy == 0)
9596 /* DY == 0 means move to the start of the screen line. The
9597 value of nlines is > 0 if continuation lines were involved,
9598 or if the original IT position was at start of a line. */
9599 RESTORE_IT (it, it, it2data);
9600 if (nlines > 0)
9601 move_it_by_lines (it, nlines);
9602 /* The above code moves us to some position NLINES down,
9603 usually to its first glyph (leftmost in an L2R line), but
9604 that's not necessarily the start of the line, under bidi
9605 reordering. We want to get to the character position
9606 that is immediately after the newline of the previous
9607 line. */
9608 if (it->bidi_p
9609 && !it->continuation_lines_width
9610 && !STRINGP (it->string)
9611 && IT_CHARPOS (*it) > BEGV
9612 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9614 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9616 DEC_BOTH (cp, bp);
9617 cp = find_newline_no_quit (cp, bp, -1, NULL);
9618 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9620 bidi_unshelve_cache (it3data, true);
9622 else
9624 /* The y-position we try to reach, relative to *IT.
9625 Note that H has been subtracted in front of the if-statement. */
9626 int target_y = it->current_y + h - dy;
9627 int y0 = it3.current_y;
9628 int y1;
9629 int line_height;
9631 RESTORE_IT (&it3, &it3, it3data);
9632 y1 = line_bottom_y (&it3);
9633 line_height = y1 - y0;
9634 RESTORE_IT (it, it, it2data);
9635 /* If we did not reach target_y, try to move further backward if
9636 we can. If we moved too far backward, try to move forward. */
9637 if (target_y < it->current_y
9638 /* This is heuristic. In a window that's 3 lines high, with
9639 a line height of 13 pixels each, recentering with point
9640 on the bottom line will try to move -39/2 = 19 pixels
9641 backward. Try to avoid moving into the first line. */
9642 && (it->current_y - target_y
9643 > min (window_box_height (it->w), line_height * 2 / 3))
9644 && IT_CHARPOS (*it) > BEGV)
9646 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9647 target_y - it->current_y));
9648 dy = it->current_y - target_y;
9649 goto move_further_back;
9651 else if (target_y >= it->current_y + line_height
9652 && IT_CHARPOS (*it) < ZV)
9654 /* Should move forward by at least one line, maybe more.
9656 Note: Calling move_it_by_lines can be expensive on
9657 terminal frames, where compute_motion is used (via
9658 vmotion) to do the job, when there are very long lines
9659 and truncate-lines is nil. That's the reason for
9660 treating terminal frames specially here. */
9662 if (!FRAME_WINDOW_P (it->f))
9663 move_it_vertically (it, target_y - it->current_y);
9664 else
9668 move_it_by_lines (it, 1);
9670 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9677 /* Move IT by a specified amount of pixel lines DY. DY negative means
9678 move backwards. DY = 0 means move to start of screen line. At the
9679 end, IT will be on the start of a screen line. */
9681 void
9682 move_it_vertically (struct it *it, int dy)
9684 if (dy <= 0)
9685 move_it_vertically_backward (it, -dy);
9686 else
9688 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9689 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9690 MOVE_TO_POS | MOVE_TO_Y);
9691 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9693 /* If buffer ends in ZV without a newline, move to the start of
9694 the line to satisfy the post-condition. */
9695 if (IT_CHARPOS (*it) == ZV
9696 && ZV > BEGV
9697 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9698 move_it_by_lines (it, 0);
9703 /* Move iterator IT past the end of the text line it is in. */
9705 void
9706 move_it_past_eol (struct it *it)
9708 enum move_it_result rc;
9710 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9711 if (rc == MOVE_NEWLINE_OR_CR)
9712 set_iterator_to_next (it, false);
9716 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9717 negative means move up. DVPOS == 0 means move to the start of the
9718 screen line.
9720 Optimization idea: If we would know that IT->f doesn't use
9721 a face with proportional font, we could be faster for
9722 truncate-lines nil. */
9724 void
9725 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9728 /* The commented-out optimization uses vmotion on terminals. This
9729 gives bad results, because elements like it->what, on which
9730 callers such as pos_visible_p rely, aren't updated. */
9731 /* struct position pos;
9732 if (!FRAME_WINDOW_P (it->f))
9734 struct text_pos textpos;
9736 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9737 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9738 reseat (it, textpos, true);
9739 it->vpos += pos.vpos;
9740 it->current_y += pos.vpos;
9742 else */
9744 if (dvpos == 0)
9746 /* DVPOS == 0 means move to the start of the screen line. */
9747 move_it_vertically_backward (it, 0);
9748 /* Let next call to line_bottom_y calculate real line height. */
9749 last_height = 0;
9751 else if (dvpos > 0)
9753 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9754 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9756 /* Only move to the next buffer position if we ended up in a
9757 string from display property, not in an overlay string
9758 (before-string or after-string). That is because the
9759 latter don't conceal the underlying buffer position, so
9760 we can ask to move the iterator to the exact position we
9761 are interested in. Note that, even if we are already at
9762 IT_CHARPOS (*it), the call below is not a no-op, as it
9763 will detect that we are at the end of the string, pop the
9764 iterator, and compute it->current_x and it->hpos
9765 correctly. */
9766 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9767 -1, -1, -1, MOVE_TO_POS);
9770 else
9772 struct it it2;
9773 void *it2data = NULL;
9774 ptrdiff_t start_charpos, i;
9775 int nchars_per_row
9776 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9777 bool hit_pos_limit = false;
9778 ptrdiff_t pos_limit;
9780 /* Start at the beginning of the screen line containing IT's
9781 position. This may actually move vertically backwards,
9782 in case of overlays, so adjust dvpos accordingly. */
9783 dvpos += it->vpos;
9784 move_it_vertically_backward (it, 0);
9785 dvpos -= it->vpos;
9787 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9788 screen lines, and reseat the iterator there. */
9789 start_charpos = IT_CHARPOS (*it);
9790 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9791 pos_limit = BEGV;
9792 else
9793 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9795 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9796 back_to_previous_visible_line_start (it);
9797 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9798 hit_pos_limit = true;
9799 reseat (it, it->current.pos, true);
9801 /* Move further back if we end up in a string or an image. */
9802 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9804 /* First try to move to start of display line. */
9805 dvpos += it->vpos;
9806 move_it_vertically_backward (it, 0);
9807 dvpos -= it->vpos;
9808 if (IT_POS_VALID_AFTER_MOVE_P (it))
9809 break;
9810 /* If start of line is still in string or image,
9811 move further back. */
9812 back_to_previous_visible_line_start (it);
9813 reseat (it, it->current.pos, true);
9814 dvpos--;
9817 it->current_x = it->hpos = 0;
9819 /* Above call may have moved too far if continuation lines
9820 are involved. Scan forward and see if it did. */
9821 SAVE_IT (it2, *it, it2data);
9822 it2.vpos = it2.current_y = 0;
9823 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9824 it->vpos -= it2.vpos;
9825 it->current_y -= it2.current_y;
9826 it->current_x = it->hpos = 0;
9828 /* If we moved too far back, move IT some lines forward. */
9829 if (it2.vpos > -dvpos)
9831 int delta = it2.vpos + dvpos;
9833 RESTORE_IT (&it2, &it2, it2data);
9834 SAVE_IT (it2, *it, it2data);
9835 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9836 /* Move back again if we got too far ahead. */
9837 if (IT_CHARPOS (*it) >= start_charpos)
9838 RESTORE_IT (it, &it2, it2data);
9839 else
9840 bidi_unshelve_cache (it2data, true);
9842 else if (hit_pos_limit && pos_limit > BEGV
9843 && dvpos < 0 && it2.vpos < -dvpos)
9845 /* If we hit the limit, but still didn't make it far enough
9846 back, that means there's a display string with a newline
9847 covering a large chunk of text, and that caused
9848 back_to_previous_visible_line_start try to go too far.
9849 Punish those who commit such atrocities by going back
9850 until we've reached DVPOS, after lifting the limit, which
9851 could make it slow for very long lines. "If it hurts,
9852 don't do that!" */
9853 dvpos += it2.vpos;
9854 RESTORE_IT (it, it, it2data);
9855 for (i = -dvpos; i > 0; --i)
9857 back_to_previous_visible_line_start (it);
9858 it->vpos--;
9860 reseat_1 (it, it->current.pos, true);
9862 else
9863 RESTORE_IT (it, it, it2data);
9867 /* Return true if IT points into the middle of a display vector. */
9869 bool
9870 in_display_vector_p (struct it *it)
9872 return (it->method == GET_FROM_DISPLAY_VECTOR
9873 && it->current.dpvec_index > 0
9874 && it->dpvec + it->current.dpvec_index != it->dpend);
9877 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9878 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9879 WINDOW must be a live window and defaults to the selected one. The
9880 return value is a cons of the maximum pixel-width of any text line and
9881 the maximum pixel-height of all text lines.
9883 The optional argument FROM, if non-nil, specifies the first text
9884 position and defaults to the minimum accessible position of the buffer.
9885 If FROM is t, use the minimum accessible position that starts a
9886 non-empty line. TO, if non-nil, specifies the last text position and
9887 defaults to the maximum accessible position of the buffer. If TO is t,
9888 use the maximum accessible position that ends a non-empty line.
9890 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9891 width that can be returned. X-LIMIT nil or omitted, means to use the
9892 pixel-width of WINDOW's body; use this if you want to know how high
9893 WINDOW should be become in order to fit all of its buffer's text with
9894 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9895 if you intend to change WINDOW's width. In any case, text whose
9896 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9897 of long lines can take some time, it's always a good idea to make this
9898 argument as small as possible; in particular, if the buffer contains
9899 long lines that shall be truncated anyway.
9901 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9902 height (excluding the height of the mode- or header-line, if any) that
9903 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9904 ignored. Since calculating the text height of a large buffer can take
9905 some time, it makes sense to specify this argument if the size of the
9906 buffer is large or unknown.
9908 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9909 include the height of the mode- or header-line of WINDOW in the return
9910 value. If it is either the symbol `mode-line' or `header-line', include
9911 only the height of that line, if present, in the return value. If t,
9912 include the height of both, if present, in the return value. */)
9913 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9914 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9916 struct window *w = decode_live_window (window);
9917 Lisp_Object buffer = w->contents;
9918 struct buffer *b;
9919 struct it it;
9920 struct buffer *old_b = NULL;
9921 ptrdiff_t start, end, pos;
9922 struct text_pos startp;
9923 void *itdata = NULL;
9924 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9926 CHECK_BUFFER (buffer);
9927 b = XBUFFER (buffer);
9929 if (b != current_buffer)
9931 old_b = current_buffer;
9932 set_buffer_internal (b);
9935 if (NILP (from))
9936 start = BEGV;
9937 else if (EQ (from, Qt))
9939 start = pos = BEGV;
9940 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9941 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9942 start = pos;
9943 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9944 start = pos;
9946 else
9948 CHECK_NUMBER_COERCE_MARKER (from);
9949 start = min (max (XINT (from), BEGV), ZV);
9952 if (NILP (to))
9953 end = ZV;
9954 else if (EQ (to, Qt))
9956 end = pos = ZV;
9957 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9958 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9959 end = pos;
9960 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9961 end = pos;
9963 else
9965 CHECK_NUMBER_COERCE_MARKER (to);
9966 end = max (start, min (XINT (to), ZV));
9969 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9970 max_x = XINT (x_limit);
9972 if (NILP (y_limit))
9973 max_y = INT_MAX;
9974 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9975 max_y = XINT (y_limit);
9977 itdata = bidi_shelve_cache ();
9978 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9979 start_display (&it, w, startp);
9981 if (NILP (x_limit))
9982 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9983 else
9985 it.last_visible_x = max_x;
9986 /* Actually, we never want move_it_to stop at to_x. But to make
9987 sure that move_it_in_display_line_to always moves far enough,
9988 we set it to INT_MAX and specify MOVE_TO_X. */
9989 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9990 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9991 /* Don't return more than X-LIMIT. */
9992 if (x > max_x)
9993 x = max_x;
9996 /* Subtract height of header-line which was counted automatically by
9997 start_display. */
9998 y = it.current_y + it.max_ascent + it.max_descent
9999 - WINDOW_HEADER_LINE_HEIGHT (w);
10000 /* Don't return more than Y-LIMIT. */
10001 if (y > max_y)
10002 y = max_y;
10004 if (EQ (mode_and_header_line, Qheader_line)
10005 || EQ (mode_and_header_line, Qt))
10006 /* Re-add height of header-line as requested. */
10007 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10009 if (EQ (mode_and_header_line, Qmode_line)
10010 || EQ (mode_and_header_line, Qt))
10011 /* Add height of mode-line as requested. */
10012 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10014 bidi_unshelve_cache (itdata, false);
10016 if (old_b)
10017 set_buffer_internal (old_b);
10019 return Fcons (make_number (x), make_number (y));
10022 /***********************************************************************
10023 Messages
10024 ***********************************************************************/
10026 /* Return the number of arguments the format string FORMAT needs. */
10028 static ptrdiff_t
10029 format_nargs (char const *format)
10031 ptrdiff_t nargs = 0;
10032 for (char const *p = format; (p = strchr (p, '%')); p++)
10033 if (p[1] == '%')
10034 p++;
10035 else
10036 nargs++;
10037 return nargs;
10040 /* Add a message with format string FORMAT and formatted arguments
10041 to *Messages*. */
10043 void
10044 add_to_log (const char *format, ...)
10046 va_list ap;
10047 va_start (ap, format);
10048 vadd_to_log (format, ap);
10049 va_end (ap);
10052 void
10053 vadd_to_log (char const *format, va_list ap)
10055 ptrdiff_t form_nargs = format_nargs (format);
10056 ptrdiff_t nargs = 1 + form_nargs;
10057 Lisp_Object args[10];
10058 eassert (nargs <= ARRAYELTS (args));
10059 AUTO_STRING (args0, format);
10060 args[0] = args0;
10061 for (ptrdiff_t i = 1; i <= nargs; i++)
10062 args[i] = va_arg (ap, Lisp_Object);
10063 Lisp_Object msg = Qnil;
10064 msg = Fformat_message (nargs, args);
10066 ptrdiff_t len = SBYTES (msg) + 1;
10067 USE_SAFE_ALLOCA;
10068 char *buffer = SAFE_ALLOCA (len);
10069 memcpy (buffer, SDATA (msg), len);
10071 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10072 SAFE_FREE ();
10076 /* Output a newline in the *Messages* buffer if "needs" one. */
10078 void
10079 message_log_maybe_newline (void)
10081 if (message_log_need_newline)
10082 message_dolog ("", 0, true, false);
10086 /* Add a string M of length NBYTES to the message log, optionally
10087 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10088 true, means interpret the contents of M as multibyte. This
10089 function calls low-level routines in order to bypass text property
10090 hooks, etc. which might not be safe to run.
10092 This may GC (insert may run before/after change hooks),
10093 so the buffer M must NOT point to a Lisp string. */
10095 void
10096 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10098 const unsigned char *msg = (const unsigned char *) m;
10100 if (!NILP (Vmemory_full))
10101 return;
10103 if (!NILP (Vmessage_log_max))
10105 struct buffer *oldbuf;
10106 Lisp_Object oldpoint, oldbegv, oldzv;
10107 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10108 ptrdiff_t point_at_end = 0;
10109 ptrdiff_t zv_at_end = 0;
10110 Lisp_Object old_deactivate_mark;
10112 old_deactivate_mark = Vdeactivate_mark;
10113 oldbuf = current_buffer;
10115 /* Ensure the Messages buffer exists, and switch to it.
10116 If we created it, set the major-mode. */
10117 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10118 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10119 if (newbuffer
10120 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10121 call0 (intern ("messages-buffer-mode"));
10123 bset_undo_list (current_buffer, Qt);
10124 bset_cache_long_scans (current_buffer, Qnil);
10126 oldpoint = message_dolog_marker1;
10127 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10128 oldbegv = message_dolog_marker2;
10129 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10130 oldzv = message_dolog_marker3;
10131 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10133 if (PT == Z)
10134 point_at_end = 1;
10135 if (ZV == Z)
10136 zv_at_end = 1;
10138 BEGV = BEG;
10139 BEGV_BYTE = BEG_BYTE;
10140 ZV = Z;
10141 ZV_BYTE = Z_BYTE;
10142 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10144 /* Insert the string--maybe converting multibyte to single byte
10145 or vice versa, so that all the text fits the buffer. */
10146 if (multibyte
10147 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10149 ptrdiff_t i;
10150 int c, char_bytes;
10151 char work[1];
10153 /* Convert a multibyte string to single-byte
10154 for the *Message* buffer. */
10155 for (i = 0; i < nbytes; i += char_bytes)
10157 c = string_char_and_length (msg + i, &char_bytes);
10158 work[0] = CHAR_TO_BYTE8 (c);
10159 insert_1_both (work, 1, 1, true, false, false);
10162 else if (! multibyte
10163 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10165 ptrdiff_t i;
10166 int c, char_bytes;
10167 unsigned char str[MAX_MULTIBYTE_LENGTH];
10168 /* Convert a single-byte string to multibyte
10169 for the *Message* buffer. */
10170 for (i = 0; i < nbytes; i++)
10172 c = msg[i];
10173 MAKE_CHAR_MULTIBYTE (c);
10174 char_bytes = CHAR_STRING (c, str);
10175 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10178 else if (nbytes)
10179 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10180 true, false, false);
10182 if (nlflag)
10184 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10185 printmax_t dups;
10187 insert_1_both ("\n", 1, 1, true, false, false);
10189 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10190 this_bol = PT;
10191 this_bol_byte = PT_BYTE;
10193 /* See if this line duplicates the previous one.
10194 If so, combine duplicates. */
10195 if (this_bol > BEG)
10197 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10198 prev_bol = PT;
10199 prev_bol_byte = PT_BYTE;
10201 dups = message_log_check_duplicate (prev_bol_byte,
10202 this_bol_byte);
10203 if (dups)
10205 del_range_both (prev_bol, prev_bol_byte,
10206 this_bol, this_bol_byte, false);
10207 if (dups > 1)
10209 char dupstr[sizeof " [ times]"
10210 + INT_STRLEN_BOUND (printmax_t)];
10212 /* If you change this format, don't forget to also
10213 change message_log_check_duplicate. */
10214 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10215 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10216 insert_1_both (dupstr, duplen, duplen,
10217 true, false, true);
10222 /* If we have more than the desired maximum number of lines
10223 in the *Messages* buffer now, delete the oldest ones.
10224 This is safe because we don't have undo in this buffer. */
10226 if (NATNUMP (Vmessage_log_max))
10228 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10229 -XFASTINT (Vmessage_log_max) - 1, false);
10230 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10233 BEGV = marker_position (oldbegv);
10234 BEGV_BYTE = marker_byte_position (oldbegv);
10236 if (zv_at_end)
10238 ZV = Z;
10239 ZV_BYTE = Z_BYTE;
10241 else
10243 ZV = marker_position (oldzv);
10244 ZV_BYTE = marker_byte_position (oldzv);
10247 if (point_at_end)
10248 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10249 else
10250 /* We can't do Fgoto_char (oldpoint) because it will run some
10251 Lisp code. */
10252 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10253 marker_byte_position (oldpoint));
10255 unchain_marker (XMARKER (oldpoint));
10256 unchain_marker (XMARKER (oldbegv));
10257 unchain_marker (XMARKER (oldzv));
10259 /* We called insert_1_both above with its 5th argument (PREPARE)
10260 false, which prevents insert_1_both from calling
10261 prepare_to_modify_buffer, which in turns prevents us from
10262 incrementing windows_or_buffers_changed even if *Messages* is
10263 shown in some window. So we must manually set
10264 windows_or_buffers_changed here to make up for that. */
10265 windows_or_buffers_changed = old_windows_or_buffers_changed;
10266 bset_redisplay (current_buffer);
10268 set_buffer_internal (oldbuf);
10270 message_log_need_newline = !nlflag;
10271 Vdeactivate_mark = old_deactivate_mark;
10276 /* We are at the end of the buffer after just having inserted a newline.
10277 (Note: We depend on the fact we won't be crossing the gap.)
10278 Check to see if the most recent message looks a lot like the previous one.
10279 Return 0 if different, 1 if the new one should just replace it, or a
10280 value N > 1 if we should also append " [N times]". */
10282 static intmax_t
10283 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10285 ptrdiff_t i;
10286 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10287 bool seen_dots = false;
10288 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10289 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10291 for (i = 0; i < len; i++)
10293 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10294 seen_dots = true;
10295 if (p1[i] != p2[i])
10296 return seen_dots;
10298 p1 += len;
10299 if (*p1 == '\n')
10300 return 2;
10301 if (*p1++ == ' ' && *p1++ == '[')
10303 char *pend;
10304 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10305 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10306 return n + 1;
10308 return 0;
10312 /* Display an echo area message M with a specified length of NBYTES
10313 bytes. The string may include null characters. If M is not a
10314 string, clear out any existing message, and let the mini-buffer
10315 text show through.
10317 This function cancels echoing. */
10319 void
10320 message3 (Lisp_Object m)
10322 clear_message (true, true);
10323 cancel_echoing ();
10325 /* First flush out any partial line written with print. */
10326 message_log_maybe_newline ();
10327 if (STRINGP (m))
10329 ptrdiff_t nbytes = SBYTES (m);
10330 bool multibyte = STRING_MULTIBYTE (m);
10331 char *buffer;
10332 USE_SAFE_ALLOCA;
10333 SAFE_ALLOCA_STRING (buffer, m);
10334 message_dolog (buffer, nbytes, true, multibyte);
10335 SAFE_FREE ();
10337 if (! inhibit_message)
10338 message3_nolog (m);
10341 /* Log the message M to stderr. Log an empty line if M is not a string. */
10343 static void
10344 message_to_stderr (Lisp_Object m)
10346 if (noninteractive_need_newline)
10348 noninteractive_need_newline = false;
10349 fputc ('\n', stderr);
10351 if (STRINGP (m))
10353 Lisp_Object coding_system = Vlocale_coding_system;
10354 Lisp_Object s;
10356 if (!NILP (Vcoding_system_for_write))
10357 coding_system = Vcoding_system_for_write;
10358 if (!NILP (coding_system))
10359 s = code_convert_string_norecord (m, coding_system, true);
10360 else
10361 s = m;
10363 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10365 if (!cursor_in_echo_area)
10366 fputc ('\n', stderr);
10367 fflush (stderr);
10370 /* The non-logging version of message3.
10371 This does not cancel echoing, because it is used for echoing.
10372 Perhaps we need to make a separate function for echoing
10373 and make this cancel echoing. */
10375 void
10376 message3_nolog (Lisp_Object m)
10378 struct frame *sf = SELECTED_FRAME ();
10380 if (FRAME_INITIAL_P (sf))
10381 message_to_stderr (m);
10382 /* Error messages get reported properly by cmd_error, so this must be just an
10383 informative message; if the frame hasn't really been initialized yet, just
10384 toss it. */
10385 else if (INTERACTIVE && sf->glyphs_initialized_p)
10387 /* Get the frame containing the mini-buffer
10388 that the selected frame is using. */
10389 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10390 Lisp_Object frame = XWINDOW (mini_window)->frame;
10391 struct frame *f = XFRAME (frame);
10393 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10394 Fmake_frame_visible (frame);
10396 if (STRINGP (m) && SCHARS (m) > 0)
10398 set_message (m);
10399 if (minibuffer_auto_raise)
10400 Fraise_frame (frame);
10401 /* Assume we are not echoing.
10402 (If we are, echo_now will override this.) */
10403 echo_message_buffer = Qnil;
10405 else
10406 clear_message (true, true);
10408 do_pending_window_change (false);
10409 echo_area_display (true);
10410 do_pending_window_change (false);
10411 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10412 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10417 /* Display a null-terminated echo area message M. If M is 0, clear
10418 out any existing message, and let the mini-buffer text show through.
10420 The buffer M must continue to exist until after the echo area gets
10421 cleared or some other message gets displayed there. Do not pass
10422 text that is stored in a Lisp string. Do not pass text in a buffer
10423 that was alloca'd. */
10425 void
10426 message1 (const char *m)
10428 message3 (m ? build_unibyte_string (m) : Qnil);
10432 /* The non-logging counterpart of message1. */
10434 void
10435 message1_nolog (const char *m)
10437 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10440 /* Display a message M which contains a single %s
10441 which gets replaced with STRING. */
10443 void
10444 message_with_string (const char *m, Lisp_Object string, bool log)
10446 CHECK_STRING (string);
10448 bool need_message;
10449 if (noninteractive)
10450 need_message = !!m;
10451 else if (!INTERACTIVE)
10452 need_message = false;
10453 else
10455 /* The frame whose minibuffer we're going to display the message on.
10456 It may be larger than the selected frame, so we need
10457 to use its buffer, not the selected frame's buffer. */
10458 Lisp_Object mini_window;
10459 struct frame *f, *sf = SELECTED_FRAME ();
10461 /* Get the frame containing the minibuffer
10462 that the selected frame is using. */
10463 mini_window = FRAME_MINIBUF_WINDOW (sf);
10464 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10466 /* Error messages get reported properly by cmd_error, so this must be
10467 just an informative message; if the frame hasn't really been
10468 initialized yet, just toss it. */
10469 need_message = f->glyphs_initialized_p;
10472 if (need_message)
10474 AUTO_STRING (fmt, m);
10475 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10477 if (noninteractive)
10478 message_to_stderr (msg);
10479 else
10481 if (log)
10482 message3 (msg);
10483 else
10484 message3_nolog (msg);
10486 /* Print should start at the beginning of the message
10487 buffer next time. */
10488 message_buf_print = false;
10494 /* Dump an informative message to the minibuf. If M is 0, clear out
10495 any existing message, and let the mini-buffer text show through.
10497 The message must be safe ASCII and the format must not contain ` or
10498 '. If your message and format do not fit into this category,
10499 convert your arguments to Lisp objects and use Fmessage instead. */
10501 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10502 vmessage (const char *m, va_list ap)
10504 if (noninteractive)
10506 if (m)
10508 if (noninteractive_need_newline)
10509 putc ('\n', stderr);
10510 noninteractive_need_newline = false;
10511 vfprintf (stderr, m, ap);
10512 if (!cursor_in_echo_area)
10513 fprintf (stderr, "\n");
10514 fflush (stderr);
10517 else if (INTERACTIVE)
10519 /* The frame whose mini-buffer we're going to display the message
10520 on. It may be larger than the selected frame, so we need to
10521 use its buffer, not the selected frame's buffer. */
10522 Lisp_Object mini_window;
10523 struct frame *f, *sf = SELECTED_FRAME ();
10525 /* Get the frame containing the mini-buffer
10526 that the selected frame is using. */
10527 mini_window = FRAME_MINIBUF_WINDOW (sf);
10528 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10530 /* Error messages get reported properly by cmd_error, so this must be
10531 just an informative message; if the frame hasn't really been
10532 initialized yet, just toss it. */
10533 if (f->glyphs_initialized_p)
10535 if (m)
10537 ptrdiff_t len;
10538 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10539 USE_SAFE_ALLOCA;
10540 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10542 len = doprnt (message_buf, maxsize, m, 0, ap);
10544 message3 (make_string (message_buf, len));
10545 SAFE_FREE ();
10547 else
10548 message1 (0);
10550 /* Print should start at the beginning of the message
10551 buffer next time. */
10552 message_buf_print = false;
10557 void
10558 message (const char *m, ...)
10560 va_list ap;
10561 va_start (ap, m);
10562 vmessage (m, ap);
10563 va_end (ap);
10567 /* Display the current message in the current mini-buffer. This is
10568 only called from error handlers in process.c, and is not time
10569 critical. */
10571 void
10572 update_echo_area (void)
10574 if (!NILP (echo_area_buffer[0]))
10576 Lisp_Object string;
10577 string = Fcurrent_message ();
10578 message3 (string);
10583 /* Make sure echo area buffers in `echo_buffers' are live.
10584 If they aren't, make new ones. */
10586 static void
10587 ensure_echo_area_buffers (void)
10589 for (int i = 0; i < 2; i++)
10590 if (!BUFFERP (echo_buffer[i])
10591 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10593 Lisp_Object old_buffer = echo_buffer[i];
10594 static char const name_fmt[] = " *Echo Area %d*";
10595 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10596 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10597 echo_buffer[i] = Fget_buffer_create (lname);
10598 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10599 /* to force word wrap in echo area -
10600 it was decided to postpone this*/
10601 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10603 for (int j = 0; j < 2; j++)
10604 if (EQ (old_buffer, echo_area_buffer[j]))
10605 echo_area_buffer[j] = echo_buffer[i];
10610 /* Call FN with args A1..A2 with either the current or last displayed
10611 echo_area_buffer as current buffer.
10613 WHICH zero means use the current message buffer
10614 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10615 from echo_buffer[] and clear it.
10617 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10618 suitable buffer from echo_buffer[] and clear it.
10620 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10621 that the current message becomes the last displayed one, choose a
10622 suitable buffer for echo_area_buffer[0], and clear it.
10624 Value is what FN returns. */
10626 static bool
10627 with_echo_area_buffer (struct window *w, int which,
10628 bool (*fn) (ptrdiff_t, Lisp_Object),
10629 ptrdiff_t a1, Lisp_Object a2)
10631 Lisp_Object buffer;
10632 bool this_one, the_other, clear_buffer_p, rc;
10633 ptrdiff_t count = SPECPDL_INDEX ();
10635 /* If buffers aren't live, make new ones. */
10636 ensure_echo_area_buffers ();
10638 clear_buffer_p = false;
10640 if (which == 0)
10641 this_one = false, the_other = true;
10642 else if (which > 0)
10643 this_one = true, the_other = false;
10644 else
10646 this_one = false, the_other = true;
10647 clear_buffer_p = true;
10649 /* We need a fresh one in case the current echo buffer equals
10650 the one containing the last displayed echo area message. */
10651 if (!NILP (echo_area_buffer[this_one])
10652 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10653 echo_area_buffer[this_one] = Qnil;
10656 /* Choose a suitable buffer from echo_buffer[] if we don't
10657 have one. */
10658 if (NILP (echo_area_buffer[this_one]))
10660 echo_area_buffer[this_one]
10661 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10662 ? echo_buffer[the_other]
10663 : echo_buffer[this_one]);
10664 clear_buffer_p = true;
10667 buffer = echo_area_buffer[this_one];
10669 /* Don't get confused by reusing the buffer used for echoing
10670 for a different purpose. */
10671 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10672 cancel_echoing ();
10674 record_unwind_protect (unwind_with_echo_area_buffer,
10675 with_echo_area_buffer_unwind_data (w));
10677 /* Make the echo area buffer current. Note that for display
10678 purposes, it is not necessary that the displayed window's buffer
10679 == current_buffer, except for text property lookup. So, let's
10680 only set that buffer temporarily here without doing a full
10681 Fset_window_buffer. We must also change w->pointm, though,
10682 because otherwise an assertions in unshow_buffer fails, and Emacs
10683 aborts. */
10684 set_buffer_internal_1 (XBUFFER (buffer));
10685 if (w)
10687 wset_buffer (w, buffer);
10688 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10689 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10692 bset_undo_list (current_buffer, Qt);
10693 bset_read_only (current_buffer, Qnil);
10694 specbind (Qinhibit_read_only, Qt);
10695 specbind (Qinhibit_modification_hooks, Qt);
10697 if (clear_buffer_p && Z > BEG)
10698 del_range (BEG, Z);
10700 eassert (BEGV >= BEG);
10701 eassert (ZV <= Z && ZV >= BEGV);
10703 rc = fn (a1, a2);
10705 eassert (BEGV >= BEG);
10706 eassert (ZV <= Z && ZV >= BEGV);
10708 unbind_to (count, Qnil);
10709 return rc;
10713 /* Save state that should be preserved around the call to the function
10714 FN called in with_echo_area_buffer. */
10716 static Lisp_Object
10717 with_echo_area_buffer_unwind_data (struct window *w)
10719 int i = 0;
10720 Lisp_Object vector, tmp;
10722 /* Reduce consing by keeping one vector in
10723 Vwith_echo_area_save_vector. */
10724 vector = Vwith_echo_area_save_vector;
10725 Vwith_echo_area_save_vector = Qnil;
10727 if (NILP (vector))
10728 vector = Fmake_vector (make_number (11), Qnil);
10730 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10731 ASET (vector, i, Vdeactivate_mark); ++i;
10732 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10734 if (w)
10736 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10737 ASET (vector, i, w->contents); ++i;
10738 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10739 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10740 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10741 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10742 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10743 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10745 else
10747 int end = i + 8;
10748 for (; i < end; ++i)
10749 ASET (vector, i, Qnil);
10752 eassert (i == ASIZE (vector));
10753 return vector;
10757 /* Restore global state from VECTOR which was created by
10758 with_echo_area_buffer_unwind_data. */
10760 static void
10761 unwind_with_echo_area_buffer (Lisp_Object vector)
10763 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10764 Vdeactivate_mark = AREF (vector, 1);
10765 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10767 if (WINDOWP (AREF (vector, 3)))
10769 struct window *w;
10770 Lisp_Object buffer;
10772 w = XWINDOW (AREF (vector, 3));
10773 buffer = AREF (vector, 4);
10775 wset_buffer (w, buffer);
10776 set_marker_both (w->pointm, buffer,
10777 XFASTINT (AREF (vector, 5)),
10778 XFASTINT (AREF (vector, 6)));
10779 set_marker_both (w->old_pointm, buffer,
10780 XFASTINT (AREF (vector, 7)),
10781 XFASTINT (AREF (vector, 8)));
10782 set_marker_both (w->start, buffer,
10783 XFASTINT (AREF (vector, 9)),
10784 XFASTINT (AREF (vector, 10)));
10787 Vwith_echo_area_save_vector = vector;
10791 /* Set up the echo area for use by print functions. MULTIBYTE_P
10792 means we will print multibyte. */
10794 void
10795 setup_echo_area_for_printing (bool multibyte_p)
10797 /* If we can't find an echo area any more, exit. */
10798 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10799 Fkill_emacs (Qnil);
10801 ensure_echo_area_buffers ();
10803 if (!message_buf_print)
10805 /* A message has been output since the last time we printed.
10806 Choose a fresh echo area buffer. */
10807 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10808 echo_area_buffer[0] = echo_buffer[1];
10809 else
10810 echo_area_buffer[0] = echo_buffer[0];
10812 /* Switch to that buffer and clear it. */
10813 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10814 bset_truncate_lines (current_buffer, Qnil);
10816 if (Z > BEG)
10818 ptrdiff_t count = SPECPDL_INDEX ();
10819 specbind (Qinhibit_read_only, Qt);
10820 /* Note that undo recording is always disabled. */
10821 del_range (BEG, Z);
10822 unbind_to (count, Qnil);
10824 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10826 /* Set up the buffer for the multibyteness we need. */
10827 if (multibyte_p
10828 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10829 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10831 /* Raise the frame containing the echo area. */
10832 if (minibuffer_auto_raise)
10834 struct frame *sf = SELECTED_FRAME ();
10835 Lisp_Object mini_window;
10836 mini_window = FRAME_MINIBUF_WINDOW (sf);
10837 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10840 message_log_maybe_newline ();
10841 message_buf_print = true;
10843 else
10845 if (NILP (echo_area_buffer[0]))
10847 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10848 echo_area_buffer[0] = echo_buffer[1];
10849 else
10850 echo_area_buffer[0] = echo_buffer[0];
10853 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10855 /* Someone switched buffers between print requests. */
10856 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10857 bset_truncate_lines (current_buffer, Qnil);
10863 /* Display an echo area message in window W. Value is true if W's
10864 height is changed. If display_last_displayed_message_p,
10865 display the message that was last displayed, otherwise
10866 display the current message. */
10868 static bool
10869 display_echo_area (struct window *w)
10871 bool no_message_p, window_height_changed_p;
10873 /* Temporarily disable garbage collections while displaying the echo
10874 area. This is done because a GC can print a message itself.
10875 That message would modify the echo area buffer's contents while a
10876 redisplay of the buffer is going on, and seriously confuse
10877 redisplay. */
10878 ptrdiff_t count = inhibit_garbage_collection ();
10880 /* If there is no message, we must call display_echo_area_1
10881 nevertheless because it resizes the window. But we will have to
10882 reset the echo_area_buffer in question to nil at the end because
10883 with_echo_area_buffer will sets it to an empty buffer. */
10884 bool i = display_last_displayed_message_p;
10885 /* According to the C99, C11 and C++11 standards, the integral value
10886 of a "bool" is always 0 or 1, so this array access is safe here,
10887 if oddly typed. */
10888 no_message_p = NILP (echo_area_buffer[i]);
10890 window_height_changed_p
10891 = with_echo_area_buffer (w, display_last_displayed_message_p,
10892 display_echo_area_1,
10893 (intptr_t) w, Qnil);
10895 if (no_message_p)
10896 echo_area_buffer[i] = Qnil;
10898 unbind_to (count, Qnil);
10899 return window_height_changed_p;
10903 /* Helper for display_echo_area. Display the current buffer which
10904 contains the current echo area message in window W, a mini-window,
10905 a pointer to which is passed in A1. A2..A4 are currently not used.
10906 Change the height of W so that all of the message is displayed.
10907 Value is true if height of W was changed. */
10909 static bool
10910 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10912 intptr_t i1 = a1;
10913 struct window *w = (struct window *) i1;
10914 Lisp_Object window;
10915 struct text_pos start;
10917 /* We are about to enter redisplay without going through
10918 redisplay_internal, so we need to forget these faces by hand
10919 here. */
10920 forget_escape_and_glyphless_faces ();
10922 /* Do this before displaying, so that we have a large enough glyph
10923 matrix for the display. If we can't get enough space for the
10924 whole text, display the last N lines. That works by setting w->start. */
10925 bool window_height_changed_p = resize_mini_window (w, false);
10927 /* Use the starting position chosen by resize_mini_window. */
10928 SET_TEXT_POS_FROM_MARKER (start, w->start);
10930 /* Display. */
10931 clear_glyph_matrix (w->desired_matrix);
10932 XSETWINDOW (window, w);
10933 try_window (window, start, 0);
10935 return window_height_changed_p;
10939 /* Resize the echo area window to exactly the size needed for the
10940 currently displayed message, if there is one. If a mini-buffer
10941 is active, don't shrink it. */
10943 void
10944 resize_echo_area_exactly (void)
10946 if (BUFFERP (echo_area_buffer[0])
10947 && WINDOWP (echo_area_window))
10949 struct window *w = XWINDOW (echo_area_window);
10950 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10951 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10952 (intptr_t) w, resize_exactly);
10953 if (resized_p)
10955 windows_or_buffers_changed = 42;
10956 update_mode_lines = 30;
10957 redisplay_internal ();
10963 /* Callback function for with_echo_area_buffer, when used from
10964 resize_echo_area_exactly. A1 contains a pointer to the window to
10965 resize, EXACTLY non-nil means resize the mini-window exactly to the
10966 size of the text displayed. A3 and A4 are not used. Value is what
10967 resize_mini_window returns. */
10969 static bool
10970 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10972 intptr_t i1 = a1;
10973 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10977 /* Resize mini-window W to fit the size of its contents. EXACT_P
10978 means size the window exactly to the size needed. Otherwise, it's
10979 only enlarged until W's buffer is empty.
10981 Set W->start to the right place to begin display. If the whole
10982 contents fit, start at the beginning. Otherwise, start so as
10983 to make the end of the contents appear. This is particularly
10984 important for y-or-n-p, but seems desirable generally.
10986 Value is true if the window height has been changed. */
10988 bool
10989 resize_mini_window (struct window *w, bool exact_p)
10991 struct frame *f = XFRAME (w->frame);
10992 bool window_height_changed_p = false;
10994 eassert (MINI_WINDOW_P (w));
10996 /* By default, start display at the beginning. */
10997 set_marker_both (w->start, w->contents,
10998 BUF_BEGV (XBUFFER (w->contents)),
10999 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11001 /* Don't resize windows while redisplaying a window; it would
11002 confuse redisplay functions when the size of the window they are
11003 displaying changes from under them. Such a resizing can happen,
11004 for instance, when which-func prints a long message while
11005 we are running fontification-functions. We're running these
11006 functions with safe_call which binds inhibit-redisplay to t. */
11007 if (!NILP (Vinhibit_redisplay))
11008 return false;
11010 /* Nil means don't try to resize. */
11011 if (NILP (Vresize_mini_windows)
11012 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11013 return false;
11015 if (!FRAME_MINIBUF_ONLY_P (f))
11017 struct it it;
11018 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11019 + WINDOW_PIXEL_HEIGHT (w));
11020 int unit = FRAME_LINE_HEIGHT (f);
11021 int height, max_height;
11022 struct text_pos start;
11023 struct buffer *old_current_buffer = NULL;
11025 if (current_buffer != XBUFFER (w->contents))
11027 old_current_buffer = current_buffer;
11028 set_buffer_internal (XBUFFER (w->contents));
11031 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11033 /* Compute the max. number of lines specified by the user. */
11034 if (FLOATP (Vmax_mini_window_height))
11035 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11036 else if (INTEGERP (Vmax_mini_window_height))
11037 max_height = XINT (Vmax_mini_window_height) * unit;
11038 else
11039 max_height = total_height / 4;
11041 /* Correct that max. height if it's bogus. */
11042 max_height = clip_to_bounds (unit, max_height, total_height);
11044 /* Find out the height of the text in the window. */
11045 if (it.line_wrap == TRUNCATE)
11046 height = unit;
11047 else
11049 last_height = 0;
11050 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11051 if (it.max_ascent == 0 && it.max_descent == 0)
11052 height = it.current_y + last_height;
11053 else
11054 height = it.current_y + it.max_ascent + it.max_descent;
11055 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11058 /* Compute a suitable window start. */
11059 if (height > max_height)
11061 height = (max_height / unit) * unit;
11062 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11063 move_it_vertically_backward (&it, height - unit);
11064 start = it.current.pos;
11066 else
11067 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11068 SET_MARKER_FROM_TEXT_POS (w->start, start);
11070 if (EQ (Vresize_mini_windows, Qgrow_only))
11072 /* Let it grow only, until we display an empty message, in which
11073 case the window shrinks again. */
11074 if (height > WINDOW_PIXEL_HEIGHT (w))
11076 int old_height = WINDOW_PIXEL_HEIGHT (w);
11078 FRAME_WINDOWS_FROZEN (f) = true;
11079 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11080 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11082 else if (height < WINDOW_PIXEL_HEIGHT (w)
11083 && (exact_p || BEGV == ZV))
11085 int old_height = WINDOW_PIXEL_HEIGHT (w);
11087 FRAME_WINDOWS_FROZEN (f) = false;
11088 shrink_mini_window (w, true);
11089 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11092 else
11094 /* Always resize to exact size needed. */
11095 if (height > WINDOW_PIXEL_HEIGHT (w))
11097 int old_height = WINDOW_PIXEL_HEIGHT (w);
11099 FRAME_WINDOWS_FROZEN (f) = true;
11100 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11101 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11103 else if (height < WINDOW_PIXEL_HEIGHT (w))
11105 int old_height = WINDOW_PIXEL_HEIGHT (w);
11107 FRAME_WINDOWS_FROZEN (f) = false;
11108 shrink_mini_window (w, true);
11110 if (height)
11112 FRAME_WINDOWS_FROZEN (f) = true;
11113 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11116 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11120 if (old_current_buffer)
11121 set_buffer_internal (old_current_buffer);
11124 return window_height_changed_p;
11128 /* Value is the current message, a string, or nil if there is no
11129 current message. */
11131 Lisp_Object
11132 current_message (void)
11134 Lisp_Object msg;
11136 if (!BUFFERP (echo_area_buffer[0]))
11137 msg = Qnil;
11138 else
11140 with_echo_area_buffer (0, 0, current_message_1,
11141 (intptr_t) &msg, Qnil);
11142 if (NILP (msg))
11143 echo_area_buffer[0] = Qnil;
11146 return msg;
11150 static bool
11151 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11153 intptr_t i1 = a1;
11154 Lisp_Object *msg = (Lisp_Object *) i1;
11156 if (Z > BEG)
11157 *msg = make_buffer_string (BEG, Z, true);
11158 else
11159 *msg = Qnil;
11160 return false;
11164 /* Push the current message on Vmessage_stack for later restoration
11165 by restore_message. Value is true if the current message isn't
11166 empty. This is a relatively infrequent operation, so it's not
11167 worth optimizing. */
11169 bool
11170 push_message (void)
11172 Lisp_Object msg = current_message ();
11173 Vmessage_stack = Fcons (msg, Vmessage_stack);
11174 return STRINGP (msg);
11178 /* Restore message display from the top of Vmessage_stack. */
11180 void
11181 restore_message (void)
11183 eassert (CONSP (Vmessage_stack));
11184 message3_nolog (XCAR (Vmessage_stack));
11188 /* Handler for unwind-protect calling pop_message. */
11190 void
11191 pop_message_unwind (void)
11193 /* Pop the top-most entry off Vmessage_stack. */
11194 eassert (CONSP (Vmessage_stack));
11195 Vmessage_stack = XCDR (Vmessage_stack);
11199 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11200 exits. If the stack is not empty, we have a missing pop_message
11201 somewhere. */
11203 void
11204 check_message_stack (void)
11206 if (!NILP (Vmessage_stack))
11207 emacs_abort ();
11211 /* Truncate to NCHARS what will be displayed in the echo area the next
11212 time we display it---but don't redisplay it now. */
11214 void
11215 truncate_echo_area (ptrdiff_t nchars)
11217 if (nchars == 0)
11218 echo_area_buffer[0] = Qnil;
11219 else if (!noninteractive
11220 && INTERACTIVE
11221 && !NILP (echo_area_buffer[0]))
11223 struct frame *sf = SELECTED_FRAME ();
11224 /* Error messages get reported properly by cmd_error, so this must be
11225 just an informative message; if the frame hasn't really been
11226 initialized yet, just toss it. */
11227 if (sf->glyphs_initialized_p)
11228 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11233 /* Helper function for truncate_echo_area. Truncate the current
11234 message to at most NCHARS characters. */
11236 static bool
11237 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11239 if (BEG + nchars < Z)
11240 del_range (BEG + nchars, Z);
11241 if (Z == BEG)
11242 echo_area_buffer[0] = Qnil;
11243 return false;
11246 /* Set the current message to STRING. */
11248 static void
11249 set_message (Lisp_Object string)
11251 eassert (STRINGP (string));
11253 message_enable_multibyte = STRING_MULTIBYTE (string);
11255 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11256 message_buf_print = false;
11257 help_echo_showing_p = false;
11259 if (STRINGP (Vdebug_on_message)
11260 && STRINGP (string)
11261 && fast_string_match (Vdebug_on_message, string) >= 0)
11262 call_debugger (list2 (Qerror, string));
11266 /* Helper function for set_message. First argument is ignored and second
11267 argument has the same meaning as for set_message.
11268 This function is called with the echo area buffer being current. */
11270 static bool
11271 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11273 eassert (STRINGP (string));
11275 /* Change multibyteness of the echo buffer appropriately. */
11276 if (message_enable_multibyte
11277 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11278 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11280 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11281 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11282 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11284 /* Insert new message at BEG. */
11285 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11287 /* This function takes care of single/multibyte conversion.
11288 We just have to ensure that the echo area buffer has the right
11289 setting of enable_multibyte_characters. */
11290 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11292 return false;
11296 /* Clear messages. CURRENT_P means clear the current message.
11297 LAST_DISPLAYED_P means clear the message last displayed. */
11299 void
11300 clear_message (bool current_p, bool last_displayed_p)
11302 if (current_p)
11304 echo_area_buffer[0] = Qnil;
11305 message_cleared_p = true;
11308 if (last_displayed_p)
11309 echo_area_buffer[1] = Qnil;
11311 message_buf_print = false;
11314 /* Clear garbaged frames.
11316 This function is used where the old redisplay called
11317 redraw_garbaged_frames which in turn called redraw_frame which in
11318 turn called clear_frame. The call to clear_frame was a source of
11319 flickering. I believe a clear_frame is not necessary. It should
11320 suffice in the new redisplay to invalidate all current matrices,
11321 and ensure a complete redisplay of all windows. */
11323 static void
11324 clear_garbaged_frames (void)
11326 if (frame_garbaged)
11328 Lisp_Object tail, frame;
11329 struct frame *sf = SELECTED_FRAME ();
11331 FOR_EACH_FRAME (tail, frame)
11333 struct frame *f = XFRAME (frame);
11335 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11337 if (f->resized_p
11338 /* It makes no sense to redraw a non-selected TTY
11339 frame, since that will actually clear the
11340 selected frame, and might leave the selected
11341 frame with corrupted display, if it happens not
11342 to be marked garbaged. */
11343 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11344 redraw_frame (f);
11345 else
11346 clear_current_matrices (f);
11347 fset_redisplay (f);
11348 f->garbaged = false;
11349 f->resized_p = false;
11353 frame_garbaged = false;
11358 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11359 selected_frame. */
11361 static void
11362 echo_area_display (bool update_frame_p)
11364 Lisp_Object mini_window;
11365 struct window *w;
11366 struct frame *f;
11367 bool window_height_changed_p = false;
11368 struct frame *sf = SELECTED_FRAME ();
11370 mini_window = FRAME_MINIBUF_WINDOW (sf);
11371 w = XWINDOW (mini_window);
11372 f = XFRAME (WINDOW_FRAME (w));
11374 /* Don't display if frame is invisible or not yet initialized. */
11375 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11376 return;
11378 #ifdef HAVE_WINDOW_SYSTEM
11379 /* When Emacs starts, selected_frame may be the initial terminal
11380 frame. If we let this through, a message would be displayed on
11381 the terminal. */
11382 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11383 return;
11384 #endif /* HAVE_WINDOW_SYSTEM */
11386 /* Redraw garbaged frames. */
11387 clear_garbaged_frames ();
11389 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11391 echo_area_window = mini_window;
11392 window_height_changed_p = display_echo_area (w);
11393 w->must_be_updated_p = true;
11395 /* Update the display, unless called from redisplay_internal.
11396 Also don't update the screen during redisplay itself. The
11397 update will happen at the end of redisplay, and an update
11398 here could cause confusion. */
11399 if (update_frame_p && !redisplaying_p)
11401 int n = 0;
11403 /* If the display update has been interrupted by pending
11404 input, update mode lines in the frame. Due to the
11405 pending input, it might have been that redisplay hasn't
11406 been called, so that mode lines above the echo area are
11407 garbaged. This looks odd, so we prevent it here. */
11408 if (!display_completed)
11409 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11411 if (window_height_changed_p
11412 /* Don't do this if Emacs is shutting down. Redisplay
11413 needs to run hooks. */
11414 && !NILP (Vrun_hooks))
11416 /* Must update other windows. Likewise as in other
11417 cases, don't let this update be interrupted by
11418 pending input. */
11419 ptrdiff_t count = SPECPDL_INDEX ();
11420 specbind (Qredisplay_dont_pause, Qt);
11421 fset_redisplay (f);
11422 redisplay_internal ();
11423 unbind_to (count, Qnil);
11425 else if (FRAME_WINDOW_P (f) && n == 0)
11427 /* Window configuration is the same as before.
11428 Can do with a display update of the echo area,
11429 unless we displayed some mode lines. */
11430 update_single_window (w);
11431 flush_frame (f);
11433 else
11434 update_frame (f, true, true);
11436 /* If cursor is in the echo area, make sure that the next
11437 redisplay displays the minibuffer, so that the cursor will
11438 be replaced with what the minibuffer wants. */
11439 if (cursor_in_echo_area)
11440 wset_redisplay (XWINDOW (mini_window));
11443 else if (!EQ (mini_window, selected_window))
11444 wset_redisplay (XWINDOW (mini_window));
11446 /* Last displayed message is now the current message. */
11447 echo_area_buffer[1] = echo_area_buffer[0];
11448 /* Inform read_char that we're not echoing. */
11449 echo_message_buffer = Qnil;
11451 /* Prevent redisplay optimization in redisplay_internal by resetting
11452 this_line_start_pos. This is done because the mini-buffer now
11453 displays the message instead of its buffer text. */
11454 if (EQ (mini_window, selected_window))
11455 CHARPOS (this_line_start_pos) = 0;
11457 if (window_height_changed_p)
11459 fset_redisplay (f);
11461 /* If window configuration was changed, frames may have been
11462 marked garbaged. Clear them or we will experience
11463 surprises wrt scrolling.
11464 FIXME: How/why/when? */
11465 clear_garbaged_frames ();
11469 /* True if W's buffer was changed but not saved. */
11471 static bool
11472 window_buffer_changed (struct window *w)
11474 struct buffer *b = XBUFFER (w->contents);
11476 eassert (BUFFER_LIVE_P (b));
11478 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11481 /* True if W has %c in its mode line and mode line should be updated. */
11483 static bool
11484 mode_line_update_needed (struct window *w)
11486 return (w->column_number_displayed != -1
11487 && !(PT == w->last_point && !window_outdated (w))
11488 && (w->column_number_displayed != current_column ()));
11491 /* True if window start of W is frozen and may not be changed during
11492 redisplay. */
11494 static bool
11495 window_frozen_p (struct window *w)
11497 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11499 Lisp_Object window;
11501 XSETWINDOW (window, w);
11502 if (MINI_WINDOW_P (w))
11503 return false;
11504 else if (EQ (window, selected_window))
11505 return false;
11506 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11507 && EQ (window, Vminibuf_scroll_window))
11508 /* This special window can't be frozen too. */
11509 return false;
11510 else
11511 return true;
11513 return false;
11516 /***********************************************************************
11517 Mode Lines and Frame Titles
11518 ***********************************************************************/
11520 /* A buffer for constructing non-propertized mode-line strings and
11521 frame titles in it; allocated from the heap in init_xdisp and
11522 resized as needed in store_mode_line_noprop_char. */
11524 static char *mode_line_noprop_buf;
11526 /* The buffer's end, and a current output position in it. */
11528 static char *mode_line_noprop_buf_end;
11529 static char *mode_line_noprop_ptr;
11531 #define MODE_LINE_NOPROP_LEN(start) \
11532 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11534 static enum {
11535 MODE_LINE_DISPLAY = 0,
11536 MODE_LINE_TITLE,
11537 MODE_LINE_NOPROP,
11538 MODE_LINE_STRING
11539 } mode_line_target;
11541 /* Alist that caches the results of :propertize.
11542 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11543 static Lisp_Object mode_line_proptrans_alist;
11545 /* List of strings making up the mode-line. */
11546 static Lisp_Object mode_line_string_list;
11548 /* Base face property when building propertized mode line string. */
11549 static Lisp_Object mode_line_string_face;
11550 static Lisp_Object mode_line_string_face_prop;
11553 /* Unwind data for mode line strings */
11555 static Lisp_Object Vmode_line_unwind_vector;
11557 static Lisp_Object
11558 format_mode_line_unwind_data (struct frame *target_frame,
11559 struct buffer *obuf,
11560 Lisp_Object owin,
11561 bool save_proptrans)
11563 Lisp_Object vector, tmp;
11565 /* Reduce consing by keeping one vector in
11566 Vwith_echo_area_save_vector. */
11567 vector = Vmode_line_unwind_vector;
11568 Vmode_line_unwind_vector = Qnil;
11570 if (NILP (vector))
11571 vector = Fmake_vector (make_number (10), Qnil);
11573 ASET (vector, 0, make_number (mode_line_target));
11574 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11575 ASET (vector, 2, mode_line_string_list);
11576 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11577 ASET (vector, 4, mode_line_string_face);
11578 ASET (vector, 5, mode_line_string_face_prop);
11580 if (obuf)
11581 XSETBUFFER (tmp, obuf);
11582 else
11583 tmp = Qnil;
11584 ASET (vector, 6, tmp);
11585 ASET (vector, 7, owin);
11586 if (target_frame)
11588 /* Similarly to `with-selected-window', if the operation selects
11589 a window on another frame, we must restore that frame's
11590 selected window, and (for a tty) the top-frame. */
11591 ASET (vector, 8, target_frame->selected_window);
11592 if (FRAME_TERMCAP_P (target_frame))
11593 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11596 return vector;
11599 static void
11600 unwind_format_mode_line (Lisp_Object vector)
11602 Lisp_Object old_window = AREF (vector, 7);
11603 Lisp_Object target_frame_window = AREF (vector, 8);
11604 Lisp_Object old_top_frame = AREF (vector, 9);
11606 mode_line_target = XINT (AREF (vector, 0));
11607 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11608 mode_line_string_list = AREF (vector, 2);
11609 if (! EQ (AREF (vector, 3), Qt))
11610 mode_line_proptrans_alist = AREF (vector, 3);
11611 mode_line_string_face = AREF (vector, 4);
11612 mode_line_string_face_prop = AREF (vector, 5);
11614 /* Select window before buffer, since it may change the buffer. */
11615 if (!NILP (old_window))
11617 /* If the operation that we are unwinding had selected a window
11618 on a different frame, reset its frame-selected-window. For a
11619 text terminal, reset its top-frame if necessary. */
11620 if (!NILP (target_frame_window))
11622 Lisp_Object frame
11623 = WINDOW_FRAME (XWINDOW (target_frame_window));
11625 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11626 Fselect_window (target_frame_window, Qt);
11628 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11629 Fselect_frame (old_top_frame, Qt);
11632 Fselect_window (old_window, Qt);
11635 if (!NILP (AREF (vector, 6)))
11637 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11638 ASET (vector, 6, Qnil);
11641 Vmode_line_unwind_vector = vector;
11645 /* Store a single character C for the frame title in mode_line_noprop_buf.
11646 Re-allocate mode_line_noprop_buf if necessary. */
11648 static void
11649 store_mode_line_noprop_char (char c)
11651 /* If output position has reached the end of the allocated buffer,
11652 increase the buffer's size. */
11653 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11655 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11656 ptrdiff_t size = len;
11657 mode_line_noprop_buf =
11658 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11659 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11660 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11663 *mode_line_noprop_ptr++ = c;
11667 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11668 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11669 characters that yield more columns than PRECISION; PRECISION <= 0
11670 means copy the whole string. Pad with spaces until FIELD_WIDTH
11671 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11672 pad. Called from display_mode_element when it is used to build a
11673 frame title. */
11675 static int
11676 store_mode_line_noprop (const char *string, int field_width, int precision)
11678 const unsigned char *str = (const unsigned char *) string;
11679 int n = 0;
11680 ptrdiff_t dummy, nbytes;
11682 /* Copy at most PRECISION chars from STR. */
11683 nbytes = strlen (string);
11684 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11685 while (nbytes--)
11686 store_mode_line_noprop_char (*str++);
11688 /* Fill up with spaces until FIELD_WIDTH reached. */
11689 while (field_width > 0
11690 && n < field_width)
11692 store_mode_line_noprop_char (' ');
11693 ++n;
11696 return n;
11699 /***********************************************************************
11700 Frame Titles
11701 ***********************************************************************/
11703 #ifdef HAVE_WINDOW_SYSTEM
11705 /* Set the title of FRAME, if it has changed. The title format is
11706 Vicon_title_format if FRAME is iconified, otherwise it is
11707 frame_title_format. */
11709 static void
11710 x_consider_frame_title (Lisp_Object frame)
11712 struct frame *f = XFRAME (frame);
11714 if ((FRAME_WINDOW_P (f)
11715 || FRAME_MINIBUF_ONLY_P (f)
11716 || f->explicit_name)
11717 && NILP (Fframe_parameter (frame, Qtooltip)))
11719 /* Do we have more than one visible frame on this X display? */
11720 Lisp_Object tail, other_frame, fmt;
11721 ptrdiff_t title_start;
11722 char *title;
11723 ptrdiff_t len;
11724 struct it it;
11725 ptrdiff_t count = SPECPDL_INDEX ();
11727 FOR_EACH_FRAME (tail, other_frame)
11729 struct frame *tf = XFRAME (other_frame);
11731 if (tf != f
11732 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11733 && !FRAME_MINIBUF_ONLY_P (tf)
11734 && !EQ (other_frame, tip_frame)
11735 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11736 break;
11739 /* Set global variable indicating that multiple frames exist. */
11740 multiple_frames = CONSP (tail);
11742 /* Switch to the buffer of selected window of the frame. Set up
11743 mode_line_target so that display_mode_element will output into
11744 mode_line_noprop_buf; then display the title. */
11745 record_unwind_protect (unwind_format_mode_line,
11746 format_mode_line_unwind_data
11747 (f, current_buffer, selected_window, false));
11748 /* select-frame calls resize_mini_window, which could resize the
11749 mini-window and by that undo the effect of this redisplay
11750 cycle wrt minibuffer and echo-area display. Binding
11751 inhibit-redisplay to t makes the call to resize_mini_window a
11752 no-op, thus avoiding the adverse side effects. */
11753 specbind (Qinhibit_redisplay, Qt);
11755 Fselect_window (f->selected_window, Qt);
11756 set_buffer_internal_1
11757 (XBUFFER (XWINDOW (f->selected_window)->contents));
11758 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11760 mode_line_target = MODE_LINE_TITLE;
11761 title_start = MODE_LINE_NOPROP_LEN (0);
11762 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11763 NULL, DEFAULT_FACE_ID);
11764 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11765 len = MODE_LINE_NOPROP_LEN (title_start);
11766 title = mode_line_noprop_buf + title_start;
11767 unbind_to (count, Qnil);
11769 /* Set the title only if it's changed. This avoids consing in
11770 the common case where it hasn't. (If it turns out that we've
11771 already wasted too much time by walking through the list with
11772 display_mode_element, then we might need to optimize at a
11773 higher level than this.) */
11774 if (! STRINGP (f->name)
11775 || SBYTES (f->name) != len
11776 || memcmp (title, SDATA (f->name), len) != 0)
11777 x_implicitly_set_name (f, make_string (title, len), Qnil);
11781 #endif /* not HAVE_WINDOW_SYSTEM */
11784 /***********************************************************************
11785 Menu Bars
11786 ***********************************************************************/
11788 /* True if we will not redisplay all visible windows. */
11789 #define REDISPLAY_SOME_P() \
11790 ((windows_or_buffers_changed == 0 \
11791 || windows_or_buffers_changed == REDISPLAY_SOME) \
11792 && (update_mode_lines == 0 \
11793 || update_mode_lines == REDISPLAY_SOME))
11795 /* Prepare for redisplay by updating menu-bar item lists when
11796 appropriate. This can call eval. */
11798 static void
11799 prepare_menu_bars (void)
11801 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11802 bool some_windows = REDISPLAY_SOME_P ();
11803 Lisp_Object tooltip_frame;
11805 #ifdef HAVE_WINDOW_SYSTEM
11806 tooltip_frame = tip_frame;
11807 #else
11808 tooltip_frame = Qnil;
11809 #endif
11811 if (FUNCTIONP (Vpre_redisplay_function))
11813 Lisp_Object windows = all_windows ? Qt : Qnil;
11814 if (all_windows && some_windows)
11816 Lisp_Object ws = window_list ();
11817 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11819 Lisp_Object this = XCAR (ws);
11820 struct window *w = XWINDOW (this);
11821 if (w->redisplay
11822 || XFRAME (w->frame)->redisplay
11823 || XBUFFER (w->contents)->text->redisplay)
11825 windows = Fcons (this, windows);
11829 safe__call1 (true, Vpre_redisplay_function, windows);
11832 /* Update all frame titles based on their buffer names, etc. We do
11833 this before the menu bars so that the buffer-menu will show the
11834 up-to-date frame titles. */
11835 #ifdef HAVE_WINDOW_SYSTEM
11836 if (all_windows)
11838 Lisp_Object tail, frame;
11840 FOR_EACH_FRAME (tail, frame)
11842 struct frame *f = XFRAME (frame);
11843 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11844 if (some_windows
11845 && !f->redisplay
11846 && !w->redisplay
11847 && !XBUFFER (w->contents)->text->redisplay)
11848 continue;
11850 if (!EQ (frame, tooltip_frame)
11851 && (FRAME_ICONIFIED_P (f)
11852 || FRAME_VISIBLE_P (f) == 1
11853 /* Exclude TTY frames that are obscured because they
11854 are not the top frame on their console. This is
11855 because x_consider_frame_title actually switches
11856 to the frame, which for TTY frames means it is
11857 marked as garbaged, and will be completely
11858 redrawn on the next redisplay cycle. This causes
11859 TTY frames to be completely redrawn, when there
11860 are more than one of them, even though nothing
11861 should be changed on display. */
11862 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11863 x_consider_frame_title (frame);
11866 #endif /* HAVE_WINDOW_SYSTEM */
11868 /* Update the menu bar item lists, if appropriate. This has to be
11869 done before any actual redisplay or generation of display lines. */
11871 if (all_windows)
11873 Lisp_Object tail, frame;
11874 ptrdiff_t count = SPECPDL_INDEX ();
11875 /* True means that update_menu_bar has run its hooks
11876 so any further calls to update_menu_bar shouldn't do so again. */
11877 bool menu_bar_hooks_run = false;
11879 record_unwind_save_match_data ();
11881 FOR_EACH_FRAME (tail, frame)
11883 struct frame *f = XFRAME (frame);
11884 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11886 /* Ignore tooltip frame. */
11887 if (EQ (frame, tooltip_frame))
11888 continue;
11890 if (some_windows
11891 && !f->redisplay
11892 && !w->redisplay
11893 && !XBUFFER (w->contents)->text->redisplay)
11894 continue;
11896 run_window_size_change_functions (frame);
11897 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11898 #ifdef HAVE_WINDOW_SYSTEM
11899 update_tool_bar (f, false);
11900 #endif
11903 unbind_to (count, Qnil);
11905 else
11907 struct frame *sf = SELECTED_FRAME ();
11908 update_menu_bar (sf, true, false);
11909 #ifdef HAVE_WINDOW_SYSTEM
11910 update_tool_bar (sf, true);
11911 #endif
11916 /* Update the menu bar item list for frame F. This has to be done
11917 before we start to fill in any display lines, because it can call
11918 eval.
11920 If SAVE_MATCH_DATA, we must save and restore it here.
11922 If HOOKS_RUN, a previous call to update_menu_bar
11923 already ran the menu bar hooks for this redisplay, so there
11924 is no need to run them again. The return value is the
11925 updated value of this flag, to pass to the next call. */
11927 static bool
11928 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11930 Lisp_Object window;
11931 struct window *w;
11933 /* If called recursively during a menu update, do nothing. This can
11934 happen when, for instance, an activate-menubar-hook causes a
11935 redisplay. */
11936 if (inhibit_menubar_update)
11937 return hooks_run;
11939 window = FRAME_SELECTED_WINDOW (f);
11940 w = XWINDOW (window);
11942 if (FRAME_WINDOW_P (f)
11944 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11945 || defined (HAVE_NS) || defined (USE_GTK)
11946 FRAME_EXTERNAL_MENU_BAR (f)
11947 #else
11948 FRAME_MENU_BAR_LINES (f) > 0
11949 #endif
11950 : FRAME_MENU_BAR_LINES (f) > 0)
11952 /* If the user has switched buffers or windows, we need to
11953 recompute to reflect the new bindings. But we'll
11954 recompute when update_mode_lines is set too; that means
11955 that people can use force-mode-line-update to request
11956 that the menu bar be recomputed. The adverse effect on
11957 the rest of the redisplay algorithm is about the same as
11958 windows_or_buffers_changed anyway. */
11959 if (windows_or_buffers_changed
11960 /* This used to test w->update_mode_line, but we believe
11961 there is no need to recompute the menu in that case. */
11962 || update_mode_lines
11963 || window_buffer_changed (w))
11965 struct buffer *prev = current_buffer;
11966 ptrdiff_t count = SPECPDL_INDEX ();
11968 specbind (Qinhibit_menubar_update, Qt);
11970 set_buffer_internal_1 (XBUFFER (w->contents));
11971 if (save_match_data)
11972 record_unwind_save_match_data ();
11973 if (NILP (Voverriding_local_map_menu_flag))
11975 specbind (Qoverriding_terminal_local_map, Qnil);
11976 specbind (Qoverriding_local_map, Qnil);
11979 if (!hooks_run)
11981 /* Run the Lucid hook. */
11982 safe_run_hooks (Qactivate_menubar_hook);
11984 /* If it has changed current-menubar from previous value,
11985 really recompute the menu-bar from the value. */
11986 if (! NILP (Vlucid_menu_bar_dirty_flag))
11987 call0 (Qrecompute_lucid_menubar);
11989 safe_run_hooks (Qmenu_bar_update_hook);
11991 hooks_run = true;
11994 XSETFRAME (Vmenu_updating_frame, f);
11995 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11997 /* Redisplay the menu bar in case we changed it. */
11998 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11999 || defined (HAVE_NS) || defined (USE_GTK)
12000 if (FRAME_WINDOW_P (f))
12002 #if defined (HAVE_NS)
12003 /* All frames on Mac OS share the same menubar. So only
12004 the selected frame should be allowed to set it. */
12005 if (f == SELECTED_FRAME ())
12006 #endif
12007 set_frame_menubar (f, false, false);
12009 else
12010 /* On a terminal screen, the menu bar is an ordinary screen
12011 line, and this makes it get updated. */
12012 w->update_mode_line = true;
12013 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12014 /* In the non-toolkit version, the menu bar is an ordinary screen
12015 line, and this makes it get updated. */
12016 w->update_mode_line = true;
12017 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12019 unbind_to (count, Qnil);
12020 set_buffer_internal_1 (prev);
12024 return hooks_run;
12027 /***********************************************************************
12028 Tool-bars
12029 ***********************************************************************/
12031 #ifdef HAVE_WINDOW_SYSTEM
12033 /* Select `frame' temporarily without running all the code in
12034 do_switch_frame.
12035 FIXME: Maybe do_switch_frame should be trimmed down similarly
12036 when `norecord' is set. */
12037 static void
12038 fast_set_selected_frame (Lisp_Object frame)
12040 if (!EQ (selected_frame, frame))
12042 selected_frame = frame;
12043 selected_window = XFRAME (frame)->selected_window;
12047 /* Update the tool-bar item list for frame F. This has to be done
12048 before we start to fill in any display lines. Called from
12049 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12050 and restore it here. */
12052 static void
12053 update_tool_bar (struct frame *f, bool save_match_data)
12055 #if defined (USE_GTK) || defined (HAVE_NS)
12056 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12057 #else
12058 bool do_update = (WINDOWP (f->tool_bar_window)
12059 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12060 #endif
12062 if (do_update)
12064 Lisp_Object window;
12065 struct window *w;
12067 window = FRAME_SELECTED_WINDOW (f);
12068 w = XWINDOW (window);
12070 /* If the user has switched buffers or windows, we need to
12071 recompute to reflect the new bindings. But we'll
12072 recompute when update_mode_lines is set too; that means
12073 that people can use force-mode-line-update to request
12074 that the menu bar be recomputed. The adverse effect on
12075 the rest of the redisplay algorithm is about the same as
12076 windows_or_buffers_changed anyway. */
12077 if (windows_or_buffers_changed
12078 || w->update_mode_line
12079 || update_mode_lines
12080 || window_buffer_changed (w))
12082 struct buffer *prev = current_buffer;
12083 ptrdiff_t count = SPECPDL_INDEX ();
12084 Lisp_Object frame, new_tool_bar;
12085 int new_n_tool_bar;
12087 /* Set current_buffer to the buffer of the selected
12088 window of the frame, so that we get the right local
12089 keymaps. */
12090 set_buffer_internal_1 (XBUFFER (w->contents));
12092 /* Save match data, if we must. */
12093 if (save_match_data)
12094 record_unwind_save_match_data ();
12096 /* Make sure that we don't accidentally use bogus keymaps. */
12097 if (NILP (Voverriding_local_map_menu_flag))
12099 specbind (Qoverriding_terminal_local_map, Qnil);
12100 specbind (Qoverriding_local_map, Qnil);
12103 /* We must temporarily set the selected frame to this frame
12104 before calling tool_bar_items, because the calculation of
12105 the tool-bar keymap uses the selected frame (see
12106 `tool-bar-make-keymap' in tool-bar.el). */
12107 eassert (EQ (selected_window,
12108 /* Since we only explicitly preserve selected_frame,
12109 check that selected_window would be redundant. */
12110 XFRAME (selected_frame)->selected_window));
12111 record_unwind_protect (fast_set_selected_frame, selected_frame);
12112 XSETFRAME (frame, f);
12113 fast_set_selected_frame (frame);
12115 /* Build desired tool-bar items from keymaps. */
12116 new_tool_bar
12117 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12118 &new_n_tool_bar);
12120 /* Redisplay the tool-bar if we changed it. */
12121 if (new_n_tool_bar != f->n_tool_bar_items
12122 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12124 /* Redisplay that happens asynchronously due to an expose event
12125 may access f->tool_bar_items. Make sure we update both
12126 variables within BLOCK_INPUT so no such event interrupts. */
12127 block_input ();
12128 fset_tool_bar_items (f, new_tool_bar);
12129 f->n_tool_bar_items = new_n_tool_bar;
12130 w->update_mode_line = true;
12131 unblock_input ();
12134 unbind_to (count, Qnil);
12135 set_buffer_internal_1 (prev);
12140 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12142 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12143 F's desired tool-bar contents. F->tool_bar_items must have
12144 been set up previously by calling prepare_menu_bars. */
12146 static void
12147 build_desired_tool_bar_string (struct frame *f)
12149 int i, size, size_needed;
12150 Lisp_Object image, plist;
12152 image = plist = Qnil;
12154 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12155 Otherwise, make a new string. */
12157 /* The size of the string we might be able to reuse. */
12158 size = (STRINGP (f->desired_tool_bar_string)
12159 ? SCHARS (f->desired_tool_bar_string)
12160 : 0);
12162 /* We need one space in the string for each image. */
12163 size_needed = f->n_tool_bar_items;
12165 /* Reuse f->desired_tool_bar_string, if possible. */
12166 if (size < size_needed || NILP (f->desired_tool_bar_string))
12167 fset_desired_tool_bar_string
12168 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12169 else
12171 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12172 Fremove_text_properties (make_number (0), make_number (size),
12173 props, f->desired_tool_bar_string);
12176 /* Put a `display' property on the string for the images to display,
12177 put a `menu_item' property on tool-bar items with a value that
12178 is the index of the item in F's tool-bar item vector. */
12179 for (i = 0; i < f->n_tool_bar_items; ++i)
12181 #define PROP(IDX) \
12182 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12184 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12185 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12186 int hmargin, vmargin, relief, idx, end;
12188 /* If image is a vector, choose the image according to the
12189 button state. */
12190 image = PROP (TOOL_BAR_ITEM_IMAGES);
12191 if (VECTORP (image))
12193 if (enabled_p)
12194 idx = (selected_p
12195 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12196 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12197 else
12198 idx = (selected_p
12199 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12200 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12202 eassert (ASIZE (image) >= idx);
12203 image = AREF (image, idx);
12205 else
12206 idx = -1;
12208 /* Ignore invalid image specifications. */
12209 if (!valid_image_p (image))
12210 continue;
12212 /* Display the tool-bar button pressed, or depressed. */
12213 plist = Fcopy_sequence (XCDR (image));
12215 /* Compute margin and relief to draw. */
12216 relief = (tool_bar_button_relief >= 0
12217 ? tool_bar_button_relief
12218 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12219 hmargin = vmargin = relief;
12221 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12222 INT_MAX - max (hmargin, vmargin)))
12224 hmargin += XFASTINT (Vtool_bar_button_margin);
12225 vmargin += XFASTINT (Vtool_bar_button_margin);
12227 else if (CONSP (Vtool_bar_button_margin))
12229 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12230 INT_MAX - hmargin))
12231 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12233 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12234 INT_MAX - vmargin))
12235 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12238 if (auto_raise_tool_bar_buttons_p)
12240 /* Add a `:relief' property to the image spec if the item is
12241 selected. */
12242 if (selected_p)
12244 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12245 hmargin -= relief;
12246 vmargin -= relief;
12249 else
12251 /* If image is selected, display it pressed, i.e. with a
12252 negative relief. If it's not selected, display it with a
12253 raised relief. */
12254 plist = Fplist_put (plist, QCrelief,
12255 (selected_p
12256 ? make_number (-relief)
12257 : make_number (relief)));
12258 hmargin -= relief;
12259 vmargin -= relief;
12262 /* Put a margin around the image. */
12263 if (hmargin || vmargin)
12265 if (hmargin == vmargin)
12266 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12267 else
12268 plist = Fplist_put (plist, QCmargin,
12269 Fcons (make_number (hmargin),
12270 make_number (vmargin)));
12273 /* If button is not enabled, and we don't have special images
12274 for the disabled state, make the image appear disabled by
12275 applying an appropriate algorithm to it. */
12276 if (!enabled_p && idx < 0)
12277 plist = Fplist_put (plist, QCconversion, Qdisabled);
12279 /* Put a `display' text property on the string for the image to
12280 display. Put a `menu-item' property on the string that gives
12281 the start of this item's properties in the tool-bar items
12282 vector. */
12283 image = Fcons (Qimage, plist);
12284 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12285 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12287 /* Let the last image hide all remaining spaces in the tool bar
12288 string. The string can be longer than needed when we reuse a
12289 previous string. */
12290 if (i + 1 == f->n_tool_bar_items)
12291 end = SCHARS (f->desired_tool_bar_string);
12292 else
12293 end = i + 1;
12294 Fadd_text_properties (make_number (i), make_number (end),
12295 props, f->desired_tool_bar_string);
12296 #undef PROP
12301 /* Display one line of the tool-bar of frame IT->f.
12303 HEIGHT specifies the desired height of the tool-bar line.
12304 If the actual height of the glyph row is less than HEIGHT, the
12305 row's height is increased to HEIGHT, and the icons are centered
12306 vertically in the new height.
12308 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12309 count a final empty row in case the tool-bar width exactly matches
12310 the window width.
12313 static void
12314 display_tool_bar_line (struct it *it, int height)
12316 struct glyph_row *row = it->glyph_row;
12317 int max_x = it->last_visible_x;
12318 struct glyph *last;
12320 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12321 clear_glyph_row (row);
12322 row->enabled_p = true;
12323 row->y = it->current_y;
12325 /* Note that this isn't made use of if the face hasn't a box,
12326 so there's no need to check the face here. */
12327 it->start_of_box_run_p = true;
12329 while (it->current_x < max_x)
12331 int x, n_glyphs_before, i, nglyphs;
12332 struct it it_before;
12334 /* Get the next display element. */
12335 if (!get_next_display_element (it))
12337 /* Don't count empty row if we are counting needed tool-bar lines. */
12338 if (height < 0 && !it->hpos)
12339 return;
12340 break;
12343 /* Produce glyphs. */
12344 n_glyphs_before = row->used[TEXT_AREA];
12345 it_before = *it;
12347 PRODUCE_GLYPHS (it);
12349 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12350 i = 0;
12351 x = it_before.current_x;
12352 while (i < nglyphs)
12354 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12356 if (x + glyph->pixel_width > max_x)
12358 /* Glyph doesn't fit on line. Backtrack. */
12359 row->used[TEXT_AREA] = n_glyphs_before;
12360 *it = it_before;
12361 /* If this is the only glyph on this line, it will never fit on the
12362 tool-bar, so skip it. But ensure there is at least one glyph,
12363 so we don't accidentally disable the tool-bar. */
12364 if (n_glyphs_before == 0
12365 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12366 break;
12367 goto out;
12370 ++it->hpos;
12371 x += glyph->pixel_width;
12372 ++i;
12375 /* Stop at line end. */
12376 if (ITERATOR_AT_END_OF_LINE_P (it))
12377 break;
12379 set_iterator_to_next (it, true);
12382 out:;
12384 row->displays_text_p = row->used[TEXT_AREA] != 0;
12386 /* Use default face for the border below the tool bar.
12388 FIXME: When auto-resize-tool-bars is grow-only, there is
12389 no additional border below the possibly empty tool-bar lines.
12390 So to make the extra empty lines look "normal", we have to
12391 use the tool-bar face for the border too. */
12392 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12393 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12394 it->face_id = DEFAULT_FACE_ID;
12396 extend_face_to_end_of_line (it);
12397 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12398 last->right_box_line_p = true;
12399 if (last == row->glyphs[TEXT_AREA])
12400 last->left_box_line_p = true;
12402 /* Make line the desired height and center it vertically. */
12403 if ((height -= it->max_ascent + it->max_descent) > 0)
12405 /* Don't add more than one line height. */
12406 height %= FRAME_LINE_HEIGHT (it->f);
12407 it->max_ascent += height / 2;
12408 it->max_descent += (height + 1) / 2;
12411 compute_line_metrics (it);
12413 /* If line is empty, make it occupy the rest of the tool-bar. */
12414 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12416 row->height = row->phys_height = it->last_visible_y - row->y;
12417 row->visible_height = row->height;
12418 row->ascent = row->phys_ascent = 0;
12419 row->extra_line_spacing = 0;
12422 row->full_width_p = true;
12423 row->continued_p = false;
12424 row->truncated_on_left_p = false;
12425 row->truncated_on_right_p = false;
12427 it->current_x = it->hpos = 0;
12428 it->current_y += row->height;
12429 ++it->vpos;
12430 ++it->glyph_row;
12434 /* Value is the number of pixels needed to make all tool-bar items of
12435 frame F visible. The actual number of glyph rows needed is
12436 returned in *N_ROWS if non-NULL. */
12437 static int
12438 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12440 struct window *w = XWINDOW (f->tool_bar_window);
12441 struct it it;
12442 /* tool_bar_height is called from redisplay_tool_bar after building
12443 the desired matrix, so use (unused) mode-line row as temporary row to
12444 avoid destroying the first tool-bar row. */
12445 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12447 /* Initialize an iterator for iteration over
12448 F->desired_tool_bar_string in the tool-bar window of frame F. */
12449 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12450 temp_row->reversed_p = false;
12451 it.first_visible_x = 0;
12452 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12453 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12454 it.paragraph_embedding = L2R;
12456 while (!ITERATOR_AT_END_P (&it))
12458 clear_glyph_row (temp_row);
12459 it.glyph_row = temp_row;
12460 display_tool_bar_line (&it, -1);
12462 clear_glyph_row (temp_row);
12464 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12465 if (n_rows)
12466 *n_rows = it.vpos > 0 ? it.vpos : -1;
12468 if (pixelwise)
12469 return it.current_y;
12470 else
12471 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12474 #endif /* !USE_GTK && !HAVE_NS */
12476 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12477 0, 2, 0,
12478 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12479 If FRAME is nil or omitted, use the selected frame. Optional argument
12480 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12481 (Lisp_Object frame, Lisp_Object pixelwise)
12483 int height = 0;
12485 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12486 struct frame *f = decode_any_frame (frame);
12488 if (WINDOWP (f->tool_bar_window)
12489 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12491 update_tool_bar (f, true);
12492 if (f->n_tool_bar_items)
12494 build_desired_tool_bar_string (f);
12495 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12498 #endif
12500 return make_number (height);
12504 /* Display the tool-bar of frame F. Value is true if tool-bar's
12505 height should be changed. */
12506 static bool
12507 redisplay_tool_bar (struct frame *f)
12509 f->tool_bar_redisplayed = true;
12510 #if defined (USE_GTK) || defined (HAVE_NS)
12512 if (FRAME_EXTERNAL_TOOL_BAR (f))
12513 update_frame_tool_bar (f);
12514 return false;
12516 #else /* !USE_GTK && !HAVE_NS */
12518 struct window *w;
12519 struct it it;
12520 struct glyph_row *row;
12522 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12523 do anything. This means you must start with tool-bar-lines
12524 non-zero to get the auto-sizing effect. Or in other words, you
12525 can turn off tool-bars by specifying tool-bar-lines zero. */
12526 if (!WINDOWP (f->tool_bar_window)
12527 || (w = XWINDOW (f->tool_bar_window),
12528 WINDOW_TOTAL_LINES (w) == 0))
12529 return false;
12531 /* Set up an iterator for the tool-bar window. */
12532 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12533 it.first_visible_x = 0;
12534 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12535 row = it.glyph_row;
12536 row->reversed_p = false;
12538 /* Build a string that represents the contents of the tool-bar. */
12539 build_desired_tool_bar_string (f);
12540 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12541 /* FIXME: This should be controlled by a user option. But it
12542 doesn't make sense to have an R2L tool bar if the menu bar cannot
12543 be drawn also R2L, and making the menu bar R2L is tricky due
12544 toolkit-specific code that implements it. If an R2L tool bar is
12545 ever supported, display_tool_bar_line should also be augmented to
12546 call unproduce_glyphs like display_line and display_string
12547 do. */
12548 it.paragraph_embedding = L2R;
12550 if (f->n_tool_bar_rows == 0)
12552 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12554 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12556 x_change_tool_bar_height (f, new_height);
12557 frame_default_tool_bar_height = new_height;
12558 /* Always do that now. */
12559 clear_glyph_matrix (w->desired_matrix);
12560 f->fonts_changed = true;
12561 return true;
12565 /* Display as many lines as needed to display all tool-bar items. */
12567 if (f->n_tool_bar_rows > 0)
12569 int border, rows, height, extra;
12571 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12572 border = XINT (Vtool_bar_border);
12573 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12574 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12575 else if (EQ (Vtool_bar_border, Qborder_width))
12576 border = f->border_width;
12577 else
12578 border = 0;
12579 if (border < 0)
12580 border = 0;
12582 rows = f->n_tool_bar_rows;
12583 height = max (1, (it.last_visible_y - border) / rows);
12584 extra = it.last_visible_y - border - height * rows;
12586 while (it.current_y < it.last_visible_y)
12588 int h = 0;
12589 if (extra > 0 && rows-- > 0)
12591 h = (extra + rows - 1) / rows;
12592 extra -= h;
12594 display_tool_bar_line (&it, height + h);
12597 else
12599 while (it.current_y < it.last_visible_y)
12600 display_tool_bar_line (&it, 0);
12603 /* It doesn't make much sense to try scrolling in the tool-bar
12604 window, so don't do it. */
12605 w->desired_matrix->no_scrolling_p = true;
12606 w->must_be_updated_p = true;
12608 if (!NILP (Vauto_resize_tool_bars))
12610 bool change_height_p = true;
12612 /* If we couldn't display everything, change the tool-bar's
12613 height if there is room for more. */
12614 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12615 change_height_p = true;
12617 /* We subtract 1 because display_tool_bar_line advances the
12618 glyph_row pointer before returning to its caller. We want to
12619 examine the last glyph row produced by
12620 display_tool_bar_line. */
12621 row = it.glyph_row - 1;
12623 /* If there are blank lines at the end, except for a partially
12624 visible blank line at the end that is smaller than
12625 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12626 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12627 && row->height >= FRAME_LINE_HEIGHT (f))
12628 change_height_p = true;
12630 /* If row displays tool-bar items, but is partially visible,
12631 change the tool-bar's height. */
12632 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12633 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12634 change_height_p = true;
12636 /* Resize windows as needed by changing the `tool-bar-lines'
12637 frame parameter. */
12638 if (change_height_p)
12640 int nrows;
12641 int new_height = tool_bar_height (f, &nrows, true);
12643 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12644 && !f->minimize_tool_bar_window_p)
12645 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12646 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12647 f->minimize_tool_bar_window_p = false;
12649 if (change_height_p)
12651 x_change_tool_bar_height (f, new_height);
12652 frame_default_tool_bar_height = new_height;
12653 clear_glyph_matrix (w->desired_matrix);
12654 f->n_tool_bar_rows = nrows;
12655 f->fonts_changed = true;
12657 return true;
12662 f->minimize_tool_bar_window_p = false;
12663 return false;
12665 #endif /* USE_GTK || HAVE_NS */
12668 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12670 /* Get information about the tool-bar item which is displayed in GLYPH
12671 on frame F. Return in *PROP_IDX the index where tool-bar item
12672 properties start in F->tool_bar_items. Value is false if
12673 GLYPH doesn't display a tool-bar item. */
12675 static bool
12676 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12678 Lisp_Object prop;
12679 int charpos;
12681 /* This function can be called asynchronously, which means we must
12682 exclude any possibility that Fget_text_property signals an
12683 error. */
12684 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12685 charpos = max (0, charpos);
12687 /* Get the text property `menu-item' at pos. The value of that
12688 property is the start index of this item's properties in
12689 F->tool_bar_items. */
12690 prop = Fget_text_property (make_number (charpos),
12691 Qmenu_item, f->current_tool_bar_string);
12692 if (! INTEGERP (prop))
12693 return false;
12694 *prop_idx = XINT (prop);
12695 return true;
12699 /* Get information about the tool-bar item at position X/Y on frame F.
12700 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12701 the current matrix of the tool-bar window of F, or NULL if not
12702 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12703 item in F->tool_bar_items. Value is
12705 -1 if X/Y is not on a tool-bar item
12706 0 if X/Y is on the same item that was highlighted before.
12707 1 otherwise. */
12709 static int
12710 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12711 int *hpos, int *vpos, int *prop_idx)
12713 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12714 struct window *w = XWINDOW (f->tool_bar_window);
12715 int area;
12717 /* Find the glyph under X/Y. */
12718 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12719 if (*glyph == NULL)
12720 return -1;
12722 /* Get the start of this tool-bar item's properties in
12723 f->tool_bar_items. */
12724 if (!tool_bar_item_info (f, *glyph, prop_idx))
12725 return -1;
12727 /* Is mouse on the highlighted item? */
12728 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12729 && *vpos >= hlinfo->mouse_face_beg_row
12730 && *vpos <= hlinfo->mouse_face_end_row
12731 && (*vpos > hlinfo->mouse_face_beg_row
12732 || *hpos >= hlinfo->mouse_face_beg_col)
12733 && (*vpos < hlinfo->mouse_face_end_row
12734 || *hpos < hlinfo->mouse_face_end_col
12735 || hlinfo->mouse_face_past_end))
12736 return 0;
12738 return 1;
12742 /* EXPORT:
12743 Handle mouse button event on the tool-bar of frame F, at
12744 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12745 false for button release. MODIFIERS is event modifiers for button
12746 release. */
12748 void
12749 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12750 int modifiers)
12752 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12753 struct window *w = XWINDOW (f->tool_bar_window);
12754 int hpos, vpos, prop_idx;
12755 struct glyph *glyph;
12756 Lisp_Object enabled_p;
12757 int ts;
12759 /* If not on the highlighted tool-bar item, and mouse-highlight is
12760 non-nil, return. This is so we generate the tool-bar button
12761 click only when the mouse button is released on the same item as
12762 where it was pressed. However, when mouse-highlight is disabled,
12763 generate the click when the button is released regardless of the
12764 highlight, since tool-bar items are not highlighted in that
12765 case. */
12766 frame_to_window_pixel_xy (w, &x, &y);
12767 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12768 if (ts == -1
12769 || (ts != 0 && !NILP (Vmouse_highlight)))
12770 return;
12772 /* When mouse-highlight is off, generate the click for the item
12773 where the button was pressed, disregarding where it was
12774 released. */
12775 if (NILP (Vmouse_highlight) && !down_p)
12776 prop_idx = f->last_tool_bar_item;
12778 /* If item is disabled, do nothing. */
12779 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12780 if (NILP (enabled_p))
12781 return;
12783 if (down_p)
12785 /* Show item in pressed state. */
12786 if (!NILP (Vmouse_highlight))
12787 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12788 f->last_tool_bar_item = prop_idx;
12790 else
12792 Lisp_Object key, frame;
12793 struct input_event event;
12794 EVENT_INIT (event);
12796 /* Show item in released state. */
12797 if (!NILP (Vmouse_highlight))
12798 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12800 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12802 XSETFRAME (frame, f);
12803 event.kind = TOOL_BAR_EVENT;
12804 event.frame_or_window = frame;
12805 event.arg = frame;
12806 kbd_buffer_store_event (&event);
12808 event.kind = TOOL_BAR_EVENT;
12809 event.frame_or_window = frame;
12810 event.arg = key;
12811 event.modifiers = modifiers;
12812 kbd_buffer_store_event (&event);
12813 f->last_tool_bar_item = -1;
12818 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12819 tool-bar window-relative coordinates X/Y. Called from
12820 note_mouse_highlight. */
12822 static void
12823 note_tool_bar_highlight (struct frame *f, int x, int y)
12825 Lisp_Object window = f->tool_bar_window;
12826 struct window *w = XWINDOW (window);
12827 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12828 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12829 int hpos, vpos;
12830 struct glyph *glyph;
12831 struct glyph_row *row;
12832 int i;
12833 Lisp_Object enabled_p;
12834 int prop_idx;
12835 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12836 bool mouse_down_p;
12837 int rc;
12839 /* Function note_mouse_highlight is called with negative X/Y
12840 values when mouse moves outside of the frame. */
12841 if (x <= 0 || y <= 0)
12843 clear_mouse_face (hlinfo);
12844 return;
12847 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12848 if (rc < 0)
12850 /* Not on tool-bar item. */
12851 clear_mouse_face (hlinfo);
12852 return;
12854 else if (rc == 0)
12855 /* On same tool-bar item as before. */
12856 goto set_help_echo;
12858 clear_mouse_face (hlinfo);
12860 /* Mouse is down, but on different tool-bar item? */
12861 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12862 && f == dpyinfo->last_mouse_frame);
12864 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12865 return;
12867 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12869 /* If tool-bar item is not enabled, don't highlight it. */
12870 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12871 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12873 /* Compute the x-position of the glyph. In front and past the
12874 image is a space. We include this in the highlighted area. */
12875 row = MATRIX_ROW (w->current_matrix, vpos);
12876 for (i = x = 0; i < hpos; ++i)
12877 x += row->glyphs[TEXT_AREA][i].pixel_width;
12879 /* Record this as the current active region. */
12880 hlinfo->mouse_face_beg_col = hpos;
12881 hlinfo->mouse_face_beg_row = vpos;
12882 hlinfo->mouse_face_beg_x = x;
12883 hlinfo->mouse_face_past_end = false;
12885 hlinfo->mouse_face_end_col = hpos + 1;
12886 hlinfo->mouse_face_end_row = vpos;
12887 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12888 hlinfo->mouse_face_window = window;
12889 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12891 /* Display it as active. */
12892 show_mouse_face (hlinfo, draw);
12895 set_help_echo:
12897 /* Set help_echo_string to a help string to display for this tool-bar item.
12898 XTread_socket does the rest. */
12899 help_echo_object = help_echo_window = Qnil;
12900 help_echo_pos = -1;
12901 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12902 if (NILP (help_echo_string))
12903 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12906 #endif /* !USE_GTK && !HAVE_NS */
12908 #endif /* HAVE_WINDOW_SYSTEM */
12912 /************************************************************************
12913 Horizontal scrolling
12914 ************************************************************************/
12916 /* For all leaf windows in the window tree rooted at WINDOW, set their
12917 hscroll value so that PT is (i) visible in the window, and (ii) so
12918 that it is not within a certain margin at the window's left and
12919 right border. Value is true if any window's hscroll has been
12920 changed. */
12922 static bool
12923 hscroll_window_tree (Lisp_Object window)
12925 bool hscrolled_p = false;
12926 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12927 int hscroll_step_abs = 0;
12928 double hscroll_step_rel = 0;
12930 if (hscroll_relative_p)
12932 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12933 if (hscroll_step_rel < 0)
12935 hscroll_relative_p = false;
12936 hscroll_step_abs = 0;
12939 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12941 hscroll_step_abs = XINT (Vhscroll_step);
12942 if (hscroll_step_abs < 0)
12943 hscroll_step_abs = 0;
12945 else
12946 hscroll_step_abs = 0;
12948 while (WINDOWP (window))
12950 struct window *w = XWINDOW (window);
12952 if (WINDOWP (w->contents))
12953 hscrolled_p |= hscroll_window_tree (w->contents);
12954 else if (w->cursor.vpos >= 0)
12956 int h_margin;
12957 int text_area_width;
12958 struct glyph_row *cursor_row;
12959 struct glyph_row *bottom_row;
12961 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12962 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12963 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12964 else
12965 cursor_row = bottom_row - 1;
12967 if (!cursor_row->enabled_p)
12969 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12970 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12971 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12972 else
12973 cursor_row = bottom_row - 1;
12975 bool row_r2l_p = cursor_row->reversed_p;
12977 text_area_width = window_box_width (w, TEXT_AREA);
12979 /* Scroll when cursor is inside this scroll margin. */
12980 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12982 /* If the position of this window's point has explicitly
12983 changed, no more suspend auto hscrolling. */
12984 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12985 w->suspend_auto_hscroll = false;
12987 /* Remember window point. */
12988 Fset_marker (w->old_pointm,
12989 ((w == XWINDOW (selected_window))
12990 ? make_number (BUF_PT (XBUFFER (w->contents)))
12991 : Fmarker_position (w->pointm)),
12992 w->contents);
12994 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12995 && !w->suspend_auto_hscroll
12996 /* In some pathological cases, like restoring a window
12997 configuration into a frame that is much smaller than
12998 the one from which the configuration was saved, we
12999 get glyph rows whose start and end have zero buffer
13000 positions, which we cannot handle below. Just skip
13001 such windows. */
13002 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13003 /* For left-to-right rows, hscroll when cursor is either
13004 (i) inside the right hscroll margin, or (ii) if it is
13005 inside the left margin and the window is already
13006 hscrolled. */
13007 && ((!row_r2l_p
13008 && ((w->hscroll && w->cursor.x <= h_margin)
13009 || (cursor_row->enabled_p
13010 && cursor_row->truncated_on_right_p
13011 && (w->cursor.x >= text_area_width - h_margin))))
13012 /* For right-to-left rows, the logic is similar,
13013 except that rules for scrolling to left and right
13014 are reversed. E.g., if cursor.x <= h_margin, we
13015 need to hscroll "to the right" unconditionally,
13016 and that will scroll the screen to the left so as
13017 to reveal the next portion of the row. */
13018 || (row_r2l_p
13019 && ((cursor_row->enabled_p
13020 /* FIXME: It is confusing to set the
13021 truncated_on_right_p flag when R2L rows
13022 are actually truncated on the left. */
13023 && cursor_row->truncated_on_right_p
13024 && w->cursor.x <= h_margin)
13025 || (w->hscroll
13026 && (w->cursor.x >= text_area_width - h_margin))))))
13028 struct it it;
13029 ptrdiff_t hscroll;
13030 struct buffer *saved_current_buffer;
13031 ptrdiff_t pt;
13032 int wanted_x;
13034 /* Find point in a display of infinite width. */
13035 saved_current_buffer = current_buffer;
13036 current_buffer = XBUFFER (w->contents);
13038 if (w == XWINDOW (selected_window))
13039 pt = PT;
13040 else
13041 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13043 /* Move iterator to pt starting at cursor_row->start in
13044 a line with infinite width. */
13045 init_to_row_start (&it, w, cursor_row);
13046 it.last_visible_x = INFINITY;
13047 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13048 current_buffer = saved_current_buffer;
13050 /* Position cursor in window. */
13051 if (!hscroll_relative_p && hscroll_step_abs == 0)
13052 hscroll = max (0, (it.current_x
13053 - (ITERATOR_AT_END_OF_LINE_P (&it)
13054 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13055 : (text_area_width / 2))))
13056 / FRAME_COLUMN_WIDTH (it.f);
13057 else if ((!row_r2l_p
13058 && w->cursor.x >= text_area_width - h_margin)
13059 || (row_r2l_p && w->cursor.x <= h_margin))
13061 if (hscroll_relative_p)
13062 wanted_x = text_area_width * (1 - hscroll_step_rel)
13063 - h_margin;
13064 else
13065 wanted_x = text_area_width
13066 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13067 - h_margin;
13068 hscroll
13069 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13071 else
13073 if (hscroll_relative_p)
13074 wanted_x = text_area_width * hscroll_step_rel
13075 + h_margin;
13076 else
13077 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13078 + h_margin;
13079 hscroll
13080 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13082 hscroll = max (hscroll, w->min_hscroll);
13084 /* Don't prevent redisplay optimizations if hscroll
13085 hasn't changed, as it will unnecessarily slow down
13086 redisplay. */
13087 if (w->hscroll != hscroll)
13089 struct buffer *b = XBUFFER (w->contents);
13090 b->prevent_redisplay_optimizations_p = true;
13091 w->hscroll = hscroll;
13092 hscrolled_p = true;
13097 window = w->next;
13100 /* Value is true if hscroll of any leaf window has been changed. */
13101 return hscrolled_p;
13105 /* Set hscroll so that cursor is visible and not inside horizontal
13106 scroll margins for all windows in the tree rooted at WINDOW. See
13107 also hscroll_window_tree above. Value is true if any window's
13108 hscroll has been changed. If it has, desired matrices on the frame
13109 of WINDOW are cleared. */
13111 static bool
13112 hscroll_windows (Lisp_Object window)
13114 bool hscrolled_p = hscroll_window_tree (window);
13115 if (hscrolled_p)
13116 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13117 return hscrolled_p;
13122 /************************************************************************
13123 Redisplay
13124 ************************************************************************/
13126 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13127 This is sometimes handy to have in a debugger session. */
13129 #ifdef GLYPH_DEBUG
13131 /* First and last unchanged row for try_window_id. */
13133 static int debug_first_unchanged_at_end_vpos;
13134 static int debug_last_unchanged_at_beg_vpos;
13136 /* Delta vpos and y. */
13138 static int debug_dvpos, debug_dy;
13140 /* Delta in characters and bytes for try_window_id. */
13142 static ptrdiff_t debug_delta, debug_delta_bytes;
13144 /* Values of window_end_pos and window_end_vpos at the end of
13145 try_window_id. */
13147 static ptrdiff_t debug_end_vpos;
13149 /* Append a string to W->desired_matrix->method. FMT is a printf
13150 format string. If trace_redisplay_p is true also printf the
13151 resulting string to stderr. */
13153 static void debug_method_add (struct window *, char const *, ...)
13154 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13156 static void
13157 debug_method_add (struct window *w, char const *fmt, ...)
13159 void *ptr = w;
13160 char *method = w->desired_matrix->method;
13161 int len = strlen (method);
13162 int size = sizeof w->desired_matrix->method;
13163 int remaining = size - len - 1;
13164 va_list ap;
13166 if (len && remaining)
13168 method[len] = '|';
13169 --remaining, ++len;
13172 va_start (ap, fmt);
13173 vsnprintf (method + len, remaining + 1, fmt, ap);
13174 va_end (ap);
13176 if (trace_redisplay_p)
13177 fprintf (stderr, "%p (%s): %s\n",
13178 ptr,
13179 ((BUFFERP (w->contents)
13180 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13181 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13182 : "no buffer"),
13183 method + len);
13186 #endif /* GLYPH_DEBUG */
13189 /* Value is true if all changes in window W, which displays
13190 current_buffer, are in the text between START and END. START is a
13191 buffer position, END is given as a distance from Z. Used in
13192 redisplay_internal for display optimization. */
13194 static bool
13195 text_outside_line_unchanged_p (struct window *w,
13196 ptrdiff_t start, ptrdiff_t end)
13198 bool unchanged_p = true;
13200 /* If text or overlays have changed, see where. */
13201 if (window_outdated (w))
13203 /* Gap in the line? */
13204 if (GPT < start || Z - GPT < end)
13205 unchanged_p = false;
13207 /* Changes start in front of the line, or end after it? */
13208 if (unchanged_p
13209 && (BEG_UNCHANGED < start - 1
13210 || END_UNCHANGED < end))
13211 unchanged_p = false;
13213 /* If selective display, can't optimize if changes start at the
13214 beginning of the line. */
13215 if (unchanged_p
13216 && INTEGERP (BVAR (current_buffer, selective_display))
13217 && XINT (BVAR (current_buffer, selective_display)) > 0
13218 && (BEG_UNCHANGED < start || GPT <= start))
13219 unchanged_p = false;
13221 /* If there are overlays at the start or end of the line, these
13222 may have overlay strings with newlines in them. A change at
13223 START, for instance, may actually concern the display of such
13224 overlay strings as well, and they are displayed on different
13225 lines. So, quickly rule out this case. (For the future, it
13226 might be desirable to implement something more telling than
13227 just BEG/END_UNCHANGED.) */
13228 if (unchanged_p)
13230 if (BEG + BEG_UNCHANGED == start
13231 && overlay_touches_p (start))
13232 unchanged_p = false;
13233 if (END_UNCHANGED == end
13234 && overlay_touches_p (Z - end))
13235 unchanged_p = false;
13238 /* Under bidi reordering, adding or deleting a character in the
13239 beginning of a paragraph, before the first strong directional
13240 character, can change the base direction of the paragraph (unless
13241 the buffer specifies a fixed paragraph direction), which will
13242 require redisplaying the whole paragraph. It might be worthwhile
13243 to find the paragraph limits and widen the range of redisplayed
13244 lines to that, but for now just give up this optimization. */
13245 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13246 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13247 unchanged_p = false;
13250 return unchanged_p;
13254 /* Do a frame update, taking possible shortcuts into account. This is
13255 the main external entry point for redisplay.
13257 If the last redisplay displayed an echo area message and that message
13258 is no longer requested, we clear the echo area or bring back the
13259 mini-buffer if that is in use. */
13261 void
13262 redisplay (void)
13264 redisplay_internal ();
13268 static Lisp_Object
13269 overlay_arrow_string_or_property (Lisp_Object var)
13271 Lisp_Object val;
13273 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13274 return val;
13276 return Voverlay_arrow_string;
13279 /* Return true if there are any overlay-arrows in current_buffer. */
13280 static bool
13281 overlay_arrow_in_current_buffer_p (void)
13283 Lisp_Object vlist;
13285 for (vlist = Voverlay_arrow_variable_list;
13286 CONSP (vlist);
13287 vlist = XCDR (vlist))
13289 Lisp_Object var = XCAR (vlist);
13290 Lisp_Object val;
13292 if (!SYMBOLP (var))
13293 continue;
13294 val = find_symbol_value (var);
13295 if (MARKERP (val)
13296 && current_buffer == XMARKER (val)->buffer)
13297 return true;
13299 return false;
13303 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13304 has changed. */
13306 static bool
13307 overlay_arrows_changed_p (void)
13309 Lisp_Object vlist;
13311 for (vlist = Voverlay_arrow_variable_list;
13312 CONSP (vlist);
13313 vlist = XCDR (vlist))
13315 Lisp_Object var = XCAR (vlist);
13316 Lisp_Object val, pstr;
13318 if (!SYMBOLP (var))
13319 continue;
13320 val = find_symbol_value (var);
13321 if (!MARKERP (val))
13322 continue;
13323 if (! EQ (COERCE_MARKER (val),
13324 Fget (var, Qlast_arrow_position))
13325 || ! (pstr = overlay_arrow_string_or_property (var),
13326 EQ (pstr, Fget (var, Qlast_arrow_string))))
13327 return true;
13329 return false;
13332 /* Mark overlay arrows to be updated on next redisplay. */
13334 static void
13335 update_overlay_arrows (int up_to_date)
13337 Lisp_Object vlist;
13339 for (vlist = Voverlay_arrow_variable_list;
13340 CONSP (vlist);
13341 vlist = XCDR (vlist))
13343 Lisp_Object var = XCAR (vlist);
13345 if (!SYMBOLP (var))
13346 continue;
13348 if (up_to_date > 0)
13350 Lisp_Object val = find_symbol_value (var);
13351 Fput (var, Qlast_arrow_position,
13352 COERCE_MARKER (val));
13353 Fput (var, Qlast_arrow_string,
13354 overlay_arrow_string_or_property (var));
13356 else if (up_to_date < 0
13357 || !NILP (Fget (var, Qlast_arrow_position)))
13359 Fput (var, Qlast_arrow_position, Qt);
13360 Fput (var, Qlast_arrow_string, Qt);
13366 /* Return overlay arrow string to display at row.
13367 Return integer (bitmap number) for arrow bitmap in left fringe.
13368 Return nil if no overlay arrow. */
13370 static Lisp_Object
13371 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13373 Lisp_Object vlist;
13375 for (vlist = Voverlay_arrow_variable_list;
13376 CONSP (vlist);
13377 vlist = XCDR (vlist))
13379 Lisp_Object var = XCAR (vlist);
13380 Lisp_Object val;
13382 if (!SYMBOLP (var))
13383 continue;
13385 val = find_symbol_value (var);
13387 if (MARKERP (val)
13388 && current_buffer == XMARKER (val)->buffer
13389 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13391 if (FRAME_WINDOW_P (it->f)
13392 /* FIXME: if ROW->reversed_p is set, this should test
13393 the right fringe, not the left one. */
13394 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13396 #ifdef HAVE_WINDOW_SYSTEM
13397 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13399 int fringe_bitmap = lookup_fringe_bitmap (val);
13400 if (fringe_bitmap != 0)
13401 return make_number (fringe_bitmap);
13403 #endif
13404 return make_number (-1); /* Use default arrow bitmap. */
13406 return overlay_arrow_string_or_property (var);
13410 return Qnil;
13413 /* Return true if point moved out of or into a composition. Otherwise
13414 return false. PREV_BUF and PREV_PT are the last point buffer and
13415 position. BUF and PT are the current point buffer and position. */
13417 static bool
13418 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13419 struct buffer *buf, ptrdiff_t pt)
13421 ptrdiff_t start, end;
13422 Lisp_Object prop;
13423 Lisp_Object buffer;
13425 XSETBUFFER (buffer, buf);
13426 /* Check a composition at the last point if point moved within the
13427 same buffer. */
13428 if (prev_buf == buf)
13430 if (prev_pt == pt)
13431 /* Point didn't move. */
13432 return false;
13434 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13435 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13436 && composition_valid_p (start, end, prop)
13437 && start < prev_pt && end > prev_pt)
13438 /* The last point was within the composition. Return true iff
13439 point moved out of the composition. */
13440 return (pt <= start || pt >= end);
13443 /* Check a composition at the current point. */
13444 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13445 && find_composition (pt, -1, &start, &end, &prop, buffer)
13446 && composition_valid_p (start, end, prop)
13447 && start < pt && end > pt);
13450 /* Reconsider the clip changes of buffer which is displayed in W. */
13452 static void
13453 reconsider_clip_changes (struct window *w)
13455 struct buffer *b = XBUFFER (w->contents);
13457 if (b->clip_changed
13458 && w->window_end_valid
13459 && w->current_matrix->buffer == b
13460 && w->current_matrix->zv == BUF_ZV (b)
13461 && w->current_matrix->begv == BUF_BEGV (b))
13462 b->clip_changed = false;
13464 /* If display wasn't paused, and W is not a tool bar window, see if
13465 point has been moved into or out of a composition. In that case,
13466 set b->clip_changed to force updating the screen. If
13467 b->clip_changed has already been set, skip this check. */
13468 if (!b->clip_changed && w->window_end_valid)
13470 ptrdiff_t pt = (w == XWINDOW (selected_window)
13471 ? PT : marker_position (w->pointm));
13473 if ((w->current_matrix->buffer != b || pt != w->last_point)
13474 && check_point_in_composition (w->current_matrix->buffer,
13475 w->last_point, b, pt))
13476 b->clip_changed = true;
13480 static void
13481 propagate_buffer_redisplay (void)
13482 { /* Resetting b->text->redisplay is problematic!
13483 We can't just reset it in the case that some window that displays
13484 it has not been redisplayed; and such a window can stay
13485 unredisplayed for a long time if it's currently invisible.
13486 But we do want to reset it at the end of redisplay otherwise
13487 its displayed windows will keep being redisplayed over and over
13488 again.
13489 So we copy all b->text->redisplay flags up to their windows here,
13490 such that mark_window_display_accurate can safely reset
13491 b->text->redisplay. */
13492 Lisp_Object ws = window_list ();
13493 for (; CONSP (ws); ws = XCDR (ws))
13495 struct window *thisw = XWINDOW (XCAR (ws));
13496 struct buffer *thisb = XBUFFER (thisw->contents);
13497 if (thisb->text->redisplay)
13498 thisw->redisplay = true;
13502 #define STOP_POLLING \
13503 do { if (! polling_stopped_here) stop_polling (); \
13504 polling_stopped_here = true; } while (false)
13506 #define RESUME_POLLING \
13507 do { if (polling_stopped_here) start_polling (); \
13508 polling_stopped_here = false; } while (false)
13511 /* Perhaps in the future avoid recentering windows if it
13512 is not necessary; currently that causes some problems. */
13514 static void
13515 redisplay_internal (void)
13517 struct window *w = XWINDOW (selected_window);
13518 struct window *sw;
13519 struct frame *fr;
13520 bool pending;
13521 bool must_finish = false, match_p;
13522 struct text_pos tlbufpos, tlendpos;
13523 int number_of_visible_frames;
13524 ptrdiff_t count;
13525 struct frame *sf;
13526 bool polling_stopped_here = false;
13527 Lisp_Object tail, frame;
13529 /* True means redisplay has to consider all windows on all
13530 frames. False, only selected_window is considered. */
13531 bool consider_all_windows_p;
13533 /* True means redisplay has to redisplay the miniwindow. */
13534 bool update_miniwindow_p = false;
13536 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13538 /* No redisplay if running in batch mode or frame is not yet fully
13539 initialized, or redisplay is explicitly turned off by setting
13540 Vinhibit_redisplay. */
13541 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13542 || !NILP (Vinhibit_redisplay))
13543 return;
13545 /* Don't examine these until after testing Vinhibit_redisplay.
13546 When Emacs is shutting down, perhaps because its connection to
13547 X has dropped, we should not look at them at all. */
13548 fr = XFRAME (w->frame);
13549 sf = SELECTED_FRAME ();
13551 if (!fr->glyphs_initialized_p)
13552 return;
13554 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13555 if (popup_activated ())
13556 return;
13557 #endif
13559 /* I don't think this happens but let's be paranoid. */
13560 if (redisplaying_p)
13561 return;
13563 /* Record a function that clears redisplaying_p
13564 when we leave this function. */
13565 count = SPECPDL_INDEX ();
13566 record_unwind_protect_void (unwind_redisplay);
13567 redisplaying_p = true;
13568 specbind (Qinhibit_free_realized_faces, Qnil);
13570 /* Record this function, so it appears on the profiler's backtraces. */
13571 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13573 FOR_EACH_FRAME (tail, frame)
13574 XFRAME (frame)->already_hscrolled_p = false;
13576 retry:
13577 /* Remember the currently selected window. */
13578 sw = w;
13580 pending = false;
13581 forget_escape_and_glyphless_faces ();
13583 inhibit_free_realized_faces = false;
13585 /* If face_change, init_iterator will free all realized faces, which
13586 includes the faces referenced from current matrices. So, we
13587 can't reuse current matrices in this case. */
13588 if (face_change)
13589 windows_or_buffers_changed = 47;
13591 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13592 && FRAME_TTY (sf)->previous_frame != sf)
13594 /* Since frames on a single ASCII terminal share the same
13595 display area, displaying a different frame means redisplay
13596 the whole thing. */
13597 SET_FRAME_GARBAGED (sf);
13598 #ifndef DOS_NT
13599 set_tty_color_mode (FRAME_TTY (sf), sf);
13600 #endif
13601 FRAME_TTY (sf)->previous_frame = sf;
13604 /* Set the visible flags for all frames. Do this before checking for
13605 resized or garbaged frames; they want to know if their frames are
13606 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13607 number_of_visible_frames = 0;
13609 FOR_EACH_FRAME (tail, frame)
13611 struct frame *f = XFRAME (frame);
13613 if (FRAME_VISIBLE_P (f))
13615 ++number_of_visible_frames;
13616 /* Adjust matrices for visible frames only. */
13617 if (f->fonts_changed)
13619 adjust_frame_glyphs (f);
13620 /* Disable all redisplay optimizations for this frame.
13621 This is because adjust_frame_glyphs resets the
13622 enabled_p flag for all glyph rows of all windows, so
13623 many optimizations will fail anyway, and some might
13624 fail to test that flag and do bogus things as
13625 result. */
13626 SET_FRAME_GARBAGED (f);
13627 f->fonts_changed = false;
13629 /* If cursor type has been changed on the frame
13630 other than selected, consider all frames. */
13631 if (f != sf && f->cursor_type_changed)
13632 fset_redisplay (f);
13634 clear_desired_matrices (f);
13637 /* Notice any pending interrupt request to change frame size. */
13638 do_pending_window_change (true);
13640 /* do_pending_window_change could change the selected_window due to
13641 frame resizing which makes the selected window too small. */
13642 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13643 sw = w;
13645 /* Clear frames marked as garbaged. */
13646 clear_garbaged_frames ();
13648 /* Build menubar and tool-bar items. */
13649 if (NILP (Vmemory_full))
13650 prepare_menu_bars ();
13652 reconsider_clip_changes (w);
13654 /* In most cases selected window displays current buffer. */
13655 match_p = XBUFFER (w->contents) == current_buffer;
13656 if (match_p)
13658 /* Detect case that we need to write or remove a star in the mode line. */
13659 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13660 w->update_mode_line = true;
13662 if (mode_line_update_needed (w))
13663 w->update_mode_line = true;
13665 /* If reconsider_clip_changes above decided that the narrowing
13666 in the current buffer changed, make sure all other windows
13667 showing that buffer will be redisplayed. */
13668 if (current_buffer->clip_changed)
13669 bset_update_mode_line (current_buffer);
13672 /* Normally the message* functions will have already displayed and
13673 updated the echo area, but the frame may have been trashed, or
13674 the update may have been preempted, so display the echo area
13675 again here. Checking message_cleared_p captures the case that
13676 the echo area should be cleared. */
13677 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13678 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13679 || (message_cleared_p
13680 && minibuf_level == 0
13681 /* If the mini-window is currently selected, this means the
13682 echo-area doesn't show through. */
13683 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13685 echo_area_display (false);
13687 /* If echo_area_display resizes the mini-window, the redisplay and
13688 window_sizes_changed flags of the selected frame are set, but
13689 it's too late for the hooks in window-size-change-functions,
13690 which have been examined already in prepare_menu_bars. So in
13691 that case we call the hooks here only for the selected frame. */
13692 if (sf->redisplay)
13694 ptrdiff_t count1 = SPECPDL_INDEX ();
13696 record_unwind_save_match_data ();
13697 run_window_size_change_functions (selected_frame);
13698 unbind_to (count1, Qnil);
13701 if (message_cleared_p)
13702 update_miniwindow_p = true;
13704 must_finish = true;
13706 /* If we don't display the current message, don't clear the
13707 message_cleared_p flag, because, if we did, we wouldn't clear
13708 the echo area in the next redisplay which doesn't preserve
13709 the echo area. */
13710 if (!display_last_displayed_message_p)
13711 message_cleared_p = false;
13713 else if (EQ (selected_window, minibuf_window)
13714 && (current_buffer->clip_changed || window_outdated (w))
13715 && resize_mini_window (w, false))
13717 if (sf->redisplay)
13719 ptrdiff_t count1 = SPECPDL_INDEX ();
13721 record_unwind_save_match_data ();
13722 run_window_size_change_functions (selected_frame);
13723 unbind_to (count1, Qnil);
13726 /* Resized active mini-window to fit the size of what it is
13727 showing if its contents might have changed. */
13728 must_finish = true;
13730 /* If window configuration was changed, frames may have been
13731 marked garbaged. Clear them or we will experience
13732 surprises wrt scrolling. */
13733 clear_garbaged_frames ();
13736 if (windows_or_buffers_changed && !update_mode_lines)
13737 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13738 only the windows's contents needs to be refreshed, or whether the
13739 mode-lines also need a refresh. */
13740 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13741 ? REDISPLAY_SOME : 32);
13743 /* If specs for an arrow have changed, do thorough redisplay
13744 to ensure we remove any arrow that should no longer exist. */
13745 if (overlay_arrows_changed_p ())
13746 /* Apparently, this is the only case where we update other windows,
13747 without updating other mode-lines. */
13748 windows_or_buffers_changed = 49;
13750 consider_all_windows_p = (update_mode_lines
13751 || windows_or_buffers_changed);
13753 #define AINC(a,i) \
13755 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13756 if (INTEGERP (entry)) \
13757 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13760 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13761 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13763 /* Optimize the case that only the line containing the cursor in the
13764 selected window has changed. Variables starting with this_ are
13765 set in display_line and record information about the line
13766 containing the cursor. */
13767 tlbufpos = this_line_start_pos;
13768 tlendpos = this_line_end_pos;
13769 if (!consider_all_windows_p
13770 && CHARPOS (tlbufpos) > 0
13771 && !w->update_mode_line
13772 && !current_buffer->clip_changed
13773 && !current_buffer->prevent_redisplay_optimizations_p
13774 && FRAME_VISIBLE_P (XFRAME (w->frame))
13775 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13776 && !XFRAME (w->frame)->cursor_type_changed
13777 && !XFRAME (w->frame)->face_change
13778 /* Make sure recorded data applies to current buffer, etc. */
13779 && this_line_buffer == current_buffer
13780 && match_p
13781 && !w->force_start
13782 && !w->optional_new_start
13783 /* Point must be on the line that we have info recorded about. */
13784 && PT >= CHARPOS (tlbufpos)
13785 && PT <= Z - CHARPOS (tlendpos)
13786 /* All text outside that line, including its final newline,
13787 must be unchanged. */
13788 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13789 CHARPOS (tlendpos)))
13791 if (CHARPOS (tlbufpos) > BEGV
13792 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13793 && (CHARPOS (tlbufpos) == ZV
13794 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13795 /* Former continuation line has disappeared by becoming empty. */
13796 goto cancel;
13797 else if (window_outdated (w) || MINI_WINDOW_P (w))
13799 /* We have to handle the case of continuation around a
13800 wide-column character (see the comment in indent.c around
13801 line 1340).
13803 For instance, in the following case:
13805 -------- Insert --------
13806 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13807 J_I_ ==> J_I_ `^^' are cursors.
13808 ^^ ^^
13809 -------- --------
13811 As we have to redraw the line above, we cannot use this
13812 optimization. */
13814 struct it it;
13815 int line_height_before = this_line_pixel_height;
13817 /* Note that start_display will handle the case that the
13818 line starting at tlbufpos is a continuation line. */
13819 start_display (&it, w, tlbufpos);
13821 /* Implementation note: It this still necessary? */
13822 if (it.current_x != this_line_start_x)
13823 goto cancel;
13825 TRACE ((stderr, "trying display optimization 1\n"));
13826 w->cursor.vpos = -1;
13827 overlay_arrow_seen = false;
13828 it.vpos = this_line_vpos;
13829 it.current_y = this_line_y;
13830 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13831 display_line (&it);
13833 /* If line contains point, is not continued,
13834 and ends at same distance from eob as before, we win. */
13835 if (w->cursor.vpos >= 0
13836 /* Line is not continued, otherwise this_line_start_pos
13837 would have been set to 0 in display_line. */
13838 && CHARPOS (this_line_start_pos)
13839 /* Line ends as before. */
13840 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13841 /* Line has same height as before. Otherwise other lines
13842 would have to be shifted up or down. */
13843 && this_line_pixel_height == line_height_before)
13845 /* If this is not the window's last line, we must adjust
13846 the charstarts of the lines below. */
13847 if (it.current_y < it.last_visible_y)
13849 struct glyph_row *row
13850 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13851 ptrdiff_t delta, delta_bytes;
13853 /* We used to distinguish between two cases here,
13854 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13855 when the line ends in a newline or the end of the
13856 buffer's accessible portion. But both cases did
13857 the same, so they were collapsed. */
13858 delta = (Z
13859 - CHARPOS (tlendpos)
13860 - MATRIX_ROW_START_CHARPOS (row));
13861 delta_bytes = (Z_BYTE
13862 - BYTEPOS (tlendpos)
13863 - MATRIX_ROW_START_BYTEPOS (row));
13865 increment_matrix_positions (w->current_matrix,
13866 this_line_vpos + 1,
13867 w->current_matrix->nrows,
13868 delta, delta_bytes);
13871 /* If this row displays text now but previously didn't,
13872 or vice versa, w->window_end_vpos may have to be
13873 adjusted. */
13874 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13876 if (w->window_end_vpos < this_line_vpos)
13877 w->window_end_vpos = this_line_vpos;
13879 else if (w->window_end_vpos == this_line_vpos
13880 && this_line_vpos > 0)
13881 w->window_end_vpos = this_line_vpos - 1;
13882 w->window_end_valid = false;
13884 /* Update hint: No need to try to scroll in update_window. */
13885 w->desired_matrix->no_scrolling_p = true;
13887 #ifdef GLYPH_DEBUG
13888 *w->desired_matrix->method = 0;
13889 debug_method_add (w, "optimization 1");
13890 #endif
13891 #ifdef HAVE_WINDOW_SYSTEM
13892 update_window_fringes (w, false);
13893 #endif
13894 goto update;
13896 else
13897 goto cancel;
13899 else if (/* Cursor position hasn't changed. */
13900 PT == w->last_point
13901 /* Make sure the cursor was last displayed
13902 in this window. Otherwise we have to reposition it. */
13904 /* PXW: Must be converted to pixels, probably. */
13905 && 0 <= w->cursor.vpos
13906 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13908 if (!must_finish)
13910 do_pending_window_change (true);
13911 /* If selected_window changed, redisplay again. */
13912 if (WINDOWP (selected_window)
13913 && (w = XWINDOW (selected_window)) != sw)
13914 goto retry;
13916 /* We used to always goto end_of_redisplay here, but this
13917 isn't enough if we have a blinking cursor. */
13918 if (w->cursor_off_p == w->last_cursor_off_p)
13919 goto end_of_redisplay;
13921 goto update;
13923 /* If highlighting the region, or if the cursor is in the echo area,
13924 then we can't just move the cursor. */
13925 else if (NILP (Vshow_trailing_whitespace)
13926 && !cursor_in_echo_area)
13928 struct it it;
13929 struct glyph_row *row;
13931 /* Skip from tlbufpos to PT and see where it is. Note that
13932 PT may be in invisible text. If so, we will end at the
13933 next visible position. */
13934 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13935 NULL, DEFAULT_FACE_ID);
13936 it.current_x = this_line_start_x;
13937 it.current_y = this_line_y;
13938 it.vpos = this_line_vpos;
13940 /* The call to move_it_to stops in front of PT, but
13941 moves over before-strings. */
13942 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13944 if (it.vpos == this_line_vpos
13945 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13946 row->enabled_p))
13948 eassert (this_line_vpos == it.vpos);
13949 eassert (this_line_y == it.current_y);
13950 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13951 if (cursor_row_fully_visible_p (w, false, true))
13953 #ifdef GLYPH_DEBUG
13954 *w->desired_matrix->method = 0;
13955 debug_method_add (w, "optimization 3");
13956 #endif
13957 goto update;
13959 else
13960 goto cancel;
13962 else
13963 goto cancel;
13966 cancel:
13967 /* Text changed drastically or point moved off of line. */
13968 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13971 CHARPOS (this_line_start_pos) = 0;
13972 ++clear_face_cache_count;
13973 #ifdef HAVE_WINDOW_SYSTEM
13974 ++clear_image_cache_count;
13975 #endif
13977 /* Build desired matrices, and update the display. If
13978 consider_all_windows_p, do it for all windows on all frames that
13979 require redisplay, as specified by their 'redisplay' flag.
13980 Otherwise do it for selected_window, only. */
13982 if (consider_all_windows_p)
13984 FOR_EACH_FRAME (tail, frame)
13985 XFRAME (frame)->updated_p = false;
13987 propagate_buffer_redisplay ();
13989 FOR_EACH_FRAME (tail, frame)
13991 struct frame *f = XFRAME (frame);
13993 /* We don't have to do anything for unselected terminal
13994 frames. */
13995 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13996 && !EQ (FRAME_TTY (f)->top_frame, frame))
13997 continue;
13999 retry_frame:
14000 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14002 bool gcscrollbars
14003 /* Only GC scrollbars when we redisplay the whole frame. */
14004 = f->redisplay || !REDISPLAY_SOME_P ();
14005 bool f_redisplay_flag = f->redisplay;
14006 /* Mark all the scroll bars to be removed; we'll redeem
14007 the ones we want when we redisplay their windows. */
14008 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14009 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14011 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14012 redisplay_windows (FRAME_ROOT_WINDOW (f));
14013 /* Remember that the invisible frames need to be redisplayed next
14014 time they're visible. */
14015 else if (!REDISPLAY_SOME_P ())
14016 f->redisplay = true;
14018 /* The X error handler may have deleted that frame. */
14019 if (!FRAME_LIVE_P (f))
14020 continue;
14022 /* Any scroll bars which redisplay_windows should have
14023 nuked should now go away. */
14024 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14025 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14027 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14029 /* If fonts changed on visible frame, display again. */
14030 if (f->fonts_changed)
14032 adjust_frame_glyphs (f);
14033 /* Disable all redisplay optimizations for this
14034 frame. For the reasons, see the comment near
14035 the previous call to adjust_frame_glyphs above. */
14036 SET_FRAME_GARBAGED (f);
14037 f->fonts_changed = false;
14038 goto retry_frame;
14041 /* See if we have to hscroll. */
14042 if (!f->already_hscrolled_p)
14044 f->already_hscrolled_p = true;
14045 if (hscroll_windows (f->root_window))
14046 goto retry_frame;
14049 /* If the frame's redisplay flag was not set before
14050 we went about redisplaying its windows, but it is
14051 set now, that means we employed some redisplay
14052 optimizations inside redisplay_windows, and
14053 bypassed producing some screen lines. But if
14054 f->redisplay is now set, it might mean the old
14055 faces are no longer valid (e.g., if redisplaying
14056 some window called some Lisp which defined a new
14057 face or redefined an existing face), so trying to
14058 use them in update_frame will segfault.
14059 Therefore, we must redisplay this frame. */
14060 if (!f_redisplay_flag && f->redisplay)
14061 goto retry_frame;
14063 /* Prevent various kinds of signals during display
14064 update. stdio is not robust about handling
14065 signals, which can cause an apparent I/O error. */
14066 if (interrupt_input)
14067 unrequest_sigio ();
14068 STOP_POLLING;
14070 pending |= update_frame (f, false, false);
14071 f->cursor_type_changed = false;
14072 f->updated_p = true;
14077 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14079 if (!pending)
14081 /* Do the mark_window_display_accurate after all windows have
14082 been redisplayed because this call resets flags in buffers
14083 which are needed for proper redisplay. */
14084 FOR_EACH_FRAME (tail, frame)
14086 struct frame *f = XFRAME (frame);
14087 if (f->updated_p)
14089 f->redisplay = false;
14090 mark_window_display_accurate (f->root_window, true);
14091 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14092 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14097 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14099 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14100 /* Use list_of_error, not Qerror, so that
14101 we catch only errors and don't run the debugger. */
14102 internal_condition_case_1 (redisplay_window_1, selected_window,
14103 list_of_error,
14104 redisplay_window_error);
14105 if (update_miniwindow_p)
14106 internal_condition_case_1 (redisplay_window_1,
14107 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14108 redisplay_window_error);
14110 /* Compare desired and current matrices, perform output. */
14112 update:
14113 /* If fonts changed, display again. Likewise if redisplay_window_1
14114 above caused some change (e.g., a change in faces) that requires
14115 considering the entire frame again. */
14116 if (sf->fonts_changed || sf->redisplay)
14118 if (sf->redisplay)
14120 /* Set this to force a more thorough redisplay.
14121 Otherwise, we might immediately loop back to the
14122 above "else-if" clause (since all the conditions that
14123 led here might still be true), and we will then
14124 infloop, because the selected-frame's redisplay flag
14125 is not (and cannot be) reset. */
14126 windows_or_buffers_changed = 50;
14128 goto retry;
14131 /* Prevent freeing of realized faces, since desired matrices are
14132 pending that reference the faces we computed and cached. */
14133 inhibit_free_realized_faces = true;
14135 /* Prevent various kinds of signals during display update.
14136 stdio is not robust about handling signals,
14137 which can cause an apparent I/O error. */
14138 if (interrupt_input)
14139 unrequest_sigio ();
14140 STOP_POLLING;
14142 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14144 if (hscroll_windows (selected_window))
14145 goto retry;
14147 XWINDOW (selected_window)->must_be_updated_p = true;
14148 pending = update_frame (sf, false, false);
14149 sf->cursor_type_changed = false;
14152 /* We may have called echo_area_display at the top of this
14153 function. If the echo area is on another frame, that may
14154 have put text on a frame other than the selected one, so the
14155 above call to update_frame would not have caught it. Catch
14156 it here. */
14157 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14158 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14160 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14162 XWINDOW (mini_window)->must_be_updated_p = true;
14163 pending |= update_frame (mini_frame, false, false);
14164 mini_frame->cursor_type_changed = false;
14165 if (!pending && hscroll_windows (mini_window))
14166 goto retry;
14170 /* If display was paused because of pending input, make sure we do a
14171 thorough update the next time. */
14172 if (pending)
14174 /* Prevent the optimization at the beginning of
14175 redisplay_internal that tries a single-line update of the
14176 line containing the cursor in the selected window. */
14177 CHARPOS (this_line_start_pos) = 0;
14179 /* Let the overlay arrow be updated the next time. */
14180 update_overlay_arrows (0);
14182 /* If we pause after scrolling, some rows in the current
14183 matrices of some windows are not valid. */
14184 if (!WINDOW_FULL_WIDTH_P (w)
14185 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14186 update_mode_lines = 36;
14188 else
14190 if (!consider_all_windows_p)
14192 /* This has already been done above if
14193 consider_all_windows_p is set. */
14194 if (XBUFFER (w->contents)->text->redisplay
14195 && buffer_window_count (XBUFFER (w->contents)) > 1)
14196 /* This can happen if b->text->redisplay was set during
14197 jit-lock. */
14198 propagate_buffer_redisplay ();
14199 mark_window_display_accurate_1 (w, true);
14201 /* Say overlay arrows are up to date. */
14202 update_overlay_arrows (1);
14204 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14205 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14208 update_mode_lines = 0;
14209 windows_or_buffers_changed = 0;
14212 /* Start SIGIO interrupts coming again. Having them off during the
14213 code above makes it less likely one will discard output, but not
14214 impossible, since there might be stuff in the system buffer here.
14215 But it is much hairier to try to do anything about that. */
14216 if (interrupt_input)
14217 request_sigio ();
14218 RESUME_POLLING;
14220 /* If a frame has become visible which was not before, redisplay
14221 again, so that we display it. Expose events for such a frame
14222 (which it gets when becoming visible) don't call the parts of
14223 redisplay constructing glyphs, so simply exposing a frame won't
14224 display anything in this case. So, we have to display these
14225 frames here explicitly. */
14226 if (!pending)
14228 int new_count = 0;
14230 FOR_EACH_FRAME (tail, frame)
14232 if (XFRAME (frame)->visible)
14233 new_count++;
14236 if (new_count != number_of_visible_frames)
14237 windows_or_buffers_changed = 52;
14240 /* Change frame size now if a change is pending. */
14241 do_pending_window_change (true);
14243 /* If we just did a pending size change, or have additional
14244 visible frames, or selected_window changed, redisplay again. */
14245 if ((windows_or_buffers_changed && !pending)
14246 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14247 goto retry;
14249 /* Clear the face and image caches.
14251 We used to do this only if consider_all_windows_p. But the cache
14252 needs to be cleared if a timer creates images in the current
14253 buffer (e.g. the test case in Bug#6230). */
14255 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14257 clear_face_cache (false);
14258 clear_face_cache_count = 0;
14261 #ifdef HAVE_WINDOW_SYSTEM
14262 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14264 clear_image_caches (Qnil);
14265 clear_image_cache_count = 0;
14267 #endif /* HAVE_WINDOW_SYSTEM */
14269 end_of_redisplay:
14270 #ifdef HAVE_NS
14271 ns_set_doc_edited ();
14272 #endif
14273 if (interrupt_input && interrupts_deferred)
14274 request_sigio ();
14276 unbind_to (count, Qnil);
14277 RESUME_POLLING;
14281 /* Redisplay, but leave alone any recent echo area message unless
14282 another message has been requested in its place.
14284 This is useful in situations where you need to redisplay but no
14285 user action has occurred, making it inappropriate for the message
14286 area to be cleared. See tracking_off and
14287 wait_reading_process_output for examples of these situations.
14289 FROM_WHERE is an integer saying from where this function was
14290 called. This is useful for debugging. */
14292 void
14293 redisplay_preserve_echo_area (int from_where)
14295 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14297 if (!NILP (echo_area_buffer[1]))
14299 /* We have a previously displayed message, but no current
14300 message. Redisplay the previous message. */
14301 display_last_displayed_message_p = true;
14302 redisplay_internal ();
14303 display_last_displayed_message_p = false;
14305 else
14306 redisplay_internal ();
14308 flush_frame (SELECTED_FRAME ());
14312 /* Function registered with record_unwind_protect in redisplay_internal. */
14314 static void
14315 unwind_redisplay (void)
14317 redisplaying_p = false;
14321 /* Mark the display of leaf window W as accurate or inaccurate.
14322 If ACCURATE_P, mark display of W as accurate.
14323 If !ACCURATE_P, arrange for W to be redisplayed the next
14324 time redisplay_internal is called. */
14326 static void
14327 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14329 struct buffer *b = XBUFFER (w->contents);
14331 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14332 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14333 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14335 if (accurate_p)
14337 b->clip_changed = false;
14338 b->prevent_redisplay_optimizations_p = false;
14339 eassert (buffer_window_count (b) > 0);
14340 /* Resetting b->text->redisplay is problematic!
14341 In order to make it safer to do it here, redisplay_internal must
14342 have copied all b->text->redisplay to their respective windows. */
14343 b->text->redisplay = false;
14345 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14346 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14347 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14348 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14350 w->current_matrix->buffer = b;
14351 w->current_matrix->begv = BUF_BEGV (b);
14352 w->current_matrix->zv = BUF_ZV (b);
14354 w->last_cursor_vpos = w->cursor.vpos;
14355 w->last_cursor_off_p = w->cursor_off_p;
14357 if (w == XWINDOW (selected_window))
14358 w->last_point = BUF_PT (b);
14359 else
14360 w->last_point = marker_position (w->pointm);
14362 w->window_end_valid = true;
14363 w->update_mode_line = false;
14366 w->redisplay = !accurate_p;
14370 /* Mark the display of windows in the window tree rooted at WINDOW as
14371 accurate or inaccurate. If ACCURATE_P, mark display of
14372 windows as accurate. If !ACCURATE_P, arrange for windows to
14373 be redisplayed the next time redisplay_internal is called. */
14375 void
14376 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14378 struct window *w;
14380 for (; !NILP (window); window = w->next)
14382 w = XWINDOW (window);
14383 if (WINDOWP (w->contents))
14384 mark_window_display_accurate (w->contents, accurate_p);
14385 else
14386 mark_window_display_accurate_1 (w, accurate_p);
14389 if (accurate_p)
14390 update_overlay_arrows (1);
14391 else
14392 /* Force a thorough redisplay the next time by setting
14393 last_arrow_position and last_arrow_string to t, which is
14394 unequal to any useful value of Voverlay_arrow_... */
14395 update_overlay_arrows (-1);
14399 /* Return value in display table DP (Lisp_Char_Table *) for character
14400 C. Since a display table doesn't have any parent, we don't have to
14401 follow parent. Do not call this function directly but use the
14402 macro DISP_CHAR_VECTOR. */
14404 Lisp_Object
14405 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14407 Lisp_Object val;
14409 if (ASCII_CHAR_P (c))
14411 val = dp->ascii;
14412 if (SUB_CHAR_TABLE_P (val))
14413 val = XSUB_CHAR_TABLE (val)->contents[c];
14415 else
14417 Lisp_Object table;
14419 XSETCHAR_TABLE (table, dp);
14420 val = char_table_ref (table, c);
14422 if (NILP (val))
14423 val = dp->defalt;
14424 return val;
14429 /***********************************************************************
14430 Window Redisplay
14431 ***********************************************************************/
14433 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14435 static void
14436 redisplay_windows (Lisp_Object window)
14438 while (!NILP (window))
14440 struct window *w = XWINDOW (window);
14442 if (WINDOWP (w->contents))
14443 redisplay_windows (w->contents);
14444 else if (BUFFERP (w->contents))
14446 displayed_buffer = XBUFFER (w->contents);
14447 /* Use list_of_error, not Qerror, so that
14448 we catch only errors and don't run the debugger. */
14449 internal_condition_case_1 (redisplay_window_0, window,
14450 list_of_error,
14451 redisplay_window_error);
14454 window = w->next;
14458 static Lisp_Object
14459 redisplay_window_error (Lisp_Object ignore)
14461 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14462 return Qnil;
14465 static Lisp_Object
14466 redisplay_window_0 (Lisp_Object window)
14468 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14469 redisplay_window (window, false);
14470 return Qnil;
14473 static Lisp_Object
14474 redisplay_window_1 (Lisp_Object window)
14476 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14477 redisplay_window (window, true);
14478 return Qnil;
14482 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14483 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14484 which positions recorded in ROW differ from current buffer
14485 positions.
14487 Return true iff cursor is on this row. */
14489 static bool
14490 set_cursor_from_row (struct window *w, struct glyph_row *row,
14491 struct glyph_matrix *matrix,
14492 ptrdiff_t delta, ptrdiff_t delta_bytes,
14493 int dy, int dvpos)
14495 struct glyph *glyph = row->glyphs[TEXT_AREA];
14496 struct glyph *end = glyph + row->used[TEXT_AREA];
14497 struct glyph *cursor = NULL;
14498 /* The last known character position in row. */
14499 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14500 int x = row->x;
14501 ptrdiff_t pt_old = PT - delta;
14502 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14503 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14504 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14505 /* A glyph beyond the edge of TEXT_AREA which we should never
14506 touch. */
14507 struct glyph *glyphs_end = end;
14508 /* True means we've found a match for cursor position, but that
14509 glyph has the avoid_cursor_p flag set. */
14510 bool match_with_avoid_cursor = false;
14511 /* True means we've seen at least one glyph that came from a
14512 display string. */
14513 bool string_seen = false;
14514 /* Largest and smallest buffer positions seen so far during scan of
14515 glyph row. */
14516 ptrdiff_t bpos_max = pos_before;
14517 ptrdiff_t bpos_min = pos_after;
14518 /* Last buffer position covered by an overlay string with an integer
14519 `cursor' property. */
14520 ptrdiff_t bpos_covered = 0;
14521 /* True means the display string on which to display the cursor
14522 comes from a text property, not from an overlay. */
14523 bool string_from_text_prop = false;
14525 /* Don't even try doing anything if called for a mode-line or
14526 header-line row, since the rest of the code isn't prepared to
14527 deal with such calamities. */
14528 eassert (!row->mode_line_p);
14529 if (row->mode_line_p)
14530 return false;
14532 /* Skip over glyphs not having an object at the start and the end of
14533 the row. These are special glyphs like truncation marks on
14534 terminal frames. */
14535 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14537 if (!row->reversed_p)
14539 while (glyph < end
14540 && NILP (glyph->object)
14541 && glyph->charpos < 0)
14543 x += glyph->pixel_width;
14544 ++glyph;
14546 while (end > glyph
14547 && NILP ((end - 1)->object)
14548 /* CHARPOS is zero for blanks and stretch glyphs
14549 inserted by extend_face_to_end_of_line. */
14550 && (end - 1)->charpos <= 0)
14551 --end;
14552 glyph_before = glyph - 1;
14553 glyph_after = end;
14555 else
14557 struct glyph *g;
14559 /* If the glyph row is reversed, we need to process it from back
14560 to front, so swap the edge pointers. */
14561 glyphs_end = end = glyph - 1;
14562 glyph += row->used[TEXT_AREA] - 1;
14564 while (glyph > end + 1
14565 && NILP (glyph->object)
14566 && glyph->charpos < 0)
14568 --glyph;
14569 x -= glyph->pixel_width;
14571 if (NILP (glyph->object) && glyph->charpos < 0)
14572 --glyph;
14573 /* By default, in reversed rows we put the cursor on the
14574 rightmost (first in the reading order) glyph. */
14575 for (g = end + 1; g < glyph; g++)
14576 x += g->pixel_width;
14577 while (end < glyph
14578 && NILP ((end + 1)->object)
14579 && (end + 1)->charpos <= 0)
14580 ++end;
14581 glyph_before = glyph + 1;
14582 glyph_after = end;
14585 else if (row->reversed_p)
14587 /* In R2L rows that don't display text, put the cursor on the
14588 rightmost glyph. Case in point: an empty last line that is
14589 part of an R2L paragraph. */
14590 cursor = end - 1;
14591 /* Avoid placing the cursor on the last glyph of the row, where
14592 on terminal frames we hold the vertical border between
14593 adjacent windows. */
14594 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14595 && !WINDOW_RIGHTMOST_P (w)
14596 && cursor == row->glyphs[LAST_AREA] - 1)
14597 cursor--;
14598 x = -1; /* will be computed below, at label compute_x */
14601 /* Step 1: Try to find the glyph whose character position
14602 corresponds to point. If that's not possible, find 2 glyphs
14603 whose character positions are the closest to point, one before
14604 point, the other after it. */
14605 if (!row->reversed_p)
14606 while (/* not marched to end of glyph row */
14607 glyph < end
14608 /* glyph was not inserted by redisplay for internal purposes */
14609 && !NILP (glyph->object))
14611 if (BUFFERP (glyph->object))
14613 ptrdiff_t dpos = glyph->charpos - pt_old;
14615 if (glyph->charpos > bpos_max)
14616 bpos_max = glyph->charpos;
14617 if (glyph->charpos < bpos_min)
14618 bpos_min = glyph->charpos;
14619 if (!glyph->avoid_cursor_p)
14621 /* If we hit point, we've found the glyph on which to
14622 display the cursor. */
14623 if (dpos == 0)
14625 match_with_avoid_cursor = false;
14626 break;
14628 /* See if we've found a better approximation to
14629 POS_BEFORE or to POS_AFTER. */
14630 if (0 > dpos && dpos > pos_before - pt_old)
14632 pos_before = glyph->charpos;
14633 glyph_before = glyph;
14635 else if (0 < dpos && dpos < pos_after - pt_old)
14637 pos_after = glyph->charpos;
14638 glyph_after = glyph;
14641 else if (dpos == 0)
14642 match_with_avoid_cursor = true;
14644 else if (STRINGP (glyph->object))
14646 Lisp_Object chprop;
14647 ptrdiff_t glyph_pos = glyph->charpos;
14649 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14650 glyph->object);
14651 if (!NILP (chprop))
14653 /* If the string came from a `display' text property,
14654 look up the buffer position of that property and
14655 use that position to update bpos_max, as if we
14656 actually saw such a position in one of the row's
14657 glyphs. This helps with supporting integer values
14658 of `cursor' property on the display string in
14659 situations where most or all of the row's buffer
14660 text is completely covered by display properties,
14661 so that no glyph with valid buffer positions is
14662 ever seen in the row. */
14663 ptrdiff_t prop_pos =
14664 string_buffer_position_lim (glyph->object, pos_before,
14665 pos_after, false);
14667 if (prop_pos >= pos_before)
14668 bpos_max = prop_pos;
14670 if (INTEGERP (chprop))
14672 bpos_covered = bpos_max + XINT (chprop);
14673 /* If the `cursor' property covers buffer positions up
14674 to and including point, we should display cursor on
14675 this glyph. Note that, if a `cursor' property on one
14676 of the string's characters has an integer value, we
14677 will break out of the loop below _before_ we get to
14678 the position match above. IOW, integer values of
14679 the `cursor' property override the "exact match for
14680 point" strategy of positioning the cursor. */
14681 /* Implementation note: bpos_max == pt_old when, e.g.,
14682 we are in an empty line, where bpos_max is set to
14683 MATRIX_ROW_START_CHARPOS, see above. */
14684 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14686 cursor = glyph;
14687 break;
14691 string_seen = true;
14693 x += glyph->pixel_width;
14694 ++glyph;
14696 else if (glyph > end) /* row is reversed */
14697 while (!NILP (glyph->object))
14699 if (BUFFERP (glyph->object))
14701 ptrdiff_t dpos = glyph->charpos - pt_old;
14703 if (glyph->charpos > bpos_max)
14704 bpos_max = glyph->charpos;
14705 if (glyph->charpos < bpos_min)
14706 bpos_min = glyph->charpos;
14707 if (!glyph->avoid_cursor_p)
14709 if (dpos == 0)
14711 match_with_avoid_cursor = false;
14712 break;
14714 if (0 > dpos && dpos > pos_before - pt_old)
14716 pos_before = glyph->charpos;
14717 glyph_before = glyph;
14719 else if (0 < dpos && dpos < pos_after - pt_old)
14721 pos_after = glyph->charpos;
14722 glyph_after = glyph;
14725 else if (dpos == 0)
14726 match_with_avoid_cursor = true;
14728 else if (STRINGP (glyph->object))
14730 Lisp_Object chprop;
14731 ptrdiff_t glyph_pos = glyph->charpos;
14733 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14734 glyph->object);
14735 if (!NILP (chprop))
14737 ptrdiff_t prop_pos =
14738 string_buffer_position_lim (glyph->object, pos_before,
14739 pos_after, false);
14741 if (prop_pos >= pos_before)
14742 bpos_max = prop_pos;
14744 if (INTEGERP (chprop))
14746 bpos_covered = bpos_max + XINT (chprop);
14747 /* If the `cursor' property covers buffer positions up
14748 to and including point, we should display cursor on
14749 this glyph. */
14750 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14752 cursor = glyph;
14753 break;
14756 string_seen = true;
14758 --glyph;
14759 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14761 x--; /* can't use any pixel_width */
14762 break;
14764 x -= glyph->pixel_width;
14767 /* Step 2: If we didn't find an exact match for point, we need to
14768 look for a proper place to put the cursor among glyphs between
14769 GLYPH_BEFORE and GLYPH_AFTER. */
14770 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14771 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14772 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14774 /* An empty line has a single glyph whose OBJECT is nil and
14775 whose CHARPOS is the position of a newline on that line.
14776 Note that on a TTY, there are more glyphs after that, which
14777 were produced by extend_face_to_end_of_line, but their
14778 CHARPOS is zero or negative. */
14779 bool empty_line_p =
14780 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14781 && NILP (glyph->object) && glyph->charpos > 0
14782 /* On a TTY, continued and truncated rows also have a glyph at
14783 their end whose OBJECT is nil and whose CHARPOS is
14784 positive (the continuation and truncation glyphs), but such
14785 rows are obviously not "empty". */
14786 && !(row->continued_p || row->truncated_on_right_p));
14788 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14790 ptrdiff_t ellipsis_pos;
14792 /* Scan back over the ellipsis glyphs. */
14793 if (!row->reversed_p)
14795 ellipsis_pos = (glyph - 1)->charpos;
14796 while (glyph > row->glyphs[TEXT_AREA]
14797 && (glyph - 1)->charpos == ellipsis_pos)
14798 glyph--, x -= glyph->pixel_width;
14799 /* That loop always goes one position too far, including
14800 the glyph before the ellipsis. So scan forward over
14801 that one. */
14802 x += glyph->pixel_width;
14803 glyph++;
14805 else /* row is reversed */
14807 ellipsis_pos = (glyph + 1)->charpos;
14808 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14809 && (glyph + 1)->charpos == ellipsis_pos)
14810 glyph++, x += glyph->pixel_width;
14811 x -= glyph->pixel_width;
14812 glyph--;
14815 else if (match_with_avoid_cursor)
14817 cursor = glyph_after;
14818 x = -1;
14820 else if (string_seen)
14822 int incr = row->reversed_p ? -1 : +1;
14824 /* Need to find the glyph that came out of a string which is
14825 present at point. That glyph is somewhere between
14826 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14827 positioned between POS_BEFORE and POS_AFTER in the
14828 buffer. */
14829 struct glyph *start, *stop;
14830 ptrdiff_t pos = pos_before;
14832 x = -1;
14834 /* If the row ends in a newline from a display string,
14835 reordering could have moved the glyphs belonging to the
14836 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14837 in this case we extend the search to the last glyph in
14838 the row that was not inserted by redisplay. */
14839 if (row->ends_in_newline_from_string_p)
14841 glyph_after = end;
14842 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14845 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14846 correspond to POS_BEFORE and POS_AFTER, respectively. We
14847 need START and STOP in the order that corresponds to the
14848 row's direction as given by its reversed_p flag. If the
14849 directionality of characters between POS_BEFORE and
14850 POS_AFTER is the opposite of the row's base direction,
14851 these characters will have been reordered for display,
14852 and we need to reverse START and STOP. */
14853 if (!row->reversed_p)
14855 start = min (glyph_before, glyph_after);
14856 stop = max (glyph_before, glyph_after);
14858 else
14860 start = max (glyph_before, glyph_after);
14861 stop = min (glyph_before, glyph_after);
14863 for (glyph = start + incr;
14864 row->reversed_p ? glyph > stop : glyph < stop; )
14867 /* Any glyphs that come from the buffer are here because
14868 of bidi reordering. Skip them, and only pay
14869 attention to glyphs that came from some string. */
14870 if (STRINGP (glyph->object))
14872 Lisp_Object str;
14873 ptrdiff_t tem;
14874 /* If the display property covers the newline, we
14875 need to search for it one position farther. */
14876 ptrdiff_t lim = pos_after
14877 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14879 string_from_text_prop = false;
14880 str = glyph->object;
14881 tem = string_buffer_position_lim (str, pos, lim, false);
14882 if (tem == 0 /* from overlay */
14883 || pos <= tem)
14885 /* If the string from which this glyph came is
14886 found in the buffer at point, or at position
14887 that is closer to point than pos_after, then
14888 we've found the glyph we've been looking for.
14889 If it comes from an overlay (tem == 0), and
14890 it has the `cursor' property on one of its
14891 glyphs, record that glyph as a candidate for
14892 displaying the cursor. (As in the
14893 unidirectional version, we will display the
14894 cursor on the last candidate we find.) */
14895 if (tem == 0
14896 || tem == pt_old
14897 || (tem - pt_old > 0 && tem < pos_after))
14899 /* The glyphs from this string could have
14900 been reordered. Find the one with the
14901 smallest string position. Or there could
14902 be a character in the string with the
14903 `cursor' property, which means display
14904 cursor on that character's glyph. */
14905 ptrdiff_t strpos = glyph->charpos;
14907 if (tem)
14909 cursor = glyph;
14910 string_from_text_prop = true;
14912 for ( ;
14913 (row->reversed_p ? glyph > stop : glyph < stop)
14914 && EQ (glyph->object, str);
14915 glyph += incr)
14917 Lisp_Object cprop;
14918 ptrdiff_t gpos = glyph->charpos;
14920 cprop = Fget_char_property (make_number (gpos),
14921 Qcursor,
14922 glyph->object);
14923 if (!NILP (cprop))
14925 cursor = glyph;
14926 break;
14928 if (tem && glyph->charpos < strpos)
14930 strpos = glyph->charpos;
14931 cursor = glyph;
14935 if (tem == pt_old
14936 || (tem - pt_old > 0 && tem < pos_after))
14937 goto compute_x;
14939 if (tem)
14940 pos = tem + 1; /* don't find previous instances */
14942 /* This string is not what we want; skip all of the
14943 glyphs that came from it. */
14944 while ((row->reversed_p ? glyph > stop : glyph < stop)
14945 && EQ (glyph->object, str))
14946 glyph += incr;
14948 else
14949 glyph += incr;
14952 /* If we reached the end of the line, and END was from a string,
14953 the cursor is not on this line. */
14954 if (cursor == NULL
14955 && (row->reversed_p ? glyph <= end : glyph >= end)
14956 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14957 && STRINGP (end->object)
14958 && row->continued_p)
14959 return false;
14961 /* A truncated row may not include PT among its character positions.
14962 Setting the cursor inside the scroll margin will trigger
14963 recalculation of hscroll in hscroll_window_tree. But if a
14964 display string covers point, defer to the string-handling
14965 code below to figure this out. */
14966 else if (row->truncated_on_left_p && pt_old < bpos_min)
14968 cursor = glyph_before;
14969 x = -1;
14971 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14972 /* Zero-width characters produce no glyphs. */
14973 || (!empty_line_p
14974 && (row->reversed_p
14975 ? glyph_after > glyphs_end
14976 : glyph_after < glyphs_end)))
14978 cursor = glyph_after;
14979 x = -1;
14983 compute_x:
14984 if (cursor != NULL)
14985 glyph = cursor;
14986 else if (glyph == glyphs_end
14987 && pos_before == pos_after
14988 && STRINGP ((row->reversed_p
14989 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14990 : row->glyphs[TEXT_AREA])->object))
14992 /* If all the glyphs of this row came from strings, put the
14993 cursor on the first glyph of the row. This avoids having the
14994 cursor outside of the text area in this very rare and hard
14995 use case. */
14996 glyph =
14997 row->reversed_p
14998 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14999 : row->glyphs[TEXT_AREA];
15001 if (x < 0)
15003 struct glyph *g;
15005 /* Need to compute x that corresponds to GLYPH. */
15006 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15008 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15009 emacs_abort ();
15010 x += g->pixel_width;
15014 /* ROW could be part of a continued line, which, under bidi
15015 reordering, might have other rows whose start and end charpos
15016 occlude point. Only set w->cursor if we found a better
15017 approximation to the cursor position than we have from previously
15018 examined candidate rows belonging to the same continued line. */
15019 if (/* We already have a candidate row. */
15020 w->cursor.vpos >= 0
15021 /* That candidate is not the row we are processing. */
15022 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15023 /* Make sure cursor.vpos specifies a row whose start and end
15024 charpos occlude point, and it is valid candidate for being a
15025 cursor-row. This is because some callers of this function
15026 leave cursor.vpos at the row where the cursor was displayed
15027 during the last redisplay cycle. */
15028 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15029 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15030 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15032 struct glyph *g1
15033 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15035 /* Don't consider glyphs that are outside TEXT_AREA. */
15036 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15037 return false;
15038 /* Keep the candidate whose buffer position is the closest to
15039 point or has the `cursor' property. */
15040 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15041 w->cursor.hpos >= 0
15042 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15043 && ((BUFFERP (g1->object)
15044 && (g1->charpos == pt_old /* An exact match always wins. */
15045 || (BUFFERP (glyph->object)
15046 && eabs (g1->charpos - pt_old)
15047 < eabs (glyph->charpos - pt_old))))
15048 /* Previous candidate is a glyph from a string that has
15049 a non-nil `cursor' property. */
15050 || (STRINGP (g1->object)
15051 && (!NILP (Fget_char_property (make_number (g1->charpos),
15052 Qcursor, g1->object))
15053 /* Previous candidate is from the same display
15054 string as this one, and the display string
15055 came from a text property. */
15056 || (EQ (g1->object, glyph->object)
15057 && string_from_text_prop)
15058 /* this candidate is from newline and its
15059 position is not an exact match */
15060 || (NILP (glyph->object)
15061 && glyph->charpos != pt_old)))))
15062 return false;
15063 /* If this candidate gives an exact match, use that. */
15064 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15065 /* If this candidate is a glyph created for the
15066 terminating newline of a line, and point is on that
15067 newline, it wins because it's an exact match. */
15068 || (!row->continued_p
15069 && NILP (glyph->object)
15070 && glyph->charpos == 0
15071 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15072 /* Otherwise, keep the candidate that comes from a row
15073 spanning less buffer positions. This may win when one or
15074 both candidate positions are on glyphs that came from
15075 display strings, for which we cannot compare buffer
15076 positions. */
15077 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15078 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15079 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15080 return false;
15082 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15083 w->cursor.x = x;
15084 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15085 w->cursor.y = row->y + dy;
15087 if (w == XWINDOW (selected_window))
15089 if (!row->continued_p
15090 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15091 && row->x == 0)
15093 this_line_buffer = XBUFFER (w->contents);
15095 CHARPOS (this_line_start_pos)
15096 = MATRIX_ROW_START_CHARPOS (row) + delta;
15097 BYTEPOS (this_line_start_pos)
15098 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15100 CHARPOS (this_line_end_pos)
15101 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15102 BYTEPOS (this_line_end_pos)
15103 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15105 this_line_y = w->cursor.y;
15106 this_line_pixel_height = row->height;
15107 this_line_vpos = w->cursor.vpos;
15108 this_line_start_x = row->x;
15110 else
15111 CHARPOS (this_line_start_pos) = 0;
15114 return true;
15118 /* Run window scroll functions, if any, for WINDOW with new window
15119 start STARTP. Sets the window start of WINDOW to that position.
15121 We assume that the window's buffer is really current. */
15123 static struct text_pos
15124 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15126 struct window *w = XWINDOW (window);
15127 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15129 eassert (current_buffer == XBUFFER (w->contents));
15131 if (!NILP (Vwindow_scroll_functions))
15133 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15134 make_number (CHARPOS (startp)));
15135 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15136 /* In case the hook functions switch buffers. */
15137 set_buffer_internal (XBUFFER (w->contents));
15140 return startp;
15144 /* Make sure the line containing the cursor is fully visible.
15145 A value of true means there is nothing to be done.
15146 (Either the line is fully visible, or it cannot be made so,
15147 or we cannot tell.)
15149 If FORCE_P, return false even if partial visible cursor row
15150 is higher than window.
15152 If CURRENT_MATRIX_P, use the information from the
15153 window's current glyph matrix; otherwise use the desired glyph
15154 matrix.
15156 A value of false means the caller should do scrolling
15157 as if point had gone off the screen. */
15159 static bool
15160 cursor_row_fully_visible_p (struct window *w, bool force_p,
15161 bool current_matrix_p)
15163 struct glyph_matrix *matrix;
15164 struct glyph_row *row;
15165 int window_height;
15167 if (!make_cursor_line_fully_visible_p)
15168 return true;
15170 /* It's not always possible to find the cursor, e.g, when a window
15171 is full of overlay strings. Don't do anything in that case. */
15172 if (w->cursor.vpos < 0)
15173 return true;
15175 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15176 row = MATRIX_ROW (matrix, w->cursor.vpos);
15178 /* If the cursor row is not partially visible, there's nothing to do. */
15179 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15180 return true;
15182 /* If the row the cursor is in is taller than the window's height,
15183 it's not clear what to do, so do nothing. */
15184 window_height = window_box_height (w);
15185 if (row->height >= window_height)
15187 if (!force_p || MINI_WINDOW_P (w)
15188 || w->vscroll || w->cursor.vpos == 0)
15189 return true;
15191 return false;
15195 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15196 means only WINDOW is redisplayed in redisplay_internal.
15197 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15198 in redisplay_window to bring a partially visible line into view in
15199 the case that only the cursor has moved.
15201 LAST_LINE_MISFIT should be true if we're scrolling because the
15202 last screen line's vertical height extends past the end of the screen.
15204 Value is
15206 1 if scrolling succeeded
15208 0 if scrolling didn't find point.
15210 -1 if new fonts have been loaded so that we must interrupt
15211 redisplay, adjust glyph matrices, and try again. */
15213 enum
15215 SCROLLING_SUCCESS,
15216 SCROLLING_FAILED,
15217 SCROLLING_NEED_LARGER_MATRICES
15220 /* If scroll-conservatively is more than this, never recenter.
15222 If you change this, don't forget to update the doc string of
15223 `scroll-conservatively' and the Emacs manual. */
15224 #define SCROLL_LIMIT 100
15226 static int
15227 try_scrolling (Lisp_Object window, bool just_this_one_p,
15228 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15229 bool temp_scroll_step, bool last_line_misfit)
15231 struct window *w = XWINDOW (window);
15232 struct frame *f = XFRAME (w->frame);
15233 struct text_pos pos, startp;
15234 struct it it;
15235 int this_scroll_margin, scroll_max, rc, height;
15236 int dy = 0, amount_to_scroll = 0;
15237 bool scroll_down_p = false;
15238 int extra_scroll_margin_lines = last_line_misfit;
15239 Lisp_Object aggressive;
15240 /* We will never try scrolling more than this number of lines. */
15241 int scroll_limit = SCROLL_LIMIT;
15242 int frame_line_height = default_line_pixel_height (w);
15243 int window_total_lines
15244 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15246 #ifdef GLYPH_DEBUG
15247 debug_method_add (w, "try_scrolling");
15248 #endif
15250 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15252 /* Compute scroll margin height in pixels. We scroll when point is
15253 within this distance from the top or bottom of the window. */
15254 if (scroll_margin > 0)
15255 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15256 * frame_line_height;
15257 else
15258 this_scroll_margin = 0;
15260 /* Force arg_scroll_conservatively to have a reasonable value, to
15261 avoid scrolling too far away with slow move_it_* functions. Note
15262 that the user can supply scroll-conservatively equal to
15263 `most-positive-fixnum', which can be larger than INT_MAX. */
15264 if (arg_scroll_conservatively > scroll_limit)
15266 arg_scroll_conservatively = scroll_limit + 1;
15267 scroll_max = scroll_limit * frame_line_height;
15269 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15270 /* Compute how much we should try to scroll maximally to bring
15271 point into view. */
15272 scroll_max = (max (scroll_step,
15273 max (arg_scroll_conservatively, temp_scroll_step))
15274 * frame_line_height);
15275 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15276 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15277 /* We're trying to scroll because of aggressive scrolling but no
15278 scroll_step is set. Choose an arbitrary one. */
15279 scroll_max = 10 * frame_line_height;
15280 else
15281 scroll_max = 0;
15283 too_near_end:
15285 /* Decide whether to scroll down. */
15286 if (PT > CHARPOS (startp))
15288 int scroll_margin_y;
15290 /* Compute the pixel ypos of the scroll margin, then move IT to
15291 either that ypos or PT, whichever comes first. */
15292 start_display (&it, w, startp);
15293 scroll_margin_y = it.last_visible_y - this_scroll_margin
15294 - frame_line_height * extra_scroll_margin_lines;
15295 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15296 (MOVE_TO_POS | MOVE_TO_Y));
15298 if (PT > CHARPOS (it.current.pos))
15300 int y0 = line_bottom_y (&it);
15301 /* Compute how many pixels below window bottom to stop searching
15302 for PT. This avoids costly search for PT that is far away if
15303 the user limited scrolling by a small number of lines, but
15304 always finds PT if scroll_conservatively is set to a large
15305 number, such as most-positive-fixnum. */
15306 int slack = max (scroll_max, 10 * frame_line_height);
15307 int y_to_move = it.last_visible_y + slack;
15309 /* Compute the distance from the scroll margin to PT or to
15310 the scroll limit, whichever comes first. This should
15311 include the height of the cursor line, to make that line
15312 fully visible. */
15313 move_it_to (&it, PT, -1, y_to_move,
15314 -1, MOVE_TO_POS | MOVE_TO_Y);
15315 dy = line_bottom_y (&it) - y0;
15317 if (dy > scroll_max)
15318 return SCROLLING_FAILED;
15320 if (dy > 0)
15321 scroll_down_p = true;
15323 else if (PT == IT_CHARPOS (it)
15324 && IT_CHARPOS (it) < ZV
15325 && it.method == GET_FROM_STRING
15326 && arg_scroll_conservatively > scroll_limit
15327 && it.current_x == 0)
15329 enum move_it_result skip;
15330 int y1 = it.current_y;
15331 int vpos;
15333 /* A before-string that includes newlines and is displayed
15334 on the last visible screen line could fail us under
15335 scroll-conservatively > 100, because we will be unable to
15336 position the cursor on that last visible line. Try to
15337 recover by finding the first screen line that has some
15338 glyphs coming from the buffer text. */
15339 do {
15340 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15341 if (skip != MOVE_NEWLINE_OR_CR
15342 || IT_CHARPOS (it) != PT
15343 || it.method == GET_FROM_BUFFER)
15344 break;
15345 vpos = it.vpos;
15346 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15347 } while (it.vpos > vpos);
15349 dy = it.current_y - y1;
15351 if (dy > scroll_max)
15352 return SCROLLING_FAILED;
15354 if (dy > 0)
15355 scroll_down_p = true;
15359 if (scroll_down_p)
15361 /* Point is in or below the bottom scroll margin, so move the
15362 window start down. If scrolling conservatively, move it just
15363 enough down to make point visible. If scroll_step is set,
15364 move it down by scroll_step. */
15365 if (arg_scroll_conservatively)
15366 amount_to_scroll
15367 = min (max (dy, frame_line_height),
15368 frame_line_height * arg_scroll_conservatively);
15369 else if (scroll_step || temp_scroll_step)
15370 amount_to_scroll = scroll_max;
15371 else
15373 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15374 height = WINDOW_BOX_TEXT_HEIGHT (w);
15375 if (NUMBERP (aggressive))
15377 double float_amount = XFLOATINT (aggressive) * height;
15378 int aggressive_scroll = float_amount;
15379 if (aggressive_scroll == 0 && float_amount > 0)
15380 aggressive_scroll = 1;
15381 /* Don't let point enter the scroll margin near top of
15382 the window. This could happen if the value of
15383 scroll_up_aggressively is too large and there are
15384 non-zero margins, because scroll_up_aggressively
15385 means put point that fraction of window height
15386 _from_the_bottom_margin_. */
15387 if (aggressive_scroll + 2 * this_scroll_margin > height)
15388 aggressive_scroll = height - 2 * this_scroll_margin;
15389 amount_to_scroll = dy + aggressive_scroll;
15393 if (amount_to_scroll <= 0)
15394 return SCROLLING_FAILED;
15396 start_display (&it, w, startp);
15397 if (arg_scroll_conservatively <= scroll_limit)
15398 move_it_vertically (&it, amount_to_scroll);
15399 else
15401 /* Extra precision for users who set scroll-conservatively
15402 to a large number: make sure the amount we scroll
15403 the window start is never less than amount_to_scroll,
15404 which was computed as distance from window bottom to
15405 point. This matters when lines at window top and lines
15406 below window bottom have different height. */
15407 struct it it1;
15408 void *it1data = NULL;
15409 /* We use a temporary it1 because line_bottom_y can modify
15410 its argument, if it moves one line down; see there. */
15411 int start_y;
15413 SAVE_IT (it1, it, it1data);
15414 start_y = line_bottom_y (&it1);
15415 do {
15416 RESTORE_IT (&it, &it, it1data);
15417 move_it_by_lines (&it, 1);
15418 SAVE_IT (it1, it, it1data);
15419 } while (IT_CHARPOS (it) < ZV
15420 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15421 bidi_unshelve_cache (it1data, true);
15424 /* If STARTP is unchanged, move it down another screen line. */
15425 if (IT_CHARPOS (it) == CHARPOS (startp))
15426 move_it_by_lines (&it, 1);
15427 startp = it.current.pos;
15429 else
15431 struct text_pos scroll_margin_pos = startp;
15432 int y_offset = 0;
15434 /* See if point is inside the scroll margin at the top of the
15435 window. */
15436 if (this_scroll_margin)
15438 int y_start;
15440 start_display (&it, w, startp);
15441 y_start = it.current_y;
15442 move_it_vertically (&it, this_scroll_margin);
15443 scroll_margin_pos = it.current.pos;
15444 /* If we didn't move enough before hitting ZV, request
15445 additional amount of scroll, to move point out of the
15446 scroll margin. */
15447 if (IT_CHARPOS (it) == ZV
15448 && it.current_y - y_start < this_scroll_margin)
15449 y_offset = this_scroll_margin - (it.current_y - y_start);
15452 if (PT < CHARPOS (scroll_margin_pos))
15454 /* Point is in the scroll margin at the top of the window or
15455 above what is displayed in the window. */
15456 int y0, y_to_move;
15458 /* Compute the vertical distance from PT to the scroll
15459 margin position. Move as far as scroll_max allows, or
15460 one screenful, or 10 screen lines, whichever is largest.
15461 Give up if distance is greater than scroll_max or if we
15462 didn't reach the scroll margin position. */
15463 SET_TEXT_POS (pos, PT, PT_BYTE);
15464 start_display (&it, w, pos);
15465 y0 = it.current_y;
15466 y_to_move = max (it.last_visible_y,
15467 max (scroll_max, 10 * frame_line_height));
15468 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15469 y_to_move, -1,
15470 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15471 dy = it.current_y - y0;
15472 if (dy > scroll_max
15473 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15474 return SCROLLING_FAILED;
15476 /* Additional scroll for when ZV was too close to point. */
15477 dy += y_offset;
15479 /* Compute new window start. */
15480 start_display (&it, w, startp);
15482 if (arg_scroll_conservatively)
15483 amount_to_scroll = max (dy, frame_line_height
15484 * max (scroll_step, temp_scroll_step));
15485 else if (scroll_step || temp_scroll_step)
15486 amount_to_scroll = scroll_max;
15487 else
15489 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15490 height = WINDOW_BOX_TEXT_HEIGHT (w);
15491 if (NUMBERP (aggressive))
15493 double float_amount = XFLOATINT (aggressive) * height;
15494 int aggressive_scroll = float_amount;
15495 if (aggressive_scroll == 0 && float_amount > 0)
15496 aggressive_scroll = 1;
15497 /* Don't let point enter the scroll margin near
15498 bottom of the window, if the value of
15499 scroll_down_aggressively happens to be too
15500 large. */
15501 if (aggressive_scroll + 2 * this_scroll_margin > height)
15502 aggressive_scroll = height - 2 * this_scroll_margin;
15503 amount_to_scroll = dy + aggressive_scroll;
15507 if (amount_to_scroll <= 0)
15508 return SCROLLING_FAILED;
15510 move_it_vertically_backward (&it, amount_to_scroll);
15511 startp = it.current.pos;
15515 /* Run window scroll functions. */
15516 startp = run_window_scroll_functions (window, startp);
15518 /* Display the window. Give up if new fonts are loaded, or if point
15519 doesn't appear. */
15520 if (!try_window (window, startp, 0))
15521 rc = SCROLLING_NEED_LARGER_MATRICES;
15522 else if (w->cursor.vpos < 0)
15524 clear_glyph_matrix (w->desired_matrix);
15525 rc = SCROLLING_FAILED;
15527 else
15529 /* Maybe forget recorded base line for line number display. */
15530 if (!just_this_one_p
15531 || current_buffer->clip_changed
15532 || BEG_UNCHANGED < CHARPOS (startp))
15533 w->base_line_number = 0;
15535 /* If cursor ends up on a partially visible line,
15536 treat that as being off the bottom of the screen. */
15537 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15538 false)
15539 /* It's possible that the cursor is on the first line of the
15540 buffer, which is partially obscured due to a vscroll
15541 (Bug#7537). In that case, avoid looping forever. */
15542 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15544 clear_glyph_matrix (w->desired_matrix);
15545 ++extra_scroll_margin_lines;
15546 goto too_near_end;
15548 rc = SCROLLING_SUCCESS;
15551 return rc;
15555 /* Compute a suitable window start for window W if display of W starts
15556 on a continuation line. Value is true if a new window start
15557 was computed.
15559 The new window start will be computed, based on W's width, starting
15560 from the start of the continued line. It is the start of the
15561 screen line with the minimum distance from the old start W->start,
15562 which is still before point (otherwise point will definitely not
15563 be visible in the window). */
15565 static bool
15566 compute_window_start_on_continuation_line (struct window *w)
15568 struct text_pos pos, start_pos, pos_before_pt;
15569 bool window_start_changed_p = false;
15571 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15573 /* If window start is on a continuation line... Window start may be
15574 < BEGV in case there's invisible text at the start of the
15575 buffer (M-x rmail, for example). */
15576 if (CHARPOS (start_pos) > BEGV
15577 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15579 struct it it;
15580 struct glyph_row *row;
15582 /* Handle the case that the window start is out of range. */
15583 if (CHARPOS (start_pos) < BEGV)
15584 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15585 else if (CHARPOS (start_pos) > ZV)
15586 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15588 /* Find the start of the continued line. This should be fast
15589 because find_newline is fast (newline cache). */
15590 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15591 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15592 row, DEFAULT_FACE_ID);
15593 reseat_at_previous_visible_line_start (&it);
15595 /* If the line start is "too far" away from the window start,
15596 say it takes too much time to compute a new window start.
15597 Also, give up if the line start is after point, as in that
15598 case point will not be visible with any window start we
15599 compute. */
15600 if (IT_CHARPOS (it) <= PT
15601 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15602 /* PXW: Do we need upper bounds here? */
15603 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15605 int min_distance, distance;
15607 /* Move forward by display lines to find the new window
15608 start. If window width was enlarged, the new start can
15609 be expected to be > the old start. If window width was
15610 decreased, the new window start will be < the old start.
15611 So, we're looking for the display line start with the
15612 minimum distance from the old window start. */
15613 pos_before_pt = pos = it.current.pos;
15614 min_distance = INFINITY;
15615 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15616 distance < min_distance)
15618 min_distance = distance;
15619 if (CHARPOS (pos) <= PT)
15620 pos_before_pt = pos;
15621 pos = it.current.pos;
15622 if (it.line_wrap == WORD_WRAP)
15624 /* Under WORD_WRAP, move_it_by_lines is likely to
15625 overshoot and stop not at the first, but the
15626 second character from the left margin. So in
15627 that case, we need a more tight control on the X
15628 coordinate of the iterator than move_it_by_lines
15629 promises in its contract. The method is to first
15630 go to the last (rightmost) visible character of a
15631 line, then move to the leftmost character on the
15632 next line in a separate call. */
15633 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15634 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15635 move_it_to (&it, ZV, 0,
15636 it.current_y + it.max_ascent + it.max_descent, -1,
15637 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15639 else
15640 move_it_by_lines (&it, 1);
15643 /* It makes very little sense to make the new window start
15644 after point, as point won't be visible. If that's what
15645 the loop above finds, fall back on the candidate before
15646 or at point that is closest to the old window start. */
15647 if (CHARPOS (pos) > PT)
15648 pos = pos_before_pt;
15650 /* Set the window start there. */
15651 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15652 window_start_changed_p = true;
15656 return window_start_changed_p;
15660 /* Try cursor movement in case text has not changed in window WINDOW,
15661 with window start STARTP. Value is
15663 CURSOR_MOVEMENT_SUCCESS if successful
15665 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15667 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15668 display. *SCROLL_STEP is set to true, under certain circumstances, if
15669 we want to scroll as if scroll-step were set to 1. See the code.
15671 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15672 which case we have to abort this redisplay, and adjust matrices
15673 first. */
15675 enum
15677 CURSOR_MOVEMENT_SUCCESS,
15678 CURSOR_MOVEMENT_CANNOT_BE_USED,
15679 CURSOR_MOVEMENT_MUST_SCROLL,
15680 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15683 static int
15684 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15685 bool *scroll_step)
15687 struct window *w = XWINDOW (window);
15688 struct frame *f = XFRAME (w->frame);
15689 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15691 #ifdef GLYPH_DEBUG
15692 if (inhibit_try_cursor_movement)
15693 return rc;
15694 #endif
15696 /* Previously, there was a check for Lisp integer in the
15697 if-statement below. Now, this field is converted to
15698 ptrdiff_t, thus zero means invalid position in a buffer. */
15699 eassert (w->last_point > 0);
15700 /* Likewise there was a check whether window_end_vpos is nil or larger
15701 than the window. Now window_end_vpos is int and so never nil, but
15702 let's leave eassert to check whether it fits in the window. */
15703 eassert (!w->window_end_valid
15704 || w->window_end_vpos < w->current_matrix->nrows);
15706 /* Handle case where text has not changed, only point, and it has
15707 not moved off the frame. */
15708 if (/* Point may be in this window. */
15709 PT >= CHARPOS (startp)
15710 /* Selective display hasn't changed. */
15711 && !current_buffer->clip_changed
15712 /* Function force-mode-line-update is used to force a thorough
15713 redisplay. It sets either windows_or_buffers_changed or
15714 update_mode_lines. So don't take a shortcut here for these
15715 cases. */
15716 && !update_mode_lines
15717 && !windows_or_buffers_changed
15718 && !f->cursor_type_changed
15719 && NILP (Vshow_trailing_whitespace)
15720 /* This code is not used for mini-buffer for the sake of the case
15721 of redisplaying to replace an echo area message; since in
15722 that case the mini-buffer contents per se are usually
15723 unchanged. This code is of no real use in the mini-buffer
15724 since the handling of this_line_start_pos, etc., in redisplay
15725 handles the same cases. */
15726 && !EQ (window, minibuf_window)
15727 && (FRAME_WINDOW_P (f)
15728 || !overlay_arrow_in_current_buffer_p ()))
15730 int this_scroll_margin, top_scroll_margin;
15731 struct glyph_row *row = NULL;
15732 int frame_line_height = default_line_pixel_height (w);
15733 int window_total_lines
15734 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15736 #ifdef GLYPH_DEBUG
15737 debug_method_add (w, "cursor movement");
15738 #endif
15740 /* Scroll if point within this distance from the top or bottom
15741 of the window. This is a pixel value. */
15742 if (scroll_margin > 0)
15744 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15745 this_scroll_margin *= frame_line_height;
15747 else
15748 this_scroll_margin = 0;
15750 top_scroll_margin = this_scroll_margin;
15751 if (WINDOW_WANTS_HEADER_LINE_P (w))
15752 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15754 /* Start with the row the cursor was displayed during the last
15755 not paused redisplay. Give up if that row is not valid. */
15756 if (w->last_cursor_vpos < 0
15757 || w->last_cursor_vpos >= w->current_matrix->nrows)
15758 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15759 else
15761 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15762 if (row->mode_line_p)
15763 ++row;
15764 if (!row->enabled_p)
15765 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15768 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15770 bool scroll_p = false, must_scroll = false;
15771 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15773 if (PT > w->last_point)
15775 /* Point has moved forward. */
15776 while (MATRIX_ROW_END_CHARPOS (row) < PT
15777 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15779 eassert (row->enabled_p);
15780 ++row;
15783 /* If the end position of a row equals the start
15784 position of the next row, and PT is at that position,
15785 we would rather display cursor in the next line. */
15786 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15787 && MATRIX_ROW_END_CHARPOS (row) == PT
15788 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15789 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15790 && !cursor_row_p (row))
15791 ++row;
15793 /* If within the scroll margin, scroll. Note that
15794 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15795 the next line would be drawn, and that
15796 this_scroll_margin can be zero. */
15797 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15798 || PT > MATRIX_ROW_END_CHARPOS (row)
15799 /* Line is completely visible last line in window
15800 and PT is to be set in the next line. */
15801 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15802 && PT == MATRIX_ROW_END_CHARPOS (row)
15803 && !row->ends_at_zv_p
15804 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15805 scroll_p = true;
15807 else if (PT < w->last_point)
15809 /* Cursor has to be moved backward. Note that PT >=
15810 CHARPOS (startp) because of the outer if-statement. */
15811 while (!row->mode_line_p
15812 && (MATRIX_ROW_START_CHARPOS (row) > PT
15813 || (MATRIX_ROW_START_CHARPOS (row) == PT
15814 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15815 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15816 row > w->current_matrix->rows
15817 && (row-1)->ends_in_newline_from_string_p))))
15818 && (row->y > top_scroll_margin
15819 || CHARPOS (startp) == BEGV))
15821 eassert (row->enabled_p);
15822 --row;
15825 /* Consider the following case: Window starts at BEGV,
15826 there is invisible, intangible text at BEGV, so that
15827 display starts at some point START > BEGV. It can
15828 happen that we are called with PT somewhere between
15829 BEGV and START. Try to handle that case. */
15830 if (row < w->current_matrix->rows
15831 || row->mode_line_p)
15833 row = w->current_matrix->rows;
15834 if (row->mode_line_p)
15835 ++row;
15838 /* Due to newlines in overlay strings, we may have to
15839 skip forward over overlay strings. */
15840 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15841 && MATRIX_ROW_END_CHARPOS (row) == PT
15842 && !cursor_row_p (row))
15843 ++row;
15845 /* If within the scroll margin, scroll. */
15846 if (row->y < top_scroll_margin
15847 && CHARPOS (startp) != BEGV)
15848 scroll_p = true;
15850 else
15852 /* Cursor did not move. So don't scroll even if cursor line
15853 is partially visible, as it was so before. */
15854 rc = CURSOR_MOVEMENT_SUCCESS;
15857 if (PT < MATRIX_ROW_START_CHARPOS (row)
15858 || PT > MATRIX_ROW_END_CHARPOS (row))
15860 /* if PT is not in the glyph row, give up. */
15861 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15862 must_scroll = true;
15864 else if (rc != CURSOR_MOVEMENT_SUCCESS
15865 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15867 struct glyph_row *row1;
15869 /* If rows are bidi-reordered and point moved, back up
15870 until we find a row that does not belong to a
15871 continuation line. This is because we must consider
15872 all rows of a continued line as candidates for the
15873 new cursor positioning, since row start and end
15874 positions change non-linearly with vertical position
15875 in such rows. */
15876 /* FIXME: Revisit this when glyph ``spilling'' in
15877 continuation lines' rows is implemented for
15878 bidi-reordered rows. */
15879 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15880 MATRIX_ROW_CONTINUATION_LINE_P (row);
15881 --row)
15883 /* If we hit the beginning of the displayed portion
15884 without finding the first row of a continued
15885 line, give up. */
15886 if (row <= row1)
15888 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15889 break;
15891 eassert (row->enabled_p);
15894 if (must_scroll)
15896 else if (rc != CURSOR_MOVEMENT_SUCCESS
15897 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15898 /* Make sure this isn't a header line by any chance, since
15899 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15900 && !row->mode_line_p
15901 && make_cursor_line_fully_visible_p)
15903 if (PT == MATRIX_ROW_END_CHARPOS (row)
15904 && !row->ends_at_zv_p
15905 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15906 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15907 else if (row->height > window_box_height (w))
15909 /* If we end up in a partially visible line, let's
15910 make it fully visible, except when it's taller
15911 than the window, in which case we can't do much
15912 about it. */
15913 *scroll_step = true;
15914 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15916 else
15918 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15919 if (!cursor_row_fully_visible_p (w, false, true))
15920 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15921 else
15922 rc = CURSOR_MOVEMENT_SUCCESS;
15925 else if (scroll_p)
15926 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15927 else if (rc != CURSOR_MOVEMENT_SUCCESS
15928 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15930 /* With bidi-reordered rows, there could be more than
15931 one candidate row whose start and end positions
15932 occlude point. We need to let set_cursor_from_row
15933 find the best candidate. */
15934 /* FIXME: Revisit this when glyph ``spilling'' in
15935 continuation lines' rows is implemented for
15936 bidi-reordered rows. */
15937 bool rv = false;
15941 bool at_zv_p = false, exact_match_p = false;
15943 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15944 && PT <= MATRIX_ROW_END_CHARPOS (row)
15945 && cursor_row_p (row))
15946 rv |= set_cursor_from_row (w, row, w->current_matrix,
15947 0, 0, 0, 0);
15948 /* As soon as we've found the exact match for point,
15949 or the first suitable row whose ends_at_zv_p flag
15950 is set, we are done. */
15951 if (rv)
15953 at_zv_p = MATRIX_ROW (w->current_matrix,
15954 w->cursor.vpos)->ends_at_zv_p;
15955 if (!at_zv_p
15956 && w->cursor.hpos >= 0
15957 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15958 w->cursor.vpos))
15960 struct glyph_row *candidate =
15961 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15962 struct glyph *g =
15963 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15964 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15966 exact_match_p =
15967 (BUFFERP (g->object) && g->charpos == PT)
15968 || (NILP (g->object)
15969 && (g->charpos == PT
15970 || (g->charpos == 0 && endpos - 1 == PT)));
15972 if (at_zv_p || exact_match_p)
15974 rc = CURSOR_MOVEMENT_SUCCESS;
15975 break;
15978 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15979 break;
15980 ++row;
15982 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15983 || row->continued_p)
15984 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15985 || (MATRIX_ROW_START_CHARPOS (row) == PT
15986 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15987 /* If we didn't find any candidate rows, or exited the
15988 loop before all the candidates were examined, signal
15989 to the caller that this method failed. */
15990 if (rc != CURSOR_MOVEMENT_SUCCESS
15991 && !(rv
15992 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15993 && !row->continued_p))
15994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15995 else if (rv)
15996 rc = CURSOR_MOVEMENT_SUCCESS;
15998 else
16002 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16004 rc = CURSOR_MOVEMENT_SUCCESS;
16005 break;
16007 ++row;
16009 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16010 && MATRIX_ROW_START_CHARPOS (row) == PT
16011 && cursor_row_p (row));
16016 return rc;
16020 void
16021 set_vertical_scroll_bar (struct window *w)
16023 ptrdiff_t start, end, whole;
16025 /* Calculate the start and end positions for the current window.
16026 At some point, it would be nice to choose between scrollbars
16027 which reflect the whole buffer size, with special markers
16028 indicating narrowing, and scrollbars which reflect only the
16029 visible region.
16031 Note that mini-buffers sometimes aren't displaying any text. */
16032 if (!MINI_WINDOW_P (w)
16033 || (w == XWINDOW (minibuf_window)
16034 && NILP (echo_area_buffer[0])))
16036 struct buffer *buf = XBUFFER (w->contents);
16037 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16038 start = marker_position (w->start) - BUF_BEGV (buf);
16039 /* I don't think this is guaranteed to be right. For the
16040 moment, we'll pretend it is. */
16041 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16043 if (end < start)
16044 end = start;
16045 if (whole < (end - start))
16046 whole = end - start;
16048 else
16049 start = end = whole = 0;
16051 /* Indicate what this scroll bar ought to be displaying now. */
16052 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16053 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16054 (w, end - start, whole, start);
16058 void
16059 set_horizontal_scroll_bar (struct window *w)
16061 int start, end, whole, portion;
16063 if (!MINI_WINDOW_P (w)
16064 || (w == XWINDOW (minibuf_window)
16065 && NILP (echo_area_buffer[0])))
16067 struct buffer *b = XBUFFER (w->contents);
16068 struct buffer *old_buffer = NULL;
16069 struct it it;
16070 struct text_pos startp;
16072 if (b != current_buffer)
16074 old_buffer = current_buffer;
16075 set_buffer_internal (b);
16078 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16079 start_display (&it, w, startp);
16080 it.last_visible_x = INT_MAX;
16081 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16082 MOVE_TO_X | MOVE_TO_Y);
16083 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16084 window_box_height (w), -1,
16085 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16087 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16088 end = start + window_box_width (w, TEXT_AREA);
16089 portion = end - start;
16090 /* After enlarging a horizontally scrolled window such that it
16091 gets at least as wide as the text it contains, make sure that
16092 the thumb doesn't fill the entire scroll bar so we can still
16093 drag it back to see the entire text. */
16094 whole = max (whole, end);
16096 if (it.bidi_p)
16098 Lisp_Object pdir;
16100 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16101 if (EQ (pdir, Qright_to_left))
16103 start = whole - end;
16104 end = start + portion;
16108 if (old_buffer)
16109 set_buffer_internal (old_buffer);
16111 else
16112 start = end = whole = portion = 0;
16114 w->hscroll_whole = whole;
16116 /* Indicate what this scroll bar ought to be displaying now. */
16117 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16118 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16119 (w, portion, whole, start);
16123 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16124 selected_window is redisplayed.
16126 We can return without actually redisplaying the window if fonts has been
16127 changed on window's frame. In that case, redisplay_internal will retry.
16129 As one of the important parts of redisplaying a window, we need to
16130 decide whether the previous window-start position (stored in the
16131 window's w->start marker position) is still valid, and if it isn't,
16132 recompute it. Some details about that:
16134 . The previous window-start could be in a continuation line, in
16135 which case we need to recompute it when the window width
16136 changes. See compute_window_start_on_continuation_line and its
16137 call below.
16139 . The text that changed since last redisplay could include the
16140 previous window-start position. In that case, we try to salvage
16141 what we can from the current glyph matrix by calling
16142 try_scrolling, which see.
16144 . Some Emacs command could force us to use a specific window-start
16145 position by setting the window's force_start flag, or gently
16146 propose doing that by setting the window's optional_new_start
16147 flag. In these cases, we try using the specified start point if
16148 that succeeds (i.e. the window desired matrix is successfully
16149 recomputed, and point location is within the window). In case
16150 of optional_new_start, we first check if the specified start
16151 position is feasible, i.e. if it will allow point to be
16152 displayed in the window. If using the specified start point
16153 fails, e.g., if new fonts are needed to be loaded, we abort the
16154 redisplay cycle and leave it up to the next cycle to figure out
16155 things.
16157 . Note that the window's force_start flag is sometimes set by
16158 redisplay itself, when it decides that the previous window start
16159 point is fine and should be kept. Search for "goto force_start"
16160 below to see the details. Like the values of window-start
16161 specified outside of redisplay, these internally-deduced values
16162 are tested for feasibility, and ignored if found to be
16163 unfeasible.
16165 . Note that the function try_window, used to completely redisplay
16166 a window, accepts the window's start point as its argument.
16167 This is used several times in the redisplay code to control
16168 where the window start will be, according to user options such
16169 as scroll-conservatively, and also to ensure the screen line
16170 showing point will be fully (as opposed to partially) visible on
16171 display. */
16173 static void
16174 redisplay_window (Lisp_Object window, bool just_this_one_p)
16176 struct window *w = XWINDOW (window);
16177 struct frame *f = XFRAME (w->frame);
16178 struct buffer *buffer = XBUFFER (w->contents);
16179 struct buffer *old = current_buffer;
16180 struct text_pos lpoint, opoint, startp;
16181 bool update_mode_line;
16182 int tem;
16183 struct it it;
16184 /* Record it now because it's overwritten. */
16185 bool current_matrix_up_to_date_p = false;
16186 bool used_current_matrix_p = false;
16187 /* This is less strict than current_matrix_up_to_date_p.
16188 It indicates that the buffer contents and narrowing are unchanged. */
16189 bool buffer_unchanged_p = false;
16190 bool temp_scroll_step = false;
16191 ptrdiff_t count = SPECPDL_INDEX ();
16192 int rc;
16193 int centering_position = -1;
16194 bool last_line_misfit = false;
16195 ptrdiff_t beg_unchanged, end_unchanged;
16196 int frame_line_height;
16197 bool use_desired_matrix;
16198 void *itdata = NULL;
16200 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16201 opoint = lpoint;
16203 #ifdef GLYPH_DEBUG
16204 *w->desired_matrix->method = 0;
16205 #endif
16207 if (!just_this_one_p
16208 && REDISPLAY_SOME_P ()
16209 && !w->redisplay
16210 && !w->update_mode_line
16211 && !f->face_change
16212 && !f->redisplay
16213 && !buffer->text->redisplay
16214 && BUF_PT (buffer) == w->last_point)
16215 return;
16217 /* Make sure that both W's markers are valid. */
16218 eassert (XMARKER (w->start)->buffer == buffer);
16219 eassert (XMARKER (w->pointm)->buffer == buffer);
16221 /* We come here again if we need to run window-text-change-functions
16222 below. */
16223 restart:
16224 reconsider_clip_changes (w);
16225 frame_line_height = default_line_pixel_height (w);
16227 /* Has the mode line to be updated? */
16228 update_mode_line = (w->update_mode_line
16229 || update_mode_lines
16230 || buffer->clip_changed
16231 || buffer->prevent_redisplay_optimizations_p);
16233 if (!just_this_one_p)
16234 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16235 cleverly elsewhere. */
16236 w->must_be_updated_p = true;
16238 if (MINI_WINDOW_P (w))
16240 if (w == XWINDOW (echo_area_window)
16241 && !NILP (echo_area_buffer[0]))
16243 if (update_mode_line)
16244 /* We may have to update a tty frame's menu bar or a
16245 tool-bar. Example `M-x C-h C-h C-g'. */
16246 goto finish_menu_bars;
16247 else
16248 /* We've already displayed the echo area glyphs in this window. */
16249 goto finish_scroll_bars;
16251 else if ((w != XWINDOW (minibuf_window)
16252 || minibuf_level == 0)
16253 /* When buffer is nonempty, redisplay window normally. */
16254 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16255 /* Quail displays non-mini buffers in minibuffer window.
16256 In that case, redisplay the window normally. */
16257 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16259 /* W is a mini-buffer window, but it's not active, so clear
16260 it. */
16261 int yb = window_text_bottom_y (w);
16262 struct glyph_row *row;
16263 int y;
16265 for (y = 0, row = w->desired_matrix->rows;
16266 y < yb;
16267 y += row->height, ++row)
16268 blank_row (w, row, y);
16269 goto finish_scroll_bars;
16272 clear_glyph_matrix (w->desired_matrix);
16275 /* Otherwise set up data on this window; select its buffer and point
16276 value. */
16277 /* Really select the buffer, for the sake of buffer-local
16278 variables. */
16279 set_buffer_internal_1 (XBUFFER (w->contents));
16281 current_matrix_up_to_date_p
16282 = (w->window_end_valid
16283 && !current_buffer->clip_changed
16284 && !current_buffer->prevent_redisplay_optimizations_p
16285 && !window_outdated (w));
16287 /* Run the window-text-change-functions
16288 if it is possible that the text on the screen has changed
16289 (either due to modification of the text, or any other reason). */
16290 if (!current_matrix_up_to_date_p
16291 && !NILP (Vwindow_text_change_functions))
16293 safe_run_hooks (Qwindow_text_change_functions);
16294 goto restart;
16297 beg_unchanged = BEG_UNCHANGED;
16298 end_unchanged = END_UNCHANGED;
16300 SET_TEXT_POS (opoint, PT, PT_BYTE);
16302 specbind (Qinhibit_point_motion_hooks, Qt);
16304 buffer_unchanged_p
16305 = (w->window_end_valid
16306 && !current_buffer->clip_changed
16307 && !window_outdated (w));
16309 /* When windows_or_buffers_changed is non-zero, we can't rely
16310 on the window end being valid, so set it to zero there. */
16311 if (windows_or_buffers_changed)
16313 /* If window starts on a continuation line, maybe adjust the
16314 window start in case the window's width changed. */
16315 if (XMARKER (w->start)->buffer == current_buffer)
16316 compute_window_start_on_continuation_line (w);
16318 w->window_end_valid = false;
16319 /* If so, we also can't rely on current matrix
16320 and should not fool try_cursor_movement below. */
16321 current_matrix_up_to_date_p = false;
16324 /* Some sanity checks. */
16325 CHECK_WINDOW_END (w);
16326 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16327 emacs_abort ();
16328 if (BYTEPOS (opoint) < CHARPOS (opoint))
16329 emacs_abort ();
16331 if (mode_line_update_needed (w))
16332 update_mode_line = true;
16334 /* Point refers normally to the selected window. For any other
16335 window, set up appropriate value. */
16336 if (!EQ (window, selected_window))
16338 ptrdiff_t new_pt = marker_position (w->pointm);
16339 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16341 if (new_pt < BEGV)
16343 new_pt = BEGV;
16344 new_pt_byte = BEGV_BYTE;
16345 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16347 else if (new_pt > (ZV - 1))
16349 new_pt = ZV;
16350 new_pt_byte = ZV_BYTE;
16351 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16354 /* We don't use SET_PT so that the point-motion hooks don't run. */
16355 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16358 /* If any of the character widths specified in the display table
16359 have changed, invalidate the width run cache. It's true that
16360 this may be a bit late to catch such changes, but the rest of
16361 redisplay goes (non-fatally) haywire when the display table is
16362 changed, so why should we worry about doing any better? */
16363 if (current_buffer->width_run_cache
16364 || (current_buffer->base_buffer
16365 && current_buffer->base_buffer->width_run_cache))
16367 struct Lisp_Char_Table *disptab = buffer_display_table ();
16369 if (! disptab_matches_widthtab
16370 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16372 struct buffer *buf = current_buffer;
16374 if (buf->base_buffer)
16375 buf = buf->base_buffer;
16376 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16377 recompute_width_table (current_buffer, disptab);
16381 /* If window-start is screwed up, choose a new one. */
16382 if (XMARKER (w->start)->buffer != current_buffer)
16383 goto recenter;
16385 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16387 /* If someone specified a new starting point but did not insist,
16388 check whether it can be used. */
16389 if ((w->optional_new_start || window_frozen_p (w))
16390 && CHARPOS (startp) >= BEGV
16391 && CHARPOS (startp) <= ZV)
16393 ptrdiff_t it_charpos;
16395 w->optional_new_start = false;
16396 start_display (&it, w, startp);
16397 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16398 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16399 /* Record IT's position now, since line_bottom_y might change
16400 that. */
16401 it_charpos = IT_CHARPOS (it);
16402 /* Make sure we set the force_start flag only if the cursor row
16403 will be fully visible. Otherwise, the code under force_start
16404 label below will try to move point back into view, which is
16405 not what the code which sets optional_new_start wants. */
16406 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16407 && !w->force_start)
16409 if (it_charpos == PT)
16410 w->force_start = true;
16411 /* IT may overshoot PT if text at PT is invisible. */
16412 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16413 w->force_start = true;
16414 #ifdef GLYPH_DEBUG
16415 if (w->force_start)
16417 if (window_frozen_p (w))
16418 debug_method_add (w, "set force_start from frozen window start");
16419 else
16420 debug_method_add (w, "set force_start from optional_new_start");
16422 #endif
16426 force_start:
16428 /* Handle case where place to start displaying has been specified,
16429 unless the specified location is outside the accessible range. */
16430 if (w->force_start)
16432 /* We set this later on if we have to adjust point. */
16433 int new_vpos = -1;
16435 w->force_start = false;
16436 w->vscroll = 0;
16437 w->window_end_valid = false;
16439 /* Forget any recorded base line for line number display. */
16440 if (!buffer_unchanged_p)
16441 w->base_line_number = 0;
16443 /* Redisplay the mode line. Select the buffer properly for that.
16444 Also, run the hook window-scroll-functions
16445 because we have scrolled. */
16446 /* Note, we do this after clearing force_start because
16447 if there's an error, it is better to forget about force_start
16448 than to get into an infinite loop calling the hook functions
16449 and having them get more errors. */
16450 if (!update_mode_line
16451 || ! NILP (Vwindow_scroll_functions))
16453 update_mode_line = true;
16454 w->update_mode_line = true;
16455 startp = run_window_scroll_functions (window, startp);
16458 if (CHARPOS (startp) < BEGV)
16459 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16460 else if (CHARPOS (startp) > ZV)
16461 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16463 /* Redisplay, then check if cursor has been set during the
16464 redisplay. Give up if new fonts were loaded. */
16465 /* We used to issue a CHECK_MARGINS argument to try_window here,
16466 but this causes scrolling to fail when point begins inside
16467 the scroll margin (bug#148) -- cyd */
16468 if (!try_window (window, startp, 0))
16470 w->force_start = true;
16471 clear_glyph_matrix (w->desired_matrix);
16472 goto need_larger_matrices;
16475 if (w->cursor.vpos < 0)
16477 /* If point does not appear, try to move point so it does
16478 appear. The desired matrix has been built above, so we
16479 can use it here. First see if point is in invisible
16480 text, and if so, move it to the first visible buffer
16481 position past that. */
16482 struct glyph_row *r = NULL;
16483 Lisp_Object invprop =
16484 get_char_property_and_overlay (make_number (PT), Qinvisible,
16485 Qnil, NULL);
16487 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16489 ptrdiff_t alt_pt;
16490 Lisp_Object invprop_end =
16491 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16492 Qnil, Qnil);
16494 if (NATNUMP (invprop_end))
16495 alt_pt = XFASTINT (invprop_end);
16496 else
16497 alt_pt = ZV;
16498 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16499 NULL, 0);
16501 if (r)
16502 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16503 else /* Give up and just move to the middle of the window. */
16504 new_vpos = window_box_height (w) / 2;
16507 if (!cursor_row_fully_visible_p (w, false, false))
16509 /* Point does appear, but on a line partly visible at end of window.
16510 Move it back to a fully-visible line. */
16511 new_vpos = window_box_height (w);
16512 /* But if window_box_height suggests a Y coordinate that is
16513 not less than we already have, that line will clearly not
16514 be fully visible, so give up and scroll the display.
16515 This can happen when the default face uses a font whose
16516 dimensions are different from the frame's default
16517 font. */
16518 if (new_vpos >= w->cursor.y)
16520 w->cursor.vpos = -1;
16521 clear_glyph_matrix (w->desired_matrix);
16522 goto try_to_scroll;
16525 else if (w->cursor.vpos >= 0)
16527 /* Some people insist on not letting point enter the scroll
16528 margin, even though this part handles windows that didn't
16529 scroll at all. */
16530 int window_total_lines
16531 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16532 int margin = min (scroll_margin, window_total_lines / 4);
16533 int pixel_margin = margin * frame_line_height;
16534 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16536 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16537 below, which finds the row to move point to, advances by
16538 the Y coordinate of the _next_ row, see the definition of
16539 MATRIX_ROW_BOTTOM_Y. */
16540 if (w->cursor.vpos < margin + header_line)
16542 w->cursor.vpos = -1;
16543 clear_glyph_matrix (w->desired_matrix);
16544 goto try_to_scroll;
16546 else
16548 int window_height = window_box_height (w);
16550 if (header_line)
16551 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16552 if (w->cursor.y >= window_height - pixel_margin)
16554 w->cursor.vpos = -1;
16555 clear_glyph_matrix (w->desired_matrix);
16556 goto try_to_scroll;
16561 /* If we need to move point for either of the above reasons,
16562 now actually do it. */
16563 if (new_vpos >= 0)
16565 struct glyph_row *row;
16567 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16568 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16569 ++row;
16571 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16572 MATRIX_ROW_START_BYTEPOS (row));
16574 if (w != XWINDOW (selected_window))
16575 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16576 else if (current_buffer == old)
16577 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16579 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16581 /* Re-run pre-redisplay-function so it can update the region
16582 according to the new position of point. */
16583 /* Other than the cursor, w's redisplay is done so we can set its
16584 redisplay to false. Also the buffer's redisplay can be set to
16585 false, since propagate_buffer_redisplay should have already
16586 propagated its info to `w' anyway. */
16587 w->redisplay = false;
16588 XBUFFER (w->contents)->text->redisplay = false;
16589 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16591 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16593 /* pre-redisplay-function made changes (e.g. move the region)
16594 that require another round of redisplay. */
16595 clear_glyph_matrix (w->desired_matrix);
16596 if (!try_window (window, startp, 0))
16597 goto need_larger_matrices;
16600 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16602 clear_glyph_matrix (w->desired_matrix);
16603 goto try_to_scroll;
16606 #ifdef GLYPH_DEBUG
16607 debug_method_add (w, "forced window start");
16608 #endif
16609 goto done;
16612 /* Handle case where text has not changed, only point, and it has
16613 not moved off the frame, and we are not retrying after hscroll.
16614 (current_matrix_up_to_date_p is true when retrying.) */
16615 if (current_matrix_up_to_date_p
16616 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16617 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16619 switch (rc)
16621 case CURSOR_MOVEMENT_SUCCESS:
16622 used_current_matrix_p = true;
16623 goto done;
16625 case CURSOR_MOVEMENT_MUST_SCROLL:
16626 goto try_to_scroll;
16628 default:
16629 emacs_abort ();
16632 /* If current starting point was originally the beginning of a line
16633 but no longer is, find a new starting point. */
16634 else if (w->start_at_line_beg
16635 && !(CHARPOS (startp) <= BEGV
16636 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16638 #ifdef GLYPH_DEBUG
16639 debug_method_add (w, "recenter 1");
16640 #endif
16641 goto recenter;
16644 /* Try scrolling with try_window_id. Value is > 0 if update has
16645 been done, it is -1 if we know that the same window start will
16646 not work. It is 0 if unsuccessful for some other reason. */
16647 else if ((tem = try_window_id (w)) != 0)
16649 #ifdef GLYPH_DEBUG
16650 debug_method_add (w, "try_window_id %d", tem);
16651 #endif
16653 if (f->fonts_changed)
16654 goto need_larger_matrices;
16655 if (tem > 0)
16656 goto done;
16658 /* Otherwise try_window_id has returned -1 which means that we
16659 don't want the alternative below this comment to execute. */
16661 else if (CHARPOS (startp) >= BEGV
16662 && CHARPOS (startp) <= ZV
16663 && PT >= CHARPOS (startp)
16664 && (CHARPOS (startp) < ZV
16665 /* Avoid starting at end of buffer. */
16666 || CHARPOS (startp) == BEGV
16667 || !window_outdated (w)))
16669 int d1, d2, d5, d6;
16670 int rtop, rbot;
16672 /* If first window line is a continuation line, and window start
16673 is inside the modified region, but the first change is before
16674 current window start, we must select a new window start.
16676 However, if this is the result of a down-mouse event (e.g. by
16677 extending the mouse-drag-overlay), we don't want to select a
16678 new window start, since that would change the position under
16679 the mouse, resulting in an unwanted mouse-movement rather
16680 than a simple mouse-click. */
16681 if (!w->start_at_line_beg
16682 && NILP (do_mouse_tracking)
16683 && CHARPOS (startp) > BEGV
16684 && CHARPOS (startp) > BEG + beg_unchanged
16685 && CHARPOS (startp) <= Z - end_unchanged
16686 /* Even if w->start_at_line_beg is nil, a new window may
16687 start at a line_beg, since that's how set_buffer_window
16688 sets it. So, we need to check the return value of
16689 compute_window_start_on_continuation_line. (See also
16690 bug#197). */
16691 && XMARKER (w->start)->buffer == current_buffer
16692 && compute_window_start_on_continuation_line (w)
16693 /* It doesn't make sense to force the window start like we
16694 do at label force_start if it is already known that point
16695 will not be fully visible in the resulting window, because
16696 doing so will move point from its correct position
16697 instead of scrolling the window to bring point into view.
16698 See bug#9324. */
16699 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16700 /* A very tall row could need more than the window height,
16701 in which case we accept that it is partially visible. */
16702 && (rtop != 0) == (rbot != 0))
16704 w->force_start = true;
16705 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16706 #ifdef GLYPH_DEBUG
16707 debug_method_add (w, "recomputed window start in continuation line");
16708 #endif
16709 goto force_start;
16712 #ifdef GLYPH_DEBUG
16713 debug_method_add (w, "same window start");
16714 #endif
16716 /* Try to redisplay starting at same place as before.
16717 If point has not moved off frame, accept the results. */
16718 if (!current_matrix_up_to_date_p
16719 /* Don't use try_window_reusing_current_matrix in this case
16720 because a window scroll function can have changed the
16721 buffer. */
16722 || !NILP (Vwindow_scroll_functions)
16723 || MINI_WINDOW_P (w)
16724 || !(used_current_matrix_p
16725 = try_window_reusing_current_matrix (w)))
16727 IF_DEBUG (debug_method_add (w, "1"));
16728 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16729 /* -1 means we need to scroll.
16730 0 means we need new matrices, but fonts_changed
16731 is set in that case, so we will detect it below. */
16732 goto try_to_scroll;
16735 if (f->fonts_changed)
16736 goto need_larger_matrices;
16738 if (w->cursor.vpos >= 0)
16740 if (!just_this_one_p
16741 || current_buffer->clip_changed
16742 || BEG_UNCHANGED < CHARPOS (startp))
16743 /* Forget any recorded base line for line number display. */
16744 w->base_line_number = 0;
16746 if (!cursor_row_fully_visible_p (w, true, false))
16748 clear_glyph_matrix (w->desired_matrix);
16749 last_line_misfit = true;
16751 /* Drop through and scroll. */
16752 else
16753 goto done;
16755 else
16756 clear_glyph_matrix (w->desired_matrix);
16759 try_to_scroll:
16761 /* Redisplay the mode line. Select the buffer properly for that. */
16762 if (!update_mode_line)
16764 update_mode_line = true;
16765 w->update_mode_line = true;
16768 /* Try to scroll by specified few lines. */
16769 if ((scroll_conservatively
16770 || emacs_scroll_step
16771 || temp_scroll_step
16772 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16773 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16774 && CHARPOS (startp) >= BEGV
16775 && CHARPOS (startp) <= ZV)
16777 /* The function returns -1 if new fonts were loaded, 1 if
16778 successful, 0 if not successful. */
16779 int ss = try_scrolling (window, just_this_one_p,
16780 scroll_conservatively,
16781 emacs_scroll_step,
16782 temp_scroll_step, last_line_misfit);
16783 switch (ss)
16785 case SCROLLING_SUCCESS:
16786 goto done;
16788 case SCROLLING_NEED_LARGER_MATRICES:
16789 goto need_larger_matrices;
16791 case SCROLLING_FAILED:
16792 break;
16794 default:
16795 emacs_abort ();
16799 /* Finally, just choose a place to start which positions point
16800 according to user preferences. */
16802 recenter:
16804 #ifdef GLYPH_DEBUG
16805 debug_method_add (w, "recenter");
16806 #endif
16808 /* Forget any previously recorded base line for line number display. */
16809 if (!buffer_unchanged_p)
16810 w->base_line_number = 0;
16812 /* Determine the window start relative to point. */
16813 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16814 it.current_y = it.last_visible_y;
16815 if (centering_position < 0)
16817 int window_total_lines
16818 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16819 int margin
16820 = scroll_margin > 0
16821 ? min (scroll_margin, window_total_lines / 4)
16822 : 0;
16823 ptrdiff_t margin_pos = CHARPOS (startp);
16824 Lisp_Object aggressive;
16825 bool scrolling_up;
16827 /* If there is a scroll margin at the top of the window, find
16828 its character position. */
16829 if (margin
16830 /* Cannot call start_display if startp is not in the
16831 accessible region of the buffer. This can happen when we
16832 have just switched to a different buffer and/or changed
16833 its restriction. In that case, startp is initialized to
16834 the character position 1 (BEGV) because we did not yet
16835 have chance to display the buffer even once. */
16836 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16838 struct it it1;
16839 void *it1data = NULL;
16841 SAVE_IT (it1, it, it1data);
16842 start_display (&it1, w, startp);
16843 move_it_vertically (&it1, margin * frame_line_height);
16844 margin_pos = IT_CHARPOS (it1);
16845 RESTORE_IT (&it, &it, it1data);
16847 scrolling_up = PT > margin_pos;
16848 aggressive =
16849 scrolling_up
16850 ? BVAR (current_buffer, scroll_up_aggressively)
16851 : BVAR (current_buffer, scroll_down_aggressively);
16853 if (!MINI_WINDOW_P (w)
16854 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16856 int pt_offset = 0;
16858 /* Setting scroll-conservatively overrides
16859 scroll-*-aggressively. */
16860 if (!scroll_conservatively && NUMBERP (aggressive))
16862 double float_amount = XFLOATINT (aggressive);
16864 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16865 if (pt_offset == 0 && float_amount > 0)
16866 pt_offset = 1;
16867 if (pt_offset && margin > 0)
16868 margin -= 1;
16870 /* Compute how much to move the window start backward from
16871 point so that point will be displayed where the user
16872 wants it. */
16873 if (scrolling_up)
16875 centering_position = it.last_visible_y;
16876 if (pt_offset)
16877 centering_position -= pt_offset;
16878 centering_position -=
16879 (frame_line_height * (1 + margin + last_line_misfit)
16880 + WINDOW_HEADER_LINE_HEIGHT (w));
16881 /* Don't let point enter the scroll margin near top of
16882 the window. */
16883 if (centering_position < margin * frame_line_height)
16884 centering_position = margin * frame_line_height;
16886 else
16887 centering_position = margin * frame_line_height + pt_offset;
16889 else
16890 /* Set the window start half the height of the window backward
16891 from point. */
16892 centering_position = window_box_height (w) / 2;
16894 move_it_vertically_backward (&it, centering_position);
16896 eassert (IT_CHARPOS (it) >= BEGV);
16898 /* The function move_it_vertically_backward may move over more
16899 than the specified y-distance. If it->w is small, e.g. a
16900 mini-buffer window, we may end up in front of the window's
16901 display area. Start displaying at the start of the line
16902 containing PT in this case. */
16903 if (it.current_y <= 0)
16905 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16906 move_it_vertically_backward (&it, 0);
16907 it.current_y = 0;
16910 it.current_x = it.hpos = 0;
16912 /* Set the window start position here explicitly, to avoid an
16913 infinite loop in case the functions in window-scroll-functions
16914 get errors. */
16915 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16917 /* Run scroll hooks. */
16918 startp = run_window_scroll_functions (window, it.current.pos);
16920 /* We invoke try_window and try_window_reusing_current_matrix below,
16921 and they manipulate the bidi cache. Save and restore the cache
16922 state of our iterator, so we could continue using it after that. */
16923 itdata = bidi_shelve_cache ();
16925 /* Redisplay the window. */
16926 use_desired_matrix = false;
16927 if (!current_matrix_up_to_date_p
16928 || windows_or_buffers_changed
16929 || f->cursor_type_changed
16930 /* Don't use try_window_reusing_current_matrix in this case
16931 because it can have changed the buffer. */
16932 || !NILP (Vwindow_scroll_functions)
16933 || !just_this_one_p
16934 || MINI_WINDOW_P (w)
16935 || !(used_current_matrix_p
16936 = try_window_reusing_current_matrix (w)))
16937 use_desired_matrix = (try_window (window, startp, 0) == 1);
16939 bidi_unshelve_cache (itdata, false);
16941 /* If new fonts have been loaded (due to fontsets), give up. We
16942 have to start a new redisplay since we need to re-adjust glyph
16943 matrices. */
16944 if (f->fonts_changed)
16945 goto need_larger_matrices;
16947 /* If cursor did not appear assume that the middle of the window is
16948 in the first line of the window. Do it again with the next line.
16949 (Imagine a window of height 100, displaying two lines of height
16950 60. Moving back 50 from it->last_visible_y will end in the first
16951 line.) */
16952 if (w->cursor.vpos < 0)
16954 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16956 clear_glyph_matrix (w->desired_matrix);
16957 move_it_by_lines (&it, 1);
16958 try_window (window, it.current.pos, 0);
16960 else if (PT < IT_CHARPOS (it))
16962 clear_glyph_matrix (w->desired_matrix);
16963 move_it_by_lines (&it, -1);
16964 try_window (window, it.current.pos, 0);
16966 else if (scroll_conservatively > SCROLL_LIMIT
16967 && (it.method == GET_FROM_STRING
16968 || overlay_touches_p (IT_CHARPOS (it)))
16969 && IT_CHARPOS (it) < ZV)
16971 /* If the window starts with a before-string that spans more
16972 than one screen line, using that position to display the
16973 window might fail to bring point into the view, because
16974 start_display will always start by displaying the string,
16975 whereas the code above determines where to set w->start
16976 by the buffer position of the place where it takes screen
16977 coordinates. Try to recover by finding the next screen
16978 line that displays buffer text. */
16979 ptrdiff_t pos0 = IT_CHARPOS (it);
16981 clear_glyph_matrix (w->desired_matrix);
16982 do {
16983 move_it_by_lines (&it, 1);
16984 } while (IT_CHARPOS (it) == pos0);
16985 try_window (window, it.current.pos, 0);
16987 else
16989 /* Not much we can do about it. */
16993 /* Consider the following case: Window starts at BEGV, there is
16994 invisible, intangible text at BEGV, so that display starts at
16995 some point START > BEGV. It can happen that we are called with
16996 PT somewhere between BEGV and START. Try to handle that case,
16997 and similar ones. */
16998 if (w->cursor.vpos < 0)
17000 /* Prefer the desired matrix to the current matrix, if possible,
17001 in the fallback calculations below. This is because using
17002 the current matrix might completely goof, e.g. if its first
17003 row is after point. */
17004 struct glyph_matrix *matrix =
17005 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17006 /* First, try locating the proper glyph row for PT. */
17007 struct glyph_row *row =
17008 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17010 /* Sometimes point is at the beginning of invisible text that is
17011 before the 1st character displayed in the row. In that case,
17012 row_containing_pos fails to find the row, because no glyphs
17013 with appropriate buffer positions are present in the row.
17014 Therefore, we next try to find the row which shows the 1st
17015 position after the invisible text. */
17016 if (!row)
17018 Lisp_Object val =
17019 get_char_property_and_overlay (make_number (PT), Qinvisible,
17020 Qnil, NULL);
17022 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17024 ptrdiff_t alt_pos;
17025 Lisp_Object invis_end =
17026 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17027 Qnil, Qnil);
17029 if (NATNUMP (invis_end))
17030 alt_pos = XFASTINT (invis_end);
17031 else
17032 alt_pos = ZV;
17033 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17036 /* Finally, fall back on the first row of the window after the
17037 header line (if any). This is slightly better than not
17038 displaying the cursor at all. */
17039 if (!row)
17041 row = matrix->rows;
17042 if (row->mode_line_p)
17043 ++row;
17045 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17048 if (!cursor_row_fully_visible_p (w, false, false))
17050 /* If vscroll is enabled, disable it and try again. */
17051 if (w->vscroll)
17053 w->vscroll = 0;
17054 clear_glyph_matrix (w->desired_matrix);
17055 goto recenter;
17058 /* Users who set scroll-conservatively to a large number want
17059 point just above/below the scroll margin. If we ended up
17060 with point's row partially visible, move the window start to
17061 make that row fully visible and out of the margin. */
17062 if (scroll_conservatively > SCROLL_LIMIT)
17064 int window_total_lines
17065 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17066 int margin =
17067 scroll_margin > 0
17068 ? min (scroll_margin, window_total_lines / 4)
17069 : 0;
17070 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17072 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17073 clear_glyph_matrix (w->desired_matrix);
17074 if (1 == try_window (window, it.current.pos,
17075 TRY_WINDOW_CHECK_MARGINS))
17076 goto done;
17079 /* If centering point failed to make the whole line visible,
17080 put point at the top instead. That has to make the whole line
17081 visible, if it can be done. */
17082 if (centering_position == 0)
17083 goto done;
17085 clear_glyph_matrix (w->desired_matrix);
17086 centering_position = 0;
17087 goto recenter;
17090 done:
17092 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17093 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17094 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17096 /* Display the mode line, if we must. */
17097 if ((update_mode_line
17098 /* If window not full width, must redo its mode line
17099 if (a) the window to its side is being redone and
17100 (b) we do a frame-based redisplay. This is a consequence
17101 of how inverted lines are drawn in frame-based redisplay. */
17102 || (!just_this_one_p
17103 && !FRAME_WINDOW_P (f)
17104 && !WINDOW_FULL_WIDTH_P (w))
17105 /* Line number to display. */
17106 || w->base_line_pos > 0
17107 /* Column number is displayed and different from the one displayed. */
17108 || (w->column_number_displayed != -1
17109 && (w->column_number_displayed != current_column ())))
17110 /* This means that the window has a mode line. */
17111 && (WINDOW_WANTS_MODELINE_P (w)
17112 || WINDOW_WANTS_HEADER_LINE_P (w)))
17115 display_mode_lines (w);
17117 /* If mode line height has changed, arrange for a thorough
17118 immediate redisplay using the correct mode line height. */
17119 if (WINDOW_WANTS_MODELINE_P (w)
17120 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17122 f->fonts_changed = true;
17123 w->mode_line_height = -1;
17124 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17125 = DESIRED_MODE_LINE_HEIGHT (w);
17128 /* If header line height has changed, arrange for a thorough
17129 immediate redisplay using the correct header line height. */
17130 if (WINDOW_WANTS_HEADER_LINE_P (w)
17131 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17133 f->fonts_changed = true;
17134 w->header_line_height = -1;
17135 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17136 = DESIRED_HEADER_LINE_HEIGHT (w);
17139 if (f->fonts_changed)
17140 goto need_larger_matrices;
17143 if (!line_number_displayed && w->base_line_pos != -1)
17145 w->base_line_pos = 0;
17146 w->base_line_number = 0;
17149 finish_menu_bars:
17151 /* When we reach a frame's selected window, redo the frame's menu
17152 bar and the frame's title. */
17153 if (update_mode_line
17154 && EQ (FRAME_SELECTED_WINDOW (f), window))
17156 bool redisplay_menu_p;
17158 if (FRAME_WINDOW_P (f))
17160 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17161 || defined (HAVE_NS) || defined (USE_GTK)
17162 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17163 #else
17164 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17165 #endif
17167 else
17168 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17170 if (redisplay_menu_p)
17171 display_menu_bar (w);
17173 #ifdef HAVE_WINDOW_SYSTEM
17174 if (FRAME_WINDOW_P (f))
17176 #if defined (USE_GTK) || defined (HAVE_NS)
17177 if (FRAME_EXTERNAL_TOOL_BAR (f))
17178 redisplay_tool_bar (f);
17179 #else
17180 if (WINDOWP (f->tool_bar_window)
17181 && (FRAME_TOOL_BAR_LINES (f) > 0
17182 || !NILP (Vauto_resize_tool_bars))
17183 && redisplay_tool_bar (f))
17184 ignore_mouse_drag_p = true;
17185 #endif
17187 x_consider_frame_title (w->frame);
17188 #endif
17191 #ifdef HAVE_WINDOW_SYSTEM
17192 if (FRAME_WINDOW_P (f)
17193 && update_window_fringes (w, (just_this_one_p
17194 || (!used_current_matrix_p && !overlay_arrow_seen)
17195 || w->pseudo_window_p)))
17197 update_begin (f);
17198 block_input ();
17199 if (draw_window_fringes (w, true))
17201 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17202 x_draw_right_divider (w);
17203 else
17204 x_draw_vertical_border (w);
17206 unblock_input ();
17207 update_end (f);
17210 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17211 x_draw_bottom_divider (w);
17212 #endif /* HAVE_WINDOW_SYSTEM */
17214 /* We go to this label, with fonts_changed set, if it is
17215 necessary to try again using larger glyph matrices.
17216 We have to redeem the scroll bar even in this case,
17217 because the loop in redisplay_internal expects that. */
17218 need_larger_matrices:
17220 finish_scroll_bars:
17222 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17224 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17225 /* Set the thumb's position and size. */
17226 set_vertical_scroll_bar (w);
17228 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17229 /* Set the thumb's position and size. */
17230 set_horizontal_scroll_bar (w);
17232 /* Note that we actually used the scroll bar attached to this
17233 window, so it shouldn't be deleted at the end of redisplay. */
17234 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17235 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17238 /* Restore current_buffer and value of point in it. The window
17239 update may have changed the buffer, so first make sure `opoint'
17240 is still valid (Bug#6177). */
17241 if (CHARPOS (opoint) < BEGV)
17242 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17243 else if (CHARPOS (opoint) > ZV)
17244 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17245 else
17246 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17248 set_buffer_internal_1 (old);
17249 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17250 shorter. This can be caused by log truncation in *Messages*. */
17251 if (CHARPOS (lpoint) <= ZV)
17252 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17254 unbind_to (count, Qnil);
17258 /* Build the complete desired matrix of WINDOW with a window start
17259 buffer position POS.
17261 Value is 1 if successful. It is zero if fonts were loaded during
17262 redisplay which makes re-adjusting glyph matrices necessary, and -1
17263 if point would appear in the scroll margins.
17264 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17265 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17266 set in FLAGS.) */
17269 try_window (Lisp_Object window, struct text_pos pos, int flags)
17271 struct window *w = XWINDOW (window);
17272 struct it it;
17273 struct glyph_row *last_text_row = NULL;
17274 struct frame *f = XFRAME (w->frame);
17275 int frame_line_height = default_line_pixel_height (w);
17277 /* Make POS the new window start. */
17278 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17280 /* Mark cursor position as unknown. No overlay arrow seen. */
17281 w->cursor.vpos = -1;
17282 overlay_arrow_seen = false;
17284 /* Initialize iterator and info to start at POS. */
17285 start_display (&it, w, pos);
17286 it.glyph_row->reversed_p = false;
17288 /* Display all lines of W. */
17289 while (it.current_y < it.last_visible_y)
17291 if (display_line (&it))
17292 last_text_row = it.glyph_row - 1;
17293 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17294 return 0;
17297 /* Don't let the cursor end in the scroll margins. */
17298 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17299 && !MINI_WINDOW_P (w))
17301 int this_scroll_margin;
17302 int window_total_lines
17303 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17305 if (scroll_margin > 0)
17307 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17308 this_scroll_margin *= frame_line_height;
17310 else
17311 this_scroll_margin = 0;
17313 if ((w->cursor.y >= 0 /* not vscrolled */
17314 && w->cursor.y < this_scroll_margin
17315 && CHARPOS (pos) > BEGV
17316 && IT_CHARPOS (it) < ZV)
17317 /* rms: considering make_cursor_line_fully_visible_p here
17318 seems to give wrong results. We don't want to recenter
17319 when the last line is partly visible, we want to allow
17320 that case to be handled in the usual way. */
17321 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17323 w->cursor.vpos = -1;
17324 clear_glyph_matrix (w->desired_matrix);
17325 return -1;
17329 /* If bottom moved off end of frame, change mode line percentage. */
17330 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17331 w->update_mode_line = true;
17333 /* Set window_end_pos to the offset of the last character displayed
17334 on the window from the end of current_buffer. Set
17335 window_end_vpos to its row number. */
17336 if (last_text_row)
17338 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17339 adjust_window_ends (w, last_text_row, false);
17340 eassert
17341 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17342 w->window_end_vpos)));
17344 else
17346 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17347 w->window_end_pos = Z - ZV;
17348 w->window_end_vpos = 0;
17351 /* But that is not valid info until redisplay finishes. */
17352 w->window_end_valid = false;
17353 return 1;
17358 /************************************************************************
17359 Window redisplay reusing current matrix when buffer has not changed
17360 ************************************************************************/
17362 /* Try redisplay of window W showing an unchanged buffer with a
17363 different window start than the last time it was displayed by
17364 reusing its current matrix. Value is true if successful.
17365 W->start is the new window start. */
17367 static bool
17368 try_window_reusing_current_matrix (struct window *w)
17370 struct frame *f = XFRAME (w->frame);
17371 struct glyph_row *bottom_row;
17372 struct it it;
17373 struct run run;
17374 struct text_pos start, new_start;
17375 int nrows_scrolled, i;
17376 struct glyph_row *last_text_row;
17377 struct glyph_row *last_reused_text_row;
17378 struct glyph_row *start_row;
17379 int start_vpos, min_y, max_y;
17381 #ifdef GLYPH_DEBUG
17382 if (inhibit_try_window_reusing)
17383 return false;
17384 #endif
17386 if (/* This function doesn't handle terminal frames. */
17387 !FRAME_WINDOW_P (f)
17388 /* Don't try to reuse the display if windows have been split
17389 or such. */
17390 || windows_or_buffers_changed
17391 || f->cursor_type_changed)
17392 return false;
17394 /* Can't do this if showing trailing whitespace. */
17395 if (!NILP (Vshow_trailing_whitespace))
17396 return false;
17398 /* If top-line visibility has changed, give up. */
17399 if (WINDOW_WANTS_HEADER_LINE_P (w)
17400 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17401 return false;
17403 /* Give up if old or new display is scrolled vertically. We could
17404 make this function handle this, but right now it doesn't. */
17405 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17406 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17407 return false;
17409 /* The variable new_start now holds the new window start. The old
17410 start `start' can be determined from the current matrix. */
17411 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17412 start = start_row->minpos;
17413 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17415 /* Clear the desired matrix for the display below. */
17416 clear_glyph_matrix (w->desired_matrix);
17418 if (CHARPOS (new_start) <= CHARPOS (start))
17420 /* Don't use this method if the display starts with an ellipsis
17421 displayed for invisible text. It's not easy to handle that case
17422 below, and it's certainly not worth the effort since this is
17423 not a frequent case. */
17424 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17425 return false;
17427 IF_DEBUG (debug_method_add (w, "twu1"));
17429 /* Display up to a row that can be reused. The variable
17430 last_text_row is set to the last row displayed that displays
17431 text. Note that it.vpos == 0 if or if not there is a
17432 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17433 start_display (&it, w, new_start);
17434 w->cursor.vpos = -1;
17435 last_text_row = last_reused_text_row = NULL;
17437 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17439 /* If we have reached into the characters in the START row,
17440 that means the line boundaries have changed. So we
17441 can't start copying with the row START. Maybe it will
17442 work to start copying with the following row. */
17443 while (IT_CHARPOS (it) > CHARPOS (start))
17445 /* Advance to the next row as the "start". */
17446 start_row++;
17447 start = start_row->minpos;
17448 /* If there are no more rows to try, or just one, give up. */
17449 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17450 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17451 || CHARPOS (start) == ZV)
17453 clear_glyph_matrix (w->desired_matrix);
17454 return false;
17457 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17459 /* If we have reached alignment, we can copy the rest of the
17460 rows. */
17461 if (IT_CHARPOS (it) == CHARPOS (start)
17462 /* Don't accept "alignment" inside a display vector,
17463 since start_row could have started in the middle of
17464 that same display vector (thus their character
17465 positions match), and we have no way of telling if
17466 that is the case. */
17467 && it.current.dpvec_index < 0)
17468 break;
17470 it.glyph_row->reversed_p = false;
17471 if (display_line (&it))
17472 last_text_row = it.glyph_row - 1;
17476 /* A value of current_y < last_visible_y means that we stopped
17477 at the previous window start, which in turn means that we
17478 have at least one reusable row. */
17479 if (it.current_y < it.last_visible_y)
17481 struct glyph_row *row;
17483 /* IT.vpos always starts from 0; it counts text lines. */
17484 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17486 /* Find PT if not already found in the lines displayed. */
17487 if (w->cursor.vpos < 0)
17489 int dy = it.current_y - start_row->y;
17491 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17492 row = row_containing_pos (w, PT, row, NULL, dy);
17493 if (row)
17494 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17495 dy, nrows_scrolled);
17496 else
17498 clear_glyph_matrix (w->desired_matrix);
17499 return false;
17503 /* Scroll the display. Do it before the current matrix is
17504 changed. The problem here is that update has not yet
17505 run, i.e. part of the current matrix is not up to date.
17506 scroll_run_hook will clear the cursor, and use the
17507 current matrix to get the height of the row the cursor is
17508 in. */
17509 run.current_y = start_row->y;
17510 run.desired_y = it.current_y;
17511 run.height = it.last_visible_y - it.current_y;
17513 if (run.height > 0 && run.current_y != run.desired_y)
17515 update_begin (f);
17516 FRAME_RIF (f)->update_window_begin_hook (w);
17517 FRAME_RIF (f)->clear_window_mouse_face (w);
17518 FRAME_RIF (f)->scroll_run_hook (w, &run);
17519 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17520 update_end (f);
17523 /* Shift current matrix down by nrows_scrolled lines. */
17524 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17525 rotate_matrix (w->current_matrix,
17526 start_vpos,
17527 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17528 nrows_scrolled);
17530 /* Disable lines that must be updated. */
17531 for (i = 0; i < nrows_scrolled; ++i)
17532 (start_row + i)->enabled_p = false;
17534 /* Re-compute Y positions. */
17535 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17536 max_y = it.last_visible_y;
17537 for (row = start_row + nrows_scrolled;
17538 row < bottom_row;
17539 ++row)
17541 row->y = it.current_y;
17542 row->visible_height = row->height;
17544 if (row->y < min_y)
17545 row->visible_height -= min_y - row->y;
17546 if (row->y + row->height > max_y)
17547 row->visible_height -= row->y + row->height - max_y;
17548 if (row->fringe_bitmap_periodic_p)
17549 row->redraw_fringe_bitmaps_p = true;
17551 it.current_y += row->height;
17553 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17554 last_reused_text_row = row;
17555 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17556 break;
17559 /* Disable lines in the current matrix which are now
17560 below the window. */
17561 for (++row; row < bottom_row; ++row)
17562 row->enabled_p = row->mode_line_p = false;
17565 /* Update window_end_pos etc.; last_reused_text_row is the last
17566 reused row from the current matrix containing text, if any.
17567 The value of last_text_row is the last displayed line
17568 containing text. */
17569 if (last_reused_text_row)
17570 adjust_window_ends (w, last_reused_text_row, true);
17571 else if (last_text_row)
17572 adjust_window_ends (w, last_text_row, false);
17573 else
17575 /* This window must be completely empty. */
17576 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17577 w->window_end_pos = Z - ZV;
17578 w->window_end_vpos = 0;
17580 w->window_end_valid = false;
17582 /* Update hint: don't try scrolling again in update_window. */
17583 w->desired_matrix->no_scrolling_p = true;
17585 #ifdef GLYPH_DEBUG
17586 debug_method_add (w, "try_window_reusing_current_matrix 1");
17587 #endif
17588 return true;
17590 else if (CHARPOS (new_start) > CHARPOS (start))
17592 struct glyph_row *pt_row, *row;
17593 struct glyph_row *first_reusable_row;
17594 struct glyph_row *first_row_to_display;
17595 int dy;
17596 int yb = window_text_bottom_y (w);
17598 /* Find the row starting at new_start, if there is one. Don't
17599 reuse a partially visible line at the end. */
17600 first_reusable_row = start_row;
17601 while (first_reusable_row->enabled_p
17602 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17603 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17604 < CHARPOS (new_start)))
17605 ++first_reusable_row;
17607 /* Give up if there is no row to reuse. */
17608 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17609 || !first_reusable_row->enabled_p
17610 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17611 != CHARPOS (new_start)))
17612 return false;
17614 /* We can reuse fully visible rows beginning with
17615 first_reusable_row to the end of the window. Set
17616 first_row_to_display to the first row that cannot be reused.
17617 Set pt_row to the row containing point, if there is any. */
17618 pt_row = NULL;
17619 for (first_row_to_display = first_reusable_row;
17620 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17621 ++first_row_to_display)
17623 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17624 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17625 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17626 && first_row_to_display->ends_at_zv_p
17627 && pt_row == NULL)))
17628 pt_row = first_row_to_display;
17631 /* Start displaying at the start of first_row_to_display. */
17632 eassert (first_row_to_display->y < yb);
17633 init_to_row_start (&it, w, first_row_to_display);
17635 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17636 - start_vpos);
17637 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17638 - nrows_scrolled);
17639 it.current_y = (first_row_to_display->y - first_reusable_row->y
17640 + WINDOW_HEADER_LINE_HEIGHT (w));
17642 /* Display lines beginning with first_row_to_display in the
17643 desired matrix. Set last_text_row to the last row displayed
17644 that displays text. */
17645 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17646 if (pt_row == NULL)
17647 w->cursor.vpos = -1;
17648 last_text_row = NULL;
17649 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17650 if (display_line (&it))
17651 last_text_row = it.glyph_row - 1;
17653 /* If point is in a reused row, adjust y and vpos of the cursor
17654 position. */
17655 if (pt_row)
17657 w->cursor.vpos -= nrows_scrolled;
17658 w->cursor.y -= first_reusable_row->y - start_row->y;
17661 /* Give up if point isn't in a row displayed or reused. (This
17662 also handles the case where w->cursor.vpos < nrows_scrolled
17663 after the calls to display_line, which can happen with scroll
17664 margins. See bug#1295.) */
17665 if (w->cursor.vpos < 0)
17667 clear_glyph_matrix (w->desired_matrix);
17668 return false;
17671 /* Scroll the display. */
17672 run.current_y = first_reusable_row->y;
17673 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17674 run.height = it.last_visible_y - run.current_y;
17675 dy = run.current_y - run.desired_y;
17677 if (run.height)
17679 update_begin (f);
17680 FRAME_RIF (f)->update_window_begin_hook (w);
17681 FRAME_RIF (f)->clear_window_mouse_face (w);
17682 FRAME_RIF (f)->scroll_run_hook (w, &run);
17683 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17684 update_end (f);
17687 /* Adjust Y positions of reused rows. */
17688 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17689 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17690 max_y = it.last_visible_y;
17691 for (row = first_reusable_row; row < first_row_to_display; ++row)
17693 row->y -= dy;
17694 row->visible_height = row->height;
17695 if (row->y < min_y)
17696 row->visible_height -= min_y - row->y;
17697 if (row->y + row->height > max_y)
17698 row->visible_height -= row->y + row->height - max_y;
17699 if (row->fringe_bitmap_periodic_p)
17700 row->redraw_fringe_bitmaps_p = true;
17703 /* Scroll the current matrix. */
17704 eassert (nrows_scrolled > 0);
17705 rotate_matrix (w->current_matrix,
17706 start_vpos,
17707 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17708 -nrows_scrolled);
17710 /* Disable rows not reused. */
17711 for (row -= nrows_scrolled; row < bottom_row; ++row)
17712 row->enabled_p = false;
17714 /* Point may have moved to a different line, so we cannot assume that
17715 the previous cursor position is valid; locate the correct row. */
17716 if (pt_row)
17718 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17719 row < bottom_row
17720 && PT >= MATRIX_ROW_END_CHARPOS (row)
17721 && !row->ends_at_zv_p;
17722 row++)
17724 w->cursor.vpos++;
17725 w->cursor.y = row->y;
17727 if (row < bottom_row)
17729 /* Can't simply scan the row for point with
17730 bidi-reordered glyph rows. Let set_cursor_from_row
17731 figure out where to put the cursor, and if it fails,
17732 give up. */
17733 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17735 if (!set_cursor_from_row (w, row, w->current_matrix,
17736 0, 0, 0, 0))
17738 clear_glyph_matrix (w->desired_matrix);
17739 return false;
17742 else
17744 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17745 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17747 for (; glyph < end
17748 && (!BUFFERP (glyph->object)
17749 || glyph->charpos < PT);
17750 glyph++)
17752 w->cursor.hpos++;
17753 w->cursor.x += glyph->pixel_width;
17759 /* Adjust window end. A null value of last_text_row means that
17760 the window end is in reused rows which in turn means that
17761 only its vpos can have changed. */
17762 if (last_text_row)
17763 adjust_window_ends (w, last_text_row, false);
17764 else
17765 w->window_end_vpos -= nrows_scrolled;
17767 w->window_end_valid = false;
17768 w->desired_matrix->no_scrolling_p = true;
17770 #ifdef GLYPH_DEBUG
17771 debug_method_add (w, "try_window_reusing_current_matrix 2");
17772 #endif
17773 return true;
17776 return false;
17781 /************************************************************************
17782 Window redisplay reusing current matrix when buffer has changed
17783 ************************************************************************/
17785 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17786 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17787 ptrdiff_t *, ptrdiff_t *);
17788 static struct glyph_row *
17789 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17790 struct glyph_row *);
17793 /* Return the last row in MATRIX displaying text. If row START is
17794 non-null, start searching with that row. IT gives the dimensions
17795 of the display. Value is null if matrix is empty; otherwise it is
17796 a pointer to the row found. */
17798 static struct glyph_row *
17799 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17800 struct glyph_row *start)
17802 struct glyph_row *row, *row_found;
17804 /* Set row_found to the last row in IT->w's current matrix
17805 displaying text. The loop looks funny but think of partially
17806 visible lines. */
17807 row_found = NULL;
17808 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17809 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17811 eassert (row->enabled_p);
17812 row_found = row;
17813 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17814 break;
17815 ++row;
17818 return row_found;
17822 /* Return the last row in the current matrix of W that is not affected
17823 by changes at the start of current_buffer that occurred since W's
17824 current matrix was built. Value is null if no such row exists.
17826 BEG_UNCHANGED us the number of characters unchanged at the start of
17827 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17828 first changed character in current_buffer. Characters at positions <
17829 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17830 when the current matrix was built. */
17832 static struct glyph_row *
17833 find_last_unchanged_at_beg_row (struct window *w)
17835 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17836 struct glyph_row *row;
17837 struct glyph_row *row_found = NULL;
17838 int yb = window_text_bottom_y (w);
17840 /* Find the last row displaying unchanged text. */
17841 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17842 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17843 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17844 ++row)
17846 if (/* If row ends before first_changed_pos, it is unchanged,
17847 except in some case. */
17848 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17849 /* When row ends in ZV and we write at ZV it is not
17850 unchanged. */
17851 && !row->ends_at_zv_p
17852 /* When first_changed_pos is the end of a continued line,
17853 row is not unchanged because it may be no longer
17854 continued. */
17855 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17856 && (row->continued_p
17857 || row->exact_window_width_line_p))
17858 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17859 needs to be recomputed, so don't consider this row as
17860 unchanged. This happens when the last line was
17861 bidi-reordered and was killed immediately before this
17862 redisplay cycle. In that case, ROW->end stores the
17863 buffer position of the first visual-order character of
17864 the killed text, which is now beyond ZV. */
17865 && CHARPOS (row->end.pos) <= ZV)
17866 row_found = row;
17868 /* Stop if last visible row. */
17869 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17870 break;
17873 return row_found;
17877 /* Find the first glyph row in the current matrix of W that is not
17878 affected by changes at the end of current_buffer since the
17879 time W's current matrix was built.
17881 Return in *DELTA the number of chars by which buffer positions in
17882 unchanged text at the end of current_buffer must be adjusted.
17884 Return in *DELTA_BYTES the corresponding number of bytes.
17886 Value is null if no such row exists, i.e. all rows are affected by
17887 changes. */
17889 static struct glyph_row *
17890 find_first_unchanged_at_end_row (struct window *w,
17891 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17893 struct glyph_row *row;
17894 struct glyph_row *row_found = NULL;
17896 *delta = *delta_bytes = 0;
17898 /* Display must not have been paused, otherwise the current matrix
17899 is not up to date. */
17900 eassert (w->window_end_valid);
17902 /* A value of window_end_pos >= END_UNCHANGED means that the window
17903 end is in the range of changed text. If so, there is no
17904 unchanged row at the end of W's current matrix. */
17905 if (w->window_end_pos >= END_UNCHANGED)
17906 return NULL;
17908 /* Set row to the last row in W's current matrix displaying text. */
17909 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17911 /* If matrix is entirely empty, no unchanged row exists. */
17912 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17914 /* The value of row is the last glyph row in the matrix having a
17915 meaningful buffer position in it. The end position of row
17916 corresponds to window_end_pos. This allows us to translate
17917 buffer positions in the current matrix to current buffer
17918 positions for characters not in changed text. */
17919 ptrdiff_t Z_old =
17920 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17921 ptrdiff_t Z_BYTE_old =
17922 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17923 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17924 struct glyph_row *first_text_row
17925 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17927 *delta = Z - Z_old;
17928 *delta_bytes = Z_BYTE - Z_BYTE_old;
17930 /* Set last_unchanged_pos to the buffer position of the last
17931 character in the buffer that has not been changed. Z is the
17932 index + 1 of the last character in current_buffer, i.e. by
17933 subtracting END_UNCHANGED we get the index of the last
17934 unchanged character, and we have to add BEG to get its buffer
17935 position. */
17936 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17937 last_unchanged_pos_old = last_unchanged_pos - *delta;
17939 /* Search backward from ROW for a row displaying a line that
17940 starts at a minimum position >= last_unchanged_pos_old. */
17941 for (; row > first_text_row; --row)
17943 /* This used to abort, but it can happen.
17944 It is ok to just stop the search instead here. KFS. */
17945 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17946 break;
17948 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17949 row_found = row;
17953 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17955 return row_found;
17959 /* Make sure that glyph rows in the current matrix of window W
17960 reference the same glyph memory as corresponding rows in the
17961 frame's frame matrix. This function is called after scrolling W's
17962 current matrix on a terminal frame in try_window_id and
17963 try_window_reusing_current_matrix. */
17965 static void
17966 sync_frame_with_window_matrix_rows (struct window *w)
17968 struct frame *f = XFRAME (w->frame);
17969 struct glyph_row *window_row, *window_row_end, *frame_row;
17971 /* Preconditions: W must be a leaf window and full-width. Its frame
17972 must have a frame matrix. */
17973 eassert (BUFFERP (w->contents));
17974 eassert (WINDOW_FULL_WIDTH_P (w));
17975 eassert (!FRAME_WINDOW_P (f));
17977 /* If W is a full-width window, glyph pointers in W's current matrix
17978 have, by definition, to be the same as glyph pointers in the
17979 corresponding frame matrix. Note that frame matrices have no
17980 marginal areas (see build_frame_matrix). */
17981 window_row = w->current_matrix->rows;
17982 window_row_end = window_row + w->current_matrix->nrows;
17983 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17984 while (window_row < window_row_end)
17986 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17987 struct glyph *end = window_row->glyphs[LAST_AREA];
17989 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17990 frame_row->glyphs[TEXT_AREA] = start;
17991 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17992 frame_row->glyphs[LAST_AREA] = end;
17994 /* Disable frame rows whose corresponding window rows have
17995 been disabled in try_window_id. */
17996 if (!window_row->enabled_p)
17997 frame_row->enabled_p = false;
17999 ++window_row, ++frame_row;
18004 /* Find the glyph row in window W containing CHARPOS. Consider all
18005 rows between START and END (not inclusive). END null means search
18006 all rows to the end of the display area of W. Value is the row
18007 containing CHARPOS or null. */
18009 struct glyph_row *
18010 row_containing_pos (struct window *w, ptrdiff_t charpos,
18011 struct glyph_row *start, struct glyph_row *end, int dy)
18013 struct glyph_row *row = start;
18014 struct glyph_row *best_row = NULL;
18015 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18016 int last_y;
18018 /* If we happen to start on a header-line, skip that. */
18019 if (row->mode_line_p)
18020 ++row;
18022 if ((end && row >= end) || !row->enabled_p)
18023 return NULL;
18025 last_y = window_text_bottom_y (w) - dy;
18027 while (true)
18029 /* Give up if we have gone too far. */
18030 if ((end && row >= end) || !row->enabled_p)
18031 return NULL;
18032 /* This formerly returned if they were equal.
18033 I think that both quantities are of a "last plus one" type;
18034 if so, when they are equal, the row is within the screen. -- rms. */
18035 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18036 return NULL;
18038 /* If it is in this row, return this row. */
18039 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18040 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18041 /* The end position of a row equals the start
18042 position of the next row. If CHARPOS is there, we
18043 would rather consider it displayed in the next
18044 line, except when this line ends in ZV. */
18045 && !row_for_charpos_p (row, charpos)))
18046 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18048 struct glyph *g;
18050 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18051 || (!best_row && !row->continued_p))
18052 return row;
18053 /* In bidi-reordered rows, there could be several rows whose
18054 edges surround CHARPOS, all of these rows belonging to
18055 the same continued line. We need to find the row which
18056 fits CHARPOS the best. */
18057 for (g = row->glyphs[TEXT_AREA];
18058 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18059 g++)
18061 if (!STRINGP (g->object))
18063 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18065 mindif = eabs (g->charpos - charpos);
18066 best_row = row;
18067 /* Exact match always wins. */
18068 if (mindif == 0)
18069 return best_row;
18074 else if (best_row && !row->continued_p)
18075 return best_row;
18076 ++row;
18081 /* Try to redisplay window W by reusing its existing display. W's
18082 current matrix must be up to date when this function is called,
18083 i.e., window_end_valid must be true.
18085 Value is
18087 >= 1 if successful, i.e. display has been updated
18088 specifically:
18089 1 means the changes were in front of a newline that precedes
18090 the window start, and the whole current matrix was reused
18091 2 means the changes were after the last position displayed
18092 in the window, and the whole current matrix was reused
18093 3 means portions of the current matrix were reused, while
18094 some of the screen lines were redrawn
18095 -1 if redisplay with same window start is known not to succeed
18096 0 if otherwise unsuccessful
18098 The following steps are performed:
18100 1. Find the last row in the current matrix of W that is not
18101 affected by changes at the start of current_buffer. If no such row
18102 is found, give up.
18104 2. Find the first row in W's current matrix that is not affected by
18105 changes at the end of current_buffer. Maybe there is no such row.
18107 3. Display lines beginning with the row + 1 found in step 1 to the
18108 row found in step 2 or, if step 2 didn't find a row, to the end of
18109 the window.
18111 4. If cursor is not known to appear on the window, give up.
18113 5. If display stopped at the row found in step 2, scroll the
18114 display and current matrix as needed.
18116 6. Maybe display some lines at the end of W, if we must. This can
18117 happen under various circumstances, like a partially visible line
18118 becoming fully visible, or because newly displayed lines are displayed
18119 in smaller font sizes.
18121 7. Update W's window end information. */
18123 static int
18124 try_window_id (struct window *w)
18126 struct frame *f = XFRAME (w->frame);
18127 struct glyph_matrix *current_matrix = w->current_matrix;
18128 struct glyph_matrix *desired_matrix = w->desired_matrix;
18129 struct glyph_row *last_unchanged_at_beg_row;
18130 struct glyph_row *first_unchanged_at_end_row;
18131 struct glyph_row *row;
18132 struct glyph_row *bottom_row;
18133 int bottom_vpos;
18134 struct it it;
18135 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18136 int dvpos, dy;
18137 struct text_pos start_pos;
18138 struct run run;
18139 int first_unchanged_at_end_vpos = 0;
18140 struct glyph_row *last_text_row, *last_text_row_at_end;
18141 struct text_pos start;
18142 ptrdiff_t first_changed_charpos, last_changed_charpos;
18144 #ifdef GLYPH_DEBUG
18145 if (inhibit_try_window_id)
18146 return 0;
18147 #endif
18149 /* This is handy for debugging. */
18150 #if false
18151 #define GIVE_UP(X) \
18152 do { \
18153 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18154 return 0; \
18155 } while (false)
18156 #else
18157 #define GIVE_UP(X) return 0
18158 #endif
18160 SET_TEXT_POS_FROM_MARKER (start, w->start);
18162 /* Don't use this for mini-windows because these can show
18163 messages and mini-buffers, and we don't handle that here. */
18164 if (MINI_WINDOW_P (w))
18165 GIVE_UP (1);
18167 /* This flag is used to prevent redisplay optimizations. */
18168 if (windows_or_buffers_changed || f->cursor_type_changed)
18169 GIVE_UP (2);
18171 /* This function's optimizations cannot be used if overlays have
18172 changed in the buffer displayed by the window, so give up if they
18173 have. */
18174 if (w->last_overlay_modified != OVERLAY_MODIFF)
18175 GIVE_UP (200);
18177 /* Verify that narrowing has not changed.
18178 Also verify that we were not told to prevent redisplay optimizations.
18179 It would be nice to further
18180 reduce the number of cases where this prevents try_window_id. */
18181 if (current_buffer->clip_changed
18182 || current_buffer->prevent_redisplay_optimizations_p)
18183 GIVE_UP (3);
18185 /* Window must either use window-based redisplay or be full width. */
18186 if (!FRAME_WINDOW_P (f)
18187 && (!FRAME_LINE_INS_DEL_OK (f)
18188 || !WINDOW_FULL_WIDTH_P (w)))
18189 GIVE_UP (4);
18191 /* Give up if point is known NOT to appear in W. */
18192 if (PT < CHARPOS (start))
18193 GIVE_UP (5);
18195 /* Another way to prevent redisplay optimizations. */
18196 if (w->last_modified == 0)
18197 GIVE_UP (6);
18199 /* Verify that window is not hscrolled. */
18200 if (w->hscroll != 0)
18201 GIVE_UP (7);
18203 /* Verify that display wasn't paused. */
18204 if (!w->window_end_valid)
18205 GIVE_UP (8);
18207 /* Likewise if highlighting trailing whitespace. */
18208 if (!NILP (Vshow_trailing_whitespace))
18209 GIVE_UP (11);
18211 /* Can't use this if overlay arrow position and/or string have
18212 changed. */
18213 if (overlay_arrows_changed_p ())
18214 GIVE_UP (12);
18216 /* When word-wrap is on, adding a space to the first word of a
18217 wrapped line can change the wrap position, altering the line
18218 above it. It might be worthwhile to handle this more
18219 intelligently, but for now just redisplay from scratch. */
18220 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18221 GIVE_UP (21);
18223 /* Under bidi reordering, adding or deleting a character in the
18224 beginning of a paragraph, before the first strong directional
18225 character, can change the base direction of the paragraph (unless
18226 the buffer specifies a fixed paragraph direction), which will
18227 require redisplaying the whole paragraph. It might be worthwhile
18228 to find the paragraph limits and widen the range of redisplayed
18229 lines to that, but for now just give up this optimization and
18230 redisplay from scratch. */
18231 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18232 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18233 GIVE_UP (22);
18235 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18236 to that variable require thorough redisplay. */
18237 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18238 GIVE_UP (23);
18240 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18241 only if buffer has really changed. The reason is that the gap is
18242 initially at Z for freshly visited files. The code below would
18243 set end_unchanged to 0 in that case. */
18244 if (MODIFF > SAVE_MODIFF
18245 /* This seems to happen sometimes after saving a buffer. */
18246 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18248 if (GPT - BEG < BEG_UNCHANGED)
18249 BEG_UNCHANGED = GPT - BEG;
18250 if (Z - GPT < END_UNCHANGED)
18251 END_UNCHANGED = Z - GPT;
18254 /* The position of the first and last character that has been changed. */
18255 first_changed_charpos = BEG + BEG_UNCHANGED;
18256 last_changed_charpos = Z - END_UNCHANGED;
18258 /* If window starts after a line end, and the last change is in
18259 front of that newline, then changes don't affect the display.
18260 This case happens with stealth-fontification. Note that although
18261 the display is unchanged, glyph positions in the matrix have to
18262 be adjusted, of course. */
18263 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18264 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18265 && ((last_changed_charpos < CHARPOS (start)
18266 && CHARPOS (start) == BEGV)
18267 || (last_changed_charpos < CHARPOS (start) - 1
18268 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18270 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18271 struct glyph_row *r0;
18273 /* Compute how many chars/bytes have been added to or removed
18274 from the buffer. */
18275 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18276 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18277 Z_delta = Z - Z_old;
18278 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18280 /* Give up if PT is not in the window. Note that it already has
18281 been checked at the start of try_window_id that PT is not in
18282 front of the window start. */
18283 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18284 GIVE_UP (13);
18286 /* If window start is unchanged, we can reuse the whole matrix
18287 as is, after adjusting glyph positions. No need to compute
18288 the window end again, since its offset from Z hasn't changed. */
18289 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18290 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18291 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18292 /* PT must not be in a partially visible line. */
18293 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18294 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18296 /* Adjust positions in the glyph matrix. */
18297 if (Z_delta || Z_delta_bytes)
18299 struct glyph_row *r1
18300 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18301 increment_matrix_positions (w->current_matrix,
18302 MATRIX_ROW_VPOS (r0, current_matrix),
18303 MATRIX_ROW_VPOS (r1, current_matrix),
18304 Z_delta, Z_delta_bytes);
18307 /* Set the cursor. */
18308 row = row_containing_pos (w, PT, r0, NULL, 0);
18309 if (row)
18310 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18311 return 1;
18315 /* Handle the case that changes are all below what is displayed in
18316 the window, and that PT is in the window. This shortcut cannot
18317 be taken if ZV is visible in the window, and text has been added
18318 there that is visible in the window. */
18319 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18320 /* ZV is not visible in the window, or there are no
18321 changes at ZV, actually. */
18322 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18323 || first_changed_charpos == last_changed_charpos))
18325 struct glyph_row *r0;
18327 /* Give up if PT is not in the window. Note that it already has
18328 been checked at the start of try_window_id that PT is not in
18329 front of the window start. */
18330 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18331 GIVE_UP (14);
18333 /* If window start is unchanged, we can reuse the whole matrix
18334 as is, without changing glyph positions since no text has
18335 been added/removed in front of the window end. */
18336 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18337 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18338 /* PT must not be in a partially visible line. */
18339 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18340 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18342 /* We have to compute the window end anew since text
18343 could have been added/removed after it. */
18344 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18345 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18347 /* Set the cursor. */
18348 row = row_containing_pos (w, PT, r0, NULL, 0);
18349 if (row)
18350 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18351 return 2;
18355 /* Give up if window start is in the changed area.
18357 The condition used to read
18359 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18361 but why that was tested escapes me at the moment. */
18362 if (CHARPOS (start) >= first_changed_charpos
18363 && CHARPOS (start) <= last_changed_charpos)
18364 GIVE_UP (15);
18366 /* Check that window start agrees with the start of the first glyph
18367 row in its current matrix. Check this after we know the window
18368 start is not in changed text, otherwise positions would not be
18369 comparable. */
18370 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18371 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18372 GIVE_UP (16);
18374 /* Give up if the window ends in strings. Overlay strings
18375 at the end are difficult to handle, so don't try. */
18376 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18377 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18378 GIVE_UP (20);
18380 /* Compute the position at which we have to start displaying new
18381 lines. Some of the lines at the top of the window might be
18382 reusable because they are not displaying changed text. Find the
18383 last row in W's current matrix not affected by changes at the
18384 start of current_buffer. Value is null if changes start in the
18385 first line of window. */
18386 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18387 if (last_unchanged_at_beg_row)
18389 /* Avoid starting to display in the middle of a character, a TAB
18390 for instance. This is easier than to set up the iterator
18391 exactly, and it's not a frequent case, so the additional
18392 effort wouldn't really pay off. */
18393 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18394 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18395 && last_unchanged_at_beg_row > w->current_matrix->rows)
18396 --last_unchanged_at_beg_row;
18398 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18399 GIVE_UP (17);
18401 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18402 GIVE_UP (18);
18403 start_pos = it.current.pos;
18405 /* Start displaying new lines in the desired matrix at the same
18406 vpos we would use in the current matrix, i.e. below
18407 last_unchanged_at_beg_row. */
18408 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18409 current_matrix);
18410 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18411 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18413 eassert (it.hpos == 0 && it.current_x == 0);
18415 else
18417 /* There are no reusable lines at the start of the window.
18418 Start displaying in the first text line. */
18419 start_display (&it, w, start);
18420 it.vpos = it.first_vpos;
18421 start_pos = it.current.pos;
18424 /* Find the first row that is not affected by changes at the end of
18425 the buffer. Value will be null if there is no unchanged row, in
18426 which case we must redisplay to the end of the window. delta
18427 will be set to the value by which buffer positions beginning with
18428 first_unchanged_at_end_row have to be adjusted due to text
18429 changes. */
18430 first_unchanged_at_end_row
18431 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18432 IF_DEBUG (debug_delta = delta);
18433 IF_DEBUG (debug_delta_bytes = delta_bytes);
18435 /* Set stop_pos to the buffer position up to which we will have to
18436 display new lines. If first_unchanged_at_end_row != NULL, this
18437 is the buffer position of the start of the line displayed in that
18438 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18439 that we don't stop at a buffer position. */
18440 stop_pos = 0;
18441 if (first_unchanged_at_end_row)
18443 eassert (last_unchanged_at_beg_row == NULL
18444 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18446 /* If this is a continuation line, move forward to the next one
18447 that isn't. Changes in lines above affect this line.
18448 Caution: this may move first_unchanged_at_end_row to a row
18449 not displaying text. */
18450 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18451 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18452 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18453 < it.last_visible_y))
18454 ++first_unchanged_at_end_row;
18456 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18457 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18458 >= it.last_visible_y))
18459 first_unchanged_at_end_row = NULL;
18460 else
18462 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18463 + delta);
18464 first_unchanged_at_end_vpos
18465 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18466 eassert (stop_pos >= Z - END_UNCHANGED);
18469 else if (last_unchanged_at_beg_row == NULL)
18470 GIVE_UP (19);
18473 #ifdef GLYPH_DEBUG
18475 /* Either there is no unchanged row at the end, or the one we have
18476 now displays text. This is a necessary condition for the window
18477 end pos calculation at the end of this function. */
18478 eassert (first_unchanged_at_end_row == NULL
18479 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18481 debug_last_unchanged_at_beg_vpos
18482 = (last_unchanged_at_beg_row
18483 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18484 : -1);
18485 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18487 #endif /* GLYPH_DEBUG */
18490 /* Display new lines. Set last_text_row to the last new line
18491 displayed which has text on it, i.e. might end up as being the
18492 line where the window_end_vpos is. */
18493 w->cursor.vpos = -1;
18494 last_text_row = NULL;
18495 overlay_arrow_seen = false;
18496 if (it.current_y < it.last_visible_y
18497 && !f->fonts_changed
18498 && (first_unchanged_at_end_row == NULL
18499 || IT_CHARPOS (it) < stop_pos))
18500 it.glyph_row->reversed_p = false;
18501 while (it.current_y < it.last_visible_y
18502 && !f->fonts_changed
18503 && (first_unchanged_at_end_row == NULL
18504 || IT_CHARPOS (it) < stop_pos))
18506 if (display_line (&it))
18507 last_text_row = it.glyph_row - 1;
18510 if (f->fonts_changed)
18511 return -1;
18513 /* The redisplay iterations in display_line above could have
18514 triggered font-lock, which could have done something that
18515 invalidates IT->w window's end-point information, on which we
18516 rely below. E.g., one package, which will remain unnamed, used
18517 to install a font-lock-fontify-region-function that called
18518 bury-buffer, whose side effect is to switch the buffer displayed
18519 by IT->w, and that predictably resets IT->w's window_end_valid
18520 flag, which we already tested at the entry to this function.
18521 Amply punish such packages/modes by giving up on this
18522 optimization in those cases. */
18523 if (!w->window_end_valid)
18525 clear_glyph_matrix (w->desired_matrix);
18526 return -1;
18529 /* Compute differences in buffer positions, y-positions etc. for
18530 lines reused at the bottom of the window. Compute what we can
18531 scroll. */
18532 if (first_unchanged_at_end_row
18533 /* No lines reused because we displayed everything up to the
18534 bottom of the window. */
18535 && it.current_y < it.last_visible_y)
18537 dvpos = (it.vpos
18538 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18539 current_matrix));
18540 dy = it.current_y - first_unchanged_at_end_row->y;
18541 run.current_y = first_unchanged_at_end_row->y;
18542 run.desired_y = run.current_y + dy;
18543 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18545 else
18547 delta = delta_bytes = dvpos = dy
18548 = run.current_y = run.desired_y = run.height = 0;
18549 first_unchanged_at_end_row = NULL;
18551 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18554 /* Find the cursor if not already found. We have to decide whether
18555 PT will appear on this window (it sometimes doesn't, but this is
18556 not a very frequent case.) This decision has to be made before
18557 the current matrix is altered. A value of cursor.vpos < 0 means
18558 that PT is either in one of the lines beginning at
18559 first_unchanged_at_end_row or below the window. Don't care for
18560 lines that might be displayed later at the window end; as
18561 mentioned, this is not a frequent case. */
18562 if (w->cursor.vpos < 0)
18564 /* Cursor in unchanged rows at the top? */
18565 if (PT < CHARPOS (start_pos)
18566 && last_unchanged_at_beg_row)
18568 row = row_containing_pos (w, PT,
18569 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18570 last_unchanged_at_beg_row + 1, 0);
18571 if (row)
18572 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18575 /* Start from first_unchanged_at_end_row looking for PT. */
18576 else if (first_unchanged_at_end_row)
18578 row = row_containing_pos (w, PT - delta,
18579 first_unchanged_at_end_row, NULL, 0);
18580 if (row)
18581 set_cursor_from_row (w, row, w->current_matrix, delta,
18582 delta_bytes, dy, dvpos);
18585 /* Give up if cursor was not found. */
18586 if (w->cursor.vpos < 0)
18588 clear_glyph_matrix (w->desired_matrix);
18589 return -1;
18593 /* Don't let the cursor end in the scroll margins. */
18595 int this_scroll_margin, cursor_height;
18596 int frame_line_height = default_line_pixel_height (w);
18597 int window_total_lines
18598 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18600 this_scroll_margin =
18601 max (0, min (scroll_margin, window_total_lines / 4));
18602 this_scroll_margin *= frame_line_height;
18603 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18605 if ((w->cursor.y < this_scroll_margin
18606 && CHARPOS (start) > BEGV)
18607 /* Old redisplay didn't take scroll margin into account at the bottom,
18608 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18609 || (w->cursor.y + (make_cursor_line_fully_visible_p
18610 ? cursor_height + this_scroll_margin
18611 : 1)) > it.last_visible_y)
18613 w->cursor.vpos = -1;
18614 clear_glyph_matrix (w->desired_matrix);
18615 return -1;
18619 /* Scroll the display. Do it before changing the current matrix so
18620 that xterm.c doesn't get confused about where the cursor glyph is
18621 found. */
18622 if (dy && run.height)
18624 update_begin (f);
18626 if (FRAME_WINDOW_P (f))
18628 FRAME_RIF (f)->update_window_begin_hook (w);
18629 FRAME_RIF (f)->clear_window_mouse_face (w);
18630 FRAME_RIF (f)->scroll_run_hook (w, &run);
18631 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18633 else
18635 /* Terminal frame. In this case, dvpos gives the number of
18636 lines to scroll by; dvpos < 0 means scroll up. */
18637 int from_vpos
18638 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18639 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18640 int end = (WINDOW_TOP_EDGE_LINE (w)
18641 + WINDOW_WANTS_HEADER_LINE_P (w)
18642 + window_internal_height (w));
18644 #if defined (HAVE_GPM) || defined (MSDOS)
18645 x_clear_window_mouse_face (w);
18646 #endif
18647 /* Perform the operation on the screen. */
18648 if (dvpos > 0)
18650 /* Scroll last_unchanged_at_beg_row to the end of the
18651 window down dvpos lines. */
18652 set_terminal_window (f, end);
18654 /* On dumb terminals delete dvpos lines at the end
18655 before inserting dvpos empty lines. */
18656 if (!FRAME_SCROLL_REGION_OK (f))
18657 ins_del_lines (f, end - dvpos, -dvpos);
18659 /* Insert dvpos empty lines in front of
18660 last_unchanged_at_beg_row. */
18661 ins_del_lines (f, from, dvpos);
18663 else if (dvpos < 0)
18665 /* Scroll up last_unchanged_at_beg_vpos to the end of
18666 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18667 set_terminal_window (f, end);
18669 /* Delete dvpos lines in front of
18670 last_unchanged_at_beg_vpos. ins_del_lines will set
18671 the cursor to the given vpos and emit |dvpos| delete
18672 line sequences. */
18673 ins_del_lines (f, from + dvpos, dvpos);
18675 /* On a dumb terminal insert dvpos empty lines at the
18676 end. */
18677 if (!FRAME_SCROLL_REGION_OK (f))
18678 ins_del_lines (f, end + dvpos, -dvpos);
18681 set_terminal_window (f, 0);
18684 update_end (f);
18687 /* Shift reused rows of the current matrix to the right position.
18688 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18689 text. */
18690 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18691 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18692 if (dvpos < 0)
18694 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18695 bottom_vpos, dvpos);
18696 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18697 bottom_vpos);
18699 else if (dvpos > 0)
18701 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18702 bottom_vpos, dvpos);
18703 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18704 first_unchanged_at_end_vpos + dvpos);
18707 /* For frame-based redisplay, make sure that current frame and window
18708 matrix are in sync with respect to glyph memory. */
18709 if (!FRAME_WINDOW_P (f))
18710 sync_frame_with_window_matrix_rows (w);
18712 /* Adjust buffer positions in reused rows. */
18713 if (delta || delta_bytes)
18714 increment_matrix_positions (current_matrix,
18715 first_unchanged_at_end_vpos + dvpos,
18716 bottom_vpos, delta, delta_bytes);
18718 /* Adjust Y positions. */
18719 if (dy)
18720 shift_glyph_matrix (w, current_matrix,
18721 first_unchanged_at_end_vpos + dvpos,
18722 bottom_vpos, dy);
18724 if (first_unchanged_at_end_row)
18726 first_unchanged_at_end_row += dvpos;
18727 if (first_unchanged_at_end_row->y >= it.last_visible_y
18728 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18729 first_unchanged_at_end_row = NULL;
18732 /* If scrolling up, there may be some lines to display at the end of
18733 the window. */
18734 last_text_row_at_end = NULL;
18735 if (dy < 0)
18737 /* Scrolling up can leave for example a partially visible line
18738 at the end of the window to be redisplayed. */
18739 /* Set last_row to the glyph row in the current matrix where the
18740 window end line is found. It has been moved up or down in
18741 the matrix by dvpos. */
18742 int last_vpos = w->window_end_vpos + dvpos;
18743 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18745 /* If last_row is the window end line, it should display text. */
18746 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18748 /* If window end line was partially visible before, begin
18749 displaying at that line. Otherwise begin displaying with the
18750 line following it. */
18751 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18753 init_to_row_start (&it, w, last_row);
18754 it.vpos = last_vpos;
18755 it.current_y = last_row->y;
18757 else
18759 init_to_row_end (&it, w, last_row);
18760 it.vpos = 1 + last_vpos;
18761 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18762 ++last_row;
18765 /* We may start in a continuation line. If so, we have to
18766 get the right continuation_lines_width and current_x. */
18767 it.continuation_lines_width = last_row->continuation_lines_width;
18768 it.hpos = it.current_x = 0;
18770 /* Display the rest of the lines at the window end. */
18771 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18772 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18774 /* Is it always sure that the display agrees with lines in
18775 the current matrix? I don't think so, so we mark rows
18776 displayed invalid in the current matrix by setting their
18777 enabled_p flag to false. */
18778 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18779 if (display_line (&it))
18780 last_text_row_at_end = it.glyph_row - 1;
18784 /* Update window_end_pos and window_end_vpos. */
18785 if (first_unchanged_at_end_row && !last_text_row_at_end)
18787 /* Window end line if one of the preserved rows from the current
18788 matrix. Set row to the last row displaying text in current
18789 matrix starting at first_unchanged_at_end_row, after
18790 scrolling. */
18791 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18792 row = find_last_row_displaying_text (w->current_matrix, &it,
18793 first_unchanged_at_end_row);
18794 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18795 adjust_window_ends (w, row, true);
18796 eassert (w->window_end_bytepos >= 0);
18797 IF_DEBUG (debug_method_add (w, "A"));
18799 else if (last_text_row_at_end)
18801 adjust_window_ends (w, last_text_row_at_end, false);
18802 eassert (w->window_end_bytepos >= 0);
18803 IF_DEBUG (debug_method_add (w, "B"));
18805 else if (last_text_row)
18807 /* We have displayed either to the end of the window or at the
18808 end of the window, i.e. the last row with text is to be found
18809 in the desired matrix. */
18810 adjust_window_ends (w, last_text_row, false);
18811 eassert (w->window_end_bytepos >= 0);
18813 else if (first_unchanged_at_end_row == NULL
18814 && last_text_row == NULL
18815 && last_text_row_at_end == NULL)
18817 /* Displayed to end of window, but no line containing text was
18818 displayed. Lines were deleted at the end of the window. */
18819 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18820 int vpos = w->window_end_vpos;
18821 struct glyph_row *current_row = current_matrix->rows + vpos;
18822 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18824 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18826 eassert (first_vpos <= vpos);
18827 if (desired_row->enabled_p)
18829 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18830 row = desired_row;
18832 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18833 row = current_row;
18836 w->window_end_vpos = vpos + 1;
18837 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18838 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18839 eassert (w->window_end_bytepos >= 0);
18840 IF_DEBUG (debug_method_add (w, "C"));
18842 else
18843 emacs_abort ();
18845 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18846 debug_end_vpos = w->window_end_vpos));
18848 /* Record that display has not been completed. */
18849 w->window_end_valid = false;
18850 w->desired_matrix->no_scrolling_p = true;
18851 return 3;
18853 #undef GIVE_UP
18858 /***********************************************************************
18859 More debugging support
18860 ***********************************************************************/
18862 #ifdef GLYPH_DEBUG
18864 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18865 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18866 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18869 /* Dump the contents of glyph matrix MATRIX on stderr.
18871 GLYPHS 0 means don't show glyph contents.
18872 GLYPHS 1 means show glyphs in short form
18873 GLYPHS > 1 means show glyphs in long form. */
18875 void
18876 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18878 int i;
18879 for (i = 0; i < matrix->nrows; ++i)
18880 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18884 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18885 the glyph row and area where the glyph comes from. */
18887 void
18888 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18890 if (glyph->type == CHAR_GLYPH
18891 || glyph->type == GLYPHLESS_GLYPH)
18893 fprintf (stderr,
18894 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18895 glyph - row->glyphs[TEXT_AREA],
18896 (glyph->type == CHAR_GLYPH
18897 ? 'C'
18898 : 'G'),
18899 glyph->charpos,
18900 (BUFFERP (glyph->object)
18901 ? 'B'
18902 : (STRINGP (glyph->object)
18903 ? 'S'
18904 : (NILP (glyph->object)
18905 ? '0'
18906 : '-'))),
18907 glyph->pixel_width,
18908 glyph->u.ch,
18909 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18910 ? glyph->u.ch
18911 : '.'),
18912 glyph->face_id,
18913 glyph->left_box_line_p,
18914 glyph->right_box_line_p);
18916 else if (glyph->type == STRETCH_GLYPH)
18918 fprintf (stderr,
18919 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18920 glyph - row->glyphs[TEXT_AREA],
18921 'S',
18922 glyph->charpos,
18923 (BUFFERP (glyph->object)
18924 ? 'B'
18925 : (STRINGP (glyph->object)
18926 ? 'S'
18927 : (NILP (glyph->object)
18928 ? '0'
18929 : '-'))),
18930 glyph->pixel_width,
18932 ' ',
18933 glyph->face_id,
18934 glyph->left_box_line_p,
18935 glyph->right_box_line_p);
18937 else if (glyph->type == IMAGE_GLYPH)
18939 fprintf (stderr,
18940 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18941 glyph - row->glyphs[TEXT_AREA],
18942 'I',
18943 glyph->charpos,
18944 (BUFFERP (glyph->object)
18945 ? 'B'
18946 : (STRINGP (glyph->object)
18947 ? 'S'
18948 : (NILP (glyph->object)
18949 ? '0'
18950 : '-'))),
18951 glyph->pixel_width,
18952 glyph->u.img_id,
18953 '.',
18954 glyph->face_id,
18955 glyph->left_box_line_p,
18956 glyph->right_box_line_p);
18958 else if (glyph->type == COMPOSITE_GLYPH)
18960 fprintf (stderr,
18961 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18962 glyph - row->glyphs[TEXT_AREA],
18963 '+',
18964 glyph->charpos,
18965 (BUFFERP (glyph->object)
18966 ? 'B'
18967 : (STRINGP (glyph->object)
18968 ? 'S'
18969 : (NILP (glyph->object)
18970 ? '0'
18971 : '-'))),
18972 glyph->pixel_width,
18973 glyph->u.cmp.id);
18974 if (glyph->u.cmp.automatic)
18975 fprintf (stderr,
18976 "[%d-%d]",
18977 glyph->slice.cmp.from, glyph->slice.cmp.to);
18978 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18979 glyph->face_id,
18980 glyph->left_box_line_p,
18981 glyph->right_box_line_p);
18983 else if (glyph->type == XWIDGET_GLYPH)
18985 #ifndef HAVE_XWIDGETS
18986 eassume (false);
18987 #else
18988 fprintf (stderr,
18989 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18990 glyph - row->glyphs[TEXT_AREA],
18991 'X',
18992 glyph->charpos,
18993 (BUFFERP (glyph->object)
18994 ? 'B'
18995 : (STRINGP (glyph->object)
18996 ? 'S'
18997 : '-')),
18998 glyph->pixel_width,
18999 glyph->u.xwidget,
19000 '.',
19001 glyph->face_id,
19002 glyph->left_box_line_p,
19003 glyph->right_box_line_p);
19004 #endif
19009 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19010 GLYPHS 0 means don't show glyph contents.
19011 GLYPHS 1 means show glyphs in short form
19012 GLYPHS > 1 means show glyphs in long form. */
19014 void
19015 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19017 if (glyphs != 1)
19019 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19020 fprintf (stderr, "==============================================================================\n");
19022 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19023 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19024 vpos,
19025 MATRIX_ROW_START_CHARPOS (row),
19026 MATRIX_ROW_END_CHARPOS (row),
19027 row->used[TEXT_AREA],
19028 row->contains_overlapping_glyphs_p,
19029 row->enabled_p,
19030 row->truncated_on_left_p,
19031 row->truncated_on_right_p,
19032 row->continued_p,
19033 MATRIX_ROW_CONTINUATION_LINE_P (row),
19034 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19035 row->ends_at_zv_p,
19036 row->fill_line_p,
19037 row->ends_in_middle_of_char_p,
19038 row->starts_in_middle_of_char_p,
19039 row->mouse_face_p,
19040 row->x,
19041 row->y,
19042 row->pixel_width,
19043 row->height,
19044 row->visible_height,
19045 row->ascent,
19046 row->phys_ascent);
19047 /* The next 3 lines should align to "Start" in the header. */
19048 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19049 row->end.overlay_string_index,
19050 row->continuation_lines_width);
19051 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19052 CHARPOS (row->start.string_pos),
19053 CHARPOS (row->end.string_pos));
19054 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19055 row->end.dpvec_index);
19058 if (glyphs > 1)
19060 int area;
19062 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19064 struct glyph *glyph = row->glyphs[area];
19065 struct glyph *glyph_end = glyph + row->used[area];
19067 /* Glyph for a line end in text. */
19068 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19069 ++glyph_end;
19071 if (glyph < glyph_end)
19072 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19074 for (; glyph < glyph_end; ++glyph)
19075 dump_glyph (row, glyph, area);
19078 else if (glyphs == 1)
19080 int area;
19081 char s[SHRT_MAX + 4];
19083 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19085 int i;
19087 for (i = 0; i < row->used[area]; ++i)
19089 struct glyph *glyph = row->glyphs[area] + i;
19090 if (i == row->used[area] - 1
19091 && area == TEXT_AREA
19092 && NILP (glyph->object)
19093 && glyph->type == CHAR_GLYPH
19094 && glyph->u.ch == ' ')
19096 strcpy (&s[i], "[\\n]");
19097 i += 4;
19099 else if (glyph->type == CHAR_GLYPH
19100 && glyph->u.ch < 0x80
19101 && glyph->u.ch >= ' ')
19102 s[i] = glyph->u.ch;
19103 else
19104 s[i] = '.';
19107 s[i] = '\0';
19108 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19114 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19115 Sdump_glyph_matrix, 0, 1, "p",
19116 doc: /* Dump the current matrix of the selected window to stderr.
19117 Shows contents of glyph row structures. With non-nil
19118 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19119 glyphs in short form, otherwise show glyphs in long form.
19121 Interactively, no argument means show glyphs in short form;
19122 with numeric argument, its value is passed as the GLYPHS flag. */)
19123 (Lisp_Object glyphs)
19125 struct window *w = XWINDOW (selected_window);
19126 struct buffer *buffer = XBUFFER (w->contents);
19128 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19129 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19130 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19131 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19132 fprintf (stderr, "=============================================\n");
19133 dump_glyph_matrix (w->current_matrix,
19134 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19135 return Qnil;
19139 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19140 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19141 Only text-mode frames have frame glyph matrices. */)
19142 (void)
19144 struct frame *f = XFRAME (selected_frame);
19146 if (f->current_matrix)
19147 dump_glyph_matrix (f->current_matrix, 1);
19148 else
19149 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19150 return Qnil;
19154 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19155 doc: /* Dump glyph row ROW to stderr.
19156 GLYPH 0 means don't dump glyphs.
19157 GLYPH 1 means dump glyphs in short form.
19158 GLYPH > 1 or omitted means dump glyphs in long form. */)
19159 (Lisp_Object row, Lisp_Object glyphs)
19161 struct glyph_matrix *matrix;
19162 EMACS_INT vpos;
19164 CHECK_NUMBER (row);
19165 matrix = XWINDOW (selected_window)->current_matrix;
19166 vpos = XINT (row);
19167 if (vpos >= 0 && vpos < matrix->nrows)
19168 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19169 vpos,
19170 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19171 return Qnil;
19175 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19176 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19177 GLYPH 0 means don't dump glyphs.
19178 GLYPH 1 means dump glyphs in short form.
19179 GLYPH > 1 or omitted means dump glyphs in long form.
19181 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19182 do nothing. */)
19183 (Lisp_Object row, Lisp_Object glyphs)
19185 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19186 struct frame *sf = SELECTED_FRAME ();
19187 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19188 EMACS_INT vpos;
19190 CHECK_NUMBER (row);
19191 vpos = XINT (row);
19192 if (vpos >= 0 && vpos < m->nrows)
19193 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19194 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19195 #endif
19196 return Qnil;
19200 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19201 doc: /* Toggle tracing of redisplay.
19202 With ARG, turn tracing on if and only if ARG is positive. */)
19203 (Lisp_Object arg)
19205 if (NILP (arg))
19206 trace_redisplay_p = !trace_redisplay_p;
19207 else
19209 arg = Fprefix_numeric_value (arg);
19210 trace_redisplay_p = XINT (arg) > 0;
19213 return Qnil;
19217 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19218 doc: /* Like `format', but print result to stderr.
19219 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19220 (ptrdiff_t nargs, Lisp_Object *args)
19222 Lisp_Object s = Fformat (nargs, args);
19223 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19224 return Qnil;
19227 #endif /* GLYPH_DEBUG */
19231 /***********************************************************************
19232 Building Desired Matrix Rows
19233 ***********************************************************************/
19235 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19236 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19238 static struct glyph_row *
19239 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19241 struct frame *f = XFRAME (WINDOW_FRAME (w));
19242 struct buffer *buffer = XBUFFER (w->contents);
19243 struct buffer *old = current_buffer;
19244 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19245 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19246 const unsigned char *arrow_end = arrow_string + arrow_len;
19247 const unsigned char *p;
19248 struct it it;
19249 bool multibyte_p;
19250 int n_glyphs_before;
19252 set_buffer_temp (buffer);
19253 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19254 scratch_glyph_row.reversed_p = false;
19255 it.glyph_row->used[TEXT_AREA] = 0;
19256 SET_TEXT_POS (it.position, 0, 0);
19258 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19259 p = arrow_string;
19260 while (p < arrow_end)
19262 Lisp_Object face, ilisp;
19264 /* Get the next character. */
19265 if (multibyte_p)
19266 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19267 else
19269 it.c = it.char_to_display = *p, it.len = 1;
19270 if (! ASCII_CHAR_P (it.c))
19271 it.char_to_display = BYTE8_TO_CHAR (it.c);
19273 p += it.len;
19275 /* Get its face. */
19276 ilisp = make_number (p - arrow_string);
19277 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19278 it.face_id = compute_char_face (f, it.char_to_display, face);
19280 /* Compute its width, get its glyphs. */
19281 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19282 SET_TEXT_POS (it.position, -1, -1);
19283 PRODUCE_GLYPHS (&it);
19285 /* If this character doesn't fit any more in the line, we have
19286 to remove some glyphs. */
19287 if (it.current_x > it.last_visible_x)
19289 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19290 break;
19294 set_buffer_temp (old);
19295 return it.glyph_row;
19299 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19300 glyphs to insert is determined by produce_special_glyphs. */
19302 static void
19303 insert_left_trunc_glyphs (struct it *it)
19305 struct it truncate_it;
19306 struct glyph *from, *end, *to, *toend;
19308 eassert (!FRAME_WINDOW_P (it->f)
19309 || (!it->glyph_row->reversed_p
19310 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19311 || (it->glyph_row->reversed_p
19312 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19314 /* Get the truncation glyphs. */
19315 truncate_it = *it;
19316 truncate_it.current_x = 0;
19317 truncate_it.face_id = DEFAULT_FACE_ID;
19318 truncate_it.glyph_row = &scratch_glyph_row;
19319 truncate_it.area = TEXT_AREA;
19320 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19321 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19322 truncate_it.object = Qnil;
19323 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19325 /* Overwrite glyphs from IT with truncation glyphs. */
19326 if (!it->glyph_row->reversed_p)
19328 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19330 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19331 end = from + tused;
19332 to = it->glyph_row->glyphs[TEXT_AREA];
19333 toend = to + it->glyph_row->used[TEXT_AREA];
19334 if (FRAME_WINDOW_P (it->f))
19336 /* On GUI frames, when variable-size fonts are displayed,
19337 the truncation glyphs may need more pixels than the row's
19338 glyphs they overwrite. We overwrite more glyphs to free
19339 enough screen real estate, and enlarge the stretch glyph
19340 on the right (see display_line), if there is one, to
19341 preserve the screen position of the truncation glyphs on
19342 the right. */
19343 int w = 0;
19344 struct glyph *g = to;
19345 short used;
19347 /* The first glyph could be partially visible, in which case
19348 it->glyph_row->x will be negative. But we want the left
19349 truncation glyphs to be aligned at the left margin of the
19350 window, so we override the x coordinate at which the row
19351 will begin. */
19352 it->glyph_row->x = 0;
19353 while (g < toend && w < it->truncation_pixel_width)
19355 w += g->pixel_width;
19356 ++g;
19358 if (g - to - tused > 0)
19360 memmove (to + tused, g, (toend - g) * sizeof(*g));
19361 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19363 used = it->glyph_row->used[TEXT_AREA];
19364 if (it->glyph_row->truncated_on_right_p
19365 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19366 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19367 == STRETCH_GLYPH)
19369 int extra = w - it->truncation_pixel_width;
19371 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19375 while (from < end)
19376 *to++ = *from++;
19378 /* There may be padding glyphs left over. Overwrite them too. */
19379 if (!FRAME_WINDOW_P (it->f))
19381 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19383 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19384 while (from < end)
19385 *to++ = *from++;
19389 if (to > toend)
19390 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19392 else
19394 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19396 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19397 that back to front. */
19398 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19399 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19400 toend = it->glyph_row->glyphs[TEXT_AREA];
19401 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19402 if (FRAME_WINDOW_P (it->f))
19404 int w = 0;
19405 struct glyph *g = to;
19407 while (g >= toend && w < it->truncation_pixel_width)
19409 w += g->pixel_width;
19410 --g;
19412 if (to - g - tused > 0)
19413 to = g + tused;
19414 if (it->glyph_row->truncated_on_right_p
19415 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19416 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19418 int extra = w - it->truncation_pixel_width;
19420 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19424 while (from >= end && to >= toend)
19425 *to-- = *from--;
19426 if (!FRAME_WINDOW_P (it->f))
19428 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19430 from =
19431 truncate_it.glyph_row->glyphs[TEXT_AREA]
19432 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19433 while (from >= end && to >= toend)
19434 *to-- = *from--;
19437 if (from >= end)
19439 /* Need to free some room before prepending additional
19440 glyphs. */
19441 int move_by = from - end + 1;
19442 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19443 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19445 for ( ; g >= g0; g--)
19446 g[move_by] = *g;
19447 while (from >= end)
19448 *to-- = *from--;
19449 it->glyph_row->used[TEXT_AREA] += move_by;
19454 /* Compute the hash code for ROW. */
19455 unsigned
19456 row_hash (struct glyph_row *row)
19458 int area, k;
19459 unsigned hashval = 0;
19461 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19462 for (k = 0; k < row->used[area]; ++k)
19463 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19464 + row->glyphs[area][k].u.val
19465 + row->glyphs[area][k].face_id
19466 + row->glyphs[area][k].padding_p
19467 + (row->glyphs[area][k].type << 2));
19469 return hashval;
19472 /* Compute the pixel height and width of IT->glyph_row.
19474 Most of the time, ascent and height of a display line will be equal
19475 to the max_ascent and max_height values of the display iterator
19476 structure. This is not the case if
19478 1. We hit ZV without displaying anything. In this case, max_ascent
19479 and max_height will be zero.
19481 2. We have some glyphs that don't contribute to the line height.
19482 (The glyph row flag contributes_to_line_height_p is for future
19483 pixmap extensions).
19485 The first case is easily covered by using default values because in
19486 these cases, the line height does not really matter, except that it
19487 must not be zero. */
19489 static void
19490 compute_line_metrics (struct it *it)
19492 struct glyph_row *row = it->glyph_row;
19494 if (FRAME_WINDOW_P (it->f))
19496 int i, min_y, max_y;
19498 /* The line may consist of one space only, that was added to
19499 place the cursor on it. If so, the row's height hasn't been
19500 computed yet. */
19501 if (row->height == 0)
19503 if (it->max_ascent + it->max_descent == 0)
19504 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19505 row->ascent = it->max_ascent;
19506 row->height = it->max_ascent + it->max_descent;
19507 row->phys_ascent = it->max_phys_ascent;
19508 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19509 row->extra_line_spacing = it->max_extra_line_spacing;
19512 /* Compute the width of this line. */
19513 row->pixel_width = row->x;
19514 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19515 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19517 eassert (row->pixel_width >= 0);
19518 eassert (row->ascent >= 0 && row->height > 0);
19520 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19521 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19523 /* If first line's physical ascent is larger than its logical
19524 ascent, use the physical ascent, and make the row taller.
19525 This makes accented characters fully visible. */
19526 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19527 && row->phys_ascent > row->ascent)
19529 row->height += row->phys_ascent - row->ascent;
19530 row->ascent = row->phys_ascent;
19533 /* Compute how much of the line is visible. */
19534 row->visible_height = row->height;
19536 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19537 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19539 if (row->y < min_y)
19540 row->visible_height -= min_y - row->y;
19541 if (row->y + row->height > max_y)
19542 row->visible_height -= row->y + row->height - max_y;
19544 else
19546 row->pixel_width = row->used[TEXT_AREA];
19547 if (row->continued_p)
19548 row->pixel_width -= it->continuation_pixel_width;
19549 else if (row->truncated_on_right_p)
19550 row->pixel_width -= it->truncation_pixel_width;
19551 row->ascent = row->phys_ascent = 0;
19552 row->height = row->phys_height = row->visible_height = 1;
19553 row->extra_line_spacing = 0;
19556 /* Compute a hash code for this row. */
19557 row->hash = row_hash (row);
19559 it->max_ascent = it->max_descent = 0;
19560 it->max_phys_ascent = it->max_phys_descent = 0;
19564 /* Append one space to the glyph row of iterator IT if doing a
19565 window-based redisplay. The space has the same face as
19566 IT->face_id. Value is true if a space was added.
19568 This function is called to make sure that there is always one glyph
19569 at the end of a glyph row that the cursor can be set on under
19570 window-systems. (If there weren't such a glyph we would not know
19571 how wide and tall a box cursor should be displayed).
19573 At the same time this space let's a nicely handle clearing to the
19574 end of the line if the row ends in italic text. */
19576 static bool
19577 append_space_for_newline (struct it *it, bool default_face_p)
19579 if (FRAME_WINDOW_P (it->f))
19581 int n = it->glyph_row->used[TEXT_AREA];
19583 if (it->glyph_row->glyphs[TEXT_AREA] + n
19584 < it->glyph_row->glyphs[1 + TEXT_AREA])
19586 /* Save some values that must not be changed.
19587 Must save IT->c and IT->len because otherwise
19588 ITERATOR_AT_END_P wouldn't work anymore after
19589 append_space_for_newline has been called. */
19590 enum display_element_type saved_what = it->what;
19591 int saved_c = it->c, saved_len = it->len;
19592 int saved_char_to_display = it->char_to_display;
19593 int saved_x = it->current_x;
19594 int saved_face_id = it->face_id;
19595 bool saved_box_end = it->end_of_box_run_p;
19596 struct text_pos saved_pos;
19597 Lisp_Object saved_object;
19598 struct face *face;
19600 saved_object = it->object;
19601 saved_pos = it->position;
19603 it->what = IT_CHARACTER;
19604 memset (&it->position, 0, sizeof it->position);
19605 it->object = Qnil;
19606 it->c = it->char_to_display = ' ';
19607 it->len = 1;
19609 /* If the default face was remapped, be sure to use the
19610 remapped face for the appended newline. */
19611 if (default_face_p)
19612 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19613 else if (it->face_before_selective_p)
19614 it->face_id = it->saved_face_id;
19615 face = FACE_FROM_ID (it->f, it->face_id);
19616 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19617 /* In R2L rows, we will prepend a stretch glyph that will
19618 have the end_of_box_run_p flag set for it, so there's no
19619 need for the appended newline glyph to have that flag
19620 set. */
19621 if (it->glyph_row->reversed_p
19622 /* But if the appended newline glyph goes all the way to
19623 the end of the row, there will be no stretch glyph,
19624 so leave the box flag set. */
19625 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19626 it->end_of_box_run_p = false;
19628 PRODUCE_GLYPHS (it);
19630 #ifdef HAVE_WINDOW_SYSTEM
19631 /* Make sure this space glyph has the right ascent and
19632 descent values, or else cursor at end of line will look
19633 funny, and height of empty lines will be incorrect. */
19634 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19635 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19636 if (n == 0)
19638 Lisp_Object height, total_height;
19639 int extra_line_spacing = it->extra_line_spacing;
19640 int boff = font->baseline_offset;
19642 if (font->vertical_centering)
19643 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19645 it->object = saved_object; /* get_it_property needs this */
19646 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19647 /* Must do a subset of line height processing from
19648 x_produce_glyph for newline characters. */
19649 height = get_it_property (it, Qline_height);
19650 if (CONSP (height)
19651 && CONSP (XCDR (height))
19652 && NILP (XCDR (XCDR (height))))
19654 total_height = XCAR (XCDR (height));
19655 height = XCAR (height);
19657 else
19658 total_height = Qnil;
19659 height = calc_line_height_property (it, height, font, boff, true);
19661 if (it->override_ascent >= 0)
19663 it->ascent = it->override_ascent;
19664 it->descent = it->override_descent;
19665 boff = it->override_boff;
19667 if (EQ (height, Qt))
19668 extra_line_spacing = 0;
19669 else
19671 Lisp_Object spacing;
19673 it->phys_ascent = it->ascent;
19674 it->phys_descent = it->descent;
19675 if (!NILP (height)
19676 && XINT (height) > it->ascent + it->descent)
19677 it->ascent = XINT (height) - it->descent;
19679 if (!NILP (total_height))
19680 spacing = calc_line_height_property (it, total_height, font,
19681 boff, false);
19682 else
19684 spacing = get_it_property (it, Qline_spacing);
19685 spacing = calc_line_height_property (it, spacing, font,
19686 boff, false);
19688 if (INTEGERP (spacing))
19690 extra_line_spacing = XINT (spacing);
19691 if (!NILP (total_height))
19692 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19695 if (extra_line_spacing > 0)
19697 it->descent += extra_line_spacing;
19698 if (extra_line_spacing > it->max_extra_line_spacing)
19699 it->max_extra_line_spacing = extra_line_spacing;
19701 it->max_ascent = it->ascent;
19702 it->max_descent = it->descent;
19703 /* Make sure compute_line_metrics recomputes the row height. */
19704 it->glyph_row->height = 0;
19707 g->ascent = it->max_ascent;
19708 g->descent = it->max_descent;
19709 #endif
19711 it->override_ascent = -1;
19712 it->constrain_row_ascent_descent_p = false;
19713 it->current_x = saved_x;
19714 it->object = saved_object;
19715 it->position = saved_pos;
19716 it->what = saved_what;
19717 it->face_id = saved_face_id;
19718 it->len = saved_len;
19719 it->c = saved_c;
19720 it->char_to_display = saved_char_to_display;
19721 it->end_of_box_run_p = saved_box_end;
19722 return true;
19726 return false;
19730 /* Extend the face of the last glyph in the text area of IT->glyph_row
19731 to the end of the display line. Called from display_line. If the
19732 glyph row is empty, add a space glyph to it so that we know the
19733 face to draw. Set the glyph row flag fill_line_p. If the glyph
19734 row is R2L, prepend a stretch glyph to cover the empty space to the
19735 left of the leftmost glyph. */
19737 static void
19738 extend_face_to_end_of_line (struct it *it)
19740 struct face *face, *default_face;
19741 struct frame *f = it->f;
19743 /* If line is already filled, do nothing. Non window-system frames
19744 get a grace of one more ``pixel'' because their characters are
19745 1-``pixel'' wide, so they hit the equality too early. This grace
19746 is needed only for R2L rows that are not continued, to produce
19747 one extra blank where we could display the cursor. */
19748 if ((it->current_x >= it->last_visible_x
19749 + (!FRAME_WINDOW_P (f)
19750 && it->glyph_row->reversed_p
19751 && !it->glyph_row->continued_p))
19752 /* If the window has display margins, we will need to extend
19753 their face even if the text area is filled. */
19754 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19755 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19756 return;
19758 /* The default face, possibly remapped. */
19759 default_face = FACE_FROM_ID_OR_NULL (f,
19760 lookup_basic_face (f, DEFAULT_FACE_ID));
19762 /* Face extension extends the background and box of IT->face_id
19763 to the end of the line. If the background equals the background
19764 of the frame, we don't have to do anything. */
19765 face = FACE_FROM_ID (f, (it->face_before_selective_p
19766 ? it->saved_face_id
19767 : it->face_id));
19769 if (FRAME_WINDOW_P (f)
19770 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19771 && face->box == FACE_NO_BOX
19772 && face->background == FRAME_BACKGROUND_PIXEL (f)
19773 #ifdef HAVE_WINDOW_SYSTEM
19774 && !face->stipple
19775 #endif
19776 && !it->glyph_row->reversed_p)
19777 return;
19779 /* Set the glyph row flag indicating that the face of the last glyph
19780 in the text area has to be drawn to the end of the text area. */
19781 it->glyph_row->fill_line_p = true;
19783 /* If current character of IT is not ASCII, make sure we have the
19784 ASCII face. This will be automatically undone the next time
19785 get_next_display_element returns a multibyte character. Note
19786 that the character will always be single byte in unibyte
19787 text. */
19788 if (!ASCII_CHAR_P (it->c))
19790 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19793 if (FRAME_WINDOW_P (f))
19795 /* If the row is empty, add a space with the current face of IT,
19796 so that we know which face to draw. */
19797 if (it->glyph_row->used[TEXT_AREA] == 0)
19799 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19800 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19801 it->glyph_row->used[TEXT_AREA] = 1;
19803 /* Mode line and the header line don't have margins, and
19804 likewise the frame's tool-bar window, if there is any. */
19805 if (!(it->glyph_row->mode_line_p
19806 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19807 || (WINDOWP (f->tool_bar_window)
19808 && it->w == XWINDOW (f->tool_bar_window))
19809 #endif
19812 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19813 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19815 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19816 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19817 default_face->id;
19818 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19820 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19821 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19823 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19824 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19825 default_face->id;
19826 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19829 #ifdef HAVE_WINDOW_SYSTEM
19830 if (it->glyph_row->reversed_p)
19832 /* Prepend a stretch glyph to the row, such that the
19833 rightmost glyph will be drawn flushed all the way to the
19834 right margin of the window. The stretch glyph that will
19835 occupy the empty space, if any, to the left of the
19836 glyphs. */
19837 struct font *font = face->font ? face->font : FRAME_FONT (f);
19838 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19839 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19840 struct glyph *g;
19841 int row_width, stretch_ascent, stretch_width;
19842 struct text_pos saved_pos;
19843 int saved_face_id;
19844 bool saved_avoid_cursor, saved_box_start;
19846 for (row_width = 0, g = row_start; g < row_end; g++)
19847 row_width += g->pixel_width;
19849 /* FIXME: There are various minor display glitches in R2L
19850 rows when only one of the fringes is missing. The
19851 strange condition below produces the least bad effect. */
19852 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19853 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19854 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19855 stretch_width = window_box_width (it->w, TEXT_AREA);
19856 else
19857 stretch_width = it->last_visible_x - it->first_visible_x;
19858 stretch_width -= row_width;
19860 if (stretch_width > 0)
19862 stretch_ascent =
19863 (((it->ascent + it->descent)
19864 * FONT_BASE (font)) / FONT_HEIGHT (font));
19865 saved_pos = it->position;
19866 memset (&it->position, 0, sizeof it->position);
19867 saved_avoid_cursor = it->avoid_cursor_p;
19868 it->avoid_cursor_p = true;
19869 saved_face_id = it->face_id;
19870 saved_box_start = it->start_of_box_run_p;
19871 /* The last row's stretch glyph should get the default
19872 face, to avoid painting the rest of the window with
19873 the region face, if the region ends at ZV. */
19874 if (it->glyph_row->ends_at_zv_p)
19875 it->face_id = default_face->id;
19876 else
19877 it->face_id = face->id;
19878 it->start_of_box_run_p = false;
19879 append_stretch_glyph (it, Qnil, stretch_width,
19880 it->ascent + it->descent, stretch_ascent);
19881 it->position = saved_pos;
19882 it->avoid_cursor_p = saved_avoid_cursor;
19883 it->face_id = saved_face_id;
19884 it->start_of_box_run_p = saved_box_start;
19886 /* If stretch_width comes out negative, it means that the
19887 last glyph is only partially visible. In R2L rows, we
19888 want the leftmost glyph to be partially visible, so we
19889 need to give the row the corresponding left offset. */
19890 if (stretch_width < 0)
19891 it->glyph_row->x = stretch_width;
19893 #endif /* HAVE_WINDOW_SYSTEM */
19895 else
19897 /* Save some values that must not be changed. */
19898 int saved_x = it->current_x;
19899 struct text_pos saved_pos;
19900 Lisp_Object saved_object;
19901 enum display_element_type saved_what = it->what;
19902 int saved_face_id = it->face_id;
19904 saved_object = it->object;
19905 saved_pos = it->position;
19907 it->what = IT_CHARACTER;
19908 memset (&it->position, 0, sizeof it->position);
19909 it->object = Qnil;
19910 it->c = it->char_to_display = ' ';
19911 it->len = 1;
19913 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19914 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19915 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19916 && !it->glyph_row->mode_line_p
19917 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19919 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19920 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19922 for (it->current_x = 0; g < e; g++)
19923 it->current_x += g->pixel_width;
19925 it->area = LEFT_MARGIN_AREA;
19926 it->face_id = default_face->id;
19927 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19928 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19930 PRODUCE_GLYPHS (it);
19931 /* term.c:produce_glyphs advances it->current_x only for
19932 TEXT_AREA. */
19933 it->current_x += it->pixel_width;
19936 it->current_x = saved_x;
19937 it->area = TEXT_AREA;
19940 /* The last row's blank glyphs should get the default face, to
19941 avoid painting the rest of the window with the region face,
19942 if the region ends at ZV. */
19943 if (it->glyph_row->ends_at_zv_p)
19944 it->face_id = default_face->id;
19945 else
19946 it->face_id = face->id;
19947 PRODUCE_GLYPHS (it);
19949 while (it->current_x <= it->last_visible_x)
19950 PRODUCE_GLYPHS (it);
19952 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19953 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19954 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19955 && !it->glyph_row->mode_line_p
19956 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19958 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19959 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19961 for ( ; g < e; g++)
19962 it->current_x += g->pixel_width;
19964 it->area = RIGHT_MARGIN_AREA;
19965 it->face_id = default_face->id;
19966 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19967 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19969 PRODUCE_GLYPHS (it);
19970 it->current_x += it->pixel_width;
19973 it->area = TEXT_AREA;
19976 /* Don't count these blanks really. It would let us insert a left
19977 truncation glyph below and make us set the cursor on them, maybe. */
19978 it->current_x = saved_x;
19979 it->object = saved_object;
19980 it->position = saved_pos;
19981 it->what = saved_what;
19982 it->face_id = saved_face_id;
19987 /* Value is true if text starting at CHARPOS in current_buffer is
19988 trailing whitespace. */
19990 static bool
19991 trailing_whitespace_p (ptrdiff_t charpos)
19993 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19994 int c = 0;
19996 while (bytepos < ZV_BYTE
19997 && (c = FETCH_CHAR (bytepos),
19998 c == ' ' || c == '\t'))
19999 ++bytepos;
20001 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20003 if (bytepos != PT_BYTE)
20004 return true;
20006 return false;
20010 /* Highlight trailing whitespace, if any, in ROW. */
20012 static void
20013 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20015 int used = row->used[TEXT_AREA];
20017 if (used)
20019 struct glyph *start = row->glyphs[TEXT_AREA];
20020 struct glyph *glyph = start + used - 1;
20022 if (row->reversed_p)
20024 /* Right-to-left rows need to be processed in the opposite
20025 direction, so swap the edge pointers. */
20026 glyph = start;
20027 start = row->glyphs[TEXT_AREA] + used - 1;
20030 /* Skip over glyphs inserted to display the cursor at the
20031 end of a line, for extending the face of the last glyph
20032 to the end of the line on terminals, and for truncation
20033 and continuation glyphs. */
20034 if (!row->reversed_p)
20036 while (glyph >= start
20037 && glyph->type == CHAR_GLYPH
20038 && NILP (glyph->object))
20039 --glyph;
20041 else
20043 while (glyph <= start
20044 && glyph->type == CHAR_GLYPH
20045 && NILP (glyph->object))
20046 ++glyph;
20049 /* If last glyph is a space or stretch, and it's trailing
20050 whitespace, set the face of all trailing whitespace glyphs in
20051 IT->glyph_row to `trailing-whitespace'. */
20052 if ((row->reversed_p ? glyph <= start : glyph >= start)
20053 && BUFFERP (glyph->object)
20054 && (glyph->type == STRETCH_GLYPH
20055 || (glyph->type == CHAR_GLYPH
20056 && glyph->u.ch == ' '))
20057 && trailing_whitespace_p (glyph->charpos))
20059 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20060 if (face_id < 0)
20061 return;
20063 if (!row->reversed_p)
20065 while (glyph >= start
20066 && BUFFERP (glyph->object)
20067 && (glyph->type == STRETCH_GLYPH
20068 || (glyph->type == CHAR_GLYPH
20069 && glyph->u.ch == ' ')))
20070 (glyph--)->face_id = face_id;
20072 else
20074 while (glyph <= start
20075 && BUFFERP (glyph->object)
20076 && (glyph->type == STRETCH_GLYPH
20077 || (glyph->type == CHAR_GLYPH
20078 && glyph->u.ch == ' ')))
20079 (glyph++)->face_id = face_id;
20086 /* Value is true if glyph row ROW should be
20087 considered to hold the buffer position CHARPOS. */
20089 static bool
20090 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20092 bool result = true;
20094 if (charpos == CHARPOS (row->end.pos)
20095 || charpos == MATRIX_ROW_END_CHARPOS (row))
20097 /* Suppose the row ends on a string.
20098 Unless the row is continued, that means it ends on a newline
20099 in the string. If it's anything other than a display string
20100 (e.g., a before-string from an overlay), we don't want the
20101 cursor there. (This heuristic seems to give the optimal
20102 behavior for the various types of multi-line strings.)
20103 One exception: if the string has `cursor' property on one of
20104 its characters, we _do_ want the cursor there. */
20105 if (CHARPOS (row->end.string_pos) >= 0)
20107 if (row->continued_p)
20108 result = true;
20109 else
20111 /* Check for `display' property. */
20112 struct glyph *beg = row->glyphs[TEXT_AREA];
20113 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20114 struct glyph *glyph;
20116 result = false;
20117 for (glyph = end; glyph >= beg; --glyph)
20118 if (STRINGP (glyph->object))
20120 Lisp_Object prop
20121 = Fget_char_property (make_number (charpos),
20122 Qdisplay, Qnil);
20123 result =
20124 (!NILP (prop)
20125 && display_prop_string_p (prop, glyph->object));
20126 /* If there's a `cursor' property on one of the
20127 string's characters, this row is a cursor row,
20128 even though this is not a display string. */
20129 if (!result)
20131 Lisp_Object s = glyph->object;
20133 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20135 ptrdiff_t gpos = glyph->charpos;
20137 if (!NILP (Fget_char_property (make_number (gpos),
20138 Qcursor, s)))
20140 result = true;
20141 break;
20145 break;
20149 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20151 /* If the row ends in middle of a real character,
20152 and the line is continued, we want the cursor here.
20153 That's because CHARPOS (ROW->end.pos) would equal
20154 PT if PT is before the character. */
20155 if (!row->ends_in_ellipsis_p)
20156 result = row->continued_p;
20157 else
20158 /* If the row ends in an ellipsis, then
20159 CHARPOS (ROW->end.pos) will equal point after the
20160 invisible text. We want that position to be displayed
20161 after the ellipsis. */
20162 result = false;
20164 /* If the row ends at ZV, display the cursor at the end of that
20165 row instead of at the start of the row below. */
20166 else
20167 result = row->ends_at_zv_p;
20170 return result;
20173 /* Value is true if glyph row ROW should be
20174 used to hold the cursor. */
20176 static bool
20177 cursor_row_p (struct glyph_row *row)
20179 return row_for_charpos_p (row, PT);
20184 /* Push the property PROP so that it will be rendered at the current
20185 position in IT. Return true if PROP was successfully pushed, false
20186 otherwise. Called from handle_line_prefix to handle the
20187 `line-prefix' and `wrap-prefix' properties. */
20189 static bool
20190 push_prefix_prop (struct it *it, Lisp_Object prop)
20192 struct text_pos pos =
20193 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20195 eassert (it->method == GET_FROM_BUFFER
20196 || it->method == GET_FROM_DISPLAY_VECTOR
20197 || it->method == GET_FROM_STRING
20198 || it->method == GET_FROM_IMAGE);
20200 /* We need to save the current buffer/string position, so it will be
20201 restored by pop_it, because iterate_out_of_display_property
20202 depends on that being set correctly, but some situations leave
20203 it->position not yet set when this function is called. */
20204 push_it (it, &pos);
20206 if (STRINGP (prop))
20208 if (SCHARS (prop) == 0)
20210 pop_it (it);
20211 return false;
20214 it->string = prop;
20215 it->string_from_prefix_prop_p = true;
20216 it->multibyte_p = STRING_MULTIBYTE (it->string);
20217 it->current.overlay_string_index = -1;
20218 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20219 it->end_charpos = it->string_nchars = SCHARS (it->string);
20220 it->method = GET_FROM_STRING;
20221 it->stop_charpos = 0;
20222 it->prev_stop = 0;
20223 it->base_level_stop = 0;
20225 /* Force paragraph direction to be that of the parent
20226 buffer/string. */
20227 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20228 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20229 else
20230 it->paragraph_embedding = L2R;
20232 /* Set up the bidi iterator for this display string. */
20233 if (it->bidi_p)
20235 it->bidi_it.string.lstring = it->string;
20236 it->bidi_it.string.s = NULL;
20237 it->bidi_it.string.schars = it->end_charpos;
20238 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20239 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20240 it->bidi_it.string.unibyte = !it->multibyte_p;
20241 it->bidi_it.w = it->w;
20242 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20245 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20247 it->method = GET_FROM_STRETCH;
20248 it->object = prop;
20250 #ifdef HAVE_WINDOW_SYSTEM
20251 else if (IMAGEP (prop))
20253 it->what = IT_IMAGE;
20254 it->image_id = lookup_image (it->f, prop);
20255 it->method = GET_FROM_IMAGE;
20257 #endif /* HAVE_WINDOW_SYSTEM */
20258 else
20260 pop_it (it); /* bogus display property, give up */
20261 return false;
20264 return true;
20267 /* Return the character-property PROP at the current position in IT. */
20269 static Lisp_Object
20270 get_it_property (struct it *it, Lisp_Object prop)
20272 Lisp_Object position, object = it->object;
20274 if (STRINGP (object))
20275 position = make_number (IT_STRING_CHARPOS (*it));
20276 else if (BUFFERP (object))
20278 position = make_number (IT_CHARPOS (*it));
20279 object = it->window;
20281 else
20282 return Qnil;
20284 return Fget_char_property (position, prop, object);
20287 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20289 static void
20290 handle_line_prefix (struct it *it)
20292 Lisp_Object prefix;
20294 if (it->continuation_lines_width > 0)
20296 prefix = get_it_property (it, Qwrap_prefix);
20297 if (NILP (prefix))
20298 prefix = Vwrap_prefix;
20300 else
20302 prefix = get_it_property (it, Qline_prefix);
20303 if (NILP (prefix))
20304 prefix = Vline_prefix;
20306 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20308 /* If the prefix is wider than the window, and we try to wrap
20309 it, it would acquire its own wrap prefix, and so on till the
20310 iterator stack overflows. So, don't wrap the prefix. */
20311 it->line_wrap = TRUNCATE;
20312 it->avoid_cursor_p = true;
20318 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20319 only for R2L lines from display_line and display_string, when they
20320 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20321 the line/string needs to be continued on the next glyph row. */
20322 static void
20323 unproduce_glyphs (struct it *it, int n)
20325 struct glyph *glyph, *end;
20327 eassert (it->glyph_row);
20328 eassert (it->glyph_row->reversed_p);
20329 eassert (it->area == TEXT_AREA);
20330 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20332 if (n > it->glyph_row->used[TEXT_AREA])
20333 n = it->glyph_row->used[TEXT_AREA];
20334 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20335 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20336 for ( ; glyph < end; glyph++)
20337 glyph[-n] = *glyph;
20340 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20341 and ROW->maxpos. */
20342 static void
20343 find_row_edges (struct it *it, struct glyph_row *row,
20344 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20345 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20347 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20348 lines' rows is implemented for bidi-reordered rows. */
20350 /* ROW->minpos is the value of min_pos, the minimal buffer position
20351 we have in ROW, or ROW->start.pos if that is smaller. */
20352 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20353 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20354 else
20355 /* We didn't find buffer positions smaller than ROW->start, or
20356 didn't find _any_ valid buffer positions in any of the glyphs,
20357 so we must trust the iterator's computed positions. */
20358 row->minpos = row->start.pos;
20359 if (max_pos <= 0)
20361 max_pos = CHARPOS (it->current.pos);
20362 max_bpos = BYTEPOS (it->current.pos);
20365 /* Here are the various use-cases for ending the row, and the
20366 corresponding values for ROW->maxpos:
20368 Line ends in a newline from buffer eol_pos + 1
20369 Line is continued from buffer max_pos + 1
20370 Line is truncated on right it->current.pos
20371 Line ends in a newline from string max_pos + 1(*)
20372 (*) + 1 only when line ends in a forward scan
20373 Line is continued from string max_pos
20374 Line is continued from display vector max_pos
20375 Line is entirely from a string min_pos == max_pos
20376 Line is entirely from a display vector min_pos == max_pos
20377 Line that ends at ZV ZV
20379 If you discover other use-cases, please add them here as
20380 appropriate. */
20381 if (row->ends_at_zv_p)
20382 row->maxpos = it->current.pos;
20383 else if (row->used[TEXT_AREA])
20385 bool seen_this_string = false;
20386 struct glyph_row *r1 = row - 1;
20388 /* Did we see the same display string on the previous row? */
20389 if (STRINGP (it->object)
20390 /* this is not the first row */
20391 && row > it->w->desired_matrix->rows
20392 /* previous row is not the header line */
20393 && !r1->mode_line_p
20394 /* previous row also ends in a newline from a string */
20395 && r1->ends_in_newline_from_string_p)
20397 struct glyph *start, *end;
20399 /* Search for the last glyph of the previous row that came
20400 from buffer or string. Depending on whether the row is
20401 L2R or R2L, we need to process it front to back or the
20402 other way round. */
20403 if (!r1->reversed_p)
20405 start = r1->glyphs[TEXT_AREA];
20406 end = start + r1->used[TEXT_AREA];
20407 /* Glyphs inserted by redisplay have nil as their object. */
20408 while (end > start
20409 && NILP ((end - 1)->object)
20410 && (end - 1)->charpos <= 0)
20411 --end;
20412 if (end > start)
20414 if (EQ ((end - 1)->object, it->object))
20415 seen_this_string = true;
20417 else
20418 /* If all the glyphs of the previous row were inserted
20419 by redisplay, it means the previous row was
20420 produced from a single newline, which is only
20421 possible if that newline came from the same string
20422 as the one which produced this ROW. */
20423 seen_this_string = true;
20425 else
20427 end = r1->glyphs[TEXT_AREA] - 1;
20428 start = end + r1->used[TEXT_AREA];
20429 while (end < start
20430 && NILP ((end + 1)->object)
20431 && (end + 1)->charpos <= 0)
20432 ++end;
20433 if (end < start)
20435 if (EQ ((end + 1)->object, it->object))
20436 seen_this_string = true;
20438 else
20439 seen_this_string = true;
20442 /* Take note of each display string that covers a newline only
20443 once, the first time we see it. This is for when a display
20444 string includes more than one newline in it. */
20445 if (row->ends_in_newline_from_string_p && !seen_this_string)
20447 /* If we were scanning the buffer forward when we displayed
20448 the string, we want to account for at least one buffer
20449 position that belongs to this row (position covered by
20450 the display string), so that cursor positioning will
20451 consider this row as a candidate when point is at the end
20452 of the visual line represented by this row. This is not
20453 required when scanning back, because max_pos will already
20454 have a much larger value. */
20455 if (CHARPOS (row->end.pos) > max_pos)
20456 INC_BOTH (max_pos, max_bpos);
20457 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20459 else if (CHARPOS (it->eol_pos) > 0)
20460 SET_TEXT_POS (row->maxpos,
20461 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20462 else if (row->continued_p)
20464 /* If max_pos is different from IT's current position, it
20465 means IT->method does not belong to the display element
20466 at max_pos. However, it also means that the display
20467 element at max_pos was displayed in its entirety on this
20468 line, which is equivalent to saying that the next line
20469 starts at the next buffer position. */
20470 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20471 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20472 else
20474 INC_BOTH (max_pos, max_bpos);
20475 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20478 else if (row->truncated_on_right_p)
20479 /* display_line already called reseat_at_next_visible_line_start,
20480 which puts the iterator at the beginning of the next line, in
20481 the logical order. */
20482 row->maxpos = it->current.pos;
20483 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20484 /* A line that is entirely from a string/image/stretch... */
20485 row->maxpos = row->minpos;
20486 else
20487 emacs_abort ();
20489 else
20490 row->maxpos = it->current.pos;
20493 /* Construct the glyph row IT->glyph_row in the desired matrix of
20494 IT->w from text at the current position of IT. See dispextern.h
20495 for an overview of struct it. Value is true if
20496 IT->glyph_row displays text, as opposed to a line displaying ZV
20497 only. */
20499 static bool
20500 display_line (struct it *it)
20502 struct glyph_row *row = it->glyph_row;
20503 Lisp_Object overlay_arrow_string;
20504 struct it wrap_it;
20505 void *wrap_data = NULL;
20506 bool may_wrap = false;
20507 int wrap_x UNINIT;
20508 int wrap_row_used = -1;
20509 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20510 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20511 int wrap_row_extra_line_spacing UNINIT;
20512 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20513 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20514 int cvpos;
20515 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20516 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20517 bool pending_handle_line_prefix = false;
20519 /* We always start displaying at hpos zero even if hscrolled. */
20520 eassert (it->hpos == 0 && it->current_x == 0);
20522 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20523 >= it->w->desired_matrix->nrows)
20525 it->w->nrows_scale_factor++;
20526 it->f->fonts_changed = true;
20527 return false;
20530 /* Clear the result glyph row and enable it. */
20531 prepare_desired_row (it->w, row, false);
20533 row->y = it->current_y;
20534 row->start = it->start;
20535 row->continuation_lines_width = it->continuation_lines_width;
20536 row->displays_text_p = true;
20537 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20538 it->starts_in_middle_of_char_p = false;
20540 /* Arrange the overlays nicely for our purposes. Usually, we call
20541 display_line on only one line at a time, in which case this
20542 can't really hurt too much, or we call it on lines which appear
20543 one after another in the buffer, in which case all calls to
20544 recenter_overlay_lists but the first will be pretty cheap. */
20545 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20547 /* Move over display elements that are not visible because we are
20548 hscrolled. This may stop at an x-position < IT->first_visible_x
20549 if the first glyph is partially visible or if we hit a line end. */
20550 if (it->current_x < it->first_visible_x)
20552 enum move_it_result move_result;
20554 this_line_min_pos = row->start.pos;
20555 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20556 MOVE_TO_POS | MOVE_TO_X);
20557 /* If we are under a large hscroll, move_it_in_display_line_to
20558 could hit the end of the line without reaching
20559 it->first_visible_x. Pretend that we did reach it. This is
20560 especially important on a TTY, where we will call
20561 extend_face_to_end_of_line, which needs to know how many
20562 blank glyphs to produce. */
20563 if (it->current_x < it->first_visible_x
20564 && (move_result == MOVE_NEWLINE_OR_CR
20565 || move_result == MOVE_POS_MATCH_OR_ZV))
20566 it->current_x = it->first_visible_x;
20568 /* Record the smallest positions seen while we moved over
20569 display elements that are not visible. This is needed by
20570 redisplay_internal for optimizing the case where the cursor
20571 stays inside the same line. The rest of this function only
20572 considers positions that are actually displayed, so
20573 RECORD_MAX_MIN_POS will not otherwise record positions that
20574 are hscrolled to the left of the left edge of the window. */
20575 min_pos = CHARPOS (this_line_min_pos);
20576 min_bpos = BYTEPOS (this_line_min_pos);
20578 else if (it->area == TEXT_AREA)
20580 /* We only do this when not calling move_it_in_display_line_to
20581 above, because that function calls itself handle_line_prefix. */
20582 handle_line_prefix (it);
20584 else
20586 /* Line-prefix and wrap-prefix are always displayed in the text
20587 area. But if this is the first call to display_line after
20588 init_iterator, the iterator might have been set up to write
20589 into a marginal area, e.g. if the line begins with some
20590 display property that writes to the margins. So we need to
20591 wait with the call to handle_line_prefix until whatever
20592 writes to the margin has done its job. */
20593 pending_handle_line_prefix = true;
20596 /* Get the initial row height. This is either the height of the
20597 text hscrolled, if there is any, or zero. */
20598 row->ascent = it->max_ascent;
20599 row->height = it->max_ascent + it->max_descent;
20600 row->phys_ascent = it->max_phys_ascent;
20601 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20602 row->extra_line_spacing = it->max_extra_line_spacing;
20604 /* Utility macro to record max and min buffer positions seen until now. */
20605 #define RECORD_MAX_MIN_POS(IT) \
20606 do \
20608 bool composition_p \
20609 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20610 ptrdiff_t current_pos = \
20611 composition_p ? (IT)->cmp_it.charpos \
20612 : IT_CHARPOS (*(IT)); \
20613 ptrdiff_t current_bpos = \
20614 composition_p ? CHAR_TO_BYTE (current_pos) \
20615 : IT_BYTEPOS (*(IT)); \
20616 if (current_pos < min_pos) \
20618 min_pos = current_pos; \
20619 min_bpos = current_bpos; \
20621 if (IT_CHARPOS (*it) > max_pos) \
20623 max_pos = IT_CHARPOS (*it); \
20624 max_bpos = IT_BYTEPOS (*it); \
20627 while (false)
20629 /* Loop generating characters. The loop is left with IT on the next
20630 character to display. */
20631 while (true)
20633 int n_glyphs_before, hpos_before, x_before;
20634 int x, nglyphs;
20635 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20637 /* Retrieve the next thing to display. Value is false if end of
20638 buffer reached. */
20639 if (!get_next_display_element (it))
20641 /* Maybe add a space at the end of this line that is used to
20642 display the cursor there under X. Set the charpos of the
20643 first glyph of blank lines not corresponding to any text
20644 to -1. */
20645 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20646 row->exact_window_width_line_p = true;
20647 else if ((append_space_for_newline (it, true)
20648 && row->used[TEXT_AREA] == 1)
20649 || row->used[TEXT_AREA] == 0)
20651 row->glyphs[TEXT_AREA]->charpos = -1;
20652 row->displays_text_p = false;
20654 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20655 && (!MINI_WINDOW_P (it->w)
20656 || (minibuf_level && EQ (it->window, minibuf_window))))
20657 row->indicate_empty_line_p = true;
20660 it->continuation_lines_width = 0;
20661 row->ends_at_zv_p = true;
20662 /* A row that displays right-to-left text must always have
20663 its last face extended all the way to the end of line,
20664 even if this row ends in ZV, because we still write to
20665 the screen left to right. We also need to extend the
20666 last face if the default face is remapped to some
20667 different face, otherwise the functions that clear
20668 portions of the screen will clear with the default face's
20669 background color. */
20670 if (row->reversed_p
20671 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20672 extend_face_to_end_of_line (it);
20673 break;
20676 /* Now, get the metrics of what we want to display. This also
20677 generates glyphs in `row' (which is IT->glyph_row). */
20678 n_glyphs_before = row->used[TEXT_AREA];
20679 x = it->current_x;
20681 /* Remember the line height so far in case the next element doesn't
20682 fit on the line. */
20683 if (it->line_wrap != TRUNCATE)
20685 ascent = it->max_ascent;
20686 descent = it->max_descent;
20687 phys_ascent = it->max_phys_ascent;
20688 phys_descent = it->max_phys_descent;
20690 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20692 if (IT_DISPLAYING_WHITESPACE (it))
20693 may_wrap = true;
20694 else if (may_wrap)
20696 SAVE_IT (wrap_it, *it, wrap_data);
20697 wrap_x = x;
20698 wrap_row_used = row->used[TEXT_AREA];
20699 wrap_row_ascent = row->ascent;
20700 wrap_row_height = row->height;
20701 wrap_row_phys_ascent = row->phys_ascent;
20702 wrap_row_phys_height = row->phys_height;
20703 wrap_row_extra_line_spacing = row->extra_line_spacing;
20704 wrap_row_min_pos = min_pos;
20705 wrap_row_min_bpos = min_bpos;
20706 wrap_row_max_pos = max_pos;
20707 wrap_row_max_bpos = max_bpos;
20708 may_wrap = false;
20713 PRODUCE_GLYPHS (it);
20715 /* If this display element was in marginal areas, continue with
20716 the next one. */
20717 if (it->area != TEXT_AREA)
20719 row->ascent = max (row->ascent, it->max_ascent);
20720 row->height = max (row->height, it->max_ascent + it->max_descent);
20721 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20722 row->phys_height = max (row->phys_height,
20723 it->max_phys_ascent + it->max_phys_descent);
20724 row->extra_line_spacing = max (row->extra_line_spacing,
20725 it->max_extra_line_spacing);
20726 set_iterator_to_next (it, true);
20727 /* If we didn't handle the line/wrap prefix above, and the
20728 call to set_iterator_to_next just switched to TEXT_AREA,
20729 process the prefix now. */
20730 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20732 pending_handle_line_prefix = false;
20733 handle_line_prefix (it);
20735 continue;
20738 /* Does the display element fit on the line? If we truncate
20739 lines, we should draw past the right edge of the window. If
20740 we don't truncate, we want to stop so that we can display the
20741 continuation glyph before the right margin. If lines are
20742 continued, there are two possible strategies for characters
20743 resulting in more than 1 glyph (e.g. tabs): Display as many
20744 glyphs as possible in this line and leave the rest for the
20745 continuation line, or display the whole element in the next
20746 line. Original redisplay did the former, so we do it also. */
20747 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20748 hpos_before = it->hpos;
20749 x_before = x;
20751 if (/* Not a newline. */
20752 nglyphs > 0
20753 /* Glyphs produced fit entirely in the line. */
20754 && it->current_x < it->last_visible_x)
20756 it->hpos += nglyphs;
20757 row->ascent = max (row->ascent, it->max_ascent);
20758 row->height = max (row->height, it->max_ascent + it->max_descent);
20759 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20760 row->phys_height = max (row->phys_height,
20761 it->max_phys_ascent + it->max_phys_descent);
20762 row->extra_line_spacing = max (row->extra_line_spacing,
20763 it->max_extra_line_spacing);
20764 if (it->current_x - it->pixel_width < it->first_visible_x
20765 /* In R2L rows, we arrange in extend_face_to_end_of_line
20766 to add a right offset to the line, by a suitable
20767 change to the stretch glyph that is the leftmost
20768 glyph of the line. */
20769 && !row->reversed_p)
20770 row->x = x - it->first_visible_x;
20771 /* Record the maximum and minimum buffer positions seen so
20772 far in glyphs that will be displayed by this row. */
20773 if (it->bidi_p)
20774 RECORD_MAX_MIN_POS (it);
20776 else
20778 int i, new_x;
20779 struct glyph *glyph;
20781 for (i = 0; i < nglyphs; ++i, x = new_x)
20783 /* Identify the glyphs added by the last call to
20784 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20785 the previous glyphs. */
20786 if (!row->reversed_p)
20787 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20788 else
20789 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20790 new_x = x + glyph->pixel_width;
20792 if (/* Lines are continued. */
20793 it->line_wrap != TRUNCATE
20794 && (/* Glyph doesn't fit on the line. */
20795 new_x > it->last_visible_x
20796 /* Or it fits exactly on a window system frame. */
20797 || (new_x == it->last_visible_x
20798 && FRAME_WINDOW_P (it->f)
20799 && (row->reversed_p
20800 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20801 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20803 /* End of a continued line. */
20805 if (it->hpos == 0
20806 || (new_x == it->last_visible_x
20807 && FRAME_WINDOW_P (it->f)
20808 && (row->reversed_p
20809 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20810 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20812 /* Current glyph is the only one on the line or
20813 fits exactly on the line. We must continue
20814 the line because we can't draw the cursor
20815 after the glyph. */
20816 row->continued_p = true;
20817 it->current_x = new_x;
20818 it->continuation_lines_width += new_x;
20819 ++it->hpos;
20820 if (i == nglyphs - 1)
20822 /* If line-wrap is on, check if a previous
20823 wrap point was found. */
20824 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20825 && wrap_row_used > 0
20826 /* Even if there is a previous wrap
20827 point, continue the line here as
20828 usual, if (i) the previous character
20829 was a space or tab AND (ii) the
20830 current character is not. */
20831 && (!may_wrap
20832 || IT_DISPLAYING_WHITESPACE (it)))
20833 goto back_to_wrap;
20835 /* Record the maximum and minimum buffer
20836 positions seen so far in glyphs that will be
20837 displayed by this row. */
20838 if (it->bidi_p)
20839 RECORD_MAX_MIN_POS (it);
20840 set_iterator_to_next (it, true);
20841 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20843 if (!get_next_display_element (it))
20845 row->exact_window_width_line_p = true;
20846 it->continuation_lines_width = 0;
20847 row->continued_p = false;
20848 row->ends_at_zv_p = true;
20850 else if (ITERATOR_AT_END_OF_LINE_P (it))
20852 row->continued_p = false;
20853 row->exact_window_width_line_p = true;
20855 /* If line-wrap is on, check if a
20856 previous wrap point was found. */
20857 else if (wrap_row_used > 0
20858 /* Even if there is a previous wrap
20859 point, continue the line here as
20860 usual, if (i) the previous character
20861 was a space or tab AND (ii) the
20862 current character is not. */
20863 && (!may_wrap
20864 || IT_DISPLAYING_WHITESPACE (it)))
20865 goto back_to_wrap;
20869 else if (it->bidi_p)
20870 RECORD_MAX_MIN_POS (it);
20871 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20872 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20873 extend_face_to_end_of_line (it);
20875 else if (CHAR_GLYPH_PADDING_P (*glyph)
20876 && !FRAME_WINDOW_P (it->f))
20878 /* A padding glyph that doesn't fit on this line.
20879 This means the whole character doesn't fit
20880 on the line. */
20881 if (row->reversed_p)
20882 unproduce_glyphs (it, row->used[TEXT_AREA]
20883 - n_glyphs_before);
20884 row->used[TEXT_AREA] = n_glyphs_before;
20886 /* Fill the rest of the row with continuation
20887 glyphs like in 20.x. */
20888 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20889 < row->glyphs[1 + TEXT_AREA])
20890 produce_special_glyphs (it, IT_CONTINUATION);
20892 row->continued_p = true;
20893 it->current_x = x_before;
20894 it->continuation_lines_width += x_before;
20896 /* Restore the height to what it was before the
20897 element not fitting on the line. */
20898 it->max_ascent = ascent;
20899 it->max_descent = descent;
20900 it->max_phys_ascent = phys_ascent;
20901 it->max_phys_descent = phys_descent;
20902 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20903 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20904 extend_face_to_end_of_line (it);
20906 else if (wrap_row_used > 0)
20908 back_to_wrap:
20909 if (row->reversed_p)
20910 unproduce_glyphs (it,
20911 row->used[TEXT_AREA] - wrap_row_used);
20912 RESTORE_IT (it, &wrap_it, wrap_data);
20913 it->continuation_lines_width += wrap_x;
20914 row->used[TEXT_AREA] = wrap_row_used;
20915 row->ascent = wrap_row_ascent;
20916 row->height = wrap_row_height;
20917 row->phys_ascent = wrap_row_phys_ascent;
20918 row->phys_height = wrap_row_phys_height;
20919 row->extra_line_spacing = wrap_row_extra_line_spacing;
20920 min_pos = wrap_row_min_pos;
20921 min_bpos = wrap_row_min_bpos;
20922 max_pos = wrap_row_max_pos;
20923 max_bpos = wrap_row_max_bpos;
20924 row->continued_p = true;
20925 row->ends_at_zv_p = false;
20926 row->exact_window_width_line_p = false;
20927 it->continuation_lines_width += x;
20929 /* Make sure that a non-default face is extended
20930 up to the right margin of the window. */
20931 extend_face_to_end_of_line (it);
20933 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20935 /* A TAB that extends past the right edge of the
20936 window. This produces a single glyph on
20937 window system frames. We leave the glyph in
20938 this row and let it fill the row, but don't
20939 consume the TAB. */
20940 if ((row->reversed_p
20941 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20942 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20943 produce_special_glyphs (it, IT_CONTINUATION);
20944 it->continuation_lines_width += it->last_visible_x;
20945 row->ends_in_middle_of_char_p = true;
20946 row->continued_p = true;
20947 glyph->pixel_width = it->last_visible_x - x;
20948 it->starts_in_middle_of_char_p = true;
20949 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20950 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20951 extend_face_to_end_of_line (it);
20953 else
20955 /* Something other than a TAB that draws past
20956 the right edge of the window. Restore
20957 positions to values before the element. */
20958 if (row->reversed_p)
20959 unproduce_glyphs (it, row->used[TEXT_AREA]
20960 - (n_glyphs_before + i));
20961 row->used[TEXT_AREA] = n_glyphs_before + i;
20963 /* Display continuation glyphs. */
20964 it->current_x = x_before;
20965 it->continuation_lines_width += x;
20966 if (!FRAME_WINDOW_P (it->f)
20967 || (row->reversed_p
20968 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20969 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20970 produce_special_glyphs (it, IT_CONTINUATION);
20971 row->continued_p = true;
20973 extend_face_to_end_of_line (it);
20975 if (nglyphs > 1 && i > 0)
20977 row->ends_in_middle_of_char_p = true;
20978 it->starts_in_middle_of_char_p = true;
20981 /* Restore the height to what it was before the
20982 element not fitting on the line. */
20983 it->max_ascent = ascent;
20984 it->max_descent = descent;
20985 it->max_phys_ascent = phys_ascent;
20986 it->max_phys_descent = phys_descent;
20989 break;
20991 else if (new_x > it->first_visible_x)
20993 /* Increment number of glyphs actually displayed. */
20994 ++it->hpos;
20996 /* Record the maximum and minimum buffer positions
20997 seen so far in glyphs that will be displayed by
20998 this row. */
20999 if (it->bidi_p)
21000 RECORD_MAX_MIN_POS (it);
21002 if (x < it->first_visible_x && !row->reversed_p)
21003 /* Glyph is partially visible, i.e. row starts at
21004 negative X position. Don't do that in R2L
21005 rows, where we arrange to add a right offset to
21006 the line in extend_face_to_end_of_line, by a
21007 suitable change to the stretch glyph that is
21008 the leftmost glyph of the line. */
21009 row->x = x - it->first_visible_x;
21010 /* When the last glyph of an R2L row only fits
21011 partially on the line, we need to set row->x to a
21012 negative offset, so that the leftmost glyph is
21013 the one that is partially visible. But if we are
21014 going to produce the truncation glyph, this will
21015 be taken care of in produce_special_glyphs. */
21016 if (row->reversed_p
21017 && new_x > it->last_visible_x
21018 && !(it->line_wrap == TRUNCATE
21019 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21021 eassert (FRAME_WINDOW_P (it->f));
21022 row->x = it->last_visible_x - new_x;
21025 else
21027 /* Glyph is completely off the left margin of the
21028 window. This should not happen because of the
21029 move_it_in_display_line at the start of this
21030 function, unless the text display area of the
21031 window is empty. */
21032 eassert (it->first_visible_x <= it->last_visible_x);
21035 /* Even if this display element produced no glyphs at all,
21036 we want to record its position. */
21037 if (it->bidi_p && nglyphs == 0)
21038 RECORD_MAX_MIN_POS (it);
21040 row->ascent = max (row->ascent, it->max_ascent);
21041 row->height = max (row->height, it->max_ascent + it->max_descent);
21042 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21043 row->phys_height = max (row->phys_height,
21044 it->max_phys_ascent + it->max_phys_descent);
21045 row->extra_line_spacing = max (row->extra_line_spacing,
21046 it->max_extra_line_spacing);
21048 /* End of this display line if row is continued. */
21049 if (row->continued_p || row->ends_at_zv_p)
21050 break;
21053 at_end_of_line:
21054 /* Is this a line end? If yes, we're also done, after making
21055 sure that a non-default face is extended up to the right
21056 margin of the window. */
21057 if (ITERATOR_AT_END_OF_LINE_P (it))
21059 int used_before = row->used[TEXT_AREA];
21061 row->ends_in_newline_from_string_p = STRINGP (it->object);
21063 /* Add a space at the end of the line that is used to
21064 display the cursor there. */
21065 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21066 append_space_for_newline (it, false);
21068 /* Extend the face to the end of the line. */
21069 extend_face_to_end_of_line (it);
21071 /* Make sure we have the position. */
21072 if (used_before == 0)
21073 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21075 /* Record the position of the newline, for use in
21076 find_row_edges. */
21077 it->eol_pos = it->current.pos;
21079 /* Consume the line end. This skips over invisible lines. */
21080 set_iterator_to_next (it, true);
21081 it->continuation_lines_width = 0;
21082 break;
21085 /* Proceed with next display element. Note that this skips
21086 over lines invisible because of selective display. */
21087 set_iterator_to_next (it, true);
21089 /* If we truncate lines, we are done when the last displayed
21090 glyphs reach past the right margin of the window. */
21091 if (it->line_wrap == TRUNCATE
21092 && ((FRAME_WINDOW_P (it->f)
21093 /* Images are preprocessed in produce_image_glyph such
21094 that they are cropped at the right edge of the
21095 window, so an image glyph will always end exactly at
21096 last_visible_x, even if there's no right fringe. */
21097 && ((row->reversed_p
21098 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21099 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21100 || it->what == IT_IMAGE))
21101 ? (it->current_x >= it->last_visible_x)
21102 : (it->current_x > it->last_visible_x)))
21104 /* Maybe add truncation glyphs. */
21105 if (!FRAME_WINDOW_P (it->f)
21106 || (row->reversed_p
21107 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21108 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21110 int i, n;
21112 if (!row->reversed_p)
21114 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21115 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21116 break;
21118 else
21120 for (i = 0; i < row->used[TEXT_AREA]; i++)
21121 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21122 break;
21123 /* Remove any padding glyphs at the front of ROW, to
21124 make room for the truncation glyphs we will be
21125 adding below. The loop below always inserts at
21126 least one truncation glyph, so also remove the
21127 last glyph added to ROW. */
21128 unproduce_glyphs (it, i + 1);
21129 /* Adjust i for the loop below. */
21130 i = row->used[TEXT_AREA] - (i + 1);
21133 /* produce_special_glyphs overwrites the last glyph, so
21134 we don't want that if we want to keep that last
21135 glyph, which means it's an image. */
21136 if (it->current_x > it->last_visible_x)
21138 it->current_x = x_before;
21139 if (!FRAME_WINDOW_P (it->f))
21141 for (n = row->used[TEXT_AREA]; i < n; ++i)
21143 row->used[TEXT_AREA] = i;
21144 produce_special_glyphs (it, IT_TRUNCATION);
21147 else
21149 row->used[TEXT_AREA] = i;
21150 produce_special_glyphs (it, IT_TRUNCATION);
21152 it->hpos = hpos_before;
21155 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21157 /* Don't truncate if we can overflow newline into fringe. */
21158 if (!get_next_display_element (it))
21160 it->continuation_lines_width = 0;
21161 row->ends_at_zv_p = true;
21162 row->exact_window_width_line_p = true;
21163 break;
21165 if (ITERATOR_AT_END_OF_LINE_P (it))
21167 row->exact_window_width_line_p = true;
21168 goto at_end_of_line;
21170 it->current_x = x_before;
21171 it->hpos = hpos_before;
21174 row->truncated_on_right_p = true;
21175 it->continuation_lines_width = 0;
21176 reseat_at_next_visible_line_start (it, false);
21177 /* We insist below that IT's position be at ZV because in
21178 bidi-reordered lines the character at visible line start
21179 might not be the character that follows the newline in
21180 the logical order. */
21181 if (IT_BYTEPOS (*it) > BEG_BYTE)
21182 row->ends_at_zv_p =
21183 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21184 else
21185 row->ends_at_zv_p = false;
21186 break;
21190 if (wrap_data)
21191 bidi_unshelve_cache (wrap_data, true);
21193 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21194 at the left window margin. */
21195 if (it->first_visible_x
21196 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21198 if (!FRAME_WINDOW_P (it->f)
21199 || (((row->reversed_p
21200 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21201 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21202 /* Don't let insert_left_trunc_glyphs overwrite the
21203 first glyph of the row if it is an image. */
21204 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21205 insert_left_trunc_glyphs (it);
21206 row->truncated_on_left_p = true;
21209 /* Remember the position at which this line ends.
21211 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21212 cannot be before the call to find_row_edges below, since that is
21213 where these positions are determined. */
21214 row->end = it->current;
21215 if (!it->bidi_p)
21217 row->minpos = row->start.pos;
21218 row->maxpos = row->end.pos;
21220 else
21222 /* ROW->minpos and ROW->maxpos must be the smallest and
21223 `1 + the largest' buffer positions in ROW. But if ROW was
21224 bidi-reordered, these two positions can be anywhere in the
21225 row, so we must determine them now. */
21226 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21229 /* If the start of this line is the overlay arrow-position, then
21230 mark this glyph row as the one containing the overlay arrow.
21231 This is clearly a mess with variable size fonts. It would be
21232 better to let it be displayed like cursors under X. */
21233 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21234 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21235 !NILP (overlay_arrow_string)))
21237 /* Overlay arrow in window redisplay is a fringe bitmap. */
21238 if (STRINGP (overlay_arrow_string))
21240 struct glyph_row *arrow_row
21241 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21242 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21243 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21244 struct glyph *p = row->glyphs[TEXT_AREA];
21245 struct glyph *p2, *end;
21247 /* Copy the arrow glyphs. */
21248 while (glyph < arrow_end)
21249 *p++ = *glyph++;
21251 /* Throw away padding glyphs. */
21252 p2 = p;
21253 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21254 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21255 ++p2;
21256 if (p2 > p)
21258 while (p2 < end)
21259 *p++ = *p2++;
21260 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21263 else
21265 eassert (INTEGERP (overlay_arrow_string));
21266 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21268 overlay_arrow_seen = true;
21271 /* Highlight trailing whitespace. */
21272 if (!NILP (Vshow_trailing_whitespace))
21273 highlight_trailing_whitespace (it->f, it->glyph_row);
21275 /* Compute pixel dimensions of this line. */
21276 compute_line_metrics (it);
21278 /* Implementation note: No changes in the glyphs of ROW or in their
21279 faces can be done past this point, because compute_line_metrics
21280 computes ROW's hash value and stores it within the glyph_row
21281 structure. */
21283 /* Record whether this row ends inside an ellipsis. */
21284 row->ends_in_ellipsis_p
21285 = (it->method == GET_FROM_DISPLAY_VECTOR
21286 && it->ellipsis_p);
21288 /* Save fringe bitmaps in this row. */
21289 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21290 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21291 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21292 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21294 it->left_user_fringe_bitmap = 0;
21295 it->left_user_fringe_face_id = 0;
21296 it->right_user_fringe_bitmap = 0;
21297 it->right_user_fringe_face_id = 0;
21299 /* Maybe set the cursor. */
21300 cvpos = it->w->cursor.vpos;
21301 if ((cvpos < 0
21302 /* In bidi-reordered rows, keep checking for proper cursor
21303 position even if one has been found already, because buffer
21304 positions in such rows change non-linearly with ROW->VPOS,
21305 when a line is continued. One exception: when we are at ZV,
21306 display cursor on the first suitable glyph row, since all
21307 the empty rows after that also have their position set to ZV. */
21308 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21309 lines' rows is implemented for bidi-reordered rows. */
21310 || (it->bidi_p
21311 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21312 && PT >= MATRIX_ROW_START_CHARPOS (row)
21313 && PT <= MATRIX_ROW_END_CHARPOS (row)
21314 && cursor_row_p (row))
21315 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21317 /* Prepare for the next line. This line starts horizontally at (X
21318 HPOS) = (0 0). Vertical positions are incremented. As a
21319 convenience for the caller, IT->glyph_row is set to the next
21320 row to be used. */
21321 it->current_x = it->hpos = 0;
21322 it->current_y += row->height;
21323 SET_TEXT_POS (it->eol_pos, 0, 0);
21324 ++it->vpos;
21325 ++it->glyph_row;
21326 /* The next row should by default use the same value of the
21327 reversed_p flag as this one. set_iterator_to_next decides when
21328 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21329 the flag accordingly. */
21330 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21331 it->glyph_row->reversed_p = row->reversed_p;
21332 it->start = row->end;
21333 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21335 #undef RECORD_MAX_MIN_POS
21338 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21339 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21340 doc: /* Return paragraph direction at point in BUFFER.
21341 Value is either `left-to-right' or `right-to-left'.
21342 If BUFFER is omitted or nil, it defaults to the current buffer.
21344 Paragraph direction determines how the text in the paragraph is displayed.
21345 In left-to-right paragraphs, text begins at the left margin of the window
21346 and the reading direction is generally left to right. In right-to-left
21347 paragraphs, text begins at the right margin and is read from right to left.
21349 See also `bidi-paragraph-direction'. */)
21350 (Lisp_Object buffer)
21352 struct buffer *buf = current_buffer;
21353 struct buffer *old = buf;
21355 if (! NILP (buffer))
21357 CHECK_BUFFER (buffer);
21358 buf = XBUFFER (buffer);
21361 if (NILP (BVAR (buf, bidi_display_reordering))
21362 || NILP (BVAR (buf, enable_multibyte_characters))
21363 /* When we are loading loadup.el, the character property tables
21364 needed for bidi iteration are not yet available. */
21365 || redisplay__inhibit_bidi)
21366 return Qleft_to_right;
21367 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21368 return BVAR (buf, bidi_paragraph_direction);
21369 else
21371 /* Determine the direction from buffer text. We could try to
21372 use current_matrix if it is up to date, but this seems fast
21373 enough as it is. */
21374 struct bidi_it itb;
21375 ptrdiff_t pos = BUF_PT (buf);
21376 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21377 int c;
21378 void *itb_data = bidi_shelve_cache ();
21380 set_buffer_temp (buf);
21381 /* bidi_paragraph_init finds the base direction of the paragraph
21382 by searching forward from paragraph start. We need the base
21383 direction of the current or _previous_ paragraph, so we need
21384 to make sure we are within that paragraph. To that end, find
21385 the previous non-empty line. */
21386 if (pos >= ZV && pos > BEGV)
21387 DEC_BOTH (pos, bytepos);
21388 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21389 if (fast_looking_at (trailing_white_space,
21390 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21392 while ((c = FETCH_BYTE (bytepos)) == '\n'
21393 || c == ' ' || c == '\t' || c == '\f')
21395 if (bytepos <= BEGV_BYTE)
21396 break;
21397 bytepos--;
21398 pos--;
21400 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21401 bytepos--;
21403 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21404 itb.paragraph_dir = NEUTRAL_DIR;
21405 itb.string.s = NULL;
21406 itb.string.lstring = Qnil;
21407 itb.string.bufpos = 0;
21408 itb.string.from_disp_str = false;
21409 itb.string.unibyte = false;
21410 /* We have no window to use here for ignoring window-specific
21411 overlays. Using NULL for window pointer will cause
21412 compute_display_string_pos to use the current buffer. */
21413 itb.w = NULL;
21414 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21415 bidi_unshelve_cache (itb_data, false);
21416 set_buffer_temp (old);
21417 switch (itb.paragraph_dir)
21419 case L2R:
21420 return Qleft_to_right;
21421 break;
21422 case R2L:
21423 return Qright_to_left;
21424 break;
21425 default:
21426 emacs_abort ();
21431 DEFUN ("bidi-find-overridden-directionality",
21432 Fbidi_find_overridden_directionality,
21433 Sbidi_find_overridden_directionality, 2, 3, 0,
21434 doc: /* Return position between FROM and TO where directionality was overridden.
21436 This function returns the first character position in the specified
21437 region of OBJECT where there is a character whose `bidi-class' property
21438 is `L', but which was forced to display as `R' by a directional
21439 override, and likewise with characters whose `bidi-class' is `R'
21440 or `AL' that were forced to display as `L'.
21442 If no such character is found, the function returns nil.
21444 OBJECT is a Lisp string or buffer to search for overridden
21445 directionality, and defaults to the current buffer if nil or omitted.
21446 OBJECT can also be a window, in which case the function will search
21447 the buffer displayed in that window. Passing the window instead of
21448 a buffer is preferable when the buffer is displayed in some window,
21449 because this function will then be able to correctly account for
21450 window-specific overlays, which can affect the results.
21452 Strong directional characters `L', `R', and `AL' can have their
21453 intrinsic directionality overridden by directional override
21454 control characters RLO (u+202e) and LRO (u+202d). See the
21455 function `get-char-code-property' for a way to inquire about
21456 the `bidi-class' property of a character. */)
21457 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21459 struct buffer *buf = current_buffer;
21460 struct buffer *old = buf;
21461 struct window *w = NULL;
21462 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21463 struct bidi_it itb;
21464 ptrdiff_t from_pos, to_pos, from_bpos;
21465 void *itb_data;
21467 if (!NILP (object))
21469 if (BUFFERP (object))
21470 buf = XBUFFER (object);
21471 else if (WINDOWP (object))
21473 w = decode_live_window (object);
21474 buf = XBUFFER (w->contents);
21475 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21477 else
21478 CHECK_STRING (object);
21481 if (STRINGP (object))
21483 /* Characters in unibyte strings are always treated by bidi.c as
21484 strong LTR. */
21485 if (!STRING_MULTIBYTE (object)
21486 /* When we are loading loadup.el, the character property
21487 tables needed for bidi iteration are not yet
21488 available. */
21489 || redisplay__inhibit_bidi)
21490 return Qnil;
21492 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21493 if (from_pos >= SCHARS (object))
21494 return Qnil;
21496 /* Set up the bidi iterator. */
21497 itb_data = bidi_shelve_cache ();
21498 itb.paragraph_dir = NEUTRAL_DIR;
21499 itb.string.lstring = object;
21500 itb.string.s = NULL;
21501 itb.string.schars = SCHARS (object);
21502 itb.string.bufpos = 0;
21503 itb.string.from_disp_str = false;
21504 itb.string.unibyte = false;
21505 itb.w = w;
21506 bidi_init_it (0, 0, frame_window_p, &itb);
21508 else
21510 /* Nothing this fancy can happen in unibyte buffers, or in a
21511 buffer that disabled reordering, or if FROM is at EOB. */
21512 if (NILP (BVAR (buf, bidi_display_reordering))
21513 || NILP (BVAR (buf, enable_multibyte_characters))
21514 /* When we are loading loadup.el, the character property
21515 tables needed for bidi iteration are not yet
21516 available. */
21517 || redisplay__inhibit_bidi)
21518 return Qnil;
21520 set_buffer_temp (buf);
21521 validate_region (&from, &to);
21522 from_pos = XINT (from);
21523 to_pos = XINT (to);
21524 if (from_pos >= ZV)
21525 return Qnil;
21527 /* Set up the bidi iterator. */
21528 itb_data = bidi_shelve_cache ();
21529 from_bpos = CHAR_TO_BYTE (from_pos);
21530 if (from_pos == BEGV)
21532 itb.charpos = BEGV;
21533 itb.bytepos = BEGV_BYTE;
21535 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21537 itb.charpos = from_pos;
21538 itb.bytepos = from_bpos;
21540 else
21541 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21542 -1, &itb.bytepos);
21543 itb.paragraph_dir = NEUTRAL_DIR;
21544 itb.string.s = NULL;
21545 itb.string.lstring = Qnil;
21546 itb.string.bufpos = 0;
21547 itb.string.from_disp_str = false;
21548 itb.string.unibyte = false;
21549 itb.w = w;
21550 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21553 ptrdiff_t found;
21554 do {
21555 /* For the purposes of this function, the actual base direction of
21556 the paragraph doesn't matter, so just set it to L2R. */
21557 bidi_paragraph_init (L2R, &itb, false);
21558 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21560 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21562 bidi_unshelve_cache (itb_data, false);
21563 set_buffer_temp (old);
21565 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21568 DEFUN ("move-point-visually", Fmove_point_visually,
21569 Smove_point_visually, 1, 1, 0,
21570 doc: /* Move point in the visual order in the specified DIRECTION.
21571 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21572 left.
21574 Value is the new character position of point. */)
21575 (Lisp_Object direction)
21577 struct window *w = XWINDOW (selected_window);
21578 struct buffer *b = XBUFFER (w->contents);
21579 struct glyph_row *row;
21580 int dir;
21581 Lisp_Object paragraph_dir;
21583 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21584 (!(ROW)->continued_p \
21585 && NILP ((GLYPH)->object) \
21586 && (GLYPH)->type == CHAR_GLYPH \
21587 && (GLYPH)->u.ch == ' ' \
21588 && (GLYPH)->charpos >= 0 \
21589 && !(GLYPH)->avoid_cursor_p)
21591 CHECK_NUMBER (direction);
21592 dir = XINT (direction);
21593 if (dir > 0)
21594 dir = 1;
21595 else
21596 dir = -1;
21598 /* If current matrix is up-to-date, we can use the information
21599 recorded in the glyphs, at least as long as the goal is on the
21600 screen. */
21601 if (w->window_end_valid
21602 && !windows_or_buffers_changed
21603 && b
21604 && !b->clip_changed
21605 && !b->prevent_redisplay_optimizations_p
21606 && !window_outdated (w)
21607 /* We rely below on the cursor coordinates to be up to date, but
21608 we cannot trust them if some command moved point since the
21609 last complete redisplay. */
21610 && w->last_point == BUF_PT (b)
21611 && w->cursor.vpos >= 0
21612 && w->cursor.vpos < w->current_matrix->nrows
21613 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21615 struct glyph *g = row->glyphs[TEXT_AREA];
21616 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21617 struct glyph *gpt = g + w->cursor.hpos;
21619 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21621 if (BUFFERP (g->object) && g->charpos != PT)
21623 SET_PT (g->charpos);
21624 w->cursor.vpos = -1;
21625 return make_number (PT);
21627 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21629 ptrdiff_t new_pos;
21631 if (BUFFERP (gpt->object))
21633 new_pos = PT;
21634 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21635 new_pos += (row->reversed_p ? -dir : dir);
21636 else
21637 new_pos -= (row->reversed_p ? -dir : dir);
21639 else if (BUFFERP (g->object))
21640 new_pos = g->charpos;
21641 else
21642 break;
21643 SET_PT (new_pos);
21644 w->cursor.vpos = -1;
21645 return make_number (PT);
21647 else if (ROW_GLYPH_NEWLINE_P (row, g))
21649 /* Glyphs inserted at the end of a non-empty line for
21650 positioning the cursor have zero charpos, so we must
21651 deduce the value of point by other means. */
21652 if (g->charpos > 0)
21653 SET_PT (g->charpos);
21654 else if (row->ends_at_zv_p && PT != ZV)
21655 SET_PT (ZV);
21656 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21657 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21658 else
21659 break;
21660 w->cursor.vpos = -1;
21661 return make_number (PT);
21664 if (g == e || NILP (g->object))
21666 if (row->truncated_on_left_p || row->truncated_on_right_p)
21667 goto simulate_display;
21668 if (!row->reversed_p)
21669 row += dir;
21670 else
21671 row -= dir;
21672 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21673 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21674 goto simulate_display;
21676 if (dir > 0)
21678 if (row->reversed_p && !row->continued_p)
21680 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21681 w->cursor.vpos = -1;
21682 return make_number (PT);
21684 g = row->glyphs[TEXT_AREA];
21685 e = g + row->used[TEXT_AREA];
21686 for ( ; g < e; g++)
21688 if (BUFFERP (g->object)
21689 /* Empty lines have only one glyph, which stands
21690 for the newline, and whose charpos is the
21691 buffer position of the newline. */
21692 || ROW_GLYPH_NEWLINE_P (row, g)
21693 /* When the buffer ends in a newline, the line at
21694 EOB also has one glyph, but its charpos is -1. */
21695 || (row->ends_at_zv_p
21696 && !row->reversed_p
21697 && NILP (g->object)
21698 && g->type == CHAR_GLYPH
21699 && g->u.ch == ' '))
21701 if (g->charpos > 0)
21702 SET_PT (g->charpos);
21703 else if (!row->reversed_p
21704 && row->ends_at_zv_p
21705 && PT != ZV)
21706 SET_PT (ZV);
21707 else
21708 continue;
21709 w->cursor.vpos = -1;
21710 return make_number (PT);
21714 else
21716 if (!row->reversed_p && !row->continued_p)
21718 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21719 w->cursor.vpos = -1;
21720 return make_number (PT);
21722 e = row->glyphs[TEXT_AREA];
21723 g = e + row->used[TEXT_AREA] - 1;
21724 for ( ; g >= e; g--)
21726 if (BUFFERP (g->object)
21727 || (ROW_GLYPH_NEWLINE_P (row, g)
21728 && g->charpos > 0)
21729 /* Empty R2L lines on GUI frames have the buffer
21730 position of the newline stored in the stretch
21731 glyph. */
21732 || g->type == STRETCH_GLYPH
21733 || (row->ends_at_zv_p
21734 && row->reversed_p
21735 && NILP (g->object)
21736 && g->type == CHAR_GLYPH
21737 && g->u.ch == ' '))
21739 if (g->charpos > 0)
21740 SET_PT (g->charpos);
21741 else if (row->reversed_p
21742 && row->ends_at_zv_p
21743 && PT != ZV)
21744 SET_PT (ZV);
21745 else
21746 continue;
21747 w->cursor.vpos = -1;
21748 return make_number (PT);
21755 simulate_display:
21757 /* If we wind up here, we failed to move by using the glyphs, so we
21758 need to simulate display instead. */
21760 if (b)
21761 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21762 else
21763 paragraph_dir = Qleft_to_right;
21764 if (EQ (paragraph_dir, Qright_to_left))
21765 dir = -dir;
21766 if (PT <= BEGV && dir < 0)
21767 xsignal0 (Qbeginning_of_buffer);
21768 else if (PT >= ZV && dir > 0)
21769 xsignal0 (Qend_of_buffer);
21770 else
21772 struct text_pos pt;
21773 struct it it;
21774 int pt_x, target_x, pixel_width, pt_vpos;
21775 bool at_eol_p;
21776 bool overshoot_expected = false;
21777 bool target_is_eol_p = false;
21779 /* Setup the arena. */
21780 SET_TEXT_POS (pt, PT, PT_BYTE);
21781 start_display (&it, w, pt);
21782 /* When lines are truncated, we could be called with point
21783 outside of the windows edges, in which case move_it_*
21784 functions either prematurely stop at window's edge or jump to
21785 the next screen line, whereas we rely below on our ability to
21786 reach point, in order to start from its X coordinate. So we
21787 need to disregard the window's horizontal extent in that case. */
21788 if (it.line_wrap == TRUNCATE)
21789 it.last_visible_x = INFINITY;
21791 if (it.cmp_it.id < 0
21792 && it.method == GET_FROM_STRING
21793 && it.area == TEXT_AREA
21794 && it.string_from_display_prop_p
21795 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21796 overshoot_expected = true;
21798 /* Find the X coordinate of point. We start from the beginning
21799 of this or previous line to make sure we are before point in
21800 the logical order (since the move_it_* functions can only
21801 move forward). */
21802 reseat:
21803 reseat_at_previous_visible_line_start (&it);
21804 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21805 if (IT_CHARPOS (it) != PT)
21807 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21808 -1, -1, -1, MOVE_TO_POS);
21809 /* If we missed point because the character there is
21810 displayed out of a display vector that has more than one
21811 glyph, retry expecting overshoot. */
21812 if (it.method == GET_FROM_DISPLAY_VECTOR
21813 && it.current.dpvec_index > 0
21814 && !overshoot_expected)
21816 overshoot_expected = true;
21817 goto reseat;
21819 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21820 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21822 pt_x = it.current_x;
21823 pt_vpos = it.vpos;
21824 if (dir > 0 || overshoot_expected)
21826 struct glyph_row *row = it.glyph_row;
21828 /* When point is at beginning of line, we don't have
21829 information about the glyph there loaded into struct
21830 it. Calling get_next_display_element fixes that. */
21831 if (pt_x == 0)
21832 get_next_display_element (&it);
21833 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21834 it.glyph_row = NULL;
21835 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21836 it.glyph_row = row;
21837 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21838 it, lest it will become out of sync with it's buffer
21839 position. */
21840 it.current_x = pt_x;
21842 else
21843 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21844 pixel_width = it.pixel_width;
21845 if (overshoot_expected && at_eol_p)
21846 pixel_width = 0;
21847 else if (pixel_width <= 0)
21848 pixel_width = 1;
21850 /* If there's a display string (or something similar) at point,
21851 we are actually at the glyph to the left of point, so we need
21852 to correct the X coordinate. */
21853 if (overshoot_expected)
21855 if (it.bidi_p)
21856 pt_x += pixel_width * it.bidi_it.scan_dir;
21857 else
21858 pt_x += pixel_width;
21861 /* Compute target X coordinate, either to the left or to the
21862 right of point. On TTY frames, all characters have the same
21863 pixel width of 1, so we can use that. On GUI frames we don't
21864 have an easy way of getting at the pixel width of the
21865 character to the left of point, so we use a different method
21866 of getting to that place. */
21867 if (dir > 0)
21868 target_x = pt_x + pixel_width;
21869 else
21870 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21872 /* Target X coordinate could be one line above or below the line
21873 of point, in which case we need to adjust the target X
21874 coordinate. Also, if moving to the left, we need to begin at
21875 the left edge of the point's screen line. */
21876 if (dir < 0)
21878 if (pt_x > 0)
21880 start_display (&it, w, pt);
21881 if (it.line_wrap == TRUNCATE)
21882 it.last_visible_x = INFINITY;
21883 reseat_at_previous_visible_line_start (&it);
21884 it.current_x = it.current_y = it.hpos = 0;
21885 if (pt_vpos != 0)
21886 move_it_by_lines (&it, pt_vpos);
21888 else
21890 move_it_by_lines (&it, -1);
21891 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21892 target_is_eol_p = true;
21893 /* Under word-wrap, we don't know the x coordinate of
21894 the last character displayed on the previous line,
21895 which immediately precedes the wrap point. To find
21896 out its x coordinate, we try moving to the right
21897 margin of the window, which will stop at the wrap
21898 point, and then reset target_x to point at the
21899 character that precedes the wrap point. This is not
21900 needed on GUI frames, because (see below) there we
21901 move from the left margin one grapheme cluster at a
21902 time, and stop when we hit the wrap point. */
21903 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21905 void *it_data = NULL;
21906 struct it it2;
21908 SAVE_IT (it2, it, it_data);
21909 move_it_in_display_line_to (&it, ZV, target_x,
21910 MOVE_TO_POS | MOVE_TO_X);
21911 /* If we arrived at target_x, that _is_ the last
21912 character on the previous line. */
21913 if (it.current_x != target_x)
21914 target_x = it.current_x - 1;
21915 RESTORE_IT (&it, &it2, it_data);
21919 else
21921 if (at_eol_p
21922 || (target_x >= it.last_visible_x
21923 && it.line_wrap != TRUNCATE))
21925 if (pt_x > 0)
21926 move_it_by_lines (&it, 0);
21927 move_it_by_lines (&it, 1);
21928 target_x = 0;
21932 /* Move to the target X coordinate. */
21933 /* On GUI frames, as we don't know the X coordinate of the
21934 character to the left of point, moving point to the left
21935 requires walking, one grapheme cluster at a time, until we
21936 find ourself at a place immediately to the left of the
21937 character at point. */
21938 if (FRAME_WINDOW_P (it.f) && dir < 0)
21940 struct text_pos new_pos;
21941 enum move_it_result rc = MOVE_X_REACHED;
21943 if (it.current_x == 0)
21944 get_next_display_element (&it);
21945 if (it.what == IT_COMPOSITION)
21947 new_pos.charpos = it.cmp_it.charpos;
21948 new_pos.bytepos = -1;
21950 else
21951 new_pos = it.current.pos;
21953 while (it.current_x + it.pixel_width <= target_x
21954 && (rc == MOVE_X_REACHED
21955 /* Under word-wrap, move_it_in_display_line_to
21956 stops at correct coordinates, but sometimes
21957 returns MOVE_POS_MATCH_OR_ZV. */
21958 || (it.line_wrap == WORD_WRAP
21959 && rc == MOVE_POS_MATCH_OR_ZV)))
21961 int new_x = it.current_x + it.pixel_width;
21963 /* For composed characters, we want the position of the
21964 first character in the grapheme cluster (usually, the
21965 composition's base character), whereas it.current
21966 might give us the position of the _last_ one, e.g. if
21967 the composition is rendered in reverse due to bidi
21968 reordering. */
21969 if (it.what == IT_COMPOSITION)
21971 new_pos.charpos = it.cmp_it.charpos;
21972 new_pos.bytepos = -1;
21974 else
21975 new_pos = it.current.pos;
21976 if (new_x == it.current_x)
21977 new_x++;
21978 rc = move_it_in_display_line_to (&it, ZV, new_x,
21979 MOVE_TO_POS | MOVE_TO_X);
21980 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21981 break;
21983 /* The previous position we saw in the loop is the one we
21984 want. */
21985 if (new_pos.bytepos == -1)
21986 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21987 it.current.pos = new_pos;
21989 else if (it.current_x != target_x)
21990 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21992 /* If we ended up in a display string that covers point, move to
21993 buffer position to the right in the visual order. */
21994 if (dir > 0)
21996 while (IT_CHARPOS (it) == PT)
21998 set_iterator_to_next (&it, false);
21999 if (!get_next_display_element (&it))
22000 break;
22004 /* Move point to that position. */
22005 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22008 return make_number (PT);
22010 #undef ROW_GLYPH_NEWLINE_P
22013 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22014 Sbidi_resolved_levels, 0, 1, 0,
22015 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22017 The resolved levels are produced by the Emacs bidi reordering engine
22018 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22019 read the Unicode Standard Annex 9 (UAX#9) for background information
22020 about these levels.
22022 VPOS is the zero-based number of the current window's screen line
22023 for which to produce the resolved levels. If VPOS is nil or omitted,
22024 it defaults to the screen line of point. If the window displays a
22025 header line, VPOS of zero will report on the header line, and first
22026 line of text in the window will have VPOS of 1.
22028 Value is an array of resolved levels, indexed by glyph number.
22029 Glyphs are numbered from zero starting from the beginning of the
22030 screen line, i.e. the left edge of the window for left-to-right lines
22031 and from the right edge for right-to-left lines. The resolved levels
22032 are produced only for the window's text area; text in display margins
22033 is not included.
22035 If the selected window's display is not up-to-date, or if the specified
22036 screen line does not display text, this function returns nil. It is
22037 highly recommended to bind this function to some simple key, like F8,
22038 in order to avoid these problems.
22040 This function exists mainly for testing the correctness of the
22041 Emacs UBA implementation, in particular with the test suite. */)
22042 (Lisp_Object vpos)
22044 struct window *w = XWINDOW (selected_window);
22045 struct buffer *b = XBUFFER (w->contents);
22046 int nrow;
22047 struct glyph_row *row;
22049 if (NILP (vpos))
22051 int d1, d2, d3, d4, d5;
22053 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22055 else
22057 CHECK_NUMBER_COERCE_MARKER (vpos);
22058 nrow = XINT (vpos);
22061 /* We require up-to-date glyph matrix for this window. */
22062 if (w->window_end_valid
22063 && !windows_or_buffers_changed
22064 && b
22065 && !b->clip_changed
22066 && !b->prevent_redisplay_optimizations_p
22067 && !window_outdated (w)
22068 && nrow >= 0
22069 && nrow < w->current_matrix->nrows
22070 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22071 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22073 struct glyph *g, *e, *g1;
22074 int nglyphs, i;
22075 Lisp_Object levels;
22077 if (!row->reversed_p) /* Left-to-right glyph row. */
22079 g = g1 = row->glyphs[TEXT_AREA];
22080 e = g + row->used[TEXT_AREA];
22082 /* Skip over glyphs at the start of the row that was
22083 generated by redisplay for its own needs. */
22084 while (g < e
22085 && NILP (g->object)
22086 && g->charpos < 0)
22087 g++;
22088 g1 = g;
22090 /* Count the "interesting" glyphs in this row. */
22091 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22092 nglyphs++;
22094 /* Create and fill the array. */
22095 levels = make_uninit_vector (nglyphs);
22096 for (i = 0; g1 < g; i++, g1++)
22097 ASET (levels, i, make_number (g1->resolved_level));
22099 else /* Right-to-left glyph row. */
22101 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22102 e = row->glyphs[TEXT_AREA] - 1;
22103 while (g > e
22104 && NILP (g->object)
22105 && g->charpos < 0)
22106 g--;
22107 g1 = g;
22108 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22109 nglyphs++;
22110 levels = make_uninit_vector (nglyphs);
22111 for (i = 0; g1 > g; i++, g1--)
22112 ASET (levels, i, make_number (g1->resolved_level));
22114 return levels;
22116 else
22117 return Qnil;
22122 /***********************************************************************
22123 Menu Bar
22124 ***********************************************************************/
22126 /* Redisplay the menu bar in the frame for window W.
22128 The menu bar of X frames that don't have X toolkit support is
22129 displayed in a special window W->frame->menu_bar_window.
22131 The menu bar of terminal frames is treated specially as far as
22132 glyph matrices are concerned. Menu bar lines are not part of
22133 windows, so the update is done directly on the frame matrix rows
22134 for the menu bar. */
22136 static void
22137 display_menu_bar (struct window *w)
22139 struct frame *f = XFRAME (WINDOW_FRAME (w));
22140 struct it it;
22141 Lisp_Object items;
22142 int i;
22144 /* Don't do all this for graphical frames. */
22145 #ifdef HAVE_NTGUI
22146 if (FRAME_W32_P (f))
22147 return;
22148 #endif
22149 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22150 if (FRAME_X_P (f))
22151 return;
22152 #endif
22154 #ifdef HAVE_NS
22155 if (FRAME_NS_P (f))
22156 return;
22157 #endif /* HAVE_NS */
22159 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22160 eassert (!FRAME_WINDOW_P (f));
22161 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22162 it.first_visible_x = 0;
22163 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22164 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22165 if (FRAME_WINDOW_P (f))
22167 /* Menu bar lines are displayed in the desired matrix of the
22168 dummy window menu_bar_window. */
22169 struct window *menu_w;
22170 menu_w = XWINDOW (f->menu_bar_window);
22171 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22172 MENU_FACE_ID);
22173 it.first_visible_x = 0;
22174 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22176 else
22177 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22179 /* This is a TTY frame, i.e. character hpos/vpos are used as
22180 pixel x/y. */
22181 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22182 MENU_FACE_ID);
22183 it.first_visible_x = 0;
22184 it.last_visible_x = FRAME_COLS (f);
22187 /* FIXME: This should be controlled by a user option. See the
22188 comments in redisplay_tool_bar and display_mode_line about
22189 this. */
22190 it.paragraph_embedding = L2R;
22192 /* Clear all rows of the menu bar. */
22193 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22195 struct glyph_row *row = it.glyph_row + i;
22196 clear_glyph_row (row);
22197 row->enabled_p = true;
22198 row->full_width_p = true;
22199 row->reversed_p = false;
22202 /* Display all items of the menu bar. */
22203 items = FRAME_MENU_BAR_ITEMS (it.f);
22204 for (i = 0; i < ASIZE (items); i += 4)
22206 Lisp_Object string;
22208 /* Stop at nil string. */
22209 string = AREF (items, i + 1);
22210 if (NILP (string))
22211 break;
22213 /* Remember where item was displayed. */
22214 ASET (items, i + 3, make_number (it.hpos));
22216 /* Display the item, pad with one space. */
22217 if (it.current_x < it.last_visible_x)
22218 display_string (NULL, string, Qnil, 0, 0, &it,
22219 SCHARS (string) + 1, 0, 0, -1);
22222 /* Fill out the line with spaces. */
22223 if (it.current_x < it.last_visible_x)
22224 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22226 /* Compute the total height of the lines. */
22227 compute_line_metrics (&it);
22230 /* Deep copy of a glyph row, including the glyphs. */
22231 static void
22232 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22234 struct glyph *pointers[1 + LAST_AREA];
22235 int to_used = to->used[TEXT_AREA];
22237 /* Save glyph pointers of TO. */
22238 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22240 /* Do a structure assignment. */
22241 *to = *from;
22243 /* Restore original glyph pointers of TO. */
22244 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22246 /* Copy the glyphs. */
22247 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22248 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22250 /* If we filled only part of the TO row, fill the rest with
22251 space_glyph (which will display as empty space). */
22252 if (to_used > from->used[TEXT_AREA])
22253 fill_up_frame_row_with_spaces (to, to_used);
22256 /* Display one menu item on a TTY, by overwriting the glyphs in the
22257 frame F's desired glyph matrix with glyphs produced from the menu
22258 item text. Called from term.c to display TTY drop-down menus one
22259 item at a time.
22261 ITEM_TEXT is the menu item text as a C string.
22263 FACE_ID is the face ID to be used for this menu item. FACE_ID
22264 could specify one of 3 faces: a face for an enabled item, a face
22265 for a disabled item, or a face for a selected item.
22267 X and Y are coordinates of the first glyph in the frame's desired
22268 matrix to be overwritten by the menu item. Since this is a TTY, Y
22269 is the zero-based number of the glyph row and X is the zero-based
22270 glyph number in the row, starting from left, where to start
22271 displaying the item.
22273 SUBMENU means this menu item drops down a submenu, which
22274 should be indicated by displaying a proper visual cue after the
22275 item text. */
22277 void
22278 display_tty_menu_item (const char *item_text, int width, int face_id,
22279 int x, int y, bool submenu)
22281 struct it it;
22282 struct frame *f = SELECTED_FRAME ();
22283 struct window *w = XWINDOW (f->selected_window);
22284 struct glyph_row *row;
22285 size_t item_len = strlen (item_text);
22287 eassert (FRAME_TERMCAP_P (f));
22289 /* Don't write beyond the matrix's last row. This can happen for
22290 TTY screens that are not high enough to show the entire menu.
22291 (This is actually a bit of defensive programming, as
22292 tty_menu_display already limits the number of menu items to one
22293 less than the number of screen lines.) */
22294 if (y >= f->desired_matrix->nrows)
22295 return;
22297 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22298 it.first_visible_x = 0;
22299 it.last_visible_x = FRAME_COLS (f) - 1;
22300 row = it.glyph_row;
22301 /* Start with the row contents from the current matrix. */
22302 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22303 bool saved_width = row->full_width_p;
22304 row->full_width_p = true;
22305 bool saved_reversed = row->reversed_p;
22306 row->reversed_p = false;
22307 row->enabled_p = true;
22309 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22310 desired face. */
22311 eassert (x < f->desired_matrix->matrix_w);
22312 it.current_x = it.hpos = x;
22313 it.current_y = it.vpos = y;
22314 int saved_used = row->used[TEXT_AREA];
22315 bool saved_truncated = row->truncated_on_right_p;
22316 row->used[TEXT_AREA] = x;
22317 it.face_id = face_id;
22318 it.line_wrap = TRUNCATE;
22320 /* FIXME: This should be controlled by a user option. See the
22321 comments in redisplay_tool_bar and display_mode_line about this.
22322 Also, if paragraph_embedding could ever be R2L, changes will be
22323 needed to avoid shifting to the right the row characters in
22324 term.c:append_glyph. */
22325 it.paragraph_embedding = L2R;
22327 /* Pad with a space on the left. */
22328 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22329 width--;
22330 /* Display the menu item, pad with spaces to WIDTH. */
22331 if (submenu)
22333 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22334 item_len, 0, FRAME_COLS (f) - 1, -1);
22335 width -= item_len;
22336 /* Indicate with " >" that there's a submenu. */
22337 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22338 FRAME_COLS (f) - 1, -1);
22340 else
22341 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22342 width, 0, FRAME_COLS (f) - 1, -1);
22344 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22345 row->truncated_on_right_p = saved_truncated;
22346 row->hash = row_hash (row);
22347 row->full_width_p = saved_width;
22348 row->reversed_p = saved_reversed;
22351 /***********************************************************************
22352 Mode Line
22353 ***********************************************************************/
22355 /* Redisplay mode lines in the window tree whose root is WINDOW.
22356 If FORCE, redisplay mode lines unconditionally.
22357 Otherwise, redisplay only mode lines that are garbaged. Value is
22358 the number of windows whose mode lines were redisplayed. */
22360 static int
22361 redisplay_mode_lines (Lisp_Object window, bool force)
22363 int nwindows = 0;
22365 while (!NILP (window))
22367 struct window *w = XWINDOW (window);
22369 if (WINDOWP (w->contents))
22370 nwindows += redisplay_mode_lines (w->contents, force);
22371 else if (force
22372 || FRAME_GARBAGED_P (XFRAME (w->frame))
22373 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22375 struct text_pos lpoint;
22376 struct buffer *old = current_buffer;
22378 /* Set the window's buffer for the mode line display. */
22379 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22380 set_buffer_internal_1 (XBUFFER (w->contents));
22382 /* Point refers normally to the selected window. For any
22383 other window, set up appropriate value. */
22384 if (!EQ (window, selected_window))
22386 struct text_pos pt;
22388 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22389 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22392 /* Display mode lines. */
22393 clear_glyph_matrix (w->desired_matrix);
22394 if (display_mode_lines (w))
22395 ++nwindows;
22397 /* Restore old settings. */
22398 set_buffer_internal_1 (old);
22399 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22402 window = w->next;
22405 return nwindows;
22409 /* Display the mode and/or header line of window W. Value is the
22410 sum number of mode lines and header lines displayed. */
22412 static int
22413 display_mode_lines (struct window *w)
22415 Lisp_Object old_selected_window = selected_window;
22416 Lisp_Object old_selected_frame = selected_frame;
22417 Lisp_Object new_frame = w->frame;
22418 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22419 int n = 0;
22421 selected_frame = new_frame;
22422 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22423 or window's point, then we'd need select_window_1 here as well. */
22424 XSETWINDOW (selected_window, w);
22425 XFRAME (new_frame)->selected_window = selected_window;
22427 /* These will be set while the mode line specs are processed. */
22428 line_number_displayed = false;
22429 w->column_number_displayed = -1;
22431 if (WINDOW_WANTS_MODELINE_P (w))
22433 struct window *sel_w = XWINDOW (old_selected_window);
22435 /* Select mode line face based on the real selected window. */
22436 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22437 BVAR (current_buffer, mode_line_format));
22438 ++n;
22441 if (WINDOW_WANTS_HEADER_LINE_P (w))
22443 display_mode_line (w, HEADER_LINE_FACE_ID,
22444 BVAR (current_buffer, header_line_format));
22445 ++n;
22448 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22449 selected_frame = old_selected_frame;
22450 selected_window = old_selected_window;
22451 if (n > 0)
22452 w->must_be_updated_p = true;
22453 return n;
22457 /* Display mode or header line of window W. FACE_ID specifies which
22458 line to display; it is either MODE_LINE_FACE_ID or
22459 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22460 display. Value is the pixel height of the mode/header line
22461 displayed. */
22463 static int
22464 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22466 struct it it;
22467 struct face *face;
22468 ptrdiff_t count = SPECPDL_INDEX ();
22470 init_iterator (&it, w, -1, -1, NULL, face_id);
22471 /* Don't extend on a previously drawn mode-line.
22472 This may happen if called from pos_visible_p. */
22473 it.glyph_row->enabled_p = false;
22474 prepare_desired_row (w, it.glyph_row, true);
22476 it.glyph_row->mode_line_p = true;
22478 /* FIXME: This should be controlled by a user option. But
22479 supporting such an option is not trivial, since the mode line is
22480 made up of many separate strings. */
22481 it.paragraph_embedding = L2R;
22483 record_unwind_protect (unwind_format_mode_line,
22484 format_mode_line_unwind_data (NULL, NULL,
22485 Qnil, false));
22487 mode_line_target = MODE_LINE_DISPLAY;
22489 /* Temporarily make frame's keyboard the current kboard so that
22490 kboard-local variables in the mode_line_format will get the right
22491 values. */
22492 push_kboard (FRAME_KBOARD (it.f));
22493 record_unwind_save_match_data ();
22494 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22495 pop_kboard ();
22497 unbind_to (count, Qnil);
22499 /* Fill up with spaces. */
22500 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22502 compute_line_metrics (&it);
22503 it.glyph_row->full_width_p = true;
22504 it.glyph_row->continued_p = false;
22505 it.glyph_row->truncated_on_left_p = false;
22506 it.glyph_row->truncated_on_right_p = false;
22508 /* Make a 3D mode-line have a shadow at its right end. */
22509 face = FACE_FROM_ID (it.f, face_id);
22510 extend_face_to_end_of_line (&it);
22511 if (face->box != FACE_NO_BOX)
22513 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22514 + it.glyph_row->used[TEXT_AREA] - 1);
22515 last->right_box_line_p = true;
22518 return it.glyph_row->height;
22521 /* Move element ELT in LIST to the front of LIST.
22522 Return the updated list. */
22524 static Lisp_Object
22525 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22527 register Lisp_Object tail, prev;
22528 register Lisp_Object tem;
22530 tail = list;
22531 prev = Qnil;
22532 while (CONSP (tail))
22534 tem = XCAR (tail);
22536 if (EQ (elt, tem))
22538 /* Splice out the link TAIL. */
22539 if (NILP (prev))
22540 list = XCDR (tail);
22541 else
22542 Fsetcdr (prev, XCDR (tail));
22544 /* Now make it the first. */
22545 Fsetcdr (tail, list);
22546 return tail;
22548 else
22549 prev = tail;
22550 tail = XCDR (tail);
22551 QUIT;
22554 /* Not found--return unchanged LIST. */
22555 return list;
22558 /* Contribute ELT to the mode line for window IT->w. How it
22559 translates into text depends on its data type.
22561 IT describes the display environment in which we display, as usual.
22563 DEPTH is the depth in recursion. It is used to prevent
22564 infinite recursion here.
22566 FIELD_WIDTH is the number of characters the display of ELT should
22567 occupy in the mode line, and PRECISION is the maximum number of
22568 characters to display from ELT's representation. See
22569 display_string for details.
22571 Returns the hpos of the end of the text generated by ELT.
22573 PROPS is a property list to add to any string we encounter.
22575 If RISKY, remove (disregard) any properties in any string
22576 we encounter, and ignore :eval and :propertize.
22578 The global variable `mode_line_target' determines whether the
22579 output is passed to `store_mode_line_noprop',
22580 `store_mode_line_string', or `display_string'. */
22582 static int
22583 display_mode_element (struct it *it, int depth, int field_width, int precision,
22584 Lisp_Object elt, Lisp_Object props, bool risky)
22586 int n = 0, field, prec;
22587 bool literal = false;
22589 tail_recurse:
22590 if (depth > 100)
22591 elt = build_string ("*too-deep*");
22593 depth++;
22595 switch (XTYPE (elt))
22597 case Lisp_String:
22599 /* A string: output it and check for %-constructs within it. */
22600 unsigned char c;
22601 ptrdiff_t offset = 0;
22603 if (SCHARS (elt) > 0
22604 && (!NILP (props) || risky))
22606 Lisp_Object oprops, aelt;
22607 oprops = Ftext_properties_at (make_number (0), elt);
22609 /* If the starting string's properties are not what
22610 we want, translate the string. Also, if the string
22611 is risky, do that anyway. */
22613 if (NILP (Fequal (props, oprops)) || risky)
22615 /* If the starting string has properties,
22616 merge the specified ones onto the existing ones. */
22617 if (! NILP (oprops) && !risky)
22619 Lisp_Object tem;
22621 oprops = Fcopy_sequence (oprops);
22622 tem = props;
22623 while (CONSP (tem))
22625 oprops = Fplist_put (oprops, XCAR (tem),
22626 XCAR (XCDR (tem)));
22627 tem = XCDR (XCDR (tem));
22629 props = oprops;
22632 aelt = Fassoc (elt, mode_line_proptrans_alist);
22633 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22635 /* AELT is what we want. Move it to the front
22636 without consing. */
22637 elt = XCAR (aelt);
22638 mode_line_proptrans_alist
22639 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22641 else
22643 Lisp_Object tem;
22645 /* If AELT has the wrong props, it is useless.
22646 so get rid of it. */
22647 if (! NILP (aelt))
22648 mode_line_proptrans_alist
22649 = Fdelq (aelt, mode_line_proptrans_alist);
22651 elt = Fcopy_sequence (elt);
22652 Fset_text_properties (make_number (0), Flength (elt),
22653 props, elt);
22654 /* Add this item to mode_line_proptrans_alist. */
22655 mode_line_proptrans_alist
22656 = Fcons (Fcons (elt, props),
22657 mode_line_proptrans_alist);
22658 /* Truncate mode_line_proptrans_alist
22659 to at most 50 elements. */
22660 tem = Fnthcdr (make_number (50),
22661 mode_line_proptrans_alist);
22662 if (! NILP (tem))
22663 XSETCDR (tem, Qnil);
22668 offset = 0;
22670 if (literal)
22672 prec = precision - n;
22673 switch (mode_line_target)
22675 case MODE_LINE_NOPROP:
22676 case MODE_LINE_TITLE:
22677 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22678 break;
22679 case MODE_LINE_STRING:
22680 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22681 break;
22682 case MODE_LINE_DISPLAY:
22683 n += display_string (NULL, elt, Qnil, 0, 0, it,
22684 0, prec, 0, STRING_MULTIBYTE (elt));
22685 break;
22688 break;
22691 /* Handle the non-literal case. */
22693 while ((precision <= 0 || n < precision)
22694 && SREF (elt, offset) != 0
22695 && (mode_line_target != MODE_LINE_DISPLAY
22696 || it->current_x < it->last_visible_x))
22698 ptrdiff_t last_offset = offset;
22700 /* Advance to end of string or next format specifier. */
22701 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22704 if (offset - 1 != last_offset)
22706 ptrdiff_t nchars, nbytes;
22708 /* Output to end of string or up to '%'. Field width
22709 is length of string. Don't output more than
22710 PRECISION allows us. */
22711 offset--;
22713 prec = c_string_width (SDATA (elt) + last_offset,
22714 offset - last_offset, precision - n,
22715 &nchars, &nbytes);
22717 switch (mode_line_target)
22719 case MODE_LINE_NOPROP:
22720 case MODE_LINE_TITLE:
22721 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22722 break;
22723 case MODE_LINE_STRING:
22725 ptrdiff_t bytepos = last_offset;
22726 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22727 ptrdiff_t endpos = (precision <= 0
22728 ? string_byte_to_char (elt, offset)
22729 : charpos + nchars);
22730 Lisp_Object mode_string
22731 = Fsubstring (elt, make_number (charpos),
22732 make_number (endpos));
22733 n += store_mode_line_string (NULL, mode_string, false,
22734 0, 0, Qnil);
22736 break;
22737 case MODE_LINE_DISPLAY:
22739 ptrdiff_t bytepos = last_offset;
22740 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22742 if (precision <= 0)
22743 nchars = string_byte_to_char (elt, offset) - charpos;
22744 n += display_string (NULL, elt, Qnil, 0, charpos,
22745 it, 0, nchars, 0,
22746 STRING_MULTIBYTE (elt));
22748 break;
22751 else /* c == '%' */
22753 ptrdiff_t percent_position = offset;
22755 /* Get the specified minimum width. Zero means
22756 don't pad. */
22757 field = 0;
22758 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22759 field = field * 10 + c - '0';
22761 /* Don't pad beyond the total padding allowed. */
22762 if (field_width - n > 0 && field > field_width - n)
22763 field = field_width - n;
22765 /* Note that either PRECISION <= 0 or N < PRECISION. */
22766 prec = precision - n;
22768 if (c == 'M')
22769 n += display_mode_element (it, depth, field, prec,
22770 Vglobal_mode_string, props,
22771 risky);
22772 else if (c != 0)
22774 bool multibyte;
22775 ptrdiff_t bytepos, charpos;
22776 const char *spec;
22777 Lisp_Object string;
22779 bytepos = percent_position;
22780 charpos = (STRING_MULTIBYTE (elt)
22781 ? string_byte_to_char (elt, bytepos)
22782 : bytepos);
22783 spec = decode_mode_spec (it->w, c, field, &string);
22784 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22786 switch (mode_line_target)
22788 case MODE_LINE_NOPROP:
22789 case MODE_LINE_TITLE:
22790 n += store_mode_line_noprop (spec, field, prec);
22791 break;
22792 case MODE_LINE_STRING:
22794 Lisp_Object tem = build_string (spec);
22795 props = Ftext_properties_at (make_number (charpos), elt);
22796 /* Should only keep face property in props */
22797 n += store_mode_line_string (NULL, tem, false,
22798 field, prec, props);
22800 break;
22801 case MODE_LINE_DISPLAY:
22803 int nglyphs_before, nwritten;
22805 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22806 nwritten = display_string (spec, string, elt,
22807 charpos, 0, it,
22808 field, prec, 0,
22809 multibyte);
22811 /* Assign to the glyphs written above the
22812 string where the `%x' came from, position
22813 of the `%'. */
22814 if (nwritten > 0)
22816 struct glyph *glyph
22817 = (it->glyph_row->glyphs[TEXT_AREA]
22818 + nglyphs_before);
22819 int i;
22821 for (i = 0; i < nwritten; ++i)
22823 glyph[i].object = elt;
22824 glyph[i].charpos = charpos;
22827 n += nwritten;
22830 break;
22833 else /* c == 0 */
22834 break;
22838 break;
22840 case Lisp_Symbol:
22841 /* A symbol: process the value of the symbol recursively
22842 as if it appeared here directly. Avoid error if symbol void.
22843 Special case: if value of symbol is a string, output the string
22844 literally. */
22846 register Lisp_Object tem;
22848 /* If the variable is not marked as risky to set
22849 then its contents are risky to use. */
22850 if (NILP (Fget (elt, Qrisky_local_variable)))
22851 risky = true;
22853 tem = Fboundp (elt);
22854 if (!NILP (tem))
22856 tem = Fsymbol_value (elt);
22857 /* If value is a string, output that string literally:
22858 don't check for % within it. */
22859 if (STRINGP (tem))
22860 literal = true;
22862 if (!EQ (tem, elt))
22864 /* Give up right away for nil or t. */
22865 elt = tem;
22866 goto tail_recurse;
22870 break;
22872 case Lisp_Cons:
22874 register Lisp_Object car, tem;
22876 /* A cons cell: five distinct cases.
22877 If first element is :eval or :propertize, do something special.
22878 If first element is a string or a cons, process all the elements
22879 and effectively concatenate them.
22880 If first element is a negative number, truncate displaying cdr to
22881 at most that many characters. If positive, pad (with spaces)
22882 to at least that many characters.
22883 If first element is a symbol, process the cadr or caddr recursively
22884 according to whether the symbol's value is non-nil or nil. */
22885 car = XCAR (elt);
22886 if (EQ (car, QCeval))
22888 /* An element of the form (:eval FORM) means evaluate FORM
22889 and use the result as mode line elements. */
22891 if (risky)
22892 break;
22894 if (CONSP (XCDR (elt)))
22896 Lisp_Object spec;
22897 spec = safe__eval (true, XCAR (XCDR (elt)));
22898 n += display_mode_element (it, depth, field_width - n,
22899 precision - n, spec, props,
22900 risky);
22903 else if (EQ (car, QCpropertize))
22905 /* An element of the form (:propertize ELT PROPS...)
22906 means display ELT but applying properties PROPS. */
22908 if (risky)
22909 break;
22911 if (CONSP (XCDR (elt)))
22912 n += display_mode_element (it, depth, field_width - n,
22913 precision - n, XCAR (XCDR (elt)),
22914 XCDR (XCDR (elt)), risky);
22916 else if (SYMBOLP (car))
22918 tem = Fboundp (car);
22919 elt = XCDR (elt);
22920 if (!CONSP (elt))
22921 goto invalid;
22922 /* elt is now the cdr, and we know it is a cons cell.
22923 Use its car if CAR has a non-nil value. */
22924 if (!NILP (tem))
22926 tem = Fsymbol_value (car);
22927 if (!NILP (tem))
22929 elt = XCAR (elt);
22930 goto tail_recurse;
22933 /* Symbol's value is nil (or symbol is unbound)
22934 Get the cddr of the original list
22935 and if possible find the caddr and use that. */
22936 elt = XCDR (elt);
22937 if (NILP (elt))
22938 break;
22939 else if (!CONSP (elt))
22940 goto invalid;
22941 elt = XCAR (elt);
22942 goto tail_recurse;
22944 else if (INTEGERP (car))
22946 register int lim = XINT (car);
22947 elt = XCDR (elt);
22948 if (lim < 0)
22950 /* Negative int means reduce maximum width. */
22951 if (precision <= 0)
22952 precision = -lim;
22953 else
22954 precision = min (precision, -lim);
22956 else if (lim > 0)
22958 /* Padding specified. Don't let it be more than
22959 current maximum. */
22960 if (precision > 0)
22961 lim = min (precision, lim);
22963 /* If that's more padding than already wanted, queue it.
22964 But don't reduce padding already specified even if
22965 that is beyond the current truncation point. */
22966 field_width = max (lim, field_width);
22968 goto tail_recurse;
22970 else if (STRINGP (car) || CONSP (car))
22972 Lisp_Object halftail = elt;
22973 int len = 0;
22975 while (CONSP (elt)
22976 && (precision <= 0 || n < precision))
22978 n += display_mode_element (it, depth,
22979 /* Do padding only after the last
22980 element in the list. */
22981 (! CONSP (XCDR (elt))
22982 ? field_width - n
22983 : 0),
22984 precision - n, XCAR (elt),
22985 props, risky);
22986 elt = XCDR (elt);
22987 len++;
22988 if ((len & 1) == 0)
22989 halftail = XCDR (halftail);
22990 /* Check for cycle. */
22991 if (EQ (halftail, elt))
22992 break;
22996 break;
22998 default:
22999 invalid:
23000 elt = build_string ("*invalid*");
23001 goto tail_recurse;
23004 /* Pad to FIELD_WIDTH. */
23005 if (field_width > 0 && n < field_width)
23007 switch (mode_line_target)
23009 case MODE_LINE_NOPROP:
23010 case MODE_LINE_TITLE:
23011 n += store_mode_line_noprop ("", field_width - n, 0);
23012 break;
23013 case MODE_LINE_STRING:
23014 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23015 Qnil);
23016 break;
23017 case MODE_LINE_DISPLAY:
23018 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23019 0, 0, 0);
23020 break;
23024 return n;
23027 /* Store a mode-line string element in mode_line_string_list.
23029 If STRING is non-null, display that C string. Otherwise, the Lisp
23030 string LISP_STRING is displayed.
23032 FIELD_WIDTH is the minimum number of output glyphs to produce.
23033 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23034 with spaces. FIELD_WIDTH <= 0 means don't pad.
23036 PRECISION is the maximum number of characters to output from
23037 STRING. PRECISION <= 0 means don't truncate the string.
23039 If COPY_STRING, make a copy of LISP_STRING before adding
23040 properties to the string.
23042 PROPS are the properties to add to the string.
23043 The mode_line_string_face face property is always added to the string.
23046 static int
23047 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23048 bool copy_string,
23049 int field_width, int precision, Lisp_Object props)
23051 ptrdiff_t len;
23052 int n = 0;
23054 if (string != NULL)
23056 len = strlen (string);
23057 if (precision > 0 && len > precision)
23058 len = precision;
23059 lisp_string = make_string (string, len);
23060 if (NILP (props))
23061 props = mode_line_string_face_prop;
23062 else if (!NILP (mode_line_string_face))
23064 Lisp_Object face = Fplist_get (props, Qface);
23065 props = Fcopy_sequence (props);
23066 if (NILP (face))
23067 face = mode_line_string_face;
23068 else
23069 face = list2 (face, mode_line_string_face);
23070 props = Fplist_put (props, Qface, face);
23072 Fadd_text_properties (make_number (0), make_number (len),
23073 props, lisp_string);
23075 else
23077 len = XFASTINT (Flength (lisp_string));
23078 if (precision > 0 && len > precision)
23080 len = precision;
23081 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23082 precision = -1;
23084 if (!NILP (mode_line_string_face))
23086 Lisp_Object face;
23087 if (NILP (props))
23088 props = Ftext_properties_at (make_number (0), lisp_string);
23089 face = Fplist_get (props, Qface);
23090 if (NILP (face))
23091 face = mode_line_string_face;
23092 else
23093 face = list2 (face, mode_line_string_face);
23094 props = list2 (Qface, face);
23095 if (copy_string)
23096 lisp_string = Fcopy_sequence (lisp_string);
23098 if (!NILP (props))
23099 Fadd_text_properties (make_number (0), make_number (len),
23100 props, lisp_string);
23103 if (len > 0)
23105 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23106 n += len;
23109 if (field_width > len)
23111 field_width -= len;
23112 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23113 if (!NILP (props))
23114 Fadd_text_properties (make_number (0), make_number (field_width),
23115 props, lisp_string);
23116 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23117 n += field_width;
23120 return n;
23124 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23125 1, 4, 0,
23126 doc: /* Format a string out of a mode line format specification.
23127 First arg FORMAT specifies the mode line format (see `mode-line-format'
23128 for details) to use.
23130 By default, the format is evaluated for the currently selected window.
23132 Optional second arg FACE specifies the face property to put on all
23133 characters for which no face is specified. The value nil means the
23134 default face. The value t means whatever face the window's mode line
23135 currently uses (either `mode-line' or `mode-line-inactive',
23136 depending on whether the window is the selected window or not).
23137 An integer value means the value string has no text
23138 properties.
23140 Optional third and fourth args WINDOW and BUFFER specify the window
23141 and buffer to use as the context for the formatting (defaults
23142 are the selected window and the WINDOW's buffer). */)
23143 (Lisp_Object format, Lisp_Object face,
23144 Lisp_Object window, Lisp_Object buffer)
23146 struct it it;
23147 int len;
23148 struct window *w;
23149 struct buffer *old_buffer = NULL;
23150 int face_id;
23151 bool no_props = INTEGERP (face);
23152 ptrdiff_t count = SPECPDL_INDEX ();
23153 Lisp_Object str;
23154 int string_start = 0;
23156 w = decode_any_window (window);
23157 XSETWINDOW (window, w);
23159 if (NILP (buffer))
23160 buffer = w->contents;
23161 CHECK_BUFFER (buffer);
23163 /* Make formatting the modeline a non-op when noninteractive, otherwise
23164 there will be problems later caused by a partially initialized frame. */
23165 if (NILP (format) || noninteractive)
23166 return empty_unibyte_string;
23168 if (no_props)
23169 face = Qnil;
23171 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23172 : EQ (face, Qt) ? (EQ (window, selected_window)
23173 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23174 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23175 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23176 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23177 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23178 : DEFAULT_FACE_ID;
23180 old_buffer = current_buffer;
23182 /* Save things including mode_line_proptrans_alist,
23183 and set that to nil so that we don't alter the outer value. */
23184 record_unwind_protect (unwind_format_mode_line,
23185 format_mode_line_unwind_data
23186 (XFRAME (WINDOW_FRAME (w)),
23187 old_buffer, selected_window, true));
23188 mode_line_proptrans_alist = Qnil;
23190 Fselect_window (window, Qt);
23191 set_buffer_internal_1 (XBUFFER (buffer));
23193 init_iterator (&it, w, -1, -1, NULL, face_id);
23195 if (no_props)
23197 mode_line_target = MODE_LINE_NOPROP;
23198 mode_line_string_face_prop = Qnil;
23199 mode_line_string_list = Qnil;
23200 string_start = MODE_LINE_NOPROP_LEN (0);
23202 else
23204 mode_line_target = MODE_LINE_STRING;
23205 mode_line_string_list = Qnil;
23206 mode_line_string_face = face;
23207 mode_line_string_face_prop
23208 = NILP (face) ? Qnil : list2 (Qface, face);
23211 push_kboard (FRAME_KBOARD (it.f));
23212 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23213 pop_kboard ();
23215 if (no_props)
23217 len = MODE_LINE_NOPROP_LEN (string_start);
23218 str = make_string (mode_line_noprop_buf + string_start, len);
23220 else
23222 mode_line_string_list = Fnreverse (mode_line_string_list);
23223 str = Fmapconcat (Qidentity, mode_line_string_list,
23224 empty_unibyte_string);
23227 unbind_to (count, Qnil);
23228 return str;
23231 /* Write a null-terminated, right justified decimal representation of
23232 the positive integer D to BUF using a minimal field width WIDTH. */
23234 static void
23235 pint2str (register char *buf, register int width, register ptrdiff_t d)
23237 register char *p = buf;
23239 if (d <= 0)
23240 *p++ = '0';
23241 else
23243 while (d > 0)
23245 *p++ = d % 10 + '0';
23246 d /= 10;
23250 for (width -= (int) (p - buf); width > 0; --width)
23251 *p++ = ' ';
23252 *p-- = '\0';
23253 while (p > buf)
23255 d = *buf;
23256 *buf++ = *p;
23257 *p-- = d;
23261 /* Write a null-terminated, right justified decimal and "human
23262 readable" representation of the nonnegative integer D to BUF using
23263 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23265 static const char power_letter[] =
23267 0, /* no letter */
23268 'k', /* kilo */
23269 'M', /* mega */
23270 'G', /* giga */
23271 'T', /* tera */
23272 'P', /* peta */
23273 'E', /* exa */
23274 'Z', /* zetta */
23275 'Y' /* yotta */
23278 static void
23279 pint2hrstr (char *buf, int width, ptrdiff_t d)
23281 /* We aim to represent the nonnegative integer D as
23282 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23283 ptrdiff_t quotient = d;
23284 int remainder = 0;
23285 /* -1 means: do not use TENTHS. */
23286 int tenths = -1;
23287 int exponent = 0;
23289 /* Length of QUOTIENT.TENTHS as a string. */
23290 int length;
23292 char * psuffix;
23293 char * p;
23295 if (quotient >= 1000)
23297 /* Scale to the appropriate EXPONENT. */
23300 remainder = quotient % 1000;
23301 quotient /= 1000;
23302 exponent++;
23304 while (quotient >= 1000);
23306 /* Round to nearest and decide whether to use TENTHS or not. */
23307 if (quotient <= 9)
23309 tenths = remainder / 100;
23310 if (remainder % 100 >= 50)
23312 if (tenths < 9)
23313 tenths++;
23314 else
23316 quotient++;
23317 if (quotient == 10)
23318 tenths = -1;
23319 else
23320 tenths = 0;
23324 else
23325 if (remainder >= 500)
23327 if (quotient < 999)
23328 quotient++;
23329 else
23331 quotient = 1;
23332 exponent++;
23333 tenths = 0;
23338 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23339 if (tenths == -1 && quotient <= 99)
23340 if (quotient <= 9)
23341 length = 1;
23342 else
23343 length = 2;
23344 else
23345 length = 3;
23346 p = psuffix = buf + max (width, length);
23348 /* Print EXPONENT. */
23349 *psuffix++ = power_letter[exponent];
23350 *psuffix = '\0';
23352 /* Print TENTHS. */
23353 if (tenths >= 0)
23355 *--p = '0' + tenths;
23356 *--p = '.';
23359 /* Print QUOTIENT. */
23362 int digit = quotient % 10;
23363 *--p = '0' + digit;
23365 while ((quotient /= 10) != 0);
23367 /* Print leading spaces. */
23368 while (buf < p)
23369 *--p = ' ';
23372 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23373 If EOL_FLAG, set also a mnemonic character for end-of-line
23374 type of CODING_SYSTEM. Return updated pointer into BUF. */
23376 static unsigned char invalid_eol_type[] = "(*invalid*)";
23378 static char *
23379 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23381 Lisp_Object val;
23382 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23383 const unsigned char *eol_str;
23384 int eol_str_len;
23385 /* The EOL conversion we are using. */
23386 Lisp_Object eoltype;
23388 val = CODING_SYSTEM_SPEC (coding_system);
23389 eoltype = Qnil;
23391 if (!VECTORP (val)) /* Not yet decided. */
23393 *buf++ = multibyte ? '-' : ' ';
23394 if (eol_flag)
23395 eoltype = eol_mnemonic_undecided;
23396 /* Don't mention EOL conversion if it isn't decided. */
23398 else
23400 Lisp_Object attrs;
23401 Lisp_Object eolvalue;
23403 attrs = AREF (val, 0);
23404 eolvalue = AREF (val, 2);
23406 *buf++ = multibyte
23407 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23408 : ' ';
23410 if (eol_flag)
23412 /* The EOL conversion that is normal on this system. */
23414 if (NILP (eolvalue)) /* Not yet decided. */
23415 eoltype = eol_mnemonic_undecided;
23416 else if (VECTORP (eolvalue)) /* Not yet decided. */
23417 eoltype = eol_mnemonic_undecided;
23418 else /* eolvalue is Qunix, Qdos, or Qmac. */
23419 eoltype = (EQ (eolvalue, Qunix)
23420 ? eol_mnemonic_unix
23421 : EQ (eolvalue, Qdos)
23422 ? eol_mnemonic_dos : eol_mnemonic_mac);
23426 if (eol_flag)
23428 /* Mention the EOL conversion if it is not the usual one. */
23429 if (STRINGP (eoltype))
23431 eol_str = SDATA (eoltype);
23432 eol_str_len = SBYTES (eoltype);
23434 else if (CHARACTERP (eoltype))
23436 int c = XFASTINT (eoltype);
23437 return buf + CHAR_STRING (c, (unsigned char *) buf);
23439 else
23441 eol_str = invalid_eol_type;
23442 eol_str_len = sizeof (invalid_eol_type) - 1;
23444 memcpy (buf, eol_str, eol_str_len);
23445 buf += eol_str_len;
23448 return buf;
23451 /* Return the approximate percentage N is of D (rounding upward), or 99,
23452 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23454 static int
23455 percent99 (ptrdiff_t n, ptrdiff_t d)
23457 int percent = (d - 1 + 100.0 * n) / d;
23458 return min (percent, 99);
23461 /* Return a string for the output of a mode line %-spec for window W,
23462 generated by character C. FIELD_WIDTH > 0 means pad the string
23463 returned with spaces to that value. Return a Lisp string in
23464 *STRING if the resulting string is taken from that Lisp string.
23466 Note we operate on the current buffer for most purposes. */
23468 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23470 static const char *
23471 decode_mode_spec (struct window *w, register int c, int field_width,
23472 Lisp_Object *string)
23474 Lisp_Object obj;
23475 struct frame *f = XFRAME (WINDOW_FRAME (w));
23476 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23477 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23478 produce strings from numerical values, so limit preposterously
23479 large values of FIELD_WIDTH to avoid overrunning the buffer's
23480 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23481 bytes plus the terminating null. */
23482 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23483 struct buffer *b = current_buffer;
23485 obj = Qnil;
23486 *string = Qnil;
23488 switch (c)
23490 case '*':
23491 if (!NILP (BVAR (b, read_only)))
23492 return "%";
23493 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23494 return "*";
23495 return "-";
23497 case '+':
23498 /* This differs from %* only for a modified read-only buffer. */
23499 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23500 return "*";
23501 if (!NILP (BVAR (b, read_only)))
23502 return "%";
23503 return "-";
23505 case '&':
23506 /* This differs from %* in ignoring read-only-ness. */
23507 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23508 return "*";
23509 return "-";
23511 case '%':
23512 return "%";
23514 case '[':
23516 int i;
23517 char *p;
23519 if (command_loop_level > 5)
23520 return "[[[... ";
23521 p = decode_mode_spec_buf;
23522 for (i = 0; i < command_loop_level; i++)
23523 *p++ = '[';
23524 *p = 0;
23525 return decode_mode_spec_buf;
23528 case ']':
23530 int i;
23531 char *p;
23533 if (command_loop_level > 5)
23534 return " ...]]]";
23535 p = decode_mode_spec_buf;
23536 for (i = 0; i < command_loop_level; i++)
23537 *p++ = ']';
23538 *p = 0;
23539 return decode_mode_spec_buf;
23542 case '-':
23544 register int i;
23546 /* Let lots_of_dashes be a string of infinite length. */
23547 if (mode_line_target == MODE_LINE_NOPROP
23548 || mode_line_target == MODE_LINE_STRING)
23549 return "--";
23550 if (field_width <= 0
23551 || field_width > sizeof (lots_of_dashes))
23553 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23554 decode_mode_spec_buf[i] = '-';
23555 decode_mode_spec_buf[i] = '\0';
23556 return decode_mode_spec_buf;
23558 else
23559 return lots_of_dashes;
23562 case 'b':
23563 obj = BVAR (b, name);
23564 break;
23566 case 'c':
23567 /* %c and %l are ignored in `frame-title-format'.
23568 (In redisplay_internal, the frame title is drawn _before_ the
23569 windows are updated, so the stuff which depends on actual
23570 window contents (such as %l) may fail to render properly, or
23571 even crash emacs.) */
23572 if (mode_line_target == MODE_LINE_TITLE)
23573 return "";
23574 else
23576 ptrdiff_t col = current_column ();
23577 w->column_number_displayed = col;
23578 pint2str (decode_mode_spec_buf, width, col);
23579 return decode_mode_spec_buf;
23582 case 'e':
23583 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23585 if (NILP (Vmemory_full))
23586 return "";
23587 else
23588 return "!MEM FULL! ";
23590 #else
23591 return "";
23592 #endif
23594 case 'F':
23595 /* %F displays the frame name. */
23596 if (!NILP (f->title))
23597 return SSDATA (f->title);
23598 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23599 return SSDATA (f->name);
23600 return "Emacs";
23602 case 'f':
23603 obj = BVAR (b, filename);
23604 break;
23606 case 'i':
23608 ptrdiff_t size = ZV - BEGV;
23609 pint2str (decode_mode_spec_buf, width, size);
23610 return decode_mode_spec_buf;
23613 case 'I':
23615 ptrdiff_t size = ZV - BEGV;
23616 pint2hrstr (decode_mode_spec_buf, width, size);
23617 return decode_mode_spec_buf;
23620 case 'l':
23622 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23623 ptrdiff_t topline, nlines, height;
23624 ptrdiff_t junk;
23626 /* %c and %l are ignored in `frame-title-format'. */
23627 if (mode_line_target == MODE_LINE_TITLE)
23628 return "";
23630 startpos = marker_position (w->start);
23631 startpos_byte = marker_byte_position (w->start);
23632 height = WINDOW_TOTAL_LINES (w);
23634 /* If we decided that this buffer isn't suitable for line numbers,
23635 don't forget that too fast. */
23636 if (w->base_line_pos == -1)
23637 goto no_value;
23639 /* If the buffer is very big, don't waste time. */
23640 if (INTEGERP (Vline_number_display_limit)
23641 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23643 w->base_line_pos = 0;
23644 w->base_line_number = 0;
23645 goto no_value;
23648 if (w->base_line_number > 0
23649 && w->base_line_pos > 0
23650 && w->base_line_pos <= startpos)
23652 line = w->base_line_number;
23653 linepos = w->base_line_pos;
23654 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23656 else
23658 line = 1;
23659 linepos = BUF_BEGV (b);
23660 linepos_byte = BUF_BEGV_BYTE (b);
23663 /* Count lines from base line to window start position. */
23664 nlines = display_count_lines (linepos_byte,
23665 startpos_byte,
23666 startpos, &junk);
23668 topline = nlines + line;
23670 /* Determine a new base line, if the old one is too close
23671 or too far away, or if we did not have one.
23672 "Too close" means it's plausible a scroll-down would
23673 go back past it. */
23674 if (startpos == BUF_BEGV (b))
23676 w->base_line_number = topline;
23677 w->base_line_pos = BUF_BEGV (b);
23679 else if (nlines < height + 25 || nlines > height * 3 + 50
23680 || linepos == BUF_BEGV (b))
23682 ptrdiff_t limit = BUF_BEGV (b);
23683 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23684 ptrdiff_t position;
23685 ptrdiff_t distance =
23686 (height * 2 + 30) * line_number_display_limit_width;
23688 if (startpos - distance > limit)
23690 limit = startpos - distance;
23691 limit_byte = CHAR_TO_BYTE (limit);
23694 nlines = display_count_lines (startpos_byte,
23695 limit_byte,
23696 - (height * 2 + 30),
23697 &position);
23698 /* If we couldn't find the lines we wanted within
23699 line_number_display_limit_width chars per line,
23700 give up on line numbers for this window. */
23701 if (position == limit_byte && limit == startpos - distance)
23703 w->base_line_pos = -1;
23704 w->base_line_number = 0;
23705 goto no_value;
23708 w->base_line_number = topline - nlines;
23709 w->base_line_pos = BYTE_TO_CHAR (position);
23712 /* Now count lines from the start pos to point. */
23713 nlines = display_count_lines (startpos_byte,
23714 PT_BYTE, PT, &junk);
23716 /* Record that we did display the line number. */
23717 line_number_displayed = true;
23719 /* Make the string to show. */
23720 pint2str (decode_mode_spec_buf, width, topline + nlines);
23721 return decode_mode_spec_buf;
23722 no_value:
23724 char *p = decode_mode_spec_buf;
23725 int pad = width - 2;
23726 while (pad-- > 0)
23727 *p++ = ' ';
23728 *p++ = '?';
23729 *p++ = '?';
23730 *p = '\0';
23731 return decode_mode_spec_buf;
23734 break;
23736 case 'm':
23737 obj = BVAR (b, mode_name);
23738 break;
23740 case 'n':
23741 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23742 return " Narrow";
23743 break;
23745 case 'p':
23747 ptrdiff_t pos = marker_position (w->start);
23748 ptrdiff_t begv = BUF_BEGV (b);
23749 ptrdiff_t zv = BUF_ZV (b);
23751 if (w->window_end_pos <= BUF_Z (b) - zv)
23752 return pos <= begv ? "All" : "Bottom";
23753 else if (pos <= begv)
23754 return "Top";
23755 else
23757 sprintf (decode_mode_spec_buf, "%2d%%",
23758 percent99 (pos - begv, zv - begv));
23759 return decode_mode_spec_buf;
23763 /* Display percentage of size above the bottom of the screen. */
23764 case 'P':
23766 ptrdiff_t toppos = marker_position (w->start);
23767 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23768 ptrdiff_t begv = BUF_BEGV (b);
23769 ptrdiff_t zv = BUF_ZV (b);
23771 if (zv <= botpos)
23772 return toppos <= begv ? "All" : "Bottom";
23773 else
23775 sprintf (decode_mode_spec_buf,
23776 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23777 percent99 (botpos - begv, zv - begv));
23778 return decode_mode_spec_buf;
23782 case 's':
23783 /* status of process */
23784 obj = Fget_buffer_process (Fcurrent_buffer ());
23785 if (NILP (obj))
23786 return "no process";
23787 #ifndef MSDOS
23788 obj = Fsymbol_name (Fprocess_status (obj));
23789 #endif
23790 break;
23792 case '@':
23794 ptrdiff_t count = inhibit_garbage_collection ();
23795 Lisp_Object curdir = BVAR (current_buffer, directory);
23796 Lisp_Object val = Qnil;
23798 if (STRINGP (curdir))
23799 val = call1 (intern ("file-remote-p"), curdir);
23801 unbind_to (count, Qnil);
23803 if (NILP (val))
23804 return "-";
23805 else
23806 return "@";
23809 case 'z':
23810 /* coding-system (not including end-of-line format) */
23811 case 'Z':
23812 /* coding-system (including end-of-line type) */
23814 bool eol_flag = (c == 'Z');
23815 char *p = decode_mode_spec_buf;
23817 if (! FRAME_WINDOW_P (f))
23819 /* No need to mention EOL here--the terminal never needs
23820 to do EOL conversion. */
23821 p = decode_mode_spec_coding (CODING_ID_NAME
23822 (FRAME_KEYBOARD_CODING (f)->id),
23823 p, false);
23824 p = decode_mode_spec_coding (CODING_ID_NAME
23825 (FRAME_TERMINAL_CODING (f)->id),
23826 p, false);
23828 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23829 p, eol_flag);
23831 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23832 #ifdef subprocesses
23833 obj = Fget_buffer_process (Fcurrent_buffer ());
23834 if (PROCESSP (obj))
23836 p = decode_mode_spec_coding
23837 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23838 p = decode_mode_spec_coding
23839 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23841 #endif /* subprocesses */
23842 #endif /* false */
23843 *p = 0;
23844 return decode_mode_spec_buf;
23848 if (STRINGP (obj))
23850 *string = obj;
23851 return SSDATA (obj);
23853 else
23854 return "";
23858 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23859 means count lines back from START_BYTE. But don't go beyond
23860 LIMIT_BYTE. Return the number of lines thus found (always
23861 nonnegative).
23863 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23864 either the position COUNT lines after/before START_BYTE, if we
23865 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23866 COUNT lines. */
23868 static ptrdiff_t
23869 display_count_lines (ptrdiff_t start_byte,
23870 ptrdiff_t limit_byte, ptrdiff_t count,
23871 ptrdiff_t *byte_pos_ptr)
23873 register unsigned char *cursor;
23874 unsigned char *base;
23876 register ptrdiff_t ceiling;
23877 register unsigned char *ceiling_addr;
23878 ptrdiff_t orig_count = count;
23880 /* If we are not in selective display mode,
23881 check only for newlines. */
23882 bool selective_display
23883 = (!NILP (BVAR (current_buffer, selective_display))
23884 && !INTEGERP (BVAR (current_buffer, selective_display)));
23886 if (count > 0)
23888 while (start_byte < limit_byte)
23890 ceiling = BUFFER_CEILING_OF (start_byte);
23891 ceiling = min (limit_byte - 1, ceiling);
23892 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23893 base = (cursor = BYTE_POS_ADDR (start_byte));
23897 if (selective_display)
23899 while (*cursor != '\n' && *cursor != 015
23900 && ++cursor != ceiling_addr)
23901 continue;
23902 if (cursor == ceiling_addr)
23903 break;
23905 else
23907 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23908 if (! cursor)
23909 break;
23912 cursor++;
23914 if (--count == 0)
23916 start_byte += cursor - base;
23917 *byte_pos_ptr = start_byte;
23918 return orig_count;
23921 while (cursor < ceiling_addr);
23923 start_byte += ceiling_addr - base;
23926 else
23928 while (start_byte > limit_byte)
23930 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23931 ceiling = max (limit_byte, ceiling);
23932 ceiling_addr = BYTE_POS_ADDR (ceiling);
23933 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23934 while (true)
23936 if (selective_display)
23938 while (--cursor >= ceiling_addr
23939 && *cursor != '\n' && *cursor != 015)
23940 continue;
23941 if (cursor < ceiling_addr)
23942 break;
23944 else
23946 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23947 if (! cursor)
23948 break;
23951 if (++count == 0)
23953 start_byte += cursor - base + 1;
23954 *byte_pos_ptr = start_byte;
23955 /* When scanning backwards, we should
23956 not count the newline posterior to which we stop. */
23957 return - orig_count - 1;
23960 start_byte += ceiling_addr - base;
23964 *byte_pos_ptr = limit_byte;
23966 if (count < 0)
23967 return - orig_count + count;
23968 return orig_count - count;
23974 /***********************************************************************
23975 Displaying strings
23976 ***********************************************************************/
23978 /* Display a NUL-terminated string, starting with index START.
23980 If STRING is non-null, display that C string. Otherwise, the Lisp
23981 string LISP_STRING is displayed. There's a case that STRING is
23982 non-null and LISP_STRING is not nil. It means STRING is a string
23983 data of LISP_STRING. In that case, we display LISP_STRING while
23984 ignoring its text properties.
23986 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23987 FACE_STRING. Display STRING or LISP_STRING with the face at
23988 FACE_STRING_POS in FACE_STRING:
23990 Display the string in the environment given by IT, but use the
23991 standard display table, temporarily.
23993 FIELD_WIDTH is the minimum number of output glyphs to produce.
23994 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23995 with spaces. If STRING has more characters, more than FIELD_WIDTH
23996 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23998 PRECISION is the maximum number of characters to output from
23999 STRING. PRECISION < 0 means don't truncate the string.
24001 This is roughly equivalent to printf format specifiers:
24003 FIELD_WIDTH PRECISION PRINTF
24004 ----------------------------------------
24005 -1 -1 %s
24006 -1 10 %.10s
24007 10 -1 %10s
24008 20 10 %20.10s
24010 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24011 display them, and < 0 means obey the current buffer's value of
24012 enable_multibyte_characters.
24014 Value is the number of columns displayed. */
24016 static int
24017 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24018 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24019 int field_width, int precision, int max_x, int multibyte)
24021 int hpos_at_start = it->hpos;
24022 int saved_face_id = it->face_id;
24023 struct glyph_row *row = it->glyph_row;
24024 ptrdiff_t it_charpos;
24026 /* Initialize the iterator IT for iteration over STRING beginning
24027 with index START. */
24028 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24029 precision, field_width, multibyte);
24030 if (string && STRINGP (lisp_string))
24031 /* LISP_STRING is the one returned by decode_mode_spec. We should
24032 ignore its text properties. */
24033 it->stop_charpos = it->end_charpos;
24035 /* If displaying STRING, set up the face of the iterator from
24036 FACE_STRING, if that's given. */
24037 if (STRINGP (face_string))
24039 ptrdiff_t endptr;
24040 struct face *face;
24042 it->face_id
24043 = face_at_string_position (it->w, face_string, face_string_pos,
24044 0, &endptr, it->base_face_id, false);
24045 face = FACE_FROM_ID (it->f, it->face_id);
24046 it->face_box_p = face->box != FACE_NO_BOX;
24049 /* Set max_x to the maximum allowed X position. Don't let it go
24050 beyond the right edge of the window. */
24051 if (max_x <= 0)
24052 max_x = it->last_visible_x;
24053 else
24054 max_x = min (max_x, it->last_visible_x);
24056 /* Skip over display elements that are not visible. because IT->w is
24057 hscrolled. */
24058 if (it->current_x < it->first_visible_x)
24059 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24060 MOVE_TO_POS | MOVE_TO_X);
24062 row->ascent = it->max_ascent;
24063 row->height = it->max_ascent + it->max_descent;
24064 row->phys_ascent = it->max_phys_ascent;
24065 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24066 row->extra_line_spacing = it->max_extra_line_spacing;
24068 if (STRINGP (it->string))
24069 it_charpos = IT_STRING_CHARPOS (*it);
24070 else
24071 it_charpos = IT_CHARPOS (*it);
24073 /* This condition is for the case that we are called with current_x
24074 past last_visible_x. */
24075 while (it->current_x < max_x)
24077 int x_before, x, n_glyphs_before, i, nglyphs;
24079 /* Get the next display element. */
24080 if (!get_next_display_element (it))
24081 break;
24083 /* Produce glyphs. */
24084 x_before = it->current_x;
24085 n_glyphs_before = row->used[TEXT_AREA];
24086 PRODUCE_GLYPHS (it);
24088 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24089 i = 0;
24090 x = x_before;
24091 while (i < nglyphs)
24093 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24095 if (it->line_wrap != TRUNCATE
24096 && x + glyph->pixel_width > max_x)
24098 /* End of continued line or max_x reached. */
24099 if (CHAR_GLYPH_PADDING_P (*glyph))
24101 /* A wide character is unbreakable. */
24102 if (row->reversed_p)
24103 unproduce_glyphs (it, row->used[TEXT_AREA]
24104 - n_glyphs_before);
24105 row->used[TEXT_AREA] = n_glyphs_before;
24106 it->current_x = x_before;
24108 else
24110 if (row->reversed_p)
24111 unproduce_glyphs (it, row->used[TEXT_AREA]
24112 - (n_glyphs_before + i));
24113 row->used[TEXT_AREA] = n_glyphs_before + i;
24114 it->current_x = x;
24116 break;
24118 else if (x + glyph->pixel_width >= it->first_visible_x)
24120 /* Glyph is at least partially visible. */
24121 ++it->hpos;
24122 if (x < it->first_visible_x)
24123 row->x = x - it->first_visible_x;
24125 else
24127 /* Glyph is off the left margin of the display area.
24128 Should not happen. */
24129 emacs_abort ();
24132 row->ascent = max (row->ascent, it->max_ascent);
24133 row->height = max (row->height, it->max_ascent + it->max_descent);
24134 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24135 row->phys_height = max (row->phys_height,
24136 it->max_phys_ascent + it->max_phys_descent);
24137 row->extra_line_spacing = max (row->extra_line_spacing,
24138 it->max_extra_line_spacing);
24139 x += glyph->pixel_width;
24140 ++i;
24143 /* Stop if max_x reached. */
24144 if (i < nglyphs)
24145 break;
24147 /* Stop at line ends. */
24148 if (ITERATOR_AT_END_OF_LINE_P (it))
24150 it->continuation_lines_width = 0;
24151 break;
24154 set_iterator_to_next (it, true);
24155 if (STRINGP (it->string))
24156 it_charpos = IT_STRING_CHARPOS (*it);
24157 else
24158 it_charpos = IT_CHARPOS (*it);
24160 /* Stop if truncating at the right edge. */
24161 if (it->line_wrap == TRUNCATE
24162 && it->current_x >= it->last_visible_x)
24164 /* Add truncation mark, but don't do it if the line is
24165 truncated at a padding space. */
24166 if (it_charpos < it->string_nchars)
24168 if (!FRAME_WINDOW_P (it->f))
24170 int ii, n;
24172 if (it->current_x > it->last_visible_x)
24174 if (!row->reversed_p)
24176 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24177 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24178 break;
24180 else
24182 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24183 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24184 break;
24185 unproduce_glyphs (it, ii + 1);
24186 ii = row->used[TEXT_AREA] - (ii + 1);
24188 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24190 row->used[TEXT_AREA] = ii;
24191 produce_special_glyphs (it, IT_TRUNCATION);
24194 produce_special_glyphs (it, IT_TRUNCATION);
24196 row->truncated_on_right_p = true;
24198 break;
24202 /* Maybe insert a truncation at the left. */
24203 if (it->first_visible_x
24204 && it_charpos > 0)
24206 if (!FRAME_WINDOW_P (it->f)
24207 || (row->reversed_p
24208 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24209 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24210 insert_left_trunc_glyphs (it);
24211 row->truncated_on_left_p = true;
24214 it->face_id = saved_face_id;
24216 /* Value is number of columns displayed. */
24217 return it->hpos - hpos_at_start;
24222 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24223 appears as an element of LIST or as the car of an element of LIST.
24224 If PROPVAL is a list, compare each element against LIST in that
24225 way, and return 1/2 if any element of PROPVAL is found in LIST.
24226 Otherwise return 0. This function cannot quit.
24227 The return value is 2 if the text is invisible but with an ellipsis
24228 and 1 if it's invisible and without an ellipsis. */
24231 invisible_prop (Lisp_Object propval, Lisp_Object list)
24233 Lisp_Object tail, proptail;
24235 for (tail = list; CONSP (tail); tail = XCDR (tail))
24237 register Lisp_Object tem;
24238 tem = XCAR (tail);
24239 if (EQ (propval, tem))
24240 return 1;
24241 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24242 return NILP (XCDR (tem)) ? 1 : 2;
24245 if (CONSP (propval))
24247 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24249 Lisp_Object propelt;
24250 propelt = XCAR (proptail);
24251 for (tail = list; CONSP (tail); tail = XCDR (tail))
24253 register Lisp_Object tem;
24254 tem = XCAR (tail);
24255 if (EQ (propelt, tem))
24256 return 1;
24257 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24258 return NILP (XCDR (tem)) ? 1 : 2;
24263 return 0;
24266 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24267 doc: /* Non-nil if the property makes the text invisible.
24268 POS-OR-PROP can be a marker or number, in which case it is taken to be
24269 a position in the current buffer and the value of the `invisible' property
24270 is checked; or it can be some other value, which is then presumed to be the
24271 value of the `invisible' property of the text of interest.
24272 The non-nil value returned can be t for truly invisible text or something
24273 else if the text is replaced by an ellipsis. */)
24274 (Lisp_Object pos_or_prop)
24276 Lisp_Object prop
24277 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24278 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24279 : pos_or_prop);
24280 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24281 return (invis == 0 ? Qnil
24282 : invis == 1 ? Qt
24283 : make_number (invis));
24286 /* Calculate a width or height in pixels from a specification using
24287 the following elements:
24289 SPEC ::=
24290 NUM - a (fractional) multiple of the default font width/height
24291 (NUM) - specifies exactly NUM pixels
24292 UNIT - a fixed number of pixels, see below.
24293 ELEMENT - size of a display element in pixels, see below.
24294 (NUM . SPEC) - equals NUM * SPEC
24295 (+ SPEC SPEC ...) - add pixel values
24296 (- SPEC SPEC ...) - subtract pixel values
24297 (- SPEC) - negate pixel value
24299 NUM ::=
24300 INT or FLOAT - a number constant
24301 SYMBOL - use symbol's (buffer local) variable binding.
24303 UNIT ::=
24304 in - pixels per inch *)
24305 mm - pixels per 1/1000 meter *)
24306 cm - pixels per 1/100 meter *)
24307 width - width of current font in pixels.
24308 height - height of current font in pixels.
24310 *) using the ratio(s) defined in display-pixels-per-inch.
24312 ELEMENT ::=
24314 left-fringe - left fringe width in pixels
24315 right-fringe - right fringe width in pixels
24317 left-margin - left margin width in pixels
24318 right-margin - right margin width in pixels
24320 scroll-bar - scroll-bar area width in pixels
24322 Examples:
24324 Pixels corresponding to 5 inches:
24325 (5 . in)
24327 Total width of non-text areas on left side of window (if scroll-bar is on left):
24328 '(space :width (+ left-fringe left-margin scroll-bar))
24330 Align to first text column (in header line):
24331 '(space :align-to 0)
24333 Align to middle of text area minus half the width of variable `my-image'
24334 containing a loaded image:
24335 '(space :align-to (0.5 . (- text my-image)))
24337 Width of left margin minus width of 1 character in the default font:
24338 '(space :width (- left-margin 1))
24340 Width of left margin minus width of 2 characters in the current font:
24341 '(space :width (- left-margin (2 . width)))
24343 Center 1 character over left-margin (in header line):
24344 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24346 Different ways to express width of left fringe plus left margin minus one pixel:
24347 '(space :width (- (+ left-fringe left-margin) (1)))
24348 '(space :width (+ left-fringe left-margin (- (1))))
24349 '(space :width (+ left-fringe left-margin (-1)))
24353 static bool
24354 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24355 struct font *font, bool width_p, int *align_to)
24357 double pixels;
24359 # define OK_PIXELS(val) (*res = (val), true)
24360 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24362 if (NILP (prop))
24363 return OK_PIXELS (0);
24365 eassert (FRAME_LIVE_P (it->f));
24367 if (SYMBOLP (prop))
24369 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24371 char *unit = SSDATA (SYMBOL_NAME (prop));
24373 if (unit[0] == 'i' && unit[1] == 'n')
24374 pixels = 1.0;
24375 else if (unit[0] == 'm' && unit[1] == 'm')
24376 pixels = 25.4;
24377 else if (unit[0] == 'c' && unit[1] == 'm')
24378 pixels = 2.54;
24379 else
24380 pixels = 0;
24381 if (pixels > 0)
24383 double ppi = (width_p ? FRAME_RES_X (it->f)
24384 : FRAME_RES_Y (it->f));
24386 if (ppi > 0)
24387 return OK_PIXELS (ppi / pixels);
24388 return false;
24392 #ifdef HAVE_WINDOW_SYSTEM
24393 if (EQ (prop, Qheight))
24394 return OK_PIXELS (font
24395 ? normal_char_height (font, -1)
24396 : FRAME_LINE_HEIGHT (it->f));
24397 if (EQ (prop, Qwidth))
24398 return OK_PIXELS (font
24399 ? FONT_WIDTH (font)
24400 : FRAME_COLUMN_WIDTH (it->f));
24401 #else
24402 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24403 return OK_PIXELS (1);
24404 #endif
24406 if (EQ (prop, Qtext))
24407 return OK_PIXELS (width_p
24408 ? window_box_width (it->w, TEXT_AREA)
24409 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24411 if (align_to && *align_to < 0)
24413 *res = 0;
24414 if (EQ (prop, Qleft))
24415 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24416 if (EQ (prop, Qright))
24417 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24418 if (EQ (prop, Qcenter))
24419 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24420 + window_box_width (it->w, TEXT_AREA) / 2);
24421 if (EQ (prop, Qleft_fringe))
24422 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24423 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24424 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24425 if (EQ (prop, Qright_fringe))
24426 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24427 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24428 : window_box_right_offset (it->w, TEXT_AREA));
24429 if (EQ (prop, Qleft_margin))
24430 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24431 if (EQ (prop, Qright_margin))
24432 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24433 if (EQ (prop, Qscroll_bar))
24434 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24436 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24437 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24438 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24439 : 0)));
24441 else
24443 if (EQ (prop, Qleft_fringe))
24444 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24445 if (EQ (prop, Qright_fringe))
24446 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24447 if (EQ (prop, Qleft_margin))
24448 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24449 if (EQ (prop, Qright_margin))
24450 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24451 if (EQ (prop, Qscroll_bar))
24452 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24455 prop = buffer_local_value (prop, it->w->contents);
24456 if (EQ (prop, Qunbound))
24457 prop = Qnil;
24460 if (NUMBERP (prop))
24462 int base_unit = (width_p
24463 ? FRAME_COLUMN_WIDTH (it->f)
24464 : FRAME_LINE_HEIGHT (it->f));
24465 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24468 if (CONSP (prop))
24470 Lisp_Object car = XCAR (prop);
24471 Lisp_Object cdr = XCDR (prop);
24473 if (SYMBOLP (car))
24475 #ifdef HAVE_WINDOW_SYSTEM
24476 if (FRAME_WINDOW_P (it->f)
24477 && valid_image_p (prop))
24479 ptrdiff_t id = lookup_image (it->f, prop);
24480 struct image *img = IMAGE_FROM_ID (it->f, id);
24482 return OK_PIXELS (width_p ? img->width : img->height);
24484 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24486 // TODO: Don't return dummy size.
24487 return OK_PIXELS (100);
24489 #endif
24490 if (EQ (car, Qplus) || EQ (car, Qminus))
24492 bool first = true;
24493 double px;
24495 pixels = 0;
24496 while (CONSP (cdr))
24498 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24499 font, width_p, align_to))
24500 return false;
24501 if (first)
24502 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24503 else
24504 pixels += px;
24505 cdr = XCDR (cdr);
24507 if (EQ (car, Qminus))
24508 pixels = -pixels;
24509 return OK_PIXELS (pixels);
24512 car = buffer_local_value (car, it->w->contents);
24513 if (EQ (car, Qunbound))
24514 car = Qnil;
24517 if (NUMBERP (car))
24519 double fact;
24520 pixels = XFLOATINT (car);
24521 if (NILP (cdr))
24522 return OK_PIXELS (pixels);
24523 if (calc_pixel_width_or_height (&fact, it, cdr,
24524 font, width_p, align_to))
24525 return OK_PIXELS (pixels * fact);
24526 return false;
24529 return false;
24532 return false;
24535 void
24536 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24538 #ifdef HAVE_WINDOW_SYSTEM
24539 normal_char_ascent_descent (font, -1, ascent, descent);
24540 #else
24541 *ascent = 1;
24542 *descent = 0;
24543 #endif
24547 /***********************************************************************
24548 Glyph Display
24549 ***********************************************************************/
24551 #ifdef HAVE_WINDOW_SYSTEM
24553 #ifdef GLYPH_DEBUG
24555 void
24556 dump_glyph_string (struct glyph_string *s)
24558 fprintf (stderr, "glyph string\n");
24559 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24560 s->x, s->y, s->width, s->height);
24561 fprintf (stderr, " ybase = %d\n", s->ybase);
24562 fprintf (stderr, " hl = %d\n", s->hl);
24563 fprintf (stderr, " left overhang = %d, right = %d\n",
24564 s->left_overhang, s->right_overhang);
24565 fprintf (stderr, " nchars = %d\n", s->nchars);
24566 fprintf (stderr, " extends to end of line = %d\n",
24567 s->extends_to_end_of_line_p);
24568 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24569 fprintf (stderr, " bg width = %d\n", s->background_width);
24572 #endif /* GLYPH_DEBUG */
24574 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24575 of XChar2b structures for S; it can't be allocated in
24576 init_glyph_string because it must be allocated via `alloca'. W
24577 is the window on which S is drawn. ROW and AREA are the glyph row
24578 and area within the row from which S is constructed. START is the
24579 index of the first glyph structure covered by S. HL is a
24580 face-override for drawing S. */
24582 #ifdef HAVE_NTGUI
24583 #define OPTIONAL_HDC(hdc) HDC hdc,
24584 #define DECLARE_HDC(hdc) HDC hdc;
24585 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24586 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24587 #endif
24589 #ifndef OPTIONAL_HDC
24590 #define OPTIONAL_HDC(hdc)
24591 #define DECLARE_HDC(hdc)
24592 #define ALLOCATE_HDC(hdc, f)
24593 #define RELEASE_HDC(hdc, f)
24594 #endif
24596 static void
24597 init_glyph_string (struct glyph_string *s,
24598 OPTIONAL_HDC (hdc)
24599 XChar2b *char2b, struct window *w, struct glyph_row *row,
24600 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24602 memset (s, 0, sizeof *s);
24603 s->w = w;
24604 s->f = XFRAME (w->frame);
24605 #ifdef HAVE_NTGUI
24606 s->hdc = hdc;
24607 #endif
24608 s->display = FRAME_X_DISPLAY (s->f);
24609 s->window = FRAME_X_WINDOW (s->f);
24610 s->char2b = char2b;
24611 s->hl = hl;
24612 s->row = row;
24613 s->area = area;
24614 s->first_glyph = row->glyphs[area] + start;
24615 s->height = row->height;
24616 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24617 s->ybase = s->y + row->ascent;
24621 /* Append the list of glyph strings with head H and tail T to the list
24622 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24624 static void
24625 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24626 struct glyph_string *h, struct glyph_string *t)
24628 if (h)
24630 if (*head)
24631 (*tail)->next = h;
24632 else
24633 *head = h;
24634 h->prev = *tail;
24635 *tail = t;
24640 /* Prepend the list of glyph strings with head H and tail T to the
24641 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24642 result. */
24644 static void
24645 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24646 struct glyph_string *h, struct glyph_string *t)
24648 if (h)
24650 if (*head)
24651 (*head)->prev = t;
24652 else
24653 *tail = t;
24654 t->next = *head;
24655 *head = h;
24660 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24661 Set *HEAD and *TAIL to the resulting list. */
24663 static void
24664 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24665 struct glyph_string *s)
24667 s->next = s->prev = NULL;
24668 append_glyph_string_lists (head, tail, s, s);
24672 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24673 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24674 make sure that X resources for the face returned are allocated.
24675 Value is a pointer to a realized face that is ready for display if
24676 DISPLAY_P. */
24678 static struct face *
24679 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24680 XChar2b *char2b, bool display_p)
24682 struct face *face = FACE_FROM_ID (f, face_id);
24683 unsigned code = 0;
24685 if (face->font)
24687 code = face->font->driver->encode_char (face->font, c);
24689 if (code == FONT_INVALID_CODE)
24690 code = 0;
24692 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24694 /* Make sure X resources of the face are allocated. */
24695 #ifdef HAVE_X_WINDOWS
24696 if (display_p)
24697 #endif
24699 eassert (face != NULL);
24700 prepare_face_for_display (f, face);
24703 return face;
24707 /* Get face and two-byte form of character glyph GLYPH on frame F.
24708 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24709 a pointer to a realized face that is ready for display. */
24711 static struct face *
24712 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24713 XChar2b *char2b)
24715 struct face *face;
24716 unsigned code = 0;
24718 eassert (glyph->type == CHAR_GLYPH);
24719 face = FACE_FROM_ID (f, glyph->face_id);
24721 /* Make sure X resources of the face are allocated. */
24722 prepare_face_for_display (f, face);
24724 if (face->font)
24726 if (CHAR_BYTE8_P (glyph->u.ch))
24727 code = CHAR_TO_BYTE8 (glyph->u.ch);
24728 else
24729 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24731 if (code == FONT_INVALID_CODE)
24732 code = 0;
24735 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24736 return face;
24740 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24741 Return true iff FONT has a glyph for C. */
24743 static bool
24744 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24746 unsigned code;
24748 if (CHAR_BYTE8_P (c))
24749 code = CHAR_TO_BYTE8 (c);
24750 else
24751 code = font->driver->encode_char (font, c);
24753 if (code == FONT_INVALID_CODE)
24754 return false;
24755 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24756 return true;
24760 /* Fill glyph string S with composition components specified by S->cmp.
24762 BASE_FACE is the base face of the composition.
24763 S->cmp_from is the index of the first component for S.
24765 OVERLAPS non-zero means S should draw the foreground only, and use
24766 its physical height for clipping. See also draw_glyphs.
24768 Value is the index of a component not in S. */
24770 static int
24771 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24772 int overlaps)
24774 int i;
24775 /* For all glyphs of this composition, starting at the offset
24776 S->cmp_from, until we reach the end of the definition or encounter a
24777 glyph that requires the different face, add it to S. */
24778 struct face *face;
24780 eassert (s);
24782 s->for_overlaps = overlaps;
24783 s->face = NULL;
24784 s->font = NULL;
24785 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24787 int c = COMPOSITION_GLYPH (s->cmp, i);
24789 /* TAB in a composition means display glyphs with padding space
24790 on the left or right. */
24791 if (c != '\t')
24793 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24794 -1, Qnil);
24796 face = get_char_face_and_encoding (s->f, c, face_id,
24797 s->char2b + i, true);
24798 if (face)
24800 if (! s->face)
24802 s->face = face;
24803 s->font = s->face->font;
24805 else if (s->face != face)
24806 break;
24809 ++s->nchars;
24811 s->cmp_to = i;
24813 if (s->face == NULL)
24815 s->face = base_face->ascii_face;
24816 s->font = s->face->font;
24819 /* All glyph strings for the same composition has the same width,
24820 i.e. the width set for the first component of the composition. */
24821 s->width = s->first_glyph->pixel_width;
24823 /* If the specified font could not be loaded, use the frame's
24824 default font, but record the fact that we couldn't load it in
24825 the glyph string so that we can draw rectangles for the
24826 characters of the glyph string. */
24827 if (s->font == NULL)
24829 s->font_not_found_p = true;
24830 s->font = FRAME_FONT (s->f);
24833 /* Adjust base line for subscript/superscript text. */
24834 s->ybase += s->first_glyph->voffset;
24836 return s->cmp_to;
24839 static int
24840 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24841 int start, int end, int overlaps)
24843 struct glyph *glyph, *last;
24844 Lisp_Object lgstring;
24845 int i;
24847 s->for_overlaps = overlaps;
24848 glyph = s->row->glyphs[s->area] + start;
24849 last = s->row->glyphs[s->area] + end;
24850 s->cmp_id = glyph->u.cmp.id;
24851 s->cmp_from = glyph->slice.cmp.from;
24852 s->cmp_to = glyph->slice.cmp.to + 1;
24853 s->face = FACE_FROM_ID (s->f, face_id);
24854 lgstring = composition_gstring_from_id (s->cmp_id);
24855 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24856 glyph++;
24857 while (glyph < last
24858 && glyph->u.cmp.automatic
24859 && glyph->u.cmp.id == s->cmp_id
24860 && s->cmp_to == glyph->slice.cmp.from)
24861 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24863 for (i = s->cmp_from; i < s->cmp_to; i++)
24865 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24866 unsigned code = LGLYPH_CODE (lglyph);
24868 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24870 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24871 return glyph - s->row->glyphs[s->area];
24875 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24876 See the comment of fill_glyph_string for arguments.
24877 Value is the index of the first glyph not in S. */
24880 static int
24881 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24882 int start, int end, int overlaps)
24884 struct glyph *glyph, *last;
24885 int voffset;
24887 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24888 s->for_overlaps = overlaps;
24889 glyph = s->row->glyphs[s->area] + start;
24890 last = s->row->glyphs[s->area] + end;
24891 voffset = glyph->voffset;
24892 s->face = FACE_FROM_ID (s->f, face_id);
24893 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24894 s->nchars = 1;
24895 s->width = glyph->pixel_width;
24896 glyph++;
24897 while (glyph < last
24898 && glyph->type == GLYPHLESS_GLYPH
24899 && glyph->voffset == voffset
24900 && glyph->face_id == face_id)
24902 s->nchars++;
24903 s->width += glyph->pixel_width;
24904 glyph++;
24906 s->ybase += voffset;
24907 return glyph - s->row->glyphs[s->area];
24911 /* Fill glyph string S from a sequence of character glyphs.
24913 FACE_ID is the face id of the string. START is the index of the
24914 first glyph to consider, END is the index of the last + 1.
24915 OVERLAPS non-zero means S should draw the foreground only, and use
24916 its physical height for clipping. See also draw_glyphs.
24918 Value is the index of the first glyph not in S. */
24920 static int
24921 fill_glyph_string (struct glyph_string *s, int face_id,
24922 int start, int end, int overlaps)
24924 struct glyph *glyph, *last;
24925 int voffset;
24926 bool glyph_not_available_p;
24928 eassert (s->f == XFRAME (s->w->frame));
24929 eassert (s->nchars == 0);
24930 eassert (start >= 0 && end > start);
24932 s->for_overlaps = overlaps;
24933 glyph = s->row->glyphs[s->area] + start;
24934 last = s->row->glyphs[s->area] + end;
24935 voffset = glyph->voffset;
24936 s->padding_p = glyph->padding_p;
24937 glyph_not_available_p = glyph->glyph_not_available_p;
24939 while (glyph < last
24940 && glyph->type == CHAR_GLYPH
24941 && glyph->voffset == voffset
24942 /* Same face id implies same font, nowadays. */
24943 && glyph->face_id == face_id
24944 && glyph->glyph_not_available_p == glyph_not_available_p)
24946 s->face = get_glyph_face_and_encoding (s->f, glyph,
24947 s->char2b + s->nchars);
24948 ++s->nchars;
24949 eassert (s->nchars <= end - start);
24950 s->width += glyph->pixel_width;
24951 if (glyph++->padding_p != s->padding_p)
24952 break;
24955 s->font = s->face->font;
24957 /* If the specified font could not be loaded, use the frame's font,
24958 but record the fact that we couldn't load it in
24959 S->font_not_found_p so that we can draw rectangles for the
24960 characters of the glyph string. */
24961 if (s->font == NULL || glyph_not_available_p)
24963 s->font_not_found_p = true;
24964 s->font = FRAME_FONT (s->f);
24967 /* Adjust base line for subscript/superscript text. */
24968 s->ybase += voffset;
24970 eassert (s->face && s->face->gc);
24971 return glyph - s->row->glyphs[s->area];
24975 /* Fill glyph string S from image glyph S->first_glyph. */
24977 static void
24978 fill_image_glyph_string (struct glyph_string *s)
24980 eassert (s->first_glyph->type == IMAGE_GLYPH);
24981 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24982 eassert (s->img);
24983 s->slice = s->first_glyph->slice.img;
24984 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24985 s->font = s->face->font;
24986 s->width = s->first_glyph->pixel_width;
24988 /* Adjust base line for subscript/superscript text. */
24989 s->ybase += s->first_glyph->voffset;
24993 #ifdef HAVE_XWIDGETS
24994 static void
24995 fill_xwidget_glyph_string (struct glyph_string *s)
24997 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24998 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24999 s->font = s->face->font;
25000 s->width = s->first_glyph->pixel_width;
25001 s->ybase += s->first_glyph->voffset;
25002 s->xwidget = s->first_glyph->u.xwidget;
25004 #endif
25005 /* Fill glyph string S from a sequence of stretch glyphs.
25007 START is the index of the first glyph to consider,
25008 END is the index of the last + 1.
25010 Value is the index of the first glyph not in S. */
25012 static int
25013 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25015 struct glyph *glyph, *last;
25016 int voffset, face_id;
25018 eassert (s->first_glyph->type == STRETCH_GLYPH);
25020 glyph = s->row->glyphs[s->area] + start;
25021 last = s->row->glyphs[s->area] + end;
25022 face_id = glyph->face_id;
25023 s->face = FACE_FROM_ID (s->f, face_id);
25024 s->font = s->face->font;
25025 s->width = glyph->pixel_width;
25026 s->nchars = 1;
25027 voffset = glyph->voffset;
25029 for (++glyph;
25030 (glyph < last
25031 && glyph->type == STRETCH_GLYPH
25032 && glyph->voffset == voffset
25033 && glyph->face_id == face_id);
25034 ++glyph)
25035 s->width += glyph->pixel_width;
25037 /* Adjust base line for subscript/superscript text. */
25038 s->ybase += voffset;
25040 /* The case that face->gc == 0 is handled when drawing the glyph
25041 string by calling prepare_face_for_display. */
25042 eassert (s->face);
25043 return glyph - s->row->glyphs[s->area];
25046 static struct font_metrics *
25047 get_per_char_metric (struct font *font, XChar2b *char2b)
25049 static struct font_metrics metrics;
25050 unsigned code;
25052 if (! font)
25053 return NULL;
25054 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25055 if (code == FONT_INVALID_CODE)
25056 return NULL;
25057 font->driver->text_extents (font, &code, 1, &metrics);
25058 return &metrics;
25061 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25062 for FONT. Values are taken from font-global ones, except for fonts
25063 that claim preposterously large values, but whose glyphs actually
25064 have reasonable dimensions. C is the character to use for metrics
25065 if the font-global values are too large; if C is negative, the
25066 function selects a default character. */
25067 static void
25068 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25070 *ascent = FONT_BASE (font);
25071 *descent = FONT_DESCENT (font);
25073 if (FONT_TOO_HIGH (font))
25075 XChar2b char2b;
25077 /* Get metrics of C, defaulting to a reasonably sized ASCII
25078 character. */
25079 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25081 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25083 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25085 /* We add 1 pixel to character dimensions as heuristics
25086 that produces nicer display, e.g. when the face has
25087 the box attribute. */
25088 *ascent = pcm->ascent + 1;
25089 *descent = pcm->descent + 1;
25095 /* A subroutine that computes a reasonable "normal character height"
25096 for fonts that claim preposterously large vertical dimensions, but
25097 whose glyphs are actually reasonably sized. C is the character
25098 whose metrics to use for those fonts, or -1 for default
25099 character. */
25100 static int
25101 normal_char_height (struct font *font, int c)
25103 int ascent, descent;
25105 normal_char_ascent_descent (font, c, &ascent, &descent);
25107 return ascent + descent;
25110 /* EXPORT for RIF:
25111 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25112 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25113 assumed to be zero. */
25115 void
25116 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25118 *left = *right = 0;
25120 if (glyph->type == CHAR_GLYPH)
25122 XChar2b char2b;
25123 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25124 if (face->font)
25126 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25127 if (pcm)
25129 if (pcm->rbearing > pcm->width)
25130 *right = pcm->rbearing - pcm->width;
25131 if (pcm->lbearing < 0)
25132 *left = -pcm->lbearing;
25136 else if (glyph->type == COMPOSITE_GLYPH)
25138 if (! glyph->u.cmp.automatic)
25140 struct composition *cmp = composition_table[glyph->u.cmp.id];
25142 if (cmp->rbearing > cmp->pixel_width)
25143 *right = cmp->rbearing - cmp->pixel_width;
25144 if (cmp->lbearing < 0)
25145 *left = - cmp->lbearing;
25147 else
25149 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25150 struct font_metrics metrics;
25152 composition_gstring_width (gstring, glyph->slice.cmp.from,
25153 glyph->slice.cmp.to + 1, &metrics);
25154 if (metrics.rbearing > metrics.width)
25155 *right = metrics.rbearing - metrics.width;
25156 if (metrics.lbearing < 0)
25157 *left = - metrics.lbearing;
25163 /* Return the index of the first glyph preceding glyph string S that
25164 is overwritten by S because of S's left overhang. Value is -1
25165 if no glyphs are overwritten. */
25167 static int
25168 left_overwritten (struct glyph_string *s)
25170 int k;
25172 if (s->left_overhang)
25174 int x = 0, i;
25175 struct glyph *glyphs = s->row->glyphs[s->area];
25176 int first = s->first_glyph - glyphs;
25178 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25179 x -= glyphs[i].pixel_width;
25181 k = i + 1;
25183 else
25184 k = -1;
25186 return k;
25190 /* Return the index of the first glyph preceding glyph string S that
25191 is overwriting S because of its right overhang. Value is -1 if no
25192 glyph in front of S overwrites S. */
25194 static int
25195 left_overwriting (struct glyph_string *s)
25197 int i, k, x;
25198 struct glyph *glyphs = s->row->glyphs[s->area];
25199 int first = s->first_glyph - glyphs;
25201 k = -1;
25202 x = 0;
25203 for (i = first - 1; i >= 0; --i)
25205 int left, right;
25206 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25207 if (x + right > 0)
25208 k = i;
25209 x -= glyphs[i].pixel_width;
25212 return k;
25216 /* Return the index of the last glyph following glyph string S that is
25217 overwritten by S because of S's right overhang. Value is -1 if
25218 no such glyph is found. */
25220 static int
25221 right_overwritten (struct glyph_string *s)
25223 int k = -1;
25225 if (s->right_overhang)
25227 int x = 0, i;
25228 struct glyph *glyphs = s->row->glyphs[s->area];
25229 int first = (s->first_glyph - glyphs
25230 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25231 int end = s->row->used[s->area];
25233 for (i = first; i < end && s->right_overhang > x; ++i)
25234 x += glyphs[i].pixel_width;
25236 k = i;
25239 return k;
25243 /* Return the index of the last glyph following glyph string S that
25244 overwrites S because of its left overhang. Value is negative
25245 if no such glyph is found. */
25247 static int
25248 right_overwriting (struct glyph_string *s)
25250 int i, k, x;
25251 int end = s->row->used[s->area];
25252 struct glyph *glyphs = s->row->glyphs[s->area];
25253 int first = (s->first_glyph - glyphs
25254 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25256 k = -1;
25257 x = 0;
25258 for (i = first; i < end; ++i)
25260 int left, right;
25261 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25262 if (x - left < 0)
25263 k = i;
25264 x += glyphs[i].pixel_width;
25267 return k;
25271 /* Set background width of glyph string S. START is the index of the
25272 first glyph following S. LAST_X is the right-most x-position + 1
25273 in the drawing area. */
25275 static void
25276 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25278 /* If the face of this glyph string has to be drawn to the end of
25279 the drawing area, set S->extends_to_end_of_line_p. */
25281 if (start == s->row->used[s->area]
25282 && ((s->row->fill_line_p
25283 && (s->hl == DRAW_NORMAL_TEXT
25284 || s->hl == DRAW_IMAGE_RAISED
25285 || s->hl == DRAW_IMAGE_SUNKEN))
25286 || s->hl == DRAW_MOUSE_FACE))
25287 s->extends_to_end_of_line_p = true;
25289 /* If S extends its face to the end of the line, set its
25290 background_width to the distance to the right edge of the drawing
25291 area. */
25292 if (s->extends_to_end_of_line_p)
25293 s->background_width = last_x - s->x + 1;
25294 else
25295 s->background_width = s->width;
25299 /* Compute overhangs and x-positions for glyph string S and its
25300 predecessors, or successors. X is the starting x-position for S.
25301 BACKWARD_P means process predecessors. */
25303 static void
25304 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25306 if (backward_p)
25308 while (s)
25310 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25311 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25312 x -= s->width;
25313 s->x = x;
25314 s = s->prev;
25317 else
25319 while (s)
25321 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25322 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25323 s->x = x;
25324 x += s->width;
25325 s = s->next;
25332 /* The following macros are only called from draw_glyphs below.
25333 They reference the following parameters of that function directly:
25334 `w', `row', `area', and `overlap_p'
25335 as well as the following local variables:
25336 `s', `f', and `hdc' (in W32) */
25338 #ifdef HAVE_NTGUI
25339 /* On W32, silently add local `hdc' variable to argument list of
25340 init_glyph_string. */
25341 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25342 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25343 #else
25344 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25345 init_glyph_string (s, char2b, w, row, area, start, hl)
25346 #endif
25348 /* Add a glyph string for a stretch glyph to the list of strings
25349 between HEAD and TAIL. START is the index of the stretch glyph in
25350 row area AREA of glyph row ROW. END is the index of the last glyph
25351 in that glyph row area. X is the current output position assigned
25352 to the new glyph string constructed. HL overrides that face of the
25353 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25354 is the right-most x-position of the drawing area. */
25356 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25357 and below -- keep them on one line. */
25358 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25359 do \
25361 s = alloca (sizeof *s); \
25362 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25363 START = fill_stretch_glyph_string (s, START, END); \
25364 append_glyph_string (&HEAD, &TAIL, s); \
25365 s->x = (X); \
25367 while (false)
25370 /* Add a glyph string for an image glyph to the list of strings
25371 between HEAD and TAIL. START is the index of the image glyph in
25372 row area AREA of glyph row ROW. END is the index of the last glyph
25373 in that glyph row area. X is the current output position assigned
25374 to the new glyph string constructed. HL overrides that face of the
25375 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25376 is the right-most x-position of the drawing area. */
25378 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25379 do \
25381 s = alloca (sizeof *s); \
25382 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25383 fill_image_glyph_string (s); \
25384 append_glyph_string (&HEAD, &TAIL, s); \
25385 ++START; \
25386 s->x = (X); \
25388 while (false)
25390 #ifndef HAVE_XWIDGETS
25391 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25392 eassume (false)
25393 #else
25394 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25395 do \
25397 s = alloca (sizeof *s); \
25398 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25399 fill_xwidget_glyph_string (s); \
25400 append_glyph_string (&(HEAD), &(TAIL), s); \
25401 ++(START); \
25402 s->x = (X); \
25404 while (false)
25405 #endif
25407 /* Add a glyph string for a sequence of character glyphs to the list
25408 of strings between HEAD and TAIL. START is the index of the first
25409 glyph in row area AREA of glyph row ROW that is part of the new
25410 glyph string. END is the index of the last glyph in that glyph row
25411 area. X is the current output position assigned to the new glyph
25412 string constructed. HL overrides that face of the glyph; e.g. it
25413 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25414 right-most x-position of the drawing area. */
25416 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25417 do \
25419 int face_id; \
25420 XChar2b *char2b; \
25422 face_id = (row)->glyphs[area][START].face_id; \
25424 s = alloca (sizeof *s); \
25425 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25426 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25427 append_glyph_string (&HEAD, &TAIL, s); \
25428 s->x = (X); \
25429 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25431 while (false)
25434 /* Add a glyph string for a composite sequence to the list of strings
25435 between HEAD and TAIL. START is the index of the first glyph in
25436 row area AREA of glyph row ROW that is part of the new glyph
25437 string. END is the index of the last glyph in that glyph row area.
25438 X is the current output position assigned to the new glyph string
25439 constructed. HL overrides that face of the glyph; e.g. it is
25440 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25441 x-position of the drawing area. */
25443 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25444 do { \
25445 int face_id = (row)->glyphs[area][START].face_id; \
25446 struct face *base_face = FACE_FROM_ID (f, face_id); \
25447 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25448 struct composition *cmp = composition_table[cmp_id]; \
25449 XChar2b *char2b; \
25450 struct glyph_string *first_s = NULL; \
25451 int n; \
25453 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25455 /* Make glyph_strings for each glyph sequence that is drawable by \
25456 the same face, and append them to HEAD/TAIL. */ \
25457 for (n = 0; n < cmp->glyph_len;) \
25459 s = alloca (sizeof *s); \
25460 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25461 append_glyph_string (&(HEAD), &(TAIL), s); \
25462 s->cmp = cmp; \
25463 s->cmp_from = n; \
25464 s->x = (X); \
25465 if (n == 0) \
25466 first_s = s; \
25467 n = fill_composite_glyph_string (s, base_face, overlaps); \
25470 ++START; \
25471 s = first_s; \
25472 } while (false)
25475 /* Add a glyph string for a glyph-string sequence to the list of strings
25476 between HEAD and TAIL. */
25478 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25479 do { \
25480 int face_id; \
25481 XChar2b *char2b; \
25482 Lisp_Object gstring; \
25484 face_id = (row)->glyphs[area][START].face_id; \
25485 gstring = (composition_gstring_from_id \
25486 ((row)->glyphs[area][START].u.cmp.id)); \
25487 s = alloca (sizeof *s); \
25488 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25489 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25490 append_glyph_string (&(HEAD), &(TAIL), s); \
25491 s->x = (X); \
25492 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25493 } while (false)
25496 /* Add a glyph string for a sequence of glyphless character's glyphs
25497 to the list of strings between HEAD and TAIL. The meanings of
25498 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25500 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25501 do \
25503 int face_id; \
25505 face_id = (row)->glyphs[area][START].face_id; \
25507 s = alloca (sizeof *s); \
25508 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25509 append_glyph_string (&HEAD, &TAIL, s); \
25510 s->x = (X); \
25511 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25512 overlaps); \
25514 while (false)
25517 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25518 of AREA of glyph row ROW on window W between indices START and END.
25519 HL overrides the face for drawing glyph strings, e.g. it is
25520 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25521 x-positions of the drawing area.
25523 This is an ugly monster macro construct because we must use alloca
25524 to allocate glyph strings (because draw_glyphs can be called
25525 asynchronously). */
25527 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25528 do \
25530 HEAD = TAIL = NULL; \
25531 while (START < END) \
25533 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25534 switch (first_glyph->type) \
25536 case CHAR_GLYPH: \
25537 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25538 HL, X, LAST_X); \
25539 break; \
25541 case COMPOSITE_GLYPH: \
25542 if (first_glyph->u.cmp.automatic) \
25543 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25544 HL, X, LAST_X); \
25545 else \
25546 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25547 HL, X, LAST_X); \
25548 break; \
25550 case STRETCH_GLYPH: \
25551 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25552 HL, X, LAST_X); \
25553 break; \
25555 case IMAGE_GLYPH: \
25556 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25557 HL, X, LAST_X); \
25558 break;
25560 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25561 case XWIDGET_GLYPH: \
25562 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25563 HL, X, LAST_X); \
25564 break;
25566 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25567 case GLYPHLESS_GLYPH: \
25568 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25569 HL, X, LAST_X); \
25570 break; \
25572 default: \
25573 emacs_abort (); \
25576 if (s) \
25578 set_glyph_string_background_width (s, START, LAST_X); \
25579 (X) += s->width; \
25582 } while (false)
25585 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25586 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25587 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25588 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25591 /* Draw glyphs between START and END in AREA of ROW on window W,
25592 starting at x-position X. X is relative to AREA in W. HL is a
25593 face-override with the following meaning:
25595 DRAW_NORMAL_TEXT draw normally
25596 DRAW_CURSOR draw in cursor face
25597 DRAW_MOUSE_FACE draw in mouse face.
25598 DRAW_INVERSE_VIDEO draw in mode line face
25599 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25600 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25602 If OVERLAPS is non-zero, draw only the foreground of characters and
25603 clip to the physical height of ROW. Non-zero value also defines
25604 the overlapping part to be drawn:
25606 OVERLAPS_PRED overlap with preceding rows
25607 OVERLAPS_SUCC overlap with succeeding rows
25608 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25609 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25611 Value is the x-position reached, relative to AREA of W. */
25613 static int
25614 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25615 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25616 enum draw_glyphs_face hl, int overlaps)
25618 struct glyph_string *head, *tail;
25619 struct glyph_string *s;
25620 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25621 int i, j, x_reached, last_x, area_left = 0;
25622 struct frame *f = XFRAME (WINDOW_FRAME (w));
25623 DECLARE_HDC (hdc);
25625 ALLOCATE_HDC (hdc, f);
25627 /* Let's rather be paranoid than getting a SEGV. */
25628 end = min (end, row->used[area]);
25629 start = clip_to_bounds (0, start, end);
25631 /* Translate X to frame coordinates. Set last_x to the right
25632 end of the drawing area. */
25633 if (row->full_width_p)
25635 /* X is relative to the left edge of W, without scroll bars
25636 or fringes. */
25637 area_left = WINDOW_LEFT_EDGE_X (w);
25638 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25639 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25641 else
25643 area_left = window_box_left (w, area);
25644 last_x = area_left + window_box_width (w, area);
25646 x += area_left;
25648 /* Build a doubly-linked list of glyph_string structures between
25649 head and tail from what we have to draw. Note that the macro
25650 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25651 the reason we use a separate variable `i'. */
25652 i = start;
25653 USE_SAFE_ALLOCA;
25654 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25655 if (tail)
25656 x_reached = tail->x + tail->background_width;
25657 else
25658 x_reached = x;
25660 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25661 the row, redraw some glyphs in front or following the glyph
25662 strings built above. */
25663 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25665 struct glyph_string *h, *t;
25666 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25667 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25668 bool check_mouse_face = false;
25669 int dummy_x = 0;
25671 /* If mouse highlighting is on, we may need to draw adjacent
25672 glyphs using mouse-face highlighting. */
25673 if (area == TEXT_AREA && row->mouse_face_p
25674 && hlinfo->mouse_face_beg_row >= 0
25675 && hlinfo->mouse_face_end_row >= 0)
25677 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25679 if (row_vpos >= hlinfo->mouse_face_beg_row
25680 && row_vpos <= hlinfo->mouse_face_end_row)
25682 check_mouse_face = true;
25683 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25684 ? hlinfo->mouse_face_beg_col : 0;
25685 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25686 ? hlinfo->mouse_face_end_col
25687 : row->used[TEXT_AREA];
25691 /* Compute overhangs for all glyph strings. */
25692 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25693 for (s = head; s; s = s->next)
25694 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25696 /* Prepend glyph strings for glyphs in front of the first glyph
25697 string that are overwritten because of the first glyph
25698 string's left overhang. The background of all strings
25699 prepended must be drawn because the first glyph string
25700 draws over it. */
25701 i = left_overwritten (head);
25702 if (i >= 0)
25704 enum draw_glyphs_face overlap_hl;
25706 /* If this row contains mouse highlighting, attempt to draw
25707 the overlapped glyphs with the correct highlight. This
25708 code fails if the overlap encompasses more than one glyph
25709 and mouse-highlight spans only some of these glyphs.
25710 However, making it work perfectly involves a lot more
25711 code, and I don't know if the pathological case occurs in
25712 practice, so we'll stick to this for now. --- cyd */
25713 if (check_mouse_face
25714 && mouse_beg_col < start && mouse_end_col > i)
25715 overlap_hl = DRAW_MOUSE_FACE;
25716 else
25717 overlap_hl = DRAW_NORMAL_TEXT;
25719 if (hl != overlap_hl)
25720 clip_head = head;
25721 j = i;
25722 BUILD_GLYPH_STRINGS (j, start, h, t,
25723 overlap_hl, dummy_x, last_x);
25724 start = i;
25725 compute_overhangs_and_x (t, head->x, true);
25726 prepend_glyph_string_lists (&head, &tail, h, t);
25727 if (clip_head == NULL)
25728 clip_head = head;
25731 /* Prepend glyph strings for glyphs in front of the first glyph
25732 string that overwrite that glyph string because of their
25733 right overhang. For these strings, only the foreground must
25734 be drawn, because it draws over the glyph string at `head'.
25735 The background must not be drawn because this would overwrite
25736 right overhangs of preceding glyphs for which no glyph
25737 strings exist. */
25738 i = left_overwriting (head);
25739 if (i >= 0)
25741 enum draw_glyphs_face overlap_hl;
25743 if (check_mouse_face
25744 && mouse_beg_col < start && mouse_end_col > i)
25745 overlap_hl = DRAW_MOUSE_FACE;
25746 else
25747 overlap_hl = DRAW_NORMAL_TEXT;
25749 if (hl == overlap_hl || clip_head == NULL)
25750 clip_head = head;
25751 BUILD_GLYPH_STRINGS (i, start, h, t,
25752 overlap_hl, dummy_x, last_x);
25753 for (s = h; s; s = s->next)
25754 s->background_filled_p = true;
25755 compute_overhangs_and_x (t, head->x, true);
25756 prepend_glyph_string_lists (&head, &tail, h, t);
25759 /* Append glyphs strings for glyphs following the last glyph
25760 string tail that are overwritten by tail. The background of
25761 these strings has to be drawn because tail's foreground draws
25762 over it. */
25763 i = right_overwritten (tail);
25764 if (i >= 0)
25766 enum draw_glyphs_face overlap_hl;
25768 if (check_mouse_face
25769 && mouse_beg_col < i && mouse_end_col > end)
25770 overlap_hl = DRAW_MOUSE_FACE;
25771 else
25772 overlap_hl = DRAW_NORMAL_TEXT;
25774 if (hl != overlap_hl)
25775 clip_tail = tail;
25776 BUILD_GLYPH_STRINGS (end, i, h, t,
25777 overlap_hl, x, last_x);
25778 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25779 we don't have `end = i;' here. */
25780 compute_overhangs_and_x (h, tail->x + tail->width, false);
25781 append_glyph_string_lists (&head, &tail, h, t);
25782 if (clip_tail == NULL)
25783 clip_tail = tail;
25786 /* Append glyph strings for glyphs following the last glyph
25787 string tail that overwrite tail. The foreground of such
25788 glyphs has to be drawn because it writes into the background
25789 of tail. The background must not be drawn because it could
25790 paint over the foreground of following glyphs. */
25791 i = right_overwriting (tail);
25792 if (i >= 0)
25794 enum draw_glyphs_face overlap_hl;
25795 if (check_mouse_face
25796 && mouse_beg_col < i && mouse_end_col > end)
25797 overlap_hl = DRAW_MOUSE_FACE;
25798 else
25799 overlap_hl = DRAW_NORMAL_TEXT;
25801 if (hl == overlap_hl || clip_tail == NULL)
25802 clip_tail = tail;
25803 i++; /* We must include the Ith glyph. */
25804 BUILD_GLYPH_STRINGS (end, i, h, t,
25805 overlap_hl, x, last_x);
25806 for (s = h; s; s = s->next)
25807 s->background_filled_p = true;
25808 compute_overhangs_and_x (h, tail->x + tail->width, false);
25809 append_glyph_string_lists (&head, &tail, h, t);
25811 if (clip_head || clip_tail)
25812 for (s = head; s; s = s->next)
25814 s->clip_head = clip_head;
25815 s->clip_tail = clip_tail;
25819 /* Draw all strings. */
25820 for (s = head; s; s = s->next)
25821 FRAME_RIF (f)->draw_glyph_string (s);
25823 #ifndef HAVE_NS
25824 /* When focus a sole frame and move horizontally, this clears on_p
25825 causing a failure to erase prev cursor position. */
25826 if (area == TEXT_AREA
25827 && !row->full_width_p
25828 /* When drawing overlapping rows, only the glyph strings'
25829 foreground is drawn, which doesn't erase a cursor
25830 completely. */
25831 && !overlaps)
25833 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25834 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25835 : (tail ? tail->x + tail->background_width : x));
25836 x0 -= area_left;
25837 x1 -= area_left;
25839 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25840 row->y, MATRIX_ROW_BOTTOM_Y (row));
25842 #endif
25844 /* Value is the x-position up to which drawn, relative to AREA of W.
25845 This doesn't include parts drawn because of overhangs. */
25846 if (row->full_width_p)
25847 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25848 else
25849 x_reached -= area_left;
25851 RELEASE_HDC (hdc, f);
25853 SAFE_FREE ();
25854 return x_reached;
25857 /* Expand row matrix if too narrow. Don't expand if area
25858 is not present. */
25860 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25862 if (!it->f->fonts_changed \
25863 && (it->glyph_row->glyphs[area] \
25864 < it->glyph_row->glyphs[area + 1])) \
25866 it->w->ncols_scale_factor++; \
25867 it->f->fonts_changed = true; \
25871 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25872 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25874 static void
25875 append_glyph (struct it *it)
25877 struct glyph *glyph;
25878 enum glyph_row_area area = it->area;
25880 eassert (it->glyph_row);
25881 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25883 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25884 if (glyph < it->glyph_row->glyphs[area + 1])
25886 /* If the glyph row is reversed, we need to prepend the glyph
25887 rather than append it. */
25888 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25890 struct glyph *g;
25892 /* Make room for the additional glyph. */
25893 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25894 g[1] = *g;
25895 glyph = it->glyph_row->glyphs[area];
25897 glyph->charpos = CHARPOS (it->position);
25898 glyph->object = it->object;
25899 if (it->pixel_width > 0)
25901 eassert (it->pixel_width <= SHRT_MAX);
25902 glyph->pixel_width = it->pixel_width;
25903 glyph->padding_p = false;
25905 else
25907 /* Assure at least 1-pixel width. Otherwise, cursor can't
25908 be displayed correctly. */
25909 glyph->pixel_width = 1;
25910 glyph->padding_p = true;
25912 glyph->ascent = it->ascent;
25913 glyph->descent = it->descent;
25914 glyph->voffset = it->voffset;
25915 glyph->type = CHAR_GLYPH;
25916 glyph->avoid_cursor_p = it->avoid_cursor_p;
25917 glyph->multibyte_p = it->multibyte_p;
25918 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25920 /* In R2L rows, the left and the right box edges need to be
25921 drawn in reverse direction. */
25922 glyph->right_box_line_p = it->start_of_box_run_p;
25923 glyph->left_box_line_p = it->end_of_box_run_p;
25925 else
25927 glyph->left_box_line_p = it->start_of_box_run_p;
25928 glyph->right_box_line_p = it->end_of_box_run_p;
25930 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25931 || it->phys_descent > it->descent);
25932 glyph->glyph_not_available_p = it->glyph_not_available_p;
25933 glyph->face_id = it->face_id;
25934 glyph->u.ch = it->char_to_display;
25935 glyph->slice.img = null_glyph_slice;
25936 glyph->font_type = FONT_TYPE_UNKNOWN;
25937 if (it->bidi_p)
25939 glyph->resolved_level = it->bidi_it.resolved_level;
25940 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25941 glyph->bidi_type = it->bidi_it.type;
25943 else
25945 glyph->resolved_level = 0;
25946 glyph->bidi_type = UNKNOWN_BT;
25948 ++it->glyph_row->used[area];
25950 else
25951 IT_EXPAND_MATRIX_WIDTH (it, area);
25954 /* Store one glyph for the composition IT->cmp_it.id in
25955 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25956 non-null. */
25958 static void
25959 append_composite_glyph (struct it *it)
25961 struct glyph *glyph;
25962 enum glyph_row_area area = it->area;
25964 eassert (it->glyph_row);
25966 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25967 if (glyph < it->glyph_row->glyphs[area + 1])
25969 /* If the glyph row is reversed, we need to prepend the glyph
25970 rather than append it. */
25971 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25973 struct glyph *g;
25975 /* Make room for the new glyph. */
25976 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25977 g[1] = *g;
25978 glyph = it->glyph_row->glyphs[it->area];
25980 glyph->charpos = it->cmp_it.charpos;
25981 glyph->object = it->object;
25982 eassert (it->pixel_width <= SHRT_MAX);
25983 glyph->pixel_width = it->pixel_width;
25984 glyph->ascent = it->ascent;
25985 glyph->descent = it->descent;
25986 glyph->voffset = it->voffset;
25987 glyph->type = COMPOSITE_GLYPH;
25988 if (it->cmp_it.ch < 0)
25990 glyph->u.cmp.automatic = false;
25991 glyph->u.cmp.id = it->cmp_it.id;
25992 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25994 else
25996 glyph->u.cmp.automatic = true;
25997 glyph->u.cmp.id = it->cmp_it.id;
25998 glyph->slice.cmp.from = it->cmp_it.from;
25999 glyph->slice.cmp.to = it->cmp_it.to - 1;
26001 glyph->avoid_cursor_p = it->avoid_cursor_p;
26002 glyph->multibyte_p = it->multibyte_p;
26003 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26005 /* In R2L rows, the left and the right box edges need to be
26006 drawn in reverse direction. */
26007 glyph->right_box_line_p = it->start_of_box_run_p;
26008 glyph->left_box_line_p = it->end_of_box_run_p;
26010 else
26012 glyph->left_box_line_p = it->start_of_box_run_p;
26013 glyph->right_box_line_p = it->end_of_box_run_p;
26015 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26016 || it->phys_descent > it->descent);
26017 glyph->padding_p = false;
26018 glyph->glyph_not_available_p = false;
26019 glyph->face_id = it->face_id;
26020 glyph->font_type = FONT_TYPE_UNKNOWN;
26021 if (it->bidi_p)
26023 glyph->resolved_level = it->bidi_it.resolved_level;
26024 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26025 glyph->bidi_type = it->bidi_it.type;
26027 ++it->glyph_row->used[area];
26029 else
26030 IT_EXPAND_MATRIX_WIDTH (it, area);
26034 /* Change IT->ascent and IT->height according to the setting of
26035 IT->voffset. */
26037 static void
26038 take_vertical_position_into_account (struct it *it)
26040 if (it->voffset)
26042 if (it->voffset < 0)
26043 /* Increase the ascent so that we can display the text higher
26044 in the line. */
26045 it->ascent -= it->voffset;
26046 else
26047 /* Increase the descent so that we can display the text lower
26048 in the line. */
26049 it->descent += it->voffset;
26054 /* Produce glyphs/get display metrics for the image IT is loaded with.
26055 See the description of struct display_iterator in dispextern.h for
26056 an overview of struct display_iterator. */
26058 static void
26059 produce_image_glyph (struct it *it)
26061 struct image *img;
26062 struct face *face;
26063 int glyph_ascent, crop;
26064 struct glyph_slice slice;
26066 eassert (it->what == IT_IMAGE);
26068 face = FACE_FROM_ID (it->f, it->face_id);
26069 /* Make sure X resources of the face is loaded. */
26070 prepare_face_for_display (it->f, face);
26072 if (it->image_id < 0)
26074 /* Fringe bitmap. */
26075 it->ascent = it->phys_ascent = 0;
26076 it->descent = it->phys_descent = 0;
26077 it->pixel_width = 0;
26078 it->nglyphs = 0;
26079 return;
26082 img = IMAGE_FROM_ID (it->f, it->image_id);
26083 /* Make sure X resources of the image is loaded. */
26084 prepare_image_for_display (it->f, img);
26086 slice.x = slice.y = 0;
26087 slice.width = img->width;
26088 slice.height = img->height;
26090 if (INTEGERP (it->slice.x))
26091 slice.x = XINT (it->slice.x);
26092 else if (FLOATP (it->slice.x))
26093 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26095 if (INTEGERP (it->slice.y))
26096 slice.y = XINT (it->slice.y);
26097 else if (FLOATP (it->slice.y))
26098 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26100 if (INTEGERP (it->slice.width))
26101 slice.width = XINT (it->slice.width);
26102 else if (FLOATP (it->slice.width))
26103 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26105 if (INTEGERP (it->slice.height))
26106 slice.height = XINT (it->slice.height);
26107 else if (FLOATP (it->slice.height))
26108 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26110 if (slice.x >= img->width)
26111 slice.x = img->width;
26112 if (slice.y >= img->height)
26113 slice.y = img->height;
26114 if (slice.x + slice.width >= img->width)
26115 slice.width = img->width - slice.x;
26116 if (slice.y + slice.height > img->height)
26117 slice.height = img->height - slice.y;
26119 if (slice.width == 0 || slice.height == 0)
26120 return;
26122 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26124 it->descent = slice.height - glyph_ascent;
26125 if (slice.y == 0)
26126 it->descent += img->vmargin;
26127 if (slice.y + slice.height == img->height)
26128 it->descent += img->vmargin;
26129 it->phys_descent = it->descent;
26131 it->pixel_width = slice.width;
26132 if (slice.x == 0)
26133 it->pixel_width += img->hmargin;
26134 if (slice.x + slice.width == img->width)
26135 it->pixel_width += img->hmargin;
26137 /* It's quite possible for images to have an ascent greater than
26138 their height, so don't get confused in that case. */
26139 if (it->descent < 0)
26140 it->descent = 0;
26142 it->nglyphs = 1;
26144 if (face->box != FACE_NO_BOX)
26146 if (face->box_line_width > 0)
26148 if (slice.y == 0)
26149 it->ascent += face->box_line_width;
26150 if (slice.y + slice.height == img->height)
26151 it->descent += face->box_line_width;
26154 if (it->start_of_box_run_p && slice.x == 0)
26155 it->pixel_width += eabs (face->box_line_width);
26156 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26157 it->pixel_width += eabs (face->box_line_width);
26160 take_vertical_position_into_account (it);
26162 /* Automatically crop wide image glyphs at right edge so we can
26163 draw the cursor on same display row. */
26164 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26165 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26167 it->pixel_width -= crop;
26168 slice.width -= crop;
26171 if (it->glyph_row)
26173 struct glyph *glyph;
26174 enum glyph_row_area area = it->area;
26176 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26177 if (it->glyph_row->reversed_p)
26179 struct glyph *g;
26181 /* Make room for the new glyph. */
26182 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26183 g[1] = *g;
26184 glyph = it->glyph_row->glyphs[it->area];
26186 if (glyph < it->glyph_row->glyphs[area + 1])
26188 glyph->charpos = CHARPOS (it->position);
26189 glyph->object = it->object;
26190 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26191 glyph->ascent = glyph_ascent;
26192 glyph->descent = it->descent;
26193 glyph->voffset = it->voffset;
26194 glyph->type = IMAGE_GLYPH;
26195 glyph->avoid_cursor_p = it->avoid_cursor_p;
26196 glyph->multibyte_p = it->multibyte_p;
26197 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26199 /* In R2L rows, the left and the right box edges need to be
26200 drawn in reverse direction. */
26201 glyph->right_box_line_p = it->start_of_box_run_p;
26202 glyph->left_box_line_p = it->end_of_box_run_p;
26204 else
26206 glyph->left_box_line_p = it->start_of_box_run_p;
26207 glyph->right_box_line_p = it->end_of_box_run_p;
26209 glyph->overlaps_vertically_p = false;
26210 glyph->padding_p = false;
26211 glyph->glyph_not_available_p = false;
26212 glyph->face_id = it->face_id;
26213 glyph->u.img_id = img->id;
26214 glyph->slice.img = slice;
26215 glyph->font_type = FONT_TYPE_UNKNOWN;
26216 if (it->bidi_p)
26218 glyph->resolved_level = it->bidi_it.resolved_level;
26219 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26220 glyph->bidi_type = it->bidi_it.type;
26222 ++it->glyph_row->used[area];
26224 else
26225 IT_EXPAND_MATRIX_WIDTH (it, area);
26229 static void
26230 produce_xwidget_glyph (struct it *it)
26232 #ifdef HAVE_XWIDGETS
26233 struct xwidget *xw;
26234 int glyph_ascent, crop;
26235 eassert (it->what == IT_XWIDGET);
26237 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26238 /* Make sure X resources of the face is loaded. */
26239 prepare_face_for_display (it->f, face);
26241 xw = it->xwidget;
26242 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26243 it->descent = xw->height/2;
26244 it->phys_descent = it->descent;
26245 it->pixel_width = xw->width;
26246 /* It's quite possible for images to have an ascent greater than
26247 their height, so don't get confused in that case. */
26248 if (it->descent < 0)
26249 it->descent = 0;
26251 it->nglyphs = 1;
26253 if (face->box != FACE_NO_BOX)
26255 if (face->box_line_width > 0)
26257 it->ascent += face->box_line_width;
26258 it->descent += face->box_line_width;
26261 if (it->start_of_box_run_p)
26262 it->pixel_width += eabs (face->box_line_width);
26263 it->pixel_width += eabs (face->box_line_width);
26266 take_vertical_position_into_account (it);
26268 /* Automatically crop wide image glyphs at right edge so we can
26269 draw the cursor on same display row. */
26270 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26271 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26272 it->pixel_width -= crop;
26274 if (it->glyph_row)
26276 enum glyph_row_area area = it->area;
26277 struct glyph *glyph
26278 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26280 if (it->glyph_row->reversed_p)
26282 struct glyph *g;
26284 /* Make room for the new glyph. */
26285 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26286 g[1] = *g;
26287 glyph = it->glyph_row->glyphs[it->area];
26289 if (glyph < it->glyph_row->glyphs[area + 1])
26291 glyph->charpos = CHARPOS (it->position);
26292 glyph->object = it->object;
26293 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26294 glyph->ascent = glyph_ascent;
26295 glyph->descent = it->descent;
26296 glyph->voffset = it->voffset;
26297 glyph->type = XWIDGET_GLYPH;
26298 glyph->avoid_cursor_p = it->avoid_cursor_p;
26299 glyph->multibyte_p = it->multibyte_p;
26300 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26302 /* In R2L rows, the left and the right box edges need to be
26303 drawn in reverse direction. */
26304 glyph->right_box_line_p = it->start_of_box_run_p;
26305 glyph->left_box_line_p = it->end_of_box_run_p;
26307 else
26309 glyph->left_box_line_p = it->start_of_box_run_p;
26310 glyph->right_box_line_p = it->end_of_box_run_p;
26312 glyph->overlaps_vertically_p = 0;
26313 glyph->padding_p = 0;
26314 glyph->glyph_not_available_p = 0;
26315 glyph->face_id = it->face_id;
26316 glyph->u.xwidget = it->xwidget;
26317 glyph->font_type = FONT_TYPE_UNKNOWN;
26318 if (it->bidi_p)
26320 glyph->resolved_level = it->bidi_it.resolved_level;
26321 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26322 glyph->bidi_type = it->bidi_it.type;
26324 ++it->glyph_row->used[area];
26326 else
26327 IT_EXPAND_MATRIX_WIDTH (it, area);
26329 #endif
26332 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26333 of the glyph, WIDTH and HEIGHT are the width and height of the
26334 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26336 static void
26337 append_stretch_glyph (struct it *it, Lisp_Object object,
26338 int width, int height, int ascent)
26340 struct glyph *glyph;
26341 enum glyph_row_area area = it->area;
26343 eassert (ascent >= 0 && ascent <= height);
26345 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26346 if (glyph < it->glyph_row->glyphs[area + 1])
26348 /* If the glyph row is reversed, we need to prepend the glyph
26349 rather than append it. */
26350 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26352 struct glyph *g;
26354 /* Make room for the additional glyph. */
26355 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26356 g[1] = *g;
26357 glyph = it->glyph_row->glyphs[area];
26359 /* Decrease the width of the first glyph of the row that
26360 begins before first_visible_x (e.g., due to hscroll).
26361 This is so the overall width of the row becomes smaller
26362 by the scroll amount, and the stretch glyph appended by
26363 extend_face_to_end_of_line will be wider, to shift the
26364 row glyphs to the right. (In L2R rows, the corresponding
26365 left-shift effect is accomplished by setting row->x to a
26366 negative value, which won't work with R2L rows.)
26368 This must leave us with a positive value of WIDTH, since
26369 otherwise the call to move_it_in_display_line_to at the
26370 beginning of display_line would have got past the entire
26371 first glyph, and then it->current_x would have been
26372 greater or equal to it->first_visible_x. */
26373 if (it->current_x < it->first_visible_x)
26374 width -= it->first_visible_x - it->current_x;
26375 eassert (width > 0);
26377 glyph->charpos = CHARPOS (it->position);
26378 glyph->object = object;
26379 /* FIXME: It would be better to use TYPE_MAX here, but
26380 __typeof__ is not portable enough... */
26381 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26382 glyph->ascent = ascent;
26383 glyph->descent = height - ascent;
26384 glyph->voffset = it->voffset;
26385 glyph->type = STRETCH_GLYPH;
26386 glyph->avoid_cursor_p = it->avoid_cursor_p;
26387 glyph->multibyte_p = it->multibyte_p;
26388 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26390 /* In R2L rows, the left and the right box edges need to be
26391 drawn in reverse direction. */
26392 glyph->right_box_line_p = it->start_of_box_run_p;
26393 glyph->left_box_line_p = it->end_of_box_run_p;
26395 else
26397 glyph->left_box_line_p = it->start_of_box_run_p;
26398 glyph->right_box_line_p = it->end_of_box_run_p;
26400 glyph->overlaps_vertically_p = false;
26401 glyph->padding_p = false;
26402 glyph->glyph_not_available_p = false;
26403 glyph->face_id = it->face_id;
26404 glyph->u.stretch.ascent = ascent;
26405 glyph->u.stretch.height = height;
26406 glyph->slice.img = null_glyph_slice;
26407 glyph->font_type = FONT_TYPE_UNKNOWN;
26408 if (it->bidi_p)
26410 glyph->resolved_level = it->bidi_it.resolved_level;
26411 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26412 glyph->bidi_type = it->bidi_it.type;
26414 else
26416 glyph->resolved_level = 0;
26417 glyph->bidi_type = UNKNOWN_BT;
26419 ++it->glyph_row->used[area];
26421 else
26422 IT_EXPAND_MATRIX_WIDTH (it, area);
26425 #endif /* HAVE_WINDOW_SYSTEM */
26427 /* Produce a stretch glyph for iterator IT. IT->object is the value
26428 of the glyph property displayed. The value must be a list
26429 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26430 being recognized:
26432 1. `:width WIDTH' specifies that the space should be WIDTH *
26433 canonical char width wide. WIDTH may be an integer or floating
26434 point number.
26436 2. `:relative-width FACTOR' specifies that the width of the stretch
26437 should be computed from the width of the first character having the
26438 `glyph' property, and should be FACTOR times that width.
26440 3. `:align-to HPOS' specifies that the space should be wide enough
26441 to reach HPOS, a value in canonical character units.
26443 Exactly one of the above pairs must be present.
26445 4. `:height HEIGHT' specifies that the height of the stretch produced
26446 should be HEIGHT, measured in canonical character units.
26448 5. `:relative-height FACTOR' specifies that the height of the
26449 stretch should be FACTOR times the height of the characters having
26450 the glyph property.
26452 Either none or exactly one of 4 or 5 must be present.
26454 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26455 of the stretch should be used for the ascent of the stretch.
26456 ASCENT must be in the range 0 <= ASCENT <= 100. */
26458 void
26459 produce_stretch_glyph (struct it *it)
26461 /* (space :width WIDTH :height HEIGHT ...) */
26462 Lisp_Object prop, plist;
26463 int width = 0, height = 0, align_to = -1;
26464 bool zero_width_ok_p = false;
26465 double tem;
26466 struct font *font = NULL;
26468 #ifdef HAVE_WINDOW_SYSTEM
26469 int ascent = 0;
26470 bool zero_height_ok_p = false;
26472 if (FRAME_WINDOW_P (it->f))
26474 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26475 font = face->font ? face->font : FRAME_FONT (it->f);
26476 prepare_face_for_display (it->f, face);
26478 #endif
26480 /* List should start with `space'. */
26481 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26482 plist = XCDR (it->object);
26484 /* Compute the width of the stretch. */
26485 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26486 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26488 /* Absolute width `:width WIDTH' specified and valid. */
26489 zero_width_ok_p = true;
26490 width = (int)tem;
26492 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26494 /* Relative width `:relative-width FACTOR' specified and valid.
26495 Compute the width of the characters having the `glyph'
26496 property. */
26497 struct it it2;
26498 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26500 it2 = *it;
26501 if (it->multibyte_p)
26502 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26503 else
26505 it2.c = it2.char_to_display = *p, it2.len = 1;
26506 if (! ASCII_CHAR_P (it2.c))
26507 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26510 it2.glyph_row = NULL;
26511 it2.what = IT_CHARACTER;
26512 PRODUCE_GLYPHS (&it2);
26513 width = NUMVAL (prop) * it2.pixel_width;
26515 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26516 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26517 &align_to))
26519 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26520 align_to = (align_to < 0
26522 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26523 else if (align_to < 0)
26524 align_to = window_box_left_offset (it->w, TEXT_AREA);
26525 width = max (0, (int)tem + align_to - it->current_x);
26526 zero_width_ok_p = true;
26528 else
26529 /* Nothing specified -> width defaults to canonical char width. */
26530 width = FRAME_COLUMN_WIDTH (it->f);
26532 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26533 width = 1;
26535 #ifdef HAVE_WINDOW_SYSTEM
26536 /* Compute height. */
26537 if (FRAME_WINDOW_P (it->f))
26539 int default_height = normal_char_height (font, ' ');
26541 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26542 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26544 height = (int)tem;
26545 zero_height_ok_p = true;
26547 else if (prop = Fplist_get (plist, QCrelative_height),
26548 NUMVAL (prop) > 0)
26549 height = default_height * NUMVAL (prop);
26550 else
26551 height = default_height;
26553 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26554 height = 1;
26556 /* Compute percentage of height used for ascent. If
26557 `:ascent ASCENT' is present and valid, use that. Otherwise,
26558 derive the ascent from the font in use. */
26559 if (prop = Fplist_get (plist, QCascent),
26560 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26561 ascent = height * NUMVAL (prop) / 100.0;
26562 else if (!NILP (prop)
26563 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26564 ascent = min (max (0, (int)tem), height);
26565 else
26566 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26568 else
26569 #endif /* HAVE_WINDOW_SYSTEM */
26570 height = 1;
26572 if (width > 0 && it->line_wrap != TRUNCATE
26573 && it->current_x + width > it->last_visible_x)
26575 width = it->last_visible_x - it->current_x;
26576 #ifdef HAVE_WINDOW_SYSTEM
26577 /* Subtract one more pixel from the stretch width, but only on
26578 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26579 width -= FRAME_WINDOW_P (it->f);
26580 #endif
26583 if (width > 0 && height > 0 && it->glyph_row)
26585 Lisp_Object o_object = it->object;
26586 Lisp_Object object = it->stack[it->sp - 1].string;
26587 int n = width;
26589 if (!STRINGP (object))
26590 object = it->w->contents;
26591 #ifdef HAVE_WINDOW_SYSTEM
26592 if (FRAME_WINDOW_P (it->f))
26593 append_stretch_glyph (it, object, width, height, ascent);
26594 else
26595 #endif
26597 it->object = object;
26598 it->char_to_display = ' ';
26599 it->pixel_width = it->len = 1;
26600 while (n--)
26601 tty_append_glyph (it);
26602 it->object = o_object;
26606 it->pixel_width = width;
26607 #ifdef HAVE_WINDOW_SYSTEM
26608 if (FRAME_WINDOW_P (it->f))
26610 it->ascent = it->phys_ascent = ascent;
26611 it->descent = it->phys_descent = height - it->ascent;
26612 it->nglyphs = width > 0 && height > 0;
26613 take_vertical_position_into_account (it);
26615 else
26616 #endif
26617 it->nglyphs = width;
26620 /* Get information about special display element WHAT in an
26621 environment described by IT. WHAT is one of IT_TRUNCATION or
26622 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26623 non-null glyph_row member. This function ensures that fields like
26624 face_id, c, len of IT are left untouched. */
26626 static void
26627 produce_special_glyphs (struct it *it, enum display_element_type what)
26629 struct it temp_it;
26630 Lisp_Object gc;
26631 GLYPH glyph;
26633 temp_it = *it;
26634 temp_it.object = Qnil;
26635 memset (&temp_it.current, 0, sizeof temp_it.current);
26637 if (what == IT_CONTINUATION)
26639 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26640 if (it->bidi_it.paragraph_dir == R2L)
26641 SET_GLYPH_FROM_CHAR (glyph, '/');
26642 else
26643 SET_GLYPH_FROM_CHAR (glyph, '\\');
26644 if (it->dp
26645 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26647 /* FIXME: Should we mirror GC for R2L lines? */
26648 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26649 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26652 else if (what == IT_TRUNCATION)
26654 /* Truncation glyph. */
26655 SET_GLYPH_FROM_CHAR (glyph, '$');
26656 if (it->dp
26657 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26659 /* FIXME: Should we mirror GC for R2L lines? */
26660 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26661 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26664 else
26665 emacs_abort ();
26667 #ifdef HAVE_WINDOW_SYSTEM
26668 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26669 is turned off, we precede the truncation/continuation glyphs by a
26670 stretch glyph whose width is computed such that these special
26671 glyphs are aligned at the window margin, even when very different
26672 fonts are used in different glyph rows. */
26673 if (FRAME_WINDOW_P (temp_it.f)
26674 /* init_iterator calls this with it->glyph_row == NULL, and it
26675 wants only the pixel width of the truncation/continuation
26676 glyphs. */
26677 && temp_it.glyph_row
26678 /* insert_left_trunc_glyphs calls us at the beginning of the
26679 row, and it has its own calculation of the stretch glyph
26680 width. */
26681 && temp_it.glyph_row->used[TEXT_AREA] > 0
26682 && (temp_it.glyph_row->reversed_p
26683 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26684 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26686 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26688 if (stretch_width > 0)
26690 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26691 struct font *font =
26692 face->font ? face->font : FRAME_FONT (temp_it.f);
26693 int stretch_ascent =
26694 (((temp_it.ascent + temp_it.descent)
26695 * FONT_BASE (font)) / FONT_HEIGHT (font));
26697 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26698 temp_it.ascent + temp_it.descent,
26699 stretch_ascent);
26702 #endif
26704 temp_it.dp = NULL;
26705 temp_it.what = IT_CHARACTER;
26706 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26707 temp_it.face_id = GLYPH_FACE (glyph);
26708 temp_it.len = CHAR_BYTES (temp_it.c);
26710 PRODUCE_GLYPHS (&temp_it);
26711 it->pixel_width = temp_it.pixel_width;
26712 it->nglyphs = temp_it.nglyphs;
26715 #ifdef HAVE_WINDOW_SYSTEM
26717 /* Calculate line-height and line-spacing properties.
26718 An integer value specifies explicit pixel value.
26719 A float value specifies relative value to current face height.
26720 A cons (float . face-name) specifies relative value to
26721 height of specified face font.
26723 Returns height in pixels, or nil. */
26725 static Lisp_Object
26726 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26727 int boff, bool override)
26729 Lisp_Object face_name = Qnil;
26730 int ascent, descent, height;
26732 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26733 return val;
26735 if (CONSP (val))
26737 face_name = XCAR (val);
26738 val = XCDR (val);
26739 if (!NUMBERP (val))
26740 val = make_number (1);
26741 if (NILP (face_name))
26743 height = it->ascent + it->descent;
26744 goto scale;
26748 if (NILP (face_name))
26750 font = FRAME_FONT (it->f);
26751 boff = FRAME_BASELINE_OFFSET (it->f);
26753 else if (EQ (face_name, Qt))
26755 override = false;
26757 else
26759 int face_id;
26760 struct face *face;
26762 face_id = lookup_named_face (it->f, face_name, false);
26763 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26764 if (face == NULL || ((font = face->font) == NULL))
26765 return make_number (-1);
26766 boff = font->baseline_offset;
26767 if (font->vertical_centering)
26768 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26771 normal_char_ascent_descent (font, -1, &ascent, &descent);
26773 if (override)
26775 it->override_ascent = ascent;
26776 it->override_descent = descent;
26777 it->override_boff = boff;
26780 height = ascent + descent;
26782 scale:
26783 if (FLOATP (val))
26784 height = (int)(XFLOAT_DATA (val) * height);
26785 else if (INTEGERP (val))
26786 height *= XINT (val);
26788 return make_number (height);
26792 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26793 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26794 and only if this is for a character for which no font was found.
26796 If the display method (it->glyphless_method) is
26797 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26798 length of the acronym or the hexadecimal string, UPPER_XOFF and
26799 UPPER_YOFF are pixel offsets for the upper part of the string,
26800 LOWER_XOFF and LOWER_YOFF are for the lower part.
26802 For the other display methods, LEN through LOWER_YOFF are zero. */
26804 static void
26805 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26806 short upper_xoff, short upper_yoff,
26807 short lower_xoff, short lower_yoff)
26809 struct glyph *glyph;
26810 enum glyph_row_area area = it->area;
26812 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26813 if (glyph < it->glyph_row->glyphs[area + 1])
26815 /* If the glyph row is reversed, we need to prepend the glyph
26816 rather than append it. */
26817 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26819 struct glyph *g;
26821 /* Make room for the additional glyph. */
26822 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26823 g[1] = *g;
26824 glyph = it->glyph_row->glyphs[area];
26826 glyph->charpos = CHARPOS (it->position);
26827 glyph->object = it->object;
26828 eassert (it->pixel_width <= SHRT_MAX);
26829 glyph->pixel_width = it->pixel_width;
26830 glyph->ascent = it->ascent;
26831 glyph->descent = it->descent;
26832 glyph->voffset = it->voffset;
26833 glyph->type = GLYPHLESS_GLYPH;
26834 glyph->u.glyphless.method = it->glyphless_method;
26835 glyph->u.glyphless.for_no_font = for_no_font;
26836 glyph->u.glyphless.len = len;
26837 glyph->u.glyphless.ch = it->c;
26838 glyph->slice.glyphless.upper_xoff = upper_xoff;
26839 glyph->slice.glyphless.upper_yoff = upper_yoff;
26840 glyph->slice.glyphless.lower_xoff = lower_xoff;
26841 glyph->slice.glyphless.lower_yoff = lower_yoff;
26842 glyph->avoid_cursor_p = it->avoid_cursor_p;
26843 glyph->multibyte_p = it->multibyte_p;
26844 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26846 /* In R2L rows, the left and the right box edges need to be
26847 drawn in reverse direction. */
26848 glyph->right_box_line_p = it->start_of_box_run_p;
26849 glyph->left_box_line_p = it->end_of_box_run_p;
26851 else
26853 glyph->left_box_line_p = it->start_of_box_run_p;
26854 glyph->right_box_line_p = it->end_of_box_run_p;
26856 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26857 || it->phys_descent > it->descent);
26858 glyph->padding_p = false;
26859 glyph->glyph_not_available_p = false;
26860 glyph->face_id = face_id;
26861 glyph->font_type = FONT_TYPE_UNKNOWN;
26862 if (it->bidi_p)
26864 glyph->resolved_level = it->bidi_it.resolved_level;
26865 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26866 glyph->bidi_type = it->bidi_it.type;
26868 ++it->glyph_row->used[area];
26870 else
26871 IT_EXPAND_MATRIX_WIDTH (it, area);
26875 /* Produce a glyph for a glyphless character for iterator IT.
26876 IT->glyphless_method specifies which method to use for displaying
26877 the character. See the description of enum
26878 glyphless_display_method in dispextern.h for the detail.
26880 FOR_NO_FONT is true if and only if this is for a character for
26881 which no font was found. ACRONYM, if non-nil, is an acronym string
26882 for the character. */
26884 static void
26885 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26887 int face_id;
26888 struct face *face;
26889 struct font *font;
26890 int base_width, base_height, width, height;
26891 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26892 int len;
26894 /* Get the metrics of the base font. We always refer to the current
26895 ASCII face. */
26896 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26897 font = face->font ? face->font : FRAME_FONT (it->f);
26898 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26899 it->ascent += font->baseline_offset;
26900 it->descent -= font->baseline_offset;
26901 base_height = it->ascent + it->descent;
26902 base_width = font->average_width;
26904 face_id = merge_glyphless_glyph_face (it);
26906 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26908 it->pixel_width = THIN_SPACE_WIDTH;
26909 len = 0;
26910 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26912 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26914 width = CHARACTER_WIDTH (it->c);
26915 if (width == 0)
26916 width = 1;
26917 else if (width > 4)
26918 width = 4;
26919 it->pixel_width = base_width * width;
26920 len = 0;
26921 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26923 else
26925 char buf[7];
26926 const char *str;
26927 unsigned int code[6];
26928 int upper_len;
26929 int ascent, descent;
26930 struct font_metrics metrics_upper, metrics_lower;
26932 face = FACE_FROM_ID (it->f, face_id);
26933 font = face->font ? face->font : FRAME_FONT (it->f);
26934 prepare_face_for_display (it->f, face);
26936 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26938 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26939 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26940 if (CONSP (acronym))
26941 acronym = XCAR (acronym);
26942 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26944 else
26946 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26947 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26948 str = buf;
26950 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26951 code[len] = font->driver->encode_char (font, str[len]);
26952 upper_len = (len + 1) / 2;
26953 font->driver->text_extents (font, code, upper_len,
26954 &metrics_upper);
26955 font->driver->text_extents (font, code + upper_len, len - upper_len,
26956 &metrics_lower);
26960 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26961 width = max (metrics_upper.width, metrics_lower.width) + 4;
26962 upper_xoff = upper_yoff = 2; /* the typical case */
26963 if (base_width >= width)
26965 /* Align the upper to the left, the lower to the right. */
26966 it->pixel_width = base_width;
26967 lower_xoff = base_width - 2 - metrics_lower.width;
26969 else
26971 /* Center the shorter one. */
26972 it->pixel_width = width;
26973 if (metrics_upper.width >= metrics_lower.width)
26974 lower_xoff = (width - metrics_lower.width) / 2;
26975 else
26977 /* FIXME: This code doesn't look right. It formerly was
26978 missing the "lower_xoff = 0;", which couldn't have
26979 been right since it left lower_xoff uninitialized. */
26980 lower_xoff = 0;
26981 upper_xoff = (width - metrics_upper.width) / 2;
26985 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26986 top, bottom, and between upper and lower strings. */
26987 height = (metrics_upper.ascent + metrics_upper.descent
26988 + metrics_lower.ascent + metrics_lower.descent) + 5;
26989 /* Center vertically.
26990 H:base_height, D:base_descent
26991 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26993 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26994 descent = D - H/2 + h/2;
26995 lower_yoff = descent - 2 - ld;
26996 upper_yoff = lower_yoff - la - 1 - ud; */
26997 ascent = - (it->descent - (base_height + height + 1) / 2);
26998 descent = it->descent - (base_height - height) / 2;
26999 lower_yoff = descent - 2 - metrics_lower.descent;
27000 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27001 - metrics_upper.descent);
27002 /* Don't make the height shorter than the base height. */
27003 if (height > base_height)
27005 it->ascent = ascent;
27006 it->descent = descent;
27010 it->phys_ascent = it->ascent;
27011 it->phys_descent = it->descent;
27012 if (it->glyph_row)
27013 append_glyphless_glyph (it, face_id, for_no_font, len,
27014 upper_xoff, upper_yoff,
27015 lower_xoff, lower_yoff);
27016 it->nglyphs = 1;
27017 take_vertical_position_into_account (it);
27021 /* RIF:
27022 Produce glyphs/get display metrics for the display element IT is
27023 loaded with. See the description of struct it in dispextern.h
27024 for an overview of struct it. */
27026 void
27027 x_produce_glyphs (struct it *it)
27029 int extra_line_spacing = it->extra_line_spacing;
27031 it->glyph_not_available_p = false;
27033 if (it->what == IT_CHARACTER)
27035 XChar2b char2b;
27036 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27037 struct font *font = face->font;
27038 struct font_metrics *pcm = NULL;
27039 int boff; /* Baseline offset. */
27041 if (font == NULL)
27043 /* When no suitable font is found, display this character by
27044 the method specified in the first extra slot of
27045 Vglyphless_char_display. */
27046 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27048 eassert (it->what == IT_GLYPHLESS);
27049 produce_glyphless_glyph (it, true,
27050 STRINGP (acronym) ? acronym : Qnil);
27051 goto done;
27054 boff = font->baseline_offset;
27055 if (font->vertical_centering)
27056 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27058 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27060 it->nglyphs = 1;
27062 if (it->override_ascent >= 0)
27064 it->ascent = it->override_ascent;
27065 it->descent = it->override_descent;
27066 boff = it->override_boff;
27068 else
27070 it->ascent = FONT_BASE (font) + boff;
27071 it->descent = FONT_DESCENT (font) - boff;
27074 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27076 pcm = get_per_char_metric (font, &char2b);
27077 if (pcm->width == 0
27078 && pcm->rbearing == 0 && pcm->lbearing == 0)
27079 pcm = NULL;
27082 if (pcm)
27084 it->phys_ascent = pcm->ascent + boff;
27085 it->phys_descent = pcm->descent - boff;
27086 it->pixel_width = pcm->width;
27087 /* Don't use font-global values for ascent and descent
27088 if they result in an exceedingly large line height. */
27089 if (it->override_ascent < 0)
27091 if (FONT_TOO_HIGH (font))
27093 it->ascent = it->phys_ascent;
27094 it->descent = it->phys_descent;
27095 /* These limitations are enforced by an
27096 assertion near the end of this function. */
27097 if (it->ascent < 0)
27098 it->ascent = 0;
27099 if (it->descent < 0)
27100 it->descent = 0;
27104 else
27106 it->glyph_not_available_p = true;
27107 it->phys_ascent = it->ascent;
27108 it->phys_descent = it->descent;
27109 it->pixel_width = font->space_width;
27112 if (it->constrain_row_ascent_descent_p)
27114 if (it->descent > it->max_descent)
27116 it->ascent += it->descent - it->max_descent;
27117 it->descent = it->max_descent;
27119 if (it->ascent > it->max_ascent)
27121 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27122 it->ascent = it->max_ascent;
27124 it->phys_ascent = min (it->phys_ascent, it->ascent);
27125 it->phys_descent = min (it->phys_descent, it->descent);
27126 extra_line_spacing = 0;
27129 /* If this is a space inside a region of text with
27130 `space-width' property, change its width. */
27131 bool stretched_p
27132 = it->char_to_display == ' ' && !NILP (it->space_width);
27133 if (stretched_p)
27134 it->pixel_width *= XFLOATINT (it->space_width);
27136 /* If face has a box, add the box thickness to the character
27137 height. If character has a box line to the left and/or
27138 right, add the box line width to the character's width. */
27139 if (face->box != FACE_NO_BOX)
27141 int thick = face->box_line_width;
27143 if (thick > 0)
27145 it->ascent += thick;
27146 it->descent += thick;
27148 else
27149 thick = -thick;
27151 if (it->start_of_box_run_p)
27152 it->pixel_width += thick;
27153 if (it->end_of_box_run_p)
27154 it->pixel_width += thick;
27157 /* If face has an overline, add the height of the overline
27158 (1 pixel) and a 1 pixel margin to the character height. */
27159 if (face->overline_p)
27160 it->ascent += overline_margin;
27162 if (it->constrain_row_ascent_descent_p)
27164 if (it->ascent > it->max_ascent)
27165 it->ascent = it->max_ascent;
27166 if (it->descent > it->max_descent)
27167 it->descent = it->max_descent;
27170 take_vertical_position_into_account (it);
27172 /* If we have to actually produce glyphs, do it. */
27173 if (it->glyph_row)
27175 if (stretched_p)
27177 /* Translate a space with a `space-width' property
27178 into a stretch glyph. */
27179 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27180 / FONT_HEIGHT (font));
27181 append_stretch_glyph (it, it->object, it->pixel_width,
27182 it->ascent + it->descent, ascent);
27184 else
27185 append_glyph (it);
27187 /* If characters with lbearing or rbearing are displayed
27188 in this line, record that fact in a flag of the
27189 glyph row. This is used to optimize X output code. */
27190 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27191 it->glyph_row->contains_overlapping_glyphs_p = true;
27193 if (! stretched_p && it->pixel_width == 0)
27194 /* We assure that all visible glyphs have at least 1-pixel
27195 width. */
27196 it->pixel_width = 1;
27198 else if (it->char_to_display == '\n')
27200 /* A newline has no width, but we need the height of the
27201 line. But if previous part of the line sets a height,
27202 don't increase that height. */
27204 Lisp_Object height;
27205 Lisp_Object total_height = Qnil;
27207 it->override_ascent = -1;
27208 it->pixel_width = 0;
27209 it->nglyphs = 0;
27211 height = get_it_property (it, Qline_height);
27212 /* Split (line-height total-height) list. */
27213 if (CONSP (height)
27214 && CONSP (XCDR (height))
27215 && NILP (XCDR (XCDR (height))))
27217 total_height = XCAR (XCDR (height));
27218 height = XCAR (height);
27220 height = calc_line_height_property (it, height, font, boff, true);
27222 if (it->override_ascent >= 0)
27224 it->ascent = it->override_ascent;
27225 it->descent = it->override_descent;
27226 boff = it->override_boff;
27228 else
27230 if (FONT_TOO_HIGH (font))
27232 it->ascent = font->pixel_size + boff - 1;
27233 it->descent = -boff + 1;
27234 if (it->descent < 0)
27235 it->descent = 0;
27237 else
27239 it->ascent = FONT_BASE (font) + boff;
27240 it->descent = FONT_DESCENT (font) - boff;
27244 if (EQ (height, Qt))
27246 if (it->descent > it->max_descent)
27248 it->ascent += it->descent - it->max_descent;
27249 it->descent = it->max_descent;
27251 if (it->ascent > it->max_ascent)
27253 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27254 it->ascent = it->max_ascent;
27256 it->phys_ascent = min (it->phys_ascent, it->ascent);
27257 it->phys_descent = min (it->phys_descent, it->descent);
27258 it->constrain_row_ascent_descent_p = true;
27259 extra_line_spacing = 0;
27261 else
27263 Lisp_Object spacing;
27265 it->phys_ascent = it->ascent;
27266 it->phys_descent = it->descent;
27268 if ((it->max_ascent > 0 || it->max_descent > 0)
27269 && face->box != FACE_NO_BOX
27270 && face->box_line_width > 0)
27272 it->ascent += face->box_line_width;
27273 it->descent += face->box_line_width;
27275 if (!NILP (height)
27276 && XINT (height) > it->ascent + it->descent)
27277 it->ascent = XINT (height) - it->descent;
27279 if (!NILP (total_height))
27280 spacing = calc_line_height_property (it, total_height, font,
27281 boff, false);
27282 else
27284 spacing = get_it_property (it, Qline_spacing);
27285 spacing = calc_line_height_property (it, spacing, font,
27286 boff, false);
27288 if (INTEGERP (spacing))
27290 extra_line_spacing = XINT (spacing);
27291 if (!NILP (total_height))
27292 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27296 else /* i.e. (it->char_to_display == '\t') */
27298 if (font->space_width > 0)
27300 int tab_width = it->tab_width * font->space_width;
27301 int x = it->current_x + it->continuation_lines_width;
27302 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27304 /* If the distance from the current position to the next tab
27305 stop is less than a space character width, use the
27306 tab stop after that. */
27307 if (next_tab_x - x < font->space_width)
27308 next_tab_x += tab_width;
27310 it->pixel_width = next_tab_x - x;
27311 it->nglyphs = 1;
27312 if (FONT_TOO_HIGH (font))
27314 if (get_char_glyph_code (' ', font, &char2b))
27316 pcm = get_per_char_metric (font, &char2b);
27317 if (pcm->width == 0
27318 && pcm->rbearing == 0 && pcm->lbearing == 0)
27319 pcm = NULL;
27322 if (pcm)
27324 it->ascent = pcm->ascent + boff;
27325 it->descent = pcm->descent - boff;
27327 else
27329 it->ascent = font->pixel_size + boff - 1;
27330 it->descent = -boff + 1;
27332 if (it->ascent < 0)
27333 it->ascent = 0;
27334 if (it->descent < 0)
27335 it->descent = 0;
27337 else
27339 it->ascent = FONT_BASE (font) + boff;
27340 it->descent = FONT_DESCENT (font) - boff;
27342 it->phys_ascent = it->ascent;
27343 it->phys_descent = it->descent;
27345 if (it->glyph_row)
27347 append_stretch_glyph (it, it->object, it->pixel_width,
27348 it->ascent + it->descent, it->ascent);
27351 else
27353 it->pixel_width = 0;
27354 it->nglyphs = 1;
27358 if (FONT_TOO_HIGH (font))
27360 int font_ascent, font_descent;
27362 /* For very large fonts, where we ignore the declared font
27363 dimensions, and go by per-character metrics instead,
27364 don't let the row ascent and descent values (and the row
27365 height computed from them) be smaller than the "normal"
27366 character metrics. This avoids unpleasant effects
27367 whereby lines on display would change their height
27368 depending on which characters are shown. */
27369 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27370 it->max_ascent = max (it->max_ascent, font_ascent);
27371 it->max_descent = max (it->max_descent, font_descent);
27374 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27376 /* A static composition.
27378 Note: A composition is represented as one glyph in the
27379 glyph matrix. There are no padding glyphs.
27381 Important note: pixel_width, ascent, and descent are the
27382 values of what is drawn by draw_glyphs (i.e. the values of
27383 the overall glyphs composed). */
27384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27385 int boff; /* baseline offset */
27386 struct composition *cmp = composition_table[it->cmp_it.id];
27387 int glyph_len = cmp->glyph_len;
27388 struct font *font = face->font;
27390 it->nglyphs = 1;
27392 /* If we have not yet calculated pixel size data of glyphs of
27393 the composition for the current face font, calculate them
27394 now. Theoretically, we have to check all fonts for the
27395 glyphs, but that requires much time and memory space. So,
27396 here we check only the font of the first glyph. This may
27397 lead to incorrect display, but it's very rare, and C-l
27398 (recenter-top-bottom) can correct the display anyway. */
27399 if (! cmp->font || cmp->font != font)
27401 /* Ascent and descent of the font of the first character
27402 of this composition (adjusted by baseline offset).
27403 Ascent and descent of overall glyphs should not be less
27404 than these, respectively. */
27405 int font_ascent, font_descent, font_height;
27406 /* Bounding box of the overall glyphs. */
27407 int leftmost, rightmost, lowest, highest;
27408 int lbearing, rbearing;
27409 int i, width, ascent, descent;
27410 int c;
27411 XChar2b char2b;
27412 struct font_metrics *pcm;
27413 ptrdiff_t pos;
27415 eassume (0 < glyph_len); /* See Bug#8512. */
27417 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27418 while (c == '\t' && 0 < --glyph_len);
27420 bool right_padded = glyph_len < cmp->glyph_len;
27421 for (i = 0; i < glyph_len; i++)
27423 c = COMPOSITION_GLYPH (cmp, i);
27424 if (c != '\t')
27425 break;
27426 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27428 bool left_padded = i > 0;
27430 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27431 : IT_CHARPOS (*it));
27432 /* If no suitable font is found, use the default font. */
27433 bool font_not_found_p = font == NULL;
27434 if (font_not_found_p)
27436 face = face->ascii_face;
27437 font = face->font;
27439 boff = font->baseline_offset;
27440 if (font->vertical_centering)
27441 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27442 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27443 font_ascent += boff;
27444 font_descent -= boff;
27445 font_height = font_ascent + font_descent;
27447 cmp->font = font;
27449 pcm = NULL;
27450 if (! font_not_found_p)
27452 get_char_face_and_encoding (it->f, c, it->face_id,
27453 &char2b, false);
27454 pcm = get_per_char_metric (font, &char2b);
27457 /* Initialize the bounding box. */
27458 if (pcm)
27460 width = cmp->glyph_len > 0 ? pcm->width : 0;
27461 ascent = pcm->ascent;
27462 descent = pcm->descent;
27463 lbearing = pcm->lbearing;
27464 rbearing = pcm->rbearing;
27466 else
27468 width = cmp->glyph_len > 0 ? font->space_width : 0;
27469 ascent = FONT_BASE (font);
27470 descent = FONT_DESCENT (font);
27471 lbearing = 0;
27472 rbearing = width;
27475 rightmost = width;
27476 leftmost = 0;
27477 lowest = - descent + boff;
27478 highest = ascent + boff;
27480 if (! font_not_found_p
27481 && font->default_ascent
27482 && CHAR_TABLE_P (Vuse_default_ascent)
27483 && !NILP (Faref (Vuse_default_ascent,
27484 make_number (it->char_to_display))))
27485 highest = font->default_ascent + boff;
27487 /* Draw the first glyph at the normal position. It may be
27488 shifted to right later if some other glyphs are drawn
27489 at the left. */
27490 cmp->offsets[i * 2] = 0;
27491 cmp->offsets[i * 2 + 1] = boff;
27492 cmp->lbearing = lbearing;
27493 cmp->rbearing = rbearing;
27495 /* Set cmp->offsets for the remaining glyphs. */
27496 for (i++; i < glyph_len; i++)
27498 int left, right, btm, top;
27499 int ch = COMPOSITION_GLYPH (cmp, i);
27500 int face_id;
27501 struct face *this_face;
27503 if (ch == '\t')
27504 ch = ' ';
27505 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27506 this_face = FACE_FROM_ID (it->f, face_id);
27507 font = this_face->font;
27509 if (font == NULL)
27510 pcm = NULL;
27511 else
27513 get_char_face_and_encoding (it->f, ch, face_id,
27514 &char2b, false);
27515 pcm = get_per_char_metric (font, &char2b);
27517 if (! pcm)
27518 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27519 else
27521 width = pcm->width;
27522 ascent = pcm->ascent;
27523 descent = pcm->descent;
27524 lbearing = pcm->lbearing;
27525 rbearing = pcm->rbearing;
27526 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27528 /* Relative composition with or without
27529 alternate chars. */
27530 left = (leftmost + rightmost - width) / 2;
27531 btm = - descent + boff;
27532 if (font->relative_compose
27533 && (! CHAR_TABLE_P (Vignore_relative_composition)
27534 || NILP (Faref (Vignore_relative_composition,
27535 make_number (ch)))))
27538 if (- descent >= font->relative_compose)
27539 /* One extra pixel between two glyphs. */
27540 btm = highest + 1;
27541 else if (ascent <= 0)
27542 /* One extra pixel between two glyphs. */
27543 btm = lowest - 1 - ascent - descent;
27546 else
27548 /* A composition rule is specified by an integer
27549 value that encodes global and new reference
27550 points (GREF and NREF). GREF and NREF are
27551 specified by numbers as below:
27553 0---1---2 -- ascent
27557 9--10--11 -- center
27559 ---3---4---5--- baseline
27561 6---7---8 -- descent
27563 int rule = COMPOSITION_RULE (cmp, i);
27564 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27566 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27567 grefx = gref % 3, nrefx = nref % 3;
27568 grefy = gref / 3, nrefy = nref / 3;
27569 if (xoff)
27570 xoff = font_height * (xoff - 128) / 256;
27571 if (yoff)
27572 yoff = font_height * (yoff - 128) / 256;
27574 left = (leftmost
27575 + grefx * (rightmost - leftmost) / 2
27576 - nrefx * width / 2
27577 + xoff);
27579 btm = ((grefy == 0 ? highest
27580 : grefy == 1 ? 0
27581 : grefy == 2 ? lowest
27582 : (highest + lowest) / 2)
27583 - (nrefy == 0 ? ascent + descent
27584 : nrefy == 1 ? descent - boff
27585 : nrefy == 2 ? 0
27586 : (ascent + descent) / 2)
27587 + yoff);
27590 cmp->offsets[i * 2] = left;
27591 cmp->offsets[i * 2 + 1] = btm + descent;
27593 /* Update the bounding box of the overall glyphs. */
27594 if (width > 0)
27596 right = left + width;
27597 if (left < leftmost)
27598 leftmost = left;
27599 if (right > rightmost)
27600 rightmost = right;
27602 top = btm + descent + ascent;
27603 if (top > highest)
27604 highest = top;
27605 if (btm < lowest)
27606 lowest = btm;
27608 if (cmp->lbearing > left + lbearing)
27609 cmp->lbearing = left + lbearing;
27610 if (cmp->rbearing < left + rbearing)
27611 cmp->rbearing = left + rbearing;
27615 /* If there are glyphs whose x-offsets are negative,
27616 shift all glyphs to the right and make all x-offsets
27617 non-negative. */
27618 if (leftmost < 0)
27620 for (i = 0; i < cmp->glyph_len; i++)
27621 cmp->offsets[i * 2] -= leftmost;
27622 rightmost -= leftmost;
27623 cmp->lbearing -= leftmost;
27624 cmp->rbearing -= leftmost;
27627 if (left_padded && cmp->lbearing < 0)
27629 for (i = 0; i < cmp->glyph_len; i++)
27630 cmp->offsets[i * 2] -= cmp->lbearing;
27631 rightmost -= cmp->lbearing;
27632 cmp->rbearing -= cmp->lbearing;
27633 cmp->lbearing = 0;
27635 if (right_padded && rightmost < cmp->rbearing)
27637 rightmost = cmp->rbearing;
27640 cmp->pixel_width = rightmost;
27641 cmp->ascent = highest;
27642 cmp->descent = - lowest;
27643 if (cmp->ascent < font_ascent)
27644 cmp->ascent = font_ascent;
27645 if (cmp->descent < font_descent)
27646 cmp->descent = font_descent;
27649 if (it->glyph_row
27650 && (cmp->lbearing < 0
27651 || cmp->rbearing > cmp->pixel_width))
27652 it->glyph_row->contains_overlapping_glyphs_p = true;
27654 it->pixel_width = cmp->pixel_width;
27655 it->ascent = it->phys_ascent = cmp->ascent;
27656 it->descent = it->phys_descent = cmp->descent;
27657 if (face->box != FACE_NO_BOX)
27659 int thick = face->box_line_width;
27661 if (thick > 0)
27663 it->ascent += thick;
27664 it->descent += thick;
27666 else
27667 thick = - thick;
27669 if (it->start_of_box_run_p)
27670 it->pixel_width += thick;
27671 if (it->end_of_box_run_p)
27672 it->pixel_width += thick;
27675 /* If face has an overline, add the height of the overline
27676 (1 pixel) and a 1 pixel margin to the character height. */
27677 if (face->overline_p)
27678 it->ascent += overline_margin;
27680 take_vertical_position_into_account (it);
27681 if (it->ascent < 0)
27682 it->ascent = 0;
27683 if (it->descent < 0)
27684 it->descent = 0;
27686 if (it->glyph_row && cmp->glyph_len > 0)
27687 append_composite_glyph (it);
27689 else if (it->what == IT_COMPOSITION)
27691 /* A dynamic (automatic) composition. */
27692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27693 Lisp_Object gstring;
27694 struct font_metrics metrics;
27696 it->nglyphs = 1;
27698 gstring = composition_gstring_from_id (it->cmp_it.id);
27699 it->pixel_width
27700 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27701 &metrics);
27702 if (it->glyph_row
27703 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27704 it->glyph_row->contains_overlapping_glyphs_p = true;
27705 it->ascent = it->phys_ascent = metrics.ascent;
27706 it->descent = it->phys_descent = metrics.descent;
27707 if (face->box != FACE_NO_BOX)
27709 int thick = face->box_line_width;
27711 if (thick > 0)
27713 it->ascent += thick;
27714 it->descent += thick;
27716 else
27717 thick = - thick;
27719 if (it->start_of_box_run_p)
27720 it->pixel_width += thick;
27721 if (it->end_of_box_run_p)
27722 it->pixel_width += thick;
27724 /* If face has an overline, add the height of the overline
27725 (1 pixel) and a 1 pixel margin to the character height. */
27726 if (face->overline_p)
27727 it->ascent += overline_margin;
27728 take_vertical_position_into_account (it);
27729 if (it->ascent < 0)
27730 it->ascent = 0;
27731 if (it->descent < 0)
27732 it->descent = 0;
27734 if (it->glyph_row)
27735 append_composite_glyph (it);
27737 else if (it->what == IT_GLYPHLESS)
27738 produce_glyphless_glyph (it, false, Qnil);
27739 else if (it->what == IT_IMAGE)
27740 produce_image_glyph (it);
27741 else if (it->what == IT_STRETCH)
27742 produce_stretch_glyph (it);
27743 else if (it->what == IT_XWIDGET)
27744 produce_xwidget_glyph (it);
27746 done:
27747 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27748 because this isn't true for images with `:ascent 100'. */
27749 eassert (it->ascent >= 0 && it->descent >= 0);
27750 if (it->area == TEXT_AREA)
27751 it->current_x += it->pixel_width;
27753 if (extra_line_spacing > 0)
27755 it->descent += extra_line_spacing;
27756 if (extra_line_spacing > it->max_extra_line_spacing)
27757 it->max_extra_line_spacing = extra_line_spacing;
27760 it->max_ascent = max (it->max_ascent, it->ascent);
27761 it->max_descent = max (it->max_descent, it->descent);
27762 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27763 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27766 /* EXPORT for RIF:
27767 Output LEN glyphs starting at START at the nominal cursor position.
27768 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27769 being updated, and UPDATED_AREA is the area of that row being updated. */
27771 void
27772 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27773 struct glyph *start, enum glyph_row_area updated_area, int len)
27775 int x, hpos, chpos = w->phys_cursor.hpos;
27777 eassert (updated_row);
27778 /* When the window is hscrolled, cursor hpos can legitimately be out
27779 of bounds, but we draw the cursor at the corresponding window
27780 margin in that case. */
27781 if (!updated_row->reversed_p && chpos < 0)
27782 chpos = 0;
27783 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27784 chpos = updated_row->used[TEXT_AREA] - 1;
27786 block_input ();
27788 /* Write glyphs. */
27790 hpos = start - updated_row->glyphs[updated_area];
27791 x = draw_glyphs (w, w->output_cursor.x,
27792 updated_row, updated_area,
27793 hpos, hpos + len,
27794 DRAW_NORMAL_TEXT, 0);
27796 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27797 if (updated_area == TEXT_AREA
27798 && w->phys_cursor_on_p
27799 && w->phys_cursor.vpos == w->output_cursor.vpos
27800 && chpos >= hpos
27801 && chpos < hpos + len)
27802 w->phys_cursor_on_p = false;
27804 unblock_input ();
27806 /* Advance the output cursor. */
27807 w->output_cursor.hpos += len;
27808 w->output_cursor.x = x;
27812 /* EXPORT for RIF:
27813 Insert LEN glyphs from START at the nominal cursor position. */
27815 void
27816 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27817 struct glyph *start, enum glyph_row_area updated_area, int len)
27819 struct frame *f;
27820 int line_height, shift_by_width, shifted_region_width;
27821 struct glyph_row *row;
27822 struct glyph *glyph;
27823 int frame_x, frame_y;
27824 ptrdiff_t hpos;
27826 eassert (updated_row);
27827 block_input ();
27828 f = XFRAME (WINDOW_FRAME (w));
27830 /* Get the height of the line we are in. */
27831 row = updated_row;
27832 line_height = row->height;
27834 /* Get the width of the glyphs to insert. */
27835 shift_by_width = 0;
27836 for (glyph = start; glyph < start + len; ++glyph)
27837 shift_by_width += glyph->pixel_width;
27839 /* Get the width of the region to shift right. */
27840 shifted_region_width = (window_box_width (w, updated_area)
27841 - w->output_cursor.x
27842 - shift_by_width);
27844 /* Shift right. */
27845 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27846 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27848 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27849 line_height, shift_by_width);
27851 /* Write the glyphs. */
27852 hpos = start - row->glyphs[updated_area];
27853 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27854 hpos, hpos + len,
27855 DRAW_NORMAL_TEXT, 0);
27857 /* Advance the output cursor. */
27858 w->output_cursor.hpos += len;
27859 w->output_cursor.x += shift_by_width;
27860 unblock_input ();
27864 /* EXPORT for RIF:
27865 Erase the current text line from the nominal cursor position
27866 (inclusive) to pixel column TO_X (exclusive). The idea is that
27867 everything from TO_X onward is already erased.
27869 TO_X is a pixel position relative to UPDATED_AREA of currently
27870 updated window W. TO_X == -1 means clear to the end of this area. */
27872 void
27873 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27874 enum glyph_row_area updated_area, int to_x)
27876 struct frame *f;
27877 int max_x, min_y, max_y;
27878 int from_x, from_y, to_y;
27880 eassert (updated_row);
27881 f = XFRAME (w->frame);
27883 if (updated_row->full_width_p)
27884 max_x = (WINDOW_PIXEL_WIDTH (w)
27885 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27886 else
27887 max_x = window_box_width (w, updated_area);
27888 max_y = window_text_bottom_y (w);
27890 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27891 of window. For TO_X > 0, truncate to end of drawing area. */
27892 if (to_x == 0)
27893 return;
27894 else if (to_x < 0)
27895 to_x = max_x;
27896 else
27897 to_x = min (to_x, max_x);
27899 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27901 /* Notice if the cursor will be cleared by this operation. */
27902 if (!updated_row->full_width_p)
27903 notice_overwritten_cursor (w, updated_area,
27904 w->output_cursor.x, -1,
27905 updated_row->y,
27906 MATRIX_ROW_BOTTOM_Y (updated_row));
27908 from_x = w->output_cursor.x;
27910 /* Translate to frame coordinates. */
27911 if (updated_row->full_width_p)
27913 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27914 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27916 else
27918 int area_left = window_box_left (w, updated_area);
27919 from_x += area_left;
27920 to_x += area_left;
27923 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27924 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27925 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27927 /* Prevent inadvertently clearing to end of the X window. */
27928 if (to_x > from_x && to_y > from_y)
27930 block_input ();
27931 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27932 to_x - from_x, to_y - from_y);
27933 unblock_input ();
27937 #endif /* HAVE_WINDOW_SYSTEM */
27941 /***********************************************************************
27942 Cursor types
27943 ***********************************************************************/
27945 /* Value is the internal representation of the specified cursor type
27946 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27947 of the bar cursor. */
27949 static enum text_cursor_kinds
27950 get_specified_cursor_type (Lisp_Object arg, int *width)
27952 enum text_cursor_kinds type;
27954 if (NILP (arg))
27955 return NO_CURSOR;
27957 if (EQ (arg, Qbox))
27958 return FILLED_BOX_CURSOR;
27960 if (EQ (arg, Qhollow))
27961 return HOLLOW_BOX_CURSOR;
27963 if (EQ (arg, Qbar))
27965 *width = 2;
27966 return BAR_CURSOR;
27969 if (CONSP (arg)
27970 && EQ (XCAR (arg), Qbar)
27971 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27973 *width = XINT (XCDR (arg));
27974 return BAR_CURSOR;
27977 if (EQ (arg, Qhbar))
27979 *width = 2;
27980 return HBAR_CURSOR;
27983 if (CONSP (arg)
27984 && EQ (XCAR (arg), Qhbar)
27985 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27987 *width = XINT (XCDR (arg));
27988 return HBAR_CURSOR;
27991 /* Treat anything unknown as "hollow box cursor".
27992 It was bad to signal an error; people have trouble fixing
27993 .Xdefaults with Emacs, when it has something bad in it. */
27994 type = HOLLOW_BOX_CURSOR;
27996 return type;
27999 /* Set the default cursor types for specified frame. */
28000 void
28001 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28003 int width = 1;
28004 Lisp_Object tem;
28006 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28007 FRAME_CURSOR_WIDTH (f) = width;
28009 /* By default, set up the blink-off state depending on the on-state. */
28011 tem = Fassoc (arg, Vblink_cursor_alist);
28012 if (!NILP (tem))
28014 FRAME_BLINK_OFF_CURSOR (f)
28015 = get_specified_cursor_type (XCDR (tem), &width);
28016 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28018 else
28019 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28021 /* Make sure the cursor gets redrawn. */
28022 f->cursor_type_changed = true;
28026 #ifdef HAVE_WINDOW_SYSTEM
28028 /* Return the cursor we want to be displayed in window W. Return
28029 width of bar/hbar cursor through WIDTH arg. Return with
28030 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28031 (i.e. if the `system caret' should track this cursor).
28033 In a mini-buffer window, we want the cursor only to appear if we
28034 are reading input from this window. For the selected window, we
28035 want the cursor type given by the frame parameter or buffer local
28036 setting of cursor-type. If explicitly marked off, draw no cursor.
28037 In all other cases, we want a hollow box cursor. */
28039 static enum text_cursor_kinds
28040 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28041 bool *active_cursor)
28043 struct frame *f = XFRAME (w->frame);
28044 struct buffer *b = XBUFFER (w->contents);
28045 int cursor_type = DEFAULT_CURSOR;
28046 Lisp_Object alt_cursor;
28047 bool non_selected = false;
28049 *active_cursor = true;
28051 /* Echo area */
28052 if (cursor_in_echo_area
28053 && FRAME_HAS_MINIBUF_P (f)
28054 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28056 if (w == XWINDOW (echo_area_window))
28058 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28060 *width = FRAME_CURSOR_WIDTH (f);
28061 return FRAME_DESIRED_CURSOR (f);
28063 else
28064 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28067 *active_cursor = false;
28068 non_selected = true;
28071 /* Detect a nonselected window or nonselected frame. */
28072 else if (w != XWINDOW (f->selected_window)
28073 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28075 *active_cursor = false;
28077 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28078 return NO_CURSOR;
28080 non_selected = true;
28083 /* Never display a cursor in a window in which cursor-type is nil. */
28084 if (NILP (BVAR (b, cursor_type)))
28085 return NO_CURSOR;
28087 /* Get the normal cursor type for this window. */
28088 if (EQ (BVAR (b, cursor_type), Qt))
28090 cursor_type = FRAME_DESIRED_CURSOR (f);
28091 *width = FRAME_CURSOR_WIDTH (f);
28093 else
28094 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28096 /* Use cursor-in-non-selected-windows instead
28097 for non-selected window or frame. */
28098 if (non_selected)
28100 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28101 if (!EQ (Qt, alt_cursor))
28102 return get_specified_cursor_type (alt_cursor, width);
28103 /* t means modify the normal cursor type. */
28104 if (cursor_type == FILLED_BOX_CURSOR)
28105 cursor_type = HOLLOW_BOX_CURSOR;
28106 else if (cursor_type == BAR_CURSOR && *width > 1)
28107 --*width;
28108 return cursor_type;
28111 /* Use normal cursor if not blinked off. */
28112 if (!w->cursor_off_p)
28114 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28115 return NO_CURSOR;
28116 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28118 if (cursor_type == FILLED_BOX_CURSOR)
28120 /* Using a block cursor on large images can be very annoying.
28121 So use a hollow cursor for "large" images.
28122 If image is not transparent (no mask), also use hollow cursor. */
28123 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28124 if (img != NULL && IMAGEP (img->spec))
28126 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28127 where N = size of default frame font size.
28128 This should cover most of the "tiny" icons people may use. */
28129 if (!img->mask
28130 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28131 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28132 cursor_type = HOLLOW_BOX_CURSOR;
28135 else if (cursor_type != NO_CURSOR)
28137 /* Display current only supports BOX and HOLLOW cursors for images.
28138 So for now, unconditionally use a HOLLOW cursor when cursor is
28139 not a solid box cursor. */
28140 cursor_type = HOLLOW_BOX_CURSOR;
28143 return cursor_type;
28146 /* Cursor is blinked off, so determine how to "toggle" it. */
28148 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28149 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28150 return get_specified_cursor_type (XCDR (alt_cursor), width);
28152 /* Then see if frame has specified a specific blink off cursor type. */
28153 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28155 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28156 return FRAME_BLINK_OFF_CURSOR (f);
28159 #if false
28160 /* Some people liked having a permanently visible blinking cursor,
28161 while others had very strong opinions against it. So it was
28162 decided to remove it. KFS 2003-09-03 */
28164 /* Finally perform built-in cursor blinking:
28165 filled box <-> hollow box
28166 wide [h]bar <-> narrow [h]bar
28167 narrow [h]bar <-> no cursor
28168 other type <-> no cursor */
28170 if (cursor_type == FILLED_BOX_CURSOR)
28171 return HOLLOW_BOX_CURSOR;
28173 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28175 *width = 1;
28176 return cursor_type;
28178 #endif
28180 return NO_CURSOR;
28184 /* Notice when the text cursor of window W has been completely
28185 overwritten by a drawing operation that outputs glyphs in AREA
28186 starting at X0 and ending at X1 in the line starting at Y0 and
28187 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28188 the rest of the line after X0 has been written. Y coordinates
28189 are window-relative. */
28191 static void
28192 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28193 int x0, int x1, int y0, int y1)
28195 int cx0, cx1, cy0, cy1;
28196 struct glyph_row *row;
28198 if (!w->phys_cursor_on_p)
28199 return;
28200 if (area != TEXT_AREA)
28201 return;
28203 if (w->phys_cursor.vpos < 0
28204 || w->phys_cursor.vpos >= w->current_matrix->nrows
28205 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28206 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28207 return;
28209 if (row->cursor_in_fringe_p)
28211 row->cursor_in_fringe_p = false;
28212 draw_fringe_bitmap (w, row, row->reversed_p);
28213 w->phys_cursor_on_p = false;
28214 return;
28217 cx0 = w->phys_cursor.x;
28218 cx1 = cx0 + w->phys_cursor_width;
28219 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28220 return;
28222 /* The cursor image will be completely removed from the
28223 screen if the output area intersects the cursor area in
28224 y-direction. When we draw in [y0 y1[, and some part of
28225 the cursor is at y < y0, that part must have been drawn
28226 before. When scrolling, the cursor is erased before
28227 actually scrolling, so we don't come here. When not
28228 scrolling, the rows above the old cursor row must have
28229 changed, and in this case these rows must have written
28230 over the cursor image.
28232 Likewise if part of the cursor is below y1, with the
28233 exception of the cursor being in the first blank row at
28234 the buffer and window end because update_text_area
28235 doesn't draw that row. (Except when it does, but
28236 that's handled in update_text_area.) */
28238 cy0 = w->phys_cursor.y;
28239 cy1 = cy0 + w->phys_cursor_height;
28240 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28241 return;
28243 w->phys_cursor_on_p = false;
28246 #endif /* HAVE_WINDOW_SYSTEM */
28249 /************************************************************************
28250 Mouse Face
28251 ************************************************************************/
28253 #ifdef HAVE_WINDOW_SYSTEM
28255 /* EXPORT for RIF:
28256 Fix the display of area AREA of overlapping row ROW in window W
28257 with respect to the overlapping part OVERLAPS. */
28259 void
28260 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28261 enum glyph_row_area area, int overlaps)
28263 int i, x;
28265 block_input ();
28267 x = 0;
28268 for (i = 0; i < row->used[area];)
28270 if (row->glyphs[area][i].overlaps_vertically_p)
28272 int start = i, start_x = x;
28276 x += row->glyphs[area][i].pixel_width;
28277 ++i;
28279 while (i < row->used[area]
28280 && row->glyphs[area][i].overlaps_vertically_p);
28282 draw_glyphs (w, start_x, row, area,
28283 start, i,
28284 DRAW_NORMAL_TEXT, overlaps);
28286 else
28288 x += row->glyphs[area][i].pixel_width;
28289 ++i;
28293 unblock_input ();
28297 /* EXPORT:
28298 Draw the cursor glyph of window W in glyph row ROW. See the
28299 comment of draw_glyphs for the meaning of HL. */
28301 void
28302 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28303 enum draw_glyphs_face hl)
28305 /* If cursor hpos is out of bounds, don't draw garbage. This can
28306 happen in mini-buffer windows when switching between echo area
28307 glyphs and mini-buffer. */
28308 if ((row->reversed_p
28309 ? (w->phys_cursor.hpos >= 0)
28310 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28312 bool on_p = w->phys_cursor_on_p;
28313 int x1;
28314 int hpos = w->phys_cursor.hpos;
28316 /* When the window is hscrolled, cursor hpos can legitimately be
28317 out of bounds, but we draw the cursor at the corresponding
28318 window margin in that case. */
28319 if (!row->reversed_p && hpos < 0)
28320 hpos = 0;
28321 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28322 hpos = row->used[TEXT_AREA] - 1;
28324 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28325 hl, 0);
28326 w->phys_cursor_on_p = on_p;
28328 if (hl == DRAW_CURSOR)
28329 w->phys_cursor_width = x1 - w->phys_cursor.x;
28330 /* When we erase the cursor, and ROW is overlapped by other
28331 rows, make sure that these overlapping parts of other rows
28332 are redrawn. */
28333 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28335 w->phys_cursor_width = x1 - w->phys_cursor.x;
28337 if (row > w->current_matrix->rows
28338 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28339 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28340 OVERLAPS_ERASED_CURSOR);
28342 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28343 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28344 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28345 OVERLAPS_ERASED_CURSOR);
28351 /* Erase the image of a cursor of window W from the screen. */
28353 void
28354 erase_phys_cursor (struct window *w)
28356 struct frame *f = XFRAME (w->frame);
28357 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28358 int hpos = w->phys_cursor.hpos;
28359 int vpos = w->phys_cursor.vpos;
28360 bool mouse_face_here_p = false;
28361 struct glyph_matrix *active_glyphs = w->current_matrix;
28362 struct glyph_row *cursor_row;
28363 struct glyph *cursor_glyph;
28364 enum draw_glyphs_face hl;
28366 /* No cursor displayed or row invalidated => nothing to do on the
28367 screen. */
28368 if (w->phys_cursor_type == NO_CURSOR)
28369 goto mark_cursor_off;
28371 /* VPOS >= active_glyphs->nrows means that window has been resized.
28372 Don't bother to erase the cursor. */
28373 if (vpos >= active_glyphs->nrows)
28374 goto mark_cursor_off;
28376 /* If row containing cursor is marked invalid, there is nothing we
28377 can do. */
28378 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28379 if (!cursor_row->enabled_p)
28380 goto mark_cursor_off;
28382 /* If line spacing is > 0, old cursor may only be partially visible in
28383 window after split-window. So adjust visible height. */
28384 cursor_row->visible_height = min (cursor_row->visible_height,
28385 window_text_bottom_y (w) - cursor_row->y);
28387 /* If row is completely invisible, don't attempt to delete a cursor which
28388 isn't there. This can happen if cursor is at top of a window, and
28389 we switch to a buffer with a header line in that window. */
28390 if (cursor_row->visible_height <= 0)
28391 goto mark_cursor_off;
28393 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28394 if (cursor_row->cursor_in_fringe_p)
28396 cursor_row->cursor_in_fringe_p = false;
28397 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28398 goto mark_cursor_off;
28401 /* This can happen when the new row is shorter than the old one.
28402 In this case, either draw_glyphs or clear_end_of_line
28403 should have cleared the cursor. Note that we wouldn't be
28404 able to erase the cursor in this case because we don't have a
28405 cursor glyph at hand. */
28406 if ((cursor_row->reversed_p
28407 ? (w->phys_cursor.hpos < 0)
28408 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28409 goto mark_cursor_off;
28411 /* When the window is hscrolled, cursor hpos can legitimately be out
28412 of bounds, but we draw the cursor at the corresponding window
28413 margin in that case. */
28414 if (!cursor_row->reversed_p && hpos < 0)
28415 hpos = 0;
28416 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28417 hpos = cursor_row->used[TEXT_AREA] - 1;
28419 /* If the cursor is in the mouse face area, redisplay that when
28420 we clear the cursor. */
28421 if (! NILP (hlinfo->mouse_face_window)
28422 && coords_in_mouse_face_p (w, hpos, vpos)
28423 /* Don't redraw the cursor's spot in mouse face if it is at the
28424 end of a line (on a newline). The cursor appears there, but
28425 mouse highlighting does not. */
28426 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28427 mouse_face_here_p = true;
28429 /* Maybe clear the display under the cursor. */
28430 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28432 int x, y;
28433 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28434 int width;
28436 cursor_glyph = get_phys_cursor_glyph (w);
28437 if (cursor_glyph == NULL)
28438 goto mark_cursor_off;
28440 width = cursor_glyph->pixel_width;
28441 x = w->phys_cursor.x;
28442 if (x < 0)
28444 width += x;
28445 x = 0;
28447 width = min (width, window_box_width (w, TEXT_AREA) - x);
28448 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28449 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28451 if (width > 0)
28452 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28455 /* Erase the cursor by redrawing the character underneath it. */
28456 if (mouse_face_here_p)
28457 hl = DRAW_MOUSE_FACE;
28458 else
28459 hl = DRAW_NORMAL_TEXT;
28460 draw_phys_cursor_glyph (w, cursor_row, hl);
28462 mark_cursor_off:
28463 w->phys_cursor_on_p = false;
28464 w->phys_cursor_type = NO_CURSOR;
28468 /* Display or clear cursor of window W. If !ON, clear the cursor.
28469 If ON, display the cursor; where to put the cursor is specified by
28470 HPOS, VPOS, X and Y. */
28472 void
28473 display_and_set_cursor (struct window *w, bool on,
28474 int hpos, int vpos, int x, int y)
28476 struct frame *f = XFRAME (w->frame);
28477 int new_cursor_type;
28478 int new_cursor_width;
28479 bool active_cursor;
28480 struct glyph_row *glyph_row;
28481 struct glyph *glyph;
28483 /* This is pointless on invisible frames, and dangerous on garbaged
28484 windows and frames; in the latter case, the frame or window may
28485 be in the midst of changing its size, and x and y may be off the
28486 window. */
28487 if (! FRAME_VISIBLE_P (f)
28488 || FRAME_GARBAGED_P (f)
28489 || vpos >= w->current_matrix->nrows
28490 || hpos >= w->current_matrix->matrix_w)
28491 return;
28493 /* If cursor is off and we want it off, return quickly. */
28494 if (!on && !w->phys_cursor_on_p)
28495 return;
28497 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28498 /* If cursor row is not enabled, we don't really know where to
28499 display the cursor. */
28500 if (!glyph_row->enabled_p)
28502 w->phys_cursor_on_p = false;
28503 return;
28506 glyph = NULL;
28507 if (!glyph_row->exact_window_width_line_p
28508 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28509 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28511 eassert (input_blocked_p ());
28513 /* Set new_cursor_type to the cursor we want to be displayed. */
28514 new_cursor_type = get_window_cursor_type (w, glyph,
28515 &new_cursor_width, &active_cursor);
28517 /* If cursor is currently being shown and we don't want it to be or
28518 it is in the wrong place, or the cursor type is not what we want,
28519 erase it. */
28520 if (w->phys_cursor_on_p
28521 && (!on
28522 || w->phys_cursor.x != x
28523 || w->phys_cursor.y != y
28524 /* HPOS can be negative in R2L rows whose
28525 exact_window_width_line_p flag is set (i.e. their newline
28526 would "overflow into the fringe"). */
28527 || hpos < 0
28528 || new_cursor_type != w->phys_cursor_type
28529 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28530 && new_cursor_width != w->phys_cursor_width)))
28531 erase_phys_cursor (w);
28533 /* Don't check phys_cursor_on_p here because that flag is only set
28534 to false in some cases where we know that the cursor has been
28535 completely erased, to avoid the extra work of erasing the cursor
28536 twice. In other words, phys_cursor_on_p can be true and the cursor
28537 still not be visible, or it has only been partly erased. */
28538 if (on)
28540 w->phys_cursor_ascent = glyph_row->ascent;
28541 w->phys_cursor_height = glyph_row->height;
28543 /* Set phys_cursor_.* before x_draw_.* is called because some
28544 of them may need the information. */
28545 w->phys_cursor.x = x;
28546 w->phys_cursor.y = glyph_row->y;
28547 w->phys_cursor.hpos = hpos;
28548 w->phys_cursor.vpos = vpos;
28551 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28552 new_cursor_type, new_cursor_width,
28553 on, active_cursor);
28557 /* Switch the display of W's cursor on or off, according to the value
28558 of ON. */
28560 static void
28561 update_window_cursor (struct window *w, bool on)
28563 /* Don't update cursor in windows whose frame is in the process
28564 of being deleted. */
28565 if (w->current_matrix)
28567 int hpos = w->phys_cursor.hpos;
28568 int vpos = w->phys_cursor.vpos;
28569 struct glyph_row *row;
28571 if (vpos >= w->current_matrix->nrows
28572 || hpos >= w->current_matrix->matrix_w)
28573 return;
28575 row = MATRIX_ROW (w->current_matrix, vpos);
28577 /* When the window is hscrolled, cursor hpos can legitimately be
28578 out of bounds, but we draw the cursor at the corresponding
28579 window margin in that case. */
28580 if (!row->reversed_p && hpos < 0)
28581 hpos = 0;
28582 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28583 hpos = row->used[TEXT_AREA] - 1;
28585 block_input ();
28586 display_and_set_cursor (w, on, hpos, vpos,
28587 w->phys_cursor.x, w->phys_cursor.y);
28588 unblock_input ();
28593 /* Call update_window_cursor with parameter ON_P on all leaf windows
28594 in the window tree rooted at W. */
28596 static void
28597 update_cursor_in_window_tree (struct window *w, bool on_p)
28599 while (w)
28601 if (WINDOWP (w->contents))
28602 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28603 else
28604 update_window_cursor (w, on_p);
28606 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28611 /* EXPORT:
28612 Display the cursor on window W, or clear it, according to ON_P.
28613 Don't change the cursor's position. */
28615 void
28616 x_update_cursor (struct frame *f, bool on_p)
28618 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28622 /* EXPORT:
28623 Clear the cursor of window W to background color, and mark the
28624 cursor as not shown. This is used when the text where the cursor
28625 is about to be rewritten. */
28627 void
28628 x_clear_cursor (struct window *w)
28630 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28631 update_window_cursor (w, false);
28634 #endif /* HAVE_WINDOW_SYSTEM */
28636 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28637 and MSDOS. */
28638 static void
28639 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28640 int start_hpos, int end_hpos,
28641 enum draw_glyphs_face draw)
28643 #ifdef HAVE_WINDOW_SYSTEM
28644 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28646 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28647 return;
28649 #endif
28650 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28651 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28652 #endif
28655 /* Display the active region described by mouse_face_* according to DRAW. */
28657 static void
28658 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28660 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28661 struct frame *f = XFRAME (WINDOW_FRAME (w));
28663 if (/* If window is in the process of being destroyed, don't bother
28664 to do anything. */
28665 w->current_matrix != NULL
28666 /* Don't update mouse highlight if hidden. */
28667 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28668 /* Recognize when we are called to operate on rows that don't exist
28669 anymore. This can happen when a window is split. */
28670 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28672 bool phys_cursor_on_p = w->phys_cursor_on_p;
28673 struct glyph_row *row, *first, *last;
28675 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28676 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28678 for (row = first; row <= last && row->enabled_p; ++row)
28680 int start_hpos, end_hpos, start_x;
28682 /* For all but the first row, the highlight starts at column 0. */
28683 if (row == first)
28685 /* R2L rows have BEG and END in reversed order, but the
28686 screen drawing geometry is always left to right. So
28687 we need to mirror the beginning and end of the
28688 highlighted area in R2L rows. */
28689 if (!row->reversed_p)
28691 start_hpos = hlinfo->mouse_face_beg_col;
28692 start_x = hlinfo->mouse_face_beg_x;
28694 else if (row == last)
28696 start_hpos = hlinfo->mouse_face_end_col;
28697 start_x = hlinfo->mouse_face_end_x;
28699 else
28701 start_hpos = 0;
28702 start_x = 0;
28705 else if (row->reversed_p && row == last)
28707 start_hpos = hlinfo->mouse_face_end_col;
28708 start_x = hlinfo->mouse_face_end_x;
28710 else
28712 start_hpos = 0;
28713 start_x = 0;
28716 if (row == last)
28718 if (!row->reversed_p)
28719 end_hpos = hlinfo->mouse_face_end_col;
28720 else if (row == first)
28721 end_hpos = hlinfo->mouse_face_beg_col;
28722 else
28724 end_hpos = row->used[TEXT_AREA];
28725 if (draw == DRAW_NORMAL_TEXT)
28726 row->fill_line_p = true; /* Clear to end of line. */
28729 else if (row->reversed_p && row == first)
28730 end_hpos = hlinfo->mouse_face_beg_col;
28731 else
28733 end_hpos = row->used[TEXT_AREA];
28734 if (draw == DRAW_NORMAL_TEXT)
28735 row->fill_line_p = true; /* Clear to end of line. */
28738 if (end_hpos > start_hpos)
28740 draw_row_with_mouse_face (w, start_x, row,
28741 start_hpos, end_hpos, draw);
28743 row->mouse_face_p
28744 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28748 /* When we've written over the cursor, arrange for it to
28749 be displayed again. */
28750 if (FRAME_WINDOW_P (f)
28751 && phys_cursor_on_p && !w->phys_cursor_on_p)
28753 #ifdef HAVE_WINDOW_SYSTEM
28754 int hpos = w->phys_cursor.hpos;
28756 /* When the window is hscrolled, cursor hpos can legitimately be
28757 out of bounds, but we draw the cursor at the corresponding
28758 window margin in that case. */
28759 if (!row->reversed_p && hpos < 0)
28760 hpos = 0;
28761 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28762 hpos = row->used[TEXT_AREA] - 1;
28764 block_input ();
28765 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28766 w->phys_cursor.x, w->phys_cursor.y);
28767 unblock_input ();
28768 #endif /* HAVE_WINDOW_SYSTEM */
28772 #ifdef HAVE_WINDOW_SYSTEM
28773 /* Change the mouse cursor. */
28774 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28776 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28777 if (draw == DRAW_NORMAL_TEXT
28778 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28779 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28780 else
28781 #endif
28782 if (draw == DRAW_MOUSE_FACE)
28783 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28784 else
28785 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28787 #endif /* HAVE_WINDOW_SYSTEM */
28790 /* EXPORT:
28791 Clear out the mouse-highlighted active region.
28792 Redraw it un-highlighted first. Value is true if mouse
28793 face was actually drawn unhighlighted. */
28795 bool
28796 clear_mouse_face (Mouse_HLInfo *hlinfo)
28798 bool cleared
28799 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28800 if (cleared)
28801 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28802 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28803 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28804 hlinfo->mouse_face_window = Qnil;
28805 hlinfo->mouse_face_overlay = Qnil;
28806 return cleared;
28809 /* Return true if the coordinates HPOS and VPOS on windows W are
28810 within the mouse face on that window. */
28811 static bool
28812 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28814 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28816 /* Quickly resolve the easy cases. */
28817 if (!(WINDOWP (hlinfo->mouse_face_window)
28818 && XWINDOW (hlinfo->mouse_face_window) == w))
28819 return false;
28820 if (vpos < hlinfo->mouse_face_beg_row
28821 || vpos > hlinfo->mouse_face_end_row)
28822 return false;
28823 if (vpos > hlinfo->mouse_face_beg_row
28824 && vpos < hlinfo->mouse_face_end_row)
28825 return true;
28827 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28829 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28831 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28832 return true;
28834 else if ((vpos == hlinfo->mouse_face_beg_row
28835 && hpos >= hlinfo->mouse_face_beg_col)
28836 || (vpos == hlinfo->mouse_face_end_row
28837 && hpos < hlinfo->mouse_face_end_col))
28838 return true;
28840 else
28842 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28844 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28845 return true;
28847 else if ((vpos == hlinfo->mouse_face_beg_row
28848 && hpos <= hlinfo->mouse_face_beg_col)
28849 || (vpos == hlinfo->mouse_face_end_row
28850 && hpos > hlinfo->mouse_face_end_col))
28851 return true;
28853 return false;
28857 /* EXPORT:
28858 True if physical cursor of window W is within mouse face. */
28860 bool
28861 cursor_in_mouse_face_p (struct window *w)
28863 int hpos = w->phys_cursor.hpos;
28864 int vpos = w->phys_cursor.vpos;
28865 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28867 /* When the window is hscrolled, cursor hpos can legitimately be out
28868 of bounds, but we draw the cursor at the corresponding window
28869 margin in that case. */
28870 if (!row->reversed_p && hpos < 0)
28871 hpos = 0;
28872 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28873 hpos = row->used[TEXT_AREA] - 1;
28875 return coords_in_mouse_face_p (w, hpos, vpos);
28880 /* Find the glyph rows START_ROW and END_ROW of window W that display
28881 characters between buffer positions START_CHARPOS and END_CHARPOS
28882 (excluding END_CHARPOS). DISP_STRING is a display string that
28883 covers these buffer positions. This is similar to
28884 row_containing_pos, but is more accurate when bidi reordering makes
28885 buffer positions change non-linearly with glyph rows. */
28886 static void
28887 rows_from_pos_range (struct window *w,
28888 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28889 Lisp_Object disp_string,
28890 struct glyph_row **start, struct glyph_row **end)
28892 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28893 int last_y = window_text_bottom_y (w);
28894 struct glyph_row *row;
28896 *start = NULL;
28897 *end = NULL;
28899 while (!first->enabled_p
28900 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28901 first++;
28903 /* Find the START row. */
28904 for (row = first;
28905 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28906 row++)
28908 /* A row can potentially be the START row if the range of the
28909 characters it displays intersects the range
28910 [START_CHARPOS..END_CHARPOS). */
28911 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28912 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28913 /* See the commentary in row_containing_pos, for the
28914 explanation of the complicated way to check whether
28915 some position is beyond the end of the characters
28916 displayed by a row. */
28917 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28918 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28919 && !row->ends_at_zv_p
28920 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28921 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28922 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28923 && !row->ends_at_zv_p
28924 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28926 /* Found a candidate row. Now make sure at least one of the
28927 glyphs it displays has a charpos from the range
28928 [START_CHARPOS..END_CHARPOS).
28930 This is not obvious because bidi reordering could make
28931 buffer positions of a row be 1,2,3,102,101,100, and if we
28932 want to highlight characters in [50..60), we don't want
28933 this row, even though [50..60) does intersect [1..103),
28934 the range of character positions given by the row's start
28935 and end positions. */
28936 struct glyph *g = row->glyphs[TEXT_AREA];
28937 struct glyph *e = g + row->used[TEXT_AREA];
28939 while (g < e)
28941 if (((BUFFERP (g->object) || NILP (g->object))
28942 && start_charpos <= g->charpos && g->charpos < end_charpos)
28943 /* A glyph that comes from DISP_STRING is by
28944 definition to be highlighted. */
28945 || EQ (g->object, disp_string))
28946 *start = row;
28947 g++;
28949 if (*start)
28950 break;
28954 /* Find the END row. */
28955 if (!*start
28956 /* If the last row is partially visible, start looking for END
28957 from that row, instead of starting from FIRST. */
28958 && !(row->enabled_p
28959 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28960 row = first;
28961 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28963 struct glyph_row *next = row + 1;
28964 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28966 if (!next->enabled_p
28967 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28968 /* The first row >= START whose range of displayed characters
28969 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28970 is the row END + 1. */
28971 || (start_charpos < next_start
28972 && end_charpos < next_start)
28973 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28974 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28975 && !next->ends_at_zv_p
28976 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28977 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28978 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28979 && !next->ends_at_zv_p
28980 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28982 *end = row;
28983 break;
28985 else
28987 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28988 but none of the characters it displays are in the range, it is
28989 also END + 1. */
28990 struct glyph *g = next->glyphs[TEXT_AREA];
28991 struct glyph *s = g;
28992 struct glyph *e = g + next->used[TEXT_AREA];
28994 while (g < e)
28996 if (((BUFFERP (g->object) || NILP (g->object))
28997 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28998 /* If the buffer position of the first glyph in
28999 the row is equal to END_CHARPOS, it means
29000 the last character to be highlighted is the
29001 newline of ROW, and we must consider NEXT as
29002 END, not END+1. */
29003 || (((!next->reversed_p && g == s)
29004 || (next->reversed_p && g == e - 1))
29005 && (g->charpos == end_charpos
29006 /* Special case for when NEXT is an
29007 empty line at ZV. */
29008 || (g->charpos == -1
29009 && !row->ends_at_zv_p
29010 && next_start == end_charpos)))))
29011 /* A glyph that comes from DISP_STRING is by
29012 definition to be highlighted. */
29013 || EQ (g->object, disp_string))
29014 break;
29015 g++;
29017 if (g == e)
29019 *end = row;
29020 break;
29022 /* The first row that ends at ZV must be the last to be
29023 highlighted. */
29024 else if (next->ends_at_zv_p)
29026 *end = next;
29027 break;
29033 /* This function sets the mouse_face_* elements of HLINFO, assuming
29034 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29035 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29036 for the overlay or run of text properties specifying the mouse
29037 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29038 before-string and after-string that must also be highlighted.
29039 DISP_STRING, if non-nil, is a display string that may cover some
29040 or all of the highlighted text. */
29042 static void
29043 mouse_face_from_buffer_pos (Lisp_Object window,
29044 Mouse_HLInfo *hlinfo,
29045 ptrdiff_t mouse_charpos,
29046 ptrdiff_t start_charpos,
29047 ptrdiff_t end_charpos,
29048 Lisp_Object before_string,
29049 Lisp_Object after_string,
29050 Lisp_Object disp_string)
29052 struct window *w = XWINDOW (window);
29053 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29054 struct glyph_row *r1, *r2;
29055 struct glyph *glyph, *end;
29056 ptrdiff_t ignore, pos;
29057 int x;
29059 eassert (NILP (disp_string) || STRINGP (disp_string));
29060 eassert (NILP (before_string) || STRINGP (before_string));
29061 eassert (NILP (after_string) || STRINGP (after_string));
29063 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29064 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29065 if (r1 == NULL)
29066 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29067 /* If the before-string or display-string contains newlines,
29068 rows_from_pos_range skips to its last row. Move back. */
29069 if (!NILP (before_string) || !NILP (disp_string))
29071 struct glyph_row *prev;
29072 while ((prev = r1 - 1, prev >= first)
29073 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29074 && prev->used[TEXT_AREA] > 0)
29076 struct glyph *beg = prev->glyphs[TEXT_AREA];
29077 glyph = beg + prev->used[TEXT_AREA];
29078 while (--glyph >= beg && NILP (glyph->object));
29079 if (glyph < beg
29080 || !(EQ (glyph->object, before_string)
29081 || EQ (glyph->object, disp_string)))
29082 break;
29083 r1 = prev;
29086 if (r2 == NULL)
29088 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29089 hlinfo->mouse_face_past_end = true;
29091 else if (!NILP (after_string))
29093 /* If the after-string has newlines, advance to its last row. */
29094 struct glyph_row *next;
29095 struct glyph_row *last
29096 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29098 for (next = r2 + 1;
29099 next <= last
29100 && next->used[TEXT_AREA] > 0
29101 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29102 ++next)
29103 r2 = next;
29105 /* The rest of the display engine assumes that mouse_face_beg_row is
29106 either above mouse_face_end_row or identical to it. But with
29107 bidi-reordered continued lines, the row for START_CHARPOS could
29108 be below the row for END_CHARPOS. If so, swap the rows and store
29109 them in correct order. */
29110 if (r1->y > r2->y)
29112 struct glyph_row *tem = r2;
29114 r2 = r1;
29115 r1 = tem;
29118 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29119 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29121 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29122 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29123 could be anywhere in the row and in any order. The strategy
29124 below is to find the leftmost and the rightmost glyph that
29125 belongs to either of these 3 strings, or whose position is
29126 between START_CHARPOS and END_CHARPOS, and highlight all the
29127 glyphs between those two. This may cover more than just the text
29128 between START_CHARPOS and END_CHARPOS if the range of characters
29129 strides the bidi level boundary, e.g. if the beginning is in R2L
29130 text while the end is in L2R text or vice versa. */
29131 if (!r1->reversed_p)
29133 /* This row is in a left to right paragraph. Scan it left to
29134 right. */
29135 glyph = r1->glyphs[TEXT_AREA];
29136 end = glyph + r1->used[TEXT_AREA];
29137 x = r1->x;
29139 /* Skip truncation glyphs at the start of the glyph row. */
29140 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29141 for (; glyph < end
29142 && NILP (glyph->object)
29143 && glyph->charpos < 0;
29144 ++glyph)
29145 x += glyph->pixel_width;
29147 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29148 or DISP_STRING, and the first glyph from buffer whose
29149 position is between START_CHARPOS and END_CHARPOS. */
29150 for (; glyph < end
29151 && !NILP (glyph->object)
29152 && !EQ (glyph->object, disp_string)
29153 && !(BUFFERP (glyph->object)
29154 && (glyph->charpos >= start_charpos
29155 && glyph->charpos < end_charpos));
29156 ++glyph)
29158 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29159 are present at buffer positions between START_CHARPOS and
29160 END_CHARPOS, or if they come from an overlay. */
29161 if (EQ (glyph->object, before_string))
29163 pos = string_buffer_position (before_string,
29164 start_charpos);
29165 /* If pos == 0, it means before_string came from an
29166 overlay, not from a buffer position. */
29167 if (!pos || (pos >= start_charpos && pos < end_charpos))
29168 break;
29170 else if (EQ (glyph->object, after_string))
29172 pos = string_buffer_position (after_string, end_charpos);
29173 if (!pos || (pos >= start_charpos && pos < end_charpos))
29174 break;
29176 x += glyph->pixel_width;
29178 hlinfo->mouse_face_beg_x = x;
29179 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29181 else
29183 /* This row is in a right to left paragraph. Scan it right to
29184 left. */
29185 struct glyph *g;
29187 end = r1->glyphs[TEXT_AREA] - 1;
29188 glyph = end + r1->used[TEXT_AREA];
29190 /* Skip truncation glyphs at the start of the glyph row. */
29191 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29192 for (; glyph > end
29193 && NILP (glyph->object)
29194 && glyph->charpos < 0;
29195 --glyph)
29198 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29199 or DISP_STRING, and the first glyph from buffer whose
29200 position is between START_CHARPOS and END_CHARPOS. */
29201 for (; glyph > end
29202 && !NILP (glyph->object)
29203 && !EQ (glyph->object, disp_string)
29204 && !(BUFFERP (glyph->object)
29205 && (glyph->charpos >= start_charpos
29206 && glyph->charpos < end_charpos));
29207 --glyph)
29209 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29210 are present at buffer positions between START_CHARPOS and
29211 END_CHARPOS, or if they come from an overlay. */
29212 if (EQ (glyph->object, before_string))
29214 pos = string_buffer_position (before_string, start_charpos);
29215 /* If pos == 0, it means before_string came from an
29216 overlay, not from a buffer position. */
29217 if (!pos || (pos >= start_charpos && pos < end_charpos))
29218 break;
29220 else if (EQ (glyph->object, after_string))
29222 pos = string_buffer_position (after_string, end_charpos);
29223 if (!pos || (pos >= start_charpos && pos < end_charpos))
29224 break;
29228 glyph++; /* first glyph to the right of the highlighted area */
29229 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29230 x += g->pixel_width;
29231 hlinfo->mouse_face_beg_x = x;
29232 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29235 /* If the highlight ends in a different row, compute GLYPH and END
29236 for the end row. Otherwise, reuse the values computed above for
29237 the row where the highlight begins. */
29238 if (r2 != r1)
29240 if (!r2->reversed_p)
29242 glyph = r2->glyphs[TEXT_AREA];
29243 end = glyph + r2->used[TEXT_AREA];
29244 x = r2->x;
29246 else
29248 end = r2->glyphs[TEXT_AREA] - 1;
29249 glyph = end + r2->used[TEXT_AREA];
29253 if (!r2->reversed_p)
29255 /* Skip truncation and continuation glyphs near the end of the
29256 row, and also blanks and stretch glyphs inserted by
29257 extend_face_to_end_of_line. */
29258 while (end > glyph
29259 && NILP ((end - 1)->object))
29260 --end;
29261 /* Scan the rest of the glyph row from the end, looking for the
29262 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29263 DISP_STRING, or whose position is between START_CHARPOS
29264 and END_CHARPOS */
29265 for (--end;
29266 end > glyph
29267 && !NILP (end->object)
29268 && !EQ (end->object, disp_string)
29269 && !(BUFFERP (end->object)
29270 && (end->charpos >= start_charpos
29271 && end->charpos < end_charpos));
29272 --end)
29274 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29275 are present at buffer positions between START_CHARPOS and
29276 END_CHARPOS, or if they come from an overlay. */
29277 if (EQ (end->object, before_string))
29279 pos = string_buffer_position (before_string, start_charpos);
29280 if (!pos || (pos >= start_charpos && pos < end_charpos))
29281 break;
29283 else if (EQ (end->object, after_string))
29285 pos = string_buffer_position (after_string, end_charpos);
29286 if (!pos || (pos >= start_charpos && pos < end_charpos))
29287 break;
29290 /* Find the X coordinate of the last glyph to be highlighted. */
29291 for (; glyph <= end; ++glyph)
29292 x += glyph->pixel_width;
29294 hlinfo->mouse_face_end_x = x;
29295 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29297 else
29299 /* Skip truncation and continuation glyphs near the end of the
29300 row, and also blanks and stretch glyphs inserted by
29301 extend_face_to_end_of_line. */
29302 x = r2->x;
29303 end++;
29304 while (end < glyph
29305 && NILP (end->object))
29307 x += end->pixel_width;
29308 ++end;
29310 /* Scan the rest of the glyph row from the end, looking for the
29311 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29312 DISP_STRING, or whose position is between START_CHARPOS
29313 and END_CHARPOS */
29314 for ( ;
29315 end < glyph
29316 && !NILP (end->object)
29317 && !EQ (end->object, disp_string)
29318 && !(BUFFERP (end->object)
29319 && (end->charpos >= start_charpos
29320 && end->charpos < end_charpos));
29321 ++end)
29323 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29324 are present at buffer positions between START_CHARPOS and
29325 END_CHARPOS, or if they come from an overlay. */
29326 if (EQ (end->object, before_string))
29328 pos = string_buffer_position (before_string, start_charpos);
29329 if (!pos || (pos >= start_charpos && pos < end_charpos))
29330 break;
29332 else if (EQ (end->object, after_string))
29334 pos = string_buffer_position (after_string, end_charpos);
29335 if (!pos || (pos >= start_charpos && pos < end_charpos))
29336 break;
29338 x += end->pixel_width;
29340 /* If we exited the above loop because we arrived at the last
29341 glyph of the row, and its buffer position is still not in
29342 range, it means the last character in range is the preceding
29343 newline. Bump the end column and x values to get past the
29344 last glyph. */
29345 if (end == glyph
29346 && BUFFERP (end->object)
29347 && (end->charpos < start_charpos
29348 || end->charpos >= end_charpos))
29350 x += end->pixel_width;
29351 ++end;
29353 hlinfo->mouse_face_end_x = x;
29354 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29357 hlinfo->mouse_face_window = window;
29358 hlinfo->mouse_face_face_id
29359 = face_at_buffer_position (w, mouse_charpos, &ignore,
29360 mouse_charpos + 1,
29361 !hlinfo->mouse_face_hidden, -1);
29362 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29365 /* The following function is not used anymore (replaced with
29366 mouse_face_from_string_pos), but I leave it here for the time
29367 being, in case someone would. */
29369 #if false /* not used */
29371 /* Find the position of the glyph for position POS in OBJECT in
29372 window W's current matrix, and return in *X, *Y the pixel
29373 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29375 RIGHT_P means return the position of the right edge of the glyph.
29376 !RIGHT_P means return the left edge position.
29378 If no glyph for POS exists in the matrix, return the position of
29379 the glyph with the next smaller position that is in the matrix, if
29380 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29381 exists in the matrix, return the position of the glyph with the
29382 next larger position in OBJECT.
29384 Value is true if a glyph was found. */
29386 static bool
29387 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29388 int *hpos, int *vpos, int *x, int *y, bool right_p)
29390 int yb = window_text_bottom_y (w);
29391 struct glyph_row *r;
29392 struct glyph *best_glyph = NULL;
29393 struct glyph_row *best_row = NULL;
29394 int best_x = 0;
29396 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29397 r->enabled_p && r->y < yb;
29398 ++r)
29400 struct glyph *g = r->glyphs[TEXT_AREA];
29401 struct glyph *e = g + r->used[TEXT_AREA];
29402 int gx;
29404 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29405 if (EQ (g->object, object))
29407 if (g->charpos == pos)
29409 best_glyph = g;
29410 best_x = gx;
29411 best_row = r;
29412 goto found;
29414 else if (best_glyph == NULL
29415 || ((eabs (g->charpos - pos)
29416 < eabs (best_glyph->charpos - pos))
29417 && (right_p
29418 ? g->charpos < pos
29419 : g->charpos > pos)))
29421 best_glyph = g;
29422 best_x = gx;
29423 best_row = r;
29428 found:
29430 if (best_glyph)
29432 *x = best_x;
29433 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29435 if (right_p)
29437 *x += best_glyph->pixel_width;
29438 ++*hpos;
29441 *y = best_row->y;
29442 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29445 return best_glyph != NULL;
29447 #endif /* not used */
29449 /* Find the positions of the first and the last glyphs in window W's
29450 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29451 (assumed to be a string), and return in HLINFO's mouse_face_*
29452 members the pixel and column/row coordinates of those glyphs. */
29454 static void
29455 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29456 Lisp_Object object,
29457 ptrdiff_t startpos, ptrdiff_t endpos)
29459 int yb = window_text_bottom_y (w);
29460 struct glyph_row *r;
29461 struct glyph *g, *e;
29462 int gx;
29463 bool found = false;
29465 /* Find the glyph row with at least one position in the range
29466 [STARTPOS..ENDPOS), and the first glyph in that row whose
29467 position belongs to that range. */
29468 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29469 r->enabled_p && r->y < yb;
29470 ++r)
29472 if (!r->reversed_p)
29474 g = r->glyphs[TEXT_AREA];
29475 e = g + r->used[TEXT_AREA];
29476 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29477 if (EQ (g->object, object)
29478 && startpos <= g->charpos && g->charpos < endpos)
29480 hlinfo->mouse_face_beg_row
29481 = MATRIX_ROW_VPOS (r, w->current_matrix);
29482 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29483 hlinfo->mouse_face_beg_x = gx;
29484 found = true;
29485 break;
29488 else
29490 struct glyph *g1;
29492 e = r->glyphs[TEXT_AREA];
29493 g = e + r->used[TEXT_AREA];
29494 for ( ; g > e; --g)
29495 if (EQ ((g-1)->object, object)
29496 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29498 hlinfo->mouse_face_beg_row
29499 = MATRIX_ROW_VPOS (r, w->current_matrix);
29500 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29501 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29502 gx += g1->pixel_width;
29503 hlinfo->mouse_face_beg_x = gx;
29504 found = true;
29505 break;
29508 if (found)
29509 break;
29512 if (!found)
29513 return;
29515 /* Starting with the next row, look for the first row which does NOT
29516 include any glyphs whose positions are in the range. */
29517 for (++r; r->enabled_p && r->y < yb; ++r)
29519 g = r->glyphs[TEXT_AREA];
29520 e = g + r->used[TEXT_AREA];
29521 found = false;
29522 for ( ; g < e; ++g)
29523 if (EQ (g->object, object)
29524 && startpos <= g->charpos && g->charpos < endpos)
29526 found = true;
29527 break;
29529 if (!found)
29530 break;
29533 /* The highlighted region ends on the previous row. */
29534 r--;
29536 /* Set the end row. */
29537 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29539 /* Compute and set the end column and the end column's horizontal
29540 pixel coordinate. */
29541 if (!r->reversed_p)
29543 g = r->glyphs[TEXT_AREA];
29544 e = g + r->used[TEXT_AREA];
29545 for ( ; e > g; --e)
29546 if (EQ ((e-1)->object, object)
29547 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29548 break;
29549 hlinfo->mouse_face_end_col = e - g;
29551 for (gx = r->x; g < e; ++g)
29552 gx += g->pixel_width;
29553 hlinfo->mouse_face_end_x = gx;
29555 else
29557 e = r->glyphs[TEXT_AREA];
29558 g = e + r->used[TEXT_AREA];
29559 for (gx = r->x ; e < g; ++e)
29561 if (EQ (e->object, object)
29562 && startpos <= e->charpos && e->charpos < endpos)
29563 break;
29564 gx += e->pixel_width;
29566 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29567 hlinfo->mouse_face_end_x = gx;
29571 #ifdef HAVE_WINDOW_SYSTEM
29573 /* See if position X, Y is within a hot-spot of an image. */
29575 static bool
29576 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29578 if (!CONSP (hot_spot))
29579 return false;
29581 if (EQ (XCAR (hot_spot), Qrect))
29583 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29584 Lisp_Object rect = XCDR (hot_spot);
29585 Lisp_Object tem;
29586 if (!CONSP (rect))
29587 return false;
29588 if (!CONSP (XCAR (rect)))
29589 return false;
29590 if (!CONSP (XCDR (rect)))
29591 return false;
29592 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29593 return false;
29594 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29595 return false;
29596 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29597 return false;
29598 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29599 return false;
29600 return true;
29602 else if (EQ (XCAR (hot_spot), Qcircle))
29604 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29605 Lisp_Object circ = XCDR (hot_spot);
29606 Lisp_Object lr, lx0, ly0;
29607 if (CONSP (circ)
29608 && CONSP (XCAR (circ))
29609 && (lr = XCDR (circ), NUMBERP (lr))
29610 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29611 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29613 double r = XFLOATINT (lr);
29614 double dx = XINT (lx0) - x;
29615 double dy = XINT (ly0) - y;
29616 return (dx * dx + dy * dy <= r * r);
29619 else if (EQ (XCAR (hot_spot), Qpoly))
29621 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29622 if (VECTORP (XCDR (hot_spot)))
29624 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29625 Lisp_Object *poly = v->contents;
29626 ptrdiff_t n = v->header.size;
29627 ptrdiff_t i;
29628 bool inside = false;
29629 Lisp_Object lx, ly;
29630 int x0, y0;
29632 /* Need an even number of coordinates, and at least 3 edges. */
29633 if (n < 6 || n & 1)
29634 return false;
29636 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29637 If count is odd, we are inside polygon. Pixels on edges
29638 may or may not be included depending on actual geometry of the
29639 polygon. */
29640 if ((lx = poly[n-2], !INTEGERP (lx))
29641 || (ly = poly[n-1], !INTEGERP (lx)))
29642 return false;
29643 x0 = XINT (lx), y0 = XINT (ly);
29644 for (i = 0; i < n; i += 2)
29646 int x1 = x0, y1 = y0;
29647 if ((lx = poly[i], !INTEGERP (lx))
29648 || (ly = poly[i+1], !INTEGERP (ly)))
29649 return false;
29650 x0 = XINT (lx), y0 = XINT (ly);
29652 /* Does this segment cross the X line? */
29653 if (x0 >= x)
29655 if (x1 >= x)
29656 continue;
29658 else if (x1 < x)
29659 continue;
29660 if (y > y0 && y > y1)
29661 continue;
29662 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29663 inside = !inside;
29665 return inside;
29668 return false;
29671 Lisp_Object
29672 find_hot_spot (Lisp_Object map, int x, int y)
29674 while (CONSP (map))
29676 if (CONSP (XCAR (map))
29677 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29678 return XCAR (map);
29679 map = XCDR (map);
29682 return Qnil;
29685 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29686 3, 3, 0,
29687 doc: /* Lookup in image map MAP coordinates X and Y.
29688 An image map is an alist where each element has the format (AREA ID PLIST).
29689 An AREA is specified as either a rectangle, a circle, or a polygon:
29690 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29691 pixel coordinates of the upper left and bottom right corners.
29692 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29693 and the radius of the circle; r may be a float or integer.
29694 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29695 vector describes one corner in the polygon.
29696 Returns the alist element for the first matching AREA in MAP. */)
29697 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29699 if (NILP (map))
29700 return Qnil;
29702 CHECK_NUMBER (x);
29703 CHECK_NUMBER (y);
29705 return find_hot_spot (map,
29706 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29707 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29709 #endif /* HAVE_WINDOW_SYSTEM */
29712 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29713 static void
29714 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29716 #ifdef HAVE_WINDOW_SYSTEM
29717 if (!FRAME_WINDOW_P (f))
29718 return;
29720 /* Do not change cursor shape while dragging mouse. */
29721 if (EQ (do_mouse_tracking, Qdragging))
29722 return;
29724 if (!NILP (pointer))
29726 if (EQ (pointer, Qarrow))
29727 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29728 else if (EQ (pointer, Qhand))
29729 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29730 else if (EQ (pointer, Qtext))
29731 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29732 else if (EQ (pointer, intern ("hdrag")))
29733 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29734 else if (EQ (pointer, intern ("nhdrag")))
29735 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29736 # ifdef HAVE_X_WINDOWS
29737 else if (EQ (pointer, intern ("vdrag")))
29738 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29739 # endif
29740 else if (EQ (pointer, intern ("hourglass")))
29741 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29742 else if (EQ (pointer, Qmodeline))
29743 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29744 else
29745 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29748 if (cursor != No_Cursor)
29749 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29750 #endif
29753 /* Take proper action when mouse has moved to the mode or header line
29754 or marginal area AREA of window W, x-position X and y-position Y.
29755 X is relative to the start of the text display area of W, so the
29756 width of bitmap areas and scroll bars must be subtracted to get a
29757 position relative to the start of the mode line. */
29759 static void
29760 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29761 enum window_part area)
29763 struct window *w = XWINDOW (window);
29764 struct frame *f = XFRAME (w->frame);
29765 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29766 #ifdef HAVE_WINDOW_SYSTEM
29767 Display_Info *dpyinfo;
29768 #endif
29769 Cursor cursor = No_Cursor;
29770 Lisp_Object pointer = Qnil;
29771 int dx, dy, width, height;
29772 ptrdiff_t charpos;
29773 Lisp_Object string, object = Qnil;
29774 Lisp_Object pos UNINIT;
29775 Lisp_Object mouse_face;
29776 int original_x_pixel = x;
29777 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29778 struct glyph_row *row UNINIT;
29780 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29782 int x0;
29783 struct glyph *end;
29785 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29786 returns them in row/column units! */
29787 string = mode_line_string (w, area, &x, &y, &charpos,
29788 &object, &dx, &dy, &width, &height);
29790 row = (area == ON_MODE_LINE
29791 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29792 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29794 /* Find the glyph under the mouse pointer. */
29795 if (row->mode_line_p && row->enabled_p)
29797 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29798 end = glyph + row->used[TEXT_AREA];
29800 for (x0 = original_x_pixel;
29801 glyph < end && x0 >= glyph->pixel_width;
29802 ++glyph)
29803 x0 -= glyph->pixel_width;
29805 if (glyph >= end)
29806 glyph = NULL;
29809 else
29811 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29812 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29813 returns them in row/column units! */
29814 string = marginal_area_string (w, area, &x, &y, &charpos,
29815 &object, &dx, &dy, &width, &height);
29818 Lisp_Object help = Qnil;
29820 #ifdef HAVE_WINDOW_SYSTEM
29821 if (IMAGEP (object))
29823 Lisp_Object image_map, hotspot;
29824 if ((image_map = Fplist_get (XCDR (object), QCmap),
29825 !NILP (image_map))
29826 && (hotspot = find_hot_spot (image_map, dx, dy),
29827 CONSP (hotspot))
29828 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29830 Lisp_Object plist;
29832 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29833 If so, we could look for mouse-enter, mouse-leave
29834 properties in PLIST (and do something...). */
29835 hotspot = XCDR (hotspot);
29836 if (CONSP (hotspot)
29837 && (plist = XCAR (hotspot), CONSP (plist)))
29839 pointer = Fplist_get (plist, Qpointer);
29840 if (NILP (pointer))
29841 pointer = Qhand;
29842 help = Fplist_get (plist, Qhelp_echo);
29843 if (!NILP (help))
29845 help_echo_string = help;
29846 XSETWINDOW (help_echo_window, w);
29847 help_echo_object = w->contents;
29848 help_echo_pos = charpos;
29852 if (NILP (pointer))
29853 pointer = Fplist_get (XCDR (object), QCpointer);
29855 #endif /* HAVE_WINDOW_SYSTEM */
29857 if (STRINGP (string))
29858 pos = make_number (charpos);
29860 /* Set the help text and mouse pointer. If the mouse is on a part
29861 of the mode line without any text (e.g. past the right edge of
29862 the mode line text), use the default help text and pointer. */
29863 if (STRINGP (string) || area == ON_MODE_LINE)
29865 /* Arrange to display the help by setting the global variables
29866 help_echo_string, help_echo_object, and help_echo_pos. */
29867 if (NILP (help))
29869 if (STRINGP (string))
29870 help = Fget_text_property (pos, Qhelp_echo, string);
29872 if (!NILP (help))
29874 help_echo_string = help;
29875 XSETWINDOW (help_echo_window, w);
29876 help_echo_object = string;
29877 help_echo_pos = charpos;
29879 else if (area == ON_MODE_LINE)
29881 Lisp_Object default_help
29882 = buffer_local_value (Qmode_line_default_help_echo,
29883 w->contents);
29885 if (STRINGP (default_help))
29887 help_echo_string = default_help;
29888 XSETWINDOW (help_echo_window, w);
29889 help_echo_object = Qnil;
29890 help_echo_pos = -1;
29895 #ifdef HAVE_WINDOW_SYSTEM
29896 /* Change the mouse pointer according to what is under it. */
29897 if (FRAME_WINDOW_P (f))
29899 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29900 || minibuf_level
29901 || NILP (Vresize_mini_windows));
29903 dpyinfo = FRAME_DISPLAY_INFO (f);
29904 if (STRINGP (string))
29906 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29908 if (NILP (pointer))
29909 pointer = Fget_text_property (pos, Qpointer, string);
29911 /* Change the mouse pointer according to what is under X/Y. */
29912 if (NILP (pointer)
29913 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29915 Lisp_Object map;
29916 map = Fget_text_property (pos, Qlocal_map, string);
29917 if (!KEYMAPP (map))
29918 map = Fget_text_property (pos, Qkeymap, string);
29919 if (!KEYMAPP (map) && draggable)
29920 cursor = dpyinfo->vertical_scroll_bar_cursor;
29923 else if (draggable)
29924 /* Default mode-line pointer. */
29925 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29927 #endif
29930 /* Change the mouse face according to what is under X/Y. */
29931 bool mouse_face_shown = false;
29932 if (STRINGP (string))
29934 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29935 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29936 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29937 && glyph)
29939 Lisp_Object b, e;
29941 struct glyph * tmp_glyph;
29943 int gpos;
29944 int gseq_length;
29945 int total_pixel_width;
29946 ptrdiff_t begpos, endpos, ignore;
29948 int vpos, hpos;
29950 b = Fprevious_single_property_change (make_number (charpos + 1),
29951 Qmouse_face, string, Qnil);
29952 if (NILP (b))
29953 begpos = 0;
29954 else
29955 begpos = XINT (b);
29957 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29958 if (NILP (e))
29959 endpos = SCHARS (string);
29960 else
29961 endpos = XINT (e);
29963 /* Calculate the glyph position GPOS of GLYPH in the
29964 displayed string, relative to the beginning of the
29965 highlighted part of the string.
29967 Note: GPOS is different from CHARPOS. CHARPOS is the
29968 position of GLYPH in the internal string object. A mode
29969 line string format has structures which are converted to
29970 a flattened string by the Emacs Lisp interpreter. The
29971 internal string is an element of those structures. The
29972 displayed string is the flattened string. */
29973 tmp_glyph = row_start_glyph;
29974 while (tmp_glyph < glyph
29975 && (!(EQ (tmp_glyph->object, glyph->object)
29976 && begpos <= tmp_glyph->charpos
29977 && tmp_glyph->charpos < endpos)))
29978 tmp_glyph++;
29979 gpos = glyph - tmp_glyph;
29981 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29982 the highlighted part of the displayed string to which
29983 GLYPH belongs. Note: GSEQ_LENGTH is different from
29984 SCHARS (STRING), because the latter returns the length of
29985 the internal string. */
29986 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29987 tmp_glyph > glyph
29988 && (!(EQ (tmp_glyph->object, glyph->object)
29989 && begpos <= tmp_glyph->charpos
29990 && tmp_glyph->charpos < endpos));
29991 tmp_glyph--)
29993 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29995 /* Calculate the total pixel width of all the glyphs between
29996 the beginning of the highlighted area and GLYPH. */
29997 total_pixel_width = 0;
29998 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29999 total_pixel_width += tmp_glyph->pixel_width;
30001 /* Pre calculation of re-rendering position. Note: X is in
30002 column units here, after the call to mode_line_string or
30003 marginal_area_string. */
30004 hpos = x - gpos;
30005 vpos = (area == ON_MODE_LINE
30006 ? (w->current_matrix)->nrows - 1
30007 : 0);
30009 /* If GLYPH's position is included in the region that is
30010 already drawn in mouse face, we have nothing to do. */
30011 if ( EQ (window, hlinfo->mouse_face_window)
30012 && (!row->reversed_p
30013 ? (hlinfo->mouse_face_beg_col <= hpos
30014 && hpos < hlinfo->mouse_face_end_col)
30015 /* In R2L rows we swap BEG and END, see below. */
30016 : (hlinfo->mouse_face_end_col <= hpos
30017 && hpos < hlinfo->mouse_face_beg_col))
30018 && hlinfo->mouse_face_beg_row == vpos )
30019 return;
30021 if (clear_mouse_face (hlinfo))
30022 cursor = No_Cursor;
30024 if (!row->reversed_p)
30026 hlinfo->mouse_face_beg_col = hpos;
30027 hlinfo->mouse_face_beg_x = original_x_pixel
30028 - (total_pixel_width + dx);
30029 hlinfo->mouse_face_end_col = hpos + gseq_length;
30030 hlinfo->mouse_face_end_x = 0;
30032 else
30034 /* In R2L rows, show_mouse_face expects BEG and END
30035 coordinates to be swapped. */
30036 hlinfo->mouse_face_end_col = hpos;
30037 hlinfo->mouse_face_end_x = original_x_pixel
30038 - (total_pixel_width + dx);
30039 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30040 hlinfo->mouse_face_beg_x = 0;
30043 hlinfo->mouse_face_beg_row = vpos;
30044 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30045 hlinfo->mouse_face_past_end = false;
30046 hlinfo->mouse_face_window = window;
30048 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30049 charpos,
30050 0, &ignore,
30051 glyph->face_id,
30052 true);
30053 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30054 mouse_face_shown = true;
30056 if (NILP (pointer))
30057 pointer = Qhand;
30061 /* If mouse-face doesn't need to be shown, clear any existing
30062 mouse-face. */
30063 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30064 clear_mouse_face (hlinfo);
30066 define_frame_cursor1 (f, cursor, pointer);
30070 /* EXPORT:
30071 Take proper action when the mouse has moved to position X, Y on
30072 frame F with regards to highlighting portions of display that have
30073 mouse-face properties. Also de-highlight portions of display where
30074 the mouse was before, set the mouse pointer shape as appropriate
30075 for the mouse coordinates, and activate help echo (tooltips).
30076 X and Y can be negative or out of range. */
30078 void
30079 note_mouse_highlight (struct frame *f, int x, int y)
30081 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30082 enum window_part part = ON_NOTHING;
30083 Lisp_Object window;
30084 struct window *w;
30085 Cursor cursor = No_Cursor;
30086 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30087 struct buffer *b;
30089 /* When a menu is active, don't highlight because this looks odd. */
30090 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30091 if (popup_activated ())
30092 return;
30093 #endif
30095 if (!f->glyphs_initialized_p
30096 || f->pointer_invisible)
30097 return;
30099 hlinfo->mouse_face_mouse_x = x;
30100 hlinfo->mouse_face_mouse_y = y;
30101 hlinfo->mouse_face_mouse_frame = f;
30103 if (hlinfo->mouse_face_defer)
30104 return;
30106 /* Which window is that in? */
30107 window = window_from_coordinates (f, x, y, &part, true);
30109 /* If displaying active text in another window, clear that. */
30110 if (! EQ (window, hlinfo->mouse_face_window)
30111 /* Also clear if we move out of text area in same window. */
30112 || (!NILP (hlinfo->mouse_face_window)
30113 && !NILP (window)
30114 && part != ON_TEXT
30115 && part != ON_MODE_LINE
30116 && part != ON_HEADER_LINE))
30117 clear_mouse_face (hlinfo);
30119 /* Not on a window -> return. */
30120 if (!WINDOWP (window))
30121 return;
30123 /* Reset help_echo_string. It will get recomputed below. */
30124 help_echo_string = Qnil;
30126 /* Convert to window-relative pixel coordinates. */
30127 w = XWINDOW (window);
30128 frame_to_window_pixel_xy (w, &x, &y);
30130 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30131 /* Handle tool-bar window differently since it doesn't display a
30132 buffer. */
30133 if (EQ (window, f->tool_bar_window))
30135 note_tool_bar_highlight (f, x, y);
30136 return;
30138 #endif
30140 /* Mouse is on the mode, header line or margin? */
30141 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30142 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30144 note_mode_line_or_margin_highlight (window, x, y, part);
30146 #ifdef HAVE_WINDOW_SYSTEM
30147 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30149 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30150 /* Show non-text cursor (Bug#16647). */
30151 goto set_cursor;
30153 else
30154 #endif
30155 return;
30158 #ifdef HAVE_WINDOW_SYSTEM
30159 if (part == ON_VERTICAL_BORDER)
30161 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30162 help_echo_string = build_string ("drag-mouse-1: resize");
30164 else if (part == ON_RIGHT_DIVIDER)
30166 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30167 help_echo_string = build_string ("drag-mouse-1: resize");
30169 else if (part == ON_BOTTOM_DIVIDER)
30170 if (! WINDOW_BOTTOMMOST_P (w)
30171 || minibuf_level
30172 || NILP (Vresize_mini_windows))
30174 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30175 help_echo_string = build_string ("drag-mouse-1: resize");
30177 else
30178 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30179 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30180 || part == ON_VERTICAL_SCROLL_BAR
30181 || part == ON_HORIZONTAL_SCROLL_BAR)
30182 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30183 else
30184 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30185 #endif
30187 /* Are we in a window whose display is up to date?
30188 And verify the buffer's text has not changed. */
30189 b = XBUFFER (w->contents);
30190 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30192 int hpos, vpos, dx, dy, area = LAST_AREA;
30193 ptrdiff_t pos;
30194 struct glyph *glyph;
30195 Lisp_Object object;
30196 Lisp_Object mouse_face = Qnil, position;
30197 Lisp_Object *overlay_vec = NULL;
30198 ptrdiff_t i, noverlays;
30199 struct buffer *obuf;
30200 ptrdiff_t obegv, ozv;
30201 bool same_region;
30203 /* Find the glyph under X/Y. */
30204 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30206 #ifdef HAVE_WINDOW_SYSTEM
30207 /* Look for :pointer property on image. */
30208 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30210 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30211 if (img != NULL && IMAGEP (img->spec))
30213 Lisp_Object image_map, hotspot;
30214 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30215 !NILP (image_map))
30216 && (hotspot = find_hot_spot (image_map,
30217 glyph->slice.img.x + dx,
30218 glyph->slice.img.y + dy),
30219 CONSP (hotspot))
30220 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30222 Lisp_Object plist;
30224 /* Could check XCAR (hotspot) to see if we enter/leave
30225 this hot-spot.
30226 If so, we could look for mouse-enter, mouse-leave
30227 properties in PLIST (and do something...). */
30228 hotspot = XCDR (hotspot);
30229 if (CONSP (hotspot)
30230 && (plist = XCAR (hotspot), CONSP (plist)))
30232 pointer = Fplist_get (plist, Qpointer);
30233 if (NILP (pointer))
30234 pointer = Qhand;
30235 help_echo_string = Fplist_get (plist, Qhelp_echo);
30236 if (!NILP (help_echo_string))
30238 help_echo_window = window;
30239 help_echo_object = glyph->object;
30240 help_echo_pos = glyph->charpos;
30244 if (NILP (pointer))
30245 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30248 #endif /* HAVE_WINDOW_SYSTEM */
30250 /* Clear mouse face if X/Y not over text. */
30251 if (glyph == NULL
30252 || area != TEXT_AREA
30253 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30254 /* Glyph's OBJECT is nil for glyphs inserted by the
30255 display engine for its internal purposes, like truncation
30256 and continuation glyphs and blanks beyond the end of
30257 line's text on text terminals. If we are over such a
30258 glyph, we are not over any text. */
30259 || NILP (glyph->object)
30260 /* R2L rows have a stretch glyph at their front, which
30261 stands for no text, whereas L2R rows have no glyphs at
30262 all beyond the end of text. Treat such stretch glyphs
30263 like we do with NULL glyphs in L2R rows. */
30264 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30265 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30266 && glyph->type == STRETCH_GLYPH
30267 && glyph->avoid_cursor_p))
30269 if (clear_mouse_face (hlinfo))
30270 cursor = No_Cursor;
30271 if (FRAME_WINDOW_P (f) && NILP (pointer))
30273 #ifdef HAVE_WINDOW_SYSTEM
30274 if (area != TEXT_AREA)
30275 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30276 else
30277 pointer = Vvoid_text_area_pointer;
30278 #endif
30280 goto set_cursor;
30283 pos = glyph->charpos;
30284 object = glyph->object;
30285 if (!STRINGP (object) && !BUFFERP (object))
30286 goto set_cursor;
30288 /* If we get an out-of-range value, return now; avoid an error. */
30289 if (BUFFERP (object) && pos > BUF_Z (b))
30290 goto set_cursor;
30292 /* Make the window's buffer temporarily current for
30293 overlays_at and compute_char_face. */
30294 obuf = current_buffer;
30295 current_buffer = b;
30296 obegv = BEGV;
30297 ozv = ZV;
30298 BEGV = BEG;
30299 ZV = Z;
30301 /* Is this char mouse-active or does it have help-echo? */
30302 position = make_number (pos);
30304 USE_SAFE_ALLOCA;
30306 if (BUFFERP (object))
30308 /* Put all the overlays we want in a vector in overlay_vec. */
30309 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30310 /* Sort overlays into increasing priority order. */
30311 noverlays = sort_overlays (overlay_vec, noverlays, w);
30313 else
30314 noverlays = 0;
30316 if (NILP (Vmouse_highlight))
30318 clear_mouse_face (hlinfo);
30319 goto check_help_echo;
30322 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30324 if (same_region)
30325 cursor = No_Cursor;
30327 /* Check mouse-face highlighting. */
30328 if (! same_region
30329 /* If there exists an overlay with mouse-face overlapping
30330 the one we are currently highlighting, we have to
30331 check if we enter the overlapping overlay, and then
30332 highlight only that. */
30333 || (OVERLAYP (hlinfo->mouse_face_overlay)
30334 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30336 /* Find the highest priority overlay with a mouse-face. */
30337 Lisp_Object overlay = Qnil;
30338 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30340 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30341 if (!NILP (mouse_face))
30342 overlay = overlay_vec[i];
30345 /* If we're highlighting the same overlay as before, there's
30346 no need to do that again. */
30347 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30348 goto check_help_echo;
30349 hlinfo->mouse_face_overlay = overlay;
30351 /* Clear the display of the old active region, if any. */
30352 if (clear_mouse_face (hlinfo))
30353 cursor = No_Cursor;
30355 /* If no overlay applies, get a text property. */
30356 if (NILP (overlay))
30357 mouse_face = Fget_text_property (position, Qmouse_face, object);
30359 /* Next, compute the bounds of the mouse highlighting and
30360 display it. */
30361 if (!NILP (mouse_face) && STRINGP (object))
30363 /* The mouse-highlighting comes from a display string
30364 with a mouse-face. */
30365 Lisp_Object s, e;
30366 ptrdiff_t ignore;
30368 s = Fprevious_single_property_change
30369 (make_number (pos + 1), Qmouse_face, object, Qnil);
30370 e = Fnext_single_property_change
30371 (position, Qmouse_face, object, Qnil);
30372 if (NILP (s))
30373 s = make_number (0);
30374 if (NILP (e))
30375 e = make_number (SCHARS (object));
30376 mouse_face_from_string_pos (w, hlinfo, object,
30377 XINT (s), XINT (e));
30378 hlinfo->mouse_face_past_end = false;
30379 hlinfo->mouse_face_window = window;
30380 hlinfo->mouse_face_face_id
30381 = face_at_string_position (w, object, pos, 0, &ignore,
30382 glyph->face_id, true);
30383 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30384 cursor = No_Cursor;
30386 else
30388 /* The mouse-highlighting, if any, comes from an overlay
30389 or text property in the buffer. */
30390 Lisp_Object buffer UNINIT;
30391 Lisp_Object disp_string UNINIT;
30393 if (STRINGP (object))
30395 /* If we are on a display string with no mouse-face,
30396 check if the text under it has one. */
30397 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30398 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30399 pos = string_buffer_position (object, start);
30400 if (pos > 0)
30402 mouse_face = get_char_property_and_overlay
30403 (make_number (pos), Qmouse_face, w->contents, &overlay);
30404 buffer = w->contents;
30405 disp_string = object;
30408 else
30410 buffer = object;
30411 disp_string = Qnil;
30414 if (!NILP (mouse_face))
30416 Lisp_Object before, after;
30417 Lisp_Object before_string, after_string;
30418 /* To correctly find the limits of mouse highlight
30419 in a bidi-reordered buffer, we must not use the
30420 optimization of limiting the search in
30421 previous-single-property-change and
30422 next-single-property-change, because
30423 rows_from_pos_range needs the real start and end
30424 positions to DTRT in this case. That's because
30425 the first row visible in a window does not
30426 necessarily display the character whose position
30427 is the smallest. */
30428 Lisp_Object lim1
30429 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30430 ? Fmarker_position (w->start)
30431 : Qnil;
30432 Lisp_Object lim2
30433 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30434 ? make_number (BUF_Z (XBUFFER (buffer))
30435 - w->window_end_pos)
30436 : Qnil;
30438 if (NILP (overlay))
30440 /* Handle the text property case. */
30441 before = Fprevious_single_property_change
30442 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30443 after = Fnext_single_property_change
30444 (make_number (pos), Qmouse_face, buffer, lim2);
30445 before_string = after_string = Qnil;
30447 else
30449 /* Handle the overlay case. */
30450 before = Foverlay_start (overlay);
30451 after = Foverlay_end (overlay);
30452 before_string = Foverlay_get (overlay, Qbefore_string);
30453 after_string = Foverlay_get (overlay, Qafter_string);
30455 if (!STRINGP (before_string)) before_string = Qnil;
30456 if (!STRINGP (after_string)) after_string = Qnil;
30459 mouse_face_from_buffer_pos (window, hlinfo, pos,
30460 NILP (before)
30462 : XFASTINT (before),
30463 NILP (after)
30464 ? BUF_Z (XBUFFER (buffer))
30465 : XFASTINT (after),
30466 before_string, after_string,
30467 disp_string);
30468 cursor = No_Cursor;
30473 check_help_echo:
30475 /* Look for a `help-echo' property. */
30476 if (NILP (help_echo_string)) {
30477 Lisp_Object help, overlay;
30479 /* Check overlays first. */
30480 help = overlay = Qnil;
30481 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30483 overlay = overlay_vec[i];
30484 help = Foverlay_get (overlay, Qhelp_echo);
30487 if (!NILP (help))
30489 help_echo_string = help;
30490 help_echo_window = window;
30491 help_echo_object = overlay;
30492 help_echo_pos = pos;
30494 else
30496 Lisp_Object obj = glyph->object;
30497 ptrdiff_t charpos = glyph->charpos;
30499 /* Try text properties. */
30500 if (STRINGP (obj)
30501 && charpos >= 0
30502 && charpos < SCHARS (obj))
30504 help = Fget_text_property (make_number (charpos),
30505 Qhelp_echo, obj);
30506 if (NILP (help))
30508 /* If the string itself doesn't specify a help-echo,
30509 see if the buffer text ``under'' it does. */
30510 struct glyph_row *r
30511 = MATRIX_ROW (w->current_matrix, vpos);
30512 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30513 ptrdiff_t p = string_buffer_position (obj, start);
30514 if (p > 0)
30516 help = Fget_char_property (make_number (p),
30517 Qhelp_echo, w->contents);
30518 if (!NILP (help))
30520 charpos = p;
30521 obj = w->contents;
30526 else if (BUFFERP (obj)
30527 && charpos >= BEGV
30528 && charpos < ZV)
30529 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30530 obj);
30532 if (!NILP (help))
30534 help_echo_string = help;
30535 help_echo_window = window;
30536 help_echo_object = obj;
30537 help_echo_pos = charpos;
30542 #ifdef HAVE_WINDOW_SYSTEM
30543 /* Look for a `pointer' property. */
30544 if (FRAME_WINDOW_P (f) && NILP (pointer))
30546 /* Check overlays first. */
30547 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30548 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30550 if (NILP (pointer))
30552 Lisp_Object obj = glyph->object;
30553 ptrdiff_t charpos = glyph->charpos;
30555 /* Try text properties. */
30556 if (STRINGP (obj)
30557 && charpos >= 0
30558 && charpos < SCHARS (obj))
30560 pointer = Fget_text_property (make_number (charpos),
30561 Qpointer, obj);
30562 if (NILP (pointer))
30564 /* If the string itself doesn't specify a pointer,
30565 see if the buffer text ``under'' it does. */
30566 struct glyph_row *r
30567 = MATRIX_ROW (w->current_matrix, vpos);
30568 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30569 ptrdiff_t p = string_buffer_position (obj, start);
30570 if (p > 0)
30571 pointer = Fget_char_property (make_number (p),
30572 Qpointer, w->contents);
30575 else if (BUFFERP (obj)
30576 && charpos >= BEGV
30577 && charpos < ZV)
30578 pointer = Fget_text_property (make_number (charpos),
30579 Qpointer, obj);
30582 #endif /* HAVE_WINDOW_SYSTEM */
30584 BEGV = obegv;
30585 ZV = ozv;
30586 current_buffer = obuf;
30587 SAFE_FREE ();
30590 set_cursor:
30591 define_frame_cursor1 (f, cursor, pointer);
30595 /* EXPORT for RIF:
30596 Clear any mouse-face on window W. This function is part of the
30597 redisplay interface, and is called from try_window_id and similar
30598 functions to ensure the mouse-highlight is off. */
30600 void
30601 x_clear_window_mouse_face (struct window *w)
30603 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30604 Lisp_Object window;
30606 block_input ();
30607 XSETWINDOW (window, w);
30608 if (EQ (window, hlinfo->mouse_face_window))
30609 clear_mouse_face (hlinfo);
30610 unblock_input ();
30614 /* EXPORT:
30615 Just discard the mouse face information for frame F, if any.
30616 This is used when the size of F is changed. */
30618 void
30619 cancel_mouse_face (struct frame *f)
30621 Lisp_Object window;
30622 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30624 window = hlinfo->mouse_face_window;
30625 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30626 reset_mouse_highlight (hlinfo);
30631 /***********************************************************************
30632 Exposure Events
30633 ***********************************************************************/
30635 #ifdef HAVE_WINDOW_SYSTEM
30637 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30638 which intersects rectangle R. R is in window-relative coordinates. */
30640 static void
30641 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30642 enum glyph_row_area area)
30644 struct glyph *first = row->glyphs[area];
30645 struct glyph *end = row->glyphs[area] + row->used[area];
30646 struct glyph *last;
30647 int first_x, start_x, x;
30649 if (area == TEXT_AREA && row->fill_line_p)
30650 /* If row extends face to end of line write the whole line. */
30651 draw_glyphs (w, 0, row, area,
30652 0, row->used[area],
30653 DRAW_NORMAL_TEXT, 0);
30654 else
30656 /* Set START_X to the window-relative start position for drawing glyphs of
30657 AREA. The first glyph of the text area can be partially visible.
30658 The first glyphs of other areas cannot. */
30659 start_x = window_box_left_offset (w, area);
30660 x = start_x;
30661 if (area == TEXT_AREA)
30662 x += row->x;
30664 /* Find the first glyph that must be redrawn. */
30665 while (first < end
30666 && x + first->pixel_width < r->x)
30668 x += first->pixel_width;
30669 ++first;
30672 /* Find the last one. */
30673 last = first;
30674 first_x = x;
30675 /* Use a signed int intermediate value to avoid catastrophic
30676 failures due to comparison between signed and unsigned, when
30677 x is negative (can happen for wide images that are hscrolled). */
30678 int r_end = r->x + r->width;
30679 while (last < end && x < r_end)
30681 x += last->pixel_width;
30682 ++last;
30685 /* Repaint. */
30686 if (last > first)
30687 draw_glyphs (w, first_x - start_x, row, area,
30688 first - row->glyphs[area], last - row->glyphs[area],
30689 DRAW_NORMAL_TEXT, 0);
30694 /* Redraw the parts of the glyph row ROW on window W intersecting
30695 rectangle R. R is in window-relative coordinates. Value is
30696 true if mouse-face was overwritten. */
30698 static bool
30699 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30701 eassert (row->enabled_p);
30703 if (row->mode_line_p || w->pseudo_window_p)
30704 draw_glyphs (w, 0, row, TEXT_AREA,
30705 0, row->used[TEXT_AREA],
30706 DRAW_NORMAL_TEXT, 0);
30707 else
30709 if (row->used[LEFT_MARGIN_AREA])
30710 expose_area (w, row, r, LEFT_MARGIN_AREA);
30711 if (row->used[TEXT_AREA])
30712 expose_area (w, row, r, TEXT_AREA);
30713 if (row->used[RIGHT_MARGIN_AREA])
30714 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30715 draw_row_fringe_bitmaps (w, row);
30718 return row->mouse_face_p;
30722 /* Redraw those parts of glyphs rows during expose event handling that
30723 overlap other rows. Redrawing of an exposed line writes over parts
30724 of lines overlapping that exposed line; this function fixes that.
30726 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30727 row in W's current matrix that is exposed and overlaps other rows.
30728 LAST_OVERLAPPING_ROW is the last such row. */
30730 static void
30731 expose_overlaps (struct window *w,
30732 struct glyph_row *first_overlapping_row,
30733 struct glyph_row *last_overlapping_row,
30734 XRectangle *r)
30736 struct glyph_row *row;
30738 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30739 if (row->overlapping_p)
30741 eassert (row->enabled_p && !row->mode_line_p);
30743 row->clip = r;
30744 if (row->used[LEFT_MARGIN_AREA])
30745 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30747 if (row->used[TEXT_AREA])
30748 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30750 if (row->used[RIGHT_MARGIN_AREA])
30751 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30752 row->clip = NULL;
30757 /* Return true if W's cursor intersects rectangle R. */
30759 static bool
30760 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30762 XRectangle cr, result;
30763 struct glyph *cursor_glyph;
30764 struct glyph_row *row;
30766 if (w->phys_cursor.vpos >= 0
30767 && w->phys_cursor.vpos < w->current_matrix->nrows
30768 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30769 row->enabled_p)
30770 && row->cursor_in_fringe_p)
30772 /* Cursor is in the fringe. */
30773 cr.x = window_box_right_offset (w,
30774 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30775 ? RIGHT_MARGIN_AREA
30776 : TEXT_AREA));
30777 cr.y = row->y;
30778 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30779 cr.height = row->height;
30780 return x_intersect_rectangles (&cr, r, &result);
30783 cursor_glyph = get_phys_cursor_glyph (w);
30784 if (cursor_glyph)
30786 /* r is relative to W's box, but w->phys_cursor.x is relative
30787 to left edge of W's TEXT area. Adjust it. */
30788 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30789 cr.y = w->phys_cursor.y;
30790 cr.width = cursor_glyph->pixel_width;
30791 cr.height = w->phys_cursor_height;
30792 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30793 I assume the effect is the same -- and this is portable. */
30794 return x_intersect_rectangles (&cr, r, &result);
30796 /* If we don't understand the format, pretend we're not in the hot-spot. */
30797 return false;
30801 /* EXPORT:
30802 Draw a vertical window border to the right of window W if W doesn't
30803 have vertical scroll bars. */
30805 void
30806 x_draw_vertical_border (struct window *w)
30808 struct frame *f = XFRAME (WINDOW_FRAME (w));
30810 /* We could do better, if we knew what type of scroll-bar the adjacent
30811 windows (on either side) have... But we don't :-(
30812 However, I think this works ok. ++KFS 2003-04-25 */
30814 /* Redraw borders between horizontally adjacent windows. Don't
30815 do it for frames with vertical scroll bars because either the
30816 right scroll bar of a window, or the left scroll bar of its
30817 neighbor will suffice as a border. */
30818 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30819 return;
30821 /* Note: It is necessary to redraw both the left and the right
30822 borders, for when only this single window W is being
30823 redisplayed. */
30824 if (!WINDOW_RIGHTMOST_P (w)
30825 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30827 int x0, x1, y0, y1;
30829 window_box_edges (w, &x0, &y0, &x1, &y1);
30830 y1 -= 1;
30832 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30833 x1 -= 1;
30835 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30838 if (!WINDOW_LEFTMOST_P (w)
30839 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30841 int x0, x1, y0, y1;
30843 window_box_edges (w, &x0, &y0, &x1, &y1);
30844 y1 -= 1;
30846 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30847 x0 -= 1;
30849 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30854 /* Draw window dividers for window W. */
30856 void
30857 x_draw_right_divider (struct window *w)
30859 struct frame *f = WINDOW_XFRAME (w);
30861 if (w->mini || w->pseudo_window_p)
30862 return;
30863 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30865 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30866 int x1 = WINDOW_RIGHT_EDGE_X (w);
30867 int y0 = WINDOW_TOP_EDGE_Y (w);
30868 /* The bottom divider prevails. */
30869 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30871 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30875 static void
30876 x_draw_bottom_divider (struct window *w)
30878 struct frame *f = XFRAME (WINDOW_FRAME (w));
30880 if (w->mini || w->pseudo_window_p)
30881 return;
30882 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30884 int x0 = WINDOW_LEFT_EDGE_X (w);
30885 int x1 = WINDOW_RIGHT_EDGE_X (w);
30886 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30887 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30889 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30893 /* Redraw the part of window W intersection rectangle FR. Pixel
30894 coordinates in FR are frame-relative. Call this function with
30895 input blocked. Value is true if the exposure overwrites
30896 mouse-face. */
30898 static bool
30899 expose_window (struct window *w, XRectangle *fr)
30901 struct frame *f = XFRAME (w->frame);
30902 XRectangle wr, r;
30903 bool mouse_face_overwritten_p = false;
30905 /* If window is not yet fully initialized, do nothing. This can
30906 happen when toolkit scroll bars are used and a window is split.
30907 Reconfiguring the scroll bar will generate an expose for a newly
30908 created window. */
30909 if (w->current_matrix == NULL)
30910 return false;
30912 /* When we're currently updating the window, display and current
30913 matrix usually don't agree. Arrange for a thorough display
30914 later. */
30915 if (w->must_be_updated_p)
30917 SET_FRAME_GARBAGED (f);
30918 return false;
30921 /* Frame-relative pixel rectangle of W. */
30922 wr.x = WINDOW_LEFT_EDGE_X (w);
30923 wr.y = WINDOW_TOP_EDGE_Y (w);
30924 wr.width = WINDOW_PIXEL_WIDTH (w);
30925 wr.height = WINDOW_PIXEL_HEIGHT (w);
30927 if (x_intersect_rectangles (fr, &wr, &r))
30929 int yb = window_text_bottom_y (w);
30930 struct glyph_row *row;
30931 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30933 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30934 r.x, r.y, r.width, r.height));
30936 /* Convert to window coordinates. */
30937 r.x -= WINDOW_LEFT_EDGE_X (w);
30938 r.y -= WINDOW_TOP_EDGE_Y (w);
30940 /* Turn off the cursor. */
30941 bool cursor_cleared_p = (!w->pseudo_window_p
30942 && phys_cursor_in_rect_p (w, &r));
30943 if (cursor_cleared_p)
30944 x_clear_cursor (w);
30946 /* If the row containing the cursor extends face to end of line,
30947 then expose_area might overwrite the cursor outside the
30948 rectangle and thus notice_overwritten_cursor might clear
30949 w->phys_cursor_on_p. We remember the original value and
30950 check later if it is changed. */
30951 bool phys_cursor_on_p = w->phys_cursor_on_p;
30953 /* Use a signed int intermediate value to avoid catastrophic
30954 failures due to comparison between signed and unsigned, when
30955 y0 or y1 is negative (can happen for tall images). */
30956 int r_bottom = r.y + r.height;
30958 /* Update lines intersecting rectangle R. */
30959 first_overlapping_row = last_overlapping_row = NULL;
30960 for (row = w->current_matrix->rows;
30961 row->enabled_p;
30962 ++row)
30964 int y0 = row->y;
30965 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30967 if ((y0 >= r.y && y0 < r_bottom)
30968 || (y1 > r.y && y1 < r_bottom)
30969 || (r.y >= y0 && r.y < y1)
30970 || (r_bottom > y0 && r_bottom < y1))
30972 /* A header line may be overlapping, but there is no need
30973 to fix overlapping areas for them. KFS 2005-02-12 */
30974 if (row->overlapping_p && !row->mode_line_p)
30976 if (first_overlapping_row == NULL)
30977 first_overlapping_row = row;
30978 last_overlapping_row = row;
30981 row->clip = fr;
30982 if (expose_line (w, row, &r))
30983 mouse_face_overwritten_p = true;
30984 row->clip = NULL;
30986 else if (row->overlapping_p)
30988 /* We must redraw a row overlapping the exposed area. */
30989 if (y0 < r.y
30990 ? y0 + row->phys_height > r.y
30991 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30993 if (first_overlapping_row == NULL)
30994 first_overlapping_row = row;
30995 last_overlapping_row = row;
30999 if (y1 >= yb)
31000 break;
31003 /* Display the mode line if there is one. */
31004 if (WINDOW_WANTS_MODELINE_P (w)
31005 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31006 row->enabled_p)
31007 && row->y < r_bottom)
31009 if (expose_line (w, row, &r))
31010 mouse_face_overwritten_p = true;
31013 if (!w->pseudo_window_p)
31015 /* Fix the display of overlapping rows. */
31016 if (first_overlapping_row)
31017 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31018 fr);
31020 /* Draw border between windows. */
31021 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31022 x_draw_right_divider (w);
31023 else
31024 x_draw_vertical_border (w);
31026 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31027 x_draw_bottom_divider (w);
31029 /* Turn the cursor on again. */
31030 if (cursor_cleared_p
31031 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31032 update_window_cursor (w, true);
31036 return mouse_face_overwritten_p;
31041 /* Redraw (parts) of all windows in the window tree rooted at W that
31042 intersect R. R contains frame pixel coordinates. Value is
31043 true if the exposure overwrites mouse-face. */
31045 static bool
31046 expose_window_tree (struct window *w, XRectangle *r)
31048 struct frame *f = XFRAME (w->frame);
31049 bool mouse_face_overwritten_p = false;
31051 while (w && !FRAME_GARBAGED_P (f))
31053 mouse_face_overwritten_p
31054 |= (WINDOWP (w->contents)
31055 ? expose_window_tree (XWINDOW (w->contents), r)
31056 : expose_window (w, r));
31058 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31061 return mouse_face_overwritten_p;
31065 /* EXPORT:
31066 Redisplay an exposed area of frame F. X and Y are the upper-left
31067 corner of the exposed rectangle. W and H are width and height of
31068 the exposed area. All are pixel values. W or H zero means redraw
31069 the entire frame. */
31071 void
31072 expose_frame (struct frame *f, int x, int y, int w, int h)
31074 XRectangle r;
31075 bool mouse_face_overwritten_p = false;
31077 TRACE ((stderr, "expose_frame "));
31079 /* No need to redraw if frame will be redrawn soon. */
31080 if (FRAME_GARBAGED_P (f))
31082 TRACE ((stderr, " garbaged\n"));
31083 return;
31086 /* If basic faces haven't been realized yet, there is no point in
31087 trying to redraw anything. This can happen when we get an expose
31088 event while Emacs is starting, e.g. by moving another window. */
31089 if (FRAME_FACE_CACHE (f) == NULL
31090 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31092 TRACE ((stderr, " no faces\n"));
31093 return;
31096 if (w == 0 || h == 0)
31098 r.x = r.y = 0;
31099 r.width = FRAME_TEXT_WIDTH (f);
31100 r.height = FRAME_TEXT_HEIGHT (f);
31102 else
31104 r.x = x;
31105 r.y = y;
31106 r.width = w;
31107 r.height = h;
31110 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31111 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31113 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31114 if (WINDOWP (f->tool_bar_window))
31115 mouse_face_overwritten_p
31116 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31117 #endif
31119 #ifdef HAVE_X_WINDOWS
31120 #ifndef MSDOS
31121 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31122 if (WINDOWP (f->menu_bar_window))
31123 mouse_face_overwritten_p
31124 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31125 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31126 #endif
31127 #endif
31129 /* Some window managers support a focus-follows-mouse style with
31130 delayed raising of frames. Imagine a partially obscured frame,
31131 and moving the mouse into partially obscured mouse-face on that
31132 frame. The visible part of the mouse-face will be highlighted,
31133 then the WM raises the obscured frame. With at least one WM, KDE
31134 2.1, Emacs is not getting any event for the raising of the frame
31135 (even tried with SubstructureRedirectMask), only Expose events.
31136 These expose events will draw text normally, i.e. not
31137 highlighted. Which means we must redo the highlight here.
31138 Subsume it under ``we love X''. --gerd 2001-08-15 */
31139 /* Included in Windows version because Windows most likely does not
31140 do the right thing if any third party tool offers
31141 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31142 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31144 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31145 if (f == hlinfo->mouse_face_mouse_frame)
31147 int mouse_x = hlinfo->mouse_face_mouse_x;
31148 int mouse_y = hlinfo->mouse_face_mouse_y;
31149 clear_mouse_face (hlinfo);
31150 note_mouse_highlight (f, mouse_x, mouse_y);
31156 /* EXPORT:
31157 Determine the intersection of two rectangles R1 and R2. Return
31158 the intersection in *RESULT. Value is true if RESULT is not
31159 empty. */
31161 bool
31162 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31164 XRectangle *left, *right;
31165 XRectangle *upper, *lower;
31166 bool intersection_p = false;
31168 /* Rearrange so that R1 is the left-most rectangle. */
31169 if (r1->x < r2->x)
31170 left = r1, right = r2;
31171 else
31172 left = r2, right = r1;
31174 /* X0 of the intersection is right.x0, if this is inside R1,
31175 otherwise there is no intersection. */
31176 if (right->x <= left->x + left->width)
31178 result->x = right->x;
31180 /* The right end of the intersection is the minimum of
31181 the right ends of left and right. */
31182 result->width = (min (left->x + left->width, right->x + right->width)
31183 - result->x);
31185 /* Same game for Y. */
31186 if (r1->y < r2->y)
31187 upper = r1, lower = r2;
31188 else
31189 upper = r2, lower = r1;
31191 /* The upper end of the intersection is lower.y0, if this is inside
31192 of upper. Otherwise, there is no intersection. */
31193 if (lower->y <= upper->y + upper->height)
31195 result->y = lower->y;
31197 /* The lower end of the intersection is the minimum of the lower
31198 ends of upper and lower. */
31199 result->height = (min (lower->y + lower->height,
31200 upper->y + upper->height)
31201 - result->y);
31202 intersection_p = true;
31206 return intersection_p;
31209 #endif /* HAVE_WINDOW_SYSTEM */
31212 /***********************************************************************
31213 Initialization
31214 ***********************************************************************/
31216 void
31217 syms_of_xdisp (void)
31219 Vwith_echo_area_save_vector = Qnil;
31220 staticpro (&Vwith_echo_area_save_vector);
31222 Vmessage_stack = Qnil;
31223 staticpro (&Vmessage_stack);
31225 /* Non-nil means don't actually do any redisplay. */
31226 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31228 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31230 DEFVAR_BOOL("inhibit-message", inhibit_message,
31231 doc: /* Non-nil means calls to `message' are not displayed.
31232 They are still logged to the *Messages* buffer. */);
31233 inhibit_message = 0;
31235 message_dolog_marker1 = Fmake_marker ();
31236 staticpro (&message_dolog_marker1);
31237 message_dolog_marker2 = Fmake_marker ();
31238 staticpro (&message_dolog_marker2);
31239 message_dolog_marker3 = Fmake_marker ();
31240 staticpro (&message_dolog_marker3);
31242 #ifdef GLYPH_DEBUG
31243 defsubr (&Sdump_frame_glyph_matrix);
31244 defsubr (&Sdump_glyph_matrix);
31245 defsubr (&Sdump_glyph_row);
31246 defsubr (&Sdump_tool_bar_row);
31247 defsubr (&Strace_redisplay);
31248 defsubr (&Strace_to_stderr);
31249 #endif
31250 #ifdef HAVE_WINDOW_SYSTEM
31251 defsubr (&Stool_bar_height);
31252 defsubr (&Slookup_image_map);
31253 #endif
31254 defsubr (&Sline_pixel_height);
31255 defsubr (&Sformat_mode_line);
31256 defsubr (&Sinvisible_p);
31257 defsubr (&Scurrent_bidi_paragraph_direction);
31258 defsubr (&Swindow_text_pixel_size);
31259 defsubr (&Smove_point_visually);
31260 defsubr (&Sbidi_find_overridden_directionality);
31262 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31263 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31264 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31265 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31266 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31267 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31268 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31269 DEFSYM (Qeval, "eval");
31270 DEFSYM (QCdata, ":data");
31272 /* Names of text properties relevant for redisplay. */
31273 DEFSYM (Qdisplay, "display");
31274 DEFSYM (Qspace_width, "space-width");
31275 DEFSYM (Qraise, "raise");
31276 DEFSYM (Qslice, "slice");
31277 DEFSYM (Qspace, "space");
31278 DEFSYM (Qmargin, "margin");
31279 DEFSYM (Qpointer, "pointer");
31280 DEFSYM (Qleft_margin, "left-margin");
31281 DEFSYM (Qright_margin, "right-margin");
31282 DEFSYM (Qcenter, "center");
31283 DEFSYM (Qline_height, "line-height");
31284 DEFSYM (QCalign_to, ":align-to");
31285 DEFSYM (QCrelative_width, ":relative-width");
31286 DEFSYM (QCrelative_height, ":relative-height");
31287 DEFSYM (QCeval, ":eval");
31288 DEFSYM (QCpropertize, ":propertize");
31289 DEFSYM (QCfile, ":file");
31290 DEFSYM (Qfontified, "fontified");
31291 DEFSYM (Qfontification_functions, "fontification-functions");
31293 /* Name of the face used to highlight trailing whitespace. */
31294 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31296 /* Name and number of the face used to highlight escape glyphs. */
31297 DEFSYM (Qescape_glyph, "escape-glyph");
31299 /* Name and number of the face used to highlight non-breaking
31300 spaces/hyphens. */
31301 DEFSYM (Qnobreak_space, "nobreak-space");
31302 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31304 /* The symbol 'image' which is the car of the lists used to represent
31305 images in Lisp. Also a tool bar style. */
31306 DEFSYM (Qimage, "image");
31308 /* Tool bar styles. */
31309 DEFSYM (Qtext, "text");
31310 DEFSYM (Qboth, "both");
31311 DEFSYM (Qboth_horiz, "both-horiz");
31312 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31314 /* The image map types. */
31315 DEFSYM (QCmap, ":map");
31316 DEFSYM (QCpointer, ":pointer");
31317 DEFSYM (Qrect, "rect");
31318 DEFSYM (Qcircle, "circle");
31319 DEFSYM (Qpoly, "poly");
31321 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31323 DEFSYM (Qgrow_only, "grow-only");
31324 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31325 DEFSYM (Qposition, "position");
31326 DEFSYM (Qbuffer_position, "buffer-position");
31327 DEFSYM (Qobject, "object");
31329 /* Cursor shapes. */
31330 DEFSYM (Qbar, "bar");
31331 DEFSYM (Qhbar, "hbar");
31332 DEFSYM (Qbox, "box");
31333 DEFSYM (Qhollow, "hollow");
31335 /* Pointer shapes. */
31336 DEFSYM (Qhand, "hand");
31337 DEFSYM (Qarrow, "arrow");
31338 /* also Qtext */
31340 DEFSYM (Qdragging, "dragging");
31342 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31344 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31345 staticpro (&list_of_error);
31347 /* Values of those variables at last redisplay are stored as
31348 properties on 'overlay-arrow-position' symbol. However, if
31349 Voverlay_arrow_position is a marker, last-arrow-position is its
31350 numerical position. */
31351 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31352 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31354 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31355 properties on a symbol in overlay-arrow-variable-list. */
31356 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31357 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31359 echo_buffer[0] = echo_buffer[1] = Qnil;
31360 staticpro (&echo_buffer[0]);
31361 staticpro (&echo_buffer[1]);
31363 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31364 staticpro (&echo_area_buffer[0]);
31365 staticpro (&echo_area_buffer[1]);
31367 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31368 staticpro (&Vmessages_buffer_name);
31370 mode_line_proptrans_alist = Qnil;
31371 staticpro (&mode_line_proptrans_alist);
31372 mode_line_string_list = Qnil;
31373 staticpro (&mode_line_string_list);
31374 mode_line_string_face = Qnil;
31375 staticpro (&mode_line_string_face);
31376 mode_line_string_face_prop = Qnil;
31377 staticpro (&mode_line_string_face_prop);
31378 Vmode_line_unwind_vector = Qnil;
31379 staticpro (&Vmode_line_unwind_vector);
31381 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31383 help_echo_string = Qnil;
31384 staticpro (&help_echo_string);
31385 help_echo_object = Qnil;
31386 staticpro (&help_echo_object);
31387 help_echo_window = Qnil;
31388 staticpro (&help_echo_window);
31389 previous_help_echo_string = Qnil;
31390 staticpro (&previous_help_echo_string);
31391 help_echo_pos = -1;
31393 DEFSYM (Qright_to_left, "right-to-left");
31394 DEFSYM (Qleft_to_right, "left-to-right");
31395 defsubr (&Sbidi_resolved_levels);
31397 #ifdef HAVE_WINDOW_SYSTEM
31398 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31399 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31400 For example, if a block cursor is over a tab, it will be drawn as
31401 wide as that tab on the display. */);
31402 x_stretch_cursor_p = 0;
31403 #endif
31405 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31406 doc: /* Non-nil means highlight trailing whitespace.
31407 The face used for trailing whitespace is `trailing-whitespace'. */);
31408 Vshow_trailing_whitespace = Qnil;
31410 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31411 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31412 If the value is t, Emacs highlights non-ASCII chars which have the
31413 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31414 or `nobreak-hyphen' face respectively.
31416 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31417 U+2011 (non-breaking hyphen) are affected.
31419 Any other non-nil value means to display these characters as a escape
31420 glyph followed by an ordinary space or hyphen.
31422 A value of nil means no special handling of these characters. */);
31423 Vnobreak_char_display = Qt;
31425 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31426 doc: /* The pointer shape to show in void text areas.
31427 A value of nil means to show the text pointer. Other options are
31428 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31429 `hourglass'. */);
31430 Vvoid_text_area_pointer = Qarrow;
31432 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31433 doc: /* Non-nil means don't actually do any redisplay.
31434 This is used for internal purposes. */);
31435 Vinhibit_redisplay = Qnil;
31437 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31438 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31439 Vglobal_mode_string = Qnil;
31441 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31442 doc: /* Marker for where to display an arrow on top of the buffer text.
31443 This must be the beginning of a line in order to work.
31444 See also `overlay-arrow-string'. */);
31445 Voverlay_arrow_position = Qnil;
31447 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31448 doc: /* String to display as an arrow in non-window frames.
31449 See also `overlay-arrow-position'. */);
31450 Voverlay_arrow_string = build_pure_c_string ("=>");
31452 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31453 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31454 The symbols on this list are examined during redisplay to determine
31455 where to display overlay arrows. */);
31456 Voverlay_arrow_variable_list
31457 = list1 (intern_c_string ("overlay-arrow-position"));
31459 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31460 doc: /* The number of lines to try scrolling a window by when point moves out.
31461 If that fails to bring point back on frame, point is centered instead.
31462 If this is zero, point is always centered after it moves off frame.
31463 If you want scrolling to always be a line at a time, you should set
31464 `scroll-conservatively' to a large value rather than set this to 1. */);
31466 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31467 doc: /* Scroll up to this many lines, to bring point back on screen.
31468 If point moves off-screen, redisplay will scroll by up to
31469 `scroll-conservatively' lines in order to bring point just barely
31470 onto the screen again. If that cannot be done, then redisplay
31471 recenters point as usual.
31473 If the value is greater than 100, redisplay will never recenter point,
31474 but will always scroll just enough text to bring point into view, even
31475 if you move far away.
31477 A value of zero means always recenter point if it moves off screen. */);
31478 scroll_conservatively = 0;
31480 DEFVAR_INT ("scroll-margin", scroll_margin,
31481 doc: /* Number of lines of margin at the top and bottom of a window.
31482 Recenter the window whenever point gets within this many lines
31483 of the top or bottom of the window. */);
31484 scroll_margin = 0;
31486 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31487 doc: /* Pixels per inch value for non-window system displays.
31488 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31489 Vdisplay_pixels_per_inch = make_float (72.0);
31491 #ifdef GLYPH_DEBUG
31492 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31493 #endif
31495 DEFVAR_LISP ("truncate-partial-width-windows",
31496 Vtruncate_partial_width_windows,
31497 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31498 For an integer value, truncate lines in each window narrower than the
31499 full frame width, provided the total window width in column units is less
31500 than that integer; otherwise, respect the value of `truncate-lines'.
31501 The total width of the window is as returned by `window-total-width', it
31502 includes the fringes, the continuation and truncation glyphs, the
31503 display margins (if any), and the scroll bar
31505 For any other non-nil value, truncate lines in all windows that do
31506 not span the full frame width.
31508 A value of nil means to respect the value of `truncate-lines'.
31510 If `word-wrap' is enabled, you might want to reduce this. */);
31511 Vtruncate_partial_width_windows = make_number (50);
31513 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31514 doc: /* Maximum buffer size for which line number should be displayed.
31515 If the buffer is bigger than this, the line number does not appear
31516 in the mode line. A value of nil means no limit. */);
31517 Vline_number_display_limit = Qnil;
31519 DEFVAR_INT ("line-number-display-limit-width",
31520 line_number_display_limit_width,
31521 doc: /* Maximum line width (in characters) for line number display.
31522 If the average length of the lines near point is bigger than this, then the
31523 line number may be omitted from the mode line. */);
31524 line_number_display_limit_width = 200;
31526 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31527 doc: /* Non-nil means highlight region even in nonselected windows. */);
31528 highlight_nonselected_windows = false;
31530 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31531 doc: /* Non-nil if more than one frame is visible on this display.
31532 Minibuffer-only frames don't count, but iconified frames do.
31533 This variable is not guaranteed to be accurate except while processing
31534 `frame-title-format' and `icon-title-format'. */);
31536 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31537 doc: /* Template for displaying the title bar of visible frames.
31538 \(Assuming the window manager supports this feature.)
31540 This variable has the same structure as `mode-line-format', except that
31541 the %c and %l constructs are ignored. It is used only on frames for
31542 which no explicit name has been set (see `modify-frame-parameters'). */);
31544 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31545 doc: /* Template for displaying the title bar of an iconified frame.
31546 \(Assuming the window manager supports this feature.)
31547 This variable has the same structure as `mode-line-format' (which see),
31548 and is used only on frames for which no explicit name has been set
31549 \(see `modify-frame-parameters'). */);
31550 Vicon_title_format
31551 = Vframe_title_format
31552 = listn (CONSTYPE_PURE, 3,
31553 intern_c_string ("multiple-frames"),
31554 build_pure_c_string ("%b"),
31555 listn (CONSTYPE_PURE, 4,
31556 empty_unibyte_string,
31557 intern_c_string ("invocation-name"),
31558 build_pure_c_string ("@"),
31559 intern_c_string ("system-name")));
31561 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31562 doc: /* Maximum number of lines to keep in the message log buffer.
31563 If nil, disable message logging. If t, log messages but don't truncate
31564 the buffer when it becomes large. */);
31565 Vmessage_log_max = make_number (1000);
31567 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31568 doc: /* List of functions to call before redisplaying a window with scrolling.
31569 Each function is called with two arguments, the window and its new
31570 display-start position.
31571 These functions are called whenever the `window-start' marker is modified,
31572 either to point into another buffer (e.g. via `set-window-buffer') or another
31573 place in the same buffer.
31574 Note that the value of `window-end' is not valid when these functions are
31575 called.
31577 Warning: Do not use this feature to alter the way the window
31578 is scrolled. It is not designed for that, and such use probably won't
31579 work. */);
31580 Vwindow_scroll_functions = Qnil;
31582 DEFVAR_LISP ("window-text-change-functions",
31583 Vwindow_text_change_functions,
31584 doc: /* Functions to call in redisplay when text in the window might change. */);
31585 Vwindow_text_change_functions = Qnil;
31587 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31588 doc: /* Functions called when redisplay of a window reaches the end trigger.
31589 Each function is called with two arguments, the window and the end trigger value.
31590 See `set-window-redisplay-end-trigger'. */);
31591 Vredisplay_end_trigger_functions = Qnil;
31593 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31594 doc: /* Non-nil means autoselect window with mouse pointer.
31595 If nil, do not autoselect windows.
31596 A positive number means delay autoselection by that many seconds: a
31597 window is autoselected only after the mouse has remained in that
31598 window for the duration of the delay.
31599 A negative number has a similar effect, but causes windows to be
31600 autoselected only after the mouse has stopped moving. (Because of
31601 the way Emacs compares mouse events, you will occasionally wait twice
31602 that time before the window gets selected.)
31603 Any other value means to autoselect window instantaneously when the
31604 mouse pointer enters it.
31606 Autoselection selects the minibuffer only if it is active, and never
31607 unselects the minibuffer if it is active.
31609 When customizing this variable make sure that the actual value of
31610 `focus-follows-mouse' matches the behavior of your window manager. */);
31611 Vmouse_autoselect_window = Qnil;
31613 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31614 doc: /* Non-nil means automatically resize tool-bars.
31615 This dynamically changes the tool-bar's height to the minimum height
31616 that is needed to make all tool-bar items visible.
31617 If value is `grow-only', the tool-bar's height is only increased
31618 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31619 Vauto_resize_tool_bars = Qt;
31621 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31622 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31623 auto_raise_tool_bar_buttons_p = true;
31625 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31626 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31627 make_cursor_line_fully_visible_p = true;
31629 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31630 doc: /* Border below tool-bar in pixels.
31631 If an integer, use it as the height of the border.
31632 If it is one of `internal-border-width' or `border-width', use the
31633 value of the corresponding frame parameter.
31634 Otherwise, no border is added below the tool-bar. */);
31635 Vtool_bar_border = Qinternal_border_width;
31637 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31638 doc: /* Margin around tool-bar buttons in pixels.
31639 If an integer, use that for both horizontal and vertical margins.
31640 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31641 HORZ specifying the horizontal margin, and VERT specifying the
31642 vertical margin. */);
31643 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31645 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31646 doc: /* Relief thickness of tool-bar buttons. */);
31647 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31649 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31650 doc: /* Tool bar style to use.
31651 It can be one of
31652 image - show images only
31653 text - show text only
31654 both - show both, text below image
31655 both-horiz - show text to the right of the image
31656 text-image-horiz - show text to the left of the image
31657 any other - use system default or image if no system default.
31659 This variable only affects the GTK+ toolkit version of Emacs. */);
31660 Vtool_bar_style = Qnil;
31662 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31663 doc: /* Maximum number of characters a label can have to be shown.
31664 The tool bar style must also show labels for this to have any effect, see
31665 `tool-bar-style'. */);
31666 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31668 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31669 doc: /* List of functions to call to fontify regions of text.
31670 Each function is called with one argument POS. Functions must
31671 fontify a region starting at POS in the current buffer, and give
31672 fontified regions the property `fontified'. */);
31673 Vfontification_functions = Qnil;
31674 Fmake_variable_buffer_local (Qfontification_functions);
31676 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31677 unibyte_display_via_language_environment,
31678 doc: /* Non-nil means display unibyte text according to language environment.
31679 Specifically, this means that raw bytes in the range 160-255 decimal
31680 are displayed by converting them to the equivalent multibyte characters
31681 according to the current language environment. As a result, they are
31682 displayed according to the current fontset.
31684 Note that this variable affects only how these bytes are displayed,
31685 but does not change the fact they are interpreted as raw bytes. */);
31686 unibyte_display_via_language_environment = false;
31688 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31689 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31690 If a float, it specifies a fraction of the mini-window frame's height.
31691 If an integer, it specifies a number of lines. */);
31692 Vmax_mini_window_height = make_float (0.25);
31694 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31695 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31696 A value of nil means don't automatically resize mini-windows.
31697 A value of t means resize them to fit the text displayed in them.
31698 A value of `grow-only', the default, means let mini-windows grow only;
31699 they return to their normal size when the minibuffer is closed, or the
31700 echo area becomes empty. */);
31701 /* Contrary to the doc string, we initialize this to nil, so that
31702 loading loadup.el won't try to resize windows before loading
31703 window.el, where some functions we need to call for this live.
31704 We assign the 'grow-only' value right after loading window.el
31705 during loadup. */
31706 Vresize_mini_windows = Qnil;
31708 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31709 doc: /* Alist specifying how to blink the cursor off.
31710 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31711 `cursor-type' frame-parameter or variable equals ON-STATE,
31712 comparing using `equal', Emacs uses OFF-STATE to specify
31713 how to blink it off. ON-STATE and OFF-STATE are values for
31714 the `cursor-type' frame parameter.
31716 If a frame's ON-STATE has no entry in this list,
31717 the frame's other specifications determine how to blink the cursor off. */);
31718 Vblink_cursor_alist = Qnil;
31720 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31721 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31722 If non-nil, windows are automatically scrolled horizontally to make
31723 point visible. */);
31724 automatic_hscrolling_p = true;
31725 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31727 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31728 doc: /* How many columns away from the window edge point is allowed to get
31729 before automatic hscrolling will horizontally scroll the window. */);
31730 hscroll_margin = 5;
31732 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31733 doc: /* How many columns to scroll the window when point gets too close to the edge.
31734 When point is less than `hscroll-margin' columns from the window
31735 edge, automatic hscrolling will scroll the window by the amount of columns
31736 determined by this variable. If its value is a positive integer, scroll that
31737 many columns. If it's a positive floating-point number, it specifies the
31738 fraction of the window's width to scroll. If it's nil or zero, point will be
31739 centered horizontally after the scroll. Any other value, including negative
31740 numbers, are treated as if the value were zero.
31742 Automatic hscrolling always moves point outside the scroll margin, so if
31743 point was more than scroll step columns inside the margin, the window will
31744 scroll more than the value given by the scroll step.
31746 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31747 and `scroll-right' overrides this variable's effect. */);
31748 Vhscroll_step = make_number (0);
31750 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31751 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31752 Bind this around calls to `message' to let it take effect. */);
31753 message_truncate_lines = false;
31755 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31756 doc: /* Normal hook run to update the menu bar definitions.
31757 Redisplay runs this hook before it redisplays the menu bar.
31758 This is used to update menus such as Buffers, whose contents depend on
31759 various data. */);
31760 Vmenu_bar_update_hook = Qnil;
31762 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31763 doc: /* Frame for which we are updating a menu.
31764 The enable predicate for a menu binding should check this variable. */);
31765 Vmenu_updating_frame = Qnil;
31767 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31768 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31769 inhibit_menubar_update = false;
31771 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31772 doc: /* Prefix prepended to all continuation lines at display time.
31773 The value may be a string, an image, or a stretch-glyph; it is
31774 interpreted in the same way as the value of a `display' text property.
31776 This variable is overridden by any `wrap-prefix' text or overlay
31777 property.
31779 To add a prefix to non-continuation lines, use `line-prefix'. */);
31780 Vwrap_prefix = Qnil;
31781 DEFSYM (Qwrap_prefix, "wrap-prefix");
31782 Fmake_variable_buffer_local (Qwrap_prefix);
31784 DEFVAR_LISP ("line-prefix", Vline_prefix,
31785 doc: /* Prefix prepended to all non-continuation lines at display time.
31786 The value may be a string, an image, or a stretch-glyph; it is
31787 interpreted in the same way as the value of a `display' text property.
31789 This variable is overridden by any `line-prefix' text or overlay
31790 property.
31792 To add a prefix to continuation lines, use `wrap-prefix'. */);
31793 Vline_prefix = Qnil;
31794 DEFSYM (Qline_prefix, "line-prefix");
31795 Fmake_variable_buffer_local (Qline_prefix);
31797 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31798 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31799 inhibit_eval_during_redisplay = false;
31801 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31802 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31803 inhibit_free_realized_faces = false;
31805 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31806 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31807 Intended for use during debugging and for testing bidi display;
31808 see biditest.el in the test suite. */);
31809 inhibit_bidi_mirroring = false;
31811 #ifdef GLYPH_DEBUG
31812 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31813 doc: /* Inhibit try_window_id display optimization. */);
31814 inhibit_try_window_id = false;
31816 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31817 doc: /* Inhibit try_window_reusing display optimization. */);
31818 inhibit_try_window_reusing = false;
31820 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31821 doc: /* Inhibit try_cursor_movement display optimization. */);
31822 inhibit_try_cursor_movement = false;
31823 #endif /* GLYPH_DEBUG */
31825 DEFVAR_INT ("overline-margin", overline_margin,
31826 doc: /* Space between overline and text, in pixels.
31827 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31828 margin to the character height. */);
31829 overline_margin = 2;
31831 DEFVAR_INT ("underline-minimum-offset",
31832 underline_minimum_offset,
31833 doc: /* Minimum distance between baseline and underline.
31834 This can improve legibility of underlined text at small font sizes,
31835 particularly when using variable `x-use-underline-position-properties'
31836 with fonts that specify an UNDERLINE_POSITION relatively close to the
31837 baseline. The default value is 1. */);
31838 underline_minimum_offset = 1;
31840 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31841 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31842 This feature only works when on a window system that can change
31843 cursor shapes. */);
31844 display_hourglass_p = true;
31846 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31847 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31848 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31850 #ifdef HAVE_WINDOW_SYSTEM
31851 hourglass_atimer = NULL;
31852 hourglass_shown_p = false;
31853 #endif /* HAVE_WINDOW_SYSTEM */
31855 /* Name of the face used to display glyphless characters. */
31856 DEFSYM (Qglyphless_char, "glyphless-char");
31858 /* Method symbols for Vglyphless_char_display. */
31859 DEFSYM (Qhex_code, "hex-code");
31860 DEFSYM (Qempty_box, "empty-box");
31861 DEFSYM (Qthin_space, "thin-space");
31862 DEFSYM (Qzero_width, "zero-width");
31864 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31865 doc: /* Function run just before redisplay.
31866 It is called with one argument, which is the set of windows that are to
31867 be redisplayed. This set can be nil (meaning, only the selected window),
31868 or t (meaning all windows). */);
31869 Vpre_redisplay_function = intern ("ignore");
31871 /* Symbol for the purpose of Vglyphless_char_display. */
31872 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31873 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31875 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31876 doc: /* Char-table defining glyphless characters.
31877 Each element, if non-nil, should be one of the following:
31878 an ASCII acronym string: display this string in a box
31879 `hex-code': display the hexadecimal code of a character in a box
31880 `empty-box': display as an empty box
31881 `thin-space': display as 1-pixel width space
31882 `zero-width': don't display
31883 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31884 display method for graphical terminals and text terminals respectively.
31885 GRAPHICAL and TEXT should each have one of the values listed above.
31887 The char-table has one extra slot to control the display of a character for
31888 which no font is found. This slot only takes effect on graphical terminals.
31889 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31890 `thin-space'. The default is `empty-box'.
31892 If a character has a non-nil entry in an active display table, the
31893 display table takes effect; in this case, Emacs does not consult
31894 `glyphless-char-display' at all. */);
31895 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31896 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31897 Qempty_box);
31899 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31900 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31901 Vdebug_on_message = Qnil;
31903 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31904 doc: /* */);
31905 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31907 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31908 doc: /* */);
31909 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31911 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31912 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31913 Vredisplay__variables = Qnil;
31915 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31916 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31917 /* Initialize to t, since we need to disable reordering until
31918 loadup.el successfully loads charprop.el. */
31919 redisplay__inhibit_bidi = true;
31923 /* Initialize this module when Emacs starts. */
31925 void
31926 init_xdisp (void)
31928 CHARPOS (this_line_start_pos) = 0;
31930 if (!noninteractive)
31932 struct window *m = XWINDOW (minibuf_window);
31933 Lisp_Object frame = m->frame;
31934 struct frame *f = XFRAME (frame);
31935 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31936 struct window *r = XWINDOW (root);
31937 int i;
31939 echo_area_window = minibuf_window;
31941 r->top_line = FRAME_TOP_MARGIN (f);
31942 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31943 r->total_cols = FRAME_COLS (f);
31944 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31945 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31946 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31948 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31949 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31950 m->total_cols = FRAME_COLS (f);
31951 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31952 m->total_lines = 1;
31953 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31955 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31956 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31957 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31959 /* The default ellipsis glyphs `...'. */
31960 for (i = 0; i < 3; ++i)
31961 default_invis_vector[i] = make_number ('.');
31965 /* Allocate the buffer for frame titles.
31966 Also used for `format-mode-line'. */
31967 int size = 100;
31968 mode_line_noprop_buf = xmalloc (size);
31969 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31970 mode_line_noprop_ptr = mode_line_noprop_buf;
31971 mode_line_target = MODE_LINE_DISPLAY;
31974 help_echo_showing_p = false;
31977 #ifdef HAVE_WINDOW_SYSTEM
31979 /* Platform-independent portion of hourglass implementation. */
31981 /* Timer function of hourglass_atimer. */
31983 static void
31984 show_hourglass (struct atimer *timer)
31986 /* The timer implementation will cancel this timer automatically
31987 after this function has run. Set hourglass_atimer to null
31988 so that we know the timer doesn't have to be canceled. */
31989 hourglass_atimer = NULL;
31991 if (!hourglass_shown_p)
31993 Lisp_Object tail, frame;
31995 block_input ();
31997 FOR_EACH_FRAME (tail, frame)
31999 struct frame *f = XFRAME (frame);
32001 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32002 && FRAME_RIF (f)->show_hourglass)
32003 FRAME_RIF (f)->show_hourglass (f);
32006 hourglass_shown_p = true;
32007 unblock_input ();
32011 /* Cancel a currently active hourglass timer, and start a new one. */
32013 void
32014 start_hourglass (void)
32016 struct timespec delay;
32018 cancel_hourglass ();
32020 if (INTEGERP (Vhourglass_delay)
32021 && XINT (Vhourglass_delay) > 0)
32022 delay = make_timespec (min (XINT (Vhourglass_delay),
32023 TYPE_MAXIMUM (time_t)),
32025 else if (FLOATP (Vhourglass_delay)
32026 && XFLOAT_DATA (Vhourglass_delay) > 0)
32027 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32028 else
32029 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32031 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32032 show_hourglass, NULL);
32035 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32036 shown. */
32038 void
32039 cancel_hourglass (void)
32041 if (hourglass_atimer)
32043 cancel_atimer (hourglass_atimer);
32044 hourglass_atimer = NULL;
32047 if (hourglass_shown_p)
32049 Lisp_Object tail, frame;
32051 block_input ();
32053 FOR_EACH_FRAME (tail, frame)
32055 struct frame *f = XFRAME (frame);
32057 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32058 && FRAME_RIF (f)->hide_hourglass)
32059 FRAME_RIF (f)->hide_hourglass (f);
32060 #ifdef HAVE_NTGUI
32061 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32062 else if (!FRAME_W32_P (f))
32063 w32_arrow_cursor ();
32064 #endif
32067 hourglass_shown_p = false;
32068 unblock_input ();
32072 #endif /* HAVE_WINDOW_SYSTEM */