* src/xdisp.c (block_buffer_flips, unblock_buffer_flips): Now static.
[emacs.git] / src / xdisp.c
blob2acdfa90b7a385fcdaa541643ded0215f386b2a5
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 void
626 maybe_set_redisplay (Lisp_Object symbol)
628 if (HASH_TABLE_P (Vredisplay__variables)
629 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void block_buffer_flips (void);
822 static void unblock_buffer_flips (void);
823 static void redisplay_windows (Lisp_Object);
824 static void redisplay_window (Lisp_Object, bool);
825 static Lisp_Object redisplay_window_error (Lisp_Object);
826 static Lisp_Object redisplay_window_0 (Lisp_Object);
827 static Lisp_Object redisplay_window_1 (Lisp_Object);
828 static bool set_cursor_from_row (struct window *, struct glyph_row *,
829 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
830 int, int);
831 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
832 static bool update_menu_bar (struct frame *, bool, bool);
833 static bool try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static bool display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
839 Lisp_Object, bool);
840 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
841 Lisp_Object);
842 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
843 static void display_menu_bar (struct window *);
844 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
845 ptrdiff_t *);
846 static int display_string (const char *, Lisp_Object, Lisp_Object,
847 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
848 static void compute_line_metrics (struct it *);
849 static void run_redisplay_end_trigger_hook (struct it *);
850 static bool get_overlay_strings (struct it *, ptrdiff_t);
851 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
852 static void next_overlay_string (struct it *);
853 static void reseat (struct it *, struct text_pos, bool);
854 static void reseat_1 (struct it *, struct text_pos, bool);
855 static bool next_element_from_display_vector (struct it *);
856 static bool next_element_from_string (struct it *);
857 static bool next_element_from_c_string (struct it *);
858 static bool next_element_from_buffer (struct it *);
859 static bool next_element_from_composition (struct it *);
860 static bool next_element_from_image (struct it *);
861 static bool next_element_from_stretch (struct it *);
862 static bool next_element_from_xwidget (struct it *);
863 static void load_overlay_strings (struct it *, ptrdiff_t);
864 static bool get_next_display_element (struct it *);
865 static enum move_it_result
866 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
867 enum move_operation_enum);
868 static void get_visually_first_element (struct it *);
869 static void compute_stop_pos (struct it *);
870 static int face_before_or_after_it_pos (struct it *, bool);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, bool);
877 static int underlying_face_id (struct it *);
879 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
880 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
882 #ifdef HAVE_WINDOW_SYSTEM
884 static void update_tool_bar (struct frame *, bool);
885 static void x_draw_bottom_divider (struct window *w);
886 static void notice_overwritten_cursor (struct window *,
887 enum glyph_row_area,
888 int, int, int, int);
889 static int normal_char_height (struct font *, int);
890 static void normal_char_ascent_descent (struct font *, int, int *, int *);
892 static void append_stretch_glyph (struct it *, Lisp_Object,
893 int, int, int);
895 static Lisp_Object get_it_property (struct it *, Lisp_Object);
896 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
897 struct font *, int, bool);
899 #endif /* HAVE_WINDOW_SYSTEM */
901 static void produce_special_glyphs (struct it *, enum display_element_type);
902 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
903 static bool coords_in_mouse_face_p (struct window *, int, int);
907 /***********************************************************************
908 Window display dimensions
909 ***********************************************************************/
911 /* Return the bottom boundary y-position for text lines in window W.
912 This is the first y position at which a line cannot start.
913 It is relative to the top of the window.
915 This is the height of W minus the height of a mode line, if any. */
918 window_text_bottom_y (struct window *w)
920 int height = WINDOW_PIXEL_HEIGHT (w);
922 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
927 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
929 return height;
932 /* Return the pixel width of display area AREA of window W.
933 ANY_AREA means return the total width of W, not including
934 fringes to the left and right of the window. */
937 window_box_width (struct window *w, enum glyph_row_area area)
939 int width = w->pixel_width;
941 if (!w->pseudo_window_p)
943 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
944 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
946 if (area == TEXT_AREA)
947 width -= (WINDOW_MARGINS_WIDTH (w)
948 + WINDOW_FRINGES_WIDTH (w));
949 else if (area == LEFT_MARGIN_AREA)
950 width = WINDOW_LEFT_MARGIN_WIDTH (w);
951 else if (area == RIGHT_MARGIN_AREA)
952 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
955 /* With wide margins, fringes, etc. we might end up with a negative
956 width, correct that here. */
957 return max (0, width);
961 /* Return the pixel height of the display area of window W, not
962 including mode lines of W, if any. */
965 window_box_height (struct window *w)
967 struct frame *f = XFRAME (w->frame);
968 int height = WINDOW_PIXEL_HEIGHT (w);
970 eassert (height >= 0);
972 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
973 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
975 /* Note: the code below that determines the mode-line/header-line
976 height is essentially the same as that contained in the macro
977 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
978 the appropriate glyph row has its `mode_line_p' flag set,
979 and if it doesn't, uses estimate_mode_line_height instead. */
981 if (WINDOW_WANTS_MODELINE_P (w))
983 struct glyph_row *ml_row
984 = (w->current_matrix && w->current_matrix->rows
985 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
986 : 0);
987 if (ml_row && ml_row->mode_line_p)
988 height -= ml_row->height;
989 else
990 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
993 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 struct glyph_row *hl_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
998 : 0);
999 if (hl_row && hl_row->mode_line_p)
1000 height -= hl_row->height;
1001 else
1002 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1005 /* With a very small font and a mode-line that's taller than
1006 default, we might end up with a negative height. */
1007 return max (0, height);
1010 /* Return the window-relative coordinate of the left edge of display
1011 area AREA of window W. ANY_AREA means return the left edge of the
1012 whole window, to the right of the left fringe of W. */
1015 window_box_left_offset (struct window *w, enum glyph_row_area area)
1017 int x;
1019 if (w->pseudo_window_p)
1020 return 0;
1022 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1024 if (area == TEXT_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA));
1027 else if (area == RIGHT_MARGIN_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA)
1030 + window_box_width (w, TEXT_AREA)
1031 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1033 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1034 else if (area == LEFT_MARGIN_AREA
1035 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1038 /* Don't return more than the window's pixel width. */
1039 return min (x, w->pixel_width);
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. ANY_AREA means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1047 static int
1048 window_box_right_offset (struct window *w, enum glyph_row_area area)
1050 /* Don't return more than the window's pixel width. */
1051 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1052 w->pixel_width);
1055 /* Return the frame-relative coordinate of the left edge of display
1056 area AREA of window W. ANY_AREA means return the left edge of the
1057 whole window, to the right of the left fringe of W. */
1060 window_box_left (struct window *w, enum glyph_row_area area)
1062 struct frame *f = XFRAME (w->frame);
1063 int x;
1065 if (w->pseudo_window_p)
1066 return FRAME_INTERNAL_BORDER_WIDTH (f);
1068 x = (WINDOW_LEFT_EDGE_X (w)
1069 + window_box_left_offset (w, area));
1071 return x;
1075 /* Return the frame-relative coordinate of the right edge of display
1076 area AREA of window W. ANY_AREA means return the right edge of the
1077 whole window, to the left of the right fringe of W. */
1080 window_box_right (struct window *w, enum glyph_row_area area)
1082 return window_box_left (w, area) + window_box_width (w, area);
1085 /* Get the bounding box of the display area AREA of window W, without
1086 mode lines, in frame-relative coordinates. ANY_AREA means the
1087 whole window, not including the left and right fringes of
1088 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1089 coordinates of the upper-left corner of the box. Return in
1090 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1092 void
1093 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1094 int *box_y, int *box_width, int *box_height)
1096 if (box_width)
1097 *box_width = window_box_width (w, area);
1098 if (box_height)
1099 *box_height = window_box_height (w);
1100 if (box_x)
1101 *box_x = window_box_left (w, area);
1102 if (box_y)
1104 *box_y = WINDOW_TOP_EDGE_Y (w);
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1110 #ifdef HAVE_WINDOW_SYSTEM
1112 /* Get the bounding box of the display area AREA of window W, without
1113 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1114 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1115 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1116 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1117 box. */
1119 static void
1120 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1121 int *bottom_right_x, int *bottom_right_y)
1123 window_box (w, ANY_AREA, top_left_x, top_left_y,
1124 bottom_right_x, bottom_right_y);
1125 *bottom_right_x += *top_left_x;
1126 *bottom_right_y += *top_left_y;
1129 #endif /* HAVE_WINDOW_SYSTEM */
1131 /***********************************************************************
1132 Utilities
1133 ***********************************************************************/
1135 /* Return the bottom y-position of the line the iterator IT is in.
1136 This can modify IT's settings. */
1139 line_bottom_y (struct it *it)
1141 int line_height = it->max_ascent + it->max_descent;
1142 int line_top_y = it->current_y;
1144 if (line_height == 0)
1146 if (last_height)
1147 line_height = last_height;
1148 else if (IT_CHARPOS (*it) < ZV)
1150 move_it_by_lines (it, 1);
1151 line_height = (it->max_ascent || it->max_descent
1152 ? it->max_ascent + it->max_descent
1153 : last_height);
1155 else
1157 struct glyph_row *row = it->glyph_row;
1159 /* Use the default character height. */
1160 it->glyph_row = NULL;
1161 it->what = IT_CHARACTER;
1162 it->c = ' ';
1163 it->len = 1;
1164 PRODUCE_GLYPHS (it);
1165 line_height = it->ascent + it->descent;
1166 it->glyph_row = row;
1170 return line_top_y + line_height;
1173 DEFUN ("line-pixel-height", Fline_pixel_height,
1174 Sline_pixel_height, 0, 0, 0,
1175 doc: /* Return height in pixels of text line in the selected window.
1177 Value is the height in pixels of the line at point. */)
1178 (void)
1180 struct it it;
1181 struct text_pos pt;
1182 struct window *w = XWINDOW (selected_window);
1183 struct buffer *old_buffer = NULL;
1184 Lisp_Object result;
1186 if (XBUFFER (w->contents) != current_buffer)
1188 old_buffer = current_buffer;
1189 set_buffer_internal_1 (XBUFFER (w->contents));
1191 SET_TEXT_POS (pt, PT, PT_BYTE);
1192 start_display (&it, w, pt);
1193 it.vpos = it.current_y = 0;
1194 last_height = 0;
1195 result = make_number (line_bottom_y (&it));
1196 if (old_buffer)
1197 set_buffer_internal_1 (old_buffer);
1199 return result;
1202 /* Return the default pixel height of text lines in window W. The
1203 value is the canonical height of the W frame's default font, plus
1204 any extra space required by the line-spacing variable or frame
1205 parameter.
1207 Implementation note: this ignores any line-spacing text properties
1208 put on the newline characters. This is because those properties
1209 only affect the _screen_ line ending in the newline (i.e., in a
1210 continued line, only the last screen line will be affected), which
1211 means only a small number of lines in a buffer can ever use this
1212 feature. Since this function is used to compute the default pixel
1213 equivalent of text lines in a window, we can safely ignore those
1214 few lines. For the same reasons, we ignore the line-height
1215 properties. */
1217 default_line_pixel_height (struct window *w)
1219 struct frame *f = WINDOW_XFRAME (w);
1220 int height = FRAME_LINE_HEIGHT (f);
1222 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1224 struct buffer *b = XBUFFER (w->contents);
1225 Lisp_Object val = BVAR (b, extra_line_spacing);
1227 if (NILP (val))
1228 val = BVAR (&buffer_defaults, extra_line_spacing);
1229 if (!NILP (val))
1231 if (RANGED_INTEGERP (0, val, INT_MAX))
1232 height += XFASTINT (val);
1233 else if (FLOATP (val))
1235 int addon = XFLOAT_DATA (val) * height + 0.5;
1237 if (addon >= 0)
1238 height += addon;
1241 else
1242 height += f->extra_line_spacing;
1245 return height;
1248 /* Subroutine of pos_visible_p below. Extracts a display string, if
1249 any, from the display spec given as its argument. */
1250 static Lisp_Object
1251 string_from_display_spec (Lisp_Object spec)
1253 if (CONSP (spec))
1255 while (CONSP (spec))
1257 if (STRINGP (XCAR (spec)))
1258 return XCAR (spec);
1259 spec = XCDR (spec);
1262 else if (VECTORP (spec))
1264 ptrdiff_t i;
1266 for (i = 0; i < ASIZE (spec); i++)
1268 if (STRINGP (AREF (spec, i)))
1269 return AREF (spec, i);
1271 return Qnil;
1274 return spec;
1278 /* Limit insanely large values of W->hscroll on frame F to the largest
1279 value that will still prevent first_visible_x and last_visible_x of
1280 'struct it' from overflowing an int. */
1281 static int
1282 window_hscroll_limited (struct window *w, struct frame *f)
1284 ptrdiff_t window_hscroll = w->hscroll;
1285 int window_text_width = window_box_width (w, TEXT_AREA);
1286 int colwidth = FRAME_COLUMN_WIDTH (f);
1288 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1289 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1291 return window_hscroll;
1294 /* Return true if position CHARPOS is visible in window W.
1295 CHARPOS < 0 means return info about WINDOW_END position.
1296 If visible, set *X and *Y to pixel coordinates of top left corner.
1297 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1298 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1300 bool
1301 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1302 int *rtop, int *rbot, int *rowh, int *vpos)
1304 struct it it;
1305 void *itdata = bidi_shelve_cache ();
1306 struct text_pos top;
1307 bool visible_p = false;
1308 struct buffer *old_buffer = NULL;
1309 bool r2l = false;
1311 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1312 return visible_p;
1314 if (XBUFFER (w->contents) != current_buffer)
1316 old_buffer = current_buffer;
1317 set_buffer_internal_1 (XBUFFER (w->contents));
1320 SET_TEXT_POS_FROM_MARKER (top, w->start);
1321 /* Scrolling a minibuffer window via scroll bar when the echo area
1322 shows long text sometimes resets the minibuffer contents behind
1323 our backs. Also, someone might narrow-to-region and immediately
1324 call a scroll function. */
1325 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1328 /* If the top of the window is after CHARPOS, the latter is surely
1329 not visible. */
1330 if (charpos >= 0 && CHARPOS (top) > charpos)
1331 return visible_p;
1333 /* Compute exact mode line heights. */
1334 if (WINDOW_WANTS_MODELINE_P (w))
1335 w->mode_line_height
1336 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1337 BVAR (current_buffer, mode_line_format));
1339 if (WINDOW_WANTS_HEADER_LINE_P (w))
1340 w->header_line_height
1341 = display_mode_line (w, HEADER_LINE_FACE_ID,
1342 BVAR (current_buffer, header_line_format));
1344 start_display (&it, w, top);
1345 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1346 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1348 if (charpos >= 0
1349 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1350 && IT_CHARPOS (it) >= charpos)
1351 /* When scanning backwards under bidi iteration, move_it_to
1352 stops at or _before_ CHARPOS, because it stops at or to
1353 the _right_ of the character at CHARPOS. */
1354 || (it.bidi_p && it.bidi_it.scan_dir == -1
1355 && IT_CHARPOS (it) <= charpos)))
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1359 or covered by a display property, move_it_to stops at the end
1360 of the invisible text, to the right of CHARPOS. (ii) If
1361 CHARPOS is in a display vector, move_it_to stops on its last
1362 glyph. */
1363 int top_x = it.current_x;
1364 int top_y = it.current_y;
1365 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1366 int bottom_y;
1367 struct it save_it;
1368 void *save_it_data = NULL;
1370 /* Calling line_bottom_y may change it.method, it.position, etc. */
1371 SAVE_IT (save_it, it, save_it_data);
1372 last_height = 0;
1373 bottom_y = line_bottom_y (&it);
1374 if (top_y < window_top_y)
1375 visible_p = bottom_y > window_top_y;
1376 else if (top_y < it.last_visible_y)
1377 visible_p = true;
1378 if (bottom_y >= it.last_visible_y
1379 && it.bidi_p && it.bidi_it.scan_dir == -1
1380 && IT_CHARPOS (it) < charpos)
1382 /* When the last line of the window is scanned backwards
1383 under bidi iteration, we could be duped into thinking
1384 that we have passed CHARPOS, when in fact move_it_to
1385 simply stopped short of CHARPOS because it reached
1386 last_visible_y. To see if that's what happened, we call
1387 move_it_to again with a slightly larger vertical limit,
1388 and see if it actually moved vertically; if it did, we
1389 didn't really reach CHARPOS, which is beyond window end. */
1390 /* Why 10? because we don't know how many canonical lines
1391 will the height of the next line(s) be. So we guess. */
1392 int ten_more_lines = 10 * default_line_pixel_height (w);
1394 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1395 MOVE_TO_POS | MOVE_TO_Y);
1396 if (it.current_y > top_y)
1397 visible_p = false;
1400 RESTORE_IT (&it, &save_it, save_it_data);
1401 if (visible_p)
1403 if (it.method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2, it2_prev;
1412 /* The idea is to get to the previous buffer
1413 position, consume the character there, and use
1414 the pixel coordinates we get after that. But if
1415 the previous buffer position is also displayed
1416 from a display vector, we need to consume all of
1417 the glyphs from that display vector. */
1418 start_display (&it2, w, top);
1419 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* If we didn't get to CHARPOS - 1, there's some
1421 replacing display property at that position, and
1422 we stopped after it. That is exactly the place
1423 whose coordinates we want. */
1424 if (IT_CHARPOS (it2) != charpos - 1)
1425 it2_prev = it2;
1426 else
1428 /* Iterate until we get out of the display
1429 vector that displays the character at
1430 CHARPOS - 1. */
1431 do {
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 it2_prev = it2;
1435 set_iterator_to_next (&it2, true);
1436 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1437 && IT_CHARPOS (it2) < charpos);
1439 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1440 || it2_prev.current_x > it2_prev.last_visible_x)
1441 top_x = it.glyph_row->x;
1442 else
1444 top_x = it2_prev.current_x;
1445 top_y = it2_prev.current_y;
1449 else if (IT_CHARPOS (it) != charpos)
1451 Lisp_Object cpos = make_number (charpos);
1452 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1453 Lisp_Object string = string_from_display_spec (spec);
1454 struct text_pos tpos;
1455 bool newline_in_string
1456 = (STRINGP (string)
1457 && memchr (SDATA (string), '\n', SBYTES (string)));
1459 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1460 bool replacing_spec_p
1461 = (!NILP (spec)
1462 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1463 charpos, FRAME_WINDOW_P (it.f)));
1464 /* The tricky code below is needed because there's a
1465 discrepancy between move_it_to and how we set cursor
1466 when PT is at the beginning of a portion of text
1467 covered by a display property or an overlay with a
1468 display property, or the display line ends in a
1469 newline from a display string. move_it_to will stop
1470 _after_ such display strings, whereas
1471 set_cursor_from_row conspires with cursor_row_p to
1472 place the cursor on the first glyph produced from the
1473 display string. */
1475 /* We have overshoot PT because it is covered by a
1476 display property that replaces the text it covers.
1477 If the string includes embedded newlines, we are also
1478 in the wrong display line. Backtrack to the correct
1479 line, where the display property begins. */
1480 if (replacing_spec_p)
1482 Lisp_Object startpos, endpos;
1483 EMACS_INT start, end;
1484 struct it it3;
1486 /* Find the first and the last buffer positions
1487 covered by the display string. */
1488 endpos =
1489 Fnext_single_char_property_change (cpos, Qdisplay,
1490 Qnil, Qnil);
1491 startpos =
1492 Fprevious_single_char_property_change (endpos, Qdisplay,
1493 Qnil, Qnil);
1494 start = XFASTINT (startpos);
1495 end = XFASTINT (endpos);
1496 /* Move to the last buffer position before the
1497 display property. */
1498 start_display (&it3, w, top);
1499 if (start > CHARPOS (top))
1500 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1501 /* Move forward one more line if the position before
1502 the display string is a newline or if it is the
1503 rightmost character on a line that is
1504 continued or word-wrapped. */
1505 if (it3.method == GET_FROM_BUFFER
1506 && (it3.c == '\n'
1507 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1508 move_it_by_lines (&it3, 1);
1509 else if (move_it_in_display_line_to (&it3, -1,
1510 it3.current_x
1511 + it3.pixel_width,
1512 MOVE_TO_X)
1513 == MOVE_LINE_CONTINUED)
1515 move_it_by_lines (&it3, 1);
1516 /* When we are under word-wrap, the #$@%!
1517 move_it_by_lines moves 2 lines, so we need to
1518 fix that up. */
1519 if (it3.line_wrap == WORD_WRAP)
1520 move_it_by_lines (&it3, -1);
1523 /* Record the vertical coordinate of the display
1524 line where we wound up. */
1525 top_y = it3.current_y;
1526 if (it3.bidi_p)
1528 /* When characters are reordered for display,
1529 the character displayed to the left of the
1530 display string could be _after_ the display
1531 property in the logical order. Use the
1532 smallest vertical position of these two. */
1533 start_display (&it3, w, top);
1534 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1535 if (it3.current_y < top_y)
1536 top_y = it3.current_y;
1538 /* Move from the top of the window to the beginning
1539 of the display line where the display string
1540 begins. */
1541 start_display (&it3, w, top);
1542 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1543 /* If it3_moved stays false after the 'while' loop
1544 below, that means we already were at a newline
1545 before the loop (e.g., the display string begins
1546 with a newline), so we don't need to (and cannot)
1547 inspect the glyphs of it3.glyph_row, because
1548 PRODUCE_GLYPHS will not produce anything for a
1549 newline, and thus it3.glyph_row stays at its
1550 stale content it got at top of the window. */
1551 bool it3_moved = false;
1552 /* Finally, advance the iterator until we hit the
1553 first display element whose character position is
1554 CHARPOS, or until the first newline from the
1555 display string, which signals the end of the
1556 display line. */
1557 while (get_next_display_element (&it3))
1559 PRODUCE_GLYPHS (&it3);
1560 if (IT_CHARPOS (it3) == charpos
1561 || ITERATOR_AT_END_OF_LINE_P (&it3))
1562 break;
1563 it3_moved = true;
1564 set_iterator_to_next (&it3, false);
1566 top_x = it3.current_x - it3.pixel_width;
1567 /* Normally, we would exit the above loop because we
1568 found the display element whose character
1569 position is CHARPOS. For the contingency that we
1570 didn't, and stopped at the first newline from the
1571 display string, move back over the glyphs
1572 produced from the string, until we find the
1573 rightmost glyph not from the string. */
1574 if (it3_moved
1575 && newline_in_string
1576 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1578 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1579 + it3.glyph_row->used[TEXT_AREA];
1581 while (EQ ((g - 1)->object, string))
1583 --g;
1584 top_x -= g->pixel_width;
1586 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1587 + it3.glyph_row->used[TEXT_AREA]);
1592 *x = top_x;
1593 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1594 *rtop = max (0, window_top_y - top_y);
1595 *rbot = max (0, bottom_y - it.last_visible_y);
1596 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1597 - max (top_y, window_top_y)));
1598 *vpos = it.vpos;
1599 if (it.bidi_it.paragraph_dir == R2L)
1600 r2l = true;
1603 else
1605 /* Either we were asked to provide info about WINDOW_END, or
1606 CHARPOS is in the partially visible glyph row at end of
1607 window. */
1608 struct it it2;
1609 void *it2data = NULL;
1611 SAVE_IT (it2, it, it2data);
1612 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1613 move_it_by_lines (&it, 1);
1614 if (charpos < IT_CHARPOS (it)
1615 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1617 visible_p = true;
1618 RESTORE_IT (&it2, &it2, it2data);
1619 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1620 *x = it2.current_x;
1621 *y = it2.current_y + it2.max_ascent - it2.ascent;
1622 *rtop = max (0, -it2.current_y);
1623 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1624 - it.last_visible_y));
1625 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1626 it.last_visible_y)
1627 - max (it2.current_y,
1628 WINDOW_HEADER_LINE_HEIGHT (w))));
1629 *vpos = it2.vpos;
1630 if (it2.bidi_it.paragraph_dir == R2L)
1631 r2l = true;
1633 else
1634 bidi_unshelve_cache (it2data, true);
1636 bidi_unshelve_cache (itdata, false);
1638 if (old_buffer)
1639 set_buffer_internal_1 (old_buffer);
1641 if (visible_p)
1643 if (w->hscroll > 0)
1644 *x -=
1645 window_hscroll_limited (w, WINDOW_XFRAME (w))
1646 * WINDOW_FRAME_COLUMN_WIDTH (w);
1647 /* For lines in an R2L paragraph, we need to mirror the X pixel
1648 coordinate wrt the text area. For the reasons, see the
1649 commentary in buffer_posn_from_coords and the explanation of
1650 the geometry used by the move_it_* functions at the end of
1651 the large commentary near the beginning of this file. */
1652 if (r2l)
1653 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1656 #if false
1657 /* Debugging code. */
1658 if (visible_p)
1659 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1660 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1661 else
1662 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1663 #endif
1665 return visible_p;
1669 /* Return the next character from STR. Return in *LEN the length of
1670 the character. This is like STRING_CHAR_AND_LENGTH but never
1671 returns an invalid character. If we find one, we return a `?', but
1672 with the length of the invalid character. */
1674 static int
1675 string_char_and_length (const unsigned char *str, int *len)
1677 int c;
1679 c = STRING_CHAR_AND_LENGTH (str, *len);
1680 if (!CHAR_VALID_P (c))
1681 /* We may not change the length here because other places in Emacs
1682 don't use this function, i.e. they silently accept invalid
1683 characters. */
1684 c = '?';
1686 return c;
1691 /* Given a position POS containing a valid character and byte position
1692 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1694 static struct text_pos
1695 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1697 eassert (STRINGP (string) && nchars >= 0);
1699 if (STRING_MULTIBYTE (string))
1701 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1702 int len;
1704 while (nchars--)
1706 string_char_and_length (p, &len);
1707 p += len;
1708 CHARPOS (pos) += 1;
1709 BYTEPOS (pos) += len;
1712 else
1713 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1715 return pos;
1719 /* Value is the text position, i.e. character and byte position,
1720 for character position CHARPOS in STRING. */
1722 static struct text_pos
1723 string_pos (ptrdiff_t charpos, Lisp_Object string)
1725 struct text_pos pos;
1726 eassert (STRINGP (string));
1727 eassert (charpos >= 0);
1728 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1729 return pos;
1733 /* Value is a text position, i.e. character and byte position, for
1734 character position CHARPOS in C string S. MULTIBYTE_P
1735 means recognize multibyte characters. */
1737 static struct text_pos
1738 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1740 struct text_pos pos;
1742 eassert (s != NULL);
1743 eassert (charpos >= 0);
1745 if (multibyte_p)
1747 int len;
1749 SET_TEXT_POS (pos, 0, 0);
1750 while (charpos--)
1752 string_char_and_length ((const unsigned char *) s, &len);
1753 s += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1758 else
1759 SET_TEXT_POS (pos, charpos, charpos);
1761 return pos;
1765 /* Value is the number of characters in C string S. MULTIBYTE_P
1766 means recognize multibyte characters. */
1768 static ptrdiff_t
1769 number_of_chars (const char *s, bool multibyte_p)
1771 ptrdiff_t nchars;
1773 if (multibyte_p)
1775 ptrdiff_t rest = strlen (s);
1776 int len;
1777 const unsigned char *p = (const unsigned char *) s;
1779 for (nchars = 0; rest > 0; ++nchars)
1781 string_char_and_length (p, &len);
1782 rest -= len, p += len;
1785 else
1786 nchars = strlen (s);
1788 return nchars;
1792 /* Compute byte position NEWPOS->bytepos corresponding to
1793 NEWPOS->charpos. POS is a known position in string STRING.
1794 NEWPOS->charpos must be >= POS.charpos. */
1796 static void
1797 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1799 eassert (STRINGP (string));
1800 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1802 if (STRING_MULTIBYTE (string))
1803 *newpos = string_pos_nchars_ahead (pos, string,
1804 CHARPOS (*newpos) - CHARPOS (pos));
1805 else
1806 BYTEPOS (*newpos) = CHARPOS (*newpos);
1809 /* EXPORT:
1810 Return an estimation of the pixel height of mode or header lines on
1811 frame F. FACE_ID specifies what line's height to estimate. */
1814 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1816 #ifdef HAVE_WINDOW_SYSTEM
1817 if (FRAME_WINDOW_P (f))
1819 int height = FONT_HEIGHT (FRAME_FONT (f));
1821 /* This function is called so early when Emacs starts that the face
1822 cache and mode line face are not yet initialized. */
1823 if (FRAME_FACE_CACHE (f))
1825 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1826 if (face)
1828 if (face->font)
1829 height = normal_char_height (face->font, -1);
1830 if (face->box_line_width > 0)
1831 height += 2 * face->box_line_width;
1835 return height;
1837 #endif
1839 return 1;
1842 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1843 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1844 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1845 not force the value into range. */
1847 void
1848 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1849 NativeRectangle *bounds, bool noclip)
1852 #ifdef HAVE_WINDOW_SYSTEM
1853 if (FRAME_WINDOW_P (f))
1855 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1856 even for negative values. */
1857 if (pix_x < 0)
1858 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1859 if (pix_y < 0)
1860 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1862 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1863 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1865 if (bounds)
1866 STORE_NATIVE_RECT (*bounds,
1867 FRAME_COL_TO_PIXEL_X (f, pix_x),
1868 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1869 FRAME_COLUMN_WIDTH (f) - 1,
1870 FRAME_LINE_HEIGHT (f) - 1);
1872 /* PXW: Should we clip pixels before converting to columns/lines? */
1873 if (!noclip)
1875 if (pix_x < 0)
1876 pix_x = 0;
1877 else if (pix_x > FRAME_TOTAL_COLS (f))
1878 pix_x = FRAME_TOTAL_COLS (f);
1880 if (pix_y < 0)
1881 pix_y = 0;
1882 else if (pix_y > FRAME_TOTAL_LINES (f))
1883 pix_y = FRAME_TOTAL_LINES (f);
1886 #endif
1888 *x = pix_x;
1889 *y = pix_y;
1893 /* Find the glyph under window-relative coordinates X/Y in window W.
1894 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1895 strings. Return in *HPOS and *VPOS the row and column number of
1896 the glyph found. Return in *AREA the glyph area containing X.
1897 Value is a pointer to the glyph found or null if X/Y is not on
1898 text, or we can't tell because W's current matrix is not up to
1899 date. */
1901 static struct glyph *
1902 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1903 int *dx, int *dy, int *area)
1905 struct glyph *glyph, *end;
1906 struct glyph_row *row = NULL;
1907 int x0, i;
1909 /* Find row containing Y. Give up if some row is not enabled. */
1910 for (i = 0; i < w->current_matrix->nrows; ++i)
1912 row = MATRIX_ROW (w->current_matrix, i);
1913 if (!row->enabled_p)
1914 return NULL;
1915 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1916 break;
1919 *vpos = i;
1920 *hpos = 0;
1922 /* Give up if Y is not in the window. */
1923 if (i == w->current_matrix->nrows)
1924 return NULL;
1926 /* Get the glyph area containing X. */
1927 if (w->pseudo_window_p)
1929 *area = TEXT_AREA;
1930 x0 = 0;
1932 else
1934 if (x < window_box_left_offset (w, TEXT_AREA))
1936 *area = LEFT_MARGIN_AREA;
1937 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1939 else if (x < window_box_right_offset (w, TEXT_AREA))
1941 *area = TEXT_AREA;
1942 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1944 else
1946 *area = RIGHT_MARGIN_AREA;
1947 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1951 /* Find glyph containing X. */
1952 glyph = row->glyphs[*area];
1953 end = glyph + row->used[*area];
1954 x -= x0;
1955 while (glyph < end && x >= glyph->pixel_width)
1957 x -= glyph->pixel_width;
1958 ++glyph;
1961 if (glyph == end)
1962 return NULL;
1964 if (dx)
1966 *dx = x;
1967 *dy = y - (row->y + row->ascent - glyph->ascent);
1970 *hpos = glyph - row->glyphs[*area];
1971 return glyph;
1974 /* Convert frame-relative x/y to coordinates relative to window W.
1975 Takes pseudo-windows into account. */
1977 static void
1978 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1980 if (w->pseudo_window_p)
1982 /* A pseudo-window is always full-width, and starts at the
1983 left edge of the frame, plus a frame border. */
1984 struct frame *f = XFRAME (w->frame);
1985 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1986 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1988 else
1990 *x -= WINDOW_LEFT_EDGE_X (w);
1991 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1995 #ifdef HAVE_WINDOW_SYSTEM
1997 /* EXPORT:
1998 Return in RECTS[] at most N clipping rectangles for glyph string S.
1999 Return the number of stored rectangles. */
2002 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2004 XRectangle r;
2006 if (n <= 0)
2007 return 0;
2009 if (s->row->full_width_p)
2011 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2012 r.x = WINDOW_LEFT_EDGE_X (s->w);
2013 if (s->row->mode_line_p)
2014 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2015 else
2016 r.width = WINDOW_PIXEL_WIDTH (s->w);
2018 /* Unless displaying a mode or menu bar line, which are always
2019 fully visible, clip to the visible part of the row. */
2020 if (s->w->pseudo_window_p)
2021 r.height = s->row->visible_height;
2022 else
2023 r.height = s->height;
2025 else
2027 /* This is a text line that may be partially visible. */
2028 r.x = window_box_left (s->w, s->area);
2029 r.width = window_box_width (s->w, s->area);
2030 r.height = s->row->visible_height;
2033 if (s->clip_head)
2034 if (r.x < s->clip_head->x)
2036 if (r.width >= s->clip_head->x - r.x)
2037 r.width -= s->clip_head->x - r.x;
2038 else
2039 r.width = 0;
2040 r.x = s->clip_head->x;
2042 if (s->clip_tail)
2043 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2045 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2046 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2047 else
2048 r.width = 0;
2051 /* If S draws overlapping rows, it's sufficient to use the top and
2052 bottom of the window for clipping because this glyph string
2053 intentionally draws over other lines. */
2054 if (s->for_overlaps)
2056 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2057 r.height = window_text_bottom_y (s->w) - r.y;
2059 /* Alas, the above simple strategy does not work for the
2060 environments with anti-aliased text: if the same text is
2061 drawn onto the same place multiple times, it gets thicker.
2062 If the overlap we are processing is for the erased cursor, we
2063 take the intersection with the rectangle of the cursor. */
2064 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2066 XRectangle rc, r_save = r;
2068 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2069 rc.y = s->w->phys_cursor.y;
2070 rc.width = s->w->phys_cursor_width;
2071 rc.height = s->w->phys_cursor_height;
2073 x_intersect_rectangles (&r_save, &rc, &r);
2076 else
2078 /* Don't use S->y for clipping because it doesn't take partially
2079 visible lines into account. For example, it can be negative for
2080 partially visible lines at the top of a window. */
2081 if (!s->row->full_width_p
2082 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2083 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2084 else
2085 r.y = max (0, s->row->y);
2088 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2090 /* If drawing the cursor, don't let glyph draw outside its
2091 advertised boundaries. Cleartype does this under some circumstances. */
2092 if (s->hl == DRAW_CURSOR)
2094 struct glyph *glyph = s->first_glyph;
2095 int height, max_y;
2097 if (s->x > r.x)
2099 if (r.width >= s->x - r.x)
2100 r.width -= s->x - r.x;
2101 else /* R2L hscrolled row with cursor outside text area */
2102 r.width = 0;
2103 r.x = s->x;
2105 r.width = min (r.width, glyph->pixel_width);
2107 /* If r.y is below window bottom, ensure that we still see a cursor. */
2108 height = min (glyph->ascent + glyph->descent,
2109 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2110 max_y = window_text_bottom_y (s->w) - height;
2111 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2112 if (s->ybase - glyph->ascent > max_y)
2114 r.y = max_y;
2115 r.height = height;
2117 else
2119 /* Don't draw cursor glyph taller than our actual glyph. */
2120 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2121 if (height < r.height)
2123 max_y = r.y + r.height;
2124 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2125 r.height = min (max_y - r.y, height);
2130 if (s->row->clip)
2132 XRectangle r_save = r;
2134 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2135 r.width = 0;
2138 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2139 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2141 #ifdef CONVERT_FROM_XRECT
2142 CONVERT_FROM_XRECT (r, *rects);
2143 #else
2144 *rects = r;
2145 #endif
2146 return 1;
2148 else
2150 /* If we are processing overlapping and allowed to return
2151 multiple clipping rectangles, we exclude the row of the glyph
2152 string from the clipping rectangle. This is to avoid drawing
2153 the same text on the environment with anti-aliasing. */
2154 #ifdef CONVERT_FROM_XRECT
2155 XRectangle rs[2];
2156 #else
2157 XRectangle *rs = rects;
2158 #endif
2159 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2161 if (s->for_overlaps & OVERLAPS_PRED)
2163 rs[i] = r;
2164 if (r.y + r.height > row_y)
2166 if (r.y < row_y)
2167 rs[i].height = row_y - r.y;
2168 else
2169 rs[i].height = 0;
2171 i++;
2173 if (s->for_overlaps & OVERLAPS_SUCC)
2175 rs[i] = r;
2176 if (r.y < row_y + s->row->visible_height)
2178 if (r.y + r.height > row_y + s->row->visible_height)
2180 rs[i].y = row_y + s->row->visible_height;
2181 rs[i].height = r.y + r.height - rs[i].y;
2183 else
2184 rs[i].height = 0;
2186 i++;
2189 n = i;
2190 #ifdef CONVERT_FROM_XRECT
2191 for (i = 0; i < n; i++)
2192 CONVERT_FROM_XRECT (rs[i], rects[i]);
2193 #endif
2194 return n;
2198 /* EXPORT:
2199 Return in *NR the clipping rectangle for glyph string S. */
2201 void
2202 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2204 get_glyph_string_clip_rects (s, nr, 1);
2208 /* EXPORT:
2209 Return the position and height of the phys cursor in window W.
2210 Set w->phys_cursor_width to width of phys cursor.
2213 void
2214 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2215 struct glyph *glyph, int *xp, int *yp, int *heightp)
2217 struct frame *f = XFRAME (WINDOW_FRAME (w));
2218 int x, y, wd, h, h0, y0, ascent;
2220 /* Compute the width of the rectangle to draw. If on a stretch
2221 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2222 rectangle as wide as the glyph, but use a canonical character
2223 width instead. */
2224 wd = glyph->pixel_width;
2226 x = w->phys_cursor.x;
2227 if (x < 0)
2229 wd += x;
2230 x = 0;
2233 if (glyph->type == STRETCH_GLYPH
2234 && !x_stretch_cursor_p)
2235 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2236 w->phys_cursor_width = wd;
2238 /* Don't let the hollow cursor glyph descend below the glyph row's
2239 ascent value, lest the hollow cursor looks funny. */
2240 y = w->phys_cursor.y;
2241 ascent = row->ascent;
2242 if (row->ascent < glyph->ascent)
2244 y -= glyph->ascent - row->ascent;
2245 ascent = glyph->ascent;
2248 /* If y is below window bottom, ensure that we still see a cursor. */
2249 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2251 h = max (h0, ascent + glyph->descent);
2252 h0 = min (h0, ascent + glyph->descent);
2254 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2255 if (y < y0)
2257 h = max (h - (y0 - y) + 1, h0);
2258 y = y0 - 1;
2260 else
2262 y0 = window_text_bottom_y (w) - h0;
2263 if (y > y0)
2265 h += y - y0;
2266 y = y0;
2270 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2271 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2272 *heightp = h;
2276 * Remember which glyph the mouse is over.
2279 void
2280 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2282 Lisp_Object window;
2283 struct window *w;
2284 struct glyph_row *r, *gr, *end_row;
2285 enum window_part part;
2286 enum glyph_row_area area;
2287 int x, y, width, height;
2289 /* Try to determine frame pixel position and size of the glyph under
2290 frame pixel coordinates X/Y on frame F. */
2292 if (window_resize_pixelwise)
2294 width = height = 1;
2295 goto virtual_glyph;
2297 else if (!f->glyphs_initialized_p
2298 || (window = window_from_coordinates (f, gx, gy, &part, false),
2299 NILP (window)))
2301 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2302 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2303 goto virtual_glyph;
2306 w = XWINDOW (window);
2307 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2308 height = WINDOW_FRAME_LINE_HEIGHT (w);
2310 x = window_relative_x_coord (w, part, gx);
2311 y = gy - WINDOW_TOP_EDGE_Y (w);
2313 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2314 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2316 if (w->pseudo_window_p)
2318 area = TEXT_AREA;
2319 part = ON_MODE_LINE; /* Don't adjust margin. */
2320 goto text_glyph;
2323 switch (part)
2325 case ON_LEFT_MARGIN:
2326 area = LEFT_MARGIN_AREA;
2327 goto text_glyph;
2329 case ON_RIGHT_MARGIN:
2330 area = RIGHT_MARGIN_AREA;
2331 goto text_glyph;
2333 case ON_HEADER_LINE:
2334 case ON_MODE_LINE:
2335 gr = (part == ON_HEADER_LINE
2336 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2337 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2338 gy = gr->y;
2339 area = TEXT_AREA;
2340 goto text_glyph_row_found;
2342 case ON_TEXT:
2343 area = TEXT_AREA;
2345 text_glyph:
2346 gr = 0; gy = 0;
2347 for (; r <= end_row && r->enabled_p; ++r)
2348 if (r->y + r->height > y)
2350 gr = r; gy = r->y;
2351 break;
2354 text_glyph_row_found:
2355 if (gr && gy <= y)
2357 struct glyph *g = gr->glyphs[area];
2358 struct glyph *end = g + gr->used[area];
2360 height = gr->height;
2361 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2362 if (gx + g->pixel_width > x)
2363 break;
2365 if (g < end)
2367 if (g->type == IMAGE_GLYPH)
2369 /* Don't remember when mouse is over image, as
2370 image may have hot-spots. */
2371 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2372 return;
2374 width = g->pixel_width;
2376 else
2378 /* Use nominal char spacing at end of line. */
2379 x -= gx;
2380 gx += (x / width) * width;
2383 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 gx += window_box_left_offset (w, area);
2386 /* Don't expand over the modeline to make sure the vertical
2387 drag cursor is shown early enough. */
2388 height = min (height,
2389 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2392 else
2394 /* Use nominal line height at end of window. */
2395 gx = (x / width) * width;
2396 y -= gy;
2397 gy += (y / height) * height;
2398 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2399 /* See comment above. */
2400 height = min (height,
2401 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2403 break;
2405 case ON_LEFT_FRINGE:
2406 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2407 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2408 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2409 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2410 goto row_glyph;
2412 case ON_RIGHT_FRINGE:
2413 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2414 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2415 : window_box_right_offset (w, TEXT_AREA));
2416 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2417 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2418 && !WINDOW_RIGHTMOST_P (w))
2419 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2420 /* Make sure the vertical border can get her own glyph to the
2421 right of the one we build here. */
2422 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2423 else
2424 width = WINDOW_PIXEL_WIDTH (w) - gx;
2425 else
2426 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2428 goto row_glyph;
2430 case ON_VERTICAL_BORDER:
2431 gx = WINDOW_PIXEL_WIDTH (w) - width;
2432 goto row_glyph;
2434 case ON_VERTICAL_SCROLL_BAR:
2435 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2437 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2438 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2439 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2440 : 0)));
2441 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2443 row_glyph:
2444 gr = 0, gy = 0;
2445 for (; r <= end_row && r->enabled_p; ++r)
2446 if (r->y + r->height > y)
2448 gr = r; gy = r->y;
2449 break;
2452 if (gr && gy <= y)
2453 height = gr->height;
2454 else
2456 /* Use nominal line height at end of window. */
2457 y -= gy;
2458 gy += (y / height) * height;
2460 break;
2462 case ON_RIGHT_DIVIDER:
2463 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2464 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2465 gy = 0;
2466 /* The bottom divider prevails. */
2467 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2468 goto add_edge;
2470 case ON_BOTTOM_DIVIDER:
2471 gx = 0;
2472 width = WINDOW_PIXEL_WIDTH (w);
2473 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2474 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2475 goto add_edge;
2477 default:
2479 virtual_glyph:
2480 /* If there is no glyph under the mouse, then we divide the screen
2481 into a grid of the smallest glyph in the frame, and use that
2482 as our "glyph". */
2484 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2485 round down even for negative values. */
2486 if (gx < 0)
2487 gx -= width - 1;
2488 if (gy < 0)
2489 gy -= height - 1;
2491 gx = (gx / width) * width;
2492 gy = (gy / height) * height;
2494 goto store_rect;
2497 add_edge:
2498 gx += WINDOW_LEFT_EDGE_X (w);
2499 gy += WINDOW_TOP_EDGE_Y (w);
2501 store_rect:
2502 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2504 /* Visible feedback for debugging. */
2505 #if false && defined HAVE_X_WINDOWS
2506 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2507 f->output_data.x->normal_gc,
2508 gx, gy, width, height);
2509 #endif
2513 #endif /* HAVE_WINDOW_SYSTEM */
2515 static void
2516 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2518 eassert (w);
2519 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2520 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2521 w->window_end_vpos
2522 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2525 /***********************************************************************
2526 Lisp form evaluation
2527 ***********************************************************************/
2529 /* Error handler for safe_eval and safe_call. */
2531 static Lisp_Object
2532 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2534 add_to_log ("Error during redisplay: %S signaled %S",
2535 Flist (nargs, args), arg);
2536 return Qnil;
2539 /* Call function FUNC with the rest of NARGS - 1 arguments
2540 following. Return the result, or nil if something went
2541 wrong. Prevent redisplay during the evaluation. */
2543 static Lisp_Object
2544 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2546 Lisp_Object val;
2548 if (inhibit_eval_during_redisplay)
2549 val = Qnil;
2550 else
2552 ptrdiff_t i;
2553 ptrdiff_t count = SPECPDL_INDEX ();
2554 Lisp_Object *args;
2555 USE_SAFE_ALLOCA;
2556 SAFE_ALLOCA_LISP (args, nargs);
2558 args[0] = func;
2559 for (i = 1; i < nargs; i++)
2560 args[i] = va_arg (ap, Lisp_Object);
2562 specbind (Qinhibit_redisplay, Qt);
2563 if (inhibit_quit)
2564 specbind (Qinhibit_quit, Qt);
2565 /* Use Qt to ensure debugger does not run,
2566 so there is no possibility of wanting to redisplay. */
2567 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2568 safe_eval_handler);
2569 SAFE_FREE ();
2570 val = unbind_to (count, val);
2573 return val;
2576 Lisp_Object
2577 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2579 Lisp_Object retval;
2580 va_list ap;
2582 va_start (ap, func);
2583 retval = safe__call (false, nargs, func, ap);
2584 va_end (ap);
2585 return retval;
2588 /* Call function FN with one argument ARG.
2589 Return the result, or nil if something went wrong. */
2591 Lisp_Object
2592 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2594 return safe_call (2, fn, arg);
2597 static Lisp_Object
2598 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2600 Lisp_Object retval;
2601 va_list ap;
2603 va_start (ap, fn);
2604 retval = safe__call (inhibit_quit, 2, fn, ap);
2605 va_end (ap);
2606 return retval;
2609 Lisp_Object
2610 safe_eval (Lisp_Object sexpr)
2612 return safe__call1 (false, Qeval, sexpr);
2615 static Lisp_Object
2616 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2618 return safe__call1 (inhibit_quit, Qeval, sexpr);
2621 /* Call function FN with two arguments ARG1 and ARG2.
2622 Return the result, or nil if something went wrong. */
2624 Lisp_Object
2625 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2627 return safe_call (3, fn, arg1, arg2);
2632 /***********************************************************************
2633 Debugging
2634 ***********************************************************************/
2636 /* Define CHECK_IT to perform sanity checks on iterators.
2637 This is for debugging. It is too slow to do unconditionally. */
2639 static void
2640 CHECK_IT (struct it *it)
2642 #if false
2643 if (it->method == GET_FROM_STRING)
2645 eassert (STRINGP (it->string));
2646 eassert (IT_STRING_CHARPOS (*it) >= 0);
2648 else
2650 eassert (IT_STRING_CHARPOS (*it) < 0);
2651 if (it->method == GET_FROM_BUFFER)
2653 /* Check that character and byte positions agree. */
2654 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2658 if (it->dpvec)
2659 eassert (it->current.dpvec_index >= 0);
2660 else
2661 eassert (it->current.dpvec_index < 0);
2662 #endif
2666 /* Check that the window end of window W is what we expect it
2667 to be---the last row in the current matrix displaying text. */
2669 static void
2670 CHECK_WINDOW_END (struct window *w)
2672 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2673 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2675 struct glyph_row *row;
2676 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2677 !row->enabled_p
2678 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2679 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2681 #endif
2684 /***********************************************************************
2685 Iterator initialization
2686 ***********************************************************************/
2688 /* Initialize IT for displaying current_buffer in window W, starting
2689 at character position CHARPOS. CHARPOS < 0 means that no buffer
2690 position is specified which is useful when the iterator is assigned
2691 a position later. BYTEPOS is the byte position corresponding to
2692 CHARPOS.
2694 If ROW is not null, calls to produce_glyphs with IT as parameter
2695 will produce glyphs in that row.
2697 BASE_FACE_ID is the id of a base face to use. It must be one of
2698 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2699 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2700 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2702 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2704 will be initialized to use the corresponding mode line glyph row of
2705 the desired matrix of W. */
2707 void
2708 init_iterator (struct it *it, struct window *w,
2709 ptrdiff_t charpos, ptrdiff_t bytepos,
2710 struct glyph_row *row, enum face_id base_face_id)
2712 enum face_id remapped_base_face_id = base_face_id;
2714 /* Some precondition checks. */
2715 eassert (w != NULL && it != NULL);
2716 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2717 && charpos <= ZV));
2719 /* If face attributes have been changed since the last redisplay,
2720 free realized faces now because they depend on face definitions
2721 that might have changed. Don't free faces while there might be
2722 desired matrices pending which reference these faces. */
2723 if (!inhibit_free_realized_faces)
2725 if (face_change)
2727 face_change = false;
2728 free_all_realized_faces (Qnil);
2730 else if (XFRAME (w->frame)->face_change)
2732 XFRAME (w->frame)->face_change = 0;
2733 free_all_realized_faces (w->frame);
2737 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2738 if (! NILP (Vface_remapping_alist))
2739 remapped_base_face_id
2740 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2742 /* Use one of the mode line rows of W's desired matrix if
2743 appropriate. */
2744 if (row == NULL)
2746 if (base_face_id == MODE_LINE_FACE_ID
2747 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2748 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2749 else if (base_face_id == HEADER_LINE_FACE_ID)
2750 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2753 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2754 Other parts of redisplay rely on that. */
2755 memclear (it, sizeof *it);
2756 it->current.overlay_string_index = -1;
2757 it->current.dpvec_index = -1;
2758 it->base_face_id = remapped_base_face_id;
2759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2760 it->paragraph_embedding = L2R;
2761 it->bidi_it.w = w;
2763 /* The window in which we iterate over current_buffer: */
2764 XSETWINDOW (it->window, w);
2765 it->w = w;
2766 it->f = XFRAME (w->frame);
2768 it->cmp_it.id = -1;
2770 /* Extra space between lines (on window systems only). */
2771 if (base_face_id == DEFAULT_FACE_ID
2772 && FRAME_WINDOW_P (it->f))
2774 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2776 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2777 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2778 * FRAME_LINE_HEIGHT (it->f));
2779 else if (it->f->extra_line_spacing > 0)
2780 it->extra_line_spacing = it->f->extra_line_spacing;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 it->override_ascent = -1;
2794 /* Are control characters displayed as `^C'? */
2795 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2797 /* -1 means everything between a CR and the following line end
2798 is invisible. >0 means lines indented more than this value are
2799 invisible. */
2800 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2801 ? (clip_to_bounds
2802 (-1, XINT (BVAR (current_buffer, selective_display)),
2803 PTRDIFF_MAX))
2804 : (!NILP (BVAR (current_buffer, selective_display))
2805 ? -1 : 0));
2806 it->selective_display_ellipsis_p
2807 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2809 /* Display table to use. */
2810 it->dp = window_display_table (w);
2812 /* Are multibyte characters enabled in current_buffer? */
2813 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2815 /* Get the position at which the redisplay_end_trigger hook should
2816 be run, if it is to be run at all. */
2817 if (MARKERP (w->redisplay_end_trigger)
2818 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2819 it->redisplay_end_trigger_charpos
2820 = marker_position (w->redisplay_end_trigger);
2821 else if (INTEGERP (w->redisplay_end_trigger))
2822 it->redisplay_end_trigger_charpos
2823 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2824 PTRDIFF_MAX);
2826 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2828 /* Are lines in the display truncated? */
2829 if (TRUNCATE != 0)
2830 it->line_wrap = TRUNCATE;
2831 if (base_face_id == DEFAULT_FACE_ID
2832 && !it->w->hscroll
2833 && (WINDOW_FULL_WIDTH_P (it->w)
2834 || NILP (Vtruncate_partial_width_windows)
2835 || (INTEGERP (Vtruncate_partial_width_windows)
2836 /* PXW: Shall we do something about this? */
2837 && (XINT (Vtruncate_partial_width_windows)
2838 <= WINDOW_TOTAL_COLS (it->w))))
2839 && NILP (BVAR (current_buffer, truncate_lines)))
2840 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2841 ? WINDOW_WRAP : WORD_WRAP;
2843 /* Get dimensions of truncation and continuation glyphs. These are
2844 displayed as fringe bitmaps under X, but we need them for such
2845 frames when the fringes are turned off. But leave the dimensions
2846 zero for tooltip frames, as these glyphs look ugly there and also
2847 sabotage calculations of tooltip dimensions in x-show-tip. */
2848 #ifdef HAVE_WINDOW_SYSTEM
2849 if (!(FRAME_WINDOW_P (it->f)
2850 && FRAMEP (tip_frame)
2851 && it->f == XFRAME (tip_frame)))
2852 #endif
2854 if (it->line_wrap == TRUNCATE)
2856 /* We will need the truncation glyph. */
2857 eassert (it->glyph_row == NULL);
2858 produce_special_glyphs (it, IT_TRUNCATION);
2859 it->truncation_pixel_width = it->pixel_width;
2861 else
2863 /* We will need the continuation glyph. */
2864 eassert (it->glyph_row == NULL);
2865 produce_special_glyphs (it, IT_CONTINUATION);
2866 it->continuation_pixel_width = it->pixel_width;
2870 /* Reset these values to zero because the produce_special_glyphs
2871 above has changed them. */
2872 it->pixel_width = it->ascent = it->descent = 0;
2873 it->phys_ascent = it->phys_descent = 0;
2875 /* Set this after getting the dimensions of truncation and
2876 continuation glyphs, so that we don't produce glyphs when calling
2877 produce_special_glyphs, above. */
2878 it->glyph_row = row;
2879 it->area = TEXT_AREA;
2881 /* Get the dimensions of the display area. The display area
2882 consists of the visible window area plus a horizontally scrolled
2883 part to the left of the window. All x-values are relative to the
2884 start of this total display area. */
2885 if (base_face_id != DEFAULT_FACE_ID)
2887 /* Mode lines, menu bar in terminal frames. */
2888 it->first_visible_x = 0;
2889 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2891 else
2893 it->first_visible_x
2894 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2895 it->last_visible_x = (it->first_visible_x
2896 + window_box_width (w, TEXT_AREA));
2898 /* If we truncate lines, leave room for the truncation glyph(s) at
2899 the right margin. Otherwise, leave room for the continuation
2900 glyph(s). Done only if the window has no right fringe. */
2901 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2903 if (it->line_wrap == TRUNCATE)
2904 it->last_visible_x -= it->truncation_pixel_width;
2905 else
2906 it->last_visible_x -= it->continuation_pixel_width;
2909 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2910 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2913 /* Leave room for a border glyph. */
2914 if (!FRAME_WINDOW_P (it->f)
2915 && !WINDOW_RIGHTMOST_P (it->w))
2916 it->last_visible_x -= 1;
2918 it->last_visible_y = window_text_bottom_y (w);
2920 /* For mode lines and alike, arrange for the first glyph having a
2921 left box line if the face specifies a box. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2924 struct face *face;
2926 it->face_id = remapped_base_face_id;
2928 /* If we have a boxed mode line, make the first character appear
2929 with a left box line. */
2930 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2931 if (face && face->box != FACE_NO_BOX)
2932 it->start_of_box_run_p = true;
2935 /* If a buffer position was specified, set the iterator there,
2936 getting overlays and face properties from that position. */
2937 if (charpos >= BUF_BEG (current_buffer))
2939 it->stop_charpos = charpos;
2940 it->end_charpos = ZV;
2941 eassert (charpos == BYTE_TO_CHAR (bytepos));
2942 IT_CHARPOS (*it) = charpos;
2943 IT_BYTEPOS (*it) = bytepos;
2945 /* We will rely on `reseat' to set this up properly, via
2946 handle_face_prop. */
2947 it->face_id = it->base_face_id;
2949 it->start = it->current;
2950 /* Do we need to reorder bidirectional text? Not if this is a
2951 unibyte buffer: by definition, none of the single-byte
2952 characters are strong R2L, so no reordering is needed. And
2953 bidi.c doesn't support unibyte buffers anyway. Also, don't
2954 reorder while we are loading loadup.el, since the tables of
2955 character properties needed for reordering are not yet
2956 available. */
2957 it->bidi_p =
2958 !redisplay__inhibit_bidi
2959 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2960 && it->multibyte_p;
2962 /* If we are to reorder bidirectional text, init the bidi
2963 iterator. */
2964 if (it->bidi_p)
2966 /* Since we don't know at this point whether there will be
2967 any R2L lines in the window, we reserve space for
2968 truncation/continuation glyphs even if only the left
2969 fringe is absent. */
2970 if (base_face_id == DEFAULT_FACE_ID
2971 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2972 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2974 if (it->line_wrap == TRUNCATE)
2975 it->last_visible_x -= it->truncation_pixel_width;
2976 else
2977 it->last_visible_x -= it->continuation_pixel_width;
2979 /* Note the paragraph direction that this buffer wants to
2980 use. */
2981 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2982 Qleft_to_right))
2983 it->paragraph_embedding = L2R;
2984 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2985 Qright_to_left))
2986 it->paragraph_embedding = R2L;
2987 else
2988 it->paragraph_embedding = NEUTRAL_DIR;
2989 bidi_unshelve_cache (NULL, false);
2990 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2991 &it->bidi_it);
2994 /* Compute faces etc. */
2995 reseat (it, it->current.pos, true);
2998 CHECK_IT (it);
3002 /* Initialize IT for the display of window W with window start POS. */
3004 void
3005 start_display (struct it *it, struct window *w, struct text_pos pos)
3007 struct glyph_row *row;
3008 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3010 row = w->desired_matrix->rows + first_vpos;
3011 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3012 it->first_vpos = first_vpos;
3014 /* Don't reseat to previous visible line start if current start
3015 position is in a string or image. */
3016 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3018 int first_y = it->current_y;
3020 /* If window start is not at a line start, skip forward to POS to
3021 get the correct continuation lines width. */
3022 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3023 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3024 if (!start_at_line_beg_p)
3026 int new_x;
3028 reseat_at_previous_visible_line_start (it);
3029 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3031 new_x = it->current_x + it->pixel_width;
3033 /* If lines are continued, this line may end in the middle
3034 of a multi-glyph character (e.g. a control character
3035 displayed as \003, or in the middle of an overlay
3036 string). In this case move_it_to above will not have
3037 taken us to the start of the continuation line but to the
3038 end of the continued line. */
3039 if (it->current_x > 0
3040 && it->line_wrap != TRUNCATE /* Lines are continued. */
3041 && (/* And glyph doesn't fit on the line. */
3042 new_x > it->last_visible_x
3043 /* Or it fits exactly and we're on a window
3044 system frame. */
3045 || (new_x == it->last_visible_x
3046 && FRAME_WINDOW_P (it->f)
3047 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3048 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3049 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3051 if ((it->current.dpvec_index >= 0
3052 || it->current.overlay_string_index >= 0)
3053 /* If we are on a newline from a display vector or
3054 overlay string, then we are already at the end of
3055 a screen line; no need to go to the next line in
3056 that case, as this line is not really continued.
3057 (If we do go to the next line, C-e will not DTRT.) */
3058 && it->c != '\n')
3060 set_iterator_to_next (it, true);
3061 move_it_in_display_line_to (it, -1, -1, 0);
3064 it->continuation_lines_width += it->current_x;
3066 /* If the character at POS is displayed via a display
3067 vector, move_it_to above stops at the final glyph of
3068 IT->dpvec. To make the caller redisplay that character
3069 again (a.k.a. start at POS), we need to reset the
3070 dpvec_index to the beginning of IT->dpvec. */
3071 else if (it->current.dpvec_index >= 0)
3072 it->current.dpvec_index = 0;
3074 /* We're starting a new display line, not affected by the
3075 height of the continued line, so clear the appropriate
3076 fields in the iterator structure. */
3077 it->max_ascent = it->max_descent = 0;
3078 it->max_phys_ascent = it->max_phys_descent = 0;
3080 it->current_y = first_y;
3081 it->vpos = 0;
3082 it->current_x = it->hpos = 0;
3088 /* Return true if POS is a position in ellipses displayed for invisible
3089 text. W is the window we display, for text property lookup. */
3091 static bool
3092 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3094 Lisp_Object prop, window;
3095 bool ellipses_p = false;
3096 ptrdiff_t charpos = CHARPOS (pos->pos);
3098 /* If POS specifies a position in a display vector, this might
3099 be for an ellipsis displayed for invisible text. We won't
3100 get the iterator set up for delivering that ellipsis unless
3101 we make sure that it gets aware of the invisible text. */
3102 if (pos->dpvec_index >= 0
3103 && pos->overlay_string_index < 0
3104 && CHARPOS (pos->string_pos) < 0
3105 && charpos > BEGV
3106 && (XSETWINDOW (window, w),
3107 prop = Fget_char_property (make_number (charpos),
3108 Qinvisible, window),
3109 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3111 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3112 window);
3113 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3116 return ellipses_p;
3120 /* Initialize IT for stepping through current_buffer in window W,
3121 starting at position POS that includes overlay string and display
3122 vector/ control character translation position information. Value
3123 is false if there are overlay strings with newlines at POS. */
3125 static bool
3126 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3128 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3129 int i;
3130 bool overlay_strings_with_newlines = false;
3132 /* If POS specifies a position in a display vector, this might
3133 be for an ellipsis displayed for invisible text. We won't
3134 get the iterator set up for delivering that ellipsis unless
3135 we make sure that it gets aware of the invisible text. */
3136 if (in_ellipses_for_invisible_text_p (pos, w))
3138 --charpos;
3139 bytepos = 0;
3142 /* Keep in mind: the call to reseat in init_iterator skips invisible
3143 text, so we might end up at a position different from POS. This
3144 is only a problem when POS is a row start after a newline and an
3145 overlay starts there with an after-string, and the overlay has an
3146 invisible property. Since we don't skip invisible text in
3147 display_line and elsewhere immediately after consuming the
3148 newline before the row start, such a POS will not be in a string,
3149 but the call to init_iterator below will move us to the
3150 after-string. */
3151 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3153 /* This only scans the current chunk -- it should scan all chunks.
3154 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3155 to 16 in 22.1 to make this a lesser problem. */
3156 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3158 const char *s = SSDATA (it->overlay_strings[i]);
3159 const char *e = s + SBYTES (it->overlay_strings[i]);
3161 while (s < e && *s != '\n')
3162 ++s;
3164 if (s < e)
3166 overlay_strings_with_newlines = true;
3167 break;
3171 /* If position is within an overlay string, set up IT to the right
3172 overlay string. */
3173 if (pos->overlay_string_index >= 0)
3175 int relative_index;
3177 /* If the first overlay string happens to have a `display'
3178 property for an image, the iterator will be set up for that
3179 image, and we have to undo that setup first before we can
3180 correct the overlay string index. */
3181 if (it->method == GET_FROM_IMAGE)
3182 pop_it (it);
3184 /* We already have the first chunk of overlay strings in
3185 IT->overlay_strings. Load more until the one for
3186 pos->overlay_string_index is in IT->overlay_strings. */
3187 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3189 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3190 it->current.overlay_string_index = 0;
3191 while (n--)
3193 load_overlay_strings (it, 0);
3194 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3198 it->current.overlay_string_index = pos->overlay_string_index;
3199 relative_index = (it->current.overlay_string_index
3200 % OVERLAY_STRING_CHUNK_SIZE);
3201 it->string = it->overlay_strings[relative_index];
3202 eassert (STRINGP (it->string));
3203 it->current.string_pos = pos->string_pos;
3204 it->method = GET_FROM_STRING;
3205 it->end_charpos = SCHARS (it->string);
3206 /* Set up the bidi iterator for this overlay string. */
3207 if (it->bidi_p)
3209 it->bidi_it.string.lstring = it->string;
3210 it->bidi_it.string.s = NULL;
3211 it->bidi_it.string.schars = SCHARS (it->string);
3212 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3213 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3214 it->bidi_it.string.unibyte = !it->multibyte_p;
3215 it->bidi_it.w = it->w;
3216 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3217 FRAME_WINDOW_P (it->f), &it->bidi_it);
3219 /* Synchronize the state of the bidi iterator with
3220 pos->string_pos. For any string position other than
3221 zero, this will be done automagically when we resume
3222 iteration over the string and get_visually_first_element
3223 is called. But if string_pos is zero, and the string is
3224 to be reordered for display, we need to resync manually,
3225 since it could be that the iteration state recorded in
3226 pos ended at string_pos of 0 moving backwards in string. */
3227 if (CHARPOS (pos->string_pos) == 0)
3229 get_visually_first_element (it);
3230 if (IT_STRING_CHARPOS (*it) != 0)
3231 do {
3232 /* Paranoia. */
3233 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3234 bidi_move_to_visually_next (&it->bidi_it);
3235 } while (it->bidi_it.charpos != 0);
3237 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3238 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3242 if (CHARPOS (pos->string_pos) >= 0)
3244 /* Recorded position is not in an overlay string, but in another
3245 string. This can only be a string from a `display' property.
3246 IT should already be filled with that string. */
3247 it->current.string_pos = pos->string_pos;
3248 eassert (STRINGP (it->string));
3249 if (it->bidi_p)
3250 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3251 FRAME_WINDOW_P (it->f), &it->bidi_it);
3254 /* Restore position in display vector translations, control
3255 character translations or ellipses. */
3256 if (pos->dpvec_index >= 0)
3258 if (it->dpvec == NULL)
3259 get_next_display_element (it);
3260 eassert (it->dpvec && it->current.dpvec_index == 0);
3261 it->current.dpvec_index = pos->dpvec_index;
3264 CHECK_IT (it);
3265 return !overlay_strings_with_newlines;
3269 /* Initialize IT for stepping through current_buffer in window W
3270 starting at ROW->start. */
3272 static void
3273 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3275 init_from_display_pos (it, w, &row->start);
3276 it->start = row->start;
3277 it->continuation_lines_width = row->continuation_lines_width;
3278 CHECK_IT (it);
3282 /* Initialize IT for stepping through current_buffer in window W
3283 starting in the line following ROW, i.e. starting at ROW->end.
3284 Value is false if there are overlay strings with newlines at ROW's
3285 end position. */
3287 static bool
3288 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3290 bool success = false;
3292 if (init_from_display_pos (it, w, &row->end))
3294 if (row->continued_p)
3295 it->continuation_lines_width
3296 = row->continuation_lines_width + row->pixel_width;
3297 CHECK_IT (it);
3298 success = true;
3301 return success;
3307 /***********************************************************************
3308 Text properties
3309 ***********************************************************************/
3311 /* Called when IT reaches IT->stop_charpos. Handle text property and
3312 overlay changes. Set IT->stop_charpos to the next position where
3313 to stop. */
3315 static void
3316 handle_stop (struct it *it)
3318 enum prop_handled handled;
3319 bool handle_overlay_change_p;
3320 struct props *p;
3322 it->dpvec = NULL;
3323 it->current.dpvec_index = -1;
3324 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3325 it->ellipsis_p = false;
3327 /* Use face of preceding text for ellipsis (if invisible) */
3328 if (it->selective_display_ellipsis_p)
3329 it->saved_face_id = it->face_id;
3331 /* Here's the description of the semantics of, and the logic behind,
3332 the various HANDLED_* statuses:
3334 HANDLED_NORMALLY means the handler did its job, and the loop
3335 should proceed to calling the next handler in order.
3337 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3338 change in the properties and overlays at current position, so the
3339 loop should be restarted, to re-invoke the handlers that were
3340 already called. This happens when fontification-functions were
3341 called by handle_fontified_prop, and actually fontified
3342 something. Another case where HANDLED_RECOMPUTE_PROPS is
3343 returned is when we discover overlay strings that need to be
3344 displayed right away. The loop below will continue for as long
3345 as the status is HANDLED_RECOMPUTE_PROPS.
3347 HANDLED_RETURN means return immediately to the caller, to
3348 continue iteration without calling any further handlers. This is
3349 used when we need to act on some property right away, for example
3350 when we need to display the ellipsis or a replacing display
3351 property, such as display string or image.
3353 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3354 consumed, and the handler switched to the next overlay string.
3355 This signals the loop below to refrain from looking for more
3356 overlays before all the overlay strings of the current overlay
3357 are processed.
3359 Some of the handlers called by the loop push the iterator state
3360 onto the stack (see 'push_it'), and arrange for the iteration to
3361 continue with another object, such as an image, a display string,
3362 or an overlay string. In most such cases, it->stop_charpos is
3363 set to the first character of the string, so that when the
3364 iteration resumes, this function will immediately be called
3365 again, to examine the properties at the beginning of the string.
3367 When a display or overlay string is exhausted, the iterator state
3368 is popped (see 'pop_it'), and iteration continues with the
3369 previous object. Again, in many such cases this function is
3370 called again to find the next position where properties might
3371 change. */
3375 handled = HANDLED_NORMALLY;
3377 /* Call text property handlers. */
3378 for (p = it_props; p->handler; ++p)
3380 handled = p->handler (it);
3382 if (handled == HANDLED_RECOMPUTE_PROPS)
3383 break;
3384 else if (handled == HANDLED_RETURN)
3386 /* We still want to show before and after strings from
3387 overlays even if the actual buffer text is replaced. */
3388 if (!handle_overlay_change_p
3389 || it->sp > 1
3390 /* Don't call get_overlay_strings_1 if we already
3391 have overlay strings loaded, because doing so
3392 will load them again and push the iterator state
3393 onto the stack one more time, which is not
3394 expected by the rest of the code that processes
3395 overlay strings. */
3396 || (it->current.overlay_string_index < 0
3397 && !get_overlay_strings_1 (it, 0, false)))
3399 if (it->ellipsis_p)
3400 setup_for_ellipsis (it, 0);
3401 /* When handling a display spec, we might load an
3402 empty string. In that case, discard it here. We
3403 used to discard it in handle_single_display_spec,
3404 but that causes get_overlay_strings_1, above, to
3405 ignore overlay strings that we must check. */
3406 if (STRINGP (it->string) && !SCHARS (it->string))
3407 pop_it (it);
3408 return;
3410 else if (STRINGP (it->string) && !SCHARS (it->string))
3411 pop_it (it);
3412 else
3414 it->string_from_display_prop_p = false;
3415 it->from_disp_prop_p = false;
3416 handle_overlay_change_p = false;
3418 handled = HANDLED_RECOMPUTE_PROPS;
3419 break;
3421 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3422 handle_overlay_change_p = false;
3425 if (handled != HANDLED_RECOMPUTE_PROPS)
3427 /* Don't check for overlay strings below when set to deliver
3428 characters from a display vector. */
3429 if (it->method == GET_FROM_DISPLAY_VECTOR)
3430 handle_overlay_change_p = false;
3432 /* Handle overlay changes.
3433 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3434 if it finds overlays. */
3435 if (handle_overlay_change_p)
3436 handled = handle_overlay_change (it);
3439 if (it->ellipsis_p)
3441 setup_for_ellipsis (it, 0);
3442 break;
3445 while (handled == HANDLED_RECOMPUTE_PROPS);
3447 /* Determine where to stop next. */
3448 if (handled == HANDLED_NORMALLY)
3449 compute_stop_pos (it);
3453 /* Compute IT->stop_charpos from text property and overlay change
3454 information for IT's current position. */
3456 static void
3457 compute_stop_pos (struct it *it)
3459 register INTERVAL iv, next_iv;
3460 Lisp_Object object, limit, position;
3461 ptrdiff_t charpos, bytepos;
3463 if (STRINGP (it->string))
3465 /* Strings are usually short, so don't limit the search for
3466 properties. */
3467 it->stop_charpos = it->end_charpos;
3468 object = it->string;
3469 limit = Qnil;
3470 charpos = IT_STRING_CHARPOS (*it);
3471 bytepos = IT_STRING_BYTEPOS (*it);
3473 else
3475 ptrdiff_t pos;
3477 /* If end_charpos is out of range for some reason, such as a
3478 misbehaving display function, rationalize it (Bug#5984). */
3479 if (it->end_charpos > ZV)
3480 it->end_charpos = ZV;
3481 it->stop_charpos = it->end_charpos;
3483 /* If next overlay change is in front of the current stop pos
3484 (which is IT->end_charpos), stop there. Note: value of
3485 next_overlay_change is point-max if no overlay change
3486 follows. */
3487 charpos = IT_CHARPOS (*it);
3488 bytepos = IT_BYTEPOS (*it);
3489 pos = next_overlay_change (charpos);
3490 if (pos < it->stop_charpos)
3491 it->stop_charpos = pos;
3493 /* Set up variables for computing the stop position from text
3494 property changes. */
3495 XSETBUFFER (object, current_buffer);
3496 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3499 /* Get the interval containing IT's position. Value is a null
3500 interval if there isn't such an interval. */
3501 position = make_number (charpos);
3502 iv = validate_interval_range (object, &position, &position, false);
3503 if (iv)
3505 Lisp_Object values_here[LAST_PROP_IDX];
3506 struct props *p;
3508 /* Get properties here. */
3509 for (p = it_props; p->handler; ++p)
3510 values_here[p->idx] = textget (iv->plist,
3511 builtin_lisp_symbol (p->name));
3513 /* Look for an interval following iv that has different
3514 properties. */
3515 for (next_iv = next_interval (iv);
3516 (next_iv
3517 && (NILP (limit)
3518 || XFASTINT (limit) > next_iv->position));
3519 next_iv = next_interval (next_iv))
3521 for (p = it_props; p->handler; ++p)
3523 Lisp_Object new_value = textget (next_iv->plist,
3524 builtin_lisp_symbol (p->name));
3525 if (!EQ (values_here[p->idx], new_value))
3526 break;
3529 if (p->handler)
3530 break;
3533 if (next_iv)
3535 if (INTEGERP (limit)
3536 && next_iv->position >= XFASTINT (limit))
3537 /* No text property change up to limit. */
3538 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3539 else
3540 /* Text properties change in next_iv. */
3541 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3545 if (it->cmp_it.id < 0)
3547 ptrdiff_t stoppos = it->end_charpos;
3549 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3550 stoppos = -1;
3551 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3552 stoppos, it->string);
3555 eassert (STRINGP (it->string)
3556 || (it->stop_charpos >= BEGV
3557 && it->stop_charpos >= IT_CHARPOS (*it)));
3561 /* Return the position of the next overlay change after POS in
3562 current_buffer. Value is point-max if no overlay change
3563 follows. This is like `next-overlay-change' but doesn't use
3564 xmalloc. */
3566 static ptrdiff_t
3567 next_overlay_change (ptrdiff_t pos)
3569 ptrdiff_t i, noverlays;
3570 ptrdiff_t endpos;
3571 Lisp_Object *overlays;
3572 USE_SAFE_ALLOCA;
3574 /* Get all overlays at the given position. */
3575 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3577 /* If any of these overlays ends before endpos,
3578 use its ending point instead. */
3579 for (i = 0; i < noverlays; ++i)
3581 Lisp_Object oend;
3582 ptrdiff_t oendpos;
3584 oend = OVERLAY_END (overlays[i]);
3585 oendpos = OVERLAY_POSITION (oend);
3586 endpos = min (endpos, oendpos);
3589 SAFE_FREE ();
3590 return endpos;
3593 /* How many characters forward to search for a display property or
3594 display string. Searching too far forward makes the bidi display
3595 sluggish, especially in small windows. */
3596 #define MAX_DISP_SCAN 250
3598 /* Return the character position of a display string at or after
3599 position specified by POSITION. If no display string exists at or
3600 after POSITION, return ZV. A display string is either an overlay
3601 with `display' property whose value is a string, or a `display'
3602 text property whose value is a string. STRING is data about the
3603 string to iterate; if STRING->lstring is nil, we are iterating a
3604 buffer. FRAME_WINDOW_P is true when we are displaying a window
3605 on a GUI frame. DISP_PROP is set to zero if we searched
3606 MAX_DISP_SCAN characters forward without finding any display
3607 strings, non-zero otherwise. It is set to 2 if the display string
3608 uses any kind of `(space ...)' spec that will produce a stretch of
3609 white space in the text area. */
3610 ptrdiff_t
3611 compute_display_string_pos (struct text_pos *position,
3612 struct bidi_string_data *string,
3613 struct window *w,
3614 bool frame_window_p, int *disp_prop)
3616 /* OBJECT = nil means current buffer. */
3617 Lisp_Object object, object1;
3618 Lisp_Object pos, spec, limpos;
3619 bool string_p = string && (STRINGP (string->lstring) || string->s);
3620 ptrdiff_t eob = string_p ? string->schars : ZV;
3621 ptrdiff_t begb = string_p ? 0 : BEGV;
3622 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3623 ptrdiff_t lim =
3624 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3625 struct text_pos tpos;
3626 int rv = 0;
3628 if (string && STRINGP (string->lstring))
3629 object1 = object = string->lstring;
3630 else if (w && !string_p)
3632 XSETWINDOW (object, w);
3633 object1 = Qnil;
3635 else
3636 object1 = object = Qnil;
3638 *disp_prop = 1;
3640 if (charpos >= eob
3641 /* We don't support display properties whose values are strings
3642 that have display string properties. */
3643 || string->from_disp_str
3644 /* C strings cannot have display properties. */
3645 || (string->s && !STRINGP (object)))
3647 *disp_prop = 0;
3648 return eob;
3651 /* If the character at CHARPOS is where the display string begins,
3652 return CHARPOS. */
3653 pos = make_number (charpos);
3654 if (STRINGP (object))
3655 bufpos = string->bufpos;
3656 else
3657 bufpos = charpos;
3658 tpos = *position;
3659 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3660 && (charpos <= begb
3661 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3662 object),
3663 spec))
3664 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3665 frame_window_p)))
3667 if (rv == 2)
3668 *disp_prop = 2;
3669 return charpos;
3672 /* Look forward for the first character with a `display' property
3673 that will replace the underlying text when displayed. */
3674 limpos = make_number (lim);
3675 do {
3676 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3677 CHARPOS (tpos) = XFASTINT (pos);
3678 if (CHARPOS (tpos) >= lim)
3680 *disp_prop = 0;
3681 break;
3683 if (STRINGP (object))
3684 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3685 else
3686 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3687 spec = Fget_char_property (pos, Qdisplay, object);
3688 if (!STRINGP (object))
3689 bufpos = CHARPOS (tpos);
3690 } while (NILP (spec)
3691 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3692 bufpos, frame_window_p)));
3693 if (rv == 2)
3694 *disp_prop = 2;
3696 return CHARPOS (tpos);
3699 /* Return the character position of the end of the display string that
3700 started at CHARPOS. If there's no display string at CHARPOS,
3701 return -1. A display string is either an overlay with `display'
3702 property whose value is a string or a `display' text property whose
3703 value is a string. */
3704 ptrdiff_t
3705 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3707 /* OBJECT = nil means current buffer. */
3708 Lisp_Object object =
3709 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3710 Lisp_Object pos = make_number (charpos);
3711 ptrdiff_t eob =
3712 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3714 if (charpos >= eob || (string->s && !STRINGP (object)))
3715 return eob;
3717 /* It could happen that the display property or overlay was removed
3718 since we found it in compute_display_string_pos above. One way
3719 this can happen is if JIT font-lock was called (through
3720 handle_fontified_prop), and jit-lock-functions remove text
3721 properties or overlays from the portion of buffer that includes
3722 CHARPOS. Muse mode is known to do that, for example. In this
3723 case, we return -1 to the caller, to signal that no display
3724 string is actually present at CHARPOS. See bidi_fetch_char for
3725 how this is handled.
3727 An alternative would be to never look for display properties past
3728 it->stop_charpos. But neither compute_display_string_pos nor
3729 bidi_fetch_char that calls it know or care where the next
3730 stop_charpos is. */
3731 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3732 return -1;
3734 /* Look forward for the first character where the `display' property
3735 changes. */
3736 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3738 return XFASTINT (pos);
3743 /***********************************************************************
3744 Fontification
3745 ***********************************************************************/
3747 /* Handle changes in the `fontified' property of the current buffer by
3748 calling hook functions from Qfontification_functions to fontify
3749 regions of text. */
3751 static enum prop_handled
3752 handle_fontified_prop (struct it *it)
3754 Lisp_Object prop, pos;
3755 enum prop_handled handled = HANDLED_NORMALLY;
3757 if (!NILP (Vmemory_full))
3758 return handled;
3760 /* Get the value of the `fontified' property at IT's current buffer
3761 position. (The `fontified' property doesn't have a special
3762 meaning in strings.) If the value is nil, call functions from
3763 Qfontification_functions. */
3764 if (!STRINGP (it->string)
3765 && it->s == NULL
3766 && !NILP (Vfontification_functions)
3767 && !NILP (Vrun_hooks)
3768 && (pos = make_number (IT_CHARPOS (*it)),
3769 prop = Fget_char_property (pos, Qfontified, Qnil),
3770 /* Ignore the special cased nil value always present at EOB since
3771 no amount of fontifying will be able to change it. */
3772 NILP (prop) && IT_CHARPOS (*it) < Z))
3774 ptrdiff_t count = SPECPDL_INDEX ();
3775 Lisp_Object val;
3776 struct buffer *obuf = current_buffer;
3777 ptrdiff_t begv = BEGV, zv = ZV;
3778 bool old_clip_changed = current_buffer->clip_changed;
3780 val = Vfontification_functions;
3781 specbind (Qfontification_functions, Qnil);
3783 eassert (it->end_charpos == ZV);
3785 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3786 safe_call1 (val, pos);
3787 else
3789 Lisp_Object fns, fn;
3791 fns = Qnil;
3793 for (; CONSP (val); val = XCDR (val))
3795 fn = XCAR (val);
3797 if (EQ (fn, Qt))
3799 /* A value of t indicates this hook has a local
3800 binding; it means to run the global binding too.
3801 In a global value, t should not occur. If it
3802 does, we must ignore it to avoid an endless
3803 loop. */
3804 for (fns = Fdefault_value (Qfontification_functions);
3805 CONSP (fns);
3806 fns = XCDR (fns))
3808 fn = XCAR (fns);
3809 if (!EQ (fn, Qt))
3810 safe_call1 (fn, pos);
3813 else
3814 safe_call1 (fn, pos);
3818 unbind_to (count, Qnil);
3820 /* Fontification functions routinely call `save-restriction'.
3821 Normally, this tags clip_changed, which can confuse redisplay
3822 (see discussion in Bug#6671). Since we don't perform any
3823 special handling of fontification changes in the case where
3824 `save-restriction' isn't called, there's no point doing so in
3825 this case either. So, if the buffer's restrictions are
3826 actually left unchanged, reset clip_changed. */
3827 if (obuf == current_buffer)
3829 if (begv == BEGV && zv == ZV)
3830 current_buffer->clip_changed = old_clip_changed;
3832 /* There isn't much we can reasonably do to protect against
3833 misbehaving fontification, but here's a fig leaf. */
3834 else if (BUFFER_LIVE_P (obuf))
3835 set_buffer_internal_1 (obuf);
3837 /* The fontification code may have added/removed text.
3838 It could do even a lot worse, but let's at least protect against
3839 the most obvious case where only the text past `pos' gets changed',
3840 as is/was done in grep.el where some escapes sequences are turned
3841 into face properties (bug#7876). */
3842 it->end_charpos = ZV;
3844 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3845 something. This avoids an endless loop if they failed to
3846 fontify the text for which reason ever. */
3847 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3848 handled = HANDLED_RECOMPUTE_PROPS;
3851 return handled;
3856 /***********************************************************************
3857 Faces
3858 ***********************************************************************/
3860 /* Set up iterator IT from face properties at its current position.
3861 Called from handle_stop. */
3863 static enum prop_handled
3864 handle_face_prop (struct it *it)
3866 int new_face_id;
3867 ptrdiff_t next_stop;
3869 if (!STRINGP (it->string))
3871 new_face_id
3872 = face_at_buffer_position (it->w,
3873 IT_CHARPOS (*it),
3874 &next_stop,
3875 (IT_CHARPOS (*it)
3876 + TEXT_PROP_DISTANCE_LIMIT),
3877 false, it->base_face_id);
3879 /* Is this a start of a run of characters with box face?
3880 Caveat: this can be called for a freshly initialized
3881 iterator; face_id is -1 in this case. We know that the new
3882 face will not change until limit, i.e. if the new face has a
3883 box, all characters up to limit will have one. But, as
3884 usual, we don't know whether limit is really the end. */
3885 if (new_face_id != it->face_id)
3887 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3888 /* If it->face_id is -1, old_face below will be NULL, see
3889 the definition of FACE_FROM_ID_OR_NULL. This will happen
3890 if this is the initial call that gets the face. */
3891 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3893 /* If the value of face_id of the iterator is -1, we have to
3894 look in front of IT's position and see whether there is a
3895 face there that's different from new_face_id. */
3896 if (!old_face && IT_CHARPOS (*it) > BEG)
3898 int prev_face_id = face_before_it_pos (it);
3900 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3903 /* If the new face has a box, but the old face does not,
3904 this is the start of a run of characters with box face,
3905 i.e. this character has a shadow on the left side. */
3906 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3907 && (old_face == NULL || !old_face->box));
3908 it->face_box_p = new_face->box != FACE_NO_BOX;
3911 else
3913 int base_face_id;
3914 ptrdiff_t bufpos;
3915 int i;
3916 Lisp_Object from_overlay
3917 = (it->current.overlay_string_index >= 0
3918 ? it->string_overlays[it->current.overlay_string_index
3919 % OVERLAY_STRING_CHUNK_SIZE]
3920 : Qnil);
3922 /* See if we got to this string directly or indirectly from
3923 an overlay property. That includes the before-string or
3924 after-string of an overlay, strings in display properties
3925 provided by an overlay, their text properties, etc.
3927 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3928 if (! NILP (from_overlay))
3929 for (i = it->sp - 1; i >= 0; i--)
3931 if (it->stack[i].current.overlay_string_index >= 0)
3932 from_overlay
3933 = it->string_overlays[it->stack[i].current.overlay_string_index
3934 % OVERLAY_STRING_CHUNK_SIZE];
3935 else if (! NILP (it->stack[i].from_overlay))
3936 from_overlay = it->stack[i].from_overlay;
3938 if (!NILP (from_overlay))
3939 break;
3942 if (! NILP (from_overlay))
3944 bufpos = IT_CHARPOS (*it);
3945 /* For a string from an overlay, the base face depends
3946 only on text properties and ignores overlays. */
3947 base_face_id
3948 = face_for_overlay_string (it->w,
3949 IT_CHARPOS (*it),
3950 &next_stop,
3951 (IT_CHARPOS (*it)
3952 + TEXT_PROP_DISTANCE_LIMIT),
3953 false,
3954 from_overlay);
3956 else
3958 bufpos = 0;
3960 /* For strings from a `display' property, use the face at
3961 IT's current buffer position as the base face to merge
3962 with, so that overlay strings appear in the same face as
3963 surrounding text, unless they specify their own faces.
3964 For strings from wrap-prefix and line-prefix properties,
3965 use the default face, possibly remapped via
3966 Vface_remapping_alist. */
3967 /* Note that the fact that we use the face at _buffer_
3968 position means that a 'display' property on an overlay
3969 string will not inherit the face of that overlay string,
3970 but will instead revert to the face of buffer text
3971 covered by the overlay. This is visible, e.g., when the
3972 overlay specifies a box face, but neither the buffer nor
3973 the display string do. This sounds like a design bug,
3974 but Emacs always did that since v21.1, so changing that
3975 might be a big deal. */
3976 base_face_id = it->string_from_prefix_prop_p
3977 ? (!NILP (Vface_remapping_alist)
3978 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3979 : DEFAULT_FACE_ID)
3980 : underlying_face_id (it);
3983 new_face_id = face_at_string_position (it->w,
3984 it->string,
3985 IT_STRING_CHARPOS (*it),
3986 bufpos,
3987 &next_stop,
3988 base_face_id, false);
3990 /* Is this a start of a run of characters with box? Caveat:
3991 this can be called for a freshly allocated iterator; face_id
3992 is -1 is this case. We know that the new face will not
3993 change until the next check pos, i.e. if the new face has a
3994 box, all characters up to that position will have a
3995 box. But, as usual, we don't know whether that position
3996 is really the end. */
3997 if (new_face_id != it->face_id)
3999 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4000 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4002 /* If new face has a box but old face hasn't, this is the
4003 start of a run of characters with box, i.e. it has a
4004 shadow on the left side. */
4005 it->start_of_box_run_p
4006 = new_face->box && (old_face == NULL || !old_face->box);
4007 it->face_box_p = new_face->box != FACE_NO_BOX;
4011 it->face_id = new_face_id;
4012 return HANDLED_NORMALLY;
4016 /* Return the ID of the face ``underlying'' IT's current position,
4017 which is in a string. If the iterator is associated with a
4018 buffer, return the face at IT's current buffer position.
4019 Otherwise, use the iterator's base_face_id. */
4021 static int
4022 underlying_face_id (struct it *it)
4024 int face_id = it->base_face_id, i;
4026 eassert (STRINGP (it->string));
4028 for (i = it->sp - 1; i >= 0; --i)
4029 if (NILP (it->stack[i].string))
4030 face_id = it->stack[i].face_id;
4032 return face_id;
4036 /* Compute the face one character before or after the current position
4037 of IT, in the visual order. BEFORE_P means get the face
4038 in front (to the left in L2R paragraphs, to the right in R2L
4039 paragraphs) of IT's screen position. Value is the ID of the face. */
4041 static int
4042 face_before_or_after_it_pos (struct it *it, bool before_p)
4044 int face_id, limit;
4045 ptrdiff_t next_check_charpos;
4046 struct it it_copy;
4047 void *it_copy_data = NULL;
4049 eassert (it->s == NULL);
4051 if (STRINGP (it->string))
4053 ptrdiff_t bufpos, charpos;
4054 int base_face_id;
4056 /* No face change past the end of the string (for the case
4057 we are padding with spaces). No face change before the
4058 string start. */
4059 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4060 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4061 return it->face_id;
4063 if (!it->bidi_p)
4065 /* Set charpos to the position before or after IT's current
4066 position, in the logical order, which in the non-bidi
4067 case is the same as the visual order. */
4068 if (before_p)
4069 charpos = IT_STRING_CHARPOS (*it) - 1;
4070 else if (it->what == IT_COMPOSITION)
4071 /* For composition, we must check the character after the
4072 composition. */
4073 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4074 else
4075 charpos = IT_STRING_CHARPOS (*it) + 1;
4077 else
4079 if (before_p)
4081 /* With bidi iteration, the character before the current
4082 in the visual order cannot be found by simple
4083 iteration, because "reverse" reordering is not
4084 supported. Instead, we need to start from the string
4085 beginning and go all the way to the current string
4086 position, remembering the previous position. */
4087 /* Ignore face changes before the first visible
4088 character on this display line. */
4089 if (it->current_x <= it->first_visible_x)
4090 return it->face_id;
4091 SAVE_IT (it_copy, *it, it_copy_data);
4092 IT_STRING_CHARPOS (it_copy) = 0;
4093 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4097 charpos = IT_STRING_CHARPOS (it_copy);
4098 if (charpos >= SCHARS (it->string))
4099 break;
4100 bidi_move_to_visually_next (&it_copy.bidi_it);
4102 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4104 RESTORE_IT (it, it, it_copy_data);
4106 else
4108 /* Set charpos to the string position of the character
4109 that comes after IT's current position in the visual
4110 order. */
4111 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4113 it_copy = *it;
4114 while (n--)
4115 bidi_move_to_visually_next (&it_copy.bidi_it);
4117 charpos = it_copy.bidi_it.charpos;
4120 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4122 if (it->current.overlay_string_index >= 0)
4123 bufpos = IT_CHARPOS (*it);
4124 else
4125 bufpos = 0;
4127 base_face_id = underlying_face_id (it);
4129 /* Get the face for ASCII, or unibyte. */
4130 face_id = face_at_string_position (it->w,
4131 it->string,
4132 charpos,
4133 bufpos,
4134 &next_check_charpos,
4135 base_face_id, false);
4137 /* Correct the face for charsets different from ASCII. Do it
4138 for the multibyte case only. The face returned above is
4139 suitable for unibyte text if IT->string is unibyte. */
4140 if (STRING_MULTIBYTE (it->string))
4142 struct text_pos pos1 = string_pos (charpos, it->string);
4143 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4144 int c, len;
4145 struct face *face = FACE_FROM_ID (it->f, face_id);
4147 c = string_char_and_length (p, &len);
4148 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4151 else
4153 struct text_pos pos;
4155 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4156 || (IT_CHARPOS (*it) <= BEGV && before_p))
4157 return it->face_id;
4159 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4160 pos = it->current.pos;
4162 if (!it->bidi_p)
4164 if (before_p)
4165 DEC_TEXT_POS (pos, it->multibyte_p);
4166 else
4168 if (it->what == IT_COMPOSITION)
4170 /* For composition, we must check the position after
4171 the composition. */
4172 pos.charpos += it->cmp_it.nchars;
4173 pos.bytepos += it->len;
4175 else
4176 INC_TEXT_POS (pos, it->multibyte_p);
4179 else
4181 if (before_p)
4183 int current_x;
4185 /* With bidi iteration, the character before the current
4186 in the visual order cannot be found by simple
4187 iteration, because "reverse" reordering is not
4188 supported. Instead, we need to use the move_it_*
4189 family of functions, and move to the previous
4190 character starting from the beginning of the visual
4191 line. */
4192 /* Ignore face changes before the first visible
4193 character on this display line. */
4194 if (it->current_x <= it->first_visible_x)
4195 return it->face_id;
4196 SAVE_IT (it_copy, *it, it_copy_data);
4197 /* Implementation note: Since move_it_in_display_line
4198 works in the iterator geometry, and thinks the first
4199 character is always the leftmost, even in R2L lines,
4200 we don't need to distinguish between the R2L and L2R
4201 cases here. */
4202 current_x = it_copy.current_x;
4203 move_it_vertically_backward (&it_copy, 0);
4204 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4205 pos = it_copy.current.pos;
4206 RESTORE_IT (it, it, it_copy_data);
4208 else
4210 /* Set charpos to the buffer position of the character
4211 that comes after IT's current position in the visual
4212 order. */
4213 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4215 it_copy = *it;
4216 while (n--)
4217 bidi_move_to_visually_next (&it_copy.bidi_it);
4219 SET_TEXT_POS (pos,
4220 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4223 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4225 /* Determine face for CHARSET_ASCII, or unibyte. */
4226 face_id = face_at_buffer_position (it->w,
4227 CHARPOS (pos),
4228 &next_check_charpos,
4229 limit, false, -1);
4231 /* Correct the face for charsets different from ASCII. Do it
4232 for the multibyte case only. The face returned above is
4233 suitable for unibyte text if current_buffer is unibyte. */
4234 if (it->multibyte_p)
4236 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4237 struct face *face = FACE_FROM_ID (it->f, face_id);
4238 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4242 return face_id;
4247 /***********************************************************************
4248 Invisible text
4249 ***********************************************************************/
4251 /* Set up iterator IT from invisible properties at its current
4252 position. Called from handle_stop. */
4254 static enum prop_handled
4255 handle_invisible_prop (struct it *it)
4257 enum prop_handled handled = HANDLED_NORMALLY;
4258 int invis;
4259 Lisp_Object prop;
4261 if (STRINGP (it->string))
4263 Lisp_Object end_charpos, limit;
4265 /* Get the value of the invisible text property at the
4266 current position. Value will be nil if there is no such
4267 property. */
4268 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4269 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4270 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4272 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4274 /* Record whether we have to display an ellipsis for the
4275 invisible text. */
4276 bool display_ellipsis_p = (invis == 2);
4277 ptrdiff_t len, endpos;
4279 handled = HANDLED_RECOMPUTE_PROPS;
4281 /* Get the position at which the next visible text can be
4282 found in IT->string, if any. */
4283 endpos = len = SCHARS (it->string);
4284 XSETINT (limit, len);
4287 end_charpos
4288 = Fnext_single_property_change (end_charpos, Qinvisible,
4289 it->string, limit);
4290 /* Since LIMIT is always an integer, so should be the
4291 value returned by Fnext_single_property_change. */
4292 eassert (INTEGERP (end_charpos));
4293 if (INTEGERP (end_charpos))
4295 endpos = XFASTINT (end_charpos);
4296 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4297 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4298 if (invis == 2)
4299 display_ellipsis_p = true;
4301 else /* Should never happen; but if it does, exit the loop. */
4302 endpos = len;
4304 while (invis != 0 && endpos < len);
4306 if (display_ellipsis_p)
4307 it->ellipsis_p = true;
4309 if (endpos < len)
4311 /* Text at END_CHARPOS is visible. Move IT there. */
4312 struct text_pos old;
4313 ptrdiff_t oldpos;
4315 old = it->current.string_pos;
4316 oldpos = CHARPOS (old);
4317 if (it->bidi_p)
4319 if (it->bidi_it.first_elt
4320 && it->bidi_it.charpos < SCHARS (it->string))
4321 bidi_paragraph_init (it->paragraph_embedding,
4322 &it->bidi_it, true);
4323 /* Bidi-iterate out of the invisible text. */
4326 bidi_move_to_visually_next (&it->bidi_it);
4328 while (oldpos <= it->bidi_it.charpos
4329 && it->bidi_it.charpos < endpos);
4331 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4332 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4333 if (IT_CHARPOS (*it) >= endpos)
4334 it->prev_stop = endpos;
4336 else
4338 IT_STRING_CHARPOS (*it) = endpos;
4339 compute_string_pos (&it->current.string_pos, old, it->string);
4342 else
4344 /* The rest of the string is invisible. If this is an
4345 overlay string, proceed with the next overlay string
4346 or whatever comes and return a character from there. */
4347 if (it->current.overlay_string_index >= 0
4348 && !display_ellipsis_p)
4350 next_overlay_string (it);
4351 /* Don't check for overlay strings when we just
4352 finished processing them. */
4353 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4355 else
4357 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4358 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4363 else
4365 ptrdiff_t newpos, next_stop, start_charpos, tem;
4366 Lisp_Object pos, overlay;
4368 /* First of all, is there invisible text at this position? */
4369 tem = start_charpos = IT_CHARPOS (*it);
4370 pos = make_number (tem);
4371 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4372 &overlay);
4373 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4375 /* If we are on invisible text, skip over it. */
4376 if (invis != 0 && start_charpos < it->end_charpos)
4378 /* Record whether we have to display an ellipsis for the
4379 invisible text. */
4380 bool display_ellipsis_p = invis == 2;
4382 handled = HANDLED_RECOMPUTE_PROPS;
4384 /* Loop skipping over invisible text. The loop is left at
4385 ZV or with IT on the first char being visible again. */
4388 /* Try to skip some invisible text. Return value is the
4389 position reached which can be equal to where we start
4390 if there is nothing invisible there. This skips both
4391 over invisible text properties and overlays with
4392 invisible property. */
4393 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4395 /* If we skipped nothing at all we weren't at invisible
4396 text in the first place. If everything to the end of
4397 the buffer was skipped, end the loop. */
4398 if (newpos == tem || newpos >= ZV)
4399 invis = 0;
4400 else
4402 /* We skipped some characters but not necessarily
4403 all there are. Check if we ended up on visible
4404 text. Fget_char_property returns the property of
4405 the char before the given position, i.e. if we
4406 get invis = 0, this means that the char at
4407 newpos is visible. */
4408 pos = make_number (newpos);
4409 prop = Fget_char_property (pos, Qinvisible, it->window);
4410 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4413 /* If we ended up on invisible text, proceed to
4414 skip starting with next_stop. */
4415 if (invis != 0)
4416 tem = next_stop;
4418 /* If there are adjacent invisible texts, don't lose the
4419 second one's ellipsis. */
4420 if (invis == 2)
4421 display_ellipsis_p = true;
4423 while (invis != 0);
4425 /* The position newpos is now either ZV or on visible text. */
4426 if (it->bidi_p)
4428 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4429 bool on_newline
4430 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4431 bool after_newline
4432 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4434 /* If the invisible text ends on a newline or on a
4435 character after a newline, we can avoid the costly,
4436 character by character, bidi iteration to NEWPOS, and
4437 instead simply reseat the iterator there. That's
4438 because all bidi reordering information is tossed at
4439 the newline. This is a big win for modes that hide
4440 complete lines, like Outline, Org, etc. */
4441 if (on_newline || after_newline)
4443 struct text_pos tpos;
4444 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4446 SET_TEXT_POS (tpos, newpos, bpos);
4447 reseat_1 (it, tpos, false);
4448 /* If we reseat on a newline/ZV, we need to prep the
4449 bidi iterator for advancing to the next character
4450 after the newline/EOB, keeping the current paragraph
4451 direction (so that PRODUCE_GLYPHS does TRT wrt
4452 prepending/appending glyphs to a glyph row). */
4453 if (on_newline)
4455 it->bidi_it.first_elt = false;
4456 it->bidi_it.paragraph_dir = pdir;
4457 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4458 it->bidi_it.nchars = 1;
4459 it->bidi_it.ch_len = 1;
4462 else /* Must use the slow method. */
4464 /* With bidi iteration, the region of invisible text
4465 could start and/or end in the middle of a
4466 non-base embedding level. Therefore, we need to
4467 skip invisible text using the bidi iterator,
4468 starting at IT's current position, until we find
4469 ourselves outside of the invisible text.
4470 Skipping invisible text _after_ bidi iteration
4471 avoids affecting the visual order of the
4472 displayed text when invisible properties are
4473 added or removed. */
4474 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4476 /* If we were `reseat'ed to a new paragraph,
4477 determine the paragraph base direction. We
4478 need to do it now because
4479 next_element_from_buffer may not have a
4480 chance to do it, if we are going to skip any
4481 text at the beginning, which resets the
4482 FIRST_ELT flag. */
4483 bidi_paragraph_init (it->paragraph_embedding,
4484 &it->bidi_it, true);
4488 bidi_move_to_visually_next (&it->bidi_it);
4490 while (it->stop_charpos <= it->bidi_it.charpos
4491 && it->bidi_it.charpos < newpos);
4492 IT_CHARPOS (*it) = it->bidi_it.charpos;
4493 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4494 /* If we overstepped NEWPOS, record its position in
4495 the iterator, so that we skip invisible text if
4496 later the bidi iteration lands us in the
4497 invisible region again. */
4498 if (IT_CHARPOS (*it) >= newpos)
4499 it->prev_stop = newpos;
4502 else
4504 IT_CHARPOS (*it) = newpos;
4505 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4508 if (display_ellipsis_p)
4510 /* Make sure that the glyphs of the ellipsis will get
4511 correct `charpos' values. If we would not update
4512 it->position here, the glyphs would belong to the
4513 last visible character _before_ the invisible
4514 text, which confuses `set_cursor_from_row'.
4516 We use the last invisible position instead of the
4517 first because this way the cursor is always drawn on
4518 the first "." of the ellipsis, whenever PT is inside
4519 the invisible text. Otherwise the cursor would be
4520 placed _after_ the ellipsis when the point is after the
4521 first invisible character. */
4522 if (!STRINGP (it->object))
4524 it->position.charpos = newpos - 1;
4525 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4529 /* If there are before-strings at the start of invisible
4530 text, and the text is invisible because of a text
4531 property, arrange to show before-strings because 20.x did
4532 it that way. (If the text is invisible because of an
4533 overlay property instead of a text property, this is
4534 already handled in the overlay code.) */
4535 if (NILP (overlay)
4536 && get_overlay_strings (it, it->stop_charpos))
4538 handled = HANDLED_RECOMPUTE_PROPS;
4539 if (it->sp > 0)
4541 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4542 /* The call to get_overlay_strings above recomputes
4543 it->stop_charpos, but it only considers changes
4544 in properties and overlays beyond iterator's
4545 current position. This causes us to miss changes
4546 that happen exactly where the invisible property
4547 ended. So we play it safe here and force the
4548 iterator to check for potential stop positions
4549 immediately after the invisible text. Note that
4550 if get_overlay_strings returns true, it
4551 normally also pushed the iterator stack, so we
4552 need to update the stop position in the slot
4553 below the current one. */
4554 it->stack[it->sp - 1].stop_charpos
4555 = CHARPOS (it->stack[it->sp - 1].current.pos);
4558 else if (display_ellipsis_p)
4560 it->ellipsis_p = true;
4561 /* Let the ellipsis display before
4562 considering any properties of the following char.
4563 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4564 handled = HANDLED_RETURN;
4569 return handled;
4573 /* Make iterator IT return `...' next.
4574 Replaces LEN characters from buffer. */
4576 static void
4577 setup_for_ellipsis (struct it *it, int len)
4579 /* Use the display table definition for `...'. Invalid glyphs
4580 will be handled by the method returning elements from dpvec. */
4581 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4583 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4584 it->dpvec = v->contents;
4585 it->dpend = v->contents + v->header.size;
4587 else
4589 /* Default `...'. */
4590 it->dpvec = default_invis_vector;
4591 it->dpend = default_invis_vector + 3;
4594 it->dpvec_char_len = len;
4595 it->current.dpvec_index = 0;
4596 it->dpvec_face_id = -1;
4598 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4599 face as the preceding text. IT->saved_face_id was set in
4600 handle_stop to the face of the preceding character, and will be
4601 different from IT->face_id only if the invisible text skipped in
4602 handle_invisible_prop has some non-default face on its first
4603 character. We thus ignore the face of the invisible text when we
4604 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4605 if (it->saved_face_id >= 0)
4606 it->face_id = it->saved_face_id;
4608 /* If the ellipsis represents buffer text, it means we advanced in
4609 the buffer, so we should no longer ignore overlay strings. */
4610 if (it->method == GET_FROM_BUFFER)
4611 it->ignore_overlay_strings_at_pos_p = false;
4613 it->method = GET_FROM_DISPLAY_VECTOR;
4614 it->ellipsis_p = true;
4619 /***********************************************************************
4620 'display' property
4621 ***********************************************************************/
4623 /* Set up iterator IT from `display' property at its current position.
4624 Called from handle_stop.
4625 We return HANDLED_RETURN if some part of the display property
4626 overrides the display of the buffer text itself.
4627 Otherwise we return HANDLED_NORMALLY. */
4629 static enum prop_handled
4630 handle_display_prop (struct it *it)
4632 Lisp_Object propval, object, overlay;
4633 struct text_pos *position;
4634 ptrdiff_t bufpos;
4635 /* Nonzero if some property replaces the display of the text itself. */
4636 int display_replaced = 0;
4638 if (STRINGP (it->string))
4640 object = it->string;
4641 position = &it->current.string_pos;
4642 bufpos = CHARPOS (it->current.pos);
4644 else
4646 XSETWINDOW (object, it->w);
4647 position = &it->current.pos;
4648 bufpos = CHARPOS (*position);
4651 /* Reset those iterator values set from display property values. */
4652 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4653 it->space_width = Qnil;
4654 it->font_height = Qnil;
4655 it->voffset = 0;
4657 /* We don't support recursive `display' properties, i.e. string
4658 values that have a string `display' property, that have a string
4659 `display' property etc. */
4660 if (!it->string_from_display_prop_p)
4661 it->area = TEXT_AREA;
4663 propval = get_char_property_and_overlay (make_number (position->charpos),
4664 Qdisplay, object, &overlay);
4665 if (NILP (propval))
4666 return HANDLED_NORMALLY;
4667 /* Now OVERLAY is the overlay that gave us this property, or nil
4668 if it was a text property. */
4670 if (!STRINGP (it->string))
4671 object = it->w->contents;
4673 display_replaced = handle_display_spec (it, propval, object, overlay,
4674 position, bufpos,
4675 FRAME_WINDOW_P (it->f));
4676 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4679 /* Subroutine of handle_display_prop. Returns non-zero if the display
4680 specification in SPEC is a replacing specification, i.e. it would
4681 replace the text covered by `display' property with something else,
4682 such as an image or a display string. If SPEC includes any kind or
4683 `(space ...) specification, the value is 2; this is used by
4684 compute_display_string_pos, which see.
4686 See handle_single_display_spec for documentation of arguments.
4687 FRAME_WINDOW_P is true if the window being redisplayed is on a
4688 GUI frame; this argument is used only if IT is NULL, see below.
4690 IT can be NULL, if this is called by the bidi reordering code
4691 through compute_display_string_pos, which see. In that case, this
4692 function only examines SPEC, but does not otherwise "handle" it, in
4693 the sense that it doesn't set up members of IT from the display
4694 spec. */
4695 static int
4696 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4697 Lisp_Object overlay, struct text_pos *position,
4698 ptrdiff_t bufpos, bool frame_window_p)
4700 int replacing = 0;
4702 if (CONSP (spec)
4703 /* Simple specifications. */
4704 && !EQ (XCAR (spec), Qimage)
4705 #ifdef HAVE_XWIDGETS
4706 && !EQ (XCAR (spec), Qxwidget)
4707 #endif
4708 && !EQ (XCAR (spec), Qspace)
4709 && !EQ (XCAR (spec), Qwhen)
4710 && !EQ (XCAR (spec), Qslice)
4711 && !EQ (XCAR (spec), Qspace_width)
4712 && !EQ (XCAR (spec), Qheight)
4713 && !EQ (XCAR (spec), Qraise)
4714 /* Marginal area specifications. */
4715 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4716 && !EQ (XCAR (spec), Qleft_fringe)
4717 && !EQ (XCAR (spec), Qright_fringe)
4718 && !NILP (XCAR (spec)))
4720 for (; CONSP (spec); spec = XCDR (spec))
4722 int rv = handle_single_display_spec (it, XCAR (spec), object,
4723 overlay, position, bufpos,
4724 replacing, frame_window_p);
4725 if (rv != 0)
4727 replacing = rv;
4728 /* If some text in a string is replaced, `position' no
4729 longer points to the position of `object'. */
4730 if (!it || STRINGP (object))
4731 break;
4735 else if (VECTORP (spec))
4737 ptrdiff_t i;
4738 for (i = 0; i < ASIZE (spec); ++i)
4740 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4741 overlay, position, bufpos,
4742 replacing, frame_window_p);
4743 if (rv != 0)
4745 replacing = rv;
4746 /* If some text in a string is replaced, `position' no
4747 longer points to the position of `object'. */
4748 if (!it || STRINGP (object))
4749 break;
4753 else
4754 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4755 bufpos, 0, frame_window_p);
4756 return replacing;
4759 /* Value is the position of the end of the `display' property starting
4760 at START_POS in OBJECT. */
4762 static struct text_pos
4763 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4765 Lisp_Object end;
4766 struct text_pos end_pos;
4768 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4769 Qdisplay, object, Qnil);
4770 CHARPOS (end_pos) = XFASTINT (end);
4771 if (STRINGP (object))
4772 compute_string_pos (&end_pos, start_pos, it->string);
4773 else
4774 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4776 return end_pos;
4780 /* Set up IT from a single `display' property specification SPEC. OBJECT
4781 is the object in which the `display' property was found. *POSITION
4782 is the position in OBJECT at which the `display' property was found.
4783 BUFPOS is the buffer position of OBJECT (different from POSITION if
4784 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4785 previously saw a display specification which already replaced text
4786 display with something else, for example an image; we ignore such
4787 properties after the first one has been processed.
4789 OVERLAY is the overlay this `display' property came from,
4790 or nil if it was a text property.
4792 If SPEC is a `space' or `image' specification, and in some other
4793 cases too, set *POSITION to the position where the `display'
4794 property ends.
4796 If IT is NULL, only examine the property specification in SPEC, but
4797 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4798 is intended to be displayed in a window on a GUI frame.
4800 Value is non-zero if something was found which replaces the display
4801 of buffer or string text. */
4803 static int
4804 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4805 Lisp_Object overlay, struct text_pos *position,
4806 ptrdiff_t bufpos, int display_replaced,
4807 bool frame_window_p)
4809 Lisp_Object form;
4810 Lisp_Object location, value;
4811 struct text_pos start_pos = *position;
4813 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4814 If the result is non-nil, use VALUE instead of SPEC. */
4815 form = Qt;
4816 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4818 spec = XCDR (spec);
4819 if (!CONSP (spec))
4820 return 0;
4821 form = XCAR (spec);
4822 spec = XCDR (spec);
4825 if (!NILP (form) && !EQ (form, Qt))
4827 ptrdiff_t count = SPECPDL_INDEX ();
4829 /* Bind `object' to the object having the `display' property, a
4830 buffer or string. Bind `position' to the position in the
4831 object where the property was found, and `buffer-position'
4832 to the current position in the buffer. */
4834 if (NILP (object))
4835 XSETBUFFER (object, current_buffer);
4836 specbind (Qobject, object);
4837 specbind (Qposition, make_number (CHARPOS (*position)));
4838 specbind (Qbuffer_position, make_number (bufpos));
4839 form = safe_eval (form);
4840 unbind_to (count, Qnil);
4843 if (NILP (form))
4844 return 0;
4846 /* Handle `(height HEIGHT)' specifications. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qheight)
4849 && CONSP (XCDR (spec)))
4851 if (it)
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4856 it->font_height = XCAR (XCDR (spec));
4857 if (!NILP (it->font_height))
4859 int new_height = -1;
4861 if (CONSP (it->font_height)
4862 && (EQ (XCAR (it->font_height), Qplus)
4863 || EQ (XCAR (it->font_height), Qminus))
4864 && CONSP (XCDR (it->font_height))
4865 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4867 /* `(+ N)' or `(- N)' where N is an integer. */
4868 int steps = XINT (XCAR (XCDR (it->font_height)));
4869 if (EQ (XCAR (it->font_height), Qplus))
4870 steps = - steps;
4871 it->face_id = smaller_face (it->f, it->face_id, steps);
4873 else if (FUNCTIONP (it->font_height))
4875 /* Call function with current height as argument.
4876 Value is the new height. */
4877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4878 Lisp_Object height;
4879 height = safe_call1 (it->font_height,
4880 face->lface[LFACE_HEIGHT_INDEX]);
4881 if (NUMBERP (height))
4882 new_height = XFLOATINT (height);
4884 else if (NUMBERP (it->font_height))
4886 /* Value is a multiple of the canonical char height. */
4887 struct face *f;
4889 f = FACE_FROM_ID (it->f,
4890 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4891 new_height = (XFLOATINT (it->font_height)
4892 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4894 else
4896 /* Evaluate IT->font_height with `height' bound to the
4897 current specified height to get the new height. */
4898 ptrdiff_t count = SPECPDL_INDEX ();
4899 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4901 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4902 value = safe_eval (it->font_height);
4903 unbind_to (count, Qnil);
4905 if (NUMBERP (value))
4906 new_height = XFLOATINT (value);
4909 if (new_height > 0)
4910 it->face_id = face_with_height (it->f, it->face_id, new_height);
4914 return 0;
4917 /* Handle `(space-width WIDTH)'. */
4918 if (CONSP (spec)
4919 && EQ (XCAR (spec), Qspace_width)
4920 && CONSP (XCDR (spec)))
4922 if (it)
4924 if (!FRAME_WINDOW_P (it->f))
4925 return 0;
4927 value = XCAR (XCDR (spec));
4928 if (NUMBERP (value) && XFLOATINT (value) > 0)
4929 it->space_width = value;
4932 return 0;
4935 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4936 if (CONSP (spec)
4937 && EQ (XCAR (spec), Qslice))
4939 Lisp_Object tem;
4941 if (it)
4943 if (!FRAME_WINDOW_P (it->f))
4944 return 0;
4946 if (tem = XCDR (spec), CONSP (tem))
4948 it->slice.x = XCAR (tem);
4949 if (tem = XCDR (tem), CONSP (tem))
4951 it->slice.y = XCAR (tem);
4952 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.width = XCAR (tem);
4955 if (tem = XCDR (tem), CONSP (tem))
4956 it->slice.height = XCAR (tem);
4962 return 0;
4965 /* Handle `(raise FACTOR)'. */
4966 if (CONSP (spec)
4967 && EQ (XCAR (spec), Qraise)
4968 && CONSP (XCDR (spec)))
4970 if (it)
4972 if (!FRAME_WINDOW_P (it->f))
4973 return 0;
4975 #ifdef HAVE_WINDOW_SYSTEM
4976 value = XCAR (XCDR (spec));
4977 if (NUMBERP (value))
4979 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4980 it->voffset = - (XFLOATINT (value)
4981 * (normal_char_height (face->font, -1)));
4983 #endif /* HAVE_WINDOW_SYSTEM */
4986 return 0;
4989 /* Don't handle the other kinds of display specifications
4990 inside a string that we got from a `display' property. */
4991 if (it && it->string_from_display_prop_p)
4992 return 0;
4994 /* Characters having this form of property are not displayed, so
4995 we have to find the end of the property. */
4996 if (it)
4998 start_pos = *position;
4999 *position = display_prop_end (it, object, start_pos);
5000 /* If the display property comes from an overlay, don't consider
5001 any potential stop_charpos values before the end of that
5002 overlay. Since display_prop_end will happily find another
5003 'display' property coming from some other overlay or text
5004 property on buffer positions before this overlay's end, we
5005 need to ignore them, or else we risk displaying this
5006 overlay's display string/image twice. */
5007 if (!NILP (overlay))
5009 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5011 if (ovendpos > CHARPOS (*position))
5012 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5015 value = Qnil;
5017 /* Stop the scan at that end position--we assume that all
5018 text properties change there. */
5019 if (it)
5020 it->stop_charpos = position->charpos;
5022 /* Handle `(left-fringe BITMAP [FACE])'
5023 and `(right-fringe BITMAP [FACE])'. */
5024 if (CONSP (spec)
5025 && (EQ (XCAR (spec), Qleft_fringe)
5026 || EQ (XCAR (spec), Qright_fringe))
5027 && CONSP (XCDR (spec)))
5029 if (it)
5031 if (!FRAME_WINDOW_P (it->f))
5032 /* If we return here, POSITION has been advanced
5033 across the text with this property. */
5035 /* Synchronize the bidi iterator with POSITION. This is
5036 needed because we are not going to push the iterator
5037 on behalf of this display property, so there will be
5038 no pop_it call to do this synchronization for us. */
5039 if (it->bidi_p)
5041 it->position = *position;
5042 iterate_out_of_display_property (it);
5043 *position = it->position;
5045 return 1;
5048 else if (!frame_window_p)
5049 return 1;
5051 #ifdef HAVE_WINDOW_SYSTEM
5052 value = XCAR (XCDR (spec));
5053 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5054 if (! fringe_bitmap)
5055 /* If we return here, POSITION has been advanced
5056 across the text with this property. */
5058 if (it && it->bidi_p)
5060 it->position = *position;
5061 iterate_out_of_display_property (it);
5062 *position = it->position;
5064 return 1;
5067 if (it)
5069 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5071 if (CONSP (XCDR (XCDR (spec))))
5073 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5074 int face_id2 = lookup_derived_face (it->f, face_name,
5075 FRINGE_FACE_ID, false);
5076 if (face_id2 >= 0)
5077 face_id = face_id2;
5080 /* Save current settings of IT so that we can restore them
5081 when we are finished with the glyph property value. */
5082 push_it (it, position);
5084 it->area = TEXT_AREA;
5085 it->what = IT_IMAGE;
5086 it->image_id = -1; /* no image */
5087 it->position = start_pos;
5088 it->object = NILP (object) ? it->w->contents : object;
5089 it->method = GET_FROM_IMAGE;
5090 it->from_overlay = Qnil;
5091 it->face_id = face_id;
5092 it->from_disp_prop_p = true;
5094 /* Say that we haven't consumed the characters with
5095 `display' property yet. The call to pop_it in
5096 set_iterator_to_next will clean this up. */
5097 *position = start_pos;
5099 if (EQ (XCAR (spec), Qleft_fringe))
5101 it->left_user_fringe_bitmap = fringe_bitmap;
5102 it->left_user_fringe_face_id = face_id;
5104 else
5106 it->right_user_fringe_bitmap = fringe_bitmap;
5107 it->right_user_fringe_face_id = face_id;
5110 #endif /* HAVE_WINDOW_SYSTEM */
5111 return 1;
5114 /* Prepare to handle `((margin left-margin) ...)',
5115 `((margin right-margin) ...)' and `((margin nil) ...)'
5116 prefixes for display specifications. */
5117 location = Qunbound;
5118 if (CONSP (spec) && CONSP (XCAR (spec)))
5120 Lisp_Object tem;
5122 value = XCDR (spec);
5123 if (CONSP (value))
5124 value = XCAR (value);
5126 tem = XCAR (spec);
5127 if (EQ (XCAR (tem), Qmargin)
5128 && (tem = XCDR (tem),
5129 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5130 (NILP (tem)
5131 || EQ (tem, Qleft_margin)
5132 || EQ (tem, Qright_margin))))
5133 location = tem;
5136 if (EQ (location, Qunbound))
5138 location = Qnil;
5139 value = spec;
5142 /* After this point, VALUE is the property after any
5143 margin prefix has been stripped. It must be a string,
5144 an image specification, or `(space ...)'.
5146 LOCATION specifies where to display: `left-margin',
5147 `right-margin' or nil. */
5149 bool valid_p = (STRINGP (value)
5150 #ifdef HAVE_WINDOW_SYSTEM
5151 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5152 && valid_image_p (value))
5153 #endif /* not HAVE_WINDOW_SYSTEM */
5154 || (CONSP (value) && EQ (XCAR (value), Qspace))
5155 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5156 && valid_xwidget_spec_p (value)));
5158 if (valid_p && display_replaced == 0)
5160 int retval = 1;
5162 if (!it)
5164 /* Callers need to know whether the display spec is any kind
5165 of `(space ...)' spec that is about to affect text-area
5166 display. */
5167 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5168 retval = 2;
5169 return retval;
5172 /* Save current settings of IT so that we can restore them
5173 when we are finished with the glyph property value. */
5174 push_it (it, position);
5175 it->from_overlay = overlay;
5176 it->from_disp_prop_p = true;
5178 if (NILP (location))
5179 it->area = TEXT_AREA;
5180 else if (EQ (location, Qleft_margin))
5181 it->area = LEFT_MARGIN_AREA;
5182 else
5183 it->area = RIGHT_MARGIN_AREA;
5185 if (STRINGP (value))
5187 it->string = value;
5188 it->multibyte_p = STRING_MULTIBYTE (it->string);
5189 it->current.overlay_string_index = -1;
5190 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5191 it->end_charpos = it->string_nchars = SCHARS (it->string);
5192 it->method = GET_FROM_STRING;
5193 it->stop_charpos = 0;
5194 it->prev_stop = 0;
5195 it->base_level_stop = 0;
5196 it->string_from_display_prop_p = true;
5197 /* Say that we haven't consumed the characters with
5198 `display' property yet. The call to pop_it in
5199 set_iterator_to_next will clean this up. */
5200 if (BUFFERP (object))
5201 *position = start_pos;
5203 /* Force paragraph direction to be that of the parent
5204 object. If the parent object's paragraph direction is
5205 not yet determined, default to L2R. */
5206 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5207 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5208 else
5209 it->paragraph_embedding = L2R;
5211 /* Set up the bidi iterator for this display string. */
5212 if (it->bidi_p)
5214 it->bidi_it.string.lstring = it->string;
5215 it->bidi_it.string.s = NULL;
5216 it->bidi_it.string.schars = it->end_charpos;
5217 it->bidi_it.string.bufpos = bufpos;
5218 it->bidi_it.string.from_disp_str = true;
5219 it->bidi_it.string.unibyte = !it->multibyte_p;
5220 it->bidi_it.w = it->w;
5221 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5224 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5226 it->method = GET_FROM_STRETCH;
5227 it->object = value;
5228 *position = it->position = start_pos;
5229 retval = 1 + (it->area == TEXT_AREA);
5231 else if (valid_xwidget_spec_p (value))
5233 it->what = IT_XWIDGET;
5234 it->method = GET_FROM_XWIDGET;
5235 it->position = start_pos;
5236 it->object = NILP (object) ? it->w->contents : object;
5237 *position = start_pos;
5238 it->xwidget = lookup_xwidget (value);
5240 #ifdef HAVE_WINDOW_SYSTEM
5241 else
5243 it->what = IT_IMAGE;
5244 it->image_id = lookup_image (it->f, value);
5245 it->position = start_pos;
5246 it->object = NILP (object) ? it->w->contents : object;
5247 it->method = GET_FROM_IMAGE;
5249 /* Say that we haven't consumed the characters with
5250 `display' property yet. The call to pop_it in
5251 set_iterator_to_next will clean this up. */
5252 *position = start_pos;
5254 #endif /* HAVE_WINDOW_SYSTEM */
5256 return retval;
5259 /* Invalid property or property not supported. Restore
5260 POSITION to what it was before. */
5261 *position = start_pos;
5262 return 0;
5265 /* Check if PROP is a display property value whose text should be
5266 treated as intangible. OVERLAY is the overlay from which PROP
5267 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5268 specify the buffer position covered by PROP. */
5270 bool
5271 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5272 ptrdiff_t charpos, ptrdiff_t bytepos)
5274 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5275 struct text_pos position;
5277 SET_TEXT_POS (position, charpos, bytepos);
5278 return (handle_display_spec (NULL, prop, Qnil, overlay,
5279 &position, charpos, frame_window_p)
5280 != 0);
5284 /* Return true if PROP is a display sub-property value containing STRING.
5286 Implementation note: this and the following function are really
5287 special cases of handle_display_spec and
5288 handle_single_display_spec, and should ideally use the same code.
5289 Until they do, these two pairs must be consistent and must be
5290 modified in sync. */
5292 static bool
5293 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5295 if (EQ (string, prop))
5296 return true;
5298 /* Skip over `when FORM'. */
5299 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5301 prop = XCDR (prop);
5302 if (!CONSP (prop))
5303 return false;
5304 /* Actually, the condition following `when' should be eval'ed,
5305 like handle_single_display_spec does, and we should return
5306 false if it evaluates to nil. However, this function is
5307 called only when the buffer was already displayed and some
5308 glyph in the glyph matrix was found to come from a display
5309 string. Therefore, the condition was already evaluated, and
5310 the result was non-nil, otherwise the display string wouldn't
5311 have been displayed and we would have never been called for
5312 this property. Thus, we can skip the evaluation and assume
5313 its result is non-nil. */
5314 prop = XCDR (prop);
5317 if (CONSP (prop))
5318 /* Skip over `margin LOCATION'. */
5319 if (EQ (XCAR (prop), Qmargin))
5321 prop = XCDR (prop);
5322 if (!CONSP (prop))
5323 return false;
5325 prop = XCDR (prop);
5326 if (!CONSP (prop))
5327 return false;
5330 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5334 /* Return true if STRING appears in the `display' property PROP. */
5336 static bool
5337 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5339 if (CONSP (prop)
5340 && !EQ (XCAR (prop), Qwhen)
5341 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5343 /* A list of sub-properties. */
5344 while (CONSP (prop))
5346 if (single_display_spec_string_p (XCAR (prop), string))
5347 return true;
5348 prop = XCDR (prop);
5351 else if (VECTORP (prop))
5353 /* A vector of sub-properties. */
5354 ptrdiff_t i;
5355 for (i = 0; i < ASIZE (prop); ++i)
5356 if (single_display_spec_string_p (AREF (prop, i), string))
5357 return true;
5359 else
5360 return single_display_spec_string_p (prop, string);
5362 return false;
5365 /* Look for STRING in overlays and text properties in the current
5366 buffer, between character positions FROM and TO (excluding TO).
5367 BACK_P means look back (in this case, TO is supposed to be
5368 less than FROM).
5369 Value is the first character position where STRING was found, or
5370 zero if it wasn't found before hitting TO.
5372 This function may only use code that doesn't eval because it is
5373 called asynchronously from note_mouse_highlight. */
5375 static ptrdiff_t
5376 string_buffer_position_lim (Lisp_Object string,
5377 ptrdiff_t from, ptrdiff_t to, bool back_p)
5379 Lisp_Object limit, prop, pos;
5380 bool found = false;
5382 pos = make_number (max (from, BEGV));
5384 if (!back_p) /* looking forward */
5386 limit = make_number (min (to, ZV));
5387 while (!found && !EQ (pos, limit))
5389 prop = Fget_char_property (pos, Qdisplay, Qnil);
5390 if (!NILP (prop) && display_prop_string_p (prop, string))
5391 found = true;
5392 else
5393 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5394 limit);
5397 else /* looking back */
5399 limit = make_number (max (to, BEGV));
5400 while (!found && !EQ (pos, limit))
5402 prop = Fget_char_property (pos, Qdisplay, Qnil);
5403 if (!NILP (prop) && display_prop_string_p (prop, string))
5404 found = true;
5405 else
5406 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5407 limit);
5411 return found ? XINT (pos) : 0;
5414 /* Determine which buffer position in current buffer STRING comes from.
5415 AROUND_CHARPOS is an approximate position where it could come from.
5416 Value is the buffer position or 0 if it couldn't be determined.
5418 This function is necessary because we don't record buffer positions
5419 in glyphs generated from strings (to keep struct glyph small).
5420 This function may only use code that doesn't eval because it is
5421 called asynchronously from note_mouse_highlight. */
5423 static ptrdiff_t
5424 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5426 const int MAX_DISTANCE = 1000;
5427 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5428 around_charpos + MAX_DISTANCE,
5429 false);
5431 if (!found)
5432 found = string_buffer_position_lim (string, around_charpos,
5433 around_charpos - MAX_DISTANCE, true);
5434 return found;
5439 /***********************************************************************
5440 `composition' property
5441 ***********************************************************************/
5443 /* Set up iterator IT from `composition' property at its current
5444 position. Called from handle_stop. */
5446 static enum prop_handled
5447 handle_composition_prop (struct it *it)
5449 Lisp_Object prop, string;
5450 ptrdiff_t pos, pos_byte, start, end;
5452 if (STRINGP (it->string))
5454 unsigned char *s;
5456 pos = IT_STRING_CHARPOS (*it);
5457 pos_byte = IT_STRING_BYTEPOS (*it);
5458 string = it->string;
5459 s = SDATA (string) + pos_byte;
5460 it->c = STRING_CHAR (s);
5462 else
5464 pos = IT_CHARPOS (*it);
5465 pos_byte = IT_BYTEPOS (*it);
5466 string = Qnil;
5467 it->c = FETCH_CHAR (pos_byte);
5470 /* If there's a valid composition and point is not inside of the
5471 composition (in the case that the composition is from the current
5472 buffer), draw a glyph composed from the composition components. */
5473 if (find_composition (pos, -1, &start, &end, &prop, string)
5474 && composition_valid_p (start, end, prop)
5475 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5477 if (start < pos)
5478 /* As we can't handle this situation (perhaps font-lock added
5479 a new composition), we just return here hoping that next
5480 redisplay will detect this composition much earlier. */
5481 return HANDLED_NORMALLY;
5482 if (start != pos)
5484 if (STRINGP (it->string))
5485 pos_byte = string_char_to_byte (it->string, start);
5486 else
5487 pos_byte = CHAR_TO_BYTE (start);
5489 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5490 prop, string);
5492 if (it->cmp_it.id >= 0)
5494 it->cmp_it.ch = -1;
5495 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5496 it->cmp_it.nglyphs = -1;
5500 return HANDLED_NORMALLY;
5505 /***********************************************************************
5506 Overlay strings
5507 ***********************************************************************/
5509 /* The following structure is used to record overlay strings for
5510 later sorting in load_overlay_strings. */
5512 struct overlay_entry
5514 Lisp_Object overlay;
5515 Lisp_Object string;
5516 EMACS_INT priority;
5517 bool after_string_p;
5521 /* Set up iterator IT from overlay strings at its current position.
5522 Called from handle_stop. */
5524 static enum prop_handled
5525 handle_overlay_change (struct it *it)
5527 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5528 return HANDLED_RECOMPUTE_PROPS;
5529 else
5530 return HANDLED_NORMALLY;
5534 /* Set up the next overlay string for delivery by IT, if there is an
5535 overlay string to deliver. Called by set_iterator_to_next when the
5536 end of the current overlay string is reached. If there are more
5537 overlay strings to display, IT->string and
5538 IT->current.overlay_string_index are set appropriately here.
5539 Otherwise IT->string is set to nil. */
5541 static void
5542 next_overlay_string (struct it *it)
5544 ++it->current.overlay_string_index;
5545 if (it->current.overlay_string_index == it->n_overlay_strings)
5547 /* No more overlay strings. Restore IT's settings to what
5548 they were before overlay strings were processed, and
5549 continue to deliver from current_buffer. */
5551 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5552 pop_it (it);
5553 eassert (it->sp > 0
5554 || (NILP (it->string)
5555 && it->method == GET_FROM_BUFFER
5556 && it->stop_charpos >= BEGV
5557 && it->stop_charpos <= it->end_charpos));
5558 it->current.overlay_string_index = -1;
5559 it->n_overlay_strings = 0;
5560 /* If there's an empty display string on the stack, pop the
5561 stack, to resync the bidi iterator with IT's position. Such
5562 empty strings are pushed onto the stack in
5563 get_overlay_strings_1. */
5564 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5565 pop_it (it);
5567 /* Since we've exhausted overlay strings at this buffer
5568 position, set the flag to ignore overlays until we move to
5569 another position. The flag is reset in
5570 next_element_from_buffer. */
5571 it->ignore_overlay_strings_at_pos_p = true;
5573 /* If we're at the end of the buffer, record that we have
5574 processed the overlay strings there already, so that
5575 next_element_from_buffer doesn't try it again. */
5576 if (NILP (it->string)
5577 && IT_CHARPOS (*it) >= it->end_charpos
5578 && it->overlay_strings_charpos >= it->end_charpos)
5579 it->overlay_strings_at_end_processed_p = true;
5580 /* Note: we reset overlay_strings_charpos only here, to make
5581 sure the just-processed overlays were indeed at EOB.
5582 Otherwise, overlays on text with invisible text property,
5583 which are processed with IT's position past the invisible
5584 text, might fool us into thinking the overlays at EOB were
5585 already processed (linum-mode can cause this, for
5586 example). */
5587 it->overlay_strings_charpos = -1;
5589 else
5591 /* There are more overlay strings to process. If
5592 IT->current.overlay_string_index has advanced to a position
5593 where we must load IT->overlay_strings with more strings, do
5594 it. We must load at the IT->overlay_strings_charpos where
5595 IT->n_overlay_strings was originally computed; when invisible
5596 text is present, this might not be IT_CHARPOS (Bug#7016). */
5597 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5599 if (it->current.overlay_string_index && i == 0)
5600 load_overlay_strings (it, it->overlay_strings_charpos);
5602 /* Initialize IT to deliver display elements from the overlay
5603 string. */
5604 it->string = it->overlay_strings[i];
5605 it->multibyte_p = STRING_MULTIBYTE (it->string);
5606 SET_TEXT_POS (it->current.string_pos, 0, 0);
5607 it->method = GET_FROM_STRING;
5608 it->stop_charpos = 0;
5609 it->end_charpos = SCHARS (it->string);
5610 if (it->cmp_it.stop_pos >= 0)
5611 it->cmp_it.stop_pos = 0;
5612 it->prev_stop = 0;
5613 it->base_level_stop = 0;
5615 /* Set up the bidi iterator for this overlay string. */
5616 if (it->bidi_p)
5618 it->bidi_it.string.lstring = it->string;
5619 it->bidi_it.string.s = NULL;
5620 it->bidi_it.string.schars = SCHARS (it->string);
5621 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5622 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5623 it->bidi_it.string.unibyte = !it->multibyte_p;
5624 it->bidi_it.w = it->w;
5625 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5629 CHECK_IT (it);
5633 /* Compare two overlay_entry structures E1 and E2. Used as a
5634 comparison function for qsort in load_overlay_strings. Overlay
5635 strings for the same position are sorted so that
5637 1. All after-strings come in front of before-strings, except
5638 when they come from the same overlay.
5640 2. Within after-strings, strings are sorted so that overlay strings
5641 from overlays with higher priorities come first.
5643 2. Within before-strings, strings are sorted so that overlay
5644 strings from overlays with higher priorities come last.
5646 Value is analogous to strcmp. */
5649 static int
5650 compare_overlay_entries (const void *e1, const void *e2)
5652 struct overlay_entry const *entry1 = e1;
5653 struct overlay_entry const *entry2 = e2;
5654 int result;
5656 if (entry1->after_string_p != entry2->after_string_p)
5658 /* Let after-strings appear in front of before-strings if
5659 they come from different overlays. */
5660 if (EQ (entry1->overlay, entry2->overlay))
5661 result = entry1->after_string_p ? 1 : -1;
5662 else
5663 result = entry1->after_string_p ? -1 : 1;
5665 else if (entry1->priority != entry2->priority)
5667 if (entry1->after_string_p)
5668 /* After-strings sorted in order of decreasing priority. */
5669 result = entry2->priority < entry1->priority ? -1 : 1;
5670 else
5671 /* Before-strings sorted in order of increasing priority. */
5672 result = entry1->priority < entry2->priority ? -1 : 1;
5674 else
5675 result = 0;
5677 return result;
5681 /* Load the vector IT->overlay_strings with overlay strings from IT's
5682 current buffer position, or from CHARPOS if that is > 0. Set
5683 IT->n_overlays to the total number of overlay strings found.
5685 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5686 a time. On entry into load_overlay_strings,
5687 IT->current.overlay_string_index gives the number of overlay
5688 strings that have already been loaded by previous calls to this
5689 function.
5691 IT->add_overlay_start contains an additional overlay start
5692 position to consider for taking overlay strings from, if non-zero.
5693 This position comes into play when the overlay has an `invisible'
5694 property, and both before and after-strings. When we've skipped to
5695 the end of the overlay, because of its `invisible' property, we
5696 nevertheless want its before-string to appear.
5697 IT->add_overlay_start will contain the overlay start position
5698 in this case.
5700 Overlay strings are sorted so that after-string strings come in
5701 front of before-string strings. Within before and after-strings,
5702 strings are sorted by overlay priority. See also function
5703 compare_overlay_entries. */
5705 static void
5706 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5708 Lisp_Object overlay, window, str, invisible;
5709 struct Lisp_Overlay *ov;
5710 ptrdiff_t start, end;
5711 ptrdiff_t n = 0, i, j;
5712 int invis;
5713 struct overlay_entry entriesbuf[20];
5714 ptrdiff_t size = ARRAYELTS (entriesbuf);
5715 struct overlay_entry *entries = entriesbuf;
5716 USE_SAFE_ALLOCA;
5718 if (charpos <= 0)
5719 charpos = IT_CHARPOS (*it);
5721 /* Append the overlay string STRING of overlay OVERLAY to vector
5722 `entries' which has size `size' and currently contains `n'
5723 elements. AFTER_P means STRING is an after-string of
5724 OVERLAY. */
5725 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5726 do \
5728 Lisp_Object priority; \
5730 if (n == size) \
5732 struct overlay_entry *old = entries; \
5733 SAFE_NALLOCA (entries, 2, size); \
5734 memcpy (entries, old, size * sizeof *entries); \
5735 size *= 2; \
5738 entries[n].string = (STRING); \
5739 entries[n].overlay = (OVERLAY); \
5740 priority = Foverlay_get ((OVERLAY), Qpriority); \
5741 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5742 entries[n].after_string_p = (AFTER_P); \
5743 ++n; \
5745 while (false)
5747 /* Process overlay before the overlay center. */
5748 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5750 XSETMISC (overlay, ov);
5751 eassert (OVERLAYP (overlay));
5752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5755 if (end < charpos)
5756 break;
5758 /* Skip this overlay if it doesn't start or end at IT's current
5759 position. */
5760 if (end != charpos && start != charpos)
5761 continue;
5763 /* Skip this overlay if it doesn't apply to IT->w. */
5764 window = Foverlay_get (overlay, Qwindow);
5765 if (WINDOWP (window) && XWINDOW (window) != it->w)
5766 continue;
5768 /* If the text ``under'' the overlay is invisible, both before-
5769 and after-strings from this overlay are visible; start and
5770 end position are indistinguishable. */
5771 invisible = Foverlay_get (overlay, Qinvisible);
5772 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5774 /* If overlay has a non-empty before-string, record it. */
5775 if ((start == charpos || (end == charpos && invis != 0))
5776 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5777 && SCHARS (str))
5778 RECORD_OVERLAY_STRING (overlay, str, false);
5780 /* If overlay has a non-empty after-string, record it. */
5781 if ((end == charpos || (start == charpos && invis != 0))
5782 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5783 && SCHARS (str))
5784 RECORD_OVERLAY_STRING (overlay, str, true);
5787 /* Process overlays after the overlay center. */
5788 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5790 XSETMISC (overlay, ov);
5791 eassert (OVERLAYP (overlay));
5792 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5793 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5795 if (start > charpos)
5796 break;
5798 /* Skip this overlay if it doesn't start or end at IT's current
5799 position. */
5800 if (end != charpos && start != charpos)
5801 continue;
5803 /* Skip this overlay if it doesn't apply to IT->w. */
5804 window = Foverlay_get (overlay, Qwindow);
5805 if (WINDOWP (window) && XWINDOW (window) != it->w)
5806 continue;
5808 /* If the text ``under'' the overlay is invisible, it has a zero
5809 dimension, and both before- and after-strings apply. */
5810 invisible = Foverlay_get (overlay, Qinvisible);
5811 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5813 /* If overlay has a non-empty before-string, record it. */
5814 if ((start == charpos || (end == charpos && invis != 0))
5815 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5816 && SCHARS (str))
5817 RECORD_OVERLAY_STRING (overlay, str, false);
5819 /* If overlay has a non-empty after-string, record it. */
5820 if ((end == charpos || (start == charpos && invis != 0))
5821 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5822 && SCHARS (str))
5823 RECORD_OVERLAY_STRING (overlay, str, true);
5826 #undef RECORD_OVERLAY_STRING
5828 /* Sort entries. */
5829 if (n > 1)
5830 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5832 /* Record number of overlay strings, and where we computed it. */
5833 it->n_overlay_strings = n;
5834 it->overlay_strings_charpos = charpos;
5836 /* IT->current.overlay_string_index is the number of overlay strings
5837 that have already been consumed by IT. Copy some of the
5838 remaining overlay strings to IT->overlay_strings. */
5839 i = 0;
5840 j = it->current.overlay_string_index;
5841 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5843 it->overlay_strings[i] = entries[j].string;
5844 it->string_overlays[i++] = entries[j++].overlay;
5847 CHECK_IT (it);
5848 SAFE_FREE ();
5852 /* Get the first chunk of overlay strings at IT's current buffer
5853 position, or at CHARPOS if that is > 0. Value is true if at
5854 least one overlay string was found. */
5856 static bool
5857 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5859 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5860 process. This fills IT->overlay_strings with strings, and sets
5861 IT->n_overlay_strings to the total number of strings to process.
5862 IT->pos.overlay_string_index has to be set temporarily to zero
5863 because load_overlay_strings needs this; it must be set to -1
5864 when no overlay strings are found because a zero value would
5865 indicate a position in the first overlay string. */
5866 it->current.overlay_string_index = 0;
5867 load_overlay_strings (it, charpos);
5869 /* If we found overlay strings, set up IT to deliver display
5870 elements from the first one. Otherwise set up IT to deliver
5871 from current_buffer. */
5872 if (it->n_overlay_strings)
5874 /* Make sure we know settings in current_buffer, so that we can
5875 restore meaningful values when we're done with the overlay
5876 strings. */
5877 if (compute_stop_p)
5878 compute_stop_pos (it);
5879 eassert (it->face_id >= 0);
5881 /* Save IT's settings. They are restored after all overlay
5882 strings have been processed. */
5883 eassert (!compute_stop_p || it->sp == 0);
5885 /* When called from handle_stop, there might be an empty display
5886 string loaded. In that case, don't bother saving it. But
5887 don't use this optimization with the bidi iterator, since we
5888 need the corresponding pop_it call to resync the bidi
5889 iterator's position with IT's position, after we are done
5890 with the overlay strings. (The corresponding call to pop_it
5891 in case of an empty display string is in
5892 next_overlay_string.) */
5893 if (!(!it->bidi_p
5894 && STRINGP (it->string) && !SCHARS (it->string)))
5895 push_it (it, NULL);
5897 /* Set up IT to deliver display elements from the first overlay
5898 string. */
5899 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5900 it->string = it->overlay_strings[0];
5901 it->from_overlay = Qnil;
5902 it->stop_charpos = 0;
5903 eassert (STRINGP (it->string));
5904 it->end_charpos = SCHARS (it->string);
5905 it->prev_stop = 0;
5906 it->base_level_stop = 0;
5907 it->multibyte_p = STRING_MULTIBYTE (it->string);
5908 it->method = GET_FROM_STRING;
5909 it->from_disp_prop_p = 0;
5911 /* Force paragraph direction to be that of the parent
5912 buffer. */
5913 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5914 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5915 else
5916 it->paragraph_embedding = L2R;
5918 /* Set up the bidi iterator for this overlay string. */
5919 if (it->bidi_p)
5921 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5923 it->bidi_it.string.lstring = it->string;
5924 it->bidi_it.string.s = NULL;
5925 it->bidi_it.string.schars = SCHARS (it->string);
5926 it->bidi_it.string.bufpos = pos;
5927 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5928 it->bidi_it.string.unibyte = !it->multibyte_p;
5929 it->bidi_it.w = it->w;
5930 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5932 return true;
5935 it->current.overlay_string_index = -1;
5936 return false;
5939 static bool
5940 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5942 it->string = Qnil;
5943 it->method = GET_FROM_BUFFER;
5945 get_overlay_strings_1 (it, charpos, true);
5947 CHECK_IT (it);
5949 /* Value is true if we found at least one overlay string. */
5950 return STRINGP (it->string);
5955 /***********************************************************************
5956 Saving and restoring state
5957 ***********************************************************************/
5959 /* Save current settings of IT on IT->stack. Called, for example,
5960 before setting up IT for an overlay string, to be able to restore
5961 IT's settings to what they were after the overlay string has been
5962 processed. If POSITION is non-NULL, it is the position to save on
5963 the stack instead of IT->position. */
5965 static void
5966 push_it (struct it *it, struct text_pos *position)
5968 struct iterator_stack_entry *p;
5970 eassert (it->sp < IT_STACK_SIZE);
5971 p = it->stack + it->sp;
5973 p->stop_charpos = it->stop_charpos;
5974 p->prev_stop = it->prev_stop;
5975 p->base_level_stop = it->base_level_stop;
5976 p->cmp_it = it->cmp_it;
5977 eassert (it->face_id >= 0);
5978 p->face_id = it->face_id;
5979 p->string = it->string;
5980 p->method = it->method;
5981 p->from_overlay = it->from_overlay;
5982 switch (p->method)
5984 case GET_FROM_IMAGE:
5985 p->u.image.object = it->object;
5986 p->u.image.image_id = it->image_id;
5987 p->u.image.slice = it->slice;
5988 break;
5989 case GET_FROM_STRETCH:
5990 p->u.stretch.object = it->object;
5991 break;
5992 case GET_FROM_XWIDGET:
5993 p->u.xwidget.object = it->object;
5994 break;
5995 case GET_FROM_BUFFER:
5996 case GET_FROM_DISPLAY_VECTOR:
5997 case GET_FROM_STRING:
5998 case GET_FROM_C_STRING:
5999 break;
6000 default:
6001 emacs_abort ();
6003 p->position = position ? *position : it->position;
6004 p->current = it->current;
6005 p->end_charpos = it->end_charpos;
6006 p->string_nchars = it->string_nchars;
6007 p->area = it->area;
6008 p->multibyte_p = it->multibyte_p;
6009 p->avoid_cursor_p = it->avoid_cursor_p;
6010 p->space_width = it->space_width;
6011 p->font_height = it->font_height;
6012 p->voffset = it->voffset;
6013 p->string_from_display_prop_p = it->string_from_display_prop_p;
6014 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6015 p->display_ellipsis_p = false;
6016 p->line_wrap = it->line_wrap;
6017 p->bidi_p = it->bidi_p;
6018 p->paragraph_embedding = it->paragraph_embedding;
6019 p->from_disp_prop_p = it->from_disp_prop_p;
6020 ++it->sp;
6022 /* Save the state of the bidi iterator as well. */
6023 if (it->bidi_p)
6024 bidi_push_it (&it->bidi_it);
6027 static void
6028 iterate_out_of_display_property (struct it *it)
6030 bool buffer_p = !STRINGP (it->string);
6031 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6032 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6034 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6036 /* Maybe initialize paragraph direction. If we are at the beginning
6037 of a new paragraph, next_element_from_buffer may not have a
6038 chance to do that. */
6039 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6040 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6041 /* prev_stop can be zero, so check against BEGV as well. */
6042 while (it->bidi_it.charpos >= bob
6043 && it->prev_stop <= it->bidi_it.charpos
6044 && it->bidi_it.charpos < CHARPOS (it->position)
6045 && it->bidi_it.charpos < eob)
6046 bidi_move_to_visually_next (&it->bidi_it);
6047 /* Record the stop_pos we just crossed, for when we cross it
6048 back, maybe. */
6049 if (it->bidi_it.charpos > CHARPOS (it->position))
6050 it->prev_stop = CHARPOS (it->position);
6051 /* If we ended up not where pop_it put us, resync IT's
6052 positional members with the bidi iterator. */
6053 if (it->bidi_it.charpos != CHARPOS (it->position))
6054 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6055 if (buffer_p)
6056 it->current.pos = it->position;
6057 else
6058 it->current.string_pos = it->position;
6061 /* Restore IT's settings from IT->stack. Called, for example, when no
6062 more overlay strings must be processed, and we return to delivering
6063 display elements from a buffer, or when the end of a string from a
6064 `display' property is reached and we return to delivering display
6065 elements from an overlay string, or from a buffer. */
6067 static void
6068 pop_it (struct it *it)
6070 struct iterator_stack_entry *p;
6071 bool from_display_prop = it->from_disp_prop_p;
6072 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6074 eassert (it->sp > 0);
6075 --it->sp;
6076 p = it->stack + it->sp;
6077 it->stop_charpos = p->stop_charpos;
6078 it->prev_stop = p->prev_stop;
6079 it->base_level_stop = p->base_level_stop;
6080 it->cmp_it = p->cmp_it;
6081 it->face_id = p->face_id;
6082 it->current = p->current;
6083 it->position = p->position;
6084 it->string = p->string;
6085 it->from_overlay = p->from_overlay;
6086 if (NILP (it->string))
6087 SET_TEXT_POS (it->current.string_pos, -1, -1);
6088 it->method = p->method;
6089 switch (it->method)
6091 case GET_FROM_IMAGE:
6092 it->image_id = p->u.image.image_id;
6093 it->object = p->u.image.object;
6094 it->slice = p->u.image.slice;
6095 break;
6096 case GET_FROM_XWIDGET:
6097 it->object = p->u.xwidget.object;
6098 break;
6099 case GET_FROM_STRETCH:
6100 it->object = p->u.stretch.object;
6101 break;
6102 case GET_FROM_BUFFER:
6103 it->object = it->w->contents;
6104 break;
6105 case GET_FROM_STRING:
6107 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6109 /* Restore the face_box_p flag, since it could have been
6110 overwritten by the face of the object that we just finished
6111 displaying. */
6112 if (face)
6113 it->face_box_p = face->box != FACE_NO_BOX;
6114 it->object = it->string;
6116 break;
6117 case GET_FROM_DISPLAY_VECTOR:
6118 if (it->s)
6119 it->method = GET_FROM_C_STRING;
6120 else if (STRINGP (it->string))
6121 it->method = GET_FROM_STRING;
6122 else
6124 it->method = GET_FROM_BUFFER;
6125 it->object = it->w->contents;
6127 break;
6128 case GET_FROM_C_STRING:
6129 break;
6130 default:
6131 emacs_abort ();
6133 it->end_charpos = p->end_charpos;
6134 it->string_nchars = p->string_nchars;
6135 it->area = p->area;
6136 it->multibyte_p = p->multibyte_p;
6137 it->avoid_cursor_p = p->avoid_cursor_p;
6138 it->space_width = p->space_width;
6139 it->font_height = p->font_height;
6140 it->voffset = p->voffset;
6141 it->string_from_display_prop_p = p->string_from_display_prop_p;
6142 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6143 it->line_wrap = p->line_wrap;
6144 it->bidi_p = p->bidi_p;
6145 it->paragraph_embedding = p->paragraph_embedding;
6146 it->from_disp_prop_p = p->from_disp_prop_p;
6147 if (it->bidi_p)
6149 bidi_pop_it (&it->bidi_it);
6150 /* Bidi-iterate until we get out of the portion of text, if any,
6151 covered by a `display' text property or by an overlay with
6152 `display' property. (We cannot just jump there, because the
6153 internal coherency of the bidi iterator state can not be
6154 preserved across such jumps.) We also must determine the
6155 paragraph base direction if the overlay we just processed is
6156 at the beginning of a new paragraph. */
6157 if (from_display_prop
6158 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6159 iterate_out_of_display_property (it);
6161 eassert ((BUFFERP (it->object)
6162 && IT_CHARPOS (*it) == it->bidi_it.charpos
6163 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6164 || (STRINGP (it->object)
6165 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6166 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6167 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6169 /* If we move the iterator over text covered by a display property
6170 to a new buffer position, any info about previously seen overlays
6171 is no longer valid. */
6172 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6173 it->ignore_overlay_strings_at_pos_p = false;
6178 /***********************************************************************
6179 Moving over lines
6180 ***********************************************************************/
6182 /* Set IT's current position to the previous line start. */
6184 static void
6185 back_to_previous_line_start (struct it *it)
6187 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6189 DEC_BOTH (cp, bp);
6190 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6194 /* Move IT to the next line start.
6196 Value is true if a newline was found. Set *SKIPPED_P to true if
6197 we skipped over part of the text (as opposed to moving the iterator
6198 continuously over the text). Otherwise, don't change the value
6199 of *SKIPPED_P.
6201 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6202 iterator on the newline, if it was found.
6204 Newlines may come from buffer text, overlay strings, or strings
6205 displayed via the `display' property. That's the reason we can't
6206 simply use find_newline_no_quit.
6208 Note that this function may not skip over invisible text that is so
6209 because of text properties and immediately follows a newline. If
6210 it would, function reseat_at_next_visible_line_start, when called
6211 from set_iterator_to_next, would effectively make invisible
6212 characters following a newline part of the wrong glyph row, which
6213 leads to wrong cursor motion. */
6215 static bool
6216 forward_to_next_line_start (struct it *it, bool *skipped_p,
6217 struct bidi_it *bidi_it_prev)
6219 ptrdiff_t old_selective;
6220 bool newline_found_p = false;
6221 int n;
6222 const int MAX_NEWLINE_DISTANCE = 500;
6224 /* If already on a newline, just consume it to avoid unintended
6225 skipping over invisible text below. */
6226 if (it->what == IT_CHARACTER
6227 && it->c == '\n'
6228 && CHARPOS (it->position) == IT_CHARPOS (*it))
6230 if (it->bidi_p && bidi_it_prev)
6231 *bidi_it_prev = it->bidi_it;
6232 set_iterator_to_next (it, false);
6233 it->c = 0;
6234 return true;
6237 /* Don't handle selective display in the following. It's (a)
6238 unnecessary because it's done by the caller, and (b) leads to an
6239 infinite recursion because next_element_from_ellipsis indirectly
6240 calls this function. */
6241 old_selective = it->selective;
6242 it->selective = 0;
6244 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6245 from buffer text. */
6246 for (n = 0;
6247 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6248 n += !STRINGP (it->string))
6250 if (!get_next_display_element (it))
6251 return false;
6252 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6253 if (newline_found_p && it->bidi_p && bidi_it_prev)
6254 *bidi_it_prev = it->bidi_it;
6255 set_iterator_to_next (it, false);
6258 /* If we didn't find a newline near enough, see if we can use a
6259 short-cut. */
6260 if (!newline_found_p)
6262 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6263 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6264 1, &bytepos);
6265 Lisp_Object pos;
6267 eassert (!STRINGP (it->string));
6269 /* If there isn't any `display' property in sight, and no
6270 overlays, we can just use the position of the newline in
6271 buffer text. */
6272 if (it->stop_charpos >= limit
6273 || ((pos = Fnext_single_property_change (make_number (start),
6274 Qdisplay, Qnil,
6275 make_number (limit)),
6276 NILP (pos))
6277 && next_overlay_change (start) == ZV))
6279 if (!it->bidi_p)
6281 IT_CHARPOS (*it) = limit;
6282 IT_BYTEPOS (*it) = bytepos;
6284 else
6286 struct bidi_it bprev;
6288 /* Help bidi.c avoid expensive searches for display
6289 properties and overlays, by telling it that there are
6290 none up to `limit'. */
6291 if (it->bidi_it.disp_pos < limit)
6293 it->bidi_it.disp_pos = limit;
6294 it->bidi_it.disp_prop = 0;
6296 do {
6297 bprev = it->bidi_it;
6298 bidi_move_to_visually_next (&it->bidi_it);
6299 } while (it->bidi_it.charpos != limit);
6300 IT_CHARPOS (*it) = limit;
6301 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6302 if (bidi_it_prev)
6303 *bidi_it_prev = bprev;
6305 *skipped_p = newline_found_p = true;
6307 else
6309 while (!newline_found_p)
6311 if (!get_next_display_element (it))
6312 break;
6313 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6314 if (newline_found_p && it->bidi_p && bidi_it_prev)
6315 *bidi_it_prev = it->bidi_it;
6316 set_iterator_to_next (it, false);
6321 it->selective = old_selective;
6322 return newline_found_p;
6326 /* Set IT's current position to the previous visible line start. Skip
6327 invisible text that is so either due to text properties or due to
6328 selective display. Caution: this does not change IT->current_x and
6329 IT->hpos. */
6331 static void
6332 back_to_previous_visible_line_start (struct it *it)
6334 while (IT_CHARPOS (*it) > BEGV)
6336 back_to_previous_line_start (it);
6338 if (IT_CHARPOS (*it) <= BEGV)
6339 break;
6341 /* If selective > 0, then lines indented more than its value are
6342 invisible. */
6343 if (it->selective > 0
6344 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6345 it->selective))
6346 continue;
6348 /* Check the newline before point for invisibility. */
6350 Lisp_Object prop;
6351 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6352 Qinvisible, it->window);
6353 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6354 continue;
6357 if (IT_CHARPOS (*it) <= BEGV)
6358 break;
6361 struct it it2;
6362 void *it2data = NULL;
6363 ptrdiff_t pos;
6364 ptrdiff_t beg, end;
6365 Lisp_Object val, overlay;
6367 SAVE_IT (it2, *it, it2data);
6369 /* If newline is part of a composition, continue from start of composition */
6370 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6371 && beg < IT_CHARPOS (*it))
6372 goto replaced;
6374 /* If newline is replaced by a display property, find start of overlay
6375 or interval and continue search from that point. */
6376 pos = --IT_CHARPOS (it2);
6377 --IT_BYTEPOS (it2);
6378 it2.sp = 0;
6379 bidi_unshelve_cache (NULL, false);
6380 it2.string_from_display_prop_p = false;
6381 it2.from_disp_prop_p = false;
6382 if (handle_display_prop (&it2) == HANDLED_RETURN
6383 && !NILP (val = get_char_property_and_overlay
6384 (make_number (pos), Qdisplay, Qnil, &overlay))
6385 && (OVERLAYP (overlay)
6386 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6387 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6389 RESTORE_IT (it, it, it2data);
6390 goto replaced;
6393 /* Newline is not replaced by anything -- so we are done. */
6394 RESTORE_IT (it, it, it2data);
6395 break;
6397 replaced:
6398 if (beg < BEGV)
6399 beg = BEGV;
6400 IT_CHARPOS (*it) = beg;
6401 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6405 it->continuation_lines_width = 0;
6407 eassert (IT_CHARPOS (*it) >= BEGV);
6408 eassert (IT_CHARPOS (*it) == BEGV
6409 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6410 CHECK_IT (it);
6414 /* Reseat iterator IT at the previous visible line start. Skip
6415 invisible text that is so either due to text properties or due to
6416 selective display. At the end, update IT's overlay information,
6417 face information etc. */
6419 void
6420 reseat_at_previous_visible_line_start (struct it *it)
6422 back_to_previous_visible_line_start (it);
6423 reseat (it, it->current.pos, true);
6424 CHECK_IT (it);
6428 /* Reseat iterator IT on the next visible line start in the current
6429 buffer. ON_NEWLINE_P means position IT on the newline
6430 preceding the line start. Skip over invisible text that is so
6431 because of selective display. Compute faces, overlays etc at the
6432 new position. Note that this function does not skip over text that
6433 is invisible because of text properties. */
6435 static void
6436 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6438 bool skipped_p = false;
6439 struct bidi_it bidi_it_prev;
6440 bool newline_found_p
6441 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6443 /* Skip over lines that are invisible because they are indented
6444 more than the value of IT->selective. */
6445 if (it->selective > 0)
6446 while (IT_CHARPOS (*it) < ZV
6447 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6448 it->selective))
6450 eassert (IT_BYTEPOS (*it) == BEGV
6451 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6452 newline_found_p =
6453 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6456 /* Position on the newline if that's what's requested. */
6457 if (on_newline_p && newline_found_p)
6459 if (STRINGP (it->string))
6461 if (IT_STRING_CHARPOS (*it) > 0)
6463 if (!it->bidi_p)
6465 --IT_STRING_CHARPOS (*it);
6466 --IT_STRING_BYTEPOS (*it);
6468 else
6470 /* We need to restore the bidi iterator to the state
6471 it had on the newline, and resync the IT's
6472 position with that. */
6473 it->bidi_it = bidi_it_prev;
6474 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6475 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6479 else if (IT_CHARPOS (*it) > BEGV)
6481 if (!it->bidi_p)
6483 --IT_CHARPOS (*it);
6484 --IT_BYTEPOS (*it);
6486 else
6488 /* We need to restore the bidi iterator to the state it
6489 had on the newline and resync IT with that. */
6490 it->bidi_it = bidi_it_prev;
6491 IT_CHARPOS (*it) = it->bidi_it.charpos;
6492 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6494 reseat (it, it->current.pos, false);
6497 else if (skipped_p)
6498 reseat (it, it->current.pos, false);
6500 CHECK_IT (it);
6505 /***********************************************************************
6506 Changing an iterator's position
6507 ***********************************************************************/
6509 /* Change IT's current position to POS in current_buffer.
6510 If FORCE_P, always check for text properties at the new position.
6511 Otherwise, text properties are only looked up if POS >=
6512 IT->check_charpos of a property. */
6514 static void
6515 reseat (struct it *it, struct text_pos pos, bool force_p)
6517 ptrdiff_t original_pos = IT_CHARPOS (*it);
6519 reseat_1 (it, pos, false);
6521 /* Determine where to check text properties. Avoid doing it
6522 where possible because text property lookup is very expensive. */
6523 if (force_p
6524 || CHARPOS (pos) > it->stop_charpos
6525 || CHARPOS (pos) < original_pos)
6527 if (it->bidi_p)
6529 /* For bidi iteration, we need to prime prev_stop and
6530 base_level_stop with our best estimations. */
6531 /* Implementation note: Of course, POS is not necessarily a
6532 stop position, so assigning prev_pos to it is a lie; we
6533 should have called compute_stop_backwards. However, if
6534 the current buffer does not include any R2L characters,
6535 that call would be a waste of cycles, because the
6536 iterator will never move back, and thus never cross this
6537 "fake" stop position. So we delay that backward search
6538 until the time we really need it, in next_element_from_buffer. */
6539 if (CHARPOS (pos) != it->prev_stop)
6540 it->prev_stop = CHARPOS (pos);
6541 if (CHARPOS (pos) < it->base_level_stop)
6542 it->base_level_stop = 0; /* meaning it's unknown */
6543 handle_stop (it);
6545 else
6547 handle_stop (it);
6548 it->prev_stop = it->base_level_stop = 0;
6553 CHECK_IT (it);
6557 /* Change IT's buffer position to POS. SET_STOP_P means set
6558 IT->stop_pos to POS, also. */
6560 static void
6561 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6563 /* Don't call this function when scanning a C string. */
6564 eassert (it->s == NULL);
6566 /* POS must be a reasonable value. */
6567 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6569 it->current.pos = it->position = pos;
6570 it->end_charpos = ZV;
6571 it->dpvec = NULL;
6572 it->current.dpvec_index = -1;
6573 it->current.overlay_string_index = -1;
6574 IT_STRING_CHARPOS (*it) = -1;
6575 IT_STRING_BYTEPOS (*it) = -1;
6576 it->string = Qnil;
6577 it->method = GET_FROM_BUFFER;
6578 it->object = it->w->contents;
6579 it->area = TEXT_AREA;
6580 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6581 it->sp = 0;
6582 it->string_from_display_prop_p = false;
6583 it->string_from_prefix_prop_p = false;
6585 it->from_disp_prop_p = false;
6586 it->face_before_selective_p = false;
6587 if (it->bidi_p)
6589 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6590 &it->bidi_it);
6591 bidi_unshelve_cache (NULL, false);
6592 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6593 it->bidi_it.string.s = NULL;
6594 it->bidi_it.string.lstring = Qnil;
6595 it->bidi_it.string.bufpos = 0;
6596 it->bidi_it.string.from_disp_str = false;
6597 it->bidi_it.string.unibyte = false;
6598 it->bidi_it.w = it->w;
6601 if (set_stop_p)
6603 it->stop_charpos = CHARPOS (pos);
6604 it->base_level_stop = CHARPOS (pos);
6606 /* This make the information stored in it->cmp_it invalidate. */
6607 it->cmp_it.id = -1;
6611 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6612 If S is non-null, it is a C string to iterate over. Otherwise,
6613 STRING gives a Lisp string to iterate over.
6615 If PRECISION > 0, don't return more then PRECISION number of
6616 characters from the string.
6618 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6619 characters have been returned. FIELD_WIDTH < 0 means an infinite
6620 field width.
6622 MULTIBYTE = 0 means disable processing of multibyte characters,
6623 MULTIBYTE > 0 means enable it,
6624 MULTIBYTE < 0 means use IT->multibyte_p.
6626 IT must be initialized via a prior call to init_iterator before
6627 calling this function. */
6629 static void
6630 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6631 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6632 int multibyte)
6634 /* No text property checks performed by default, but see below. */
6635 it->stop_charpos = -1;
6637 /* Set iterator position and end position. */
6638 memset (&it->current, 0, sizeof it->current);
6639 it->current.overlay_string_index = -1;
6640 it->current.dpvec_index = -1;
6641 eassert (charpos >= 0);
6643 /* If STRING is specified, use its multibyteness, otherwise use the
6644 setting of MULTIBYTE, if specified. */
6645 if (multibyte >= 0)
6646 it->multibyte_p = multibyte > 0;
6648 /* Bidirectional reordering of strings is controlled by the default
6649 value of bidi-display-reordering. Don't try to reorder while
6650 loading loadup.el, as the necessary character property tables are
6651 not yet available. */
6652 it->bidi_p =
6653 !redisplay__inhibit_bidi
6654 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6656 if (s == NULL)
6658 eassert (STRINGP (string));
6659 it->string = string;
6660 it->s = NULL;
6661 it->end_charpos = it->string_nchars = SCHARS (string);
6662 it->method = GET_FROM_STRING;
6663 it->current.string_pos = string_pos (charpos, string);
6665 if (it->bidi_p)
6667 it->bidi_it.string.lstring = string;
6668 it->bidi_it.string.s = NULL;
6669 it->bidi_it.string.schars = it->end_charpos;
6670 it->bidi_it.string.bufpos = 0;
6671 it->bidi_it.string.from_disp_str = false;
6672 it->bidi_it.string.unibyte = !it->multibyte_p;
6673 it->bidi_it.w = it->w;
6674 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6675 FRAME_WINDOW_P (it->f), &it->bidi_it);
6678 else
6680 it->s = (const unsigned char *) s;
6681 it->string = Qnil;
6683 /* Note that we use IT->current.pos, not it->current.string_pos,
6684 for displaying C strings. */
6685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6686 if (it->multibyte_p)
6688 it->current.pos = c_string_pos (charpos, s, true);
6689 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6691 else
6693 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6694 it->end_charpos = it->string_nchars = strlen (s);
6697 if (it->bidi_p)
6699 it->bidi_it.string.lstring = Qnil;
6700 it->bidi_it.string.s = (const unsigned char *) s;
6701 it->bidi_it.string.schars = it->end_charpos;
6702 it->bidi_it.string.bufpos = 0;
6703 it->bidi_it.string.from_disp_str = false;
6704 it->bidi_it.string.unibyte = !it->multibyte_p;
6705 it->bidi_it.w = it->w;
6706 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6707 &it->bidi_it);
6709 it->method = GET_FROM_C_STRING;
6712 /* PRECISION > 0 means don't return more than PRECISION characters
6713 from the string. */
6714 if (precision > 0 && it->end_charpos - charpos > precision)
6716 it->end_charpos = it->string_nchars = charpos + precision;
6717 if (it->bidi_p)
6718 it->bidi_it.string.schars = it->end_charpos;
6721 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6722 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6723 FIELD_WIDTH < 0 means infinite field width. This is useful for
6724 padding with `-' at the end of a mode line. */
6725 if (field_width < 0)
6726 field_width = INFINITY;
6727 /* Implementation note: We deliberately don't enlarge
6728 it->bidi_it.string.schars here to fit it->end_charpos, because
6729 the bidi iterator cannot produce characters out of thin air. */
6730 if (field_width > it->end_charpos - charpos)
6731 it->end_charpos = charpos + field_width;
6733 /* Use the standard display table for displaying strings. */
6734 if (DISP_TABLE_P (Vstandard_display_table))
6735 it->dp = XCHAR_TABLE (Vstandard_display_table);
6737 it->stop_charpos = charpos;
6738 it->prev_stop = charpos;
6739 it->base_level_stop = 0;
6740 if (it->bidi_p)
6742 it->bidi_it.first_elt = true;
6743 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6744 it->bidi_it.disp_pos = -1;
6746 if (s == NULL && it->multibyte_p)
6748 ptrdiff_t endpos = SCHARS (it->string);
6749 if (endpos > it->end_charpos)
6750 endpos = it->end_charpos;
6751 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6752 it->string);
6754 CHECK_IT (it);
6759 /***********************************************************************
6760 Iteration
6761 ***********************************************************************/
6763 /* Map enum it_method value to corresponding next_element_from_* function. */
6765 typedef bool (*next_element_function) (struct it *);
6767 static next_element_function const get_next_element[NUM_IT_METHODS] =
6769 next_element_from_buffer,
6770 next_element_from_display_vector,
6771 next_element_from_string,
6772 next_element_from_c_string,
6773 next_element_from_image,
6774 next_element_from_stretch,
6775 next_element_from_xwidget,
6778 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6781 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6782 (possibly with the following characters). */
6784 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6785 ((IT)->cmp_it.id >= 0 \
6786 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6787 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6788 END_CHARPOS, (IT)->w, \
6789 FACE_FROM_ID_OR_NULL ((IT)->f, \
6790 (IT)->face_id), \
6791 (IT)->string)))
6794 /* Lookup the char-table Vglyphless_char_display for character C (-1
6795 if we want information for no-font case), and return the display
6796 method symbol. By side-effect, update it->what and
6797 it->glyphless_method. This function is called from
6798 get_next_display_element for each character element, and from
6799 x_produce_glyphs when no suitable font was found. */
6801 Lisp_Object
6802 lookup_glyphless_char_display (int c, struct it *it)
6804 Lisp_Object glyphless_method = Qnil;
6806 if (CHAR_TABLE_P (Vglyphless_char_display)
6807 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6809 if (c >= 0)
6811 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6812 if (CONSP (glyphless_method))
6813 glyphless_method = FRAME_WINDOW_P (it->f)
6814 ? XCAR (glyphless_method)
6815 : XCDR (glyphless_method);
6817 else
6818 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6821 retry:
6822 if (NILP (glyphless_method))
6824 if (c >= 0)
6825 /* The default is to display the character by a proper font. */
6826 return Qnil;
6827 /* The default for the no-font case is to display an empty box. */
6828 glyphless_method = Qempty_box;
6830 if (EQ (glyphless_method, Qzero_width))
6832 if (c >= 0)
6833 return glyphless_method;
6834 /* This method can't be used for the no-font case. */
6835 glyphless_method = Qempty_box;
6837 if (EQ (glyphless_method, Qthin_space))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6839 else if (EQ (glyphless_method, Qempty_box))
6840 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6841 else if (EQ (glyphless_method, Qhex_code))
6842 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6843 else if (STRINGP (glyphless_method))
6844 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6845 else
6847 /* Invalid value. We use the default method. */
6848 glyphless_method = Qnil;
6849 goto retry;
6851 it->what = IT_GLYPHLESS;
6852 return glyphless_method;
6855 /* Merge escape glyph face and cache the result. */
6857 static struct frame *last_escape_glyph_frame = NULL;
6858 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6859 static int last_escape_glyph_merged_face_id = 0;
6861 static int
6862 merge_escape_glyph_face (struct it *it)
6864 int face_id;
6866 if (it->f == last_escape_glyph_frame
6867 && it->face_id == last_escape_glyph_face_id)
6868 face_id = last_escape_glyph_merged_face_id;
6869 else
6871 /* Merge the `escape-glyph' face into the current face. */
6872 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6873 last_escape_glyph_frame = it->f;
6874 last_escape_glyph_face_id = it->face_id;
6875 last_escape_glyph_merged_face_id = face_id;
6877 return face_id;
6880 /* Likewise for glyphless glyph face. */
6882 static struct frame *last_glyphless_glyph_frame = NULL;
6883 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6884 static int last_glyphless_glyph_merged_face_id = 0;
6887 merge_glyphless_glyph_face (struct it *it)
6889 int face_id;
6891 if (it->f == last_glyphless_glyph_frame
6892 && it->face_id == last_glyphless_glyph_face_id)
6893 face_id = last_glyphless_glyph_merged_face_id;
6894 else
6896 /* Merge the `glyphless-char' face into the current face. */
6897 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6898 last_glyphless_glyph_frame = it->f;
6899 last_glyphless_glyph_face_id = it->face_id;
6900 last_glyphless_glyph_merged_face_id = face_id;
6902 return face_id;
6905 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6906 be called before redisplaying windows, and when the frame's face
6907 cache is freed. */
6908 void
6909 forget_escape_and_glyphless_faces (void)
6911 last_escape_glyph_frame = NULL;
6912 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6913 last_glyphless_glyph_frame = NULL;
6914 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6917 /* Load IT's display element fields with information about the next
6918 display element from the current position of IT. Value is false if
6919 end of buffer (or C string) is reached. */
6921 static bool
6922 get_next_display_element (struct it *it)
6924 /* True means that we found a display element. False means that
6925 we hit the end of what we iterate over. Performance note: the
6926 function pointer `method' used here turns out to be faster than
6927 using a sequence of if-statements. */
6928 bool success_p;
6930 get_next:
6931 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6933 if (it->what == IT_CHARACTER)
6935 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6936 and only if (a) the resolved directionality of that character
6937 is R..." */
6938 /* FIXME: Do we need an exception for characters from display
6939 tables? */
6940 if (it->bidi_p && it->bidi_it.type == STRONG_R
6941 && !inhibit_bidi_mirroring)
6942 it->c = bidi_mirror_char (it->c);
6943 /* Map via display table or translate control characters.
6944 IT->c, IT->len etc. have been set to the next character by
6945 the function call above. If we have a display table, and it
6946 contains an entry for IT->c, translate it. Don't do this if
6947 IT->c itself comes from a display table, otherwise we could
6948 end up in an infinite recursion. (An alternative could be to
6949 count the recursion depth of this function and signal an
6950 error when a certain maximum depth is reached.) Is it worth
6951 it? */
6952 if (success_p && it->dpvec == NULL)
6954 Lisp_Object dv;
6955 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6956 bool nonascii_space_p = false;
6957 bool nonascii_hyphen_p = false;
6958 int c = it->c; /* This is the character to display. */
6960 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6962 eassert (SINGLE_BYTE_CHAR_P (c));
6963 if (unibyte_display_via_language_environment)
6965 c = DECODE_CHAR (unibyte, c);
6966 if (c < 0)
6967 c = BYTE8_TO_CHAR (it->c);
6969 else
6970 c = BYTE8_TO_CHAR (it->c);
6973 if (it->dp
6974 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6975 VECTORP (dv)))
6977 struct Lisp_Vector *v = XVECTOR (dv);
6979 /* Return the first character from the display table
6980 entry, if not empty. If empty, don't display the
6981 current character. */
6982 if (v->header.size)
6984 it->dpvec_char_len = it->len;
6985 it->dpvec = v->contents;
6986 it->dpend = v->contents + v->header.size;
6987 it->current.dpvec_index = 0;
6988 it->dpvec_face_id = -1;
6989 it->saved_face_id = it->face_id;
6990 it->method = GET_FROM_DISPLAY_VECTOR;
6991 it->ellipsis_p = false;
6993 else
6995 set_iterator_to_next (it, false);
6997 goto get_next;
7000 if (! NILP (lookup_glyphless_char_display (c, it)))
7002 if (it->what == IT_GLYPHLESS)
7003 goto done;
7004 /* Don't display this character. */
7005 set_iterator_to_next (it, false);
7006 goto get_next;
7009 /* If `nobreak-char-display' is non-nil, we display
7010 non-ASCII spaces and hyphens specially. */
7011 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7013 if (c == NO_BREAK_SPACE)
7014 nonascii_space_p = true;
7015 else if (c == SOFT_HYPHEN || c == HYPHEN
7016 || c == NON_BREAKING_HYPHEN)
7017 nonascii_hyphen_p = true;
7020 /* Translate control characters into `\003' or `^C' form.
7021 Control characters coming from a display table entry are
7022 currently not translated because we use IT->dpvec to hold
7023 the translation. This could easily be changed but I
7024 don't believe that it is worth doing.
7026 The characters handled by `nobreak-char-display' must be
7027 translated too.
7029 Non-printable characters and raw-byte characters are also
7030 translated to octal form. */
7031 if (((c < ' ' || c == 127) /* ASCII control chars. */
7032 ? (it->area != TEXT_AREA
7033 /* In mode line, treat \n, \t like other crl chars. */
7034 || (c != '\t'
7035 && it->glyph_row
7036 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7037 || (c != '\n' && c != '\t'))
7038 : (nonascii_space_p
7039 || nonascii_hyphen_p
7040 || CHAR_BYTE8_P (c)
7041 || ! CHAR_PRINTABLE_P (c))))
7043 /* C is a control character, non-ASCII space/hyphen,
7044 raw-byte, or a non-printable character which must be
7045 displayed either as '\003' or as `^C' where the '\\'
7046 and '^' can be defined in the display table. Fill
7047 IT->ctl_chars with glyphs for what we have to
7048 display. Then, set IT->dpvec to these glyphs. */
7049 Lisp_Object gc;
7050 int ctl_len;
7051 int face_id;
7052 int lface_id = 0;
7053 int escape_glyph;
7055 /* Handle control characters with ^. */
7057 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7059 int g;
7061 g = '^'; /* default glyph for Control */
7062 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7063 if (it->dp
7064 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7066 g = GLYPH_CODE_CHAR (gc);
7067 lface_id = GLYPH_CODE_FACE (gc);
7070 face_id = (lface_id
7071 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7072 : merge_escape_glyph_face (it));
7074 XSETINT (it->ctl_chars[0], g);
7075 XSETINT (it->ctl_chars[1], c ^ 0100);
7076 ctl_len = 2;
7077 goto display_control;
7080 /* Handle non-ascii space in the mode where it only gets
7081 highlighting. */
7083 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7085 /* Merge `nobreak-space' into the current face. */
7086 face_id = merge_faces (it->f, Qnobreak_space, 0,
7087 it->face_id);
7088 XSETINT (it->ctl_chars[0], ' ');
7089 ctl_len = 1;
7090 goto display_control;
7093 /* Handle non-ascii hyphens in the mode where it only
7094 gets highlighting. */
7096 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7098 /* Merge `nobreak-space' into the current face. */
7099 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7100 it->face_id);
7101 XSETINT (it->ctl_chars[0], '-');
7102 ctl_len = 1;
7103 goto display_control;
7106 /* Handle sequences that start with the "escape glyph". */
7108 /* the default escape glyph is \. */
7109 escape_glyph = '\\';
7111 if (it->dp
7112 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7114 escape_glyph = GLYPH_CODE_CHAR (gc);
7115 lface_id = GLYPH_CODE_FACE (gc);
7118 face_id = (lface_id
7119 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7120 : merge_escape_glyph_face (it));
7122 /* Draw non-ASCII space/hyphen with escape glyph: */
7124 if (nonascii_space_p || nonascii_hyphen_p)
7126 XSETINT (it->ctl_chars[0], escape_glyph);
7127 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7128 ctl_len = 2;
7129 goto display_control;
7133 char str[10];
7134 int len, i;
7136 if (CHAR_BYTE8_P (c))
7137 /* Display \200 instead of \17777600. */
7138 c = CHAR_TO_BYTE8 (c);
7139 len = sprintf (str, "%03o", c + 0u);
7141 XSETINT (it->ctl_chars[0], escape_glyph);
7142 for (i = 0; i < len; i++)
7143 XSETINT (it->ctl_chars[i + 1], str[i]);
7144 ctl_len = len + 1;
7147 display_control:
7148 /* Set up IT->dpvec and return first character from it. */
7149 it->dpvec_char_len = it->len;
7150 it->dpvec = it->ctl_chars;
7151 it->dpend = it->dpvec + ctl_len;
7152 it->current.dpvec_index = 0;
7153 it->dpvec_face_id = face_id;
7154 it->saved_face_id = it->face_id;
7155 it->method = GET_FROM_DISPLAY_VECTOR;
7156 it->ellipsis_p = false;
7157 goto get_next;
7159 it->char_to_display = c;
7161 else if (success_p)
7163 it->char_to_display = it->c;
7167 #ifdef HAVE_WINDOW_SYSTEM
7168 /* Adjust face id for a multibyte character. There are no multibyte
7169 character in unibyte text. */
7170 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7171 && it->multibyte_p
7172 && success_p
7173 && FRAME_WINDOW_P (it->f))
7175 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7177 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7179 /* Automatic composition with glyph-string. */
7180 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7182 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7184 else
7186 ptrdiff_t pos = (it->s ? -1
7187 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7188 : IT_CHARPOS (*it));
7189 int c;
7191 if (it->what == IT_CHARACTER)
7192 c = it->char_to_display;
7193 else
7195 struct composition *cmp = composition_table[it->cmp_it.id];
7196 int i;
7198 c = ' ';
7199 for (i = 0; i < cmp->glyph_len; i++)
7200 /* TAB in a composition means display glyphs with
7201 padding space on the left or right. */
7202 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7203 break;
7205 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7208 #endif /* HAVE_WINDOW_SYSTEM */
7210 done:
7211 /* Is this character the last one of a run of characters with
7212 box? If yes, set IT->end_of_box_run_p to true. */
7213 if (it->face_box_p
7214 && it->s == NULL)
7216 if (it->method == GET_FROM_STRING && it->sp)
7218 int face_id = underlying_face_id (it);
7219 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7221 if (face)
7223 if (face->box == FACE_NO_BOX)
7225 /* If the box comes from face properties in a
7226 display string, check faces in that string. */
7227 int string_face_id = face_after_it_pos (it);
7228 it->end_of_box_run_p
7229 = (FACE_FROM_ID (it->f, string_face_id)->box
7230 == FACE_NO_BOX);
7232 /* Otherwise, the box comes from the underlying face.
7233 If this is the last string character displayed, check
7234 the next buffer location. */
7235 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7236 /* n_overlay_strings is unreliable unless
7237 overlay_string_index is non-negative. */
7238 && ((it->current.overlay_string_index >= 0
7239 && (it->current.overlay_string_index
7240 == it->n_overlay_strings - 1))
7241 /* A string from display property. */
7242 || it->from_disp_prop_p))
7244 ptrdiff_t ignore;
7245 int next_face_id;
7246 bool text_from_string = false;
7247 /* Normally, the next buffer location is stored in
7248 IT->current.pos... */
7249 struct text_pos pos = it->current.pos;
7251 /* ...but for a string from a display property, the
7252 next buffer position is stored in the 'position'
7253 member of the iteration stack slot below the
7254 current one, see handle_single_display_spec. By
7255 contrast, it->current.pos was not yet updated to
7256 point to that buffer position; that will happen
7257 in pop_it, after we finish displaying the current
7258 string. Note that we already checked above that
7259 it->sp is positive, so subtracting one from it is
7260 safe. */
7261 if (it->from_disp_prop_p)
7263 int stackp = it->sp - 1;
7265 /* Find the stack level with data from buffer. */
7266 while (stackp >= 0
7267 && STRINGP ((it->stack + stackp)->string))
7268 stackp--;
7269 if (stackp < 0)
7271 /* If no stack slot was found for iterating
7272 a buffer, we are displaying text from a
7273 string, most probably the mode line or
7274 the header line, and that string has a
7275 display string on some of its
7276 characters. */
7277 text_from_string = true;
7278 pos = it->stack[it->sp - 1].position;
7280 else
7281 pos = (it->stack + stackp)->position;
7283 else
7284 INC_TEXT_POS (pos, it->multibyte_p);
7286 if (text_from_string)
7288 Lisp_Object base_string = it->stack[it->sp - 1].string;
7290 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7291 it->end_of_box_run_p = true;
7292 else
7294 next_face_id
7295 = face_at_string_position (it->w, base_string,
7296 CHARPOS (pos), 0,
7297 &ignore, face_id, false);
7298 it->end_of_box_run_p
7299 = (FACE_FROM_ID (it->f, next_face_id)->box
7300 == FACE_NO_BOX);
7303 else if (CHARPOS (pos) >= ZV)
7304 it->end_of_box_run_p = true;
7305 else
7307 next_face_id =
7308 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7309 CHARPOS (pos)
7310 + TEXT_PROP_DISTANCE_LIMIT,
7311 false, -1);
7312 it->end_of_box_run_p
7313 = (FACE_FROM_ID (it->f, next_face_id)->box
7314 == FACE_NO_BOX);
7319 /* next_element_from_display_vector sets this flag according to
7320 faces of the display vector glyphs, see there. */
7321 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7323 int face_id = face_after_it_pos (it);
7324 it->end_of_box_run_p
7325 = (face_id != it->face_id
7326 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7329 /* If we reached the end of the object we've been iterating (e.g., a
7330 display string or an overlay string), and there's something on
7331 IT->stack, proceed with what's on the stack. It doesn't make
7332 sense to return false if there's unprocessed stuff on the stack,
7333 because otherwise that stuff will never be displayed. */
7334 if (!success_p && it->sp > 0)
7336 set_iterator_to_next (it, false);
7337 success_p = get_next_display_element (it);
7340 /* Value is false if end of buffer or string reached. */
7341 return success_p;
7345 /* Move IT to the next display element.
7347 RESEAT_P means if called on a newline in buffer text,
7348 skip to the next visible line start.
7350 Functions get_next_display_element and set_iterator_to_next are
7351 separate because I find this arrangement easier to handle than a
7352 get_next_display_element function that also increments IT's
7353 position. The way it is we can first look at an iterator's current
7354 display element, decide whether it fits on a line, and if it does,
7355 increment the iterator position. The other way around we probably
7356 would either need a flag indicating whether the iterator has to be
7357 incremented the next time, or we would have to implement a
7358 decrement position function which would not be easy to write. */
7360 void
7361 set_iterator_to_next (struct it *it, bool reseat_p)
7363 /* Reset flags indicating start and end of a sequence of characters
7364 with box. Reset them at the start of this function because
7365 moving the iterator to a new position might set them. */
7366 it->start_of_box_run_p = it->end_of_box_run_p = false;
7368 switch (it->method)
7370 case GET_FROM_BUFFER:
7371 /* The current display element of IT is a character from
7372 current_buffer. Advance in the buffer, and maybe skip over
7373 invisible lines that are so because of selective display. */
7374 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7375 reseat_at_next_visible_line_start (it, false);
7376 else if (it->cmp_it.id >= 0)
7378 /* We are currently getting glyphs from a composition. */
7379 if (! it->bidi_p)
7381 IT_CHARPOS (*it) += it->cmp_it.nchars;
7382 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7384 else
7386 int i;
7388 /* Update IT's char/byte positions to point to the first
7389 character of the next grapheme cluster, or to the
7390 character visually after the current composition. */
7391 for (i = 0; i < it->cmp_it.nchars; i++)
7392 bidi_move_to_visually_next (&it->bidi_it);
7393 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7394 IT_CHARPOS (*it) = it->bidi_it.charpos;
7397 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7398 && it->cmp_it.to < it->cmp_it.nglyphs)
7400 /* Composition created while scanning forward. Proceed
7401 to the next grapheme cluster. */
7402 it->cmp_it.from = it->cmp_it.to;
7404 else if ((it->bidi_p && it->cmp_it.reversed_p)
7405 && it->cmp_it.from > 0)
7407 /* Composition created while scanning backward. Proceed
7408 to the previous grapheme cluster. */
7409 it->cmp_it.to = it->cmp_it.from;
7411 else
7413 /* No more grapheme clusters in this composition.
7414 Find the next stop position. */
7415 ptrdiff_t stop = it->end_charpos;
7417 if (it->bidi_it.scan_dir < 0)
7418 /* Now we are scanning backward and don't know
7419 where to stop. */
7420 stop = -1;
7421 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7422 IT_BYTEPOS (*it), stop, Qnil);
7425 else
7427 eassert (it->len != 0);
7429 if (!it->bidi_p)
7431 IT_BYTEPOS (*it) += it->len;
7432 IT_CHARPOS (*it) += 1;
7434 else
7436 int prev_scan_dir = it->bidi_it.scan_dir;
7437 /* If this is a new paragraph, determine its base
7438 direction (a.k.a. its base embedding level). */
7439 if (it->bidi_it.new_paragraph)
7440 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7441 false);
7442 bidi_move_to_visually_next (&it->bidi_it);
7443 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7444 IT_CHARPOS (*it) = it->bidi_it.charpos;
7445 if (prev_scan_dir != it->bidi_it.scan_dir)
7447 /* As the scan direction was changed, we must
7448 re-compute the stop position for composition. */
7449 ptrdiff_t stop = it->end_charpos;
7450 if (it->bidi_it.scan_dir < 0)
7451 stop = -1;
7452 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7453 IT_BYTEPOS (*it), stop, Qnil);
7456 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7458 break;
7460 case GET_FROM_C_STRING:
7461 /* Current display element of IT is from a C string. */
7462 if (!it->bidi_p
7463 /* If the string position is beyond string's end, it means
7464 next_element_from_c_string is padding the string with
7465 blanks, in which case we bypass the bidi iterator,
7466 because it cannot deal with such virtual characters. */
7467 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7469 IT_BYTEPOS (*it) += it->len;
7470 IT_CHARPOS (*it) += 1;
7472 else
7474 bidi_move_to_visually_next (&it->bidi_it);
7475 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7476 IT_CHARPOS (*it) = it->bidi_it.charpos;
7478 break;
7480 case GET_FROM_DISPLAY_VECTOR:
7481 /* Current display element of IT is from a display table entry.
7482 Advance in the display table definition. Reset it to null if
7483 end reached, and continue with characters from buffers/
7484 strings. */
7485 ++it->current.dpvec_index;
7487 /* Restore face of the iterator to what they were before the
7488 display vector entry (these entries may contain faces). */
7489 it->face_id = it->saved_face_id;
7491 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7493 bool recheck_faces = it->ellipsis_p;
7495 if (it->s)
7496 it->method = GET_FROM_C_STRING;
7497 else if (STRINGP (it->string))
7498 it->method = GET_FROM_STRING;
7499 else
7501 it->method = GET_FROM_BUFFER;
7502 it->object = it->w->contents;
7505 it->dpvec = NULL;
7506 it->current.dpvec_index = -1;
7508 /* Skip over characters which were displayed via IT->dpvec. */
7509 if (it->dpvec_char_len < 0)
7510 reseat_at_next_visible_line_start (it, true);
7511 else if (it->dpvec_char_len > 0)
7513 it->len = it->dpvec_char_len;
7514 set_iterator_to_next (it, reseat_p);
7517 /* Maybe recheck faces after display vector. */
7518 if (recheck_faces)
7520 if (it->method == GET_FROM_STRING)
7521 it->stop_charpos = IT_STRING_CHARPOS (*it);
7522 else
7523 it->stop_charpos = IT_CHARPOS (*it);
7526 break;
7528 case GET_FROM_STRING:
7529 /* Current display element is a character from a Lisp string. */
7530 eassert (it->s == NULL && STRINGP (it->string));
7531 /* Don't advance past string end. These conditions are true
7532 when set_iterator_to_next is called at the end of
7533 get_next_display_element, in which case the Lisp string is
7534 already exhausted, and all we want is pop the iterator
7535 stack. */
7536 if (it->current.overlay_string_index >= 0)
7538 /* This is an overlay string, so there's no padding with
7539 spaces, and the number of characters in the string is
7540 where the string ends. */
7541 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7542 goto consider_string_end;
7544 else
7546 /* Not an overlay string. There could be padding, so test
7547 against it->end_charpos. */
7548 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7549 goto consider_string_end;
7551 if (it->cmp_it.id >= 0)
7553 /* We are delivering display elements from a composition.
7554 Update the string position past the grapheme cluster
7555 we've just processed. */
7556 if (! it->bidi_p)
7558 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7559 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7561 else
7563 int i;
7565 for (i = 0; i < it->cmp_it.nchars; i++)
7566 bidi_move_to_visually_next (&it->bidi_it);
7567 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7568 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7571 /* Did we exhaust all the grapheme clusters of this
7572 composition? */
7573 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7574 && (it->cmp_it.to < it->cmp_it.nglyphs))
7576 /* Not all the grapheme clusters were processed yet;
7577 advance to the next cluster. */
7578 it->cmp_it.from = it->cmp_it.to;
7580 else if ((it->bidi_p && it->cmp_it.reversed_p)
7581 && it->cmp_it.from > 0)
7583 /* Likewise: advance to the next cluster, but going in
7584 the reverse direction. */
7585 it->cmp_it.to = it->cmp_it.from;
7587 else
7589 /* This composition was fully processed; find the next
7590 candidate place for checking for composed
7591 characters. */
7592 /* Always limit string searches to the string length;
7593 any padding spaces are not part of the string, and
7594 there cannot be any compositions in that padding. */
7595 ptrdiff_t stop = SCHARS (it->string);
7597 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7598 stop = -1;
7599 else if (it->end_charpos < stop)
7601 /* Cf. PRECISION in reseat_to_string: we might be
7602 limited in how many of the string characters we
7603 need to deliver. */
7604 stop = it->end_charpos;
7606 composition_compute_stop_pos (&it->cmp_it,
7607 IT_STRING_CHARPOS (*it),
7608 IT_STRING_BYTEPOS (*it), stop,
7609 it->string);
7612 else
7614 if (!it->bidi_p
7615 /* If the string position is beyond string's end, it
7616 means next_element_from_string is padding the string
7617 with blanks, in which case we bypass the bidi
7618 iterator, because it cannot deal with such virtual
7619 characters. */
7620 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7622 IT_STRING_BYTEPOS (*it) += it->len;
7623 IT_STRING_CHARPOS (*it) += 1;
7625 else
7627 int prev_scan_dir = it->bidi_it.scan_dir;
7629 bidi_move_to_visually_next (&it->bidi_it);
7630 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7631 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7632 /* If the scan direction changes, we may need to update
7633 the place where to check for composed characters. */
7634 if (prev_scan_dir != it->bidi_it.scan_dir)
7636 ptrdiff_t stop = SCHARS (it->string);
7638 if (it->bidi_it.scan_dir < 0)
7639 stop = -1;
7640 else if (it->end_charpos < stop)
7641 stop = it->end_charpos;
7643 composition_compute_stop_pos (&it->cmp_it,
7644 IT_STRING_CHARPOS (*it),
7645 IT_STRING_BYTEPOS (*it), stop,
7646 it->string);
7651 consider_string_end:
7653 if (it->current.overlay_string_index >= 0)
7655 /* IT->string is an overlay string. Advance to the
7656 next, if there is one. */
7657 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7659 it->ellipsis_p = false;
7660 next_overlay_string (it);
7661 if (it->ellipsis_p)
7662 setup_for_ellipsis (it, 0);
7665 else
7667 /* IT->string is not an overlay string. If we reached
7668 its end, and there is something on IT->stack, proceed
7669 with what is on the stack. This can be either another
7670 string, this time an overlay string, or a buffer. */
7671 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7672 && it->sp > 0)
7674 pop_it (it);
7675 if (it->method == GET_FROM_STRING)
7676 goto consider_string_end;
7679 break;
7681 case GET_FROM_IMAGE:
7682 case GET_FROM_STRETCH:
7683 case GET_FROM_XWIDGET:
7685 /* The position etc with which we have to proceed are on
7686 the stack. The position may be at the end of a string,
7687 if the `display' property takes up the whole string. */
7688 eassert (it->sp > 0);
7689 pop_it (it);
7690 if (it->method == GET_FROM_STRING)
7691 goto consider_string_end;
7692 break;
7694 default:
7695 /* There are no other methods defined, so this should be a bug. */
7696 emacs_abort ();
7699 eassert (it->method != GET_FROM_STRING
7700 || (STRINGP (it->string)
7701 && IT_STRING_CHARPOS (*it) >= 0));
7704 /* Load IT's display element fields with information about the next
7705 display element which comes from a display table entry or from the
7706 result of translating a control character to one of the forms `^C'
7707 or `\003'.
7709 IT->dpvec holds the glyphs to return as characters.
7710 IT->saved_face_id holds the face id before the display vector--it
7711 is restored into IT->face_id in set_iterator_to_next. */
7713 static bool
7714 next_element_from_display_vector (struct it *it)
7716 Lisp_Object gc;
7717 int prev_face_id = it->face_id;
7718 int next_face_id;
7720 /* Precondition. */
7721 eassert (it->dpvec && it->current.dpvec_index >= 0);
7723 it->face_id = it->saved_face_id;
7725 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7726 That seemed totally bogus - so I changed it... */
7727 gc = it->dpvec[it->current.dpvec_index];
7729 if (GLYPH_CODE_P (gc))
7731 struct face *this_face, *prev_face, *next_face;
7733 it->c = GLYPH_CODE_CHAR (gc);
7734 it->len = CHAR_BYTES (it->c);
7736 /* The entry may contain a face id to use. Such a face id is
7737 the id of a Lisp face, not a realized face. A face id of
7738 zero means no face is specified. */
7739 if (it->dpvec_face_id >= 0)
7740 it->face_id = it->dpvec_face_id;
7741 else
7743 int lface_id = GLYPH_CODE_FACE (gc);
7744 if (lface_id > 0)
7745 it->face_id = merge_faces (it->f, Qt, lface_id,
7746 it->saved_face_id);
7749 /* Glyphs in the display vector could have the box face, so we
7750 need to set the related flags in the iterator, as
7751 appropriate. */
7752 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7753 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7755 /* Is this character the first character of a box-face run? */
7756 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7757 && (!prev_face
7758 || prev_face->box == FACE_NO_BOX));
7760 /* For the last character of the box-face run, we need to look
7761 either at the next glyph from the display vector, or at the
7762 face we saw before the display vector. */
7763 next_face_id = it->saved_face_id;
7764 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7766 if (it->dpvec_face_id >= 0)
7767 next_face_id = it->dpvec_face_id;
7768 else
7770 int lface_id =
7771 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7773 if (lface_id > 0)
7774 next_face_id = merge_faces (it->f, Qt, lface_id,
7775 it->saved_face_id);
7778 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7779 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7780 && (!next_face
7781 || next_face->box == FACE_NO_BOX));
7782 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7784 else
7785 /* Display table entry is invalid. Return a space. */
7786 it->c = ' ', it->len = 1;
7788 /* Don't change position and object of the iterator here. They are
7789 still the values of the character that had this display table
7790 entry or was translated, and that's what we want. */
7791 it->what = IT_CHARACTER;
7792 return true;
7795 /* Get the first element of string/buffer in the visual order, after
7796 being reseated to a new position in a string or a buffer. */
7797 static void
7798 get_visually_first_element (struct it *it)
7800 bool string_p = STRINGP (it->string) || it->s;
7801 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7802 ptrdiff_t bob = (string_p ? 0 : BEGV);
7804 if (STRINGP (it->string))
7806 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7807 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7809 else
7811 it->bidi_it.charpos = IT_CHARPOS (*it);
7812 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7815 if (it->bidi_it.charpos == eob)
7817 /* Nothing to do, but reset the FIRST_ELT flag, like
7818 bidi_paragraph_init does, because we are not going to
7819 call it. */
7820 it->bidi_it.first_elt = false;
7822 else if (it->bidi_it.charpos == bob
7823 || (!string_p
7824 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7825 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7827 /* If we are at the beginning of a line/string, we can produce
7828 the next element right away. */
7829 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7830 bidi_move_to_visually_next (&it->bidi_it);
7832 else
7834 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7836 /* We need to prime the bidi iterator starting at the line's or
7837 string's beginning, before we will be able to produce the
7838 next element. */
7839 if (string_p)
7840 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7841 else
7842 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7843 IT_BYTEPOS (*it), -1,
7844 &it->bidi_it.bytepos);
7845 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7848 /* Now return to buffer/string position where we were asked
7849 to get the next display element, and produce that. */
7850 bidi_move_to_visually_next (&it->bidi_it);
7852 while (it->bidi_it.bytepos != orig_bytepos
7853 && it->bidi_it.charpos < eob);
7856 /* Adjust IT's position information to where we ended up. */
7857 if (STRINGP (it->string))
7859 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7860 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7862 else
7864 IT_CHARPOS (*it) = it->bidi_it.charpos;
7865 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7868 if (STRINGP (it->string) || !it->s)
7870 ptrdiff_t stop, charpos, bytepos;
7872 if (STRINGP (it->string))
7874 eassert (!it->s);
7875 stop = SCHARS (it->string);
7876 if (stop > it->end_charpos)
7877 stop = it->end_charpos;
7878 charpos = IT_STRING_CHARPOS (*it);
7879 bytepos = IT_STRING_BYTEPOS (*it);
7881 else
7883 stop = it->end_charpos;
7884 charpos = IT_CHARPOS (*it);
7885 bytepos = IT_BYTEPOS (*it);
7887 if (it->bidi_it.scan_dir < 0)
7888 stop = -1;
7889 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7890 it->string);
7894 /* Load IT with the next display element from Lisp string IT->string.
7895 IT->current.string_pos is the current position within the string.
7896 If IT->current.overlay_string_index >= 0, the Lisp string is an
7897 overlay string. */
7899 static bool
7900 next_element_from_string (struct it *it)
7902 struct text_pos position;
7904 eassert (STRINGP (it->string));
7905 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7906 eassert (IT_STRING_CHARPOS (*it) >= 0);
7907 position = it->current.string_pos;
7909 /* With bidi reordering, the character to display might not be the
7910 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7911 that we were reseat()ed to a new string, whose paragraph
7912 direction is not known. */
7913 if (it->bidi_p && it->bidi_it.first_elt)
7915 get_visually_first_element (it);
7916 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7919 /* Time to check for invisible text? */
7920 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7922 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7924 if (!(!it->bidi_p
7925 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7926 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7928 /* With bidi non-linear iteration, we could find
7929 ourselves far beyond the last computed stop_charpos,
7930 with several other stop positions in between that we
7931 missed. Scan them all now, in buffer's logical
7932 order, until we find and handle the last stop_charpos
7933 that precedes our current position. */
7934 handle_stop_backwards (it, it->stop_charpos);
7935 return GET_NEXT_DISPLAY_ELEMENT (it);
7937 else
7939 if (it->bidi_p)
7941 /* Take note of the stop position we just moved
7942 across, for when we will move back across it. */
7943 it->prev_stop = it->stop_charpos;
7944 /* If we are at base paragraph embedding level, take
7945 note of the last stop position seen at this
7946 level. */
7947 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7948 it->base_level_stop = it->stop_charpos;
7950 handle_stop (it);
7952 /* Since a handler may have changed IT->method, we must
7953 recurse here. */
7954 return GET_NEXT_DISPLAY_ELEMENT (it);
7957 else if (it->bidi_p
7958 /* If we are before prev_stop, we may have overstepped
7959 on our way backwards a stop_pos, and if so, we need
7960 to handle that stop_pos. */
7961 && IT_STRING_CHARPOS (*it) < it->prev_stop
7962 /* We can sometimes back up for reasons that have nothing
7963 to do with bidi reordering. E.g., compositions. The
7964 code below is only needed when we are above the base
7965 embedding level, so test for that explicitly. */
7966 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7968 /* If we lost track of base_level_stop, we have no better
7969 place for handle_stop_backwards to start from than string
7970 beginning. This happens, e.g., when we were reseated to
7971 the previous screenful of text by vertical-motion. */
7972 if (it->base_level_stop <= 0
7973 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7974 it->base_level_stop = 0;
7975 handle_stop_backwards (it, it->base_level_stop);
7976 return GET_NEXT_DISPLAY_ELEMENT (it);
7980 if (it->current.overlay_string_index >= 0)
7982 /* Get the next character from an overlay string. In overlay
7983 strings, there is no field width or padding with spaces to
7984 do. */
7985 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7987 it->what = IT_EOB;
7988 return false;
7990 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7991 IT_STRING_BYTEPOS (*it),
7992 it->bidi_it.scan_dir < 0
7993 ? -1
7994 : SCHARS (it->string))
7995 && next_element_from_composition (it))
7997 return true;
7999 else if (STRING_MULTIBYTE (it->string))
8001 const unsigned char *s = (SDATA (it->string)
8002 + IT_STRING_BYTEPOS (*it));
8003 it->c = string_char_and_length (s, &it->len);
8005 else
8007 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8008 it->len = 1;
8011 else
8013 /* Get the next character from a Lisp string that is not an
8014 overlay string. Such strings come from the mode line, for
8015 example. We may have to pad with spaces, or truncate the
8016 string. See also next_element_from_c_string. */
8017 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8019 it->what = IT_EOB;
8020 return false;
8022 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8024 /* Pad with spaces. */
8025 it->c = ' ', it->len = 1;
8026 CHARPOS (position) = BYTEPOS (position) = -1;
8028 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8029 IT_STRING_BYTEPOS (*it),
8030 it->bidi_it.scan_dir < 0
8031 ? -1
8032 : it->string_nchars)
8033 && next_element_from_composition (it))
8035 return true;
8037 else if (STRING_MULTIBYTE (it->string))
8039 const unsigned char *s = (SDATA (it->string)
8040 + IT_STRING_BYTEPOS (*it));
8041 it->c = string_char_and_length (s, &it->len);
8043 else
8045 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8046 it->len = 1;
8050 /* Record what we have and where it came from. */
8051 it->what = IT_CHARACTER;
8052 it->object = it->string;
8053 it->position = position;
8054 return true;
8058 /* Load IT with next display element from C string IT->s.
8059 IT->string_nchars is the maximum number of characters to return
8060 from the string. IT->end_charpos may be greater than
8061 IT->string_nchars when this function is called, in which case we
8062 may have to return padding spaces. Value is false if end of string
8063 reached, including padding spaces. */
8065 static bool
8066 next_element_from_c_string (struct it *it)
8068 bool success_p = true;
8070 eassert (it->s);
8071 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8072 it->what = IT_CHARACTER;
8073 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8074 it->object = make_number (0);
8076 /* With bidi reordering, the character to display might not be the
8077 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8078 we were reseated to a new string, whose paragraph direction is
8079 not known. */
8080 if (it->bidi_p && it->bidi_it.first_elt)
8081 get_visually_first_element (it);
8083 /* IT's position can be greater than IT->string_nchars in case a
8084 field width or precision has been specified when the iterator was
8085 initialized. */
8086 if (IT_CHARPOS (*it) >= it->end_charpos)
8088 /* End of the game. */
8089 it->what = IT_EOB;
8090 success_p = false;
8092 else if (IT_CHARPOS (*it) >= it->string_nchars)
8094 /* Pad with spaces. */
8095 it->c = ' ', it->len = 1;
8096 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8098 else if (it->multibyte_p)
8099 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8100 else
8101 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8103 return success_p;
8107 /* Set up IT to return characters from an ellipsis, if appropriate.
8108 The definition of the ellipsis glyphs may come from a display table
8109 entry. This function fills IT with the first glyph from the
8110 ellipsis if an ellipsis is to be displayed. */
8112 static bool
8113 next_element_from_ellipsis (struct it *it)
8115 if (it->selective_display_ellipsis_p)
8116 setup_for_ellipsis (it, it->len);
8117 else
8119 /* The face at the current position may be different from the
8120 face we find after the invisible text. Remember what it
8121 was in IT->saved_face_id, and signal that it's there by
8122 setting face_before_selective_p. */
8123 it->saved_face_id = it->face_id;
8124 it->method = GET_FROM_BUFFER;
8125 it->object = it->w->contents;
8126 reseat_at_next_visible_line_start (it, true);
8127 it->face_before_selective_p = true;
8130 return GET_NEXT_DISPLAY_ELEMENT (it);
8134 /* Deliver an image display element. The iterator IT is already
8135 filled with image information (done in handle_display_prop). Value
8136 is always true. */
8139 static bool
8140 next_element_from_image (struct it *it)
8142 it->what = IT_IMAGE;
8143 return true;
8146 static bool
8147 next_element_from_xwidget (struct it *it)
8149 it->what = IT_XWIDGET;
8150 return true;
8154 /* Fill iterator IT with next display element from a stretch glyph
8155 property. IT->object is the value of the text property. Value is
8156 always true. */
8158 static bool
8159 next_element_from_stretch (struct it *it)
8161 it->what = IT_STRETCH;
8162 return true;
8165 /* Scan backwards from IT's current position until we find a stop
8166 position, or until BEGV. This is called when we find ourself
8167 before both the last known prev_stop and base_level_stop while
8168 reordering bidirectional text. */
8170 static void
8171 compute_stop_pos_backwards (struct it *it)
8173 const int SCAN_BACK_LIMIT = 1000;
8174 struct text_pos pos;
8175 struct display_pos save_current = it->current;
8176 struct text_pos save_position = it->position;
8177 ptrdiff_t charpos = IT_CHARPOS (*it);
8178 ptrdiff_t where_we_are = charpos;
8179 ptrdiff_t save_stop_pos = it->stop_charpos;
8180 ptrdiff_t save_end_pos = it->end_charpos;
8182 eassert (NILP (it->string) && !it->s);
8183 eassert (it->bidi_p);
8184 it->bidi_p = false;
8187 it->end_charpos = min (charpos + 1, ZV);
8188 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8189 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8190 reseat_1 (it, pos, false);
8191 compute_stop_pos (it);
8192 /* We must advance forward, right? */
8193 if (it->stop_charpos <= charpos)
8194 emacs_abort ();
8196 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8198 if (it->stop_charpos <= where_we_are)
8199 it->prev_stop = it->stop_charpos;
8200 else
8201 it->prev_stop = BEGV;
8202 it->bidi_p = true;
8203 it->current = save_current;
8204 it->position = save_position;
8205 it->stop_charpos = save_stop_pos;
8206 it->end_charpos = save_end_pos;
8209 /* Scan forward from CHARPOS in the current buffer/string, until we
8210 find a stop position > current IT's position. Then handle the stop
8211 position before that. This is called when we bump into a stop
8212 position while reordering bidirectional text. CHARPOS should be
8213 the last previously processed stop_pos (or BEGV/0, if none were
8214 processed yet) whose position is less that IT's current
8215 position. */
8217 static void
8218 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8220 bool bufp = !STRINGP (it->string);
8221 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8222 struct display_pos save_current = it->current;
8223 struct text_pos save_position = it->position;
8224 struct text_pos pos1;
8225 ptrdiff_t next_stop;
8227 /* Scan in strict logical order. */
8228 eassert (it->bidi_p);
8229 it->bidi_p = false;
8232 it->prev_stop = charpos;
8233 if (bufp)
8235 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8236 reseat_1 (it, pos1, false);
8238 else
8239 it->current.string_pos = string_pos (charpos, it->string);
8240 compute_stop_pos (it);
8241 /* We must advance forward, right? */
8242 if (it->stop_charpos <= it->prev_stop)
8243 emacs_abort ();
8244 charpos = it->stop_charpos;
8246 while (charpos <= where_we_are);
8248 it->bidi_p = true;
8249 it->current = save_current;
8250 it->position = save_position;
8251 next_stop = it->stop_charpos;
8252 it->stop_charpos = it->prev_stop;
8253 handle_stop (it);
8254 it->stop_charpos = next_stop;
8257 /* Load IT with the next display element from current_buffer. Value
8258 is false if end of buffer reached. IT->stop_charpos is the next
8259 position at which to stop and check for text properties or buffer
8260 end. */
8262 static bool
8263 next_element_from_buffer (struct it *it)
8265 bool success_p = true;
8267 eassert (IT_CHARPOS (*it) >= BEGV);
8268 eassert (NILP (it->string) && !it->s);
8269 eassert (!it->bidi_p
8270 || (EQ (it->bidi_it.string.lstring, Qnil)
8271 && it->bidi_it.string.s == NULL));
8273 /* With bidi reordering, the character to display might not be the
8274 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8275 we were reseat()ed to a new buffer position, which is potentially
8276 a different paragraph. */
8277 if (it->bidi_p && it->bidi_it.first_elt)
8279 get_visually_first_element (it);
8280 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8283 if (IT_CHARPOS (*it) >= it->stop_charpos)
8285 if (IT_CHARPOS (*it) >= it->end_charpos)
8287 bool overlay_strings_follow_p;
8289 /* End of the game, except when overlay strings follow that
8290 haven't been returned yet. */
8291 if (it->overlay_strings_at_end_processed_p)
8292 overlay_strings_follow_p = false;
8293 else
8295 it->overlay_strings_at_end_processed_p = true;
8296 overlay_strings_follow_p = get_overlay_strings (it, 0);
8299 if (overlay_strings_follow_p)
8300 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8301 else
8303 it->what = IT_EOB;
8304 it->position = it->current.pos;
8305 success_p = false;
8308 else if (!(!it->bidi_p
8309 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8310 || IT_CHARPOS (*it) == it->stop_charpos))
8312 /* With bidi non-linear iteration, we could find ourselves
8313 far beyond the last computed stop_charpos, with several
8314 other stop positions in between that we missed. Scan
8315 them all now, in buffer's logical order, until we find
8316 and handle the last stop_charpos that precedes our
8317 current position. */
8318 handle_stop_backwards (it, it->stop_charpos);
8319 it->ignore_overlay_strings_at_pos_p = false;
8320 return GET_NEXT_DISPLAY_ELEMENT (it);
8322 else
8324 if (it->bidi_p)
8326 /* Take note of the stop position we just moved across,
8327 for when we will move back across it. */
8328 it->prev_stop = it->stop_charpos;
8329 /* If we are at base paragraph embedding level, take
8330 note of the last stop position seen at this
8331 level. */
8332 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8333 it->base_level_stop = it->stop_charpos;
8335 handle_stop (it);
8336 it->ignore_overlay_strings_at_pos_p = false;
8337 return GET_NEXT_DISPLAY_ELEMENT (it);
8340 else if (it->bidi_p
8341 /* If we are before prev_stop, we may have overstepped on
8342 our way backwards a stop_pos, and if so, we need to
8343 handle that stop_pos. */
8344 && IT_CHARPOS (*it) < it->prev_stop
8345 /* We can sometimes back up for reasons that have nothing
8346 to do with bidi reordering. E.g., compositions. The
8347 code below is only needed when we are above the base
8348 embedding level, so test for that explicitly. */
8349 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8351 if (it->base_level_stop <= 0
8352 || IT_CHARPOS (*it) < it->base_level_stop)
8354 /* If we lost track of base_level_stop, we need to find
8355 prev_stop by looking backwards. This happens, e.g., when
8356 we were reseated to the previous screenful of text by
8357 vertical-motion. */
8358 it->base_level_stop = BEGV;
8359 compute_stop_pos_backwards (it);
8360 handle_stop_backwards (it, it->prev_stop);
8362 else
8363 handle_stop_backwards (it, it->base_level_stop);
8364 it->ignore_overlay_strings_at_pos_p = false;
8365 return GET_NEXT_DISPLAY_ELEMENT (it);
8367 else
8369 /* No face changes, overlays etc. in sight, so just return a
8370 character from current_buffer. */
8371 unsigned char *p;
8372 ptrdiff_t stop;
8374 /* We moved to the next buffer position, so any info about
8375 previously seen overlays is no longer valid. */
8376 it->ignore_overlay_strings_at_pos_p = false;
8378 /* Maybe run the redisplay end trigger hook. Performance note:
8379 This doesn't seem to cost measurable time. */
8380 if (it->redisplay_end_trigger_charpos
8381 && it->glyph_row
8382 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8383 run_redisplay_end_trigger_hook (it);
8385 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8386 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8387 stop)
8388 && next_element_from_composition (it))
8390 return true;
8393 /* Get the next character, maybe multibyte. */
8394 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8395 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8396 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8397 else
8398 it->c = *p, it->len = 1;
8400 /* Record what we have and where it came from. */
8401 it->what = IT_CHARACTER;
8402 it->object = it->w->contents;
8403 it->position = it->current.pos;
8405 /* Normally we return the character found above, except when we
8406 really want to return an ellipsis for selective display. */
8407 if (it->selective)
8409 if (it->c == '\n')
8411 /* A value of selective > 0 means hide lines indented more
8412 than that number of columns. */
8413 if (it->selective > 0
8414 && IT_CHARPOS (*it) + 1 < ZV
8415 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8416 IT_BYTEPOS (*it) + 1,
8417 it->selective))
8419 success_p = next_element_from_ellipsis (it);
8420 it->dpvec_char_len = -1;
8423 else if (it->c == '\r' && it->selective == -1)
8425 /* A value of selective == -1 means that everything from the
8426 CR to the end of the line is invisible, with maybe an
8427 ellipsis displayed for it. */
8428 success_p = next_element_from_ellipsis (it);
8429 it->dpvec_char_len = -1;
8434 /* Value is false if end of buffer reached. */
8435 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8436 return success_p;
8440 /* Run the redisplay end trigger hook for IT. */
8442 static void
8443 run_redisplay_end_trigger_hook (struct it *it)
8445 /* IT->glyph_row should be non-null, i.e. we should be actually
8446 displaying something, or otherwise we should not run the hook. */
8447 eassert (it->glyph_row);
8449 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8450 it->redisplay_end_trigger_charpos = 0;
8452 /* Since we are *trying* to run these functions, don't try to run
8453 them again, even if they get an error. */
8454 wset_redisplay_end_trigger (it->w, Qnil);
8455 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8456 make_number (charpos));
8458 /* Notice if it changed the face of the character we are on. */
8459 handle_face_prop (it);
8463 /* Deliver a composition display element. Unlike the other
8464 next_element_from_XXX, this function is not registered in the array
8465 get_next_element[]. It is called from next_element_from_buffer and
8466 next_element_from_string when necessary. */
8468 static bool
8469 next_element_from_composition (struct it *it)
8471 it->what = IT_COMPOSITION;
8472 it->len = it->cmp_it.nbytes;
8473 if (STRINGP (it->string))
8475 if (it->c < 0)
8477 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8478 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8479 return false;
8481 it->position = it->current.string_pos;
8482 it->object = it->string;
8483 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8484 IT_STRING_BYTEPOS (*it), it->string);
8486 else
8488 if (it->c < 0)
8490 IT_CHARPOS (*it) += it->cmp_it.nchars;
8491 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8492 if (it->bidi_p)
8494 if (it->bidi_it.new_paragraph)
8495 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8496 false);
8497 /* Resync the bidi iterator with IT's new position.
8498 FIXME: this doesn't support bidirectional text. */
8499 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8500 bidi_move_to_visually_next (&it->bidi_it);
8502 return false;
8504 it->position = it->current.pos;
8505 it->object = it->w->contents;
8506 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8507 IT_BYTEPOS (*it), Qnil);
8509 return true;
8514 /***********************************************************************
8515 Moving an iterator without producing glyphs
8516 ***********************************************************************/
8518 /* Check if iterator is at a position corresponding to a valid buffer
8519 position after some move_it_ call. */
8521 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8522 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8525 /* Move iterator IT to a specified buffer or X position within one
8526 line on the display without producing glyphs.
8528 OP should be a bit mask including some or all of these bits:
8529 MOVE_TO_X: Stop upon reaching x-position TO_X.
8530 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8531 Regardless of OP's value, stop upon reaching the end of the display line.
8533 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8534 This means, in particular, that TO_X includes window's horizontal
8535 scroll amount.
8537 The return value has several possible values that
8538 say what condition caused the scan to stop:
8540 MOVE_POS_MATCH_OR_ZV
8541 - when TO_POS or ZV was reached.
8543 MOVE_X_REACHED
8544 -when TO_X was reached before TO_POS or ZV were reached.
8546 MOVE_LINE_CONTINUED
8547 - when we reached the end of the display area and the line must
8548 be continued.
8550 MOVE_LINE_TRUNCATED
8551 - when we reached the end of the display area and the line is
8552 truncated.
8554 MOVE_NEWLINE_OR_CR
8555 - when we stopped at a line end, i.e. a newline or a CR and selective
8556 display is on. */
8558 static enum move_it_result
8559 move_it_in_display_line_to (struct it *it,
8560 ptrdiff_t to_charpos, int to_x,
8561 enum move_operation_enum op)
8563 enum move_it_result result = MOVE_UNDEFINED;
8564 struct glyph_row *saved_glyph_row;
8565 struct it wrap_it, atpos_it, atx_it, ppos_it;
8566 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8567 void *ppos_data = NULL;
8568 bool may_wrap = false;
8569 enum it_method prev_method = it->method;
8570 ptrdiff_t closest_pos UNINIT;
8571 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8572 bool saw_smaller_pos = prev_pos < to_charpos;
8574 /* Don't produce glyphs in produce_glyphs. */
8575 saved_glyph_row = it->glyph_row;
8576 it->glyph_row = NULL;
8578 /* Use wrap_it to save a copy of IT wherever a word wrap could
8579 occur. Use atpos_it to save a copy of IT at the desired buffer
8580 position, if found, so that we can scan ahead and check if the
8581 word later overshoots the window edge. Use atx_it similarly, for
8582 pixel positions. */
8583 wrap_it.sp = -1;
8584 atpos_it.sp = -1;
8585 atx_it.sp = -1;
8587 /* Use ppos_it under bidi reordering to save a copy of IT for the
8588 initial position. We restore that position in IT when we have
8589 scanned the entire display line without finding a match for
8590 TO_CHARPOS and all the character positions are greater than
8591 TO_CHARPOS. We then restart the scan from the initial position,
8592 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8593 the closest to TO_CHARPOS. */
8594 if (it->bidi_p)
8596 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8598 SAVE_IT (ppos_it, *it, ppos_data);
8599 closest_pos = IT_CHARPOS (*it);
8601 else
8602 closest_pos = ZV;
8605 #define BUFFER_POS_REACHED_P() \
8606 ((op & MOVE_TO_POS) != 0 \
8607 && BUFFERP (it->object) \
8608 && (IT_CHARPOS (*it) == to_charpos \
8609 || ((!it->bidi_p \
8610 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8611 && IT_CHARPOS (*it) > to_charpos) \
8612 || (it->what == IT_COMPOSITION \
8613 && ((IT_CHARPOS (*it) > to_charpos \
8614 && to_charpos >= it->cmp_it.charpos) \
8615 || (IT_CHARPOS (*it) < to_charpos \
8616 && to_charpos <= it->cmp_it.charpos)))) \
8617 && (it->method == GET_FROM_BUFFER \
8618 || (it->method == GET_FROM_DISPLAY_VECTOR \
8619 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8621 /* If there's a line-/wrap-prefix, handle it. */
8622 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8623 handle_line_prefix (it);
8625 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8626 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8628 while (true)
8630 int x, i, ascent = 0, descent = 0;
8632 /* Utility macro to reset an iterator with x, ascent, and descent. */
8633 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8634 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8635 (IT)->max_descent = descent)
8637 /* Stop if we move beyond TO_CHARPOS (after an image or a
8638 display string or stretch glyph). */
8639 if ((op & MOVE_TO_POS) != 0
8640 && BUFFERP (it->object)
8641 && it->method == GET_FROM_BUFFER
8642 && (((!it->bidi_p
8643 /* When the iterator is at base embedding level, we
8644 are guaranteed that characters are delivered for
8645 display in strictly increasing order of their
8646 buffer positions. */
8647 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8648 && IT_CHARPOS (*it) > to_charpos)
8649 || (it->bidi_p
8650 && (prev_method == GET_FROM_IMAGE
8651 || prev_method == GET_FROM_STRETCH
8652 || prev_method == GET_FROM_STRING)
8653 /* Passed TO_CHARPOS from left to right. */
8654 && ((prev_pos < to_charpos
8655 && IT_CHARPOS (*it) > to_charpos)
8656 /* Passed TO_CHARPOS from right to left. */
8657 || (prev_pos > to_charpos
8658 && IT_CHARPOS (*it) < to_charpos)))))
8660 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8662 result = MOVE_POS_MATCH_OR_ZV;
8663 break;
8665 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8666 /* If wrap_it is valid, the current position might be in a
8667 word that is wrapped. So, save the iterator in
8668 atpos_it and continue to see if wrapping happens. */
8669 SAVE_IT (atpos_it, *it, atpos_data);
8672 /* Stop when ZV reached.
8673 We used to stop here when TO_CHARPOS reached as well, but that is
8674 too soon if this glyph does not fit on this line. So we handle it
8675 explicitly below. */
8676 if (!get_next_display_element (it))
8678 result = MOVE_POS_MATCH_OR_ZV;
8679 break;
8682 if (it->line_wrap == TRUNCATE)
8684 if (BUFFER_POS_REACHED_P ())
8686 result = MOVE_POS_MATCH_OR_ZV;
8687 break;
8690 else
8692 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8694 if (IT_DISPLAYING_WHITESPACE (it))
8695 may_wrap = true;
8696 else if (may_wrap)
8698 /* We have reached a glyph that follows one or more
8699 whitespace characters. If the position is
8700 already found, we are done. */
8701 if (atpos_it.sp >= 0)
8703 RESTORE_IT (it, &atpos_it, atpos_data);
8704 result = MOVE_POS_MATCH_OR_ZV;
8705 goto done;
8707 if (atx_it.sp >= 0)
8709 RESTORE_IT (it, &atx_it, atx_data);
8710 result = MOVE_X_REACHED;
8711 goto done;
8713 /* Otherwise, we can wrap here. */
8714 SAVE_IT (wrap_it, *it, wrap_data);
8715 may_wrap = false;
8720 /* Remember the line height for the current line, in case
8721 the next element doesn't fit on the line. */
8722 ascent = it->max_ascent;
8723 descent = it->max_descent;
8725 /* The call to produce_glyphs will get the metrics of the
8726 display element IT is loaded with. Record the x-position
8727 before this display element, in case it doesn't fit on the
8728 line. */
8729 x = it->current_x;
8731 PRODUCE_GLYPHS (it);
8733 if (it->area != TEXT_AREA)
8735 prev_method = it->method;
8736 if (it->method == GET_FROM_BUFFER)
8737 prev_pos = IT_CHARPOS (*it);
8738 set_iterator_to_next (it, true);
8739 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8740 SET_TEXT_POS (this_line_min_pos,
8741 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8742 if (it->bidi_p
8743 && (op & MOVE_TO_POS)
8744 && IT_CHARPOS (*it) > to_charpos
8745 && IT_CHARPOS (*it) < closest_pos)
8746 closest_pos = IT_CHARPOS (*it);
8747 continue;
8750 /* The number of glyphs we get back in IT->nglyphs will normally
8751 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8752 character on a terminal frame, or (iii) a line end. For the
8753 second case, IT->nglyphs - 1 padding glyphs will be present.
8754 (On X frames, there is only one glyph produced for a
8755 composite character.)
8757 The behavior implemented below means, for continuation lines,
8758 that as many spaces of a TAB as fit on the current line are
8759 displayed there. For terminal frames, as many glyphs of a
8760 multi-glyph character are displayed in the current line, too.
8761 This is what the old redisplay code did, and we keep it that
8762 way. Under X, the whole shape of a complex character must
8763 fit on the line or it will be completely displayed in the
8764 next line.
8766 Note that both for tabs and padding glyphs, all glyphs have
8767 the same width. */
8768 if (it->nglyphs)
8770 /* More than one glyph or glyph doesn't fit on line. All
8771 glyphs have the same width. */
8772 int single_glyph_width = it->pixel_width / it->nglyphs;
8773 int new_x;
8774 int x_before_this_char = x;
8775 int hpos_before_this_char = it->hpos;
8777 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8779 new_x = x + single_glyph_width;
8781 /* We want to leave anything reaching TO_X to the caller. */
8782 if ((op & MOVE_TO_X) && new_x > to_x)
8784 if (BUFFER_POS_REACHED_P ())
8786 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8787 goto buffer_pos_reached;
8788 if (atpos_it.sp < 0)
8790 SAVE_IT (atpos_it, *it, atpos_data);
8791 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8794 else
8796 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8798 it->current_x = x;
8799 result = MOVE_X_REACHED;
8800 break;
8802 if (atx_it.sp < 0)
8804 SAVE_IT (atx_it, *it, atx_data);
8805 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8810 if (/* Lines are continued. */
8811 it->line_wrap != TRUNCATE
8812 && (/* And glyph doesn't fit on the line. */
8813 new_x > it->last_visible_x
8814 /* Or it fits exactly and we're on a window
8815 system frame. */
8816 || (new_x == it->last_visible_x
8817 && FRAME_WINDOW_P (it->f)
8818 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8819 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8820 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8822 bool moved_forward = false;
8824 if (/* IT->hpos == 0 means the very first glyph
8825 doesn't fit on the line, e.g. a wide image. */
8826 it->hpos == 0
8827 || (new_x == it->last_visible_x
8828 && FRAME_WINDOW_P (it->f)))
8830 ++it->hpos;
8831 it->current_x = new_x;
8833 /* The character's last glyph just barely fits
8834 in this row. */
8835 if (i == it->nglyphs - 1)
8837 /* If this is the destination position,
8838 return a position *before* it in this row,
8839 now that we know it fits in this row. */
8840 if (BUFFER_POS_REACHED_P ())
8842 bool can_wrap = true;
8844 /* If we are at a whitespace character
8845 that barely fits on this screen line,
8846 but the next character is also
8847 whitespace, we cannot wrap here. */
8848 if (it->line_wrap == WORD_WRAP
8849 && wrap_it.sp >= 0
8850 && may_wrap
8851 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8853 struct it tem_it;
8854 void *tem_data = NULL;
8856 SAVE_IT (tem_it, *it, tem_data);
8857 set_iterator_to_next (it, true);
8858 if (get_next_display_element (it)
8859 && IT_DISPLAYING_WHITESPACE (it))
8860 can_wrap = false;
8861 RESTORE_IT (it, &tem_it, tem_data);
8863 if (it->line_wrap != WORD_WRAP
8864 || wrap_it.sp < 0
8865 /* If we've just found whitespace
8866 where we can wrap, effectively
8867 ignore the previous wrap point --
8868 it is no longer relevant, but we
8869 won't have an opportunity to
8870 update it, since we've reached
8871 the edge of this screen line. */
8872 || (may_wrap && can_wrap
8873 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8875 it->hpos = hpos_before_this_char;
8876 it->current_x = x_before_this_char;
8877 result = MOVE_POS_MATCH_OR_ZV;
8878 break;
8880 if (it->line_wrap == WORD_WRAP
8881 && atpos_it.sp < 0)
8883 SAVE_IT (atpos_it, *it, atpos_data);
8884 atpos_it.current_x = x_before_this_char;
8885 atpos_it.hpos = hpos_before_this_char;
8889 prev_method = it->method;
8890 if (it->method == GET_FROM_BUFFER)
8891 prev_pos = IT_CHARPOS (*it);
8892 set_iterator_to_next (it, true);
8893 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8894 SET_TEXT_POS (this_line_min_pos,
8895 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8896 /* On graphical terminals, newlines may
8897 "overflow" into the fringe if
8898 overflow-newline-into-fringe is non-nil.
8899 On text terminals, and on graphical
8900 terminals with no right margin, newlines
8901 may overflow into the last glyph on the
8902 display line.*/
8903 if (!FRAME_WINDOW_P (it->f)
8904 || ((it->bidi_p
8905 && it->bidi_it.paragraph_dir == R2L)
8906 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8907 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8908 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8910 if (!get_next_display_element (it))
8912 result = MOVE_POS_MATCH_OR_ZV;
8913 break;
8915 moved_forward = true;
8916 if (BUFFER_POS_REACHED_P ())
8918 if (ITERATOR_AT_END_OF_LINE_P (it))
8919 result = MOVE_POS_MATCH_OR_ZV;
8920 else
8921 result = MOVE_LINE_CONTINUED;
8922 break;
8924 if (ITERATOR_AT_END_OF_LINE_P (it)
8925 && (it->line_wrap != WORD_WRAP
8926 || wrap_it.sp < 0
8927 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8929 result = MOVE_NEWLINE_OR_CR;
8930 break;
8935 else
8936 IT_RESET_X_ASCENT_DESCENT (it);
8938 /* If the screen line ends with whitespace, and we
8939 are under word-wrap, don't use wrap_it: it is no
8940 longer relevant, but we won't have an opportunity
8941 to update it, since we are done with this screen
8942 line. */
8943 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8944 /* If the character after the one which set the
8945 may_wrap flag is also whitespace, we can't
8946 wrap here, since the screen line cannot be
8947 wrapped in the middle of whitespace.
8948 Therefore, wrap_it _is_ relevant in that
8949 case. */
8950 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8952 /* If we've found TO_X, go back there, as we now
8953 know the last word fits on this screen line. */
8954 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8955 && atx_it.sp >= 0)
8957 RESTORE_IT (it, &atx_it, atx_data);
8958 atpos_it.sp = -1;
8959 atx_it.sp = -1;
8960 result = MOVE_X_REACHED;
8961 break;
8964 else if (wrap_it.sp >= 0)
8966 RESTORE_IT (it, &wrap_it, wrap_data);
8967 atpos_it.sp = -1;
8968 atx_it.sp = -1;
8971 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8972 IT_CHARPOS (*it)));
8973 result = MOVE_LINE_CONTINUED;
8974 break;
8977 if (BUFFER_POS_REACHED_P ())
8979 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8980 goto buffer_pos_reached;
8981 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8983 SAVE_IT (atpos_it, *it, atpos_data);
8984 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8988 if (new_x > it->first_visible_x)
8990 /* Glyph is visible. Increment number of glyphs that
8991 would be displayed. */
8992 ++it->hpos;
8996 if (result != MOVE_UNDEFINED)
8997 break;
8999 else if (BUFFER_POS_REACHED_P ())
9001 buffer_pos_reached:
9002 IT_RESET_X_ASCENT_DESCENT (it);
9003 result = MOVE_POS_MATCH_OR_ZV;
9004 break;
9006 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9008 /* Stop when TO_X specified and reached. This check is
9009 necessary here because of lines consisting of a line end,
9010 only. The line end will not produce any glyphs and we
9011 would never get MOVE_X_REACHED. */
9012 eassert (it->nglyphs == 0);
9013 result = MOVE_X_REACHED;
9014 break;
9017 /* Is this a line end? If yes, we're done. */
9018 if (ITERATOR_AT_END_OF_LINE_P (it))
9020 /* If we are past TO_CHARPOS, but never saw any character
9021 positions smaller than TO_CHARPOS, return
9022 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9023 did. */
9024 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9026 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9028 if (closest_pos < ZV)
9030 RESTORE_IT (it, &ppos_it, ppos_data);
9031 /* Don't recurse if closest_pos is equal to
9032 to_charpos, since we have just tried that. */
9033 if (closest_pos != to_charpos)
9034 move_it_in_display_line_to (it, closest_pos, -1,
9035 MOVE_TO_POS);
9036 result = MOVE_POS_MATCH_OR_ZV;
9038 else
9039 goto buffer_pos_reached;
9041 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9042 && IT_CHARPOS (*it) > to_charpos)
9043 goto buffer_pos_reached;
9044 else
9045 result = MOVE_NEWLINE_OR_CR;
9047 else
9048 result = MOVE_NEWLINE_OR_CR;
9049 /* If we've processed the newline, make sure this flag is
9050 reset, as it must only be set when the newline itself is
9051 processed. */
9052 if (result == MOVE_NEWLINE_OR_CR)
9053 it->constrain_row_ascent_descent_p = false;
9054 break;
9057 prev_method = it->method;
9058 if (it->method == GET_FROM_BUFFER)
9059 prev_pos = IT_CHARPOS (*it);
9060 /* The current display element has been consumed. Advance
9061 to the next. */
9062 set_iterator_to_next (it, true);
9063 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9064 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9065 if (IT_CHARPOS (*it) < to_charpos)
9066 saw_smaller_pos = true;
9067 if (it->bidi_p
9068 && (op & MOVE_TO_POS)
9069 && IT_CHARPOS (*it) >= to_charpos
9070 && IT_CHARPOS (*it) < closest_pos)
9071 closest_pos = IT_CHARPOS (*it);
9073 /* Stop if lines are truncated and IT's current x-position is
9074 past the right edge of the window now. */
9075 if (it->line_wrap == TRUNCATE
9076 && it->current_x >= it->last_visible_x)
9078 if (!FRAME_WINDOW_P (it->f)
9079 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9080 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9081 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9082 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9084 bool at_eob_p = false;
9086 if ((at_eob_p = !get_next_display_element (it))
9087 || BUFFER_POS_REACHED_P ()
9088 /* If we are past TO_CHARPOS, but never saw any
9089 character positions smaller than TO_CHARPOS,
9090 return MOVE_POS_MATCH_OR_ZV, like the
9091 unidirectional display did. */
9092 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9093 && !saw_smaller_pos
9094 && IT_CHARPOS (*it) > to_charpos))
9096 if (it->bidi_p
9097 && !BUFFER_POS_REACHED_P ()
9098 && !at_eob_p && closest_pos < ZV)
9100 RESTORE_IT (it, &ppos_it, ppos_data);
9101 if (closest_pos != to_charpos)
9102 move_it_in_display_line_to (it, closest_pos, -1,
9103 MOVE_TO_POS);
9105 result = MOVE_POS_MATCH_OR_ZV;
9106 break;
9108 if (ITERATOR_AT_END_OF_LINE_P (it))
9110 result = MOVE_NEWLINE_OR_CR;
9111 break;
9114 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9115 && !saw_smaller_pos
9116 && IT_CHARPOS (*it) > to_charpos)
9118 if (closest_pos < ZV)
9120 RESTORE_IT (it, &ppos_it, ppos_data);
9121 if (closest_pos != to_charpos)
9122 move_it_in_display_line_to (it, closest_pos, -1,
9123 MOVE_TO_POS);
9125 result = MOVE_POS_MATCH_OR_ZV;
9126 break;
9128 result = MOVE_LINE_TRUNCATED;
9129 break;
9131 #undef IT_RESET_X_ASCENT_DESCENT
9134 #undef BUFFER_POS_REACHED_P
9136 /* If we scanned beyond TO_POS, restore the saved iterator either to
9137 the wrap point (if found), or to atpos/atx location. We decide which
9138 data to use to restore the saved iterator state by their X coordinates,
9139 since buffer positions might increase non-monotonically with screen
9140 coordinates due to bidi reordering. */
9141 if (result == MOVE_LINE_CONTINUED
9142 && it->line_wrap == WORD_WRAP
9143 && wrap_it.sp >= 0
9144 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9145 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9146 RESTORE_IT (it, &wrap_it, wrap_data);
9147 else if (atpos_it.sp >= 0)
9148 RESTORE_IT (it, &atpos_it, atpos_data);
9149 else if (atx_it.sp >= 0)
9150 RESTORE_IT (it, &atx_it, atx_data);
9152 done:
9154 if (atpos_data)
9155 bidi_unshelve_cache (atpos_data, true);
9156 if (atx_data)
9157 bidi_unshelve_cache (atx_data, true);
9158 if (wrap_data)
9159 bidi_unshelve_cache (wrap_data, true);
9160 if (ppos_data)
9161 bidi_unshelve_cache (ppos_data, true);
9163 /* Restore the iterator settings altered at the beginning of this
9164 function. */
9165 it->glyph_row = saved_glyph_row;
9166 return result;
9169 /* For external use. */
9170 void
9171 move_it_in_display_line (struct it *it,
9172 ptrdiff_t to_charpos, int to_x,
9173 enum move_operation_enum op)
9175 if (it->line_wrap == WORD_WRAP
9176 && (op & MOVE_TO_X))
9178 struct it save_it;
9179 void *save_data = NULL;
9180 int skip;
9182 SAVE_IT (save_it, *it, save_data);
9183 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9184 /* When word-wrap is on, TO_X may lie past the end
9185 of a wrapped line. Then it->current is the
9186 character on the next line, so backtrack to the
9187 space before the wrap point. */
9188 if (skip == MOVE_LINE_CONTINUED)
9190 int prev_x = max (it->current_x - 1, 0);
9191 RESTORE_IT (it, &save_it, save_data);
9192 move_it_in_display_line_to
9193 (it, -1, prev_x, MOVE_TO_X);
9195 else
9196 bidi_unshelve_cache (save_data, true);
9198 else
9199 move_it_in_display_line_to (it, to_charpos, to_x, op);
9203 /* Move IT forward until it satisfies one or more of the criteria in
9204 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9206 OP is a bit-mask that specifies where to stop, and in particular,
9207 which of those four position arguments makes a difference. See the
9208 description of enum move_operation_enum.
9210 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9211 screen line, this function will set IT to the next position that is
9212 displayed to the right of TO_CHARPOS on the screen.
9214 Return the maximum pixel length of any line scanned but never more
9215 than it.last_visible_x. */
9218 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9220 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9221 int line_height, line_start_x = 0, reached = 0;
9222 int max_current_x = 0;
9223 void *backup_data = NULL;
9225 for (;;)
9227 if (op & MOVE_TO_VPOS)
9229 /* If no TO_CHARPOS and no TO_X specified, stop at the
9230 start of the line TO_VPOS. */
9231 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9233 if (it->vpos == to_vpos)
9235 reached = 1;
9236 break;
9238 else
9239 skip = move_it_in_display_line_to (it, -1, -1, 0);
9241 else
9243 /* TO_VPOS >= 0 means stop at TO_X in the line at
9244 TO_VPOS, or at TO_POS, whichever comes first. */
9245 if (it->vpos == to_vpos)
9247 reached = 2;
9248 break;
9251 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9253 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9255 reached = 3;
9256 break;
9258 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9260 /* We have reached TO_X but not in the line we want. */
9261 skip = move_it_in_display_line_to (it, to_charpos,
9262 -1, MOVE_TO_POS);
9263 if (skip == MOVE_POS_MATCH_OR_ZV)
9265 reached = 4;
9266 break;
9271 else if (op & MOVE_TO_Y)
9273 struct it it_backup;
9275 if (it->line_wrap == WORD_WRAP)
9276 SAVE_IT (it_backup, *it, backup_data);
9278 /* TO_Y specified means stop at TO_X in the line containing
9279 TO_Y---or at TO_CHARPOS if this is reached first. The
9280 problem is that we can't really tell whether the line
9281 contains TO_Y before we have completely scanned it, and
9282 this may skip past TO_X. What we do is to first scan to
9283 TO_X.
9285 If TO_X is not specified, use a TO_X of zero. The reason
9286 is to make the outcome of this function more predictable.
9287 If we didn't use TO_X == 0, we would stop at the end of
9288 the line which is probably not what a caller would expect
9289 to happen. */
9290 skip = move_it_in_display_line_to
9291 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9292 (MOVE_TO_X | (op & MOVE_TO_POS)));
9294 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9295 if (skip == MOVE_POS_MATCH_OR_ZV)
9296 reached = 5;
9297 else if (skip == MOVE_X_REACHED)
9299 /* If TO_X was reached, we want to know whether TO_Y is
9300 in the line. We know this is the case if the already
9301 scanned glyphs make the line tall enough. Otherwise,
9302 we must check by scanning the rest of the line. */
9303 line_height = it->max_ascent + it->max_descent;
9304 if (to_y >= it->current_y
9305 && to_y < it->current_y + line_height)
9307 reached = 6;
9308 break;
9310 SAVE_IT (it_backup, *it, backup_data);
9311 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9312 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9313 op & MOVE_TO_POS);
9314 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9315 line_height = it->max_ascent + it->max_descent;
9316 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9318 if (to_y >= it->current_y
9319 && to_y < it->current_y + line_height)
9321 /* If TO_Y is in this line and TO_X was reached
9322 above, we scanned too far. We have to restore
9323 IT's settings to the ones before skipping. But
9324 keep the more accurate values of max_ascent and
9325 max_descent we've found while skipping the rest
9326 of the line, for the sake of callers, such as
9327 pos_visible_p, that need to know the line
9328 height. */
9329 int max_ascent = it->max_ascent;
9330 int max_descent = it->max_descent;
9332 RESTORE_IT (it, &it_backup, backup_data);
9333 it->max_ascent = max_ascent;
9334 it->max_descent = max_descent;
9335 reached = 6;
9337 else
9339 skip = skip2;
9340 if (skip == MOVE_POS_MATCH_OR_ZV)
9341 reached = 7;
9344 else
9346 /* Check whether TO_Y is in this line. */
9347 line_height = it->max_ascent + it->max_descent;
9348 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9350 if (to_y >= it->current_y
9351 && to_y < it->current_y + line_height)
9353 if (to_y > it->current_y)
9354 max_current_x = max (it->current_x, max_current_x);
9356 /* When word-wrap is on, TO_X may lie past the end
9357 of a wrapped line. Then it->current is the
9358 character on the next line, so backtrack to the
9359 space before the wrap point. */
9360 if (skip == MOVE_LINE_CONTINUED
9361 && it->line_wrap == WORD_WRAP)
9363 int prev_x = max (it->current_x - 1, 0);
9364 RESTORE_IT (it, &it_backup, backup_data);
9365 skip = move_it_in_display_line_to
9366 (it, -1, prev_x, MOVE_TO_X);
9369 reached = 6;
9373 if (reached)
9375 max_current_x = max (it->current_x, max_current_x);
9376 break;
9379 else if (BUFFERP (it->object)
9380 && (it->method == GET_FROM_BUFFER
9381 || it->method == GET_FROM_STRETCH)
9382 && IT_CHARPOS (*it) >= to_charpos
9383 /* Under bidi iteration, a call to set_iterator_to_next
9384 can scan far beyond to_charpos if the initial
9385 portion of the next line needs to be reordered. In
9386 that case, give move_it_in_display_line_to another
9387 chance below. */
9388 && !(it->bidi_p
9389 && it->bidi_it.scan_dir == -1))
9390 skip = MOVE_POS_MATCH_OR_ZV;
9391 else
9392 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9394 switch (skip)
9396 case MOVE_POS_MATCH_OR_ZV:
9397 max_current_x = max (it->current_x, max_current_x);
9398 reached = 8;
9399 goto out;
9401 case MOVE_NEWLINE_OR_CR:
9402 max_current_x = max (it->current_x, max_current_x);
9403 set_iterator_to_next (it, true);
9404 it->continuation_lines_width = 0;
9405 break;
9407 case MOVE_LINE_TRUNCATED:
9408 max_current_x = it->last_visible_x;
9409 it->continuation_lines_width = 0;
9410 reseat_at_next_visible_line_start (it, false);
9411 if ((op & MOVE_TO_POS) != 0
9412 && IT_CHARPOS (*it) > to_charpos)
9414 reached = 9;
9415 goto out;
9417 break;
9419 case MOVE_LINE_CONTINUED:
9420 max_current_x = it->last_visible_x;
9421 /* For continued lines ending in a tab, some of the glyphs
9422 associated with the tab are displayed on the current
9423 line. Since it->current_x does not include these glyphs,
9424 we use it->last_visible_x instead. */
9425 if (it->c == '\t')
9427 it->continuation_lines_width += it->last_visible_x;
9428 /* When moving by vpos, ensure that the iterator really
9429 advances to the next line (bug#847, bug#969). Fixme:
9430 do we need to do this in other circumstances? */
9431 if (it->current_x != it->last_visible_x
9432 && (op & MOVE_TO_VPOS)
9433 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9435 line_start_x = it->current_x + it->pixel_width
9436 - it->last_visible_x;
9437 if (FRAME_WINDOW_P (it->f))
9439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9440 struct font *face_font = face->font;
9442 /* When display_line produces a continued line
9443 that ends in a TAB, it skips a tab stop that
9444 is closer than the font's space character
9445 width (see x_produce_glyphs where it produces
9446 the stretch glyph which represents a TAB).
9447 We need to reproduce the same logic here. */
9448 eassert (face_font);
9449 if (face_font)
9451 if (line_start_x < face_font->space_width)
9452 line_start_x
9453 += it->tab_width * face_font->space_width;
9456 set_iterator_to_next (it, false);
9459 else
9460 it->continuation_lines_width += it->current_x;
9461 break;
9463 default:
9464 emacs_abort ();
9467 /* Reset/increment for the next run. */
9468 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9469 it->current_x = line_start_x;
9470 line_start_x = 0;
9471 it->hpos = 0;
9472 it->current_y += it->max_ascent + it->max_descent;
9473 ++it->vpos;
9474 last_height = it->max_ascent + it->max_descent;
9475 it->max_ascent = it->max_descent = 0;
9478 out:
9480 /* On text terminals, we may stop at the end of a line in the middle
9481 of a multi-character glyph. If the glyph itself is continued,
9482 i.e. it is actually displayed on the next line, don't treat this
9483 stopping point as valid; move to the next line instead (unless
9484 that brings us offscreen). */
9485 if (!FRAME_WINDOW_P (it->f)
9486 && op & MOVE_TO_POS
9487 && IT_CHARPOS (*it) == to_charpos
9488 && it->what == IT_CHARACTER
9489 && it->nglyphs > 1
9490 && it->line_wrap == WINDOW_WRAP
9491 && it->current_x == it->last_visible_x - 1
9492 && it->c != '\n'
9493 && it->c != '\t'
9494 && it->w->window_end_valid
9495 && it->vpos < it->w->window_end_vpos)
9497 it->continuation_lines_width += it->current_x;
9498 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9499 it->current_y += it->max_ascent + it->max_descent;
9500 ++it->vpos;
9501 last_height = it->max_ascent + it->max_descent;
9504 if (backup_data)
9505 bidi_unshelve_cache (backup_data, true);
9507 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9509 return max_current_x;
9513 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9515 If DY > 0, move IT backward at least that many pixels. DY = 0
9516 means move IT backward to the preceding line start or BEGV. This
9517 function may move over more than DY pixels if IT->current_y - DY
9518 ends up in the middle of a line; in this case IT->current_y will be
9519 set to the top of the line moved to. */
9521 void
9522 move_it_vertically_backward (struct it *it, int dy)
9524 int nlines, h;
9525 struct it it2, it3;
9526 void *it2data = NULL, *it3data = NULL;
9527 ptrdiff_t start_pos;
9528 int nchars_per_row
9529 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9530 ptrdiff_t pos_limit;
9532 move_further_back:
9533 eassert (dy >= 0);
9535 start_pos = IT_CHARPOS (*it);
9537 /* Estimate how many newlines we must move back. */
9538 nlines = max (1, dy / default_line_pixel_height (it->w));
9539 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9540 pos_limit = BEGV;
9541 else
9542 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9544 /* Set the iterator's position that many lines back. But don't go
9545 back more than NLINES full screen lines -- this wins a day with
9546 buffers which have very long lines. */
9547 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9548 back_to_previous_visible_line_start (it);
9550 /* Reseat the iterator here. When moving backward, we don't want
9551 reseat to skip forward over invisible text, set up the iterator
9552 to deliver from overlay strings at the new position etc. So,
9553 use reseat_1 here. */
9554 reseat_1 (it, it->current.pos, true);
9556 /* We are now surely at a line start. */
9557 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9558 reordering is in effect. */
9559 it->continuation_lines_width = 0;
9561 /* Move forward and see what y-distance we moved. First move to the
9562 start of the next line so that we get its height. We need this
9563 height to be able to tell whether we reached the specified
9564 y-distance. */
9565 SAVE_IT (it2, *it, it2data);
9566 it2.max_ascent = it2.max_descent = 0;
9569 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9570 MOVE_TO_POS | MOVE_TO_VPOS);
9572 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9573 /* If we are in a display string which starts at START_POS,
9574 and that display string includes a newline, and we are
9575 right after that newline (i.e. at the beginning of a
9576 display line), exit the loop, because otherwise we will
9577 infloop, since move_it_to will see that it is already at
9578 START_POS and will not move. */
9579 || (it2.method == GET_FROM_STRING
9580 && IT_CHARPOS (it2) == start_pos
9581 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9582 eassert (IT_CHARPOS (*it) >= BEGV);
9583 SAVE_IT (it3, it2, it3data);
9585 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9586 eassert (IT_CHARPOS (*it) >= BEGV);
9587 /* H is the actual vertical distance from the position in *IT
9588 and the starting position. */
9589 h = it2.current_y - it->current_y;
9590 /* NLINES is the distance in number of lines. */
9591 nlines = it2.vpos - it->vpos;
9593 /* Correct IT's y and vpos position
9594 so that they are relative to the starting point. */
9595 it->vpos -= nlines;
9596 it->current_y -= h;
9598 if (dy == 0)
9600 /* DY == 0 means move to the start of the screen line. The
9601 value of nlines is > 0 if continuation lines were involved,
9602 or if the original IT position was at start of a line. */
9603 RESTORE_IT (it, it, it2data);
9604 if (nlines > 0)
9605 move_it_by_lines (it, nlines);
9606 /* The above code moves us to some position NLINES down,
9607 usually to its first glyph (leftmost in an L2R line), but
9608 that's not necessarily the start of the line, under bidi
9609 reordering. We want to get to the character position
9610 that is immediately after the newline of the previous
9611 line. */
9612 if (it->bidi_p
9613 && !it->continuation_lines_width
9614 && !STRINGP (it->string)
9615 && IT_CHARPOS (*it) > BEGV
9616 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9618 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9620 DEC_BOTH (cp, bp);
9621 cp = find_newline_no_quit (cp, bp, -1, NULL);
9622 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9624 bidi_unshelve_cache (it3data, true);
9626 else
9628 /* The y-position we try to reach, relative to *IT.
9629 Note that H has been subtracted in front of the if-statement. */
9630 int target_y = it->current_y + h - dy;
9631 int y0 = it3.current_y;
9632 int y1;
9633 int line_height;
9635 RESTORE_IT (&it3, &it3, it3data);
9636 y1 = line_bottom_y (&it3);
9637 line_height = y1 - y0;
9638 RESTORE_IT (it, it, it2data);
9639 /* If we did not reach target_y, try to move further backward if
9640 we can. If we moved too far backward, try to move forward. */
9641 if (target_y < it->current_y
9642 /* This is heuristic. In a window that's 3 lines high, with
9643 a line height of 13 pixels each, recentering with point
9644 on the bottom line will try to move -39/2 = 19 pixels
9645 backward. Try to avoid moving into the first line. */
9646 && (it->current_y - target_y
9647 > min (window_box_height (it->w), line_height * 2 / 3))
9648 && IT_CHARPOS (*it) > BEGV)
9650 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9651 target_y - it->current_y));
9652 dy = it->current_y - target_y;
9653 goto move_further_back;
9655 else if (target_y >= it->current_y + line_height
9656 && IT_CHARPOS (*it) < ZV)
9658 /* Should move forward by at least one line, maybe more.
9660 Note: Calling move_it_by_lines can be expensive on
9661 terminal frames, where compute_motion is used (via
9662 vmotion) to do the job, when there are very long lines
9663 and truncate-lines is nil. That's the reason for
9664 treating terminal frames specially here. */
9666 if (!FRAME_WINDOW_P (it->f))
9667 move_it_vertically (it, target_y - it->current_y);
9668 else
9672 move_it_by_lines (it, 1);
9674 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9681 /* Move IT by a specified amount of pixel lines DY. DY negative means
9682 move backwards. DY = 0 means move to start of screen line. At the
9683 end, IT will be on the start of a screen line. */
9685 void
9686 move_it_vertically (struct it *it, int dy)
9688 if (dy <= 0)
9689 move_it_vertically_backward (it, -dy);
9690 else
9692 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9693 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9694 MOVE_TO_POS | MOVE_TO_Y);
9695 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9697 /* If buffer ends in ZV without a newline, move to the start of
9698 the line to satisfy the post-condition. */
9699 if (IT_CHARPOS (*it) == ZV
9700 && ZV > BEGV
9701 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9702 move_it_by_lines (it, 0);
9707 /* Move iterator IT past the end of the text line it is in. */
9709 void
9710 move_it_past_eol (struct it *it)
9712 enum move_it_result rc;
9714 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9715 if (rc == MOVE_NEWLINE_OR_CR)
9716 set_iterator_to_next (it, false);
9720 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9721 negative means move up. DVPOS == 0 means move to the start of the
9722 screen line.
9724 Optimization idea: If we would know that IT->f doesn't use
9725 a face with proportional font, we could be faster for
9726 truncate-lines nil. */
9728 void
9729 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9732 /* The commented-out optimization uses vmotion on terminals. This
9733 gives bad results, because elements like it->what, on which
9734 callers such as pos_visible_p rely, aren't updated. */
9735 /* struct position pos;
9736 if (!FRAME_WINDOW_P (it->f))
9738 struct text_pos textpos;
9740 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9741 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9742 reseat (it, textpos, true);
9743 it->vpos += pos.vpos;
9744 it->current_y += pos.vpos;
9746 else */
9748 if (dvpos == 0)
9750 /* DVPOS == 0 means move to the start of the screen line. */
9751 move_it_vertically_backward (it, 0);
9752 /* Let next call to line_bottom_y calculate real line height. */
9753 last_height = 0;
9755 else if (dvpos > 0)
9757 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9758 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9760 /* Only move to the next buffer position if we ended up in a
9761 string from display property, not in an overlay string
9762 (before-string or after-string). That is because the
9763 latter don't conceal the underlying buffer position, so
9764 we can ask to move the iterator to the exact position we
9765 are interested in. Note that, even if we are already at
9766 IT_CHARPOS (*it), the call below is not a no-op, as it
9767 will detect that we are at the end of the string, pop the
9768 iterator, and compute it->current_x and it->hpos
9769 correctly. */
9770 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9771 -1, -1, -1, MOVE_TO_POS);
9774 else
9776 struct it it2;
9777 void *it2data = NULL;
9778 ptrdiff_t start_charpos, i;
9779 int nchars_per_row
9780 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9781 bool hit_pos_limit = false;
9782 ptrdiff_t pos_limit;
9784 /* Start at the beginning of the screen line containing IT's
9785 position. This may actually move vertically backwards,
9786 in case of overlays, so adjust dvpos accordingly. */
9787 dvpos += it->vpos;
9788 move_it_vertically_backward (it, 0);
9789 dvpos -= it->vpos;
9791 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9792 screen lines, and reseat the iterator there. */
9793 start_charpos = IT_CHARPOS (*it);
9794 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9795 pos_limit = BEGV;
9796 else
9797 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9799 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9800 back_to_previous_visible_line_start (it);
9801 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9802 hit_pos_limit = true;
9803 reseat (it, it->current.pos, true);
9805 /* Move further back if we end up in a string or an image. */
9806 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9808 /* First try to move to start of display line. */
9809 dvpos += it->vpos;
9810 move_it_vertically_backward (it, 0);
9811 dvpos -= it->vpos;
9812 if (IT_POS_VALID_AFTER_MOVE_P (it))
9813 break;
9814 /* If start of line is still in string or image,
9815 move further back. */
9816 back_to_previous_visible_line_start (it);
9817 reseat (it, it->current.pos, true);
9818 dvpos--;
9821 it->current_x = it->hpos = 0;
9823 /* Above call may have moved too far if continuation lines
9824 are involved. Scan forward and see if it did. */
9825 SAVE_IT (it2, *it, it2data);
9826 it2.vpos = it2.current_y = 0;
9827 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9828 it->vpos -= it2.vpos;
9829 it->current_y -= it2.current_y;
9830 it->current_x = it->hpos = 0;
9832 /* If we moved too far back, move IT some lines forward. */
9833 if (it2.vpos > -dvpos)
9835 int delta = it2.vpos + dvpos;
9837 RESTORE_IT (&it2, &it2, it2data);
9838 SAVE_IT (it2, *it, it2data);
9839 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9840 /* Move back again if we got too far ahead. */
9841 if (IT_CHARPOS (*it) >= start_charpos)
9842 RESTORE_IT (it, &it2, it2data);
9843 else
9844 bidi_unshelve_cache (it2data, true);
9846 else if (hit_pos_limit && pos_limit > BEGV
9847 && dvpos < 0 && it2.vpos < -dvpos)
9849 /* If we hit the limit, but still didn't make it far enough
9850 back, that means there's a display string with a newline
9851 covering a large chunk of text, and that caused
9852 back_to_previous_visible_line_start try to go too far.
9853 Punish those who commit such atrocities by going back
9854 until we've reached DVPOS, after lifting the limit, which
9855 could make it slow for very long lines. "If it hurts,
9856 don't do that!" */
9857 dvpos += it2.vpos;
9858 RESTORE_IT (it, it, it2data);
9859 for (i = -dvpos; i > 0; --i)
9861 back_to_previous_visible_line_start (it);
9862 it->vpos--;
9864 reseat_1 (it, it->current.pos, true);
9866 else
9867 RESTORE_IT (it, it, it2data);
9871 /* Return true if IT points into the middle of a display vector. */
9873 bool
9874 in_display_vector_p (struct it *it)
9876 return (it->method == GET_FROM_DISPLAY_VECTOR
9877 && it->current.dpvec_index > 0
9878 && it->dpvec + it->current.dpvec_index != it->dpend);
9881 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9882 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9883 WINDOW must be a live window and defaults to the selected one. The
9884 return value is a cons of the maximum pixel-width of any text line and
9885 the maximum pixel-height of all text lines.
9887 The optional argument FROM, if non-nil, specifies the first text
9888 position and defaults to the minimum accessible position of the buffer.
9889 If FROM is t, use the minimum accessible position that starts a
9890 non-empty line. TO, if non-nil, specifies the last text position and
9891 defaults to the maximum accessible position of the buffer. If TO is t,
9892 use the maximum accessible position that ends a non-empty line.
9894 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9895 width that can be returned. X-LIMIT nil or omitted, means to use the
9896 pixel-width of WINDOW's body; use this if you want to know how high
9897 WINDOW should be become in order to fit all of its buffer's text with
9898 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9899 if you intend to change WINDOW's width. In any case, text whose
9900 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9901 of long lines can take some time, it's always a good idea to make this
9902 argument as small as possible; in particular, if the buffer contains
9903 long lines that shall be truncated anyway.
9905 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9906 height (excluding the height of the mode- or header-line, if any) that
9907 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9908 ignored. Since calculating the text height of a large buffer can take
9909 some time, it makes sense to specify this argument if the size of the
9910 buffer is large or unknown.
9912 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9913 include the height of the mode- or header-line of WINDOW in the return
9914 value. If it is either the symbol `mode-line' or `header-line', include
9915 only the height of that line, if present, in the return value. If t,
9916 include the height of both, if present, in the return value. */)
9917 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9918 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9920 struct window *w = decode_live_window (window);
9921 Lisp_Object buffer = w->contents;
9922 struct buffer *b;
9923 struct it it;
9924 struct buffer *old_b = NULL;
9925 ptrdiff_t start, end, pos;
9926 struct text_pos startp;
9927 void *itdata = NULL;
9928 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9930 CHECK_BUFFER (buffer);
9931 b = XBUFFER (buffer);
9933 if (b != current_buffer)
9935 old_b = current_buffer;
9936 set_buffer_internal (b);
9939 if (NILP (from))
9940 start = BEGV;
9941 else if (EQ (from, Qt))
9943 start = pos = BEGV;
9944 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9945 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9946 start = pos;
9947 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9948 start = pos;
9950 else
9952 CHECK_NUMBER_COERCE_MARKER (from);
9953 start = min (max (XINT (from), BEGV), ZV);
9956 if (NILP (to))
9957 end = ZV;
9958 else if (EQ (to, Qt))
9960 end = pos = ZV;
9961 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9962 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9963 end = pos;
9964 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9965 end = pos;
9967 else
9969 CHECK_NUMBER_COERCE_MARKER (to);
9970 end = max (start, min (XINT (to), ZV));
9973 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9974 max_x = XINT (x_limit);
9976 if (NILP (y_limit))
9977 max_y = INT_MAX;
9978 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9979 max_y = XINT (y_limit);
9981 itdata = bidi_shelve_cache ();
9982 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9983 start_display (&it, w, startp);
9985 if (NILP (x_limit))
9986 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9987 else
9989 it.last_visible_x = max_x;
9990 /* Actually, we never want move_it_to stop at to_x. But to make
9991 sure that move_it_in_display_line_to always moves far enough,
9992 we set it to INT_MAX and specify MOVE_TO_X. */
9993 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9994 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9995 /* Don't return more than X-LIMIT. */
9996 if (x > max_x)
9997 x = max_x;
10000 /* Subtract height of header-line which was counted automatically by
10001 start_display. */
10002 y = it.current_y + it.max_ascent + it.max_descent
10003 - WINDOW_HEADER_LINE_HEIGHT (w);
10004 /* Don't return more than Y-LIMIT. */
10005 if (y > max_y)
10006 y = max_y;
10008 if (EQ (mode_and_header_line, Qheader_line)
10009 || EQ (mode_and_header_line, Qt))
10010 /* Re-add height of header-line as requested. */
10011 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10013 if (EQ (mode_and_header_line, Qmode_line)
10014 || EQ (mode_and_header_line, Qt))
10015 /* Add height of mode-line as requested. */
10016 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10018 bidi_unshelve_cache (itdata, false);
10020 if (old_b)
10021 set_buffer_internal (old_b);
10023 return Fcons (make_number (x), make_number (y));
10026 /***********************************************************************
10027 Messages
10028 ***********************************************************************/
10030 /* Return the number of arguments the format string FORMAT needs. */
10032 static ptrdiff_t
10033 format_nargs (char const *format)
10035 ptrdiff_t nargs = 0;
10036 for (char const *p = format; (p = strchr (p, '%')); p++)
10037 if (p[1] == '%')
10038 p++;
10039 else
10040 nargs++;
10041 return nargs;
10044 /* Add a message with format string FORMAT and formatted arguments
10045 to *Messages*. */
10047 void
10048 add_to_log (const char *format, ...)
10050 va_list ap;
10051 va_start (ap, format);
10052 vadd_to_log (format, ap);
10053 va_end (ap);
10056 void
10057 vadd_to_log (char const *format, va_list ap)
10059 ptrdiff_t form_nargs = format_nargs (format);
10060 ptrdiff_t nargs = 1 + form_nargs;
10061 Lisp_Object args[10];
10062 eassert (nargs <= ARRAYELTS (args));
10063 AUTO_STRING (args0, format);
10064 args[0] = args0;
10065 for (ptrdiff_t i = 1; i <= nargs; i++)
10066 args[i] = va_arg (ap, Lisp_Object);
10067 Lisp_Object msg = Qnil;
10068 msg = Fformat_message (nargs, args);
10070 ptrdiff_t len = SBYTES (msg) + 1;
10071 USE_SAFE_ALLOCA;
10072 char *buffer = SAFE_ALLOCA (len);
10073 memcpy (buffer, SDATA (msg), len);
10075 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10076 SAFE_FREE ();
10080 /* Output a newline in the *Messages* buffer if "needs" one. */
10082 void
10083 message_log_maybe_newline (void)
10085 if (message_log_need_newline)
10086 message_dolog ("", 0, true, false);
10090 /* Add a string M of length NBYTES to the message log, optionally
10091 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10092 true, means interpret the contents of M as multibyte. This
10093 function calls low-level routines in order to bypass text property
10094 hooks, etc. which might not be safe to run.
10096 This may GC (insert may run before/after change hooks),
10097 so the buffer M must NOT point to a Lisp string. */
10099 void
10100 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10102 const unsigned char *msg = (const unsigned char *) m;
10104 if (!NILP (Vmemory_full))
10105 return;
10107 if (!NILP (Vmessage_log_max))
10109 struct buffer *oldbuf;
10110 Lisp_Object oldpoint, oldbegv, oldzv;
10111 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10112 ptrdiff_t point_at_end = 0;
10113 ptrdiff_t zv_at_end = 0;
10114 Lisp_Object old_deactivate_mark;
10116 old_deactivate_mark = Vdeactivate_mark;
10117 oldbuf = current_buffer;
10119 /* Ensure the Messages buffer exists, and switch to it.
10120 If we created it, set the major-mode. */
10121 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10122 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10123 if (newbuffer
10124 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10125 call0 (intern ("messages-buffer-mode"));
10127 bset_undo_list (current_buffer, Qt);
10128 bset_cache_long_scans (current_buffer, Qnil);
10130 oldpoint = message_dolog_marker1;
10131 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10132 oldbegv = message_dolog_marker2;
10133 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10134 oldzv = message_dolog_marker3;
10135 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10137 if (PT == Z)
10138 point_at_end = 1;
10139 if (ZV == Z)
10140 zv_at_end = 1;
10142 BEGV = BEG;
10143 BEGV_BYTE = BEG_BYTE;
10144 ZV = Z;
10145 ZV_BYTE = Z_BYTE;
10146 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10148 /* Insert the string--maybe converting multibyte to single byte
10149 or vice versa, so that all the text fits the buffer. */
10150 if (multibyte
10151 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10153 ptrdiff_t i;
10154 int c, char_bytes;
10155 char work[1];
10157 /* Convert a multibyte string to single-byte
10158 for the *Message* buffer. */
10159 for (i = 0; i < nbytes; i += char_bytes)
10161 c = string_char_and_length (msg + i, &char_bytes);
10162 work[0] = CHAR_TO_BYTE8 (c);
10163 insert_1_both (work, 1, 1, true, false, false);
10166 else if (! multibyte
10167 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10169 ptrdiff_t i;
10170 int c, char_bytes;
10171 unsigned char str[MAX_MULTIBYTE_LENGTH];
10172 /* Convert a single-byte string to multibyte
10173 for the *Message* buffer. */
10174 for (i = 0; i < nbytes; i++)
10176 c = msg[i];
10177 MAKE_CHAR_MULTIBYTE (c);
10178 char_bytes = CHAR_STRING (c, str);
10179 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10182 else if (nbytes)
10183 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10184 true, false, false);
10186 if (nlflag)
10188 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10189 printmax_t dups;
10191 insert_1_both ("\n", 1, 1, true, false, false);
10193 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10194 this_bol = PT;
10195 this_bol_byte = PT_BYTE;
10197 /* See if this line duplicates the previous one.
10198 If so, combine duplicates. */
10199 if (this_bol > BEG)
10201 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10202 prev_bol = PT;
10203 prev_bol_byte = PT_BYTE;
10205 dups = message_log_check_duplicate (prev_bol_byte,
10206 this_bol_byte);
10207 if (dups)
10209 del_range_both (prev_bol, prev_bol_byte,
10210 this_bol, this_bol_byte, false);
10211 if (dups > 1)
10213 char dupstr[sizeof " [ times]"
10214 + INT_STRLEN_BOUND (printmax_t)];
10216 /* If you change this format, don't forget to also
10217 change message_log_check_duplicate. */
10218 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10220 insert_1_both (dupstr, duplen, duplen,
10221 true, false, true);
10226 /* If we have more than the desired maximum number of lines
10227 in the *Messages* buffer now, delete the oldest ones.
10228 This is safe because we don't have undo in this buffer. */
10230 if (NATNUMP (Vmessage_log_max))
10232 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10233 -XFASTINT (Vmessage_log_max) - 1, false);
10234 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10237 BEGV = marker_position (oldbegv);
10238 BEGV_BYTE = marker_byte_position (oldbegv);
10240 if (zv_at_end)
10242 ZV = Z;
10243 ZV_BYTE = Z_BYTE;
10245 else
10247 ZV = marker_position (oldzv);
10248 ZV_BYTE = marker_byte_position (oldzv);
10251 if (point_at_end)
10252 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10253 else
10254 /* We can't do Fgoto_char (oldpoint) because it will run some
10255 Lisp code. */
10256 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10257 marker_byte_position (oldpoint));
10259 unchain_marker (XMARKER (oldpoint));
10260 unchain_marker (XMARKER (oldbegv));
10261 unchain_marker (XMARKER (oldzv));
10263 /* We called insert_1_both above with its 5th argument (PREPARE)
10264 false, which prevents insert_1_both from calling
10265 prepare_to_modify_buffer, which in turns prevents us from
10266 incrementing windows_or_buffers_changed even if *Messages* is
10267 shown in some window. So we must manually set
10268 windows_or_buffers_changed here to make up for that. */
10269 windows_or_buffers_changed = old_windows_or_buffers_changed;
10270 bset_redisplay (current_buffer);
10272 set_buffer_internal (oldbuf);
10274 message_log_need_newline = !nlflag;
10275 Vdeactivate_mark = old_deactivate_mark;
10280 /* We are at the end of the buffer after just having inserted a newline.
10281 (Note: We depend on the fact we won't be crossing the gap.)
10282 Check to see if the most recent message looks a lot like the previous one.
10283 Return 0 if different, 1 if the new one should just replace it, or a
10284 value N > 1 if we should also append " [N times]". */
10286 static intmax_t
10287 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10289 ptrdiff_t i;
10290 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10291 bool seen_dots = false;
10292 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10293 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10295 for (i = 0; i < len; i++)
10297 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10298 seen_dots = true;
10299 if (p1[i] != p2[i])
10300 return seen_dots;
10302 p1 += len;
10303 if (*p1 == '\n')
10304 return 2;
10305 if (*p1++ == ' ' && *p1++ == '[')
10307 char *pend;
10308 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10309 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10310 return n + 1;
10312 return 0;
10316 /* Display an echo area message M with a specified length of NBYTES
10317 bytes. The string may include null characters. If M is not a
10318 string, clear out any existing message, and let the mini-buffer
10319 text show through.
10321 This function cancels echoing. */
10323 void
10324 message3 (Lisp_Object m)
10326 clear_message (true, true);
10327 cancel_echoing ();
10329 /* First flush out any partial line written with print. */
10330 message_log_maybe_newline ();
10331 if (STRINGP (m))
10333 ptrdiff_t nbytes = SBYTES (m);
10334 bool multibyte = STRING_MULTIBYTE (m);
10335 char *buffer;
10336 USE_SAFE_ALLOCA;
10337 SAFE_ALLOCA_STRING (buffer, m);
10338 message_dolog (buffer, nbytes, true, multibyte);
10339 SAFE_FREE ();
10341 if (! inhibit_message)
10342 message3_nolog (m);
10345 /* Log the message M to stderr. Log an empty line if M is not a string. */
10347 static void
10348 message_to_stderr (Lisp_Object m)
10350 if (noninteractive_need_newline)
10352 noninteractive_need_newline = false;
10353 fputc ('\n', stderr);
10355 if (STRINGP (m))
10357 Lisp_Object coding_system = Vlocale_coding_system;
10358 Lisp_Object s;
10360 if (!NILP (Vcoding_system_for_write))
10361 coding_system = Vcoding_system_for_write;
10362 if (!NILP (coding_system))
10363 s = code_convert_string_norecord (m, coding_system, true);
10364 else
10365 s = m;
10367 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10369 if (!cursor_in_echo_area)
10370 fputc ('\n', stderr);
10371 fflush (stderr);
10374 /* The non-logging version of message3.
10375 This does not cancel echoing, because it is used for echoing.
10376 Perhaps we need to make a separate function for echoing
10377 and make this cancel echoing. */
10379 void
10380 message3_nolog (Lisp_Object m)
10382 struct frame *sf = SELECTED_FRAME ();
10384 if (FRAME_INITIAL_P (sf))
10385 message_to_stderr (m);
10386 /* Error messages get reported properly by cmd_error, so this must be just an
10387 informative message; if the frame hasn't really been initialized yet, just
10388 toss it. */
10389 else if (INTERACTIVE && sf->glyphs_initialized_p)
10391 /* Get the frame containing the mini-buffer
10392 that the selected frame is using. */
10393 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10394 Lisp_Object frame = XWINDOW (mini_window)->frame;
10395 struct frame *f = XFRAME (frame);
10397 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10398 Fmake_frame_visible (frame);
10400 if (STRINGP (m) && SCHARS (m) > 0)
10402 set_message (m);
10403 if (minibuffer_auto_raise)
10404 Fraise_frame (frame);
10405 /* Assume we are not echoing.
10406 (If we are, echo_now will override this.) */
10407 echo_message_buffer = Qnil;
10409 else
10410 clear_message (true, true);
10412 do_pending_window_change (false);
10413 echo_area_display (true);
10414 do_pending_window_change (false);
10415 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10416 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10421 /* Display a null-terminated echo area message M. If M is 0, clear
10422 out any existing message, and let the mini-buffer text show through.
10424 The buffer M must continue to exist until after the echo area gets
10425 cleared or some other message gets displayed there. Do not pass
10426 text that is stored in a Lisp string. Do not pass text in a buffer
10427 that was alloca'd. */
10429 void
10430 message1 (const char *m)
10432 message3 (m ? build_unibyte_string (m) : Qnil);
10436 /* The non-logging counterpart of message1. */
10438 void
10439 message1_nolog (const char *m)
10441 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10444 /* Display a message M which contains a single %s
10445 which gets replaced with STRING. */
10447 void
10448 message_with_string (const char *m, Lisp_Object string, bool log)
10450 CHECK_STRING (string);
10452 bool need_message;
10453 if (noninteractive)
10454 need_message = !!m;
10455 else if (!INTERACTIVE)
10456 need_message = false;
10457 else
10459 /* The frame whose minibuffer we're going to display the message on.
10460 It may be larger than the selected frame, so we need
10461 to use its buffer, not the selected frame's buffer. */
10462 Lisp_Object mini_window;
10463 struct frame *f, *sf = SELECTED_FRAME ();
10465 /* Get the frame containing the minibuffer
10466 that the selected frame is using. */
10467 mini_window = FRAME_MINIBUF_WINDOW (sf);
10468 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10470 /* Error messages get reported properly by cmd_error, so this must be
10471 just an informative message; if the frame hasn't really been
10472 initialized yet, just toss it. */
10473 need_message = f->glyphs_initialized_p;
10476 if (need_message)
10478 AUTO_STRING (fmt, m);
10479 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10481 if (noninteractive)
10482 message_to_stderr (msg);
10483 else
10485 if (log)
10486 message3 (msg);
10487 else
10488 message3_nolog (msg);
10490 /* Print should start at the beginning of the message
10491 buffer next time. */
10492 message_buf_print = false;
10498 /* Dump an informative message to the minibuf. If M is 0, clear out
10499 any existing message, and let the mini-buffer text show through.
10501 The message must be safe ASCII and the format must not contain ` or
10502 '. If your message and format do not fit into this category,
10503 convert your arguments to Lisp objects and use Fmessage instead. */
10505 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10506 vmessage (const char *m, va_list ap)
10508 if (noninteractive)
10510 if (m)
10512 if (noninteractive_need_newline)
10513 putc ('\n', stderr);
10514 noninteractive_need_newline = false;
10515 vfprintf (stderr, m, ap);
10516 if (!cursor_in_echo_area)
10517 fprintf (stderr, "\n");
10518 fflush (stderr);
10521 else if (INTERACTIVE)
10523 /* The frame whose mini-buffer we're going to display the message
10524 on. It may be larger than the selected frame, so we need to
10525 use its buffer, not the selected frame's buffer. */
10526 Lisp_Object mini_window;
10527 struct frame *f, *sf = SELECTED_FRAME ();
10529 /* Get the frame containing the mini-buffer
10530 that the selected frame is using. */
10531 mini_window = FRAME_MINIBUF_WINDOW (sf);
10532 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10534 /* Error messages get reported properly by cmd_error, so this must be
10535 just an informative message; if the frame hasn't really been
10536 initialized yet, just toss it. */
10537 if (f->glyphs_initialized_p)
10539 if (m)
10541 ptrdiff_t len;
10542 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10543 USE_SAFE_ALLOCA;
10544 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10546 len = doprnt (message_buf, maxsize, m, 0, ap);
10548 message3 (make_string (message_buf, len));
10549 SAFE_FREE ();
10551 else
10552 message1 (0);
10554 /* Print should start at the beginning of the message
10555 buffer next time. */
10556 message_buf_print = false;
10561 void
10562 message (const char *m, ...)
10564 va_list ap;
10565 va_start (ap, m);
10566 vmessage (m, ap);
10567 va_end (ap);
10571 /* Display the current message in the current mini-buffer. This is
10572 only called from error handlers in process.c, and is not time
10573 critical. */
10575 void
10576 update_echo_area (void)
10578 if (!NILP (echo_area_buffer[0]))
10580 Lisp_Object string;
10581 string = Fcurrent_message ();
10582 message3 (string);
10587 /* Make sure echo area buffers in `echo_buffers' are live.
10588 If they aren't, make new ones. */
10590 static void
10591 ensure_echo_area_buffers (void)
10593 for (int i = 0; i < 2; i++)
10594 if (!BUFFERP (echo_buffer[i])
10595 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10597 Lisp_Object old_buffer = echo_buffer[i];
10598 static char const name_fmt[] = " *Echo Area %d*";
10599 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10600 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10601 echo_buffer[i] = Fget_buffer_create (lname);
10602 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10603 /* to force word wrap in echo area -
10604 it was decided to postpone this*/
10605 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10607 for (int j = 0; j < 2; j++)
10608 if (EQ (old_buffer, echo_area_buffer[j]))
10609 echo_area_buffer[j] = echo_buffer[i];
10614 /* Call FN with args A1..A2 with either the current or last displayed
10615 echo_area_buffer as current buffer.
10617 WHICH zero means use the current message buffer
10618 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10619 from echo_buffer[] and clear it.
10621 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10622 suitable buffer from echo_buffer[] and clear it.
10624 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10625 that the current message becomes the last displayed one, choose a
10626 suitable buffer for echo_area_buffer[0], and clear it.
10628 Value is what FN returns. */
10630 static bool
10631 with_echo_area_buffer (struct window *w, int which,
10632 bool (*fn) (ptrdiff_t, Lisp_Object),
10633 ptrdiff_t a1, Lisp_Object a2)
10635 Lisp_Object buffer;
10636 bool this_one, the_other, clear_buffer_p, rc;
10637 ptrdiff_t count = SPECPDL_INDEX ();
10639 /* If buffers aren't live, make new ones. */
10640 ensure_echo_area_buffers ();
10642 clear_buffer_p = false;
10644 if (which == 0)
10645 this_one = false, the_other = true;
10646 else if (which > 0)
10647 this_one = true, the_other = false;
10648 else
10650 this_one = false, the_other = true;
10651 clear_buffer_p = true;
10653 /* We need a fresh one in case the current echo buffer equals
10654 the one containing the last displayed echo area message. */
10655 if (!NILP (echo_area_buffer[this_one])
10656 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10657 echo_area_buffer[this_one] = Qnil;
10660 /* Choose a suitable buffer from echo_buffer[] if we don't
10661 have one. */
10662 if (NILP (echo_area_buffer[this_one]))
10664 echo_area_buffer[this_one]
10665 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10666 ? echo_buffer[the_other]
10667 : echo_buffer[this_one]);
10668 clear_buffer_p = true;
10671 buffer = echo_area_buffer[this_one];
10673 /* Don't get confused by reusing the buffer used for echoing
10674 for a different purpose. */
10675 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10676 cancel_echoing ();
10678 record_unwind_protect (unwind_with_echo_area_buffer,
10679 with_echo_area_buffer_unwind_data (w));
10681 /* Make the echo area buffer current. Note that for display
10682 purposes, it is not necessary that the displayed window's buffer
10683 == current_buffer, except for text property lookup. So, let's
10684 only set that buffer temporarily here without doing a full
10685 Fset_window_buffer. We must also change w->pointm, though,
10686 because otherwise an assertions in unshow_buffer fails, and Emacs
10687 aborts. */
10688 set_buffer_internal_1 (XBUFFER (buffer));
10689 if (w)
10691 wset_buffer (w, buffer);
10692 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10693 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10696 bset_undo_list (current_buffer, Qt);
10697 bset_read_only (current_buffer, Qnil);
10698 specbind (Qinhibit_read_only, Qt);
10699 specbind (Qinhibit_modification_hooks, Qt);
10701 if (clear_buffer_p && Z > BEG)
10702 del_range (BEG, Z);
10704 eassert (BEGV >= BEG);
10705 eassert (ZV <= Z && ZV >= BEGV);
10707 rc = fn (a1, a2);
10709 eassert (BEGV >= BEG);
10710 eassert (ZV <= Z && ZV >= BEGV);
10712 unbind_to (count, Qnil);
10713 return rc;
10717 /* Save state that should be preserved around the call to the function
10718 FN called in with_echo_area_buffer. */
10720 static Lisp_Object
10721 with_echo_area_buffer_unwind_data (struct window *w)
10723 int i = 0;
10724 Lisp_Object vector, tmp;
10726 /* Reduce consing by keeping one vector in
10727 Vwith_echo_area_save_vector. */
10728 vector = Vwith_echo_area_save_vector;
10729 Vwith_echo_area_save_vector = Qnil;
10731 if (NILP (vector))
10732 vector = Fmake_vector (make_number (11), Qnil);
10734 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10735 ASET (vector, i, Vdeactivate_mark); ++i;
10736 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10738 if (w)
10740 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10741 ASET (vector, i, w->contents); ++i;
10742 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10743 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10744 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10745 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10746 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10747 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10749 else
10751 int end = i + 8;
10752 for (; i < end; ++i)
10753 ASET (vector, i, Qnil);
10756 eassert (i == ASIZE (vector));
10757 return vector;
10761 /* Restore global state from VECTOR which was created by
10762 with_echo_area_buffer_unwind_data. */
10764 static void
10765 unwind_with_echo_area_buffer (Lisp_Object vector)
10767 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10768 Vdeactivate_mark = AREF (vector, 1);
10769 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10771 if (WINDOWP (AREF (vector, 3)))
10773 struct window *w;
10774 Lisp_Object buffer;
10776 w = XWINDOW (AREF (vector, 3));
10777 buffer = AREF (vector, 4);
10779 wset_buffer (w, buffer);
10780 set_marker_both (w->pointm, buffer,
10781 XFASTINT (AREF (vector, 5)),
10782 XFASTINT (AREF (vector, 6)));
10783 set_marker_both (w->old_pointm, buffer,
10784 XFASTINT (AREF (vector, 7)),
10785 XFASTINT (AREF (vector, 8)));
10786 set_marker_both (w->start, buffer,
10787 XFASTINT (AREF (vector, 9)),
10788 XFASTINT (AREF (vector, 10)));
10791 Vwith_echo_area_save_vector = vector;
10795 /* Set up the echo area for use by print functions. MULTIBYTE_P
10796 means we will print multibyte. */
10798 void
10799 setup_echo_area_for_printing (bool multibyte_p)
10801 /* If we can't find an echo area any more, exit. */
10802 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10803 Fkill_emacs (Qnil);
10805 ensure_echo_area_buffers ();
10807 if (!message_buf_print)
10809 /* A message has been output since the last time we printed.
10810 Choose a fresh echo area buffer. */
10811 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10812 echo_area_buffer[0] = echo_buffer[1];
10813 else
10814 echo_area_buffer[0] = echo_buffer[0];
10816 /* Switch to that buffer and clear it. */
10817 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10818 bset_truncate_lines (current_buffer, Qnil);
10820 if (Z > BEG)
10822 ptrdiff_t count = SPECPDL_INDEX ();
10823 specbind (Qinhibit_read_only, Qt);
10824 /* Note that undo recording is always disabled. */
10825 del_range (BEG, Z);
10826 unbind_to (count, Qnil);
10828 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10830 /* Set up the buffer for the multibyteness we need. */
10831 if (multibyte_p
10832 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10833 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10835 /* Raise the frame containing the echo area. */
10836 if (minibuffer_auto_raise)
10838 struct frame *sf = SELECTED_FRAME ();
10839 Lisp_Object mini_window;
10840 mini_window = FRAME_MINIBUF_WINDOW (sf);
10841 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10844 message_log_maybe_newline ();
10845 message_buf_print = true;
10847 else
10849 if (NILP (echo_area_buffer[0]))
10851 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10852 echo_area_buffer[0] = echo_buffer[1];
10853 else
10854 echo_area_buffer[0] = echo_buffer[0];
10857 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10859 /* Someone switched buffers between print requests. */
10860 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10861 bset_truncate_lines (current_buffer, Qnil);
10867 /* Display an echo area message in window W. Value is true if W's
10868 height is changed. If display_last_displayed_message_p,
10869 display the message that was last displayed, otherwise
10870 display the current message. */
10872 static bool
10873 display_echo_area (struct window *w)
10875 bool no_message_p, window_height_changed_p;
10877 /* Temporarily disable garbage collections while displaying the echo
10878 area. This is done because a GC can print a message itself.
10879 That message would modify the echo area buffer's contents while a
10880 redisplay of the buffer is going on, and seriously confuse
10881 redisplay. */
10882 ptrdiff_t count = inhibit_garbage_collection ();
10884 /* If there is no message, we must call display_echo_area_1
10885 nevertheless because it resizes the window. But we will have to
10886 reset the echo_area_buffer in question to nil at the end because
10887 with_echo_area_buffer will sets it to an empty buffer. */
10888 bool i = display_last_displayed_message_p;
10889 /* According to the C99, C11 and C++11 standards, the integral value
10890 of a "bool" is always 0 or 1, so this array access is safe here,
10891 if oddly typed. */
10892 no_message_p = NILP (echo_area_buffer[i]);
10894 window_height_changed_p
10895 = with_echo_area_buffer (w, display_last_displayed_message_p,
10896 display_echo_area_1,
10897 (intptr_t) w, Qnil);
10899 if (no_message_p)
10900 echo_area_buffer[i] = Qnil;
10902 unbind_to (count, Qnil);
10903 return window_height_changed_p;
10907 /* Helper for display_echo_area. Display the current buffer which
10908 contains the current echo area message in window W, a mini-window,
10909 a pointer to which is passed in A1. A2..A4 are currently not used.
10910 Change the height of W so that all of the message is displayed.
10911 Value is true if height of W was changed. */
10913 static bool
10914 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10916 intptr_t i1 = a1;
10917 struct window *w = (struct window *) i1;
10918 Lisp_Object window;
10919 struct text_pos start;
10921 /* We are about to enter redisplay without going through
10922 redisplay_internal, so we need to forget these faces by hand
10923 here. */
10924 forget_escape_and_glyphless_faces ();
10926 /* Do this before displaying, so that we have a large enough glyph
10927 matrix for the display. If we can't get enough space for the
10928 whole text, display the last N lines. That works by setting w->start. */
10929 bool window_height_changed_p = resize_mini_window (w, false);
10931 /* Use the starting position chosen by resize_mini_window. */
10932 SET_TEXT_POS_FROM_MARKER (start, w->start);
10934 /* Display. */
10935 clear_glyph_matrix (w->desired_matrix);
10936 XSETWINDOW (window, w);
10937 try_window (window, start, 0);
10939 return window_height_changed_p;
10943 /* Resize the echo area window to exactly the size needed for the
10944 currently displayed message, if there is one. If a mini-buffer
10945 is active, don't shrink it. */
10947 void
10948 resize_echo_area_exactly (void)
10950 if (BUFFERP (echo_area_buffer[0])
10951 && WINDOWP (echo_area_window))
10953 struct window *w = XWINDOW (echo_area_window);
10954 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10955 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10956 (intptr_t) w, resize_exactly);
10957 if (resized_p)
10959 windows_or_buffers_changed = 42;
10960 update_mode_lines = 30;
10961 redisplay_internal ();
10967 /* Callback function for with_echo_area_buffer, when used from
10968 resize_echo_area_exactly. A1 contains a pointer to the window to
10969 resize, EXACTLY non-nil means resize the mini-window exactly to the
10970 size of the text displayed. A3 and A4 are not used. Value is what
10971 resize_mini_window returns. */
10973 static bool
10974 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10976 intptr_t i1 = a1;
10977 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10981 /* Resize mini-window W to fit the size of its contents. EXACT_P
10982 means size the window exactly to the size needed. Otherwise, it's
10983 only enlarged until W's buffer is empty.
10985 Set W->start to the right place to begin display. If the whole
10986 contents fit, start at the beginning. Otherwise, start so as
10987 to make the end of the contents appear. This is particularly
10988 important for y-or-n-p, but seems desirable generally.
10990 Value is true if the window height has been changed. */
10992 bool
10993 resize_mini_window (struct window *w, bool exact_p)
10995 struct frame *f = XFRAME (w->frame);
10996 bool window_height_changed_p = false;
10998 eassert (MINI_WINDOW_P (w));
11000 /* By default, start display at the beginning. */
11001 set_marker_both (w->start, w->contents,
11002 BUF_BEGV (XBUFFER (w->contents)),
11003 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11005 /* Don't resize windows while redisplaying a window; it would
11006 confuse redisplay functions when the size of the window they are
11007 displaying changes from under them. Such a resizing can happen,
11008 for instance, when which-func prints a long message while
11009 we are running fontification-functions. We're running these
11010 functions with safe_call which binds inhibit-redisplay to t. */
11011 if (!NILP (Vinhibit_redisplay))
11012 return false;
11014 /* Nil means don't try to resize. */
11015 if (NILP (Vresize_mini_windows)
11016 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11017 return false;
11019 if (!FRAME_MINIBUF_ONLY_P (f))
11021 struct it it;
11022 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11023 + WINDOW_PIXEL_HEIGHT (w));
11024 int unit = FRAME_LINE_HEIGHT (f);
11025 int height, max_height;
11026 struct text_pos start;
11027 struct buffer *old_current_buffer = NULL;
11029 if (current_buffer != XBUFFER (w->contents))
11031 old_current_buffer = current_buffer;
11032 set_buffer_internal (XBUFFER (w->contents));
11035 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11037 /* Compute the max. number of lines specified by the user. */
11038 if (FLOATP (Vmax_mini_window_height))
11039 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11040 else if (INTEGERP (Vmax_mini_window_height))
11041 max_height = XINT (Vmax_mini_window_height) * unit;
11042 else
11043 max_height = total_height / 4;
11045 /* Correct that max. height if it's bogus. */
11046 max_height = clip_to_bounds (unit, max_height, total_height);
11048 /* Find out the height of the text in the window. */
11049 if (it.line_wrap == TRUNCATE)
11050 height = unit;
11051 else
11053 last_height = 0;
11054 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11055 if (it.max_ascent == 0 && it.max_descent == 0)
11056 height = it.current_y + last_height;
11057 else
11058 height = it.current_y + it.max_ascent + it.max_descent;
11059 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11062 /* Compute a suitable window start. */
11063 if (height > max_height)
11065 height = (max_height / unit) * unit;
11066 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11067 move_it_vertically_backward (&it, height - unit);
11068 start = it.current.pos;
11070 else
11071 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11072 SET_MARKER_FROM_TEXT_POS (w->start, start);
11074 if (EQ (Vresize_mini_windows, Qgrow_only))
11076 /* Let it grow only, until we display an empty message, in which
11077 case the window shrinks again. */
11078 if (height > WINDOW_PIXEL_HEIGHT (w))
11080 int old_height = WINDOW_PIXEL_HEIGHT (w);
11082 FRAME_WINDOWS_FROZEN (f) = true;
11083 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11084 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11086 else if (height < WINDOW_PIXEL_HEIGHT (w)
11087 && (exact_p || BEGV == ZV))
11089 int old_height = WINDOW_PIXEL_HEIGHT (w);
11091 FRAME_WINDOWS_FROZEN (f) = false;
11092 shrink_mini_window (w, true);
11093 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11096 else
11098 /* Always resize to exact size needed. */
11099 if (height > WINDOW_PIXEL_HEIGHT (w))
11101 int old_height = WINDOW_PIXEL_HEIGHT (w);
11103 FRAME_WINDOWS_FROZEN (f) = true;
11104 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11105 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11107 else if (height < WINDOW_PIXEL_HEIGHT (w))
11109 int old_height = WINDOW_PIXEL_HEIGHT (w);
11111 FRAME_WINDOWS_FROZEN (f) = false;
11112 shrink_mini_window (w, true);
11114 if (height)
11116 FRAME_WINDOWS_FROZEN (f) = true;
11117 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11120 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11124 if (old_current_buffer)
11125 set_buffer_internal (old_current_buffer);
11128 return window_height_changed_p;
11132 /* Value is the current message, a string, or nil if there is no
11133 current message. */
11135 Lisp_Object
11136 current_message (void)
11138 Lisp_Object msg;
11140 if (!BUFFERP (echo_area_buffer[0]))
11141 msg = Qnil;
11142 else
11144 with_echo_area_buffer (0, 0, current_message_1,
11145 (intptr_t) &msg, Qnil);
11146 if (NILP (msg))
11147 echo_area_buffer[0] = Qnil;
11150 return msg;
11154 static bool
11155 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11157 intptr_t i1 = a1;
11158 Lisp_Object *msg = (Lisp_Object *) i1;
11160 if (Z > BEG)
11161 *msg = make_buffer_string (BEG, Z, true);
11162 else
11163 *msg = Qnil;
11164 return false;
11168 /* Push the current message on Vmessage_stack for later restoration
11169 by restore_message. Value is true if the current message isn't
11170 empty. This is a relatively infrequent operation, so it's not
11171 worth optimizing. */
11173 bool
11174 push_message (void)
11176 Lisp_Object msg = current_message ();
11177 Vmessage_stack = Fcons (msg, Vmessage_stack);
11178 return STRINGP (msg);
11182 /* Restore message display from the top of Vmessage_stack. */
11184 void
11185 restore_message (void)
11187 eassert (CONSP (Vmessage_stack));
11188 message3_nolog (XCAR (Vmessage_stack));
11192 /* Handler for unwind-protect calling pop_message. */
11194 void
11195 pop_message_unwind (void)
11197 /* Pop the top-most entry off Vmessage_stack. */
11198 eassert (CONSP (Vmessage_stack));
11199 Vmessage_stack = XCDR (Vmessage_stack);
11203 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11204 exits. If the stack is not empty, we have a missing pop_message
11205 somewhere. */
11207 void
11208 check_message_stack (void)
11210 if (!NILP (Vmessage_stack))
11211 emacs_abort ();
11215 /* Truncate to NCHARS what will be displayed in the echo area the next
11216 time we display it---but don't redisplay it now. */
11218 void
11219 truncate_echo_area (ptrdiff_t nchars)
11221 if (nchars == 0)
11222 echo_area_buffer[0] = Qnil;
11223 else if (!noninteractive
11224 && INTERACTIVE
11225 && !NILP (echo_area_buffer[0]))
11227 struct frame *sf = SELECTED_FRAME ();
11228 /* Error messages get reported properly by cmd_error, so this must be
11229 just an informative message; if the frame hasn't really been
11230 initialized yet, just toss it. */
11231 if (sf->glyphs_initialized_p)
11232 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11237 /* Helper function for truncate_echo_area. Truncate the current
11238 message to at most NCHARS characters. */
11240 static bool
11241 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11243 if (BEG + nchars < Z)
11244 del_range (BEG + nchars, Z);
11245 if (Z == BEG)
11246 echo_area_buffer[0] = Qnil;
11247 return false;
11250 /* Set the current message to STRING. */
11252 static void
11253 set_message (Lisp_Object string)
11255 eassert (STRINGP (string));
11257 message_enable_multibyte = STRING_MULTIBYTE (string);
11259 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11260 message_buf_print = false;
11261 help_echo_showing_p = false;
11263 if (STRINGP (Vdebug_on_message)
11264 && STRINGP (string)
11265 && fast_string_match (Vdebug_on_message, string) >= 0)
11266 call_debugger (list2 (Qerror, string));
11270 /* Helper function for set_message. First argument is ignored and second
11271 argument has the same meaning as for set_message.
11272 This function is called with the echo area buffer being current. */
11274 static bool
11275 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11277 eassert (STRINGP (string));
11279 /* Change multibyteness of the echo buffer appropriately. */
11280 if (message_enable_multibyte
11281 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11282 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11284 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11285 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11286 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11288 /* Insert new message at BEG. */
11289 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11291 /* This function takes care of single/multibyte conversion.
11292 We just have to ensure that the echo area buffer has the right
11293 setting of enable_multibyte_characters. */
11294 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11296 return false;
11300 /* Clear messages. CURRENT_P means clear the current message.
11301 LAST_DISPLAYED_P means clear the message last displayed. */
11303 void
11304 clear_message (bool current_p, bool last_displayed_p)
11306 if (current_p)
11308 echo_area_buffer[0] = Qnil;
11309 message_cleared_p = true;
11312 if (last_displayed_p)
11313 echo_area_buffer[1] = Qnil;
11315 message_buf_print = false;
11318 /* Clear garbaged frames.
11320 This function is used where the old redisplay called
11321 redraw_garbaged_frames which in turn called redraw_frame which in
11322 turn called clear_frame. The call to clear_frame was a source of
11323 flickering. I believe a clear_frame is not necessary. It should
11324 suffice in the new redisplay to invalidate all current matrices,
11325 and ensure a complete redisplay of all windows. */
11327 static void
11328 clear_garbaged_frames (void)
11330 if (frame_garbaged)
11332 Lisp_Object tail, frame;
11333 struct frame *sf = SELECTED_FRAME ();
11335 FOR_EACH_FRAME (tail, frame)
11337 struct frame *f = XFRAME (frame);
11339 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11341 if (f->resized_p
11342 /* It makes no sense to redraw a non-selected TTY
11343 frame, since that will actually clear the
11344 selected frame, and might leave the selected
11345 frame with corrupted display, if it happens not
11346 to be marked garbaged. */
11347 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11348 redraw_frame (f);
11349 else
11350 clear_current_matrices (f);
11351 fset_redisplay (f);
11352 f->garbaged = false;
11353 f->resized_p = false;
11357 frame_garbaged = false;
11362 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11363 selected_frame. */
11365 static void
11366 echo_area_display (bool update_frame_p)
11368 Lisp_Object mini_window;
11369 struct window *w;
11370 struct frame *f;
11371 bool window_height_changed_p = false;
11372 struct frame *sf = SELECTED_FRAME ();
11374 mini_window = FRAME_MINIBUF_WINDOW (sf);
11375 w = XWINDOW (mini_window);
11376 f = XFRAME (WINDOW_FRAME (w));
11378 /* Don't display if frame is invisible or not yet initialized. */
11379 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11380 return;
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 /* When Emacs starts, selected_frame may be the initial terminal
11384 frame. If we let this through, a message would be displayed on
11385 the terminal. */
11386 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11387 return;
11388 #endif /* HAVE_WINDOW_SYSTEM */
11390 /* Redraw garbaged frames. */
11391 clear_garbaged_frames ();
11393 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11395 echo_area_window = mini_window;
11396 window_height_changed_p = display_echo_area (w);
11397 w->must_be_updated_p = true;
11399 /* Update the display, unless called from redisplay_internal.
11400 Also don't update the screen during redisplay itself. The
11401 update will happen at the end of redisplay, and an update
11402 here could cause confusion. */
11403 if (update_frame_p && !redisplaying_p)
11405 int n = 0;
11407 /* If the display update has been interrupted by pending
11408 input, update mode lines in the frame. Due to the
11409 pending input, it might have been that redisplay hasn't
11410 been called, so that mode lines above the echo area are
11411 garbaged. This looks odd, so we prevent it here. */
11412 if (!display_completed)
11413 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11415 if (window_height_changed_p
11416 /* Don't do this if Emacs is shutting down. Redisplay
11417 needs to run hooks. */
11418 && !NILP (Vrun_hooks))
11420 /* Must update other windows. Likewise as in other
11421 cases, don't let this update be interrupted by
11422 pending input. */
11423 ptrdiff_t count = SPECPDL_INDEX ();
11424 specbind (Qredisplay_dont_pause, Qt);
11425 fset_redisplay (f);
11426 redisplay_internal ();
11427 unbind_to (count, Qnil);
11429 else if (FRAME_WINDOW_P (f) && n == 0)
11431 /* Window configuration is the same as before.
11432 Can do with a display update of the echo area,
11433 unless we displayed some mode lines. */
11434 update_single_window (w);
11435 flush_frame (f);
11437 else
11438 update_frame (f, true, true);
11440 /* If cursor is in the echo area, make sure that the next
11441 redisplay displays the minibuffer, so that the cursor will
11442 be replaced with what the minibuffer wants. */
11443 if (cursor_in_echo_area)
11444 wset_redisplay (XWINDOW (mini_window));
11447 else if (!EQ (mini_window, selected_window))
11448 wset_redisplay (XWINDOW (mini_window));
11450 /* Last displayed message is now the current message. */
11451 echo_area_buffer[1] = echo_area_buffer[0];
11452 /* Inform read_char that we're not echoing. */
11453 echo_message_buffer = Qnil;
11455 /* Prevent redisplay optimization in redisplay_internal by resetting
11456 this_line_start_pos. This is done because the mini-buffer now
11457 displays the message instead of its buffer text. */
11458 if (EQ (mini_window, selected_window))
11459 CHARPOS (this_line_start_pos) = 0;
11461 if (window_height_changed_p)
11463 fset_redisplay (f);
11465 /* If window configuration was changed, frames may have been
11466 marked garbaged. Clear them or we will experience
11467 surprises wrt scrolling.
11468 FIXME: How/why/when? */
11469 clear_garbaged_frames ();
11473 /* True if W's buffer was changed but not saved. */
11475 static bool
11476 window_buffer_changed (struct window *w)
11478 struct buffer *b = XBUFFER (w->contents);
11480 eassert (BUFFER_LIVE_P (b));
11482 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11485 /* True if W has %c in its mode line and mode line should be updated. */
11487 static bool
11488 mode_line_update_needed (struct window *w)
11490 return (w->column_number_displayed != -1
11491 && !(PT == w->last_point && !window_outdated (w))
11492 && (w->column_number_displayed != current_column ()));
11495 /* True if window start of W is frozen and may not be changed during
11496 redisplay. */
11498 static bool
11499 window_frozen_p (struct window *w)
11501 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11503 Lisp_Object window;
11505 XSETWINDOW (window, w);
11506 if (MINI_WINDOW_P (w))
11507 return false;
11508 else if (EQ (window, selected_window))
11509 return false;
11510 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11511 && EQ (window, Vminibuf_scroll_window))
11512 /* This special window can't be frozen too. */
11513 return false;
11514 else
11515 return true;
11517 return false;
11520 /***********************************************************************
11521 Mode Lines and Frame Titles
11522 ***********************************************************************/
11524 /* A buffer for constructing non-propertized mode-line strings and
11525 frame titles in it; allocated from the heap in init_xdisp and
11526 resized as needed in store_mode_line_noprop_char. */
11528 static char *mode_line_noprop_buf;
11530 /* The buffer's end, and a current output position in it. */
11532 static char *mode_line_noprop_buf_end;
11533 static char *mode_line_noprop_ptr;
11535 #define MODE_LINE_NOPROP_LEN(start) \
11536 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11538 static enum {
11539 MODE_LINE_DISPLAY = 0,
11540 MODE_LINE_TITLE,
11541 MODE_LINE_NOPROP,
11542 MODE_LINE_STRING
11543 } mode_line_target;
11545 /* Alist that caches the results of :propertize.
11546 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11547 static Lisp_Object mode_line_proptrans_alist;
11549 /* List of strings making up the mode-line. */
11550 static Lisp_Object mode_line_string_list;
11552 /* Base face property when building propertized mode line string. */
11553 static Lisp_Object mode_line_string_face;
11554 static Lisp_Object mode_line_string_face_prop;
11557 /* Unwind data for mode line strings */
11559 static Lisp_Object Vmode_line_unwind_vector;
11561 static Lisp_Object
11562 format_mode_line_unwind_data (struct frame *target_frame,
11563 struct buffer *obuf,
11564 Lisp_Object owin,
11565 bool save_proptrans)
11567 Lisp_Object vector, tmp;
11569 /* Reduce consing by keeping one vector in
11570 Vwith_echo_area_save_vector. */
11571 vector = Vmode_line_unwind_vector;
11572 Vmode_line_unwind_vector = Qnil;
11574 if (NILP (vector))
11575 vector = Fmake_vector (make_number (10), Qnil);
11577 ASET (vector, 0, make_number (mode_line_target));
11578 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11579 ASET (vector, 2, mode_line_string_list);
11580 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11581 ASET (vector, 4, mode_line_string_face);
11582 ASET (vector, 5, mode_line_string_face_prop);
11584 if (obuf)
11585 XSETBUFFER (tmp, obuf);
11586 else
11587 tmp = Qnil;
11588 ASET (vector, 6, tmp);
11589 ASET (vector, 7, owin);
11590 if (target_frame)
11592 /* Similarly to `with-selected-window', if the operation selects
11593 a window on another frame, we must restore that frame's
11594 selected window, and (for a tty) the top-frame. */
11595 ASET (vector, 8, target_frame->selected_window);
11596 if (FRAME_TERMCAP_P (target_frame))
11597 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11600 return vector;
11603 static void
11604 unwind_format_mode_line (Lisp_Object vector)
11606 Lisp_Object old_window = AREF (vector, 7);
11607 Lisp_Object target_frame_window = AREF (vector, 8);
11608 Lisp_Object old_top_frame = AREF (vector, 9);
11610 mode_line_target = XINT (AREF (vector, 0));
11611 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11612 mode_line_string_list = AREF (vector, 2);
11613 if (! EQ (AREF (vector, 3), Qt))
11614 mode_line_proptrans_alist = AREF (vector, 3);
11615 mode_line_string_face = AREF (vector, 4);
11616 mode_line_string_face_prop = AREF (vector, 5);
11618 /* Select window before buffer, since it may change the buffer. */
11619 if (!NILP (old_window))
11621 /* If the operation that we are unwinding had selected a window
11622 on a different frame, reset its frame-selected-window. For a
11623 text terminal, reset its top-frame if necessary. */
11624 if (!NILP (target_frame_window))
11626 Lisp_Object frame
11627 = WINDOW_FRAME (XWINDOW (target_frame_window));
11629 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11630 Fselect_window (target_frame_window, Qt);
11632 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11633 Fselect_frame (old_top_frame, Qt);
11636 Fselect_window (old_window, Qt);
11639 if (!NILP (AREF (vector, 6)))
11641 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11642 ASET (vector, 6, Qnil);
11645 Vmode_line_unwind_vector = vector;
11649 /* Store a single character C for the frame title in mode_line_noprop_buf.
11650 Re-allocate mode_line_noprop_buf if necessary. */
11652 static void
11653 store_mode_line_noprop_char (char c)
11655 /* If output position has reached the end of the allocated buffer,
11656 increase the buffer's size. */
11657 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11659 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11660 ptrdiff_t size = len;
11661 mode_line_noprop_buf =
11662 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11663 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11664 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11667 *mode_line_noprop_ptr++ = c;
11671 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11672 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11673 characters that yield more columns than PRECISION; PRECISION <= 0
11674 means copy the whole string. Pad with spaces until FIELD_WIDTH
11675 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11676 pad. Called from display_mode_element when it is used to build a
11677 frame title. */
11679 static int
11680 store_mode_line_noprop (const char *string, int field_width, int precision)
11682 const unsigned char *str = (const unsigned char *) string;
11683 int n = 0;
11684 ptrdiff_t dummy, nbytes;
11686 /* Copy at most PRECISION chars from STR. */
11687 nbytes = strlen (string);
11688 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11689 while (nbytes--)
11690 store_mode_line_noprop_char (*str++);
11692 /* Fill up with spaces until FIELD_WIDTH reached. */
11693 while (field_width > 0
11694 && n < field_width)
11696 store_mode_line_noprop_char (' ');
11697 ++n;
11700 return n;
11703 /***********************************************************************
11704 Frame Titles
11705 ***********************************************************************/
11707 #ifdef HAVE_WINDOW_SYSTEM
11709 /* Set the title of FRAME, if it has changed. The title format is
11710 Vicon_title_format if FRAME is iconified, otherwise it is
11711 frame_title_format. */
11713 static void
11714 x_consider_frame_title (Lisp_Object frame)
11716 struct frame *f = XFRAME (frame);
11718 if ((FRAME_WINDOW_P (f)
11719 || FRAME_MINIBUF_ONLY_P (f)
11720 || f->explicit_name)
11721 && NILP (Fframe_parameter (frame, Qtooltip)))
11723 /* Do we have more than one visible frame on this X display? */
11724 Lisp_Object tail, other_frame, fmt;
11725 ptrdiff_t title_start;
11726 char *title;
11727 ptrdiff_t len;
11728 struct it it;
11729 ptrdiff_t count = SPECPDL_INDEX ();
11731 FOR_EACH_FRAME (tail, other_frame)
11733 struct frame *tf = XFRAME (other_frame);
11735 if (tf != f
11736 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11737 && !FRAME_MINIBUF_ONLY_P (tf)
11738 && !EQ (other_frame, tip_frame)
11739 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11740 break;
11743 /* Set global variable indicating that multiple frames exist. */
11744 multiple_frames = CONSP (tail);
11746 /* Switch to the buffer of selected window of the frame. Set up
11747 mode_line_target so that display_mode_element will output into
11748 mode_line_noprop_buf; then display the title. */
11749 record_unwind_protect (unwind_format_mode_line,
11750 format_mode_line_unwind_data
11751 (f, current_buffer, selected_window, false));
11752 /* select-frame calls resize_mini_window, which could resize the
11753 mini-window and by that undo the effect of this redisplay
11754 cycle wrt minibuffer and echo-area display. Binding
11755 inhibit-redisplay to t makes the call to resize_mini_window a
11756 no-op, thus avoiding the adverse side effects. */
11757 specbind (Qinhibit_redisplay, Qt);
11759 Fselect_window (f->selected_window, Qt);
11760 set_buffer_internal_1
11761 (XBUFFER (XWINDOW (f->selected_window)->contents));
11762 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11764 mode_line_target = MODE_LINE_TITLE;
11765 title_start = MODE_LINE_NOPROP_LEN (0);
11766 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11767 NULL, DEFAULT_FACE_ID);
11768 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11769 len = MODE_LINE_NOPROP_LEN (title_start);
11770 title = mode_line_noprop_buf + title_start;
11771 unbind_to (count, Qnil);
11773 /* Set the title only if it's changed. This avoids consing in
11774 the common case where it hasn't. (If it turns out that we've
11775 already wasted too much time by walking through the list with
11776 display_mode_element, then we might need to optimize at a
11777 higher level than this.) */
11778 if (! STRINGP (f->name)
11779 || SBYTES (f->name) != len
11780 || memcmp (title, SDATA (f->name), len) != 0)
11781 x_implicitly_set_name (f, make_string (title, len), Qnil);
11785 #endif /* not HAVE_WINDOW_SYSTEM */
11788 /***********************************************************************
11789 Menu Bars
11790 ***********************************************************************/
11792 /* True if we will not redisplay all visible windows. */
11793 #define REDISPLAY_SOME_P() \
11794 ((windows_or_buffers_changed == 0 \
11795 || windows_or_buffers_changed == REDISPLAY_SOME) \
11796 && (update_mode_lines == 0 \
11797 || update_mode_lines == REDISPLAY_SOME))
11799 /* Prepare for redisplay by updating menu-bar item lists when
11800 appropriate. This can call eval. */
11802 static void
11803 prepare_menu_bars (void)
11805 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11806 bool some_windows = REDISPLAY_SOME_P ();
11807 Lisp_Object tooltip_frame;
11809 #ifdef HAVE_WINDOW_SYSTEM
11810 tooltip_frame = tip_frame;
11811 #else
11812 tooltip_frame = Qnil;
11813 #endif
11815 if (FUNCTIONP (Vpre_redisplay_function))
11817 Lisp_Object windows = all_windows ? Qt : Qnil;
11818 if (all_windows && some_windows)
11820 Lisp_Object ws = window_list ();
11821 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11823 Lisp_Object this = XCAR (ws);
11824 struct window *w = XWINDOW (this);
11825 if (w->redisplay
11826 || XFRAME (w->frame)->redisplay
11827 || XBUFFER (w->contents)->text->redisplay)
11829 windows = Fcons (this, windows);
11833 safe__call1 (true, Vpre_redisplay_function, windows);
11836 /* Update all frame titles based on their buffer names, etc. We do
11837 this before the menu bars so that the buffer-menu will show the
11838 up-to-date frame titles. */
11839 #ifdef HAVE_WINDOW_SYSTEM
11840 if (all_windows)
11842 Lisp_Object tail, frame;
11844 FOR_EACH_FRAME (tail, frame)
11846 struct frame *f = XFRAME (frame);
11847 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11848 if (some_windows
11849 && !f->redisplay
11850 && !w->redisplay
11851 && !XBUFFER (w->contents)->text->redisplay)
11852 continue;
11854 if (!EQ (frame, tooltip_frame)
11855 && (FRAME_ICONIFIED_P (f)
11856 || FRAME_VISIBLE_P (f) == 1
11857 /* Exclude TTY frames that are obscured because they
11858 are not the top frame on their console. This is
11859 because x_consider_frame_title actually switches
11860 to the frame, which for TTY frames means it is
11861 marked as garbaged, and will be completely
11862 redrawn on the next redisplay cycle. This causes
11863 TTY frames to be completely redrawn, when there
11864 are more than one of them, even though nothing
11865 should be changed on display. */
11866 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11867 x_consider_frame_title (frame);
11870 #endif /* HAVE_WINDOW_SYSTEM */
11872 /* Update the menu bar item lists, if appropriate. This has to be
11873 done before any actual redisplay or generation of display lines. */
11875 if (all_windows)
11877 Lisp_Object tail, frame;
11878 ptrdiff_t count = SPECPDL_INDEX ();
11879 /* True means that update_menu_bar has run its hooks
11880 so any further calls to update_menu_bar shouldn't do so again. */
11881 bool menu_bar_hooks_run = false;
11883 record_unwind_save_match_data ();
11885 FOR_EACH_FRAME (tail, frame)
11887 struct frame *f = XFRAME (frame);
11888 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11890 /* Ignore tooltip frame. */
11891 if (EQ (frame, tooltip_frame))
11892 continue;
11894 if (some_windows
11895 && !f->redisplay
11896 && !w->redisplay
11897 && !XBUFFER (w->contents)->text->redisplay)
11898 continue;
11900 run_window_size_change_functions (frame);
11901 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11902 #ifdef HAVE_WINDOW_SYSTEM
11903 update_tool_bar (f, false);
11904 #endif
11907 unbind_to (count, Qnil);
11909 else
11911 struct frame *sf = SELECTED_FRAME ();
11912 update_menu_bar (sf, true, false);
11913 #ifdef HAVE_WINDOW_SYSTEM
11914 update_tool_bar (sf, true);
11915 #endif
11920 /* Update the menu bar item list for frame F. This has to be done
11921 before we start to fill in any display lines, because it can call
11922 eval.
11924 If SAVE_MATCH_DATA, we must save and restore it here.
11926 If HOOKS_RUN, a previous call to update_menu_bar
11927 already ran the menu bar hooks for this redisplay, so there
11928 is no need to run them again. The return value is the
11929 updated value of this flag, to pass to the next call. */
11931 static bool
11932 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11934 Lisp_Object window;
11935 struct window *w;
11937 /* If called recursively during a menu update, do nothing. This can
11938 happen when, for instance, an activate-menubar-hook causes a
11939 redisplay. */
11940 if (inhibit_menubar_update)
11941 return hooks_run;
11943 window = FRAME_SELECTED_WINDOW (f);
11944 w = XWINDOW (window);
11946 if (FRAME_WINDOW_P (f)
11948 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11949 || defined (HAVE_NS) || defined (USE_GTK)
11950 FRAME_EXTERNAL_MENU_BAR (f)
11951 #else
11952 FRAME_MENU_BAR_LINES (f) > 0
11953 #endif
11954 : FRAME_MENU_BAR_LINES (f) > 0)
11956 /* If the user has switched buffers or windows, we need to
11957 recompute to reflect the new bindings. But we'll
11958 recompute when update_mode_lines is set too; that means
11959 that people can use force-mode-line-update to request
11960 that the menu bar be recomputed. The adverse effect on
11961 the rest of the redisplay algorithm is about the same as
11962 windows_or_buffers_changed anyway. */
11963 if (windows_or_buffers_changed
11964 /* This used to test w->update_mode_line, but we believe
11965 there is no need to recompute the menu in that case. */
11966 || update_mode_lines
11967 || window_buffer_changed (w))
11969 struct buffer *prev = current_buffer;
11970 ptrdiff_t count = SPECPDL_INDEX ();
11972 specbind (Qinhibit_menubar_update, Qt);
11974 set_buffer_internal_1 (XBUFFER (w->contents));
11975 if (save_match_data)
11976 record_unwind_save_match_data ();
11977 if (NILP (Voverriding_local_map_menu_flag))
11979 specbind (Qoverriding_terminal_local_map, Qnil);
11980 specbind (Qoverriding_local_map, Qnil);
11983 if (!hooks_run)
11985 /* Run the Lucid hook. */
11986 safe_run_hooks (Qactivate_menubar_hook);
11988 /* If it has changed current-menubar from previous value,
11989 really recompute the menu-bar from the value. */
11990 if (! NILP (Vlucid_menu_bar_dirty_flag))
11991 call0 (Qrecompute_lucid_menubar);
11993 safe_run_hooks (Qmenu_bar_update_hook);
11995 hooks_run = true;
11998 XSETFRAME (Vmenu_updating_frame, f);
11999 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12001 /* Redisplay the menu bar in case we changed it. */
12002 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12003 || defined (HAVE_NS) || defined (USE_GTK)
12004 if (FRAME_WINDOW_P (f))
12006 #if defined (HAVE_NS)
12007 /* All frames on Mac OS share the same menubar. So only
12008 the selected frame should be allowed to set it. */
12009 if (f == SELECTED_FRAME ())
12010 #endif
12011 set_frame_menubar (f, false, false);
12013 else
12014 /* On a terminal screen, the menu bar is an ordinary screen
12015 line, and this makes it get updated. */
12016 w->update_mode_line = true;
12017 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12018 /* In the non-toolkit version, the menu bar is an ordinary screen
12019 line, and this makes it get updated. */
12020 w->update_mode_line = true;
12021 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12023 unbind_to (count, Qnil);
12024 set_buffer_internal_1 (prev);
12028 return hooks_run;
12031 /***********************************************************************
12032 Tool-bars
12033 ***********************************************************************/
12035 #ifdef HAVE_WINDOW_SYSTEM
12037 /* Select `frame' temporarily without running all the code in
12038 do_switch_frame.
12039 FIXME: Maybe do_switch_frame should be trimmed down similarly
12040 when `norecord' is set. */
12041 static void
12042 fast_set_selected_frame (Lisp_Object frame)
12044 if (!EQ (selected_frame, frame))
12046 selected_frame = frame;
12047 selected_window = XFRAME (frame)->selected_window;
12051 /* Update the tool-bar item list for frame F. This has to be done
12052 before we start to fill in any display lines. Called from
12053 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12054 and restore it here. */
12056 static void
12057 update_tool_bar (struct frame *f, bool save_match_data)
12059 #if defined (USE_GTK) || defined (HAVE_NS)
12060 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12061 #else
12062 bool do_update = (WINDOWP (f->tool_bar_window)
12063 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12064 #endif
12066 if (do_update)
12068 Lisp_Object window;
12069 struct window *w;
12071 window = FRAME_SELECTED_WINDOW (f);
12072 w = XWINDOW (window);
12074 /* If the user has switched buffers or windows, we need to
12075 recompute to reflect the new bindings. But we'll
12076 recompute when update_mode_lines is set too; that means
12077 that people can use force-mode-line-update to request
12078 that the menu bar be recomputed. The adverse effect on
12079 the rest of the redisplay algorithm is about the same as
12080 windows_or_buffers_changed anyway. */
12081 if (windows_or_buffers_changed
12082 || w->update_mode_line
12083 || update_mode_lines
12084 || window_buffer_changed (w))
12086 struct buffer *prev = current_buffer;
12087 ptrdiff_t count = SPECPDL_INDEX ();
12088 Lisp_Object frame, new_tool_bar;
12089 int new_n_tool_bar;
12091 /* Set current_buffer to the buffer of the selected
12092 window of the frame, so that we get the right local
12093 keymaps. */
12094 set_buffer_internal_1 (XBUFFER (w->contents));
12096 /* Save match data, if we must. */
12097 if (save_match_data)
12098 record_unwind_save_match_data ();
12100 /* Make sure that we don't accidentally use bogus keymaps. */
12101 if (NILP (Voverriding_local_map_menu_flag))
12103 specbind (Qoverriding_terminal_local_map, Qnil);
12104 specbind (Qoverriding_local_map, Qnil);
12107 /* We must temporarily set the selected frame to this frame
12108 before calling tool_bar_items, because the calculation of
12109 the tool-bar keymap uses the selected frame (see
12110 `tool-bar-make-keymap' in tool-bar.el). */
12111 eassert (EQ (selected_window,
12112 /* Since we only explicitly preserve selected_frame,
12113 check that selected_window would be redundant. */
12114 XFRAME (selected_frame)->selected_window));
12115 record_unwind_protect (fast_set_selected_frame, selected_frame);
12116 XSETFRAME (frame, f);
12117 fast_set_selected_frame (frame);
12119 /* Build desired tool-bar items from keymaps. */
12120 new_tool_bar
12121 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12122 &new_n_tool_bar);
12124 /* Redisplay the tool-bar if we changed it. */
12125 if (new_n_tool_bar != f->n_tool_bar_items
12126 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12128 /* Redisplay that happens asynchronously due to an expose event
12129 may access f->tool_bar_items. Make sure we update both
12130 variables within BLOCK_INPUT so no such event interrupts. */
12131 block_input ();
12132 fset_tool_bar_items (f, new_tool_bar);
12133 f->n_tool_bar_items = new_n_tool_bar;
12134 w->update_mode_line = true;
12135 unblock_input ();
12138 unbind_to (count, Qnil);
12139 set_buffer_internal_1 (prev);
12144 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12146 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12147 F's desired tool-bar contents. F->tool_bar_items must have
12148 been set up previously by calling prepare_menu_bars. */
12150 static void
12151 build_desired_tool_bar_string (struct frame *f)
12153 int i, size, size_needed;
12154 Lisp_Object image, plist;
12156 image = plist = Qnil;
12158 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12159 Otherwise, make a new string. */
12161 /* The size of the string we might be able to reuse. */
12162 size = (STRINGP (f->desired_tool_bar_string)
12163 ? SCHARS (f->desired_tool_bar_string)
12164 : 0);
12166 /* We need one space in the string for each image. */
12167 size_needed = f->n_tool_bar_items;
12169 /* Reuse f->desired_tool_bar_string, if possible. */
12170 if (size < size_needed || NILP (f->desired_tool_bar_string))
12171 fset_desired_tool_bar_string
12172 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12173 else
12175 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12176 Fremove_text_properties (make_number (0), make_number (size),
12177 props, f->desired_tool_bar_string);
12180 /* Put a `display' property on the string for the images to display,
12181 put a `menu_item' property on tool-bar items with a value that
12182 is the index of the item in F's tool-bar item vector. */
12183 for (i = 0; i < f->n_tool_bar_items; ++i)
12185 #define PROP(IDX) \
12186 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12188 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12189 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12190 int hmargin, vmargin, relief, idx, end;
12192 /* If image is a vector, choose the image according to the
12193 button state. */
12194 image = PROP (TOOL_BAR_ITEM_IMAGES);
12195 if (VECTORP (image))
12197 if (enabled_p)
12198 idx = (selected_p
12199 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12200 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12201 else
12202 idx = (selected_p
12203 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12204 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12206 eassert (ASIZE (image) >= idx);
12207 image = AREF (image, idx);
12209 else
12210 idx = -1;
12212 /* Ignore invalid image specifications. */
12213 if (!valid_image_p (image))
12214 continue;
12216 /* Display the tool-bar button pressed, or depressed. */
12217 plist = Fcopy_sequence (XCDR (image));
12219 /* Compute margin and relief to draw. */
12220 relief = (tool_bar_button_relief >= 0
12221 ? tool_bar_button_relief
12222 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12223 hmargin = vmargin = relief;
12225 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12226 INT_MAX - max (hmargin, vmargin)))
12228 hmargin += XFASTINT (Vtool_bar_button_margin);
12229 vmargin += XFASTINT (Vtool_bar_button_margin);
12231 else if (CONSP (Vtool_bar_button_margin))
12233 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12234 INT_MAX - hmargin))
12235 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12237 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12238 INT_MAX - vmargin))
12239 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12242 if (auto_raise_tool_bar_buttons_p)
12244 /* Add a `:relief' property to the image spec if the item is
12245 selected. */
12246 if (selected_p)
12248 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12249 hmargin -= relief;
12250 vmargin -= relief;
12253 else
12255 /* If image is selected, display it pressed, i.e. with a
12256 negative relief. If it's not selected, display it with a
12257 raised relief. */
12258 plist = Fplist_put (plist, QCrelief,
12259 (selected_p
12260 ? make_number (-relief)
12261 : make_number (relief)));
12262 hmargin -= relief;
12263 vmargin -= relief;
12266 /* Put a margin around the image. */
12267 if (hmargin || vmargin)
12269 if (hmargin == vmargin)
12270 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12271 else
12272 plist = Fplist_put (plist, QCmargin,
12273 Fcons (make_number (hmargin),
12274 make_number (vmargin)));
12277 /* If button is not enabled, and we don't have special images
12278 for the disabled state, make the image appear disabled by
12279 applying an appropriate algorithm to it. */
12280 if (!enabled_p && idx < 0)
12281 plist = Fplist_put (plist, QCconversion, Qdisabled);
12283 /* Put a `display' text property on the string for the image to
12284 display. Put a `menu-item' property on the string that gives
12285 the start of this item's properties in the tool-bar items
12286 vector. */
12287 image = Fcons (Qimage, plist);
12288 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12289 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12291 /* Let the last image hide all remaining spaces in the tool bar
12292 string. The string can be longer than needed when we reuse a
12293 previous string. */
12294 if (i + 1 == f->n_tool_bar_items)
12295 end = SCHARS (f->desired_tool_bar_string);
12296 else
12297 end = i + 1;
12298 Fadd_text_properties (make_number (i), make_number (end),
12299 props, f->desired_tool_bar_string);
12300 #undef PROP
12305 /* Display one line of the tool-bar of frame IT->f.
12307 HEIGHT specifies the desired height of the tool-bar line.
12308 If the actual height of the glyph row is less than HEIGHT, the
12309 row's height is increased to HEIGHT, and the icons are centered
12310 vertically in the new height.
12312 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12313 count a final empty row in case the tool-bar width exactly matches
12314 the window width.
12317 static void
12318 display_tool_bar_line (struct it *it, int height)
12320 struct glyph_row *row = it->glyph_row;
12321 int max_x = it->last_visible_x;
12322 struct glyph *last;
12324 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12325 clear_glyph_row (row);
12326 row->enabled_p = true;
12327 row->y = it->current_y;
12329 /* Note that this isn't made use of if the face hasn't a box,
12330 so there's no need to check the face here. */
12331 it->start_of_box_run_p = true;
12333 while (it->current_x < max_x)
12335 int x, n_glyphs_before, i, nglyphs;
12336 struct it it_before;
12338 /* Get the next display element. */
12339 if (!get_next_display_element (it))
12341 /* Don't count empty row if we are counting needed tool-bar lines. */
12342 if (height < 0 && !it->hpos)
12343 return;
12344 break;
12347 /* Produce glyphs. */
12348 n_glyphs_before = row->used[TEXT_AREA];
12349 it_before = *it;
12351 PRODUCE_GLYPHS (it);
12353 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12354 i = 0;
12355 x = it_before.current_x;
12356 while (i < nglyphs)
12358 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12360 if (x + glyph->pixel_width > max_x)
12362 /* Glyph doesn't fit on line. Backtrack. */
12363 row->used[TEXT_AREA] = n_glyphs_before;
12364 *it = it_before;
12365 /* If this is the only glyph on this line, it will never fit on the
12366 tool-bar, so skip it. But ensure there is at least one glyph,
12367 so we don't accidentally disable the tool-bar. */
12368 if (n_glyphs_before == 0
12369 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12370 break;
12371 goto out;
12374 ++it->hpos;
12375 x += glyph->pixel_width;
12376 ++i;
12379 /* Stop at line end. */
12380 if (ITERATOR_AT_END_OF_LINE_P (it))
12381 break;
12383 set_iterator_to_next (it, true);
12386 out:;
12388 row->displays_text_p = row->used[TEXT_AREA] != 0;
12390 /* Use default face for the border below the tool bar.
12392 FIXME: When auto-resize-tool-bars is grow-only, there is
12393 no additional border below the possibly empty tool-bar lines.
12394 So to make the extra empty lines look "normal", we have to
12395 use the tool-bar face for the border too. */
12396 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12397 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12398 it->face_id = DEFAULT_FACE_ID;
12400 extend_face_to_end_of_line (it);
12401 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12402 last->right_box_line_p = true;
12403 if (last == row->glyphs[TEXT_AREA])
12404 last->left_box_line_p = true;
12406 /* Make line the desired height and center it vertically. */
12407 if ((height -= it->max_ascent + it->max_descent) > 0)
12409 /* Don't add more than one line height. */
12410 height %= FRAME_LINE_HEIGHT (it->f);
12411 it->max_ascent += height / 2;
12412 it->max_descent += (height + 1) / 2;
12415 compute_line_metrics (it);
12417 /* If line is empty, make it occupy the rest of the tool-bar. */
12418 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12420 row->height = row->phys_height = it->last_visible_y - row->y;
12421 row->visible_height = row->height;
12422 row->ascent = row->phys_ascent = 0;
12423 row->extra_line_spacing = 0;
12426 row->full_width_p = true;
12427 row->continued_p = false;
12428 row->truncated_on_left_p = false;
12429 row->truncated_on_right_p = false;
12431 it->current_x = it->hpos = 0;
12432 it->current_y += row->height;
12433 ++it->vpos;
12434 ++it->glyph_row;
12438 /* Value is the number of pixels needed to make all tool-bar items of
12439 frame F visible. The actual number of glyph rows needed is
12440 returned in *N_ROWS if non-NULL. */
12441 static int
12442 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12444 struct window *w = XWINDOW (f->tool_bar_window);
12445 struct it it;
12446 /* tool_bar_height is called from redisplay_tool_bar after building
12447 the desired matrix, so use (unused) mode-line row as temporary row to
12448 avoid destroying the first tool-bar row. */
12449 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12451 /* Initialize an iterator for iteration over
12452 F->desired_tool_bar_string in the tool-bar window of frame F. */
12453 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12454 temp_row->reversed_p = false;
12455 it.first_visible_x = 0;
12456 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12457 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12458 it.paragraph_embedding = L2R;
12460 while (!ITERATOR_AT_END_P (&it))
12462 clear_glyph_row (temp_row);
12463 it.glyph_row = temp_row;
12464 display_tool_bar_line (&it, -1);
12466 clear_glyph_row (temp_row);
12468 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12469 if (n_rows)
12470 *n_rows = it.vpos > 0 ? it.vpos : -1;
12472 if (pixelwise)
12473 return it.current_y;
12474 else
12475 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12478 #endif /* !USE_GTK && !HAVE_NS */
12480 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12481 0, 2, 0,
12482 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12483 If FRAME is nil or omitted, use the selected frame. Optional argument
12484 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12485 (Lisp_Object frame, Lisp_Object pixelwise)
12487 int height = 0;
12489 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12490 struct frame *f = decode_any_frame (frame);
12492 if (WINDOWP (f->tool_bar_window)
12493 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12495 update_tool_bar (f, true);
12496 if (f->n_tool_bar_items)
12498 build_desired_tool_bar_string (f);
12499 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12502 #endif
12504 return make_number (height);
12508 /* Display the tool-bar of frame F. Value is true if tool-bar's
12509 height should be changed. */
12510 static bool
12511 redisplay_tool_bar (struct frame *f)
12513 f->tool_bar_redisplayed = true;
12514 #if defined (USE_GTK) || defined (HAVE_NS)
12516 if (FRAME_EXTERNAL_TOOL_BAR (f))
12517 update_frame_tool_bar (f);
12518 return false;
12520 #else /* !USE_GTK && !HAVE_NS */
12522 struct window *w;
12523 struct it it;
12524 struct glyph_row *row;
12526 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12527 do anything. This means you must start with tool-bar-lines
12528 non-zero to get the auto-sizing effect. Or in other words, you
12529 can turn off tool-bars by specifying tool-bar-lines zero. */
12530 if (!WINDOWP (f->tool_bar_window)
12531 || (w = XWINDOW (f->tool_bar_window),
12532 WINDOW_TOTAL_LINES (w) == 0))
12533 return false;
12535 /* Set up an iterator for the tool-bar window. */
12536 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12537 it.first_visible_x = 0;
12538 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12539 row = it.glyph_row;
12540 row->reversed_p = false;
12542 /* Build a string that represents the contents of the tool-bar. */
12543 build_desired_tool_bar_string (f);
12544 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12545 /* FIXME: This should be controlled by a user option. But it
12546 doesn't make sense to have an R2L tool bar if the menu bar cannot
12547 be drawn also R2L, and making the menu bar R2L is tricky due
12548 toolkit-specific code that implements it. If an R2L tool bar is
12549 ever supported, display_tool_bar_line should also be augmented to
12550 call unproduce_glyphs like display_line and display_string
12551 do. */
12552 it.paragraph_embedding = L2R;
12554 if (f->n_tool_bar_rows == 0)
12556 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12558 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12560 x_change_tool_bar_height (f, new_height);
12561 frame_default_tool_bar_height = new_height;
12562 /* Always do that now. */
12563 clear_glyph_matrix (w->desired_matrix);
12564 f->fonts_changed = true;
12565 return true;
12569 /* Display as many lines as needed to display all tool-bar items. */
12571 if (f->n_tool_bar_rows > 0)
12573 int border, rows, height, extra;
12575 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12576 border = XINT (Vtool_bar_border);
12577 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12578 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12579 else if (EQ (Vtool_bar_border, Qborder_width))
12580 border = f->border_width;
12581 else
12582 border = 0;
12583 if (border < 0)
12584 border = 0;
12586 rows = f->n_tool_bar_rows;
12587 height = max (1, (it.last_visible_y - border) / rows);
12588 extra = it.last_visible_y - border - height * rows;
12590 while (it.current_y < it.last_visible_y)
12592 int h = 0;
12593 if (extra > 0 && rows-- > 0)
12595 h = (extra + rows - 1) / rows;
12596 extra -= h;
12598 display_tool_bar_line (&it, height + h);
12601 else
12603 while (it.current_y < it.last_visible_y)
12604 display_tool_bar_line (&it, 0);
12607 /* It doesn't make much sense to try scrolling in the tool-bar
12608 window, so don't do it. */
12609 w->desired_matrix->no_scrolling_p = true;
12610 w->must_be_updated_p = true;
12612 if (!NILP (Vauto_resize_tool_bars))
12614 bool change_height_p = true;
12616 /* If we couldn't display everything, change the tool-bar's
12617 height if there is room for more. */
12618 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12619 change_height_p = true;
12621 /* We subtract 1 because display_tool_bar_line advances the
12622 glyph_row pointer before returning to its caller. We want to
12623 examine the last glyph row produced by
12624 display_tool_bar_line. */
12625 row = it.glyph_row - 1;
12627 /* If there are blank lines at the end, except for a partially
12628 visible blank line at the end that is smaller than
12629 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12630 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12631 && row->height >= FRAME_LINE_HEIGHT (f))
12632 change_height_p = true;
12634 /* If row displays tool-bar items, but is partially visible,
12635 change the tool-bar's height. */
12636 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12637 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12638 change_height_p = true;
12640 /* Resize windows as needed by changing the `tool-bar-lines'
12641 frame parameter. */
12642 if (change_height_p)
12644 int nrows;
12645 int new_height = tool_bar_height (f, &nrows, true);
12647 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12648 && !f->minimize_tool_bar_window_p)
12649 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12650 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12651 f->minimize_tool_bar_window_p = false;
12653 if (change_height_p)
12655 x_change_tool_bar_height (f, new_height);
12656 frame_default_tool_bar_height = new_height;
12657 clear_glyph_matrix (w->desired_matrix);
12658 f->n_tool_bar_rows = nrows;
12659 f->fonts_changed = true;
12661 return true;
12666 f->minimize_tool_bar_window_p = false;
12667 return false;
12669 #endif /* USE_GTK || HAVE_NS */
12672 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12674 /* Get information about the tool-bar item which is displayed in GLYPH
12675 on frame F. Return in *PROP_IDX the index where tool-bar item
12676 properties start in F->tool_bar_items. Value is false if
12677 GLYPH doesn't display a tool-bar item. */
12679 static bool
12680 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12682 Lisp_Object prop;
12683 int charpos;
12685 /* This function can be called asynchronously, which means we must
12686 exclude any possibility that Fget_text_property signals an
12687 error. */
12688 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12689 charpos = max (0, charpos);
12691 /* Get the text property `menu-item' at pos. The value of that
12692 property is the start index of this item's properties in
12693 F->tool_bar_items. */
12694 prop = Fget_text_property (make_number (charpos),
12695 Qmenu_item, f->current_tool_bar_string);
12696 if (! INTEGERP (prop))
12697 return false;
12698 *prop_idx = XINT (prop);
12699 return true;
12703 /* Get information about the tool-bar item at position X/Y on frame F.
12704 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12705 the current matrix of the tool-bar window of F, or NULL if not
12706 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12707 item in F->tool_bar_items. Value is
12709 -1 if X/Y is not on a tool-bar item
12710 0 if X/Y is on the same item that was highlighted before.
12711 1 otherwise. */
12713 static int
12714 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12715 int *hpos, int *vpos, int *prop_idx)
12717 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12718 struct window *w = XWINDOW (f->tool_bar_window);
12719 int area;
12721 /* Find the glyph under X/Y. */
12722 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12723 if (*glyph == NULL)
12724 return -1;
12726 /* Get the start of this tool-bar item's properties in
12727 f->tool_bar_items. */
12728 if (!tool_bar_item_info (f, *glyph, prop_idx))
12729 return -1;
12731 /* Is mouse on the highlighted item? */
12732 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12733 && *vpos >= hlinfo->mouse_face_beg_row
12734 && *vpos <= hlinfo->mouse_face_end_row
12735 && (*vpos > hlinfo->mouse_face_beg_row
12736 || *hpos >= hlinfo->mouse_face_beg_col)
12737 && (*vpos < hlinfo->mouse_face_end_row
12738 || *hpos < hlinfo->mouse_face_end_col
12739 || hlinfo->mouse_face_past_end))
12740 return 0;
12742 return 1;
12746 /* EXPORT:
12747 Handle mouse button event on the tool-bar of frame F, at
12748 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12749 false for button release. MODIFIERS is event modifiers for button
12750 release. */
12752 void
12753 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12754 int modifiers)
12756 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12757 struct window *w = XWINDOW (f->tool_bar_window);
12758 int hpos, vpos, prop_idx;
12759 struct glyph *glyph;
12760 Lisp_Object enabled_p;
12761 int ts;
12763 /* If not on the highlighted tool-bar item, and mouse-highlight is
12764 non-nil, return. This is so we generate the tool-bar button
12765 click only when the mouse button is released on the same item as
12766 where it was pressed. However, when mouse-highlight is disabled,
12767 generate the click when the button is released regardless of the
12768 highlight, since tool-bar items are not highlighted in that
12769 case. */
12770 frame_to_window_pixel_xy (w, &x, &y);
12771 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12772 if (ts == -1
12773 || (ts != 0 && !NILP (Vmouse_highlight)))
12774 return;
12776 /* When mouse-highlight is off, generate the click for the item
12777 where the button was pressed, disregarding where it was
12778 released. */
12779 if (NILP (Vmouse_highlight) && !down_p)
12780 prop_idx = f->last_tool_bar_item;
12782 /* If item is disabled, do nothing. */
12783 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12784 if (NILP (enabled_p))
12785 return;
12787 if (down_p)
12789 /* Show item in pressed state. */
12790 if (!NILP (Vmouse_highlight))
12791 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12792 f->last_tool_bar_item = prop_idx;
12794 else
12796 Lisp_Object key, frame;
12797 struct input_event event;
12798 EVENT_INIT (event);
12800 /* Show item in released state. */
12801 if (!NILP (Vmouse_highlight))
12802 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12804 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12806 XSETFRAME (frame, f);
12807 event.kind = TOOL_BAR_EVENT;
12808 event.frame_or_window = frame;
12809 event.arg = frame;
12810 kbd_buffer_store_event (&event);
12812 event.kind = TOOL_BAR_EVENT;
12813 event.frame_or_window = frame;
12814 event.arg = key;
12815 event.modifiers = modifiers;
12816 kbd_buffer_store_event (&event);
12817 f->last_tool_bar_item = -1;
12822 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12823 tool-bar window-relative coordinates X/Y. Called from
12824 note_mouse_highlight. */
12826 static void
12827 note_tool_bar_highlight (struct frame *f, int x, int y)
12829 Lisp_Object window = f->tool_bar_window;
12830 struct window *w = XWINDOW (window);
12831 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12832 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12833 int hpos, vpos;
12834 struct glyph *glyph;
12835 struct glyph_row *row;
12836 int i;
12837 Lisp_Object enabled_p;
12838 int prop_idx;
12839 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12840 bool mouse_down_p;
12841 int rc;
12843 /* Function note_mouse_highlight is called with negative X/Y
12844 values when mouse moves outside of the frame. */
12845 if (x <= 0 || y <= 0)
12847 clear_mouse_face (hlinfo);
12848 return;
12851 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12852 if (rc < 0)
12854 /* Not on tool-bar item. */
12855 clear_mouse_face (hlinfo);
12856 return;
12858 else if (rc == 0)
12859 /* On same tool-bar item as before. */
12860 goto set_help_echo;
12862 clear_mouse_face (hlinfo);
12864 /* Mouse is down, but on different tool-bar item? */
12865 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12866 && f == dpyinfo->last_mouse_frame);
12868 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12869 return;
12871 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12873 /* If tool-bar item is not enabled, don't highlight it. */
12874 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12875 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12877 /* Compute the x-position of the glyph. In front and past the
12878 image is a space. We include this in the highlighted area. */
12879 row = MATRIX_ROW (w->current_matrix, vpos);
12880 for (i = x = 0; i < hpos; ++i)
12881 x += row->glyphs[TEXT_AREA][i].pixel_width;
12883 /* Record this as the current active region. */
12884 hlinfo->mouse_face_beg_col = hpos;
12885 hlinfo->mouse_face_beg_row = vpos;
12886 hlinfo->mouse_face_beg_x = x;
12887 hlinfo->mouse_face_past_end = false;
12889 hlinfo->mouse_face_end_col = hpos + 1;
12890 hlinfo->mouse_face_end_row = vpos;
12891 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12892 hlinfo->mouse_face_window = window;
12893 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12895 /* Display it as active. */
12896 show_mouse_face (hlinfo, draw);
12899 set_help_echo:
12901 /* Set help_echo_string to a help string to display for this tool-bar item.
12902 XTread_socket does the rest. */
12903 help_echo_object = help_echo_window = Qnil;
12904 help_echo_pos = -1;
12905 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12906 if (NILP (help_echo_string))
12907 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12910 #endif /* !USE_GTK && !HAVE_NS */
12912 #endif /* HAVE_WINDOW_SYSTEM */
12916 /************************************************************************
12917 Horizontal scrolling
12918 ************************************************************************/
12920 /* For all leaf windows in the window tree rooted at WINDOW, set their
12921 hscroll value so that PT is (i) visible in the window, and (ii) so
12922 that it is not within a certain margin at the window's left and
12923 right border. Value is true if any window's hscroll has been
12924 changed. */
12926 static bool
12927 hscroll_window_tree (Lisp_Object window)
12929 bool hscrolled_p = false;
12930 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12931 int hscroll_step_abs = 0;
12932 double hscroll_step_rel = 0;
12934 if (hscroll_relative_p)
12936 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12937 if (hscroll_step_rel < 0)
12939 hscroll_relative_p = false;
12940 hscroll_step_abs = 0;
12943 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12945 hscroll_step_abs = XINT (Vhscroll_step);
12946 if (hscroll_step_abs < 0)
12947 hscroll_step_abs = 0;
12949 else
12950 hscroll_step_abs = 0;
12952 while (WINDOWP (window))
12954 struct window *w = XWINDOW (window);
12956 if (WINDOWP (w->contents))
12957 hscrolled_p |= hscroll_window_tree (w->contents);
12958 else if (w->cursor.vpos >= 0)
12960 int h_margin;
12961 int text_area_width;
12962 struct glyph_row *cursor_row;
12963 struct glyph_row *bottom_row;
12965 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12966 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12967 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12968 else
12969 cursor_row = bottom_row - 1;
12971 if (!cursor_row->enabled_p)
12973 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12974 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12975 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12976 else
12977 cursor_row = bottom_row - 1;
12979 bool row_r2l_p = cursor_row->reversed_p;
12981 text_area_width = window_box_width (w, TEXT_AREA);
12983 /* Scroll when cursor is inside this scroll margin. */
12984 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12986 /* If the position of this window's point has explicitly
12987 changed, no more suspend auto hscrolling. */
12988 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12989 w->suspend_auto_hscroll = false;
12991 /* Remember window point. */
12992 Fset_marker (w->old_pointm,
12993 ((w == XWINDOW (selected_window))
12994 ? make_number (BUF_PT (XBUFFER (w->contents)))
12995 : Fmarker_position (w->pointm)),
12996 w->contents);
12998 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12999 && !w->suspend_auto_hscroll
13000 /* In some pathological cases, like restoring a window
13001 configuration into a frame that is much smaller than
13002 the one from which the configuration was saved, we
13003 get glyph rows whose start and end have zero buffer
13004 positions, which we cannot handle below. Just skip
13005 such windows. */
13006 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13007 /* For left-to-right rows, hscroll when cursor is either
13008 (i) inside the right hscroll margin, or (ii) if it is
13009 inside the left margin and the window is already
13010 hscrolled. */
13011 && ((!row_r2l_p
13012 && ((w->hscroll && w->cursor.x <= h_margin)
13013 || (cursor_row->enabled_p
13014 && cursor_row->truncated_on_right_p
13015 && (w->cursor.x >= text_area_width - h_margin))))
13016 /* For right-to-left rows, the logic is similar,
13017 except that rules for scrolling to left and right
13018 are reversed. E.g., if cursor.x <= h_margin, we
13019 need to hscroll "to the right" unconditionally,
13020 and that will scroll the screen to the left so as
13021 to reveal the next portion of the row. */
13022 || (row_r2l_p
13023 && ((cursor_row->enabled_p
13024 /* FIXME: It is confusing to set the
13025 truncated_on_right_p flag when R2L rows
13026 are actually truncated on the left. */
13027 && cursor_row->truncated_on_right_p
13028 && w->cursor.x <= h_margin)
13029 || (w->hscroll
13030 && (w->cursor.x >= text_area_width - h_margin))))))
13032 struct it it;
13033 ptrdiff_t hscroll;
13034 struct buffer *saved_current_buffer;
13035 ptrdiff_t pt;
13036 int wanted_x;
13038 /* Find point in a display of infinite width. */
13039 saved_current_buffer = current_buffer;
13040 current_buffer = XBUFFER (w->contents);
13042 if (w == XWINDOW (selected_window))
13043 pt = PT;
13044 else
13045 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13047 /* Move iterator to pt starting at cursor_row->start in
13048 a line with infinite width. */
13049 init_to_row_start (&it, w, cursor_row);
13050 it.last_visible_x = INFINITY;
13051 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13052 current_buffer = saved_current_buffer;
13054 /* Position cursor in window. */
13055 if (!hscroll_relative_p && hscroll_step_abs == 0)
13056 hscroll = max (0, (it.current_x
13057 - (ITERATOR_AT_END_OF_LINE_P (&it)
13058 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13059 : (text_area_width / 2))))
13060 / FRAME_COLUMN_WIDTH (it.f);
13061 else if ((!row_r2l_p
13062 && w->cursor.x >= text_area_width - h_margin)
13063 || (row_r2l_p && w->cursor.x <= h_margin))
13065 if (hscroll_relative_p)
13066 wanted_x = text_area_width * (1 - hscroll_step_rel)
13067 - h_margin;
13068 else
13069 wanted_x = text_area_width
13070 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13071 - h_margin;
13072 hscroll
13073 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13075 else
13077 if (hscroll_relative_p)
13078 wanted_x = text_area_width * hscroll_step_rel
13079 + h_margin;
13080 else
13081 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13082 + h_margin;
13083 hscroll
13084 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13086 hscroll = max (hscroll, w->min_hscroll);
13088 /* Don't prevent redisplay optimizations if hscroll
13089 hasn't changed, as it will unnecessarily slow down
13090 redisplay. */
13091 if (w->hscroll != hscroll)
13093 struct buffer *b = XBUFFER (w->contents);
13094 b->prevent_redisplay_optimizations_p = true;
13095 w->hscroll = hscroll;
13096 hscrolled_p = true;
13101 window = w->next;
13104 /* Value is true if hscroll of any leaf window has been changed. */
13105 return hscrolled_p;
13109 /* Set hscroll so that cursor is visible and not inside horizontal
13110 scroll margins for all windows in the tree rooted at WINDOW. See
13111 also hscroll_window_tree above. Value is true if any window's
13112 hscroll has been changed. If it has, desired matrices on the frame
13113 of WINDOW are cleared. */
13115 static bool
13116 hscroll_windows (Lisp_Object window)
13118 bool hscrolled_p = hscroll_window_tree (window);
13119 if (hscrolled_p)
13120 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13121 return hscrolled_p;
13126 /************************************************************************
13127 Redisplay
13128 ************************************************************************/
13130 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13131 This is sometimes handy to have in a debugger session. */
13133 #ifdef GLYPH_DEBUG
13135 /* First and last unchanged row for try_window_id. */
13137 static int debug_first_unchanged_at_end_vpos;
13138 static int debug_last_unchanged_at_beg_vpos;
13140 /* Delta vpos and y. */
13142 static int debug_dvpos, debug_dy;
13144 /* Delta in characters and bytes for try_window_id. */
13146 static ptrdiff_t debug_delta, debug_delta_bytes;
13148 /* Values of window_end_pos and window_end_vpos at the end of
13149 try_window_id. */
13151 static ptrdiff_t debug_end_vpos;
13153 /* Append a string to W->desired_matrix->method. FMT is a printf
13154 format string. If trace_redisplay_p is true also printf the
13155 resulting string to stderr. */
13157 static void debug_method_add (struct window *, char const *, ...)
13158 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13160 static void
13161 debug_method_add (struct window *w, char const *fmt, ...)
13163 void *ptr = w;
13164 char *method = w->desired_matrix->method;
13165 int len = strlen (method);
13166 int size = sizeof w->desired_matrix->method;
13167 int remaining = size - len - 1;
13168 va_list ap;
13170 if (len && remaining)
13172 method[len] = '|';
13173 --remaining, ++len;
13176 va_start (ap, fmt);
13177 vsnprintf (method + len, remaining + 1, fmt, ap);
13178 va_end (ap);
13180 if (trace_redisplay_p)
13181 fprintf (stderr, "%p (%s): %s\n",
13182 ptr,
13183 ((BUFFERP (w->contents)
13184 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13185 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13186 : "no buffer"),
13187 method + len);
13190 #endif /* GLYPH_DEBUG */
13193 /* Value is true if all changes in window W, which displays
13194 current_buffer, are in the text between START and END. START is a
13195 buffer position, END is given as a distance from Z. Used in
13196 redisplay_internal for display optimization. */
13198 static bool
13199 text_outside_line_unchanged_p (struct window *w,
13200 ptrdiff_t start, ptrdiff_t end)
13202 bool unchanged_p = true;
13204 /* If text or overlays have changed, see where. */
13205 if (window_outdated (w))
13207 /* Gap in the line? */
13208 if (GPT < start || Z - GPT < end)
13209 unchanged_p = false;
13211 /* Changes start in front of the line, or end after it? */
13212 if (unchanged_p
13213 && (BEG_UNCHANGED < start - 1
13214 || END_UNCHANGED < end))
13215 unchanged_p = false;
13217 /* If selective display, can't optimize if changes start at the
13218 beginning of the line. */
13219 if (unchanged_p
13220 && INTEGERP (BVAR (current_buffer, selective_display))
13221 && XINT (BVAR (current_buffer, selective_display)) > 0
13222 && (BEG_UNCHANGED < start || GPT <= start))
13223 unchanged_p = false;
13225 /* If there are overlays at the start or end of the line, these
13226 may have overlay strings with newlines in them. A change at
13227 START, for instance, may actually concern the display of such
13228 overlay strings as well, and they are displayed on different
13229 lines. So, quickly rule out this case. (For the future, it
13230 might be desirable to implement something more telling than
13231 just BEG/END_UNCHANGED.) */
13232 if (unchanged_p)
13234 if (BEG + BEG_UNCHANGED == start
13235 && overlay_touches_p (start))
13236 unchanged_p = false;
13237 if (END_UNCHANGED == end
13238 && overlay_touches_p (Z - end))
13239 unchanged_p = false;
13242 /* Under bidi reordering, adding or deleting a character in the
13243 beginning of a paragraph, before the first strong directional
13244 character, can change the base direction of the paragraph (unless
13245 the buffer specifies a fixed paragraph direction), which will
13246 require redisplaying the whole paragraph. It might be worthwhile
13247 to find the paragraph limits and widen the range of redisplayed
13248 lines to that, but for now just give up this optimization. */
13249 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13250 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13251 unchanged_p = false;
13254 return unchanged_p;
13258 /* Do a frame update, taking possible shortcuts into account. This is
13259 the main external entry point for redisplay.
13261 If the last redisplay displayed an echo area message and that message
13262 is no longer requested, we clear the echo area or bring back the
13263 mini-buffer if that is in use. */
13265 void
13266 redisplay (void)
13268 redisplay_internal ();
13272 static Lisp_Object
13273 overlay_arrow_string_or_property (Lisp_Object var)
13275 Lisp_Object val;
13277 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13278 return val;
13280 return Voverlay_arrow_string;
13283 /* Return true if there are any overlay-arrows in current_buffer. */
13284 static bool
13285 overlay_arrow_in_current_buffer_p (void)
13287 Lisp_Object vlist;
13289 for (vlist = Voverlay_arrow_variable_list;
13290 CONSP (vlist);
13291 vlist = XCDR (vlist))
13293 Lisp_Object var = XCAR (vlist);
13294 Lisp_Object val;
13296 if (!SYMBOLP (var))
13297 continue;
13298 val = find_symbol_value (var);
13299 if (MARKERP (val)
13300 && current_buffer == XMARKER (val)->buffer)
13301 return true;
13303 return false;
13307 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13308 has changed. */
13310 static bool
13311 overlay_arrows_changed_p (void)
13313 Lisp_Object vlist;
13315 for (vlist = Voverlay_arrow_variable_list;
13316 CONSP (vlist);
13317 vlist = XCDR (vlist))
13319 Lisp_Object var = XCAR (vlist);
13320 Lisp_Object val, pstr;
13322 if (!SYMBOLP (var))
13323 continue;
13324 val = find_symbol_value (var);
13325 if (!MARKERP (val))
13326 continue;
13327 if (! EQ (COERCE_MARKER (val),
13328 Fget (var, Qlast_arrow_position))
13329 || ! (pstr = overlay_arrow_string_or_property (var),
13330 EQ (pstr, Fget (var, Qlast_arrow_string))))
13331 return true;
13333 return false;
13336 /* Mark overlay arrows to be updated on next redisplay. */
13338 static void
13339 update_overlay_arrows (int up_to_date)
13341 Lisp_Object vlist;
13343 for (vlist = Voverlay_arrow_variable_list;
13344 CONSP (vlist);
13345 vlist = XCDR (vlist))
13347 Lisp_Object var = XCAR (vlist);
13349 if (!SYMBOLP (var))
13350 continue;
13352 if (up_to_date > 0)
13354 Lisp_Object val = find_symbol_value (var);
13355 Fput (var, Qlast_arrow_position,
13356 COERCE_MARKER (val));
13357 Fput (var, Qlast_arrow_string,
13358 overlay_arrow_string_or_property (var));
13360 else if (up_to_date < 0
13361 || !NILP (Fget (var, Qlast_arrow_position)))
13363 Fput (var, Qlast_arrow_position, Qt);
13364 Fput (var, Qlast_arrow_string, Qt);
13370 /* Return overlay arrow string to display at row.
13371 Return integer (bitmap number) for arrow bitmap in left fringe.
13372 Return nil if no overlay arrow. */
13374 static Lisp_Object
13375 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13377 Lisp_Object vlist;
13379 for (vlist = Voverlay_arrow_variable_list;
13380 CONSP (vlist);
13381 vlist = XCDR (vlist))
13383 Lisp_Object var = XCAR (vlist);
13384 Lisp_Object val;
13386 if (!SYMBOLP (var))
13387 continue;
13389 val = find_symbol_value (var);
13391 if (MARKERP (val)
13392 && current_buffer == XMARKER (val)->buffer
13393 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13395 if (FRAME_WINDOW_P (it->f)
13396 /* FIXME: if ROW->reversed_p is set, this should test
13397 the right fringe, not the left one. */
13398 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13400 #ifdef HAVE_WINDOW_SYSTEM
13401 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13403 int fringe_bitmap = lookup_fringe_bitmap (val);
13404 if (fringe_bitmap != 0)
13405 return make_number (fringe_bitmap);
13407 #endif
13408 return make_number (-1); /* Use default arrow bitmap. */
13410 return overlay_arrow_string_or_property (var);
13414 return Qnil;
13417 /* Return true if point moved out of or into a composition. Otherwise
13418 return false. PREV_BUF and PREV_PT are the last point buffer and
13419 position. BUF and PT are the current point buffer and position. */
13421 static bool
13422 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13423 struct buffer *buf, ptrdiff_t pt)
13425 ptrdiff_t start, end;
13426 Lisp_Object prop;
13427 Lisp_Object buffer;
13429 XSETBUFFER (buffer, buf);
13430 /* Check a composition at the last point if point moved within the
13431 same buffer. */
13432 if (prev_buf == buf)
13434 if (prev_pt == pt)
13435 /* Point didn't move. */
13436 return false;
13438 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13439 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13440 && composition_valid_p (start, end, prop)
13441 && start < prev_pt && end > prev_pt)
13442 /* The last point was within the composition. Return true iff
13443 point moved out of the composition. */
13444 return (pt <= start || pt >= end);
13447 /* Check a composition at the current point. */
13448 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13449 && find_composition (pt, -1, &start, &end, &prop, buffer)
13450 && composition_valid_p (start, end, prop)
13451 && start < pt && end > pt);
13454 /* Reconsider the clip changes of buffer which is displayed in W. */
13456 static void
13457 reconsider_clip_changes (struct window *w)
13459 struct buffer *b = XBUFFER (w->contents);
13461 if (b->clip_changed
13462 && w->window_end_valid
13463 && w->current_matrix->buffer == b
13464 && w->current_matrix->zv == BUF_ZV (b)
13465 && w->current_matrix->begv == BUF_BEGV (b))
13466 b->clip_changed = false;
13468 /* If display wasn't paused, and W is not a tool bar window, see if
13469 point has been moved into or out of a composition. In that case,
13470 set b->clip_changed to force updating the screen. If
13471 b->clip_changed has already been set, skip this check. */
13472 if (!b->clip_changed && w->window_end_valid)
13474 ptrdiff_t pt = (w == XWINDOW (selected_window)
13475 ? PT : marker_position (w->pointm));
13477 if ((w->current_matrix->buffer != b || pt != w->last_point)
13478 && check_point_in_composition (w->current_matrix->buffer,
13479 w->last_point, b, pt))
13480 b->clip_changed = true;
13484 static void
13485 propagate_buffer_redisplay (void)
13486 { /* Resetting b->text->redisplay is problematic!
13487 We can't just reset it in the case that some window that displays
13488 it has not been redisplayed; and such a window can stay
13489 unredisplayed for a long time if it's currently invisible.
13490 But we do want to reset it at the end of redisplay otherwise
13491 its displayed windows will keep being redisplayed over and over
13492 again.
13493 So we copy all b->text->redisplay flags up to their windows here,
13494 such that mark_window_display_accurate can safely reset
13495 b->text->redisplay. */
13496 Lisp_Object ws = window_list ();
13497 for (; CONSP (ws); ws = XCDR (ws))
13499 struct window *thisw = XWINDOW (XCAR (ws));
13500 struct buffer *thisb = XBUFFER (thisw->contents);
13501 if (thisb->text->redisplay)
13502 thisw->redisplay = true;
13506 #define STOP_POLLING \
13507 do { if (! polling_stopped_here) stop_polling (); \
13508 polling_stopped_here = true; } while (false)
13510 #define RESUME_POLLING \
13511 do { if (polling_stopped_here) start_polling (); \
13512 polling_stopped_here = false; } while (false)
13515 /* Perhaps in the future avoid recentering windows if it
13516 is not necessary; currently that causes some problems. */
13518 static void
13519 redisplay_internal (void)
13521 struct window *w = XWINDOW (selected_window);
13522 struct window *sw;
13523 struct frame *fr;
13524 bool pending;
13525 bool must_finish = false, match_p;
13526 struct text_pos tlbufpos, tlendpos;
13527 int number_of_visible_frames;
13528 ptrdiff_t count;
13529 struct frame *sf;
13530 bool polling_stopped_here = false;
13531 Lisp_Object tail, frame;
13533 /* Set a limit to the number of retries we perform due to horizontal
13534 scrolling, this avoids getting stuck in an uninterruptible
13535 infinite loop (Bug #24633). */
13536 enum { MAX_HSCROLL_RETRIES = 16 };
13537 int hscroll_retries = 0;
13539 /* True means redisplay has to consider all windows on all
13540 frames. False, only selected_window is considered. */
13541 bool consider_all_windows_p;
13543 /* True means redisplay has to redisplay the miniwindow. */
13544 bool update_miniwindow_p = false;
13546 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13548 /* No redisplay if running in batch mode or frame is not yet fully
13549 initialized, or redisplay is explicitly turned off by setting
13550 Vinhibit_redisplay. */
13551 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13552 || !NILP (Vinhibit_redisplay))
13553 return;
13555 /* Don't examine these until after testing Vinhibit_redisplay.
13556 When Emacs is shutting down, perhaps because its connection to
13557 X has dropped, we should not look at them at all. */
13558 fr = XFRAME (w->frame);
13559 sf = SELECTED_FRAME ();
13561 if (!fr->glyphs_initialized_p)
13562 return;
13564 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13565 if (popup_activated ())
13566 return;
13567 #endif
13569 /* I don't think this happens but let's be paranoid. */
13570 if (redisplaying_p)
13571 return;
13573 /* Record a function that clears redisplaying_p
13574 when we leave this function. */
13575 count = SPECPDL_INDEX ();
13576 record_unwind_protect_void (unwind_redisplay);
13577 redisplaying_p = true;
13578 block_buffer_flips ();
13579 specbind (Qinhibit_free_realized_faces, Qnil);
13581 /* Record this function, so it appears on the profiler's backtraces. */
13582 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13584 FOR_EACH_FRAME (tail, frame)
13585 XFRAME (frame)->already_hscrolled_p = false;
13587 retry:
13588 /* Remember the currently selected window. */
13589 sw = w;
13591 pending = false;
13592 forget_escape_and_glyphless_faces ();
13594 inhibit_free_realized_faces = false;
13596 /* If face_change, init_iterator will free all realized faces, which
13597 includes the faces referenced from current matrices. So, we
13598 can't reuse current matrices in this case. */
13599 if (face_change)
13600 windows_or_buffers_changed = 47;
13602 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13603 && FRAME_TTY (sf)->previous_frame != sf)
13605 /* Since frames on a single ASCII terminal share the same
13606 display area, displaying a different frame means redisplay
13607 the whole thing. */
13608 SET_FRAME_GARBAGED (sf);
13609 #ifndef DOS_NT
13610 set_tty_color_mode (FRAME_TTY (sf), sf);
13611 #endif
13612 FRAME_TTY (sf)->previous_frame = sf;
13615 /* Set the visible flags for all frames. Do this before checking for
13616 resized or garbaged frames; they want to know if their frames are
13617 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13618 number_of_visible_frames = 0;
13620 FOR_EACH_FRAME (tail, frame)
13622 struct frame *f = XFRAME (frame);
13624 if (FRAME_VISIBLE_P (f))
13626 ++number_of_visible_frames;
13627 /* Adjust matrices for visible frames only. */
13628 if (f->fonts_changed)
13630 adjust_frame_glyphs (f);
13631 /* Disable all redisplay optimizations for this frame.
13632 This is because adjust_frame_glyphs resets the
13633 enabled_p flag for all glyph rows of all windows, so
13634 many optimizations will fail anyway, and some might
13635 fail to test that flag and do bogus things as
13636 result. */
13637 SET_FRAME_GARBAGED (f);
13638 f->fonts_changed = false;
13640 /* If cursor type has been changed on the frame
13641 other than selected, consider all frames. */
13642 if (f != sf && f->cursor_type_changed)
13643 fset_redisplay (f);
13645 clear_desired_matrices (f);
13648 /* Notice any pending interrupt request to change frame size. */
13649 do_pending_window_change (true);
13651 /* do_pending_window_change could change the selected_window due to
13652 frame resizing which makes the selected window too small. */
13653 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13654 sw = w;
13656 /* Clear frames marked as garbaged. */
13657 clear_garbaged_frames ();
13659 /* Build menubar and tool-bar items. */
13660 if (NILP (Vmemory_full))
13661 prepare_menu_bars ();
13663 reconsider_clip_changes (w);
13665 /* In most cases selected window displays current buffer. */
13666 match_p = XBUFFER (w->contents) == current_buffer;
13667 if (match_p)
13669 /* Detect case that we need to write or remove a star in the mode line. */
13670 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13671 w->update_mode_line = true;
13673 if (mode_line_update_needed (w))
13674 w->update_mode_line = true;
13676 /* If reconsider_clip_changes above decided that the narrowing
13677 in the current buffer changed, make sure all other windows
13678 showing that buffer will be redisplayed. */
13679 if (current_buffer->clip_changed)
13680 bset_update_mode_line (current_buffer);
13683 /* Normally the message* functions will have already displayed and
13684 updated the echo area, but the frame may have been trashed, or
13685 the update may have been preempted, so display the echo area
13686 again here. Checking message_cleared_p captures the case that
13687 the echo area should be cleared. */
13688 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13689 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13690 || (message_cleared_p
13691 && minibuf_level == 0
13692 /* If the mini-window is currently selected, this means the
13693 echo-area doesn't show through. */
13694 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13696 echo_area_display (false);
13698 /* If echo_area_display resizes the mini-window, the redisplay and
13699 window_sizes_changed flags of the selected frame are set, but
13700 it's too late for the hooks in window-size-change-functions,
13701 which have been examined already in prepare_menu_bars. So in
13702 that case we call the hooks here only for the selected frame. */
13703 if (sf->redisplay)
13705 ptrdiff_t count1 = SPECPDL_INDEX ();
13707 record_unwind_save_match_data ();
13708 run_window_size_change_functions (selected_frame);
13709 unbind_to (count1, Qnil);
13712 if (message_cleared_p)
13713 update_miniwindow_p = true;
13715 must_finish = true;
13717 /* If we don't display the current message, don't clear the
13718 message_cleared_p flag, because, if we did, we wouldn't clear
13719 the echo area in the next redisplay which doesn't preserve
13720 the echo area. */
13721 if (!display_last_displayed_message_p)
13722 message_cleared_p = false;
13724 else if (EQ (selected_window, minibuf_window)
13725 && (current_buffer->clip_changed || window_outdated (w))
13726 && resize_mini_window (w, false))
13728 if (sf->redisplay)
13730 ptrdiff_t count1 = SPECPDL_INDEX ();
13732 record_unwind_save_match_data ();
13733 run_window_size_change_functions (selected_frame);
13734 unbind_to (count1, Qnil);
13737 /* Resized active mini-window to fit the size of what it is
13738 showing if its contents might have changed. */
13739 must_finish = true;
13741 /* If window configuration was changed, frames may have been
13742 marked garbaged. Clear them or we will experience
13743 surprises wrt scrolling. */
13744 clear_garbaged_frames ();
13747 if (windows_or_buffers_changed && !update_mode_lines)
13748 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13749 only the windows's contents needs to be refreshed, or whether the
13750 mode-lines also need a refresh. */
13751 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13752 ? REDISPLAY_SOME : 32);
13754 /* If specs for an arrow have changed, do thorough redisplay
13755 to ensure we remove any arrow that should no longer exist. */
13756 if (overlay_arrows_changed_p ())
13757 /* Apparently, this is the only case where we update other windows,
13758 without updating other mode-lines. */
13759 windows_or_buffers_changed = 49;
13761 consider_all_windows_p = (update_mode_lines
13762 || windows_or_buffers_changed);
13764 #define AINC(a,i) \
13766 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13767 if (INTEGERP (entry)) \
13768 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13771 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13772 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13774 /* Optimize the case that only the line containing the cursor in the
13775 selected window has changed. Variables starting with this_ are
13776 set in display_line and record information about the line
13777 containing the cursor. */
13778 tlbufpos = this_line_start_pos;
13779 tlendpos = this_line_end_pos;
13780 if (!consider_all_windows_p
13781 && CHARPOS (tlbufpos) > 0
13782 && !w->update_mode_line
13783 && !current_buffer->clip_changed
13784 && !current_buffer->prevent_redisplay_optimizations_p
13785 && FRAME_VISIBLE_P (XFRAME (w->frame))
13786 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13787 && !XFRAME (w->frame)->cursor_type_changed
13788 && !XFRAME (w->frame)->face_change
13789 /* Make sure recorded data applies to current buffer, etc. */
13790 && this_line_buffer == current_buffer
13791 && match_p
13792 && !w->force_start
13793 && !w->optional_new_start
13794 /* Point must be on the line that we have info recorded about. */
13795 && PT >= CHARPOS (tlbufpos)
13796 && PT <= Z - CHARPOS (tlendpos)
13797 /* All text outside that line, including its final newline,
13798 must be unchanged. */
13799 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13800 CHARPOS (tlendpos)))
13802 if (CHARPOS (tlbufpos) > BEGV
13803 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13804 && (CHARPOS (tlbufpos) == ZV
13805 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13806 /* Former continuation line has disappeared by becoming empty. */
13807 goto cancel;
13808 else if (window_outdated (w) || MINI_WINDOW_P (w))
13810 /* We have to handle the case of continuation around a
13811 wide-column character (see the comment in indent.c around
13812 line 1340).
13814 For instance, in the following case:
13816 -------- Insert --------
13817 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13818 J_I_ ==> J_I_ `^^' are cursors.
13819 ^^ ^^
13820 -------- --------
13822 As we have to redraw the line above, we cannot use this
13823 optimization. */
13825 struct it it;
13826 int line_height_before = this_line_pixel_height;
13828 /* Note that start_display will handle the case that the
13829 line starting at tlbufpos is a continuation line. */
13830 start_display (&it, w, tlbufpos);
13832 /* Implementation note: It this still necessary? */
13833 if (it.current_x != this_line_start_x)
13834 goto cancel;
13836 TRACE ((stderr, "trying display optimization 1\n"));
13837 w->cursor.vpos = -1;
13838 overlay_arrow_seen = false;
13839 it.vpos = this_line_vpos;
13840 it.current_y = this_line_y;
13841 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13842 display_line (&it);
13844 /* If line contains point, is not continued,
13845 and ends at same distance from eob as before, we win. */
13846 if (w->cursor.vpos >= 0
13847 /* Line is not continued, otherwise this_line_start_pos
13848 would have been set to 0 in display_line. */
13849 && CHARPOS (this_line_start_pos)
13850 /* Line ends as before. */
13851 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13852 /* Line has same height as before. Otherwise other lines
13853 would have to be shifted up or down. */
13854 && this_line_pixel_height == line_height_before)
13856 /* If this is not the window's last line, we must adjust
13857 the charstarts of the lines below. */
13858 if (it.current_y < it.last_visible_y)
13860 struct glyph_row *row
13861 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13862 ptrdiff_t delta, delta_bytes;
13864 /* We used to distinguish between two cases here,
13865 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13866 when the line ends in a newline or the end of the
13867 buffer's accessible portion. But both cases did
13868 the same, so they were collapsed. */
13869 delta = (Z
13870 - CHARPOS (tlendpos)
13871 - MATRIX_ROW_START_CHARPOS (row));
13872 delta_bytes = (Z_BYTE
13873 - BYTEPOS (tlendpos)
13874 - MATRIX_ROW_START_BYTEPOS (row));
13876 increment_matrix_positions (w->current_matrix,
13877 this_line_vpos + 1,
13878 w->current_matrix->nrows,
13879 delta, delta_bytes);
13882 /* If this row displays text now but previously didn't,
13883 or vice versa, w->window_end_vpos may have to be
13884 adjusted. */
13885 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13887 if (w->window_end_vpos < this_line_vpos)
13888 w->window_end_vpos = this_line_vpos;
13890 else if (w->window_end_vpos == this_line_vpos
13891 && this_line_vpos > 0)
13892 w->window_end_vpos = this_line_vpos - 1;
13893 w->window_end_valid = false;
13895 /* Update hint: No need to try to scroll in update_window. */
13896 w->desired_matrix->no_scrolling_p = true;
13898 #ifdef GLYPH_DEBUG
13899 *w->desired_matrix->method = 0;
13900 debug_method_add (w, "optimization 1");
13901 #endif
13902 #ifdef HAVE_WINDOW_SYSTEM
13903 update_window_fringes (w, false);
13904 #endif
13905 goto update;
13907 else
13908 goto cancel;
13910 else if (/* Cursor position hasn't changed. */
13911 PT == w->last_point
13912 /* Make sure the cursor was last displayed
13913 in this window. Otherwise we have to reposition it. */
13915 /* PXW: Must be converted to pixels, probably. */
13916 && 0 <= w->cursor.vpos
13917 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13919 if (!must_finish)
13921 do_pending_window_change (true);
13922 /* If selected_window changed, redisplay again. */
13923 if (WINDOWP (selected_window)
13924 && (w = XWINDOW (selected_window)) != sw)
13925 goto retry;
13927 /* We used to always goto end_of_redisplay here, but this
13928 isn't enough if we have a blinking cursor. */
13929 if (w->cursor_off_p == w->last_cursor_off_p)
13930 goto end_of_redisplay;
13932 goto update;
13934 /* If highlighting the region, or if the cursor is in the echo area,
13935 then we can't just move the cursor. */
13936 else if (NILP (Vshow_trailing_whitespace)
13937 && !cursor_in_echo_area)
13939 struct it it;
13940 struct glyph_row *row;
13942 /* Skip from tlbufpos to PT and see where it is. Note that
13943 PT may be in invisible text. If so, we will end at the
13944 next visible position. */
13945 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13946 NULL, DEFAULT_FACE_ID);
13947 it.current_x = this_line_start_x;
13948 it.current_y = this_line_y;
13949 it.vpos = this_line_vpos;
13951 /* The call to move_it_to stops in front of PT, but
13952 moves over before-strings. */
13953 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13955 if (it.vpos == this_line_vpos
13956 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13957 row->enabled_p))
13959 eassert (this_line_vpos == it.vpos);
13960 eassert (this_line_y == it.current_y);
13961 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13962 if (cursor_row_fully_visible_p (w, false, true))
13964 #ifdef GLYPH_DEBUG
13965 *w->desired_matrix->method = 0;
13966 debug_method_add (w, "optimization 3");
13967 #endif
13968 goto update;
13970 else
13971 goto cancel;
13973 else
13974 goto cancel;
13977 cancel:
13978 /* Text changed drastically or point moved off of line. */
13979 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13982 CHARPOS (this_line_start_pos) = 0;
13983 ++clear_face_cache_count;
13984 #ifdef HAVE_WINDOW_SYSTEM
13985 ++clear_image_cache_count;
13986 #endif
13988 /* Build desired matrices, and update the display. If
13989 consider_all_windows_p, do it for all windows on all frames that
13990 require redisplay, as specified by their 'redisplay' flag.
13991 Otherwise do it for selected_window, only. */
13993 if (consider_all_windows_p)
13995 FOR_EACH_FRAME (tail, frame)
13996 XFRAME (frame)->updated_p = false;
13998 propagate_buffer_redisplay ();
14000 FOR_EACH_FRAME (tail, frame)
14002 struct frame *f = XFRAME (frame);
14004 /* We don't have to do anything for unselected terminal
14005 frames. */
14006 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14007 && !EQ (FRAME_TTY (f)->top_frame, frame))
14008 continue;
14010 retry_frame:
14011 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14013 bool gcscrollbars
14014 /* Only GC scrollbars when we redisplay the whole frame. */
14015 = f->redisplay || !REDISPLAY_SOME_P ();
14016 bool f_redisplay_flag = f->redisplay;
14017 /* Mark all the scroll bars to be removed; we'll redeem
14018 the ones we want when we redisplay their windows. */
14019 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14020 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14022 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14023 redisplay_windows (FRAME_ROOT_WINDOW (f));
14024 /* Remember that the invisible frames need to be redisplayed next
14025 time they're visible. */
14026 else if (!REDISPLAY_SOME_P ())
14027 f->redisplay = true;
14029 /* The X error handler may have deleted that frame. */
14030 if (!FRAME_LIVE_P (f))
14031 continue;
14033 /* Any scroll bars which redisplay_windows should have
14034 nuked should now go away. */
14035 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14036 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14038 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14040 /* If fonts changed on visible frame, display again. */
14041 if (f->fonts_changed)
14043 adjust_frame_glyphs (f);
14044 /* Disable all redisplay optimizations for this
14045 frame. For the reasons, see the comment near
14046 the previous call to adjust_frame_glyphs above. */
14047 SET_FRAME_GARBAGED (f);
14048 f->fonts_changed = false;
14049 goto retry_frame;
14052 /* See if we have to hscroll. */
14053 if (!f->already_hscrolled_p)
14055 f->already_hscrolled_p = true;
14056 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14057 && hscroll_windows (f->root_window))
14059 hscroll_retries++;
14060 goto retry_frame;
14064 /* If the frame's redisplay flag was not set before
14065 we went about redisplaying its windows, but it is
14066 set now, that means we employed some redisplay
14067 optimizations inside redisplay_windows, and
14068 bypassed producing some screen lines. But if
14069 f->redisplay is now set, it might mean the old
14070 faces are no longer valid (e.g., if redisplaying
14071 some window called some Lisp which defined a new
14072 face or redefined an existing face), so trying to
14073 use them in update_frame will segfault.
14074 Therefore, we must redisplay this frame. */
14075 if (!f_redisplay_flag && f->redisplay)
14076 goto retry_frame;
14078 /* In some case (e.g., window resize), we notice
14079 only during window updating that the window
14080 content changed unpredictably (e.g., a GTK
14081 scrollbar moved) and that our previous estimation
14082 of the frame content was garbage. We have to
14083 start over. These cases should be rare, so going
14084 all the way back to the top of redisplay should
14085 be good enough.
14087 Why FRAME_WINDOW_P? See
14088 https://lists.gnu.org/archive/html/emacs-devel/2016-10/msg00957.html
14091 if (FRAME_GARBAGED_P (f) && FRAME_WINDOW_P (f))
14092 goto retry;
14094 /* Prevent various kinds of signals during display
14095 update. stdio is not robust about handling
14096 signals, which can cause an apparent I/O error. */
14097 if (interrupt_input)
14098 unrequest_sigio ();
14099 STOP_POLLING;
14101 pending |= update_frame (f, false, false);
14102 f->cursor_type_changed = false;
14103 f->updated_p = true;
14108 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14110 if (!pending)
14112 /* Do the mark_window_display_accurate after all windows have
14113 been redisplayed because this call resets flags in buffers
14114 which are needed for proper redisplay. */
14115 FOR_EACH_FRAME (tail, frame)
14117 struct frame *f = XFRAME (frame);
14118 if (f->updated_p)
14120 f->redisplay = false;
14121 f->garbaged = false;
14122 mark_window_display_accurate (f->root_window, true);
14123 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14124 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14129 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14131 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14132 /* Use list_of_error, not Qerror, so that
14133 we catch only errors and don't run the debugger. */
14134 internal_condition_case_1 (redisplay_window_1, selected_window,
14135 list_of_error,
14136 redisplay_window_error);
14137 if (update_miniwindow_p)
14138 internal_condition_case_1 (redisplay_window_1,
14139 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14140 redisplay_window_error);
14142 /* Compare desired and current matrices, perform output. */
14144 update:
14145 /* If fonts changed, display again. Likewise if redisplay_window_1
14146 above caused some change (e.g., a change in faces) that requires
14147 considering the entire frame again. */
14148 if (sf->fonts_changed || sf->redisplay)
14150 if (sf->redisplay)
14152 /* Set this to force a more thorough redisplay.
14153 Otherwise, we might immediately loop back to the
14154 above "else-if" clause (since all the conditions that
14155 led here might still be true), and we will then
14156 infloop, because the selected-frame's redisplay flag
14157 is not (and cannot be) reset. */
14158 windows_or_buffers_changed = 50;
14160 goto retry;
14163 /* Prevent freeing of realized faces, since desired matrices are
14164 pending that reference the faces we computed and cached. */
14165 inhibit_free_realized_faces = true;
14167 /* Prevent various kinds of signals during display update.
14168 stdio is not robust about handling signals,
14169 which can cause an apparent I/O error. */
14170 if (interrupt_input)
14171 unrequest_sigio ();
14172 STOP_POLLING;
14174 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14176 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14177 && hscroll_windows (selected_window))
14179 hscroll_retries++;
14180 goto retry;
14183 XWINDOW (selected_window)->must_be_updated_p = true;
14184 pending = update_frame (sf, false, false);
14185 sf->cursor_type_changed = false;
14188 /* We may have called echo_area_display at the top of this
14189 function. If the echo area is on another frame, that may
14190 have put text on a frame other than the selected one, so the
14191 above call to update_frame would not have caught it. Catch
14192 it here. */
14193 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14194 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14196 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14198 XWINDOW (mini_window)->must_be_updated_p = true;
14199 pending |= update_frame (mini_frame, false, false);
14200 mini_frame->cursor_type_changed = false;
14201 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14202 && hscroll_windows (mini_window))
14204 hscroll_retries++;
14205 goto retry;
14210 /* If display was paused because of pending input, make sure we do a
14211 thorough update the next time. */
14212 if (pending)
14214 /* Prevent the optimization at the beginning of
14215 redisplay_internal that tries a single-line update of the
14216 line containing the cursor in the selected window. */
14217 CHARPOS (this_line_start_pos) = 0;
14219 /* Let the overlay arrow be updated the next time. */
14220 update_overlay_arrows (0);
14222 /* If we pause after scrolling, some rows in the current
14223 matrices of some windows are not valid. */
14224 if (!WINDOW_FULL_WIDTH_P (w)
14225 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14226 update_mode_lines = 36;
14228 else
14230 if (!consider_all_windows_p)
14232 /* This has already been done above if
14233 consider_all_windows_p is set. */
14234 if (XBUFFER (w->contents)->text->redisplay
14235 && buffer_window_count (XBUFFER (w->contents)) > 1)
14236 /* This can happen if b->text->redisplay was set during
14237 jit-lock. */
14238 propagate_buffer_redisplay ();
14239 mark_window_display_accurate_1 (w, true);
14241 /* Say overlay arrows are up to date. */
14242 update_overlay_arrows (1);
14244 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14245 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14248 update_mode_lines = 0;
14249 windows_or_buffers_changed = 0;
14252 /* Start SIGIO interrupts coming again. Having them off during the
14253 code above makes it less likely one will discard output, but not
14254 impossible, since there might be stuff in the system buffer here.
14255 But it is much hairier to try to do anything about that. */
14256 if (interrupt_input)
14257 request_sigio ();
14258 RESUME_POLLING;
14260 /* If a frame has become visible which was not before, redisplay
14261 again, so that we display it. Expose events for such a frame
14262 (which it gets when becoming visible) don't call the parts of
14263 redisplay constructing glyphs, so simply exposing a frame won't
14264 display anything in this case. So, we have to display these
14265 frames here explicitly. */
14266 if (!pending)
14268 int new_count = 0;
14270 FOR_EACH_FRAME (tail, frame)
14272 if (XFRAME (frame)->visible)
14273 new_count++;
14276 if (new_count != number_of_visible_frames)
14277 windows_or_buffers_changed = 52;
14280 /* Change frame size now if a change is pending. */
14281 do_pending_window_change (true);
14283 /* If we just did a pending size change, or have additional
14284 visible frames, or selected_window changed, redisplay again. */
14285 if ((windows_or_buffers_changed && !pending)
14286 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14287 goto retry;
14289 /* Clear the face and image caches.
14291 We used to do this only if consider_all_windows_p. But the cache
14292 needs to be cleared if a timer creates images in the current
14293 buffer (e.g. the test case in Bug#6230). */
14295 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14297 clear_face_cache (false);
14298 clear_face_cache_count = 0;
14301 #ifdef HAVE_WINDOW_SYSTEM
14302 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14304 clear_image_caches (Qnil);
14305 clear_image_cache_count = 0;
14307 #endif /* HAVE_WINDOW_SYSTEM */
14309 end_of_redisplay:
14310 #ifdef HAVE_NS
14311 ns_set_doc_edited ();
14312 #endif
14313 if (interrupt_input && interrupts_deferred)
14314 request_sigio ();
14316 unbind_to (count, Qnil);
14317 RESUME_POLLING;
14320 static void
14321 unwind_redisplay_preserve_echo_area (void)
14323 unblock_buffer_flips ();
14326 /* Redisplay, but leave alone any recent echo area message unless
14327 another message has been requested in its place.
14329 This is useful in situations where you need to redisplay but no
14330 user action has occurred, making it inappropriate for the message
14331 area to be cleared. See tracking_off and
14332 wait_reading_process_output for examples of these situations.
14334 FROM_WHERE is an integer saying from where this function was
14335 called. This is useful for debugging. */
14337 void
14338 redisplay_preserve_echo_area (int from_where)
14340 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14342 block_input ();
14343 ptrdiff_t count = SPECPDL_INDEX ();
14344 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14345 block_buffer_flips ();
14346 unblock_input ();
14348 if (!NILP (echo_area_buffer[1]))
14350 /* We have a previously displayed message, but no current
14351 message. Redisplay the previous message. */
14352 display_last_displayed_message_p = true;
14353 redisplay_internal ();
14354 display_last_displayed_message_p = false;
14356 else
14357 redisplay_internal ();
14359 flush_frame (SELECTED_FRAME ());
14360 unbind_to (count, Qnil);
14364 /* Function registered with record_unwind_protect in redisplay_internal. */
14366 static void
14367 unwind_redisplay (void)
14369 redisplaying_p = false;
14370 unblock_buffer_flips ();
14374 /* Mark the display of leaf window W as accurate or inaccurate.
14375 If ACCURATE_P, mark display of W as accurate.
14376 If !ACCURATE_P, arrange for W to be redisplayed the next
14377 time redisplay_internal is called. */
14379 static void
14380 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14382 struct buffer *b = XBUFFER (w->contents);
14384 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14385 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14386 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14388 if (accurate_p)
14390 b->clip_changed = false;
14391 b->prevent_redisplay_optimizations_p = false;
14392 eassert (buffer_window_count (b) > 0);
14393 /* Resetting b->text->redisplay is problematic!
14394 In order to make it safer to do it here, redisplay_internal must
14395 have copied all b->text->redisplay to their respective windows. */
14396 b->text->redisplay = false;
14398 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14399 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14400 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14401 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14403 w->current_matrix->buffer = b;
14404 w->current_matrix->begv = BUF_BEGV (b);
14405 w->current_matrix->zv = BUF_ZV (b);
14407 w->last_cursor_vpos = w->cursor.vpos;
14408 w->last_cursor_off_p = w->cursor_off_p;
14410 if (w == XWINDOW (selected_window))
14411 w->last_point = BUF_PT (b);
14412 else
14413 w->last_point = marker_position (w->pointm);
14415 w->window_end_valid = true;
14416 w->update_mode_line = false;
14419 w->redisplay = !accurate_p;
14423 /* Mark the display of windows in the window tree rooted at WINDOW as
14424 accurate or inaccurate. If ACCURATE_P, mark display of
14425 windows as accurate. If !ACCURATE_P, arrange for windows to
14426 be redisplayed the next time redisplay_internal is called. */
14428 void
14429 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14431 struct window *w;
14433 for (; !NILP (window); window = w->next)
14435 w = XWINDOW (window);
14436 if (WINDOWP (w->contents))
14437 mark_window_display_accurate (w->contents, accurate_p);
14438 else
14439 mark_window_display_accurate_1 (w, accurate_p);
14442 if (accurate_p)
14443 update_overlay_arrows (1);
14444 else
14445 /* Force a thorough redisplay the next time by setting
14446 last_arrow_position and last_arrow_string to t, which is
14447 unequal to any useful value of Voverlay_arrow_... */
14448 update_overlay_arrows (-1);
14452 /* Return value in display table DP (Lisp_Char_Table *) for character
14453 C. Since a display table doesn't have any parent, we don't have to
14454 follow parent. Do not call this function directly but use the
14455 macro DISP_CHAR_VECTOR. */
14457 Lisp_Object
14458 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14460 Lisp_Object val;
14462 if (ASCII_CHAR_P (c))
14464 val = dp->ascii;
14465 if (SUB_CHAR_TABLE_P (val))
14466 val = XSUB_CHAR_TABLE (val)->contents[c];
14468 else
14470 Lisp_Object table;
14472 XSETCHAR_TABLE (table, dp);
14473 val = char_table_ref (table, c);
14475 if (NILP (val))
14476 val = dp->defalt;
14477 return val;
14480 static int buffer_flip_blocked_depth;
14482 static void
14483 block_buffer_flips (void)
14485 eassert (buffer_flip_blocked_depth >= 0);
14486 buffer_flip_blocked_depth++;
14489 static void
14490 unblock_buffer_flips (void)
14492 eassert (buffer_flip_blocked_depth > 0);
14493 if (--buffer_flip_blocked_depth == 0)
14495 Lisp_Object tail, frame;
14496 block_input ();
14497 FOR_EACH_FRAME (tail, frame)
14499 struct frame *f = XFRAME (frame);
14500 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14501 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14503 unblock_input ();
14507 bool
14508 buffer_flipping_blocked_p (void)
14510 return buffer_flip_blocked_depth > 0;
14514 /***********************************************************************
14515 Window Redisplay
14516 ***********************************************************************/
14518 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14520 static void
14521 redisplay_windows (Lisp_Object window)
14523 while (!NILP (window))
14525 struct window *w = XWINDOW (window);
14527 if (WINDOWP (w->contents))
14528 redisplay_windows (w->contents);
14529 else if (BUFFERP (w->contents))
14531 displayed_buffer = XBUFFER (w->contents);
14532 /* Use list_of_error, not Qerror, so that
14533 we catch only errors and don't run the debugger. */
14534 internal_condition_case_1 (redisplay_window_0, window,
14535 list_of_error,
14536 redisplay_window_error);
14539 window = w->next;
14543 static Lisp_Object
14544 redisplay_window_error (Lisp_Object ignore)
14546 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14547 return Qnil;
14550 static Lisp_Object
14551 redisplay_window_0 (Lisp_Object window)
14553 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14554 redisplay_window (window, false);
14555 return Qnil;
14558 static Lisp_Object
14559 redisplay_window_1 (Lisp_Object window)
14561 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14562 redisplay_window (window, true);
14563 return Qnil;
14567 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14568 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14569 which positions recorded in ROW differ from current buffer
14570 positions.
14572 Return true iff cursor is on this row. */
14574 static bool
14575 set_cursor_from_row (struct window *w, struct glyph_row *row,
14576 struct glyph_matrix *matrix,
14577 ptrdiff_t delta, ptrdiff_t delta_bytes,
14578 int dy, int dvpos)
14580 struct glyph *glyph = row->glyphs[TEXT_AREA];
14581 struct glyph *end = glyph + row->used[TEXT_AREA];
14582 struct glyph *cursor = NULL;
14583 /* The last known character position in row. */
14584 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14585 int x = row->x;
14586 ptrdiff_t pt_old = PT - delta;
14587 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14588 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14589 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14590 /* A glyph beyond the edge of TEXT_AREA which we should never
14591 touch. */
14592 struct glyph *glyphs_end = end;
14593 /* True means we've found a match for cursor position, but that
14594 glyph has the avoid_cursor_p flag set. */
14595 bool match_with_avoid_cursor = false;
14596 /* True means we've seen at least one glyph that came from a
14597 display string. */
14598 bool string_seen = false;
14599 /* Largest and smallest buffer positions seen so far during scan of
14600 glyph row. */
14601 ptrdiff_t bpos_max = pos_before;
14602 ptrdiff_t bpos_min = pos_after;
14603 /* Last buffer position covered by an overlay string with an integer
14604 `cursor' property. */
14605 ptrdiff_t bpos_covered = 0;
14606 /* True means the display string on which to display the cursor
14607 comes from a text property, not from an overlay. */
14608 bool string_from_text_prop = false;
14610 /* Don't even try doing anything if called for a mode-line or
14611 header-line row, since the rest of the code isn't prepared to
14612 deal with such calamities. */
14613 eassert (!row->mode_line_p);
14614 if (row->mode_line_p)
14615 return false;
14617 /* Skip over glyphs not having an object at the start and the end of
14618 the row. These are special glyphs like truncation marks on
14619 terminal frames. */
14620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14622 if (!row->reversed_p)
14624 while (glyph < end
14625 && NILP (glyph->object)
14626 && glyph->charpos < 0)
14628 x += glyph->pixel_width;
14629 ++glyph;
14631 while (end > glyph
14632 && NILP ((end - 1)->object)
14633 /* CHARPOS is zero for blanks and stretch glyphs
14634 inserted by extend_face_to_end_of_line. */
14635 && (end - 1)->charpos <= 0)
14636 --end;
14637 glyph_before = glyph - 1;
14638 glyph_after = end;
14640 else
14642 struct glyph *g;
14644 /* If the glyph row is reversed, we need to process it from back
14645 to front, so swap the edge pointers. */
14646 glyphs_end = end = glyph - 1;
14647 glyph += row->used[TEXT_AREA] - 1;
14649 while (glyph > end + 1
14650 && NILP (glyph->object)
14651 && glyph->charpos < 0)
14653 --glyph;
14654 x -= glyph->pixel_width;
14656 if (NILP (glyph->object) && glyph->charpos < 0)
14657 --glyph;
14658 /* By default, in reversed rows we put the cursor on the
14659 rightmost (first in the reading order) glyph. */
14660 for (g = end + 1; g < glyph; g++)
14661 x += g->pixel_width;
14662 while (end < glyph
14663 && NILP ((end + 1)->object)
14664 && (end + 1)->charpos <= 0)
14665 ++end;
14666 glyph_before = glyph + 1;
14667 glyph_after = end;
14670 else if (row->reversed_p)
14672 /* In R2L rows that don't display text, put the cursor on the
14673 rightmost glyph. Case in point: an empty last line that is
14674 part of an R2L paragraph. */
14675 cursor = end - 1;
14676 /* Avoid placing the cursor on the last glyph of the row, where
14677 on terminal frames we hold the vertical border between
14678 adjacent windows. */
14679 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14680 && !WINDOW_RIGHTMOST_P (w)
14681 && cursor == row->glyphs[LAST_AREA] - 1)
14682 cursor--;
14683 x = -1; /* will be computed below, at label compute_x */
14686 /* Step 1: Try to find the glyph whose character position
14687 corresponds to point. If that's not possible, find 2 glyphs
14688 whose character positions are the closest to point, one before
14689 point, the other after it. */
14690 if (!row->reversed_p)
14691 while (/* not marched to end of glyph row */
14692 glyph < end
14693 /* glyph was not inserted by redisplay for internal purposes */
14694 && !NILP (glyph->object))
14696 if (BUFFERP (glyph->object))
14698 ptrdiff_t dpos = glyph->charpos - pt_old;
14700 if (glyph->charpos > bpos_max)
14701 bpos_max = glyph->charpos;
14702 if (glyph->charpos < bpos_min)
14703 bpos_min = glyph->charpos;
14704 if (!glyph->avoid_cursor_p)
14706 /* If we hit point, we've found the glyph on which to
14707 display the cursor. */
14708 if (dpos == 0)
14710 match_with_avoid_cursor = false;
14711 break;
14713 /* See if we've found a better approximation to
14714 POS_BEFORE or to POS_AFTER. */
14715 if (0 > dpos && dpos > pos_before - pt_old)
14717 pos_before = glyph->charpos;
14718 glyph_before = glyph;
14720 else if (0 < dpos && dpos < pos_after - pt_old)
14722 pos_after = glyph->charpos;
14723 glyph_after = glyph;
14726 else if (dpos == 0)
14727 match_with_avoid_cursor = true;
14729 else if (STRINGP (glyph->object))
14731 Lisp_Object chprop;
14732 ptrdiff_t glyph_pos = glyph->charpos;
14734 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14735 glyph->object);
14736 if (!NILP (chprop))
14738 /* If the string came from a `display' text property,
14739 look up the buffer position of that property and
14740 use that position to update bpos_max, as if we
14741 actually saw such a position in one of the row's
14742 glyphs. This helps with supporting integer values
14743 of `cursor' property on the display string in
14744 situations where most or all of the row's buffer
14745 text is completely covered by display properties,
14746 so that no glyph with valid buffer positions is
14747 ever seen in the row. */
14748 ptrdiff_t prop_pos =
14749 string_buffer_position_lim (glyph->object, pos_before,
14750 pos_after, false);
14752 if (prop_pos >= pos_before)
14753 bpos_max = prop_pos;
14755 if (INTEGERP (chprop))
14757 bpos_covered = bpos_max + XINT (chprop);
14758 /* If the `cursor' property covers buffer positions up
14759 to and including point, we should display cursor on
14760 this glyph. Note that, if a `cursor' property on one
14761 of the string's characters has an integer value, we
14762 will break out of the loop below _before_ we get to
14763 the position match above. IOW, integer values of
14764 the `cursor' property override the "exact match for
14765 point" strategy of positioning the cursor. */
14766 /* Implementation note: bpos_max == pt_old when, e.g.,
14767 we are in an empty line, where bpos_max is set to
14768 MATRIX_ROW_START_CHARPOS, see above. */
14769 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14771 cursor = glyph;
14772 break;
14776 string_seen = true;
14778 x += glyph->pixel_width;
14779 ++glyph;
14781 else if (glyph > end) /* row is reversed */
14782 while (!NILP (glyph->object))
14784 if (BUFFERP (glyph->object))
14786 ptrdiff_t dpos = glyph->charpos - pt_old;
14788 if (glyph->charpos > bpos_max)
14789 bpos_max = glyph->charpos;
14790 if (glyph->charpos < bpos_min)
14791 bpos_min = glyph->charpos;
14792 if (!glyph->avoid_cursor_p)
14794 if (dpos == 0)
14796 match_with_avoid_cursor = false;
14797 break;
14799 if (0 > dpos && dpos > pos_before - pt_old)
14801 pos_before = glyph->charpos;
14802 glyph_before = glyph;
14804 else if (0 < dpos && dpos < pos_after - pt_old)
14806 pos_after = glyph->charpos;
14807 glyph_after = glyph;
14810 else if (dpos == 0)
14811 match_with_avoid_cursor = true;
14813 else if (STRINGP (glyph->object))
14815 Lisp_Object chprop;
14816 ptrdiff_t glyph_pos = glyph->charpos;
14818 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14819 glyph->object);
14820 if (!NILP (chprop))
14822 ptrdiff_t prop_pos =
14823 string_buffer_position_lim (glyph->object, pos_before,
14824 pos_after, false);
14826 if (prop_pos >= pos_before)
14827 bpos_max = prop_pos;
14829 if (INTEGERP (chprop))
14831 bpos_covered = bpos_max + XINT (chprop);
14832 /* If the `cursor' property covers buffer positions up
14833 to and including point, we should display cursor on
14834 this glyph. */
14835 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14837 cursor = glyph;
14838 break;
14841 string_seen = true;
14843 --glyph;
14844 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14846 x--; /* can't use any pixel_width */
14847 break;
14849 x -= glyph->pixel_width;
14852 /* Step 2: If we didn't find an exact match for point, we need to
14853 look for a proper place to put the cursor among glyphs between
14854 GLYPH_BEFORE and GLYPH_AFTER. */
14855 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14856 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14857 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14859 /* An empty line has a single glyph whose OBJECT is nil and
14860 whose CHARPOS is the position of a newline on that line.
14861 Note that on a TTY, there are more glyphs after that, which
14862 were produced by extend_face_to_end_of_line, but their
14863 CHARPOS is zero or negative. */
14864 bool empty_line_p =
14865 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14866 && NILP (glyph->object) && glyph->charpos > 0
14867 /* On a TTY, continued and truncated rows also have a glyph at
14868 their end whose OBJECT is nil and whose CHARPOS is
14869 positive (the continuation and truncation glyphs), but such
14870 rows are obviously not "empty". */
14871 && !(row->continued_p || row->truncated_on_right_p));
14873 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14875 ptrdiff_t ellipsis_pos;
14877 /* Scan back over the ellipsis glyphs. */
14878 if (!row->reversed_p)
14880 ellipsis_pos = (glyph - 1)->charpos;
14881 while (glyph > row->glyphs[TEXT_AREA]
14882 && (glyph - 1)->charpos == ellipsis_pos)
14883 glyph--, x -= glyph->pixel_width;
14884 /* That loop always goes one position too far, including
14885 the glyph before the ellipsis. So scan forward over
14886 that one. */
14887 x += glyph->pixel_width;
14888 glyph++;
14890 else /* row is reversed */
14892 ellipsis_pos = (glyph + 1)->charpos;
14893 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14894 && (glyph + 1)->charpos == ellipsis_pos)
14895 glyph++, x += glyph->pixel_width;
14896 x -= glyph->pixel_width;
14897 glyph--;
14900 else if (match_with_avoid_cursor)
14902 cursor = glyph_after;
14903 x = -1;
14905 else if (string_seen)
14907 int incr = row->reversed_p ? -1 : +1;
14909 /* Need to find the glyph that came out of a string which is
14910 present at point. That glyph is somewhere between
14911 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14912 positioned between POS_BEFORE and POS_AFTER in the
14913 buffer. */
14914 struct glyph *start, *stop;
14915 ptrdiff_t pos = pos_before;
14917 x = -1;
14919 /* If the row ends in a newline from a display string,
14920 reordering could have moved the glyphs belonging to the
14921 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14922 in this case we extend the search to the last glyph in
14923 the row that was not inserted by redisplay. */
14924 if (row->ends_in_newline_from_string_p)
14926 glyph_after = end;
14927 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14930 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14931 correspond to POS_BEFORE and POS_AFTER, respectively. We
14932 need START and STOP in the order that corresponds to the
14933 row's direction as given by its reversed_p flag. If the
14934 directionality of characters between POS_BEFORE and
14935 POS_AFTER is the opposite of the row's base direction,
14936 these characters will have been reordered for display,
14937 and we need to reverse START and STOP. */
14938 if (!row->reversed_p)
14940 start = min (glyph_before, glyph_after);
14941 stop = max (glyph_before, glyph_after);
14943 else
14945 start = max (glyph_before, glyph_after);
14946 stop = min (glyph_before, glyph_after);
14948 for (glyph = start + incr;
14949 row->reversed_p ? glyph > stop : glyph < stop; )
14952 /* Any glyphs that come from the buffer are here because
14953 of bidi reordering. Skip them, and only pay
14954 attention to glyphs that came from some string. */
14955 if (STRINGP (glyph->object))
14957 Lisp_Object str;
14958 ptrdiff_t tem;
14959 /* If the display property covers the newline, we
14960 need to search for it one position farther. */
14961 ptrdiff_t lim = pos_after
14962 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14964 string_from_text_prop = false;
14965 str = glyph->object;
14966 tem = string_buffer_position_lim (str, pos, lim, false);
14967 if (tem == 0 /* from overlay */
14968 || pos <= tem)
14970 /* If the string from which this glyph came is
14971 found in the buffer at point, or at position
14972 that is closer to point than pos_after, then
14973 we've found the glyph we've been looking for.
14974 If it comes from an overlay (tem == 0), and
14975 it has the `cursor' property on one of its
14976 glyphs, record that glyph as a candidate for
14977 displaying the cursor. (As in the
14978 unidirectional version, we will display the
14979 cursor on the last candidate we find.) */
14980 if (tem == 0
14981 || tem == pt_old
14982 || (tem - pt_old > 0 && tem < pos_after))
14984 /* The glyphs from this string could have
14985 been reordered. Find the one with the
14986 smallest string position. Or there could
14987 be a character in the string with the
14988 `cursor' property, which means display
14989 cursor on that character's glyph. */
14990 ptrdiff_t strpos = glyph->charpos;
14992 if (tem)
14994 cursor = glyph;
14995 string_from_text_prop = true;
14997 for ( ;
14998 (row->reversed_p ? glyph > stop : glyph < stop)
14999 && EQ (glyph->object, str);
15000 glyph += incr)
15002 Lisp_Object cprop;
15003 ptrdiff_t gpos = glyph->charpos;
15005 cprop = Fget_char_property (make_number (gpos),
15006 Qcursor,
15007 glyph->object);
15008 if (!NILP (cprop))
15010 cursor = glyph;
15011 break;
15013 if (tem && glyph->charpos < strpos)
15015 strpos = glyph->charpos;
15016 cursor = glyph;
15020 if (tem == pt_old
15021 || (tem - pt_old > 0 && tem < pos_after))
15022 goto compute_x;
15024 if (tem)
15025 pos = tem + 1; /* don't find previous instances */
15027 /* This string is not what we want; skip all of the
15028 glyphs that came from it. */
15029 while ((row->reversed_p ? glyph > stop : glyph < stop)
15030 && EQ (glyph->object, str))
15031 glyph += incr;
15033 else
15034 glyph += incr;
15037 /* If we reached the end of the line, and END was from a string,
15038 the cursor is not on this line. */
15039 if (cursor == NULL
15040 && (row->reversed_p ? glyph <= end : glyph >= end)
15041 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15042 && STRINGP (end->object)
15043 && row->continued_p)
15044 return false;
15046 /* A truncated row may not include PT among its character positions.
15047 Setting the cursor inside the scroll margin will trigger
15048 recalculation of hscroll in hscroll_window_tree. But if a
15049 display string covers point, defer to the string-handling
15050 code below to figure this out. */
15051 else if (row->truncated_on_left_p && pt_old < bpos_min)
15053 cursor = glyph_before;
15054 x = -1;
15056 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15057 /* Zero-width characters produce no glyphs. */
15058 || (!empty_line_p
15059 && (row->reversed_p
15060 ? glyph_after > glyphs_end
15061 : glyph_after < glyphs_end)))
15063 cursor = glyph_after;
15064 x = -1;
15068 compute_x:
15069 if (cursor != NULL)
15070 glyph = cursor;
15071 else if (glyph == glyphs_end
15072 && pos_before == pos_after
15073 && STRINGP ((row->reversed_p
15074 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15075 : row->glyphs[TEXT_AREA])->object))
15077 /* If all the glyphs of this row came from strings, put the
15078 cursor on the first glyph of the row. This avoids having the
15079 cursor outside of the text area in this very rare and hard
15080 use case. */
15081 glyph =
15082 row->reversed_p
15083 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15084 : row->glyphs[TEXT_AREA];
15086 if (x < 0)
15088 struct glyph *g;
15090 /* Need to compute x that corresponds to GLYPH. */
15091 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15093 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15094 emacs_abort ();
15095 x += g->pixel_width;
15099 /* ROW could be part of a continued line, which, under bidi
15100 reordering, might have other rows whose start and end charpos
15101 occlude point. Only set w->cursor if we found a better
15102 approximation to the cursor position than we have from previously
15103 examined candidate rows belonging to the same continued line. */
15104 if (/* We already have a candidate row. */
15105 w->cursor.vpos >= 0
15106 /* That candidate is not the row we are processing. */
15107 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15108 /* Make sure cursor.vpos specifies a row whose start and end
15109 charpos occlude point, and it is valid candidate for being a
15110 cursor-row. This is because some callers of this function
15111 leave cursor.vpos at the row where the cursor was displayed
15112 during the last redisplay cycle. */
15113 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15114 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15115 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15117 struct glyph *g1
15118 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15120 /* Don't consider glyphs that are outside TEXT_AREA. */
15121 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15122 return false;
15123 /* Keep the candidate whose buffer position is the closest to
15124 point or has the `cursor' property. */
15125 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15126 w->cursor.hpos >= 0
15127 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15128 && ((BUFFERP (g1->object)
15129 && (g1->charpos == pt_old /* An exact match always wins. */
15130 || (BUFFERP (glyph->object)
15131 && eabs (g1->charpos - pt_old)
15132 < eabs (glyph->charpos - pt_old))))
15133 /* Previous candidate is a glyph from a string that has
15134 a non-nil `cursor' property. */
15135 || (STRINGP (g1->object)
15136 && (!NILP (Fget_char_property (make_number (g1->charpos),
15137 Qcursor, g1->object))
15138 /* Previous candidate is from the same display
15139 string as this one, and the display string
15140 came from a text property. */
15141 || (EQ (g1->object, glyph->object)
15142 && string_from_text_prop)
15143 /* this candidate is from newline and its
15144 position is not an exact match */
15145 || (NILP (glyph->object)
15146 && glyph->charpos != pt_old)))))
15147 return false;
15148 /* If this candidate gives an exact match, use that. */
15149 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15150 /* If this candidate is a glyph created for the
15151 terminating newline of a line, and point is on that
15152 newline, it wins because it's an exact match. */
15153 || (!row->continued_p
15154 && NILP (glyph->object)
15155 && glyph->charpos == 0
15156 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15157 /* Otherwise, keep the candidate that comes from a row
15158 spanning less buffer positions. This may win when one or
15159 both candidate positions are on glyphs that came from
15160 display strings, for which we cannot compare buffer
15161 positions. */
15162 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15163 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15164 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15165 return false;
15167 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15168 w->cursor.x = x;
15169 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15170 w->cursor.y = row->y + dy;
15172 if (w == XWINDOW (selected_window))
15174 if (!row->continued_p
15175 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15176 && row->x == 0)
15178 this_line_buffer = XBUFFER (w->contents);
15180 CHARPOS (this_line_start_pos)
15181 = MATRIX_ROW_START_CHARPOS (row) + delta;
15182 BYTEPOS (this_line_start_pos)
15183 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15185 CHARPOS (this_line_end_pos)
15186 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15187 BYTEPOS (this_line_end_pos)
15188 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15190 this_line_y = w->cursor.y;
15191 this_line_pixel_height = row->height;
15192 this_line_vpos = w->cursor.vpos;
15193 this_line_start_x = row->x;
15195 else
15196 CHARPOS (this_line_start_pos) = 0;
15199 return true;
15203 /* Run window scroll functions, if any, for WINDOW with new window
15204 start STARTP. Sets the window start of WINDOW to that position.
15206 We assume that the window's buffer is really current. */
15208 static struct text_pos
15209 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15211 struct window *w = XWINDOW (window);
15212 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15214 eassert (current_buffer == XBUFFER (w->contents));
15216 if (!NILP (Vwindow_scroll_functions))
15218 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15219 make_number (CHARPOS (startp)));
15220 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15221 /* In case the hook functions switch buffers. */
15222 set_buffer_internal (XBUFFER (w->contents));
15225 return startp;
15229 /* Make sure the line containing the cursor is fully visible.
15230 A value of true means there is nothing to be done.
15231 (Either the line is fully visible, or it cannot be made so,
15232 or we cannot tell.)
15234 If FORCE_P, return false even if partial visible cursor row
15235 is higher than window.
15237 If CURRENT_MATRIX_P, use the information from the
15238 window's current glyph matrix; otherwise use the desired glyph
15239 matrix.
15241 A value of false means the caller should do scrolling
15242 as if point had gone off the screen. */
15244 static bool
15245 cursor_row_fully_visible_p (struct window *w, bool force_p,
15246 bool current_matrix_p)
15248 struct glyph_matrix *matrix;
15249 struct glyph_row *row;
15250 int window_height;
15252 if (!make_cursor_line_fully_visible_p)
15253 return true;
15255 /* It's not always possible to find the cursor, e.g, when a window
15256 is full of overlay strings. Don't do anything in that case. */
15257 if (w->cursor.vpos < 0)
15258 return true;
15260 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15261 row = MATRIX_ROW (matrix, w->cursor.vpos);
15263 /* If the cursor row is not partially visible, there's nothing to do. */
15264 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15265 return true;
15267 /* If the row the cursor is in is taller than the window's height,
15268 it's not clear what to do, so do nothing. */
15269 window_height = window_box_height (w);
15270 if (row->height >= window_height)
15272 if (!force_p || MINI_WINDOW_P (w)
15273 || w->vscroll || w->cursor.vpos == 0)
15274 return true;
15276 return false;
15280 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15281 means only WINDOW is redisplayed in redisplay_internal.
15282 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15283 in redisplay_window to bring a partially visible line into view in
15284 the case that only the cursor has moved.
15286 LAST_LINE_MISFIT should be true if we're scrolling because the
15287 last screen line's vertical height extends past the end of the screen.
15289 Value is
15291 1 if scrolling succeeded
15293 0 if scrolling didn't find point.
15295 -1 if new fonts have been loaded so that we must interrupt
15296 redisplay, adjust glyph matrices, and try again. */
15298 enum
15300 SCROLLING_SUCCESS,
15301 SCROLLING_FAILED,
15302 SCROLLING_NEED_LARGER_MATRICES
15305 /* If scroll-conservatively is more than this, never recenter.
15307 If you change this, don't forget to update the doc string of
15308 `scroll-conservatively' and the Emacs manual. */
15309 #define SCROLL_LIMIT 100
15311 static int
15312 try_scrolling (Lisp_Object window, bool just_this_one_p,
15313 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15314 bool temp_scroll_step, bool last_line_misfit)
15316 struct window *w = XWINDOW (window);
15317 struct frame *f = XFRAME (w->frame);
15318 struct text_pos pos, startp;
15319 struct it it;
15320 int this_scroll_margin, scroll_max, rc, height;
15321 int dy = 0, amount_to_scroll = 0;
15322 bool scroll_down_p = false;
15323 int extra_scroll_margin_lines = last_line_misfit;
15324 Lisp_Object aggressive;
15325 /* We will never try scrolling more than this number of lines. */
15326 int scroll_limit = SCROLL_LIMIT;
15327 int frame_line_height = default_line_pixel_height (w);
15328 int window_total_lines
15329 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15331 #ifdef GLYPH_DEBUG
15332 debug_method_add (w, "try_scrolling");
15333 #endif
15335 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15337 /* Compute scroll margin height in pixels. We scroll when point is
15338 within this distance from the top or bottom of the window. */
15339 if (scroll_margin > 0)
15340 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15341 * frame_line_height;
15342 else
15343 this_scroll_margin = 0;
15345 /* Force arg_scroll_conservatively to have a reasonable value, to
15346 avoid scrolling too far away with slow move_it_* functions. Note
15347 that the user can supply scroll-conservatively equal to
15348 `most-positive-fixnum', which can be larger than INT_MAX. */
15349 if (arg_scroll_conservatively > scroll_limit)
15351 arg_scroll_conservatively = scroll_limit + 1;
15352 scroll_max = scroll_limit * frame_line_height;
15354 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15355 /* Compute how much we should try to scroll maximally to bring
15356 point into view. */
15357 scroll_max = (max (scroll_step,
15358 max (arg_scroll_conservatively, temp_scroll_step))
15359 * frame_line_height);
15360 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15361 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15362 /* We're trying to scroll because of aggressive scrolling but no
15363 scroll_step is set. Choose an arbitrary one. */
15364 scroll_max = 10 * frame_line_height;
15365 else
15366 scroll_max = 0;
15368 too_near_end:
15370 /* Decide whether to scroll down. */
15371 if (PT > CHARPOS (startp))
15373 int scroll_margin_y;
15375 /* Compute the pixel ypos of the scroll margin, then move IT to
15376 either that ypos or PT, whichever comes first. */
15377 start_display (&it, w, startp);
15378 scroll_margin_y = it.last_visible_y - this_scroll_margin
15379 - frame_line_height * extra_scroll_margin_lines;
15380 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15381 (MOVE_TO_POS | MOVE_TO_Y));
15383 if (PT > CHARPOS (it.current.pos))
15385 int y0 = line_bottom_y (&it);
15386 /* Compute how many pixels below window bottom to stop searching
15387 for PT. This avoids costly search for PT that is far away if
15388 the user limited scrolling by a small number of lines, but
15389 always finds PT if scroll_conservatively is set to a large
15390 number, such as most-positive-fixnum. */
15391 int slack = max (scroll_max, 10 * frame_line_height);
15392 int y_to_move = it.last_visible_y + slack;
15394 /* Compute the distance from the scroll margin to PT or to
15395 the scroll limit, whichever comes first. This should
15396 include the height of the cursor line, to make that line
15397 fully visible. */
15398 move_it_to (&it, PT, -1, y_to_move,
15399 -1, MOVE_TO_POS | MOVE_TO_Y);
15400 dy = line_bottom_y (&it) - y0;
15402 if (dy > scroll_max)
15403 return SCROLLING_FAILED;
15405 if (dy > 0)
15406 scroll_down_p = true;
15408 else if (PT == IT_CHARPOS (it)
15409 && IT_CHARPOS (it) < ZV
15410 && it.method == GET_FROM_STRING
15411 && arg_scroll_conservatively > scroll_limit
15412 && it.current_x == 0)
15414 enum move_it_result skip;
15415 int y1 = it.current_y;
15416 int vpos;
15418 /* A before-string that includes newlines and is displayed
15419 on the last visible screen line could fail us under
15420 scroll-conservatively > 100, because we will be unable to
15421 position the cursor on that last visible line. Try to
15422 recover by finding the first screen line that has some
15423 glyphs coming from the buffer text. */
15424 do {
15425 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15426 if (skip != MOVE_NEWLINE_OR_CR
15427 || IT_CHARPOS (it) != PT
15428 || it.method == GET_FROM_BUFFER)
15429 break;
15430 vpos = it.vpos;
15431 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15432 } while (it.vpos > vpos);
15434 dy = it.current_y - y1;
15436 if (dy > scroll_max)
15437 return SCROLLING_FAILED;
15439 if (dy > 0)
15440 scroll_down_p = true;
15444 if (scroll_down_p)
15446 /* Point is in or below the bottom scroll margin, so move the
15447 window start down. If scrolling conservatively, move it just
15448 enough down to make point visible. If scroll_step is set,
15449 move it down by scroll_step. */
15450 if (arg_scroll_conservatively)
15451 amount_to_scroll
15452 = min (max (dy, frame_line_height),
15453 frame_line_height * arg_scroll_conservatively);
15454 else if (scroll_step || temp_scroll_step)
15455 amount_to_scroll = scroll_max;
15456 else
15458 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15459 height = WINDOW_BOX_TEXT_HEIGHT (w);
15460 if (NUMBERP (aggressive))
15462 double float_amount = XFLOATINT (aggressive) * height;
15463 int aggressive_scroll = float_amount;
15464 if (aggressive_scroll == 0 && float_amount > 0)
15465 aggressive_scroll = 1;
15466 /* Don't let point enter the scroll margin near top of
15467 the window. This could happen if the value of
15468 scroll_up_aggressively is too large and there are
15469 non-zero margins, because scroll_up_aggressively
15470 means put point that fraction of window height
15471 _from_the_bottom_margin_. */
15472 if (aggressive_scroll + 2 * this_scroll_margin > height)
15473 aggressive_scroll = height - 2 * this_scroll_margin;
15474 amount_to_scroll = dy + aggressive_scroll;
15478 if (amount_to_scroll <= 0)
15479 return SCROLLING_FAILED;
15481 start_display (&it, w, startp);
15482 if (arg_scroll_conservatively <= scroll_limit)
15483 move_it_vertically (&it, amount_to_scroll);
15484 else
15486 /* Extra precision for users who set scroll-conservatively
15487 to a large number: make sure the amount we scroll
15488 the window start is never less than amount_to_scroll,
15489 which was computed as distance from window bottom to
15490 point. This matters when lines at window top and lines
15491 below window bottom have different height. */
15492 struct it it1;
15493 void *it1data = NULL;
15494 /* We use a temporary it1 because line_bottom_y can modify
15495 its argument, if it moves one line down; see there. */
15496 int start_y;
15498 SAVE_IT (it1, it, it1data);
15499 start_y = line_bottom_y (&it1);
15500 do {
15501 RESTORE_IT (&it, &it, it1data);
15502 move_it_by_lines (&it, 1);
15503 SAVE_IT (it1, it, it1data);
15504 } while (IT_CHARPOS (it) < ZV
15505 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15506 bidi_unshelve_cache (it1data, true);
15509 /* If STARTP is unchanged, move it down another screen line. */
15510 if (IT_CHARPOS (it) == CHARPOS (startp))
15511 move_it_by_lines (&it, 1);
15512 startp = it.current.pos;
15514 else
15516 struct text_pos scroll_margin_pos = startp;
15517 int y_offset = 0;
15519 /* See if point is inside the scroll margin at the top of the
15520 window. */
15521 if (this_scroll_margin)
15523 int y_start;
15525 start_display (&it, w, startp);
15526 y_start = it.current_y;
15527 move_it_vertically (&it, this_scroll_margin);
15528 scroll_margin_pos = it.current.pos;
15529 /* If we didn't move enough before hitting ZV, request
15530 additional amount of scroll, to move point out of the
15531 scroll margin. */
15532 if (IT_CHARPOS (it) == ZV
15533 && it.current_y - y_start < this_scroll_margin)
15534 y_offset = this_scroll_margin - (it.current_y - y_start);
15537 if (PT < CHARPOS (scroll_margin_pos))
15539 /* Point is in the scroll margin at the top of the window or
15540 above what is displayed in the window. */
15541 int y0, y_to_move;
15543 /* Compute the vertical distance from PT to the scroll
15544 margin position. Move as far as scroll_max allows, or
15545 one screenful, or 10 screen lines, whichever is largest.
15546 Give up if distance is greater than scroll_max or if we
15547 didn't reach the scroll margin position. */
15548 SET_TEXT_POS (pos, PT, PT_BYTE);
15549 start_display (&it, w, pos);
15550 y0 = it.current_y;
15551 y_to_move = max (it.last_visible_y,
15552 max (scroll_max, 10 * frame_line_height));
15553 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15554 y_to_move, -1,
15555 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15556 dy = it.current_y - y0;
15557 if (dy > scroll_max
15558 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15559 return SCROLLING_FAILED;
15561 /* Additional scroll for when ZV was too close to point. */
15562 dy += y_offset;
15564 /* Compute new window start. */
15565 start_display (&it, w, startp);
15567 if (arg_scroll_conservatively)
15568 amount_to_scroll = max (dy, frame_line_height
15569 * max (scroll_step, temp_scroll_step));
15570 else if (scroll_step || temp_scroll_step)
15571 amount_to_scroll = scroll_max;
15572 else
15574 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15575 height = WINDOW_BOX_TEXT_HEIGHT (w);
15576 if (NUMBERP (aggressive))
15578 double float_amount = XFLOATINT (aggressive) * height;
15579 int aggressive_scroll = float_amount;
15580 if (aggressive_scroll == 0 && float_amount > 0)
15581 aggressive_scroll = 1;
15582 /* Don't let point enter the scroll margin near
15583 bottom of the window, if the value of
15584 scroll_down_aggressively happens to be too
15585 large. */
15586 if (aggressive_scroll + 2 * this_scroll_margin > height)
15587 aggressive_scroll = height - 2 * this_scroll_margin;
15588 amount_to_scroll = dy + aggressive_scroll;
15592 if (amount_to_scroll <= 0)
15593 return SCROLLING_FAILED;
15595 move_it_vertically_backward (&it, amount_to_scroll);
15596 startp = it.current.pos;
15600 /* Run window scroll functions. */
15601 startp = run_window_scroll_functions (window, startp);
15603 /* Display the window. Give up if new fonts are loaded, or if point
15604 doesn't appear. */
15605 if (!try_window (window, startp, 0))
15606 rc = SCROLLING_NEED_LARGER_MATRICES;
15607 else if (w->cursor.vpos < 0)
15609 clear_glyph_matrix (w->desired_matrix);
15610 rc = SCROLLING_FAILED;
15612 else
15614 /* Maybe forget recorded base line for line number display. */
15615 if (!just_this_one_p
15616 || current_buffer->clip_changed
15617 || BEG_UNCHANGED < CHARPOS (startp))
15618 w->base_line_number = 0;
15620 /* If cursor ends up on a partially visible line,
15621 treat that as being off the bottom of the screen. */
15622 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15623 false)
15624 /* It's possible that the cursor is on the first line of the
15625 buffer, which is partially obscured due to a vscroll
15626 (Bug#7537). In that case, avoid looping forever. */
15627 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15629 clear_glyph_matrix (w->desired_matrix);
15630 ++extra_scroll_margin_lines;
15631 goto too_near_end;
15633 rc = SCROLLING_SUCCESS;
15636 return rc;
15640 /* Compute a suitable window start for window W if display of W starts
15641 on a continuation line. Value is true if a new window start
15642 was computed.
15644 The new window start will be computed, based on W's width, starting
15645 from the start of the continued line. It is the start of the
15646 screen line with the minimum distance from the old start W->start,
15647 which is still before point (otherwise point will definitely not
15648 be visible in the window). */
15650 static bool
15651 compute_window_start_on_continuation_line (struct window *w)
15653 struct text_pos pos, start_pos, pos_before_pt;
15654 bool window_start_changed_p = false;
15656 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15658 /* If window start is on a continuation line... Window start may be
15659 < BEGV in case there's invisible text at the start of the
15660 buffer (M-x rmail, for example). */
15661 if (CHARPOS (start_pos) > BEGV
15662 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15664 struct it it;
15665 struct glyph_row *row;
15667 /* Handle the case that the window start is out of range. */
15668 if (CHARPOS (start_pos) < BEGV)
15669 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15670 else if (CHARPOS (start_pos) > ZV)
15671 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15673 /* Find the start of the continued line. This should be fast
15674 because find_newline is fast (newline cache). */
15675 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15676 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15677 row, DEFAULT_FACE_ID);
15678 reseat_at_previous_visible_line_start (&it);
15680 /* If the line start is "too far" away from the window start,
15681 say it takes too much time to compute a new window start.
15682 Also, give up if the line start is after point, as in that
15683 case point will not be visible with any window start we
15684 compute. */
15685 if (IT_CHARPOS (it) <= PT
15686 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15687 /* PXW: Do we need upper bounds here? */
15688 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15690 int min_distance, distance;
15692 /* Move forward by display lines to find the new window
15693 start. If window width was enlarged, the new start can
15694 be expected to be > the old start. If window width was
15695 decreased, the new window start will be < the old start.
15696 So, we're looking for the display line start with the
15697 minimum distance from the old window start. */
15698 pos_before_pt = pos = it.current.pos;
15699 min_distance = INFINITY;
15700 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15701 distance < min_distance)
15703 min_distance = distance;
15704 if (CHARPOS (pos) <= PT)
15705 pos_before_pt = pos;
15706 pos = it.current.pos;
15707 if (it.line_wrap == WORD_WRAP)
15709 /* Under WORD_WRAP, move_it_by_lines is likely to
15710 overshoot and stop not at the first, but the
15711 second character from the left margin. So in
15712 that case, we need a more tight control on the X
15713 coordinate of the iterator than move_it_by_lines
15714 promises in its contract. The method is to first
15715 go to the last (rightmost) visible character of a
15716 line, then move to the leftmost character on the
15717 next line in a separate call. */
15718 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15719 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15720 move_it_to (&it, ZV, 0,
15721 it.current_y + it.max_ascent + it.max_descent, -1,
15722 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15724 else
15725 move_it_by_lines (&it, 1);
15728 /* It makes very little sense to make the new window start
15729 after point, as point won't be visible. If that's what
15730 the loop above finds, fall back on the candidate before
15731 or at point that is closest to the old window start. */
15732 if (CHARPOS (pos) > PT)
15733 pos = pos_before_pt;
15735 /* Set the window start there. */
15736 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15737 window_start_changed_p = true;
15741 return window_start_changed_p;
15745 /* Try cursor movement in case text has not changed in window WINDOW,
15746 with window start STARTP. Value is
15748 CURSOR_MOVEMENT_SUCCESS if successful
15750 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15752 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15753 display. *SCROLL_STEP is set to true, under certain circumstances, if
15754 we want to scroll as if scroll-step were set to 1. See the code.
15756 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15757 which case we have to abort this redisplay, and adjust matrices
15758 first. */
15760 enum
15762 CURSOR_MOVEMENT_SUCCESS,
15763 CURSOR_MOVEMENT_CANNOT_BE_USED,
15764 CURSOR_MOVEMENT_MUST_SCROLL,
15765 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15768 static int
15769 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15770 bool *scroll_step)
15772 struct window *w = XWINDOW (window);
15773 struct frame *f = XFRAME (w->frame);
15774 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15776 #ifdef GLYPH_DEBUG
15777 if (inhibit_try_cursor_movement)
15778 return rc;
15779 #endif
15781 /* Previously, there was a check for Lisp integer in the
15782 if-statement below. Now, this field is converted to
15783 ptrdiff_t, thus zero means invalid position in a buffer. */
15784 eassert (w->last_point > 0);
15785 /* Likewise there was a check whether window_end_vpos is nil or larger
15786 than the window. Now window_end_vpos is int and so never nil, but
15787 let's leave eassert to check whether it fits in the window. */
15788 eassert (!w->window_end_valid
15789 || w->window_end_vpos < w->current_matrix->nrows);
15791 /* Handle case where text has not changed, only point, and it has
15792 not moved off the frame. */
15793 if (/* Point may be in this window. */
15794 PT >= CHARPOS (startp)
15795 /* Selective display hasn't changed. */
15796 && !current_buffer->clip_changed
15797 /* Function force-mode-line-update is used to force a thorough
15798 redisplay. It sets either windows_or_buffers_changed or
15799 update_mode_lines. So don't take a shortcut here for these
15800 cases. */
15801 && !update_mode_lines
15802 && !windows_or_buffers_changed
15803 && !f->cursor_type_changed
15804 && NILP (Vshow_trailing_whitespace)
15805 /* This code is not used for mini-buffer for the sake of the case
15806 of redisplaying to replace an echo area message; since in
15807 that case the mini-buffer contents per se are usually
15808 unchanged. This code is of no real use in the mini-buffer
15809 since the handling of this_line_start_pos, etc., in redisplay
15810 handles the same cases. */
15811 && !EQ (window, minibuf_window)
15812 && (FRAME_WINDOW_P (f)
15813 || !overlay_arrow_in_current_buffer_p ()))
15815 int this_scroll_margin, top_scroll_margin;
15816 struct glyph_row *row = NULL;
15817 int frame_line_height = default_line_pixel_height (w);
15818 int window_total_lines
15819 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15821 #ifdef GLYPH_DEBUG
15822 debug_method_add (w, "cursor movement");
15823 #endif
15825 /* Scroll if point within this distance from the top or bottom
15826 of the window. This is a pixel value. */
15827 if (scroll_margin > 0)
15829 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15830 this_scroll_margin *= frame_line_height;
15832 else
15833 this_scroll_margin = 0;
15835 top_scroll_margin = this_scroll_margin;
15836 if (WINDOW_WANTS_HEADER_LINE_P (w))
15837 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15839 /* Start with the row the cursor was displayed during the last
15840 not paused redisplay. Give up if that row is not valid. */
15841 if (w->last_cursor_vpos < 0
15842 || w->last_cursor_vpos >= w->current_matrix->nrows)
15843 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15844 else
15846 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15847 if (row->mode_line_p)
15848 ++row;
15849 if (!row->enabled_p)
15850 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15853 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15855 bool scroll_p = false, must_scroll = false;
15856 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15858 if (PT > w->last_point)
15860 /* Point has moved forward. */
15861 while (MATRIX_ROW_END_CHARPOS (row) < PT
15862 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15864 eassert (row->enabled_p);
15865 ++row;
15868 /* If the end position of a row equals the start
15869 position of the next row, and PT is at that position,
15870 we would rather display cursor in the next line. */
15871 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15872 && MATRIX_ROW_END_CHARPOS (row) == PT
15873 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15874 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15875 && !cursor_row_p (row))
15876 ++row;
15878 /* If within the scroll margin, scroll. Note that
15879 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15880 the next line would be drawn, and that
15881 this_scroll_margin can be zero. */
15882 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15883 || PT > MATRIX_ROW_END_CHARPOS (row)
15884 /* Line is completely visible last line in window
15885 and PT is to be set in the next line. */
15886 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15887 && PT == MATRIX_ROW_END_CHARPOS (row)
15888 && !row->ends_at_zv_p
15889 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15890 scroll_p = true;
15892 else if (PT < w->last_point)
15894 /* Cursor has to be moved backward. Note that PT >=
15895 CHARPOS (startp) because of the outer if-statement. */
15896 while (!row->mode_line_p
15897 && (MATRIX_ROW_START_CHARPOS (row) > PT
15898 || (MATRIX_ROW_START_CHARPOS (row) == PT
15899 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15900 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15901 row > w->current_matrix->rows
15902 && (row-1)->ends_in_newline_from_string_p))))
15903 && (row->y > top_scroll_margin
15904 || CHARPOS (startp) == BEGV))
15906 eassert (row->enabled_p);
15907 --row;
15910 /* Consider the following case: Window starts at BEGV,
15911 there is invisible, intangible text at BEGV, so that
15912 display starts at some point START > BEGV. It can
15913 happen that we are called with PT somewhere between
15914 BEGV and START. Try to handle that case. */
15915 if (row < w->current_matrix->rows
15916 || row->mode_line_p)
15918 row = w->current_matrix->rows;
15919 if (row->mode_line_p)
15920 ++row;
15923 /* Due to newlines in overlay strings, we may have to
15924 skip forward over overlay strings. */
15925 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15926 && MATRIX_ROW_END_CHARPOS (row) == PT
15927 && !cursor_row_p (row))
15928 ++row;
15930 /* If within the scroll margin, scroll. */
15931 if (row->y < top_scroll_margin
15932 && CHARPOS (startp) != BEGV)
15933 scroll_p = true;
15935 else
15937 /* Cursor did not move. So don't scroll even if cursor line
15938 is partially visible, as it was so before. */
15939 rc = CURSOR_MOVEMENT_SUCCESS;
15942 if (PT < MATRIX_ROW_START_CHARPOS (row)
15943 || PT > MATRIX_ROW_END_CHARPOS (row))
15945 /* if PT is not in the glyph row, give up. */
15946 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15947 must_scroll = true;
15949 else if (rc != CURSOR_MOVEMENT_SUCCESS
15950 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15952 struct glyph_row *row1;
15954 /* If rows are bidi-reordered and point moved, back up
15955 until we find a row that does not belong to a
15956 continuation line. This is because we must consider
15957 all rows of a continued line as candidates for the
15958 new cursor positioning, since row start and end
15959 positions change non-linearly with vertical position
15960 in such rows. */
15961 /* FIXME: Revisit this when glyph ``spilling'' in
15962 continuation lines' rows is implemented for
15963 bidi-reordered rows. */
15964 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15965 MATRIX_ROW_CONTINUATION_LINE_P (row);
15966 --row)
15968 /* If we hit the beginning of the displayed portion
15969 without finding the first row of a continued
15970 line, give up. */
15971 if (row <= row1)
15973 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15974 break;
15976 eassert (row->enabled_p);
15979 if (must_scroll)
15981 else if (rc != CURSOR_MOVEMENT_SUCCESS
15982 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15983 /* Make sure this isn't a header line by any chance, since
15984 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15985 && !row->mode_line_p
15986 && make_cursor_line_fully_visible_p)
15988 if (PT == MATRIX_ROW_END_CHARPOS (row)
15989 && !row->ends_at_zv_p
15990 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15991 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15992 else if (row->height > window_box_height (w))
15994 /* If we end up in a partially visible line, let's
15995 make it fully visible, except when it's taller
15996 than the window, in which case we can't do much
15997 about it. */
15998 *scroll_step = true;
15999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16001 else
16003 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16004 if (!cursor_row_fully_visible_p (w, false, true))
16005 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16006 else
16007 rc = CURSOR_MOVEMENT_SUCCESS;
16010 else if (scroll_p)
16011 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16012 else if (rc != CURSOR_MOVEMENT_SUCCESS
16013 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16015 /* With bidi-reordered rows, there could be more than
16016 one candidate row whose start and end positions
16017 occlude point. We need to let set_cursor_from_row
16018 find the best candidate. */
16019 /* FIXME: Revisit this when glyph ``spilling'' in
16020 continuation lines' rows is implemented for
16021 bidi-reordered rows. */
16022 bool rv = false;
16026 bool at_zv_p = false, exact_match_p = false;
16028 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16029 && PT <= MATRIX_ROW_END_CHARPOS (row)
16030 && cursor_row_p (row))
16031 rv |= set_cursor_from_row (w, row, w->current_matrix,
16032 0, 0, 0, 0);
16033 /* As soon as we've found the exact match for point,
16034 or the first suitable row whose ends_at_zv_p flag
16035 is set, we are done. */
16036 if (rv)
16038 at_zv_p = MATRIX_ROW (w->current_matrix,
16039 w->cursor.vpos)->ends_at_zv_p;
16040 if (!at_zv_p
16041 && w->cursor.hpos >= 0
16042 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16043 w->cursor.vpos))
16045 struct glyph_row *candidate =
16046 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16047 struct glyph *g =
16048 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16049 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16051 exact_match_p =
16052 (BUFFERP (g->object) && g->charpos == PT)
16053 || (NILP (g->object)
16054 && (g->charpos == PT
16055 || (g->charpos == 0 && endpos - 1 == PT)));
16057 if (at_zv_p || exact_match_p)
16059 rc = CURSOR_MOVEMENT_SUCCESS;
16060 break;
16063 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16064 break;
16065 ++row;
16067 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16068 || row->continued_p)
16069 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16070 || (MATRIX_ROW_START_CHARPOS (row) == PT
16071 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16072 /* If we didn't find any candidate rows, or exited the
16073 loop before all the candidates were examined, signal
16074 to the caller that this method failed. */
16075 if (rc != CURSOR_MOVEMENT_SUCCESS
16076 && !(rv
16077 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16078 && !row->continued_p))
16079 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16080 else if (rv)
16081 rc = CURSOR_MOVEMENT_SUCCESS;
16083 else
16087 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16089 rc = CURSOR_MOVEMENT_SUCCESS;
16090 break;
16092 ++row;
16094 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16095 && MATRIX_ROW_START_CHARPOS (row) == PT
16096 && cursor_row_p (row));
16101 return rc;
16105 void
16106 set_vertical_scroll_bar (struct window *w)
16108 ptrdiff_t start, end, whole;
16110 /* Calculate the start and end positions for the current window.
16111 At some point, it would be nice to choose between scrollbars
16112 which reflect the whole buffer size, with special markers
16113 indicating narrowing, and scrollbars which reflect only the
16114 visible region.
16116 Note that mini-buffers sometimes aren't displaying any text. */
16117 if (!MINI_WINDOW_P (w)
16118 || (w == XWINDOW (minibuf_window)
16119 && NILP (echo_area_buffer[0])))
16121 struct buffer *buf = XBUFFER (w->contents);
16122 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16123 start = marker_position (w->start) - BUF_BEGV (buf);
16124 /* I don't think this is guaranteed to be right. For the
16125 moment, we'll pretend it is. */
16126 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16128 if (end < start)
16129 end = start;
16130 if (whole < (end - start))
16131 whole = end - start;
16133 else
16134 start = end = whole = 0;
16136 /* Indicate what this scroll bar ought to be displaying now. */
16137 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16138 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16139 (w, end - start, whole, start);
16143 void
16144 set_horizontal_scroll_bar (struct window *w)
16146 int start, end, whole, portion;
16148 if (!MINI_WINDOW_P (w)
16149 || (w == XWINDOW (minibuf_window)
16150 && NILP (echo_area_buffer[0])))
16152 struct buffer *b = XBUFFER (w->contents);
16153 struct buffer *old_buffer = NULL;
16154 struct it it;
16155 struct text_pos startp;
16157 if (b != current_buffer)
16159 old_buffer = current_buffer;
16160 set_buffer_internal (b);
16163 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16164 start_display (&it, w, startp);
16165 it.last_visible_x = INT_MAX;
16166 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16167 MOVE_TO_X | MOVE_TO_Y);
16168 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16169 window_box_height (w), -1,
16170 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16172 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16173 end = start + window_box_width (w, TEXT_AREA);
16174 portion = end - start;
16175 /* After enlarging a horizontally scrolled window such that it
16176 gets at least as wide as the text it contains, make sure that
16177 the thumb doesn't fill the entire scroll bar so we can still
16178 drag it back to see the entire text. */
16179 whole = max (whole, end);
16181 if (it.bidi_p)
16183 Lisp_Object pdir;
16185 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16186 if (EQ (pdir, Qright_to_left))
16188 start = whole - end;
16189 end = start + portion;
16193 if (old_buffer)
16194 set_buffer_internal (old_buffer);
16196 else
16197 start = end = whole = portion = 0;
16199 w->hscroll_whole = whole;
16201 /* Indicate what this scroll bar ought to be displaying now. */
16202 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16203 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16204 (w, portion, whole, start);
16208 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16209 selected_window is redisplayed.
16211 We can return without actually redisplaying the window if fonts has been
16212 changed on window's frame. In that case, redisplay_internal will retry.
16214 As one of the important parts of redisplaying a window, we need to
16215 decide whether the previous window-start position (stored in the
16216 window's w->start marker position) is still valid, and if it isn't,
16217 recompute it. Some details about that:
16219 . The previous window-start could be in a continuation line, in
16220 which case we need to recompute it when the window width
16221 changes. See compute_window_start_on_continuation_line and its
16222 call below.
16224 . The text that changed since last redisplay could include the
16225 previous window-start position. In that case, we try to salvage
16226 what we can from the current glyph matrix by calling
16227 try_scrolling, which see.
16229 . Some Emacs command could force us to use a specific window-start
16230 position by setting the window's force_start flag, or gently
16231 propose doing that by setting the window's optional_new_start
16232 flag. In these cases, we try using the specified start point if
16233 that succeeds (i.e. the window desired matrix is successfully
16234 recomputed, and point location is within the window). In case
16235 of optional_new_start, we first check if the specified start
16236 position is feasible, i.e. if it will allow point to be
16237 displayed in the window. If using the specified start point
16238 fails, e.g., if new fonts are needed to be loaded, we abort the
16239 redisplay cycle and leave it up to the next cycle to figure out
16240 things.
16242 . Note that the window's force_start flag is sometimes set by
16243 redisplay itself, when it decides that the previous window start
16244 point is fine and should be kept. Search for "goto force_start"
16245 below to see the details. Like the values of window-start
16246 specified outside of redisplay, these internally-deduced values
16247 are tested for feasibility, and ignored if found to be
16248 unfeasible.
16250 . Note that the function try_window, used to completely redisplay
16251 a window, accepts the window's start point as its argument.
16252 This is used several times in the redisplay code to control
16253 where the window start will be, according to user options such
16254 as scroll-conservatively, and also to ensure the screen line
16255 showing point will be fully (as opposed to partially) visible on
16256 display. */
16258 static void
16259 redisplay_window (Lisp_Object window, bool just_this_one_p)
16261 struct window *w = XWINDOW (window);
16262 struct frame *f = XFRAME (w->frame);
16263 struct buffer *buffer = XBUFFER (w->contents);
16264 struct buffer *old = current_buffer;
16265 struct text_pos lpoint, opoint, startp;
16266 bool update_mode_line;
16267 int tem;
16268 struct it it;
16269 /* Record it now because it's overwritten. */
16270 bool current_matrix_up_to_date_p = false;
16271 bool used_current_matrix_p = false;
16272 /* This is less strict than current_matrix_up_to_date_p.
16273 It indicates that the buffer contents and narrowing are unchanged. */
16274 bool buffer_unchanged_p = false;
16275 bool temp_scroll_step = false;
16276 ptrdiff_t count = SPECPDL_INDEX ();
16277 int rc;
16278 int centering_position = -1;
16279 bool last_line_misfit = false;
16280 ptrdiff_t beg_unchanged, end_unchanged;
16281 int frame_line_height;
16282 bool use_desired_matrix;
16283 void *itdata = NULL;
16285 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16286 opoint = lpoint;
16288 #ifdef GLYPH_DEBUG
16289 *w->desired_matrix->method = 0;
16290 #endif
16292 if (!just_this_one_p
16293 && REDISPLAY_SOME_P ()
16294 && !w->redisplay
16295 && !w->update_mode_line
16296 && !f->face_change
16297 && !f->redisplay
16298 && !buffer->text->redisplay
16299 && BUF_PT (buffer) == w->last_point)
16300 return;
16302 /* Make sure that both W's markers are valid. */
16303 eassert (XMARKER (w->start)->buffer == buffer);
16304 eassert (XMARKER (w->pointm)->buffer == buffer);
16306 /* We come here again if we need to run window-text-change-functions
16307 below. */
16308 restart:
16309 reconsider_clip_changes (w);
16310 frame_line_height = default_line_pixel_height (w);
16312 /* Has the mode line to be updated? */
16313 update_mode_line = (w->update_mode_line
16314 || update_mode_lines
16315 || buffer->clip_changed
16316 || buffer->prevent_redisplay_optimizations_p);
16318 if (!just_this_one_p)
16319 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16320 cleverly elsewhere. */
16321 w->must_be_updated_p = true;
16323 if (MINI_WINDOW_P (w))
16325 if (w == XWINDOW (echo_area_window)
16326 && !NILP (echo_area_buffer[0]))
16328 if (update_mode_line)
16329 /* We may have to update a tty frame's menu bar or a
16330 tool-bar. Example `M-x C-h C-h C-g'. */
16331 goto finish_menu_bars;
16332 else
16333 /* We've already displayed the echo area glyphs in this window. */
16334 goto finish_scroll_bars;
16336 else if ((w != XWINDOW (minibuf_window)
16337 || minibuf_level == 0)
16338 /* When buffer is nonempty, redisplay window normally. */
16339 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16340 /* Quail displays non-mini buffers in minibuffer window.
16341 In that case, redisplay the window normally. */
16342 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16344 /* W is a mini-buffer window, but it's not active, so clear
16345 it. */
16346 int yb = window_text_bottom_y (w);
16347 struct glyph_row *row;
16348 int y;
16350 for (y = 0, row = w->desired_matrix->rows;
16351 y < yb;
16352 y += row->height, ++row)
16353 blank_row (w, row, y);
16354 goto finish_scroll_bars;
16357 clear_glyph_matrix (w->desired_matrix);
16360 /* Otherwise set up data on this window; select its buffer and point
16361 value. */
16362 /* Really select the buffer, for the sake of buffer-local
16363 variables. */
16364 set_buffer_internal_1 (XBUFFER (w->contents));
16366 current_matrix_up_to_date_p
16367 = (w->window_end_valid
16368 && !current_buffer->clip_changed
16369 && !current_buffer->prevent_redisplay_optimizations_p
16370 && !window_outdated (w));
16372 /* Run the window-text-change-functions
16373 if it is possible that the text on the screen has changed
16374 (either due to modification of the text, or any other reason). */
16375 if (!current_matrix_up_to_date_p
16376 && !NILP (Vwindow_text_change_functions))
16378 safe_run_hooks (Qwindow_text_change_functions);
16379 goto restart;
16382 beg_unchanged = BEG_UNCHANGED;
16383 end_unchanged = END_UNCHANGED;
16385 SET_TEXT_POS (opoint, PT, PT_BYTE);
16387 specbind (Qinhibit_point_motion_hooks, Qt);
16389 buffer_unchanged_p
16390 = (w->window_end_valid
16391 && !current_buffer->clip_changed
16392 && !window_outdated (w));
16394 /* When windows_or_buffers_changed is non-zero, we can't rely
16395 on the window end being valid, so set it to zero there. */
16396 if (windows_or_buffers_changed)
16398 /* If window starts on a continuation line, maybe adjust the
16399 window start in case the window's width changed. */
16400 if (XMARKER (w->start)->buffer == current_buffer)
16401 compute_window_start_on_continuation_line (w);
16403 w->window_end_valid = false;
16404 /* If so, we also can't rely on current matrix
16405 and should not fool try_cursor_movement below. */
16406 current_matrix_up_to_date_p = false;
16409 /* Some sanity checks. */
16410 CHECK_WINDOW_END (w);
16411 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16412 emacs_abort ();
16413 if (BYTEPOS (opoint) < CHARPOS (opoint))
16414 emacs_abort ();
16416 if (mode_line_update_needed (w))
16417 update_mode_line = true;
16419 /* Point refers normally to the selected window. For any other
16420 window, set up appropriate value. */
16421 if (!EQ (window, selected_window))
16423 ptrdiff_t new_pt = marker_position (w->pointm);
16424 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16426 if (new_pt < BEGV)
16428 new_pt = BEGV;
16429 new_pt_byte = BEGV_BYTE;
16430 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16432 else if (new_pt > (ZV - 1))
16434 new_pt = ZV;
16435 new_pt_byte = ZV_BYTE;
16436 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16439 /* We don't use SET_PT so that the point-motion hooks don't run. */
16440 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16443 /* If any of the character widths specified in the display table
16444 have changed, invalidate the width run cache. It's true that
16445 this may be a bit late to catch such changes, but the rest of
16446 redisplay goes (non-fatally) haywire when the display table is
16447 changed, so why should we worry about doing any better? */
16448 if (current_buffer->width_run_cache
16449 || (current_buffer->base_buffer
16450 && current_buffer->base_buffer->width_run_cache))
16452 struct Lisp_Char_Table *disptab = buffer_display_table ();
16454 if (! disptab_matches_widthtab
16455 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16457 struct buffer *buf = current_buffer;
16459 if (buf->base_buffer)
16460 buf = buf->base_buffer;
16461 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16462 recompute_width_table (current_buffer, disptab);
16466 /* If window-start is screwed up, choose a new one. */
16467 if (XMARKER (w->start)->buffer != current_buffer)
16468 goto recenter;
16470 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16472 /* If someone specified a new starting point but did not insist,
16473 check whether it can be used. */
16474 if ((w->optional_new_start || window_frozen_p (w))
16475 && CHARPOS (startp) >= BEGV
16476 && CHARPOS (startp) <= ZV)
16478 ptrdiff_t it_charpos;
16480 w->optional_new_start = false;
16481 start_display (&it, w, startp);
16482 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16483 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16484 /* Record IT's position now, since line_bottom_y might change
16485 that. */
16486 it_charpos = IT_CHARPOS (it);
16487 /* Make sure we set the force_start flag only if the cursor row
16488 will be fully visible. Otherwise, the code under force_start
16489 label below will try to move point back into view, which is
16490 not what the code which sets optional_new_start wants. */
16491 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16492 && !w->force_start)
16494 if (it_charpos == PT)
16495 w->force_start = true;
16496 /* IT may overshoot PT if text at PT is invisible. */
16497 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16498 w->force_start = true;
16499 #ifdef GLYPH_DEBUG
16500 if (w->force_start)
16502 if (window_frozen_p (w))
16503 debug_method_add (w, "set force_start from frozen window start");
16504 else
16505 debug_method_add (w, "set force_start from optional_new_start");
16507 #endif
16511 force_start:
16513 /* Handle case where place to start displaying has been specified,
16514 unless the specified location is outside the accessible range. */
16515 if (w->force_start)
16517 /* We set this later on if we have to adjust point. */
16518 int new_vpos = -1;
16520 w->force_start = false;
16521 w->vscroll = 0;
16522 w->window_end_valid = false;
16524 /* Forget any recorded base line for line number display. */
16525 if (!buffer_unchanged_p)
16526 w->base_line_number = 0;
16528 /* Redisplay the mode line. Select the buffer properly for that.
16529 Also, run the hook window-scroll-functions
16530 because we have scrolled. */
16531 /* Note, we do this after clearing force_start because
16532 if there's an error, it is better to forget about force_start
16533 than to get into an infinite loop calling the hook functions
16534 and having them get more errors. */
16535 if (!update_mode_line
16536 || ! NILP (Vwindow_scroll_functions))
16538 update_mode_line = true;
16539 w->update_mode_line = true;
16540 startp = run_window_scroll_functions (window, startp);
16543 if (CHARPOS (startp) < BEGV)
16544 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16545 else if (CHARPOS (startp) > ZV)
16546 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16548 /* Redisplay, then check if cursor has been set during the
16549 redisplay. Give up if new fonts were loaded. */
16550 /* We used to issue a CHECK_MARGINS argument to try_window here,
16551 but this causes scrolling to fail when point begins inside
16552 the scroll margin (bug#148) -- cyd */
16553 if (!try_window (window, startp, 0))
16555 w->force_start = true;
16556 clear_glyph_matrix (w->desired_matrix);
16557 goto need_larger_matrices;
16560 if (w->cursor.vpos < 0)
16562 /* If point does not appear, try to move point so it does
16563 appear. The desired matrix has been built above, so we
16564 can use it here. First see if point is in invisible
16565 text, and if so, move it to the first visible buffer
16566 position past that. */
16567 struct glyph_row *r = NULL;
16568 Lisp_Object invprop =
16569 get_char_property_and_overlay (make_number (PT), Qinvisible,
16570 Qnil, NULL);
16572 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16574 ptrdiff_t alt_pt;
16575 Lisp_Object invprop_end =
16576 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16577 Qnil, Qnil);
16579 if (NATNUMP (invprop_end))
16580 alt_pt = XFASTINT (invprop_end);
16581 else
16582 alt_pt = ZV;
16583 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16584 NULL, 0);
16586 if (r)
16587 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16588 else /* Give up and just move to the middle of the window. */
16589 new_vpos = window_box_height (w) / 2;
16592 if (!cursor_row_fully_visible_p (w, false, false))
16594 /* Point does appear, but on a line partly visible at end of window.
16595 Move it back to a fully-visible line. */
16596 new_vpos = window_box_height (w);
16597 /* But if window_box_height suggests a Y coordinate that is
16598 not less than we already have, that line will clearly not
16599 be fully visible, so give up and scroll the display.
16600 This can happen when the default face uses a font whose
16601 dimensions are different from the frame's default
16602 font. */
16603 if (new_vpos >= w->cursor.y)
16605 w->cursor.vpos = -1;
16606 clear_glyph_matrix (w->desired_matrix);
16607 goto try_to_scroll;
16610 else if (w->cursor.vpos >= 0)
16612 /* Some people insist on not letting point enter the scroll
16613 margin, even though this part handles windows that didn't
16614 scroll at all. */
16615 int window_total_lines
16616 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16617 int margin = min (scroll_margin, window_total_lines / 4);
16618 int pixel_margin = margin * frame_line_height;
16619 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16621 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16622 below, which finds the row to move point to, advances by
16623 the Y coordinate of the _next_ row, see the definition of
16624 MATRIX_ROW_BOTTOM_Y. */
16625 if (w->cursor.vpos < margin + header_line)
16627 w->cursor.vpos = -1;
16628 clear_glyph_matrix (w->desired_matrix);
16629 goto try_to_scroll;
16631 else
16633 int window_height = window_box_height (w);
16635 if (header_line)
16636 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16637 if (w->cursor.y >= window_height - pixel_margin)
16639 w->cursor.vpos = -1;
16640 clear_glyph_matrix (w->desired_matrix);
16641 goto try_to_scroll;
16646 /* If we need to move point for either of the above reasons,
16647 now actually do it. */
16648 if (new_vpos >= 0)
16650 struct glyph_row *row;
16652 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16653 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16654 ++row;
16656 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16657 MATRIX_ROW_START_BYTEPOS (row));
16659 if (w != XWINDOW (selected_window))
16660 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16661 else if (current_buffer == old)
16662 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16664 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16666 /* Re-run pre-redisplay-function so it can update the region
16667 according to the new position of point. */
16668 /* Other than the cursor, w's redisplay is done so we can set its
16669 redisplay to false. Also the buffer's redisplay can be set to
16670 false, since propagate_buffer_redisplay should have already
16671 propagated its info to `w' anyway. */
16672 w->redisplay = false;
16673 XBUFFER (w->contents)->text->redisplay = false;
16674 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16676 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16678 /* pre-redisplay-function made changes (e.g. move the region)
16679 that require another round of redisplay. */
16680 clear_glyph_matrix (w->desired_matrix);
16681 if (!try_window (window, startp, 0))
16682 goto need_larger_matrices;
16685 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16687 clear_glyph_matrix (w->desired_matrix);
16688 goto try_to_scroll;
16691 #ifdef GLYPH_DEBUG
16692 debug_method_add (w, "forced window start");
16693 #endif
16694 goto done;
16697 /* Handle case where text has not changed, only point, and it has
16698 not moved off the frame, and we are not retrying after hscroll.
16699 (current_matrix_up_to_date_p is true when retrying.) */
16700 if (current_matrix_up_to_date_p
16701 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16702 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16704 switch (rc)
16706 case CURSOR_MOVEMENT_SUCCESS:
16707 used_current_matrix_p = true;
16708 goto done;
16710 case CURSOR_MOVEMENT_MUST_SCROLL:
16711 goto try_to_scroll;
16713 default:
16714 emacs_abort ();
16717 /* If current starting point was originally the beginning of a line
16718 but no longer is, find a new starting point. */
16719 else if (w->start_at_line_beg
16720 && !(CHARPOS (startp) <= BEGV
16721 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16723 #ifdef GLYPH_DEBUG
16724 debug_method_add (w, "recenter 1");
16725 #endif
16726 goto recenter;
16729 /* Try scrolling with try_window_id. Value is > 0 if update has
16730 been done, it is -1 if we know that the same window start will
16731 not work. It is 0 if unsuccessful for some other reason. */
16732 else if ((tem = try_window_id (w)) != 0)
16734 #ifdef GLYPH_DEBUG
16735 debug_method_add (w, "try_window_id %d", tem);
16736 #endif
16738 if (f->fonts_changed)
16739 goto need_larger_matrices;
16740 if (tem > 0)
16741 goto done;
16743 /* Otherwise try_window_id has returned -1 which means that we
16744 don't want the alternative below this comment to execute. */
16746 else if (CHARPOS (startp) >= BEGV
16747 && CHARPOS (startp) <= ZV
16748 && PT >= CHARPOS (startp)
16749 && (CHARPOS (startp) < ZV
16750 /* Avoid starting at end of buffer. */
16751 || CHARPOS (startp) == BEGV
16752 || !window_outdated (w)))
16754 int d1, d2, d5, d6;
16755 int rtop, rbot;
16757 /* If first window line is a continuation line, and window start
16758 is inside the modified region, but the first change is before
16759 current window start, we must select a new window start.
16761 However, if this is the result of a down-mouse event (e.g. by
16762 extending the mouse-drag-overlay), we don't want to select a
16763 new window start, since that would change the position under
16764 the mouse, resulting in an unwanted mouse-movement rather
16765 than a simple mouse-click. */
16766 if (!w->start_at_line_beg
16767 && NILP (do_mouse_tracking)
16768 && CHARPOS (startp) > BEGV
16769 && CHARPOS (startp) > BEG + beg_unchanged
16770 && CHARPOS (startp) <= Z - end_unchanged
16771 /* Even if w->start_at_line_beg is nil, a new window may
16772 start at a line_beg, since that's how set_buffer_window
16773 sets it. So, we need to check the return value of
16774 compute_window_start_on_continuation_line. (See also
16775 bug#197). */
16776 && XMARKER (w->start)->buffer == current_buffer
16777 && compute_window_start_on_continuation_line (w)
16778 /* It doesn't make sense to force the window start like we
16779 do at label force_start if it is already known that point
16780 will not be fully visible in the resulting window, because
16781 doing so will move point from its correct position
16782 instead of scrolling the window to bring point into view.
16783 See bug#9324. */
16784 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16785 /* A very tall row could need more than the window height,
16786 in which case we accept that it is partially visible. */
16787 && (rtop != 0) == (rbot != 0))
16789 w->force_start = true;
16790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16791 #ifdef GLYPH_DEBUG
16792 debug_method_add (w, "recomputed window start in continuation line");
16793 #endif
16794 goto force_start;
16797 #ifdef GLYPH_DEBUG
16798 debug_method_add (w, "same window start");
16799 #endif
16801 /* Try to redisplay starting at same place as before.
16802 If point has not moved off frame, accept the results. */
16803 if (!current_matrix_up_to_date_p
16804 /* Don't use try_window_reusing_current_matrix in this case
16805 because a window scroll function can have changed the
16806 buffer. */
16807 || !NILP (Vwindow_scroll_functions)
16808 || MINI_WINDOW_P (w)
16809 || !(used_current_matrix_p
16810 = try_window_reusing_current_matrix (w)))
16812 IF_DEBUG (debug_method_add (w, "1"));
16813 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16814 /* -1 means we need to scroll.
16815 0 means we need new matrices, but fonts_changed
16816 is set in that case, so we will detect it below. */
16817 goto try_to_scroll;
16820 if (f->fonts_changed)
16821 goto need_larger_matrices;
16823 if (w->cursor.vpos >= 0)
16825 if (!just_this_one_p
16826 || current_buffer->clip_changed
16827 || BEG_UNCHANGED < CHARPOS (startp))
16828 /* Forget any recorded base line for line number display. */
16829 w->base_line_number = 0;
16831 if (!cursor_row_fully_visible_p (w, true, false))
16833 clear_glyph_matrix (w->desired_matrix);
16834 last_line_misfit = true;
16836 /* Drop through and scroll. */
16837 else
16838 goto done;
16840 else
16841 clear_glyph_matrix (w->desired_matrix);
16844 try_to_scroll:
16846 /* Redisplay the mode line. Select the buffer properly for that. */
16847 if (!update_mode_line)
16849 update_mode_line = true;
16850 w->update_mode_line = true;
16853 /* Try to scroll by specified few lines. */
16854 if ((scroll_conservatively
16855 || emacs_scroll_step
16856 || temp_scroll_step
16857 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16858 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16859 && CHARPOS (startp) >= BEGV
16860 && CHARPOS (startp) <= ZV)
16862 /* The function returns -1 if new fonts were loaded, 1 if
16863 successful, 0 if not successful. */
16864 int ss = try_scrolling (window, just_this_one_p,
16865 scroll_conservatively,
16866 emacs_scroll_step,
16867 temp_scroll_step, last_line_misfit);
16868 switch (ss)
16870 case SCROLLING_SUCCESS:
16871 goto done;
16873 case SCROLLING_NEED_LARGER_MATRICES:
16874 goto need_larger_matrices;
16876 case SCROLLING_FAILED:
16877 break;
16879 default:
16880 emacs_abort ();
16884 /* Finally, just choose a place to start which positions point
16885 according to user preferences. */
16887 recenter:
16889 #ifdef GLYPH_DEBUG
16890 debug_method_add (w, "recenter");
16891 #endif
16893 /* Forget any previously recorded base line for line number display. */
16894 if (!buffer_unchanged_p)
16895 w->base_line_number = 0;
16897 /* Determine the window start relative to point. */
16898 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16899 it.current_y = it.last_visible_y;
16900 if (centering_position < 0)
16902 int window_total_lines
16903 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16904 int margin
16905 = scroll_margin > 0
16906 ? min (scroll_margin, window_total_lines / 4)
16907 : 0;
16908 ptrdiff_t margin_pos = CHARPOS (startp);
16909 Lisp_Object aggressive;
16910 bool scrolling_up;
16912 /* If there is a scroll margin at the top of the window, find
16913 its character position. */
16914 if (margin
16915 /* Cannot call start_display if startp is not in the
16916 accessible region of the buffer. This can happen when we
16917 have just switched to a different buffer and/or changed
16918 its restriction. In that case, startp is initialized to
16919 the character position 1 (BEGV) because we did not yet
16920 have chance to display the buffer even once. */
16921 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16923 struct it it1;
16924 void *it1data = NULL;
16926 SAVE_IT (it1, it, it1data);
16927 start_display (&it1, w, startp);
16928 move_it_vertically (&it1, margin * frame_line_height);
16929 margin_pos = IT_CHARPOS (it1);
16930 RESTORE_IT (&it, &it, it1data);
16932 scrolling_up = PT > margin_pos;
16933 aggressive =
16934 scrolling_up
16935 ? BVAR (current_buffer, scroll_up_aggressively)
16936 : BVAR (current_buffer, scroll_down_aggressively);
16938 if (!MINI_WINDOW_P (w)
16939 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16941 int pt_offset = 0;
16943 /* Setting scroll-conservatively overrides
16944 scroll-*-aggressively. */
16945 if (!scroll_conservatively && NUMBERP (aggressive))
16947 double float_amount = XFLOATINT (aggressive);
16949 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16950 if (pt_offset == 0 && float_amount > 0)
16951 pt_offset = 1;
16952 if (pt_offset && margin > 0)
16953 margin -= 1;
16955 /* Compute how much to move the window start backward from
16956 point so that point will be displayed where the user
16957 wants it. */
16958 if (scrolling_up)
16960 centering_position = it.last_visible_y;
16961 if (pt_offset)
16962 centering_position -= pt_offset;
16963 centering_position -=
16964 (frame_line_height * (1 + margin + last_line_misfit)
16965 + WINDOW_HEADER_LINE_HEIGHT (w));
16966 /* Don't let point enter the scroll margin near top of
16967 the window. */
16968 if (centering_position < margin * frame_line_height)
16969 centering_position = margin * frame_line_height;
16971 else
16972 centering_position = margin * frame_line_height + pt_offset;
16974 else
16975 /* Set the window start half the height of the window backward
16976 from point. */
16977 centering_position = window_box_height (w) / 2;
16979 move_it_vertically_backward (&it, centering_position);
16981 eassert (IT_CHARPOS (it) >= BEGV);
16983 /* The function move_it_vertically_backward may move over more
16984 than the specified y-distance. If it->w is small, e.g. a
16985 mini-buffer window, we may end up in front of the window's
16986 display area. Start displaying at the start of the line
16987 containing PT in this case. */
16988 if (it.current_y <= 0)
16990 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16991 move_it_vertically_backward (&it, 0);
16992 it.current_y = 0;
16995 it.current_x = it.hpos = 0;
16997 /* Set the window start position here explicitly, to avoid an
16998 infinite loop in case the functions in window-scroll-functions
16999 get errors. */
17000 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17002 /* Run scroll hooks. */
17003 startp = run_window_scroll_functions (window, it.current.pos);
17005 /* We invoke try_window and try_window_reusing_current_matrix below,
17006 and they manipulate the bidi cache. Save and restore the cache
17007 state of our iterator, so we could continue using it after that. */
17008 itdata = bidi_shelve_cache ();
17010 /* Redisplay the window. */
17011 use_desired_matrix = false;
17012 if (!current_matrix_up_to_date_p
17013 || windows_or_buffers_changed
17014 || f->cursor_type_changed
17015 /* Don't use try_window_reusing_current_matrix in this case
17016 because it can have changed the buffer. */
17017 || !NILP (Vwindow_scroll_functions)
17018 || !just_this_one_p
17019 || MINI_WINDOW_P (w)
17020 || !(used_current_matrix_p
17021 = try_window_reusing_current_matrix (w)))
17022 use_desired_matrix = (try_window (window, startp, 0) == 1);
17024 bidi_unshelve_cache (itdata, false);
17026 /* If new fonts have been loaded (due to fontsets), give up. We
17027 have to start a new redisplay since we need to re-adjust glyph
17028 matrices. */
17029 if (f->fonts_changed)
17030 goto need_larger_matrices;
17032 /* If cursor did not appear assume that the middle of the window is
17033 in the first line of the window. Do it again with the next line.
17034 (Imagine a window of height 100, displaying two lines of height
17035 60. Moving back 50 from it->last_visible_y will end in the first
17036 line.) */
17037 if (w->cursor.vpos < 0)
17039 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17041 clear_glyph_matrix (w->desired_matrix);
17042 move_it_by_lines (&it, 1);
17043 try_window (window, it.current.pos, 0);
17045 else if (PT < IT_CHARPOS (it))
17047 clear_glyph_matrix (w->desired_matrix);
17048 move_it_by_lines (&it, -1);
17049 try_window (window, it.current.pos, 0);
17051 else if (scroll_conservatively > SCROLL_LIMIT
17052 && (it.method == GET_FROM_STRING
17053 || overlay_touches_p (IT_CHARPOS (it)))
17054 && IT_CHARPOS (it) < ZV)
17056 /* If the window starts with a before-string that spans more
17057 than one screen line, using that position to display the
17058 window might fail to bring point into the view, because
17059 start_display will always start by displaying the string,
17060 whereas the code above determines where to set w->start
17061 by the buffer position of the place where it takes screen
17062 coordinates. Try to recover by finding the next screen
17063 line that displays buffer text. */
17064 ptrdiff_t pos0 = IT_CHARPOS (it);
17066 clear_glyph_matrix (w->desired_matrix);
17067 do {
17068 move_it_by_lines (&it, 1);
17069 } while (IT_CHARPOS (it) == pos0);
17070 try_window (window, it.current.pos, 0);
17072 else
17074 /* Not much we can do about it. */
17078 /* Consider the following case: Window starts at BEGV, there is
17079 invisible, intangible text at BEGV, so that display starts at
17080 some point START > BEGV. It can happen that we are called with
17081 PT somewhere between BEGV and START. Try to handle that case,
17082 and similar ones. */
17083 if (w->cursor.vpos < 0)
17085 /* Prefer the desired matrix to the current matrix, if possible,
17086 in the fallback calculations below. This is because using
17087 the current matrix might completely goof, e.g. if its first
17088 row is after point. */
17089 struct glyph_matrix *matrix =
17090 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17091 /* First, try locating the proper glyph row for PT. */
17092 struct glyph_row *row =
17093 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17095 /* Sometimes point is at the beginning of invisible text that is
17096 before the 1st character displayed in the row. In that case,
17097 row_containing_pos fails to find the row, because no glyphs
17098 with appropriate buffer positions are present in the row.
17099 Therefore, we next try to find the row which shows the 1st
17100 position after the invisible text. */
17101 if (!row)
17103 Lisp_Object val =
17104 get_char_property_and_overlay (make_number (PT), Qinvisible,
17105 Qnil, NULL);
17107 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17109 ptrdiff_t alt_pos;
17110 Lisp_Object invis_end =
17111 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17112 Qnil, Qnil);
17114 if (NATNUMP (invis_end))
17115 alt_pos = XFASTINT (invis_end);
17116 else
17117 alt_pos = ZV;
17118 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17121 /* Finally, fall back on the first row of the window after the
17122 header line (if any). This is slightly better than not
17123 displaying the cursor at all. */
17124 if (!row)
17126 row = matrix->rows;
17127 if (row->mode_line_p)
17128 ++row;
17130 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17133 if (!cursor_row_fully_visible_p (w, false, false))
17135 /* If vscroll is enabled, disable it and try again. */
17136 if (w->vscroll)
17138 w->vscroll = 0;
17139 clear_glyph_matrix (w->desired_matrix);
17140 goto recenter;
17143 /* Users who set scroll-conservatively to a large number want
17144 point just above/below the scroll margin. If we ended up
17145 with point's row partially visible, move the window start to
17146 make that row fully visible and out of the margin. */
17147 if (scroll_conservatively > SCROLL_LIMIT)
17149 int window_total_lines
17150 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17151 int margin =
17152 scroll_margin > 0
17153 ? min (scroll_margin, window_total_lines / 4)
17154 : 0;
17155 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17157 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17158 clear_glyph_matrix (w->desired_matrix);
17159 if (1 == try_window (window, it.current.pos,
17160 TRY_WINDOW_CHECK_MARGINS))
17161 goto done;
17164 /* If centering point failed to make the whole line visible,
17165 put point at the top instead. That has to make the whole line
17166 visible, if it can be done. */
17167 if (centering_position == 0)
17168 goto done;
17170 clear_glyph_matrix (w->desired_matrix);
17171 centering_position = 0;
17172 goto recenter;
17175 done:
17177 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17178 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17179 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17181 /* Display the mode line, if we must. */
17182 if ((update_mode_line
17183 /* If window not full width, must redo its mode line
17184 if (a) the window to its side is being redone and
17185 (b) we do a frame-based redisplay. This is a consequence
17186 of how inverted lines are drawn in frame-based redisplay. */
17187 || (!just_this_one_p
17188 && !FRAME_WINDOW_P (f)
17189 && !WINDOW_FULL_WIDTH_P (w))
17190 /* Line number to display. */
17191 || w->base_line_pos > 0
17192 /* Column number is displayed and different from the one displayed. */
17193 || (w->column_number_displayed != -1
17194 && (w->column_number_displayed != current_column ())))
17195 /* This means that the window has a mode line. */
17196 && (WINDOW_WANTS_MODELINE_P (w)
17197 || WINDOW_WANTS_HEADER_LINE_P (w)))
17200 display_mode_lines (w);
17202 /* If mode line height has changed, arrange for a thorough
17203 immediate redisplay using the correct mode line height. */
17204 if (WINDOW_WANTS_MODELINE_P (w)
17205 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17207 f->fonts_changed = true;
17208 w->mode_line_height = -1;
17209 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17210 = DESIRED_MODE_LINE_HEIGHT (w);
17213 /* If header line height has changed, arrange for a thorough
17214 immediate redisplay using the correct header line height. */
17215 if (WINDOW_WANTS_HEADER_LINE_P (w)
17216 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17218 f->fonts_changed = true;
17219 w->header_line_height = -1;
17220 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17221 = DESIRED_HEADER_LINE_HEIGHT (w);
17224 if (f->fonts_changed)
17225 goto need_larger_matrices;
17228 if (!line_number_displayed && w->base_line_pos != -1)
17230 w->base_line_pos = 0;
17231 w->base_line_number = 0;
17234 finish_menu_bars:
17236 /* When we reach a frame's selected window, redo the frame's menu
17237 bar and the frame's title. */
17238 if (update_mode_line
17239 && EQ (FRAME_SELECTED_WINDOW (f), window))
17241 bool redisplay_menu_p;
17243 if (FRAME_WINDOW_P (f))
17245 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17246 || defined (HAVE_NS) || defined (USE_GTK)
17247 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17248 #else
17249 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17250 #endif
17252 else
17253 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17255 if (redisplay_menu_p)
17256 display_menu_bar (w);
17258 #ifdef HAVE_WINDOW_SYSTEM
17259 if (FRAME_WINDOW_P (f))
17261 #if defined (USE_GTK) || defined (HAVE_NS)
17262 if (FRAME_EXTERNAL_TOOL_BAR (f))
17263 redisplay_tool_bar (f);
17264 #else
17265 if (WINDOWP (f->tool_bar_window)
17266 && (FRAME_TOOL_BAR_LINES (f) > 0
17267 || !NILP (Vauto_resize_tool_bars))
17268 && redisplay_tool_bar (f))
17269 ignore_mouse_drag_p = true;
17270 #endif
17272 x_consider_frame_title (w->frame);
17273 #endif
17276 #ifdef HAVE_WINDOW_SYSTEM
17277 if (FRAME_WINDOW_P (f)
17278 && update_window_fringes (w, (just_this_one_p
17279 || (!used_current_matrix_p && !overlay_arrow_seen)
17280 || w->pseudo_window_p)))
17282 update_begin (f);
17283 block_input ();
17284 if (draw_window_fringes (w, true))
17286 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17287 x_draw_right_divider (w);
17288 else
17289 x_draw_vertical_border (w);
17291 unblock_input ();
17292 update_end (f);
17295 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17296 x_draw_bottom_divider (w);
17297 #endif /* HAVE_WINDOW_SYSTEM */
17299 /* We go to this label, with fonts_changed set, if it is
17300 necessary to try again using larger glyph matrices.
17301 We have to redeem the scroll bar even in this case,
17302 because the loop in redisplay_internal expects that. */
17303 need_larger_matrices:
17305 finish_scroll_bars:
17307 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17309 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17310 /* Set the thumb's position and size. */
17311 set_vertical_scroll_bar (w);
17313 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17314 /* Set the thumb's position and size. */
17315 set_horizontal_scroll_bar (w);
17317 /* Note that we actually used the scroll bar attached to this
17318 window, so it shouldn't be deleted at the end of redisplay. */
17319 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17320 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17323 /* Restore current_buffer and value of point in it. The window
17324 update may have changed the buffer, so first make sure `opoint'
17325 is still valid (Bug#6177). */
17326 if (CHARPOS (opoint) < BEGV)
17327 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17328 else if (CHARPOS (opoint) > ZV)
17329 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17330 else
17331 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17333 set_buffer_internal_1 (old);
17334 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17335 shorter. This can be caused by log truncation in *Messages*. */
17336 if (CHARPOS (lpoint) <= ZV)
17337 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17339 unbind_to (count, Qnil);
17343 /* Build the complete desired matrix of WINDOW with a window start
17344 buffer position POS.
17346 Value is 1 if successful. It is zero if fonts were loaded during
17347 redisplay which makes re-adjusting glyph matrices necessary, and -1
17348 if point would appear in the scroll margins.
17349 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17350 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17351 set in FLAGS.) */
17354 try_window (Lisp_Object window, struct text_pos pos, int flags)
17356 struct window *w = XWINDOW (window);
17357 struct it it;
17358 struct glyph_row *last_text_row = NULL;
17359 struct frame *f = XFRAME (w->frame);
17360 int frame_line_height = default_line_pixel_height (w);
17362 /* Make POS the new window start. */
17363 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17365 /* Mark cursor position as unknown. No overlay arrow seen. */
17366 w->cursor.vpos = -1;
17367 overlay_arrow_seen = false;
17369 /* Initialize iterator and info to start at POS. */
17370 start_display (&it, w, pos);
17371 it.glyph_row->reversed_p = false;
17373 /* Display all lines of W. */
17374 while (it.current_y < it.last_visible_y)
17376 if (display_line (&it))
17377 last_text_row = it.glyph_row - 1;
17378 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17379 return 0;
17382 /* Don't let the cursor end in the scroll margins. */
17383 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17384 && !MINI_WINDOW_P (w))
17386 int this_scroll_margin;
17387 int window_total_lines
17388 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17390 if (scroll_margin > 0)
17392 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17393 this_scroll_margin *= frame_line_height;
17395 else
17396 this_scroll_margin = 0;
17398 if ((w->cursor.y >= 0 /* not vscrolled */
17399 && w->cursor.y < this_scroll_margin
17400 && CHARPOS (pos) > BEGV
17401 && IT_CHARPOS (it) < ZV)
17402 /* rms: considering make_cursor_line_fully_visible_p here
17403 seems to give wrong results. We don't want to recenter
17404 when the last line is partly visible, we want to allow
17405 that case to be handled in the usual way. */
17406 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17408 w->cursor.vpos = -1;
17409 clear_glyph_matrix (w->desired_matrix);
17410 return -1;
17414 /* If bottom moved off end of frame, change mode line percentage. */
17415 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17416 w->update_mode_line = true;
17418 /* Set window_end_pos to the offset of the last character displayed
17419 on the window from the end of current_buffer. Set
17420 window_end_vpos to its row number. */
17421 if (last_text_row)
17423 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17424 adjust_window_ends (w, last_text_row, false);
17425 eassert
17426 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17427 w->window_end_vpos)));
17429 else
17431 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17432 w->window_end_pos = Z - ZV;
17433 w->window_end_vpos = 0;
17436 /* But that is not valid info until redisplay finishes. */
17437 w->window_end_valid = false;
17438 return 1;
17443 /************************************************************************
17444 Window redisplay reusing current matrix when buffer has not changed
17445 ************************************************************************/
17447 /* Try redisplay of window W showing an unchanged buffer with a
17448 different window start than the last time it was displayed by
17449 reusing its current matrix. Value is true if successful.
17450 W->start is the new window start. */
17452 static bool
17453 try_window_reusing_current_matrix (struct window *w)
17455 struct frame *f = XFRAME (w->frame);
17456 struct glyph_row *bottom_row;
17457 struct it it;
17458 struct run run;
17459 struct text_pos start, new_start;
17460 int nrows_scrolled, i;
17461 struct glyph_row *last_text_row;
17462 struct glyph_row *last_reused_text_row;
17463 struct glyph_row *start_row;
17464 int start_vpos, min_y, max_y;
17466 #ifdef GLYPH_DEBUG
17467 if (inhibit_try_window_reusing)
17468 return false;
17469 #endif
17471 if (/* This function doesn't handle terminal frames. */
17472 !FRAME_WINDOW_P (f)
17473 /* Don't try to reuse the display if windows have been split
17474 or such. */
17475 || windows_or_buffers_changed
17476 || f->cursor_type_changed)
17477 return false;
17479 /* Can't do this if showing trailing whitespace. */
17480 if (!NILP (Vshow_trailing_whitespace))
17481 return false;
17483 /* If top-line visibility has changed, give up. */
17484 if (WINDOW_WANTS_HEADER_LINE_P (w)
17485 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17486 return false;
17488 /* Give up if old or new display is scrolled vertically. We could
17489 make this function handle this, but right now it doesn't. */
17490 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17491 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17492 return false;
17494 /* The variable new_start now holds the new window start. The old
17495 start `start' can be determined from the current matrix. */
17496 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17497 start = start_row->minpos;
17498 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17500 /* Clear the desired matrix for the display below. */
17501 clear_glyph_matrix (w->desired_matrix);
17503 if (CHARPOS (new_start) <= CHARPOS (start))
17505 /* Don't use this method if the display starts with an ellipsis
17506 displayed for invisible text. It's not easy to handle that case
17507 below, and it's certainly not worth the effort since this is
17508 not a frequent case. */
17509 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17510 return false;
17512 IF_DEBUG (debug_method_add (w, "twu1"));
17514 /* Display up to a row that can be reused. The variable
17515 last_text_row is set to the last row displayed that displays
17516 text. Note that it.vpos == 0 if or if not there is a
17517 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17518 start_display (&it, w, new_start);
17519 w->cursor.vpos = -1;
17520 last_text_row = last_reused_text_row = NULL;
17522 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17524 /* If we have reached into the characters in the START row,
17525 that means the line boundaries have changed. So we
17526 can't start copying with the row START. Maybe it will
17527 work to start copying with the following row. */
17528 while (IT_CHARPOS (it) > CHARPOS (start))
17530 /* Advance to the next row as the "start". */
17531 start_row++;
17532 start = start_row->minpos;
17533 /* If there are no more rows to try, or just one, give up. */
17534 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17535 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17536 || CHARPOS (start) == ZV)
17538 clear_glyph_matrix (w->desired_matrix);
17539 return false;
17542 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17544 /* If we have reached alignment, we can copy the rest of the
17545 rows. */
17546 if (IT_CHARPOS (it) == CHARPOS (start)
17547 /* Don't accept "alignment" inside a display vector,
17548 since start_row could have started in the middle of
17549 that same display vector (thus their character
17550 positions match), and we have no way of telling if
17551 that is the case. */
17552 && it.current.dpvec_index < 0)
17553 break;
17555 it.glyph_row->reversed_p = false;
17556 if (display_line (&it))
17557 last_text_row = it.glyph_row - 1;
17561 /* A value of current_y < last_visible_y means that we stopped
17562 at the previous window start, which in turn means that we
17563 have at least one reusable row. */
17564 if (it.current_y < it.last_visible_y)
17566 struct glyph_row *row;
17568 /* IT.vpos always starts from 0; it counts text lines. */
17569 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17571 /* Find PT if not already found in the lines displayed. */
17572 if (w->cursor.vpos < 0)
17574 int dy = it.current_y - start_row->y;
17576 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17577 row = row_containing_pos (w, PT, row, NULL, dy);
17578 if (row)
17579 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17580 dy, nrows_scrolled);
17581 else
17583 clear_glyph_matrix (w->desired_matrix);
17584 return false;
17588 /* Scroll the display. Do it before the current matrix is
17589 changed. The problem here is that update has not yet
17590 run, i.e. part of the current matrix is not up to date.
17591 scroll_run_hook will clear the cursor, and use the
17592 current matrix to get the height of the row the cursor is
17593 in. */
17594 run.current_y = start_row->y;
17595 run.desired_y = it.current_y;
17596 run.height = it.last_visible_y - it.current_y;
17598 if (run.height > 0 && run.current_y != run.desired_y)
17600 update_begin (f);
17601 FRAME_RIF (f)->update_window_begin_hook (w);
17602 FRAME_RIF (f)->clear_window_mouse_face (w);
17603 FRAME_RIF (f)->scroll_run_hook (w, &run);
17604 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17605 update_end (f);
17608 /* Shift current matrix down by nrows_scrolled lines. */
17609 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17610 rotate_matrix (w->current_matrix,
17611 start_vpos,
17612 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17613 nrows_scrolled);
17615 /* Disable lines that must be updated. */
17616 for (i = 0; i < nrows_scrolled; ++i)
17617 (start_row + i)->enabled_p = false;
17619 /* Re-compute Y positions. */
17620 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17621 max_y = it.last_visible_y;
17622 for (row = start_row + nrows_scrolled;
17623 row < bottom_row;
17624 ++row)
17626 row->y = it.current_y;
17627 row->visible_height = row->height;
17629 if (row->y < min_y)
17630 row->visible_height -= min_y - row->y;
17631 if (row->y + row->height > max_y)
17632 row->visible_height -= row->y + row->height - max_y;
17633 if (row->fringe_bitmap_periodic_p)
17634 row->redraw_fringe_bitmaps_p = true;
17636 it.current_y += row->height;
17638 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17639 last_reused_text_row = row;
17640 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17641 break;
17644 /* Disable lines in the current matrix which are now
17645 below the window. */
17646 for (++row; row < bottom_row; ++row)
17647 row->enabled_p = row->mode_line_p = false;
17650 /* Update window_end_pos etc.; last_reused_text_row is the last
17651 reused row from the current matrix containing text, if any.
17652 The value of last_text_row is the last displayed line
17653 containing text. */
17654 if (last_reused_text_row)
17655 adjust_window_ends (w, last_reused_text_row, true);
17656 else if (last_text_row)
17657 adjust_window_ends (w, last_text_row, false);
17658 else
17660 /* This window must be completely empty. */
17661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17662 w->window_end_pos = Z - ZV;
17663 w->window_end_vpos = 0;
17665 w->window_end_valid = false;
17667 /* Update hint: don't try scrolling again in update_window. */
17668 w->desired_matrix->no_scrolling_p = true;
17670 #ifdef GLYPH_DEBUG
17671 debug_method_add (w, "try_window_reusing_current_matrix 1");
17672 #endif
17673 return true;
17675 else if (CHARPOS (new_start) > CHARPOS (start))
17677 struct glyph_row *pt_row, *row;
17678 struct glyph_row *first_reusable_row;
17679 struct glyph_row *first_row_to_display;
17680 int dy;
17681 int yb = window_text_bottom_y (w);
17683 /* Find the row starting at new_start, if there is one. Don't
17684 reuse a partially visible line at the end. */
17685 first_reusable_row = start_row;
17686 while (first_reusable_row->enabled_p
17687 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17688 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17689 < CHARPOS (new_start)))
17690 ++first_reusable_row;
17692 /* Give up if there is no row to reuse. */
17693 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17694 || !first_reusable_row->enabled_p
17695 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17696 != CHARPOS (new_start)))
17697 return false;
17699 /* We can reuse fully visible rows beginning with
17700 first_reusable_row to the end of the window. Set
17701 first_row_to_display to the first row that cannot be reused.
17702 Set pt_row to the row containing point, if there is any. */
17703 pt_row = NULL;
17704 for (first_row_to_display = first_reusable_row;
17705 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17706 ++first_row_to_display)
17708 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17709 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17710 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17711 && first_row_to_display->ends_at_zv_p
17712 && pt_row == NULL)))
17713 pt_row = first_row_to_display;
17716 /* Start displaying at the start of first_row_to_display. */
17717 eassert (first_row_to_display->y < yb);
17718 init_to_row_start (&it, w, first_row_to_display);
17720 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17721 - start_vpos);
17722 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17723 - nrows_scrolled);
17724 it.current_y = (first_row_to_display->y - first_reusable_row->y
17725 + WINDOW_HEADER_LINE_HEIGHT (w));
17727 /* Display lines beginning with first_row_to_display in the
17728 desired matrix. Set last_text_row to the last row displayed
17729 that displays text. */
17730 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17731 if (pt_row == NULL)
17732 w->cursor.vpos = -1;
17733 last_text_row = NULL;
17734 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17735 if (display_line (&it))
17736 last_text_row = it.glyph_row - 1;
17738 /* If point is in a reused row, adjust y and vpos of the cursor
17739 position. */
17740 if (pt_row)
17742 w->cursor.vpos -= nrows_scrolled;
17743 w->cursor.y -= first_reusable_row->y - start_row->y;
17746 /* Give up if point isn't in a row displayed or reused. (This
17747 also handles the case where w->cursor.vpos < nrows_scrolled
17748 after the calls to display_line, which can happen with scroll
17749 margins. See bug#1295.) */
17750 if (w->cursor.vpos < 0)
17752 clear_glyph_matrix (w->desired_matrix);
17753 return false;
17756 /* Scroll the display. */
17757 run.current_y = first_reusable_row->y;
17758 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17759 run.height = it.last_visible_y - run.current_y;
17760 dy = run.current_y - run.desired_y;
17762 if (run.height)
17764 update_begin (f);
17765 FRAME_RIF (f)->update_window_begin_hook (w);
17766 FRAME_RIF (f)->clear_window_mouse_face (w);
17767 FRAME_RIF (f)->scroll_run_hook (w, &run);
17768 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17769 update_end (f);
17772 /* Adjust Y positions of reused rows. */
17773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17774 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17775 max_y = it.last_visible_y;
17776 for (row = first_reusable_row; row < first_row_to_display; ++row)
17778 row->y -= dy;
17779 row->visible_height = row->height;
17780 if (row->y < min_y)
17781 row->visible_height -= min_y - row->y;
17782 if (row->y + row->height > max_y)
17783 row->visible_height -= row->y + row->height - max_y;
17784 if (row->fringe_bitmap_periodic_p)
17785 row->redraw_fringe_bitmaps_p = true;
17788 /* Scroll the current matrix. */
17789 eassert (nrows_scrolled > 0);
17790 rotate_matrix (w->current_matrix,
17791 start_vpos,
17792 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17793 -nrows_scrolled);
17795 /* Disable rows not reused. */
17796 for (row -= nrows_scrolled; row < bottom_row; ++row)
17797 row->enabled_p = false;
17799 /* Point may have moved to a different line, so we cannot assume that
17800 the previous cursor position is valid; locate the correct row. */
17801 if (pt_row)
17803 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17804 row < bottom_row
17805 && PT >= MATRIX_ROW_END_CHARPOS (row)
17806 && !row->ends_at_zv_p;
17807 row++)
17809 w->cursor.vpos++;
17810 w->cursor.y = row->y;
17812 if (row < bottom_row)
17814 /* Can't simply scan the row for point with
17815 bidi-reordered glyph rows. Let set_cursor_from_row
17816 figure out where to put the cursor, and if it fails,
17817 give up. */
17818 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17820 if (!set_cursor_from_row (w, row, w->current_matrix,
17821 0, 0, 0, 0))
17823 clear_glyph_matrix (w->desired_matrix);
17824 return false;
17827 else
17829 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17830 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17832 for (; glyph < end
17833 && (!BUFFERP (glyph->object)
17834 || glyph->charpos < PT);
17835 glyph++)
17837 w->cursor.hpos++;
17838 w->cursor.x += glyph->pixel_width;
17844 /* Adjust window end. A null value of last_text_row means that
17845 the window end is in reused rows which in turn means that
17846 only its vpos can have changed. */
17847 if (last_text_row)
17848 adjust_window_ends (w, last_text_row, false);
17849 else
17850 w->window_end_vpos -= nrows_scrolled;
17852 w->window_end_valid = false;
17853 w->desired_matrix->no_scrolling_p = true;
17855 #ifdef GLYPH_DEBUG
17856 debug_method_add (w, "try_window_reusing_current_matrix 2");
17857 #endif
17858 return true;
17861 return false;
17866 /************************************************************************
17867 Window redisplay reusing current matrix when buffer has changed
17868 ************************************************************************/
17870 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17871 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17872 ptrdiff_t *, ptrdiff_t *);
17873 static struct glyph_row *
17874 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17875 struct glyph_row *);
17878 /* Return the last row in MATRIX displaying text. If row START is
17879 non-null, start searching with that row. IT gives the dimensions
17880 of the display. Value is null if matrix is empty; otherwise it is
17881 a pointer to the row found. */
17883 static struct glyph_row *
17884 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17885 struct glyph_row *start)
17887 struct glyph_row *row, *row_found;
17889 /* Set row_found to the last row in IT->w's current matrix
17890 displaying text. The loop looks funny but think of partially
17891 visible lines. */
17892 row_found = NULL;
17893 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17894 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17896 eassert (row->enabled_p);
17897 row_found = row;
17898 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17899 break;
17900 ++row;
17903 return row_found;
17907 /* Return the last row in the current matrix of W that is not affected
17908 by changes at the start of current_buffer that occurred since W's
17909 current matrix was built. Value is null if no such row exists.
17911 BEG_UNCHANGED us the number of characters unchanged at the start of
17912 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17913 first changed character in current_buffer. Characters at positions <
17914 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17915 when the current matrix was built. */
17917 static struct glyph_row *
17918 find_last_unchanged_at_beg_row (struct window *w)
17920 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17921 struct glyph_row *row;
17922 struct glyph_row *row_found = NULL;
17923 int yb = window_text_bottom_y (w);
17925 /* Find the last row displaying unchanged text. */
17926 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17927 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17928 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17929 ++row)
17931 if (/* If row ends before first_changed_pos, it is unchanged,
17932 except in some case. */
17933 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17934 /* When row ends in ZV and we write at ZV it is not
17935 unchanged. */
17936 && !row->ends_at_zv_p
17937 /* When first_changed_pos is the end of a continued line,
17938 row is not unchanged because it may be no longer
17939 continued. */
17940 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17941 && (row->continued_p
17942 || row->exact_window_width_line_p))
17943 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17944 needs to be recomputed, so don't consider this row as
17945 unchanged. This happens when the last line was
17946 bidi-reordered and was killed immediately before this
17947 redisplay cycle. In that case, ROW->end stores the
17948 buffer position of the first visual-order character of
17949 the killed text, which is now beyond ZV. */
17950 && CHARPOS (row->end.pos) <= ZV)
17951 row_found = row;
17953 /* Stop if last visible row. */
17954 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17955 break;
17958 return row_found;
17962 /* Find the first glyph row in the current matrix of W that is not
17963 affected by changes at the end of current_buffer since the
17964 time W's current matrix was built.
17966 Return in *DELTA the number of chars by which buffer positions in
17967 unchanged text at the end of current_buffer must be adjusted.
17969 Return in *DELTA_BYTES the corresponding number of bytes.
17971 Value is null if no such row exists, i.e. all rows are affected by
17972 changes. */
17974 static struct glyph_row *
17975 find_first_unchanged_at_end_row (struct window *w,
17976 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17978 struct glyph_row *row;
17979 struct glyph_row *row_found = NULL;
17981 *delta = *delta_bytes = 0;
17983 /* Display must not have been paused, otherwise the current matrix
17984 is not up to date. */
17985 eassert (w->window_end_valid);
17987 /* A value of window_end_pos >= END_UNCHANGED means that the window
17988 end is in the range of changed text. If so, there is no
17989 unchanged row at the end of W's current matrix. */
17990 if (w->window_end_pos >= END_UNCHANGED)
17991 return NULL;
17993 /* Set row to the last row in W's current matrix displaying text. */
17994 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17996 /* If matrix is entirely empty, no unchanged row exists. */
17997 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17999 /* The value of row is the last glyph row in the matrix having a
18000 meaningful buffer position in it. The end position of row
18001 corresponds to window_end_pos. This allows us to translate
18002 buffer positions in the current matrix to current buffer
18003 positions for characters not in changed text. */
18004 ptrdiff_t Z_old =
18005 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18006 ptrdiff_t Z_BYTE_old =
18007 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18008 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18009 struct glyph_row *first_text_row
18010 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18012 *delta = Z - Z_old;
18013 *delta_bytes = Z_BYTE - Z_BYTE_old;
18015 /* Set last_unchanged_pos to the buffer position of the last
18016 character in the buffer that has not been changed. Z is the
18017 index + 1 of the last character in current_buffer, i.e. by
18018 subtracting END_UNCHANGED we get the index of the last
18019 unchanged character, and we have to add BEG to get its buffer
18020 position. */
18021 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18022 last_unchanged_pos_old = last_unchanged_pos - *delta;
18024 /* Search backward from ROW for a row displaying a line that
18025 starts at a minimum position >= last_unchanged_pos_old. */
18026 for (; row > first_text_row; --row)
18028 /* This used to abort, but it can happen.
18029 It is ok to just stop the search instead here. KFS. */
18030 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18031 break;
18033 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18034 row_found = row;
18038 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18040 return row_found;
18044 /* Make sure that glyph rows in the current matrix of window W
18045 reference the same glyph memory as corresponding rows in the
18046 frame's frame matrix. This function is called after scrolling W's
18047 current matrix on a terminal frame in try_window_id and
18048 try_window_reusing_current_matrix. */
18050 static void
18051 sync_frame_with_window_matrix_rows (struct window *w)
18053 struct frame *f = XFRAME (w->frame);
18054 struct glyph_row *window_row, *window_row_end, *frame_row;
18056 /* Preconditions: W must be a leaf window and full-width. Its frame
18057 must have a frame matrix. */
18058 eassert (BUFFERP (w->contents));
18059 eassert (WINDOW_FULL_WIDTH_P (w));
18060 eassert (!FRAME_WINDOW_P (f));
18062 /* If W is a full-width window, glyph pointers in W's current matrix
18063 have, by definition, to be the same as glyph pointers in the
18064 corresponding frame matrix. Note that frame matrices have no
18065 marginal areas (see build_frame_matrix). */
18066 window_row = w->current_matrix->rows;
18067 window_row_end = window_row + w->current_matrix->nrows;
18068 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18069 while (window_row < window_row_end)
18071 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18072 struct glyph *end = window_row->glyphs[LAST_AREA];
18074 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18075 frame_row->glyphs[TEXT_AREA] = start;
18076 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18077 frame_row->glyphs[LAST_AREA] = end;
18079 /* Disable frame rows whose corresponding window rows have
18080 been disabled in try_window_id. */
18081 if (!window_row->enabled_p)
18082 frame_row->enabled_p = false;
18084 ++window_row, ++frame_row;
18089 /* Find the glyph row in window W containing CHARPOS. Consider all
18090 rows between START and END (not inclusive). END null means search
18091 all rows to the end of the display area of W. Value is the row
18092 containing CHARPOS or null. */
18094 struct glyph_row *
18095 row_containing_pos (struct window *w, ptrdiff_t charpos,
18096 struct glyph_row *start, struct glyph_row *end, int dy)
18098 struct glyph_row *row = start;
18099 struct glyph_row *best_row = NULL;
18100 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18101 int last_y;
18103 /* If we happen to start on a header-line, skip that. */
18104 if (row->mode_line_p)
18105 ++row;
18107 if ((end && row >= end) || !row->enabled_p)
18108 return NULL;
18110 last_y = window_text_bottom_y (w) - dy;
18112 while (true)
18114 /* Give up if we have gone too far. */
18115 if ((end && row >= end) || !row->enabled_p)
18116 return NULL;
18117 /* This formerly returned if they were equal.
18118 I think that both quantities are of a "last plus one" type;
18119 if so, when they are equal, the row is within the screen. -- rms. */
18120 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18121 return NULL;
18123 /* If it is in this row, return this row. */
18124 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18125 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18126 /* The end position of a row equals the start
18127 position of the next row. If CHARPOS is there, we
18128 would rather consider it displayed in the next
18129 line, except when this line ends in ZV. */
18130 && !row_for_charpos_p (row, charpos)))
18131 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18133 struct glyph *g;
18135 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18136 || (!best_row && !row->continued_p))
18137 return row;
18138 /* In bidi-reordered rows, there could be several rows whose
18139 edges surround CHARPOS, all of these rows belonging to
18140 the same continued line. We need to find the row which
18141 fits CHARPOS the best. */
18142 for (g = row->glyphs[TEXT_AREA];
18143 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18144 g++)
18146 if (!STRINGP (g->object))
18148 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18150 mindif = eabs (g->charpos - charpos);
18151 best_row = row;
18152 /* Exact match always wins. */
18153 if (mindif == 0)
18154 return best_row;
18159 else if (best_row && !row->continued_p)
18160 return best_row;
18161 ++row;
18166 /* Try to redisplay window W by reusing its existing display. W's
18167 current matrix must be up to date when this function is called,
18168 i.e., window_end_valid must be true.
18170 Value is
18172 >= 1 if successful, i.e. display has been updated
18173 specifically:
18174 1 means the changes were in front of a newline that precedes
18175 the window start, and the whole current matrix was reused
18176 2 means the changes were after the last position displayed
18177 in the window, and the whole current matrix was reused
18178 3 means portions of the current matrix were reused, while
18179 some of the screen lines were redrawn
18180 -1 if redisplay with same window start is known not to succeed
18181 0 if otherwise unsuccessful
18183 The following steps are performed:
18185 1. Find the last row in the current matrix of W that is not
18186 affected by changes at the start of current_buffer. If no such row
18187 is found, give up.
18189 2. Find the first row in W's current matrix that is not affected by
18190 changes at the end of current_buffer. Maybe there is no such row.
18192 3. Display lines beginning with the row + 1 found in step 1 to the
18193 row found in step 2 or, if step 2 didn't find a row, to the end of
18194 the window.
18196 4. If cursor is not known to appear on the window, give up.
18198 5. If display stopped at the row found in step 2, scroll the
18199 display and current matrix as needed.
18201 6. Maybe display some lines at the end of W, if we must. This can
18202 happen under various circumstances, like a partially visible line
18203 becoming fully visible, or because newly displayed lines are displayed
18204 in smaller font sizes.
18206 7. Update W's window end information. */
18208 static int
18209 try_window_id (struct window *w)
18211 struct frame *f = XFRAME (w->frame);
18212 struct glyph_matrix *current_matrix = w->current_matrix;
18213 struct glyph_matrix *desired_matrix = w->desired_matrix;
18214 struct glyph_row *last_unchanged_at_beg_row;
18215 struct glyph_row *first_unchanged_at_end_row;
18216 struct glyph_row *row;
18217 struct glyph_row *bottom_row;
18218 int bottom_vpos;
18219 struct it it;
18220 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18221 int dvpos, dy;
18222 struct text_pos start_pos;
18223 struct run run;
18224 int first_unchanged_at_end_vpos = 0;
18225 struct glyph_row *last_text_row, *last_text_row_at_end;
18226 struct text_pos start;
18227 ptrdiff_t first_changed_charpos, last_changed_charpos;
18229 #ifdef GLYPH_DEBUG
18230 if (inhibit_try_window_id)
18231 return 0;
18232 #endif
18234 /* This is handy for debugging. */
18235 #if false
18236 #define GIVE_UP(X) \
18237 do { \
18238 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18239 return 0; \
18240 } while (false)
18241 #else
18242 #define GIVE_UP(X) return 0
18243 #endif
18245 SET_TEXT_POS_FROM_MARKER (start, w->start);
18247 /* Don't use this for mini-windows because these can show
18248 messages and mini-buffers, and we don't handle that here. */
18249 if (MINI_WINDOW_P (w))
18250 GIVE_UP (1);
18252 /* This flag is used to prevent redisplay optimizations. */
18253 if (windows_or_buffers_changed || f->cursor_type_changed)
18254 GIVE_UP (2);
18256 /* This function's optimizations cannot be used if overlays have
18257 changed in the buffer displayed by the window, so give up if they
18258 have. */
18259 if (w->last_overlay_modified != OVERLAY_MODIFF)
18260 GIVE_UP (200);
18262 /* Verify that narrowing has not changed.
18263 Also verify that we were not told to prevent redisplay optimizations.
18264 It would be nice to further
18265 reduce the number of cases where this prevents try_window_id. */
18266 if (current_buffer->clip_changed
18267 || current_buffer->prevent_redisplay_optimizations_p)
18268 GIVE_UP (3);
18270 /* Window must either use window-based redisplay or be full width. */
18271 if (!FRAME_WINDOW_P (f)
18272 && (!FRAME_LINE_INS_DEL_OK (f)
18273 || !WINDOW_FULL_WIDTH_P (w)))
18274 GIVE_UP (4);
18276 /* Give up if point is known NOT to appear in W. */
18277 if (PT < CHARPOS (start))
18278 GIVE_UP (5);
18280 /* Another way to prevent redisplay optimizations. */
18281 if (w->last_modified == 0)
18282 GIVE_UP (6);
18284 /* Verify that window is not hscrolled. */
18285 if (w->hscroll != 0)
18286 GIVE_UP (7);
18288 /* Verify that display wasn't paused. */
18289 if (!w->window_end_valid)
18290 GIVE_UP (8);
18292 /* Likewise if highlighting trailing whitespace. */
18293 if (!NILP (Vshow_trailing_whitespace))
18294 GIVE_UP (11);
18296 /* Can't use this if overlay arrow position and/or string have
18297 changed. */
18298 if (overlay_arrows_changed_p ())
18299 GIVE_UP (12);
18301 /* When word-wrap is on, adding a space to the first word of a
18302 wrapped line can change the wrap position, altering the line
18303 above it. It might be worthwhile to handle this more
18304 intelligently, but for now just redisplay from scratch. */
18305 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18306 GIVE_UP (21);
18308 /* Under bidi reordering, adding or deleting a character in the
18309 beginning of a paragraph, before the first strong directional
18310 character, can change the base direction of the paragraph (unless
18311 the buffer specifies a fixed paragraph direction), which will
18312 require redisplaying the whole paragraph. It might be worthwhile
18313 to find the paragraph limits and widen the range of redisplayed
18314 lines to that, but for now just give up this optimization and
18315 redisplay from scratch. */
18316 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18317 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18318 GIVE_UP (22);
18320 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18321 to that variable require thorough redisplay. */
18322 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18323 GIVE_UP (23);
18325 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18326 only if buffer has really changed. The reason is that the gap is
18327 initially at Z for freshly visited files. The code below would
18328 set end_unchanged to 0 in that case. */
18329 if (MODIFF > SAVE_MODIFF
18330 /* This seems to happen sometimes after saving a buffer. */
18331 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18333 if (GPT - BEG < BEG_UNCHANGED)
18334 BEG_UNCHANGED = GPT - BEG;
18335 if (Z - GPT < END_UNCHANGED)
18336 END_UNCHANGED = Z - GPT;
18339 /* The position of the first and last character that has been changed. */
18340 first_changed_charpos = BEG + BEG_UNCHANGED;
18341 last_changed_charpos = Z - END_UNCHANGED;
18343 /* If window starts after a line end, and the last change is in
18344 front of that newline, then changes don't affect the display.
18345 This case happens with stealth-fontification. Note that although
18346 the display is unchanged, glyph positions in the matrix have to
18347 be adjusted, of course. */
18348 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18349 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18350 && ((last_changed_charpos < CHARPOS (start)
18351 && CHARPOS (start) == BEGV)
18352 || (last_changed_charpos < CHARPOS (start) - 1
18353 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18355 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18356 struct glyph_row *r0;
18358 /* Compute how many chars/bytes have been added to or removed
18359 from the buffer. */
18360 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18361 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18362 Z_delta = Z - Z_old;
18363 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18365 /* Give up if PT is not in the window. Note that it already has
18366 been checked at the start of try_window_id that PT is not in
18367 front of the window start. */
18368 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18369 GIVE_UP (13);
18371 /* If window start is unchanged, we can reuse the whole matrix
18372 as is, after adjusting glyph positions. No need to compute
18373 the window end again, since its offset from Z hasn't changed. */
18374 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18375 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18376 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18377 /* PT must not be in a partially visible line. */
18378 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18379 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18381 /* Adjust positions in the glyph matrix. */
18382 if (Z_delta || Z_delta_bytes)
18384 struct glyph_row *r1
18385 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18386 increment_matrix_positions (w->current_matrix,
18387 MATRIX_ROW_VPOS (r0, current_matrix),
18388 MATRIX_ROW_VPOS (r1, current_matrix),
18389 Z_delta, Z_delta_bytes);
18392 /* Set the cursor. */
18393 row = row_containing_pos (w, PT, r0, NULL, 0);
18394 if (row)
18395 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18396 return 1;
18400 /* Handle the case that changes are all below what is displayed in
18401 the window, and that PT is in the window. This shortcut cannot
18402 be taken if ZV is visible in the window, and text has been added
18403 there that is visible in the window. */
18404 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18405 /* ZV is not visible in the window, or there are no
18406 changes at ZV, actually. */
18407 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18408 || first_changed_charpos == last_changed_charpos))
18410 struct glyph_row *r0;
18412 /* Give up if PT is not in the window. Note that it already has
18413 been checked at the start of try_window_id that PT is not in
18414 front of the window start. */
18415 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18416 GIVE_UP (14);
18418 /* If window start is unchanged, we can reuse the whole matrix
18419 as is, without changing glyph positions since no text has
18420 been added/removed in front of the window end. */
18421 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18422 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18423 /* PT must not be in a partially visible line. */
18424 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18425 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18427 /* We have to compute the window end anew since text
18428 could have been added/removed after it. */
18429 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18430 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18432 /* Set the cursor. */
18433 row = row_containing_pos (w, PT, r0, NULL, 0);
18434 if (row)
18435 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18436 return 2;
18440 /* Give up if window start is in the changed area.
18442 The condition used to read
18444 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18446 but why that was tested escapes me at the moment. */
18447 if (CHARPOS (start) >= first_changed_charpos
18448 && CHARPOS (start) <= last_changed_charpos)
18449 GIVE_UP (15);
18451 /* Check that window start agrees with the start of the first glyph
18452 row in its current matrix. Check this after we know the window
18453 start is not in changed text, otherwise positions would not be
18454 comparable. */
18455 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18456 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18457 GIVE_UP (16);
18459 /* Give up if the window ends in strings. Overlay strings
18460 at the end are difficult to handle, so don't try. */
18461 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18462 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18463 GIVE_UP (20);
18465 /* Compute the position at which we have to start displaying new
18466 lines. Some of the lines at the top of the window might be
18467 reusable because they are not displaying changed text. Find the
18468 last row in W's current matrix not affected by changes at the
18469 start of current_buffer. Value is null if changes start in the
18470 first line of window. */
18471 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18472 if (last_unchanged_at_beg_row)
18474 /* Avoid starting to display in the middle of a character, a TAB
18475 for instance. This is easier than to set up the iterator
18476 exactly, and it's not a frequent case, so the additional
18477 effort wouldn't really pay off. */
18478 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18479 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18480 && last_unchanged_at_beg_row > w->current_matrix->rows)
18481 --last_unchanged_at_beg_row;
18483 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18484 GIVE_UP (17);
18486 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18487 GIVE_UP (18);
18488 start_pos = it.current.pos;
18490 /* Start displaying new lines in the desired matrix at the same
18491 vpos we would use in the current matrix, i.e. below
18492 last_unchanged_at_beg_row. */
18493 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18494 current_matrix);
18495 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18496 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18498 eassert (it.hpos == 0 && it.current_x == 0);
18500 else
18502 /* There are no reusable lines at the start of the window.
18503 Start displaying in the first text line. */
18504 start_display (&it, w, start);
18505 it.vpos = it.first_vpos;
18506 start_pos = it.current.pos;
18509 /* Find the first row that is not affected by changes at the end of
18510 the buffer. Value will be null if there is no unchanged row, in
18511 which case we must redisplay to the end of the window. delta
18512 will be set to the value by which buffer positions beginning with
18513 first_unchanged_at_end_row have to be adjusted due to text
18514 changes. */
18515 first_unchanged_at_end_row
18516 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18517 IF_DEBUG (debug_delta = delta);
18518 IF_DEBUG (debug_delta_bytes = delta_bytes);
18520 /* Set stop_pos to the buffer position up to which we will have to
18521 display new lines. If first_unchanged_at_end_row != NULL, this
18522 is the buffer position of the start of the line displayed in that
18523 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18524 that we don't stop at a buffer position. */
18525 stop_pos = 0;
18526 if (first_unchanged_at_end_row)
18528 eassert (last_unchanged_at_beg_row == NULL
18529 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18531 /* If this is a continuation line, move forward to the next one
18532 that isn't. Changes in lines above affect this line.
18533 Caution: this may move first_unchanged_at_end_row to a row
18534 not displaying text. */
18535 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18536 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18537 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18538 < it.last_visible_y))
18539 ++first_unchanged_at_end_row;
18541 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18542 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18543 >= it.last_visible_y))
18544 first_unchanged_at_end_row = NULL;
18545 else
18547 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18548 + delta);
18549 first_unchanged_at_end_vpos
18550 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18551 eassert (stop_pos >= Z - END_UNCHANGED);
18554 else if (last_unchanged_at_beg_row == NULL)
18555 GIVE_UP (19);
18558 #ifdef GLYPH_DEBUG
18560 /* Either there is no unchanged row at the end, or the one we have
18561 now displays text. This is a necessary condition for the window
18562 end pos calculation at the end of this function. */
18563 eassert (first_unchanged_at_end_row == NULL
18564 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18566 debug_last_unchanged_at_beg_vpos
18567 = (last_unchanged_at_beg_row
18568 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18569 : -1);
18570 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18572 #endif /* GLYPH_DEBUG */
18575 /* Display new lines. Set last_text_row to the last new line
18576 displayed which has text on it, i.e. might end up as being the
18577 line where the window_end_vpos is. */
18578 w->cursor.vpos = -1;
18579 last_text_row = NULL;
18580 overlay_arrow_seen = false;
18581 if (it.current_y < it.last_visible_y
18582 && !f->fonts_changed
18583 && (first_unchanged_at_end_row == NULL
18584 || IT_CHARPOS (it) < stop_pos))
18585 it.glyph_row->reversed_p = false;
18586 while (it.current_y < it.last_visible_y
18587 && !f->fonts_changed
18588 && (first_unchanged_at_end_row == NULL
18589 || IT_CHARPOS (it) < stop_pos))
18591 if (display_line (&it))
18592 last_text_row = it.glyph_row - 1;
18595 if (f->fonts_changed)
18596 return -1;
18598 /* The redisplay iterations in display_line above could have
18599 triggered font-lock, which could have done something that
18600 invalidates IT->w window's end-point information, on which we
18601 rely below. E.g., one package, which will remain unnamed, used
18602 to install a font-lock-fontify-region-function that called
18603 bury-buffer, whose side effect is to switch the buffer displayed
18604 by IT->w, and that predictably resets IT->w's window_end_valid
18605 flag, which we already tested at the entry to this function.
18606 Amply punish such packages/modes by giving up on this
18607 optimization in those cases. */
18608 if (!w->window_end_valid)
18610 clear_glyph_matrix (w->desired_matrix);
18611 return -1;
18614 /* Compute differences in buffer positions, y-positions etc. for
18615 lines reused at the bottom of the window. Compute what we can
18616 scroll. */
18617 if (first_unchanged_at_end_row
18618 /* No lines reused because we displayed everything up to the
18619 bottom of the window. */
18620 && it.current_y < it.last_visible_y)
18622 dvpos = (it.vpos
18623 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18624 current_matrix));
18625 dy = it.current_y - first_unchanged_at_end_row->y;
18626 run.current_y = first_unchanged_at_end_row->y;
18627 run.desired_y = run.current_y + dy;
18628 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18630 else
18632 delta = delta_bytes = dvpos = dy
18633 = run.current_y = run.desired_y = run.height = 0;
18634 first_unchanged_at_end_row = NULL;
18636 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18639 /* Find the cursor if not already found. We have to decide whether
18640 PT will appear on this window (it sometimes doesn't, but this is
18641 not a very frequent case.) This decision has to be made before
18642 the current matrix is altered. A value of cursor.vpos < 0 means
18643 that PT is either in one of the lines beginning at
18644 first_unchanged_at_end_row or below the window. Don't care for
18645 lines that might be displayed later at the window end; as
18646 mentioned, this is not a frequent case. */
18647 if (w->cursor.vpos < 0)
18649 /* Cursor in unchanged rows at the top? */
18650 if (PT < CHARPOS (start_pos)
18651 && last_unchanged_at_beg_row)
18653 row = row_containing_pos (w, PT,
18654 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18655 last_unchanged_at_beg_row + 1, 0);
18656 if (row)
18657 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18660 /* Start from first_unchanged_at_end_row looking for PT. */
18661 else if (first_unchanged_at_end_row)
18663 row = row_containing_pos (w, PT - delta,
18664 first_unchanged_at_end_row, NULL, 0);
18665 if (row)
18666 set_cursor_from_row (w, row, w->current_matrix, delta,
18667 delta_bytes, dy, dvpos);
18670 /* Give up if cursor was not found. */
18671 if (w->cursor.vpos < 0)
18673 clear_glyph_matrix (w->desired_matrix);
18674 return -1;
18678 /* Don't let the cursor end in the scroll margins. */
18680 int this_scroll_margin, cursor_height;
18681 int frame_line_height = default_line_pixel_height (w);
18682 int window_total_lines
18683 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18685 this_scroll_margin =
18686 max (0, min (scroll_margin, window_total_lines / 4));
18687 this_scroll_margin *= frame_line_height;
18688 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18690 if ((w->cursor.y < this_scroll_margin
18691 && CHARPOS (start) > BEGV)
18692 /* Old redisplay didn't take scroll margin into account at the bottom,
18693 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18694 || (w->cursor.y + (make_cursor_line_fully_visible_p
18695 ? cursor_height + this_scroll_margin
18696 : 1)) > it.last_visible_y)
18698 w->cursor.vpos = -1;
18699 clear_glyph_matrix (w->desired_matrix);
18700 return -1;
18704 /* Scroll the display. Do it before changing the current matrix so
18705 that xterm.c doesn't get confused about where the cursor glyph is
18706 found. */
18707 if (dy && run.height)
18709 update_begin (f);
18711 if (FRAME_WINDOW_P (f))
18713 FRAME_RIF (f)->update_window_begin_hook (w);
18714 FRAME_RIF (f)->clear_window_mouse_face (w);
18715 FRAME_RIF (f)->scroll_run_hook (w, &run);
18716 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18718 else
18720 /* Terminal frame. In this case, dvpos gives the number of
18721 lines to scroll by; dvpos < 0 means scroll up. */
18722 int from_vpos
18723 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18724 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18725 int end = (WINDOW_TOP_EDGE_LINE (w)
18726 + WINDOW_WANTS_HEADER_LINE_P (w)
18727 + window_internal_height (w));
18729 #if defined (HAVE_GPM) || defined (MSDOS)
18730 x_clear_window_mouse_face (w);
18731 #endif
18732 /* Perform the operation on the screen. */
18733 if (dvpos > 0)
18735 /* Scroll last_unchanged_at_beg_row to the end of the
18736 window down dvpos lines. */
18737 set_terminal_window (f, end);
18739 /* On dumb terminals delete dvpos lines at the end
18740 before inserting dvpos empty lines. */
18741 if (!FRAME_SCROLL_REGION_OK (f))
18742 ins_del_lines (f, end - dvpos, -dvpos);
18744 /* Insert dvpos empty lines in front of
18745 last_unchanged_at_beg_row. */
18746 ins_del_lines (f, from, dvpos);
18748 else if (dvpos < 0)
18750 /* Scroll up last_unchanged_at_beg_vpos to the end of
18751 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18752 set_terminal_window (f, end);
18754 /* Delete dvpos lines in front of
18755 last_unchanged_at_beg_vpos. ins_del_lines will set
18756 the cursor to the given vpos and emit |dvpos| delete
18757 line sequences. */
18758 ins_del_lines (f, from + dvpos, dvpos);
18760 /* On a dumb terminal insert dvpos empty lines at the
18761 end. */
18762 if (!FRAME_SCROLL_REGION_OK (f))
18763 ins_del_lines (f, end + dvpos, -dvpos);
18766 set_terminal_window (f, 0);
18769 update_end (f);
18772 /* Shift reused rows of the current matrix to the right position.
18773 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18774 text. */
18775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18776 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18777 if (dvpos < 0)
18779 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18780 bottom_vpos, dvpos);
18781 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18782 bottom_vpos);
18784 else if (dvpos > 0)
18786 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18787 bottom_vpos, dvpos);
18788 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18789 first_unchanged_at_end_vpos + dvpos);
18792 /* For frame-based redisplay, make sure that current frame and window
18793 matrix are in sync with respect to glyph memory. */
18794 if (!FRAME_WINDOW_P (f))
18795 sync_frame_with_window_matrix_rows (w);
18797 /* Adjust buffer positions in reused rows. */
18798 if (delta || delta_bytes)
18799 increment_matrix_positions (current_matrix,
18800 first_unchanged_at_end_vpos + dvpos,
18801 bottom_vpos, delta, delta_bytes);
18803 /* Adjust Y positions. */
18804 if (dy)
18805 shift_glyph_matrix (w, current_matrix,
18806 first_unchanged_at_end_vpos + dvpos,
18807 bottom_vpos, dy);
18809 if (first_unchanged_at_end_row)
18811 first_unchanged_at_end_row += dvpos;
18812 if (first_unchanged_at_end_row->y >= it.last_visible_y
18813 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18814 first_unchanged_at_end_row = NULL;
18817 /* If scrolling up, there may be some lines to display at the end of
18818 the window. */
18819 last_text_row_at_end = NULL;
18820 if (dy < 0)
18822 /* Scrolling up can leave for example a partially visible line
18823 at the end of the window to be redisplayed. */
18824 /* Set last_row to the glyph row in the current matrix where the
18825 window end line is found. It has been moved up or down in
18826 the matrix by dvpos. */
18827 int last_vpos = w->window_end_vpos + dvpos;
18828 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18830 /* If last_row is the window end line, it should display text. */
18831 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18833 /* If window end line was partially visible before, begin
18834 displaying at that line. Otherwise begin displaying with the
18835 line following it. */
18836 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18838 init_to_row_start (&it, w, last_row);
18839 it.vpos = last_vpos;
18840 it.current_y = last_row->y;
18842 else
18844 init_to_row_end (&it, w, last_row);
18845 it.vpos = 1 + last_vpos;
18846 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18847 ++last_row;
18850 /* We may start in a continuation line. If so, we have to
18851 get the right continuation_lines_width and current_x. */
18852 it.continuation_lines_width = last_row->continuation_lines_width;
18853 it.hpos = it.current_x = 0;
18855 /* Display the rest of the lines at the window end. */
18856 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18857 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18859 /* Is it always sure that the display agrees with lines in
18860 the current matrix? I don't think so, so we mark rows
18861 displayed invalid in the current matrix by setting their
18862 enabled_p flag to false. */
18863 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18864 if (display_line (&it))
18865 last_text_row_at_end = it.glyph_row - 1;
18869 /* Update window_end_pos and window_end_vpos. */
18870 if (first_unchanged_at_end_row && !last_text_row_at_end)
18872 /* Window end line if one of the preserved rows from the current
18873 matrix. Set row to the last row displaying text in current
18874 matrix starting at first_unchanged_at_end_row, after
18875 scrolling. */
18876 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18877 row = find_last_row_displaying_text (w->current_matrix, &it,
18878 first_unchanged_at_end_row);
18879 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18880 adjust_window_ends (w, row, true);
18881 eassert (w->window_end_bytepos >= 0);
18882 IF_DEBUG (debug_method_add (w, "A"));
18884 else if (last_text_row_at_end)
18886 adjust_window_ends (w, last_text_row_at_end, false);
18887 eassert (w->window_end_bytepos >= 0);
18888 IF_DEBUG (debug_method_add (w, "B"));
18890 else if (last_text_row)
18892 /* We have displayed either to the end of the window or at the
18893 end of the window, i.e. the last row with text is to be found
18894 in the desired matrix. */
18895 adjust_window_ends (w, last_text_row, false);
18896 eassert (w->window_end_bytepos >= 0);
18898 else if (first_unchanged_at_end_row == NULL
18899 && last_text_row == NULL
18900 && last_text_row_at_end == NULL)
18902 /* Displayed to end of window, but no line containing text was
18903 displayed. Lines were deleted at the end of the window. */
18904 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18905 int vpos = w->window_end_vpos;
18906 struct glyph_row *current_row = current_matrix->rows + vpos;
18907 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18909 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18911 eassert (first_vpos <= vpos);
18912 if (desired_row->enabled_p)
18914 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18915 row = desired_row;
18917 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18918 row = current_row;
18921 w->window_end_vpos = vpos + 1;
18922 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18923 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18924 eassert (w->window_end_bytepos >= 0);
18925 IF_DEBUG (debug_method_add (w, "C"));
18927 else
18928 emacs_abort ();
18930 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18931 debug_end_vpos = w->window_end_vpos));
18933 /* Record that display has not been completed. */
18934 w->window_end_valid = false;
18935 w->desired_matrix->no_scrolling_p = true;
18936 return 3;
18938 #undef GIVE_UP
18943 /***********************************************************************
18944 More debugging support
18945 ***********************************************************************/
18947 #ifdef GLYPH_DEBUG
18949 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18950 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18951 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18954 /* Dump the contents of glyph matrix MATRIX on stderr.
18956 GLYPHS 0 means don't show glyph contents.
18957 GLYPHS 1 means show glyphs in short form
18958 GLYPHS > 1 means show glyphs in long form. */
18960 void
18961 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18963 int i;
18964 for (i = 0; i < matrix->nrows; ++i)
18965 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18969 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18970 the glyph row and area where the glyph comes from. */
18972 void
18973 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18975 if (glyph->type == CHAR_GLYPH
18976 || glyph->type == GLYPHLESS_GLYPH)
18978 fprintf (stderr,
18979 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18980 glyph - row->glyphs[TEXT_AREA],
18981 (glyph->type == CHAR_GLYPH
18982 ? 'C'
18983 : 'G'),
18984 glyph->charpos,
18985 (BUFFERP (glyph->object)
18986 ? 'B'
18987 : (STRINGP (glyph->object)
18988 ? 'S'
18989 : (NILP (glyph->object)
18990 ? '0'
18991 : '-'))),
18992 glyph->pixel_width,
18993 glyph->u.ch,
18994 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18995 ? glyph->u.ch
18996 : '.'),
18997 glyph->face_id,
18998 glyph->left_box_line_p,
18999 glyph->right_box_line_p);
19001 else if (glyph->type == STRETCH_GLYPH)
19003 fprintf (stderr,
19004 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19005 glyph - row->glyphs[TEXT_AREA],
19006 'S',
19007 glyph->charpos,
19008 (BUFFERP (glyph->object)
19009 ? 'B'
19010 : (STRINGP (glyph->object)
19011 ? 'S'
19012 : (NILP (glyph->object)
19013 ? '0'
19014 : '-'))),
19015 glyph->pixel_width,
19017 ' ',
19018 glyph->face_id,
19019 glyph->left_box_line_p,
19020 glyph->right_box_line_p);
19022 else if (glyph->type == IMAGE_GLYPH)
19024 fprintf (stderr,
19025 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19026 glyph - row->glyphs[TEXT_AREA],
19027 'I',
19028 glyph->charpos,
19029 (BUFFERP (glyph->object)
19030 ? 'B'
19031 : (STRINGP (glyph->object)
19032 ? 'S'
19033 : (NILP (glyph->object)
19034 ? '0'
19035 : '-'))),
19036 glyph->pixel_width,
19037 glyph->u.img_id,
19038 '.',
19039 glyph->face_id,
19040 glyph->left_box_line_p,
19041 glyph->right_box_line_p);
19043 else if (glyph->type == COMPOSITE_GLYPH)
19045 fprintf (stderr,
19046 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19047 glyph - row->glyphs[TEXT_AREA],
19048 '+',
19049 glyph->charpos,
19050 (BUFFERP (glyph->object)
19051 ? 'B'
19052 : (STRINGP (glyph->object)
19053 ? 'S'
19054 : (NILP (glyph->object)
19055 ? '0'
19056 : '-'))),
19057 glyph->pixel_width,
19058 glyph->u.cmp.id);
19059 if (glyph->u.cmp.automatic)
19060 fprintf (stderr,
19061 "[%d-%d]",
19062 glyph->slice.cmp.from, glyph->slice.cmp.to);
19063 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19064 glyph->face_id,
19065 glyph->left_box_line_p,
19066 glyph->right_box_line_p);
19068 else if (glyph->type == XWIDGET_GLYPH)
19070 #ifndef HAVE_XWIDGETS
19071 eassume (false);
19072 #else
19073 fprintf (stderr,
19074 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19075 glyph - row->glyphs[TEXT_AREA],
19076 'X',
19077 glyph->charpos,
19078 (BUFFERP (glyph->object)
19079 ? 'B'
19080 : (STRINGP (glyph->object)
19081 ? 'S'
19082 : '-')),
19083 glyph->pixel_width,
19084 glyph->u.xwidget,
19085 '.',
19086 glyph->face_id,
19087 glyph->left_box_line_p,
19088 glyph->right_box_line_p);
19089 #endif
19094 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19095 GLYPHS 0 means don't show glyph contents.
19096 GLYPHS 1 means show glyphs in short form
19097 GLYPHS > 1 means show glyphs in long form. */
19099 void
19100 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19102 if (glyphs != 1)
19104 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19105 fprintf (stderr, "==============================================================================\n");
19107 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19108 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19109 vpos,
19110 MATRIX_ROW_START_CHARPOS (row),
19111 MATRIX_ROW_END_CHARPOS (row),
19112 row->used[TEXT_AREA],
19113 row->contains_overlapping_glyphs_p,
19114 row->enabled_p,
19115 row->truncated_on_left_p,
19116 row->truncated_on_right_p,
19117 row->continued_p,
19118 MATRIX_ROW_CONTINUATION_LINE_P (row),
19119 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19120 row->ends_at_zv_p,
19121 row->fill_line_p,
19122 row->ends_in_middle_of_char_p,
19123 row->starts_in_middle_of_char_p,
19124 row->mouse_face_p,
19125 row->x,
19126 row->y,
19127 row->pixel_width,
19128 row->height,
19129 row->visible_height,
19130 row->ascent,
19131 row->phys_ascent);
19132 /* The next 3 lines should align to "Start" in the header. */
19133 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19134 row->end.overlay_string_index,
19135 row->continuation_lines_width);
19136 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19137 CHARPOS (row->start.string_pos),
19138 CHARPOS (row->end.string_pos));
19139 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19140 row->end.dpvec_index);
19143 if (glyphs > 1)
19145 int area;
19147 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19149 struct glyph *glyph = row->glyphs[area];
19150 struct glyph *glyph_end = glyph + row->used[area];
19152 /* Glyph for a line end in text. */
19153 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19154 ++glyph_end;
19156 if (glyph < glyph_end)
19157 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19159 for (; glyph < glyph_end; ++glyph)
19160 dump_glyph (row, glyph, area);
19163 else if (glyphs == 1)
19165 int area;
19166 char s[SHRT_MAX + 4];
19168 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19170 int i;
19172 for (i = 0; i < row->used[area]; ++i)
19174 struct glyph *glyph = row->glyphs[area] + i;
19175 if (i == row->used[area] - 1
19176 && area == TEXT_AREA
19177 && NILP (glyph->object)
19178 && glyph->type == CHAR_GLYPH
19179 && glyph->u.ch == ' ')
19181 strcpy (&s[i], "[\\n]");
19182 i += 4;
19184 else if (glyph->type == CHAR_GLYPH
19185 && glyph->u.ch < 0x80
19186 && glyph->u.ch >= ' ')
19187 s[i] = glyph->u.ch;
19188 else
19189 s[i] = '.';
19192 s[i] = '\0';
19193 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19199 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19200 Sdump_glyph_matrix, 0, 1, "p",
19201 doc: /* Dump the current matrix of the selected window to stderr.
19202 Shows contents of glyph row structures. With non-nil
19203 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19204 glyphs in short form, otherwise show glyphs in long form.
19206 Interactively, no argument means show glyphs in short form;
19207 with numeric argument, its value is passed as the GLYPHS flag. */)
19208 (Lisp_Object glyphs)
19210 struct window *w = XWINDOW (selected_window);
19211 struct buffer *buffer = XBUFFER (w->contents);
19213 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19214 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19215 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19216 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19217 fprintf (stderr, "=============================================\n");
19218 dump_glyph_matrix (w->current_matrix,
19219 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19220 return Qnil;
19224 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19225 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19226 Only text-mode frames have frame glyph matrices. */)
19227 (void)
19229 struct frame *f = XFRAME (selected_frame);
19231 if (f->current_matrix)
19232 dump_glyph_matrix (f->current_matrix, 1);
19233 else
19234 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19235 return Qnil;
19239 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19240 doc: /* Dump glyph row ROW to stderr.
19241 GLYPH 0 means don't dump glyphs.
19242 GLYPH 1 means dump glyphs in short form.
19243 GLYPH > 1 or omitted means dump glyphs in long form. */)
19244 (Lisp_Object row, Lisp_Object glyphs)
19246 struct glyph_matrix *matrix;
19247 EMACS_INT vpos;
19249 CHECK_NUMBER (row);
19250 matrix = XWINDOW (selected_window)->current_matrix;
19251 vpos = XINT (row);
19252 if (vpos >= 0 && vpos < matrix->nrows)
19253 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19254 vpos,
19255 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19256 return Qnil;
19260 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19261 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19262 GLYPH 0 means don't dump glyphs.
19263 GLYPH 1 means dump glyphs in short form.
19264 GLYPH > 1 or omitted means dump glyphs in long form.
19266 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19267 do nothing. */)
19268 (Lisp_Object row, Lisp_Object glyphs)
19270 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19271 struct frame *sf = SELECTED_FRAME ();
19272 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19273 EMACS_INT vpos;
19275 CHECK_NUMBER (row);
19276 vpos = XINT (row);
19277 if (vpos >= 0 && vpos < m->nrows)
19278 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19279 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19280 #endif
19281 return Qnil;
19285 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19286 doc: /* Toggle tracing of redisplay.
19287 With ARG, turn tracing on if and only if ARG is positive. */)
19288 (Lisp_Object arg)
19290 if (NILP (arg))
19291 trace_redisplay_p = !trace_redisplay_p;
19292 else
19294 arg = Fprefix_numeric_value (arg);
19295 trace_redisplay_p = XINT (arg) > 0;
19298 return Qnil;
19302 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19303 doc: /* Like `format', but print result to stderr.
19304 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19305 (ptrdiff_t nargs, Lisp_Object *args)
19307 Lisp_Object s = Fformat (nargs, args);
19308 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19309 return Qnil;
19312 #endif /* GLYPH_DEBUG */
19316 /***********************************************************************
19317 Building Desired Matrix Rows
19318 ***********************************************************************/
19320 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19321 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19323 static struct glyph_row *
19324 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19326 struct frame *f = XFRAME (WINDOW_FRAME (w));
19327 struct buffer *buffer = XBUFFER (w->contents);
19328 struct buffer *old = current_buffer;
19329 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19330 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19331 const unsigned char *arrow_end = arrow_string + arrow_len;
19332 const unsigned char *p;
19333 struct it it;
19334 bool multibyte_p;
19335 int n_glyphs_before;
19337 set_buffer_temp (buffer);
19338 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19339 scratch_glyph_row.reversed_p = false;
19340 it.glyph_row->used[TEXT_AREA] = 0;
19341 SET_TEXT_POS (it.position, 0, 0);
19343 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19344 p = arrow_string;
19345 while (p < arrow_end)
19347 Lisp_Object face, ilisp;
19349 /* Get the next character. */
19350 if (multibyte_p)
19351 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19352 else
19354 it.c = it.char_to_display = *p, it.len = 1;
19355 if (! ASCII_CHAR_P (it.c))
19356 it.char_to_display = BYTE8_TO_CHAR (it.c);
19358 p += it.len;
19360 /* Get its face. */
19361 ilisp = make_number (p - arrow_string);
19362 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19363 it.face_id = compute_char_face (f, it.char_to_display, face);
19365 /* Compute its width, get its glyphs. */
19366 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19367 SET_TEXT_POS (it.position, -1, -1);
19368 PRODUCE_GLYPHS (&it);
19370 /* If this character doesn't fit any more in the line, we have
19371 to remove some glyphs. */
19372 if (it.current_x > it.last_visible_x)
19374 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19375 break;
19379 set_buffer_temp (old);
19380 return it.glyph_row;
19384 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19385 glyphs to insert is determined by produce_special_glyphs. */
19387 static void
19388 insert_left_trunc_glyphs (struct it *it)
19390 struct it truncate_it;
19391 struct glyph *from, *end, *to, *toend;
19393 eassert (!FRAME_WINDOW_P (it->f)
19394 || (!it->glyph_row->reversed_p
19395 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19396 || (it->glyph_row->reversed_p
19397 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19399 /* Get the truncation glyphs. */
19400 truncate_it = *it;
19401 truncate_it.current_x = 0;
19402 truncate_it.face_id = DEFAULT_FACE_ID;
19403 truncate_it.glyph_row = &scratch_glyph_row;
19404 truncate_it.area = TEXT_AREA;
19405 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19406 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19407 truncate_it.object = Qnil;
19408 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19410 /* Overwrite glyphs from IT with truncation glyphs. */
19411 if (!it->glyph_row->reversed_p)
19413 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19415 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19416 end = from + tused;
19417 to = it->glyph_row->glyphs[TEXT_AREA];
19418 toend = to + it->glyph_row->used[TEXT_AREA];
19419 if (FRAME_WINDOW_P (it->f))
19421 /* On GUI frames, when variable-size fonts are displayed,
19422 the truncation glyphs may need more pixels than the row's
19423 glyphs they overwrite. We overwrite more glyphs to free
19424 enough screen real estate, and enlarge the stretch glyph
19425 on the right (see display_line), if there is one, to
19426 preserve the screen position of the truncation glyphs on
19427 the right. */
19428 int w = 0;
19429 struct glyph *g = to;
19430 short used;
19432 /* The first glyph could be partially visible, in which case
19433 it->glyph_row->x will be negative. But we want the left
19434 truncation glyphs to be aligned at the left margin of the
19435 window, so we override the x coordinate at which the row
19436 will begin. */
19437 it->glyph_row->x = 0;
19438 while (g < toend && w < it->truncation_pixel_width)
19440 w += g->pixel_width;
19441 ++g;
19443 if (g - to - tused > 0)
19445 memmove (to + tused, g, (toend - g) * sizeof(*g));
19446 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19448 used = it->glyph_row->used[TEXT_AREA];
19449 if (it->glyph_row->truncated_on_right_p
19450 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19451 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19452 == STRETCH_GLYPH)
19454 int extra = w - it->truncation_pixel_width;
19456 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19460 while (from < end)
19461 *to++ = *from++;
19463 /* There may be padding glyphs left over. Overwrite them too. */
19464 if (!FRAME_WINDOW_P (it->f))
19466 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19468 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19469 while (from < end)
19470 *to++ = *from++;
19474 if (to > toend)
19475 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19477 else
19479 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19481 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19482 that back to front. */
19483 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19484 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19485 toend = it->glyph_row->glyphs[TEXT_AREA];
19486 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19487 if (FRAME_WINDOW_P (it->f))
19489 int w = 0;
19490 struct glyph *g = to;
19492 while (g >= toend && w < it->truncation_pixel_width)
19494 w += g->pixel_width;
19495 --g;
19497 if (to - g - tused > 0)
19498 to = g + tused;
19499 if (it->glyph_row->truncated_on_right_p
19500 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19501 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19503 int extra = w - it->truncation_pixel_width;
19505 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19509 while (from >= end && to >= toend)
19510 *to-- = *from--;
19511 if (!FRAME_WINDOW_P (it->f))
19513 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19515 from =
19516 truncate_it.glyph_row->glyphs[TEXT_AREA]
19517 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19518 while (from >= end && to >= toend)
19519 *to-- = *from--;
19522 if (from >= end)
19524 /* Need to free some room before prepending additional
19525 glyphs. */
19526 int move_by = from - end + 1;
19527 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19528 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19530 for ( ; g >= g0; g--)
19531 g[move_by] = *g;
19532 while (from >= end)
19533 *to-- = *from--;
19534 it->glyph_row->used[TEXT_AREA] += move_by;
19539 /* Compute the hash code for ROW. */
19540 unsigned
19541 row_hash (struct glyph_row *row)
19543 int area, k;
19544 unsigned hashval = 0;
19546 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19547 for (k = 0; k < row->used[area]; ++k)
19548 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19549 + row->glyphs[area][k].u.val
19550 + row->glyphs[area][k].face_id
19551 + row->glyphs[area][k].padding_p
19552 + (row->glyphs[area][k].type << 2));
19554 return hashval;
19557 /* Compute the pixel height and width of IT->glyph_row.
19559 Most of the time, ascent and height of a display line will be equal
19560 to the max_ascent and max_height values of the display iterator
19561 structure. This is not the case if
19563 1. We hit ZV without displaying anything. In this case, max_ascent
19564 and max_height will be zero.
19566 2. We have some glyphs that don't contribute to the line height.
19567 (The glyph row flag contributes_to_line_height_p is for future
19568 pixmap extensions).
19570 The first case is easily covered by using default values because in
19571 these cases, the line height does not really matter, except that it
19572 must not be zero. */
19574 static void
19575 compute_line_metrics (struct it *it)
19577 struct glyph_row *row = it->glyph_row;
19579 if (FRAME_WINDOW_P (it->f))
19581 int i, min_y, max_y;
19583 /* The line may consist of one space only, that was added to
19584 place the cursor on it. If so, the row's height hasn't been
19585 computed yet. */
19586 if (row->height == 0)
19588 if (it->max_ascent + it->max_descent == 0)
19589 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19590 row->ascent = it->max_ascent;
19591 row->height = it->max_ascent + it->max_descent;
19592 row->phys_ascent = it->max_phys_ascent;
19593 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19594 row->extra_line_spacing = it->max_extra_line_spacing;
19597 /* Compute the width of this line. */
19598 row->pixel_width = row->x;
19599 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19600 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19602 eassert (row->pixel_width >= 0);
19603 eassert (row->ascent >= 0 && row->height > 0);
19605 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19606 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19608 /* If first line's physical ascent is larger than its logical
19609 ascent, use the physical ascent, and make the row taller.
19610 This makes accented characters fully visible. */
19611 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19612 && row->phys_ascent > row->ascent)
19614 row->height += row->phys_ascent - row->ascent;
19615 row->ascent = row->phys_ascent;
19618 /* Compute how much of the line is visible. */
19619 row->visible_height = row->height;
19621 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19622 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19624 if (row->y < min_y)
19625 row->visible_height -= min_y - row->y;
19626 if (row->y + row->height > max_y)
19627 row->visible_height -= row->y + row->height - max_y;
19629 else
19631 row->pixel_width = row->used[TEXT_AREA];
19632 if (row->continued_p)
19633 row->pixel_width -= it->continuation_pixel_width;
19634 else if (row->truncated_on_right_p)
19635 row->pixel_width -= it->truncation_pixel_width;
19636 row->ascent = row->phys_ascent = 0;
19637 row->height = row->phys_height = row->visible_height = 1;
19638 row->extra_line_spacing = 0;
19641 /* Compute a hash code for this row. */
19642 row->hash = row_hash (row);
19644 it->max_ascent = it->max_descent = 0;
19645 it->max_phys_ascent = it->max_phys_descent = 0;
19649 /* Append one space to the glyph row of iterator IT if doing a
19650 window-based redisplay. The space has the same face as
19651 IT->face_id. Value is true if a space was added.
19653 This function is called to make sure that there is always one glyph
19654 at the end of a glyph row that the cursor can be set on under
19655 window-systems. (If there weren't such a glyph we would not know
19656 how wide and tall a box cursor should be displayed).
19658 At the same time this space let's a nicely handle clearing to the
19659 end of the line if the row ends in italic text. */
19661 static bool
19662 append_space_for_newline (struct it *it, bool default_face_p)
19664 if (FRAME_WINDOW_P (it->f))
19666 int n = it->glyph_row->used[TEXT_AREA];
19668 if (it->glyph_row->glyphs[TEXT_AREA] + n
19669 < it->glyph_row->glyphs[1 + TEXT_AREA])
19671 /* Save some values that must not be changed.
19672 Must save IT->c and IT->len because otherwise
19673 ITERATOR_AT_END_P wouldn't work anymore after
19674 append_space_for_newline has been called. */
19675 enum display_element_type saved_what = it->what;
19676 int saved_c = it->c, saved_len = it->len;
19677 int saved_char_to_display = it->char_to_display;
19678 int saved_x = it->current_x;
19679 int saved_face_id = it->face_id;
19680 bool saved_box_end = it->end_of_box_run_p;
19681 struct text_pos saved_pos;
19682 Lisp_Object saved_object;
19683 struct face *face;
19685 saved_object = it->object;
19686 saved_pos = it->position;
19688 it->what = IT_CHARACTER;
19689 memset (&it->position, 0, sizeof it->position);
19690 it->object = Qnil;
19691 it->c = it->char_to_display = ' ';
19692 it->len = 1;
19694 /* If the default face was remapped, be sure to use the
19695 remapped face for the appended newline. */
19696 if (default_face_p)
19697 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19698 else if (it->face_before_selective_p)
19699 it->face_id = it->saved_face_id;
19700 face = FACE_FROM_ID (it->f, it->face_id);
19701 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19702 /* In R2L rows, we will prepend a stretch glyph that will
19703 have the end_of_box_run_p flag set for it, so there's no
19704 need for the appended newline glyph to have that flag
19705 set. */
19706 if (it->glyph_row->reversed_p
19707 /* But if the appended newline glyph goes all the way to
19708 the end of the row, there will be no stretch glyph,
19709 so leave the box flag set. */
19710 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19711 it->end_of_box_run_p = false;
19713 PRODUCE_GLYPHS (it);
19715 #ifdef HAVE_WINDOW_SYSTEM
19716 /* Make sure this space glyph has the right ascent and
19717 descent values, or else cursor at end of line will look
19718 funny, and height of empty lines will be incorrect. */
19719 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19720 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19721 if (n == 0)
19723 Lisp_Object height, total_height;
19724 int extra_line_spacing = it->extra_line_spacing;
19725 int boff = font->baseline_offset;
19727 if (font->vertical_centering)
19728 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19730 it->object = saved_object; /* get_it_property needs this */
19731 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19732 /* Must do a subset of line height processing from
19733 x_produce_glyph for newline characters. */
19734 height = get_it_property (it, Qline_height);
19735 if (CONSP (height)
19736 && CONSP (XCDR (height))
19737 && NILP (XCDR (XCDR (height))))
19739 total_height = XCAR (XCDR (height));
19740 height = XCAR (height);
19742 else
19743 total_height = Qnil;
19744 height = calc_line_height_property (it, height, font, boff, true);
19746 if (it->override_ascent >= 0)
19748 it->ascent = it->override_ascent;
19749 it->descent = it->override_descent;
19750 boff = it->override_boff;
19752 if (EQ (height, Qt))
19753 extra_line_spacing = 0;
19754 else
19756 Lisp_Object spacing;
19758 it->phys_ascent = it->ascent;
19759 it->phys_descent = it->descent;
19760 if (!NILP (height)
19761 && XINT (height) > it->ascent + it->descent)
19762 it->ascent = XINT (height) - it->descent;
19764 if (!NILP (total_height))
19765 spacing = calc_line_height_property (it, total_height, font,
19766 boff, false);
19767 else
19769 spacing = get_it_property (it, Qline_spacing);
19770 spacing = calc_line_height_property (it, spacing, font,
19771 boff, false);
19773 if (INTEGERP (spacing))
19775 extra_line_spacing = XINT (spacing);
19776 if (!NILP (total_height))
19777 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19780 if (extra_line_spacing > 0)
19782 it->descent += extra_line_spacing;
19783 if (extra_line_spacing > it->max_extra_line_spacing)
19784 it->max_extra_line_spacing = extra_line_spacing;
19786 it->max_ascent = it->ascent;
19787 it->max_descent = it->descent;
19788 /* Make sure compute_line_metrics recomputes the row height. */
19789 it->glyph_row->height = 0;
19792 g->ascent = it->max_ascent;
19793 g->descent = it->max_descent;
19794 #endif
19796 it->override_ascent = -1;
19797 it->constrain_row_ascent_descent_p = false;
19798 it->current_x = saved_x;
19799 it->object = saved_object;
19800 it->position = saved_pos;
19801 it->what = saved_what;
19802 it->face_id = saved_face_id;
19803 it->len = saved_len;
19804 it->c = saved_c;
19805 it->char_to_display = saved_char_to_display;
19806 it->end_of_box_run_p = saved_box_end;
19807 return true;
19811 return false;
19815 /* Extend the face of the last glyph in the text area of IT->glyph_row
19816 to the end of the display line. Called from display_line. If the
19817 glyph row is empty, add a space glyph to it so that we know the
19818 face to draw. Set the glyph row flag fill_line_p. If the glyph
19819 row is R2L, prepend a stretch glyph to cover the empty space to the
19820 left of the leftmost glyph. */
19822 static void
19823 extend_face_to_end_of_line (struct it *it)
19825 struct face *face, *default_face;
19826 struct frame *f = it->f;
19828 /* If line is already filled, do nothing. Non window-system frames
19829 get a grace of one more ``pixel'' because their characters are
19830 1-``pixel'' wide, so they hit the equality too early. This grace
19831 is needed only for R2L rows that are not continued, to produce
19832 one extra blank where we could display the cursor. */
19833 if ((it->current_x >= it->last_visible_x
19834 + (!FRAME_WINDOW_P (f)
19835 && it->glyph_row->reversed_p
19836 && !it->glyph_row->continued_p))
19837 /* If the window has display margins, we will need to extend
19838 their face even if the text area is filled. */
19839 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19840 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19841 return;
19843 /* The default face, possibly remapped. */
19844 default_face = FACE_FROM_ID_OR_NULL (f,
19845 lookup_basic_face (f, DEFAULT_FACE_ID));
19847 /* Face extension extends the background and box of IT->face_id
19848 to the end of the line. If the background equals the background
19849 of the frame, we don't have to do anything. */
19850 face = FACE_FROM_ID (f, (it->face_before_selective_p
19851 ? it->saved_face_id
19852 : it->face_id));
19854 if (FRAME_WINDOW_P (f)
19855 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19856 && face->box == FACE_NO_BOX
19857 && face->background == FRAME_BACKGROUND_PIXEL (f)
19858 #ifdef HAVE_WINDOW_SYSTEM
19859 && !face->stipple
19860 #endif
19861 && !it->glyph_row->reversed_p)
19862 return;
19864 /* Set the glyph row flag indicating that the face of the last glyph
19865 in the text area has to be drawn to the end of the text area. */
19866 it->glyph_row->fill_line_p = true;
19868 /* If current character of IT is not ASCII, make sure we have the
19869 ASCII face. This will be automatically undone the next time
19870 get_next_display_element returns a multibyte character. Note
19871 that the character will always be single byte in unibyte
19872 text. */
19873 if (!ASCII_CHAR_P (it->c))
19875 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19878 if (FRAME_WINDOW_P (f))
19880 /* If the row is empty, add a space with the current face of IT,
19881 so that we know which face to draw. */
19882 if (it->glyph_row->used[TEXT_AREA] == 0)
19884 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19885 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19886 it->glyph_row->used[TEXT_AREA] = 1;
19888 /* Mode line and the header line don't have margins, and
19889 likewise the frame's tool-bar window, if there is any. */
19890 if (!(it->glyph_row->mode_line_p
19891 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19892 || (WINDOWP (f->tool_bar_window)
19893 && it->w == XWINDOW (f->tool_bar_window))
19894 #endif
19897 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19898 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19900 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19901 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19902 default_face->id;
19903 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19905 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19906 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19908 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19909 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19910 default_face->id;
19911 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19914 #ifdef HAVE_WINDOW_SYSTEM
19915 if (it->glyph_row->reversed_p)
19917 /* Prepend a stretch glyph to the row, such that the
19918 rightmost glyph will be drawn flushed all the way to the
19919 right margin of the window. The stretch glyph that will
19920 occupy the empty space, if any, to the left of the
19921 glyphs. */
19922 struct font *font = face->font ? face->font : FRAME_FONT (f);
19923 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19924 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19925 struct glyph *g;
19926 int row_width, stretch_ascent, stretch_width;
19927 struct text_pos saved_pos;
19928 int saved_face_id;
19929 bool saved_avoid_cursor, saved_box_start;
19931 for (row_width = 0, g = row_start; g < row_end; g++)
19932 row_width += g->pixel_width;
19934 /* FIXME: There are various minor display glitches in R2L
19935 rows when only one of the fringes is missing. The
19936 strange condition below produces the least bad effect. */
19937 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19938 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19939 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19940 stretch_width = window_box_width (it->w, TEXT_AREA);
19941 else
19942 stretch_width = it->last_visible_x - it->first_visible_x;
19943 stretch_width -= row_width;
19945 if (stretch_width > 0)
19947 stretch_ascent =
19948 (((it->ascent + it->descent)
19949 * FONT_BASE (font)) / FONT_HEIGHT (font));
19950 saved_pos = it->position;
19951 memset (&it->position, 0, sizeof it->position);
19952 saved_avoid_cursor = it->avoid_cursor_p;
19953 it->avoid_cursor_p = true;
19954 saved_face_id = it->face_id;
19955 saved_box_start = it->start_of_box_run_p;
19956 /* The last row's stretch glyph should get the default
19957 face, to avoid painting the rest of the window with
19958 the region face, if the region ends at ZV. */
19959 if (it->glyph_row->ends_at_zv_p)
19960 it->face_id = default_face->id;
19961 else
19962 it->face_id = face->id;
19963 it->start_of_box_run_p = false;
19964 append_stretch_glyph (it, Qnil, stretch_width,
19965 it->ascent + it->descent, stretch_ascent);
19966 it->position = saved_pos;
19967 it->avoid_cursor_p = saved_avoid_cursor;
19968 it->face_id = saved_face_id;
19969 it->start_of_box_run_p = saved_box_start;
19971 /* If stretch_width comes out negative, it means that the
19972 last glyph is only partially visible. In R2L rows, we
19973 want the leftmost glyph to be partially visible, so we
19974 need to give the row the corresponding left offset. */
19975 if (stretch_width < 0)
19976 it->glyph_row->x = stretch_width;
19978 #endif /* HAVE_WINDOW_SYSTEM */
19980 else
19982 /* Save some values that must not be changed. */
19983 int saved_x = it->current_x;
19984 struct text_pos saved_pos;
19985 Lisp_Object saved_object;
19986 enum display_element_type saved_what = it->what;
19987 int saved_face_id = it->face_id;
19989 saved_object = it->object;
19990 saved_pos = it->position;
19992 it->what = IT_CHARACTER;
19993 memset (&it->position, 0, sizeof it->position);
19994 it->object = Qnil;
19995 it->c = it->char_to_display = ' ';
19996 it->len = 1;
19998 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19999 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20000 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20001 && !it->glyph_row->mode_line_p
20002 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20004 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20005 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20007 for (it->current_x = 0; g < e; g++)
20008 it->current_x += g->pixel_width;
20010 it->area = LEFT_MARGIN_AREA;
20011 it->face_id = default_face->id;
20012 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20013 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20015 PRODUCE_GLYPHS (it);
20016 /* term.c:produce_glyphs advances it->current_x only for
20017 TEXT_AREA. */
20018 it->current_x += it->pixel_width;
20021 it->current_x = saved_x;
20022 it->area = TEXT_AREA;
20025 /* The last row's blank glyphs should get the default face, to
20026 avoid painting the rest of the window with the region face,
20027 if the region ends at ZV. */
20028 if (it->glyph_row->ends_at_zv_p)
20029 it->face_id = default_face->id;
20030 else
20031 it->face_id = face->id;
20032 PRODUCE_GLYPHS (it);
20034 while (it->current_x <= it->last_visible_x)
20035 PRODUCE_GLYPHS (it);
20037 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20038 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20039 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20040 && !it->glyph_row->mode_line_p
20041 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20043 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20044 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20046 for ( ; g < e; g++)
20047 it->current_x += g->pixel_width;
20049 it->area = RIGHT_MARGIN_AREA;
20050 it->face_id = default_face->id;
20051 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20052 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20054 PRODUCE_GLYPHS (it);
20055 it->current_x += it->pixel_width;
20058 it->area = TEXT_AREA;
20061 /* Don't count these blanks really. It would let us insert a left
20062 truncation glyph below and make us set the cursor on them, maybe. */
20063 it->current_x = saved_x;
20064 it->object = saved_object;
20065 it->position = saved_pos;
20066 it->what = saved_what;
20067 it->face_id = saved_face_id;
20072 /* Value is true if text starting at CHARPOS in current_buffer is
20073 trailing whitespace. */
20075 static bool
20076 trailing_whitespace_p (ptrdiff_t charpos)
20078 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20079 int c = 0;
20081 while (bytepos < ZV_BYTE
20082 && (c = FETCH_CHAR (bytepos),
20083 c == ' ' || c == '\t'))
20084 ++bytepos;
20086 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20088 if (bytepos != PT_BYTE)
20089 return true;
20091 return false;
20095 /* Highlight trailing whitespace, if any, in ROW. */
20097 static void
20098 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20100 int used = row->used[TEXT_AREA];
20102 if (used)
20104 struct glyph *start = row->glyphs[TEXT_AREA];
20105 struct glyph *glyph = start + used - 1;
20107 if (row->reversed_p)
20109 /* Right-to-left rows need to be processed in the opposite
20110 direction, so swap the edge pointers. */
20111 glyph = start;
20112 start = row->glyphs[TEXT_AREA] + used - 1;
20115 /* Skip over glyphs inserted to display the cursor at the
20116 end of a line, for extending the face of the last glyph
20117 to the end of the line on terminals, and for truncation
20118 and continuation glyphs. */
20119 if (!row->reversed_p)
20121 while (glyph >= start
20122 && glyph->type == CHAR_GLYPH
20123 && NILP (glyph->object))
20124 --glyph;
20126 else
20128 while (glyph <= start
20129 && glyph->type == CHAR_GLYPH
20130 && NILP (glyph->object))
20131 ++glyph;
20134 /* If last glyph is a space or stretch, and it's trailing
20135 whitespace, set the face of all trailing whitespace glyphs in
20136 IT->glyph_row to `trailing-whitespace'. */
20137 if ((row->reversed_p ? glyph <= start : glyph >= start)
20138 && BUFFERP (glyph->object)
20139 && (glyph->type == STRETCH_GLYPH
20140 || (glyph->type == CHAR_GLYPH
20141 && glyph->u.ch == ' '))
20142 && trailing_whitespace_p (glyph->charpos))
20144 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20145 if (face_id < 0)
20146 return;
20148 if (!row->reversed_p)
20150 while (glyph >= start
20151 && BUFFERP (glyph->object)
20152 && (glyph->type == STRETCH_GLYPH
20153 || (glyph->type == CHAR_GLYPH
20154 && glyph->u.ch == ' ')))
20155 (glyph--)->face_id = face_id;
20157 else
20159 while (glyph <= start
20160 && BUFFERP (glyph->object)
20161 && (glyph->type == STRETCH_GLYPH
20162 || (glyph->type == CHAR_GLYPH
20163 && glyph->u.ch == ' ')))
20164 (glyph++)->face_id = face_id;
20171 /* Value is true if glyph row ROW should be
20172 considered to hold the buffer position CHARPOS. */
20174 static bool
20175 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20177 bool result = true;
20179 if (charpos == CHARPOS (row->end.pos)
20180 || charpos == MATRIX_ROW_END_CHARPOS (row))
20182 /* Suppose the row ends on a string.
20183 Unless the row is continued, that means it ends on a newline
20184 in the string. If it's anything other than a display string
20185 (e.g., a before-string from an overlay), we don't want the
20186 cursor there. (This heuristic seems to give the optimal
20187 behavior for the various types of multi-line strings.)
20188 One exception: if the string has `cursor' property on one of
20189 its characters, we _do_ want the cursor there. */
20190 if (CHARPOS (row->end.string_pos) >= 0)
20192 if (row->continued_p)
20193 result = true;
20194 else
20196 /* Check for `display' property. */
20197 struct glyph *beg = row->glyphs[TEXT_AREA];
20198 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20199 struct glyph *glyph;
20201 result = false;
20202 for (glyph = end; glyph >= beg; --glyph)
20203 if (STRINGP (glyph->object))
20205 Lisp_Object prop
20206 = Fget_char_property (make_number (charpos),
20207 Qdisplay, Qnil);
20208 result =
20209 (!NILP (prop)
20210 && display_prop_string_p (prop, glyph->object));
20211 /* If there's a `cursor' property on one of the
20212 string's characters, this row is a cursor row,
20213 even though this is not a display string. */
20214 if (!result)
20216 Lisp_Object s = glyph->object;
20218 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20220 ptrdiff_t gpos = glyph->charpos;
20222 if (!NILP (Fget_char_property (make_number (gpos),
20223 Qcursor, s)))
20225 result = true;
20226 break;
20230 break;
20234 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20236 /* If the row ends in middle of a real character,
20237 and the line is continued, we want the cursor here.
20238 That's because CHARPOS (ROW->end.pos) would equal
20239 PT if PT is before the character. */
20240 if (!row->ends_in_ellipsis_p)
20241 result = row->continued_p;
20242 else
20243 /* If the row ends in an ellipsis, then
20244 CHARPOS (ROW->end.pos) will equal point after the
20245 invisible text. We want that position to be displayed
20246 after the ellipsis. */
20247 result = false;
20249 /* If the row ends at ZV, display the cursor at the end of that
20250 row instead of at the start of the row below. */
20251 else
20252 result = row->ends_at_zv_p;
20255 return result;
20258 /* Value is true if glyph row ROW should be
20259 used to hold the cursor. */
20261 static bool
20262 cursor_row_p (struct glyph_row *row)
20264 return row_for_charpos_p (row, PT);
20269 /* Push the property PROP so that it will be rendered at the current
20270 position in IT. Return true if PROP was successfully pushed, false
20271 otherwise. Called from handle_line_prefix to handle the
20272 `line-prefix' and `wrap-prefix' properties. */
20274 static bool
20275 push_prefix_prop (struct it *it, Lisp_Object prop)
20277 struct text_pos pos =
20278 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20280 eassert (it->method == GET_FROM_BUFFER
20281 || it->method == GET_FROM_DISPLAY_VECTOR
20282 || it->method == GET_FROM_STRING
20283 || it->method == GET_FROM_IMAGE);
20285 /* We need to save the current buffer/string position, so it will be
20286 restored by pop_it, because iterate_out_of_display_property
20287 depends on that being set correctly, but some situations leave
20288 it->position not yet set when this function is called. */
20289 push_it (it, &pos);
20291 if (STRINGP (prop))
20293 if (SCHARS (prop) == 0)
20295 pop_it (it);
20296 return false;
20299 it->string = prop;
20300 it->string_from_prefix_prop_p = true;
20301 it->multibyte_p = STRING_MULTIBYTE (it->string);
20302 it->current.overlay_string_index = -1;
20303 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20304 it->end_charpos = it->string_nchars = SCHARS (it->string);
20305 it->method = GET_FROM_STRING;
20306 it->stop_charpos = 0;
20307 it->prev_stop = 0;
20308 it->base_level_stop = 0;
20310 /* Force paragraph direction to be that of the parent
20311 buffer/string. */
20312 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20313 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20314 else
20315 it->paragraph_embedding = L2R;
20317 /* Set up the bidi iterator for this display string. */
20318 if (it->bidi_p)
20320 it->bidi_it.string.lstring = it->string;
20321 it->bidi_it.string.s = NULL;
20322 it->bidi_it.string.schars = it->end_charpos;
20323 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20324 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20325 it->bidi_it.string.unibyte = !it->multibyte_p;
20326 it->bidi_it.w = it->w;
20327 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20330 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20332 it->method = GET_FROM_STRETCH;
20333 it->object = prop;
20335 #ifdef HAVE_WINDOW_SYSTEM
20336 else if (IMAGEP (prop))
20338 it->what = IT_IMAGE;
20339 it->image_id = lookup_image (it->f, prop);
20340 it->method = GET_FROM_IMAGE;
20342 #endif /* HAVE_WINDOW_SYSTEM */
20343 else
20345 pop_it (it); /* bogus display property, give up */
20346 return false;
20349 return true;
20352 /* Return the character-property PROP at the current position in IT. */
20354 static Lisp_Object
20355 get_it_property (struct it *it, Lisp_Object prop)
20357 Lisp_Object position, object = it->object;
20359 if (STRINGP (object))
20360 position = make_number (IT_STRING_CHARPOS (*it));
20361 else if (BUFFERP (object))
20363 position = make_number (IT_CHARPOS (*it));
20364 object = it->window;
20366 else
20367 return Qnil;
20369 return Fget_char_property (position, prop, object);
20372 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20374 static void
20375 handle_line_prefix (struct it *it)
20377 Lisp_Object prefix;
20379 if (it->continuation_lines_width > 0)
20381 prefix = get_it_property (it, Qwrap_prefix);
20382 if (NILP (prefix))
20383 prefix = Vwrap_prefix;
20385 else
20387 prefix = get_it_property (it, Qline_prefix);
20388 if (NILP (prefix))
20389 prefix = Vline_prefix;
20391 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20393 /* If the prefix is wider than the window, and we try to wrap
20394 it, it would acquire its own wrap prefix, and so on till the
20395 iterator stack overflows. So, don't wrap the prefix. */
20396 it->line_wrap = TRUNCATE;
20397 it->avoid_cursor_p = true;
20403 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20404 only for R2L lines from display_line and display_string, when they
20405 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20406 the line/string needs to be continued on the next glyph row. */
20407 static void
20408 unproduce_glyphs (struct it *it, int n)
20410 struct glyph *glyph, *end;
20412 eassert (it->glyph_row);
20413 eassert (it->glyph_row->reversed_p);
20414 eassert (it->area == TEXT_AREA);
20415 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20417 if (n > it->glyph_row->used[TEXT_AREA])
20418 n = it->glyph_row->used[TEXT_AREA];
20419 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20420 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20421 for ( ; glyph < end; glyph++)
20422 glyph[-n] = *glyph;
20425 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20426 and ROW->maxpos. */
20427 static void
20428 find_row_edges (struct it *it, struct glyph_row *row,
20429 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20430 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20432 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20433 lines' rows is implemented for bidi-reordered rows. */
20435 /* ROW->minpos is the value of min_pos, the minimal buffer position
20436 we have in ROW, or ROW->start.pos if that is smaller. */
20437 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20438 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20439 else
20440 /* We didn't find buffer positions smaller than ROW->start, or
20441 didn't find _any_ valid buffer positions in any of the glyphs,
20442 so we must trust the iterator's computed positions. */
20443 row->minpos = row->start.pos;
20444 if (max_pos <= 0)
20446 max_pos = CHARPOS (it->current.pos);
20447 max_bpos = BYTEPOS (it->current.pos);
20450 /* Here are the various use-cases for ending the row, and the
20451 corresponding values for ROW->maxpos:
20453 Line ends in a newline from buffer eol_pos + 1
20454 Line is continued from buffer max_pos + 1
20455 Line is truncated on right it->current.pos
20456 Line ends in a newline from string max_pos + 1(*)
20457 (*) + 1 only when line ends in a forward scan
20458 Line is continued from string max_pos
20459 Line is continued from display vector max_pos
20460 Line is entirely from a string min_pos == max_pos
20461 Line is entirely from a display vector min_pos == max_pos
20462 Line that ends at ZV ZV
20464 If you discover other use-cases, please add them here as
20465 appropriate. */
20466 if (row->ends_at_zv_p)
20467 row->maxpos = it->current.pos;
20468 else if (row->used[TEXT_AREA])
20470 bool seen_this_string = false;
20471 struct glyph_row *r1 = row - 1;
20473 /* Did we see the same display string on the previous row? */
20474 if (STRINGP (it->object)
20475 /* this is not the first row */
20476 && row > it->w->desired_matrix->rows
20477 /* previous row is not the header line */
20478 && !r1->mode_line_p
20479 /* previous row also ends in a newline from a string */
20480 && r1->ends_in_newline_from_string_p)
20482 struct glyph *start, *end;
20484 /* Search for the last glyph of the previous row that came
20485 from buffer or string. Depending on whether the row is
20486 L2R or R2L, we need to process it front to back or the
20487 other way round. */
20488 if (!r1->reversed_p)
20490 start = r1->glyphs[TEXT_AREA];
20491 end = start + r1->used[TEXT_AREA];
20492 /* Glyphs inserted by redisplay have nil as their object. */
20493 while (end > start
20494 && NILP ((end - 1)->object)
20495 && (end - 1)->charpos <= 0)
20496 --end;
20497 if (end > start)
20499 if (EQ ((end - 1)->object, it->object))
20500 seen_this_string = true;
20502 else
20503 /* If all the glyphs of the previous row were inserted
20504 by redisplay, it means the previous row was
20505 produced from a single newline, which is only
20506 possible if that newline came from the same string
20507 as the one which produced this ROW. */
20508 seen_this_string = true;
20510 else
20512 end = r1->glyphs[TEXT_AREA] - 1;
20513 start = end + r1->used[TEXT_AREA];
20514 while (end < start
20515 && NILP ((end + 1)->object)
20516 && (end + 1)->charpos <= 0)
20517 ++end;
20518 if (end < start)
20520 if (EQ ((end + 1)->object, it->object))
20521 seen_this_string = true;
20523 else
20524 seen_this_string = true;
20527 /* Take note of each display string that covers a newline only
20528 once, the first time we see it. This is for when a display
20529 string includes more than one newline in it. */
20530 if (row->ends_in_newline_from_string_p && !seen_this_string)
20532 /* If we were scanning the buffer forward when we displayed
20533 the string, we want to account for at least one buffer
20534 position that belongs to this row (position covered by
20535 the display string), so that cursor positioning will
20536 consider this row as a candidate when point is at the end
20537 of the visual line represented by this row. This is not
20538 required when scanning back, because max_pos will already
20539 have a much larger value. */
20540 if (CHARPOS (row->end.pos) > max_pos)
20541 INC_BOTH (max_pos, max_bpos);
20542 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20544 else if (CHARPOS (it->eol_pos) > 0)
20545 SET_TEXT_POS (row->maxpos,
20546 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20547 else if (row->continued_p)
20549 /* If max_pos is different from IT's current position, it
20550 means IT->method does not belong to the display element
20551 at max_pos. However, it also means that the display
20552 element at max_pos was displayed in its entirety on this
20553 line, which is equivalent to saying that the next line
20554 starts at the next buffer position. */
20555 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20556 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20557 else
20559 INC_BOTH (max_pos, max_bpos);
20560 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20563 else if (row->truncated_on_right_p)
20564 /* display_line already called reseat_at_next_visible_line_start,
20565 which puts the iterator at the beginning of the next line, in
20566 the logical order. */
20567 row->maxpos = it->current.pos;
20568 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20569 /* A line that is entirely from a string/image/stretch... */
20570 row->maxpos = row->minpos;
20571 else
20572 emacs_abort ();
20574 else
20575 row->maxpos = it->current.pos;
20578 /* Construct the glyph row IT->glyph_row in the desired matrix of
20579 IT->w from text at the current position of IT. See dispextern.h
20580 for an overview of struct it. Value is true if
20581 IT->glyph_row displays text, as opposed to a line displaying ZV
20582 only. */
20584 static bool
20585 display_line (struct it *it)
20587 struct glyph_row *row = it->glyph_row;
20588 Lisp_Object overlay_arrow_string;
20589 struct it wrap_it;
20590 void *wrap_data = NULL;
20591 bool may_wrap = false;
20592 int wrap_x UNINIT;
20593 int wrap_row_used = -1;
20594 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20595 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20596 int wrap_row_extra_line_spacing UNINIT;
20597 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20598 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20599 int cvpos;
20600 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20601 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20602 bool pending_handle_line_prefix = false;
20604 /* We always start displaying at hpos zero even if hscrolled. */
20605 eassert (it->hpos == 0 && it->current_x == 0);
20607 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20608 >= it->w->desired_matrix->nrows)
20610 it->w->nrows_scale_factor++;
20611 it->f->fonts_changed = true;
20612 return false;
20615 /* Clear the result glyph row and enable it. */
20616 prepare_desired_row (it->w, row, false);
20618 row->y = it->current_y;
20619 row->start = it->start;
20620 row->continuation_lines_width = it->continuation_lines_width;
20621 row->displays_text_p = true;
20622 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20623 it->starts_in_middle_of_char_p = false;
20625 /* Arrange the overlays nicely for our purposes. Usually, we call
20626 display_line on only one line at a time, in which case this
20627 can't really hurt too much, or we call it on lines which appear
20628 one after another in the buffer, in which case all calls to
20629 recenter_overlay_lists but the first will be pretty cheap. */
20630 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20632 /* Move over display elements that are not visible because we are
20633 hscrolled. This may stop at an x-position < IT->first_visible_x
20634 if the first glyph is partially visible or if we hit a line end. */
20635 if (it->current_x < it->first_visible_x)
20637 enum move_it_result move_result;
20639 this_line_min_pos = row->start.pos;
20640 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20641 MOVE_TO_POS | MOVE_TO_X);
20642 /* If we are under a large hscroll, move_it_in_display_line_to
20643 could hit the end of the line without reaching
20644 it->first_visible_x. Pretend that we did reach it. This is
20645 especially important on a TTY, where we will call
20646 extend_face_to_end_of_line, which needs to know how many
20647 blank glyphs to produce. */
20648 if (it->current_x < it->first_visible_x
20649 && (move_result == MOVE_NEWLINE_OR_CR
20650 || move_result == MOVE_POS_MATCH_OR_ZV))
20651 it->current_x = it->first_visible_x;
20653 /* Record the smallest positions seen while we moved over
20654 display elements that are not visible. This is needed by
20655 redisplay_internal for optimizing the case where the cursor
20656 stays inside the same line. The rest of this function only
20657 considers positions that are actually displayed, so
20658 RECORD_MAX_MIN_POS will not otherwise record positions that
20659 are hscrolled to the left of the left edge of the window. */
20660 min_pos = CHARPOS (this_line_min_pos);
20661 min_bpos = BYTEPOS (this_line_min_pos);
20663 else if (it->area == TEXT_AREA)
20665 /* We only do this when not calling move_it_in_display_line_to
20666 above, because that function calls itself handle_line_prefix. */
20667 handle_line_prefix (it);
20669 else
20671 /* Line-prefix and wrap-prefix are always displayed in the text
20672 area. But if this is the first call to display_line after
20673 init_iterator, the iterator might have been set up to write
20674 into a marginal area, e.g. if the line begins with some
20675 display property that writes to the margins. So we need to
20676 wait with the call to handle_line_prefix until whatever
20677 writes to the margin has done its job. */
20678 pending_handle_line_prefix = true;
20681 /* Get the initial row height. This is either the height of the
20682 text hscrolled, if there is any, or zero. */
20683 row->ascent = it->max_ascent;
20684 row->height = it->max_ascent + it->max_descent;
20685 row->phys_ascent = it->max_phys_ascent;
20686 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20687 row->extra_line_spacing = it->max_extra_line_spacing;
20689 /* Utility macro to record max and min buffer positions seen until now. */
20690 #define RECORD_MAX_MIN_POS(IT) \
20691 do \
20693 bool composition_p \
20694 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20695 ptrdiff_t current_pos = \
20696 composition_p ? (IT)->cmp_it.charpos \
20697 : IT_CHARPOS (*(IT)); \
20698 ptrdiff_t current_bpos = \
20699 composition_p ? CHAR_TO_BYTE (current_pos) \
20700 : IT_BYTEPOS (*(IT)); \
20701 if (current_pos < min_pos) \
20703 min_pos = current_pos; \
20704 min_bpos = current_bpos; \
20706 if (IT_CHARPOS (*it) > max_pos) \
20708 max_pos = IT_CHARPOS (*it); \
20709 max_bpos = IT_BYTEPOS (*it); \
20712 while (false)
20714 /* Loop generating characters. The loop is left with IT on the next
20715 character to display. */
20716 while (true)
20718 int n_glyphs_before, hpos_before, x_before;
20719 int x, nglyphs;
20720 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20722 /* Retrieve the next thing to display. Value is false if end of
20723 buffer reached. */
20724 if (!get_next_display_element (it))
20726 /* Maybe add a space at the end of this line that is used to
20727 display the cursor there under X. Set the charpos of the
20728 first glyph of blank lines not corresponding to any text
20729 to -1. */
20730 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20731 row->exact_window_width_line_p = true;
20732 else if ((append_space_for_newline (it, true)
20733 && row->used[TEXT_AREA] == 1)
20734 || row->used[TEXT_AREA] == 0)
20736 row->glyphs[TEXT_AREA]->charpos = -1;
20737 row->displays_text_p = false;
20739 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20740 && (!MINI_WINDOW_P (it->w)
20741 || (minibuf_level && EQ (it->window, minibuf_window))))
20742 row->indicate_empty_line_p = true;
20745 it->continuation_lines_width = 0;
20746 row->ends_at_zv_p = true;
20747 /* A row that displays right-to-left text must always have
20748 its last face extended all the way to the end of line,
20749 even if this row ends in ZV, because we still write to
20750 the screen left to right. We also need to extend the
20751 last face if the default face is remapped to some
20752 different face, otherwise the functions that clear
20753 portions of the screen will clear with the default face's
20754 background color. */
20755 if (row->reversed_p
20756 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20757 extend_face_to_end_of_line (it);
20758 break;
20761 /* Now, get the metrics of what we want to display. This also
20762 generates glyphs in `row' (which is IT->glyph_row). */
20763 n_glyphs_before = row->used[TEXT_AREA];
20764 x = it->current_x;
20766 /* Remember the line height so far in case the next element doesn't
20767 fit on the line. */
20768 if (it->line_wrap != TRUNCATE)
20770 ascent = it->max_ascent;
20771 descent = it->max_descent;
20772 phys_ascent = it->max_phys_ascent;
20773 phys_descent = it->max_phys_descent;
20775 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20777 if (IT_DISPLAYING_WHITESPACE (it))
20778 may_wrap = true;
20779 else if (may_wrap)
20781 SAVE_IT (wrap_it, *it, wrap_data);
20782 wrap_x = x;
20783 wrap_row_used = row->used[TEXT_AREA];
20784 wrap_row_ascent = row->ascent;
20785 wrap_row_height = row->height;
20786 wrap_row_phys_ascent = row->phys_ascent;
20787 wrap_row_phys_height = row->phys_height;
20788 wrap_row_extra_line_spacing = row->extra_line_spacing;
20789 wrap_row_min_pos = min_pos;
20790 wrap_row_min_bpos = min_bpos;
20791 wrap_row_max_pos = max_pos;
20792 wrap_row_max_bpos = max_bpos;
20793 may_wrap = false;
20798 PRODUCE_GLYPHS (it);
20800 /* If this display element was in marginal areas, continue with
20801 the next one. */
20802 if (it->area != TEXT_AREA)
20804 row->ascent = max (row->ascent, it->max_ascent);
20805 row->height = max (row->height, it->max_ascent + it->max_descent);
20806 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20807 row->phys_height = max (row->phys_height,
20808 it->max_phys_ascent + it->max_phys_descent);
20809 row->extra_line_spacing = max (row->extra_line_spacing,
20810 it->max_extra_line_spacing);
20811 set_iterator_to_next (it, true);
20812 /* If we didn't handle the line/wrap prefix above, and the
20813 call to set_iterator_to_next just switched to TEXT_AREA,
20814 process the prefix now. */
20815 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20817 pending_handle_line_prefix = false;
20818 handle_line_prefix (it);
20820 continue;
20823 /* Does the display element fit on the line? If we truncate
20824 lines, we should draw past the right edge of the window. If
20825 we don't truncate, we want to stop so that we can display the
20826 continuation glyph before the right margin. If lines are
20827 continued, there are two possible strategies for characters
20828 resulting in more than 1 glyph (e.g. tabs): Display as many
20829 glyphs as possible in this line and leave the rest for the
20830 continuation line, or display the whole element in the next
20831 line. Original redisplay did the former, so we do it also. */
20832 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20833 hpos_before = it->hpos;
20834 x_before = x;
20836 if (/* Not a newline. */
20837 nglyphs > 0
20838 /* Glyphs produced fit entirely in the line. */
20839 && it->current_x < it->last_visible_x)
20841 it->hpos += nglyphs;
20842 row->ascent = max (row->ascent, it->max_ascent);
20843 row->height = max (row->height, it->max_ascent + it->max_descent);
20844 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20845 row->phys_height = max (row->phys_height,
20846 it->max_phys_ascent + it->max_phys_descent);
20847 row->extra_line_spacing = max (row->extra_line_spacing,
20848 it->max_extra_line_spacing);
20849 if (it->current_x - it->pixel_width < it->first_visible_x
20850 /* In R2L rows, we arrange in extend_face_to_end_of_line
20851 to add a right offset to the line, by a suitable
20852 change to the stretch glyph that is the leftmost
20853 glyph of the line. */
20854 && !row->reversed_p)
20855 row->x = x - it->first_visible_x;
20856 /* Record the maximum and minimum buffer positions seen so
20857 far in glyphs that will be displayed by this row. */
20858 if (it->bidi_p)
20859 RECORD_MAX_MIN_POS (it);
20861 else
20863 int i, new_x;
20864 struct glyph *glyph;
20866 for (i = 0; i < nglyphs; ++i, x = new_x)
20868 /* Identify the glyphs added by the last call to
20869 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20870 the previous glyphs. */
20871 if (!row->reversed_p)
20872 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20873 else
20874 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20875 new_x = x + glyph->pixel_width;
20877 if (/* Lines are continued. */
20878 it->line_wrap != TRUNCATE
20879 && (/* Glyph doesn't fit on the line. */
20880 new_x > it->last_visible_x
20881 /* Or it fits exactly on a window system frame. */
20882 || (new_x == it->last_visible_x
20883 && FRAME_WINDOW_P (it->f)
20884 && (row->reversed_p
20885 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20886 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20888 /* End of a continued line. */
20890 if (it->hpos == 0
20891 || (new_x == it->last_visible_x
20892 && FRAME_WINDOW_P (it->f)
20893 && (row->reversed_p
20894 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20895 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20897 /* Current glyph is the only one on the line or
20898 fits exactly on the line. We must continue
20899 the line because we can't draw the cursor
20900 after the glyph. */
20901 row->continued_p = true;
20902 it->current_x = new_x;
20903 it->continuation_lines_width += new_x;
20904 ++it->hpos;
20905 if (i == nglyphs - 1)
20907 /* If line-wrap is on, check if a previous
20908 wrap point was found. */
20909 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20910 && wrap_row_used > 0
20911 /* Even if there is a previous wrap
20912 point, continue the line here as
20913 usual, if (i) the previous character
20914 was a space or tab AND (ii) the
20915 current character is not. */
20916 && (!may_wrap
20917 || IT_DISPLAYING_WHITESPACE (it)))
20918 goto back_to_wrap;
20920 /* Record the maximum and minimum buffer
20921 positions seen so far in glyphs that will be
20922 displayed by this row. */
20923 if (it->bidi_p)
20924 RECORD_MAX_MIN_POS (it);
20925 set_iterator_to_next (it, true);
20926 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20928 if (!get_next_display_element (it))
20930 row->exact_window_width_line_p = true;
20931 it->continuation_lines_width = 0;
20932 row->continued_p = false;
20933 row->ends_at_zv_p = true;
20935 else if (ITERATOR_AT_END_OF_LINE_P (it))
20937 row->continued_p = false;
20938 row->exact_window_width_line_p = true;
20940 /* If line-wrap is on, check if a
20941 previous wrap point was found. */
20942 else if (wrap_row_used > 0
20943 /* Even if there is a previous wrap
20944 point, continue the line here as
20945 usual, if (i) the previous character
20946 was a space or tab AND (ii) the
20947 current character is not. */
20948 && (!may_wrap
20949 || IT_DISPLAYING_WHITESPACE (it)))
20950 goto back_to_wrap;
20954 else if (it->bidi_p)
20955 RECORD_MAX_MIN_POS (it);
20956 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20957 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20958 extend_face_to_end_of_line (it);
20960 else if (CHAR_GLYPH_PADDING_P (*glyph)
20961 && !FRAME_WINDOW_P (it->f))
20963 /* A padding glyph that doesn't fit on this line.
20964 This means the whole character doesn't fit
20965 on the line. */
20966 if (row->reversed_p)
20967 unproduce_glyphs (it, row->used[TEXT_AREA]
20968 - n_glyphs_before);
20969 row->used[TEXT_AREA] = n_glyphs_before;
20971 /* Fill the rest of the row with continuation
20972 glyphs like in 20.x. */
20973 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20974 < row->glyphs[1 + TEXT_AREA])
20975 produce_special_glyphs (it, IT_CONTINUATION);
20977 row->continued_p = true;
20978 it->current_x = x_before;
20979 it->continuation_lines_width += x_before;
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;
20987 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20988 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20989 extend_face_to_end_of_line (it);
20991 else if (wrap_row_used > 0)
20993 back_to_wrap:
20994 if (row->reversed_p)
20995 unproduce_glyphs (it,
20996 row->used[TEXT_AREA] - wrap_row_used);
20997 RESTORE_IT (it, &wrap_it, wrap_data);
20998 it->continuation_lines_width += wrap_x;
20999 row->used[TEXT_AREA] = wrap_row_used;
21000 row->ascent = wrap_row_ascent;
21001 row->height = wrap_row_height;
21002 row->phys_ascent = wrap_row_phys_ascent;
21003 row->phys_height = wrap_row_phys_height;
21004 row->extra_line_spacing = wrap_row_extra_line_spacing;
21005 min_pos = wrap_row_min_pos;
21006 min_bpos = wrap_row_min_bpos;
21007 max_pos = wrap_row_max_pos;
21008 max_bpos = wrap_row_max_bpos;
21009 row->continued_p = true;
21010 row->ends_at_zv_p = false;
21011 row->exact_window_width_line_p = false;
21012 it->continuation_lines_width += x;
21014 /* Make sure that a non-default face is extended
21015 up to the right margin of the window. */
21016 extend_face_to_end_of_line (it);
21018 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
21020 /* A TAB that extends past the right edge of the
21021 window. This produces a single glyph on
21022 window system frames. We leave the glyph in
21023 this row and let it fill the row, but don't
21024 consume the TAB. */
21025 if ((row->reversed_p
21026 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21027 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21028 produce_special_glyphs (it, IT_CONTINUATION);
21029 it->continuation_lines_width += it->last_visible_x;
21030 row->ends_in_middle_of_char_p = true;
21031 row->continued_p = true;
21032 glyph->pixel_width = it->last_visible_x - x;
21033 it->starts_in_middle_of_char_p = true;
21034 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21035 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21036 extend_face_to_end_of_line (it);
21038 else
21040 /* Something other than a TAB that draws past
21041 the right edge of the window. Restore
21042 positions to values before the element. */
21043 if (row->reversed_p)
21044 unproduce_glyphs (it, row->used[TEXT_AREA]
21045 - (n_glyphs_before + i));
21046 row->used[TEXT_AREA] = n_glyphs_before + i;
21048 /* Display continuation glyphs. */
21049 it->current_x = x_before;
21050 it->continuation_lines_width += x;
21051 if (!FRAME_WINDOW_P (it->f)
21052 || (row->reversed_p
21053 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21054 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21055 produce_special_glyphs (it, IT_CONTINUATION);
21056 row->continued_p = true;
21058 extend_face_to_end_of_line (it);
21060 if (nglyphs > 1 && i > 0)
21062 row->ends_in_middle_of_char_p = true;
21063 it->starts_in_middle_of_char_p = true;
21066 /* Restore the height to what it was before the
21067 element not fitting on the line. */
21068 it->max_ascent = ascent;
21069 it->max_descent = descent;
21070 it->max_phys_ascent = phys_ascent;
21071 it->max_phys_descent = phys_descent;
21074 break;
21076 else if (new_x > it->first_visible_x)
21078 /* Increment number of glyphs actually displayed. */
21079 ++it->hpos;
21081 /* Record the maximum and minimum buffer positions
21082 seen so far in glyphs that will be displayed by
21083 this row. */
21084 if (it->bidi_p)
21085 RECORD_MAX_MIN_POS (it);
21087 if (x < it->first_visible_x && !row->reversed_p)
21088 /* Glyph is partially visible, i.e. row starts at
21089 negative X position. Don't do that in R2L
21090 rows, where we arrange to add a right offset to
21091 the line in extend_face_to_end_of_line, by a
21092 suitable change to the stretch glyph that is
21093 the leftmost glyph of the line. */
21094 row->x = x - it->first_visible_x;
21095 /* When the last glyph of an R2L row only fits
21096 partially on the line, we need to set row->x to a
21097 negative offset, so that the leftmost glyph is
21098 the one that is partially visible. But if we are
21099 going to produce the truncation glyph, this will
21100 be taken care of in produce_special_glyphs. */
21101 if (row->reversed_p
21102 && new_x > it->last_visible_x
21103 && !(it->line_wrap == TRUNCATE
21104 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21106 eassert (FRAME_WINDOW_P (it->f));
21107 row->x = it->last_visible_x - new_x;
21110 else
21112 /* Glyph is completely off the left margin of the
21113 window. This should not happen because of the
21114 move_it_in_display_line at the start of this
21115 function, unless the text display area of the
21116 window is empty. */
21117 eassert (it->first_visible_x <= it->last_visible_x);
21120 /* Even if this display element produced no glyphs at all,
21121 we want to record its position. */
21122 if (it->bidi_p && nglyphs == 0)
21123 RECORD_MAX_MIN_POS (it);
21125 row->ascent = max (row->ascent, it->max_ascent);
21126 row->height = max (row->height, it->max_ascent + it->max_descent);
21127 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21128 row->phys_height = max (row->phys_height,
21129 it->max_phys_ascent + it->max_phys_descent);
21130 row->extra_line_spacing = max (row->extra_line_spacing,
21131 it->max_extra_line_spacing);
21133 /* End of this display line if row is continued. */
21134 if (row->continued_p || row->ends_at_zv_p)
21135 break;
21138 at_end_of_line:
21139 /* Is this a line end? If yes, we're also done, after making
21140 sure that a non-default face is extended up to the right
21141 margin of the window. */
21142 if (ITERATOR_AT_END_OF_LINE_P (it))
21144 int used_before = row->used[TEXT_AREA];
21146 row->ends_in_newline_from_string_p = STRINGP (it->object);
21148 /* Add a space at the end of the line that is used to
21149 display the cursor there. */
21150 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21151 append_space_for_newline (it, false);
21153 /* Extend the face to the end of the line. */
21154 extend_face_to_end_of_line (it);
21156 /* Make sure we have the position. */
21157 if (used_before == 0)
21158 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21160 /* Record the position of the newline, for use in
21161 find_row_edges. */
21162 it->eol_pos = it->current.pos;
21164 /* Consume the line end. This skips over invisible lines. */
21165 set_iterator_to_next (it, true);
21166 it->continuation_lines_width = 0;
21167 break;
21170 /* Proceed with next display element. Note that this skips
21171 over lines invisible because of selective display. */
21172 set_iterator_to_next (it, true);
21174 /* If we truncate lines, we are done when the last displayed
21175 glyphs reach past the right margin of the window. */
21176 if (it->line_wrap == TRUNCATE
21177 && ((FRAME_WINDOW_P (it->f)
21178 /* Images are preprocessed in produce_image_glyph such
21179 that they are cropped at the right edge of the
21180 window, so an image glyph will always end exactly at
21181 last_visible_x, even if there's no right fringe. */
21182 && ((row->reversed_p
21183 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21184 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21185 || it->what == IT_IMAGE))
21186 ? (it->current_x >= it->last_visible_x)
21187 : (it->current_x > it->last_visible_x)))
21189 /* Maybe add truncation glyphs. */
21190 if (!FRAME_WINDOW_P (it->f)
21191 || (row->reversed_p
21192 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21193 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21195 int i, n;
21197 if (!row->reversed_p)
21199 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21200 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21201 break;
21203 else
21205 for (i = 0; i < row->used[TEXT_AREA]; i++)
21206 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21207 break;
21208 /* Remove any padding glyphs at the front of ROW, to
21209 make room for the truncation glyphs we will be
21210 adding below. The loop below always inserts at
21211 least one truncation glyph, so also remove the
21212 last glyph added to ROW. */
21213 unproduce_glyphs (it, i + 1);
21214 /* Adjust i for the loop below. */
21215 i = row->used[TEXT_AREA] - (i + 1);
21218 /* produce_special_glyphs overwrites the last glyph, so
21219 we don't want that if we want to keep that last
21220 glyph, which means it's an image. */
21221 if (it->current_x > it->last_visible_x)
21223 it->current_x = x_before;
21224 if (!FRAME_WINDOW_P (it->f))
21226 for (n = row->used[TEXT_AREA]; i < n; ++i)
21228 row->used[TEXT_AREA] = i;
21229 produce_special_glyphs (it, IT_TRUNCATION);
21232 else
21234 row->used[TEXT_AREA] = i;
21235 produce_special_glyphs (it, IT_TRUNCATION);
21237 it->hpos = hpos_before;
21240 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21242 /* Don't truncate if we can overflow newline into fringe. */
21243 if (!get_next_display_element (it))
21245 it->continuation_lines_width = 0;
21246 row->ends_at_zv_p = true;
21247 row->exact_window_width_line_p = true;
21248 break;
21250 if (ITERATOR_AT_END_OF_LINE_P (it))
21252 row->exact_window_width_line_p = true;
21253 goto at_end_of_line;
21255 it->current_x = x_before;
21256 it->hpos = hpos_before;
21259 row->truncated_on_right_p = true;
21260 it->continuation_lines_width = 0;
21261 reseat_at_next_visible_line_start (it, false);
21262 /* We insist below that IT's position be at ZV because in
21263 bidi-reordered lines the character at visible line start
21264 might not be the character that follows the newline in
21265 the logical order. */
21266 if (IT_BYTEPOS (*it) > BEG_BYTE)
21267 row->ends_at_zv_p =
21268 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21269 else
21270 row->ends_at_zv_p = false;
21271 break;
21275 if (wrap_data)
21276 bidi_unshelve_cache (wrap_data, true);
21278 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21279 at the left window margin. */
21280 if (it->first_visible_x
21281 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21283 if (!FRAME_WINDOW_P (it->f)
21284 || (((row->reversed_p
21285 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21286 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21287 /* Don't let insert_left_trunc_glyphs overwrite the
21288 first glyph of the row if it is an image. */
21289 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21290 insert_left_trunc_glyphs (it);
21291 row->truncated_on_left_p = true;
21294 /* Remember the position at which this line ends.
21296 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21297 cannot be before the call to find_row_edges below, since that is
21298 where these positions are determined. */
21299 row->end = it->current;
21300 if (!it->bidi_p)
21302 row->minpos = row->start.pos;
21303 row->maxpos = row->end.pos;
21305 else
21307 /* ROW->minpos and ROW->maxpos must be the smallest and
21308 `1 + the largest' buffer positions in ROW. But if ROW was
21309 bidi-reordered, these two positions can be anywhere in the
21310 row, so we must determine them now. */
21311 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21314 /* If the start of this line is the overlay arrow-position, then
21315 mark this glyph row as the one containing the overlay arrow.
21316 This is clearly a mess with variable size fonts. It would be
21317 better to let it be displayed like cursors under X. */
21318 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21319 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21320 !NILP (overlay_arrow_string)))
21322 /* Overlay arrow in window redisplay is a fringe bitmap. */
21323 if (STRINGP (overlay_arrow_string))
21325 struct glyph_row *arrow_row
21326 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21327 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21328 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21329 struct glyph *p = row->glyphs[TEXT_AREA];
21330 struct glyph *p2, *end;
21332 /* Copy the arrow glyphs. */
21333 while (glyph < arrow_end)
21334 *p++ = *glyph++;
21336 /* Throw away padding glyphs. */
21337 p2 = p;
21338 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21339 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21340 ++p2;
21341 if (p2 > p)
21343 while (p2 < end)
21344 *p++ = *p2++;
21345 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21348 else
21350 eassert (INTEGERP (overlay_arrow_string));
21351 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21353 overlay_arrow_seen = true;
21356 /* Highlight trailing whitespace. */
21357 if (!NILP (Vshow_trailing_whitespace))
21358 highlight_trailing_whitespace (it->f, it->glyph_row);
21360 /* Compute pixel dimensions of this line. */
21361 compute_line_metrics (it);
21363 /* Implementation note: No changes in the glyphs of ROW or in their
21364 faces can be done past this point, because compute_line_metrics
21365 computes ROW's hash value and stores it within the glyph_row
21366 structure. */
21368 /* Record whether this row ends inside an ellipsis. */
21369 row->ends_in_ellipsis_p
21370 = (it->method == GET_FROM_DISPLAY_VECTOR
21371 && it->ellipsis_p);
21373 /* Save fringe bitmaps in this row. */
21374 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21375 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21376 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21377 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21379 it->left_user_fringe_bitmap = 0;
21380 it->left_user_fringe_face_id = 0;
21381 it->right_user_fringe_bitmap = 0;
21382 it->right_user_fringe_face_id = 0;
21384 /* Maybe set the cursor. */
21385 cvpos = it->w->cursor.vpos;
21386 if ((cvpos < 0
21387 /* In bidi-reordered rows, keep checking for proper cursor
21388 position even if one has been found already, because buffer
21389 positions in such rows change non-linearly with ROW->VPOS,
21390 when a line is continued. One exception: when we are at ZV,
21391 display cursor on the first suitable glyph row, since all
21392 the empty rows after that also have their position set to ZV. */
21393 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21394 lines' rows is implemented for bidi-reordered rows. */
21395 || (it->bidi_p
21396 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21397 && PT >= MATRIX_ROW_START_CHARPOS (row)
21398 && PT <= MATRIX_ROW_END_CHARPOS (row)
21399 && cursor_row_p (row))
21400 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21402 /* Prepare for the next line. This line starts horizontally at (X
21403 HPOS) = (0 0). Vertical positions are incremented. As a
21404 convenience for the caller, IT->glyph_row is set to the next
21405 row to be used. */
21406 it->current_x = it->hpos = 0;
21407 it->current_y += row->height;
21408 SET_TEXT_POS (it->eol_pos, 0, 0);
21409 ++it->vpos;
21410 ++it->glyph_row;
21411 /* The next row should by default use the same value of the
21412 reversed_p flag as this one. set_iterator_to_next decides when
21413 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21414 the flag accordingly. */
21415 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21416 it->glyph_row->reversed_p = row->reversed_p;
21417 it->start = row->end;
21418 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21420 #undef RECORD_MAX_MIN_POS
21423 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21424 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21425 doc: /* Return paragraph direction at point in BUFFER.
21426 Value is either `left-to-right' or `right-to-left'.
21427 If BUFFER is omitted or nil, it defaults to the current buffer.
21429 Paragraph direction determines how the text in the paragraph is displayed.
21430 In left-to-right paragraphs, text begins at the left margin of the window
21431 and the reading direction is generally left to right. In right-to-left
21432 paragraphs, text begins at the right margin and is read from right to left.
21434 See also `bidi-paragraph-direction'. */)
21435 (Lisp_Object buffer)
21437 struct buffer *buf = current_buffer;
21438 struct buffer *old = buf;
21440 if (! NILP (buffer))
21442 CHECK_BUFFER (buffer);
21443 buf = XBUFFER (buffer);
21446 if (NILP (BVAR (buf, bidi_display_reordering))
21447 || NILP (BVAR (buf, enable_multibyte_characters))
21448 /* When we are loading loadup.el, the character property tables
21449 needed for bidi iteration are not yet available. */
21450 || redisplay__inhibit_bidi)
21451 return Qleft_to_right;
21452 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21453 return BVAR (buf, bidi_paragraph_direction);
21454 else
21456 /* Determine the direction from buffer text. We could try to
21457 use current_matrix if it is up to date, but this seems fast
21458 enough as it is. */
21459 struct bidi_it itb;
21460 ptrdiff_t pos = BUF_PT (buf);
21461 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21462 int c;
21463 void *itb_data = bidi_shelve_cache ();
21465 set_buffer_temp (buf);
21466 /* bidi_paragraph_init finds the base direction of the paragraph
21467 by searching forward from paragraph start. We need the base
21468 direction of the current or _previous_ paragraph, so we need
21469 to make sure we are within that paragraph. To that end, find
21470 the previous non-empty line. */
21471 if (pos >= ZV && pos > BEGV)
21472 DEC_BOTH (pos, bytepos);
21473 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21474 if (fast_looking_at (trailing_white_space,
21475 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21477 while ((c = FETCH_BYTE (bytepos)) == '\n'
21478 || c == ' ' || c == '\t' || c == '\f')
21480 if (bytepos <= BEGV_BYTE)
21481 break;
21482 bytepos--;
21483 pos--;
21485 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21486 bytepos--;
21488 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21489 itb.paragraph_dir = NEUTRAL_DIR;
21490 itb.string.s = NULL;
21491 itb.string.lstring = Qnil;
21492 itb.string.bufpos = 0;
21493 itb.string.from_disp_str = false;
21494 itb.string.unibyte = false;
21495 /* We have no window to use here for ignoring window-specific
21496 overlays. Using NULL for window pointer will cause
21497 compute_display_string_pos to use the current buffer. */
21498 itb.w = NULL;
21499 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21500 bidi_unshelve_cache (itb_data, false);
21501 set_buffer_temp (old);
21502 switch (itb.paragraph_dir)
21504 case L2R:
21505 return Qleft_to_right;
21506 break;
21507 case R2L:
21508 return Qright_to_left;
21509 break;
21510 default:
21511 emacs_abort ();
21516 DEFUN ("bidi-find-overridden-directionality",
21517 Fbidi_find_overridden_directionality,
21518 Sbidi_find_overridden_directionality, 2, 3, 0,
21519 doc: /* Return position between FROM and TO where directionality was overridden.
21521 This function returns the first character position in the specified
21522 region of OBJECT where there is a character whose `bidi-class' property
21523 is `L', but which was forced to display as `R' by a directional
21524 override, and likewise with characters whose `bidi-class' is `R'
21525 or `AL' that were forced to display as `L'.
21527 If no such character is found, the function returns nil.
21529 OBJECT is a Lisp string or buffer to search for overridden
21530 directionality, and defaults to the current buffer if nil or omitted.
21531 OBJECT can also be a window, in which case the function will search
21532 the buffer displayed in that window. Passing the window instead of
21533 a buffer is preferable when the buffer is displayed in some window,
21534 because this function will then be able to correctly account for
21535 window-specific overlays, which can affect the results.
21537 Strong directional characters `L', `R', and `AL' can have their
21538 intrinsic directionality overridden by directional override
21539 control characters RLO (u+202e) and LRO (u+202d). See the
21540 function `get-char-code-property' for a way to inquire about
21541 the `bidi-class' property of a character. */)
21542 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21544 struct buffer *buf = current_buffer;
21545 struct buffer *old = buf;
21546 struct window *w = NULL;
21547 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21548 struct bidi_it itb;
21549 ptrdiff_t from_pos, to_pos, from_bpos;
21550 void *itb_data;
21552 if (!NILP (object))
21554 if (BUFFERP (object))
21555 buf = XBUFFER (object);
21556 else if (WINDOWP (object))
21558 w = decode_live_window (object);
21559 buf = XBUFFER (w->contents);
21560 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21562 else
21563 CHECK_STRING (object);
21566 if (STRINGP (object))
21568 /* Characters in unibyte strings are always treated by bidi.c as
21569 strong LTR. */
21570 if (!STRING_MULTIBYTE (object)
21571 /* When we are loading loadup.el, the character property
21572 tables needed for bidi iteration are not yet
21573 available. */
21574 || redisplay__inhibit_bidi)
21575 return Qnil;
21577 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21578 if (from_pos >= SCHARS (object))
21579 return Qnil;
21581 /* Set up the bidi iterator. */
21582 itb_data = bidi_shelve_cache ();
21583 itb.paragraph_dir = NEUTRAL_DIR;
21584 itb.string.lstring = object;
21585 itb.string.s = NULL;
21586 itb.string.schars = SCHARS (object);
21587 itb.string.bufpos = 0;
21588 itb.string.from_disp_str = false;
21589 itb.string.unibyte = false;
21590 itb.w = w;
21591 bidi_init_it (0, 0, frame_window_p, &itb);
21593 else
21595 /* Nothing this fancy can happen in unibyte buffers, or in a
21596 buffer that disabled reordering, or if FROM is at EOB. */
21597 if (NILP (BVAR (buf, bidi_display_reordering))
21598 || NILP (BVAR (buf, enable_multibyte_characters))
21599 /* When we are loading loadup.el, the character property
21600 tables needed for bidi iteration are not yet
21601 available. */
21602 || redisplay__inhibit_bidi)
21603 return Qnil;
21605 set_buffer_temp (buf);
21606 validate_region (&from, &to);
21607 from_pos = XINT (from);
21608 to_pos = XINT (to);
21609 if (from_pos >= ZV)
21610 return Qnil;
21612 /* Set up the bidi iterator. */
21613 itb_data = bidi_shelve_cache ();
21614 from_bpos = CHAR_TO_BYTE (from_pos);
21615 if (from_pos == BEGV)
21617 itb.charpos = BEGV;
21618 itb.bytepos = BEGV_BYTE;
21620 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21622 itb.charpos = from_pos;
21623 itb.bytepos = from_bpos;
21625 else
21626 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21627 -1, &itb.bytepos);
21628 itb.paragraph_dir = NEUTRAL_DIR;
21629 itb.string.s = NULL;
21630 itb.string.lstring = Qnil;
21631 itb.string.bufpos = 0;
21632 itb.string.from_disp_str = false;
21633 itb.string.unibyte = false;
21634 itb.w = w;
21635 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21638 ptrdiff_t found;
21639 do {
21640 /* For the purposes of this function, the actual base direction of
21641 the paragraph doesn't matter, so just set it to L2R. */
21642 bidi_paragraph_init (L2R, &itb, false);
21643 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21645 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21647 bidi_unshelve_cache (itb_data, false);
21648 set_buffer_temp (old);
21650 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21653 DEFUN ("move-point-visually", Fmove_point_visually,
21654 Smove_point_visually, 1, 1, 0,
21655 doc: /* Move point in the visual order in the specified DIRECTION.
21656 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21657 left.
21659 Value is the new character position of point. */)
21660 (Lisp_Object direction)
21662 struct window *w = XWINDOW (selected_window);
21663 struct buffer *b = XBUFFER (w->contents);
21664 struct glyph_row *row;
21665 int dir;
21666 Lisp_Object paragraph_dir;
21668 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21669 (!(ROW)->continued_p \
21670 && NILP ((GLYPH)->object) \
21671 && (GLYPH)->type == CHAR_GLYPH \
21672 && (GLYPH)->u.ch == ' ' \
21673 && (GLYPH)->charpos >= 0 \
21674 && !(GLYPH)->avoid_cursor_p)
21676 CHECK_NUMBER (direction);
21677 dir = XINT (direction);
21678 if (dir > 0)
21679 dir = 1;
21680 else
21681 dir = -1;
21683 /* If current matrix is up-to-date, we can use the information
21684 recorded in the glyphs, at least as long as the goal is on the
21685 screen. */
21686 if (w->window_end_valid
21687 && !windows_or_buffers_changed
21688 && b
21689 && !b->clip_changed
21690 && !b->prevent_redisplay_optimizations_p
21691 && !window_outdated (w)
21692 /* We rely below on the cursor coordinates to be up to date, but
21693 we cannot trust them if some command moved point since the
21694 last complete redisplay. */
21695 && w->last_point == BUF_PT (b)
21696 && w->cursor.vpos >= 0
21697 && w->cursor.vpos < w->current_matrix->nrows
21698 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21700 struct glyph *g = row->glyphs[TEXT_AREA];
21701 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21702 struct glyph *gpt = g + w->cursor.hpos;
21704 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21706 if (BUFFERP (g->object) && g->charpos != PT)
21708 SET_PT (g->charpos);
21709 w->cursor.vpos = -1;
21710 return make_number (PT);
21712 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21714 ptrdiff_t new_pos;
21716 if (BUFFERP (gpt->object))
21718 new_pos = PT;
21719 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21720 new_pos += (row->reversed_p ? -dir : dir);
21721 else
21722 new_pos -= (row->reversed_p ? -dir : dir);
21724 else if (BUFFERP (g->object))
21725 new_pos = g->charpos;
21726 else
21727 break;
21728 SET_PT (new_pos);
21729 w->cursor.vpos = -1;
21730 return make_number (PT);
21732 else if (ROW_GLYPH_NEWLINE_P (row, g))
21734 /* Glyphs inserted at the end of a non-empty line for
21735 positioning the cursor have zero charpos, so we must
21736 deduce the value of point by other means. */
21737 if (g->charpos > 0)
21738 SET_PT (g->charpos);
21739 else if (row->ends_at_zv_p && PT != ZV)
21740 SET_PT (ZV);
21741 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21742 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21743 else
21744 break;
21745 w->cursor.vpos = -1;
21746 return make_number (PT);
21749 if (g == e || NILP (g->object))
21751 if (row->truncated_on_left_p || row->truncated_on_right_p)
21752 goto simulate_display;
21753 if (!row->reversed_p)
21754 row += dir;
21755 else
21756 row -= dir;
21757 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21758 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21759 goto simulate_display;
21761 if (dir > 0)
21763 if (row->reversed_p && !row->continued_p)
21765 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21766 w->cursor.vpos = -1;
21767 return make_number (PT);
21769 g = row->glyphs[TEXT_AREA];
21770 e = g + row->used[TEXT_AREA];
21771 for ( ; g < e; g++)
21773 if (BUFFERP (g->object)
21774 /* Empty lines have only one glyph, which stands
21775 for the newline, and whose charpos is the
21776 buffer position of the newline. */
21777 || ROW_GLYPH_NEWLINE_P (row, g)
21778 /* When the buffer ends in a newline, the line at
21779 EOB also has one glyph, but its charpos is -1. */
21780 || (row->ends_at_zv_p
21781 && !row->reversed_p
21782 && NILP (g->object)
21783 && g->type == CHAR_GLYPH
21784 && g->u.ch == ' '))
21786 if (g->charpos > 0)
21787 SET_PT (g->charpos);
21788 else if (!row->reversed_p
21789 && row->ends_at_zv_p
21790 && PT != ZV)
21791 SET_PT (ZV);
21792 else
21793 continue;
21794 w->cursor.vpos = -1;
21795 return make_number (PT);
21799 else
21801 if (!row->reversed_p && !row->continued_p)
21803 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21804 w->cursor.vpos = -1;
21805 return make_number (PT);
21807 e = row->glyphs[TEXT_AREA];
21808 g = e + row->used[TEXT_AREA] - 1;
21809 for ( ; g >= e; g--)
21811 if (BUFFERP (g->object)
21812 || (ROW_GLYPH_NEWLINE_P (row, g)
21813 && g->charpos > 0)
21814 /* Empty R2L lines on GUI frames have the buffer
21815 position of the newline stored in the stretch
21816 glyph. */
21817 || g->type == STRETCH_GLYPH
21818 || (row->ends_at_zv_p
21819 && row->reversed_p
21820 && NILP (g->object)
21821 && g->type == CHAR_GLYPH
21822 && g->u.ch == ' '))
21824 if (g->charpos > 0)
21825 SET_PT (g->charpos);
21826 else if (row->reversed_p
21827 && row->ends_at_zv_p
21828 && PT != ZV)
21829 SET_PT (ZV);
21830 else
21831 continue;
21832 w->cursor.vpos = -1;
21833 return make_number (PT);
21840 simulate_display:
21842 /* If we wind up here, we failed to move by using the glyphs, so we
21843 need to simulate display instead. */
21845 if (b)
21846 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21847 else
21848 paragraph_dir = Qleft_to_right;
21849 if (EQ (paragraph_dir, Qright_to_left))
21850 dir = -dir;
21851 if (PT <= BEGV && dir < 0)
21852 xsignal0 (Qbeginning_of_buffer);
21853 else if (PT >= ZV && dir > 0)
21854 xsignal0 (Qend_of_buffer);
21855 else
21857 struct text_pos pt;
21858 struct it it;
21859 int pt_x, target_x, pixel_width, pt_vpos;
21860 bool at_eol_p;
21861 bool overshoot_expected = false;
21862 bool target_is_eol_p = false;
21864 /* Setup the arena. */
21865 SET_TEXT_POS (pt, PT, PT_BYTE);
21866 start_display (&it, w, pt);
21867 /* When lines are truncated, we could be called with point
21868 outside of the windows edges, in which case move_it_*
21869 functions either prematurely stop at window's edge or jump to
21870 the next screen line, whereas we rely below on our ability to
21871 reach point, in order to start from its X coordinate. So we
21872 need to disregard the window's horizontal extent in that case. */
21873 if (it.line_wrap == TRUNCATE)
21874 it.last_visible_x = INFINITY;
21876 if (it.cmp_it.id < 0
21877 && it.method == GET_FROM_STRING
21878 && it.area == TEXT_AREA
21879 && it.string_from_display_prop_p
21880 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21881 overshoot_expected = true;
21883 /* Find the X coordinate of point. We start from the beginning
21884 of this or previous line to make sure we are before point in
21885 the logical order (since the move_it_* functions can only
21886 move forward). */
21887 reseat:
21888 reseat_at_previous_visible_line_start (&it);
21889 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21890 if (IT_CHARPOS (it) != PT)
21892 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21893 -1, -1, -1, MOVE_TO_POS);
21894 /* If we missed point because the character there is
21895 displayed out of a display vector that has more than one
21896 glyph, retry expecting overshoot. */
21897 if (it.method == GET_FROM_DISPLAY_VECTOR
21898 && it.current.dpvec_index > 0
21899 && !overshoot_expected)
21901 overshoot_expected = true;
21902 goto reseat;
21904 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21905 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21907 pt_x = it.current_x;
21908 pt_vpos = it.vpos;
21909 if (dir > 0 || overshoot_expected)
21911 struct glyph_row *row = it.glyph_row;
21913 /* When point is at beginning of line, we don't have
21914 information about the glyph there loaded into struct
21915 it. Calling get_next_display_element fixes that. */
21916 if (pt_x == 0)
21917 get_next_display_element (&it);
21918 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21919 it.glyph_row = NULL;
21920 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21921 it.glyph_row = row;
21922 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21923 it, lest it will become out of sync with it's buffer
21924 position. */
21925 it.current_x = pt_x;
21927 else
21928 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21929 pixel_width = it.pixel_width;
21930 if (overshoot_expected && at_eol_p)
21931 pixel_width = 0;
21932 else if (pixel_width <= 0)
21933 pixel_width = 1;
21935 /* If there's a display string (or something similar) at point,
21936 we are actually at the glyph to the left of point, so we need
21937 to correct the X coordinate. */
21938 if (overshoot_expected)
21940 if (it.bidi_p)
21941 pt_x += pixel_width * it.bidi_it.scan_dir;
21942 else
21943 pt_x += pixel_width;
21946 /* Compute target X coordinate, either to the left or to the
21947 right of point. On TTY frames, all characters have the same
21948 pixel width of 1, so we can use that. On GUI frames we don't
21949 have an easy way of getting at the pixel width of the
21950 character to the left of point, so we use a different method
21951 of getting to that place. */
21952 if (dir > 0)
21953 target_x = pt_x + pixel_width;
21954 else
21955 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21957 /* Target X coordinate could be one line above or below the line
21958 of point, in which case we need to adjust the target X
21959 coordinate. Also, if moving to the left, we need to begin at
21960 the left edge of the point's screen line. */
21961 if (dir < 0)
21963 if (pt_x > 0)
21965 start_display (&it, w, pt);
21966 if (it.line_wrap == TRUNCATE)
21967 it.last_visible_x = INFINITY;
21968 reseat_at_previous_visible_line_start (&it);
21969 it.current_x = it.current_y = it.hpos = 0;
21970 if (pt_vpos != 0)
21971 move_it_by_lines (&it, pt_vpos);
21973 else
21975 move_it_by_lines (&it, -1);
21976 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21977 target_is_eol_p = true;
21978 /* Under word-wrap, we don't know the x coordinate of
21979 the last character displayed on the previous line,
21980 which immediately precedes the wrap point. To find
21981 out its x coordinate, we try moving to the right
21982 margin of the window, which will stop at the wrap
21983 point, and then reset target_x to point at the
21984 character that precedes the wrap point. This is not
21985 needed on GUI frames, because (see below) there we
21986 move from the left margin one grapheme cluster at a
21987 time, and stop when we hit the wrap point. */
21988 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21990 void *it_data = NULL;
21991 struct it it2;
21993 SAVE_IT (it2, it, it_data);
21994 move_it_in_display_line_to (&it, ZV, target_x,
21995 MOVE_TO_POS | MOVE_TO_X);
21996 /* If we arrived at target_x, that _is_ the last
21997 character on the previous line. */
21998 if (it.current_x != target_x)
21999 target_x = it.current_x - 1;
22000 RESTORE_IT (&it, &it2, it_data);
22004 else
22006 if (at_eol_p
22007 || (target_x >= it.last_visible_x
22008 && it.line_wrap != TRUNCATE))
22010 if (pt_x > 0)
22011 move_it_by_lines (&it, 0);
22012 move_it_by_lines (&it, 1);
22013 target_x = 0;
22017 /* Move to the target X coordinate. */
22018 /* On GUI frames, as we don't know the X coordinate of the
22019 character to the left of point, moving point to the left
22020 requires walking, one grapheme cluster at a time, until we
22021 find ourself at a place immediately to the left of the
22022 character at point. */
22023 if (FRAME_WINDOW_P (it.f) && dir < 0)
22025 struct text_pos new_pos;
22026 enum move_it_result rc = MOVE_X_REACHED;
22028 if (it.current_x == 0)
22029 get_next_display_element (&it);
22030 if (it.what == IT_COMPOSITION)
22032 new_pos.charpos = it.cmp_it.charpos;
22033 new_pos.bytepos = -1;
22035 else
22036 new_pos = it.current.pos;
22038 while (it.current_x + it.pixel_width <= target_x
22039 && (rc == MOVE_X_REACHED
22040 /* Under word-wrap, move_it_in_display_line_to
22041 stops at correct coordinates, but sometimes
22042 returns MOVE_POS_MATCH_OR_ZV. */
22043 || (it.line_wrap == WORD_WRAP
22044 && rc == MOVE_POS_MATCH_OR_ZV)))
22046 int new_x = it.current_x + it.pixel_width;
22048 /* For composed characters, we want the position of the
22049 first character in the grapheme cluster (usually, the
22050 composition's base character), whereas it.current
22051 might give us the position of the _last_ one, e.g. if
22052 the composition is rendered in reverse due to bidi
22053 reordering. */
22054 if (it.what == IT_COMPOSITION)
22056 new_pos.charpos = it.cmp_it.charpos;
22057 new_pos.bytepos = -1;
22059 else
22060 new_pos = it.current.pos;
22061 if (new_x == it.current_x)
22062 new_x++;
22063 rc = move_it_in_display_line_to (&it, ZV, new_x,
22064 MOVE_TO_POS | MOVE_TO_X);
22065 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22066 break;
22068 /* The previous position we saw in the loop is the one we
22069 want. */
22070 if (new_pos.bytepos == -1)
22071 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22072 it.current.pos = new_pos;
22074 else if (it.current_x != target_x)
22075 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22077 /* If we ended up in a display string that covers point, move to
22078 buffer position to the right in the visual order. */
22079 if (dir > 0)
22081 while (IT_CHARPOS (it) == PT)
22083 set_iterator_to_next (&it, false);
22084 if (!get_next_display_element (&it))
22085 break;
22089 /* Move point to that position. */
22090 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22093 return make_number (PT);
22095 #undef ROW_GLYPH_NEWLINE_P
22098 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22099 Sbidi_resolved_levels, 0, 1, 0,
22100 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22102 The resolved levels are produced by the Emacs bidi reordering engine
22103 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22104 read the Unicode Standard Annex 9 (UAX#9) for background information
22105 about these levels.
22107 VPOS is the zero-based number of the current window's screen line
22108 for which to produce the resolved levels. If VPOS is nil or omitted,
22109 it defaults to the screen line of point. If the window displays a
22110 header line, VPOS of zero will report on the header line, and first
22111 line of text in the window will have VPOS of 1.
22113 Value is an array of resolved levels, indexed by glyph number.
22114 Glyphs are numbered from zero starting from the beginning of the
22115 screen line, i.e. the left edge of the window for left-to-right lines
22116 and from the right edge for right-to-left lines. The resolved levels
22117 are produced only for the window's text area; text in display margins
22118 is not included.
22120 If the selected window's display is not up-to-date, or if the specified
22121 screen line does not display text, this function returns nil. It is
22122 highly recommended to bind this function to some simple key, like F8,
22123 in order to avoid these problems.
22125 This function exists mainly for testing the correctness of the
22126 Emacs UBA implementation, in particular with the test suite. */)
22127 (Lisp_Object vpos)
22129 struct window *w = XWINDOW (selected_window);
22130 struct buffer *b = XBUFFER (w->contents);
22131 int nrow;
22132 struct glyph_row *row;
22134 if (NILP (vpos))
22136 int d1, d2, d3, d4, d5;
22138 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22140 else
22142 CHECK_NUMBER_COERCE_MARKER (vpos);
22143 nrow = XINT (vpos);
22146 /* We require up-to-date glyph matrix for this window. */
22147 if (w->window_end_valid
22148 && !windows_or_buffers_changed
22149 && b
22150 && !b->clip_changed
22151 && !b->prevent_redisplay_optimizations_p
22152 && !window_outdated (w)
22153 && nrow >= 0
22154 && nrow < w->current_matrix->nrows
22155 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22156 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22158 struct glyph *g, *e, *g1;
22159 int nglyphs, i;
22160 Lisp_Object levels;
22162 if (!row->reversed_p) /* Left-to-right glyph row. */
22164 g = g1 = row->glyphs[TEXT_AREA];
22165 e = g + row->used[TEXT_AREA];
22167 /* Skip over glyphs at the start of the row that was
22168 generated by redisplay for its own needs. */
22169 while (g < e
22170 && NILP (g->object)
22171 && g->charpos < 0)
22172 g++;
22173 g1 = g;
22175 /* Count the "interesting" glyphs in this row. */
22176 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22177 nglyphs++;
22179 /* Create and fill the array. */
22180 levels = make_uninit_vector (nglyphs);
22181 for (i = 0; g1 < g; i++, g1++)
22182 ASET (levels, i, make_number (g1->resolved_level));
22184 else /* Right-to-left glyph row. */
22186 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22187 e = row->glyphs[TEXT_AREA] - 1;
22188 while (g > e
22189 && NILP (g->object)
22190 && g->charpos < 0)
22191 g--;
22192 g1 = g;
22193 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22194 nglyphs++;
22195 levels = make_uninit_vector (nglyphs);
22196 for (i = 0; g1 > g; i++, g1--)
22197 ASET (levels, i, make_number (g1->resolved_level));
22199 return levels;
22201 else
22202 return Qnil;
22207 /***********************************************************************
22208 Menu Bar
22209 ***********************************************************************/
22211 /* Redisplay the menu bar in the frame for window W.
22213 The menu bar of X frames that don't have X toolkit support is
22214 displayed in a special window W->frame->menu_bar_window.
22216 The menu bar of terminal frames is treated specially as far as
22217 glyph matrices are concerned. Menu bar lines are not part of
22218 windows, so the update is done directly on the frame matrix rows
22219 for the menu bar. */
22221 static void
22222 display_menu_bar (struct window *w)
22224 struct frame *f = XFRAME (WINDOW_FRAME (w));
22225 struct it it;
22226 Lisp_Object items;
22227 int i;
22229 /* Don't do all this for graphical frames. */
22230 #ifdef HAVE_NTGUI
22231 if (FRAME_W32_P (f))
22232 return;
22233 #endif
22234 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22235 if (FRAME_X_P (f))
22236 return;
22237 #endif
22239 #ifdef HAVE_NS
22240 if (FRAME_NS_P (f))
22241 return;
22242 #endif /* HAVE_NS */
22244 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22245 eassert (!FRAME_WINDOW_P (f));
22246 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22247 it.first_visible_x = 0;
22248 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22249 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22250 if (FRAME_WINDOW_P (f))
22252 /* Menu bar lines are displayed in the desired matrix of the
22253 dummy window menu_bar_window. */
22254 struct window *menu_w;
22255 menu_w = XWINDOW (f->menu_bar_window);
22256 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22257 MENU_FACE_ID);
22258 it.first_visible_x = 0;
22259 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22261 else
22262 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22264 /* This is a TTY frame, i.e. character hpos/vpos are used as
22265 pixel x/y. */
22266 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22267 MENU_FACE_ID);
22268 it.first_visible_x = 0;
22269 it.last_visible_x = FRAME_COLS (f);
22272 /* FIXME: This should be controlled by a user option. See the
22273 comments in redisplay_tool_bar and display_mode_line about
22274 this. */
22275 it.paragraph_embedding = L2R;
22277 /* Clear all rows of the menu bar. */
22278 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22280 struct glyph_row *row = it.glyph_row + i;
22281 clear_glyph_row (row);
22282 row->enabled_p = true;
22283 row->full_width_p = true;
22284 row->reversed_p = false;
22287 /* Display all items of the menu bar. */
22288 items = FRAME_MENU_BAR_ITEMS (it.f);
22289 for (i = 0; i < ASIZE (items); i += 4)
22291 Lisp_Object string;
22293 /* Stop at nil string. */
22294 string = AREF (items, i + 1);
22295 if (NILP (string))
22296 break;
22298 /* Remember where item was displayed. */
22299 ASET (items, i + 3, make_number (it.hpos));
22301 /* Display the item, pad with one space. */
22302 if (it.current_x < it.last_visible_x)
22303 display_string (NULL, string, Qnil, 0, 0, &it,
22304 SCHARS (string) + 1, 0, 0, -1);
22307 /* Fill out the line with spaces. */
22308 if (it.current_x < it.last_visible_x)
22309 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22311 /* Compute the total height of the lines. */
22312 compute_line_metrics (&it);
22315 /* Deep copy of a glyph row, including the glyphs. */
22316 static void
22317 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22319 struct glyph *pointers[1 + LAST_AREA];
22320 int to_used = to->used[TEXT_AREA];
22322 /* Save glyph pointers of TO. */
22323 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22325 /* Do a structure assignment. */
22326 *to = *from;
22328 /* Restore original glyph pointers of TO. */
22329 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22331 /* Copy the glyphs. */
22332 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22333 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22335 /* If we filled only part of the TO row, fill the rest with
22336 space_glyph (which will display as empty space). */
22337 if (to_used > from->used[TEXT_AREA])
22338 fill_up_frame_row_with_spaces (to, to_used);
22341 /* Display one menu item on a TTY, by overwriting the glyphs in the
22342 frame F's desired glyph matrix with glyphs produced from the menu
22343 item text. Called from term.c to display TTY drop-down menus one
22344 item at a time.
22346 ITEM_TEXT is the menu item text as a C string.
22348 FACE_ID is the face ID to be used for this menu item. FACE_ID
22349 could specify one of 3 faces: a face for an enabled item, a face
22350 for a disabled item, or a face for a selected item.
22352 X and Y are coordinates of the first glyph in the frame's desired
22353 matrix to be overwritten by the menu item. Since this is a TTY, Y
22354 is the zero-based number of the glyph row and X is the zero-based
22355 glyph number in the row, starting from left, where to start
22356 displaying the item.
22358 SUBMENU means this menu item drops down a submenu, which
22359 should be indicated by displaying a proper visual cue after the
22360 item text. */
22362 void
22363 display_tty_menu_item (const char *item_text, int width, int face_id,
22364 int x, int y, bool submenu)
22366 struct it it;
22367 struct frame *f = SELECTED_FRAME ();
22368 struct window *w = XWINDOW (f->selected_window);
22369 struct glyph_row *row;
22370 size_t item_len = strlen (item_text);
22372 eassert (FRAME_TERMCAP_P (f));
22374 /* Don't write beyond the matrix's last row. This can happen for
22375 TTY screens that are not high enough to show the entire menu.
22376 (This is actually a bit of defensive programming, as
22377 tty_menu_display already limits the number of menu items to one
22378 less than the number of screen lines.) */
22379 if (y >= f->desired_matrix->nrows)
22380 return;
22382 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22383 it.first_visible_x = 0;
22384 it.last_visible_x = FRAME_COLS (f) - 1;
22385 row = it.glyph_row;
22386 /* Start with the row contents from the current matrix. */
22387 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22388 bool saved_width = row->full_width_p;
22389 row->full_width_p = true;
22390 bool saved_reversed = row->reversed_p;
22391 row->reversed_p = false;
22392 row->enabled_p = true;
22394 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22395 desired face. */
22396 eassert (x < f->desired_matrix->matrix_w);
22397 it.current_x = it.hpos = x;
22398 it.current_y = it.vpos = y;
22399 int saved_used = row->used[TEXT_AREA];
22400 bool saved_truncated = row->truncated_on_right_p;
22401 row->used[TEXT_AREA] = x;
22402 it.face_id = face_id;
22403 it.line_wrap = TRUNCATE;
22405 /* FIXME: This should be controlled by a user option. See the
22406 comments in redisplay_tool_bar and display_mode_line about this.
22407 Also, if paragraph_embedding could ever be R2L, changes will be
22408 needed to avoid shifting to the right the row characters in
22409 term.c:append_glyph. */
22410 it.paragraph_embedding = L2R;
22412 /* Pad with a space on the left. */
22413 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22414 width--;
22415 /* Display the menu item, pad with spaces to WIDTH. */
22416 if (submenu)
22418 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22419 item_len, 0, FRAME_COLS (f) - 1, -1);
22420 width -= item_len;
22421 /* Indicate with " >" that there's a submenu. */
22422 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22423 FRAME_COLS (f) - 1, -1);
22425 else
22426 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22427 width, 0, FRAME_COLS (f) - 1, -1);
22429 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22430 row->truncated_on_right_p = saved_truncated;
22431 row->hash = row_hash (row);
22432 row->full_width_p = saved_width;
22433 row->reversed_p = saved_reversed;
22436 /***********************************************************************
22437 Mode Line
22438 ***********************************************************************/
22440 /* Redisplay mode lines in the window tree whose root is WINDOW.
22441 If FORCE, redisplay mode lines unconditionally.
22442 Otherwise, redisplay only mode lines that are garbaged. Value is
22443 the number of windows whose mode lines were redisplayed. */
22445 static int
22446 redisplay_mode_lines (Lisp_Object window, bool force)
22448 int nwindows = 0;
22450 while (!NILP (window))
22452 struct window *w = XWINDOW (window);
22454 if (WINDOWP (w->contents))
22455 nwindows += redisplay_mode_lines (w->contents, force);
22456 else if (force
22457 || FRAME_GARBAGED_P (XFRAME (w->frame))
22458 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22460 struct text_pos lpoint;
22461 struct buffer *old = current_buffer;
22463 /* Set the window's buffer for the mode line display. */
22464 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22465 set_buffer_internal_1 (XBUFFER (w->contents));
22467 /* Point refers normally to the selected window. For any
22468 other window, set up appropriate value. */
22469 if (!EQ (window, selected_window))
22471 struct text_pos pt;
22473 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22474 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22477 /* Display mode lines. */
22478 clear_glyph_matrix (w->desired_matrix);
22479 if (display_mode_lines (w))
22480 ++nwindows;
22482 /* Restore old settings. */
22483 set_buffer_internal_1 (old);
22484 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22487 window = w->next;
22490 return nwindows;
22494 /* Display the mode and/or header line of window W. Value is the
22495 sum number of mode lines and header lines displayed. */
22497 static int
22498 display_mode_lines (struct window *w)
22500 Lisp_Object old_selected_window = selected_window;
22501 Lisp_Object old_selected_frame = selected_frame;
22502 Lisp_Object new_frame = w->frame;
22503 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22504 int n = 0;
22506 selected_frame = new_frame;
22507 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22508 or window's point, then we'd need select_window_1 here as well. */
22509 XSETWINDOW (selected_window, w);
22510 XFRAME (new_frame)->selected_window = selected_window;
22512 /* These will be set while the mode line specs are processed. */
22513 line_number_displayed = false;
22514 w->column_number_displayed = -1;
22516 if (WINDOW_WANTS_MODELINE_P (w))
22518 struct window *sel_w = XWINDOW (old_selected_window);
22520 /* Select mode line face based on the real selected window. */
22521 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22522 BVAR (current_buffer, mode_line_format));
22523 ++n;
22526 if (WINDOW_WANTS_HEADER_LINE_P (w))
22528 display_mode_line (w, HEADER_LINE_FACE_ID,
22529 BVAR (current_buffer, header_line_format));
22530 ++n;
22533 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22534 selected_frame = old_selected_frame;
22535 selected_window = old_selected_window;
22536 if (n > 0)
22537 w->must_be_updated_p = true;
22538 return n;
22542 /* Display mode or header line of window W. FACE_ID specifies which
22543 line to display; it is either MODE_LINE_FACE_ID or
22544 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22545 display. Value is the pixel height of the mode/header line
22546 displayed. */
22548 static int
22549 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22551 struct it it;
22552 struct face *face;
22553 ptrdiff_t count = SPECPDL_INDEX ();
22555 init_iterator (&it, w, -1, -1, NULL, face_id);
22556 /* Don't extend on a previously drawn mode-line.
22557 This may happen if called from pos_visible_p. */
22558 it.glyph_row->enabled_p = false;
22559 prepare_desired_row (w, it.glyph_row, true);
22561 it.glyph_row->mode_line_p = true;
22563 /* FIXME: This should be controlled by a user option. But
22564 supporting such an option is not trivial, since the mode line is
22565 made up of many separate strings. */
22566 it.paragraph_embedding = L2R;
22568 record_unwind_protect (unwind_format_mode_line,
22569 format_mode_line_unwind_data (NULL, NULL,
22570 Qnil, false));
22572 mode_line_target = MODE_LINE_DISPLAY;
22574 /* Temporarily make frame's keyboard the current kboard so that
22575 kboard-local variables in the mode_line_format will get the right
22576 values. */
22577 push_kboard (FRAME_KBOARD (it.f));
22578 record_unwind_save_match_data ();
22579 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22580 pop_kboard ();
22582 unbind_to (count, Qnil);
22584 /* Fill up with spaces. */
22585 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22587 compute_line_metrics (&it);
22588 it.glyph_row->full_width_p = true;
22589 it.glyph_row->continued_p = false;
22590 it.glyph_row->truncated_on_left_p = false;
22591 it.glyph_row->truncated_on_right_p = false;
22593 /* Make a 3D mode-line have a shadow at its right end. */
22594 face = FACE_FROM_ID (it.f, face_id);
22595 extend_face_to_end_of_line (&it);
22596 if (face->box != FACE_NO_BOX)
22598 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22599 + it.glyph_row->used[TEXT_AREA] - 1);
22600 last->right_box_line_p = true;
22603 return it.glyph_row->height;
22606 /* Move element ELT in LIST to the front of LIST.
22607 Return the updated list. */
22609 static Lisp_Object
22610 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22612 register Lisp_Object tail, prev;
22613 register Lisp_Object tem;
22615 tail = list;
22616 prev = Qnil;
22617 while (CONSP (tail))
22619 tem = XCAR (tail);
22621 if (EQ (elt, tem))
22623 /* Splice out the link TAIL. */
22624 if (NILP (prev))
22625 list = XCDR (tail);
22626 else
22627 Fsetcdr (prev, XCDR (tail));
22629 /* Now make it the first. */
22630 Fsetcdr (tail, list);
22631 return tail;
22633 else
22634 prev = tail;
22635 tail = XCDR (tail);
22636 QUIT;
22639 /* Not found--return unchanged LIST. */
22640 return list;
22643 /* Contribute ELT to the mode line for window IT->w. How it
22644 translates into text depends on its data type.
22646 IT describes the display environment in which we display, as usual.
22648 DEPTH is the depth in recursion. It is used to prevent
22649 infinite recursion here.
22651 FIELD_WIDTH is the number of characters the display of ELT should
22652 occupy in the mode line, and PRECISION is the maximum number of
22653 characters to display from ELT's representation. See
22654 display_string for details.
22656 Returns the hpos of the end of the text generated by ELT.
22658 PROPS is a property list to add to any string we encounter.
22660 If RISKY, remove (disregard) any properties in any string
22661 we encounter, and ignore :eval and :propertize.
22663 The global variable `mode_line_target' determines whether the
22664 output is passed to `store_mode_line_noprop',
22665 `store_mode_line_string', or `display_string'. */
22667 static int
22668 display_mode_element (struct it *it, int depth, int field_width, int precision,
22669 Lisp_Object elt, Lisp_Object props, bool risky)
22671 int n = 0, field, prec;
22672 bool literal = false;
22674 tail_recurse:
22675 if (depth > 100)
22676 elt = build_string ("*too-deep*");
22678 depth++;
22680 switch (XTYPE (elt))
22682 case Lisp_String:
22684 /* A string: output it and check for %-constructs within it. */
22685 unsigned char c;
22686 ptrdiff_t offset = 0;
22688 if (SCHARS (elt) > 0
22689 && (!NILP (props) || risky))
22691 Lisp_Object oprops, aelt;
22692 oprops = Ftext_properties_at (make_number (0), elt);
22694 /* If the starting string's properties are not what
22695 we want, translate the string. Also, if the string
22696 is risky, do that anyway. */
22698 if (NILP (Fequal (props, oprops)) || risky)
22700 /* If the starting string has properties,
22701 merge the specified ones onto the existing ones. */
22702 if (! NILP (oprops) && !risky)
22704 Lisp_Object tem;
22706 oprops = Fcopy_sequence (oprops);
22707 tem = props;
22708 while (CONSP (tem))
22710 oprops = Fplist_put (oprops, XCAR (tem),
22711 XCAR (XCDR (tem)));
22712 tem = XCDR (XCDR (tem));
22714 props = oprops;
22717 aelt = Fassoc (elt, mode_line_proptrans_alist);
22718 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22720 /* AELT is what we want. Move it to the front
22721 without consing. */
22722 elt = XCAR (aelt);
22723 mode_line_proptrans_alist
22724 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22726 else
22728 Lisp_Object tem;
22730 /* If AELT has the wrong props, it is useless.
22731 so get rid of it. */
22732 if (! NILP (aelt))
22733 mode_line_proptrans_alist
22734 = Fdelq (aelt, mode_line_proptrans_alist);
22736 elt = Fcopy_sequence (elt);
22737 Fset_text_properties (make_number (0), Flength (elt),
22738 props, elt);
22739 /* Add this item to mode_line_proptrans_alist. */
22740 mode_line_proptrans_alist
22741 = Fcons (Fcons (elt, props),
22742 mode_line_proptrans_alist);
22743 /* Truncate mode_line_proptrans_alist
22744 to at most 50 elements. */
22745 tem = Fnthcdr (make_number (50),
22746 mode_line_proptrans_alist);
22747 if (! NILP (tem))
22748 XSETCDR (tem, Qnil);
22753 offset = 0;
22755 if (literal)
22757 prec = precision - n;
22758 switch (mode_line_target)
22760 case MODE_LINE_NOPROP:
22761 case MODE_LINE_TITLE:
22762 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22763 break;
22764 case MODE_LINE_STRING:
22765 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22766 break;
22767 case MODE_LINE_DISPLAY:
22768 n += display_string (NULL, elt, Qnil, 0, 0, it,
22769 0, prec, 0, STRING_MULTIBYTE (elt));
22770 break;
22773 break;
22776 /* Handle the non-literal case. */
22778 while ((precision <= 0 || n < precision)
22779 && SREF (elt, offset) != 0
22780 && (mode_line_target != MODE_LINE_DISPLAY
22781 || it->current_x < it->last_visible_x))
22783 ptrdiff_t last_offset = offset;
22785 /* Advance to end of string or next format specifier. */
22786 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22789 if (offset - 1 != last_offset)
22791 ptrdiff_t nchars, nbytes;
22793 /* Output to end of string or up to '%'. Field width
22794 is length of string. Don't output more than
22795 PRECISION allows us. */
22796 offset--;
22798 prec = c_string_width (SDATA (elt) + last_offset,
22799 offset - last_offset, precision - n,
22800 &nchars, &nbytes);
22802 switch (mode_line_target)
22804 case MODE_LINE_NOPROP:
22805 case MODE_LINE_TITLE:
22806 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22807 break;
22808 case MODE_LINE_STRING:
22810 ptrdiff_t bytepos = last_offset;
22811 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22812 ptrdiff_t endpos = (precision <= 0
22813 ? string_byte_to_char (elt, offset)
22814 : charpos + nchars);
22815 Lisp_Object mode_string
22816 = Fsubstring (elt, make_number (charpos),
22817 make_number (endpos));
22818 n += store_mode_line_string (NULL, mode_string, false,
22819 0, 0, Qnil);
22821 break;
22822 case MODE_LINE_DISPLAY:
22824 ptrdiff_t bytepos = last_offset;
22825 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22827 if (precision <= 0)
22828 nchars = string_byte_to_char (elt, offset) - charpos;
22829 n += display_string (NULL, elt, Qnil, 0, charpos,
22830 it, 0, nchars, 0,
22831 STRING_MULTIBYTE (elt));
22833 break;
22836 else /* c == '%' */
22838 ptrdiff_t percent_position = offset;
22840 /* Get the specified minimum width. Zero means
22841 don't pad. */
22842 field = 0;
22843 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22844 field = field * 10 + c - '0';
22846 /* Don't pad beyond the total padding allowed. */
22847 if (field_width - n > 0 && field > field_width - n)
22848 field = field_width - n;
22850 /* Note that either PRECISION <= 0 or N < PRECISION. */
22851 prec = precision - n;
22853 if (c == 'M')
22854 n += display_mode_element (it, depth, field, prec,
22855 Vglobal_mode_string, props,
22856 risky);
22857 else if (c != 0)
22859 bool multibyte;
22860 ptrdiff_t bytepos, charpos;
22861 const char *spec;
22862 Lisp_Object string;
22864 bytepos = percent_position;
22865 charpos = (STRING_MULTIBYTE (elt)
22866 ? string_byte_to_char (elt, bytepos)
22867 : bytepos);
22868 spec = decode_mode_spec (it->w, c, field, &string);
22869 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22871 switch (mode_line_target)
22873 case MODE_LINE_NOPROP:
22874 case MODE_LINE_TITLE:
22875 n += store_mode_line_noprop (spec, field, prec);
22876 break;
22877 case MODE_LINE_STRING:
22879 Lisp_Object tem = build_string (spec);
22880 props = Ftext_properties_at (make_number (charpos), elt);
22881 /* Should only keep face property in props */
22882 n += store_mode_line_string (NULL, tem, false,
22883 field, prec, props);
22885 break;
22886 case MODE_LINE_DISPLAY:
22888 int nglyphs_before, nwritten;
22890 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22891 nwritten = display_string (spec, string, elt,
22892 charpos, 0, it,
22893 field, prec, 0,
22894 multibyte);
22896 /* Assign to the glyphs written above the
22897 string where the `%x' came from, position
22898 of the `%'. */
22899 if (nwritten > 0)
22901 struct glyph *glyph
22902 = (it->glyph_row->glyphs[TEXT_AREA]
22903 + nglyphs_before);
22904 int i;
22906 for (i = 0; i < nwritten; ++i)
22908 glyph[i].object = elt;
22909 glyph[i].charpos = charpos;
22912 n += nwritten;
22915 break;
22918 else /* c == 0 */
22919 break;
22923 break;
22925 case Lisp_Symbol:
22926 /* A symbol: process the value of the symbol recursively
22927 as if it appeared here directly. Avoid error if symbol void.
22928 Special case: if value of symbol is a string, output the string
22929 literally. */
22931 register Lisp_Object tem;
22933 /* If the variable is not marked as risky to set
22934 then its contents are risky to use. */
22935 if (NILP (Fget (elt, Qrisky_local_variable)))
22936 risky = true;
22938 tem = Fboundp (elt);
22939 if (!NILP (tem))
22941 tem = Fsymbol_value (elt);
22942 /* If value is a string, output that string literally:
22943 don't check for % within it. */
22944 if (STRINGP (tem))
22945 literal = true;
22947 if (!EQ (tem, elt))
22949 /* Give up right away for nil or t. */
22950 elt = tem;
22951 goto tail_recurse;
22955 break;
22957 case Lisp_Cons:
22959 register Lisp_Object car, tem;
22961 /* A cons cell: five distinct cases.
22962 If first element is :eval or :propertize, do something special.
22963 If first element is a string or a cons, process all the elements
22964 and effectively concatenate them.
22965 If first element is a negative number, truncate displaying cdr to
22966 at most that many characters. If positive, pad (with spaces)
22967 to at least that many characters.
22968 If first element is a symbol, process the cadr or caddr recursively
22969 according to whether the symbol's value is non-nil or nil. */
22970 car = XCAR (elt);
22971 if (EQ (car, QCeval))
22973 /* An element of the form (:eval FORM) means evaluate FORM
22974 and use the result as mode line elements. */
22976 if (risky)
22977 break;
22979 if (CONSP (XCDR (elt)))
22981 Lisp_Object spec;
22982 spec = safe__eval (true, XCAR (XCDR (elt)));
22983 n += display_mode_element (it, depth, field_width - n,
22984 precision - n, spec, props,
22985 risky);
22988 else if (EQ (car, QCpropertize))
22990 /* An element of the form (:propertize ELT PROPS...)
22991 means display ELT but applying properties PROPS. */
22993 if (risky)
22994 break;
22996 if (CONSP (XCDR (elt)))
22997 n += display_mode_element (it, depth, field_width - n,
22998 precision - n, XCAR (XCDR (elt)),
22999 XCDR (XCDR (elt)), risky);
23001 else if (SYMBOLP (car))
23003 tem = Fboundp (car);
23004 elt = XCDR (elt);
23005 if (!CONSP (elt))
23006 goto invalid;
23007 /* elt is now the cdr, and we know it is a cons cell.
23008 Use its car if CAR has a non-nil value. */
23009 if (!NILP (tem))
23011 tem = Fsymbol_value (car);
23012 if (!NILP (tem))
23014 elt = XCAR (elt);
23015 goto tail_recurse;
23018 /* Symbol's value is nil (or symbol is unbound)
23019 Get the cddr of the original list
23020 and if possible find the caddr and use that. */
23021 elt = XCDR (elt);
23022 if (NILP (elt))
23023 break;
23024 else if (!CONSP (elt))
23025 goto invalid;
23026 elt = XCAR (elt);
23027 goto tail_recurse;
23029 else if (INTEGERP (car))
23031 register int lim = XINT (car);
23032 elt = XCDR (elt);
23033 if (lim < 0)
23035 /* Negative int means reduce maximum width. */
23036 if (precision <= 0)
23037 precision = -lim;
23038 else
23039 precision = min (precision, -lim);
23041 else if (lim > 0)
23043 /* Padding specified. Don't let it be more than
23044 current maximum. */
23045 if (precision > 0)
23046 lim = min (precision, lim);
23048 /* If that's more padding than already wanted, queue it.
23049 But don't reduce padding already specified even if
23050 that is beyond the current truncation point. */
23051 field_width = max (lim, field_width);
23053 goto tail_recurse;
23055 else if (STRINGP (car) || CONSP (car))
23057 Lisp_Object halftail = elt;
23058 int len = 0;
23060 while (CONSP (elt)
23061 && (precision <= 0 || n < precision))
23063 n += display_mode_element (it, depth,
23064 /* Do padding only after the last
23065 element in the list. */
23066 (! CONSP (XCDR (elt))
23067 ? field_width - n
23068 : 0),
23069 precision - n, XCAR (elt),
23070 props, risky);
23071 elt = XCDR (elt);
23072 len++;
23073 if ((len & 1) == 0)
23074 halftail = XCDR (halftail);
23075 /* Check for cycle. */
23076 if (EQ (halftail, elt))
23077 break;
23081 break;
23083 default:
23084 invalid:
23085 elt = build_string ("*invalid*");
23086 goto tail_recurse;
23089 /* Pad to FIELD_WIDTH. */
23090 if (field_width > 0 && n < field_width)
23092 switch (mode_line_target)
23094 case MODE_LINE_NOPROP:
23095 case MODE_LINE_TITLE:
23096 n += store_mode_line_noprop ("", field_width - n, 0);
23097 break;
23098 case MODE_LINE_STRING:
23099 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23100 Qnil);
23101 break;
23102 case MODE_LINE_DISPLAY:
23103 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23104 0, 0, 0);
23105 break;
23109 return n;
23112 /* Store a mode-line string element in mode_line_string_list.
23114 If STRING is non-null, display that C string. Otherwise, the Lisp
23115 string LISP_STRING is displayed.
23117 FIELD_WIDTH is the minimum number of output glyphs to produce.
23118 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23119 with spaces. FIELD_WIDTH <= 0 means don't pad.
23121 PRECISION is the maximum number of characters to output from
23122 STRING. PRECISION <= 0 means don't truncate the string.
23124 If COPY_STRING, make a copy of LISP_STRING before adding
23125 properties to the string.
23127 PROPS are the properties to add to the string.
23128 The mode_line_string_face face property is always added to the string.
23131 static int
23132 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23133 bool copy_string,
23134 int field_width, int precision, Lisp_Object props)
23136 ptrdiff_t len;
23137 int n = 0;
23139 if (string != NULL)
23141 len = strlen (string);
23142 if (precision > 0 && len > precision)
23143 len = precision;
23144 lisp_string = make_string (string, len);
23145 if (NILP (props))
23146 props = mode_line_string_face_prop;
23147 else if (!NILP (mode_line_string_face))
23149 Lisp_Object face = Fplist_get (props, Qface);
23150 props = Fcopy_sequence (props);
23151 if (NILP (face))
23152 face = mode_line_string_face;
23153 else
23154 face = list2 (face, mode_line_string_face);
23155 props = Fplist_put (props, Qface, face);
23157 Fadd_text_properties (make_number (0), make_number (len),
23158 props, lisp_string);
23160 else
23162 len = XFASTINT (Flength (lisp_string));
23163 if (precision > 0 && len > precision)
23165 len = precision;
23166 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23167 precision = -1;
23169 if (!NILP (mode_line_string_face))
23171 Lisp_Object face;
23172 if (NILP (props))
23173 props = Ftext_properties_at (make_number (0), lisp_string);
23174 face = Fplist_get (props, Qface);
23175 if (NILP (face))
23176 face = mode_line_string_face;
23177 else
23178 face = list2 (face, mode_line_string_face);
23179 props = list2 (Qface, face);
23180 if (copy_string)
23181 lisp_string = Fcopy_sequence (lisp_string);
23183 if (!NILP (props))
23184 Fadd_text_properties (make_number (0), make_number (len),
23185 props, lisp_string);
23188 if (len > 0)
23190 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23191 n += len;
23194 if (field_width > len)
23196 field_width -= len;
23197 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23198 if (!NILP (props))
23199 Fadd_text_properties (make_number (0), make_number (field_width),
23200 props, lisp_string);
23201 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23202 n += field_width;
23205 return n;
23209 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23210 1, 4, 0,
23211 doc: /* Format a string out of a mode line format specification.
23212 First arg FORMAT specifies the mode line format (see `mode-line-format'
23213 for details) to use.
23215 By default, the format is evaluated for the currently selected window.
23217 Optional second arg FACE specifies the face property to put on all
23218 characters for which no face is specified. The value nil means the
23219 default face. The value t means whatever face the window's mode line
23220 currently uses (either `mode-line' or `mode-line-inactive',
23221 depending on whether the window is the selected window or not).
23222 An integer value means the value string has no text
23223 properties.
23225 Optional third and fourth args WINDOW and BUFFER specify the window
23226 and buffer to use as the context for the formatting (defaults
23227 are the selected window and the WINDOW's buffer). */)
23228 (Lisp_Object format, Lisp_Object face,
23229 Lisp_Object window, Lisp_Object buffer)
23231 struct it it;
23232 int len;
23233 struct window *w;
23234 struct buffer *old_buffer = NULL;
23235 int face_id;
23236 bool no_props = INTEGERP (face);
23237 ptrdiff_t count = SPECPDL_INDEX ();
23238 Lisp_Object str;
23239 int string_start = 0;
23241 w = decode_any_window (window);
23242 XSETWINDOW (window, w);
23244 if (NILP (buffer))
23245 buffer = w->contents;
23246 CHECK_BUFFER (buffer);
23248 /* Make formatting the modeline a non-op when noninteractive, otherwise
23249 there will be problems later caused by a partially initialized frame. */
23250 if (NILP (format) || noninteractive)
23251 return empty_unibyte_string;
23253 if (no_props)
23254 face = Qnil;
23256 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23257 : EQ (face, Qt) ? (EQ (window, selected_window)
23258 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23259 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23260 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23261 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23262 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23263 : DEFAULT_FACE_ID;
23265 old_buffer = current_buffer;
23267 /* Save things including mode_line_proptrans_alist,
23268 and set that to nil so that we don't alter the outer value. */
23269 record_unwind_protect (unwind_format_mode_line,
23270 format_mode_line_unwind_data
23271 (XFRAME (WINDOW_FRAME (w)),
23272 old_buffer, selected_window, true));
23273 mode_line_proptrans_alist = Qnil;
23275 Fselect_window (window, Qt);
23276 set_buffer_internal_1 (XBUFFER (buffer));
23278 init_iterator (&it, w, -1, -1, NULL, face_id);
23280 if (no_props)
23282 mode_line_target = MODE_LINE_NOPROP;
23283 mode_line_string_face_prop = Qnil;
23284 mode_line_string_list = Qnil;
23285 string_start = MODE_LINE_NOPROP_LEN (0);
23287 else
23289 mode_line_target = MODE_LINE_STRING;
23290 mode_line_string_list = Qnil;
23291 mode_line_string_face = face;
23292 mode_line_string_face_prop
23293 = NILP (face) ? Qnil : list2 (Qface, face);
23296 push_kboard (FRAME_KBOARD (it.f));
23297 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23298 pop_kboard ();
23300 if (no_props)
23302 len = MODE_LINE_NOPROP_LEN (string_start);
23303 str = make_string (mode_line_noprop_buf + string_start, len);
23305 else
23307 mode_line_string_list = Fnreverse (mode_line_string_list);
23308 str = Fmapconcat (Qidentity, mode_line_string_list,
23309 empty_unibyte_string);
23312 unbind_to (count, Qnil);
23313 return str;
23316 /* Write a null-terminated, right justified decimal representation of
23317 the positive integer D to BUF using a minimal field width WIDTH. */
23319 static void
23320 pint2str (register char *buf, register int width, register ptrdiff_t d)
23322 register char *p = buf;
23324 if (d <= 0)
23325 *p++ = '0';
23326 else
23328 while (d > 0)
23330 *p++ = d % 10 + '0';
23331 d /= 10;
23335 for (width -= (int) (p - buf); width > 0; --width)
23336 *p++ = ' ';
23337 *p-- = '\0';
23338 while (p > buf)
23340 d = *buf;
23341 *buf++ = *p;
23342 *p-- = d;
23346 /* Write a null-terminated, right justified decimal and "human
23347 readable" representation of the nonnegative integer D to BUF using
23348 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23350 static const char power_letter[] =
23352 0, /* no letter */
23353 'k', /* kilo */
23354 'M', /* mega */
23355 'G', /* giga */
23356 'T', /* tera */
23357 'P', /* peta */
23358 'E', /* exa */
23359 'Z', /* zetta */
23360 'Y' /* yotta */
23363 static void
23364 pint2hrstr (char *buf, int width, ptrdiff_t d)
23366 /* We aim to represent the nonnegative integer D as
23367 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23368 ptrdiff_t quotient = d;
23369 int remainder = 0;
23370 /* -1 means: do not use TENTHS. */
23371 int tenths = -1;
23372 int exponent = 0;
23374 /* Length of QUOTIENT.TENTHS as a string. */
23375 int length;
23377 char * psuffix;
23378 char * p;
23380 if (quotient >= 1000)
23382 /* Scale to the appropriate EXPONENT. */
23385 remainder = quotient % 1000;
23386 quotient /= 1000;
23387 exponent++;
23389 while (quotient >= 1000);
23391 /* Round to nearest and decide whether to use TENTHS or not. */
23392 if (quotient <= 9)
23394 tenths = remainder / 100;
23395 if (remainder % 100 >= 50)
23397 if (tenths < 9)
23398 tenths++;
23399 else
23401 quotient++;
23402 if (quotient == 10)
23403 tenths = -1;
23404 else
23405 tenths = 0;
23409 else
23410 if (remainder >= 500)
23412 if (quotient < 999)
23413 quotient++;
23414 else
23416 quotient = 1;
23417 exponent++;
23418 tenths = 0;
23423 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23424 if (tenths == -1 && quotient <= 99)
23425 if (quotient <= 9)
23426 length = 1;
23427 else
23428 length = 2;
23429 else
23430 length = 3;
23431 p = psuffix = buf + max (width, length);
23433 /* Print EXPONENT. */
23434 *psuffix++ = power_letter[exponent];
23435 *psuffix = '\0';
23437 /* Print TENTHS. */
23438 if (tenths >= 0)
23440 *--p = '0' + tenths;
23441 *--p = '.';
23444 /* Print QUOTIENT. */
23447 int digit = quotient % 10;
23448 *--p = '0' + digit;
23450 while ((quotient /= 10) != 0);
23452 /* Print leading spaces. */
23453 while (buf < p)
23454 *--p = ' ';
23457 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23458 If EOL_FLAG, set also a mnemonic character for end-of-line
23459 type of CODING_SYSTEM. Return updated pointer into BUF. */
23461 static unsigned char invalid_eol_type[] = "(*invalid*)";
23463 static char *
23464 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23466 Lisp_Object val;
23467 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23468 const unsigned char *eol_str;
23469 int eol_str_len;
23470 /* The EOL conversion we are using. */
23471 Lisp_Object eoltype;
23473 val = CODING_SYSTEM_SPEC (coding_system);
23474 eoltype = Qnil;
23476 if (!VECTORP (val)) /* Not yet decided. */
23478 *buf++ = multibyte ? '-' : ' ';
23479 if (eol_flag)
23480 eoltype = eol_mnemonic_undecided;
23481 /* Don't mention EOL conversion if it isn't decided. */
23483 else
23485 Lisp_Object attrs;
23486 Lisp_Object eolvalue;
23488 attrs = AREF (val, 0);
23489 eolvalue = AREF (val, 2);
23491 *buf++ = multibyte
23492 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23493 : ' ';
23495 if (eol_flag)
23497 /* The EOL conversion that is normal on this system. */
23499 if (NILP (eolvalue)) /* Not yet decided. */
23500 eoltype = eol_mnemonic_undecided;
23501 else if (VECTORP (eolvalue)) /* Not yet decided. */
23502 eoltype = eol_mnemonic_undecided;
23503 else /* eolvalue is Qunix, Qdos, or Qmac. */
23504 eoltype = (EQ (eolvalue, Qunix)
23505 ? eol_mnemonic_unix
23506 : EQ (eolvalue, Qdos)
23507 ? eol_mnemonic_dos : eol_mnemonic_mac);
23511 if (eol_flag)
23513 /* Mention the EOL conversion if it is not the usual one. */
23514 if (STRINGP (eoltype))
23516 eol_str = SDATA (eoltype);
23517 eol_str_len = SBYTES (eoltype);
23519 else if (CHARACTERP (eoltype))
23521 int c = XFASTINT (eoltype);
23522 return buf + CHAR_STRING (c, (unsigned char *) buf);
23524 else
23526 eol_str = invalid_eol_type;
23527 eol_str_len = sizeof (invalid_eol_type) - 1;
23529 memcpy (buf, eol_str, eol_str_len);
23530 buf += eol_str_len;
23533 return buf;
23536 /* Return the approximate percentage N is of D (rounding upward), or 99,
23537 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23539 static int
23540 percent99 (ptrdiff_t n, ptrdiff_t d)
23542 int percent = (d - 1 + 100.0 * n) / d;
23543 return min (percent, 99);
23546 /* Return a string for the output of a mode line %-spec for window W,
23547 generated by character C. FIELD_WIDTH > 0 means pad the string
23548 returned with spaces to that value. Return a Lisp string in
23549 *STRING if the resulting string is taken from that Lisp string.
23551 Note we operate on the current buffer for most purposes. */
23553 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23555 static const char *
23556 decode_mode_spec (struct window *w, register int c, int field_width,
23557 Lisp_Object *string)
23559 Lisp_Object obj;
23560 struct frame *f = XFRAME (WINDOW_FRAME (w));
23561 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23562 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23563 produce strings from numerical values, so limit preposterously
23564 large values of FIELD_WIDTH to avoid overrunning the buffer's
23565 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23566 bytes plus the terminating null. */
23567 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23568 struct buffer *b = current_buffer;
23570 obj = Qnil;
23571 *string = Qnil;
23573 switch (c)
23575 case '*':
23576 if (!NILP (BVAR (b, read_only)))
23577 return "%";
23578 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23579 return "*";
23580 return "-";
23582 case '+':
23583 /* This differs from %* only for a modified read-only buffer. */
23584 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23585 return "*";
23586 if (!NILP (BVAR (b, read_only)))
23587 return "%";
23588 return "-";
23590 case '&':
23591 /* This differs from %* in ignoring read-only-ness. */
23592 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23593 return "*";
23594 return "-";
23596 case '%':
23597 return "%";
23599 case '[':
23601 int i;
23602 char *p;
23604 if (command_loop_level > 5)
23605 return "[[[... ";
23606 p = decode_mode_spec_buf;
23607 for (i = 0; i < command_loop_level; i++)
23608 *p++ = '[';
23609 *p = 0;
23610 return decode_mode_spec_buf;
23613 case ']':
23615 int i;
23616 char *p;
23618 if (command_loop_level > 5)
23619 return " ...]]]";
23620 p = decode_mode_spec_buf;
23621 for (i = 0; i < command_loop_level; i++)
23622 *p++ = ']';
23623 *p = 0;
23624 return decode_mode_spec_buf;
23627 case '-':
23629 register int i;
23631 /* Let lots_of_dashes be a string of infinite length. */
23632 if (mode_line_target == MODE_LINE_NOPROP
23633 || mode_line_target == MODE_LINE_STRING)
23634 return "--";
23635 if (field_width <= 0
23636 || field_width > sizeof (lots_of_dashes))
23638 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23639 decode_mode_spec_buf[i] = '-';
23640 decode_mode_spec_buf[i] = '\0';
23641 return decode_mode_spec_buf;
23643 else
23644 return lots_of_dashes;
23647 case 'b':
23648 obj = BVAR (b, name);
23649 break;
23651 case 'c':
23652 /* %c and %l are ignored in `frame-title-format'.
23653 (In redisplay_internal, the frame title is drawn _before_ the
23654 windows are updated, so the stuff which depends on actual
23655 window contents (such as %l) may fail to render properly, or
23656 even crash emacs.) */
23657 if (mode_line_target == MODE_LINE_TITLE)
23658 return "";
23659 else
23661 ptrdiff_t col = current_column ();
23662 w->column_number_displayed = col;
23663 pint2str (decode_mode_spec_buf, width, col);
23664 return decode_mode_spec_buf;
23667 case 'e':
23668 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23670 if (NILP (Vmemory_full))
23671 return "";
23672 else
23673 return "!MEM FULL! ";
23675 #else
23676 return "";
23677 #endif
23679 case 'F':
23680 /* %F displays the frame name. */
23681 if (!NILP (f->title))
23682 return SSDATA (f->title);
23683 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23684 return SSDATA (f->name);
23685 return "Emacs";
23687 case 'f':
23688 obj = BVAR (b, filename);
23689 break;
23691 case 'i':
23693 ptrdiff_t size = ZV - BEGV;
23694 pint2str (decode_mode_spec_buf, width, size);
23695 return decode_mode_spec_buf;
23698 case 'I':
23700 ptrdiff_t size = ZV - BEGV;
23701 pint2hrstr (decode_mode_spec_buf, width, size);
23702 return decode_mode_spec_buf;
23705 case 'l':
23707 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23708 ptrdiff_t topline, nlines, height;
23709 ptrdiff_t junk;
23711 /* %c and %l are ignored in `frame-title-format'. */
23712 if (mode_line_target == MODE_LINE_TITLE)
23713 return "";
23715 startpos = marker_position (w->start);
23716 startpos_byte = marker_byte_position (w->start);
23717 height = WINDOW_TOTAL_LINES (w);
23719 /* If we decided that this buffer isn't suitable for line numbers,
23720 don't forget that too fast. */
23721 if (w->base_line_pos == -1)
23722 goto no_value;
23724 /* If the buffer is very big, don't waste time. */
23725 if (INTEGERP (Vline_number_display_limit)
23726 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23728 w->base_line_pos = 0;
23729 w->base_line_number = 0;
23730 goto no_value;
23733 if (w->base_line_number > 0
23734 && w->base_line_pos > 0
23735 && w->base_line_pos <= startpos)
23737 line = w->base_line_number;
23738 linepos = w->base_line_pos;
23739 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23741 else
23743 line = 1;
23744 linepos = BUF_BEGV (b);
23745 linepos_byte = BUF_BEGV_BYTE (b);
23748 /* Count lines from base line to window start position. */
23749 nlines = display_count_lines (linepos_byte,
23750 startpos_byte,
23751 startpos, &junk);
23753 topline = nlines + line;
23755 /* Determine a new base line, if the old one is too close
23756 or too far away, or if we did not have one.
23757 "Too close" means it's plausible a scroll-down would
23758 go back past it. */
23759 if (startpos == BUF_BEGV (b))
23761 w->base_line_number = topline;
23762 w->base_line_pos = BUF_BEGV (b);
23764 else if (nlines < height + 25 || nlines > height * 3 + 50
23765 || linepos == BUF_BEGV (b))
23767 ptrdiff_t limit = BUF_BEGV (b);
23768 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23769 ptrdiff_t position;
23770 ptrdiff_t distance =
23771 (height * 2 + 30) * line_number_display_limit_width;
23773 if (startpos - distance > limit)
23775 limit = startpos - distance;
23776 limit_byte = CHAR_TO_BYTE (limit);
23779 nlines = display_count_lines (startpos_byte,
23780 limit_byte,
23781 - (height * 2 + 30),
23782 &position);
23783 /* If we couldn't find the lines we wanted within
23784 line_number_display_limit_width chars per line,
23785 give up on line numbers for this window. */
23786 if (position == limit_byte && limit == startpos - distance)
23788 w->base_line_pos = -1;
23789 w->base_line_number = 0;
23790 goto no_value;
23793 w->base_line_number = topline - nlines;
23794 w->base_line_pos = BYTE_TO_CHAR (position);
23797 /* Now count lines from the start pos to point. */
23798 nlines = display_count_lines (startpos_byte,
23799 PT_BYTE, PT, &junk);
23801 /* Record that we did display the line number. */
23802 line_number_displayed = true;
23804 /* Make the string to show. */
23805 pint2str (decode_mode_spec_buf, width, topline + nlines);
23806 return decode_mode_spec_buf;
23807 no_value:
23809 char *p = decode_mode_spec_buf;
23810 int pad = width - 2;
23811 while (pad-- > 0)
23812 *p++ = ' ';
23813 *p++ = '?';
23814 *p++ = '?';
23815 *p = '\0';
23816 return decode_mode_spec_buf;
23819 break;
23821 case 'm':
23822 obj = BVAR (b, mode_name);
23823 break;
23825 case 'n':
23826 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23827 return " Narrow";
23828 break;
23830 case 'p':
23832 ptrdiff_t pos = marker_position (w->start);
23833 ptrdiff_t begv = BUF_BEGV (b);
23834 ptrdiff_t zv = BUF_ZV (b);
23836 if (w->window_end_pos <= BUF_Z (b) - zv)
23837 return pos <= begv ? "All" : "Bottom";
23838 else if (pos <= begv)
23839 return "Top";
23840 else
23842 sprintf (decode_mode_spec_buf, "%2d%%",
23843 percent99 (pos - begv, zv - begv));
23844 return decode_mode_spec_buf;
23848 /* Display percentage of size above the bottom of the screen. */
23849 case 'P':
23851 ptrdiff_t toppos = marker_position (w->start);
23852 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23853 ptrdiff_t begv = BUF_BEGV (b);
23854 ptrdiff_t zv = BUF_ZV (b);
23856 if (zv <= botpos)
23857 return toppos <= begv ? "All" : "Bottom";
23858 else
23860 sprintf (decode_mode_spec_buf,
23861 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23862 percent99 (botpos - begv, zv - begv));
23863 return decode_mode_spec_buf;
23867 case 's':
23868 /* status of process */
23869 obj = Fget_buffer_process (Fcurrent_buffer ());
23870 if (NILP (obj))
23871 return "no process";
23872 #ifndef MSDOS
23873 obj = Fsymbol_name (Fprocess_status (obj));
23874 #endif
23875 break;
23877 case '@':
23879 ptrdiff_t count = inhibit_garbage_collection ();
23880 Lisp_Object curdir = BVAR (current_buffer, directory);
23881 Lisp_Object val = Qnil;
23883 if (STRINGP (curdir))
23884 val = call1 (intern ("file-remote-p"), curdir);
23886 unbind_to (count, Qnil);
23888 if (NILP (val))
23889 return "-";
23890 else
23891 return "@";
23894 case 'z':
23895 /* coding-system (not including end-of-line format) */
23896 case 'Z':
23897 /* coding-system (including end-of-line type) */
23899 bool eol_flag = (c == 'Z');
23900 char *p = decode_mode_spec_buf;
23902 if (! FRAME_WINDOW_P (f))
23904 /* No need to mention EOL here--the terminal never needs
23905 to do EOL conversion. */
23906 p = decode_mode_spec_coding (CODING_ID_NAME
23907 (FRAME_KEYBOARD_CODING (f)->id),
23908 p, false);
23909 p = decode_mode_spec_coding (CODING_ID_NAME
23910 (FRAME_TERMINAL_CODING (f)->id),
23911 p, false);
23913 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23914 p, eol_flag);
23916 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23917 #ifdef subprocesses
23918 obj = Fget_buffer_process (Fcurrent_buffer ());
23919 if (PROCESSP (obj))
23921 p = decode_mode_spec_coding
23922 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23923 p = decode_mode_spec_coding
23924 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23926 #endif /* subprocesses */
23927 #endif /* false */
23928 *p = 0;
23929 return decode_mode_spec_buf;
23933 if (STRINGP (obj))
23935 *string = obj;
23936 return SSDATA (obj);
23938 else
23939 return "";
23943 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23944 means count lines back from START_BYTE. But don't go beyond
23945 LIMIT_BYTE. Return the number of lines thus found (always
23946 nonnegative).
23948 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23949 either the position COUNT lines after/before START_BYTE, if we
23950 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23951 COUNT lines. */
23953 static ptrdiff_t
23954 display_count_lines (ptrdiff_t start_byte,
23955 ptrdiff_t limit_byte, ptrdiff_t count,
23956 ptrdiff_t *byte_pos_ptr)
23958 register unsigned char *cursor;
23959 unsigned char *base;
23961 register ptrdiff_t ceiling;
23962 register unsigned char *ceiling_addr;
23963 ptrdiff_t orig_count = count;
23965 /* If we are not in selective display mode,
23966 check only for newlines. */
23967 bool selective_display
23968 = (!NILP (BVAR (current_buffer, selective_display))
23969 && !INTEGERP (BVAR (current_buffer, selective_display)));
23971 if (count > 0)
23973 while (start_byte < limit_byte)
23975 ceiling = BUFFER_CEILING_OF (start_byte);
23976 ceiling = min (limit_byte - 1, ceiling);
23977 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23978 base = (cursor = BYTE_POS_ADDR (start_byte));
23982 if (selective_display)
23984 while (*cursor != '\n' && *cursor != 015
23985 && ++cursor != ceiling_addr)
23986 continue;
23987 if (cursor == ceiling_addr)
23988 break;
23990 else
23992 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23993 if (! cursor)
23994 break;
23997 cursor++;
23999 if (--count == 0)
24001 start_byte += cursor - base;
24002 *byte_pos_ptr = start_byte;
24003 return orig_count;
24006 while (cursor < ceiling_addr);
24008 start_byte += ceiling_addr - base;
24011 else
24013 while (start_byte > limit_byte)
24015 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24016 ceiling = max (limit_byte, ceiling);
24017 ceiling_addr = BYTE_POS_ADDR (ceiling);
24018 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24019 while (true)
24021 if (selective_display)
24023 while (--cursor >= ceiling_addr
24024 && *cursor != '\n' && *cursor != 015)
24025 continue;
24026 if (cursor < ceiling_addr)
24027 break;
24029 else
24031 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24032 if (! cursor)
24033 break;
24036 if (++count == 0)
24038 start_byte += cursor - base + 1;
24039 *byte_pos_ptr = start_byte;
24040 /* When scanning backwards, we should
24041 not count the newline posterior to which we stop. */
24042 return - orig_count - 1;
24045 start_byte += ceiling_addr - base;
24049 *byte_pos_ptr = limit_byte;
24051 if (count < 0)
24052 return - orig_count + count;
24053 return orig_count - count;
24059 /***********************************************************************
24060 Displaying strings
24061 ***********************************************************************/
24063 /* Display a NUL-terminated string, starting with index START.
24065 If STRING is non-null, display that C string. Otherwise, the Lisp
24066 string LISP_STRING is displayed. There's a case that STRING is
24067 non-null and LISP_STRING is not nil. It means STRING is a string
24068 data of LISP_STRING. In that case, we display LISP_STRING while
24069 ignoring its text properties.
24071 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24072 FACE_STRING. Display STRING or LISP_STRING with the face at
24073 FACE_STRING_POS in FACE_STRING:
24075 Display the string in the environment given by IT, but use the
24076 standard display table, temporarily.
24078 FIELD_WIDTH is the minimum number of output glyphs to produce.
24079 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24080 with spaces. If STRING has more characters, more than FIELD_WIDTH
24081 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24083 PRECISION is the maximum number of characters to output from
24084 STRING. PRECISION < 0 means don't truncate the string.
24086 This is roughly equivalent to printf format specifiers:
24088 FIELD_WIDTH PRECISION PRINTF
24089 ----------------------------------------
24090 -1 -1 %s
24091 -1 10 %.10s
24092 10 -1 %10s
24093 20 10 %20.10s
24095 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24096 display them, and < 0 means obey the current buffer's value of
24097 enable_multibyte_characters.
24099 Value is the number of columns displayed. */
24101 static int
24102 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24103 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24104 int field_width, int precision, int max_x, int multibyte)
24106 int hpos_at_start = it->hpos;
24107 int saved_face_id = it->face_id;
24108 struct glyph_row *row = it->glyph_row;
24109 ptrdiff_t it_charpos;
24111 /* Initialize the iterator IT for iteration over STRING beginning
24112 with index START. */
24113 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24114 precision, field_width, multibyte);
24115 if (string && STRINGP (lisp_string))
24116 /* LISP_STRING is the one returned by decode_mode_spec. We should
24117 ignore its text properties. */
24118 it->stop_charpos = it->end_charpos;
24120 /* If displaying STRING, set up the face of the iterator from
24121 FACE_STRING, if that's given. */
24122 if (STRINGP (face_string))
24124 ptrdiff_t endptr;
24125 struct face *face;
24127 it->face_id
24128 = face_at_string_position (it->w, face_string, face_string_pos,
24129 0, &endptr, it->base_face_id, false);
24130 face = FACE_FROM_ID (it->f, it->face_id);
24131 it->face_box_p = face->box != FACE_NO_BOX;
24134 /* Set max_x to the maximum allowed X position. Don't let it go
24135 beyond the right edge of the window. */
24136 if (max_x <= 0)
24137 max_x = it->last_visible_x;
24138 else
24139 max_x = min (max_x, it->last_visible_x);
24141 /* Skip over display elements that are not visible. because IT->w is
24142 hscrolled. */
24143 if (it->current_x < it->first_visible_x)
24144 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24145 MOVE_TO_POS | MOVE_TO_X);
24147 row->ascent = it->max_ascent;
24148 row->height = it->max_ascent + it->max_descent;
24149 row->phys_ascent = it->max_phys_ascent;
24150 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24151 row->extra_line_spacing = it->max_extra_line_spacing;
24153 if (STRINGP (it->string))
24154 it_charpos = IT_STRING_CHARPOS (*it);
24155 else
24156 it_charpos = IT_CHARPOS (*it);
24158 /* This condition is for the case that we are called with current_x
24159 past last_visible_x. */
24160 while (it->current_x < max_x)
24162 int x_before, x, n_glyphs_before, i, nglyphs;
24164 /* Get the next display element. */
24165 if (!get_next_display_element (it))
24166 break;
24168 /* Produce glyphs. */
24169 x_before = it->current_x;
24170 n_glyphs_before = row->used[TEXT_AREA];
24171 PRODUCE_GLYPHS (it);
24173 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24174 i = 0;
24175 x = x_before;
24176 while (i < nglyphs)
24178 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24180 if (it->line_wrap != TRUNCATE
24181 && x + glyph->pixel_width > max_x)
24183 /* End of continued line or max_x reached. */
24184 if (CHAR_GLYPH_PADDING_P (*glyph))
24186 /* A wide character is unbreakable. */
24187 if (row->reversed_p)
24188 unproduce_glyphs (it, row->used[TEXT_AREA]
24189 - n_glyphs_before);
24190 row->used[TEXT_AREA] = n_glyphs_before;
24191 it->current_x = x_before;
24193 else
24195 if (row->reversed_p)
24196 unproduce_glyphs (it, row->used[TEXT_AREA]
24197 - (n_glyphs_before + i));
24198 row->used[TEXT_AREA] = n_glyphs_before + i;
24199 it->current_x = x;
24201 break;
24203 else if (x + glyph->pixel_width >= it->first_visible_x)
24205 /* Glyph is at least partially visible. */
24206 ++it->hpos;
24207 if (x < it->first_visible_x)
24208 row->x = x - it->first_visible_x;
24210 else
24212 /* Glyph is off the left margin of the display area.
24213 Should not happen. */
24214 emacs_abort ();
24217 row->ascent = max (row->ascent, it->max_ascent);
24218 row->height = max (row->height, it->max_ascent + it->max_descent);
24219 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24220 row->phys_height = max (row->phys_height,
24221 it->max_phys_ascent + it->max_phys_descent);
24222 row->extra_line_spacing = max (row->extra_line_spacing,
24223 it->max_extra_line_spacing);
24224 x += glyph->pixel_width;
24225 ++i;
24228 /* Stop if max_x reached. */
24229 if (i < nglyphs)
24230 break;
24232 /* Stop at line ends. */
24233 if (ITERATOR_AT_END_OF_LINE_P (it))
24235 it->continuation_lines_width = 0;
24236 break;
24239 set_iterator_to_next (it, true);
24240 if (STRINGP (it->string))
24241 it_charpos = IT_STRING_CHARPOS (*it);
24242 else
24243 it_charpos = IT_CHARPOS (*it);
24245 /* Stop if truncating at the right edge. */
24246 if (it->line_wrap == TRUNCATE
24247 && it->current_x >= it->last_visible_x)
24249 /* Add truncation mark, but don't do it if the line is
24250 truncated at a padding space. */
24251 if (it_charpos < it->string_nchars)
24253 if (!FRAME_WINDOW_P (it->f))
24255 int ii, n;
24257 if (it->current_x > it->last_visible_x)
24259 if (!row->reversed_p)
24261 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24262 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24263 break;
24265 else
24267 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24268 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24269 break;
24270 unproduce_glyphs (it, ii + 1);
24271 ii = row->used[TEXT_AREA] - (ii + 1);
24273 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24275 row->used[TEXT_AREA] = ii;
24276 produce_special_glyphs (it, IT_TRUNCATION);
24279 produce_special_glyphs (it, IT_TRUNCATION);
24281 row->truncated_on_right_p = true;
24283 break;
24287 /* Maybe insert a truncation at the left. */
24288 if (it->first_visible_x
24289 && it_charpos > 0)
24291 if (!FRAME_WINDOW_P (it->f)
24292 || (row->reversed_p
24293 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24294 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24295 insert_left_trunc_glyphs (it);
24296 row->truncated_on_left_p = true;
24299 it->face_id = saved_face_id;
24301 /* Value is number of columns displayed. */
24302 return it->hpos - hpos_at_start;
24307 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24308 appears as an element of LIST or as the car of an element of LIST.
24309 If PROPVAL is a list, compare each element against LIST in that
24310 way, and return 1/2 if any element of PROPVAL is found in LIST.
24311 Otherwise return 0. This function cannot quit.
24312 The return value is 2 if the text is invisible but with an ellipsis
24313 and 1 if it's invisible and without an ellipsis. */
24316 invisible_prop (Lisp_Object propval, Lisp_Object list)
24318 Lisp_Object tail, proptail;
24320 for (tail = list; CONSP (tail); tail = XCDR (tail))
24322 register Lisp_Object tem;
24323 tem = XCAR (tail);
24324 if (EQ (propval, tem))
24325 return 1;
24326 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24327 return NILP (XCDR (tem)) ? 1 : 2;
24330 if (CONSP (propval))
24332 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24334 Lisp_Object propelt;
24335 propelt = XCAR (proptail);
24336 for (tail = list; CONSP (tail); tail = XCDR (tail))
24338 register Lisp_Object tem;
24339 tem = XCAR (tail);
24340 if (EQ (propelt, tem))
24341 return 1;
24342 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24343 return NILP (XCDR (tem)) ? 1 : 2;
24348 return 0;
24351 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24352 doc: /* Non-nil if the property makes the text invisible.
24353 POS-OR-PROP can be a marker or number, in which case it is taken to be
24354 a position in the current buffer and the value of the `invisible' property
24355 is checked; or it can be some other value, which is then presumed to be the
24356 value of the `invisible' property of the text of interest.
24357 The non-nil value returned can be t for truly invisible text or something
24358 else if the text is replaced by an ellipsis. */)
24359 (Lisp_Object pos_or_prop)
24361 Lisp_Object prop
24362 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24363 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24364 : pos_or_prop);
24365 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24366 return (invis == 0 ? Qnil
24367 : invis == 1 ? Qt
24368 : make_number (invis));
24371 /* Calculate a width or height in pixels from a specification using
24372 the following elements:
24374 SPEC ::=
24375 NUM - a (fractional) multiple of the default font width/height
24376 (NUM) - specifies exactly NUM pixels
24377 UNIT - a fixed number of pixels, see below.
24378 ELEMENT - size of a display element in pixels, see below.
24379 (NUM . SPEC) - equals NUM * SPEC
24380 (+ SPEC SPEC ...) - add pixel values
24381 (- SPEC SPEC ...) - subtract pixel values
24382 (- SPEC) - negate pixel value
24384 NUM ::=
24385 INT or FLOAT - a number constant
24386 SYMBOL - use symbol's (buffer local) variable binding.
24388 UNIT ::=
24389 in - pixels per inch *)
24390 mm - pixels per 1/1000 meter *)
24391 cm - pixels per 1/100 meter *)
24392 width - width of current font in pixels.
24393 height - height of current font in pixels.
24395 *) using the ratio(s) defined in display-pixels-per-inch.
24397 ELEMENT ::=
24399 left-fringe - left fringe width in pixels
24400 right-fringe - right fringe width in pixels
24402 left-margin - left margin width in pixels
24403 right-margin - right margin width in pixels
24405 scroll-bar - scroll-bar area width in pixels
24407 Examples:
24409 Pixels corresponding to 5 inches:
24410 (5 . in)
24412 Total width of non-text areas on left side of window (if scroll-bar is on left):
24413 '(space :width (+ left-fringe left-margin scroll-bar))
24415 Align to first text column (in header line):
24416 '(space :align-to 0)
24418 Align to middle of text area minus half the width of variable `my-image'
24419 containing a loaded image:
24420 '(space :align-to (0.5 . (- text my-image)))
24422 Width of left margin minus width of 1 character in the default font:
24423 '(space :width (- left-margin 1))
24425 Width of left margin minus width of 2 characters in the current font:
24426 '(space :width (- left-margin (2 . width)))
24428 Center 1 character over left-margin (in header line):
24429 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24431 Different ways to express width of left fringe plus left margin minus one pixel:
24432 '(space :width (- (+ left-fringe left-margin) (1)))
24433 '(space :width (+ left-fringe left-margin (- (1))))
24434 '(space :width (+ left-fringe left-margin (-1)))
24438 static bool
24439 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24440 struct font *font, bool width_p, int *align_to)
24442 double pixels;
24444 # define OK_PIXELS(val) (*res = (val), true)
24445 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24447 if (NILP (prop))
24448 return OK_PIXELS (0);
24450 eassert (FRAME_LIVE_P (it->f));
24452 if (SYMBOLP (prop))
24454 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24456 char *unit = SSDATA (SYMBOL_NAME (prop));
24458 if (unit[0] == 'i' && unit[1] == 'n')
24459 pixels = 1.0;
24460 else if (unit[0] == 'm' && unit[1] == 'm')
24461 pixels = 25.4;
24462 else if (unit[0] == 'c' && unit[1] == 'm')
24463 pixels = 2.54;
24464 else
24465 pixels = 0;
24466 if (pixels > 0)
24468 double ppi = (width_p ? FRAME_RES_X (it->f)
24469 : FRAME_RES_Y (it->f));
24471 if (ppi > 0)
24472 return OK_PIXELS (ppi / pixels);
24473 return false;
24477 #ifdef HAVE_WINDOW_SYSTEM
24478 if (EQ (prop, Qheight))
24479 return OK_PIXELS (font
24480 ? normal_char_height (font, -1)
24481 : FRAME_LINE_HEIGHT (it->f));
24482 if (EQ (prop, Qwidth))
24483 return OK_PIXELS (font
24484 ? FONT_WIDTH (font)
24485 : FRAME_COLUMN_WIDTH (it->f));
24486 #else
24487 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24488 return OK_PIXELS (1);
24489 #endif
24491 if (EQ (prop, Qtext))
24492 return OK_PIXELS (width_p
24493 ? window_box_width (it->w, TEXT_AREA)
24494 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24496 if (align_to && *align_to < 0)
24498 *res = 0;
24499 if (EQ (prop, Qleft))
24500 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24501 if (EQ (prop, Qright))
24502 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24503 if (EQ (prop, Qcenter))
24504 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24505 + window_box_width (it->w, TEXT_AREA) / 2);
24506 if (EQ (prop, Qleft_fringe))
24507 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24508 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24509 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24510 if (EQ (prop, Qright_fringe))
24511 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24512 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24513 : window_box_right_offset (it->w, TEXT_AREA));
24514 if (EQ (prop, Qleft_margin))
24515 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24516 if (EQ (prop, Qright_margin))
24517 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24518 if (EQ (prop, Qscroll_bar))
24519 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24521 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24522 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24523 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24524 : 0)));
24526 else
24528 if (EQ (prop, Qleft_fringe))
24529 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24530 if (EQ (prop, Qright_fringe))
24531 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24532 if (EQ (prop, Qleft_margin))
24533 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24534 if (EQ (prop, Qright_margin))
24535 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24536 if (EQ (prop, Qscroll_bar))
24537 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24540 prop = buffer_local_value (prop, it->w->contents);
24541 if (EQ (prop, Qunbound))
24542 prop = Qnil;
24545 if (NUMBERP (prop))
24547 int base_unit = (width_p
24548 ? FRAME_COLUMN_WIDTH (it->f)
24549 : FRAME_LINE_HEIGHT (it->f));
24550 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24553 if (CONSP (prop))
24555 Lisp_Object car = XCAR (prop);
24556 Lisp_Object cdr = XCDR (prop);
24558 if (SYMBOLP (car))
24560 #ifdef HAVE_WINDOW_SYSTEM
24561 if (FRAME_WINDOW_P (it->f)
24562 && valid_image_p (prop))
24564 ptrdiff_t id = lookup_image (it->f, prop);
24565 struct image *img = IMAGE_FROM_ID (it->f, id);
24567 return OK_PIXELS (width_p ? img->width : img->height);
24569 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24571 /* TODO: Don't return dummy size. */
24572 return OK_PIXELS (100);
24574 #endif
24575 if (EQ (car, Qplus) || EQ (car, Qminus))
24577 bool first = true;
24578 double px;
24580 pixels = 0;
24581 while (CONSP (cdr))
24583 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24584 font, width_p, align_to))
24585 return false;
24586 if (first)
24587 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24588 else
24589 pixels += px;
24590 cdr = XCDR (cdr);
24592 if (EQ (car, Qminus))
24593 pixels = -pixels;
24594 return OK_PIXELS (pixels);
24597 car = buffer_local_value (car, it->w->contents);
24598 if (EQ (car, Qunbound))
24599 car = Qnil;
24602 if (NUMBERP (car))
24604 double fact;
24605 pixels = XFLOATINT (car);
24606 if (NILP (cdr))
24607 return OK_PIXELS (pixels);
24608 if (calc_pixel_width_or_height (&fact, it, cdr,
24609 font, width_p, align_to))
24610 return OK_PIXELS (pixels * fact);
24611 return false;
24614 return false;
24617 return false;
24620 void
24621 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24623 #ifdef HAVE_WINDOW_SYSTEM
24624 normal_char_ascent_descent (font, -1, ascent, descent);
24625 #else
24626 *ascent = 1;
24627 *descent = 0;
24628 #endif
24632 /***********************************************************************
24633 Glyph Display
24634 ***********************************************************************/
24636 #ifdef HAVE_WINDOW_SYSTEM
24638 #ifdef GLYPH_DEBUG
24640 void
24641 dump_glyph_string (struct glyph_string *s)
24643 fprintf (stderr, "glyph string\n");
24644 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24645 s->x, s->y, s->width, s->height);
24646 fprintf (stderr, " ybase = %d\n", s->ybase);
24647 fprintf (stderr, " hl = %d\n", s->hl);
24648 fprintf (stderr, " left overhang = %d, right = %d\n",
24649 s->left_overhang, s->right_overhang);
24650 fprintf (stderr, " nchars = %d\n", s->nchars);
24651 fprintf (stderr, " extends to end of line = %d\n",
24652 s->extends_to_end_of_line_p);
24653 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24654 fprintf (stderr, " bg width = %d\n", s->background_width);
24657 #endif /* GLYPH_DEBUG */
24659 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24660 of XChar2b structures for S; it can't be allocated in
24661 init_glyph_string because it must be allocated via `alloca'. W
24662 is the window on which S is drawn. ROW and AREA are the glyph row
24663 and area within the row from which S is constructed. START is the
24664 index of the first glyph structure covered by S. HL is a
24665 face-override for drawing S. */
24667 #ifdef HAVE_NTGUI
24668 #define OPTIONAL_HDC(hdc) HDC hdc,
24669 #define DECLARE_HDC(hdc) HDC hdc;
24670 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24671 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24672 #endif
24674 #ifndef OPTIONAL_HDC
24675 #define OPTIONAL_HDC(hdc)
24676 #define DECLARE_HDC(hdc)
24677 #define ALLOCATE_HDC(hdc, f)
24678 #define RELEASE_HDC(hdc, f)
24679 #endif
24681 static void
24682 init_glyph_string (struct glyph_string *s,
24683 OPTIONAL_HDC (hdc)
24684 XChar2b *char2b, struct window *w, struct glyph_row *row,
24685 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24687 memset (s, 0, sizeof *s);
24688 s->w = w;
24689 s->f = XFRAME (w->frame);
24690 #ifdef HAVE_NTGUI
24691 s->hdc = hdc;
24692 #endif
24693 s->display = FRAME_X_DISPLAY (s->f);
24694 s->char2b = char2b;
24695 s->hl = hl;
24696 s->row = row;
24697 s->area = area;
24698 s->first_glyph = row->glyphs[area] + start;
24699 s->height = row->height;
24700 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24701 s->ybase = s->y + row->ascent;
24705 /* Append the list of glyph strings with head H and tail T to the list
24706 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24708 static void
24709 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24710 struct glyph_string *h, struct glyph_string *t)
24712 if (h)
24714 if (*head)
24715 (*tail)->next = h;
24716 else
24717 *head = h;
24718 h->prev = *tail;
24719 *tail = t;
24724 /* Prepend the list of glyph strings with head H and tail T to the
24725 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24726 result. */
24728 static void
24729 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24730 struct glyph_string *h, struct glyph_string *t)
24732 if (h)
24734 if (*head)
24735 (*head)->prev = t;
24736 else
24737 *tail = t;
24738 t->next = *head;
24739 *head = h;
24744 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24745 Set *HEAD and *TAIL to the resulting list. */
24747 static void
24748 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24749 struct glyph_string *s)
24751 s->next = s->prev = NULL;
24752 append_glyph_string_lists (head, tail, s, s);
24756 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24757 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24758 make sure that X resources for the face returned are allocated.
24759 Value is a pointer to a realized face that is ready for display if
24760 DISPLAY_P. */
24762 static struct face *
24763 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24764 XChar2b *char2b, bool display_p)
24766 struct face *face = FACE_FROM_ID (f, face_id);
24767 unsigned code = 0;
24769 if (face->font)
24771 code = face->font->driver->encode_char (face->font, c);
24773 if (code == FONT_INVALID_CODE)
24774 code = 0;
24776 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24778 /* Make sure X resources of the face are allocated. */
24779 #ifdef HAVE_X_WINDOWS
24780 if (display_p)
24781 #endif
24783 eassert (face != NULL);
24784 prepare_face_for_display (f, face);
24787 return face;
24791 /* Get face and two-byte form of character glyph GLYPH on frame F.
24792 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24793 a pointer to a realized face that is ready for display. */
24795 static struct face *
24796 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24797 XChar2b *char2b)
24799 struct face *face;
24800 unsigned code = 0;
24802 eassert (glyph->type == CHAR_GLYPH);
24803 face = FACE_FROM_ID (f, glyph->face_id);
24805 /* Make sure X resources of the face are allocated. */
24806 prepare_face_for_display (f, face);
24808 if (face->font)
24810 if (CHAR_BYTE8_P (glyph->u.ch))
24811 code = CHAR_TO_BYTE8 (glyph->u.ch);
24812 else
24813 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24815 if (code == FONT_INVALID_CODE)
24816 code = 0;
24819 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24820 return face;
24824 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24825 Return true iff FONT has a glyph for C. */
24827 static bool
24828 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24830 unsigned code;
24832 if (CHAR_BYTE8_P (c))
24833 code = CHAR_TO_BYTE8 (c);
24834 else
24835 code = font->driver->encode_char (font, c);
24837 if (code == FONT_INVALID_CODE)
24838 return false;
24839 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24840 return true;
24844 /* Fill glyph string S with composition components specified by S->cmp.
24846 BASE_FACE is the base face of the composition.
24847 S->cmp_from is the index of the first component for S.
24849 OVERLAPS non-zero means S should draw the foreground only, and use
24850 its physical height for clipping. See also draw_glyphs.
24852 Value is the index of a component not in S. */
24854 static int
24855 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24856 int overlaps)
24858 int i;
24859 /* For all glyphs of this composition, starting at the offset
24860 S->cmp_from, until we reach the end of the definition or encounter a
24861 glyph that requires the different face, add it to S. */
24862 struct face *face;
24864 eassert (s);
24866 s->for_overlaps = overlaps;
24867 s->face = NULL;
24868 s->font = NULL;
24869 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24871 int c = COMPOSITION_GLYPH (s->cmp, i);
24873 /* TAB in a composition means display glyphs with padding space
24874 on the left or right. */
24875 if (c != '\t')
24877 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24878 -1, Qnil);
24880 face = get_char_face_and_encoding (s->f, c, face_id,
24881 s->char2b + i, true);
24882 if (face)
24884 if (! s->face)
24886 s->face = face;
24887 s->font = s->face->font;
24889 else if (s->face != face)
24890 break;
24893 ++s->nchars;
24895 s->cmp_to = i;
24897 if (s->face == NULL)
24899 s->face = base_face->ascii_face;
24900 s->font = s->face->font;
24903 /* All glyph strings for the same composition has the same width,
24904 i.e. the width set for the first component of the composition. */
24905 s->width = s->first_glyph->pixel_width;
24907 /* If the specified font could not be loaded, use the frame's
24908 default font, but record the fact that we couldn't load it in
24909 the glyph string so that we can draw rectangles for the
24910 characters of the glyph string. */
24911 if (s->font == NULL)
24913 s->font_not_found_p = true;
24914 s->font = FRAME_FONT (s->f);
24917 /* Adjust base line for subscript/superscript text. */
24918 s->ybase += s->first_glyph->voffset;
24920 return s->cmp_to;
24923 static int
24924 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24925 int start, int end, int overlaps)
24927 struct glyph *glyph, *last;
24928 Lisp_Object lgstring;
24929 int i;
24931 s->for_overlaps = overlaps;
24932 glyph = s->row->glyphs[s->area] + start;
24933 last = s->row->glyphs[s->area] + end;
24934 s->cmp_id = glyph->u.cmp.id;
24935 s->cmp_from = glyph->slice.cmp.from;
24936 s->cmp_to = glyph->slice.cmp.to + 1;
24937 s->face = FACE_FROM_ID (s->f, face_id);
24938 lgstring = composition_gstring_from_id (s->cmp_id);
24939 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24940 glyph++;
24941 while (glyph < last
24942 && glyph->u.cmp.automatic
24943 && glyph->u.cmp.id == s->cmp_id
24944 && s->cmp_to == glyph->slice.cmp.from)
24945 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24947 for (i = s->cmp_from; i < s->cmp_to; i++)
24949 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24950 unsigned code = LGLYPH_CODE (lglyph);
24952 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24954 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24955 return glyph - s->row->glyphs[s->area];
24959 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24960 See the comment of fill_glyph_string for arguments.
24961 Value is the index of the first glyph not in S. */
24964 static int
24965 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24966 int start, int end, int overlaps)
24968 struct glyph *glyph, *last;
24969 int voffset;
24971 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24972 s->for_overlaps = overlaps;
24973 glyph = s->row->glyphs[s->area] + start;
24974 last = s->row->glyphs[s->area] + end;
24975 voffset = glyph->voffset;
24976 s->face = FACE_FROM_ID (s->f, face_id);
24977 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24978 s->nchars = 1;
24979 s->width = glyph->pixel_width;
24980 glyph++;
24981 while (glyph < last
24982 && glyph->type == GLYPHLESS_GLYPH
24983 && glyph->voffset == voffset
24984 && glyph->face_id == face_id)
24986 s->nchars++;
24987 s->width += glyph->pixel_width;
24988 glyph++;
24990 s->ybase += voffset;
24991 return glyph - s->row->glyphs[s->area];
24995 /* Fill glyph string S from a sequence of character glyphs.
24997 FACE_ID is the face id of the string. START is the index of the
24998 first glyph to consider, END is the index of the last + 1.
24999 OVERLAPS non-zero means S should draw the foreground only, and use
25000 its physical height for clipping. See also draw_glyphs.
25002 Value is the index of the first glyph not in S. */
25004 static int
25005 fill_glyph_string (struct glyph_string *s, int face_id,
25006 int start, int end, int overlaps)
25008 struct glyph *glyph, *last;
25009 int voffset;
25010 bool glyph_not_available_p;
25012 eassert (s->f == XFRAME (s->w->frame));
25013 eassert (s->nchars == 0);
25014 eassert (start >= 0 && end > start);
25016 s->for_overlaps = overlaps;
25017 glyph = s->row->glyphs[s->area] + start;
25018 last = s->row->glyphs[s->area] + end;
25019 voffset = glyph->voffset;
25020 s->padding_p = glyph->padding_p;
25021 glyph_not_available_p = glyph->glyph_not_available_p;
25023 while (glyph < last
25024 && glyph->type == CHAR_GLYPH
25025 && glyph->voffset == voffset
25026 /* Same face id implies same font, nowadays. */
25027 && glyph->face_id == face_id
25028 && glyph->glyph_not_available_p == glyph_not_available_p)
25030 s->face = get_glyph_face_and_encoding (s->f, glyph,
25031 s->char2b + s->nchars);
25032 ++s->nchars;
25033 eassert (s->nchars <= end - start);
25034 s->width += glyph->pixel_width;
25035 if (glyph++->padding_p != s->padding_p)
25036 break;
25039 s->font = s->face->font;
25041 /* If the specified font could not be loaded, use the frame's font,
25042 but record the fact that we couldn't load it in
25043 S->font_not_found_p so that we can draw rectangles for the
25044 characters of the glyph string. */
25045 if (s->font == NULL || glyph_not_available_p)
25047 s->font_not_found_p = true;
25048 s->font = FRAME_FONT (s->f);
25051 /* Adjust base line for subscript/superscript text. */
25052 s->ybase += voffset;
25054 eassert (s->face && s->face->gc);
25055 return glyph - s->row->glyphs[s->area];
25059 /* Fill glyph string S from image glyph S->first_glyph. */
25061 static void
25062 fill_image_glyph_string (struct glyph_string *s)
25064 eassert (s->first_glyph->type == IMAGE_GLYPH);
25065 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25066 eassert (s->img);
25067 s->slice = s->first_glyph->slice.img;
25068 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25069 s->font = s->face->font;
25070 s->width = s->first_glyph->pixel_width;
25072 /* Adjust base line for subscript/superscript text. */
25073 s->ybase += s->first_glyph->voffset;
25077 #ifdef HAVE_XWIDGETS
25078 static void
25079 fill_xwidget_glyph_string (struct glyph_string *s)
25081 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25082 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25083 s->font = s->face->font;
25084 s->width = s->first_glyph->pixel_width;
25085 s->ybase += s->first_glyph->voffset;
25086 s->xwidget = s->first_glyph->u.xwidget;
25088 #endif
25089 /* Fill glyph string S from a sequence of stretch glyphs.
25091 START is the index of the first glyph to consider,
25092 END is the index of the last + 1.
25094 Value is the index of the first glyph not in S. */
25096 static int
25097 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25099 struct glyph *glyph, *last;
25100 int voffset, face_id;
25102 eassert (s->first_glyph->type == STRETCH_GLYPH);
25104 glyph = s->row->glyphs[s->area] + start;
25105 last = s->row->glyphs[s->area] + end;
25106 face_id = glyph->face_id;
25107 s->face = FACE_FROM_ID (s->f, face_id);
25108 s->font = s->face->font;
25109 s->width = glyph->pixel_width;
25110 s->nchars = 1;
25111 voffset = glyph->voffset;
25113 for (++glyph;
25114 (glyph < last
25115 && glyph->type == STRETCH_GLYPH
25116 && glyph->voffset == voffset
25117 && glyph->face_id == face_id);
25118 ++glyph)
25119 s->width += glyph->pixel_width;
25121 /* Adjust base line for subscript/superscript text. */
25122 s->ybase += voffset;
25124 /* The case that face->gc == 0 is handled when drawing the glyph
25125 string by calling prepare_face_for_display. */
25126 eassert (s->face);
25127 return glyph - s->row->glyphs[s->area];
25130 static struct font_metrics *
25131 get_per_char_metric (struct font *font, XChar2b *char2b)
25133 static struct font_metrics metrics;
25134 unsigned code;
25136 if (! font)
25137 return NULL;
25138 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25139 if (code == FONT_INVALID_CODE)
25140 return NULL;
25141 font->driver->text_extents (font, &code, 1, &metrics);
25142 return &metrics;
25145 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25146 for FONT. Values are taken from font-global ones, except for fonts
25147 that claim preposterously large values, but whose glyphs actually
25148 have reasonable dimensions. C is the character to use for metrics
25149 if the font-global values are too large; if C is negative, the
25150 function selects a default character. */
25151 static void
25152 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25154 *ascent = FONT_BASE (font);
25155 *descent = FONT_DESCENT (font);
25157 if (FONT_TOO_HIGH (font))
25159 XChar2b char2b;
25161 /* Get metrics of C, defaulting to a reasonably sized ASCII
25162 character. */
25163 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25165 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25167 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25169 /* We add 1 pixel to character dimensions as heuristics
25170 that produces nicer display, e.g. when the face has
25171 the box attribute. */
25172 *ascent = pcm->ascent + 1;
25173 *descent = pcm->descent + 1;
25179 /* A subroutine that computes a reasonable "normal character height"
25180 for fonts that claim preposterously large vertical dimensions, but
25181 whose glyphs are actually reasonably sized. C is the character
25182 whose metrics to use for those fonts, or -1 for default
25183 character. */
25184 static int
25185 normal_char_height (struct font *font, int c)
25187 int ascent, descent;
25189 normal_char_ascent_descent (font, c, &ascent, &descent);
25191 return ascent + descent;
25194 /* EXPORT for RIF:
25195 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25196 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25197 assumed to be zero. */
25199 void
25200 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25202 *left = *right = 0;
25204 if (glyph->type == CHAR_GLYPH)
25206 XChar2b char2b;
25207 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25208 if (face->font)
25210 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25211 if (pcm)
25213 if (pcm->rbearing > pcm->width)
25214 *right = pcm->rbearing - pcm->width;
25215 if (pcm->lbearing < 0)
25216 *left = -pcm->lbearing;
25220 else if (glyph->type == COMPOSITE_GLYPH)
25222 if (! glyph->u.cmp.automatic)
25224 struct composition *cmp = composition_table[glyph->u.cmp.id];
25226 if (cmp->rbearing > cmp->pixel_width)
25227 *right = cmp->rbearing - cmp->pixel_width;
25228 if (cmp->lbearing < 0)
25229 *left = - cmp->lbearing;
25231 else
25233 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25234 struct font_metrics metrics;
25236 composition_gstring_width (gstring, glyph->slice.cmp.from,
25237 glyph->slice.cmp.to + 1, &metrics);
25238 if (metrics.rbearing > metrics.width)
25239 *right = metrics.rbearing - metrics.width;
25240 if (metrics.lbearing < 0)
25241 *left = - metrics.lbearing;
25247 /* Return the index of the first glyph preceding glyph string S that
25248 is overwritten by S because of S's left overhang. Value is -1
25249 if no glyphs are overwritten. */
25251 static int
25252 left_overwritten (struct glyph_string *s)
25254 int k;
25256 if (s->left_overhang)
25258 int x = 0, i;
25259 struct glyph *glyphs = s->row->glyphs[s->area];
25260 int first = s->first_glyph - glyphs;
25262 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25263 x -= glyphs[i].pixel_width;
25265 k = i + 1;
25267 else
25268 k = -1;
25270 return k;
25274 /* Return the index of the first glyph preceding glyph string S that
25275 is overwriting S because of its right overhang. Value is -1 if no
25276 glyph in front of S overwrites S. */
25278 static int
25279 left_overwriting (struct glyph_string *s)
25281 int i, k, x;
25282 struct glyph *glyphs = s->row->glyphs[s->area];
25283 int first = s->first_glyph - glyphs;
25285 k = -1;
25286 x = 0;
25287 for (i = first - 1; i >= 0; --i)
25289 int left, right;
25290 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25291 if (x + right > 0)
25292 k = i;
25293 x -= glyphs[i].pixel_width;
25296 return k;
25300 /* Return the index of the last glyph following glyph string S that is
25301 overwritten by S because of S's right overhang. Value is -1 if
25302 no such glyph is found. */
25304 static int
25305 right_overwritten (struct glyph_string *s)
25307 int k = -1;
25309 if (s->right_overhang)
25311 int x = 0, i;
25312 struct glyph *glyphs = s->row->glyphs[s->area];
25313 int first = (s->first_glyph - glyphs
25314 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25315 int end = s->row->used[s->area];
25317 for (i = first; i < end && s->right_overhang > x; ++i)
25318 x += glyphs[i].pixel_width;
25320 k = i;
25323 return k;
25327 /* Return the index of the last glyph following glyph string S that
25328 overwrites S because of its left overhang. Value is negative
25329 if no such glyph is found. */
25331 static int
25332 right_overwriting (struct glyph_string *s)
25334 int i, k, x;
25335 int end = s->row->used[s->area];
25336 struct glyph *glyphs = s->row->glyphs[s->area];
25337 int first = (s->first_glyph - glyphs
25338 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25340 k = -1;
25341 x = 0;
25342 for (i = first; i < end; ++i)
25344 int left, right;
25345 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25346 if (x - left < 0)
25347 k = i;
25348 x += glyphs[i].pixel_width;
25351 return k;
25355 /* Set background width of glyph string S. START is the index of the
25356 first glyph following S. LAST_X is the right-most x-position + 1
25357 in the drawing area. */
25359 static void
25360 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25362 /* If the face of this glyph string has to be drawn to the end of
25363 the drawing area, set S->extends_to_end_of_line_p. */
25365 if (start == s->row->used[s->area]
25366 && ((s->row->fill_line_p
25367 && (s->hl == DRAW_NORMAL_TEXT
25368 || s->hl == DRAW_IMAGE_RAISED
25369 || s->hl == DRAW_IMAGE_SUNKEN))
25370 || s->hl == DRAW_MOUSE_FACE))
25371 s->extends_to_end_of_line_p = true;
25373 /* If S extends its face to the end of the line, set its
25374 background_width to the distance to the right edge of the drawing
25375 area. */
25376 if (s->extends_to_end_of_line_p)
25377 s->background_width = last_x - s->x + 1;
25378 else
25379 s->background_width = s->width;
25383 /* Compute overhangs and x-positions for glyph string S and its
25384 predecessors, or successors. X is the starting x-position for S.
25385 BACKWARD_P means process predecessors. */
25387 static void
25388 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25390 if (backward_p)
25392 while (s)
25394 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25395 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25396 x -= s->width;
25397 s->x = x;
25398 s = s->prev;
25401 else
25403 while (s)
25405 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25406 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25407 s->x = x;
25408 x += s->width;
25409 s = s->next;
25416 /* The following macros are only called from draw_glyphs below.
25417 They reference the following parameters of that function directly:
25418 `w', `row', `area', and `overlap_p'
25419 as well as the following local variables:
25420 `s', `f', and `hdc' (in W32) */
25422 #ifdef HAVE_NTGUI
25423 /* On W32, silently add local `hdc' variable to argument list of
25424 init_glyph_string. */
25425 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25426 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25427 #else
25428 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25429 init_glyph_string (s, char2b, w, row, area, start, hl)
25430 #endif
25432 /* Add a glyph string for a stretch glyph to the list of strings
25433 between HEAD and TAIL. START is the index of the stretch glyph in
25434 row area AREA of glyph row ROW. END is the index of the last glyph
25435 in that glyph row area. X is the current output position assigned
25436 to the new glyph string constructed. HL overrides that face of the
25437 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25438 is the right-most x-position of the drawing area. */
25440 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25441 and below -- keep them on one line. */
25442 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25443 do \
25445 s = alloca (sizeof *s); \
25446 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25447 START = fill_stretch_glyph_string (s, START, END); \
25448 append_glyph_string (&HEAD, &TAIL, s); \
25449 s->x = (X); \
25451 while (false)
25454 /* Add a glyph string for an image glyph to the list of strings
25455 between HEAD and TAIL. START is the index of the image glyph in
25456 row area AREA of glyph row ROW. END is the index of the last glyph
25457 in that glyph row area. X is the current output position assigned
25458 to the new glyph string constructed. HL overrides that face of the
25459 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25460 is the right-most x-position of the drawing area. */
25462 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25463 do \
25465 s = alloca (sizeof *s); \
25466 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25467 fill_image_glyph_string (s); \
25468 append_glyph_string (&HEAD, &TAIL, s); \
25469 ++START; \
25470 s->x = (X); \
25472 while (false)
25474 #ifndef HAVE_XWIDGETS
25475 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25476 eassume (false)
25477 #else
25478 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25479 do \
25481 s = alloca (sizeof *s); \
25482 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25483 fill_xwidget_glyph_string (s); \
25484 append_glyph_string (&(HEAD), &(TAIL), s); \
25485 ++(START); \
25486 s->x = (X); \
25488 while (false)
25489 #endif
25491 /* Add a glyph string for a sequence of character glyphs to the list
25492 of strings between HEAD and TAIL. START is the index of the first
25493 glyph in row area AREA of glyph row ROW that is part of the new
25494 glyph string. END is the index of the last glyph in that glyph row
25495 area. X is the current output position assigned to the new glyph
25496 string constructed. HL overrides that face of the glyph; e.g. it
25497 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25498 right-most x-position of the drawing area. */
25500 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25501 do \
25503 int face_id; \
25504 XChar2b *char2b; \
25506 face_id = (row)->glyphs[area][START].face_id; \
25508 s = alloca (sizeof *s); \
25509 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25510 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25511 append_glyph_string (&HEAD, &TAIL, s); \
25512 s->x = (X); \
25513 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25515 while (false)
25518 /* Add a glyph string for a composite sequence to the list of strings
25519 between HEAD and TAIL. START is the index of the first glyph in
25520 row area AREA of glyph row ROW that is part of the new glyph
25521 string. END is the index of the last glyph in that glyph row area.
25522 X is the current output position assigned to the new glyph string
25523 constructed. HL overrides that face of the glyph; e.g. it is
25524 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25525 x-position of the drawing area. */
25527 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25528 do { \
25529 int face_id = (row)->glyphs[area][START].face_id; \
25530 struct face *base_face = FACE_FROM_ID (f, face_id); \
25531 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25532 struct composition *cmp = composition_table[cmp_id]; \
25533 XChar2b *char2b; \
25534 struct glyph_string *first_s = NULL; \
25535 int n; \
25537 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25539 /* Make glyph_strings for each glyph sequence that is drawable by \
25540 the same face, and append them to HEAD/TAIL. */ \
25541 for (n = 0; n < cmp->glyph_len;) \
25543 s = alloca (sizeof *s); \
25544 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25545 append_glyph_string (&(HEAD), &(TAIL), s); \
25546 s->cmp = cmp; \
25547 s->cmp_from = n; \
25548 s->x = (X); \
25549 if (n == 0) \
25550 first_s = s; \
25551 n = fill_composite_glyph_string (s, base_face, overlaps); \
25554 ++START; \
25555 s = first_s; \
25556 } while (false)
25559 /* Add a glyph string for a glyph-string sequence to the list of strings
25560 between HEAD and TAIL. */
25562 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25563 do { \
25564 int face_id; \
25565 XChar2b *char2b; \
25566 Lisp_Object gstring; \
25568 face_id = (row)->glyphs[area][START].face_id; \
25569 gstring = (composition_gstring_from_id \
25570 ((row)->glyphs[area][START].u.cmp.id)); \
25571 s = alloca (sizeof *s); \
25572 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25573 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25574 append_glyph_string (&(HEAD), &(TAIL), s); \
25575 s->x = (X); \
25576 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25577 } while (false)
25580 /* Add a glyph string for a sequence of glyphless character's glyphs
25581 to the list of strings between HEAD and TAIL. The meanings of
25582 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25584 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25585 do \
25587 int face_id; \
25589 face_id = (row)->glyphs[area][START].face_id; \
25591 s = alloca (sizeof *s); \
25592 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25593 append_glyph_string (&HEAD, &TAIL, s); \
25594 s->x = (X); \
25595 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25596 overlaps); \
25598 while (false)
25601 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25602 of AREA of glyph row ROW on window W between indices START and END.
25603 HL overrides the face for drawing glyph strings, e.g. it is
25604 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25605 x-positions of the drawing area.
25607 This is an ugly monster macro construct because we must use alloca
25608 to allocate glyph strings (because draw_glyphs can be called
25609 asynchronously). */
25611 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25612 do \
25614 HEAD = TAIL = NULL; \
25615 while (START < END) \
25617 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25618 switch (first_glyph->type) \
25620 case CHAR_GLYPH: \
25621 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25622 HL, X, LAST_X); \
25623 break; \
25625 case COMPOSITE_GLYPH: \
25626 if (first_glyph->u.cmp.automatic) \
25627 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25628 HL, X, LAST_X); \
25629 else \
25630 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25631 HL, X, LAST_X); \
25632 break; \
25634 case STRETCH_GLYPH: \
25635 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25636 HL, X, LAST_X); \
25637 break; \
25639 case IMAGE_GLYPH: \
25640 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25641 HL, X, LAST_X); \
25642 break;
25644 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25645 case XWIDGET_GLYPH: \
25646 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25647 HL, X, LAST_X); \
25648 break;
25650 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25651 case GLYPHLESS_GLYPH: \
25652 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25653 HL, X, LAST_X); \
25654 break; \
25656 default: \
25657 emacs_abort (); \
25660 if (s) \
25662 set_glyph_string_background_width (s, START, LAST_X); \
25663 (X) += s->width; \
25666 } while (false)
25669 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25670 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25671 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25672 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25675 /* Draw glyphs between START and END in AREA of ROW on window W,
25676 starting at x-position X. X is relative to AREA in W. HL is a
25677 face-override with the following meaning:
25679 DRAW_NORMAL_TEXT draw normally
25680 DRAW_CURSOR draw in cursor face
25681 DRAW_MOUSE_FACE draw in mouse face.
25682 DRAW_INVERSE_VIDEO draw in mode line face
25683 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25684 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25686 If OVERLAPS is non-zero, draw only the foreground of characters and
25687 clip to the physical height of ROW. Non-zero value also defines
25688 the overlapping part to be drawn:
25690 OVERLAPS_PRED overlap with preceding rows
25691 OVERLAPS_SUCC overlap with succeeding rows
25692 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25693 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25695 Value is the x-position reached, relative to AREA of W. */
25697 static int
25698 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25699 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25700 enum draw_glyphs_face hl, int overlaps)
25702 struct glyph_string *head, *tail;
25703 struct glyph_string *s;
25704 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25705 int i, j, x_reached, last_x, area_left = 0;
25706 struct frame *f = XFRAME (WINDOW_FRAME (w));
25707 DECLARE_HDC (hdc);
25709 ALLOCATE_HDC (hdc, f);
25711 /* Let's rather be paranoid than getting a SEGV. */
25712 end = min (end, row->used[area]);
25713 start = clip_to_bounds (0, start, end);
25715 /* Translate X to frame coordinates. Set last_x to the right
25716 end of the drawing area. */
25717 if (row->full_width_p)
25719 /* X is relative to the left edge of W, without scroll bars
25720 or fringes. */
25721 area_left = WINDOW_LEFT_EDGE_X (w);
25722 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25723 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25725 else
25727 area_left = window_box_left (w, area);
25728 last_x = area_left + window_box_width (w, area);
25730 x += area_left;
25732 /* Build a doubly-linked list of glyph_string structures between
25733 head and tail from what we have to draw. Note that the macro
25734 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25735 the reason we use a separate variable `i'. */
25736 i = start;
25737 USE_SAFE_ALLOCA;
25738 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25739 if (tail)
25740 x_reached = tail->x + tail->background_width;
25741 else
25742 x_reached = x;
25744 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25745 the row, redraw some glyphs in front or following the glyph
25746 strings built above. */
25747 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25749 struct glyph_string *h, *t;
25750 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25751 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25752 bool check_mouse_face = false;
25753 int dummy_x = 0;
25755 /* If mouse highlighting is on, we may need to draw adjacent
25756 glyphs using mouse-face highlighting. */
25757 if (area == TEXT_AREA && row->mouse_face_p
25758 && hlinfo->mouse_face_beg_row >= 0
25759 && hlinfo->mouse_face_end_row >= 0)
25761 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25763 if (row_vpos >= hlinfo->mouse_face_beg_row
25764 && row_vpos <= hlinfo->mouse_face_end_row)
25766 check_mouse_face = true;
25767 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25768 ? hlinfo->mouse_face_beg_col : 0;
25769 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25770 ? hlinfo->mouse_face_end_col
25771 : row->used[TEXT_AREA];
25775 /* Compute overhangs for all glyph strings. */
25776 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25777 for (s = head; s; s = s->next)
25778 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25780 /* Prepend glyph strings for glyphs in front of the first glyph
25781 string that are overwritten because of the first glyph
25782 string's left overhang. The background of all strings
25783 prepended must be drawn because the first glyph string
25784 draws over it. */
25785 i = left_overwritten (head);
25786 if (i >= 0)
25788 enum draw_glyphs_face overlap_hl;
25790 /* If this row contains mouse highlighting, attempt to draw
25791 the overlapped glyphs with the correct highlight. This
25792 code fails if the overlap encompasses more than one glyph
25793 and mouse-highlight spans only some of these glyphs.
25794 However, making it work perfectly involves a lot more
25795 code, and I don't know if the pathological case occurs in
25796 practice, so we'll stick to this for now. --- cyd */
25797 if (check_mouse_face
25798 && mouse_beg_col < start && mouse_end_col > i)
25799 overlap_hl = DRAW_MOUSE_FACE;
25800 else
25801 overlap_hl = DRAW_NORMAL_TEXT;
25803 if (hl != overlap_hl)
25804 clip_head = head;
25805 j = i;
25806 BUILD_GLYPH_STRINGS (j, start, h, t,
25807 overlap_hl, dummy_x, last_x);
25808 start = i;
25809 compute_overhangs_and_x (t, head->x, true);
25810 prepend_glyph_string_lists (&head, &tail, h, t);
25811 if (clip_head == NULL)
25812 clip_head = head;
25815 /* Prepend glyph strings for glyphs in front of the first glyph
25816 string that overwrite that glyph string because of their
25817 right overhang. For these strings, only the foreground must
25818 be drawn, because it draws over the glyph string at `head'.
25819 The background must not be drawn because this would overwrite
25820 right overhangs of preceding glyphs for which no glyph
25821 strings exist. */
25822 i = left_overwriting (head);
25823 if (i >= 0)
25825 enum draw_glyphs_face overlap_hl;
25827 if (check_mouse_face
25828 && mouse_beg_col < start && mouse_end_col > i)
25829 overlap_hl = DRAW_MOUSE_FACE;
25830 else
25831 overlap_hl = DRAW_NORMAL_TEXT;
25833 if (hl == overlap_hl || clip_head == NULL)
25834 clip_head = head;
25835 BUILD_GLYPH_STRINGS (i, start, h, t,
25836 overlap_hl, dummy_x, last_x);
25837 for (s = h; s; s = s->next)
25838 s->background_filled_p = true;
25839 compute_overhangs_and_x (t, head->x, true);
25840 prepend_glyph_string_lists (&head, &tail, h, t);
25843 /* Append glyphs strings for glyphs following the last glyph
25844 string tail that are overwritten by tail. The background of
25845 these strings has to be drawn because tail's foreground draws
25846 over it. */
25847 i = right_overwritten (tail);
25848 if (i >= 0)
25850 enum draw_glyphs_face overlap_hl;
25852 if (check_mouse_face
25853 && mouse_beg_col < i && mouse_end_col > end)
25854 overlap_hl = DRAW_MOUSE_FACE;
25855 else
25856 overlap_hl = DRAW_NORMAL_TEXT;
25858 if (hl != overlap_hl)
25859 clip_tail = tail;
25860 BUILD_GLYPH_STRINGS (end, i, h, t,
25861 overlap_hl, x, last_x);
25862 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25863 we don't have `end = i;' here. */
25864 compute_overhangs_and_x (h, tail->x + tail->width, false);
25865 append_glyph_string_lists (&head, &tail, h, t);
25866 if (clip_tail == NULL)
25867 clip_tail = tail;
25870 /* Append glyph strings for glyphs following the last glyph
25871 string tail that overwrite tail. The foreground of such
25872 glyphs has to be drawn because it writes into the background
25873 of tail. The background must not be drawn because it could
25874 paint over the foreground of following glyphs. */
25875 i = right_overwriting (tail);
25876 if (i >= 0)
25878 enum draw_glyphs_face overlap_hl;
25879 if (check_mouse_face
25880 && mouse_beg_col < i && mouse_end_col > end)
25881 overlap_hl = DRAW_MOUSE_FACE;
25882 else
25883 overlap_hl = DRAW_NORMAL_TEXT;
25885 if (hl == overlap_hl || clip_tail == NULL)
25886 clip_tail = tail;
25887 i++; /* We must include the Ith glyph. */
25888 BUILD_GLYPH_STRINGS (end, i, h, t,
25889 overlap_hl, x, last_x);
25890 for (s = h; s; s = s->next)
25891 s->background_filled_p = true;
25892 compute_overhangs_and_x (h, tail->x + tail->width, false);
25893 append_glyph_string_lists (&head, &tail, h, t);
25895 if (clip_head || clip_tail)
25896 for (s = head; s; s = s->next)
25898 s->clip_head = clip_head;
25899 s->clip_tail = clip_tail;
25903 /* Draw all strings. */
25904 for (s = head; s; s = s->next)
25905 FRAME_RIF (f)->draw_glyph_string (s);
25907 #ifndef HAVE_NS
25908 /* When focus a sole frame and move horizontally, this clears on_p
25909 causing a failure to erase prev cursor position. */
25910 if (area == TEXT_AREA
25911 && !row->full_width_p
25912 /* When drawing overlapping rows, only the glyph strings'
25913 foreground is drawn, which doesn't erase a cursor
25914 completely. */
25915 && !overlaps)
25917 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25918 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25919 : (tail ? tail->x + tail->background_width : x));
25920 x0 -= area_left;
25921 x1 -= area_left;
25923 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25924 row->y, MATRIX_ROW_BOTTOM_Y (row));
25926 #endif
25928 /* Value is the x-position up to which drawn, relative to AREA of W.
25929 This doesn't include parts drawn because of overhangs. */
25930 if (row->full_width_p)
25931 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25932 else
25933 x_reached -= area_left;
25935 RELEASE_HDC (hdc, f);
25937 SAFE_FREE ();
25938 return x_reached;
25941 /* Expand row matrix if too narrow. Don't expand if area
25942 is not present. */
25944 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25946 if (!it->f->fonts_changed \
25947 && (it->glyph_row->glyphs[area] \
25948 < it->glyph_row->glyphs[area + 1])) \
25950 it->w->ncols_scale_factor++; \
25951 it->f->fonts_changed = true; \
25955 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25956 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25958 static void
25959 append_glyph (struct it *it)
25961 struct glyph *glyph;
25962 enum glyph_row_area area = it->area;
25964 eassert (it->glyph_row);
25965 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25967 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25968 if (glyph < it->glyph_row->glyphs[area + 1])
25970 /* If the glyph row is reversed, we need to prepend the glyph
25971 rather than append it. */
25972 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25974 struct glyph *g;
25976 /* Make room for the additional glyph. */
25977 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25978 g[1] = *g;
25979 glyph = it->glyph_row->glyphs[area];
25981 glyph->charpos = CHARPOS (it->position);
25982 glyph->object = it->object;
25983 if (it->pixel_width > 0)
25985 eassert (it->pixel_width <= SHRT_MAX);
25986 glyph->pixel_width = it->pixel_width;
25987 glyph->padding_p = false;
25989 else
25991 /* Assure at least 1-pixel width. Otherwise, cursor can't
25992 be displayed correctly. */
25993 glyph->pixel_width = 1;
25994 glyph->padding_p = true;
25996 glyph->ascent = it->ascent;
25997 glyph->descent = it->descent;
25998 glyph->voffset = it->voffset;
25999 glyph->type = CHAR_GLYPH;
26000 glyph->avoid_cursor_p = it->avoid_cursor_p;
26001 glyph->multibyte_p = it->multibyte_p;
26002 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26004 /* In R2L rows, the left and the right box edges need to be
26005 drawn in reverse direction. */
26006 glyph->right_box_line_p = it->start_of_box_run_p;
26007 glyph->left_box_line_p = it->end_of_box_run_p;
26009 else
26011 glyph->left_box_line_p = it->start_of_box_run_p;
26012 glyph->right_box_line_p = it->end_of_box_run_p;
26014 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26015 || it->phys_descent > it->descent);
26016 glyph->glyph_not_available_p = it->glyph_not_available_p;
26017 glyph->face_id = it->face_id;
26018 glyph->u.ch = it->char_to_display;
26019 glyph->slice.img = null_glyph_slice;
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 else
26029 glyph->resolved_level = 0;
26030 glyph->bidi_type = UNKNOWN_BT;
26032 ++it->glyph_row->used[area];
26034 else
26035 IT_EXPAND_MATRIX_WIDTH (it, area);
26038 /* Store one glyph for the composition IT->cmp_it.id in
26039 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26040 non-null. */
26042 static void
26043 append_composite_glyph (struct it *it)
26045 struct glyph *glyph;
26046 enum glyph_row_area area = it->area;
26048 eassert (it->glyph_row);
26050 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26051 if (glyph < it->glyph_row->glyphs[area + 1])
26053 /* If the glyph row is reversed, we need to prepend the glyph
26054 rather than append it. */
26055 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26057 struct glyph *g;
26059 /* Make room for the new glyph. */
26060 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26061 g[1] = *g;
26062 glyph = it->glyph_row->glyphs[it->area];
26064 glyph->charpos = it->cmp_it.charpos;
26065 glyph->object = it->object;
26066 eassert (it->pixel_width <= SHRT_MAX);
26067 glyph->pixel_width = it->pixel_width;
26068 glyph->ascent = it->ascent;
26069 glyph->descent = it->descent;
26070 glyph->voffset = it->voffset;
26071 glyph->type = COMPOSITE_GLYPH;
26072 if (it->cmp_it.ch < 0)
26074 glyph->u.cmp.automatic = false;
26075 glyph->u.cmp.id = it->cmp_it.id;
26076 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26078 else
26080 glyph->u.cmp.automatic = true;
26081 glyph->u.cmp.id = it->cmp_it.id;
26082 glyph->slice.cmp.from = it->cmp_it.from;
26083 glyph->slice.cmp.to = it->cmp_it.to - 1;
26085 glyph->avoid_cursor_p = it->avoid_cursor_p;
26086 glyph->multibyte_p = it->multibyte_p;
26087 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26089 /* In R2L rows, the left and the right box edges need to be
26090 drawn in reverse direction. */
26091 glyph->right_box_line_p = it->start_of_box_run_p;
26092 glyph->left_box_line_p = it->end_of_box_run_p;
26094 else
26096 glyph->left_box_line_p = it->start_of_box_run_p;
26097 glyph->right_box_line_p = it->end_of_box_run_p;
26099 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26100 || it->phys_descent > it->descent);
26101 glyph->padding_p = false;
26102 glyph->glyph_not_available_p = false;
26103 glyph->face_id = it->face_id;
26104 glyph->font_type = FONT_TYPE_UNKNOWN;
26105 if (it->bidi_p)
26107 glyph->resolved_level = it->bidi_it.resolved_level;
26108 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26109 glyph->bidi_type = it->bidi_it.type;
26111 ++it->glyph_row->used[area];
26113 else
26114 IT_EXPAND_MATRIX_WIDTH (it, area);
26118 /* Change IT->ascent and IT->height according to the setting of
26119 IT->voffset. */
26121 static void
26122 take_vertical_position_into_account (struct it *it)
26124 if (it->voffset)
26126 if (it->voffset < 0)
26127 /* Increase the ascent so that we can display the text higher
26128 in the line. */
26129 it->ascent -= it->voffset;
26130 else
26131 /* Increase the descent so that we can display the text lower
26132 in the line. */
26133 it->descent += it->voffset;
26138 /* Produce glyphs/get display metrics for the image IT is loaded with.
26139 See the description of struct display_iterator in dispextern.h for
26140 an overview of struct display_iterator. */
26142 static void
26143 produce_image_glyph (struct it *it)
26145 struct image *img;
26146 struct face *face;
26147 int glyph_ascent, crop;
26148 struct glyph_slice slice;
26150 eassert (it->what == IT_IMAGE);
26152 face = FACE_FROM_ID (it->f, it->face_id);
26153 /* Make sure X resources of the face is loaded. */
26154 prepare_face_for_display (it->f, face);
26156 if (it->image_id < 0)
26158 /* Fringe bitmap. */
26159 it->ascent = it->phys_ascent = 0;
26160 it->descent = it->phys_descent = 0;
26161 it->pixel_width = 0;
26162 it->nglyphs = 0;
26163 return;
26166 img = IMAGE_FROM_ID (it->f, it->image_id);
26167 /* Make sure X resources of the image is loaded. */
26168 prepare_image_for_display (it->f, img);
26170 slice.x = slice.y = 0;
26171 slice.width = img->width;
26172 slice.height = img->height;
26174 if (INTEGERP (it->slice.x))
26175 slice.x = XINT (it->slice.x);
26176 else if (FLOATP (it->slice.x))
26177 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26179 if (INTEGERP (it->slice.y))
26180 slice.y = XINT (it->slice.y);
26181 else if (FLOATP (it->slice.y))
26182 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26184 if (INTEGERP (it->slice.width))
26185 slice.width = XINT (it->slice.width);
26186 else if (FLOATP (it->slice.width))
26187 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26189 if (INTEGERP (it->slice.height))
26190 slice.height = XINT (it->slice.height);
26191 else if (FLOATP (it->slice.height))
26192 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26194 if (slice.x >= img->width)
26195 slice.x = img->width;
26196 if (slice.y >= img->height)
26197 slice.y = img->height;
26198 if (slice.x + slice.width >= img->width)
26199 slice.width = img->width - slice.x;
26200 if (slice.y + slice.height > img->height)
26201 slice.height = img->height - slice.y;
26203 if (slice.width == 0 || slice.height == 0)
26204 return;
26206 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26208 it->descent = slice.height - glyph_ascent;
26209 if (slice.y == 0)
26210 it->descent += img->vmargin;
26211 if (slice.y + slice.height == img->height)
26212 it->descent += img->vmargin;
26213 it->phys_descent = it->descent;
26215 it->pixel_width = slice.width;
26216 if (slice.x == 0)
26217 it->pixel_width += img->hmargin;
26218 if (slice.x + slice.width == img->width)
26219 it->pixel_width += img->hmargin;
26221 /* It's quite possible for images to have an ascent greater than
26222 their height, so don't get confused in that case. */
26223 if (it->descent < 0)
26224 it->descent = 0;
26226 it->nglyphs = 1;
26228 if (face->box != FACE_NO_BOX)
26230 if (face->box_line_width > 0)
26232 if (slice.y == 0)
26233 it->ascent += face->box_line_width;
26234 if (slice.y + slice.height == img->height)
26235 it->descent += face->box_line_width;
26238 if (it->start_of_box_run_p && slice.x == 0)
26239 it->pixel_width += eabs (face->box_line_width);
26240 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26241 it->pixel_width += eabs (face->box_line_width);
26244 take_vertical_position_into_account (it);
26246 /* Automatically crop wide image glyphs at right edge so we can
26247 draw the cursor on same display row. */
26248 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26249 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26251 it->pixel_width -= crop;
26252 slice.width -= crop;
26255 if (it->glyph_row)
26257 struct glyph *glyph;
26258 enum glyph_row_area area = it->area;
26260 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26261 if (it->glyph_row->reversed_p)
26263 struct glyph *g;
26265 /* Make room for the new glyph. */
26266 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26267 g[1] = *g;
26268 glyph = it->glyph_row->glyphs[it->area];
26270 if (glyph < it->glyph_row->glyphs[area + 1])
26272 glyph->charpos = CHARPOS (it->position);
26273 glyph->object = it->object;
26274 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26275 glyph->ascent = glyph_ascent;
26276 glyph->descent = it->descent;
26277 glyph->voffset = it->voffset;
26278 glyph->type = IMAGE_GLYPH;
26279 glyph->avoid_cursor_p = it->avoid_cursor_p;
26280 glyph->multibyte_p = it->multibyte_p;
26281 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26283 /* In R2L rows, the left and the right box edges need to be
26284 drawn in reverse direction. */
26285 glyph->right_box_line_p = it->start_of_box_run_p;
26286 glyph->left_box_line_p = it->end_of_box_run_p;
26288 else
26290 glyph->left_box_line_p = it->start_of_box_run_p;
26291 glyph->right_box_line_p = it->end_of_box_run_p;
26293 glyph->overlaps_vertically_p = false;
26294 glyph->padding_p = false;
26295 glyph->glyph_not_available_p = false;
26296 glyph->face_id = it->face_id;
26297 glyph->u.img_id = img->id;
26298 glyph->slice.img = slice;
26299 glyph->font_type = FONT_TYPE_UNKNOWN;
26300 if (it->bidi_p)
26302 glyph->resolved_level = it->bidi_it.resolved_level;
26303 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26304 glyph->bidi_type = it->bidi_it.type;
26306 ++it->glyph_row->used[area];
26308 else
26309 IT_EXPAND_MATRIX_WIDTH (it, area);
26313 static void
26314 produce_xwidget_glyph (struct it *it)
26316 #ifdef HAVE_XWIDGETS
26317 struct xwidget *xw;
26318 int glyph_ascent, crop;
26319 eassert (it->what == IT_XWIDGET);
26321 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26322 /* Make sure X resources of the face is loaded. */
26323 prepare_face_for_display (it->f, face);
26325 xw = it->xwidget;
26326 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26327 it->descent = xw->height/2;
26328 it->phys_descent = it->descent;
26329 it->pixel_width = xw->width;
26330 /* It's quite possible for images to have an ascent greater than
26331 their height, so don't get confused in that case. */
26332 if (it->descent < 0)
26333 it->descent = 0;
26335 it->nglyphs = 1;
26337 if (face->box != FACE_NO_BOX)
26339 if (face->box_line_width > 0)
26341 it->ascent += face->box_line_width;
26342 it->descent += face->box_line_width;
26345 if (it->start_of_box_run_p)
26346 it->pixel_width += eabs (face->box_line_width);
26347 it->pixel_width += eabs (face->box_line_width);
26350 take_vertical_position_into_account (it);
26352 /* Automatically crop wide image glyphs at right edge so we can
26353 draw the cursor on same display row. */
26354 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26355 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26356 it->pixel_width -= crop;
26358 if (it->glyph_row)
26360 enum glyph_row_area area = it->area;
26361 struct glyph *glyph
26362 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26364 if (it->glyph_row->reversed_p)
26366 struct glyph *g;
26368 /* Make room for the new glyph. */
26369 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26370 g[1] = *g;
26371 glyph = it->glyph_row->glyphs[it->area];
26373 if (glyph < it->glyph_row->glyphs[area + 1])
26375 glyph->charpos = CHARPOS (it->position);
26376 glyph->object = it->object;
26377 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26378 glyph->ascent = glyph_ascent;
26379 glyph->descent = it->descent;
26380 glyph->voffset = it->voffset;
26381 glyph->type = XWIDGET_GLYPH;
26382 glyph->avoid_cursor_p = it->avoid_cursor_p;
26383 glyph->multibyte_p = it->multibyte_p;
26384 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26386 /* In R2L rows, the left and the right box edges need to be
26387 drawn in reverse direction. */
26388 glyph->right_box_line_p = it->start_of_box_run_p;
26389 glyph->left_box_line_p = it->end_of_box_run_p;
26391 else
26393 glyph->left_box_line_p = it->start_of_box_run_p;
26394 glyph->right_box_line_p = it->end_of_box_run_p;
26396 glyph->overlaps_vertically_p = 0;
26397 glyph->padding_p = 0;
26398 glyph->glyph_not_available_p = 0;
26399 glyph->face_id = it->face_id;
26400 glyph->u.xwidget = it->xwidget;
26401 glyph->font_type = FONT_TYPE_UNKNOWN;
26402 if (it->bidi_p)
26404 glyph->resolved_level = it->bidi_it.resolved_level;
26405 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26406 glyph->bidi_type = it->bidi_it.type;
26408 ++it->glyph_row->used[area];
26410 else
26411 IT_EXPAND_MATRIX_WIDTH (it, area);
26413 #endif
26416 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26417 of the glyph, WIDTH and HEIGHT are the width and height of the
26418 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26420 static void
26421 append_stretch_glyph (struct it *it, Lisp_Object object,
26422 int width, int height, int ascent)
26424 struct glyph *glyph;
26425 enum glyph_row_area area = it->area;
26427 eassert (ascent >= 0 && ascent <= height);
26429 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26430 if (glyph < it->glyph_row->glyphs[area + 1])
26432 /* If the glyph row is reversed, we need to prepend the glyph
26433 rather than append it. */
26434 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26436 struct glyph *g;
26438 /* Make room for the additional glyph. */
26439 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26440 g[1] = *g;
26441 glyph = it->glyph_row->glyphs[area];
26443 /* Decrease the width of the first glyph of the row that
26444 begins before first_visible_x (e.g., due to hscroll).
26445 This is so the overall width of the row becomes smaller
26446 by the scroll amount, and the stretch glyph appended by
26447 extend_face_to_end_of_line will be wider, to shift the
26448 row glyphs to the right. (In L2R rows, the corresponding
26449 left-shift effect is accomplished by setting row->x to a
26450 negative value, which won't work with R2L rows.)
26452 This must leave us with a positive value of WIDTH, since
26453 otherwise the call to move_it_in_display_line_to at the
26454 beginning of display_line would have got past the entire
26455 first glyph, and then it->current_x would have been
26456 greater or equal to it->first_visible_x. */
26457 if (it->current_x < it->first_visible_x)
26458 width -= it->first_visible_x - it->current_x;
26459 eassert (width > 0);
26461 glyph->charpos = CHARPOS (it->position);
26462 glyph->object = object;
26463 /* FIXME: It would be better to use TYPE_MAX here, but
26464 __typeof__ is not portable enough... */
26465 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26466 glyph->ascent = ascent;
26467 glyph->descent = height - ascent;
26468 glyph->voffset = it->voffset;
26469 glyph->type = STRETCH_GLYPH;
26470 glyph->avoid_cursor_p = it->avoid_cursor_p;
26471 glyph->multibyte_p = it->multibyte_p;
26472 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26474 /* In R2L rows, the left and the right box edges need to be
26475 drawn in reverse direction. */
26476 glyph->right_box_line_p = it->start_of_box_run_p;
26477 glyph->left_box_line_p = it->end_of_box_run_p;
26479 else
26481 glyph->left_box_line_p = it->start_of_box_run_p;
26482 glyph->right_box_line_p = it->end_of_box_run_p;
26484 glyph->overlaps_vertically_p = false;
26485 glyph->padding_p = false;
26486 glyph->glyph_not_available_p = false;
26487 glyph->face_id = it->face_id;
26488 glyph->u.stretch.ascent = ascent;
26489 glyph->u.stretch.height = height;
26490 glyph->slice.img = null_glyph_slice;
26491 glyph->font_type = FONT_TYPE_UNKNOWN;
26492 if (it->bidi_p)
26494 glyph->resolved_level = it->bidi_it.resolved_level;
26495 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26496 glyph->bidi_type = it->bidi_it.type;
26498 else
26500 glyph->resolved_level = 0;
26501 glyph->bidi_type = UNKNOWN_BT;
26503 ++it->glyph_row->used[area];
26505 else
26506 IT_EXPAND_MATRIX_WIDTH (it, area);
26509 #endif /* HAVE_WINDOW_SYSTEM */
26511 /* Produce a stretch glyph for iterator IT. IT->object is the value
26512 of the glyph property displayed. The value must be a list
26513 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26514 being recognized:
26516 1. `:width WIDTH' specifies that the space should be WIDTH *
26517 canonical char width wide. WIDTH may be an integer or floating
26518 point number.
26520 2. `:relative-width FACTOR' specifies that the width of the stretch
26521 should be computed from the width of the first character having the
26522 `glyph' property, and should be FACTOR times that width.
26524 3. `:align-to HPOS' specifies that the space should be wide enough
26525 to reach HPOS, a value in canonical character units.
26527 Exactly one of the above pairs must be present.
26529 4. `:height HEIGHT' specifies that the height of the stretch produced
26530 should be HEIGHT, measured in canonical character units.
26532 5. `:relative-height FACTOR' specifies that the height of the
26533 stretch should be FACTOR times the height of the characters having
26534 the glyph property.
26536 Either none or exactly one of 4 or 5 must be present.
26538 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26539 of the stretch should be used for the ascent of the stretch.
26540 ASCENT must be in the range 0 <= ASCENT <= 100. */
26542 void
26543 produce_stretch_glyph (struct it *it)
26545 /* (space :width WIDTH :height HEIGHT ...) */
26546 Lisp_Object prop, plist;
26547 int width = 0, height = 0, align_to = -1;
26548 bool zero_width_ok_p = false;
26549 double tem;
26550 struct font *font = NULL;
26552 #ifdef HAVE_WINDOW_SYSTEM
26553 int ascent = 0;
26554 bool zero_height_ok_p = false;
26556 if (FRAME_WINDOW_P (it->f))
26558 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26559 font = face->font ? face->font : FRAME_FONT (it->f);
26560 prepare_face_for_display (it->f, face);
26562 #endif
26564 /* List should start with `space'. */
26565 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26566 plist = XCDR (it->object);
26568 /* Compute the width of the stretch. */
26569 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26570 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26572 /* Absolute width `:width WIDTH' specified and valid. */
26573 zero_width_ok_p = true;
26574 width = (int)tem;
26576 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26578 /* Relative width `:relative-width FACTOR' specified and valid.
26579 Compute the width of the characters having the `glyph'
26580 property. */
26581 struct it it2;
26582 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26584 it2 = *it;
26585 if (it->multibyte_p)
26586 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26587 else
26589 it2.c = it2.char_to_display = *p, it2.len = 1;
26590 if (! ASCII_CHAR_P (it2.c))
26591 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26594 it2.glyph_row = NULL;
26595 it2.what = IT_CHARACTER;
26596 PRODUCE_GLYPHS (&it2);
26597 width = NUMVAL (prop) * it2.pixel_width;
26599 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26600 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26601 &align_to))
26603 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26604 align_to = (align_to < 0
26606 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26607 else if (align_to < 0)
26608 align_to = window_box_left_offset (it->w, TEXT_AREA);
26609 width = max (0, (int)tem + align_to - it->current_x);
26610 zero_width_ok_p = true;
26612 else
26613 /* Nothing specified -> width defaults to canonical char width. */
26614 width = FRAME_COLUMN_WIDTH (it->f);
26616 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26617 width = 1;
26619 #ifdef HAVE_WINDOW_SYSTEM
26620 /* Compute height. */
26621 if (FRAME_WINDOW_P (it->f))
26623 int default_height = normal_char_height (font, ' ');
26625 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26626 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26628 height = (int)tem;
26629 zero_height_ok_p = true;
26631 else if (prop = Fplist_get (plist, QCrelative_height),
26632 NUMVAL (prop) > 0)
26633 height = default_height * NUMVAL (prop);
26634 else
26635 height = default_height;
26637 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26638 height = 1;
26640 /* Compute percentage of height used for ascent. If
26641 `:ascent ASCENT' is present and valid, use that. Otherwise,
26642 derive the ascent from the font in use. */
26643 if (prop = Fplist_get (plist, QCascent),
26644 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26645 ascent = height * NUMVAL (prop) / 100.0;
26646 else if (!NILP (prop)
26647 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26648 ascent = min (max (0, (int)tem), height);
26649 else
26650 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26652 else
26653 #endif /* HAVE_WINDOW_SYSTEM */
26654 height = 1;
26656 if (width > 0 && it->line_wrap != TRUNCATE
26657 && it->current_x + width > it->last_visible_x)
26659 width = it->last_visible_x - it->current_x;
26660 #ifdef HAVE_WINDOW_SYSTEM
26661 /* Subtract one more pixel from the stretch width, but only on
26662 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26663 width -= FRAME_WINDOW_P (it->f);
26664 #endif
26667 if (width > 0 && height > 0 && it->glyph_row)
26669 Lisp_Object o_object = it->object;
26670 Lisp_Object object = it->stack[it->sp - 1].string;
26671 int n = width;
26673 if (!STRINGP (object))
26674 object = it->w->contents;
26675 #ifdef HAVE_WINDOW_SYSTEM
26676 if (FRAME_WINDOW_P (it->f))
26677 append_stretch_glyph (it, object, width, height, ascent);
26678 else
26679 #endif
26681 it->object = object;
26682 it->char_to_display = ' ';
26683 it->pixel_width = it->len = 1;
26684 while (n--)
26685 tty_append_glyph (it);
26686 it->object = o_object;
26690 it->pixel_width = width;
26691 #ifdef HAVE_WINDOW_SYSTEM
26692 if (FRAME_WINDOW_P (it->f))
26694 it->ascent = it->phys_ascent = ascent;
26695 it->descent = it->phys_descent = height - it->ascent;
26696 it->nglyphs = width > 0 && height > 0;
26697 take_vertical_position_into_account (it);
26699 else
26700 #endif
26701 it->nglyphs = width;
26704 /* Get information about special display element WHAT in an
26705 environment described by IT. WHAT is one of IT_TRUNCATION or
26706 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26707 non-null glyph_row member. This function ensures that fields like
26708 face_id, c, len of IT are left untouched. */
26710 static void
26711 produce_special_glyphs (struct it *it, enum display_element_type what)
26713 struct it temp_it;
26714 Lisp_Object gc;
26715 GLYPH glyph;
26717 temp_it = *it;
26718 temp_it.object = Qnil;
26719 memset (&temp_it.current, 0, sizeof temp_it.current);
26721 if (what == IT_CONTINUATION)
26723 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26724 if (it->bidi_it.paragraph_dir == R2L)
26725 SET_GLYPH_FROM_CHAR (glyph, '/');
26726 else
26727 SET_GLYPH_FROM_CHAR (glyph, '\\');
26728 if (it->dp
26729 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26731 /* FIXME: Should we mirror GC for R2L lines? */
26732 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26733 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26736 else if (what == IT_TRUNCATION)
26738 /* Truncation glyph. */
26739 SET_GLYPH_FROM_CHAR (glyph, '$');
26740 if (it->dp
26741 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26743 /* FIXME: Should we mirror GC for R2L lines? */
26744 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26745 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26748 else
26749 emacs_abort ();
26751 #ifdef HAVE_WINDOW_SYSTEM
26752 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26753 is turned off, we precede the truncation/continuation glyphs by a
26754 stretch glyph whose width is computed such that these special
26755 glyphs are aligned at the window margin, even when very different
26756 fonts are used in different glyph rows. */
26757 if (FRAME_WINDOW_P (temp_it.f)
26758 /* init_iterator calls this with it->glyph_row == NULL, and it
26759 wants only the pixel width of the truncation/continuation
26760 glyphs. */
26761 && temp_it.glyph_row
26762 /* insert_left_trunc_glyphs calls us at the beginning of the
26763 row, and it has its own calculation of the stretch glyph
26764 width. */
26765 && temp_it.glyph_row->used[TEXT_AREA] > 0
26766 && (temp_it.glyph_row->reversed_p
26767 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26768 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26770 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26772 if (stretch_width > 0)
26774 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26775 struct font *font =
26776 face->font ? face->font : FRAME_FONT (temp_it.f);
26777 int stretch_ascent =
26778 (((temp_it.ascent + temp_it.descent)
26779 * FONT_BASE (font)) / FONT_HEIGHT (font));
26781 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26782 temp_it.ascent + temp_it.descent,
26783 stretch_ascent);
26786 #endif
26788 temp_it.dp = NULL;
26789 temp_it.what = IT_CHARACTER;
26790 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26791 temp_it.face_id = GLYPH_FACE (glyph);
26792 temp_it.len = CHAR_BYTES (temp_it.c);
26794 PRODUCE_GLYPHS (&temp_it);
26795 it->pixel_width = temp_it.pixel_width;
26796 it->nglyphs = temp_it.nglyphs;
26799 #ifdef HAVE_WINDOW_SYSTEM
26801 /* Calculate line-height and line-spacing properties.
26802 An integer value specifies explicit pixel value.
26803 A float value specifies relative value to current face height.
26804 A cons (float . face-name) specifies relative value to
26805 height of specified face font.
26807 Returns height in pixels, or nil. */
26809 static Lisp_Object
26810 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26811 int boff, bool override)
26813 Lisp_Object face_name = Qnil;
26814 int ascent, descent, height;
26816 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26817 return val;
26819 if (CONSP (val))
26821 face_name = XCAR (val);
26822 val = XCDR (val);
26823 if (!NUMBERP (val))
26824 val = make_number (1);
26825 if (NILP (face_name))
26827 height = it->ascent + it->descent;
26828 goto scale;
26832 if (NILP (face_name))
26834 font = FRAME_FONT (it->f);
26835 boff = FRAME_BASELINE_OFFSET (it->f);
26837 else if (EQ (face_name, Qt))
26839 override = false;
26841 else
26843 int face_id;
26844 struct face *face;
26846 face_id = lookup_named_face (it->f, face_name, false);
26847 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26848 if (face == NULL || ((font = face->font) == NULL))
26849 return make_number (-1);
26850 boff = font->baseline_offset;
26851 if (font->vertical_centering)
26852 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26855 normal_char_ascent_descent (font, -1, &ascent, &descent);
26857 if (override)
26859 it->override_ascent = ascent;
26860 it->override_descent = descent;
26861 it->override_boff = boff;
26864 height = ascent + descent;
26866 scale:
26867 if (FLOATP (val))
26868 height = (int)(XFLOAT_DATA (val) * height);
26869 else if (INTEGERP (val))
26870 height *= XINT (val);
26872 return make_number (height);
26876 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26877 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26878 and only if this is for a character for which no font was found.
26880 If the display method (it->glyphless_method) is
26881 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26882 length of the acronym or the hexadecimal string, UPPER_XOFF and
26883 UPPER_YOFF are pixel offsets for the upper part of the string,
26884 LOWER_XOFF and LOWER_YOFF are for the lower part.
26886 For the other display methods, LEN through LOWER_YOFF are zero. */
26888 static void
26889 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26890 short upper_xoff, short upper_yoff,
26891 short lower_xoff, short lower_yoff)
26893 struct glyph *glyph;
26894 enum glyph_row_area area = it->area;
26896 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26897 if (glyph < it->glyph_row->glyphs[area + 1])
26899 /* If the glyph row is reversed, we need to prepend the glyph
26900 rather than append it. */
26901 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26903 struct glyph *g;
26905 /* Make room for the additional glyph. */
26906 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26907 g[1] = *g;
26908 glyph = it->glyph_row->glyphs[area];
26910 glyph->charpos = CHARPOS (it->position);
26911 glyph->object = it->object;
26912 eassert (it->pixel_width <= SHRT_MAX);
26913 glyph->pixel_width = it->pixel_width;
26914 glyph->ascent = it->ascent;
26915 glyph->descent = it->descent;
26916 glyph->voffset = it->voffset;
26917 glyph->type = GLYPHLESS_GLYPH;
26918 glyph->u.glyphless.method = it->glyphless_method;
26919 glyph->u.glyphless.for_no_font = for_no_font;
26920 glyph->u.glyphless.len = len;
26921 glyph->u.glyphless.ch = it->c;
26922 glyph->slice.glyphless.upper_xoff = upper_xoff;
26923 glyph->slice.glyphless.upper_yoff = upper_yoff;
26924 glyph->slice.glyphless.lower_xoff = lower_xoff;
26925 glyph->slice.glyphless.lower_yoff = lower_yoff;
26926 glyph->avoid_cursor_p = it->avoid_cursor_p;
26927 glyph->multibyte_p = it->multibyte_p;
26928 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26930 /* In R2L rows, the left and the right box edges need to be
26931 drawn in reverse direction. */
26932 glyph->right_box_line_p = it->start_of_box_run_p;
26933 glyph->left_box_line_p = it->end_of_box_run_p;
26935 else
26937 glyph->left_box_line_p = it->start_of_box_run_p;
26938 glyph->right_box_line_p = it->end_of_box_run_p;
26940 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26941 || it->phys_descent > it->descent);
26942 glyph->padding_p = false;
26943 glyph->glyph_not_available_p = false;
26944 glyph->face_id = face_id;
26945 glyph->font_type = FONT_TYPE_UNKNOWN;
26946 if (it->bidi_p)
26948 glyph->resolved_level = it->bidi_it.resolved_level;
26949 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26950 glyph->bidi_type = it->bidi_it.type;
26952 ++it->glyph_row->used[area];
26954 else
26955 IT_EXPAND_MATRIX_WIDTH (it, area);
26959 /* Produce a glyph for a glyphless character for iterator IT.
26960 IT->glyphless_method specifies which method to use for displaying
26961 the character. See the description of enum
26962 glyphless_display_method in dispextern.h for the detail.
26964 FOR_NO_FONT is true if and only if this is for a character for
26965 which no font was found. ACRONYM, if non-nil, is an acronym string
26966 for the character. */
26968 static void
26969 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26971 int face_id;
26972 struct face *face;
26973 struct font *font;
26974 int base_width, base_height, width, height;
26975 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26976 int len;
26978 /* Get the metrics of the base font. We always refer to the current
26979 ASCII face. */
26980 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26981 font = face->font ? face->font : FRAME_FONT (it->f);
26982 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26983 it->ascent += font->baseline_offset;
26984 it->descent -= font->baseline_offset;
26985 base_height = it->ascent + it->descent;
26986 base_width = font->average_width;
26988 face_id = merge_glyphless_glyph_face (it);
26990 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26992 it->pixel_width = THIN_SPACE_WIDTH;
26993 len = 0;
26994 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26996 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26998 width = CHARACTER_WIDTH (it->c);
26999 if (width == 0)
27000 width = 1;
27001 else if (width > 4)
27002 width = 4;
27003 it->pixel_width = base_width * width;
27004 len = 0;
27005 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27007 else
27009 char buf[7];
27010 const char *str;
27011 unsigned int code[6];
27012 int upper_len;
27013 int ascent, descent;
27014 struct font_metrics metrics_upper, metrics_lower;
27016 face = FACE_FROM_ID (it->f, face_id);
27017 font = face->font ? face->font : FRAME_FONT (it->f);
27018 prepare_face_for_display (it->f, face);
27020 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27022 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27023 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27024 if (CONSP (acronym))
27025 acronym = XCAR (acronym);
27026 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27028 else
27030 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27031 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27032 str = buf;
27034 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27035 code[len] = font->driver->encode_char (font, str[len]);
27036 upper_len = (len + 1) / 2;
27037 font->driver->text_extents (font, code, upper_len,
27038 &metrics_upper);
27039 font->driver->text_extents (font, code + upper_len, len - upper_len,
27040 &metrics_lower);
27044 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27045 width = max (metrics_upper.width, metrics_lower.width) + 4;
27046 upper_xoff = upper_yoff = 2; /* the typical case */
27047 if (base_width >= width)
27049 /* Align the upper to the left, the lower to the right. */
27050 it->pixel_width = base_width;
27051 lower_xoff = base_width - 2 - metrics_lower.width;
27053 else
27055 /* Center the shorter one. */
27056 it->pixel_width = width;
27057 if (metrics_upper.width >= metrics_lower.width)
27058 lower_xoff = (width - metrics_lower.width) / 2;
27059 else
27061 /* FIXME: This code doesn't look right. It formerly was
27062 missing the "lower_xoff = 0;", which couldn't have
27063 been right since it left lower_xoff uninitialized. */
27064 lower_xoff = 0;
27065 upper_xoff = (width - metrics_upper.width) / 2;
27069 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27070 top, bottom, and between upper and lower strings. */
27071 height = (metrics_upper.ascent + metrics_upper.descent
27072 + metrics_lower.ascent + metrics_lower.descent) + 5;
27073 /* Center vertically.
27074 H:base_height, D:base_descent
27075 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27077 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27078 descent = D - H/2 + h/2;
27079 lower_yoff = descent - 2 - ld;
27080 upper_yoff = lower_yoff - la - 1 - ud; */
27081 ascent = - (it->descent - (base_height + height + 1) / 2);
27082 descent = it->descent - (base_height - height) / 2;
27083 lower_yoff = descent - 2 - metrics_lower.descent;
27084 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27085 - metrics_upper.descent);
27086 /* Don't make the height shorter than the base height. */
27087 if (height > base_height)
27089 it->ascent = ascent;
27090 it->descent = descent;
27094 it->phys_ascent = it->ascent;
27095 it->phys_descent = it->descent;
27096 if (it->glyph_row)
27097 append_glyphless_glyph (it, face_id, for_no_font, len,
27098 upper_xoff, upper_yoff,
27099 lower_xoff, lower_yoff);
27100 it->nglyphs = 1;
27101 take_vertical_position_into_account (it);
27105 /* RIF:
27106 Produce glyphs/get display metrics for the display element IT is
27107 loaded with. See the description of struct it in dispextern.h
27108 for an overview of struct it. */
27110 void
27111 x_produce_glyphs (struct it *it)
27113 int extra_line_spacing = it->extra_line_spacing;
27115 it->glyph_not_available_p = false;
27117 if (it->what == IT_CHARACTER)
27119 XChar2b char2b;
27120 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27121 struct font *font = face->font;
27122 struct font_metrics *pcm = NULL;
27123 int boff; /* Baseline offset. */
27125 if (font == NULL)
27127 /* When no suitable font is found, display this character by
27128 the method specified in the first extra slot of
27129 Vglyphless_char_display. */
27130 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27132 eassert (it->what == IT_GLYPHLESS);
27133 produce_glyphless_glyph (it, true,
27134 STRINGP (acronym) ? acronym : Qnil);
27135 goto done;
27138 boff = font->baseline_offset;
27139 if (font->vertical_centering)
27140 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27142 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27144 it->nglyphs = 1;
27146 if (it->override_ascent >= 0)
27148 it->ascent = it->override_ascent;
27149 it->descent = it->override_descent;
27150 boff = it->override_boff;
27152 else
27154 it->ascent = FONT_BASE (font) + boff;
27155 it->descent = FONT_DESCENT (font) - boff;
27158 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27160 pcm = get_per_char_metric (font, &char2b);
27161 if (pcm->width == 0
27162 && pcm->rbearing == 0 && pcm->lbearing == 0)
27163 pcm = NULL;
27166 if (pcm)
27168 it->phys_ascent = pcm->ascent + boff;
27169 it->phys_descent = pcm->descent - boff;
27170 it->pixel_width = pcm->width;
27171 /* Don't use font-global values for ascent and descent
27172 if they result in an exceedingly large line height. */
27173 if (it->override_ascent < 0)
27175 if (FONT_TOO_HIGH (font))
27177 it->ascent = it->phys_ascent;
27178 it->descent = it->phys_descent;
27179 /* These limitations are enforced by an
27180 assertion near the end of this function. */
27181 if (it->ascent < 0)
27182 it->ascent = 0;
27183 if (it->descent < 0)
27184 it->descent = 0;
27188 else
27190 it->glyph_not_available_p = true;
27191 it->phys_ascent = it->ascent;
27192 it->phys_descent = it->descent;
27193 it->pixel_width = font->space_width;
27196 if (it->constrain_row_ascent_descent_p)
27198 if (it->descent > it->max_descent)
27200 it->ascent += it->descent - it->max_descent;
27201 it->descent = it->max_descent;
27203 if (it->ascent > it->max_ascent)
27205 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27206 it->ascent = it->max_ascent;
27208 it->phys_ascent = min (it->phys_ascent, it->ascent);
27209 it->phys_descent = min (it->phys_descent, it->descent);
27210 extra_line_spacing = 0;
27213 /* If this is a space inside a region of text with
27214 `space-width' property, change its width. */
27215 bool stretched_p
27216 = it->char_to_display == ' ' && !NILP (it->space_width);
27217 if (stretched_p)
27218 it->pixel_width *= XFLOATINT (it->space_width);
27220 /* If face has a box, add the box thickness to the character
27221 height. If character has a box line to the left and/or
27222 right, add the box line width to the character's width. */
27223 if (face->box != FACE_NO_BOX)
27225 int thick = face->box_line_width;
27227 if (thick > 0)
27229 it->ascent += thick;
27230 it->descent += thick;
27232 else
27233 thick = -thick;
27235 if (it->start_of_box_run_p)
27236 it->pixel_width += thick;
27237 if (it->end_of_box_run_p)
27238 it->pixel_width += thick;
27241 /* If face has an overline, add the height of the overline
27242 (1 pixel) and a 1 pixel margin to the character height. */
27243 if (face->overline_p)
27244 it->ascent += overline_margin;
27246 if (it->constrain_row_ascent_descent_p)
27248 if (it->ascent > it->max_ascent)
27249 it->ascent = it->max_ascent;
27250 if (it->descent > it->max_descent)
27251 it->descent = it->max_descent;
27254 take_vertical_position_into_account (it);
27256 /* If we have to actually produce glyphs, do it. */
27257 if (it->glyph_row)
27259 if (stretched_p)
27261 /* Translate a space with a `space-width' property
27262 into a stretch glyph. */
27263 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27264 / FONT_HEIGHT (font));
27265 append_stretch_glyph (it, it->object, it->pixel_width,
27266 it->ascent + it->descent, ascent);
27268 else
27269 append_glyph (it);
27271 /* If characters with lbearing or rbearing are displayed
27272 in this line, record that fact in a flag of the
27273 glyph row. This is used to optimize X output code. */
27274 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27275 it->glyph_row->contains_overlapping_glyphs_p = true;
27277 if (! stretched_p && it->pixel_width == 0)
27278 /* We assure that all visible glyphs have at least 1-pixel
27279 width. */
27280 it->pixel_width = 1;
27282 else if (it->char_to_display == '\n')
27284 /* A newline has no width, but we need the height of the
27285 line. But if previous part of the line sets a height,
27286 don't increase that height. */
27288 Lisp_Object height;
27289 Lisp_Object total_height = Qnil;
27291 it->override_ascent = -1;
27292 it->pixel_width = 0;
27293 it->nglyphs = 0;
27295 height = get_it_property (it, Qline_height);
27296 /* Split (line-height total-height) list. */
27297 if (CONSP (height)
27298 && CONSP (XCDR (height))
27299 && NILP (XCDR (XCDR (height))))
27301 total_height = XCAR (XCDR (height));
27302 height = XCAR (height);
27304 height = calc_line_height_property (it, height, font, boff, true);
27306 if (it->override_ascent >= 0)
27308 it->ascent = it->override_ascent;
27309 it->descent = it->override_descent;
27310 boff = it->override_boff;
27312 else
27314 if (FONT_TOO_HIGH (font))
27316 it->ascent = font->pixel_size + boff - 1;
27317 it->descent = -boff + 1;
27318 if (it->descent < 0)
27319 it->descent = 0;
27321 else
27323 it->ascent = FONT_BASE (font) + boff;
27324 it->descent = FONT_DESCENT (font) - boff;
27328 if (EQ (height, Qt))
27330 if (it->descent > it->max_descent)
27332 it->ascent += it->descent - it->max_descent;
27333 it->descent = it->max_descent;
27335 if (it->ascent > it->max_ascent)
27337 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27338 it->ascent = it->max_ascent;
27340 it->phys_ascent = min (it->phys_ascent, it->ascent);
27341 it->phys_descent = min (it->phys_descent, it->descent);
27342 it->constrain_row_ascent_descent_p = true;
27343 extra_line_spacing = 0;
27345 else
27347 Lisp_Object spacing;
27349 it->phys_ascent = it->ascent;
27350 it->phys_descent = it->descent;
27352 if ((it->max_ascent > 0 || it->max_descent > 0)
27353 && face->box != FACE_NO_BOX
27354 && face->box_line_width > 0)
27356 it->ascent += face->box_line_width;
27357 it->descent += face->box_line_width;
27359 if (!NILP (height)
27360 && XINT (height) > it->ascent + it->descent)
27361 it->ascent = XINT (height) - it->descent;
27363 if (!NILP (total_height))
27364 spacing = calc_line_height_property (it, total_height, font,
27365 boff, false);
27366 else
27368 spacing = get_it_property (it, Qline_spacing);
27369 spacing = calc_line_height_property (it, spacing, font,
27370 boff, false);
27372 if (INTEGERP (spacing))
27374 extra_line_spacing = XINT (spacing);
27375 if (!NILP (total_height))
27376 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27380 else /* i.e. (it->char_to_display == '\t') */
27382 if (font->space_width > 0)
27384 int tab_width = it->tab_width * font->space_width;
27385 int x = it->current_x + it->continuation_lines_width;
27386 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27388 /* If the distance from the current position to the next tab
27389 stop is less than a space character width, use the
27390 tab stop after that. */
27391 if (next_tab_x - x < font->space_width)
27392 next_tab_x += tab_width;
27394 it->pixel_width = next_tab_x - x;
27395 it->nglyphs = 1;
27396 if (FONT_TOO_HIGH (font))
27398 if (get_char_glyph_code (' ', font, &char2b))
27400 pcm = get_per_char_metric (font, &char2b);
27401 if (pcm->width == 0
27402 && pcm->rbearing == 0 && pcm->lbearing == 0)
27403 pcm = NULL;
27406 if (pcm)
27408 it->ascent = pcm->ascent + boff;
27409 it->descent = pcm->descent - boff;
27411 else
27413 it->ascent = font->pixel_size + boff - 1;
27414 it->descent = -boff + 1;
27416 if (it->ascent < 0)
27417 it->ascent = 0;
27418 if (it->descent < 0)
27419 it->descent = 0;
27421 else
27423 it->ascent = FONT_BASE (font) + boff;
27424 it->descent = FONT_DESCENT (font) - boff;
27426 it->phys_ascent = it->ascent;
27427 it->phys_descent = it->descent;
27429 if (it->glyph_row)
27431 append_stretch_glyph (it, it->object, it->pixel_width,
27432 it->ascent + it->descent, it->ascent);
27435 else
27437 it->pixel_width = 0;
27438 it->nglyphs = 1;
27442 if (FONT_TOO_HIGH (font))
27444 int font_ascent, font_descent;
27446 /* For very large fonts, where we ignore the declared font
27447 dimensions, and go by per-character metrics instead,
27448 don't let the row ascent and descent values (and the row
27449 height computed from them) be smaller than the "normal"
27450 character metrics. This avoids unpleasant effects
27451 whereby lines on display would change their height
27452 depending on which characters are shown. */
27453 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27454 it->max_ascent = max (it->max_ascent, font_ascent);
27455 it->max_descent = max (it->max_descent, font_descent);
27458 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27460 /* A static composition.
27462 Note: A composition is represented as one glyph in the
27463 glyph matrix. There are no padding glyphs.
27465 Important note: pixel_width, ascent, and descent are the
27466 values of what is drawn by draw_glyphs (i.e. the values of
27467 the overall glyphs composed). */
27468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27469 int boff; /* baseline offset */
27470 struct composition *cmp = composition_table[it->cmp_it.id];
27471 int glyph_len = cmp->glyph_len;
27472 struct font *font = face->font;
27474 it->nglyphs = 1;
27476 /* If we have not yet calculated pixel size data of glyphs of
27477 the composition for the current face font, calculate them
27478 now. Theoretically, we have to check all fonts for the
27479 glyphs, but that requires much time and memory space. So,
27480 here we check only the font of the first glyph. This may
27481 lead to incorrect display, but it's very rare, and C-l
27482 (recenter-top-bottom) can correct the display anyway. */
27483 if (! cmp->font || cmp->font != font)
27485 /* Ascent and descent of the font of the first character
27486 of this composition (adjusted by baseline offset).
27487 Ascent and descent of overall glyphs should not be less
27488 than these, respectively. */
27489 int font_ascent, font_descent, font_height;
27490 /* Bounding box of the overall glyphs. */
27491 int leftmost, rightmost, lowest, highest;
27492 int lbearing, rbearing;
27493 int i, width, ascent, descent;
27494 int c;
27495 XChar2b char2b;
27496 struct font_metrics *pcm;
27497 ptrdiff_t pos;
27499 eassume (0 < glyph_len); /* See Bug#8512. */
27501 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27502 while (c == '\t' && 0 < --glyph_len);
27504 bool right_padded = glyph_len < cmp->glyph_len;
27505 for (i = 0; i < glyph_len; i++)
27507 c = COMPOSITION_GLYPH (cmp, i);
27508 if (c != '\t')
27509 break;
27510 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27512 bool left_padded = i > 0;
27514 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27515 : IT_CHARPOS (*it));
27516 /* If no suitable font is found, use the default font. */
27517 bool font_not_found_p = font == NULL;
27518 if (font_not_found_p)
27520 face = face->ascii_face;
27521 font = face->font;
27523 boff = font->baseline_offset;
27524 if (font->vertical_centering)
27525 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27526 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27527 font_ascent += boff;
27528 font_descent -= boff;
27529 font_height = font_ascent + font_descent;
27531 cmp->font = font;
27533 pcm = NULL;
27534 if (! font_not_found_p)
27536 get_char_face_and_encoding (it->f, c, it->face_id,
27537 &char2b, false);
27538 pcm = get_per_char_metric (font, &char2b);
27541 /* Initialize the bounding box. */
27542 if (pcm)
27544 width = cmp->glyph_len > 0 ? pcm->width : 0;
27545 ascent = pcm->ascent;
27546 descent = pcm->descent;
27547 lbearing = pcm->lbearing;
27548 rbearing = pcm->rbearing;
27550 else
27552 width = cmp->glyph_len > 0 ? font->space_width : 0;
27553 ascent = FONT_BASE (font);
27554 descent = FONT_DESCENT (font);
27555 lbearing = 0;
27556 rbearing = width;
27559 rightmost = width;
27560 leftmost = 0;
27561 lowest = - descent + boff;
27562 highest = ascent + boff;
27564 if (! font_not_found_p
27565 && font->default_ascent
27566 && CHAR_TABLE_P (Vuse_default_ascent)
27567 && !NILP (Faref (Vuse_default_ascent,
27568 make_number (it->char_to_display))))
27569 highest = font->default_ascent + boff;
27571 /* Draw the first glyph at the normal position. It may be
27572 shifted to right later if some other glyphs are drawn
27573 at the left. */
27574 cmp->offsets[i * 2] = 0;
27575 cmp->offsets[i * 2 + 1] = boff;
27576 cmp->lbearing = lbearing;
27577 cmp->rbearing = rbearing;
27579 /* Set cmp->offsets for the remaining glyphs. */
27580 for (i++; i < glyph_len; i++)
27582 int left, right, btm, top;
27583 int ch = COMPOSITION_GLYPH (cmp, i);
27584 int face_id;
27585 struct face *this_face;
27587 if (ch == '\t')
27588 ch = ' ';
27589 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27590 this_face = FACE_FROM_ID (it->f, face_id);
27591 font = this_face->font;
27593 if (font == NULL)
27594 pcm = NULL;
27595 else
27597 get_char_face_and_encoding (it->f, ch, face_id,
27598 &char2b, false);
27599 pcm = get_per_char_metric (font, &char2b);
27601 if (! pcm)
27602 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27603 else
27605 width = pcm->width;
27606 ascent = pcm->ascent;
27607 descent = pcm->descent;
27608 lbearing = pcm->lbearing;
27609 rbearing = pcm->rbearing;
27610 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27612 /* Relative composition with or without
27613 alternate chars. */
27614 left = (leftmost + rightmost - width) / 2;
27615 btm = - descent + boff;
27616 if (font->relative_compose
27617 && (! CHAR_TABLE_P (Vignore_relative_composition)
27618 || NILP (Faref (Vignore_relative_composition,
27619 make_number (ch)))))
27622 if (- descent >= font->relative_compose)
27623 /* One extra pixel between two glyphs. */
27624 btm = highest + 1;
27625 else if (ascent <= 0)
27626 /* One extra pixel between two glyphs. */
27627 btm = lowest - 1 - ascent - descent;
27630 else
27632 /* A composition rule is specified by an integer
27633 value that encodes global and new reference
27634 points (GREF and NREF). GREF and NREF are
27635 specified by numbers as below:
27637 0---1---2 -- ascent
27641 9--10--11 -- center
27643 ---3---4---5--- baseline
27645 6---7---8 -- descent
27647 int rule = COMPOSITION_RULE (cmp, i);
27648 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27650 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27651 grefx = gref % 3, nrefx = nref % 3;
27652 grefy = gref / 3, nrefy = nref / 3;
27653 if (xoff)
27654 xoff = font_height * (xoff - 128) / 256;
27655 if (yoff)
27656 yoff = font_height * (yoff - 128) / 256;
27658 left = (leftmost
27659 + grefx * (rightmost - leftmost) / 2
27660 - nrefx * width / 2
27661 + xoff);
27663 btm = ((grefy == 0 ? highest
27664 : grefy == 1 ? 0
27665 : grefy == 2 ? lowest
27666 : (highest + lowest) / 2)
27667 - (nrefy == 0 ? ascent + descent
27668 : nrefy == 1 ? descent - boff
27669 : nrefy == 2 ? 0
27670 : (ascent + descent) / 2)
27671 + yoff);
27674 cmp->offsets[i * 2] = left;
27675 cmp->offsets[i * 2 + 1] = btm + descent;
27677 /* Update the bounding box of the overall glyphs. */
27678 if (width > 0)
27680 right = left + width;
27681 if (left < leftmost)
27682 leftmost = left;
27683 if (right > rightmost)
27684 rightmost = right;
27686 top = btm + descent + ascent;
27687 if (top > highest)
27688 highest = top;
27689 if (btm < lowest)
27690 lowest = btm;
27692 if (cmp->lbearing > left + lbearing)
27693 cmp->lbearing = left + lbearing;
27694 if (cmp->rbearing < left + rbearing)
27695 cmp->rbearing = left + rbearing;
27699 /* If there are glyphs whose x-offsets are negative,
27700 shift all glyphs to the right and make all x-offsets
27701 non-negative. */
27702 if (leftmost < 0)
27704 for (i = 0; i < cmp->glyph_len; i++)
27705 cmp->offsets[i * 2] -= leftmost;
27706 rightmost -= leftmost;
27707 cmp->lbearing -= leftmost;
27708 cmp->rbearing -= leftmost;
27711 if (left_padded && cmp->lbearing < 0)
27713 for (i = 0; i < cmp->glyph_len; i++)
27714 cmp->offsets[i * 2] -= cmp->lbearing;
27715 rightmost -= cmp->lbearing;
27716 cmp->rbearing -= cmp->lbearing;
27717 cmp->lbearing = 0;
27719 if (right_padded && rightmost < cmp->rbearing)
27721 rightmost = cmp->rbearing;
27724 cmp->pixel_width = rightmost;
27725 cmp->ascent = highest;
27726 cmp->descent = - lowest;
27727 if (cmp->ascent < font_ascent)
27728 cmp->ascent = font_ascent;
27729 if (cmp->descent < font_descent)
27730 cmp->descent = font_descent;
27733 if (it->glyph_row
27734 && (cmp->lbearing < 0
27735 || cmp->rbearing > cmp->pixel_width))
27736 it->glyph_row->contains_overlapping_glyphs_p = true;
27738 it->pixel_width = cmp->pixel_width;
27739 it->ascent = it->phys_ascent = cmp->ascent;
27740 it->descent = it->phys_descent = cmp->descent;
27741 if (face->box != FACE_NO_BOX)
27743 int thick = face->box_line_width;
27745 if (thick > 0)
27747 it->ascent += thick;
27748 it->descent += thick;
27750 else
27751 thick = - thick;
27753 if (it->start_of_box_run_p)
27754 it->pixel_width += thick;
27755 if (it->end_of_box_run_p)
27756 it->pixel_width += thick;
27759 /* If face has an overline, add the height of the overline
27760 (1 pixel) and a 1 pixel margin to the character height. */
27761 if (face->overline_p)
27762 it->ascent += overline_margin;
27764 take_vertical_position_into_account (it);
27765 if (it->ascent < 0)
27766 it->ascent = 0;
27767 if (it->descent < 0)
27768 it->descent = 0;
27770 if (it->glyph_row && cmp->glyph_len > 0)
27771 append_composite_glyph (it);
27773 else if (it->what == IT_COMPOSITION)
27775 /* A dynamic (automatic) composition. */
27776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27777 Lisp_Object gstring;
27778 struct font_metrics metrics;
27780 it->nglyphs = 1;
27782 gstring = composition_gstring_from_id (it->cmp_it.id);
27783 it->pixel_width
27784 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27785 &metrics);
27786 if (it->glyph_row
27787 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27788 it->glyph_row->contains_overlapping_glyphs_p = true;
27789 it->ascent = it->phys_ascent = metrics.ascent;
27790 it->descent = it->phys_descent = metrics.descent;
27791 if (face->box != FACE_NO_BOX)
27793 int thick = face->box_line_width;
27795 if (thick > 0)
27797 it->ascent += thick;
27798 it->descent += thick;
27800 else
27801 thick = - thick;
27803 if (it->start_of_box_run_p)
27804 it->pixel_width += thick;
27805 if (it->end_of_box_run_p)
27806 it->pixel_width += thick;
27808 /* If face has an overline, add the height of the overline
27809 (1 pixel) and a 1 pixel margin to the character height. */
27810 if (face->overline_p)
27811 it->ascent += overline_margin;
27812 take_vertical_position_into_account (it);
27813 if (it->ascent < 0)
27814 it->ascent = 0;
27815 if (it->descent < 0)
27816 it->descent = 0;
27818 if (it->glyph_row)
27819 append_composite_glyph (it);
27821 else if (it->what == IT_GLYPHLESS)
27822 produce_glyphless_glyph (it, false, Qnil);
27823 else if (it->what == IT_IMAGE)
27824 produce_image_glyph (it);
27825 else if (it->what == IT_STRETCH)
27826 produce_stretch_glyph (it);
27827 else if (it->what == IT_XWIDGET)
27828 produce_xwidget_glyph (it);
27830 done:
27831 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27832 because this isn't true for images with `:ascent 100'. */
27833 eassert (it->ascent >= 0 && it->descent >= 0);
27834 if (it->area == TEXT_AREA)
27835 it->current_x += it->pixel_width;
27837 if (extra_line_spacing > 0)
27839 it->descent += extra_line_spacing;
27840 if (extra_line_spacing > it->max_extra_line_spacing)
27841 it->max_extra_line_spacing = extra_line_spacing;
27844 it->max_ascent = max (it->max_ascent, it->ascent);
27845 it->max_descent = max (it->max_descent, it->descent);
27846 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27847 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27850 /* EXPORT for RIF:
27851 Output LEN glyphs starting at START at the nominal cursor position.
27852 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27853 being updated, and UPDATED_AREA is the area of that row being updated. */
27855 void
27856 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27857 struct glyph *start, enum glyph_row_area updated_area, int len)
27859 int x, hpos, chpos = w->phys_cursor.hpos;
27861 eassert (updated_row);
27862 /* When the window is hscrolled, cursor hpos can legitimately be out
27863 of bounds, but we draw the cursor at the corresponding window
27864 margin in that case. */
27865 if (!updated_row->reversed_p && chpos < 0)
27866 chpos = 0;
27867 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27868 chpos = updated_row->used[TEXT_AREA] - 1;
27870 block_input ();
27872 /* Write glyphs. */
27874 hpos = start - updated_row->glyphs[updated_area];
27875 x = draw_glyphs (w, w->output_cursor.x,
27876 updated_row, updated_area,
27877 hpos, hpos + len,
27878 DRAW_NORMAL_TEXT, 0);
27880 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27881 if (updated_area == TEXT_AREA
27882 && w->phys_cursor_on_p
27883 && w->phys_cursor.vpos == w->output_cursor.vpos
27884 && chpos >= hpos
27885 && chpos < hpos + len)
27886 w->phys_cursor_on_p = false;
27888 unblock_input ();
27890 /* Advance the output cursor. */
27891 w->output_cursor.hpos += len;
27892 w->output_cursor.x = x;
27896 /* EXPORT for RIF:
27897 Insert LEN glyphs from START at the nominal cursor position. */
27899 void
27900 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27901 struct glyph *start, enum glyph_row_area updated_area, int len)
27903 struct frame *f;
27904 int line_height, shift_by_width, shifted_region_width;
27905 struct glyph_row *row;
27906 struct glyph *glyph;
27907 int frame_x, frame_y;
27908 ptrdiff_t hpos;
27910 eassert (updated_row);
27911 block_input ();
27912 f = XFRAME (WINDOW_FRAME (w));
27914 /* Get the height of the line we are in. */
27915 row = updated_row;
27916 line_height = row->height;
27918 /* Get the width of the glyphs to insert. */
27919 shift_by_width = 0;
27920 for (glyph = start; glyph < start + len; ++glyph)
27921 shift_by_width += glyph->pixel_width;
27923 /* Get the width of the region to shift right. */
27924 shifted_region_width = (window_box_width (w, updated_area)
27925 - w->output_cursor.x
27926 - shift_by_width);
27928 /* Shift right. */
27929 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27930 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27932 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27933 line_height, shift_by_width);
27935 /* Write the glyphs. */
27936 hpos = start - row->glyphs[updated_area];
27937 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27938 hpos, hpos + len,
27939 DRAW_NORMAL_TEXT, 0);
27941 /* Advance the output cursor. */
27942 w->output_cursor.hpos += len;
27943 w->output_cursor.x += shift_by_width;
27944 unblock_input ();
27948 /* EXPORT for RIF:
27949 Erase the current text line from the nominal cursor position
27950 (inclusive) to pixel column TO_X (exclusive). The idea is that
27951 everything from TO_X onward is already erased.
27953 TO_X is a pixel position relative to UPDATED_AREA of currently
27954 updated window W. TO_X == -1 means clear to the end of this area. */
27956 void
27957 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27958 enum glyph_row_area updated_area, int to_x)
27960 struct frame *f;
27961 int max_x, min_y, max_y;
27962 int from_x, from_y, to_y;
27964 eassert (updated_row);
27965 f = XFRAME (w->frame);
27967 if (updated_row->full_width_p)
27968 max_x = (WINDOW_PIXEL_WIDTH (w)
27969 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27970 else
27971 max_x = window_box_width (w, updated_area);
27972 max_y = window_text_bottom_y (w);
27974 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27975 of window. For TO_X > 0, truncate to end of drawing area. */
27976 if (to_x == 0)
27977 return;
27978 else if (to_x < 0)
27979 to_x = max_x;
27980 else
27981 to_x = min (to_x, max_x);
27983 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27985 /* Notice if the cursor will be cleared by this operation. */
27986 if (!updated_row->full_width_p)
27987 notice_overwritten_cursor (w, updated_area,
27988 w->output_cursor.x, -1,
27989 updated_row->y,
27990 MATRIX_ROW_BOTTOM_Y (updated_row));
27992 from_x = w->output_cursor.x;
27994 /* Translate to frame coordinates. */
27995 if (updated_row->full_width_p)
27997 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27998 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28000 else
28002 int area_left = window_box_left (w, updated_area);
28003 from_x += area_left;
28004 to_x += area_left;
28007 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28008 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28009 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28011 /* Prevent inadvertently clearing to end of the X window. */
28012 if (to_x > from_x && to_y > from_y)
28014 block_input ();
28015 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28016 to_x - from_x, to_y - from_y);
28017 unblock_input ();
28021 #endif /* HAVE_WINDOW_SYSTEM */
28025 /***********************************************************************
28026 Cursor types
28027 ***********************************************************************/
28029 /* Value is the internal representation of the specified cursor type
28030 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28031 of the bar cursor. */
28033 static enum text_cursor_kinds
28034 get_specified_cursor_type (Lisp_Object arg, int *width)
28036 enum text_cursor_kinds type;
28038 if (NILP (arg))
28039 return NO_CURSOR;
28041 if (EQ (arg, Qbox))
28042 return FILLED_BOX_CURSOR;
28044 if (EQ (arg, Qhollow))
28045 return HOLLOW_BOX_CURSOR;
28047 if (EQ (arg, Qbar))
28049 *width = 2;
28050 return BAR_CURSOR;
28053 if (CONSP (arg)
28054 && EQ (XCAR (arg), Qbar)
28055 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28057 *width = XINT (XCDR (arg));
28058 return BAR_CURSOR;
28061 if (EQ (arg, Qhbar))
28063 *width = 2;
28064 return HBAR_CURSOR;
28067 if (CONSP (arg)
28068 && EQ (XCAR (arg), Qhbar)
28069 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28071 *width = XINT (XCDR (arg));
28072 return HBAR_CURSOR;
28075 /* Treat anything unknown as "hollow box cursor".
28076 It was bad to signal an error; people have trouble fixing
28077 .Xdefaults with Emacs, when it has something bad in it. */
28078 type = HOLLOW_BOX_CURSOR;
28080 return type;
28083 /* Set the default cursor types for specified frame. */
28084 void
28085 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28087 int width = 1;
28088 Lisp_Object tem;
28090 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28091 FRAME_CURSOR_WIDTH (f) = width;
28093 /* By default, set up the blink-off state depending on the on-state. */
28095 tem = Fassoc (arg, Vblink_cursor_alist);
28096 if (!NILP (tem))
28098 FRAME_BLINK_OFF_CURSOR (f)
28099 = get_specified_cursor_type (XCDR (tem), &width);
28100 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28102 else
28103 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28105 /* Make sure the cursor gets redrawn. */
28106 f->cursor_type_changed = true;
28110 #ifdef HAVE_WINDOW_SYSTEM
28112 /* Return the cursor we want to be displayed in window W. Return
28113 width of bar/hbar cursor through WIDTH arg. Return with
28114 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28115 (i.e. if the `system caret' should track this cursor).
28117 In a mini-buffer window, we want the cursor only to appear if we
28118 are reading input from this window. For the selected window, we
28119 want the cursor type given by the frame parameter or buffer local
28120 setting of cursor-type. If explicitly marked off, draw no cursor.
28121 In all other cases, we want a hollow box cursor. */
28123 static enum text_cursor_kinds
28124 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28125 bool *active_cursor)
28127 struct frame *f = XFRAME (w->frame);
28128 struct buffer *b = XBUFFER (w->contents);
28129 int cursor_type = DEFAULT_CURSOR;
28130 Lisp_Object alt_cursor;
28131 bool non_selected = false;
28133 *active_cursor = true;
28135 /* Echo area */
28136 if (cursor_in_echo_area
28137 && FRAME_HAS_MINIBUF_P (f)
28138 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28140 if (w == XWINDOW (echo_area_window))
28142 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28144 *width = FRAME_CURSOR_WIDTH (f);
28145 return FRAME_DESIRED_CURSOR (f);
28147 else
28148 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28151 *active_cursor = false;
28152 non_selected = true;
28155 /* Detect a nonselected window or nonselected frame. */
28156 else if (w != XWINDOW (f->selected_window)
28157 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28159 *active_cursor = false;
28161 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28162 return NO_CURSOR;
28164 non_selected = true;
28167 /* Never display a cursor in a window in which cursor-type is nil. */
28168 if (NILP (BVAR (b, cursor_type)))
28169 return NO_CURSOR;
28171 /* Get the normal cursor type for this window. */
28172 if (EQ (BVAR (b, cursor_type), Qt))
28174 cursor_type = FRAME_DESIRED_CURSOR (f);
28175 *width = FRAME_CURSOR_WIDTH (f);
28177 else
28178 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28180 /* Use cursor-in-non-selected-windows instead
28181 for non-selected window or frame. */
28182 if (non_selected)
28184 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28185 if (!EQ (Qt, alt_cursor))
28186 return get_specified_cursor_type (alt_cursor, width);
28187 /* t means modify the normal cursor type. */
28188 if (cursor_type == FILLED_BOX_CURSOR)
28189 cursor_type = HOLLOW_BOX_CURSOR;
28190 else if (cursor_type == BAR_CURSOR && *width > 1)
28191 --*width;
28192 return cursor_type;
28195 /* Use normal cursor if not blinked off. */
28196 if (!w->cursor_off_p)
28198 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28199 return NO_CURSOR;
28200 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28202 if (cursor_type == FILLED_BOX_CURSOR)
28204 /* Using a block cursor on large images can be very annoying.
28205 So use a hollow cursor for "large" images.
28206 If image is not transparent (no mask), also use hollow cursor. */
28207 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28208 if (img != NULL && IMAGEP (img->spec))
28210 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28211 where N = size of default frame font size.
28212 This should cover most of the "tiny" icons people may use. */
28213 if (!img->mask
28214 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28215 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28216 cursor_type = HOLLOW_BOX_CURSOR;
28219 else if (cursor_type != NO_CURSOR)
28221 /* Display current only supports BOX and HOLLOW cursors for images.
28222 So for now, unconditionally use a HOLLOW cursor when cursor is
28223 not a solid box cursor. */
28224 cursor_type = HOLLOW_BOX_CURSOR;
28227 return cursor_type;
28230 /* Cursor is blinked off, so determine how to "toggle" it. */
28232 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28233 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28234 return get_specified_cursor_type (XCDR (alt_cursor), width);
28236 /* Then see if frame has specified a specific blink off cursor type. */
28237 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28239 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28240 return FRAME_BLINK_OFF_CURSOR (f);
28243 #if false
28244 /* Some people liked having a permanently visible blinking cursor,
28245 while others had very strong opinions against it. So it was
28246 decided to remove it. KFS 2003-09-03 */
28248 /* Finally perform built-in cursor blinking:
28249 filled box <-> hollow box
28250 wide [h]bar <-> narrow [h]bar
28251 narrow [h]bar <-> no cursor
28252 other type <-> no cursor */
28254 if (cursor_type == FILLED_BOX_CURSOR)
28255 return HOLLOW_BOX_CURSOR;
28257 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28259 *width = 1;
28260 return cursor_type;
28262 #endif
28264 return NO_CURSOR;
28268 /* Notice when the text cursor of window W has been completely
28269 overwritten by a drawing operation that outputs glyphs in AREA
28270 starting at X0 and ending at X1 in the line starting at Y0 and
28271 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28272 the rest of the line after X0 has been written. Y coordinates
28273 are window-relative. */
28275 static void
28276 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28277 int x0, int x1, int y0, int y1)
28279 int cx0, cx1, cy0, cy1;
28280 struct glyph_row *row;
28282 if (!w->phys_cursor_on_p)
28283 return;
28284 if (area != TEXT_AREA)
28285 return;
28287 if (w->phys_cursor.vpos < 0
28288 || w->phys_cursor.vpos >= w->current_matrix->nrows
28289 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28290 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28291 return;
28293 if (row->cursor_in_fringe_p)
28295 row->cursor_in_fringe_p = false;
28296 draw_fringe_bitmap (w, row, row->reversed_p);
28297 w->phys_cursor_on_p = false;
28298 return;
28301 cx0 = w->phys_cursor.x;
28302 cx1 = cx0 + w->phys_cursor_width;
28303 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28304 return;
28306 /* The cursor image will be completely removed from the
28307 screen if the output area intersects the cursor area in
28308 y-direction. When we draw in [y0 y1[, and some part of
28309 the cursor is at y < y0, that part must have been drawn
28310 before. When scrolling, the cursor is erased before
28311 actually scrolling, so we don't come here. When not
28312 scrolling, the rows above the old cursor row must have
28313 changed, and in this case these rows must have written
28314 over the cursor image.
28316 Likewise if part of the cursor is below y1, with the
28317 exception of the cursor being in the first blank row at
28318 the buffer and window end because update_text_area
28319 doesn't draw that row. (Except when it does, but
28320 that's handled in update_text_area.) */
28322 cy0 = w->phys_cursor.y;
28323 cy1 = cy0 + w->phys_cursor_height;
28324 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28325 return;
28327 w->phys_cursor_on_p = false;
28330 #endif /* HAVE_WINDOW_SYSTEM */
28333 /************************************************************************
28334 Mouse Face
28335 ************************************************************************/
28337 #ifdef HAVE_WINDOW_SYSTEM
28339 /* EXPORT for RIF:
28340 Fix the display of area AREA of overlapping row ROW in window W
28341 with respect to the overlapping part OVERLAPS. */
28343 void
28344 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28345 enum glyph_row_area area, int overlaps)
28347 int i, x;
28349 block_input ();
28351 x = 0;
28352 for (i = 0; i < row->used[area];)
28354 if (row->glyphs[area][i].overlaps_vertically_p)
28356 int start = i, start_x = x;
28360 x += row->glyphs[area][i].pixel_width;
28361 ++i;
28363 while (i < row->used[area]
28364 && row->glyphs[area][i].overlaps_vertically_p);
28366 draw_glyphs (w, start_x, row, area,
28367 start, i,
28368 DRAW_NORMAL_TEXT, overlaps);
28370 else
28372 x += row->glyphs[area][i].pixel_width;
28373 ++i;
28377 unblock_input ();
28381 /* EXPORT:
28382 Draw the cursor glyph of window W in glyph row ROW. See the
28383 comment of draw_glyphs for the meaning of HL. */
28385 void
28386 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28387 enum draw_glyphs_face hl)
28389 /* If cursor hpos is out of bounds, don't draw garbage. This can
28390 happen in mini-buffer windows when switching between echo area
28391 glyphs and mini-buffer. */
28392 if ((row->reversed_p
28393 ? (w->phys_cursor.hpos >= 0)
28394 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28396 bool on_p = w->phys_cursor_on_p;
28397 int x1;
28398 int hpos = w->phys_cursor.hpos;
28400 /* When the window is hscrolled, cursor hpos can legitimately be
28401 out of bounds, but we draw the cursor at the corresponding
28402 window margin in that case. */
28403 if (!row->reversed_p && hpos < 0)
28404 hpos = 0;
28405 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28406 hpos = row->used[TEXT_AREA] - 1;
28408 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28409 hl, 0);
28410 w->phys_cursor_on_p = on_p;
28412 if (hl == DRAW_CURSOR)
28413 w->phys_cursor_width = x1 - w->phys_cursor.x;
28414 /* When we erase the cursor, and ROW is overlapped by other
28415 rows, make sure that these overlapping parts of other rows
28416 are redrawn. */
28417 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28419 w->phys_cursor_width = x1 - w->phys_cursor.x;
28421 if (row > w->current_matrix->rows
28422 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28423 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28424 OVERLAPS_ERASED_CURSOR);
28426 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28427 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28428 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28429 OVERLAPS_ERASED_CURSOR);
28435 /* Erase the image of a cursor of window W from the screen. */
28437 void
28438 erase_phys_cursor (struct window *w)
28440 struct frame *f = XFRAME (w->frame);
28441 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28442 int hpos = w->phys_cursor.hpos;
28443 int vpos = w->phys_cursor.vpos;
28444 bool mouse_face_here_p = false;
28445 struct glyph_matrix *active_glyphs = w->current_matrix;
28446 struct glyph_row *cursor_row;
28447 struct glyph *cursor_glyph;
28448 enum draw_glyphs_face hl;
28450 /* No cursor displayed or row invalidated => nothing to do on the
28451 screen. */
28452 if (w->phys_cursor_type == NO_CURSOR)
28453 goto mark_cursor_off;
28455 /* VPOS >= active_glyphs->nrows means that window has been resized.
28456 Don't bother to erase the cursor. */
28457 if (vpos >= active_glyphs->nrows)
28458 goto mark_cursor_off;
28460 /* If row containing cursor is marked invalid, there is nothing we
28461 can do. */
28462 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28463 if (!cursor_row->enabled_p)
28464 goto mark_cursor_off;
28466 /* If line spacing is > 0, old cursor may only be partially visible in
28467 window after split-window. So adjust visible height. */
28468 cursor_row->visible_height = min (cursor_row->visible_height,
28469 window_text_bottom_y (w) - cursor_row->y);
28471 /* If row is completely invisible, don't attempt to delete a cursor which
28472 isn't there. This can happen if cursor is at top of a window, and
28473 we switch to a buffer with a header line in that window. */
28474 if (cursor_row->visible_height <= 0)
28475 goto mark_cursor_off;
28477 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28478 if (cursor_row->cursor_in_fringe_p)
28480 cursor_row->cursor_in_fringe_p = false;
28481 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28482 goto mark_cursor_off;
28485 /* This can happen when the new row is shorter than the old one.
28486 In this case, either draw_glyphs or clear_end_of_line
28487 should have cleared the cursor. Note that we wouldn't be
28488 able to erase the cursor in this case because we don't have a
28489 cursor glyph at hand. */
28490 if ((cursor_row->reversed_p
28491 ? (w->phys_cursor.hpos < 0)
28492 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28493 goto mark_cursor_off;
28495 /* When the window is hscrolled, cursor hpos can legitimately be out
28496 of bounds, but we draw the cursor at the corresponding window
28497 margin in that case. */
28498 if (!cursor_row->reversed_p && hpos < 0)
28499 hpos = 0;
28500 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28501 hpos = cursor_row->used[TEXT_AREA] - 1;
28503 /* If the cursor is in the mouse face area, redisplay that when
28504 we clear the cursor. */
28505 if (! NILP (hlinfo->mouse_face_window)
28506 && coords_in_mouse_face_p (w, hpos, vpos)
28507 /* Don't redraw the cursor's spot in mouse face if it is at the
28508 end of a line (on a newline). The cursor appears there, but
28509 mouse highlighting does not. */
28510 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28511 mouse_face_here_p = true;
28513 /* Maybe clear the display under the cursor. */
28514 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28516 int x, y;
28517 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28518 int width;
28520 cursor_glyph = get_phys_cursor_glyph (w);
28521 if (cursor_glyph == NULL)
28522 goto mark_cursor_off;
28524 width = cursor_glyph->pixel_width;
28525 x = w->phys_cursor.x;
28526 if (x < 0)
28528 width += x;
28529 x = 0;
28531 width = min (width, window_box_width (w, TEXT_AREA) - x);
28532 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28533 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28535 if (width > 0)
28536 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28539 /* Erase the cursor by redrawing the character underneath it. */
28540 if (mouse_face_here_p)
28541 hl = DRAW_MOUSE_FACE;
28542 else
28543 hl = DRAW_NORMAL_TEXT;
28544 draw_phys_cursor_glyph (w, cursor_row, hl);
28546 mark_cursor_off:
28547 w->phys_cursor_on_p = false;
28548 w->phys_cursor_type = NO_CURSOR;
28552 /* Display or clear cursor of window W. If !ON, clear the cursor.
28553 If ON, display the cursor; where to put the cursor is specified by
28554 HPOS, VPOS, X and Y. */
28556 void
28557 display_and_set_cursor (struct window *w, bool on,
28558 int hpos, int vpos, int x, int y)
28560 struct frame *f = XFRAME (w->frame);
28561 int new_cursor_type;
28562 int new_cursor_width;
28563 bool active_cursor;
28564 struct glyph_row *glyph_row;
28565 struct glyph *glyph;
28567 /* This is pointless on invisible frames, and dangerous on garbaged
28568 windows and frames; in the latter case, the frame or window may
28569 be in the midst of changing its size, and x and y may be off the
28570 window. */
28571 if (! FRAME_VISIBLE_P (f)
28572 || FRAME_GARBAGED_P (f)
28573 || vpos >= w->current_matrix->nrows
28574 || hpos >= w->current_matrix->matrix_w)
28575 return;
28577 /* If cursor is off and we want it off, return quickly. */
28578 if (!on && !w->phys_cursor_on_p)
28579 return;
28581 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28582 /* If cursor row is not enabled, we don't really know where to
28583 display the cursor. */
28584 if (!glyph_row->enabled_p)
28586 w->phys_cursor_on_p = false;
28587 return;
28590 glyph = NULL;
28591 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28592 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28594 eassert (input_blocked_p ());
28596 /* Set new_cursor_type to the cursor we want to be displayed. */
28597 new_cursor_type = get_window_cursor_type (w, glyph,
28598 &new_cursor_width, &active_cursor);
28600 /* If cursor is currently being shown and we don't want it to be or
28601 it is in the wrong place, or the cursor type is not what we want,
28602 erase it. */
28603 if (w->phys_cursor_on_p
28604 && (!on
28605 || w->phys_cursor.x != x
28606 || w->phys_cursor.y != y
28607 /* HPOS can be negative in R2L rows whose
28608 exact_window_width_line_p flag is set (i.e. their newline
28609 would "overflow into the fringe"). */
28610 || hpos < 0
28611 || new_cursor_type != w->phys_cursor_type
28612 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28613 && new_cursor_width != w->phys_cursor_width)))
28614 erase_phys_cursor (w);
28616 /* Don't check phys_cursor_on_p here because that flag is only set
28617 to false in some cases where we know that the cursor has been
28618 completely erased, to avoid the extra work of erasing the cursor
28619 twice. In other words, phys_cursor_on_p can be true and the cursor
28620 still not be visible, or it has only been partly erased. */
28621 if (on)
28623 w->phys_cursor_ascent = glyph_row->ascent;
28624 w->phys_cursor_height = glyph_row->height;
28626 /* Set phys_cursor_.* before x_draw_.* is called because some
28627 of them may need the information. */
28628 w->phys_cursor.x = x;
28629 w->phys_cursor.y = glyph_row->y;
28630 w->phys_cursor.hpos = hpos;
28631 w->phys_cursor.vpos = vpos;
28634 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28635 new_cursor_type, new_cursor_width,
28636 on, active_cursor);
28640 /* Switch the display of W's cursor on or off, according to the value
28641 of ON. */
28643 static void
28644 update_window_cursor (struct window *w, bool on)
28646 /* Don't update cursor in windows whose frame is in the process
28647 of being deleted. */
28648 if (w->current_matrix)
28650 int hpos = w->phys_cursor.hpos;
28651 int vpos = w->phys_cursor.vpos;
28652 struct glyph_row *row;
28654 if (vpos >= w->current_matrix->nrows
28655 || hpos >= w->current_matrix->matrix_w)
28656 return;
28658 row = MATRIX_ROW (w->current_matrix, vpos);
28660 /* When the window is hscrolled, cursor hpos can legitimately be
28661 out of bounds, but we draw the cursor at the corresponding
28662 window margin in that case. */
28663 if (!row->reversed_p && hpos < 0)
28664 hpos = 0;
28665 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28666 hpos = row->used[TEXT_AREA] - 1;
28668 block_input ();
28669 display_and_set_cursor (w, on, hpos, vpos,
28670 w->phys_cursor.x, w->phys_cursor.y);
28671 unblock_input ();
28676 /* Call update_window_cursor with parameter ON_P on all leaf windows
28677 in the window tree rooted at W. */
28679 static void
28680 update_cursor_in_window_tree (struct window *w, bool on_p)
28682 while (w)
28684 if (WINDOWP (w->contents))
28685 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28686 else
28687 update_window_cursor (w, on_p);
28689 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28694 /* EXPORT:
28695 Display the cursor on window W, or clear it, according to ON_P.
28696 Don't change the cursor's position. */
28698 void
28699 x_update_cursor (struct frame *f, bool on_p)
28701 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28705 /* EXPORT:
28706 Clear the cursor of window W to background color, and mark the
28707 cursor as not shown. This is used when the text where the cursor
28708 is about to be rewritten. */
28710 void
28711 x_clear_cursor (struct window *w)
28713 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28714 update_window_cursor (w, false);
28717 #endif /* HAVE_WINDOW_SYSTEM */
28719 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28720 and MSDOS. */
28721 static void
28722 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28723 int start_hpos, int end_hpos,
28724 enum draw_glyphs_face draw)
28726 #ifdef HAVE_WINDOW_SYSTEM
28727 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28729 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28730 return;
28732 #endif
28733 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28734 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28735 #endif
28738 /* Display the active region described by mouse_face_* according to DRAW. */
28740 static void
28741 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28743 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28744 struct frame *f = XFRAME (WINDOW_FRAME (w));
28746 if (/* If window is in the process of being destroyed, don't bother
28747 to do anything. */
28748 w->current_matrix != NULL
28749 /* Don't update mouse highlight if hidden. */
28750 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28751 /* Recognize when we are called to operate on rows that don't exist
28752 anymore. This can happen when a window is split. */
28753 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28755 bool phys_cursor_on_p = w->phys_cursor_on_p;
28756 struct glyph_row *row, *first, *last;
28758 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28759 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28761 for (row = first; row <= last && row->enabled_p; ++row)
28763 int start_hpos, end_hpos, start_x;
28765 /* For all but the first row, the highlight starts at column 0. */
28766 if (row == first)
28768 /* R2L rows have BEG and END in reversed order, but the
28769 screen drawing geometry is always left to right. So
28770 we need to mirror the beginning and end of the
28771 highlighted area in R2L rows. */
28772 if (!row->reversed_p)
28774 start_hpos = hlinfo->mouse_face_beg_col;
28775 start_x = hlinfo->mouse_face_beg_x;
28777 else if (row == last)
28779 start_hpos = hlinfo->mouse_face_end_col;
28780 start_x = hlinfo->mouse_face_end_x;
28782 else
28784 start_hpos = 0;
28785 start_x = 0;
28788 else if (row->reversed_p && row == last)
28790 start_hpos = hlinfo->mouse_face_end_col;
28791 start_x = hlinfo->mouse_face_end_x;
28793 else
28795 start_hpos = 0;
28796 start_x = 0;
28799 if (row == last)
28801 if (!row->reversed_p)
28802 end_hpos = hlinfo->mouse_face_end_col;
28803 else if (row == first)
28804 end_hpos = hlinfo->mouse_face_beg_col;
28805 else
28807 end_hpos = row->used[TEXT_AREA];
28808 if (draw == DRAW_NORMAL_TEXT)
28809 row->fill_line_p = true; /* Clear to end of line. */
28812 else if (row->reversed_p && row == first)
28813 end_hpos = hlinfo->mouse_face_beg_col;
28814 else
28816 end_hpos = row->used[TEXT_AREA];
28817 if (draw == DRAW_NORMAL_TEXT)
28818 row->fill_line_p = true; /* Clear to end of line. */
28821 if (end_hpos > start_hpos)
28823 draw_row_with_mouse_face (w, start_x, row,
28824 start_hpos, end_hpos, draw);
28826 row->mouse_face_p
28827 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28831 /* When we've written over the cursor, arrange for it to
28832 be displayed again. */
28833 if (FRAME_WINDOW_P (f)
28834 && phys_cursor_on_p && !w->phys_cursor_on_p)
28836 #ifdef HAVE_WINDOW_SYSTEM
28837 int hpos = w->phys_cursor.hpos;
28839 /* When the window is hscrolled, cursor hpos can legitimately be
28840 out of bounds, but we draw the cursor at the corresponding
28841 window margin in that case. */
28842 if (!row->reversed_p && hpos < 0)
28843 hpos = 0;
28844 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28845 hpos = row->used[TEXT_AREA] - 1;
28847 block_input ();
28848 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28849 w->phys_cursor.x, w->phys_cursor.y);
28850 unblock_input ();
28851 #endif /* HAVE_WINDOW_SYSTEM */
28855 #ifdef HAVE_WINDOW_SYSTEM
28856 /* Change the mouse cursor. */
28857 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28859 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28860 if (draw == DRAW_NORMAL_TEXT
28861 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28862 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28863 else
28864 #endif
28865 if (draw == DRAW_MOUSE_FACE)
28866 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28867 else
28868 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28870 #endif /* HAVE_WINDOW_SYSTEM */
28873 /* EXPORT:
28874 Clear out the mouse-highlighted active region.
28875 Redraw it un-highlighted first. Value is true if mouse
28876 face was actually drawn unhighlighted. */
28878 bool
28879 clear_mouse_face (Mouse_HLInfo *hlinfo)
28881 bool cleared
28882 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28883 if (cleared)
28884 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28885 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28886 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28887 hlinfo->mouse_face_window = Qnil;
28888 hlinfo->mouse_face_overlay = Qnil;
28889 return cleared;
28892 /* Return true if the coordinates HPOS and VPOS on windows W are
28893 within the mouse face on that window. */
28894 static bool
28895 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28897 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28899 /* Quickly resolve the easy cases. */
28900 if (!(WINDOWP (hlinfo->mouse_face_window)
28901 && XWINDOW (hlinfo->mouse_face_window) == w))
28902 return false;
28903 if (vpos < hlinfo->mouse_face_beg_row
28904 || vpos > hlinfo->mouse_face_end_row)
28905 return false;
28906 if (vpos > hlinfo->mouse_face_beg_row
28907 && vpos < hlinfo->mouse_face_end_row)
28908 return true;
28910 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28912 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28914 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28915 return true;
28917 else if ((vpos == hlinfo->mouse_face_beg_row
28918 && hpos >= hlinfo->mouse_face_beg_col)
28919 || (vpos == hlinfo->mouse_face_end_row
28920 && hpos < hlinfo->mouse_face_end_col))
28921 return true;
28923 else
28925 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28927 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28928 return true;
28930 else if ((vpos == hlinfo->mouse_face_beg_row
28931 && hpos <= hlinfo->mouse_face_beg_col)
28932 || (vpos == hlinfo->mouse_face_end_row
28933 && hpos > hlinfo->mouse_face_end_col))
28934 return true;
28936 return false;
28940 /* EXPORT:
28941 True if physical cursor of window W is within mouse face. */
28943 bool
28944 cursor_in_mouse_face_p (struct window *w)
28946 int hpos = w->phys_cursor.hpos;
28947 int vpos = w->phys_cursor.vpos;
28948 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28950 /* When the window is hscrolled, cursor hpos can legitimately be out
28951 of bounds, but we draw the cursor at the corresponding window
28952 margin in that case. */
28953 if (!row->reversed_p && hpos < 0)
28954 hpos = 0;
28955 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28956 hpos = row->used[TEXT_AREA] - 1;
28958 return coords_in_mouse_face_p (w, hpos, vpos);
28963 /* Find the glyph rows START_ROW and END_ROW of window W that display
28964 characters between buffer positions START_CHARPOS and END_CHARPOS
28965 (excluding END_CHARPOS). DISP_STRING is a display string that
28966 covers these buffer positions. This is similar to
28967 row_containing_pos, but is more accurate when bidi reordering makes
28968 buffer positions change non-linearly with glyph rows. */
28969 static void
28970 rows_from_pos_range (struct window *w,
28971 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28972 Lisp_Object disp_string,
28973 struct glyph_row **start, struct glyph_row **end)
28975 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28976 int last_y = window_text_bottom_y (w);
28977 struct glyph_row *row;
28979 *start = NULL;
28980 *end = NULL;
28982 while (!first->enabled_p
28983 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28984 first++;
28986 /* Find the START row. */
28987 for (row = first;
28988 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28989 row++)
28991 /* A row can potentially be the START row if the range of the
28992 characters it displays intersects the range
28993 [START_CHARPOS..END_CHARPOS). */
28994 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28995 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28996 /* See the commentary in row_containing_pos, for the
28997 explanation of the complicated way to check whether
28998 some position is beyond the end of the characters
28999 displayed by a row. */
29000 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29001 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29002 && !row->ends_at_zv_p
29003 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29004 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29005 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29006 && !row->ends_at_zv_p
29007 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29009 /* Found a candidate row. Now make sure at least one of the
29010 glyphs it displays has a charpos from the range
29011 [START_CHARPOS..END_CHARPOS).
29013 This is not obvious because bidi reordering could make
29014 buffer positions of a row be 1,2,3,102,101,100, and if we
29015 want to highlight characters in [50..60), we don't want
29016 this row, even though [50..60) does intersect [1..103),
29017 the range of character positions given by the row's start
29018 and end positions. */
29019 struct glyph *g = row->glyphs[TEXT_AREA];
29020 struct glyph *e = g + row->used[TEXT_AREA];
29022 while (g < e)
29024 if (((BUFFERP (g->object) || NILP (g->object))
29025 && start_charpos <= g->charpos && g->charpos < end_charpos)
29026 /* A glyph that comes from DISP_STRING is by
29027 definition to be highlighted. */
29028 || EQ (g->object, disp_string))
29029 *start = row;
29030 g++;
29032 if (*start)
29033 break;
29037 /* Find the END row. */
29038 if (!*start
29039 /* If the last row is partially visible, start looking for END
29040 from that row, instead of starting from FIRST. */
29041 && !(row->enabled_p
29042 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29043 row = first;
29044 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29046 struct glyph_row *next = row + 1;
29047 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29049 if (!next->enabled_p
29050 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29051 /* The first row >= START whose range of displayed characters
29052 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29053 is the row END + 1. */
29054 || (start_charpos < next_start
29055 && end_charpos < next_start)
29056 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29057 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29058 && !next->ends_at_zv_p
29059 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29060 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29061 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29062 && !next->ends_at_zv_p
29063 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29065 *end = row;
29066 break;
29068 else
29070 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29071 but none of the characters it displays are in the range, it is
29072 also END + 1. */
29073 struct glyph *g = next->glyphs[TEXT_AREA];
29074 struct glyph *s = g;
29075 struct glyph *e = g + next->used[TEXT_AREA];
29077 while (g < e)
29079 if (((BUFFERP (g->object) || NILP (g->object))
29080 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29081 /* If the buffer position of the first glyph in
29082 the row is equal to END_CHARPOS, it means
29083 the last character to be highlighted is the
29084 newline of ROW, and we must consider NEXT as
29085 END, not END+1. */
29086 || (((!next->reversed_p && g == s)
29087 || (next->reversed_p && g == e - 1))
29088 && (g->charpos == end_charpos
29089 /* Special case for when NEXT is an
29090 empty line at ZV. */
29091 || (g->charpos == -1
29092 && !row->ends_at_zv_p
29093 && next_start == end_charpos)))))
29094 /* A glyph that comes from DISP_STRING is by
29095 definition to be highlighted. */
29096 || EQ (g->object, disp_string))
29097 break;
29098 g++;
29100 if (g == e)
29102 *end = row;
29103 break;
29105 /* The first row that ends at ZV must be the last to be
29106 highlighted. */
29107 else if (next->ends_at_zv_p)
29109 *end = next;
29110 break;
29116 /* This function sets the mouse_face_* elements of HLINFO, assuming
29117 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29118 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29119 for the overlay or run of text properties specifying the mouse
29120 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29121 before-string and after-string that must also be highlighted.
29122 DISP_STRING, if non-nil, is a display string that may cover some
29123 or all of the highlighted text. */
29125 static void
29126 mouse_face_from_buffer_pos (Lisp_Object window,
29127 Mouse_HLInfo *hlinfo,
29128 ptrdiff_t mouse_charpos,
29129 ptrdiff_t start_charpos,
29130 ptrdiff_t end_charpos,
29131 Lisp_Object before_string,
29132 Lisp_Object after_string,
29133 Lisp_Object disp_string)
29135 struct window *w = XWINDOW (window);
29136 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29137 struct glyph_row *r1, *r2;
29138 struct glyph *glyph, *end;
29139 ptrdiff_t ignore, pos;
29140 int x;
29142 eassert (NILP (disp_string) || STRINGP (disp_string));
29143 eassert (NILP (before_string) || STRINGP (before_string));
29144 eassert (NILP (after_string) || STRINGP (after_string));
29146 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29147 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29148 if (r1 == NULL)
29149 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29150 /* If the before-string or display-string contains newlines,
29151 rows_from_pos_range skips to its last row. Move back. */
29152 if (!NILP (before_string) || !NILP (disp_string))
29154 struct glyph_row *prev;
29155 while ((prev = r1 - 1, prev >= first)
29156 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29157 && prev->used[TEXT_AREA] > 0)
29159 struct glyph *beg = prev->glyphs[TEXT_AREA];
29160 glyph = beg + prev->used[TEXT_AREA];
29161 while (--glyph >= beg && NILP (glyph->object));
29162 if (glyph < beg
29163 || !(EQ (glyph->object, before_string)
29164 || EQ (glyph->object, disp_string)))
29165 break;
29166 r1 = prev;
29169 if (r2 == NULL)
29171 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29172 hlinfo->mouse_face_past_end = true;
29174 else if (!NILP (after_string))
29176 /* If the after-string has newlines, advance to its last row. */
29177 struct glyph_row *next;
29178 struct glyph_row *last
29179 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29181 for (next = r2 + 1;
29182 next <= last
29183 && next->used[TEXT_AREA] > 0
29184 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29185 ++next)
29186 r2 = next;
29188 /* The rest of the display engine assumes that mouse_face_beg_row is
29189 either above mouse_face_end_row or identical to it. But with
29190 bidi-reordered continued lines, the row for START_CHARPOS could
29191 be below the row for END_CHARPOS. If so, swap the rows and store
29192 them in correct order. */
29193 if (r1->y > r2->y)
29195 struct glyph_row *tem = r2;
29197 r2 = r1;
29198 r1 = tem;
29201 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29202 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29204 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29205 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29206 could be anywhere in the row and in any order. The strategy
29207 below is to find the leftmost and the rightmost glyph that
29208 belongs to either of these 3 strings, or whose position is
29209 between START_CHARPOS and END_CHARPOS, and highlight all the
29210 glyphs between those two. This may cover more than just the text
29211 between START_CHARPOS and END_CHARPOS if the range of characters
29212 strides the bidi level boundary, e.g. if the beginning is in R2L
29213 text while the end is in L2R text or vice versa. */
29214 if (!r1->reversed_p)
29216 /* This row is in a left to right paragraph. Scan it left to
29217 right. */
29218 glyph = r1->glyphs[TEXT_AREA];
29219 end = glyph + r1->used[TEXT_AREA];
29220 x = r1->x;
29222 /* Skip truncation glyphs at the start of the glyph row. */
29223 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29224 for (; glyph < end
29225 && NILP (glyph->object)
29226 && glyph->charpos < 0;
29227 ++glyph)
29228 x += glyph->pixel_width;
29230 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29231 or DISP_STRING, and the first glyph from buffer whose
29232 position is between START_CHARPOS and END_CHARPOS. */
29233 for (; glyph < end
29234 && !NILP (glyph->object)
29235 && !EQ (glyph->object, disp_string)
29236 && !(BUFFERP (glyph->object)
29237 && (glyph->charpos >= start_charpos
29238 && glyph->charpos < end_charpos));
29239 ++glyph)
29241 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29242 are present at buffer positions between START_CHARPOS and
29243 END_CHARPOS, or if they come from an overlay. */
29244 if (EQ (glyph->object, before_string))
29246 pos = string_buffer_position (before_string,
29247 start_charpos);
29248 /* If pos == 0, it means before_string came from an
29249 overlay, not from a buffer position. */
29250 if (!pos || (pos >= start_charpos && pos < end_charpos))
29251 break;
29253 else if (EQ (glyph->object, after_string))
29255 pos = string_buffer_position (after_string, end_charpos);
29256 if (!pos || (pos >= start_charpos && pos < end_charpos))
29257 break;
29259 x += glyph->pixel_width;
29261 hlinfo->mouse_face_beg_x = x;
29262 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29264 else
29266 /* This row is in a right to left paragraph. Scan it right to
29267 left. */
29268 struct glyph *g;
29270 end = r1->glyphs[TEXT_AREA] - 1;
29271 glyph = end + r1->used[TEXT_AREA];
29273 /* Skip truncation glyphs at the start of the glyph row. */
29274 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29275 for (; glyph > end
29276 && NILP (glyph->object)
29277 && glyph->charpos < 0;
29278 --glyph)
29281 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29282 or DISP_STRING, and the first glyph from buffer whose
29283 position is between START_CHARPOS and END_CHARPOS. */
29284 for (; glyph > end
29285 && !NILP (glyph->object)
29286 && !EQ (glyph->object, disp_string)
29287 && !(BUFFERP (glyph->object)
29288 && (glyph->charpos >= start_charpos
29289 && glyph->charpos < end_charpos));
29290 --glyph)
29292 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29293 are present at buffer positions between START_CHARPOS and
29294 END_CHARPOS, or if they come from an overlay. */
29295 if (EQ (glyph->object, before_string))
29297 pos = string_buffer_position (before_string, start_charpos);
29298 /* If pos == 0, it means before_string came from an
29299 overlay, not from a buffer position. */
29300 if (!pos || (pos >= start_charpos && pos < end_charpos))
29301 break;
29303 else if (EQ (glyph->object, after_string))
29305 pos = string_buffer_position (after_string, end_charpos);
29306 if (!pos || (pos >= start_charpos && pos < end_charpos))
29307 break;
29311 glyph++; /* first glyph to the right of the highlighted area */
29312 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29313 x += g->pixel_width;
29314 hlinfo->mouse_face_beg_x = x;
29315 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29318 /* If the highlight ends in a different row, compute GLYPH and END
29319 for the end row. Otherwise, reuse the values computed above for
29320 the row where the highlight begins. */
29321 if (r2 != r1)
29323 if (!r2->reversed_p)
29325 glyph = r2->glyphs[TEXT_AREA];
29326 end = glyph + r2->used[TEXT_AREA];
29327 x = r2->x;
29329 else
29331 end = r2->glyphs[TEXT_AREA] - 1;
29332 glyph = end + r2->used[TEXT_AREA];
29336 if (!r2->reversed_p)
29338 /* Skip truncation and continuation glyphs near the end of the
29339 row, and also blanks and stretch glyphs inserted by
29340 extend_face_to_end_of_line. */
29341 while (end > glyph
29342 && NILP ((end - 1)->object))
29343 --end;
29344 /* Scan the rest of the glyph row from the end, looking for the
29345 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29346 DISP_STRING, or whose position is between START_CHARPOS
29347 and END_CHARPOS */
29348 for (--end;
29349 end > glyph
29350 && !NILP (end->object)
29351 && !EQ (end->object, disp_string)
29352 && !(BUFFERP (end->object)
29353 && (end->charpos >= start_charpos
29354 && end->charpos < end_charpos));
29355 --end)
29357 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29358 are present at buffer positions between START_CHARPOS and
29359 END_CHARPOS, or if they come from an overlay. */
29360 if (EQ (end->object, before_string))
29362 pos = string_buffer_position (before_string, start_charpos);
29363 if (!pos || (pos >= start_charpos && pos < end_charpos))
29364 break;
29366 else if (EQ (end->object, after_string))
29368 pos = string_buffer_position (after_string, end_charpos);
29369 if (!pos || (pos >= start_charpos && pos < end_charpos))
29370 break;
29373 /* Find the X coordinate of the last glyph to be highlighted. */
29374 for (; glyph <= end; ++glyph)
29375 x += glyph->pixel_width;
29377 hlinfo->mouse_face_end_x = x;
29378 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29380 else
29382 /* Skip truncation and continuation glyphs near the end of the
29383 row, and also blanks and stretch glyphs inserted by
29384 extend_face_to_end_of_line. */
29385 x = r2->x;
29386 end++;
29387 while (end < glyph
29388 && NILP (end->object))
29390 x += end->pixel_width;
29391 ++end;
29393 /* Scan the rest of the glyph row from the end, looking for the
29394 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29395 DISP_STRING, or whose position is between START_CHARPOS
29396 and END_CHARPOS */
29397 for ( ;
29398 end < glyph
29399 && !NILP (end->object)
29400 && !EQ (end->object, disp_string)
29401 && !(BUFFERP (end->object)
29402 && (end->charpos >= start_charpos
29403 && end->charpos < end_charpos));
29404 ++end)
29406 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29407 are present at buffer positions between START_CHARPOS and
29408 END_CHARPOS, or if they come from an overlay. */
29409 if (EQ (end->object, before_string))
29411 pos = string_buffer_position (before_string, start_charpos);
29412 if (!pos || (pos >= start_charpos && pos < end_charpos))
29413 break;
29415 else if (EQ (end->object, after_string))
29417 pos = string_buffer_position (after_string, end_charpos);
29418 if (!pos || (pos >= start_charpos && pos < end_charpos))
29419 break;
29421 x += end->pixel_width;
29423 /* If we exited the above loop because we arrived at the last
29424 glyph of the row, and its buffer position is still not in
29425 range, it means the last character in range is the preceding
29426 newline. Bump the end column and x values to get past the
29427 last glyph. */
29428 if (end == glyph
29429 && BUFFERP (end->object)
29430 && (end->charpos < start_charpos
29431 || end->charpos >= end_charpos))
29433 x += end->pixel_width;
29434 ++end;
29436 hlinfo->mouse_face_end_x = x;
29437 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29440 hlinfo->mouse_face_window = window;
29441 hlinfo->mouse_face_face_id
29442 = face_at_buffer_position (w, mouse_charpos, &ignore,
29443 mouse_charpos + 1,
29444 !hlinfo->mouse_face_hidden, -1);
29445 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29448 /* The following function is not used anymore (replaced with
29449 mouse_face_from_string_pos), but I leave it here for the time
29450 being, in case someone would. */
29452 #if false /* not used */
29454 /* Find the position of the glyph for position POS in OBJECT in
29455 window W's current matrix, and return in *X, *Y the pixel
29456 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29458 RIGHT_P means return the position of the right edge of the glyph.
29459 !RIGHT_P means return the left edge position.
29461 If no glyph for POS exists in the matrix, return the position of
29462 the glyph with the next smaller position that is in the matrix, if
29463 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29464 exists in the matrix, return the position of the glyph with the
29465 next larger position in OBJECT.
29467 Value is true if a glyph was found. */
29469 static bool
29470 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29471 int *hpos, int *vpos, int *x, int *y, bool right_p)
29473 int yb = window_text_bottom_y (w);
29474 struct glyph_row *r;
29475 struct glyph *best_glyph = NULL;
29476 struct glyph_row *best_row = NULL;
29477 int best_x = 0;
29479 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29480 r->enabled_p && r->y < yb;
29481 ++r)
29483 struct glyph *g = r->glyphs[TEXT_AREA];
29484 struct glyph *e = g + r->used[TEXT_AREA];
29485 int gx;
29487 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29488 if (EQ (g->object, object))
29490 if (g->charpos == pos)
29492 best_glyph = g;
29493 best_x = gx;
29494 best_row = r;
29495 goto found;
29497 else if (best_glyph == NULL
29498 || ((eabs (g->charpos - pos)
29499 < eabs (best_glyph->charpos - pos))
29500 && (right_p
29501 ? g->charpos < pos
29502 : g->charpos > pos)))
29504 best_glyph = g;
29505 best_x = gx;
29506 best_row = r;
29511 found:
29513 if (best_glyph)
29515 *x = best_x;
29516 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29518 if (right_p)
29520 *x += best_glyph->pixel_width;
29521 ++*hpos;
29524 *y = best_row->y;
29525 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29528 return best_glyph != NULL;
29530 #endif /* not used */
29532 /* Find the positions of the first and the last glyphs in window W's
29533 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29534 (assumed to be a string), and return in HLINFO's mouse_face_*
29535 members the pixel and column/row coordinates of those glyphs. */
29537 static void
29538 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29539 Lisp_Object object,
29540 ptrdiff_t startpos, ptrdiff_t endpos)
29542 int yb = window_text_bottom_y (w);
29543 struct glyph_row *r;
29544 struct glyph *g, *e;
29545 int gx;
29546 bool found = false;
29548 /* Find the glyph row with at least one position in the range
29549 [STARTPOS..ENDPOS), and the first glyph in that row whose
29550 position belongs to that range. */
29551 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29552 r->enabled_p && r->y < yb;
29553 ++r)
29555 if (!r->reversed_p)
29557 g = r->glyphs[TEXT_AREA];
29558 e = g + r->used[TEXT_AREA];
29559 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29560 if (EQ (g->object, object)
29561 && startpos <= g->charpos && g->charpos < endpos)
29563 hlinfo->mouse_face_beg_row
29564 = MATRIX_ROW_VPOS (r, w->current_matrix);
29565 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29566 hlinfo->mouse_face_beg_x = gx;
29567 found = true;
29568 break;
29571 else
29573 struct glyph *g1;
29575 e = r->glyphs[TEXT_AREA];
29576 g = e + r->used[TEXT_AREA];
29577 for ( ; g > e; --g)
29578 if (EQ ((g-1)->object, object)
29579 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29581 hlinfo->mouse_face_beg_row
29582 = MATRIX_ROW_VPOS (r, w->current_matrix);
29583 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29584 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29585 gx += g1->pixel_width;
29586 hlinfo->mouse_face_beg_x = gx;
29587 found = true;
29588 break;
29591 if (found)
29592 break;
29595 if (!found)
29596 return;
29598 /* Starting with the next row, look for the first row which does NOT
29599 include any glyphs whose positions are in the range. */
29600 for (++r; r->enabled_p && r->y < yb; ++r)
29602 g = r->glyphs[TEXT_AREA];
29603 e = g + r->used[TEXT_AREA];
29604 found = false;
29605 for ( ; g < e; ++g)
29606 if (EQ (g->object, object)
29607 && startpos <= g->charpos && g->charpos < endpos)
29609 found = true;
29610 break;
29612 if (!found)
29613 break;
29616 /* The highlighted region ends on the previous row. */
29617 r--;
29619 /* Set the end row. */
29620 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29622 /* Compute and set the end column and the end column's horizontal
29623 pixel coordinate. */
29624 if (!r->reversed_p)
29626 g = r->glyphs[TEXT_AREA];
29627 e = g + r->used[TEXT_AREA];
29628 for ( ; e > g; --e)
29629 if (EQ ((e-1)->object, object)
29630 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29631 break;
29632 hlinfo->mouse_face_end_col = e - g;
29634 for (gx = r->x; g < e; ++g)
29635 gx += g->pixel_width;
29636 hlinfo->mouse_face_end_x = gx;
29638 else
29640 e = r->glyphs[TEXT_AREA];
29641 g = e + r->used[TEXT_AREA];
29642 for (gx = r->x ; e < g; ++e)
29644 if (EQ (e->object, object)
29645 && startpos <= e->charpos && e->charpos < endpos)
29646 break;
29647 gx += e->pixel_width;
29649 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29650 hlinfo->mouse_face_end_x = gx;
29654 #ifdef HAVE_WINDOW_SYSTEM
29656 /* See if position X, Y is within a hot-spot of an image. */
29658 static bool
29659 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29661 if (!CONSP (hot_spot))
29662 return false;
29664 if (EQ (XCAR (hot_spot), Qrect))
29666 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29667 Lisp_Object rect = XCDR (hot_spot);
29668 Lisp_Object tem;
29669 if (!CONSP (rect))
29670 return false;
29671 if (!CONSP (XCAR (rect)))
29672 return false;
29673 if (!CONSP (XCDR (rect)))
29674 return false;
29675 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29676 return false;
29677 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29678 return false;
29679 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29680 return false;
29681 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29682 return false;
29683 return true;
29685 else if (EQ (XCAR (hot_spot), Qcircle))
29687 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29688 Lisp_Object circ = XCDR (hot_spot);
29689 Lisp_Object lr, lx0, ly0;
29690 if (CONSP (circ)
29691 && CONSP (XCAR (circ))
29692 && (lr = XCDR (circ), NUMBERP (lr))
29693 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29694 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29696 double r = XFLOATINT (lr);
29697 double dx = XINT (lx0) - x;
29698 double dy = XINT (ly0) - y;
29699 return (dx * dx + dy * dy <= r * r);
29702 else if (EQ (XCAR (hot_spot), Qpoly))
29704 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29705 if (VECTORP (XCDR (hot_spot)))
29707 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29708 Lisp_Object *poly = v->contents;
29709 ptrdiff_t n = v->header.size;
29710 ptrdiff_t i;
29711 bool inside = false;
29712 Lisp_Object lx, ly;
29713 int x0, y0;
29715 /* Need an even number of coordinates, and at least 3 edges. */
29716 if (n < 6 || n & 1)
29717 return false;
29719 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29720 If count is odd, we are inside polygon. Pixels on edges
29721 may or may not be included depending on actual geometry of the
29722 polygon. */
29723 if ((lx = poly[n-2], !INTEGERP (lx))
29724 || (ly = poly[n-1], !INTEGERP (lx)))
29725 return false;
29726 x0 = XINT (lx), y0 = XINT (ly);
29727 for (i = 0; i < n; i += 2)
29729 int x1 = x0, y1 = y0;
29730 if ((lx = poly[i], !INTEGERP (lx))
29731 || (ly = poly[i+1], !INTEGERP (ly)))
29732 return false;
29733 x0 = XINT (lx), y0 = XINT (ly);
29735 /* Does this segment cross the X line? */
29736 if (x0 >= x)
29738 if (x1 >= x)
29739 continue;
29741 else if (x1 < x)
29742 continue;
29743 if (y > y0 && y > y1)
29744 continue;
29745 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29746 inside = !inside;
29748 return inside;
29751 return false;
29754 Lisp_Object
29755 find_hot_spot (Lisp_Object map, int x, int y)
29757 while (CONSP (map))
29759 if (CONSP (XCAR (map))
29760 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29761 return XCAR (map);
29762 map = XCDR (map);
29765 return Qnil;
29768 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29769 3, 3, 0,
29770 doc: /* Lookup in image map MAP coordinates X and Y.
29771 An image map is an alist where each element has the format (AREA ID PLIST).
29772 An AREA is specified as either a rectangle, a circle, or a polygon:
29773 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29774 pixel coordinates of the upper left and bottom right corners.
29775 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29776 and the radius of the circle; r may be a float or integer.
29777 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29778 vector describes one corner in the polygon.
29779 Returns the alist element for the first matching AREA in MAP. */)
29780 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29782 if (NILP (map))
29783 return Qnil;
29785 CHECK_NUMBER (x);
29786 CHECK_NUMBER (y);
29788 return find_hot_spot (map,
29789 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29790 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29792 #endif /* HAVE_WINDOW_SYSTEM */
29795 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29796 static void
29797 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29799 #ifdef HAVE_WINDOW_SYSTEM
29800 if (!FRAME_WINDOW_P (f))
29801 return;
29803 /* Do not change cursor shape while dragging mouse. */
29804 if (EQ (do_mouse_tracking, Qdragging))
29805 return;
29807 if (!NILP (pointer))
29809 if (EQ (pointer, Qarrow))
29810 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29811 else if (EQ (pointer, Qhand))
29812 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29813 else if (EQ (pointer, Qtext))
29814 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29815 else if (EQ (pointer, intern ("hdrag")))
29816 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29817 else if (EQ (pointer, intern ("nhdrag")))
29818 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29819 # ifdef HAVE_X_WINDOWS
29820 else if (EQ (pointer, intern ("vdrag")))
29821 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29822 # endif
29823 else if (EQ (pointer, intern ("hourglass")))
29824 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29825 else if (EQ (pointer, Qmodeline))
29826 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29827 else
29828 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29831 if (cursor != No_Cursor)
29832 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29833 #endif
29836 /* Take proper action when mouse has moved to the mode or header line
29837 or marginal area AREA of window W, x-position X and y-position Y.
29838 X is relative to the start of the text display area of W, so the
29839 width of bitmap areas and scroll bars must be subtracted to get a
29840 position relative to the start of the mode line. */
29842 static void
29843 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29844 enum window_part area)
29846 struct window *w = XWINDOW (window);
29847 struct frame *f = XFRAME (w->frame);
29848 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29849 #ifdef HAVE_WINDOW_SYSTEM
29850 Display_Info *dpyinfo;
29851 #endif
29852 Cursor cursor = No_Cursor;
29853 Lisp_Object pointer = Qnil;
29854 int dx, dy, width, height;
29855 ptrdiff_t charpos;
29856 Lisp_Object string, object = Qnil;
29857 Lisp_Object pos UNINIT;
29858 Lisp_Object mouse_face;
29859 int original_x_pixel = x;
29860 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29861 struct glyph_row *row UNINIT;
29863 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29865 int x0;
29866 struct glyph *end;
29868 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29869 returns them in row/column units! */
29870 string = mode_line_string (w, area, &x, &y, &charpos,
29871 &object, &dx, &dy, &width, &height);
29873 row = (area == ON_MODE_LINE
29874 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29875 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29877 /* Find the glyph under the mouse pointer. */
29878 if (row->mode_line_p && row->enabled_p)
29880 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29881 end = glyph + row->used[TEXT_AREA];
29883 for (x0 = original_x_pixel;
29884 glyph < end && x0 >= glyph->pixel_width;
29885 ++glyph)
29886 x0 -= glyph->pixel_width;
29888 if (glyph >= end)
29889 glyph = NULL;
29892 else
29894 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29895 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29896 returns them in row/column units! */
29897 string = marginal_area_string (w, area, &x, &y, &charpos,
29898 &object, &dx, &dy, &width, &height);
29901 Lisp_Object help = Qnil;
29903 #ifdef HAVE_WINDOW_SYSTEM
29904 if (IMAGEP (object))
29906 Lisp_Object image_map, hotspot;
29907 if ((image_map = Fplist_get (XCDR (object), QCmap),
29908 !NILP (image_map))
29909 && (hotspot = find_hot_spot (image_map, dx, dy),
29910 CONSP (hotspot))
29911 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29913 Lisp_Object plist;
29915 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29916 If so, we could look for mouse-enter, mouse-leave
29917 properties in PLIST (and do something...). */
29918 hotspot = XCDR (hotspot);
29919 if (CONSP (hotspot)
29920 && (plist = XCAR (hotspot), CONSP (plist)))
29922 pointer = Fplist_get (plist, Qpointer);
29923 if (NILP (pointer))
29924 pointer = Qhand;
29925 help = Fplist_get (plist, Qhelp_echo);
29926 if (!NILP (help))
29928 help_echo_string = help;
29929 XSETWINDOW (help_echo_window, w);
29930 help_echo_object = w->contents;
29931 help_echo_pos = charpos;
29935 if (NILP (pointer))
29936 pointer = Fplist_get (XCDR (object), QCpointer);
29938 #endif /* HAVE_WINDOW_SYSTEM */
29940 if (STRINGP (string))
29941 pos = make_number (charpos);
29943 /* Set the help text and mouse pointer. If the mouse is on a part
29944 of the mode line without any text (e.g. past the right edge of
29945 the mode line text), use the default help text and pointer. */
29946 if (STRINGP (string) || area == ON_MODE_LINE)
29948 /* Arrange to display the help by setting the global variables
29949 help_echo_string, help_echo_object, and help_echo_pos. */
29950 if (NILP (help))
29952 if (STRINGP (string))
29953 help = Fget_text_property (pos, Qhelp_echo, string);
29955 if (!NILP (help))
29957 help_echo_string = help;
29958 XSETWINDOW (help_echo_window, w);
29959 help_echo_object = string;
29960 help_echo_pos = charpos;
29962 else if (area == ON_MODE_LINE)
29964 Lisp_Object default_help
29965 = buffer_local_value (Qmode_line_default_help_echo,
29966 w->contents);
29968 if (STRINGP (default_help))
29970 help_echo_string = default_help;
29971 XSETWINDOW (help_echo_window, w);
29972 help_echo_object = Qnil;
29973 help_echo_pos = -1;
29978 #ifdef HAVE_WINDOW_SYSTEM
29979 /* Change the mouse pointer according to what is under it. */
29980 if (FRAME_WINDOW_P (f))
29982 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29983 || minibuf_level
29984 || NILP (Vresize_mini_windows));
29986 dpyinfo = FRAME_DISPLAY_INFO (f);
29987 if (STRINGP (string))
29989 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29991 if (NILP (pointer))
29992 pointer = Fget_text_property (pos, Qpointer, string);
29994 /* Change the mouse pointer according to what is under X/Y. */
29995 if (NILP (pointer)
29996 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29998 Lisp_Object map;
29999 map = Fget_text_property (pos, Qlocal_map, string);
30000 if (!KEYMAPP (map))
30001 map = Fget_text_property (pos, Qkeymap, string);
30002 if (!KEYMAPP (map) && draggable)
30003 cursor = dpyinfo->vertical_scroll_bar_cursor;
30006 else if (draggable)
30007 /* Default mode-line pointer. */
30008 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30010 #endif
30013 /* Change the mouse face according to what is under X/Y. */
30014 bool mouse_face_shown = false;
30015 if (STRINGP (string))
30017 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30018 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30019 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30020 && glyph)
30022 Lisp_Object b, e;
30024 struct glyph * tmp_glyph;
30026 int gpos;
30027 int gseq_length;
30028 int total_pixel_width;
30029 ptrdiff_t begpos, endpos, ignore;
30031 int vpos, hpos;
30033 b = Fprevious_single_property_change (make_number (charpos + 1),
30034 Qmouse_face, string, Qnil);
30035 if (NILP (b))
30036 begpos = 0;
30037 else
30038 begpos = XINT (b);
30040 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30041 if (NILP (e))
30042 endpos = SCHARS (string);
30043 else
30044 endpos = XINT (e);
30046 /* Calculate the glyph position GPOS of GLYPH in the
30047 displayed string, relative to the beginning of the
30048 highlighted part of the string.
30050 Note: GPOS is different from CHARPOS. CHARPOS is the
30051 position of GLYPH in the internal string object. A mode
30052 line string format has structures which are converted to
30053 a flattened string by the Emacs Lisp interpreter. The
30054 internal string is an element of those structures. The
30055 displayed string is the flattened string. */
30056 tmp_glyph = row_start_glyph;
30057 while (tmp_glyph < glyph
30058 && (!(EQ (tmp_glyph->object, glyph->object)
30059 && begpos <= tmp_glyph->charpos
30060 && tmp_glyph->charpos < endpos)))
30061 tmp_glyph++;
30062 gpos = glyph - tmp_glyph;
30064 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30065 the highlighted part of the displayed string to which
30066 GLYPH belongs. Note: GSEQ_LENGTH is different from
30067 SCHARS (STRING), because the latter returns the length of
30068 the internal string. */
30069 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30070 tmp_glyph > glyph
30071 && (!(EQ (tmp_glyph->object, glyph->object)
30072 && begpos <= tmp_glyph->charpos
30073 && tmp_glyph->charpos < endpos));
30074 tmp_glyph--)
30076 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30078 /* Calculate the total pixel width of all the glyphs between
30079 the beginning of the highlighted area and GLYPH. */
30080 total_pixel_width = 0;
30081 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30082 total_pixel_width += tmp_glyph->pixel_width;
30084 /* Pre calculation of re-rendering position. Note: X is in
30085 column units here, after the call to mode_line_string or
30086 marginal_area_string. */
30087 hpos = x - gpos;
30088 vpos = (area == ON_MODE_LINE
30089 ? (w->current_matrix)->nrows - 1
30090 : 0);
30092 /* If GLYPH's position is included in the region that is
30093 already drawn in mouse face, we have nothing to do. */
30094 if ( EQ (window, hlinfo->mouse_face_window)
30095 && (!row->reversed_p
30096 ? (hlinfo->mouse_face_beg_col <= hpos
30097 && hpos < hlinfo->mouse_face_end_col)
30098 /* In R2L rows we swap BEG and END, see below. */
30099 : (hlinfo->mouse_face_end_col <= hpos
30100 && hpos < hlinfo->mouse_face_beg_col))
30101 && hlinfo->mouse_face_beg_row == vpos )
30102 return;
30104 if (clear_mouse_face (hlinfo))
30105 cursor = No_Cursor;
30107 if (!row->reversed_p)
30109 hlinfo->mouse_face_beg_col = hpos;
30110 hlinfo->mouse_face_beg_x = original_x_pixel
30111 - (total_pixel_width + dx);
30112 hlinfo->mouse_face_end_col = hpos + gseq_length;
30113 hlinfo->mouse_face_end_x = 0;
30115 else
30117 /* In R2L rows, show_mouse_face expects BEG and END
30118 coordinates to be swapped. */
30119 hlinfo->mouse_face_end_col = hpos;
30120 hlinfo->mouse_face_end_x = original_x_pixel
30121 - (total_pixel_width + dx);
30122 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30123 hlinfo->mouse_face_beg_x = 0;
30126 hlinfo->mouse_face_beg_row = vpos;
30127 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30128 hlinfo->mouse_face_past_end = false;
30129 hlinfo->mouse_face_window = window;
30131 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30132 charpos,
30133 0, &ignore,
30134 glyph->face_id,
30135 true);
30136 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30137 mouse_face_shown = true;
30139 if (NILP (pointer))
30140 pointer = Qhand;
30144 /* If mouse-face doesn't need to be shown, clear any existing
30145 mouse-face. */
30146 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30147 clear_mouse_face (hlinfo);
30149 define_frame_cursor1 (f, cursor, pointer);
30153 /* EXPORT:
30154 Take proper action when the mouse has moved to position X, Y on
30155 frame F with regards to highlighting portions of display that have
30156 mouse-face properties. Also de-highlight portions of display where
30157 the mouse was before, set the mouse pointer shape as appropriate
30158 for the mouse coordinates, and activate help echo (tooltips).
30159 X and Y can be negative or out of range. */
30161 void
30162 note_mouse_highlight (struct frame *f, int x, int y)
30164 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30165 enum window_part part = ON_NOTHING;
30166 Lisp_Object window;
30167 struct window *w;
30168 Cursor cursor = No_Cursor;
30169 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30170 struct buffer *b;
30172 /* When a menu is active, don't highlight because this looks odd. */
30173 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30174 if (popup_activated ())
30175 return;
30176 #endif
30178 if (!f->glyphs_initialized_p
30179 || f->pointer_invisible)
30180 return;
30182 hlinfo->mouse_face_mouse_x = x;
30183 hlinfo->mouse_face_mouse_y = y;
30184 hlinfo->mouse_face_mouse_frame = f;
30186 if (hlinfo->mouse_face_defer)
30187 return;
30189 /* Which window is that in? */
30190 window = window_from_coordinates (f, x, y, &part, true);
30192 /* If displaying active text in another window, clear that. */
30193 if (! EQ (window, hlinfo->mouse_face_window)
30194 /* Also clear if we move out of text area in same window. */
30195 || (!NILP (hlinfo->mouse_face_window)
30196 && !NILP (window)
30197 && part != ON_TEXT
30198 && part != ON_MODE_LINE
30199 && part != ON_HEADER_LINE))
30200 clear_mouse_face (hlinfo);
30202 /* Not on a window -> return. */
30203 if (!WINDOWP (window))
30204 return;
30206 /* Reset help_echo_string. It will get recomputed below. */
30207 help_echo_string = Qnil;
30209 /* Convert to window-relative pixel coordinates. */
30210 w = XWINDOW (window);
30211 frame_to_window_pixel_xy (w, &x, &y);
30213 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30214 /* Handle tool-bar window differently since it doesn't display a
30215 buffer. */
30216 if (EQ (window, f->tool_bar_window))
30218 note_tool_bar_highlight (f, x, y);
30219 return;
30221 #endif
30223 /* Mouse is on the mode, header line or margin? */
30224 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30225 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30227 note_mode_line_or_margin_highlight (window, x, y, part);
30229 #ifdef HAVE_WINDOW_SYSTEM
30230 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30232 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30233 /* Show non-text cursor (Bug#16647). */
30234 goto set_cursor;
30236 else
30237 #endif
30238 return;
30241 #ifdef HAVE_WINDOW_SYSTEM
30242 if (part == ON_VERTICAL_BORDER)
30244 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30245 help_echo_string = build_string ("drag-mouse-1: resize");
30247 else if (part == ON_RIGHT_DIVIDER)
30249 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30250 help_echo_string = build_string ("drag-mouse-1: resize");
30252 else if (part == ON_BOTTOM_DIVIDER)
30253 if (! WINDOW_BOTTOMMOST_P (w)
30254 || minibuf_level
30255 || NILP (Vresize_mini_windows))
30257 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30258 help_echo_string = build_string ("drag-mouse-1: resize");
30260 else
30261 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30262 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30263 || part == ON_VERTICAL_SCROLL_BAR
30264 || part == ON_HORIZONTAL_SCROLL_BAR)
30265 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30266 else
30267 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30268 #endif
30270 /* Are we in a window whose display is up to date?
30271 And verify the buffer's text has not changed. */
30272 b = XBUFFER (w->contents);
30273 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30275 int hpos, vpos, dx, dy, area = LAST_AREA;
30276 ptrdiff_t pos;
30277 struct glyph *glyph;
30278 Lisp_Object object;
30279 Lisp_Object mouse_face = Qnil, position;
30280 Lisp_Object *overlay_vec = NULL;
30281 ptrdiff_t i, noverlays;
30282 struct buffer *obuf;
30283 ptrdiff_t obegv, ozv;
30284 bool same_region;
30286 /* Find the glyph under X/Y. */
30287 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30289 #ifdef HAVE_WINDOW_SYSTEM
30290 /* Look for :pointer property on image. */
30291 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30293 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30294 if (img != NULL && IMAGEP (img->spec))
30296 Lisp_Object image_map, hotspot;
30297 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30298 !NILP (image_map))
30299 && (hotspot = find_hot_spot (image_map,
30300 glyph->slice.img.x + dx,
30301 glyph->slice.img.y + dy),
30302 CONSP (hotspot))
30303 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30305 Lisp_Object plist;
30307 /* Could check XCAR (hotspot) to see if we enter/leave
30308 this hot-spot.
30309 If so, we could look for mouse-enter, mouse-leave
30310 properties in PLIST (and do something...). */
30311 hotspot = XCDR (hotspot);
30312 if (CONSP (hotspot)
30313 && (plist = XCAR (hotspot), CONSP (plist)))
30315 pointer = Fplist_get (plist, Qpointer);
30316 if (NILP (pointer))
30317 pointer = Qhand;
30318 help_echo_string = Fplist_get (plist, Qhelp_echo);
30319 if (!NILP (help_echo_string))
30321 help_echo_window = window;
30322 help_echo_object = glyph->object;
30323 help_echo_pos = glyph->charpos;
30327 if (NILP (pointer))
30328 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30331 #endif /* HAVE_WINDOW_SYSTEM */
30333 /* Clear mouse face if X/Y not over text. */
30334 if (glyph == NULL
30335 || area != TEXT_AREA
30336 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30337 /* Glyph's OBJECT is nil for glyphs inserted by the
30338 display engine for its internal purposes, like truncation
30339 and continuation glyphs and blanks beyond the end of
30340 line's text on text terminals. If we are over such a
30341 glyph, we are not over any text. */
30342 || NILP (glyph->object)
30343 /* R2L rows have a stretch glyph at their front, which
30344 stands for no text, whereas L2R rows have no glyphs at
30345 all beyond the end of text. Treat such stretch glyphs
30346 like we do with NULL glyphs in L2R rows. */
30347 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30348 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30349 && glyph->type == STRETCH_GLYPH
30350 && glyph->avoid_cursor_p))
30352 if (clear_mouse_face (hlinfo))
30353 cursor = No_Cursor;
30354 if (FRAME_WINDOW_P (f) && NILP (pointer))
30356 #ifdef HAVE_WINDOW_SYSTEM
30357 if (area != TEXT_AREA)
30358 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30359 else
30360 pointer = Vvoid_text_area_pointer;
30361 #endif
30363 goto set_cursor;
30366 pos = glyph->charpos;
30367 object = glyph->object;
30368 if (!STRINGP (object) && !BUFFERP (object))
30369 goto set_cursor;
30371 /* If we get an out-of-range value, return now; avoid an error. */
30372 if (BUFFERP (object) && pos > BUF_Z (b))
30373 goto set_cursor;
30375 /* Make the window's buffer temporarily current for
30376 overlays_at and compute_char_face. */
30377 obuf = current_buffer;
30378 current_buffer = b;
30379 obegv = BEGV;
30380 ozv = ZV;
30381 BEGV = BEG;
30382 ZV = Z;
30384 /* Is this char mouse-active or does it have help-echo? */
30385 position = make_number (pos);
30387 USE_SAFE_ALLOCA;
30389 if (BUFFERP (object))
30391 /* Put all the overlays we want in a vector in overlay_vec. */
30392 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30393 /* Sort overlays into increasing priority order. */
30394 noverlays = sort_overlays (overlay_vec, noverlays, w);
30396 else
30397 noverlays = 0;
30399 if (NILP (Vmouse_highlight))
30401 clear_mouse_face (hlinfo);
30402 goto check_help_echo;
30405 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30407 if (same_region)
30408 cursor = No_Cursor;
30410 /* Check mouse-face highlighting. */
30411 if (! same_region
30412 /* If there exists an overlay with mouse-face overlapping
30413 the one we are currently highlighting, we have to
30414 check if we enter the overlapping overlay, and then
30415 highlight only that. */
30416 || (OVERLAYP (hlinfo->mouse_face_overlay)
30417 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30419 /* Find the highest priority overlay with a mouse-face. */
30420 Lisp_Object overlay = Qnil;
30421 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30423 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30424 if (!NILP (mouse_face))
30425 overlay = overlay_vec[i];
30428 /* If we're highlighting the same overlay as before, there's
30429 no need to do that again. */
30430 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30431 goto check_help_echo;
30432 hlinfo->mouse_face_overlay = overlay;
30434 /* Clear the display of the old active region, if any. */
30435 if (clear_mouse_face (hlinfo))
30436 cursor = No_Cursor;
30438 /* If no overlay applies, get a text property. */
30439 if (NILP (overlay))
30440 mouse_face = Fget_text_property (position, Qmouse_face, object);
30442 /* Next, compute the bounds of the mouse highlighting and
30443 display it. */
30444 if (!NILP (mouse_face) && STRINGP (object))
30446 /* The mouse-highlighting comes from a display string
30447 with a mouse-face. */
30448 Lisp_Object s, e;
30449 ptrdiff_t ignore;
30451 s = Fprevious_single_property_change
30452 (make_number (pos + 1), Qmouse_face, object, Qnil);
30453 e = Fnext_single_property_change
30454 (position, Qmouse_face, object, Qnil);
30455 if (NILP (s))
30456 s = make_number (0);
30457 if (NILP (e))
30458 e = make_number (SCHARS (object));
30459 mouse_face_from_string_pos (w, hlinfo, object,
30460 XINT (s), XINT (e));
30461 hlinfo->mouse_face_past_end = false;
30462 hlinfo->mouse_face_window = window;
30463 hlinfo->mouse_face_face_id
30464 = face_at_string_position (w, object, pos, 0, &ignore,
30465 glyph->face_id, true);
30466 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30467 cursor = No_Cursor;
30469 else
30471 /* The mouse-highlighting, if any, comes from an overlay
30472 or text property in the buffer. */
30473 Lisp_Object buffer UNINIT;
30474 Lisp_Object disp_string UNINIT;
30476 if (STRINGP (object))
30478 /* If we are on a display string with no mouse-face,
30479 check if the text under it has one. */
30480 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30481 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30482 pos = string_buffer_position (object, start);
30483 if (pos > 0)
30485 mouse_face = get_char_property_and_overlay
30486 (make_number (pos), Qmouse_face, w->contents, &overlay);
30487 buffer = w->contents;
30488 disp_string = object;
30491 else
30493 buffer = object;
30494 disp_string = Qnil;
30497 if (!NILP (mouse_face))
30499 Lisp_Object before, after;
30500 Lisp_Object before_string, after_string;
30501 /* To correctly find the limits of mouse highlight
30502 in a bidi-reordered buffer, we must not use the
30503 optimization of limiting the search in
30504 previous-single-property-change and
30505 next-single-property-change, because
30506 rows_from_pos_range needs the real start and end
30507 positions to DTRT in this case. That's because
30508 the first row visible in a window does not
30509 necessarily display the character whose position
30510 is the smallest. */
30511 Lisp_Object lim1
30512 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30513 ? Fmarker_position (w->start)
30514 : Qnil;
30515 Lisp_Object lim2
30516 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30517 ? make_number (BUF_Z (XBUFFER (buffer))
30518 - w->window_end_pos)
30519 : Qnil;
30521 if (NILP (overlay))
30523 /* Handle the text property case. */
30524 before = Fprevious_single_property_change
30525 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30526 after = Fnext_single_property_change
30527 (make_number (pos), Qmouse_face, buffer, lim2);
30528 before_string = after_string = Qnil;
30530 else
30532 /* Handle the overlay case. */
30533 before = Foverlay_start (overlay);
30534 after = Foverlay_end (overlay);
30535 before_string = Foverlay_get (overlay, Qbefore_string);
30536 after_string = Foverlay_get (overlay, Qafter_string);
30538 if (!STRINGP (before_string)) before_string = Qnil;
30539 if (!STRINGP (after_string)) after_string = Qnil;
30542 mouse_face_from_buffer_pos (window, hlinfo, pos,
30543 NILP (before)
30545 : XFASTINT (before),
30546 NILP (after)
30547 ? BUF_Z (XBUFFER (buffer))
30548 : XFASTINT (after),
30549 before_string, after_string,
30550 disp_string);
30551 cursor = No_Cursor;
30556 check_help_echo:
30558 /* Look for a `help-echo' property. */
30559 if (NILP (help_echo_string)) {
30560 Lisp_Object help, overlay;
30562 /* Check overlays first. */
30563 help = overlay = Qnil;
30564 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30566 overlay = overlay_vec[i];
30567 help = Foverlay_get (overlay, Qhelp_echo);
30570 if (!NILP (help))
30572 help_echo_string = help;
30573 help_echo_window = window;
30574 help_echo_object = overlay;
30575 help_echo_pos = pos;
30577 else
30579 Lisp_Object obj = glyph->object;
30580 ptrdiff_t charpos = glyph->charpos;
30582 /* Try text properties. */
30583 if (STRINGP (obj)
30584 && charpos >= 0
30585 && charpos < SCHARS (obj))
30587 help = Fget_text_property (make_number (charpos),
30588 Qhelp_echo, obj);
30589 if (NILP (help))
30591 /* If the string itself doesn't specify a help-echo,
30592 see if the buffer text ``under'' it does. */
30593 struct glyph_row *r
30594 = MATRIX_ROW (w->current_matrix, vpos);
30595 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30596 ptrdiff_t p = string_buffer_position (obj, start);
30597 if (p > 0)
30599 help = Fget_char_property (make_number (p),
30600 Qhelp_echo, w->contents);
30601 if (!NILP (help))
30603 charpos = p;
30604 obj = w->contents;
30609 else if (BUFFERP (obj)
30610 && charpos >= BEGV
30611 && charpos < ZV)
30612 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30613 obj);
30615 if (!NILP (help))
30617 help_echo_string = help;
30618 help_echo_window = window;
30619 help_echo_object = obj;
30620 help_echo_pos = charpos;
30625 #ifdef HAVE_WINDOW_SYSTEM
30626 /* Look for a `pointer' property. */
30627 if (FRAME_WINDOW_P (f) && NILP (pointer))
30629 /* Check overlays first. */
30630 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30631 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30633 if (NILP (pointer))
30635 Lisp_Object obj = glyph->object;
30636 ptrdiff_t charpos = glyph->charpos;
30638 /* Try text properties. */
30639 if (STRINGP (obj)
30640 && charpos >= 0
30641 && charpos < SCHARS (obj))
30643 pointer = Fget_text_property (make_number (charpos),
30644 Qpointer, obj);
30645 if (NILP (pointer))
30647 /* If the string itself doesn't specify a pointer,
30648 see if the buffer text ``under'' it does. */
30649 struct glyph_row *r
30650 = MATRIX_ROW (w->current_matrix, vpos);
30651 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30652 ptrdiff_t p = string_buffer_position (obj, start);
30653 if (p > 0)
30654 pointer = Fget_char_property (make_number (p),
30655 Qpointer, w->contents);
30658 else if (BUFFERP (obj)
30659 && charpos >= BEGV
30660 && charpos < ZV)
30661 pointer = Fget_text_property (make_number (charpos),
30662 Qpointer, obj);
30665 #endif /* HAVE_WINDOW_SYSTEM */
30667 BEGV = obegv;
30668 ZV = ozv;
30669 current_buffer = obuf;
30670 SAFE_FREE ();
30673 set_cursor:
30674 define_frame_cursor1 (f, cursor, pointer);
30678 /* EXPORT for RIF:
30679 Clear any mouse-face on window W. This function is part of the
30680 redisplay interface, and is called from try_window_id and similar
30681 functions to ensure the mouse-highlight is off. */
30683 void
30684 x_clear_window_mouse_face (struct window *w)
30686 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30687 Lisp_Object window;
30689 block_input ();
30690 XSETWINDOW (window, w);
30691 if (EQ (window, hlinfo->mouse_face_window))
30692 clear_mouse_face (hlinfo);
30693 unblock_input ();
30697 /* EXPORT:
30698 Just discard the mouse face information for frame F, if any.
30699 This is used when the size of F is changed. */
30701 void
30702 cancel_mouse_face (struct frame *f)
30704 Lisp_Object window;
30705 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30707 window = hlinfo->mouse_face_window;
30708 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30709 reset_mouse_highlight (hlinfo);
30714 /***********************************************************************
30715 Exposure Events
30716 ***********************************************************************/
30718 #ifdef HAVE_WINDOW_SYSTEM
30720 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30721 which intersects rectangle R. R is in window-relative coordinates. */
30723 static void
30724 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30725 enum glyph_row_area area)
30727 struct glyph *first = row->glyphs[area];
30728 struct glyph *end = row->glyphs[area] + row->used[area];
30729 struct glyph *last;
30730 int first_x, start_x, x;
30732 if (area == TEXT_AREA && row->fill_line_p)
30733 /* If row extends face to end of line write the whole line. */
30734 draw_glyphs (w, 0, row, area,
30735 0, row->used[area],
30736 DRAW_NORMAL_TEXT, 0);
30737 else
30739 /* Set START_X to the window-relative start position for drawing glyphs of
30740 AREA. The first glyph of the text area can be partially visible.
30741 The first glyphs of other areas cannot. */
30742 start_x = window_box_left_offset (w, area);
30743 x = start_x;
30744 if (area == TEXT_AREA)
30745 x += row->x;
30747 /* Find the first glyph that must be redrawn. */
30748 while (first < end
30749 && x + first->pixel_width < r->x)
30751 x += first->pixel_width;
30752 ++first;
30755 /* Find the last one. */
30756 last = first;
30757 first_x = x;
30758 /* Use a signed int intermediate value to avoid catastrophic
30759 failures due to comparison between signed and unsigned, when
30760 x is negative (can happen for wide images that are hscrolled). */
30761 int r_end = r->x + r->width;
30762 while (last < end && x < r_end)
30764 x += last->pixel_width;
30765 ++last;
30768 /* Repaint. */
30769 if (last > first)
30770 draw_glyphs (w, first_x - start_x, row, area,
30771 first - row->glyphs[area], last - row->glyphs[area],
30772 DRAW_NORMAL_TEXT, 0);
30777 /* Redraw the parts of the glyph row ROW on window W intersecting
30778 rectangle R. R is in window-relative coordinates. Value is
30779 true if mouse-face was overwritten. */
30781 static bool
30782 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30784 eassert (row->enabled_p);
30786 if (row->mode_line_p || w->pseudo_window_p)
30787 draw_glyphs (w, 0, row, TEXT_AREA,
30788 0, row->used[TEXT_AREA],
30789 DRAW_NORMAL_TEXT, 0);
30790 else
30792 if (row->used[LEFT_MARGIN_AREA])
30793 expose_area (w, row, r, LEFT_MARGIN_AREA);
30794 if (row->used[TEXT_AREA])
30795 expose_area (w, row, r, TEXT_AREA);
30796 if (row->used[RIGHT_MARGIN_AREA])
30797 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30798 draw_row_fringe_bitmaps (w, row);
30801 return row->mouse_face_p;
30805 /* Redraw those parts of glyphs rows during expose event handling that
30806 overlap other rows. Redrawing of an exposed line writes over parts
30807 of lines overlapping that exposed line; this function fixes that.
30809 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30810 row in W's current matrix that is exposed and overlaps other rows.
30811 LAST_OVERLAPPING_ROW is the last such row. */
30813 static void
30814 expose_overlaps (struct window *w,
30815 struct glyph_row *first_overlapping_row,
30816 struct glyph_row *last_overlapping_row,
30817 XRectangle *r)
30819 struct glyph_row *row;
30821 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30822 if (row->overlapping_p)
30824 eassert (row->enabled_p && !row->mode_line_p);
30826 row->clip = r;
30827 if (row->used[LEFT_MARGIN_AREA])
30828 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30830 if (row->used[TEXT_AREA])
30831 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30833 if (row->used[RIGHT_MARGIN_AREA])
30834 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30835 row->clip = NULL;
30840 /* Return true if W's cursor intersects rectangle R. */
30842 static bool
30843 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30845 XRectangle cr, result;
30846 struct glyph *cursor_glyph;
30847 struct glyph_row *row;
30849 if (w->phys_cursor.vpos >= 0
30850 && w->phys_cursor.vpos < w->current_matrix->nrows
30851 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30852 row->enabled_p)
30853 && row->cursor_in_fringe_p)
30855 /* Cursor is in the fringe. */
30856 cr.x = window_box_right_offset (w,
30857 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30858 ? RIGHT_MARGIN_AREA
30859 : TEXT_AREA));
30860 cr.y = row->y;
30861 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30862 cr.height = row->height;
30863 return x_intersect_rectangles (&cr, r, &result);
30866 cursor_glyph = get_phys_cursor_glyph (w);
30867 if (cursor_glyph)
30869 /* r is relative to W's box, but w->phys_cursor.x is relative
30870 to left edge of W's TEXT area. Adjust it. */
30871 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30872 cr.y = w->phys_cursor.y;
30873 cr.width = cursor_glyph->pixel_width;
30874 cr.height = w->phys_cursor_height;
30875 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30876 I assume the effect is the same -- and this is portable. */
30877 return x_intersect_rectangles (&cr, r, &result);
30879 /* If we don't understand the format, pretend we're not in the hot-spot. */
30880 return false;
30884 /* EXPORT:
30885 Draw a vertical window border to the right of window W if W doesn't
30886 have vertical scroll bars. */
30888 void
30889 x_draw_vertical_border (struct window *w)
30891 struct frame *f = XFRAME (WINDOW_FRAME (w));
30893 /* We could do better, if we knew what type of scroll-bar the adjacent
30894 windows (on either side) have... But we don't :-(
30895 However, I think this works ok. ++KFS 2003-04-25 */
30897 /* Redraw borders between horizontally adjacent windows. Don't
30898 do it for frames with vertical scroll bars because either the
30899 right scroll bar of a window, or the left scroll bar of its
30900 neighbor will suffice as a border. */
30901 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30902 return;
30904 /* Note: It is necessary to redraw both the left and the right
30905 borders, for when only this single window W is being
30906 redisplayed. */
30907 if (!WINDOW_RIGHTMOST_P (w)
30908 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30910 int x0, x1, y0, y1;
30912 window_box_edges (w, &x0, &y0, &x1, &y1);
30913 y1 -= 1;
30915 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30916 x1 -= 1;
30918 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30921 if (!WINDOW_LEFTMOST_P (w)
30922 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30924 int x0, x1, y0, y1;
30926 window_box_edges (w, &x0, &y0, &x1, &y1);
30927 y1 -= 1;
30929 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30930 x0 -= 1;
30932 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30937 /* Draw window dividers for window W. */
30939 void
30940 x_draw_right_divider (struct window *w)
30942 struct frame *f = WINDOW_XFRAME (w);
30944 if (w->mini || w->pseudo_window_p)
30945 return;
30946 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30948 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30949 int x1 = WINDOW_RIGHT_EDGE_X (w);
30950 int y0 = WINDOW_TOP_EDGE_Y (w);
30951 /* The bottom divider prevails. */
30952 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30954 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30958 static void
30959 x_draw_bottom_divider (struct window *w)
30961 struct frame *f = XFRAME (WINDOW_FRAME (w));
30963 if (w->mini || w->pseudo_window_p)
30964 return;
30965 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30967 int x0 = WINDOW_LEFT_EDGE_X (w);
30968 int x1 = WINDOW_RIGHT_EDGE_X (w);
30969 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30970 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30972 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30976 /* Redraw the part of window W intersection rectangle FR. Pixel
30977 coordinates in FR are frame-relative. Call this function with
30978 input blocked. Value is true if the exposure overwrites
30979 mouse-face. */
30981 static bool
30982 expose_window (struct window *w, XRectangle *fr)
30984 struct frame *f = XFRAME (w->frame);
30985 XRectangle wr, r;
30986 bool mouse_face_overwritten_p = false;
30988 /* If window is not yet fully initialized, do nothing. This can
30989 happen when toolkit scroll bars are used and a window is split.
30990 Reconfiguring the scroll bar will generate an expose for a newly
30991 created window. */
30992 if (w->current_matrix == NULL)
30993 return false;
30995 /* When we're currently updating the window, display and current
30996 matrix usually don't agree. Arrange for a thorough display
30997 later. */
30998 if (w->must_be_updated_p)
31000 SET_FRAME_GARBAGED (f);
31001 return false;
31004 /* Frame-relative pixel rectangle of W. */
31005 wr.x = WINDOW_LEFT_EDGE_X (w);
31006 wr.y = WINDOW_TOP_EDGE_Y (w);
31007 wr.width = WINDOW_PIXEL_WIDTH (w);
31008 wr.height = WINDOW_PIXEL_HEIGHT (w);
31010 if (x_intersect_rectangles (fr, &wr, &r))
31012 int yb = window_text_bottom_y (w);
31013 struct glyph_row *row;
31014 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31016 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31017 r.x, r.y, r.width, r.height));
31019 /* Convert to window coordinates. */
31020 r.x -= WINDOW_LEFT_EDGE_X (w);
31021 r.y -= WINDOW_TOP_EDGE_Y (w);
31023 /* Turn off the cursor. */
31024 bool cursor_cleared_p = (!w->pseudo_window_p
31025 && phys_cursor_in_rect_p (w, &r));
31026 if (cursor_cleared_p)
31027 x_clear_cursor (w);
31029 /* If the row containing the cursor extends face to end of line,
31030 then expose_area might overwrite the cursor outside the
31031 rectangle and thus notice_overwritten_cursor might clear
31032 w->phys_cursor_on_p. We remember the original value and
31033 check later if it is changed. */
31034 bool phys_cursor_on_p = w->phys_cursor_on_p;
31036 /* Use a signed int intermediate value to avoid catastrophic
31037 failures due to comparison between signed and unsigned, when
31038 y0 or y1 is negative (can happen for tall images). */
31039 int r_bottom = r.y + r.height;
31041 /* Update lines intersecting rectangle R. */
31042 first_overlapping_row = last_overlapping_row = NULL;
31043 for (row = w->current_matrix->rows;
31044 row->enabled_p;
31045 ++row)
31047 int y0 = row->y;
31048 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31050 if ((y0 >= r.y && y0 < r_bottom)
31051 || (y1 > r.y && y1 < r_bottom)
31052 || (r.y >= y0 && r.y < y1)
31053 || (r_bottom > y0 && r_bottom < y1))
31055 /* A header line may be overlapping, but there is no need
31056 to fix overlapping areas for them. KFS 2005-02-12 */
31057 if (row->overlapping_p && !row->mode_line_p)
31059 if (first_overlapping_row == NULL)
31060 first_overlapping_row = row;
31061 last_overlapping_row = row;
31064 row->clip = fr;
31065 if (expose_line (w, row, &r))
31066 mouse_face_overwritten_p = true;
31067 row->clip = NULL;
31069 else if (row->overlapping_p)
31071 /* We must redraw a row overlapping the exposed area. */
31072 if (y0 < r.y
31073 ? y0 + row->phys_height > r.y
31074 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31076 if (first_overlapping_row == NULL)
31077 first_overlapping_row = row;
31078 last_overlapping_row = row;
31082 if (y1 >= yb)
31083 break;
31086 /* Display the mode line if there is one. */
31087 if (WINDOW_WANTS_MODELINE_P (w)
31088 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31089 row->enabled_p)
31090 && row->y < r_bottom)
31092 if (expose_line (w, row, &r))
31093 mouse_face_overwritten_p = true;
31096 if (!w->pseudo_window_p)
31098 /* Fix the display of overlapping rows. */
31099 if (first_overlapping_row)
31100 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31101 fr);
31103 /* Draw border between windows. */
31104 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31105 x_draw_right_divider (w);
31106 else
31107 x_draw_vertical_border (w);
31109 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31110 x_draw_bottom_divider (w);
31112 /* Turn the cursor on again. */
31113 if (cursor_cleared_p
31114 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31115 update_window_cursor (w, true);
31119 return mouse_face_overwritten_p;
31124 /* Redraw (parts) of all windows in the window tree rooted at W that
31125 intersect R. R contains frame pixel coordinates. Value is
31126 true if the exposure overwrites mouse-face. */
31128 static bool
31129 expose_window_tree (struct window *w, XRectangle *r)
31131 struct frame *f = XFRAME (w->frame);
31132 bool mouse_face_overwritten_p = false;
31134 while (w && !FRAME_GARBAGED_P (f))
31136 mouse_face_overwritten_p
31137 |= (WINDOWP (w->contents)
31138 ? expose_window_tree (XWINDOW (w->contents), r)
31139 : expose_window (w, r));
31141 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31144 return mouse_face_overwritten_p;
31148 /* EXPORT:
31149 Redisplay an exposed area of frame F. X and Y are the upper-left
31150 corner of the exposed rectangle. W and H are width and height of
31151 the exposed area. All are pixel values. W or H zero means redraw
31152 the entire frame. */
31154 void
31155 expose_frame (struct frame *f, int x, int y, int w, int h)
31157 XRectangle r;
31158 bool mouse_face_overwritten_p = false;
31160 TRACE ((stderr, "expose_frame "));
31162 /* No need to redraw if frame will be redrawn soon. */
31163 if (FRAME_GARBAGED_P (f))
31165 TRACE ((stderr, " garbaged\n"));
31166 return;
31169 /* If basic faces haven't been realized yet, there is no point in
31170 trying to redraw anything. This can happen when we get an expose
31171 event while Emacs is starting, e.g. by moving another window. */
31172 if (FRAME_FACE_CACHE (f) == NULL
31173 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31175 TRACE ((stderr, " no faces\n"));
31176 return;
31179 if (w == 0 || h == 0)
31181 r.x = r.y = 0;
31182 r.width = FRAME_TEXT_WIDTH (f);
31183 r.height = FRAME_TEXT_HEIGHT (f);
31185 else
31187 r.x = x;
31188 r.y = y;
31189 r.width = w;
31190 r.height = h;
31193 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31194 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31196 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31197 if (WINDOWP (f->tool_bar_window))
31198 mouse_face_overwritten_p
31199 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31200 #endif
31202 #ifdef HAVE_X_WINDOWS
31203 #ifndef MSDOS
31204 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31205 if (WINDOWP (f->menu_bar_window))
31206 mouse_face_overwritten_p
31207 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31208 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31209 #endif
31210 #endif
31212 /* Some window managers support a focus-follows-mouse style with
31213 delayed raising of frames. Imagine a partially obscured frame,
31214 and moving the mouse into partially obscured mouse-face on that
31215 frame. The visible part of the mouse-face will be highlighted,
31216 then the WM raises the obscured frame. With at least one WM, KDE
31217 2.1, Emacs is not getting any event for the raising of the frame
31218 (even tried with SubstructureRedirectMask), only Expose events.
31219 These expose events will draw text normally, i.e. not
31220 highlighted. Which means we must redo the highlight here.
31221 Subsume it under ``we love X''. --gerd 2001-08-15 */
31222 /* Included in Windows version because Windows most likely does not
31223 do the right thing if any third party tool offers
31224 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31225 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31227 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31228 if (f == hlinfo->mouse_face_mouse_frame)
31230 int mouse_x = hlinfo->mouse_face_mouse_x;
31231 int mouse_y = hlinfo->mouse_face_mouse_y;
31232 clear_mouse_face (hlinfo);
31233 note_mouse_highlight (f, mouse_x, mouse_y);
31239 /* EXPORT:
31240 Determine the intersection of two rectangles R1 and R2. Return
31241 the intersection in *RESULT. Value is true if RESULT is not
31242 empty. */
31244 bool
31245 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31247 XRectangle *left, *right;
31248 XRectangle *upper, *lower;
31249 bool intersection_p = false;
31251 /* Rearrange so that R1 is the left-most rectangle. */
31252 if (r1->x < r2->x)
31253 left = r1, right = r2;
31254 else
31255 left = r2, right = r1;
31257 /* X0 of the intersection is right.x0, if this is inside R1,
31258 otherwise there is no intersection. */
31259 if (right->x <= left->x + left->width)
31261 result->x = right->x;
31263 /* The right end of the intersection is the minimum of
31264 the right ends of left and right. */
31265 result->width = (min (left->x + left->width, right->x + right->width)
31266 - result->x);
31268 /* Same game for Y. */
31269 if (r1->y < r2->y)
31270 upper = r1, lower = r2;
31271 else
31272 upper = r2, lower = r1;
31274 /* The upper end of the intersection is lower.y0, if this is inside
31275 of upper. Otherwise, there is no intersection. */
31276 if (lower->y <= upper->y + upper->height)
31278 result->y = lower->y;
31280 /* The lower end of the intersection is the minimum of the lower
31281 ends of upper and lower. */
31282 result->height = (min (lower->y + lower->height,
31283 upper->y + upper->height)
31284 - result->y);
31285 intersection_p = true;
31289 return intersection_p;
31292 #endif /* HAVE_WINDOW_SYSTEM */
31295 /***********************************************************************
31296 Initialization
31297 ***********************************************************************/
31299 void
31300 syms_of_xdisp (void)
31302 Vwith_echo_area_save_vector = Qnil;
31303 staticpro (&Vwith_echo_area_save_vector);
31305 Vmessage_stack = Qnil;
31306 staticpro (&Vmessage_stack);
31308 /* Non-nil means don't actually do any redisplay. */
31309 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31311 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31313 DEFVAR_BOOL("inhibit-message", inhibit_message,
31314 doc: /* Non-nil means calls to `message' are not displayed.
31315 They are still logged to the *Messages* buffer. */);
31316 inhibit_message = 0;
31318 message_dolog_marker1 = Fmake_marker ();
31319 staticpro (&message_dolog_marker1);
31320 message_dolog_marker2 = Fmake_marker ();
31321 staticpro (&message_dolog_marker2);
31322 message_dolog_marker3 = Fmake_marker ();
31323 staticpro (&message_dolog_marker3);
31325 #ifdef GLYPH_DEBUG
31326 defsubr (&Sdump_frame_glyph_matrix);
31327 defsubr (&Sdump_glyph_matrix);
31328 defsubr (&Sdump_glyph_row);
31329 defsubr (&Sdump_tool_bar_row);
31330 defsubr (&Strace_redisplay);
31331 defsubr (&Strace_to_stderr);
31332 #endif
31333 #ifdef HAVE_WINDOW_SYSTEM
31334 defsubr (&Stool_bar_height);
31335 defsubr (&Slookup_image_map);
31336 #endif
31337 defsubr (&Sline_pixel_height);
31338 defsubr (&Sformat_mode_line);
31339 defsubr (&Sinvisible_p);
31340 defsubr (&Scurrent_bidi_paragraph_direction);
31341 defsubr (&Swindow_text_pixel_size);
31342 defsubr (&Smove_point_visually);
31343 defsubr (&Sbidi_find_overridden_directionality);
31345 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31346 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31347 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31348 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31349 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31350 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31351 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31352 DEFSYM (Qeval, "eval");
31353 DEFSYM (QCdata, ":data");
31355 /* Names of text properties relevant for redisplay. */
31356 DEFSYM (Qdisplay, "display");
31357 DEFSYM (Qspace_width, "space-width");
31358 DEFSYM (Qraise, "raise");
31359 DEFSYM (Qslice, "slice");
31360 DEFSYM (Qspace, "space");
31361 DEFSYM (Qmargin, "margin");
31362 DEFSYM (Qpointer, "pointer");
31363 DEFSYM (Qleft_margin, "left-margin");
31364 DEFSYM (Qright_margin, "right-margin");
31365 DEFSYM (Qcenter, "center");
31366 DEFSYM (Qline_height, "line-height");
31367 DEFSYM (QCalign_to, ":align-to");
31368 DEFSYM (QCrelative_width, ":relative-width");
31369 DEFSYM (QCrelative_height, ":relative-height");
31370 DEFSYM (QCeval, ":eval");
31371 DEFSYM (QCpropertize, ":propertize");
31372 DEFSYM (QCfile, ":file");
31373 DEFSYM (Qfontified, "fontified");
31374 DEFSYM (Qfontification_functions, "fontification-functions");
31376 /* Name of the face used to highlight trailing whitespace. */
31377 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31379 /* Name and number of the face used to highlight escape glyphs. */
31380 DEFSYM (Qescape_glyph, "escape-glyph");
31382 /* Name and number of the face used to highlight non-breaking
31383 spaces/hyphens. */
31384 DEFSYM (Qnobreak_space, "nobreak-space");
31385 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31387 /* The symbol 'image' which is the car of the lists used to represent
31388 images in Lisp. Also a tool bar style. */
31389 DEFSYM (Qimage, "image");
31391 /* Tool bar styles. */
31392 DEFSYM (Qtext, "text");
31393 DEFSYM (Qboth, "both");
31394 DEFSYM (Qboth_horiz, "both-horiz");
31395 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31397 /* The image map types. */
31398 DEFSYM (QCmap, ":map");
31399 DEFSYM (QCpointer, ":pointer");
31400 DEFSYM (Qrect, "rect");
31401 DEFSYM (Qcircle, "circle");
31402 DEFSYM (Qpoly, "poly");
31404 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31406 DEFSYM (Qgrow_only, "grow-only");
31407 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31408 DEFSYM (Qposition, "position");
31409 DEFSYM (Qbuffer_position, "buffer-position");
31410 DEFSYM (Qobject, "object");
31412 /* Cursor shapes. */
31413 DEFSYM (Qbar, "bar");
31414 DEFSYM (Qhbar, "hbar");
31415 DEFSYM (Qbox, "box");
31416 DEFSYM (Qhollow, "hollow");
31418 /* Pointer shapes. */
31419 DEFSYM (Qhand, "hand");
31420 DEFSYM (Qarrow, "arrow");
31421 /* also Qtext */
31423 DEFSYM (Qdragging, "dragging");
31425 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31427 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31428 staticpro (&list_of_error);
31430 /* Values of those variables at last redisplay are stored as
31431 properties on 'overlay-arrow-position' symbol. However, if
31432 Voverlay_arrow_position is a marker, last-arrow-position is its
31433 numerical position. */
31434 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31435 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31437 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31438 properties on a symbol in overlay-arrow-variable-list. */
31439 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31440 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31442 echo_buffer[0] = echo_buffer[1] = Qnil;
31443 staticpro (&echo_buffer[0]);
31444 staticpro (&echo_buffer[1]);
31446 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31447 staticpro (&echo_area_buffer[0]);
31448 staticpro (&echo_area_buffer[1]);
31450 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31451 staticpro (&Vmessages_buffer_name);
31453 mode_line_proptrans_alist = Qnil;
31454 staticpro (&mode_line_proptrans_alist);
31455 mode_line_string_list = Qnil;
31456 staticpro (&mode_line_string_list);
31457 mode_line_string_face = Qnil;
31458 staticpro (&mode_line_string_face);
31459 mode_line_string_face_prop = Qnil;
31460 staticpro (&mode_line_string_face_prop);
31461 Vmode_line_unwind_vector = Qnil;
31462 staticpro (&Vmode_line_unwind_vector);
31464 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31466 help_echo_string = Qnil;
31467 staticpro (&help_echo_string);
31468 help_echo_object = Qnil;
31469 staticpro (&help_echo_object);
31470 help_echo_window = Qnil;
31471 staticpro (&help_echo_window);
31472 previous_help_echo_string = Qnil;
31473 staticpro (&previous_help_echo_string);
31474 help_echo_pos = -1;
31476 DEFSYM (Qright_to_left, "right-to-left");
31477 DEFSYM (Qleft_to_right, "left-to-right");
31478 defsubr (&Sbidi_resolved_levels);
31480 #ifdef HAVE_WINDOW_SYSTEM
31481 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31482 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31483 For example, if a block cursor is over a tab, it will be drawn as
31484 wide as that tab on the display. */);
31485 x_stretch_cursor_p = 0;
31486 #endif
31488 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31489 doc: /* Non-nil means highlight trailing whitespace.
31490 The face used for trailing whitespace is `trailing-whitespace'. */);
31491 Vshow_trailing_whitespace = Qnil;
31493 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31494 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31495 If the value is t, Emacs highlights non-ASCII chars which have the
31496 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31497 or `nobreak-hyphen' face respectively.
31499 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31500 U+2011 (non-breaking hyphen) are affected.
31502 Any other non-nil value means to display these characters as a escape
31503 glyph followed by an ordinary space or hyphen.
31505 A value of nil means no special handling of these characters. */);
31506 Vnobreak_char_display = Qt;
31508 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31509 doc: /* The pointer shape to show in void text areas.
31510 A value of nil means to show the text pointer. Other options are
31511 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31512 `hourglass'. */);
31513 Vvoid_text_area_pointer = Qarrow;
31515 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31516 doc: /* Non-nil means don't actually do any redisplay.
31517 This is used for internal purposes. */);
31518 Vinhibit_redisplay = Qnil;
31520 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31521 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31522 Vglobal_mode_string = Qnil;
31524 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31525 doc: /* Marker for where to display an arrow on top of the buffer text.
31526 This must be the beginning of a line in order to work.
31527 See also `overlay-arrow-string'. */);
31528 Voverlay_arrow_position = Qnil;
31530 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31531 doc: /* String to display as an arrow in non-window frames.
31532 See also `overlay-arrow-position'. */);
31533 Voverlay_arrow_string = build_pure_c_string ("=>");
31535 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31536 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31537 The symbols on this list are examined during redisplay to determine
31538 where to display overlay arrows. */);
31539 Voverlay_arrow_variable_list
31540 = list1 (intern_c_string ("overlay-arrow-position"));
31542 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31543 doc: /* The number of lines to try scrolling a window by when point moves out.
31544 If that fails to bring point back on frame, point is centered instead.
31545 If this is zero, point is always centered after it moves off frame.
31546 If you want scrolling to always be a line at a time, you should set
31547 `scroll-conservatively' to a large value rather than set this to 1. */);
31549 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31550 doc: /* Scroll up to this many lines, to bring point back on screen.
31551 If point moves off-screen, redisplay will scroll by up to
31552 `scroll-conservatively' lines in order to bring point just barely
31553 onto the screen again. If that cannot be done, then redisplay
31554 recenters point as usual.
31556 If the value is greater than 100, redisplay will never recenter point,
31557 but will always scroll just enough text to bring point into view, even
31558 if you move far away.
31560 A value of zero means always recenter point if it moves off screen. */);
31561 scroll_conservatively = 0;
31563 DEFVAR_INT ("scroll-margin", scroll_margin,
31564 doc: /* Number of lines of margin at the top and bottom of a window.
31565 Recenter the window whenever point gets within this many lines
31566 of the top or bottom of the window. */);
31567 scroll_margin = 0;
31569 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31570 doc: /* Pixels per inch value for non-window system displays.
31571 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31572 Vdisplay_pixels_per_inch = make_float (72.0);
31574 #ifdef GLYPH_DEBUG
31575 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31576 #endif
31578 DEFVAR_LISP ("truncate-partial-width-windows",
31579 Vtruncate_partial_width_windows,
31580 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31581 For an integer value, truncate lines in each window narrower than the
31582 full frame width, provided the total window width in column units is less
31583 than that integer; otherwise, respect the value of `truncate-lines'.
31584 The total width of the window is as returned by `window-total-width', it
31585 includes the fringes, the continuation and truncation glyphs, the
31586 display margins (if any), and the scroll bar
31588 For any other non-nil value, truncate lines in all windows that do
31589 not span the full frame width.
31591 A value of nil means to respect the value of `truncate-lines'.
31593 If `word-wrap' is enabled, you might want to reduce this. */);
31594 Vtruncate_partial_width_windows = make_number (50);
31596 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31597 doc: /* Maximum buffer size for which line number should be displayed.
31598 If the buffer is bigger than this, the line number does not appear
31599 in the mode line. A value of nil means no limit. */);
31600 Vline_number_display_limit = Qnil;
31602 DEFVAR_INT ("line-number-display-limit-width",
31603 line_number_display_limit_width,
31604 doc: /* Maximum line width (in characters) for line number display.
31605 If the average length of the lines near point is bigger than this, then the
31606 line number may be omitted from the mode line. */);
31607 line_number_display_limit_width = 200;
31609 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31610 doc: /* Non-nil means highlight region even in nonselected windows. */);
31611 highlight_nonselected_windows = false;
31613 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31614 doc: /* Non-nil if more than one frame is visible on this display.
31615 Minibuffer-only frames don't count, but iconified frames do.
31616 This variable is not guaranteed to be accurate except while processing
31617 `frame-title-format' and `icon-title-format'. */);
31619 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31620 doc: /* Template for displaying the title bar of visible frames.
31621 \(Assuming the window manager supports this feature.)
31623 This variable has the same structure as `mode-line-format', except that
31624 the %c and %l constructs are ignored. It is used only on frames for
31625 which no explicit name has been set (see `modify-frame-parameters'). */);
31627 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31628 doc: /* Template for displaying the title bar of an iconified frame.
31629 \(Assuming the window manager supports this feature.)
31630 This variable has the same structure as `mode-line-format' (which see),
31631 and is used only on frames for which no explicit name has been set
31632 \(see `modify-frame-parameters'). */);
31633 Vicon_title_format
31634 = Vframe_title_format
31635 = listn (CONSTYPE_PURE, 3,
31636 intern_c_string ("multiple-frames"),
31637 build_pure_c_string ("%b"),
31638 listn (CONSTYPE_PURE, 4,
31639 empty_unibyte_string,
31640 intern_c_string ("invocation-name"),
31641 build_pure_c_string ("@"),
31642 intern_c_string ("system-name")));
31644 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31645 doc: /* Maximum number of lines to keep in the message log buffer.
31646 If nil, disable message logging. If t, log messages but don't truncate
31647 the buffer when it becomes large. */);
31648 Vmessage_log_max = make_number (1000);
31650 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31651 doc: /* List of functions to call before redisplaying a window with scrolling.
31652 Each function is called with two arguments, the window and its new
31653 display-start position.
31654 These functions are called whenever the `window-start' marker is modified,
31655 either to point into another buffer (e.g. via `set-window-buffer') or another
31656 place in the same buffer.
31657 Note that the value of `window-end' is not valid when these functions are
31658 called.
31660 Warning: Do not use this feature to alter the way the window
31661 is scrolled. It is not designed for that, and such use probably won't
31662 work. */);
31663 Vwindow_scroll_functions = Qnil;
31665 DEFVAR_LISP ("window-text-change-functions",
31666 Vwindow_text_change_functions,
31667 doc: /* Functions to call in redisplay when text in the window might change. */);
31668 Vwindow_text_change_functions = Qnil;
31670 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31671 doc: /* Functions called when redisplay of a window reaches the end trigger.
31672 Each function is called with two arguments, the window and the end trigger value.
31673 See `set-window-redisplay-end-trigger'. */);
31674 Vredisplay_end_trigger_functions = Qnil;
31676 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31677 doc: /* Non-nil means autoselect window with mouse pointer.
31678 If nil, do not autoselect windows.
31679 A positive number means delay autoselection by that many seconds: a
31680 window is autoselected only after the mouse has remained in that
31681 window for the duration of the delay.
31682 A negative number has a similar effect, but causes windows to be
31683 autoselected only after the mouse has stopped moving. (Because of
31684 the way Emacs compares mouse events, you will occasionally wait twice
31685 that time before the window gets selected.)
31686 Any other value means to autoselect window instantaneously when the
31687 mouse pointer enters it.
31689 Autoselection selects the minibuffer only if it is active, and never
31690 unselects the minibuffer if it is active.
31692 When customizing this variable make sure that the actual value of
31693 `focus-follows-mouse' matches the behavior of your window manager. */);
31694 Vmouse_autoselect_window = Qnil;
31696 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31697 doc: /* Non-nil means automatically resize tool-bars.
31698 This dynamically changes the tool-bar's height to the minimum height
31699 that is needed to make all tool-bar items visible.
31700 If value is `grow-only', the tool-bar's height is only increased
31701 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31702 Vauto_resize_tool_bars = Qt;
31704 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31705 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31706 auto_raise_tool_bar_buttons_p = true;
31708 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31709 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31710 make_cursor_line_fully_visible_p = true;
31712 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31713 doc: /* Border below tool-bar in pixels.
31714 If an integer, use it as the height of the border.
31715 If it is one of `internal-border-width' or `border-width', use the
31716 value of the corresponding frame parameter.
31717 Otherwise, no border is added below the tool-bar. */);
31718 Vtool_bar_border = Qinternal_border_width;
31720 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31721 doc: /* Margin around tool-bar buttons in pixels.
31722 If an integer, use that for both horizontal and vertical margins.
31723 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31724 HORZ specifying the horizontal margin, and VERT specifying the
31725 vertical margin. */);
31726 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31728 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31729 doc: /* Relief thickness of tool-bar buttons. */);
31730 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31732 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31733 doc: /* Tool bar style to use.
31734 It can be one of
31735 image - show images only
31736 text - show text only
31737 both - show both, text below image
31738 both-horiz - show text to the right of the image
31739 text-image-horiz - show text to the left of the image
31740 any other - use system default or image if no system default.
31742 This variable only affects the GTK+ toolkit version of Emacs. */);
31743 Vtool_bar_style = Qnil;
31745 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31746 doc: /* Maximum number of characters a label can have to be shown.
31747 The tool bar style must also show labels for this to have any effect, see
31748 `tool-bar-style'. */);
31749 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31751 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31752 doc: /* List of functions to call to fontify regions of text.
31753 Each function is called with one argument POS. Functions must
31754 fontify a region starting at POS in the current buffer, and give
31755 fontified regions the property `fontified'. */);
31756 Vfontification_functions = Qnil;
31757 Fmake_variable_buffer_local (Qfontification_functions);
31759 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31760 unibyte_display_via_language_environment,
31761 doc: /* Non-nil means display unibyte text according to language environment.
31762 Specifically, this means that raw bytes in the range 160-255 decimal
31763 are displayed by converting them to the equivalent multibyte characters
31764 according to the current language environment. As a result, they are
31765 displayed according to the current fontset.
31767 Note that this variable affects only how these bytes are displayed,
31768 but does not change the fact they are interpreted as raw bytes. */);
31769 unibyte_display_via_language_environment = false;
31771 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31772 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31773 If a float, it specifies a fraction of the mini-window frame's height.
31774 If an integer, it specifies a number of lines. */);
31775 Vmax_mini_window_height = make_float (0.25);
31777 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31778 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31779 A value of nil means don't automatically resize mini-windows.
31780 A value of t means resize them to fit the text displayed in them.
31781 A value of `grow-only', the default, means let mini-windows grow only;
31782 they return to their normal size when the minibuffer is closed, or the
31783 echo area becomes empty. */);
31784 /* Contrary to the doc string, we initialize this to nil, so that
31785 loading loadup.el won't try to resize windows before loading
31786 window.el, where some functions we need to call for this live.
31787 We assign the 'grow-only' value right after loading window.el
31788 during loadup. */
31789 Vresize_mini_windows = Qnil;
31791 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31792 doc: /* Alist specifying how to blink the cursor off.
31793 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31794 `cursor-type' frame-parameter or variable equals ON-STATE,
31795 comparing using `equal', Emacs uses OFF-STATE to specify
31796 how to blink it off. ON-STATE and OFF-STATE are values for
31797 the `cursor-type' frame parameter.
31799 If a frame's ON-STATE has no entry in this list,
31800 the frame's other specifications determine how to blink the cursor off. */);
31801 Vblink_cursor_alist = Qnil;
31803 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31804 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31805 If non-nil, windows are automatically scrolled horizontally to make
31806 point visible. */);
31807 automatic_hscrolling_p = true;
31808 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31810 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31811 doc: /* How many columns away from the window edge point is allowed to get
31812 before automatic hscrolling will horizontally scroll the window. */);
31813 hscroll_margin = 5;
31815 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31816 doc: /* How many columns to scroll the window when point gets too close to the edge.
31817 When point is less than `hscroll-margin' columns from the window
31818 edge, automatic hscrolling will scroll the window by the amount of columns
31819 determined by this variable. If its value is a positive integer, scroll that
31820 many columns. If it's a positive floating-point number, it specifies the
31821 fraction of the window's width to scroll. If it's nil or zero, point will be
31822 centered horizontally after the scroll. Any other value, including negative
31823 numbers, are treated as if the value were zero.
31825 Automatic hscrolling always moves point outside the scroll margin, so if
31826 point was more than scroll step columns inside the margin, the window will
31827 scroll more than the value given by the scroll step.
31829 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31830 and `scroll-right' overrides this variable's effect. */);
31831 Vhscroll_step = make_number (0);
31833 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31834 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31835 Bind this around calls to `message' to let it take effect. */);
31836 message_truncate_lines = false;
31838 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31839 doc: /* Normal hook run to update the menu bar definitions.
31840 Redisplay runs this hook before it redisplays the menu bar.
31841 This is used to update menus such as Buffers, whose contents depend on
31842 various data. */);
31843 Vmenu_bar_update_hook = Qnil;
31845 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31846 doc: /* Frame for which we are updating a menu.
31847 The enable predicate for a menu binding should check this variable. */);
31848 Vmenu_updating_frame = Qnil;
31850 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31851 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31852 inhibit_menubar_update = false;
31854 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31855 doc: /* Prefix prepended to all continuation lines at display time.
31856 The value may be a string, an image, or a stretch-glyph; it is
31857 interpreted in the same way as the value of a `display' text property.
31859 This variable is overridden by any `wrap-prefix' text or overlay
31860 property.
31862 To add a prefix to non-continuation lines, use `line-prefix'. */);
31863 Vwrap_prefix = Qnil;
31864 DEFSYM (Qwrap_prefix, "wrap-prefix");
31865 Fmake_variable_buffer_local (Qwrap_prefix);
31867 DEFVAR_LISP ("line-prefix", Vline_prefix,
31868 doc: /* Prefix prepended to all non-continuation lines at display time.
31869 The value may be a string, an image, or a stretch-glyph; it is
31870 interpreted in the same way as the value of a `display' text property.
31872 This variable is overridden by any `line-prefix' text or overlay
31873 property.
31875 To add a prefix to continuation lines, use `wrap-prefix'. */);
31876 Vline_prefix = Qnil;
31877 DEFSYM (Qline_prefix, "line-prefix");
31878 Fmake_variable_buffer_local (Qline_prefix);
31880 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31881 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31882 inhibit_eval_during_redisplay = false;
31884 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31885 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31886 inhibit_free_realized_faces = false;
31888 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31889 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31890 Intended for use during debugging and for testing bidi display;
31891 see biditest.el in the test suite. */);
31892 inhibit_bidi_mirroring = false;
31894 #ifdef GLYPH_DEBUG
31895 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31896 doc: /* Inhibit try_window_id display optimization. */);
31897 inhibit_try_window_id = false;
31899 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31900 doc: /* Inhibit try_window_reusing display optimization. */);
31901 inhibit_try_window_reusing = false;
31903 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31904 doc: /* Inhibit try_cursor_movement display optimization. */);
31905 inhibit_try_cursor_movement = false;
31906 #endif /* GLYPH_DEBUG */
31908 DEFVAR_INT ("overline-margin", overline_margin,
31909 doc: /* Space between overline and text, in pixels.
31910 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31911 margin to the character height. */);
31912 overline_margin = 2;
31914 DEFVAR_INT ("underline-minimum-offset",
31915 underline_minimum_offset,
31916 doc: /* Minimum distance between baseline and underline.
31917 This can improve legibility of underlined text at small font sizes,
31918 particularly when using variable `x-use-underline-position-properties'
31919 with fonts that specify an UNDERLINE_POSITION relatively close to the
31920 baseline. The default value is 1. */);
31921 underline_minimum_offset = 1;
31923 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31924 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31925 This feature only works when on a window system that can change
31926 cursor shapes. */);
31927 display_hourglass_p = true;
31929 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31930 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31931 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31933 #ifdef HAVE_WINDOW_SYSTEM
31934 hourglass_atimer = NULL;
31935 hourglass_shown_p = false;
31936 #endif /* HAVE_WINDOW_SYSTEM */
31938 /* Name of the face used to display glyphless characters. */
31939 DEFSYM (Qglyphless_char, "glyphless-char");
31941 /* Method symbols for Vglyphless_char_display. */
31942 DEFSYM (Qhex_code, "hex-code");
31943 DEFSYM (Qempty_box, "empty-box");
31944 DEFSYM (Qthin_space, "thin-space");
31945 DEFSYM (Qzero_width, "zero-width");
31947 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31948 doc: /* Function run just before redisplay.
31949 It is called with one argument, which is the set of windows that are to
31950 be redisplayed. This set can be nil (meaning, only the selected window),
31951 or t (meaning all windows). */);
31952 Vpre_redisplay_function = intern ("ignore");
31954 /* Symbol for the purpose of Vglyphless_char_display. */
31955 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31956 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31958 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31959 doc: /* Char-table defining glyphless characters.
31960 Each element, if non-nil, should be one of the following:
31961 an ASCII acronym string: display this string in a box
31962 `hex-code': display the hexadecimal code of a character in a box
31963 `empty-box': display as an empty box
31964 `thin-space': display as 1-pixel width space
31965 `zero-width': don't display
31966 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31967 display method for graphical terminals and text terminals respectively.
31968 GRAPHICAL and TEXT should each have one of the values listed above.
31970 The char-table has one extra slot to control the display of a character for
31971 which no font is found. This slot only takes effect on graphical terminals.
31972 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31973 `thin-space'. The default is `empty-box'.
31975 If a character has a non-nil entry in an active display table, the
31976 display table takes effect; in this case, Emacs does not consult
31977 `glyphless-char-display' at all. */);
31978 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31979 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31980 Qempty_box);
31982 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31983 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31984 Vdebug_on_message = Qnil;
31986 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31987 doc: /* */);
31988 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31990 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31991 doc: /* */);
31992 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31994 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31995 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31996 Vredisplay__variables = Qnil;
31998 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31999 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32000 /* Initialize to t, since we need to disable reordering until
32001 loadup.el successfully loads charprop.el. */
32002 redisplay__inhibit_bidi = true;
32006 /* Initialize this module when Emacs starts. */
32008 void
32009 init_xdisp (void)
32011 CHARPOS (this_line_start_pos) = 0;
32013 if (!noninteractive)
32015 struct window *m = XWINDOW (minibuf_window);
32016 Lisp_Object frame = m->frame;
32017 struct frame *f = XFRAME (frame);
32018 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32019 struct window *r = XWINDOW (root);
32020 int i;
32022 echo_area_window = minibuf_window;
32024 r->top_line = FRAME_TOP_MARGIN (f);
32025 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32026 r->total_cols = FRAME_COLS (f);
32027 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32028 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32029 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32031 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32032 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32033 m->total_cols = FRAME_COLS (f);
32034 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32035 m->total_lines = 1;
32036 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32038 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32039 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32040 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32042 /* The default ellipsis glyphs `...'. */
32043 for (i = 0; i < 3; ++i)
32044 default_invis_vector[i] = make_number ('.');
32048 /* Allocate the buffer for frame titles.
32049 Also used for `format-mode-line'. */
32050 int size = 100;
32051 mode_line_noprop_buf = xmalloc (size);
32052 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32053 mode_line_noprop_ptr = mode_line_noprop_buf;
32054 mode_line_target = MODE_LINE_DISPLAY;
32057 help_echo_showing_p = false;
32060 #ifdef HAVE_WINDOW_SYSTEM
32062 /* Platform-independent portion of hourglass implementation. */
32064 /* Timer function of hourglass_atimer. */
32066 static void
32067 show_hourglass (struct atimer *timer)
32069 /* The timer implementation will cancel this timer automatically
32070 after this function has run. Set hourglass_atimer to null
32071 so that we know the timer doesn't have to be canceled. */
32072 hourglass_atimer = NULL;
32074 if (!hourglass_shown_p)
32076 Lisp_Object tail, frame;
32078 block_input ();
32080 FOR_EACH_FRAME (tail, frame)
32082 struct frame *f = XFRAME (frame);
32084 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32085 && FRAME_RIF (f)->show_hourglass)
32086 FRAME_RIF (f)->show_hourglass (f);
32089 hourglass_shown_p = true;
32090 unblock_input ();
32094 /* Cancel a currently active hourglass timer, and start a new one. */
32096 void
32097 start_hourglass (void)
32099 struct timespec delay;
32101 cancel_hourglass ();
32103 if (INTEGERP (Vhourglass_delay)
32104 && XINT (Vhourglass_delay) > 0)
32105 delay = make_timespec (min (XINT (Vhourglass_delay),
32106 TYPE_MAXIMUM (time_t)),
32108 else if (FLOATP (Vhourglass_delay)
32109 && XFLOAT_DATA (Vhourglass_delay) > 0)
32110 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32111 else
32112 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32114 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32115 show_hourglass, NULL);
32118 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32119 shown. */
32121 void
32122 cancel_hourglass (void)
32124 if (hourglass_atimer)
32126 cancel_atimer (hourglass_atimer);
32127 hourglass_atimer = NULL;
32130 if (hourglass_shown_p)
32132 Lisp_Object tail, frame;
32134 block_input ();
32136 FOR_EACH_FRAME (tail, frame)
32138 struct frame *f = XFRAME (frame);
32140 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32141 && FRAME_RIF (f)->hide_hourglass)
32142 FRAME_RIF (f)->hide_hourglass (f);
32143 #ifdef HAVE_NTGUI
32144 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32145 else if (!FRAME_W32_P (f))
32146 w32_arrow_cursor ();
32147 #endif
32150 hourglass_shown_p = false;
32151 unblock_input ();
32155 #endif /* HAVE_WINDOW_SYSTEM */