Document atomic windows in Elisp manual (Bug#18170)
[emacs.git] / src / xdisp.c
blob3af5ea49ab63d731c312f84ec45702032c52290a
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 redisplay_windows (Lisp_Object);
822 static void redisplay_window (Lisp_Object, bool);
823 static Lisp_Object redisplay_window_error (Lisp_Object);
824 static Lisp_Object redisplay_window_0 (Lisp_Object);
825 static Lisp_Object redisplay_window_1 (Lisp_Object);
826 static bool set_cursor_from_row (struct window *, struct glyph_row *,
827 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
828 int, int);
829 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
830 static bool update_menu_bar (struct frame *, bool, bool);
831 static bool try_window_reusing_current_matrix (struct window *);
832 static int try_window_id (struct window *);
833 static bool display_line (struct it *);
834 static int display_mode_lines (struct window *);
835 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
836 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
837 Lisp_Object, bool);
838 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
839 Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
843 ptrdiff_t *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static bool get_overlay_strings (struct it *, ptrdiff_t);
849 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, bool);
852 static void reseat_1 (struct it *, struct text_pos, bool);
853 static bool next_element_from_display_vector (struct it *);
854 static bool next_element_from_string (struct it *);
855 static bool next_element_from_c_string (struct it *);
856 static bool next_element_from_buffer (struct it *);
857 static bool next_element_from_composition (struct it *);
858 static bool next_element_from_image (struct it *);
859 static bool next_element_from_stretch (struct it *);
860 static bool next_element_from_xwidget (struct it *);
861 static void load_overlay_strings (struct it *, ptrdiff_t);
862 static bool get_next_display_element (struct it *);
863 static enum move_it_result
864 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
865 enum move_operation_enum);
866 static void get_visually_first_element (struct it *);
867 static void compute_stop_pos (struct it *);
868 static int face_before_or_after_it_pos (struct it *, bool);
869 static ptrdiff_t next_overlay_change (ptrdiff_t);
870 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
871 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
872 static int handle_single_display_spec (struct it *, Lisp_Object,
873 Lisp_Object, Lisp_Object,
874 struct text_pos *, ptrdiff_t, int, bool);
875 static int underlying_face_id (struct it *);
877 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
878 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
880 #ifdef HAVE_WINDOW_SYSTEM
882 static void update_tool_bar (struct frame *, bool);
883 static void x_draw_bottom_divider (struct window *w);
884 static void notice_overwritten_cursor (struct window *,
885 enum glyph_row_area,
886 int, int, int, int);
887 static int normal_char_height (struct font *, int);
888 static void normal_char_ascent_descent (struct font *, int, int *, int *);
890 static void append_stretch_glyph (struct it *, Lisp_Object,
891 int, int, int);
893 static Lisp_Object get_it_property (struct it *, Lisp_Object);
894 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
895 struct font *, int, bool);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return true if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1298 bool
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 bool visible_p = false;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. Also, someone might narrow-to-region and immediately
1322 call a scroll function. */
1323 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1324 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1326 /* If the top of the window is after CHARPOS, the latter is surely
1327 not visible. */
1328 if (charpos >= 0 && CHARPOS (top) > charpos)
1329 return visible_p;
1331 /* Compute exact mode line heights. */
1332 if (WINDOW_WANTS_MODELINE_P (w))
1333 w->mode_line_height
1334 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1335 BVAR (current_buffer, mode_line_format));
1337 if (WINDOW_WANTS_HEADER_LINE_P (w))
1338 w->header_line_height
1339 = display_mode_line (w, HEADER_LINE_FACE_ID,
1340 BVAR (current_buffer, header_line_format));
1342 start_display (&it, w, top);
1343 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1344 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1346 if (charpos >= 0
1347 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1348 && IT_CHARPOS (it) >= charpos)
1349 /* When scanning backwards under bidi iteration, move_it_to
1350 stops at or _before_ CHARPOS, because it stops at or to
1351 the _right_ of the character at CHARPOS. */
1352 || (it.bidi_p && it.bidi_it.scan_dir == -1
1353 && IT_CHARPOS (it) <= charpos)))
1355 /* We have reached CHARPOS, or passed it. How the call to
1356 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1357 or covered by a display property, move_it_to stops at the end
1358 of the invisible text, to the right of CHARPOS. (ii) If
1359 CHARPOS is in a display vector, move_it_to stops on its last
1360 glyph. */
1361 int top_x = it.current_x;
1362 int top_y = it.current_y;
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364 int bottom_y;
1365 struct it save_it;
1366 void *save_it_data = NULL;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 SAVE_IT (save_it, it, save_it_data);
1370 last_height = 0;
1371 bottom_y = line_bottom_y (&it);
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = true;
1376 if (bottom_y >= it.last_visible_y
1377 && it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) < charpos)
1380 /* When the last line of the window is scanned backwards
1381 under bidi iteration, we could be duped into thinking
1382 that we have passed CHARPOS, when in fact move_it_to
1383 simply stopped short of CHARPOS because it reached
1384 last_visible_y. To see if that's what happened, we call
1385 move_it_to again with a slightly larger vertical limit,
1386 and see if it actually moved vertically; if it did, we
1387 didn't really reach CHARPOS, which is beyond window end. */
1388 /* Why 10? because we don't know how many canonical lines
1389 will the height of the next line(s) be. So we guess. */
1390 int ten_more_lines = 10 * default_line_pixel_height (w);
1392 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1393 MOVE_TO_POS | MOVE_TO_Y);
1394 if (it.current_y > top_y)
1395 visible_p = false;
1398 RESTORE_IT (&it, &save_it, save_it_data);
1399 if (visible_p)
1401 if (it.method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2, it2_prev;
1410 /* The idea is to get to the previous buffer
1411 position, consume the character there, and use
1412 the pixel coordinates we get after that. But if
1413 the previous buffer position is also displayed
1414 from a display vector, we need to consume all of
1415 the glyphs from that display vector. */
1416 start_display (&it2, w, top);
1417 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1418 /* If we didn't get to CHARPOS - 1, there's some
1419 replacing display property at that position, and
1420 we stopped after it. That is exactly the place
1421 whose coordinates we want. */
1422 if (IT_CHARPOS (it2) != charpos - 1)
1423 it2_prev = it2;
1424 else
1426 /* Iterate until we get out of the display
1427 vector that displays the character at
1428 CHARPOS - 1. */
1429 do {
1430 get_next_display_element (&it2);
1431 PRODUCE_GLYPHS (&it2);
1432 it2_prev = it2;
1433 set_iterator_to_next (&it2, true);
1434 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1435 && IT_CHARPOS (it2) < charpos);
1437 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1438 || it2_prev.current_x > it2_prev.last_visible_x)
1439 top_x = it.glyph_row->x;
1440 else
1442 top_x = it2_prev.current_x;
1443 top_y = it2_prev.current_y;
1447 else if (IT_CHARPOS (it) != charpos)
1449 Lisp_Object cpos = make_number (charpos);
1450 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1451 Lisp_Object string = string_from_display_spec (spec);
1452 struct text_pos tpos;
1453 bool newline_in_string
1454 = (STRINGP (string)
1455 && memchr (SDATA (string), '\n', SBYTES (string)));
1457 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1458 bool replacing_spec_p
1459 = (!NILP (spec)
1460 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1461 charpos, FRAME_WINDOW_P (it.f)));
1462 /* The tricky code below is needed because there's a
1463 discrepancy between move_it_to and how we set cursor
1464 when PT is at the beginning of a portion of text
1465 covered by a display property or an overlay with a
1466 display property, or the display line ends in a
1467 newline from a display string. move_it_to will stop
1468 _after_ such display strings, whereas
1469 set_cursor_from_row conspires with cursor_row_p to
1470 place the cursor on the first glyph produced from the
1471 display string. */
1473 /* We have overshoot PT because it is covered by a
1474 display property that replaces the text it covers.
1475 If the string includes embedded newlines, we are also
1476 in the wrong display line. Backtrack to the correct
1477 line, where the display property begins. */
1478 if (replacing_spec_p)
1480 Lisp_Object startpos, endpos;
1481 EMACS_INT start, end;
1482 struct it it3;
1484 /* Find the first and the last buffer positions
1485 covered by the display string. */
1486 endpos =
1487 Fnext_single_char_property_change (cpos, Qdisplay,
1488 Qnil, Qnil);
1489 startpos =
1490 Fprevious_single_char_property_change (endpos, Qdisplay,
1491 Qnil, Qnil);
1492 start = XFASTINT (startpos);
1493 end = XFASTINT (endpos);
1494 /* Move to the last buffer position before the
1495 display property. */
1496 start_display (&it3, w, top);
1497 if (start > CHARPOS (top))
1498 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1499 /* Move forward one more line if the position before
1500 the display string is a newline or if it is the
1501 rightmost character on a line that is
1502 continued or word-wrapped. */
1503 if (it3.method == GET_FROM_BUFFER
1504 && (it3.c == '\n'
1505 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1506 move_it_by_lines (&it3, 1);
1507 else if (move_it_in_display_line_to (&it3, -1,
1508 it3.current_x
1509 + it3.pixel_width,
1510 MOVE_TO_X)
1511 == MOVE_LINE_CONTINUED)
1513 move_it_by_lines (&it3, 1);
1514 /* When we are under word-wrap, the #$@%!
1515 move_it_by_lines moves 2 lines, so we need to
1516 fix that up. */
1517 if (it3.line_wrap == WORD_WRAP)
1518 move_it_by_lines (&it3, -1);
1521 /* Record the vertical coordinate of the display
1522 line where we wound up. */
1523 top_y = it3.current_y;
1524 if (it3.bidi_p)
1526 /* When characters are reordered for display,
1527 the character displayed to the left of the
1528 display string could be _after_ the display
1529 property in the logical order. Use the
1530 smallest vertical position of these two. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1533 if (it3.current_y < top_y)
1534 top_y = it3.current_y;
1536 /* Move from the top of the window to the beginning
1537 of the display line where the display string
1538 begins. */
1539 start_display (&it3, w, top);
1540 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1541 /* If it3_moved stays false after the 'while' loop
1542 below, that means we already were at a newline
1543 before the loop (e.g., the display string begins
1544 with a newline), so we don't need to (and cannot)
1545 inspect the glyphs of it3.glyph_row, because
1546 PRODUCE_GLYPHS will not produce anything for a
1547 newline, and thus it3.glyph_row stays at its
1548 stale content it got at top of the window. */
1549 bool it3_moved = false;
1550 /* Finally, advance the iterator until we hit the
1551 first display element whose character position is
1552 CHARPOS, or until the first newline from the
1553 display string, which signals the end of the
1554 display line. */
1555 while (get_next_display_element (&it3))
1557 PRODUCE_GLYPHS (&it3);
1558 if (IT_CHARPOS (it3) == charpos
1559 || ITERATOR_AT_END_OF_LINE_P (&it3))
1560 break;
1561 it3_moved = true;
1562 set_iterator_to_next (&it3, false);
1564 top_x = it3.current_x - it3.pixel_width;
1565 /* Normally, we would exit the above loop because we
1566 found the display element whose character
1567 position is CHARPOS. For the contingency that we
1568 didn't, and stopped at the first newline from the
1569 display string, move back over the glyphs
1570 produced from the string, until we find the
1571 rightmost glyph not from the string. */
1572 if (it3_moved
1573 && newline_in_string
1574 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1576 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA];
1579 while (EQ ((g - 1)->object, string))
1581 --g;
1582 top_x -= g->pixel_width;
1584 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1585 + it3.glyph_row->used[TEXT_AREA]);
1590 *x = top_x;
1591 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1592 *rtop = max (0, window_top_y - top_y);
1593 *rbot = max (0, bottom_y - it.last_visible_y);
1594 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1595 - max (top_y, window_top_y)));
1596 *vpos = it.vpos;
1597 if (it.bidi_it.paragraph_dir == R2L)
1598 r2l = true;
1601 else
1603 /* Either we were asked to provide info about WINDOW_END, or
1604 CHARPOS is in the partially visible glyph row at end of
1605 window. */
1606 struct it it2;
1607 void *it2data = NULL;
1609 SAVE_IT (it2, it, it2data);
1610 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1611 move_it_by_lines (&it, 1);
1612 if (charpos < IT_CHARPOS (it)
1613 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1615 visible_p = true;
1616 RESTORE_IT (&it2, &it2, it2data);
1617 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1618 *x = it2.current_x;
1619 *y = it2.current_y + it2.max_ascent - it2.ascent;
1620 *rtop = max (0, -it2.current_y);
1621 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1622 - it.last_visible_y));
1623 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1624 it.last_visible_y)
1625 - max (it2.current_y,
1626 WINDOW_HEADER_LINE_HEIGHT (w))));
1627 *vpos = it2.vpos;
1628 if (it2.bidi_it.paragraph_dir == R2L)
1629 r2l = true;
1631 else
1632 bidi_unshelve_cache (it2data, true);
1634 bidi_unshelve_cache (itdata, false);
1636 if (old_buffer)
1637 set_buffer_internal_1 (old_buffer);
1639 if (visible_p)
1641 if (w->hscroll > 0)
1642 *x -=
1643 window_hscroll_limited (w, WINDOW_XFRAME (w))
1644 * WINDOW_FRAME_COLUMN_WIDTH (w);
1645 /* For lines in an R2L paragraph, we need to mirror the X pixel
1646 coordinate wrt the text area. For the reasons, see the
1647 commentary in buffer_posn_from_coords and the explanation of
1648 the geometry used by the move_it_* functions at the end of
1649 the large commentary near the beginning of this file. */
1650 if (r2l)
1651 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1654 #if false
1655 /* Debugging code. */
1656 if (visible_p)
1657 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1658 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1659 else
1660 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1661 #endif
1663 return visible_p;
1667 /* Return the next character from STR. Return in *LEN the length of
1668 the character. This is like STRING_CHAR_AND_LENGTH but never
1669 returns an invalid character. If we find one, we return a `?', but
1670 with the length of the invalid character. */
1672 static int
1673 string_char_and_length (const unsigned char *str, int *len)
1675 int c;
1677 c = STRING_CHAR_AND_LENGTH (str, *len);
1678 if (!CHAR_VALID_P (c))
1679 /* We may not change the length here because other places in Emacs
1680 don't use this function, i.e. they silently accept invalid
1681 characters. */
1682 c = '?';
1684 return c;
1689 /* Given a position POS containing a valid character and byte position
1690 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1692 static struct text_pos
1693 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1695 eassert (STRINGP (string) && nchars >= 0);
1697 if (STRING_MULTIBYTE (string))
1699 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1700 int len;
1702 while (nchars--)
1704 string_char_and_length (p, &len);
1705 p += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1710 else
1711 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1713 return pos;
1717 /* Value is the text position, i.e. character and byte position,
1718 for character position CHARPOS in STRING. */
1720 static struct text_pos
1721 string_pos (ptrdiff_t charpos, Lisp_Object string)
1723 struct text_pos pos;
1724 eassert (STRINGP (string));
1725 eassert (charpos >= 0);
1726 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1727 return pos;
1731 /* Value is a text position, i.e. character and byte position, for
1732 character position CHARPOS in C string S. MULTIBYTE_P
1733 means recognize multibyte characters. */
1735 static struct text_pos
1736 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1738 struct text_pos pos;
1740 eassert (s != NULL);
1741 eassert (charpos >= 0);
1743 if (multibyte_p)
1745 int len;
1747 SET_TEXT_POS (pos, 0, 0);
1748 while (charpos--)
1750 string_char_and_length ((const unsigned char *) s, &len);
1751 s += len;
1752 CHARPOS (pos) += 1;
1753 BYTEPOS (pos) += len;
1756 else
1757 SET_TEXT_POS (pos, charpos, charpos);
1759 return pos;
1763 /* Value is the number of characters in C string S. MULTIBYTE_P
1764 means recognize multibyte characters. */
1766 static ptrdiff_t
1767 number_of_chars (const char *s, bool multibyte_p)
1769 ptrdiff_t nchars;
1771 if (multibyte_p)
1773 ptrdiff_t rest = strlen (s);
1774 int len;
1775 const unsigned char *p = (const unsigned char *) s;
1777 for (nchars = 0; rest > 0; ++nchars)
1779 string_char_and_length (p, &len);
1780 rest -= len, p += len;
1783 else
1784 nchars = strlen (s);
1786 return nchars;
1790 /* Compute byte position NEWPOS->bytepos corresponding to
1791 NEWPOS->charpos. POS is a known position in string STRING.
1792 NEWPOS->charpos must be >= POS.charpos. */
1794 static void
1795 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1797 eassert (STRINGP (string));
1798 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1800 if (STRING_MULTIBYTE (string))
1801 *newpos = string_pos_nchars_ahead (pos, string,
1802 CHARPOS (*newpos) - CHARPOS (pos));
1803 else
1804 BYTEPOS (*newpos) = CHARPOS (*newpos);
1807 /* EXPORT:
1808 Return an estimation of the pixel height of mode or header lines on
1809 frame F. FACE_ID specifies what line's height to estimate. */
1812 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 if (FRAME_WINDOW_P (f))
1817 int height = FONT_HEIGHT (FRAME_FONT (f));
1819 /* This function is called so early when Emacs starts that the face
1820 cache and mode line face are not yet initialized. */
1821 if (FRAME_FACE_CACHE (f))
1823 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1824 if (face)
1826 if (face->font)
1827 height = normal_char_height (face->font, -1);
1828 if (face->box_line_width > 0)
1829 height += 2 * face->box_line_width;
1833 return height;
1835 #endif
1837 return 1;
1840 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1841 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1842 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1843 not force the value into range. */
1845 void
1846 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1847 NativeRectangle *bounds, bool noclip)
1850 #ifdef HAVE_WINDOW_SYSTEM
1851 if (FRAME_WINDOW_P (f))
1853 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1854 even for negative values. */
1855 if (pix_x < 0)
1856 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1857 if (pix_y < 0)
1858 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1860 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1861 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1863 if (bounds)
1864 STORE_NATIVE_RECT (*bounds,
1865 FRAME_COL_TO_PIXEL_X (f, pix_x),
1866 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1867 FRAME_COLUMN_WIDTH (f) - 1,
1868 FRAME_LINE_HEIGHT (f) - 1);
1870 /* PXW: Should we clip pixels before converting to columns/lines? */
1871 if (!noclip)
1873 if (pix_x < 0)
1874 pix_x = 0;
1875 else if (pix_x > FRAME_TOTAL_COLS (f))
1876 pix_x = FRAME_TOTAL_COLS (f);
1878 if (pix_y < 0)
1879 pix_y = 0;
1880 else if (pix_y > FRAME_TOTAL_LINES (f))
1881 pix_y = FRAME_TOTAL_LINES (f);
1884 #endif
1886 *x = pix_x;
1887 *y = pix_y;
1891 /* Find the glyph under window-relative coordinates X/Y in window W.
1892 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1893 strings. Return in *HPOS and *VPOS the row and column number of
1894 the glyph found. Return in *AREA the glyph area containing X.
1895 Value is a pointer to the glyph found or null if X/Y is not on
1896 text, or we can't tell because W's current matrix is not up to
1897 date. */
1899 static struct glyph *
1900 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1901 int *dx, int *dy, int *area)
1903 struct glyph *glyph, *end;
1904 struct glyph_row *row = NULL;
1905 int x0, i;
1907 /* Find row containing Y. Give up if some row is not enabled. */
1908 for (i = 0; i < w->current_matrix->nrows; ++i)
1910 row = MATRIX_ROW (w->current_matrix, i);
1911 if (!row->enabled_p)
1912 return NULL;
1913 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1914 break;
1917 *vpos = i;
1918 *hpos = 0;
1920 /* Give up if Y is not in the window. */
1921 if (i == w->current_matrix->nrows)
1922 return NULL;
1924 /* Get the glyph area containing X. */
1925 if (w->pseudo_window_p)
1927 *area = TEXT_AREA;
1928 x0 = 0;
1930 else
1932 if (x < window_box_left_offset (w, TEXT_AREA))
1934 *area = LEFT_MARGIN_AREA;
1935 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1937 else if (x < window_box_right_offset (w, TEXT_AREA))
1939 *area = TEXT_AREA;
1940 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1942 else
1944 *area = RIGHT_MARGIN_AREA;
1945 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1949 /* Find glyph containing X. */
1950 glyph = row->glyphs[*area];
1951 end = glyph + row->used[*area];
1952 x -= x0;
1953 while (glyph < end && x >= glyph->pixel_width)
1955 x -= glyph->pixel_width;
1956 ++glyph;
1959 if (glyph == end)
1960 return NULL;
1962 if (dx)
1964 *dx = x;
1965 *dy = y - (row->y + row->ascent - glyph->ascent);
1968 *hpos = glyph - row->glyphs[*area];
1969 return glyph;
1972 /* Convert frame-relative x/y to coordinates relative to window W.
1973 Takes pseudo-windows into account. */
1975 static void
1976 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1978 if (w->pseudo_window_p)
1980 /* A pseudo-window is always full-width, and starts at the
1981 left edge of the frame, plus a frame border. */
1982 struct frame *f = XFRAME (w->frame);
1983 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 else
1988 *x -= WINDOW_LEFT_EDGE_X (w);
1989 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1993 #ifdef HAVE_WINDOW_SYSTEM
1995 /* EXPORT:
1996 Return in RECTS[] at most N clipping rectangles for glyph string S.
1997 Return the number of stored rectangles. */
2000 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2002 XRectangle r;
2004 if (n <= 0)
2005 return 0;
2007 if (s->row->full_width_p)
2009 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2010 r.x = WINDOW_LEFT_EDGE_X (s->w);
2011 if (s->row->mode_line_p)
2012 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2013 else
2014 r.width = WINDOW_PIXEL_WIDTH (s->w);
2016 /* Unless displaying a mode or menu bar line, which are always
2017 fully visible, clip to the visible part of the row. */
2018 if (s->w->pseudo_window_p)
2019 r.height = s->row->visible_height;
2020 else
2021 r.height = s->height;
2023 else
2025 /* This is a text line that may be partially visible. */
2026 r.x = window_box_left (s->w, s->area);
2027 r.width = window_box_width (s->w, s->area);
2028 r.height = s->row->visible_height;
2031 if (s->clip_head)
2032 if (r.x < s->clip_head->x)
2034 if (r.width >= s->clip_head->x - r.x)
2035 r.width -= s->clip_head->x - r.x;
2036 else
2037 r.width = 0;
2038 r.x = s->clip_head->x;
2040 if (s->clip_tail)
2041 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2043 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2044 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2045 else
2046 r.width = 0;
2049 /* If S draws overlapping rows, it's sufficient to use the top and
2050 bottom of the window for clipping because this glyph string
2051 intentionally draws over other lines. */
2052 if (s->for_overlaps)
2054 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2055 r.height = window_text_bottom_y (s->w) - r.y;
2057 /* Alas, the above simple strategy does not work for the
2058 environments with anti-aliased text: if the same text is
2059 drawn onto the same place multiple times, it gets thicker.
2060 If the overlap we are processing is for the erased cursor, we
2061 take the intersection with the rectangle of the cursor. */
2062 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2064 XRectangle rc, r_save = r;
2066 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2067 rc.y = s->w->phys_cursor.y;
2068 rc.width = s->w->phys_cursor_width;
2069 rc.height = s->w->phys_cursor_height;
2071 x_intersect_rectangles (&r_save, &rc, &r);
2074 else
2076 /* Don't use S->y for clipping because it doesn't take partially
2077 visible lines into account. For example, it can be negative for
2078 partially visible lines at the top of a window. */
2079 if (!s->row->full_width_p
2080 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2081 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2082 else
2083 r.y = max (0, s->row->y);
2086 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2088 /* If drawing the cursor, don't let glyph draw outside its
2089 advertised boundaries. Cleartype does this under some circumstances. */
2090 if (s->hl == DRAW_CURSOR)
2092 struct glyph *glyph = s->first_glyph;
2093 int height, max_y;
2095 if (s->x > r.x)
2097 if (r.width >= s->x - r.x)
2098 r.width -= s->x - r.x;
2099 else /* R2L hscrolled row with cursor outside text area */
2100 r.width = 0;
2101 r.x = s->x;
2103 r.width = min (r.width, glyph->pixel_width);
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2112 r.y = max_y;
2113 r.height = height;
2115 else
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2128 if (s->row->clip)
2130 XRectangle r_save = r;
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2146 else
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2159 if (s->for_overlaps & OVERLAPS_PRED)
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2176 if (r.y + r.height > row_y + s->row->visible_height)
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2181 else
2182 rs[i].height = 0;
2184 i++;
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2202 get_glyph_string_clip_rects (s, nr, 1);
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0, ascent;
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width;
2224 x = w->phys_cursor.x;
2225 if (x < 0)
2227 wd += x;
2228 x = 0;
2231 if (glyph->type == STRETCH_GLYPH
2232 && !x_stretch_cursor_p)
2233 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2234 w->phys_cursor_width = wd;
2236 /* Don't let the hollow cursor glyph descend below the glyph row's
2237 ascent value, lest the hollow cursor looks funny. */
2238 y = w->phys_cursor.y;
2239 ascent = row->ascent;
2240 if (row->ascent < glyph->ascent)
2242 y -= glyph->ascent - row->ascent;
2243 ascent = glyph->ascent;
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249 h = max (h0, ascent + glyph->descent);
2250 h0 = min (h0, ascent + glyph->descent);
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2258 else
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2263 h += y - y0;
2264 y = y0;
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2274 * Remember which glyph the mouse is over.
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2290 if (window_resize_pixelwise)
2292 width = height = 1;
2293 goto virtual_glyph;
2295 else if (!f->glyphs_initialized_p
2296 || (window = window_from_coordinates (f, gx, gy, &part, false),
2297 NILP (window)))
2299 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2300 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2301 goto virtual_glyph;
2304 w = XWINDOW (window);
2305 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2306 height = WINDOW_FRAME_LINE_HEIGHT (w);
2308 x = window_relative_x_coord (w, part, gx);
2309 y = gy - WINDOW_TOP_EDGE_Y (w);
2311 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2312 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2314 if (w->pseudo_window_p)
2316 area = TEXT_AREA;
2317 part = ON_MODE_LINE; /* Don't adjust margin. */
2318 goto text_glyph;
2321 switch (part)
2323 case ON_LEFT_MARGIN:
2324 area = LEFT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_RIGHT_MARGIN:
2328 area = RIGHT_MARGIN_AREA;
2329 goto text_glyph;
2331 case ON_HEADER_LINE:
2332 case ON_MODE_LINE:
2333 gr = (part == ON_HEADER_LINE
2334 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2335 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2336 gy = gr->y;
2337 area = TEXT_AREA;
2338 goto text_glyph_row_found;
2340 case ON_TEXT:
2341 area = TEXT_AREA;
2343 text_glyph:
2344 gr = 0; gy = 0;
2345 for (; r <= end_row && r->enabled_p; ++r)
2346 if (r->y + r->height > y)
2348 gr = r; gy = r->y;
2349 break;
2352 text_glyph_row_found:
2353 if (gr && gy <= y)
2355 struct glyph *g = gr->glyphs[area];
2356 struct glyph *end = g + gr->used[area];
2358 height = gr->height;
2359 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2360 if (gx + g->pixel_width > x)
2361 break;
2363 if (g < end)
2365 if (g->type == IMAGE_GLYPH)
2367 /* Don't remember when mouse is over image, as
2368 image may have hot-spots. */
2369 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2370 return;
2372 width = g->pixel_width;
2374 else
2376 /* Use nominal char spacing at end of line. */
2377 x -= gx;
2378 gx += (x / width) * width;
2381 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2383 gx += window_box_left_offset (w, area);
2384 /* Don't expand over the modeline to make sure the vertical
2385 drag cursor is shown early enough. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2390 else
2392 /* Use nominal line height at end of window. */
2393 gx = (x / width) * width;
2394 y -= gy;
2395 gy += (y / height) * height;
2396 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2397 /* See comment above. */
2398 height = min (height,
2399 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2401 break;
2403 case ON_LEFT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2406 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2407 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2408 goto row_glyph;
2410 case ON_RIGHT_FRINGE:
2411 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2412 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2413 : window_box_right_offset (w, TEXT_AREA));
2414 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2415 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2416 && !WINDOW_RIGHTMOST_P (w))
2417 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2418 /* Make sure the vertical border can get her own glyph to the
2419 right of the one we build here. */
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2421 else
2422 width = WINDOW_PIXEL_WIDTH (w) - gx;
2423 else
2424 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2426 goto row_glyph;
2428 case ON_VERTICAL_BORDER:
2429 gx = WINDOW_PIXEL_WIDTH (w) - width;
2430 goto row_glyph;
2432 case ON_VERTICAL_SCROLL_BAR:
2433 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2436 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2438 : 0)));
2439 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441 row_glyph:
2442 gr = 0, gy = 0;
2443 for (; r <= end_row && r->enabled_p; ++r)
2444 if (r->y + r->height > y)
2446 gr = r; gy = r->y;
2447 break;
2450 if (gr && gy <= y)
2451 height = gr->height;
2452 else
2454 /* Use nominal line height at end of window. */
2455 y -= gy;
2456 gy += (y / height) * height;
2458 break;
2460 case ON_RIGHT_DIVIDER:
2461 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2462 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2463 gy = 0;
2464 /* The bottom divider prevails. */
2465 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 case ON_BOTTOM_DIVIDER:
2469 gx = 0;
2470 width = WINDOW_PIXEL_WIDTH (w);
2471 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2472 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2473 goto add_edge;
2475 default:
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2492 goto store_rect;
2495 add_edge:
2496 gx += WINDOW_LEFT_EDGE_X (w);
2497 gy += WINDOW_TOP_EDGE_Y (w);
2499 store_rect:
2500 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2502 /* Visible feedback for debugging. */
2503 #if false && defined HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2511 #endif /* HAVE_WINDOW_SYSTEM */
2513 static void
2514 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 eassert (w);
2517 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2518 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2519 w->window_end_vpos
2520 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2523 /***********************************************************************
2524 Lisp form evaluation
2525 ***********************************************************************/
2527 /* Error handler for safe_eval and safe_call. */
2529 static Lisp_Object
2530 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 add_to_log ("Error during redisplay: %S signaled %S",
2533 Flist (nargs, args), arg);
2534 return Qnil;
2537 /* Call function FUNC with the rest of NARGS - 1 arguments
2538 following. Return the result, or nil if something went
2539 wrong. Prevent redisplay during the evaluation. */
2541 static Lisp_Object
2542 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2544 Lisp_Object val;
2546 if (inhibit_eval_during_redisplay)
2547 val = Qnil;
2548 else
2550 ptrdiff_t i;
2551 ptrdiff_t count = SPECPDL_INDEX ();
2552 Lisp_Object *args;
2553 USE_SAFE_ALLOCA;
2554 SAFE_ALLOCA_LISP (args, nargs);
2556 args[0] = func;
2557 for (i = 1; i < nargs; i++)
2558 args[i] = va_arg (ap, Lisp_Object);
2560 specbind (Qinhibit_redisplay, Qt);
2561 if (inhibit_quit)
2562 specbind (Qinhibit_quit, Qt);
2563 /* Use Qt to ensure debugger does not run,
2564 so there is no possibility of wanting to redisplay. */
2565 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2566 safe_eval_handler);
2567 SAFE_FREE ();
2568 val = unbind_to (count, val);
2571 return val;
2574 Lisp_Object
2575 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2577 Lisp_Object retval;
2578 va_list ap;
2580 va_start (ap, func);
2581 retval = safe__call (false, nargs, func, ap);
2582 va_end (ap);
2583 return retval;
2586 /* Call function FN with one argument ARG.
2587 Return the result, or nil if something went wrong. */
2589 Lisp_Object
2590 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2592 return safe_call (2, fn, arg);
2595 static Lisp_Object
2596 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2598 Lisp_Object retval;
2599 va_list ap;
2601 va_start (ap, fn);
2602 retval = safe__call (inhibit_quit, 2, fn, ap);
2603 va_end (ap);
2604 return retval;
2607 Lisp_Object
2608 safe_eval (Lisp_Object sexpr)
2610 return safe__call1 (false, Qeval, sexpr);
2613 static Lisp_Object
2614 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2616 return safe__call1 (inhibit_quit, Qeval, sexpr);
2619 /* Call function FN with two arguments ARG1 and ARG2.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2625 return safe_call (3, fn, arg1, arg2);
2630 /***********************************************************************
2631 Debugging
2632 ***********************************************************************/
2634 /* Define CHECK_IT to perform sanity checks on iterators.
2635 This is for debugging. It is too slow to do unconditionally. */
2637 static void
2638 CHECK_IT (struct it *it)
2640 #if false
2641 if (it->method == GET_FROM_STRING)
2643 eassert (STRINGP (it->string));
2644 eassert (IT_STRING_CHARPOS (*it) >= 0);
2646 else
2648 eassert (IT_STRING_CHARPOS (*it) < 0);
2649 if (it->method == GET_FROM_BUFFER)
2651 /* Check that character and byte positions agree. */
2652 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2656 if (it->dpvec)
2657 eassert (it->current.dpvec_index >= 0);
2658 else
2659 eassert (it->current.dpvec_index < 0);
2660 #endif
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 CHECK_WINDOW_END (struct window *w)
2670 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2671 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2673 struct glyph_row *row;
2674 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2675 !row->enabled_p
2676 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2677 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2679 #endif
2682 /***********************************************************************
2683 Iterator initialization
2684 ***********************************************************************/
2686 /* Initialize IT for displaying current_buffer in window W, starting
2687 at character position CHARPOS. CHARPOS < 0 means that no buffer
2688 position is specified which is useful when the iterator is assigned
2689 a position later. BYTEPOS is the byte position corresponding to
2690 CHARPOS.
2692 If ROW is not null, calls to produce_glyphs with IT as parameter
2693 will produce glyphs in that row.
2695 BASE_FACE_ID is the id of a base face to use. It must be one of
2696 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2697 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2698 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2700 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2701 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2702 will be initialized to use the corresponding mode line glyph row of
2703 the desired matrix of W. */
2705 void
2706 init_iterator (struct it *it, struct window *w,
2707 ptrdiff_t charpos, ptrdiff_t bytepos,
2708 struct glyph_row *row, enum face_id base_face_id)
2710 enum face_id remapped_base_face_id = base_face_id;
2712 /* Some precondition checks. */
2713 eassert (w != NULL && it != NULL);
2714 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2715 && charpos <= ZV));
2717 /* If face attributes have been changed since the last redisplay,
2718 free realized faces now because they depend on face definitions
2719 that might have changed. Don't free faces while there might be
2720 desired matrices pending which reference these faces. */
2721 if (!inhibit_free_realized_faces)
2723 if (face_change)
2725 face_change = false;
2726 free_all_realized_faces (Qnil);
2728 else if (XFRAME (w->frame)->face_change)
2730 XFRAME (w->frame)->face_change = 0;
2731 free_all_realized_faces (w->frame);
2735 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2736 if (! NILP (Vface_remapping_alist))
2737 remapped_base_face_id
2738 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2740 /* Use one of the mode line rows of W's desired matrix if
2741 appropriate. */
2742 if (row == NULL)
2744 if (base_face_id == MODE_LINE_FACE_ID
2745 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2746 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2747 else if (base_face_id == HEADER_LINE_FACE_ID)
2748 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2751 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2752 Other parts of redisplay rely on that. */
2753 memclear (it, sizeof *it);
2754 it->current.overlay_string_index = -1;
2755 it->current.dpvec_index = -1;
2756 it->base_face_id = remapped_base_face_id;
2757 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2758 it->paragraph_embedding = L2R;
2759 it->bidi_it.w = w;
2761 /* The window in which we iterate over current_buffer: */
2762 XSETWINDOW (it->window, w);
2763 it->w = w;
2764 it->f = XFRAME (w->frame);
2766 it->cmp_it.id = -1;
2768 /* Extra space between lines (on window systems only). */
2769 if (base_face_id == DEFAULT_FACE_ID
2770 && FRAME_WINDOW_P (it->f))
2772 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2774 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2776 * FRAME_LINE_HEIGHT (it->f));
2777 else if (it->f->extra_line_spacing > 0)
2778 it->extra_line_spacing = it->f->extra_line_spacing;
2781 /* If realized faces have been removed, e.g. because of face
2782 attribute changes of named faces, recompute them. When running
2783 in batch mode, the face cache of the initial frame is null. If
2784 we happen to get called, make a dummy face cache. */
2785 if (FRAME_FACE_CACHE (it->f) == NULL)
2786 init_frame_faces (it->f);
2787 if (FRAME_FACE_CACHE (it->f)->used == 0)
2788 recompute_basic_faces (it->f);
2790 it->override_ascent = -1;
2792 /* Are control characters displayed as `^C'? */
2793 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2795 /* -1 means everything between a CR and the following line end
2796 is invisible. >0 means lines indented more than this value are
2797 invisible. */
2798 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2799 ? (clip_to_bounds
2800 (-1, XINT (BVAR (current_buffer, selective_display)),
2801 PTRDIFF_MAX))
2802 : (!NILP (BVAR (current_buffer, selective_display))
2803 ? -1 : 0));
2804 it->selective_display_ellipsis_p
2805 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2807 /* Display table to use. */
2808 it->dp = window_display_table (w);
2810 /* Are multibyte characters enabled in current_buffer? */
2811 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2813 /* Get the position at which the redisplay_end_trigger hook should
2814 be run, if it is to be run at all. */
2815 if (MARKERP (w->redisplay_end_trigger)
2816 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2817 it->redisplay_end_trigger_charpos
2818 = marker_position (w->redisplay_end_trigger);
2819 else if (INTEGERP (w->redisplay_end_trigger))
2820 it->redisplay_end_trigger_charpos
2821 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2822 PTRDIFF_MAX);
2824 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2826 /* Are lines in the display truncated? */
2827 if (TRUNCATE != 0)
2828 it->line_wrap = TRUNCATE;
2829 if (base_face_id == DEFAULT_FACE_ID
2830 && !it->w->hscroll
2831 && (WINDOW_FULL_WIDTH_P (it->w)
2832 || NILP (Vtruncate_partial_width_windows)
2833 || (INTEGERP (Vtruncate_partial_width_windows)
2834 /* PXW: Shall we do something about this? */
2835 && (XINT (Vtruncate_partial_width_windows)
2836 <= WINDOW_TOTAL_COLS (it->w))))
2837 && NILP (BVAR (current_buffer, truncate_lines)))
2838 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2839 ? WINDOW_WRAP : WORD_WRAP;
2841 /* Get dimensions of truncation and continuation glyphs. These are
2842 displayed as fringe bitmaps under X, but we need them for such
2843 frames when the fringes are turned off. But leave the dimensions
2844 zero for tooltip frames, as these glyphs look ugly there and also
2845 sabotage calculations of tooltip dimensions in x-show-tip. */
2846 #ifdef HAVE_WINDOW_SYSTEM
2847 if (!(FRAME_WINDOW_P (it->f)
2848 && FRAMEP (tip_frame)
2849 && it->f == XFRAME (tip_frame)))
2850 #endif
2852 if (it->line_wrap == TRUNCATE)
2854 /* We will need the truncation glyph. */
2855 eassert (it->glyph_row == NULL);
2856 produce_special_glyphs (it, IT_TRUNCATION);
2857 it->truncation_pixel_width = it->pixel_width;
2859 else
2861 /* We will need the continuation glyph. */
2862 eassert (it->glyph_row == NULL);
2863 produce_special_glyphs (it, IT_CONTINUATION);
2864 it->continuation_pixel_width = it->pixel_width;
2868 /* Reset these values to zero because the produce_special_glyphs
2869 above has changed them. */
2870 it->pixel_width = it->ascent = it->descent = 0;
2871 it->phys_ascent = it->phys_descent = 0;
2873 /* Set this after getting the dimensions of truncation and
2874 continuation glyphs, so that we don't produce glyphs when calling
2875 produce_special_glyphs, above. */
2876 it->glyph_row = row;
2877 it->area = TEXT_AREA;
2879 /* Get the dimensions of the display area. The display area
2880 consists of the visible window area plus a horizontally scrolled
2881 part to the left of the window. All x-values are relative to the
2882 start of this total display area. */
2883 if (base_face_id != DEFAULT_FACE_ID)
2885 /* Mode lines, menu bar in terminal frames. */
2886 it->first_visible_x = 0;
2887 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2889 else
2891 it->first_visible_x
2892 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2893 it->last_visible_x = (it->first_visible_x
2894 + window_box_width (w, TEXT_AREA));
2896 /* If we truncate lines, leave room for the truncation glyph(s) at
2897 the right margin. Otherwise, leave room for the continuation
2898 glyph(s). Done only if the window has no right fringe. */
2899 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2901 if (it->line_wrap == TRUNCATE)
2902 it->last_visible_x -= it->truncation_pixel_width;
2903 else
2904 it->last_visible_x -= it->continuation_pixel_width;
2907 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2908 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 /* Leave room for a border glyph. */
2912 if (!FRAME_WINDOW_P (it->f)
2913 && !WINDOW_RIGHTMOST_P (it->w))
2914 it->last_visible_x -= 1;
2916 it->last_visible_y = window_text_bottom_y (w);
2918 /* For mode lines and alike, arrange for the first glyph having a
2919 left box line if the face specifies a box. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 struct face *face;
2924 it->face_id = remapped_base_face_id;
2926 /* If we have a boxed mode line, make the first character appear
2927 with a left box line. */
2928 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2929 if (face && face->box != FACE_NO_BOX)
2930 it->start_of_box_run_p = true;
2933 /* If a buffer position was specified, set the iterator there,
2934 getting overlays and face properties from that position. */
2935 if (charpos >= BUF_BEG (current_buffer))
2937 it->stop_charpos = charpos;
2938 it->end_charpos = ZV;
2939 eassert (charpos == BYTE_TO_CHAR (bytepos));
2940 IT_CHARPOS (*it) = charpos;
2941 IT_BYTEPOS (*it) = bytepos;
2943 /* We will rely on `reseat' to set this up properly, via
2944 handle_face_prop. */
2945 it->face_id = it->base_face_id;
2947 it->start = it->current;
2948 /* Do we need to reorder bidirectional text? Not if this is a
2949 unibyte buffer: by definition, none of the single-byte
2950 characters are strong R2L, so no reordering is needed. And
2951 bidi.c doesn't support unibyte buffers anyway. Also, don't
2952 reorder while we are loading loadup.el, since the tables of
2953 character properties needed for reordering are not yet
2954 available. */
2955 it->bidi_p =
2956 !redisplay__inhibit_bidi
2957 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2958 && it->multibyte_p;
2960 /* If we are to reorder bidirectional text, init the bidi
2961 iterator. */
2962 if (it->bidi_p)
2964 /* Since we don't know at this point whether there will be
2965 any R2L lines in the window, we reserve space for
2966 truncation/continuation glyphs even if only the left
2967 fringe is absent. */
2968 if (base_face_id == DEFAULT_FACE_ID
2969 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2970 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2972 if (it->line_wrap == TRUNCATE)
2973 it->last_visible_x -= it->truncation_pixel_width;
2974 else
2975 it->last_visible_x -= it->continuation_pixel_width;
2977 /* Note the paragraph direction that this buffer wants to
2978 use. */
2979 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2980 Qleft_to_right))
2981 it->paragraph_embedding = L2R;
2982 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2983 Qright_to_left))
2984 it->paragraph_embedding = R2L;
2985 else
2986 it->paragraph_embedding = NEUTRAL_DIR;
2987 bidi_unshelve_cache (NULL, false);
2988 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2989 &it->bidi_it);
2992 /* Compute faces etc. */
2993 reseat (it, it->current.pos, true);
2996 CHECK_IT (it);
3000 /* Initialize IT for the display of window W with window start POS. */
3002 void
3003 start_display (struct it *it, struct window *w, struct text_pos pos)
3005 struct glyph_row *row;
3006 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3008 row = w->desired_matrix->rows + first_vpos;
3009 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3010 it->first_vpos = first_vpos;
3012 /* Don't reseat to previous visible line start if current start
3013 position is in a string or image. */
3014 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3016 int first_y = it->current_y;
3018 /* If window start is not at a line start, skip forward to POS to
3019 get the correct continuation lines width. */
3020 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3021 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3022 if (!start_at_line_beg_p)
3024 int new_x;
3026 reseat_at_previous_visible_line_start (it);
3027 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3029 new_x = it->current_x + it->pixel_width;
3031 /* If lines are continued, this line may end in the middle
3032 of a multi-glyph character (e.g. a control character
3033 displayed as \003, or in the middle of an overlay
3034 string). In this case move_it_to above will not have
3035 taken us to the start of the continuation line but to the
3036 end of the continued line. */
3037 if (it->current_x > 0
3038 && it->line_wrap != TRUNCATE /* Lines are continued. */
3039 && (/* And glyph doesn't fit on the line. */
3040 new_x > it->last_visible_x
3041 /* Or it fits exactly and we're on a window
3042 system frame. */
3043 || (new_x == it->last_visible_x
3044 && FRAME_WINDOW_P (it->f)
3045 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3046 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3047 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3049 if ((it->current.dpvec_index >= 0
3050 || it->current.overlay_string_index >= 0)
3051 /* If we are on a newline from a display vector or
3052 overlay string, then we are already at the end of
3053 a screen line; no need to go to the next line in
3054 that case, as this line is not really continued.
3055 (If we do go to the next line, C-e will not DTRT.) */
3056 && it->c != '\n')
3058 set_iterator_to_next (it, true);
3059 move_it_in_display_line_to (it, -1, -1, 0);
3062 it->continuation_lines_width += it->current_x;
3064 /* If the character at POS is displayed via a display
3065 vector, move_it_to above stops at the final glyph of
3066 IT->dpvec. To make the caller redisplay that character
3067 again (a.k.a. start at POS), we need to reset the
3068 dpvec_index to the beginning of IT->dpvec. */
3069 else if (it->current.dpvec_index >= 0)
3070 it->current.dpvec_index = 0;
3072 /* We're starting a new display line, not affected by the
3073 height of the continued line, so clear the appropriate
3074 fields in the iterator structure. */
3075 it->max_ascent = it->max_descent = 0;
3076 it->max_phys_ascent = it->max_phys_descent = 0;
3078 it->current_y = first_y;
3079 it->vpos = 0;
3080 it->current_x = it->hpos = 0;
3086 /* Return true if POS is a position in ellipses displayed for invisible
3087 text. W is the window we display, for text property lookup. */
3089 static bool
3090 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3092 Lisp_Object prop, window;
3093 bool ellipses_p = false;
3094 ptrdiff_t charpos = CHARPOS (pos->pos);
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (pos->dpvec_index >= 0
3101 && pos->overlay_string_index < 0
3102 && CHARPOS (pos->string_pos) < 0
3103 && charpos > BEGV
3104 && (XSETWINDOW (window, w),
3105 prop = Fget_char_property (make_number (charpos),
3106 Qinvisible, window),
3107 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3109 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3110 window);
3111 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3114 return ellipses_p;
3118 /* Initialize IT for stepping through current_buffer in window W,
3119 starting at position POS that includes overlay string and display
3120 vector/ control character translation position information. Value
3121 is false if there are overlay strings with newlines at POS. */
3123 static bool
3124 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3126 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3127 int i;
3128 bool overlay_strings_with_newlines = false;
3130 /* If POS specifies a position in a display vector, this might
3131 be for an ellipsis displayed for invisible text. We won't
3132 get the iterator set up for delivering that ellipsis unless
3133 we make sure that it gets aware of the invisible text. */
3134 if (in_ellipses_for_invisible_text_p (pos, w))
3136 --charpos;
3137 bytepos = 0;
3140 /* Keep in mind: the call to reseat in init_iterator skips invisible
3141 text, so we might end up at a position different from POS. This
3142 is only a problem when POS is a row start after a newline and an
3143 overlay starts there with an after-string, and the overlay has an
3144 invisible property. Since we don't skip invisible text in
3145 display_line and elsewhere immediately after consuming the
3146 newline before the row start, such a POS will not be in a string,
3147 but the call to init_iterator below will move us to the
3148 after-string. */
3149 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3151 /* This only scans the current chunk -- it should scan all chunks.
3152 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3153 to 16 in 22.1 to make this a lesser problem. */
3154 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3156 const char *s = SSDATA (it->overlay_strings[i]);
3157 const char *e = s + SBYTES (it->overlay_strings[i]);
3159 while (s < e && *s != '\n')
3160 ++s;
3162 if (s < e)
3164 overlay_strings_with_newlines = true;
3165 break;
3169 /* If position is within an overlay string, set up IT to the right
3170 overlay string. */
3171 if (pos->overlay_string_index >= 0)
3173 int relative_index;
3175 /* If the first overlay string happens to have a `display'
3176 property for an image, the iterator will be set up for that
3177 image, and we have to undo that setup first before we can
3178 correct the overlay string index. */
3179 if (it->method == GET_FROM_IMAGE)
3180 pop_it (it);
3182 /* We already have the first chunk of overlay strings in
3183 IT->overlay_strings. Load more until the one for
3184 pos->overlay_string_index is in IT->overlay_strings. */
3185 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3187 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3188 it->current.overlay_string_index = 0;
3189 while (n--)
3191 load_overlay_strings (it, 0);
3192 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = pos->overlay_string_index;
3197 relative_index = (it->current.overlay_string_index
3198 % OVERLAY_STRING_CHUNK_SIZE);
3199 it->string = it->overlay_strings[relative_index];
3200 eassert (STRINGP (it->string));
3201 it->current.string_pos = pos->string_pos;
3202 it->method = GET_FROM_STRING;
3203 it->end_charpos = SCHARS (it->string);
3204 /* Set up the bidi iterator for this overlay string. */
3205 if (it->bidi_p)
3207 it->bidi_it.string.lstring = it->string;
3208 it->bidi_it.string.s = NULL;
3209 it->bidi_it.string.schars = SCHARS (it->string);
3210 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3211 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3212 it->bidi_it.string.unibyte = !it->multibyte_p;
3213 it->bidi_it.w = it->w;
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3217 /* Synchronize the state of the bidi iterator with
3218 pos->string_pos. For any string position other than
3219 zero, this will be done automagically when we resume
3220 iteration over the string and get_visually_first_element
3221 is called. But if string_pos is zero, and the string is
3222 to be reordered for display, we need to resync manually,
3223 since it could be that the iteration state recorded in
3224 pos ended at string_pos of 0 moving backwards in string. */
3225 if (CHARPOS (pos->string_pos) == 0)
3227 get_visually_first_element (it);
3228 if (IT_STRING_CHARPOS (*it) != 0)
3229 do {
3230 /* Paranoia. */
3231 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3232 bidi_move_to_visually_next (&it->bidi_it);
3233 } while (it->bidi_it.charpos != 0);
3235 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3236 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3240 if (CHARPOS (pos->string_pos) >= 0)
3242 /* Recorded position is not in an overlay string, but in another
3243 string. This can only be a string from a `display' property.
3244 IT should already be filled with that string. */
3245 it->current.string_pos = pos->string_pos;
3246 eassert (STRINGP (it->string));
3247 if (it->bidi_p)
3248 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3249 FRAME_WINDOW_P (it->f), &it->bidi_it);
3252 /* Restore position in display vector translations, control
3253 character translations or ellipses. */
3254 if (pos->dpvec_index >= 0)
3256 if (it->dpvec == NULL)
3257 get_next_display_element (it);
3258 eassert (it->dpvec && it->current.dpvec_index == 0);
3259 it->current.dpvec_index = pos->dpvec_index;
3262 CHECK_IT (it);
3263 return !overlay_strings_with_newlines;
3267 /* Initialize IT for stepping through current_buffer in window W
3268 starting at ROW->start. */
3270 static void
3271 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3273 init_from_display_pos (it, w, &row->start);
3274 it->start = row->start;
3275 it->continuation_lines_width = row->continuation_lines_width;
3276 CHECK_IT (it);
3280 /* Initialize IT for stepping through current_buffer in window W
3281 starting in the line following ROW, i.e. starting at ROW->end.
3282 Value is false if there are overlay strings with newlines at ROW's
3283 end position. */
3285 static bool
3286 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3288 bool success = false;
3290 if (init_from_display_pos (it, w, &row->end))
3292 if (row->continued_p)
3293 it->continuation_lines_width
3294 = row->continuation_lines_width + row->pixel_width;
3295 CHECK_IT (it);
3296 success = true;
3299 return success;
3305 /***********************************************************************
3306 Text properties
3307 ***********************************************************************/
3309 /* Called when IT reaches IT->stop_charpos. Handle text property and
3310 overlay changes. Set IT->stop_charpos to the next position where
3311 to stop. */
3313 static void
3314 handle_stop (struct it *it)
3316 enum prop_handled handled;
3317 bool handle_overlay_change_p;
3318 struct props *p;
3320 it->dpvec = NULL;
3321 it->current.dpvec_index = -1;
3322 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3323 it->ellipsis_p = false;
3325 /* Use face of preceding text for ellipsis (if invisible) */
3326 if (it->selective_display_ellipsis_p)
3327 it->saved_face_id = it->face_id;
3329 /* Here's the description of the semantics of, and the logic behind,
3330 the various HANDLED_* statuses:
3332 HANDLED_NORMALLY means the handler did its job, and the loop
3333 should proceed to calling the next handler in order.
3335 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3336 change in the properties and overlays at current position, so the
3337 loop should be restarted, to re-invoke the handlers that were
3338 already called. This happens when fontification-functions were
3339 called by handle_fontified_prop, and actually fontified
3340 something. Another case where HANDLED_RECOMPUTE_PROPS is
3341 returned is when we discover overlay strings that need to be
3342 displayed right away. The loop below will continue for as long
3343 as the status is HANDLED_RECOMPUTE_PROPS.
3345 HANDLED_RETURN means return immediately to the caller, to
3346 continue iteration without calling any further handlers. This is
3347 used when we need to act on some property right away, for example
3348 when we need to display the ellipsis or a replacing display
3349 property, such as display string or image.
3351 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3352 consumed, and the handler switched to the next overlay string.
3353 This signals the loop below to refrain from looking for more
3354 overlays before all the overlay strings of the current overlay
3355 are processed.
3357 Some of the handlers called by the loop push the iterator state
3358 onto the stack (see 'push_it'), and arrange for the iteration to
3359 continue with another object, such as an image, a display string,
3360 or an overlay string. In most such cases, it->stop_charpos is
3361 set to the first character of the string, so that when the
3362 iteration resumes, this function will immediately be called
3363 again, to examine the properties at the beginning of the string.
3365 When a display or overlay string is exhausted, the iterator state
3366 is popped (see 'pop_it'), and iteration continues with the
3367 previous object. Again, in many such cases this function is
3368 called again to find the next position where properties might
3369 change. */
3373 handled = HANDLED_NORMALLY;
3375 /* Call text property handlers. */
3376 for (p = it_props; p->handler; ++p)
3378 handled = p->handler (it);
3380 if (handled == HANDLED_RECOMPUTE_PROPS)
3381 break;
3382 else if (handled == HANDLED_RETURN)
3384 /* We still want to show before and after strings from
3385 overlays even if the actual buffer text is replaced. */
3386 if (!handle_overlay_change_p
3387 || it->sp > 1
3388 /* Don't call get_overlay_strings_1 if we already
3389 have overlay strings loaded, because doing so
3390 will load them again and push the iterator state
3391 onto the stack one more time, which is not
3392 expected by the rest of the code that processes
3393 overlay strings. */
3394 || (it->current.overlay_string_index < 0
3395 && !get_overlay_strings_1 (it, 0, false)))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->string_from_display_prop_p = false;
3413 it->from_disp_prop_p = false;
3414 handle_overlay_change_p = false;
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 break;
3419 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3420 handle_overlay_change_p = false;
3423 if (handled != HANDLED_RECOMPUTE_PROPS)
3425 /* Don't check for overlay strings below when set to deliver
3426 characters from a display vector. */
3427 if (it->method == GET_FROM_DISPLAY_VECTOR)
3428 handle_overlay_change_p = false;
3430 /* Handle overlay changes.
3431 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3432 if it finds overlays. */
3433 if (handle_overlay_change_p)
3434 handled = handle_overlay_change (it);
3437 if (it->ellipsis_p)
3439 setup_for_ellipsis (it, 0);
3440 break;
3443 while (handled == HANDLED_RECOMPUTE_PROPS);
3445 /* Determine where to stop next. */
3446 if (handled == HANDLED_NORMALLY)
3447 compute_stop_pos (it);
3451 /* Compute IT->stop_charpos from text property and overlay change
3452 information for IT's current position. */
3454 static void
3455 compute_stop_pos (struct it *it)
3457 register INTERVAL iv, next_iv;
3458 Lisp_Object object, limit, position;
3459 ptrdiff_t charpos, bytepos;
3461 if (STRINGP (it->string))
3463 /* Strings are usually short, so don't limit the search for
3464 properties. */
3465 it->stop_charpos = it->end_charpos;
3466 object = it->string;
3467 limit = Qnil;
3468 charpos = IT_STRING_CHARPOS (*it);
3469 bytepos = IT_STRING_BYTEPOS (*it);
3471 else
3473 ptrdiff_t pos;
3475 /* If end_charpos is out of range for some reason, such as a
3476 misbehaving display function, rationalize it (Bug#5984). */
3477 if (it->end_charpos > ZV)
3478 it->end_charpos = ZV;
3479 it->stop_charpos = it->end_charpos;
3481 /* If next overlay change is in front of the current stop pos
3482 (which is IT->end_charpos), stop there. Note: value of
3483 next_overlay_change is point-max if no overlay change
3484 follows. */
3485 charpos = IT_CHARPOS (*it);
3486 bytepos = IT_BYTEPOS (*it);
3487 pos = next_overlay_change (charpos);
3488 if (pos < it->stop_charpos)
3489 it->stop_charpos = pos;
3491 /* Set up variables for computing the stop position from text
3492 property changes. */
3493 XSETBUFFER (object, current_buffer);
3494 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3497 /* Get the interval containing IT's position. Value is a null
3498 interval if there isn't such an interval. */
3499 position = make_number (charpos);
3500 iv = validate_interval_range (object, &position, &position, false);
3501 if (iv)
3503 Lisp_Object values_here[LAST_PROP_IDX];
3504 struct props *p;
3506 /* Get properties here. */
3507 for (p = it_props; p->handler; ++p)
3508 values_here[p->idx] = textget (iv->plist,
3509 builtin_lisp_symbol (p->name));
3511 /* Look for an interval following iv that has different
3512 properties. */
3513 for (next_iv = next_interval (iv);
3514 (next_iv
3515 && (NILP (limit)
3516 || XFASTINT (limit) > next_iv->position));
3517 next_iv = next_interval (next_iv))
3519 for (p = it_props; p->handler; ++p)
3521 Lisp_Object new_value = textget (next_iv->plist,
3522 builtin_lisp_symbol (p->name));
3523 if (!EQ (values_here[p->idx], new_value))
3524 break;
3527 if (p->handler)
3528 break;
3531 if (next_iv)
3533 if (INTEGERP (limit)
3534 && next_iv->position >= XFASTINT (limit))
3535 /* No text property change up to limit. */
3536 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3537 else
3538 /* Text properties change in next_iv. */
3539 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3543 if (it->cmp_it.id < 0)
3545 ptrdiff_t stoppos = it->end_charpos;
3547 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3548 stoppos = -1;
3549 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3550 stoppos, it->string);
3553 eassert (STRINGP (it->string)
3554 || (it->stop_charpos >= BEGV
3555 && it->stop_charpos >= IT_CHARPOS (*it)));
3559 /* Return the position of the next overlay change after POS in
3560 current_buffer. Value is point-max if no overlay change
3561 follows. This is like `next-overlay-change' but doesn't use
3562 xmalloc. */
3564 static ptrdiff_t
3565 next_overlay_change (ptrdiff_t pos)
3567 ptrdiff_t i, noverlays;
3568 ptrdiff_t endpos;
3569 Lisp_Object *overlays;
3570 USE_SAFE_ALLOCA;
3572 /* Get all overlays at the given position. */
3573 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3575 /* If any of these overlays ends before endpos,
3576 use its ending point instead. */
3577 for (i = 0; i < noverlays; ++i)
3579 Lisp_Object oend;
3580 ptrdiff_t oendpos;
3582 oend = OVERLAY_END (overlays[i]);
3583 oendpos = OVERLAY_POSITION (oend);
3584 endpos = min (endpos, oendpos);
3587 SAFE_FREE ();
3588 return endpos;
3591 /* How many characters forward to search for a display property or
3592 display string. Searching too far forward makes the bidi display
3593 sluggish, especially in small windows. */
3594 #define MAX_DISP_SCAN 250
3596 /* Return the character position of a display string at or after
3597 position specified by POSITION. If no display string exists at or
3598 after POSITION, return ZV. A display string is either an overlay
3599 with `display' property whose value is a string, or a `display'
3600 text property whose value is a string. STRING is data about the
3601 string to iterate; if STRING->lstring is nil, we are iterating a
3602 buffer. FRAME_WINDOW_P is true when we are displaying a window
3603 on a GUI frame. DISP_PROP is set to zero if we searched
3604 MAX_DISP_SCAN characters forward without finding any display
3605 strings, non-zero otherwise. It is set to 2 if the display string
3606 uses any kind of `(space ...)' spec that will produce a stretch of
3607 white space in the text area. */
3608 ptrdiff_t
3609 compute_display_string_pos (struct text_pos *position,
3610 struct bidi_string_data *string,
3611 struct window *w,
3612 bool frame_window_p, int *disp_prop)
3614 /* OBJECT = nil means current buffer. */
3615 Lisp_Object object, object1;
3616 Lisp_Object pos, spec, limpos;
3617 bool string_p = string && (STRINGP (string->lstring) || string->s);
3618 ptrdiff_t eob = string_p ? string->schars : ZV;
3619 ptrdiff_t begb = string_p ? 0 : BEGV;
3620 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3621 ptrdiff_t lim =
3622 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3623 struct text_pos tpos;
3624 int rv = 0;
3626 if (string && STRINGP (string->lstring))
3627 object1 = object = string->lstring;
3628 else if (w && !string_p)
3630 XSETWINDOW (object, w);
3631 object1 = Qnil;
3633 else
3634 object1 = object = Qnil;
3636 *disp_prop = 1;
3638 if (charpos >= eob
3639 /* We don't support display properties whose values are strings
3640 that have display string properties. */
3641 || string->from_disp_str
3642 /* C strings cannot have display properties. */
3643 || (string->s && !STRINGP (object)))
3645 *disp_prop = 0;
3646 return eob;
3649 /* If the character at CHARPOS is where the display string begins,
3650 return CHARPOS. */
3651 pos = make_number (charpos);
3652 if (STRINGP (object))
3653 bufpos = string->bufpos;
3654 else
3655 bufpos = charpos;
3656 tpos = *position;
3657 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3658 && (charpos <= begb
3659 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3660 object),
3661 spec))
3662 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3663 frame_window_p)))
3665 if (rv == 2)
3666 *disp_prop = 2;
3667 return charpos;
3670 /* Look forward for the first character with a `display' property
3671 that will replace the underlying text when displayed. */
3672 limpos = make_number (lim);
3673 do {
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3675 CHARPOS (tpos) = XFASTINT (pos);
3676 if (CHARPOS (tpos) >= lim)
3678 *disp_prop = 0;
3679 break;
3681 if (STRINGP (object))
3682 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3683 else
3684 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3685 spec = Fget_char_property (pos, Qdisplay, object);
3686 if (!STRINGP (object))
3687 bufpos = CHARPOS (tpos);
3688 } while (NILP (spec)
3689 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3690 bufpos, frame_window_p)));
3691 if (rv == 2)
3692 *disp_prop = 2;
3694 return CHARPOS (tpos);
3697 /* Return the character position of the end of the display string that
3698 started at CHARPOS. If there's no display string at CHARPOS,
3699 return -1. A display string is either an overlay with `display'
3700 property whose value is a string or a `display' text property whose
3701 value is a string. */
3702 ptrdiff_t
3703 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3705 /* OBJECT = nil means current buffer. */
3706 Lisp_Object object =
3707 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3708 Lisp_Object pos = make_number (charpos);
3709 ptrdiff_t eob =
3710 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3712 if (charpos >= eob || (string->s && !STRINGP (object)))
3713 return eob;
3715 /* It could happen that the display property or overlay was removed
3716 since we found it in compute_display_string_pos above. One way
3717 this can happen is if JIT font-lock was called (through
3718 handle_fontified_prop), and jit-lock-functions remove text
3719 properties or overlays from the portion of buffer that includes
3720 CHARPOS. Muse mode is known to do that, for example. In this
3721 case, we return -1 to the caller, to signal that no display
3722 string is actually present at CHARPOS. See bidi_fetch_char for
3723 how this is handled.
3725 An alternative would be to never look for display properties past
3726 it->stop_charpos. But neither compute_display_string_pos nor
3727 bidi_fetch_char that calls it know or care where the next
3728 stop_charpos is. */
3729 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3730 return -1;
3732 /* Look forward for the first character where the `display' property
3733 changes. */
3734 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3736 return XFASTINT (pos);
3741 /***********************************************************************
3742 Fontification
3743 ***********************************************************************/
3745 /* Handle changes in the `fontified' property of the current buffer by
3746 calling hook functions from Qfontification_functions to fontify
3747 regions of text. */
3749 static enum prop_handled
3750 handle_fontified_prop (struct it *it)
3752 Lisp_Object prop, pos;
3753 enum prop_handled handled = HANDLED_NORMALLY;
3755 if (!NILP (Vmemory_full))
3756 return handled;
3758 /* Get the value of the `fontified' property at IT's current buffer
3759 position. (The `fontified' property doesn't have a special
3760 meaning in strings.) If the value is nil, call functions from
3761 Qfontification_functions. */
3762 if (!STRINGP (it->string)
3763 && it->s == NULL
3764 && !NILP (Vfontification_functions)
3765 && !NILP (Vrun_hooks)
3766 && (pos = make_number (IT_CHARPOS (*it)),
3767 prop = Fget_char_property (pos, Qfontified, Qnil),
3768 /* Ignore the special cased nil value always present at EOB since
3769 no amount of fontifying will be able to change it. */
3770 NILP (prop) && IT_CHARPOS (*it) < Z))
3772 ptrdiff_t count = SPECPDL_INDEX ();
3773 Lisp_Object val;
3774 struct buffer *obuf = current_buffer;
3775 ptrdiff_t begv = BEGV, zv = ZV;
3776 bool old_clip_changed = current_buffer->clip_changed;
3778 val = Vfontification_functions;
3779 specbind (Qfontification_functions, Qnil);
3781 eassert (it->end_charpos == ZV);
3783 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3784 safe_call1 (val, pos);
3785 else
3787 Lisp_Object fns, fn;
3789 fns = Qnil;
3791 for (; CONSP (val); val = XCDR (val))
3793 fn = XCAR (val);
3795 if (EQ (fn, Qt))
3797 /* A value of t indicates this hook has a local
3798 binding; it means to run the global binding too.
3799 In a global value, t should not occur. If it
3800 does, we must ignore it to avoid an endless
3801 loop. */
3802 for (fns = Fdefault_value (Qfontification_functions);
3803 CONSP (fns);
3804 fns = XCDR (fns))
3806 fn = XCAR (fns);
3807 if (!EQ (fn, Qt))
3808 safe_call1 (fn, pos);
3811 else
3812 safe_call1 (fn, pos);
3816 unbind_to (count, Qnil);
3818 /* Fontification functions routinely call `save-restriction'.
3819 Normally, this tags clip_changed, which can confuse redisplay
3820 (see discussion in Bug#6671). Since we don't perform any
3821 special handling of fontification changes in the case where
3822 `save-restriction' isn't called, there's no point doing so in
3823 this case either. So, if the buffer's restrictions are
3824 actually left unchanged, reset clip_changed. */
3825 if (obuf == current_buffer)
3827 if (begv == BEGV && zv == ZV)
3828 current_buffer->clip_changed = old_clip_changed;
3830 /* There isn't much we can reasonably do to protect against
3831 misbehaving fontification, but here's a fig leaf. */
3832 else if (BUFFER_LIVE_P (obuf))
3833 set_buffer_internal_1 (obuf);
3835 /* The fontification code may have added/removed text.
3836 It could do even a lot worse, but let's at least protect against
3837 the most obvious case where only the text past `pos' gets changed',
3838 as is/was done in grep.el where some escapes sequences are turned
3839 into face properties (bug#7876). */
3840 it->end_charpos = ZV;
3842 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3843 something. This avoids an endless loop if they failed to
3844 fontify the text for which reason ever. */
3845 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3846 handled = HANDLED_RECOMPUTE_PROPS;
3849 return handled;
3854 /***********************************************************************
3855 Faces
3856 ***********************************************************************/
3858 /* Set up iterator IT from face properties at its current position.
3859 Called from handle_stop. */
3861 static enum prop_handled
3862 handle_face_prop (struct it *it)
3864 int new_face_id;
3865 ptrdiff_t next_stop;
3867 if (!STRINGP (it->string))
3869 new_face_id
3870 = face_at_buffer_position (it->w,
3871 IT_CHARPOS (*it),
3872 &next_stop,
3873 (IT_CHARPOS (*it)
3874 + TEXT_PROP_DISTANCE_LIMIT),
3875 false, it->base_face_id);
3877 /* Is this a start of a run of characters with box face?
3878 Caveat: this can be called for a freshly initialized
3879 iterator; face_id is -1 in this case. We know that the new
3880 face will not change until limit, i.e. if the new face has a
3881 box, all characters up to limit will have one. But, as
3882 usual, we don't know whether limit is really the end. */
3883 if (new_face_id != it->face_id)
3885 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3886 /* If it->face_id is -1, old_face below will be NULL, see
3887 the definition of FACE_FROM_ID_OR_NULL. This will happen
3888 if this is the initial call that gets the face. */
3889 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3891 /* If the value of face_id of the iterator is -1, we have to
3892 look in front of IT's position and see whether there is a
3893 face there that's different from new_face_id. */
3894 if (!old_face && IT_CHARPOS (*it) > BEG)
3896 int prev_face_id = face_before_it_pos (it);
3898 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3901 /* If the new face has a box, but the old face does not,
3902 this is the start of a run of characters with box face,
3903 i.e. this character has a shadow on the left side. */
3904 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3905 && (old_face == NULL || !old_face->box));
3906 it->face_box_p = new_face->box != FACE_NO_BOX;
3909 else
3911 int base_face_id;
3912 ptrdiff_t bufpos;
3913 int i;
3914 Lisp_Object from_overlay
3915 = (it->current.overlay_string_index >= 0
3916 ? it->string_overlays[it->current.overlay_string_index
3917 % OVERLAY_STRING_CHUNK_SIZE]
3918 : Qnil);
3920 /* See if we got to this string directly or indirectly from
3921 an overlay property. That includes the before-string or
3922 after-string of an overlay, strings in display properties
3923 provided by an overlay, their text properties, etc.
3925 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3926 if (! NILP (from_overlay))
3927 for (i = it->sp - 1; i >= 0; i--)
3929 if (it->stack[i].current.overlay_string_index >= 0)
3930 from_overlay
3931 = it->string_overlays[it->stack[i].current.overlay_string_index
3932 % OVERLAY_STRING_CHUNK_SIZE];
3933 else if (! NILP (it->stack[i].from_overlay))
3934 from_overlay = it->stack[i].from_overlay;
3936 if (!NILP (from_overlay))
3937 break;
3940 if (! NILP (from_overlay))
3942 bufpos = IT_CHARPOS (*it);
3943 /* For a string from an overlay, the base face depends
3944 only on text properties and ignores overlays. */
3945 base_face_id
3946 = face_for_overlay_string (it->w,
3947 IT_CHARPOS (*it),
3948 &next_stop,
3949 (IT_CHARPOS (*it)
3950 + TEXT_PROP_DISTANCE_LIMIT),
3951 false,
3952 from_overlay);
3954 else
3956 bufpos = 0;
3958 /* For strings from a `display' property, use the face at
3959 IT's current buffer position as the base face to merge
3960 with, so that overlay strings appear in the same face as
3961 surrounding text, unless they specify their own faces.
3962 For strings from wrap-prefix and line-prefix properties,
3963 use the default face, possibly remapped via
3964 Vface_remapping_alist. */
3965 /* Note that the fact that we use the face at _buffer_
3966 position means that a 'display' property on an overlay
3967 string will not inherit the face of that overlay string,
3968 but will instead revert to the face of buffer text
3969 covered by the overlay. This is visible, e.g., when the
3970 overlay specifies a box face, but neither the buffer nor
3971 the display string do. This sounds like a design bug,
3972 but Emacs always did that since v21.1, so changing that
3973 might be a big deal. */
3974 base_face_id = it->string_from_prefix_prop_p
3975 ? (!NILP (Vface_remapping_alist)
3976 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3977 : DEFAULT_FACE_ID)
3978 : underlying_face_id (it);
3981 new_face_id = face_at_string_position (it->w,
3982 it->string,
3983 IT_STRING_CHARPOS (*it),
3984 bufpos,
3985 &next_stop,
3986 base_face_id, false);
3988 /* Is this a start of a run of characters with box? Caveat:
3989 this can be called for a freshly allocated iterator; face_id
3990 is -1 is this case. We know that the new face will not
3991 change until the next check pos, i.e. if the new face has a
3992 box, all characters up to that position will have a
3993 box. But, as usual, we don't know whether that position
3994 is really the end. */
3995 if (new_face_id != it->face_id)
3997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3998 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4000 /* If new face has a box but old face hasn't, this is the
4001 start of a run of characters with box, i.e. it has a
4002 shadow on the left side. */
4003 it->start_of_box_run_p
4004 = new_face->box && (old_face == NULL || !old_face->box);
4005 it->face_box_p = new_face->box != FACE_NO_BOX;
4009 it->face_id = new_face_id;
4010 return HANDLED_NORMALLY;
4014 /* Return the ID of the face ``underlying'' IT's current position,
4015 which is in a string. If the iterator is associated with a
4016 buffer, return the face at IT's current buffer position.
4017 Otherwise, use the iterator's base_face_id. */
4019 static int
4020 underlying_face_id (struct it *it)
4022 int face_id = it->base_face_id, i;
4024 eassert (STRINGP (it->string));
4026 for (i = it->sp - 1; i >= 0; --i)
4027 if (NILP (it->stack[i].string))
4028 face_id = it->stack[i].face_id;
4030 return face_id;
4034 /* Compute the face one character before or after the current position
4035 of IT, in the visual order. BEFORE_P means get the face
4036 in front (to the left in L2R paragraphs, to the right in R2L
4037 paragraphs) of IT's screen position. Value is the ID of the face. */
4039 static int
4040 face_before_or_after_it_pos (struct it *it, bool before_p)
4042 int face_id, limit;
4043 ptrdiff_t next_check_charpos;
4044 struct it it_copy;
4045 void *it_copy_data = NULL;
4047 eassert (it->s == NULL);
4049 if (STRINGP (it->string))
4051 ptrdiff_t bufpos, charpos;
4052 int base_face_id;
4054 /* No face change past the end of the string (for the case
4055 we are padding with spaces). No face change before the
4056 string start. */
4057 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4058 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4059 return it->face_id;
4061 if (!it->bidi_p)
4063 /* Set charpos to the position before or after IT's current
4064 position, in the logical order, which in the non-bidi
4065 case is the same as the visual order. */
4066 if (before_p)
4067 charpos = IT_STRING_CHARPOS (*it) - 1;
4068 else if (it->what == IT_COMPOSITION)
4069 /* For composition, we must check the character after the
4070 composition. */
4071 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4072 else
4073 charpos = IT_STRING_CHARPOS (*it) + 1;
4075 else
4077 if (before_p)
4079 /* With bidi iteration, the character before the current
4080 in the visual order cannot be found by simple
4081 iteration, because "reverse" reordering is not
4082 supported. Instead, we need to start from the string
4083 beginning and go all the way to the current string
4084 position, remembering the previous position. */
4085 /* Ignore face changes before the first visible
4086 character on this display line. */
4087 if (it->current_x <= it->first_visible_x)
4088 return it->face_id;
4089 SAVE_IT (it_copy, *it, it_copy_data);
4090 IT_STRING_CHARPOS (it_copy) = 0;
4091 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4095 charpos = IT_STRING_CHARPOS (it_copy);
4096 if (charpos >= SCHARS (it->string))
4097 break;
4098 bidi_move_to_visually_next (&it_copy.bidi_it);
4100 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 int current_x;
4183 /* With bidi iteration, the character before the current
4184 in the visual order cannot be found by simple
4185 iteration, because "reverse" reordering is not
4186 supported. Instead, we need to use the move_it_*
4187 family of functions, and move to the previous
4188 character starting from the beginning of the visual
4189 line. */
4190 /* Ignore face changes before the first visible
4191 character on this display line. */
4192 if (it->current_x <= it->first_visible_x)
4193 return it->face_id;
4194 SAVE_IT (it_copy, *it, it_copy_data);
4195 /* Implementation note: Since move_it_in_display_line
4196 works in the iterator geometry, and thinks the first
4197 character is always the leftmost, even in R2L lines,
4198 we don't need to distinguish between the R2L and L2R
4199 cases here. */
4200 current_x = it_copy.current_x;
4201 move_it_vertically_backward (&it_copy, 0);
4202 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4203 pos = it_copy.current.pos;
4204 RESTORE_IT (it, it, it_copy_data);
4206 else
4208 /* Set charpos to the buffer position of the character
4209 that comes after IT's current position in the visual
4210 order. */
4211 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4213 it_copy = *it;
4214 while (n--)
4215 bidi_move_to_visually_next (&it_copy.bidi_it);
4217 SET_TEXT_POS (pos,
4218 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4221 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4223 /* Determine face for CHARSET_ASCII, or unibyte. */
4224 face_id = face_at_buffer_position (it->w,
4225 CHARPOS (pos),
4226 &next_check_charpos,
4227 limit, false, -1);
4229 /* Correct the face for charsets different from ASCII. Do it
4230 for the multibyte case only. The face returned above is
4231 suitable for unibyte text if current_buffer is unibyte. */
4232 if (it->multibyte_p)
4234 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4235 struct face *face = FACE_FROM_ID (it->f, face_id);
4236 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4240 return face_id;
4245 /***********************************************************************
4246 Invisible text
4247 ***********************************************************************/
4249 /* Set up iterator IT from invisible properties at its current
4250 position. Called from handle_stop. */
4252 static enum prop_handled
4253 handle_invisible_prop (struct it *it)
4255 enum prop_handled handled = HANDLED_NORMALLY;
4256 int invis;
4257 Lisp_Object prop;
4259 if (STRINGP (it->string))
4261 Lisp_Object end_charpos, limit;
4263 /* Get the value of the invisible text property at the
4264 current position. Value will be nil if there is no such
4265 property. */
4266 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4267 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4268 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4272 /* Record whether we have to display an ellipsis for the
4273 invisible text. */
4274 bool display_ellipsis_p = (invis == 2);
4275 ptrdiff_t len, endpos;
4277 handled = HANDLED_RECOMPUTE_PROPS;
4279 /* Get the position at which the next visible text can be
4280 found in IT->string, if any. */
4281 endpos = len = SCHARS (it->string);
4282 XSETINT (limit, len);
4285 end_charpos
4286 = Fnext_single_property_change (end_charpos, Qinvisible,
4287 it->string, limit);
4288 /* Since LIMIT is always an integer, so should be the
4289 value returned by Fnext_single_property_change. */
4290 eassert (INTEGERP (end_charpos));
4291 if (INTEGERP (end_charpos))
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis == 2)
4297 display_ellipsis_p = true;
4299 else /* Should never happen; but if it does, exit the loop. */
4300 endpos = len;
4302 while (invis != 0 && endpos < len);
4304 if (display_ellipsis_p)
4305 it->ellipsis_p = true;
4307 if (endpos < len)
4309 /* Text at END_CHARPOS is visible. Move IT there. */
4310 struct text_pos old;
4311 ptrdiff_t oldpos;
4313 old = it->current.string_pos;
4314 oldpos = CHARPOS (old);
4315 if (it->bidi_p)
4317 if (it->bidi_it.first_elt
4318 && it->bidi_it.charpos < SCHARS (it->string))
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, true);
4321 /* Bidi-iterate out of the invisible text. */
4324 bidi_move_to_visually_next (&it->bidi_it);
4326 while (oldpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < endpos);
4329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4331 if (IT_CHARPOS (*it) >= endpos)
4332 it->prev_stop = endpos;
4334 else
4336 IT_STRING_CHARPOS (*it) = endpos;
4337 compute_string_pos (&it->current.string_pos, old, it->string);
4340 else
4342 /* The rest of the string is invisible. If this is an
4343 overlay string, proceed with the next overlay string
4344 or whatever comes and return a character from there. */
4345 if (it->current.overlay_string_index >= 0
4346 && !display_ellipsis_p)
4348 next_overlay_string (it);
4349 /* Don't check for overlay strings when we just
4350 finished processing them. */
4351 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4353 else
4355 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4356 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4361 else
4363 ptrdiff_t newpos, next_stop, start_charpos, tem;
4364 Lisp_Object pos, overlay;
4366 /* First of all, is there invisible text at this position? */
4367 tem = start_charpos = IT_CHARPOS (*it);
4368 pos = make_number (tem);
4369 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4370 &overlay);
4371 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4373 /* If we are on invisible text, skip over it. */
4374 if (invis != 0 && start_charpos < it->end_charpos)
4376 /* Record whether we have to display an ellipsis for the
4377 invisible text. */
4378 bool display_ellipsis_p = invis == 2;
4380 handled = HANDLED_RECOMPUTE_PROPS;
4382 /* Loop skipping over invisible text. The loop is left at
4383 ZV or with IT on the first char being visible again. */
4386 /* Try to skip some invisible text. Return value is the
4387 position reached which can be equal to where we start
4388 if there is nothing invisible there. This skips both
4389 over invisible text properties and overlays with
4390 invisible property. */
4391 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4393 /* If we skipped nothing at all we weren't at invisible
4394 text in the first place. If everything to the end of
4395 the buffer was skipped, end the loop. */
4396 if (newpos == tem || newpos >= ZV)
4397 invis = 0;
4398 else
4400 /* We skipped some characters but not necessarily
4401 all there are. Check if we ended up on visible
4402 text. Fget_char_property returns the property of
4403 the char before the given position, i.e. if we
4404 get invis = 0, this means that the char at
4405 newpos is visible. */
4406 pos = make_number (newpos);
4407 prop = Fget_char_property (pos, Qinvisible, it->window);
4408 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4411 /* If we ended up on invisible text, proceed to
4412 skip starting with next_stop. */
4413 if (invis != 0)
4414 tem = next_stop;
4416 /* If there are adjacent invisible texts, don't lose the
4417 second one's ellipsis. */
4418 if (invis == 2)
4419 display_ellipsis_p = true;
4421 while (invis != 0);
4423 /* The position newpos is now either ZV or on visible text. */
4424 if (it->bidi_p)
4426 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4427 bool on_newline
4428 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4429 bool after_newline
4430 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4432 /* If the invisible text ends on a newline or on a
4433 character after a newline, we can avoid the costly,
4434 character by character, bidi iteration to NEWPOS, and
4435 instead simply reseat the iterator there. That's
4436 because all bidi reordering information is tossed at
4437 the newline. This is a big win for modes that hide
4438 complete lines, like Outline, Org, etc. */
4439 if (on_newline || after_newline)
4441 struct text_pos tpos;
4442 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4444 SET_TEXT_POS (tpos, newpos, bpos);
4445 reseat_1 (it, tpos, false);
4446 /* If we reseat on a newline/ZV, we need to prep the
4447 bidi iterator for advancing to the next character
4448 after the newline/EOB, keeping the current paragraph
4449 direction (so that PRODUCE_GLYPHS does TRT wrt
4450 prepending/appending glyphs to a glyph row). */
4451 if (on_newline)
4453 it->bidi_it.first_elt = false;
4454 it->bidi_it.paragraph_dir = pdir;
4455 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4456 it->bidi_it.nchars = 1;
4457 it->bidi_it.ch_len = 1;
4460 else /* Must use the slow method. */
4462 /* With bidi iteration, the region of invisible text
4463 could start and/or end in the middle of a
4464 non-base embedding level. Therefore, we need to
4465 skip invisible text using the bidi iterator,
4466 starting at IT's current position, until we find
4467 ourselves outside of the invisible text.
4468 Skipping invisible text _after_ bidi iteration
4469 avoids affecting the visual order of the
4470 displayed text when invisible properties are
4471 added or removed. */
4472 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4474 /* If we were `reseat'ed to a new paragraph,
4475 determine the paragraph base direction. We
4476 need to do it now because
4477 next_element_from_buffer may not have a
4478 chance to do it, if we are going to skip any
4479 text at the beginning, which resets the
4480 FIRST_ELT flag. */
4481 bidi_paragraph_init (it->paragraph_embedding,
4482 &it->bidi_it, true);
4486 bidi_move_to_visually_next (&it->bidi_it);
4488 while (it->stop_charpos <= it->bidi_it.charpos
4489 && it->bidi_it.charpos < newpos);
4490 IT_CHARPOS (*it) = it->bidi_it.charpos;
4491 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4492 /* If we overstepped NEWPOS, record its position in
4493 the iterator, so that we skip invisible text if
4494 later the bidi iteration lands us in the
4495 invisible region again. */
4496 if (IT_CHARPOS (*it) >= newpos)
4497 it->prev_stop = newpos;
4500 else
4502 IT_CHARPOS (*it) = newpos;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4506 if (display_ellipsis_p)
4508 /* Make sure that the glyphs of the ellipsis will get
4509 correct `charpos' values. If we would not update
4510 it->position here, the glyphs would belong to the
4511 last visible character _before_ the invisible
4512 text, which confuses `set_cursor_from_row'.
4514 We use the last invisible position instead of the
4515 first because this way the cursor is always drawn on
4516 the first "." of the ellipsis, whenever PT is inside
4517 the invisible text. Otherwise the cursor would be
4518 placed _after_ the ellipsis when the point is after the
4519 first invisible character. */
4520 if (!STRINGP (it->object))
4522 it->position.charpos = newpos - 1;
4523 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4527 /* If there are before-strings at the start of invisible
4528 text, and the text is invisible because of a text
4529 property, arrange to show before-strings because 20.x did
4530 it that way. (If the text is invisible because of an
4531 overlay property instead of a text property, this is
4532 already handled in the overlay code.) */
4533 if (NILP (overlay)
4534 && get_overlay_strings (it, it->stop_charpos))
4536 handled = HANDLED_RECOMPUTE_PROPS;
4537 if (it->sp > 0)
4539 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4540 /* The call to get_overlay_strings above recomputes
4541 it->stop_charpos, but it only considers changes
4542 in properties and overlays beyond iterator's
4543 current position. This causes us to miss changes
4544 that happen exactly where the invisible property
4545 ended. So we play it safe here and force the
4546 iterator to check for potential stop positions
4547 immediately after the invisible text. Note that
4548 if get_overlay_strings returns true, it
4549 normally also pushed the iterator stack, so we
4550 need to update the stop position in the slot
4551 below the current one. */
4552 it->stack[it->sp - 1].stop_charpos
4553 = CHARPOS (it->stack[it->sp - 1].current.pos);
4556 else if (display_ellipsis_p)
4558 it->ellipsis_p = true;
4559 /* Let the ellipsis display before
4560 considering any properties of the following char.
4561 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4562 handled = HANDLED_RETURN;
4567 return handled;
4571 /* Make iterator IT return `...' next.
4572 Replaces LEN characters from buffer. */
4574 static void
4575 setup_for_ellipsis (struct it *it, int len)
4577 /* Use the display table definition for `...'. Invalid glyphs
4578 will be handled by the method returning elements from dpvec. */
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4581 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4582 it->dpvec = v->contents;
4583 it->dpend = v->contents + v->header.size;
4585 else
4587 /* Default `...'. */
4588 it->dpvec = default_invis_vector;
4589 it->dpend = default_invis_vector + 3;
4592 it->dpvec_char_len = len;
4593 it->current.dpvec_index = 0;
4594 it->dpvec_face_id = -1;
4596 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4597 face as the preceding text. IT->saved_face_id was set in
4598 handle_stop to the face of the preceding character, and will be
4599 different from IT->face_id only if the invisible text skipped in
4600 handle_invisible_prop has some non-default face on its first
4601 character. We thus ignore the face of the invisible text when we
4602 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4603 if (it->saved_face_id >= 0)
4604 it->face_id = it->saved_face_id;
4606 /* If the ellipsis represents buffer text, it means we advanced in
4607 the buffer, so we should no longer ignore overlay strings. */
4608 if (it->method == GET_FROM_BUFFER)
4609 it->ignore_overlay_strings_at_pos_p = false;
4611 it->method = GET_FROM_DISPLAY_VECTOR;
4612 it->ellipsis_p = true;
4617 /***********************************************************************
4618 'display' property
4619 ***********************************************************************/
4621 /* Set up iterator IT from `display' property at its current position.
4622 Called from handle_stop.
4623 We return HANDLED_RETURN if some part of the display property
4624 overrides the display of the buffer text itself.
4625 Otherwise we return HANDLED_NORMALLY. */
4627 static enum prop_handled
4628 handle_display_prop (struct it *it)
4630 Lisp_Object propval, object, overlay;
4631 struct text_pos *position;
4632 ptrdiff_t bufpos;
4633 /* Nonzero if some property replaces the display of the text itself. */
4634 int display_replaced = 0;
4636 if (STRINGP (it->string))
4638 object = it->string;
4639 position = &it->current.string_pos;
4640 bufpos = CHARPOS (it->current.pos);
4642 else
4644 XSETWINDOW (object, it->w);
4645 position = &it->current.pos;
4646 bufpos = CHARPOS (*position);
4649 /* Reset those iterator values set from display property values. */
4650 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4651 it->space_width = Qnil;
4652 it->font_height = Qnil;
4653 it->voffset = 0;
4655 /* We don't support recursive `display' properties, i.e. string
4656 values that have a string `display' property, that have a string
4657 `display' property etc. */
4658 if (!it->string_from_display_prop_p)
4659 it->area = TEXT_AREA;
4661 propval = get_char_property_and_overlay (make_number (position->charpos),
4662 Qdisplay, object, &overlay);
4663 if (NILP (propval))
4664 return HANDLED_NORMALLY;
4665 /* Now OVERLAY is the overlay that gave us this property, or nil
4666 if it was a text property. */
4668 if (!STRINGP (it->string))
4669 object = it->w->contents;
4671 display_replaced = handle_display_spec (it, propval, object, overlay,
4672 position, bufpos,
4673 FRAME_WINDOW_P (it->f));
4674 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4677 /* Subroutine of handle_display_prop. Returns non-zero if the display
4678 specification in SPEC is a replacing specification, i.e. it would
4679 replace the text covered by `display' property with something else,
4680 such as an image or a display string. If SPEC includes any kind or
4681 `(space ...) specification, the value is 2; this is used by
4682 compute_display_string_pos, which see.
4684 See handle_single_display_spec for documentation of arguments.
4685 FRAME_WINDOW_P is true if the window being redisplayed is on a
4686 GUI frame; this argument is used only if IT is NULL, see below.
4688 IT can be NULL, if this is called by the bidi reordering code
4689 through compute_display_string_pos, which see. In that case, this
4690 function only examines SPEC, but does not otherwise "handle" it, in
4691 the sense that it doesn't set up members of IT from the display
4692 spec. */
4693 static int
4694 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4695 Lisp_Object overlay, struct text_pos *position,
4696 ptrdiff_t bufpos, bool frame_window_p)
4698 int replacing = 0;
4700 if (CONSP (spec)
4701 /* Simple specifications. */
4702 && !EQ (XCAR (spec), Qimage)
4703 #ifdef HAVE_XWIDGETS
4704 && !EQ (XCAR (spec), Qxwidget)
4705 #endif
4706 && !EQ (XCAR (spec), Qspace)
4707 && !EQ (XCAR (spec), Qwhen)
4708 && !EQ (XCAR (spec), Qslice)
4709 && !EQ (XCAR (spec), Qspace_width)
4710 && !EQ (XCAR (spec), Qheight)
4711 && !EQ (XCAR (spec), Qraise)
4712 /* Marginal area specifications. */
4713 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4714 && !EQ (XCAR (spec), Qleft_fringe)
4715 && !EQ (XCAR (spec), Qright_fringe)
4716 && !NILP (XCAR (spec)))
4718 for (; CONSP (spec); spec = XCDR (spec))
4720 int rv = handle_single_display_spec (it, XCAR (spec), object,
4721 overlay, position, bufpos,
4722 replacing, frame_window_p);
4723 if (rv != 0)
4725 replacing = rv;
4726 /* If some text in a string is replaced, `position' no
4727 longer points to the position of `object'. */
4728 if (!it || STRINGP (object))
4729 break;
4733 else if (VECTORP (spec))
4735 ptrdiff_t i;
4736 for (i = 0; i < ASIZE (spec); ++i)
4738 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4739 overlay, position, bufpos,
4740 replacing, frame_window_p);
4741 if (rv != 0)
4743 replacing = rv;
4744 /* If some text in a string is replaced, `position' no
4745 longer points to the position of `object'. */
4746 if (!it || STRINGP (object))
4747 break;
4751 else
4752 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4753 bufpos, 0, frame_window_p);
4754 return replacing;
4757 /* Value is the position of the end of the `display' property starting
4758 at START_POS in OBJECT. */
4760 static struct text_pos
4761 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4763 Lisp_Object end;
4764 struct text_pos end_pos;
4766 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4767 Qdisplay, object, Qnil);
4768 CHARPOS (end_pos) = XFASTINT (end);
4769 if (STRINGP (object))
4770 compute_string_pos (&end_pos, start_pos, it->string);
4771 else
4772 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4774 return end_pos;
4778 /* Set up IT from a single `display' property specification SPEC. OBJECT
4779 is the object in which the `display' property was found. *POSITION
4780 is the position in OBJECT at which the `display' property was found.
4781 BUFPOS is the buffer position of OBJECT (different from POSITION if
4782 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4783 previously saw a display specification which already replaced text
4784 display with something else, for example an image; we ignore such
4785 properties after the first one has been processed.
4787 OVERLAY is the overlay this `display' property came from,
4788 or nil if it was a text property.
4790 If SPEC is a `space' or `image' specification, and in some other
4791 cases too, set *POSITION to the position where the `display'
4792 property ends.
4794 If IT is NULL, only examine the property specification in SPEC, but
4795 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4796 is intended to be displayed in a window on a GUI frame.
4798 Value is non-zero if something was found which replaces the display
4799 of buffer or string text. */
4801 static int
4802 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4803 Lisp_Object overlay, struct text_pos *position,
4804 ptrdiff_t bufpos, int display_replaced,
4805 bool frame_window_p)
4807 Lisp_Object form;
4808 Lisp_Object location, value;
4809 struct text_pos start_pos = *position;
4811 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4812 If the result is non-nil, use VALUE instead of SPEC. */
4813 form = Qt;
4814 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4816 spec = XCDR (spec);
4817 if (!CONSP (spec))
4818 return 0;
4819 form = XCAR (spec);
4820 spec = XCDR (spec);
4823 if (!NILP (form) && !EQ (form, Qt))
4825 ptrdiff_t count = SPECPDL_INDEX ();
4827 /* Bind `object' to the object having the `display' property, a
4828 buffer or string. Bind `position' to the position in the
4829 object where the property was found, and `buffer-position'
4830 to the current position in the buffer. */
4832 if (NILP (object))
4833 XSETBUFFER (object, current_buffer);
4834 specbind (Qobject, object);
4835 specbind (Qposition, make_number (CHARPOS (*position)));
4836 specbind (Qbuffer_position, make_number (bufpos));
4837 form = safe_eval (form);
4838 unbind_to (count, Qnil);
4841 if (NILP (form))
4842 return 0;
4844 /* Handle `(height HEIGHT)' specifications. */
4845 if (CONSP (spec)
4846 && EQ (XCAR (spec), Qheight)
4847 && CONSP (XCDR (spec)))
4849 if (it)
4851 if (!FRAME_WINDOW_P (it->f))
4852 return 0;
4854 it->font_height = XCAR (XCDR (spec));
4855 if (!NILP (it->font_height))
4857 int new_height = -1;
4859 if (CONSP (it->font_height)
4860 && (EQ (XCAR (it->font_height), Qplus)
4861 || EQ (XCAR (it->font_height), Qminus))
4862 && CONSP (XCDR (it->font_height))
4863 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4865 /* `(+ N)' or `(- N)' where N is an integer. */
4866 int steps = XINT (XCAR (XCDR (it->font_height)));
4867 if (EQ (XCAR (it->font_height), Qplus))
4868 steps = - steps;
4869 it->face_id = smaller_face (it->f, it->face_id, steps);
4871 else if (FUNCTIONP (it->font_height))
4873 /* Call function with current height as argument.
4874 Value is the new height. */
4875 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4876 Lisp_Object height;
4877 height = safe_call1 (it->font_height,
4878 face->lface[LFACE_HEIGHT_INDEX]);
4879 if (NUMBERP (height))
4880 new_height = XFLOATINT (height);
4882 else if (NUMBERP (it->font_height))
4884 /* Value is a multiple of the canonical char height. */
4885 struct face *f;
4887 f = FACE_FROM_ID (it->f,
4888 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4889 new_height = (XFLOATINT (it->font_height)
4890 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4892 else
4894 /* Evaluate IT->font_height with `height' bound to the
4895 current specified height to get the new height. */
4896 ptrdiff_t count = SPECPDL_INDEX ();
4897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4899 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4900 value = safe_eval (it->font_height);
4901 unbind_to (count, Qnil);
4903 if (NUMBERP (value))
4904 new_height = XFLOATINT (value);
4907 if (new_height > 0)
4908 it->face_id = face_with_height (it->f, it->face_id, new_height);
4912 return 0;
4915 /* Handle `(space-width WIDTH)'. */
4916 if (CONSP (spec)
4917 && EQ (XCAR (spec), Qspace_width)
4918 && CONSP (XCDR (spec)))
4920 if (it)
4922 if (!FRAME_WINDOW_P (it->f))
4923 return 0;
4925 value = XCAR (XCDR (spec));
4926 if (NUMBERP (value) && XFLOATINT (value) > 0)
4927 it->space_width = value;
4930 return 0;
4933 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4934 if (CONSP (spec)
4935 && EQ (XCAR (spec), Qslice))
4937 Lisp_Object tem;
4939 if (it)
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4944 if (tem = XCDR (spec), CONSP (tem))
4946 it->slice.x = XCAR (tem);
4947 if (tem = XCDR (tem), CONSP (tem))
4949 it->slice.y = XCAR (tem);
4950 if (tem = XCDR (tem), CONSP (tem))
4952 it->slice.width = XCAR (tem);
4953 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.height = XCAR (tem);
4960 return 0;
4963 /* Handle `(raise FACTOR)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qraise)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 #ifdef HAVE_WINDOW_SYSTEM
4974 value = XCAR (XCDR (spec));
4975 if (NUMBERP (value))
4977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4978 it->voffset = - (XFLOATINT (value)
4979 * (normal_char_height (face->font, -1)));
4981 #endif /* HAVE_WINDOW_SYSTEM */
4984 return 0;
4987 /* Don't handle the other kinds of display specifications
4988 inside a string that we got from a `display' property. */
4989 if (it && it->string_from_display_prop_p)
4990 return 0;
4992 /* Characters having this form of property are not displayed, so
4993 we have to find the end of the property. */
4994 if (it)
4996 start_pos = *position;
4997 *position = display_prop_end (it, object, start_pos);
4998 /* If the display property comes from an overlay, don't consider
4999 any potential stop_charpos values before the end of that
5000 overlay. Since display_prop_end will happily find another
5001 'display' property coming from some other overlay or text
5002 property on buffer positions before this overlay's end, we
5003 need to ignore them, or else we risk displaying this
5004 overlay's display string/image twice. */
5005 if (!NILP (overlay))
5007 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5009 if (ovendpos > CHARPOS (*position))
5010 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5013 value = Qnil;
5015 /* Stop the scan at that end position--we assume that all
5016 text properties change there. */
5017 if (it)
5018 it->stop_charpos = position->charpos;
5020 /* Handle `(left-fringe BITMAP [FACE])'
5021 and `(right-fringe BITMAP [FACE])'. */
5022 if (CONSP (spec)
5023 && (EQ (XCAR (spec), Qleft_fringe)
5024 || EQ (XCAR (spec), Qright_fringe))
5025 && CONSP (XCDR (spec)))
5027 if (it)
5029 if (!FRAME_WINDOW_P (it->f))
5030 /* If we return here, POSITION has been advanced
5031 across the text with this property. */
5033 /* Synchronize the bidi iterator with POSITION. This is
5034 needed because we are not going to push the iterator
5035 on behalf of this display property, so there will be
5036 no pop_it call to do this synchronization for us. */
5037 if (it->bidi_p)
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5043 return 1;
5046 else if (!frame_window_p)
5047 return 1;
5049 #ifdef HAVE_WINDOW_SYSTEM
5050 value = XCAR (XCDR (spec));
5051 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5052 if (! fringe_bitmap)
5053 /* If we return here, POSITION has been advanced
5054 across the text with this property. */
5056 if (it && it->bidi_p)
5058 it->position = *position;
5059 iterate_out_of_display_property (it);
5060 *position = it->position;
5062 return 1;
5065 if (it)
5067 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5069 if (CONSP (XCDR (XCDR (spec))))
5071 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5072 int face_id2 = lookup_derived_face (it->f, face_name,
5073 FRINGE_FACE_ID, false);
5074 if (face_id2 >= 0)
5075 face_id = face_id2;
5078 /* Save current settings of IT so that we can restore them
5079 when we are finished with the glyph property value. */
5080 push_it (it, position);
5082 it->area = TEXT_AREA;
5083 it->what = IT_IMAGE;
5084 it->image_id = -1; /* no image */
5085 it->position = start_pos;
5086 it->object = NILP (object) ? it->w->contents : object;
5087 it->method = GET_FROM_IMAGE;
5088 it->from_overlay = Qnil;
5089 it->face_id = face_id;
5090 it->from_disp_prop_p = true;
5092 /* Say that we haven't consumed the characters with
5093 `display' property yet. The call to pop_it in
5094 set_iterator_to_next will clean this up. */
5095 *position = start_pos;
5097 if (EQ (XCAR (spec), Qleft_fringe))
5099 it->left_user_fringe_bitmap = fringe_bitmap;
5100 it->left_user_fringe_face_id = face_id;
5102 else
5104 it->right_user_fringe_bitmap = fringe_bitmap;
5105 it->right_user_fringe_face_id = face_id;
5108 #endif /* HAVE_WINDOW_SYSTEM */
5109 return 1;
5112 /* Prepare to handle `((margin left-margin) ...)',
5113 `((margin right-margin) ...)' and `((margin nil) ...)'
5114 prefixes for display specifications. */
5115 location = Qunbound;
5116 if (CONSP (spec) && CONSP (XCAR (spec)))
5118 Lisp_Object tem;
5120 value = XCDR (spec);
5121 if (CONSP (value))
5122 value = XCAR (value);
5124 tem = XCAR (spec);
5125 if (EQ (XCAR (tem), Qmargin)
5126 && (tem = XCDR (tem),
5127 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5128 (NILP (tem)
5129 || EQ (tem, Qleft_margin)
5130 || EQ (tem, Qright_margin))))
5131 location = tem;
5134 if (EQ (location, Qunbound))
5136 location = Qnil;
5137 value = spec;
5140 /* After this point, VALUE is the property after any
5141 margin prefix has been stripped. It must be a string,
5142 an image specification, or `(space ...)'.
5144 LOCATION specifies where to display: `left-margin',
5145 `right-margin' or nil. */
5147 bool valid_p = (STRINGP (value)
5148 #ifdef HAVE_WINDOW_SYSTEM
5149 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5150 && valid_image_p (value))
5151 #endif /* not HAVE_WINDOW_SYSTEM */
5152 || (CONSP (value) && EQ (XCAR (value), Qspace))
5153 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5154 && valid_xwidget_spec_p (value)));
5156 if (valid_p && display_replaced == 0)
5158 int retval = 1;
5160 if (!it)
5162 /* Callers need to know whether the display spec is any kind
5163 of `(space ...)' spec that is about to affect text-area
5164 display. */
5165 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5166 retval = 2;
5167 return retval;
5170 /* Save current settings of IT so that we can restore them
5171 when we are finished with the glyph property value. */
5172 push_it (it, position);
5173 it->from_overlay = overlay;
5174 it->from_disp_prop_p = true;
5176 if (NILP (location))
5177 it->area = TEXT_AREA;
5178 else if (EQ (location, Qleft_margin))
5179 it->area = LEFT_MARGIN_AREA;
5180 else
5181 it->area = RIGHT_MARGIN_AREA;
5183 if (STRINGP (value))
5185 it->string = value;
5186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5187 it->current.overlay_string_index = -1;
5188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5189 it->end_charpos = it->string_nchars = SCHARS (it->string);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 it->prev_stop = 0;
5193 it->base_level_stop = 0;
5194 it->string_from_display_prop_p = true;
5195 /* Say that we haven't consumed the characters with
5196 `display' property yet. The call to pop_it in
5197 set_iterator_to_next will clean this up. */
5198 if (BUFFERP (object))
5199 *position = start_pos;
5201 /* Force paragraph direction to be that of the parent
5202 object. If the parent object's paragraph direction is
5203 not yet determined, default to L2R. */
5204 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5205 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5206 else
5207 it->paragraph_embedding = L2R;
5209 /* Set up the bidi iterator for this display string. */
5210 if (it->bidi_p)
5212 it->bidi_it.string.lstring = it->string;
5213 it->bidi_it.string.s = NULL;
5214 it->bidi_it.string.schars = it->end_charpos;
5215 it->bidi_it.string.bufpos = bufpos;
5216 it->bidi_it.string.from_disp_str = true;
5217 it->bidi_it.string.unibyte = !it->multibyte_p;
5218 it->bidi_it.w = it->w;
5219 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5222 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5224 it->method = GET_FROM_STRETCH;
5225 it->object = value;
5226 *position = it->position = start_pos;
5227 retval = 1 + (it->area == TEXT_AREA);
5229 else if (valid_xwidget_spec_p (value))
5231 it->what = IT_XWIDGET;
5232 it->method = GET_FROM_XWIDGET;
5233 it->position = start_pos;
5234 it->object = NILP (object) ? it->w->contents : object;
5235 *position = start_pos;
5236 it->xwidget = lookup_xwidget (value);
5238 #ifdef HAVE_WINDOW_SYSTEM
5239 else
5241 it->what = IT_IMAGE;
5242 it->image_id = lookup_image (it->f, value);
5243 it->position = start_pos;
5244 it->object = NILP (object) ? it->w->contents : object;
5245 it->method = GET_FROM_IMAGE;
5247 /* Say that we haven't consumed the characters with
5248 `display' property yet. The call to pop_it in
5249 set_iterator_to_next will clean this up. */
5250 *position = start_pos;
5252 #endif /* HAVE_WINDOW_SYSTEM */
5254 return retval;
5257 /* Invalid property or property not supported. Restore
5258 POSITION to what it was before. */
5259 *position = start_pos;
5260 return 0;
5263 /* Check if PROP is a display property value whose text should be
5264 treated as intangible. OVERLAY is the overlay from which PROP
5265 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5266 specify the buffer position covered by PROP. */
5268 bool
5269 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5270 ptrdiff_t charpos, ptrdiff_t bytepos)
5272 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5273 struct text_pos position;
5275 SET_TEXT_POS (position, charpos, bytepos);
5276 return (handle_display_spec (NULL, prop, Qnil, overlay,
5277 &position, charpos, frame_window_p)
5278 != 0);
5282 /* Return true if PROP is a display sub-property value containing STRING.
5284 Implementation note: this and the following function are really
5285 special cases of handle_display_spec and
5286 handle_single_display_spec, and should ideally use the same code.
5287 Until they do, these two pairs must be consistent and must be
5288 modified in sync. */
5290 static bool
5291 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5293 if (EQ (string, prop))
5294 return true;
5296 /* Skip over `when FORM'. */
5297 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5299 prop = XCDR (prop);
5300 if (!CONSP (prop))
5301 return false;
5302 /* Actually, the condition following `when' should be eval'ed,
5303 like handle_single_display_spec does, and we should return
5304 false if it evaluates to nil. However, this function is
5305 called only when the buffer was already displayed and some
5306 glyph in the glyph matrix was found to come from a display
5307 string. Therefore, the condition was already evaluated, and
5308 the result was non-nil, otherwise the display string wouldn't
5309 have been displayed and we would have never been called for
5310 this property. Thus, we can skip the evaluation and assume
5311 its result is non-nil. */
5312 prop = XCDR (prop);
5315 if (CONSP (prop))
5316 /* Skip over `margin LOCATION'. */
5317 if (EQ (XCAR (prop), Qmargin))
5319 prop = XCDR (prop);
5320 if (!CONSP (prop))
5321 return false;
5323 prop = XCDR (prop);
5324 if (!CONSP (prop))
5325 return false;
5328 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5332 /* Return true if STRING appears in the `display' property PROP. */
5334 static bool
5335 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5337 if (CONSP (prop)
5338 && !EQ (XCAR (prop), Qwhen)
5339 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5341 /* A list of sub-properties. */
5342 while (CONSP (prop))
5344 if (single_display_spec_string_p (XCAR (prop), string))
5345 return true;
5346 prop = XCDR (prop);
5349 else if (VECTORP (prop))
5351 /* A vector of sub-properties. */
5352 ptrdiff_t i;
5353 for (i = 0; i < ASIZE (prop); ++i)
5354 if (single_display_spec_string_p (AREF (prop, i), string))
5355 return true;
5357 else
5358 return single_display_spec_string_p (prop, string);
5360 return false;
5363 /* Look for STRING in overlays and text properties in the current
5364 buffer, between character positions FROM and TO (excluding TO).
5365 BACK_P means look back (in this case, TO is supposed to be
5366 less than FROM).
5367 Value is the first character position where STRING was found, or
5368 zero if it wasn't found before hitting TO.
5370 This function may only use code that doesn't eval because it is
5371 called asynchronously from note_mouse_highlight. */
5373 static ptrdiff_t
5374 string_buffer_position_lim (Lisp_Object string,
5375 ptrdiff_t from, ptrdiff_t to, bool back_p)
5377 Lisp_Object limit, prop, pos;
5378 bool found = false;
5380 pos = make_number (max (from, BEGV));
5382 if (!back_p) /* looking forward */
5384 limit = make_number (min (to, ZV));
5385 while (!found && !EQ (pos, limit))
5387 prop = Fget_char_property (pos, Qdisplay, Qnil);
5388 if (!NILP (prop) && display_prop_string_p (prop, string))
5389 found = true;
5390 else
5391 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5392 limit);
5395 else /* looking back */
5397 limit = make_number (max (to, BEGV));
5398 while (!found && !EQ (pos, limit))
5400 prop = Fget_char_property (pos, Qdisplay, Qnil);
5401 if (!NILP (prop) && display_prop_string_p (prop, string))
5402 found = true;
5403 else
5404 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5405 limit);
5409 return found ? XINT (pos) : 0;
5412 /* Determine which buffer position in current buffer STRING comes from.
5413 AROUND_CHARPOS is an approximate position where it could come from.
5414 Value is the buffer position or 0 if it couldn't be determined.
5416 This function is necessary because we don't record buffer positions
5417 in glyphs generated from strings (to keep struct glyph small).
5418 This function may only use code that doesn't eval because it is
5419 called asynchronously from note_mouse_highlight. */
5421 static ptrdiff_t
5422 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5424 const int MAX_DISTANCE = 1000;
5425 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5426 around_charpos + MAX_DISTANCE,
5427 false);
5429 if (!found)
5430 found = string_buffer_position_lim (string, around_charpos,
5431 around_charpos - MAX_DISTANCE, true);
5432 return found;
5437 /***********************************************************************
5438 `composition' property
5439 ***********************************************************************/
5441 /* Set up iterator IT from `composition' property at its current
5442 position. Called from handle_stop. */
5444 static enum prop_handled
5445 handle_composition_prop (struct it *it)
5447 Lisp_Object prop, string;
5448 ptrdiff_t pos, pos_byte, start, end;
5450 if (STRINGP (it->string))
5452 unsigned char *s;
5454 pos = IT_STRING_CHARPOS (*it);
5455 pos_byte = IT_STRING_BYTEPOS (*it);
5456 string = it->string;
5457 s = SDATA (string) + pos_byte;
5458 it->c = STRING_CHAR (s);
5460 else
5462 pos = IT_CHARPOS (*it);
5463 pos_byte = IT_BYTEPOS (*it);
5464 string = Qnil;
5465 it->c = FETCH_CHAR (pos_byte);
5468 /* If there's a valid composition and point is not inside of the
5469 composition (in the case that the composition is from the current
5470 buffer), draw a glyph composed from the composition components. */
5471 if (find_composition (pos, -1, &start, &end, &prop, string)
5472 && composition_valid_p (start, end, prop)
5473 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5475 if (start < pos)
5476 /* As we can't handle this situation (perhaps font-lock added
5477 a new composition), we just return here hoping that next
5478 redisplay will detect this composition much earlier. */
5479 return HANDLED_NORMALLY;
5480 if (start != pos)
5482 if (STRINGP (it->string))
5483 pos_byte = string_char_to_byte (it->string, start);
5484 else
5485 pos_byte = CHAR_TO_BYTE (start);
5487 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5488 prop, string);
5490 if (it->cmp_it.id >= 0)
5492 it->cmp_it.ch = -1;
5493 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5494 it->cmp_it.nglyphs = -1;
5498 return HANDLED_NORMALLY;
5503 /***********************************************************************
5504 Overlay strings
5505 ***********************************************************************/
5507 /* The following structure is used to record overlay strings for
5508 later sorting in load_overlay_strings. */
5510 struct overlay_entry
5512 Lisp_Object overlay;
5513 Lisp_Object string;
5514 EMACS_INT priority;
5515 bool after_string_p;
5519 /* Set up iterator IT from overlay strings at its current position.
5520 Called from handle_stop. */
5522 static enum prop_handled
5523 handle_overlay_change (struct it *it)
5525 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5526 return HANDLED_RECOMPUTE_PROPS;
5527 else
5528 return HANDLED_NORMALLY;
5532 /* Set up the next overlay string for delivery by IT, if there is an
5533 overlay string to deliver. Called by set_iterator_to_next when the
5534 end of the current overlay string is reached. If there are more
5535 overlay strings to display, IT->string and
5536 IT->current.overlay_string_index are set appropriately here.
5537 Otherwise IT->string is set to nil. */
5539 static void
5540 next_overlay_string (struct it *it)
5542 ++it->current.overlay_string_index;
5543 if (it->current.overlay_string_index == it->n_overlay_strings)
5545 /* No more overlay strings. Restore IT's settings to what
5546 they were before overlay strings were processed, and
5547 continue to deliver from current_buffer. */
5549 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5550 pop_it (it);
5551 eassert (it->sp > 0
5552 || (NILP (it->string)
5553 && it->method == GET_FROM_BUFFER
5554 && it->stop_charpos >= BEGV
5555 && it->stop_charpos <= it->end_charpos));
5556 it->current.overlay_string_index = -1;
5557 it->n_overlay_strings = 0;
5558 /* If there's an empty display string on the stack, pop the
5559 stack, to resync the bidi iterator with IT's position. Such
5560 empty strings are pushed onto the stack in
5561 get_overlay_strings_1. */
5562 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5563 pop_it (it);
5565 /* Since we've exhausted overlay strings at this buffer
5566 position, set the flag to ignore overlays until we move to
5567 another position. The flag is reset in
5568 next_element_from_buffer. */
5569 it->ignore_overlay_strings_at_pos_p = true;
5571 /* If we're at the end of the buffer, record that we have
5572 processed the overlay strings there already, so that
5573 next_element_from_buffer doesn't try it again. */
5574 if (NILP (it->string)
5575 && IT_CHARPOS (*it) >= it->end_charpos
5576 && it->overlay_strings_charpos >= it->end_charpos)
5577 it->overlay_strings_at_end_processed_p = true;
5578 /* Note: we reset overlay_strings_charpos only here, to make
5579 sure the just-processed overlays were indeed at EOB.
5580 Otherwise, overlays on text with invisible text property,
5581 which are processed with IT's position past the invisible
5582 text, might fool us into thinking the overlays at EOB were
5583 already processed (linum-mode can cause this, for
5584 example). */
5585 it->overlay_strings_charpos = -1;
5587 else
5589 /* There are more overlay strings to process. If
5590 IT->current.overlay_string_index has advanced to a position
5591 where we must load IT->overlay_strings with more strings, do
5592 it. We must load at the IT->overlay_strings_charpos where
5593 IT->n_overlay_strings was originally computed; when invisible
5594 text is present, this might not be IT_CHARPOS (Bug#7016). */
5595 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5597 if (it->current.overlay_string_index && i == 0)
5598 load_overlay_strings (it, it->overlay_strings_charpos);
5600 /* Initialize IT to deliver display elements from the overlay
5601 string. */
5602 it->string = it->overlay_strings[i];
5603 it->multibyte_p = STRING_MULTIBYTE (it->string);
5604 SET_TEXT_POS (it->current.string_pos, 0, 0);
5605 it->method = GET_FROM_STRING;
5606 it->stop_charpos = 0;
5607 it->end_charpos = SCHARS (it->string);
5608 if (it->cmp_it.stop_pos >= 0)
5609 it->cmp_it.stop_pos = 0;
5610 it->prev_stop = 0;
5611 it->base_level_stop = 0;
5613 /* Set up the bidi iterator for this overlay string. */
5614 if (it->bidi_p)
5616 it->bidi_it.string.lstring = it->string;
5617 it->bidi_it.string.s = NULL;
5618 it->bidi_it.string.schars = SCHARS (it->string);
5619 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5620 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5621 it->bidi_it.string.unibyte = !it->multibyte_p;
5622 it->bidi_it.w = it->w;
5623 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5627 CHECK_IT (it);
5631 /* Compare two overlay_entry structures E1 and E2. Used as a
5632 comparison function for qsort in load_overlay_strings. Overlay
5633 strings for the same position are sorted so that
5635 1. All after-strings come in front of before-strings, except
5636 when they come from the same overlay.
5638 2. Within after-strings, strings are sorted so that overlay strings
5639 from overlays with higher priorities come first.
5641 2. Within before-strings, strings are sorted so that overlay
5642 strings from overlays with higher priorities come last.
5644 Value is analogous to strcmp. */
5647 static int
5648 compare_overlay_entries (const void *e1, const void *e2)
5650 struct overlay_entry const *entry1 = e1;
5651 struct overlay_entry const *entry2 = e2;
5652 int result;
5654 if (entry1->after_string_p != entry2->after_string_p)
5656 /* Let after-strings appear in front of before-strings if
5657 they come from different overlays. */
5658 if (EQ (entry1->overlay, entry2->overlay))
5659 result = entry1->after_string_p ? 1 : -1;
5660 else
5661 result = entry1->after_string_p ? -1 : 1;
5663 else if (entry1->priority != entry2->priority)
5665 if (entry1->after_string_p)
5666 /* After-strings sorted in order of decreasing priority. */
5667 result = entry2->priority < entry1->priority ? -1 : 1;
5668 else
5669 /* Before-strings sorted in order of increasing priority. */
5670 result = entry1->priority < entry2->priority ? -1 : 1;
5672 else
5673 result = 0;
5675 return result;
5679 /* Load the vector IT->overlay_strings with overlay strings from IT's
5680 current buffer position, or from CHARPOS if that is > 0. Set
5681 IT->n_overlays to the total number of overlay strings found.
5683 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5684 a time. On entry into load_overlay_strings,
5685 IT->current.overlay_string_index gives the number of overlay
5686 strings that have already been loaded by previous calls to this
5687 function.
5689 IT->add_overlay_start contains an additional overlay start
5690 position to consider for taking overlay strings from, if non-zero.
5691 This position comes into play when the overlay has an `invisible'
5692 property, and both before and after-strings. When we've skipped to
5693 the end of the overlay, because of its `invisible' property, we
5694 nevertheless want its before-string to appear.
5695 IT->add_overlay_start will contain the overlay start position
5696 in this case.
5698 Overlay strings are sorted so that after-string strings come in
5699 front of before-string strings. Within before and after-strings,
5700 strings are sorted by overlay priority. See also function
5701 compare_overlay_entries. */
5703 static void
5704 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5706 Lisp_Object overlay, window, str, invisible;
5707 struct Lisp_Overlay *ov;
5708 ptrdiff_t start, end;
5709 ptrdiff_t n = 0, i, j;
5710 int invis;
5711 struct overlay_entry entriesbuf[20];
5712 ptrdiff_t size = ARRAYELTS (entriesbuf);
5713 struct overlay_entry *entries = entriesbuf;
5714 USE_SAFE_ALLOCA;
5716 if (charpos <= 0)
5717 charpos = IT_CHARPOS (*it);
5719 /* Append the overlay string STRING of overlay OVERLAY to vector
5720 `entries' which has size `size' and currently contains `n'
5721 elements. AFTER_P means STRING is an after-string of
5722 OVERLAY. */
5723 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5724 do \
5726 Lisp_Object priority; \
5728 if (n == size) \
5730 struct overlay_entry *old = entries; \
5731 SAFE_NALLOCA (entries, 2, size); \
5732 memcpy (entries, old, size * sizeof *entries); \
5733 size *= 2; \
5736 entries[n].string = (STRING); \
5737 entries[n].overlay = (OVERLAY); \
5738 priority = Foverlay_get ((OVERLAY), Qpriority); \
5739 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5740 entries[n].after_string_p = (AFTER_P); \
5741 ++n; \
5743 while (false)
5745 /* Process overlay before the overlay center. */
5746 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5748 XSETMISC (overlay, ov);
5749 eassert (OVERLAYP (overlay));
5750 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5751 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5753 if (end < charpos)
5754 break;
5756 /* Skip this overlay if it doesn't start or end at IT's current
5757 position. */
5758 if (end != charpos && start != charpos)
5759 continue;
5761 /* Skip this overlay if it doesn't apply to IT->w. */
5762 window = Foverlay_get (overlay, Qwindow);
5763 if (WINDOWP (window) && XWINDOW (window) != it->w)
5764 continue;
5766 /* If the text ``under'' the overlay is invisible, both before-
5767 and after-strings from this overlay are visible; start and
5768 end position are indistinguishable. */
5769 invisible = Foverlay_get (overlay, Qinvisible);
5770 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772 /* If overlay has a non-empty before-string, record it. */
5773 if ((start == charpos || (end == charpos && invis != 0))
5774 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5775 && SCHARS (str))
5776 RECORD_OVERLAY_STRING (overlay, str, false);
5778 /* If overlay has a non-empty after-string, record it. */
5779 if ((end == charpos || (start == charpos && invis != 0))
5780 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5781 && SCHARS (str))
5782 RECORD_OVERLAY_STRING (overlay, str, true);
5785 /* Process overlays after the overlay center. */
5786 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5788 XSETMISC (overlay, ov);
5789 eassert (OVERLAYP (overlay));
5790 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5791 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5793 if (start > charpos)
5794 break;
5796 /* Skip this overlay if it doesn't start or end at IT's current
5797 position. */
5798 if (end != charpos && start != charpos)
5799 continue;
5801 /* Skip this overlay if it doesn't apply to IT->w. */
5802 window = Foverlay_get (overlay, Qwindow);
5803 if (WINDOWP (window) && XWINDOW (window) != it->w)
5804 continue;
5806 /* If the text ``under'' the overlay is invisible, it has a zero
5807 dimension, and both before- and after-strings apply. */
5808 invisible = Foverlay_get (overlay, Qinvisible);
5809 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5811 /* If overlay has a non-empty before-string, record it. */
5812 if ((start == charpos || (end == charpos && invis != 0))
5813 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5814 && SCHARS (str))
5815 RECORD_OVERLAY_STRING (overlay, str, false);
5817 /* If overlay has a non-empty after-string, record it. */
5818 if ((end == charpos || (start == charpos && invis != 0))
5819 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5820 && SCHARS (str))
5821 RECORD_OVERLAY_STRING (overlay, str, true);
5824 #undef RECORD_OVERLAY_STRING
5826 /* Sort entries. */
5827 if (n > 1)
5828 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5830 /* Record number of overlay strings, and where we computed it. */
5831 it->n_overlay_strings = n;
5832 it->overlay_strings_charpos = charpos;
5834 /* IT->current.overlay_string_index is the number of overlay strings
5835 that have already been consumed by IT. Copy some of the
5836 remaining overlay strings to IT->overlay_strings. */
5837 i = 0;
5838 j = it->current.overlay_string_index;
5839 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5841 it->overlay_strings[i] = entries[j].string;
5842 it->string_overlays[i++] = entries[j++].overlay;
5845 CHECK_IT (it);
5846 SAFE_FREE ();
5850 /* Get the first chunk of overlay strings at IT's current buffer
5851 position, or at CHARPOS if that is > 0. Value is true if at
5852 least one overlay string was found. */
5854 static bool
5855 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5857 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5858 process. This fills IT->overlay_strings with strings, and sets
5859 IT->n_overlay_strings to the total number of strings to process.
5860 IT->pos.overlay_string_index has to be set temporarily to zero
5861 because load_overlay_strings needs this; it must be set to -1
5862 when no overlay strings are found because a zero value would
5863 indicate a position in the first overlay string. */
5864 it->current.overlay_string_index = 0;
5865 load_overlay_strings (it, charpos);
5867 /* If we found overlay strings, set up IT to deliver display
5868 elements from the first one. Otherwise set up IT to deliver
5869 from current_buffer. */
5870 if (it->n_overlay_strings)
5872 /* Make sure we know settings in current_buffer, so that we can
5873 restore meaningful values when we're done with the overlay
5874 strings. */
5875 if (compute_stop_p)
5876 compute_stop_pos (it);
5877 eassert (it->face_id >= 0);
5879 /* Save IT's settings. They are restored after all overlay
5880 strings have been processed. */
5881 eassert (!compute_stop_p || it->sp == 0);
5883 /* When called from handle_stop, there might be an empty display
5884 string loaded. In that case, don't bother saving it. But
5885 don't use this optimization with the bidi iterator, since we
5886 need the corresponding pop_it call to resync the bidi
5887 iterator's position with IT's position, after we are done
5888 with the overlay strings. (The corresponding call to pop_it
5889 in case of an empty display string is in
5890 next_overlay_string.) */
5891 if (!(!it->bidi_p
5892 && STRINGP (it->string) && !SCHARS (it->string)))
5893 push_it (it, NULL);
5895 /* Set up IT to deliver display elements from the first overlay
5896 string. */
5897 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5898 it->string = it->overlay_strings[0];
5899 it->from_overlay = Qnil;
5900 it->stop_charpos = 0;
5901 eassert (STRINGP (it->string));
5902 it->end_charpos = SCHARS (it->string);
5903 it->prev_stop = 0;
5904 it->base_level_stop = 0;
5905 it->multibyte_p = STRING_MULTIBYTE (it->string);
5906 it->method = GET_FROM_STRING;
5907 it->from_disp_prop_p = 0;
5909 /* Force paragraph direction to be that of the parent
5910 buffer. */
5911 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5912 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5913 else
5914 it->paragraph_embedding = L2R;
5916 /* Set up the bidi iterator for this overlay string. */
5917 if (it->bidi_p)
5919 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5921 it->bidi_it.string.lstring = it->string;
5922 it->bidi_it.string.s = NULL;
5923 it->bidi_it.string.schars = SCHARS (it->string);
5924 it->bidi_it.string.bufpos = pos;
5925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5926 it->bidi_it.string.unibyte = !it->multibyte_p;
5927 it->bidi_it.w = it->w;
5928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5930 return true;
5933 it->current.overlay_string_index = -1;
5934 return false;
5937 static bool
5938 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5940 it->string = Qnil;
5941 it->method = GET_FROM_BUFFER;
5943 get_overlay_strings_1 (it, charpos, true);
5945 CHECK_IT (it);
5947 /* Value is true if we found at least one overlay string. */
5948 return STRINGP (it->string);
5953 /***********************************************************************
5954 Saving and restoring state
5955 ***********************************************************************/
5957 /* Save current settings of IT on IT->stack. Called, for example,
5958 before setting up IT for an overlay string, to be able to restore
5959 IT's settings to what they were after the overlay string has been
5960 processed. If POSITION is non-NULL, it is the position to save on
5961 the stack instead of IT->position. */
5963 static void
5964 push_it (struct it *it, struct text_pos *position)
5966 struct iterator_stack_entry *p;
5968 eassert (it->sp < IT_STACK_SIZE);
5969 p = it->stack + it->sp;
5971 p->stop_charpos = it->stop_charpos;
5972 p->prev_stop = it->prev_stop;
5973 p->base_level_stop = it->base_level_stop;
5974 p->cmp_it = it->cmp_it;
5975 eassert (it->face_id >= 0);
5976 p->face_id = it->face_id;
5977 p->string = it->string;
5978 p->method = it->method;
5979 p->from_overlay = it->from_overlay;
5980 switch (p->method)
5982 case GET_FROM_IMAGE:
5983 p->u.image.object = it->object;
5984 p->u.image.image_id = it->image_id;
5985 p->u.image.slice = it->slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 p->u.stretch.object = it->object;
5989 break;
5990 case GET_FROM_XWIDGET:
5991 p->u.xwidget.object = it->object;
5992 break;
5993 case GET_FROM_BUFFER:
5994 case GET_FROM_DISPLAY_VECTOR:
5995 case GET_FROM_STRING:
5996 case GET_FROM_C_STRING:
5997 break;
5998 default:
5999 emacs_abort ();
6001 p->position = position ? *position : it->position;
6002 p->current = it->current;
6003 p->end_charpos = it->end_charpos;
6004 p->string_nchars = it->string_nchars;
6005 p->area = it->area;
6006 p->multibyte_p = it->multibyte_p;
6007 p->avoid_cursor_p = it->avoid_cursor_p;
6008 p->space_width = it->space_width;
6009 p->font_height = it->font_height;
6010 p->voffset = it->voffset;
6011 p->string_from_display_prop_p = it->string_from_display_prop_p;
6012 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6013 p->display_ellipsis_p = false;
6014 p->line_wrap = it->line_wrap;
6015 p->bidi_p = it->bidi_p;
6016 p->paragraph_embedding = it->paragraph_embedding;
6017 p->from_disp_prop_p = it->from_disp_prop_p;
6018 ++it->sp;
6020 /* Save the state of the bidi iterator as well. */
6021 if (it->bidi_p)
6022 bidi_push_it (&it->bidi_it);
6025 static void
6026 iterate_out_of_display_property (struct it *it)
6028 bool buffer_p = !STRINGP (it->string);
6029 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6030 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6032 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6034 /* Maybe initialize paragraph direction. If we are at the beginning
6035 of a new paragraph, next_element_from_buffer may not have a
6036 chance to do that. */
6037 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6038 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6039 /* prev_stop can be zero, so check against BEGV as well. */
6040 while (it->bidi_it.charpos >= bob
6041 && it->prev_stop <= it->bidi_it.charpos
6042 && it->bidi_it.charpos < CHARPOS (it->position)
6043 && it->bidi_it.charpos < eob)
6044 bidi_move_to_visually_next (&it->bidi_it);
6045 /* Record the stop_pos we just crossed, for when we cross it
6046 back, maybe. */
6047 if (it->bidi_it.charpos > CHARPOS (it->position))
6048 it->prev_stop = CHARPOS (it->position);
6049 /* If we ended up not where pop_it put us, resync IT's
6050 positional members with the bidi iterator. */
6051 if (it->bidi_it.charpos != CHARPOS (it->position))
6052 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6053 if (buffer_p)
6054 it->current.pos = it->position;
6055 else
6056 it->current.string_pos = it->position;
6059 /* Restore IT's settings from IT->stack. Called, for example, when no
6060 more overlay strings must be processed, and we return to delivering
6061 display elements from a buffer, or when the end of a string from a
6062 `display' property is reached and we return to delivering display
6063 elements from an overlay string, or from a buffer. */
6065 static void
6066 pop_it (struct it *it)
6068 struct iterator_stack_entry *p;
6069 bool from_display_prop = it->from_disp_prop_p;
6070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6072 eassert (it->sp > 0);
6073 --it->sp;
6074 p = it->stack + it->sp;
6075 it->stop_charpos = p->stop_charpos;
6076 it->prev_stop = p->prev_stop;
6077 it->base_level_stop = p->base_level_stop;
6078 it->cmp_it = p->cmp_it;
6079 it->face_id = p->face_id;
6080 it->current = p->current;
6081 it->position = p->position;
6082 it->string = p->string;
6083 it->from_overlay = p->from_overlay;
6084 if (NILP (it->string))
6085 SET_TEXT_POS (it->current.string_pos, -1, -1);
6086 it->method = p->method;
6087 switch (it->method)
6089 case GET_FROM_IMAGE:
6090 it->image_id = p->u.image.image_id;
6091 it->object = p->u.image.object;
6092 it->slice = p->u.image.slice;
6093 break;
6094 case GET_FROM_XWIDGET:
6095 it->object = p->u.xwidget.object;
6096 break;
6097 case GET_FROM_STRETCH:
6098 it->object = p->u.stretch.object;
6099 break;
6100 case GET_FROM_BUFFER:
6101 it->object = it->w->contents;
6102 break;
6103 case GET_FROM_STRING:
6105 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6107 /* Restore the face_box_p flag, since it could have been
6108 overwritten by the face of the object that we just finished
6109 displaying. */
6110 if (face)
6111 it->face_box_p = face->box != FACE_NO_BOX;
6112 it->object = it->string;
6114 break;
6115 case GET_FROM_DISPLAY_VECTOR:
6116 if (it->s)
6117 it->method = GET_FROM_C_STRING;
6118 else if (STRINGP (it->string))
6119 it->method = GET_FROM_STRING;
6120 else
6122 it->method = GET_FROM_BUFFER;
6123 it->object = it->w->contents;
6125 break;
6126 case GET_FROM_C_STRING:
6127 break;
6128 default:
6129 emacs_abort ();
6131 it->end_charpos = p->end_charpos;
6132 it->string_nchars = p->string_nchars;
6133 it->area = p->area;
6134 it->multibyte_p = p->multibyte_p;
6135 it->avoid_cursor_p = p->avoid_cursor_p;
6136 it->space_width = p->space_width;
6137 it->font_height = p->font_height;
6138 it->voffset = p->voffset;
6139 it->string_from_display_prop_p = p->string_from_display_prop_p;
6140 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6141 it->line_wrap = p->line_wrap;
6142 it->bidi_p = p->bidi_p;
6143 it->paragraph_embedding = p->paragraph_embedding;
6144 it->from_disp_prop_p = p->from_disp_prop_p;
6145 if (it->bidi_p)
6147 bidi_pop_it (&it->bidi_it);
6148 /* Bidi-iterate until we get out of the portion of text, if any,
6149 covered by a `display' text property or by an overlay with
6150 `display' property. (We cannot just jump there, because the
6151 internal coherency of the bidi iterator state can not be
6152 preserved across such jumps.) We also must determine the
6153 paragraph base direction if the overlay we just processed is
6154 at the beginning of a new paragraph. */
6155 if (from_display_prop
6156 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6157 iterate_out_of_display_property (it);
6159 eassert ((BUFFERP (it->object)
6160 && IT_CHARPOS (*it) == it->bidi_it.charpos
6161 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6162 || (STRINGP (it->object)
6163 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6164 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6165 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6167 /* If we move the iterator over text covered by a display property
6168 to a new buffer position, any info about previously seen overlays
6169 is no longer valid. */
6170 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6171 it->ignore_overlay_strings_at_pos_p = false;
6176 /***********************************************************************
6177 Moving over lines
6178 ***********************************************************************/
6180 /* Set IT's current position to the previous line start. */
6182 static void
6183 back_to_previous_line_start (struct it *it)
6185 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6187 DEC_BOTH (cp, bp);
6188 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6192 /* Move IT to the next line start.
6194 Value is true if a newline was found. Set *SKIPPED_P to true if
6195 we skipped over part of the text (as opposed to moving the iterator
6196 continuously over the text). Otherwise, don't change the value
6197 of *SKIPPED_P.
6199 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6200 iterator on the newline, if it was found.
6202 Newlines may come from buffer text, overlay strings, or strings
6203 displayed via the `display' property. That's the reason we can't
6204 simply use find_newline_no_quit.
6206 Note that this function may not skip over invisible text that is so
6207 because of text properties and immediately follows a newline. If
6208 it would, function reseat_at_next_visible_line_start, when called
6209 from set_iterator_to_next, would effectively make invisible
6210 characters following a newline part of the wrong glyph row, which
6211 leads to wrong cursor motion. */
6213 static bool
6214 forward_to_next_line_start (struct it *it, bool *skipped_p,
6215 struct bidi_it *bidi_it_prev)
6217 ptrdiff_t old_selective;
6218 bool newline_found_p = false;
6219 int n;
6220 const int MAX_NEWLINE_DISTANCE = 500;
6222 /* If already on a newline, just consume it to avoid unintended
6223 skipping over invisible text below. */
6224 if (it->what == IT_CHARACTER
6225 && it->c == '\n'
6226 && CHARPOS (it->position) == IT_CHARPOS (*it))
6228 if (it->bidi_p && bidi_it_prev)
6229 *bidi_it_prev = it->bidi_it;
6230 set_iterator_to_next (it, false);
6231 it->c = 0;
6232 return true;
6235 /* Don't handle selective display in the following. It's (a)
6236 unnecessary because it's done by the caller, and (b) leads to an
6237 infinite recursion because next_element_from_ellipsis indirectly
6238 calls this function. */
6239 old_selective = it->selective;
6240 it->selective = 0;
6242 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6243 from buffer text. */
6244 for (n = 0;
6245 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6246 n += !STRINGP (it->string))
6248 if (!get_next_display_element (it))
6249 return false;
6250 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6251 if (newline_found_p && it->bidi_p && bidi_it_prev)
6252 *bidi_it_prev = it->bidi_it;
6253 set_iterator_to_next (it, false);
6256 /* If we didn't find a newline near enough, see if we can use a
6257 short-cut. */
6258 if (!newline_found_p)
6260 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6261 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6262 1, &bytepos);
6263 Lisp_Object pos;
6265 eassert (!STRINGP (it->string));
6267 /* If there isn't any `display' property in sight, and no
6268 overlays, we can just use the position of the newline in
6269 buffer text. */
6270 if (it->stop_charpos >= limit
6271 || ((pos = Fnext_single_property_change (make_number (start),
6272 Qdisplay, Qnil,
6273 make_number (limit)),
6274 NILP (pos))
6275 && next_overlay_change (start) == ZV))
6277 if (!it->bidi_p)
6279 IT_CHARPOS (*it) = limit;
6280 IT_BYTEPOS (*it) = bytepos;
6282 else
6284 struct bidi_it bprev;
6286 /* Help bidi.c avoid expensive searches for display
6287 properties and overlays, by telling it that there are
6288 none up to `limit'. */
6289 if (it->bidi_it.disp_pos < limit)
6291 it->bidi_it.disp_pos = limit;
6292 it->bidi_it.disp_prop = 0;
6294 do {
6295 bprev = it->bidi_it;
6296 bidi_move_to_visually_next (&it->bidi_it);
6297 } while (it->bidi_it.charpos != limit);
6298 IT_CHARPOS (*it) = limit;
6299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 if (bidi_it_prev)
6301 *bidi_it_prev = bprev;
6303 *skipped_p = newline_found_p = true;
6305 else
6307 while (!newline_found_p)
6309 if (!get_next_display_element (it))
6310 break;
6311 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6312 if (newline_found_p && it->bidi_p && bidi_it_prev)
6313 *bidi_it_prev = it->bidi_it;
6314 set_iterator_to_next (it, false);
6319 it->selective = old_selective;
6320 return newline_found_p;
6324 /* Set IT's current position to the previous visible line start. Skip
6325 invisible text that is so either due to text properties or due to
6326 selective display. Caution: this does not change IT->current_x and
6327 IT->hpos. */
6329 static void
6330 back_to_previous_visible_line_start (struct it *it)
6332 while (IT_CHARPOS (*it) > BEGV)
6334 back_to_previous_line_start (it);
6336 if (IT_CHARPOS (*it) <= BEGV)
6337 break;
6339 /* If selective > 0, then lines indented more than its value are
6340 invisible. */
6341 if (it->selective > 0
6342 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6343 it->selective))
6344 continue;
6346 /* Check the newline before point for invisibility. */
6348 Lisp_Object prop;
6349 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6350 Qinvisible, it->window);
6351 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6352 continue;
6355 if (IT_CHARPOS (*it) <= BEGV)
6356 break;
6359 struct it it2;
6360 void *it2data = NULL;
6361 ptrdiff_t pos;
6362 ptrdiff_t beg, end;
6363 Lisp_Object val, overlay;
6365 SAVE_IT (it2, *it, it2data);
6367 /* If newline is part of a composition, continue from start of composition */
6368 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6369 && beg < IT_CHARPOS (*it))
6370 goto replaced;
6372 /* If newline is replaced by a display property, find start of overlay
6373 or interval and continue search from that point. */
6374 pos = --IT_CHARPOS (it2);
6375 --IT_BYTEPOS (it2);
6376 it2.sp = 0;
6377 bidi_unshelve_cache (NULL, false);
6378 it2.string_from_display_prop_p = false;
6379 it2.from_disp_prop_p = false;
6380 if (handle_display_prop (&it2) == HANDLED_RETURN
6381 && !NILP (val = get_char_property_and_overlay
6382 (make_number (pos), Qdisplay, Qnil, &overlay))
6383 && (OVERLAYP (overlay)
6384 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6385 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6387 RESTORE_IT (it, it, it2data);
6388 goto replaced;
6391 /* Newline is not replaced by anything -- so we are done. */
6392 RESTORE_IT (it, it, it2data);
6393 break;
6395 replaced:
6396 if (beg < BEGV)
6397 beg = BEGV;
6398 IT_CHARPOS (*it) = beg;
6399 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6403 it->continuation_lines_width = 0;
6405 eassert (IT_CHARPOS (*it) >= BEGV);
6406 eassert (IT_CHARPOS (*it) == BEGV
6407 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6408 CHECK_IT (it);
6412 /* Reseat iterator IT at the previous visible line start. Skip
6413 invisible text that is so either due to text properties or due to
6414 selective display. At the end, update IT's overlay information,
6415 face information etc. */
6417 void
6418 reseat_at_previous_visible_line_start (struct it *it)
6420 back_to_previous_visible_line_start (it);
6421 reseat (it, it->current.pos, true);
6422 CHECK_IT (it);
6426 /* Reseat iterator IT on the next visible line start in the current
6427 buffer. ON_NEWLINE_P means position IT on the newline
6428 preceding the line start. Skip over invisible text that is so
6429 because of selective display. Compute faces, overlays etc at the
6430 new position. Note that this function does not skip over text that
6431 is invisible because of text properties. */
6433 static void
6434 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6436 bool skipped_p = false;
6437 struct bidi_it bidi_it_prev;
6438 bool newline_found_p
6439 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6441 /* Skip over lines that are invisible because they are indented
6442 more than the value of IT->selective. */
6443 if (it->selective > 0)
6444 while (IT_CHARPOS (*it) < ZV
6445 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6446 it->selective))
6448 eassert (IT_BYTEPOS (*it) == BEGV
6449 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6450 newline_found_p =
6451 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6454 /* Position on the newline if that's what's requested. */
6455 if (on_newline_p && newline_found_p)
6457 if (STRINGP (it->string))
6459 if (IT_STRING_CHARPOS (*it) > 0)
6461 if (!it->bidi_p)
6463 --IT_STRING_CHARPOS (*it);
6464 --IT_STRING_BYTEPOS (*it);
6466 else
6468 /* We need to restore the bidi iterator to the state
6469 it had on the newline, and resync the IT's
6470 position with that. */
6471 it->bidi_it = bidi_it_prev;
6472 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6473 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6477 else if (IT_CHARPOS (*it) > BEGV)
6479 if (!it->bidi_p)
6481 --IT_CHARPOS (*it);
6482 --IT_BYTEPOS (*it);
6484 else
6486 /* We need to restore the bidi iterator to the state it
6487 had on the newline and resync IT with that. */
6488 it->bidi_it = bidi_it_prev;
6489 IT_CHARPOS (*it) = it->bidi_it.charpos;
6490 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6492 reseat (it, it->current.pos, false);
6495 else if (skipped_p)
6496 reseat (it, it->current.pos, false);
6498 CHECK_IT (it);
6503 /***********************************************************************
6504 Changing an iterator's position
6505 ***********************************************************************/
6507 /* Change IT's current position to POS in current_buffer.
6508 If FORCE_P, always check for text properties at the new position.
6509 Otherwise, text properties are only looked up if POS >=
6510 IT->check_charpos of a property. */
6512 static void
6513 reseat (struct it *it, struct text_pos pos, bool force_p)
6515 ptrdiff_t original_pos = IT_CHARPOS (*it);
6517 reseat_1 (it, pos, false);
6519 /* Determine where to check text properties. Avoid doing it
6520 where possible because text property lookup is very expensive. */
6521 if (force_p
6522 || CHARPOS (pos) > it->stop_charpos
6523 || CHARPOS (pos) < original_pos)
6525 if (it->bidi_p)
6527 /* For bidi iteration, we need to prime prev_stop and
6528 base_level_stop with our best estimations. */
6529 /* Implementation note: Of course, POS is not necessarily a
6530 stop position, so assigning prev_pos to it is a lie; we
6531 should have called compute_stop_backwards. However, if
6532 the current buffer does not include any R2L characters,
6533 that call would be a waste of cycles, because the
6534 iterator will never move back, and thus never cross this
6535 "fake" stop position. So we delay that backward search
6536 until the time we really need it, in next_element_from_buffer. */
6537 if (CHARPOS (pos) != it->prev_stop)
6538 it->prev_stop = CHARPOS (pos);
6539 if (CHARPOS (pos) < it->base_level_stop)
6540 it->base_level_stop = 0; /* meaning it's unknown */
6541 handle_stop (it);
6543 else
6545 handle_stop (it);
6546 it->prev_stop = it->base_level_stop = 0;
6551 CHECK_IT (it);
6555 /* Change IT's buffer position to POS. SET_STOP_P means set
6556 IT->stop_pos to POS, also. */
6558 static void
6559 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6561 /* Don't call this function when scanning a C string. */
6562 eassert (it->s == NULL);
6564 /* POS must be a reasonable value. */
6565 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6567 it->current.pos = it->position = pos;
6568 it->end_charpos = ZV;
6569 it->dpvec = NULL;
6570 it->current.dpvec_index = -1;
6571 it->current.overlay_string_index = -1;
6572 IT_STRING_CHARPOS (*it) = -1;
6573 IT_STRING_BYTEPOS (*it) = -1;
6574 it->string = Qnil;
6575 it->method = GET_FROM_BUFFER;
6576 it->object = it->w->contents;
6577 it->area = TEXT_AREA;
6578 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6579 it->sp = 0;
6580 it->string_from_display_prop_p = false;
6581 it->string_from_prefix_prop_p = false;
6583 it->from_disp_prop_p = false;
6584 it->face_before_selective_p = false;
6585 if (it->bidi_p)
6587 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6588 &it->bidi_it);
6589 bidi_unshelve_cache (NULL, false);
6590 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6591 it->bidi_it.string.s = NULL;
6592 it->bidi_it.string.lstring = Qnil;
6593 it->bidi_it.string.bufpos = 0;
6594 it->bidi_it.string.from_disp_str = false;
6595 it->bidi_it.string.unibyte = false;
6596 it->bidi_it.w = it->w;
6599 if (set_stop_p)
6601 it->stop_charpos = CHARPOS (pos);
6602 it->base_level_stop = CHARPOS (pos);
6604 /* This make the information stored in it->cmp_it invalidate. */
6605 it->cmp_it.id = -1;
6609 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6610 If S is non-null, it is a C string to iterate over. Otherwise,
6611 STRING gives a Lisp string to iterate over.
6613 If PRECISION > 0, don't return more then PRECISION number of
6614 characters from the string.
6616 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6617 characters have been returned. FIELD_WIDTH < 0 means an infinite
6618 field width.
6620 MULTIBYTE = 0 means disable processing of multibyte characters,
6621 MULTIBYTE > 0 means enable it,
6622 MULTIBYTE < 0 means use IT->multibyte_p.
6624 IT must be initialized via a prior call to init_iterator before
6625 calling this function. */
6627 static void
6628 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6629 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6630 int multibyte)
6632 /* No text property checks performed by default, but see below. */
6633 it->stop_charpos = -1;
6635 /* Set iterator position and end position. */
6636 memset (&it->current, 0, sizeof it->current);
6637 it->current.overlay_string_index = -1;
6638 it->current.dpvec_index = -1;
6639 eassert (charpos >= 0);
6641 /* If STRING is specified, use its multibyteness, otherwise use the
6642 setting of MULTIBYTE, if specified. */
6643 if (multibyte >= 0)
6644 it->multibyte_p = multibyte > 0;
6646 /* Bidirectional reordering of strings is controlled by the default
6647 value of bidi-display-reordering. Don't try to reorder while
6648 loading loadup.el, as the necessary character property tables are
6649 not yet available. */
6650 it->bidi_p =
6651 !redisplay__inhibit_bidi
6652 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6654 if (s == NULL)
6656 eassert (STRINGP (string));
6657 it->string = string;
6658 it->s = NULL;
6659 it->end_charpos = it->string_nchars = SCHARS (string);
6660 it->method = GET_FROM_STRING;
6661 it->current.string_pos = string_pos (charpos, string);
6663 if (it->bidi_p)
6665 it->bidi_it.string.lstring = string;
6666 it->bidi_it.string.s = NULL;
6667 it->bidi_it.string.schars = it->end_charpos;
6668 it->bidi_it.string.bufpos = 0;
6669 it->bidi_it.string.from_disp_str = false;
6670 it->bidi_it.string.unibyte = !it->multibyte_p;
6671 it->bidi_it.w = it->w;
6672 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6673 FRAME_WINDOW_P (it->f), &it->bidi_it);
6676 else
6678 it->s = (const unsigned char *) s;
6679 it->string = Qnil;
6681 /* Note that we use IT->current.pos, not it->current.string_pos,
6682 for displaying C strings. */
6683 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6684 if (it->multibyte_p)
6686 it->current.pos = c_string_pos (charpos, s, true);
6687 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6689 else
6691 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6692 it->end_charpos = it->string_nchars = strlen (s);
6695 if (it->bidi_p)
6697 it->bidi_it.string.lstring = Qnil;
6698 it->bidi_it.string.s = (const unsigned char *) s;
6699 it->bidi_it.string.schars = it->end_charpos;
6700 it->bidi_it.string.bufpos = 0;
6701 it->bidi_it.string.from_disp_str = false;
6702 it->bidi_it.string.unibyte = !it->multibyte_p;
6703 it->bidi_it.w = it->w;
6704 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6705 &it->bidi_it);
6707 it->method = GET_FROM_C_STRING;
6710 /* PRECISION > 0 means don't return more than PRECISION characters
6711 from the string. */
6712 if (precision > 0 && it->end_charpos - charpos > precision)
6714 it->end_charpos = it->string_nchars = charpos + precision;
6715 if (it->bidi_p)
6716 it->bidi_it.string.schars = it->end_charpos;
6719 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6720 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6721 FIELD_WIDTH < 0 means infinite field width. This is useful for
6722 padding with `-' at the end of a mode line. */
6723 if (field_width < 0)
6724 field_width = INFINITY;
6725 /* Implementation note: We deliberately don't enlarge
6726 it->bidi_it.string.schars here to fit it->end_charpos, because
6727 the bidi iterator cannot produce characters out of thin air. */
6728 if (field_width > it->end_charpos - charpos)
6729 it->end_charpos = charpos + field_width;
6731 /* Use the standard display table for displaying strings. */
6732 if (DISP_TABLE_P (Vstandard_display_table))
6733 it->dp = XCHAR_TABLE (Vstandard_display_table);
6735 it->stop_charpos = charpos;
6736 it->prev_stop = charpos;
6737 it->base_level_stop = 0;
6738 if (it->bidi_p)
6740 it->bidi_it.first_elt = true;
6741 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6742 it->bidi_it.disp_pos = -1;
6744 if (s == NULL && it->multibyte_p)
6746 ptrdiff_t endpos = SCHARS (it->string);
6747 if (endpos > it->end_charpos)
6748 endpos = it->end_charpos;
6749 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6750 it->string);
6752 CHECK_IT (it);
6757 /***********************************************************************
6758 Iteration
6759 ***********************************************************************/
6761 /* Map enum it_method value to corresponding next_element_from_* function. */
6763 typedef bool (*next_element_function) (struct it *);
6765 static next_element_function const get_next_element[NUM_IT_METHODS] =
6767 next_element_from_buffer,
6768 next_element_from_display_vector,
6769 next_element_from_string,
6770 next_element_from_c_string,
6771 next_element_from_image,
6772 next_element_from_stretch,
6773 next_element_from_xwidget,
6776 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6779 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6780 (possibly with the following characters). */
6782 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6783 ((IT)->cmp_it.id >= 0 \
6784 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6785 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6786 END_CHARPOS, (IT)->w, \
6787 FACE_FROM_ID_OR_NULL ((IT)->f, \
6788 (IT)->face_id), \
6789 (IT)->string)))
6792 /* Lookup the char-table Vglyphless_char_display for character C (-1
6793 if we want information for no-font case), and return the display
6794 method symbol. By side-effect, update it->what and
6795 it->glyphless_method. This function is called from
6796 get_next_display_element for each character element, and from
6797 x_produce_glyphs when no suitable font was found. */
6799 Lisp_Object
6800 lookup_glyphless_char_display (int c, struct it *it)
6802 Lisp_Object glyphless_method = Qnil;
6804 if (CHAR_TABLE_P (Vglyphless_char_display)
6805 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6807 if (c >= 0)
6809 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6810 if (CONSP (glyphless_method))
6811 glyphless_method = FRAME_WINDOW_P (it->f)
6812 ? XCAR (glyphless_method)
6813 : XCDR (glyphless_method);
6815 else
6816 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6819 retry:
6820 if (NILP (glyphless_method))
6822 if (c >= 0)
6823 /* The default is to display the character by a proper font. */
6824 return Qnil;
6825 /* The default for the no-font case is to display an empty box. */
6826 glyphless_method = Qempty_box;
6828 if (EQ (glyphless_method, Qzero_width))
6830 if (c >= 0)
6831 return glyphless_method;
6832 /* This method can't be used for the no-font case. */
6833 glyphless_method = Qempty_box;
6835 if (EQ (glyphless_method, Qthin_space))
6836 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6837 else if (EQ (glyphless_method, Qempty_box))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6839 else if (EQ (glyphless_method, Qhex_code))
6840 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6841 else if (STRINGP (glyphless_method))
6842 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6843 else
6845 /* Invalid value. We use the default method. */
6846 glyphless_method = Qnil;
6847 goto retry;
6849 it->what = IT_GLYPHLESS;
6850 return glyphless_method;
6853 /* Merge escape glyph face and cache the result. */
6855 static struct frame *last_escape_glyph_frame = NULL;
6856 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6857 static int last_escape_glyph_merged_face_id = 0;
6859 static int
6860 merge_escape_glyph_face (struct it *it)
6862 int face_id;
6864 if (it->f == last_escape_glyph_frame
6865 && it->face_id == last_escape_glyph_face_id)
6866 face_id = last_escape_glyph_merged_face_id;
6867 else
6869 /* Merge the `escape-glyph' face into the current face. */
6870 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6871 last_escape_glyph_frame = it->f;
6872 last_escape_glyph_face_id = it->face_id;
6873 last_escape_glyph_merged_face_id = face_id;
6875 return face_id;
6878 /* Likewise for glyphless glyph face. */
6880 static struct frame *last_glyphless_glyph_frame = NULL;
6881 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6882 static int last_glyphless_glyph_merged_face_id = 0;
6885 merge_glyphless_glyph_face (struct it *it)
6887 int face_id;
6889 if (it->f == last_glyphless_glyph_frame
6890 && it->face_id == last_glyphless_glyph_face_id)
6891 face_id = last_glyphless_glyph_merged_face_id;
6892 else
6894 /* Merge the `glyphless-char' face into the current face. */
6895 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6896 last_glyphless_glyph_frame = it->f;
6897 last_glyphless_glyph_face_id = it->face_id;
6898 last_glyphless_glyph_merged_face_id = face_id;
6900 return face_id;
6903 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6904 be called before redisplaying windows, and when the frame's face
6905 cache is freed. */
6906 void
6907 forget_escape_and_glyphless_faces (void)
6909 last_escape_glyph_frame = NULL;
6910 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6911 last_glyphless_glyph_frame = NULL;
6912 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6915 /* Load IT's display element fields with information about the next
6916 display element from the current position of IT. Value is false if
6917 end of buffer (or C string) is reached. */
6919 static bool
6920 get_next_display_element (struct it *it)
6922 /* True means that we found a display element. False means that
6923 we hit the end of what we iterate over. Performance note: the
6924 function pointer `method' used here turns out to be faster than
6925 using a sequence of if-statements. */
6926 bool success_p;
6928 get_next:
6929 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6931 if (it->what == IT_CHARACTER)
6933 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6934 and only if (a) the resolved directionality of that character
6935 is R..." */
6936 /* FIXME: Do we need an exception for characters from display
6937 tables? */
6938 if (it->bidi_p && it->bidi_it.type == STRONG_R
6939 && !inhibit_bidi_mirroring)
6940 it->c = bidi_mirror_char (it->c);
6941 /* Map via display table or translate control characters.
6942 IT->c, IT->len etc. have been set to the next character by
6943 the function call above. If we have a display table, and it
6944 contains an entry for IT->c, translate it. Don't do this if
6945 IT->c itself comes from a display table, otherwise we could
6946 end up in an infinite recursion. (An alternative could be to
6947 count the recursion depth of this function and signal an
6948 error when a certain maximum depth is reached.) Is it worth
6949 it? */
6950 if (success_p && it->dpvec == NULL)
6952 Lisp_Object dv;
6953 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6954 bool nonascii_space_p = false;
6955 bool nonascii_hyphen_p = false;
6956 int c = it->c; /* This is the character to display. */
6958 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6960 eassert (SINGLE_BYTE_CHAR_P (c));
6961 if (unibyte_display_via_language_environment)
6963 c = DECODE_CHAR (unibyte, c);
6964 if (c < 0)
6965 c = BYTE8_TO_CHAR (it->c);
6967 else
6968 c = BYTE8_TO_CHAR (it->c);
6971 if (it->dp
6972 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6973 VECTORP (dv)))
6975 struct Lisp_Vector *v = XVECTOR (dv);
6977 /* Return the first character from the display table
6978 entry, if not empty. If empty, don't display the
6979 current character. */
6980 if (v->header.size)
6982 it->dpvec_char_len = it->len;
6983 it->dpvec = v->contents;
6984 it->dpend = v->contents + v->header.size;
6985 it->current.dpvec_index = 0;
6986 it->dpvec_face_id = -1;
6987 it->saved_face_id = it->face_id;
6988 it->method = GET_FROM_DISPLAY_VECTOR;
6989 it->ellipsis_p = false;
6991 else
6993 set_iterator_to_next (it, false);
6995 goto get_next;
6998 if (! NILP (lookup_glyphless_char_display (c, it)))
7000 if (it->what == IT_GLYPHLESS)
7001 goto done;
7002 /* Don't display this character. */
7003 set_iterator_to_next (it, false);
7004 goto get_next;
7007 /* If `nobreak-char-display' is non-nil, we display
7008 non-ASCII spaces and hyphens specially. */
7009 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7011 if (c == NO_BREAK_SPACE)
7012 nonascii_space_p = true;
7013 else if (c == SOFT_HYPHEN || c == HYPHEN
7014 || c == NON_BREAKING_HYPHEN)
7015 nonascii_hyphen_p = true;
7018 /* Translate control characters into `\003' or `^C' form.
7019 Control characters coming from a display table entry are
7020 currently not translated because we use IT->dpvec to hold
7021 the translation. This could easily be changed but I
7022 don't believe that it is worth doing.
7024 The characters handled by `nobreak-char-display' must be
7025 translated too.
7027 Non-printable characters and raw-byte characters are also
7028 translated to octal form. */
7029 if (((c < ' ' || c == 127) /* ASCII control chars. */
7030 ? (it->area != TEXT_AREA
7031 /* In mode line, treat \n, \t like other crl chars. */
7032 || (c != '\t'
7033 && it->glyph_row
7034 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7035 || (c != '\n' && c != '\t'))
7036 : (nonascii_space_p
7037 || nonascii_hyphen_p
7038 || CHAR_BYTE8_P (c)
7039 || ! CHAR_PRINTABLE_P (c))))
7041 /* C is a control character, non-ASCII space/hyphen,
7042 raw-byte, or a non-printable character which must be
7043 displayed either as '\003' or as `^C' where the '\\'
7044 and '^' can be defined in the display table. Fill
7045 IT->ctl_chars with glyphs for what we have to
7046 display. Then, set IT->dpvec to these glyphs. */
7047 Lisp_Object gc;
7048 int ctl_len;
7049 int face_id;
7050 int lface_id = 0;
7051 int escape_glyph;
7053 /* Handle control characters with ^. */
7055 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7057 int g;
7059 g = '^'; /* default glyph for Control */
7060 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7061 if (it->dp
7062 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7064 g = GLYPH_CODE_CHAR (gc);
7065 lface_id = GLYPH_CODE_FACE (gc);
7068 face_id = (lface_id
7069 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7070 : merge_escape_glyph_face (it));
7072 XSETINT (it->ctl_chars[0], g);
7073 XSETINT (it->ctl_chars[1], c ^ 0100);
7074 ctl_len = 2;
7075 goto display_control;
7078 /* Handle non-ascii space in the mode where it only gets
7079 highlighting. */
7081 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7083 /* Merge `nobreak-space' into the current face. */
7084 face_id = merge_faces (it->f, Qnobreak_space, 0,
7085 it->face_id);
7086 XSETINT (it->ctl_chars[0], ' ');
7087 ctl_len = 1;
7088 goto display_control;
7091 /* Handle non-ascii hyphens in the mode where it only
7092 gets highlighting. */
7094 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7096 /* Merge `nobreak-space' into the current face. */
7097 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7098 it->face_id);
7099 XSETINT (it->ctl_chars[0], '-');
7100 ctl_len = 1;
7101 goto display_control;
7104 /* Handle sequences that start with the "escape glyph". */
7106 /* the default escape glyph is \. */
7107 escape_glyph = '\\';
7109 if (it->dp
7110 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7112 escape_glyph = GLYPH_CODE_CHAR (gc);
7113 lface_id = GLYPH_CODE_FACE (gc);
7116 face_id = (lface_id
7117 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7118 : merge_escape_glyph_face (it));
7120 /* Draw non-ASCII space/hyphen with escape glyph: */
7122 if (nonascii_space_p || nonascii_hyphen_p)
7124 XSETINT (it->ctl_chars[0], escape_glyph);
7125 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7126 ctl_len = 2;
7127 goto display_control;
7131 char str[10];
7132 int len, i;
7134 if (CHAR_BYTE8_P (c))
7135 /* Display \200 instead of \17777600. */
7136 c = CHAR_TO_BYTE8 (c);
7137 len = sprintf (str, "%03o", c + 0u);
7139 XSETINT (it->ctl_chars[0], escape_glyph);
7140 for (i = 0; i < len; i++)
7141 XSETINT (it->ctl_chars[i + 1], str[i]);
7142 ctl_len = len + 1;
7145 display_control:
7146 /* Set up IT->dpvec and return first character from it. */
7147 it->dpvec_char_len = it->len;
7148 it->dpvec = it->ctl_chars;
7149 it->dpend = it->dpvec + ctl_len;
7150 it->current.dpvec_index = 0;
7151 it->dpvec_face_id = face_id;
7152 it->saved_face_id = it->face_id;
7153 it->method = GET_FROM_DISPLAY_VECTOR;
7154 it->ellipsis_p = false;
7155 goto get_next;
7157 it->char_to_display = c;
7159 else if (success_p)
7161 it->char_to_display = it->c;
7165 #ifdef HAVE_WINDOW_SYSTEM
7166 /* Adjust face id for a multibyte character. There are no multibyte
7167 character in unibyte text. */
7168 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7169 && it->multibyte_p
7170 && success_p
7171 && FRAME_WINDOW_P (it->f))
7173 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7175 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7177 /* Automatic composition with glyph-string. */
7178 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7180 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7182 else
7184 ptrdiff_t pos = (it->s ? -1
7185 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7186 : IT_CHARPOS (*it));
7187 int c;
7189 if (it->what == IT_CHARACTER)
7190 c = it->char_to_display;
7191 else
7193 struct composition *cmp = composition_table[it->cmp_it.id];
7194 int i;
7196 c = ' ';
7197 for (i = 0; i < cmp->glyph_len; i++)
7198 /* TAB in a composition means display glyphs with
7199 padding space on the left or right. */
7200 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7201 break;
7203 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7206 #endif /* HAVE_WINDOW_SYSTEM */
7208 done:
7209 /* Is this character the last one of a run of characters with
7210 box? If yes, set IT->end_of_box_run_p to true. */
7211 if (it->face_box_p
7212 && it->s == NULL)
7214 if (it->method == GET_FROM_STRING && it->sp)
7216 int face_id = underlying_face_id (it);
7217 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7219 if (face)
7221 if (face->box == FACE_NO_BOX)
7223 /* If the box comes from face properties in a
7224 display string, check faces in that string. */
7225 int string_face_id = face_after_it_pos (it);
7226 it->end_of_box_run_p
7227 = (FACE_FROM_ID (it->f, string_face_id)->box
7228 == FACE_NO_BOX);
7230 /* Otherwise, the box comes from the underlying face.
7231 If this is the last string character displayed, check
7232 the next buffer location. */
7233 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7234 /* n_overlay_strings is unreliable unless
7235 overlay_string_index is non-negative. */
7236 && ((it->current.overlay_string_index >= 0
7237 && (it->current.overlay_string_index
7238 == it->n_overlay_strings - 1))
7239 /* A string from display property. */
7240 || it->from_disp_prop_p))
7242 ptrdiff_t ignore;
7243 int next_face_id;
7244 bool text_from_string = false;
7245 /* Normally, the next buffer location is stored in
7246 IT->current.pos... */
7247 struct text_pos pos = it->current.pos;
7249 /* ...but for a string from a display property, the
7250 next buffer position is stored in the 'position'
7251 member of the iteration stack slot below the
7252 current one, see handle_single_display_spec. By
7253 contrast, it->current.pos was not yet updated to
7254 point to that buffer position; that will happen
7255 in pop_it, after we finish displaying the current
7256 string. Note that we already checked above that
7257 it->sp is positive, so subtracting one from it is
7258 safe. */
7259 if (it->from_disp_prop_p)
7261 int stackp = it->sp - 1;
7263 /* Find the stack level with data from buffer. */
7264 while (stackp >= 0
7265 && STRINGP ((it->stack + stackp)->string))
7266 stackp--;
7267 if (stackp < 0)
7269 /* If no stack slot was found for iterating
7270 a buffer, we are displaying text from a
7271 string, most probably the mode line or
7272 the header line, and that string has a
7273 display string on some of its
7274 characters. */
7275 text_from_string = true;
7276 pos = it->stack[it->sp - 1].position;
7278 else
7279 pos = (it->stack + stackp)->position;
7281 else
7282 INC_TEXT_POS (pos, it->multibyte_p);
7284 if (text_from_string)
7286 Lisp_Object base_string = it->stack[it->sp - 1].string;
7288 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7289 it->end_of_box_run_p = true;
7290 else
7292 next_face_id
7293 = face_at_string_position (it->w, base_string,
7294 CHARPOS (pos), 0,
7295 &ignore, face_id, false);
7296 it->end_of_box_run_p
7297 = (FACE_FROM_ID (it->f, next_face_id)->box
7298 == FACE_NO_BOX);
7301 else if (CHARPOS (pos) >= ZV)
7302 it->end_of_box_run_p = true;
7303 else
7305 next_face_id =
7306 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7307 CHARPOS (pos)
7308 + TEXT_PROP_DISTANCE_LIMIT,
7309 false, -1);
7310 it->end_of_box_run_p
7311 = (FACE_FROM_ID (it->f, next_face_id)->box
7312 == FACE_NO_BOX);
7317 /* next_element_from_display_vector sets this flag according to
7318 faces of the display vector glyphs, see there. */
7319 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7321 int face_id = face_after_it_pos (it);
7322 it->end_of_box_run_p
7323 = (face_id != it->face_id
7324 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7327 /* If we reached the end of the object we've been iterating (e.g., a
7328 display string or an overlay string), and there's something on
7329 IT->stack, proceed with what's on the stack. It doesn't make
7330 sense to return false if there's unprocessed stuff on the stack,
7331 because otherwise that stuff will never be displayed. */
7332 if (!success_p && it->sp > 0)
7334 set_iterator_to_next (it, false);
7335 success_p = get_next_display_element (it);
7338 /* Value is false if end of buffer or string reached. */
7339 return success_p;
7343 /* Move IT to the next display element.
7345 RESEAT_P means if called on a newline in buffer text,
7346 skip to the next visible line start.
7348 Functions get_next_display_element and set_iterator_to_next are
7349 separate because I find this arrangement easier to handle than a
7350 get_next_display_element function that also increments IT's
7351 position. The way it is we can first look at an iterator's current
7352 display element, decide whether it fits on a line, and if it does,
7353 increment the iterator position. The other way around we probably
7354 would either need a flag indicating whether the iterator has to be
7355 incremented the next time, or we would have to implement a
7356 decrement position function which would not be easy to write. */
7358 void
7359 set_iterator_to_next (struct it *it, bool reseat_p)
7361 /* Reset flags indicating start and end of a sequence of characters
7362 with box. Reset them at the start of this function because
7363 moving the iterator to a new position might set them. */
7364 it->start_of_box_run_p = it->end_of_box_run_p = false;
7366 switch (it->method)
7368 case GET_FROM_BUFFER:
7369 /* The current display element of IT is a character from
7370 current_buffer. Advance in the buffer, and maybe skip over
7371 invisible lines that are so because of selective display. */
7372 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7373 reseat_at_next_visible_line_start (it, false);
7374 else if (it->cmp_it.id >= 0)
7376 /* We are currently getting glyphs from a composition. */
7377 if (! it->bidi_p)
7379 IT_CHARPOS (*it) += it->cmp_it.nchars;
7380 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7382 else
7384 int i;
7386 /* Update IT's char/byte positions to point to the first
7387 character of the next grapheme cluster, or to the
7388 character visually after the current composition. */
7389 for (i = 0; i < it->cmp_it.nchars; i++)
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_CHARPOS (*it) = it->bidi_it.charpos;
7395 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7396 && it->cmp_it.to < it->cmp_it.nglyphs)
7398 /* Composition created while scanning forward. Proceed
7399 to the next grapheme cluster. */
7400 it->cmp_it.from = it->cmp_it.to;
7402 else if ((it->bidi_p && it->cmp_it.reversed_p)
7403 && it->cmp_it.from > 0)
7405 /* Composition created while scanning backward. Proceed
7406 to the previous grapheme cluster. */
7407 it->cmp_it.to = it->cmp_it.from;
7409 else
7411 /* No more grapheme clusters in this composition.
7412 Find the next stop position. */
7413 ptrdiff_t stop = it->end_charpos;
7415 if (it->bidi_it.scan_dir < 0)
7416 /* Now we are scanning backward and don't know
7417 where to stop. */
7418 stop = -1;
7419 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7420 IT_BYTEPOS (*it), stop, Qnil);
7423 else
7425 eassert (it->len != 0);
7427 if (!it->bidi_p)
7429 IT_BYTEPOS (*it) += it->len;
7430 IT_CHARPOS (*it) += 1;
7432 else
7434 int prev_scan_dir = it->bidi_it.scan_dir;
7435 /* If this is a new paragraph, determine its base
7436 direction (a.k.a. its base embedding level). */
7437 if (it->bidi_it.new_paragraph)
7438 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7439 false);
7440 bidi_move_to_visually_next (&it->bidi_it);
7441 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7442 IT_CHARPOS (*it) = it->bidi_it.charpos;
7443 if (prev_scan_dir != it->bidi_it.scan_dir)
7445 /* As the scan direction was changed, we must
7446 re-compute the stop position for composition. */
7447 ptrdiff_t stop = it->end_charpos;
7448 if (it->bidi_it.scan_dir < 0)
7449 stop = -1;
7450 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7451 IT_BYTEPOS (*it), stop, Qnil);
7454 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7456 break;
7458 case GET_FROM_C_STRING:
7459 /* Current display element of IT is from a C string. */
7460 if (!it->bidi_p
7461 /* If the string position is beyond string's end, it means
7462 next_element_from_c_string is padding the string with
7463 blanks, in which case we bypass the bidi iterator,
7464 because it cannot deal with such virtual characters. */
7465 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7467 IT_BYTEPOS (*it) += it->len;
7468 IT_CHARPOS (*it) += 1;
7470 else
7472 bidi_move_to_visually_next (&it->bidi_it);
7473 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7474 IT_CHARPOS (*it) = it->bidi_it.charpos;
7476 break;
7478 case GET_FROM_DISPLAY_VECTOR:
7479 /* Current display element of IT is from a display table entry.
7480 Advance in the display table definition. Reset it to null if
7481 end reached, and continue with characters from buffers/
7482 strings. */
7483 ++it->current.dpvec_index;
7485 /* Restore face of the iterator to what they were before the
7486 display vector entry (these entries may contain faces). */
7487 it->face_id = it->saved_face_id;
7489 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7491 bool recheck_faces = it->ellipsis_p;
7493 if (it->s)
7494 it->method = GET_FROM_C_STRING;
7495 else if (STRINGP (it->string))
7496 it->method = GET_FROM_STRING;
7497 else
7499 it->method = GET_FROM_BUFFER;
7500 it->object = it->w->contents;
7503 it->dpvec = NULL;
7504 it->current.dpvec_index = -1;
7506 /* Skip over characters which were displayed via IT->dpvec. */
7507 if (it->dpvec_char_len < 0)
7508 reseat_at_next_visible_line_start (it, true);
7509 else if (it->dpvec_char_len > 0)
7511 it->len = it->dpvec_char_len;
7512 set_iterator_to_next (it, reseat_p);
7515 /* Maybe recheck faces after display vector. */
7516 if (recheck_faces)
7518 if (it->method == GET_FROM_STRING)
7519 it->stop_charpos = IT_STRING_CHARPOS (*it);
7520 else
7521 it->stop_charpos = IT_CHARPOS (*it);
7524 break;
7526 case GET_FROM_STRING:
7527 /* Current display element is a character from a Lisp string. */
7528 eassert (it->s == NULL && STRINGP (it->string));
7529 /* Don't advance past string end. These conditions are true
7530 when set_iterator_to_next is called at the end of
7531 get_next_display_element, in which case the Lisp string is
7532 already exhausted, and all we want is pop the iterator
7533 stack. */
7534 if (it->current.overlay_string_index >= 0)
7536 /* This is an overlay string, so there's no padding with
7537 spaces, and the number of characters in the string is
7538 where the string ends. */
7539 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7540 goto consider_string_end;
7542 else
7544 /* Not an overlay string. There could be padding, so test
7545 against it->end_charpos. */
7546 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7547 goto consider_string_end;
7549 if (it->cmp_it.id >= 0)
7551 /* We are delivering display elements from a composition.
7552 Update the string position past the grapheme cluster
7553 we've just processed. */
7554 if (! it->bidi_p)
7556 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7557 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7559 else
7561 int i;
7563 for (i = 0; i < it->cmp_it.nchars; i++)
7564 bidi_move_to_visually_next (&it->bidi_it);
7565 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7566 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7569 /* Did we exhaust all the grapheme clusters of this
7570 composition? */
7571 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7572 && (it->cmp_it.to < it->cmp_it.nglyphs))
7574 /* Not all the grapheme clusters were processed yet;
7575 advance to the next cluster. */
7576 it->cmp_it.from = it->cmp_it.to;
7578 else if ((it->bidi_p && it->cmp_it.reversed_p)
7579 && it->cmp_it.from > 0)
7581 /* Likewise: advance to the next cluster, but going in
7582 the reverse direction. */
7583 it->cmp_it.to = it->cmp_it.from;
7585 else
7587 /* This composition was fully processed; find the next
7588 candidate place for checking for composed
7589 characters. */
7590 /* Always limit string searches to the string length;
7591 any padding spaces are not part of the string, and
7592 there cannot be any compositions in that padding. */
7593 ptrdiff_t stop = SCHARS (it->string);
7595 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7596 stop = -1;
7597 else if (it->end_charpos < stop)
7599 /* Cf. PRECISION in reseat_to_string: we might be
7600 limited in how many of the string characters we
7601 need to deliver. */
7602 stop = it->end_charpos;
7604 composition_compute_stop_pos (&it->cmp_it,
7605 IT_STRING_CHARPOS (*it),
7606 IT_STRING_BYTEPOS (*it), stop,
7607 it->string);
7610 else
7612 if (!it->bidi_p
7613 /* If the string position is beyond string's end, it
7614 means next_element_from_string is padding the string
7615 with blanks, in which case we bypass the bidi
7616 iterator, because it cannot deal with such virtual
7617 characters. */
7618 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7620 IT_STRING_BYTEPOS (*it) += it->len;
7621 IT_STRING_CHARPOS (*it) += 1;
7623 else
7625 int prev_scan_dir = it->bidi_it.scan_dir;
7627 bidi_move_to_visually_next (&it->bidi_it);
7628 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7629 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7630 /* If the scan direction changes, we may need to update
7631 the place where to check for composed characters. */
7632 if (prev_scan_dir != it->bidi_it.scan_dir)
7634 ptrdiff_t stop = SCHARS (it->string);
7636 if (it->bidi_it.scan_dir < 0)
7637 stop = -1;
7638 else if (it->end_charpos < stop)
7639 stop = it->end_charpos;
7641 composition_compute_stop_pos (&it->cmp_it,
7642 IT_STRING_CHARPOS (*it),
7643 IT_STRING_BYTEPOS (*it), stop,
7644 it->string);
7649 consider_string_end:
7651 if (it->current.overlay_string_index >= 0)
7653 /* IT->string is an overlay string. Advance to the
7654 next, if there is one. */
7655 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7657 it->ellipsis_p = false;
7658 next_overlay_string (it);
7659 if (it->ellipsis_p)
7660 setup_for_ellipsis (it, 0);
7663 else
7665 /* IT->string is not an overlay string. If we reached
7666 its end, and there is something on IT->stack, proceed
7667 with what is on the stack. This can be either another
7668 string, this time an overlay string, or a buffer. */
7669 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7670 && it->sp > 0)
7672 pop_it (it);
7673 if (it->method == GET_FROM_STRING)
7674 goto consider_string_end;
7677 break;
7679 case GET_FROM_IMAGE:
7680 case GET_FROM_STRETCH:
7681 case GET_FROM_XWIDGET:
7683 /* The position etc with which we have to proceed are on
7684 the stack. The position may be at the end of a string,
7685 if the `display' property takes up the whole string. */
7686 eassert (it->sp > 0);
7687 pop_it (it);
7688 if (it->method == GET_FROM_STRING)
7689 goto consider_string_end;
7690 break;
7692 default:
7693 /* There are no other methods defined, so this should be a bug. */
7694 emacs_abort ();
7697 eassert (it->method != GET_FROM_STRING
7698 || (STRINGP (it->string)
7699 && IT_STRING_CHARPOS (*it) >= 0));
7702 /* Load IT's display element fields with information about the next
7703 display element which comes from a display table entry or from the
7704 result of translating a control character to one of the forms `^C'
7705 or `\003'.
7707 IT->dpvec holds the glyphs to return as characters.
7708 IT->saved_face_id holds the face id before the display vector--it
7709 is restored into IT->face_id in set_iterator_to_next. */
7711 static bool
7712 next_element_from_display_vector (struct it *it)
7714 Lisp_Object gc;
7715 int prev_face_id = it->face_id;
7716 int next_face_id;
7718 /* Precondition. */
7719 eassert (it->dpvec && it->current.dpvec_index >= 0);
7721 it->face_id = it->saved_face_id;
7723 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7724 That seemed totally bogus - so I changed it... */
7725 gc = it->dpvec[it->current.dpvec_index];
7727 if (GLYPH_CODE_P (gc))
7729 struct face *this_face, *prev_face, *next_face;
7731 it->c = GLYPH_CODE_CHAR (gc);
7732 it->len = CHAR_BYTES (it->c);
7734 /* The entry may contain a face id to use. Such a face id is
7735 the id of a Lisp face, not a realized face. A face id of
7736 zero means no face is specified. */
7737 if (it->dpvec_face_id >= 0)
7738 it->face_id = it->dpvec_face_id;
7739 else
7741 int lface_id = GLYPH_CODE_FACE (gc);
7742 if (lface_id > 0)
7743 it->face_id = merge_faces (it->f, Qt, lface_id,
7744 it->saved_face_id);
7747 /* Glyphs in the display vector could have the box face, so we
7748 need to set the related flags in the iterator, as
7749 appropriate. */
7750 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7751 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7753 /* Is this character the first character of a box-face run? */
7754 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7755 && (!prev_face
7756 || prev_face->box == FACE_NO_BOX));
7758 /* For the last character of the box-face run, we need to look
7759 either at the next glyph from the display vector, or at the
7760 face we saw before the display vector. */
7761 next_face_id = it->saved_face_id;
7762 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7764 if (it->dpvec_face_id >= 0)
7765 next_face_id = it->dpvec_face_id;
7766 else
7768 int lface_id =
7769 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7771 if (lface_id > 0)
7772 next_face_id = merge_faces (it->f, Qt, lface_id,
7773 it->saved_face_id);
7776 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7777 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7778 && (!next_face
7779 || next_face->box == FACE_NO_BOX));
7780 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7782 else
7783 /* Display table entry is invalid. Return a space. */
7784 it->c = ' ', it->len = 1;
7786 /* Don't change position and object of the iterator here. They are
7787 still the values of the character that had this display table
7788 entry or was translated, and that's what we want. */
7789 it->what = IT_CHARACTER;
7790 return true;
7793 /* Get the first element of string/buffer in the visual order, after
7794 being reseated to a new position in a string or a buffer. */
7795 static void
7796 get_visually_first_element (struct it *it)
7798 bool string_p = STRINGP (it->string) || it->s;
7799 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7800 ptrdiff_t bob = (string_p ? 0 : BEGV);
7802 if (STRINGP (it->string))
7804 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7805 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7807 else
7809 it->bidi_it.charpos = IT_CHARPOS (*it);
7810 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7813 if (it->bidi_it.charpos == eob)
7815 /* Nothing to do, but reset the FIRST_ELT flag, like
7816 bidi_paragraph_init does, because we are not going to
7817 call it. */
7818 it->bidi_it.first_elt = false;
7820 else if (it->bidi_it.charpos == bob
7821 || (!string_p
7822 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7823 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7825 /* If we are at the beginning of a line/string, we can produce
7826 the next element right away. */
7827 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7828 bidi_move_to_visually_next (&it->bidi_it);
7830 else
7832 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7834 /* We need to prime the bidi iterator starting at the line's or
7835 string's beginning, before we will be able to produce the
7836 next element. */
7837 if (string_p)
7838 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7839 else
7840 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7841 IT_BYTEPOS (*it), -1,
7842 &it->bidi_it.bytepos);
7843 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7846 /* Now return to buffer/string position where we were asked
7847 to get the next display element, and produce that. */
7848 bidi_move_to_visually_next (&it->bidi_it);
7850 while (it->bidi_it.bytepos != orig_bytepos
7851 && it->bidi_it.charpos < eob);
7854 /* Adjust IT's position information to where we ended up. */
7855 if (STRINGP (it->string))
7857 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7858 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7860 else
7862 IT_CHARPOS (*it) = it->bidi_it.charpos;
7863 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7866 if (STRINGP (it->string) || !it->s)
7868 ptrdiff_t stop, charpos, bytepos;
7870 if (STRINGP (it->string))
7872 eassert (!it->s);
7873 stop = SCHARS (it->string);
7874 if (stop > it->end_charpos)
7875 stop = it->end_charpos;
7876 charpos = IT_STRING_CHARPOS (*it);
7877 bytepos = IT_STRING_BYTEPOS (*it);
7879 else
7881 stop = it->end_charpos;
7882 charpos = IT_CHARPOS (*it);
7883 bytepos = IT_BYTEPOS (*it);
7885 if (it->bidi_it.scan_dir < 0)
7886 stop = -1;
7887 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7888 it->string);
7892 /* Load IT with the next display element from Lisp string IT->string.
7893 IT->current.string_pos is the current position within the string.
7894 If IT->current.overlay_string_index >= 0, the Lisp string is an
7895 overlay string. */
7897 static bool
7898 next_element_from_string (struct it *it)
7900 struct text_pos position;
7902 eassert (STRINGP (it->string));
7903 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7904 eassert (IT_STRING_CHARPOS (*it) >= 0);
7905 position = it->current.string_pos;
7907 /* With bidi reordering, the character to display might not be the
7908 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7909 that we were reseat()ed to a new string, whose paragraph
7910 direction is not known. */
7911 if (it->bidi_p && it->bidi_it.first_elt)
7913 get_visually_first_element (it);
7914 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7917 /* Time to check for invisible text? */
7918 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7920 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7922 if (!(!it->bidi_p
7923 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7924 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7926 /* With bidi non-linear iteration, we could find
7927 ourselves far beyond the last computed stop_charpos,
7928 with several other stop positions in between that we
7929 missed. Scan them all now, in buffer's logical
7930 order, until we find and handle the last stop_charpos
7931 that precedes our current position. */
7932 handle_stop_backwards (it, it->stop_charpos);
7933 return GET_NEXT_DISPLAY_ELEMENT (it);
7935 else
7937 if (it->bidi_p)
7939 /* Take note of the stop position we just moved
7940 across, for when we will move back across it. */
7941 it->prev_stop = it->stop_charpos;
7942 /* If we are at base paragraph embedding level, take
7943 note of the last stop position seen at this
7944 level. */
7945 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7946 it->base_level_stop = it->stop_charpos;
7948 handle_stop (it);
7950 /* Since a handler may have changed IT->method, we must
7951 recurse here. */
7952 return GET_NEXT_DISPLAY_ELEMENT (it);
7955 else if (it->bidi_p
7956 /* If we are before prev_stop, we may have overstepped
7957 on our way backwards a stop_pos, and if so, we need
7958 to handle that stop_pos. */
7959 && IT_STRING_CHARPOS (*it) < it->prev_stop
7960 /* We can sometimes back up for reasons that have nothing
7961 to do with bidi reordering. E.g., compositions. The
7962 code below is only needed when we are above the base
7963 embedding level, so test for that explicitly. */
7964 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7966 /* If we lost track of base_level_stop, we have no better
7967 place for handle_stop_backwards to start from than string
7968 beginning. This happens, e.g., when we were reseated to
7969 the previous screenful of text by vertical-motion. */
7970 if (it->base_level_stop <= 0
7971 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7972 it->base_level_stop = 0;
7973 handle_stop_backwards (it, it->base_level_stop);
7974 return GET_NEXT_DISPLAY_ELEMENT (it);
7978 if (it->current.overlay_string_index >= 0)
7980 /* Get the next character from an overlay string. In overlay
7981 strings, there is no field width or padding with spaces to
7982 do. */
7983 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7985 it->what = IT_EOB;
7986 return false;
7988 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7989 IT_STRING_BYTEPOS (*it),
7990 it->bidi_it.scan_dir < 0
7991 ? -1
7992 : SCHARS (it->string))
7993 && next_element_from_composition (it))
7995 return true;
7997 else if (STRING_MULTIBYTE (it->string))
7999 const unsigned char *s = (SDATA (it->string)
8000 + IT_STRING_BYTEPOS (*it));
8001 it->c = string_char_and_length (s, &it->len);
8003 else
8005 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8006 it->len = 1;
8009 else
8011 /* Get the next character from a Lisp string that is not an
8012 overlay string. Such strings come from the mode line, for
8013 example. We may have to pad with spaces, or truncate the
8014 string. See also next_element_from_c_string. */
8015 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8017 it->what = IT_EOB;
8018 return false;
8020 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8022 /* Pad with spaces. */
8023 it->c = ' ', it->len = 1;
8024 CHARPOS (position) = BYTEPOS (position) = -1;
8026 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8027 IT_STRING_BYTEPOS (*it),
8028 it->bidi_it.scan_dir < 0
8029 ? -1
8030 : it->string_nchars)
8031 && next_element_from_composition (it))
8033 return true;
8035 else if (STRING_MULTIBYTE (it->string))
8037 const unsigned char *s = (SDATA (it->string)
8038 + IT_STRING_BYTEPOS (*it));
8039 it->c = string_char_and_length (s, &it->len);
8041 else
8043 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8044 it->len = 1;
8048 /* Record what we have and where it came from. */
8049 it->what = IT_CHARACTER;
8050 it->object = it->string;
8051 it->position = position;
8052 return true;
8056 /* Load IT with next display element from C string IT->s.
8057 IT->string_nchars is the maximum number of characters to return
8058 from the string. IT->end_charpos may be greater than
8059 IT->string_nchars when this function is called, in which case we
8060 may have to return padding spaces. Value is false if end of string
8061 reached, including padding spaces. */
8063 static bool
8064 next_element_from_c_string (struct it *it)
8066 bool success_p = true;
8068 eassert (it->s);
8069 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8070 it->what = IT_CHARACTER;
8071 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8072 it->object = make_number (0);
8074 /* With bidi reordering, the character to display might not be the
8075 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8076 we were reseated to a new string, whose paragraph direction is
8077 not known. */
8078 if (it->bidi_p && it->bidi_it.first_elt)
8079 get_visually_first_element (it);
8081 /* IT's position can be greater than IT->string_nchars in case a
8082 field width or precision has been specified when the iterator was
8083 initialized. */
8084 if (IT_CHARPOS (*it) >= it->end_charpos)
8086 /* End of the game. */
8087 it->what = IT_EOB;
8088 success_p = false;
8090 else if (IT_CHARPOS (*it) >= it->string_nchars)
8092 /* Pad with spaces. */
8093 it->c = ' ', it->len = 1;
8094 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8096 else if (it->multibyte_p)
8097 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8098 else
8099 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8101 return success_p;
8105 /* Set up IT to return characters from an ellipsis, if appropriate.
8106 The definition of the ellipsis glyphs may come from a display table
8107 entry. This function fills IT with the first glyph from the
8108 ellipsis if an ellipsis is to be displayed. */
8110 static bool
8111 next_element_from_ellipsis (struct it *it)
8113 if (it->selective_display_ellipsis_p)
8114 setup_for_ellipsis (it, it->len);
8115 else
8117 /* The face at the current position may be different from the
8118 face we find after the invisible text. Remember what it
8119 was in IT->saved_face_id, and signal that it's there by
8120 setting face_before_selective_p. */
8121 it->saved_face_id = it->face_id;
8122 it->method = GET_FROM_BUFFER;
8123 it->object = it->w->contents;
8124 reseat_at_next_visible_line_start (it, true);
8125 it->face_before_selective_p = true;
8128 return GET_NEXT_DISPLAY_ELEMENT (it);
8132 /* Deliver an image display element. The iterator IT is already
8133 filled with image information (done in handle_display_prop). Value
8134 is always true. */
8137 static bool
8138 next_element_from_image (struct it *it)
8140 it->what = IT_IMAGE;
8141 return true;
8144 static bool
8145 next_element_from_xwidget (struct it *it)
8147 it->what = IT_XWIDGET;
8148 return true;
8152 /* Fill iterator IT with next display element from a stretch glyph
8153 property. IT->object is the value of the text property. Value is
8154 always true. */
8156 static bool
8157 next_element_from_stretch (struct it *it)
8159 it->what = IT_STRETCH;
8160 return true;
8163 /* Scan backwards from IT's current position until we find a stop
8164 position, or until BEGV. This is called when we find ourself
8165 before both the last known prev_stop and base_level_stop while
8166 reordering bidirectional text. */
8168 static void
8169 compute_stop_pos_backwards (struct it *it)
8171 const int SCAN_BACK_LIMIT = 1000;
8172 struct text_pos pos;
8173 struct display_pos save_current = it->current;
8174 struct text_pos save_position = it->position;
8175 ptrdiff_t charpos = IT_CHARPOS (*it);
8176 ptrdiff_t where_we_are = charpos;
8177 ptrdiff_t save_stop_pos = it->stop_charpos;
8178 ptrdiff_t save_end_pos = it->end_charpos;
8180 eassert (NILP (it->string) && !it->s);
8181 eassert (it->bidi_p);
8182 it->bidi_p = false;
8185 it->end_charpos = min (charpos + 1, ZV);
8186 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8187 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8188 reseat_1 (it, pos, false);
8189 compute_stop_pos (it);
8190 /* We must advance forward, right? */
8191 if (it->stop_charpos <= charpos)
8192 emacs_abort ();
8194 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8196 if (it->stop_charpos <= where_we_are)
8197 it->prev_stop = it->stop_charpos;
8198 else
8199 it->prev_stop = BEGV;
8200 it->bidi_p = true;
8201 it->current = save_current;
8202 it->position = save_position;
8203 it->stop_charpos = save_stop_pos;
8204 it->end_charpos = save_end_pos;
8207 /* Scan forward from CHARPOS in the current buffer/string, until we
8208 find a stop position > current IT's position. Then handle the stop
8209 position before that. This is called when we bump into a stop
8210 position while reordering bidirectional text. CHARPOS should be
8211 the last previously processed stop_pos (or BEGV/0, if none were
8212 processed yet) whose position is less that IT's current
8213 position. */
8215 static void
8216 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8218 bool bufp = !STRINGP (it->string);
8219 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8220 struct display_pos save_current = it->current;
8221 struct text_pos save_position = it->position;
8222 struct text_pos pos1;
8223 ptrdiff_t next_stop;
8225 /* Scan in strict logical order. */
8226 eassert (it->bidi_p);
8227 it->bidi_p = false;
8230 it->prev_stop = charpos;
8231 if (bufp)
8233 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8234 reseat_1 (it, pos1, false);
8236 else
8237 it->current.string_pos = string_pos (charpos, it->string);
8238 compute_stop_pos (it);
8239 /* We must advance forward, right? */
8240 if (it->stop_charpos <= it->prev_stop)
8241 emacs_abort ();
8242 charpos = it->stop_charpos;
8244 while (charpos <= where_we_are);
8246 it->bidi_p = true;
8247 it->current = save_current;
8248 it->position = save_position;
8249 next_stop = it->stop_charpos;
8250 it->stop_charpos = it->prev_stop;
8251 handle_stop (it);
8252 it->stop_charpos = next_stop;
8255 /* Load IT with the next display element from current_buffer. Value
8256 is false if end of buffer reached. IT->stop_charpos is the next
8257 position at which to stop and check for text properties or buffer
8258 end. */
8260 static bool
8261 next_element_from_buffer (struct it *it)
8263 bool success_p = true;
8265 eassert (IT_CHARPOS (*it) >= BEGV);
8266 eassert (NILP (it->string) && !it->s);
8267 eassert (!it->bidi_p
8268 || (EQ (it->bidi_it.string.lstring, Qnil)
8269 && it->bidi_it.string.s == NULL));
8271 /* With bidi reordering, the character to display might not be the
8272 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8273 we were reseat()ed to a new buffer position, which is potentially
8274 a different paragraph. */
8275 if (it->bidi_p && it->bidi_it.first_elt)
8277 get_visually_first_element (it);
8278 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8281 if (IT_CHARPOS (*it) >= it->stop_charpos)
8283 if (IT_CHARPOS (*it) >= it->end_charpos)
8285 bool overlay_strings_follow_p;
8287 /* End of the game, except when overlay strings follow that
8288 haven't been returned yet. */
8289 if (it->overlay_strings_at_end_processed_p)
8290 overlay_strings_follow_p = false;
8291 else
8293 it->overlay_strings_at_end_processed_p = true;
8294 overlay_strings_follow_p = get_overlay_strings (it, 0);
8297 if (overlay_strings_follow_p)
8298 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8299 else
8301 it->what = IT_EOB;
8302 it->position = it->current.pos;
8303 success_p = false;
8306 else if (!(!it->bidi_p
8307 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8308 || IT_CHARPOS (*it) == it->stop_charpos))
8310 /* With bidi non-linear iteration, we could find ourselves
8311 far beyond the last computed stop_charpos, with several
8312 other stop positions in between that we missed. Scan
8313 them all now, in buffer's logical order, until we find
8314 and handle the last stop_charpos that precedes our
8315 current position. */
8316 handle_stop_backwards (it, it->stop_charpos);
8317 it->ignore_overlay_strings_at_pos_p = false;
8318 return GET_NEXT_DISPLAY_ELEMENT (it);
8320 else
8322 if (it->bidi_p)
8324 /* Take note of the stop position we just moved across,
8325 for when we will move back across it. */
8326 it->prev_stop = it->stop_charpos;
8327 /* If we are at base paragraph embedding level, take
8328 note of the last stop position seen at this
8329 level. */
8330 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8331 it->base_level_stop = it->stop_charpos;
8333 handle_stop (it);
8334 it->ignore_overlay_strings_at_pos_p = false;
8335 return GET_NEXT_DISPLAY_ELEMENT (it);
8338 else if (it->bidi_p
8339 /* If we are before prev_stop, we may have overstepped on
8340 our way backwards a stop_pos, and if so, we need to
8341 handle that stop_pos. */
8342 && IT_CHARPOS (*it) < it->prev_stop
8343 /* We can sometimes back up for reasons that have nothing
8344 to do with bidi reordering. E.g., compositions. The
8345 code below is only needed when we are above the base
8346 embedding level, so test for that explicitly. */
8347 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8349 if (it->base_level_stop <= 0
8350 || IT_CHARPOS (*it) < it->base_level_stop)
8352 /* If we lost track of base_level_stop, we need to find
8353 prev_stop by looking backwards. This happens, e.g., when
8354 we were reseated to the previous screenful of text by
8355 vertical-motion. */
8356 it->base_level_stop = BEGV;
8357 compute_stop_pos_backwards (it);
8358 handle_stop_backwards (it, it->prev_stop);
8360 else
8361 handle_stop_backwards (it, it->base_level_stop);
8362 it->ignore_overlay_strings_at_pos_p = false;
8363 return GET_NEXT_DISPLAY_ELEMENT (it);
8365 else
8367 /* No face changes, overlays etc. in sight, so just return a
8368 character from current_buffer. */
8369 unsigned char *p;
8370 ptrdiff_t stop;
8372 /* We moved to the next buffer position, so any info about
8373 previously seen overlays is no longer valid. */
8374 it->ignore_overlay_strings_at_pos_p = false;
8376 /* Maybe run the redisplay end trigger hook. Performance note:
8377 This doesn't seem to cost measurable time. */
8378 if (it->redisplay_end_trigger_charpos
8379 && it->glyph_row
8380 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8381 run_redisplay_end_trigger_hook (it);
8383 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8384 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8385 stop)
8386 && next_element_from_composition (it))
8388 return true;
8391 /* Get the next character, maybe multibyte. */
8392 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8393 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8394 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8395 else
8396 it->c = *p, it->len = 1;
8398 /* Record what we have and where it came from. */
8399 it->what = IT_CHARACTER;
8400 it->object = it->w->contents;
8401 it->position = it->current.pos;
8403 /* Normally we return the character found above, except when we
8404 really want to return an ellipsis for selective display. */
8405 if (it->selective)
8407 if (it->c == '\n')
8409 /* A value of selective > 0 means hide lines indented more
8410 than that number of columns. */
8411 if (it->selective > 0
8412 && IT_CHARPOS (*it) + 1 < ZV
8413 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8414 IT_BYTEPOS (*it) + 1,
8415 it->selective))
8417 success_p = next_element_from_ellipsis (it);
8418 it->dpvec_char_len = -1;
8421 else if (it->c == '\r' && it->selective == -1)
8423 /* A value of selective == -1 means that everything from the
8424 CR to the end of the line is invisible, with maybe an
8425 ellipsis displayed for it. */
8426 success_p = next_element_from_ellipsis (it);
8427 it->dpvec_char_len = -1;
8432 /* Value is false if end of buffer reached. */
8433 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8434 return success_p;
8438 /* Run the redisplay end trigger hook for IT. */
8440 static void
8441 run_redisplay_end_trigger_hook (struct it *it)
8443 /* IT->glyph_row should be non-null, i.e. we should be actually
8444 displaying something, or otherwise we should not run the hook. */
8445 eassert (it->glyph_row);
8447 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8448 it->redisplay_end_trigger_charpos = 0;
8450 /* Since we are *trying* to run these functions, don't try to run
8451 them again, even if they get an error. */
8452 wset_redisplay_end_trigger (it->w, Qnil);
8453 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8454 make_number (charpos));
8456 /* Notice if it changed the face of the character we are on. */
8457 handle_face_prop (it);
8461 /* Deliver a composition display element. Unlike the other
8462 next_element_from_XXX, this function is not registered in the array
8463 get_next_element[]. It is called from next_element_from_buffer and
8464 next_element_from_string when necessary. */
8466 static bool
8467 next_element_from_composition (struct it *it)
8469 it->what = IT_COMPOSITION;
8470 it->len = it->cmp_it.nbytes;
8471 if (STRINGP (it->string))
8473 if (it->c < 0)
8475 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8476 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8477 return false;
8479 it->position = it->current.string_pos;
8480 it->object = it->string;
8481 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8482 IT_STRING_BYTEPOS (*it), it->string);
8484 else
8486 if (it->c < 0)
8488 IT_CHARPOS (*it) += it->cmp_it.nchars;
8489 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8490 if (it->bidi_p)
8492 if (it->bidi_it.new_paragraph)
8493 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8494 false);
8495 /* Resync the bidi iterator with IT's new position.
8496 FIXME: this doesn't support bidirectional text. */
8497 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8498 bidi_move_to_visually_next (&it->bidi_it);
8500 return false;
8502 it->position = it->current.pos;
8503 it->object = it->w->contents;
8504 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8505 IT_BYTEPOS (*it), Qnil);
8507 return true;
8512 /***********************************************************************
8513 Moving an iterator without producing glyphs
8514 ***********************************************************************/
8516 /* Check if iterator is at a position corresponding to a valid buffer
8517 position after some move_it_ call. */
8519 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8520 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8523 /* Move iterator IT to a specified buffer or X position within one
8524 line on the display without producing glyphs.
8526 OP should be a bit mask including some or all of these bits:
8527 MOVE_TO_X: Stop upon reaching x-position TO_X.
8528 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8529 Regardless of OP's value, stop upon reaching the end of the display line.
8531 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8532 This means, in particular, that TO_X includes window's horizontal
8533 scroll amount.
8535 The return value has several possible values that
8536 say what condition caused the scan to stop:
8538 MOVE_POS_MATCH_OR_ZV
8539 - when TO_POS or ZV was reached.
8541 MOVE_X_REACHED
8542 -when TO_X was reached before TO_POS or ZV were reached.
8544 MOVE_LINE_CONTINUED
8545 - when we reached the end of the display area and the line must
8546 be continued.
8548 MOVE_LINE_TRUNCATED
8549 - when we reached the end of the display area and the line is
8550 truncated.
8552 MOVE_NEWLINE_OR_CR
8553 - when we stopped at a line end, i.e. a newline or a CR and selective
8554 display is on. */
8556 static enum move_it_result
8557 move_it_in_display_line_to (struct it *it,
8558 ptrdiff_t to_charpos, int to_x,
8559 enum move_operation_enum op)
8561 enum move_it_result result = MOVE_UNDEFINED;
8562 struct glyph_row *saved_glyph_row;
8563 struct it wrap_it, atpos_it, atx_it, ppos_it;
8564 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8565 void *ppos_data = NULL;
8566 bool may_wrap = false;
8567 enum it_method prev_method = it->method;
8568 ptrdiff_t closest_pos UNINIT;
8569 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8570 bool saw_smaller_pos = prev_pos < to_charpos;
8572 /* Don't produce glyphs in produce_glyphs. */
8573 saved_glyph_row = it->glyph_row;
8574 it->glyph_row = NULL;
8576 /* Use wrap_it to save a copy of IT wherever a word wrap could
8577 occur. Use atpos_it to save a copy of IT at the desired buffer
8578 position, if found, so that we can scan ahead and check if the
8579 word later overshoots the window edge. Use atx_it similarly, for
8580 pixel positions. */
8581 wrap_it.sp = -1;
8582 atpos_it.sp = -1;
8583 atx_it.sp = -1;
8585 /* Use ppos_it under bidi reordering to save a copy of IT for the
8586 initial position. We restore that position in IT when we have
8587 scanned the entire display line without finding a match for
8588 TO_CHARPOS and all the character positions are greater than
8589 TO_CHARPOS. We then restart the scan from the initial position,
8590 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8591 the closest to TO_CHARPOS. */
8592 if (it->bidi_p)
8594 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8596 SAVE_IT (ppos_it, *it, ppos_data);
8597 closest_pos = IT_CHARPOS (*it);
8599 else
8600 closest_pos = ZV;
8603 #define BUFFER_POS_REACHED_P() \
8604 ((op & MOVE_TO_POS) != 0 \
8605 && BUFFERP (it->object) \
8606 && (IT_CHARPOS (*it) == to_charpos \
8607 || ((!it->bidi_p \
8608 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8609 && IT_CHARPOS (*it) > to_charpos) \
8610 || (it->what == IT_COMPOSITION \
8611 && ((IT_CHARPOS (*it) > to_charpos \
8612 && to_charpos >= it->cmp_it.charpos) \
8613 || (IT_CHARPOS (*it) < to_charpos \
8614 && to_charpos <= it->cmp_it.charpos)))) \
8615 && (it->method == GET_FROM_BUFFER \
8616 || (it->method == GET_FROM_DISPLAY_VECTOR \
8617 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8619 /* If there's a line-/wrap-prefix, handle it. */
8620 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8621 handle_line_prefix (it);
8623 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8624 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8626 while (true)
8628 int x, i, ascent = 0, descent = 0;
8630 /* Utility macro to reset an iterator with x, ascent, and descent. */
8631 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8632 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8633 (IT)->max_descent = descent)
8635 /* Stop if we move beyond TO_CHARPOS (after an image or a
8636 display string or stretch glyph). */
8637 if ((op & MOVE_TO_POS) != 0
8638 && BUFFERP (it->object)
8639 && it->method == GET_FROM_BUFFER
8640 && (((!it->bidi_p
8641 /* When the iterator is at base embedding level, we
8642 are guaranteed that characters are delivered for
8643 display in strictly increasing order of their
8644 buffer positions. */
8645 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8646 && IT_CHARPOS (*it) > to_charpos)
8647 || (it->bidi_p
8648 && (prev_method == GET_FROM_IMAGE
8649 || prev_method == GET_FROM_STRETCH
8650 || prev_method == GET_FROM_STRING)
8651 /* Passed TO_CHARPOS from left to right. */
8652 && ((prev_pos < to_charpos
8653 && IT_CHARPOS (*it) > to_charpos)
8654 /* Passed TO_CHARPOS from right to left. */
8655 || (prev_pos > to_charpos
8656 && IT_CHARPOS (*it) < to_charpos)))))
8658 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8660 result = MOVE_POS_MATCH_OR_ZV;
8661 break;
8663 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8664 /* If wrap_it is valid, the current position might be in a
8665 word that is wrapped. So, save the iterator in
8666 atpos_it and continue to see if wrapping happens. */
8667 SAVE_IT (atpos_it, *it, atpos_data);
8670 /* Stop when ZV reached.
8671 We used to stop here when TO_CHARPOS reached as well, but that is
8672 too soon if this glyph does not fit on this line. So we handle it
8673 explicitly below. */
8674 if (!get_next_display_element (it))
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8680 if (it->line_wrap == TRUNCATE)
8682 if (BUFFER_POS_REACHED_P ())
8684 result = MOVE_POS_MATCH_OR_ZV;
8685 break;
8688 else
8690 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8692 if (IT_DISPLAYING_WHITESPACE (it))
8693 may_wrap = true;
8694 else if (may_wrap)
8696 /* We have reached a glyph that follows one or more
8697 whitespace characters. If the position is
8698 already found, we are done. */
8699 if (atpos_it.sp >= 0)
8701 RESTORE_IT (it, &atpos_it, atpos_data);
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 goto done;
8705 if (atx_it.sp >= 0)
8707 RESTORE_IT (it, &atx_it, atx_data);
8708 result = MOVE_X_REACHED;
8709 goto done;
8711 /* Otherwise, we can wrap here. */
8712 SAVE_IT (wrap_it, *it, wrap_data);
8713 may_wrap = false;
8718 /* Remember the line height for the current line, in case
8719 the next element doesn't fit on the line. */
8720 ascent = it->max_ascent;
8721 descent = it->max_descent;
8723 /* The call to produce_glyphs will get the metrics of the
8724 display element IT is loaded with. Record the x-position
8725 before this display element, in case it doesn't fit on the
8726 line. */
8727 x = it->current_x;
8729 PRODUCE_GLYPHS (it);
8731 if (it->area != TEXT_AREA)
8733 prev_method = it->method;
8734 if (it->method == GET_FROM_BUFFER)
8735 prev_pos = IT_CHARPOS (*it);
8736 set_iterator_to_next (it, true);
8737 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8738 SET_TEXT_POS (this_line_min_pos,
8739 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8740 if (it->bidi_p
8741 && (op & MOVE_TO_POS)
8742 && IT_CHARPOS (*it) > to_charpos
8743 && IT_CHARPOS (*it) < closest_pos)
8744 closest_pos = IT_CHARPOS (*it);
8745 continue;
8748 /* The number of glyphs we get back in IT->nglyphs will normally
8749 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8750 character on a terminal frame, or (iii) a line end. For the
8751 second case, IT->nglyphs - 1 padding glyphs will be present.
8752 (On X frames, there is only one glyph produced for a
8753 composite character.)
8755 The behavior implemented below means, for continuation lines,
8756 that as many spaces of a TAB as fit on the current line are
8757 displayed there. For terminal frames, as many glyphs of a
8758 multi-glyph character are displayed in the current line, too.
8759 This is what the old redisplay code did, and we keep it that
8760 way. Under X, the whole shape of a complex character must
8761 fit on the line or it will be completely displayed in the
8762 next line.
8764 Note that both for tabs and padding glyphs, all glyphs have
8765 the same width. */
8766 if (it->nglyphs)
8768 /* More than one glyph or glyph doesn't fit on line. All
8769 glyphs have the same width. */
8770 int single_glyph_width = it->pixel_width / it->nglyphs;
8771 int new_x;
8772 int x_before_this_char = x;
8773 int hpos_before_this_char = it->hpos;
8775 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8777 new_x = x + single_glyph_width;
8779 /* We want to leave anything reaching TO_X to the caller. */
8780 if ((op & MOVE_TO_X) && new_x > to_x)
8782 if (BUFFER_POS_REACHED_P ())
8784 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8785 goto buffer_pos_reached;
8786 if (atpos_it.sp < 0)
8788 SAVE_IT (atpos_it, *it, atpos_data);
8789 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8792 else
8794 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8796 it->current_x = x;
8797 result = MOVE_X_REACHED;
8798 break;
8800 if (atx_it.sp < 0)
8802 SAVE_IT (atx_it, *it, atx_data);
8803 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8808 if (/* Lines are continued. */
8809 it->line_wrap != TRUNCATE
8810 && (/* And glyph doesn't fit on the line. */
8811 new_x > it->last_visible_x
8812 /* Or it fits exactly and we're on a window
8813 system frame. */
8814 || (new_x == it->last_visible_x
8815 && FRAME_WINDOW_P (it->f)
8816 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8817 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8818 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8820 bool moved_forward = false;
8822 if (/* IT->hpos == 0 means the very first glyph
8823 doesn't fit on the line, e.g. a wide image. */
8824 it->hpos == 0
8825 || (new_x == it->last_visible_x
8826 && FRAME_WINDOW_P (it->f)))
8828 ++it->hpos;
8829 it->current_x = new_x;
8831 /* The character's last glyph just barely fits
8832 in this row. */
8833 if (i == it->nglyphs - 1)
8835 /* If this is the destination position,
8836 return a position *before* it in this row,
8837 now that we know it fits in this row. */
8838 if (BUFFER_POS_REACHED_P ())
8840 bool can_wrap = true;
8842 /* If we are at a whitespace character
8843 that barely fits on this screen line,
8844 but the next character is also
8845 whitespace, we cannot wrap here. */
8846 if (it->line_wrap == WORD_WRAP
8847 && wrap_it.sp >= 0
8848 && may_wrap
8849 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8851 struct it tem_it;
8852 void *tem_data = NULL;
8854 SAVE_IT (tem_it, *it, tem_data);
8855 set_iterator_to_next (it, true);
8856 if (get_next_display_element (it)
8857 && IT_DISPLAYING_WHITESPACE (it))
8858 can_wrap = false;
8859 RESTORE_IT (it, &tem_it, tem_data);
8861 if (it->line_wrap != WORD_WRAP
8862 || wrap_it.sp < 0
8863 /* If we've just found whitespace
8864 where we can wrap, effectively
8865 ignore the previous wrap point --
8866 it is no longer relevant, but we
8867 won't have an opportunity to
8868 update it, since we've reached
8869 the edge of this screen line. */
8870 || (may_wrap && can_wrap
8871 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8873 it->hpos = hpos_before_this_char;
8874 it->current_x = x_before_this_char;
8875 result = MOVE_POS_MATCH_OR_ZV;
8876 break;
8878 if (it->line_wrap == WORD_WRAP
8879 && atpos_it.sp < 0)
8881 SAVE_IT (atpos_it, *it, atpos_data);
8882 atpos_it.current_x = x_before_this_char;
8883 atpos_it.hpos = hpos_before_this_char;
8887 prev_method = it->method;
8888 if (it->method == GET_FROM_BUFFER)
8889 prev_pos = IT_CHARPOS (*it);
8890 set_iterator_to_next (it, true);
8891 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8892 SET_TEXT_POS (this_line_min_pos,
8893 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8894 /* On graphical terminals, newlines may
8895 "overflow" into the fringe if
8896 overflow-newline-into-fringe is non-nil.
8897 On text terminals, and on graphical
8898 terminals with no right margin, newlines
8899 may overflow into the last glyph on the
8900 display line.*/
8901 if (!FRAME_WINDOW_P (it->f)
8902 || ((it->bidi_p
8903 && it->bidi_it.paragraph_dir == R2L)
8904 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8905 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8906 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8908 if (!get_next_display_element (it))
8910 result = MOVE_POS_MATCH_OR_ZV;
8911 break;
8913 moved_forward = true;
8914 if (BUFFER_POS_REACHED_P ())
8916 if (ITERATOR_AT_END_OF_LINE_P (it))
8917 result = MOVE_POS_MATCH_OR_ZV;
8918 else
8919 result = MOVE_LINE_CONTINUED;
8920 break;
8922 if (ITERATOR_AT_END_OF_LINE_P (it)
8923 && (it->line_wrap != WORD_WRAP
8924 || wrap_it.sp < 0
8925 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8927 result = MOVE_NEWLINE_OR_CR;
8928 break;
8933 else
8934 IT_RESET_X_ASCENT_DESCENT (it);
8936 /* If the screen line ends with whitespace, and we
8937 are under word-wrap, don't use wrap_it: it is no
8938 longer relevant, but we won't have an opportunity
8939 to update it, since we are done with this screen
8940 line. */
8941 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8942 /* If the character after the one which set the
8943 may_wrap flag is also whitespace, we can't
8944 wrap here, since the screen line cannot be
8945 wrapped in the middle of whitespace.
8946 Therefore, wrap_it _is_ relevant in that
8947 case. */
8948 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8950 /* If we've found TO_X, go back there, as we now
8951 know the last word fits on this screen line. */
8952 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8953 && atx_it.sp >= 0)
8955 RESTORE_IT (it, &atx_it, atx_data);
8956 atpos_it.sp = -1;
8957 atx_it.sp = -1;
8958 result = MOVE_X_REACHED;
8959 break;
8962 else if (wrap_it.sp >= 0)
8964 RESTORE_IT (it, &wrap_it, wrap_data);
8965 atpos_it.sp = -1;
8966 atx_it.sp = -1;
8969 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8970 IT_CHARPOS (*it)));
8971 result = MOVE_LINE_CONTINUED;
8972 break;
8975 if (BUFFER_POS_REACHED_P ())
8977 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8978 goto buffer_pos_reached;
8979 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8981 SAVE_IT (atpos_it, *it, atpos_data);
8982 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8986 if (new_x > it->first_visible_x)
8988 /* Glyph is visible. Increment number of glyphs that
8989 would be displayed. */
8990 ++it->hpos;
8994 if (result != MOVE_UNDEFINED)
8995 break;
8997 else if (BUFFER_POS_REACHED_P ())
8999 buffer_pos_reached:
9000 IT_RESET_X_ASCENT_DESCENT (it);
9001 result = MOVE_POS_MATCH_OR_ZV;
9002 break;
9004 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9006 /* Stop when TO_X specified and reached. This check is
9007 necessary here because of lines consisting of a line end,
9008 only. The line end will not produce any glyphs and we
9009 would never get MOVE_X_REACHED. */
9010 eassert (it->nglyphs == 0);
9011 result = MOVE_X_REACHED;
9012 break;
9015 /* Is this a line end? If yes, we're done. */
9016 if (ITERATOR_AT_END_OF_LINE_P (it))
9018 /* If we are past TO_CHARPOS, but never saw any character
9019 positions smaller than TO_CHARPOS, return
9020 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9021 did. */
9022 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9024 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9026 if (closest_pos < ZV)
9028 RESTORE_IT (it, &ppos_it, ppos_data);
9029 /* Don't recurse if closest_pos is equal to
9030 to_charpos, since we have just tried that. */
9031 if (closest_pos != to_charpos)
9032 move_it_in_display_line_to (it, closest_pos, -1,
9033 MOVE_TO_POS);
9034 result = MOVE_POS_MATCH_OR_ZV;
9036 else
9037 goto buffer_pos_reached;
9039 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9040 && IT_CHARPOS (*it) > to_charpos)
9041 goto buffer_pos_reached;
9042 else
9043 result = MOVE_NEWLINE_OR_CR;
9045 else
9046 result = MOVE_NEWLINE_OR_CR;
9047 /* If we've processed the newline, make sure this flag is
9048 reset, as it must only be set when the newline itself is
9049 processed. */
9050 if (result == MOVE_NEWLINE_OR_CR)
9051 it->constrain_row_ascent_descent_p = false;
9052 break;
9055 prev_method = it->method;
9056 if (it->method == GET_FROM_BUFFER)
9057 prev_pos = IT_CHARPOS (*it);
9058 /* The current display element has been consumed. Advance
9059 to the next. */
9060 set_iterator_to_next (it, true);
9061 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9062 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9063 if (IT_CHARPOS (*it) < to_charpos)
9064 saw_smaller_pos = true;
9065 if (it->bidi_p
9066 && (op & MOVE_TO_POS)
9067 && IT_CHARPOS (*it) >= to_charpos
9068 && IT_CHARPOS (*it) < closest_pos)
9069 closest_pos = IT_CHARPOS (*it);
9071 /* Stop if lines are truncated and IT's current x-position is
9072 past the right edge of the window now. */
9073 if (it->line_wrap == TRUNCATE
9074 && it->current_x >= it->last_visible_x)
9076 if (!FRAME_WINDOW_P (it->f)
9077 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9078 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9079 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9080 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9082 bool at_eob_p = false;
9084 if ((at_eob_p = !get_next_display_element (it))
9085 || BUFFER_POS_REACHED_P ()
9086 /* If we are past TO_CHARPOS, but never saw any
9087 character positions smaller than TO_CHARPOS,
9088 return MOVE_POS_MATCH_OR_ZV, like the
9089 unidirectional display did. */
9090 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9091 && !saw_smaller_pos
9092 && IT_CHARPOS (*it) > to_charpos))
9094 if (it->bidi_p
9095 && !BUFFER_POS_REACHED_P ()
9096 && !at_eob_p && closest_pos < ZV)
9098 RESTORE_IT (it, &ppos_it, ppos_data);
9099 if (closest_pos != to_charpos)
9100 move_it_in_display_line_to (it, closest_pos, -1,
9101 MOVE_TO_POS);
9103 result = MOVE_POS_MATCH_OR_ZV;
9104 break;
9106 if (ITERATOR_AT_END_OF_LINE_P (it))
9108 result = MOVE_NEWLINE_OR_CR;
9109 break;
9112 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9113 && !saw_smaller_pos
9114 && IT_CHARPOS (*it) > to_charpos)
9116 if (closest_pos < ZV)
9118 RESTORE_IT (it, &ppos_it, ppos_data);
9119 if (closest_pos != to_charpos)
9120 move_it_in_display_line_to (it, closest_pos, -1,
9121 MOVE_TO_POS);
9123 result = MOVE_POS_MATCH_OR_ZV;
9124 break;
9126 result = MOVE_LINE_TRUNCATED;
9127 break;
9129 #undef IT_RESET_X_ASCENT_DESCENT
9132 #undef BUFFER_POS_REACHED_P
9134 /* If we scanned beyond TO_POS, restore the saved iterator either to
9135 the wrap point (if found), or to atpos/atx location. We decide which
9136 data to use to restore the saved iterator state by their X coordinates,
9137 since buffer positions might increase non-monotonically with screen
9138 coordinates due to bidi reordering. */
9139 if (result == MOVE_LINE_CONTINUED
9140 && it->line_wrap == WORD_WRAP
9141 && wrap_it.sp >= 0
9142 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9143 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9144 RESTORE_IT (it, &wrap_it, wrap_data);
9145 else if (atpos_it.sp >= 0)
9146 RESTORE_IT (it, &atpos_it, atpos_data);
9147 else if (atx_it.sp >= 0)
9148 RESTORE_IT (it, &atx_it, atx_data);
9150 done:
9152 if (atpos_data)
9153 bidi_unshelve_cache (atpos_data, true);
9154 if (atx_data)
9155 bidi_unshelve_cache (atx_data, true);
9156 if (wrap_data)
9157 bidi_unshelve_cache (wrap_data, true);
9158 if (ppos_data)
9159 bidi_unshelve_cache (ppos_data, true);
9161 /* Restore the iterator settings altered at the beginning of this
9162 function. */
9163 it->glyph_row = saved_glyph_row;
9164 return result;
9167 /* For external use. */
9168 void
9169 move_it_in_display_line (struct it *it,
9170 ptrdiff_t to_charpos, int to_x,
9171 enum move_operation_enum op)
9173 if (it->line_wrap == WORD_WRAP
9174 && (op & MOVE_TO_X))
9176 struct it save_it;
9177 void *save_data = NULL;
9178 int skip;
9180 SAVE_IT (save_it, *it, save_data);
9181 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9182 /* When word-wrap is on, TO_X may lie past the end
9183 of a wrapped line. Then it->current is the
9184 character on the next line, so backtrack to the
9185 space before the wrap point. */
9186 if (skip == MOVE_LINE_CONTINUED)
9188 int prev_x = max (it->current_x - 1, 0);
9189 RESTORE_IT (it, &save_it, save_data);
9190 move_it_in_display_line_to
9191 (it, -1, prev_x, MOVE_TO_X);
9193 else
9194 bidi_unshelve_cache (save_data, true);
9196 else
9197 move_it_in_display_line_to (it, to_charpos, to_x, op);
9201 /* Move IT forward until it satisfies one or more of the criteria in
9202 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9204 OP is a bit-mask that specifies where to stop, and in particular,
9205 which of those four position arguments makes a difference. See the
9206 description of enum move_operation_enum.
9208 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9209 screen line, this function will set IT to the next position that is
9210 displayed to the right of TO_CHARPOS on the screen.
9212 Return the maximum pixel length of any line scanned but never more
9213 than it.last_visible_x. */
9216 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9218 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9219 int line_height, line_start_x = 0, reached = 0;
9220 int max_current_x = 0;
9221 void *backup_data = NULL;
9223 for (;;)
9225 if (op & MOVE_TO_VPOS)
9227 /* If no TO_CHARPOS and no TO_X specified, stop at the
9228 start of the line TO_VPOS. */
9229 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9231 if (it->vpos == to_vpos)
9233 reached = 1;
9234 break;
9236 else
9237 skip = move_it_in_display_line_to (it, -1, -1, 0);
9239 else
9241 /* TO_VPOS >= 0 means stop at TO_X in the line at
9242 TO_VPOS, or at TO_POS, whichever comes first. */
9243 if (it->vpos == to_vpos)
9245 reached = 2;
9246 break;
9249 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9251 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9253 reached = 3;
9254 break;
9256 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9258 /* We have reached TO_X but not in the line we want. */
9259 skip = move_it_in_display_line_to (it, to_charpos,
9260 -1, MOVE_TO_POS);
9261 if (skip == MOVE_POS_MATCH_OR_ZV)
9263 reached = 4;
9264 break;
9269 else if (op & MOVE_TO_Y)
9271 struct it it_backup;
9273 if (it->line_wrap == WORD_WRAP)
9274 SAVE_IT (it_backup, *it, backup_data);
9276 /* TO_Y specified means stop at TO_X in the line containing
9277 TO_Y---or at TO_CHARPOS if this is reached first. The
9278 problem is that we can't really tell whether the line
9279 contains TO_Y before we have completely scanned it, and
9280 this may skip past TO_X. What we do is to first scan to
9281 TO_X.
9283 If TO_X is not specified, use a TO_X of zero. The reason
9284 is to make the outcome of this function more predictable.
9285 If we didn't use TO_X == 0, we would stop at the end of
9286 the line which is probably not what a caller would expect
9287 to happen. */
9288 skip = move_it_in_display_line_to
9289 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9290 (MOVE_TO_X | (op & MOVE_TO_POS)));
9292 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9293 if (skip == MOVE_POS_MATCH_OR_ZV)
9294 reached = 5;
9295 else if (skip == MOVE_X_REACHED)
9297 /* If TO_X was reached, we want to know whether TO_Y is
9298 in the line. We know this is the case if the already
9299 scanned glyphs make the line tall enough. Otherwise,
9300 we must check by scanning the rest of the line. */
9301 line_height = it->max_ascent + it->max_descent;
9302 if (to_y >= it->current_y
9303 && to_y < it->current_y + line_height)
9305 reached = 6;
9306 break;
9308 SAVE_IT (it_backup, *it, backup_data);
9309 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9310 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9311 op & MOVE_TO_POS);
9312 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9313 line_height = it->max_ascent + it->max_descent;
9314 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9316 if (to_y >= it->current_y
9317 && to_y < it->current_y + line_height)
9319 /* If TO_Y is in this line and TO_X was reached
9320 above, we scanned too far. We have to restore
9321 IT's settings to the ones before skipping. But
9322 keep the more accurate values of max_ascent and
9323 max_descent we've found while skipping the rest
9324 of the line, for the sake of callers, such as
9325 pos_visible_p, that need to know the line
9326 height. */
9327 int max_ascent = it->max_ascent;
9328 int max_descent = it->max_descent;
9330 RESTORE_IT (it, &it_backup, backup_data);
9331 it->max_ascent = max_ascent;
9332 it->max_descent = max_descent;
9333 reached = 6;
9335 else
9337 skip = skip2;
9338 if (skip == MOVE_POS_MATCH_OR_ZV)
9339 reached = 7;
9342 else
9344 /* Check whether TO_Y is in this line. */
9345 line_height = it->max_ascent + it->max_descent;
9346 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9348 if (to_y >= it->current_y
9349 && to_y < it->current_y + line_height)
9351 if (to_y > it->current_y)
9352 max_current_x = max (it->current_x, max_current_x);
9354 /* When word-wrap is on, TO_X may lie past the end
9355 of a wrapped line. Then it->current is the
9356 character on the next line, so backtrack to the
9357 space before the wrap point. */
9358 if (skip == MOVE_LINE_CONTINUED
9359 && it->line_wrap == WORD_WRAP)
9361 int prev_x = max (it->current_x - 1, 0);
9362 RESTORE_IT (it, &it_backup, backup_data);
9363 skip = move_it_in_display_line_to
9364 (it, -1, prev_x, MOVE_TO_X);
9367 reached = 6;
9371 if (reached)
9373 max_current_x = max (it->current_x, max_current_x);
9374 break;
9377 else if (BUFFERP (it->object)
9378 && (it->method == GET_FROM_BUFFER
9379 || it->method == GET_FROM_STRETCH)
9380 && IT_CHARPOS (*it) >= to_charpos
9381 /* Under bidi iteration, a call to set_iterator_to_next
9382 can scan far beyond to_charpos if the initial
9383 portion of the next line needs to be reordered. In
9384 that case, give move_it_in_display_line_to another
9385 chance below. */
9386 && !(it->bidi_p
9387 && it->bidi_it.scan_dir == -1))
9388 skip = MOVE_POS_MATCH_OR_ZV;
9389 else
9390 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9392 switch (skip)
9394 case MOVE_POS_MATCH_OR_ZV:
9395 max_current_x = max (it->current_x, max_current_x);
9396 reached = 8;
9397 goto out;
9399 case MOVE_NEWLINE_OR_CR:
9400 max_current_x = max (it->current_x, max_current_x);
9401 set_iterator_to_next (it, true);
9402 it->continuation_lines_width = 0;
9403 break;
9405 case MOVE_LINE_TRUNCATED:
9406 max_current_x = it->last_visible_x;
9407 it->continuation_lines_width = 0;
9408 reseat_at_next_visible_line_start (it, false);
9409 if ((op & MOVE_TO_POS) != 0
9410 && IT_CHARPOS (*it) > to_charpos)
9412 reached = 9;
9413 goto out;
9415 break;
9417 case MOVE_LINE_CONTINUED:
9418 max_current_x = it->last_visible_x;
9419 /* For continued lines ending in a tab, some of the glyphs
9420 associated with the tab are displayed on the current
9421 line. Since it->current_x does not include these glyphs,
9422 we use it->last_visible_x instead. */
9423 if (it->c == '\t')
9425 it->continuation_lines_width += it->last_visible_x;
9426 /* When moving by vpos, ensure that the iterator really
9427 advances to the next line (bug#847, bug#969). Fixme:
9428 do we need to do this in other circumstances? */
9429 if (it->current_x != it->last_visible_x
9430 && (op & MOVE_TO_VPOS)
9431 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9433 line_start_x = it->current_x + it->pixel_width
9434 - it->last_visible_x;
9435 if (FRAME_WINDOW_P (it->f))
9437 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9438 struct font *face_font = face->font;
9440 /* When display_line produces a continued line
9441 that ends in a TAB, it skips a tab stop that
9442 is closer than the font's space character
9443 width (see x_produce_glyphs where it produces
9444 the stretch glyph which represents a TAB).
9445 We need to reproduce the same logic here. */
9446 eassert (face_font);
9447 if (face_font)
9449 if (line_start_x < face_font->space_width)
9450 line_start_x
9451 += it->tab_width * face_font->space_width;
9454 set_iterator_to_next (it, false);
9457 else
9458 it->continuation_lines_width += it->current_x;
9459 break;
9461 default:
9462 emacs_abort ();
9465 /* Reset/increment for the next run. */
9466 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9467 it->current_x = line_start_x;
9468 line_start_x = 0;
9469 it->hpos = 0;
9470 it->current_y += it->max_ascent + it->max_descent;
9471 ++it->vpos;
9472 last_height = it->max_ascent + it->max_descent;
9473 it->max_ascent = it->max_descent = 0;
9476 out:
9478 /* On text terminals, we may stop at the end of a line in the middle
9479 of a multi-character glyph. If the glyph itself is continued,
9480 i.e. it is actually displayed on the next line, don't treat this
9481 stopping point as valid; move to the next line instead (unless
9482 that brings us offscreen). */
9483 if (!FRAME_WINDOW_P (it->f)
9484 && op & MOVE_TO_POS
9485 && IT_CHARPOS (*it) == to_charpos
9486 && it->what == IT_CHARACTER
9487 && it->nglyphs > 1
9488 && it->line_wrap == WINDOW_WRAP
9489 && it->current_x == it->last_visible_x - 1
9490 && it->c != '\n'
9491 && it->c != '\t'
9492 && it->w->window_end_valid
9493 && it->vpos < it->w->window_end_vpos)
9495 it->continuation_lines_width += it->current_x;
9496 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9497 it->current_y += it->max_ascent + it->max_descent;
9498 ++it->vpos;
9499 last_height = it->max_ascent + it->max_descent;
9502 if (backup_data)
9503 bidi_unshelve_cache (backup_data, true);
9505 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9507 return max_current_x;
9511 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9513 If DY > 0, move IT backward at least that many pixels. DY = 0
9514 means move IT backward to the preceding line start or BEGV. This
9515 function may move over more than DY pixels if IT->current_y - DY
9516 ends up in the middle of a line; in this case IT->current_y will be
9517 set to the top of the line moved to. */
9519 void
9520 move_it_vertically_backward (struct it *it, int dy)
9522 int nlines, h;
9523 struct it it2, it3;
9524 void *it2data = NULL, *it3data = NULL;
9525 ptrdiff_t start_pos;
9526 int nchars_per_row
9527 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9528 ptrdiff_t pos_limit;
9530 move_further_back:
9531 eassert (dy >= 0);
9533 start_pos = IT_CHARPOS (*it);
9535 /* Estimate how many newlines we must move back. */
9536 nlines = max (1, dy / default_line_pixel_height (it->w));
9537 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9538 pos_limit = BEGV;
9539 else
9540 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9542 /* Set the iterator's position that many lines back. But don't go
9543 back more than NLINES full screen lines -- this wins a day with
9544 buffers which have very long lines. */
9545 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9546 back_to_previous_visible_line_start (it);
9548 /* Reseat the iterator here. When moving backward, we don't want
9549 reseat to skip forward over invisible text, set up the iterator
9550 to deliver from overlay strings at the new position etc. So,
9551 use reseat_1 here. */
9552 reseat_1 (it, it->current.pos, true);
9554 /* We are now surely at a line start. */
9555 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9556 reordering is in effect. */
9557 it->continuation_lines_width = 0;
9559 /* Move forward and see what y-distance we moved. First move to the
9560 start of the next line so that we get its height. We need this
9561 height to be able to tell whether we reached the specified
9562 y-distance. */
9563 SAVE_IT (it2, *it, it2data);
9564 it2.max_ascent = it2.max_descent = 0;
9567 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9568 MOVE_TO_POS | MOVE_TO_VPOS);
9570 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9571 /* If we are in a display string which starts at START_POS,
9572 and that display string includes a newline, and we are
9573 right after that newline (i.e. at the beginning of a
9574 display line), exit the loop, because otherwise we will
9575 infloop, since move_it_to will see that it is already at
9576 START_POS and will not move. */
9577 || (it2.method == GET_FROM_STRING
9578 && IT_CHARPOS (it2) == start_pos
9579 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9580 eassert (IT_CHARPOS (*it) >= BEGV);
9581 SAVE_IT (it3, it2, it3data);
9583 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9584 eassert (IT_CHARPOS (*it) >= BEGV);
9585 /* H is the actual vertical distance from the position in *IT
9586 and the starting position. */
9587 h = it2.current_y - it->current_y;
9588 /* NLINES is the distance in number of lines. */
9589 nlines = it2.vpos - it->vpos;
9591 /* Correct IT's y and vpos position
9592 so that they are relative to the starting point. */
9593 it->vpos -= nlines;
9594 it->current_y -= h;
9596 if (dy == 0)
9598 /* DY == 0 means move to the start of the screen line. The
9599 value of nlines is > 0 if continuation lines were involved,
9600 or if the original IT position was at start of a line. */
9601 RESTORE_IT (it, it, it2data);
9602 if (nlines > 0)
9603 move_it_by_lines (it, nlines);
9604 /* The above code moves us to some position NLINES down,
9605 usually to its first glyph (leftmost in an L2R line), but
9606 that's not necessarily the start of the line, under bidi
9607 reordering. We want to get to the character position
9608 that is immediately after the newline of the previous
9609 line. */
9610 if (it->bidi_p
9611 && !it->continuation_lines_width
9612 && !STRINGP (it->string)
9613 && IT_CHARPOS (*it) > BEGV
9614 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9616 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9618 DEC_BOTH (cp, bp);
9619 cp = find_newline_no_quit (cp, bp, -1, NULL);
9620 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9622 bidi_unshelve_cache (it3data, true);
9624 else
9626 /* The y-position we try to reach, relative to *IT.
9627 Note that H has been subtracted in front of the if-statement. */
9628 int target_y = it->current_y + h - dy;
9629 int y0 = it3.current_y;
9630 int y1;
9631 int line_height;
9633 RESTORE_IT (&it3, &it3, it3data);
9634 y1 = line_bottom_y (&it3);
9635 line_height = y1 - y0;
9636 RESTORE_IT (it, it, it2data);
9637 /* If we did not reach target_y, try to move further backward if
9638 we can. If we moved too far backward, try to move forward. */
9639 if (target_y < it->current_y
9640 /* This is heuristic. In a window that's 3 lines high, with
9641 a line height of 13 pixels each, recentering with point
9642 on the bottom line will try to move -39/2 = 19 pixels
9643 backward. Try to avoid moving into the first line. */
9644 && (it->current_y - target_y
9645 > min (window_box_height (it->w), line_height * 2 / 3))
9646 && IT_CHARPOS (*it) > BEGV)
9648 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9649 target_y - it->current_y));
9650 dy = it->current_y - target_y;
9651 goto move_further_back;
9653 else if (target_y >= it->current_y + line_height
9654 && IT_CHARPOS (*it) < ZV)
9656 /* Should move forward by at least one line, maybe more.
9658 Note: Calling move_it_by_lines can be expensive on
9659 terminal frames, where compute_motion is used (via
9660 vmotion) to do the job, when there are very long lines
9661 and truncate-lines is nil. That's the reason for
9662 treating terminal frames specially here. */
9664 if (!FRAME_WINDOW_P (it->f))
9665 move_it_vertically (it, target_y - it->current_y);
9666 else
9670 move_it_by_lines (it, 1);
9672 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9679 /* Move IT by a specified amount of pixel lines DY. DY negative means
9680 move backwards. DY = 0 means move to start of screen line. At the
9681 end, IT will be on the start of a screen line. */
9683 void
9684 move_it_vertically (struct it *it, int dy)
9686 if (dy <= 0)
9687 move_it_vertically_backward (it, -dy);
9688 else
9690 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9691 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9692 MOVE_TO_POS | MOVE_TO_Y);
9693 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9695 /* If buffer ends in ZV without a newline, move to the start of
9696 the line to satisfy the post-condition. */
9697 if (IT_CHARPOS (*it) == ZV
9698 && ZV > BEGV
9699 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9700 move_it_by_lines (it, 0);
9705 /* Move iterator IT past the end of the text line it is in. */
9707 void
9708 move_it_past_eol (struct it *it)
9710 enum move_it_result rc;
9712 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9713 if (rc == MOVE_NEWLINE_OR_CR)
9714 set_iterator_to_next (it, false);
9718 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9719 negative means move up. DVPOS == 0 means move to the start of the
9720 screen line.
9722 Optimization idea: If we would know that IT->f doesn't use
9723 a face with proportional font, we could be faster for
9724 truncate-lines nil. */
9726 void
9727 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9730 /* The commented-out optimization uses vmotion on terminals. This
9731 gives bad results, because elements like it->what, on which
9732 callers such as pos_visible_p rely, aren't updated. */
9733 /* struct position pos;
9734 if (!FRAME_WINDOW_P (it->f))
9736 struct text_pos textpos;
9738 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9739 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9740 reseat (it, textpos, true);
9741 it->vpos += pos.vpos;
9742 it->current_y += pos.vpos;
9744 else */
9746 if (dvpos == 0)
9748 /* DVPOS == 0 means move to the start of the screen line. */
9749 move_it_vertically_backward (it, 0);
9750 /* Let next call to line_bottom_y calculate real line height. */
9751 last_height = 0;
9753 else if (dvpos > 0)
9755 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9756 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9758 /* Only move to the next buffer position if we ended up in a
9759 string from display property, not in an overlay string
9760 (before-string or after-string). That is because the
9761 latter don't conceal the underlying buffer position, so
9762 we can ask to move the iterator to the exact position we
9763 are interested in. Note that, even if we are already at
9764 IT_CHARPOS (*it), the call below is not a no-op, as it
9765 will detect that we are at the end of the string, pop the
9766 iterator, and compute it->current_x and it->hpos
9767 correctly. */
9768 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9769 -1, -1, -1, MOVE_TO_POS);
9772 else
9774 struct it it2;
9775 void *it2data = NULL;
9776 ptrdiff_t start_charpos, i;
9777 int nchars_per_row
9778 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9779 bool hit_pos_limit = false;
9780 ptrdiff_t pos_limit;
9782 /* Start at the beginning of the screen line containing IT's
9783 position. This may actually move vertically backwards,
9784 in case of overlays, so adjust dvpos accordingly. */
9785 dvpos += it->vpos;
9786 move_it_vertically_backward (it, 0);
9787 dvpos -= it->vpos;
9789 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9790 screen lines, and reseat the iterator there. */
9791 start_charpos = IT_CHARPOS (*it);
9792 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9793 pos_limit = BEGV;
9794 else
9795 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9797 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9798 back_to_previous_visible_line_start (it);
9799 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9800 hit_pos_limit = true;
9801 reseat (it, it->current.pos, true);
9803 /* Move further back if we end up in a string or an image. */
9804 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9806 /* First try to move to start of display line. */
9807 dvpos += it->vpos;
9808 move_it_vertically_backward (it, 0);
9809 dvpos -= it->vpos;
9810 if (IT_POS_VALID_AFTER_MOVE_P (it))
9811 break;
9812 /* If start of line is still in string or image,
9813 move further back. */
9814 back_to_previous_visible_line_start (it);
9815 reseat (it, it->current.pos, true);
9816 dvpos--;
9819 it->current_x = it->hpos = 0;
9821 /* Above call may have moved too far if continuation lines
9822 are involved. Scan forward and see if it did. */
9823 SAVE_IT (it2, *it, it2data);
9824 it2.vpos = it2.current_y = 0;
9825 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9826 it->vpos -= it2.vpos;
9827 it->current_y -= it2.current_y;
9828 it->current_x = it->hpos = 0;
9830 /* If we moved too far back, move IT some lines forward. */
9831 if (it2.vpos > -dvpos)
9833 int delta = it2.vpos + dvpos;
9835 RESTORE_IT (&it2, &it2, it2data);
9836 SAVE_IT (it2, *it, it2data);
9837 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9838 /* Move back again if we got too far ahead. */
9839 if (IT_CHARPOS (*it) >= start_charpos)
9840 RESTORE_IT (it, &it2, it2data);
9841 else
9842 bidi_unshelve_cache (it2data, true);
9844 else if (hit_pos_limit && pos_limit > BEGV
9845 && dvpos < 0 && it2.vpos < -dvpos)
9847 /* If we hit the limit, but still didn't make it far enough
9848 back, that means there's a display string with a newline
9849 covering a large chunk of text, and that caused
9850 back_to_previous_visible_line_start try to go too far.
9851 Punish those who commit such atrocities by going back
9852 until we've reached DVPOS, after lifting the limit, which
9853 could make it slow for very long lines. "If it hurts,
9854 don't do that!" */
9855 dvpos += it2.vpos;
9856 RESTORE_IT (it, it, it2data);
9857 for (i = -dvpos; i > 0; --i)
9859 back_to_previous_visible_line_start (it);
9860 it->vpos--;
9862 reseat_1 (it, it->current.pos, true);
9864 else
9865 RESTORE_IT (it, it, it2data);
9869 /* Return true if IT points into the middle of a display vector. */
9871 bool
9872 in_display_vector_p (struct it *it)
9874 return (it->method == GET_FROM_DISPLAY_VECTOR
9875 && it->current.dpvec_index > 0
9876 && it->dpvec + it->current.dpvec_index != it->dpend);
9879 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9880 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9881 WINDOW must be a live window and defaults to the selected one. The
9882 return value is a cons of the maximum pixel-width of any text line and
9883 the maximum pixel-height of all text lines.
9885 The optional argument FROM, if non-nil, specifies the first text
9886 position and defaults to the minimum accessible position of the buffer.
9887 If FROM is t, use the minimum accessible position that starts a
9888 non-empty line. TO, if non-nil, specifies the last text position and
9889 defaults to the maximum accessible position of the buffer. If TO is t,
9890 use the maximum accessible position that ends a non-empty line.
9892 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9893 width that can be returned. X-LIMIT nil or omitted, means to use the
9894 pixel-width of WINDOW's body; use this if you want to know how high
9895 WINDOW should be become in order to fit all of its buffer's text with
9896 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9897 if you intend to change WINDOW's width. In any case, text whose
9898 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9899 of long lines can take some time, it's always a good idea to make this
9900 argument as small as possible; in particular, if the buffer contains
9901 long lines that shall be truncated anyway.
9903 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9904 height (excluding the height of the mode- or header-line, if any) that
9905 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9906 ignored. Since calculating the text height of a large buffer can take
9907 some time, it makes sense to specify this argument if the size of the
9908 buffer is large or unknown.
9910 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9911 include the height of the mode- or header-line of WINDOW in the return
9912 value. If it is either the symbol `mode-line' or `header-line', include
9913 only the height of that line, if present, in the return value. If t,
9914 include the height of both, if present, in the return value. */)
9915 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9916 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9918 struct window *w = decode_live_window (window);
9919 Lisp_Object buffer = w->contents;
9920 struct buffer *b;
9921 struct it it;
9922 struct buffer *old_b = NULL;
9923 ptrdiff_t start, end, pos;
9924 struct text_pos startp;
9925 void *itdata = NULL;
9926 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9928 CHECK_BUFFER (buffer);
9929 b = XBUFFER (buffer);
9931 if (b != current_buffer)
9933 old_b = current_buffer;
9934 set_buffer_internal (b);
9937 if (NILP (from))
9938 start = BEGV;
9939 else if (EQ (from, Qt))
9941 start = pos = BEGV;
9942 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9943 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9944 start = pos;
9945 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9946 start = pos;
9948 else
9950 CHECK_NUMBER_COERCE_MARKER (from);
9951 start = min (max (XINT (from), BEGV), ZV);
9954 if (NILP (to))
9955 end = ZV;
9956 else if (EQ (to, Qt))
9958 end = pos = ZV;
9959 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9960 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9961 end = pos;
9962 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9963 end = pos;
9965 else
9967 CHECK_NUMBER_COERCE_MARKER (to);
9968 end = max (start, min (XINT (to), ZV));
9971 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9972 max_x = XINT (x_limit);
9974 if (NILP (y_limit))
9975 max_y = INT_MAX;
9976 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9977 max_y = XINT (y_limit);
9979 itdata = bidi_shelve_cache ();
9980 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9981 start_display (&it, w, startp);
9983 if (NILP (x_limit))
9984 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9985 else
9987 it.last_visible_x = max_x;
9988 /* Actually, we never want move_it_to stop at to_x. But to make
9989 sure that move_it_in_display_line_to always moves far enough,
9990 we set it to INT_MAX and specify MOVE_TO_X. */
9991 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9992 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9993 /* Don't return more than X-LIMIT. */
9994 if (x > max_x)
9995 x = max_x;
9998 /* Subtract height of header-line which was counted automatically by
9999 start_display. */
10000 y = it.current_y + it.max_ascent + it.max_descent
10001 - WINDOW_HEADER_LINE_HEIGHT (w);
10002 /* Don't return more than Y-LIMIT. */
10003 if (y > max_y)
10004 y = max_y;
10006 if (EQ (mode_and_header_line, Qheader_line)
10007 || EQ (mode_and_header_line, Qt))
10008 /* Re-add height of header-line as requested. */
10009 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10011 if (EQ (mode_and_header_line, Qmode_line)
10012 || EQ (mode_and_header_line, Qt))
10013 /* Add height of mode-line as requested. */
10014 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10016 bidi_unshelve_cache (itdata, false);
10018 if (old_b)
10019 set_buffer_internal (old_b);
10021 return Fcons (make_number (x), make_number (y));
10024 /***********************************************************************
10025 Messages
10026 ***********************************************************************/
10028 /* Return the number of arguments the format string FORMAT needs. */
10030 static ptrdiff_t
10031 format_nargs (char const *format)
10033 ptrdiff_t nargs = 0;
10034 for (char const *p = format; (p = strchr (p, '%')); p++)
10035 if (p[1] == '%')
10036 p++;
10037 else
10038 nargs++;
10039 return nargs;
10042 /* Add a message with format string FORMAT and formatted arguments
10043 to *Messages*. */
10045 void
10046 add_to_log (const char *format, ...)
10048 va_list ap;
10049 va_start (ap, format);
10050 vadd_to_log (format, ap);
10051 va_end (ap);
10054 void
10055 vadd_to_log (char const *format, va_list ap)
10057 ptrdiff_t form_nargs = format_nargs (format);
10058 ptrdiff_t nargs = 1 + form_nargs;
10059 Lisp_Object args[10];
10060 eassert (nargs <= ARRAYELTS (args));
10061 AUTO_STRING (args0, format);
10062 args[0] = args0;
10063 for (ptrdiff_t i = 1; i <= nargs; i++)
10064 args[i] = va_arg (ap, Lisp_Object);
10065 Lisp_Object msg = Qnil;
10066 msg = Fformat_message (nargs, args);
10068 ptrdiff_t len = SBYTES (msg) + 1;
10069 USE_SAFE_ALLOCA;
10070 char *buffer = SAFE_ALLOCA (len);
10071 memcpy (buffer, SDATA (msg), len);
10073 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10074 SAFE_FREE ();
10078 /* Output a newline in the *Messages* buffer if "needs" one. */
10080 void
10081 message_log_maybe_newline (void)
10083 if (message_log_need_newline)
10084 message_dolog ("", 0, true, false);
10088 /* Add a string M of length NBYTES to the message log, optionally
10089 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10090 true, means interpret the contents of M as multibyte. This
10091 function calls low-level routines in order to bypass text property
10092 hooks, etc. which might not be safe to run.
10094 This may GC (insert may run before/after change hooks),
10095 so the buffer M must NOT point to a Lisp string. */
10097 void
10098 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10100 const unsigned char *msg = (const unsigned char *) m;
10102 if (!NILP (Vmemory_full))
10103 return;
10105 if (!NILP (Vmessage_log_max))
10107 struct buffer *oldbuf;
10108 Lisp_Object oldpoint, oldbegv, oldzv;
10109 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10110 ptrdiff_t point_at_end = 0;
10111 ptrdiff_t zv_at_end = 0;
10112 Lisp_Object old_deactivate_mark;
10114 old_deactivate_mark = Vdeactivate_mark;
10115 oldbuf = current_buffer;
10117 /* Ensure the Messages buffer exists, and switch to it.
10118 If we created it, set the major-mode. */
10119 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10120 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10121 if (newbuffer
10122 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10123 call0 (intern ("messages-buffer-mode"));
10125 bset_undo_list (current_buffer, Qt);
10126 bset_cache_long_scans (current_buffer, Qnil);
10128 oldpoint = message_dolog_marker1;
10129 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10130 oldbegv = message_dolog_marker2;
10131 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10132 oldzv = message_dolog_marker3;
10133 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10135 if (PT == Z)
10136 point_at_end = 1;
10137 if (ZV == Z)
10138 zv_at_end = 1;
10140 BEGV = BEG;
10141 BEGV_BYTE = BEG_BYTE;
10142 ZV = Z;
10143 ZV_BYTE = Z_BYTE;
10144 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10146 /* Insert the string--maybe converting multibyte to single byte
10147 or vice versa, so that all the text fits the buffer. */
10148 if (multibyte
10149 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10151 ptrdiff_t i;
10152 int c, char_bytes;
10153 char work[1];
10155 /* Convert a multibyte string to single-byte
10156 for the *Message* buffer. */
10157 for (i = 0; i < nbytes; i += char_bytes)
10159 c = string_char_and_length (msg + i, &char_bytes);
10160 work[0] = CHAR_TO_BYTE8 (c);
10161 insert_1_both (work, 1, 1, true, false, false);
10164 else if (! multibyte
10165 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10167 ptrdiff_t i;
10168 int c, char_bytes;
10169 unsigned char str[MAX_MULTIBYTE_LENGTH];
10170 /* Convert a single-byte string to multibyte
10171 for the *Message* buffer. */
10172 for (i = 0; i < nbytes; i++)
10174 c = msg[i];
10175 MAKE_CHAR_MULTIBYTE (c);
10176 char_bytes = CHAR_STRING (c, str);
10177 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10180 else if (nbytes)
10181 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10182 true, false, false);
10184 if (nlflag)
10186 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10187 printmax_t dups;
10189 insert_1_both ("\n", 1, 1, true, false, false);
10191 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10192 this_bol = PT;
10193 this_bol_byte = PT_BYTE;
10195 /* See if this line duplicates the previous one.
10196 If so, combine duplicates. */
10197 if (this_bol > BEG)
10199 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10200 prev_bol = PT;
10201 prev_bol_byte = PT_BYTE;
10203 dups = message_log_check_duplicate (prev_bol_byte,
10204 this_bol_byte);
10205 if (dups)
10207 del_range_both (prev_bol, prev_bol_byte,
10208 this_bol, this_bol_byte, false);
10209 if (dups > 1)
10211 char dupstr[sizeof " [ times]"
10212 + INT_STRLEN_BOUND (printmax_t)];
10214 /* If you change this format, don't forget to also
10215 change message_log_check_duplicate. */
10216 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10217 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10218 insert_1_both (dupstr, duplen, duplen,
10219 true, false, true);
10224 /* If we have more than the desired maximum number of lines
10225 in the *Messages* buffer now, delete the oldest ones.
10226 This is safe because we don't have undo in this buffer. */
10228 if (NATNUMP (Vmessage_log_max))
10230 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10231 -XFASTINT (Vmessage_log_max) - 1, false);
10232 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10235 BEGV = marker_position (oldbegv);
10236 BEGV_BYTE = marker_byte_position (oldbegv);
10238 if (zv_at_end)
10240 ZV = Z;
10241 ZV_BYTE = Z_BYTE;
10243 else
10245 ZV = marker_position (oldzv);
10246 ZV_BYTE = marker_byte_position (oldzv);
10249 if (point_at_end)
10250 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10251 else
10252 /* We can't do Fgoto_char (oldpoint) because it will run some
10253 Lisp code. */
10254 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10255 marker_byte_position (oldpoint));
10257 unchain_marker (XMARKER (oldpoint));
10258 unchain_marker (XMARKER (oldbegv));
10259 unchain_marker (XMARKER (oldzv));
10261 /* We called insert_1_both above with its 5th argument (PREPARE)
10262 false, which prevents insert_1_both from calling
10263 prepare_to_modify_buffer, which in turns prevents us from
10264 incrementing windows_or_buffers_changed even if *Messages* is
10265 shown in some window. So we must manually set
10266 windows_or_buffers_changed here to make up for that. */
10267 windows_or_buffers_changed = old_windows_or_buffers_changed;
10268 bset_redisplay (current_buffer);
10270 set_buffer_internal (oldbuf);
10272 message_log_need_newline = !nlflag;
10273 Vdeactivate_mark = old_deactivate_mark;
10278 /* We are at the end of the buffer after just having inserted a newline.
10279 (Note: We depend on the fact we won't be crossing the gap.)
10280 Check to see if the most recent message looks a lot like the previous one.
10281 Return 0 if different, 1 if the new one should just replace it, or a
10282 value N > 1 if we should also append " [N times]". */
10284 static intmax_t
10285 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10287 ptrdiff_t i;
10288 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10289 bool seen_dots = false;
10290 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10291 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10293 for (i = 0; i < len; i++)
10295 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10296 seen_dots = true;
10297 if (p1[i] != p2[i])
10298 return seen_dots;
10300 p1 += len;
10301 if (*p1 == '\n')
10302 return 2;
10303 if (*p1++ == ' ' && *p1++ == '[')
10305 char *pend;
10306 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10307 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10308 return n + 1;
10310 return 0;
10314 /* Display an echo area message M with a specified length of NBYTES
10315 bytes. The string may include null characters. If M is not a
10316 string, clear out any existing message, and let the mini-buffer
10317 text show through.
10319 This function cancels echoing. */
10321 void
10322 message3 (Lisp_Object m)
10324 clear_message (true, true);
10325 cancel_echoing ();
10327 /* First flush out any partial line written with print. */
10328 message_log_maybe_newline ();
10329 if (STRINGP (m))
10331 ptrdiff_t nbytes = SBYTES (m);
10332 bool multibyte = STRING_MULTIBYTE (m);
10333 char *buffer;
10334 USE_SAFE_ALLOCA;
10335 SAFE_ALLOCA_STRING (buffer, m);
10336 message_dolog (buffer, nbytes, true, multibyte);
10337 SAFE_FREE ();
10339 if (! inhibit_message)
10340 message3_nolog (m);
10343 /* Log the message M to stderr. Log an empty line if M is not a string. */
10345 static void
10346 message_to_stderr (Lisp_Object m)
10348 if (noninteractive_need_newline)
10350 noninteractive_need_newline = false;
10351 fputc ('\n', stderr);
10353 if (STRINGP (m))
10355 Lisp_Object coding_system = Vlocale_coding_system;
10356 Lisp_Object s;
10358 if (!NILP (Vcoding_system_for_write))
10359 coding_system = Vcoding_system_for_write;
10360 if (!NILP (coding_system))
10361 s = code_convert_string_norecord (m, coding_system, true);
10362 else
10363 s = m;
10365 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10367 if (!cursor_in_echo_area)
10368 fputc ('\n', stderr);
10369 fflush (stderr);
10372 /* The non-logging version of message3.
10373 This does not cancel echoing, because it is used for echoing.
10374 Perhaps we need to make a separate function for echoing
10375 and make this cancel echoing. */
10377 void
10378 message3_nolog (Lisp_Object m)
10380 struct frame *sf = SELECTED_FRAME ();
10382 if (FRAME_INITIAL_P (sf))
10383 message_to_stderr (m);
10384 /* Error messages get reported properly by cmd_error, so this must be just an
10385 informative message; if the frame hasn't really been initialized yet, just
10386 toss it. */
10387 else if (INTERACTIVE && sf->glyphs_initialized_p)
10389 /* Get the frame containing the mini-buffer
10390 that the selected frame is using. */
10391 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10392 Lisp_Object frame = XWINDOW (mini_window)->frame;
10393 struct frame *f = XFRAME (frame);
10395 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10396 Fmake_frame_visible (frame);
10398 if (STRINGP (m) && SCHARS (m) > 0)
10400 set_message (m);
10401 if (minibuffer_auto_raise)
10402 Fraise_frame (frame);
10403 /* Assume we are not echoing.
10404 (If we are, echo_now will override this.) */
10405 echo_message_buffer = Qnil;
10407 else
10408 clear_message (true, true);
10410 do_pending_window_change (false);
10411 echo_area_display (true);
10412 do_pending_window_change (false);
10413 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10414 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10419 /* Display a null-terminated echo area message M. If M is 0, clear
10420 out any existing message, and let the mini-buffer text show through.
10422 The buffer M must continue to exist until after the echo area gets
10423 cleared or some other message gets displayed there. Do not pass
10424 text that is stored in a Lisp string. Do not pass text in a buffer
10425 that was alloca'd. */
10427 void
10428 message1 (const char *m)
10430 message3 (m ? build_unibyte_string (m) : Qnil);
10434 /* The non-logging counterpart of message1. */
10436 void
10437 message1_nolog (const char *m)
10439 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10442 /* Display a message M which contains a single %s
10443 which gets replaced with STRING. */
10445 void
10446 message_with_string (const char *m, Lisp_Object string, bool log)
10448 CHECK_STRING (string);
10450 bool need_message;
10451 if (noninteractive)
10452 need_message = !!m;
10453 else if (!INTERACTIVE)
10454 need_message = false;
10455 else
10457 /* The frame whose minibuffer we're going to display the message on.
10458 It may be larger than the selected frame, so we need
10459 to use its buffer, not the selected frame's buffer. */
10460 Lisp_Object mini_window;
10461 struct frame *f, *sf = SELECTED_FRAME ();
10463 /* Get the frame containing the minibuffer
10464 that the selected frame is using. */
10465 mini_window = FRAME_MINIBUF_WINDOW (sf);
10466 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10468 /* Error messages get reported properly by cmd_error, so this must be
10469 just an informative message; if the frame hasn't really been
10470 initialized yet, just toss it. */
10471 need_message = f->glyphs_initialized_p;
10474 if (need_message)
10476 AUTO_STRING (fmt, m);
10477 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10479 if (noninteractive)
10480 message_to_stderr (msg);
10481 else
10483 if (log)
10484 message3 (msg);
10485 else
10486 message3_nolog (msg);
10488 /* Print should start at the beginning of the message
10489 buffer next time. */
10490 message_buf_print = false;
10496 /* Dump an informative message to the minibuf. If M is 0, clear out
10497 any existing message, and let the mini-buffer text show through.
10499 The message must be safe ASCII and the format must not contain ` or
10500 '. If your message and format do not fit into this category,
10501 convert your arguments to Lisp objects and use Fmessage instead. */
10503 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10504 vmessage (const char *m, va_list ap)
10506 if (noninteractive)
10508 if (m)
10510 if (noninteractive_need_newline)
10511 putc ('\n', stderr);
10512 noninteractive_need_newline = false;
10513 vfprintf (stderr, m, ap);
10514 if (!cursor_in_echo_area)
10515 fprintf (stderr, "\n");
10516 fflush (stderr);
10519 else if (INTERACTIVE)
10521 /* The frame whose mini-buffer we're going to display the message
10522 on. It may be larger than the selected frame, so we need to
10523 use its buffer, not the selected frame's buffer. */
10524 Lisp_Object mini_window;
10525 struct frame *f, *sf = SELECTED_FRAME ();
10527 /* Get the frame containing the mini-buffer
10528 that the selected frame is using. */
10529 mini_window = FRAME_MINIBUF_WINDOW (sf);
10530 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10532 /* Error messages get reported properly by cmd_error, so this must be
10533 just an informative message; if the frame hasn't really been
10534 initialized yet, just toss it. */
10535 if (f->glyphs_initialized_p)
10537 if (m)
10539 ptrdiff_t len;
10540 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10541 USE_SAFE_ALLOCA;
10542 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10544 len = doprnt (message_buf, maxsize, m, 0, ap);
10546 message3 (make_string (message_buf, len));
10547 SAFE_FREE ();
10549 else
10550 message1 (0);
10552 /* Print should start at the beginning of the message
10553 buffer next time. */
10554 message_buf_print = false;
10559 void
10560 message (const char *m, ...)
10562 va_list ap;
10563 va_start (ap, m);
10564 vmessage (m, ap);
10565 va_end (ap);
10569 /* Display the current message in the current mini-buffer. This is
10570 only called from error handlers in process.c, and is not time
10571 critical. */
10573 void
10574 update_echo_area (void)
10576 if (!NILP (echo_area_buffer[0]))
10578 Lisp_Object string;
10579 string = Fcurrent_message ();
10580 message3 (string);
10585 /* Make sure echo area buffers in `echo_buffers' are live.
10586 If they aren't, make new ones. */
10588 static void
10589 ensure_echo_area_buffers (void)
10591 for (int i = 0; i < 2; i++)
10592 if (!BUFFERP (echo_buffer[i])
10593 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10595 Lisp_Object old_buffer = echo_buffer[i];
10596 static char const name_fmt[] = " *Echo Area %d*";
10597 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10598 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10599 echo_buffer[i] = Fget_buffer_create (lname);
10600 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10601 /* to force word wrap in echo area -
10602 it was decided to postpone this*/
10603 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10605 for (int j = 0; j < 2; j++)
10606 if (EQ (old_buffer, echo_area_buffer[j]))
10607 echo_area_buffer[j] = echo_buffer[i];
10612 /* Call FN with args A1..A2 with either the current or last displayed
10613 echo_area_buffer as current buffer.
10615 WHICH zero means use the current message buffer
10616 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10617 from echo_buffer[] and clear it.
10619 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10620 suitable buffer from echo_buffer[] and clear it.
10622 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10623 that the current message becomes the last displayed one, choose a
10624 suitable buffer for echo_area_buffer[0], and clear it.
10626 Value is what FN returns. */
10628 static bool
10629 with_echo_area_buffer (struct window *w, int which,
10630 bool (*fn) (ptrdiff_t, Lisp_Object),
10631 ptrdiff_t a1, Lisp_Object a2)
10633 Lisp_Object buffer;
10634 bool this_one, the_other, clear_buffer_p, rc;
10635 ptrdiff_t count = SPECPDL_INDEX ();
10637 /* If buffers aren't live, make new ones. */
10638 ensure_echo_area_buffers ();
10640 clear_buffer_p = false;
10642 if (which == 0)
10643 this_one = false, the_other = true;
10644 else if (which > 0)
10645 this_one = true, the_other = false;
10646 else
10648 this_one = false, the_other = true;
10649 clear_buffer_p = true;
10651 /* We need a fresh one in case the current echo buffer equals
10652 the one containing the last displayed echo area message. */
10653 if (!NILP (echo_area_buffer[this_one])
10654 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10655 echo_area_buffer[this_one] = Qnil;
10658 /* Choose a suitable buffer from echo_buffer[] if we don't
10659 have one. */
10660 if (NILP (echo_area_buffer[this_one]))
10662 echo_area_buffer[this_one]
10663 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10664 ? echo_buffer[the_other]
10665 : echo_buffer[this_one]);
10666 clear_buffer_p = true;
10669 buffer = echo_area_buffer[this_one];
10671 /* Don't get confused by reusing the buffer used for echoing
10672 for a different purpose. */
10673 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10674 cancel_echoing ();
10676 record_unwind_protect (unwind_with_echo_area_buffer,
10677 with_echo_area_buffer_unwind_data (w));
10679 /* Make the echo area buffer current. Note that for display
10680 purposes, it is not necessary that the displayed window's buffer
10681 == current_buffer, except for text property lookup. So, let's
10682 only set that buffer temporarily here without doing a full
10683 Fset_window_buffer. We must also change w->pointm, though,
10684 because otherwise an assertions in unshow_buffer fails, and Emacs
10685 aborts. */
10686 set_buffer_internal_1 (XBUFFER (buffer));
10687 if (w)
10689 wset_buffer (w, buffer);
10690 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10691 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10694 bset_undo_list (current_buffer, Qt);
10695 bset_read_only (current_buffer, Qnil);
10696 specbind (Qinhibit_read_only, Qt);
10697 specbind (Qinhibit_modification_hooks, Qt);
10699 if (clear_buffer_p && Z > BEG)
10700 del_range (BEG, Z);
10702 eassert (BEGV >= BEG);
10703 eassert (ZV <= Z && ZV >= BEGV);
10705 rc = fn (a1, a2);
10707 eassert (BEGV >= BEG);
10708 eassert (ZV <= Z && ZV >= BEGV);
10710 unbind_to (count, Qnil);
10711 return rc;
10715 /* Save state that should be preserved around the call to the function
10716 FN called in with_echo_area_buffer. */
10718 static Lisp_Object
10719 with_echo_area_buffer_unwind_data (struct window *w)
10721 int i = 0;
10722 Lisp_Object vector, tmp;
10724 /* Reduce consing by keeping one vector in
10725 Vwith_echo_area_save_vector. */
10726 vector = Vwith_echo_area_save_vector;
10727 Vwith_echo_area_save_vector = Qnil;
10729 if (NILP (vector))
10730 vector = Fmake_vector (make_number (11), Qnil);
10732 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10733 ASET (vector, i, Vdeactivate_mark); ++i;
10734 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10736 if (w)
10738 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10739 ASET (vector, i, w->contents); ++i;
10740 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10741 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10742 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10743 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10744 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10745 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10747 else
10749 int end = i + 8;
10750 for (; i < end; ++i)
10751 ASET (vector, i, Qnil);
10754 eassert (i == ASIZE (vector));
10755 return vector;
10759 /* Restore global state from VECTOR which was created by
10760 with_echo_area_buffer_unwind_data. */
10762 static void
10763 unwind_with_echo_area_buffer (Lisp_Object vector)
10765 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10766 Vdeactivate_mark = AREF (vector, 1);
10767 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10769 if (WINDOWP (AREF (vector, 3)))
10771 struct window *w;
10772 Lisp_Object buffer;
10774 w = XWINDOW (AREF (vector, 3));
10775 buffer = AREF (vector, 4);
10777 wset_buffer (w, buffer);
10778 set_marker_both (w->pointm, buffer,
10779 XFASTINT (AREF (vector, 5)),
10780 XFASTINT (AREF (vector, 6)));
10781 set_marker_both (w->old_pointm, buffer,
10782 XFASTINT (AREF (vector, 7)),
10783 XFASTINT (AREF (vector, 8)));
10784 set_marker_both (w->start, buffer,
10785 XFASTINT (AREF (vector, 9)),
10786 XFASTINT (AREF (vector, 10)));
10789 Vwith_echo_area_save_vector = vector;
10793 /* Set up the echo area for use by print functions. MULTIBYTE_P
10794 means we will print multibyte. */
10796 void
10797 setup_echo_area_for_printing (bool multibyte_p)
10799 /* If we can't find an echo area any more, exit. */
10800 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10801 Fkill_emacs (Qnil);
10803 ensure_echo_area_buffers ();
10805 if (!message_buf_print)
10807 /* A message has been output since the last time we printed.
10808 Choose a fresh echo area buffer. */
10809 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10810 echo_area_buffer[0] = echo_buffer[1];
10811 else
10812 echo_area_buffer[0] = echo_buffer[0];
10814 /* Switch to that buffer and clear it. */
10815 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10816 bset_truncate_lines (current_buffer, Qnil);
10818 if (Z > BEG)
10820 ptrdiff_t count = SPECPDL_INDEX ();
10821 specbind (Qinhibit_read_only, Qt);
10822 /* Note that undo recording is always disabled. */
10823 del_range (BEG, Z);
10824 unbind_to (count, Qnil);
10826 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10828 /* Set up the buffer for the multibyteness we need. */
10829 if (multibyte_p
10830 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10831 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10833 /* Raise the frame containing the echo area. */
10834 if (minibuffer_auto_raise)
10836 struct frame *sf = SELECTED_FRAME ();
10837 Lisp_Object mini_window;
10838 mini_window = FRAME_MINIBUF_WINDOW (sf);
10839 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10842 message_log_maybe_newline ();
10843 message_buf_print = true;
10845 else
10847 if (NILP (echo_area_buffer[0]))
10849 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10850 echo_area_buffer[0] = echo_buffer[1];
10851 else
10852 echo_area_buffer[0] = echo_buffer[0];
10855 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10857 /* Someone switched buffers between print requests. */
10858 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10859 bset_truncate_lines (current_buffer, Qnil);
10865 /* Display an echo area message in window W. Value is true if W's
10866 height is changed. If display_last_displayed_message_p,
10867 display the message that was last displayed, otherwise
10868 display the current message. */
10870 static bool
10871 display_echo_area (struct window *w)
10873 bool no_message_p, window_height_changed_p;
10875 /* Temporarily disable garbage collections while displaying the echo
10876 area. This is done because a GC can print a message itself.
10877 That message would modify the echo area buffer's contents while a
10878 redisplay of the buffer is going on, and seriously confuse
10879 redisplay. */
10880 ptrdiff_t count = inhibit_garbage_collection ();
10882 /* If there is no message, we must call display_echo_area_1
10883 nevertheless because it resizes the window. But we will have to
10884 reset the echo_area_buffer in question to nil at the end because
10885 with_echo_area_buffer will sets it to an empty buffer. */
10886 bool i = display_last_displayed_message_p;
10887 /* According to the C99, C11 and C++11 standards, the integral value
10888 of a "bool" is always 0 or 1, so this array access is safe here,
10889 if oddly typed. */
10890 no_message_p = NILP (echo_area_buffer[i]);
10892 window_height_changed_p
10893 = with_echo_area_buffer (w, display_last_displayed_message_p,
10894 display_echo_area_1,
10895 (intptr_t) w, Qnil);
10897 if (no_message_p)
10898 echo_area_buffer[i] = Qnil;
10900 unbind_to (count, Qnil);
10901 return window_height_changed_p;
10905 /* Helper for display_echo_area. Display the current buffer which
10906 contains the current echo area message in window W, a mini-window,
10907 a pointer to which is passed in A1. A2..A4 are currently not used.
10908 Change the height of W so that all of the message is displayed.
10909 Value is true if height of W was changed. */
10911 static bool
10912 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10914 intptr_t i1 = a1;
10915 struct window *w = (struct window *) i1;
10916 Lisp_Object window;
10917 struct text_pos start;
10919 /* We are about to enter redisplay without going through
10920 redisplay_internal, so we need to forget these faces by hand
10921 here. */
10922 forget_escape_and_glyphless_faces ();
10924 /* Do this before displaying, so that we have a large enough glyph
10925 matrix for the display. If we can't get enough space for the
10926 whole text, display the last N lines. That works by setting w->start. */
10927 bool window_height_changed_p = resize_mini_window (w, false);
10929 /* Use the starting position chosen by resize_mini_window. */
10930 SET_TEXT_POS_FROM_MARKER (start, w->start);
10932 /* Display. */
10933 clear_glyph_matrix (w->desired_matrix);
10934 XSETWINDOW (window, w);
10935 try_window (window, start, 0);
10937 return window_height_changed_p;
10941 /* Resize the echo area window to exactly the size needed for the
10942 currently displayed message, if there is one. If a mini-buffer
10943 is active, don't shrink it. */
10945 void
10946 resize_echo_area_exactly (void)
10948 if (BUFFERP (echo_area_buffer[0])
10949 && WINDOWP (echo_area_window))
10951 struct window *w = XWINDOW (echo_area_window);
10952 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10953 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10954 (intptr_t) w, resize_exactly);
10955 if (resized_p)
10957 windows_or_buffers_changed = 42;
10958 update_mode_lines = 30;
10959 redisplay_internal ();
10965 /* Callback function for with_echo_area_buffer, when used from
10966 resize_echo_area_exactly. A1 contains a pointer to the window to
10967 resize, EXACTLY non-nil means resize the mini-window exactly to the
10968 size of the text displayed. A3 and A4 are not used. Value is what
10969 resize_mini_window returns. */
10971 static bool
10972 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10974 intptr_t i1 = a1;
10975 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10979 /* Resize mini-window W to fit the size of its contents. EXACT_P
10980 means size the window exactly to the size needed. Otherwise, it's
10981 only enlarged until W's buffer is empty.
10983 Set W->start to the right place to begin display. If the whole
10984 contents fit, start at the beginning. Otherwise, start so as
10985 to make the end of the contents appear. This is particularly
10986 important for y-or-n-p, but seems desirable generally.
10988 Value is true if the window height has been changed. */
10990 bool
10991 resize_mini_window (struct window *w, bool exact_p)
10993 struct frame *f = XFRAME (w->frame);
10994 bool window_height_changed_p = false;
10996 eassert (MINI_WINDOW_P (w));
10998 /* By default, start display at the beginning. */
10999 set_marker_both (w->start, w->contents,
11000 BUF_BEGV (XBUFFER (w->contents)),
11001 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11003 /* Don't resize windows while redisplaying a window; it would
11004 confuse redisplay functions when the size of the window they are
11005 displaying changes from under them. Such a resizing can happen,
11006 for instance, when which-func prints a long message while
11007 we are running fontification-functions. We're running these
11008 functions with safe_call which binds inhibit-redisplay to t. */
11009 if (!NILP (Vinhibit_redisplay))
11010 return false;
11012 /* Nil means don't try to resize. */
11013 if (NILP (Vresize_mini_windows)
11014 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11015 return false;
11017 if (!FRAME_MINIBUF_ONLY_P (f))
11019 struct it it;
11020 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11021 + WINDOW_PIXEL_HEIGHT (w));
11022 int unit = FRAME_LINE_HEIGHT (f);
11023 int height, max_height;
11024 struct text_pos start;
11025 struct buffer *old_current_buffer = NULL;
11027 if (current_buffer != XBUFFER (w->contents))
11029 old_current_buffer = current_buffer;
11030 set_buffer_internal (XBUFFER (w->contents));
11033 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11035 /* Compute the max. number of lines specified by the user. */
11036 if (FLOATP (Vmax_mini_window_height))
11037 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11038 else if (INTEGERP (Vmax_mini_window_height))
11039 max_height = XINT (Vmax_mini_window_height) * unit;
11040 else
11041 max_height = total_height / 4;
11043 /* Correct that max. height if it's bogus. */
11044 max_height = clip_to_bounds (unit, max_height, total_height);
11046 /* Find out the height of the text in the window. */
11047 if (it.line_wrap == TRUNCATE)
11048 height = unit;
11049 else
11051 last_height = 0;
11052 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11053 if (it.max_ascent == 0 && it.max_descent == 0)
11054 height = it.current_y + last_height;
11055 else
11056 height = it.current_y + it.max_ascent + it.max_descent;
11057 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11060 /* Compute a suitable window start. */
11061 if (height > max_height)
11063 height = (max_height / unit) * unit;
11064 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11065 move_it_vertically_backward (&it, height - unit);
11066 start = it.current.pos;
11068 else
11069 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11070 SET_MARKER_FROM_TEXT_POS (w->start, start);
11072 if (EQ (Vresize_mini_windows, Qgrow_only))
11074 /* Let it grow only, until we display an empty message, in which
11075 case the window shrinks again. */
11076 if (height > WINDOW_PIXEL_HEIGHT (w))
11078 int old_height = WINDOW_PIXEL_HEIGHT (w);
11080 FRAME_WINDOWS_FROZEN (f) = true;
11081 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11082 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11084 else if (height < WINDOW_PIXEL_HEIGHT (w)
11085 && (exact_p || BEGV == ZV))
11087 int old_height = WINDOW_PIXEL_HEIGHT (w);
11089 FRAME_WINDOWS_FROZEN (f) = false;
11090 shrink_mini_window (w, true);
11091 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11094 else
11096 /* Always resize to exact size needed. */
11097 if (height > WINDOW_PIXEL_HEIGHT (w))
11099 int old_height = WINDOW_PIXEL_HEIGHT (w);
11101 FRAME_WINDOWS_FROZEN (f) = true;
11102 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11103 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11105 else if (height < WINDOW_PIXEL_HEIGHT (w))
11107 int old_height = WINDOW_PIXEL_HEIGHT (w);
11109 FRAME_WINDOWS_FROZEN (f) = false;
11110 shrink_mini_window (w, true);
11112 if (height)
11114 FRAME_WINDOWS_FROZEN (f) = true;
11115 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11118 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11122 if (old_current_buffer)
11123 set_buffer_internal (old_current_buffer);
11126 return window_height_changed_p;
11130 /* Value is the current message, a string, or nil if there is no
11131 current message. */
11133 Lisp_Object
11134 current_message (void)
11136 Lisp_Object msg;
11138 if (!BUFFERP (echo_area_buffer[0]))
11139 msg = Qnil;
11140 else
11142 with_echo_area_buffer (0, 0, current_message_1,
11143 (intptr_t) &msg, Qnil);
11144 if (NILP (msg))
11145 echo_area_buffer[0] = Qnil;
11148 return msg;
11152 static bool
11153 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11155 intptr_t i1 = a1;
11156 Lisp_Object *msg = (Lisp_Object *) i1;
11158 if (Z > BEG)
11159 *msg = make_buffer_string (BEG, Z, true);
11160 else
11161 *msg = Qnil;
11162 return false;
11166 /* Push the current message on Vmessage_stack for later restoration
11167 by restore_message. Value is true if the current message isn't
11168 empty. This is a relatively infrequent operation, so it's not
11169 worth optimizing. */
11171 bool
11172 push_message (void)
11174 Lisp_Object msg = current_message ();
11175 Vmessage_stack = Fcons (msg, Vmessage_stack);
11176 return STRINGP (msg);
11180 /* Restore message display from the top of Vmessage_stack. */
11182 void
11183 restore_message (void)
11185 eassert (CONSP (Vmessage_stack));
11186 message3_nolog (XCAR (Vmessage_stack));
11190 /* Handler for unwind-protect calling pop_message. */
11192 void
11193 pop_message_unwind (void)
11195 /* Pop the top-most entry off Vmessage_stack. */
11196 eassert (CONSP (Vmessage_stack));
11197 Vmessage_stack = XCDR (Vmessage_stack);
11201 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11202 exits. If the stack is not empty, we have a missing pop_message
11203 somewhere. */
11205 void
11206 check_message_stack (void)
11208 if (!NILP (Vmessage_stack))
11209 emacs_abort ();
11213 /* Truncate to NCHARS what will be displayed in the echo area the next
11214 time we display it---but don't redisplay it now. */
11216 void
11217 truncate_echo_area (ptrdiff_t nchars)
11219 if (nchars == 0)
11220 echo_area_buffer[0] = Qnil;
11221 else if (!noninteractive
11222 && INTERACTIVE
11223 && !NILP (echo_area_buffer[0]))
11225 struct frame *sf = SELECTED_FRAME ();
11226 /* Error messages get reported properly by cmd_error, so this must be
11227 just an informative message; if the frame hasn't really been
11228 initialized yet, just toss it. */
11229 if (sf->glyphs_initialized_p)
11230 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11235 /* Helper function for truncate_echo_area. Truncate the current
11236 message to at most NCHARS characters. */
11238 static bool
11239 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11241 if (BEG + nchars < Z)
11242 del_range (BEG + nchars, Z);
11243 if (Z == BEG)
11244 echo_area_buffer[0] = Qnil;
11245 return false;
11248 /* Set the current message to STRING. */
11250 static void
11251 set_message (Lisp_Object string)
11253 eassert (STRINGP (string));
11255 message_enable_multibyte = STRING_MULTIBYTE (string);
11257 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11258 message_buf_print = false;
11259 help_echo_showing_p = false;
11261 if (STRINGP (Vdebug_on_message)
11262 && STRINGP (string)
11263 && fast_string_match (Vdebug_on_message, string) >= 0)
11264 call_debugger (list2 (Qerror, string));
11268 /* Helper function for set_message. First argument is ignored and second
11269 argument has the same meaning as for set_message.
11270 This function is called with the echo area buffer being current. */
11272 static bool
11273 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11275 eassert (STRINGP (string));
11277 /* Change multibyteness of the echo buffer appropriately. */
11278 if (message_enable_multibyte
11279 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11280 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11282 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11283 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11284 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11286 /* Insert new message at BEG. */
11287 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11289 /* This function takes care of single/multibyte conversion.
11290 We just have to ensure that the echo area buffer has the right
11291 setting of enable_multibyte_characters. */
11292 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11294 return false;
11298 /* Clear messages. CURRENT_P means clear the current message.
11299 LAST_DISPLAYED_P means clear the message last displayed. */
11301 void
11302 clear_message (bool current_p, bool last_displayed_p)
11304 if (current_p)
11306 echo_area_buffer[0] = Qnil;
11307 message_cleared_p = true;
11310 if (last_displayed_p)
11311 echo_area_buffer[1] = Qnil;
11313 message_buf_print = false;
11316 /* Clear garbaged frames.
11318 This function is used where the old redisplay called
11319 redraw_garbaged_frames which in turn called redraw_frame which in
11320 turn called clear_frame. The call to clear_frame was a source of
11321 flickering. I believe a clear_frame is not necessary. It should
11322 suffice in the new redisplay to invalidate all current matrices,
11323 and ensure a complete redisplay of all windows. */
11325 static void
11326 clear_garbaged_frames (void)
11328 if (frame_garbaged)
11330 Lisp_Object tail, frame;
11331 struct frame *sf = SELECTED_FRAME ();
11333 FOR_EACH_FRAME (tail, frame)
11335 struct frame *f = XFRAME (frame);
11337 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11339 if (f->resized_p
11340 /* It makes no sense to redraw a non-selected TTY
11341 frame, since that will actually clear the
11342 selected frame, and might leave the selected
11343 frame with corrupted display, if it happens not
11344 to be marked garbaged. */
11345 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11346 redraw_frame (f);
11347 else
11348 clear_current_matrices (f);
11349 fset_redisplay (f);
11350 f->garbaged = false;
11351 f->resized_p = false;
11355 frame_garbaged = false;
11360 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11361 selected_frame. */
11363 static void
11364 echo_area_display (bool update_frame_p)
11366 Lisp_Object mini_window;
11367 struct window *w;
11368 struct frame *f;
11369 bool window_height_changed_p = false;
11370 struct frame *sf = SELECTED_FRAME ();
11372 mini_window = FRAME_MINIBUF_WINDOW (sf);
11373 w = XWINDOW (mini_window);
11374 f = XFRAME (WINDOW_FRAME (w));
11376 /* Don't display if frame is invisible or not yet initialized. */
11377 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11378 return;
11380 #ifdef HAVE_WINDOW_SYSTEM
11381 /* When Emacs starts, selected_frame may be the initial terminal
11382 frame. If we let this through, a message would be displayed on
11383 the terminal. */
11384 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11385 return;
11386 #endif /* HAVE_WINDOW_SYSTEM */
11388 /* Redraw garbaged frames. */
11389 clear_garbaged_frames ();
11391 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11393 echo_area_window = mini_window;
11394 window_height_changed_p = display_echo_area (w);
11395 w->must_be_updated_p = true;
11397 /* Update the display, unless called from redisplay_internal.
11398 Also don't update the screen during redisplay itself. The
11399 update will happen at the end of redisplay, and an update
11400 here could cause confusion. */
11401 if (update_frame_p && !redisplaying_p)
11403 int n = 0;
11405 /* If the display update has been interrupted by pending
11406 input, update mode lines in the frame. Due to the
11407 pending input, it might have been that redisplay hasn't
11408 been called, so that mode lines above the echo area are
11409 garbaged. This looks odd, so we prevent it here. */
11410 if (!display_completed)
11411 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11413 if (window_height_changed_p
11414 /* Don't do this if Emacs is shutting down. Redisplay
11415 needs to run hooks. */
11416 && !NILP (Vrun_hooks))
11418 /* Must update other windows. Likewise as in other
11419 cases, don't let this update be interrupted by
11420 pending input. */
11421 ptrdiff_t count = SPECPDL_INDEX ();
11422 specbind (Qredisplay_dont_pause, Qt);
11423 fset_redisplay (f);
11424 redisplay_internal ();
11425 unbind_to (count, Qnil);
11427 else if (FRAME_WINDOW_P (f) && n == 0)
11429 /* Window configuration is the same as before.
11430 Can do with a display update of the echo area,
11431 unless we displayed some mode lines. */
11432 update_single_window (w);
11433 flush_frame (f);
11435 else
11436 update_frame (f, true, true);
11438 /* If cursor is in the echo area, make sure that the next
11439 redisplay displays the minibuffer, so that the cursor will
11440 be replaced with what the minibuffer wants. */
11441 if (cursor_in_echo_area)
11442 wset_redisplay (XWINDOW (mini_window));
11445 else if (!EQ (mini_window, selected_window))
11446 wset_redisplay (XWINDOW (mini_window));
11448 /* Last displayed message is now the current message. */
11449 echo_area_buffer[1] = echo_area_buffer[0];
11450 /* Inform read_char that we're not echoing. */
11451 echo_message_buffer = Qnil;
11453 /* Prevent redisplay optimization in redisplay_internal by resetting
11454 this_line_start_pos. This is done because the mini-buffer now
11455 displays the message instead of its buffer text. */
11456 if (EQ (mini_window, selected_window))
11457 CHARPOS (this_line_start_pos) = 0;
11459 if (window_height_changed_p)
11461 fset_redisplay (f);
11463 /* If window configuration was changed, frames may have been
11464 marked garbaged. Clear them or we will experience
11465 surprises wrt scrolling.
11466 FIXME: How/why/when? */
11467 clear_garbaged_frames ();
11471 /* True if W's buffer was changed but not saved. */
11473 static bool
11474 window_buffer_changed (struct window *w)
11476 struct buffer *b = XBUFFER (w->contents);
11478 eassert (BUFFER_LIVE_P (b));
11480 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11483 /* True if W has %c in its mode line and mode line should be updated. */
11485 static bool
11486 mode_line_update_needed (struct window *w)
11488 return (w->column_number_displayed != -1
11489 && !(PT == w->last_point && !window_outdated (w))
11490 && (w->column_number_displayed != current_column ()));
11493 /* True if window start of W is frozen and may not be changed during
11494 redisplay. */
11496 static bool
11497 window_frozen_p (struct window *w)
11499 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11501 Lisp_Object window;
11503 XSETWINDOW (window, w);
11504 if (MINI_WINDOW_P (w))
11505 return false;
11506 else if (EQ (window, selected_window))
11507 return false;
11508 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11509 && EQ (window, Vminibuf_scroll_window))
11510 /* This special window can't be frozen too. */
11511 return false;
11512 else
11513 return true;
11515 return false;
11518 /***********************************************************************
11519 Mode Lines and Frame Titles
11520 ***********************************************************************/
11522 /* A buffer for constructing non-propertized mode-line strings and
11523 frame titles in it; allocated from the heap in init_xdisp and
11524 resized as needed in store_mode_line_noprop_char. */
11526 static char *mode_line_noprop_buf;
11528 /* The buffer's end, and a current output position in it. */
11530 static char *mode_line_noprop_buf_end;
11531 static char *mode_line_noprop_ptr;
11533 #define MODE_LINE_NOPROP_LEN(start) \
11534 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11536 static enum {
11537 MODE_LINE_DISPLAY = 0,
11538 MODE_LINE_TITLE,
11539 MODE_LINE_NOPROP,
11540 MODE_LINE_STRING
11541 } mode_line_target;
11543 /* Alist that caches the results of :propertize.
11544 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11545 static Lisp_Object mode_line_proptrans_alist;
11547 /* List of strings making up the mode-line. */
11548 static Lisp_Object mode_line_string_list;
11550 /* Base face property when building propertized mode line string. */
11551 static Lisp_Object mode_line_string_face;
11552 static Lisp_Object mode_line_string_face_prop;
11555 /* Unwind data for mode line strings */
11557 static Lisp_Object Vmode_line_unwind_vector;
11559 static Lisp_Object
11560 format_mode_line_unwind_data (struct frame *target_frame,
11561 struct buffer *obuf,
11562 Lisp_Object owin,
11563 bool save_proptrans)
11565 Lisp_Object vector, tmp;
11567 /* Reduce consing by keeping one vector in
11568 Vwith_echo_area_save_vector. */
11569 vector = Vmode_line_unwind_vector;
11570 Vmode_line_unwind_vector = Qnil;
11572 if (NILP (vector))
11573 vector = Fmake_vector (make_number (10), Qnil);
11575 ASET (vector, 0, make_number (mode_line_target));
11576 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11577 ASET (vector, 2, mode_line_string_list);
11578 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11579 ASET (vector, 4, mode_line_string_face);
11580 ASET (vector, 5, mode_line_string_face_prop);
11582 if (obuf)
11583 XSETBUFFER (tmp, obuf);
11584 else
11585 tmp = Qnil;
11586 ASET (vector, 6, tmp);
11587 ASET (vector, 7, owin);
11588 if (target_frame)
11590 /* Similarly to `with-selected-window', if the operation selects
11591 a window on another frame, we must restore that frame's
11592 selected window, and (for a tty) the top-frame. */
11593 ASET (vector, 8, target_frame->selected_window);
11594 if (FRAME_TERMCAP_P (target_frame))
11595 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11598 return vector;
11601 static void
11602 unwind_format_mode_line (Lisp_Object vector)
11604 Lisp_Object old_window = AREF (vector, 7);
11605 Lisp_Object target_frame_window = AREF (vector, 8);
11606 Lisp_Object old_top_frame = AREF (vector, 9);
11608 mode_line_target = XINT (AREF (vector, 0));
11609 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11610 mode_line_string_list = AREF (vector, 2);
11611 if (! EQ (AREF (vector, 3), Qt))
11612 mode_line_proptrans_alist = AREF (vector, 3);
11613 mode_line_string_face = AREF (vector, 4);
11614 mode_line_string_face_prop = AREF (vector, 5);
11616 /* Select window before buffer, since it may change the buffer. */
11617 if (!NILP (old_window))
11619 /* If the operation that we are unwinding had selected a window
11620 on a different frame, reset its frame-selected-window. For a
11621 text terminal, reset its top-frame if necessary. */
11622 if (!NILP (target_frame_window))
11624 Lisp_Object frame
11625 = WINDOW_FRAME (XWINDOW (target_frame_window));
11627 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11628 Fselect_window (target_frame_window, Qt);
11630 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11631 Fselect_frame (old_top_frame, Qt);
11634 Fselect_window (old_window, Qt);
11637 if (!NILP (AREF (vector, 6)))
11639 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11640 ASET (vector, 6, Qnil);
11643 Vmode_line_unwind_vector = vector;
11647 /* Store a single character C for the frame title in mode_line_noprop_buf.
11648 Re-allocate mode_line_noprop_buf if necessary. */
11650 static void
11651 store_mode_line_noprop_char (char c)
11653 /* If output position has reached the end of the allocated buffer,
11654 increase the buffer's size. */
11655 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11657 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11658 ptrdiff_t size = len;
11659 mode_line_noprop_buf =
11660 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11661 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11662 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11665 *mode_line_noprop_ptr++ = c;
11669 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11670 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11671 characters that yield more columns than PRECISION; PRECISION <= 0
11672 means copy the whole string. Pad with spaces until FIELD_WIDTH
11673 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11674 pad. Called from display_mode_element when it is used to build a
11675 frame title. */
11677 static int
11678 store_mode_line_noprop (const char *string, int field_width, int precision)
11680 const unsigned char *str = (const unsigned char *) string;
11681 int n = 0;
11682 ptrdiff_t dummy, nbytes;
11684 /* Copy at most PRECISION chars from STR. */
11685 nbytes = strlen (string);
11686 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11687 while (nbytes--)
11688 store_mode_line_noprop_char (*str++);
11690 /* Fill up with spaces until FIELD_WIDTH reached. */
11691 while (field_width > 0
11692 && n < field_width)
11694 store_mode_line_noprop_char (' ');
11695 ++n;
11698 return n;
11701 /***********************************************************************
11702 Frame Titles
11703 ***********************************************************************/
11705 #ifdef HAVE_WINDOW_SYSTEM
11707 /* Set the title of FRAME, if it has changed. The title format is
11708 Vicon_title_format if FRAME is iconified, otherwise it is
11709 frame_title_format. */
11711 static void
11712 x_consider_frame_title (Lisp_Object frame)
11714 struct frame *f = XFRAME (frame);
11716 if ((FRAME_WINDOW_P (f)
11717 || FRAME_MINIBUF_ONLY_P (f)
11718 || f->explicit_name)
11719 && NILP (Fframe_parameter (frame, Qtooltip)))
11721 /* Do we have more than one visible frame on this X display? */
11722 Lisp_Object tail, other_frame, fmt;
11723 ptrdiff_t title_start;
11724 char *title;
11725 ptrdiff_t len;
11726 struct it it;
11727 ptrdiff_t count = SPECPDL_INDEX ();
11729 FOR_EACH_FRAME (tail, other_frame)
11731 struct frame *tf = XFRAME (other_frame);
11733 if (tf != f
11734 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11735 && !FRAME_MINIBUF_ONLY_P (tf)
11736 && !EQ (other_frame, tip_frame)
11737 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11738 break;
11741 /* Set global variable indicating that multiple frames exist. */
11742 multiple_frames = CONSP (tail);
11744 /* Switch to the buffer of selected window of the frame. Set up
11745 mode_line_target so that display_mode_element will output into
11746 mode_line_noprop_buf; then display the title. */
11747 record_unwind_protect (unwind_format_mode_line,
11748 format_mode_line_unwind_data
11749 (f, current_buffer, selected_window, false));
11750 /* select-frame calls resize_mini_window, which could resize the
11751 mini-window and by that undo the effect of this redisplay
11752 cycle wrt minibuffer and echo-area display. Binding
11753 inhibit-redisplay to t makes the call to resize_mini_window a
11754 no-op, thus avoiding the adverse side effects. */
11755 specbind (Qinhibit_redisplay, Qt);
11757 Fselect_window (f->selected_window, Qt);
11758 set_buffer_internal_1
11759 (XBUFFER (XWINDOW (f->selected_window)->contents));
11760 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11762 mode_line_target = MODE_LINE_TITLE;
11763 title_start = MODE_LINE_NOPROP_LEN (0);
11764 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11765 NULL, DEFAULT_FACE_ID);
11766 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11767 len = MODE_LINE_NOPROP_LEN (title_start);
11768 title = mode_line_noprop_buf + title_start;
11769 unbind_to (count, Qnil);
11771 /* Set the title only if it's changed. This avoids consing in
11772 the common case where it hasn't. (If it turns out that we've
11773 already wasted too much time by walking through the list with
11774 display_mode_element, then we might need to optimize at a
11775 higher level than this.) */
11776 if (! STRINGP (f->name)
11777 || SBYTES (f->name) != len
11778 || memcmp (title, SDATA (f->name), len) != 0)
11779 x_implicitly_set_name (f, make_string (title, len), Qnil);
11783 #endif /* not HAVE_WINDOW_SYSTEM */
11786 /***********************************************************************
11787 Menu Bars
11788 ***********************************************************************/
11790 /* True if we will not redisplay all visible windows. */
11791 #define REDISPLAY_SOME_P() \
11792 ((windows_or_buffers_changed == 0 \
11793 || windows_or_buffers_changed == REDISPLAY_SOME) \
11794 && (update_mode_lines == 0 \
11795 || update_mode_lines == REDISPLAY_SOME))
11797 /* Prepare for redisplay by updating menu-bar item lists when
11798 appropriate. This can call eval. */
11800 static void
11801 prepare_menu_bars (void)
11803 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11804 bool some_windows = REDISPLAY_SOME_P ();
11805 Lisp_Object tooltip_frame;
11807 #ifdef HAVE_WINDOW_SYSTEM
11808 tooltip_frame = tip_frame;
11809 #else
11810 tooltip_frame = Qnil;
11811 #endif
11813 if (FUNCTIONP (Vpre_redisplay_function))
11815 Lisp_Object windows = all_windows ? Qt : Qnil;
11816 if (all_windows && some_windows)
11818 Lisp_Object ws = window_list ();
11819 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11821 Lisp_Object this = XCAR (ws);
11822 struct window *w = XWINDOW (this);
11823 if (w->redisplay
11824 || XFRAME (w->frame)->redisplay
11825 || XBUFFER (w->contents)->text->redisplay)
11827 windows = Fcons (this, windows);
11831 safe__call1 (true, Vpre_redisplay_function, windows);
11834 /* Update all frame titles based on their buffer names, etc. We do
11835 this before the menu bars so that the buffer-menu will show the
11836 up-to-date frame titles. */
11837 #ifdef HAVE_WINDOW_SYSTEM
11838 if (all_windows)
11840 Lisp_Object tail, frame;
11842 FOR_EACH_FRAME (tail, frame)
11844 struct frame *f = XFRAME (frame);
11845 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11846 if (some_windows
11847 && !f->redisplay
11848 && !w->redisplay
11849 && !XBUFFER (w->contents)->text->redisplay)
11850 continue;
11852 if (!EQ (frame, tooltip_frame)
11853 && (FRAME_ICONIFIED_P (f)
11854 || FRAME_VISIBLE_P (f) == 1
11855 /* Exclude TTY frames that are obscured because they
11856 are not the top frame on their console. This is
11857 because x_consider_frame_title actually switches
11858 to the frame, which for TTY frames means it is
11859 marked as garbaged, and will be completely
11860 redrawn on the next redisplay cycle. This causes
11861 TTY frames to be completely redrawn, when there
11862 are more than one of them, even though nothing
11863 should be changed on display. */
11864 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11865 x_consider_frame_title (frame);
11868 #endif /* HAVE_WINDOW_SYSTEM */
11870 /* Update the menu bar item lists, if appropriate. This has to be
11871 done before any actual redisplay or generation of display lines. */
11873 if (all_windows)
11875 Lisp_Object tail, frame;
11876 ptrdiff_t count = SPECPDL_INDEX ();
11877 /* True means that update_menu_bar has run its hooks
11878 so any further calls to update_menu_bar shouldn't do so again. */
11879 bool menu_bar_hooks_run = false;
11881 record_unwind_save_match_data ();
11883 FOR_EACH_FRAME (tail, frame)
11885 struct frame *f = XFRAME (frame);
11886 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11888 /* Ignore tooltip frame. */
11889 if (EQ (frame, tooltip_frame))
11890 continue;
11892 if (some_windows
11893 && !f->redisplay
11894 && !w->redisplay
11895 && !XBUFFER (w->contents)->text->redisplay)
11896 continue;
11898 run_window_size_change_functions (frame);
11899 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11900 #ifdef HAVE_WINDOW_SYSTEM
11901 update_tool_bar (f, false);
11902 #endif
11905 unbind_to (count, Qnil);
11907 else
11909 struct frame *sf = SELECTED_FRAME ();
11910 update_menu_bar (sf, true, false);
11911 #ifdef HAVE_WINDOW_SYSTEM
11912 update_tool_bar (sf, true);
11913 #endif
11918 /* Update the menu bar item list for frame F. This has to be done
11919 before we start to fill in any display lines, because it can call
11920 eval.
11922 If SAVE_MATCH_DATA, we must save and restore it here.
11924 If HOOKS_RUN, a previous call to update_menu_bar
11925 already ran the menu bar hooks for this redisplay, so there
11926 is no need to run them again. The return value is the
11927 updated value of this flag, to pass to the next call. */
11929 static bool
11930 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11932 Lisp_Object window;
11933 struct window *w;
11935 /* If called recursively during a menu update, do nothing. This can
11936 happen when, for instance, an activate-menubar-hook causes a
11937 redisplay. */
11938 if (inhibit_menubar_update)
11939 return hooks_run;
11941 window = FRAME_SELECTED_WINDOW (f);
11942 w = XWINDOW (window);
11944 if (FRAME_WINDOW_P (f)
11946 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11947 || defined (HAVE_NS) || defined (USE_GTK)
11948 FRAME_EXTERNAL_MENU_BAR (f)
11949 #else
11950 FRAME_MENU_BAR_LINES (f) > 0
11951 #endif
11952 : FRAME_MENU_BAR_LINES (f) > 0)
11954 /* If the user has switched buffers or windows, we need to
11955 recompute to reflect the new bindings. But we'll
11956 recompute when update_mode_lines is set too; that means
11957 that people can use force-mode-line-update to request
11958 that the menu bar be recomputed. The adverse effect on
11959 the rest of the redisplay algorithm is about the same as
11960 windows_or_buffers_changed anyway. */
11961 if (windows_or_buffers_changed
11962 /* This used to test w->update_mode_line, but we believe
11963 there is no need to recompute the menu in that case. */
11964 || update_mode_lines
11965 || window_buffer_changed (w))
11967 struct buffer *prev = current_buffer;
11968 ptrdiff_t count = SPECPDL_INDEX ();
11970 specbind (Qinhibit_menubar_update, Qt);
11972 set_buffer_internal_1 (XBUFFER (w->contents));
11973 if (save_match_data)
11974 record_unwind_save_match_data ();
11975 if (NILP (Voverriding_local_map_menu_flag))
11977 specbind (Qoverriding_terminal_local_map, Qnil);
11978 specbind (Qoverriding_local_map, Qnil);
11981 if (!hooks_run)
11983 /* Run the Lucid hook. */
11984 safe_run_hooks (Qactivate_menubar_hook);
11986 /* If it has changed current-menubar from previous value,
11987 really recompute the menu-bar from the value. */
11988 if (! NILP (Vlucid_menu_bar_dirty_flag))
11989 call0 (Qrecompute_lucid_menubar);
11991 safe_run_hooks (Qmenu_bar_update_hook);
11993 hooks_run = true;
11996 XSETFRAME (Vmenu_updating_frame, f);
11997 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11999 /* Redisplay the menu bar in case we changed it. */
12000 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12001 || defined (HAVE_NS) || defined (USE_GTK)
12002 if (FRAME_WINDOW_P (f))
12004 #if defined (HAVE_NS)
12005 /* All frames on Mac OS share the same menubar. So only
12006 the selected frame should be allowed to set it. */
12007 if (f == SELECTED_FRAME ())
12008 #endif
12009 set_frame_menubar (f, false, false);
12011 else
12012 /* On a terminal screen, the menu bar is an ordinary screen
12013 line, and this makes it get updated. */
12014 w->update_mode_line = true;
12015 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12016 /* In the non-toolkit version, the menu bar is an ordinary screen
12017 line, and this makes it get updated. */
12018 w->update_mode_line = true;
12019 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12021 unbind_to (count, Qnil);
12022 set_buffer_internal_1 (prev);
12026 return hooks_run;
12029 /***********************************************************************
12030 Tool-bars
12031 ***********************************************************************/
12033 #ifdef HAVE_WINDOW_SYSTEM
12035 /* Select `frame' temporarily without running all the code in
12036 do_switch_frame.
12037 FIXME: Maybe do_switch_frame should be trimmed down similarly
12038 when `norecord' is set. */
12039 static void
12040 fast_set_selected_frame (Lisp_Object frame)
12042 if (!EQ (selected_frame, frame))
12044 selected_frame = frame;
12045 selected_window = XFRAME (frame)->selected_window;
12049 /* Update the tool-bar item list for frame F. This has to be done
12050 before we start to fill in any display lines. Called from
12051 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12052 and restore it here. */
12054 static void
12055 update_tool_bar (struct frame *f, bool save_match_data)
12057 #if defined (USE_GTK) || defined (HAVE_NS)
12058 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12059 #else
12060 bool do_update = (WINDOWP (f->tool_bar_window)
12061 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12062 #endif
12064 if (do_update)
12066 Lisp_Object window;
12067 struct window *w;
12069 window = FRAME_SELECTED_WINDOW (f);
12070 w = XWINDOW (window);
12072 /* If the user has switched buffers or windows, we need to
12073 recompute to reflect the new bindings. But we'll
12074 recompute when update_mode_lines is set too; that means
12075 that people can use force-mode-line-update to request
12076 that the menu bar be recomputed. The adverse effect on
12077 the rest of the redisplay algorithm is about the same as
12078 windows_or_buffers_changed anyway. */
12079 if (windows_or_buffers_changed
12080 || w->update_mode_line
12081 || update_mode_lines
12082 || window_buffer_changed (w))
12084 struct buffer *prev = current_buffer;
12085 ptrdiff_t count = SPECPDL_INDEX ();
12086 Lisp_Object frame, new_tool_bar;
12087 int new_n_tool_bar;
12089 /* Set current_buffer to the buffer of the selected
12090 window of the frame, so that we get the right local
12091 keymaps. */
12092 set_buffer_internal_1 (XBUFFER (w->contents));
12094 /* Save match data, if we must. */
12095 if (save_match_data)
12096 record_unwind_save_match_data ();
12098 /* Make sure that we don't accidentally use bogus keymaps. */
12099 if (NILP (Voverriding_local_map_menu_flag))
12101 specbind (Qoverriding_terminal_local_map, Qnil);
12102 specbind (Qoverriding_local_map, Qnil);
12105 /* We must temporarily set the selected frame to this frame
12106 before calling tool_bar_items, because the calculation of
12107 the tool-bar keymap uses the selected frame (see
12108 `tool-bar-make-keymap' in tool-bar.el). */
12109 eassert (EQ (selected_window,
12110 /* Since we only explicitly preserve selected_frame,
12111 check that selected_window would be redundant. */
12112 XFRAME (selected_frame)->selected_window));
12113 record_unwind_protect (fast_set_selected_frame, selected_frame);
12114 XSETFRAME (frame, f);
12115 fast_set_selected_frame (frame);
12117 /* Build desired tool-bar items from keymaps. */
12118 new_tool_bar
12119 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12120 &new_n_tool_bar);
12122 /* Redisplay the tool-bar if we changed it. */
12123 if (new_n_tool_bar != f->n_tool_bar_items
12124 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12126 /* Redisplay that happens asynchronously due to an expose event
12127 may access f->tool_bar_items. Make sure we update both
12128 variables within BLOCK_INPUT so no such event interrupts. */
12129 block_input ();
12130 fset_tool_bar_items (f, new_tool_bar);
12131 f->n_tool_bar_items = new_n_tool_bar;
12132 w->update_mode_line = true;
12133 unblock_input ();
12136 unbind_to (count, Qnil);
12137 set_buffer_internal_1 (prev);
12142 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12144 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12145 F's desired tool-bar contents. F->tool_bar_items must have
12146 been set up previously by calling prepare_menu_bars. */
12148 static void
12149 build_desired_tool_bar_string (struct frame *f)
12151 int i, size, size_needed;
12152 Lisp_Object image, plist;
12154 image = plist = Qnil;
12156 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12157 Otherwise, make a new string. */
12159 /* The size of the string we might be able to reuse. */
12160 size = (STRINGP (f->desired_tool_bar_string)
12161 ? SCHARS (f->desired_tool_bar_string)
12162 : 0);
12164 /* We need one space in the string for each image. */
12165 size_needed = f->n_tool_bar_items;
12167 /* Reuse f->desired_tool_bar_string, if possible. */
12168 if (size < size_needed || NILP (f->desired_tool_bar_string))
12169 fset_desired_tool_bar_string
12170 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12171 else
12173 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12174 Fremove_text_properties (make_number (0), make_number (size),
12175 props, f->desired_tool_bar_string);
12178 /* Put a `display' property on the string for the images to display,
12179 put a `menu_item' property on tool-bar items with a value that
12180 is the index of the item in F's tool-bar item vector. */
12181 for (i = 0; i < f->n_tool_bar_items; ++i)
12183 #define PROP(IDX) \
12184 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12186 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12187 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12188 int hmargin, vmargin, relief, idx, end;
12190 /* If image is a vector, choose the image according to the
12191 button state. */
12192 image = PROP (TOOL_BAR_ITEM_IMAGES);
12193 if (VECTORP (image))
12195 if (enabled_p)
12196 idx = (selected_p
12197 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12198 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12199 else
12200 idx = (selected_p
12201 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12202 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12204 eassert (ASIZE (image) >= idx);
12205 image = AREF (image, idx);
12207 else
12208 idx = -1;
12210 /* Ignore invalid image specifications. */
12211 if (!valid_image_p (image))
12212 continue;
12214 /* Display the tool-bar button pressed, or depressed. */
12215 plist = Fcopy_sequence (XCDR (image));
12217 /* Compute margin and relief to draw. */
12218 relief = (tool_bar_button_relief >= 0
12219 ? tool_bar_button_relief
12220 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12221 hmargin = vmargin = relief;
12223 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12224 INT_MAX - max (hmargin, vmargin)))
12226 hmargin += XFASTINT (Vtool_bar_button_margin);
12227 vmargin += XFASTINT (Vtool_bar_button_margin);
12229 else if (CONSP (Vtool_bar_button_margin))
12231 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12232 INT_MAX - hmargin))
12233 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12235 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12236 INT_MAX - vmargin))
12237 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12240 if (auto_raise_tool_bar_buttons_p)
12242 /* Add a `:relief' property to the image spec if the item is
12243 selected. */
12244 if (selected_p)
12246 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12247 hmargin -= relief;
12248 vmargin -= relief;
12251 else
12253 /* If image is selected, display it pressed, i.e. with a
12254 negative relief. If it's not selected, display it with a
12255 raised relief. */
12256 plist = Fplist_put (plist, QCrelief,
12257 (selected_p
12258 ? make_number (-relief)
12259 : make_number (relief)));
12260 hmargin -= relief;
12261 vmargin -= relief;
12264 /* Put a margin around the image. */
12265 if (hmargin || vmargin)
12267 if (hmargin == vmargin)
12268 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12269 else
12270 plist = Fplist_put (plist, QCmargin,
12271 Fcons (make_number (hmargin),
12272 make_number (vmargin)));
12275 /* If button is not enabled, and we don't have special images
12276 for the disabled state, make the image appear disabled by
12277 applying an appropriate algorithm to it. */
12278 if (!enabled_p && idx < 0)
12279 plist = Fplist_put (plist, QCconversion, Qdisabled);
12281 /* Put a `display' text property on the string for the image to
12282 display. Put a `menu-item' property on the string that gives
12283 the start of this item's properties in the tool-bar items
12284 vector. */
12285 image = Fcons (Qimage, plist);
12286 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12287 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12289 /* Let the last image hide all remaining spaces in the tool bar
12290 string. The string can be longer than needed when we reuse a
12291 previous string. */
12292 if (i + 1 == f->n_tool_bar_items)
12293 end = SCHARS (f->desired_tool_bar_string);
12294 else
12295 end = i + 1;
12296 Fadd_text_properties (make_number (i), make_number (end),
12297 props, f->desired_tool_bar_string);
12298 #undef PROP
12303 /* Display one line of the tool-bar of frame IT->f.
12305 HEIGHT specifies the desired height of the tool-bar line.
12306 If the actual height of the glyph row is less than HEIGHT, the
12307 row's height is increased to HEIGHT, and the icons are centered
12308 vertically in the new height.
12310 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12311 count a final empty row in case the tool-bar width exactly matches
12312 the window width.
12315 static void
12316 display_tool_bar_line (struct it *it, int height)
12318 struct glyph_row *row = it->glyph_row;
12319 int max_x = it->last_visible_x;
12320 struct glyph *last;
12322 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12323 clear_glyph_row (row);
12324 row->enabled_p = true;
12325 row->y = it->current_y;
12327 /* Note that this isn't made use of if the face hasn't a box,
12328 so there's no need to check the face here. */
12329 it->start_of_box_run_p = true;
12331 while (it->current_x < max_x)
12333 int x, n_glyphs_before, i, nglyphs;
12334 struct it it_before;
12336 /* Get the next display element. */
12337 if (!get_next_display_element (it))
12339 /* Don't count empty row if we are counting needed tool-bar lines. */
12340 if (height < 0 && !it->hpos)
12341 return;
12342 break;
12345 /* Produce glyphs. */
12346 n_glyphs_before = row->used[TEXT_AREA];
12347 it_before = *it;
12349 PRODUCE_GLYPHS (it);
12351 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12352 i = 0;
12353 x = it_before.current_x;
12354 while (i < nglyphs)
12356 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12358 if (x + glyph->pixel_width > max_x)
12360 /* Glyph doesn't fit on line. Backtrack. */
12361 row->used[TEXT_AREA] = n_glyphs_before;
12362 *it = it_before;
12363 /* If this is the only glyph on this line, it will never fit on the
12364 tool-bar, so skip it. But ensure there is at least one glyph,
12365 so we don't accidentally disable the tool-bar. */
12366 if (n_glyphs_before == 0
12367 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12368 break;
12369 goto out;
12372 ++it->hpos;
12373 x += glyph->pixel_width;
12374 ++i;
12377 /* Stop at line end. */
12378 if (ITERATOR_AT_END_OF_LINE_P (it))
12379 break;
12381 set_iterator_to_next (it, true);
12384 out:;
12386 row->displays_text_p = row->used[TEXT_AREA] != 0;
12388 /* Use default face for the border below the tool bar.
12390 FIXME: When auto-resize-tool-bars is grow-only, there is
12391 no additional border below the possibly empty tool-bar lines.
12392 So to make the extra empty lines look "normal", we have to
12393 use the tool-bar face for the border too. */
12394 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12395 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12396 it->face_id = DEFAULT_FACE_ID;
12398 extend_face_to_end_of_line (it);
12399 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12400 last->right_box_line_p = true;
12401 if (last == row->glyphs[TEXT_AREA])
12402 last->left_box_line_p = true;
12404 /* Make line the desired height and center it vertically. */
12405 if ((height -= it->max_ascent + it->max_descent) > 0)
12407 /* Don't add more than one line height. */
12408 height %= FRAME_LINE_HEIGHT (it->f);
12409 it->max_ascent += height / 2;
12410 it->max_descent += (height + 1) / 2;
12413 compute_line_metrics (it);
12415 /* If line is empty, make it occupy the rest of the tool-bar. */
12416 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12418 row->height = row->phys_height = it->last_visible_y - row->y;
12419 row->visible_height = row->height;
12420 row->ascent = row->phys_ascent = 0;
12421 row->extra_line_spacing = 0;
12424 row->full_width_p = true;
12425 row->continued_p = false;
12426 row->truncated_on_left_p = false;
12427 row->truncated_on_right_p = false;
12429 it->current_x = it->hpos = 0;
12430 it->current_y += row->height;
12431 ++it->vpos;
12432 ++it->glyph_row;
12436 /* Value is the number of pixels needed to make all tool-bar items of
12437 frame F visible. The actual number of glyph rows needed is
12438 returned in *N_ROWS if non-NULL. */
12439 static int
12440 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12442 struct window *w = XWINDOW (f->tool_bar_window);
12443 struct it it;
12444 /* tool_bar_height is called from redisplay_tool_bar after building
12445 the desired matrix, so use (unused) mode-line row as temporary row to
12446 avoid destroying the first tool-bar row. */
12447 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12449 /* Initialize an iterator for iteration over
12450 F->desired_tool_bar_string in the tool-bar window of frame F. */
12451 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12452 temp_row->reversed_p = false;
12453 it.first_visible_x = 0;
12454 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12455 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12456 it.paragraph_embedding = L2R;
12458 while (!ITERATOR_AT_END_P (&it))
12460 clear_glyph_row (temp_row);
12461 it.glyph_row = temp_row;
12462 display_tool_bar_line (&it, -1);
12464 clear_glyph_row (temp_row);
12466 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12467 if (n_rows)
12468 *n_rows = it.vpos > 0 ? it.vpos : -1;
12470 if (pixelwise)
12471 return it.current_y;
12472 else
12473 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12476 #endif /* !USE_GTK && !HAVE_NS */
12478 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12479 0, 2, 0,
12480 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12481 If FRAME is nil or omitted, use the selected frame. Optional argument
12482 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12483 (Lisp_Object frame, Lisp_Object pixelwise)
12485 int height = 0;
12487 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12488 struct frame *f = decode_any_frame (frame);
12490 if (WINDOWP (f->tool_bar_window)
12491 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12493 update_tool_bar (f, true);
12494 if (f->n_tool_bar_items)
12496 build_desired_tool_bar_string (f);
12497 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12500 #endif
12502 return make_number (height);
12506 /* Display the tool-bar of frame F. Value is true if tool-bar's
12507 height should be changed. */
12508 static bool
12509 redisplay_tool_bar (struct frame *f)
12511 f->tool_bar_redisplayed = true;
12512 #if defined (USE_GTK) || defined (HAVE_NS)
12514 if (FRAME_EXTERNAL_TOOL_BAR (f))
12515 update_frame_tool_bar (f);
12516 return false;
12518 #else /* !USE_GTK && !HAVE_NS */
12520 struct window *w;
12521 struct it it;
12522 struct glyph_row *row;
12524 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12525 do anything. This means you must start with tool-bar-lines
12526 non-zero to get the auto-sizing effect. Or in other words, you
12527 can turn off tool-bars by specifying tool-bar-lines zero. */
12528 if (!WINDOWP (f->tool_bar_window)
12529 || (w = XWINDOW (f->tool_bar_window),
12530 WINDOW_TOTAL_LINES (w) == 0))
12531 return false;
12533 /* Set up an iterator for the tool-bar window. */
12534 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12535 it.first_visible_x = 0;
12536 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12537 row = it.glyph_row;
12538 row->reversed_p = false;
12540 /* Build a string that represents the contents of the tool-bar. */
12541 build_desired_tool_bar_string (f);
12542 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12543 /* FIXME: This should be controlled by a user option. But it
12544 doesn't make sense to have an R2L tool bar if the menu bar cannot
12545 be drawn also R2L, and making the menu bar R2L is tricky due
12546 toolkit-specific code that implements it. If an R2L tool bar is
12547 ever supported, display_tool_bar_line should also be augmented to
12548 call unproduce_glyphs like display_line and display_string
12549 do. */
12550 it.paragraph_embedding = L2R;
12552 if (f->n_tool_bar_rows == 0)
12554 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12556 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12558 x_change_tool_bar_height (f, new_height);
12559 frame_default_tool_bar_height = new_height;
12560 /* Always do that now. */
12561 clear_glyph_matrix (w->desired_matrix);
12562 f->fonts_changed = true;
12563 return true;
12567 /* Display as many lines as needed to display all tool-bar items. */
12569 if (f->n_tool_bar_rows > 0)
12571 int border, rows, height, extra;
12573 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12574 border = XINT (Vtool_bar_border);
12575 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12576 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12577 else if (EQ (Vtool_bar_border, Qborder_width))
12578 border = f->border_width;
12579 else
12580 border = 0;
12581 if (border < 0)
12582 border = 0;
12584 rows = f->n_tool_bar_rows;
12585 height = max (1, (it.last_visible_y - border) / rows);
12586 extra = it.last_visible_y - border - height * rows;
12588 while (it.current_y < it.last_visible_y)
12590 int h = 0;
12591 if (extra > 0 && rows-- > 0)
12593 h = (extra + rows - 1) / rows;
12594 extra -= h;
12596 display_tool_bar_line (&it, height + h);
12599 else
12601 while (it.current_y < it.last_visible_y)
12602 display_tool_bar_line (&it, 0);
12605 /* It doesn't make much sense to try scrolling in the tool-bar
12606 window, so don't do it. */
12607 w->desired_matrix->no_scrolling_p = true;
12608 w->must_be_updated_p = true;
12610 if (!NILP (Vauto_resize_tool_bars))
12612 bool change_height_p = true;
12614 /* If we couldn't display everything, change the tool-bar's
12615 height if there is room for more. */
12616 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12617 change_height_p = true;
12619 /* We subtract 1 because display_tool_bar_line advances the
12620 glyph_row pointer before returning to its caller. We want to
12621 examine the last glyph row produced by
12622 display_tool_bar_line. */
12623 row = it.glyph_row - 1;
12625 /* If there are blank lines at the end, except for a partially
12626 visible blank line at the end that is smaller than
12627 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12628 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12629 && row->height >= FRAME_LINE_HEIGHT (f))
12630 change_height_p = true;
12632 /* If row displays tool-bar items, but is partially visible,
12633 change the tool-bar's height. */
12634 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12635 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12636 change_height_p = true;
12638 /* Resize windows as needed by changing the `tool-bar-lines'
12639 frame parameter. */
12640 if (change_height_p)
12642 int nrows;
12643 int new_height = tool_bar_height (f, &nrows, true);
12645 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12646 && !f->minimize_tool_bar_window_p)
12647 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12648 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12649 f->minimize_tool_bar_window_p = false;
12651 if (change_height_p)
12653 x_change_tool_bar_height (f, new_height);
12654 frame_default_tool_bar_height = new_height;
12655 clear_glyph_matrix (w->desired_matrix);
12656 f->n_tool_bar_rows = nrows;
12657 f->fonts_changed = true;
12659 return true;
12664 f->minimize_tool_bar_window_p = false;
12665 return false;
12667 #endif /* USE_GTK || HAVE_NS */
12670 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12672 /* Get information about the tool-bar item which is displayed in GLYPH
12673 on frame F. Return in *PROP_IDX the index where tool-bar item
12674 properties start in F->tool_bar_items. Value is false if
12675 GLYPH doesn't display a tool-bar item. */
12677 static bool
12678 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12680 Lisp_Object prop;
12681 int charpos;
12683 /* This function can be called asynchronously, which means we must
12684 exclude any possibility that Fget_text_property signals an
12685 error. */
12686 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12687 charpos = max (0, charpos);
12689 /* Get the text property `menu-item' at pos. The value of that
12690 property is the start index of this item's properties in
12691 F->tool_bar_items. */
12692 prop = Fget_text_property (make_number (charpos),
12693 Qmenu_item, f->current_tool_bar_string);
12694 if (! INTEGERP (prop))
12695 return false;
12696 *prop_idx = XINT (prop);
12697 return true;
12701 /* Get information about the tool-bar item at position X/Y on frame F.
12702 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12703 the current matrix of the tool-bar window of F, or NULL if not
12704 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12705 item in F->tool_bar_items. Value is
12707 -1 if X/Y is not on a tool-bar item
12708 0 if X/Y is on the same item that was highlighted before.
12709 1 otherwise. */
12711 static int
12712 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12713 int *hpos, int *vpos, int *prop_idx)
12715 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12716 struct window *w = XWINDOW (f->tool_bar_window);
12717 int area;
12719 /* Find the glyph under X/Y. */
12720 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12721 if (*glyph == NULL)
12722 return -1;
12724 /* Get the start of this tool-bar item's properties in
12725 f->tool_bar_items. */
12726 if (!tool_bar_item_info (f, *glyph, prop_idx))
12727 return -1;
12729 /* Is mouse on the highlighted item? */
12730 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12731 && *vpos >= hlinfo->mouse_face_beg_row
12732 && *vpos <= hlinfo->mouse_face_end_row
12733 && (*vpos > hlinfo->mouse_face_beg_row
12734 || *hpos >= hlinfo->mouse_face_beg_col)
12735 && (*vpos < hlinfo->mouse_face_end_row
12736 || *hpos < hlinfo->mouse_face_end_col
12737 || hlinfo->mouse_face_past_end))
12738 return 0;
12740 return 1;
12744 /* EXPORT:
12745 Handle mouse button event on the tool-bar of frame F, at
12746 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12747 false for button release. MODIFIERS is event modifiers for button
12748 release. */
12750 void
12751 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12752 int modifiers)
12754 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12755 struct window *w = XWINDOW (f->tool_bar_window);
12756 int hpos, vpos, prop_idx;
12757 struct glyph *glyph;
12758 Lisp_Object enabled_p;
12759 int ts;
12761 /* If not on the highlighted tool-bar item, and mouse-highlight is
12762 non-nil, return. This is so we generate the tool-bar button
12763 click only when the mouse button is released on the same item as
12764 where it was pressed. However, when mouse-highlight is disabled,
12765 generate the click when the button is released regardless of the
12766 highlight, since tool-bar items are not highlighted in that
12767 case. */
12768 frame_to_window_pixel_xy (w, &x, &y);
12769 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12770 if (ts == -1
12771 || (ts != 0 && !NILP (Vmouse_highlight)))
12772 return;
12774 /* When mouse-highlight is off, generate the click for the item
12775 where the button was pressed, disregarding where it was
12776 released. */
12777 if (NILP (Vmouse_highlight) && !down_p)
12778 prop_idx = f->last_tool_bar_item;
12780 /* If item is disabled, do nothing. */
12781 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12782 if (NILP (enabled_p))
12783 return;
12785 if (down_p)
12787 /* Show item in pressed state. */
12788 if (!NILP (Vmouse_highlight))
12789 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12790 f->last_tool_bar_item = prop_idx;
12792 else
12794 Lisp_Object key, frame;
12795 struct input_event event;
12796 EVENT_INIT (event);
12798 /* Show item in released state. */
12799 if (!NILP (Vmouse_highlight))
12800 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12802 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12804 XSETFRAME (frame, f);
12805 event.kind = TOOL_BAR_EVENT;
12806 event.frame_or_window = frame;
12807 event.arg = frame;
12808 kbd_buffer_store_event (&event);
12810 event.kind = TOOL_BAR_EVENT;
12811 event.frame_or_window = frame;
12812 event.arg = key;
12813 event.modifiers = modifiers;
12814 kbd_buffer_store_event (&event);
12815 f->last_tool_bar_item = -1;
12820 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12821 tool-bar window-relative coordinates X/Y. Called from
12822 note_mouse_highlight. */
12824 static void
12825 note_tool_bar_highlight (struct frame *f, int x, int y)
12827 Lisp_Object window = f->tool_bar_window;
12828 struct window *w = XWINDOW (window);
12829 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12830 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12831 int hpos, vpos;
12832 struct glyph *glyph;
12833 struct glyph_row *row;
12834 int i;
12835 Lisp_Object enabled_p;
12836 int prop_idx;
12837 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12838 bool mouse_down_p;
12839 int rc;
12841 /* Function note_mouse_highlight is called with negative X/Y
12842 values when mouse moves outside of the frame. */
12843 if (x <= 0 || y <= 0)
12845 clear_mouse_face (hlinfo);
12846 return;
12849 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12850 if (rc < 0)
12852 /* Not on tool-bar item. */
12853 clear_mouse_face (hlinfo);
12854 return;
12856 else if (rc == 0)
12857 /* On same tool-bar item as before. */
12858 goto set_help_echo;
12860 clear_mouse_face (hlinfo);
12862 /* Mouse is down, but on different tool-bar item? */
12863 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12864 && f == dpyinfo->last_mouse_frame);
12866 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12867 return;
12869 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12871 /* If tool-bar item is not enabled, don't highlight it. */
12872 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12873 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12875 /* Compute the x-position of the glyph. In front and past the
12876 image is a space. We include this in the highlighted area. */
12877 row = MATRIX_ROW (w->current_matrix, vpos);
12878 for (i = x = 0; i < hpos; ++i)
12879 x += row->glyphs[TEXT_AREA][i].pixel_width;
12881 /* Record this as the current active region. */
12882 hlinfo->mouse_face_beg_col = hpos;
12883 hlinfo->mouse_face_beg_row = vpos;
12884 hlinfo->mouse_face_beg_x = x;
12885 hlinfo->mouse_face_past_end = false;
12887 hlinfo->mouse_face_end_col = hpos + 1;
12888 hlinfo->mouse_face_end_row = vpos;
12889 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12890 hlinfo->mouse_face_window = window;
12891 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12893 /* Display it as active. */
12894 show_mouse_face (hlinfo, draw);
12897 set_help_echo:
12899 /* Set help_echo_string to a help string to display for this tool-bar item.
12900 XTread_socket does the rest. */
12901 help_echo_object = help_echo_window = Qnil;
12902 help_echo_pos = -1;
12903 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12904 if (NILP (help_echo_string))
12905 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12908 #endif /* !USE_GTK && !HAVE_NS */
12910 #endif /* HAVE_WINDOW_SYSTEM */
12914 /************************************************************************
12915 Horizontal scrolling
12916 ************************************************************************/
12918 /* For all leaf windows in the window tree rooted at WINDOW, set their
12919 hscroll value so that PT is (i) visible in the window, and (ii) so
12920 that it is not within a certain margin at the window's left and
12921 right border. Value is true if any window's hscroll has been
12922 changed. */
12924 static bool
12925 hscroll_window_tree (Lisp_Object window)
12927 bool hscrolled_p = false;
12928 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12929 int hscroll_step_abs = 0;
12930 double hscroll_step_rel = 0;
12932 if (hscroll_relative_p)
12934 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12935 if (hscroll_step_rel < 0)
12937 hscroll_relative_p = false;
12938 hscroll_step_abs = 0;
12941 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12943 hscroll_step_abs = XINT (Vhscroll_step);
12944 if (hscroll_step_abs < 0)
12945 hscroll_step_abs = 0;
12947 else
12948 hscroll_step_abs = 0;
12950 while (WINDOWP (window))
12952 struct window *w = XWINDOW (window);
12954 if (WINDOWP (w->contents))
12955 hscrolled_p |= hscroll_window_tree (w->contents);
12956 else if (w->cursor.vpos >= 0)
12958 int h_margin;
12959 int text_area_width;
12960 struct glyph_row *cursor_row;
12961 struct glyph_row *bottom_row;
12963 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12964 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12965 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12966 else
12967 cursor_row = bottom_row - 1;
12969 if (!cursor_row->enabled_p)
12971 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12972 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12973 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12974 else
12975 cursor_row = bottom_row - 1;
12977 bool row_r2l_p = cursor_row->reversed_p;
12979 text_area_width = window_box_width (w, TEXT_AREA);
12981 /* Scroll when cursor is inside this scroll margin. */
12982 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12984 /* If the position of this window's point has explicitly
12985 changed, no more suspend auto hscrolling. */
12986 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12987 w->suspend_auto_hscroll = false;
12989 /* Remember window point. */
12990 Fset_marker (w->old_pointm,
12991 ((w == XWINDOW (selected_window))
12992 ? make_number (BUF_PT (XBUFFER (w->contents)))
12993 : Fmarker_position (w->pointm)),
12994 w->contents);
12996 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12997 && !w->suspend_auto_hscroll
12998 /* In some pathological cases, like restoring a window
12999 configuration into a frame that is much smaller than
13000 the one from which the configuration was saved, we
13001 get glyph rows whose start and end have zero buffer
13002 positions, which we cannot handle below. Just skip
13003 such windows. */
13004 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13005 /* For left-to-right rows, hscroll when cursor is either
13006 (i) inside the right hscroll margin, or (ii) if it is
13007 inside the left margin and the window is already
13008 hscrolled. */
13009 && ((!row_r2l_p
13010 && ((w->hscroll && w->cursor.x <= h_margin)
13011 || (cursor_row->enabled_p
13012 && cursor_row->truncated_on_right_p
13013 && (w->cursor.x >= text_area_width - h_margin))))
13014 /* For right-to-left rows, the logic is similar,
13015 except that rules for scrolling to left and right
13016 are reversed. E.g., if cursor.x <= h_margin, we
13017 need to hscroll "to the right" unconditionally,
13018 and that will scroll the screen to the left so as
13019 to reveal the next portion of the row. */
13020 || (row_r2l_p
13021 && ((cursor_row->enabled_p
13022 /* FIXME: It is confusing to set the
13023 truncated_on_right_p flag when R2L rows
13024 are actually truncated on the left. */
13025 && cursor_row->truncated_on_right_p
13026 && w->cursor.x <= h_margin)
13027 || (w->hscroll
13028 && (w->cursor.x >= text_area_width - h_margin))))))
13030 struct it it;
13031 ptrdiff_t hscroll;
13032 struct buffer *saved_current_buffer;
13033 ptrdiff_t pt;
13034 int wanted_x;
13036 /* Find point in a display of infinite width. */
13037 saved_current_buffer = current_buffer;
13038 current_buffer = XBUFFER (w->contents);
13040 if (w == XWINDOW (selected_window))
13041 pt = PT;
13042 else
13043 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13045 /* Move iterator to pt starting at cursor_row->start in
13046 a line with infinite width. */
13047 init_to_row_start (&it, w, cursor_row);
13048 it.last_visible_x = INFINITY;
13049 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13050 current_buffer = saved_current_buffer;
13052 /* Position cursor in window. */
13053 if (!hscroll_relative_p && hscroll_step_abs == 0)
13054 hscroll = max (0, (it.current_x
13055 - (ITERATOR_AT_END_OF_LINE_P (&it)
13056 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13057 : (text_area_width / 2))))
13058 / FRAME_COLUMN_WIDTH (it.f);
13059 else if ((!row_r2l_p
13060 && w->cursor.x >= text_area_width - h_margin)
13061 || (row_r2l_p && w->cursor.x <= h_margin))
13063 if (hscroll_relative_p)
13064 wanted_x = text_area_width * (1 - hscroll_step_rel)
13065 - h_margin;
13066 else
13067 wanted_x = text_area_width
13068 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13069 - h_margin;
13070 hscroll
13071 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13073 else
13075 if (hscroll_relative_p)
13076 wanted_x = text_area_width * hscroll_step_rel
13077 + h_margin;
13078 else
13079 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13080 + h_margin;
13081 hscroll
13082 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13084 hscroll = max (hscroll, w->min_hscroll);
13086 /* Don't prevent redisplay optimizations if hscroll
13087 hasn't changed, as it will unnecessarily slow down
13088 redisplay. */
13089 if (w->hscroll != hscroll)
13091 struct buffer *b = XBUFFER (w->contents);
13092 b->prevent_redisplay_optimizations_p = true;
13093 w->hscroll = hscroll;
13094 hscrolled_p = true;
13099 window = w->next;
13102 /* Value is true if hscroll of any leaf window has been changed. */
13103 return hscrolled_p;
13107 /* Set hscroll so that cursor is visible and not inside horizontal
13108 scroll margins for all windows in the tree rooted at WINDOW. See
13109 also hscroll_window_tree above. Value is true if any window's
13110 hscroll has been changed. If it has, desired matrices on the frame
13111 of WINDOW are cleared. */
13113 static bool
13114 hscroll_windows (Lisp_Object window)
13116 bool hscrolled_p = hscroll_window_tree (window);
13117 if (hscrolled_p)
13118 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13119 return hscrolled_p;
13124 /************************************************************************
13125 Redisplay
13126 ************************************************************************/
13128 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13129 This is sometimes handy to have in a debugger session. */
13131 #ifdef GLYPH_DEBUG
13133 /* First and last unchanged row for try_window_id. */
13135 static int debug_first_unchanged_at_end_vpos;
13136 static int debug_last_unchanged_at_beg_vpos;
13138 /* Delta vpos and y. */
13140 static int debug_dvpos, debug_dy;
13142 /* Delta in characters and bytes for try_window_id. */
13144 static ptrdiff_t debug_delta, debug_delta_bytes;
13146 /* Values of window_end_pos and window_end_vpos at the end of
13147 try_window_id. */
13149 static ptrdiff_t debug_end_vpos;
13151 /* Append a string to W->desired_matrix->method. FMT is a printf
13152 format string. If trace_redisplay_p is true also printf the
13153 resulting string to stderr. */
13155 static void debug_method_add (struct window *, char const *, ...)
13156 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13158 static void
13159 debug_method_add (struct window *w, char const *fmt, ...)
13161 void *ptr = w;
13162 char *method = w->desired_matrix->method;
13163 int len = strlen (method);
13164 int size = sizeof w->desired_matrix->method;
13165 int remaining = size - len - 1;
13166 va_list ap;
13168 if (len && remaining)
13170 method[len] = '|';
13171 --remaining, ++len;
13174 va_start (ap, fmt);
13175 vsnprintf (method + len, remaining + 1, fmt, ap);
13176 va_end (ap);
13178 if (trace_redisplay_p)
13179 fprintf (stderr, "%p (%s): %s\n",
13180 ptr,
13181 ((BUFFERP (w->contents)
13182 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13183 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13184 : "no buffer"),
13185 method + len);
13188 #endif /* GLYPH_DEBUG */
13191 /* Value is true if all changes in window W, which displays
13192 current_buffer, are in the text between START and END. START is a
13193 buffer position, END is given as a distance from Z. Used in
13194 redisplay_internal for display optimization. */
13196 static bool
13197 text_outside_line_unchanged_p (struct window *w,
13198 ptrdiff_t start, ptrdiff_t end)
13200 bool unchanged_p = true;
13202 /* If text or overlays have changed, see where. */
13203 if (window_outdated (w))
13205 /* Gap in the line? */
13206 if (GPT < start || Z - GPT < end)
13207 unchanged_p = false;
13209 /* Changes start in front of the line, or end after it? */
13210 if (unchanged_p
13211 && (BEG_UNCHANGED < start - 1
13212 || END_UNCHANGED < end))
13213 unchanged_p = false;
13215 /* If selective display, can't optimize if changes start at the
13216 beginning of the line. */
13217 if (unchanged_p
13218 && INTEGERP (BVAR (current_buffer, selective_display))
13219 && XINT (BVAR (current_buffer, selective_display)) > 0
13220 && (BEG_UNCHANGED < start || GPT <= start))
13221 unchanged_p = false;
13223 /* If there are overlays at the start or end of the line, these
13224 may have overlay strings with newlines in them. A change at
13225 START, for instance, may actually concern the display of such
13226 overlay strings as well, and they are displayed on different
13227 lines. So, quickly rule out this case. (For the future, it
13228 might be desirable to implement something more telling than
13229 just BEG/END_UNCHANGED.) */
13230 if (unchanged_p)
13232 if (BEG + BEG_UNCHANGED == start
13233 && overlay_touches_p (start))
13234 unchanged_p = false;
13235 if (END_UNCHANGED == end
13236 && overlay_touches_p (Z - end))
13237 unchanged_p = false;
13240 /* Under bidi reordering, adding or deleting a character in the
13241 beginning of a paragraph, before the first strong directional
13242 character, can change the base direction of the paragraph (unless
13243 the buffer specifies a fixed paragraph direction), which will
13244 require redisplaying the whole paragraph. It might be worthwhile
13245 to find the paragraph limits and widen the range of redisplayed
13246 lines to that, but for now just give up this optimization. */
13247 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13248 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13249 unchanged_p = false;
13252 return unchanged_p;
13256 /* Do a frame update, taking possible shortcuts into account. This is
13257 the main external entry point for redisplay.
13259 If the last redisplay displayed an echo area message and that message
13260 is no longer requested, we clear the echo area or bring back the
13261 mini-buffer if that is in use. */
13263 void
13264 redisplay (void)
13266 redisplay_internal ();
13270 static Lisp_Object
13271 overlay_arrow_string_or_property (Lisp_Object var)
13273 Lisp_Object val;
13275 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13276 return val;
13278 return Voverlay_arrow_string;
13281 /* Return true if there are any overlay-arrows in current_buffer. */
13282 static bool
13283 overlay_arrow_in_current_buffer_p (void)
13285 Lisp_Object vlist;
13287 for (vlist = Voverlay_arrow_variable_list;
13288 CONSP (vlist);
13289 vlist = XCDR (vlist))
13291 Lisp_Object var = XCAR (vlist);
13292 Lisp_Object val;
13294 if (!SYMBOLP (var))
13295 continue;
13296 val = find_symbol_value (var);
13297 if (MARKERP (val)
13298 && current_buffer == XMARKER (val)->buffer)
13299 return true;
13301 return false;
13305 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13306 has changed. */
13308 static bool
13309 overlay_arrows_changed_p (void)
13311 Lisp_Object vlist;
13313 for (vlist = Voverlay_arrow_variable_list;
13314 CONSP (vlist);
13315 vlist = XCDR (vlist))
13317 Lisp_Object var = XCAR (vlist);
13318 Lisp_Object val, pstr;
13320 if (!SYMBOLP (var))
13321 continue;
13322 val = find_symbol_value (var);
13323 if (!MARKERP (val))
13324 continue;
13325 if (! EQ (COERCE_MARKER (val),
13326 Fget (var, Qlast_arrow_position))
13327 || ! (pstr = overlay_arrow_string_or_property (var),
13328 EQ (pstr, Fget (var, Qlast_arrow_string))))
13329 return true;
13331 return false;
13334 /* Mark overlay arrows to be updated on next redisplay. */
13336 static void
13337 update_overlay_arrows (int up_to_date)
13339 Lisp_Object vlist;
13341 for (vlist = Voverlay_arrow_variable_list;
13342 CONSP (vlist);
13343 vlist = XCDR (vlist))
13345 Lisp_Object var = XCAR (vlist);
13347 if (!SYMBOLP (var))
13348 continue;
13350 if (up_to_date > 0)
13352 Lisp_Object val = find_symbol_value (var);
13353 Fput (var, Qlast_arrow_position,
13354 COERCE_MARKER (val));
13355 Fput (var, Qlast_arrow_string,
13356 overlay_arrow_string_or_property (var));
13358 else if (up_to_date < 0
13359 || !NILP (Fget (var, Qlast_arrow_position)))
13361 Fput (var, Qlast_arrow_position, Qt);
13362 Fput (var, Qlast_arrow_string, Qt);
13368 /* Return overlay arrow string to display at row.
13369 Return integer (bitmap number) for arrow bitmap in left fringe.
13370 Return nil if no overlay arrow. */
13372 static Lisp_Object
13373 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13375 Lisp_Object vlist;
13377 for (vlist = Voverlay_arrow_variable_list;
13378 CONSP (vlist);
13379 vlist = XCDR (vlist))
13381 Lisp_Object var = XCAR (vlist);
13382 Lisp_Object val;
13384 if (!SYMBOLP (var))
13385 continue;
13387 val = find_symbol_value (var);
13389 if (MARKERP (val)
13390 && current_buffer == XMARKER (val)->buffer
13391 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13393 if (FRAME_WINDOW_P (it->f)
13394 /* FIXME: if ROW->reversed_p is set, this should test
13395 the right fringe, not the left one. */
13396 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13398 #ifdef HAVE_WINDOW_SYSTEM
13399 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13401 int fringe_bitmap = lookup_fringe_bitmap (val);
13402 if (fringe_bitmap != 0)
13403 return make_number (fringe_bitmap);
13405 #endif
13406 return make_number (-1); /* Use default arrow bitmap. */
13408 return overlay_arrow_string_or_property (var);
13412 return Qnil;
13415 /* Return true if point moved out of or into a composition. Otherwise
13416 return false. PREV_BUF and PREV_PT are the last point buffer and
13417 position. BUF and PT are the current point buffer and position. */
13419 static bool
13420 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13421 struct buffer *buf, ptrdiff_t pt)
13423 ptrdiff_t start, end;
13424 Lisp_Object prop;
13425 Lisp_Object buffer;
13427 XSETBUFFER (buffer, buf);
13428 /* Check a composition at the last point if point moved within the
13429 same buffer. */
13430 if (prev_buf == buf)
13432 if (prev_pt == pt)
13433 /* Point didn't move. */
13434 return false;
13436 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13437 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13438 && composition_valid_p (start, end, prop)
13439 && start < prev_pt && end > prev_pt)
13440 /* The last point was within the composition. Return true iff
13441 point moved out of the composition. */
13442 return (pt <= start || pt >= end);
13445 /* Check a composition at the current point. */
13446 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13447 && find_composition (pt, -1, &start, &end, &prop, buffer)
13448 && composition_valid_p (start, end, prop)
13449 && start < pt && end > pt);
13452 /* Reconsider the clip changes of buffer which is displayed in W. */
13454 static void
13455 reconsider_clip_changes (struct window *w)
13457 struct buffer *b = XBUFFER (w->contents);
13459 if (b->clip_changed
13460 && w->window_end_valid
13461 && w->current_matrix->buffer == b
13462 && w->current_matrix->zv == BUF_ZV (b)
13463 && w->current_matrix->begv == BUF_BEGV (b))
13464 b->clip_changed = false;
13466 /* If display wasn't paused, and W is not a tool bar window, see if
13467 point has been moved into or out of a composition. In that case,
13468 set b->clip_changed to force updating the screen. If
13469 b->clip_changed has already been set, skip this check. */
13470 if (!b->clip_changed && w->window_end_valid)
13472 ptrdiff_t pt = (w == XWINDOW (selected_window)
13473 ? PT : marker_position (w->pointm));
13475 if ((w->current_matrix->buffer != b || pt != w->last_point)
13476 && check_point_in_composition (w->current_matrix->buffer,
13477 w->last_point, b, pt))
13478 b->clip_changed = true;
13482 static void
13483 propagate_buffer_redisplay (void)
13484 { /* Resetting b->text->redisplay is problematic!
13485 We can't just reset it in the case that some window that displays
13486 it has not been redisplayed; and such a window can stay
13487 unredisplayed for a long time if it's currently invisible.
13488 But we do want to reset it at the end of redisplay otherwise
13489 its displayed windows will keep being redisplayed over and over
13490 again.
13491 So we copy all b->text->redisplay flags up to their windows here,
13492 such that mark_window_display_accurate can safely reset
13493 b->text->redisplay. */
13494 Lisp_Object ws = window_list ();
13495 for (; CONSP (ws); ws = XCDR (ws))
13497 struct window *thisw = XWINDOW (XCAR (ws));
13498 struct buffer *thisb = XBUFFER (thisw->contents);
13499 if (thisb->text->redisplay)
13500 thisw->redisplay = true;
13504 #define STOP_POLLING \
13505 do { if (! polling_stopped_here) stop_polling (); \
13506 polling_stopped_here = true; } while (false)
13508 #define RESUME_POLLING \
13509 do { if (polling_stopped_here) start_polling (); \
13510 polling_stopped_here = false; } while (false)
13513 /* Perhaps in the future avoid recentering windows if it
13514 is not necessary; currently that causes some problems. */
13516 static void
13517 redisplay_internal (void)
13519 struct window *w = XWINDOW (selected_window);
13520 struct window *sw;
13521 struct frame *fr;
13522 bool pending;
13523 bool must_finish = false, match_p;
13524 struct text_pos tlbufpos, tlendpos;
13525 int number_of_visible_frames;
13526 ptrdiff_t count;
13527 struct frame *sf;
13528 bool polling_stopped_here = false;
13529 Lisp_Object tail, frame;
13531 /* True means redisplay has to consider all windows on all
13532 frames. False, only selected_window is considered. */
13533 bool consider_all_windows_p;
13535 /* True means redisplay has to redisplay the miniwindow. */
13536 bool update_miniwindow_p = false;
13538 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13540 /* No redisplay if running in batch mode or frame is not yet fully
13541 initialized, or redisplay is explicitly turned off by setting
13542 Vinhibit_redisplay. */
13543 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13544 || !NILP (Vinhibit_redisplay))
13545 return;
13547 /* Don't examine these until after testing Vinhibit_redisplay.
13548 When Emacs is shutting down, perhaps because its connection to
13549 X has dropped, we should not look at them at all. */
13550 fr = XFRAME (w->frame);
13551 sf = SELECTED_FRAME ();
13553 if (!fr->glyphs_initialized_p)
13554 return;
13556 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13557 if (popup_activated ())
13558 return;
13559 #endif
13561 /* I don't think this happens but let's be paranoid. */
13562 if (redisplaying_p)
13563 return;
13565 /* Record a function that clears redisplaying_p
13566 when we leave this function. */
13567 count = SPECPDL_INDEX ();
13568 record_unwind_protect_void (unwind_redisplay);
13569 redisplaying_p = true;
13570 specbind (Qinhibit_free_realized_faces, Qnil);
13572 /* Record this function, so it appears on the profiler's backtraces. */
13573 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13575 FOR_EACH_FRAME (tail, frame)
13576 XFRAME (frame)->already_hscrolled_p = false;
13578 retry:
13579 /* Remember the currently selected window. */
13580 sw = w;
13582 pending = false;
13583 forget_escape_and_glyphless_faces ();
13585 inhibit_free_realized_faces = false;
13587 /* If face_change, init_iterator will free all realized faces, which
13588 includes the faces referenced from current matrices. So, we
13589 can't reuse current matrices in this case. */
13590 if (face_change)
13591 windows_or_buffers_changed = 47;
13593 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13594 && FRAME_TTY (sf)->previous_frame != sf)
13596 /* Since frames on a single ASCII terminal share the same
13597 display area, displaying a different frame means redisplay
13598 the whole thing. */
13599 SET_FRAME_GARBAGED (sf);
13600 #ifndef DOS_NT
13601 set_tty_color_mode (FRAME_TTY (sf), sf);
13602 #endif
13603 FRAME_TTY (sf)->previous_frame = sf;
13606 /* Set the visible flags for all frames. Do this before checking for
13607 resized or garbaged frames; they want to know if their frames are
13608 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13609 number_of_visible_frames = 0;
13611 FOR_EACH_FRAME (tail, frame)
13613 struct frame *f = XFRAME (frame);
13615 if (FRAME_VISIBLE_P (f))
13617 ++number_of_visible_frames;
13618 /* Adjust matrices for visible frames only. */
13619 if (f->fonts_changed)
13621 adjust_frame_glyphs (f);
13622 /* Disable all redisplay optimizations for this frame.
13623 This is because adjust_frame_glyphs resets the
13624 enabled_p flag for all glyph rows of all windows, so
13625 many optimizations will fail anyway, and some might
13626 fail to test that flag and do bogus things as
13627 result. */
13628 SET_FRAME_GARBAGED (f);
13629 f->fonts_changed = false;
13631 /* If cursor type has been changed on the frame
13632 other than selected, consider all frames. */
13633 if (f != sf && f->cursor_type_changed)
13634 fset_redisplay (f);
13636 clear_desired_matrices (f);
13639 /* Notice any pending interrupt request to change frame size. */
13640 do_pending_window_change (true);
13642 /* do_pending_window_change could change the selected_window due to
13643 frame resizing which makes the selected window too small. */
13644 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13645 sw = w;
13647 /* Clear frames marked as garbaged. */
13648 clear_garbaged_frames ();
13650 /* Build menubar and tool-bar items. */
13651 if (NILP (Vmemory_full))
13652 prepare_menu_bars ();
13654 reconsider_clip_changes (w);
13656 /* In most cases selected window displays current buffer. */
13657 match_p = XBUFFER (w->contents) == current_buffer;
13658 if (match_p)
13660 /* Detect case that we need to write or remove a star in the mode line. */
13661 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13662 w->update_mode_line = true;
13664 if (mode_line_update_needed (w))
13665 w->update_mode_line = true;
13667 /* If reconsider_clip_changes above decided that the narrowing
13668 in the current buffer changed, make sure all other windows
13669 showing that buffer will be redisplayed. */
13670 if (current_buffer->clip_changed)
13671 bset_update_mode_line (current_buffer);
13674 /* Normally the message* functions will have already displayed and
13675 updated the echo area, but the frame may have been trashed, or
13676 the update may have been preempted, so display the echo area
13677 again here. Checking message_cleared_p captures the case that
13678 the echo area should be cleared. */
13679 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13680 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13681 || (message_cleared_p
13682 && minibuf_level == 0
13683 /* If the mini-window is currently selected, this means the
13684 echo-area doesn't show through. */
13685 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13687 echo_area_display (false);
13689 /* If echo_area_display resizes the mini-window, the redisplay and
13690 window_sizes_changed flags of the selected frame are set, but
13691 it's too late for the hooks in window-size-change-functions,
13692 which have been examined already in prepare_menu_bars. So in
13693 that case we call the hooks here only for the selected frame. */
13694 if (sf->redisplay)
13696 ptrdiff_t count1 = SPECPDL_INDEX ();
13698 record_unwind_save_match_data ();
13699 run_window_size_change_functions (selected_frame);
13700 unbind_to (count1, Qnil);
13703 if (message_cleared_p)
13704 update_miniwindow_p = true;
13706 must_finish = true;
13708 /* If we don't display the current message, don't clear the
13709 message_cleared_p flag, because, if we did, we wouldn't clear
13710 the echo area in the next redisplay which doesn't preserve
13711 the echo area. */
13712 if (!display_last_displayed_message_p)
13713 message_cleared_p = false;
13715 else if (EQ (selected_window, minibuf_window)
13716 && (current_buffer->clip_changed || window_outdated (w))
13717 && resize_mini_window (w, false))
13719 if (sf->redisplay)
13721 ptrdiff_t count1 = SPECPDL_INDEX ();
13723 record_unwind_save_match_data ();
13724 run_window_size_change_functions (selected_frame);
13725 unbind_to (count1, Qnil);
13728 /* Resized active mini-window to fit the size of what it is
13729 showing if its contents might have changed. */
13730 must_finish = true;
13732 /* If window configuration was changed, frames may have been
13733 marked garbaged. Clear them or we will experience
13734 surprises wrt scrolling. */
13735 clear_garbaged_frames ();
13738 if (windows_or_buffers_changed && !update_mode_lines)
13739 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13740 only the windows's contents needs to be refreshed, or whether the
13741 mode-lines also need a refresh. */
13742 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13743 ? REDISPLAY_SOME : 32);
13745 /* If specs for an arrow have changed, do thorough redisplay
13746 to ensure we remove any arrow that should no longer exist. */
13747 if (overlay_arrows_changed_p ())
13748 /* Apparently, this is the only case where we update other windows,
13749 without updating other mode-lines. */
13750 windows_or_buffers_changed = 49;
13752 consider_all_windows_p = (update_mode_lines
13753 || windows_or_buffers_changed);
13755 #define AINC(a,i) \
13757 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13758 if (INTEGERP (entry)) \
13759 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13762 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13763 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13765 /* Optimize the case that only the line containing the cursor in the
13766 selected window has changed. Variables starting with this_ are
13767 set in display_line and record information about the line
13768 containing the cursor. */
13769 tlbufpos = this_line_start_pos;
13770 tlendpos = this_line_end_pos;
13771 if (!consider_all_windows_p
13772 && CHARPOS (tlbufpos) > 0
13773 && !w->update_mode_line
13774 && !current_buffer->clip_changed
13775 && !current_buffer->prevent_redisplay_optimizations_p
13776 && FRAME_VISIBLE_P (XFRAME (w->frame))
13777 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13778 && !XFRAME (w->frame)->cursor_type_changed
13779 && !XFRAME (w->frame)->face_change
13780 /* Make sure recorded data applies to current buffer, etc. */
13781 && this_line_buffer == current_buffer
13782 && match_p
13783 && !w->force_start
13784 && !w->optional_new_start
13785 /* Point must be on the line that we have info recorded about. */
13786 && PT >= CHARPOS (tlbufpos)
13787 && PT <= Z - CHARPOS (tlendpos)
13788 /* All text outside that line, including its final newline,
13789 must be unchanged. */
13790 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13791 CHARPOS (tlendpos)))
13793 if (CHARPOS (tlbufpos) > BEGV
13794 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13795 && (CHARPOS (tlbufpos) == ZV
13796 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13797 /* Former continuation line has disappeared by becoming empty. */
13798 goto cancel;
13799 else if (window_outdated (w) || MINI_WINDOW_P (w))
13801 /* We have to handle the case of continuation around a
13802 wide-column character (see the comment in indent.c around
13803 line 1340).
13805 For instance, in the following case:
13807 -------- Insert --------
13808 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13809 J_I_ ==> J_I_ `^^' are cursors.
13810 ^^ ^^
13811 -------- --------
13813 As we have to redraw the line above, we cannot use this
13814 optimization. */
13816 struct it it;
13817 int line_height_before = this_line_pixel_height;
13819 /* Note that start_display will handle the case that the
13820 line starting at tlbufpos is a continuation line. */
13821 start_display (&it, w, tlbufpos);
13823 /* Implementation note: It this still necessary? */
13824 if (it.current_x != this_line_start_x)
13825 goto cancel;
13827 TRACE ((stderr, "trying display optimization 1\n"));
13828 w->cursor.vpos = -1;
13829 overlay_arrow_seen = false;
13830 it.vpos = this_line_vpos;
13831 it.current_y = this_line_y;
13832 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13833 display_line (&it);
13835 /* If line contains point, is not continued,
13836 and ends at same distance from eob as before, we win. */
13837 if (w->cursor.vpos >= 0
13838 /* Line is not continued, otherwise this_line_start_pos
13839 would have been set to 0 in display_line. */
13840 && CHARPOS (this_line_start_pos)
13841 /* Line ends as before. */
13842 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13843 /* Line has same height as before. Otherwise other lines
13844 would have to be shifted up or down. */
13845 && this_line_pixel_height == line_height_before)
13847 /* If this is not the window's last line, we must adjust
13848 the charstarts of the lines below. */
13849 if (it.current_y < it.last_visible_y)
13851 struct glyph_row *row
13852 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13853 ptrdiff_t delta, delta_bytes;
13855 /* We used to distinguish between two cases here,
13856 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13857 when the line ends in a newline or the end of the
13858 buffer's accessible portion. But both cases did
13859 the same, so they were collapsed. */
13860 delta = (Z
13861 - CHARPOS (tlendpos)
13862 - MATRIX_ROW_START_CHARPOS (row));
13863 delta_bytes = (Z_BYTE
13864 - BYTEPOS (tlendpos)
13865 - MATRIX_ROW_START_BYTEPOS (row));
13867 increment_matrix_positions (w->current_matrix,
13868 this_line_vpos + 1,
13869 w->current_matrix->nrows,
13870 delta, delta_bytes);
13873 /* If this row displays text now but previously didn't,
13874 or vice versa, w->window_end_vpos may have to be
13875 adjusted. */
13876 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13878 if (w->window_end_vpos < this_line_vpos)
13879 w->window_end_vpos = this_line_vpos;
13881 else if (w->window_end_vpos == this_line_vpos
13882 && this_line_vpos > 0)
13883 w->window_end_vpos = this_line_vpos - 1;
13884 w->window_end_valid = false;
13886 /* Update hint: No need to try to scroll in update_window. */
13887 w->desired_matrix->no_scrolling_p = true;
13889 #ifdef GLYPH_DEBUG
13890 *w->desired_matrix->method = 0;
13891 debug_method_add (w, "optimization 1");
13892 #endif
13893 #ifdef HAVE_WINDOW_SYSTEM
13894 update_window_fringes (w, false);
13895 #endif
13896 goto update;
13898 else
13899 goto cancel;
13901 else if (/* Cursor position hasn't changed. */
13902 PT == w->last_point
13903 /* Make sure the cursor was last displayed
13904 in this window. Otherwise we have to reposition it. */
13906 /* PXW: Must be converted to pixels, probably. */
13907 && 0 <= w->cursor.vpos
13908 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13910 if (!must_finish)
13912 do_pending_window_change (true);
13913 /* If selected_window changed, redisplay again. */
13914 if (WINDOWP (selected_window)
13915 && (w = XWINDOW (selected_window)) != sw)
13916 goto retry;
13918 /* We used to always goto end_of_redisplay here, but this
13919 isn't enough if we have a blinking cursor. */
13920 if (w->cursor_off_p == w->last_cursor_off_p)
13921 goto end_of_redisplay;
13923 goto update;
13925 /* If highlighting the region, or if the cursor is in the echo area,
13926 then we can't just move the cursor. */
13927 else if (NILP (Vshow_trailing_whitespace)
13928 && !cursor_in_echo_area)
13930 struct it it;
13931 struct glyph_row *row;
13933 /* Skip from tlbufpos to PT and see where it is. Note that
13934 PT may be in invisible text. If so, we will end at the
13935 next visible position. */
13936 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13937 NULL, DEFAULT_FACE_ID);
13938 it.current_x = this_line_start_x;
13939 it.current_y = this_line_y;
13940 it.vpos = this_line_vpos;
13942 /* The call to move_it_to stops in front of PT, but
13943 moves over before-strings. */
13944 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13946 if (it.vpos == this_line_vpos
13947 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13948 row->enabled_p))
13950 eassert (this_line_vpos == it.vpos);
13951 eassert (this_line_y == it.current_y);
13952 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13953 if (cursor_row_fully_visible_p (w, false, true))
13955 #ifdef GLYPH_DEBUG
13956 *w->desired_matrix->method = 0;
13957 debug_method_add (w, "optimization 3");
13958 #endif
13959 goto update;
13961 else
13962 goto cancel;
13964 else
13965 goto cancel;
13968 cancel:
13969 /* Text changed drastically or point moved off of line. */
13970 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13973 CHARPOS (this_line_start_pos) = 0;
13974 ++clear_face_cache_count;
13975 #ifdef HAVE_WINDOW_SYSTEM
13976 ++clear_image_cache_count;
13977 #endif
13979 /* Build desired matrices, and update the display. If
13980 consider_all_windows_p, do it for all windows on all frames that
13981 require redisplay, as specified by their 'redisplay' flag.
13982 Otherwise do it for selected_window, only. */
13984 if (consider_all_windows_p)
13986 FOR_EACH_FRAME (tail, frame)
13987 XFRAME (frame)->updated_p = false;
13989 propagate_buffer_redisplay ();
13991 FOR_EACH_FRAME (tail, frame)
13993 struct frame *f = XFRAME (frame);
13995 /* We don't have to do anything for unselected terminal
13996 frames. */
13997 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13998 && !EQ (FRAME_TTY (f)->top_frame, frame))
13999 continue;
14001 retry_frame:
14002 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14004 bool gcscrollbars
14005 /* Only GC scrollbars when we redisplay the whole frame. */
14006 = f->redisplay || !REDISPLAY_SOME_P ();
14007 bool f_redisplay_flag = f->redisplay;
14008 /* Mark all the scroll bars to be removed; we'll redeem
14009 the ones we want when we redisplay their windows. */
14010 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14011 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14013 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14014 redisplay_windows (FRAME_ROOT_WINDOW (f));
14015 /* Remember that the invisible frames need to be redisplayed next
14016 time they're visible. */
14017 else if (!REDISPLAY_SOME_P ())
14018 f->redisplay = true;
14020 /* The X error handler may have deleted that frame. */
14021 if (!FRAME_LIVE_P (f))
14022 continue;
14024 /* Any scroll bars which redisplay_windows should have
14025 nuked should now go away. */
14026 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14027 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14029 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14031 /* If fonts changed on visible frame, display again. */
14032 if (f->fonts_changed)
14034 adjust_frame_glyphs (f);
14035 /* Disable all redisplay optimizations for this
14036 frame. For the reasons, see the comment near
14037 the previous call to adjust_frame_glyphs above. */
14038 SET_FRAME_GARBAGED (f);
14039 f->fonts_changed = false;
14040 goto retry_frame;
14043 /* See if we have to hscroll. */
14044 if (!f->already_hscrolled_p)
14046 f->already_hscrolled_p = true;
14047 if (hscroll_windows (f->root_window))
14048 goto retry_frame;
14051 /* If the frame's redisplay flag was not set before
14052 we went about redisplaying its windows, but it is
14053 set now, that means we employed some redisplay
14054 optimizations inside redisplay_windows, and
14055 bypassed producing some screen lines. But if
14056 f->redisplay is now set, it might mean the old
14057 faces are no longer valid (e.g., if redisplaying
14058 some window called some Lisp which defined a new
14059 face or redefined an existing face), so trying to
14060 use them in update_frame will segfault.
14061 Therefore, we must redisplay this frame. */
14062 if (!f_redisplay_flag && f->redisplay)
14063 goto retry_frame;
14065 /* Prevent various kinds of signals during display
14066 update. stdio is not robust about handling
14067 signals, which can cause an apparent I/O error. */
14068 if (interrupt_input)
14069 unrequest_sigio ();
14070 STOP_POLLING;
14072 pending |= update_frame (f, false, false);
14073 f->cursor_type_changed = false;
14074 f->updated_p = true;
14079 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14081 if (!pending)
14083 /* Do the mark_window_display_accurate after all windows have
14084 been redisplayed because this call resets flags in buffers
14085 which are needed for proper redisplay. */
14086 FOR_EACH_FRAME (tail, frame)
14088 struct frame *f = XFRAME (frame);
14089 if (f->updated_p)
14091 f->redisplay = false;
14092 mark_window_display_accurate (f->root_window, true);
14093 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14094 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14099 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14101 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14102 /* Use list_of_error, not Qerror, so that
14103 we catch only errors and don't run the debugger. */
14104 internal_condition_case_1 (redisplay_window_1, selected_window,
14105 list_of_error,
14106 redisplay_window_error);
14107 if (update_miniwindow_p)
14108 internal_condition_case_1 (redisplay_window_1,
14109 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14110 redisplay_window_error);
14112 /* Compare desired and current matrices, perform output. */
14114 update:
14115 /* If fonts changed, display again. Likewise if redisplay_window_1
14116 above caused some change (e.g., a change in faces) that requires
14117 considering the entire frame again. */
14118 if (sf->fonts_changed || sf->redisplay)
14120 if (sf->redisplay)
14122 /* Set this to force a more thorough redisplay.
14123 Otherwise, we might immediately loop back to the
14124 above "else-if" clause (since all the conditions that
14125 led here might still be true), and we will then
14126 infloop, because the selected-frame's redisplay flag
14127 is not (and cannot be) reset. */
14128 windows_or_buffers_changed = 50;
14130 goto retry;
14133 /* Prevent freeing of realized faces, since desired matrices are
14134 pending that reference the faces we computed and cached. */
14135 inhibit_free_realized_faces = true;
14137 /* Prevent various kinds of signals during display update.
14138 stdio is not robust about handling signals,
14139 which can cause an apparent I/O error. */
14140 if (interrupt_input)
14141 unrequest_sigio ();
14142 STOP_POLLING;
14144 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14146 if (hscroll_windows (selected_window))
14147 goto retry;
14149 XWINDOW (selected_window)->must_be_updated_p = true;
14150 pending = update_frame (sf, false, false);
14151 sf->cursor_type_changed = false;
14154 /* We may have called echo_area_display at the top of this
14155 function. If the echo area is on another frame, that may
14156 have put text on a frame other than the selected one, so the
14157 above call to update_frame would not have caught it. Catch
14158 it here. */
14159 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14160 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14162 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14164 XWINDOW (mini_window)->must_be_updated_p = true;
14165 pending |= update_frame (mini_frame, false, false);
14166 mini_frame->cursor_type_changed = false;
14167 if (!pending && hscroll_windows (mini_window))
14168 goto retry;
14172 /* If display was paused because of pending input, make sure we do a
14173 thorough update the next time. */
14174 if (pending)
14176 /* Prevent the optimization at the beginning of
14177 redisplay_internal that tries a single-line update of the
14178 line containing the cursor in the selected window. */
14179 CHARPOS (this_line_start_pos) = 0;
14181 /* Let the overlay arrow be updated the next time. */
14182 update_overlay_arrows (0);
14184 /* If we pause after scrolling, some rows in the current
14185 matrices of some windows are not valid. */
14186 if (!WINDOW_FULL_WIDTH_P (w)
14187 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14188 update_mode_lines = 36;
14190 else
14192 if (!consider_all_windows_p)
14194 /* This has already been done above if
14195 consider_all_windows_p is set. */
14196 if (XBUFFER (w->contents)->text->redisplay
14197 && buffer_window_count (XBUFFER (w->contents)) > 1)
14198 /* This can happen if b->text->redisplay was set during
14199 jit-lock. */
14200 propagate_buffer_redisplay ();
14201 mark_window_display_accurate_1 (w, true);
14203 /* Say overlay arrows are up to date. */
14204 update_overlay_arrows (1);
14206 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14207 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14210 update_mode_lines = 0;
14211 windows_or_buffers_changed = 0;
14214 /* Start SIGIO interrupts coming again. Having them off during the
14215 code above makes it less likely one will discard output, but not
14216 impossible, since there might be stuff in the system buffer here.
14217 But it is much hairier to try to do anything about that. */
14218 if (interrupt_input)
14219 request_sigio ();
14220 RESUME_POLLING;
14222 /* If a frame has become visible which was not before, redisplay
14223 again, so that we display it. Expose events for such a frame
14224 (which it gets when becoming visible) don't call the parts of
14225 redisplay constructing glyphs, so simply exposing a frame won't
14226 display anything in this case. So, we have to display these
14227 frames here explicitly. */
14228 if (!pending)
14230 int new_count = 0;
14232 FOR_EACH_FRAME (tail, frame)
14234 if (XFRAME (frame)->visible)
14235 new_count++;
14238 if (new_count != number_of_visible_frames)
14239 windows_or_buffers_changed = 52;
14242 /* Change frame size now if a change is pending. */
14243 do_pending_window_change (true);
14245 /* If we just did a pending size change, or have additional
14246 visible frames, or selected_window changed, redisplay again. */
14247 if ((windows_or_buffers_changed && !pending)
14248 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14249 goto retry;
14251 /* Clear the face and image caches.
14253 We used to do this only if consider_all_windows_p. But the cache
14254 needs to be cleared if a timer creates images in the current
14255 buffer (e.g. the test case in Bug#6230). */
14257 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14259 clear_face_cache (false);
14260 clear_face_cache_count = 0;
14263 #ifdef HAVE_WINDOW_SYSTEM
14264 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14266 clear_image_caches (Qnil);
14267 clear_image_cache_count = 0;
14269 #endif /* HAVE_WINDOW_SYSTEM */
14271 end_of_redisplay:
14272 #ifdef HAVE_NS
14273 ns_set_doc_edited ();
14274 #endif
14275 if (interrupt_input && interrupts_deferred)
14276 request_sigio ();
14278 unbind_to (count, Qnil);
14279 RESUME_POLLING;
14283 /* Redisplay, but leave alone any recent echo area message unless
14284 another message has been requested in its place.
14286 This is useful in situations where you need to redisplay but no
14287 user action has occurred, making it inappropriate for the message
14288 area to be cleared. See tracking_off and
14289 wait_reading_process_output for examples of these situations.
14291 FROM_WHERE is an integer saying from where this function was
14292 called. This is useful for debugging. */
14294 void
14295 redisplay_preserve_echo_area (int from_where)
14297 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14299 if (!NILP (echo_area_buffer[1]))
14301 /* We have a previously displayed message, but no current
14302 message. Redisplay the previous message. */
14303 display_last_displayed_message_p = true;
14304 redisplay_internal ();
14305 display_last_displayed_message_p = false;
14307 else
14308 redisplay_internal ();
14310 flush_frame (SELECTED_FRAME ());
14314 /* Function registered with record_unwind_protect in redisplay_internal. */
14316 static void
14317 unwind_redisplay (void)
14319 redisplaying_p = false;
14323 /* Mark the display of leaf window W as accurate or inaccurate.
14324 If ACCURATE_P, mark display of W as accurate.
14325 If !ACCURATE_P, arrange for W to be redisplayed the next
14326 time redisplay_internal is called. */
14328 static void
14329 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14331 struct buffer *b = XBUFFER (w->contents);
14333 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14334 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14335 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14337 if (accurate_p)
14339 b->clip_changed = false;
14340 b->prevent_redisplay_optimizations_p = false;
14341 eassert (buffer_window_count (b) > 0);
14342 /* Resetting b->text->redisplay is problematic!
14343 In order to make it safer to do it here, redisplay_internal must
14344 have copied all b->text->redisplay to their respective windows. */
14345 b->text->redisplay = false;
14347 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14348 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14349 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14350 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14352 w->current_matrix->buffer = b;
14353 w->current_matrix->begv = BUF_BEGV (b);
14354 w->current_matrix->zv = BUF_ZV (b);
14356 w->last_cursor_vpos = w->cursor.vpos;
14357 w->last_cursor_off_p = w->cursor_off_p;
14359 if (w == XWINDOW (selected_window))
14360 w->last_point = BUF_PT (b);
14361 else
14362 w->last_point = marker_position (w->pointm);
14364 w->window_end_valid = true;
14365 w->update_mode_line = false;
14368 w->redisplay = !accurate_p;
14372 /* Mark the display of windows in the window tree rooted at WINDOW as
14373 accurate or inaccurate. If ACCURATE_P, mark display of
14374 windows as accurate. If !ACCURATE_P, arrange for windows to
14375 be redisplayed the next time redisplay_internal is called. */
14377 void
14378 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14380 struct window *w;
14382 for (; !NILP (window); window = w->next)
14384 w = XWINDOW (window);
14385 if (WINDOWP (w->contents))
14386 mark_window_display_accurate (w->contents, accurate_p);
14387 else
14388 mark_window_display_accurate_1 (w, accurate_p);
14391 if (accurate_p)
14392 update_overlay_arrows (1);
14393 else
14394 /* Force a thorough redisplay the next time by setting
14395 last_arrow_position and last_arrow_string to t, which is
14396 unequal to any useful value of Voverlay_arrow_... */
14397 update_overlay_arrows (-1);
14401 /* Return value in display table DP (Lisp_Char_Table *) for character
14402 C. Since a display table doesn't have any parent, we don't have to
14403 follow parent. Do not call this function directly but use the
14404 macro DISP_CHAR_VECTOR. */
14406 Lisp_Object
14407 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14409 Lisp_Object val;
14411 if (ASCII_CHAR_P (c))
14413 val = dp->ascii;
14414 if (SUB_CHAR_TABLE_P (val))
14415 val = XSUB_CHAR_TABLE (val)->contents[c];
14417 else
14419 Lisp_Object table;
14421 XSETCHAR_TABLE (table, dp);
14422 val = char_table_ref (table, c);
14424 if (NILP (val))
14425 val = dp->defalt;
14426 return val;
14431 /***********************************************************************
14432 Window Redisplay
14433 ***********************************************************************/
14435 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14437 static void
14438 redisplay_windows (Lisp_Object window)
14440 while (!NILP (window))
14442 struct window *w = XWINDOW (window);
14444 if (WINDOWP (w->contents))
14445 redisplay_windows (w->contents);
14446 else if (BUFFERP (w->contents))
14448 displayed_buffer = XBUFFER (w->contents);
14449 /* Use list_of_error, not Qerror, so that
14450 we catch only errors and don't run the debugger. */
14451 internal_condition_case_1 (redisplay_window_0, window,
14452 list_of_error,
14453 redisplay_window_error);
14456 window = w->next;
14460 static Lisp_Object
14461 redisplay_window_error (Lisp_Object ignore)
14463 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14464 return Qnil;
14467 static Lisp_Object
14468 redisplay_window_0 (Lisp_Object window)
14470 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14471 redisplay_window (window, false);
14472 return Qnil;
14475 static Lisp_Object
14476 redisplay_window_1 (Lisp_Object window)
14478 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14479 redisplay_window (window, true);
14480 return Qnil;
14484 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14485 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14486 which positions recorded in ROW differ from current buffer
14487 positions.
14489 Return true iff cursor is on this row. */
14491 static bool
14492 set_cursor_from_row (struct window *w, struct glyph_row *row,
14493 struct glyph_matrix *matrix,
14494 ptrdiff_t delta, ptrdiff_t delta_bytes,
14495 int dy, int dvpos)
14497 struct glyph *glyph = row->glyphs[TEXT_AREA];
14498 struct glyph *end = glyph + row->used[TEXT_AREA];
14499 struct glyph *cursor = NULL;
14500 /* The last known character position in row. */
14501 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14502 int x = row->x;
14503 ptrdiff_t pt_old = PT - delta;
14504 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14505 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14506 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14507 /* A glyph beyond the edge of TEXT_AREA which we should never
14508 touch. */
14509 struct glyph *glyphs_end = end;
14510 /* True means we've found a match for cursor position, but that
14511 glyph has the avoid_cursor_p flag set. */
14512 bool match_with_avoid_cursor = false;
14513 /* True means we've seen at least one glyph that came from a
14514 display string. */
14515 bool string_seen = false;
14516 /* Largest and smallest buffer positions seen so far during scan of
14517 glyph row. */
14518 ptrdiff_t bpos_max = pos_before;
14519 ptrdiff_t bpos_min = pos_after;
14520 /* Last buffer position covered by an overlay string with an integer
14521 `cursor' property. */
14522 ptrdiff_t bpos_covered = 0;
14523 /* True means the display string on which to display the cursor
14524 comes from a text property, not from an overlay. */
14525 bool string_from_text_prop = false;
14527 /* Don't even try doing anything if called for a mode-line or
14528 header-line row, since the rest of the code isn't prepared to
14529 deal with such calamities. */
14530 eassert (!row->mode_line_p);
14531 if (row->mode_line_p)
14532 return false;
14534 /* Skip over glyphs not having an object at the start and the end of
14535 the row. These are special glyphs like truncation marks on
14536 terminal frames. */
14537 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14539 if (!row->reversed_p)
14541 while (glyph < end
14542 && NILP (glyph->object)
14543 && glyph->charpos < 0)
14545 x += glyph->pixel_width;
14546 ++glyph;
14548 while (end > glyph
14549 && NILP ((end - 1)->object)
14550 /* CHARPOS is zero for blanks and stretch glyphs
14551 inserted by extend_face_to_end_of_line. */
14552 && (end - 1)->charpos <= 0)
14553 --end;
14554 glyph_before = glyph - 1;
14555 glyph_after = end;
14557 else
14559 struct glyph *g;
14561 /* If the glyph row is reversed, we need to process it from back
14562 to front, so swap the edge pointers. */
14563 glyphs_end = end = glyph - 1;
14564 glyph += row->used[TEXT_AREA] - 1;
14566 while (glyph > end + 1
14567 && NILP (glyph->object)
14568 && glyph->charpos < 0)
14570 --glyph;
14571 x -= glyph->pixel_width;
14573 if (NILP (glyph->object) && glyph->charpos < 0)
14574 --glyph;
14575 /* By default, in reversed rows we put the cursor on the
14576 rightmost (first in the reading order) glyph. */
14577 for (g = end + 1; g < glyph; g++)
14578 x += g->pixel_width;
14579 while (end < glyph
14580 && NILP ((end + 1)->object)
14581 && (end + 1)->charpos <= 0)
14582 ++end;
14583 glyph_before = glyph + 1;
14584 glyph_after = end;
14587 else if (row->reversed_p)
14589 /* In R2L rows that don't display text, put the cursor on the
14590 rightmost glyph. Case in point: an empty last line that is
14591 part of an R2L paragraph. */
14592 cursor = end - 1;
14593 /* Avoid placing the cursor on the last glyph of the row, where
14594 on terminal frames we hold the vertical border between
14595 adjacent windows. */
14596 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14597 && !WINDOW_RIGHTMOST_P (w)
14598 && cursor == row->glyphs[LAST_AREA] - 1)
14599 cursor--;
14600 x = -1; /* will be computed below, at label compute_x */
14603 /* Step 1: Try to find the glyph whose character position
14604 corresponds to point. If that's not possible, find 2 glyphs
14605 whose character positions are the closest to point, one before
14606 point, the other after it. */
14607 if (!row->reversed_p)
14608 while (/* not marched to end of glyph row */
14609 glyph < end
14610 /* glyph was not inserted by redisplay for internal purposes */
14611 && !NILP (glyph->object))
14613 if (BUFFERP (glyph->object))
14615 ptrdiff_t dpos = glyph->charpos - pt_old;
14617 if (glyph->charpos > bpos_max)
14618 bpos_max = glyph->charpos;
14619 if (glyph->charpos < bpos_min)
14620 bpos_min = glyph->charpos;
14621 if (!glyph->avoid_cursor_p)
14623 /* If we hit point, we've found the glyph on which to
14624 display the cursor. */
14625 if (dpos == 0)
14627 match_with_avoid_cursor = false;
14628 break;
14630 /* See if we've found a better approximation to
14631 POS_BEFORE or to POS_AFTER. */
14632 if (0 > dpos && dpos > pos_before - pt_old)
14634 pos_before = glyph->charpos;
14635 glyph_before = glyph;
14637 else if (0 < dpos && dpos < pos_after - pt_old)
14639 pos_after = glyph->charpos;
14640 glyph_after = glyph;
14643 else if (dpos == 0)
14644 match_with_avoid_cursor = true;
14646 else if (STRINGP (glyph->object))
14648 Lisp_Object chprop;
14649 ptrdiff_t glyph_pos = glyph->charpos;
14651 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14652 glyph->object);
14653 if (!NILP (chprop))
14655 /* If the string came from a `display' text property,
14656 look up the buffer position of that property and
14657 use that position to update bpos_max, as if we
14658 actually saw such a position in one of the row's
14659 glyphs. This helps with supporting integer values
14660 of `cursor' property on the display string in
14661 situations where most or all of the row's buffer
14662 text is completely covered by display properties,
14663 so that no glyph with valid buffer positions is
14664 ever seen in the row. */
14665 ptrdiff_t prop_pos =
14666 string_buffer_position_lim (glyph->object, pos_before,
14667 pos_after, false);
14669 if (prop_pos >= pos_before)
14670 bpos_max = prop_pos;
14672 if (INTEGERP (chprop))
14674 bpos_covered = bpos_max + XINT (chprop);
14675 /* If the `cursor' property covers buffer positions up
14676 to and including point, we should display cursor on
14677 this glyph. Note that, if a `cursor' property on one
14678 of the string's characters has an integer value, we
14679 will break out of the loop below _before_ we get to
14680 the position match above. IOW, integer values of
14681 the `cursor' property override the "exact match for
14682 point" strategy of positioning the cursor. */
14683 /* Implementation note: bpos_max == pt_old when, e.g.,
14684 we are in an empty line, where bpos_max is set to
14685 MATRIX_ROW_START_CHARPOS, see above. */
14686 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14688 cursor = glyph;
14689 break;
14693 string_seen = true;
14695 x += glyph->pixel_width;
14696 ++glyph;
14698 else if (glyph > end) /* row is reversed */
14699 while (!NILP (glyph->object))
14701 if (BUFFERP (glyph->object))
14703 ptrdiff_t dpos = glyph->charpos - pt_old;
14705 if (glyph->charpos > bpos_max)
14706 bpos_max = glyph->charpos;
14707 if (glyph->charpos < bpos_min)
14708 bpos_min = glyph->charpos;
14709 if (!glyph->avoid_cursor_p)
14711 if (dpos == 0)
14713 match_with_avoid_cursor = false;
14714 break;
14716 if (0 > dpos && dpos > pos_before - pt_old)
14718 pos_before = glyph->charpos;
14719 glyph_before = glyph;
14721 else if (0 < dpos && dpos < pos_after - pt_old)
14723 pos_after = glyph->charpos;
14724 glyph_after = glyph;
14727 else if (dpos == 0)
14728 match_with_avoid_cursor = true;
14730 else if (STRINGP (glyph->object))
14732 Lisp_Object chprop;
14733 ptrdiff_t glyph_pos = glyph->charpos;
14735 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14736 glyph->object);
14737 if (!NILP (chprop))
14739 ptrdiff_t prop_pos =
14740 string_buffer_position_lim (glyph->object, pos_before,
14741 pos_after, false);
14743 if (prop_pos >= pos_before)
14744 bpos_max = prop_pos;
14746 if (INTEGERP (chprop))
14748 bpos_covered = bpos_max + XINT (chprop);
14749 /* If the `cursor' property covers buffer positions up
14750 to and including point, we should display cursor on
14751 this glyph. */
14752 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14754 cursor = glyph;
14755 break;
14758 string_seen = true;
14760 --glyph;
14761 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14763 x--; /* can't use any pixel_width */
14764 break;
14766 x -= glyph->pixel_width;
14769 /* Step 2: If we didn't find an exact match for point, we need to
14770 look for a proper place to put the cursor among glyphs between
14771 GLYPH_BEFORE and GLYPH_AFTER. */
14772 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14773 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14774 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14776 /* An empty line has a single glyph whose OBJECT is nil and
14777 whose CHARPOS is the position of a newline on that line.
14778 Note that on a TTY, there are more glyphs after that, which
14779 were produced by extend_face_to_end_of_line, but their
14780 CHARPOS is zero or negative. */
14781 bool empty_line_p =
14782 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14783 && NILP (glyph->object) && glyph->charpos > 0
14784 /* On a TTY, continued and truncated rows also have a glyph at
14785 their end whose OBJECT is nil and whose CHARPOS is
14786 positive (the continuation and truncation glyphs), but such
14787 rows are obviously not "empty". */
14788 && !(row->continued_p || row->truncated_on_right_p));
14790 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14792 ptrdiff_t ellipsis_pos;
14794 /* Scan back over the ellipsis glyphs. */
14795 if (!row->reversed_p)
14797 ellipsis_pos = (glyph - 1)->charpos;
14798 while (glyph > row->glyphs[TEXT_AREA]
14799 && (glyph - 1)->charpos == ellipsis_pos)
14800 glyph--, x -= glyph->pixel_width;
14801 /* That loop always goes one position too far, including
14802 the glyph before the ellipsis. So scan forward over
14803 that one. */
14804 x += glyph->pixel_width;
14805 glyph++;
14807 else /* row is reversed */
14809 ellipsis_pos = (glyph + 1)->charpos;
14810 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14811 && (glyph + 1)->charpos == ellipsis_pos)
14812 glyph++, x += glyph->pixel_width;
14813 x -= glyph->pixel_width;
14814 glyph--;
14817 else if (match_with_avoid_cursor)
14819 cursor = glyph_after;
14820 x = -1;
14822 else if (string_seen)
14824 int incr = row->reversed_p ? -1 : +1;
14826 /* Need to find the glyph that came out of a string which is
14827 present at point. That glyph is somewhere between
14828 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14829 positioned between POS_BEFORE and POS_AFTER in the
14830 buffer. */
14831 struct glyph *start, *stop;
14832 ptrdiff_t pos = pos_before;
14834 x = -1;
14836 /* If the row ends in a newline from a display string,
14837 reordering could have moved the glyphs belonging to the
14838 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14839 in this case we extend the search to the last glyph in
14840 the row that was not inserted by redisplay. */
14841 if (row->ends_in_newline_from_string_p)
14843 glyph_after = end;
14844 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14847 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14848 correspond to POS_BEFORE and POS_AFTER, respectively. We
14849 need START and STOP in the order that corresponds to the
14850 row's direction as given by its reversed_p flag. If the
14851 directionality of characters between POS_BEFORE and
14852 POS_AFTER is the opposite of the row's base direction,
14853 these characters will have been reordered for display,
14854 and we need to reverse START and STOP. */
14855 if (!row->reversed_p)
14857 start = min (glyph_before, glyph_after);
14858 stop = max (glyph_before, glyph_after);
14860 else
14862 start = max (glyph_before, glyph_after);
14863 stop = min (glyph_before, glyph_after);
14865 for (glyph = start + incr;
14866 row->reversed_p ? glyph > stop : glyph < stop; )
14869 /* Any glyphs that come from the buffer are here because
14870 of bidi reordering. Skip them, and only pay
14871 attention to glyphs that came from some string. */
14872 if (STRINGP (glyph->object))
14874 Lisp_Object str;
14875 ptrdiff_t tem;
14876 /* If the display property covers the newline, we
14877 need to search for it one position farther. */
14878 ptrdiff_t lim = pos_after
14879 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14881 string_from_text_prop = false;
14882 str = glyph->object;
14883 tem = string_buffer_position_lim (str, pos, lim, false);
14884 if (tem == 0 /* from overlay */
14885 || pos <= tem)
14887 /* If the string from which this glyph came is
14888 found in the buffer at point, or at position
14889 that is closer to point than pos_after, then
14890 we've found the glyph we've been looking for.
14891 If it comes from an overlay (tem == 0), and
14892 it has the `cursor' property on one of its
14893 glyphs, record that glyph as a candidate for
14894 displaying the cursor. (As in the
14895 unidirectional version, we will display the
14896 cursor on the last candidate we find.) */
14897 if (tem == 0
14898 || tem == pt_old
14899 || (tem - pt_old > 0 && tem < pos_after))
14901 /* The glyphs from this string could have
14902 been reordered. Find the one with the
14903 smallest string position. Or there could
14904 be a character in the string with the
14905 `cursor' property, which means display
14906 cursor on that character's glyph. */
14907 ptrdiff_t strpos = glyph->charpos;
14909 if (tem)
14911 cursor = glyph;
14912 string_from_text_prop = true;
14914 for ( ;
14915 (row->reversed_p ? glyph > stop : glyph < stop)
14916 && EQ (glyph->object, str);
14917 glyph += incr)
14919 Lisp_Object cprop;
14920 ptrdiff_t gpos = glyph->charpos;
14922 cprop = Fget_char_property (make_number (gpos),
14923 Qcursor,
14924 glyph->object);
14925 if (!NILP (cprop))
14927 cursor = glyph;
14928 break;
14930 if (tem && glyph->charpos < strpos)
14932 strpos = glyph->charpos;
14933 cursor = glyph;
14937 if (tem == pt_old
14938 || (tem - pt_old > 0 && tem < pos_after))
14939 goto compute_x;
14941 if (tem)
14942 pos = tem + 1; /* don't find previous instances */
14944 /* This string is not what we want; skip all of the
14945 glyphs that came from it. */
14946 while ((row->reversed_p ? glyph > stop : glyph < stop)
14947 && EQ (glyph->object, str))
14948 glyph += incr;
14950 else
14951 glyph += incr;
14954 /* If we reached the end of the line, and END was from a string,
14955 the cursor is not on this line. */
14956 if (cursor == NULL
14957 && (row->reversed_p ? glyph <= end : glyph >= end)
14958 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14959 && STRINGP (end->object)
14960 && row->continued_p)
14961 return false;
14963 /* A truncated row may not include PT among its character positions.
14964 Setting the cursor inside the scroll margin will trigger
14965 recalculation of hscroll in hscroll_window_tree. But if a
14966 display string covers point, defer to the string-handling
14967 code below to figure this out. */
14968 else if (row->truncated_on_left_p && pt_old < bpos_min)
14970 cursor = glyph_before;
14971 x = -1;
14973 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14974 /* Zero-width characters produce no glyphs. */
14975 || (!empty_line_p
14976 && (row->reversed_p
14977 ? glyph_after > glyphs_end
14978 : glyph_after < glyphs_end)))
14980 cursor = glyph_after;
14981 x = -1;
14985 compute_x:
14986 if (cursor != NULL)
14987 glyph = cursor;
14988 else if (glyph == glyphs_end
14989 && pos_before == pos_after
14990 && STRINGP ((row->reversed_p
14991 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14992 : row->glyphs[TEXT_AREA])->object))
14994 /* If all the glyphs of this row came from strings, put the
14995 cursor on the first glyph of the row. This avoids having the
14996 cursor outside of the text area in this very rare and hard
14997 use case. */
14998 glyph =
14999 row->reversed_p
15000 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15001 : row->glyphs[TEXT_AREA];
15003 if (x < 0)
15005 struct glyph *g;
15007 /* Need to compute x that corresponds to GLYPH. */
15008 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15010 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15011 emacs_abort ();
15012 x += g->pixel_width;
15016 /* ROW could be part of a continued line, which, under bidi
15017 reordering, might have other rows whose start and end charpos
15018 occlude point. Only set w->cursor if we found a better
15019 approximation to the cursor position than we have from previously
15020 examined candidate rows belonging to the same continued line. */
15021 if (/* We already have a candidate row. */
15022 w->cursor.vpos >= 0
15023 /* That candidate is not the row we are processing. */
15024 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15025 /* Make sure cursor.vpos specifies a row whose start and end
15026 charpos occlude point, and it is valid candidate for being a
15027 cursor-row. This is because some callers of this function
15028 leave cursor.vpos at the row where the cursor was displayed
15029 during the last redisplay cycle. */
15030 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15031 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15032 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15034 struct glyph *g1
15035 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15037 /* Don't consider glyphs that are outside TEXT_AREA. */
15038 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15039 return false;
15040 /* Keep the candidate whose buffer position is the closest to
15041 point or has the `cursor' property. */
15042 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15043 w->cursor.hpos >= 0
15044 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15045 && ((BUFFERP (g1->object)
15046 && (g1->charpos == pt_old /* An exact match always wins. */
15047 || (BUFFERP (glyph->object)
15048 && eabs (g1->charpos - pt_old)
15049 < eabs (glyph->charpos - pt_old))))
15050 /* Previous candidate is a glyph from a string that has
15051 a non-nil `cursor' property. */
15052 || (STRINGP (g1->object)
15053 && (!NILP (Fget_char_property (make_number (g1->charpos),
15054 Qcursor, g1->object))
15055 /* Previous candidate is from the same display
15056 string as this one, and the display string
15057 came from a text property. */
15058 || (EQ (g1->object, glyph->object)
15059 && string_from_text_prop)
15060 /* this candidate is from newline and its
15061 position is not an exact match */
15062 || (NILP (glyph->object)
15063 && glyph->charpos != pt_old)))))
15064 return false;
15065 /* If this candidate gives an exact match, use that. */
15066 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15067 /* If this candidate is a glyph created for the
15068 terminating newline of a line, and point is on that
15069 newline, it wins because it's an exact match. */
15070 || (!row->continued_p
15071 && NILP (glyph->object)
15072 && glyph->charpos == 0
15073 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15074 /* Otherwise, keep the candidate that comes from a row
15075 spanning less buffer positions. This may win when one or
15076 both candidate positions are on glyphs that came from
15077 display strings, for which we cannot compare buffer
15078 positions. */
15079 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15080 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15081 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15082 return false;
15084 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15085 w->cursor.x = x;
15086 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15087 w->cursor.y = row->y + dy;
15089 if (w == XWINDOW (selected_window))
15091 if (!row->continued_p
15092 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15093 && row->x == 0)
15095 this_line_buffer = XBUFFER (w->contents);
15097 CHARPOS (this_line_start_pos)
15098 = MATRIX_ROW_START_CHARPOS (row) + delta;
15099 BYTEPOS (this_line_start_pos)
15100 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15102 CHARPOS (this_line_end_pos)
15103 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15104 BYTEPOS (this_line_end_pos)
15105 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15107 this_line_y = w->cursor.y;
15108 this_line_pixel_height = row->height;
15109 this_line_vpos = w->cursor.vpos;
15110 this_line_start_x = row->x;
15112 else
15113 CHARPOS (this_line_start_pos) = 0;
15116 return true;
15120 /* Run window scroll functions, if any, for WINDOW with new window
15121 start STARTP. Sets the window start of WINDOW to that position.
15123 We assume that the window's buffer is really current. */
15125 static struct text_pos
15126 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15128 struct window *w = XWINDOW (window);
15129 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15131 eassert (current_buffer == XBUFFER (w->contents));
15133 if (!NILP (Vwindow_scroll_functions))
15135 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15136 make_number (CHARPOS (startp)));
15137 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15138 /* In case the hook functions switch buffers. */
15139 set_buffer_internal (XBUFFER (w->contents));
15142 return startp;
15146 /* Make sure the line containing the cursor is fully visible.
15147 A value of true means there is nothing to be done.
15148 (Either the line is fully visible, or it cannot be made so,
15149 or we cannot tell.)
15151 If FORCE_P, return false even if partial visible cursor row
15152 is higher than window.
15154 If CURRENT_MATRIX_P, use the information from the
15155 window's current glyph matrix; otherwise use the desired glyph
15156 matrix.
15158 A value of false means the caller should do scrolling
15159 as if point had gone off the screen. */
15161 static bool
15162 cursor_row_fully_visible_p (struct window *w, bool force_p,
15163 bool current_matrix_p)
15165 struct glyph_matrix *matrix;
15166 struct glyph_row *row;
15167 int window_height;
15169 if (!make_cursor_line_fully_visible_p)
15170 return true;
15172 /* It's not always possible to find the cursor, e.g, when a window
15173 is full of overlay strings. Don't do anything in that case. */
15174 if (w->cursor.vpos < 0)
15175 return true;
15177 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15178 row = MATRIX_ROW (matrix, w->cursor.vpos);
15180 /* If the cursor row is not partially visible, there's nothing to do. */
15181 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15182 return true;
15184 /* If the row the cursor is in is taller than the window's height,
15185 it's not clear what to do, so do nothing. */
15186 window_height = window_box_height (w);
15187 if (row->height >= window_height)
15189 if (!force_p || MINI_WINDOW_P (w)
15190 || w->vscroll || w->cursor.vpos == 0)
15191 return true;
15193 return false;
15197 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15198 means only WINDOW is redisplayed in redisplay_internal.
15199 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15200 in redisplay_window to bring a partially visible line into view in
15201 the case that only the cursor has moved.
15203 LAST_LINE_MISFIT should be true if we're scrolling because the
15204 last screen line's vertical height extends past the end of the screen.
15206 Value is
15208 1 if scrolling succeeded
15210 0 if scrolling didn't find point.
15212 -1 if new fonts have been loaded so that we must interrupt
15213 redisplay, adjust glyph matrices, and try again. */
15215 enum
15217 SCROLLING_SUCCESS,
15218 SCROLLING_FAILED,
15219 SCROLLING_NEED_LARGER_MATRICES
15222 /* If scroll-conservatively is more than this, never recenter.
15224 If you change this, don't forget to update the doc string of
15225 `scroll-conservatively' and the Emacs manual. */
15226 #define SCROLL_LIMIT 100
15228 static int
15229 try_scrolling (Lisp_Object window, bool just_this_one_p,
15230 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15231 bool temp_scroll_step, bool last_line_misfit)
15233 struct window *w = XWINDOW (window);
15234 struct frame *f = XFRAME (w->frame);
15235 struct text_pos pos, startp;
15236 struct it it;
15237 int this_scroll_margin, scroll_max, rc, height;
15238 int dy = 0, amount_to_scroll = 0;
15239 bool scroll_down_p = false;
15240 int extra_scroll_margin_lines = last_line_misfit;
15241 Lisp_Object aggressive;
15242 /* We will never try scrolling more than this number of lines. */
15243 int scroll_limit = SCROLL_LIMIT;
15244 int frame_line_height = default_line_pixel_height (w);
15245 int window_total_lines
15246 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15248 #ifdef GLYPH_DEBUG
15249 debug_method_add (w, "try_scrolling");
15250 #endif
15252 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15254 /* Compute scroll margin height in pixels. We scroll when point is
15255 within this distance from the top or bottom of the window. */
15256 if (scroll_margin > 0)
15257 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15258 * frame_line_height;
15259 else
15260 this_scroll_margin = 0;
15262 /* Force arg_scroll_conservatively to have a reasonable value, to
15263 avoid scrolling too far away with slow move_it_* functions. Note
15264 that the user can supply scroll-conservatively equal to
15265 `most-positive-fixnum', which can be larger than INT_MAX. */
15266 if (arg_scroll_conservatively > scroll_limit)
15268 arg_scroll_conservatively = scroll_limit + 1;
15269 scroll_max = scroll_limit * frame_line_height;
15271 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15272 /* Compute how much we should try to scroll maximally to bring
15273 point into view. */
15274 scroll_max = (max (scroll_step,
15275 max (arg_scroll_conservatively, temp_scroll_step))
15276 * frame_line_height);
15277 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15278 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15279 /* We're trying to scroll because of aggressive scrolling but no
15280 scroll_step is set. Choose an arbitrary one. */
15281 scroll_max = 10 * frame_line_height;
15282 else
15283 scroll_max = 0;
15285 too_near_end:
15287 /* Decide whether to scroll down. */
15288 if (PT > CHARPOS (startp))
15290 int scroll_margin_y;
15292 /* Compute the pixel ypos of the scroll margin, then move IT to
15293 either that ypos or PT, whichever comes first. */
15294 start_display (&it, w, startp);
15295 scroll_margin_y = it.last_visible_y - this_scroll_margin
15296 - frame_line_height * extra_scroll_margin_lines;
15297 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15298 (MOVE_TO_POS | MOVE_TO_Y));
15300 if (PT > CHARPOS (it.current.pos))
15302 int y0 = line_bottom_y (&it);
15303 /* Compute how many pixels below window bottom to stop searching
15304 for PT. This avoids costly search for PT that is far away if
15305 the user limited scrolling by a small number of lines, but
15306 always finds PT if scroll_conservatively is set to a large
15307 number, such as most-positive-fixnum. */
15308 int slack = max (scroll_max, 10 * frame_line_height);
15309 int y_to_move = it.last_visible_y + slack;
15311 /* Compute the distance from the scroll margin to PT or to
15312 the scroll limit, whichever comes first. This should
15313 include the height of the cursor line, to make that line
15314 fully visible. */
15315 move_it_to (&it, PT, -1, y_to_move,
15316 -1, MOVE_TO_POS | MOVE_TO_Y);
15317 dy = line_bottom_y (&it) - y0;
15319 if (dy > scroll_max)
15320 return SCROLLING_FAILED;
15322 if (dy > 0)
15323 scroll_down_p = true;
15325 else if (PT == IT_CHARPOS (it)
15326 && IT_CHARPOS (it) < ZV
15327 && it.method == GET_FROM_STRING
15328 && arg_scroll_conservatively > scroll_limit
15329 && it.current_x == 0)
15331 enum move_it_result skip;
15332 int y1 = it.current_y;
15333 int vpos;
15335 /* A before-string that includes newlines and is displayed
15336 on the last visible screen line could fail us under
15337 scroll-conservatively > 100, because we will be unable to
15338 position the cursor on that last visible line. Try to
15339 recover by finding the first screen line that has some
15340 glyphs coming from the buffer text. */
15341 do {
15342 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15343 if (skip != MOVE_NEWLINE_OR_CR
15344 || IT_CHARPOS (it) != PT
15345 || it.method == GET_FROM_BUFFER)
15346 break;
15347 vpos = it.vpos;
15348 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15349 } while (it.vpos > vpos);
15351 dy = it.current_y - y1;
15353 if (dy > scroll_max)
15354 return SCROLLING_FAILED;
15356 if (dy > 0)
15357 scroll_down_p = true;
15361 if (scroll_down_p)
15363 /* Point is in or below the bottom scroll margin, so move the
15364 window start down. If scrolling conservatively, move it just
15365 enough down to make point visible. If scroll_step is set,
15366 move it down by scroll_step. */
15367 if (arg_scroll_conservatively)
15368 amount_to_scroll
15369 = min (max (dy, frame_line_height),
15370 frame_line_height * arg_scroll_conservatively);
15371 else if (scroll_step || temp_scroll_step)
15372 amount_to_scroll = scroll_max;
15373 else
15375 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15376 height = WINDOW_BOX_TEXT_HEIGHT (w);
15377 if (NUMBERP (aggressive))
15379 double float_amount = XFLOATINT (aggressive) * height;
15380 int aggressive_scroll = float_amount;
15381 if (aggressive_scroll == 0 && float_amount > 0)
15382 aggressive_scroll = 1;
15383 /* Don't let point enter the scroll margin near top of
15384 the window. This could happen if the value of
15385 scroll_up_aggressively is too large and there are
15386 non-zero margins, because scroll_up_aggressively
15387 means put point that fraction of window height
15388 _from_the_bottom_margin_. */
15389 if (aggressive_scroll + 2 * this_scroll_margin > height)
15390 aggressive_scroll = height - 2 * this_scroll_margin;
15391 amount_to_scroll = dy + aggressive_scroll;
15395 if (amount_to_scroll <= 0)
15396 return SCROLLING_FAILED;
15398 start_display (&it, w, startp);
15399 if (arg_scroll_conservatively <= scroll_limit)
15400 move_it_vertically (&it, amount_to_scroll);
15401 else
15403 /* Extra precision for users who set scroll-conservatively
15404 to a large number: make sure the amount we scroll
15405 the window start is never less than amount_to_scroll,
15406 which was computed as distance from window bottom to
15407 point. This matters when lines at window top and lines
15408 below window bottom have different height. */
15409 struct it it1;
15410 void *it1data = NULL;
15411 /* We use a temporary it1 because line_bottom_y can modify
15412 its argument, if it moves one line down; see there. */
15413 int start_y;
15415 SAVE_IT (it1, it, it1data);
15416 start_y = line_bottom_y (&it1);
15417 do {
15418 RESTORE_IT (&it, &it, it1data);
15419 move_it_by_lines (&it, 1);
15420 SAVE_IT (it1, it, it1data);
15421 } while (IT_CHARPOS (it) < ZV
15422 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15423 bidi_unshelve_cache (it1data, true);
15426 /* If STARTP is unchanged, move it down another screen line. */
15427 if (IT_CHARPOS (it) == CHARPOS (startp))
15428 move_it_by_lines (&it, 1);
15429 startp = it.current.pos;
15431 else
15433 struct text_pos scroll_margin_pos = startp;
15434 int y_offset = 0;
15436 /* See if point is inside the scroll margin at the top of the
15437 window. */
15438 if (this_scroll_margin)
15440 int y_start;
15442 start_display (&it, w, startp);
15443 y_start = it.current_y;
15444 move_it_vertically (&it, this_scroll_margin);
15445 scroll_margin_pos = it.current.pos;
15446 /* If we didn't move enough before hitting ZV, request
15447 additional amount of scroll, to move point out of the
15448 scroll margin. */
15449 if (IT_CHARPOS (it) == ZV
15450 && it.current_y - y_start < this_scroll_margin)
15451 y_offset = this_scroll_margin - (it.current_y - y_start);
15454 if (PT < CHARPOS (scroll_margin_pos))
15456 /* Point is in the scroll margin at the top of the window or
15457 above what is displayed in the window. */
15458 int y0, y_to_move;
15460 /* Compute the vertical distance from PT to the scroll
15461 margin position. Move as far as scroll_max allows, or
15462 one screenful, or 10 screen lines, whichever is largest.
15463 Give up if distance is greater than scroll_max or if we
15464 didn't reach the scroll margin position. */
15465 SET_TEXT_POS (pos, PT, PT_BYTE);
15466 start_display (&it, w, pos);
15467 y0 = it.current_y;
15468 y_to_move = max (it.last_visible_y,
15469 max (scroll_max, 10 * frame_line_height));
15470 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15471 y_to_move, -1,
15472 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15473 dy = it.current_y - y0;
15474 if (dy > scroll_max
15475 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15476 return SCROLLING_FAILED;
15478 /* Additional scroll for when ZV was too close to point. */
15479 dy += y_offset;
15481 /* Compute new window start. */
15482 start_display (&it, w, startp);
15484 if (arg_scroll_conservatively)
15485 amount_to_scroll = max (dy, frame_line_height
15486 * max (scroll_step, temp_scroll_step));
15487 else if (scroll_step || temp_scroll_step)
15488 amount_to_scroll = scroll_max;
15489 else
15491 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15492 height = WINDOW_BOX_TEXT_HEIGHT (w);
15493 if (NUMBERP (aggressive))
15495 double float_amount = XFLOATINT (aggressive) * height;
15496 int aggressive_scroll = float_amount;
15497 if (aggressive_scroll == 0 && float_amount > 0)
15498 aggressive_scroll = 1;
15499 /* Don't let point enter the scroll margin near
15500 bottom of the window, if the value of
15501 scroll_down_aggressively happens to be too
15502 large. */
15503 if (aggressive_scroll + 2 * this_scroll_margin > height)
15504 aggressive_scroll = height - 2 * this_scroll_margin;
15505 amount_to_scroll = dy + aggressive_scroll;
15509 if (amount_to_scroll <= 0)
15510 return SCROLLING_FAILED;
15512 move_it_vertically_backward (&it, amount_to_scroll);
15513 startp = it.current.pos;
15517 /* Run window scroll functions. */
15518 startp = run_window_scroll_functions (window, startp);
15520 /* Display the window. Give up if new fonts are loaded, or if point
15521 doesn't appear. */
15522 if (!try_window (window, startp, 0))
15523 rc = SCROLLING_NEED_LARGER_MATRICES;
15524 else if (w->cursor.vpos < 0)
15526 clear_glyph_matrix (w->desired_matrix);
15527 rc = SCROLLING_FAILED;
15529 else
15531 /* Maybe forget recorded base line for line number display. */
15532 if (!just_this_one_p
15533 || current_buffer->clip_changed
15534 || BEG_UNCHANGED < CHARPOS (startp))
15535 w->base_line_number = 0;
15537 /* If cursor ends up on a partially visible line,
15538 treat that as being off the bottom of the screen. */
15539 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15540 false)
15541 /* It's possible that the cursor is on the first line of the
15542 buffer, which is partially obscured due to a vscroll
15543 (Bug#7537). In that case, avoid looping forever. */
15544 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15546 clear_glyph_matrix (w->desired_matrix);
15547 ++extra_scroll_margin_lines;
15548 goto too_near_end;
15550 rc = SCROLLING_SUCCESS;
15553 return rc;
15557 /* Compute a suitable window start for window W if display of W starts
15558 on a continuation line. Value is true if a new window start
15559 was computed.
15561 The new window start will be computed, based on W's width, starting
15562 from the start of the continued line. It is the start of the
15563 screen line with the minimum distance from the old start W->start,
15564 which is still before point (otherwise point will definitely not
15565 be visible in the window). */
15567 static bool
15568 compute_window_start_on_continuation_line (struct window *w)
15570 struct text_pos pos, start_pos, pos_before_pt;
15571 bool window_start_changed_p = false;
15573 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15575 /* If window start is on a continuation line... Window start may be
15576 < BEGV in case there's invisible text at the start of the
15577 buffer (M-x rmail, for example). */
15578 if (CHARPOS (start_pos) > BEGV
15579 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15581 struct it it;
15582 struct glyph_row *row;
15584 /* Handle the case that the window start is out of range. */
15585 if (CHARPOS (start_pos) < BEGV)
15586 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15587 else if (CHARPOS (start_pos) > ZV)
15588 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15590 /* Find the start of the continued line. This should be fast
15591 because find_newline is fast (newline cache). */
15592 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15593 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15594 row, DEFAULT_FACE_ID);
15595 reseat_at_previous_visible_line_start (&it);
15597 /* If the line start is "too far" away from the window start,
15598 say it takes too much time to compute a new window start.
15599 Also, give up if the line start is after point, as in that
15600 case point will not be visible with any window start we
15601 compute. */
15602 if (IT_CHARPOS (it) <= PT
15603 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15604 /* PXW: Do we need upper bounds here? */
15605 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15607 int min_distance, distance;
15609 /* Move forward by display lines to find the new window
15610 start. If window width was enlarged, the new start can
15611 be expected to be > the old start. If window width was
15612 decreased, the new window start will be < the old start.
15613 So, we're looking for the display line start with the
15614 minimum distance from the old window start. */
15615 pos_before_pt = pos = it.current.pos;
15616 min_distance = INFINITY;
15617 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15618 distance < min_distance)
15620 min_distance = distance;
15621 if (CHARPOS (pos) <= PT)
15622 pos_before_pt = pos;
15623 pos = it.current.pos;
15624 if (it.line_wrap == WORD_WRAP)
15626 /* Under WORD_WRAP, move_it_by_lines is likely to
15627 overshoot and stop not at the first, but the
15628 second character from the left margin. So in
15629 that case, we need a more tight control on the X
15630 coordinate of the iterator than move_it_by_lines
15631 promises in its contract. The method is to first
15632 go to the last (rightmost) visible character of a
15633 line, then move to the leftmost character on the
15634 next line in a separate call. */
15635 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15636 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15637 move_it_to (&it, ZV, 0,
15638 it.current_y + it.max_ascent + it.max_descent, -1,
15639 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15641 else
15642 move_it_by_lines (&it, 1);
15645 /* It makes very little sense to make the new window start
15646 after point, as point won't be visible. If that's what
15647 the loop above finds, fall back on the candidate before
15648 or at point that is closest to the old window start. */
15649 if (CHARPOS (pos) > PT)
15650 pos = pos_before_pt;
15652 /* Set the window start there. */
15653 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15654 window_start_changed_p = true;
15658 return window_start_changed_p;
15662 /* Try cursor movement in case text has not changed in window WINDOW,
15663 with window start STARTP. Value is
15665 CURSOR_MOVEMENT_SUCCESS if successful
15667 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15669 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15670 display. *SCROLL_STEP is set to true, under certain circumstances, if
15671 we want to scroll as if scroll-step were set to 1. See the code.
15673 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15674 which case we have to abort this redisplay, and adjust matrices
15675 first. */
15677 enum
15679 CURSOR_MOVEMENT_SUCCESS,
15680 CURSOR_MOVEMENT_CANNOT_BE_USED,
15681 CURSOR_MOVEMENT_MUST_SCROLL,
15682 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15685 static int
15686 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15687 bool *scroll_step)
15689 struct window *w = XWINDOW (window);
15690 struct frame *f = XFRAME (w->frame);
15691 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15693 #ifdef GLYPH_DEBUG
15694 if (inhibit_try_cursor_movement)
15695 return rc;
15696 #endif
15698 /* Previously, there was a check for Lisp integer in the
15699 if-statement below. Now, this field is converted to
15700 ptrdiff_t, thus zero means invalid position in a buffer. */
15701 eassert (w->last_point > 0);
15702 /* Likewise there was a check whether window_end_vpos is nil or larger
15703 than the window. Now window_end_vpos is int and so never nil, but
15704 let's leave eassert to check whether it fits in the window. */
15705 eassert (!w->window_end_valid
15706 || w->window_end_vpos < w->current_matrix->nrows);
15708 /* Handle case where text has not changed, only point, and it has
15709 not moved off the frame. */
15710 if (/* Point may be in this window. */
15711 PT >= CHARPOS (startp)
15712 /* Selective display hasn't changed. */
15713 && !current_buffer->clip_changed
15714 /* Function force-mode-line-update is used to force a thorough
15715 redisplay. It sets either windows_or_buffers_changed or
15716 update_mode_lines. So don't take a shortcut here for these
15717 cases. */
15718 && !update_mode_lines
15719 && !windows_or_buffers_changed
15720 && !f->cursor_type_changed
15721 && NILP (Vshow_trailing_whitespace)
15722 /* This code is not used for mini-buffer for the sake of the case
15723 of redisplaying to replace an echo area message; since in
15724 that case the mini-buffer contents per se are usually
15725 unchanged. This code is of no real use in the mini-buffer
15726 since the handling of this_line_start_pos, etc., in redisplay
15727 handles the same cases. */
15728 && !EQ (window, minibuf_window)
15729 && (FRAME_WINDOW_P (f)
15730 || !overlay_arrow_in_current_buffer_p ()))
15732 int this_scroll_margin, top_scroll_margin;
15733 struct glyph_row *row = NULL;
15734 int frame_line_height = default_line_pixel_height (w);
15735 int window_total_lines
15736 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15738 #ifdef GLYPH_DEBUG
15739 debug_method_add (w, "cursor movement");
15740 #endif
15742 /* Scroll if point within this distance from the top or bottom
15743 of the window. This is a pixel value. */
15744 if (scroll_margin > 0)
15746 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15747 this_scroll_margin *= frame_line_height;
15749 else
15750 this_scroll_margin = 0;
15752 top_scroll_margin = this_scroll_margin;
15753 if (WINDOW_WANTS_HEADER_LINE_P (w))
15754 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15756 /* Start with the row the cursor was displayed during the last
15757 not paused redisplay. Give up if that row is not valid. */
15758 if (w->last_cursor_vpos < 0
15759 || w->last_cursor_vpos >= w->current_matrix->nrows)
15760 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15761 else
15763 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15764 if (row->mode_line_p)
15765 ++row;
15766 if (!row->enabled_p)
15767 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15770 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15772 bool scroll_p = false, must_scroll = false;
15773 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15775 if (PT > w->last_point)
15777 /* Point has moved forward. */
15778 while (MATRIX_ROW_END_CHARPOS (row) < PT
15779 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15781 eassert (row->enabled_p);
15782 ++row;
15785 /* If the end position of a row equals the start
15786 position of the next row, and PT is at that position,
15787 we would rather display cursor in the next line. */
15788 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15789 && MATRIX_ROW_END_CHARPOS (row) == PT
15790 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15791 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15792 && !cursor_row_p (row))
15793 ++row;
15795 /* If within the scroll margin, scroll. Note that
15796 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15797 the next line would be drawn, and that
15798 this_scroll_margin can be zero. */
15799 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15800 || PT > MATRIX_ROW_END_CHARPOS (row)
15801 /* Line is completely visible last line in window
15802 and PT is to be set in the next line. */
15803 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15804 && PT == MATRIX_ROW_END_CHARPOS (row)
15805 && !row->ends_at_zv_p
15806 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15807 scroll_p = true;
15809 else if (PT < w->last_point)
15811 /* Cursor has to be moved backward. Note that PT >=
15812 CHARPOS (startp) because of the outer if-statement. */
15813 while (!row->mode_line_p
15814 && (MATRIX_ROW_START_CHARPOS (row) > PT
15815 || (MATRIX_ROW_START_CHARPOS (row) == PT
15816 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15817 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15818 row > w->current_matrix->rows
15819 && (row-1)->ends_in_newline_from_string_p))))
15820 && (row->y > top_scroll_margin
15821 || CHARPOS (startp) == BEGV))
15823 eassert (row->enabled_p);
15824 --row;
15827 /* Consider the following case: Window starts at BEGV,
15828 there is invisible, intangible text at BEGV, so that
15829 display starts at some point START > BEGV. It can
15830 happen that we are called with PT somewhere between
15831 BEGV and START. Try to handle that case. */
15832 if (row < w->current_matrix->rows
15833 || row->mode_line_p)
15835 row = w->current_matrix->rows;
15836 if (row->mode_line_p)
15837 ++row;
15840 /* Due to newlines in overlay strings, we may have to
15841 skip forward over overlay strings. */
15842 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15843 && MATRIX_ROW_END_CHARPOS (row) == PT
15844 && !cursor_row_p (row))
15845 ++row;
15847 /* If within the scroll margin, scroll. */
15848 if (row->y < top_scroll_margin
15849 && CHARPOS (startp) != BEGV)
15850 scroll_p = true;
15852 else
15854 /* Cursor did not move. So don't scroll even if cursor line
15855 is partially visible, as it was so before. */
15856 rc = CURSOR_MOVEMENT_SUCCESS;
15859 if (PT < MATRIX_ROW_START_CHARPOS (row)
15860 || PT > MATRIX_ROW_END_CHARPOS (row))
15862 /* if PT is not in the glyph row, give up. */
15863 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15864 must_scroll = true;
15866 else if (rc != CURSOR_MOVEMENT_SUCCESS
15867 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15869 struct glyph_row *row1;
15871 /* If rows are bidi-reordered and point moved, back up
15872 until we find a row that does not belong to a
15873 continuation line. This is because we must consider
15874 all rows of a continued line as candidates for the
15875 new cursor positioning, since row start and end
15876 positions change non-linearly with vertical position
15877 in such rows. */
15878 /* FIXME: Revisit this when glyph ``spilling'' in
15879 continuation lines' rows is implemented for
15880 bidi-reordered rows. */
15881 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15882 MATRIX_ROW_CONTINUATION_LINE_P (row);
15883 --row)
15885 /* If we hit the beginning of the displayed portion
15886 without finding the first row of a continued
15887 line, give up. */
15888 if (row <= row1)
15890 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15891 break;
15893 eassert (row->enabled_p);
15896 if (must_scroll)
15898 else if (rc != CURSOR_MOVEMENT_SUCCESS
15899 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15900 /* Make sure this isn't a header line by any chance, since
15901 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15902 && !row->mode_line_p
15903 && make_cursor_line_fully_visible_p)
15905 if (PT == MATRIX_ROW_END_CHARPOS (row)
15906 && !row->ends_at_zv_p
15907 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15908 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15909 else if (row->height > window_box_height (w))
15911 /* If we end up in a partially visible line, let's
15912 make it fully visible, except when it's taller
15913 than the window, in which case we can't do much
15914 about it. */
15915 *scroll_step = true;
15916 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15918 else
15920 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15921 if (!cursor_row_fully_visible_p (w, false, true))
15922 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15923 else
15924 rc = CURSOR_MOVEMENT_SUCCESS;
15927 else if (scroll_p)
15928 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15929 else if (rc != CURSOR_MOVEMENT_SUCCESS
15930 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15932 /* With bidi-reordered rows, there could be more than
15933 one candidate row whose start and end positions
15934 occlude point. We need to let set_cursor_from_row
15935 find the best candidate. */
15936 /* FIXME: Revisit this when glyph ``spilling'' in
15937 continuation lines' rows is implemented for
15938 bidi-reordered rows. */
15939 bool rv = false;
15943 bool at_zv_p = false, exact_match_p = false;
15945 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15946 && PT <= MATRIX_ROW_END_CHARPOS (row)
15947 && cursor_row_p (row))
15948 rv |= set_cursor_from_row (w, row, w->current_matrix,
15949 0, 0, 0, 0);
15950 /* As soon as we've found the exact match for point,
15951 or the first suitable row whose ends_at_zv_p flag
15952 is set, we are done. */
15953 if (rv)
15955 at_zv_p = MATRIX_ROW (w->current_matrix,
15956 w->cursor.vpos)->ends_at_zv_p;
15957 if (!at_zv_p
15958 && w->cursor.hpos >= 0
15959 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15960 w->cursor.vpos))
15962 struct glyph_row *candidate =
15963 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15964 struct glyph *g =
15965 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15966 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15968 exact_match_p =
15969 (BUFFERP (g->object) && g->charpos == PT)
15970 || (NILP (g->object)
15971 && (g->charpos == PT
15972 || (g->charpos == 0 && endpos - 1 == PT)));
15974 if (at_zv_p || exact_match_p)
15976 rc = CURSOR_MOVEMENT_SUCCESS;
15977 break;
15980 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15981 break;
15982 ++row;
15984 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15985 || row->continued_p)
15986 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15987 || (MATRIX_ROW_START_CHARPOS (row) == PT
15988 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15989 /* If we didn't find any candidate rows, or exited the
15990 loop before all the candidates were examined, signal
15991 to the caller that this method failed. */
15992 if (rc != CURSOR_MOVEMENT_SUCCESS
15993 && !(rv
15994 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15995 && !row->continued_p))
15996 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15997 else if (rv)
15998 rc = CURSOR_MOVEMENT_SUCCESS;
16000 else
16004 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16006 rc = CURSOR_MOVEMENT_SUCCESS;
16007 break;
16009 ++row;
16011 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16012 && MATRIX_ROW_START_CHARPOS (row) == PT
16013 && cursor_row_p (row));
16018 return rc;
16022 void
16023 set_vertical_scroll_bar (struct window *w)
16025 ptrdiff_t start, end, whole;
16027 /* Calculate the start and end positions for the current window.
16028 At some point, it would be nice to choose between scrollbars
16029 which reflect the whole buffer size, with special markers
16030 indicating narrowing, and scrollbars which reflect only the
16031 visible region.
16033 Note that mini-buffers sometimes aren't displaying any text. */
16034 if (!MINI_WINDOW_P (w)
16035 || (w == XWINDOW (minibuf_window)
16036 && NILP (echo_area_buffer[0])))
16038 struct buffer *buf = XBUFFER (w->contents);
16039 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16040 start = marker_position (w->start) - BUF_BEGV (buf);
16041 /* I don't think this is guaranteed to be right. For the
16042 moment, we'll pretend it is. */
16043 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16045 if (end < start)
16046 end = start;
16047 if (whole < (end - start))
16048 whole = end - start;
16050 else
16051 start = end = whole = 0;
16053 /* Indicate what this scroll bar ought to be displaying now. */
16054 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16055 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16056 (w, end - start, whole, start);
16060 void
16061 set_horizontal_scroll_bar (struct window *w)
16063 int start, end, whole, portion;
16065 if (!MINI_WINDOW_P (w)
16066 || (w == XWINDOW (minibuf_window)
16067 && NILP (echo_area_buffer[0])))
16069 struct buffer *b = XBUFFER (w->contents);
16070 struct buffer *old_buffer = NULL;
16071 struct it it;
16072 struct text_pos startp;
16074 if (b != current_buffer)
16076 old_buffer = current_buffer;
16077 set_buffer_internal (b);
16080 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16081 start_display (&it, w, startp);
16082 it.last_visible_x = INT_MAX;
16083 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16084 MOVE_TO_X | MOVE_TO_Y);
16085 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16086 window_box_height (w), -1,
16087 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16089 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16090 end = start + window_box_width (w, TEXT_AREA);
16091 portion = end - start;
16092 /* After enlarging a horizontally scrolled window such that it
16093 gets at least as wide as the text it contains, make sure that
16094 the thumb doesn't fill the entire scroll bar so we can still
16095 drag it back to see the entire text. */
16096 whole = max (whole, end);
16098 if (it.bidi_p)
16100 Lisp_Object pdir;
16102 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16103 if (EQ (pdir, Qright_to_left))
16105 start = whole - end;
16106 end = start + portion;
16110 if (old_buffer)
16111 set_buffer_internal (old_buffer);
16113 else
16114 start = end = whole = portion = 0;
16116 w->hscroll_whole = whole;
16118 /* Indicate what this scroll bar ought to be displaying now. */
16119 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16120 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16121 (w, portion, whole, start);
16125 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16126 selected_window is redisplayed.
16128 We can return without actually redisplaying the window if fonts has been
16129 changed on window's frame. In that case, redisplay_internal will retry.
16131 As one of the important parts of redisplaying a window, we need to
16132 decide whether the previous window-start position (stored in the
16133 window's w->start marker position) is still valid, and if it isn't,
16134 recompute it. Some details about that:
16136 . The previous window-start could be in a continuation line, in
16137 which case we need to recompute it when the window width
16138 changes. See compute_window_start_on_continuation_line and its
16139 call below.
16141 . The text that changed since last redisplay could include the
16142 previous window-start position. In that case, we try to salvage
16143 what we can from the current glyph matrix by calling
16144 try_scrolling, which see.
16146 . Some Emacs command could force us to use a specific window-start
16147 position by setting the window's force_start flag, or gently
16148 propose doing that by setting the window's optional_new_start
16149 flag. In these cases, we try using the specified start point if
16150 that succeeds (i.e. the window desired matrix is successfully
16151 recomputed, and point location is within the window). In case
16152 of optional_new_start, we first check if the specified start
16153 position is feasible, i.e. if it will allow point to be
16154 displayed in the window. If using the specified start point
16155 fails, e.g., if new fonts are needed to be loaded, we abort the
16156 redisplay cycle and leave it up to the next cycle to figure out
16157 things.
16159 . Note that the window's force_start flag is sometimes set by
16160 redisplay itself, when it decides that the previous window start
16161 point is fine and should be kept. Search for "goto force_start"
16162 below to see the details. Like the values of window-start
16163 specified outside of redisplay, these internally-deduced values
16164 are tested for feasibility, and ignored if found to be
16165 unfeasible.
16167 . Note that the function try_window, used to completely redisplay
16168 a window, accepts the window's start point as its argument.
16169 This is used several times in the redisplay code to control
16170 where the window start will be, according to user options such
16171 as scroll-conservatively, and also to ensure the screen line
16172 showing point will be fully (as opposed to partially) visible on
16173 display. */
16175 static void
16176 redisplay_window (Lisp_Object window, bool just_this_one_p)
16178 struct window *w = XWINDOW (window);
16179 struct frame *f = XFRAME (w->frame);
16180 struct buffer *buffer = XBUFFER (w->contents);
16181 struct buffer *old = current_buffer;
16182 struct text_pos lpoint, opoint, startp;
16183 bool update_mode_line;
16184 int tem;
16185 struct it it;
16186 /* Record it now because it's overwritten. */
16187 bool current_matrix_up_to_date_p = false;
16188 bool used_current_matrix_p = false;
16189 /* This is less strict than current_matrix_up_to_date_p.
16190 It indicates that the buffer contents and narrowing are unchanged. */
16191 bool buffer_unchanged_p = false;
16192 bool temp_scroll_step = false;
16193 ptrdiff_t count = SPECPDL_INDEX ();
16194 int rc;
16195 int centering_position = -1;
16196 bool last_line_misfit = false;
16197 ptrdiff_t beg_unchanged, end_unchanged;
16198 int frame_line_height;
16199 bool use_desired_matrix;
16200 void *itdata = NULL;
16202 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16203 opoint = lpoint;
16205 #ifdef GLYPH_DEBUG
16206 *w->desired_matrix->method = 0;
16207 #endif
16209 if (!just_this_one_p
16210 && REDISPLAY_SOME_P ()
16211 && !w->redisplay
16212 && !w->update_mode_line
16213 && !f->face_change
16214 && !f->redisplay
16215 && !buffer->text->redisplay
16216 && BUF_PT (buffer) == w->last_point)
16217 return;
16219 /* Make sure that both W's markers are valid. */
16220 eassert (XMARKER (w->start)->buffer == buffer);
16221 eassert (XMARKER (w->pointm)->buffer == buffer);
16223 /* We come here again if we need to run window-text-change-functions
16224 below. */
16225 restart:
16226 reconsider_clip_changes (w);
16227 frame_line_height = default_line_pixel_height (w);
16229 /* Has the mode line to be updated? */
16230 update_mode_line = (w->update_mode_line
16231 || update_mode_lines
16232 || buffer->clip_changed
16233 || buffer->prevent_redisplay_optimizations_p);
16235 if (!just_this_one_p)
16236 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16237 cleverly elsewhere. */
16238 w->must_be_updated_p = true;
16240 if (MINI_WINDOW_P (w))
16242 if (w == XWINDOW (echo_area_window)
16243 && !NILP (echo_area_buffer[0]))
16245 if (update_mode_line)
16246 /* We may have to update a tty frame's menu bar or a
16247 tool-bar. Example `M-x C-h C-h C-g'. */
16248 goto finish_menu_bars;
16249 else
16250 /* We've already displayed the echo area glyphs in this window. */
16251 goto finish_scroll_bars;
16253 else if ((w != XWINDOW (minibuf_window)
16254 || minibuf_level == 0)
16255 /* When buffer is nonempty, redisplay window normally. */
16256 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16257 /* Quail displays non-mini buffers in minibuffer window.
16258 In that case, redisplay the window normally. */
16259 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16261 /* W is a mini-buffer window, but it's not active, so clear
16262 it. */
16263 int yb = window_text_bottom_y (w);
16264 struct glyph_row *row;
16265 int y;
16267 for (y = 0, row = w->desired_matrix->rows;
16268 y < yb;
16269 y += row->height, ++row)
16270 blank_row (w, row, y);
16271 goto finish_scroll_bars;
16274 clear_glyph_matrix (w->desired_matrix);
16277 /* Otherwise set up data on this window; select its buffer and point
16278 value. */
16279 /* Really select the buffer, for the sake of buffer-local
16280 variables. */
16281 set_buffer_internal_1 (XBUFFER (w->contents));
16283 current_matrix_up_to_date_p
16284 = (w->window_end_valid
16285 && !current_buffer->clip_changed
16286 && !current_buffer->prevent_redisplay_optimizations_p
16287 && !window_outdated (w));
16289 /* Run the window-text-change-functions
16290 if it is possible that the text on the screen has changed
16291 (either due to modification of the text, or any other reason). */
16292 if (!current_matrix_up_to_date_p
16293 && !NILP (Vwindow_text_change_functions))
16295 safe_run_hooks (Qwindow_text_change_functions);
16296 goto restart;
16299 beg_unchanged = BEG_UNCHANGED;
16300 end_unchanged = END_UNCHANGED;
16302 SET_TEXT_POS (opoint, PT, PT_BYTE);
16304 specbind (Qinhibit_point_motion_hooks, Qt);
16306 buffer_unchanged_p
16307 = (w->window_end_valid
16308 && !current_buffer->clip_changed
16309 && !window_outdated (w));
16311 /* When windows_or_buffers_changed is non-zero, we can't rely
16312 on the window end being valid, so set it to zero there. */
16313 if (windows_or_buffers_changed)
16315 /* If window starts on a continuation line, maybe adjust the
16316 window start in case the window's width changed. */
16317 if (XMARKER (w->start)->buffer == current_buffer)
16318 compute_window_start_on_continuation_line (w);
16320 w->window_end_valid = false;
16321 /* If so, we also can't rely on current matrix
16322 and should not fool try_cursor_movement below. */
16323 current_matrix_up_to_date_p = false;
16326 /* Some sanity checks. */
16327 CHECK_WINDOW_END (w);
16328 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16329 emacs_abort ();
16330 if (BYTEPOS (opoint) < CHARPOS (opoint))
16331 emacs_abort ();
16333 if (mode_line_update_needed (w))
16334 update_mode_line = true;
16336 /* Point refers normally to the selected window. For any other
16337 window, set up appropriate value. */
16338 if (!EQ (window, selected_window))
16340 ptrdiff_t new_pt = marker_position (w->pointm);
16341 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16343 if (new_pt < BEGV)
16345 new_pt = BEGV;
16346 new_pt_byte = BEGV_BYTE;
16347 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16349 else if (new_pt > (ZV - 1))
16351 new_pt = ZV;
16352 new_pt_byte = ZV_BYTE;
16353 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16356 /* We don't use SET_PT so that the point-motion hooks don't run. */
16357 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16360 /* If any of the character widths specified in the display table
16361 have changed, invalidate the width run cache. It's true that
16362 this may be a bit late to catch such changes, but the rest of
16363 redisplay goes (non-fatally) haywire when the display table is
16364 changed, so why should we worry about doing any better? */
16365 if (current_buffer->width_run_cache
16366 || (current_buffer->base_buffer
16367 && current_buffer->base_buffer->width_run_cache))
16369 struct Lisp_Char_Table *disptab = buffer_display_table ();
16371 if (! disptab_matches_widthtab
16372 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16374 struct buffer *buf = current_buffer;
16376 if (buf->base_buffer)
16377 buf = buf->base_buffer;
16378 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16379 recompute_width_table (current_buffer, disptab);
16383 /* If window-start is screwed up, choose a new one. */
16384 if (XMARKER (w->start)->buffer != current_buffer)
16385 goto recenter;
16387 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16389 /* If someone specified a new starting point but did not insist,
16390 check whether it can be used. */
16391 if ((w->optional_new_start || window_frozen_p (w))
16392 && CHARPOS (startp) >= BEGV
16393 && CHARPOS (startp) <= ZV)
16395 ptrdiff_t it_charpos;
16397 w->optional_new_start = false;
16398 start_display (&it, w, startp);
16399 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16400 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16401 /* Record IT's position now, since line_bottom_y might change
16402 that. */
16403 it_charpos = IT_CHARPOS (it);
16404 /* Make sure we set the force_start flag only if the cursor row
16405 will be fully visible. Otherwise, the code under force_start
16406 label below will try to move point back into view, which is
16407 not what the code which sets optional_new_start wants. */
16408 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16409 && !w->force_start)
16411 if (it_charpos == PT)
16412 w->force_start = true;
16413 /* IT may overshoot PT if text at PT is invisible. */
16414 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16415 w->force_start = true;
16416 #ifdef GLYPH_DEBUG
16417 if (w->force_start)
16419 if (window_frozen_p (w))
16420 debug_method_add (w, "set force_start from frozen window start");
16421 else
16422 debug_method_add (w, "set force_start from optional_new_start");
16424 #endif
16428 force_start:
16430 /* Handle case where place to start displaying has been specified,
16431 unless the specified location is outside the accessible range. */
16432 if (w->force_start)
16434 /* We set this later on if we have to adjust point. */
16435 int new_vpos = -1;
16437 w->force_start = false;
16438 w->vscroll = 0;
16439 w->window_end_valid = false;
16441 /* Forget any recorded base line for line number display. */
16442 if (!buffer_unchanged_p)
16443 w->base_line_number = 0;
16445 /* Redisplay the mode line. Select the buffer properly for that.
16446 Also, run the hook window-scroll-functions
16447 because we have scrolled. */
16448 /* Note, we do this after clearing force_start because
16449 if there's an error, it is better to forget about force_start
16450 than to get into an infinite loop calling the hook functions
16451 and having them get more errors. */
16452 if (!update_mode_line
16453 || ! NILP (Vwindow_scroll_functions))
16455 update_mode_line = true;
16456 w->update_mode_line = true;
16457 startp = run_window_scroll_functions (window, startp);
16460 if (CHARPOS (startp) < BEGV)
16461 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16462 else if (CHARPOS (startp) > ZV)
16463 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16465 /* Redisplay, then check if cursor has been set during the
16466 redisplay. Give up if new fonts were loaded. */
16467 /* We used to issue a CHECK_MARGINS argument to try_window here,
16468 but this causes scrolling to fail when point begins inside
16469 the scroll margin (bug#148) -- cyd */
16470 if (!try_window (window, startp, 0))
16472 w->force_start = true;
16473 clear_glyph_matrix (w->desired_matrix);
16474 goto need_larger_matrices;
16477 if (w->cursor.vpos < 0)
16479 /* If point does not appear, try to move point so it does
16480 appear. The desired matrix has been built above, so we
16481 can use it here. First see if point is in invisible
16482 text, and if so, move it to the first visible buffer
16483 position past that. */
16484 struct glyph_row *r = NULL;
16485 Lisp_Object invprop =
16486 get_char_property_and_overlay (make_number (PT), Qinvisible,
16487 Qnil, NULL);
16489 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16491 ptrdiff_t alt_pt;
16492 Lisp_Object invprop_end =
16493 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16494 Qnil, Qnil);
16496 if (NATNUMP (invprop_end))
16497 alt_pt = XFASTINT (invprop_end);
16498 else
16499 alt_pt = ZV;
16500 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16501 NULL, 0);
16503 if (r)
16504 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16505 else /* Give up and just move to the middle of the window. */
16506 new_vpos = window_box_height (w) / 2;
16509 if (!cursor_row_fully_visible_p (w, false, false))
16511 /* Point does appear, but on a line partly visible at end of window.
16512 Move it back to a fully-visible line. */
16513 new_vpos = window_box_height (w);
16514 /* But if window_box_height suggests a Y coordinate that is
16515 not less than we already have, that line will clearly not
16516 be fully visible, so give up and scroll the display.
16517 This can happen when the default face uses a font whose
16518 dimensions are different from the frame's default
16519 font. */
16520 if (new_vpos >= w->cursor.y)
16522 w->cursor.vpos = -1;
16523 clear_glyph_matrix (w->desired_matrix);
16524 goto try_to_scroll;
16527 else if (w->cursor.vpos >= 0)
16529 /* Some people insist on not letting point enter the scroll
16530 margin, even though this part handles windows that didn't
16531 scroll at all. */
16532 int window_total_lines
16533 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16534 int margin = min (scroll_margin, window_total_lines / 4);
16535 int pixel_margin = margin * frame_line_height;
16536 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16538 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16539 below, which finds the row to move point to, advances by
16540 the Y coordinate of the _next_ row, see the definition of
16541 MATRIX_ROW_BOTTOM_Y. */
16542 if (w->cursor.vpos < margin + header_line)
16544 w->cursor.vpos = -1;
16545 clear_glyph_matrix (w->desired_matrix);
16546 goto try_to_scroll;
16548 else
16550 int window_height = window_box_height (w);
16552 if (header_line)
16553 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16554 if (w->cursor.y >= window_height - pixel_margin)
16556 w->cursor.vpos = -1;
16557 clear_glyph_matrix (w->desired_matrix);
16558 goto try_to_scroll;
16563 /* If we need to move point for either of the above reasons,
16564 now actually do it. */
16565 if (new_vpos >= 0)
16567 struct glyph_row *row;
16569 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16570 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16571 ++row;
16573 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16574 MATRIX_ROW_START_BYTEPOS (row));
16576 if (w != XWINDOW (selected_window))
16577 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16578 else if (current_buffer == old)
16579 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16581 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16583 /* Re-run pre-redisplay-function so it can update the region
16584 according to the new position of point. */
16585 /* Other than the cursor, w's redisplay is done so we can set its
16586 redisplay to false. Also the buffer's redisplay can be set to
16587 false, since propagate_buffer_redisplay should have already
16588 propagated its info to `w' anyway. */
16589 w->redisplay = false;
16590 XBUFFER (w->contents)->text->redisplay = false;
16591 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16593 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16595 /* pre-redisplay-function made changes (e.g. move the region)
16596 that require another round of redisplay. */
16597 clear_glyph_matrix (w->desired_matrix);
16598 if (!try_window (window, startp, 0))
16599 goto need_larger_matrices;
16602 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16604 clear_glyph_matrix (w->desired_matrix);
16605 goto try_to_scroll;
16608 #ifdef GLYPH_DEBUG
16609 debug_method_add (w, "forced window start");
16610 #endif
16611 goto done;
16614 /* Handle case where text has not changed, only point, and it has
16615 not moved off the frame, and we are not retrying after hscroll.
16616 (current_matrix_up_to_date_p is true when retrying.) */
16617 if (current_matrix_up_to_date_p
16618 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16619 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16621 switch (rc)
16623 case CURSOR_MOVEMENT_SUCCESS:
16624 used_current_matrix_p = true;
16625 goto done;
16627 case CURSOR_MOVEMENT_MUST_SCROLL:
16628 goto try_to_scroll;
16630 default:
16631 emacs_abort ();
16634 /* If current starting point was originally the beginning of a line
16635 but no longer is, find a new starting point. */
16636 else if (w->start_at_line_beg
16637 && !(CHARPOS (startp) <= BEGV
16638 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16640 #ifdef GLYPH_DEBUG
16641 debug_method_add (w, "recenter 1");
16642 #endif
16643 goto recenter;
16646 /* Try scrolling with try_window_id. Value is > 0 if update has
16647 been done, it is -1 if we know that the same window start will
16648 not work. It is 0 if unsuccessful for some other reason. */
16649 else if ((tem = try_window_id (w)) != 0)
16651 #ifdef GLYPH_DEBUG
16652 debug_method_add (w, "try_window_id %d", tem);
16653 #endif
16655 if (f->fonts_changed)
16656 goto need_larger_matrices;
16657 if (tem > 0)
16658 goto done;
16660 /* Otherwise try_window_id has returned -1 which means that we
16661 don't want the alternative below this comment to execute. */
16663 else if (CHARPOS (startp) >= BEGV
16664 && CHARPOS (startp) <= ZV
16665 && PT >= CHARPOS (startp)
16666 && (CHARPOS (startp) < ZV
16667 /* Avoid starting at end of buffer. */
16668 || CHARPOS (startp) == BEGV
16669 || !window_outdated (w)))
16671 int d1, d2, d5, d6;
16672 int rtop, rbot;
16674 /* If first window line is a continuation line, and window start
16675 is inside the modified region, but the first change is before
16676 current window start, we must select a new window start.
16678 However, if this is the result of a down-mouse event (e.g. by
16679 extending the mouse-drag-overlay), we don't want to select a
16680 new window start, since that would change the position under
16681 the mouse, resulting in an unwanted mouse-movement rather
16682 than a simple mouse-click. */
16683 if (!w->start_at_line_beg
16684 && NILP (do_mouse_tracking)
16685 && CHARPOS (startp) > BEGV
16686 && CHARPOS (startp) > BEG + beg_unchanged
16687 && CHARPOS (startp) <= Z - end_unchanged
16688 /* Even if w->start_at_line_beg is nil, a new window may
16689 start at a line_beg, since that's how set_buffer_window
16690 sets it. So, we need to check the return value of
16691 compute_window_start_on_continuation_line. (See also
16692 bug#197). */
16693 && XMARKER (w->start)->buffer == current_buffer
16694 && compute_window_start_on_continuation_line (w)
16695 /* It doesn't make sense to force the window start like we
16696 do at label force_start if it is already known that point
16697 will not be fully visible in the resulting window, because
16698 doing so will move point from its correct position
16699 instead of scrolling the window to bring point into view.
16700 See bug#9324. */
16701 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16702 /* A very tall row could need more than the window height,
16703 in which case we accept that it is partially visible. */
16704 && (rtop != 0) == (rbot != 0))
16706 w->force_start = true;
16707 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16708 #ifdef GLYPH_DEBUG
16709 debug_method_add (w, "recomputed window start in continuation line");
16710 #endif
16711 goto force_start;
16714 #ifdef GLYPH_DEBUG
16715 debug_method_add (w, "same window start");
16716 #endif
16718 /* Try to redisplay starting at same place as before.
16719 If point has not moved off frame, accept the results. */
16720 if (!current_matrix_up_to_date_p
16721 /* Don't use try_window_reusing_current_matrix in this case
16722 because a window scroll function can have changed the
16723 buffer. */
16724 || !NILP (Vwindow_scroll_functions)
16725 || MINI_WINDOW_P (w)
16726 || !(used_current_matrix_p
16727 = try_window_reusing_current_matrix (w)))
16729 IF_DEBUG (debug_method_add (w, "1"));
16730 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16731 /* -1 means we need to scroll.
16732 0 means we need new matrices, but fonts_changed
16733 is set in that case, so we will detect it below. */
16734 goto try_to_scroll;
16737 if (f->fonts_changed)
16738 goto need_larger_matrices;
16740 if (w->cursor.vpos >= 0)
16742 if (!just_this_one_p
16743 || current_buffer->clip_changed
16744 || BEG_UNCHANGED < CHARPOS (startp))
16745 /* Forget any recorded base line for line number display. */
16746 w->base_line_number = 0;
16748 if (!cursor_row_fully_visible_p (w, true, false))
16750 clear_glyph_matrix (w->desired_matrix);
16751 last_line_misfit = true;
16753 /* Drop through and scroll. */
16754 else
16755 goto done;
16757 else
16758 clear_glyph_matrix (w->desired_matrix);
16761 try_to_scroll:
16763 /* Redisplay the mode line. Select the buffer properly for that. */
16764 if (!update_mode_line)
16766 update_mode_line = true;
16767 w->update_mode_line = true;
16770 /* Try to scroll by specified few lines. */
16771 if ((scroll_conservatively
16772 || emacs_scroll_step
16773 || temp_scroll_step
16774 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16775 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16776 && CHARPOS (startp) >= BEGV
16777 && CHARPOS (startp) <= ZV)
16779 /* The function returns -1 if new fonts were loaded, 1 if
16780 successful, 0 if not successful. */
16781 int ss = try_scrolling (window, just_this_one_p,
16782 scroll_conservatively,
16783 emacs_scroll_step,
16784 temp_scroll_step, last_line_misfit);
16785 switch (ss)
16787 case SCROLLING_SUCCESS:
16788 goto done;
16790 case SCROLLING_NEED_LARGER_MATRICES:
16791 goto need_larger_matrices;
16793 case SCROLLING_FAILED:
16794 break;
16796 default:
16797 emacs_abort ();
16801 /* Finally, just choose a place to start which positions point
16802 according to user preferences. */
16804 recenter:
16806 #ifdef GLYPH_DEBUG
16807 debug_method_add (w, "recenter");
16808 #endif
16810 /* Forget any previously recorded base line for line number display. */
16811 if (!buffer_unchanged_p)
16812 w->base_line_number = 0;
16814 /* Determine the window start relative to point. */
16815 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16816 it.current_y = it.last_visible_y;
16817 if (centering_position < 0)
16819 int window_total_lines
16820 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16821 int margin
16822 = scroll_margin > 0
16823 ? min (scroll_margin, window_total_lines / 4)
16824 : 0;
16825 ptrdiff_t margin_pos = CHARPOS (startp);
16826 Lisp_Object aggressive;
16827 bool scrolling_up;
16829 /* If there is a scroll margin at the top of the window, find
16830 its character position. */
16831 if (margin
16832 /* Cannot call start_display if startp is not in the
16833 accessible region of the buffer. This can happen when we
16834 have just switched to a different buffer and/or changed
16835 its restriction. In that case, startp is initialized to
16836 the character position 1 (BEGV) because we did not yet
16837 have chance to display the buffer even once. */
16838 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16840 struct it it1;
16841 void *it1data = NULL;
16843 SAVE_IT (it1, it, it1data);
16844 start_display (&it1, w, startp);
16845 move_it_vertically (&it1, margin * frame_line_height);
16846 margin_pos = IT_CHARPOS (it1);
16847 RESTORE_IT (&it, &it, it1data);
16849 scrolling_up = PT > margin_pos;
16850 aggressive =
16851 scrolling_up
16852 ? BVAR (current_buffer, scroll_up_aggressively)
16853 : BVAR (current_buffer, scroll_down_aggressively);
16855 if (!MINI_WINDOW_P (w)
16856 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16858 int pt_offset = 0;
16860 /* Setting scroll-conservatively overrides
16861 scroll-*-aggressively. */
16862 if (!scroll_conservatively && NUMBERP (aggressive))
16864 double float_amount = XFLOATINT (aggressive);
16866 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16867 if (pt_offset == 0 && float_amount > 0)
16868 pt_offset = 1;
16869 if (pt_offset && margin > 0)
16870 margin -= 1;
16872 /* Compute how much to move the window start backward from
16873 point so that point will be displayed where the user
16874 wants it. */
16875 if (scrolling_up)
16877 centering_position = it.last_visible_y;
16878 if (pt_offset)
16879 centering_position -= pt_offset;
16880 centering_position -=
16881 (frame_line_height * (1 + margin + last_line_misfit)
16882 + WINDOW_HEADER_LINE_HEIGHT (w));
16883 /* Don't let point enter the scroll margin near top of
16884 the window. */
16885 if (centering_position < margin * frame_line_height)
16886 centering_position = margin * frame_line_height;
16888 else
16889 centering_position = margin * frame_line_height + pt_offset;
16891 else
16892 /* Set the window start half the height of the window backward
16893 from point. */
16894 centering_position = window_box_height (w) / 2;
16896 move_it_vertically_backward (&it, centering_position);
16898 eassert (IT_CHARPOS (it) >= BEGV);
16900 /* The function move_it_vertically_backward may move over more
16901 than the specified y-distance. If it->w is small, e.g. a
16902 mini-buffer window, we may end up in front of the window's
16903 display area. Start displaying at the start of the line
16904 containing PT in this case. */
16905 if (it.current_y <= 0)
16907 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16908 move_it_vertically_backward (&it, 0);
16909 it.current_y = 0;
16912 it.current_x = it.hpos = 0;
16914 /* Set the window start position here explicitly, to avoid an
16915 infinite loop in case the functions in window-scroll-functions
16916 get errors. */
16917 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16919 /* Run scroll hooks. */
16920 startp = run_window_scroll_functions (window, it.current.pos);
16922 /* We invoke try_window and try_window_reusing_current_matrix below,
16923 and they manipulate the bidi cache. Save and restore the cache
16924 state of our iterator, so we could continue using it after that. */
16925 itdata = bidi_shelve_cache ();
16927 /* Redisplay the window. */
16928 use_desired_matrix = false;
16929 if (!current_matrix_up_to_date_p
16930 || windows_or_buffers_changed
16931 || f->cursor_type_changed
16932 /* Don't use try_window_reusing_current_matrix in this case
16933 because it can have changed the buffer. */
16934 || !NILP (Vwindow_scroll_functions)
16935 || !just_this_one_p
16936 || MINI_WINDOW_P (w)
16937 || !(used_current_matrix_p
16938 = try_window_reusing_current_matrix (w)))
16939 use_desired_matrix = (try_window (window, startp, 0) == 1);
16941 bidi_unshelve_cache (itdata, false);
16943 /* If new fonts have been loaded (due to fontsets), give up. We
16944 have to start a new redisplay since we need to re-adjust glyph
16945 matrices. */
16946 if (f->fonts_changed)
16947 goto need_larger_matrices;
16949 /* If cursor did not appear assume that the middle of the window is
16950 in the first line of the window. Do it again with the next line.
16951 (Imagine a window of height 100, displaying two lines of height
16952 60. Moving back 50 from it->last_visible_y will end in the first
16953 line.) */
16954 if (w->cursor.vpos < 0)
16956 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16958 clear_glyph_matrix (w->desired_matrix);
16959 move_it_by_lines (&it, 1);
16960 try_window (window, it.current.pos, 0);
16962 else if (PT < IT_CHARPOS (it))
16964 clear_glyph_matrix (w->desired_matrix);
16965 move_it_by_lines (&it, -1);
16966 try_window (window, it.current.pos, 0);
16968 else if (scroll_conservatively > SCROLL_LIMIT
16969 && (it.method == GET_FROM_STRING
16970 || overlay_touches_p (IT_CHARPOS (it)))
16971 && IT_CHARPOS (it) < ZV)
16973 /* If the window starts with a before-string that spans more
16974 than one screen line, using that position to display the
16975 window might fail to bring point into the view, because
16976 start_display will always start by displaying the string,
16977 whereas the code above determines where to set w->start
16978 by the buffer position of the place where it takes screen
16979 coordinates. Try to recover by finding the next screen
16980 line that displays buffer text. */
16981 ptrdiff_t pos0 = IT_CHARPOS (it);
16983 clear_glyph_matrix (w->desired_matrix);
16984 do {
16985 move_it_by_lines (&it, 1);
16986 } while (IT_CHARPOS (it) == pos0);
16987 try_window (window, it.current.pos, 0);
16989 else
16991 /* Not much we can do about it. */
16995 /* Consider the following case: Window starts at BEGV, there is
16996 invisible, intangible text at BEGV, so that display starts at
16997 some point START > BEGV. It can happen that we are called with
16998 PT somewhere between BEGV and START. Try to handle that case,
16999 and similar ones. */
17000 if (w->cursor.vpos < 0)
17002 /* Prefer the desired matrix to the current matrix, if possible,
17003 in the fallback calculations below. This is because using
17004 the current matrix might completely goof, e.g. if its first
17005 row is after point. */
17006 struct glyph_matrix *matrix =
17007 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17008 /* First, try locating the proper glyph row for PT. */
17009 struct glyph_row *row =
17010 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17012 /* Sometimes point is at the beginning of invisible text that is
17013 before the 1st character displayed in the row. In that case,
17014 row_containing_pos fails to find the row, because no glyphs
17015 with appropriate buffer positions are present in the row.
17016 Therefore, we next try to find the row which shows the 1st
17017 position after the invisible text. */
17018 if (!row)
17020 Lisp_Object val =
17021 get_char_property_and_overlay (make_number (PT), Qinvisible,
17022 Qnil, NULL);
17024 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17026 ptrdiff_t alt_pos;
17027 Lisp_Object invis_end =
17028 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17029 Qnil, Qnil);
17031 if (NATNUMP (invis_end))
17032 alt_pos = XFASTINT (invis_end);
17033 else
17034 alt_pos = ZV;
17035 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17038 /* Finally, fall back on the first row of the window after the
17039 header line (if any). This is slightly better than not
17040 displaying the cursor at all. */
17041 if (!row)
17043 row = matrix->rows;
17044 if (row->mode_line_p)
17045 ++row;
17047 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17050 if (!cursor_row_fully_visible_p (w, false, false))
17052 /* If vscroll is enabled, disable it and try again. */
17053 if (w->vscroll)
17055 w->vscroll = 0;
17056 clear_glyph_matrix (w->desired_matrix);
17057 goto recenter;
17060 /* Users who set scroll-conservatively to a large number want
17061 point just above/below the scroll margin. If we ended up
17062 with point's row partially visible, move the window start to
17063 make that row fully visible and out of the margin. */
17064 if (scroll_conservatively > SCROLL_LIMIT)
17066 int window_total_lines
17067 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17068 int margin =
17069 scroll_margin > 0
17070 ? min (scroll_margin, window_total_lines / 4)
17071 : 0;
17072 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17074 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17075 clear_glyph_matrix (w->desired_matrix);
17076 if (1 == try_window (window, it.current.pos,
17077 TRY_WINDOW_CHECK_MARGINS))
17078 goto done;
17081 /* If centering point failed to make the whole line visible,
17082 put point at the top instead. That has to make the whole line
17083 visible, if it can be done. */
17084 if (centering_position == 0)
17085 goto done;
17087 clear_glyph_matrix (w->desired_matrix);
17088 centering_position = 0;
17089 goto recenter;
17092 done:
17094 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17095 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17096 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17098 /* Display the mode line, if we must. */
17099 if ((update_mode_line
17100 /* If window not full width, must redo its mode line
17101 if (a) the window to its side is being redone and
17102 (b) we do a frame-based redisplay. This is a consequence
17103 of how inverted lines are drawn in frame-based redisplay. */
17104 || (!just_this_one_p
17105 && !FRAME_WINDOW_P (f)
17106 && !WINDOW_FULL_WIDTH_P (w))
17107 /* Line number to display. */
17108 || w->base_line_pos > 0
17109 /* Column number is displayed and different from the one displayed. */
17110 || (w->column_number_displayed != -1
17111 && (w->column_number_displayed != current_column ())))
17112 /* This means that the window has a mode line. */
17113 && (WINDOW_WANTS_MODELINE_P (w)
17114 || WINDOW_WANTS_HEADER_LINE_P (w)))
17117 display_mode_lines (w);
17119 /* If mode line height has changed, arrange for a thorough
17120 immediate redisplay using the correct mode line height. */
17121 if (WINDOW_WANTS_MODELINE_P (w)
17122 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17124 f->fonts_changed = true;
17125 w->mode_line_height = -1;
17126 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17127 = DESIRED_MODE_LINE_HEIGHT (w);
17130 /* If header line height has changed, arrange for a thorough
17131 immediate redisplay using the correct header line height. */
17132 if (WINDOW_WANTS_HEADER_LINE_P (w)
17133 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17135 f->fonts_changed = true;
17136 w->header_line_height = -1;
17137 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17138 = DESIRED_HEADER_LINE_HEIGHT (w);
17141 if (f->fonts_changed)
17142 goto need_larger_matrices;
17145 if (!line_number_displayed && w->base_line_pos != -1)
17147 w->base_line_pos = 0;
17148 w->base_line_number = 0;
17151 finish_menu_bars:
17153 /* When we reach a frame's selected window, redo the frame's menu
17154 bar and the frame's title. */
17155 if (update_mode_line
17156 && EQ (FRAME_SELECTED_WINDOW (f), window))
17158 bool redisplay_menu_p;
17160 if (FRAME_WINDOW_P (f))
17162 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17163 || defined (HAVE_NS) || defined (USE_GTK)
17164 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17165 #else
17166 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17167 #endif
17169 else
17170 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17172 if (redisplay_menu_p)
17173 display_menu_bar (w);
17175 #ifdef HAVE_WINDOW_SYSTEM
17176 if (FRAME_WINDOW_P (f))
17178 #if defined (USE_GTK) || defined (HAVE_NS)
17179 if (FRAME_EXTERNAL_TOOL_BAR (f))
17180 redisplay_tool_bar (f);
17181 #else
17182 if (WINDOWP (f->tool_bar_window)
17183 && (FRAME_TOOL_BAR_LINES (f) > 0
17184 || !NILP (Vauto_resize_tool_bars))
17185 && redisplay_tool_bar (f))
17186 ignore_mouse_drag_p = true;
17187 #endif
17189 x_consider_frame_title (w->frame);
17190 #endif
17193 #ifdef HAVE_WINDOW_SYSTEM
17194 if (FRAME_WINDOW_P (f)
17195 && update_window_fringes (w, (just_this_one_p
17196 || (!used_current_matrix_p && !overlay_arrow_seen)
17197 || w->pseudo_window_p)))
17199 update_begin (f);
17200 block_input ();
17201 if (draw_window_fringes (w, true))
17203 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17204 x_draw_right_divider (w);
17205 else
17206 x_draw_vertical_border (w);
17208 unblock_input ();
17209 update_end (f);
17212 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17213 x_draw_bottom_divider (w);
17214 #endif /* HAVE_WINDOW_SYSTEM */
17216 /* We go to this label, with fonts_changed set, if it is
17217 necessary to try again using larger glyph matrices.
17218 We have to redeem the scroll bar even in this case,
17219 because the loop in redisplay_internal expects that. */
17220 need_larger_matrices:
17222 finish_scroll_bars:
17224 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17226 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17227 /* Set the thumb's position and size. */
17228 set_vertical_scroll_bar (w);
17230 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17231 /* Set the thumb's position and size. */
17232 set_horizontal_scroll_bar (w);
17234 /* Note that we actually used the scroll bar attached to this
17235 window, so it shouldn't be deleted at the end of redisplay. */
17236 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17237 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17240 /* Restore current_buffer and value of point in it. The window
17241 update may have changed the buffer, so first make sure `opoint'
17242 is still valid (Bug#6177). */
17243 if (CHARPOS (opoint) < BEGV)
17244 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17245 else if (CHARPOS (opoint) > ZV)
17246 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17247 else
17248 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17250 set_buffer_internal_1 (old);
17251 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17252 shorter. This can be caused by log truncation in *Messages*. */
17253 if (CHARPOS (lpoint) <= ZV)
17254 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17256 unbind_to (count, Qnil);
17260 /* Build the complete desired matrix of WINDOW with a window start
17261 buffer position POS.
17263 Value is 1 if successful. It is zero if fonts were loaded during
17264 redisplay which makes re-adjusting glyph matrices necessary, and -1
17265 if point would appear in the scroll margins.
17266 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17267 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17268 set in FLAGS.) */
17271 try_window (Lisp_Object window, struct text_pos pos, int flags)
17273 struct window *w = XWINDOW (window);
17274 struct it it;
17275 struct glyph_row *last_text_row = NULL;
17276 struct frame *f = XFRAME (w->frame);
17277 int frame_line_height = default_line_pixel_height (w);
17279 /* Make POS the new window start. */
17280 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17282 /* Mark cursor position as unknown. No overlay arrow seen. */
17283 w->cursor.vpos = -1;
17284 overlay_arrow_seen = false;
17286 /* Initialize iterator and info to start at POS. */
17287 start_display (&it, w, pos);
17288 it.glyph_row->reversed_p = false;
17290 /* Display all lines of W. */
17291 while (it.current_y < it.last_visible_y)
17293 if (display_line (&it))
17294 last_text_row = it.glyph_row - 1;
17295 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17296 return 0;
17299 /* Don't let the cursor end in the scroll margins. */
17300 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17301 && !MINI_WINDOW_P (w))
17303 int this_scroll_margin;
17304 int window_total_lines
17305 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17307 if (scroll_margin > 0)
17309 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17310 this_scroll_margin *= frame_line_height;
17312 else
17313 this_scroll_margin = 0;
17315 if ((w->cursor.y >= 0 /* not vscrolled */
17316 && w->cursor.y < this_scroll_margin
17317 && CHARPOS (pos) > BEGV
17318 && IT_CHARPOS (it) < ZV)
17319 /* rms: considering make_cursor_line_fully_visible_p here
17320 seems to give wrong results. We don't want to recenter
17321 when the last line is partly visible, we want to allow
17322 that case to be handled in the usual way. */
17323 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17325 w->cursor.vpos = -1;
17326 clear_glyph_matrix (w->desired_matrix);
17327 return -1;
17331 /* If bottom moved off end of frame, change mode line percentage. */
17332 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17333 w->update_mode_line = true;
17335 /* Set window_end_pos to the offset of the last character displayed
17336 on the window from the end of current_buffer. Set
17337 window_end_vpos to its row number. */
17338 if (last_text_row)
17340 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17341 adjust_window_ends (w, last_text_row, false);
17342 eassert
17343 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17344 w->window_end_vpos)));
17346 else
17348 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17349 w->window_end_pos = Z - ZV;
17350 w->window_end_vpos = 0;
17353 /* But that is not valid info until redisplay finishes. */
17354 w->window_end_valid = false;
17355 return 1;
17360 /************************************************************************
17361 Window redisplay reusing current matrix when buffer has not changed
17362 ************************************************************************/
17364 /* Try redisplay of window W showing an unchanged buffer with a
17365 different window start than the last time it was displayed by
17366 reusing its current matrix. Value is true if successful.
17367 W->start is the new window start. */
17369 static bool
17370 try_window_reusing_current_matrix (struct window *w)
17372 struct frame *f = XFRAME (w->frame);
17373 struct glyph_row *bottom_row;
17374 struct it it;
17375 struct run run;
17376 struct text_pos start, new_start;
17377 int nrows_scrolled, i;
17378 struct glyph_row *last_text_row;
17379 struct glyph_row *last_reused_text_row;
17380 struct glyph_row *start_row;
17381 int start_vpos, min_y, max_y;
17383 #ifdef GLYPH_DEBUG
17384 if (inhibit_try_window_reusing)
17385 return false;
17386 #endif
17388 if (/* This function doesn't handle terminal frames. */
17389 !FRAME_WINDOW_P (f)
17390 /* Don't try to reuse the display if windows have been split
17391 or such. */
17392 || windows_or_buffers_changed
17393 || f->cursor_type_changed)
17394 return false;
17396 /* Can't do this if showing trailing whitespace. */
17397 if (!NILP (Vshow_trailing_whitespace))
17398 return false;
17400 /* If top-line visibility has changed, give up. */
17401 if (WINDOW_WANTS_HEADER_LINE_P (w)
17402 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17403 return false;
17405 /* Give up if old or new display is scrolled vertically. We could
17406 make this function handle this, but right now it doesn't. */
17407 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17408 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17409 return false;
17411 /* The variable new_start now holds the new window start. The old
17412 start `start' can be determined from the current matrix. */
17413 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17414 start = start_row->minpos;
17415 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17417 /* Clear the desired matrix for the display below. */
17418 clear_glyph_matrix (w->desired_matrix);
17420 if (CHARPOS (new_start) <= CHARPOS (start))
17422 /* Don't use this method if the display starts with an ellipsis
17423 displayed for invisible text. It's not easy to handle that case
17424 below, and it's certainly not worth the effort since this is
17425 not a frequent case. */
17426 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17427 return false;
17429 IF_DEBUG (debug_method_add (w, "twu1"));
17431 /* Display up to a row that can be reused. The variable
17432 last_text_row is set to the last row displayed that displays
17433 text. Note that it.vpos == 0 if or if not there is a
17434 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17435 start_display (&it, w, new_start);
17436 w->cursor.vpos = -1;
17437 last_text_row = last_reused_text_row = NULL;
17439 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17441 /* If we have reached into the characters in the START row,
17442 that means the line boundaries have changed. So we
17443 can't start copying with the row START. Maybe it will
17444 work to start copying with the following row. */
17445 while (IT_CHARPOS (it) > CHARPOS (start))
17447 /* Advance to the next row as the "start". */
17448 start_row++;
17449 start = start_row->minpos;
17450 /* If there are no more rows to try, or just one, give up. */
17451 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17452 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17453 || CHARPOS (start) == ZV)
17455 clear_glyph_matrix (w->desired_matrix);
17456 return false;
17459 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17461 /* If we have reached alignment, we can copy the rest of the
17462 rows. */
17463 if (IT_CHARPOS (it) == CHARPOS (start)
17464 /* Don't accept "alignment" inside a display vector,
17465 since start_row could have started in the middle of
17466 that same display vector (thus their character
17467 positions match), and we have no way of telling if
17468 that is the case. */
17469 && it.current.dpvec_index < 0)
17470 break;
17472 it.glyph_row->reversed_p = false;
17473 if (display_line (&it))
17474 last_text_row = it.glyph_row - 1;
17478 /* A value of current_y < last_visible_y means that we stopped
17479 at the previous window start, which in turn means that we
17480 have at least one reusable row. */
17481 if (it.current_y < it.last_visible_y)
17483 struct glyph_row *row;
17485 /* IT.vpos always starts from 0; it counts text lines. */
17486 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17488 /* Find PT if not already found in the lines displayed. */
17489 if (w->cursor.vpos < 0)
17491 int dy = it.current_y - start_row->y;
17493 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17494 row = row_containing_pos (w, PT, row, NULL, dy);
17495 if (row)
17496 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17497 dy, nrows_scrolled);
17498 else
17500 clear_glyph_matrix (w->desired_matrix);
17501 return false;
17505 /* Scroll the display. Do it before the current matrix is
17506 changed. The problem here is that update has not yet
17507 run, i.e. part of the current matrix is not up to date.
17508 scroll_run_hook will clear the cursor, and use the
17509 current matrix to get the height of the row the cursor is
17510 in. */
17511 run.current_y = start_row->y;
17512 run.desired_y = it.current_y;
17513 run.height = it.last_visible_y - it.current_y;
17515 if (run.height > 0 && run.current_y != run.desired_y)
17517 update_begin (f);
17518 FRAME_RIF (f)->update_window_begin_hook (w);
17519 FRAME_RIF (f)->clear_window_mouse_face (w);
17520 FRAME_RIF (f)->scroll_run_hook (w, &run);
17521 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17522 update_end (f);
17525 /* Shift current matrix down by nrows_scrolled lines. */
17526 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17527 rotate_matrix (w->current_matrix,
17528 start_vpos,
17529 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17530 nrows_scrolled);
17532 /* Disable lines that must be updated. */
17533 for (i = 0; i < nrows_scrolled; ++i)
17534 (start_row + i)->enabled_p = false;
17536 /* Re-compute Y positions. */
17537 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17538 max_y = it.last_visible_y;
17539 for (row = start_row + nrows_scrolled;
17540 row < bottom_row;
17541 ++row)
17543 row->y = it.current_y;
17544 row->visible_height = row->height;
17546 if (row->y < min_y)
17547 row->visible_height -= min_y - row->y;
17548 if (row->y + row->height > max_y)
17549 row->visible_height -= row->y + row->height - max_y;
17550 if (row->fringe_bitmap_periodic_p)
17551 row->redraw_fringe_bitmaps_p = true;
17553 it.current_y += row->height;
17555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17556 last_reused_text_row = row;
17557 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17558 break;
17561 /* Disable lines in the current matrix which are now
17562 below the window. */
17563 for (++row; row < bottom_row; ++row)
17564 row->enabled_p = row->mode_line_p = false;
17567 /* Update window_end_pos etc.; last_reused_text_row is the last
17568 reused row from the current matrix containing text, if any.
17569 The value of last_text_row is the last displayed line
17570 containing text. */
17571 if (last_reused_text_row)
17572 adjust_window_ends (w, last_reused_text_row, true);
17573 else if (last_text_row)
17574 adjust_window_ends (w, last_text_row, false);
17575 else
17577 /* This window must be completely empty. */
17578 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17579 w->window_end_pos = Z - ZV;
17580 w->window_end_vpos = 0;
17582 w->window_end_valid = false;
17584 /* Update hint: don't try scrolling again in update_window. */
17585 w->desired_matrix->no_scrolling_p = true;
17587 #ifdef GLYPH_DEBUG
17588 debug_method_add (w, "try_window_reusing_current_matrix 1");
17589 #endif
17590 return true;
17592 else if (CHARPOS (new_start) > CHARPOS (start))
17594 struct glyph_row *pt_row, *row;
17595 struct glyph_row *first_reusable_row;
17596 struct glyph_row *first_row_to_display;
17597 int dy;
17598 int yb = window_text_bottom_y (w);
17600 /* Find the row starting at new_start, if there is one. Don't
17601 reuse a partially visible line at the end. */
17602 first_reusable_row = start_row;
17603 while (first_reusable_row->enabled_p
17604 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17605 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17606 < CHARPOS (new_start)))
17607 ++first_reusable_row;
17609 /* Give up if there is no row to reuse. */
17610 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17611 || !first_reusable_row->enabled_p
17612 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17613 != CHARPOS (new_start)))
17614 return false;
17616 /* We can reuse fully visible rows beginning with
17617 first_reusable_row to the end of the window. Set
17618 first_row_to_display to the first row that cannot be reused.
17619 Set pt_row to the row containing point, if there is any. */
17620 pt_row = NULL;
17621 for (first_row_to_display = first_reusable_row;
17622 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17623 ++first_row_to_display)
17625 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17626 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17627 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17628 && first_row_to_display->ends_at_zv_p
17629 && pt_row == NULL)))
17630 pt_row = first_row_to_display;
17633 /* Start displaying at the start of first_row_to_display. */
17634 eassert (first_row_to_display->y < yb);
17635 init_to_row_start (&it, w, first_row_to_display);
17637 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17638 - start_vpos);
17639 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17640 - nrows_scrolled);
17641 it.current_y = (first_row_to_display->y - first_reusable_row->y
17642 + WINDOW_HEADER_LINE_HEIGHT (w));
17644 /* Display lines beginning with first_row_to_display in the
17645 desired matrix. Set last_text_row to the last row displayed
17646 that displays text. */
17647 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17648 if (pt_row == NULL)
17649 w->cursor.vpos = -1;
17650 last_text_row = NULL;
17651 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17652 if (display_line (&it))
17653 last_text_row = it.glyph_row - 1;
17655 /* If point is in a reused row, adjust y and vpos of the cursor
17656 position. */
17657 if (pt_row)
17659 w->cursor.vpos -= nrows_scrolled;
17660 w->cursor.y -= first_reusable_row->y - start_row->y;
17663 /* Give up if point isn't in a row displayed or reused. (This
17664 also handles the case where w->cursor.vpos < nrows_scrolled
17665 after the calls to display_line, which can happen with scroll
17666 margins. See bug#1295.) */
17667 if (w->cursor.vpos < 0)
17669 clear_glyph_matrix (w->desired_matrix);
17670 return false;
17673 /* Scroll the display. */
17674 run.current_y = first_reusable_row->y;
17675 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17676 run.height = it.last_visible_y - run.current_y;
17677 dy = run.current_y - run.desired_y;
17679 if (run.height)
17681 update_begin (f);
17682 FRAME_RIF (f)->update_window_begin_hook (w);
17683 FRAME_RIF (f)->clear_window_mouse_face (w);
17684 FRAME_RIF (f)->scroll_run_hook (w, &run);
17685 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17686 update_end (f);
17689 /* Adjust Y positions of reused rows. */
17690 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17691 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17692 max_y = it.last_visible_y;
17693 for (row = first_reusable_row; row < first_row_to_display; ++row)
17695 row->y -= dy;
17696 row->visible_height = row->height;
17697 if (row->y < min_y)
17698 row->visible_height -= min_y - row->y;
17699 if (row->y + row->height > max_y)
17700 row->visible_height -= row->y + row->height - max_y;
17701 if (row->fringe_bitmap_periodic_p)
17702 row->redraw_fringe_bitmaps_p = true;
17705 /* Scroll the current matrix. */
17706 eassert (nrows_scrolled > 0);
17707 rotate_matrix (w->current_matrix,
17708 start_vpos,
17709 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17710 -nrows_scrolled);
17712 /* Disable rows not reused. */
17713 for (row -= nrows_scrolled; row < bottom_row; ++row)
17714 row->enabled_p = false;
17716 /* Point may have moved to a different line, so we cannot assume that
17717 the previous cursor position is valid; locate the correct row. */
17718 if (pt_row)
17720 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17721 row < bottom_row
17722 && PT >= MATRIX_ROW_END_CHARPOS (row)
17723 && !row->ends_at_zv_p;
17724 row++)
17726 w->cursor.vpos++;
17727 w->cursor.y = row->y;
17729 if (row < bottom_row)
17731 /* Can't simply scan the row for point with
17732 bidi-reordered glyph rows. Let set_cursor_from_row
17733 figure out where to put the cursor, and if it fails,
17734 give up. */
17735 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17737 if (!set_cursor_from_row (w, row, w->current_matrix,
17738 0, 0, 0, 0))
17740 clear_glyph_matrix (w->desired_matrix);
17741 return false;
17744 else
17746 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17747 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17749 for (; glyph < end
17750 && (!BUFFERP (glyph->object)
17751 || glyph->charpos < PT);
17752 glyph++)
17754 w->cursor.hpos++;
17755 w->cursor.x += glyph->pixel_width;
17761 /* Adjust window end. A null value of last_text_row means that
17762 the window end is in reused rows which in turn means that
17763 only its vpos can have changed. */
17764 if (last_text_row)
17765 adjust_window_ends (w, last_text_row, false);
17766 else
17767 w->window_end_vpos -= nrows_scrolled;
17769 w->window_end_valid = false;
17770 w->desired_matrix->no_scrolling_p = true;
17772 #ifdef GLYPH_DEBUG
17773 debug_method_add (w, "try_window_reusing_current_matrix 2");
17774 #endif
17775 return true;
17778 return false;
17783 /************************************************************************
17784 Window redisplay reusing current matrix when buffer has changed
17785 ************************************************************************/
17787 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17788 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17789 ptrdiff_t *, ptrdiff_t *);
17790 static struct glyph_row *
17791 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17792 struct glyph_row *);
17795 /* Return the last row in MATRIX displaying text. If row START is
17796 non-null, start searching with that row. IT gives the dimensions
17797 of the display. Value is null if matrix is empty; otherwise it is
17798 a pointer to the row found. */
17800 static struct glyph_row *
17801 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17802 struct glyph_row *start)
17804 struct glyph_row *row, *row_found;
17806 /* Set row_found to the last row in IT->w's current matrix
17807 displaying text. The loop looks funny but think of partially
17808 visible lines. */
17809 row_found = NULL;
17810 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17811 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17813 eassert (row->enabled_p);
17814 row_found = row;
17815 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17816 break;
17817 ++row;
17820 return row_found;
17824 /* Return the last row in the current matrix of W that is not affected
17825 by changes at the start of current_buffer that occurred since W's
17826 current matrix was built. Value is null if no such row exists.
17828 BEG_UNCHANGED us the number of characters unchanged at the start of
17829 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17830 first changed character in current_buffer. Characters at positions <
17831 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17832 when the current matrix was built. */
17834 static struct glyph_row *
17835 find_last_unchanged_at_beg_row (struct window *w)
17837 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17838 struct glyph_row *row;
17839 struct glyph_row *row_found = NULL;
17840 int yb = window_text_bottom_y (w);
17842 /* Find the last row displaying unchanged text. */
17843 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17844 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17845 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17846 ++row)
17848 if (/* If row ends before first_changed_pos, it is unchanged,
17849 except in some case. */
17850 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17851 /* When row ends in ZV and we write at ZV it is not
17852 unchanged. */
17853 && !row->ends_at_zv_p
17854 /* When first_changed_pos is the end of a continued line,
17855 row is not unchanged because it may be no longer
17856 continued. */
17857 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17858 && (row->continued_p
17859 || row->exact_window_width_line_p))
17860 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17861 needs to be recomputed, so don't consider this row as
17862 unchanged. This happens when the last line was
17863 bidi-reordered and was killed immediately before this
17864 redisplay cycle. In that case, ROW->end stores the
17865 buffer position of the first visual-order character of
17866 the killed text, which is now beyond ZV. */
17867 && CHARPOS (row->end.pos) <= ZV)
17868 row_found = row;
17870 /* Stop if last visible row. */
17871 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17872 break;
17875 return row_found;
17879 /* Find the first glyph row in the current matrix of W that is not
17880 affected by changes at the end of current_buffer since the
17881 time W's current matrix was built.
17883 Return in *DELTA the number of chars by which buffer positions in
17884 unchanged text at the end of current_buffer must be adjusted.
17886 Return in *DELTA_BYTES the corresponding number of bytes.
17888 Value is null if no such row exists, i.e. all rows are affected by
17889 changes. */
17891 static struct glyph_row *
17892 find_first_unchanged_at_end_row (struct window *w,
17893 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17895 struct glyph_row *row;
17896 struct glyph_row *row_found = NULL;
17898 *delta = *delta_bytes = 0;
17900 /* Display must not have been paused, otherwise the current matrix
17901 is not up to date. */
17902 eassert (w->window_end_valid);
17904 /* A value of window_end_pos >= END_UNCHANGED means that the window
17905 end is in the range of changed text. If so, there is no
17906 unchanged row at the end of W's current matrix. */
17907 if (w->window_end_pos >= END_UNCHANGED)
17908 return NULL;
17910 /* Set row to the last row in W's current matrix displaying text. */
17911 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17913 /* If matrix is entirely empty, no unchanged row exists. */
17914 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17916 /* The value of row is the last glyph row in the matrix having a
17917 meaningful buffer position in it. The end position of row
17918 corresponds to window_end_pos. This allows us to translate
17919 buffer positions in the current matrix to current buffer
17920 positions for characters not in changed text. */
17921 ptrdiff_t Z_old =
17922 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17923 ptrdiff_t Z_BYTE_old =
17924 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17925 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17926 struct glyph_row *first_text_row
17927 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17929 *delta = Z - Z_old;
17930 *delta_bytes = Z_BYTE - Z_BYTE_old;
17932 /* Set last_unchanged_pos to the buffer position of the last
17933 character in the buffer that has not been changed. Z is the
17934 index + 1 of the last character in current_buffer, i.e. by
17935 subtracting END_UNCHANGED we get the index of the last
17936 unchanged character, and we have to add BEG to get its buffer
17937 position. */
17938 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17939 last_unchanged_pos_old = last_unchanged_pos - *delta;
17941 /* Search backward from ROW for a row displaying a line that
17942 starts at a minimum position >= last_unchanged_pos_old. */
17943 for (; row > first_text_row; --row)
17945 /* This used to abort, but it can happen.
17946 It is ok to just stop the search instead here. KFS. */
17947 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17948 break;
17950 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17951 row_found = row;
17955 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17957 return row_found;
17961 /* Make sure that glyph rows in the current matrix of window W
17962 reference the same glyph memory as corresponding rows in the
17963 frame's frame matrix. This function is called after scrolling W's
17964 current matrix on a terminal frame in try_window_id and
17965 try_window_reusing_current_matrix. */
17967 static void
17968 sync_frame_with_window_matrix_rows (struct window *w)
17970 struct frame *f = XFRAME (w->frame);
17971 struct glyph_row *window_row, *window_row_end, *frame_row;
17973 /* Preconditions: W must be a leaf window and full-width. Its frame
17974 must have a frame matrix. */
17975 eassert (BUFFERP (w->contents));
17976 eassert (WINDOW_FULL_WIDTH_P (w));
17977 eassert (!FRAME_WINDOW_P (f));
17979 /* If W is a full-width window, glyph pointers in W's current matrix
17980 have, by definition, to be the same as glyph pointers in the
17981 corresponding frame matrix. Note that frame matrices have no
17982 marginal areas (see build_frame_matrix). */
17983 window_row = w->current_matrix->rows;
17984 window_row_end = window_row + w->current_matrix->nrows;
17985 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17986 while (window_row < window_row_end)
17988 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17989 struct glyph *end = window_row->glyphs[LAST_AREA];
17991 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17992 frame_row->glyphs[TEXT_AREA] = start;
17993 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17994 frame_row->glyphs[LAST_AREA] = end;
17996 /* Disable frame rows whose corresponding window rows have
17997 been disabled in try_window_id. */
17998 if (!window_row->enabled_p)
17999 frame_row->enabled_p = false;
18001 ++window_row, ++frame_row;
18006 /* Find the glyph row in window W containing CHARPOS. Consider all
18007 rows between START and END (not inclusive). END null means search
18008 all rows to the end of the display area of W. Value is the row
18009 containing CHARPOS or null. */
18011 struct glyph_row *
18012 row_containing_pos (struct window *w, ptrdiff_t charpos,
18013 struct glyph_row *start, struct glyph_row *end, int dy)
18015 struct glyph_row *row = start;
18016 struct glyph_row *best_row = NULL;
18017 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18018 int last_y;
18020 /* If we happen to start on a header-line, skip that. */
18021 if (row->mode_line_p)
18022 ++row;
18024 if ((end && row >= end) || !row->enabled_p)
18025 return NULL;
18027 last_y = window_text_bottom_y (w) - dy;
18029 while (true)
18031 /* Give up if we have gone too far. */
18032 if ((end && row >= end) || !row->enabled_p)
18033 return NULL;
18034 /* This formerly returned if they were equal.
18035 I think that both quantities are of a "last plus one" type;
18036 if so, when they are equal, the row is within the screen. -- rms. */
18037 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18038 return NULL;
18040 /* If it is in this row, return this row. */
18041 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18042 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18043 /* The end position of a row equals the start
18044 position of the next row. If CHARPOS is there, we
18045 would rather consider it displayed in the next
18046 line, except when this line ends in ZV. */
18047 && !row_for_charpos_p (row, charpos)))
18048 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18050 struct glyph *g;
18052 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18053 || (!best_row && !row->continued_p))
18054 return row;
18055 /* In bidi-reordered rows, there could be several rows whose
18056 edges surround CHARPOS, all of these rows belonging to
18057 the same continued line. We need to find the row which
18058 fits CHARPOS the best. */
18059 for (g = row->glyphs[TEXT_AREA];
18060 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18061 g++)
18063 if (!STRINGP (g->object))
18065 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18067 mindif = eabs (g->charpos - charpos);
18068 best_row = row;
18069 /* Exact match always wins. */
18070 if (mindif == 0)
18071 return best_row;
18076 else if (best_row && !row->continued_p)
18077 return best_row;
18078 ++row;
18083 /* Try to redisplay window W by reusing its existing display. W's
18084 current matrix must be up to date when this function is called,
18085 i.e., window_end_valid must be true.
18087 Value is
18089 >= 1 if successful, i.e. display has been updated
18090 specifically:
18091 1 means the changes were in front of a newline that precedes
18092 the window start, and the whole current matrix was reused
18093 2 means the changes were after the last position displayed
18094 in the window, and the whole current matrix was reused
18095 3 means portions of the current matrix were reused, while
18096 some of the screen lines were redrawn
18097 -1 if redisplay with same window start is known not to succeed
18098 0 if otherwise unsuccessful
18100 The following steps are performed:
18102 1. Find the last row in the current matrix of W that is not
18103 affected by changes at the start of current_buffer. If no such row
18104 is found, give up.
18106 2. Find the first row in W's current matrix that is not affected by
18107 changes at the end of current_buffer. Maybe there is no such row.
18109 3. Display lines beginning with the row + 1 found in step 1 to the
18110 row found in step 2 or, if step 2 didn't find a row, to the end of
18111 the window.
18113 4. If cursor is not known to appear on the window, give up.
18115 5. If display stopped at the row found in step 2, scroll the
18116 display and current matrix as needed.
18118 6. Maybe display some lines at the end of W, if we must. This can
18119 happen under various circumstances, like a partially visible line
18120 becoming fully visible, or because newly displayed lines are displayed
18121 in smaller font sizes.
18123 7. Update W's window end information. */
18125 static int
18126 try_window_id (struct window *w)
18128 struct frame *f = XFRAME (w->frame);
18129 struct glyph_matrix *current_matrix = w->current_matrix;
18130 struct glyph_matrix *desired_matrix = w->desired_matrix;
18131 struct glyph_row *last_unchanged_at_beg_row;
18132 struct glyph_row *first_unchanged_at_end_row;
18133 struct glyph_row *row;
18134 struct glyph_row *bottom_row;
18135 int bottom_vpos;
18136 struct it it;
18137 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18138 int dvpos, dy;
18139 struct text_pos start_pos;
18140 struct run run;
18141 int first_unchanged_at_end_vpos = 0;
18142 struct glyph_row *last_text_row, *last_text_row_at_end;
18143 struct text_pos start;
18144 ptrdiff_t first_changed_charpos, last_changed_charpos;
18146 #ifdef GLYPH_DEBUG
18147 if (inhibit_try_window_id)
18148 return 0;
18149 #endif
18151 /* This is handy for debugging. */
18152 #if false
18153 #define GIVE_UP(X) \
18154 do { \
18155 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18156 return 0; \
18157 } while (false)
18158 #else
18159 #define GIVE_UP(X) return 0
18160 #endif
18162 SET_TEXT_POS_FROM_MARKER (start, w->start);
18164 /* Don't use this for mini-windows because these can show
18165 messages and mini-buffers, and we don't handle that here. */
18166 if (MINI_WINDOW_P (w))
18167 GIVE_UP (1);
18169 /* This flag is used to prevent redisplay optimizations. */
18170 if (windows_or_buffers_changed || f->cursor_type_changed)
18171 GIVE_UP (2);
18173 /* This function's optimizations cannot be used if overlays have
18174 changed in the buffer displayed by the window, so give up if they
18175 have. */
18176 if (w->last_overlay_modified != OVERLAY_MODIFF)
18177 GIVE_UP (200);
18179 /* Verify that narrowing has not changed.
18180 Also verify that we were not told to prevent redisplay optimizations.
18181 It would be nice to further
18182 reduce the number of cases where this prevents try_window_id. */
18183 if (current_buffer->clip_changed
18184 || current_buffer->prevent_redisplay_optimizations_p)
18185 GIVE_UP (3);
18187 /* Window must either use window-based redisplay or be full width. */
18188 if (!FRAME_WINDOW_P (f)
18189 && (!FRAME_LINE_INS_DEL_OK (f)
18190 || !WINDOW_FULL_WIDTH_P (w)))
18191 GIVE_UP (4);
18193 /* Give up if point is known NOT to appear in W. */
18194 if (PT < CHARPOS (start))
18195 GIVE_UP (5);
18197 /* Another way to prevent redisplay optimizations. */
18198 if (w->last_modified == 0)
18199 GIVE_UP (6);
18201 /* Verify that window is not hscrolled. */
18202 if (w->hscroll != 0)
18203 GIVE_UP (7);
18205 /* Verify that display wasn't paused. */
18206 if (!w->window_end_valid)
18207 GIVE_UP (8);
18209 /* Likewise if highlighting trailing whitespace. */
18210 if (!NILP (Vshow_trailing_whitespace))
18211 GIVE_UP (11);
18213 /* Can't use this if overlay arrow position and/or string have
18214 changed. */
18215 if (overlay_arrows_changed_p ())
18216 GIVE_UP (12);
18218 /* When word-wrap is on, adding a space to the first word of a
18219 wrapped line can change the wrap position, altering the line
18220 above it. It might be worthwhile to handle this more
18221 intelligently, but for now just redisplay from scratch. */
18222 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18223 GIVE_UP (21);
18225 /* Under bidi reordering, adding or deleting a character in the
18226 beginning of a paragraph, before the first strong directional
18227 character, can change the base direction of the paragraph (unless
18228 the buffer specifies a fixed paragraph direction), which will
18229 require redisplaying the whole paragraph. It might be worthwhile
18230 to find the paragraph limits and widen the range of redisplayed
18231 lines to that, but for now just give up this optimization and
18232 redisplay from scratch. */
18233 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18234 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18235 GIVE_UP (22);
18237 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18238 to that variable require thorough redisplay. */
18239 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18240 GIVE_UP (23);
18242 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18243 only if buffer has really changed. The reason is that the gap is
18244 initially at Z for freshly visited files. The code below would
18245 set end_unchanged to 0 in that case. */
18246 if (MODIFF > SAVE_MODIFF
18247 /* This seems to happen sometimes after saving a buffer. */
18248 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18250 if (GPT - BEG < BEG_UNCHANGED)
18251 BEG_UNCHANGED = GPT - BEG;
18252 if (Z - GPT < END_UNCHANGED)
18253 END_UNCHANGED = Z - GPT;
18256 /* The position of the first and last character that has been changed. */
18257 first_changed_charpos = BEG + BEG_UNCHANGED;
18258 last_changed_charpos = Z - END_UNCHANGED;
18260 /* If window starts after a line end, and the last change is in
18261 front of that newline, then changes don't affect the display.
18262 This case happens with stealth-fontification. Note that although
18263 the display is unchanged, glyph positions in the matrix have to
18264 be adjusted, of course. */
18265 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18266 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18267 && ((last_changed_charpos < CHARPOS (start)
18268 && CHARPOS (start) == BEGV)
18269 || (last_changed_charpos < CHARPOS (start) - 1
18270 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18272 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18273 struct glyph_row *r0;
18275 /* Compute how many chars/bytes have been added to or removed
18276 from the buffer. */
18277 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18278 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18279 Z_delta = Z - Z_old;
18280 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18282 /* Give up if PT is not in the window. Note that it already has
18283 been checked at the start of try_window_id that PT is not in
18284 front of the window start. */
18285 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18286 GIVE_UP (13);
18288 /* If window start is unchanged, we can reuse the whole matrix
18289 as is, after adjusting glyph positions. No need to compute
18290 the window end again, since its offset from Z hasn't changed. */
18291 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18292 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18293 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18294 /* PT must not be in a partially visible line. */
18295 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18296 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18298 /* Adjust positions in the glyph matrix. */
18299 if (Z_delta || Z_delta_bytes)
18301 struct glyph_row *r1
18302 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18303 increment_matrix_positions (w->current_matrix,
18304 MATRIX_ROW_VPOS (r0, current_matrix),
18305 MATRIX_ROW_VPOS (r1, current_matrix),
18306 Z_delta, Z_delta_bytes);
18309 /* Set the cursor. */
18310 row = row_containing_pos (w, PT, r0, NULL, 0);
18311 if (row)
18312 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18313 return 1;
18317 /* Handle the case that changes are all below what is displayed in
18318 the window, and that PT is in the window. This shortcut cannot
18319 be taken if ZV is visible in the window, and text has been added
18320 there that is visible in the window. */
18321 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18322 /* ZV is not visible in the window, or there are no
18323 changes at ZV, actually. */
18324 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18325 || first_changed_charpos == last_changed_charpos))
18327 struct glyph_row *r0;
18329 /* Give up if PT is not in the window. Note that it already has
18330 been checked at the start of try_window_id that PT is not in
18331 front of the window start. */
18332 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18333 GIVE_UP (14);
18335 /* If window start is unchanged, we can reuse the whole matrix
18336 as is, without changing glyph positions since no text has
18337 been added/removed in front of the window end. */
18338 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18339 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18340 /* PT must not be in a partially visible line. */
18341 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18342 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18344 /* We have to compute the window end anew since text
18345 could have been added/removed after it. */
18346 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18347 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18349 /* Set the cursor. */
18350 row = row_containing_pos (w, PT, r0, NULL, 0);
18351 if (row)
18352 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18353 return 2;
18357 /* Give up if window start is in the changed area.
18359 The condition used to read
18361 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18363 but why that was tested escapes me at the moment. */
18364 if (CHARPOS (start) >= first_changed_charpos
18365 && CHARPOS (start) <= last_changed_charpos)
18366 GIVE_UP (15);
18368 /* Check that window start agrees with the start of the first glyph
18369 row in its current matrix. Check this after we know the window
18370 start is not in changed text, otherwise positions would not be
18371 comparable. */
18372 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18373 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18374 GIVE_UP (16);
18376 /* Give up if the window ends in strings. Overlay strings
18377 at the end are difficult to handle, so don't try. */
18378 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18379 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18380 GIVE_UP (20);
18382 /* Compute the position at which we have to start displaying new
18383 lines. Some of the lines at the top of the window might be
18384 reusable because they are not displaying changed text. Find the
18385 last row in W's current matrix not affected by changes at the
18386 start of current_buffer. Value is null if changes start in the
18387 first line of window. */
18388 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18389 if (last_unchanged_at_beg_row)
18391 /* Avoid starting to display in the middle of a character, a TAB
18392 for instance. This is easier than to set up the iterator
18393 exactly, and it's not a frequent case, so the additional
18394 effort wouldn't really pay off. */
18395 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18396 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18397 && last_unchanged_at_beg_row > w->current_matrix->rows)
18398 --last_unchanged_at_beg_row;
18400 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18401 GIVE_UP (17);
18403 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18404 GIVE_UP (18);
18405 start_pos = it.current.pos;
18407 /* Start displaying new lines in the desired matrix at the same
18408 vpos we would use in the current matrix, i.e. below
18409 last_unchanged_at_beg_row. */
18410 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18411 current_matrix);
18412 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18413 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18415 eassert (it.hpos == 0 && it.current_x == 0);
18417 else
18419 /* There are no reusable lines at the start of the window.
18420 Start displaying in the first text line. */
18421 start_display (&it, w, start);
18422 it.vpos = it.first_vpos;
18423 start_pos = it.current.pos;
18426 /* Find the first row that is not affected by changes at the end of
18427 the buffer. Value will be null if there is no unchanged row, in
18428 which case we must redisplay to the end of the window. delta
18429 will be set to the value by which buffer positions beginning with
18430 first_unchanged_at_end_row have to be adjusted due to text
18431 changes. */
18432 first_unchanged_at_end_row
18433 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18434 IF_DEBUG (debug_delta = delta);
18435 IF_DEBUG (debug_delta_bytes = delta_bytes);
18437 /* Set stop_pos to the buffer position up to which we will have to
18438 display new lines. If first_unchanged_at_end_row != NULL, this
18439 is the buffer position of the start of the line displayed in that
18440 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18441 that we don't stop at a buffer position. */
18442 stop_pos = 0;
18443 if (first_unchanged_at_end_row)
18445 eassert (last_unchanged_at_beg_row == NULL
18446 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18448 /* If this is a continuation line, move forward to the next one
18449 that isn't. Changes in lines above affect this line.
18450 Caution: this may move first_unchanged_at_end_row to a row
18451 not displaying text. */
18452 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18453 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18454 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18455 < it.last_visible_y))
18456 ++first_unchanged_at_end_row;
18458 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18459 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18460 >= it.last_visible_y))
18461 first_unchanged_at_end_row = NULL;
18462 else
18464 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18465 + delta);
18466 first_unchanged_at_end_vpos
18467 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18468 eassert (stop_pos >= Z - END_UNCHANGED);
18471 else if (last_unchanged_at_beg_row == NULL)
18472 GIVE_UP (19);
18475 #ifdef GLYPH_DEBUG
18477 /* Either there is no unchanged row at the end, or the one we have
18478 now displays text. This is a necessary condition for the window
18479 end pos calculation at the end of this function. */
18480 eassert (first_unchanged_at_end_row == NULL
18481 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18483 debug_last_unchanged_at_beg_vpos
18484 = (last_unchanged_at_beg_row
18485 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18486 : -1);
18487 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18489 #endif /* GLYPH_DEBUG */
18492 /* Display new lines. Set last_text_row to the last new line
18493 displayed which has text on it, i.e. might end up as being the
18494 line where the window_end_vpos is. */
18495 w->cursor.vpos = -1;
18496 last_text_row = NULL;
18497 overlay_arrow_seen = false;
18498 if (it.current_y < it.last_visible_y
18499 && !f->fonts_changed
18500 && (first_unchanged_at_end_row == NULL
18501 || IT_CHARPOS (it) < stop_pos))
18502 it.glyph_row->reversed_p = false;
18503 while (it.current_y < it.last_visible_y
18504 && !f->fonts_changed
18505 && (first_unchanged_at_end_row == NULL
18506 || IT_CHARPOS (it) < stop_pos))
18508 if (display_line (&it))
18509 last_text_row = it.glyph_row - 1;
18512 if (f->fonts_changed)
18513 return -1;
18515 /* The redisplay iterations in display_line above could have
18516 triggered font-lock, which could have done something that
18517 invalidates IT->w window's end-point information, on which we
18518 rely below. E.g., one package, which will remain unnamed, used
18519 to install a font-lock-fontify-region-function that called
18520 bury-buffer, whose side effect is to switch the buffer displayed
18521 by IT->w, and that predictably resets IT->w's window_end_valid
18522 flag, which we already tested at the entry to this function.
18523 Amply punish such packages/modes by giving up on this
18524 optimization in those cases. */
18525 if (!w->window_end_valid)
18527 clear_glyph_matrix (w->desired_matrix);
18528 return -1;
18531 /* Compute differences in buffer positions, y-positions etc. for
18532 lines reused at the bottom of the window. Compute what we can
18533 scroll. */
18534 if (first_unchanged_at_end_row
18535 /* No lines reused because we displayed everything up to the
18536 bottom of the window. */
18537 && it.current_y < it.last_visible_y)
18539 dvpos = (it.vpos
18540 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18541 current_matrix));
18542 dy = it.current_y - first_unchanged_at_end_row->y;
18543 run.current_y = first_unchanged_at_end_row->y;
18544 run.desired_y = run.current_y + dy;
18545 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18547 else
18549 delta = delta_bytes = dvpos = dy
18550 = run.current_y = run.desired_y = run.height = 0;
18551 first_unchanged_at_end_row = NULL;
18553 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18556 /* Find the cursor if not already found. We have to decide whether
18557 PT will appear on this window (it sometimes doesn't, but this is
18558 not a very frequent case.) This decision has to be made before
18559 the current matrix is altered. A value of cursor.vpos < 0 means
18560 that PT is either in one of the lines beginning at
18561 first_unchanged_at_end_row or below the window. Don't care for
18562 lines that might be displayed later at the window end; as
18563 mentioned, this is not a frequent case. */
18564 if (w->cursor.vpos < 0)
18566 /* Cursor in unchanged rows at the top? */
18567 if (PT < CHARPOS (start_pos)
18568 && last_unchanged_at_beg_row)
18570 row = row_containing_pos (w, PT,
18571 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18572 last_unchanged_at_beg_row + 1, 0);
18573 if (row)
18574 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18577 /* Start from first_unchanged_at_end_row looking for PT. */
18578 else if (first_unchanged_at_end_row)
18580 row = row_containing_pos (w, PT - delta,
18581 first_unchanged_at_end_row, NULL, 0);
18582 if (row)
18583 set_cursor_from_row (w, row, w->current_matrix, delta,
18584 delta_bytes, dy, dvpos);
18587 /* Give up if cursor was not found. */
18588 if (w->cursor.vpos < 0)
18590 clear_glyph_matrix (w->desired_matrix);
18591 return -1;
18595 /* Don't let the cursor end in the scroll margins. */
18597 int this_scroll_margin, cursor_height;
18598 int frame_line_height = default_line_pixel_height (w);
18599 int window_total_lines
18600 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18602 this_scroll_margin =
18603 max (0, min (scroll_margin, window_total_lines / 4));
18604 this_scroll_margin *= frame_line_height;
18605 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18607 if ((w->cursor.y < this_scroll_margin
18608 && CHARPOS (start) > BEGV)
18609 /* Old redisplay didn't take scroll margin into account at the bottom,
18610 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18611 || (w->cursor.y + (make_cursor_line_fully_visible_p
18612 ? cursor_height + this_scroll_margin
18613 : 1)) > it.last_visible_y)
18615 w->cursor.vpos = -1;
18616 clear_glyph_matrix (w->desired_matrix);
18617 return -1;
18621 /* Scroll the display. Do it before changing the current matrix so
18622 that xterm.c doesn't get confused about where the cursor glyph is
18623 found. */
18624 if (dy && run.height)
18626 update_begin (f);
18628 if (FRAME_WINDOW_P (f))
18630 FRAME_RIF (f)->update_window_begin_hook (w);
18631 FRAME_RIF (f)->clear_window_mouse_face (w);
18632 FRAME_RIF (f)->scroll_run_hook (w, &run);
18633 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18635 else
18637 /* Terminal frame. In this case, dvpos gives the number of
18638 lines to scroll by; dvpos < 0 means scroll up. */
18639 int from_vpos
18640 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18641 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18642 int end = (WINDOW_TOP_EDGE_LINE (w)
18643 + WINDOW_WANTS_HEADER_LINE_P (w)
18644 + window_internal_height (w));
18646 #if defined (HAVE_GPM) || defined (MSDOS)
18647 x_clear_window_mouse_face (w);
18648 #endif
18649 /* Perform the operation on the screen. */
18650 if (dvpos > 0)
18652 /* Scroll last_unchanged_at_beg_row to the end of the
18653 window down dvpos lines. */
18654 set_terminal_window (f, end);
18656 /* On dumb terminals delete dvpos lines at the end
18657 before inserting dvpos empty lines. */
18658 if (!FRAME_SCROLL_REGION_OK (f))
18659 ins_del_lines (f, end - dvpos, -dvpos);
18661 /* Insert dvpos empty lines in front of
18662 last_unchanged_at_beg_row. */
18663 ins_del_lines (f, from, dvpos);
18665 else if (dvpos < 0)
18667 /* Scroll up last_unchanged_at_beg_vpos to the end of
18668 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18669 set_terminal_window (f, end);
18671 /* Delete dvpos lines in front of
18672 last_unchanged_at_beg_vpos. ins_del_lines will set
18673 the cursor to the given vpos and emit |dvpos| delete
18674 line sequences. */
18675 ins_del_lines (f, from + dvpos, dvpos);
18677 /* On a dumb terminal insert dvpos empty lines at the
18678 end. */
18679 if (!FRAME_SCROLL_REGION_OK (f))
18680 ins_del_lines (f, end + dvpos, -dvpos);
18683 set_terminal_window (f, 0);
18686 update_end (f);
18689 /* Shift reused rows of the current matrix to the right position.
18690 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18691 text. */
18692 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18693 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18694 if (dvpos < 0)
18696 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18697 bottom_vpos, dvpos);
18698 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18699 bottom_vpos);
18701 else if (dvpos > 0)
18703 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18704 bottom_vpos, dvpos);
18705 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18706 first_unchanged_at_end_vpos + dvpos);
18709 /* For frame-based redisplay, make sure that current frame and window
18710 matrix are in sync with respect to glyph memory. */
18711 if (!FRAME_WINDOW_P (f))
18712 sync_frame_with_window_matrix_rows (w);
18714 /* Adjust buffer positions in reused rows. */
18715 if (delta || delta_bytes)
18716 increment_matrix_positions (current_matrix,
18717 first_unchanged_at_end_vpos + dvpos,
18718 bottom_vpos, delta, delta_bytes);
18720 /* Adjust Y positions. */
18721 if (dy)
18722 shift_glyph_matrix (w, current_matrix,
18723 first_unchanged_at_end_vpos + dvpos,
18724 bottom_vpos, dy);
18726 if (first_unchanged_at_end_row)
18728 first_unchanged_at_end_row += dvpos;
18729 if (first_unchanged_at_end_row->y >= it.last_visible_y
18730 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18731 first_unchanged_at_end_row = NULL;
18734 /* If scrolling up, there may be some lines to display at the end of
18735 the window. */
18736 last_text_row_at_end = NULL;
18737 if (dy < 0)
18739 /* Scrolling up can leave for example a partially visible line
18740 at the end of the window to be redisplayed. */
18741 /* Set last_row to the glyph row in the current matrix where the
18742 window end line is found. It has been moved up or down in
18743 the matrix by dvpos. */
18744 int last_vpos = w->window_end_vpos + dvpos;
18745 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18747 /* If last_row is the window end line, it should display text. */
18748 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18750 /* If window end line was partially visible before, begin
18751 displaying at that line. Otherwise begin displaying with the
18752 line following it. */
18753 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18755 init_to_row_start (&it, w, last_row);
18756 it.vpos = last_vpos;
18757 it.current_y = last_row->y;
18759 else
18761 init_to_row_end (&it, w, last_row);
18762 it.vpos = 1 + last_vpos;
18763 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18764 ++last_row;
18767 /* We may start in a continuation line. If so, we have to
18768 get the right continuation_lines_width and current_x. */
18769 it.continuation_lines_width = last_row->continuation_lines_width;
18770 it.hpos = it.current_x = 0;
18772 /* Display the rest of the lines at the window end. */
18773 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18774 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18776 /* Is it always sure that the display agrees with lines in
18777 the current matrix? I don't think so, so we mark rows
18778 displayed invalid in the current matrix by setting their
18779 enabled_p flag to false. */
18780 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18781 if (display_line (&it))
18782 last_text_row_at_end = it.glyph_row - 1;
18786 /* Update window_end_pos and window_end_vpos. */
18787 if (first_unchanged_at_end_row && !last_text_row_at_end)
18789 /* Window end line if one of the preserved rows from the current
18790 matrix. Set row to the last row displaying text in current
18791 matrix starting at first_unchanged_at_end_row, after
18792 scrolling. */
18793 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18794 row = find_last_row_displaying_text (w->current_matrix, &it,
18795 first_unchanged_at_end_row);
18796 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18797 adjust_window_ends (w, row, true);
18798 eassert (w->window_end_bytepos >= 0);
18799 IF_DEBUG (debug_method_add (w, "A"));
18801 else if (last_text_row_at_end)
18803 adjust_window_ends (w, last_text_row_at_end, false);
18804 eassert (w->window_end_bytepos >= 0);
18805 IF_DEBUG (debug_method_add (w, "B"));
18807 else if (last_text_row)
18809 /* We have displayed either to the end of the window or at the
18810 end of the window, i.e. the last row with text is to be found
18811 in the desired matrix. */
18812 adjust_window_ends (w, last_text_row, false);
18813 eassert (w->window_end_bytepos >= 0);
18815 else if (first_unchanged_at_end_row == NULL
18816 && last_text_row == NULL
18817 && last_text_row_at_end == NULL)
18819 /* Displayed to end of window, but no line containing text was
18820 displayed. Lines were deleted at the end of the window. */
18821 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18822 int vpos = w->window_end_vpos;
18823 struct glyph_row *current_row = current_matrix->rows + vpos;
18824 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18826 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18828 eassert (first_vpos <= vpos);
18829 if (desired_row->enabled_p)
18831 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18832 row = desired_row;
18834 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18835 row = current_row;
18838 w->window_end_vpos = vpos + 1;
18839 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18840 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18841 eassert (w->window_end_bytepos >= 0);
18842 IF_DEBUG (debug_method_add (w, "C"));
18844 else
18845 emacs_abort ();
18847 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18848 debug_end_vpos = w->window_end_vpos));
18850 /* Record that display has not been completed. */
18851 w->window_end_valid = false;
18852 w->desired_matrix->no_scrolling_p = true;
18853 return 3;
18855 #undef GIVE_UP
18860 /***********************************************************************
18861 More debugging support
18862 ***********************************************************************/
18864 #ifdef GLYPH_DEBUG
18866 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18867 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18868 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18871 /* Dump the contents of glyph matrix MATRIX on stderr.
18873 GLYPHS 0 means don't show glyph contents.
18874 GLYPHS 1 means show glyphs in short form
18875 GLYPHS > 1 means show glyphs in long form. */
18877 void
18878 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18880 int i;
18881 for (i = 0; i < matrix->nrows; ++i)
18882 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18886 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18887 the glyph row and area where the glyph comes from. */
18889 void
18890 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18892 if (glyph->type == CHAR_GLYPH
18893 || glyph->type == GLYPHLESS_GLYPH)
18895 fprintf (stderr,
18896 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18897 glyph - row->glyphs[TEXT_AREA],
18898 (glyph->type == CHAR_GLYPH
18899 ? 'C'
18900 : 'G'),
18901 glyph->charpos,
18902 (BUFFERP (glyph->object)
18903 ? 'B'
18904 : (STRINGP (glyph->object)
18905 ? 'S'
18906 : (NILP (glyph->object)
18907 ? '0'
18908 : '-'))),
18909 glyph->pixel_width,
18910 glyph->u.ch,
18911 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18912 ? glyph->u.ch
18913 : '.'),
18914 glyph->face_id,
18915 glyph->left_box_line_p,
18916 glyph->right_box_line_p);
18918 else if (glyph->type == STRETCH_GLYPH)
18920 fprintf (stderr,
18921 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18922 glyph - row->glyphs[TEXT_AREA],
18923 'S',
18924 glyph->charpos,
18925 (BUFFERP (glyph->object)
18926 ? 'B'
18927 : (STRINGP (glyph->object)
18928 ? 'S'
18929 : (NILP (glyph->object)
18930 ? '0'
18931 : '-'))),
18932 glyph->pixel_width,
18934 ' ',
18935 glyph->face_id,
18936 glyph->left_box_line_p,
18937 glyph->right_box_line_p);
18939 else if (glyph->type == IMAGE_GLYPH)
18941 fprintf (stderr,
18942 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18943 glyph - row->glyphs[TEXT_AREA],
18944 'I',
18945 glyph->charpos,
18946 (BUFFERP (glyph->object)
18947 ? 'B'
18948 : (STRINGP (glyph->object)
18949 ? 'S'
18950 : (NILP (glyph->object)
18951 ? '0'
18952 : '-'))),
18953 glyph->pixel_width,
18954 glyph->u.img_id,
18955 '.',
18956 glyph->face_id,
18957 glyph->left_box_line_p,
18958 glyph->right_box_line_p);
18960 else if (glyph->type == COMPOSITE_GLYPH)
18962 fprintf (stderr,
18963 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18964 glyph - row->glyphs[TEXT_AREA],
18965 '+',
18966 glyph->charpos,
18967 (BUFFERP (glyph->object)
18968 ? 'B'
18969 : (STRINGP (glyph->object)
18970 ? 'S'
18971 : (NILP (glyph->object)
18972 ? '0'
18973 : '-'))),
18974 glyph->pixel_width,
18975 glyph->u.cmp.id);
18976 if (glyph->u.cmp.automatic)
18977 fprintf (stderr,
18978 "[%d-%d]",
18979 glyph->slice.cmp.from, glyph->slice.cmp.to);
18980 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18981 glyph->face_id,
18982 glyph->left_box_line_p,
18983 glyph->right_box_line_p);
18985 else if (glyph->type == XWIDGET_GLYPH)
18987 #ifndef HAVE_XWIDGETS
18988 eassume (false);
18989 #else
18990 fprintf (stderr,
18991 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18992 glyph - row->glyphs[TEXT_AREA],
18993 'X',
18994 glyph->charpos,
18995 (BUFFERP (glyph->object)
18996 ? 'B'
18997 : (STRINGP (glyph->object)
18998 ? 'S'
18999 : '-')),
19000 glyph->pixel_width,
19001 glyph->u.xwidget,
19002 '.',
19003 glyph->face_id,
19004 glyph->left_box_line_p,
19005 glyph->right_box_line_p);
19006 #endif
19011 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19012 GLYPHS 0 means don't show glyph contents.
19013 GLYPHS 1 means show glyphs in short form
19014 GLYPHS > 1 means show glyphs in long form. */
19016 void
19017 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19019 if (glyphs != 1)
19021 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19022 fprintf (stderr, "==============================================================================\n");
19024 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19025 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19026 vpos,
19027 MATRIX_ROW_START_CHARPOS (row),
19028 MATRIX_ROW_END_CHARPOS (row),
19029 row->used[TEXT_AREA],
19030 row->contains_overlapping_glyphs_p,
19031 row->enabled_p,
19032 row->truncated_on_left_p,
19033 row->truncated_on_right_p,
19034 row->continued_p,
19035 MATRIX_ROW_CONTINUATION_LINE_P (row),
19036 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19037 row->ends_at_zv_p,
19038 row->fill_line_p,
19039 row->ends_in_middle_of_char_p,
19040 row->starts_in_middle_of_char_p,
19041 row->mouse_face_p,
19042 row->x,
19043 row->y,
19044 row->pixel_width,
19045 row->height,
19046 row->visible_height,
19047 row->ascent,
19048 row->phys_ascent);
19049 /* The next 3 lines should align to "Start" in the header. */
19050 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19051 row->end.overlay_string_index,
19052 row->continuation_lines_width);
19053 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19054 CHARPOS (row->start.string_pos),
19055 CHARPOS (row->end.string_pos));
19056 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19057 row->end.dpvec_index);
19060 if (glyphs > 1)
19062 int area;
19064 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19066 struct glyph *glyph = row->glyphs[area];
19067 struct glyph *glyph_end = glyph + row->used[area];
19069 /* Glyph for a line end in text. */
19070 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19071 ++glyph_end;
19073 if (glyph < glyph_end)
19074 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19076 for (; glyph < glyph_end; ++glyph)
19077 dump_glyph (row, glyph, area);
19080 else if (glyphs == 1)
19082 int area;
19083 char s[SHRT_MAX + 4];
19085 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19087 int i;
19089 for (i = 0; i < row->used[area]; ++i)
19091 struct glyph *glyph = row->glyphs[area] + i;
19092 if (i == row->used[area] - 1
19093 && area == TEXT_AREA
19094 && NILP (glyph->object)
19095 && glyph->type == CHAR_GLYPH
19096 && glyph->u.ch == ' ')
19098 strcpy (&s[i], "[\\n]");
19099 i += 4;
19101 else if (glyph->type == CHAR_GLYPH
19102 && glyph->u.ch < 0x80
19103 && glyph->u.ch >= ' ')
19104 s[i] = glyph->u.ch;
19105 else
19106 s[i] = '.';
19109 s[i] = '\0';
19110 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19116 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19117 Sdump_glyph_matrix, 0, 1, "p",
19118 doc: /* Dump the current matrix of the selected window to stderr.
19119 Shows contents of glyph row structures. With non-nil
19120 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19121 glyphs in short form, otherwise show glyphs in long form.
19123 Interactively, no argument means show glyphs in short form;
19124 with numeric argument, its value is passed as the GLYPHS flag. */)
19125 (Lisp_Object glyphs)
19127 struct window *w = XWINDOW (selected_window);
19128 struct buffer *buffer = XBUFFER (w->contents);
19130 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19131 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19132 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19133 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19134 fprintf (stderr, "=============================================\n");
19135 dump_glyph_matrix (w->current_matrix,
19136 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19137 return Qnil;
19141 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19142 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19143 Only text-mode frames have frame glyph matrices. */)
19144 (void)
19146 struct frame *f = XFRAME (selected_frame);
19148 if (f->current_matrix)
19149 dump_glyph_matrix (f->current_matrix, 1);
19150 else
19151 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19152 return Qnil;
19156 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19157 doc: /* Dump glyph row ROW to stderr.
19158 GLYPH 0 means don't dump glyphs.
19159 GLYPH 1 means dump glyphs in short form.
19160 GLYPH > 1 or omitted means dump glyphs in long form. */)
19161 (Lisp_Object row, Lisp_Object glyphs)
19163 struct glyph_matrix *matrix;
19164 EMACS_INT vpos;
19166 CHECK_NUMBER (row);
19167 matrix = XWINDOW (selected_window)->current_matrix;
19168 vpos = XINT (row);
19169 if (vpos >= 0 && vpos < matrix->nrows)
19170 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19171 vpos,
19172 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19173 return Qnil;
19177 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19178 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19179 GLYPH 0 means don't dump glyphs.
19180 GLYPH 1 means dump glyphs in short form.
19181 GLYPH > 1 or omitted means dump glyphs in long form.
19183 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19184 do nothing. */)
19185 (Lisp_Object row, Lisp_Object glyphs)
19187 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19188 struct frame *sf = SELECTED_FRAME ();
19189 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19190 EMACS_INT vpos;
19192 CHECK_NUMBER (row);
19193 vpos = XINT (row);
19194 if (vpos >= 0 && vpos < m->nrows)
19195 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19196 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19197 #endif
19198 return Qnil;
19202 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19203 doc: /* Toggle tracing of redisplay.
19204 With ARG, turn tracing on if and only if ARG is positive. */)
19205 (Lisp_Object arg)
19207 if (NILP (arg))
19208 trace_redisplay_p = !trace_redisplay_p;
19209 else
19211 arg = Fprefix_numeric_value (arg);
19212 trace_redisplay_p = XINT (arg) > 0;
19215 return Qnil;
19219 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19220 doc: /* Like `format', but print result to stderr.
19221 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19222 (ptrdiff_t nargs, Lisp_Object *args)
19224 Lisp_Object s = Fformat (nargs, args);
19225 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19226 return Qnil;
19229 #endif /* GLYPH_DEBUG */
19233 /***********************************************************************
19234 Building Desired Matrix Rows
19235 ***********************************************************************/
19237 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19238 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19240 static struct glyph_row *
19241 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19243 struct frame *f = XFRAME (WINDOW_FRAME (w));
19244 struct buffer *buffer = XBUFFER (w->contents);
19245 struct buffer *old = current_buffer;
19246 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19247 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19248 const unsigned char *arrow_end = arrow_string + arrow_len;
19249 const unsigned char *p;
19250 struct it it;
19251 bool multibyte_p;
19252 int n_glyphs_before;
19254 set_buffer_temp (buffer);
19255 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19256 scratch_glyph_row.reversed_p = false;
19257 it.glyph_row->used[TEXT_AREA] = 0;
19258 SET_TEXT_POS (it.position, 0, 0);
19260 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19261 p = arrow_string;
19262 while (p < arrow_end)
19264 Lisp_Object face, ilisp;
19266 /* Get the next character. */
19267 if (multibyte_p)
19268 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19269 else
19271 it.c = it.char_to_display = *p, it.len = 1;
19272 if (! ASCII_CHAR_P (it.c))
19273 it.char_to_display = BYTE8_TO_CHAR (it.c);
19275 p += it.len;
19277 /* Get its face. */
19278 ilisp = make_number (p - arrow_string);
19279 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19280 it.face_id = compute_char_face (f, it.char_to_display, face);
19282 /* Compute its width, get its glyphs. */
19283 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19284 SET_TEXT_POS (it.position, -1, -1);
19285 PRODUCE_GLYPHS (&it);
19287 /* If this character doesn't fit any more in the line, we have
19288 to remove some glyphs. */
19289 if (it.current_x > it.last_visible_x)
19291 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19292 break;
19296 set_buffer_temp (old);
19297 return it.glyph_row;
19301 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19302 glyphs to insert is determined by produce_special_glyphs. */
19304 static void
19305 insert_left_trunc_glyphs (struct it *it)
19307 struct it truncate_it;
19308 struct glyph *from, *end, *to, *toend;
19310 eassert (!FRAME_WINDOW_P (it->f)
19311 || (!it->glyph_row->reversed_p
19312 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19313 || (it->glyph_row->reversed_p
19314 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19316 /* Get the truncation glyphs. */
19317 truncate_it = *it;
19318 truncate_it.current_x = 0;
19319 truncate_it.face_id = DEFAULT_FACE_ID;
19320 truncate_it.glyph_row = &scratch_glyph_row;
19321 truncate_it.area = TEXT_AREA;
19322 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19323 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19324 truncate_it.object = Qnil;
19325 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19327 /* Overwrite glyphs from IT with truncation glyphs. */
19328 if (!it->glyph_row->reversed_p)
19330 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19332 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19333 end = from + tused;
19334 to = it->glyph_row->glyphs[TEXT_AREA];
19335 toend = to + it->glyph_row->used[TEXT_AREA];
19336 if (FRAME_WINDOW_P (it->f))
19338 /* On GUI frames, when variable-size fonts are displayed,
19339 the truncation glyphs may need more pixels than the row's
19340 glyphs they overwrite. We overwrite more glyphs to free
19341 enough screen real estate, and enlarge the stretch glyph
19342 on the right (see display_line), if there is one, to
19343 preserve the screen position of the truncation glyphs on
19344 the right. */
19345 int w = 0;
19346 struct glyph *g = to;
19347 short used;
19349 /* The first glyph could be partially visible, in which case
19350 it->glyph_row->x will be negative. But we want the left
19351 truncation glyphs to be aligned at the left margin of the
19352 window, so we override the x coordinate at which the row
19353 will begin. */
19354 it->glyph_row->x = 0;
19355 while (g < toend && w < it->truncation_pixel_width)
19357 w += g->pixel_width;
19358 ++g;
19360 if (g - to - tused > 0)
19362 memmove (to + tused, g, (toend - g) * sizeof(*g));
19363 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19365 used = it->glyph_row->used[TEXT_AREA];
19366 if (it->glyph_row->truncated_on_right_p
19367 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19368 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19369 == STRETCH_GLYPH)
19371 int extra = w - it->truncation_pixel_width;
19373 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19377 while (from < end)
19378 *to++ = *from++;
19380 /* There may be padding glyphs left over. Overwrite them too. */
19381 if (!FRAME_WINDOW_P (it->f))
19383 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19385 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19386 while (from < end)
19387 *to++ = *from++;
19391 if (to > toend)
19392 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19394 else
19396 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19398 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19399 that back to front. */
19400 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19401 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19402 toend = it->glyph_row->glyphs[TEXT_AREA];
19403 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19404 if (FRAME_WINDOW_P (it->f))
19406 int w = 0;
19407 struct glyph *g = to;
19409 while (g >= toend && w < it->truncation_pixel_width)
19411 w += g->pixel_width;
19412 --g;
19414 if (to - g - tused > 0)
19415 to = g + tused;
19416 if (it->glyph_row->truncated_on_right_p
19417 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19418 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19420 int extra = w - it->truncation_pixel_width;
19422 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19426 while (from >= end && to >= toend)
19427 *to-- = *from--;
19428 if (!FRAME_WINDOW_P (it->f))
19430 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19432 from =
19433 truncate_it.glyph_row->glyphs[TEXT_AREA]
19434 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19435 while (from >= end && to >= toend)
19436 *to-- = *from--;
19439 if (from >= end)
19441 /* Need to free some room before prepending additional
19442 glyphs. */
19443 int move_by = from - end + 1;
19444 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19445 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19447 for ( ; g >= g0; g--)
19448 g[move_by] = *g;
19449 while (from >= end)
19450 *to-- = *from--;
19451 it->glyph_row->used[TEXT_AREA] += move_by;
19456 /* Compute the hash code for ROW. */
19457 unsigned
19458 row_hash (struct glyph_row *row)
19460 int area, k;
19461 unsigned hashval = 0;
19463 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19464 for (k = 0; k < row->used[area]; ++k)
19465 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19466 + row->glyphs[area][k].u.val
19467 + row->glyphs[area][k].face_id
19468 + row->glyphs[area][k].padding_p
19469 + (row->glyphs[area][k].type << 2));
19471 return hashval;
19474 /* Compute the pixel height and width of IT->glyph_row.
19476 Most of the time, ascent and height of a display line will be equal
19477 to the max_ascent and max_height values of the display iterator
19478 structure. This is not the case if
19480 1. We hit ZV without displaying anything. In this case, max_ascent
19481 and max_height will be zero.
19483 2. We have some glyphs that don't contribute to the line height.
19484 (The glyph row flag contributes_to_line_height_p is for future
19485 pixmap extensions).
19487 The first case is easily covered by using default values because in
19488 these cases, the line height does not really matter, except that it
19489 must not be zero. */
19491 static void
19492 compute_line_metrics (struct it *it)
19494 struct glyph_row *row = it->glyph_row;
19496 if (FRAME_WINDOW_P (it->f))
19498 int i, min_y, max_y;
19500 /* The line may consist of one space only, that was added to
19501 place the cursor on it. If so, the row's height hasn't been
19502 computed yet. */
19503 if (row->height == 0)
19505 if (it->max_ascent + it->max_descent == 0)
19506 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19507 row->ascent = it->max_ascent;
19508 row->height = it->max_ascent + it->max_descent;
19509 row->phys_ascent = it->max_phys_ascent;
19510 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19511 row->extra_line_spacing = it->max_extra_line_spacing;
19514 /* Compute the width of this line. */
19515 row->pixel_width = row->x;
19516 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19517 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19519 eassert (row->pixel_width >= 0);
19520 eassert (row->ascent >= 0 && row->height > 0);
19522 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19523 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19525 /* If first line's physical ascent is larger than its logical
19526 ascent, use the physical ascent, and make the row taller.
19527 This makes accented characters fully visible. */
19528 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19529 && row->phys_ascent > row->ascent)
19531 row->height += row->phys_ascent - row->ascent;
19532 row->ascent = row->phys_ascent;
19535 /* Compute how much of the line is visible. */
19536 row->visible_height = row->height;
19538 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19539 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19541 if (row->y < min_y)
19542 row->visible_height -= min_y - row->y;
19543 if (row->y + row->height > max_y)
19544 row->visible_height -= row->y + row->height - max_y;
19546 else
19548 row->pixel_width = row->used[TEXT_AREA];
19549 if (row->continued_p)
19550 row->pixel_width -= it->continuation_pixel_width;
19551 else if (row->truncated_on_right_p)
19552 row->pixel_width -= it->truncation_pixel_width;
19553 row->ascent = row->phys_ascent = 0;
19554 row->height = row->phys_height = row->visible_height = 1;
19555 row->extra_line_spacing = 0;
19558 /* Compute a hash code for this row. */
19559 row->hash = row_hash (row);
19561 it->max_ascent = it->max_descent = 0;
19562 it->max_phys_ascent = it->max_phys_descent = 0;
19566 /* Append one space to the glyph row of iterator IT if doing a
19567 window-based redisplay. The space has the same face as
19568 IT->face_id. Value is true if a space was added.
19570 This function is called to make sure that there is always one glyph
19571 at the end of a glyph row that the cursor can be set on under
19572 window-systems. (If there weren't such a glyph we would not know
19573 how wide and tall a box cursor should be displayed).
19575 At the same time this space let's a nicely handle clearing to the
19576 end of the line if the row ends in italic text. */
19578 static bool
19579 append_space_for_newline (struct it *it, bool default_face_p)
19581 if (FRAME_WINDOW_P (it->f))
19583 int n = it->glyph_row->used[TEXT_AREA];
19585 if (it->glyph_row->glyphs[TEXT_AREA] + n
19586 < it->glyph_row->glyphs[1 + TEXT_AREA])
19588 /* Save some values that must not be changed.
19589 Must save IT->c and IT->len because otherwise
19590 ITERATOR_AT_END_P wouldn't work anymore after
19591 append_space_for_newline has been called. */
19592 enum display_element_type saved_what = it->what;
19593 int saved_c = it->c, saved_len = it->len;
19594 int saved_char_to_display = it->char_to_display;
19595 int saved_x = it->current_x;
19596 int saved_face_id = it->face_id;
19597 bool saved_box_end = it->end_of_box_run_p;
19598 struct text_pos saved_pos;
19599 Lisp_Object saved_object;
19600 struct face *face;
19602 saved_object = it->object;
19603 saved_pos = it->position;
19605 it->what = IT_CHARACTER;
19606 memset (&it->position, 0, sizeof it->position);
19607 it->object = Qnil;
19608 it->c = it->char_to_display = ' ';
19609 it->len = 1;
19611 /* If the default face was remapped, be sure to use the
19612 remapped face for the appended newline. */
19613 if (default_face_p)
19614 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19615 else if (it->face_before_selective_p)
19616 it->face_id = it->saved_face_id;
19617 face = FACE_FROM_ID (it->f, it->face_id);
19618 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19619 /* In R2L rows, we will prepend a stretch glyph that will
19620 have the end_of_box_run_p flag set for it, so there's no
19621 need for the appended newline glyph to have that flag
19622 set. */
19623 if (it->glyph_row->reversed_p
19624 /* But if the appended newline glyph goes all the way to
19625 the end of the row, there will be no stretch glyph,
19626 so leave the box flag set. */
19627 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19628 it->end_of_box_run_p = false;
19630 PRODUCE_GLYPHS (it);
19632 #ifdef HAVE_WINDOW_SYSTEM
19633 /* Make sure this space glyph has the right ascent and
19634 descent values, or else cursor at end of line will look
19635 funny, and height of empty lines will be incorrect. */
19636 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19637 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19638 if (n == 0)
19640 Lisp_Object height, total_height;
19641 int extra_line_spacing = it->extra_line_spacing;
19642 int boff = font->baseline_offset;
19644 if (font->vertical_centering)
19645 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19647 it->object = saved_object; /* get_it_property needs this */
19648 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19649 /* Must do a subset of line height processing from
19650 x_produce_glyph for newline characters. */
19651 height = get_it_property (it, Qline_height);
19652 if (CONSP (height)
19653 && CONSP (XCDR (height))
19654 && NILP (XCDR (XCDR (height))))
19656 total_height = XCAR (XCDR (height));
19657 height = XCAR (height);
19659 else
19660 total_height = Qnil;
19661 height = calc_line_height_property (it, height, font, boff, true);
19663 if (it->override_ascent >= 0)
19665 it->ascent = it->override_ascent;
19666 it->descent = it->override_descent;
19667 boff = it->override_boff;
19669 if (EQ (height, Qt))
19670 extra_line_spacing = 0;
19671 else
19673 Lisp_Object spacing;
19675 it->phys_ascent = it->ascent;
19676 it->phys_descent = it->descent;
19677 if (!NILP (height)
19678 && XINT (height) > it->ascent + it->descent)
19679 it->ascent = XINT (height) - it->descent;
19681 if (!NILP (total_height))
19682 spacing = calc_line_height_property (it, total_height, font,
19683 boff, false);
19684 else
19686 spacing = get_it_property (it, Qline_spacing);
19687 spacing = calc_line_height_property (it, spacing, font,
19688 boff, false);
19690 if (INTEGERP (spacing))
19692 extra_line_spacing = XINT (spacing);
19693 if (!NILP (total_height))
19694 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19697 if (extra_line_spacing > 0)
19699 it->descent += extra_line_spacing;
19700 if (extra_line_spacing > it->max_extra_line_spacing)
19701 it->max_extra_line_spacing = extra_line_spacing;
19703 it->max_ascent = it->ascent;
19704 it->max_descent = it->descent;
19705 /* Make sure compute_line_metrics recomputes the row height. */
19706 it->glyph_row->height = 0;
19709 g->ascent = it->max_ascent;
19710 g->descent = it->max_descent;
19711 #endif
19713 it->override_ascent = -1;
19714 it->constrain_row_ascent_descent_p = false;
19715 it->current_x = saved_x;
19716 it->object = saved_object;
19717 it->position = saved_pos;
19718 it->what = saved_what;
19719 it->face_id = saved_face_id;
19720 it->len = saved_len;
19721 it->c = saved_c;
19722 it->char_to_display = saved_char_to_display;
19723 it->end_of_box_run_p = saved_box_end;
19724 return true;
19728 return false;
19732 /* Extend the face of the last glyph in the text area of IT->glyph_row
19733 to the end of the display line. Called from display_line. If the
19734 glyph row is empty, add a space glyph to it so that we know the
19735 face to draw. Set the glyph row flag fill_line_p. If the glyph
19736 row is R2L, prepend a stretch glyph to cover the empty space to the
19737 left of the leftmost glyph. */
19739 static void
19740 extend_face_to_end_of_line (struct it *it)
19742 struct face *face, *default_face;
19743 struct frame *f = it->f;
19745 /* If line is already filled, do nothing. Non window-system frames
19746 get a grace of one more ``pixel'' because their characters are
19747 1-``pixel'' wide, so they hit the equality too early. This grace
19748 is needed only for R2L rows that are not continued, to produce
19749 one extra blank where we could display the cursor. */
19750 if ((it->current_x >= it->last_visible_x
19751 + (!FRAME_WINDOW_P (f)
19752 && it->glyph_row->reversed_p
19753 && !it->glyph_row->continued_p))
19754 /* If the window has display margins, we will need to extend
19755 their face even if the text area is filled. */
19756 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19757 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19758 return;
19760 /* The default face, possibly remapped. */
19761 default_face = FACE_FROM_ID_OR_NULL (f,
19762 lookup_basic_face (f, DEFAULT_FACE_ID));
19764 /* Face extension extends the background and box of IT->face_id
19765 to the end of the line. If the background equals the background
19766 of the frame, we don't have to do anything. */
19767 face = FACE_FROM_ID (f, (it->face_before_selective_p
19768 ? it->saved_face_id
19769 : it->face_id));
19771 if (FRAME_WINDOW_P (f)
19772 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19773 && face->box == FACE_NO_BOX
19774 && face->background == FRAME_BACKGROUND_PIXEL (f)
19775 #ifdef HAVE_WINDOW_SYSTEM
19776 && !face->stipple
19777 #endif
19778 && !it->glyph_row->reversed_p)
19779 return;
19781 /* Set the glyph row flag indicating that the face of the last glyph
19782 in the text area has to be drawn to the end of the text area. */
19783 it->glyph_row->fill_line_p = true;
19785 /* If current character of IT is not ASCII, make sure we have the
19786 ASCII face. This will be automatically undone the next time
19787 get_next_display_element returns a multibyte character. Note
19788 that the character will always be single byte in unibyte
19789 text. */
19790 if (!ASCII_CHAR_P (it->c))
19792 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19795 if (FRAME_WINDOW_P (f))
19797 /* If the row is empty, add a space with the current face of IT,
19798 so that we know which face to draw. */
19799 if (it->glyph_row->used[TEXT_AREA] == 0)
19801 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19802 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19803 it->glyph_row->used[TEXT_AREA] = 1;
19805 /* Mode line and the header line don't have margins, and
19806 likewise the frame's tool-bar window, if there is any. */
19807 if (!(it->glyph_row->mode_line_p
19808 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19809 || (WINDOWP (f->tool_bar_window)
19810 && it->w == XWINDOW (f->tool_bar_window))
19811 #endif
19814 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19815 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19817 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19818 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19819 default_face->id;
19820 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19822 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19823 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19825 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19826 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19827 default_face->id;
19828 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19831 #ifdef HAVE_WINDOW_SYSTEM
19832 if (it->glyph_row->reversed_p)
19834 /* Prepend a stretch glyph to the row, such that the
19835 rightmost glyph will be drawn flushed all the way to the
19836 right margin of the window. The stretch glyph that will
19837 occupy the empty space, if any, to the left of the
19838 glyphs. */
19839 struct font *font = face->font ? face->font : FRAME_FONT (f);
19840 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19841 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19842 struct glyph *g;
19843 int row_width, stretch_ascent, stretch_width;
19844 struct text_pos saved_pos;
19845 int saved_face_id;
19846 bool saved_avoid_cursor, saved_box_start;
19848 for (row_width = 0, g = row_start; g < row_end; g++)
19849 row_width += g->pixel_width;
19851 /* FIXME: There are various minor display glitches in R2L
19852 rows when only one of the fringes is missing. The
19853 strange condition below produces the least bad effect. */
19854 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19855 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19856 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19857 stretch_width = window_box_width (it->w, TEXT_AREA);
19858 else
19859 stretch_width = it->last_visible_x - it->first_visible_x;
19860 stretch_width -= row_width;
19862 if (stretch_width > 0)
19864 stretch_ascent =
19865 (((it->ascent + it->descent)
19866 * FONT_BASE (font)) / FONT_HEIGHT (font));
19867 saved_pos = it->position;
19868 memset (&it->position, 0, sizeof it->position);
19869 saved_avoid_cursor = it->avoid_cursor_p;
19870 it->avoid_cursor_p = true;
19871 saved_face_id = it->face_id;
19872 saved_box_start = it->start_of_box_run_p;
19873 /* The last row's stretch glyph should get the default
19874 face, to avoid painting the rest of the window with
19875 the region face, if the region ends at ZV. */
19876 if (it->glyph_row->ends_at_zv_p)
19877 it->face_id = default_face->id;
19878 else
19879 it->face_id = face->id;
19880 it->start_of_box_run_p = false;
19881 append_stretch_glyph (it, Qnil, stretch_width,
19882 it->ascent + it->descent, stretch_ascent);
19883 it->position = saved_pos;
19884 it->avoid_cursor_p = saved_avoid_cursor;
19885 it->face_id = saved_face_id;
19886 it->start_of_box_run_p = saved_box_start;
19888 /* If stretch_width comes out negative, it means that the
19889 last glyph is only partially visible. In R2L rows, we
19890 want the leftmost glyph to be partially visible, so we
19891 need to give the row the corresponding left offset. */
19892 if (stretch_width < 0)
19893 it->glyph_row->x = stretch_width;
19895 #endif /* HAVE_WINDOW_SYSTEM */
19897 else
19899 /* Save some values that must not be changed. */
19900 int saved_x = it->current_x;
19901 struct text_pos saved_pos;
19902 Lisp_Object saved_object;
19903 enum display_element_type saved_what = it->what;
19904 int saved_face_id = it->face_id;
19906 saved_object = it->object;
19907 saved_pos = it->position;
19909 it->what = IT_CHARACTER;
19910 memset (&it->position, 0, sizeof it->position);
19911 it->object = Qnil;
19912 it->c = it->char_to_display = ' ';
19913 it->len = 1;
19915 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19916 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19917 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19918 && !it->glyph_row->mode_line_p
19919 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19921 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19922 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19924 for (it->current_x = 0; g < e; g++)
19925 it->current_x += g->pixel_width;
19927 it->area = LEFT_MARGIN_AREA;
19928 it->face_id = default_face->id;
19929 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19930 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19932 PRODUCE_GLYPHS (it);
19933 /* term.c:produce_glyphs advances it->current_x only for
19934 TEXT_AREA. */
19935 it->current_x += it->pixel_width;
19938 it->current_x = saved_x;
19939 it->area = TEXT_AREA;
19942 /* The last row's blank glyphs should get the default face, to
19943 avoid painting the rest of the window with the region face,
19944 if the region ends at ZV. */
19945 if (it->glyph_row->ends_at_zv_p)
19946 it->face_id = default_face->id;
19947 else
19948 it->face_id = face->id;
19949 PRODUCE_GLYPHS (it);
19951 while (it->current_x <= it->last_visible_x)
19952 PRODUCE_GLYPHS (it);
19954 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19955 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19956 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19957 && !it->glyph_row->mode_line_p
19958 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19960 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19961 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19963 for ( ; g < e; g++)
19964 it->current_x += g->pixel_width;
19966 it->area = RIGHT_MARGIN_AREA;
19967 it->face_id = default_face->id;
19968 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19969 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19971 PRODUCE_GLYPHS (it);
19972 it->current_x += it->pixel_width;
19975 it->area = TEXT_AREA;
19978 /* Don't count these blanks really. It would let us insert a left
19979 truncation glyph below and make us set the cursor on them, maybe. */
19980 it->current_x = saved_x;
19981 it->object = saved_object;
19982 it->position = saved_pos;
19983 it->what = saved_what;
19984 it->face_id = saved_face_id;
19989 /* Value is true if text starting at CHARPOS in current_buffer is
19990 trailing whitespace. */
19992 static bool
19993 trailing_whitespace_p (ptrdiff_t charpos)
19995 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19996 int c = 0;
19998 while (bytepos < ZV_BYTE
19999 && (c = FETCH_CHAR (bytepos),
20000 c == ' ' || c == '\t'))
20001 ++bytepos;
20003 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20005 if (bytepos != PT_BYTE)
20006 return true;
20008 return false;
20012 /* Highlight trailing whitespace, if any, in ROW. */
20014 static void
20015 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20017 int used = row->used[TEXT_AREA];
20019 if (used)
20021 struct glyph *start = row->glyphs[TEXT_AREA];
20022 struct glyph *glyph = start + used - 1;
20024 if (row->reversed_p)
20026 /* Right-to-left rows need to be processed in the opposite
20027 direction, so swap the edge pointers. */
20028 glyph = start;
20029 start = row->glyphs[TEXT_AREA] + used - 1;
20032 /* Skip over glyphs inserted to display the cursor at the
20033 end of a line, for extending the face of the last glyph
20034 to the end of the line on terminals, and for truncation
20035 and continuation glyphs. */
20036 if (!row->reversed_p)
20038 while (glyph >= start
20039 && glyph->type == CHAR_GLYPH
20040 && NILP (glyph->object))
20041 --glyph;
20043 else
20045 while (glyph <= start
20046 && glyph->type == CHAR_GLYPH
20047 && NILP (glyph->object))
20048 ++glyph;
20051 /* If last glyph is a space or stretch, and it's trailing
20052 whitespace, set the face of all trailing whitespace glyphs in
20053 IT->glyph_row to `trailing-whitespace'. */
20054 if ((row->reversed_p ? glyph <= start : glyph >= start)
20055 && BUFFERP (glyph->object)
20056 && (glyph->type == STRETCH_GLYPH
20057 || (glyph->type == CHAR_GLYPH
20058 && glyph->u.ch == ' '))
20059 && trailing_whitespace_p (glyph->charpos))
20061 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20062 if (face_id < 0)
20063 return;
20065 if (!row->reversed_p)
20067 while (glyph >= start
20068 && BUFFERP (glyph->object)
20069 && (glyph->type == STRETCH_GLYPH
20070 || (glyph->type == CHAR_GLYPH
20071 && glyph->u.ch == ' ')))
20072 (glyph--)->face_id = face_id;
20074 else
20076 while (glyph <= start
20077 && BUFFERP (glyph->object)
20078 && (glyph->type == STRETCH_GLYPH
20079 || (glyph->type == CHAR_GLYPH
20080 && glyph->u.ch == ' ')))
20081 (glyph++)->face_id = face_id;
20088 /* Value is true if glyph row ROW should be
20089 considered to hold the buffer position CHARPOS. */
20091 static bool
20092 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20094 bool result = true;
20096 if (charpos == CHARPOS (row->end.pos)
20097 || charpos == MATRIX_ROW_END_CHARPOS (row))
20099 /* Suppose the row ends on a string.
20100 Unless the row is continued, that means it ends on a newline
20101 in the string. If it's anything other than a display string
20102 (e.g., a before-string from an overlay), we don't want the
20103 cursor there. (This heuristic seems to give the optimal
20104 behavior for the various types of multi-line strings.)
20105 One exception: if the string has `cursor' property on one of
20106 its characters, we _do_ want the cursor there. */
20107 if (CHARPOS (row->end.string_pos) >= 0)
20109 if (row->continued_p)
20110 result = true;
20111 else
20113 /* Check for `display' property. */
20114 struct glyph *beg = row->glyphs[TEXT_AREA];
20115 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20116 struct glyph *glyph;
20118 result = false;
20119 for (glyph = end; glyph >= beg; --glyph)
20120 if (STRINGP (glyph->object))
20122 Lisp_Object prop
20123 = Fget_char_property (make_number (charpos),
20124 Qdisplay, Qnil);
20125 result =
20126 (!NILP (prop)
20127 && display_prop_string_p (prop, glyph->object));
20128 /* If there's a `cursor' property on one of the
20129 string's characters, this row is a cursor row,
20130 even though this is not a display string. */
20131 if (!result)
20133 Lisp_Object s = glyph->object;
20135 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20137 ptrdiff_t gpos = glyph->charpos;
20139 if (!NILP (Fget_char_property (make_number (gpos),
20140 Qcursor, s)))
20142 result = true;
20143 break;
20147 break;
20151 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20153 /* If the row ends in middle of a real character,
20154 and the line is continued, we want the cursor here.
20155 That's because CHARPOS (ROW->end.pos) would equal
20156 PT if PT is before the character. */
20157 if (!row->ends_in_ellipsis_p)
20158 result = row->continued_p;
20159 else
20160 /* If the row ends in an ellipsis, then
20161 CHARPOS (ROW->end.pos) will equal point after the
20162 invisible text. We want that position to be displayed
20163 after the ellipsis. */
20164 result = false;
20166 /* If the row ends at ZV, display the cursor at the end of that
20167 row instead of at the start of the row below. */
20168 else
20169 result = row->ends_at_zv_p;
20172 return result;
20175 /* Value is true if glyph row ROW should be
20176 used to hold the cursor. */
20178 static bool
20179 cursor_row_p (struct glyph_row *row)
20181 return row_for_charpos_p (row, PT);
20186 /* Push the property PROP so that it will be rendered at the current
20187 position in IT. Return true if PROP was successfully pushed, false
20188 otherwise. Called from handle_line_prefix to handle the
20189 `line-prefix' and `wrap-prefix' properties. */
20191 static bool
20192 push_prefix_prop (struct it *it, Lisp_Object prop)
20194 struct text_pos pos =
20195 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20197 eassert (it->method == GET_FROM_BUFFER
20198 || it->method == GET_FROM_DISPLAY_VECTOR
20199 || it->method == GET_FROM_STRING
20200 || it->method == GET_FROM_IMAGE);
20202 /* We need to save the current buffer/string position, so it will be
20203 restored by pop_it, because iterate_out_of_display_property
20204 depends on that being set correctly, but some situations leave
20205 it->position not yet set when this function is called. */
20206 push_it (it, &pos);
20208 if (STRINGP (prop))
20210 if (SCHARS (prop) == 0)
20212 pop_it (it);
20213 return false;
20216 it->string = prop;
20217 it->string_from_prefix_prop_p = true;
20218 it->multibyte_p = STRING_MULTIBYTE (it->string);
20219 it->current.overlay_string_index = -1;
20220 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20221 it->end_charpos = it->string_nchars = SCHARS (it->string);
20222 it->method = GET_FROM_STRING;
20223 it->stop_charpos = 0;
20224 it->prev_stop = 0;
20225 it->base_level_stop = 0;
20227 /* Force paragraph direction to be that of the parent
20228 buffer/string. */
20229 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20230 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20231 else
20232 it->paragraph_embedding = L2R;
20234 /* Set up the bidi iterator for this display string. */
20235 if (it->bidi_p)
20237 it->bidi_it.string.lstring = it->string;
20238 it->bidi_it.string.s = NULL;
20239 it->bidi_it.string.schars = it->end_charpos;
20240 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20241 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20242 it->bidi_it.string.unibyte = !it->multibyte_p;
20243 it->bidi_it.w = it->w;
20244 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20247 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20249 it->method = GET_FROM_STRETCH;
20250 it->object = prop;
20252 #ifdef HAVE_WINDOW_SYSTEM
20253 else if (IMAGEP (prop))
20255 it->what = IT_IMAGE;
20256 it->image_id = lookup_image (it->f, prop);
20257 it->method = GET_FROM_IMAGE;
20259 #endif /* HAVE_WINDOW_SYSTEM */
20260 else
20262 pop_it (it); /* bogus display property, give up */
20263 return false;
20266 return true;
20269 /* Return the character-property PROP at the current position in IT. */
20271 static Lisp_Object
20272 get_it_property (struct it *it, Lisp_Object prop)
20274 Lisp_Object position, object = it->object;
20276 if (STRINGP (object))
20277 position = make_number (IT_STRING_CHARPOS (*it));
20278 else if (BUFFERP (object))
20280 position = make_number (IT_CHARPOS (*it));
20281 object = it->window;
20283 else
20284 return Qnil;
20286 return Fget_char_property (position, prop, object);
20289 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20291 static void
20292 handle_line_prefix (struct it *it)
20294 Lisp_Object prefix;
20296 if (it->continuation_lines_width > 0)
20298 prefix = get_it_property (it, Qwrap_prefix);
20299 if (NILP (prefix))
20300 prefix = Vwrap_prefix;
20302 else
20304 prefix = get_it_property (it, Qline_prefix);
20305 if (NILP (prefix))
20306 prefix = Vline_prefix;
20308 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20310 /* If the prefix is wider than the window, and we try to wrap
20311 it, it would acquire its own wrap prefix, and so on till the
20312 iterator stack overflows. So, don't wrap the prefix. */
20313 it->line_wrap = TRUNCATE;
20314 it->avoid_cursor_p = true;
20320 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20321 only for R2L lines from display_line and display_string, when they
20322 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20323 the line/string needs to be continued on the next glyph row. */
20324 static void
20325 unproduce_glyphs (struct it *it, int n)
20327 struct glyph *glyph, *end;
20329 eassert (it->glyph_row);
20330 eassert (it->glyph_row->reversed_p);
20331 eassert (it->area == TEXT_AREA);
20332 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20334 if (n > it->glyph_row->used[TEXT_AREA])
20335 n = it->glyph_row->used[TEXT_AREA];
20336 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20337 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20338 for ( ; glyph < end; glyph++)
20339 glyph[-n] = *glyph;
20342 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20343 and ROW->maxpos. */
20344 static void
20345 find_row_edges (struct it *it, struct glyph_row *row,
20346 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20347 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20349 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20350 lines' rows is implemented for bidi-reordered rows. */
20352 /* ROW->minpos is the value of min_pos, the minimal buffer position
20353 we have in ROW, or ROW->start.pos if that is smaller. */
20354 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20355 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20356 else
20357 /* We didn't find buffer positions smaller than ROW->start, or
20358 didn't find _any_ valid buffer positions in any of the glyphs,
20359 so we must trust the iterator's computed positions. */
20360 row->minpos = row->start.pos;
20361 if (max_pos <= 0)
20363 max_pos = CHARPOS (it->current.pos);
20364 max_bpos = BYTEPOS (it->current.pos);
20367 /* Here are the various use-cases for ending the row, and the
20368 corresponding values for ROW->maxpos:
20370 Line ends in a newline from buffer eol_pos + 1
20371 Line is continued from buffer max_pos + 1
20372 Line is truncated on right it->current.pos
20373 Line ends in a newline from string max_pos + 1(*)
20374 (*) + 1 only when line ends in a forward scan
20375 Line is continued from string max_pos
20376 Line is continued from display vector max_pos
20377 Line is entirely from a string min_pos == max_pos
20378 Line is entirely from a display vector min_pos == max_pos
20379 Line that ends at ZV ZV
20381 If you discover other use-cases, please add them here as
20382 appropriate. */
20383 if (row->ends_at_zv_p)
20384 row->maxpos = it->current.pos;
20385 else if (row->used[TEXT_AREA])
20387 bool seen_this_string = false;
20388 struct glyph_row *r1 = row - 1;
20390 /* Did we see the same display string on the previous row? */
20391 if (STRINGP (it->object)
20392 /* this is not the first row */
20393 && row > it->w->desired_matrix->rows
20394 /* previous row is not the header line */
20395 && !r1->mode_line_p
20396 /* previous row also ends in a newline from a string */
20397 && r1->ends_in_newline_from_string_p)
20399 struct glyph *start, *end;
20401 /* Search for the last glyph of the previous row that came
20402 from buffer or string. Depending on whether the row is
20403 L2R or R2L, we need to process it front to back or the
20404 other way round. */
20405 if (!r1->reversed_p)
20407 start = r1->glyphs[TEXT_AREA];
20408 end = start + r1->used[TEXT_AREA];
20409 /* Glyphs inserted by redisplay have nil as their object. */
20410 while (end > start
20411 && NILP ((end - 1)->object)
20412 && (end - 1)->charpos <= 0)
20413 --end;
20414 if (end > start)
20416 if (EQ ((end - 1)->object, it->object))
20417 seen_this_string = true;
20419 else
20420 /* If all the glyphs of the previous row were inserted
20421 by redisplay, it means the previous row was
20422 produced from a single newline, which is only
20423 possible if that newline came from the same string
20424 as the one which produced this ROW. */
20425 seen_this_string = true;
20427 else
20429 end = r1->glyphs[TEXT_AREA] - 1;
20430 start = end + r1->used[TEXT_AREA];
20431 while (end < start
20432 && NILP ((end + 1)->object)
20433 && (end + 1)->charpos <= 0)
20434 ++end;
20435 if (end < start)
20437 if (EQ ((end + 1)->object, it->object))
20438 seen_this_string = true;
20440 else
20441 seen_this_string = true;
20444 /* Take note of each display string that covers a newline only
20445 once, the first time we see it. This is for when a display
20446 string includes more than one newline in it. */
20447 if (row->ends_in_newline_from_string_p && !seen_this_string)
20449 /* If we were scanning the buffer forward when we displayed
20450 the string, we want to account for at least one buffer
20451 position that belongs to this row (position covered by
20452 the display string), so that cursor positioning will
20453 consider this row as a candidate when point is at the end
20454 of the visual line represented by this row. This is not
20455 required when scanning back, because max_pos will already
20456 have a much larger value. */
20457 if (CHARPOS (row->end.pos) > max_pos)
20458 INC_BOTH (max_pos, max_bpos);
20459 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20461 else if (CHARPOS (it->eol_pos) > 0)
20462 SET_TEXT_POS (row->maxpos,
20463 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20464 else if (row->continued_p)
20466 /* If max_pos is different from IT's current position, it
20467 means IT->method does not belong to the display element
20468 at max_pos. However, it also means that the display
20469 element at max_pos was displayed in its entirety on this
20470 line, which is equivalent to saying that the next line
20471 starts at the next buffer position. */
20472 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20473 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20474 else
20476 INC_BOTH (max_pos, max_bpos);
20477 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20480 else if (row->truncated_on_right_p)
20481 /* display_line already called reseat_at_next_visible_line_start,
20482 which puts the iterator at the beginning of the next line, in
20483 the logical order. */
20484 row->maxpos = it->current.pos;
20485 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20486 /* A line that is entirely from a string/image/stretch... */
20487 row->maxpos = row->minpos;
20488 else
20489 emacs_abort ();
20491 else
20492 row->maxpos = it->current.pos;
20495 /* Construct the glyph row IT->glyph_row in the desired matrix of
20496 IT->w from text at the current position of IT. See dispextern.h
20497 for an overview of struct it. Value is true if
20498 IT->glyph_row displays text, as opposed to a line displaying ZV
20499 only. */
20501 static bool
20502 display_line (struct it *it)
20504 struct glyph_row *row = it->glyph_row;
20505 Lisp_Object overlay_arrow_string;
20506 struct it wrap_it;
20507 void *wrap_data = NULL;
20508 bool may_wrap = false;
20509 int wrap_x UNINIT;
20510 int wrap_row_used = -1;
20511 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20512 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20513 int wrap_row_extra_line_spacing UNINIT;
20514 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20515 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20516 int cvpos;
20517 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20518 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20519 bool pending_handle_line_prefix = false;
20521 /* We always start displaying at hpos zero even if hscrolled. */
20522 eassert (it->hpos == 0 && it->current_x == 0);
20524 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20525 >= it->w->desired_matrix->nrows)
20527 it->w->nrows_scale_factor++;
20528 it->f->fonts_changed = true;
20529 return false;
20532 /* Clear the result glyph row and enable it. */
20533 prepare_desired_row (it->w, row, false);
20535 row->y = it->current_y;
20536 row->start = it->start;
20537 row->continuation_lines_width = it->continuation_lines_width;
20538 row->displays_text_p = true;
20539 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20540 it->starts_in_middle_of_char_p = false;
20542 /* Arrange the overlays nicely for our purposes. Usually, we call
20543 display_line on only one line at a time, in which case this
20544 can't really hurt too much, or we call it on lines which appear
20545 one after another in the buffer, in which case all calls to
20546 recenter_overlay_lists but the first will be pretty cheap. */
20547 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20549 /* Move over display elements that are not visible because we are
20550 hscrolled. This may stop at an x-position < IT->first_visible_x
20551 if the first glyph is partially visible or if we hit a line end. */
20552 if (it->current_x < it->first_visible_x)
20554 enum move_it_result move_result;
20556 this_line_min_pos = row->start.pos;
20557 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20558 MOVE_TO_POS | MOVE_TO_X);
20559 /* If we are under a large hscroll, move_it_in_display_line_to
20560 could hit the end of the line without reaching
20561 it->first_visible_x. Pretend that we did reach it. This is
20562 especially important on a TTY, where we will call
20563 extend_face_to_end_of_line, which needs to know how many
20564 blank glyphs to produce. */
20565 if (it->current_x < it->first_visible_x
20566 && (move_result == MOVE_NEWLINE_OR_CR
20567 || move_result == MOVE_POS_MATCH_OR_ZV))
20568 it->current_x = it->first_visible_x;
20570 /* Record the smallest positions seen while we moved over
20571 display elements that are not visible. This is needed by
20572 redisplay_internal for optimizing the case where the cursor
20573 stays inside the same line. The rest of this function only
20574 considers positions that are actually displayed, so
20575 RECORD_MAX_MIN_POS will not otherwise record positions that
20576 are hscrolled to the left of the left edge of the window. */
20577 min_pos = CHARPOS (this_line_min_pos);
20578 min_bpos = BYTEPOS (this_line_min_pos);
20580 else if (it->area == TEXT_AREA)
20582 /* We only do this when not calling move_it_in_display_line_to
20583 above, because that function calls itself handle_line_prefix. */
20584 handle_line_prefix (it);
20586 else
20588 /* Line-prefix and wrap-prefix are always displayed in the text
20589 area. But if this is the first call to display_line after
20590 init_iterator, the iterator might have been set up to write
20591 into a marginal area, e.g. if the line begins with some
20592 display property that writes to the margins. So we need to
20593 wait with the call to handle_line_prefix until whatever
20594 writes to the margin has done its job. */
20595 pending_handle_line_prefix = true;
20598 /* Get the initial row height. This is either the height of the
20599 text hscrolled, if there is any, or zero. */
20600 row->ascent = it->max_ascent;
20601 row->height = it->max_ascent + it->max_descent;
20602 row->phys_ascent = it->max_phys_ascent;
20603 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20604 row->extra_line_spacing = it->max_extra_line_spacing;
20606 /* Utility macro to record max and min buffer positions seen until now. */
20607 #define RECORD_MAX_MIN_POS(IT) \
20608 do \
20610 bool composition_p \
20611 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20612 ptrdiff_t current_pos = \
20613 composition_p ? (IT)->cmp_it.charpos \
20614 : IT_CHARPOS (*(IT)); \
20615 ptrdiff_t current_bpos = \
20616 composition_p ? CHAR_TO_BYTE (current_pos) \
20617 : IT_BYTEPOS (*(IT)); \
20618 if (current_pos < min_pos) \
20620 min_pos = current_pos; \
20621 min_bpos = current_bpos; \
20623 if (IT_CHARPOS (*it) > max_pos) \
20625 max_pos = IT_CHARPOS (*it); \
20626 max_bpos = IT_BYTEPOS (*it); \
20629 while (false)
20631 /* Loop generating characters. The loop is left with IT on the next
20632 character to display. */
20633 while (true)
20635 int n_glyphs_before, hpos_before, x_before;
20636 int x, nglyphs;
20637 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20639 /* Retrieve the next thing to display. Value is false if end of
20640 buffer reached. */
20641 if (!get_next_display_element (it))
20643 /* Maybe add a space at the end of this line that is used to
20644 display the cursor there under X. Set the charpos of the
20645 first glyph of blank lines not corresponding to any text
20646 to -1. */
20647 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20648 row->exact_window_width_line_p = true;
20649 else if ((append_space_for_newline (it, true)
20650 && row->used[TEXT_AREA] == 1)
20651 || row->used[TEXT_AREA] == 0)
20653 row->glyphs[TEXT_AREA]->charpos = -1;
20654 row->displays_text_p = false;
20656 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20657 && (!MINI_WINDOW_P (it->w)
20658 || (minibuf_level && EQ (it->window, minibuf_window))))
20659 row->indicate_empty_line_p = true;
20662 it->continuation_lines_width = 0;
20663 row->ends_at_zv_p = true;
20664 /* A row that displays right-to-left text must always have
20665 its last face extended all the way to the end of line,
20666 even if this row ends in ZV, because we still write to
20667 the screen left to right. We also need to extend the
20668 last face if the default face is remapped to some
20669 different face, otherwise the functions that clear
20670 portions of the screen will clear with the default face's
20671 background color. */
20672 if (row->reversed_p
20673 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20674 extend_face_to_end_of_line (it);
20675 break;
20678 /* Now, get the metrics of what we want to display. This also
20679 generates glyphs in `row' (which is IT->glyph_row). */
20680 n_glyphs_before = row->used[TEXT_AREA];
20681 x = it->current_x;
20683 /* Remember the line height so far in case the next element doesn't
20684 fit on the line. */
20685 if (it->line_wrap != TRUNCATE)
20687 ascent = it->max_ascent;
20688 descent = it->max_descent;
20689 phys_ascent = it->max_phys_ascent;
20690 phys_descent = it->max_phys_descent;
20692 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20694 if (IT_DISPLAYING_WHITESPACE (it))
20695 may_wrap = true;
20696 else if (may_wrap)
20698 SAVE_IT (wrap_it, *it, wrap_data);
20699 wrap_x = x;
20700 wrap_row_used = row->used[TEXT_AREA];
20701 wrap_row_ascent = row->ascent;
20702 wrap_row_height = row->height;
20703 wrap_row_phys_ascent = row->phys_ascent;
20704 wrap_row_phys_height = row->phys_height;
20705 wrap_row_extra_line_spacing = row->extra_line_spacing;
20706 wrap_row_min_pos = min_pos;
20707 wrap_row_min_bpos = min_bpos;
20708 wrap_row_max_pos = max_pos;
20709 wrap_row_max_bpos = max_bpos;
20710 may_wrap = false;
20715 PRODUCE_GLYPHS (it);
20717 /* If this display element was in marginal areas, continue with
20718 the next one. */
20719 if (it->area != TEXT_AREA)
20721 row->ascent = max (row->ascent, it->max_ascent);
20722 row->height = max (row->height, it->max_ascent + it->max_descent);
20723 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20724 row->phys_height = max (row->phys_height,
20725 it->max_phys_ascent + it->max_phys_descent);
20726 row->extra_line_spacing = max (row->extra_line_spacing,
20727 it->max_extra_line_spacing);
20728 set_iterator_to_next (it, true);
20729 /* If we didn't handle the line/wrap prefix above, and the
20730 call to set_iterator_to_next just switched to TEXT_AREA,
20731 process the prefix now. */
20732 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20734 pending_handle_line_prefix = false;
20735 handle_line_prefix (it);
20737 continue;
20740 /* Does the display element fit on the line? If we truncate
20741 lines, we should draw past the right edge of the window. If
20742 we don't truncate, we want to stop so that we can display the
20743 continuation glyph before the right margin. If lines are
20744 continued, there are two possible strategies for characters
20745 resulting in more than 1 glyph (e.g. tabs): Display as many
20746 glyphs as possible in this line and leave the rest for the
20747 continuation line, or display the whole element in the next
20748 line. Original redisplay did the former, so we do it also. */
20749 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20750 hpos_before = it->hpos;
20751 x_before = x;
20753 if (/* Not a newline. */
20754 nglyphs > 0
20755 /* Glyphs produced fit entirely in the line. */
20756 && it->current_x < it->last_visible_x)
20758 it->hpos += nglyphs;
20759 row->ascent = max (row->ascent, it->max_ascent);
20760 row->height = max (row->height, it->max_ascent + it->max_descent);
20761 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20762 row->phys_height = max (row->phys_height,
20763 it->max_phys_ascent + it->max_phys_descent);
20764 row->extra_line_spacing = max (row->extra_line_spacing,
20765 it->max_extra_line_spacing);
20766 if (it->current_x - it->pixel_width < it->first_visible_x
20767 /* In R2L rows, we arrange in extend_face_to_end_of_line
20768 to add a right offset to the line, by a suitable
20769 change to the stretch glyph that is the leftmost
20770 glyph of the line. */
20771 && !row->reversed_p)
20772 row->x = x - it->first_visible_x;
20773 /* Record the maximum and minimum buffer positions seen so
20774 far in glyphs that will be displayed by this row. */
20775 if (it->bidi_p)
20776 RECORD_MAX_MIN_POS (it);
20778 else
20780 int i, new_x;
20781 struct glyph *glyph;
20783 for (i = 0; i < nglyphs; ++i, x = new_x)
20785 /* Identify the glyphs added by the last call to
20786 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20787 the previous glyphs. */
20788 if (!row->reversed_p)
20789 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20790 else
20791 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20792 new_x = x + glyph->pixel_width;
20794 if (/* Lines are continued. */
20795 it->line_wrap != TRUNCATE
20796 && (/* Glyph doesn't fit on the line. */
20797 new_x > it->last_visible_x
20798 /* Or it fits exactly on a window system frame. */
20799 || (new_x == it->last_visible_x
20800 && FRAME_WINDOW_P (it->f)
20801 && (row->reversed_p
20802 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20803 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20805 /* End of a continued line. */
20807 if (it->hpos == 0
20808 || (new_x == it->last_visible_x
20809 && FRAME_WINDOW_P (it->f)
20810 && (row->reversed_p
20811 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20812 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20814 /* Current glyph is the only one on the line or
20815 fits exactly on the line. We must continue
20816 the line because we can't draw the cursor
20817 after the glyph. */
20818 row->continued_p = true;
20819 it->current_x = new_x;
20820 it->continuation_lines_width += new_x;
20821 ++it->hpos;
20822 if (i == nglyphs - 1)
20824 /* If line-wrap is on, check if a previous
20825 wrap point was found. */
20826 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20827 && wrap_row_used > 0
20828 /* Even if there is a previous wrap
20829 point, continue the line here as
20830 usual, if (i) the previous character
20831 was a space or tab AND (ii) the
20832 current character is not. */
20833 && (!may_wrap
20834 || IT_DISPLAYING_WHITESPACE (it)))
20835 goto back_to_wrap;
20837 /* Record the maximum and minimum buffer
20838 positions seen so far in glyphs that will be
20839 displayed by this row. */
20840 if (it->bidi_p)
20841 RECORD_MAX_MIN_POS (it);
20842 set_iterator_to_next (it, true);
20843 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20845 if (!get_next_display_element (it))
20847 row->exact_window_width_line_p = true;
20848 it->continuation_lines_width = 0;
20849 row->continued_p = false;
20850 row->ends_at_zv_p = true;
20852 else if (ITERATOR_AT_END_OF_LINE_P (it))
20854 row->continued_p = false;
20855 row->exact_window_width_line_p = true;
20857 /* If line-wrap is on, check if a
20858 previous wrap point was found. */
20859 else if (wrap_row_used > 0
20860 /* Even if there is a previous wrap
20861 point, continue the line here as
20862 usual, if (i) the previous character
20863 was a space or tab AND (ii) the
20864 current character is not. */
20865 && (!may_wrap
20866 || IT_DISPLAYING_WHITESPACE (it)))
20867 goto back_to_wrap;
20871 else if (it->bidi_p)
20872 RECORD_MAX_MIN_POS (it);
20873 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20874 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20875 extend_face_to_end_of_line (it);
20877 else if (CHAR_GLYPH_PADDING_P (*glyph)
20878 && !FRAME_WINDOW_P (it->f))
20880 /* A padding glyph that doesn't fit on this line.
20881 This means the whole character doesn't fit
20882 on the line. */
20883 if (row->reversed_p)
20884 unproduce_glyphs (it, row->used[TEXT_AREA]
20885 - n_glyphs_before);
20886 row->used[TEXT_AREA] = n_glyphs_before;
20888 /* Fill the rest of the row with continuation
20889 glyphs like in 20.x. */
20890 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20891 < row->glyphs[1 + TEXT_AREA])
20892 produce_special_glyphs (it, IT_CONTINUATION);
20894 row->continued_p = true;
20895 it->current_x = x_before;
20896 it->continuation_lines_width += x_before;
20898 /* Restore the height to what it was before the
20899 element not fitting on the line. */
20900 it->max_ascent = ascent;
20901 it->max_descent = descent;
20902 it->max_phys_ascent = phys_ascent;
20903 it->max_phys_descent = phys_descent;
20904 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20905 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20906 extend_face_to_end_of_line (it);
20908 else if (wrap_row_used > 0)
20910 back_to_wrap:
20911 if (row->reversed_p)
20912 unproduce_glyphs (it,
20913 row->used[TEXT_AREA] - wrap_row_used);
20914 RESTORE_IT (it, &wrap_it, wrap_data);
20915 it->continuation_lines_width += wrap_x;
20916 row->used[TEXT_AREA] = wrap_row_used;
20917 row->ascent = wrap_row_ascent;
20918 row->height = wrap_row_height;
20919 row->phys_ascent = wrap_row_phys_ascent;
20920 row->phys_height = wrap_row_phys_height;
20921 row->extra_line_spacing = wrap_row_extra_line_spacing;
20922 min_pos = wrap_row_min_pos;
20923 min_bpos = wrap_row_min_bpos;
20924 max_pos = wrap_row_max_pos;
20925 max_bpos = wrap_row_max_bpos;
20926 row->continued_p = true;
20927 row->ends_at_zv_p = false;
20928 row->exact_window_width_line_p = false;
20929 it->continuation_lines_width += x;
20931 /* Make sure that a non-default face is extended
20932 up to the right margin of the window. */
20933 extend_face_to_end_of_line (it);
20935 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20937 /* A TAB that extends past the right edge of the
20938 window. This produces a single glyph on
20939 window system frames. We leave the glyph in
20940 this row and let it fill the row, but don't
20941 consume the TAB. */
20942 if ((row->reversed_p
20943 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20944 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20945 produce_special_glyphs (it, IT_CONTINUATION);
20946 it->continuation_lines_width += it->last_visible_x;
20947 row->ends_in_middle_of_char_p = true;
20948 row->continued_p = true;
20949 glyph->pixel_width = it->last_visible_x - x;
20950 it->starts_in_middle_of_char_p = true;
20951 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20952 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20953 extend_face_to_end_of_line (it);
20955 else
20957 /* Something other than a TAB that draws past
20958 the right edge of the window. Restore
20959 positions to values before the element. */
20960 if (row->reversed_p)
20961 unproduce_glyphs (it, row->used[TEXT_AREA]
20962 - (n_glyphs_before + i));
20963 row->used[TEXT_AREA] = n_glyphs_before + i;
20965 /* Display continuation glyphs. */
20966 it->current_x = x_before;
20967 it->continuation_lines_width += x;
20968 if (!FRAME_WINDOW_P (it->f)
20969 || (row->reversed_p
20970 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20971 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20972 produce_special_glyphs (it, IT_CONTINUATION);
20973 row->continued_p = true;
20975 extend_face_to_end_of_line (it);
20977 if (nglyphs > 1 && i > 0)
20979 row->ends_in_middle_of_char_p = true;
20980 it->starts_in_middle_of_char_p = true;
20983 /* Restore the height to what it was before the
20984 element not fitting on the line. */
20985 it->max_ascent = ascent;
20986 it->max_descent = descent;
20987 it->max_phys_ascent = phys_ascent;
20988 it->max_phys_descent = phys_descent;
20991 break;
20993 else if (new_x > it->first_visible_x)
20995 /* Increment number of glyphs actually displayed. */
20996 ++it->hpos;
20998 /* Record the maximum and minimum buffer positions
20999 seen so far in glyphs that will be displayed by
21000 this row. */
21001 if (it->bidi_p)
21002 RECORD_MAX_MIN_POS (it);
21004 if (x < it->first_visible_x && !row->reversed_p)
21005 /* Glyph is partially visible, i.e. row starts at
21006 negative X position. Don't do that in R2L
21007 rows, where we arrange to add a right offset to
21008 the line in extend_face_to_end_of_line, by a
21009 suitable change to the stretch glyph that is
21010 the leftmost glyph of the line. */
21011 row->x = x - it->first_visible_x;
21012 /* When the last glyph of an R2L row only fits
21013 partially on the line, we need to set row->x to a
21014 negative offset, so that the leftmost glyph is
21015 the one that is partially visible. But if we are
21016 going to produce the truncation glyph, this will
21017 be taken care of in produce_special_glyphs. */
21018 if (row->reversed_p
21019 && new_x > it->last_visible_x
21020 && !(it->line_wrap == TRUNCATE
21021 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21023 eassert (FRAME_WINDOW_P (it->f));
21024 row->x = it->last_visible_x - new_x;
21027 else
21029 /* Glyph is completely off the left margin of the
21030 window. This should not happen because of the
21031 move_it_in_display_line at the start of this
21032 function, unless the text display area of the
21033 window is empty. */
21034 eassert (it->first_visible_x <= it->last_visible_x);
21037 /* Even if this display element produced no glyphs at all,
21038 we want to record its position. */
21039 if (it->bidi_p && nglyphs == 0)
21040 RECORD_MAX_MIN_POS (it);
21042 row->ascent = max (row->ascent, it->max_ascent);
21043 row->height = max (row->height, it->max_ascent + it->max_descent);
21044 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21045 row->phys_height = max (row->phys_height,
21046 it->max_phys_ascent + it->max_phys_descent);
21047 row->extra_line_spacing = max (row->extra_line_spacing,
21048 it->max_extra_line_spacing);
21050 /* End of this display line if row is continued. */
21051 if (row->continued_p || row->ends_at_zv_p)
21052 break;
21055 at_end_of_line:
21056 /* Is this a line end? If yes, we're also done, after making
21057 sure that a non-default face is extended up to the right
21058 margin of the window. */
21059 if (ITERATOR_AT_END_OF_LINE_P (it))
21061 int used_before = row->used[TEXT_AREA];
21063 row->ends_in_newline_from_string_p = STRINGP (it->object);
21065 /* Add a space at the end of the line that is used to
21066 display the cursor there. */
21067 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21068 append_space_for_newline (it, false);
21070 /* Extend the face to the end of the line. */
21071 extend_face_to_end_of_line (it);
21073 /* Make sure we have the position. */
21074 if (used_before == 0)
21075 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21077 /* Record the position of the newline, for use in
21078 find_row_edges. */
21079 it->eol_pos = it->current.pos;
21081 /* Consume the line end. This skips over invisible lines. */
21082 set_iterator_to_next (it, true);
21083 it->continuation_lines_width = 0;
21084 break;
21087 /* Proceed with next display element. Note that this skips
21088 over lines invisible because of selective display. */
21089 set_iterator_to_next (it, true);
21091 /* If we truncate lines, we are done when the last displayed
21092 glyphs reach past the right margin of the window. */
21093 if (it->line_wrap == TRUNCATE
21094 && ((FRAME_WINDOW_P (it->f)
21095 /* Images are preprocessed in produce_image_glyph such
21096 that they are cropped at the right edge of the
21097 window, so an image glyph will always end exactly at
21098 last_visible_x, even if there's no right fringe. */
21099 && ((row->reversed_p
21100 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21101 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21102 || it->what == IT_IMAGE))
21103 ? (it->current_x >= it->last_visible_x)
21104 : (it->current_x > it->last_visible_x)))
21106 /* Maybe add truncation glyphs. */
21107 if (!FRAME_WINDOW_P (it->f)
21108 || (row->reversed_p
21109 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21110 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21112 int i, n;
21114 if (!row->reversed_p)
21116 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21117 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21118 break;
21120 else
21122 for (i = 0; i < row->used[TEXT_AREA]; i++)
21123 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21124 break;
21125 /* Remove any padding glyphs at the front of ROW, to
21126 make room for the truncation glyphs we will be
21127 adding below. The loop below always inserts at
21128 least one truncation glyph, so also remove the
21129 last glyph added to ROW. */
21130 unproduce_glyphs (it, i + 1);
21131 /* Adjust i for the loop below. */
21132 i = row->used[TEXT_AREA] - (i + 1);
21135 /* produce_special_glyphs overwrites the last glyph, so
21136 we don't want that if we want to keep that last
21137 glyph, which means it's an image. */
21138 if (it->current_x > it->last_visible_x)
21140 it->current_x = x_before;
21141 if (!FRAME_WINDOW_P (it->f))
21143 for (n = row->used[TEXT_AREA]; i < n; ++i)
21145 row->used[TEXT_AREA] = i;
21146 produce_special_glyphs (it, IT_TRUNCATION);
21149 else
21151 row->used[TEXT_AREA] = i;
21152 produce_special_glyphs (it, IT_TRUNCATION);
21154 it->hpos = hpos_before;
21157 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21159 /* Don't truncate if we can overflow newline into fringe. */
21160 if (!get_next_display_element (it))
21162 it->continuation_lines_width = 0;
21163 row->ends_at_zv_p = true;
21164 row->exact_window_width_line_p = true;
21165 break;
21167 if (ITERATOR_AT_END_OF_LINE_P (it))
21169 row->exact_window_width_line_p = true;
21170 goto at_end_of_line;
21172 it->current_x = x_before;
21173 it->hpos = hpos_before;
21176 row->truncated_on_right_p = true;
21177 it->continuation_lines_width = 0;
21178 reseat_at_next_visible_line_start (it, false);
21179 /* We insist below that IT's position be at ZV because in
21180 bidi-reordered lines the character at visible line start
21181 might not be the character that follows the newline in
21182 the logical order. */
21183 if (IT_BYTEPOS (*it) > BEG_BYTE)
21184 row->ends_at_zv_p =
21185 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21186 else
21187 row->ends_at_zv_p = false;
21188 break;
21192 if (wrap_data)
21193 bidi_unshelve_cache (wrap_data, true);
21195 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21196 at the left window margin. */
21197 if (it->first_visible_x
21198 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21200 if (!FRAME_WINDOW_P (it->f)
21201 || (((row->reversed_p
21202 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21203 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21204 /* Don't let insert_left_trunc_glyphs overwrite the
21205 first glyph of the row if it is an image. */
21206 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21207 insert_left_trunc_glyphs (it);
21208 row->truncated_on_left_p = true;
21211 /* Remember the position at which this line ends.
21213 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21214 cannot be before the call to find_row_edges below, since that is
21215 where these positions are determined. */
21216 row->end = it->current;
21217 if (!it->bidi_p)
21219 row->minpos = row->start.pos;
21220 row->maxpos = row->end.pos;
21222 else
21224 /* ROW->minpos and ROW->maxpos must be the smallest and
21225 `1 + the largest' buffer positions in ROW. But if ROW was
21226 bidi-reordered, these two positions can be anywhere in the
21227 row, so we must determine them now. */
21228 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21231 /* If the start of this line is the overlay arrow-position, then
21232 mark this glyph row as the one containing the overlay arrow.
21233 This is clearly a mess with variable size fonts. It would be
21234 better to let it be displayed like cursors under X. */
21235 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21236 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21237 !NILP (overlay_arrow_string)))
21239 /* Overlay arrow in window redisplay is a fringe bitmap. */
21240 if (STRINGP (overlay_arrow_string))
21242 struct glyph_row *arrow_row
21243 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21244 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21245 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21246 struct glyph *p = row->glyphs[TEXT_AREA];
21247 struct glyph *p2, *end;
21249 /* Copy the arrow glyphs. */
21250 while (glyph < arrow_end)
21251 *p++ = *glyph++;
21253 /* Throw away padding glyphs. */
21254 p2 = p;
21255 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21256 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21257 ++p2;
21258 if (p2 > p)
21260 while (p2 < end)
21261 *p++ = *p2++;
21262 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21265 else
21267 eassert (INTEGERP (overlay_arrow_string));
21268 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21270 overlay_arrow_seen = true;
21273 /* Highlight trailing whitespace. */
21274 if (!NILP (Vshow_trailing_whitespace))
21275 highlight_trailing_whitespace (it->f, it->glyph_row);
21277 /* Compute pixel dimensions of this line. */
21278 compute_line_metrics (it);
21280 /* Implementation note: No changes in the glyphs of ROW or in their
21281 faces can be done past this point, because compute_line_metrics
21282 computes ROW's hash value and stores it within the glyph_row
21283 structure. */
21285 /* Record whether this row ends inside an ellipsis. */
21286 row->ends_in_ellipsis_p
21287 = (it->method == GET_FROM_DISPLAY_VECTOR
21288 && it->ellipsis_p);
21290 /* Save fringe bitmaps in this row. */
21291 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21292 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21293 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21294 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21296 it->left_user_fringe_bitmap = 0;
21297 it->left_user_fringe_face_id = 0;
21298 it->right_user_fringe_bitmap = 0;
21299 it->right_user_fringe_face_id = 0;
21301 /* Maybe set the cursor. */
21302 cvpos = it->w->cursor.vpos;
21303 if ((cvpos < 0
21304 /* In bidi-reordered rows, keep checking for proper cursor
21305 position even if one has been found already, because buffer
21306 positions in such rows change non-linearly with ROW->VPOS,
21307 when a line is continued. One exception: when we are at ZV,
21308 display cursor on the first suitable glyph row, since all
21309 the empty rows after that also have their position set to ZV. */
21310 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21311 lines' rows is implemented for bidi-reordered rows. */
21312 || (it->bidi_p
21313 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21314 && PT >= MATRIX_ROW_START_CHARPOS (row)
21315 && PT <= MATRIX_ROW_END_CHARPOS (row)
21316 && cursor_row_p (row))
21317 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21319 /* Prepare for the next line. This line starts horizontally at (X
21320 HPOS) = (0 0). Vertical positions are incremented. As a
21321 convenience for the caller, IT->glyph_row is set to the next
21322 row to be used. */
21323 it->current_x = it->hpos = 0;
21324 it->current_y += row->height;
21325 SET_TEXT_POS (it->eol_pos, 0, 0);
21326 ++it->vpos;
21327 ++it->glyph_row;
21328 /* The next row should by default use the same value of the
21329 reversed_p flag as this one. set_iterator_to_next decides when
21330 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21331 the flag accordingly. */
21332 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21333 it->glyph_row->reversed_p = row->reversed_p;
21334 it->start = row->end;
21335 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21337 #undef RECORD_MAX_MIN_POS
21340 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21341 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21342 doc: /* Return paragraph direction at point in BUFFER.
21343 Value is either `left-to-right' or `right-to-left'.
21344 If BUFFER is omitted or nil, it defaults to the current buffer.
21346 Paragraph direction determines how the text in the paragraph is displayed.
21347 In left-to-right paragraphs, text begins at the left margin of the window
21348 and the reading direction is generally left to right. In right-to-left
21349 paragraphs, text begins at the right margin and is read from right to left.
21351 See also `bidi-paragraph-direction'. */)
21352 (Lisp_Object buffer)
21354 struct buffer *buf = current_buffer;
21355 struct buffer *old = buf;
21357 if (! NILP (buffer))
21359 CHECK_BUFFER (buffer);
21360 buf = XBUFFER (buffer);
21363 if (NILP (BVAR (buf, bidi_display_reordering))
21364 || NILP (BVAR (buf, enable_multibyte_characters))
21365 /* When we are loading loadup.el, the character property tables
21366 needed for bidi iteration are not yet available. */
21367 || redisplay__inhibit_bidi)
21368 return Qleft_to_right;
21369 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21370 return BVAR (buf, bidi_paragraph_direction);
21371 else
21373 /* Determine the direction from buffer text. We could try to
21374 use current_matrix if it is up to date, but this seems fast
21375 enough as it is. */
21376 struct bidi_it itb;
21377 ptrdiff_t pos = BUF_PT (buf);
21378 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21379 int c;
21380 void *itb_data = bidi_shelve_cache ();
21382 set_buffer_temp (buf);
21383 /* bidi_paragraph_init finds the base direction of the paragraph
21384 by searching forward from paragraph start. We need the base
21385 direction of the current or _previous_ paragraph, so we need
21386 to make sure we are within that paragraph. To that end, find
21387 the previous non-empty line. */
21388 if (pos >= ZV && pos > BEGV)
21389 DEC_BOTH (pos, bytepos);
21390 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21391 if (fast_looking_at (trailing_white_space,
21392 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21394 while ((c = FETCH_BYTE (bytepos)) == '\n'
21395 || c == ' ' || c == '\t' || c == '\f')
21397 if (bytepos <= BEGV_BYTE)
21398 break;
21399 bytepos--;
21400 pos--;
21402 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21403 bytepos--;
21405 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21406 itb.paragraph_dir = NEUTRAL_DIR;
21407 itb.string.s = NULL;
21408 itb.string.lstring = Qnil;
21409 itb.string.bufpos = 0;
21410 itb.string.from_disp_str = false;
21411 itb.string.unibyte = false;
21412 /* We have no window to use here for ignoring window-specific
21413 overlays. Using NULL for window pointer will cause
21414 compute_display_string_pos to use the current buffer. */
21415 itb.w = NULL;
21416 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21417 bidi_unshelve_cache (itb_data, false);
21418 set_buffer_temp (old);
21419 switch (itb.paragraph_dir)
21421 case L2R:
21422 return Qleft_to_right;
21423 break;
21424 case R2L:
21425 return Qright_to_left;
21426 break;
21427 default:
21428 emacs_abort ();
21433 DEFUN ("bidi-find-overridden-directionality",
21434 Fbidi_find_overridden_directionality,
21435 Sbidi_find_overridden_directionality, 2, 3, 0,
21436 doc: /* Return position between FROM and TO where directionality was overridden.
21438 This function returns the first character position in the specified
21439 region of OBJECT where there is a character whose `bidi-class' property
21440 is `L', but which was forced to display as `R' by a directional
21441 override, and likewise with characters whose `bidi-class' is `R'
21442 or `AL' that were forced to display as `L'.
21444 If no such character is found, the function returns nil.
21446 OBJECT is a Lisp string or buffer to search for overridden
21447 directionality, and defaults to the current buffer if nil or omitted.
21448 OBJECT can also be a window, in which case the function will search
21449 the buffer displayed in that window. Passing the window instead of
21450 a buffer is preferable when the buffer is displayed in some window,
21451 because this function will then be able to correctly account for
21452 window-specific overlays, which can affect the results.
21454 Strong directional characters `L', `R', and `AL' can have their
21455 intrinsic directionality overridden by directional override
21456 control characters RLO (u+202e) and LRO (u+202d). See the
21457 function `get-char-code-property' for a way to inquire about
21458 the `bidi-class' property of a character. */)
21459 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21461 struct buffer *buf = current_buffer;
21462 struct buffer *old = buf;
21463 struct window *w = NULL;
21464 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21465 struct bidi_it itb;
21466 ptrdiff_t from_pos, to_pos, from_bpos;
21467 void *itb_data;
21469 if (!NILP (object))
21471 if (BUFFERP (object))
21472 buf = XBUFFER (object);
21473 else if (WINDOWP (object))
21475 w = decode_live_window (object);
21476 buf = XBUFFER (w->contents);
21477 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21479 else
21480 CHECK_STRING (object);
21483 if (STRINGP (object))
21485 /* Characters in unibyte strings are always treated by bidi.c as
21486 strong LTR. */
21487 if (!STRING_MULTIBYTE (object)
21488 /* When we are loading loadup.el, the character property
21489 tables needed for bidi iteration are not yet
21490 available. */
21491 || redisplay__inhibit_bidi)
21492 return Qnil;
21494 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21495 if (from_pos >= SCHARS (object))
21496 return Qnil;
21498 /* Set up the bidi iterator. */
21499 itb_data = bidi_shelve_cache ();
21500 itb.paragraph_dir = NEUTRAL_DIR;
21501 itb.string.lstring = object;
21502 itb.string.s = NULL;
21503 itb.string.schars = SCHARS (object);
21504 itb.string.bufpos = 0;
21505 itb.string.from_disp_str = false;
21506 itb.string.unibyte = false;
21507 itb.w = w;
21508 bidi_init_it (0, 0, frame_window_p, &itb);
21510 else
21512 /* Nothing this fancy can happen in unibyte buffers, or in a
21513 buffer that disabled reordering, or if FROM is at EOB. */
21514 if (NILP (BVAR (buf, bidi_display_reordering))
21515 || NILP (BVAR (buf, enable_multibyte_characters))
21516 /* When we are loading loadup.el, the character property
21517 tables needed for bidi iteration are not yet
21518 available. */
21519 || redisplay__inhibit_bidi)
21520 return Qnil;
21522 set_buffer_temp (buf);
21523 validate_region (&from, &to);
21524 from_pos = XINT (from);
21525 to_pos = XINT (to);
21526 if (from_pos >= ZV)
21527 return Qnil;
21529 /* Set up the bidi iterator. */
21530 itb_data = bidi_shelve_cache ();
21531 from_bpos = CHAR_TO_BYTE (from_pos);
21532 if (from_pos == BEGV)
21534 itb.charpos = BEGV;
21535 itb.bytepos = BEGV_BYTE;
21537 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21539 itb.charpos = from_pos;
21540 itb.bytepos = from_bpos;
21542 else
21543 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21544 -1, &itb.bytepos);
21545 itb.paragraph_dir = NEUTRAL_DIR;
21546 itb.string.s = NULL;
21547 itb.string.lstring = Qnil;
21548 itb.string.bufpos = 0;
21549 itb.string.from_disp_str = false;
21550 itb.string.unibyte = false;
21551 itb.w = w;
21552 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21555 ptrdiff_t found;
21556 do {
21557 /* For the purposes of this function, the actual base direction of
21558 the paragraph doesn't matter, so just set it to L2R. */
21559 bidi_paragraph_init (L2R, &itb, false);
21560 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21562 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21564 bidi_unshelve_cache (itb_data, false);
21565 set_buffer_temp (old);
21567 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21570 DEFUN ("move-point-visually", Fmove_point_visually,
21571 Smove_point_visually, 1, 1, 0,
21572 doc: /* Move point in the visual order in the specified DIRECTION.
21573 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21574 left.
21576 Value is the new character position of point. */)
21577 (Lisp_Object direction)
21579 struct window *w = XWINDOW (selected_window);
21580 struct buffer *b = XBUFFER (w->contents);
21581 struct glyph_row *row;
21582 int dir;
21583 Lisp_Object paragraph_dir;
21585 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21586 (!(ROW)->continued_p \
21587 && NILP ((GLYPH)->object) \
21588 && (GLYPH)->type == CHAR_GLYPH \
21589 && (GLYPH)->u.ch == ' ' \
21590 && (GLYPH)->charpos >= 0 \
21591 && !(GLYPH)->avoid_cursor_p)
21593 CHECK_NUMBER (direction);
21594 dir = XINT (direction);
21595 if (dir > 0)
21596 dir = 1;
21597 else
21598 dir = -1;
21600 /* If current matrix is up-to-date, we can use the information
21601 recorded in the glyphs, at least as long as the goal is on the
21602 screen. */
21603 if (w->window_end_valid
21604 && !windows_or_buffers_changed
21605 && b
21606 && !b->clip_changed
21607 && !b->prevent_redisplay_optimizations_p
21608 && !window_outdated (w)
21609 /* We rely below on the cursor coordinates to be up to date, but
21610 we cannot trust them if some command moved point since the
21611 last complete redisplay. */
21612 && w->last_point == BUF_PT (b)
21613 && w->cursor.vpos >= 0
21614 && w->cursor.vpos < w->current_matrix->nrows
21615 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21617 struct glyph *g = row->glyphs[TEXT_AREA];
21618 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21619 struct glyph *gpt = g + w->cursor.hpos;
21621 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21623 if (BUFFERP (g->object) && g->charpos != PT)
21625 SET_PT (g->charpos);
21626 w->cursor.vpos = -1;
21627 return make_number (PT);
21629 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21631 ptrdiff_t new_pos;
21633 if (BUFFERP (gpt->object))
21635 new_pos = PT;
21636 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21637 new_pos += (row->reversed_p ? -dir : dir);
21638 else
21639 new_pos -= (row->reversed_p ? -dir : dir);
21641 else if (BUFFERP (g->object))
21642 new_pos = g->charpos;
21643 else
21644 break;
21645 SET_PT (new_pos);
21646 w->cursor.vpos = -1;
21647 return make_number (PT);
21649 else if (ROW_GLYPH_NEWLINE_P (row, g))
21651 /* Glyphs inserted at the end of a non-empty line for
21652 positioning the cursor have zero charpos, so we must
21653 deduce the value of point by other means. */
21654 if (g->charpos > 0)
21655 SET_PT (g->charpos);
21656 else if (row->ends_at_zv_p && PT != ZV)
21657 SET_PT (ZV);
21658 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21659 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21660 else
21661 break;
21662 w->cursor.vpos = -1;
21663 return make_number (PT);
21666 if (g == e || NILP (g->object))
21668 if (row->truncated_on_left_p || row->truncated_on_right_p)
21669 goto simulate_display;
21670 if (!row->reversed_p)
21671 row += dir;
21672 else
21673 row -= dir;
21674 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21675 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21676 goto simulate_display;
21678 if (dir > 0)
21680 if (row->reversed_p && !row->continued_p)
21682 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21683 w->cursor.vpos = -1;
21684 return make_number (PT);
21686 g = row->glyphs[TEXT_AREA];
21687 e = g + row->used[TEXT_AREA];
21688 for ( ; g < e; g++)
21690 if (BUFFERP (g->object)
21691 /* Empty lines have only one glyph, which stands
21692 for the newline, and whose charpos is the
21693 buffer position of the newline. */
21694 || ROW_GLYPH_NEWLINE_P (row, g)
21695 /* When the buffer ends in a newline, the line at
21696 EOB also has one glyph, but its charpos is -1. */
21697 || (row->ends_at_zv_p
21698 && !row->reversed_p
21699 && NILP (g->object)
21700 && g->type == CHAR_GLYPH
21701 && g->u.ch == ' '))
21703 if (g->charpos > 0)
21704 SET_PT (g->charpos);
21705 else if (!row->reversed_p
21706 && row->ends_at_zv_p
21707 && PT != ZV)
21708 SET_PT (ZV);
21709 else
21710 continue;
21711 w->cursor.vpos = -1;
21712 return make_number (PT);
21716 else
21718 if (!row->reversed_p && !row->continued_p)
21720 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21721 w->cursor.vpos = -1;
21722 return make_number (PT);
21724 e = row->glyphs[TEXT_AREA];
21725 g = e + row->used[TEXT_AREA] - 1;
21726 for ( ; g >= e; g--)
21728 if (BUFFERP (g->object)
21729 || (ROW_GLYPH_NEWLINE_P (row, g)
21730 && g->charpos > 0)
21731 /* Empty R2L lines on GUI frames have the buffer
21732 position of the newline stored in the stretch
21733 glyph. */
21734 || g->type == STRETCH_GLYPH
21735 || (row->ends_at_zv_p
21736 && row->reversed_p
21737 && NILP (g->object)
21738 && g->type == CHAR_GLYPH
21739 && g->u.ch == ' '))
21741 if (g->charpos > 0)
21742 SET_PT (g->charpos);
21743 else if (row->reversed_p
21744 && row->ends_at_zv_p
21745 && PT != ZV)
21746 SET_PT (ZV);
21747 else
21748 continue;
21749 w->cursor.vpos = -1;
21750 return make_number (PT);
21757 simulate_display:
21759 /* If we wind up here, we failed to move by using the glyphs, so we
21760 need to simulate display instead. */
21762 if (b)
21763 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21764 else
21765 paragraph_dir = Qleft_to_right;
21766 if (EQ (paragraph_dir, Qright_to_left))
21767 dir = -dir;
21768 if (PT <= BEGV && dir < 0)
21769 xsignal0 (Qbeginning_of_buffer);
21770 else if (PT >= ZV && dir > 0)
21771 xsignal0 (Qend_of_buffer);
21772 else
21774 struct text_pos pt;
21775 struct it it;
21776 int pt_x, target_x, pixel_width, pt_vpos;
21777 bool at_eol_p;
21778 bool overshoot_expected = false;
21779 bool target_is_eol_p = false;
21781 /* Setup the arena. */
21782 SET_TEXT_POS (pt, PT, PT_BYTE);
21783 start_display (&it, w, pt);
21784 /* When lines are truncated, we could be called with point
21785 outside of the windows edges, in which case move_it_*
21786 functions either prematurely stop at window's edge or jump to
21787 the next screen line, whereas we rely below on our ability to
21788 reach point, in order to start from its X coordinate. So we
21789 need to disregard the window's horizontal extent in that case. */
21790 if (it.line_wrap == TRUNCATE)
21791 it.last_visible_x = INFINITY;
21793 if (it.cmp_it.id < 0
21794 && it.method == GET_FROM_STRING
21795 && it.area == TEXT_AREA
21796 && it.string_from_display_prop_p
21797 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21798 overshoot_expected = true;
21800 /* Find the X coordinate of point. We start from the beginning
21801 of this or previous line to make sure we are before point in
21802 the logical order (since the move_it_* functions can only
21803 move forward). */
21804 reseat:
21805 reseat_at_previous_visible_line_start (&it);
21806 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21807 if (IT_CHARPOS (it) != PT)
21809 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21810 -1, -1, -1, MOVE_TO_POS);
21811 /* If we missed point because the character there is
21812 displayed out of a display vector that has more than one
21813 glyph, retry expecting overshoot. */
21814 if (it.method == GET_FROM_DISPLAY_VECTOR
21815 && it.current.dpvec_index > 0
21816 && !overshoot_expected)
21818 overshoot_expected = true;
21819 goto reseat;
21821 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21822 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21824 pt_x = it.current_x;
21825 pt_vpos = it.vpos;
21826 if (dir > 0 || overshoot_expected)
21828 struct glyph_row *row = it.glyph_row;
21830 /* When point is at beginning of line, we don't have
21831 information about the glyph there loaded into struct
21832 it. Calling get_next_display_element fixes that. */
21833 if (pt_x == 0)
21834 get_next_display_element (&it);
21835 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21836 it.glyph_row = NULL;
21837 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21838 it.glyph_row = row;
21839 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21840 it, lest it will become out of sync with it's buffer
21841 position. */
21842 it.current_x = pt_x;
21844 else
21845 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21846 pixel_width = it.pixel_width;
21847 if (overshoot_expected && at_eol_p)
21848 pixel_width = 0;
21849 else if (pixel_width <= 0)
21850 pixel_width = 1;
21852 /* If there's a display string (or something similar) at point,
21853 we are actually at the glyph to the left of point, so we need
21854 to correct the X coordinate. */
21855 if (overshoot_expected)
21857 if (it.bidi_p)
21858 pt_x += pixel_width * it.bidi_it.scan_dir;
21859 else
21860 pt_x += pixel_width;
21863 /* Compute target X coordinate, either to the left or to the
21864 right of point. On TTY frames, all characters have the same
21865 pixel width of 1, so we can use that. On GUI frames we don't
21866 have an easy way of getting at the pixel width of the
21867 character to the left of point, so we use a different method
21868 of getting to that place. */
21869 if (dir > 0)
21870 target_x = pt_x + pixel_width;
21871 else
21872 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21874 /* Target X coordinate could be one line above or below the line
21875 of point, in which case we need to adjust the target X
21876 coordinate. Also, if moving to the left, we need to begin at
21877 the left edge of the point's screen line. */
21878 if (dir < 0)
21880 if (pt_x > 0)
21882 start_display (&it, w, pt);
21883 if (it.line_wrap == TRUNCATE)
21884 it.last_visible_x = INFINITY;
21885 reseat_at_previous_visible_line_start (&it);
21886 it.current_x = it.current_y = it.hpos = 0;
21887 if (pt_vpos != 0)
21888 move_it_by_lines (&it, pt_vpos);
21890 else
21892 move_it_by_lines (&it, -1);
21893 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21894 target_is_eol_p = true;
21895 /* Under word-wrap, we don't know the x coordinate of
21896 the last character displayed on the previous line,
21897 which immediately precedes the wrap point. To find
21898 out its x coordinate, we try moving to the right
21899 margin of the window, which will stop at the wrap
21900 point, and then reset target_x to point at the
21901 character that precedes the wrap point. This is not
21902 needed on GUI frames, because (see below) there we
21903 move from the left margin one grapheme cluster at a
21904 time, and stop when we hit the wrap point. */
21905 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21907 void *it_data = NULL;
21908 struct it it2;
21910 SAVE_IT (it2, it, it_data);
21911 move_it_in_display_line_to (&it, ZV, target_x,
21912 MOVE_TO_POS | MOVE_TO_X);
21913 /* If we arrived at target_x, that _is_ the last
21914 character on the previous line. */
21915 if (it.current_x != target_x)
21916 target_x = it.current_x - 1;
21917 RESTORE_IT (&it, &it2, it_data);
21921 else
21923 if (at_eol_p
21924 || (target_x >= it.last_visible_x
21925 && it.line_wrap != TRUNCATE))
21927 if (pt_x > 0)
21928 move_it_by_lines (&it, 0);
21929 move_it_by_lines (&it, 1);
21930 target_x = 0;
21934 /* Move to the target X coordinate. */
21935 /* On GUI frames, as we don't know the X coordinate of the
21936 character to the left of point, moving point to the left
21937 requires walking, one grapheme cluster at a time, until we
21938 find ourself at a place immediately to the left of the
21939 character at point. */
21940 if (FRAME_WINDOW_P (it.f) && dir < 0)
21942 struct text_pos new_pos;
21943 enum move_it_result rc = MOVE_X_REACHED;
21945 if (it.current_x == 0)
21946 get_next_display_element (&it);
21947 if (it.what == IT_COMPOSITION)
21949 new_pos.charpos = it.cmp_it.charpos;
21950 new_pos.bytepos = -1;
21952 else
21953 new_pos = it.current.pos;
21955 while (it.current_x + it.pixel_width <= target_x
21956 && (rc == MOVE_X_REACHED
21957 /* Under word-wrap, move_it_in_display_line_to
21958 stops at correct coordinates, but sometimes
21959 returns MOVE_POS_MATCH_OR_ZV. */
21960 || (it.line_wrap == WORD_WRAP
21961 && rc == MOVE_POS_MATCH_OR_ZV)))
21963 int new_x = it.current_x + it.pixel_width;
21965 /* For composed characters, we want the position of the
21966 first character in the grapheme cluster (usually, the
21967 composition's base character), whereas it.current
21968 might give us the position of the _last_ one, e.g. if
21969 the composition is rendered in reverse due to bidi
21970 reordering. */
21971 if (it.what == IT_COMPOSITION)
21973 new_pos.charpos = it.cmp_it.charpos;
21974 new_pos.bytepos = -1;
21976 else
21977 new_pos = it.current.pos;
21978 if (new_x == it.current_x)
21979 new_x++;
21980 rc = move_it_in_display_line_to (&it, ZV, new_x,
21981 MOVE_TO_POS | MOVE_TO_X);
21982 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21983 break;
21985 /* The previous position we saw in the loop is the one we
21986 want. */
21987 if (new_pos.bytepos == -1)
21988 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21989 it.current.pos = new_pos;
21991 else if (it.current_x != target_x)
21992 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21994 /* If we ended up in a display string that covers point, move to
21995 buffer position to the right in the visual order. */
21996 if (dir > 0)
21998 while (IT_CHARPOS (it) == PT)
22000 set_iterator_to_next (&it, false);
22001 if (!get_next_display_element (&it))
22002 break;
22006 /* Move point to that position. */
22007 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22010 return make_number (PT);
22012 #undef ROW_GLYPH_NEWLINE_P
22015 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22016 Sbidi_resolved_levels, 0, 1, 0,
22017 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22019 The resolved levels are produced by the Emacs bidi reordering engine
22020 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22021 read the Unicode Standard Annex 9 (UAX#9) for background information
22022 about these levels.
22024 VPOS is the zero-based number of the current window's screen line
22025 for which to produce the resolved levels. If VPOS is nil or omitted,
22026 it defaults to the screen line of point. If the window displays a
22027 header line, VPOS of zero will report on the header line, and first
22028 line of text in the window will have VPOS of 1.
22030 Value is an array of resolved levels, indexed by glyph number.
22031 Glyphs are numbered from zero starting from the beginning of the
22032 screen line, i.e. the left edge of the window for left-to-right lines
22033 and from the right edge for right-to-left lines. The resolved levels
22034 are produced only for the window's text area; text in display margins
22035 is not included.
22037 If the selected window's display is not up-to-date, or if the specified
22038 screen line does not display text, this function returns nil. It is
22039 highly recommended to bind this function to some simple key, like F8,
22040 in order to avoid these problems.
22042 This function exists mainly for testing the correctness of the
22043 Emacs UBA implementation, in particular with the test suite. */)
22044 (Lisp_Object vpos)
22046 struct window *w = XWINDOW (selected_window);
22047 struct buffer *b = XBUFFER (w->contents);
22048 int nrow;
22049 struct glyph_row *row;
22051 if (NILP (vpos))
22053 int d1, d2, d3, d4, d5;
22055 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22057 else
22059 CHECK_NUMBER_COERCE_MARKER (vpos);
22060 nrow = XINT (vpos);
22063 /* We require up-to-date glyph matrix for this window. */
22064 if (w->window_end_valid
22065 && !windows_or_buffers_changed
22066 && b
22067 && !b->clip_changed
22068 && !b->prevent_redisplay_optimizations_p
22069 && !window_outdated (w)
22070 && nrow >= 0
22071 && nrow < w->current_matrix->nrows
22072 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22073 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22075 struct glyph *g, *e, *g1;
22076 int nglyphs, i;
22077 Lisp_Object levels;
22079 if (!row->reversed_p) /* Left-to-right glyph row. */
22081 g = g1 = row->glyphs[TEXT_AREA];
22082 e = g + row->used[TEXT_AREA];
22084 /* Skip over glyphs at the start of the row that was
22085 generated by redisplay for its own needs. */
22086 while (g < e
22087 && NILP (g->object)
22088 && g->charpos < 0)
22089 g++;
22090 g1 = g;
22092 /* Count the "interesting" glyphs in this row. */
22093 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22094 nglyphs++;
22096 /* Create and fill the array. */
22097 levels = make_uninit_vector (nglyphs);
22098 for (i = 0; g1 < g; i++, g1++)
22099 ASET (levels, i, make_number (g1->resolved_level));
22101 else /* Right-to-left glyph row. */
22103 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22104 e = row->glyphs[TEXT_AREA] - 1;
22105 while (g > e
22106 && NILP (g->object)
22107 && g->charpos < 0)
22108 g--;
22109 g1 = g;
22110 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22111 nglyphs++;
22112 levels = make_uninit_vector (nglyphs);
22113 for (i = 0; g1 > g; i++, g1--)
22114 ASET (levels, i, make_number (g1->resolved_level));
22116 return levels;
22118 else
22119 return Qnil;
22124 /***********************************************************************
22125 Menu Bar
22126 ***********************************************************************/
22128 /* Redisplay the menu bar in the frame for window W.
22130 The menu bar of X frames that don't have X toolkit support is
22131 displayed in a special window W->frame->menu_bar_window.
22133 The menu bar of terminal frames is treated specially as far as
22134 glyph matrices are concerned. Menu bar lines are not part of
22135 windows, so the update is done directly on the frame matrix rows
22136 for the menu bar. */
22138 static void
22139 display_menu_bar (struct window *w)
22141 struct frame *f = XFRAME (WINDOW_FRAME (w));
22142 struct it it;
22143 Lisp_Object items;
22144 int i;
22146 /* Don't do all this for graphical frames. */
22147 #ifdef HAVE_NTGUI
22148 if (FRAME_W32_P (f))
22149 return;
22150 #endif
22151 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22152 if (FRAME_X_P (f))
22153 return;
22154 #endif
22156 #ifdef HAVE_NS
22157 if (FRAME_NS_P (f))
22158 return;
22159 #endif /* HAVE_NS */
22161 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22162 eassert (!FRAME_WINDOW_P (f));
22163 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22164 it.first_visible_x = 0;
22165 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22166 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22167 if (FRAME_WINDOW_P (f))
22169 /* Menu bar lines are displayed in the desired matrix of the
22170 dummy window menu_bar_window. */
22171 struct window *menu_w;
22172 menu_w = XWINDOW (f->menu_bar_window);
22173 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22174 MENU_FACE_ID);
22175 it.first_visible_x = 0;
22176 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22178 else
22179 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22181 /* This is a TTY frame, i.e. character hpos/vpos are used as
22182 pixel x/y. */
22183 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22184 MENU_FACE_ID);
22185 it.first_visible_x = 0;
22186 it.last_visible_x = FRAME_COLS (f);
22189 /* FIXME: This should be controlled by a user option. See the
22190 comments in redisplay_tool_bar and display_mode_line about
22191 this. */
22192 it.paragraph_embedding = L2R;
22194 /* Clear all rows of the menu bar. */
22195 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22197 struct glyph_row *row = it.glyph_row + i;
22198 clear_glyph_row (row);
22199 row->enabled_p = true;
22200 row->full_width_p = true;
22201 row->reversed_p = false;
22204 /* Display all items of the menu bar. */
22205 items = FRAME_MENU_BAR_ITEMS (it.f);
22206 for (i = 0; i < ASIZE (items); i += 4)
22208 Lisp_Object string;
22210 /* Stop at nil string. */
22211 string = AREF (items, i + 1);
22212 if (NILP (string))
22213 break;
22215 /* Remember where item was displayed. */
22216 ASET (items, i + 3, make_number (it.hpos));
22218 /* Display the item, pad with one space. */
22219 if (it.current_x < it.last_visible_x)
22220 display_string (NULL, string, Qnil, 0, 0, &it,
22221 SCHARS (string) + 1, 0, 0, -1);
22224 /* Fill out the line with spaces. */
22225 if (it.current_x < it.last_visible_x)
22226 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22228 /* Compute the total height of the lines. */
22229 compute_line_metrics (&it);
22232 /* Deep copy of a glyph row, including the glyphs. */
22233 static void
22234 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22236 struct glyph *pointers[1 + LAST_AREA];
22237 int to_used = to->used[TEXT_AREA];
22239 /* Save glyph pointers of TO. */
22240 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22242 /* Do a structure assignment. */
22243 *to = *from;
22245 /* Restore original glyph pointers of TO. */
22246 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22248 /* Copy the glyphs. */
22249 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22250 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22252 /* If we filled only part of the TO row, fill the rest with
22253 space_glyph (which will display as empty space). */
22254 if (to_used > from->used[TEXT_AREA])
22255 fill_up_frame_row_with_spaces (to, to_used);
22258 /* Display one menu item on a TTY, by overwriting the glyphs in the
22259 frame F's desired glyph matrix with glyphs produced from the menu
22260 item text. Called from term.c to display TTY drop-down menus one
22261 item at a time.
22263 ITEM_TEXT is the menu item text as a C string.
22265 FACE_ID is the face ID to be used for this menu item. FACE_ID
22266 could specify one of 3 faces: a face for an enabled item, a face
22267 for a disabled item, or a face for a selected item.
22269 X and Y are coordinates of the first glyph in the frame's desired
22270 matrix to be overwritten by the menu item. Since this is a TTY, Y
22271 is the zero-based number of the glyph row and X is the zero-based
22272 glyph number in the row, starting from left, where to start
22273 displaying the item.
22275 SUBMENU means this menu item drops down a submenu, which
22276 should be indicated by displaying a proper visual cue after the
22277 item text. */
22279 void
22280 display_tty_menu_item (const char *item_text, int width, int face_id,
22281 int x, int y, bool submenu)
22283 struct it it;
22284 struct frame *f = SELECTED_FRAME ();
22285 struct window *w = XWINDOW (f->selected_window);
22286 struct glyph_row *row;
22287 size_t item_len = strlen (item_text);
22289 eassert (FRAME_TERMCAP_P (f));
22291 /* Don't write beyond the matrix's last row. This can happen for
22292 TTY screens that are not high enough to show the entire menu.
22293 (This is actually a bit of defensive programming, as
22294 tty_menu_display already limits the number of menu items to one
22295 less than the number of screen lines.) */
22296 if (y >= f->desired_matrix->nrows)
22297 return;
22299 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22300 it.first_visible_x = 0;
22301 it.last_visible_x = FRAME_COLS (f) - 1;
22302 row = it.glyph_row;
22303 /* Start with the row contents from the current matrix. */
22304 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22305 bool saved_width = row->full_width_p;
22306 row->full_width_p = true;
22307 bool saved_reversed = row->reversed_p;
22308 row->reversed_p = false;
22309 row->enabled_p = true;
22311 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22312 desired face. */
22313 eassert (x < f->desired_matrix->matrix_w);
22314 it.current_x = it.hpos = x;
22315 it.current_y = it.vpos = y;
22316 int saved_used = row->used[TEXT_AREA];
22317 bool saved_truncated = row->truncated_on_right_p;
22318 row->used[TEXT_AREA] = x;
22319 it.face_id = face_id;
22320 it.line_wrap = TRUNCATE;
22322 /* FIXME: This should be controlled by a user option. See the
22323 comments in redisplay_tool_bar and display_mode_line about this.
22324 Also, if paragraph_embedding could ever be R2L, changes will be
22325 needed to avoid shifting to the right the row characters in
22326 term.c:append_glyph. */
22327 it.paragraph_embedding = L2R;
22329 /* Pad with a space on the left. */
22330 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22331 width--;
22332 /* Display the menu item, pad with spaces to WIDTH. */
22333 if (submenu)
22335 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22336 item_len, 0, FRAME_COLS (f) - 1, -1);
22337 width -= item_len;
22338 /* Indicate with " >" that there's a submenu. */
22339 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22340 FRAME_COLS (f) - 1, -1);
22342 else
22343 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22344 width, 0, FRAME_COLS (f) - 1, -1);
22346 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22347 row->truncated_on_right_p = saved_truncated;
22348 row->hash = row_hash (row);
22349 row->full_width_p = saved_width;
22350 row->reversed_p = saved_reversed;
22353 /***********************************************************************
22354 Mode Line
22355 ***********************************************************************/
22357 /* Redisplay mode lines in the window tree whose root is WINDOW.
22358 If FORCE, redisplay mode lines unconditionally.
22359 Otherwise, redisplay only mode lines that are garbaged. Value is
22360 the number of windows whose mode lines were redisplayed. */
22362 static int
22363 redisplay_mode_lines (Lisp_Object window, bool force)
22365 int nwindows = 0;
22367 while (!NILP (window))
22369 struct window *w = XWINDOW (window);
22371 if (WINDOWP (w->contents))
22372 nwindows += redisplay_mode_lines (w->contents, force);
22373 else if (force
22374 || FRAME_GARBAGED_P (XFRAME (w->frame))
22375 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22377 struct text_pos lpoint;
22378 struct buffer *old = current_buffer;
22380 /* Set the window's buffer for the mode line display. */
22381 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22382 set_buffer_internal_1 (XBUFFER (w->contents));
22384 /* Point refers normally to the selected window. For any
22385 other window, set up appropriate value. */
22386 if (!EQ (window, selected_window))
22388 struct text_pos pt;
22390 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22391 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22394 /* Display mode lines. */
22395 clear_glyph_matrix (w->desired_matrix);
22396 if (display_mode_lines (w))
22397 ++nwindows;
22399 /* Restore old settings. */
22400 set_buffer_internal_1 (old);
22401 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22404 window = w->next;
22407 return nwindows;
22411 /* Display the mode and/or header line of window W. Value is the
22412 sum number of mode lines and header lines displayed. */
22414 static int
22415 display_mode_lines (struct window *w)
22417 Lisp_Object old_selected_window = selected_window;
22418 Lisp_Object old_selected_frame = selected_frame;
22419 Lisp_Object new_frame = w->frame;
22420 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22421 int n = 0;
22423 selected_frame = new_frame;
22424 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22425 or window's point, then we'd need select_window_1 here as well. */
22426 XSETWINDOW (selected_window, w);
22427 XFRAME (new_frame)->selected_window = selected_window;
22429 /* These will be set while the mode line specs are processed. */
22430 line_number_displayed = false;
22431 w->column_number_displayed = -1;
22433 if (WINDOW_WANTS_MODELINE_P (w))
22435 struct window *sel_w = XWINDOW (old_selected_window);
22437 /* Select mode line face based on the real selected window. */
22438 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22439 BVAR (current_buffer, mode_line_format));
22440 ++n;
22443 if (WINDOW_WANTS_HEADER_LINE_P (w))
22445 display_mode_line (w, HEADER_LINE_FACE_ID,
22446 BVAR (current_buffer, header_line_format));
22447 ++n;
22450 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22451 selected_frame = old_selected_frame;
22452 selected_window = old_selected_window;
22453 if (n > 0)
22454 w->must_be_updated_p = true;
22455 return n;
22459 /* Display mode or header line of window W. FACE_ID specifies which
22460 line to display; it is either MODE_LINE_FACE_ID or
22461 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22462 display. Value is the pixel height of the mode/header line
22463 displayed. */
22465 static int
22466 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22468 struct it it;
22469 struct face *face;
22470 ptrdiff_t count = SPECPDL_INDEX ();
22472 init_iterator (&it, w, -1, -1, NULL, face_id);
22473 /* Don't extend on a previously drawn mode-line.
22474 This may happen if called from pos_visible_p. */
22475 it.glyph_row->enabled_p = false;
22476 prepare_desired_row (w, it.glyph_row, true);
22478 it.glyph_row->mode_line_p = true;
22480 /* FIXME: This should be controlled by a user option. But
22481 supporting such an option is not trivial, since the mode line is
22482 made up of many separate strings. */
22483 it.paragraph_embedding = L2R;
22485 record_unwind_protect (unwind_format_mode_line,
22486 format_mode_line_unwind_data (NULL, NULL,
22487 Qnil, false));
22489 mode_line_target = MODE_LINE_DISPLAY;
22491 /* Temporarily make frame's keyboard the current kboard so that
22492 kboard-local variables in the mode_line_format will get the right
22493 values. */
22494 push_kboard (FRAME_KBOARD (it.f));
22495 record_unwind_save_match_data ();
22496 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22497 pop_kboard ();
22499 unbind_to (count, Qnil);
22501 /* Fill up with spaces. */
22502 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22504 compute_line_metrics (&it);
22505 it.glyph_row->full_width_p = true;
22506 it.glyph_row->continued_p = false;
22507 it.glyph_row->truncated_on_left_p = false;
22508 it.glyph_row->truncated_on_right_p = false;
22510 /* Make a 3D mode-line have a shadow at its right end. */
22511 face = FACE_FROM_ID (it.f, face_id);
22512 extend_face_to_end_of_line (&it);
22513 if (face->box != FACE_NO_BOX)
22515 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22516 + it.glyph_row->used[TEXT_AREA] - 1);
22517 last->right_box_line_p = true;
22520 return it.glyph_row->height;
22523 /* Move element ELT in LIST to the front of LIST.
22524 Return the updated list. */
22526 static Lisp_Object
22527 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22529 register Lisp_Object tail, prev;
22530 register Lisp_Object tem;
22532 tail = list;
22533 prev = Qnil;
22534 while (CONSP (tail))
22536 tem = XCAR (tail);
22538 if (EQ (elt, tem))
22540 /* Splice out the link TAIL. */
22541 if (NILP (prev))
22542 list = XCDR (tail);
22543 else
22544 Fsetcdr (prev, XCDR (tail));
22546 /* Now make it the first. */
22547 Fsetcdr (tail, list);
22548 return tail;
22550 else
22551 prev = tail;
22552 tail = XCDR (tail);
22553 QUIT;
22556 /* Not found--return unchanged LIST. */
22557 return list;
22560 /* Contribute ELT to the mode line for window IT->w. How it
22561 translates into text depends on its data type.
22563 IT describes the display environment in which we display, as usual.
22565 DEPTH is the depth in recursion. It is used to prevent
22566 infinite recursion here.
22568 FIELD_WIDTH is the number of characters the display of ELT should
22569 occupy in the mode line, and PRECISION is the maximum number of
22570 characters to display from ELT's representation. See
22571 display_string for details.
22573 Returns the hpos of the end of the text generated by ELT.
22575 PROPS is a property list to add to any string we encounter.
22577 If RISKY, remove (disregard) any properties in any string
22578 we encounter, and ignore :eval and :propertize.
22580 The global variable `mode_line_target' determines whether the
22581 output is passed to `store_mode_line_noprop',
22582 `store_mode_line_string', or `display_string'. */
22584 static int
22585 display_mode_element (struct it *it, int depth, int field_width, int precision,
22586 Lisp_Object elt, Lisp_Object props, bool risky)
22588 int n = 0, field, prec;
22589 bool literal = false;
22591 tail_recurse:
22592 if (depth > 100)
22593 elt = build_string ("*too-deep*");
22595 depth++;
22597 switch (XTYPE (elt))
22599 case Lisp_String:
22601 /* A string: output it and check for %-constructs within it. */
22602 unsigned char c;
22603 ptrdiff_t offset = 0;
22605 if (SCHARS (elt) > 0
22606 && (!NILP (props) || risky))
22608 Lisp_Object oprops, aelt;
22609 oprops = Ftext_properties_at (make_number (0), elt);
22611 /* If the starting string's properties are not what
22612 we want, translate the string. Also, if the string
22613 is risky, do that anyway. */
22615 if (NILP (Fequal (props, oprops)) || risky)
22617 /* If the starting string has properties,
22618 merge the specified ones onto the existing ones. */
22619 if (! NILP (oprops) && !risky)
22621 Lisp_Object tem;
22623 oprops = Fcopy_sequence (oprops);
22624 tem = props;
22625 while (CONSP (tem))
22627 oprops = Fplist_put (oprops, XCAR (tem),
22628 XCAR (XCDR (tem)));
22629 tem = XCDR (XCDR (tem));
22631 props = oprops;
22634 aelt = Fassoc (elt, mode_line_proptrans_alist);
22635 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22637 /* AELT is what we want. Move it to the front
22638 without consing. */
22639 elt = XCAR (aelt);
22640 mode_line_proptrans_alist
22641 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22643 else
22645 Lisp_Object tem;
22647 /* If AELT has the wrong props, it is useless.
22648 so get rid of it. */
22649 if (! NILP (aelt))
22650 mode_line_proptrans_alist
22651 = Fdelq (aelt, mode_line_proptrans_alist);
22653 elt = Fcopy_sequence (elt);
22654 Fset_text_properties (make_number (0), Flength (elt),
22655 props, elt);
22656 /* Add this item to mode_line_proptrans_alist. */
22657 mode_line_proptrans_alist
22658 = Fcons (Fcons (elt, props),
22659 mode_line_proptrans_alist);
22660 /* Truncate mode_line_proptrans_alist
22661 to at most 50 elements. */
22662 tem = Fnthcdr (make_number (50),
22663 mode_line_proptrans_alist);
22664 if (! NILP (tem))
22665 XSETCDR (tem, Qnil);
22670 offset = 0;
22672 if (literal)
22674 prec = precision - n;
22675 switch (mode_line_target)
22677 case MODE_LINE_NOPROP:
22678 case MODE_LINE_TITLE:
22679 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22680 break;
22681 case MODE_LINE_STRING:
22682 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22683 break;
22684 case MODE_LINE_DISPLAY:
22685 n += display_string (NULL, elt, Qnil, 0, 0, it,
22686 0, prec, 0, STRING_MULTIBYTE (elt));
22687 break;
22690 break;
22693 /* Handle the non-literal case. */
22695 while ((precision <= 0 || n < precision)
22696 && SREF (elt, offset) != 0
22697 && (mode_line_target != MODE_LINE_DISPLAY
22698 || it->current_x < it->last_visible_x))
22700 ptrdiff_t last_offset = offset;
22702 /* Advance to end of string or next format specifier. */
22703 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22706 if (offset - 1 != last_offset)
22708 ptrdiff_t nchars, nbytes;
22710 /* Output to end of string or up to '%'. Field width
22711 is length of string. Don't output more than
22712 PRECISION allows us. */
22713 offset--;
22715 prec = c_string_width (SDATA (elt) + last_offset,
22716 offset - last_offset, precision - n,
22717 &nchars, &nbytes);
22719 switch (mode_line_target)
22721 case MODE_LINE_NOPROP:
22722 case MODE_LINE_TITLE:
22723 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22724 break;
22725 case MODE_LINE_STRING:
22727 ptrdiff_t bytepos = last_offset;
22728 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22729 ptrdiff_t endpos = (precision <= 0
22730 ? string_byte_to_char (elt, offset)
22731 : charpos + nchars);
22732 Lisp_Object mode_string
22733 = Fsubstring (elt, make_number (charpos),
22734 make_number (endpos));
22735 n += store_mode_line_string (NULL, mode_string, false,
22736 0, 0, Qnil);
22738 break;
22739 case MODE_LINE_DISPLAY:
22741 ptrdiff_t bytepos = last_offset;
22742 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22744 if (precision <= 0)
22745 nchars = string_byte_to_char (elt, offset) - charpos;
22746 n += display_string (NULL, elt, Qnil, 0, charpos,
22747 it, 0, nchars, 0,
22748 STRING_MULTIBYTE (elt));
22750 break;
22753 else /* c == '%' */
22755 ptrdiff_t percent_position = offset;
22757 /* Get the specified minimum width. Zero means
22758 don't pad. */
22759 field = 0;
22760 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22761 field = field * 10 + c - '0';
22763 /* Don't pad beyond the total padding allowed. */
22764 if (field_width - n > 0 && field > field_width - n)
22765 field = field_width - n;
22767 /* Note that either PRECISION <= 0 or N < PRECISION. */
22768 prec = precision - n;
22770 if (c == 'M')
22771 n += display_mode_element (it, depth, field, prec,
22772 Vglobal_mode_string, props,
22773 risky);
22774 else if (c != 0)
22776 bool multibyte;
22777 ptrdiff_t bytepos, charpos;
22778 const char *spec;
22779 Lisp_Object string;
22781 bytepos = percent_position;
22782 charpos = (STRING_MULTIBYTE (elt)
22783 ? string_byte_to_char (elt, bytepos)
22784 : bytepos);
22785 spec = decode_mode_spec (it->w, c, field, &string);
22786 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22788 switch (mode_line_target)
22790 case MODE_LINE_NOPROP:
22791 case MODE_LINE_TITLE:
22792 n += store_mode_line_noprop (spec, field, prec);
22793 break;
22794 case MODE_LINE_STRING:
22796 Lisp_Object tem = build_string (spec);
22797 props = Ftext_properties_at (make_number (charpos), elt);
22798 /* Should only keep face property in props */
22799 n += store_mode_line_string (NULL, tem, false,
22800 field, prec, props);
22802 break;
22803 case MODE_LINE_DISPLAY:
22805 int nglyphs_before, nwritten;
22807 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22808 nwritten = display_string (spec, string, elt,
22809 charpos, 0, it,
22810 field, prec, 0,
22811 multibyte);
22813 /* Assign to the glyphs written above the
22814 string where the `%x' came from, position
22815 of the `%'. */
22816 if (nwritten > 0)
22818 struct glyph *glyph
22819 = (it->glyph_row->glyphs[TEXT_AREA]
22820 + nglyphs_before);
22821 int i;
22823 for (i = 0; i < nwritten; ++i)
22825 glyph[i].object = elt;
22826 glyph[i].charpos = charpos;
22829 n += nwritten;
22832 break;
22835 else /* c == 0 */
22836 break;
22840 break;
22842 case Lisp_Symbol:
22843 /* A symbol: process the value of the symbol recursively
22844 as if it appeared here directly. Avoid error if symbol void.
22845 Special case: if value of symbol is a string, output the string
22846 literally. */
22848 register Lisp_Object tem;
22850 /* If the variable is not marked as risky to set
22851 then its contents are risky to use. */
22852 if (NILP (Fget (elt, Qrisky_local_variable)))
22853 risky = true;
22855 tem = Fboundp (elt);
22856 if (!NILP (tem))
22858 tem = Fsymbol_value (elt);
22859 /* If value is a string, output that string literally:
22860 don't check for % within it. */
22861 if (STRINGP (tem))
22862 literal = true;
22864 if (!EQ (tem, elt))
22866 /* Give up right away for nil or t. */
22867 elt = tem;
22868 goto tail_recurse;
22872 break;
22874 case Lisp_Cons:
22876 register Lisp_Object car, tem;
22878 /* A cons cell: five distinct cases.
22879 If first element is :eval or :propertize, do something special.
22880 If first element is a string or a cons, process all the elements
22881 and effectively concatenate them.
22882 If first element is a negative number, truncate displaying cdr to
22883 at most that many characters. If positive, pad (with spaces)
22884 to at least that many characters.
22885 If first element is a symbol, process the cadr or caddr recursively
22886 according to whether the symbol's value is non-nil or nil. */
22887 car = XCAR (elt);
22888 if (EQ (car, QCeval))
22890 /* An element of the form (:eval FORM) means evaluate FORM
22891 and use the result as mode line elements. */
22893 if (risky)
22894 break;
22896 if (CONSP (XCDR (elt)))
22898 Lisp_Object spec;
22899 spec = safe__eval (true, XCAR (XCDR (elt)));
22900 n += display_mode_element (it, depth, field_width - n,
22901 precision - n, spec, props,
22902 risky);
22905 else if (EQ (car, QCpropertize))
22907 /* An element of the form (:propertize ELT PROPS...)
22908 means display ELT but applying properties PROPS. */
22910 if (risky)
22911 break;
22913 if (CONSP (XCDR (elt)))
22914 n += display_mode_element (it, depth, field_width - n,
22915 precision - n, XCAR (XCDR (elt)),
22916 XCDR (XCDR (elt)), risky);
22918 else if (SYMBOLP (car))
22920 tem = Fboundp (car);
22921 elt = XCDR (elt);
22922 if (!CONSP (elt))
22923 goto invalid;
22924 /* elt is now the cdr, and we know it is a cons cell.
22925 Use its car if CAR has a non-nil value. */
22926 if (!NILP (tem))
22928 tem = Fsymbol_value (car);
22929 if (!NILP (tem))
22931 elt = XCAR (elt);
22932 goto tail_recurse;
22935 /* Symbol's value is nil (or symbol is unbound)
22936 Get the cddr of the original list
22937 and if possible find the caddr and use that. */
22938 elt = XCDR (elt);
22939 if (NILP (elt))
22940 break;
22941 else if (!CONSP (elt))
22942 goto invalid;
22943 elt = XCAR (elt);
22944 goto tail_recurse;
22946 else if (INTEGERP (car))
22948 register int lim = XINT (car);
22949 elt = XCDR (elt);
22950 if (lim < 0)
22952 /* Negative int means reduce maximum width. */
22953 if (precision <= 0)
22954 precision = -lim;
22955 else
22956 precision = min (precision, -lim);
22958 else if (lim > 0)
22960 /* Padding specified. Don't let it be more than
22961 current maximum. */
22962 if (precision > 0)
22963 lim = min (precision, lim);
22965 /* If that's more padding than already wanted, queue it.
22966 But don't reduce padding already specified even if
22967 that is beyond the current truncation point. */
22968 field_width = max (lim, field_width);
22970 goto tail_recurse;
22972 else if (STRINGP (car) || CONSP (car))
22974 Lisp_Object halftail = elt;
22975 int len = 0;
22977 while (CONSP (elt)
22978 && (precision <= 0 || n < precision))
22980 n += display_mode_element (it, depth,
22981 /* Do padding only after the last
22982 element in the list. */
22983 (! CONSP (XCDR (elt))
22984 ? field_width - n
22985 : 0),
22986 precision - n, XCAR (elt),
22987 props, risky);
22988 elt = XCDR (elt);
22989 len++;
22990 if ((len & 1) == 0)
22991 halftail = XCDR (halftail);
22992 /* Check for cycle. */
22993 if (EQ (halftail, elt))
22994 break;
22998 break;
23000 default:
23001 invalid:
23002 elt = build_string ("*invalid*");
23003 goto tail_recurse;
23006 /* Pad to FIELD_WIDTH. */
23007 if (field_width > 0 && n < field_width)
23009 switch (mode_line_target)
23011 case MODE_LINE_NOPROP:
23012 case MODE_LINE_TITLE:
23013 n += store_mode_line_noprop ("", field_width - n, 0);
23014 break;
23015 case MODE_LINE_STRING:
23016 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23017 Qnil);
23018 break;
23019 case MODE_LINE_DISPLAY:
23020 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23021 0, 0, 0);
23022 break;
23026 return n;
23029 /* Store a mode-line string element in mode_line_string_list.
23031 If STRING is non-null, display that C string. Otherwise, the Lisp
23032 string LISP_STRING is displayed.
23034 FIELD_WIDTH is the minimum number of output glyphs to produce.
23035 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23036 with spaces. FIELD_WIDTH <= 0 means don't pad.
23038 PRECISION is the maximum number of characters to output from
23039 STRING. PRECISION <= 0 means don't truncate the string.
23041 If COPY_STRING, make a copy of LISP_STRING before adding
23042 properties to the string.
23044 PROPS are the properties to add to the string.
23045 The mode_line_string_face face property is always added to the string.
23048 static int
23049 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23050 bool copy_string,
23051 int field_width, int precision, Lisp_Object props)
23053 ptrdiff_t len;
23054 int n = 0;
23056 if (string != NULL)
23058 len = strlen (string);
23059 if (precision > 0 && len > precision)
23060 len = precision;
23061 lisp_string = make_string (string, len);
23062 if (NILP (props))
23063 props = mode_line_string_face_prop;
23064 else if (!NILP (mode_line_string_face))
23066 Lisp_Object face = Fplist_get (props, Qface);
23067 props = Fcopy_sequence (props);
23068 if (NILP (face))
23069 face = mode_line_string_face;
23070 else
23071 face = list2 (face, mode_line_string_face);
23072 props = Fplist_put (props, Qface, face);
23074 Fadd_text_properties (make_number (0), make_number (len),
23075 props, lisp_string);
23077 else
23079 len = XFASTINT (Flength (lisp_string));
23080 if (precision > 0 && len > precision)
23082 len = precision;
23083 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23084 precision = -1;
23086 if (!NILP (mode_line_string_face))
23088 Lisp_Object face;
23089 if (NILP (props))
23090 props = Ftext_properties_at (make_number (0), lisp_string);
23091 face = Fplist_get (props, Qface);
23092 if (NILP (face))
23093 face = mode_line_string_face;
23094 else
23095 face = list2 (face, mode_line_string_face);
23096 props = list2 (Qface, face);
23097 if (copy_string)
23098 lisp_string = Fcopy_sequence (lisp_string);
23100 if (!NILP (props))
23101 Fadd_text_properties (make_number (0), make_number (len),
23102 props, lisp_string);
23105 if (len > 0)
23107 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23108 n += len;
23111 if (field_width > len)
23113 field_width -= len;
23114 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23115 if (!NILP (props))
23116 Fadd_text_properties (make_number (0), make_number (field_width),
23117 props, lisp_string);
23118 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23119 n += field_width;
23122 return n;
23126 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23127 1, 4, 0,
23128 doc: /* Format a string out of a mode line format specification.
23129 First arg FORMAT specifies the mode line format (see `mode-line-format'
23130 for details) to use.
23132 By default, the format is evaluated for the currently selected window.
23134 Optional second arg FACE specifies the face property to put on all
23135 characters for which no face is specified. The value nil means the
23136 default face. The value t means whatever face the window's mode line
23137 currently uses (either `mode-line' or `mode-line-inactive',
23138 depending on whether the window is the selected window or not).
23139 An integer value means the value string has no text
23140 properties.
23142 Optional third and fourth args WINDOW and BUFFER specify the window
23143 and buffer to use as the context for the formatting (defaults
23144 are the selected window and the WINDOW's buffer). */)
23145 (Lisp_Object format, Lisp_Object face,
23146 Lisp_Object window, Lisp_Object buffer)
23148 struct it it;
23149 int len;
23150 struct window *w;
23151 struct buffer *old_buffer = NULL;
23152 int face_id;
23153 bool no_props = INTEGERP (face);
23154 ptrdiff_t count = SPECPDL_INDEX ();
23155 Lisp_Object str;
23156 int string_start = 0;
23158 w = decode_any_window (window);
23159 XSETWINDOW (window, w);
23161 if (NILP (buffer))
23162 buffer = w->contents;
23163 CHECK_BUFFER (buffer);
23165 /* Make formatting the modeline a non-op when noninteractive, otherwise
23166 there will be problems later caused by a partially initialized frame. */
23167 if (NILP (format) || noninteractive)
23168 return empty_unibyte_string;
23170 if (no_props)
23171 face = Qnil;
23173 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23174 : EQ (face, Qt) ? (EQ (window, selected_window)
23175 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23176 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23177 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23178 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23179 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23180 : DEFAULT_FACE_ID;
23182 old_buffer = current_buffer;
23184 /* Save things including mode_line_proptrans_alist,
23185 and set that to nil so that we don't alter the outer value. */
23186 record_unwind_protect (unwind_format_mode_line,
23187 format_mode_line_unwind_data
23188 (XFRAME (WINDOW_FRAME (w)),
23189 old_buffer, selected_window, true));
23190 mode_line_proptrans_alist = Qnil;
23192 Fselect_window (window, Qt);
23193 set_buffer_internal_1 (XBUFFER (buffer));
23195 init_iterator (&it, w, -1, -1, NULL, face_id);
23197 if (no_props)
23199 mode_line_target = MODE_LINE_NOPROP;
23200 mode_line_string_face_prop = Qnil;
23201 mode_line_string_list = Qnil;
23202 string_start = MODE_LINE_NOPROP_LEN (0);
23204 else
23206 mode_line_target = MODE_LINE_STRING;
23207 mode_line_string_list = Qnil;
23208 mode_line_string_face = face;
23209 mode_line_string_face_prop
23210 = NILP (face) ? Qnil : list2 (Qface, face);
23213 push_kboard (FRAME_KBOARD (it.f));
23214 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23215 pop_kboard ();
23217 if (no_props)
23219 len = MODE_LINE_NOPROP_LEN (string_start);
23220 str = make_string (mode_line_noprop_buf + string_start, len);
23222 else
23224 mode_line_string_list = Fnreverse (mode_line_string_list);
23225 str = Fmapconcat (Qidentity, mode_line_string_list,
23226 empty_unibyte_string);
23229 unbind_to (count, Qnil);
23230 return str;
23233 /* Write a null-terminated, right justified decimal representation of
23234 the positive integer D to BUF using a minimal field width WIDTH. */
23236 static void
23237 pint2str (register char *buf, register int width, register ptrdiff_t d)
23239 register char *p = buf;
23241 if (d <= 0)
23242 *p++ = '0';
23243 else
23245 while (d > 0)
23247 *p++ = d % 10 + '0';
23248 d /= 10;
23252 for (width -= (int) (p - buf); width > 0; --width)
23253 *p++ = ' ';
23254 *p-- = '\0';
23255 while (p > buf)
23257 d = *buf;
23258 *buf++ = *p;
23259 *p-- = d;
23263 /* Write a null-terminated, right justified decimal and "human
23264 readable" representation of the nonnegative integer D to BUF using
23265 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23267 static const char power_letter[] =
23269 0, /* no letter */
23270 'k', /* kilo */
23271 'M', /* mega */
23272 'G', /* giga */
23273 'T', /* tera */
23274 'P', /* peta */
23275 'E', /* exa */
23276 'Z', /* zetta */
23277 'Y' /* yotta */
23280 static void
23281 pint2hrstr (char *buf, int width, ptrdiff_t d)
23283 /* We aim to represent the nonnegative integer D as
23284 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23285 ptrdiff_t quotient = d;
23286 int remainder = 0;
23287 /* -1 means: do not use TENTHS. */
23288 int tenths = -1;
23289 int exponent = 0;
23291 /* Length of QUOTIENT.TENTHS as a string. */
23292 int length;
23294 char * psuffix;
23295 char * p;
23297 if (quotient >= 1000)
23299 /* Scale to the appropriate EXPONENT. */
23302 remainder = quotient % 1000;
23303 quotient /= 1000;
23304 exponent++;
23306 while (quotient >= 1000);
23308 /* Round to nearest and decide whether to use TENTHS or not. */
23309 if (quotient <= 9)
23311 tenths = remainder / 100;
23312 if (remainder % 100 >= 50)
23314 if (tenths < 9)
23315 tenths++;
23316 else
23318 quotient++;
23319 if (quotient == 10)
23320 tenths = -1;
23321 else
23322 tenths = 0;
23326 else
23327 if (remainder >= 500)
23329 if (quotient < 999)
23330 quotient++;
23331 else
23333 quotient = 1;
23334 exponent++;
23335 tenths = 0;
23340 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23341 if (tenths == -1 && quotient <= 99)
23342 if (quotient <= 9)
23343 length = 1;
23344 else
23345 length = 2;
23346 else
23347 length = 3;
23348 p = psuffix = buf + max (width, length);
23350 /* Print EXPONENT. */
23351 *psuffix++ = power_letter[exponent];
23352 *psuffix = '\0';
23354 /* Print TENTHS. */
23355 if (tenths >= 0)
23357 *--p = '0' + tenths;
23358 *--p = '.';
23361 /* Print QUOTIENT. */
23364 int digit = quotient % 10;
23365 *--p = '0' + digit;
23367 while ((quotient /= 10) != 0);
23369 /* Print leading spaces. */
23370 while (buf < p)
23371 *--p = ' ';
23374 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23375 If EOL_FLAG, set also a mnemonic character for end-of-line
23376 type of CODING_SYSTEM. Return updated pointer into BUF. */
23378 static unsigned char invalid_eol_type[] = "(*invalid*)";
23380 static char *
23381 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23383 Lisp_Object val;
23384 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23385 const unsigned char *eol_str;
23386 int eol_str_len;
23387 /* The EOL conversion we are using. */
23388 Lisp_Object eoltype;
23390 val = CODING_SYSTEM_SPEC (coding_system);
23391 eoltype = Qnil;
23393 if (!VECTORP (val)) /* Not yet decided. */
23395 *buf++ = multibyte ? '-' : ' ';
23396 if (eol_flag)
23397 eoltype = eol_mnemonic_undecided;
23398 /* Don't mention EOL conversion if it isn't decided. */
23400 else
23402 Lisp_Object attrs;
23403 Lisp_Object eolvalue;
23405 attrs = AREF (val, 0);
23406 eolvalue = AREF (val, 2);
23408 *buf++ = multibyte
23409 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23410 : ' ';
23412 if (eol_flag)
23414 /* The EOL conversion that is normal on this system. */
23416 if (NILP (eolvalue)) /* Not yet decided. */
23417 eoltype = eol_mnemonic_undecided;
23418 else if (VECTORP (eolvalue)) /* Not yet decided. */
23419 eoltype = eol_mnemonic_undecided;
23420 else /* eolvalue is Qunix, Qdos, or Qmac. */
23421 eoltype = (EQ (eolvalue, Qunix)
23422 ? eol_mnemonic_unix
23423 : EQ (eolvalue, Qdos)
23424 ? eol_mnemonic_dos : eol_mnemonic_mac);
23428 if (eol_flag)
23430 /* Mention the EOL conversion if it is not the usual one. */
23431 if (STRINGP (eoltype))
23433 eol_str = SDATA (eoltype);
23434 eol_str_len = SBYTES (eoltype);
23436 else if (CHARACTERP (eoltype))
23438 int c = XFASTINT (eoltype);
23439 return buf + CHAR_STRING (c, (unsigned char *) buf);
23441 else
23443 eol_str = invalid_eol_type;
23444 eol_str_len = sizeof (invalid_eol_type) - 1;
23446 memcpy (buf, eol_str, eol_str_len);
23447 buf += eol_str_len;
23450 return buf;
23453 /* Return the approximate percentage N is of D (rounding upward), or 99,
23454 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23456 static int
23457 percent99 (ptrdiff_t n, ptrdiff_t d)
23459 int percent = (d - 1 + 100.0 * n) / d;
23460 return min (percent, 99);
23463 /* Return a string for the output of a mode line %-spec for window W,
23464 generated by character C. FIELD_WIDTH > 0 means pad the string
23465 returned with spaces to that value. Return a Lisp string in
23466 *STRING if the resulting string is taken from that Lisp string.
23468 Note we operate on the current buffer for most purposes. */
23470 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23472 static const char *
23473 decode_mode_spec (struct window *w, register int c, int field_width,
23474 Lisp_Object *string)
23476 Lisp_Object obj;
23477 struct frame *f = XFRAME (WINDOW_FRAME (w));
23478 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23479 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23480 produce strings from numerical values, so limit preposterously
23481 large values of FIELD_WIDTH to avoid overrunning the buffer's
23482 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23483 bytes plus the terminating null. */
23484 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23485 struct buffer *b = current_buffer;
23487 obj = Qnil;
23488 *string = Qnil;
23490 switch (c)
23492 case '*':
23493 if (!NILP (BVAR (b, read_only)))
23494 return "%";
23495 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23496 return "*";
23497 return "-";
23499 case '+':
23500 /* This differs from %* only for a modified read-only buffer. */
23501 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23502 return "*";
23503 if (!NILP (BVAR (b, read_only)))
23504 return "%";
23505 return "-";
23507 case '&':
23508 /* This differs from %* in ignoring read-only-ness. */
23509 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23510 return "*";
23511 return "-";
23513 case '%':
23514 return "%";
23516 case '[':
23518 int i;
23519 char *p;
23521 if (command_loop_level > 5)
23522 return "[[[... ";
23523 p = decode_mode_spec_buf;
23524 for (i = 0; i < command_loop_level; i++)
23525 *p++ = '[';
23526 *p = 0;
23527 return decode_mode_spec_buf;
23530 case ']':
23532 int i;
23533 char *p;
23535 if (command_loop_level > 5)
23536 return " ...]]]";
23537 p = decode_mode_spec_buf;
23538 for (i = 0; i < command_loop_level; i++)
23539 *p++ = ']';
23540 *p = 0;
23541 return decode_mode_spec_buf;
23544 case '-':
23546 register int i;
23548 /* Let lots_of_dashes be a string of infinite length. */
23549 if (mode_line_target == MODE_LINE_NOPROP
23550 || mode_line_target == MODE_LINE_STRING)
23551 return "--";
23552 if (field_width <= 0
23553 || field_width > sizeof (lots_of_dashes))
23555 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23556 decode_mode_spec_buf[i] = '-';
23557 decode_mode_spec_buf[i] = '\0';
23558 return decode_mode_spec_buf;
23560 else
23561 return lots_of_dashes;
23564 case 'b':
23565 obj = BVAR (b, name);
23566 break;
23568 case 'c':
23569 /* %c and %l are ignored in `frame-title-format'.
23570 (In redisplay_internal, the frame title is drawn _before_ the
23571 windows are updated, so the stuff which depends on actual
23572 window contents (such as %l) may fail to render properly, or
23573 even crash emacs.) */
23574 if (mode_line_target == MODE_LINE_TITLE)
23575 return "";
23576 else
23578 ptrdiff_t col = current_column ();
23579 w->column_number_displayed = col;
23580 pint2str (decode_mode_spec_buf, width, col);
23581 return decode_mode_spec_buf;
23584 case 'e':
23585 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23587 if (NILP (Vmemory_full))
23588 return "";
23589 else
23590 return "!MEM FULL! ";
23592 #else
23593 return "";
23594 #endif
23596 case 'F':
23597 /* %F displays the frame name. */
23598 if (!NILP (f->title))
23599 return SSDATA (f->title);
23600 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23601 return SSDATA (f->name);
23602 return "Emacs";
23604 case 'f':
23605 obj = BVAR (b, filename);
23606 break;
23608 case 'i':
23610 ptrdiff_t size = ZV - BEGV;
23611 pint2str (decode_mode_spec_buf, width, size);
23612 return decode_mode_spec_buf;
23615 case 'I':
23617 ptrdiff_t size = ZV - BEGV;
23618 pint2hrstr (decode_mode_spec_buf, width, size);
23619 return decode_mode_spec_buf;
23622 case 'l':
23624 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23625 ptrdiff_t topline, nlines, height;
23626 ptrdiff_t junk;
23628 /* %c and %l are ignored in `frame-title-format'. */
23629 if (mode_line_target == MODE_LINE_TITLE)
23630 return "";
23632 startpos = marker_position (w->start);
23633 startpos_byte = marker_byte_position (w->start);
23634 height = WINDOW_TOTAL_LINES (w);
23636 /* If we decided that this buffer isn't suitable for line numbers,
23637 don't forget that too fast. */
23638 if (w->base_line_pos == -1)
23639 goto no_value;
23641 /* If the buffer is very big, don't waste time. */
23642 if (INTEGERP (Vline_number_display_limit)
23643 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23645 w->base_line_pos = 0;
23646 w->base_line_number = 0;
23647 goto no_value;
23650 if (w->base_line_number > 0
23651 && w->base_line_pos > 0
23652 && w->base_line_pos <= startpos)
23654 line = w->base_line_number;
23655 linepos = w->base_line_pos;
23656 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23658 else
23660 line = 1;
23661 linepos = BUF_BEGV (b);
23662 linepos_byte = BUF_BEGV_BYTE (b);
23665 /* Count lines from base line to window start position. */
23666 nlines = display_count_lines (linepos_byte,
23667 startpos_byte,
23668 startpos, &junk);
23670 topline = nlines + line;
23672 /* Determine a new base line, if the old one is too close
23673 or too far away, or if we did not have one.
23674 "Too close" means it's plausible a scroll-down would
23675 go back past it. */
23676 if (startpos == BUF_BEGV (b))
23678 w->base_line_number = topline;
23679 w->base_line_pos = BUF_BEGV (b);
23681 else if (nlines < height + 25 || nlines > height * 3 + 50
23682 || linepos == BUF_BEGV (b))
23684 ptrdiff_t limit = BUF_BEGV (b);
23685 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23686 ptrdiff_t position;
23687 ptrdiff_t distance =
23688 (height * 2 + 30) * line_number_display_limit_width;
23690 if (startpos - distance > limit)
23692 limit = startpos - distance;
23693 limit_byte = CHAR_TO_BYTE (limit);
23696 nlines = display_count_lines (startpos_byte,
23697 limit_byte,
23698 - (height * 2 + 30),
23699 &position);
23700 /* If we couldn't find the lines we wanted within
23701 line_number_display_limit_width chars per line,
23702 give up on line numbers for this window. */
23703 if (position == limit_byte && limit == startpos - distance)
23705 w->base_line_pos = -1;
23706 w->base_line_number = 0;
23707 goto no_value;
23710 w->base_line_number = topline - nlines;
23711 w->base_line_pos = BYTE_TO_CHAR (position);
23714 /* Now count lines from the start pos to point. */
23715 nlines = display_count_lines (startpos_byte,
23716 PT_BYTE, PT, &junk);
23718 /* Record that we did display the line number. */
23719 line_number_displayed = true;
23721 /* Make the string to show. */
23722 pint2str (decode_mode_spec_buf, width, topline + nlines);
23723 return decode_mode_spec_buf;
23724 no_value:
23726 char *p = decode_mode_spec_buf;
23727 int pad = width - 2;
23728 while (pad-- > 0)
23729 *p++ = ' ';
23730 *p++ = '?';
23731 *p++ = '?';
23732 *p = '\0';
23733 return decode_mode_spec_buf;
23736 break;
23738 case 'm':
23739 obj = BVAR (b, mode_name);
23740 break;
23742 case 'n':
23743 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23744 return " Narrow";
23745 break;
23747 case 'p':
23749 ptrdiff_t pos = marker_position (w->start);
23750 ptrdiff_t begv = BUF_BEGV (b);
23751 ptrdiff_t zv = BUF_ZV (b);
23753 if (w->window_end_pos <= BUF_Z (b) - zv)
23754 return pos <= begv ? "All" : "Bottom";
23755 else if (pos <= begv)
23756 return "Top";
23757 else
23759 sprintf (decode_mode_spec_buf, "%2d%%",
23760 percent99 (pos - begv, zv - begv));
23761 return decode_mode_spec_buf;
23765 /* Display percentage of size above the bottom of the screen. */
23766 case 'P':
23768 ptrdiff_t toppos = marker_position (w->start);
23769 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23770 ptrdiff_t begv = BUF_BEGV (b);
23771 ptrdiff_t zv = BUF_ZV (b);
23773 if (zv <= botpos)
23774 return toppos <= begv ? "All" : "Bottom";
23775 else
23777 sprintf (decode_mode_spec_buf,
23778 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23779 percent99 (botpos - begv, zv - begv));
23780 return decode_mode_spec_buf;
23784 case 's':
23785 /* status of process */
23786 obj = Fget_buffer_process (Fcurrent_buffer ());
23787 if (NILP (obj))
23788 return "no process";
23789 #ifndef MSDOS
23790 obj = Fsymbol_name (Fprocess_status (obj));
23791 #endif
23792 break;
23794 case '@':
23796 ptrdiff_t count = inhibit_garbage_collection ();
23797 Lisp_Object curdir = BVAR (current_buffer, directory);
23798 Lisp_Object val = Qnil;
23800 if (STRINGP (curdir))
23801 val = call1 (intern ("file-remote-p"), curdir);
23803 unbind_to (count, Qnil);
23805 if (NILP (val))
23806 return "-";
23807 else
23808 return "@";
23811 case 'z':
23812 /* coding-system (not including end-of-line format) */
23813 case 'Z':
23814 /* coding-system (including end-of-line type) */
23816 bool eol_flag = (c == 'Z');
23817 char *p = decode_mode_spec_buf;
23819 if (! FRAME_WINDOW_P (f))
23821 /* No need to mention EOL here--the terminal never needs
23822 to do EOL conversion. */
23823 p = decode_mode_spec_coding (CODING_ID_NAME
23824 (FRAME_KEYBOARD_CODING (f)->id),
23825 p, false);
23826 p = decode_mode_spec_coding (CODING_ID_NAME
23827 (FRAME_TERMINAL_CODING (f)->id),
23828 p, false);
23830 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23831 p, eol_flag);
23833 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23834 #ifdef subprocesses
23835 obj = Fget_buffer_process (Fcurrent_buffer ());
23836 if (PROCESSP (obj))
23838 p = decode_mode_spec_coding
23839 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23840 p = decode_mode_spec_coding
23841 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23843 #endif /* subprocesses */
23844 #endif /* false */
23845 *p = 0;
23846 return decode_mode_spec_buf;
23850 if (STRINGP (obj))
23852 *string = obj;
23853 return SSDATA (obj);
23855 else
23856 return "";
23860 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23861 means count lines back from START_BYTE. But don't go beyond
23862 LIMIT_BYTE. Return the number of lines thus found (always
23863 nonnegative).
23865 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23866 either the position COUNT lines after/before START_BYTE, if we
23867 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23868 COUNT lines. */
23870 static ptrdiff_t
23871 display_count_lines (ptrdiff_t start_byte,
23872 ptrdiff_t limit_byte, ptrdiff_t count,
23873 ptrdiff_t *byte_pos_ptr)
23875 register unsigned char *cursor;
23876 unsigned char *base;
23878 register ptrdiff_t ceiling;
23879 register unsigned char *ceiling_addr;
23880 ptrdiff_t orig_count = count;
23882 /* If we are not in selective display mode,
23883 check only for newlines. */
23884 bool selective_display
23885 = (!NILP (BVAR (current_buffer, selective_display))
23886 && !INTEGERP (BVAR (current_buffer, selective_display)));
23888 if (count > 0)
23890 while (start_byte < limit_byte)
23892 ceiling = BUFFER_CEILING_OF (start_byte);
23893 ceiling = min (limit_byte - 1, ceiling);
23894 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23895 base = (cursor = BYTE_POS_ADDR (start_byte));
23899 if (selective_display)
23901 while (*cursor != '\n' && *cursor != 015
23902 && ++cursor != ceiling_addr)
23903 continue;
23904 if (cursor == ceiling_addr)
23905 break;
23907 else
23909 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23910 if (! cursor)
23911 break;
23914 cursor++;
23916 if (--count == 0)
23918 start_byte += cursor - base;
23919 *byte_pos_ptr = start_byte;
23920 return orig_count;
23923 while (cursor < ceiling_addr);
23925 start_byte += ceiling_addr - base;
23928 else
23930 while (start_byte > limit_byte)
23932 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23933 ceiling = max (limit_byte, ceiling);
23934 ceiling_addr = BYTE_POS_ADDR (ceiling);
23935 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23936 while (true)
23938 if (selective_display)
23940 while (--cursor >= ceiling_addr
23941 && *cursor != '\n' && *cursor != 015)
23942 continue;
23943 if (cursor < ceiling_addr)
23944 break;
23946 else
23948 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23949 if (! cursor)
23950 break;
23953 if (++count == 0)
23955 start_byte += cursor - base + 1;
23956 *byte_pos_ptr = start_byte;
23957 /* When scanning backwards, we should
23958 not count the newline posterior to which we stop. */
23959 return - orig_count - 1;
23962 start_byte += ceiling_addr - base;
23966 *byte_pos_ptr = limit_byte;
23968 if (count < 0)
23969 return - orig_count + count;
23970 return orig_count - count;
23976 /***********************************************************************
23977 Displaying strings
23978 ***********************************************************************/
23980 /* Display a NUL-terminated string, starting with index START.
23982 If STRING is non-null, display that C string. Otherwise, the Lisp
23983 string LISP_STRING is displayed. There's a case that STRING is
23984 non-null and LISP_STRING is not nil. It means STRING is a string
23985 data of LISP_STRING. In that case, we display LISP_STRING while
23986 ignoring its text properties.
23988 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23989 FACE_STRING. Display STRING or LISP_STRING with the face at
23990 FACE_STRING_POS in FACE_STRING:
23992 Display the string in the environment given by IT, but use the
23993 standard display table, temporarily.
23995 FIELD_WIDTH is the minimum number of output glyphs to produce.
23996 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23997 with spaces. If STRING has more characters, more than FIELD_WIDTH
23998 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24000 PRECISION is the maximum number of characters to output from
24001 STRING. PRECISION < 0 means don't truncate the string.
24003 This is roughly equivalent to printf format specifiers:
24005 FIELD_WIDTH PRECISION PRINTF
24006 ----------------------------------------
24007 -1 -1 %s
24008 -1 10 %.10s
24009 10 -1 %10s
24010 20 10 %20.10s
24012 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24013 display them, and < 0 means obey the current buffer's value of
24014 enable_multibyte_characters.
24016 Value is the number of columns displayed. */
24018 static int
24019 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24020 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24021 int field_width, int precision, int max_x, int multibyte)
24023 int hpos_at_start = it->hpos;
24024 int saved_face_id = it->face_id;
24025 struct glyph_row *row = it->glyph_row;
24026 ptrdiff_t it_charpos;
24028 /* Initialize the iterator IT for iteration over STRING beginning
24029 with index START. */
24030 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24031 precision, field_width, multibyte);
24032 if (string && STRINGP (lisp_string))
24033 /* LISP_STRING is the one returned by decode_mode_spec. We should
24034 ignore its text properties. */
24035 it->stop_charpos = it->end_charpos;
24037 /* If displaying STRING, set up the face of the iterator from
24038 FACE_STRING, if that's given. */
24039 if (STRINGP (face_string))
24041 ptrdiff_t endptr;
24042 struct face *face;
24044 it->face_id
24045 = face_at_string_position (it->w, face_string, face_string_pos,
24046 0, &endptr, it->base_face_id, false);
24047 face = FACE_FROM_ID (it->f, it->face_id);
24048 it->face_box_p = face->box != FACE_NO_BOX;
24051 /* Set max_x to the maximum allowed X position. Don't let it go
24052 beyond the right edge of the window. */
24053 if (max_x <= 0)
24054 max_x = it->last_visible_x;
24055 else
24056 max_x = min (max_x, it->last_visible_x);
24058 /* Skip over display elements that are not visible. because IT->w is
24059 hscrolled. */
24060 if (it->current_x < it->first_visible_x)
24061 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24062 MOVE_TO_POS | MOVE_TO_X);
24064 row->ascent = it->max_ascent;
24065 row->height = it->max_ascent + it->max_descent;
24066 row->phys_ascent = it->max_phys_ascent;
24067 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24068 row->extra_line_spacing = it->max_extra_line_spacing;
24070 if (STRINGP (it->string))
24071 it_charpos = IT_STRING_CHARPOS (*it);
24072 else
24073 it_charpos = IT_CHARPOS (*it);
24075 /* This condition is for the case that we are called with current_x
24076 past last_visible_x. */
24077 while (it->current_x < max_x)
24079 int x_before, x, n_glyphs_before, i, nglyphs;
24081 /* Get the next display element. */
24082 if (!get_next_display_element (it))
24083 break;
24085 /* Produce glyphs. */
24086 x_before = it->current_x;
24087 n_glyphs_before = row->used[TEXT_AREA];
24088 PRODUCE_GLYPHS (it);
24090 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24091 i = 0;
24092 x = x_before;
24093 while (i < nglyphs)
24095 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24097 if (it->line_wrap != TRUNCATE
24098 && x + glyph->pixel_width > max_x)
24100 /* End of continued line or max_x reached. */
24101 if (CHAR_GLYPH_PADDING_P (*glyph))
24103 /* A wide character is unbreakable. */
24104 if (row->reversed_p)
24105 unproduce_glyphs (it, row->used[TEXT_AREA]
24106 - n_glyphs_before);
24107 row->used[TEXT_AREA] = n_glyphs_before;
24108 it->current_x = x_before;
24110 else
24112 if (row->reversed_p)
24113 unproduce_glyphs (it, row->used[TEXT_AREA]
24114 - (n_glyphs_before + i));
24115 row->used[TEXT_AREA] = n_glyphs_before + i;
24116 it->current_x = x;
24118 break;
24120 else if (x + glyph->pixel_width >= it->first_visible_x)
24122 /* Glyph is at least partially visible. */
24123 ++it->hpos;
24124 if (x < it->first_visible_x)
24125 row->x = x - it->first_visible_x;
24127 else
24129 /* Glyph is off the left margin of the display area.
24130 Should not happen. */
24131 emacs_abort ();
24134 row->ascent = max (row->ascent, it->max_ascent);
24135 row->height = max (row->height, it->max_ascent + it->max_descent);
24136 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24137 row->phys_height = max (row->phys_height,
24138 it->max_phys_ascent + it->max_phys_descent);
24139 row->extra_line_spacing = max (row->extra_line_spacing,
24140 it->max_extra_line_spacing);
24141 x += glyph->pixel_width;
24142 ++i;
24145 /* Stop if max_x reached. */
24146 if (i < nglyphs)
24147 break;
24149 /* Stop at line ends. */
24150 if (ITERATOR_AT_END_OF_LINE_P (it))
24152 it->continuation_lines_width = 0;
24153 break;
24156 set_iterator_to_next (it, true);
24157 if (STRINGP (it->string))
24158 it_charpos = IT_STRING_CHARPOS (*it);
24159 else
24160 it_charpos = IT_CHARPOS (*it);
24162 /* Stop if truncating at the right edge. */
24163 if (it->line_wrap == TRUNCATE
24164 && it->current_x >= it->last_visible_x)
24166 /* Add truncation mark, but don't do it if the line is
24167 truncated at a padding space. */
24168 if (it_charpos < it->string_nchars)
24170 if (!FRAME_WINDOW_P (it->f))
24172 int ii, n;
24174 if (it->current_x > it->last_visible_x)
24176 if (!row->reversed_p)
24178 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24179 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24180 break;
24182 else
24184 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24185 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24186 break;
24187 unproduce_glyphs (it, ii + 1);
24188 ii = row->used[TEXT_AREA] - (ii + 1);
24190 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24192 row->used[TEXT_AREA] = ii;
24193 produce_special_glyphs (it, IT_TRUNCATION);
24196 produce_special_glyphs (it, IT_TRUNCATION);
24198 row->truncated_on_right_p = true;
24200 break;
24204 /* Maybe insert a truncation at the left. */
24205 if (it->first_visible_x
24206 && it_charpos > 0)
24208 if (!FRAME_WINDOW_P (it->f)
24209 || (row->reversed_p
24210 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24211 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24212 insert_left_trunc_glyphs (it);
24213 row->truncated_on_left_p = true;
24216 it->face_id = saved_face_id;
24218 /* Value is number of columns displayed. */
24219 return it->hpos - hpos_at_start;
24224 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24225 appears as an element of LIST or as the car of an element of LIST.
24226 If PROPVAL is a list, compare each element against LIST in that
24227 way, and return 1/2 if any element of PROPVAL is found in LIST.
24228 Otherwise return 0. This function cannot quit.
24229 The return value is 2 if the text is invisible but with an ellipsis
24230 and 1 if it's invisible and without an ellipsis. */
24233 invisible_prop (Lisp_Object propval, Lisp_Object list)
24235 Lisp_Object tail, proptail;
24237 for (tail = list; CONSP (tail); tail = XCDR (tail))
24239 register Lisp_Object tem;
24240 tem = XCAR (tail);
24241 if (EQ (propval, tem))
24242 return 1;
24243 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24244 return NILP (XCDR (tem)) ? 1 : 2;
24247 if (CONSP (propval))
24249 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24251 Lisp_Object propelt;
24252 propelt = XCAR (proptail);
24253 for (tail = list; CONSP (tail); tail = XCDR (tail))
24255 register Lisp_Object tem;
24256 tem = XCAR (tail);
24257 if (EQ (propelt, tem))
24258 return 1;
24259 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24260 return NILP (XCDR (tem)) ? 1 : 2;
24265 return 0;
24268 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24269 doc: /* Non-nil if the property makes the text invisible.
24270 POS-OR-PROP can be a marker or number, in which case it is taken to be
24271 a position in the current buffer and the value of the `invisible' property
24272 is checked; or it can be some other value, which is then presumed to be the
24273 value of the `invisible' property of the text of interest.
24274 The non-nil value returned can be t for truly invisible text or something
24275 else if the text is replaced by an ellipsis. */)
24276 (Lisp_Object pos_or_prop)
24278 Lisp_Object prop
24279 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24280 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24281 : pos_or_prop);
24282 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24283 return (invis == 0 ? Qnil
24284 : invis == 1 ? Qt
24285 : make_number (invis));
24288 /* Calculate a width or height in pixels from a specification using
24289 the following elements:
24291 SPEC ::=
24292 NUM - a (fractional) multiple of the default font width/height
24293 (NUM) - specifies exactly NUM pixels
24294 UNIT - a fixed number of pixels, see below.
24295 ELEMENT - size of a display element in pixels, see below.
24296 (NUM . SPEC) - equals NUM * SPEC
24297 (+ SPEC SPEC ...) - add pixel values
24298 (- SPEC SPEC ...) - subtract pixel values
24299 (- SPEC) - negate pixel value
24301 NUM ::=
24302 INT or FLOAT - a number constant
24303 SYMBOL - use symbol's (buffer local) variable binding.
24305 UNIT ::=
24306 in - pixels per inch *)
24307 mm - pixels per 1/1000 meter *)
24308 cm - pixels per 1/100 meter *)
24309 width - width of current font in pixels.
24310 height - height of current font in pixels.
24312 *) using the ratio(s) defined in display-pixels-per-inch.
24314 ELEMENT ::=
24316 left-fringe - left fringe width in pixels
24317 right-fringe - right fringe width in pixels
24319 left-margin - left margin width in pixels
24320 right-margin - right margin width in pixels
24322 scroll-bar - scroll-bar area width in pixels
24324 Examples:
24326 Pixels corresponding to 5 inches:
24327 (5 . in)
24329 Total width of non-text areas on left side of window (if scroll-bar is on left):
24330 '(space :width (+ left-fringe left-margin scroll-bar))
24332 Align to first text column (in header line):
24333 '(space :align-to 0)
24335 Align to middle of text area minus half the width of variable `my-image'
24336 containing a loaded image:
24337 '(space :align-to (0.5 . (- text my-image)))
24339 Width of left margin minus width of 1 character in the default font:
24340 '(space :width (- left-margin 1))
24342 Width of left margin minus width of 2 characters in the current font:
24343 '(space :width (- left-margin (2 . width)))
24345 Center 1 character over left-margin (in header line):
24346 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24348 Different ways to express width of left fringe plus left margin minus one pixel:
24349 '(space :width (- (+ left-fringe left-margin) (1)))
24350 '(space :width (+ left-fringe left-margin (- (1))))
24351 '(space :width (+ left-fringe left-margin (-1)))
24355 static bool
24356 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24357 struct font *font, bool width_p, int *align_to)
24359 double pixels;
24361 # define OK_PIXELS(val) (*res = (val), true)
24362 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24364 if (NILP (prop))
24365 return OK_PIXELS (0);
24367 eassert (FRAME_LIVE_P (it->f));
24369 if (SYMBOLP (prop))
24371 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24373 char *unit = SSDATA (SYMBOL_NAME (prop));
24375 if (unit[0] == 'i' && unit[1] == 'n')
24376 pixels = 1.0;
24377 else if (unit[0] == 'm' && unit[1] == 'm')
24378 pixels = 25.4;
24379 else if (unit[0] == 'c' && unit[1] == 'm')
24380 pixels = 2.54;
24381 else
24382 pixels = 0;
24383 if (pixels > 0)
24385 double ppi = (width_p ? FRAME_RES_X (it->f)
24386 : FRAME_RES_Y (it->f));
24388 if (ppi > 0)
24389 return OK_PIXELS (ppi / pixels);
24390 return false;
24394 #ifdef HAVE_WINDOW_SYSTEM
24395 if (EQ (prop, Qheight))
24396 return OK_PIXELS (font
24397 ? normal_char_height (font, -1)
24398 : FRAME_LINE_HEIGHT (it->f));
24399 if (EQ (prop, Qwidth))
24400 return OK_PIXELS (font
24401 ? FONT_WIDTH (font)
24402 : FRAME_COLUMN_WIDTH (it->f));
24403 #else
24404 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24405 return OK_PIXELS (1);
24406 #endif
24408 if (EQ (prop, Qtext))
24409 return OK_PIXELS (width_p
24410 ? window_box_width (it->w, TEXT_AREA)
24411 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24413 if (align_to && *align_to < 0)
24415 *res = 0;
24416 if (EQ (prop, Qleft))
24417 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24418 if (EQ (prop, Qright))
24419 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24420 if (EQ (prop, Qcenter))
24421 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24422 + window_box_width (it->w, TEXT_AREA) / 2);
24423 if (EQ (prop, Qleft_fringe))
24424 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24425 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24426 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24427 if (EQ (prop, Qright_fringe))
24428 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24429 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24430 : window_box_right_offset (it->w, TEXT_AREA));
24431 if (EQ (prop, Qleft_margin))
24432 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24433 if (EQ (prop, Qright_margin))
24434 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24435 if (EQ (prop, Qscroll_bar))
24436 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24438 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24439 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24440 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24441 : 0)));
24443 else
24445 if (EQ (prop, Qleft_fringe))
24446 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24447 if (EQ (prop, Qright_fringe))
24448 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24449 if (EQ (prop, Qleft_margin))
24450 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24451 if (EQ (prop, Qright_margin))
24452 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24453 if (EQ (prop, Qscroll_bar))
24454 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24457 prop = buffer_local_value (prop, it->w->contents);
24458 if (EQ (prop, Qunbound))
24459 prop = Qnil;
24462 if (NUMBERP (prop))
24464 int base_unit = (width_p
24465 ? FRAME_COLUMN_WIDTH (it->f)
24466 : FRAME_LINE_HEIGHT (it->f));
24467 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24470 if (CONSP (prop))
24472 Lisp_Object car = XCAR (prop);
24473 Lisp_Object cdr = XCDR (prop);
24475 if (SYMBOLP (car))
24477 #ifdef HAVE_WINDOW_SYSTEM
24478 if (FRAME_WINDOW_P (it->f)
24479 && valid_image_p (prop))
24481 ptrdiff_t id = lookup_image (it->f, prop);
24482 struct image *img = IMAGE_FROM_ID (it->f, id);
24484 return OK_PIXELS (width_p ? img->width : img->height);
24486 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24488 // TODO: Don't return dummy size.
24489 return OK_PIXELS (100);
24491 #endif
24492 if (EQ (car, Qplus) || EQ (car, Qminus))
24494 bool first = true;
24495 double px;
24497 pixels = 0;
24498 while (CONSP (cdr))
24500 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24501 font, width_p, align_to))
24502 return false;
24503 if (first)
24504 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24505 else
24506 pixels += px;
24507 cdr = XCDR (cdr);
24509 if (EQ (car, Qminus))
24510 pixels = -pixels;
24511 return OK_PIXELS (pixels);
24514 car = buffer_local_value (car, it->w->contents);
24515 if (EQ (car, Qunbound))
24516 car = Qnil;
24519 if (NUMBERP (car))
24521 double fact;
24522 pixels = XFLOATINT (car);
24523 if (NILP (cdr))
24524 return OK_PIXELS (pixels);
24525 if (calc_pixel_width_or_height (&fact, it, cdr,
24526 font, width_p, align_to))
24527 return OK_PIXELS (pixels * fact);
24528 return false;
24531 return false;
24534 return false;
24537 void
24538 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24540 #ifdef HAVE_WINDOW_SYSTEM
24541 normal_char_ascent_descent (font, -1, ascent, descent);
24542 #else
24543 *ascent = 1;
24544 *descent = 0;
24545 #endif
24549 /***********************************************************************
24550 Glyph Display
24551 ***********************************************************************/
24553 #ifdef HAVE_WINDOW_SYSTEM
24555 #ifdef GLYPH_DEBUG
24557 void
24558 dump_glyph_string (struct glyph_string *s)
24560 fprintf (stderr, "glyph string\n");
24561 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24562 s->x, s->y, s->width, s->height);
24563 fprintf (stderr, " ybase = %d\n", s->ybase);
24564 fprintf (stderr, " hl = %d\n", s->hl);
24565 fprintf (stderr, " left overhang = %d, right = %d\n",
24566 s->left_overhang, s->right_overhang);
24567 fprintf (stderr, " nchars = %d\n", s->nchars);
24568 fprintf (stderr, " extends to end of line = %d\n",
24569 s->extends_to_end_of_line_p);
24570 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24571 fprintf (stderr, " bg width = %d\n", s->background_width);
24574 #endif /* GLYPH_DEBUG */
24576 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24577 of XChar2b structures for S; it can't be allocated in
24578 init_glyph_string because it must be allocated via `alloca'. W
24579 is the window on which S is drawn. ROW and AREA are the glyph row
24580 and area within the row from which S is constructed. START is the
24581 index of the first glyph structure covered by S. HL is a
24582 face-override for drawing S. */
24584 #ifdef HAVE_NTGUI
24585 #define OPTIONAL_HDC(hdc) HDC hdc,
24586 #define DECLARE_HDC(hdc) HDC hdc;
24587 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24588 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24589 #endif
24591 #ifndef OPTIONAL_HDC
24592 #define OPTIONAL_HDC(hdc)
24593 #define DECLARE_HDC(hdc)
24594 #define ALLOCATE_HDC(hdc, f)
24595 #define RELEASE_HDC(hdc, f)
24596 #endif
24598 static void
24599 init_glyph_string (struct glyph_string *s,
24600 OPTIONAL_HDC (hdc)
24601 XChar2b *char2b, struct window *w, struct glyph_row *row,
24602 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24604 memset (s, 0, sizeof *s);
24605 s->w = w;
24606 s->f = XFRAME (w->frame);
24607 #ifdef HAVE_NTGUI
24608 s->hdc = hdc;
24609 #endif
24610 s->display = FRAME_X_DISPLAY (s->f);
24611 s->window = FRAME_X_WINDOW (s->f);
24612 s->char2b = char2b;
24613 s->hl = hl;
24614 s->row = row;
24615 s->area = area;
24616 s->first_glyph = row->glyphs[area] + start;
24617 s->height = row->height;
24618 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24619 s->ybase = s->y + row->ascent;
24623 /* Append the list of glyph strings with head H and tail T to the list
24624 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24626 static void
24627 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24628 struct glyph_string *h, struct glyph_string *t)
24630 if (h)
24632 if (*head)
24633 (*tail)->next = h;
24634 else
24635 *head = h;
24636 h->prev = *tail;
24637 *tail = t;
24642 /* Prepend the list of glyph strings with head H and tail T to the
24643 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24644 result. */
24646 static void
24647 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24648 struct glyph_string *h, struct glyph_string *t)
24650 if (h)
24652 if (*head)
24653 (*head)->prev = t;
24654 else
24655 *tail = t;
24656 t->next = *head;
24657 *head = h;
24662 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24663 Set *HEAD and *TAIL to the resulting list. */
24665 static void
24666 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24667 struct glyph_string *s)
24669 s->next = s->prev = NULL;
24670 append_glyph_string_lists (head, tail, s, s);
24674 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24675 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24676 make sure that X resources for the face returned are allocated.
24677 Value is a pointer to a realized face that is ready for display if
24678 DISPLAY_P. */
24680 static struct face *
24681 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24682 XChar2b *char2b, bool display_p)
24684 struct face *face = FACE_FROM_ID (f, face_id);
24685 unsigned code = 0;
24687 if (face->font)
24689 code = face->font->driver->encode_char (face->font, c);
24691 if (code == FONT_INVALID_CODE)
24692 code = 0;
24694 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24696 /* Make sure X resources of the face are allocated. */
24697 #ifdef HAVE_X_WINDOWS
24698 if (display_p)
24699 #endif
24701 eassert (face != NULL);
24702 prepare_face_for_display (f, face);
24705 return face;
24709 /* Get face and two-byte form of character glyph GLYPH on frame F.
24710 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24711 a pointer to a realized face that is ready for display. */
24713 static struct face *
24714 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24715 XChar2b *char2b)
24717 struct face *face;
24718 unsigned code = 0;
24720 eassert (glyph->type == CHAR_GLYPH);
24721 face = FACE_FROM_ID (f, glyph->face_id);
24723 /* Make sure X resources of the face are allocated. */
24724 prepare_face_for_display (f, face);
24726 if (face->font)
24728 if (CHAR_BYTE8_P (glyph->u.ch))
24729 code = CHAR_TO_BYTE8 (glyph->u.ch);
24730 else
24731 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24733 if (code == FONT_INVALID_CODE)
24734 code = 0;
24737 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24738 return face;
24742 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24743 Return true iff FONT has a glyph for C. */
24745 static bool
24746 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24748 unsigned code;
24750 if (CHAR_BYTE8_P (c))
24751 code = CHAR_TO_BYTE8 (c);
24752 else
24753 code = font->driver->encode_char (font, c);
24755 if (code == FONT_INVALID_CODE)
24756 return false;
24757 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24758 return true;
24762 /* Fill glyph string S with composition components specified by S->cmp.
24764 BASE_FACE is the base face of the composition.
24765 S->cmp_from is the index of the first component for S.
24767 OVERLAPS non-zero means S should draw the foreground only, and use
24768 its physical height for clipping. See also draw_glyphs.
24770 Value is the index of a component not in S. */
24772 static int
24773 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24774 int overlaps)
24776 int i;
24777 /* For all glyphs of this composition, starting at the offset
24778 S->cmp_from, until we reach the end of the definition or encounter a
24779 glyph that requires the different face, add it to S. */
24780 struct face *face;
24782 eassert (s);
24784 s->for_overlaps = overlaps;
24785 s->face = NULL;
24786 s->font = NULL;
24787 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24789 int c = COMPOSITION_GLYPH (s->cmp, i);
24791 /* TAB in a composition means display glyphs with padding space
24792 on the left or right. */
24793 if (c != '\t')
24795 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24796 -1, Qnil);
24798 face = get_char_face_and_encoding (s->f, c, face_id,
24799 s->char2b + i, true);
24800 if (face)
24802 if (! s->face)
24804 s->face = face;
24805 s->font = s->face->font;
24807 else if (s->face != face)
24808 break;
24811 ++s->nchars;
24813 s->cmp_to = i;
24815 if (s->face == NULL)
24817 s->face = base_face->ascii_face;
24818 s->font = s->face->font;
24821 /* All glyph strings for the same composition has the same width,
24822 i.e. the width set for the first component of the composition. */
24823 s->width = s->first_glyph->pixel_width;
24825 /* If the specified font could not be loaded, use the frame's
24826 default font, but record the fact that we couldn't load it in
24827 the glyph string so that we can draw rectangles for the
24828 characters of the glyph string. */
24829 if (s->font == NULL)
24831 s->font_not_found_p = true;
24832 s->font = FRAME_FONT (s->f);
24835 /* Adjust base line for subscript/superscript text. */
24836 s->ybase += s->first_glyph->voffset;
24838 return s->cmp_to;
24841 static int
24842 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24843 int start, int end, int overlaps)
24845 struct glyph *glyph, *last;
24846 Lisp_Object lgstring;
24847 int i;
24849 s->for_overlaps = overlaps;
24850 glyph = s->row->glyphs[s->area] + start;
24851 last = s->row->glyphs[s->area] + end;
24852 s->cmp_id = glyph->u.cmp.id;
24853 s->cmp_from = glyph->slice.cmp.from;
24854 s->cmp_to = glyph->slice.cmp.to + 1;
24855 s->face = FACE_FROM_ID (s->f, face_id);
24856 lgstring = composition_gstring_from_id (s->cmp_id);
24857 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24858 glyph++;
24859 while (glyph < last
24860 && glyph->u.cmp.automatic
24861 && glyph->u.cmp.id == s->cmp_id
24862 && s->cmp_to == glyph->slice.cmp.from)
24863 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24865 for (i = s->cmp_from; i < s->cmp_to; i++)
24867 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24868 unsigned code = LGLYPH_CODE (lglyph);
24870 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24872 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24873 return glyph - s->row->glyphs[s->area];
24877 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24878 See the comment of fill_glyph_string for arguments.
24879 Value is the index of the first glyph not in S. */
24882 static int
24883 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24884 int start, int end, int overlaps)
24886 struct glyph *glyph, *last;
24887 int voffset;
24889 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24890 s->for_overlaps = overlaps;
24891 glyph = s->row->glyphs[s->area] + start;
24892 last = s->row->glyphs[s->area] + end;
24893 voffset = glyph->voffset;
24894 s->face = FACE_FROM_ID (s->f, face_id);
24895 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24896 s->nchars = 1;
24897 s->width = glyph->pixel_width;
24898 glyph++;
24899 while (glyph < last
24900 && glyph->type == GLYPHLESS_GLYPH
24901 && glyph->voffset == voffset
24902 && glyph->face_id == face_id)
24904 s->nchars++;
24905 s->width += glyph->pixel_width;
24906 glyph++;
24908 s->ybase += voffset;
24909 return glyph - s->row->glyphs[s->area];
24913 /* Fill glyph string S from a sequence of character glyphs.
24915 FACE_ID is the face id of the string. START is the index of the
24916 first glyph to consider, END is the index of the last + 1.
24917 OVERLAPS non-zero means S should draw the foreground only, and use
24918 its physical height for clipping. See also draw_glyphs.
24920 Value is the index of the first glyph not in S. */
24922 static int
24923 fill_glyph_string (struct glyph_string *s, int face_id,
24924 int start, int end, int overlaps)
24926 struct glyph *glyph, *last;
24927 int voffset;
24928 bool glyph_not_available_p;
24930 eassert (s->f == XFRAME (s->w->frame));
24931 eassert (s->nchars == 0);
24932 eassert (start >= 0 && end > start);
24934 s->for_overlaps = overlaps;
24935 glyph = s->row->glyphs[s->area] + start;
24936 last = s->row->glyphs[s->area] + end;
24937 voffset = glyph->voffset;
24938 s->padding_p = glyph->padding_p;
24939 glyph_not_available_p = glyph->glyph_not_available_p;
24941 while (glyph < last
24942 && glyph->type == CHAR_GLYPH
24943 && glyph->voffset == voffset
24944 /* Same face id implies same font, nowadays. */
24945 && glyph->face_id == face_id
24946 && glyph->glyph_not_available_p == glyph_not_available_p)
24948 s->face = get_glyph_face_and_encoding (s->f, glyph,
24949 s->char2b + s->nchars);
24950 ++s->nchars;
24951 eassert (s->nchars <= end - start);
24952 s->width += glyph->pixel_width;
24953 if (glyph++->padding_p != s->padding_p)
24954 break;
24957 s->font = s->face->font;
24959 /* If the specified font could not be loaded, use the frame's font,
24960 but record the fact that we couldn't load it in
24961 S->font_not_found_p so that we can draw rectangles for the
24962 characters of the glyph string. */
24963 if (s->font == NULL || glyph_not_available_p)
24965 s->font_not_found_p = true;
24966 s->font = FRAME_FONT (s->f);
24969 /* Adjust base line for subscript/superscript text. */
24970 s->ybase += voffset;
24972 eassert (s->face && s->face->gc);
24973 return glyph - s->row->glyphs[s->area];
24977 /* Fill glyph string S from image glyph S->first_glyph. */
24979 static void
24980 fill_image_glyph_string (struct glyph_string *s)
24982 eassert (s->first_glyph->type == IMAGE_GLYPH);
24983 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24984 eassert (s->img);
24985 s->slice = s->first_glyph->slice.img;
24986 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24987 s->font = s->face->font;
24988 s->width = s->first_glyph->pixel_width;
24990 /* Adjust base line for subscript/superscript text. */
24991 s->ybase += s->first_glyph->voffset;
24995 #ifdef HAVE_XWIDGETS
24996 static void
24997 fill_xwidget_glyph_string (struct glyph_string *s)
24999 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25000 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25001 s->font = s->face->font;
25002 s->width = s->first_glyph->pixel_width;
25003 s->ybase += s->first_glyph->voffset;
25004 s->xwidget = s->first_glyph->u.xwidget;
25006 #endif
25007 /* Fill glyph string S from a sequence of stretch glyphs.
25009 START is the index of the first glyph to consider,
25010 END is the index of the last + 1.
25012 Value is the index of the first glyph not in S. */
25014 static int
25015 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25017 struct glyph *glyph, *last;
25018 int voffset, face_id;
25020 eassert (s->first_glyph->type == STRETCH_GLYPH);
25022 glyph = s->row->glyphs[s->area] + start;
25023 last = s->row->glyphs[s->area] + end;
25024 face_id = glyph->face_id;
25025 s->face = FACE_FROM_ID (s->f, face_id);
25026 s->font = s->face->font;
25027 s->width = glyph->pixel_width;
25028 s->nchars = 1;
25029 voffset = glyph->voffset;
25031 for (++glyph;
25032 (glyph < last
25033 && glyph->type == STRETCH_GLYPH
25034 && glyph->voffset == voffset
25035 && glyph->face_id == face_id);
25036 ++glyph)
25037 s->width += glyph->pixel_width;
25039 /* Adjust base line for subscript/superscript text. */
25040 s->ybase += voffset;
25042 /* The case that face->gc == 0 is handled when drawing the glyph
25043 string by calling prepare_face_for_display. */
25044 eassert (s->face);
25045 return glyph - s->row->glyphs[s->area];
25048 static struct font_metrics *
25049 get_per_char_metric (struct font *font, XChar2b *char2b)
25051 static struct font_metrics metrics;
25052 unsigned code;
25054 if (! font)
25055 return NULL;
25056 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25057 if (code == FONT_INVALID_CODE)
25058 return NULL;
25059 font->driver->text_extents (font, &code, 1, &metrics);
25060 return &metrics;
25063 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25064 for FONT. Values are taken from font-global ones, except for fonts
25065 that claim preposterously large values, but whose glyphs actually
25066 have reasonable dimensions. C is the character to use for metrics
25067 if the font-global values are too large; if C is negative, the
25068 function selects a default character. */
25069 static void
25070 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25072 *ascent = FONT_BASE (font);
25073 *descent = FONT_DESCENT (font);
25075 if (FONT_TOO_HIGH (font))
25077 XChar2b char2b;
25079 /* Get metrics of C, defaulting to a reasonably sized ASCII
25080 character. */
25081 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25083 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25085 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25087 /* We add 1 pixel to character dimensions as heuristics
25088 that produces nicer display, e.g. when the face has
25089 the box attribute. */
25090 *ascent = pcm->ascent + 1;
25091 *descent = pcm->descent + 1;
25097 /* A subroutine that computes a reasonable "normal character height"
25098 for fonts that claim preposterously large vertical dimensions, but
25099 whose glyphs are actually reasonably sized. C is the character
25100 whose metrics to use for those fonts, or -1 for default
25101 character. */
25102 static int
25103 normal_char_height (struct font *font, int c)
25105 int ascent, descent;
25107 normal_char_ascent_descent (font, c, &ascent, &descent);
25109 return ascent + descent;
25112 /* EXPORT for RIF:
25113 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25114 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25115 assumed to be zero. */
25117 void
25118 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25120 *left = *right = 0;
25122 if (glyph->type == CHAR_GLYPH)
25124 XChar2b char2b;
25125 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25126 if (face->font)
25128 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25129 if (pcm)
25131 if (pcm->rbearing > pcm->width)
25132 *right = pcm->rbearing - pcm->width;
25133 if (pcm->lbearing < 0)
25134 *left = -pcm->lbearing;
25138 else if (glyph->type == COMPOSITE_GLYPH)
25140 if (! glyph->u.cmp.automatic)
25142 struct composition *cmp = composition_table[glyph->u.cmp.id];
25144 if (cmp->rbearing > cmp->pixel_width)
25145 *right = cmp->rbearing - cmp->pixel_width;
25146 if (cmp->lbearing < 0)
25147 *left = - cmp->lbearing;
25149 else
25151 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25152 struct font_metrics metrics;
25154 composition_gstring_width (gstring, glyph->slice.cmp.from,
25155 glyph->slice.cmp.to + 1, &metrics);
25156 if (metrics.rbearing > metrics.width)
25157 *right = metrics.rbearing - metrics.width;
25158 if (metrics.lbearing < 0)
25159 *left = - metrics.lbearing;
25165 /* Return the index of the first glyph preceding glyph string S that
25166 is overwritten by S because of S's left overhang. Value is -1
25167 if no glyphs are overwritten. */
25169 static int
25170 left_overwritten (struct glyph_string *s)
25172 int k;
25174 if (s->left_overhang)
25176 int x = 0, i;
25177 struct glyph *glyphs = s->row->glyphs[s->area];
25178 int first = s->first_glyph - glyphs;
25180 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25181 x -= glyphs[i].pixel_width;
25183 k = i + 1;
25185 else
25186 k = -1;
25188 return k;
25192 /* Return the index of the first glyph preceding glyph string S that
25193 is overwriting S because of its right overhang. Value is -1 if no
25194 glyph in front of S overwrites S. */
25196 static int
25197 left_overwriting (struct glyph_string *s)
25199 int i, k, x;
25200 struct glyph *glyphs = s->row->glyphs[s->area];
25201 int first = s->first_glyph - glyphs;
25203 k = -1;
25204 x = 0;
25205 for (i = first - 1; i >= 0; --i)
25207 int left, right;
25208 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25209 if (x + right > 0)
25210 k = i;
25211 x -= glyphs[i].pixel_width;
25214 return k;
25218 /* Return the index of the last glyph following glyph string S that is
25219 overwritten by S because of S's right overhang. Value is -1 if
25220 no such glyph is found. */
25222 static int
25223 right_overwritten (struct glyph_string *s)
25225 int k = -1;
25227 if (s->right_overhang)
25229 int x = 0, i;
25230 struct glyph *glyphs = s->row->glyphs[s->area];
25231 int first = (s->first_glyph - glyphs
25232 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25233 int end = s->row->used[s->area];
25235 for (i = first; i < end && s->right_overhang > x; ++i)
25236 x += glyphs[i].pixel_width;
25238 k = i;
25241 return k;
25245 /* Return the index of the last glyph following glyph string S that
25246 overwrites S because of its left overhang. Value is negative
25247 if no such glyph is found. */
25249 static int
25250 right_overwriting (struct glyph_string *s)
25252 int i, k, x;
25253 int end = s->row->used[s->area];
25254 struct glyph *glyphs = s->row->glyphs[s->area];
25255 int first = (s->first_glyph - glyphs
25256 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25258 k = -1;
25259 x = 0;
25260 for (i = first; i < end; ++i)
25262 int left, right;
25263 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25264 if (x - left < 0)
25265 k = i;
25266 x += glyphs[i].pixel_width;
25269 return k;
25273 /* Set background width of glyph string S. START is the index of the
25274 first glyph following S. LAST_X is the right-most x-position + 1
25275 in the drawing area. */
25277 static void
25278 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25280 /* If the face of this glyph string has to be drawn to the end of
25281 the drawing area, set S->extends_to_end_of_line_p. */
25283 if (start == s->row->used[s->area]
25284 && ((s->row->fill_line_p
25285 && (s->hl == DRAW_NORMAL_TEXT
25286 || s->hl == DRAW_IMAGE_RAISED
25287 || s->hl == DRAW_IMAGE_SUNKEN))
25288 || s->hl == DRAW_MOUSE_FACE))
25289 s->extends_to_end_of_line_p = true;
25291 /* If S extends its face to the end of the line, set its
25292 background_width to the distance to the right edge of the drawing
25293 area. */
25294 if (s->extends_to_end_of_line_p)
25295 s->background_width = last_x - s->x + 1;
25296 else
25297 s->background_width = s->width;
25301 /* Compute overhangs and x-positions for glyph string S and its
25302 predecessors, or successors. X is the starting x-position for S.
25303 BACKWARD_P means process predecessors. */
25305 static void
25306 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25308 if (backward_p)
25310 while (s)
25312 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25313 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25314 x -= s->width;
25315 s->x = x;
25316 s = s->prev;
25319 else
25321 while (s)
25323 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25324 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25325 s->x = x;
25326 x += s->width;
25327 s = s->next;
25334 /* The following macros are only called from draw_glyphs below.
25335 They reference the following parameters of that function directly:
25336 `w', `row', `area', and `overlap_p'
25337 as well as the following local variables:
25338 `s', `f', and `hdc' (in W32) */
25340 #ifdef HAVE_NTGUI
25341 /* On W32, silently add local `hdc' variable to argument list of
25342 init_glyph_string. */
25343 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25344 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25345 #else
25346 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25347 init_glyph_string (s, char2b, w, row, area, start, hl)
25348 #endif
25350 /* Add a glyph string for a stretch glyph to the list of strings
25351 between HEAD and TAIL. START is the index of the stretch glyph in
25352 row area AREA of glyph row ROW. END is the index of the last glyph
25353 in that glyph row area. X is the current output position assigned
25354 to the new glyph string constructed. HL overrides that face of the
25355 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25356 is the right-most x-position of the drawing area. */
25358 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25359 and below -- keep them on one line. */
25360 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25361 do \
25363 s = alloca (sizeof *s); \
25364 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25365 START = fill_stretch_glyph_string (s, START, END); \
25366 append_glyph_string (&HEAD, &TAIL, s); \
25367 s->x = (X); \
25369 while (false)
25372 /* Add a glyph string for an image glyph to the list of strings
25373 between HEAD and TAIL. START is the index of the image glyph in
25374 row area AREA of glyph row ROW. END is the index of the last glyph
25375 in that glyph row area. X is the current output position assigned
25376 to the new glyph string constructed. HL overrides that face of the
25377 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25378 is the right-most x-position of the drawing area. */
25380 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25381 do \
25383 s = alloca (sizeof *s); \
25384 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25385 fill_image_glyph_string (s); \
25386 append_glyph_string (&HEAD, &TAIL, s); \
25387 ++START; \
25388 s->x = (X); \
25390 while (false)
25392 #ifndef HAVE_XWIDGETS
25393 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25394 eassume (false)
25395 #else
25396 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25397 do \
25399 s = alloca (sizeof *s); \
25400 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25401 fill_xwidget_glyph_string (s); \
25402 append_glyph_string (&(HEAD), &(TAIL), s); \
25403 ++(START); \
25404 s->x = (X); \
25406 while (false)
25407 #endif
25409 /* Add a glyph string for a sequence of character glyphs to the list
25410 of strings between HEAD and TAIL. START is the index of the first
25411 glyph in row area AREA of glyph row ROW that is part of the new
25412 glyph string. END is the index of the last glyph in that glyph row
25413 area. X is the current output position assigned to the new glyph
25414 string constructed. HL overrides that face of the glyph; e.g. it
25415 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25416 right-most x-position of the drawing area. */
25418 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25419 do \
25421 int face_id; \
25422 XChar2b *char2b; \
25424 face_id = (row)->glyphs[area][START].face_id; \
25426 s = alloca (sizeof *s); \
25427 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25428 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25429 append_glyph_string (&HEAD, &TAIL, s); \
25430 s->x = (X); \
25431 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25433 while (false)
25436 /* Add a glyph string for a composite sequence to the list of strings
25437 between HEAD and TAIL. START is the index of the first glyph in
25438 row area AREA of glyph row ROW that is part of the new glyph
25439 string. END is the index of the last glyph in that glyph row area.
25440 X is the current output position assigned to the new glyph string
25441 constructed. HL overrides that face of the glyph; e.g. it is
25442 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25443 x-position of the drawing area. */
25445 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25446 do { \
25447 int face_id = (row)->glyphs[area][START].face_id; \
25448 struct face *base_face = FACE_FROM_ID (f, face_id); \
25449 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25450 struct composition *cmp = composition_table[cmp_id]; \
25451 XChar2b *char2b; \
25452 struct glyph_string *first_s = NULL; \
25453 int n; \
25455 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25457 /* Make glyph_strings for each glyph sequence that is drawable by \
25458 the same face, and append them to HEAD/TAIL. */ \
25459 for (n = 0; n < cmp->glyph_len;) \
25461 s = alloca (sizeof *s); \
25462 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25463 append_glyph_string (&(HEAD), &(TAIL), s); \
25464 s->cmp = cmp; \
25465 s->cmp_from = n; \
25466 s->x = (X); \
25467 if (n == 0) \
25468 first_s = s; \
25469 n = fill_composite_glyph_string (s, base_face, overlaps); \
25472 ++START; \
25473 s = first_s; \
25474 } while (false)
25477 /* Add a glyph string for a glyph-string sequence to the list of strings
25478 between HEAD and TAIL. */
25480 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25481 do { \
25482 int face_id; \
25483 XChar2b *char2b; \
25484 Lisp_Object gstring; \
25486 face_id = (row)->glyphs[area][START].face_id; \
25487 gstring = (composition_gstring_from_id \
25488 ((row)->glyphs[area][START].u.cmp.id)); \
25489 s = alloca (sizeof *s); \
25490 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25491 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25492 append_glyph_string (&(HEAD), &(TAIL), s); \
25493 s->x = (X); \
25494 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25495 } while (false)
25498 /* Add a glyph string for a sequence of glyphless character's glyphs
25499 to the list of strings between HEAD and TAIL. The meanings of
25500 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25502 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25503 do \
25505 int face_id; \
25507 face_id = (row)->glyphs[area][START].face_id; \
25509 s = alloca (sizeof *s); \
25510 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25511 append_glyph_string (&HEAD, &TAIL, s); \
25512 s->x = (X); \
25513 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25514 overlaps); \
25516 while (false)
25519 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25520 of AREA of glyph row ROW on window W between indices START and END.
25521 HL overrides the face for drawing glyph strings, e.g. it is
25522 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25523 x-positions of the drawing area.
25525 This is an ugly monster macro construct because we must use alloca
25526 to allocate glyph strings (because draw_glyphs can be called
25527 asynchronously). */
25529 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25530 do \
25532 HEAD = TAIL = NULL; \
25533 while (START < END) \
25535 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25536 switch (first_glyph->type) \
25538 case CHAR_GLYPH: \
25539 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25540 HL, X, LAST_X); \
25541 break; \
25543 case COMPOSITE_GLYPH: \
25544 if (first_glyph->u.cmp.automatic) \
25545 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25546 HL, X, LAST_X); \
25547 else \
25548 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25549 HL, X, LAST_X); \
25550 break; \
25552 case STRETCH_GLYPH: \
25553 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25554 HL, X, LAST_X); \
25555 break; \
25557 case IMAGE_GLYPH: \
25558 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25559 HL, X, LAST_X); \
25560 break;
25562 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25563 case XWIDGET_GLYPH: \
25564 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25565 HL, X, LAST_X); \
25566 break;
25568 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25569 case GLYPHLESS_GLYPH: \
25570 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25571 HL, X, LAST_X); \
25572 break; \
25574 default: \
25575 emacs_abort (); \
25578 if (s) \
25580 set_glyph_string_background_width (s, START, LAST_X); \
25581 (X) += s->width; \
25584 } while (false)
25587 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25588 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25589 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25590 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25593 /* Draw glyphs between START and END in AREA of ROW on window W,
25594 starting at x-position X. X is relative to AREA in W. HL is a
25595 face-override with the following meaning:
25597 DRAW_NORMAL_TEXT draw normally
25598 DRAW_CURSOR draw in cursor face
25599 DRAW_MOUSE_FACE draw in mouse face.
25600 DRAW_INVERSE_VIDEO draw in mode line face
25601 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25602 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25604 If OVERLAPS is non-zero, draw only the foreground of characters and
25605 clip to the physical height of ROW. Non-zero value also defines
25606 the overlapping part to be drawn:
25608 OVERLAPS_PRED overlap with preceding rows
25609 OVERLAPS_SUCC overlap with succeeding rows
25610 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25611 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25613 Value is the x-position reached, relative to AREA of W. */
25615 static int
25616 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25617 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25618 enum draw_glyphs_face hl, int overlaps)
25620 struct glyph_string *head, *tail;
25621 struct glyph_string *s;
25622 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25623 int i, j, x_reached, last_x, area_left = 0;
25624 struct frame *f = XFRAME (WINDOW_FRAME (w));
25625 DECLARE_HDC (hdc);
25627 ALLOCATE_HDC (hdc, f);
25629 /* Let's rather be paranoid than getting a SEGV. */
25630 end = min (end, row->used[area]);
25631 start = clip_to_bounds (0, start, end);
25633 /* Translate X to frame coordinates. Set last_x to the right
25634 end of the drawing area. */
25635 if (row->full_width_p)
25637 /* X is relative to the left edge of W, without scroll bars
25638 or fringes. */
25639 area_left = WINDOW_LEFT_EDGE_X (w);
25640 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25641 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25643 else
25645 area_left = window_box_left (w, area);
25646 last_x = area_left + window_box_width (w, area);
25648 x += area_left;
25650 /* Build a doubly-linked list of glyph_string structures between
25651 head and tail from what we have to draw. Note that the macro
25652 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25653 the reason we use a separate variable `i'. */
25654 i = start;
25655 USE_SAFE_ALLOCA;
25656 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25657 if (tail)
25658 x_reached = tail->x + tail->background_width;
25659 else
25660 x_reached = x;
25662 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25663 the row, redraw some glyphs in front or following the glyph
25664 strings built above. */
25665 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25667 struct glyph_string *h, *t;
25668 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25669 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25670 bool check_mouse_face = false;
25671 int dummy_x = 0;
25673 /* If mouse highlighting is on, we may need to draw adjacent
25674 glyphs using mouse-face highlighting. */
25675 if (area == TEXT_AREA && row->mouse_face_p
25676 && hlinfo->mouse_face_beg_row >= 0
25677 && hlinfo->mouse_face_end_row >= 0)
25679 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25681 if (row_vpos >= hlinfo->mouse_face_beg_row
25682 && row_vpos <= hlinfo->mouse_face_end_row)
25684 check_mouse_face = true;
25685 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25686 ? hlinfo->mouse_face_beg_col : 0;
25687 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25688 ? hlinfo->mouse_face_end_col
25689 : row->used[TEXT_AREA];
25693 /* Compute overhangs for all glyph strings. */
25694 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25695 for (s = head; s; s = s->next)
25696 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25698 /* Prepend glyph strings for glyphs in front of the first glyph
25699 string that are overwritten because of the first glyph
25700 string's left overhang. The background of all strings
25701 prepended must be drawn because the first glyph string
25702 draws over it. */
25703 i = left_overwritten (head);
25704 if (i >= 0)
25706 enum draw_glyphs_face overlap_hl;
25708 /* If this row contains mouse highlighting, attempt to draw
25709 the overlapped glyphs with the correct highlight. This
25710 code fails if the overlap encompasses more than one glyph
25711 and mouse-highlight spans only some of these glyphs.
25712 However, making it work perfectly involves a lot more
25713 code, and I don't know if the pathological case occurs in
25714 practice, so we'll stick to this for now. --- cyd */
25715 if (check_mouse_face
25716 && mouse_beg_col < start && mouse_end_col > i)
25717 overlap_hl = DRAW_MOUSE_FACE;
25718 else
25719 overlap_hl = DRAW_NORMAL_TEXT;
25721 if (hl != overlap_hl)
25722 clip_head = head;
25723 j = i;
25724 BUILD_GLYPH_STRINGS (j, start, h, t,
25725 overlap_hl, dummy_x, last_x);
25726 start = i;
25727 compute_overhangs_and_x (t, head->x, true);
25728 prepend_glyph_string_lists (&head, &tail, h, t);
25729 if (clip_head == NULL)
25730 clip_head = head;
25733 /* Prepend glyph strings for glyphs in front of the first glyph
25734 string that overwrite that glyph string because of their
25735 right overhang. For these strings, only the foreground must
25736 be drawn, because it draws over the glyph string at `head'.
25737 The background must not be drawn because this would overwrite
25738 right overhangs of preceding glyphs for which no glyph
25739 strings exist. */
25740 i = left_overwriting (head);
25741 if (i >= 0)
25743 enum draw_glyphs_face overlap_hl;
25745 if (check_mouse_face
25746 && mouse_beg_col < start && mouse_end_col > i)
25747 overlap_hl = DRAW_MOUSE_FACE;
25748 else
25749 overlap_hl = DRAW_NORMAL_TEXT;
25751 if (hl == overlap_hl || clip_head == NULL)
25752 clip_head = head;
25753 BUILD_GLYPH_STRINGS (i, start, h, t,
25754 overlap_hl, dummy_x, last_x);
25755 for (s = h; s; s = s->next)
25756 s->background_filled_p = true;
25757 compute_overhangs_and_x (t, head->x, true);
25758 prepend_glyph_string_lists (&head, &tail, h, t);
25761 /* Append glyphs strings for glyphs following the last glyph
25762 string tail that are overwritten by tail. The background of
25763 these strings has to be drawn because tail's foreground draws
25764 over it. */
25765 i = right_overwritten (tail);
25766 if (i >= 0)
25768 enum draw_glyphs_face overlap_hl;
25770 if (check_mouse_face
25771 && mouse_beg_col < i && mouse_end_col > end)
25772 overlap_hl = DRAW_MOUSE_FACE;
25773 else
25774 overlap_hl = DRAW_NORMAL_TEXT;
25776 if (hl != overlap_hl)
25777 clip_tail = tail;
25778 BUILD_GLYPH_STRINGS (end, i, h, t,
25779 overlap_hl, x, last_x);
25780 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25781 we don't have `end = i;' here. */
25782 compute_overhangs_and_x (h, tail->x + tail->width, false);
25783 append_glyph_string_lists (&head, &tail, h, t);
25784 if (clip_tail == NULL)
25785 clip_tail = tail;
25788 /* Append glyph strings for glyphs following the last glyph
25789 string tail that overwrite tail. The foreground of such
25790 glyphs has to be drawn because it writes into the background
25791 of tail. The background must not be drawn because it could
25792 paint over the foreground of following glyphs. */
25793 i = right_overwriting (tail);
25794 if (i >= 0)
25796 enum draw_glyphs_face overlap_hl;
25797 if (check_mouse_face
25798 && mouse_beg_col < i && mouse_end_col > end)
25799 overlap_hl = DRAW_MOUSE_FACE;
25800 else
25801 overlap_hl = DRAW_NORMAL_TEXT;
25803 if (hl == overlap_hl || clip_tail == NULL)
25804 clip_tail = tail;
25805 i++; /* We must include the Ith glyph. */
25806 BUILD_GLYPH_STRINGS (end, i, h, t,
25807 overlap_hl, x, last_x);
25808 for (s = h; s; s = s->next)
25809 s->background_filled_p = true;
25810 compute_overhangs_and_x (h, tail->x + tail->width, false);
25811 append_glyph_string_lists (&head, &tail, h, t);
25813 if (clip_head || clip_tail)
25814 for (s = head; s; s = s->next)
25816 s->clip_head = clip_head;
25817 s->clip_tail = clip_tail;
25821 /* Draw all strings. */
25822 for (s = head; s; s = s->next)
25823 FRAME_RIF (f)->draw_glyph_string (s);
25825 #ifndef HAVE_NS
25826 /* When focus a sole frame and move horizontally, this clears on_p
25827 causing a failure to erase prev cursor position. */
25828 if (area == TEXT_AREA
25829 && !row->full_width_p
25830 /* When drawing overlapping rows, only the glyph strings'
25831 foreground is drawn, which doesn't erase a cursor
25832 completely. */
25833 && !overlaps)
25835 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25836 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25837 : (tail ? tail->x + tail->background_width : x));
25838 x0 -= area_left;
25839 x1 -= area_left;
25841 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25842 row->y, MATRIX_ROW_BOTTOM_Y (row));
25844 #endif
25846 /* Value is the x-position up to which drawn, relative to AREA of W.
25847 This doesn't include parts drawn because of overhangs. */
25848 if (row->full_width_p)
25849 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25850 else
25851 x_reached -= area_left;
25853 RELEASE_HDC (hdc, f);
25855 SAFE_FREE ();
25856 return x_reached;
25859 /* Expand row matrix if too narrow. Don't expand if area
25860 is not present. */
25862 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25864 if (!it->f->fonts_changed \
25865 && (it->glyph_row->glyphs[area] \
25866 < it->glyph_row->glyphs[area + 1])) \
25868 it->w->ncols_scale_factor++; \
25869 it->f->fonts_changed = true; \
25873 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25874 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25876 static void
25877 append_glyph (struct it *it)
25879 struct glyph *glyph;
25880 enum glyph_row_area area = it->area;
25882 eassert (it->glyph_row);
25883 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25885 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25886 if (glyph < it->glyph_row->glyphs[area + 1])
25888 /* If the glyph row is reversed, we need to prepend the glyph
25889 rather than append it. */
25890 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25892 struct glyph *g;
25894 /* Make room for the additional glyph. */
25895 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25896 g[1] = *g;
25897 glyph = it->glyph_row->glyphs[area];
25899 glyph->charpos = CHARPOS (it->position);
25900 glyph->object = it->object;
25901 if (it->pixel_width > 0)
25903 eassert (it->pixel_width <= SHRT_MAX);
25904 glyph->pixel_width = it->pixel_width;
25905 glyph->padding_p = false;
25907 else
25909 /* Assure at least 1-pixel width. Otherwise, cursor can't
25910 be displayed correctly. */
25911 glyph->pixel_width = 1;
25912 glyph->padding_p = true;
25914 glyph->ascent = it->ascent;
25915 glyph->descent = it->descent;
25916 glyph->voffset = it->voffset;
25917 glyph->type = CHAR_GLYPH;
25918 glyph->avoid_cursor_p = it->avoid_cursor_p;
25919 glyph->multibyte_p = it->multibyte_p;
25920 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25922 /* In R2L rows, the left and the right box edges need to be
25923 drawn in reverse direction. */
25924 glyph->right_box_line_p = it->start_of_box_run_p;
25925 glyph->left_box_line_p = it->end_of_box_run_p;
25927 else
25929 glyph->left_box_line_p = it->start_of_box_run_p;
25930 glyph->right_box_line_p = it->end_of_box_run_p;
25932 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25933 || it->phys_descent > it->descent);
25934 glyph->glyph_not_available_p = it->glyph_not_available_p;
25935 glyph->face_id = it->face_id;
25936 glyph->u.ch = it->char_to_display;
25937 glyph->slice.img = null_glyph_slice;
25938 glyph->font_type = FONT_TYPE_UNKNOWN;
25939 if (it->bidi_p)
25941 glyph->resolved_level = it->bidi_it.resolved_level;
25942 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25943 glyph->bidi_type = it->bidi_it.type;
25945 else
25947 glyph->resolved_level = 0;
25948 glyph->bidi_type = UNKNOWN_BT;
25950 ++it->glyph_row->used[area];
25952 else
25953 IT_EXPAND_MATRIX_WIDTH (it, area);
25956 /* Store one glyph for the composition IT->cmp_it.id in
25957 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25958 non-null. */
25960 static void
25961 append_composite_glyph (struct it *it)
25963 struct glyph *glyph;
25964 enum glyph_row_area area = it->area;
25966 eassert (it->glyph_row);
25968 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25969 if (glyph < it->glyph_row->glyphs[area + 1])
25971 /* If the glyph row is reversed, we need to prepend the glyph
25972 rather than append it. */
25973 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25975 struct glyph *g;
25977 /* Make room for the new glyph. */
25978 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25979 g[1] = *g;
25980 glyph = it->glyph_row->glyphs[it->area];
25982 glyph->charpos = it->cmp_it.charpos;
25983 glyph->object = it->object;
25984 eassert (it->pixel_width <= SHRT_MAX);
25985 glyph->pixel_width = it->pixel_width;
25986 glyph->ascent = it->ascent;
25987 glyph->descent = it->descent;
25988 glyph->voffset = it->voffset;
25989 glyph->type = COMPOSITE_GLYPH;
25990 if (it->cmp_it.ch < 0)
25992 glyph->u.cmp.automatic = false;
25993 glyph->u.cmp.id = it->cmp_it.id;
25994 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25996 else
25998 glyph->u.cmp.automatic = true;
25999 glyph->u.cmp.id = it->cmp_it.id;
26000 glyph->slice.cmp.from = it->cmp_it.from;
26001 glyph->slice.cmp.to = it->cmp_it.to - 1;
26003 glyph->avoid_cursor_p = it->avoid_cursor_p;
26004 glyph->multibyte_p = it->multibyte_p;
26005 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26007 /* In R2L rows, the left and the right box edges need to be
26008 drawn in reverse direction. */
26009 glyph->right_box_line_p = it->start_of_box_run_p;
26010 glyph->left_box_line_p = it->end_of_box_run_p;
26012 else
26014 glyph->left_box_line_p = it->start_of_box_run_p;
26015 glyph->right_box_line_p = it->end_of_box_run_p;
26017 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26018 || it->phys_descent > it->descent);
26019 glyph->padding_p = false;
26020 glyph->glyph_not_available_p = false;
26021 glyph->face_id = it->face_id;
26022 glyph->font_type = FONT_TYPE_UNKNOWN;
26023 if (it->bidi_p)
26025 glyph->resolved_level = it->bidi_it.resolved_level;
26026 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26027 glyph->bidi_type = it->bidi_it.type;
26029 ++it->glyph_row->used[area];
26031 else
26032 IT_EXPAND_MATRIX_WIDTH (it, area);
26036 /* Change IT->ascent and IT->height according to the setting of
26037 IT->voffset. */
26039 static void
26040 take_vertical_position_into_account (struct it *it)
26042 if (it->voffset)
26044 if (it->voffset < 0)
26045 /* Increase the ascent so that we can display the text higher
26046 in the line. */
26047 it->ascent -= it->voffset;
26048 else
26049 /* Increase the descent so that we can display the text lower
26050 in the line. */
26051 it->descent += it->voffset;
26056 /* Produce glyphs/get display metrics for the image IT is loaded with.
26057 See the description of struct display_iterator in dispextern.h for
26058 an overview of struct display_iterator. */
26060 static void
26061 produce_image_glyph (struct it *it)
26063 struct image *img;
26064 struct face *face;
26065 int glyph_ascent, crop;
26066 struct glyph_slice slice;
26068 eassert (it->what == IT_IMAGE);
26070 face = FACE_FROM_ID (it->f, it->face_id);
26071 /* Make sure X resources of the face is loaded. */
26072 prepare_face_for_display (it->f, face);
26074 if (it->image_id < 0)
26076 /* Fringe bitmap. */
26077 it->ascent = it->phys_ascent = 0;
26078 it->descent = it->phys_descent = 0;
26079 it->pixel_width = 0;
26080 it->nglyphs = 0;
26081 return;
26084 img = IMAGE_FROM_ID (it->f, it->image_id);
26085 /* Make sure X resources of the image is loaded. */
26086 prepare_image_for_display (it->f, img);
26088 slice.x = slice.y = 0;
26089 slice.width = img->width;
26090 slice.height = img->height;
26092 if (INTEGERP (it->slice.x))
26093 slice.x = XINT (it->slice.x);
26094 else if (FLOATP (it->slice.x))
26095 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26097 if (INTEGERP (it->slice.y))
26098 slice.y = XINT (it->slice.y);
26099 else if (FLOATP (it->slice.y))
26100 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26102 if (INTEGERP (it->slice.width))
26103 slice.width = XINT (it->slice.width);
26104 else if (FLOATP (it->slice.width))
26105 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26107 if (INTEGERP (it->slice.height))
26108 slice.height = XINT (it->slice.height);
26109 else if (FLOATP (it->slice.height))
26110 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26112 if (slice.x >= img->width)
26113 slice.x = img->width;
26114 if (slice.y >= img->height)
26115 slice.y = img->height;
26116 if (slice.x + slice.width >= img->width)
26117 slice.width = img->width - slice.x;
26118 if (slice.y + slice.height > img->height)
26119 slice.height = img->height - slice.y;
26121 if (slice.width == 0 || slice.height == 0)
26122 return;
26124 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26126 it->descent = slice.height - glyph_ascent;
26127 if (slice.y == 0)
26128 it->descent += img->vmargin;
26129 if (slice.y + slice.height == img->height)
26130 it->descent += img->vmargin;
26131 it->phys_descent = it->descent;
26133 it->pixel_width = slice.width;
26134 if (slice.x == 0)
26135 it->pixel_width += img->hmargin;
26136 if (slice.x + slice.width == img->width)
26137 it->pixel_width += img->hmargin;
26139 /* It's quite possible for images to have an ascent greater than
26140 their height, so don't get confused in that case. */
26141 if (it->descent < 0)
26142 it->descent = 0;
26144 it->nglyphs = 1;
26146 if (face->box != FACE_NO_BOX)
26148 if (face->box_line_width > 0)
26150 if (slice.y == 0)
26151 it->ascent += face->box_line_width;
26152 if (slice.y + slice.height == img->height)
26153 it->descent += face->box_line_width;
26156 if (it->start_of_box_run_p && slice.x == 0)
26157 it->pixel_width += eabs (face->box_line_width);
26158 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26159 it->pixel_width += eabs (face->box_line_width);
26162 take_vertical_position_into_account (it);
26164 /* Automatically crop wide image glyphs at right edge so we can
26165 draw the cursor on same display row. */
26166 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26167 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26169 it->pixel_width -= crop;
26170 slice.width -= crop;
26173 if (it->glyph_row)
26175 struct glyph *glyph;
26176 enum glyph_row_area area = it->area;
26178 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26179 if (it->glyph_row->reversed_p)
26181 struct glyph *g;
26183 /* Make room for the new glyph. */
26184 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26185 g[1] = *g;
26186 glyph = it->glyph_row->glyphs[it->area];
26188 if (glyph < it->glyph_row->glyphs[area + 1])
26190 glyph->charpos = CHARPOS (it->position);
26191 glyph->object = it->object;
26192 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26193 glyph->ascent = glyph_ascent;
26194 glyph->descent = it->descent;
26195 glyph->voffset = it->voffset;
26196 glyph->type = IMAGE_GLYPH;
26197 glyph->avoid_cursor_p = it->avoid_cursor_p;
26198 glyph->multibyte_p = it->multibyte_p;
26199 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26201 /* In R2L rows, the left and the right box edges need to be
26202 drawn in reverse direction. */
26203 glyph->right_box_line_p = it->start_of_box_run_p;
26204 glyph->left_box_line_p = it->end_of_box_run_p;
26206 else
26208 glyph->left_box_line_p = it->start_of_box_run_p;
26209 glyph->right_box_line_p = it->end_of_box_run_p;
26211 glyph->overlaps_vertically_p = false;
26212 glyph->padding_p = false;
26213 glyph->glyph_not_available_p = false;
26214 glyph->face_id = it->face_id;
26215 glyph->u.img_id = img->id;
26216 glyph->slice.img = slice;
26217 glyph->font_type = FONT_TYPE_UNKNOWN;
26218 if (it->bidi_p)
26220 glyph->resolved_level = it->bidi_it.resolved_level;
26221 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26222 glyph->bidi_type = it->bidi_it.type;
26224 ++it->glyph_row->used[area];
26226 else
26227 IT_EXPAND_MATRIX_WIDTH (it, area);
26231 static void
26232 produce_xwidget_glyph (struct it *it)
26234 #ifdef HAVE_XWIDGETS
26235 struct xwidget *xw;
26236 int glyph_ascent, crop;
26237 eassert (it->what == IT_XWIDGET);
26239 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26240 /* Make sure X resources of the face is loaded. */
26241 prepare_face_for_display (it->f, face);
26243 xw = it->xwidget;
26244 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26245 it->descent = xw->height/2;
26246 it->phys_descent = it->descent;
26247 it->pixel_width = xw->width;
26248 /* It's quite possible for images to have an ascent greater than
26249 their height, so don't get confused in that case. */
26250 if (it->descent < 0)
26251 it->descent = 0;
26253 it->nglyphs = 1;
26255 if (face->box != FACE_NO_BOX)
26257 if (face->box_line_width > 0)
26259 it->ascent += face->box_line_width;
26260 it->descent += face->box_line_width;
26263 if (it->start_of_box_run_p)
26264 it->pixel_width += eabs (face->box_line_width);
26265 it->pixel_width += eabs (face->box_line_width);
26268 take_vertical_position_into_account (it);
26270 /* Automatically crop wide image glyphs at right edge so we can
26271 draw the cursor on same display row. */
26272 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26273 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26274 it->pixel_width -= crop;
26276 if (it->glyph_row)
26278 enum glyph_row_area area = it->area;
26279 struct glyph *glyph
26280 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26282 if (it->glyph_row->reversed_p)
26284 struct glyph *g;
26286 /* Make room for the new glyph. */
26287 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26288 g[1] = *g;
26289 glyph = it->glyph_row->glyphs[it->area];
26291 if (glyph < it->glyph_row->glyphs[area + 1])
26293 glyph->charpos = CHARPOS (it->position);
26294 glyph->object = it->object;
26295 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26296 glyph->ascent = glyph_ascent;
26297 glyph->descent = it->descent;
26298 glyph->voffset = it->voffset;
26299 glyph->type = XWIDGET_GLYPH;
26300 glyph->avoid_cursor_p = it->avoid_cursor_p;
26301 glyph->multibyte_p = it->multibyte_p;
26302 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26304 /* In R2L rows, the left and the right box edges need to be
26305 drawn in reverse direction. */
26306 glyph->right_box_line_p = it->start_of_box_run_p;
26307 glyph->left_box_line_p = it->end_of_box_run_p;
26309 else
26311 glyph->left_box_line_p = it->start_of_box_run_p;
26312 glyph->right_box_line_p = it->end_of_box_run_p;
26314 glyph->overlaps_vertically_p = 0;
26315 glyph->padding_p = 0;
26316 glyph->glyph_not_available_p = 0;
26317 glyph->face_id = it->face_id;
26318 glyph->u.xwidget = it->xwidget;
26319 glyph->font_type = FONT_TYPE_UNKNOWN;
26320 if (it->bidi_p)
26322 glyph->resolved_level = it->bidi_it.resolved_level;
26323 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26324 glyph->bidi_type = it->bidi_it.type;
26326 ++it->glyph_row->used[area];
26328 else
26329 IT_EXPAND_MATRIX_WIDTH (it, area);
26331 #endif
26334 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26335 of the glyph, WIDTH and HEIGHT are the width and height of the
26336 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26338 static void
26339 append_stretch_glyph (struct it *it, Lisp_Object object,
26340 int width, int height, int ascent)
26342 struct glyph *glyph;
26343 enum glyph_row_area area = it->area;
26345 eassert (ascent >= 0 && ascent <= height);
26347 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26348 if (glyph < it->glyph_row->glyphs[area + 1])
26350 /* If the glyph row is reversed, we need to prepend the glyph
26351 rather than append it. */
26352 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26354 struct glyph *g;
26356 /* Make room for the additional glyph. */
26357 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26358 g[1] = *g;
26359 glyph = it->glyph_row->glyphs[area];
26361 /* Decrease the width of the first glyph of the row that
26362 begins before first_visible_x (e.g., due to hscroll).
26363 This is so the overall width of the row becomes smaller
26364 by the scroll amount, and the stretch glyph appended by
26365 extend_face_to_end_of_line will be wider, to shift the
26366 row glyphs to the right. (In L2R rows, the corresponding
26367 left-shift effect is accomplished by setting row->x to a
26368 negative value, which won't work with R2L rows.)
26370 This must leave us with a positive value of WIDTH, since
26371 otherwise the call to move_it_in_display_line_to at the
26372 beginning of display_line would have got past the entire
26373 first glyph, and then it->current_x would have been
26374 greater or equal to it->first_visible_x. */
26375 if (it->current_x < it->first_visible_x)
26376 width -= it->first_visible_x - it->current_x;
26377 eassert (width > 0);
26379 glyph->charpos = CHARPOS (it->position);
26380 glyph->object = object;
26381 /* FIXME: It would be better to use TYPE_MAX here, but
26382 __typeof__ is not portable enough... */
26383 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26384 glyph->ascent = ascent;
26385 glyph->descent = height - ascent;
26386 glyph->voffset = it->voffset;
26387 glyph->type = STRETCH_GLYPH;
26388 glyph->avoid_cursor_p = it->avoid_cursor_p;
26389 glyph->multibyte_p = it->multibyte_p;
26390 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26392 /* In R2L rows, the left and the right box edges need to be
26393 drawn in reverse direction. */
26394 glyph->right_box_line_p = it->start_of_box_run_p;
26395 glyph->left_box_line_p = it->end_of_box_run_p;
26397 else
26399 glyph->left_box_line_p = it->start_of_box_run_p;
26400 glyph->right_box_line_p = it->end_of_box_run_p;
26402 glyph->overlaps_vertically_p = false;
26403 glyph->padding_p = false;
26404 glyph->glyph_not_available_p = false;
26405 glyph->face_id = it->face_id;
26406 glyph->u.stretch.ascent = ascent;
26407 glyph->u.stretch.height = height;
26408 glyph->slice.img = null_glyph_slice;
26409 glyph->font_type = FONT_TYPE_UNKNOWN;
26410 if (it->bidi_p)
26412 glyph->resolved_level = it->bidi_it.resolved_level;
26413 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26414 glyph->bidi_type = it->bidi_it.type;
26416 else
26418 glyph->resolved_level = 0;
26419 glyph->bidi_type = UNKNOWN_BT;
26421 ++it->glyph_row->used[area];
26423 else
26424 IT_EXPAND_MATRIX_WIDTH (it, area);
26427 #endif /* HAVE_WINDOW_SYSTEM */
26429 /* Produce a stretch glyph for iterator IT. IT->object is the value
26430 of the glyph property displayed. The value must be a list
26431 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26432 being recognized:
26434 1. `:width WIDTH' specifies that the space should be WIDTH *
26435 canonical char width wide. WIDTH may be an integer or floating
26436 point number.
26438 2. `:relative-width FACTOR' specifies that the width of the stretch
26439 should be computed from the width of the first character having the
26440 `glyph' property, and should be FACTOR times that width.
26442 3. `:align-to HPOS' specifies that the space should be wide enough
26443 to reach HPOS, a value in canonical character units.
26445 Exactly one of the above pairs must be present.
26447 4. `:height HEIGHT' specifies that the height of the stretch produced
26448 should be HEIGHT, measured in canonical character units.
26450 5. `:relative-height FACTOR' specifies that the height of the
26451 stretch should be FACTOR times the height of the characters having
26452 the glyph property.
26454 Either none or exactly one of 4 or 5 must be present.
26456 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26457 of the stretch should be used for the ascent of the stretch.
26458 ASCENT must be in the range 0 <= ASCENT <= 100. */
26460 void
26461 produce_stretch_glyph (struct it *it)
26463 /* (space :width WIDTH :height HEIGHT ...) */
26464 Lisp_Object prop, plist;
26465 int width = 0, height = 0, align_to = -1;
26466 bool zero_width_ok_p = false;
26467 double tem;
26468 struct font *font = NULL;
26470 #ifdef HAVE_WINDOW_SYSTEM
26471 int ascent = 0;
26472 bool zero_height_ok_p = false;
26474 if (FRAME_WINDOW_P (it->f))
26476 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26477 font = face->font ? face->font : FRAME_FONT (it->f);
26478 prepare_face_for_display (it->f, face);
26480 #endif
26482 /* List should start with `space'. */
26483 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26484 plist = XCDR (it->object);
26486 /* Compute the width of the stretch. */
26487 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26488 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26490 /* Absolute width `:width WIDTH' specified and valid. */
26491 zero_width_ok_p = true;
26492 width = (int)tem;
26494 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26496 /* Relative width `:relative-width FACTOR' specified and valid.
26497 Compute the width of the characters having the `glyph'
26498 property. */
26499 struct it it2;
26500 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26502 it2 = *it;
26503 if (it->multibyte_p)
26504 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26505 else
26507 it2.c = it2.char_to_display = *p, it2.len = 1;
26508 if (! ASCII_CHAR_P (it2.c))
26509 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26512 it2.glyph_row = NULL;
26513 it2.what = IT_CHARACTER;
26514 PRODUCE_GLYPHS (&it2);
26515 width = NUMVAL (prop) * it2.pixel_width;
26517 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26518 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26519 &align_to))
26521 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26522 align_to = (align_to < 0
26524 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26525 else if (align_to < 0)
26526 align_to = window_box_left_offset (it->w, TEXT_AREA);
26527 width = max (0, (int)tem + align_to - it->current_x);
26528 zero_width_ok_p = true;
26530 else
26531 /* Nothing specified -> width defaults to canonical char width. */
26532 width = FRAME_COLUMN_WIDTH (it->f);
26534 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26535 width = 1;
26537 #ifdef HAVE_WINDOW_SYSTEM
26538 /* Compute height. */
26539 if (FRAME_WINDOW_P (it->f))
26541 int default_height = normal_char_height (font, ' ');
26543 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26544 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26546 height = (int)tem;
26547 zero_height_ok_p = true;
26549 else if (prop = Fplist_get (plist, QCrelative_height),
26550 NUMVAL (prop) > 0)
26551 height = default_height * NUMVAL (prop);
26552 else
26553 height = default_height;
26555 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26556 height = 1;
26558 /* Compute percentage of height used for ascent. If
26559 `:ascent ASCENT' is present and valid, use that. Otherwise,
26560 derive the ascent from the font in use. */
26561 if (prop = Fplist_get (plist, QCascent),
26562 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26563 ascent = height * NUMVAL (prop) / 100.0;
26564 else if (!NILP (prop)
26565 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26566 ascent = min (max (0, (int)tem), height);
26567 else
26568 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26570 else
26571 #endif /* HAVE_WINDOW_SYSTEM */
26572 height = 1;
26574 if (width > 0 && it->line_wrap != TRUNCATE
26575 && it->current_x + width > it->last_visible_x)
26577 width = it->last_visible_x - it->current_x;
26578 #ifdef HAVE_WINDOW_SYSTEM
26579 /* Subtract one more pixel from the stretch width, but only on
26580 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26581 width -= FRAME_WINDOW_P (it->f);
26582 #endif
26585 if (width > 0 && height > 0 && it->glyph_row)
26587 Lisp_Object o_object = it->object;
26588 Lisp_Object object = it->stack[it->sp - 1].string;
26589 int n = width;
26591 if (!STRINGP (object))
26592 object = it->w->contents;
26593 #ifdef HAVE_WINDOW_SYSTEM
26594 if (FRAME_WINDOW_P (it->f))
26595 append_stretch_glyph (it, object, width, height, ascent);
26596 else
26597 #endif
26599 it->object = object;
26600 it->char_to_display = ' ';
26601 it->pixel_width = it->len = 1;
26602 while (n--)
26603 tty_append_glyph (it);
26604 it->object = o_object;
26608 it->pixel_width = width;
26609 #ifdef HAVE_WINDOW_SYSTEM
26610 if (FRAME_WINDOW_P (it->f))
26612 it->ascent = it->phys_ascent = ascent;
26613 it->descent = it->phys_descent = height - it->ascent;
26614 it->nglyphs = width > 0 && height > 0;
26615 take_vertical_position_into_account (it);
26617 else
26618 #endif
26619 it->nglyphs = width;
26622 /* Get information about special display element WHAT in an
26623 environment described by IT. WHAT is one of IT_TRUNCATION or
26624 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26625 non-null glyph_row member. This function ensures that fields like
26626 face_id, c, len of IT are left untouched. */
26628 static void
26629 produce_special_glyphs (struct it *it, enum display_element_type what)
26631 struct it temp_it;
26632 Lisp_Object gc;
26633 GLYPH glyph;
26635 temp_it = *it;
26636 temp_it.object = Qnil;
26637 memset (&temp_it.current, 0, sizeof temp_it.current);
26639 if (what == IT_CONTINUATION)
26641 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26642 if (it->bidi_it.paragraph_dir == R2L)
26643 SET_GLYPH_FROM_CHAR (glyph, '/');
26644 else
26645 SET_GLYPH_FROM_CHAR (glyph, '\\');
26646 if (it->dp
26647 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26649 /* FIXME: Should we mirror GC for R2L lines? */
26650 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26651 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26654 else if (what == IT_TRUNCATION)
26656 /* Truncation glyph. */
26657 SET_GLYPH_FROM_CHAR (glyph, '$');
26658 if (it->dp
26659 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26661 /* FIXME: Should we mirror GC for R2L lines? */
26662 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26663 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26666 else
26667 emacs_abort ();
26669 #ifdef HAVE_WINDOW_SYSTEM
26670 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26671 is turned off, we precede the truncation/continuation glyphs by a
26672 stretch glyph whose width is computed such that these special
26673 glyphs are aligned at the window margin, even when very different
26674 fonts are used in different glyph rows. */
26675 if (FRAME_WINDOW_P (temp_it.f)
26676 /* init_iterator calls this with it->glyph_row == NULL, and it
26677 wants only the pixel width of the truncation/continuation
26678 glyphs. */
26679 && temp_it.glyph_row
26680 /* insert_left_trunc_glyphs calls us at the beginning of the
26681 row, and it has its own calculation of the stretch glyph
26682 width. */
26683 && temp_it.glyph_row->used[TEXT_AREA] > 0
26684 && (temp_it.glyph_row->reversed_p
26685 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26686 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26688 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26690 if (stretch_width > 0)
26692 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26693 struct font *font =
26694 face->font ? face->font : FRAME_FONT (temp_it.f);
26695 int stretch_ascent =
26696 (((temp_it.ascent + temp_it.descent)
26697 * FONT_BASE (font)) / FONT_HEIGHT (font));
26699 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26700 temp_it.ascent + temp_it.descent,
26701 stretch_ascent);
26704 #endif
26706 temp_it.dp = NULL;
26707 temp_it.what = IT_CHARACTER;
26708 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26709 temp_it.face_id = GLYPH_FACE (glyph);
26710 temp_it.len = CHAR_BYTES (temp_it.c);
26712 PRODUCE_GLYPHS (&temp_it);
26713 it->pixel_width = temp_it.pixel_width;
26714 it->nglyphs = temp_it.nglyphs;
26717 #ifdef HAVE_WINDOW_SYSTEM
26719 /* Calculate line-height and line-spacing properties.
26720 An integer value specifies explicit pixel value.
26721 A float value specifies relative value to current face height.
26722 A cons (float . face-name) specifies relative value to
26723 height of specified face font.
26725 Returns height in pixels, or nil. */
26727 static Lisp_Object
26728 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26729 int boff, bool override)
26731 Lisp_Object face_name = Qnil;
26732 int ascent, descent, height;
26734 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26735 return val;
26737 if (CONSP (val))
26739 face_name = XCAR (val);
26740 val = XCDR (val);
26741 if (!NUMBERP (val))
26742 val = make_number (1);
26743 if (NILP (face_name))
26745 height = it->ascent + it->descent;
26746 goto scale;
26750 if (NILP (face_name))
26752 font = FRAME_FONT (it->f);
26753 boff = FRAME_BASELINE_OFFSET (it->f);
26755 else if (EQ (face_name, Qt))
26757 override = false;
26759 else
26761 int face_id;
26762 struct face *face;
26764 face_id = lookup_named_face (it->f, face_name, false);
26765 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26766 if (face == NULL || ((font = face->font) == NULL))
26767 return make_number (-1);
26768 boff = font->baseline_offset;
26769 if (font->vertical_centering)
26770 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26773 normal_char_ascent_descent (font, -1, &ascent, &descent);
26775 if (override)
26777 it->override_ascent = ascent;
26778 it->override_descent = descent;
26779 it->override_boff = boff;
26782 height = ascent + descent;
26784 scale:
26785 if (FLOATP (val))
26786 height = (int)(XFLOAT_DATA (val) * height);
26787 else if (INTEGERP (val))
26788 height *= XINT (val);
26790 return make_number (height);
26794 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26795 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26796 and only if this is for a character for which no font was found.
26798 If the display method (it->glyphless_method) is
26799 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26800 length of the acronym or the hexadecimal string, UPPER_XOFF and
26801 UPPER_YOFF are pixel offsets for the upper part of the string,
26802 LOWER_XOFF and LOWER_YOFF are for the lower part.
26804 For the other display methods, LEN through LOWER_YOFF are zero. */
26806 static void
26807 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26808 short upper_xoff, short upper_yoff,
26809 short lower_xoff, short lower_yoff)
26811 struct glyph *glyph;
26812 enum glyph_row_area area = it->area;
26814 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26815 if (glyph < it->glyph_row->glyphs[area + 1])
26817 /* If the glyph row is reversed, we need to prepend the glyph
26818 rather than append it. */
26819 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26821 struct glyph *g;
26823 /* Make room for the additional glyph. */
26824 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26825 g[1] = *g;
26826 glyph = it->glyph_row->glyphs[area];
26828 glyph->charpos = CHARPOS (it->position);
26829 glyph->object = it->object;
26830 eassert (it->pixel_width <= SHRT_MAX);
26831 glyph->pixel_width = it->pixel_width;
26832 glyph->ascent = it->ascent;
26833 glyph->descent = it->descent;
26834 glyph->voffset = it->voffset;
26835 glyph->type = GLYPHLESS_GLYPH;
26836 glyph->u.glyphless.method = it->glyphless_method;
26837 glyph->u.glyphless.for_no_font = for_no_font;
26838 glyph->u.glyphless.len = len;
26839 glyph->u.glyphless.ch = it->c;
26840 glyph->slice.glyphless.upper_xoff = upper_xoff;
26841 glyph->slice.glyphless.upper_yoff = upper_yoff;
26842 glyph->slice.glyphless.lower_xoff = lower_xoff;
26843 glyph->slice.glyphless.lower_yoff = lower_yoff;
26844 glyph->avoid_cursor_p = it->avoid_cursor_p;
26845 glyph->multibyte_p = it->multibyte_p;
26846 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26848 /* In R2L rows, the left and the right box edges need to be
26849 drawn in reverse direction. */
26850 glyph->right_box_line_p = it->start_of_box_run_p;
26851 glyph->left_box_line_p = it->end_of_box_run_p;
26853 else
26855 glyph->left_box_line_p = it->start_of_box_run_p;
26856 glyph->right_box_line_p = it->end_of_box_run_p;
26858 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26859 || it->phys_descent > it->descent);
26860 glyph->padding_p = false;
26861 glyph->glyph_not_available_p = false;
26862 glyph->face_id = face_id;
26863 glyph->font_type = FONT_TYPE_UNKNOWN;
26864 if (it->bidi_p)
26866 glyph->resolved_level = it->bidi_it.resolved_level;
26867 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26868 glyph->bidi_type = it->bidi_it.type;
26870 ++it->glyph_row->used[area];
26872 else
26873 IT_EXPAND_MATRIX_WIDTH (it, area);
26877 /* Produce a glyph for a glyphless character for iterator IT.
26878 IT->glyphless_method specifies which method to use for displaying
26879 the character. See the description of enum
26880 glyphless_display_method in dispextern.h for the detail.
26882 FOR_NO_FONT is true if and only if this is for a character for
26883 which no font was found. ACRONYM, if non-nil, is an acronym string
26884 for the character. */
26886 static void
26887 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26889 int face_id;
26890 struct face *face;
26891 struct font *font;
26892 int base_width, base_height, width, height;
26893 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26894 int len;
26896 /* Get the metrics of the base font. We always refer to the current
26897 ASCII face. */
26898 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26899 font = face->font ? face->font : FRAME_FONT (it->f);
26900 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26901 it->ascent += font->baseline_offset;
26902 it->descent -= font->baseline_offset;
26903 base_height = it->ascent + it->descent;
26904 base_width = font->average_width;
26906 face_id = merge_glyphless_glyph_face (it);
26908 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26910 it->pixel_width = THIN_SPACE_WIDTH;
26911 len = 0;
26912 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26914 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26916 width = CHARACTER_WIDTH (it->c);
26917 if (width == 0)
26918 width = 1;
26919 else if (width > 4)
26920 width = 4;
26921 it->pixel_width = base_width * width;
26922 len = 0;
26923 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26925 else
26927 char buf[7];
26928 const char *str;
26929 unsigned int code[6];
26930 int upper_len;
26931 int ascent, descent;
26932 struct font_metrics metrics_upper, metrics_lower;
26934 face = FACE_FROM_ID (it->f, face_id);
26935 font = face->font ? face->font : FRAME_FONT (it->f);
26936 prepare_face_for_display (it->f, face);
26938 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26940 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26941 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26942 if (CONSP (acronym))
26943 acronym = XCAR (acronym);
26944 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26946 else
26948 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26949 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26950 str = buf;
26952 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26953 code[len] = font->driver->encode_char (font, str[len]);
26954 upper_len = (len + 1) / 2;
26955 font->driver->text_extents (font, code, upper_len,
26956 &metrics_upper);
26957 font->driver->text_extents (font, code + upper_len, len - upper_len,
26958 &metrics_lower);
26962 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26963 width = max (metrics_upper.width, metrics_lower.width) + 4;
26964 upper_xoff = upper_yoff = 2; /* the typical case */
26965 if (base_width >= width)
26967 /* Align the upper to the left, the lower to the right. */
26968 it->pixel_width = base_width;
26969 lower_xoff = base_width - 2 - metrics_lower.width;
26971 else
26973 /* Center the shorter one. */
26974 it->pixel_width = width;
26975 if (metrics_upper.width >= metrics_lower.width)
26976 lower_xoff = (width - metrics_lower.width) / 2;
26977 else
26979 /* FIXME: This code doesn't look right. It formerly was
26980 missing the "lower_xoff = 0;", which couldn't have
26981 been right since it left lower_xoff uninitialized. */
26982 lower_xoff = 0;
26983 upper_xoff = (width - metrics_upper.width) / 2;
26987 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26988 top, bottom, and between upper and lower strings. */
26989 height = (metrics_upper.ascent + metrics_upper.descent
26990 + metrics_lower.ascent + metrics_lower.descent) + 5;
26991 /* Center vertically.
26992 H:base_height, D:base_descent
26993 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26995 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26996 descent = D - H/2 + h/2;
26997 lower_yoff = descent - 2 - ld;
26998 upper_yoff = lower_yoff - la - 1 - ud; */
26999 ascent = - (it->descent - (base_height + height + 1) / 2);
27000 descent = it->descent - (base_height - height) / 2;
27001 lower_yoff = descent - 2 - metrics_lower.descent;
27002 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27003 - metrics_upper.descent);
27004 /* Don't make the height shorter than the base height. */
27005 if (height > base_height)
27007 it->ascent = ascent;
27008 it->descent = descent;
27012 it->phys_ascent = it->ascent;
27013 it->phys_descent = it->descent;
27014 if (it->glyph_row)
27015 append_glyphless_glyph (it, face_id, for_no_font, len,
27016 upper_xoff, upper_yoff,
27017 lower_xoff, lower_yoff);
27018 it->nglyphs = 1;
27019 take_vertical_position_into_account (it);
27023 /* RIF:
27024 Produce glyphs/get display metrics for the display element IT is
27025 loaded with. See the description of struct it in dispextern.h
27026 for an overview of struct it. */
27028 void
27029 x_produce_glyphs (struct it *it)
27031 int extra_line_spacing = it->extra_line_spacing;
27033 it->glyph_not_available_p = false;
27035 if (it->what == IT_CHARACTER)
27037 XChar2b char2b;
27038 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27039 struct font *font = face->font;
27040 struct font_metrics *pcm = NULL;
27041 int boff; /* Baseline offset. */
27043 if (font == NULL)
27045 /* When no suitable font is found, display this character by
27046 the method specified in the first extra slot of
27047 Vglyphless_char_display. */
27048 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27050 eassert (it->what == IT_GLYPHLESS);
27051 produce_glyphless_glyph (it, true,
27052 STRINGP (acronym) ? acronym : Qnil);
27053 goto done;
27056 boff = font->baseline_offset;
27057 if (font->vertical_centering)
27058 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27060 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27062 it->nglyphs = 1;
27064 if (it->override_ascent >= 0)
27066 it->ascent = it->override_ascent;
27067 it->descent = it->override_descent;
27068 boff = it->override_boff;
27070 else
27072 it->ascent = FONT_BASE (font) + boff;
27073 it->descent = FONT_DESCENT (font) - boff;
27076 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27078 pcm = get_per_char_metric (font, &char2b);
27079 if (pcm->width == 0
27080 && pcm->rbearing == 0 && pcm->lbearing == 0)
27081 pcm = NULL;
27084 if (pcm)
27086 it->phys_ascent = pcm->ascent + boff;
27087 it->phys_descent = pcm->descent - boff;
27088 it->pixel_width = pcm->width;
27089 /* Don't use font-global values for ascent and descent
27090 if they result in an exceedingly large line height. */
27091 if (it->override_ascent < 0)
27093 if (FONT_TOO_HIGH (font))
27095 it->ascent = it->phys_ascent;
27096 it->descent = it->phys_descent;
27097 /* These limitations are enforced by an
27098 assertion near the end of this function. */
27099 if (it->ascent < 0)
27100 it->ascent = 0;
27101 if (it->descent < 0)
27102 it->descent = 0;
27106 else
27108 it->glyph_not_available_p = true;
27109 it->phys_ascent = it->ascent;
27110 it->phys_descent = it->descent;
27111 it->pixel_width = font->space_width;
27114 if (it->constrain_row_ascent_descent_p)
27116 if (it->descent > it->max_descent)
27118 it->ascent += it->descent - it->max_descent;
27119 it->descent = it->max_descent;
27121 if (it->ascent > it->max_ascent)
27123 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27124 it->ascent = it->max_ascent;
27126 it->phys_ascent = min (it->phys_ascent, it->ascent);
27127 it->phys_descent = min (it->phys_descent, it->descent);
27128 extra_line_spacing = 0;
27131 /* If this is a space inside a region of text with
27132 `space-width' property, change its width. */
27133 bool stretched_p
27134 = it->char_to_display == ' ' && !NILP (it->space_width);
27135 if (stretched_p)
27136 it->pixel_width *= XFLOATINT (it->space_width);
27138 /* If face has a box, add the box thickness to the character
27139 height. If character has a box line to the left and/or
27140 right, add the box line width to the character's width. */
27141 if (face->box != FACE_NO_BOX)
27143 int thick = face->box_line_width;
27145 if (thick > 0)
27147 it->ascent += thick;
27148 it->descent += thick;
27150 else
27151 thick = -thick;
27153 if (it->start_of_box_run_p)
27154 it->pixel_width += thick;
27155 if (it->end_of_box_run_p)
27156 it->pixel_width += thick;
27159 /* If face has an overline, add the height of the overline
27160 (1 pixel) and a 1 pixel margin to the character height. */
27161 if (face->overline_p)
27162 it->ascent += overline_margin;
27164 if (it->constrain_row_ascent_descent_p)
27166 if (it->ascent > it->max_ascent)
27167 it->ascent = it->max_ascent;
27168 if (it->descent > it->max_descent)
27169 it->descent = it->max_descent;
27172 take_vertical_position_into_account (it);
27174 /* If we have to actually produce glyphs, do it. */
27175 if (it->glyph_row)
27177 if (stretched_p)
27179 /* Translate a space with a `space-width' property
27180 into a stretch glyph. */
27181 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27182 / FONT_HEIGHT (font));
27183 append_stretch_glyph (it, it->object, it->pixel_width,
27184 it->ascent + it->descent, ascent);
27186 else
27187 append_glyph (it);
27189 /* If characters with lbearing or rbearing are displayed
27190 in this line, record that fact in a flag of the
27191 glyph row. This is used to optimize X output code. */
27192 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27193 it->glyph_row->contains_overlapping_glyphs_p = true;
27195 if (! stretched_p && it->pixel_width == 0)
27196 /* We assure that all visible glyphs have at least 1-pixel
27197 width. */
27198 it->pixel_width = 1;
27200 else if (it->char_to_display == '\n')
27202 /* A newline has no width, but we need the height of the
27203 line. But if previous part of the line sets a height,
27204 don't increase that height. */
27206 Lisp_Object height;
27207 Lisp_Object total_height = Qnil;
27209 it->override_ascent = -1;
27210 it->pixel_width = 0;
27211 it->nglyphs = 0;
27213 height = get_it_property (it, Qline_height);
27214 /* Split (line-height total-height) list. */
27215 if (CONSP (height)
27216 && CONSP (XCDR (height))
27217 && NILP (XCDR (XCDR (height))))
27219 total_height = XCAR (XCDR (height));
27220 height = XCAR (height);
27222 height = calc_line_height_property (it, height, font, boff, true);
27224 if (it->override_ascent >= 0)
27226 it->ascent = it->override_ascent;
27227 it->descent = it->override_descent;
27228 boff = it->override_boff;
27230 else
27232 if (FONT_TOO_HIGH (font))
27234 it->ascent = font->pixel_size + boff - 1;
27235 it->descent = -boff + 1;
27236 if (it->descent < 0)
27237 it->descent = 0;
27239 else
27241 it->ascent = FONT_BASE (font) + boff;
27242 it->descent = FONT_DESCENT (font) - boff;
27246 if (EQ (height, Qt))
27248 if (it->descent > it->max_descent)
27250 it->ascent += it->descent - it->max_descent;
27251 it->descent = it->max_descent;
27253 if (it->ascent > it->max_ascent)
27255 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27256 it->ascent = it->max_ascent;
27258 it->phys_ascent = min (it->phys_ascent, it->ascent);
27259 it->phys_descent = min (it->phys_descent, it->descent);
27260 it->constrain_row_ascent_descent_p = true;
27261 extra_line_spacing = 0;
27263 else
27265 Lisp_Object spacing;
27267 it->phys_ascent = it->ascent;
27268 it->phys_descent = it->descent;
27270 if ((it->max_ascent > 0 || it->max_descent > 0)
27271 && face->box != FACE_NO_BOX
27272 && face->box_line_width > 0)
27274 it->ascent += face->box_line_width;
27275 it->descent += face->box_line_width;
27277 if (!NILP (height)
27278 && XINT (height) > it->ascent + it->descent)
27279 it->ascent = XINT (height) - it->descent;
27281 if (!NILP (total_height))
27282 spacing = calc_line_height_property (it, total_height, font,
27283 boff, false);
27284 else
27286 spacing = get_it_property (it, Qline_spacing);
27287 spacing = calc_line_height_property (it, spacing, font,
27288 boff, false);
27290 if (INTEGERP (spacing))
27292 extra_line_spacing = XINT (spacing);
27293 if (!NILP (total_height))
27294 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27298 else /* i.e. (it->char_to_display == '\t') */
27300 if (font->space_width > 0)
27302 int tab_width = it->tab_width * font->space_width;
27303 int x = it->current_x + it->continuation_lines_width;
27304 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27306 /* If the distance from the current position to the next tab
27307 stop is less than a space character width, use the
27308 tab stop after that. */
27309 if (next_tab_x - x < font->space_width)
27310 next_tab_x += tab_width;
27312 it->pixel_width = next_tab_x - x;
27313 it->nglyphs = 1;
27314 if (FONT_TOO_HIGH (font))
27316 if (get_char_glyph_code (' ', font, &char2b))
27318 pcm = get_per_char_metric (font, &char2b);
27319 if (pcm->width == 0
27320 && pcm->rbearing == 0 && pcm->lbearing == 0)
27321 pcm = NULL;
27324 if (pcm)
27326 it->ascent = pcm->ascent + boff;
27327 it->descent = pcm->descent - boff;
27329 else
27331 it->ascent = font->pixel_size + boff - 1;
27332 it->descent = -boff + 1;
27334 if (it->ascent < 0)
27335 it->ascent = 0;
27336 if (it->descent < 0)
27337 it->descent = 0;
27339 else
27341 it->ascent = FONT_BASE (font) + boff;
27342 it->descent = FONT_DESCENT (font) - boff;
27344 it->phys_ascent = it->ascent;
27345 it->phys_descent = it->descent;
27347 if (it->glyph_row)
27349 append_stretch_glyph (it, it->object, it->pixel_width,
27350 it->ascent + it->descent, it->ascent);
27353 else
27355 it->pixel_width = 0;
27356 it->nglyphs = 1;
27360 if (FONT_TOO_HIGH (font))
27362 int font_ascent, font_descent;
27364 /* For very large fonts, where we ignore the declared font
27365 dimensions, and go by per-character metrics instead,
27366 don't let the row ascent and descent values (and the row
27367 height computed from them) be smaller than the "normal"
27368 character metrics. This avoids unpleasant effects
27369 whereby lines on display would change their height
27370 depending on which characters are shown. */
27371 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27372 it->max_ascent = max (it->max_ascent, font_ascent);
27373 it->max_descent = max (it->max_descent, font_descent);
27376 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27378 /* A static composition.
27380 Note: A composition is represented as one glyph in the
27381 glyph matrix. There are no padding glyphs.
27383 Important note: pixel_width, ascent, and descent are the
27384 values of what is drawn by draw_glyphs (i.e. the values of
27385 the overall glyphs composed). */
27386 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27387 int boff; /* baseline offset */
27388 struct composition *cmp = composition_table[it->cmp_it.id];
27389 int glyph_len = cmp->glyph_len;
27390 struct font *font = face->font;
27392 it->nglyphs = 1;
27394 /* If we have not yet calculated pixel size data of glyphs of
27395 the composition for the current face font, calculate them
27396 now. Theoretically, we have to check all fonts for the
27397 glyphs, but that requires much time and memory space. So,
27398 here we check only the font of the first glyph. This may
27399 lead to incorrect display, but it's very rare, and C-l
27400 (recenter-top-bottom) can correct the display anyway. */
27401 if (! cmp->font || cmp->font != font)
27403 /* Ascent and descent of the font of the first character
27404 of this composition (adjusted by baseline offset).
27405 Ascent and descent of overall glyphs should not be less
27406 than these, respectively. */
27407 int font_ascent, font_descent, font_height;
27408 /* Bounding box of the overall glyphs. */
27409 int leftmost, rightmost, lowest, highest;
27410 int lbearing, rbearing;
27411 int i, width, ascent, descent;
27412 int c;
27413 XChar2b char2b;
27414 struct font_metrics *pcm;
27415 ptrdiff_t pos;
27417 eassume (0 < glyph_len); /* See Bug#8512. */
27419 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27420 while (c == '\t' && 0 < --glyph_len);
27422 bool right_padded = glyph_len < cmp->glyph_len;
27423 for (i = 0; i < glyph_len; i++)
27425 c = COMPOSITION_GLYPH (cmp, i);
27426 if (c != '\t')
27427 break;
27428 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27430 bool left_padded = i > 0;
27432 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27433 : IT_CHARPOS (*it));
27434 /* If no suitable font is found, use the default font. */
27435 bool font_not_found_p = font == NULL;
27436 if (font_not_found_p)
27438 face = face->ascii_face;
27439 font = face->font;
27441 boff = font->baseline_offset;
27442 if (font->vertical_centering)
27443 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27444 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27445 font_ascent += boff;
27446 font_descent -= boff;
27447 font_height = font_ascent + font_descent;
27449 cmp->font = font;
27451 pcm = NULL;
27452 if (! font_not_found_p)
27454 get_char_face_and_encoding (it->f, c, it->face_id,
27455 &char2b, false);
27456 pcm = get_per_char_metric (font, &char2b);
27459 /* Initialize the bounding box. */
27460 if (pcm)
27462 width = cmp->glyph_len > 0 ? pcm->width : 0;
27463 ascent = pcm->ascent;
27464 descent = pcm->descent;
27465 lbearing = pcm->lbearing;
27466 rbearing = pcm->rbearing;
27468 else
27470 width = cmp->glyph_len > 0 ? font->space_width : 0;
27471 ascent = FONT_BASE (font);
27472 descent = FONT_DESCENT (font);
27473 lbearing = 0;
27474 rbearing = width;
27477 rightmost = width;
27478 leftmost = 0;
27479 lowest = - descent + boff;
27480 highest = ascent + boff;
27482 if (! font_not_found_p
27483 && font->default_ascent
27484 && CHAR_TABLE_P (Vuse_default_ascent)
27485 && !NILP (Faref (Vuse_default_ascent,
27486 make_number (it->char_to_display))))
27487 highest = font->default_ascent + boff;
27489 /* Draw the first glyph at the normal position. It may be
27490 shifted to right later if some other glyphs are drawn
27491 at the left. */
27492 cmp->offsets[i * 2] = 0;
27493 cmp->offsets[i * 2 + 1] = boff;
27494 cmp->lbearing = lbearing;
27495 cmp->rbearing = rbearing;
27497 /* Set cmp->offsets for the remaining glyphs. */
27498 for (i++; i < glyph_len; i++)
27500 int left, right, btm, top;
27501 int ch = COMPOSITION_GLYPH (cmp, i);
27502 int face_id;
27503 struct face *this_face;
27505 if (ch == '\t')
27506 ch = ' ';
27507 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27508 this_face = FACE_FROM_ID (it->f, face_id);
27509 font = this_face->font;
27511 if (font == NULL)
27512 pcm = NULL;
27513 else
27515 get_char_face_and_encoding (it->f, ch, face_id,
27516 &char2b, false);
27517 pcm = get_per_char_metric (font, &char2b);
27519 if (! pcm)
27520 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27521 else
27523 width = pcm->width;
27524 ascent = pcm->ascent;
27525 descent = pcm->descent;
27526 lbearing = pcm->lbearing;
27527 rbearing = pcm->rbearing;
27528 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27530 /* Relative composition with or without
27531 alternate chars. */
27532 left = (leftmost + rightmost - width) / 2;
27533 btm = - descent + boff;
27534 if (font->relative_compose
27535 && (! CHAR_TABLE_P (Vignore_relative_composition)
27536 || NILP (Faref (Vignore_relative_composition,
27537 make_number (ch)))))
27540 if (- descent >= font->relative_compose)
27541 /* One extra pixel between two glyphs. */
27542 btm = highest + 1;
27543 else if (ascent <= 0)
27544 /* One extra pixel between two glyphs. */
27545 btm = lowest - 1 - ascent - descent;
27548 else
27550 /* A composition rule is specified by an integer
27551 value that encodes global and new reference
27552 points (GREF and NREF). GREF and NREF are
27553 specified by numbers as below:
27555 0---1---2 -- ascent
27559 9--10--11 -- center
27561 ---3---4---5--- baseline
27563 6---7---8 -- descent
27565 int rule = COMPOSITION_RULE (cmp, i);
27566 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27568 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27569 grefx = gref % 3, nrefx = nref % 3;
27570 grefy = gref / 3, nrefy = nref / 3;
27571 if (xoff)
27572 xoff = font_height * (xoff - 128) / 256;
27573 if (yoff)
27574 yoff = font_height * (yoff - 128) / 256;
27576 left = (leftmost
27577 + grefx * (rightmost - leftmost) / 2
27578 - nrefx * width / 2
27579 + xoff);
27581 btm = ((grefy == 0 ? highest
27582 : grefy == 1 ? 0
27583 : grefy == 2 ? lowest
27584 : (highest + lowest) / 2)
27585 - (nrefy == 0 ? ascent + descent
27586 : nrefy == 1 ? descent - boff
27587 : nrefy == 2 ? 0
27588 : (ascent + descent) / 2)
27589 + yoff);
27592 cmp->offsets[i * 2] = left;
27593 cmp->offsets[i * 2 + 1] = btm + descent;
27595 /* Update the bounding box of the overall glyphs. */
27596 if (width > 0)
27598 right = left + width;
27599 if (left < leftmost)
27600 leftmost = left;
27601 if (right > rightmost)
27602 rightmost = right;
27604 top = btm + descent + ascent;
27605 if (top > highest)
27606 highest = top;
27607 if (btm < lowest)
27608 lowest = btm;
27610 if (cmp->lbearing > left + lbearing)
27611 cmp->lbearing = left + lbearing;
27612 if (cmp->rbearing < left + rbearing)
27613 cmp->rbearing = left + rbearing;
27617 /* If there are glyphs whose x-offsets are negative,
27618 shift all glyphs to the right and make all x-offsets
27619 non-negative. */
27620 if (leftmost < 0)
27622 for (i = 0; i < cmp->glyph_len; i++)
27623 cmp->offsets[i * 2] -= leftmost;
27624 rightmost -= leftmost;
27625 cmp->lbearing -= leftmost;
27626 cmp->rbearing -= leftmost;
27629 if (left_padded && cmp->lbearing < 0)
27631 for (i = 0; i < cmp->glyph_len; i++)
27632 cmp->offsets[i * 2] -= cmp->lbearing;
27633 rightmost -= cmp->lbearing;
27634 cmp->rbearing -= cmp->lbearing;
27635 cmp->lbearing = 0;
27637 if (right_padded && rightmost < cmp->rbearing)
27639 rightmost = cmp->rbearing;
27642 cmp->pixel_width = rightmost;
27643 cmp->ascent = highest;
27644 cmp->descent = - lowest;
27645 if (cmp->ascent < font_ascent)
27646 cmp->ascent = font_ascent;
27647 if (cmp->descent < font_descent)
27648 cmp->descent = font_descent;
27651 if (it->glyph_row
27652 && (cmp->lbearing < 0
27653 || cmp->rbearing > cmp->pixel_width))
27654 it->glyph_row->contains_overlapping_glyphs_p = true;
27656 it->pixel_width = cmp->pixel_width;
27657 it->ascent = it->phys_ascent = cmp->ascent;
27658 it->descent = it->phys_descent = cmp->descent;
27659 if (face->box != FACE_NO_BOX)
27661 int thick = face->box_line_width;
27663 if (thick > 0)
27665 it->ascent += thick;
27666 it->descent += thick;
27668 else
27669 thick = - thick;
27671 if (it->start_of_box_run_p)
27672 it->pixel_width += thick;
27673 if (it->end_of_box_run_p)
27674 it->pixel_width += thick;
27677 /* If face has an overline, add the height of the overline
27678 (1 pixel) and a 1 pixel margin to the character height. */
27679 if (face->overline_p)
27680 it->ascent += overline_margin;
27682 take_vertical_position_into_account (it);
27683 if (it->ascent < 0)
27684 it->ascent = 0;
27685 if (it->descent < 0)
27686 it->descent = 0;
27688 if (it->glyph_row && cmp->glyph_len > 0)
27689 append_composite_glyph (it);
27691 else if (it->what == IT_COMPOSITION)
27693 /* A dynamic (automatic) composition. */
27694 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27695 Lisp_Object gstring;
27696 struct font_metrics metrics;
27698 it->nglyphs = 1;
27700 gstring = composition_gstring_from_id (it->cmp_it.id);
27701 it->pixel_width
27702 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27703 &metrics);
27704 if (it->glyph_row
27705 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27706 it->glyph_row->contains_overlapping_glyphs_p = true;
27707 it->ascent = it->phys_ascent = metrics.ascent;
27708 it->descent = it->phys_descent = metrics.descent;
27709 if (face->box != FACE_NO_BOX)
27711 int thick = face->box_line_width;
27713 if (thick > 0)
27715 it->ascent += thick;
27716 it->descent += thick;
27718 else
27719 thick = - thick;
27721 if (it->start_of_box_run_p)
27722 it->pixel_width += thick;
27723 if (it->end_of_box_run_p)
27724 it->pixel_width += thick;
27726 /* If face has an overline, add the height of the overline
27727 (1 pixel) and a 1 pixel margin to the character height. */
27728 if (face->overline_p)
27729 it->ascent += overline_margin;
27730 take_vertical_position_into_account (it);
27731 if (it->ascent < 0)
27732 it->ascent = 0;
27733 if (it->descent < 0)
27734 it->descent = 0;
27736 if (it->glyph_row)
27737 append_composite_glyph (it);
27739 else if (it->what == IT_GLYPHLESS)
27740 produce_glyphless_glyph (it, false, Qnil);
27741 else if (it->what == IT_IMAGE)
27742 produce_image_glyph (it);
27743 else if (it->what == IT_STRETCH)
27744 produce_stretch_glyph (it);
27745 else if (it->what == IT_XWIDGET)
27746 produce_xwidget_glyph (it);
27748 done:
27749 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27750 because this isn't true for images with `:ascent 100'. */
27751 eassert (it->ascent >= 0 && it->descent >= 0);
27752 if (it->area == TEXT_AREA)
27753 it->current_x += it->pixel_width;
27755 if (extra_line_spacing > 0)
27757 it->descent += extra_line_spacing;
27758 if (extra_line_spacing > it->max_extra_line_spacing)
27759 it->max_extra_line_spacing = extra_line_spacing;
27762 it->max_ascent = max (it->max_ascent, it->ascent);
27763 it->max_descent = max (it->max_descent, it->descent);
27764 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27765 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27768 /* EXPORT for RIF:
27769 Output LEN glyphs starting at START at the nominal cursor position.
27770 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27771 being updated, and UPDATED_AREA is the area of that row being updated. */
27773 void
27774 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27775 struct glyph *start, enum glyph_row_area updated_area, int len)
27777 int x, hpos, chpos = w->phys_cursor.hpos;
27779 eassert (updated_row);
27780 /* When the window is hscrolled, cursor hpos can legitimately be out
27781 of bounds, but we draw the cursor at the corresponding window
27782 margin in that case. */
27783 if (!updated_row->reversed_p && chpos < 0)
27784 chpos = 0;
27785 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27786 chpos = updated_row->used[TEXT_AREA] - 1;
27788 block_input ();
27790 /* Write glyphs. */
27792 hpos = start - updated_row->glyphs[updated_area];
27793 x = draw_glyphs (w, w->output_cursor.x,
27794 updated_row, updated_area,
27795 hpos, hpos + len,
27796 DRAW_NORMAL_TEXT, 0);
27798 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27799 if (updated_area == TEXT_AREA
27800 && w->phys_cursor_on_p
27801 && w->phys_cursor.vpos == w->output_cursor.vpos
27802 && chpos >= hpos
27803 && chpos < hpos + len)
27804 w->phys_cursor_on_p = false;
27806 unblock_input ();
27808 /* Advance the output cursor. */
27809 w->output_cursor.hpos += len;
27810 w->output_cursor.x = x;
27814 /* EXPORT for RIF:
27815 Insert LEN glyphs from START at the nominal cursor position. */
27817 void
27818 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27819 struct glyph *start, enum glyph_row_area updated_area, int len)
27821 struct frame *f;
27822 int line_height, shift_by_width, shifted_region_width;
27823 struct glyph_row *row;
27824 struct glyph *glyph;
27825 int frame_x, frame_y;
27826 ptrdiff_t hpos;
27828 eassert (updated_row);
27829 block_input ();
27830 f = XFRAME (WINDOW_FRAME (w));
27832 /* Get the height of the line we are in. */
27833 row = updated_row;
27834 line_height = row->height;
27836 /* Get the width of the glyphs to insert. */
27837 shift_by_width = 0;
27838 for (glyph = start; glyph < start + len; ++glyph)
27839 shift_by_width += glyph->pixel_width;
27841 /* Get the width of the region to shift right. */
27842 shifted_region_width = (window_box_width (w, updated_area)
27843 - w->output_cursor.x
27844 - shift_by_width);
27846 /* Shift right. */
27847 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27848 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27850 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27851 line_height, shift_by_width);
27853 /* Write the glyphs. */
27854 hpos = start - row->glyphs[updated_area];
27855 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27856 hpos, hpos + len,
27857 DRAW_NORMAL_TEXT, 0);
27859 /* Advance the output cursor. */
27860 w->output_cursor.hpos += len;
27861 w->output_cursor.x += shift_by_width;
27862 unblock_input ();
27866 /* EXPORT for RIF:
27867 Erase the current text line from the nominal cursor position
27868 (inclusive) to pixel column TO_X (exclusive). The idea is that
27869 everything from TO_X onward is already erased.
27871 TO_X is a pixel position relative to UPDATED_AREA of currently
27872 updated window W. TO_X == -1 means clear to the end of this area. */
27874 void
27875 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27876 enum glyph_row_area updated_area, int to_x)
27878 struct frame *f;
27879 int max_x, min_y, max_y;
27880 int from_x, from_y, to_y;
27882 eassert (updated_row);
27883 f = XFRAME (w->frame);
27885 if (updated_row->full_width_p)
27886 max_x = (WINDOW_PIXEL_WIDTH (w)
27887 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27888 else
27889 max_x = window_box_width (w, updated_area);
27890 max_y = window_text_bottom_y (w);
27892 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27893 of window. For TO_X > 0, truncate to end of drawing area. */
27894 if (to_x == 0)
27895 return;
27896 else if (to_x < 0)
27897 to_x = max_x;
27898 else
27899 to_x = min (to_x, max_x);
27901 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27903 /* Notice if the cursor will be cleared by this operation. */
27904 if (!updated_row->full_width_p)
27905 notice_overwritten_cursor (w, updated_area,
27906 w->output_cursor.x, -1,
27907 updated_row->y,
27908 MATRIX_ROW_BOTTOM_Y (updated_row));
27910 from_x = w->output_cursor.x;
27912 /* Translate to frame coordinates. */
27913 if (updated_row->full_width_p)
27915 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27916 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27918 else
27920 int area_left = window_box_left (w, updated_area);
27921 from_x += area_left;
27922 to_x += area_left;
27925 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27926 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27927 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27929 /* Prevent inadvertently clearing to end of the X window. */
27930 if (to_x > from_x && to_y > from_y)
27932 block_input ();
27933 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27934 to_x - from_x, to_y - from_y);
27935 unblock_input ();
27939 #endif /* HAVE_WINDOW_SYSTEM */
27943 /***********************************************************************
27944 Cursor types
27945 ***********************************************************************/
27947 /* Value is the internal representation of the specified cursor type
27948 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27949 of the bar cursor. */
27951 static enum text_cursor_kinds
27952 get_specified_cursor_type (Lisp_Object arg, int *width)
27954 enum text_cursor_kinds type;
27956 if (NILP (arg))
27957 return NO_CURSOR;
27959 if (EQ (arg, Qbox))
27960 return FILLED_BOX_CURSOR;
27962 if (EQ (arg, Qhollow))
27963 return HOLLOW_BOX_CURSOR;
27965 if (EQ (arg, Qbar))
27967 *width = 2;
27968 return BAR_CURSOR;
27971 if (CONSP (arg)
27972 && EQ (XCAR (arg), Qbar)
27973 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27975 *width = XINT (XCDR (arg));
27976 return BAR_CURSOR;
27979 if (EQ (arg, Qhbar))
27981 *width = 2;
27982 return HBAR_CURSOR;
27985 if (CONSP (arg)
27986 && EQ (XCAR (arg), Qhbar)
27987 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27989 *width = XINT (XCDR (arg));
27990 return HBAR_CURSOR;
27993 /* Treat anything unknown as "hollow box cursor".
27994 It was bad to signal an error; people have trouble fixing
27995 .Xdefaults with Emacs, when it has something bad in it. */
27996 type = HOLLOW_BOX_CURSOR;
27998 return type;
28001 /* Set the default cursor types for specified frame. */
28002 void
28003 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28005 int width = 1;
28006 Lisp_Object tem;
28008 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28009 FRAME_CURSOR_WIDTH (f) = width;
28011 /* By default, set up the blink-off state depending on the on-state. */
28013 tem = Fassoc (arg, Vblink_cursor_alist);
28014 if (!NILP (tem))
28016 FRAME_BLINK_OFF_CURSOR (f)
28017 = get_specified_cursor_type (XCDR (tem), &width);
28018 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28020 else
28021 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28023 /* Make sure the cursor gets redrawn. */
28024 f->cursor_type_changed = true;
28028 #ifdef HAVE_WINDOW_SYSTEM
28030 /* Return the cursor we want to be displayed in window W. Return
28031 width of bar/hbar cursor through WIDTH arg. Return with
28032 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28033 (i.e. if the `system caret' should track this cursor).
28035 In a mini-buffer window, we want the cursor only to appear if we
28036 are reading input from this window. For the selected window, we
28037 want the cursor type given by the frame parameter or buffer local
28038 setting of cursor-type. If explicitly marked off, draw no cursor.
28039 In all other cases, we want a hollow box cursor. */
28041 static enum text_cursor_kinds
28042 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28043 bool *active_cursor)
28045 struct frame *f = XFRAME (w->frame);
28046 struct buffer *b = XBUFFER (w->contents);
28047 int cursor_type = DEFAULT_CURSOR;
28048 Lisp_Object alt_cursor;
28049 bool non_selected = false;
28051 *active_cursor = true;
28053 /* Echo area */
28054 if (cursor_in_echo_area
28055 && FRAME_HAS_MINIBUF_P (f)
28056 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28058 if (w == XWINDOW (echo_area_window))
28060 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28062 *width = FRAME_CURSOR_WIDTH (f);
28063 return FRAME_DESIRED_CURSOR (f);
28065 else
28066 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28069 *active_cursor = false;
28070 non_selected = true;
28073 /* Detect a nonselected window or nonselected frame. */
28074 else if (w != XWINDOW (f->selected_window)
28075 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28077 *active_cursor = false;
28079 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28080 return NO_CURSOR;
28082 non_selected = true;
28085 /* Never display a cursor in a window in which cursor-type is nil. */
28086 if (NILP (BVAR (b, cursor_type)))
28087 return NO_CURSOR;
28089 /* Get the normal cursor type for this window. */
28090 if (EQ (BVAR (b, cursor_type), Qt))
28092 cursor_type = FRAME_DESIRED_CURSOR (f);
28093 *width = FRAME_CURSOR_WIDTH (f);
28095 else
28096 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28098 /* Use cursor-in-non-selected-windows instead
28099 for non-selected window or frame. */
28100 if (non_selected)
28102 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28103 if (!EQ (Qt, alt_cursor))
28104 return get_specified_cursor_type (alt_cursor, width);
28105 /* t means modify the normal cursor type. */
28106 if (cursor_type == FILLED_BOX_CURSOR)
28107 cursor_type = HOLLOW_BOX_CURSOR;
28108 else if (cursor_type == BAR_CURSOR && *width > 1)
28109 --*width;
28110 return cursor_type;
28113 /* Use normal cursor if not blinked off. */
28114 if (!w->cursor_off_p)
28116 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28117 return NO_CURSOR;
28118 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28120 if (cursor_type == FILLED_BOX_CURSOR)
28122 /* Using a block cursor on large images can be very annoying.
28123 So use a hollow cursor for "large" images.
28124 If image is not transparent (no mask), also use hollow cursor. */
28125 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28126 if (img != NULL && IMAGEP (img->spec))
28128 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28129 where N = size of default frame font size.
28130 This should cover most of the "tiny" icons people may use. */
28131 if (!img->mask
28132 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28133 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28134 cursor_type = HOLLOW_BOX_CURSOR;
28137 else if (cursor_type != NO_CURSOR)
28139 /* Display current only supports BOX and HOLLOW cursors for images.
28140 So for now, unconditionally use a HOLLOW cursor when cursor is
28141 not a solid box cursor. */
28142 cursor_type = HOLLOW_BOX_CURSOR;
28145 return cursor_type;
28148 /* Cursor is blinked off, so determine how to "toggle" it. */
28150 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28151 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28152 return get_specified_cursor_type (XCDR (alt_cursor), width);
28154 /* Then see if frame has specified a specific blink off cursor type. */
28155 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28157 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28158 return FRAME_BLINK_OFF_CURSOR (f);
28161 #if false
28162 /* Some people liked having a permanently visible blinking cursor,
28163 while others had very strong opinions against it. So it was
28164 decided to remove it. KFS 2003-09-03 */
28166 /* Finally perform built-in cursor blinking:
28167 filled box <-> hollow box
28168 wide [h]bar <-> narrow [h]bar
28169 narrow [h]bar <-> no cursor
28170 other type <-> no cursor */
28172 if (cursor_type == FILLED_BOX_CURSOR)
28173 return HOLLOW_BOX_CURSOR;
28175 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28177 *width = 1;
28178 return cursor_type;
28180 #endif
28182 return NO_CURSOR;
28186 /* Notice when the text cursor of window W has been completely
28187 overwritten by a drawing operation that outputs glyphs in AREA
28188 starting at X0 and ending at X1 in the line starting at Y0 and
28189 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28190 the rest of the line after X0 has been written. Y coordinates
28191 are window-relative. */
28193 static void
28194 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28195 int x0, int x1, int y0, int y1)
28197 int cx0, cx1, cy0, cy1;
28198 struct glyph_row *row;
28200 if (!w->phys_cursor_on_p)
28201 return;
28202 if (area != TEXT_AREA)
28203 return;
28205 if (w->phys_cursor.vpos < 0
28206 || w->phys_cursor.vpos >= w->current_matrix->nrows
28207 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28208 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28209 return;
28211 if (row->cursor_in_fringe_p)
28213 row->cursor_in_fringe_p = false;
28214 draw_fringe_bitmap (w, row, row->reversed_p);
28215 w->phys_cursor_on_p = false;
28216 return;
28219 cx0 = w->phys_cursor.x;
28220 cx1 = cx0 + w->phys_cursor_width;
28221 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28222 return;
28224 /* The cursor image will be completely removed from the
28225 screen if the output area intersects the cursor area in
28226 y-direction. When we draw in [y0 y1[, and some part of
28227 the cursor is at y < y0, that part must have been drawn
28228 before. When scrolling, the cursor is erased before
28229 actually scrolling, so we don't come here. When not
28230 scrolling, the rows above the old cursor row must have
28231 changed, and in this case these rows must have written
28232 over the cursor image.
28234 Likewise if part of the cursor is below y1, with the
28235 exception of the cursor being in the first blank row at
28236 the buffer and window end because update_text_area
28237 doesn't draw that row. (Except when it does, but
28238 that's handled in update_text_area.) */
28240 cy0 = w->phys_cursor.y;
28241 cy1 = cy0 + w->phys_cursor_height;
28242 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28243 return;
28245 w->phys_cursor_on_p = false;
28248 #endif /* HAVE_WINDOW_SYSTEM */
28251 /************************************************************************
28252 Mouse Face
28253 ************************************************************************/
28255 #ifdef HAVE_WINDOW_SYSTEM
28257 /* EXPORT for RIF:
28258 Fix the display of area AREA of overlapping row ROW in window W
28259 with respect to the overlapping part OVERLAPS. */
28261 void
28262 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28263 enum glyph_row_area area, int overlaps)
28265 int i, x;
28267 block_input ();
28269 x = 0;
28270 for (i = 0; i < row->used[area];)
28272 if (row->glyphs[area][i].overlaps_vertically_p)
28274 int start = i, start_x = x;
28278 x += row->glyphs[area][i].pixel_width;
28279 ++i;
28281 while (i < row->used[area]
28282 && row->glyphs[area][i].overlaps_vertically_p);
28284 draw_glyphs (w, start_x, row, area,
28285 start, i,
28286 DRAW_NORMAL_TEXT, overlaps);
28288 else
28290 x += row->glyphs[area][i].pixel_width;
28291 ++i;
28295 unblock_input ();
28299 /* EXPORT:
28300 Draw the cursor glyph of window W in glyph row ROW. See the
28301 comment of draw_glyphs for the meaning of HL. */
28303 void
28304 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28305 enum draw_glyphs_face hl)
28307 /* If cursor hpos is out of bounds, don't draw garbage. This can
28308 happen in mini-buffer windows when switching between echo area
28309 glyphs and mini-buffer. */
28310 if ((row->reversed_p
28311 ? (w->phys_cursor.hpos >= 0)
28312 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28314 bool on_p = w->phys_cursor_on_p;
28315 int x1;
28316 int hpos = w->phys_cursor.hpos;
28318 /* When the window is hscrolled, cursor hpos can legitimately be
28319 out of bounds, but we draw the cursor at the corresponding
28320 window margin in that case. */
28321 if (!row->reversed_p && hpos < 0)
28322 hpos = 0;
28323 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28324 hpos = row->used[TEXT_AREA] - 1;
28326 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28327 hl, 0);
28328 w->phys_cursor_on_p = on_p;
28330 if (hl == DRAW_CURSOR)
28331 w->phys_cursor_width = x1 - w->phys_cursor.x;
28332 /* When we erase the cursor, and ROW is overlapped by other
28333 rows, make sure that these overlapping parts of other rows
28334 are redrawn. */
28335 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28337 w->phys_cursor_width = x1 - w->phys_cursor.x;
28339 if (row > w->current_matrix->rows
28340 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28341 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28342 OVERLAPS_ERASED_CURSOR);
28344 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28345 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28346 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28347 OVERLAPS_ERASED_CURSOR);
28353 /* Erase the image of a cursor of window W from the screen. */
28355 void
28356 erase_phys_cursor (struct window *w)
28358 struct frame *f = XFRAME (w->frame);
28359 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28360 int hpos = w->phys_cursor.hpos;
28361 int vpos = w->phys_cursor.vpos;
28362 bool mouse_face_here_p = false;
28363 struct glyph_matrix *active_glyphs = w->current_matrix;
28364 struct glyph_row *cursor_row;
28365 struct glyph *cursor_glyph;
28366 enum draw_glyphs_face hl;
28368 /* No cursor displayed or row invalidated => nothing to do on the
28369 screen. */
28370 if (w->phys_cursor_type == NO_CURSOR)
28371 goto mark_cursor_off;
28373 /* VPOS >= active_glyphs->nrows means that window has been resized.
28374 Don't bother to erase the cursor. */
28375 if (vpos >= active_glyphs->nrows)
28376 goto mark_cursor_off;
28378 /* If row containing cursor is marked invalid, there is nothing we
28379 can do. */
28380 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28381 if (!cursor_row->enabled_p)
28382 goto mark_cursor_off;
28384 /* If line spacing is > 0, old cursor may only be partially visible in
28385 window after split-window. So adjust visible height. */
28386 cursor_row->visible_height = min (cursor_row->visible_height,
28387 window_text_bottom_y (w) - cursor_row->y);
28389 /* If row is completely invisible, don't attempt to delete a cursor which
28390 isn't there. This can happen if cursor is at top of a window, and
28391 we switch to a buffer with a header line in that window. */
28392 if (cursor_row->visible_height <= 0)
28393 goto mark_cursor_off;
28395 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28396 if (cursor_row->cursor_in_fringe_p)
28398 cursor_row->cursor_in_fringe_p = false;
28399 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28400 goto mark_cursor_off;
28403 /* This can happen when the new row is shorter than the old one.
28404 In this case, either draw_glyphs or clear_end_of_line
28405 should have cleared the cursor. Note that we wouldn't be
28406 able to erase the cursor in this case because we don't have a
28407 cursor glyph at hand. */
28408 if ((cursor_row->reversed_p
28409 ? (w->phys_cursor.hpos < 0)
28410 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28411 goto mark_cursor_off;
28413 /* When the window is hscrolled, cursor hpos can legitimately be out
28414 of bounds, but we draw the cursor at the corresponding window
28415 margin in that case. */
28416 if (!cursor_row->reversed_p && hpos < 0)
28417 hpos = 0;
28418 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28419 hpos = cursor_row->used[TEXT_AREA] - 1;
28421 /* If the cursor is in the mouse face area, redisplay that when
28422 we clear the cursor. */
28423 if (! NILP (hlinfo->mouse_face_window)
28424 && coords_in_mouse_face_p (w, hpos, vpos)
28425 /* Don't redraw the cursor's spot in mouse face if it is at the
28426 end of a line (on a newline). The cursor appears there, but
28427 mouse highlighting does not. */
28428 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28429 mouse_face_here_p = true;
28431 /* Maybe clear the display under the cursor. */
28432 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28434 int x, y;
28435 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28436 int width;
28438 cursor_glyph = get_phys_cursor_glyph (w);
28439 if (cursor_glyph == NULL)
28440 goto mark_cursor_off;
28442 width = cursor_glyph->pixel_width;
28443 x = w->phys_cursor.x;
28444 if (x < 0)
28446 width += x;
28447 x = 0;
28449 width = min (width, window_box_width (w, TEXT_AREA) - x);
28450 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28451 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28453 if (width > 0)
28454 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28457 /* Erase the cursor by redrawing the character underneath it. */
28458 if (mouse_face_here_p)
28459 hl = DRAW_MOUSE_FACE;
28460 else
28461 hl = DRAW_NORMAL_TEXT;
28462 draw_phys_cursor_glyph (w, cursor_row, hl);
28464 mark_cursor_off:
28465 w->phys_cursor_on_p = false;
28466 w->phys_cursor_type = NO_CURSOR;
28470 /* Display or clear cursor of window W. If !ON, clear the cursor.
28471 If ON, display the cursor; where to put the cursor is specified by
28472 HPOS, VPOS, X and Y. */
28474 void
28475 display_and_set_cursor (struct window *w, bool on,
28476 int hpos, int vpos, int x, int y)
28478 struct frame *f = XFRAME (w->frame);
28479 int new_cursor_type;
28480 int new_cursor_width;
28481 bool active_cursor;
28482 struct glyph_row *glyph_row;
28483 struct glyph *glyph;
28485 /* This is pointless on invisible frames, and dangerous on garbaged
28486 windows and frames; in the latter case, the frame or window may
28487 be in the midst of changing its size, and x and y may be off the
28488 window. */
28489 if (! FRAME_VISIBLE_P (f)
28490 || FRAME_GARBAGED_P (f)
28491 || vpos >= w->current_matrix->nrows
28492 || hpos >= w->current_matrix->matrix_w)
28493 return;
28495 /* If cursor is off and we want it off, return quickly. */
28496 if (!on && !w->phys_cursor_on_p)
28497 return;
28499 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28500 /* If cursor row is not enabled, we don't really know where to
28501 display the cursor. */
28502 if (!glyph_row->enabled_p)
28504 w->phys_cursor_on_p = false;
28505 return;
28508 glyph = NULL;
28509 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28510 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28512 eassert (input_blocked_p ());
28514 /* Set new_cursor_type to the cursor we want to be displayed. */
28515 new_cursor_type = get_window_cursor_type (w, glyph,
28516 &new_cursor_width, &active_cursor);
28518 /* If cursor is currently being shown and we don't want it to be or
28519 it is in the wrong place, or the cursor type is not what we want,
28520 erase it. */
28521 if (w->phys_cursor_on_p
28522 && (!on
28523 || w->phys_cursor.x != x
28524 || w->phys_cursor.y != y
28525 /* HPOS can be negative in R2L rows whose
28526 exact_window_width_line_p flag is set (i.e. their newline
28527 would "overflow into the fringe"). */
28528 || hpos < 0
28529 || new_cursor_type != w->phys_cursor_type
28530 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28531 && new_cursor_width != w->phys_cursor_width)))
28532 erase_phys_cursor (w);
28534 /* Don't check phys_cursor_on_p here because that flag is only set
28535 to false in some cases where we know that the cursor has been
28536 completely erased, to avoid the extra work of erasing the cursor
28537 twice. In other words, phys_cursor_on_p can be true and the cursor
28538 still not be visible, or it has only been partly erased. */
28539 if (on)
28541 w->phys_cursor_ascent = glyph_row->ascent;
28542 w->phys_cursor_height = glyph_row->height;
28544 /* Set phys_cursor_.* before x_draw_.* is called because some
28545 of them may need the information. */
28546 w->phys_cursor.x = x;
28547 w->phys_cursor.y = glyph_row->y;
28548 w->phys_cursor.hpos = hpos;
28549 w->phys_cursor.vpos = vpos;
28552 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28553 new_cursor_type, new_cursor_width,
28554 on, active_cursor);
28558 /* Switch the display of W's cursor on or off, according to the value
28559 of ON. */
28561 static void
28562 update_window_cursor (struct window *w, bool on)
28564 /* Don't update cursor in windows whose frame is in the process
28565 of being deleted. */
28566 if (w->current_matrix)
28568 int hpos = w->phys_cursor.hpos;
28569 int vpos = w->phys_cursor.vpos;
28570 struct glyph_row *row;
28572 if (vpos >= w->current_matrix->nrows
28573 || hpos >= w->current_matrix->matrix_w)
28574 return;
28576 row = MATRIX_ROW (w->current_matrix, vpos);
28578 /* When the window is hscrolled, cursor hpos can legitimately be
28579 out of bounds, but we draw the cursor at the corresponding
28580 window margin in that case. */
28581 if (!row->reversed_p && hpos < 0)
28582 hpos = 0;
28583 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28584 hpos = row->used[TEXT_AREA] - 1;
28586 block_input ();
28587 display_and_set_cursor (w, on, hpos, vpos,
28588 w->phys_cursor.x, w->phys_cursor.y);
28589 unblock_input ();
28594 /* Call update_window_cursor with parameter ON_P on all leaf windows
28595 in the window tree rooted at W. */
28597 static void
28598 update_cursor_in_window_tree (struct window *w, bool on_p)
28600 while (w)
28602 if (WINDOWP (w->contents))
28603 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28604 else
28605 update_window_cursor (w, on_p);
28607 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28612 /* EXPORT:
28613 Display the cursor on window W, or clear it, according to ON_P.
28614 Don't change the cursor's position. */
28616 void
28617 x_update_cursor (struct frame *f, bool on_p)
28619 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28623 /* EXPORT:
28624 Clear the cursor of window W to background color, and mark the
28625 cursor as not shown. This is used when the text where the cursor
28626 is about to be rewritten. */
28628 void
28629 x_clear_cursor (struct window *w)
28631 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28632 update_window_cursor (w, false);
28635 #endif /* HAVE_WINDOW_SYSTEM */
28637 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28638 and MSDOS. */
28639 static void
28640 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28641 int start_hpos, int end_hpos,
28642 enum draw_glyphs_face draw)
28644 #ifdef HAVE_WINDOW_SYSTEM
28645 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28647 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28648 return;
28650 #endif
28651 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28652 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28653 #endif
28656 /* Display the active region described by mouse_face_* according to DRAW. */
28658 static void
28659 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28661 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28662 struct frame *f = XFRAME (WINDOW_FRAME (w));
28664 if (/* If window is in the process of being destroyed, don't bother
28665 to do anything. */
28666 w->current_matrix != NULL
28667 /* Don't update mouse highlight if hidden. */
28668 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28669 /* Recognize when we are called to operate on rows that don't exist
28670 anymore. This can happen when a window is split. */
28671 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28673 bool phys_cursor_on_p = w->phys_cursor_on_p;
28674 struct glyph_row *row, *first, *last;
28676 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28677 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28679 for (row = first; row <= last && row->enabled_p; ++row)
28681 int start_hpos, end_hpos, start_x;
28683 /* For all but the first row, the highlight starts at column 0. */
28684 if (row == first)
28686 /* R2L rows have BEG and END in reversed order, but the
28687 screen drawing geometry is always left to right. So
28688 we need to mirror the beginning and end of the
28689 highlighted area in R2L rows. */
28690 if (!row->reversed_p)
28692 start_hpos = hlinfo->mouse_face_beg_col;
28693 start_x = hlinfo->mouse_face_beg_x;
28695 else if (row == last)
28697 start_hpos = hlinfo->mouse_face_end_col;
28698 start_x = hlinfo->mouse_face_end_x;
28700 else
28702 start_hpos = 0;
28703 start_x = 0;
28706 else if (row->reversed_p && row == last)
28708 start_hpos = hlinfo->mouse_face_end_col;
28709 start_x = hlinfo->mouse_face_end_x;
28711 else
28713 start_hpos = 0;
28714 start_x = 0;
28717 if (row == last)
28719 if (!row->reversed_p)
28720 end_hpos = hlinfo->mouse_face_end_col;
28721 else if (row == first)
28722 end_hpos = hlinfo->mouse_face_beg_col;
28723 else
28725 end_hpos = row->used[TEXT_AREA];
28726 if (draw == DRAW_NORMAL_TEXT)
28727 row->fill_line_p = true; /* Clear to end of line. */
28730 else if (row->reversed_p && row == first)
28731 end_hpos = hlinfo->mouse_face_beg_col;
28732 else
28734 end_hpos = row->used[TEXT_AREA];
28735 if (draw == DRAW_NORMAL_TEXT)
28736 row->fill_line_p = true; /* Clear to end of line. */
28739 if (end_hpos > start_hpos)
28741 draw_row_with_mouse_face (w, start_x, row,
28742 start_hpos, end_hpos, draw);
28744 row->mouse_face_p
28745 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28749 /* When we've written over the cursor, arrange for it to
28750 be displayed again. */
28751 if (FRAME_WINDOW_P (f)
28752 && phys_cursor_on_p && !w->phys_cursor_on_p)
28754 #ifdef HAVE_WINDOW_SYSTEM
28755 int hpos = w->phys_cursor.hpos;
28757 /* When the window is hscrolled, cursor hpos can legitimately be
28758 out of bounds, but we draw the cursor at the corresponding
28759 window margin in that case. */
28760 if (!row->reversed_p && hpos < 0)
28761 hpos = 0;
28762 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28763 hpos = row->used[TEXT_AREA] - 1;
28765 block_input ();
28766 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28767 w->phys_cursor.x, w->phys_cursor.y);
28768 unblock_input ();
28769 #endif /* HAVE_WINDOW_SYSTEM */
28773 #ifdef HAVE_WINDOW_SYSTEM
28774 /* Change the mouse cursor. */
28775 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28777 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28778 if (draw == DRAW_NORMAL_TEXT
28779 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28780 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28781 else
28782 #endif
28783 if (draw == DRAW_MOUSE_FACE)
28784 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28785 else
28786 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28788 #endif /* HAVE_WINDOW_SYSTEM */
28791 /* EXPORT:
28792 Clear out the mouse-highlighted active region.
28793 Redraw it un-highlighted first. Value is true if mouse
28794 face was actually drawn unhighlighted. */
28796 bool
28797 clear_mouse_face (Mouse_HLInfo *hlinfo)
28799 bool cleared
28800 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28801 if (cleared)
28802 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28803 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28804 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28805 hlinfo->mouse_face_window = Qnil;
28806 hlinfo->mouse_face_overlay = Qnil;
28807 return cleared;
28810 /* Return true if the coordinates HPOS and VPOS on windows W are
28811 within the mouse face on that window. */
28812 static bool
28813 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28815 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28817 /* Quickly resolve the easy cases. */
28818 if (!(WINDOWP (hlinfo->mouse_face_window)
28819 && XWINDOW (hlinfo->mouse_face_window) == w))
28820 return false;
28821 if (vpos < hlinfo->mouse_face_beg_row
28822 || vpos > hlinfo->mouse_face_end_row)
28823 return false;
28824 if (vpos > hlinfo->mouse_face_beg_row
28825 && vpos < hlinfo->mouse_face_end_row)
28826 return true;
28828 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28830 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28832 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28833 return true;
28835 else if ((vpos == hlinfo->mouse_face_beg_row
28836 && hpos >= hlinfo->mouse_face_beg_col)
28837 || (vpos == hlinfo->mouse_face_end_row
28838 && hpos < hlinfo->mouse_face_end_col))
28839 return true;
28841 else
28843 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28845 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28846 return true;
28848 else if ((vpos == hlinfo->mouse_face_beg_row
28849 && hpos <= hlinfo->mouse_face_beg_col)
28850 || (vpos == hlinfo->mouse_face_end_row
28851 && hpos > hlinfo->mouse_face_end_col))
28852 return true;
28854 return false;
28858 /* EXPORT:
28859 True if physical cursor of window W is within mouse face. */
28861 bool
28862 cursor_in_mouse_face_p (struct window *w)
28864 int hpos = w->phys_cursor.hpos;
28865 int vpos = w->phys_cursor.vpos;
28866 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28868 /* When the window is hscrolled, cursor hpos can legitimately be out
28869 of bounds, but we draw the cursor at the corresponding window
28870 margin in that case. */
28871 if (!row->reversed_p && hpos < 0)
28872 hpos = 0;
28873 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28874 hpos = row->used[TEXT_AREA] - 1;
28876 return coords_in_mouse_face_p (w, hpos, vpos);
28881 /* Find the glyph rows START_ROW and END_ROW of window W that display
28882 characters between buffer positions START_CHARPOS and END_CHARPOS
28883 (excluding END_CHARPOS). DISP_STRING is a display string that
28884 covers these buffer positions. This is similar to
28885 row_containing_pos, but is more accurate when bidi reordering makes
28886 buffer positions change non-linearly with glyph rows. */
28887 static void
28888 rows_from_pos_range (struct window *w,
28889 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28890 Lisp_Object disp_string,
28891 struct glyph_row **start, struct glyph_row **end)
28893 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28894 int last_y = window_text_bottom_y (w);
28895 struct glyph_row *row;
28897 *start = NULL;
28898 *end = NULL;
28900 while (!first->enabled_p
28901 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28902 first++;
28904 /* Find the START row. */
28905 for (row = first;
28906 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28907 row++)
28909 /* A row can potentially be the START row if the range of the
28910 characters it displays intersects the range
28911 [START_CHARPOS..END_CHARPOS). */
28912 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28913 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28914 /* See the commentary in row_containing_pos, for the
28915 explanation of the complicated way to check whether
28916 some position is beyond the end of the characters
28917 displayed by a row. */
28918 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28919 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28920 && !row->ends_at_zv_p
28921 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28922 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28923 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28924 && !row->ends_at_zv_p
28925 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28927 /* Found a candidate row. Now make sure at least one of the
28928 glyphs it displays has a charpos from the range
28929 [START_CHARPOS..END_CHARPOS).
28931 This is not obvious because bidi reordering could make
28932 buffer positions of a row be 1,2,3,102,101,100, and if we
28933 want to highlight characters in [50..60), we don't want
28934 this row, even though [50..60) does intersect [1..103),
28935 the range of character positions given by the row's start
28936 and end positions. */
28937 struct glyph *g = row->glyphs[TEXT_AREA];
28938 struct glyph *e = g + row->used[TEXT_AREA];
28940 while (g < e)
28942 if (((BUFFERP (g->object) || NILP (g->object))
28943 && start_charpos <= g->charpos && g->charpos < end_charpos)
28944 /* A glyph that comes from DISP_STRING is by
28945 definition to be highlighted. */
28946 || EQ (g->object, disp_string))
28947 *start = row;
28948 g++;
28950 if (*start)
28951 break;
28955 /* Find the END row. */
28956 if (!*start
28957 /* If the last row is partially visible, start looking for END
28958 from that row, instead of starting from FIRST. */
28959 && !(row->enabled_p
28960 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28961 row = first;
28962 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28964 struct glyph_row *next = row + 1;
28965 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28967 if (!next->enabled_p
28968 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28969 /* The first row >= START whose range of displayed characters
28970 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28971 is the row END + 1. */
28972 || (start_charpos < next_start
28973 && end_charpos < next_start)
28974 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28975 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28976 && !next->ends_at_zv_p
28977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28978 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28979 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28980 && !next->ends_at_zv_p
28981 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28983 *end = row;
28984 break;
28986 else
28988 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28989 but none of the characters it displays are in the range, it is
28990 also END + 1. */
28991 struct glyph *g = next->glyphs[TEXT_AREA];
28992 struct glyph *s = g;
28993 struct glyph *e = g + next->used[TEXT_AREA];
28995 while (g < e)
28997 if (((BUFFERP (g->object) || NILP (g->object))
28998 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28999 /* If the buffer position of the first glyph in
29000 the row is equal to END_CHARPOS, it means
29001 the last character to be highlighted is the
29002 newline of ROW, and we must consider NEXT as
29003 END, not END+1. */
29004 || (((!next->reversed_p && g == s)
29005 || (next->reversed_p && g == e - 1))
29006 && (g->charpos == end_charpos
29007 /* Special case for when NEXT is an
29008 empty line at ZV. */
29009 || (g->charpos == -1
29010 && !row->ends_at_zv_p
29011 && next_start == end_charpos)))))
29012 /* A glyph that comes from DISP_STRING is by
29013 definition to be highlighted. */
29014 || EQ (g->object, disp_string))
29015 break;
29016 g++;
29018 if (g == e)
29020 *end = row;
29021 break;
29023 /* The first row that ends at ZV must be the last to be
29024 highlighted. */
29025 else if (next->ends_at_zv_p)
29027 *end = next;
29028 break;
29034 /* This function sets the mouse_face_* elements of HLINFO, assuming
29035 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29036 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29037 for the overlay or run of text properties specifying the mouse
29038 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29039 before-string and after-string that must also be highlighted.
29040 DISP_STRING, if non-nil, is a display string that may cover some
29041 or all of the highlighted text. */
29043 static void
29044 mouse_face_from_buffer_pos (Lisp_Object window,
29045 Mouse_HLInfo *hlinfo,
29046 ptrdiff_t mouse_charpos,
29047 ptrdiff_t start_charpos,
29048 ptrdiff_t end_charpos,
29049 Lisp_Object before_string,
29050 Lisp_Object after_string,
29051 Lisp_Object disp_string)
29053 struct window *w = XWINDOW (window);
29054 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29055 struct glyph_row *r1, *r2;
29056 struct glyph *glyph, *end;
29057 ptrdiff_t ignore, pos;
29058 int x;
29060 eassert (NILP (disp_string) || STRINGP (disp_string));
29061 eassert (NILP (before_string) || STRINGP (before_string));
29062 eassert (NILP (after_string) || STRINGP (after_string));
29064 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29065 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29066 if (r1 == NULL)
29067 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29068 /* If the before-string or display-string contains newlines,
29069 rows_from_pos_range skips to its last row. Move back. */
29070 if (!NILP (before_string) || !NILP (disp_string))
29072 struct glyph_row *prev;
29073 while ((prev = r1 - 1, prev >= first)
29074 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29075 && prev->used[TEXT_AREA] > 0)
29077 struct glyph *beg = prev->glyphs[TEXT_AREA];
29078 glyph = beg + prev->used[TEXT_AREA];
29079 while (--glyph >= beg && NILP (glyph->object));
29080 if (glyph < beg
29081 || !(EQ (glyph->object, before_string)
29082 || EQ (glyph->object, disp_string)))
29083 break;
29084 r1 = prev;
29087 if (r2 == NULL)
29089 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29090 hlinfo->mouse_face_past_end = true;
29092 else if (!NILP (after_string))
29094 /* If the after-string has newlines, advance to its last row. */
29095 struct glyph_row *next;
29096 struct glyph_row *last
29097 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29099 for (next = r2 + 1;
29100 next <= last
29101 && next->used[TEXT_AREA] > 0
29102 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29103 ++next)
29104 r2 = next;
29106 /* The rest of the display engine assumes that mouse_face_beg_row is
29107 either above mouse_face_end_row or identical to it. But with
29108 bidi-reordered continued lines, the row for START_CHARPOS could
29109 be below the row for END_CHARPOS. If so, swap the rows and store
29110 them in correct order. */
29111 if (r1->y > r2->y)
29113 struct glyph_row *tem = r2;
29115 r2 = r1;
29116 r1 = tem;
29119 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29120 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29122 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29123 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29124 could be anywhere in the row and in any order. The strategy
29125 below is to find the leftmost and the rightmost glyph that
29126 belongs to either of these 3 strings, or whose position is
29127 between START_CHARPOS and END_CHARPOS, and highlight all the
29128 glyphs between those two. This may cover more than just the text
29129 between START_CHARPOS and END_CHARPOS if the range of characters
29130 strides the bidi level boundary, e.g. if the beginning is in R2L
29131 text while the end is in L2R text or vice versa. */
29132 if (!r1->reversed_p)
29134 /* This row is in a left to right paragraph. Scan it left to
29135 right. */
29136 glyph = r1->glyphs[TEXT_AREA];
29137 end = glyph + r1->used[TEXT_AREA];
29138 x = r1->x;
29140 /* Skip truncation glyphs at the start of the glyph row. */
29141 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29142 for (; glyph < end
29143 && NILP (glyph->object)
29144 && glyph->charpos < 0;
29145 ++glyph)
29146 x += glyph->pixel_width;
29148 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29149 or DISP_STRING, and the first glyph from buffer whose
29150 position is between START_CHARPOS and END_CHARPOS. */
29151 for (; glyph < end
29152 && !NILP (glyph->object)
29153 && !EQ (glyph->object, disp_string)
29154 && !(BUFFERP (glyph->object)
29155 && (glyph->charpos >= start_charpos
29156 && glyph->charpos < end_charpos));
29157 ++glyph)
29159 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29160 are present at buffer positions between START_CHARPOS and
29161 END_CHARPOS, or if they come from an overlay. */
29162 if (EQ (glyph->object, before_string))
29164 pos = string_buffer_position (before_string,
29165 start_charpos);
29166 /* If pos == 0, it means before_string came from an
29167 overlay, not from a buffer position. */
29168 if (!pos || (pos >= start_charpos && pos < end_charpos))
29169 break;
29171 else if (EQ (glyph->object, after_string))
29173 pos = string_buffer_position (after_string, end_charpos);
29174 if (!pos || (pos >= start_charpos && pos < end_charpos))
29175 break;
29177 x += glyph->pixel_width;
29179 hlinfo->mouse_face_beg_x = x;
29180 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29182 else
29184 /* This row is in a right to left paragraph. Scan it right to
29185 left. */
29186 struct glyph *g;
29188 end = r1->glyphs[TEXT_AREA] - 1;
29189 glyph = end + r1->used[TEXT_AREA];
29191 /* Skip truncation glyphs at the start of the glyph row. */
29192 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29193 for (; glyph > end
29194 && NILP (glyph->object)
29195 && glyph->charpos < 0;
29196 --glyph)
29199 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29200 or DISP_STRING, and the first glyph from buffer whose
29201 position is between START_CHARPOS and END_CHARPOS. */
29202 for (; glyph > end
29203 && !NILP (glyph->object)
29204 && !EQ (glyph->object, disp_string)
29205 && !(BUFFERP (glyph->object)
29206 && (glyph->charpos >= start_charpos
29207 && glyph->charpos < end_charpos));
29208 --glyph)
29210 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29211 are present at buffer positions between START_CHARPOS and
29212 END_CHARPOS, or if they come from an overlay. */
29213 if (EQ (glyph->object, before_string))
29215 pos = string_buffer_position (before_string, start_charpos);
29216 /* If pos == 0, it means before_string came from an
29217 overlay, not from a buffer position. */
29218 if (!pos || (pos >= start_charpos && pos < end_charpos))
29219 break;
29221 else if (EQ (glyph->object, after_string))
29223 pos = string_buffer_position (after_string, end_charpos);
29224 if (!pos || (pos >= start_charpos && pos < end_charpos))
29225 break;
29229 glyph++; /* first glyph to the right of the highlighted area */
29230 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29231 x += g->pixel_width;
29232 hlinfo->mouse_face_beg_x = x;
29233 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29236 /* If the highlight ends in a different row, compute GLYPH and END
29237 for the end row. Otherwise, reuse the values computed above for
29238 the row where the highlight begins. */
29239 if (r2 != r1)
29241 if (!r2->reversed_p)
29243 glyph = r2->glyphs[TEXT_AREA];
29244 end = glyph + r2->used[TEXT_AREA];
29245 x = r2->x;
29247 else
29249 end = r2->glyphs[TEXT_AREA] - 1;
29250 glyph = end + r2->used[TEXT_AREA];
29254 if (!r2->reversed_p)
29256 /* Skip truncation and continuation glyphs near the end of the
29257 row, and also blanks and stretch glyphs inserted by
29258 extend_face_to_end_of_line. */
29259 while (end > glyph
29260 && NILP ((end - 1)->object))
29261 --end;
29262 /* Scan the rest of the glyph row from the end, looking for the
29263 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29264 DISP_STRING, or whose position is between START_CHARPOS
29265 and END_CHARPOS */
29266 for (--end;
29267 end > glyph
29268 && !NILP (end->object)
29269 && !EQ (end->object, disp_string)
29270 && !(BUFFERP (end->object)
29271 && (end->charpos >= start_charpos
29272 && end->charpos < end_charpos));
29273 --end)
29275 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29276 are present at buffer positions between START_CHARPOS and
29277 END_CHARPOS, or if they come from an overlay. */
29278 if (EQ (end->object, before_string))
29280 pos = string_buffer_position (before_string, start_charpos);
29281 if (!pos || (pos >= start_charpos && pos < end_charpos))
29282 break;
29284 else if (EQ (end->object, after_string))
29286 pos = string_buffer_position (after_string, end_charpos);
29287 if (!pos || (pos >= start_charpos && pos < end_charpos))
29288 break;
29291 /* Find the X coordinate of the last glyph to be highlighted. */
29292 for (; glyph <= end; ++glyph)
29293 x += glyph->pixel_width;
29295 hlinfo->mouse_face_end_x = x;
29296 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29298 else
29300 /* Skip truncation and continuation glyphs near the end of the
29301 row, and also blanks and stretch glyphs inserted by
29302 extend_face_to_end_of_line. */
29303 x = r2->x;
29304 end++;
29305 while (end < glyph
29306 && NILP (end->object))
29308 x += end->pixel_width;
29309 ++end;
29311 /* Scan the rest of the glyph row from the end, looking for the
29312 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29313 DISP_STRING, or whose position is between START_CHARPOS
29314 and END_CHARPOS */
29315 for ( ;
29316 end < glyph
29317 && !NILP (end->object)
29318 && !EQ (end->object, disp_string)
29319 && !(BUFFERP (end->object)
29320 && (end->charpos >= start_charpos
29321 && end->charpos < end_charpos));
29322 ++end)
29324 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29325 are present at buffer positions between START_CHARPOS and
29326 END_CHARPOS, or if they come from an overlay. */
29327 if (EQ (end->object, before_string))
29329 pos = string_buffer_position (before_string, start_charpos);
29330 if (!pos || (pos >= start_charpos && pos < end_charpos))
29331 break;
29333 else if (EQ (end->object, after_string))
29335 pos = string_buffer_position (after_string, end_charpos);
29336 if (!pos || (pos >= start_charpos && pos < end_charpos))
29337 break;
29339 x += end->pixel_width;
29341 /* If we exited the above loop because we arrived at the last
29342 glyph of the row, and its buffer position is still not in
29343 range, it means the last character in range is the preceding
29344 newline. Bump the end column and x values to get past the
29345 last glyph. */
29346 if (end == glyph
29347 && BUFFERP (end->object)
29348 && (end->charpos < start_charpos
29349 || end->charpos >= end_charpos))
29351 x += end->pixel_width;
29352 ++end;
29354 hlinfo->mouse_face_end_x = x;
29355 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29358 hlinfo->mouse_face_window = window;
29359 hlinfo->mouse_face_face_id
29360 = face_at_buffer_position (w, mouse_charpos, &ignore,
29361 mouse_charpos + 1,
29362 !hlinfo->mouse_face_hidden, -1);
29363 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29366 /* The following function is not used anymore (replaced with
29367 mouse_face_from_string_pos), but I leave it here for the time
29368 being, in case someone would. */
29370 #if false /* not used */
29372 /* Find the position of the glyph for position POS in OBJECT in
29373 window W's current matrix, and return in *X, *Y the pixel
29374 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29376 RIGHT_P means return the position of the right edge of the glyph.
29377 !RIGHT_P means return the left edge position.
29379 If no glyph for POS exists in the matrix, return the position of
29380 the glyph with the next smaller position that is in the matrix, if
29381 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29382 exists in the matrix, return the position of the glyph with the
29383 next larger position in OBJECT.
29385 Value is true if a glyph was found. */
29387 static bool
29388 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29389 int *hpos, int *vpos, int *x, int *y, bool right_p)
29391 int yb = window_text_bottom_y (w);
29392 struct glyph_row *r;
29393 struct glyph *best_glyph = NULL;
29394 struct glyph_row *best_row = NULL;
29395 int best_x = 0;
29397 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29398 r->enabled_p && r->y < yb;
29399 ++r)
29401 struct glyph *g = r->glyphs[TEXT_AREA];
29402 struct glyph *e = g + r->used[TEXT_AREA];
29403 int gx;
29405 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29406 if (EQ (g->object, object))
29408 if (g->charpos == pos)
29410 best_glyph = g;
29411 best_x = gx;
29412 best_row = r;
29413 goto found;
29415 else if (best_glyph == NULL
29416 || ((eabs (g->charpos - pos)
29417 < eabs (best_glyph->charpos - pos))
29418 && (right_p
29419 ? g->charpos < pos
29420 : g->charpos > pos)))
29422 best_glyph = g;
29423 best_x = gx;
29424 best_row = r;
29429 found:
29431 if (best_glyph)
29433 *x = best_x;
29434 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29436 if (right_p)
29438 *x += best_glyph->pixel_width;
29439 ++*hpos;
29442 *y = best_row->y;
29443 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29446 return best_glyph != NULL;
29448 #endif /* not used */
29450 /* Find the positions of the first and the last glyphs in window W's
29451 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29452 (assumed to be a string), and return in HLINFO's mouse_face_*
29453 members the pixel and column/row coordinates of those glyphs. */
29455 static void
29456 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29457 Lisp_Object object,
29458 ptrdiff_t startpos, ptrdiff_t endpos)
29460 int yb = window_text_bottom_y (w);
29461 struct glyph_row *r;
29462 struct glyph *g, *e;
29463 int gx;
29464 bool found = false;
29466 /* Find the glyph row with at least one position in the range
29467 [STARTPOS..ENDPOS), and the first glyph in that row whose
29468 position belongs to that range. */
29469 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29470 r->enabled_p && r->y < yb;
29471 ++r)
29473 if (!r->reversed_p)
29475 g = r->glyphs[TEXT_AREA];
29476 e = g + r->used[TEXT_AREA];
29477 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29478 if (EQ (g->object, object)
29479 && startpos <= g->charpos && g->charpos < endpos)
29481 hlinfo->mouse_face_beg_row
29482 = MATRIX_ROW_VPOS (r, w->current_matrix);
29483 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29484 hlinfo->mouse_face_beg_x = gx;
29485 found = true;
29486 break;
29489 else
29491 struct glyph *g1;
29493 e = r->glyphs[TEXT_AREA];
29494 g = e + r->used[TEXT_AREA];
29495 for ( ; g > e; --g)
29496 if (EQ ((g-1)->object, object)
29497 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29499 hlinfo->mouse_face_beg_row
29500 = MATRIX_ROW_VPOS (r, w->current_matrix);
29501 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29502 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29503 gx += g1->pixel_width;
29504 hlinfo->mouse_face_beg_x = gx;
29505 found = true;
29506 break;
29509 if (found)
29510 break;
29513 if (!found)
29514 return;
29516 /* Starting with the next row, look for the first row which does NOT
29517 include any glyphs whose positions are in the range. */
29518 for (++r; r->enabled_p && r->y < yb; ++r)
29520 g = r->glyphs[TEXT_AREA];
29521 e = g + r->used[TEXT_AREA];
29522 found = false;
29523 for ( ; g < e; ++g)
29524 if (EQ (g->object, object)
29525 && startpos <= g->charpos && g->charpos < endpos)
29527 found = true;
29528 break;
29530 if (!found)
29531 break;
29534 /* The highlighted region ends on the previous row. */
29535 r--;
29537 /* Set the end row. */
29538 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29540 /* Compute and set the end column and the end column's horizontal
29541 pixel coordinate. */
29542 if (!r->reversed_p)
29544 g = r->glyphs[TEXT_AREA];
29545 e = g + r->used[TEXT_AREA];
29546 for ( ; e > g; --e)
29547 if (EQ ((e-1)->object, object)
29548 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29549 break;
29550 hlinfo->mouse_face_end_col = e - g;
29552 for (gx = r->x; g < e; ++g)
29553 gx += g->pixel_width;
29554 hlinfo->mouse_face_end_x = gx;
29556 else
29558 e = r->glyphs[TEXT_AREA];
29559 g = e + r->used[TEXT_AREA];
29560 for (gx = r->x ; e < g; ++e)
29562 if (EQ (e->object, object)
29563 && startpos <= e->charpos && e->charpos < endpos)
29564 break;
29565 gx += e->pixel_width;
29567 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29568 hlinfo->mouse_face_end_x = gx;
29572 #ifdef HAVE_WINDOW_SYSTEM
29574 /* See if position X, Y is within a hot-spot of an image. */
29576 static bool
29577 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29579 if (!CONSP (hot_spot))
29580 return false;
29582 if (EQ (XCAR (hot_spot), Qrect))
29584 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29585 Lisp_Object rect = XCDR (hot_spot);
29586 Lisp_Object tem;
29587 if (!CONSP (rect))
29588 return false;
29589 if (!CONSP (XCAR (rect)))
29590 return false;
29591 if (!CONSP (XCDR (rect)))
29592 return false;
29593 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29594 return false;
29595 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29596 return false;
29597 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29598 return false;
29599 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29600 return false;
29601 return true;
29603 else if (EQ (XCAR (hot_spot), Qcircle))
29605 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29606 Lisp_Object circ = XCDR (hot_spot);
29607 Lisp_Object lr, lx0, ly0;
29608 if (CONSP (circ)
29609 && CONSP (XCAR (circ))
29610 && (lr = XCDR (circ), NUMBERP (lr))
29611 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29612 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29614 double r = XFLOATINT (lr);
29615 double dx = XINT (lx0) - x;
29616 double dy = XINT (ly0) - y;
29617 return (dx * dx + dy * dy <= r * r);
29620 else if (EQ (XCAR (hot_spot), Qpoly))
29622 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29623 if (VECTORP (XCDR (hot_spot)))
29625 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29626 Lisp_Object *poly = v->contents;
29627 ptrdiff_t n = v->header.size;
29628 ptrdiff_t i;
29629 bool inside = false;
29630 Lisp_Object lx, ly;
29631 int x0, y0;
29633 /* Need an even number of coordinates, and at least 3 edges. */
29634 if (n < 6 || n & 1)
29635 return false;
29637 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29638 If count is odd, we are inside polygon. Pixels on edges
29639 may or may not be included depending on actual geometry of the
29640 polygon. */
29641 if ((lx = poly[n-2], !INTEGERP (lx))
29642 || (ly = poly[n-1], !INTEGERP (lx)))
29643 return false;
29644 x0 = XINT (lx), y0 = XINT (ly);
29645 for (i = 0; i < n; i += 2)
29647 int x1 = x0, y1 = y0;
29648 if ((lx = poly[i], !INTEGERP (lx))
29649 || (ly = poly[i+1], !INTEGERP (ly)))
29650 return false;
29651 x0 = XINT (lx), y0 = XINT (ly);
29653 /* Does this segment cross the X line? */
29654 if (x0 >= x)
29656 if (x1 >= x)
29657 continue;
29659 else if (x1 < x)
29660 continue;
29661 if (y > y0 && y > y1)
29662 continue;
29663 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29664 inside = !inside;
29666 return inside;
29669 return false;
29672 Lisp_Object
29673 find_hot_spot (Lisp_Object map, int x, int y)
29675 while (CONSP (map))
29677 if (CONSP (XCAR (map))
29678 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29679 return XCAR (map);
29680 map = XCDR (map);
29683 return Qnil;
29686 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29687 3, 3, 0,
29688 doc: /* Lookup in image map MAP coordinates X and Y.
29689 An image map is an alist where each element has the format (AREA ID PLIST).
29690 An AREA is specified as either a rectangle, a circle, or a polygon:
29691 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29692 pixel coordinates of the upper left and bottom right corners.
29693 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29694 and the radius of the circle; r may be a float or integer.
29695 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29696 vector describes one corner in the polygon.
29697 Returns the alist element for the first matching AREA in MAP. */)
29698 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29700 if (NILP (map))
29701 return Qnil;
29703 CHECK_NUMBER (x);
29704 CHECK_NUMBER (y);
29706 return find_hot_spot (map,
29707 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29708 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29710 #endif /* HAVE_WINDOW_SYSTEM */
29713 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29714 static void
29715 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29717 #ifdef HAVE_WINDOW_SYSTEM
29718 if (!FRAME_WINDOW_P (f))
29719 return;
29721 /* Do not change cursor shape while dragging mouse. */
29722 if (EQ (do_mouse_tracking, Qdragging))
29723 return;
29725 if (!NILP (pointer))
29727 if (EQ (pointer, Qarrow))
29728 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29729 else if (EQ (pointer, Qhand))
29730 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29731 else if (EQ (pointer, Qtext))
29732 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29733 else if (EQ (pointer, intern ("hdrag")))
29734 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29735 else if (EQ (pointer, intern ("nhdrag")))
29736 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29737 # ifdef HAVE_X_WINDOWS
29738 else if (EQ (pointer, intern ("vdrag")))
29739 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29740 # endif
29741 else if (EQ (pointer, intern ("hourglass")))
29742 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29743 else if (EQ (pointer, Qmodeline))
29744 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29745 else
29746 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29749 if (cursor != No_Cursor)
29750 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29751 #endif
29754 /* Take proper action when mouse has moved to the mode or header line
29755 or marginal area AREA of window W, x-position X and y-position Y.
29756 X is relative to the start of the text display area of W, so the
29757 width of bitmap areas and scroll bars must be subtracted to get a
29758 position relative to the start of the mode line. */
29760 static void
29761 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29762 enum window_part area)
29764 struct window *w = XWINDOW (window);
29765 struct frame *f = XFRAME (w->frame);
29766 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29767 #ifdef HAVE_WINDOW_SYSTEM
29768 Display_Info *dpyinfo;
29769 #endif
29770 Cursor cursor = No_Cursor;
29771 Lisp_Object pointer = Qnil;
29772 int dx, dy, width, height;
29773 ptrdiff_t charpos;
29774 Lisp_Object string, object = Qnil;
29775 Lisp_Object pos UNINIT;
29776 Lisp_Object mouse_face;
29777 int original_x_pixel = x;
29778 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29779 struct glyph_row *row UNINIT;
29781 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29783 int x0;
29784 struct glyph *end;
29786 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29787 returns them in row/column units! */
29788 string = mode_line_string (w, area, &x, &y, &charpos,
29789 &object, &dx, &dy, &width, &height);
29791 row = (area == ON_MODE_LINE
29792 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29793 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29795 /* Find the glyph under the mouse pointer. */
29796 if (row->mode_line_p && row->enabled_p)
29798 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29799 end = glyph + row->used[TEXT_AREA];
29801 for (x0 = original_x_pixel;
29802 glyph < end && x0 >= glyph->pixel_width;
29803 ++glyph)
29804 x0 -= glyph->pixel_width;
29806 if (glyph >= end)
29807 glyph = NULL;
29810 else
29812 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29813 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29814 returns them in row/column units! */
29815 string = marginal_area_string (w, area, &x, &y, &charpos,
29816 &object, &dx, &dy, &width, &height);
29819 Lisp_Object help = Qnil;
29821 #ifdef HAVE_WINDOW_SYSTEM
29822 if (IMAGEP (object))
29824 Lisp_Object image_map, hotspot;
29825 if ((image_map = Fplist_get (XCDR (object), QCmap),
29826 !NILP (image_map))
29827 && (hotspot = find_hot_spot (image_map, dx, dy),
29828 CONSP (hotspot))
29829 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29831 Lisp_Object plist;
29833 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29834 If so, we could look for mouse-enter, mouse-leave
29835 properties in PLIST (and do something...). */
29836 hotspot = XCDR (hotspot);
29837 if (CONSP (hotspot)
29838 && (plist = XCAR (hotspot), CONSP (plist)))
29840 pointer = Fplist_get (plist, Qpointer);
29841 if (NILP (pointer))
29842 pointer = Qhand;
29843 help = Fplist_get (plist, Qhelp_echo);
29844 if (!NILP (help))
29846 help_echo_string = help;
29847 XSETWINDOW (help_echo_window, w);
29848 help_echo_object = w->contents;
29849 help_echo_pos = charpos;
29853 if (NILP (pointer))
29854 pointer = Fplist_get (XCDR (object), QCpointer);
29856 #endif /* HAVE_WINDOW_SYSTEM */
29858 if (STRINGP (string))
29859 pos = make_number (charpos);
29861 /* Set the help text and mouse pointer. If the mouse is on a part
29862 of the mode line without any text (e.g. past the right edge of
29863 the mode line text), use the default help text and pointer. */
29864 if (STRINGP (string) || area == ON_MODE_LINE)
29866 /* Arrange to display the help by setting the global variables
29867 help_echo_string, help_echo_object, and help_echo_pos. */
29868 if (NILP (help))
29870 if (STRINGP (string))
29871 help = Fget_text_property (pos, Qhelp_echo, string);
29873 if (!NILP (help))
29875 help_echo_string = help;
29876 XSETWINDOW (help_echo_window, w);
29877 help_echo_object = string;
29878 help_echo_pos = charpos;
29880 else if (area == ON_MODE_LINE)
29882 Lisp_Object default_help
29883 = buffer_local_value (Qmode_line_default_help_echo,
29884 w->contents);
29886 if (STRINGP (default_help))
29888 help_echo_string = default_help;
29889 XSETWINDOW (help_echo_window, w);
29890 help_echo_object = Qnil;
29891 help_echo_pos = -1;
29896 #ifdef HAVE_WINDOW_SYSTEM
29897 /* Change the mouse pointer according to what is under it. */
29898 if (FRAME_WINDOW_P (f))
29900 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29901 || minibuf_level
29902 || NILP (Vresize_mini_windows));
29904 dpyinfo = FRAME_DISPLAY_INFO (f);
29905 if (STRINGP (string))
29907 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29909 if (NILP (pointer))
29910 pointer = Fget_text_property (pos, Qpointer, string);
29912 /* Change the mouse pointer according to what is under X/Y. */
29913 if (NILP (pointer)
29914 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29916 Lisp_Object map;
29917 map = Fget_text_property (pos, Qlocal_map, string);
29918 if (!KEYMAPP (map))
29919 map = Fget_text_property (pos, Qkeymap, string);
29920 if (!KEYMAPP (map) && draggable)
29921 cursor = dpyinfo->vertical_scroll_bar_cursor;
29924 else if (draggable)
29925 /* Default mode-line pointer. */
29926 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29928 #endif
29931 /* Change the mouse face according to what is under X/Y. */
29932 bool mouse_face_shown = false;
29933 if (STRINGP (string))
29935 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29936 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29937 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29938 && glyph)
29940 Lisp_Object b, e;
29942 struct glyph * tmp_glyph;
29944 int gpos;
29945 int gseq_length;
29946 int total_pixel_width;
29947 ptrdiff_t begpos, endpos, ignore;
29949 int vpos, hpos;
29951 b = Fprevious_single_property_change (make_number (charpos + 1),
29952 Qmouse_face, string, Qnil);
29953 if (NILP (b))
29954 begpos = 0;
29955 else
29956 begpos = XINT (b);
29958 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29959 if (NILP (e))
29960 endpos = SCHARS (string);
29961 else
29962 endpos = XINT (e);
29964 /* Calculate the glyph position GPOS of GLYPH in the
29965 displayed string, relative to the beginning of the
29966 highlighted part of the string.
29968 Note: GPOS is different from CHARPOS. CHARPOS is the
29969 position of GLYPH in the internal string object. A mode
29970 line string format has structures which are converted to
29971 a flattened string by the Emacs Lisp interpreter. The
29972 internal string is an element of those structures. The
29973 displayed string is the flattened string. */
29974 tmp_glyph = row_start_glyph;
29975 while (tmp_glyph < glyph
29976 && (!(EQ (tmp_glyph->object, glyph->object)
29977 && begpos <= tmp_glyph->charpos
29978 && tmp_glyph->charpos < endpos)))
29979 tmp_glyph++;
29980 gpos = glyph - tmp_glyph;
29982 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29983 the highlighted part of the displayed string to which
29984 GLYPH belongs. Note: GSEQ_LENGTH is different from
29985 SCHARS (STRING), because the latter returns the length of
29986 the internal string. */
29987 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29988 tmp_glyph > glyph
29989 && (!(EQ (tmp_glyph->object, glyph->object)
29990 && begpos <= tmp_glyph->charpos
29991 && tmp_glyph->charpos < endpos));
29992 tmp_glyph--)
29994 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29996 /* Calculate the total pixel width of all the glyphs between
29997 the beginning of the highlighted area and GLYPH. */
29998 total_pixel_width = 0;
29999 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30000 total_pixel_width += tmp_glyph->pixel_width;
30002 /* Pre calculation of re-rendering position. Note: X is in
30003 column units here, after the call to mode_line_string or
30004 marginal_area_string. */
30005 hpos = x - gpos;
30006 vpos = (area == ON_MODE_LINE
30007 ? (w->current_matrix)->nrows - 1
30008 : 0);
30010 /* If GLYPH's position is included in the region that is
30011 already drawn in mouse face, we have nothing to do. */
30012 if ( EQ (window, hlinfo->mouse_face_window)
30013 && (!row->reversed_p
30014 ? (hlinfo->mouse_face_beg_col <= hpos
30015 && hpos < hlinfo->mouse_face_end_col)
30016 /* In R2L rows we swap BEG and END, see below. */
30017 : (hlinfo->mouse_face_end_col <= hpos
30018 && hpos < hlinfo->mouse_face_beg_col))
30019 && hlinfo->mouse_face_beg_row == vpos )
30020 return;
30022 if (clear_mouse_face (hlinfo))
30023 cursor = No_Cursor;
30025 if (!row->reversed_p)
30027 hlinfo->mouse_face_beg_col = hpos;
30028 hlinfo->mouse_face_beg_x = original_x_pixel
30029 - (total_pixel_width + dx);
30030 hlinfo->mouse_face_end_col = hpos + gseq_length;
30031 hlinfo->mouse_face_end_x = 0;
30033 else
30035 /* In R2L rows, show_mouse_face expects BEG and END
30036 coordinates to be swapped. */
30037 hlinfo->mouse_face_end_col = hpos;
30038 hlinfo->mouse_face_end_x = original_x_pixel
30039 - (total_pixel_width + dx);
30040 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30041 hlinfo->mouse_face_beg_x = 0;
30044 hlinfo->mouse_face_beg_row = vpos;
30045 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30046 hlinfo->mouse_face_past_end = false;
30047 hlinfo->mouse_face_window = window;
30049 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30050 charpos,
30051 0, &ignore,
30052 glyph->face_id,
30053 true);
30054 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30055 mouse_face_shown = true;
30057 if (NILP (pointer))
30058 pointer = Qhand;
30062 /* If mouse-face doesn't need to be shown, clear any existing
30063 mouse-face. */
30064 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30065 clear_mouse_face (hlinfo);
30067 define_frame_cursor1 (f, cursor, pointer);
30071 /* EXPORT:
30072 Take proper action when the mouse has moved to position X, Y on
30073 frame F with regards to highlighting portions of display that have
30074 mouse-face properties. Also de-highlight portions of display where
30075 the mouse was before, set the mouse pointer shape as appropriate
30076 for the mouse coordinates, and activate help echo (tooltips).
30077 X and Y can be negative or out of range. */
30079 void
30080 note_mouse_highlight (struct frame *f, int x, int y)
30082 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30083 enum window_part part = ON_NOTHING;
30084 Lisp_Object window;
30085 struct window *w;
30086 Cursor cursor = No_Cursor;
30087 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30088 struct buffer *b;
30090 /* When a menu is active, don't highlight because this looks odd. */
30091 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30092 if (popup_activated ())
30093 return;
30094 #endif
30096 if (!f->glyphs_initialized_p
30097 || f->pointer_invisible)
30098 return;
30100 hlinfo->mouse_face_mouse_x = x;
30101 hlinfo->mouse_face_mouse_y = y;
30102 hlinfo->mouse_face_mouse_frame = f;
30104 if (hlinfo->mouse_face_defer)
30105 return;
30107 /* Which window is that in? */
30108 window = window_from_coordinates (f, x, y, &part, true);
30110 /* If displaying active text in another window, clear that. */
30111 if (! EQ (window, hlinfo->mouse_face_window)
30112 /* Also clear if we move out of text area in same window. */
30113 || (!NILP (hlinfo->mouse_face_window)
30114 && !NILP (window)
30115 && part != ON_TEXT
30116 && part != ON_MODE_LINE
30117 && part != ON_HEADER_LINE))
30118 clear_mouse_face (hlinfo);
30120 /* Not on a window -> return. */
30121 if (!WINDOWP (window))
30122 return;
30124 /* Reset help_echo_string. It will get recomputed below. */
30125 help_echo_string = Qnil;
30127 /* Convert to window-relative pixel coordinates. */
30128 w = XWINDOW (window);
30129 frame_to_window_pixel_xy (w, &x, &y);
30131 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30132 /* Handle tool-bar window differently since it doesn't display a
30133 buffer. */
30134 if (EQ (window, f->tool_bar_window))
30136 note_tool_bar_highlight (f, x, y);
30137 return;
30139 #endif
30141 /* Mouse is on the mode, header line or margin? */
30142 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30143 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30145 note_mode_line_or_margin_highlight (window, x, y, part);
30147 #ifdef HAVE_WINDOW_SYSTEM
30148 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30150 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30151 /* Show non-text cursor (Bug#16647). */
30152 goto set_cursor;
30154 else
30155 #endif
30156 return;
30159 #ifdef HAVE_WINDOW_SYSTEM
30160 if (part == ON_VERTICAL_BORDER)
30162 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30163 help_echo_string = build_string ("drag-mouse-1: resize");
30165 else if (part == ON_RIGHT_DIVIDER)
30167 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30168 help_echo_string = build_string ("drag-mouse-1: resize");
30170 else if (part == ON_BOTTOM_DIVIDER)
30171 if (! WINDOW_BOTTOMMOST_P (w)
30172 || minibuf_level
30173 || NILP (Vresize_mini_windows))
30175 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30176 help_echo_string = build_string ("drag-mouse-1: resize");
30178 else
30179 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30180 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30181 || part == ON_VERTICAL_SCROLL_BAR
30182 || part == ON_HORIZONTAL_SCROLL_BAR)
30183 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30184 else
30185 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30186 #endif
30188 /* Are we in a window whose display is up to date?
30189 And verify the buffer's text has not changed. */
30190 b = XBUFFER (w->contents);
30191 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30193 int hpos, vpos, dx, dy, area = LAST_AREA;
30194 ptrdiff_t pos;
30195 struct glyph *glyph;
30196 Lisp_Object object;
30197 Lisp_Object mouse_face = Qnil, position;
30198 Lisp_Object *overlay_vec = NULL;
30199 ptrdiff_t i, noverlays;
30200 struct buffer *obuf;
30201 ptrdiff_t obegv, ozv;
30202 bool same_region;
30204 /* Find the glyph under X/Y. */
30205 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30207 #ifdef HAVE_WINDOW_SYSTEM
30208 /* Look for :pointer property on image. */
30209 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30211 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30212 if (img != NULL && IMAGEP (img->spec))
30214 Lisp_Object image_map, hotspot;
30215 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30216 !NILP (image_map))
30217 && (hotspot = find_hot_spot (image_map,
30218 glyph->slice.img.x + dx,
30219 glyph->slice.img.y + dy),
30220 CONSP (hotspot))
30221 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30223 Lisp_Object plist;
30225 /* Could check XCAR (hotspot) to see if we enter/leave
30226 this hot-spot.
30227 If so, we could look for mouse-enter, mouse-leave
30228 properties in PLIST (and do something...). */
30229 hotspot = XCDR (hotspot);
30230 if (CONSP (hotspot)
30231 && (plist = XCAR (hotspot), CONSP (plist)))
30233 pointer = Fplist_get (plist, Qpointer);
30234 if (NILP (pointer))
30235 pointer = Qhand;
30236 help_echo_string = Fplist_get (plist, Qhelp_echo);
30237 if (!NILP (help_echo_string))
30239 help_echo_window = window;
30240 help_echo_object = glyph->object;
30241 help_echo_pos = glyph->charpos;
30245 if (NILP (pointer))
30246 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30249 #endif /* HAVE_WINDOW_SYSTEM */
30251 /* Clear mouse face if X/Y not over text. */
30252 if (glyph == NULL
30253 || area != TEXT_AREA
30254 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30255 /* Glyph's OBJECT is nil for glyphs inserted by the
30256 display engine for its internal purposes, like truncation
30257 and continuation glyphs and blanks beyond the end of
30258 line's text on text terminals. If we are over such a
30259 glyph, we are not over any text. */
30260 || NILP (glyph->object)
30261 /* R2L rows have a stretch glyph at their front, which
30262 stands for no text, whereas L2R rows have no glyphs at
30263 all beyond the end of text. Treat such stretch glyphs
30264 like we do with NULL glyphs in L2R rows. */
30265 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30266 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30267 && glyph->type == STRETCH_GLYPH
30268 && glyph->avoid_cursor_p))
30270 if (clear_mouse_face (hlinfo))
30271 cursor = No_Cursor;
30272 if (FRAME_WINDOW_P (f) && NILP (pointer))
30274 #ifdef HAVE_WINDOW_SYSTEM
30275 if (area != TEXT_AREA)
30276 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30277 else
30278 pointer = Vvoid_text_area_pointer;
30279 #endif
30281 goto set_cursor;
30284 pos = glyph->charpos;
30285 object = glyph->object;
30286 if (!STRINGP (object) && !BUFFERP (object))
30287 goto set_cursor;
30289 /* If we get an out-of-range value, return now; avoid an error. */
30290 if (BUFFERP (object) && pos > BUF_Z (b))
30291 goto set_cursor;
30293 /* Make the window's buffer temporarily current for
30294 overlays_at and compute_char_face. */
30295 obuf = current_buffer;
30296 current_buffer = b;
30297 obegv = BEGV;
30298 ozv = ZV;
30299 BEGV = BEG;
30300 ZV = Z;
30302 /* Is this char mouse-active or does it have help-echo? */
30303 position = make_number (pos);
30305 USE_SAFE_ALLOCA;
30307 if (BUFFERP (object))
30309 /* Put all the overlays we want in a vector in overlay_vec. */
30310 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30311 /* Sort overlays into increasing priority order. */
30312 noverlays = sort_overlays (overlay_vec, noverlays, w);
30314 else
30315 noverlays = 0;
30317 if (NILP (Vmouse_highlight))
30319 clear_mouse_face (hlinfo);
30320 goto check_help_echo;
30323 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30325 if (same_region)
30326 cursor = No_Cursor;
30328 /* Check mouse-face highlighting. */
30329 if (! same_region
30330 /* If there exists an overlay with mouse-face overlapping
30331 the one we are currently highlighting, we have to
30332 check if we enter the overlapping overlay, and then
30333 highlight only that. */
30334 || (OVERLAYP (hlinfo->mouse_face_overlay)
30335 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30337 /* Find the highest priority overlay with a mouse-face. */
30338 Lisp_Object overlay = Qnil;
30339 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30341 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30342 if (!NILP (mouse_face))
30343 overlay = overlay_vec[i];
30346 /* If we're highlighting the same overlay as before, there's
30347 no need to do that again. */
30348 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30349 goto check_help_echo;
30350 hlinfo->mouse_face_overlay = overlay;
30352 /* Clear the display of the old active region, if any. */
30353 if (clear_mouse_face (hlinfo))
30354 cursor = No_Cursor;
30356 /* If no overlay applies, get a text property. */
30357 if (NILP (overlay))
30358 mouse_face = Fget_text_property (position, Qmouse_face, object);
30360 /* Next, compute the bounds of the mouse highlighting and
30361 display it. */
30362 if (!NILP (mouse_face) && STRINGP (object))
30364 /* The mouse-highlighting comes from a display string
30365 with a mouse-face. */
30366 Lisp_Object s, e;
30367 ptrdiff_t ignore;
30369 s = Fprevious_single_property_change
30370 (make_number (pos + 1), Qmouse_face, object, Qnil);
30371 e = Fnext_single_property_change
30372 (position, Qmouse_face, object, Qnil);
30373 if (NILP (s))
30374 s = make_number (0);
30375 if (NILP (e))
30376 e = make_number (SCHARS (object));
30377 mouse_face_from_string_pos (w, hlinfo, object,
30378 XINT (s), XINT (e));
30379 hlinfo->mouse_face_past_end = false;
30380 hlinfo->mouse_face_window = window;
30381 hlinfo->mouse_face_face_id
30382 = face_at_string_position (w, object, pos, 0, &ignore,
30383 glyph->face_id, true);
30384 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30385 cursor = No_Cursor;
30387 else
30389 /* The mouse-highlighting, if any, comes from an overlay
30390 or text property in the buffer. */
30391 Lisp_Object buffer UNINIT;
30392 Lisp_Object disp_string UNINIT;
30394 if (STRINGP (object))
30396 /* If we are on a display string with no mouse-face,
30397 check if the text under it has one. */
30398 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30399 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30400 pos = string_buffer_position (object, start);
30401 if (pos > 0)
30403 mouse_face = get_char_property_and_overlay
30404 (make_number (pos), Qmouse_face, w->contents, &overlay);
30405 buffer = w->contents;
30406 disp_string = object;
30409 else
30411 buffer = object;
30412 disp_string = Qnil;
30415 if (!NILP (mouse_face))
30417 Lisp_Object before, after;
30418 Lisp_Object before_string, after_string;
30419 /* To correctly find the limits of mouse highlight
30420 in a bidi-reordered buffer, we must not use the
30421 optimization of limiting the search in
30422 previous-single-property-change and
30423 next-single-property-change, because
30424 rows_from_pos_range needs the real start and end
30425 positions to DTRT in this case. That's because
30426 the first row visible in a window does not
30427 necessarily display the character whose position
30428 is the smallest. */
30429 Lisp_Object lim1
30430 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30431 ? Fmarker_position (w->start)
30432 : Qnil;
30433 Lisp_Object lim2
30434 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30435 ? make_number (BUF_Z (XBUFFER (buffer))
30436 - w->window_end_pos)
30437 : Qnil;
30439 if (NILP (overlay))
30441 /* Handle the text property case. */
30442 before = Fprevious_single_property_change
30443 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30444 after = Fnext_single_property_change
30445 (make_number (pos), Qmouse_face, buffer, lim2);
30446 before_string = after_string = Qnil;
30448 else
30450 /* Handle the overlay case. */
30451 before = Foverlay_start (overlay);
30452 after = Foverlay_end (overlay);
30453 before_string = Foverlay_get (overlay, Qbefore_string);
30454 after_string = Foverlay_get (overlay, Qafter_string);
30456 if (!STRINGP (before_string)) before_string = Qnil;
30457 if (!STRINGP (after_string)) after_string = Qnil;
30460 mouse_face_from_buffer_pos (window, hlinfo, pos,
30461 NILP (before)
30463 : XFASTINT (before),
30464 NILP (after)
30465 ? BUF_Z (XBUFFER (buffer))
30466 : XFASTINT (after),
30467 before_string, after_string,
30468 disp_string);
30469 cursor = No_Cursor;
30474 check_help_echo:
30476 /* Look for a `help-echo' property. */
30477 if (NILP (help_echo_string)) {
30478 Lisp_Object help, overlay;
30480 /* Check overlays first. */
30481 help = overlay = Qnil;
30482 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30484 overlay = overlay_vec[i];
30485 help = Foverlay_get (overlay, Qhelp_echo);
30488 if (!NILP (help))
30490 help_echo_string = help;
30491 help_echo_window = window;
30492 help_echo_object = overlay;
30493 help_echo_pos = pos;
30495 else
30497 Lisp_Object obj = glyph->object;
30498 ptrdiff_t charpos = glyph->charpos;
30500 /* Try text properties. */
30501 if (STRINGP (obj)
30502 && charpos >= 0
30503 && charpos < SCHARS (obj))
30505 help = Fget_text_property (make_number (charpos),
30506 Qhelp_echo, obj);
30507 if (NILP (help))
30509 /* If the string itself doesn't specify a help-echo,
30510 see if the buffer text ``under'' it does. */
30511 struct glyph_row *r
30512 = MATRIX_ROW (w->current_matrix, vpos);
30513 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30514 ptrdiff_t p = string_buffer_position (obj, start);
30515 if (p > 0)
30517 help = Fget_char_property (make_number (p),
30518 Qhelp_echo, w->contents);
30519 if (!NILP (help))
30521 charpos = p;
30522 obj = w->contents;
30527 else if (BUFFERP (obj)
30528 && charpos >= BEGV
30529 && charpos < ZV)
30530 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30531 obj);
30533 if (!NILP (help))
30535 help_echo_string = help;
30536 help_echo_window = window;
30537 help_echo_object = obj;
30538 help_echo_pos = charpos;
30543 #ifdef HAVE_WINDOW_SYSTEM
30544 /* Look for a `pointer' property. */
30545 if (FRAME_WINDOW_P (f) && NILP (pointer))
30547 /* Check overlays first. */
30548 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30549 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30551 if (NILP (pointer))
30553 Lisp_Object obj = glyph->object;
30554 ptrdiff_t charpos = glyph->charpos;
30556 /* Try text properties. */
30557 if (STRINGP (obj)
30558 && charpos >= 0
30559 && charpos < SCHARS (obj))
30561 pointer = Fget_text_property (make_number (charpos),
30562 Qpointer, obj);
30563 if (NILP (pointer))
30565 /* If the string itself doesn't specify a pointer,
30566 see if the buffer text ``under'' it does. */
30567 struct glyph_row *r
30568 = MATRIX_ROW (w->current_matrix, vpos);
30569 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30570 ptrdiff_t p = string_buffer_position (obj, start);
30571 if (p > 0)
30572 pointer = Fget_char_property (make_number (p),
30573 Qpointer, w->contents);
30576 else if (BUFFERP (obj)
30577 && charpos >= BEGV
30578 && charpos < ZV)
30579 pointer = Fget_text_property (make_number (charpos),
30580 Qpointer, obj);
30583 #endif /* HAVE_WINDOW_SYSTEM */
30585 BEGV = obegv;
30586 ZV = ozv;
30587 current_buffer = obuf;
30588 SAFE_FREE ();
30591 set_cursor:
30592 define_frame_cursor1 (f, cursor, pointer);
30596 /* EXPORT for RIF:
30597 Clear any mouse-face on window W. This function is part of the
30598 redisplay interface, and is called from try_window_id and similar
30599 functions to ensure the mouse-highlight is off. */
30601 void
30602 x_clear_window_mouse_face (struct window *w)
30604 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30605 Lisp_Object window;
30607 block_input ();
30608 XSETWINDOW (window, w);
30609 if (EQ (window, hlinfo->mouse_face_window))
30610 clear_mouse_face (hlinfo);
30611 unblock_input ();
30615 /* EXPORT:
30616 Just discard the mouse face information for frame F, if any.
30617 This is used when the size of F is changed. */
30619 void
30620 cancel_mouse_face (struct frame *f)
30622 Lisp_Object window;
30623 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30625 window = hlinfo->mouse_face_window;
30626 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30627 reset_mouse_highlight (hlinfo);
30632 /***********************************************************************
30633 Exposure Events
30634 ***********************************************************************/
30636 #ifdef HAVE_WINDOW_SYSTEM
30638 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30639 which intersects rectangle R. R is in window-relative coordinates. */
30641 static void
30642 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30643 enum glyph_row_area area)
30645 struct glyph *first = row->glyphs[area];
30646 struct glyph *end = row->glyphs[area] + row->used[area];
30647 struct glyph *last;
30648 int first_x, start_x, x;
30650 if (area == TEXT_AREA && row->fill_line_p)
30651 /* If row extends face to end of line write the whole line. */
30652 draw_glyphs (w, 0, row, area,
30653 0, row->used[area],
30654 DRAW_NORMAL_TEXT, 0);
30655 else
30657 /* Set START_X to the window-relative start position for drawing glyphs of
30658 AREA. The first glyph of the text area can be partially visible.
30659 The first glyphs of other areas cannot. */
30660 start_x = window_box_left_offset (w, area);
30661 x = start_x;
30662 if (area == TEXT_AREA)
30663 x += row->x;
30665 /* Find the first glyph that must be redrawn. */
30666 while (first < end
30667 && x + first->pixel_width < r->x)
30669 x += first->pixel_width;
30670 ++first;
30673 /* Find the last one. */
30674 last = first;
30675 first_x = x;
30676 /* Use a signed int intermediate value to avoid catastrophic
30677 failures due to comparison between signed and unsigned, when
30678 x is negative (can happen for wide images that are hscrolled). */
30679 int r_end = r->x + r->width;
30680 while (last < end && x < r_end)
30682 x += last->pixel_width;
30683 ++last;
30686 /* Repaint. */
30687 if (last > first)
30688 draw_glyphs (w, first_x - start_x, row, area,
30689 first - row->glyphs[area], last - row->glyphs[area],
30690 DRAW_NORMAL_TEXT, 0);
30695 /* Redraw the parts of the glyph row ROW on window W intersecting
30696 rectangle R. R is in window-relative coordinates. Value is
30697 true if mouse-face was overwritten. */
30699 static bool
30700 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30702 eassert (row->enabled_p);
30704 if (row->mode_line_p || w->pseudo_window_p)
30705 draw_glyphs (w, 0, row, TEXT_AREA,
30706 0, row->used[TEXT_AREA],
30707 DRAW_NORMAL_TEXT, 0);
30708 else
30710 if (row->used[LEFT_MARGIN_AREA])
30711 expose_area (w, row, r, LEFT_MARGIN_AREA);
30712 if (row->used[TEXT_AREA])
30713 expose_area (w, row, r, TEXT_AREA);
30714 if (row->used[RIGHT_MARGIN_AREA])
30715 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30716 draw_row_fringe_bitmaps (w, row);
30719 return row->mouse_face_p;
30723 /* Redraw those parts of glyphs rows during expose event handling that
30724 overlap other rows. Redrawing of an exposed line writes over parts
30725 of lines overlapping that exposed line; this function fixes that.
30727 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30728 row in W's current matrix that is exposed and overlaps other rows.
30729 LAST_OVERLAPPING_ROW is the last such row. */
30731 static void
30732 expose_overlaps (struct window *w,
30733 struct glyph_row *first_overlapping_row,
30734 struct glyph_row *last_overlapping_row,
30735 XRectangle *r)
30737 struct glyph_row *row;
30739 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30740 if (row->overlapping_p)
30742 eassert (row->enabled_p && !row->mode_line_p);
30744 row->clip = r;
30745 if (row->used[LEFT_MARGIN_AREA])
30746 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30748 if (row->used[TEXT_AREA])
30749 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30751 if (row->used[RIGHT_MARGIN_AREA])
30752 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30753 row->clip = NULL;
30758 /* Return true if W's cursor intersects rectangle R. */
30760 static bool
30761 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30763 XRectangle cr, result;
30764 struct glyph *cursor_glyph;
30765 struct glyph_row *row;
30767 if (w->phys_cursor.vpos >= 0
30768 && w->phys_cursor.vpos < w->current_matrix->nrows
30769 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30770 row->enabled_p)
30771 && row->cursor_in_fringe_p)
30773 /* Cursor is in the fringe. */
30774 cr.x = window_box_right_offset (w,
30775 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30776 ? RIGHT_MARGIN_AREA
30777 : TEXT_AREA));
30778 cr.y = row->y;
30779 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30780 cr.height = row->height;
30781 return x_intersect_rectangles (&cr, r, &result);
30784 cursor_glyph = get_phys_cursor_glyph (w);
30785 if (cursor_glyph)
30787 /* r is relative to W's box, but w->phys_cursor.x is relative
30788 to left edge of W's TEXT area. Adjust it. */
30789 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30790 cr.y = w->phys_cursor.y;
30791 cr.width = cursor_glyph->pixel_width;
30792 cr.height = w->phys_cursor_height;
30793 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30794 I assume the effect is the same -- and this is portable. */
30795 return x_intersect_rectangles (&cr, r, &result);
30797 /* If we don't understand the format, pretend we're not in the hot-spot. */
30798 return false;
30802 /* EXPORT:
30803 Draw a vertical window border to the right of window W if W doesn't
30804 have vertical scroll bars. */
30806 void
30807 x_draw_vertical_border (struct window *w)
30809 struct frame *f = XFRAME (WINDOW_FRAME (w));
30811 /* We could do better, if we knew what type of scroll-bar the adjacent
30812 windows (on either side) have... But we don't :-(
30813 However, I think this works ok. ++KFS 2003-04-25 */
30815 /* Redraw borders between horizontally adjacent windows. Don't
30816 do it for frames with vertical scroll bars because either the
30817 right scroll bar of a window, or the left scroll bar of its
30818 neighbor will suffice as a border. */
30819 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30820 return;
30822 /* Note: It is necessary to redraw both the left and the right
30823 borders, for when only this single window W is being
30824 redisplayed. */
30825 if (!WINDOW_RIGHTMOST_P (w)
30826 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30828 int x0, x1, y0, y1;
30830 window_box_edges (w, &x0, &y0, &x1, &y1);
30831 y1 -= 1;
30833 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30834 x1 -= 1;
30836 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30839 if (!WINDOW_LEFTMOST_P (w)
30840 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30842 int x0, x1, y0, y1;
30844 window_box_edges (w, &x0, &y0, &x1, &y1);
30845 y1 -= 1;
30847 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30848 x0 -= 1;
30850 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30855 /* Draw window dividers for window W. */
30857 void
30858 x_draw_right_divider (struct window *w)
30860 struct frame *f = WINDOW_XFRAME (w);
30862 if (w->mini || w->pseudo_window_p)
30863 return;
30864 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30866 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30867 int x1 = WINDOW_RIGHT_EDGE_X (w);
30868 int y0 = WINDOW_TOP_EDGE_Y (w);
30869 /* The bottom divider prevails. */
30870 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30872 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30876 static void
30877 x_draw_bottom_divider (struct window *w)
30879 struct frame *f = XFRAME (WINDOW_FRAME (w));
30881 if (w->mini || w->pseudo_window_p)
30882 return;
30883 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30885 int x0 = WINDOW_LEFT_EDGE_X (w);
30886 int x1 = WINDOW_RIGHT_EDGE_X (w);
30887 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30888 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30890 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30894 /* Redraw the part of window W intersection rectangle FR. Pixel
30895 coordinates in FR are frame-relative. Call this function with
30896 input blocked. Value is true if the exposure overwrites
30897 mouse-face. */
30899 static bool
30900 expose_window (struct window *w, XRectangle *fr)
30902 struct frame *f = XFRAME (w->frame);
30903 XRectangle wr, r;
30904 bool mouse_face_overwritten_p = false;
30906 /* If window is not yet fully initialized, do nothing. This can
30907 happen when toolkit scroll bars are used and a window is split.
30908 Reconfiguring the scroll bar will generate an expose for a newly
30909 created window. */
30910 if (w->current_matrix == NULL)
30911 return false;
30913 /* When we're currently updating the window, display and current
30914 matrix usually don't agree. Arrange for a thorough display
30915 later. */
30916 if (w->must_be_updated_p)
30918 SET_FRAME_GARBAGED (f);
30919 return false;
30922 /* Frame-relative pixel rectangle of W. */
30923 wr.x = WINDOW_LEFT_EDGE_X (w);
30924 wr.y = WINDOW_TOP_EDGE_Y (w);
30925 wr.width = WINDOW_PIXEL_WIDTH (w);
30926 wr.height = WINDOW_PIXEL_HEIGHT (w);
30928 if (x_intersect_rectangles (fr, &wr, &r))
30930 int yb = window_text_bottom_y (w);
30931 struct glyph_row *row;
30932 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30934 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30935 r.x, r.y, r.width, r.height));
30937 /* Convert to window coordinates. */
30938 r.x -= WINDOW_LEFT_EDGE_X (w);
30939 r.y -= WINDOW_TOP_EDGE_Y (w);
30941 /* Turn off the cursor. */
30942 bool cursor_cleared_p = (!w->pseudo_window_p
30943 && phys_cursor_in_rect_p (w, &r));
30944 if (cursor_cleared_p)
30945 x_clear_cursor (w);
30947 /* If the row containing the cursor extends face to end of line,
30948 then expose_area might overwrite the cursor outside the
30949 rectangle and thus notice_overwritten_cursor might clear
30950 w->phys_cursor_on_p. We remember the original value and
30951 check later if it is changed. */
30952 bool phys_cursor_on_p = w->phys_cursor_on_p;
30954 /* Use a signed int intermediate value to avoid catastrophic
30955 failures due to comparison between signed and unsigned, when
30956 y0 or y1 is negative (can happen for tall images). */
30957 int r_bottom = r.y + r.height;
30959 /* Update lines intersecting rectangle R. */
30960 first_overlapping_row = last_overlapping_row = NULL;
30961 for (row = w->current_matrix->rows;
30962 row->enabled_p;
30963 ++row)
30965 int y0 = row->y;
30966 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30968 if ((y0 >= r.y && y0 < r_bottom)
30969 || (y1 > r.y && y1 < r_bottom)
30970 || (r.y >= y0 && r.y < y1)
30971 || (r_bottom > y0 && r_bottom < y1))
30973 /* A header line may be overlapping, but there is no need
30974 to fix overlapping areas for them. KFS 2005-02-12 */
30975 if (row->overlapping_p && !row->mode_line_p)
30977 if (first_overlapping_row == NULL)
30978 first_overlapping_row = row;
30979 last_overlapping_row = row;
30982 row->clip = fr;
30983 if (expose_line (w, row, &r))
30984 mouse_face_overwritten_p = true;
30985 row->clip = NULL;
30987 else if (row->overlapping_p)
30989 /* We must redraw a row overlapping the exposed area. */
30990 if (y0 < r.y
30991 ? y0 + row->phys_height > r.y
30992 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30994 if (first_overlapping_row == NULL)
30995 first_overlapping_row = row;
30996 last_overlapping_row = row;
31000 if (y1 >= yb)
31001 break;
31004 /* Display the mode line if there is one. */
31005 if (WINDOW_WANTS_MODELINE_P (w)
31006 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31007 row->enabled_p)
31008 && row->y < r_bottom)
31010 if (expose_line (w, row, &r))
31011 mouse_face_overwritten_p = true;
31014 if (!w->pseudo_window_p)
31016 /* Fix the display of overlapping rows. */
31017 if (first_overlapping_row)
31018 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31019 fr);
31021 /* Draw border between windows. */
31022 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31023 x_draw_right_divider (w);
31024 else
31025 x_draw_vertical_border (w);
31027 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31028 x_draw_bottom_divider (w);
31030 /* Turn the cursor on again. */
31031 if (cursor_cleared_p
31032 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31033 update_window_cursor (w, true);
31037 return mouse_face_overwritten_p;
31042 /* Redraw (parts) of all windows in the window tree rooted at W that
31043 intersect R. R contains frame pixel coordinates. Value is
31044 true if the exposure overwrites mouse-face. */
31046 static bool
31047 expose_window_tree (struct window *w, XRectangle *r)
31049 struct frame *f = XFRAME (w->frame);
31050 bool mouse_face_overwritten_p = false;
31052 while (w && !FRAME_GARBAGED_P (f))
31054 mouse_face_overwritten_p
31055 |= (WINDOWP (w->contents)
31056 ? expose_window_tree (XWINDOW (w->contents), r)
31057 : expose_window (w, r));
31059 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31062 return mouse_face_overwritten_p;
31066 /* EXPORT:
31067 Redisplay an exposed area of frame F. X and Y are the upper-left
31068 corner of the exposed rectangle. W and H are width and height of
31069 the exposed area. All are pixel values. W or H zero means redraw
31070 the entire frame. */
31072 void
31073 expose_frame (struct frame *f, int x, int y, int w, int h)
31075 XRectangle r;
31076 bool mouse_face_overwritten_p = false;
31078 TRACE ((stderr, "expose_frame "));
31080 /* No need to redraw if frame will be redrawn soon. */
31081 if (FRAME_GARBAGED_P (f))
31083 TRACE ((stderr, " garbaged\n"));
31084 return;
31087 /* If basic faces haven't been realized yet, there is no point in
31088 trying to redraw anything. This can happen when we get an expose
31089 event while Emacs is starting, e.g. by moving another window. */
31090 if (FRAME_FACE_CACHE (f) == NULL
31091 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31093 TRACE ((stderr, " no faces\n"));
31094 return;
31097 if (w == 0 || h == 0)
31099 r.x = r.y = 0;
31100 r.width = FRAME_TEXT_WIDTH (f);
31101 r.height = FRAME_TEXT_HEIGHT (f);
31103 else
31105 r.x = x;
31106 r.y = y;
31107 r.width = w;
31108 r.height = h;
31111 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31112 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31114 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31115 if (WINDOWP (f->tool_bar_window))
31116 mouse_face_overwritten_p
31117 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31118 #endif
31120 #ifdef HAVE_X_WINDOWS
31121 #ifndef MSDOS
31122 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31123 if (WINDOWP (f->menu_bar_window))
31124 mouse_face_overwritten_p
31125 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31126 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31127 #endif
31128 #endif
31130 /* Some window managers support a focus-follows-mouse style with
31131 delayed raising of frames. Imagine a partially obscured frame,
31132 and moving the mouse into partially obscured mouse-face on that
31133 frame. The visible part of the mouse-face will be highlighted,
31134 then the WM raises the obscured frame. With at least one WM, KDE
31135 2.1, Emacs is not getting any event for the raising of the frame
31136 (even tried with SubstructureRedirectMask), only Expose events.
31137 These expose events will draw text normally, i.e. not
31138 highlighted. Which means we must redo the highlight here.
31139 Subsume it under ``we love X''. --gerd 2001-08-15 */
31140 /* Included in Windows version because Windows most likely does not
31141 do the right thing if any third party tool offers
31142 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31143 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31145 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31146 if (f == hlinfo->mouse_face_mouse_frame)
31148 int mouse_x = hlinfo->mouse_face_mouse_x;
31149 int mouse_y = hlinfo->mouse_face_mouse_y;
31150 clear_mouse_face (hlinfo);
31151 note_mouse_highlight (f, mouse_x, mouse_y);
31157 /* EXPORT:
31158 Determine the intersection of two rectangles R1 and R2. Return
31159 the intersection in *RESULT. Value is true if RESULT is not
31160 empty. */
31162 bool
31163 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31165 XRectangle *left, *right;
31166 XRectangle *upper, *lower;
31167 bool intersection_p = false;
31169 /* Rearrange so that R1 is the left-most rectangle. */
31170 if (r1->x < r2->x)
31171 left = r1, right = r2;
31172 else
31173 left = r2, right = r1;
31175 /* X0 of the intersection is right.x0, if this is inside R1,
31176 otherwise there is no intersection. */
31177 if (right->x <= left->x + left->width)
31179 result->x = right->x;
31181 /* The right end of the intersection is the minimum of
31182 the right ends of left and right. */
31183 result->width = (min (left->x + left->width, right->x + right->width)
31184 - result->x);
31186 /* Same game for Y. */
31187 if (r1->y < r2->y)
31188 upper = r1, lower = r2;
31189 else
31190 upper = r2, lower = r1;
31192 /* The upper end of the intersection is lower.y0, if this is inside
31193 of upper. Otherwise, there is no intersection. */
31194 if (lower->y <= upper->y + upper->height)
31196 result->y = lower->y;
31198 /* The lower end of the intersection is the minimum of the lower
31199 ends of upper and lower. */
31200 result->height = (min (lower->y + lower->height,
31201 upper->y + upper->height)
31202 - result->y);
31203 intersection_p = true;
31207 return intersection_p;
31210 #endif /* HAVE_WINDOW_SYSTEM */
31213 /***********************************************************************
31214 Initialization
31215 ***********************************************************************/
31217 void
31218 syms_of_xdisp (void)
31220 Vwith_echo_area_save_vector = Qnil;
31221 staticpro (&Vwith_echo_area_save_vector);
31223 Vmessage_stack = Qnil;
31224 staticpro (&Vmessage_stack);
31226 /* Non-nil means don't actually do any redisplay. */
31227 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31229 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31231 DEFVAR_BOOL("inhibit-message", inhibit_message,
31232 doc: /* Non-nil means calls to `message' are not displayed.
31233 They are still logged to the *Messages* buffer. */);
31234 inhibit_message = 0;
31236 message_dolog_marker1 = Fmake_marker ();
31237 staticpro (&message_dolog_marker1);
31238 message_dolog_marker2 = Fmake_marker ();
31239 staticpro (&message_dolog_marker2);
31240 message_dolog_marker3 = Fmake_marker ();
31241 staticpro (&message_dolog_marker3);
31243 #ifdef GLYPH_DEBUG
31244 defsubr (&Sdump_frame_glyph_matrix);
31245 defsubr (&Sdump_glyph_matrix);
31246 defsubr (&Sdump_glyph_row);
31247 defsubr (&Sdump_tool_bar_row);
31248 defsubr (&Strace_redisplay);
31249 defsubr (&Strace_to_stderr);
31250 #endif
31251 #ifdef HAVE_WINDOW_SYSTEM
31252 defsubr (&Stool_bar_height);
31253 defsubr (&Slookup_image_map);
31254 #endif
31255 defsubr (&Sline_pixel_height);
31256 defsubr (&Sformat_mode_line);
31257 defsubr (&Sinvisible_p);
31258 defsubr (&Scurrent_bidi_paragraph_direction);
31259 defsubr (&Swindow_text_pixel_size);
31260 defsubr (&Smove_point_visually);
31261 defsubr (&Sbidi_find_overridden_directionality);
31263 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31264 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31265 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31266 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31267 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31268 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31269 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31270 DEFSYM (Qeval, "eval");
31271 DEFSYM (QCdata, ":data");
31273 /* Names of text properties relevant for redisplay. */
31274 DEFSYM (Qdisplay, "display");
31275 DEFSYM (Qspace_width, "space-width");
31276 DEFSYM (Qraise, "raise");
31277 DEFSYM (Qslice, "slice");
31278 DEFSYM (Qspace, "space");
31279 DEFSYM (Qmargin, "margin");
31280 DEFSYM (Qpointer, "pointer");
31281 DEFSYM (Qleft_margin, "left-margin");
31282 DEFSYM (Qright_margin, "right-margin");
31283 DEFSYM (Qcenter, "center");
31284 DEFSYM (Qline_height, "line-height");
31285 DEFSYM (QCalign_to, ":align-to");
31286 DEFSYM (QCrelative_width, ":relative-width");
31287 DEFSYM (QCrelative_height, ":relative-height");
31288 DEFSYM (QCeval, ":eval");
31289 DEFSYM (QCpropertize, ":propertize");
31290 DEFSYM (QCfile, ":file");
31291 DEFSYM (Qfontified, "fontified");
31292 DEFSYM (Qfontification_functions, "fontification-functions");
31294 /* Name of the face used to highlight trailing whitespace. */
31295 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31297 /* Name and number of the face used to highlight escape glyphs. */
31298 DEFSYM (Qescape_glyph, "escape-glyph");
31300 /* Name and number of the face used to highlight non-breaking
31301 spaces/hyphens. */
31302 DEFSYM (Qnobreak_space, "nobreak-space");
31303 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31305 /* The symbol 'image' which is the car of the lists used to represent
31306 images in Lisp. Also a tool bar style. */
31307 DEFSYM (Qimage, "image");
31309 /* Tool bar styles. */
31310 DEFSYM (Qtext, "text");
31311 DEFSYM (Qboth, "both");
31312 DEFSYM (Qboth_horiz, "both-horiz");
31313 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31315 /* The image map types. */
31316 DEFSYM (QCmap, ":map");
31317 DEFSYM (QCpointer, ":pointer");
31318 DEFSYM (Qrect, "rect");
31319 DEFSYM (Qcircle, "circle");
31320 DEFSYM (Qpoly, "poly");
31322 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31324 DEFSYM (Qgrow_only, "grow-only");
31325 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31326 DEFSYM (Qposition, "position");
31327 DEFSYM (Qbuffer_position, "buffer-position");
31328 DEFSYM (Qobject, "object");
31330 /* Cursor shapes. */
31331 DEFSYM (Qbar, "bar");
31332 DEFSYM (Qhbar, "hbar");
31333 DEFSYM (Qbox, "box");
31334 DEFSYM (Qhollow, "hollow");
31336 /* Pointer shapes. */
31337 DEFSYM (Qhand, "hand");
31338 DEFSYM (Qarrow, "arrow");
31339 /* also Qtext */
31341 DEFSYM (Qdragging, "dragging");
31343 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31345 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31346 staticpro (&list_of_error);
31348 /* Values of those variables at last redisplay are stored as
31349 properties on 'overlay-arrow-position' symbol. However, if
31350 Voverlay_arrow_position is a marker, last-arrow-position is its
31351 numerical position. */
31352 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31353 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31355 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31356 properties on a symbol in overlay-arrow-variable-list. */
31357 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31358 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31360 echo_buffer[0] = echo_buffer[1] = Qnil;
31361 staticpro (&echo_buffer[0]);
31362 staticpro (&echo_buffer[1]);
31364 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31365 staticpro (&echo_area_buffer[0]);
31366 staticpro (&echo_area_buffer[1]);
31368 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31369 staticpro (&Vmessages_buffer_name);
31371 mode_line_proptrans_alist = Qnil;
31372 staticpro (&mode_line_proptrans_alist);
31373 mode_line_string_list = Qnil;
31374 staticpro (&mode_line_string_list);
31375 mode_line_string_face = Qnil;
31376 staticpro (&mode_line_string_face);
31377 mode_line_string_face_prop = Qnil;
31378 staticpro (&mode_line_string_face_prop);
31379 Vmode_line_unwind_vector = Qnil;
31380 staticpro (&Vmode_line_unwind_vector);
31382 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31384 help_echo_string = Qnil;
31385 staticpro (&help_echo_string);
31386 help_echo_object = Qnil;
31387 staticpro (&help_echo_object);
31388 help_echo_window = Qnil;
31389 staticpro (&help_echo_window);
31390 previous_help_echo_string = Qnil;
31391 staticpro (&previous_help_echo_string);
31392 help_echo_pos = -1;
31394 DEFSYM (Qright_to_left, "right-to-left");
31395 DEFSYM (Qleft_to_right, "left-to-right");
31396 defsubr (&Sbidi_resolved_levels);
31398 #ifdef HAVE_WINDOW_SYSTEM
31399 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31400 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31401 For example, if a block cursor is over a tab, it will be drawn as
31402 wide as that tab on the display. */);
31403 x_stretch_cursor_p = 0;
31404 #endif
31406 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31407 doc: /* Non-nil means highlight trailing whitespace.
31408 The face used for trailing whitespace is `trailing-whitespace'. */);
31409 Vshow_trailing_whitespace = Qnil;
31411 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31412 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31413 If the value is t, Emacs highlights non-ASCII chars which have the
31414 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31415 or `nobreak-hyphen' face respectively.
31417 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31418 U+2011 (non-breaking hyphen) are affected.
31420 Any other non-nil value means to display these characters as a escape
31421 glyph followed by an ordinary space or hyphen.
31423 A value of nil means no special handling of these characters. */);
31424 Vnobreak_char_display = Qt;
31426 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31427 doc: /* The pointer shape to show in void text areas.
31428 A value of nil means to show the text pointer. Other options are
31429 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31430 `hourglass'. */);
31431 Vvoid_text_area_pointer = Qarrow;
31433 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31434 doc: /* Non-nil means don't actually do any redisplay.
31435 This is used for internal purposes. */);
31436 Vinhibit_redisplay = Qnil;
31438 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31439 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31440 Vglobal_mode_string = Qnil;
31442 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31443 doc: /* Marker for where to display an arrow on top of the buffer text.
31444 This must be the beginning of a line in order to work.
31445 See also `overlay-arrow-string'. */);
31446 Voverlay_arrow_position = Qnil;
31448 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31449 doc: /* String to display as an arrow in non-window frames.
31450 See also `overlay-arrow-position'. */);
31451 Voverlay_arrow_string = build_pure_c_string ("=>");
31453 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31454 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31455 The symbols on this list are examined during redisplay to determine
31456 where to display overlay arrows. */);
31457 Voverlay_arrow_variable_list
31458 = list1 (intern_c_string ("overlay-arrow-position"));
31460 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31461 doc: /* The number of lines to try scrolling a window by when point moves out.
31462 If that fails to bring point back on frame, point is centered instead.
31463 If this is zero, point is always centered after it moves off frame.
31464 If you want scrolling to always be a line at a time, you should set
31465 `scroll-conservatively' to a large value rather than set this to 1. */);
31467 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31468 doc: /* Scroll up to this many lines, to bring point back on screen.
31469 If point moves off-screen, redisplay will scroll by up to
31470 `scroll-conservatively' lines in order to bring point just barely
31471 onto the screen again. If that cannot be done, then redisplay
31472 recenters point as usual.
31474 If the value is greater than 100, redisplay will never recenter point,
31475 but will always scroll just enough text to bring point into view, even
31476 if you move far away.
31478 A value of zero means always recenter point if it moves off screen. */);
31479 scroll_conservatively = 0;
31481 DEFVAR_INT ("scroll-margin", scroll_margin,
31482 doc: /* Number of lines of margin at the top and bottom of a window.
31483 Recenter the window whenever point gets within this many lines
31484 of the top or bottom of the window. */);
31485 scroll_margin = 0;
31487 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31488 doc: /* Pixels per inch value for non-window system displays.
31489 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31490 Vdisplay_pixels_per_inch = make_float (72.0);
31492 #ifdef GLYPH_DEBUG
31493 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31494 #endif
31496 DEFVAR_LISP ("truncate-partial-width-windows",
31497 Vtruncate_partial_width_windows,
31498 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31499 For an integer value, truncate lines in each window narrower than the
31500 full frame width, provided the total window width in column units is less
31501 than that integer; otherwise, respect the value of `truncate-lines'.
31502 The total width of the window is as returned by `window-total-width', it
31503 includes the fringes, the continuation and truncation glyphs, the
31504 display margins (if any), and the scroll bar
31506 For any other non-nil value, truncate lines in all windows that do
31507 not span the full frame width.
31509 A value of nil means to respect the value of `truncate-lines'.
31511 If `word-wrap' is enabled, you might want to reduce this. */);
31512 Vtruncate_partial_width_windows = make_number (50);
31514 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31515 doc: /* Maximum buffer size for which line number should be displayed.
31516 If the buffer is bigger than this, the line number does not appear
31517 in the mode line. A value of nil means no limit. */);
31518 Vline_number_display_limit = Qnil;
31520 DEFVAR_INT ("line-number-display-limit-width",
31521 line_number_display_limit_width,
31522 doc: /* Maximum line width (in characters) for line number display.
31523 If the average length of the lines near point is bigger than this, then the
31524 line number may be omitted from the mode line. */);
31525 line_number_display_limit_width = 200;
31527 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31528 doc: /* Non-nil means highlight region even in nonselected windows. */);
31529 highlight_nonselected_windows = false;
31531 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31532 doc: /* Non-nil if more than one frame is visible on this display.
31533 Minibuffer-only frames don't count, but iconified frames do.
31534 This variable is not guaranteed to be accurate except while processing
31535 `frame-title-format' and `icon-title-format'. */);
31537 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31538 doc: /* Template for displaying the title bar of visible frames.
31539 \(Assuming the window manager supports this feature.)
31541 This variable has the same structure as `mode-line-format', except that
31542 the %c and %l constructs are ignored. It is used only on frames for
31543 which no explicit name has been set (see `modify-frame-parameters'). */);
31545 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31546 doc: /* Template for displaying the title bar of an iconified frame.
31547 \(Assuming the window manager supports this feature.)
31548 This variable has the same structure as `mode-line-format' (which see),
31549 and is used only on frames for which no explicit name has been set
31550 \(see `modify-frame-parameters'). */);
31551 Vicon_title_format
31552 = Vframe_title_format
31553 = listn (CONSTYPE_PURE, 3,
31554 intern_c_string ("multiple-frames"),
31555 build_pure_c_string ("%b"),
31556 listn (CONSTYPE_PURE, 4,
31557 empty_unibyte_string,
31558 intern_c_string ("invocation-name"),
31559 build_pure_c_string ("@"),
31560 intern_c_string ("system-name")));
31562 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31563 doc: /* Maximum number of lines to keep in the message log buffer.
31564 If nil, disable message logging. If t, log messages but don't truncate
31565 the buffer when it becomes large. */);
31566 Vmessage_log_max = make_number (1000);
31568 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31569 doc: /* List of functions to call before redisplaying a window with scrolling.
31570 Each function is called with two arguments, the window and its new
31571 display-start position.
31572 These functions are called whenever the `window-start' marker is modified,
31573 either to point into another buffer (e.g. via `set-window-buffer') or another
31574 place in the same buffer.
31575 Note that the value of `window-end' is not valid when these functions are
31576 called.
31578 Warning: Do not use this feature to alter the way the window
31579 is scrolled. It is not designed for that, and such use probably won't
31580 work. */);
31581 Vwindow_scroll_functions = Qnil;
31583 DEFVAR_LISP ("window-text-change-functions",
31584 Vwindow_text_change_functions,
31585 doc: /* Functions to call in redisplay when text in the window might change. */);
31586 Vwindow_text_change_functions = Qnil;
31588 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31589 doc: /* Functions called when redisplay of a window reaches the end trigger.
31590 Each function is called with two arguments, the window and the end trigger value.
31591 See `set-window-redisplay-end-trigger'. */);
31592 Vredisplay_end_trigger_functions = Qnil;
31594 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31595 doc: /* Non-nil means autoselect window with mouse pointer.
31596 If nil, do not autoselect windows.
31597 A positive number means delay autoselection by that many seconds: a
31598 window is autoselected only after the mouse has remained in that
31599 window for the duration of the delay.
31600 A negative number has a similar effect, but causes windows to be
31601 autoselected only after the mouse has stopped moving. (Because of
31602 the way Emacs compares mouse events, you will occasionally wait twice
31603 that time before the window gets selected.)
31604 Any other value means to autoselect window instantaneously when the
31605 mouse pointer enters it.
31607 Autoselection selects the minibuffer only if it is active, and never
31608 unselects the minibuffer if it is active.
31610 When customizing this variable make sure that the actual value of
31611 `focus-follows-mouse' matches the behavior of your window manager. */);
31612 Vmouse_autoselect_window = Qnil;
31614 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31615 doc: /* Non-nil means automatically resize tool-bars.
31616 This dynamically changes the tool-bar's height to the minimum height
31617 that is needed to make all tool-bar items visible.
31618 If value is `grow-only', the tool-bar's height is only increased
31619 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31620 Vauto_resize_tool_bars = Qt;
31622 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31623 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31624 auto_raise_tool_bar_buttons_p = true;
31626 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31627 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31628 make_cursor_line_fully_visible_p = true;
31630 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31631 doc: /* Border below tool-bar in pixels.
31632 If an integer, use it as the height of the border.
31633 If it is one of `internal-border-width' or `border-width', use the
31634 value of the corresponding frame parameter.
31635 Otherwise, no border is added below the tool-bar. */);
31636 Vtool_bar_border = Qinternal_border_width;
31638 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31639 doc: /* Margin around tool-bar buttons in pixels.
31640 If an integer, use that for both horizontal and vertical margins.
31641 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31642 HORZ specifying the horizontal margin, and VERT specifying the
31643 vertical margin. */);
31644 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31646 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31647 doc: /* Relief thickness of tool-bar buttons. */);
31648 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31650 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31651 doc: /* Tool bar style to use.
31652 It can be one of
31653 image - show images only
31654 text - show text only
31655 both - show both, text below image
31656 both-horiz - show text to the right of the image
31657 text-image-horiz - show text to the left of the image
31658 any other - use system default or image if no system default.
31660 This variable only affects the GTK+ toolkit version of Emacs. */);
31661 Vtool_bar_style = Qnil;
31663 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31664 doc: /* Maximum number of characters a label can have to be shown.
31665 The tool bar style must also show labels for this to have any effect, see
31666 `tool-bar-style'. */);
31667 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31669 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31670 doc: /* List of functions to call to fontify regions of text.
31671 Each function is called with one argument POS. Functions must
31672 fontify a region starting at POS in the current buffer, and give
31673 fontified regions the property `fontified'. */);
31674 Vfontification_functions = Qnil;
31675 Fmake_variable_buffer_local (Qfontification_functions);
31677 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31678 unibyte_display_via_language_environment,
31679 doc: /* Non-nil means display unibyte text according to language environment.
31680 Specifically, this means that raw bytes in the range 160-255 decimal
31681 are displayed by converting them to the equivalent multibyte characters
31682 according to the current language environment. As a result, they are
31683 displayed according to the current fontset.
31685 Note that this variable affects only how these bytes are displayed,
31686 but does not change the fact they are interpreted as raw bytes. */);
31687 unibyte_display_via_language_environment = false;
31689 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31690 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31691 If a float, it specifies a fraction of the mini-window frame's height.
31692 If an integer, it specifies a number of lines. */);
31693 Vmax_mini_window_height = make_float (0.25);
31695 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31696 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31697 A value of nil means don't automatically resize mini-windows.
31698 A value of t means resize them to fit the text displayed in them.
31699 A value of `grow-only', the default, means let mini-windows grow only;
31700 they return to their normal size when the minibuffer is closed, or the
31701 echo area becomes empty. */);
31702 /* Contrary to the doc string, we initialize this to nil, so that
31703 loading loadup.el won't try to resize windows before loading
31704 window.el, where some functions we need to call for this live.
31705 We assign the 'grow-only' value right after loading window.el
31706 during loadup. */
31707 Vresize_mini_windows = Qnil;
31709 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31710 doc: /* Alist specifying how to blink the cursor off.
31711 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31712 `cursor-type' frame-parameter or variable equals ON-STATE,
31713 comparing using `equal', Emacs uses OFF-STATE to specify
31714 how to blink it off. ON-STATE and OFF-STATE are values for
31715 the `cursor-type' frame parameter.
31717 If a frame's ON-STATE has no entry in this list,
31718 the frame's other specifications determine how to blink the cursor off. */);
31719 Vblink_cursor_alist = Qnil;
31721 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31722 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31723 If non-nil, windows are automatically scrolled horizontally to make
31724 point visible. */);
31725 automatic_hscrolling_p = true;
31726 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31728 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31729 doc: /* How many columns away from the window edge point is allowed to get
31730 before automatic hscrolling will horizontally scroll the window. */);
31731 hscroll_margin = 5;
31733 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31734 doc: /* How many columns to scroll the window when point gets too close to the edge.
31735 When point is less than `hscroll-margin' columns from the window
31736 edge, automatic hscrolling will scroll the window by the amount of columns
31737 determined by this variable. If its value is a positive integer, scroll that
31738 many columns. If it's a positive floating-point number, it specifies the
31739 fraction of the window's width to scroll. If it's nil or zero, point will be
31740 centered horizontally after the scroll. Any other value, including negative
31741 numbers, are treated as if the value were zero.
31743 Automatic hscrolling always moves point outside the scroll margin, so if
31744 point was more than scroll step columns inside the margin, the window will
31745 scroll more than the value given by the scroll step.
31747 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31748 and `scroll-right' overrides this variable's effect. */);
31749 Vhscroll_step = make_number (0);
31751 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31752 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31753 Bind this around calls to `message' to let it take effect. */);
31754 message_truncate_lines = false;
31756 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31757 doc: /* Normal hook run to update the menu bar definitions.
31758 Redisplay runs this hook before it redisplays the menu bar.
31759 This is used to update menus such as Buffers, whose contents depend on
31760 various data. */);
31761 Vmenu_bar_update_hook = Qnil;
31763 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31764 doc: /* Frame for which we are updating a menu.
31765 The enable predicate for a menu binding should check this variable. */);
31766 Vmenu_updating_frame = Qnil;
31768 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31769 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31770 inhibit_menubar_update = false;
31772 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31773 doc: /* Prefix prepended to all continuation lines at display time.
31774 The value may be a string, an image, or a stretch-glyph; it is
31775 interpreted in the same way as the value of a `display' text property.
31777 This variable is overridden by any `wrap-prefix' text or overlay
31778 property.
31780 To add a prefix to non-continuation lines, use `line-prefix'. */);
31781 Vwrap_prefix = Qnil;
31782 DEFSYM (Qwrap_prefix, "wrap-prefix");
31783 Fmake_variable_buffer_local (Qwrap_prefix);
31785 DEFVAR_LISP ("line-prefix", Vline_prefix,
31786 doc: /* Prefix prepended to all non-continuation lines at display time.
31787 The value may be a string, an image, or a stretch-glyph; it is
31788 interpreted in the same way as the value of a `display' text property.
31790 This variable is overridden by any `line-prefix' text or overlay
31791 property.
31793 To add a prefix to continuation lines, use `wrap-prefix'. */);
31794 Vline_prefix = Qnil;
31795 DEFSYM (Qline_prefix, "line-prefix");
31796 Fmake_variable_buffer_local (Qline_prefix);
31798 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31799 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31800 inhibit_eval_during_redisplay = false;
31802 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31803 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31804 inhibit_free_realized_faces = false;
31806 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31807 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31808 Intended for use during debugging and for testing bidi display;
31809 see biditest.el in the test suite. */);
31810 inhibit_bidi_mirroring = false;
31812 #ifdef GLYPH_DEBUG
31813 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31814 doc: /* Inhibit try_window_id display optimization. */);
31815 inhibit_try_window_id = false;
31817 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31818 doc: /* Inhibit try_window_reusing display optimization. */);
31819 inhibit_try_window_reusing = false;
31821 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31822 doc: /* Inhibit try_cursor_movement display optimization. */);
31823 inhibit_try_cursor_movement = false;
31824 #endif /* GLYPH_DEBUG */
31826 DEFVAR_INT ("overline-margin", overline_margin,
31827 doc: /* Space between overline and text, in pixels.
31828 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31829 margin to the character height. */);
31830 overline_margin = 2;
31832 DEFVAR_INT ("underline-minimum-offset",
31833 underline_minimum_offset,
31834 doc: /* Minimum distance between baseline and underline.
31835 This can improve legibility of underlined text at small font sizes,
31836 particularly when using variable `x-use-underline-position-properties'
31837 with fonts that specify an UNDERLINE_POSITION relatively close to the
31838 baseline. The default value is 1. */);
31839 underline_minimum_offset = 1;
31841 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31842 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31843 This feature only works when on a window system that can change
31844 cursor shapes. */);
31845 display_hourglass_p = true;
31847 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31848 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31849 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31851 #ifdef HAVE_WINDOW_SYSTEM
31852 hourglass_atimer = NULL;
31853 hourglass_shown_p = false;
31854 #endif /* HAVE_WINDOW_SYSTEM */
31856 /* Name of the face used to display glyphless characters. */
31857 DEFSYM (Qglyphless_char, "glyphless-char");
31859 /* Method symbols for Vglyphless_char_display. */
31860 DEFSYM (Qhex_code, "hex-code");
31861 DEFSYM (Qempty_box, "empty-box");
31862 DEFSYM (Qthin_space, "thin-space");
31863 DEFSYM (Qzero_width, "zero-width");
31865 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31866 doc: /* Function run just before redisplay.
31867 It is called with one argument, which is the set of windows that are to
31868 be redisplayed. This set can be nil (meaning, only the selected window),
31869 or t (meaning all windows). */);
31870 Vpre_redisplay_function = intern ("ignore");
31872 /* Symbol for the purpose of Vglyphless_char_display. */
31873 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31874 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31876 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31877 doc: /* Char-table defining glyphless characters.
31878 Each element, if non-nil, should be one of the following:
31879 an ASCII acronym string: display this string in a box
31880 `hex-code': display the hexadecimal code of a character in a box
31881 `empty-box': display as an empty box
31882 `thin-space': display as 1-pixel width space
31883 `zero-width': don't display
31884 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31885 display method for graphical terminals and text terminals respectively.
31886 GRAPHICAL and TEXT should each have one of the values listed above.
31888 The char-table has one extra slot to control the display of a character for
31889 which no font is found. This slot only takes effect on graphical terminals.
31890 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31891 `thin-space'. The default is `empty-box'.
31893 If a character has a non-nil entry in an active display table, the
31894 display table takes effect; in this case, Emacs does not consult
31895 `glyphless-char-display' at all. */);
31896 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31897 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31898 Qempty_box);
31900 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31901 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31902 Vdebug_on_message = Qnil;
31904 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31905 doc: /* */);
31906 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31908 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31909 doc: /* */);
31910 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31912 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31913 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31914 Vredisplay__variables = Qnil;
31916 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31917 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31918 /* Initialize to t, since we need to disable reordering until
31919 loadup.el successfully loads charprop.el. */
31920 redisplay__inhibit_bidi = true;
31924 /* Initialize this module when Emacs starts. */
31926 void
31927 init_xdisp (void)
31929 CHARPOS (this_line_start_pos) = 0;
31931 if (!noninteractive)
31933 struct window *m = XWINDOW (minibuf_window);
31934 Lisp_Object frame = m->frame;
31935 struct frame *f = XFRAME (frame);
31936 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31937 struct window *r = XWINDOW (root);
31938 int i;
31940 echo_area_window = minibuf_window;
31942 r->top_line = FRAME_TOP_MARGIN (f);
31943 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31944 r->total_cols = FRAME_COLS (f);
31945 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31946 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31947 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31949 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31950 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31951 m->total_cols = FRAME_COLS (f);
31952 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31953 m->total_lines = 1;
31954 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31956 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31957 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31958 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31960 /* The default ellipsis glyphs `...'. */
31961 for (i = 0; i < 3; ++i)
31962 default_invis_vector[i] = make_number ('.');
31966 /* Allocate the buffer for frame titles.
31967 Also used for `format-mode-line'. */
31968 int size = 100;
31969 mode_line_noprop_buf = xmalloc (size);
31970 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31971 mode_line_noprop_ptr = mode_line_noprop_buf;
31972 mode_line_target = MODE_LINE_DISPLAY;
31975 help_echo_showing_p = false;
31978 #ifdef HAVE_WINDOW_SYSTEM
31980 /* Platform-independent portion of hourglass implementation. */
31982 /* Timer function of hourglass_atimer. */
31984 static void
31985 show_hourglass (struct atimer *timer)
31987 /* The timer implementation will cancel this timer automatically
31988 after this function has run. Set hourglass_atimer to null
31989 so that we know the timer doesn't have to be canceled. */
31990 hourglass_atimer = NULL;
31992 if (!hourglass_shown_p)
31994 Lisp_Object tail, frame;
31996 block_input ();
31998 FOR_EACH_FRAME (tail, frame)
32000 struct frame *f = XFRAME (frame);
32002 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32003 && FRAME_RIF (f)->show_hourglass)
32004 FRAME_RIF (f)->show_hourglass (f);
32007 hourglass_shown_p = true;
32008 unblock_input ();
32012 /* Cancel a currently active hourglass timer, and start a new one. */
32014 void
32015 start_hourglass (void)
32017 struct timespec delay;
32019 cancel_hourglass ();
32021 if (INTEGERP (Vhourglass_delay)
32022 && XINT (Vhourglass_delay) > 0)
32023 delay = make_timespec (min (XINT (Vhourglass_delay),
32024 TYPE_MAXIMUM (time_t)),
32026 else if (FLOATP (Vhourglass_delay)
32027 && XFLOAT_DATA (Vhourglass_delay) > 0)
32028 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32029 else
32030 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32032 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32033 show_hourglass, NULL);
32036 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32037 shown. */
32039 void
32040 cancel_hourglass (void)
32042 if (hourglass_atimer)
32044 cancel_atimer (hourglass_atimer);
32045 hourglass_atimer = NULL;
32048 if (hourglass_shown_p)
32050 Lisp_Object tail, frame;
32052 block_input ();
32054 FOR_EACH_FRAME (tail, frame)
32056 struct frame *f = XFRAME (frame);
32058 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32059 && FRAME_RIF (f)->hide_hourglass)
32060 FRAME_RIF (f)->hide_hourglass (f);
32061 #ifdef HAVE_NTGUI
32062 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32063 else if (!FRAME_W32_P (f))
32064 w32_arrow_cursor ();
32065 #endif
32068 hourglass_shown_p = false;
32069 unblock_input ();
32073 #endif /* HAVE_WINDOW_SYSTEM */