Some further improvements for tramp-gvfs.el
[emacs.git] / src / xdisp.c
blob34ee877e6be84dc7dda5889a971d2b0ba9dad67b
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2017 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <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, %C, and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
626 Sset_buffer_redisplay, 4, 4, 0,
627 doc: /* Mark the current buffer for redisplay.
628 This function may be passed to `add-variable-watcher'. */)
629 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
633 return Qnil;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void block_buffer_flips (void);
822 static void unblock_buffer_flips (void);
823 static void redisplay_windows (Lisp_Object);
824 static void redisplay_window (Lisp_Object, bool);
825 static Lisp_Object redisplay_window_error (Lisp_Object);
826 static Lisp_Object redisplay_window_0 (Lisp_Object);
827 static Lisp_Object redisplay_window_1 (Lisp_Object);
828 static bool set_cursor_from_row (struct window *, struct glyph_row *,
829 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
830 int, int);
831 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
832 static bool update_menu_bar (struct frame *, bool, bool);
833 static bool try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static bool display_line (struct it *, int);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
839 Lisp_Object, bool);
840 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
841 Lisp_Object);
842 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
843 static void display_menu_bar (struct window *);
844 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
845 ptrdiff_t *);
846 static int display_string (const char *, Lisp_Object, Lisp_Object,
847 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
848 static void compute_line_metrics (struct it *);
849 static void run_redisplay_end_trigger_hook (struct it *);
850 static bool get_overlay_strings (struct it *, ptrdiff_t);
851 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
852 static void next_overlay_string (struct it *);
853 static void reseat (struct it *, struct text_pos, bool);
854 static void reseat_1 (struct it *, struct text_pos, bool);
855 static bool next_element_from_display_vector (struct it *);
856 static bool next_element_from_string (struct it *);
857 static bool next_element_from_c_string (struct it *);
858 static bool next_element_from_buffer (struct it *);
859 static bool next_element_from_composition (struct it *);
860 static bool next_element_from_image (struct it *);
861 static bool next_element_from_stretch (struct it *);
862 static bool next_element_from_xwidget (struct it *);
863 static void load_overlay_strings (struct it *, ptrdiff_t);
864 static bool get_next_display_element (struct it *);
865 static enum move_it_result
866 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
867 enum move_operation_enum);
868 static void get_visually_first_element (struct it *);
869 static void compute_stop_pos (struct it *);
870 static int face_before_or_after_it_pos (struct it *, bool);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, bool);
877 static int underlying_face_id (struct it *);
879 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
880 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
882 #ifdef HAVE_WINDOW_SYSTEM
884 static void update_tool_bar (struct frame *, bool);
885 static void x_draw_bottom_divider (struct window *w);
886 static void notice_overwritten_cursor (struct window *,
887 enum glyph_row_area,
888 int, int, int, int);
889 static int normal_char_height (struct font *, int);
890 static void normal_char_ascent_descent (struct font *, int, int *, int *);
892 static void append_stretch_glyph (struct it *, Lisp_Object,
893 int, int, int);
895 static Lisp_Object get_it_property (struct it *, Lisp_Object);
896 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
897 struct font *, int, bool);
899 #endif /* HAVE_WINDOW_SYSTEM */
901 static void produce_special_glyphs (struct it *, enum display_element_type);
902 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
903 static bool coords_in_mouse_face_p (struct window *, int, int);
907 /***********************************************************************
908 Window display dimensions
909 ***********************************************************************/
911 /* Return the bottom boundary y-position for text lines in window W.
912 This is the first y position at which a line cannot start.
913 It is relative to the top of the window.
915 This is the height of W minus the height of a mode line, if any. */
918 window_text_bottom_y (struct window *w)
920 int height = WINDOW_PIXEL_HEIGHT (w);
922 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
927 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
929 return height;
932 /* Return the pixel width of display area AREA of window W.
933 ANY_AREA means return the total width of W, not including
934 fringes to the left and right of the window. */
937 window_box_width (struct window *w, enum glyph_row_area area)
939 int width = w->pixel_width;
941 if (!w->pseudo_window_p)
943 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
944 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
946 if (area == TEXT_AREA)
947 width -= (WINDOW_MARGINS_WIDTH (w)
948 + WINDOW_FRINGES_WIDTH (w));
949 else if (area == LEFT_MARGIN_AREA)
950 width = WINDOW_LEFT_MARGIN_WIDTH (w);
951 else if (area == RIGHT_MARGIN_AREA)
952 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
955 /* With wide margins, fringes, etc. we might end up with a negative
956 width, correct that here. */
957 return max (0, width);
961 /* Return the pixel height of the display area of window W, not
962 including mode lines of W, if any. */
965 window_box_height (struct window *w)
967 struct frame *f = XFRAME (w->frame);
968 int height = WINDOW_PIXEL_HEIGHT (w);
970 eassert (height >= 0);
972 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
973 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
975 /* Note: the code below that determines the mode-line/header-line
976 height is essentially the same as that contained in the macro
977 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
978 the appropriate glyph row has its `mode_line_p' flag set,
979 and if it doesn't, uses estimate_mode_line_height instead. */
981 if (WINDOW_WANTS_MODELINE_P (w))
983 struct glyph_row *ml_row
984 = (w->current_matrix && w->current_matrix->rows
985 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
986 : 0);
987 if (ml_row && ml_row->mode_line_p)
988 height -= ml_row->height;
989 else
990 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
993 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 struct glyph_row *hl_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
998 : 0);
999 if (hl_row && hl_row->mode_line_p)
1000 height -= hl_row->height;
1001 else
1002 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1005 /* With a very small font and a mode-line that's taller than
1006 default, we might end up with a negative height. */
1007 return max (0, height);
1010 /* Return the window-relative coordinate of the left edge of display
1011 area AREA of window W. ANY_AREA means return the left edge of the
1012 whole window, to the right of the left fringe of W. */
1015 window_box_left_offset (struct window *w, enum glyph_row_area area)
1017 int x;
1019 if (w->pseudo_window_p)
1020 return 0;
1022 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1024 if (area == TEXT_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA));
1027 else if (area == RIGHT_MARGIN_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA)
1030 + window_box_width (w, TEXT_AREA)
1031 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1033 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1034 else if (area == LEFT_MARGIN_AREA
1035 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1038 /* Don't return more than the window's pixel width. */
1039 return min (x, w->pixel_width);
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. ANY_AREA means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1047 static int
1048 window_box_right_offset (struct window *w, enum glyph_row_area area)
1050 /* Don't return more than the window's pixel width. */
1051 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1052 w->pixel_width);
1055 /* Return the frame-relative coordinate of the left edge of display
1056 area AREA of window W. ANY_AREA means return the left edge of the
1057 whole window, to the right of the left fringe of W. */
1060 window_box_left (struct window *w, enum glyph_row_area area)
1062 struct frame *f = XFRAME (w->frame);
1063 int x;
1065 if (w->pseudo_window_p)
1066 return FRAME_INTERNAL_BORDER_WIDTH (f);
1068 x = (WINDOW_LEFT_EDGE_X (w)
1069 + window_box_left_offset (w, area));
1071 return x;
1075 /* Return the frame-relative coordinate of the right edge of display
1076 area AREA of window W. ANY_AREA means return the right edge of the
1077 whole window, to the left of the right fringe of W. */
1080 window_box_right (struct window *w, enum glyph_row_area area)
1082 return window_box_left (w, area) + window_box_width (w, area);
1085 /* Get the bounding box of the display area AREA of window W, without
1086 mode lines, in frame-relative coordinates. ANY_AREA means the
1087 whole window, not including the left and right fringes of
1088 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1089 coordinates of the upper-left corner of the box. Return in
1090 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1092 void
1093 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1094 int *box_y, int *box_width, int *box_height)
1096 if (box_width)
1097 *box_width = window_box_width (w, area);
1098 if (box_height)
1099 *box_height = window_box_height (w);
1100 if (box_x)
1101 *box_x = window_box_left (w, area);
1102 if (box_y)
1104 *box_y = WINDOW_TOP_EDGE_Y (w);
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1110 #ifdef HAVE_WINDOW_SYSTEM
1112 /* Get the bounding box of the display area AREA of window W, without
1113 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1114 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1115 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1116 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1117 box. */
1119 static void
1120 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1121 int *bottom_right_x, int *bottom_right_y)
1123 window_box (w, ANY_AREA, top_left_x, top_left_y,
1124 bottom_right_x, bottom_right_y);
1125 *bottom_right_x += *top_left_x;
1126 *bottom_right_y += *top_left_y;
1129 #endif /* HAVE_WINDOW_SYSTEM */
1131 /***********************************************************************
1132 Utilities
1133 ***********************************************************************/
1135 /* Return the bottom y-position of the line the iterator IT is in.
1136 This can modify IT's settings. */
1139 line_bottom_y (struct it *it)
1141 int line_height = it->max_ascent + it->max_descent;
1142 int line_top_y = it->current_y;
1144 if (line_height == 0)
1146 if (last_height)
1147 line_height = last_height;
1148 else if (IT_CHARPOS (*it) < ZV)
1150 move_it_by_lines (it, 1);
1151 line_height = (it->max_ascent || it->max_descent
1152 ? it->max_ascent + it->max_descent
1153 : last_height);
1155 else
1157 struct glyph_row *row = it->glyph_row;
1159 /* Use the default character height. */
1160 it->glyph_row = NULL;
1161 it->what = IT_CHARACTER;
1162 it->c = ' ';
1163 it->len = 1;
1164 PRODUCE_GLYPHS (it);
1165 line_height = it->ascent + it->descent;
1166 it->glyph_row = row;
1170 return line_top_y + line_height;
1173 DEFUN ("line-pixel-height", Fline_pixel_height,
1174 Sline_pixel_height, 0, 0, 0,
1175 doc: /* Return height in pixels of text line in the selected window.
1177 Value is the height in pixels of the line at point. */)
1178 (void)
1180 struct it it;
1181 struct text_pos pt;
1182 struct window *w = XWINDOW (selected_window);
1183 struct buffer *old_buffer = NULL;
1184 Lisp_Object result;
1186 if (XBUFFER (w->contents) != current_buffer)
1188 old_buffer = current_buffer;
1189 set_buffer_internal_1 (XBUFFER (w->contents));
1191 SET_TEXT_POS (pt, PT, PT_BYTE);
1192 start_display (&it, w, pt);
1193 it.vpos = it.current_y = 0;
1194 last_height = 0;
1195 result = make_number (line_bottom_y (&it));
1196 if (old_buffer)
1197 set_buffer_internal_1 (old_buffer);
1199 return result;
1202 /* Return the default pixel height of text lines in window W. The
1203 value is the canonical height of the W frame's default font, plus
1204 any extra space required by the line-spacing variable or frame
1205 parameter.
1207 Implementation note: this ignores any line-spacing text properties
1208 put on the newline characters. This is because those properties
1209 only affect the _screen_ line ending in the newline (i.e., in a
1210 continued line, only the last screen line will be affected), which
1211 means only a small number of lines in a buffer can ever use this
1212 feature. Since this function is used to compute the default pixel
1213 equivalent of text lines in a window, we can safely ignore those
1214 few lines. For the same reasons, we ignore the line-height
1215 properties. */
1217 default_line_pixel_height (struct window *w)
1219 struct frame *f = WINDOW_XFRAME (w);
1220 int height = FRAME_LINE_HEIGHT (f);
1222 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1224 struct buffer *b = XBUFFER (w->contents);
1225 Lisp_Object val = BVAR (b, extra_line_spacing);
1227 if (NILP (val))
1228 val = BVAR (&buffer_defaults, extra_line_spacing);
1229 if (!NILP (val))
1231 if (RANGED_INTEGERP (0, val, INT_MAX))
1232 height += XFASTINT (val);
1233 else if (FLOATP (val))
1235 int addon = XFLOAT_DATA (val) * height + 0.5;
1237 if (addon >= 0)
1238 height += addon;
1241 else
1242 height += f->extra_line_spacing;
1245 return height;
1248 /* Subroutine of pos_visible_p below. Extracts a display string, if
1249 any, from the display spec given as its argument. */
1250 static Lisp_Object
1251 string_from_display_spec (Lisp_Object spec)
1253 if (VECTORP (spec))
1255 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1256 if (STRINGP (AREF (spec, i)))
1257 return AREF (spec, i);
1259 else
1261 for (; CONSP (spec); spec = XCDR (spec))
1262 if (STRINGP (XCAR (spec)))
1263 return XCAR (spec);
1265 return spec;
1269 /* Limit insanely large values of W->hscroll on frame F to the largest
1270 value that will still prevent first_visible_x and last_visible_x of
1271 'struct it' from overflowing an int. */
1272 static int
1273 window_hscroll_limited (struct window *w, struct frame *f)
1275 ptrdiff_t window_hscroll = w->hscroll;
1276 int window_text_width = window_box_width (w, TEXT_AREA);
1277 int colwidth = FRAME_COLUMN_WIDTH (f);
1279 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1280 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1282 return window_hscroll;
1285 /* Return true if position CHARPOS is visible in window W.
1286 CHARPOS < 0 means return info about WINDOW_END position.
1287 If visible, set *X and *Y to pixel coordinates of top left corner.
1288 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1289 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1291 bool
1292 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1293 int *rtop, int *rbot, int *rowh, int *vpos)
1295 struct it it;
1296 void *itdata = bidi_shelve_cache ();
1297 struct text_pos top;
1298 bool visible_p = false;
1299 struct buffer *old_buffer = NULL;
1300 bool r2l = false;
1302 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1303 return visible_p;
1305 if (XBUFFER (w->contents) != current_buffer)
1307 old_buffer = current_buffer;
1308 set_buffer_internal_1 (XBUFFER (w->contents));
1311 SET_TEXT_POS_FROM_MARKER (top, w->start);
1312 /* Scrolling a minibuffer window via scroll bar when the echo area
1313 shows long text sometimes resets the minibuffer contents behind
1314 our backs. Also, someone might narrow-to-region and immediately
1315 call a scroll function. */
1316 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1317 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1319 /* If the top of the window is after CHARPOS, the latter is surely
1320 not visible. */
1321 if (charpos >= 0 && CHARPOS (top) > charpos)
1322 return visible_p;
1324 /* Compute exact mode line heights. */
1325 if (WINDOW_WANTS_MODELINE_P (w))
1326 w->mode_line_height
1327 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1328 BVAR (current_buffer, mode_line_format));
1330 if (WINDOW_WANTS_HEADER_LINE_P (w))
1331 w->header_line_height
1332 = display_mode_line (w, HEADER_LINE_FACE_ID,
1333 BVAR (current_buffer, header_line_format));
1335 start_display (&it, w, top);
1336 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1337 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1339 if (charpos >= 0
1340 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1341 && IT_CHARPOS (it) >= charpos)
1342 /* When scanning backwards under bidi iteration, move_it_to
1343 stops at or _before_ CHARPOS, because it stops at or to
1344 the _right_ of the character at CHARPOS. */
1345 || (it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) <= charpos)))
1348 /* We have reached CHARPOS, or passed it. How the call to
1349 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1350 or covered by a display property, move_it_to stops at the end
1351 of the invisible text, to the right of CHARPOS. (ii) If
1352 CHARPOS is in a display vector, move_it_to stops on its last
1353 glyph. */
1354 int top_x = it.current_x;
1355 int top_y = it.current_y;
1356 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1357 int bottom_y;
1358 struct it save_it;
1359 void *save_it_data = NULL;
1361 /* Calling line_bottom_y may change it.method, it.position, etc. */
1362 SAVE_IT (save_it, it, save_it_data);
1363 last_height = 0;
1364 bottom_y = line_bottom_y (&it);
1365 if (top_y < window_top_y)
1366 visible_p = bottom_y > window_top_y;
1367 else if (top_y < it.last_visible_y)
1368 visible_p = true;
1369 if (bottom_y >= it.last_visible_y
1370 && it.bidi_p && it.bidi_it.scan_dir == -1
1371 && IT_CHARPOS (it) < charpos)
1373 /* When the last line of the window is scanned backwards
1374 under bidi iteration, we could be duped into thinking
1375 that we have passed CHARPOS, when in fact move_it_to
1376 simply stopped short of CHARPOS because it reached
1377 last_visible_y. To see if that's what happened, we call
1378 move_it_to again with a slightly larger vertical limit,
1379 and see if it actually moved vertically; if it did, we
1380 didn't really reach CHARPOS, which is beyond window end. */
1381 /* Why 10? because we don't know how many canonical lines
1382 will the height of the next line(s) be. So we guess. */
1383 int ten_more_lines = 10 * default_line_pixel_height (w);
1385 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1386 MOVE_TO_POS | MOVE_TO_Y);
1387 if (it.current_y > top_y)
1388 visible_p = false;
1391 RESTORE_IT (&it, &save_it, save_it_data);
1392 if (visible_p)
1394 if (it.method == GET_FROM_DISPLAY_VECTOR)
1396 /* We stopped on the last glyph of a display vector.
1397 Try and recompute. Hack alert! */
1398 if (charpos < 2 || top.charpos >= charpos)
1399 top_x = it.glyph_row->x;
1400 else
1402 struct it it2, it2_prev;
1403 /* The idea is to get to the previous buffer
1404 position, consume the character there, and use
1405 the pixel coordinates we get after that. But if
1406 the previous buffer position is also displayed
1407 from a display vector, we need to consume all of
1408 the glyphs from that display vector. */
1409 start_display (&it2, w, top);
1410 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1411 /* If we didn't get to CHARPOS - 1, there's some
1412 replacing display property at that position, and
1413 we stopped after it. That is exactly the place
1414 whose coordinates we want. */
1415 if (IT_CHARPOS (it2) != charpos - 1)
1416 it2_prev = it2;
1417 else
1419 /* Iterate until we get out of the display
1420 vector that displays the character at
1421 CHARPOS - 1. */
1422 do {
1423 get_next_display_element (&it2);
1424 PRODUCE_GLYPHS (&it2);
1425 it2_prev = it2;
1426 set_iterator_to_next (&it2, true);
1427 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1428 && IT_CHARPOS (it2) < charpos);
1430 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1431 || it2_prev.current_x > it2_prev.last_visible_x)
1432 top_x = it.glyph_row->x;
1433 else
1435 top_x = it2_prev.current_x;
1436 top_y = it2_prev.current_y;
1440 else if (IT_CHARPOS (it) != charpos)
1442 Lisp_Object cpos = make_number (charpos);
1443 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1444 Lisp_Object string = string_from_display_spec (spec);
1445 struct text_pos tpos;
1446 bool newline_in_string
1447 = (STRINGP (string)
1448 && memchr (SDATA (string), '\n', SBYTES (string)));
1450 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1451 bool replacing_spec_p
1452 = (!NILP (spec)
1453 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1454 charpos, FRAME_WINDOW_P (it.f)));
1455 /* The tricky code below is needed because there's a
1456 discrepancy between move_it_to and how we set cursor
1457 when PT is at the beginning of a portion of text
1458 covered by a display property or an overlay with a
1459 display property, or the display line ends in a
1460 newline from a display string. move_it_to will stop
1461 _after_ such display strings, whereas
1462 set_cursor_from_row conspires with cursor_row_p to
1463 place the cursor on the first glyph produced from the
1464 display string. */
1466 /* We have overshoot PT because it is covered by a
1467 display property that replaces the text it covers.
1468 If the string includes embedded newlines, we are also
1469 in the wrong display line. Backtrack to the correct
1470 line, where the display property begins. */
1471 if (replacing_spec_p)
1473 Lisp_Object startpos, endpos;
1474 EMACS_INT start, end;
1475 struct it it3;
1477 /* Find the first and the last buffer positions
1478 covered by the display string. */
1479 endpos =
1480 Fnext_single_char_property_change (cpos, Qdisplay,
1481 Qnil, Qnil);
1482 startpos =
1483 Fprevious_single_char_property_change (endpos, Qdisplay,
1484 Qnil, Qnil);
1485 start = XFASTINT (startpos);
1486 end = XFASTINT (endpos);
1487 /* Move to the last buffer position before the
1488 display property. */
1489 start_display (&it3, w, top);
1490 if (start > CHARPOS (top))
1491 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1492 /* Move forward one more line if the position before
1493 the display string is a newline or if it is the
1494 rightmost character on a line that is
1495 continued or word-wrapped. */
1496 if (it3.method == GET_FROM_BUFFER
1497 && (it3.c == '\n'
1498 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1499 move_it_by_lines (&it3, 1);
1500 else if (move_it_in_display_line_to (&it3, -1,
1501 it3.current_x
1502 + it3.pixel_width,
1503 MOVE_TO_X)
1504 == MOVE_LINE_CONTINUED)
1506 move_it_by_lines (&it3, 1);
1507 /* When we are under word-wrap, the #$@%!
1508 move_it_by_lines moves 2 lines, so we need to
1509 fix that up. */
1510 if (it3.line_wrap == WORD_WRAP)
1511 move_it_by_lines (&it3, -1);
1514 /* Record the vertical coordinate of the display
1515 line where we wound up. */
1516 top_y = it3.current_y;
1517 if (it3.bidi_p)
1519 /* When characters are reordered for display,
1520 the character displayed to the left of the
1521 display string could be _after_ the display
1522 property in the logical order. Use the
1523 smallest vertical position of these two. */
1524 start_display (&it3, w, top);
1525 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1526 if (it3.current_y < top_y)
1527 top_y = it3.current_y;
1529 /* Move from the top of the window to the beginning
1530 of the display line where the display string
1531 begins. */
1532 start_display (&it3, w, top);
1533 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1534 /* If it3_moved stays false after the 'while' loop
1535 below, that means we already were at a newline
1536 before the loop (e.g., the display string begins
1537 with a newline), so we don't need to (and cannot)
1538 inspect the glyphs of it3.glyph_row, because
1539 PRODUCE_GLYPHS will not produce anything for a
1540 newline, and thus it3.glyph_row stays at its
1541 stale content it got at top of the window. */
1542 bool it3_moved = false;
1543 /* Finally, advance the iterator until we hit the
1544 first display element whose character position is
1545 CHARPOS, or until the first newline from the
1546 display string, which signals the end of the
1547 display line. */
1548 while (get_next_display_element (&it3))
1550 PRODUCE_GLYPHS (&it3);
1551 if (IT_CHARPOS (it3) == charpos
1552 || ITERATOR_AT_END_OF_LINE_P (&it3))
1553 break;
1554 it3_moved = true;
1555 set_iterator_to_next (&it3, false);
1557 top_x = it3.current_x - it3.pixel_width;
1558 /* Normally, we would exit the above loop because we
1559 found the display element whose character
1560 position is CHARPOS. For the contingency that we
1561 didn't, and stopped at the first newline from the
1562 display string, move back over the glyphs
1563 produced from the string, until we find the
1564 rightmost glyph not from the string. */
1565 if (it3_moved
1566 && newline_in_string
1567 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1569 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1570 + it3.glyph_row->used[TEXT_AREA];
1572 while (EQ ((g - 1)->object, string))
1574 --g;
1575 top_x -= g->pixel_width;
1577 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1578 + it3.glyph_row->used[TEXT_AREA]);
1583 *x = top_x;
1584 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1585 *rtop = max (0, window_top_y - top_y);
1586 *rbot = max (0, bottom_y - it.last_visible_y);
1587 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1588 - max (top_y, window_top_y)));
1589 *vpos = it.vpos;
1590 if (it.bidi_it.paragraph_dir == R2L)
1591 r2l = true;
1594 else
1596 /* Either we were asked to provide info about WINDOW_END, or
1597 CHARPOS is in the partially visible glyph row at end of
1598 window. */
1599 struct it it2;
1600 void *it2data = NULL;
1602 SAVE_IT (it2, it, it2data);
1603 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1604 move_it_by_lines (&it, 1);
1605 if (charpos < IT_CHARPOS (it)
1606 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1608 visible_p = true;
1609 RESTORE_IT (&it2, &it2, it2data);
1610 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1611 *x = it2.current_x;
1612 *y = it2.current_y + it2.max_ascent - it2.ascent;
1613 *rtop = max (0, -it2.current_y);
1614 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1615 - it.last_visible_y));
1616 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1617 it.last_visible_y)
1618 - max (it2.current_y,
1619 WINDOW_HEADER_LINE_HEIGHT (w))));
1620 *vpos = it2.vpos;
1621 if (it2.bidi_it.paragraph_dir == R2L)
1622 r2l = true;
1624 else
1625 bidi_unshelve_cache (it2data, true);
1627 bidi_unshelve_cache (itdata, false);
1629 if (old_buffer)
1630 set_buffer_internal_1 (old_buffer);
1632 if (visible_p)
1634 if (w->hscroll > 0)
1635 *x -=
1636 window_hscroll_limited (w, WINDOW_XFRAME (w))
1637 * WINDOW_FRAME_COLUMN_WIDTH (w);
1638 /* For lines in an R2L paragraph, we need to mirror the X pixel
1639 coordinate wrt the text area. For the reasons, see the
1640 commentary in buffer_posn_from_coords and the explanation of
1641 the geometry used by the move_it_* functions at the end of
1642 the large commentary near the beginning of this file. */
1643 if (r2l)
1644 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1647 #if false
1648 /* Debugging code. */
1649 if (visible_p)
1650 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1651 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1652 else
1653 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1654 #endif
1656 return visible_p;
1660 /* Return the next character from STR. Return in *LEN the length of
1661 the character. This is like STRING_CHAR_AND_LENGTH but never
1662 returns an invalid character. If we find one, we return a `?', but
1663 with the length of the invalid character. */
1665 static int
1666 string_char_and_length (const unsigned char *str, int *len)
1668 int c;
1670 c = STRING_CHAR_AND_LENGTH (str, *len);
1671 if (!CHAR_VALID_P (c))
1672 /* We may not change the length here because other places in Emacs
1673 don't use this function, i.e. they silently accept invalid
1674 characters. */
1675 c = '?';
1677 return c;
1682 /* Given a position POS containing a valid character and byte position
1683 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1685 static struct text_pos
1686 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1688 eassert (STRINGP (string) && nchars >= 0);
1690 if (STRING_MULTIBYTE (string))
1692 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1693 int len;
1695 while (nchars--)
1697 string_char_and_length (p, &len);
1698 p += len;
1699 CHARPOS (pos) += 1;
1700 BYTEPOS (pos) += len;
1703 else
1704 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1706 return pos;
1710 /* Value is the text position, i.e. character and byte position,
1711 for character position CHARPOS in STRING. */
1713 static struct text_pos
1714 string_pos (ptrdiff_t charpos, Lisp_Object string)
1716 struct text_pos pos;
1717 eassert (STRINGP (string));
1718 eassert (charpos >= 0);
1719 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1720 return pos;
1724 /* Value is a text position, i.e. character and byte position, for
1725 character position CHARPOS in C string S. MULTIBYTE_P
1726 means recognize multibyte characters. */
1728 static struct text_pos
1729 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1731 struct text_pos pos;
1733 eassert (s != NULL);
1734 eassert (charpos >= 0);
1736 if (multibyte_p)
1738 int len;
1740 SET_TEXT_POS (pos, 0, 0);
1741 while (charpos--)
1743 string_char_and_length ((const unsigned char *) s, &len);
1744 s += len;
1745 CHARPOS (pos) += 1;
1746 BYTEPOS (pos) += len;
1749 else
1750 SET_TEXT_POS (pos, charpos, charpos);
1752 return pos;
1756 /* Value is the number of characters in C string S. MULTIBYTE_P
1757 means recognize multibyte characters. */
1759 static ptrdiff_t
1760 number_of_chars (const char *s, bool multibyte_p)
1762 ptrdiff_t nchars;
1764 if (multibyte_p)
1766 ptrdiff_t rest = strlen (s);
1767 int len;
1768 const unsigned char *p = (const unsigned char *) s;
1770 for (nchars = 0; rest > 0; ++nchars)
1772 string_char_and_length (p, &len);
1773 rest -= len, p += len;
1776 else
1777 nchars = strlen (s);
1779 return nchars;
1783 /* Compute byte position NEWPOS->bytepos corresponding to
1784 NEWPOS->charpos. POS is a known position in string STRING.
1785 NEWPOS->charpos must be >= POS.charpos. */
1787 static void
1788 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1790 eassert (STRINGP (string));
1791 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1793 if (STRING_MULTIBYTE (string))
1794 *newpos = string_pos_nchars_ahead (pos, string,
1795 CHARPOS (*newpos) - CHARPOS (pos));
1796 else
1797 BYTEPOS (*newpos) = CHARPOS (*newpos);
1800 /* EXPORT:
1801 Return an estimation of the pixel height of mode or header lines on
1802 frame F. FACE_ID specifies what line's height to estimate. */
1805 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1807 #ifdef HAVE_WINDOW_SYSTEM
1808 if (FRAME_WINDOW_P (f))
1810 int height = FONT_HEIGHT (FRAME_FONT (f));
1812 /* This function is called so early when Emacs starts that the face
1813 cache and mode line face are not yet initialized. */
1814 if (FRAME_FACE_CACHE (f))
1816 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1817 if (face)
1819 if (face->font)
1820 height = normal_char_height (face->font, -1);
1821 if (face->box_line_width > 0)
1822 height += 2 * face->box_line_width;
1826 return height;
1828 #endif
1830 return 1;
1833 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1834 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1835 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1836 not force the value into range. */
1838 void
1839 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1840 NativeRectangle *bounds, bool noclip)
1843 #ifdef HAVE_WINDOW_SYSTEM
1844 if (FRAME_WINDOW_P (f))
1846 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1847 even for negative values. */
1848 if (pix_x < 0)
1849 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1850 if (pix_y < 0)
1851 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1853 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1854 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1856 if (bounds)
1857 STORE_NATIVE_RECT (*bounds,
1858 FRAME_COL_TO_PIXEL_X (f, pix_x),
1859 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1860 FRAME_COLUMN_WIDTH (f) - 1,
1861 FRAME_LINE_HEIGHT (f) - 1);
1863 /* PXW: Should we clip pixels before converting to columns/lines? */
1864 if (!noclip)
1866 if (pix_x < 0)
1867 pix_x = 0;
1868 else if (pix_x > FRAME_TOTAL_COLS (f))
1869 pix_x = FRAME_TOTAL_COLS (f);
1871 if (pix_y < 0)
1872 pix_y = 0;
1873 else if (pix_y > FRAME_TOTAL_LINES (f))
1874 pix_y = FRAME_TOTAL_LINES (f);
1877 #endif
1879 *x = pix_x;
1880 *y = pix_y;
1884 /* Find the glyph under window-relative coordinates X/Y in window W.
1885 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1886 strings. Return in *HPOS and *VPOS the row and column number of
1887 the glyph found. Return in *AREA the glyph area containing X.
1888 Value is a pointer to the glyph found or null if X/Y is not on
1889 text, or we can't tell because W's current matrix is not up to
1890 date. */
1892 static struct glyph *
1893 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1894 int *dx, int *dy, int *area)
1896 struct glyph *glyph, *end;
1897 struct glyph_row *row = NULL;
1898 int x0, i;
1900 /* Find row containing Y. Give up if some row is not enabled. */
1901 for (i = 0; i < w->current_matrix->nrows; ++i)
1903 row = MATRIX_ROW (w->current_matrix, i);
1904 if (!row->enabled_p)
1905 return NULL;
1906 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1907 break;
1910 *vpos = i;
1911 *hpos = 0;
1913 /* Give up if Y is not in the window. */
1914 if (i == w->current_matrix->nrows)
1915 return NULL;
1917 /* Get the glyph area containing X. */
1918 if (w->pseudo_window_p)
1920 *area = TEXT_AREA;
1921 x0 = 0;
1923 else
1925 if (x < window_box_left_offset (w, TEXT_AREA))
1927 *area = LEFT_MARGIN_AREA;
1928 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1930 else if (x < window_box_right_offset (w, TEXT_AREA))
1932 *area = TEXT_AREA;
1933 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1935 else
1937 *area = RIGHT_MARGIN_AREA;
1938 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1942 /* Find glyph containing X. */
1943 glyph = row->glyphs[*area];
1944 end = glyph + row->used[*area];
1945 x -= x0;
1946 while (glyph < end && x >= glyph->pixel_width)
1948 x -= glyph->pixel_width;
1949 ++glyph;
1952 if (glyph == end)
1953 return NULL;
1955 if (dx)
1957 *dx = x;
1958 *dy = y - (row->y + row->ascent - glyph->ascent);
1961 *hpos = glyph - row->glyphs[*area];
1962 return glyph;
1965 /* Convert frame-relative x/y to coordinates relative to window W.
1966 Takes pseudo-windows into account. */
1968 static void
1969 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1971 if (w->pseudo_window_p)
1973 /* A pseudo-window is always full-width, and starts at the
1974 left edge of the frame, plus a frame border. */
1975 struct frame *f = XFRAME (w->frame);
1976 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1977 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1979 else
1981 *x -= WINDOW_LEFT_EDGE_X (w);
1982 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 #ifdef HAVE_WINDOW_SYSTEM
1988 /* EXPORT:
1989 Return in RECTS[] at most N clipping rectangles for glyph string S.
1990 Return the number of stored rectangles. */
1993 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1995 XRectangle r;
1997 if (n <= 0)
1998 return 0;
2000 if (s->row->full_width_p)
2002 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2003 r.x = WINDOW_LEFT_EDGE_X (s->w);
2004 if (s->row->mode_line_p)
2005 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2006 else
2007 r.width = WINDOW_PIXEL_WIDTH (s->w);
2009 /* Unless displaying a mode or menu bar line, which are always
2010 fully visible, clip to the visible part of the row. */
2011 if (s->w->pseudo_window_p)
2012 r.height = s->row->visible_height;
2013 else
2014 r.height = s->height;
2016 else
2018 /* This is a text line that may be partially visible. */
2019 r.x = window_box_left (s->w, s->area);
2020 r.width = window_box_width (s->w, s->area);
2021 r.height = s->row->visible_height;
2024 if (s->clip_head)
2025 if (r.x < s->clip_head->x)
2027 if (r.width >= s->clip_head->x - r.x)
2028 r.width -= s->clip_head->x - r.x;
2029 else
2030 r.width = 0;
2031 r.x = s->clip_head->x;
2033 if (s->clip_tail)
2034 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2036 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2037 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2038 else
2039 r.width = 0;
2042 /* If S draws overlapping rows, it's sufficient to use the top and
2043 bottom of the window for clipping because this glyph string
2044 intentionally draws over other lines. */
2045 if (s->for_overlaps)
2047 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2048 r.height = window_text_bottom_y (s->w) - r.y;
2050 /* Alas, the above simple strategy does not work for the
2051 environments with anti-aliased text: if the same text is
2052 drawn onto the same place multiple times, it gets thicker.
2053 If the overlap we are processing is for the erased cursor, we
2054 take the intersection with the rectangle of the cursor. */
2055 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2057 XRectangle rc, r_save = r;
2059 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2060 rc.y = s->w->phys_cursor.y;
2061 rc.width = s->w->phys_cursor_width;
2062 rc.height = s->w->phys_cursor_height;
2064 x_intersect_rectangles (&r_save, &rc, &r);
2067 else
2069 /* Don't use S->y for clipping because it doesn't take partially
2070 visible lines into account. For example, it can be negative for
2071 partially visible lines at the top of a window. */
2072 if (!s->row->full_width_p
2073 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2074 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2075 else
2076 r.y = max (0, s->row->y);
2079 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2081 /* If drawing the cursor, don't let glyph draw outside its
2082 advertised boundaries. Cleartype does this under some circumstances. */
2083 if (s->hl == DRAW_CURSOR)
2085 struct glyph *glyph = s->first_glyph;
2086 int height, max_y;
2088 if (s->x > r.x)
2090 if (r.width >= s->x - r.x)
2091 r.width -= s->x - r.x;
2092 else /* R2L hscrolled row with cursor outside text area */
2093 r.width = 0;
2094 r.x = s->x;
2096 r.width = min (r.width, glyph->pixel_width);
2098 /* If r.y is below window bottom, ensure that we still see a cursor. */
2099 height = min (glyph->ascent + glyph->descent,
2100 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2101 max_y = window_text_bottom_y (s->w) - height;
2102 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2103 if (s->ybase - glyph->ascent > max_y)
2105 r.y = max_y;
2106 r.height = height;
2108 else
2110 /* Don't draw cursor glyph taller than our actual glyph. */
2111 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2112 if (height < r.height)
2114 max_y = r.y + r.height;
2115 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2116 r.height = min (max_y - r.y, height);
2121 if (s->row->clip)
2123 XRectangle r_save = r;
2125 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2126 r.width = 0;
2129 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2130 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2132 #ifdef CONVERT_FROM_XRECT
2133 CONVERT_FROM_XRECT (r, *rects);
2134 #else
2135 *rects = r;
2136 #endif
2137 return 1;
2139 else
2141 /* If we are processing overlapping and allowed to return
2142 multiple clipping rectangles, we exclude the row of the glyph
2143 string from the clipping rectangle. This is to avoid drawing
2144 the same text on the environment with anti-aliasing. */
2145 #ifdef CONVERT_FROM_XRECT
2146 XRectangle rs[2];
2147 #else
2148 XRectangle *rs = rects;
2149 #endif
2150 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2152 if (s->for_overlaps & OVERLAPS_PRED)
2154 rs[i] = r;
2155 if (r.y + r.height > row_y)
2157 if (r.y < row_y)
2158 rs[i].height = row_y - r.y;
2159 else
2160 rs[i].height = 0;
2162 i++;
2164 if (s->for_overlaps & OVERLAPS_SUCC)
2166 rs[i] = r;
2167 if (r.y < row_y + s->row->visible_height)
2169 if (r.y + r.height > row_y + s->row->visible_height)
2171 rs[i].y = row_y + s->row->visible_height;
2172 rs[i].height = r.y + r.height - rs[i].y;
2174 else
2175 rs[i].height = 0;
2177 i++;
2180 n = i;
2181 #ifdef CONVERT_FROM_XRECT
2182 for (i = 0; i < n; i++)
2183 CONVERT_FROM_XRECT (rs[i], rects[i]);
2184 #endif
2185 return n;
2189 /* EXPORT:
2190 Return in *NR the clipping rectangle for glyph string S. */
2192 void
2193 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2195 get_glyph_string_clip_rects (s, nr, 1);
2199 /* EXPORT:
2200 Return the position and height of the phys cursor in window W.
2201 Set w->phys_cursor_width to width of phys cursor.
2204 void
2205 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2206 struct glyph *glyph, int *xp, int *yp, int *heightp)
2208 struct frame *f = XFRAME (WINDOW_FRAME (w));
2209 int x, y, wd, h, h0, y0, ascent;
2211 /* Compute the width of the rectangle to draw. If on a stretch
2212 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2213 rectangle as wide as the glyph, but use a canonical character
2214 width instead. */
2215 wd = glyph->pixel_width;
2217 x = w->phys_cursor.x;
2218 if (x < 0)
2220 wd += x;
2221 x = 0;
2224 if (glyph->type == STRETCH_GLYPH
2225 && !x_stretch_cursor_p)
2226 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2227 w->phys_cursor_width = wd;
2229 /* Don't let the hollow cursor glyph descend below the glyph row's
2230 ascent value, lest the hollow cursor looks funny. */
2231 y = w->phys_cursor.y;
2232 ascent = row->ascent;
2233 if (row->ascent < glyph->ascent)
2235 y -= glyph->ascent - row->ascent;
2236 ascent = glyph->ascent;
2239 /* If y is below window bottom, ensure that we still see a cursor. */
2240 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2242 h = max (h0, ascent + glyph->descent);
2243 h0 = min (h0, ascent + glyph->descent);
2245 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2246 if (y < y0)
2248 h = max (h - (y0 - y) + 1, h0);
2249 y = y0 - 1;
2251 else
2253 y0 = window_text_bottom_y (w) - h0;
2254 if (y > y0)
2256 h += y - y0;
2257 y = y0;
2261 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2262 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2263 *heightp = h;
2267 * Remember which glyph the mouse is over.
2270 void
2271 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2273 Lisp_Object window;
2274 struct window *w;
2275 struct glyph_row *r, *gr, *end_row;
2276 enum window_part part;
2277 enum glyph_row_area area;
2278 int x, y, width, height;
2280 /* Try to determine frame pixel position and size of the glyph under
2281 frame pixel coordinates X/Y on frame F. */
2283 if (window_resize_pixelwise)
2285 width = height = 1;
2286 goto virtual_glyph;
2288 else if (!f->glyphs_initialized_p
2289 || (window = window_from_coordinates (f, gx, gy, &part, false),
2290 NILP (window)))
2292 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2293 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2294 goto virtual_glyph;
2297 w = XWINDOW (window);
2298 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2299 height = WINDOW_FRAME_LINE_HEIGHT (w);
2301 x = window_relative_x_coord (w, part, gx);
2302 y = gy - WINDOW_TOP_EDGE_Y (w);
2304 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2305 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2307 if (w->pseudo_window_p)
2309 area = TEXT_AREA;
2310 part = ON_MODE_LINE; /* Don't adjust margin. */
2311 goto text_glyph;
2314 switch (part)
2316 case ON_LEFT_MARGIN:
2317 area = LEFT_MARGIN_AREA;
2318 goto text_glyph;
2320 case ON_RIGHT_MARGIN:
2321 area = RIGHT_MARGIN_AREA;
2322 goto text_glyph;
2324 case ON_HEADER_LINE:
2325 case ON_MODE_LINE:
2326 gr = (part == ON_HEADER_LINE
2327 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2328 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2329 gy = gr->y;
2330 area = TEXT_AREA;
2331 goto text_glyph_row_found;
2333 case ON_TEXT:
2334 area = TEXT_AREA;
2336 text_glyph:
2337 gr = 0; gy = 0;
2338 for (; r <= end_row && r->enabled_p; ++r)
2339 if (r->y + r->height > y)
2341 gr = r; gy = r->y;
2342 break;
2345 text_glyph_row_found:
2346 if (gr && gy <= y)
2348 struct glyph *g = gr->glyphs[area];
2349 struct glyph *end = g + gr->used[area];
2351 height = gr->height;
2352 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2353 if (gx + g->pixel_width > x)
2354 break;
2356 if (g < end)
2358 if (g->type == IMAGE_GLYPH)
2360 /* Don't remember when mouse is over image, as
2361 image may have hot-spots. */
2362 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2363 return;
2365 width = g->pixel_width;
2367 else
2369 /* Use nominal char spacing at end of line. */
2370 x -= gx;
2371 gx += (x / width) * width;
2374 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2376 gx += window_box_left_offset (w, area);
2377 /* Don't expand over the modeline to make sure the vertical
2378 drag cursor is shown early enough. */
2379 height = min (height,
2380 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2383 else
2385 /* Use nominal line height at end of window. */
2386 gx = (x / width) * width;
2387 y -= gy;
2388 gy += (y / height) * height;
2389 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2390 /* See comment above. */
2391 height = min (height,
2392 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2394 break;
2396 case ON_LEFT_FRINGE:
2397 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2398 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2399 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2400 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2401 goto row_glyph;
2403 case ON_RIGHT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2406 : window_box_right_offset (w, TEXT_AREA));
2407 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2408 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2409 && !WINDOW_RIGHTMOST_P (w))
2410 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2411 /* Make sure the vertical border can get her own glyph to the
2412 right of the one we build here. */
2413 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2414 else
2415 width = WINDOW_PIXEL_WIDTH (w) - gx;
2416 else
2417 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2419 goto row_glyph;
2421 case ON_VERTICAL_BORDER:
2422 gx = WINDOW_PIXEL_WIDTH (w) - width;
2423 goto row_glyph;
2425 case ON_VERTICAL_SCROLL_BAR:
2426 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2428 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2429 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2431 : 0)));
2432 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2434 row_glyph:
2435 gr = 0, gy = 0;
2436 for (; r <= end_row && r->enabled_p; ++r)
2437 if (r->y + r->height > y)
2439 gr = r; gy = r->y;
2440 break;
2443 if (gr && gy <= y)
2444 height = gr->height;
2445 else
2447 /* Use nominal line height at end of window. */
2448 y -= gy;
2449 gy += (y / height) * height;
2451 break;
2453 case ON_RIGHT_DIVIDER:
2454 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2455 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2456 gy = 0;
2457 /* The bottom divider prevails. */
2458 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2459 goto add_edge;
2461 case ON_BOTTOM_DIVIDER:
2462 gx = 0;
2463 width = WINDOW_PIXEL_WIDTH (w);
2464 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2465 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 default:
2470 virtual_glyph:
2471 /* If there is no glyph under the mouse, then we divide the screen
2472 into a grid of the smallest glyph in the frame, and use that
2473 as our "glyph". */
2475 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2476 round down even for negative values. */
2477 if (gx < 0)
2478 gx -= width - 1;
2479 if (gy < 0)
2480 gy -= height - 1;
2482 gx = (gx / width) * width;
2483 gy = (gy / height) * height;
2485 goto store_rect;
2488 add_edge:
2489 gx += WINDOW_LEFT_EDGE_X (w);
2490 gy += WINDOW_TOP_EDGE_Y (w);
2492 store_rect:
2493 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2495 /* Visible feedback for debugging. */
2496 #if false && defined HAVE_X_WINDOWS
2497 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2498 f->output_data.x->normal_gc,
2499 gx, gy, width, height);
2500 #endif
2504 #endif /* HAVE_WINDOW_SYSTEM */
2506 static void
2507 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2509 eassert (w);
2510 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2511 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2512 w->window_end_vpos
2513 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2516 static bool
2517 hscrolling_current_line_p (struct window *w)
2519 return (!w->suspend_auto_hscroll
2520 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2521 Qcurrent_line));
2524 /***********************************************************************
2525 Lisp form evaluation
2526 ***********************************************************************/
2528 /* Error handler for safe_eval and safe_call. */
2530 static Lisp_Object
2531 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2533 add_to_log ("Error during redisplay: %S signaled %S",
2534 Flist (nargs, args), arg);
2535 return Qnil;
2538 /* Call function FUNC with the rest of NARGS - 1 arguments
2539 following. Return the result, or nil if something went
2540 wrong. Prevent redisplay during the evaluation. */
2542 static Lisp_Object
2543 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2545 Lisp_Object val;
2547 if (inhibit_eval_during_redisplay)
2548 val = Qnil;
2549 else
2551 ptrdiff_t i;
2552 ptrdiff_t count = SPECPDL_INDEX ();
2553 Lisp_Object *args;
2554 USE_SAFE_ALLOCA;
2555 SAFE_ALLOCA_LISP (args, nargs);
2557 args[0] = func;
2558 for (i = 1; i < nargs; i++)
2559 args[i] = va_arg (ap, Lisp_Object);
2561 specbind (Qinhibit_redisplay, Qt);
2562 if (inhibit_quit)
2563 specbind (Qinhibit_quit, Qt);
2564 /* Use Qt to ensure debugger does not run,
2565 so there is no possibility of wanting to redisplay. */
2566 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2567 safe_eval_handler);
2568 SAFE_FREE ();
2569 val = unbind_to (count, val);
2572 return val;
2575 Lisp_Object
2576 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2578 Lisp_Object retval;
2579 va_list ap;
2581 va_start (ap, func);
2582 retval = safe__call (false, nargs, func, ap);
2583 va_end (ap);
2584 return retval;
2587 /* Call function FN with one argument ARG.
2588 Return the result, or nil if something went wrong. */
2590 Lisp_Object
2591 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2593 return safe_call (2, fn, arg);
2596 static Lisp_Object
2597 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2599 Lisp_Object retval;
2600 va_list ap;
2602 va_start (ap, fn);
2603 retval = safe__call (inhibit_quit, 2, fn, ap);
2604 va_end (ap);
2605 return retval;
2608 Lisp_Object
2609 safe_eval (Lisp_Object sexpr)
2611 return safe__call1 (false, Qeval, sexpr);
2614 static Lisp_Object
2615 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2617 return safe__call1 (inhibit_quit, Qeval, sexpr);
2620 /* Call function FN with two arguments ARG1 and ARG2.
2621 Return the result, or nil if something went wrong. */
2623 Lisp_Object
2624 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2626 return safe_call (3, fn, arg1, arg2);
2631 /***********************************************************************
2632 Debugging
2633 ***********************************************************************/
2635 /* Define CHECK_IT to perform sanity checks on iterators.
2636 This is for debugging. It is too slow to do unconditionally. */
2638 static void
2639 CHECK_IT (struct it *it)
2641 #if false
2642 if (it->method == GET_FROM_STRING)
2644 eassert (STRINGP (it->string));
2645 eassert (IT_STRING_CHARPOS (*it) >= 0);
2647 else
2649 eassert (IT_STRING_CHARPOS (*it) < 0);
2650 if (it->method == GET_FROM_BUFFER)
2652 /* Check that character and byte positions agree. */
2653 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2657 if (it->dpvec)
2658 eassert (it->current.dpvec_index >= 0);
2659 else
2660 eassert (it->current.dpvec_index < 0);
2661 #endif
2665 /* Check that the window end of window W is what we expect it
2666 to be---the last row in the current matrix displaying text. */
2668 static void
2669 CHECK_WINDOW_END (struct window *w)
2671 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2672 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2674 struct glyph_row *row;
2675 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2676 !row->enabled_p
2677 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2678 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #endif
2683 /***********************************************************************
2684 Iterator initialization
2685 ***********************************************************************/
2687 /* Initialize IT for displaying current_buffer in window W, starting
2688 at character position CHARPOS. CHARPOS < 0 means that no buffer
2689 position is specified which is useful when the iterator is assigned
2690 a position later. BYTEPOS is the byte position corresponding to
2691 CHARPOS.
2693 If ROW is not null, calls to produce_glyphs with IT as parameter
2694 will produce glyphs in that row.
2696 BASE_FACE_ID is the id of a base face to use. It must be one of
2697 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2698 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2699 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2701 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2702 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2703 will be initialized to use the corresponding mode line glyph row of
2704 the desired matrix of W. */
2706 void
2707 init_iterator (struct it *it, struct window *w,
2708 ptrdiff_t charpos, ptrdiff_t bytepos,
2709 struct glyph_row *row, enum face_id base_face_id)
2711 enum face_id remapped_base_face_id = base_face_id;
2713 /* Some precondition checks. */
2714 eassert (w != NULL && it != NULL);
2715 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2716 && charpos <= ZV));
2718 /* If face attributes have been changed since the last redisplay,
2719 free realized faces now because they depend on face definitions
2720 that might have changed. Don't free faces while there might be
2721 desired matrices pending which reference these faces. */
2722 if (!inhibit_free_realized_faces)
2724 if (face_change)
2726 face_change = false;
2727 XFRAME (w->frame)->face_change = 0;
2728 free_all_realized_faces (Qnil);
2730 else if (XFRAME (w->frame)->face_change)
2732 XFRAME (w->frame)->face_change = 0;
2733 free_all_realized_faces (w->frame);
2737 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2738 if (! NILP (Vface_remapping_alist))
2739 remapped_base_face_id
2740 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2742 /* Use one of the mode line rows of W's desired matrix if
2743 appropriate. */
2744 if (row == NULL)
2746 if (base_face_id == MODE_LINE_FACE_ID
2747 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2748 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2749 else if (base_face_id == HEADER_LINE_FACE_ID)
2750 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2753 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2754 Other parts of redisplay rely on that. */
2755 memclear (it, sizeof *it);
2756 it->current.overlay_string_index = -1;
2757 it->current.dpvec_index = -1;
2758 it->base_face_id = remapped_base_face_id;
2759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2760 it->paragraph_embedding = L2R;
2761 it->bidi_it.w = w;
2763 /* The window in which we iterate over current_buffer: */
2764 XSETWINDOW (it->window, w);
2765 it->w = w;
2766 it->f = XFRAME (w->frame);
2768 it->cmp_it.id = -1;
2770 /* Extra space between lines (on window systems only). */
2771 if (base_face_id == DEFAULT_FACE_ID
2772 && FRAME_WINDOW_P (it->f))
2774 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2776 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2777 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2778 * FRAME_LINE_HEIGHT (it->f));
2779 else if (it->f->extra_line_spacing > 0)
2780 it->extra_line_spacing = it->f->extra_line_spacing;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 it->override_ascent = -1;
2794 /* Are control characters displayed as `^C'? */
2795 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2797 /* -1 means everything between a CR and the following line end
2798 is invisible. >0 means lines indented more than this value are
2799 invisible. */
2800 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2801 ? (clip_to_bounds
2802 (-1, XINT (BVAR (current_buffer, selective_display)),
2803 PTRDIFF_MAX))
2804 : (!NILP (BVAR (current_buffer, selective_display))
2805 ? -1 : 0));
2806 it->selective_display_ellipsis_p
2807 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2809 /* Display table to use. */
2810 it->dp = window_display_table (w);
2812 /* Are multibyte characters enabled in current_buffer? */
2813 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2815 /* Get the position at which the redisplay_end_trigger hook should
2816 be run, if it is to be run at all. */
2817 if (MARKERP (w->redisplay_end_trigger)
2818 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2819 it->redisplay_end_trigger_charpos
2820 = marker_position (w->redisplay_end_trigger);
2821 else if (INTEGERP (w->redisplay_end_trigger))
2822 it->redisplay_end_trigger_charpos
2823 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2824 PTRDIFF_MAX);
2826 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2828 /* Are lines in the display truncated? */
2829 if (TRUNCATE != 0)
2830 it->line_wrap = TRUNCATE;
2831 if (base_face_id == DEFAULT_FACE_ID
2832 && !it->w->hscroll
2833 && (WINDOW_FULL_WIDTH_P (it->w)
2834 || NILP (Vtruncate_partial_width_windows)
2835 || (INTEGERP (Vtruncate_partial_width_windows)
2836 /* PXW: Shall we do something about this? */
2837 && (XINT (Vtruncate_partial_width_windows)
2838 <= WINDOW_TOTAL_COLS (it->w))))
2839 && NILP (BVAR (current_buffer, truncate_lines)))
2840 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2841 ? WINDOW_WRAP : WORD_WRAP;
2843 /* Get dimensions of truncation and continuation glyphs. These are
2844 displayed as fringe bitmaps under X, but we need them for such
2845 frames when the fringes are turned off. But leave the dimensions
2846 zero for tooltip frames, as these glyphs look ugly there and also
2847 sabotage calculations of tooltip dimensions in x-show-tip. */
2848 #ifdef HAVE_WINDOW_SYSTEM
2849 if (!(FRAME_WINDOW_P (it->f)
2850 && FRAMEP (tip_frame)
2851 && it->f == XFRAME (tip_frame)))
2852 #endif
2854 if (it->line_wrap == TRUNCATE)
2856 /* We will need the truncation glyph. */
2857 eassert (it->glyph_row == NULL);
2858 produce_special_glyphs (it, IT_TRUNCATION);
2859 it->truncation_pixel_width = it->pixel_width;
2861 else
2863 /* We will need the continuation glyph. */
2864 eassert (it->glyph_row == NULL);
2865 produce_special_glyphs (it, IT_CONTINUATION);
2866 it->continuation_pixel_width = it->pixel_width;
2870 /* Reset these values to zero because the produce_special_glyphs
2871 above has changed them. */
2872 it->pixel_width = it->ascent = it->descent = 0;
2873 it->phys_ascent = it->phys_descent = 0;
2875 /* Set this after getting the dimensions of truncation and
2876 continuation glyphs, so that we don't produce glyphs when calling
2877 produce_special_glyphs, above. */
2878 it->glyph_row = row;
2879 it->area = TEXT_AREA;
2881 /* Get the dimensions of the display area. The display area
2882 consists of the visible window area plus a horizontally scrolled
2883 part to the left of the window. All x-values are relative to the
2884 start of this total display area. */
2885 if (base_face_id != DEFAULT_FACE_ID)
2887 /* Mode lines, menu bar in terminal frames. */
2888 it->first_visible_x = 0;
2889 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2891 else
2893 /* When hscrolling only the current line, don't apply the
2894 hscroll here, it will be applied by display_line when it gets
2895 to laying out the line showing point. However, if the
2896 window's min_hscroll is positive, the user specified a lower
2897 bound for automatic hscrolling, so they expect the
2898 non-current lines to obey that hscroll amount. */
2899 if (hscrolling_current_line_p (w))
2901 if (w->min_hscroll > 0)
2902 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2903 else
2904 it->first_visible_x = 0;
2906 else
2907 it->first_visible_x =
2908 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2909 it->last_visible_x = (it->first_visible_x
2910 + window_box_width (w, TEXT_AREA));
2912 /* If we truncate lines, leave room for the truncation glyph(s) at
2913 the right margin. Otherwise, leave room for the continuation
2914 glyph(s). Done only if the window has no right fringe. */
2915 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2917 if (it->line_wrap == TRUNCATE)
2918 it->last_visible_x -= it->truncation_pixel_width;
2919 else
2920 it->last_visible_x -= it->continuation_pixel_width;
2923 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2924 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2927 /* Leave room for a border glyph. */
2928 if (!FRAME_WINDOW_P (it->f)
2929 && !WINDOW_RIGHTMOST_P (it->w))
2930 it->last_visible_x -= 1;
2932 it->last_visible_y = window_text_bottom_y (w);
2934 /* For mode lines and alike, arrange for the first glyph having a
2935 left box line if the face specifies a box. */
2936 if (base_face_id != DEFAULT_FACE_ID)
2938 struct face *face;
2940 it->face_id = remapped_base_face_id;
2942 /* If we have a boxed mode line, make the first character appear
2943 with a left box line. */
2944 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2945 if (face && face->box != FACE_NO_BOX)
2946 it->start_of_box_run_p = true;
2949 /* If a buffer position was specified, set the iterator there,
2950 getting overlays and face properties from that position. */
2951 if (charpos >= BUF_BEG (current_buffer))
2953 it->stop_charpos = charpos;
2954 it->end_charpos = ZV;
2955 eassert (charpos == BYTE_TO_CHAR (bytepos));
2956 IT_CHARPOS (*it) = charpos;
2957 IT_BYTEPOS (*it) = bytepos;
2959 /* We will rely on `reseat' to set this up properly, via
2960 handle_face_prop. */
2961 it->face_id = it->base_face_id;
2963 it->start = it->current;
2964 /* Do we need to reorder bidirectional text? Not if this is a
2965 unibyte buffer: by definition, none of the single-byte
2966 characters are strong R2L, so no reordering is needed. And
2967 bidi.c doesn't support unibyte buffers anyway. Also, don't
2968 reorder while we are loading loadup.el, since the tables of
2969 character properties needed for reordering are not yet
2970 available. */
2971 it->bidi_p =
2972 !redisplay__inhibit_bidi
2973 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2974 && it->multibyte_p;
2976 /* If we are to reorder bidirectional text, init the bidi
2977 iterator. */
2978 if (it->bidi_p)
2980 /* Since we don't know at this point whether there will be
2981 any R2L lines in the window, we reserve space for
2982 truncation/continuation glyphs even if only the left
2983 fringe is absent. */
2984 if (base_face_id == DEFAULT_FACE_ID
2985 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2986 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2988 if (it->line_wrap == TRUNCATE)
2989 it->last_visible_x -= it->truncation_pixel_width;
2990 else
2991 it->last_visible_x -= it->continuation_pixel_width;
2993 /* Note the paragraph direction that this buffer wants to
2994 use. */
2995 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2996 Qleft_to_right))
2997 it->paragraph_embedding = L2R;
2998 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2999 Qright_to_left))
3000 it->paragraph_embedding = R2L;
3001 else
3002 it->paragraph_embedding = NEUTRAL_DIR;
3003 bidi_unshelve_cache (NULL, false);
3004 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3005 &it->bidi_it);
3008 /* Compute faces etc. */
3009 reseat (it, it->current.pos, true);
3012 CHECK_IT (it);
3016 /* Initialize IT for the display of window W with window start POS. */
3018 void
3019 start_display (struct it *it, struct window *w, struct text_pos pos)
3021 struct glyph_row *row;
3022 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3024 row = w->desired_matrix->rows + first_vpos;
3025 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3026 it->first_vpos = first_vpos;
3028 /* Don't reseat to previous visible line start if current start
3029 position is in a string or image. */
3030 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3032 int first_y = it->current_y;
3034 /* If window start is not at a line start, skip forward to POS to
3035 get the correct continuation lines width. */
3036 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3037 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3038 if (!start_at_line_beg_p)
3040 int new_x;
3042 reseat_at_previous_visible_line_start (it);
3043 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3045 new_x = it->current_x + it->pixel_width;
3047 /* If lines are continued, this line may end in the middle
3048 of a multi-glyph character (e.g. a control character
3049 displayed as \003, or in the middle of an overlay
3050 string). In this case move_it_to above will not have
3051 taken us to the start of the continuation line but to the
3052 end of the continued line. */
3053 if (it->current_x > 0
3054 && it->line_wrap != TRUNCATE /* Lines are continued. */
3055 && (/* And glyph doesn't fit on the line. */
3056 new_x > it->last_visible_x
3057 /* Or it fits exactly and we're on a window
3058 system frame. */
3059 || (new_x == it->last_visible_x
3060 && FRAME_WINDOW_P (it->f)
3061 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3062 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3063 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3065 if ((it->current.dpvec_index >= 0
3066 || it->current.overlay_string_index >= 0)
3067 /* If we are on a newline from a display vector or
3068 overlay string, then we are already at the end of
3069 a screen line; no need to go to the next line in
3070 that case, as this line is not really continued.
3071 (If we do go to the next line, C-e will not DTRT.) */
3072 && it->c != '\n')
3074 set_iterator_to_next (it, true);
3075 move_it_in_display_line_to (it, -1, -1, 0);
3078 it->continuation_lines_width += it->current_x;
3080 /* If the character at POS is displayed via a display
3081 vector, move_it_to above stops at the final glyph of
3082 IT->dpvec. To make the caller redisplay that character
3083 again (a.k.a. start at POS), we need to reset the
3084 dpvec_index to the beginning of IT->dpvec. */
3085 else if (it->current.dpvec_index >= 0)
3086 it->current.dpvec_index = 0;
3088 /* We're starting a new display line, not affected by the
3089 height of the continued line, so clear the appropriate
3090 fields in the iterator structure. */
3091 it->max_ascent = it->max_descent = 0;
3092 it->max_phys_ascent = it->max_phys_descent = 0;
3094 it->current_y = first_y;
3095 it->vpos = 0;
3096 it->current_x = it->hpos = 0;
3102 /* Return true if POS is a position in ellipses displayed for invisible
3103 text. W is the window we display, for text property lookup. */
3105 static bool
3106 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3108 Lisp_Object prop, window;
3109 bool ellipses_p = false;
3110 ptrdiff_t charpos = CHARPOS (pos->pos);
3112 /* If POS specifies a position in a display vector, this might
3113 be for an ellipsis displayed for invisible text. We won't
3114 get the iterator set up for delivering that ellipsis unless
3115 we make sure that it gets aware of the invisible text. */
3116 if (pos->dpvec_index >= 0
3117 && pos->overlay_string_index < 0
3118 && CHARPOS (pos->string_pos) < 0
3119 && charpos > BEGV
3120 && (XSETWINDOW (window, w),
3121 prop = Fget_char_property (make_number (charpos),
3122 Qinvisible, window),
3123 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3125 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3126 window);
3127 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3130 return ellipses_p;
3134 /* Initialize IT for stepping through current_buffer in window W,
3135 starting at position POS that includes overlay string and display
3136 vector/ control character translation position information. Value
3137 is false if there are overlay strings with newlines at POS. */
3139 static bool
3140 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3142 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3143 int i;
3144 bool overlay_strings_with_newlines = false;
3146 /* If POS specifies a position in a display vector, this might
3147 be for an ellipsis displayed for invisible text. We won't
3148 get the iterator set up for delivering that ellipsis unless
3149 we make sure that it gets aware of the invisible text. */
3150 if (in_ellipses_for_invisible_text_p (pos, w))
3152 --charpos;
3153 bytepos = 0;
3156 /* Keep in mind: the call to reseat in init_iterator skips invisible
3157 text, so we might end up at a position different from POS. This
3158 is only a problem when POS is a row start after a newline and an
3159 overlay starts there with an after-string, and the overlay has an
3160 invisible property. Since we don't skip invisible text in
3161 display_line and elsewhere immediately after consuming the
3162 newline before the row start, such a POS will not be in a string,
3163 but the call to init_iterator below will move us to the
3164 after-string. */
3165 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3167 /* This only scans the current chunk -- it should scan all chunks.
3168 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3169 to 16 in 22.1 to make this a lesser problem. */
3170 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3172 const char *s = SSDATA (it->overlay_strings[i]);
3173 const char *e = s + SBYTES (it->overlay_strings[i]);
3175 while (s < e && *s != '\n')
3176 ++s;
3178 if (s < e)
3180 overlay_strings_with_newlines = true;
3181 break;
3185 /* If position is within an overlay string, set up IT to the right
3186 overlay string. */
3187 if (pos->overlay_string_index >= 0)
3189 int relative_index;
3191 /* If the first overlay string happens to have a `display'
3192 property for an image, the iterator will be set up for that
3193 image, and we have to undo that setup first before we can
3194 correct the overlay string index. */
3195 if (it->method == GET_FROM_IMAGE)
3196 pop_it (it);
3198 /* We already have the first chunk of overlay strings in
3199 IT->overlay_strings. Load more until the one for
3200 pos->overlay_string_index is in IT->overlay_strings. */
3201 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3203 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3204 it->current.overlay_string_index = 0;
3205 while (n--)
3207 load_overlay_strings (it, 0);
3208 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3212 it->current.overlay_string_index = pos->overlay_string_index;
3213 relative_index = (it->current.overlay_string_index
3214 % OVERLAY_STRING_CHUNK_SIZE);
3215 it->string = it->overlay_strings[relative_index];
3216 eassert (STRINGP (it->string));
3217 it->current.string_pos = pos->string_pos;
3218 it->method = GET_FROM_STRING;
3219 it->end_charpos = SCHARS (it->string);
3220 /* Set up the bidi iterator for this overlay string. */
3221 if (it->bidi_p)
3223 it->bidi_it.string.lstring = it->string;
3224 it->bidi_it.string.s = NULL;
3225 it->bidi_it.string.schars = SCHARS (it->string);
3226 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3227 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3228 it->bidi_it.string.unibyte = !it->multibyte_p;
3229 it->bidi_it.w = it->w;
3230 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3231 FRAME_WINDOW_P (it->f), &it->bidi_it);
3233 /* Synchronize the state of the bidi iterator with
3234 pos->string_pos. For any string position other than
3235 zero, this will be done automagically when we resume
3236 iteration over the string and get_visually_first_element
3237 is called. But if string_pos is zero, and the string is
3238 to be reordered for display, we need to resync manually,
3239 since it could be that the iteration state recorded in
3240 pos ended at string_pos of 0 moving backwards in string. */
3241 if (CHARPOS (pos->string_pos) == 0)
3243 get_visually_first_element (it);
3244 if (IT_STRING_CHARPOS (*it) != 0)
3245 do {
3246 /* Paranoia. */
3247 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3248 bidi_move_to_visually_next (&it->bidi_it);
3249 } while (it->bidi_it.charpos != 0);
3251 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3252 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3256 if (CHARPOS (pos->string_pos) >= 0)
3258 /* Recorded position is not in an overlay string, but in another
3259 string. This can only be a string from a `display' property.
3260 IT should already be filled with that string. */
3261 it->current.string_pos = pos->string_pos;
3262 eassert (STRINGP (it->string));
3263 if (it->bidi_p)
3264 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3265 FRAME_WINDOW_P (it->f), &it->bidi_it);
3268 /* Restore position in display vector translations, control
3269 character translations or ellipses. */
3270 if (pos->dpvec_index >= 0)
3272 if (it->dpvec == NULL)
3273 get_next_display_element (it);
3274 eassert (it->dpvec && it->current.dpvec_index == 0);
3275 it->current.dpvec_index = pos->dpvec_index;
3278 CHECK_IT (it);
3279 return !overlay_strings_with_newlines;
3283 /* Initialize IT for stepping through current_buffer in window W
3284 starting at ROW->start. */
3286 static void
3287 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3289 init_from_display_pos (it, w, &row->start);
3290 it->start = row->start;
3291 it->continuation_lines_width = row->continuation_lines_width;
3292 CHECK_IT (it);
3296 /* Initialize IT for stepping through current_buffer in window W
3297 starting in the line following ROW, i.e. starting at ROW->end.
3298 Value is false if there are overlay strings with newlines at ROW's
3299 end position. */
3301 static bool
3302 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3304 bool success = false;
3306 if (init_from_display_pos (it, w, &row->end))
3308 if (row->continued_p)
3309 it->continuation_lines_width
3310 = row->continuation_lines_width + row->pixel_width;
3311 CHECK_IT (it);
3312 success = true;
3315 return success;
3321 /***********************************************************************
3322 Text properties
3323 ***********************************************************************/
3325 /* Called when IT reaches IT->stop_charpos. Handle text property and
3326 overlay changes. Set IT->stop_charpos to the next position where
3327 to stop. */
3329 static void
3330 handle_stop (struct it *it)
3332 enum prop_handled handled;
3333 bool handle_overlay_change_p;
3334 struct props *p;
3336 it->dpvec = NULL;
3337 it->current.dpvec_index = -1;
3338 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3339 it->ellipsis_p = false;
3341 /* Use face of preceding text for ellipsis (if invisible) */
3342 if (it->selective_display_ellipsis_p)
3343 it->saved_face_id = it->face_id;
3345 /* Here's the description of the semantics of, and the logic behind,
3346 the various HANDLED_* statuses:
3348 HANDLED_NORMALLY means the handler did its job, and the loop
3349 should proceed to calling the next handler in order.
3351 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3352 change in the properties and overlays at current position, so the
3353 loop should be restarted, to re-invoke the handlers that were
3354 already called. This happens when fontification-functions were
3355 called by handle_fontified_prop, and actually fontified
3356 something. Another case where HANDLED_RECOMPUTE_PROPS is
3357 returned is when we discover overlay strings that need to be
3358 displayed right away. The loop below will continue for as long
3359 as the status is HANDLED_RECOMPUTE_PROPS.
3361 HANDLED_RETURN means return immediately to the caller, to
3362 continue iteration without calling any further handlers. This is
3363 used when we need to act on some property right away, for example
3364 when we need to display the ellipsis or a replacing display
3365 property, such as display string or image.
3367 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3368 consumed, and the handler switched to the next overlay string.
3369 This signals the loop below to refrain from looking for more
3370 overlays before all the overlay strings of the current overlay
3371 are processed.
3373 Some of the handlers called by the loop push the iterator state
3374 onto the stack (see 'push_it'), and arrange for the iteration to
3375 continue with another object, such as an image, a display string,
3376 or an overlay string. In most such cases, it->stop_charpos is
3377 set to the first character of the string, so that when the
3378 iteration resumes, this function will immediately be called
3379 again, to examine the properties at the beginning of the string.
3381 When a display or overlay string is exhausted, the iterator state
3382 is popped (see 'pop_it'), and iteration continues with the
3383 previous object. Again, in many such cases this function is
3384 called again to find the next position where properties might
3385 change. */
3389 handled = HANDLED_NORMALLY;
3391 /* Call text property handlers. */
3392 for (p = it_props; p->handler; ++p)
3394 handled = p->handler (it);
3396 if (handled == HANDLED_RECOMPUTE_PROPS)
3397 break;
3398 else if (handled == HANDLED_RETURN)
3400 /* We still want to show before and after strings from
3401 overlays even if the actual buffer text is replaced. */
3402 if (!handle_overlay_change_p
3403 || it->sp > 1
3404 /* Don't call get_overlay_strings_1 if we already
3405 have overlay strings loaded, because doing so
3406 will load them again and push the iterator state
3407 onto the stack one more time, which is not
3408 expected by the rest of the code that processes
3409 overlay strings. */
3410 || (it->current.overlay_string_index < 0
3411 && !get_overlay_strings_1 (it, 0, false)))
3413 if (it->ellipsis_p)
3414 setup_for_ellipsis (it, 0);
3415 /* When handling a display spec, we might load an
3416 empty string. In that case, discard it here. We
3417 used to discard it in handle_single_display_spec,
3418 but that causes get_overlay_strings_1, above, to
3419 ignore overlay strings that we must check. */
3420 if (STRINGP (it->string) && !SCHARS (it->string))
3421 pop_it (it);
3422 return;
3424 else if (STRINGP (it->string) && !SCHARS (it->string))
3425 pop_it (it);
3426 else
3428 it->string_from_display_prop_p = false;
3429 it->from_disp_prop_p = false;
3430 handle_overlay_change_p = false;
3432 handled = HANDLED_RECOMPUTE_PROPS;
3433 break;
3435 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3436 handle_overlay_change_p = false;
3439 if (handled != HANDLED_RECOMPUTE_PROPS)
3441 /* Don't check for overlay strings below when set to deliver
3442 characters from a display vector. */
3443 if (it->method == GET_FROM_DISPLAY_VECTOR)
3444 handle_overlay_change_p = false;
3446 /* Handle overlay changes.
3447 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3448 if it finds overlays. */
3449 if (handle_overlay_change_p)
3450 handled = handle_overlay_change (it);
3453 if (it->ellipsis_p)
3455 setup_for_ellipsis (it, 0);
3456 break;
3459 while (handled == HANDLED_RECOMPUTE_PROPS);
3461 /* Determine where to stop next. */
3462 if (handled == HANDLED_NORMALLY)
3463 compute_stop_pos (it);
3467 /* Compute IT->stop_charpos from text property and overlay change
3468 information for IT's current position. */
3470 static void
3471 compute_stop_pos (struct it *it)
3473 register INTERVAL iv, next_iv;
3474 Lisp_Object object, limit, position;
3475 ptrdiff_t charpos, bytepos;
3477 if (STRINGP (it->string))
3479 /* Strings are usually short, so don't limit the search for
3480 properties. */
3481 it->stop_charpos = it->end_charpos;
3482 object = it->string;
3483 limit = Qnil;
3484 charpos = IT_STRING_CHARPOS (*it);
3485 bytepos = IT_STRING_BYTEPOS (*it);
3487 else
3489 ptrdiff_t pos;
3491 /* If end_charpos is out of range for some reason, such as a
3492 misbehaving display function, rationalize it (Bug#5984). */
3493 if (it->end_charpos > ZV)
3494 it->end_charpos = ZV;
3495 it->stop_charpos = it->end_charpos;
3497 /* If next overlay change is in front of the current stop pos
3498 (which is IT->end_charpos), stop there. Note: value of
3499 next_overlay_change is point-max if no overlay change
3500 follows. */
3501 charpos = IT_CHARPOS (*it);
3502 bytepos = IT_BYTEPOS (*it);
3503 pos = next_overlay_change (charpos);
3504 if (pos < it->stop_charpos)
3505 it->stop_charpos = pos;
3507 /* Set up variables for computing the stop position from text
3508 property changes. */
3509 XSETBUFFER (object, current_buffer);
3510 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3513 /* Get the interval containing IT's position. Value is a null
3514 interval if there isn't such an interval. */
3515 position = make_number (charpos);
3516 iv = validate_interval_range (object, &position, &position, false);
3517 if (iv)
3519 Lisp_Object values_here[LAST_PROP_IDX];
3520 struct props *p;
3522 /* Get properties here. */
3523 for (p = it_props; p->handler; ++p)
3524 values_here[p->idx] = textget (iv->plist,
3525 builtin_lisp_symbol (p->name));
3527 /* Look for an interval following iv that has different
3528 properties. */
3529 for (next_iv = next_interval (iv);
3530 (next_iv
3531 && (NILP (limit)
3532 || XFASTINT (limit) > next_iv->position));
3533 next_iv = next_interval (next_iv))
3535 for (p = it_props; p->handler; ++p)
3537 Lisp_Object new_value = textget (next_iv->plist,
3538 builtin_lisp_symbol (p->name));
3539 if (!EQ (values_here[p->idx], new_value))
3540 break;
3543 if (p->handler)
3544 break;
3547 if (next_iv)
3549 if (INTEGERP (limit)
3550 && next_iv->position >= XFASTINT (limit))
3551 /* No text property change up to limit. */
3552 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3553 else
3554 /* Text properties change in next_iv. */
3555 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3559 if (it->cmp_it.id < 0)
3561 ptrdiff_t stoppos = it->end_charpos;
3563 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3564 stoppos = -1;
3565 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3566 stoppos, it->string);
3569 eassert (STRINGP (it->string)
3570 || (it->stop_charpos >= BEGV
3571 && it->stop_charpos >= IT_CHARPOS (*it)));
3575 /* Return the position of the next overlay change after POS in
3576 current_buffer. Value is point-max if no overlay change
3577 follows. This is like `next-overlay-change' but doesn't use
3578 xmalloc. */
3580 static ptrdiff_t
3581 next_overlay_change (ptrdiff_t pos)
3583 ptrdiff_t i, noverlays;
3584 ptrdiff_t endpos;
3585 Lisp_Object *overlays;
3586 USE_SAFE_ALLOCA;
3588 /* Get all overlays at the given position. */
3589 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3591 /* If any of these overlays ends before endpos,
3592 use its ending point instead. */
3593 for (i = 0; i < noverlays; ++i)
3595 Lisp_Object oend;
3596 ptrdiff_t oendpos;
3598 oend = OVERLAY_END (overlays[i]);
3599 oendpos = OVERLAY_POSITION (oend);
3600 endpos = min (endpos, oendpos);
3603 SAFE_FREE ();
3604 return endpos;
3607 /* How many characters forward to search for a display property or
3608 display string. Searching too far forward makes the bidi display
3609 sluggish, especially in small windows. */
3610 #define MAX_DISP_SCAN 250
3612 /* Return the character position of a display string at or after
3613 position specified by POSITION. If no display string exists at or
3614 after POSITION, return ZV. A display string is either an overlay
3615 with `display' property whose value is a string, or a `display'
3616 text property whose value is a string. STRING is data about the
3617 string to iterate; if STRING->lstring is nil, we are iterating a
3618 buffer. FRAME_WINDOW_P is true when we are displaying a window
3619 on a GUI frame. DISP_PROP is set to zero if we searched
3620 MAX_DISP_SCAN characters forward without finding any display
3621 strings, non-zero otherwise. It is set to 2 if the display string
3622 uses any kind of `(space ...)' spec that will produce a stretch of
3623 white space in the text area. */
3624 ptrdiff_t
3625 compute_display_string_pos (struct text_pos *position,
3626 struct bidi_string_data *string,
3627 struct window *w,
3628 bool frame_window_p, int *disp_prop)
3630 /* OBJECT = nil means current buffer. */
3631 Lisp_Object object, object1;
3632 Lisp_Object pos, spec, limpos;
3633 bool string_p = string && (STRINGP (string->lstring) || string->s);
3634 ptrdiff_t eob = string_p ? string->schars : ZV;
3635 ptrdiff_t begb = string_p ? 0 : BEGV;
3636 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3637 ptrdiff_t lim =
3638 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3639 struct text_pos tpos;
3640 int rv = 0;
3642 if (string && STRINGP (string->lstring))
3643 object1 = object = string->lstring;
3644 else if (w && !string_p)
3646 XSETWINDOW (object, w);
3647 object1 = Qnil;
3649 else
3650 object1 = object = Qnil;
3652 *disp_prop = 1;
3654 if (charpos >= eob
3655 /* We don't support display properties whose values are strings
3656 that have display string properties. */
3657 || string->from_disp_str
3658 /* C strings cannot have display properties. */
3659 || (string->s && !STRINGP (object)))
3661 *disp_prop = 0;
3662 return eob;
3665 /* If the character at CHARPOS is where the display string begins,
3666 return CHARPOS. */
3667 pos = make_number (charpos);
3668 if (STRINGP (object))
3669 bufpos = string->bufpos;
3670 else
3671 bufpos = charpos;
3672 tpos = *position;
3673 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3674 && (charpos <= begb
3675 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3676 object),
3677 spec))
3678 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3679 frame_window_p)))
3681 if (rv == 2)
3682 *disp_prop = 2;
3683 return charpos;
3686 /* Look forward for the first character with a `display' property
3687 that will replace the underlying text when displayed. */
3688 limpos = make_number (lim);
3689 do {
3690 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3691 CHARPOS (tpos) = XFASTINT (pos);
3692 if (CHARPOS (tpos) >= lim)
3694 *disp_prop = 0;
3695 break;
3697 if (STRINGP (object))
3698 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3699 else
3700 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3701 spec = Fget_char_property (pos, Qdisplay, object);
3702 if (!STRINGP (object))
3703 bufpos = CHARPOS (tpos);
3704 } while (NILP (spec)
3705 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3706 bufpos, frame_window_p)));
3707 if (rv == 2)
3708 *disp_prop = 2;
3710 return CHARPOS (tpos);
3713 /* Return the character position of the end of the display string that
3714 started at CHARPOS. If there's no display string at CHARPOS,
3715 return -1. A display string is either an overlay with `display'
3716 property whose value is a string or a `display' text property whose
3717 value is a string. */
3718 ptrdiff_t
3719 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3721 /* OBJECT = nil means current buffer. */
3722 Lisp_Object object =
3723 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3724 Lisp_Object pos = make_number (charpos);
3725 ptrdiff_t eob =
3726 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3728 if (charpos >= eob || (string->s && !STRINGP (object)))
3729 return eob;
3731 /* It could happen that the display property or overlay was removed
3732 since we found it in compute_display_string_pos above. One way
3733 this can happen is if JIT font-lock was called (through
3734 handle_fontified_prop), and jit-lock-functions remove text
3735 properties or overlays from the portion of buffer that includes
3736 CHARPOS. Muse mode is known to do that, for example. In this
3737 case, we return -1 to the caller, to signal that no display
3738 string is actually present at CHARPOS. See bidi_fetch_char for
3739 how this is handled.
3741 An alternative would be to never look for display properties past
3742 it->stop_charpos. But neither compute_display_string_pos nor
3743 bidi_fetch_char that calls it know or care where the next
3744 stop_charpos is. */
3745 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3746 return -1;
3748 /* Look forward for the first character where the `display' property
3749 changes. */
3750 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3752 return XFASTINT (pos);
3757 /***********************************************************************
3758 Fontification
3759 ***********************************************************************/
3761 /* Handle changes in the `fontified' property of the current buffer by
3762 calling hook functions from Qfontification_functions to fontify
3763 regions of text. */
3765 static enum prop_handled
3766 handle_fontified_prop (struct it *it)
3768 Lisp_Object prop, pos;
3769 enum prop_handled handled = HANDLED_NORMALLY;
3771 if (!NILP (Vmemory_full))
3772 return handled;
3774 /* Get the value of the `fontified' property at IT's current buffer
3775 position. (The `fontified' property doesn't have a special
3776 meaning in strings.) If the value is nil, call functions from
3777 Qfontification_functions. */
3778 if (!STRINGP (it->string)
3779 && it->s == NULL
3780 && !NILP (Vfontification_functions)
3781 && !NILP (Vrun_hooks)
3782 && (pos = make_number (IT_CHARPOS (*it)),
3783 prop = Fget_char_property (pos, Qfontified, Qnil),
3784 /* Ignore the special cased nil value always present at EOB since
3785 no amount of fontifying will be able to change it. */
3786 NILP (prop) && IT_CHARPOS (*it) < Z))
3788 ptrdiff_t count = SPECPDL_INDEX ();
3789 Lisp_Object val;
3790 struct buffer *obuf = current_buffer;
3791 ptrdiff_t begv = BEGV, zv = ZV;
3792 bool old_clip_changed = current_buffer->clip_changed;
3794 val = Vfontification_functions;
3795 specbind (Qfontification_functions, Qnil);
3797 eassert (it->end_charpos == ZV);
3799 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3800 safe_call1 (val, pos);
3801 else
3803 Lisp_Object fns, fn;
3805 fns = Qnil;
3807 for (; CONSP (val); val = XCDR (val))
3809 fn = XCAR (val);
3811 if (EQ (fn, Qt))
3813 /* A value of t indicates this hook has a local
3814 binding; it means to run the global binding too.
3815 In a global value, t should not occur. If it
3816 does, we must ignore it to avoid an endless
3817 loop. */
3818 for (fns = Fdefault_value (Qfontification_functions);
3819 CONSP (fns);
3820 fns = XCDR (fns))
3822 fn = XCAR (fns);
3823 if (!EQ (fn, Qt))
3824 safe_call1 (fn, pos);
3827 else
3828 safe_call1 (fn, pos);
3832 unbind_to (count, Qnil);
3834 /* Fontification functions routinely call `save-restriction'.
3835 Normally, this tags clip_changed, which can confuse redisplay
3836 (see discussion in Bug#6671). Since we don't perform any
3837 special handling of fontification changes in the case where
3838 `save-restriction' isn't called, there's no point doing so in
3839 this case either. So, if the buffer's restrictions are
3840 actually left unchanged, reset clip_changed. */
3841 if (obuf == current_buffer)
3843 if (begv == BEGV && zv == ZV)
3844 current_buffer->clip_changed = old_clip_changed;
3846 /* There isn't much we can reasonably do to protect against
3847 misbehaving fontification, but here's a fig leaf. */
3848 else if (BUFFER_LIVE_P (obuf))
3849 set_buffer_internal_1 (obuf);
3851 /* The fontification code may have added/removed text.
3852 It could do even a lot worse, but let's at least protect against
3853 the most obvious case where only the text past `pos' gets changed',
3854 as is/was done in grep.el where some escapes sequences are turned
3855 into face properties (bug#7876). */
3856 it->end_charpos = ZV;
3858 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3859 something. This avoids an endless loop if they failed to
3860 fontify the text for which reason ever. */
3861 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3862 handled = HANDLED_RECOMPUTE_PROPS;
3865 return handled;
3870 /***********************************************************************
3871 Faces
3872 ***********************************************************************/
3874 /* Set up iterator IT from face properties at its current position.
3875 Called from handle_stop. */
3877 static enum prop_handled
3878 handle_face_prop (struct it *it)
3880 int new_face_id;
3881 ptrdiff_t next_stop;
3883 if (!STRINGP (it->string))
3885 new_face_id
3886 = face_at_buffer_position (it->w,
3887 IT_CHARPOS (*it),
3888 &next_stop,
3889 (IT_CHARPOS (*it)
3890 + TEXT_PROP_DISTANCE_LIMIT),
3891 false, it->base_face_id);
3893 /* Is this a start of a run of characters with box face?
3894 Caveat: this can be called for a freshly initialized
3895 iterator; face_id is -1 in this case. We know that the new
3896 face will not change until limit, i.e. if the new face has a
3897 box, all characters up to limit will have one. But, as
3898 usual, we don't know whether limit is really the end. */
3899 if (new_face_id != it->face_id)
3901 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3902 /* If it->face_id is -1, old_face below will be NULL, see
3903 the definition of FACE_FROM_ID_OR_NULL. This will happen
3904 if this is the initial call that gets the face. */
3905 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3907 /* If the value of face_id of the iterator is -1, we have to
3908 look in front of IT's position and see whether there is a
3909 face there that's different from new_face_id. */
3910 if (!old_face && IT_CHARPOS (*it) > BEG)
3912 int prev_face_id = face_before_it_pos (it);
3914 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3917 /* If the new face has a box, but the old face does not,
3918 this is the start of a run of characters with box face,
3919 i.e. this character has a shadow on the left side. */
3920 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3921 && (old_face == NULL || !old_face->box));
3922 it->face_box_p = new_face->box != FACE_NO_BOX;
3925 else
3927 int base_face_id;
3928 ptrdiff_t bufpos;
3929 int i;
3930 Lisp_Object from_overlay
3931 = (it->current.overlay_string_index >= 0
3932 ? it->string_overlays[it->current.overlay_string_index
3933 % OVERLAY_STRING_CHUNK_SIZE]
3934 : Qnil);
3936 /* See if we got to this string directly or indirectly from
3937 an overlay property. That includes the before-string or
3938 after-string of an overlay, strings in display properties
3939 provided by an overlay, their text properties, etc.
3941 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3942 if (! NILP (from_overlay))
3943 for (i = it->sp - 1; i >= 0; i--)
3945 if (it->stack[i].current.overlay_string_index >= 0)
3946 from_overlay
3947 = it->string_overlays[it->stack[i].current.overlay_string_index
3948 % OVERLAY_STRING_CHUNK_SIZE];
3949 else if (! NILP (it->stack[i].from_overlay))
3950 from_overlay = it->stack[i].from_overlay;
3952 if (!NILP (from_overlay))
3953 break;
3956 if (! NILP (from_overlay))
3958 bufpos = IT_CHARPOS (*it);
3959 /* For a string from an overlay, the base face depends
3960 only on text properties and ignores overlays. */
3961 base_face_id
3962 = face_for_overlay_string (it->w,
3963 IT_CHARPOS (*it),
3964 &next_stop,
3965 (IT_CHARPOS (*it)
3966 + TEXT_PROP_DISTANCE_LIMIT),
3967 false,
3968 from_overlay);
3970 else
3972 bufpos = 0;
3974 /* For strings from a `display' property, use the face at
3975 IT's current buffer position as the base face to merge
3976 with, so that overlay strings appear in the same face as
3977 surrounding text, unless they specify their own faces.
3978 For strings from wrap-prefix and line-prefix properties,
3979 use the default face, possibly remapped via
3980 Vface_remapping_alist. */
3981 /* Note that the fact that we use the face at _buffer_
3982 position means that a 'display' property on an overlay
3983 string will not inherit the face of that overlay string,
3984 but will instead revert to the face of buffer text
3985 covered by the overlay. This is visible, e.g., when the
3986 overlay specifies a box face, but neither the buffer nor
3987 the display string do. This sounds like a design bug,
3988 but Emacs always did that since v21.1, so changing that
3989 might be a big deal. */
3990 base_face_id = it->string_from_prefix_prop_p
3991 ? (!NILP (Vface_remapping_alist)
3992 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3993 : DEFAULT_FACE_ID)
3994 : underlying_face_id (it);
3997 new_face_id = face_at_string_position (it->w,
3998 it->string,
3999 IT_STRING_CHARPOS (*it),
4000 bufpos,
4001 &next_stop,
4002 base_face_id, false);
4004 /* Is this a start of a run of characters with box? Caveat:
4005 this can be called for a freshly allocated iterator; face_id
4006 is -1 is this case. We know that the new face will not
4007 change until the next check pos, i.e. if the new face has a
4008 box, all characters up to that position will have a
4009 box. But, as usual, we don't know whether that position
4010 is really the end. */
4011 if (new_face_id != it->face_id)
4013 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4014 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4016 /* If new face has a box but old face hasn't, this is the
4017 start of a run of characters with box, i.e. it has a
4018 shadow on the left side. */
4019 it->start_of_box_run_p
4020 = new_face->box && (old_face == NULL || !old_face->box);
4021 it->face_box_p = new_face->box != FACE_NO_BOX;
4025 it->face_id = new_face_id;
4026 return HANDLED_NORMALLY;
4030 /* Return the ID of the face ``underlying'' IT's current position,
4031 which is in a string. If the iterator is associated with a
4032 buffer, return the face at IT's current buffer position.
4033 Otherwise, use the iterator's base_face_id. */
4035 static int
4036 underlying_face_id (struct it *it)
4038 int face_id = it->base_face_id, i;
4040 eassert (STRINGP (it->string));
4042 for (i = it->sp - 1; i >= 0; --i)
4043 if (NILP (it->stack[i].string))
4044 face_id = it->stack[i].face_id;
4046 return face_id;
4050 /* Compute the face one character before or after the current position
4051 of IT, in the visual order. BEFORE_P means get the face
4052 in front (to the left in L2R paragraphs, to the right in R2L
4053 paragraphs) of IT's screen position. Value is the ID of the face. */
4055 static int
4056 face_before_or_after_it_pos (struct it *it, bool before_p)
4058 int face_id, limit;
4059 ptrdiff_t next_check_charpos;
4060 struct it it_copy;
4061 void *it_copy_data = NULL;
4063 eassert (it->s == NULL);
4065 if (STRINGP (it->string))
4067 ptrdiff_t bufpos, charpos;
4068 int base_face_id;
4070 /* No face change past the end of the string (for the case
4071 we are padding with spaces). No face change before the
4072 string start. */
4073 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4074 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4075 return it->face_id;
4077 if (!it->bidi_p)
4079 /* Set charpos to the position before or after IT's current
4080 position, in the logical order, which in the non-bidi
4081 case is the same as the visual order. */
4082 if (before_p)
4083 charpos = IT_STRING_CHARPOS (*it) - 1;
4084 else if (it->what == IT_COMPOSITION)
4085 /* For composition, we must check the character after the
4086 composition. */
4087 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4088 else
4089 charpos = IT_STRING_CHARPOS (*it) + 1;
4091 else
4093 if (before_p)
4095 /* With bidi iteration, the character before the current
4096 in the visual order cannot be found by simple
4097 iteration, because "reverse" reordering is not
4098 supported. Instead, we need to start from the string
4099 beginning and go all the way to the current string
4100 position, remembering the previous position. */
4101 /* Ignore face changes before the first visible
4102 character on this display line. */
4103 if (it->current_x <= it->first_visible_x)
4104 return it->face_id;
4105 SAVE_IT (it_copy, *it, it_copy_data);
4106 IT_STRING_CHARPOS (it_copy) = 0;
4107 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4111 charpos = IT_STRING_CHARPOS (it_copy);
4112 if (charpos >= SCHARS (it->string))
4113 break;
4114 bidi_move_to_visually_next (&it_copy.bidi_it);
4116 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4118 RESTORE_IT (it, it, it_copy_data);
4120 else
4122 /* Set charpos to the string position of the character
4123 that comes after IT's current position in the visual
4124 order. */
4125 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4127 it_copy = *it;
4128 while (n--)
4129 bidi_move_to_visually_next (&it_copy.bidi_it);
4131 charpos = it_copy.bidi_it.charpos;
4134 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4136 if (it->current.overlay_string_index >= 0)
4137 bufpos = IT_CHARPOS (*it);
4138 else
4139 bufpos = 0;
4141 base_face_id = underlying_face_id (it);
4143 /* Get the face for ASCII, or unibyte. */
4144 face_id = face_at_string_position (it->w,
4145 it->string,
4146 charpos,
4147 bufpos,
4148 &next_check_charpos,
4149 base_face_id, false);
4151 /* Correct the face for charsets different from ASCII. Do it
4152 for the multibyte case only. The face returned above is
4153 suitable for unibyte text if IT->string is unibyte. */
4154 if (STRING_MULTIBYTE (it->string))
4156 struct text_pos pos1 = string_pos (charpos, it->string);
4157 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4158 int c, len;
4159 struct face *face = FACE_FROM_ID (it->f, face_id);
4161 c = string_char_and_length (p, &len);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4165 else
4167 struct text_pos pos;
4169 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4170 || (IT_CHARPOS (*it) <= BEGV && before_p))
4171 return it->face_id;
4173 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4174 pos = it->current.pos;
4176 if (!it->bidi_p)
4178 if (before_p)
4179 DEC_TEXT_POS (pos, it->multibyte_p);
4180 else
4182 if (it->what == IT_COMPOSITION)
4184 /* For composition, we must check the position after
4185 the composition. */
4186 pos.charpos += it->cmp_it.nchars;
4187 pos.bytepos += it->len;
4189 else
4190 INC_TEXT_POS (pos, it->multibyte_p);
4193 else
4195 if (before_p)
4197 int current_x;
4199 /* With bidi iteration, the character before the current
4200 in the visual order cannot be found by simple
4201 iteration, because "reverse" reordering is not
4202 supported. Instead, we need to use the move_it_*
4203 family of functions, and move to the previous
4204 character starting from the beginning of the visual
4205 line. */
4206 /* Ignore face changes before the first visible
4207 character on this display line. */
4208 if (it->current_x <= it->first_visible_x)
4209 return it->face_id;
4210 SAVE_IT (it_copy, *it, it_copy_data);
4211 /* Implementation note: Since move_it_in_display_line
4212 works in the iterator geometry, and thinks the first
4213 character is always the leftmost, even in R2L lines,
4214 we don't need to distinguish between the R2L and L2R
4215 cases here. */
4216 current_x = it_copy.current_x;
4217 move_it_vertically_backward (&it_copy, 0);
4218 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4219 pos = it_copy.current.pos;
4220 RESTORE_IT (it, it, it_copy_data);
4222 else
4224 /* Set charpos to the buffer position of the character
4225 that comes after IT's current position in the visual
4226 order. */
4227 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4229 it_copy = *it;
4230 while (n--)
4231 bidi_move_to_visually_next (&it_copy.bidi_it);
4233 SET_TEXT_POS (pos,
4234 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4237 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4239 /* Determine face for CHARSET_ASCII, or unibyte. */
4240 face_id = face_at_buffer_position (it->w,
4241 CHARPOS (pos),
4242 &next_check_charpos,
4243 limit, false, -1);
4245 /* Correct the face for charsets different from ASCII. Do it
4246 for the multibyte case only. The face returned above is
4247 suitable for unibyte text if current_buffer is unibyte. */
4248 if (it->multibyte_p)
4250 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4251 struct face *face = FACE_FROM_ID (it->f, face_id);
4252 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4256 return face_id;
4261 /***********************************************************************
4262 Invisible text
4263 ***********************************************************************/
4265 /* Set up iterator IT from invisible properties at its current
4266 position. Called from handle_stop. */
4268 static enum prop_handled
4269 handle_invisible_prop (struct it *it)
4271 enum prop_handled handled = HANDLED_NORMALLY;
4272 int invis;
4273 Lisp_Object prop;
4275 if (STRINGP (it->string))
4277 Lisp_Object end_charpos, limit;
4279 /* Get the value of the invisible text property at the
4280 current position. Value will be nil if there is no such
4281 property. */
4282 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4283 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4284 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4286 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4288 /* Record whether we have to display an ellipsis for the
4289 invisible text. */
4290 bool display_ellipsis_p = (invis == 2);
4291 ptrdiff_t len, endpos;
4293 handled = HANDLED_RECOMPUTE_PROPS;
4295 /* Get the position at which the next visible text can be
4296 found in IT->string, if any. */
4297 endpos = len = SCHARS (it->string);
4298 XSETINT (limit, len);
4301 end_charpos
4302 = Fnext_single_property_change (end_charpos, Qinvisible,
4303 it->string, limit);
4304 /* Since LIMIT is always an integer, so should be the
4305 value returned by Fnext_single_property_change. */
4306 eassert (INTEGERP (end_charpos));
4307 if (INTEGERP (end_charpos))
4309 endpos = XFASTINT (end_charpos);
4310 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4311 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4312 if (invis == 2)
4313 display_ellipsis_p = true;
4315 else /* Should never happen; but if it does, exit the loop. */
4316 endpos = len;
4318 while (invis != 0 && endpos < len);
4320 if (display_ellipsis_p)
4321 it->ellipsis_p = true;
4323 if (endpos < len)
4325 /* Text at END_CHARPOS is visible. Move IT there. */
4326 struct text_pos old;
4327 ptrdiff_t oldpos;
4329 old = it->current.string_pos;
4330 oldpos = CHARPOS (old);
4331 if (it->bidi_p)
4333 if (it->bidi_it.first_elt
4334 && it->bidi_it.charpos < SCHARS (it->string))
4335 bidi_paragraph_init (it->paragraph_embedding,
4336 &it->bidi_it, true);
4337 /* Bidi-iterate out of the invisible text. */
4340 bidi_move_to_visually_next (&it->bidi_it);
4342 while (oldpos <= it->bidi_it.charpos
4343 && it->bidi_it.charpos < endpos
4344 && it->bidi_it.charpos < it->bidi_it.string.schars);
4346 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4348 if (IT_CHARPOS (*it) >= endpos)
4349 it->prev_stop = endpos;
4351 else
4353 IT_STRING_CHARPOS (*it) = endpos;
4354 compute_string_pos (&it->current.string_pos, old, it->string);
4357 else
4359 /* The rest of the string is invisible. If this is an
4360 overlay string, proceed with the next overlay string
4361 or whatever comes and return a character from there. */
4362 if (it->current.overlay_string_index >= 0
4363 && !display_ellipsis_p)
4365 next_overlay_string (it);
4366 /* Don't check for overlay strings when we just
4367 finished processing them. */
4368 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4370 else
4372 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4373 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4378 else
4380 ptrdiff_t newpos, next_stop, start_charpos, tem;
4381 Lisp_Object pos, overlay;
4383 /* First of all, is there invisible text at this position? */
4384 tem = start_charpos = IT_CHARPOS (*it);
4385 pos = make_number (tem);
4386 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4387 &overlay);
4388 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4390 /* If we are on invisible text, skip over it. */
4391 if (invis != 0 && start_charpos < it->end_charpos)
4393 /* Record whether we have to display an ellipsis for the
4394 invisible text. */
4395 bool display_ellipsis_p = invis == 2;
4397 handled = HANDLED_RECOMPUTE_PROPS;
4399 /* Loop skipping over invisible text. The loop is left at
4400 ZV or with IT on the first char being visible again. */
4403 /* Try to skip some invisible text. Return value is the
4404 position reached which can be equal to where we start
4405 if there is nothing invisible there. This skips both
4406 over invisible text properties and overlays with
4407 invisible property. */
4408 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4410 /* If we skipped nothing at all we weren't at invisible
4411 text in the first place. If everything to the end of
4412 the buffer was skipped, end the loop. */
4413 if (newpos == tem || newpos >= ZV)
4414 invis = 0;
4415 else
4417 /* We skipped some characters but not necessarily
4418 all there are. Check if we ended up on visible
4419 text. Fget_char_property returns the property of
4420 the char before the given position, i.e. if we
4421 get invis = 0, this means that the char at
4422 newpos is visible. */
4423 pos = make_number (newpos);
4424 prop = Fget_char_property (pos, Qinvisible, it->window);
4425 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4428 /* If we ended up on invisible text, proceed to
4429 skip starting with next_stop. */
4430 if (invis != 0)
4431 tem = next_stop;
4433 /* If there are adjacent invisible texts, don't lose the
4434 second one's ellipsis. */
4435 if (invis == 2)
4436 display_ellipsis_p = true;
4438 while (invis != 0);
4440 /* The position newpos is now either ZV or on visible text. */
4441 if (it->bidi_p)
4443 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4444 bool on_newline
4445 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4446 bool after_newline
4447 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4449 /* If the invisible text ends on a newline or on a
4450 character after a newline, we can avoid the costly,
4451 character by character, bidi iteration to NEWPOS, and
4452 instead simply reseat the iterator there. That's
4453 because all bidi reordering information is tossed at
4454 the newline. This is a big win for modes that hide
4455 complete lines, like Outline, Org, etc. */
4456 if (on_newline || after_newline)
4458 struct text_pos tpos;
4459 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4461 SET_TEXT_POS (tpos, newpos, bpos);
4462 reseat_1 (it, tpos, false);
4463 /* If we reseat on a newline/ZV, we need to prep the
4464 bidi iterator for advancing to the next character
4465 after the newline/EOB, keeping the current paragraph
4466 direction (so that PRODUCE_GLYPHS does TRT wrt
4467 prepending/appending glyphs to a glyph row). */
4468 if (on_newline)
4470 it->bidi_it.first_elt = false;
4471 it->bidi_it.paragraph_dir = pdir;
4472 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4473 it->bidi_it.nchars = 1;
4474 it->bidi_it.ch_len = 1;
4477 else /* Must use the slow method. */
4479 /* With bidi iteration, the region of invisible text
4480 could start and/or end in the middle of a
4481 non-base embedding level. Therefore, we need to
4482 skip invisible text using the bidi iterator,
4483 starting at IT's current position, until we find
4484 ourselves outside of the invisible text.
4485 Skipping invisible text _after_ bidi iteration
4486 avoids affecting the visual order of the
4487 displayed text when invisible properties are
4488 added or removed. */
4489 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4491 /* If we were `reseat'ed to a new paragraph,
4492 determine the paragraph base direction. We
4493 need to do it now because
4494 next_element_from_buffer may not have a
4495 chance to do it, if we are going to skip any
4496 text at the beginning, which resets the
4497 FIRST_ELT flag. */
4498 bidi_paragraph_init (it->paragraph_embedding,
4499 &it->bidi_it, true);
4503 bidi_move_to_visually_next (&it->bidi_it);
4505 while (it->stop_charpos <= it->bidi_it.charpos
4506 && it->bidi_it.charpos < newpos);
4507 IT_CHARPOS (*it) = it->bidi_it.charpos;
4508 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4509 /* If we overstepped NEWPOS, record its position in
4510 the iterator, so that we skip invisible text if
4511 later the bidi iteration lands us in the
4512 invisible region again. */
4513 if (IT_CHARPOS (*it) >= newpos)
4514 it->prev_stop = newpos;
4517 else
4519 IT_CHARPOS (*it) = newpos;
4520 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4523 if (display_ellipsis_p)
4525 /* Make sure that the glyphs of the ellipsis will get
4526 correct `charpos' values. If we would not update
4527 it->position here, the glyphs would belong to the
4528 last visible character _before_ the invisible
4529 text, which confuses `set_cursor_from_row'.
4531 We use the last invisible position instead of the
4532 first because this way the cursor is always drawn on
4533 the first "." of the ellipsis, whenever PT is inside
4534 the invisible text. Otherwise the cursor would be
4535 placed _after_ the ellipsis when the point is after the
4536 first invisible character. */
4537 if (!STRINGP (it->object))
4539 it->position.charpos = newpos - 1;
4540 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4544 /* If there are before-strings at the start of invisible
4545 text, and the text is invisible because of a text
4546 property, arrange to show before-strings because 20.x did
4547 it that way. (If the text is invisible because of an
4548 overlay property instead of a text property, this is
4549 already handled in the overlay code.) */
4550 if (NILP (overlay)
4551 && get_overlay_strings (it, it->stop_charpos))
4553 handled = HANDLED_RECOMPUTE_PROPS;
4554 if (it->sp > 0)
4556 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4557 /* The call to get_overlay_strings above recomputes
4558 it->stop_charpos, but it only considers changes
4559 in properties and overlays beyond iterator's
4560 current position. This causes us to miss changes
4561 that happen exactly where the invisible property
4562 ended. So we play it safe here and force the
4563 iterator to check for potential stop positions
4564 immediately after the invisible text. Note that
4565 if get_overlay_strings returns true, it
4566 normally also pushed the iterator stack, so we
4567 need to update the stop position in the slot
4568 below the current one. */
4569 it->stack[it->sp - 1].stop_charpos
4570 = CHARPOS (it->stack[it->sp - 1].current.pos);
4573 else if (display_ellipsis_p)
4575 it->ellipsis_p = true;
4576 /* Let the ellipsis display before
4577 considering any properties of the following char.
4578 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4579 handled = HANDLED_RETURN;
4584 return handled;
4588 /* Make iterator IT return `...' next.
4589 Replaces LEN characters from buffer. */
4591 static void
4592 setup_for_ellipsis (struct it *it, int len)
4594 /* Use the display table definition for `...'. Invalid glyphs
4595 will be handled by the method returning elements from dpvec. */
4596 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4598 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4599 it->dpvec = v->contents;
4600 it->dpend = v->contents + v->header.size;
4602 else
4604 /* Default `...'. */
4605 it->dpvec = default_invis_vector;
4606 it->dpend = default_invis_vector + 3;
4609 it->dpvec_char_len = len;
4610 it->current.dpvec_index = 0;
4611 it->dpvec_face_id = -1;
4613 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4614 face as the preceding text. IT->saved_face_id was set in
4615 handle_stop to the face of the preceding character, and will be
4616 different from IT->face_id only if the invisible text skipped in
4617 handle_invisible_prop has some non-default face on its first
4618 character. We thus ignore the face of the invisible text when we
4619 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4620 if (it->saved_face_id >= 0)
4621 it->face_id = it->saved_face_id;
4623 /* If the ellipsis represents buffer text, it means we advanced in
4624 the buffer, so we should no longer ignore overlay strings. */
4625 if (it->method == GET_FROM_BUFFER)
4626 it->ignore_overlay_strings_at_pos_p = false;
4628 it->method = GET_FROM_DISPLAY_VECTOR;
4629 it->ellipsis_p = true;
4634 /***********************************************************************
4635 'display' property
4636 ***********************************************************************/
4638 /* Set up iterator IT from `display' property at its current position.
4639 Called from handle_stop.
4640 We return HANDLED_RETURN if some part of the display property
4641 overrides the display of the buffer text itself.
4642 Otherwise we return HANDLED_NORMALLY. */
4644 static enum prop_handled
4645 handle_display_prop (struct it *it)
4647 Lisp_Object propval, object, overlay;
4648 struct text_pos *position;
4649 ptrdiff_t bufpos;
4650 /* Nonzero if some property replaces the display of the text itself. */
4651 int display_replaced = 0;
4653 if (STRINGP (it->string))
4655 object = it->string;
4656 position = &it->current.string_pos;
4657 bufpos = CHARPOS (it->current.pos);
4659 else
4661 XSETWINDOW (object, it->w);
4662 position = &it->current.pos;
4663 bufpos = CHARPOS (*position);
4666 /* Reset those iterator values set from display property values. */
4667 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4668 it->space_width = Qnil;
4669 it->font_height = Qnil;
4670 it->voffset = 0;
4672 /* We don't support recursive `display' properties, i.e. string
4673 values that have a string `display' property, that have a string
4674 `display' property etc. */
4675 if (!it->string_from_display_prop_p)
4676 it->area = TEXT_AREA;
4678 propval = get_char_property_and_overlay (make_number (position->charpos),
4679 Qdisplay, object, &overlay);
4680 if (NILP (propval))
4681 return HANDLED_NORMALLY;
4682 /* Now OVERLAY is the overlay that gave us this property, or nil
4683 if it was a text property. */
4685 if (!STRINGP (it->string))
4686 object = it->w->contents;
4688 display_replaced = handle_display_spec (it, propval, object, overlay,
4689 position, bufpos,
4690 FRAME_WINDOW_P (it->f));
4691 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4694 /* Subroutine of handle_display_prop. Returns non-zero if the display
4695 specification in SPEC is a replacing specification, i.e. it would
4696 replace the text covered by `display' property with something else,
4697 such as an image or a display string. If SPEC includes any kind or
4698 `(space ...) specification, the value is 2; this is used by
4699 compute_display_string_pos, which see.
4701 See handle_single_display_spec for documentation of arguments.
4702 FRAME_WINDOW_P is true if the window being redisplayed is on a
4703 GUI frame; this argument is used only if IT is NULL, see below.
4705 IT can be NULL, if this is called by the bidi reordering code
4706 through compute_display_string_pos, which see. In that case, this
4707 function only examines SPEC, but does not otherwise "handle" it, in
4708 the sense that it doesn't set up members of IT from the display
4709 spec. */
4710 static int
4711 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4712 Lisp_Object overlay, struct text_pos *position,
4713 ptrdiff_t bufpos, bool frame_window_p)
4715 int replacing = 0;
4717 if (CONSP (spec)
4718 /* Simple specifications. */
4719 && !EQ (XCAR (spec), Qimage)
4720 #ifdef HAVE_XWIDGETS
4721 && !EQ (XCAR (spec), Qxwidget)
4722 #endif
4723 && !EQ (XCAR (spec), Qspace)
4724 && !EQ (XCAR (spec), Qwhen)
4725 && !EQ (XCAR (spec), Qslice)
4726 && !EQ (XCAR (spec), Qspace_width)
4727 && !EQ (XCAR (spec), Qheight)
4728 && !EQ (XCAR (spec), Qraise)
4729 /* Marginal area specifications. */
4730 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4731 && !EQ (XCAR (spec), Qleft_fringe)
4732 && !EQ (XCAR (spec), Qright_fringe)
4733 && !NILP (XCAR (spec)))
4735 for (; CONSP (spec); spec = XCDR (spec))
4737 int rv = handle_single_display_spec (it, XCAR (spec), object,
4738 overlay, position, bufpos,
4739 replacing, frame_window_p);
4740 if (rv != 0)
4742 replacing = rv;
4743 /* If some text in a string is replaced, `position' no
4744 longer points to the position of `object'. */
4745 if (!it || STRINGP (object))
4746 break;
4750 else if (VECTORP (spec))
4752 ptrdiff_t i;
4753 for (i = 0; i < ASIZE (spec); ++i)
4755 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4756 overlay, position, bufpos,
4757 replacing, frame_window_p);
4758 if (rv != 0)
4760 replacing = rv;
4761 /* If some text in a string is replaced, `position' no
4762 longer points to the position of `object'. */
4763 if (!it || STRINGP (object))
4764 break;
4768 else
4769 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4770 bufpos, 0, frame_window_p);
4771 return replacing;
4774 /* Value is the position of the end of the `display' property starting
4775 at START_POS in OBJECT. */
4777 static struct text_pos
4778 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4780 Lisp_Object end;
4781 struct text_pos end_pos;
4783 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4784 Qdisplay, object, Qnil);
4785 CHARPOS (end_pos) = XFASTINT (end);
4786 if (STRINGP (object))
4787 compute_string_pos (&end_pos, start_pos, it->string);
4788 else
4789 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4791 return end_pos;
4795 /* Set up IT from a single `display' property specification SPEC. OBJECT
4796 is the object in which the `display' property was found. *POSITION
4797 is the position in OBJECT at which the `display' property was found.
4798 BUFPOS is the buffer position of OBJECT (different from POSITION if
4799 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4800 previously saw a display specification which already replaced text
4801 display with something else, for example an image; we ignore such
4802 properties after the first one has been processed.
4804 OVERLAY is the overlay this `display' property came from,
4805 or nil if it was a text property.
4807 If SPEC is a `space' or `image' specification, and in some other
4808 cases too, set *POSITION to the position where the `display'
4809 property ends.
4811 If IT is NULL, only examine the property specification in SPEC, but
4812 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4813 is intended to be displayed in a window on a GUI frame.
4815 Value is non-zero if something was found which replaces the display
4816 of buffer or string text. */
4818 static int
4819 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4820 Lisp_Object overlay, struct text_pos *position,
4821 ptrdiff_t bufpos, int display_replaced,
4822 bool frame_window_p)
4824 Lisp_Object form;
4825 Lisp_Object location, value;
4826 struct text_pos start_pos = *position;
4828 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4829 If the result is non-nil, use VALUE instead of SPEC. */
4830 form = Qt;
4831 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4833 spec = XCDR (spec);
4834 if (!CONSP (spec))
4835 return 0;
4836 form = XCAR (spec);
4837 spec = XCDR (spec);
4840 if (!NILP (form) && !EQ (form, Qt))
4842 ptrdiff_t count = SPECPDL_INDEX ();
4844 /* Bind `object' to the object having the `display' property, a
4845 buffer or string. Bind `position' to the position in the
4846 object where the property was found, and `buffer-position'
4847 to the current position in the buffer. */
4849 if (NILP (object))
4850 XSETBUFFER (object, current_buffer);
4851 specbind (Qobject, object);
4852 specbind (Qposition, make_number (CHARPOS (*position)));
4853 specbind (Qbuffer_position, make_number (bufpos));
4854 form = safe_eval (form);
4855 unbind_to (count, Qnil);
4858 if (NILP (form))
4859 return 0;
4861 /* Handle `(height HEIGHT)' specifications. */
4862 if (CONSP (spec)
4863 && EQ (XCAR (spec), Qheight)
4864 && CONSP (XCDR (spec)))
4866 if (it)
4868 if (!FRAME_WINDOW_P (it->f))
4869 return 0;
4871 it->font_height = XCAR (XCDR (spec));
4872 if (!NILP (it->font_height))
4874 int new_height = -1;
4876 if (CONSP (it->font_height)
4877 && (EQ (XCAR (it->font_height), Qplus)
4878 || EQ (XCAR (it->font_height), Qminus))
4879 && CONSP (XCDR (it->font_height))
4880 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4882 /* `(+ N)' or `(- N)' where N is an integer. */
4883 int steps = XINT (XCAR (XCDR (it->font_height)));
4884 if (EQ (XCAR (it->font_height), Qplus))
4885 steps = - steps;
4886 it->face_id = smaller_face (it->f, it->face_id, steps);
4888 else if (FUNCTIONP (it->font_height))
4890 /* Call function with current height as argument.
4891 Value is the new height. */
4892 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4893 Lisp_Object height;
4894 height = safe_call1 (it->font_height,
4895 face->lface[LFACE_HEIGHT_INDEX]);
4896 if (NUMBERP (height))
4897 new_height = XFLOATINT (height);
4899 else if (NUMBERP (it->font_height))
4901 /* Value is a multiple of the canonical char height. */
4902 struct face *f;
4904 f = FACE_FROM_ID (it->f,
4905 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4906 new_height = (XFLOATINT (it->font_height)
4907 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4909 else
4911 /* Evaluate IT->font_height with `height' bound to the
4912 current specified height to get the new height. */
4913 ptrdiff_t count = SPECPDL_INDEX ();
4914 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4916 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4917 value = safe_eval (it->font_height);
4918 unbind_to (count, Qnil);
4920 if (NUMBERP (value))
4921 new_height = XFLOATINT (value);
4924 if (new_height > 0)
4925 it->face_id = face_with_height (it->f, it->face_id, new_height);
4929 return 0;
4932 /* Handle `(space-width WIDTH)'. */
4933 if (CONSP (spec)
4934 && EQ (XCAR (spec), Qspace_width)
4935 && CONSP (XCDR (spec)))
4937 if (it)
4939 if (!FRAME_WINDOW_P (it->f))
4940 return 0;
4942 value = XCAR (XCDR (spec));
4943 if (NUMBERP (value) && XFLOATINT (value) > 0)
4944 it->space_width = value;
4947 return 0;
4950 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4951 if (CONSP (spec)
4952 && EQ (XCAR (spec), Qslice))
4954 Lisp_Object tem;
4956 if (it)
4958 if (!FRAME_WINDOW_P (it->f))
4959 return 0;
4961 if (tem = XCDR (spec), CONSP (tem))
4963 it->slice.x = XCAR (tem);
4964 if (tem = XCDR (tem), CONSP (tem))
4966 it->slice.y = XCAR (tem);
4967 if (tem = XCDR (tem), CONSP (tem))
4969 it->slice.width = XCAR (tem);
4970 if (tem = XCDR (tem), CONSP (tem))
4971 it->slice.height = XCAR (tem);
4977 return 0;
4980 /* Handle `(raise FACTOR)'. */
4981 if (CONSP (spec)
4982 && EQ (XCAR (spec), Qraise)
4983 && CONSP (XCDR (spec)))
4985 if (it)
4987 if (!FRAME_WINDOW_P (it->f))
4988 return 0;
4990 #ifdef HAVE_WINDOW_SYSTEM
4991 value = XCAR (XCDR (spec));
4992 if (NUMBERP (value))
4994 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4995 it->voffset = - (XFLOATINT (value)
4996 * (normal_char_height (face->font, -1)));
4998 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 0;
5004 /* Don't handle the other kinds of display specifications
5005 inside a string that we got from a `display' property. */
5006 if (it && it->string_from_display_prop_p)
5007 return 0;
5009 /* Characters having this form of property are not displayed, so
5010 we have to find the end of the property. */
5011 if (it)
5013 start_pos = *position;
5014 *position = display_prop_end (it, object, start_pos);
5015 /* If the display property comes from an overlay, don't consider
5016 any potential stop_charpos values before the end of that
5017 overlay. Since display_prop_end will happily find another
5018 'display' property coming from some other overlay or text
5019 property on buffer positions before this overlay's end, we
5020 need to ignore them, or else we risk displaying this
5021 overlay's display string/image twice. */
5022 if (!NILP (overlay))
5024 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5026 /* Some borderline-sane Lisp might call us with the current
5027 buffer narrowed so that overlay-end is outside the
5028 POINT_MIN..POINT_MAX region, which will then cause
5029 various assertion violations and crashes down the road,
5030 starting with pop_it when it will attempt to use POSITION
5031 set below. Prevent that. */
5032 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5034 if (ovendpos > CHARPOS (*position))
5035 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5038 value = Qnil;
5040 /* Stop the scan at that end position--we assume that all
5041 text properties change there. */
5042 if (it)
5043 it->stop_charpos = position->charpos;
5045 /* Handle `(left-fringe BITMAP [FACE])'
5046 and `(right-fringe BITMAP [FACE])'. */
5047 if (CONSP (spec)
5048 && (EQ (XCAR (spec), Qleft_fringe)
5049 || EQ (XCAR (spec), Qright_fringe))
5050 && CONSP (XCDR (spec)))
5052 if (it)
5054 if (!FRAME_WINDOW_P (it->f))
5055 /* If we return here, POSITION has been advanced
5056 across the text with this property. */
5058 /* Synchronize the bidi iterator with POSITION. This is
5059 needed because we are not going to push the iterator
5060 on behalf of this display property, so there will be
5061 no pop_it call to do this synchronization for us. */
5062 if (it->bidi_p)
5064 it->position = *position;
5065 iterate_out_of_display_property (it);
5066 *position = it->position;
5068 return 1;
5071 else if (!frame_window_p)
5072 return 1;
5074 #ifdef HAVE_WINDOW_SYSTEM
5075 value = XCAR (XCDR (spec));
5076 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5077 if (! fringe_bitmap)
5078 /* If we return here, POSITION has been advanced
5079 across the text with this property. */
5081 if (it && it->bidi_p)
5083 it->position = *position;
5084 iterate_out_of_display_property (it);
5085 *position = it->position;
5087 return 1;
5090 if (it)
5092 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5094 if (CONSP (XCDR (XCDR (spec))))
5096 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5097 int face_id2 = lookup_derived_face (it->f, face_name,
5098 FRINGE_FACE_ID, false);
5099 if (face_id2 >= 0)
5100 face_id = face_id2;
5103 /* Save current settings of IT so that we can restore them
5104 when we are finished with the glyph property value. */
5105 push_it (it, position);
5107 it->area = TEXT_AREA;
5108 it->what = IT_IMAGE;
5109 it->image_id = -1; /* no image */
5110 it->position = start_pos;
5111 it->object = NILP (object) ? it->w->contents : object;
5112 it->method = GET_FROM_IMAGE;
5113 it->from_overlay = Qnil;
5114 it->face_id = face_id;
5115 it->from_disp_prop_p = true;
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5122 if (EQ (XCAR (spec), Qleft_fringe))
5124 it->left_user_fringe_bitmap = fringe_bitmap;
5125 it->left_user_fringe_face_id = face_id;
5127 else
5129 it->right_user_fringe_bitmap = fringe_bitmap;
5130 it->right_user_fringe_face_id = face_id;
5133 #endif /* HAVE_WINDOW_SYSTEM */
5134 return 1;
5137 /* Prepare to handle `((margin left-margin) ...)',
5138 `((margin right-margin) ...)' and `((margin nil) ...)'
5139 prefixes for display specifications. */
5140 location = Qunbound;
5141 if (CONSP (spec) && CONSP (XCAR (spec)))
5143 Lisp_Object tem;
5145 value = XCDR (spec);
5146 if (CONSP (value))
5147 value = XCAR (value);
5149 tem = XCAR (spec);
5150 if (EQ (XCAR (tem), Qmargin)
5151 && (tem = XCDR (tem),
5152 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5153 (NILP (tem)
5154 || EQ (tem, Qleft_margin)
5155 || EQ (tem, Qright_margin))))
5156 location = tem;
5159 if (EQ (location, Qunbound))
5161 location = Qnil;
5162 value = spec;
5165 /* After this point, VALUE is the property after any
5166 margin prefix has been stripped. It must be a string,
5167 an image specification, or `(space ...)'.
5169 LOCATION specifies where to display: `left-margin',
5170 `right-margin' or nil. */
5172 bool valid_p = (STRINGP (value)
5173 #ifdef HAVE_WINDOW_SYSTEM
5174 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5175 && valid_image_p (value))
5176 #endif /* not HAVE_WINDOW_SYSTEM */
5177 || (CONSP (value) && EQ (XCAR (value), Qspace))
5178 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5179 && valid_xwidget_spec_p (value)));
5181 if (valid_p && display_replaced == 0)
5183 int retval = 1;
5185 if (!it)
5187 /* Callers need to know whether the display spec is any kind
5188 of `(space ...)' spec that is about to affect text-area
5189 display. */
5190 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5191 retval = 2;
5192 return retval;
5195 /* Save current settings of IT so that we can restore them
5196 when we are finished with the glyph property value. */
5197 push_it (it, position);
5198 it->from_overlay = overlay;
5199 it->from_disp_prop_p = true;
5201 if (NILP (location))
5202 it->area = TEXT_AREA;
5203 else if (EQ (location, Qleft_margin))
5204 it->area = LEFT_MARGIN_AREA;
5205 else
5206 it->area = RIGHT_MARGIN_AREA;
5208 if (STRINGP (value))
5210 it->string = value;
5211 it->multibyte_p = STRING_MULTIBYTE (it->string);
5212 it->current.overlay_string_index = -1;
5213 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5214 it->end_charpos = it->string_nchars = SCHARS (it->string);
5215 it->method = GET_FROM_STRING;
5216 it->stop_charpos = 0;
5217 it->prev_stop = 0;
5218 it->base_level_stop = 0;
5219 it->string_from_display_prop_p = true;
5220 /* Say that we haven't consumed the characters with
5221 `display' property yet. The call to pop_it in
5222 set_iterator_to_next will clean this up. */
5223 if (BUFFERP (object))
5224 *position = start_pos;
5226 /* Force paragraph direction to be that of the parent
5227 object. If the parent object's paragraph direction is
5228 not yet determined, default to L2R. */
5229 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5230 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5231 else
5232 it->paragraph_embedding = L2R;
5234 /* Set up the bidi iterator for this display string. */
5235 if (it->bidi_p)
5237 it->bidi_it.string.lstring = it->string;
5238 it->bidi_it.string.s = NULL;
5239 it->bidi_it.string.schars = it->end_charpos;
5240 it->bidi_it.string.bufpos = bufpos;
5241 it->bidi_it.string.from_disp_str = true;
5242 it->bidi_it.string.unibyte = !it->multibyte_p;
5243 it->bidi_it.w = it->w;
5244 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5249 it->method = GET_FROM_STRETCH;
5250 it->object = value;
5251 *position = it->position = start_pos;
5252 retval = 1 + (it->area == TEXT_AREA);
5254 else if (valid_xwidget_spec_p (value))
5256 it->what = IT_XWIDGET;
5257 it->method = GET_FROM_XWIDGET;
5258 it->position = start_pos;
5259 it->object = NILP (object) ? it->w->contents : object;
5260 *position = start_pos;
5261 it->xwidget = lookup_xwidget (value);
5263 #ifdef HAVE_WINDOW_SYSTEM
5264 else
5266 it->what = IT_IMAGE;
5267 it->image_id = lookup_image (it->f, value);
5268 it->position = start_pos;
5269 it->object = NILP (object) ? it->w->contents : object;
5270 it->method = GET_FROM_IMAGE;
5272 /* Say that we haven't consumed the characters with
5273 `display' property yet. The call to pop_it in
5274 set_iterator_to_next will clean this up. */
5275 *position = start_pos;
5277 #endif /* HAVE_WINDOW_SYSTEM */
5279 return retval;
5282 /* Invalid property or property not supported. Restore
5283 POSITION to what it was before. */
5284 *position = start_pos;
5285 return 0;
5288 /* Check if PROP is a display property value whose text should be
5289 treated as intangible. OVERLAY is the overlay from which PROP
5290 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5291 specify the buffer position covered by PROP. */
5293 bool
5294 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5295 ptrdiff_t charpos, ptrdiff_t bytepos)
5297 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5298 struct text_pos position;
5300 SET_TEXT_POS (position, charpos, bytepos);
5301 return (handle_display_spec (NULL, prop, Qnil, overlay,
5302 &position, charpos, frame_window_p)
5303 != 0);
5307 /* Return true if PROP is a display sub-property value containing STRING.
5309 Implementation note: this and the following function are really
5310 special cases of handle_display_spec and
5311 handle_single_display_spec, and should ideally use the same code.
5312 Until they do, these two pairs must be consistent and must be
5313 modified in sync. */
5315 static bool
5316 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5318 if (EQ (string, prop))
5319 return true;
5321 /* Skip over `when FORM'. */
5322 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5324 prop = XCDR (prop);
5325 if (!CONSP (prop))
5326 return false;
5327 /* Actually, the condition following `when' should be eval'ed,
5328 like handle_single_display_spec does, and we should return
5329 false if it evaluates to nil. However, this function is
5330 called only when the buffer was already displayed and some
5331 glyph in the glyph matrix was found to come from a display
5332 string. Therefore, the condition was already evaluated, and
5333 the result was non-nil, otherwise the display string wouldn't
5334 have been displayed and we would have never been called for
5335 this property. Thus, we can skip the evaluation and assume
5336 its result is non-nil. */
5337 prop = XCDR (prop);
5340 if (CONSP (prop))
5341 /* Skip over `margin LOCATION'. */
5342 if (EQ (XCAR (prop), Qmargin))
5344 prop = XCDR (prop);
5345 if (!CONSP (prop))
5346 return false;
5348 prop = XCDR (prop);
5349 if (!CONSP (prop))
5350 return false;
5353 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5357 /* Return true if STRING appears in the `display' property PROP. */
5359 static bool
5360 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5362 if (CONSP (prop)
5363 && !EQ (XCAR (prop), Qwhen)
5364 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5366 /* A list of sub-properties. */
5367 while (CONSP (prop))
5369 if (single_display_spec_string_p (XCAR (prop), string))
5370 return true;
5371 prop = XCDR (prop);
5374 else if (VECTORP (prop))
5376 /* A vector of sub-properties. */
5377 ptrdiff_t i;
5378 for (i = 0; i < ASIZE (prop); ++i)
5379 if (single_display_spec_string_p (AREF (prop, i), string))
5380 return true;
5382 else
5383 return single_display_spec_string_p (prop, string);
5385 return false;
5388 /* Look for STRING in overlays and text properties in the current
5389 buffer, between character positions FROM and TO (excluding TO).
5390 BACK_P means look back (in this case, TO is supposed to be
5391 less than FROM).
5392 Value is the first character position where STRING was found, or
5393 zero if it wasn't found before hitting TO.
5395 This function may only use code that doesn't eval because it is
5396 called asynchronously from note_mouse_highlight. */
5398 static ptrdiff_t
5399 string_buffer_position_lim (Lisp_Object string,
5400 ptrdiff_t from, ptrdiff_t to, bool back_p)
5402 Lisp_Object limit, prop, pos;
5403 bool found = false;
5405 pos = make_number (max (from, BEGV));
5407 if (!back_p) /* looking forward */
5409 limit = make_number (min (to, ZV));
5410 while (!found && !EQ (pos, limit))
5412 prop = Fget_char_property (pos, Qdisplay, Qnil);
5413 if (!NILP (prop) && display_prop_string_p (prop, string))
5414 found = true;
5415 else
5416 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5417 limit);
5420 else /* looking back */
5422 limit = make_number (max (to, BEGV));
5423 while (!found && !EQ (pos, limit))
5425 prop = Fget_char_property (pos, Qdisplay, Qnil);
5426 if (!NILP (prop) && display_prop_string_p (prop, string))
5427 found = true;
5428 else
5429 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5430 limit);
5434 return found ? XINT (pos) : 0;
5437 /* Determine which buffer position in current buffer STRING comes from.
5438 AROUND_CHARPOS is an approximate position where it could come from.
5439 Value is the buffer position or 0 if it couldn't be determined.
5441 This function is necessary because we don't record buffer positions
5442 in glyphs generated from strings (to keep struct glyph small).
5443 This function may only use code that doesn't eval because it is
5444 called asynchronously from note_mouse_highlight. */
5446 static ptrdiff_t
5447 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5449 const int MAX_DISTANCE = 1000;
5450 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5451 around_charpos + MAX_DISTANCE,
5452 false);
5454 if (!found)
5455 found = string_buffer_position_lim (string, around_charpos,
5456 around_charpos - MAX_DISTANCE, true);
5457 return found;
5462 /***********************************************************************
5463 `composition' property
5464 ***********************************************************************/
5466 /* Set up iterator IT from `composition' property at its current
5467 position. Called from handle_stop. */
5469 static enum prop_handled
5470 handle_composition_prop (struct it *it)
5472 Lisp_Object prop, string;
5473 ptrdiff_t pos, pos_byte, start, end;
5475 if (STRINGP (it->string))
5477 unsigned char *s;
5479 pos = IT_STRING_CHARPOS (*it);
5480 pos_byte = IT_STRING_BYTEPOS (*it);
5481 string = it->string;
5482 s = SDATA (string) + pos_byte;
5483 it->c = STRING_CHAR (s);
5485 else
5487 pos = IT_CHARPOS (*it);
5488 pos_byte = IT_BYTEPOS (*it);
5489 string = Qnil;
5490 it->c = FETCH_CHAR (pos_byte);
5493 /* If there's a valid composition and point is not inside of the
5494 composition (in the case that the composition is from the current
5495 buffer), draw a glyph composed from the composition components. */
5496 if (find_composition (pos, -1, &start, &end, &prop, string)
5497 && composition_valid_p (start, end, prop)
5498 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5500 if (start < pos)
5501 /* As we can't handle this situation (perhaps font-lock added
5502 a new composition), we just return here hoping that next
5503 redisplay will detect this composition much earlier. */
5504 return HANDLED_NORMALLY;
5505 if (start != pos)
5507 if (STRINGP (it->string))
5508 pos_byte = string_char_to_byte (it->string, start);
5509 else
5510 pos_byte = CHAR_TO_BYTE (start);
5512 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5513 prop, string);
5515 if (it->cmp_it.id >= 0)
5517 it->cmp_it.ch = -1;
5518 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5519 it->cmp_it.nglyphs = -1;
5523 return HANDLED_NORMALLY;
5528 /***********************************************************************
5529 Overlay strings
5530 ***********************************************************************/
5532 /* The following structure is used to record overlay strings for
5533 later sorting in load_overlay_strings. */
5535 struct overlay_entry
5537 Lisp_Object overlay;
5538 Lisp_Object string;
5539 EMACS_INT priority;
5540 bool after_string_p;
5544 /* Set up iterator IT from overlay strings at its current position.
5545 Called from handle_stop. */
5547 static enum prop_handled
5548 handle_overlay_change (struct it *it)
5550 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5551 return HANDLED_RECOMPUTE_PROPS;
5552 else
5553 return HANDLED_NORMALLY;
5557 /* Set up the next overlay string for delivery by IT, if there is an
5558 overlay string to deliver. Called by set_iterator_to_next when the
5559 end of the current overlay string is reached. If there are more
5560 overlay strings to display, IT->string and
5561 IT->current.overlay_string_index are set appropriately here.
5562 Otherwise IT->string is set to nil. */
5564 static void
5565 next_overlay_string (struct it *it)
5567 ++it->current.overlay_string_index;
5568 if (it->current.overlay_string_index == it->n_overlay_strings)
5570 /* No more overlay strings. Restore IT's settings to what
5571 they were before overlay strings were processed, and
5572 continue to deliver from current_buffer. */
5574 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5575 pop_it (it);
5576 eassert (it->sp > 0
5577 || (NILP (it->string)
5578 && it->method == GET_FROM_BUFFER
5579 && it->stop_charpos >= BEGV
5580 && it->stop_charpos <= it->end_charpos));
5581 it->current.overlay_string_index = -1;
5582 it->n_overlay_strings = 0;
5583 /* If there's an empty display string on the stack, pop the
5584 stack, to resync the bidi iterator with IT's position. Such
5585 empty strings are pushed onto the stack in
5586 get_overlay_strings_1. */
5587 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5588 pop_it (it);
5590 /* Since we've exhausted overlay strings at this buffer
5591 position, set the flag to ignore overlays until we move to
5592 another position. (The flag will be reset in
5593 next_element_from_buffer.) But don't do that if the overlay
5594 strings were loaded at position other than the current one,
5595 which could happen if we called pop_it above, or if the
5596 overlay strings were loaded by handle_invisible_prop at the
5597 beginning of invisible text. */
5598 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5599 it->ignore_overlay_strings_at_pos_p = true;
5601 /* If we're at the end of the buffer, record that we have
5602 processed the overlay strings there already, so that
5603 next_element_from_buffer doesn't try it again. */
5604 if (NILP (it->string)
5605 && IT_CHARPOS (*it) >= it->end_charpos
5606 && it->overlay_strings_charpos >= it->end_charpos)
5607 it->overlay_strings_at_end_processed_p = true;
5608 /* Note: we reset overlay_strings_charpos only here, to make
5609 sure the just-processed overlays were indeed at EOB.
5610 Otherwise, overlays on text with invisible text property,
5611 which are processed with IT's position past the invisible
5612 text, might fool us into thinking the overlays at EOB were
5613 already processed (linum-mode can cause this, for
5614 example). */
5615 it->overlay_strings_charpos = -1;
5617 else
5619 /* There are more overlay strings to process. If
5620 IT->current.overlay_string_index has advanced to a position
5621 where we must load IT->overlay_strings with more strings, do
5622 it. We must load at the IT->overlay_strings_charpos where
5623 IT->n_overlay_strings was originally computed; when invisible
5624 text is present, this might not be IT_CHARPOS (Bug#7016). */
5625 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5627 if (it->current.overlay_string_index && i == 0)
5628 load_overlay_strings (it, it->overlay_strings_charpos);
5630 /* Initialize IT to deliver display elements from the overlay
5631 string. */
5632 it->string = it->overlay_strings[i];
5633 it->multibyte_p = STRING_MULTIBYTE (it->string);
5634 SET_TEXT_POS (it->current.string_pos, 0, 0);
5635 it->method = GET_FROM_STRING;
5636 it->stop_charpos = 0;
5637 it->end_charpos = SCHARS (it->string);
5638 if (it->cmp_it.stop_pos >= 0)
5639 it->cmp_it.stop_pos = 0;
5640 it->prev_stop = 0;
5641 it->base_level_stop = 0;
5643 /* Set up the bidi iterator for this overlay string. */
5644 if (it->bidi_p)
5646 it->bidi_it.string.lstring = it->string;
5647 it->bidi_it.string.s = NULL;
5648 it->bidi_it.string.schars = SCHARS (it->string);
5649 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5650 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5651 it->bidi_it.string.unibyte = !it->multibyte_p;
5652 it->bidi_it.w = it->w;
5653 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5657 CHECK_IT (it);
5661 /* Compare two overlay_entry structures E1 and E2. Used as a
5662 comparison function for qsort in load_overlay_strings. Overlay
5663 strings for the same position are sorted so that
5665 1. All after-strings come in front of before-strings, except
5666 when they come from the same overlay.
5668 2. Within after-strings, strings are sorted so that overlay strings
5669 from overlays with higher priorities come first.
5671 2. Within before-strings, strings are sorted so that overlay
5672 strings from overlays with higher priorities come last.
5674 Value is analogous to strcmp. */
5677 static int
5678 compare_overlay_entries (const void *e1, const void *e2)
5680 struct overlay_entry const *entry1 = e1;
5681 struct overlay_entry const *entry2 = e2;
5682 int result;
5684 if (entry1->after_string_p != entry2->after_string_p)
5686 /* Let after-strings appear in front of before-strings if
5687 they come from different overlays. */
5688 if (EQ (entry1->overlay, entry2->overlay))
5689 result = entry1->after_string_p ? 1 : -1;
5690 else
5691 result = entry1->after_string_p ? -1 : 1;
5693 else if (entry1->priority != entry2->priority)
5695 if (entry1->after_string_p)
5696 /* After-strings sorted in order of decreasing priority. */
5697 result = entry2->priority < entry1->priority ? -1 : 1;
5698 else
5699 /* Before-strings sorted in order of increasing priority. */
5700 result = entry1->priority < entry2->priority ? -1 : 1;
5702 else
5703 result = 0;
5705 return result;
5709 /* Load the vector IT->overlay_strings with overlay strings from IT's
5710 current buffer position, or from CHARPOS if that is > 0. Set
5711 IT->n_overlays to the total number of overlay strings found.
5713 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5714 a time. On entry into load_overlay_strings,
5715 IT->current.overlay_string_index gives the number of overlay
5716 strings that have already been loaded by previous calls to this
5717 function.
5719 IT->add_overlay_start contains an additional overlay start
5720 position to consider for taking overlay strings from, if non-zero.
5721 This position comes into play when the overlay has an `invisible'
5722 property, and both before and after-strings. When we've skipped to
5723 the end of the overlay, because of its `invisible' property, we
5724 nevertheless want its before-string to appear.
5725 IT->add_overlay_start will contain the overlay start position
5726 in this case.
5728 Overlay strings are sorted so that after-string strings come in
5729 front of before-string strings. Within before and after-strings,
5730 strings are sorted by overlay priority. See also function
5731 compare_overlay_entries. */
5733 static void
5734 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5736 Lisp_Object overlay, window, str, invisible;
5737 struct Lisp_Overlay *ov;
5738 ptrdiff_t start, end;
5739 ptrdiff_t n = 0, i, j;
5740 int invis;
5741 struct overlay_entry entriesbuf[20];
5742 ptrdiff_t size = ARRAYELTS (entriesbuf);
5743 struct overlay_entry *entries = entriesbuf;
5744 USE_SAFE_ALLOCA;
5746 if (charpos <= 0)
5747 charpos = IT_CHARPOS (*it);
5749 /* Append the overlay string STRING of overlay OVERLAY to vector
5750 `entries' which has size `size' and currently contains `n'
5751 elements. AFTER_P means STRING is an after-string of
5752 OVERLAY. */
5753 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5754 do \
5756 Lisp_Object priority; \
5758 if (n == size) \
5760 struct overlay_entry *old = entries; \
5761 SAFE_NALLOCA (entries, 2, size); \
5762 memcpy (entries, old, size * sizeof *entries); \
5763 size *= 2; \
5766 entries[n].string = (STRING); \
5767 entries[n].overlay = (OVERLAY); \
5768 priority = Foverlay_get ((OVERLAY), Qpriority); \
5769 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5770 entries[n].after_string_p = (AFTER_P); \
5771 ++n; \
5773 while (false)
5775 /* Process overlay before the overlay center. */
5776 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5778 XSETMISC (overlay, ov);
5779 eassert (OVERLAYP (overlay));
5780 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5781 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5783 if (end < charpos)
5784 break;
5786 /* Skip this overlay if it doesn't start or end at IT's current
5787 position. */
5788 if (end != charpos && start != charpos)
5789 continue;
5791 /* Skip this overlay if it doesn't apply to IT->w. */
5792 window = Foverlay_get (overlay, Qwindow);
5793 if (WINDOWP (window) && XWINDOW (window) != it->w)
5794 continue;
5796 /* If the text ``under'' the overlay is invisible, both before-
5797 and after-strings from this overlay are visible; start and
5798 end position are indistinguishable. */
5799 invisible = Foverlay_get (overlay, Qinvisible);
5800 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5802 /* If overlay has a non-empty before-string, record it. */
5803 if ((start == charpos || (end == charpos && invis != 0))
5804 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5805 && SCHARS (str))
5806 RECORD_OVERLAY_STRING (overlay, str, false);
5808 /* If overlay has a non-empty after-string, record it. */
5809 if ((end == charpos || (start == charpos && invis != 0))
5810 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5811 && SCHARS (str))
5812 RECORD_OVERLAY_STRING (overlay, str, true);
5815 /* Process overlays after the overlay center. */
5816 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5818 XSETMISC (overlay, ov);
5819 eassert (OVERLAYP (overlay));
5820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5823 if (start > charpos)
5824 break;
5826 /* Skip this overlay if it doesn't start or end at IT's current
5827 position. */
5828 if (end != charpos && start != charpos)
5829 continue;
5831 /* Skip this overlay if it doesn't apply to IT->w. */
5832 window = Foverlay_get (overlay, Qwindow);
5833 if (WINDOWP (window) && XWINDOW (window) != it->w)
5834 continue;
5836 /* If the text ``under'' the overlay is invisible, it has a zero
5837 dimension, and both before- and after-strings apply. */
5838 invisible = Foverlay_get (overlay, Qinvisible);
5839 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5841 /* If overlay has a non-empty before-string, record it. */
5842 if ((start == charpos || (end == charpos && invis != 0))
5843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5844 && SCHARS (str))
5845 RECORD_OVERLAY_STRING (overlay, str, false);
5847 /* If overlay has a non-empty after-string, record it. */
5848 if ((end == charpos || (start == charpos && invis != 0))
5849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5850 && SCHARS (str))
5851 RECORD_OVERLAY_STRING (overlay, str, true);
5854 #undef RECORD_OVERLAY_STRING
5856 /* Sort entries. */
5857 if (n > 1)
5858 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5860 /* Record number of overlay strings, and where we computed it. */
5861 it->n_overlay_strings = n;
5862 it->overlay_strings_charpos = charpos;
5864 /* IT->current.overlay_string_index is the number of overlay strings
5865 that have already been consumed by IT. Copy some of the
5866 remaining overlay strings to IT->overlay_strings. */
5867 i = 0;
5868 j = it->current.overlay_string_index;
5869 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5871 it->overlay_strings[i] = entries[j].string;
5872 it->string_overlays[i++] = entries[j++].overlay;
5875 CHECK_IT (it);
5876 SAFE_FREE ();
5880 /* Get the first chunk of overlay strings at IT's current buffer
5881 position, or at CHARPOS if that is > 0. Value is true if at
5882 least one overlay string was found. */
5884 static bool
5885 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5887 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5888 process. This fills IT->overlay_strings with strings, and sets
5889 IT->n_overlay_strings to the total number of strings to process.
5890 IT->pos.overlay_string_index has to be set temporarily to zero
5891 because load_overlay_strings needs this; it must be set to -1
5892 when no overlay strings are found because a zero value would
5893 indicate a position in the first overlay string. */
5894 it->current.overlay_string_index = 0;
5895 load_overlay_strings (it, charpos);
5897 /* If we found overlay strings, set up IT to deliver display
5898 elements from the first one. Otherwise set up IT to deliver
5899 from current_buffer. */
5900 if (it->n_overlay_strings)
5902 /* Make sure we know settings in current_buffer, so that we can
5903 restore meaningful values when we're done with the overlay
5904 strings. */
5905 if (compute_stop_p)
5906 compute_stop_pos (it);
5907 eassert (it->face_id >= 0);
5909 /* Save IT's settings. They are restored after all overlay
5910 strings have been processed. */
5911 eassert (!compute_stop_p || it->sp == 0);
5913 /* When called from handle_stop, there might be an empty display
5914 string loaded. In that case, don't bother saving it. But
5915 don't use this optimization with the bidi iterator, since we
5916 need the corresponding pop_it call to resync the bidi
5917 iterator's position with IT's position, after we are done
5918 with the overlay strings. (The corresponding call to pop_it
5919 in case of an empty display string is in
5920 next_overlay_string.) */
5921 if (!(!it->bidi_p
5922 && STRINGP (it->string) && !SCHARS (it->string)))
5923 push_it (it, NULL);
5925 /* Set up IT to deliver display elements from the first overlay
5926 string. */
5927 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5928 it->string = it->overlay_strings[0];
5929 it->from_overlay = Qnil;
5930 it->stop_charpos = 0;
5931 eassert (STRINGP (it->string));
5932 it->end_charpos = SCHARS (it->string);
5933 it->prev_stop = 0;
5934 it->base_level_stop = 0;
5935 it->multibyte_p = STRING_MULTIBYTE (it->string);
5936 it->method = GET_FROM_STRING;
5937 it->from_disp_prop_p = 0;
5939 /* Force paragraph direction to be that of the parent
5940 buffer. */
5941 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5942 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5943 else
5944 it->paragraph_embedding = L2R;
5946 /* Set up the bidi iterator for this overlay string. */
5947 if (it->bidi_p)
5949 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5951 it->bidi_it.string.lstring = it->string;
5952 it->bidi_it.string.s = NULL;
5953 it->bidi_it.string.schars = SCHARS (it->string);
5954 it->bidi_it.string.bufpos = pos;
5955 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5956 it->bidi_it.string.unibyte = !it->multibyte_p;
5957 it->bidi_it.w = it->w;
5958 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5960 return true;
5963 it->current.overlay_string_index = -1;
5964 return false;
5967 static bool
5968 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5970 it->string = Qnil;
5971 it->method = GET_FROM_BUFFER;
5973 get_overlay_strings_1 (it, charpos, true);
5975 CHECK_IT (it);
5977 /* Value is true if we found at least one overlay string. */
5978 return STRINGP (it->string);
5983 /***********************************************************************
5984 Saving and restoring state
5985 ***********************************************************************/
5987 /* Save current settings of IT on IT->stack. Called, for example,
5988 before setting up IT for an overlay string, to be able to restore
5989 IT's settings to what they were after the overlay string has been
5990 processed. If POSITION is non-NULL, it is the position to save on
5991 the stack instead of IT->position. */
5993 static void
5994 push_it (struct it *it, struct text_pos *position)
5996 struct iterator_stack_entry *p;
5998 eassert (it->sp < IT_STACK_SIZE);
5999 p = it->stack + it->sp;
6001 p->stop_charpos = it->stop_charpos;
6002 p->prev_stop = it->prev_stop;
6003 p->base_level_stop = it->base_level_stop;
6004 p->cmp_it = it->cmp_it;
6005 eassert (it->face_id >= 0);
6006 p->face_id = it->face_id;
6007 p->string = it->string;
6008 p->method = it->method;
6009 p->from_overlay = it->from_overlay;
6010 switch (p->method)
6012 case GET_FROM_IMAGE:
6013 p->u.image.object = it->object;
6014 p->u.image.image_id = it->image_id;
6015 p->u.image.slice = it->slice;
6016 break;
6017 case GET_FROM_STRETCH:
6018 p->u.stretch.object = it->object;
6019 break;
6020 case GET_FROM_XWIDGET:
6021 p->u.xwidget.object = it->object;
6022 break;
6023 case GET_FROM_BUFFER:
6024 case GET_FROM_DISPLAY_VECTOR:
6025 case GET_FROM_STRING:
6026 case GET_FROM_C_STRING:
6027 break;
6028 default:
6029 emacs_abort ();
6031 p->position = position ? *position : it->position;
6032 p->current = it->current;
6033 p->end_charpos = it->end_charpos;
6034 p->string_nchars = it->string_nchars;
6035 p->area = it->area;
6036 p->multibyte_p = it->multibyte_p;
6037 p->avoid_cursor_p = it->avoid_cursor_p;
6038 p->space_width = it->space_width;
6039 p->font_height = it->font_height;
6040 p->voffset = it->voffset;
6041 p->string_from_display_prop_p = it->string_from_display_prop_p;
6042 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6043 p->display_ellipsis_p = false;
6044 p->line_wrap = it->line_wrap;
6045 p->bidi_p = it->bidi_p;
6046 p->paragraph_embedding = it->paragraph_embedding;
6047 p->from_disp_prop_p = it->from_disp_prop_p;
6048 ++it->sp;
6050 /* Save the state of the bidi iterator as well. */
6051 if (it->bidi_p)
6052 bidi_push_it (&it->bidi_it);
6055 static void
6056 iterate_out_of_display_property (struct it *it)
6058 bool buffer_p = !STRINGP (it->string);
6059 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6060 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6062 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6064 /* Maybe initialize paragraph direction. If we are at the beginning
6065 of a new paragraph, next_element_from_buffer may not have a
6066 chance to do that. */
6067 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6068 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6069 /* prev_stop can be zero, so check against BEGV as well. */
6070 while (it->bidi_it.charpos >= bob
6071 && it->prev_stop <= it->bidi_it.charpos
6072 && it->bidi_it.charpos < CHARPOS (it->position)
6073 && it->bidi_it.charpos < eob)
6074 bidi_move_to_visually_next (&it->bidi_it);
6075 /* Record the stop_pos we just crossed, for when we cross it
6076 back, maybe. */
6077 if (it->bidi_it.charpos > CHARPOS (it->position))
6078 it->prev_stop = CHARPOS (it->position);
6079 /* If we ended up not where pop_it put us, resync IT's
6080 positional members with the bidi iterator. */
6081 if (it->bidi_it.charpos != CHARPOS (it->position))
6082 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6083 if (buffer_p)
6084 it->current.pos = it->position;
6085 else
6086 it->current.string_pos = it->position;
6089 /* Restore IT's settings from IT->stack. Called, for example, when no
6090 more overlay strings must be processed, and we return to delivering
6091 display elements from a buffer, or when the end of a string from a
6092 `display' property is reached and we return to delivering display
6093 elements from an overlay string, or from a buffer. */
6095 static void
6096 pop_it (struct it *it)
6098 struct iterator_stack_entry *p;
6099 bool from_display_prop = it->from_disp_prop_p;
6100 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6102 eassert (it->sp > 0);
6103 --it->sp;
6104 p = it->stack + it->sp;
6105 it->stop_charpos = p->stop_charpos;
6106 it->prev_stop = p->prev_stop;
6107 it->base_level_stop = p->base_level_stop;
6108 it->cmp_it = p->cmp_it;
6109 it->face_id = p->face_id;
6110 it->current = p->current;
6111 it->position = p->position;
6112 it->string = p->string;
6113 it->from_overlay = p->from_overlay;
6114 if (NILP (it->string))
6115 SET_TEXT_POS (it->current.string_pos, -1, -1);
6116 it->method = p->method;
6117 switch (it->method)
6119 case GET_FROM_IMAGE:
6120 it->image_id = p->u.image.image_id;
6121 it->object = p->u.image.object;
6122 it->slice = p->u.image.slice;
6123 break;
6124 case GET_FROM_XWIDGET:
6125 it->object = p->u.xwidget.object;
6126 break;
6127 case GET_FROM_STRETCH:
6128 it->object = p->u.stretch.object;
6129 break;
6130 case GET_FROM_BUFFER:
6131 it->object = it->w->contents;
6132 break;
6133 case GET_FROM_STRING:
6135 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6137 /* Restore the face_box_p flag, since it could have been
6138 overwritten by the face of the object that we just finished
6139 displaying. */
6140 if (face)
6141 it->face_box_p = face->box != FACE_NO_BOX;
6142 it->object = it->string;
6144 break;
6145 case GET_FROM_DISPLAY_VECTOR:
6146 if (it->s)
6147 it->method = GET_FROM_C_STRING;
6148 else if (STRINGP (it->string))
6149 it->method = GET_FROM_STRING;
6150 else
6152 it->method = GET_FROM_BUFFER;
6153 it->object = it->w->contents;
6155 break;
6156 case GET_FROM_C_STRING:
6157 break;
6158 default:
6159 emacs_abort ();
6161 it->end_charpos = p->end_charpos;
6162 it->string_nchars = p->string_nchars;
6163 it->area = p->area;
6164 it->multibyte_p = p->multibyte_p;
6165 it->avoid_cursor_p = p->avoid_cursor_p;
6166 it->space_width = p->space_width;
6167 it->font_height = p->font_height;
6168 it->voffset = p->voffset;
6169 it->string_from_display_prop_p = p->string_from_display_prop_p;
6170 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6171 it->line_wrap = p->line_wrap;
6172 it->bidi_p = p->bidi_p;
6173 it->paragraph_embedding = p->paragraph_embedding;
6174 it->from_disp_prop_p = p->from_disp_prop_p;
6175 if (it->bidi_p)
6177 bidi_pop_it (&it->bidi_it);
6178 /* Bidi-iterate until we get out of the portion of text, if any,
6179 covered by a `display' text property or by an overlay with
6180 `display' property. (We cannot just jump there, because the
6181 internal coherency of the bidi iterator state can not be
6182 preserved across such jumps.) We also must determine the
6183 paragraph base direction if the overlay we just processed is
6184 at the beginning of a new paragraph. */
6185 if (from_display_prop
6186 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6187 iterate_out_of_display_property (it);
6189 eassert ((BUFFERP (it->object)
6190 && IT_CHARPOS (*it) == it->bidi_it.charpos
6191 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6192 || (STRINGP (it->object)
6193 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6194 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6195 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6197 /* If we move the iterator over text covered by a display property
6198 to a new buffer position, any info about previously seen overlays
6199 is no longer valid. */
6200 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6201 it->ignore_overlay_strings_at_pos_p = false;
6206 /***********************************************************************
6207 Moving over lines
6208 ***********************************************************************/
6210 /* Set IT's current position to the previous line start. */
6212 static void
6213 back_to_previous_line_start (struct it *it)
6215 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6217 DEC_BOTH (cp, bp);
6218 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6222 /* Move IT to the next line start.
6224 Value is true if a newline was found. Set *SKIPPED_P to true if
6225 we skipped over part of the text (as opposed to moving the iterator
6226 continuously over the text). Otherwise, don't change the value
6227 of *SKIPPED_P.
6229 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6230 iterator on the newline, if it was found.
6232 Newlines may come from buffer text, overlay strings, or strings
6233 displayed via the `display' property. That's the reason we can't
6234 simply use find_newline_no_quit.
6236 Note that this function may not skip over invisible text that is so
6237 because of text properties and immediately follows a newline. If
6238 it would, function reseat_at_next_visible_line_start, when called
6239 from set_iterator_to_next, would effectively make invisible
6240 characters following a newline part of the wrong glyph row, which
6241 leads to wrong cursor motion. */
6243 static bool
6244 forward_to_next_line_start (struct it *it, bool *skipped_p,
6245 struct bidi_it *bidi_it_prev)
6247 ptrdiff_t old_selective;
6248 bool newline_found_p = false;
6249 int n;
6250 const int MAX_NEWLINE_DISTANCE = 500;
6252 /* If already on a newline, just consume it to avoid unintended
6253 skipping over invisible text below. */
6254 if (it->what == IT_CHARACTER
6255 && it->c == '\n'
6256 && CHARPOS (it->position) == IT_CHARPOS (*it))
6258 if (it->bidi_p && bidi_it_prev)
6259 *bidi_it_prev = it->bidi_it;
6260 set_iterator_to_next (it, false);
6261 it->c = 0;
6262 return true;
6265 /* Don't handle selective display in the following. It's (a)
6266 unnecessary because it's done by the caller, and (b) leads to an
6267 infinite recursion because next_element_from_ellipsis indirectly
6268 calls this function. */
6269 old_selective = it->selective;
6270 it->selective = 0;
6272 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6273 from buffer text. */
6274 for (n = 0;
6275 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6276 n += !STRINGP (it->string))
6278 if (!get_next_display_element (it))
6279 return false;
6280 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6281 if (newline_found_p && it->bidi_p && bidi_it_prev)
6282 *bidi_it_prev = it->bidi_it;
6283 set_iterator_to_next (it, false);
6286 /* If we didn't find a newline near enough, see if we can use a
6287 short-cut. */
6288 if (!newline_found_p)
6290 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6291 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6292 1, &bytepos);
6293 Lisp_Object pos;
6295 eassert (!STRINGP (it->string));
6297 /* If there isn't any `display' property in sight, and no
6298 overlays, we can just use the position of the newline in
6299 buffer text. */
6300 if (it->stop_charpos >= limit
6301 || ((pos = Fnext_single_property_change (make_number (start),
6302 Qdisplay, Qnil,
6303 make_number (limit)),
6304 NILP (pos))
6305 && next_overlay_change (start) == ZV))
6307 if (!it->bidi_p)
6309 IT_CHARPOS (*it) = limit;
6310 IT_BYTEPOS (*it) = bytepos;
6312 else
6314 struct bidi_it bprev;
6316 /* Help bidi.c avoid expensive searches for display
6317 properties and overlays, by telling it that there are
6318 none up to `limit'. */
6319 if (it->bidi_it.disp_pos < limit)
6321 it->bidi_it.disp_pos = limit;
6322 it->bidi_it.disp_prop = 0;
6324 do {
6325 bprev = it->bidi_it;
6326 bidi_move_to_visually_next (&it->bidi_it);
6327 } while (it->bidi_it.charpos != limit);
6328 IT_CHARPOS (*it) = limit;
6329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6330 if (bidi_it_prev)
6331 *bidi_it_prev = bprev;
6333 *skipped_p = newline_found_p = true;
6335 else
6337 while (!newline_found_p)
6339 if (!get_next_display_element (it))
6340 break;
6341 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6342 if (newline_found_p && it->bidi_p && bidi_it_prev)
6343 *bidi_it_prev = it->bidi_it;
6344 set_iterator_to_next (it, false);
6349 it->selective = old_selective;
6350 return newline_found_p;
6354 /* Set IT's current position to the previous visible line start. Skip
6355 invisible text that is so either due to text properties or due to
6356 selective display. Caution: this does not change IT->current_x and
6357 IT->hpos. */
6359 static void
6360 back_to_previous_visible_line_start (struct it *it)
6362 while (IT_CHARPOS (*it) > BEGV)
6364 back_to_previous_line_start (it);
6366 if (IT_CHARPOS (*it) <= BEGV)
6367 break;
6369 /* If selective > 0, then lines indented more than its value are
6370 invisible. */
6371 if (it->selective > 0
6372 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6373 it->selective))
6374 continue;
6376 /* Check the newline before point for invisibility. */
6378 Lisp_Object prop;
6379 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6380 Qinvisible, it->window);
6381 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6382 continue;
6385 if (IT_CHARPOS (*it) <= BEGV)
6386 break;
6389 struct it it2;
6390 void *it2data = NULL;
6391 ptrdiff_t pos;
6392 ptrdiff_t beg, end;
6393 Lisp_Object val, overlay;
6395 SAVE_IT (it2, *it, it2data);
6397 /* If newline is part of a composition, continue from start of composition */
6398 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6399 && beg < IT_CHARPOS (*it))
6400 goto replaced;
6402 /* If newline is replaced by a display property, find start of overlay
6403 or interval and continue search from that point. */
6404 pos = --IT_CHARPOS (it2);
6405 --IT_BYTEPOS (it2);
6406 it2.sp = 0;
6407 bidi_unshelve_cache (NULL, false);
6408 it2.string_from_display_prop_p = false;
6409 it2.from_disp_prop_p = false;
6410 if (handle_display_prop (&it2) == HANDLED_RETURN
6411 && !NILP (val = get_char_property_and_overlay
6412 (make_number (pos), Qdisplay, Qnil, &overlay))
6413 && (OVERLAYP (overlay)
6414 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6415 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6417 RESTORE_IT (it, it, it2data);
6418 goto replaced;
6421 /* Newline is not replaced by anything -- so we are done. */
6422 RESTORE_IT (it, it, it2data);
6423 break;
6425 replaced:
6426 if (beg < BEGV)
6427 beg = BEGV;
6428 IT_CHARPOS (*it) = beg;
6429 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6433 it->continuation_lines_width = 0;
6435 eassert (IT_CHARPOS (*it) >= BEGV);
6436 eassert (IT_CHARPOS (*it) == BEGV
6437 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6438 CHECK_IT (it);
6442 /* Reseat iterator IT at the previous visible line start. Skip
6443 invisible text that is so either due to text properties or due to
6444 selective display. At the end, update IT's overlay information,
6445 face information etc. */
6447 void
6448 reseat_at_previous_visible_line_start (struct it *it)
6450 back_to_previous_visible_line_start (it);
6451 reseat (it, it->current.pos, true);
6452 CHECK_IT (it);
6456 /* Reseat iterator IT on the next visible line start in the current
6457 buffer. ON_NEWLINE_P means position IT on the newline
6458 preceding the line start. Skip over invisible text that is so
6459 because of selective display. Compute faces, overlays etc at the
6460 new position. Note that this function does not skip over text that
6461 is invisible because of text properties. */
6463 static void
6464 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6466 bool skipped_p = false;
6467 struct bidi_it bidi_it_prev;
6468 bool newline_found_p
6469 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6471 /* Skip over lines that are invisible because they are indented
6472 more than the value of IT->selective. */
6473 if (it->selective > 0)
6474 while (IT_CHARPOS (*it) < ZV
6475 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6476 it->selective))
6478 eassert (IT_BYTEPOS (*it) == BEGV
6479 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6480 newline_found_p =
6481 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6484 /* Position on the newline if that's what's requested. */
6485 if (on_newline_p && newline_found_p)
6487 if (STRINGP (it->string))
6489 if (IT_STRING_CHARPOS (*it) > 0)
6491 if (!it->bidi_p)
6493 --IT_STRING_CHARPOS (*it);
6494 --IT_STRING_BYTEPOS (*it);
6496 else
6498 /* We need to restore the bidi iterator to the state
6499 it had on the newline, and resync the IT's
6500 position with that. */
6501 it->bidi_it = bidi_it_prev;
6502 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6503 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6507 else if (IT_CHARPOS (*it) > BEGV)
6509 if (!it->bidi_p)
6511 --IT_CHARPOS (*it);
6512 --IT_BYTEPOS (*it);
6514 else
6516 /* We need to restore the bidi iterator to the state it
6517 had on the newline and resync IT with that. */
6518 it->bidi_it = bidi_it_prev;
6519 IT_CHARPOS (*it) = it->bidi_it.charpos;
6520 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6522 reseat (it, it->current.pos, false);
6525 else if (skipped_p)
6526 reseat (it, it->current.pos, false);
6528 CHECK_IT (it);
6533 /***********************************************************************
6534 Changing an iterator's position
6535 ***********************************************************************/
6537 /* Change IT's current position to POS in current_buffer.
6538 If FORCE_P, always check for text properties at the new position.
6539 Otherwise, text properties are only looked up if POS >=
6540 IT->check_charpos of a property. */
6542 static void
6543 reseat (struct it *it, struct text_pos pos, bool force_p)
6545 ptrdiff_t original_pos = IT_CHARPOS (*it);
6547 reseat_1 (it, pos, false);
6549 /* Determine where to check text properties. Avoid doing it
6550 where possible because text property lookup is very expensive. */
6551 if (force_p
6552 || CHARPOS (pos) > it->stop_charpos
6553 || CHARPOS (pos) < original_pos)
6555 if (it->bidi_p)
6557 /* For bidi iteration, we need to prime prev_stop and
6558 base_level_stop with our best estimations. */
6559 /* Implementation note: Of course, POS is not necessarily a
6560 stop position, so assigning prev_pos to it is a lie; we
6561 should have called compute_stop_backwards. However, if
6562 the current buffer does not include any R2L characters,
6563 that call would be a waste of cycles, because the
6564 iterator will never move back, and thus never cross this
6565 "fake" stop position. So we delay that backward search
6566 until the time we really need it, in next_element_from_buffer. */
6567 if (CHARPOS (pos) != it->prev_stop)
6568 it->prev_stop = CHARPOS (pos);
6569 if (CHARPOS (pos) < it->base_level_stop)
6570 it->base_level_stop = 0; /* meaning it's unknown */
6571 handle_stop (it);
6573 else
6575 handle_stop (it);
6576 it->prev_stop = it->base_level_stop = 0;
6581 CHECK_IT (it);
6585 /* Change IT's buffer position to POS. SET_STOP_P means set
6586 IT->stop_pos to POS, also. */
6588 static void
6589 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6591 /* Don't call this function when scanning a C string. */
6592 eassert (it->s == NULL);
6594 /* POS must be a reasonable value. */
6595 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6597 it->current.pos = it->position = pos;
6598 it->end_charpos = ZV;
6599 it->dpvec = NULL;
6600 it->current.dpvec_index = -1;
6601 it->current.overlay_string_index = -1;
6602 IT_STRING_CHARPOS (*it) = -1;
6603 IT_STRING_BYTEPOS (*it) = -1;
6604 it->string = Qnil;
6605 it->method = GET_FROM_BUFFER;
6606 it->object = it->w->contents;
6607 it->area = TEXT_AREA;
6608 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6609 it->sp = 0;
6610 it->string_from_display_prop_p = false;
6611 it->string_from_prefix_prop_p = false;
6613 it->from_disp_prop_p = false;
6614 it->face_before_selective_p = false;
6615 if (it->bidi_p)
6617 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6618 &it->bidi_it);
6619 bidi_unshelve_cache (NULL, false);
6620 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6621 it->bidi_it.string.s = NULL;
6622 it->bidi_it.string.lstring = Qnil;
6623 it->bidi_it.string.bufpos = 0;
6624 it->bidi_it.string.from_disp_str = false;
6625 it->bidi_it.string.unibyte = false;
6626 it->bidi_it.w = it->w;
6629 if (set_stop_p)
6631 it->stop_charpos = CHARPOS (pos);
6632 it->base_level_stop = CHARPOS (pos);
6634 /* This make the information stored in it->cmp_it invalidate. */
6635 it->cmp_it.id = -1;
6639 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6640 If S is non-null, it is a C string to iterate over. Otherwise,
6641 STRING gives a Lisp string to iterate over.
6643 If PRECISION > 0, don't return more then PRECISION number of
6644 characters from the string.
6646 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6647 characters have been returned. FIELD_WIDTH < 0 means an infinite
6648 field width.
6650 MULTIBYTE = 0 means disable processing of multibyte characters,
6651 MULTIBYTE > 0 means enable it,
6652 MULTIBYTE < 0 means use IT->multibyte_p.
6654 IT must be initialized via a prior call to init_iterator before
6655 calling this function. */
6657 static void
6658 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6659 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6660 int multibyte)
6662 /* No text property checks performed by default, but see below. */
6663 it->stop_charpos = -1;
6665 /* Set iterator position and end position. */
6666 memset (&it->current, 0, sizeof it->current);
6667 it->current.overlay_string_index = -1;
6668 it->current.dpvec_index = -1;
6669 eassert (charpos >= 0);
6671 /* If STRING is specified, use its multibyteness, otherwise use the
6672 setting of MULTIBYTE, if specified. */
6673 if (multibyte >= 0)
6674 it->multibyte_p = multibyte > 0;
6676 /* Bidirectional reordering of strings is controlled by the default
6677 value of bidi-display-reordering. Don't try to reorder while
6678 loading loadup.el, as the necessary character property tables are
6679 not yet available. */
6680 it->bidi_p =
6681 !redisplay__inhibit_bidi
6682 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6684 if (s == NULL)
6686 eassert (STRINGP (string));
6687 it->string = string;
6688 it->s = NULL;
6689 it->end_charpos = it->string_nchars = SCHARS (string);
6690 it->method = GET_FROM_STRING;
6691 it->current.string_pos = string_pos (charpos, string);
6693 if (it->bidi_p)
6695 it->bidi_it.string.lstring = string;
6696 it->bidi_it.string.s = NULL;
6697 it->bidi_it.string.schars = it->end_charpos;
6698 it->bidi_it.string.bufpos = 0;
6699 it->bidi_it.string.from_disp_str = false;
6700 it->bidi_it.string.unibyte = !it->multibyte_p;
6701 it->bidi_it.w = it->w;
6702 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6703 FRAME_WINDOW_P (it->f), &it->bidi_it);
6706 else
6708 it->s = (const unsigned char *) s;
6709 it->string = Qnil;
6711 /* Note that we use IT->current.pos, not it->current.string_pos,
6712 for displaying C strings. */
6713 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6714 if (it->multibyte_p)
6716 it->current.pos = c_string_pos (charpos, s, true);
6717 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6719 else
6721 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6722 it->end_charpos = it->string_nchars = strlen (s);
6725 if (it->bidi_p)
6727 it->bidi_it.string.lstring = Qnil;
6728 it->bidi_it.string.s = (const unsigned char *) s;
6729 it->bidi_it.string.schars = it->end_charpos;
6730 it->bidi_it.string.bufpos = 0;
6731 it->bidi_it.string.from_disp_str = false;
6732 it->bidi_it.string.unibyte = !it->multibyte_p;
6733 it->bidi_it.w = it->w;
6734 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6735 &it->bidi_it);
6737 it->method = GET_FROM_C_STRING;
6740 /* PRECISION > 0 means don't return more than PRECISION characters
6741 from the string. */
6742 if (precision > 0 && it->end_charpos - charpos > precision)
6744 it->end_charpos = it->string_nchars = charpos + precision;
6745 if (it->bidi_p)
6746 it->bidi_it.string.schars = it->end_charpos;
6749 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6750 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6751 FIELD_WIDTH < 0 means infinite field width. This is useful for
6752 padding with `-' at the end of a mode line. */
6753 if (field_width < 0)
6754 field_width = INFINITY;
6755 /* Implementation note: We deliberately don't enlarge
6756 it->bidi_it.string.schars here to fit it->end_charpos, because
6757 the bidi iterator cannot produce characters out of thin air. */
6758 if (field_width > it->end_charpos - charpos)
6759 it->end_charpos = charpos + field_width;
6761 /* Use the standard display table for displaying strings. */
6762 if (DISP_TABLE_P (Vstandard_display_table))
6763 it->dp = XCHAR_TABLE (Vstandard_display_table);
6765 it->stop_charpos = charpos;
6766 it->prev_stop = charpos;
6767 it->base_level_stop = 0;
6768 if (it->bidi_p)
6770 it->bidi_it.first_elt = true;
6771 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6772 it->bidi_it.disp_pos = -1;
6774 if (s == NULL && it->multibyte_p)
6776 ptrdiff_t endpos = SCHARS (it->string);
6777 if (endpos > it->end_charpos)
6778 endpos = it->end_charpos;
6779 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6780 it->string);
6782 CHECK_IT (it);
6787 /***********************************************************************
6788 Iteration
6789 ***********************************************************************/
6791 /* Map enum it_method value to corresponding next_element_from_* function. */
6793 typedef bool (*next_element_function) (struct it *);
6795 static next_element_function const get_next_element[NUM_IT_METHODS] =
6797 next_element_from_buffer,
6798 next_element_from_display_vector,
6799 next_element_from_string,
6800 next_element_from_c_string,
6801 next_element_from_image,
6802 next_element_from_stretch,
6803 next_element_from_xwidget,
6806 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6809 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6810 (possibly with the following characters). */
6812 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6813 ((IT)->cmp_it.id >= 0 \
6814 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6815 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6816 END_CHARPOS, (IT)->w, \
6817 FACE_FROM_ID_OR_NULL ((IT)->f, \
6818 (IT)->face_id), \
6819 (IT)->string)))
6822 /* Lookup the char-table Vglyphless_char_display for character C (-1
6823 if we want information for no-font case), and return the display
6824 method symbol. By side-effect, update it->what and
6825 it->glyphless_method. This function is called from
6826 get_next_display_element for each character element, and from
6827 x_produce_glyphs when no suitable font was found. */
6829 Lisp_Object
6830 lookup_glyphless_char_display (int c, struct it *it)
6832 Lisp_Object glyphless_method = Qnil;
6834 if (CHAR_TABLE_P (Vglyphless_char_display)
6835 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6837 if (c >= 0)
6839 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6840 if (CONSP (glyphless_method))
6841 glyphless_method = FRAME_WINDOW_P (it->f)
6842 ? XCAR (glyphless_method)
6843 : XCDR (glyphless_method);
6845 else
6846 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6849 retry:
6850 if (NILP (glyphless_method))
6852 if (c >= 0)
6853 /* The default is to display the character by a proper font. */
6854 return Qnil;
6855 /* The default for the no-font case is to display an empty box. */
6856 glyphless_method = Qempty_box;
6858 if (EQ (glyphless_method, Qzero_width))
6860 if (c >= 0)
6861 return glyphless_method;
6862 /* This method can't be used for the no-font case. */
6863 glyphless_method = Qempty_box;
6865 if (EQ (glyphless_method, Qthin_space))
6866 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6867 else if (EQ (glyphless_method, Qempty_box))
6868 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6869 else if (EQ (glyphless_method, Qhex_code))
6870 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6871 else if (STRINGP (glyphless_method))
6872 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6873 else
6875 /* Invalid value. We use the default method. */
6876 glyphless_method = Qnil;
6877 goto retry;
6879 it->what = IT_GLYPHLESS;
6880 return glyphless_method;
6883 /* Merge escape glyph face and cache the result. */
6885 static struct frame *last_escape_glyph_frame = NULL;
6886 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6887 static int last_escape_glyph_merged_face_id = 0;
6889 static int
6890 merge_escape_glyph_face (struct it *it)
6892 int face_id;
6894 if (it->f == last_escape_glyph_frame
6895 && it->face_id == last_escape_glyph_face_id)
6896 face_id = last_escape_glyph_merged_face_id;
6897 else
6899 /* Merge the `escape-glyph' face into the current face. */
6900 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6901 last_escape_glyph_frame = it->f;
6902 last_escape_glyph_face_id = it->face_id;
6903 last_escape_glyph_merged_face_id = face_id;
6905 return face_id;
6908 /* Likewise for glyphless glyph face. */
6910 static struct frame *last_glyphless_glyph_frame = NULL;
6911 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6912 static int last_glyphless_glyph_merged_face_id = 0;
6915 merge_glyphless_glyph_face (struct it *it)
6917 int face_id;
6919 if (it->f == last_glyphless_glyph_frame
6920 && it->face_id == last_glyphless_glyph_face_id)
6921 face_id = last_glyphless_glyph_merged_face_id;
6922 else
6924 /* Merge the `glyphless-char' face into the current face. */
6925 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6926 last_glyphless_glyph_frame = it->f;
6927 last_glyphless_glyph_face_id = it->face_id;
6928 last_glyphless_glyph_merged_face_id = face_id;
6930 return face_id;
6933 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6934 be called before redisplaying windows, and when the frame's face
6935 cache is freed. */
6936 void
6937 forget_escape_and_glyphless_faces (void)
6939 last_escape_glyph_frame = NULL;
6940 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6941 last_glyphless_glyph_frame = NULL;
6942 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6945 /* Load IT's display element fields with information about the next
6946 display element from the current position of IT. Value is false if
6947 end of buffer (or C string) is reached. */
6949 static bool
6950 get_next_display_element (struct it *it)
6952 /* True means that we found a display element. False means that
6953 we hit the end of what we iterate over. Performance note: the
6954 function pointer `method' used here turns out to be faster than
6955 using a sequence of if-statements. */
6956 bool success_p;
6958 get_next:
6959 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6961 if (it->what == IT_CHARACTER)
6963 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6964 and only if (a) the resolved directionality of that character
6965 is R..." */
6966 /* FIXME: Do we need an exception for characters from display
6967 tables? */
6968 if (it->bidi_p && it->bidi_it.type == STRONG_R
6969 && !inhibit_bidi_mirroring)
6970 it->c = bidi_mirror_char (it->c);
6971 /* Map via display table or translate control characters.
6972 IT->c, IT->len etc. have been set to the next character by
6973 the function call above. If we have a display table, and it
6974 contains an entry for IT->c, translate it. Don't do this if
6975 IT->c itself comes from a display table, otherwise we could
6976 end up in an infinite recursion. (An alternative could be to
6977 count the recursion depth of this function and signal an
6978 error when a certain maximum depth is reached.) Is it worth
6979 it? */
6980 if (success_p && it->dpvec == NULL)
6982 Lisp_Object dv;
6983 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6984 bool nonascii_space_p = false;
6985 bool nonascii_hyphen_p = false;
6986 int c = it->c; /* This is the character to display. */
6988 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6990 eassert (SINGLE_BYTE_CHAR_P (c));
6991 if (unibyte_display_via_language_environment)
6993 c = DECODE_CHAR (unibyte, c);
6994 if (c < 0)
6995 c = BYTE8_TO_CHAR (it->c);
6997 else
6998 c = BYTE8_TO_CHAR (it->c);
7001 if (it->dp
7002 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7003 VECTORP (dv)))
7005 struct Lisp_Vector *v = XVECTOR (dv);
7007 /* Return the first character from the display table
7008 entry, if not empty. If empty, don't display the
7009 current character. */
7010 if (v->header.size)
7012 it->dpvec_char_len = it->len;
7013 it->dpvec = v->contents;
7014 it->dpend = v->contents + v->header.size;
7015 it->current.dpvec_index = 0;
7016 it->dpvec_face_id = -1;
7017 it->saved_face_id = it->face_id;
7018 it->method = GET_FROM_DISPLAY_VECTOR;
7019 it->ellipsis_p = false;
7021 else
7023 set_iterator_to_next (it, false);
7025 goto get_next;
7028 if (! NILP (lookup_glyphless_char_display (c, it)))
7030 if (it->what == IT_GLYPHLESS)
7031 goto done;
7032 /* Don't display this character. */
7033 set_iterator_to_next (it, false);
7034 goto get_next;
7037 /* If `nobreak-char-display' is non-nil, we display
7038 non-ASCII spaces and hyphens specially. */
7039 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7041 if (c == NO_BREAK_SPACE)
7042 nonascii_space_p = true;
7043 else if (c == SOFT_HYPHEN || c == HYPHEN
7044 || c == NON_BREAKING_HYPHEN)
7045 nonascii_hyphen_p = true;
7048 /* Translate control characters into `\003' or `^C' form.
7049 Control characters coming from a display table entry are
7050 currently not translated because we use IT->dpvec to hold
7051 the translation. This could easily be changed but I
7052 don't believe that it is worth doing.
7054 The characters handled by `nobreak-char-display' must be
7055 translated too.
7057 Non-printable characters and raw-byte characters are also
7058 translated to octal or hexadecimal form. */
7059 if (((c < ' ' || c == 127) /* ASCII control chars. */
7060 ? (it->area != TEXT_AREA
7061 /* In mode line, treat \n, \t like other crl chars. */
7062 || (c != '\t'
7063 && it->glyph_row
7064 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7065 || (c != '\n' && c != '\t'))
7066 : (nonascii_space_p
7067 || nonascii_hyphen_p
7068 || CHAR_BYTE8_P (c)
7069 || ! CHAR_PRINTABLE_P (c))))
7071 /* C is a control character, non-ASCII space/hyphen,
7072 raw-byte, or a non-printable character which must be
7073 displayed either as '\003' or as `^C' where the '\\'
7074 and '^' can be defined in the display table. Fill
7075 IT->ctl_chars with glyphs for what we have to
7076 display. Then, set IT->dpvec to these glyphs. */
7077 Lisp_Object gc;
7078 int ctl_len;
7079 int face_id;
7080 int lface_id = 0;
7081 int escape_glyph;
7083 /* Handle control characters with ^. */
7085 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7087 int g;
7089 g = '^'; /* default glyph for Control */
7090 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7091 if (it->dp
7092 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7094 g = GLYPH_CODE_CHAR (gc);
7095 lface_id = GLYPH_CODE_FACE (gc);
7098 face_id = (lface_id
7099 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7100 : merge_escape_glyph_face (it));
7102 XSETINT (it->ctl_chars[0], g);
7103 XSETINT (it->ctl_chars[1], c ^ 0100);
7104 ctl_len = 2;
7105 goto display_control;
7108 /* Handle non-ascii space in the mode where it only gets
7109 highlighting. */
7111 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7113 /* Merge `nobreak-space' into the current face. */
7114 face_id = merge_faces (it->f, Qnobreak_space, 0,
7115 it->face_id);
7116 XSETINT (it->ctl_chars[0], ' ');
7117 ctl_len = 1;
7118 goto display_control;
7121 /* Handle non-ascii hyphens in the mode where it only
7122 gets highlighting. */
7124 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7126 /* Merge `nobreak-space' into the current face. */
7127 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7128 it->face_id);
7129 XSETINT (it->ctl_chars[0], '-');
7130 ctl_len = 1;
7131 goto display_control;
7134 /* Handle sequences that start with the "escape glyph". */
7136 /* the default escape glyph is \. */
7137 escape_glyph = '\\';
7139 if (it->dp
7140 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7142 escape_glyph = GLYPH_CODE_CHAR (gc);
7143 lface_id = GLYPH_CODE_FACE (gc);
7146 face_id = (lface_id
7147 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7148 : merge_escape_glyph_face (it));
7150 /* Draw non-ASCII space/hyphen with escape glyph: */
7152 if (nonascii_space_p || nonascii_hyphen_p)
7154 XSETINT (it->ctl_chars[0], escape_glyph);
7155 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7156 ctl_len = 2;
7157 goto display_control;
7161 char str[10];
7162 int len, i;
7164 if (CHAR_BYTE8_P (c))
7165 /* Display \200 or \x80 instead of \17777600. */
7166 c = CHAR_TO_BYTE8 (c);
7167 const char *format_string = display_raw_bytes_as_hex
7168 ? "x%02x"
7169 : "%03o";
7170 len = sprintf (str, format_string, c + 0u);
7172 XSETINT (it->ctl_chars[0], escape_glyph);
7173 for (i = 0; i < len; i++)
7174 XSETINT (it->ctl_chars[i + 1], str[i]);
7175 ctl_len = len + 1;
7178 display_control:
7179 /* Set up IT->dpvec and return first character from it. */
7180 it->dpvec_char_len = it->len;
7181 it->dpvec = it->ctl_chars;
7182 it->dpend = it->dpvec + ctl_len;
7183 it->current.dpvec_index = 0;
7184 it->dpvec_face_id = face_id;
7185 it->saved_face_id = it->face_id;
7186 it->method = GET_FROM_DISPLAY_VECTOR;
7187 it->ellipsis_p = false;
7188 goto get_next;
7190 it->char_to_display = c;
7192 else if (success_p)
7194 it->char_to_display = it->c;
7198 #ifdef HAVE_WINDOW_SYSTEM
7199 /* Adjust face id for a multibyte character. There are no multibyte
7200 character in unibyte text. */
7201 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7202 && it->multibyte_p
7203 && success_p
7204 && FRAME_WINDOW_P (it->f))
7206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7208 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7210 /* Automatic composition with glyph-string. */
7211 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7213 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7215 else
7217 ptrdiff_t pos = (it->s ? -1
7218 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7219 : IT_CHARPOS (*it));
7220 int c;
7222 if (it->what == IT_CHARACTER)
7223 c = it->char_to_display;
7224 else
7226 struct composition *cmp = composition_table[it->cmp_it.id];
7227 int i;
7229 c = ' ';
7230 for (i = 0; i < cmp->glyph_len; i++)
7231 /* TAB in a composition means display glyphs with
7232 padding space on the left or right. */
7233 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7234 break;
7236 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7239 #endif /* HAVE_WINDOW_SYSTEM */
7241 done:
7242 /* Is this character the last one of a run of characters with
7243 box? If yes, set IT->end_of_box_run_p to true. */
7244 if (it->face_box_p
7245 && it->s == NULL)
7247 if (it->method == GET_FROM_STRING && it->sp)
7249 int face_id = underlying_face_id (it);
7250 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7252 if (face)
7254 if (face->box == FACE_NO_BOX)
7256 /* If the box comes from face properties in a
7257 display string, check faces in that string. */
7258 int string_face_id = face_after_it_pos (it);
7259 it->end_of_box_run_p
7260 = (FACE_FROM_ID (it->f, string_face_id)->box
7261 == FACE_NO_BOX);
7263 /* Otherwise, the box comes from the underlying face.
7264 If this is the last string character displayed, check
7265 the next buffer location. */
7266 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7267 /* n_overlay_strings is unreliable unless
7268 overlay_string_index is non-negative. */
7269 && ((it->current.overlay_string_index >= 0
7270 && (it->current.overlay_string_index
7271 == it->n_overlay_strings - 1))
7272 /* A string from display property. */
7273 || it->from_disp_prop_p))
7275 ptrdiff_t ignore;
7276 int next_face_id;
7277 bool text_from_string = false;
7278 /* Normally, the next buffer location is stored in
7279 IT->current.pos... */
7280 struct text_pos pos = it->current.pos;
7282 /* ...but for a string from a display property, the
7283 next buffer position is stored in the 'position'
7284 member of the iteration stack slot below the
7285 current one, see handle_single_display_spec. By
7286 contrast, it->current.pos was not yet updated to
7287 point to that buffer position; that will happen
7288 in pop_it, after we finish displaying the current
7289 string. Note that we already checked above that
7290 it->sp is positive, so subtracting one from it is
7291 safe. */
7292 if (it->from_disp_prop_p)
7294 int stackp = it->sp - 1;
7296 /* Find the stack level with data from buffer. */
7297 while (stackp >= 0
7298 && STRINGP ((it->stack + stackp)->string))
7299 stackp--;
7300 if (stackp < 0)
7302 /* If no stack slot was found for iterating
7303 a buffer, we are displaying text from a
7304 string, most probably the mode line or
7305 the header line, and that string has a
7306 display string on some of its
7307 characters. */
7308 text_from_string = true;
7309 pos = it->stack[it->sp - 1].position;
7311 else
7312 pos = (it->stack + stackp)->position;
7314 else
7315 INC_TEXT_POS (pos, it->multibyte_p);
7317 if (text_from_string)
7319 Lisp_Object base_string = it->stack[it->sp - 1].string;
7321 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7322 it->end_of_box_run_p = true;
7323 else
7325 next_face_id
7326 = face_at_string_position (it->w, base_string,
7327 CHARPOS (pos), 0,
7328 &ignore, face_id, false);
7329 it->end_of_box_run_p
7330 = (FACE_FROM_ID (it->f, next_face_id)->box
7331 == FACE_NO_BOX);
7334 else if (CHARPOS (pos) >= ZV)
7335 it->end_of_box_run_p = true;
7336 else
7338 next_face_id =
7339 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7340 CHARPOS (pos)
7341 + TEXT_PROP_DISTANCE_LIMIT,
7342 false, -1);
7343 it->end_of_box_run_p
7344 = (FACE_FROM_ID (it->f, next_face_id)->box
7345 == FACE_NO_BOX);
7350 /* next_element_from_display_vector sets this flag according to
7351 faces of the display vector glyphs, see there. */
7352 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7354 int face_id = face_after_it_pos (it);
7355 it->end_of_box_run_p
7356 = (face_id != it->face_id
7357 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7360 /* If we reached the end of the object we've been iterating (e.g., a
7361 display string or an overlay string), and there's something on
7362 IT->stack, proceed with what's on the stack. It doesn't make
7363 sense to return false if there's unprocessed stuff on the stack,
7364 because otherwise that stuff will never be displayed. */
7365 if (!success_p && it->sp > 0)
7367 set_iterator_to_next (it, false);
7368 success_p = get_next_display_element (it);
7371 /* Value is false if end of buffer or string reached. */
7372 return success_p;
7376 /* Move IT to the next display element.
7378 RESEAT_P means if called on a newline in buffer text,
7379 skip to the next visible line start.
7381 Functions get_next_display_element and set_iterator_to_next are
7382 separate because I find this arrangement easier to handle than a
7383 get_next_display_element function that also increments IT's
7384 position. The way it is we can first look at an iterator's current
7385 display element, decide whether it fits on a line, and if it does,
7386 increment the iterator position. The other way around we probably
7387 would either need a flag indicating whether the iterator has to be
7388 incremented the next time, or we would have to implement a
7389 decrement position function which would not be easy to write. */
7391 void
7392 set_iterator_to_next (struct it *it, bool reseat_p)
7394 /* Reset flags indicating start and end of a sequence of characters
7395 with box. Reset them at the start of this function because
7396 moving the iterator to a new position might set them. */
7397 it->start_of_box_run_p = it->end_of_box_run_p = false;
7399 switch (it->method)
7401 case GET_FROM_BUFFER:
7402 /* The current display element of IT is a character from
7403 current_buffer. Advance in the buffer, and maybe skip over
7404 invisible lines that are so because of selective display. */
7405 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7406 reseat_at_next_visible_line_start (it, false);
7407 else if (it->cmp_it.id >= 0)
7409 /* We are currently getting glyphs from a composition. */
7410 if (! it->bidi_p)
7412 IT_CHARPOS (*it) += it->cmp_it.nchars;
7413 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7415 else
7417 int i;
7419 /* Update IT's char/byte positions to point to the first
7420 character of the next grapheme cluster, or to the
7421 character visually after the current composition. */
7422 for (i = 0; i < it->cmp_it.nchars; i++)
7423 bidi_move_to_visually_next (&it->bidi_it);
7424 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7425 IT_CHARPOS (*it) = it->bidi_it.charpos;
7428 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7429 && it->cmp_it.to < it->cmp_it.nglyphs)
7431 /* Composition created while scanning forward. Proceed
7432 to the next grapheme cluster. */
7433 it->cmp_it.from = it->cmp_it.to;
7435 else if ((it->bidi_p && it->cmp_it.reversed_p)
7436 && it->cmp_it.from > 0)
7438 /* Composition created while scanning backward. Proceed
7439 to the previous grapheme cluster. */
7440 it->cmp_it.to = it->cmp_it.from;
7442 else
7444 /* No more grapheme clusters in this composition.
7445 Find the next stop position. */
7446 ptrdiff_t stop = it->end_charpos;
7448 if (it->bidi_it.scan_dir < 0)
7449 /* Now we are scanning backward and don't know
7450 where to stop. */
7451 stop = -1;
7452 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7453 IT_BYTEPOS (*it), stop, Qnil);
7456 else
7458 eassert (it->len != 0);
7460 if (!it->bidi_p)
7462 IT_BYTEPOS (*it) += it->len;
7463 IT_CHARPOS (*it) += 1;
7465 else
7467 int prev_scan_dir = it->bidi_it.scan_dir;
7468 /* If this is a new paragraph, determine its base
7469 direction (a.k.a. its base embedding level). */
7470 if (it->bidi_it.new_paragraph)
7471 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7472 false);
7473 bidi_move_to_visually_next (&it->bidi_it);
7474 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7475 IT_CHARPOS (*it) = it->bidi_it.charpos;
7476 if (prev_scan_dir != it->bidi_it.scan_dir)
7478 /* As the scan direction was changed, we must
7479 re-compute the stop position for composition. */
7480 ptrdiff_t stop = it->end_charpos;
7481 if (it->bidi_it.scan_dir < 0)
7482 stop = -1;
7483 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7484 IT_BYTEPOS (*it), stop, Qnil);
7487 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7489 break;
7491 case GET_FROM_C_STRING:
7492 /* Current display element of IT is from a C string. */
7493 if (!it->bidi_p
7494 /* If the string position is beyond string's end, it means
7495 next_element_from_c_string is padding the string with
7496 blanks, in which case we bypass the bidi iterator,
7497 because it cannot deal with such virtual characters. */
7498 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7500 IT_BYTEPOS (*it) += it->len;
7501 IT_CHARPOS (*it) += 1;
7503 else
7505 bidi_move_to_visually_next (&it->bidi_it);
7506 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7507 IT_CHARPOS (*it) = it->bidi_it.charpos;
7509 break;
7511 case GET_FROM_DISPLAY_VECTOR:
7512 /* Current display element of IT is from a display table entry.
7513 Advance in the display table definition. Reset it to null if
7514 end reached, and continue with characters from buffers/
7515 strings. */
7516 ++it->current.dpvec_index;
7518 /* Restore face of the iterator to what they were before the
7519 display vector entry (these entries may contain faces). */
7520 it->face_id = it->saved_face_id;
7522 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7524 bool recheck_faces = it->ellipsis_p;
7526 if (it->s)
7527 it->method = GET_FROM_C_STRING;
7528 else if (STRINGP (it->string))
7529 it->method = GET_FROM_STRING;
7530 else
7532 it->method = GET_FROM_BUFFER;
7533 it->object = it->w->contents;
7536 it->dpvec = NULL;
7537 it->current.dpvec_index = -1;
7539 /* Skip over characters which were displayed via IT->dpvec. */
7540 if (it->dpvec_char_len < 0)
7541 reseat_at_next_visible_line_start (it, true);
7542 else if (it->dpvec_char_len > 0)
7544 it->len = it->dpvec_char_len;
7545 set_iterator_to_next (it, reseat_p);
7548 /* Maybe recheck faces after display vector. */
7549 if (recheck_faces)
7551 if (it->method == GET_FROM_STRING)
7552 it->stop_charpos = IT_STRING_CHARPOS (*it);
7553 else
7554 it->stop_charpos = IT_CHARPOS (*it);
7557 break;
7559 case GET_FROM_STRING:
7560 /* Current display element is a character from a Lisp string. */
7561 eassert (it->s == NULL && STRINGP (it->string));
7562 /* Don't advance past string end. These conditions are true
7563 when set_iterator_to_next is called at the end of
7564 get_next_display_element, in which case the Lisp string is
7565 already exhausted, and all we want is pop the iterator
7566 stack. */
7567 if (it->current.overlay_string_index >= 0)
7569 /* This is an overlay string, so there's no padding with
7570 spaces, and the number of characters in the string is
7571 where the string ends. */
7572 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7573 goto consider_string_end;
7575 else
7577 /* Not an overlay string. There could be padding, so test
7578 against it->end_charpos. */
7579 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7580 goto consider_string_end;
7582 if (it->cmp_it.id >= 0)
7584 /* We are delivering display elements from a composition.
7585 Update the string position past the grapheme cluster
7586 we've just processed. */
7587 if (! it->bidi_p)
7589 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7590 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7592 else
7594 int i;
7596 for (i = 0; i < it->cmp_it.nchars; i++)
7597 bidi_move_to_visually_next (&it->bidi_it);
7598 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7599 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7602 /* Did we exhaust all the grapheme clusters of this
7603 composition? */
7604 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7605 && (it->cmp_it.to < it->cmp_it.nglyphs))
7607 /* Not all the grapheme clusters were processed yet;
7608 advance to the next cluster. */
7609 it->cmp_it.from = it->cmp_it.to;
7611 else if ((it->bidi_p && it->cmp_it.reversed_p)
7612 && it->cmp_it.from > 0)
7614 /* Likewise: advance to the next cluster, but going in
7615 the reverse direction. */
7616 it->cmp_it.to = it->cmp_it.from;
7618 else
7620 /* This composition was fully processed; find the next
7621 candidate place for checking for composed
7622 characters. */
7623 /* Always limit string searches to the string length;
7624 any padding spaces are not part of the string, and
7625 there cannot be any compositions in that padding. */
7626 ptrdiff_t stop = SCHARS (it->string);
7628 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7629 stop = -1;
7630 else if (it->end_charpos < stop)
7632 /* Cf. PRECISION in reseat_to_string: we might be
7633 limited in how many of the string characters we
7634 need to deliver. */
7635 stop = it->end_charpos;
7637 composition_compute_stop_pos (&it->cmp_it,
7638 IT_STRING_CHARPOS (*it),
7639 IT_STRING_BYTEPOS (*it), stop,
7640 it->string);
7643 else
7645 if (!it->bidi_p
7646 /* If the string position is beyond string's end, it
7647 means next_element_from_string is padding the string
7648 with blanks, in which case we bypass the bidi
7649 iterator, because it cannot deal with such virtual
7650 characters. */
7651 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7653 IT_STRING_BYTEPOS (*it) += it->len;
7654 IT_STRING_CHARPOS (*it) += 1;
7656 else
7658 int prev_scan_dir = it->bidi_it.scan_dir;
7660 bidi_move_to_visually_next (&it->bidi_it);
7661 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7662 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7663 /* If the scan direction changes, we may need to update
7664 the place where to check for composed characters. */
7665 if (prev_scan_dir != it->bidi_it.scan_dir)
7667 ptrdiff_t stop = SCHARS (it->string);
7669 if (it->bidi_it.scan_dir < 0)
7670 stop = -1;
7671 else if (it->end_charpos < stop)
7672 stop = it->end_charpos;
7674 composition_compute_stop_pos (&it->cmp_it,
7675 IT_STRING_CHARPOS (*it),
7676 IT_STRING_BYTEPOS (*it), stop,
7677 it->string);
7682 consider_string_end:
7684 if (it->current.overlay_string_index >= 0)
7686 /* IT->string is an overlay string. Advance to the
7687 next, if there is one. */
7688 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7690 it->ellipsis_p = false;
7691 next_overlay_string (it);
7692 if (it->ellipsis_p)
7693 setup_for_ellipsis (it, 0);
7696 else
7698 /* IT->string is not an overlay string. If we reached
7699 its end, and there is something on IT->stack, proceed
7700 with what is on the stack. This can be either another
7701 string, this time an overlay string, or a buffer. */
7702 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7703 && it->sp > 0)
7705 pop_it (it);
7706 if (it->method == GET_FROM_STRING)
7707 goto consider_string_end;
7710 break;
7712 case GET_FROM_IMAGE:
7713 case GET_FROM_STRETCH:
7714 case GET_FROM_XWIDGET:
7716 /* The position etc with which we have to proceed are on
7717 the stack. The position may be at the end of a string,
7718 if the `display' property takes up the whole string. */
7719 eassert (it->sp > 0);
7720 pop_it (it);
7721 if (it->method == GET_FROM_STRING)
7722 goto consider_string_end;
7723 break;
7725 default:
7726 /* There are no other methods defined, so this should be a bug. */
7727 emacs_abort ();
7730 eassert (it->method != GET_FROM_STRING
7731 || (STRINGP (it->string)
7732 && IT_STRING_CHARPOS (*it) >= 0));
7735 /* Load IT's display element fields with information about the next
7736 display element which comes from a display table entry or from the
7737 result of translating a control character to one of the forms `^C'
7738 or `\003'.
7740 IT->dpvec holds the glyphs to return as characters.
7741 IT->saved_face_id holds the face id before the display vector--it
7742 is restored into IT->face_id in set_iterator_to_next. */
7744 static bool
7745 next_element_from_display_vector (struct it *it)
7747 Lisp_Object gc;
7748 int prev_face_id = it->face_id;
7749 int next_face_id;
7751 /* Precondition. */
7752 eassert (it->dpvec && it->current.dpvec_index >= 0);
7754 it->face_id = it->saved_face_id;
7756 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7757 That seemed totally bogus - so I changed it... */
7758 gc = it->dpvec[it->current.dpvec_index];
7760 if (GLYPH_CODE_P (gc))
7762 struct face *this_face, *prev_face, *next_face;
7764 it->c = GLYPH_CODE_CHAR (gc);
7765 it->len = CHAR_BYTES (it->c);
7767 /* The entry may contain a face id to use. Such a face id is
7768 the id of a Lisp face, not a realized face. A face id of
7769 zero means no face is specified. */
7770 if (it->dpvec_face_id >= 0)
7771 it->face_id = it->dpvec_face_id;
7772 else
7774 int lface_id = GLYPH_CODE_FACE (gc);
7775 if (lface_id > 0)
7776 it->face_id = merge_faces (it->f, Qt, lface_id,
7777 it->saved_face_id);
7780 /* Glyphs in the display vector could have the box face, so we
7781 need to set the related flags in the iterator, as
7782 appropriate. */
7783 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7784 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7786 /* Is this character the first character of a box-face run? */
7787 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7788 && (!prev_face
7789 || prev_face->box == FACE_NO_BOX));
7791 /* For the last character of the box-face run, we need to look
7792 either at the next glyph from the display vector, or at the
7793 face we saw before the display vector. */
7794 next_face_id = it->saved_face_id;
7795 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7797 if (it->dpvec_face_id >= 0)
7798 next_face_id = it->dpvec_face_id;
7799 else
7801 int lface_id =
7802 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7804 if (lface_id > 0)
7805 next_face_id = merge_faces (it->f, Qt, lface_id,
7806 it->saved_face_id);
7809 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7810 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7811 && (!next_face
7812 || next_face->box == FACE_NO_BOX));
7813 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7815 else
7816 /* Display table entry is invalid. Return a space. */
7817 it->c = ' ', it->len = 1;
7819 /* Don't change position and object of the iterator here. They are
7820 still the values of the character that had this display table
7821 entry or was translated, and that's what we want. */
7822 it->what = IT_CHARACTER;
7823 return true;
7826 /* Get the first element of string/buffer in the visual order, after
7827 being reseated to a new position in a string or a buffer. */
7828 static void
7829 get_visually_first_element (struct it *it)
7831 bool string_p = STRINGP (it->string) || it->s;
7832 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7833 ptrdiff_t bob = (string_p ? 0 : BEGV);
7835 if (STRINGP (it->string))
7837 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7838 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7840 else
7842 it->bidi_it.charpos = IT_CHARPOS (*it);
7843 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7846 if (it->bidi_it.charpos == eob)
7848 /* Nothing to do, but reset the FIRST_ELT flag, like
7849 bidi_paragraph_init does, because we are not going to
7850 call it. */
7851 it->bidi_it.first_elt = false;
7853 else if (it->bidi_it.charpos == bob
7854 || (!string_p
7855 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7856 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7858 /* If we are at the beginning of a line/string, we can produce
7859 the next element right away. */
7860 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7861 bidi_move_to_visually_next (&it->bidi_it);
7863 else
7865 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7867 /* We need to prime the bidi iterator starting at the line's or
7868 string's beginning, before we will be able to produce the
7869 next element. */
7870 if (string_p)
7871 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7872 else
7873 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7874 IT_BYTEPOS (*it), -1,
7875 &it->bidi_it.bytepos);
7876 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7879 /* Now return to buffer/string position where we were asked
7880 to get the next display element, and produce that. */
7881 bidi_move_to_visually_next (&it->bidi_it);
7883 while (it->bidi_it.bytepos != orig_bytepos
7884 && it->bidi_it.charpos < eob);
7887 /* Adjust IT's position information to where we ended up. */
7888 if (STRINGP (it->string))
7890 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7891 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7893 else
7895 IT_CHARPOS (*it) = it->bidi_it.charpos;
7896 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7899 if (STRINGP (it->string) || !it->s)
7901 ptrdiff_t stop, charpos, bytepos;
7903 if (STRINGP (it->string))
7905 eassert (!it->s);
7906 stop = SCHARS (it->string);
7907 if (stop > it->end_charpos)
7908 stop = it->end_charpos;
7909 charpos = IT_STRING_CHARPOS (*it);
7910 bytepos = IT_STRING_BYTEPOS (*it);
7912 else
7914 stop = it->end_charpos;
7915 charpos = IT_CHARPOS (*it);
7916 bytepos = IT_BYTEPOS (*it);
7918 if (it->bidi_it.scan_dir < 0)
7919 stop = -1;
7920 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7921 it->string);
7925 /* Load IT with the next display element from Lisp string IT->string.
7926 IT->current.string_pos is the current position within the string.
7927 If IT->current.overlay_string_index >= 0, the Lisp string is an
7928 overlay string. */
7930 static bool
7931 next_element_from_string (struct it *it)
7933 struct text_pos position;
7935 eassert (STRINGP (it->string));
7936 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7937 eassert (IT_STRING_CHARPOS (*it) >= 0);
7938 position = it->current.string_pos;
7940 /* With bidi reordering, the character to display might not be the
7941 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7942 that we were reseat()ed to a new string, whose paragraph
7943 direction is not known. */
7944 if (it->bidi_p && it->bidi_it.first_elt)
7946 get_visually_first_element (it);
7947 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7950 /* Time to check for invisible text? */
7951 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7953 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7955 if (!(!it->bidi_p
7956 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7957 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7959 /* With bidi non-linear iteration, we could find
7960 ourselves far beyond the last computed stop_charpos,
7961 with several other stop positions in between that we
7962 missed. Scan them all now, in buffer's logical
7963 order, until we find and handle the last stop_charpos
7964 that precedes our current position. */
7965 handle_stop_backwards (it, it->stop_charpos);
7966 return GET_NEXT_DISPLAY_ELEMENT (it);
7968 else
7970 if (it->bidi_p)
7972 /* Take note of the stop position we just moved
7973 across, for when we will move back across it. */
7974 it->prev_stop = it->stop_charpos;
7975 /* If we are at base paragraph embedding level, take
7976 note of the last stop position seen at this
7977 level. */
7978 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7979 it->base_level_stop = it->stop_charpos;
7981 handle_stop (it);
7983 /* Since a handler may have changed IT->method, we must
7984 recurse here. */
7985 return GET_NEXT_DISPLAY_ELEMENT (it);
7988 else if (it->bidi_p
7989 /* If we are before prev_stop, we may have overstepped
7990 on our way backwards a stop_pos, and if so, we need
7991 to handle that stop_pos. */
7992 && IT_STRING_CHARPOS (*it) < it->prev_stop
7993 /* We can sometimes back up for reasons that have nothing
7994 to do with bidi reordering. E.g., compositions. The
7995 code below is only needed when we are above the base
7996 embedding level, so test for that explicitly. */
7997 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7999 /* If we lost track of base_level_stop, we have no better
8000 place for handle_stop_backwards to start from than string
8001 beginning. This happens, e.g., when we were reseated to
8002 the previous screenful of text by vertical-motion. */
8003 if (it->base_level_stop <= 0
8004 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8005 it->base_level_stop = 0;
8006 handle_stop_backwards (it, it->base_level_stop);
8007 return GET_NEXT_DISPLAY_ELEMENT (it);
8011 if (it->current.overlay_string_index >= 0)
8013 /* Get the next character from an overlay string. In overlay
8014 strings, there is no field width or padding with spaces to
8015 do. */
8016 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8018 it->what = IT_EOB;
8019 return false;
8021 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8022 IT_STRING_BYTEPOS (*it),
8023 it->bidi_it.scan_dir < 0
8024 ? -1
8025 : SCHARS (it->string))
8026 && next_element_from_composition (it))
8028 return true;
8030 else if (STRING_MULTIBYTE (it->string))
8032 const unsigned char *s = (SDATA (it->string)
8033 + IT_STRING_BYTEPOS (*it));
8034 it->c = string_char_and_length (s, &it->len);
8036 else
8038 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8039 it->len = 1;
8042 else
8044 /* Get the next character from a Lisp string that is not an
8045 overlay string. Such strings come from the mode line, for
8046 example. We may have to pad with spaces, or truncate the
8047 string. See also next_element_from_c_string. */
8048 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8050 it->what = IT_EOB;
8051 return false;
8053 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8055 /* Pad with spaces. */
8056 it->c = ' ', it->len = 1;
8057 CHARPOS (position) = BYTEPOS (position) = -1;
8059 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8060 IT_STRING_BYTEPOS (*it),
8061 it->bidi_it.scan_dir < 0
8062 ? -1
8063 : it->string_nchars)
8064 && next_element_from_composition (it))
8066 return true;
8068 else if (STRING_MULTIBYTE (it->string))
8070 const unsigned char *s = (SDATA (it->string)
8071 + IT_STRING_BYTEPOS (*it));
8072 it->c = string_char_and_length (s, &it->len);
8074 else
8076 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8077 it->len = 1;
8081 /* Record what we have and where it came from. */
8082 it->what = IT_CHARACTER;
8083 it->object = it->string;
8084 it->position = position;
8085 return true;
8089 /* Load IT with next display element from C string IT->s.
8090 IT->string_nchars is the maximum number of characters to return
8091 from the string. IT->end_charpos may be greater than
8092 IT->string_nchars when this function is called, in which case we
8093 may have to return padding spaces. Value is false if end of string
8094 reached, including padding spaces. */
8096 static bool
8097 next_element_from_c_string (struct it *it)
8099 bool success_p = true;
8101 eassert (it->s);
8102 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8103 it->what = IT_CHARACTER;
8104 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8105 it->object = make_number (0);
8107 /* With bidi reordering, the character to display might not be the
8108 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8109 we were reseated to a new string, whose paragraph direction is
8110 not known. */
8111 if (it->bidi_p && it->bidi_it.first_elt)
8112 get_visually_first_element (it);
8114 /* IT's position can be greater than IT->string_nchars in case a
8115 field width or precision has been specified when the iterator was
8116 initialized. */
8117 if (IT_CHARPOS (*it) >= it->end_charpos)
8119 /* End of the game. */
8120 it->what = IT_EOB;
8121 success_p = false;
8123 else if (IT_CHARPOS (*it) >= it->string_nchars)
8125 /* Pad with spaces. */
8126 it->c = ' ', it->len = 1;
8127 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8129 else if (it->multibyte_p)
8130 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8131 else
8132 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8134 return success_p;
8138 /* Set up IT to return characters from an ellipsis, if appropriate.
8139 The definition of the ellipsis glyphs may come from a display table
8140 entry. This function fills IT with the first glyph from the
8141 ellipsis if an ellipsis is to be displayed. */
8143 static bool
8144 next_element_from_ellipsis (struct it *it)
8146 if (it->selective_display_ellipsis_p)
8147 setup_for_ellipsis (it, it->len);
8148 else
8150 /* The face at the current position may be different from the
8151 face we find after the invisible text. Remember what it
8152 was in IT->saved_face_id, and signal that it's there by
8153 setting face_before_selective_p. */
8154 it->saved_face_id = it->face_id;
8155 it->method = GET_FROM_BUFFER;
8156 it->object = it->w->contents;
8157 reseat_at_next_visible_line_start (it, true);
8158 it->face_before_selective_p = true;
8161 return GET_NEXT_DISPLAY_ELEMENT (it);
8165 /* Deliver an image display element. The iterator IT is already
8166 filled with image information (done in handle_display_prop). Value
8167 is always true. */
8170 static bool
8171 next_element_from_image (struct it *it)
8173 it->what = IT_IMAGE;
8174 return true;
8177 static bool
8178 next_element_from_xwidget (struct it *it)
8180 it->what = IT_XWIDGET;
8181 return true;
8185 /* Fill iterator IT with next display element from a stretch glyph
8186 property. IT->object is the value of the text property. Value is
8187 always true. */
8189 static bool
8190 next_element_from_stretch (struct it *it)
8192 it->what = IT_STRETCH;
8193 return true;
8196 /* Scan backwards from IT's current position until we find a stop
8197 position, or until BEGV. This is called when we find ourself
8198 before both the last known prev_stop and base_level_stop while
8199 reordering bidirectional text. */
8201 static void
8202 compute_stop_pos_backwards (struct it *it)
8204 const int SCAN_BACK_LIMIT = 1000;
8205 struct text_pos pos;
8206 struct display_pos save_current = it->current;
8207 struct text_pos save_position = it->position;
8208 ptrdiff_t charpos = IT_CHARPOS (*it);
8209 ptrdiff_t where_we_are = charpos;
8210 ptrdiff_t save_stop_pos = it->stop_charpos;
8211 ptrdiff_t save_end_pos = it->end_charpos;
8213 eassert (NILP (it->string) && !it->s);
8214 eassert (it->bidi_p);
8215 it->bidi_p = false;
8218 it->end_charpos = min (charpos + 1, ZV);
8219 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8220 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8221 reseat_1 (it, pos, false);
8222 compute_stop_pos (it);
8223 /* We must advance forward, right? */
8224 if (it->stop_charpos <= charpos)
8225 emacs_abort ();
8227 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8229 if (it->stop_charpos <= where_we_are)
8230 it->prev_stop = it->stop_charpos;
8231 else
8232 it->prev_stop = BEGV;
8233 it->bidi_p = true;
8234 it->current = save_current;
8235 it->position = save_position;
8236 it->stop_charpos = save_stop_pos;
8237 it->end_charpos = save_end_pos;
8240 /* Scan forward from CHARPOS in the current buffer/string, until we
8241 find a stop position > current IT's position. Then handle the stop
8242 position before that. This is called when we bump into a stop
8243 position while reordering bidirectional text. CHARPOS should be
8244 the last previously processed stop_pos (or BEGV/0, if none were
8245 processed yet) whose position is less that IT's current
8246 position. */
8248 static void
8249 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8251 bool bufp = !STRINGP (it->string);
8252 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8253 struct display_pos save_current = it->current;
8254 struct text_pos save_position = it->position;
8255 struct text_pos pos1;
8256 ptrdiff_t next_stop;
8258 /* Scan in strict logical order. */
8259 eassert (it->bidi_p);
8260 it->bidi_p = false;
8263 it->prev_stop = charpos;
8264 if (bufp)
8266 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8267 reseat_1 (it, pos1, false);
8269 else
8270 it->current.string_pos = string_pos (charpos, it->string);
8271 compute_stop_pos (it);
8272 /* We must advance forward, right? */
8273 if (it->stop_charpos <= it->prev_stop)
8274 emacs_abort ();
8275 charpos = it->stop_charpos;
8277 while (charpos <= where_we_are);
8279 it->bidi_p = true;
8280 it->current = save_current;
8281 it->position = save_position;
8282 next_stop = it->stop_charpos;
8283 it->stop_charpos = it->prev_stop;
8284 handle_stop (it);
8285 it->stop_charpos = next_stop;
8288 /* Load IT with the next display element from current_buffer. Value
8289 is false if end of buffer reached. IT->stop_charpos is the next
8290 position at which to stop and check for text properties or buffer
8291 end. */
8293 static bool
8294 next_element_from_buffer (struct it *it)
8296 bool success_p = true;
8298 eassert (IT_CHARPOS (*it) >= BEGV);
8299 eassert (NILP (it->string) && !it->s);
8300 eassert (!it->bidi_p
8301 || (EQ (it->bidi_it.string.lstring, Qnil)
8302 && it->bidi_it.string.s == NULL));
8304 /* With bidi reordering, the character to display might not be the
8305 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8306 we were reseat()ed to a new buffer position, which is potentially
8307 a different paragraph. */
8308 if (it->bidi_p && it->bidi_it.first_elt)
8310 get_visually_first_element (it);
8311 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8314 if (IT_CHARPOS (*it) >= it->stop_charpos)
8316 if (IT_CHARPOS (*it) >= it->end_charpos)
8318 bool overlay_strings_follow_p;
8320 /* End of the game, except when overlay strings follow that
8321 haven't been returned yet. */
8322 if (it->overlay_strings_at_end_processed_p)
8323 overlay_strings_follow_p = false;
8324 else
8326 it->overlay_strings_at_end_processed_p = true;
8327 overlay_strings_follow_p = get_overlay_strings (it, 0);
8330 if (overlay_strings_follow_p)
8331 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8332 else
8334 it->what = IT_EOB;
8335 it->position = it->current.pos;
8336 success_p = false;
8339 else if (!(!it->bidi_p
8340 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8341 || IT_CHARPOS (*it) == it->stop_charpos))
8343 /* With bidi non-linear iteration, we could find ourselves
8344 far beyond the last computed stop_charpos, with several
8345 other stop positions in between that we missed. Scan
8346 them all now, in buffer's logical order, until we find
8347 and handle the last stop_charpos that precedes our
8348 current position. */
8349 handle_stop_backwards (it, it->stop_charpos);
8350 it->ignore_overlay_strings_at_pos_p = false;
8351 return GET_NEXT_DISPLAY_ELEMENT (it);
8353 else
8355 if (it->bidi_p)
8357 /* Take note of the stop position we just moved across,
8358 for when we will move back across it. */
8359 it->prev_stop = it->stop_charpos;
8360 /* If we are at base paragraph embedding level, take
8361 note of the last stop position seen at this
8362 level. */
8363 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8364 it->base_level_stop = it->stop_charpos;
8366 handle_stop (it);
8367 it->ignore_overlay_strings_at_pos_p = false;
8368 return GET_NEXT_DISPLAY_ELEMENT (it);
8371 else if (it->bidi_p
8372 /* If we are before prev_stop, we may have overstepped on
8373 our way backwards a stop_pos, and if so, we need to
8374 handle that stop_pos. */
8375 && IT_CHARPOS (*it) < it->prev_stop
8376 /* We can sometimes back up for reasons that have nothing
8377 to do with bidi reordering. E.g., compositions. The
8378 code below is only needed when we are above the base
8379 embedding level, so test for that explicitly. */
8380 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8382 if (it->base_level_stop <= 0
8383 || IT_CHARPOS (*it) < it->base_level_stop)
8385 /* If we lost track of base_level_stop, we need to find
8386 prev_stop by looking backwards. This happens, e.g., when
8387 we were reseated to the previous screenful of text by
8388 vertical-motion. */
8389 it->base_level_stop = BEGV;
8390 compute_stop_pos_backwards (it);
8391 handle_stop_backwards (it, it->prev_stop);
8393 else
8394 handle_stop_backwards (it, it->base_level_stop);
8395 it->ignore_overlay_strings_at_pos_p = false;
8396 return GET_NEXT_DISPLAY_ELEMENT (it);
8398 else
8400 /* No face changes, overlays etc. in sight, so just return a
8401 character from current_buffer. */
8402 unsigned char *p;
8403 ptrdiff_t stop;
8405 /* We moved to the next buffer position, so any info about
8406 previously seen overlays is no longer valid. */
8407 it->ignore_overlay_strings_at_pos_p = false;
8409 /* Maybe run the redisplay end trigger hook. Performance note:
8410 This doesn't seem to cost measurable time. */
8411 if (it->redisplay_end_trigger_charpos
8412 && it->glyph_row
8413 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8414 run_redisplay_end_trigger_hook (it);
8416 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8417 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8418 stop)
8419 && next_element_from_composition (it))
8421 return true;
8424 /* Get the next character, maybe multibyte. */
8425 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8426 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8427 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8428 else
8429 it->c = *p, it->len = 1;
8431 /* Record what we have and where it came from. */
8432 it->what = IT_CHARACTER;
8433 it->object = it->w->contents;
8434 it->position = it->current.pos;
8436 /* Normally we return the character found above, except when we
8437 really want to return an ellipsis for selective display. */
8438 if (it->selective)
8440 if (it->c == '\n')
8442 /* A value of selective > 0 means hide lines indented more
8443 than that number of columns. */
8444 if (it->selective > 0
8445 && IT_CHARPOS (*it) + 1 < ZV
8446 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8447 IT_BYTEPOS (*it) + 1,
8448 it->selective))
8450 success_p = next_element_from_ellipsis (it);
8451 it->dpvec_char_len = -1;
8454 else if (it->c == '\r' && it->selective == -1)
8456 /* A value of selective == -1 means that everything from the
8457 CR to the end of the line is invisible, with maybe an
8458 ellipsis displayed for it. */
8459 success_p = next_element_from_ellipsis (it);
8460 it->dpvec_char_len = -1;
8465 /* Value is false if end of buffer reached. */
8466 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8467 return success_p;
8471 /* Run the redisplay end trigger hook for IT. */
8473 static void
8474 run_redisplay_end_trigger_hook (struct it *it)
8476 /* IT->glyph_row should be non-null, i.e. we should be actually
8477 displaying something, or otherwise we should not run the hook. */
8478 eassert (it->glyph_row);
8480 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8481 it->redisplay_end_trigger_charpos = 0;
8483 /* Since we are *trying* to run these functions, don't try to run
8484 them again, even if they get an error. */
8485 wset_redisplay_end_trigger (it->w, Qnil);
8486 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8487 make_number (charpos));
8489 /* Notice if it changed the face of the character we are on. */
8490 handle_face_prop (it);
8494 /* Deliver a composition display element. Unlike the other
8495 next_element_from_XXX, this function is not registered in the array
8496 get_next_element[]. It is called from next_element_from_buffer and
8497 next_element_from_string when necessary. */
8499 static bool
8500 next_element_from_composition (struct it *it)
8502 it->what = IT_COMPOSITION;
8503 it->len = it->cmp_it.nbytes;
8504 if (STRINGP (it->string))
8506 if (it->c < 0)
8508 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8509 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8510 return false;
8512 it->position = it->current.string_pos;
8513 it->object = it->string;
8514 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8515 IT_STRING_BYTEPOS (*it), it->string);
8517 else
8519 if (it->c < 0)
8521 IT_CHARPOS (*it) += it->cmp_it.nchars;
8522 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8523 if (it->bidi_p)
8525 if (it->bidi_it.new_paragraph)
8526 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8527 false);
8528 /* Resync the bidi iterator with IT's new position.
8529 FIXME: this doesn't support bidirectional text. */
8530 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8531 bidi_move_to_visually_next (&it->bidi_it);
8533 return false;
8535 it->position = it->current.pos;
8536 it->object = it->w->contents;
8537 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8538 IT_BYTEPOS (*it), Qnil);
8540 return true;
8545 /***********************************************************************
8546 Moving an iterator without producing glyphs
8547 ***********************************************************************/
8549 /* Check if iterator is at a position corresponding to a valid buffer
8550 position after some move_it_ call. */
8552 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8553 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8556 /* Move iterator IT to a specified buffer or X position within one
8557 line on the display without producing glyphs.
8559 OP should be a bit mask including some or all of these bits:
8560 MOVE_TO_X: Stop upon reaching x-position TO_X.
8561 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8562 Regardless of OP's value, stop upon reaching the end of the display line.
8564 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8565 This means, in particular, that TO_X includes window's horizontal
8566 scroll amount.
8568 The return value has several possible values that
8569 say what condition caused the scan to stop:
8571 MOVE_POS_MATCH_OR_ZV
8572 - when TO_POS or ZV was reached.
8574 MOVE_X_REACHED
8575 -when TO_X was reached before TO_POS or ZV were reached.
8577 MOVE_LINE_CONTINUED
8578 - when we reached the end of the display area and the line must
8579 be continued.
8581 MOVE_LINE_TRUNCATED
8582 - when we reached the end of the display area and the line is
8583 truncated.
8585 MOVE_NEWLINE_OR_CR
8586 - when we stopped at a line end, i.e. a newline or a CR and selective
8587 display is on. */
8589 static enum move_it_result
8590 move_it_in_display_line_to (struct it *it,
8591 ptrdiff_t to_charpos, int to_x,
8592 enum move_operation_enum op)
8594 enum move_it_result result = MOVE_UNDEFINED;
8595 struct glyph_row *saved_glyph_row;
8596 struct it wrap_it, atpos_it, atx_it, ppos_it;
8597 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8598 void *ppos_data = NULL;
8599 bool may_wrap = false;
8600 enum it_method prev_method = it->method;
8601 ptrdiff_t closest_pos UNINIT;
8602 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8603 bool saw_smaller_pos = prev_pos < to_charpos;
8605 /* Don't produce glyphs in produce_glyphs. */
8606 saved_glyph_row = it->glyph_row;
8607 it->glyph_row = NULL;
8609 /* Use wrap_it to save a copy of IT wherever a word wrap could
8610 occur. Use atpos_it to save a copy of IT at the desired buffer
8611 position, if found, so that we can scan ahead and check if the
8612 word later overshoots the window edge. Use atx_it similarly, for
8613 pixel positions. */
8614 wrap_it.sp = -1;
8615 atpos_it.sp = -1;
8616 atx_it.sp = -1;
8618 /* Use ppos_it under bidi reordering to save a copy of IT for the
8619 initial position. We restore that position in IT when we have
8620 scanned the entire display line without finding a match for
8621 TO_CHARPOS and all the character positions are greater than
8622 TO_CHARPOS. We then restart the scan from the initial position,
8623 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8624 the closest to TO_CHARPOS. */
8625 if (it->bidi_p)
8627 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8629 SAVE_IT (ppos_it, *it, ppos_data);
8630 closest_pos = IT_CHARPOS (*it);
8632 else
8633 closest_pos = ZV;
8636 #define BUFFER_POS_REACHED_P() \
8637 ((op & MOVE_TO_POS) != 0 \
8638 && BUFFERP (it->object) \
8639 && (IT_CHARPOS (*it) == to_charpos \
8640 || ((!it->bidi_p \
8641 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8642 && IT_CHARPOS (*it) > to_charpos) \
8643 || (it->what == IT_COMPOSITION \
8644 && ((IT_CHARPOS (*it) > to_charpos \
8645 && to_charpos >= it->cmp_it.charpos) \
8646 || (IT_CHARPOS (*it) < to_charpos \
8647 && to_charpos <= it->cmp_it.charpos)))) \
8648 && (it->method == GET_FROM_BUFFER \
8649 || (it->method == GET_FROM_DISPLAY_VECTOR \
8650 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8652 /* If there's a line-/wrap-prefix, handle it. */
8653 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8654 handle_line_prefix (it);
8656 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8657 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8659 while (true)
8661 int x, i, ascent = 0, descent = 0;
8663 /* Utility macro to reset an iterator with x, ascent, and descent. */
8664 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8665 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8666 (IT)->max_descent = descent)
8668 /* Stop if we move beyond TO_CHARPOS (after an image or a
8669 display string or stretch glyph). */
8670 if ((op & MOVE_TO_POS) != 0
8671 && BUFFERP (it->object)
8672 && it->method == GET_FROM_BUFFER
8673 && (((!it->bidi_p
8674 /* When the iterator is at base embedding level, we
8675 are guaranteed that characters are delivered for
8676 display in strictly increasing order of their
8677 buffer positions. */
8678 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8679 && IT_CHARPOS (*it) > to_charpos)
8680 || (it->bidi_p
8681 && (prev_method == GET_FROM_IMAGE
8682 || prev_method == GET_FROM_STRETCH
8683 || prev_method == GET_FROM_STRING)
8684 /* Passed TO_CHARPOS from left to right. */
8685 && ((prev_pos < to_charpos
8686 && IT_CHARPOS (*it) > to_charpos)
8687 /* Passed TO_CHARPOS from right to left. */
8688 || (prev_pos > to_charpos
8689 && IT_CHARPOS (*it) < to_charpos)))))
8691 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8693 result = MOVE_POS_MATCH_OR_ZV;
8694 break;
8696 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8697 /* If wrap_it is valid, the current position might be in a
8698 word that is wrapped. So, save the iterator in
8699 atpos_it and continue to see if wrapping happens. */
8700 SAVE_IT (atpos_it, *it, atpos_data);
8703 /* Stop when ZV reached.
8704 We used to stop here when TO_CHARPOS reached as well, but that is
8705 too soon if this glyph does not fit on this line. So we handle it
8706 explicitly below. */
8707 if (!get_next_display_element (it))
8709 result = MOVE_POS_MATCH_OR_ZV;
8710 break;
8713 if (it->line_wrap == TRUNCATE)
8715 if (BUFFER_POS_REACHED_P ())
8717 result = MOVE_POS_MATCH_OR_ZV;
8718 break;
8721 else
8723 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8725 if (IT_DISPLAYING_WHITESPACE (it))
8726 may_wrap = true;
8727 else if (may_wrap)
8729 /* We have reached a glyph that follows one or more
8730 whitespace characters. If the position is
8731 already found, we are done. */
8732 if (atpos_it.sp >= 0)
8734 RESTORE_IT (it, &atpos_it, atpos_data);
8735 result = MOVE_POS_MATCH_OR_ZV;
8736 goto done;
8738 if (atx_it.sp >= 0)
8740 RESTORE_IT (it, &atx_it, atx_data);
8741 result = MOVE_X_REACHED;
8742 goto done;
8744 /* Otherwise, we can wrap here. */
8745 SAVE_IT (wrap_it, *it, wrap_data);
8746 may_wrap = false;
8751 /* Remember the line height for the current line, in case
8752 the next element doesn't fit on the line. */
8753 ascent = it->max_ascent;
8754 descent = it->max_descent;
8756 /* The call to produce_glyphs will get the metrics of the
8757 display element IT is loaded with. Record the x-position
8758 before this display element, in case it doesn't fit on the
8759 line. */
8760 x = it->current_x;
8762 PRODUCE_GLYPHS (it);
8764 if (it->area != TEXT_AREA)
8766 prev_method = it->method;
8767 if (it->method == GET_FROM_BUFFER)
8768 prev_pos = IT_CHARPOS (*it);
8769 set_iterator_to_next (it, true);
8770 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8771 SET_TEXT_POS (this_line_min_pos,
8772 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8773 if (it->bidi_p
8774 && (op & MOVE_TO_POS)
8775 && IT_CHARPOS (*it) > to_charpos
8776 && IT_CHARPOS (*it) < closest_pos)
8777 closest_pos = IT_CHARPOS (*it);
8778 continue;
8781 /* The number of glyphs we get back in IT->nglyphs will normally
8782 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8783 character on a terminal frame, or (iii) a line end. For the
8784 second case, IT->nglyphs - 1 padding glyphs will be present.
8785 (On X frames, there is only one glyph produced for a
8786 composite character.)
8788 The behavior implemented below means, for continuation lines,
8789 that as many spaces of a TAB as fit on the current line are
8790 displayed there. For terminal frames, as many glyphs of a
8791 multi-glyph character are displayed in the current line, too.
8792 This is what the old redisplay code did, and we keep it that
8793 way. Under X, the whole shape of a complex character must
8794 fit on the line or it will be completely displayed in the
8795 next line.
8797 Note that both for tabs and padding glyphs, all glyphs have
8798 the same width. */
8799 if (it->nglyphs)
8801 /* More than one glyph or glyph doesn't fit on line. All
8802 glyphs have the same width. */
8803 int single_glyph_width = it->pixel_width / it->nglyphs;
8804 int new_x;
8805 int x_before_this_char = x;
8806 int hpos_before_this_char = it->hpos;
8808 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8810 new_x = x + single_glyph_width;
8812 /* We want to leave anything reaching TO_X to the caller. */
8813 if ((op & MOVE_TO_X) && new_x > to_x)
8815 if (BUFFER_POS_REACHED_P ())
8817 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8818 goto buffer_pos_reached;
8819 if (atpos_it.sp < 0)
8821 SAVE_IT (atpos_it, *it, atpos_data);
8822 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8825 else
8827 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8829 it->current_x = x;
8830 result = MOVE_X_REACHED;
8831 break;
8833 if (atx_it.sp < 0)
8835 SAVE_IT (atx_it, *it, atx_data);
8836 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8841 if (/* Lines are continued. */
8842 it->line_wrap != TRUNCATE
8843 && (/* And glyph doesn't fit on the line. */
8844 new_x > it->last_visible_x
8845 /* Or it fits exactly and we're on a window
8846 system frame. */
8847 || (new_x == it->last_visible_x
8848 && FRAME_WINDOW_P (it->f)
8849 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8850 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8851 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8853 bool moved_forward = false;
8855 if (/* IT->hpos == 0 means the very first glyph
8856 doesn't fit on the line, e.g. a wide image. */
8857 it->hpos == 0
8858 || (new_x == it->last_visible_x
8859 && FRAME_WINDOW_P (it->f)))
8861 ++it->hpos;
8862 it->current_x = new_x;
8864 /* The character's last glyph just barely fits
8865 in this row. */
8866 if (i == it->nglyphs - 1)
8868 /* If this is the destination position,
8869 return a position *before* it in this row,
8870 now that we know it fits in this row. */
8871 if (BUFFER_POS_REACHED_P ())
8873 bool can_wrap = true;
8875 /* If we are at a whitespace character
8876 that barely fits on this screen line,
8877 but the next character is also
8878 whitespace, we cannot wrap here. */
8879 if (it->line_wrap == WORD_WRAP
8880 && wrap_it.sp >= 0
8881 && may_wrap
8882 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8884 struct it tem_it;
8885 void *tem_data = NULL;
8887 SAVE_IT (tem_it, *it, tem_data);
8888 set_iterator_to_next (it, true);
8889 if (get_next_display_element (it)
8890 && IT_DISPLAYING_WHITESPACE (it))
8891 can_wrap = false;
8892 RESTORE_IT (it, &tem_it, tem_data);
8894 if (it->line_wrap != WORD_WRAP
8895 || wrap_it.sp < 0
8896 /* If we've just found whitespace
8897 where we can wrap, effectively
8898 ignore the previous wrap point --
8899 it is no longer relevant, but we
8900 won't have an opportunity to
8901 update it, since we've reached
8902 the edge of this screen line. */
8903 || (may_wrap && can_wrap
8904 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8906 it->hpos = hpos_before_this_char;
8907 it->current_x = x_before_this_char;
8908 result = MOVE_POS_MATCH_OR_ZV;
8909 break;
8911 if (it->line_wrap == WORD_WRAP
8912 && atpos_it.sp < 0)
8914 SAVE_IT (atpos_it, *it, atpos_data);
8915 atpos_it.current_x = x_before_this_char;
8916 atpos_it.hpos = hpos_before_this_char;
8920 prev_method = it->method;
8921 if (it->method == GET_FROM_BUFFER)
8922 prev_pos = IT_CHARPOS (*it);
8923 set_iterator_to_next (it, true);
8924 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8925 SET_TEXT_POS (this_line_min_pos,
8926 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8927 /* On graphical terminals, newlines may
8928 "overflow" into the fringe if
8929 overflow-newline-into-fringe is non-nil.
8930 On text terminals, and on graphical
8931 terminals with no right margin, newlines
8932 may overflow into the last glyph on the
8933 display line.*/
8934 if (!FRAME_WINDOW_P (it->f)
8935 || ((it->bidi_p
8936 && it->bidi_it.paragraph_dir == R2L)
8937 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8938 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8939 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8941 if (!get_next_display_element (it))
8943 result = MOVE_POS_MATCH_OR_ZV;
8944 break;
8946 moved_forward = true;
8947 if (BUFFER_POS_REACHED_P ())
8949 if (ITERATOR_AT_END_OF_LINE_P (it))
8950 result = MOVE_POS_MATCH_OR_ZV;
8951 else
8952 result = MOVE_LINE_CONTINUED;
8953 break;
8955 if (ITERATOR_AT_END_OF_LINE_P (it)
8956 && (it->line_wrap != WORD_WRAP
8957 || wrap_it.sp < 0
8958 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8960 result = MOVE_NEWLINE_OR_CR;
8961 break;
8966 else
8967 IT_RESET_X_ASCENT_DESCENT (it);
8969 /* If the screen line ends with whitespace, and we
8970 are under word-wrap, don't use wrap_it: it is no
8971 longer relevant, but we won't have an opportunity
8972 to update it, since we are done with this screen
8973 line. */
8974 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8975 /* If the character after the one which set the
8976 may_wrap flag is also whitespace, we can't
8977 wrap here, since the screen line cannot be
8978 wrapped in the middle of whitespace.
8979 Therefore, wrap_it _is_ relevant in that
8980 case. */
8981 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8983 /* If we've found TO_X, go back there, as we now
8984 know the last word fits on this screen line. */
8985 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8986 && atx_it.sp >= 0)
8988 RESTORE_IT (it, &atx_it, atx_data);
8989 atpos_it.sp = -1;
8990 atx_it.sp = -1;
8991 result = MOVE_X_REACHED;
8992 break;
8995 else if (wrap_it.sp >= 0)
8997 RESTORE_IT (it, &wrap_it, wrap_data);
8998 atpos_it.sp = -1;
8999 atx_it.sp = -1;
9002 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9003 IT_CHARPOS (*it)));
9004 result = MOVE_LINE_CONTINUED;
9005 break;
9008 if (BUFFER_POS_REACHED_P ())
9010 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9011 goto buffer_pos_reached;
9012 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9014 SAVE_IT (atpos_it, *it, atpos_data);
9015 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9019 if (new_x > it->first_visible_x)
9021 /* Glyph is visible. Increment number of glyphs that
9022 would be displayed. */
9023 ++it->hpos;
9027 if (result != MOVE_UNDEFINED)
9028 break;
9030 else if (BUFFER_POS_REACHED_P ())
9032 buffer_pos_reached:
9033 IT_RESET_X_ASCENT_DESCENT (it);
9034 result = MOVE_POS_MATCH_OR_ZV;
9035 break;
9037 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9039 /* Stop when TO_X specified and reached. This check is
9040 necessary here because of lines consisting of a line end,
9041 only. The line end will not produce any glyphs and we
9042 would never get MOVE_X_REACHED. */
9043 eassert (it->nglyphs == 0);
9044 result = MOVE_X_REACHED;
9045 break;
9048 /* Is this a line end? If yes, we're done. */
9049 if (ITERATOR_AT_END_OF_LINE_P (it))
9051 /* If we are past TO_CHARPOS, but never saw any character
9052 positions smaller than TO_CHARPOS, return
9053 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9054 did. */
9055 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9057 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9059 if (closest_pos < ZV)
9061 RESTORE_IT (it, &ppos_it, ppos_data);
9062 /* Don't recurse if closest_pos is equal to
9063 to_charpos, since we have just tried that. */
9064 if (closest_pos != to_charpos)
9065 move_it_in_display_line_to (it, closest_pos, -1,
9066 MOVE_TO_POS);
9067 result = MOVE_POS_MATCH_OR_ZV;
9069 else
9070 goto buffer_pos_reached;
9072 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9073 && IT_CHARPOS (*it) > to_charpos)
9074 goto buffer_pos_reached;
9075 else
9076 result = MOVE_NEWLINE_OR_CR;
9078 else
9079 result = MOVE_NEWLINE_OR_CR;
9080 /* If we've processed the newline, make sure this flag is
9081 reset, as it must only be set when the newline itself is
9082 processed. */
9083 if (result == MOVE_NEWLINE_OR_CR)
9084 it->constrain_row_ascent_descent_p = false;
9085 break;
9088 prev_method = it->method;
9089 if (it->method == GET_FROM_BUFFER)
9090 prev_pos = IT_CHARPOS (*it);
9091 /* The current display element has been consumed. Advance
9092 to the next. */
9093 set_iterator_to_next (it, true);
9094 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9095 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9096 if (IT_CHARPOS (*it) < to_charpos)
9097 saw_smaller_pos = true;
9098 if (it->bidi_p
9099 && (op & MOVE_TO_POS)
9100 && IT_CHARPOS (*it) >= to_charpos
9101 && IT_CHARPOS (*it) < closest_pos)
9102 closest_pos = IT_CHARPOS (*it);
9104 /* Stop if lines are truncated and IT's current x-position is
9105 past the right edge of the window now. */
9106 if (it->line_wrap == TRUNCATE
9107 && it->current_x >= it->last_visible_x)
9109 if (!FRAME_WINDOW_P (it->f)
9110 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9111 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9112 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9113 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9115 bool at_eob_p = false;
9117 if ((at_eob_p = !get_next_display_element (it))
9118 || BUFFER_POS_REACHED_P ()
9119 /* If we are past TO_CHARPOS, but never saw any
9120 character positions smaller than TO_CHARPOS,
9121 return MOVE_POS_MATCH_OR_ZV, like the
9122 unidirectional display did. */
9123 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9124 && !saw_smaller_pos
9125 && IT_CHARPOS (*it) > to_charpos))
9127 if (it->bidi_p
9128 && !BUFFER_POS_REACHED_P ()
9129 && !at_eob_p && closest_pos < ZV)
9131 RESTORE_IT (it, &ppos_it, ppos_data);
9132 if (closest_pos != to_charpos)
9133 move_it_in_display_line_to (it, closest_pos, -1,
9134 MOVE_TO_POS);
9136 result = MOVE_POS_MATCH_OR_ZV;
9137 break;
9139 if (ITERATOR_AT_END_OF_LINE_P (it))
9141 result = MOVE_NEWLINE_OR_CR;
9142 break;
9145 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9146 && !saw_smaller_pos
9147 && IT_CHARPOS (*it) > to_charpos)
9149 if (closest_pos < ZV)
9151 RESTORE_IT (it, &ppos_it, ppos_data);
9152 if (closest_pos != to_charpos)
9153 move_it_in_display_line_to (it, closest_pos, -1,
9154 MOVE_TO_POS);
9156 result = MOVE_POS_MATCH_OR_ZV;
9157 break;
9159 result = MOVE_LINE_TRUNCATED;
9160 break;
9162 #undef IT_RESET_X_ASCENT_DESCENT
9165 #undef BUFFER_POS_REACHED_P
9167 /* If we scanned beyond TO_POS, restore the saved iterator either to
9168 the wrap point (if found), or to atpos/atx location. We decide which
9169 data to use to restore the saved iterator state by their X coordinates,
9170 since buffer positions might increase non-monotonically with screen
9171 coordinates due to bidi reordering. */
9172 if (result == MOVE_LINE_CONTINUED
9173 && it->line_wrap == WORD_WRAP
9174 && wrap_it.sp >= 0
9175 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9176 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9177 RESTORE_IT (it, &wrap_it, wrap_data);
9178 else if (atpos_it.sp >= 0)
9179 RESTORE_IT (it, &atpos_it, atpos_data);
9180 else if (atx_it.sp >= 0)
9181 RESTORE_IT (it, &atx_it, atx_data);
9183 done:
9185 if (atpos_data)
9186 bidi_unshelve_cache (atpos_data, true);
9187 if (atx_data)
9188 bidi_unshelve_cache (atx_data, true);
9189 if (wrap_data)
9190 bidi_unshelve_cache (wrap_data, true);
9191 if (ppos_data)
9192 bidi_unshelve_cache (ppos_data, true);
9194 /* Restore the iterator settings altered at the beginning of this
9195 function. */
9196 it->glyph_row = saved_glyph_row;
9197 return result;
9200 /* For external use. */
9201 void
9202 move_it_in_display_line (struct it *it,
9203 ptrdiff_t to_charpos, int to_x,
9204 enum move_operation_enum op)
9206 if (it->line_wrap == WORD_WRAP
9207 && (op & MOVE_TO_X))
9209 struct it save_it;
9210 void *save_data = NULL;
9211 int skip;
9213 SAVE_IT (save_it, *it, save_data);
9214 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9215 /* When word-wrap is on, TO_X may lie past the end
9216 of a wrapped line. Then it->current is the
9217 character on the next line, so backtrack to the
9218 space before the wrap point. */
9219 if (skip == MOVE_LINE_CONTINUED)
9221 int prev_x = max (it->current_x - 1, 0);
9222 RESTORE_IT (it, &save_it, save_data);
9223 move_it_in_display_line_to
9224 (it, -1, prev_x, MOVE_TO_X);
9226 else
9227 bidi_unshelve_cache (save_data, true);
9229 else
9230 move_it_in_display_line_to (it, to_charpos, to_x, op);
9234 /* Move IT forward until it satisfies one or more of the criteria in
9235 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9237 OP is a bit-mask that specifies where to stop, and in particular,
9238 which of those four position arguments makes a difference. See the
9239 description of enum move_operation_enum.
9241 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9242 screen line, this function will set IT to the next position that is
9243 displayed to the right of TO_CHARPOS on the screen.
9245 Return the maximum pixel length of any line scanned but never more
9246 than it.last_visible_x. */
9249 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9251 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9252 int line_height, line_start_x = 0, reached = 0;
9253 int max_current_x = 0;
9254 void *backup_data = NULL;
9256 for (;;)
9258 if (op & MOVE_TO_VPOS)
9260 /* If no TO_CHARPOS and no TO_X specified, stop at the
9261 start of the line TO_VPOS. */
9262 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9264 if (it->vpos == to_vpos)
9266 reached = 1;
9267 break;
9269 else
9270 skip = move_it_in_display_line_to (it, -1, -1, 0);
9272 else
9274 /* TO_VPOS >= 0 means stop at TO_X in the line at
9275 TO_VPOS, or at TO_POS, whichever comes first. */
9276 if (it->vpos == to_vpos)
9278 reached = 2;
9279 break;
9282 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9284 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9286 reached = 3;
9287 break;
9289 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9291 /* We have reached TO_X but not in the line we want. */
9292 skip = move_it_in_display_line_to (it, to_charpos,
9293 -1, MOVE_TO_POS);
9294 if (skip == MOVE_POS_MATCH_OR_ZV)
9296 reached = 4;
9297 break;
9302 else if (op & MOVE_TO_Y)
9304 struct it it_backup;
9306 if (it->line_wrap == WORD_WRAP)
9307 SAVE_IT (it_backup, *it, backup_data);
9309 /* TO_Y specified means stop at TO_X in the line containing
9310 TO_Y---or at TO_CHARPOS if this is reached first. The
9311 problem is that we can't really tell whether the line
9312 contains TO_Y before we have completely scanned it, and
9313 this may skip past TO_X. What we do is to first scan to
9314 TO_X.
9316 If TO_X is not specified, use a TO_X of zero. The reason
9317 is to make the outcome of this function more predictable.
9318 If we didn't use TO_X == 0, we would stop at the end of
9319 the line which is probably not what a caller would expect
9320 to happen. */
9321 skip = move_it_in_display_line_to
9322 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9323 (MOVE_TO_X | (op & MOVE_TO_POS)));
9325 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9326 if (skip == MOVE_POS_MATCH_OR_ZV)
9327 reached = 5;
9328 else if (skip == MOVE_X_REACHED)
9330 /* If TO_X was reached, we want to know whether TO_Y is
9331 in the line. We know this is the case if the already
9332 scanned glyphs make the line tall enough. Otherwise,
9333 we must check by scanning the rest of the line. */
9334 line_height = it->max_ascent + it->max_descent;
9335 if (to_y >= it->current_y
9336 && to_y < it->current_y + line_height)
9338 reached = 6;
9339 break;
9341 SAVE_IT (it_backup, *it, backup_data);
9342 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9343 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9344 op & MOVE_TO_POS);
9345 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9346 line_height = it->max_ascent + it->max_descent;
9347 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9349 if (to_y >= it->current_y
9350 && to_y < it->current_y + line_height)
9352 /* If TO_Y is in this line and TO_X was reached
9353 above, we scanned too far. We have to restore
9354 IT's settings to the ones before skipping. But
9355 keep the more accurate values of max_ascent and
9356 max_descent we've found while skipping the rest
9357 of the line, for the sake of callers, such as
9358 pos_visible_p, that need to know the line
9359 height. */
9360 int max_ascent = it->max_ascent;
9361 int max_descent = it->max_descent;
9363 RESTORE_IT (it, &it_backup, backup_data);
9364 it->max_ascent = max_ascent;
9365 it->max_descent = max_descent;
9366 reached = 6;
9368 else
9370 skip = skip2;
9371 if (skip == MOVE_POS_MATCH_OR_ZV)
9372 reached = 7;
9375 else
9377 /* Check whether TO_Y is in this line. */
9378 line_height = it->max_ascent + it->max_descent;
9379 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9381 if (to_y >= it->current_y
9382 && to_y < it->current_y + line_height)
9384 if (to_y > it->current_y)
9385 max_current_x = max (it->current_x, max_current_x);
9387 /* When word-wrap is on, TO_X may lie past the end
9388 of a wrapped line. Then it->current is the
9389 character on the next line, so backtrack to the
9390 space before the wrap point. */
9391 if (skip == MOVE_LINE_CONTINUED
9392 && it->line_wrap == WORD_WRAP)
9394 int prev_x = max (it->current_x - 1, 0);
9395 RESTORE_IT (it, &it_backup, backup_data);
9396 skip = move_it_in_display_line_to
9397 (it, -1, prev_x, MOVE_TO_X);
9400 reached = 6;
9404 if (reached)
9406 max_current_x = max (it->current_x, max_current_x);
9407 break;
9410 else if (BUFFERP (it->object)
9411 && (it->method == GET_FROM_BUFFER
9412 || it->method == GET_FROM_STRETCH)
9413 && IT_CHARPOS (*it) >= to_charpos
9414 /* Under bidi iteration, a call to set_iterator_to_next
9415 can scan far beyond to_charpos if the initial
9416 portion of the next line needs to be reordered. In
9417 that case, give move_it_in_display_line_to another
9418 chance below. */
9419 && !(it->bidi_p
9420 && it->bidi_it.scan_dir == -1))
9421 skip = MOVE_POS_MATCH_OR_ZV;
9422 else
9423 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9425 switch (skip)
9427 case MOVE_POS_MATCH_OR_ZV:
9428 max_current_x = max (it->current_x, max_current_x);
9429 reached = 8;
9430 goto out;
9432 case MOVE_NEWLINE_OR_CR:
9433 max_current_x = max (it->current_x, max_current_x);
9434 set_iterator_to_next (it, true);
9435 it->continuation_lines_width = 0;
9436 break;
9438 case MOVE_LINE_TRUNCATED:
9439 max_current_x = it->last_visible_x;
9440 it->continuation_lines_width = 0;
9441 reseat_at_next_visible_line_start (it, false);
9442 if ((op & MOVE_TO_POS) != 0
9443 && IT_CHARPOS (*it) > to_charpos)
9445 reached = 9;
9446 goto out;
9448 break;
9450 case MOVE_LINE_CONTINUED:
9451 max_current_x = it->last_visible_x;
9452 /* For continued lines ending in a tab, some of the glyphs
9453 associated with the tab are displayed on the current
9454 line. Since it->current_x does not include these glyphs,
9455 we use it->last_visible_x instead. */
9456 if (it->c == '\t')
9458 it->continuation_lines_width += it->last_visible_x;
9459 /* When moving by vpos, ensure that the iterator really
9460 advances to the next line (bug#847, bug#969). Fixme:
9461 do we need to do this in other circumstances? */
9462 if (it->current_x != it->last_visible_x
9463 && (op & MOVE_TO_VPOS)
9464 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9466 line_start_x = it->current_x + it->pixel_width
9467 - it->last_visible_x;
9468 if (FRAME_WINDOW_P (it->f))
9470 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9471 struct font *face_font = face->font;
9473 /* When display_line produces a continued line
9474 that ends in a TAB, it skips a tab stop that
9475 is closer than the font's space character
9476 width (see x_produce_glyphs where it produces
9477 the stretch glyph which represents a TAB).
9478 We need to reproduce the same logic here. */
9479 eassert (face_font);
9480 if (face_font)
9482 if (line_start_x < face_font->space_width)
9483 line_start_x
9484 += it->tab_width * face_font->space_width;
9487 set_iterator_to_next (it, false);
9490 else
9491 it->continuation_lines_width += it->current_x;
9492 break;
9494 default:
9495 emacs_abort ();
9498 /* Reset/increment for the next run. */
9499 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9500 it->current_x = line_start_x;
9501 line_start_x = 0;
9502 it->hpos = 0;
9503 it->current_y += it->max_ascent + it->max_descent;
9504 ++it->vpos;
9505 last_height = it->max_ascent + it->max_descent;
9506 it->max_ascent = it->max_descent = 0;
9509 out:
9511 /* On text terminals, we may stop at the end of a line in the middle
9512 of a multi-character glyph. If the glyph itself is continued,
9513 i.e. it is actually displayed on the next line, don't treat this
9514 stopping point as valid; move to the next line instead (unless
9515 that brings us offscreen). */
9516 if (!FRAME_WINDOW_P (it->f)
9517 && op & MOVE_TO_POS
9518 && IT_CHARPOS (*it) == to_charpos
9519 && it->what == IT_CHARACTER
9520 && it->nglyphs > 1
9521 && it->line_wrap == WINDOW_WRAP
9522 && it->current_x == it->last_visible_x - 1
9523 && it->c != '\n'
9524 && it->c != '\t'
9525 && it->w->window_end_valid
9526 && it->vpos < it->w->window_end_vpos)
9528 it->continuation_lines_width += it->current_x;
9529 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9530 it->current_y += it->max_ascent + it->max_descent;
9531 ++it->vpos;
9532 last_height = it->max_ascent + it->max_descent;
9535 if (backup_data)
9536 bidi_unshelve_cache (backup_data, true);
9538 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9540 return max_current_x;
9544 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9546 If DY > 0, move IT backward at least that many pixels. DY = 0
9547 means move IT backward to the preceding line start or BEGV. This
9548 function may move over more than DY pixels if IT->current_y - DY
9549 ends up in the middle of a line; in this case IT->current_y will be
9550 set to the top of the line moved to. */
9552 void
9553 move_it_vertically_backward (struct it *it, int dy)
9555 int nlines, h;
9556 struct it it2, it3;
9557 void *it2data = NULL, *it3data = NULL;
9558 ptrdiff_t start_pos;
9559 int nchars_per_row
9560 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9561 ptrdiff_t pos_limit;
9563 move_further_back:
9564 eassert (dy >= 0);
9566 start_pos = IT_CHARPOS (*it);
9568 /* Estimate how many newlines we must move back. */
9569 nlines = max (1, dy / default_line_pixel_height (it->w));
9570 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9571 pos_limit = BEGV;
9572 else
9573 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9575 /* Set the iterator's position that many lines back. But don't go
9576 back more than NLINES full screen lines -- this wins a day with
9577 buffers which have very long lines. */
9578 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9579 back_to_previous_visible_line_start (it);
9581 /* Reseat the iterator here. When moving backward, we don't want
9582 reseat to skip forward over invisible text, set up the iterator
9583 to deliver from overlay strings at the new position etc. So,
9584 use reseat_1 here. */
9585 reseat_1 (it, it->current.pos, true);
9587 /* We are now surely at a line start. */
9588 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9589 reordering is in effect. */
9590 it->continuation_lines_width = 0;
9592 /* Move forward and see what y-distance we moved. First move to the
9593 start of the next line so that we get its height. We need this
9594 height to be able to tell whether we reached the specified
9595 y-distance. */
9596 SAVE_IT (it2, *it, it2data);
9597 it2.max_ascent = it2.max_descent = 0;
9600 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9601 MOVE_TO_POS | MOVE_TO_VPOS);
9603 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9604 /* If we are in a display string which starts at START_POS,
9605 and that display string includes a newline, and we are
9606 right after that newline (i.e. at the beginning of a
9607 display line), exit the loop, because otherwise we will
9608 infloop, since move_it_to will see that it is already at
9609 START_POS and will not move. */
9610 || (it2.method == GET_FROM_STRING
9611 && IT_CHARPOS (it2) == start_pos
9612 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9613 eassert (IT_CHARPOS (*it) >= BEGV);
9614 SAVE_IT (it3, it2, it3data);
9616 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9617 eassert (IT_CHARPOS (*it) >= BEGV);
9618 /* H is the actual vertical distance from the position in *IT
9619 and the starting position. */
9620 h = it2.current_y - it->current_y;
9621 /* NLINES is the distance in number of lines. */
9622 nlines = it2.vpos - it->vpos;
9624 /* Correct IT's y and vpos position
9625 so that they are relative to the starting point. */
9626 it->vpos -= nlines;
9627 it->current_y -= h;
9629 if (dy == 0)
9631 /* DY == 0 means move to the start of the screen line. The
9632 value of nlines is > 0 if continuation lines were involved,
9633 or if the original IT position was at start of a line. */
9634 RESTORE_IT (it, it, it2data);
9635 if (nlines > 0)
9636 move_it_by_lines (it, nlines);
9637 /* The above code moves us to some position NLINES down,
9638 usually to its first glyph (leftmost in an L2R line), but
9639 that's not necessarily the start of the line, under bidi
9640 reordering. We want to get to the character position
9641 that is immediately after the newline of the previous
9642 line. */
9643 if (it->bidi_p
9644 && !it->continuation_lines_width
9645 && !STRINGP (it->string)
9646 && IT_CHARPOS (*it) > BEGV
9647 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9649 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9651 DEC_BOTH (cp, bp);
9652 cp = find_newline_no_quit (cp, bp, -1, NULL);
9653 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9655 bidi_unshelve_cache (it3data, true);
9657 else
9659 /* The y-position we try to reach, relative to *IT.
9660 Note that H has been subtracted in front of the if-statement. */
9661 int target_y = it->current_y + h - dy;
9662 int y0 = it3.current_y;
9663 int y1;
9664 int line_height;
9666 RESTORE_IT (&it3, &it3, it3data);
9667 y1 = line_bottom_y (&it3);
9668 line_height = y1 - y0;
9669 RESTORE_IT (it, it, it2data);
9670 /* If we did not reach target_y, try to move further backward if
9671 we can. If we moved too far backward, try to move forward. */
9672 if (target_y < it->current_y
9673 /* This is heuristic. In a window that's 3 lines high, with
9674 a line height of 13 pixels each, recentering with point
9675 on the bottom line will try to move -39/2 = 19 pixels
9676 backward. Try to avoid moving into the first line. */
9677 && (it->current_y - target_y
9678 > min (window_box_height (it->w), line_height * 2 / 3))
9679 && IT_CHARPOS (*it) > BEGV)
9681 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9682 target_y - it->current_y));
9683 dy = it->current_y - target_y;
9684 goto move_further_back;
9686 else if (target_y >= it->current_y + line_height
9687 && IT_CHARPOS (*it) < ZV)
9689 /* Should move forward by at least one line, maybe more.
9691 Note: Calling move_it_by_lines can be expensive on
9692 terminal frames, where compute_motion is used (via
9693 vmotion) to do the job, when there are very long lines
9694 and truncate-lines is nil. That's the reason for
9695 treating terminal frames specially here. */
9697 if (!FRAME_WINDOW_P (it->f))
9698 move_it_vertically (it, target_y - it->current_y);
9699 else
9703 move_it_by_lines (it, 1);
9705 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9712 /* Move IT by a specified amount of pixel lines DY. DY negative means
9713 move backwards. DY = 0 means move to start of screen line. At the
9714 end, IT will be on the start of a screen line. */
9716 void
9717 move_it_vertically (struct it *it, int dy)
9719 if (dy <= 0)
9720 move_it_vertically_backward (it, -dy);
9721 else
9723 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9724 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9725 MOVE_TO_POS | MOVE_TO_Y);
9726 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9728 /* If buffer ends in ZV without a newline, move to the start of
9729 the line to satisfy the post-condition. */
9730 if (IT_CHARPOS (*it) == ZV
9731 && ZV > BEGV
9732 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9733 move_it_by_lines (it, 0);
9738 /* Move iterator IT past the end of the text line it is in. */
9740 void
9741 move_it_past_eol (struct it *it)
9743 enum move_it_result rc;
9745 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9746 if (rc == MOVE_NEWLINE_OR_CR)
9747 set_iterator_to_next (it, false);
9751 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9752 negative means move up. DVPOS == 0 means move to the start of the
9753 screen line.
9755 Optimization idea: If we would know that IT->f doesn't use
9756 a face with proportional font, we could be faster for
9757 truncate-lines nil. */
9759 void
9760 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9763 /* The commented-out optimization uses vmotion on terminals. This
9764 gives bad results, because elements like it->what, on which
9765 callers such as pos_visible_p rely, aren't updated. */
9766 /* struct position pos;
9767 if (!FRAME_WINDOW_P (it->f))
9769 struct text_pos textpos;
9771 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9772 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9773 reseat (it, textpos, true);
9774 it->vpos += pos.vpos;
9775 it->current_y += pos.vpos;
9777 else */
9779 if (dvpos == 0)
9781 /* DVPOS == 0 means move to the start of the screen line. */
9782 move_it_vertically_backward (it, 0);
9783 /* Let next call to line_bottom_y calculate real line height. */
9784 last_height = 0;
9786 else if (dvpos > 0)
9788 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9789 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9791 /* Only move to the next buffer position if we ended up in a
9792 string from display property, not in an overlay string
9793 (before-string or after-string). That is because the
9794 latter don't conceal the underlying buffer position, so
9795 we can ask to move the iterator to the exact position we
9796 are interested in. Note that, even if we are already at
9797 IT_CHARPOS (*it), the call below is not a no-op, as it
9798 will detect that we are at the end of the string, pop the
9799 iterator, and compute it->current_x and it->hpos
9800 correctly. */
9801 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9802 -1, -1, -1, MOVE_TO_POS);
9805 else
9807 struct it it2;
9808 void *it2data = NULL;
9809 ptrdiff_t start_charpos, i;
9810 int nchars_per_row
9811 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9812 bool hit_pos_limit = false;
9813 ptrdiff_t pos_limit;
9815 /* Start at the beginning of the screen line containing IT's
9816 position. This may actually move vertically backwards,
9817 in case of overlays, so adjust dvpos accordingly. */
9818 dvpos += it->vpos;
9819 move_it_vertically_backward (it, 0);
9820 dvpos -= it->vpos;
9822 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9823 screen lines, and reseat the iterator there. */
9824 start_charpos = IT_CHARPOS (*it);
9825 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9826 pos_limit = BEGV;
9827 else
9828 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9830 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9831 back_to_previous_visible_line_start (it);
9832 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9833 hit_pos_limit = true;
9834 reseat (it, it->current.pos, true);
9836 /* Move further back if we end up in a string or an image. */
9837 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9839 /* First try to move to start of display line. */
9840 dvpos += it->vpos;
9841 move_it_vertically_backward (it, 0);
9842 dvpos -= it->vpos;
9843 if (IT_POS_VALID_AFTER_MOVE_P (it))
9844 break;
9845 /* If start of line is still in string or image,
9846 move further back. */
9847 back_to_previous_visible_line_start (it);
9848 reseat (it, it->current.pos, true);
9849 dvpos--;
9852 it->current_x = it->hpos = 0;
9854 /* Above call may have moved too far if continuation lines
9855 are involved. Scan forward and see if it did. */
9856 SAVE_IT (it2, *it, it2data);
9857 it2.vpos = it2.current_y = 0;
9858 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9859 it->vpos -= it2.vpos;
9860 it->current_y -= it2.current_y;
9861 it->current_x = it->hpos = 0;
9863 /* If we moved too far back, move IT some lines forward. */
9864 if (it2.vpos > -dvpos)
9866 int delta = it2.vpos + dvpos;
9868 RESTORE_IT (&it2, &it2, it2data);
9869 SAVE_IT (it2, *it, it2data);
9870 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9871 /* Move back again if we got too far ahead. */
9872 if (IT_CHARPOS (*it) >= start_charpos)
9873 RESTORE_IT (it, &it2, it2data);
9874 else
9875 bidi_unshelve_cache (it2data, true);
9877 else if (hit_pos_limit && pos_limit > BEGV
9878 && dvpos < 0 && it2.vpos < -dvpos)
9880 /* If we hit the limit, but still didn't make it far enough
9881 back, that means there's a display string with a newline
9882 covering a large chunk of text, and that caused
9883 back_to_previous_visible_line_start try to go too far.
9884 Punish those who commit such atrocities by going back
9885 until we've reached DVPOS, after lifting the limit, which
9886 could make it slow for very long lines. "If it hurts,
9887 don't do that!" */
9888 dvpos += it2.vpos;
9889 RESTORE_IT (it, it, it2data);
9890 for (i = -dvpos; i > 0; --i)
9892 back_to_previous_visible_line_start (it);
9893 it->vpos--;
9895 reseat_1 (it, it->current.pos, true);
9897 else
9898 RESTORE_IT (it, it, it2data);
9903 partial_line_height (struct it *it_origin)
9905 int partial_height;
9906 void *it_data = NULL;
9907 struct it it;
9908 SAVE_IT (it, *it_origin, it_data);
9909 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9910 MOVE_TO_POS | MOVE_TO_Y);
9911 if (it.what == IT_EOB)
9913 int vis_height = it.last_visible_y - it.current_y;
9914 int height = it.ascent + it.descent;
9915 partial_height = (vis_height < height) ? vis_height : 0;
9917 else
9919 int last_line_y = it.current_y;
9920 move_it_by_lines (&it, 1);
9921 partial_height = (it.current_y > it.last_visible_y)
9922 ? it.last_visible_y - last_line_y : 0;
9924 RESTORE_IT (&it, &it, it_data);
9925 return partial_height;
9928 /* Return true if IT points into the middle of a display vector. */
9930 bool
9931 in_display_vector_p (struct it *it)
9933 return (it->method == GET_FROM_DISPLAY_VECTOR
9934 && it->current.dpvec_index > 0
9935 && it->dpvec + it->current.dpvec_index != it->dpend);
9938 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9939 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9940 WINDOW must be a live window and defaults to the selected one. The
9941 return value is a cons of the maximum pixel-width of any text line and
9942 the maximum pixel-height of all text lines.
9944 The optional argument FROM, if non-nil, specifies the first text
9945 position and defaults to the minimum accessible position of the buffer.
9946 If FROM is t, use the minimum accessible position that starts a
9947 non-empty line. TO, if non-nil, specifies the last text position and
9948 defaults to the maximum accessible position of the buffer. If TO is t,
9949 use the maximum accessible position that ends a non-empty line.
9951 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9952 width that can be returned. X-LIMIT nil or omitted, means to use the
9953 pixel-width of WINDOW's body; use this if you want to know how high
9954 WINDOW should be become in order to fit all of its buffer's text with
9955 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9956 if you intend to change WINDOW's width. In any case, text whose
9957 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9958 of long lines can take some time, it's always a good idea to make this
9959 argument as small as possible; in particular, if the buffer contains
9960 long lines that shall be truncated anyway.
9962 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9963 height (excluding the height of the mode- or header-line, if any) that
9964 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9965 ignored. Since calculating the text height of a large buffer can take
9966 some time, it makes sense to specify this argument if the size of the
9967 buffer is large or unknown.
9969 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9970 include the height of the mode- or header-line of WINDOW in the return
9971 value. If it is either the symbol `mode-line' or `header-line', include
9972 only the height of that line, if present, in the return value. If t,
9973 include the height of both, if present, in the return value. */)
9974 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9975 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9977 struct window *w = decode_live_window (window);
9978 Lisp_Object buffer = w->contents;
9979 struct buffer *b;
9980 struct it it;
9981 struct buffer *old_b = NULL;
9982 ptrdiff_t start, end, pos;
9983 struct text_pos startp;
9984 void *itdata = NULL;
9985 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9987 CHECK_BUFFER (buffer);
9988 b = XBUFFER (buffer);
9990 if (b != current_buffer)
9992 old_b = current_buffer;
9993 set_buffer_internal (b);
9996 if (NILP (from))
9997 start = BEGV;
9998 else if (EQ (from, Qt))
10000 start = pos = BEGV;
10001 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10002 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10003 start = pos;
10004 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10005 start = pos;
10007 else
10009 CHECK_NUMBER_COERCE_MARKER (from);
10010 start = min (max (XINT (from), BEGV), ZV);
10013 if (NILP (to))
10014 end = ZV;
10015 else if (EQ (to, Qt))
10017 end = pos = ZV;
10018 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10019 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10020 end = pos;
10021 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10022 end = pos;
10024 else
10026 CHECK_NUMBER_COERCE_MARKER (to);
10027 end = max (start, min (XINT (to), ZV));
10030 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10031 max_x = XINT (x_limit);
10033 if (NILP (y_limit))
10034 max_y = INT_MAX;
10035 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10036 max_y = XINT (y_limit);
10038 itdata = bidi_shelve_cache ();
10039 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10040 start_display (&it, w, startp);
10042 if (NILP (x_limit))
10043 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10044 else
10046 it.last_visible_x = max_x;
10047 /* Actually, we never want move_it_to stop at to_x. But to make
10048 sure that move_it_in_display_line_to always moves far enough,
10049 we set it to INT_MAX and specify MOVE_TO_X. */
10050 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10051 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10052 /* Don't return more than X-LIMIT. */
10053 if (x > max_x)
10054 x = max_x;
10057 /* Subtract height of header-line which was counted automatically by
10058 start_display. */
10059 y = it.current_y + it.max_ascent + it.max_descent
10060 - WINDOW_HEADER_LINE_HEIGHT (w);
10061 /* Don't return more than Y-LIMIT. */
10062 if (y > max_y)
10063 y = max_y;
10065 if (EQ (mode_and_header_line, Qheader_line)
10066 || EQ (mode_and_header_line, Qt))
10067 /* Re-add height of header-line as requested. */
10068 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10070 if (EQ (mode_and_header_line, Qmode_line)
10071 || EQ (mode_and_header_line, Qt))
10072 /* Add height of mode-line as requested. */
10073 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10075 bidi_unshelve_cache (itdata, false);
10077 if (old_b)
10078 set_buffer_internal (old_b);
10080 return Fcons (make_number (x), make_number (y));
10083 /***********************************************************************
10084 Messages
10085 ***********************************************************************/
10087 /* Return the number of arguments the format string FORMAT needs. */
10089 static ptrdiff_t
10090 format_nargs (char const *format)
10092 ptrdiff_t nargs = 0;
10093 for (char const *p = format; (p = strchr (p, '%')); p++)
10094 if (p[1] == '%')
10095 p++;
10096 else
10097 nargs++;
10098 return nargs;
10101 /* Add a message with format string FORMAT and formatted arguments
10102 to *Messages*. */
10104 void
10105 add_to_log (const char *format, ...)
10107 va_list ap;
10108 va_start (ap, format);
10109 vadd_to_log (format, ap);
10110 va_end (ap);
10113 void
10114 vadd_to_log (char const *format, va_list ap)
10116 ptrdiff_t form_nargs = format_nargs (format);
10117 ptrdiff_t nargs = 1 + form_nargs;
10118 Lisp_Object args[10];
10119 eassert (nargs <= ARRAYELTS (args));
10120 AUTO_STRING (args0, format);
10121 args[0] = args0;
10122 for (ptrdiff_t i = 1; i <= nargs; i++)
10123 args[i] = va_arg (ap, Lisp_Object);
10124 Lisp_Object msg = Qnil;
10125 msg = Fformat_message (nargs, args);
10127 ptrdiff_t len = SBYTES (msg) + 1;
10128 USE_SAFE_ALLOCA;
10129 char *buffer = SAFE_ALLOCA (len);
10130 memcpy (buffer, SDATA (msg), len);
10132 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10133 SAFE_FREE ();
10137 /* Output a newline in the *Messages* buffer if "needs" one. */
10139 void
10140 message_log_maybe_newline (void)
10142 if (message_log_need_newline)
10143 message_dolog ("", 0, true, false);
10147 /* Add a string M of length NBYTES to the message log, optionally
10148 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10149 true, means interpret the contents of M as multibyte. This
10150 function calls low-level routines in order to bypass text property
10151 hooks, etc. which might not be safe to run.
10153 This may GC (insert may run before/after change hooks),
10154 so the buffer M must NOT point to a Lisp string. */
10156 void
10157 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10159 const unsigned char *msg = (const unsigned char *) m;
10161 if (!NILP (Vmemory_full))
10162 return;
10164 if (!NILP (Vmessage_log_max))
10166 struct buffer *oldbuf;
10167 Lisp_Object oldpoint, oldbegv, oldzv;
10168 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10169 ptrdiff_t point_at_end = 0;
10170 ptrdiff_t zv_at_end = 0;
10171 Lisp_Object old_deactivate_mark;
10173 old_deactivate_mark = Vdeactivate_mark;
10174 oldbuf = current_buffer;
10176 /* Ensure the Messages buffer exists, and switch to it.
10177 If we created it, set the major-mode. */
10178 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10179 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10180 if (newbuffer
10181 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10182 call0 (intern ("messages-buffer-mode"));
10184 bset_undo_list (current_buffer, Qt);
10185 bset_cache_long_scans (current_buffer, Qnil);
10187 oldpoint = message_dolog_marker1;
10188 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10189 oldbegv = message_dolog_marker2;
10190 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10191 oldzv = message_dolog_marker3;
10192 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10194 if (PT == Z)
10195 point_at_end = 1;
10196 if (ZV == Z)
10197 zv_at_end = 1;
10199 BEGV = BEG;
10200 BEGV_BYTE = BEG_BYTE;
10201 ZV = Z;
10202 ZV_BYTE = Z_BYTE;
10203 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10205 /* Insert the string--maybe converting multibyte to single byte
10206 or vice versa, so that all the text fits the buffer. */
10207 if (multibyte
10208 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10210 ptrdiff_t i;
10211 int c, char_bytes;
10212 char work[1];
10214 /* Convert a multibyte string to single-byte
10215 for the *Message* buffer. */
10216 for (i = 0; i < nbytes; i += char_bytes)
10218 c = string_char_and_length (msg + i, &char_bytes);
10219 work[0] = CHAR_TO_BYTE8 (c);
10220 insert_1_both (work, 1, 1, true, false, false);
10223 else if (! multibyte
10224 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10226 ptrdiff_t i;
10227 int c, char_bytes;
10228 unsigned char str[MAX_MULTIBYTE_LENGTH];
10229 /* Convert a single-byte string to multibyte
10230 for the *Message* buffer. */
10231 for (i = 0; i < nbytes; i++)
10233 c = msg[i];
10234 MAKE_CHAR_MULTIBYTE (c);
10235 char_bytes = CHAR_STRING (c, str);
10236 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10239 else if (nbytes)
10240 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10241 true, false, false);
10243 if (nlflag)
10245 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10246 printmax_t dups;
10248 insert_1_both ("\n", 1, 1, true, false, false);
10250 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10251 this_bol = PT;
10252 this_bol_byte = PT_BYTE;
10254 /* See if this line duplicates the previous one.
10255 If so, combine duplicates. */
10256 if (this_bol > BEG)
10258 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10259 prev_bol = PT;
10260 prev_bol_byte = PT_BYTE;
10262 dups = message_log_check_duplicate (prev_bol_byte,
10263 this_bol_byte);
10264 if (dups)
10266 del_range_both (prev_bol, prev_bol_byte,
10267 this_bol, this_bol_byte, false);
10268 if (dups > 1)
10270 char dupstr[sizeof " [ times]"
10271 + INT_STRLEN_BOUND (printmax_t)];
10273 /* If you change this format, don't forget to also
10274 change message_log_check_duplicate. */
10275 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10276 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10277 insert_1_both (dupstr, duplen, duplen,
10278 true, false, true);
10283 /* If we have more than the desired maximum number of lines
10284 in the *Messages* buffer now, delete the oldest ones.
10285 This is safe because we don't have undo in this buffer. */
10287 if (NATNUMP (Vmessage_log_max))
10289 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10290 -XFASTINT (Vmessage_log_max) - 1, false);
10291 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10294 BEGV = marker_position (oldbegv);
10295 BEGV_BYTE = marker_byte_position (oldbegv);
10297 if (zv_at_end)
10299 ZV = Z;
10300 ZV_BYTE = Z_BYTE;
10302 else
10304 ZV = marker_position (oldzv);
10305 ZV_BYTE = marker_byte_position (oldzv);
10308 if (point_at_end)
10309 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10310 else
10311 /* We can't do Fgoto_char (oldpoint) because it will run some
10312 Lisp code. */
10313 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10314 marker_byte_position (oldpoint));
10316 unchain_marker (XMARKER (oldpoint));
10317 unchain_marker (XMARKER (oldbegv));
10318 unchain_marker (XMARKER (oldzv));
10320 /* We called insert_1_both above with its 5th argument (PREPARE)
10321 false, which prevents insert_1_both from calling
10322 prepare_to_modify_buffer, which in turns prevents us from
10323 incrementing windows_or_buffers_changed even if *Messages* is
10324 shown in some window. So we must manually set
10325 windows_or_buffers_changed here to make up for that. */
10326 windows_or_buffers_changed = old_windows_or_buffers_changed;
10327 bset_redisplay (current_buffer);
10329 set_buffer_internal (oldbuf);
10331 message_log_need_newline = !nlflag;
10332 Vdeactivate_mark = old_deactivate_mark;
10337 /* We are at the end of the buffer after just having inserted a newline.
10338 (Note: We depend on the fact we won't be crossing the gap.)
10339 Check to see if the most recent message looks a lot like the previous one.
10340 Return 0 if different, 1 if the new one should just replace it, or a
10341 value N > 1 if we should also append " [N times]". */
10343 static intmax_t
10344 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10346 ptrdiff_t i;
10347 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10348 bool seen_dots = false;
10349 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10350 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10352 for (i = 0; i < len; i++)
10354 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10355 seen_dots = true;
10356 if (p1[i] != p2[i])
10357 return seen_dots;
10359 p1 += len;
10360 if (*p1 == '\n')
10361 return 2;
10362 if (*p1++ == ' ' && *p1++ == '[')
10364 char *pend;
10365 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10366 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10367 return n + 1;
10369 return 0;
10373 /* Display an echo area message M with a specified length of NBYTES
10374 bytes. The string may include null characters. If M is not a
10375 string, clear out any existing message, and let the mini-buffer
10376 text show through.
10378 This function cancels echoing. */
10380 void
10381 message3 (Lisp_Object m)
10383 clear_message (true, true);
10384 cancel_echoing ();
10386 /* First flush out any partial line written with print. */
10387 message_log_maybe_newline ();
10388 if (STRINGP (m))
10390 ptrdiff_t nbytes = SBYTES (m);
10391 bool multibyte = STRING_MULTIBYTE (m);
10392 char *buffer;
10393 USE_SAFE_ALLOCA;
10394 SAFE_ALLOCA_STRING (buffer, m);
10395 message_dolog (buffer, nbytes, true, multibyte);
10396 SAFE_FREE ();
10398 if (! inhibit_message)
10399 message3_nolog (m);
10402 /* Log the message M to stderr. Log an empty line if M is not a string. */
10404 static void
10405 message_to_stderr (Lisp_Object m)
10407 if (noninteractive_need_newline)
10409 noninteractive_need_newline = false;
10410 fputc ('\n', stderr);
10412 if (STRINGP (m))
10414 Lisp_Object coding_system = Vlocale_coding_system;
10415 Lisp_Object s;
10417 if (!NILP (Vcoding_system_for_write))
10418 coding_system = Vcoding_system_for_write;
10419 if (!NILP (coding_system))
10420 s = code_convert_string_norecord (m, coding_system, true);
10421 else
10422 s = m;
10424 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10426 if (!cursor_in_echo_area)
10427 fputc ('\n', stderr);
10428 fflush (stderr);
10431 /* The non-logging version of message3.
10432 This does not cancel echoing, because it is used for echoing.
10433 Perhaps we need to make a separate function for echoing
10434 and make this cancel echoing. */
10436 void
10437 message3_nolog (Lisp_Object m)
10439 struct frame *sf = SELECTED_FRAME ();
10441 if (FRAME_INITIAL_P (sf))
10442 message_to_stderr (m);
10443 /* Error messages get reported properly by cmd_error, so this must be just an
10444 informative message; if the frame hasn't really been initialized yet, just
10445 toss it. */
10446 else if (INTERACTIVE && sf->glyphs_initialized_p)
10448 /* Get the frame containing the mini-buffer
10449 that the selected frame is using. */
10450 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10451 Lisp_Object frame = XWINDOW (mini_window)->frame;
10452 struct frame *f = XFRAME (frame);
10454 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10455 Fmake_frame_visible (frame);
10457 if (STRINGP (m) && SCHARS (m) > 0)
10459 set_message (m);
10460 if (minibuffer_auto_raise)
10461 Fraise_frame (frame);
10462 /* Assume we are not echoing.
10463 (If we are, echo_now will override this.) */
10464 echo_message_buffer = Qnil;
10466 else
10467 clear_message (true, true);
10469 do_pending_window_change (false);
10470 echo_area_display (true);
10471 do_pending_window_change (false);
10472 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10473 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10478 /* Display a null-terminated echo area message M. If M is 0, clear
10479 out any existing message, and let the mini-buffer text show through.
10481 The buffer M must continue to exist until after the echo area gets
10482 cleared or some other message gets displayed there. Do not pass
10483 text that is stored in a Lisp string. Do not pass text in a buffer
10484 that was alloca'd. */
10486 void
10487 message1 (const char *m)
10489 message3 (m ? build_unibyte_string (m) : Qnil);
10493 /* The non-logging counterpart of message1. */
10495 void
10496 message1_nolog (const char *m)
10498 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10501 /* Display a message M which contains a single %s
10502 which gets replaced with STRING. */
10504 void
10505 message_with_string (const char *m, Lisp_Object string, bool log)
10507 CHECK_STRING (string);
10509 bool need_message;
10510 if (noninteractive)
10511 need_message = !!m;
10512 else if (!INTERACTIVE)
10513 need_message = false;
10514 else
10516 /* The frame whose minibuffer we're going to display the message on.
10517 It may be larger than the selected frame, so we need
10518 to use its buffer, not the selected frame's buffer. */
10519 Lisp_Object mini_window;
10520 struct frame *f, *sf = SELECTED_FRAME ();
10522 /* Get the frame containing the minibuffer
10523 that the selected frame is using. */
10524 mini_window = FRAME_MINIBUF_WINDOW (sf);
10525 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10527 /* Error messages get reported properly by cmd_error, so this must be
10528 just an informative message; if the frame hasn't really been
10529 initialized yet, just toss it. */
10530 need_message = f->glyphs_initialized_p;
10533 if (need_message)
10535 AUTO_STRING (fmt, m);
10536 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10538 if (noninteractive)
10539 message_to_stderr (msg);
10540 else
10542 if (log)
10543 message3 (msg);
10544 else
10545 message3_nolog (msg);
10547 /* Print should start at the beginning of the message
10548 buffer next time. */
10549 message_buf_print = false;
10555 /* Dump an informative message to the minibuf. If M is 0, clear out
10556 any existing message, and let the mini-buffer text show through.
10558 The message must be safe ASCII (because when Emacs is
10559 non-interactive the message is sent straight to stderr without
10560 encoding first) and the format must not contain ` or ' (because
10561 this function does not account for `text-quoting-style'). If your
10562 message and format do not fit into this category, convert your
10563 arguments to Lisp objects and use Fmessage instead. */
10565 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10566 vmessage (const char *m, va_list ap)
10568 if (noninteractive)
10570 if (m)
10572 if (noninteractive_need_newline)
10573 putc ('\n', stderr);
10574 noninteractive_need_newline = false;
10575 vfprintf (stderr, m, ap);
10576 if (!cursor_in_echo_area)
10577 fprintf (stderr, "\n");
10578 fflush (stderr);
10581 else if (INTERACTIVE)
10583 /* The frame whose mini-buffer we're going to display the message
10584 on. It may be larger than the selected frame, so we need to
10585 use its buffer, not the selected frame's buffer. */
10586 Lisp_Object mini_window;
10587 struct frame *f, *sf = SELECTED_FRAME ();
10589 /* Get the frame containing the mini-buffer
10590 that the selected frame is using. */
10591 mini_window = FRAME_MINIBUF_WINDOW (sf);
10592 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10594 /* Error messages get reported properly by cmd_error, so this must be
10595 just an informative message; if the frame hasn't really been
10596 initialized yet, just toss it. */
10597 if (f->glyphs_initialized_p)
10599 if (m)
10601 ptrdiff_t len;
10602 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10603 USE_SAFE_ALLOCA;
10604 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10606 len = doprnt (message_buf, maxsize, m, 0, ap);
10608 message3 (make_string (message_buf, len));
10609 SAFE_FREE ();
10611 else
10612 message1 (0);
10614 /* Print should start at the beginning of the message
10615 buffer next time. */
10616 message_buf_print = false;
10621 /* See vmessage for restrictions on the text of the message. */
10622 void
10623 message (const char *m, ...)
10625 va_list ap;
10626 va_start (ap, m);
10627 vmessage (m, ap);
10628 va_end (ap);
10632 /* Display the current message in the current mini-buffer. This is
10633 only called from error handlers in process.c, and is not time
10634 critical. */
10636 void
10637 update_echo_area (void)
10639 if (!NILP (echo_area_buffer[0]))
10641 Lisp_Object string;
10642 string = Fcurrent_message ();
10643 message3 (string);
10648 /* Make sure echo area buffers in `echo_buffers' are live.
10649 If they aren't, make new ones. */
10651 static void
10652 ensure_echo_area_buffers (void)
10654 for (int i = 0; i < 2; i++)
10655 if (!BUFFERP (echo_buffer[i])
10656 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10658 Lisp_Object old_buffer = echo_buffer[i];
10659 static char const name_fmt[] = " *Echo Area %d*";
10660 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10661 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10662 echo_buffer[i] = Fget_buffer_create (lname);
10663 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10664 /* to force word wrap in echo area -
10665 it was decided to postpone this*/
10666 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10668 for (int j = 0; j < 2; j++)
10669 if (EQ (old_buffer, echo_area_buffer[j]))
10670 echo_area_buffer[j] = echo_buffer[i];
10675 /* Call FN with args A1..A2 with either the current or last displayed
10676 echo_area_buffer as current buffer.
10678 WHICH zero means use the current message buffer
10679 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10680 from echo_buffer[] and clear it.
10682 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10683 suitable buffer from echo_buffer[] and clear it.
10685 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10686 that the current message becomes the last displayed one, choose a
10687 suitable buffer for echo_area_buffer[0], and clear it.
10689 Value is what FN returns. */
10691 static bool
10692 with_echo_area_buffer (struct window *w, int which,
10693 bool (*fn) (ptrdiff_t, Lisp_Object),
10694 ptrdiff_t a1, Lisp_Object a2)
10696 Lisp_Object buffer;
10697 bool this_one, the_other, clear_buffer_p, rc;
10698 ptrdiff_t count = SPECPDL_INDEX ();
10700 /* If buffers aren't live, make new ones. */
10701 ensure_echo_area_buffers ();
10703 clear_buffer_p = false;
10705 if (which == 0)
10706 this_one = false, the_other = true;
10707 else if (which > 0)
10708 this_one = true, the_other = false;
10709 else
10711 this_one = false, the_other = true;
10712 clear_buffer_p = true;
10714 /* We need a fresh one in case the current echo buffer equals
10715 the one containing the last displayed echo area message. */
10716 if (!NILP (echo_area_buffer[this_one])
10717 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10718 echo_area_buffer[this_one] = Qnil;
10721 /* Choose a suitable buffer from echo_buffer[] if we don't
10722 have one. */
10723 if (NILP (echo_area_buffer[this_one]))
10725 echo_area_buffer[this_one]
10726 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10727 ? echo_buffer[the_other]
10728 : echo_buffer[this_one]);
10729 clear_buffer_p = true;
10732 buffer = echo_area_buffer[this_one];
10734 /* Don't get confused by reusing the buffer used for echoing
10735 for a different purpose. */
10736 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10737 cancel_echoing ();
10739 record_unwind_protect (unwind_with_echo_area_buffer,
10740 with_echo_area_buffer_unwind_data (w));
10742 /* Make the echo area buffer current. Note that for display
10743 purposes, it is not necessary that the displayed window's buffer
10744 == current_buffer, except for text property lookup. So, let's
10745 only set that buffer temporarily here without doing a full
10746 Fset_window_buffer. We must also change w->pointm, though,
10747 because otherwise an assertions in unshow_buffer fails, and Emacs
10748 aborts. */
10749 set_buffer_internal_1 (XBUFFER (buffer));
10750 if (w)
10752 wset_buffer (w, buffer);
10753 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10754 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10757 bset_undo_list (current_buffer, Qt);
10758 bset_read_only (current_buffer, Qnil);
10759 specbind (Qinhibit_read_only, Qt);
10760 specbind (Qinhibit_modification_hooks, Qt);
10762 if (clear_buffer_p && Z > BEG)
10763 del_range (BEG, Z);
10765 eassert (BEGV >= BEG);
10766 eassert (ZV <= Z && ZV >= BEGV);
10768 rc = fn (a1, a2);
10770 eassert (BEGV >= BEG);
10771 eassert (ZV <= Z && ZV >= BEGV);
10773 unbind_to (count, Qnil);
10774 return rc;
10778 /* Save state that should be preserved around the call to the function
10779 FN called in with_echo_area_buffer. */
10781 static Lisp_Object
10782 with_echo_area_buffer_unwind_data (struct window *w)
10784 int i = 0;
10785 Lisp_Object vector, tmp;
10787 /* Reduce consing by keeping one vector in
10788 Vwith_echo_area_save_vector. */
10789 vector = Vwith_echo_area_save_vector;
10790 Vwith_echo_area_save_vector = Qnil;
10792 if (NILP (vector))
10793 vector = Fmake_vector (make_number (11), Qnil);
10795 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10796 ASET (vector, i, Vdeactivate_mark); ++i;
10797 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10799 if (w)
10801 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10802 ASET (vector, i, w->contents); ++i;
10803 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10804 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10805 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10806 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10807 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10808 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10810 else
10812 int end = i + 8;
10813 for (; i < end; ++i)
10814 ASET (vector, i, Qnil);
10817 eassert (i == ASIZE (vector));
10818 return vector;
10822 /* Restore global state from VECTOR which was created by
10823 with_echo_area_buffer_unwind_data. */
10825 static void
10826 unwind_with_echo_area_buffer (Lisp_Object vector)
10828 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10829 Vdeactivate_mark = AREF (vector, 1);
10830 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10832 if (WINDOWP (AREF (vector, 3)))
10834 struct window *w;
10835 Lisp_Object buffer;
10837 w = XWINDOW (AREF (vector, 3));
10838 buffer = AREF (vector, 4);
10840 wset_buffer (w, buffer);
10841 set_marker_both (w->pointm, buffer,
10842 XFASTINT (AREF (vector, 5)),
10843 XFASTINT (AREF (vector, 6)));
10844 set_marker_both (w->old_pointm, buffer,
10845 XFASTINT (AREF (vector, 7)),
10846 XFASTINT (AREF (vector, 8)));
10847 set_marker_both (w->start, buffer,
10848 XFASTINT (AREF (vector, 9)),
10849 XFASTINT (AREF (vector, 10)));
10852 Vwith_echo_area_save_vector = vector;
10856 /* Set up the echo area for use by print functions. MULTIBYTE_P
10857 means we will print multibyte. */
10859 void
10860 setup_echo_area_for_printing (bool multibyte_p)
10862 /* If we can't find an echo area any more, exit. */
10863 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10864 Fkill_emacs (Qnil);
10866 ensure_echo_area_buffers ();
10868 if (!message_buf_print)
10870 /* A message has been output since the last time we printed.
10871 Choose a fresh echo area buffer. */
10872 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10873 echo_area_buffer[0] = echo_buffer[1];
10874 else
10875 echo_area_buffer[0] = echo_buffer[0];
10877 /* Switch to that buffer and clear it. */
10878 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10879 bset_truncate_lines (current_buffer, Qnil);
10881 if (Z > BEG)
10883 ptrdiff_t count = SPECPDL_INDEX ();
10884 specbind (Qinhibit_read_only, Qt);
10885 /* Note that undo recording is always disabled. */
10886 del_range (BEG, Z);
10887 unbind_to (count, Qnil);
10889 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10891 /* Set up the buffer for the multibyteness we need. */
10892 if (multibyte_p
10893 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10894 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10896 /* Raise the frame containing the echo area. */
10897 if (minibuffer_auto_raise)
10899 struct frame *sf = SELECTED_FRAME ();
10900 Lisp_Object mini_window;
10901 mini_window = FRAME_MINIBUF_WINDOW (sf);
10902 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10905 message_log_maybe_newline ();
10906 message_buf_print = true;
10908 else
10910 if (NILP (echo_area_buffer[0]))
10912 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10913 echo_area_buffer[0] = echo_buffer[1];
10914 else
10915 echo_area_buffer[0] = echo_buffer[0];
10918 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10920 /* Someone switched buffers between print requests. */
10921 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10922 bset_truncate_lines (current_buffer, Qnil);
10928 /* Display an echo area message in window W. Value is true if W's
10929 height is changed. If display_last_displayed_message_p,
10930 display the message that was last displayed, otherwise
10931 display the current message. */
10933 static bool
10934 display_echo_area (struct window *w)
10936 bool no_message_p, window_height_changed_p;
10938 /* Temporarily disable garbage collections while displaying the echo
10939 area. This is done because a GC can print a message itself.
10940 That message would modify the echo area buffer's contents while a
10941 redisplay of the buffer is going on, and seriously confuse
10942 redisplay. */
10943 ptrdiff_t count = inhibit_garbage_collection ();
10945 /* If there is no message, we must call display_echo_area_1
10946 nevertheless because it resizes the window. But we will have to
10947 reset the echo_area_buffer in question to nil at the end because
10948 with_echo_area_buffer will sets it to an empty buffer. */
10949 bool i = display_last_displayed_message_p;
10950 /* According to the C99, C11 and C++11 standards, the integral value
10951 of a "bool" is always 0 or 1, so this array access is safe here,
10952 if oddly typed. */
10953 no_message_p = NILP (echo_area_buffer[i]);
10955 window_height_changed_p
10956 = with_echo_area_buffer (w, display_last_displayed_message_p,
10957 display_echo_area_1,
10958 (intptr_t) w, Qnil);
10960 if (no_message_p)
10961 echo_area_buffer[i] = Qnil;
10963 unbind_to (count, Qnil);
10964 return window_height_changed_p;
10968 /* Helper for display_echo_area. Display the current buffer which
10969 contains the current echo area message in window W, a mini-window,
10970 a pointer to which is passed in A1. A2..A4 are currently not used.
10971 Change the height of W so that all of the message is displayed.
10972 Value is true if height of W was changed. */
10974 static bool
10975 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10977 intptr_t i1 = a1;
10978 struct window *w = (struct window *) i1;
10979 Lisp_Object window;
10980 struct text_pos start;
10982 /* We are about to enter redisplay without going through
10983 redisplay_internal, so we need to forget these faces by hand
10984 here. */
10985 forget_escape_and_glyphless_faces ();
10987 /* Do this before displaying, so that we have a large enough glyph
10988 matrix for the display. If we can't get enough space for the
10989 whole text, display the last N lines. That works by setting w->start. */
10990 bool window_height_changed_p = resize_mini_window (w, false);
10992 /* Use the starting position chosen by resize_mini_window. */
10993 SET_TEXT_POS_FROM_MARKER (start, w->start);
10995 /* Display. */
10996 clear_glyph_matrix (w->desired_matrix);
10997 XSETWINDOW (window, w);
10998 try_window (window, start, 0);
11000 return window_height_changed_p;
11004 /* Resize the echo area window to exactly the size needed for the
11005 currently displayed message, if there is one. If a mini-buffer
11006 is active, don't shrink it. */
11008 void
11009 resize_echo_area_exactly (void)
11011 if (BUFFERP (echo_area_buffer[0])
11012 && WINDOWP (echo_area_window))
11014 struct window *w = XWINDOW (echo_area_window);
11015 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11016 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11017 (intptr_t) w, resize_exactly);
11018 if (resized_p)
11020 windows_or_buffers_changed = 42;
11021 update_mode_lines = 30;
11022 redisplay_internal ();
11028 /* Callback function for with_echo_area_buffer, when used from
11029 resize_echo_area_exactly. A1 contains a pointer to the window to
11030 resize, EXACTLY non-nil means resize the mini-window exactly to the
11031 size of the text displayed. A3 and A4 are not used. Value is what
11032 resize_mini_window returns. */
11034 static bool
11035 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11037 intptr_t i1 = a1;
11038 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11042 /* Resize mini-window W to fit the size of its contents. EXACT_P
11043 means size the window exactly to the size needed. Otherwise, it's
11044 only enlarged until W's buffer is empty.
11046 Set W->start to the right place to begin display. If the whole
11047 contents fit, start at the beginning. Otherwise, start so as
11048 to make the end of the contents appear. This is particularly
11049 important for y-or-n-p, but seems desirable generally.
11051 Value is true if the window height has been changed. */
11053 bool
11054 resize_mini_window (struct window *w, bool exact_p)
11056 struct frame *f = XFRAME (w->frame);
11057 bool window_height_changed_p = false;
11059 eassert (MINI_WINDOW_P (w));
11061 /* By default, start display at the beginning. */
11062 set_marker_both (w->start, w->contents,
11063 BUF_BEGV (XBUFFER (w->contents)),
11064 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11066 /* Don't resize windows while redisplaying a window; it would
11067 confuse redisplay functions when the size of the window they are
11068 displaying changes from under them. Such a resizing can happen,
11069 for instance, when which-func prints a long message while
11070 we are running fontification-functions. We're running these
11071 functions with safe_call which binds inhibit-redisplay to t. */
11072 if (!NILP (Vinhibit_redisplay))
11073 return false;
11075 /* Nil means don't try to resize. */
11076 if (NILP (Vresize_mini_windows)
11077 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11078 return false;
11080 if (!FRAME_MINIBUF_ONLY_P (f))
11082 struct it it;
11083 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11084 + WINDOW_PIXEL_HEIGHT (w));
11085 int unit = FRAME_LINE_HEIGHT (f);
11086 int height, max_height;
11087 struct text_pos start;
11088 struct buffer *old_current_buffer = NULL;
11090 if (current_buffer != XBUFFER (w->contents))
11092 old_current_buffer = current_buffer;
11093 set_buffer_internal (XBUFFER (w->contents));
11096 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11098 /* Compute the max. number of lines specified by the user. */
11099 if (FLOATP (Vmax_mini_window_height))
11100 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11101 else if (INTEGERP (Vmax_mini_window_height))
11102 max_height = XINT (Vmax_mini_window_height) * unit;
11103 else
11104 max_height = total_height / 4;
11106 /* Correct that max. height if it's bogus. */
11107 max_height = clip_to_bounds (unit, max_height, total_height);
11109 /* Find out the height of the text in the window. */
11110 if (it.line_wrap == TRUNCATE)
11111 height = unit;
11112 else
11114 last_height = 0;
11115 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11116 if (it.max_ascent == 0 && it.max_descent == 0)
11117 height = it.current_y + last_height;
11118 else
11119 height = it.current_y + it.max_ascent + it.max_descent;
11120 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11123 /* Compute a suitable window start. */
11124 if (height > max_height)
11126 height = (max_height / unit) * unit;
11127 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11128 move_it_vertically_backward (&it, height - unit);
11129 start = it.current.pos;
11131 else
11132 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11133 SET_MARKER_FROM_TEXT_POS (w->start, start);
11135 if (EQ (Vresize_mini_windows, Qgrow_only))
11137 /* Let it grow only, until we display an empty message, in which
11138 case the window shrinks again. */
11139 if (height > WINDOW_PIXEL_HEIGHT (w))
11141 int old_height = WINDOW_PIXEL_HEIGHT (w);
11143 FRAME_WINDOWS_FROZEN (f) = true;
11144 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11145 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11147 else if (height < WINDOW_PIXEL_HEIGHT (w)
11148 && (exact_p || BEGV == ZV))
11150 int old_height = WINDOW_PIXEL_HEIGHT (w);
11152 FRAME_WINDOWS_FROZEN (f) = false;
11153 shrink_mini_window (w, true);
11154 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11157 else
11159 /* Always resize to exact size needed. */
11160 if (height > WINDOW_PIXEL_HEIGHT (w))
11162 int old_height = WINDOW_PIXEL_HEIGHT (w);
11164 FRAME_WINDOWS_FROZEN (f) = true;
11165 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11166 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11168 else if (height < WINDOW_PIXEL_HEIGHT (w))
11170 int old_height = WINDOW_PIXEL_HEIGHT (w);
11172 FRAME_WINDOWS_FROZEN (f) = false;
11173 shrink_mini_window (w, true);
11175 if (height)
11177 FRAME_WINDOWS_FROZEN (f) = true;
11178 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11181 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11185 if (old_current_buffer)
11186 set_buffer_internal (old_current_buffer);
11189 return window_height_changed_p;
11193 /* Value is the current message, a string, or nil if there is no
11194 current message. */
11196 Lisp_Object
11197 current_message (void)
11199 Lisp_Object msg;
11201 if (!BUFFERP (echo_area_buffer[0]))
11202 msg = Qnil;
11203 else
11205 with_echo_area_buffer (0, 0, current_message_1,
11206 (intptr_t) &msg, Qnil);
11207 if (NILP (msg))
11208 echo_area_buffer[0] = Qnil;
11211 return msg;
11215 static bool
11216 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11218 intptr_t i1 = a1;
11219 Lisp_Object *msg = (Lisp_Object *) i1;
11221 if (Z > BEG)
11222 *msg = make_buffer_string (BEG, Z, true);
11223 else
11224 *msg = Qnil;
11225 return false;
11229 /* Push the current message on Vmessage_stack for later restoration
11230 by restore_message. Value is true if the current message isn't
11231 empty. This is a relatively infrequent operation, so it's not
11232 worth optimizing. */
11234 bool
11235 push_message (void)
11237 Lisp_Object msg = current_message ();
11238 Vmessage_stack = Fcons (msg, Vmessage_stack);
11239 return STRINGP (msg);
11243 /* Restore message display from the top of Vmessage_stack. */
11245 void
11246 restore_message (void)
11248 eassert (CONSP (Vmessage_stack));
11249 message3_nolog (XCAR (Vmessage_stack));
11253 /* Handler for unwind-protect calling pop_message. */
11255 void
11256 pop_message_unwind (void)
11258 /* Pop the top-most entry off Vmessage_stack. */
11259 eassert (CONSP (Vmessage_stack));
11260 Vmessage_stack = XCDR (Vmessage_stack);
11264 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11265 exits. If the stack is not empty, we have a missing pop_message
11266 somewhere. */
11268 void
11269 check_message_stack (void)
11271 if (!NILP (Vmessage_stack))
11272 emacs_abort ();
11276 /* Truncate to NCHARS what will be displayed in the echo area the next
11277 time we display it---but don't redisplay it now. */
11279 void
11280 truncate_echo_area (ptrdiff_t nchars)
11282 if (nchars == 0)
11283 echo_area_buffer[0] = Qnil;
11284 else if (!noninteractive
11285 && INTERACTIVE
11286 && !NILP (echo_area_buffer[0]))
11288 struct frame *sf = SELECTED_FRAME ();
11289 /* Error messages get reported properly by cmd_error, so this must be
11290 just an informative message; if the frame hasn't really been
11291 initialized yet, just toss it. */
11292 if (sf->glyphs_initialized_p)
11293 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11298 /* Helper function for truncate_echo_area. Truncate the current
11299 message to at most NCHARS characters. */
11301 static bool
11302 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11304 if (BEG + nchars < Z)
11305 del_range (BEG + nchars, Z);
11306 if (Z == BEG)
11307 echo_area_buffer[0] = Qnil;
11308 return false;
11311 /* Set the current message to STRING. */
11313 static void
11314 set_message (Lisp_Object string)
11316 eassert (STRINGP (string));
11318 message_enable_multibyte = STRING_MULTIBYTE (string);
11320 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11321 message_buf_print = false;
11322 help_echo_showing_p = false;
11324 if (STRINGP (Vdebug_on_message)
11325 && STRINGP (string)
11326 && fast_string_match (Vdebug_on_message, string) >= 0)
11327 call_debugger (list2 (Qerror, string));
11331 /* Helper function for set_message. First argument is ignored and second
11332 argument has the same meaning as for set_message.
11333 This function is called with the echo area buffer being current. */
11335 static bool
11336 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11338 eassert (STRINGP (string));
11340 /* Change multibyteness of the echo buffer appropriately. */
11341 if (message_enable_multibyte
11342 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11343 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11345 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11346 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11347 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11349 /* Insert new message at BEG. */
11350 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11352 /* This function takes care of single/multibyte conversion.
11353 We just have to ensure that the echo area buffer has the right
11354 setting of enable_multibyte_characters. */
11355 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11357 return false;
11361 /* Clear messages. CURRENT_P means clear the current message.
11362 LAST_DISPLAYED_P means clear the message last displayed. */
11364 void
11365 clear_message (bool current_p, bool last_displayed_p)
11367 if (current_p)
11369 echo_area_buffer[0] = Qnil;
11370 message_cleared_p = true;
11373 if (last_displayed_p)
11374 echo_area_buffer[1] = Qnil;
11376 message_buf_print = false;
11379 /* Clear garbaged frames.
11381 This function is used where the old redisplay called
11382 redraw_garbaged_frames which in turn called redraw_frame which in
11383 turn called clear_frame. The call to clear_frame was a source of
11384 flickering. I believe a clear_frame is not necessary. It should
11385 suffice in the new redisplay to invalidate all current matrices,
11386 and ensure a complete redisplay of all windows. */
11388 static void
11389 clear_garbaged_frames (void)
11391 if (frame_garbaged)
11393 Lisp_Object tail, frame;
11394 struct frame *sf = SELECTED_FRAME ();
11396 FOR_EACH_FRAME (tail, frame)
11398 struct frame *f = XFRAME (frame);
11400 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11402 if (f->resized_p
11403 /* It makes no sense to redraw a non-selected TTY
11404 frame, since that will actually clear the
11405 selected frame, and might leave the selected
11406 frame with corrupted display, if it happens not
11407 to be marked garbaged. */
11408 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11409 redraw_frame (f);
11410 else
11411 clear_current_matrices (f);
11413 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11414 x_clear_under_internal_border (f);
11415 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11417 fset_redisplay (f);
11418 f->garbaged = false;
11419 f->resized_p = false;
11423 frame_garbaged = false;
11428 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11429 selected_frame. */
11431 static void
11432 echo_area_display (bool update_frame_p)
11434 Lisp_Object mini_window;
11435 struct window *w;
11436 struct frame *f;
11437 bool window_height_changed_p = false;
11438 struct frame *sf = SELECTED_FRAME ();
11440 mini_window = FRAME_MINIBUF_WINDOW (sf);
11441 w = XWINDOW (mini_window);
11442 f = XFRAME (WINDOW_FRAME (w));
11444 /* Don't display if frame is invisible or not yet initialized. */
11445 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11446 return;
11448 #ifdef HAVE_WINDOW_SYSTEM
11449 /* When Emacs starts, selected_frame may be the initial terminal
11450 frame. If we let this through, a message would be displayed on
11451 the terminal. */
11452 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11453 return;
11454 #endif /* HAVE_WINDOW_SYSTEM */
11456 /* Redraw garbaged frames. */
11457 clear_garbaged_frames ();
11459 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11461 echo_area_window = mini_window;
11462 window_height_changed_p = display_echo_area (w);
11463 w->must_be_updated_p = true;
11465 /* Update the display, unless called from redisplay_internal.
11466 Also don't update the screen during redisplay itself. The
11467 update will happen at the end of redisplay, and an update
11468 here could cause confusion. */
11469 if (update_frame_p && !redisplaying_p)
11471 int n = 0;
11473 /* If the display update has been interrupted by pending
11474 input, update mode lines in the frame. Due to the
11475 pending input, it might have been that redisplay hasn't
11476 been called, so that mode lines above the echo area are
11477 garbaged. This looks odd, so we prevent it here. */
11478 if (!display_completed)
11480 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11482 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11483 x_clear_under_internal_border (f);
11484 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11488 if (window_height_changed_p
11489 /* Don't do this if Emacs is shutting down. Redisplay
11490 needs to run hooks. */
11491 && !NILP (Vrun_hooks))
11493 /* Must update other windows. Likewise as in other
11494 cases, don't let this update be interrupted by
11495 pending input. */
11496 ptrdiff_t count = SPECPDL_INDEX ();
11497 specbind (Qredisplay_dont_pause, Qt);
11498 fset_redisplay (f);
11499 redisplay_internal ();
11500 unbind_to (count, Qnil);
11502 else if (FRAME_WINDOW_P (f) && n == 0)
11504 /* Window configuration is the same as before.
11505 Can do with a display update of the echo area,
11506 unless we displayed some mode lines. */
11507 update_single_window (w);
11508 flush_frame (f);
11510 else
11511 update_frame (f, true, true);
11513 /* If cursor is in the echo area, make sure that the next
11514 redisplay displays the minibuffer, so that the cursor will
11515 be replaced with what the minibuffer wants. */
11516 if (cursor_in_echo_area)
11517 wset_redisplay (XWINDOW (mini_window));
11520 else if (!EQ (mini_window, selected_window))
11521 wset_redisplay (XWINDOW (mini_window));
11523 /* Last displayed message is now the current message. */
11524 echo_area_buffer[1] = echo_area_buffer[0];
11525 /* Inform read_char that we're not echoing. */
11526 echo_message_buffer = Qnil;
11528 /* Prevent redisplay optimization in redisplay_internal by resetting
11529 this_line_start_pos. This is done because the mini-buffer now
11530 displays the message instead of its buffer text. */
11531 if (EQ (mini_window, selected_window))
11532 CHARPOS (this_line_start_pos) = 0;
11534 if (window_height_changed_p)
11536 fset_redisplay (f);
11538 /* If window configuration was changed, frames may have been
11539 marked garbaged. Clear them or we will experience
11540 surprises wrt scrolling.
11541 FIXME: How/why/when? */
11542 clear_garbaged_frames ();
11546 /* True if W's buffer was changed but not saved. */
11548 static bool
11549 window_buffer_changed (struct window *w)
11551 struct buffer *b = XBUFFER (w->contents);
11553 eassert (BUFFER_LIVE_P (b));
11555 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11558 /* True if W has %c or %C in its mode line and mode line should be updated. */
11560 static bool
11561 mode_line_update_needed (struct window *w)
11563 return (w->column_number_displayed != -1
11564 && !(PT == w->last_point && !window_outdated (w))
11565 && (w->column_number_displayed != current_column ()));
11568 /* True if window start of W is frozen and may not be changed during
11569 redisplay. */
11571 static bool
11572 window_frozen_p (struct window *w)
11574 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11576 Lisp_Object window;
11578 XSETWINDOW (window, w);
11579 if (MINI_WINDOW_P (w))
11580 return false;
11581 else if (EQ (window, selected_window))
11582 return false;
11583 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11584 && EQ (window, Vminibuf_scroll_window))
11585 /* This special window can't be frozen too. */
11586 return false;
11587 else
11588 return true;
11590 return false;
11593 /***********************************************************************
11594 Mode Lines and Frame Titles
11595 ***********************************************************************/
11597 /* A buffer for constructing non-propertized mode-line strings and
11598 frame titles in it; allocated from the heap in init_xdisp and
11599 resized as needed in store_mode_line_noprop_char. */
11601 static char *mode_line_noprop_buf;
11603 /* The buffer's end, and a current output position in it. */
11605 static char *mode_line_noprop_buf_end;
11606 static char *mode_line_noprop_ptr;
11608 #define MODE_LINE_NOPROP_LEN(start) \
11609 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11611 static enum {
11612 MODE_LINE_DISPLAY = 0,
11613 MODE_LINE_TITLE,
11614 MODE_LINE_NOPROP,
11615 MODE_LINE_STRING
11616 } mode_line_target;
11618 /* Alist that caches the results of :propertize.
11619 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11620 static Lisp_Object mode_line_proptrans_alist;
11622 /* List of strings making up the mode-line. */
11623 static Lisp_Object mode_line_string_list;
11625 /* Base face property when building propertized mode line string. */
11626 static Lisp_Object mode_line_string_face;
11627 static Lisp_Object mode_line_string_face_prop;
11630 /* Unwind data for mode line strings */
11632 static Lisp_Object Vmode_line_unwind_vector;
11634 static Lisp_Object
11635 format_mode_line_unwind_data (struct frame *target_frame,
11636 struct buffer *obuf,
11637 Lisp_Object owin,
11638 bool save_proptrans)
11640 Lisp_Object vector, tmp;
11642 /* Reduce consing by keeping one vector in
11643 Vwith_echo_area_save_vector. */
11644 vector = Vmode_line_unwind_vector;
11645 Vmode_line_unwind_vector = Qnil;
11647 if (NILP (vector))
11648 vector = Fmake_vector (make_number (10), Qnil);
11650 ASET (vector, 0, make_number (mode_line_target));
11651 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11652 ASET (vector, 2, mode_line_string_list);
11653 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11654 ASET (vector, 4, mode_line_string_face);
11655 ASET (vector, 5, mode_line_string_face_prop);
11657 if (obuf)
11658 XSETBUFFER (tmp, obuf);
11659 else
11660 tmp = Qnil;
11661 ASET (vector, 6, tmp);
11662 ASET (vector, 7, owin);
11663 if (target_frame)
11665 /* Similarly to `with-selected-window', if the operation selects
11666 a window on another frame, we must restore that frame's
11667 selected window, and (for a tty) the top-frame. */
11668 ASET (vector, 8, target_frame->selected_window);
11669 if (FRAME_TERMCAP_P (target_frame))
11670 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11673 return vector;
11676 static void
11677 unwind_format_mode_line (Lisp_Object vector)
11679 Lisp_Object old_window = AREF (vector, 7);
11680 Lisp_Object target_frame_window = AREF (vector, 8);
11681 Lisp_Object old_top_frame = AREF (vector, 9);
11683 mode_line_target = XINT (AREF (vector, 0));
11684 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11685 mode_line_string_list = AREF (vector, 2);
11686 if (! EQ (AREF (vector, 3), Qt))
11687 mode_line_proptrans_alist = AREF (vector, 3);
11688 mode_line_string_face = AREF (vector, 4);
11689 mode_line_string_face_prop = AREF (vector, 5);
11691 /* Select window before buffer, since it may change the buffer. */
11692 if (!NILP (old_window))
11694 /* If the operation that we are unwinding had selected a window
11695 on a different frame, reset its frame-selected-window. For a
11696 text terminal, reset its top-frame if necessary. */
11697 if (!NILP (target_frame_window))
11699 Lisp_Object frame
11700 = WINDOW_FRAME (XWINDOW (target_frame_window));
11702 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11703 Fselect_window (target_frame_window, Qt);
11705 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11706 Fselect_frame (old_top_frame, Qt);
11709 Fselect_window (old_window, Qt);
11712 if (!NILP (AREF (vector, 6)))
11714 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11715 ASET (vector, 6, Qnil);
11718 Vmode_line_unwind_vector = vector;
11722 /* Store a single character C for the frame title in mode_line_noprop_buf.
11723 Re-allocate mode_line_noprop_buf if necessary. */
11725 static void
11726 store_mode_line_noprop_char (char c)
11728 /* If output position has reached the end of the allocated buffer,
11729 increase the buffer's size. */
11730 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11732 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11733 ptrdiff_t size = len;
11734 mode_line_noprop_buf =
11735 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11736 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11737 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11740 *mode_line_noprop_ptr++ = c;
11744 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11745 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11746 characters that yield more columns than PRECISION; PRECISION <= 0
11747 means copy the whole string. Pad with spaces until FIELD_WIDTH
11748 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11749 pad. Called from display_mode_element when it is used to build a
11750 frame title. */
11752 static int
11753 store_mode_line_noprop (const char *string, int field_width, int precision)
11755 const unsigned char *str = (const unsigned char *) string;
11756 int n = 0;
11757 ptrdiff_t dummy, nbytes;
11759 /* Copy at most PRECISION chars from STR. */
11760 nbytes = strlen (string);
11761 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11762 while (nbytes--)
11763 store_mode_line_noprop_char (*str++);
11765 /* Fill up with spaces until FIELD_WIDTH reached. */
11766 while (field_width > 0
11767 && n < field_width)
11769 store_mode_line_noprop_char (' ');
11770 ++n;
11773 return n;
11776 /***********************************************************************
11777 Frame Titles
11778 ***********************************************************************/
11780 #ifdef HAVE_WINDOW_SYSTEM
11782 /* Set the title of FRAME, if it has changed. The title format is
11783 Vicon_title_format if FRAME is iconified, otherwise it is
11784 frame_title_format. */
11786 static void
11787 x_consider_frame_title (Lisp_Object frame)
11789 struct frame *f = XFRAME (frame);
11791 if ((FRAME_WINDOW_P (f)
11792 || FRAME_MINIBUF_ONLY_P (f)
11793 || f->explicit_name)
11794 && NILP (Fframe_parameter (frame, Qtooltip)))
11796 /* Do we have more than one visible frame on this X display? */
11797 Lisp_Object tail, other_frame, fmt;
11798 ptrdiff_t title_start;
11799 char *title;
11800 ptrdiff_t len;
11801 struct it it;
11802 ptrdiff_t count = SPECPDL_INDEX ();
11804 FOR_EACH_FRAME (tail, other_frame)
11806 struct frame *tf = XFRAME (other_frame);
11808 if (tf != f
11809 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11810 && !FRAME_MINIBUF_ONLY_P (tf)
11811 && !EQ (other_frame, tip_frame)
11812 && !FRAME_PARENT_FRAME (tf)
11813 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11814 break;
11817 /* Set global variable indicating that multiple frames exist. */
11818 multiple_frames = CONSP (tail);
11820 /* Switch to the buffer of selected window of the frame. Set up
11821 mode_line_target so that display_mode_element will output into
11822 mode_line_noprop_buf; then display the title. */
11823 record_unwind_protect (unwind_format_mode_line,
11824 format_mode_line_unwind_data
11825 (f, current_buffer, selected_window, false));
11826 /* select-frame calls resize_mini_window, which could resize the
11827 mini-window and by that undo the effect of this redisplay
11828 cycle wrt minibuffer and echo-area display. Binding
11829 inhibit-redisplay to t makes the call to resize_mini_window a
11830 no-op, thus avoiding the adverse side effects. */
11831 specbind (Qinhibit_redisplay, Qt);
11833 Fselect_window (f->selected_window, Qt);
11834 set_buffer_internal_1
11835 (XBUFFER (XWINDOW (f->selected_window)->contents));
11836 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11838 mode_line_target = MODE_LINE_TITLE;
11839 title_start = MODE_LINE_NOPROP_LEN (0);
11840 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11841 NULL, DEFAULT_FACE_ID);
11842 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11843 len = MODE_LINE_NOPROP_LEN (title_start);
11844 title = mode_line_noprop_buf + title_start;
11845 unbind_to (count, Qnil);
11847 /* Set the title only if it's changed. This avoids consing in
11848 the common case where it hasn't. (If it turns out that we've
11849 already wasted too much time by walking through the list with
11850 display_mode_element, then we might need to optimize at a
11851 higher level than this.) */
11852 if (! STRINGP (f->name)
11853 || SBYTES (f->name) != len
11854 || memcmp (title, SDATA (f->name), len) != 0)
11855 x_implicitly_set_name (f, make_string (title, len), Qnil);
11859 #endif /* not HAVE_WINDOW_SYSTEM */
11862 /***********************************************************************
11863 Menu Bars
11864 ***********************************************************************/
11866 /* True if we will not redisplay all visible windows. */
11867 #define REDISPLAY_SOME_P() \
11868 ((windows_or_buffers_changed == 0 \
11869 || windows_or_buffers_changed == REDISPLAY_SOME) \
11870 && (update_mode_lines == 0 \
11871 || update_mode_lines == REDISPLAY_SOME))
11873 /* Prepare for redisplay by updating menu-bar item lists when
11874 appropriate. This can call eval. */
11876 static void
11877 prepare_menu_bars (void)
11879 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11880 bool some_windows = REDISPLAY_SOME_P ();
11881 Lisp_Object tooltip_frame;
11883 #ifdef HAVE_WINDOW_SYSTEM
11884 tooltip_frame = tip_frame;
11885 #else
11886 tooltip_frame = Qnil;
11887 #endif
11889 if (FUNCTIONP (Vpre_redisplay_function))
11891 Lisp_Object windows = all_windows ? Qt : Qnil;
11892 if (all_windows && some_windows)
11894 Lisp_Object ws = window_list ();
11895 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11897 Lisp_Object this = XCAR (ws);
11898 struct window *w = XWINDOW (this);
11899 if (w->redisplay
11900 || XFRAME (w->frame)->redisplay
11901 || XBUFFER (w->contents)->text->redisplay)
11903 windows = Fcons (this, windows);
11907 safe__call1 (true, Vpre_redisplay_function, windows);
11910 /* Update all frame titles based on their buffer names, etc. We do
11911 this before the menu bars so that the buffer-menu will show the
11912 up-to-date frame titles. */
11913 #ifdef HAVE_WINDOW_SYSTEM
11914 if (all_windows)
11916 Lisp_Object tail, frame;
11918 FOR_EACH_FRAME (tail, frame)
11920 struct frame *f = XFRAME (frame);
11921 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11922 if (some_windows
11923 && !f->redisplay
11924 && !w->redisplay
11925 && !XBUFFER (w->contents)->text->redisplay)
11926 continue;
11928 if (!EQ (frame, tooltip_frame)
11929 && !FRAME_PARENT_FRAME (f)
11930 && (FRAME_ICONIFIED_P (f)
11931 || FRAME_VISIBLE_P (f) == 1
11932 /* Exclude TTY frames that are obscured because they
11933 are not the top frame on their console. This is
11934 because x_consider_frame_title actually switches
11935 to the frame, which for TTY frames means it is
11936 marked as garbaged, and will be completely
11937 redrawn on the next redisplay cycle. This causes
11938 TTY frames to be completely redrawn, when there
11939 are more than one of them, even though nothing
11940 should be changed on display. */
11941 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11942 x_consider_frame_title (frame);
11945 #endif /* HAVE_WINDOW_SYSTEM */
11947 /* Update the menu bar item lists, if appropriate. This has to be
11948 done before any actual redisplay or generation of display lines. */
11950 if (all_windows)
11952 Lisp_Object tail, frame;
11953 ptrdiff_t count = SPECPDL_INDEX ();
11954 /* True means that update_menu_bar has run its hooks
11955 so any further calls to update_menu_bar shouldn't do so again. */
11956 bool menu_bar_hooks_run = false;
11958 record_unwind_save_match_data ();
11960 FOR_EACH_FRAME (tail, frame)
11962 struct frame *f = XFRAME (frame);
11963 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11965 /* Ignore tooltip frame. */
11966 if (EQ (frame, tooltip_frame))
11967 continue;
11969 if (some_windows
11970 && !f->redisplay
11971 && !w->redisplay
11972 && !XBUFFER (w->contents)->text->redisplay)
11973 continue;
11975 run_window_size_change_functions (frame);
11977 if (FRAME_PARENT_FRAME (f))
11978 continue;
11980 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11981 #ifdef HAVE_WINDOW_SYSTEM
11982 update_tool_bar (f, false);
11983 #endif
11986 unbind_to (count, Qnil);
11988 else
11990 struct frame *sf = SELECTED_FRAME ();
11991 update_menu_bar (sf, true, false);
11992 #ifdef HAVE_WINDOW_SYSTEM
11993 update_tool_bar (sf, true);
11994 #endif
11999 /* Update the menu bar item list for frame F. This has to be done
12000 before we start to fill in any display lines, because it can call
12001 eval.
12003 If SAVE_MATCH_DATA, we must save and restore it here.
12005 If HOOKS_RUN, a previous call to update_menu_bar
12006 already ran the menu bar hooks for this redisplay, so there
12007 is no need to run them again. The return value is the
12008 updated value of this flag, to pass to the next call. */
12010 static bool
12011 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12013 Lisp_Object window;
12014 struct window *w;
12016 /* If called recursively during a menu update, do nothing. This can
12017 happen when, for instance, an activate-menubar-hook causes a
12018 redisplay. */
12019 if (inhibit_menubar_update)
12020 return hooks_run;
12022 window = FRAME_SELECTED_WINDOW (f);
12023 w = XWINDOW (window);
12025 if (FRAME_WINDOW_P (f)
12027 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12028 || defined (HAVE_NS) || defined (USE_GTK)
12029 FRAME_EXTERNAL_MENU_BAR (f)
12030 #else
12031 FRAME_MENU_BAR_LINES (f) > 0
12032 #endif
12033 : FRAME_MENU_BAR_LINES (f) > 0)
12035 /* If the user has switched buffers or windows, we need to
12036 recompute to reflect the new bindings. But we'll
12037 recompute when update_mode_lines is set too; that means
12038 that people can use force-mode-line-update to request
12039 that the menu bar be recomputed. The adverse effect on
12040 the rest of the redisplay algorithm is about the same as
12041 windows_or_buffers_changed anyway. */
12042 if (windows_or_buffers_changed
12043 /* This used to test w->update_mode_line, but we believe
12044 there is no need to recompute the menu in that case. */
12045 || update_mode_lines
12046 || window_buffer_changed (w))
12048 struct buffer *prev = current_buffer;
12049 ptrdiff_t count = SPECPDL_INDEX ();
12051 specbind (Qinhibit_menubar_update, Qt);
12053 set_buffer_internal_1 (XBUFFER (w->contents));
12054 if (save_match_data)
12055 record_unwind_save_match_data ();
12056 if (NILP (Voverriding_local_map_menu_flag))
12058 specbind (Qoverriding_terminal_local_map, Qnil);
12059 specbind (Qoverriding_local_map, Qnil);
12062 if (!hooks_run)
12064 /* Run the Lucid hook. */
12065 safe_run_hooks (Qactivate_menubar_hook);
12067 /* If it has changed current-menubar from previous value,
12068 really recompute the menu-bar from the value. */
12069 if (! NILP (Vlucid_menu_bar_dirty_flag))
12070 call0 (Qrecompute_lucid_menubar);
12072 safe_run_hooks (Qmenu_bar_update_hook);
12074 hooks_run = true;
12077 XSETFRAME (Vmenu_updating_frame, f);
12078 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12080 /* Redisplay the menu bar in case we changed it. */
12081 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12082 || defined (HAVE_NS) || defined (USE_GTK)
12083 if (FRAME_WINDOW_P (f))
12085 #if defined (HAVE_NS)
12086 /* All frames on Mac OS share the same menubar. So only
12087 the selected frame should be allowed to set it. */
12088 if (f == SELECTED_FRAME ())
12089 #endif
12090 set_frame_menubar (f, false, false);
12092 else
12093 /* On a terminal screen, the menu bar is an ordinary screen
12094 line, and this makes it get updated. */
12095 w->update_mode_line = true;
12096 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12097 /* In the non-toolkit version, the menu bar is an ordinary screen
12098 line, and this makes it get updated. */
12099 w->update_mode_line = true;
12100 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12102 unbind_to (count, Qnil);
12103 set_buffer_internal_1 (prev);
12107 return hooks_run;
12110 /***********************************************************************
12111 Tool-bars
12112 ***********************************************************************/
12114 #ifdef HAVE_WINDOW_SYSTEM
12116 /* Select `frame' temporarily without running all the code in
12117 do_switch_frame.
12118 FIXME: Maybe do_switch_frame should be trimmed down similarly
12119 when `norecord' is set. */
12120 static void
12121 fast_set_selected_frame (Lisp_Object frame)
12123 if (!EQ (selected_frame, frame))
12125 selected_frame = frame;
12126 selected_window = XFRAME (frame)->selected_window;
12130 /* Update the tool-bar item list for frame F. This has to be done
12131 before we start to fill in any display lines. Called from
12132 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12133 and restore it here. */
12135 static void
12136 update_tool_bar (struct frame *f, bool save_match_data)
12138 #if defined (USE_GTK) || defined (HAVE_NS)
12139 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12140 #else
12141 bool do_update = (WINDOWP (f->tool_bar_window)
12142 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12143 #endif
12145 if (do_update)
12147 Lisp_Object window;
12148 struct window *w;
12150 window = FRAME_SELECTED_WINDOW (f);
12151 w = XWINDOW (window);
12153 /* If the user has switched buffers or windows, we need to
12154 recompute to reflect the new bindings. But we'll
12155 recompute when update_mode_lines is set too; that means
12156 that people can use force-mode-line-update to request
12157 that the menu bar be recomputed. The adverse effect on
12158 the rest of the redisplay algorithm is about the same as
12159 windows_or_buffers_changed anyway. */
12160 if (windows_or_buffers_changed
12161 || w->update_mode_line
12162 || update_mode_lines
12163 || window_buffer_changed (w))
12165 struct buffer *prev = current_buffer;
12166 ptrdiff_t count = SPECPDL_INDEX ();
12167 Lisp_Object frame, new_tool_bar;
12168 int new_n_tool_bar;
12170 /* Set current_buffer to the buffer of the selected
12171 window of the frame, so that we get the right local
12172 keymaps. */
12173 set_buffer_internal_1 (XBUFFER (w->contents));
12175 /* Save match data, if we must. */
12176 if (save_match_data)
12177 record_unwind_save_match_data ();
12179 /* Make sure that we don't accidentally use bogus keymaps. */
12180 if (NILP (Voverriding_local_map_menu_flag))
12182 specbind (Qoverriding_terminal_local_map, Qnil);
12183 specbind (Qoverriding_local_map, Qnil);
12186 /* We must temporarily set the selected frame to this frame
12187 before calling tool_bar_items, because the calculation of
12188 the tool-bar keymap uses the selected frame (see
12189 `tool-bar-make-keymap' in tool-bar.el). */
12190 eassert (EQ (selected_window,
12191 /* Since we only explicitly preserve selected_frame,
12192 check that selected_window would be redundant. */
12193 XFRAME (selected_frame)->selected_window));
12194 record_unwind_protect (fast_set_selected_frame, selected_frame);
12195 XSETFRAME (frame, f);
12196 fast_set_selected_frame (frame);
12198 /* Build desired tool-bar items from keymaps. */
12199 new_tool_bar
12200 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12201 &new_n_tool_bar);
12203 /* Redisplay the tool-bar if we changed it. */
12204 if (new_n_tool_bar != f->n_tool_bar_items
12205 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12207 /* Redisplay that happens asynchronously due to an expose event
12208 may access f->tool_bar_items. Make sure we update both
12209 variables within BLOCK_INPUT so no such event interrupts. */
12210 block_input ();
12211 fset_tool_bar_items (f, new_tool_bar);
12212 f->n_tool_bar_items = new_n_tool_bar;
12213 w->update_mode_line = true;
12214 unblock_input ();
12217 unbind_to (count, Qnil);
12218 set_buffer_internal_1 (prev);
12223 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12225 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12226 F's desired tool-bar contents. F->tool_bar_items must have
12227 been set up previously by calling prepare_menu_bars. */
12229 static void
12230 build_desired_tool_bar_string (struct frame *f)
12232 int i, size, size_needed;
12233 Lisp_Object image, plist;
12235 image = plist = Qnil;
12237 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12238 Otherwise, make a new string. */
12240 /* The size of the string we might be able to reuse. */
12241 size = (STRINGP (f->desired_tool_bar_string)
12242 ? SCHARS (f->desired_tool_bar_string)
12243 : 0);
12245 /* We need one space in the string for each image. */
12246 size_needed = f->n_tool_bar_items;
12248 /* Reuse f->desired_tool_bar_string, if possible. */
12249 if (size < size_needed || NILP (f->desired_tool_bar_string))
12250 fset_desired_tool_bar_string
12251 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12252 else
12254 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12255 Fremove_text_properties (make_number (0), make_number (size),
12256 props, f->desired_tool_bar_string);
12259 /* Put a `display' property on the string for the images to display,
12260 put a `menu_item' property on tool-bar items with a value that
12261 is the index of the item in F's tool-bar item vector. */
12262 for (i = 0; i < f->n_tool_bar_items; ++i)
12264 #define PROP(IDX) \
12265 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12267 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12268 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12269 int hmargin, vmargin, relief, idx, end;
12271 /* If image is a vector, choose the image according to the
12272 button state. */
12273 image = PROP (TOOL_BAR_ITEM_IMAGES);
12274 if (VECTORP (image))
12276 if (enabled_p)
12277 idx = (selected_p
12278 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12279 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12280 else
12281 idx = (selected_p
12282 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12283 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12285 eassert (ASIZE (image) >= idx);
12286 image = AREF (image, idx);
12288 else
12289 idx = -1;
12291 /* Ignore invalid image specifications. */
12292 if (!valid_image_p (image))
12293 continue;
12295 /* Display the tool-bar button pressed, or depressed. */
12296 plist = Fcopy_sequence (XCDR (image));
12298 /* Compute margin and relief to draw. */
12299 relief = (tool_bar_button_relief >= 0
12300 ? tool_bar_button_relief
12301 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12302 hmargin = vmargin = relief;
12304 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12305 INT_MAX - max (hmargin, vmargin)))
12307 hmargin += XFASTINT (Vtool_bar_button_margin);
12308 vmargin += XFASTINT (Vtool_bar_button_margin);
12310 else if (CONSP (Vtool_bar_button_margin))
12312 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12313 INT_MAX - hmargin))
12314 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12316 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12317 INT_MAX - vmargin))
12318 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12321 if (auto_raise_tool_bar_buttons_p)
12323 /* Add a `:relief' property to the image spec if the item is
12324 selected. */
12325 if (selected_p)
12327 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12328 hmargin -= relief;
12329 vmargin -= relief;
12332 else
12334 /* If image is selected, display it pressed, i.e. with a
12335 negative relief. If it's not selected, display it with a
12336 raised relief. */
12337 plist = Fplist_put (plist, QCrelief,
12338 (selected_p
12339 ? make_number (-relief)
12340 : make_number (relief)));
12341 hmargin -= relief;
12342 vmargin -= relief;
12345 /* Put a margin around the image. */
12346 if (hmargin || vmargin)
12348 if (hmargin == vmargin)
12349 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12350 else
12351 plist = Fplist_put (plist, QCmargin,
12352 Fcons (make_number (hmargin),
12353 make_number (vmargin)));
12356 /* If button is not enabled, and we don't have special images
12357 for the disabled state, make the image appear disabled by
12358 applying an appropriate algorithm to it. */
12359 if (!enabled_p && idx < 0)
12360 plist = Fplist_put (plist, QCconversion, Qdisabled);
12362 /* Put a `display' text property on the string for the image to
12363 display. Put a `menu-item' property on the string that gives
12364 the start of this item's properties in the tool-bar items
12365 vector. */
12366 image = Fcons (Qimage, plist);
12367 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12368 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12370 /* Let the last image hide all remaining spaces in the tool bar
12371 string. The string can be longer than needed when we reuse a
12372 previous string. */
12373 if (i + 1 == f->n_tool_bar_items)
12374 end = SCHARS (f->desired_tool_bar_string);
12375 else
12376 end = i + 1;
12377 Fadd_text_properties (make_number (i), make_number (end),
12378 props, f->desired_tool_bar_string);
12379 #undef PROP
12384 /* Display one line of the tool-bar of frame IT->f.
12386 HEIGHT specifies the desired height of the tool-bar line.
12387 If the actual height of the glyph row is less than HEIGHT, the
12388 row's height is increased to HEIGHT, and the icons are centered
12389 vertically in the new height.
12391 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12392 count a final empty row in case the tool-bar width exactly matches
12393 the window width.
12396 static void
12397 display_tool_bar_line (struct it *it, int height)
12399 struct glyph_row *row = it->glyph_row;
12400 int max_x = it->last_visible_x;
12401 struct glyph *last;
12403 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12404 clear_glyph_row (row);
12405 row->enabled_p = true;
12406 row->y = it->current_y;
12408 /* Note that this isn't made use of if the face hasn't a box,
12409 so there's no need to check the face here. */
12410 it->start_of_box_run_p = true;
12412 while (it->current_x < max_x)
12414 int x, n_glyphs_before, i, nglyphs;
12415 struct it it_before;
12417 /* Get the next display element. */
12418 if (!get_next_display_element (it))
12420 /* Don't count empty row if we are counting needed tool-bar lines. */
12421 if (height < 0 && !it->hpos)
12422 return;
12423 break;
12426 /* Produce glyphs. */
12427 n_glyphs_before = row->used[TEXT_AREA];
12428 it_before = *it;
12430 PRODUCE_GLYPHS (it);
12432 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12433 i = 0;
12434 x = it_before.current_x;
12435 while (i < nglyphs)
12437 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12439 if (x + glyph->pixel_width > max_x)
12441 /* Glyph doesn't fit on line. Backtrack. */
12442 row->used[TEXT_AREA] = n_glyphs_before;
12443 *it = it_before;
12444 /* If this is the only glyph on this line, it will never fit on the
12445 tool-bar, so skip it. But ensure there is at least one glyph,
12446 so we don't accidentally disable the tool-bar. */
12447 if (n_glyphs_before == 0
12448 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12449 break;
12450 goto out;
12453 ++it->hpos;
12454 x += glyph->pixel_width;
12455 ++i;
12458 /* Stop at line end. */
12459 if (ITERATOR_AT_END_OF_LINE_P (it))
12460 break;
12462 set_iterator_to_next (it, true);
12465 out:;
12467 row->displays_text_p = row->used[TEXT_AREA] != 0;
12469 /* Use default face for the border below the tool bar.
12471 FIXME: When auto-resize-tool-bars is grow-only, there is
12472 no additional border below the possibly empty tool-bar lines.
12473 So to make the extra empty lines look "normal", we have to
12474 use the tool-bar face for the border too. */
12475 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12476 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12477 it->face_id = DEFAULT_FACE_ID;
12479 extend_face_to_end_of_line (it);
12480 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12481 last->right_box_line_p = true;
12482 if (last == row->glyphs[TEXT_AREA])
12483 last->left_box_line_p = true;
12485 /* Make line the desired height and center it vertically. */
12486 if ((height -= it->max_ascent + it->max_descent) > 0)
12488 /* Don't add more than one line height. */
12489 height %= FRAME_LINE_HEIGHT (it->f);
12490 it->max_ascent += height / 2;
12491 it->max_descent += (height + 1) / 2;
12494 compute_line_metrics (it);
12496 /* If line is empty, make it occupy the rest of the tool-bar. */
12497 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12499 row->height = row->phys_height = it->last_visible_y - row->y;
12500 row->visible_height = row->height;
12501 row->ascent = row->phys_ascent = 0;
12502 row->extra_line_spacing = 0;
12505 row->full_width_p = true;
12506 row->continued_p = false;
12507 row->truncated_on_left_p = false;
12508 row->truncated_on_right_p = false;
12510 it->current_x = it->hpos = 0;
12511 it->current_y += row->height;
12512 ++it->vpos;
12513 ++it->glyph_row;
12517 /* Value is the number of pixels needed to make all tool-bar items of
12518 frame F visible. The actual number of glyph rows needed is
12519 returned in *N_ROWS if non-NULL. */
12520 static int
12521 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12523 struct window *w = XWINDOW (f->tool_bar_window);
12524 struct it it;
12525 /* tool_bar_height is called from redisplay_tool_bar after building
12526 the desired matrix, so use (unused) mode-line row as temporary row to
12527 avoid destroying the first tool-bar row. */
12528 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12530 /* Initialize an iterator for iteration over
12531 F->desired_tool_bar_string in the tool-bar window of frame F. */
12532 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12533 temp_row->reversed_p = false;
12534 it.first_visible_x = 0;
12535 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12536 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12537 it.paragraph_embedding = L2R;
12539 while (!ITERATOR_AT_END_P (&it))
12541 clear_glyph_row (temp_row);
12542 it.glyph_row = temp_row;
12543 display_tool_bar_line (&it, -1);
12545 clear_glyph_row (temp_row);
12547 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12548 if (n_rows)
12549 *n_rows = it.vpos > 0 ? it.vpos : -1;
12551 if (pixelwise)
12552 return it.current_y;
12553 else
12554 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12557 #endif /* !USE_GTK && !HAVE_NS */
12559 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12560 0, 2, 0,
12561 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12562 If FRAME is nil or omitted, use the selected frame. Optional argument
12563 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12564 (Lisp_Object frame, Lisp_Object pixelwise)
12566 int height = 0;
12568 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12569 struct frame *f = decode_any_frame (frame);
12571 if (WINDOWP (f->tool_bar_window)
12572 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12574 update_tool_bar (f, true);
12575 if (f->n_tool_bar_items)
12577 build_desired_tool_bar_string (f);
12578 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12581 #endif
12583 return make_number (height);
12587 /* Display the tool-bar of frame F. Value is true if tool-bar's
12588 height should be changed. */
12589 static bool
12590 redisplay_tool_bar (struct frame *f)
12592 f->tool_bar_redisplayed = true;
12593 #if defined (USE_GTK) || defined (HAVE_NS)
12595 if (FRAME_EXTERNAL_TOOL_BAR (f))
12596 update_frame_tool_bar (f);
12597 return false;
12599 #else /* !USE_GTK && !HAVE_NS */
12601 struct window *w;
12602 struct it it;
12603 struct glyph_row *row;
12605 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12606 do anything. This means you must start with tool-bar-lines
12607 non-zero to get the auto-sizing effect. Or in other words, you
12608 can turn off tool-bars by specifying tool-bar-lines zero. */
12609 if (!WINDOWP (f->tool_bar_window)
12610 || (w = XWINDOW (f->tool_bar_window),
12611 WINDOW_TOTAL_LINES (w) == 0))
12612 return false;
12614 /* Set up an iterator for the tool-bar window. */
12615 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12616 it.first_visible_x = 0;
12617 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12618 row = it.glyph_row;
12619 row->reversed_p = false;
12621 /* Build a string that represents the contents of the tool-bar. */
12622 build_desired_tool_bar_string (f);
12623 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12624 /* FIXME: This should be controlled by a user option. But it
12625 doesn't make sense to have an R2L tool bar if the menu bar cannot
12626 be drawn also R2L, and making the menu bar R2L is tricky due
12627 toolkit-specific code that implements it. If an R2L tool bar is
12628 ever supported, display_tool_bar_line should also be augmented to
12629 call unproduce_glyphs like display_line and display_string
12630 do. */
12631 it.paragraph_embedding = L2R;
12633 if (f->n_tool_bar_rows == 0)
12635 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12637 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12639 x_change_tool_bar_height (f, new_height);
12640 frame_default_tool_bar_height = new_height;
12641 /* Always do that now. */
12642 clear_glyph_matrix (w->desired_matrix);
12643 f->fonts_changed = true;
12644 return true;
12648 /* Display as many lines as needed to display all tool-bar items. */
12650 if (f->n_tool_bar_rows > 0)
12652 int border, rows, height, extra;
12654 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12655 border = XINT (Vtool_bar_border);
12656 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12657 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12658 else if (EQ (Vtool_bar_border, Qborder_width))
12659 border = f->border_width;
12660 else
12661 border = 0;
12662 if (border < 0)
12663 border = 0;
12665 rows = f->n_tool_bar_rows;
12666 height = max (1, (it.last_visible_y - border) / rows);
12667 extra = it.last_visible_y - border - height * rows;
12669 while (it.current_y < it.last_visible_y)
12671 int h = 0;
12672 if (extra > 0 && rows-- > 0)
12674 h = (extra + rows - 1) / rows;
12675 extra -= h;
12677 display_tool_bar_line (&it, height + h);
12680 else
12682 while (it.current_y < it.last_visible_y)
12683 display_tool_bar_line (&it, 0);
12686 /* It doesn't make much sense to try scrolling in the tool-bar
12687 window, so don't do it. */
12688 w->desired_matrix->no_scrolling_p = true;
12689 w->must_be_updated_p = true;
12691 if (!NILP (Vauto_resize_tool_bars))
12693 bool change_height_p = true;
12695 /* If we couldn't display everything, change the tool-bar's
12696 height if there is room for more. */
12697 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12698 change_height_p = true;
12700 /* We subtract 1 because display_tool_bar_line advances the
12701 glyph_row pointer before returning to its caller. We want to
12702 examine the last glyph row produced by
12703 display_tool_bar_line. */
12704 row = it.glyph_row - 1;
12706 /* If there are blank lines at the end, except for a partially
12707 visible blank line at the end that is smaller than
12708 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12709 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12710 && row->height >= FRAME_LINE_HEIGHT (f))
12711 change_height_p = true;
12713 /* If row displays tool-bar items, but is partially visible,
12714 change the tool-bar's height. */
12715 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12716 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12717 change_height_p = true;
12719 /* Resize windows as needed by changing the `tool-bar-lines'
12720 frame parameter. */
12721 if (change_height_p)
12723 int nrows;
12724 int new_height = tool_bar_height (f, &nrows, true);
12726 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12727 && !f->minimize_tool_bar_window_p)
12728 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12729 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12730 f->minimize_tool_bar_window_p = false;
12732 if (change_height_p)
12734 x_change_tool_bar_height (f, new_height);
12735 frame_default_tool_bar_height = new_height;
12736 clear_glyph_matrix (w->desired_matrix);
12737 f->n_tool_bar_rows = nrows;
12738 f->fonts_changed = true;
12740 return true;
12745 f->minimize_tool_bar_window_p = false;
12746 return false;
12748 #endif /* USE_GTK || HAVE_NS */
12751 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12753 /* Get information about the tool-bar item which is displayed in GLYPH
12754 on frame F. Return in *PROP_IDX the index where tool-bar item
12755 properties start in F->tool_bar_items. Value is false if
12756 GLYPH doesn't display a tool-bar item. */
12758 static bool
12759 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12761 Lisp_Object prop;
12762 int charpos;
12764 /* This function can be called asynchronously, which means we must
12765 exclude any possibility that Fget_text_property signals an
12766 error. */
12767 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12768 charpos = max (0, charpos);
12770 /* Get the text property `menu-item' at pos. The value of that
12771 property is the start index of this item's properties in
12772 F->tool_bar_items. */
12773 prop = Fget_text_property (make_number (charpos),
12774 Qmenu_item, f->current_tool_bar_string);
12775 if (! INTEGERP (prop))
12776 return false;
12777 *prop_idx = XINT (prop);
12778 return true;
12782 /* Get information about the tool-bar item at position X/Y on frame F.
12783 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12784 the current matrix of the tool-bar window of F, or NULL if not
12785 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12786 item in F->tool_bar_items. Value is
12788 -1 if X/Y is not on a tool-bar item
12789 0 if X/Y is on the same item that was highlighted before.
12790 1 otherwise. */
12792 static int
12793 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12794 int *hpos, int *vpos, int *prop_idx)
12796 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12797 struct window *w = XWINDOW (f->tool_bar_window);
12798 int area;
12800 /* Find the glyph under X/Y. */
12801 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12802 if (*glyph == NULL)
12803 return -1;
12805 /* Get the start of this tool-bar item's properties in
12806 f->tool_bar_items. */
12807 if (!tool_bar_item_info (f, *glyph, prop_idx))
12808 return -1;
12810 /* Is mouse on the highlighted item? */
12811 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12812 && *vpos >= hlinfo->mouse_face_beg_row
12813 && *vpos <= hlinfo->mouse_face_end_row
12814 && (*vpos > hlinfo->mouse_face_beg_row
12815 || *hpos >= hlinfo->mouse_face_beg_col)
12816 && (*vpos < hlinfo->mouse_face_end_row
12817 || *hpos < hlinfo->mouse_face_end_col
12818 || hlinfo->mouse_face_past_end))
12819 return 0;
12821 return 1;
12825 /* EXPORT:
12826 Handle mouse button event on the tool-bar of frame F, at
12827 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12828 false for button release. MODIFIERS is event modifiers for button
12829 release. */
12831 void
12832 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12833 int modifiers)
12835 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12836 struct window *w = XWINDOW (f->tool_bar_window);
12837 int hpos, vpos, prop_idx;
12838 struct glyph *glyph;
12839 Lisp_Object enabled_p;
12840 int ts;
12842 /* If not on the highlighted tool-bar item, and mouse-highlight is
12843 non-nil, return. This is so we generate the tool-bar button
12844 click only when the mouse button is released on the same item as
12845 where it was pressed. However, when mouse-highlight is disabled,
12846 generate the click when the button is released regardless of the
12847 highlight, since tool-bar items are not highlighted in that
12848 case. */
12849 frame_to_window_pixel_xy (w, &x, &y);
12850 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12851 if (ts == -1
12852 || (ts != 0 && !NILP (Vmouse_highlight)))
12853 return;
12855 /* When mouse-highlight is off, generate the click for the item
12856 where the button was pressed, disregarding where it was
12857 released. */
12858 if (NILP (Vmouse_highlight) && !down_p)
12859 prop_idx = f->last_tool_bar_item;
12861 /* If item is disabled, do nothing. */
12862 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12863 if (NILP (enabled_p))
12864 return;
12866 if (down_p)
12868 /* Show item in pressed state. */
12869 if (!NILP (Vmouse_highlight))
12870 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12871 f->last_tool_bar_item = prop_idx;
12873 else
12875 Lisp_Object key, frame;
12876 struct input_event event;
12877 EVENT_INIT (event);
12879 /* Show item in released state. */
12880 if (!NILP (Vmouse_highlight))
12881 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12883 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12885 XSETFRAME (frame, f);
12886 event.kind = TOOL_BAR_EVENT;
12887 event.frame_or_window = frame;
12888 event.arg = frame;
12889 kbd_buffer_store_event (&event);
12891 event.kind = TOOL_BAR_EVENT;
12892 event.frame_or_window = frame;
12893 event.arg = key;
12894 event.modifiers = modifiers;
12895 kbd_buffer_store_event (&event);
12896 f->last_tool_bar_item = -1;
12901 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12902 tool-bar window-relative coordinates X/Y. Called from
12903 note_mouse_highlight. */
12905 static void
12906 note_tool_bar_highlight (struct frame *f, int x, int y)
12908 Lisp_Object window = f->tool_bar_window;
12909 struct window *w = XWINDOW (window);
12910 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12911 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12912 int hpos, vpos;
12913 struct glyph *glyph;
12914 struct glyph_row *row;
12915 int i;
12916 Lisp_Object enabled_p;
12917 int prop_idx;
12918 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12919 bool mouse_down_p;
12920 int rc;
12922 /* Function note_mouse_highlight is called with negative X/Y
12923 values when mouse moves outside of the frame. */
12924 if (x <= 0 || y <= 0)
12926 clear_mouse_face (hlinfo);
12927 return;
12930 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12931 if (rc < 0)
12933 /* Not on tool-bar item. */
12934 clear_mouse_face (hlinfo);
12935 return;
12937 else if (rc == 0)
12938 /* On same tool-bar item as before. */
12939 goto set_help_echo;
12941 clear_mouse_face (hlinfo);
12943 /* Mouse is down, but on different tool-bar item? */
12944 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12945 && f == dpyinfo->last_mouse_frame);
12947 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12948 return;
12950 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12952 /* If tool-bar item is not enabled, don't highlight it. */
12953 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12954 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12956 /* Compute the x-position of the glyph. In front and past the
12957 image is a space. We include this in the highlighted area. */
12958 row = MATRIX_ROW (w->current_matrix, vpos);
12959 for (i = x = 0; i < hpos; ++i)
12960 x += row->glyphs[TEXT_AREA][i].pixel_width;
12962 /* Record this as the current active region. */
12963 hlinfo->mouse_face_beg_col = hpos;
12964 hlinfo->mouse_face_beg_row = vpos;
12965 hlinfo->mouse_face_beg_x = x;
12966 hlinfo->mouse_face_past_end = false;
12968 hlinfo->mouse_face_end_col = hpos + 1;
12969 hlinfo->mouse_face_end_row = vpos;
12970 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12971 hlinfo->mouse_face_window = window;
12972 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12974 /* Display it as active. */
12975 show_mouse_face (hlinfo, draw);
12978 set_help_echo:
12980 /* Set help_echo_string to a help string to display for this tool-bar item.
12981 XTread_socket does the rest. */
12982 help_echo_object = help_echo_window = Qnil;
12983 help_echo_pos = -1;
12984 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12985 if (NILP (help_echo_string))
12986 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12989 #endif /* !USE_GTK && !HAVE_NS */
12991 #endif /* HAVE_WINDOW_SYSTEM */
12995 /************************************************************************
12996 Horizontal scrolling
12997 ************************************************************************/
12999 /* For all leaf windows in the window tree rooted at WINDOW, set their
13000 hscroll value so that PT is (i) visible in the window, and (ii) so
13001 that it is not within a certain margin at the window's left and
13002 right border. Value is true if any window's hscroll has been
13003 changed. */
13005 static bool
13006 hscroll_window_tree (Lisp_Object window)
13008 bool hscrolled_p = false;
13009 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13010 int hscroll_step_abs = 0;
13011 double hscroll_step_rel = 0;
13013 if (hscroll_relative_p)
13015 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13016 if (hscroll_step_rel < 0)
13018 hscroll_relative_p = false;
13019 hscroll_step_abs = 0;
13022 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13024 hscroll_step_abs = XINT (Vhscroll_step);
13025 if (hscroll_step_abs < 0)
13026 hscroll_step_abs = 0;
13028 else
13029 hscroll_step_abs = 0;
13031 while (WINDOWP (window))
13033 struct window *w = XWINDOW (window);
13035 if (WINDOWP (w->contents))
13036 hscrolled_p |= hscroll_window_tree (w->contents);
13037 else if (w->cursor.vpos >= 0)
13039 int h_margin;
13040 int text_area_width;
13041 struct glyph_row *cursor_row;
13042 struct glyph_row *bottom_row;
13044 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13045 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13046 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13047 else
13048 cursor_row = bottom_row - 1;
13050 if (!cursor_row->enabled_p)
13052 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13053 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13054 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13055 else
13056 cursor_row = bottom_row - 1;
13058 bool row_r2l_p = cursor_row->reversed_p;
13059 bool hscl = hscrolling_current_line_p (w);
13061 text_area_width = window_box_width (w, TEXT_AREA);
13063 /* Scroll when cursor is inside this scroll margin. */
13064 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13066 /* If the position of this window's point has explicitly
13067 changed, no more suspend auto hscrolling. */
13068 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13069 w->suspend_auto_hscroll = false;
13071 /* Remember window point. */
13072 Fset_marker (w->old_pointm,
13073 ((w == XWINDOW (selected_window))
13074 ? make_number (BUF_PT (XBUFFER (w->contents)))
13075 : Fmarker_position (w->pointm)),
13076 w->contents);
13078 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13079 && !w->suspend_auto_hscroll
13080 /* In some pathological cases, like restoring a window
13081 configuration into a frame that is much smaller than
13082 the one from which the configuration was saved, we
13083 get glyph rows whose start and end have zero buffer
13084 positions, which we cannot handle below. Just skip
13085 such windows. */
13086 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13087 /* For left-to-right rows, hscroll when cursor is either
13088 (i) inside the right hscroll margin, or (ii) if it is
13089 inside the left margin and the window is already
13090 hscrolled. */
13091 && ((!row_r2l_p
13092 && ((w->hscroll && w->cursor.x <= h_margin)
13093 || (cursor_row->enabled_p
13094 && cursor_row->truncated_on_right_p
13095 && (w->cursor.x >= text_area_width - h_margin))))
13096 /* For right-to-left rows, the logic is similar,
13097 except that rules for scrolling to left and right
13098 are reversed. E.g., if cursor.x <= h_margin, we
13099 need to hscroll "to the right" unconditionally,
13100 and that will scroll the screen to the left so as
13101 to reveal the next portion of the row. */
13102 || (row_r2l_p
13103 && ((cursor_row->enabled_p
13104 /* FIXME: It is confusing to set the
13105 truncated_on_right_p flag when R2L rows
13106 are actually truncated on the left. */
13107 && cursor_row->truncated_on_right_p
13108 && w->cursor.x <= h_margin)
13109 || (w->hscroll
13110 && (w->cursor.x >= text_area_width - h_margin))))
13111 /* This last condition is needed when moving
13112 vertically from an hscrolled line to a short line
13113 that doesn't need to be hscrolled. If we omit
13114 this condition, the line from which we move will
13115 remain hscrolled. */
13116 || (hscl
13117 && w->hscroll != w->min_hscroll
13118 && !cursor_row->truncated_on_left_p)))
13120 struct it it;
13121 ptrdiff_t hscroll;
13122 struct buffer *saved_current_buffer;
13123 ptrdiff_t pt;
13124 int wanted_x;
13126 /* Find point in a display of infinite width. */
13127 saved_current_buffer = current_buffer;
13128 current_buffer = XBUFFER (w->contents);
13130 if (w == XWINDOW (selected_window))
13131 pt = PT;
13132 else
13133 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13135 /* Move iterator to pt starting at cursor_row->start in
13136 a line with infinite width. */
13137 init_to_row_start (&it, w, cursor_row);
13138 if (hscl)
13139 it.first_visible_x = window_hscroll_limited (w, it.f)
13140 * FRAME_COLUMN_WIDTH (it.f);
13141 it.last_visible_x = INFINITY;
13142 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13143 /* If the line ends in an overlay string with a newline,
13144 we might infloop, because displaying the window will
13145 want to put the cursor after the overlay, i.e. at X
13146 coordinate of zero on the next screen line. So we
13147 use the buffer position prior to the overlay string
13148 instead. */
13149 if (it.method == GET_FROM_STRING && pt > 1)
13151 init_to_row_start (&it, w, cursor_row);
13152 if (hscl)
13153 it.first_visible_x = (window_hscroll_limited (w, it.f)
13154 * FRAME_COLUMN_WIDTH (it.f));
13155 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13157 current_buffer = saved_current_buffer;
13159 /* Position cursor in window. */
13160 if (!hscroll_relative_p && hscroll_step_abs == 0)
13161 hscroll = max (0, (it.current_x
13162 - (ITERATOR_AT_END_OF_LINE_P (&it)
13163 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13164 : (text_area_width / 2))))
13165 / FRAME_COLUMN_WIDTH (it.f);
13166 else if ((!row_r2l_p
13167 && w->cursor.x >= text_area_width - h_margin)
13168 || (row_r2l_p && w->cursor.x <= h_margin))
13170 if (hscroll_relative_p)
13171 wanted_x = text_area_width * (1 - hscroll_step_rel)
13172 - h_margin;
13173 else
13174 wanted_x = text_area_width
13175 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13176 - h_margin;
13177 hscroll
13178 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13180 else
13182 if (hscroll_relative_p)
13183 wanted_x = text_area_width * hscroll_step_rel
13184 + h_margin;
13185 else
13186 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13187 + h_margin;
13188 hscroll
13189 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13191 hscroll = max (hscroll, w->min_hscroll);
13193 /* Don't prevent redisplay optimizations if hscroll
13194 hasn't changed, as it will unnecessarily slow down
13195 redisplay. */
13196 if (w->hscroll != hscroll
13197 /* When hscrolling only the current line, we need to
13198 report hscroll even if its value is equal to the
13199 previous one, because the new line might need a
13200 different value. */
13201 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13203 struct buffer *b = XBUFFER (w->contents);
13204 b->prevent_redisplay_optimizations_p = true;
13205 w->hscroll = hscroll;
13206 hscrolled_p = true;
13211 window = w->next;
13214 /* Value is true if hscroll of any leaf window has been changed. */
13215 return hscrolled_p;
13219 /* Set hscroll so that cursor is visible and not inside horizontal
13220 scroll margins for all windows in the tree rooted at WINDOW. See
13221 also hscroll_window_tree above. Value is true if any window's
13222 hscroll has been changed. If it has, desired matrices on the frame
13223 of WINDOW are cleared. */
13225 static bool
13226 hscroll_windows (Lisp_Object window)
13228 bool hscrolled_p = hscroll_window_tree (window);
13229 if (hscrolled_p)
13230 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13231 return hscrolled_p;
13236 /************************************************************************
13237 Redisplay
13238 ************************************************************************/
13240 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13241 This is sometimes handy to have in a debugger session. */
13243 #ifdef GLYPH_DEBUG
13245 /* First and last unchanged row for try_window_id. */
13247 static int debug_first_unchanged_at_end_vpos;
13248 static int debug_last_unchanged_at_beg_vpos;
13250 /* Delta vpos and y. */
13252 static int debug_dvpos, debug_dy;
13254 /* Delta in characters and bytes for try_window_id. */
13256 static ptrdiff_t debug_delta, debug_delta_bytes;
13258 /* Values of window_end_pos and window_end_vpos at the end of
13259 try_window_id. */
13261 static ptrdiff_t debug_end_vpos;
13263 /* Append a string to W->desired_matrix->method. FMT is a printf
13264 format string. If trace_redisplay_p is true also printf the
13265 resulting string to stderr. */
13267 static void debug_method_add (struct window *, char const *, ...)
13268 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13270 static void
13271 debug_method_add (struct window *w, char const *fmt, ...)
13273 void *ptr = w;
13274 char *method = w->desired_matrix->method;
13275 int len = strlen (method);
13276 int size = sizeof w->desired_matrix->method;
13277 int remaining = size - len - 1;
13278 va_list ap;
13280 if (len && remaining)
13282 method[len] = '|';
13283 --remaining, ++len;
13286 va_start (ap, fmt);
13287 vsnprintf (method + len, remaining + 1, fmt, ap);
13288 va_end (ap);
13290 if (trace_redisplay_p)
13291 fprintf (stderr, "%p (%s): %s\n",
13292 ptr,
13293 ((BUFFERP (w->contents)
13294 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13295 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13296 : "no buffer"),
13297 method + len);
13300 #endif /* GLYPH_DEBUG */
13303 /* Value is true if all changes in window W, which displays
13304 current_buffer, are in the text between START and END. START is a
13305 buffer position, END is given as a distance from Z. Used in
13306 redisplay_internal for display optimization. */
13308 static bool
13309 text_outside_line_unchanged_p (struct window *w,
13310 ptrdiff_t start, ptrdiff_t end)
13312 bool unchanged_p = true;
13314 /* If text or overlays have changed, see where. */
13315 if (window_outdated (w))
13317 /* Gap in the line? */
13318 if (GPT < start || Z - GPT < end)
13319 unchanged_p = false;
13321 /* Changes start in front of the line, or end after it? */
13322 if (unchanged_p
13323 && (BEG_UNCHANGED < start - 1
13324 || END_UNCHANGED < end))
13325 unchanged_p = false;
13327 /* If selective display, can't optimize if changes start at the
13328 beginning of the line. */
13329 if (unchanged_p
13330 && INTEGERP (BVAR (current_buffer, selective_display))
13331 && XINT (BVAR (current_buffer, selective_display)) > 0
13332 && (BEG_UNCHANGED < start || GPT <= start))
13333 unchanged_p = false;
13335 /* If there are overlays at the start or end of the line, these
13336 may have overlay strings with newlines in them. A change at
13337 START, for instance, may actually concern the display of such
13338 overlay strings as well, and they are displayed on different
13339 lines. So, quickly rule out this case. (For the future, it
13340 might be desirable to implement something more telling than
13341 just BEG/END_UNCHANGED.) */
13342 if (unchanged_p)
13344 if (BEG + BEG_UNCHANGED == start
13345 && overlay_touches_p (start))
13346 unchanged_p = false;
13347 if (END_UNCHANGED == end
13348 && overlay_touches_p (Z - end))
13349 unchanged_p = false;
13352 /* Under bidi reordering, adding or deleting a character in the
13353 beginning of a paragraph, before the first strong directional
13354 character, can change the base direction of the paragraph (unless
13355 the buffer specifies a fixed paragraph direction), which will
13356 require redisplaying the whole paragraph. It might be worthwhile
13357 to find the paragraph limits and widen the range of redisplayed
13358 lines to that, but for now just give up this optimization. */
13359 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13360 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13361 unchanged_p = false;
13364 return unchanged_p;
13368 /* Do a frame update, taking possible shortcuts into account. This is
13369 the main external entry point for redisplay.
13371 If the last redisplay displayed an echo area message and that message
13372 is no longer requested, we clear the echo area or bring back the
13373 mini-buffer if that is in use. */
13375 void
13376 redisplay (void)
13378 redisplay_internal ();
13382 static Lisp_Object
13383 overlay_arrow_string_or_property (Lisp_Object var)
13385 Lisp_Object val;
13387 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13388 return val;
13390 return Voverlay_arrow_string;
13393 /* Return true if there are any overlay-arrows in current_buffer. */
13394 static bool
13395 overlay_arrow_in_current_buffer_p (void)
13397 Lisp_Object vlist;
13399 for (vlist = Voverlay_arrow_variable_list;
13400 CONSP (vlist);
13401 vlist = XCDR (vlist))
13403 Lisp_Object var = XCAR (vlist);
13404 Lisp_Object val;
13406 if (!SYMBOLP (var))
13407 continue;
13408 val = find_symbol_value (var);
13409 if (MARKERP (val)
13410 && current_buffer == XMARKER (val)->buffer)
13411 return true;
13413 return false;
13417 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13418 has changed.
13419 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13420 buffers that are affected. */
13422 static bool
13423 overlay_arrows_changed_p (bool set_redisplay)
13425 Lisp_Object vlist;
13426 bool changed = false;
13428 for (vlist = Voverlay_arrow_variable_list;
13429 CONSP (vlist);
13430 vlist = XCDR (vlist))
13432 Lisp_Object var = XCAR (vlist);
13433 Lisp_Object val, pstr;
13435 if (!SYMBOLP (var))
13436 continue;
13437 val = find_symbol_value (var);
13438 if (!MARKERP (val))
13439 continue;
13440 if (! EQ (COERCE_MARKER (val),
13441 /* FIXME: Don't we have a problem, using such a global
13442 * "last-position" if the variable is buffer-local? */
13443 Fget (var, Qlast_arrow_position))
13444 || ! (pstr = overlay_arrow_string_or_property (var),
13445 EQ (pstr, Fget (var, Qlast_arrow_string))))
13447 struct buffer *buf = XMARKER (val)->buffer;
13449 if (set_redisplay)
13451 if (buf)
13452 bset_redisplay (buf);
13453 changed = true;
13455 else
13456 return true;
13459 return changed;
13462 /* Mark overlay arrows to be updated on next redisplay. */
13464 static void
13465 update_overlay_arrows (int up_to_date)
13467 Lisp_Object vlist;
13469 for (vlist = Voverlay_arrow_variable_list;
13470 CONSP (vlist);
13471 vlist = XCDR (vlist))
13473 Lisp_Object var = XCAR (vlist);
13475 if (!SYMBOLP (var))
13476 continue;
13478 if (up_to_date > 0)
13480 Lisp_Object val = find_symbol_value (var);
13481 if (!MARKERP (val))
13482 continue;
13483 Fput (var, Qlast_arrow_position,
13484 COERCE_MARKER (val));
13485 Fput (var, Qlast_arrow_string,
13486 overlay_arrow_string_or_property (var));
13488 else if (up_to_date < 0
13489 || !NILP (Fget (var, Qlast_arrow_position)))
13491 Fput (var, Qlast_arrow_position, Qt);
13492 Fput (var, Qlast_arrow_string, Qt);
13498 /* Return overlay arrow string to display at row.
13499 Return integer (bitmap number) for arrow bitmap in left fringe.
13500 Return nil if no overlay arrow. */
13502 static Lisp_Object
13503 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13505 Lisp_Object vlist;
13507 for (vlist = Voverlay_arrow_variable_list;
13508 CONSP (vlist);
13509 vlist = XCDR (vlist))
13511 Lisp_Object var = XCAR (vlist);
13512 Lisp_Object val;
13514 if (!SYMBOLP (var))
13515 continue;
13517 val = find_symbol_value (var);
13519 if (MARKERP (val)
13520 && current_buffer == XMARKER (val)->buffer
13521 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13523 if (FRAME_WINDOW_P (it->f)
13524 /* FIXME: if ROW->reversed_p is set, this should test
13525 the right fringe, not the left one. */
13526 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13528 #ifdef HAVE_WINDOW_SYSTEM
13529 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13531 int fringe_bitmap = lookup_fringe_bitmap (val);
13532 if (fringe_bitmap != 0)
13533 return make_number (fringe_bitmap);
13535 #endif
13536 return make_number (-1); /* Use default arrow bitmap. */
13538 return overlay_arrow_string_or_property (var);
13542 return Qnil;
13545 /* Return true if point moved out of or into a composition. Otherwise
13546 return false. PREV_BUF and PREV_PT are the last point buffer and
13547 position. BUF and PT are the current point buffer and position. */
13549 static bool
13550 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13551 struct buffer *buf, ptrdiff_t pt)
13553 ptrdiff_t start, end;
13554 Lisp_Object prop;
13555 Lisp_Object buffer;
13557 XSETBUFFER (buffer, buf);
13558 /* Check a composition at the last point if point moved within the
13559 same buffer. */
13560 if (prev_buf == buf)
13562 if (prev_pt == pt)
13563 /* Point didn't move. */
13564 return false;
13566 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13567 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13568 && composition_valid_p (start, end, prop)
13569 && start < prev_pt && end > prev_pt)
13570 /* The last point was within the composition. Return true iff
13571 point moved out of the composition. */
13572 return (pt <= start || pt >= end);
13575 /* Check a composition at the current point. */
13576 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13577 && find_composition (pt, -1, &start, &end, &prop, buffer)
13578 && composition_valid_p (start, end, prop)
13579 && start < pt && end > pt);
13582 /* Reconsider the clip changes of buffer which is displayed in W. */
13584 static void
13585 reconsider_clip_changes (struct window *w)
13587 struct buffer *b = XBUFFER (w->contents);
13589 if (b->clip_changed
13590 && w->window_end_valid
13591 && w->current_matrix->buffer == b
13592 && w->current_matrix->zv == BUF_ZV (b)
13593 && w->current_matrix->begv == BUF_BEGV (b))
13594 b->clip_changed = false;
13596 /* If display wasn't paused, and W is not a tool bar window, see if
13597 point has been moved into or out of a composition. In that case,
13598 set b->clip_changed to force updating the screen. If
13599 b->clip_changed has already been set, skip this check. */
13600 if (!b->clip_changed && w->window_end_valid)
13602 ptrdiff_t pt = (w == XWINDOW (selected_window)
13603 ? PT : marker_position (w->pointm));
13605 if ((w->current_matrix->buffer != b || pt != w->last_point)
13606 && check_point_in_composition (w->current_matrix->buffer,
13607 w->last_point, b, pt))
13608 b->clip_changed = true;
13612 static void
13613 propagate_buffer_redisplay (void)
13614 { /* Resetting b->text->redisplay is problematic!
13615 We can't just reset it in the case that some window that displays
13616 it has not been redisplayed; and such a window can stay
13617 unredisplayed for a long time if it's currently invisible.
13618 But we do want to reset it at the end of redisplay otherwise
13619 its displayed windows will keep being redisplayed over and over
13620 again.
13621 So we copy all b->text->redisplay flags up to their windows here,
13622 such that mark_window_display_accurate can safely reset
13623 b->text->redisplay. */
13624 Lisp_Object ws = window_list ();
13625 for (; CONSP (ws); ws = XCDR (ws))
13627 struct window *thisw = XWINDOW (XCAR (ws));
13628 struct buffer *thisb = XBUFFER (thisw->contents);
13629 if (thisb->text->redisplay)
13630 thisw->redisplay = true;
13634 #define STOP_POLLING \
13635 do { if (! polling_stopped_here) stop_polling (); \
13636 polling_stopped_here = true; } while (false)
13638 #define RESUME_POLLING \
13639 do { if (polling_stopped_here) start_polling (); \
13640 polling_stopped_here = false; } while (false)
13643 /* Perhaps in the future avoid recentering windows if it
13644 is not necessary; currently that causes some problems. */
13646 static void
13647 redisplay_internal (void)
13649 struct window *w = XWINDOW (selected_window);
13650 struct window *sw;
13651 struct frame *fr;
13652 bool pending;
13653 bool must_finish = false, match_p;
13654 struct text_pos tlbufpos, tlendpos;
13655 int number_of_visible_frames;
13656 ptrdiff_t count;
13657 struct frame *sf;
13658 bool polling_stopped_here = false;
13659 Lisp_Object tail, frame;
13661 /* Set a limit to the number of retries we perform due to horizontal
13662 scrolling, this avoids getting stuck in an uninterruptible
13663 infinite loop (Bug #24633). */
13664 enum { MAX_HSCROLL_RETRIES = 16 };
13665 int hscroll_retries = 0;
13667 /* Limit the number of retries for when frame(s) become garbaged as
13668 result of redisplaying them. Some packages set various redisplay
13669 hooks, such as window-scroll-functions, to run Lisp that always
13670 calls APIs which cause the frame's garbaged flag to become set,
13671 so we loop indefinitely. */
13672 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13673 int garbaged_frame_retries = 0;
13675 /* True means redisplay has to consider all windows on all
13676 frames. False, only selected_window is considered. */
13677 bool consider_all_windows_p;
13679 /* True means redisplay has to redisplay the miniwindow. */
13680 bool update_miniwindow_p = false;
13682 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13684 /* No redisplay if running in batch mode or frame is not yet fully
13685 initialized, or redisplay is explicitly turned off by setting
13686 Vinhibit_redisplay. */
13687 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13688 || !NILP (Vinhibit_redisplay))
13689 return;
13691 /* Don't examine these until after testing Vinhibit_redisplay.
13692 When Emacs is shutting down, perhaps because its connection to
13693 X has dropped, we should not look at them at all. */
13694 fr = XFRAME (w->frame);
13695 sf = SELECTED_FRAME ();
13697 if (!fr->glyphs_initialized_p)
13698 return;
13700 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13701 if (popup_activated ())
13702 return;
13703 #endif
13705 /* I don't think this happens but let's be paranoid. */
13706 if (redisplaying_p)
13707 return;
13709 /* Record a function that clears redisplaying_p
13710 when we leave this function. */
13711 count = SPECPDL_INDEX ();
13712 record_unwind_protect_void (unwind_redisplay);
13713 redisplaying_p = true;
13714 block_buffer_flips ();
13715 specbind (Qinhibit_free_realized_faces, Qnil);
13717 /* Record this function, so it appears on the profiler's backtraces. */
13718 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13720 FOR_EACH_FRAME (tail, frame)
13721 XFRAME (frame)->already_hscrolled_p = false;
13723 retry:
13724 /* Remember the currently selected window. */
13725 sw = w;
13727 pending = false;
13728 forget_escape_and_glyphless_faces ();
13730 inhibit_free_realized_faces = false;
13732 /* If face_change, init_iterator will free all realized faces, which
13733 includes the faces referenced from current matrices. So, we
13734 can't reuse current matrices in this case. */
13735 if (face_change)
13736 windows_or_buffers_changed = 47;
13738 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13739 && FRAME_TTY (sf)->previous_frame != sf)
13741 /* Since frames on a single ASCII terminal share the same
13742 display area, displaying a different frame means redisplay
13743 the whole thing. */
13744 SET_FRAME_GARBAGED (sf);
13745 #ifndef DOS_NT
13746 set_tty_color_mode (FRAME_TTY (sf), sf);
13747 #endif
13748 FRAME_TTY (sf)->previous_frame = sf;
13751 /* Set the visible flags for all frames. Do this before checking for
13752 resized or garbaged frames; they want to know if their frames are
13753 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13754 number_of_visible_frames = 0;
13756 FOR_EACH_FRAME (tail, frame)
13758 struct frame *f = XFRAME (frame);
13760 if (FRAME_VISIBLE_P (f))
13762 ++number_of_visible_frames;
13763 /* Adjust matrices for visible frames only. */
13764 if (f->fonts_changed)
13766 adjust_frame_glyphs (f);
13767 /* Disable all redisplay optimizations for this frame.
13768 This is because adjust_frame_glyphs resets the
13769 enabled_p flag for all glyph rows of all windows, so
13770 many optimizations will fail anyway, and some might
13771 fail to test that flag and do bogus things as
13772 result. */
13773 SET_FRAME_GARBAGED (f);
13774 f->fonts_changed = false;
13776 /* If cursor type has been changed on the frame
13777 other than selected, consider all frames. */
13778 if (f != sf && f->cursor_type_changed)
13779 fset_redisplay (f);
13781 clear_desired_matrices (f);
13784 /* Notice any pending interrupt request to change frame size. */
13785 do_pending_window_change (true);
13787 /* do_pending_window_change could change the selected_window due to
13788 frame resizing which makes the selected window too small. */
13789 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13790 sw = w;
13792 /* Clear frames marked as garbaged. */
13793 clear_garbaged_frames ();
13795 /* Build menubar and tool-bar items. */
13796 if (NILP (Vmemory_full))
13797 prepare_menu_bars ();
13799 reconsider_clip_changes (w);
13801 /* In most cases selected window displays current buffer. */
13802 match_p = XBUFFER (w->contents) == current_buffer;
13803 if (match_p)
13805 /* Detect case that we need to write or remove a star in the mode line. */
13806 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13807 w->update_mode_line = true;
13809 if (mode_line_update_needed (w))
13810 w->update_mode_line = true;
13812 /* If reconsider_clip_changes above decided that the narrowing
13813 in the current buffer changed, make sure all other windows
13814 showing that buffer will be redisplayed. */
13815 if (current_buffer->clip_changed)
13816 bset_update_mode_line (current_buffer);
13819 /* Normally the message* functions will have already displayed and
13820 updated the echo area, but the frame may have been trashed, or
13821 the update may have been preempted, so display the echo area
13822 again here. Checking message_cleared_p captures the case that
13823 the echo area should be cleared. */
13824 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13825 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13826 || (message_cleared_p
13827 && minibuf_level == 0
13828 /* If the mini-window is currently selected, this means the
13829 echo-area doesn't show through. */
13830 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13832 echo_area_display (false);
13834 /* If echo_area_display resizes the mini-window, the redisplay and
13835 window_sizes_changed flags of the selected frame are set, but
13836 it's too late for the hooks in window-size-change-functions,
13837 which have been examined already in prepare_menu_bars. So in
13838 that case we call the hooks here only for the selected frame. */
13839 if (sf->redisplay)
13841 ptrdiff_t count1 = SPECPDL_INDEX ();
13843 record_unwind_save_match_data ();
13844 run_window_size_change_functions (selected_frame);
13845 unbind_to (count1, Qnil);
13848 if (message_cleared_p)
13849 update_miniwindow_p = true;
13851 must_finish = true;
13853 /* If we don't display the current message, don't clear the
13854 message_cleared_p flag, because, if we did, we wouldn't clear
13855 the echo area in the next redisplay which doesn't preserve
13856 the echo area. */
13857 if (!display_last_displayed_message_p)
13858 message_cleared_p = false;
13860 else if (EQ (selected_window, minibuf_window)
13861 && (current_buffer->clip_changed || window_outdated (w))
13862 && resize_mini_window (w, false))
13864 if (sf->redisplay)
13866 ptrdiff_t count1 = SPECPDL_INDEX ();
13868 record_unwind_save_match_data ();
13869 run_window_size_change_functions (selected_frame);
13870 unbind_to (count1, Qnil);
13873 /* Resized active mini-window to fit the size of what it is
13874 showing if its contents might have changed. */
13875 must_finish = true;
13877 /* If window configuration was changed, frames may have been
13878 marked garbaged. Clear them or we will experience
13879 surprises wrt scrolling. */
13880 clear_garbaged_frames ();
13883 if (windows_or_buffers_changed && !update_mode_lines)
13884 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13885 only the windows's contents needs to be refreshed, or whether the
13886 mode-lines also need a refresh. */
13887 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13888 ? REDISPLAY_SOME : 32);
13890 /* If specs for an arrow have changed, do thorough redisplay
13891 to ensure we remove any arrow that should no longer exist. */
13892 /* Apparently, this is the only case where we update other windows,
13893 without updating other mode-lines. */
13894 overlay_arrows_changed_p (true);
13896 consider_all_windows_p = (update_mode_lines
13897 || windows_or_buffers_changed);
13899 #define AINC(a,i) \
13901 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13902 if (INTEGERP (entry)) \
13903 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13906 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13907 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13909 /* Optimize the case that only the line containing the cursor in the
13910 selected window has changed. Variables starting with this_ are
13911 set in display_line and record information about the line
13912 containing the cursor. */
13913 tlbufpos = this_line_start_pos;
13914 tlendpos = this_line_end_pos;
13915 if (!consider_all_windows_p
13916 && CHARPOS (tlbufpos) > 0
13917 && !w->update_mode_line
13918 && !current_buffer->clip_changed
13919 && !current_buffer->prevent_redisplay_optimizations_p
13920 && FRAME_VISIBLE_P (XFRAME (w->frame))
13921 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13922 && !XFRAME (w->frame)->cursor_type_changed
13923 && !XFRAME (w->frame)->face_change
13924 /* Make sure recorded data applies to current buffer, etc. */
13925 && this_line_buffer == current_buffer
13926 && match_p
13927 && !w->force_start
13928 && !w->optional_new_start
13929 /* Point must be on the line that we have info recorded about. */
13930 && PT >= CHARPOS (tlbufpos)
13931 && PT <= Z - CHARPOS (tlendpos)
13932 /* All text outside that line, including its final newline,
13933 must be unchanged. */
13934 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13935 CHARPOS (tlendpos)))
13937 if (CHARPOS (tlbufpos) > BEGV
13938 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13939 && (CHARPOS (tlbufpos) == ZV
13940 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13941 /* Former continuation line has disappeared by becoming empty. */
13942 goto cancel;
13943 else if (window_outdated (w) || MINI_WINDOW_P (w))
13945 /* We have to handle the case of continuation around a
13946 wide-column character (see the comment in indent.c around
13947 line 1340).
13949 For instance, in the following case:
13951 -------- Insert --------
13952 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13953 J_I_ ==> J_I_ `^^' are cursors.
13954 ^^ ^^
13955 -------- --------
13957 As we have to redraw the line above, we cannot use this
13958 optimization. */
13960 struct it it;
13961 int line_height_before = this_line_pixel_height;
13963 /* Note that start_display will handle the case that the
13964 line starting at tlbufpos is a continuation line. */
13965 start_display (&it, w, tlbufpos);
13967 /* Implementation note: It this still necessary? */
13968 if (it.current_x != this_line_start_x)
13969 goto cancel;
13971 TRACE ((stderr, "trying display optimization 1\n"));
13972 w->cursor.vpos = -1;
13973 overlay_arrow_seen = false;
13974 it.vpos = this_line_vpos;
13975 it.current_y = this_line_y;
13976 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13977 display_line (&it, -1);
13979 /* If line contains point, is not continued,
13980 and ends at same distance from eob as before, we win. */
13981 if (w->cursor.vpos >= 0
13982 /* Line is not continued, otherwise this_line_start_pos
13983 would have been set to 0 in display_line. */
13984 && CHARPOS (this_line_start_pos)
13985 /* Line ends as before. */
13986 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13987 /* Line has same height as before. Otherwise other lines
13988 would have to be shifted up or down. */
13989 && this_line_pixel_height == line_height_before)
13991 /* If this is not the window's last line, we must adjust
13992 the charstarts of the lines below. */
13993 if (it.current_y < it.last_visible_y)
13995 struct glyph_row *row
13996 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13997 ptrdiff_t delta, delta_bytes;
13999 /* We used to distinguish between two cases here,
14000 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14001 when the line ends in a newline or the end of the
14002 buffer's accessible portion. But both cases did
14003 the same, so they were collapsed. */
14004 delta = (Z
14005 - CHARPOS (tlendpos)
14006 - MATRIX_ROW_START_CHARPOS (row));
14007 delta_bytes = (Z_BYTE
14008 - BYTEPOS (tlendpos)
14009 - MATRIX_ROW_START_BYTEPOS (row));
14011 increment_matrix_positions (w->current_matrix,
14012 this_line_vpos + 1,
14013 w->current_matrix->nrows,
14014 delta, delta_bytes);
14017 /* If this row displays text now but previously didn't,
14018 or vice versa, w->window_end_vpos may have to be
14019 adjusted. */
14020 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14022 if (w->window_end_vpos < this_line_vpos)
14023 w->window_end_vpos = this_line_vpos;
14025 else if (w->window_end_vpos == this_line_vpos
14026 && this_line_vpos > 0)
14027 w->window_end_vpos = this_line_vpos - 1;
14028 w->window_end_valid = false;
14030 /* Update hint: No need to try to scroll in update_window. */
14031 w->desired_matrix->no_scrolling_p = true;
14033 #ifdef GLYPH_DEBUG
14034 *w->desired_matrix->method = 0;
14035 debug_method_add (w, "optimization 1");
14036 #endif
14037 #ifdef HAVE_WINDOW_SYSTEM
14038 update_window_fringes (w, false);
14039 #endif
14040 goto update;
14042 else
14043 goto cancel;
14045 else if (/* Cursor position hasn't changed. */
14046 PT == w->last_point
14047 /* Make sure the cursor was last displayed
14048 in this window. Otherwise we have to reposition it. */
14050 /* PXW: Must be converted to pixels, probably. */
14051 && 0 <= w->cursor.vpos
14052 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14054 if (!must_finish)
14056 do_pending_window_change (true);
14057 /* If selected_window changed, redisplay again. */
14058 if (WINDOWP (selected_window)
14059 && (w = XWINDOW (selected_window)) != sw)
14060 goto retry;
14062 /* We used to always goto end_of_redisplay here, but this
14063 isn't enough if we have a blinking cursor. */
14064 if (w->cursor_off_p == w->last_cursor_off_p)
14065 goto end_of_redisplay;
14067 goto update;
14069 /* If highlighting the region, or if the cursor is in the echo area,
14070 then we can't just move the cursor. */
14071 else if (NILP (Vshow_trailing_whitespace)
14072 && !cursor_in_echo_area)
14074 struct it it;
14075 struct glyph_row *row;
14077 /* Skip from tlbufpos to PT and see where it is. Note that
14078 PT may be in invisible text. If so, we will end at the
14079 next visible position. */
14080 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14081 NULL, DEFAULT_FACE_ID);
14082 it.current_x = this_line_start_x;
14083 it.current_y = this_line_y;
14084 it.vpos = this_line_vpos;
14086 /* The call to move_it_to stops in front of PT, but
14087 moves over before-strings. */
14088 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14090 if (it.vpos == this_line_vpos
14091 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14092 row->enabled_p))
14094 eassert (this_line_vpos == it.vpos);
14095 eassert (this_line_y == it.current_y);
14096 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14097 if (cursor_row_fully_visible_p (w, false, true))
14099 #ifdef GLYPH_DEBUG
14100 *w->desired_matrix->method = 0;
14101 debug_method_add (w, "optimization 3");
14102 #endif
14103 goto update;
14105 else
14106 goto cancel;
14108 else
14109 goto cancel;
14112 cancel:
14113 /* Text changed drastically or point moved off of line. */
14114 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14117 CHARPOS (this_line_start_pos) = 0;
14118 ++clear_face_cache_count;
14119 #ifdef HAVE_WINDOW_SYSTEM
14120 ++clear_image_cache_count;
14121 #endif
14123 /* Build desired matrices, and update the display. If
14124 consider_all_windows_p, do it for all windows on all frames that
14125 require redisplay, as specified by their 'redisplay' flag.
14126 Otherwise do it for selected_window, only. */
14128 if (consider_all_windows_p)
14130 FOR_EACH_FRAME (tail, frame)
14131 XFRAME (frame)->updated_p = false;
14133 propagate_buffer_redisplay ();
14135 FOR_EACH_FRAME (tail, frame)
14137 struct frame *f = XFRAME (frame);
14139 /* We don't have to do anything for unselected terminal
14140 frames. */
14141 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14142 && !EQ (FRAME_TTY (f)->top_frame, frame))
14143 continue;
14145 retry_frame:
14146 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14148 bool gcscrollbars
14149 /* Only GC scrollbars when we redisplay the whole frame. */
14150 = f->redisplay || !REDISPLAY_SOME_P ();
14151 bool f_redisplay_flag = f->redisplay;
14152 /* Mark all the scroll bars to be removed; we'll redeem
14153 the ones we want when we redisplay their windows. */
14154 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14155 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14157 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14158 redisplay_windows (FRAME_ROOT_WINDOW (f));
14159 /* Remember that the invisible frames need to be redisplayed next
14160 time they're visible. */
14161 else if (!REDISPLAY_SOME_P ())
14162 f->redisplay = true;
14164 /* The X error handler may have deleted that frame. */
14165 if (!FRAME_LIVE_P (f))
14166 continue;
14168 /* Any scroll bars which redisplay_windows should have
14169 nuked should now go away. */
14170 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14171 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14173 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14175 /* If fonts changed on visible frame, display again. */
14176 if (f->fonts_changed)
14178 adjust_frame_glyphs (f);
14179 /* Disable all redisplay optimizations for this
14180 frame. For the reasons, see the comment near
14181 the previous call to adjust_frame_glyphs above. */
14182 SET_FRAME_GARBAGED (f);
14183 f->fonts_changed = false;
14184 goto retry_frame;
14187 /* See if we have to hscroll. */
14188 if (!f->already_hscrolled_p)
14190 f->already_hscrolled_p = true;
14191 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14192 && hscroll_windows (f->root_window))
14194 hscroll_retries++;
14195 goto retry_frame;
14199 /* If the frame's redisplay flag was not set before
14200 we went about redisplaying its windows, but it is
14201 set now, that means we employed some redisplay
14202 optimizations inside redisplay_windows, and
14203 bypassed producing some screen lines. But if
14204 f->redisplay is now set, it might mean the old
14205 faces are no longer valid (e.g., if redisplaying
14206 some window called some Lisp which defined a new
14207 face or redefined an existing face), so trying to
14208 use them in update_frame will segfault.
14209 Therefore, we must redisplay this frame. */
14210 if (!f_redisplay_flag && f->redisplay)
14211 goto retry_frame;
14212 /* In some case (e.g., window resize), we notice
14213 only during window updating that the window
14214 content changed unpredictably (e.g., a GTK
14215 scrollbar moved, or some Lisp hook that winds up
14216 calling adjust_frame_glyphs) and that our
14217 previous estimation of the frame content was
14218 garbage. We have to start over. These cases
14219 should be rare, so going all the way back to the
14220 top of redisplay should be good enough. */
14221 if (FRAME_GARBAGED_P (f)
14222 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14223 goto retry;
14225 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14226 x_clear_under_internal_border (f);
14227 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14229 /* Prevent various kinds of signals during display
14230 update. stdio is not robust about handling
14231 signals, which can cause an apparent I/O error. */
14232 if (interrupt_input)
14233 unrequest_sigio ();
14234 STOP_POLLING;
14236 pending |= update_frame (f, false, false);
14237 f->cursor_type_changed = false;
14238 f->updated_p = true;
14243 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14245 if (!pending)
14247 /* Do the mark_window_display_accurate after all windows have
14248 been redisplayed because this call resets flags in buffers
14249 which are needed for proper redisplay. */
14250 FOR_EACH_FRAME (tail, frame)
14252 struct frame *f = XFRAME (frame);
14253 if (f->updated_p)
14255 f->redisplay = false;
14256 f->garbaged = false;
14257 mark_window_display_accurate (f->root_window, true);
14258 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14259 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14264 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14266 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14267 /* Use list_of_error, not Qerror, so that
14268 we catch only errors and don't run the debugger. */
14269 internal_condition_case_1 (redisplay_window_1, selected_window,
14270 list_of_error,
14271 redisplay_window_error);
14272 if (update_miniwindow_p)
14273 internal_condition_case_1 (redisplay_window_1,
14274 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14275 redisplay_window_error);
14277 /* Compare desired and current matrices, perform output. */
14279 update:
14280 /* If fonts changed, display again. Likewise if redisplay_window_1
14281 above caused some change (e.g., a change in faces) that requires
14282 considering the entire frame again. */
14283 if (sf->fonts_changed || sf->redisplay)
14285 if (sf->redisplay)
14287 /* Set this to force a more thorough redisplay.
14288 Otherwise, we might immediately loop back to the
14289 above "else-if" clause (since all the conditions that
14290 led here might still be true), and we will then
14291 infloop, because the selected-frame's redisplay flag
14292 is not (and cannot be) reset. */
14293 windows_or_buffers_changed = 50;
14295 goto retry;
14298 /* Prevent freeing of realized faces, since desired matrices are
14299 pending that reference the faces we computed and cached. */
14300 inhibit_free_realized_faces = true;
14302 /* Prevent various kinds of signals during display update.
14303 stdio is not robust about handling signals,
14304 which can cause an apparent I/O error. */
14305 if (interrupt_input)
14306 unrequest_sigio ();
14307 STOP_POLLING;
14309 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14311 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14312 && hscroll_windows (selected_window))
14314 hscroll_retries++;
14315 goto retry;
14318 XWINDOW (selected_window)->must_be_updated_p = true;
14319 pending = update_frame (sf, false, false);
14320 sf->cursor_type_changed = false;
14323 /* We may have called echo_area_display at the top of this
14324 function. If the echo area is on another frame, that may
14325 have put text on a frame other than the selected one, so the
14326 above call to update_frame would not have caught it. Catch
14327 it here. */
14328 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14329 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14331 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14333 XWINDOW (mini_window)->must_be_updated_p = true;
14334 pending |= update_frame (mini_frame, false, false);
14335 mini_frame->cursor_type_changed = false;
14336 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14337 && hscroll_windows (mini_window))
14339 hscroll_retries++;
14340 goto retry;
14345 /* If display was paused because of pending input, make sure we do a
14346 thorough update the next time. */
14347 if (pending)
14349 /* Prevent the optimization at the beginning of
14350 redisplay_internal that tries a single-line update of the
14351 line containing the cursor in the selected window. */
14352 CHARPOS (this_line_start_pos) = 0;
14354 /* Let the overlay arrow be updated the next time. */
14355 update_overlay_arrows (0);
14357 /* If we pause after scrolling, some rows in the current
14358 matrices of some windows are not valid. */
14359 if (!WINDOW_FULL_WIDTH_P (w)
14360 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14361 update_mode_lines = 36;
14363 else
14365 if (!consider_all_windows_p)
14367 /* This has already been done above if
14368 consider_all_windows_p is set. */
14369 if (XBUFFER (w->contents)->text->redisplay
14370 && buffer_window_count (XBUFFER (w->contents)) > 1)
14371 /* This can happen if b->text->redisplay was set during
14372 jit-lock. */
14373 propagate_buffer_redisplay ();
14374 mark_window_display_accurate_1 (w, true);
14376 /* Say overlay arrows are up to date. */
14377 update_overlay_arrows (1);
14379 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14380 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14383 update_mode_lines = 0;
14384 windows_or_buffers_changed = 0;
14387 /* Start SIGIO interrupts coming again. Having them off during the
14388 code above makes it less likely one will discard output, but not
14389 impossible, since there might be stuff in the system buffer here.
14390 But it is much hairier to try to do anything about that. */
14391 if (interrupt_input)
14392 request_sigio ();
14393 RESUME_POLLING;
14395 /* If a frame has become visible which was not before, redisplay
14396 again, so that we display it. Expose events for such a frame
14397 (which it gets when becoming visible) don't call the parts of
14398 redisplay constructing glyphs, so simply exposing a frame won't
14399 display anything in this case. So, we have to display these
14400 frames here explicitly. */
14401 if (!pending)
14403 int new_count = 0;
14405 FOR_EACH_FRAME (tail, frame)
14407 if (XFRAME (frame)->visible)
14408 new_count++;
14411 if (new_count != number_of_visible_frames)
14412 windows_or_buffers_changed = 52;
14415 /* Change frame size now if a change is pending. */
14416 do_pending_window_change (true);
14418 /* If we just did a pending size change, or have additional
14419 visible frames, or selected_window changed, redisplay again. */
14420 if ((windows_or_buffers_changed && !pending)
14421 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14422 goto retry;
14424 /* Clear the face and image caches.
14426 We used to do this only if consider_all_windows_p. But the cache
14427 needs to be cleared if a timer creates images in the current
14428 buffer (e.g. the test case in Bug#6230). */
14430 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14432 clear_face_cache (false);
14433 clear_face_cache_count = 0;
14436 #ifdef HAVE_WINDOW_SYSTEM
14437 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14439 clear_image_caches (Qnil);
14440 clear_image_cache_count = 0;
14442 #endif /* HAVE_WINDOW_SYSTEM */
14444 end_of_redisplay:
14445 #ifdef HAVE_NS
14446 ns_set_doc_edited ();
14447 #endif
14448 if (interrupt_input && interrupts_deferred)
14449 request_sigio ();
14451 unbind_to (count, Qnil);
14452 RESUME_POLLING;
14455 static void
14456 unwind_redisplay_preserve_echo_area (void)
14458 unblock_buffer_flips ();
14461 /* Redisplay, but leave alone any recent echo area message unless
14462 another message has been requested in its place.
14464 This is useful in situations where you need to redisplay but no
14465 user action has occurred, making it inappropriate for the message
14466 area to be cleared. See tracking_off and
14467 wait_reading_process_output for examples of these situations.
14469 FROM_WHERE is an integer saying from where this function was
14470 called. This is useful for debugging. */
14472 void
14473 redisplay_preserve_echo_area (int from_where)
14475 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14477 block_input ();
14478 ptrdiff_t count = SPECPDL_INDEX ();
14479 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14480 block_buffer_flips ();
14481 unblock_input ();
14483 if (!NILP (echo_area_buffer[1]))
14485 /* We have a previously displayed message, but no current
14486 message. Redisplay the previous message. */
14487 display_last_displayed_message_p = true;
14488 redisplay_internal ();
14489 display_last_displayed_message_p = false;
14491 else
14492 redisplay_internal ();
14494 flush_frame (SELECTED_FRAME ());
14495 unbind_to (count, Qnil);
14499 /* Function registered with record_unwind_protect in redisplay_internal. */
14501 static void
14502 unwind_redisplay (void)
14504 redisplaying_p = false;
14505 unblock_buffer_flips ();
14509 /* Mark the display of leaf window W as accurate or inaccurate.
14510 If ACCURATE_P, mark display of W as accurate.
14511 If !ACCURATE_P, arrange for W to be redisplayed the next
14512 time redisplay_internal is called. */
14514 static void
14515 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14517 struct buffer *b = XBUFFER (w->contents);
14519 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14520 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14521 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14523 if (accurate_p)
14525 b->clip_changed = false;
14526 b->prevent_redisplay_optimizations_p = false;
14527 eassert (buffer_window_count (b) > 0);
14528 /* Resetting b->text->redisplay is problematic!
14529 In order to make it safer to do it here, redisplay_internal must
14530 have copied all b->text->redisplay to their respective windows. */
14531 b->text->redisplay = false;
14533 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14534 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14535 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14536 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14538 w->current_matrix->buffer = b;
14539 w->current_matrix->begv = BUF_BEGV (b);
14540 w->current_matrix->zv = BUF_ZV (b);
14542 w->last_cursor_vpos = w->cursor.vpos;
14543 w->last_cursor_off_p = w->cursor_off_p;
14545 if (w == XWINDOW (selected_window))
14546 w->last_point = BUF_PT (b);
14547 else
14548 w->last_point = marker_position (w->pointm);
14550 w->window_end_valid = true;
14551 w->update_mode_line = false;
14554 w->redisplay = !accurate_p;
14558 /* Mark the display of windows in the window tree rooted at WINDOW as
14559 accurate or inaccurate. If ACCURATE_P, mark display of
14560 windows as accurate. If !ACCURATE_P, arrange for windows to
14561 be redisplayed the next time redisplay_internal is called. */
14563 void
14564 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14566 struct window *w;
14568 for (; !NILP (window); window = w->next)
14570 w = XWINDOW (window);
14571 if (WINDOWP (w->contents))
14572 mark_window_display_accurate (w->contents, accurate_p);
14573 else
14574 mark_window_display_accurate_1 (w, accurate_p);
14577 if (accurate_p)
14578 update_overlay_arrows (1);
14579 else
14580 /* Force a thorough redisplay the next time by setting
14581 last_arrow_position and last_arrow_string to t, which is
14582 unequal to any useful value of Voverlay_arrow_... */
14583 update_overlay_arrows (-1);
14587 /* Return value in display table DP (Lisp_Char_Table *) for character
14588 C. Since a display table doesn't have any parent, we don't have to
14589 follow parent. Do not call this function directly but use the
14590 macro DISP_CHAR_VECTOR. */
14592 Lisp_Object
14593 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14595 Lisp_Object val;
14597 if (ASCII_CHAR_P (c))
14599 val = dp->ascii;
14600 if (SUB_CHAR_TABLE_P (val))
14601 val = XSUB_CHAR_TABLE (val)->contents[c];
14603 else
14605 Lisp_Object table;
14607 XSETCHAR_TABLE (table, dp);
14608 val = char_table_ref (table, c);
14610 if (NILP (val))
14611 val = dp->defalt;
14612 return val;
14615 static int buffer_flip_blocked_depth;
14617 static void
14618 block_buffer_flips (void)
14620 eassert (buffer_flip_blocked_depth >= 0);
14621 buffer_flip_blocked_depth++;
14624 static void
14625 unblock_buffer_flips (void)
14627 eassert (buffer_flip_blocked_depth > 0);
14628 if (--buffer_flip_blocked_depth == 0)
14630 Lisp_Object tail, frame;
14631 block_input ();
14632 FOR_EACH_FRAME (tail, frame)
14634 struct frame *f = XFRAME (frame);
14635 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14636 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14638 unblock_input ();
14642 bool
14643 buffer_flipping_blocked_p (void)
14645 return buffer_flip_blocked_depth > 0;
14649 /***********************************************************************
14650 Window Redisplay
14651 ***********************************************************************/
14653 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14655 static void
14656 redisplay_windows (Lisp_Object window)
14658 while (!NILP (window))
14660 struct window *w = XWINDOW (window);
14662 if (WINDOWP (w->contents))
14663 redisplay_windows (w->contents);
14664 else if (BUFFERP (w->contents))
14666 displayed_buffer = XBUFFER (w->contents);
14667 /* Use list_of_error, not Qerror, so that
14668 we catch only errors and don't run the debugger. */
14669 internal_condition_case_1 (redisplay_window_0, window,
14670 list_of_error,
14671 redisplay_window_error);
14674 window = w->next;
14678 static Lisp_Object
14679 redisplay_window_error (Lisp_Object ignore)
14681 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14682 return Qnil;
14685 static Lisp_Object
14686 redisplay_window_0 (Lisp_Object window)
14688 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14689 redisplay_window (window, false);
14690 return Qnil;
14693 static Lisp_Object
14694 redisplay_window_1 (Lisp_Object window)
14696 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14697 redisplay_window (window, true);
14698 return Qnil;
14702 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14703 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14704 which positions recorded in ROW differ from current buffer
14705 positions.
14707 Return true iff cursor is on this row. */
14709 static bool
14710 set_cursor_from_row (struct window *w, struct glyph_row *row,
14711 struct glyph_matrix *matrix,
14712 ptrdiff_t delta, ptrdiff_t delta_bytes,
14713 int dy, int dvpos)
14715 struct glyph *glyph = row->glyphs[TEXT_AREA];
14716 struct glyph *end = glyph + row->used[TEXT_AREA];
14717 struct glyph *cursor = NULL;
14718 /* The last known character position in row. */
14719 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14720 int x = row->x;
14721 ptrdiff_t pt_old = PT - delta;
14722 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14723 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14724 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14725 /* A glyph beyond the edge of TEXT_AREA which we should never
14726 touch. */
14727 struct glyph *glyphs_end = end;
14728 /* True means we've found a match for cursor position, but that
14729 glyph has the avoid_cursor_p flag set. */
14730 bool match_with_avoid_cursor = false;
14731 /* True means we've seen at least one glyph that came from a
14732 display string. */
14733 bool string_seen = false;
14734 /* Largest and smallest buffer positions seen so far during scan of
14735 glyph row. */
14736 ptrdiff_t bpos_max = pos_before;
14737 ptrdiff_t bpos_min = pos_after;
14738 /* Last buffer position covered by an overlay string with an integer
14739 `cursor' property. */
14740 ptrdiff_t bpos_covered = 0;
14741 /* True means the display string on which to display the cursor
14742 comes from a text property, not from an overlay. */
14743 bool string_from_text_prop = false;
14745 /* Don't even try doing anything if called for a mode-line or
14746 header-line row, since the rest of the code isn't prepared to
14747 deal with such calamities. */
14748 eassert (!row->mode_line_p);
14749 if (row->mode_line_p)
14750 return false;
14752 /* Skip over glyphs not having an object at the start and the end of
14753 the row. These are special glyphs like truncation marks on
14754 terminal frames. */
14755 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14757 if (!row->reversed_p)
14759 while (glyph < end
14760 && NILP (glyph->object)
14761 && glyph->charpos < 0)
14763 x += glyph->pixel_width;
14764 ++glyph;
14766 while (end > glyph
14767 && NILP ((end - 1)->object)
14768 /* CHARPOS is zero for blanks and stretch glyphs
14769 inserted by extend_face_to_end_of_line. */
14770 && (end - 1)->charpos <= 0)
14771 --end;
14772 glyph_before = glyph - 1;
14773 glyph_after = end;
14775 else
14777 struct glyph *g;
14779 /* If the glyph row is reversed, we need to process it from back
14780 to front, so swap the edge pointers. */
14781 glyphs_end = end = glyph - 1;
14782 glyph += row->used[TEXT_AREA] - 1;
14784 while (glyph > end + 1
14785 && NILP (glyph->object)
14786 && glyph->charpos < 0)
14788 --glyph;
14789 x -= glyph->pixel_width;
14791 if (NILP (glyph->object) && glyph->charpos < 0)
14792 --glyph;
14793 /* By default, in reversed rows we put the cursor on the
14794 rightmost (first in the reading order) glyph. */
14795 for (g = end + 1; g < glyph; g++)
14796 x += g->pixel_width;
14797 while (end < glyph
14798 && NILP ((end + 1)->object)
14799 && (end + 1)->charpos <= 0)
14800 ++end;
14801 glyph_before = glyph + 1;
14802 glyph_after = end;
14805 else if (row->reversed_p)
14807 /* In R2L rows that don't display text, put the cursor on the
14808 rightmost glyph. Case in point: an empty last line that is
14809 part of an R2L paragraph. */
14810 cursor = end - 1;
14811 /* Avoid placing the cursor on the last glyph of the row, where
14812 on terminal frames we hold the vertical border between
14813 adjacent windows. */
14814 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14815 && !WINDOW_RIGHTMOST_P (w)
14816 && cursor == row->glyphs[LAST_AREA] - 1)
14817 cursor--;
14818 x = -1; /* will be computed below, at label compute_x */
14821 /* Step 1: Try to find the glyph whose character position
14822 corresponds to point. If that's not possible, find 2 glyphs
14823 whose character positions are the closest to point, one before
14824 point, the other after it. */
14825 if (!row->reversed_p)
14826 while (/* not marched to end of glyph row */
14827 glyph < end
14828 /* glyph was not inserted by redisplay for internal purposes */
14829 && !NILP (glyph->object))
14831 if (BUFFERP (glyph->object))
14833 ptrdiff_t dpos = glyph->charpos - pt_old;
14835 if (glyph->charpos > bpos_max)
14836 bpos_max = glyph->charpos;
14837 if (glyph->charpos < bpos_min)
14838 bpos_min = glyph->charpos;
14839 if (!glyph->avoid_cursor_p)
14841 /* If we hit point, we've found the glyph on which to
14842 display the cursor. */
14843 if (dpos == 0)
14845 match_with_avoid_cursor = false;
14846 break;
14848 /* See if we've found a better approximation to
14849 POS_BEFORE or to POS_AFTER. */
14850 if (0 > dpos && dpos > pos_before - pt_old)
14852 pos_before = glyph->charpos;
14853 glyph_before = glyph;
14855 else if (0 < dpos && dpos < pos_after - pt_old)
14857 pos_after = glyph->charpos;
14858 glyph_after = glyph;
14861 else if (dpos == 0)
14862 match_with_avoid_cursor = true;
14864 else if (STRINGP (glyph->object))
14866 Lisp_Object chprop;
14867 ptrdiff_t glyph_pos = glyph->charpos;
14869 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14870 glyph->object);
14871 if (!NILP (chprop))
14873 /* If the string came from a `display' text property,
14874 look up the buffer position of that property and
14875 use that position to update bpos_max, as if we
14876 actually saw such a position in one of the row's
14877 glyphs. This helps with supporting integer values
14878 of `cursor' property on the display string in
14879 situations where most or all of the row's buffer
14880 text is completely covered by display properties,
14881 so that no glyph with valid buffer positions is
14882 ever seen in the row. */
14883 ptrdiff_t prop_pos =
14884 string_buffer_position_lim (glyph->object, pos_before,
14885 pos_after, false);
14887 if (prop_pos >= pos_before)
14888 bpos_max = prop_pos;
14890 if (INTEGERP (chprop))
14892 bpos_covered = bpos_max + XINT (chprop);
14893 /* If the `cursor' property covers buffer positions up
14894 to and including point, we should display cursor on
14895 this glyph. Note that, if a `cursor' property on one
14896 of the string's characters has an integer value, we
14897 will break out of the loop below _before_ we get to
14898 the position match above. IOW, integer values of
14899 the `cursor' property override the "exact match for
14900 point" strategy of positioning the cursor. */
14901 /* Implementation note: bpos_max == pt_old when, e.g.,
14902 we are in an empty line, where bpos_max is set to
14903 MATRIX_ROW_START_CHARPOS, see above. */
14904 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14906 cursor = glyph;
14907 break;
14911 string_seen = true;
14913 x += glyph->pixel_width;
14914 ++glyph;
14916 else if (glyph > end) /* row is reversed */
14917 while (!NILP (glyph->object))
14919 if (BUFFERP (glyph->object))
14921 ptrdiff_t dpos = glyph->charpos - pt_old;
14923 if (glyph->charpos > bpos_max)
14924 bpos_max = glyph->charpos;
14925 if (glyph->charpos < bpos_min)
14926 bpos_min = glyph->charpos;
14927 if (!glyph->avoid_cursor_p)
14929 if (dpos == 0)
14931 match_with_avoid_cursor = false;
14932 break;
14934 if (0 > dpos && dpos > pos_before - pt_old)
14936 pos_before = glyph->charpos;
14937 glyph_before = glyph;
14939 else if (0 < dpos && dpos < pos_after - pt_old)
14941 pos_after = glyph->charpos;
14942 glyph_after = glyph;
14945 else if (dpos == 0)
14946 match_with_avoid_cursor = true;
14948 else if (STRINGP (glyph->object))
14950 Lisp_Object chprop;
14951 ptrdiff_t glyph_pos = glyph->charpos;
14953 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14954 glyph->object);
14955 if (!NILP (chprop))
14957 ptrdiff_t prop_pos =
14958 string_buffer_position_lim (glyph->object, pos_before,
14959 pos_after, false);
14961 if (prop_pos >= pos_before)
14962 bpos_max = prop_pos;
14964 if (INTEGERP (chprop))
14966 bpos_covered = bpos_max + XINT (chprop);
14967 /* If the `cursor' property covers buffer positions up
14968 to and including point, we should display cursor on
14969 this glyph. */
14970 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14972 cursor = glyph;
14973 break;
14976 string_seen = true;
14978 --glyph;
14979 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14981 x--; /* can't use any pixel_width */
14982 break;
14984 x -= glyph->pixel_width;
14987 /* Step 2: If we didn't find an exact match for point, we need to
14988 look for a proper place to put the cursor among glyphs between
14989 GLYPH_BEFORE and GLYPH_AFTER. */
14990 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14991 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14992 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14994 /* An empty line has a single glyph whose OBJECT is nil and
14995 whose CHARPOS is the position of a newline on that line.
14996 Note that on a TTY, there are more glyphs after that, which
14997 were produced by extend_face_to_end_of_line, but their
14998 CHARPOS is zero or negative. */
14999 bool empty_line_p =
15000 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15001 && NILP (glyph->object) && glyph->charpos > 0
15002 /* On a TTY, continued and truncated rows also have a glyph at
15003 their end whose OBJECT is nil and whose CHARPOS is
15004 positive (the continuation and truncation glyphs), but such
15005 rows are obviously not "empty". */
15006 && !(row->continued_p || row->truncated_on_right_p));
15008 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15010 ptrdiff_t ellipsis_pos;
15012 /* Scan back over the ellipsis glyphs. */
15013 if (!row->reversed_p)
15015 ellipsis_pos = (glyph - 1)->charpos;
15016 while (glyph > row->glyphs[TEXT_AREA]
15017 && (glyph - 1)->charpos == ellipsis_pos)
15018 glyph--, x -= glyph->pixel_width;
15019 /* That loop always goes one position too far, including
15020 the glyph before the ellipsis. So scan forward over
15021 that one. */
15022 x += glyph->pixel_width;
15023 glyph++;
15025 else /* row is reversed */
15027 ellipsis_pos = (glyph + 1)->charpos;
15028 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15029 && (glyph + 1)->charpos == ellipsis_pos)
15030 glyph++, x += glyph->pixel_width;
15031 x -= glyph->pixel_width;
15032 glyph--;
15035 else if (match_with_avoid_cursor)
15037 cursor = glyph_after;
15038 x = -1;
15040 else if (string_seen)
15042 int incr = row->reversed_p ? -1 : +1;
15044 /* Need to find the glyph that came out of a string which is
15045 present at point. That glyph is somewhere between
15046 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15047 positioned between POS_BEFORE and POS_AFTER in the
15048 buffer. */
15049 struct glyph *start, *stop;
15050 ptrdiff_t pos = pos_before;
15052 x = -1;
15054 /* If the row ends in a newline from a display string,
15055 reordering could have moved the glyphs belonging to the
15056 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15057 in this case we extend the search to the last glyph in
15058 the row that was not inserted by redisplay. */
15059 if (row->ends_in_newline_from_string_p)
15061 glyph_after = end;
15062 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15065 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15066 correspond to POS_BEFORE and POS_AFTER, respectively. We
15067 need START and STOP in the order that corresponds to the
15068 row's direction as given by its reversed_p flag. If the
15069 directionality of characters between POS_BEFORE and
15070 POS_AFTER is the opposite of the row's base direction,
15071 these characters will have been reordered for display,
15072 and we need to reverse START and STOP. */
15073 if (!row->reversed_p)
15075 start = min (glyph_before, glyph_after);
15076 stop = max (glyph_before, glyph_after);
15078 else
15080 start = max (glyph_before, glyph_after);
15081 stop = min (glyph_before, glyph_after);
15083 for (glyph = start + incr;
15084 row->reversed_p ? glyph > stop : glyph < stop; )
15087 /* Any glyphs that come from the buffer are here because
15088 of bidi reordering. Skip them, and only pay
15089 attention to glyphs that came from some string. */
15090 if (STRINGP (glyph->object))
15092 Lisp_Object str;
15093 ptrdiff_t tem;
15094 /* If the display property covers the newline, we
15095 need to search for it one position farther. */
15096 ptrdiff_t lim = pos_after
15097 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15099 string_from_text_prop = false;
15100 str = glyph->object;
15101 tem = string_buffer_position_lim (str, pos, lim, false);
15102 if (tem == 0 /* from overlay */
15103 || pos <= tem)
15105 /* If the string from which this glyph came is
15106 found in the buffer at point, or at position
15107 that is closer to point than pos_after, then
15108 we've found the glyph we've been looking for.
15109 If it comes from an overlay (tem == 0), and
15110 it has the `cursor' property on one of its
15111 glyphs, record that glyph as a candidate for
15112 displaying the cursor. (As in the
15113 unidirectional version, we will display the
15114 cursor on the last candidate we find.) */
15115 if (tem == 0
15116 || tem == pt_old
15117 || (tem - pt_old > 0 && tem < pos_after))
15119 /* The glyphs from this string could have
15120 been reordered. Find the one with the
15121 smallest string position. Or there could
15122 be a character in the string with the
15123 `cursor' property, which means display
15124 cursor on that character's glyph. */
15125 ptrdiff_t strpos = glyph->charpos;
15127 if (tem)
15129 cursor = glyph;
15130 string_from_text_prop = true;
15132 for ( ;
15133 (row->reversed_p ? glyph > stop : glyph < stop)
15134 && EQ (glyph->object, str);
15135 glyph += incr)
15137 Lisp_Object cprop;
15138 ptrdiff_t gpos = glyph->charpos;
15140 cprop = Fget_char_property (make_number (gpos),
15141 Qcursor,
15142 glyph->object);
15143 if (!NILP (cprop))
15145 cursor = glyph;
15146 break;
15148 if (tem && glyph->charpos < strpos)
15150 strpos = glyph->charpos;
15151 cursor = glyph;
15155 if (tem == pt_old
15156 || (tem - pt_old > 0 && tem < pos_after))
15157 goto compute_x;
15159 if (tem)
15160 pos = tem + 1; /* don't find previous instances */
15162 /* This string is not what we want; skip all of the
15163 glyphs that came from it. */
15164 while ((row->reversed_p ? glyph > stop : glyph < stop)
15165 && EQ (glyph->object, str))
15166 glyph += incr;
15168 else
15169 glyph += incr;
15172 /* If we reached the end of the line, and END was from a string,
15173 the cursor is not on this line. */
15174 if (cursor == NULL
15175 && (row->reversed_p ? glyph <= end : glyph >= end)
15176 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15177 && STRINGP (end->object)
15178 && row->continued_p)
15179 return false;
15181 /* A truncated row may not include PT among its character positions.
15182 Setting the cursor inside the scroll margin will trigger
15183 recalculation of hscroll in hscroll_window_tree. But if a
15184 display string covers point, defer to the string-handling
15185 code below to figure this out. */
15186 else if (row->truncated_on_left_p && pt_old < bpos_min)
15188 cursor = glyph_before;
15189 x = -1;
15191 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15192 /* Zero-width characters produce no glyphs. */
15193 || (!empty_line_p
15194 && (row->reversed_p
15195 ? glyph_after > glyphs_end
15196 : glyph_after < glyphs_end)))
15198 cursor = glyph_after;
15199 x = -1;
15203 compute_x:
15204 if (cursor != NULL)
15205 glyph = cursor;
15206 else if (glyph == glyphs_end
15207 && pos_before == pos_after
15208 && STRINGP ((row->reversed_p
15209 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15210 : row->glyphs[TEXT_AREA])->object))
15212 /* If all the glyphs of this row came from strings, put the
15213 cursor on the first glyph of the row. This avoids having the
15214 cursor outside of the text area in this very rare and hard
15215 use case. */
15216 glyph =
15217 row->reversed_p
15218 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15219 : row->glyphs[TEXT_AREA];
15221 if (x < 0)
15223 struct glyph *g;
15225 /* Need to compute x that corresponds to GLYPH. */
15226 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15228 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15229 emacs_abort ();
15230 x += g->pixel_width;
15234 /* ROW could be part of a continued line, which, under bidi
15235 reordering, might have other rows whose start and end charpos
15236 occlude point. Only set w->cursor if we found a better
15237 approximation to the cursor position than we have from previously
15238 examined candidate rows belonging to the same continued line. */
15239 if (/* We already have a candidate row. */
15240 w->cursor.vpos >= 0
15241 /* That candidate is not the row we are processing. */
15242 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15243 /* Make sure cursor.vpos specifies a row whose start and end
15244 charpos occlude point, and it is valid candidate for being a
15245 cursor-row. This is because some callers of this function
15246 leave cursor.vpos at the row where the cursor was displayed
15247 during the last redisplay cycle. */
15248 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15249 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15250 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15252 struct glyph *g1
15253 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15255 /* Don't consider glyphs that are outside TEXT_AREA. */
15256 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15257 return false;
15258 /* Keep the candidate whose buffer position is the closest to
15259 point or has the `cursor' property. */
15260 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15261 w->cursor.hpos >= 0
15262 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15263 && ((BUFFERP (g1->object)
15264 && (g1->charpos == pt_old /* An exact match always wins. */
15265 || (BUFFERP (glyph->object)
15266 && eabs (g1->charpos - pt_old)
15267 < eabs (glyph->charpos - pt_old))))
15268 /* Previous candidate is a glyph from a string that has
15269 a non-nil `cursor' property. */
15270 || (STRINGP (g1->object)
15271 && (!NILP (Fget_char_property (make_number (g1->charpos),
15272 Qcursor, g1->object))
15273 /* Previous candidate is from the same display
15274 string as this one, and the display string
15275 came from a text property. */
15276 || (EQ (g1->object, glyph->object)
15277 && string_from_text_prop)
15278 /* this candidate is from newline and its
15279 position is not an exact match */
15280 || (NILP (glyph->object)
15281 && glyph->charpos != pt_old)))))
15282 return false;
15283 /* If this candidate gives an exact match, use that. */
15284 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15285 /* If this candidate is a glyph created for the
15286 terminating newline of a line, and point is on that
15287 newline, it wins because it's an exact match. */
15288 || (!row->continued_p
15289 && NILP (glyph->object)
15290 && glyph->charpos == 0
15291 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15292 /* Otherwise, keep the candidate that comes from a row
15293 spanning less buffer positions. This may win when one or
15294 both candidate positions are on glyphs that came from
15295 display strings, for which we cannot compare buffer
15296 positions. */
15297 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15298 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15299 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15300 return false;
15302 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15303 w->cursor.x = x;
15304 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15305 w->cursor.y = row->y + dy;
15307 if (w == XWINDOW (selected_window))
15309 if (!row->continued_p
15310 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15311 && row->x == 0)
15313 this_line_buffer = XBUFFER (w->contents);
15315 CHARPOS (this_line_start_pos)
15316 = MATRIX_ROW_START_CHARPOS (row) + delta;
15317 BYTEPOS (this_line_start_pos)
15318 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15320 CHARPOS (this_line_end_pos)
15321 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15322 BYTEPOS (this_line_end_pos)
15323 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15325 this_line_y = w->cursor.y;
15326 this_line_pixel_height = row->height;
15327 this_line_vpos = w->cursor.vpos;
15328 this_line_start_x = row->x;
15330 else
15331 CHARPOS (this_line_start_pos) = 0;
15334 return true;
15338 /* Run window scroll functions, if any, for WINDOW with new window
15339 start STARTP. Sets the window start of WINDOW to that position.
15341 We assume that the window's buffer is really current. */
15343 static struct text_pos
15344 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15346 struct window *w = XWINDOW (window);
15347 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15349 eassert (current_buffer == XBUFFER (w->contents));
15351 if (!NILP (Vwindow_scroll_functions))
15353 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15354 make_number (CHARPOS (startp)));
15355 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15356 /* In case the hook functions switch buffers. */
15357 set_buffer_internal (XBUFFER (w->contents));
15360 return startp;
15364 /* Make sure the line containing the cursor is fully visible.
15365 A value of true means there is nothing to be done.
15366 (Either the line is fully visible, or it cannot be made so,
15367 or we cannot tell.)
15369 If FORCE_P, return false even if partial visible cursor row
15370 is higher than window.
15372 If CURRENT_MATRIX_P, use the information from the
15373 window's current glyph matrix; otherwise use the desired glyph
15374 matrix.
15376 A value of false means the caller should do scrolling
15377 as if point had gone off the screen. */
15379 static bool
15380 cursor_row_fully_visible_p (struct window *w, bool force_p,
15381 bool current_matrix_p)
15383 struct glyph_matrix *matrix;
15384 struct glyph_row *row;
15385 int window_height;
15387 if (!make_cursor_line_fully_visible_p)
15388 return true;
15390 /* It's not always possible to find the cursor, e.g, when a window
15391 is full of overlay strings. Don't do anything in that case. */
15392 if (w->cursor.vpos < 0)
15393 return true;
15395 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15396 row = MATRIX_ROW (matrix, w->cursor.vpos);
15398 /* If the cursor row is not partially visible, there's nothing to do. */
15399 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15400 return true;
15402 /* If the row the cursor is in is taller than the window's height,
15403 it's not clear what to do, so do nothing. */
15404 window_height = window_box_height (w);
15405 if (row->height >= window_height)
15407 if (!force_p || MINI_WINDOW_P (w)
15408 || w->vscroll || w->cursor.vpos == 0)
15409 return true;
15411 return false;
15415 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15416 means only WINDOW is redisplayed in redisplay_internal.
15417 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15418 in redisplay_window to bring a partially visible line into view in
15419 the case that only the cursor has moved.
15421 LAST_LINE_MISFIT should be true if we're scrolling because the
15422 last screen line's vertical height extends past the end of the screen.
15424 Value is
15426 1 if scrolling succeeded
15428 0 if scrolling didn't find point.
15430 -1 if new fonts have been loaded so that we must interrupt
15431 redisplay, adjust glyph matrices, and try again. */
15433 enum
15435 SCROLLING_SUCCESS,
15436 SCROLLING_FAILED,
15437 SCROLLING_NEED_LARGER_MATRICES
15440 /* If scroll-conservatively is more than this, never recenter.
15442 If you change this, don't forget to update the doc string of
15443 `scroll-conservatively' and the Emacs manual. */
15444 #define SCROLL_LIMIT 100
15446 static int
15447 try_scrolling (Lisp_Object window, bool just_this_one_p,
15448 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15449 bool temp_scroll_step, bool last_line_misfit)
15451 struct window *w = XWINDOW (window);
15452 struct text_pos pos, startp;
15453 struct it it;
15454 int this_scroll_margin, scroll_max, rc, height;
15455 int dy = 0, amount_to_scroll = 0;
15456 bool scroll_down_p = false;
15457 int extra_scroll_margin_lines = last_line_misfit;
15458 Lisp_Object aggressive;
15459 /* We will never try scrolling more than this number of lines. */
15460 int scroll_limit = SCROLL_LIMIT;
15461 int frame_line_height = default_line_pixel_height (w);
15463 #ifdef GLYPH_DEBUG
15464 debug_method_add (w, "try_scrolling");
15465 #endif
15467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15469 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15471 /* Force arg_scroll_conservatively to have a reasonable value, to
15472 avoid scrolling too far away with slow move_it_* functions. Note
15473 that the user can supply scroll-conservatively equal to
15474 `most-positive-fixnum', which can be larger than INT_MAX. */
15475 if (arg_scroll_conservatively > scroll_limit)
15477 arg_scroll_conservatively = scroll_limit + 1;
15478 scroll_max = scroll_limit * frame_line_height;
15480 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15481 /* Compute how much we should try to scroll maximally to bring
15482 point into view. */
15483 scroll_max = (max (scroll_step,
15484 max (arg_scroll_conservatively, temp_scroll_step))
15485 * frame_line_height);
15486 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15487 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15488 /* We're trying to scroll because of aggressive scrolling but no
15489 scroll_step is set. Choose an arbitrary one. */
15490 scroll_max = 10 * frame_line_height;
15491 else
15492 scroll_max = 0;
15494 too_near_end:
15496 /* Decide whether to scroll down. */
15497 if (PT > CHARPOS (startp))
15499 int scroll_margin_y;
15501 /* Compute the pixel ypos of the scroll margin, then move IT to
15502 either that ypos or PT, whichever comes first. */
15503 start_display (&it, w, startp);
15504 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15505 - this_scroll_margin
15506 - frame_line_height * extra_scroll_margin_lines;
15507 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15508 (MOVE_TO_POS | MOVE_TO_Y));
15510 if (PT > CHARPOS (it.current.pos))
15512 int y0 = line_bottom_y (&it);
15513 /* Compute how many pixels below window bottom to stop searching
15514 for PT. This avoids costly search for PT that is far away if
15515 the user limited scrolling by a small number of lines, but
15516 always finds PT if scroll_conservatively is set to a large
15517 number, such as most-positive-fixnum. */
15518 int slack = max (scroll_max, 10 * frame_line_height);
15519 int y_to_move = it.last_visible_y + slack;
15521 /* Compute the distance from the scroll margin to PT or to
15522 the scroll limit, whichever comes first. This should
15523 include the height of the cursor line, to make that line
15524 fully visible. */
15525 move_it_to (&it, PT, -1, y_to_move,
15526 -1, MOVE_TO_POS | MOVE_TO_Y);
15527 dy = line_bottom_y (&it) - y0;
15529 if (dy > scroll_max)
15530 return SCROLLING_FAILED;
15532 if (dy > 0)
15533 scroll_down_p = true;
15535 else if (PT == IT_CHARPOS (it)
15536 && IT_CHARPOS (it) < ZV
15537 && it.method == GET_FROM_STRING
15538 && arg_scroll_conservatively > scroll_limit
15539 && it.current_x == 0)
15541 enum move_it_result skip;
15542 int y1 = it.current_y;
15543 int vpos;
15545 /* A before-string that includes newlines and is displayed
15546 on the last visible screen line could fail us under
15547 scroll-conservatively > 100, because we will be unable to
15548 position the cursor on that last visible line. Try to
15549 recover by finding the first screen line that has some
15550 glyphs coming from the buffer text. */
15551 do {
15552 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15553 if (skip != MOVE_NEWLINE_OR_CR
15554 || IT_CHARPOS (it) != PT
15555 || it.method == GET_FROM_BUFFER)
15556 break;
15557 vpos = it.vpos;
15558 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15559 } while (it.vpos > vpos);
15561 dy = it.current_y - y1;
15563 if (dy > scroll_max)
15564 return SCROLLING_FAILED;
15566 if (dy > 0)
15567 scroll_down_p = true;
15571 if (scroll_down_p)
15573 /* Point is in or below the bottom scroll margin, so move the
15574 window start down. If scrolling conservatively, move it just
15575 enough down to make point visible. If scroll_step is set,
15576 move it down by scroll_step. */
15577 if (arg_scroll_conservatively)
15578 amount_to_scroll
15579 = min (max (dy, frame_line_height),
15580 frame_line_height * arg_scroll_conservatively);
15581 else if (scroll_step || temp_scroll_step)
15582 amount_to_scroll = scroll_max;
15583 else
15585 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15586 height = WINDOW_BOX_TEXT_HEIGHT (w);
15587 if (NUMBERP (aggressive))
15589 double float_amount = XFLOATINT (aggressive) * height;
15590 int aggressive_scroll = float_amount;
15591 if (aggressive_scroll == 0 && float_amount > 0)
15592 aggressive_scroll = 1;
15593 /* Don't let point enter the scroll margin near top of
15594 the window. This could happen if the value of
15595 scroll_up_aggressively is too large and there are
15596 non-zero margins, because scroll_up_aggressively
15597 means put point that fraction of window height
15598 _from_the_bottom_margin_. */
15599 if (aggressive_scroll + 2 * this_scroll_margin > height)
15600 aggressive_scroll = height - 2 * this_scroll_margin;
15601 amount_to_scroll = dy + aggressive_scroll;
15605 if (amount_to_scroll <= 0)
15606 return SCROLLING_FAILED;
15608 start_display (&it, w, startp);
15609 if (arg_scroll_conservatively <= scroll_limit)
15610 move_it_vertically (&it, amount_to_scroll);
15611 else
15613 /* Extra precision for users who set scroll-conservatively
15614 to a large number: make sure the amount we scroll
15615 the window start is never less than amount_to_scroll,
15616 which was computed as distance from window bottom to
15617 point. This matters when lines at window top and lines
15618 below window bottom have different height. */
15619 struct it it1;
15620 void *it1data = NULL;
15621 /* We use a temporary it1 because line_bottom_y can modify
15622 its argument, if it moves one line down; see there. */
15623 int start_y;
15625 SAVE_IT (it1, it, it1data);
15626 start_y = line_bottom_y (&it1);
15627 do {
15628 RESTORE_IT (&it, &it, it1data);
15629 move_it_by_lines (&it, 1);
15630 SAVE_IT (it1, it, it1data);
15631 } while (IT_CHARPOS (it) < ZV
15632 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15633 bidi_unshelve_cache (it1data, true);
15636 /* If STARTP is unchanged, move it down another screen line. */
15637 if (IT_CHARPOS (it) == CHARPOS (startp))
15638 move_it_by_lines (&it, 1);
15639 startp = it.current.pos;
15641 else
15643 struct text_pos scroll_margin_pos = startp;
15644 int y_offset = 0;
15646 /* See if point is inside the scroll margin at the top of the
15647 window. */
15648 if (this_scroll_margin)
15650 int y_start;
15652 start_display (&it, w, startp);
15653 y_start = it.current_y;
15654 move_it_vertically (&it, this_scroll_margin);
15655 scroll_margin_pos = it.current.pos;
15656 /* If we didn't move enough before hitting ZV, request
15657 additional amount of scroll, to move point out of the
15658 scroll margin. */
15659 if (IT_CHARPOS (it) == ZV
15660 && it.current_y - y_start < this_scroll_margin)
15661 y_offset = this_scroll_margin - (it.current_y - y_start);
15664 if (PT < CHARPOS (scroll_margin_pos))
15666 /* Point is in the scroll margin at the top of the window or
15667 above what is displayed in the window. */
15668 int y0, y_to_move;
15670 /* Compute the vertical distance from PT to the scroll
15671 margin position. Move as far as scroll_max allows, or
15672 one screenful, or 10 screen lines, whichever is largest.
15673 Give up if distance is greater than scroll_max or if we
15674 didn't reach the scroll margin position. */
15675 SET_TEXT_POS (pos, PT, PT_BYTE);
15676 start_display (&it, w, pos);
15677 y0 = it.current_y;
15678 y_to_move = max (it.last_visible_y,
15679 max (scroll_max, 10 * frame_line_height));
15680 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15681 y_to_move, -1,
15682 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15683 dy = it.current_y - y0;
15684 if (dy > scroll_max
15685 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15686 return SCROLLING_FAILED;
15688 /* Additional scroll for when ZV was too close to point. */
15689 dy += y_offset;
15691 /* Compute new window start. */
15692 start_display (&it, w, startp);
15694 if (arg_scroll_conservatively)
15695 amount_to_scroll = max (dy, frame_line_height
15696 * max (scroll_step, temp_scroll_step));
15697 else if (scroll_step || temp_scroll_step)
15698 amount_to_scroll = scroll_max;
15699 else
15701 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15702 height = WINDOW_BOX_TEXT_HEIGHT (w);
15703 if (NUMBERP (aggressive))
15705 double float_amount = XFLOATINT (aggressive) * height;
15706 int aggressive_scroll = float_amount;
15707 if (aggressive_scroll == 0 && float_amount > 0)
15708 aggressive_scroll = 1;
15709 /* Don't let point enter the scroll margin near
15710 bottom of the window, if the value of
15711 scroll_down_aggressively happens to be too
15712 large. */
15713 if (aggressive_scroll + 2 * this_scroll_margin > height)
15714 aggressive_scroll = height - 2 * this_scroll_margin;
15715 amount_to_scroll = dy + aggressive_scroll;
15719 if (amount_to_scroll <= 0)
15720 return SCROLLING_FAILED;
15722 move_it_vertically_backward (&it, amount_to_scroll);
15723 startp = it.current.pos;
15727 /* Run window scroll functions. */
15728 startp = run_window_scroll_functions (window, startp);
15730 /* Display the window. Give up if new fonts are loaded, or if point
15731 doesn't appear. */
15732 if (!try_window (window, startp, 0))
15733 rc = SCROLLING_NEED_LARGER_MATRICES;
15734 else if (w->cursor.vpos < 0)
15736 clear_glyph_matrix (w->desired_matrix);
15737 rc = SCROLLING_FAILED;
15739 else
15741 /* Maybe forget recorded base line for line number display. */
15742 if (!just_this_one_p
15743 || current_buffer->clip_changed
15744 || BEG_UNCHANGED < CHARPOS (startp))
15745 w->base_line_number = 0;
15747 /* If cursor ends up on a partially visible line,
15748 treat that as being off the bottom of the screen. */
15749 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15750 false)
15751 /* It's possible that the cursor is on the first line of the
15752 buffer, which is partially obscured due to a vscroll
15753 (Bug#7537). In that case, avoid looping forever. */
15754 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15756 clear_glyph_matrix (w->desired_matrix);
15757 ++extra_scroll_margin_lines;
15758 goto too_near_end;
15760 rc = SCROLLING_SUCCESS;
15763 return rc;
15767 /* Compute a suitable window start for window W if display of W starts
15768 on a continuation line. Value is true if a new window start
15769 was computed.
15771 The new window start will be computed, based on W's width, starting
15772 from the start of the continued line. It is the start of the
15773 screen line with the minimum distance from the old start W->start,
15774 which is still before point (otherwise point will definitely not
15775 be visible in the window). */
15777 static bool
15778 compute_window_start_on_continuation_line (struct window *w)
15780 struct text_pos pos, start_pos, pos_before_pt;
15781 bool window_start_changed_p = false;
15783 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15785 /* If window start is on a continuation line... Window start may be
15786 < BEGV in case there's invisible text at the start of the
15787 buffer (M-x rmail, for example). */
15788 if (CHARPOS (start_pos) > BEGV
15789 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15791 struct it it;
15792 struct glyph_row *row;
15794 /* Handle the case that the window start is out of range. */
15795 if (CHARPOS (start_pos) < BEGV)
15796 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15797 else if (CHARPOS (start_pos) > ZV)
15798 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15800 /* Find the start of the continued line. This should be fast
15801 because find_newline is fast (newline cache). */
15802 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15803 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15804 row, DEFAULT_FACE_ID);
15805 reseat_at_previous_visible_line_start (&it);
15807 /* If the line start is "too far" away from the window start,
15808 say it takes too much time to compute a new window start.
15809 Also, give up if the line start is after point, as in that
15810 case point will not be visible with any window start we
15811 compute. */
15812 if (IT_CHARPOS (it) <= PT
15813 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15814 /* PXW: Do we need upper bounds here? */
15815 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15817 int min_distance, distance;
15819 /* Move forward by display lines to find the new window
15820 start. If window width was enlarged, the new start can
15821 be expected to be > the old start. If window width was
15822 decreased, the new window start will be < the old start.
15823 So, we're looking for the display line start with the
15824 minimum distance from the old window start. */
15825 pos_before_pt = pos = it.current.pos;
15826 min_distance = INFINITY;
15827 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15828 distance < min_distance)
15830 min_distance = distance;
15831 if (CHARPOS (pos) <= PT)
15832 pos_before_pt = pos;
15833 pos = it.current.pos;
15834 if (it.line_wrap == WORD_WRAP)
15836 /* Under WORD_WRAP, move_it_by_lines is likely to
15837 overshoot and stop not at the first, but the
15838 second character from the left margin. So in
15839 that case, we need a more tight control on the X
15840 coordinate of the iterator than move_it_by_lines
15841 promises in its contract. The method is to first
15842 go to the last (rightmost) visible character of a
15843 line, then move to the leftmost character on the
15844 next line in a separate call. */
15845 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15846 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15847 move_it_to (&it, ZV, 0,
15848 it.current_y + it.max_ascent + it.max_descent, -1,
15849 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15851 else
15852 move_it_by_lines (&it, 1);
15855 /* It makes very little sense to make the new window start
15856 after point, as point won't be visible. If that's what
15857 the loop above finds, fall back on the candidate before
15858 or at point that is closest to the old window start. */
15859 if (CHARPOS (pos) > PT)
15860 pos = pos_before_pt;
15862 /* Set the window start there. */
15863 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15864 window_start_changed_p = true;
15868 return window_start_changed_p;
15872 /* Try cursor movement in case text has not changed in window WINDOW,
15873 with window start STARTP. Value is
15875 CURSOR_MOVEMENT_SUCCESS if successful
15877 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15879 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15880 display. *SCROLL_STEP is set to true, under certain circumstances, if
15881 we want to scroll as if scroll-step were set to 1. See the code.
15883 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15884 which case we have to abort this redisplay, and adjust matrices
15885 first. */
15887 enum
15889 CURSOR_MOVEMENT_SUCCESS,
15890 CURSOR_MOVEMENT_CANNOT_BE_USED,
15891 CURSOR_MOVEMENT_MUST_SCROLL,
15892 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15895 static int
15896 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15897 bool *scroll_step)
15899 struct window *w = XWINDOW (window);
15900 struct frame *f = XFRAME (w->frame);
15901 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15903 #ifdef GLYPH_DEBUG
15904 if (inhibit_try_cursor_movement)
15905 return rc;
15906 #endif
15908 /* Previously, there was a check for Lisp integer in the
15909 if-statement below. Now, this field is converted to
15910 ptrdiff_t, thus zero means invalid position in a buffer. */
15911 eassert (w->last_point > 0);
15912 /* Likewise there was a check whether window_end_vpos is nil or larger
15913 than the window. Now window_end_vpos is int and so never nil, but
15914 let's leave eassert to check whether it fits in the window. */
15915 eassert (!w->window_end_valid
15916 || w->window_end_vpos < w->current_matrix->nrows);
15918 /* Handle case where text has not changed, only point, and it has
15919 not moved off the frame. */
15920 if (/* Point may be in this window. */
15921 PT >= CHARPOS (startp)
15922 /* Selective display hasn't changed. */
15923 && !current_buffer->clip_changed
15924 /* Function force-mode-line-update is used to force a thorough
15925 redisplay. It sets either windows_or_buffers_changed or
15926 update_mode_lines. So don't take a shortcut here for these
15927 cases. */
15928 && !update_mode_lines
15929 && !windows_or_buffers_changed
15930 && !f->cursor_type_changed
15931 && NILP (Vshow_trailing_whitespace)
15932 /* This code is not used for mini-buffer for the sake of the case
15933 of redisplaying to replace an echo area message; since in
15934 that case the mini-buffer contents per se are usually
15935 unchanged. This code is of no real use in the mini-buffer
15936 since the handling of this_line_start_pos, etc., in redisplay
15937 handles the same cases. */
15938 && !EQ (window, minibuf_window)
15939 && (FRAME_WINDOW_P (f)
15940 || !overlay_arrow_in_current_buffer_p ()))
15942 int this_scroll_margin, top_scroll_margin;
15943 struct glyph_row *row = NULL;
15945 #ifdef GLYPH_DEBUG
15946 debug_method_add (w, "cursor movement");
15947 #endif
15949 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15951 top_scroll_margin = this_scroll_margin;
15952 if (WINDOW_WANTS_HEADER_LINE_P (w))
15953 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15955 /* Start with the row the cursor was displayed during the last
15956 not paused redisplay. Give up if that row is not valid. */
15957 if (w->last_cursor_vpos < 0
15958 || w->last_cursor_vpos >= w->current_matrix->nrows)
15959 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15960 else
15962 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15963 if (row->mode_line_p)
15964 ++row;
15965 if (!row->enabled_p)
15966 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15969 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15971 bool scroll_p = false, must_scroll = false;
15972 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15974 if (PT > w->last_point)
15976 /* Point has moved forward. */
15977 while (MATRIX_ROW_END_CHARPOS (row) < PT
15978 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15980 eassert (row->enabled_p);
15981 ++row;
15984 /* If the end position of a row equals the start
15985 position of the next row, and PT is at that position,
15986 we would rather display cursor in the next line. */
15987 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15988 && MATRIX_ROW_END_CHARPOS (row) == PT
15989 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15990 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15991 && !cursor_row_p (row))
15992 ++row;
15994 /* If within the scroll margin, scroll. Note that
15995 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15996 the next line would be drawn, and that
15997 this_scroll_margin can be zero. */
15998 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15999 || PT > MATRIX_ROW_END_CHARPOS (row)
16000 /* Line is completely visible last line in window
16001 and PT is to be set in the next line. */
16002 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16003 && PT == MATRIX_ROW_END_CHARPOS (row)
16004 && !row->ends_at_zv_p
16005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16006 scroll_p = true;
16008 else if (PT < w->last_point)
16010 /* Cursor has to be moved backward. Note that PT >=
16011 CHARPOS (startp) because of the outer if-statement. */
16012 while (!row->mode_line_p
16013 && (MATRIX_ROW_START_CHARPOS (row) > PT
16014 || (MATRIX_ROW_START_CHARPOS (row) == PT
16015 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16016 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16017 row > w->current_matrix->rows
16018 && (row-1)->ends_in_newline_from_string_p))))
16019 && (row->y > top_scroll_margin
16020 || CHARPOS (startp) == BEGV))
16022 eassert (row->enabled_p);
16023 --row;
16026 /* Consider the following case: Window starts at BEGV,
16027 there is invisible, intangible text at BEGV, so that
16028 display starts at some point START > BEGV. It can
16029 happen that we are called with PT somewhere between
16030 BEGV and START. Try to handle that case. */
16031 if (row < w->current_matrix->rows
16032 || row->mode_line_p)
16034 row = w->current_matrix->rows;
16035 if (row->mode_line_p)
16036 ++row;
16039 /* Due to newlines in overlay strings, we may have to
16040 skip forward over overlay strings. */
16041 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16042 && MATRIX_ROW_END_CHARPOS (row) == PT
16043 && !cursor_row_p (row))
16044 ++row;
16046 /* If within the scroll margin, scroll. */
16047 if (row->y < top_scroll_margin
16048 && CHARPOS (startp) != BEGV)
16049 scroll_p = true;
16051 else
16053 /* Cursor did not move. So don't scroll even if cursor line
16054 is partially visible, as it was so before. */
16055 rc = CURSOR_MOVEMENT_SUCCESS;
16058 if (PT < MATRIX_ROW_START_CHARPOS (row)
16059 || PT > MATRIX_ROW_END_CHARPOS (row))
16061 /* if PT is not in the glyph row, give up. */
16062 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16063 must_scroll = true;
16065 else if (rc != CURSOR_MOVEMENT_SUCCESS
16066 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16068 struct glyph_row *row1;
16070 /* If rows are bidi-reordered and point moved, back up
16071 until we find a row that does not belong to a
16072 continuation line. This is because we must consider
16073 all rows of a continued line as candidates for the
16074 new cursor positioning, since row start and end
16075 positions change non-linearly with vertical position
16076 in such rows. */
16077 /* FIXME: Revisit this when glyph ``spilling'' in
16078 continuation lines' rows is implemented for
16079 bidi-reordered rows. */
16080 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16081 MATRIX_ROW_CONTINUATION_LINE_P (row);
16082 --row)
16084 /* If we hit the beginning of the displayed portion
16085 without finding the first row of a continued
16086 line, give up. */
16087 if (row <= row1)
16089 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16090 break;
16092 eassert (row->enabled_p);
16095 if (must_scroll)
16097 else if (rc != CURSOR_MOVEMENT_SUCCESS
16098 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16099 /* Make sure this isn't a header line by any chance, since
16100 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16101 && !row->mode_line_p
16102 && make_cursor_line_fully_visible_p)
16104 if (PT == MATRIX_ROW_END_CHARPOS (row)
16105 && !row->ends_at_zv_p
16106 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16108 else if (row->height > window_box_height (w))
16110 /* If we end up in a partially visible line, let's
16111 make it fully visible, except when it's taller
16112 than the window, in which case we can't do much
16113 about it. */
16114 *scroll_step = true;
16115 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16117 else
16119 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16120 if (!cursor_row_fully_visible_p (w, false, true))
16121 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16122 else
16123 rc = CURSOR_MOVEMENT_SUCCESS;
16126 else if (scroll_p)
16127 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16128 else if (rc != CURSOR_MOVEMENT_SUCCESS
16129 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16131 /* With bidi-reordered rows, there could be more than
16132 one candidate row whose start and end positions
16133 occlude point. We need to let set_cursor_from_row
16134 find the best candidate. */
16135 /* FIXME: Revisit this when glyph ``spilling'' in
16136 continuation lines' rows is implemented for
16137 bidi-reordered rows. */
16138 bool rv = false;
16142 bool at_zv_p = false, exact_match_p = false;
16144 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16145 && PT <= MATRIX_ROW_END_CHARPOS (row)
16146 && cursor_row_p (row))
16147 rv |= set_cursor_from_row (w, row, w->current_matrix,
16148 0, 0, 0, 0);
16149 /* As soon as we've found the exact match for point,
16150 or the first suitable row whose ends_at_zv_p flag
16151 is set, we are done. */
16152 if (rv)
16154 at_zv_p = MATRIX_ROW (w->current_matrix,
16155 w->cursor.vpos)->ends_at_zv_p;
16156 if (!at_zv_p
16157 && w->cursor.hpos >= 0
16158 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16159 w->cursor.vpos))
16161 struct glyph_row *candidate =
16162 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16163 struct glyph *g =
16164 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16165 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16167 exact_match_p =
16168 (BUFFERP (g->object) && g->charpos == PT)
16169 || (NILP (g->object)
16170 && (g->charpos == PT
16171 || (g->charpos == 0 && endpos - 1 == PT)));
16173 if (at_zv_p || exact_match_p)
16175 rc = CURSOR_MOVEMENT_SUCCESS;
16176 break;
16179 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16180 break;
16181 ++row;
16183 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16184 || row->continued_p)
16185 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16186 || (MATRIX_ROW_START_CHARPOS (row) == PT
16187 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16188 /* If we didn't find any candidate rows, or exited the
16189 loop before all the candidates were examined, signal
16190 to the caller that this method failed. */
16191 if (rc != CURSOR_MOVEMENT_SUCCESS
16192 && !(rv
16193 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16194 && !row->continued_p))
16195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16196 else if (rv)
16197 rc = CURSOR_MOVEMENT_SUCCESS;
16199 else
16203 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16205 rc = CURSOR_MOVEMENT_SUCCESS;
16206 break;
16208 ++row;
16210 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16211 && MATRIX_ROW_START_CHARPOS (row) == PT
16212 && cursor_row_p (row));
16217 return rc;
16221 void
16222 set_vertical_scroll_bar (struct window *w)
16224 ptrdiff_t start, end, whole;
16226 /* Calculate the start and end positions for the current window.
16227 At some point, it would be nice to choose between scrollbars
16228 which reflect the whole buffer size, with special markers
16229 indicating narrowing, and scrollbars which reflect only the
16230 visible region.
16232 Note that mini-buffers sometimes aren't displaying any text. */
16233 if (!MINI_WINDOW_P (w)
16234 || (w == XWINDOW (minibuf_window)
16235 && NILP (echo_area_buffer[0])))
16237 struct buffer *buf = XBUFFER (w->contents);
16238 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16239 start = marker_position (w->start) - BUF_BEGV (buf);
16240 /* I don't think this is guaranteed to be right. For the
16241 moment, we'll pretend it is. */
16242 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16244 if (end < start)
16245 end = start;
16246 if (whole < (end - start))
16247 whole = end - start;
16249 else
16250 start = end = whole = 0;
16252 /* Indicate what this scroll bar ought to be displaying now. */
16253 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16254 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16255 (w, end - start, whole, start);
16259 void
16260 set_horizontal_scroll_bar (struct window *w)
16262 int start, end, whole, portion;
16264 if (!MINI_WINDOW_P (w)
16265 || (w == XWINDOW (minibuf_window)
16266 && NILP (echo_area_buffer[0])))
16268 struct buffer *b = XBUFFER (w->contents);
16269 struct buffer *old_buffer = NULL;
16270 struct it it;
16271 struct text_pos startp;
16273 if (b != current_buffer)
16275 old_buffer = current_buffer;
16276 set_buffer_internal (b);
16279 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16280 start_display (&it, w, startp);
16281 it.last_visible_x = INT_MAX;
16282 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16283 MOVE_TO_X | MOVE_TO_Y);
16284 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16285 window_box_height (w), -1,
16286 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16288 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16289 end = start + window_box_width (w, TEXT_AREA);
16290 portion = end - start;
16291 /* After enlarging a horizontally scrolled window such that it
16292 gets at least as wide as the text it contains, make sure that
16293 the thumb doesn't fill the entire scroll bar so we can still
16294 drag it back to see the entire text. */
16295 whole = max (whole, end);
16297 if (it.bidi_p)
16299 Lisp_Object pdir;
16301 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16302 if (EQ (pdir, Qright_to_left))
16304 start = whole - end;
16305 end = start + portion;
16309 if (old_buffer)
16310 set_buffer_internal (old_buffer);
16312 else
16313 start = end = whole = portion = 0;
16315 w->hscroll_whole = whole;
16317 /* Indicate what this scroll bar ought to be displaying now. */
16318 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16319 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16320 (w, portion, whole, start);
16324 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16325 selected_window is redisplayed.
16327 We can return without actually redisplaying the window if fonts has been
16328 changed on window's frame. In that case, redisplay_internal will retry.
16330 As one of the important parts of redisplaying a window, we need to
16331 decide whether the previous window-start position (stored in the
16332 window's w->start marker position) is still valid, and if it isn't,
16333 recompute it. Some details about that:
16335 . The previous window-start could be in a continuation line, in
16336 which case we need to recompute it when the window width
16337 changes. See compute_window_start_on_continuation_line and its
16338 call below.
16340 . The text that changed since last redisplay could include the
16341 previous window-start position. In that case, we try to salvage
16342 what we can from the current glyph matrix by calling
16343 try_scrolling, which see.
16345 . Some Emacs command could force us to use a specific window-start
16346 position by setting the window's force_start flag, or gently
16347 propose doing that by setting the window's optional_new_start
16348 flag. In these cases, we try using the specified start point if
16349 that succeeds (i.e. the window desired matrix is successfully
16350 recomputed, and point location is within the window). In case
16351 of optional_new_start, we first check if the specified start
16352 position is feasible, i.e. if it will allow point to be
16353 displayed in the window. If using the specified start point
16354 fails, e.g., if new fonts are needed to be loaded, we abort the
16355 redisplay cycle and leave it up to the next cycle to figure out
16356 things.
16358 . Note that the window's force_start flag is sometimes set by
16359 redisplay itself, when it decides that the previous window start
16360 point is fine and should be kept. Search for "goto force_start"
16361 below to see the details. Like the values of window-start
16362 specified outside of redisplay, these internally-deduced values
16363 are tested for feasibility, and ignored if found to be
16364 unfeasible.
16366 . Note that the function try_window, used to completely redisplay
16367 a window, accepts the window's start point as its argument.
16368 This is used several times in the redisplay code to control
16369 where the window start will be, according to user options such
16370 as scroll-conservatively, and also to ensure the screen line
16371 showing point will be fully (as opposed to partially) visible on
16372 display. */
16374 static void
16375 redisplay_window (Lisp_Object window, bool just_this_one_p)
16377 struct window *w = XWINDOW (window);
16378 struct frame *f = XFRAME (w->frame);
16379 struct buffer *buffer = XBUFFER (w->contents);
16380 struct buffer *old = current_buffer;
16381 struct text_pos lpoint, opoint, startp;
16382 bool update_mode_line;
16383 int tem;
16384 struct it it;
16385 /* Record it now because it's overwritten. */
16386 bool current_matrix_up_to_date_p = false;
16387 bool used_current_matrix_p = false;
16388 /* This is less strict than current_matrix_up_to_date_p.
16389 It indicates that the buffer contents and narrowing are unchanged. */
16390 bool buffer_unchanged_p = false;
16391 bool temp_scroll_step = false;
16392 ptrdiff_t count = SPECPDL_INDEX ();
16393 int rc;
16394 int centering_position = -1;
16395 bool last_line_misfit = false;
16396 ptrdiff_t beg_unchanged, end_unchanged;
16397 int frame_line_height, margin;
16398 bool use_desired_matrix;
16399 void *itdata = NULL;
16401 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16402 opoint = lpoint;
16404 #ifdef GLYPH_DEBUG
16405 *w->desired_matrix->method = 0;
16406 #endif
16408 if (!just_this_one_p
16409 && REDISPLAY_SOME_P ()
16410 && !w->redisplay
16411 && !w->update_mode_line
16412 && !f->face_change
16413 && !f->redisplay
16414 && !buffer->text->redisplay
16415 && BUF_PT (buffer) == w->last_point)
16416 return;
16418 /* Make sure that both W's markers are valid. */
16419 eassert (XMARKER (w->start)->buffer == buffer);
16420 eassert (XMARKER (w->pointm)->buffer == buffer);
16422 /* We come here again if we need to run window-text-change-functions
16423 below. */
16424 restart:
16425 reconsider_clip_changes (w);
16426 frame_line_height = default_line_pixel_height (w);
16427 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16430 /* Has the mode line to be updated? */
16431 update_mode_line = (w->update_mode_line
16432 || update_mode_lines
16433 || buffer->clip_changed
16434 || buffer->prevent_redisplay_optimizations_p);
16436 if (!just_this_one_p)
16437 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16438 cleverly elsewhere. */
16439 w->must_be_updated_p = true;
16441 if (MINI_WINDOW_P (w))
16443 if (w == XWINDOW (echo_area_window)
16444 && !NILP (echo_area_buffer[0]))
16446 if (update_mode_line)
16447 /* We may have to update a tty frame's menu bar or a
16448 tool-bar. Example `M-x C-h C-h C-g'. */
16449 goto finish_menu_bars;
16450 else
16451 /* We've already displayed the echo area glyphs in this window. */
16452 goto finish_scroll_bars;
16454 else if ((w != XWINDOW (minibuf_window)
16455 || minibuf_level == 0)
16456 /* When buffer is nonempty, redisplay window normally. */
16457 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16458 /* Quail displays non-mini buffers in minibuffer window.
16459 In that case, redisplay the window normally. */
16460 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16462 /* W is a mini-buffer window, but it's not active, so clear
16463 it. */
16464 int yb = window_text_bottom_y (w);
16465 struct glyph_row *row;
16466 int y;
16468 for (y = 0, row = w->desired_matrix->rows;
16469 y < yb;
16470 y += row->height, ++row)
16471 blank_row (w, row, y);
16472 goto finish_scroll_bars;
16475 clear_glyph_matrix (w->desired_matrix);
16478 /* Otherwise set up data on this window; select its buffer and point
16479 value. */
16480 /* Really select the buffer, for the sake of buffer-local
16481 variables. */
16482 set_buffer_internal_1 (XBUFFER (w->contents));
16484 current_matrix_up_to_date_p
16485 = (w->window_end_valid
16486 && !current_buffer->clip_changed
16487 && !current_buffer->prevent_redisplay_optimizations_p
16488 && !window_outdated (w)
16489 && !hscrolling_current_line_p (w));
16491 /* Run the window-text-change-functions
16492 if it is possible that the text on the screen has changed
16493 (either due to modification of the text, or any other reason). */
16494 if (!current_matrix_up_to_date_p
16495 && !NILP (Vwindow_text_change_functions))
16497 safe_run_hooks (Qwindow_text_change_functions);
16498 goto restart;
16501 beg_unchanged = BEG_UNCHANGED;
16502 end_unchanged = END_UNCHANGED;
16504 SET_TEXT_POS (opoint, PT, PT_BYTE);
16506 specbind (Qinhibit_point_motion_hooks, Qt);
16508 buffer_unchanged_p
16509 = (w->window_end_valid
16510 && !current_buffer->clip_changed
16511 && !window_outdated (w));
16513 /* When windows_or_buffers_changed is non-zero, we can't rely
16514 on the window end being valid, so set it to zero there. */
16515 if (windows_or_buffers_changed)
16517 /* If window starts on a continuation line, maybe adjust the
16518 window start in case the window's width changed. */
16519 if (XMARKER (w->start)->buffer == current_buffer)
16520 compute_window_start_on_continuation_line (w);
16522 w->window_end_valid = false;
16523 /* If so, we also can't rely on current matrix
16524 and should not fool try_cursor_movement below. */
16525 current_matrix_up_to_date_p = false;
16528 /* Some sanity checks. */
16529 CHECK_WINDOW_END (w);
16530 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16531 emacs_abort ();
16532 if (BYTEPOS (opoint) < CHARPOS (opoint))
16533 emacs_abort ();
16535 if (mode_line_update_needed (w))
16536 update_mode_line = true;
16538 /* Point refers normally to the selected window. For any other
16539 window, set up appropriate value. */
16540 if (!EQ (window, selected_window))
16542 ptrdiff_t new_pt = marker_position (w->pointm);
16543 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16545 if (new_pt < BEGV)
16547 new_pt = BEGV;
16548 new_pt_byte = BEGV_BYTE;
16549 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16551 else if (new_pt > (ZV - 1))
16553 new_pt = ZV;
16554 new_pt_byte = ZV_BYTE;
16555 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16558 /* We don't use SET_PT so that the point-motion hooks don't run. */
16559 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16562 /* If any of the character widths specified in the display table
16563 have changed, invalidate the width run cache. It's true that
16564 this may be a bit late to catch such changes, but the rest of
16565 redisplay goes (non-fatally) haywire when the display table is
16566 changed, so why should we worry about doing any better? */
16567 if (current_buffer->width_run_cache
16568 || (current_buffer->base_buffer
16569 && current_buffer->base_buffer->width_run_cache))
16571 struct Lisp_Char_Table *disptab = buffer_display_table ();
16573 if (! disptab_matches_widthtab
16574 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16576 struct buffer *buf = current_buffer;
16578 if (buf->base_buffer)
16579 buf = buf->base_buffer;
16580 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16581 recompute_width_table (current_buffer, disptab);
16585 /* If window-start is screwed up, choose a new one. */
16586 if (XMARKER (w->start)->buffer != current_buffer)
16587 goto recenter;
16589 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16591 /* If someone specified a new starting point but did not insist,
16592 check whether it can be used. */
16593 if ((w->optional_new_start || window_frozen_p (w))
16594 && CHARPOS (startp) >= BEGV
16595 && CHARPOS (startp) <= ZV)
16597 ptrdiff_t it_charpos;
16599 w->optional_new_start = false;
16600 start_display (&it, w, startp);
16601 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16602 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16603 /* Record IT's position now, since line_bottom_y might change
16604 that. */
16605 it_charpos = IT_CHARPOS (it);
16606 /* Make sure we set the force_start flag only if the cursor row
16607 will be fully visible. Otherwise, the code under force_start
16608 label below will try to move point back into view, which is
16609 not what the code which sets optional_new_start wants. */
16610 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16611 && !w->force_start)
16613 if (it_charpos == PT)
16614 w->force_start = true;
16615 /* IT may overshoot PT if text at PT is invisible. */
16616 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16617 w->force_start = true;
16618 #ifdef GLYPH_DEBUG
16619 if (w->force_start)
16621 if (window_frozen_p (w))
16622 debug_method_add (w, "set force_start from frozen window start");
16623 else
16624 debug_method_add (w, "set force_start from optional_new_start");
16626 #endif
16630 force_start:
16632 /* Handle case where place to start displaying has been specified,
16633 unless the specified location is outside the accessible range. */
16634 if (w->force_start)
16636 /* We set this later on if we have to adjust point. */
16637 int new_vpos = -1;
16639 w->force_start = false;
16640 w->vscroll = 0;
16641 w->window_end_valid = false;
16643 /* Forget any recorded base line for line number display. */
16644 if (!buffer_unchanged_p)
16645 w->base_line_number = 0;
16647 /* Redisplay the mode line. Select the buffer properly for that.
16648 Also, run the hook window-scroll-functions
16649 because we have scrolled. */
16650 /* Note, we do this after clearing force_start because
16651 if there's an error, it is better to forget about force_start
16652 than to get into an infinite loop calling the hook functions
16653 and having them get more errors. */
16654 if (!update_mode_line
16655 || ! NILP (Vwindow_scroll_functions))
16657 update_mode_line = true;
16658 w->update_mode_line = true;
16659 startp = run_window_scroll_functions (window, startp);
16662 if (CHARPOS (startp) < BEGV)
16663 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16664 else if (CHARPOS (startp) > ZV)
16665 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16667 /* Redisplay, then check if cursor has been set during the
16668 redisplay. Give up if new fonts were loaded. */
16669 /* We used to issue a CHECK_MARGINS argument to try_window here,
16670 but this causes scrolling to fail when point begins inside
16671 the scroll margin (bug#148) -- cyd */
16672 if (!try_window (window, startp, 0))
16674 w->force_start = true;
16675 clear_glyph_matrix (w->desired_matrix);
16676 goto need_larger_matrices;
16679 if (w->cursor.vpos < 0)
16681 /* If point does not appear, try to move point so it does
16682 appear. The desired matrix has been built above, so we
16683 can use it here. First see if point is in invisible
16684 text, and if so, move it to the first visible buffer
16685 position past that. */
16686 struct glyph_row *r = NULL;
16687 Lisp_Object invprop =
16688 get_char_property_and_overlay (make_number (PT), Qinvisible,
16689 Qnil, NULL);
16691 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16693 ptrdiff_t alt_pt;
16694 Lisp_Object invprop_end =
16695 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16696 Qnil, Qnil);
16698 if (NATNUMP (invprop_end))
16699 alt_pt = XFASTINT (invprop_end);
16700 else
16701 alt_pt = ZV;
16702 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16703 NULL, 0);
16705 if (r)
16706 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16707 else /* Give up and just move to the middle of the window. */
16708 new_vpos = window_box_height (w) / 2;
16711 if (!cursor_row_fully_visible_p (w, false, false))
16713 /* Point does appear, but on a line partly visible at end of window.
16714 Move it back to a fully-visible line. */
16715 new_vpos = window_box_height (w);
16716 /* But if window_box_height suggests a Y coordinate that is
16717 not less than we already have, that line will clearly not
16718 be fully visible, so give up and scroll the display.
16719 This can happen when the default face uses a font whose
16720 dimensions are different from the frame's default
16721 font. */
16722 if (new_vpos >= w->cursor.y)
16724 w->cursor.vpos = -1;
16725 clear_glyph_matrix (w->desired_matrix);
16726 goto try_to_scroll;
16729 else if (w->cursor.vpos >= 0)
16731 /* Some people insist on not letting point enter the scroll
16732 margin, even though this part handles windows that didn't
16733 scroll at all. */
16734 int pixel_margin = margin * frame_line_height;
16735 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16737 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16738 below, which finds the row to move point to, advances by
16739 the Y coordinate of the _next_ row, see the definition of
16740 MATRIX_ROW_BOTTOM_Y. */
16741 if (w->cursor.vpos < margin + header_line)
16743 w->cursor.vpos = -1;
16744 clear_glyph_matrix (w->desired_matrix);
16745 goto try_to_scroll;
16747 else
16749 int window_height = window_box_height (w);
16751 if (header_line)
16752 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16753 if (w->cursor.y >= window_height - pixel_margin)
16755 w->cursor.vpos = -1;
16756 clear_glyph_matrix (w->desired_matrix);
16757 goto try_to_scroll;
16762 /* If we need to move point for either of the above reasons,
16763 now actually do it. */
16764 if (new_vpos >= 0)
16766 struct glyph_row *row;
16768 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16769 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16770 ++row;
16772 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16773 MATRIX_ROW_START_BYTEPOS (row));
16775 if (w != XWINDOW (selected_window))
16776 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16777 else if (current_buffer == old)
16778 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16780 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16782 /* Re-run pre-redisplay-function so it can update the region
16783 according to the new position of point. */
16784 /* Other than the cursor, w's redisplay is done so we can set its
16785 redisplay to false. Also the buffer's redisplay can be set to
16786 false, since propagate_buffer_redisplay should have already
16787 propagated its info to `w' anyway. */
16788 w->redisplay = false;
16789 XBUFFER (w->contents)->text->redisplay = false;
16790 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16792 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16794 /* pre-redisplay-function made changes (e.g. move the region)
16795 that require another round of redisplay. */
16796 clear_glyph_matrix (w->desired_matrix);
16797 if (!try_window (window, startp, 0))
16798 goto need_larger_matrices;
16801 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16803 clear_glyph_matrix (w->desired_matrix);
16804 goto try_to_scroll;
16807 #ifdef GLYPH_DEBUG
16808 debug_method_add (w, "forced window start");
16809 #endif
16810 goto done;
16813 /* Handle case where text has not changed, only point, and it has
16814 not moved off the frame, and we are not retrying after hscroll.
16815 (current_matrix_up_to_date_p is true when retrying.) */
16816 if (current_matrix_up_to_date_p
16817 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16818 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16820 switch (rc)
16822 case CURSOR_MOVEMENT_SUCCESS:
16823 used_current_matrix_p = true;
16824 goto done;
16826 case CURSOR_MOVEMENT_MUST_SCROLL:
16827 goto try_to_scroll;
16829 default:
16830 emacs_abort ();
16833 /* If current starting point was originally the beginning of a line
16834 but no longer is, find a new starting point. */
16835 else if (w->start_at_line_beg
16836 && !(CHARPOS (startp) <= BEGV
16837 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16839 #ifdef GLYPH_DEBUG
16840 debug_method_add (w, "recenter 1");
16841 #endif
16842 goto recenter;
16845 /* Try scrolling with try_window_id. Value is > 0 if update has
16846 been done, it is -1 if we know that the same window start will
16847 not work. It is 0 if unsuccessful for some other reason. */
16848 else if ((tem = try_window_id (w)) != 0)
16850 #ifdef GLYPH_DEBUG
16851 debug_method_add (w, "try_window_id %d", tem);
16852 #endif
16854 if (f->fonts_changed)
16855 goto need_larger_matrices;
16856 if (tem > 0)
16857 goto done;
16859 /* Otherwise try_window_id has returned -1 which means that we
16860 don't want the alternative below this comment to execute. */
16862 else if (CHARPOS (startp) >= BEGV
16863 && CHARPOS (startp) <= ZV
16864 && PT >= CHARPOS (startp)
16865 && (CHARPOS (startp) < ZV
16866 /* Avoid starting at end of buffer. */
16867 || CHARPOS (startp) == BEGV
16868 || !window_outdated (w)))
16870 int d1, d2, d5, d6;
16871 int rtop, rbot;
16873 /* If first window line is a continuation line, and window start
16874 is inside the modified region, but the first change is before
16875 current window start, we must select a new window start.
16877 However, if this is the result of a down-mouse event (e.g. by
16878 extending the mouse-drag-overlay), we don't want to select a
16879 new window start, since that would change the position under
16880 the mouse, resulting in an unwanted mouse-movement rather
16881 than a simple mouse-click. */
16882 if (!w->start_at_line_beg
16883 && NILP (do_mouse_tracking)
16884 && CHARPOS (startp) > BEGV
16885 && CHARPOS (startp) > BEG + beg_unchanged
16886 && CHARPOS (startp) <= Z - end_unchanged
16887 /* Even if w->start_at_line_beg is nil, a new window may
16888 start at a line_beg, since that's how set_buffer_window
16889 sets it. So, we need to check the return value of
16890 compute_window_start_on_continuation_line. (See also
16891 bug#197). */
16892 && XMARKER (w->start)->buffer == current_buffer
16893 && compute_window_start_on_continuation_line (w)
16894 /* It doesn't make sense to force the window start like we
16895 do at label force_start if it is already known that point
16896 will not be fully visible in the resulting window, because
16897 doing so will move point from its correct position
16898 instead of scrolling the window to bring point into view.
16899 See bug#9324. */
16900 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16901 /* A very tall row could need more than the window height,
16902 in which case we accept that it is partially visible. */
16903 && (rtop != 0) == (rbot != 0))
16905 w->force_start = true;
16906 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16907 #ifdef GLYPH_DEBUG
16908 debug_method_add (w, "recomputed window start in continuation line");
16909 #endif
16910 goto force_start;
16913 #ifdef GLYPH_DEBUG
16914 debug_method_add (w, "same window start");
16915 #endif
16917 /* Try to redisplay starting at same place as before.
16918 If point has not moved off frame, accept the results. */
16919 if (!current_matrix_up_to_date_p
16920 /* Don't use try_window_reusing_current_matrix in this case
16921 because a window scroll function can have changed the
16922 buffer. */
16923 || !NILP (Vwindow_scroll_functions)
16924 || MINI_WINDOW_P (w)
16925 || !(used_current_matrix_p
16926 = try_window_reusing_current_matrix (w)))
16928 IF_DEBUG (debug_method_add (w, "1"));
16929 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16930 /* -1 means we need to scroll.
16931 0 means we need new matrices, but fonts_changed
16932 is set in that case, so we will detect it below. */
16933 goto try_to_scroll;
16936 if (f->fonts_changed)
16937 goto need_larger_matrices;
16939 if (w->cursor.vpos >= 0)
16941 if (!just_this_one_p
16942 || current_buffer->clip_changed
16943 || BEG_UNCHANGED < CHARPOS (startp))
16944 /* Forget any recorded base line for line number display. */
16945 w->base_line_number = 0;
16947 if (!cursor_row_fully_visible_p (w, true, false))
16949 clear_glyph_matrix (w->desired_matrix);
16950 last_line_misfit = true;
16952 /* Drop through and scroll. */
16953 else
16954 goto done;
16956 else
16957 clear_glyph_matrix (w->desired_matrix);
16960 try_to_scroll:
16962 /* Redisplay the mode line. Select the buffer properly for that. */
16963 if (!update_mode_line)
16965 update_mode_line = true;
16966 w->update_mode_line = true;
16969 /* Try to scroll by specified few lines. */
16970 if ((scroll_conservatively
16971 || emacs_scroll_step
16972 || temp_scroll_step
16973 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16974 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16975 && CHARPOS (startp) >= BEGV
16976 && CHARPOS (startp) <= ZV)
16978 /* The function returns -1 if new fonts were loaded, 1 if
16979 successful, 0 if not successful. */
16980 int ss = try_scrolling (window, just_this_one_p,
16981 scroll_conservatively,
16982 emacs_scroll_step,
16983 temp_scroll_step, last_line_misfit);
16984 switch (ss)
16986 case SCROLLING_SUCCESS:
16987 goto done;
16989 case SCROLLING_NEED_LARGER_MATRICES:
16990 goto need_larger_matrices;
16992 case SCROLLING_FAILED:
16993 break;
16995 default:
16996 emacs_abort ();
17000 /* Finally, just choose a place to start which positions point
17001 according to user preferences. */
17003 recenter:
17005 #ifdef GLYPH_DEBUG
17006 debug_method_add (w, "recenter");
17007 #endif
17009 /* Forget any previously recorded base line for line number display. */
17010 if (!buffer_unchanged_p)
17011 w->base_line_number = 0;
17013 /* Determine the window start relative to point. */
17014 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17015 it.current_y = it.last_visible_y;
17016 if (centering_position < 0)
17018 ptrdiff_t margin_pos = CHARPOS (startp);
17019 Lisp_Object aggressive;
17020 bool scrolling_up;
17022 /* If there is a scroll margin at the top of the window, find
17023 its character position. */
17024 if (margin
17025 /* Cannot call start_display if startp is not in the
17026 accessible region of the buffer. This can happen when we
17027 have just switched to a different buffer and/or changed
17028 its restriction. In that case, startp is initialized to
17029 the character position 1 (BEGV) because we did not yet
17030 have chance to display the buffer even once. */
17031 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17033 struct it it1;
17034 void *it1data = NULL;
17036 SAVE_IT (it1, it, it1data);
17037 start_display (&it1, w, startp);
17038 move_it_vertically (&it1, margin * frame_line_height);
17039 margin_pos = IT_CHARPOS (it1);
17040 RESTORE_IT (&it, &it, it1data);
17042 scrolling_up = PT > margin_pos;
17043 aggressive =
17044 scrolling_up
17045 ? BVAR (current_buffer, scroll_up_aggressively)
17046 : BVAR (current_buffer, scroll_down_aggressively);
17048 if (!MINI_WINDOW_P (w)
17049 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17051 int pt_offset = 0;
17053 /* Setting scroll-conservatively overrides
17054 scroll-*-aggressively. */
17055 if (!scroll_conservatively && NUMBERP (aggressive))
17057 double float_amount = XFLOATINT (aggressive);
17059 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17060 if (pt_offset == 0 && float_amount > 0)
17061 pt_offset = 1;
17062 if (pt_offset && margin > 0)
17063 margin -= 1;
17065 /* Compute how much to move the window start backward from
17066 point so that point will be displayed where the user
17067 wants it. */
17068 if (scrolling_up)
17070 centering_position = it.last_visible_y;
17071 if (pt_offset)
17072 centering_position -= pt_offset;
17073 centering_position -=
17074 (frame_line_height * (1 + margin + last_line_misfit)
17075 + WINDOW_HEADER_LINE_HEIGHT (w));
17076 /* Don't let point enter the scroll margin near top of
17077 the window. */
17078 if (centering_position < margin * frame_line_height)
17079 centering_position = margin * frame_line_height;
17081 else
17082 centering_position = margin * frame_line_height + pt_offset;
17084 else
17085 /* Set the window start half the height of the window backward
17086 from point. */
17087 centering_position = window_box_height (w) / 2;
17089 move_it_vertically_backward (&it, centering_position);
17091 eassert (IT_CHARPOS (it) >= BEGV);
17093 /* The function move_it_vertically_backward may move over more
17094 than the specified y-distance. If it->w is small, e.g. a
17095 mini-buffer window, we may end up in front of the window's
17096 display area. Start displaying at the start of the line
17097 containing PT in this case. */
17098 if (it.current_y <= 0)
17100 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17101 move_it_vertically_backward (&it, 0);
17102 it.current_y = 0;
17105 it.current_x = it.hpos = 0;
17107 /* Set the window start position here explicitly, to avoid an
17108 infinite loop in case the functions in window-scroll-functions
17109 get errors. */
17110 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17112 /* Run scroll hooks. */
17113 startp = run_window_scroll_functions (window, it.current.pos);
17115 /* We invoke try_window and try_window_reusing_current_matrix below,
17116 and they manipulate the bidi cache. Save and restore the cache
17117 state of our iterator, so we could continue using it after that. */
17118 itdata = bidi_shelve_cache ();
17120 /* Redisplay the window. */
17121 use_desired_matrix = false;
17122 if (!current_matrix_up_to_date_p
17123 || windows_or_buffers_changed
17124 || f->cursor_type_changed
17125 /* Don't use try_window_reusing_current_matrix in this case
17126 because it can have changed the buffer. */
17127 || !NILP (Vwindow_scroll_functions)
17128 || !just_this_one_p
17129 || MINI_WINDOW_P (w)
17130 || !(used_current_matrix_p
17131 = try_window_reusing_current_matrix (w)))
17132 use_desired_matrix = (try_window (window, startp, 0) == 1);
17134 bidi_unshelve_cache (itdata, false);
17136 /* If new fonts have been loaded (due to fontsets), give up. We
17137 have to start a new redisplay since we need to re-adjust glyph
17138 matrices. */
17139 if (f->fonts_changed)
17140 goto need_larger_matrices;
17142 /* If cursor did not appear assume that the middle of the window is
17143 in the first line of the window. Do it again with the next line.
17144 (Imagine a window of height 100, displaying two lines of height
17145 60. Moving back 50 from it->last_visible_y will end in the first
17146 line.) */
17147 if (w->cursor.vpos < 0)
17149 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17151 clear_glyph_matrix (w->desired_matrix);
17152 move_it_by_lines (&it, 1);
17153 try_window (window, it.current.pos, 0);
17155 else if (PT < IT_CHARPOS (it))
17157 clear_glyph_matrix (w->desired_matrix);
17158 move_it_by_lines (&it, -1);
17159 try_window (window, it.current.pos, 0);
17161 else if (scroll_conservatively > SCROLL_LIMIT
17162 && (it.method == GET_FROM_STRING
17163 || overlay_touches_p (IT_CHARPOS (it)))
17164 && IT_CHARPOS (it) < ZV)
17166 /* If the window starts with a before-string that spans more
17167 than one screen line, using that position to display the
17168 window might fail to bring point into the view, because
17169 start_display will always start by displaying the string,
17170 whereas the code above determines where to set w->start
17171 by the buffer position of the place where it takes screen
17172 coordinates. Try to recover by finding the next screen
17173 line that displays buffer text. */
17174 ptrdiff_t pos0 = IT_CHARPOS (it);
17176 clear_glyph_matrix (w->desired_matrix);
17177 do {
17178 move_it_by_lines (&it, 1);
17179 } while (IT_CHARPOS (it) == pos0);
17180 try_window (window, it.current.pos, 0);
17182 else
17184 /* Not much we can do about it. */
17188 /* Consider the following case: Window starts at BEGV, there is
17189 invisible, intangible text at BEGV, so that display starts at
17190 some point START > BEGV. It can happen that we are called with
17191 PT somewhere between BEGV and START. Try to handle that case,
17192 and similar ones. */
17193 if (w->cursor.vpos < 0)
17195 /* Prefer the desired matrix to the current matrix, if possible,
17196 in the fallback calculations below. This is because using
17197 the current matrix might completely goof, e.g. if its first
17198 row is after point. */
17199 struct glyph_matrix *matrix =
17200 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17201 /* First, try locating the proper glyph row for PT. */
17202 struct glyph_row *row =
17203 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17205 /* Sometimes point is at the beginning of invisible text that is
17206 before the 1st character displayed in the row. In that case,
17207 row_containing_pos fails to find the row, because no glyphs
17208 with appropriate buffer positions are present in the row.
17209 Therefore, we next try to find the row which shows the 1st
17210 position after the invisible text. */
17211 if (!row)
17213 Lisp_Object val =
17214 get_char_property_and_overlay (make_number (PT), Qinvisible,
17215 Qnil, NULL);
17217 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17219 ptrdiff_t alt_pos;
17220 Lisp_Object invis_end =
17221 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17222 Qnil, Qnil);
17224 if (NATNUMP (invis_end))
17225 alt_pos = XFASTINT (invis_end);
17226 else
17227 alt_pos = ZV;
17228 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17231 /* Finally, fall back on the first row of the window after the
17232 header line (if any). This is slightly better than not
17233 displaying the cursor at all. */
17234 if (!row)
17236 row = matrix->rows;
17237 if (row->mode_line_p)
17238 ++row;
17240 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17243 if (!cursor_row_fully_visible_p (w, false, false))
17245 /* If vscroll is enabled, disable it and try again. */
17246 if (w->vscroll)
17248 w->vscroll = 0;
17249 clear_glyph_matrix (w->desired_matrix);
17250 goto recenter;
17253 /* Users who set scroll-conservatively to a large number want
17254 point just above/below the scroll margin. If we ended up
17255 with point's row partially visible, move the window start to
17256 make that row fully visible and out of the margin. */
17257 if (scroll_conservatively > SCROLL_LIMIT)
17259 int window_total_lines
17260 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17261 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17263 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17264 clear_glyph_matrix (w->desired_matrix);
17265 if (1 == try_window (window, it.current.pos,
17266 TRY_WINDOW_CHECK_MARGINS))
17267 goto done;
17270 /* If centering point failed to make the whole line visible,
17271 put point at the top instead. That has to make the whole line
17272 visible, if it can be done. */
17273 if (centering_position == 0)
17274 goto done;
17276 clear_glyph_matrix (w->desired_matrix);
17277 centering_position = 0;
17278 goto recenter;
17281 done:
17283 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17284 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17285 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17287 /* Display the mode line, if we must. */
17288 if ((update_mode_line
17289 /* If window not full width, must redo its mode line
17290 if (a) the window to its side is being redone and
17291 (b) we do a frame-based redisplay. This is a consequence
17292 of how inverted lines are drawn in frame-based redisplay. */
17293 || (!just_this_one_p
17294 && !FRAME_WINDOW_P (f)
17295 && !WINDOW_FULL_WIDTH_P (w))
17296 /* Line number to display. */
17297 || w->base_line_pos > 0
17298 /* Column number is displayed and different from the one displayed. */
17299 || (w->column_number_displayed != -1
17300 && (w->column_number_displayed != current_column ())))
17301 /* This means that the window has a mode line. */
17302 && (WINDOW_WANTS_MODELINE_P (w)
17303 || WINDOW_WANTS_HEADER_LINE_P (w)))
17306 display_mode_lines (w);
17308 /* If mode line height has changed, arrange for a thorough
17309 immediate redisplay using the correct mode line height. */
17310 if (WINDOW_WANTS_MODELINE_P (w)
17311 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17313 f->fonts_changed = true;
17314 w->mode_line_height = -1;
17315 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17316 = DESIRED_MODE_LINE_HEIGHT (w);
17319 /* If header line height has changed, arrange for a thorough
17320 immediate redisplay using the correct header line height. */
17321 if (WINDOW_WANTS_HEADER_LINE_P (w)
17322 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17324 f->fonts_changed = true;
17325 w->header_line_height = -1;
17326 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17327 = DESIRED_HEADER_LINE_HEIGHT (w);
17330 if (f->fonts_changed)
17331 goto need_larger_matrices;
17334 if (!line_number_displayed && w->base_line_pos != -1)
17336 w->base_line_pos = 0;
17337 w->base_line_number = 0;
17340 finish_menu_bars:
17342 /* When we reach a frame's selected window, redo the frame's menu
17343 bar and the frame's title. */
17344 if (update_mode_line
17345 && EQ (FRAME_SELECTED_WINDOW (f), window))
17347 bool redisplay_menu_p;
17349 if (FRAME_WINDOW_P (f))
17351 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17352 || defined (HAVE_NS) || defined (USE_GTK)
17353 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17354 #else
17355 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17356 #endif
17358 else
17359 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17361 if (redisplay_menu_p)
17362 display_menu_bar (w);
17364 #ifdef HAVE_WINDOW_SYSTEM
17365 if (FRAME_WINDOW_P (f))
17367 #if defined (USE_GTK) || defined (HAVE_NS)
17368 if (FRAME_EXTERNAL_TOOL_BAR (f))
17369 redisplay_tool_bar (f);
17370 #else
17371 if (WINDOWP (f->tool_bar_window)
17372 && (FRAME_TOOL_BAR_LINES (f) > 0
17373 || !NILP (Vauto_resize_tool_bars))
17374 && redisplay_tool_bar (f))
17375 ignore_mouse_drag_p = true;
17376 #endif
17378 x_consider_frame_title (w->frame);
17379 #endif
17382 #ifdef HAVE_WINDOW_SYSTEM
17383 if (FRAME_WINDOW_P (f)
17384 && update_window_fringes (w, (just_this_one_p
17385 || (!used_current_matrix_p && !overlay_arrow_seen)
17386 || w->pseudo_window_p)))
17388 update_begin (f);
17389 block_input ();
17390 if (draw_window_fringes (w, true))
17392 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17393 x_draw_right_divider (w);
17394 else
17395 x_draw_vertical_border (w);
17397 unblock_input ();
17398 update_end (f);
17401 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17402 x_draw_bottom_divider (w);
17403 #endif /* HAVE_WINDOW_SYSTEM */
17405 /* We go to this label, with fonts_changed set, if it is
17406 necessary to try again using larger glyph matrices.
17407 We have to redeem the scroll bar even in this case,
17408 because the loop in redisplay_internal expects that. */
17409 need_larger_matrices:
17411 finish_scroll_bars:
17413 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17415 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17416 /* Set the thumb's position and size. */
17417 set_vertical_scroll_bar (w);
17419 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17420 /* Set the thumb's position and size. */
17421 set_horizontal_scroll_bar (w);
17423 /* Note that we actually used the scroll bar attached to this
17424 window, so it shouldn't be deleted at the end of redisplay. */
17425 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17426 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17429 /* Restore current_buffer and value of point in it. The window
17430 update may have changed the buffer, so first make sure `opoint'
17431 is still valid (Bug#6177). */
17432 if (CHARPOS (opoint) < BEGV)
17433 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17434 else if (CHARPOS (opoint) > ZV)
17435 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17436 else
17437 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17439 set_buffer_internal_1 (old);
17440 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17441 shorter. This can be caused by log truncation in *Messages*. */
17442 if (CHARPOS (lpoint) <= ZV)
17443 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17445 unbind_to (count, Qnil);
17449 /* Build the complete desired matrix of WINDOW with a window start
17450 buffer position POS.
17452 Value is 1 if successful. It is zero if fonts were loaded during
17453 redisplay which makes re-adjusting glyph matrices necessary, and -1
17454 if point would appear in the scroll margins.
17455 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17456 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17457 set in FLAGS.) */
17460 try_window (Lisp_Object window, struct text_pos pos, int flags)
17462 struct window *w = XWINDOW (window);
17463 struct it it;
17464 struct glyph_row *last_text_row = NULL;
17465 struct frame *f = XFRAME (w->frame);
17466 int cursor_vpos = w->cursor.vpos;
17468 /* Make POS the new window start. */
17469 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17471 /* Mark cursor position as unknown. No overlay arrow seen. */
17472 w->cursor.vpos = -1;
17473 overlay_arrow_seen = false;
17475 /* Initialize iterator and info to start at POS. */
17476 start_display (&it, w, pos);
17477 it.glyph_row->reversed_p = false;
17479 /* Display all lines of W. */
17480 while (it.current_y < it.last_visible_y)
17482 if (display_line (&it, cursor_vpos))
17483 last_text_row = it.glyph_row - 1;
17484 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17485 return 0;
17488 /* Save the character position of 'it' before we call
17489 'start_display' again. */
17490 ptrdiff_t it_charpos = IT_CHARPOS (it);
17492 /* Don't let the cursor end in the scroll margins. */
17493 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17494 && !MINI_WINDOW_P (w))
17496 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17497 start_display (&it, w, pos);
17499 if ((w->cursor.y >= 0 /* not vscrolled */
17500 && w->cursor.y < this_scroll_margin
17501 && CHARPOS (pos) > BEGV
17502 && it_charpos < ZV)
17503 /* rms: considering make_cursor_line_fully_visible_p here
17504 seems to give wrong results. We don't want to recenter
17505 when the last line is partly visible, we want to allow
17506 that case to be handled in the usual way. */
17507 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17508 - this_scroll_margin - 1))
17510 w->cursor.vpos = -1;
17511 clear_glyph_matrix (w->desired_matrix);
17512 return -1;
17516 /* If bottom moved off end of frame, change mode line percentage. */
17517 if (w->window_end_pos <= 0 && Z != it_charpos)
17518 w->update_mode_line = true;
17520 /* Set window_end_pos to the offset of the last character displayed
17521 on the window from the end of current_buffer. Set
17522 window_end_vpos to its row number. */
17523 if (last_text_row)
17525 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17526 adjust_window_ends (w, last_text_row, false);
17527 eassert
17528 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17529 w->window_end_vpos)));
17531 else
17533 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17534 w->window_end_pos = Z - ZV;
17535 w->window_end_vpos = 0;
17538 /* But that is not valid info until redisplay finishes. */
17539 w->window_end_valid = false;
17540 return 1;
17545 /************************************************************************
17546 Window redisplay reusing current matrix when buffer has not changed
17547 ************************************************************************/
17549 /* Try redisplay of window W showing an unchanged buffer with a
17550 different window start than the last time it was displayed by
17551 reusing its current matrix. Value is true if successful.
17552 W->start is the new window start. */
17554 static bool
17555 try_window_reusing_current_matrix (struct window *w)
17557 struct frame *f = XFRAME (w->frame);
17558 struct glyph_row *bottom_row;
17559 struct it it;
17560 struct run run;
17561 struct text_pos start, new_start;
17562 int nrows_scrolled, i;
17563 struct glyph_row *last_text_row;
17564 struct glyph_row *last_reused_text_row;
17565 struct glyph_row *start_row;
17566 int start_vpos, min_y, max_y;
17568 #ifdef GLYPH_DEBUG
17569 if (inhibit_try_window_reusing)
17570 return false;
17571 #endif
17573 if (/* This function doesn't handle terminal frames. */
17574 !FRAME_WINDOW_P (f)
17575 /* Don't try to reuse the display if windows have been split
17576 or such. */
17577 || windows_or_buffers_changed
17578 || f->cursor_type_changed)
17579 return false;
17581 /* Can't do this if showing trailing whitespace. */
17582 if (!NILP (Vshow_trailing_whitespace))
17583 return false;
17585 /* If top-line visibility has changed, give up. */
17586 if (WINDOW_WANTS_HEADER_LINE_P (w)
17587 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17588 return false;
17590 /* Give up if old or new display is scrolled vertically. We could
17591 make this function handle this, but right now it doesn't. */
17592 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17593 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17594 return false;
17596 /* The variable new_start now holds the new window start. The old
17597 start `start' can be determined from the current matrix. */
17598 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17599 start = start_row->minpos;
17600 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17602 /* Clear the desired matrix for the display below. */
17603 clear_glyph_matrix (w->desired_matrix);
17605 if (CHARPOS (new_start) <= CHARPOS (start))
17607 /* Don't use this method if the display starts with an ellipsis
17608 displayed for invisible text. It's not easy to handle that case
17609 below, and it's certainly not worth the effort since this is
17610 not a frequent case. */
17611 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17612 return false;
17614 IF_DEBUG (debug_method_add (w, "twu1"));
17616 /* Display up to a row that can be reused. The variable
17617 last_text_row is set to the last row displayed that displays
17618 text. Note that it.vpos == 0 if or if not there is a
17619 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17620 start_display (&it, w, new_start);
17621 w->cursor.vpos = -1;
17622 last_text_row = last_reused_text_row = NULL;
17624 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17626 /* If we have reached into the characters in the START row,
17627 that means the line boundaries have changed. So we
17628 can't start copying with the row START. Maybe it will
17629 work to start copying with the following row. */
17630 while (IT_CHARPOS (it) > CHARPOS (start))
17632 /* Advance to the next row as the "start". */
17633 start_row++;
17634 start = start_row->minpos;
17635 /* If there are no more rows to try, or just one, give up. */
17636 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17637 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17638 || CHARPOS (start) == ZV)
17640 clear_glyph_matrix (w->desired_matrix);
17641 return false;
17644 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17646 /* If we have reached alignment, we can copy the rest of the
17647 rows. */
17648 if (IT_CHARPOS (it) == CHARPOS (start)
17649 /* Don't accept "alignment" inside a display vector,
17650 since start_row could have started in the middle of
17651 that same display vector (thus their character
17652 positions match), and we have no way of telling if
17653 that is the case. */
17654 && it.current.dpvec_index < 0)
17655 break;
17657 it.glyph_row->reversed_p = false;
17658 if (display_line (&it, -1))
17659 last_text_row = it.glyph_row - 1;
17663 /* A value of current_y < last_visible_y means that we stopped
17664 at the previous window start, which in turn means that we
17665 have at least one reusable row. */
17666 if (it.current_y < it.last_visible_y)
17668 struct glyph_row *row;
17670 /* IT.vpos always starts from 0; it counts text lines. */
17671 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17673 /* Find PT if not already found in the lines displayed. */
17674 if (w->cursor.vpos < 0)
17676 int dy = it.current_y - start_row->y;
17678 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17679 row = row_containing_pos (w, PT, row, NULL, dy);
17680 if (row)
17681 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17682 dy, nrows_scrolled);
17683 else
17685 clear_glyph_matrix (w->desired_matrix);
17686 return false;
17690 /* Scroll the display. Do it before the current matrix is
17691 changed. The problem here is that update has not yet
17692 run, i.e. part of the current matrix is not up to date.
17693 scroll_run_hook will clear the cursor, and use the
17694 current matrix to get the height of the row the cursor is
17695 in. */
17696 run.current_y = start_row->y;
17697 run.desired_y = it.current_y;
17698 run.height = it.last_visible_y - it.current_y;
17700 if (run.height > 0 && run.current_y != run.desired_y)
17702 update_begin (f);
17703 FRAME_RIF (f)->update_window_begin_hook (w);
17704 FRAME_RIF (f)->clear_window_mouse_face (w);
17705 FRAME_RIF (f)->scroll_run_hook (w, &run);
17706 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17707 update_end (f);
17710 /* Shift current matrix down by nrows_scrolled lines. */
17711 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17712 rotate_matrix (w->current_matrix,
17713 start_vpos,
17714 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17715 nrows_scrolled);
17717 /* Disable lines that must be updated. */
17718 for (i = 0; i < nrows_scrolled; ++i)
17719 (start_row + i)->enabled_p = false;
17721 /* Re-compute Y positions. */
17722 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17723 max_y = it.last_visible_y;
17724 for (row = start_row + nrows_scrolled;
17725 row < bottom_row;
17726 ++row)
17728 row->y = it.current_y;
17729 row->visible_height = row->height;
17731 if (row->y < min_y)
17732 row->visible_height -= min_y - row->y;
17733 if (row->y + row->height > max_y)
17734 row->visible_height -= row->y + row->height - max_y;
17735 if (row->fringe_bitmap_periodic_p)
17736 row->redraw_fringe_bitmaps_p = true;
17738 it.current_y += row->height;
17740 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17741 last_reused_text_row = row;
17742 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17743 break;
17746 /* Disable lines in the current matrix which are now
17747 below the window. */
17748 for (++row; row < bottom_row; ++row)
17749 row->enabled_p = row->mode_line_p = false;
17752 /* Update window_end_pos etc.; last_reused_text_row is the last
17753 reused row from the current matrix containing text, if any.
17754 The value of last_text_row is the last displayed line
17755 containing text. */
17756 if (last_reused_text_row)
17757 adjust_window_ends (w, last_reused_text_row, true);
17758 else if (last_text_row)
17759 adjust_window_ends (w, last_text_row, false);
17760 else
17762 /* This window must be completely empty. */
17763 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17764 w->window_end_pos = Z - ZV;
17765 w->window_end_vpos = 0;
17767 w->window_end_valid = false;
17769 /* Update hint: don't try scrolling again in update_window. */
17770 w->desired_matrix->no_scrolling_p = true;
17772 #ifdef GLYPH_DEBUG
17773 debug_method_add (w, "try_window_reusing_current_matrix 1");
17774 #endif
17775 return true;
17777 else if (CHARPOS (new_start) > CHARPOS (start))
17779 struct glyph_row *pt_row, *row;
17780 struct glyph_row *first_reusable_row;
17781 struct glyph_row *first_row_to_display;
17782 int dy;
17783 int yb = window_text_bottom_y (w);
17785 /* Find the row starting at new_start, if there is one. Don't
17786 reuse a partially visible line at the end. */
17787 first_reusable_row = start_row;
17788 while (first_reusable_row->enabled_p
17789 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17790 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17791 < CHARPOS (new_start)))
17792 ++first_reusable_row;
17794 /* Give up if there is no row to reuse. */
17795 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17796 || !first_reusable_row->enabled_p
17797 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17798 != CHARPOS (new_start)))
17799 return false;
17801 /* We can reuse fully visible rows beginning with
17802 first_reusable_row to the end of the window. Set
17803 first_row_to_display to the first row that cannot be reused.
17804 Set pt_row to the row containing point, if there is any. */
17805 pt_row = NULL;
17806 for (first_row_to_display = first_reusable_row;
17807 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17808 ++first_row_to_display)
17810 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17811 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17812 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17813 && first_row_to_display->ends_at_zv_p
17814 && pt_row == NULL)))
17815 pt_row = first_row_to_display;
17818 /* Start displaying at the start of first_row_to_display. */
17819 eassert (first_row_to_display->y < yb);
17820 init_to_row_start (&it, w, first_row_to_display);
17822 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17823 - start_vpos);
17824 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17825 - nrows_scrolled);
17826 it.current_y = (first_row_to_display->y - first_reusable_row->y
17827 + WINDOW_HEADER_LINE_HEIGHT (w));
17829 /* Display lines beginning with first_row_to_display in the
17830 desired matrix. Set last_text_row to the last row displayed
17831 that displays text. */
17832 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17833 if (pt_row == NULL)
17834 w->cursor.vpos = -1;
17835 last_text_row = NULL;
17836 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17837 if (display_line (&it, w->cursor.vpos))
17838 last_text_row = it.glyph_row - 1;
17840 /* If point is in a reused row, adjust y and vpos of the cursor
17841 position. */
17842 if (pt_row)
17844 w->cursor.vpos -= nrows_scrolled;
17845 w->cursor.y -= first_reusable_row->y - start_row->y;
17848 /* Give up if point isn't in a row displayed or reused. (This
17849 also handles the case where w->cursor.vpos < nrows_scrolled
17850 after the calls to display_line, which can happen with scroll
17851 margins. See bug#1295.) */
17852 if (w->cursor.vpos < 0)
17854 clear_glyph_matrix (w->desired_matrix);
17855 return false;
17858 /* Scroll the display. */
17859 run.current_y = first_reusable_row->y;
17860 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17861 run.height = it.last_visible_y - run.current_y;
17862 dy = run.current_y - run.desired_y;
17864 if (run.height)
17866 update_begin (f);
17867 FRAME_RIF (f)->update_window_begin_hook (w);
17868 FRAME_RIF (f)->clear_window_mouse_face (w);
17869 FRAME_RIF (f)->scroll_run_hook (w, &run);
17870 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17871 update_end (f);
17874 /* Adjust Y positions of reused rows. */
17875 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17876 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17877 max_y = it.last_visible_y;
17878 for (row = first_reusable_row; row < first_row_to_display; ++row)
17880 row->y -= dy;
17881 row->visible_height = row->height;
17882 if (row->y < min_y)
17883 row->visible_height -= min_y - row->y;
17884 if (row->y + row->height > max_y)
17885 row->visible_height -= row->y + row->height - max_y;
17886 if (row->fringe_bitmap_periodic_p)
17887 row->redraw_fringe_bitmaps_p = true;
17890 /* Scroll the current matrix. */
17891 eassert (nrows_scrolled > 0);
17892 rotate_matrix (w->current_matrix,
17893 start_vpos,
17894 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17895 -nrows_scrolled);
17897 /* Disable rows not reused. */
17898 for (row -= nrows_scrolled; row < bottom_row; ++row)
17899 row->enabled_p = false;
17901 /* Point may have moved to a different line, so we cannot assume that
17902 the previous cursor position is valid; locate the correct row. */
17903 if (pt_row)
17905 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17906 row < bottom_row
17907 && PT >= MATRIX_ROW_END_CHARPOS (row)
17908 && !row->ends_at_zv_p;
17909 row++)
17911 w->cursor.vpos++;
17912 w->cursor.y = row->y;
17914 if (row < bottom_row)
17916 /* Can't simply scan the row for point with
17917 bidi-reordered glyph rows. Let set_cursor_from_row
17918 figure out where to put the cursor, and if it fails,
17919 give up. */
17920 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17922 if (!set_cursor_from_row (w, row, w->current_matrix,
17923 0, 0, 0, 0))
17925 clear_glyph_matrix (w->desired_matrix);
17926 return false;
17929 else
17931 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17932 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17934 for (; glyph < end
17935 && (!BUFFERP (glyph->object)
17936 || glyph->charpos < PT);
17937 glyph++)
17939 w->cursor.hpos++;
17940 w->cursor.x += glyph->pixel_width;
17946 /* Adjust window end. A null value of last_text_row means that
17947 the window end is in reused rows which in turn means that
17948 only its vpos can have changed. */
17949 if (last_text_row)
17950 adjust_window_ends (w, last_text_row, false);
17951 else
17952 w->window_end_vpos -= nrows_scrolled;
17954 w->window_end_valid = false;
17955 w->desired_matrix->no_scrolling_p = true;
17957 #ifdef GLYPH_DEBUG
17958 debug_method_add (w, "try_window_reusing_current_matrix 2");
17959 #endif
17960 return true;
17963 return false;
17968 /************************************************************************
17969 Window redisplay reusing current matrix when buffer has changed
17970 ************************************************************************/
17972 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17973 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17974 ptrdiff_t *, ptrdiff_t *);
17975 static struct glyph_row *
17976 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17977 struct glyph_row *);
17980 /* Return the last row in MATRIX displaying text. If row START is
17981 non-null, start searching with that row. IT gives the dimensions
17982 of the display. Value is null if matrix is empty; otherwise it is
17983 a pointer to the row found. */
17985 static struct glyph_row *
17986 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17987 struct glyph_row *start)
17989 struct glyph_row *row, *row_found;
17991 /* Set row_found to the last row in IT->w's current matrix
17992 displaying text. The loop looks funny but think of partially
17993 visible lines. */
17994 row_found = NULL;
17995 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17996 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17998 eassert (row->enabled_p);
17999 row_found = row;
18000 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18001 break;
18002 ++row;
18005 return row_found;
18009 /* Return the last row in the current matrix of W that is not affected
18010 by changes at the start of current_buffer that occurred since W's
18011 current matrix was built. Value is null if no such row exists.
18013 BEG_UNCHANGED us the number of characters unchanged at the start of
18014 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18015 first changed character in current_buffer. Characters at positions <
18016 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18017 when the current matrix was built. */
18019 static struct glyph_row *
18020 find_last_unchanged_at_beg_row (struct window *w)
18022 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18023 struct glyph_row *row;
18024 struct glyph_row *row_found = NULL;
18025 int yb = window_text_bottom_y (w);
18027 /* Find the last row displaying unchanged text. */
18028 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18029 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18030 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18031 ++row)
18033 if (/* If row ends before first_changed_pos, it is unchanged,
18034 except in some case. */
18035 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18036 /* When row ends in ZV and we write at ZV it is not
18037 unchanged. */
18038 && !row->ends_at_zv_p
18039 /* When first_changed_pos is the end of a continued line,
18040 row is not unchanged because it may be no longer
18041 continued. */
18042 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18043 && (row->continued_p
18044 || row->exact_window_width_line_p))
18045 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18046 needs to be recomputed, so don't consider this row as
18047 unchanged. This happens when the last line was
18048 bidi-reordered and was killed immediately before this
18049 redisplay cycle. In that case, ROW->end stores the
18050 buffer position of the first visual-order character of
18051 the killed text, which is now beyond ZV. */
18052 && CHARPOS (row->end.pos) <= ZV)
18053 row_found = row;
18055 /* Stop if last visible row. */
18056 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18057 break;
18060 return row_found;
18064 /* Find the first glyph row in the current matrix of W that is not
18065 affected by changes at the end of current_buffer since the
18066 time W's current matrix was built.
18068 Return in *DELTA the number of chars by which buffer positions in
18069 unchanged text at the end of current_buffer must be adjusted.
18071 Return in *DELTA_BYTES the corresponding number of bytes.
18073 Value is null if no such row exists, i.e. all rows are affected by
18074 changes. */
18076 static struct glyph_row *
18077 find_first_unchanged_at_end_row (struct window *w,
18078 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18080 struct glyph_row *row;
18081 struct glyph_row *row_found = NULL;
18083 *delta = *delta_bytes = 0;
18085 /* Display must not have been paused, otherwise the current matrix
18086 is not up to date. */
18087 eassert (w->window_end_valid);
18089 /* A value of window_end_pos >= END_UNCHANGED means that the window
18090 end is in the range of changed text. If so, there is no
18091 unchanged row at the end of W's current matrix. */
18092 if (w->window_end_pos >= END_UNCHANGED)
18093 return NULL;
18095 /* Set row to the last row in W's current matrix displaying text. */
18096 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18098 /* If matrix is entirely empty, no unchanged row exists. */
18099 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18101 /* The value of row is the last glyph row in the matrix having a
18102 meaningful buffer position in it. The end position of row
18103 corresponds to window_end_pos. This allows us to translate
18104 buffer positions in the current matrix to current buffer
18105 positions for characters not in changed text. */
18106 ptrdiff_t Z_old =
18107 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18108 ptrdiff_t Z_BYTE_old =
18109 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18110 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18111 struct glyph_row *first_text_row
18112 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18114 *delta = Z - Z_old;
18115 *delta_bytes = Z_BYTE - Z_BYTE_old;
18117 /* Set last_unchanged_pos to the buffer position of the last
18118 character in the buffer that has not been changed. Z is the
18119 index + 1 of the last character in current_buffer, i.e. by
18120 subtracting END_UNCHANGED we get the index of the last
18121 unchanged character, and we have to add BEG to get its buffer
18122 position. */
18123 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18124 last_unchanged_pos_old = last_unchanged_pos - *delta;
18126 /* Search backward from ROW for a row displaying a line that
18127 starts at a minimum position >= last_unchanged_pos_old. */
18128 for (; row > first_text_row; --row)
18130 /* This used to abort, but it can happen.
18131 It is ok to just stop the search instead here. KFS. */
18132 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18133 break;
18135 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18136 row_found = row;
18140 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18142 return row_found;
18146 /* Make sure that glyph rows in the current matrix of window W
18147 reference the same glyph memory as corresponding rows in the
18148 frame's frame matrix. This function is called after scrolling W's
18149 current matrix on a terminal frame in try_window_id and
18150 try_window_reusing_current_matrix. */
18152 static void
18153 sync_frame_with_window_matrix_rows (struct window *w)
18155 struct frame *f = XFRAME (w->frame);
18156 struct glyph_row *window_row, *window_row_end, *frame_row;
18158 /* Preconditions: W must be a leaf window and full-width. Its frame
18159 must have a frame matrix. */
18160 eassert (BUFFERP (w->contents));
18161 eassert (WINDOW_FULL_WIDTH_P (w));
18162 eassert (!FRAME_WINDOW_P (f));
18164 /* If W is a full-width window, glyph pointers in W's current matrix
18165 have, by definition, to be the same as glyph pointers in the
18166 corresponding frame matrix. Note that frame matrices have no
18167 marginal areas (see build_frame_matrix). */
18168 window_row = w->current_matrix->rows;
18169 window_row_end = window_row + w->current_matrix->nrows;
18170 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18171 while (window_row < window_row_end)
18173 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18174 struct glyph *end = window_row->glyphs[LAST_AREA];
18176 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18177 frame_row->glyphs[TEXT_AREA] = start;
18178 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18179 frame_row->glyphs[LAST_AREA] = end;
18181 /* Disable frame rows whose corresponding window rows have
18182 been disabled in try_window_id. */
18183 if (!window_row->enabled_p)
18184 frame_row->enabled_p = false;
18186 ++window_row, ++frame_row;
18191 /* Find the glyph row in window W containing CHARPOS. Consider all
18192 rows between START and END (not inclusive). END null means search
18193 all rows to the end of the display area of W. Value is the row
18194 containing CHARPOS or null. */
18196 struct glyph_row *
18197 row_containing_pos (struct window *w, ptrdiff_t charpos,
18198 struct glyph_row *start, struct glyph_row *end, int dy)
18200 struct glyph_row *row = start;
18201 struct glyph_row *best_row = NULL;
18202 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18203 int last_y;
18205 /* If we happen to start on a header-line, skip that. */
18206 if (row->mode_line_p)
18207 ++row;
18209 if ((end && row >= end) || !row->enabled_p)
18210 return NULL;
18212 last_y = window_text_bottom_y (w) - dy;
18214 while (true)
18216 /* Give up if we have gone too far. */
18217 if ((end && row >= end) || !row->enabled_p)
18218 return NULL;
18219 /* This formerly returned if they were equal.
18220 I think that both quantities are of a "last plus one" type;
18221 if so, when they are equal, the row is within the screen. -- rms. */
18222 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18223 return NULL;
18225 /* If it is in this row, return this row. */
18226 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18227 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18228 /* The end position of a row equals the start
18229 position of the next row. If CHARPOS is there, we
18230 would rather consider it displayed in the next
18231 line, except when this line ends in ZV. */
18232 && !row_for_charpos_p (row, charpos)))
18233 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18235 struct glyph *g;
18237 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18238 || (!best_row && !row->continued_p))
18239 return row;
18240 /* In bidi-reordered rows, there could be several rows whose
18241 edges surround CHARPOS, all of these rows belonging to
18242 the same continued line. We need to find the row which
18243 fits CHARPOS the best. */
18244 for (g = row->glyphs[TEXT_AREA];
18245 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18246 g++)
18248 if (!STRINGP (g->object))
18250 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18252 mindif = eabs (g->charpos - charpos);
18253 best_row = row;
18254 /* Exact match always wins. */
18255 if (mindif == 0)
18256 return best_row;
18261 else if (best_row && !row->continued_p)
18262 return best_row;
18263 ++row;
18268 /* Try to redisplay window W by reusing its existing display. W's
18269 current matrix must be up to date when this function is called,
18270 i.e., window_end_valid must be true.
18272 Value is
18274 >= 1 if successful, i.e. display has been updated
18275 specifically:
18276 1 means the changes were in front of a newline that precedes
18277 the window start, and the whole current matrix was reused
18278 2 means the changes were after the last position displayed
18279 in the window, and the whole current matrix was reused
18280 3 means portions of the current matrix were reused, while
18281 some of the screen lines were redrawn
18282 -1 if redisplay with same window start is known not to succeed
18283 0 if otherwise unsuccessful
18285 The following steps are performed:
18287 1. Find the last row in the current matrix of W that is not
18288 affected by changes at the start of current_buffer. If no such row
18289 is found, give up.
18291 2. Find the first row in W's current matrix that is not affected by
18292 changes at the end of current_buffer. Maybe there is no such row.
18294 3. Display lines beginning with the row + 1 found in step 1 to the
18295 row found in step 2 or, if step 2 didn't find a row, to the end of
18296 the window.
18298 4. If cursor is not known to appear on the window, give up.
18300 5. If display stopped at the row found in step 2, scroll the
18301 display and current matrix as needed.
18303 6. Maybe display some lines at the end of W, if we must. This can
18304 happen under various circumstances, like a partially visible line
18305 becoming fully visible, or because newly displayed lines are displayed
18306 in smaller font sizes.
18308 7. Update W's window end information. */
18310 static int
18311 try_window_id (struct window *w)
18313 struct frame *f = XFRAME (w->frame);
18314 struct glyph_matrix *current_matrix = w->current_matrix;
18315 struct glyph_matrix *desired_matrix = w->desired_matrix;
18316 struct glyph_row *last_unchanged_at_beg_row;
18317 struct glyph_row *first_unchanged_at_end_row;
18318 struct glyph_row *row;
18319 struct glyph_row *bottom_row;
18320 int bottom_vpos;
18321 struct it it;
18322 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18323 int dvpos, dy;
18324 struct text_pos start_pos;
18325 struct run run;
18326 int first_unchanged_at_end_vpos = 0;
18327 struct glyph_row *last_text_row, *last_text_row_at_end;
18328 struct text_pos start;
18329 ptrdiff_t first_changed_charpos, last_changed_charpos;
18331 #ifdef GLYPH_DEBUG
18332 if (inhibit_try_window_id)
18333 return 0;
18334 #endif
18336 /* This is handy for debugging. */
18337 #if false
18338 #define GIVE_UP(X) \
18339 do { \
18340 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18341 return 0; \
18342 } while (false)
18343 #else
18344 #define GIVE_UP(X) return 0
18345 #endif
18347 SET_TEXT_POS_FROM_MARKER (start, w->start);
18349 /* Don't use this for mini-windows because these can show
18350 messages and mini-buffers, and we don't handle that here. */
18351 if (MINI_WINDOW_P (w))
18352 GIVE_UP (1);
18354 /* This flag is used to prevent redisplay optimizations. */
18355 if (windows_or_buffers_changed || f->cursor_type_changed)
18356 GIVE_UP (2);
18358 /* This function's optimizations cannot be used if overlays have
18359 changed in the buffer displayed by the window, so give up if they
18360 have. */
18361 if (w->last_overlay_modified != OVERLAY_MODIFF)
18362 GIVE_UP (200);
18364 /* Verify that narrowing has not changed.
18365 Also verify that we were not told to prevent redisplay optimizations.
18366 It would be nice to further
18367 reduce the number of cases where this prevents try_window_id. */
18368 if (current_buffer->clip_changed
18369 || current_buffer->prevent_redisplay_optimizations_p)
18370 GIVE_UP (3);
18372 /* Window must either use window-based redisplay or be full width. */
18373 if (!FRAME_WINDOW_P (f)
18374 && (!FRAME_LINE_INS_DEL_OK (f)
18375 || !WINDOW_FULL_WIDTH_P (w)))
18376 GIVE_UP (4);
18378 /* Give up if point is known NOT to appear in W. */
18379 if (PT < CHARPOS (start))
18380 GIVE_UP (5);
18382 /* Another way to prevent redisplay optimizations. */
18383 if (w->last_modified == 0)
18384 GIVE_UP (6);
18386 /* Verify that window is not hscrolled. */
18387 if (w->hscroll != 0)
18388 GIVE_UP (7);
18390 /* Verify that display wasn't paused. */
18391 if (!w->window_end_valid)
18392 GIVE_UP (8);
18394 /* Likewise if highlighting trailing whitespace. */
18395 if (!NILP (Vshow_trailing_whitespace))
18396 GIVE_UP (11);
18398 /* Can't use this if overlay arrow position and/or string have
18399 changed. */
18400 if (overlay_arrows_changed_p (false))
18401 GIVE_UP (12);
18403 /* When word-wrap is on, adding a space to the first word of a
18404 wrapped line can change the wrap position, altering the line
18405 above it. It might be worthwhile to handle this more
18406 intelligently, but for now just redisplay from scratch. */
18407 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18408 GIVE_UP (21);
18410 /* Under bidi reordering, adding or deleting a character in the
18411 beginning of a paragraph, before the first strong directional
18412 character, can change the base direction of the paragraph (unless
18413 the buffer specifies a fixed paragraph direction), which will
18414 require redisplaying the whole paragraph. It might be worthwhile
18415 to find the paragraph limits and widen the range of redisplayed
18416 lines to that, but for now just give up this optimization and
18417 redisplay from scratch. */
18418 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18419 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18420 GIVE_UP (22);
18422 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18423 to that variable require thorough redisplay. */
18424 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18425 GIVE_UP (23);
18427 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18428 only if buffer has really changed. The reason is that the gap is
18429 initially at Z for freshly visited files. The code below would
18430 set end_unchanged to 0 in that case. */
18431 if (MODIFF > SAVE_MODIFF
18432 /* This seems to happen sometimes after saving a buffer. */
18433 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18435 if (GPT - BEG < BEG_UNCHANGED)
18436 BEG_UNCHANGED = GPT - BEG;
18437 if (Z - GPT < END_UNCHANGED)
18438 END_UNCHANGED = Z - GPT;
18441 /* The position of the first and last character that has been changed. */
18442 first_changed_charpos = BEG + BEG_UNCHANGED;
18443 last_changed_charpos = Z - END_UNCHANGED;
18445 /* If window starts after a line end, and the last change is in
18446 front of that newline, then changes don't affect the display.
18447 This case happens with stealth-fontification. Note that although
18448 the display is unchanged, glyph positions in the matrix have to
18449 be adjusted, of course. */
18450 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18451 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18452 && ((last_changed_charpos < CHARPOS (start)
18453 && CHARPOS (start) == BEGV)
18454 || (last_changed_charpos < CHARPOS (start) - 1
18455 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18457 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18458 struct glyph_row *r0;
18460 /* Compute how many chars/bytes have been added to or removed
18461 from the buffer. */
18462 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18463 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18464 Z_delta = Z - Z_old;
18465 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18467 /* Give up if PT is not in the window. Note that it already has
18468 been checked at the start of try_window_id that PT is not in
18469 front of the window start. */
18470 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18471 GIVE_UP (13);
18473 /* If window start is unchanged, we can reuse the whole matrix
18474 as is, after adjusting glyph positions. No need to compute
18475 the window end again, since its offset from Z hasn't changed. */
18476 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18477 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18478 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18479 /* PT must not be in a partially visible line. */
18480 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18481 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18483 /* Adjust positions in the glyph matrix. */
18484 if (Z_delta || Z_delta_bytes)
18486 struct glyph_row *r1
18487 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18488 increment_matrix_positions (w->current_matrix,
18489 MATRIX_ROW_VPOS (r0, current_matrix),
18490 MATRIX_ROW_VPOS (r1, current_matrix),
18491 Z_delta, Z_delta_bytes);
18494 /* Set the cursor. */
18495 row = row_containing_pos (w, PT, r0, NULL, 0);
18496 if (row)
18497 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18498 return 1;
18502 /* Handle the case that changes are all below what is displayed in
18503 the window, and that PT is in the window. This shortcut cannot
18504 be taken if ZV is visible in the window, and text has been added
18505 there that is visible in the window. */
18506 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18507 /* ZV is not visible in the window, or there are no
18508 changes at ZV, actually. */
18509 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18510 || first_changed_charpos == last_changed_charpos))
18512 struct glyph_row *r0;
18514 /* Give up if PT is not in the window. Note that it already has
18515 been checked at the start of try_window_id that PT is not in
18516 front of the window start. */
18517 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18518 GIVE_UP (14);
18520 /* If window start is unchanged, we can reuse the whole matrix
18521 as is, without changing glyph positions since no text has
18522 been added/removed in front of the window end. */
18523 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18524 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18525 /* PT must not be in a partially visible line. */
18526 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18527 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18529 /* We have to compute the window end anew since text
18530 could have been added/removed after it. */
18531 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18532 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18534 /* Set the cursor. */
18535 row = row_containing_pos (w, PT, r0, NULL, 0);
18536 if (row)
18537 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18538 return 2;
18542 /* Give up if window start is in the changed area.
18544 The condition used to read
18546 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18548 but why that was tested escapes me at the moment. */
18549 if (CHARPOS (start) >= first_changed_charpos
18550 && CHARPOS (start) <= last_changed_charpos)
18551 GIVE_UP (15);
18553 /* Check that window start agrees with the start of the first glyph
18554 row in its current matrix. Check this after we know the window
18555 start is not in changed text, otherwise positions would not be
18556 comparable. */
18557 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18558 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18559 GIVE_UP (16);
18561 /* Give up if the window ends in strings. Overlay strings
18562 at the end are difficult to handle, so don't try. */
18563 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18564 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18565 GIVE_UP (20);
18567 /* Compute the position at which we have to start displaying new
18568 lines. Some of the lines at the top of the window might be
18569 reusable because they are not displaying changed text. Find the
18570 last row in W's current matrix not affected by changes at the
18571 start of current_buffer. Value is null if changes start in the
18572 first line of window. */
18573 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18574 if (last_unchanged_at_beg_row)
18576 /* Avoid starting to display in the middle of a character, a TAB
18577 for instance. This is easier than to set up the iterator
18578 exactly, and it's not a frequent case, so the additional
18579 effort wouldn't really pay off. */
18580 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18581 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18582 && last_unchanged_at_beg_row > w->current_matrix->rows)
18583 --last_unchanged_at_beg_row;
18585 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18586 GIVE_UP (17);
18588 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18589 GIVE_UP (18);
18590 start_pos = it.current.pos;
18592 /* Start displaying new lines in the desired matrix at the same
18593 vpos we would use in the current matrix, i.e. below
18594 last_unchanged_at_beg_row. */
18595 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18596 current_matrix);
18597 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18598 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18600 eassert (it.hpos == 0 && it.current_x == 0);
18602 else
18604 /* There are no reusable lines at the start of the window.
18605 Start displaying in the first text line. */
18606 start_display (&it, w, start);
18607 it.vpos = it.first_vpos;
18608 start_pos = it.current.pos;
18611 /* Find the first row that is not affected by changes at the end of
18612 the buffer. Value will be null if there is no unchanged row, in
18613 which case we must redisplay to the end of the window. delta
18614 will be set to the value by which buffer positions beginning with
18615 first_unchanged_at_end_row have to be adjusted due to text
18616 changes. */
18617 first_unchanged_at_end_row
18618 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18619 IF_DEBUG (debug_delta = delta);
18620 IF_DEBUG (debug_delta_bytes = delta_bytes);
18622 /* Set stop_pos to the buffer position up to which we will have to
18623 display new lines. If first_unchanged_at_end_row != NULL, this
18624 is the buffer position of the start of the line displayed in that
18625 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18626 that we don't stop at a buffer position. */
18627 stop_pos = 0;
18628 if (first_unchanged_at_end_row)
18630 eassert (last_unchanged_at_beg_row == NULL
18631 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18633 /* If this is a continuation line, move forward to the next one
18634 that isn't. Changes in lines above affect this line.
18635 Caution: this may move first_unchanged_at_end_row to a row
18636 not displaying text. */
18637 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18638 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18639 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18640 < it.last_visible_y))
18641 ++first_unchanged_at_end_row;
18643 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18644 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18645 >= it.last_visible_y))
18646 first_unchanged_at_end_row = NULL;
18647 else
18649 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18650 + delta);
18651 first_unchanged_at_end_vpos
18652 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18653 eassert (stop_pos >= Z - END_UNCHANGED);
18656 else if (last_unchanged_at_beg_row == NULL)
18657 GIVE_UP (19);
18660 #ifdef GLYPH_DEBUG
18662 /* Either there is no unchanged row at the end, or the one we have
18663 now displays text. This is a necessary condition for the window
18664 end pos calculation at the end of this function. */
18665 eassert (first_unchanged_at_end_row == NULL
18666 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18668 debug_last_unchanged_at_beg_vpos
18669 = (last_unchanged_at_beg_row
18670 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18671 : -1);
18672 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18674 #endif /* GLYPH_DEBUG */
18677 /* Display new lines. Set last_text_row to the last new line
18678 displayed which has text on it, i.e. might end up as being the
18679 line where the window_end_vpos is. */
18680 w->cursor.vpos = -1;
18681 last_text_row = NULL;
18682 overlay_arrow_seen = false;
18683 if (it.current_y < it.last_visible_y
18684 && !f->fonts_changed
18685 && (first_unchanged_at_end_row == NULL
18686 || IT_CHARPOS (it) < stop_pos))
18687 it.glyph_row->reversed_p = false;
18688 while (it.current_y < it.last_visible_y
18689 && !f->fonts_changed
18690 && (first_unchanged_at_end_row == NULL
18691 || IT_CHARPOS (it) < stop_pos))
18693 if (display_line (&it, -1))
18694 last_text_row = it.glyph_row - 1;
18697 if (f->fonts_changed)
18698 return -1;
18700 /* The redisplay iterations in display_line above could have
18701 triggered font-lock, which could have done something that
18702 invalidates IT->w window's end-point information, on which we
18703 rely below. E.g., one package, which will remain unnamed, used
18704 to install a font-lock-fontify-region-function that called
18705 bury-buffer, whose side effect is to switch the buffer displayed
18706 by IT->w, and that predictably resets IT->w's window_end_valid
18707 flag, which we already tested at the entry to this function.
18708 Amply punish such packages/modes by giving up on this
18709 optimization in those cases. */
18710 if (!w->window_end_valid)
18712 clear_glyph_matrix (w->desired_matrix);
18713 return -1;
18716 /* Compute differences in buffer positions, y-positions etc. for
18717 lines reused at the bottom of the window. Compute what we can
18718 scroll. */
18719 if (first_unchanged_at_end_row
18720 /* No lines reused because we displayed everything up to the
18721 bottom of the window. */
18722 && it.current_y < it.last_visible_y)
18724 dvpos = (it.vpos
18725 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18726 current_matrix));
18727 dy = it.current_y - first_unchanged_at_end_row->y;
18728 run.current_y = first_unchanged_at_end_row->y;
18729 run.desired_y = run.current_y + dy;
18730 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18732 else
18734 delta = delta_bytes = dvpos = dy
18735 = run.current_y = run.desired_y = run.height = 0;
18736 first_unchanged_at_end_row = NULL;
18738 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18741 /* Find the cursor if not already found. We have to decide whether
18742 PT will appear on this window (it sometimes doesn't, but this is
18743 not a very frequent case.) This decision has to be made before
18744 the current matrix is altered. A value of cursor.vpos < 0 means
18745 that PT is either in one of the lines beginning at
18746 first_unchanged_at_end_row or below the window. Don't care for
18747 lines that might be displayed later at the window end; as
18748 mentioned, this is not a frequent case. */
18749 if (w->cursor.vpos < 0)
18751 /* Cursor in unchanged rows at the top? */
18752 if (PT < CHARPOS (start_pos)
18753 && last_unchanged_at_beg_row)
18755 row = row_containing_pos (w, PT,
18756 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18757 last_unchanged_at_beg_row + 1, 0);
18758 if (row)
18759 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18762 /* Start from first_unchanged_at_end_row looking for PT. */
18763 else if (first_unchanged_at_end_row)
18765 row = row_containing_pos (w, PT - delta,
18766 first_unchanged_at_end_row, NULL, 0);
18767 if (row)
18768 set_cursor_from_row (w, row, w->current_matrix, delta,
18769 delta_bytes, dy, dvpos);
18772 /* Give up if cursor was not found. */
18773 if (w->cursor.vpos < 0)
18775 clear_glyph_matrix (w->desired_matrix);
18776 return -1;
18780 /* Don't let the cursor end in the scroll margins. */
18782 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18783 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18785 if ((w->cursor.y < this_scroll_margin
18786 && CHARPOS (start) > BEGV)
18787 /* Old redisplay didn't take scroll margin into account at the bottom,
18788 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18789 || (w->cursor.y + (make_cursor_line_fully_visible_p
18790 ? cursor_height + this_scroll_margin
18791 : 1)) > it.last_visible_y)
18793 w->cursor.vpos = -1;
18794 clear_glyph_matrix (w->desired_matrix);
18795 return -1;
18799 /* Scroll the display. Do it before changing the current matrix so
18800 that xterm.c doesn't get confused about where the cursor glyph is
18801 found. */
18802 if (dy && run.height)
18804 update_begin (f);
18806 if (FRAME_WINDOW_P (f))
18808 FRAME_RIF (f)->update_window_begin_hook (w);
18809 FRAME_RIF (f)->clear_window_mouse_face (w);
18810 FRAME_RIF (f)->scroll_run_hook (w, &run);
18811 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18813 else
18815 /* Terminal frame. In this case, dvpos gives the number of
18816 lines to scroll by; dvpos < 0 means scroll up. */
18817 int from_vpos
18818 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18819 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18820 int end = (WINDOW_TOP_EDGE_LINE (w)
18821 + WINDOW_WANTS_HEADER_LINE_P (w)
18822 + window_internal_height (w));
18824 #if defined (HAVE_GPM) || defined (MSDOS)
18825 x_clear_window_mouse_face (w);
18826 #endif
18827 /* Perform the operation on the screen. */
18828 if (dvpos > 0)
18830 /* Scroll last_unchanged_at_beg_row to the end of the
18831 window down dvpos lines. */
18832 set_terminal_window (f, end);
18834 /* On dumb terminals delete dvpos lines at the end
18835 before inserting dvpos empty lines. */
18836 if (!FRAME_SCROLL_REGION_OK (f))
18837 ins_del_lines (f, end - dvpos, -dvpos);
18839 /* Insert dvpos empty lines in front of
18840 last_unchanged_at_beg_row. */
18841 ins_del_lines (f, from, dvpos);
18843 else if (dvpos < 0)
18845 /* Scroll up last_unchanged_at_beg_vpos to the end of
18846 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18847 set_terminal_window (f, end);
18849 /* Delete dvpos lines in front of
18850 last_unchanged_at_beg_vpos. ins_del_lines will set
18851 the cursor to the given vpos and emit |dvpos| delete
18852 line sequences. */
18853 ins_del_lines (f, from + dvpos, dvpos);
18855 /* On a dumb terminal insert dvpos empty lines at the
18856 end. */
18857 if (!FRAME_SCROLL_REGION_OK (f))
18858 ins_del_lines (f, end + dvpos, -dvpos);
18861 set_terminal_window (f, 0);
18864 update_end (f);
18867 /* Shift reused rows of the current matrix to the right position.
18868 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18869 text. */
18870 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18871 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18872 if (dvpos < 0)
18874 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18875 bottom_vpos, dvpos);
18876 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18877 bottom_vpos);
18879 else if (dvpos > 0)
18881 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18882 bottom_vpos, dvpos);
18883 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18884 first_unchanged_at_end_vpos + dvpos);
18887 /* For frame-based redisplay, make sure that current frame and window
18888 matrix are in sync with respect to glyph memory. */
18889 if (!FRAME_WINDOW_P (f))
18890 sync_frame_with_window_matrix_rows (w);
18892 /* Adjust buffer positions in reused rows. */
18893 if (delta || delta_bytes)
18894 increment_matrix_positions (current_matrix,
18895 first_unchanged_at_end_vpos + dvpos,
18896 bottom_vpos, delta, delta_bytes);
18898 /* Adjust Y positions. */
18899 if (dy)
18900 shift_glyph_matrix (w, current_matrix,
18901 first_unchanged_at_end_vpos + dvpos,
18902 bottom_vpos, dy);
18904 if (first_unchanged_at_end_row)
18906 first_unchanged_at_end_row += dvpos;
18907 if (first_unchanged_at_end_row->y >= it.last_visible_y
18908 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18909 first_unchanged_at_end_row = NULL;
18912 /* If scrolling up, there may be some lines to display at the end of
18913 the window. */
18914 last_text_row_at_end = NULL;
18915 if (dy < 0)
18917 /* Scrolling up can leave for example a partially visible line
18918 at the end of the window to be redisplayed. */
18919 /* Set last_row to the glyph row in the current matrix where the
18920 window end line is found. It has been moved up or down in
18921 the matrix by dvpos. */
18922 int last_vpos = w->window_end_vpos + dvpos;
18923 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18925 /* If last_row is the window end line, it should display text. */
18926 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18928 /* If window end line was partially visible before, begin
18929 displaying at that line. Otherwise begin displaying with the
18930 line following it. */
18931 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18933 init_to_row_start (&it, w, last_row);
18934 it.vpos = last_vpos;
18935 it.current_y = last_row->y;
18937 else
18939 init_to_row_end (&it, w, last_row);
18940 it.vpos = 1 + last_vpos;
18941 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18942 ++last_row;
18945 /* We may start in a continuation line. If so, we have to
18946 get the right continuation_lines_width and current_x. */
18947 it.continuation_lines_width = last_row->continuation_lines_width;
18948 it.hpos = it.current_x = 0;
18950 /* Display the rest of the lines at the window end. */
18951 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18952 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18954 /* Is it always sure that the display agrees with lines in
18955 the current matrix? I don't think so, so we mark rows
18956 displayed invalid in the current matrix by setting their
18957 enabled_p flag to false. */
18958 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18959 if (display_line (&it, w->cursor.vpos))
18960 last_text_row_at_end = it.glyph_row - 1;
18964 /* Update window_end_pos and window_end_vpos. */
18965 if (first_unchanged_at_end_row && !last_text_row_at_end)
18967 /* Window end line if one of the preserved rows from the current
18968 matrix. Set row to the last row displaying text in current
18969 matrix starting at first_unchanged_at_end_row, after
18970 scrolling. */
18971 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18972 row = find_last_row_displaying_text (w->current_matrix, &it,
18973 first_unchanged_at_end_row);
18974 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18975 adjust_window_ends (w, row, true);
18976 eassert (w->window_end_bytepos >= 0);
18977 IF_DEBUG (debug_method_add (w, "A"));
18979 else if (last_text_row_at_end)
18981 adjust_window_ends (w, last_text_row_at_end, false);
18982 eassert (w->window_end_bytepos >= 0);
18983 IF_DEBUG (debug_method_add (w, "B"));
18985 else if (last_text_row)
18987 /* We have displayed either to the end of the window or at the
18988 end of the window, i.e. the last row with text is to be found
18989 in the desired matrix. */
18990 adjust_window_ends (w, last_text_row, false);
18991 eassert (w->window_end_bytepos >= 0);
18993 else if (first_unchanged_at_end_row == NULL
18994 && last_text_row == NULL
18995 && last_text_row_at_end == NULL)
18997 /* Displayed to end of window, but no line containing text was
18998 displayed. Lines were deleted at the end of the window. */
18999 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
19000 int vpos = w->window_end_vpos;
19001 struct glyph_row *current_row = current_matrix->rows + vpos;
19002 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19004 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19006 eassert (first_vpos <= vpos);
19007 if (desired_row->enabled_p)
19009 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19010 row = desired_row;
19012 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19013 row = current_row;
19016 w->window_end_vpos = vpos + 1;
19017 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19018 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19019 eassert (w->window_end_bytepos >= 0);
19020 IF_DEBUG (debug_method_add (w, "C"));
19022 else
19023 emacs_abort ();
19025 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19026 debug_end_vpos = w->window_end_vpos));
19028 /* Record that display has not been completed. */
19029 w->window_end_valid = false;
19030 w->desired_matrix->no_scrolling_p = true;
19031 return 3;
19033 #undef GIVE_UP
19038 /***********************************************************************
19039 More debugging support
19040 ***********************************************************************/
19042 #ifdef GLYPH_DEBUG
19044 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19045 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19046 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19049 /* Dump the contents of glyph matrix MATRIX on stderr.
19051 GLYPHS 0 means don't show glyph contents.
19052 GLYPHS 1 means show glyphs in short form
19053 GLYPHS > 1 means show glyphs in long form. */
19055 void
19056 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19058 int i;
19059 for (i = 0; i < matrix->nrows; ++i)
19060 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19064 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19065 the glyph row and area where the glyph comes from. */
19067 void
19068 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19070 if (glyph->type == CHAR_GLYPH
19071 || glyph->type == GLYPHLESS_GLYPH)
19073 fprintf (stderr,
19074 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19075 glyph - row->glyphs[TEXT_AREA],
19076 (glyph->type == CHAR_GLYPH
19077 ? 'C'
19078 : 'G'),
19079 glyph->charpos,
19080 (BUFFERP (glyph->object)
19081 ? 'B'
19082 : (STRINGP (glyph->object)
19083 ? 'S'
19084 : (NILP (glyph->object)
19085 ? '0'
19086 : '-'))),
19087 glyph->pixel_width,
19088 glyph->u.ch,
19089 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19090 ? (int) glyph->u.ch
19091 : '.'),
19092 glyph->face_id,
19093 glyph->left_box_line_p,
19094 glyph->right_box_line_p);
19096 else if (glyph->type == STRETCH_GLYPH)
19098 fprintf (stderr,
19099 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19100 glyph - row->glyphs[TEXT_AREA],
19101 'S',
19102 glyph->charpos,
19103 (BUFFERP (glyph->object)
19104 ? 'B'
19105 : (STRINGP (glyph->object)
19106 ? 'S'
19107 : (NILP (glyph->object)
19108 ? '0'
19109 : '-'))),
19110 glyph->pixel_width,
19112 ' ',
19113 glyph->face_id,
19114 glyph->left_box_line_p,
19115 glyph->right_box_line_p);
19117 else if (glyph->type == IMAGE_GLYPH)
19119 fprintf (stderr,
19120 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19121 glyph - row->glyphs[TEXT_AREA],
19122 'I',
19123 glyph->charpos,
19124 (BUFFERP (glyph->object)
19125 ? 'B'
19126 : (STRINGP (glyph->object)
19127 ? 'S'
19128 : (NILP (glyph->object)
19129 ? '0'
19130 : '-'))),
19131 glyph->pixel_width,
19132 (unsigned int) glyph->u.img_id,
19133 '.',
19134 glyph->face_id,
19135 glyph->left_box_line_p,
19136 glyph->right_box_line_p);
19138 else if (glyph->type == COMPOSITE_GLYPH)
19140 fprintf (stderr,
19141 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19142 glyph - row->glyphs[TEXT_AREA],
19143 '+',
19144 glyph->charpos,
19145 (BUFFERP (glyph->object)
19146 ? 'B'
19147 : (STRINGP (glyph->object)
19148 ? 'S'
19149 : (NILP (glyph->object)
19150 ? '0'
19151 : '-'))),
19152 glyph->pixel_width,
19153 (unsigned int) glyph->u.cmp.id);
19154 if (glyph->u.cmp.automatic)
19155 fprintf (stderr,
19156 "[%d-%d]",
19157 glyph->slice.cmp.from, glyph->slice.cmp.to);
19158 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19159 glyph->face_id,
19160 glyph->left_box_line_p,
19161 glyph->right_box_line_p);
19163 else if (glyph->type == XWIDGET_GLYPH)
19165 #ifndef HAVE_XWIDGETS
19166 eassume (false);
19167 #else
19168 fprintf (stderr,
19169 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19170 glyph - row->glyphs[TEXT_AREA],
19171 'X',
19172 glyph->charpos,
19173 (BUFFERP (glyph->object)
19174 ? 'B'
19175 : (STRINGP (glyph->object)
19176 ? 'S'
19177 : '-')),
19178 glyph->pixel_width,
19179 glyph->u.xwidget,
19180 '.',
19181 glyph->face_id,
19182 glyph->left_box_line_p,
19183 glyph->right_box_line_p);
19184 #endif
19189 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19190 GLYPHS 0 means don't show glyph contents.
19191 GLYPHS 1 means show glyphs in short form
19192 GLYPHS > 1 means show glyphs in long form. */
19194 void
19195 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19197 if (glyphs != 1)
19199 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19200 fprintf (stderr, "==============================================================================\n");
19202 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19203 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19204 vpos,
19205 MATRIX_ROW_START_CHARPOS (row),
19206 MATRIX_ROW_END_CHARPOS (row),
19207 row->used[TEXT_AREA],
19208 row->contains_overlapping_glyphs_p,
19209 row->enabled_p,
19210 row->truncated_on_left_p,
19211 row->truncated_on_right_p,
19212 row->continued_p,
19213 MATRIX_ROW_CONTINUATION_LINE_P (row),
19214 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19215 row->ends_at_zv_p,
19216 row->fill_line_p,
19217 row->ends_in_middle_of_char_p,
19218 row->starts_in_middle_of_char_p,
19219 row->mouse_face_p,
19220 row->x,
19221 row->y,
19222 row->pixel_width,
19223 row->height,
19224 row->visible_height,
19225 row->ascent,
19226 row->phys_ascent);
19227 /* The next 3 lines should align to "Start" in the header. */
19228 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19229 row->end.overlay_string_index,
19230 row->continuation_lines_width);
19231 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19232 CHARPOS (row->start.string_pos),
19233 CHARPOS (row->end.string_pos));
19234 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19235 row->end.dpvec_index);
19238 if (glyphs > 1)
19240 int area;
19242 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19244 struct glyph *glyph = row->glyphs[area];
19245 struct glyph *glyph_end = glyph + row->used[area];
19247 /* Glyph for a line end in text. */
19248 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19249 ++glyph_end;
19251 if (glyph < glyph_end)
19252 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19254 for (; glyph < glyph_end; ++glyph)
19255 dump_glyph (row, glyph, area);
19258 else if (glyphs == 1)
19260 int area;
19261 char s[SHRT_MAX + 4];
19263 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19265 int i;
19267 for (i = 0; i < row->used[area]; ++i)
19269 struct glyph *glyph = row->glyphs[area] + i;
19270 if (i == row->used[area] - 1
19271 && area == TEXT_AREA
19272 && NILP (glyph->object)
19273 && glyph->type == CHAR_GLYPH
19274 && glyph->u.ch == ' ')
19276 strcpy (&s[i], "[\\n]");
19277 i += 4;
19279 else if (glyph->type == CHAR_GLYPH
19280 && glyph->u.ch < 0x80
19281 && glyph->u.ch >= ' ')
19282 s[i] = glyph->u.ch;
19283 else
19284 s[i] = '.';
19287 s[i] = '\0';
19288 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19294 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19295 Sdump_glyph_matrix, 0, 1, "p",
19296 doc: /* Dump the current matrix of the selected window to stderr.
19297 Shows contents of glyph row structures. With non-nil
19298 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19299 glyphs in short form, otherwise show glyphs in long form.
19301 Interactively, no argument means show glyphs in short form;
19302 with numeric argument, its value is passed as the GLYPHS flag. */)
19303 (Lisp_Object glyphs)
19305 struct window *w = XWINDOW (selected_window);
19306 struct buffer *buffer = XBUFFER (w->contents);
19308 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19309 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19310 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19311 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19312 fprintf (stderr, "=============================================\n");
19313 dump_glyph_matrix (w->current_matrix,
19314 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19315 return Qnil;
19319 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19320 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19321 Only text-mode frames have frame glyph matrices. */)
19322 (void)
19324 struct frame *f = XFRAME (selected_frame);
19326 if (f->current_matrix)
19327 dump_glyph_matrix (f->current_matrix, 1);
19328 else
19329 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19330 return Qnil;
19334 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19335 doc: /* Dump glyph row ROW to stderr.
19336 GLYPH 0 means don't dump glyphs.
19337 GLYPH 1 means dump glyphs in short form.
19338 GLYPH > 1 or omitted means dump glyphs in long form. */)
19339 (Lisp_Object row, Lisp_Object glyphs)
19341 struct glyph_matrix *matrix;
19342 EMACS_INT vpos;
19344 CHECK_NUMBER (row);
19345 matrix = XWINDOW (selected_window)->current_matrix;
19346 vpos = XINT (row);
19347 if (vpos >= 0 && vpos < matrix->nrows)
19348 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19349 vpos,
19350 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19351 return Qnil;
19355 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19356 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19357 GLYPH 0 means don't dump glyphs.
19358 GLYPH 1 means dump glyphs in short form.
19359 GLYPH > 1 or omitted means dump glyphs in long form.
19361 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19362 do nothing. */)
19363 (Lisp_Object row, Lisp_Object glyphs)
19365 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19366 struct frame *sf = SELECTED_FRAME ();
19367 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19368 EMACS_INT vpos;
19370 CHECK_NUMBER (row);
19371 vpos = XINT (row);
19372 if (vpos >= 0 && vpos < m->nrows)
19373 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19374 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19375 #endif
19376 return Qnil;
19380 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19381 doc: /* Toggle tracing of redisplay.
19382 With ARG, turn tracing on if and only if ARG is positive. */)
19383 (Lisp_Object arg)
19385 if (NILP (arg))
19386 trace_redisplay_p = !trace_redisplay_p;
19387 else
19389 arg = Fprefix_numeric_value (arg);
19390 trace_redisplay_p = XINT (arg) > 0;
19393 return Qnil;
19397 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19398 doc: /* Like `format', but print result to stderr.
19399 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19400 (ptrdiff_t nargs, Lisp_Object *args)
19402 Lisp_Object s = Fformat (nargs, args);
19403 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19404 return Qnil;
19407 #endif /* GLYPH_DEBUG */
19411 /***********************************************************************
19412 Building Desired Matrix Rows
19413 ***********************************************************************/
19415 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19416 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19418 static struct glyph_row *
19419 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19421 struct frame *f = XFRAME (WINDOW_FRAME (w));
19422 struct buffer *buffer = XBUFFER (w->contents);
19423 struct buffer *old = current_buffer;
19424 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19425 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19426 const unsigned char *arrow_end = arrow_string + arrow_len;
19427 const unsigned char *p;
19428 struct it it;
19429 bool multibyte_p;
19430 int n_glyphs_before;
19432 set_buffer_temp (buffer);
19433 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19434 scratch_glyph_row.reversed_p = false;
19435 it.glyph_row->used[TEXT_AREA] = 0;
19436 SET_TEXT_POS (it.position, 0, 0);
19438 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19439 p = arrow_string;
19440 while (p < arrow_end)
19442 Lisp_Object face, ilisp;
19444 /* Get the next character. */
19445 if (multibyte_p)
19446 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19447 else
19449 it.c = it.char_to_display = *p, it.len = 1;
19450 if (! ASCII_CHAR_P (it.c))
19451 it.char_to_display = BYTE8_TO_CHAR (it.c);
19453 p += it.len;
19455 /* Get its face. */
19456 ilisp = make_number (p - arrow_string);
19457 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19458 it.face_id = compute_char_face (f, it.char_to_display, face);
19460 /* Compute its width, get its glyphs. */
19461 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19462 SET_TEXT_POS (it.position, -1, -1);
19463 PRODUCE_GLYPHS (&it);
19465 /* If this character doesn't fit any more in the line, we have
19466 to remove some glyphs. */
19467 if (it.current_x > it.last_visible_x)
19469 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19470 break;
19474 set_buffer_temp (old);
19475 return it.glyph_row;
19479 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19480 glyphs to insert is determined by produce_special_glyphs. */
19482 static void
19483 insert_left_trunc_glyphs (struct it *it)
19485 struct it truncate_it;
19486 struct glyph *from, *end, *to, *toend;
19488 eassert (!FRAME_WINDOW_P (it->f)
19489 || (!it->glyph_row->reversed_p
19490 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19491 || (it->glyph_row->reversed_p
19492 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19494 /* Get the truncation glyphs. */
19495 truncate_it = *it;
19496 truncate_it.current_x = 0;
19497 truncate_it.face_id = DEFAULT_FACE_ID;
19498 truncate_it.glyph_row = &scratch_glyph_row;
19499 truncate_it.area = TEXT_AREA;
19500 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19501 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19502 truncate_it.object = Qnil;
19503 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19505 /* Overwrite glyphs from IT with truncation glyphs. */
19506 if (!it->glyph_row->reversed_p)
19508 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19510 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19511 end = from + tused;
19512 to = it->glyph_row->glyphs[TEXT_AREA];
19513 toend = to + it->glyph_row->used[TEXT_AREA];
19514 if (FRAME_WINDOW_P (it->f))
19516 /* On GUI frames, when variable-size fonts are displayed,
19517 the truncation glyphs may need more pixels than the row's
19518 glyphs they overwrite. We overwrite more glyphs to free
19519 enough screen real estate, and enlarge the stretch glyph
19520 on the right (see display_line), if there is one, to
19521 preserve the screen position of the truncation glyphs on
19522 the right. */
19523 int w = 0;
19524 struct glyph *g = to;
19525 short used;
19527 /* The first glyph could be partially visible, in which case
19528 it->glyph_row->x will be negative. But we want the left
19529 truncation glyphs to be aligned at the left margin of the
19530 window, so we override the x coordinate at which the row
19531 will begin. */
19532 it->glyph_row->x = 0;
19533 while (g < toend && w < it->truncation_pixel_width)
19535 w += g->pixel_width;
19536 ++g;
19538 if (g - to - tused > 0)
19540 memmove (to + tused, g, (toend - g) * sizeof(*g));
19541 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19543 used = it->glyph_row->used[TEXT_AREA];
19544 if (it->glyph_row->truncated_on_right_p
19545 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19546 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19547 == STRETCH_GLYPH)
19549 int extra = w - it->truncation_pixel_width;
19551 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19555 while (from < end)
19556 *to++ = *from++;
19558 /* There may be padding glyphs left over. Overwrite them too. */
19559 if (!FRAME_WINDOW_P (it->f))
19561 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19563 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19564 while (from < end)
19565 *to++ = *from++;
19569 if (to > toend)
19570 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19572 else
19574 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19576 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19577 that back to front. */
19578 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19579 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19580 toend = it->glyph_row->glyphs[TEXT_AREA];
19581 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19582 if (FRAME_WINDOW_P (it->f))
19584 int w = 0;
19585 struct glyph *g = to;
19587 while (g >= toend && w < it->truncation_pixel_width)
19589 w += g->pixel_width;
19590 --g;
19592 if (to - g - tused > 0)
19593 to = g + tused;
19594 if (it->glyph_row->truncated_on_right_p
19595 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19596 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19598 int extra = w - it->truncation_pixel_width;
19600 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19604 while (from >= end && to >= toend)
19605 *to-- = *from--;
19606 if (!FRAME_WINDOW_P (it->f))
19608 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19610 from =
19611 truncate_it.glyph_row->glyphs[TEXT_AREA]
19612 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19613 while (from >= end && to >= toend)
19614 *to-- = *from--;
19617 if (from >= end)
19619 /* Need to free some room before prepending additional
19620 glyphs. */
19621 int move_by = from - end + 1;
19622 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19623 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19625 for ( ; g >= g0; g--)
19626 g[move_by] = *g;
19627 while (from >= end)
19628 *to-- = *from--;
19629 it->glyph_row->used[TEXT_AREA] += move_by;
19634 /* Compute the hash code for ROW. */
19635 unsigned
19636 row_hash (struct glyph_row *row)
19638 int area, k;
19639 unsigned hashval = 0;
19641 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19642 for (k = 0; k < row->used[area]; ++k)
19643 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19644 + row->glyphs[area][k].u.val
19645 + row->glyphs[area][k].face_id
19646 + row->glyphs[area][k].padding_p
19647 + (row->glyphs[area][k].type << 2));
19649 return hashval;
19652 /* Compute the pixel height and width of IT->glyph_row.
19654 Most of the time, ascent and height of a display line will be equal
19655 to the max_ascent and max_height values of the display iterator
19656 structure. This is not the case if
19658 1. We hit ZV without displaying anything. In this case, max_ascent
19659 and max_height will be zero.
19661 2. We have some glyphs that don't contribute to the line height.
19662 (The glyph row flag contributes_to_line_height_p is for future
19663 pixmap extensions).
19665 The first case is easily covered by using default values because in
19666 these cases, the line height does not really matter, except that it
19667 must not be zero. */
19669 static void
19670 compute_line_metrics (struct it *it)
19672 struct glyph_row *row = it->glyph_row;
19674 if (FRAME_WINDOW_P (it->f))
19676 int i, min_y, max_y;
19678 /* The line may consist of one space only, that was added to
19679 place the cursor on it. If so, the row's height hasn't been
19680 computed yet. */
19681 if (row->height == 0)
19683 if (it->max_ascent + it->max_descent == 0)
19684 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19685 row->ascent = it->max_ascent;
19686 row->height = it->max_ascent + it->max_descent;
19687 row->phys_ascent = it->max_phys_ascent;
19688 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19689 row->extra_line_spacing = it->max_extra_line_spacing;
19692 /* Compute the width of this line. */
19693 row->pixel_width = row->x;
19694 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19695 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19697 eassert (row->pixel_width >= 0);
19698 eassert (row->ascent >= 0 && row->height > 0);
19700 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19701 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19703 /* If first line's physical ascent is larger than its logical
19704 ascent, use the physical ascent, and make the row taller.
19705 This makes accented characters fully visible. */
19706 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19707 && row->phys_ascent > row->ascent)
19709 row->height += row->phys_ascent - row->ascent;
19710 row->ascent = row->phys_ascent;
19713 /* Compute how much of the line is visible. */
19714 row->visible_height = row->height;
19716 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19717 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19719 if (row->y < min_y)
19720 row->visible_height -= min_y - row->y;
19721 if (row->y + row->height > max_y)
19722 row->visible_height -= row->y + row->height - max_y;
19724 else
19726 row->pixel_width = row->used[TEXT_AREA];
19727 if (row->continued_p)
19728 row->pixel_width -= it->continuation_pixel_width;
19729 else if (row->truncated_on_right_p)
19730 row->pixel_width -= it->truncation_pixel_width;
19731 row->ascent = row->phys_ascent = 0;
19732 row->height = row->phys_height = row->visible_height = 1;
19733 row->extra_line_spacing = 0;
19736 /* Compute a hash code for this row. */
19737 row->hash = row_hash (row);
19739 it->max_ascent = it->max_descent = 0;
19740 it->max_phys_ascent = it->max_phys_descent = 0;
19744 /* Append one space to the glyph row of iterator IT if doing a
19745 window-based redisplay. The space has the same face as
19746 IT->face_id. Value is true if a space was added.
19748 This function is called to make sure that there is always one glyph
19749 at the end of a glyph row that the cursor can be set on under
19750 window-systems. (If there weren't such a glyph we would not know
19751 how wide and tall a box cursor should be displayed).
19753 At the same time this space let's a nicely handle clearing to the
19754 end of the line if the row ends in italic text. */
19756 static bool
19757 append_space_for_newline (struct it *it, bool default_face_p)
19759 if (FRAME_WINDOW_P (it->f))
19761 int n = it->glyph_row->used[TEXT_AREA];
19763 if (it->glyph_row->glyphs[TEXT_AREA] + n
19764 < it->glyph_row->glyphs[1 + TEXT_AREA])
19766 /* Save some values that must not be changed.
19767 Must save IT->c and IT->len because otherwise
19768 ITERATOR_AT_END_P wouldn't work anymore after
19769 append_space_for_newline has been called. */
19770 enum display_element_type saved_what = it->what;
19771 int saved_c = it->c, saved_len = it->len;
19772 int saved_char_to_display = it->char_to_display;
19773 int saved_x = it->current_x;
19774 int saved_face_id = it->face_id;
19775 bool saved_box_end = it->end_of_box_run_p;
19776 struct text_pos saved_pos;
19777 Lisp_Object saved_object;
19778 struct face *face;
19780 saved_object = it->object;
19781 saved_pos = it->position;
19783 it->what = IT_CHARACTER;
19784 memset (&it->position, 0, sizeof it->position);
19785 it->object = Qnil;
19786 it->c = it->char_to_display = ' ';
19787 it->len = 1;
19789 /* If the default face was remapped, be sure to use the
19790 remapped face for the appended newline. */
19791 if (default_face_p)
19792 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19793 else if (it->face_before_selective_p)
19794 it->face_id = it->saved_face_id;
19795 face = FACE_FROM_ID (it->f, it->face_id);
19796 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19797 /* In R2L rows, we will prepend a stretch glyph that will
19798 have the end_of_box_run_p flag set for it, so there's no
19799 need for the appended newline glyph to have that flag
19800 set. */
19801 if (it->glyph_row->reversed_p
19802 /* But if the appended newline glyph goes all the way to
19803 the end of the row, there will be no stretch glyph,
19804 so leave the box flag set. */
19805 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19806 it->end_of_box_run_p = false;
19808 PRODUCE_GLYPHS (it);
19810 #ifdef HAVE_WINDOW_SYSTEM
19811 /* Make sure this space glyph has the right ascent and
19812 descent values, or else cursor at end of line will look
19813 funny, and height of empty lines will be incorrect. */
19814 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19815 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19816 if (n == 0)
19818 Lisp_Object height, total_height;
19819 int extra_line_spacing = it->extra_line_spacing;
19820 int boff = font->baseline_offset;
19822 if (font->vertical_centering)
19823 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19825 it->object = saved_object; /* get_it_property needs this */
19826 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19827 /* Must do a subset of line height processing from
19828 x_produce_glyph for newline characters. */
19829 height = get_it_property (it, Qline_height);
19830 if (CONSP (height)
19831 && CONSP (XCDR (height))
19832 && NILP (XCDR (XCDR (height))))
19834 total_height = XCAR (XCDR (height));
19835 height = XCAR (height);
19837 else
19838 total_height = Qnil;
19839 height = calc_line_height_property (it, height, font, boff, true);
19841 if (it->override_ascent >= 0)
19843 it->ascent = it->override_ascent;
19844 it->descent = it->override_descent;
19845 boff = it->override_boff;
19847 if (EQ (height, Qt))
19848 extra_line_spacing = 0;
19849 else
19851 Lisp_Object spacing;
19853 it->phys_ascent = it->ascent;
19854 it->phys_descent = it->descent;
19855 if (!NILP (height)
19856 && XINT (height) > it->ascent + it->descent)
19857 it->ascent = XINT (height) - it->descent;
19859 if (!NILP (total_height))
19860 spacing = calc_line_height_property (it, total_height, font,
19861 boff, false);
19862 else
19864 spacing = get_it_property (it, Qline_spacing);
19865 spacing = calc_line_height_property (it, spacing, font,
19866 boff, false);
19868 if (INTEGERP (spacing))
19870 extra_line_spacing = XINT (spacing);
19871 if (!NILP (total_height))
19872 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19875 if (extra_line_spacing > 0)
19877 it->descent += extra_line_spacing;
19878 if (extra_line_spacing > it->max_extra_line_spacing)
19879 it->max_extra_line_spacing = extra_line_spacing;
19881 it->max_ascent = it->ascent;
19882 it->max_descent = it->descent;
19883 /* Make sure compute_line_metrics recomputes the row height. */
19884 it->glyph_row->height = 0;
19887 g->ascent = it->max_ascent;
19888 g->descent = it->max_descent;
19889 #endif
19891 it->override_ascent = -1;
19892 it->constrain_row_ascent_descent_p = false;
19893 it->current_x = saved_x;
19894 it->object = saved_object;
19895 it->position = saved_pos;
19896 it->what = saved_what;
19897 it->face_id = saved_face_id;
19898 it->len = saved_len;
19899 it->c = saved_c;
19900 it->char_to_display = saved_char_to_display;
19901 it->end_of_box_run_p = saved_box_end;
19902 return true;
19906 return false;
19910 /* Extend the face of the last glyph in the text area of IT->glyph_row
19911 to the end of the display line. Called from display_line. If the
19912 glyph row is empty, add a space glyph to it so that we know the
19913 face to draw. Set the glyph row flag fill_line_p. If the glyph
19914 row is R2L, prepend a stretch glyph to cover the empty space to the
19915 left of the leftmost glyph. */
19917 static void
19918 extend_face_to_end_of_line (struct it *it)
19920 struct face *face, *default_face;
19921 struct frame *f = it->f;
19923 /* If line is already filled, do nothing. Non window-system frames
19924 get a grace of one more ``pixel'' because their characters are
19925 1-``pixel'' wide, so they hit the equality too early. This grace
19926 is needed only for R2L rows that are not continued, to produce
19927 one extra blank where we could display the cursor. */
19928 if ((it->current_x >= it->last_visible_x
19929 + (!FRAME_WINDOW_P (f)
19930 && it->glyph_row->reversed_p
19931 && !it->glyph_row->continued_p))
19932 /* If the window has display margins, we will need to extend
19933 their face even if the text area is filled. */
19934 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19935 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19936 return;
19938 /* The default face, possibly remapped. */
19939 default_face = FACE_FROM_ID_OR_NULL (f,
19940 lookup_basic_face (f, DEFAULT_FACE_ID));
19942 /* Face extension extends the background and box of IT->face_id
19943 to the end of the line. If the background equals the background
19944 of the frame, we don't have to do anything. */
19945 face = FACE_FROM_ID (f, (it->face_before_selective_p
19946 ? it->saved_face_id
19947 : it->face_id));
19949 if (FRAME_WINDOW_P (f)
19950 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19951 && face->box == FACE_NO_BOX
19952 && face->background == FRAME_BACKGROUND_PIXEL (f)
19953 #ifdef HAVE_WINDOW_SYSTEM
19954 && !face->stipple
19955 #endif
19956 && !it->glyph_row->reversed_p)
19957 return;
19959 /* Set the glyph row flag indicating that the face of the last glyph
19960 in the text area has to be drawn to the end of the text area. */
19961 it->glyph_row->fill_line_p = true;
19963 /* If current character of IT is not ASCII, make sure we have the
19964 ASCII face. This will be automatically undone the next time
19965 get_next_display_element returns a multibyte character. Note
19966 that the character will always be single byte in unibyte
19967 text. */
19968 if (!ASCII_CHAR_P (it->c))
19970 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19973 if (FRAME_WINDOW_P (f))
19975 /* If the row is empty, add a space with the current face of IT,
19976 so that we know which face to draw. */
19977 if (it->glyph_row->used[TEXT_AREA] == 0)
19979 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19980 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19981 it->glyph_row->used[TEXT_AREA] = 1;
19983 /* Mode line and the header line don't have margins, and
19984 likewise the frame's tool-bar window, if there is any. */
19985 if (!(it->glyph_row->mode_line_p
19986 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19987 || (WINDOWP (f->tool_bar_window)
19988 && it->w == XWINDOW (f->tool_bar_window))
19989 #endif
19992 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19993 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19995 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19996 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19997 default_face->id;
19998 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20000 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20001 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20003 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20004 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20005 default_face->id;
20006 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20009 #ifdef HAVE_WINDOW_SYSTEM
20010 if (it->glyph_row->reversed_p)
20012 /* Prepend a stretch glyph to the row, such that the
20013 rightmost glyph will be drawn flushed all the way to the
20014 right margin of the window. The stretch glyph that will
20015 occupy the empty space, if any, to the left of the
20016 glyphs. */
20017 struct font *font = face->font ? face->font : FRAME_FONT (f);
20018 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20019 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20020 struct glyph *g;
20021 int row_width, stretch_ascent, stretch_width;
20022 struct text_pos saved_pos;
20023 int saved_face_id;
20024 bool saved_avoid_cursor, saved_box_start;
20026 for (row_width = 0, g = row_start; g < row_end; g++)
20027 row_width += g->pixel_width;
20029 /* FIXME: There are various minor display glitches in R2L
20030 rows when only one of the fringes is missing. The
20031 strange condition below produces the least bad effect. */
20032 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20033 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20034 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20035 stretch_width = window_box_width (it->w, TEXT_AREA);
20036 else
20037 stretch_width = it->last_visible_x - it->first_visible_x;
20038 stretch_width -= row_width;
20040 if (stretch_width > 0)
20042 stretch_ascent =
20043 (((it->ascent + it->descent)
20044 * FONT_BASE (font)) / FONT_HEIGHT (font));
20045 saved_pos = it->position;
20046 memset (&it->position, 0, sizeof it->position);
20047 saved_avoid_cursor = it->avoid_cursor_p;
20048 it->avoid_cursor_p = true;
20049 saved_face_id = it->face_id;
20050 saved_box_start = it->start_of_box_run_p;
20051 /* The last row's stretch glyph should get the default
20052 face, to avoid painting the rest of the window with
20053 the region face, if the region ends at ZV. */
20054 if (it->glyph_row->ends_at_zv_p)
20055 it->face_id = default_face->id;
20056 else
20057 it->face_id = face->id;
20058 it->start_of_box_run_p = false;
20059 append_stretch_glyph (it, Qnil, stretch_width,
20060 it->ascent + it->descent, stretch_ascent);
20061 it->position = saved_pos;
20062 it->avoid_cursor_p = saved_avoid_cursor;
20063 it->face_id = saved_face_id;
20064 it->start_of_box_run_p = saved_box_start;
20066 /* If stretch_width comes out negative, it means that the
20067 last glyph is only partially visible. In R2L rows, we
20068 want the leftmost glyph to be partially visible, so we
20069 need to give the row the corresponding left offset. */
20070 if (stretch_width < 0)
20071 it->glyph_row->x = stretch_width;
20073 #endif /* HAVE_WINDOW_SYSTEM */
20075 else
20077 /* Save some values that must not be changed. */
20078 int saved_x = it->current_x;
20079 struct text_pos saved_pos;
20080 Lisp_Object saved_object;
20081 enum display_element_type saved_what = it->what;
20082 int saved_face_id = it->face_id;
20084 saved_object = it->object;
20085 saved_pos = it->position;
20087 it->what = IT_CHARACTER;
20088 memset (&it->position, 0, sizeof it->position);
20089 it->object = Qnil;
20090 it->c = it->char_to_display = ' ';
20091 it->len = 1;
20093 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20094 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20095 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20096 && !it->glyph_row->mode_line_p
20097 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20099 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20100 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20102 for (it->current_x = 0; g < e; g++)
20103 it->current_x += g->pixel_width;
20105 it->area = LEFT_MARGIN_AREA;
20106 it->face_id = default_face->id;
20107 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20108 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20110 PRODUCE_GLYPHS (it);
20111 /* term.c:produce_glyphs advances it->current_x only for
20112 TEXT_AREA. */
20113 it->current_x += it->pixel_width;
20116 it->current_x = saved_x;
20117 it->area = TEXT_AREA;
20120 /* The last row's blank glyphs should get the default face, to
20121 avoid painting the rest of the window with the region face,
20122 if the region ends at ZV. */
20123 if (it->glyph_row->ends_at_zv_p)
20124 it->face_id = default_face->id;
20125 else
20126 it->face_id = face->id;
20127 PRODUCE_GLYPHS (it);
20129 while (it->current_x <= it->last_visible_x)
20130 PRODUCE_GLYPHS (it);
20132 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20133 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20134 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20135 && !it->glyph_row->mode_line_p
20136 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20138 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20139 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20141 for ( ; g < e; g++)
20142 it->current_x += g->pixel_width;
20144 it->area = RIGHT_MARGIN_AREA;
20145 it->face_id = default_face->id;
20146 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20147 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20149 PRODUCE_GLYPHS (it);
20150 it->current_x += it->pixel_width;
20153 it->area = TEXT_AREA;
20156 /* Don't count these blanks really. It would let us insert a left
20157 truncation glyph below and make us set the cursor on them, maybe. */
20158 it->current_x = saved_x;
20159 it->object = saved_object;
20160 it->position = saved_pos;
20161 it->what = saved_what;
20162 it->face_id = saved_face_id;
20167 /* Value is true if text starting at CHARPOS in current_buffer is
20168 trailing whitespace. */
20170 static bool
20171 trailing_whitespace_p (ptrdiff_t charpos)
20173 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20174 int c = 0;
20176 while (bytepos < ZV_BYTE
20177 && (c = FETCH_CHAR (bytepos),
20178 c == ' ' || c == '\t'))
20179 ++bytepos;
20181 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20183 if (bytepos != PT_BYTE)
20184 return true;
20186 return false;
20190 /* Highlight trailing whitespace, if any, in ROW. */
20192 static void
20193 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20195 int used = row->used[TEXT_AREA];
20197 if (used)
20199 struct glyph *start = row->glyphs[TEXT_AREA];
20200 struct glyph *glyph = start + used - 1;
20202 if (row->reversed_p)
20204 /* Right-to-left rows need to be processed in the opposite
20205 direction, so swap the edge pointers. */
20206 glyph = start;
20207 start = row->glyphs[TEXT_AREA] + used - 1;
20210 /* Skip over glyphs inserted to display the cursor at the
20211 end of a line, for extending the face of the last glyph
20212 to the end of the line on terminals, and for truncation
20213 and continuation glyphs. */
20214 if (!row->reversed_p)
20216 while (glyph >= start
20217 && glyph->type == CHAR_GLYPH
20218 && NILP (glyph->object))
20219 --glyph;
20221 else
20223 while (glyph <= start
20224 && glyph->type == CHAR_GLYPH
20225 && NILP (glyph->object))
20226 ++glyph;
20229 /* If last glyph is a space or stretch, and it's trailing
20230 whitespace, set the face of all trailing whitespace glyphs in
20231 IT->glyph_row to `trailing-whitespace'. */
20232 if ((row->reversed_p ? glyph <= start : glyph >= start)
20233 && BUFFERP (glyph->object)
20234 && (glyph->type == STRETCH_GLYPH
20235 || (glyph->type == CHAR_GLYPH
20236 && glyph->u.ch == ' '))
20237 && trailing_whitespace_p (glyph->charpos))
20239 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20240 if (face_id < 0)
20241 return;
20243 if (!row->reversed_p)
20245 while (glyph >= start
20246 && BUFFERP (glyph->object)
20247 && (glyph->type == STRETCH_GLYPH
20248 || (glyph->type == CHAR_GLYPH
20249 && glyph->u.ch == ' ')))
20250 (glyph--)->face_id = face_id;
20252 else
20254 while (glyph <= start
20255 && BUFFERP (glyph->object)
20256 && (glyph->type == STRETCH_GLYPH
20257 || (glyph->type == CHAR_GLYPH
20258 && glyph->u.ch == ' ')))
20259 (glyph++)->face_id = face_id;
20266 /* Value is true if glyph row ROW should be
20267 considered to hold the buffer position CHARPOS. */
20269 static bool
20270 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20272 bool result = true;
20274 if (charpos == CHARPOS (row->end.pos)
20275 || charpos == MATRIX_ROW_END_CHARPOS (row))
20277 /* Suppose the row ends on a string.
20278 Unless the row is continued, that means it ends on a newline
20279 in the string. If it's anything other than a display string
20280 (e.g., a before-string from an overlay), we don't want the
20281 cursor there. (This heuristic seems to give the optimal
20282 behavior for the various types of multi-line strings.)
20283 One exception: if the string has `cursor' property on one of
20284 its characters, we _do_ want the cursor there. */
20285 if (CHARPOS (row->end.string_pos) >= 0)
20287 if (row->continued_p)
20288 result = true;
20289 else
20291 /* Check for `display' property. */
20292 struct glyph *beg = row->glyphs[TEXT_AREA];
20293 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20294 struct glyph *glyph;
20296 result = false;
20297 for (glyph = end; glyph >= beg; --glyph)
20298 if (STRINGP (glyph->object))
20300 Lisp_Object prop
20301 = Fget_char_property (make_number (charpos),
20302 Qdisplay, Qnil);
20303 result =
20304 (!NILP (prop)
20305 && display_prop_string_p (prop, glyph->object));
20306 /* If there's a `cursor' property on one of the
20307 string's characters, this row is a cursor row,
20308 even though this is not a display string. */
20309 if (!result)
20311 Lisp_Object s = glyph->object;
20313 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20315 ptrdiff_t gpos = glyph->charpos;
20317 if (!NILP (Fget_char_property (make_number (gpos),
20318 Qcursor, s)))
20320 result = true;
20321 break;
20325 break;
20329 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20331 /* If the row ends in middle of a real character,
20332 and the line is continued, we want the cursor here.
20333 That's because CHARPOS (ROW->end.pos) would equal
20334 PT if PT is before the character. */
20335 if (!row->ends_in_ellipsis_p)
20336 result = row->continued_p;
20337 else
20338 /* If the row ends in an ellipsis, then
20339 CHARPOS (ROW->end.pos) will equal point after the
20340 invisible text. We want that position to be displayed
20341 after the ellipsis. */
20342 result = false;
20344 /* If the row ends at ZV, display the cursor at the end of that
20345 row instead of at the start of the row below. */
20346 else
20347 result = row->ends_at_zv_p;
20350 return result;
20353 /* Value is true if glyph row ROW should be
20354 used to hold the cursor. */
20356 static bool
20357 cursor_row_p (struct glyph_row *row)
20359 return row_for_charpos_p (row, PT);
20364 /* Push the property PROP so that it will be rendered at the current
20365 position in IT. Return true if PROP was successfully pushed, false
20366 otherwise. Called from handle_line_prefix to handle the
20367 `line-prefix' and `wrap-prefix' properties. */
20369 static bool
20370 push_prefix_prop (struct it *it, Lisp_Object prop)
20372 struct text_pos pos =
20373 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20375 eassert (it->method == GET_FROM_BUFFER
20376 || it->method == GET_FROM_DISPLAY_VECTOR
20377 || it->method == GET_FROM_STRING
20378 || it->method == GET_FROM_IMAGE);
20380 /* We need to save the current buffer/string position, so it will be
20381 restored by pop_it, because iterate_out_of_display_property
20382 depends on that being set correctly, but some situations leave
20383 it->position not yet set when this function is called. */
20384 push_it (it, &pos);
20386 if (STRINGP (prop))
20388 if (SCHARS (prop) == 0)
20390 pop_it (it);
20391 return false;
20394 it->string = prop;
20395 it->string_from_prefix_prop_p = true;
20396 it->multibyte_p = STRING_MULTIBYTE (it->string);
20397 it->current.overlay_string_index = -1;
20398 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20399 it->end_charpos = it->string_nchars = SCHARS (it->string);
20400 it->method = GET_FROM_STRING;
20401 it->stop_charpos = 0;
20402 it->prev_stop = 0;
20403 it->base_level_stop = 0;
20405 /* Force paragraph direction to be that of the parent
20406 buffer/string. */
20407 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20408 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20409 else
20410 it->paragraph_embedding = L2R;
20412 /* Set up the bidi iterator for this display string. */
20413 if (it->bidi_p)
20415 it->bidi_it.string.lstring = it->string;
20416 it->bidi_it.string.s = NULL;
20417 it->bidi_it.string.schars = it->end_charpos;
20418 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20419 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20420 it->bidi_it.string.unibyte = !it->multibyte_p;
20421 it->bidi_it.w = it->w;
20422 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20425 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20427 it->method = GET_FROM_STRETCH;
20428 it->object = prop;
20430 #ifdef HAVE_WINDOW_SYSTEM
20431 else if (IMAGEP (prop))
20433 it->what = IT_IMAGE;
20434 it->image_id = lookup_image (it->f, prop);
20435 it->method = GET_FROM_IMAGE;
20437 #endif /* HAVE_WINDOW_SYSTEM */
20438 else
20440 pop_it (it); /* bogus display property, give up */
20441 return false;
20444 return true;
20447 /* Return the character-property PROP at the current position in IT. */
20449 static Lisp_Object
20450 get_it_property (struct it *it, Lisp_Object prop)
20452 Lisp_Object position, object = it->object;
20454 if (STRINGP (object))
20455 position = make_number (IT_STRING_CHARPOS (*it));
20456 else if (BUFFERP (object))
20458 position = make_number (IT_CHARPOS (*it));
20459 object = it->window;
20461 else
20462 return Qnil;
20464 return Fget_char_property (position, prop, object);
20467 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20469 static void
20470 handle_line_prefix (struct it *it)
20472 Lisp_Object prefix;
20474 if (it->continuation_lines_width > 0)
20476 prefix = get_it_property (it, Qwrap_prefix);
20477 if (NILP (prefix))
20478 prefix = Vwrap_prefix;
20480 else
20482 prefix = get_it_property (it, Qline_prefix);
20483 if (NILP (prefix))
20484 prefix = Vline_prefix;
20486 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20488 /* If the prefix is wider than the window, and we try to wrap
20489 it, it would acquire its own wrap prefix, and so on till the
20490 iterator stack overflows. So, don't wrap the prefix. */
20491 it->line_wrap = TRUNCATE;
20492 it->avoid_cursor_p = true;
20498 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20499 only for R2L lines from display_line and display_string, when they
20500 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20501 the line/string needs to be continued on the next glyph row. */
20502 static void
20503 unproduce_glyphs (struct it *it, int n)
20505 struct glyph *glyph, *end;
20507 eassert (it->glyph_row);
20508 eassert (it->glyph_row->reversed_p);
20509 eassert (it->area == TEXT_AREA);
20510 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20512 if (n > it->glyph_row->used[TEXT_AREA])
20513 n = it->glyph_row->used[TEXT_AREA];
20514 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20515 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20516 for ( ; glyph < end; glyph++)
20517 glyph[-n] = *glyph;
20520 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20521 and ROW->maxpos. */
20522 static void
20523 find_row_edges (struct it *it, struct glyph_row *row,
20524 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20525 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20527 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20528 lines' rows is implemented for bidi-reordered rows. */
20530 /* ROW->minpos is the value of min_pos, the minimal buffer position
20531 we have in ROW, or ROW->start.pos if that is smaller. */
20532 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20533 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20534 else
20535 /* We didn't find buffer positions smaller than ROW->start, or
20536 didn't find _any_ valid buffer positions in any of the glyphs,
20537 so we must trust the iterator's computed positions. */
20538 row->minpos = row->start.pos;
20539 if (max_pos <= 0)
20541 max_pos = CHARPOS (it->current.pos);
20542 max_bpos = BYTEPOS (it->current.pos);
20545 /* Here are the various use-cases for ending the row, and the
20546 corresponding values for ROW->maxpos:
20548 Line ends in a newline from buffer eol_pos + 1
20549 Line is continued from buffer max_pos + 1
20550 Line is truncated on right it->current.pos
20551 Line ends in a newline from string max_pos + 1(*)
20552 (*) + 1 only when line ends in a forward scan
20553 Line is continued from string max_pos
20554 Line is continued from display vector max_pos
20555 Line is entirely from a string min_pos == max_pos
20556 Line is entirely from a display vector min_pos == max_pos
20557 Line that ends at ZV ZV
20559 If you discover other use-cases, please add them here as
20560 appropriate. */
20561 if (row->ends_at_zv_p)
20562 row->maxpos = it->current.pos;
20563 else if (row->used[TEXT_AREA])
20565 bool seen_this_string = false;
20566 struct glyph_row *r1 = row - 1;
20568 /* Did we see the same display string on the previous row? */
20569 if (STRINGP (it->object)
20570 /* this is not the first row */
20571 && row > it->w->desired_matrix->rows
20572 /* previous row is not the header line */
20573 && !r1->mode_line_p
20574 /* previous row also ends in a newline from a string */
20575 && r1->ends_in_newline_from_string_p)
20577 struct glyph *start, *end;
20579 /* Search for the last glyph of the previous row that came
20580 from buffer or string. Depending on whether the row is
20581 L2R or R2L, we need to process it front to back or the
20582 other way round. */
20583 if (!r1->reversed_p)
20585 start = r1->glyphs[TEXT_AREA];
20586 end = start + r1->used[TEXT_AREA];
20587 /* Glyphs inserted by redisplay have nil as their object. */
20588 while (end > start
20589 && NILP ((end - 1)->object)
20590 && (end - 1)->charpos <= 0)
20591 --end;
20592 if (end > start)
20594 if (EQ ((end - 1)->object, it->object))
20595 seen_this_string = true;
20597 else
20598 /* If all the glyphs of the previous row were inserted
20599 by redisplay, it means the previous row was
20600 produced from a single newline, which is only
20601 possible if that newline came from the same string
20602 as the one which produced this ROW. */
20603 seen_this_string = true;
20605 else
20607 end = r1->glyphs[TEXT_AREA] - 1;
20608 start = end + r1->used[TEXT_AREA];
20609 while (end < start
20610 && NILP ((end + 1)->object)
20611 && (end + 1)->charpos <= 0)
20612 ++end;
20613 if (end < start)
20615 if (EQ ((end + 1)->object, it->object))
20616 seen_this_string = true;
20618 else
20619 seen_this_string = true;
20622 /* Take note of each display string that covers a newline only
20623 once, the first time we see it. This is for when a display
20624 string includes more than one newline in it. */
20625 if (row->ends_in_newline_from_string_p && !seen_this_string)
20627 /* If we were scanning the buffer forward when we displayed
20628 the string, we want to account for at least one buffer
20629 position that belongs to this row (position covered by
20630 the display string), so that cursor positioning will
20631 consider this row as a candidate when point is at the end
20632 of the visual line represented by this row. This is not
20633 required when scanning back, because max_pos will already
20634 have a much larger value. */
20635 if (CHARPOS (row->end.pos) > max_pos)
20636 INC_BOTH (max_pos, max_bpos);
20637 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20639 else if (CHARPOS (it->eol_pos) > 0)
20640 SET_TEXT_POS (row->maxpos,
20641 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20642 else if (row->continued_p)
20644 /* If max_pos is different from IT's current position, it
20645 means IT->method does not belong to the display element
20646 at max_pos. However, it also means that the display
20647 element at max_pos was displayed in its entirety on this
20648 line, which is equivalent to saying that the next line
20649 starts at the next buffer position. */
20650 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20651 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20652 else
20654 INC_BOTH (max_pos, max_bpos);
20655 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20658 else if (row->truncated_on_right_p)
20659 /* display_line already called reseat_at_next_visible_line_start,
20660 which puts the iterator at the beginning of the next line, in
20661 the logical order. */
20662 row->maxpos = it->current.pos;
20663 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20664 /* A line that is entirely from a string/image/stretch... */
20665 row->maxpos = row->minpos;
20666 else
20667 emacs_abort ();
20669 else
20670 row->maxpos = it->current.pos;
20673 /* Construct the glyph row IT->glyph_row in the desired matrix of
20674 IT->w from text at the current position of IT. See dispextern.h
20675 for an overview of struct it. Value is true if
20676 IT->glyph_row displays text, as opposed to a line displaying ZV
20677 only. CURSOR_VPOS is the window-relative vertical position of
20678 the glyph row displaying the cursor, or -1 if unknown. */
20680 static bool
20681 display_line (struct it *it, int cursor_vpos)
20683 struct glyph_row *row = it->glyph_row;
20684 Lisp_Object overlay_arrow_string;
20685 struct it wrap_it;
20686 void *wrap_data = NULL;
20687 bool may_wrap = false;
20688 int wrap_x UNINIT;
20689 int wrap_row_used = -1;
20690 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20691 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20692 int wrap_row_extra_line_spacing UNINIT;
20693 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20694 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20695 int cvpos;
20696 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20697 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20698 bool pending_handle_line_prefix = false;
20699 int header_line = WINDOW_WANTS_HEADER_LINE_P (it->w);
20700 bool hscroll_this_line = (cursor_vpos >= 0
20701 && it->vpos == cursor_vpos - header_line
20702 && hscrolling_current_line_p (it->w));
20703 int first_visible_x = it->first_visible_x;
20704 int last_visible_x = it->last_visible_x;
20705 int x_incr = 0;
20707 /* We always start displaying at hpos zero even if hscrolled. */
20708 eassert (it->hpos == 0 && it->current_x == 0);
20710 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20711 >= it->w->desired_matrix->nrows)
20713 it->w->nrows_scale_factor++;
20714 it->f->fonts_changed = true;
20715 return false;
20718 /* Clear the result glyph row and enable it. */
20719 prepare_desired_row (it->w, row, false);
20721 row->y = it->current_y;
20722 row->start = it->start;
20723 row->continuation_lines_width = it->continuation_lines_width;
20724 row->displays_text_p = true;
20725 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20726 it->starts_in_middle_of_char_p = false;
20728 /* Arrange the overlays nicely for our purposes. Usually, we call
20729 display_line on only one line at a time, in which case this
20730 can't really hurt too much, or we call it on lines which appear
20731 one after another in the buffer, in which case all calls to
20732 recenter_overlay_lists but the first will be pretty cheap. */
20733 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20735 /* If we are going to display the cursor's line, account for the
20736 hscroll of that line. We subtract the window's min_hscroll,
20737 because that was already accounted for in init_iterator. */
20738 if (hscroll_this_line)
20739 x_incr =
20740 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
20741 * FRAME_COLUMN_WIDTH (it->f);
20743 /* Move over display elements that are not visible because we are
20744 hscrolled. This may stop at an x-position < first_visible_x
20745 if the first glyph is partially visible or if we hit a line end. */
20746 if (it->current_x < it->first_visible_x + x_incr)
20748 enum move_it_result move_result;
20750 this_line_min_pos = row->start.pos;
20751 if (hscroll_this_line)
20753 it->first_visible_x += x_incr;
20754 it->last_visible_x += x_incr;
20756 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20757 MOVE_TO_POS | MOVE_TO_X);
20758 /* If we are under a large hscroll, move_it_in_display_line_to
20759 could hit the end of the line without reaching
20760 first_visible_x. Pretend that we did reach it. This is
20761 especially important on a TTY, where we will call
20762 extend_face_to_end_of_line, which needs to know how many
20763 blank glyphs to produce. */
20764 if (it->current_x < it->first_visible_x
20765 && (move_result == MOVE_NEWLINE_OR_CR
20766 || move_result == MOVE_POS_MATCH_OR_ZV))
20767 it->current_x = it->first_visible_x;
20769 /* Record the smallest positions seen while we moved over
20770 display elements that are not visible. This is needed by
20771 redisplay_internal for optimizing the case where the cursor
20772 stays inside the same line. The rest of this function only
20773 considers positions that are actually displayed, so
20774 RECORD_MAX_MIN_POS will not otherwise record positions that
20775 are hscrolled to the left of the left edge of the window. */
20776 min_pos = CHARPOS (this_line_min_pos);
20777 min_bpos = BYTEPOS (this_line_min_pos);
20779 else if (it->area == TEXT_AREA)
20781 /* We only do this when not calling move_it_in_display_line_to
20782 above, because that function calls itself handle_line_prefix. */
20783 handle_line_prefix (it);
20785 else
20787 /* Line-prefix and wrap-prefix are always displayed in the text
20788 area. But if this is the first call to display_line after
20789 init_iterator, the iterator might have been set up to write
20790 into a marginal area, e.g. if the line begins with some
20791 display property that writes to the margins. So we need to
20792 wait with the call to handle_line_prefix until whatever
20793 writes to the margin has done its job. */
20794 pending_handle_line_prefix = true;
20797 /* Get the initial row height. This is either the height of the
20798 text hscrolled, if there is any, or zero. */
20799 row->ascent = it->max_ascent;
20800 row->height = it->max_ascent + it->max_descent;
20801 row->phys_ascent = it->max_phys_ascent;
20802 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20803 row->extra_line_spacing = it->max_extra_line_spacing;
20805 /* Utility macro to record max and min buffer positions seen until now. */
20806 #define RECORD_MAX_MIN_POS(IT) \
20807 do \
20809 bool composition_p \
20810 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20811 ptrdiff_t current_pos = \
20812 composition_p ? (IT)->cmp_it.charpos \
20813 : IT_CHARPOS (*(IT)); \
20814 ptrdiff_t current_bpos = \
20815 composition_p ? CHAR_TO_BYTE (current_pos) \
20816 : IT_BYTEPOS (*(IT)); \
20817 if (current_pos < min_pos) \
20819 min_pos = current_pos; \
20820 min_bpos = current_bpos; \
20822 if (IT_CHARPOS (*it) > max_pos) \
20824 max_pos = IT_CHARPOS (*it); \
20825 max_bpos = IT_BYTEPOS (*it); \
20828 while (false)
20830 /* Loop generating characters. The loop is left with IT on the next
20831 character to display. */
20832 while (true)
20834 int n_glyphs_before, hpos_before, x_before;
20835 int x, nglyphs;
20836 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20838 /* Retrieve the next thing to display. Value is false if end of
20839 buffer reached. */
20840 if (!get_next_display_element (it))
20842 /* Maybe add a space at the end of this line that is used to
20843 display the cursor there under X. Set the charpos of the
20844 first glyph of blank lines not corresponding to any text
20845 to -1. */
20846 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20847 row->exact_window_width_line_p = true;
20848 else if ((append_space_for_newline (it, true)
20849 && row->used[TEXT_AREA] == 1)
20850 || row->used[TEXT_AREA] == 0)
20852 row->glyphs[TEXT_AREA]->charpos = -1;
20853 row->displays_text_p = false;
20855 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20856 && (!MINI_WINDOW_P (it->w)
20857 || (minibuf_level && EQ (it->window, minibuf_window))))
20858 row->indicate_empty_line_p = true;
20861 it->continuation_lines_width = 0;
20862 /* Reset those iterator values set from display property
20863 values. This is for the case when the display property
20864 ends at ZV, and is not a replacing property, so pop_it is
20865 not called. */
20866 it->font_height = Qnil;
20867 it->voffset = 0;
20868 row->ends_at_zv_p = true;
20869 /* A row that displays right-to-left text must always have
20870 its last face extended all the way to the end of line,
20871 even if this row ends in ZV, because we still write to
20872 the screen left to right. We also need to extend the
20873 last face if the default face is remapped to some
20874 different face, otherwise the functions that clear
20875 portions of the screen will clear with the default face's
20876 background color. */
20877 if (row->reversed_p
20878 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20879 extend_face_to_end_of_line (it);
20880 break;
20883 /* Now, get the metrics of what we want to display. This also
20884 generates glyphs in `row' (which is IT->glyph_row). */
20885 n_glyphs_before = row->used[TEXT_AREA];
20886 x = it->current_x;
20888 /* Remember the line height so far in case the next element doesn't
20889 fit on the line. */
20890 if (it->line_wrap != TRUNCATE)
20892 ascent = it->max_ascent;
20893 descent = it->max_descent;
20894 phys_ascent = it->max_phys_ascent;
20895 phys_descent = it->max_phys_descent;
20897 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20899 if (IT_DISPLAYING_WHITESPACE (it))
20900 may_wrap = true;
20901 else if (may_wrap)
20903 SAVE_IT (wrap_it, *it, wrap_data);
20904 wrap_x = x;
20905 wrap_row_used = row->used[TEXT_AREA];
20906 wrap_row_ascent = row->ascent;
20907 wrap_row_height = row->height;
20908 wrap_row_phys_ascent = row->phys_ascent;
20909 wrap_row_phys_height = row->phys_height;
20910 wrap_row_extra_line_spacing = row->extra_line_spacing;
20911 wrap_row_min_pos = min_pos;
20912 wrap_row_min_bpos = min_bpos;
20913 wrap_row_max_pos = max_pos;
20914 wrap_row_max_bpos = max_bpos;
20915 may_wrap = false;
20920 PRODUCE_GLYPHS (it);
20922 /* If this display element was in marginal areas, continue with
20923 the next one. */
20924 if (it->area != TEXT_AREA)
20926 row->ascent = max (row->ascent, it->max_ascent);
20927 row->height = max (row->height, it->max_ascent + it->max_descent);
20928 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20929 row->phys_height = max (row->phys_height,
20930 it->max_phys_ascent + it->max_phys_descent);
20931 row->extra_line_spacing = max (row->extra_line_spacing,
20932 it->max_extra_line_spacing);
20933 set_iterator_to_next (it, true);
20934 /* If we didn't handle the line/wrap prefix above, and the
20935 call to set_iterator_to_next just switched to TEXT_AREA,
20936 process the prefix now. */
20937 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20939 pending_handle_line_prefix = false;
20940 handle_line_prefix (it);
20942 continue;
20945 /* Does the display element fit on the line? If we truncate
20946 lines, we should draw past the right edge of the window. If
20947 we don't truncate, we want to stop so that we can display the
20948 continuation glyph before the right margin. If lines are
20949 continued, there are two possible strategies for characters
20950 resulting in more than 1 glyph (e.g. tabs): Display as many
20951 glyphs as possible in this line and leave the rest for the
20952 continuation line, or display the whole element in the next
20953 line. Original redisplay did the former, so we do it also. */
20954 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20955 hpos_before = it->hpos;
20956 x_before = x;
20958 if (/* Not a newline. */
20959 nglyphs > 0
20960 /* Glyphs produced fit entirely in the line. */
20961 && it->current_x < it->last_visible_x)
20963 it->hpos += nglyphs;
20964 row->ascent = max (row->ascent, it->max_ascent);
20965 row->height = max (row->height, it->max_ascent + it->max_descent);
20966 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20967 row->phys_height = max (row->phys_height,
20968 it->max_phys_ascent + it->max_phys_descent);
20969 row->extra_line_spacing = max (row->extra_line_spacing,
20970 it->max_extra_line_spacing);
20971 if (it->current_x - it->pixel_width < it->first_visible_x
20972 /* In R2L rows, we arrange in extend_face_to_end_of_line
20973 to add a right offset to the line, by a suitable
20974 change to the stretch glyph that is the leftmost
20975 glyph of the line. */
20976 && !row->reversed_p)
20977 row->x = x - it->first_visible_x;
20978 /* Record the maximum and minimum buffer positions seen so
20979 far in glyphs that will be displayed by this row. */
20980 if (it->bidi_p)
20981 RECORD_MAX_MIN_POS (it);
20983 else
20985 int i, new_x;
20986 struct glyph *glyph;
20988 for (i = 0; i < nglyphs; ++i, x = new_x)
20990 /* Identify the glyphs added by the last call to
20991 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20992 the previous glyphs. */
20993 if (!row->reversed_p)
20994 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20995 else
20996 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20997 new_x = x + glyph->pixel_width;
20999 if (/* Lines are continued. */
21000 it->line_wrap != TRUNCATE
21001 && (/* Glyph doesn't fit on the line. */
21002 new_x > it->last_visible_x
21003 /* Or it fits exactly on a window system frame. */
21004 || (new_x == it->last_visible_x
21005 && FRAME_WINDOW_P (it->f)
21006 && (row->reversed_p
21007 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21008 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21010 /* End of a continued line. */
21012 if (it->hpos == 0
21013 || (new_x == it->last_visible_x
21014 && FRAME_WINDOW_P (it->f)
21015 && (row->reversed_p
21016 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21017 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21019 /* Current glyph is the only one on the line or
21020 fits exactly on the line. We must continue
21021 the line because we can't draw the cursor
21022 after the glyph. */
21023 row->continued_p = true;
21024 it->current_x = new_x;
21025 it->continuation_lines_width += new_x;
21026 ++it->hpos;
21027 if (i == nglyphs - 1)
21029 /* If line-wrap is on, check if a previous
21030 wrap point was found. */
21031 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21032 && wrap_row_used > 0
21033 /* Even if there is a previous wrap
21034 point, continue the line here as
21035 usual, if (i) the previous character
21036 was a space or tab AND (ii) the
21037 current character is not. */
21038 && (!may_wrap
21039 || IT_DISPLAYING_WHITESPACE (it)))
21040 goto back_to_wrap;
21042 /* Record the maximum and minimum buffer
21043 positions seen so far in glyphs that will be
21044 displayed by this row. */
21045 if (it->bidi_p)
21046 RECORD_MAX_MIN_POS (it);
21047 set_iterator_to_next (it, true);
21048 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21050 if (!get_next_display_element (it))
21052 row->exact_window_width_line_p = true;
21053 it->continuation_lines_width = 0;
21054 it->font_height = Qnil;
21055 it->voffset = 0;
21056 row->continued_p = false;
21057 row->ends_at_zv_p = true;
21059 else if (ITERATOR_AT_END_OF_LINE_P (it))
21061 row->continued_p = false;
21062 row->exact_window_width_line_p = true;
21064 /* If line-wrap is on, check if a
21065 previous wrap point was found. */
21066 else if (wrap_row_used > 0
21067 /* Even if there is a previous wrap
21068 point, continue the line here as
21069 usual, if (i) the previous character
21070 was a space or tab AND (ii) the
21071 current character is not. */
21072 && (!may_wrap
21073 || IT_DISPLAYING_WHITESPACE (it)))
21074 goto back_to_wrap;
21078 else if (it->bidi_p)
21079 RECORD_MAX_MIN_POS (it);
21080 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21081 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21082 extend_face_to_end_of_line (it);
21084 else if (CHAR_GLYPH_PADDING_P (*glyph)
21085 && !FRAME_WINDOW_P (it->f))
21087 /* A padding glyph that doesn't fit on this line.
21088 This means the whole character doesn't fit
21089 on the line. */
21090 if (row->reversed_p)
21091 unproduce_glyphs (it, row->used[TEXT_AREA]
21092 - n_glyphs_before);
21093 row->used[TEXT_AREA] = n_glyphs_before;
21095 /* Fill the rest of the row with continuation
21096 glyphs like in 20.x. */
21097 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21098 < row->glyphs[1 + TEXT_AREA])
21099 produce_special_glyphs (it, IT_CONTINUATION);
21101 row->continued_p = true;
21102 it->current_x = x_before;
21103 it->continuation_lines_width += x_before;
21105 /* Restore the height to what it was before the
21106 element not fitting on the line. */
21107 it->max_ascent = ascent;
21108 it->max_descent = descent;
21109 it->max_phys_ascent = phys_ascent;
21110 it->max_phys_descent = phys_descent;
21111 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21112 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21113 extend_face_to_end_of_line (it);
21115 else if (wrap_row_used > 0)
21117 back_to_wrap:
21118 if (row->reversed_p)
21119 unproduce_glyphs (it,
21120 row->used[TEXT_AREA] - wrap_row_used);
21121 RESTORE_IT (it, &wrap_it, wrap_data);
21122 it->continuation_lines_width += wrap_x;
21123 row->used[TEXT_AREA] = wrap_row_used;
21124 row->ascent = wrap_row_ascent;
21125 row->height = wrap_row_height;
21126 row->phys_ascent = wrap_row_phys_ascent;
21127 row->phys_height = wrap_row_phys_height;
21128 row->extra_line_spacing = wrap_row_extra_line_spacing;
21129 min_pos = wrap_row_min_pos;
21130 min_bpos = wrap_row_min_bpos;
21131 max_pos = wrap_row_max_pos;
21132 max_bpos = wrap_row_max_bpos;
21133 row->continued_p = true;
21134 row->ends_at_zv_p = false;
21135 row->exact_window_width_line_p = false;
21136 it->continuation_lines_width += x;
21138 /* Make sure that a non-default face is extended
21139 up to the right margin of the window. */
21140 extend_face_to_end_of_line (it);
21142 else if ((it->what == IT_CHARACTER
21143 || it->what == IT_STRETCH
21144 || it->what == IT_COMPOSITION)
21145 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21147 /* A TAB that extends past the right edge of the
21148 window. This produces a single glyph on
21149 window system frames. We leave the glyph in
21150 this row and let it fill the row, but don't
21151 consume the TAB. */
21152 if ((row->reversed_p
21153 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21154 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21155 produce_special_glyphs (it, IT_CONTINUATION);
21156 it->continuation_lines_width += it->last_visible_x;
21157 row->ends_in_middle_of_char_p = true;
21158 row->continued_p = true;
21159 glyph->pixel_width = it->last_visible_x - x;
21160 it->starts_in_middle_of_char_p = true;
21161 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21162 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21163 extend_face_to_end_of_line (it);
21165 else
21167 /* Something other than a TAB that draws past
21168 the right edge of the window. Restore
21169 positions to values before the element. */
21170 if (row->reversed_p)
21171 unproduce_glyphs (it, row->used[TEXT_AREA]
21172 - (n_glyphs_before + i));
21173 row->used[TEXT_AREA] = n_glyphs_before + i;
21175 /* Display continuation glyphs. */
21176 it->current_x = x_before;
21177 it->continuation_lines_width += x;
21178 if (!FRAME_WINDOW_P (it->f)
21179 || (row->reversed_p
21180 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21181 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21182 produce_special_glyphs (it, IT_CONTINUATION);
21183 row->continued_p = true;
21185 extend_face_to_end_of_line (it);
21187 if (nglyphs > 1 && i > 0)
21189 row->ends_in_middle_of_char_p = true;
21190 it->starts_in_middle_of_char_p = true;
21193 /* Restore the height to what it was before the
21194 element not fitting on the line. */
21195 it->max_ascent = ascent;
21196 it->max_descent = descent;
21197 it->max_phys_ascent = phys_ascent;
21198 it->max_phys_descent = phys_descent;
21201 break;
21203 else if (new_x > it->first_visible_x)
21205 /* Increment number of glyphs actually displayed. */
21206 ++it->hpos;
21208 /* Record the maximum and minimum buffer positions
21209 seen so far in glyphs that will be displayed by
21210 this row. */
21211 if (it->bidi_p)
21212 RECORD_MAX_MIN_POS (it);
21214 if (x < it->first_visible_x && !row->reversed_p)
21215 /* Glyph is partially visible, i.e. row starts at
21216 negative X position. Don't do that in R2L
21217 rows, where we arrange to add a right offset to
21218 the line in extend_face_to_end_of_line, by a
21219 suitable change to the stretch glyph that is
21220 the leftmost glyph of the line. */
21221 row->x = x - it->first_visible_x;
21222 /* When the last glyph of an R2L row only fits
21223 partially on the line, we need to set row->x to a
21224 negative offset, so that the leftmost glyph is
21225 the one that is partially visible. But if we are
21226 going to produce the truncation glyph, this will
21227 be taken care of in produce_special_glyphs. */
21228 if (row->reversed_p
21229 && new_x > it->last_visible_x
21230 && !(it->line_wrap == TRUNCATE
21231 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21233 eassert (FRAME_WINDOW_P (it->f));
21234 row->x = it->last_visible_x - new_x;
21237 else
21239 /* Glyph is completely off the left margin of the
21240 window. This should not happen because of the
21241 move_it_in_display_line at the start of this
21242 function, unless the text display area of the
21243 window is empty. */
21244 eassert (it->first_visible_x <= it->last_visible_x);
21247 /* Even if this display element produced no glyphs at all,
21248 we want to record its position. */
21249 if (it->bidi_p && nglyphs == 0)
21250 RECORD_MAX_MIN_POS (it);
21252 row->ascent = max (row->ascent, it->max_ascent);
21253 row->height = max (row->height, it->max_ascent + it->max_descent);
21254 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21255 row->phys_height = max (row->phys_height,
21256 it->max_phys_ascent + it->max_phys_descent);
21257 row->extra_line_spacing = max (row->extra_line_spacing,
21258 it->max_extra_line_spacing);
21260 /* End of this display line if row is continued. */
21261 if (row->continued_p || row->ends_at_zv_p)
21262 break;
21265 at_end_of_line:
21266 /* Is this a line end? If yes, we're also done, after making
21267 sure that a non-default face is extended up to the right
21268 margin of the window. */
21269 if (ITERATOR_AT_END_OF_LINE_P (it))
21271 int used_before = row->used[TEXT_AREA];
21273 row->ends_in_newline_from_string_p = STRINGP (it->object);
21275 /* Add a space at the end of the line that is used to
21276 display the cursor there. */
21277 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21278 append_space_for_newline (it, false);
21280 /* Extend the face to the end of the line. */
21281 extend_face_to_end_of_line (it);
21283 /* Make sure we have the position. */
21284 if (used_before == 0)
21285 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21287 /* Record the position of the newline, for use in
21288 find_row_edges. */
21289 it->eol_pos = it->current.pos;
21291 /* Consume the line end. This skips over invisible lines. */
21292 set_iterator_to_next (it, true);
21293 it->continuation_lines_width = 0;
21294 break;
21297 /* Proceed with next display element. Note that this skips
21298 over lines invisible because of selective display. */
21299 set_iterator_to_next (it, true);
21301 /* If we truncate lines, we are done when the last displayed
21302 glyphs reach past the right margin of the window. */
21303 if (it->line_wrap == TRUNCATE
21304 && ((FRAME_WINDOW_P (it->f)
21305 /* Images are preprocessed in produce_image_glyph such
21306 that they are cropped at the right edge of the
21307 window, so an image glyph will always end exactly at
21308 last_visible_x, even if there's no right fringe. */
21309 && ((row->reversed_p
21310 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21311 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21312 || it->what == IT_IMAGE))
21313 ? (it->current_x >= it->last_visible_x)
21314 : (it->current_x > it->last_visible_x)))
21316 /* Maybe add truncation glyphs. */
21317 if (!FRAME_WINDOW_P (it->f)
21318 || (row->reversed_p
21319 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21320 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21322 int i, n;
21324 if (!row->reversed_p)
21326 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21327 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21328 break;
21330 else
21332 for (i = 0; i < row->used[TEXT_AREA]; i++)
21333 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21334 break;
21335 /* Remove any padding glyphs at the front of ROW, to
21336 make room for the truncation glyphs we will be
21337 adding below. The loop below always inserts at
21338 least one truncation glyph, so also remove the
21339 last glyph added to ROW. */
21340 unproduce_glyphs (it, i + 1);
21341 /* Adjust i for the loop below. */
21342 i = row->used[TEXT_AREA] - (i + 1);
21345 /* produce_special_glyphs overwrites the last glyph, so
21346 we don't want that if we want to keep that last
21347 glyph, which means it's an image. */
21348 if (it->current_x > it->last_visible_x)
21350 it->current_x = x_before;
21351 if (!FRAME_WINDOW_P (it->f))
21353 for (n = row->used[TEXT_AREA]; i < n; ++i)
21355 row->used[TEXT_AREA] = i;
21356 produce_special_glyphs (it, IT_TRUNCATION);
21359 else
21361 row->used[TEXT_AREA] = i;
21362 produce_special_glyphs (it, IT_TRUNCATION);
21364 it->hpos = hpos_before;
21367 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21369 /* Don't truncate if we can overflow newline into fringe. */
21370 if (!get_next_display_element (it))
21372 it->continuation_lines_width = 0;
21373 it->font_height = Qnil;
21374 it->voffset = 0;
21375 row->ends_at_zv_p = true;
21376 row->exact_window_width_line_p = true;
21377 break;
21379 if (ITERATOR_AT_END_OF_LINE_P (it))
21381 row->exact_window_width_line_p = true;
21382 goto at_end_of_line;
21384 it->current_x = x_before;
21385 it->hpos = hpos_before;
21388 row->truncated_on_right_p = true;
21389 it->continuation_lines_width = 0;
21390 reseat_at_next_visible_line_start (it, false);
21391 /* We insist below that IT's position be at ZV because in
21392 bidi-reordered lines the character at visible line start
21393 might not be the character that follows the newline in
21394 the logical order. */
21395 if (IT_BYTEPOS (*it) > BEG_BYTE)
21396 row->ends_at_zv_p =
21397 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21398 else
21399 row->ends_at_zv_p = false;
21400 break;
21404 if (wrap_data)
21405 bidi_unshelve_cache (wrap_data, true);
21407 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21408 at the left window margin. */
21409 if (it->first_visible_x
21410 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21412 if (!FRAME_WINDOW_P (it->f)
21413 || (((row->reversed_p
21414 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21415 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21416 /* Don't let insert_left_trunc_glyphs overwrite the
21417 first glyph of the row if it is an image. */
21418 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21419 insert_left_trunc_glyphs (it);
21420 row->truncated_on_left_p = true;
21423 /* Remember the position at which this line ends.
21425 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21426 cannot be before the call to find_row_edges below, since that is
21427 where these positions are determined. */
21428 row->end = it->current;
21429 if (!it->bidi_p)
21431 row->minpos = row->start.pos;
21432 row->maxpos = row->end.pos;
21434 else
21436 /* ROW->minpos and ROW->maxpos must be the smallest and
21437 `1 + the largest' buffer positions in ROW. But if ROW was
21438 bidi-reordered, these two positions can be anywhere in the
21439 row, so we must determine them now. */
21440 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21443 /* If the start of this line is the overlay arrow-position, then
21444 mark this glyph row as the one containing the overlay arrow.
21445 This is clearly a mess with variable size fonts. It would be
21446 better to let it be displayed like cursors under X. */
21447 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21448 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21449 !NILP (overlay_arrow_string)))
21451 /* Overlay arrow in window redisplay is a fringe bitmap. */
21452 if (STRINGP (overlay_arrow_string))
21454 struct glyph_row *arrow_row
21455 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21456 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21457 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21458 struct glyph *p = row->glyphs[TEXT_AREA];
21459 struct glyph *p2, *end;
21461 /* Copy the arrow glyphs. */
21462 while (glyph < arrow_end)
21463 *p++ = *glyph++;
21465 /* Throw away padding glyphs. */
21466 p2 = p;
21467 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21468 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21469 ++p2;
21470 if (p2 > p)
21472 while (p2 < end)
21473 *p++ = *p2++;
21474 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21477 else
21479 eassert (INTEGERP (overlay_arrow_string));
21480 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21482 overlay_arrow_seen = true;
21485 /* Highlight trailing whitespace. */
21486 if (!NILP (Vshow_trailing_whitespace))
21487 highlight_trailing_whitespace (it->f, it->glyph_row);
21489 /* Compute pixel dimensions of this line. */
21490 compute_line_metrics (it);
21492 /* Implementation note: No changes in the glyphs of ROW or in their
21493 faces can be done past this point, because compute_line_metrics
21494 computes ROW's hash value and stores it within the glyph_row
21495 structure. */
21497 /* Record whether this row ends inside an ellipsis. */
21498 row->ends_in_ellipsis_p
21499 = (it->method == GET_FROM_DISPLAY_VECTOR
21500 && it->ellipsis_p);
21502 /* Save fringe bitmaps in this row. */
21503 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21504 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21505 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21506 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21508 it->left_user_fringe_bitmap = 0;
21509 it->left_user_fringe_face_id = 0;
21510 it->right_user_fringe_bitmap = 0;
21511 it->right_user_fringe_face_id = 0;
21513 /* Maybe set the cursor. */
21514 cvpos = it->w->cursor.vpos;
21515 if ((cvpos < 0
21516 /* In bidi-reordered rows, keep checking for proper cursor
21517 position even if one has been found already, because buffer
21518 positions in such rows change non-linearly with ROW->VPOS,
21519 when a line is continued. One exception: when we are at ZV,
21520 display cursor on the first suitable glyph row, since all
21521 the empty rows after that also have their position set to ZV. */
21522 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21523 lines' rows is implemented for bidi-reordered rows. */
21524 || (it->bidi_p
21525 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21526 && PT >= MATRIX_ROW_START_CHARPOS (row)
21527 && PT <= MATRIX_ROW_END_CHARPOS (row)
21528 && cursor_row_p (row))
21529 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21531 /* Prepare for the next line. This line starts horizontally at (X
21532 HPOS) = (0 0). Vertical positions are incremented. As a
21533 convenience for the caller, IT->glyph_row is set to the next
21534 row to be used. */
21535 it->current_x = it->hpos = 0;
21536 it->current_y += row->height;
21537 /* Restore the first and last visible X if we adjusted them for
21538 current-line hscrolling. */
21539 if (hscroll_this_line)
21541 it->first_visible_x = first_visible_x;
21542 it->last_visible_x = last_visible_x;
21544 SET_TEXT_POS (it->eol_pos, 0, 0);
21545 ++it->vpos;
21546 ++it->glyph_row;
21547 /* The next row should by default use the same value of the
21548 reversed_p flag as this one. set_iterator_to_next decides when
21549 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21550 the flag accordingly. */
21551 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21552 it->glyph_row->reversed_p = row->reversed_p;
21553 it->start = row->end;
21554 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21556 #undef RECORD_MAX_MIN_POS
21559 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21560 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21561 doc: /* Return paragraph direction at point in BUFFER.
21562 Value is either `left-to-right' or `right-to-left'.
21563 If BUFFER is omitted or nil, it defaults to the current buffer.
21565 Paragraph direction determines how the text in the paragraph is displayed.
21566 In left-to-right paragraphs, text begins at the left margin of the window
21567 and the reading direction is generally left to right. In right-to-left
21568 paragraphs, text begins at the right margin and is read from right to left.
21570 See also `bidi-paragraph-direction'. */)
21571 (Lisp_Object buffer)
21573 struct buffer *buf = current_buffer;
21574 struct buffer *old = buf;
21576 if (! NILP (buffer))
21578 CHECK_BUFFER (buffer);
21579 buf = XBUFFER (buffer);
21582 if (NILP (BVAR (buf, bidi_display_reordering))
21583 || NILP (BVAR (buf, enable_multibyte_characters))
21584 /* When we are loading loadup.el, the character property tables
21585 needed for bidi iteration are not yet available. */
21586 || redisplay__inhibit_bidi)
21587 return Qleft_to_right;
21588 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21589 return BVAR (buf, bidi_paragraph_direction);
21590 else
21592 /* Determine the direction from buffer text. We could try to
21593 use current_matrix if it is up to date, but this seems fast
21594 enough as it is. */
21595 struct bidi_it itb;
21596 ptrdiff_t pos = BUF_PT (buf);
21597 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21598 int c;
21599 void *itb_data = bidi_shelve_cache ();
21601 set_buffer_temp (buf);
21602 /* bidi_paragraph_init finds the base direction of the paragraph
21603 by searching forward from paragraph start. We need the base
21604 direction of the current or _previous_ paragraph, so we need
21605 to make sure we are within that paragraph. To that end, find
21606 the previous non-empty line. */
21607 if (pos >= ZV && pos > BEGV)
21608 DEC_BOTH (pos, bytepos);
21609 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21610 if (fast_looking_at (trailing_white_space,
21611 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21613 while ((c = FETCH_BYTE (bytepos)) == '\n'
21614 || c == ' ' || c == '\t' || c == '\f')
21616 if (bytepos <= BEGV_BYTE)
21617 break;
21618 bytepos--;
21619 pos--;
21621 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21622 bytepos--;
21624 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21625 itb.paragraph_dir = NEUTRAL_DIR;
21626 itb.string.s = NULL;
21627 itb.string.lstring = Qnil;
21628 itb.string.bufpos = 0;
21629 itb.string.from_disp_str = false;
21630 itb.string.unibyte = false;
21631 /* We have no window to use here for ignoring window-specific
21632 overlays. Using NULL for window pointer will cause
21633 compute_display_string_pos to use the current buffer. */
21634 itb.w = NULL;
21635 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21636 bidi_unshelve_cache (itb_data, false);
21637 set_buffer_temp (old);
21638 switch (itb.paragraph_dir)
21640 case L2R:
21641 return Qleft_to_right;
21642 break;
21643 case R2L:
21644 return Qright_to_left;
21645 break;
21646 default:
21647 emacs_abort ();
21652 DEFUN ("bidi-find-overridden-directionality",
21653 Fbidi_find_overridden_directionality,
21654 Sbidi_find_overridden_directionality, 2, 3, 0,
21655 doc: /* Return position between FROM and TO where directionality was overridden.
21657 This function returns the first character position in the specified
21658 region of OBJECT where there is a character whose `bidi-class' property
21659 is `L', but which was forced to display as `R' by a directional
21660 override, and likewise with characters whose `bidi-class' is `R'
21661 or `AL' that were forced to display as `L'.
21663 If no such character is found, the function returns nil.
21665 OBJECT is a Lisp string or buffer to search for overridden
21666 directionality, and defaults to the current buffer if nil or omitted.
21667 OBJECT can also be a window, in which case the function will search
21668 the buffer displayed in that window. Passing the window instead of
21669 a buffer is preferable when the buffer is displayed in some window,
21670 because this function will then be able to correctly account for
21671 window-specific overlays, which can affect the results.
21673 Strong directional characters `L', `R', and `AL' can have their
21674 intrinsic directionality overridden by directional override
21675 control characters RLO (u+202e) and LRO (u+202d). See the
21676 function `get-char-code-property' for a way to inquire about
21677 the `bidi-class' property of a character. */)
21678 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21680 struct buffer *buf = current_buffer;
21681 struct buffer *old = buf;
21682 struct window *w = NULL;
21683 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21684 struct bidi_it itb;
21685 ptrdiff_t from_pos, to_pos, from_bpos;
21686 void *itb_data;
21688 if (!NILP (object))
21690 if (BUFFERP (object))
21691 buf = XBUFFER (object);
21692 else if (WINDOWP (object))
21694 w = decode_live_window (object);
21695 buf = XBUFFER (w->contents);
21696 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21698 else
21699 CHECK_STRING (object);
21702 if (STRINGP (object))
21704 /* Characters in unibyte strings are always treated by bidi.c as
21705 strong LTR. */
21706 if (!STRING_MULTIBYTE (object)
21707 /* When we are loading loadup.el, the character property
21708 tables needed for bidi iteration are not yet
21709 available. */
21710 || redisplay__inhibit_bidi)
21711 return Qnil;
21713 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21714 if (from_pos >= SCHARS (object))
21715 return Qnil;
21717 /* Set up the bidi iterator. */
21718 itb_data = bidi_shelve_cache ();
21719 itb.paragraph_dir = NEUTRAL_DIR;
21720 itb.string.lstring = object;
21721 itb.string.s = NULL;
21722 itb.string.schars = SCHARS (object);
21723 itb.string.bufpos = 0;
21724 itb.string.from_disp_str = false;
21725 itb.string.unibyte = false;
21726 itb.w = w;
21727 bidi_init_it (0, 0, frame_window_p, &itb);
21729 else
21731 /* Nothing this fancy can happen in unibyte buffers, or in a
21732 buffer that disabled reordering, or if FROM is at EOB. */
21733 if (NILP (BVAR (buf, bidi_display_reordering))
21734 || NILP (BVAR (buf, enable_multibyte_characters))
21735 /* When we are loading loadup.el, the character property
21736 tables needed for bidi iteration are not yet
21737 available. */
21738 || redisplay__inhibit_bidi)
21739 return Qnil;
21741 set_buffer_temp (buf);
21742 validate_region (&from, &to);
21743 from_pos = XINT (from);
21744 to_pos = XINT (to);
21745 if (from_pos >= ZV)
21746 return Qnil;
21748 /* Set up the bidi iterator. */
21749 itb_data = bidi_shelve_cache ();
21750 from_bpos = CHAR_TO_BYTE (from_pos);
21751 if (from_pos == BEGV)
21753 itb.charpos = BEGV;
21754 itb.bytepos = BEGV_BYTE;
21756 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21758 itb.charpos = from_pos;
21759 itb.bytepos = from_bpos;
21761 else
21762 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21763 -1, &itb.bytepos);
21764 itb.paragraph_dir = NEUTRAL_DIR;
21765 itb.string.s = NULL;
21766 itb.string.lstring = Qnil;
21767 itb.string.bufpos = 0;
21768 itb.string.from_disp_str = false;
21769 itb.string.unibyte = false;
21770 itb.w = w;
21771 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21774 ptrdiff_t found;
21775 do {
21776 /* For the purposes of this function, the actual base direction of
21777 the paragraph doesn't matter, so just set it to L2R. */
21778 bidi_paragraph_init (L2R, &itb, false);
21779 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21781 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21783 bidi_unshelve_cache (itb_data, false);
21784 set_buffer_temp (old);
21786 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21789 DEFUN ("move-point-visually", Fmove_point_visually,
21790 Smove_point_visually, 1, 1, 0,
21791 doc: /* Move point in the visual order in the specified DIRECTION.
21792 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21793 left.
21795 Value is the new character position of point. */)
21796 (Lisp_Object direction)
21798 struct window *w = XWINDOW (selected_window);
21799 struct buffer *b = XBUFFER (w->contents);
21800 struct glyph_row *row;
21801 int dir;
21802 Lisp_Object paragraph_dir;
21804 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21805 (!(ROW)->continued_p \
21806 && NILP ((GLYPH)->object) \
21807 && (GLYPH)->type == CHAR_GLYPH \
21808 && (GLYPH)->u.ch == ' ' \
21809 && (GLYPH)->charpos >= 0 \
21810 && !(GLYPH)->avoid_cursor_p)
21812 CHECK_NUMBER (direction);
21813 dir = XINT (direction);
21814 if (dir > 0)
21815 dir = 1;
21816 else
21817 dir = -1;
21819 /* If current matrix is up-to-date, we can use the information
21820 recorded in the glyphs, at least as long as the goal is on the
21821 screen. */
21822 if (w->window_end_valid
21823 && !windows_or_buffers_changed
21824 && b
21825 && !b->clip_changed
21826 && !b->prevent_redisplay_optimizations_p
21827 && !window_outdated (w)
21828 /* We rely below on the cursor coordinates to be up to date, but
21829 we cannot trust them if some command moved point since the
21830 last complete redisplay. */
21831 && w->last_point == BUF_PT (b)
21832 && w->cursor.vpos >= 0
21833 && w->cursor.vpos < w->current_matrix->nrows
21834 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21836 struct glyph *g = row->glyphs[TEXT_AREA];
21837 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21838 struct glyph *gpt = g + w->cursor.hpos;
21840 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21842 if (BUFFERP (g->object) && g->charpos != PT)
21844 SET_PT (g->charpos);
21845 w->cursor.vpos = -1;
21846 return make_number (PT);
21848 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21850 ptrdiff_t new_pos;
21852 if (BUFFERP (gpt->object))
21854 new_pos = PT;
21855 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21856 new_pos += (row->reversed_p ? -dir : dir);
21857 else
21858 new_pos -= (row->reversed_p ? -dir : dir);
21860 else if (BUFFERP (g->object))
21861 new_pos = g->charpos;
21862 else
21863 break;
21864 SET_PT (new_pos);
21865 w->cursor.vpos = -1;
21866 return make_number (PT);
21868 else if (ROW_GLYPH_NEWLINE_P (row, g))
21870 /* Glyphs inserted at the end of a non-empty line for
21871 positioning the cursor have zero charpos, so we must
21872 deduce the value of point by other means. */
21873 if (g->charpos > 0)
21874 SET_PT (g->charpos);
21875 else if (row->ends_at_zv_p && PT != ZV)
21876 SET_PT (ZV);
21877 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21878 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21879 else
21880 break;
21881 w->cursor.vpos = -1;
21882 return make_number (PT);
21885 if (g == e || NILP (g->object))
21887 if (row->truncated_on_left_p || row->truncated_on_right_p)
21888 goto simulate_display;
21889 if (!row->reversed_p)
21890 row += dir;
21891 else
21892 row -= dir;
21893 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21894 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21895 goto simulate_display;
21897 if (dir > 0)
21899 if (row->reversed_p && !row->continued_p)
21901 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21902 w->cursor.vpos = -1;
21903 return make_number (PT);
21905 g = row->glyphs[TEXT_AREA];
21906 e = g + row->used[TEXT_AREA];
21907 for ( ; g < e; g++)
21909 if (BUFFERP (g->object)
21910 /* Empty lines have only one glyph, which stands
21911 for the newline, and whose charpos is the
21912 buffer position of the newline. */
21913 || ROW_GLYPH_NEWLINE_P (row, g)
21914 /* When the buffer ends in a newline, the line at
21915 EOB also has one glyph, but its charpos is -1. */
21916 || (row->ends_at_zv_p
21917 && !row->reversed_p
21918 && NILP (g->object)
21919 && g->type == CHAR_GLYPH
21920 && g->u.ch == ' '))
21922 if (g->charpos > 0)
21923 SET_PT (g->charpos);
21924 else if (!row->reversed_p
21925 && row->ends_at_zv_p
21926 && PT != ZV)
21927 SET_PT (ZV);
21928 else
21929 continue;
21930 w->cursor.vpos = -1;
21931 return make_number (PT);
21935 else
21937 if (!row->reversed_p && !row->continued_p)
21939 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21940 w->cursor.vpos = -1;
21941 return make_number (PT);
21943 e = row->glyphs[TEXT_AREA];
21944 g = e + row->used[TEXT_AREA] - 1;
21945 for ( ; g >= e; g--)
21947 if (BUFFERP (g->object)
21948 || (ROW_GLYPH_NEWLINE_P (row, g)
21949 && g->charpos > 0)
21950 /* Empty R2L lines on GUI frames have the buffer
21951 position of the newline stored in the stretch
21952 glyph. */
21953 || g->type == STRETCH_GLYPH
21954 || (row->ends_at_zv_p
21955 && row->reversed_p
21956 && NILP (g->object)
21957 && g->type == CHAR_GLYPH
21958 && g->u.ch == ' '))
21960 if (g->charpos > 0)
21961 SET_PT (g->charpos);
21962 else if (row->reversed_p
21963 && row->ends_at_zv_p
21964 && PT != ZV)
21965 SET_PT (ZV);
21966 else
21967 continue;
21968 w->cursor.vpos = -1;
21969 return make_number (PT);
21976 simulate_display:
21978 /* If we wind up here, we failed to move by using the glyphs, so we
21979 need to simulate display instead. */
21981 if (b)
21982 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21983 else
21984 paragraph_dir = Qleft_to_right;
21985 if (EQ (paragraph_dir, Qright_to_left))
21986 dir = -dir;
21987 if (PT <= BEGV && dir < 0)
21988 xsignal0 (Qbeginning_of_buffer);
21989 else if (PT >= ZV && dir > 0)
21990 xsignal0 (Qend_of_buffer);
21991 else
21993 struct text_pos pt;
21994 struct it it;
21995 int pt_x, target_x, pixel_width, pt_vpos;
21996 bool at_eol_p;
21997 bool overshoot_expected = false;
21998 bool target_is_eol_p = false;
22000 /* Setup the arena. */
22001 SET_TEXT_POS (pt, PT, PT_BYTE);
22002 start_display (&it, w, pt);
22003 /* When lines are truncated, we could be called with point
22004 outside of the windows edges, in which case move_it_*
22005 functions either prematurely stop at window's edge or jump to
22006 the next screen line, whereas we rely below on our ability to
22007 reach point, in order to start from its X coordinate. So we
22008 need to disregard the window's horizontal extent in that case. */
22009 if (it.line_wrap == TRUNCATE)
22010 it.last_visible_x = INFINITY;
22012 if (it.cmp_it.id < 0
22013 && it.method == GET_FROM_STRING
22014 && it.area == TEXT_AREA
22015 && it.string_from_display_prop_p
22016 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22017 overshoot_expected = true;
22019 /* Find the X coordinate of point. We start from the beginning
22020 of this or previous line to make sure we are before point in
22021 the logical order (since the move_it_* functions can only
22022 move forward). */
22023 reseat:
22024 reseat_at_previous_visible_line_start (&it);
22025 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22026 if (IT_CHARPOS (it) != PT)
22028 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22029 -1, -1, -1, MOVE_TO_POS);
22030 /* If we missed point because the character there is
22031 displayed out of a display vector that has more than one
22032 glyph, retry expecting overshoot. */
22033 if (it.method == GET_FROM_DISPLAY_VECTOR
22034 && it.current.dpvec_index > 0
22035 && !overshoot_expected)
22037 overshoot_expected = true;
22038 goto reseat;
22040 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22041 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22043 pt_x = it.current_x;
22044 pt_vpos = it.vpos;
22045 if (dir > 0 || overshoot_expected)
22047 struct glyph_row *row = it.glyph_row;
22049 /* When point is at beginning of line, we don't have
22050 information about the glyph there loaded into struct
22051 it. Calling get_next_display_element fixes that. */
22052 if (pt_x == 0)
22053 get_next_display_element (&it);
22054 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22055 it.glyph_row = NULL;
22056 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22057 it.glyph_row = row;
22058 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22059 it, lest it will become out of sync with it's buffer
22060 position. */
22061 it.current_x = pt_x;
22063 else
22064 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22065 pixel_width = it.pixel_width;
22066 if (overshoot_expected && at_eol_p)
22067 pixel_width = 0;
22068 else if (pixel_width <= 0)
22069 pixel_width = 1;
22071 /* If there's a display string (or something similar) at point,
22072 we are actually at the glyph to the left of point, so we need
22073 to correct the X coordinate. */
22074 if (overshoot_expected)
22076 if (it.bidi_p)
22077 pt_x += pixel_width * it.bidi_it.scan_dir;
22078 else
22079 pt_x += pixel_width;
22082 /* Compute target X coordinate, either to the left or to the
22083 right of point. On TTY frames, all characters have the same
22084 pixel width of 1, so we can use that. On GUI frames we don't
22085 have an easy way of getting at the pixel width of the
22086 character to the left of point, so we use a different method
22087 of getting to that place. */
22088 if (dir > 0)
22089 target_x = pt_x + pixel_width;
22090 else
22091 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22093 /* Target X coordinate could be one line above or below the line
22094 of point, in which case we need to adjust the target X
22095 coordinate. Also, if moving to the left, we need to begin at
22096 the left edge of the point's screen line. */
22097 if (dir < 0)
22099 if (pt_x > 0)
22101 start_display (&it, w, pt);
22102 if (it.line_wrap == TRUNCATE)
22103 it.last_visible_x = INFINITY;
22104 reseat_at_previous_visible_line_start (&it);
22105 it.current_x = it.current_y = it.hpos = 0;
22106 if (pt_vpos != 0)
22107 move_it_by_lines (&it, pt_vpos);
22109 else
22111 move_it_by_lines (&it, -1);
22112 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22113 target_is_eol_p = true;
22114 /* Under word-wrap, we don't know the x coordinate of
22115 the last character displayed on the previous line,
22116 which immediately precedes the wrap point. To find
22117 out its x coordinate, we try moving to the right
22118 margin of the window, which will stop at the wrap
22119 point, and then reset target_x to point at the
22120 character that precedes the wrap point. This is not
22121 needed on GUI frames, because (see below) there we
22122 move from the left margin one grapheme cluster at a
22123 time, and stop when we hit the wrap point. */
22124 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22126 void *it_data = NULL;
22127 struct it it2;
22129 SAVE_IT (it2, it, it_data);
22130 move_it_in_display_line_to (&it, ZV, target_x,
22131 MOVE_TO_POS | MOVE_TO_X);
22132 /* If we arrived at target_x, that _is_ the last
22133 character on the previous line. */
22134 if (it.current_x != target_x)
22135 target_x = it.current_x - 1;
22136 RESTORE_IT (&it, &it2, it_data);
22140 else
22142 if (at_eol_p
22143 || (target_x >= it.last_visible_x
22144 && it.line_wrap != TRUNCATE))
22146 if (pt_x > 0)
22147 move_it_by_lines (&it, 0);
22148 move_it_by_lines (&it, 1);
22149 target_x = 0;
22153 /* Move to the target X coordinate. */
22154 /* On GUI frames, as we don't know the X coordinate of the
22155 character to the left of point, moving point to the left
22156 requires walking, one grapheme cluster at a time, until we
22157 find ourself at a place immediately to the left of the
22158 character at point. */
22159 if (FRAME_WINDOW_P (it.f) && dir < 0)
22161 struct text_pos new_pos;
22162 enum move_it_result rc = MOVE_X_REACHED;
22164 if (it.current_x == 0)
22165 get_next_display_element (&it);
22166 if (it.what == IT_COMPOSITION)
22168 new_pos.charpos = it.cmp_it.charpos;
22169 new_pos.bytepos = -1;
22171 else
22172 new_pos = it.current.pos;
22174 while (it.current_x + it.pixel_width <= target_x
22175 && (rc == MOVE_X_REACHED
22176 /* Under word-wrap, move_it_in_display_line_to
22177 stops at correct coordinates, but sometimes
22178 returns MOVE_POS_MATCH_OR_ZV. */
22179 || (it.line_wrap == WORD_WRAP
22180 && rc == MOVE_POS_MATCH_OR_ZV)))
22182 int new_x = it.current_x + it.pixel_width;
22184 /* For composed characters, we want the position of the
22185 first character in the grapheme cluster (usually, the
22186 composition's base character), whereas it.current
22187 might give us the position of the _last_ one, e.g. if
22188 the composition is rendered in reverse due to bidi
22189 reordering. */
22190 if (it.what == IT_COMPOSITION)
22192 new_pos.charpos = it.cmp_it.charpos;
22193 new_pos.bytepos = -1;
22195 else
22196 new_pos = it.current.pos;
22197 if (new_x == it.current_x)
22198 new_x++;
22199 rc = move_it_in_display_line_to (&it, ZV, new_x,
22200 MOVE_TO_POS | MOVE_TO_X);
22201 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22202 break;
22204 /* The previous position we saw in the loop is the one we
22205 want. */
22206 if (new_pos.bytepos == -1)
22207 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22208 it.current.pos = new_pos;
22210 else if (it.current_x != target_x)
22211 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22213 /* If we ended up in a display string that covers point, move to
22214 buffer position to the right in the visual order. */
22215 if (dir > 0)
22217 while (IT_CHARPOS (it) == PT)
22219 set_iterator_to_next (&it, false);
22220 if (!get_next_display_element (&it))
22221 break;
22225 /* Move point to that position. */
22226 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22229 return make_number (PT);
22231 #undef ROW_GLYPH_NEWLINE_P
22234 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22235 Sbidi_resolved_levels, 0, 1, 0,
22236 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22238 The resolved levels are produced by the Emacs bidi reordering engine
22239 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22240 read the Unicode Standard Annex 9 (UAX#9) for background information
22241 about these levels.
22243 VPOS is the zero-based number of the current window's screen line
22244 for which to produce the resolved levels. If VPOS is nil or omitted,
22245 it defaults to the screen line of point. If the window displays a
22246 header line, VPOS of zero will report on the header line, and first
22247 line of text in the window will have VPOS of 1.
22249 Value is an array of resolved levels, indexed by glyph number.
22250 Glyphs are numbered from zero starting from the beginning of the
22251 screen line, i.e. the left edge of the window for left-to-right lines
22252 and from the right edge for right-to-left lines. The resolved levels
22253 are produced only for the window's text area; text in display margins
22254 is not included.
22256 If the selected window's display is not up-to-date, or if the specified
22257 screen line does not display text, this function returns nil. It is
22258 highly recommended to bind this function to some simple key, like F8,
22259 in order to avoid these problems.
22261 This function exists mainly for testing the correctness of the
22262 Emacs UBA implementation, in particular with the test suite. */)
22263 (Lisp_Object vpos)
22265 struct window *w = XWINDOW (selected_window);
22266 struct buffer *b = XBUFFER (w->contents);
22267 int nrow;
22268 struct glyph_row *row;
22270 if (NILP (vpos))
22272 int d1, d2, d3, d4, d5;
22274 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22276 else
22278 CHECK_NUMBER_COERCE_MARKER (vpos);
22279 nrow = XINT (vpos);
22282 /* We require up-to-date glyph matrix for this window. */
22283 if (w->window_end_valid
22284 && !windows_or_buffers_changed
22285 && b
22286 && !b->clip_changed
22287 && !b->prevent_redisplay_optimizations_p
22288 && !window_outdated (w)
22289 && nrow >= 0
22290 && nrow < w->current_matrix->nrows
22291 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22292 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22294 struct glyph *g, *e, *g1;
22295 int nglyphs, i;
22296 Lisp_Object levels;
22298 if (!row->reversed_p) /* Left-to-right glyph row. */
22300 g = g1 = row->glyphs[TEXT_AREA];
22301 e = g + row->used[TEXT_AREA];
22303 /* Skip over glyphs at the start of the row that was
22304 generated by redisplay for its own needs. */
22305 while (g < e
22306 && NILP (g->object)
22307 && g->charpos < 0)
22308 g++;
22309 g1 = g;
22311 /* Count the "interesting" glyphs in this row. */
22312 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22313 nglyphs++;
22315 /* Create and fill the array. */
22316 levels = make_uninit_vector (nglyphs);
22317 for (i = 0; g1 < g; i++, g1++)
22318 ASET (levels, i, make_number (g1->resolved_level));
22320 else /* Right-to-left glyph row. */
22322 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22323 e = row->glyphs[TEXT_AREA] - 1;
22324 while (g > e
22325 && NILP (g->object)
22326 && g->charpos < 0)
22327 g--;
22328 g1 = g;
22329 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22330 nglyphs++;
22331 levels = make_uninit_vector (nglyphs);
22332 for (i = 0; g1 > g; i++, g1--)
22333 ASET (levels, i, make_number (g1->resolved_level));
22335 return levels;
22337 else
22338 return Qnil;
22343 /***********************************************************************
22344 Menu Bar
22345 ***********************************************************************/
22347 /* Redisplay the menu bar in the frame for window W.
22349 The menu bar of X frames that don't have X toolkit support is
22350 displayed in a special window W->frame->menu_bar_window.
22352 The menu bar of terminal frames is treated specially as far as
22353 glyph matrices are concerned. Menu bar lines are not part of
22354 windows, so the update is done directly on the frame matrix rows
22355 for the menu bar. */
22357 static void
22358 display_menu_bar (struct window *w)
22360 struct frame *f = XFRAME (WINDOW_FRAME (w));
22361 struct it it;
22362 Lisp_Object items;
22363 int i;
22365 /* Don't do all this for graphical frames. */
22366 #ifdef HAVE_NTGUI
22367 if (FRAME_W32_P (f))
22368 return;
22369 #endif
22370 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22371 if (FRAME_X_P (f))
22372 return;
22373 #endif
22375 #ifdef HAVE_NS
22376 if (FRAME_NS_P (f))
22377 return;
22378 #endif /* HAVE_NS */
22380 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22381 eassert (!FRAME_WINDOW_P (f));
22382 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22383 it.first_visible_x = 0;
22384 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22385 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22386 if (FRAME_WINDOW_P (f))
22388 /* Menu bar lines are displayed in the desired matrix of the
22389 dummy window menu_bar_window. */
22390 struct window *menu_w;
22391 menu_w = XWINDOW (f->menu_bar_window);
22392 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22393 MENU_FACE_ID);
22394 it.first_visible_x = 0;
22395 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22397 else
22398 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22400 /* This is a TTY frame, i.e. character hpos/vpos are used as
22401 pixel x/y. */
22402 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22403 MENU_FACE_ID);
22404 it.first_visible_x = 0;
22405 it.last_visible_x = FRAME_COLS (f);
22408 /* FIXME: This should be controlled by a user option. See the
22409 comments in redisplay_tool_bar and display_mode_line about
22410 this. */
22411 it.paragraph_embedding = L2R;
22413 /* Clear all rows of the menu bar. */
22414 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22416 struct glyph_row *row = it.glyph_row + i;
22417 clear_glyph_row (row);
22418 row->enabled_p = true;
22419 row->full_width_p = true;
22420 row->reversed_p = false;
22423 /* Display all items of the menu bar. */
22424 items = FRAME_MENU_BAR_ITEMS (it.f);
22425 for (i = 0; i < ASIZE (items); i += 4)
22427 Lisp_Object string;
22429 /* Stop at nil string. */
22430 string = AREF (items, i + 1);
22431 if (NILP (string))
22432 break;
22434 /* Remember where item was displayed. */
22435 ASET (items, i + 3, make_number (it.hpos));
22437 /* Display the item, pad with one space. */
22438 if (it.current_x < it.last_visible_x)
22439 display_string (NULL, string, Qnil, 0, 0, &it,
22440 SCHARS (string) + 1, 0, 0, -1);
22443 /* Fill out the line with spaces. */
22444 if (it.current_x < it.last_visible_x)
22445 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22447 /* Compute the total height of the lines. */
22448 compute_line_metrics (&it);
22451 /* Deep copy of a glyph row, including the glyphs. */
22452 static void
22453 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22455 struct glyph *pointers[1 + LAST_AREA];
22456 int to_used = to->used[TEXT_AREA];
22458 /* Save glyph pointers of TO. */
22459 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22461 /* Do a structure assignment. */
22462 *to = *from;
22464 /* Restore original glyph pointers of TO. */
22465 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22467 /* Copy the glyphs. */
22468 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22469 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22471 /* If we filled only part of the TO row, fill the rest with
22472 space_glyph (which will display as empty space). */
22473 if (to_used > from->used[TEXT_AREA])
22474 fill_up_frame_row_with_spaces (to, to_used);
22477 /* Display one menu item on a TTY, by overwriting the glyphs in the
22478 frame F's desired glyph matrix with glyphs produced from the menu
22479 item text. Called from term.c to display TTY drop-down menus one
22480 item at a time.
22482 ITEM_TEXT is the menu item text as a C string.
22484 FACE_ID is the face ID to be used for this menu item. FACE_ID
22485 could specify one of 3 faces: a face for an enabled item, a face
22486 for a disabled item, or a face for a selected item.
22488 X and Y are coordinates of the first glyph in the frame's desired
22489 matrix to be overwritten by the menu item. Since this is a TTY, Y
22490 is the zero-based number of the glyph row and X is the zero-based
22491 glyph number in the row, starting from left, where to start
22492 displaying the item.
22494 SUBMENU means this menu item drops down a submenu, which
22495 should be indicated by displaying a proper visual cue after the
22496 item text. */
22498 void
22499 display_tty_menu_item (const char *item_text, int width, int face_id,
22500 int x, int y, bool submenu)
22502 struct it it;
22503 struct frame *f = SELECTED_FRAME ();
22504 struct window *w = XWINDOW (f->selected_window);
22505 struct glyph_row *row;
22506 size_t item_len = strlen (item_text);
22508 eassert (FRAME_TERMCAP_P (f));
22510 /* Don't write beyond the matrix's last row. This can happen for
22511 TTY screens that are not high enough to show the entire menu.
22512 (This is actually a bit of defensive programming, as
22513 tty_menu_display already limits the number of menu items to one
22514 less than the number of screen lines.) */
22515 if (y >= f->desired_matrix->nrows)
22516 return;
22518 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22519 it.first_visible_x = 0;
22520 it.last_visible_x = FRAME_COLS (f) - 1;
22521 row = it.glyph_row;
22522 /* Start with the row contents from the current matrix. */
22523 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22524 bool saved_width = row->full_width_p;
22525 row->full_width_p = true;
22526 bool saved_reversed = row->reversed_p;
22527 row->reversed_p = false;
22528 row->enabled_p = true;
22530 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22531 desired face. */
22532 eassert (x < f->desired_matrix->matrix_w);
22533 it.current_x = it.hpos = x;
22534 it.current_y = it.vpos = y;
22535 int saved_used = row->used[TEXT_AREA];
22536 bool saved_truncated = row->truncated_on_right_p;
22537 row->used[TEXT_AREA] = x;
22538 it.face_id = face_id;
22539 it.line_wrap = TRUNCATE;
22541 /* FIXME: This should be controlled by a user option. See the
22542 comments in redisplay_tool_bar and display_mode_line about this.
22543 Also, if paragraph_embedding could ever be R2L, changes will be
22544 needed to avoid shifting to the right the row characters in
22545 term.c:append_glyph. */
22546 it.paragraph_embedding = L2R;
22548 /* Pad with a space on the left. */
22549 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22550 width--;
22551 /* Display the menu item, pad with spaces to WIDTH. */
22552 if (submenu)
22554 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22555 item_len, 0, FRAME_COLS (f) - 1, -1);
22556 width -= item_len;
22557 /* Indicate with " >" that there's a submenu. */
22558 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22559 FRAME_COLS (f) - 1, -1);
22561 else
22562 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22563 width, 0, FRAME_COLS (f) - 1, -1);
22565 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22566 row->truncated_on_right_p = saved_truncated;
22567 row->hash = row_hash (row);
22568 row->full_width_p = saved_width;
22569 row->reversed_p = saved_reversed;
22572 /***********************************************************************
22573 Mode Line
22574 ***********************************************************************/
22576 /* Redisplay mode lines in the window tree whose root is WINDOW.
22577 If FORCE, redisplay mode lines unconditionally.
22578 Otherwise, redisplay only mode lines that are garbaged. Value is
22579 the number of windows whose mode lines were redisplayed. */
22581 static int
22582 redisplay_mode_lines (Lisp_Object window, bool force)
22584 int nwindows = 0;
22586 while (!NILP (window))
22588 struct window *w = XWINDOW (window);
22590 if (WINDOWP (w->contents))
22591 nwindows += redisplay_mode_lines (w->contents, force);
22592 else if (force
22593 || FRAME_GARBAGED_P (XFRAME (w->frame))
22594 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22596 struct text_pos lpoint;
22597 struct buffer *old = current_buffer;
22599 /* Set the window's buffer for the mode line display. */
22600 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22601 set_buffer_internal_1 (XBUFFER (w->contents));
22603 /* Point refers normally to the selected window. For any
22604 other window, set up appropriate value. */
22605 if (!EQ (window, selected_window))
22607 struct text_pos pt;
22609 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22610 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22613 /* Display mode lines. */
22614 clear_glyph_matrix (w->desired_matrix);
22615 if (display_mode_lines (w))
22616 ++nwindows;
22618 /* Restore old settings. */
22619 set_buffer_internal_1 (old);
22620 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22623 window = w->next;
22626 return nwindows;
22630 /* Display the mode and/or header line of window W. Value is the
22631 sum number of mode lines and header lines displayed. */
22633 static int
22634 display_mode_lines (struct window *w)
22636 Lisp_Object old_selected_window = selected_window;
22637 Lisp_Object old_selected_frame = selected_frame;
22638 Lisp_Object new_frame = w->frame;
22639 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22640 int n = 0;
22642 selected_frame = new_frame;
22643 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22644 or window's point, then we'd need select_window_1 here as well. */
22645 XSETWINDOW (selected_window, w);
22646 XFRAME (new_frame)->selected_window = selected_window;
22648 /* These will be set while the mode line specs are processed. */
22649 line_number_displayed = false;
22650 w->column_number_displayed = -1;
22652 if (WINDOW_WANTS_MODELINE_P (w))
22654 struct window *sel_w = XWINDOW (old_selected_window);
22656 /* Select mode line face based on the real selected window. */
22657 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22658 BVAR (current_buffer, mode_line_format));
22659 ++n;
22662 if (WINDOW_WANTS_HEADER_LINE_P (w))
22664 display_mode_line (w, HEADER_LINE_FACE_ID,
22665 BVAR (current_buffer, header_line_format));
22666 ++n;
22669 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22670 selected_frame = old_selected_frame;
22671 selected_window = old_selected_window;
22672 if (n > 0)
22673 w->must_be_updated_p = true;
22674 return n;
22678 /* Display mode or header line of window W. FACE_ID specifies which
22679 line to display; it is either MODE_LINE_FACE_ID or
22680 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22681 display. Value is the pixel height of the mode/header line
22682 displayed. */
22684 static int
22685 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22687 struct it it;
22688 struct face *face;
22689 ptrdiff_t count = SPECPDL_INDEX ();
22691 init_iterator (&it, w, -1, -1, NULL, face_id);
22692 /* Don't extend on a previously drawn mode-line.
22693 This may happen if called from pos_visible_p. */
22694 it.glyph_row->enabled_p = false;
22695 prepare_desired_row (w, it.glyph_row, true);
22697 it.glyph_row->mode_line_p = true;
22699 /* FIXME: This should be controlled by a user option. But
22700 supporting such an option is not trivial, since the mode line is
22701 made up of many separate strings. */
22702 it.paragraph_embedding = L2R;
22704 record_unwind_protect (unwind_format_mode_line,
22705 format_mode_line_unwind_data (NULL, NULL,
22706 Qnil, false));
22708 mode_line_target = MODE_LINE_DISPLAY;
22710 /* Temporarily make frame's keyboard the current kboard so that
22711 kboard-local variables in the mode_line_format will get the right
22712 values. */
22713 push_kboard (FRAME_KBOARD (it.f));
22714 record_unwind_save_match_data ();
22715 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22716 pop_kboard ();
22718 unbind_to (count, Qnil);
22720 /* Fill up with spaces. */
22721 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22723 compute_line_metrics (&it);
22724 it.glyph_row->full_width_p = true;
22725 it.glyph_row->continued_p = false;
22726 it.glyph_row->truncated_on_left_p = false;
22727 it.glyph_row->truncated_on_right_p = false;
22729 /* Make a 3D mode-line have a shadow at its right end. */
22730 face = FACE_FROM_ID (it.f, face_id);
22731 extend_face_to_end_of_line (&it);
22732 if (face->box != FACE_NO_BOX)
22734 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22735 + it.glyph_row->used[TEXT_AREA] - 1);
22736 last->right_box_line_p = true;
22739 return it.glyph_row->height;
22742 /* Move element ELT in LIST to the front of LIST.
22743 Return the updated list. */
22745 static Lisp_Object
22746 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22748 register Lisp_Object tail, prev;
22749 register Lisp_Object tem;
22751 tail = list;
22752 prev = Qnil;
22753 while (CONSP (tail))
22755 tem = XCAR (tail);
22757 if (EQ (elt, tem))
22759 /* Splice out the link TAIL. */
22760 if (NILP (prev))
22761 list = XCDR (tail);
22762 else
22763 Fsetcdr (prev, XCDR (tail));
22765 /* Now make it the first. */
22766 Fsetcdr (tail, list);
22767 return tail;
22769 else
22770 prev = tail;
22771 tail = XCDR (tail);
22772 maybe_quit ();
22775 /* Not found--return unchanged LIST. */
22776 return list;
22779 /* Contribute ELT to the mode line for window IT->w. How it
22780 translates into text depends on its data type.
22782 IT describes the display environment in which we display, as usual.
22784 DEPTH is the depth in recursion. It is used to prevent
22785 infinite recursion here.
22787 FIELD_WIDTH is the number of characters the display of ELT should
22788 occupy in the mode line, and PRECISION is the maximum number of
22789 characters to display from ELT's representation. See
22790 display_string for details.
22792 Returns the hpos of the end of the text generated by ELT.
22794 PROPS is a property list to add to any string we encounter.
22796 If RISKY, remove (disregard) any properties in any string
22797 we encounter, and ignore :eval and :propertize.
22799 The global variable `mode_line_target' determines whether the
22800 output is passed to `store_mode_line_noprop',
22801 `store_mode_line_string', or `display_string'. */
22803 static int
22804 display_mode_element (struct it *it, int depth, int field_width, int precision,
22805 Lisp_Object elt, Lisp_Object props, bool risky)
22807 int n = 0, field, prec;
22808 bool literal = false;
22810 tail_recurse:
22811 if (depth > 100)
22812 elt = build_string ("*too-deep*");
22814 depth++;
22816 switch (XTYPE (elt))
22818 case Lisp_String:
22820 /* A string: output it and check for %-constructs within it. */
22821 unsigned char c;
22822 ptrdiff_t offset = 0;
22824 if (SCHARS (elt) > 0
22825 && (!NILP (props) || risky))
22827 Lisp_Object oprops, aelt;
22828 oprops = Ftext_properties_at (make_number (0), elt);
22830 /* If the starting string's properties are not what
22831 we want, translate the string. Also, if the string
22832 is risky, do that anyway. */
22834 if (NILP (Fequal (props, oprops)) || risky)
22836 /* If the starting string has properties,
22837 merge the specified ones onto the existing ones. */
22838 if (! NILP (oprops) && !risky)
22840 Lisp_Object tem;
22842 oprops = Fcopy_sequence (oprops);
22843 tem = props;
22844 while (CONSP (tem))
22846 oprops = Fplist_put (oprops, XCAR (tem),
22847 XCAR (XCDR (tem)));
22848 tem = XCDR (XCDR (tem));
22850 props = oprops;
22853 aelt = Fassoc (elt, mode_line_proptrans_alist);
22854 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22856 /* AELT is what we want. Move it to the front
22857 without consing. */
22858 elt = XCAR (aelt);
22859 mode_line_proptrans_alist
22860 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22862 else
22864 Lisp_Object tem;
22866 /* If AELT has the wrong props, it is useless.
22867 so get rid of it. */
22868 if (! NILP (aelt))
22869 mode_line_proptrans_alist
22870 = Fdelq (aelt, mode_line_proptrans_alist);
22872 elt = Fcopy_sequence (elt);
22873 Fset_text_properties (make_number (0), Flength (elt),
22874 props, elt);
22875 /* Add this item to mode_line_proptrans_alist. */
22876 mode_line_proptrans_alist
22877 = Fcons (Fcons (elt, props),
22878 mode_line_proptrans_alist);
22879 /* Truncate mode_line_proptrans_alist
22880 to at most 50 elements. */
22881 tem = Fnthcdr (make_number (50),
22882 mode_line_proptrans_alist);
22883 if (! NILP (tem))
22884 XSETCDR (tem, Qnil);
22889 offset = 0;
22891 if (literal)
22893 prec = precision - n;
22894 switch (mode_line_target)
22896 case MODE_LINE_NOPROP:
22897 case MODE_LINE_TITLE:
22898 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22899 break;
22900 case MODE_LINE_STRING:
22901 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22902 break;
22903 case MODE_LINE_DISPLAY:
22904 n += display_string (NULL, elt, Qnil, 0, 0, it,
22905 0, prec, 0, STRING_MULTIBYTE (elt));
22906 break;
22909 break;
22912 /* Handle the non-literal case. */
22914 while ((precision <= 0 || n < precision)
22915 && SREF (elt, offset) != 0
22916 && (mode_line_target != MODE_LINE_DISPLAY
22917 || it->current_x < it->last_visible_x))
22919 ptrdiff_t last_offset = offset;
22921 /* Advance to end of string or next format specifier. */
22922 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22925 if (offset - 1 != last_offset)
22927 ptrdiff_t nchars, nbytes;
22929 /* Output to end of string or up to '%'. Field width
22930 is length of string. Don't output more than
22931 PRECISION allows us. */
22932 offset--;
22934 prec = c_string_width (SDATA (elt) + last_offset,
22935 offset - last_offset, precision - n,
22936 &nchars, &nbytes);
22938 switch (mode_line_target)
22940 case MODE_LINE_NOPROP:
22941 case MODE_LINE_TITLE:
22942 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22943 break;
22944 case MODE_LINE_STRING:
22946 ptrdiff_t bytepos = last_offset;
22947 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22948 ptrdiff_t endpos = (precision <= 0
22949 ? string_byte_to_char (elt, offset)
22950 : charpos + nchars);
22951 Lisp_Object mode_string
22952 = Fsubstring (elt, make_number (charpos),
22953 make_number (endpos));
22954 n += store_mode_line_string (NULL, mode_string, false,
22955 0, 0, Qnil);
22957 break;
22958 case MODE_LINE_DISPLAY:
22960 ptrdiff_t bytepos = last_offset;
22961 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22963 if (precision <= 0)
22964 nchars = string_byte_to_char (elt, offset) - charpos;
22965 n += display_string (NULL, elt, Qnil, 0, charpos,
22966 it, 0, nchars, 0,
22967 STRING_MULTIBYTE (elt));
22969 break;
22972 else /* c == '%' */
22974 ptrdiff_t percent_position = offset;
22976 /* Get the specified minimum width. Zero means
22977 don't pad. */
22978 field = 0;
22979 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22980 field = field * 10 + c - '0';
22982 /* Don't pad beyond the total padding allowed. */
22983 if (field_width - n > 0 && field > field_width - n)
22984 field = field_width - n;
22986 /* Note that either PRECISION <= 0 or N < PRECISION. */
22987 prec = precision - n;
22989 if (c == 'M')
22990 n += display_mode_element (it, depth, field, prec,
22991 Vglobal_mode_string, props,
22992 risky);
22993 else if (c != 0)
22995 bool multibyte;
22996 ptrdiff_t bytepos, charpos;
22997 const char *spec;
22998 Lisp_Object string;
23000 bytepos = percent_position;
23001 charpos = (STRING_MULTIBYTE (elt)
23002 ? string_byte_to_char (elt, bytepos)
23003 : bytepos);
23004 spec = decode_mode_spec (it->w, c, field, &string);
23005 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23007 switch (mode_line_target)
23009 case MODE_LINE_NOPROP:
23010 case MODE_LINE_TITLE:
23011 n += store_mode_line_noprop (spec, field, prec);
23012 break;
23013 case MODE_LINE_STRING:
23015 Lisp_Object tem = build_string (spec);
23016 props = Ftext_properties_at (make_number (charpos), elt);
23017 /* Should only keep face property in props */
23018 n += store_mode_line_string (NULL, tem, false,
23019 field, prec, props);
23021 break;
23022 case MODE_LINE_DISPLAY:
23024 int nglyphs_before, nwritten;
23026 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23027 nwritten = display_string (spec, string, elt,
23028 charpos, 0, it,
23029 field, prec, 0,
23030 multibyte);
23032 /* Assign to the glyphs written above the
23033 string where the `%x' came from, position
23034 of the `%'. */
23035 if (nwritten > 0)
23037 struct glyph *glyph
23038 = (it->glyph_row->glyphs[TEXT_AREA]
23039 + nglyphs_before);
23040 int i;
23042 for (i = 0; i < nwritten; ++i)
23044 glyph[i].object = elt;
23045 glyph[i].charpos = charpos;
23048 n += nwritten;
23051 break;
23054 else /* c == 0 */
23055 break;
23059 break;
23061 case Lisp_Symbol:
23062 /* A symbol: process the value of the symbol recursively
23063 as if it appeared here directly. Avoid error if symbol void.
23064 Special case: if value of symbol is a string, output the string
23065 literally. */
23067 register Lisp_Object tem;
23069 /* If the variable is not marked as risky to set
23070 then its contents are risky to use. */
23071 if (NILP (Fget (elt, Qrisky_local_variable)))
23072 risky = true;
23074 tem = Fboundp (elt);
23075 if (!NILP (tem))
23077 tem = Fsymbol_value (elt);
23078 /* If value is a string, output that string literally:
23079 don't check for % within it. */
23080 if (STRINGP (tem))
23081 literal = true;
23083 if (!EQ (tem, elt))
23085 /* Give up right away for nil or t. */
23086 elt = tem;
23087 goto tail_recurse;
23091 break;
23093 case Lisp_Cons:
23095 register Lisp_Object car, tem;
23097 /* A cons cell: five distinct cases.
23098 If first element is :eval or :propertize, do something special.
23099 If first element is a string or a cons, process all the elements
23100 and effectively concatenate them.
23101 If first element is a negative number, truncate displaying cdr to
23102 at most that many characters. If positive, pad (with spaces)
23103 to at least that many characters.
23104 If first element is a symbol, process the cadr or caddr recursively
23105 according to whether the symbol's value is non-nil or nil. */
23106 car = XCAR (elt);
23107 if (EQ (car, QCeval))
23109 /* An element of the form (:eval FORM) means evaluate FORM
23110 and use the result as mode line elements. */
23112 if (risky)
23113 break;
23115 if (CONSP (XCDR (elt)))
23117 Lisp_Object spec;
23118 spec = safe__eval (true, XCAR (XCDR (elt)));
23119 n += display_mode_element (it, depth, field_width - n,
23120 precision - n, spec, props,
23121 risky);
23124 else if (EQ (car, QCpropertize))
23126 /* An element of the form (:propertize ELT PROPS...)
23127 means display ELT but applying properties PROPS. */
23129 if (risky)
23130 break;
23132 if (CONSP (XCDR (elt)))
23133 n += display_mode_element (it, depth, field_width - n,
23134 precision - n, XCAR (XCDR (elt)),
23135 XCDR (XCDR (elt)), risky);
23137 else if (SYMBOLP (car))
23139 tem = Fboundp (car);
23140 elt = XCDR (elt);
23141 if (!CONSP (elt))
23142 goto invalid;
23143 /* elt is now the cdr, and we know it is a cons cell.
23144 Use its car if CAR has a non-nil value. */
23145 if (!NILP (tem))
23147 tem = Fsymbol_value (car);
23148 if (!NILP (tem))
23150 elt = XCAR (elt);
23151 goto tail_recurse;
23154 /* Symbol's value is nil (or symbol is unbound)
23155 Get the cddr of the original list
23156 and if possible find the caddr and use that. */
23157 elt = XCDR (elt);
23158 if (NILP (elt))
23159 break;
23160 else if (!CONSP (elt))
23161 goto invalid;
23162 elt = XCAR (elt);
23163 goto tail_recurse;
23165 else if (INTEGERP (car))
23167 register int lim = XINT (car);
23168 elt = XCDR (elt);
23169 if (lim < 0)
23171 /* Negative int means reduce maximum width. */
23172 if (precision <= 0)
23173 precision = -lim;
23174 else
23175 precision = min (precision, -lim);
23177 else if (lim > 0)
23179 /* Padding specified. Don't let it be more than
23180 current maximum. */
23181 if (precision > 0)
23182 lim = min (precision, lim);
23184 /* If that's more padding than already wanted, queue it.
23185 But don't reduce padding already specified even if
23186 that is beyond the current truncation point. */
23187 field_width = max (lim, field_width);
23189 goto tail_recurse;
23191 else if (STRINGP (car) || CONSP (car))
23192 FOR_EACH_TAIL_SAFE (elt)
23194 if (0 < precision && precision <= n)
23195 break;
23196 n += display_mode_element (it, depth,
23197 /* Pad after only the last
23198 list element. */
23199 (! CONSP (XCDR (elt))
23200 ? field_width - n
23201 : 0),
23202 precision - n, XCAR (elt),
23203 props, risky);
23206 break;
23208 default:
23209 invalid:
23210 elt = build_string ("*invalid*");
23211 goto tail_recurse;
23214 /* Pad to FIELD_WIDTH. */
23215 if (field_width > 0 && n < field_width)
23217 switch (mode_line_target)
23219 case MODE_LINE_NOPROP:
23220 case MODE_LINE_TITLE:
23221 n += store_mode_line_noprop ("", field_width - n, 0);
23222 break;
23223 case MODE_LINE_STRING:
23224 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23225 Qnil);
23226 break;
23227 case MODE_LINE_DISPLAY:
23228 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23229 0, 0, 0);
23230 break;
23234 return n;
23237 /* Store a mode-line string element in mode_line_string_list.
23239 If STRING is non-null, display that C string. Otherwise, the Lisp
23240 string LISP_STRING is displayed.
23242 FIELD_WIDTH is the minimum number of output glyphs to produce.
23243 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23244 with spaces. FIELD_WIDTH <= 0 means don't pad.
23246 PRECISION is the maximum number of characters to output from
23247 STRING. PRECISION <= 0 means don't truncate the string.
23249 If COPY_STRING, make a copy of LISP_STRING before adding
23250 properties to the string.
23252 PROPS are the properties to add to the string.
23253 The mode_line_string_face face property is always added to the string.
23256 static int
23257 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23258 bool copy_string,
23259 int field_width, int precision, Lisp_Object props)
23261 ptrdiff_t len;
23262 int n = 0;
23264 if (string != NULL)
23266 len = strlen (string);
23267 if (precision > 0 && len > precision)
23268 len = precision;
23269 lisp_string = make_string (string, len);
23270 if (NILP (props))
23271 props = mode_line_string_face_prop;
23272 else if (!NILP (mode_line_string_face))
23274 Lisp_Object face = Fplist_get (props, Qface);
23275 props = Fcopy_sequence (props);
23276 if (NILP (face))
23277 face = mode_line_string_face;
23278 else
23279 face = list2 (face, mode_line_string_face);
23280 props = Fplist_put (props, Qface, face);
23282 Fadd_text_properties (make_number (0), make_number (len),
23283 props, lisp_string);
23285 else
23287 len = XFASTINT (Flength (lisp_string));
23288 if (precision > 0 && len > precision)
23290 len = precision;
23291 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23292 precision = -1;
23294 if (!NILP (mode_line_string_face))
23296 Lisp_Object face;
23297 if (NILP (props))
23298 props = Ftext_properties_at (make_number (0), lisp_string);
23299 face = Fplist_get (props, Qface);
23300 if (NILP (face))
23301 face = mode_line_string_face;
23302 else
23303 face = list2 (face, mode_line_string_face);
23304 props = list2 (Qface, face);
23305 if (copy_string)
23306 lisp_string = Fcopy_sequence (lisp_string);
23308 if (!NILP (props))
23309 Fadd_text_properties (make_number (0), make_number (len),
23310 props, lisp_string);
23313 if (len > 0)
23315 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23316 n += len;
23319 if (field_width > len)
23321 field_width -= len;
23322 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23323 if (!NILP (props))
23324 Fadd_text_properties (make_number (0), make_number (field_width),
23325 props, lisp_string);
23326 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23327 n += field_width;
23330 return n;
23334 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23335 1, 4, 0,
23336 doc: /* Format a string out of a mode line format specification.
23337 First arg FORMAT specifies the mode line format (see `mode-line-format'
23338 for details) to use.
23340 By default, the format is evaluated for the currently selected window.
23342 Optional second arg FACE specifies the face property to put on all
23343 characters for which no face is specified. The value nil means the
23344 default face. The value t means whatever face the window's mode line
23345 currently uses (either `mode-line' or `mode-line-inactive',
23346 depending on whether the window is the selected window or not).
23347 An integer value means the value string has no text
23348 properties.
23350 Optional third and fourth args WINDOW and BUFFER specify the window
23351 and buffer to use as the context for the formatting (defaults
23352 are the selected window and the WINDOW's buffer). */)
23353 (Lisp_Object format, Lisp_Object face,
23354 Lisp_Object window, Lisp_Object buffer)
23356 struct it it;
23357 int len;
23358 struct window *w;
23359 struct buffer *old_buffer = NULL;
23360 int face_id;
23361 bool no_props = INTEGERP (face);
23362 ptrdiff_t count = SPECPDL_INDEX ();
23363 Lisp_Object str;
23364 int string_start = 0;
23366 w = decode_any_window (window);
23367 XSETWINDOW (window, w);
23369 if (NILP (buffer))
23370 buffer = w->contents;
23371 CHECK_BUFFER (buffer);
23373 /* Make formatting the modeline a non-op when noninteractive, otherwise
23374 there will be problems later caused by a partially initialized frame. */
23375 if (NILP (format) || noninteractive)
23376 return empty_unibyte_string;
23378 if (no_props)
23379 face = Qnil;
23381 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23382 : EQ (face, Qt) ? (EQ (window, selected_window)
23383 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23384 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23385 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23386 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23387 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23388 : DEFAULT_FACE_ID;
23390 old_buffer = current_buffer;
23392 /* Save things including mode_line_proptrans_alist,
23393 and set that to nil so that we don't alter the outer value. */
23394 record_unwind_protect (unwind_format_mode_line,
23395 format_mode_line_unwind_data
23396 (XFRAME (WINDOW_FRAME (w)),
23397 old_buffer, selected_window, true));
23398 mode_line_proptrans_alist = Qnil;
23400 Fselect_window (window, Qt);
23401 set_buffer_internal_1 (XBUFFER (buffer));
23403 init_iterator (&it, w, -1, -1, NULL, face_id);
23405 if (no_props)
23407 mode_line_target = MODE_LINE_NOPROP;
23408 mode_line_string_face_prop = Qnil;
23409 mode_line_string_list = Qnil;
23410 string_start = MODE_LINE_NOPROP_LEN (0);
23412 else
23414 mode_line_target = MODE_LINE_STRING;
23415 mode_line_string_list = Qnil;
23416 mode_line_string_face = face;
23417 mode_line_string_face_prop
23418 = NILP (face) ? Qnil : list2 (Qface, face);
23421 push_kboard (FRAME_KBOARD (it.f));
23422 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23423 pop_kboard ();
23425 if (no_props)
23427 len = MODE_LINE_NOPROP_LEN (string_start);
23428 str = make_string (mode_line_noprop_buf + string_start, len);
23430 else
23432 mode_line_string_list = Fnreverse (mode_line_string_list);
23433 str = Fmapconcat (Qidentity, mode_line_string_list,
23434 empty_unibyte_string);
23437 unbind_to (count, Qnil);
23438 return str;
23441 /* Write a null-terminated, right justified decimal representation of
23442 the positive integer D to BUF using a minimal field width WIDTH. */
23444 static void
23445 pint2str (register char *buf, register int width, register ptrdiff_t d)
23447 register char *p = buf;
23449 if (d <= 0)
23450 *p++ = '0';
23451 else
23453 while (d > 0)
23455 *p++ = d % 10 + '0';
23456 d /= 10;
23460 for (width -= (int) (p - buf); width > 0; --width)
23461 *p++ = ' ';
23462 *p-- = '\0';
23463 while (p > buf)
23465 d = *buf;
23466 *buf++ = *p;
23467 *p-- = d;
23471 /* Write a null-terminated, right justified decimal and "human
23472 readable" representation of the nonnegative integer D to BUF using
23473 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23475 static const char power_letter[] =
23477 0, /* no letter */
23478 'k', /* kilo */
23479 'M', /* mega */
23480 'G', /* giga */
23481 'T', /* tera */
23482 'P', /* peta */
23483 'E', /* exa */
23484 'Z', /* zetta */
23485 'Y' /* yotta */
23488 static void
23489 pint2hrstr (char *buf, int width, ptrdiff_t d)
23491 /* We aim to represent the nonnegative integer D as
23492 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23493 ptrdiff_t quotient = d;
23494 int remainder = 0;
23495 /* -1 means: do not use TENTHS. */
23496 int tenths = -1;
23497 int exponent = 0;
23499 /* Length of QUOTIENT.TENTHS as a string. */
23500 int length;
23502 char * psuffix;
23503 char * p;
23505 if (quotient >= 1000)
23507 /* Scale to the appropriate EXPONENT. */
23510 remainder = quotient % 1000;
23511 quotient /= 1000;
23512 exponent++;
23514 while (quotient >= 1000);
23516 /* Round to nearest and decide whether to use TENTHS or not. */
23517 if (quotient <= 9)
23519 tenths = remainder / 100;
23520 if (remainder % 100 >= 50)
23522 if (tenths < 9)
23523 tenths++;
23524 else
23526 quotient++;
23527 if (quotient == 10)
23528 tenths = -1;
23529 else
23530 tenths = 0;
23534 else
23535 if (remainder >= 500)
23537 if (quotient < 999)
23538 quotient++;
23539 else
23541 quotient = 1;
23542 exponent++;
23543 tenths = 0;
23548 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23549 if (tenths == -1 && quotient <= 99)
23550 if (quotient <= 9)
23551 length = 1;
23552 else
23553 length = 2;
23554 else
23555 length = 3;
23556 p = psuffix = buf + max (width, length);
23558 /* Print EXPONENT. */
23559 *psuffix++ = power_letter[exponent];
23560 *psuffix = '\0';
23562 /* Print TENTHS. */
23563 if (tenths >= 0)
23565 *--p = '0' + tenths;
23566 *--p = '.';
23569 /* Print QUOTIENT. */
23572 int digit = quotient % 10;
23573 *--p = '0' + digit;
23575 while ((quotient /= 10) != 0);
23577 /* Print leading spaces. */
23578 while (buf < p)
23579 *--p = ' ';
23582 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23583 If EOL_FLAG, set also a mnemonic character for end-of-line
23584 type of CODING_SYSTEM. Return updated pointer into BUF. */
23586 static unsigned char invalid_eol_type[] = "(*invalid*)";
23588 static char *
23589 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23591 Lisp_Object val;
23592 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23593 const unsigned char *eol_str;
23594 int eol_str_len;
23595 /* The EOL conversion we are using. */
23596 Lisp_Object eoltype;
23598 val = CODING_SYSTEM_SPEC (coding_system);
23599 eoltype = Qnil;
23601 if (!VECTORP (val)) /* Not yet decided. */
23603 *buf++ = multibyte ? '-' : ' ';
23604 if (eol_flag)
23605 eoltype = eol_mnemonic_undecided;
23606 /* Don't mention EOL conversion if it isn't decided. */
23608 else
23610 Lisp_Object attrs;
23611 Lisp_Object eolvalue;
23613 attrs = AREF (val, 0);
23614 eolvalue = AREF (val, 2);
23616 *buf++ = multibyte
23617 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23618 : ' ';
23620 if (eol_flag)
23622 /* The EOL conversion that is normal on this system. */
23624 if (NILP (eolvalue)) /* Not yet decided. */
23625 eoltype = eol_mnemonic_undecided;
23626 else if (VECTORP (eolvalue)) /* Not yet decided. */
23627 eoltype = eol_mnemonic_undecided;
23628 else /* eolvalue is Qunix, Qdos, or Qmac. */
23629 eoltype = (EQ (eolvalue, Qunix)
23630 ? eol_mnemonic_unix
23631 : EQ (eolvalue, Qdos)
23632 ? eol_mnemonic_dos : eol_mnemonic_mac);
23636 if (eol_flag)
23638 /* Mention the EOL conversion if it is not the usual one. */
23639 if (STRINGP (eoltype))
23641 eol_str = SDATA (eoltype);
23642 eol_str_len = SBYTES (eoltype);
23644 else if (CHARACTERP (eoltype))
23646 int c = XFASTINT (eoltype);
23647 return buf + CHAR_STRING (c, (unsigned char *) buf);
23649 else
23651 eol_str = invalid_eol_type;
23652 eol_str_len = sizeof (invalid_eol_type) - 1;
23654 memcpy (buf, eol_str, eol_str_len);
23655 buf += eol_str_len;
23658 return buf;
23661 /* Return the approximate percentage N is of D (rounding upward), or 99,
23662 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23664 static int
23665 percent99 (ptrdiff_t n, ptrdiff_t d)
23667 int percent = (d - 1 + 100.0 * n) / d;
23668 return min (percent, 99);
23671 /* Return a string for the output of a mode line %-spec for window W,
23672 generated by character C. FIELD_WIDTH > 0 means pad the string
23673 returned with spaces to that value. Return a Lisp string in
23674 *STRING if the resulting string is taken from that Lisp string.
23676 Note we operate on the current buffer for most purposes. */
23678 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23680 static const char *
23681 decode_mode_spec (struct window *w, register int c, int field_width,
23682 Lisp_Object *string)
23684 Lisp_Object obj;
23685 struct frame *f = XFRAME (WINDOW_FRAME (w));
23686 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23687 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23688 produce strings from numerical values, so limit preposterously
23689 large values of FIELD_WIDTH to avoid overrunning the buffer's
23690 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23691 bytes plus the terminating null. */
23692 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23693 struct buffer *b = current_buffer;
23695 obj = Qnil;
23696 *string = Qnil;
23698 switch (c)
23700 case '*':
23701 if (!NILP (BVAR (b, read_only)))
23702 return "%";
23703 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23704 return "*";
23705 return "-";
23707 case '+':
23708 /* This differs from %* only for a modified read-only buffer. */
23709 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23710 return "*";
23711 if (!NILP (BVAR (b, read_only)))
23712 return "%";
23713 return "-";
23715 case '&':
23716 /* This differs from %* in ignoring read-only-ness. */
23717 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23718 return "*";
23719 return "-";
23721 case '%':
23722 return "%";
23724 case '[':
23726 int i;
23727 char *p;
23729 if (command_loop_level > 5)
23730 return "[[[... ";
23731 p = decode_mode_spec_buf;
23732 for (i = 0; i < command_loop_level; i++)
23733 *p++ = '[';
23734 *p = 0;
23735 return decode_mode_spec_buf;
23738 case ']':
23740 int i;
23741 char *p;
23743 if (command_loop_level > 5)
23744 return " ...]]]";
23745 p = decode_mode_spec_buf;
23746 for (i = 0; i < command_loop_level; i++)
23747 *p++ = ']';
23748 *p = 0;
23749 return decode_mode_spec_buf;
23752 case '-':
23754 register int i;
23756 /* Let lots_of_dashes be a string of infinite length. */
23757 if (mode_line_target == MODE_LINE_NOPROP
23758 || mode_line_target == MODE_LINE_STRING)
23759 return "--";
23760 if (field_width <= 0
23761 || field_width > sizeof (lots_of_dashes))
23763 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23764 decode_mode_spec_buf[i] = '-';
23765 decode_mode_spec_buf[i] = '\0';
23766 return decode_mode_spec_buf;
23768 else
23769 return lots_of_dashes;
23772 case 'b':
23773 obj = BVAR (b, name);
23774 break;
23776 case 'c':
23777 case 'C':
23778 /* %c, %C, and %l are ignored in `frame-title-format'.
23779 (In redisplay_internal, the frame title is drawn _before_ the
23780 windows are updated, so the stuff which depends on actual
23781 window contents (such as %l) may fail to render properly, or
23782 even crash emacs.) */
23783 if (mode_line_target == MODE_LINE_TITLE)
23784 return "";
23785 else
23787 ptrdiff_t col = current_column ();
23788 int disp_col = (c == 'C') ? col + 1 : col;
23789 w->column_number_displayed = col;
23790 pint2str (decode_mode_spec_buf, width, disp_col);
23791 return decode_mode_spec_buf;
23794 case 'e':
23795 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23797 if (NILP (Vmemory_full))
23798 return "";
23799 else
23800 return "!MEM FULL! ";
23802 #else
23803 return "";
23804 #endif
23806 case 'F':
23807 /* %F displays the frame name. */
23808 if (!NILP (f->title))
23809 return SSDATA (f->title);
23810 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23811 return SSDATA (f->name);
23812 return "Emacs";
23814 case 'f':
23815 obj = BVAR (b, filename);
23816 break;
23818 case 'i':
23820 ptrdiff_t size = ZV - BEGV;
23821 pint2str (decode_mode_spec_buf, width, size);
23822 return decode_mode_spec_buf;
23825 case 'I':
23827 ptrdiff_t size = ZV - BEGV;
23828 pint2hrstr (decode_mode_spec_buf, width, size);
23829 return decode_mode_spec_buf;
23832 case 'l':
23834 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23835 ptrdiff_t topline, nlines, height;
23836 ptrdiff_t junk;
23838 /* %c, %C, and %l are ignored in `frame-title-format'. */
23839 if (mode_line_target == MODE_LINE_TITLE)
23840 return "";
23842 startpos = marker_position (w->start);
23843 startpos_byte = marker_byte_position (w->start);
23844 height = WINDOW_TOTAL_LINES (w);
23846 /* If we decided that this buffer isn't suitable for line numbers,
23847 don't forget that too fast. */
23848 if (w->base_line_pos == -1)
23849 goto no_value;
23851 /* If the buffer is very big, don't waste time. */
23852 if (INTEGERP (Vline_number_display_limit)
23853 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23855 w->base_line_pos = 0;
23856 w->base_line_number = 0;
23857 goto no_value;
23860 if (w->base_line_number > 0
23861 && w->base_line_pos > 0
23862 && w->base_line_pos <= startpos)
23864 line = w->base_line_number;
23865 linepos = w->base_line_pos;
23866 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23868 else
23870 line = 1;
23871 linepos = BUF_BEGV (b);
23872 linepos_byte = BUF_BEGV_BYTE (b);
23875 /* Count lines from base line to window start position. */
23876 nlines = display_count_lines (linepos_byte,
23877 startpos_byte,
23878 startpos, &junk);
23880 topline = nlines + line;
23882 /* Determine a new base line, if the old one is too close
23883 or too far away, or if we did not have one.
23884 "Too close" means it's plausible a scroll-down would
23885 go back past it. */
23886 if (startpos == BUF_BEGV (b))
23888 w->base_line_number = topline;
23889 w->base_line_pos = BUF_BEGV (b);
23891 else if (nlines < height + 25 || nlines > height * 3 + 50
23892 || linepos == BUF_BEGV (b))
23894 ptrdiff_t limit = BUF_BEGV (b);
23895 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23896 ptrdiff_t position;
23897 ptrdiff_t distance =
23898 (height * 2 + 30) * line_number_display_limit_width;
23900 if (startpos - distance > limit)
23902 limit = startpos - distance;
23903 limit_byte = CHAR_TO_BYTE (limit);
23906 nlines = display_count_lines (startpos_byte,
23907 limit_byte,
23908 - (height * 2 + 30),
23909 &position);
23910 /* If we couldn't find the lines we wanted within
23911 line_number_display_limit_width chars per line,
23912 give up on line numbers for this window. */
23913 if (position == limit_byte && limit == startpos - distance)
23915 w->base_line_pos = -1;
23916 w->base_line_number = 0;
23917 goto no_value;
23920 w->base_line_number = topline - nlines;
23921 w->base_line_pos = BYTE_TO_CHAR (position);
23924 /* Now count lines from the start pos to point. */
23925 nlines = display_count_lines (startpos_byte,
23926 PT_BYTE, PT, &junk);
23928 /* Record that we did display the line number. */
23929 line_number_displayed = true;
23931 /* Make the string to show. */
23932 pint2str (decode_mode_spec_buf, width, topline + nlines);
23933 return decode_mode_spec_buf;
23934 no_value:
23936 char *p = decode_mode_spec_buf;
23937 int pad = width - 2;
23938 while (pad-- > 0)
23939 *p++ = ' ';
23940 *p++ = '?';
23941 *p++ = '?';
23942 *p = '\0';
23943 return decode_mode_spec_buf;
23946 break;
23948 case 'm':
23949 obj = BVAR (b, mode_name);
23950 break;
23952 case 'n':
23953 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23954 return " Narrow";
23955 break;
23957 /* Display the "degree of travel" of the window through the buffer. */
23958 case 'o':
23960 ptrdiff_t toppos = marker_position (w->start);
23961 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23962 ptrdiff_t begv = BUF_BEGV (b);
23963 ptrdiff_t zv = BUF_ZV (b);
23965 if (zv <= botpos)
23966 return toppos <= begv ? "All" : "Bottom";
23967 else if (toppos <= begv)
23968 return "Top";
23969 else
23971 sprintf (decode_mode_spec_buf, "%2d%%",
23972 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
23973 return decode_mode_spec_buf;
23977 /* Display percentage of buffer above the top of the screen. */
23978 case 'p':
23980 ptrdiff_t pos = marker_position (w->start);
23981 ptrdiff_t begv = BUF_BEGV (b);
23982 ptrdiff_t zv = BUF_ZV (b);
23984 if (w->window_end_pos <= BUF_Z (b) - zv)
23985 return pos <= begv ? "All" : "Bottom";
23986 else if (pos <= begv)
23987 return "Top";
23988 else
23990 sprintf (decode_mode_spec_buf, "%2d%%",
23991 percent99 (pos - begv, zv - begv));
23992 return decode_mode_spec_buf;
23996 /* Display percentage of size above the bottom of the screen. */
23997 case 'P':
23999 ptrdiff_t toppos = marker_position (w->start);
24000 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24001 ptrdiff_t begv = BUF_BEGV (b);
24002 ptrdiff_t zv = BUF_ZV (b);
24004 if (zv <= botpos)
24005 return toppos <= begv ? "All" : "Bottom";
24006 else
24008 sprintf (decode_mode_spec_buf,
24009 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24010 percent99 (botpos - begv, zv - begv));
24011 return decode_mode_spec_buf;
24015 /* Display percentage offsets of top and bottom of the window,
24016 using "All" (but not "Top" or "Bottom") where appropriate. */
24017 case 'q':
24019 ptrdiff_t toppos = marker_position (w->start);
24020 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24021 ptrdiff_t begv = BUF_BEGV (b);
24022 ptrdiff_t zv = BUF_ZV (b);
24023 int top_perc, bot_perc;
24025 if ((toppos <= begv) && (zv <= botpos))
24026 return "All ";
24028 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24029 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24031 if (top_perc == bot_perc)
24032 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24033 else
24034 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24036 return decode_mode_spec_buf;
24039 case 's':
24040 /* status of process */
24041 obj = Fget_buffer_process (Fcurrent_buffer ());
24042 if (NILP (obj))
24043 return "no process";
24044 #ifndef MSDOS
24045 obj = Fsymbol_name (Fprocess_status (obj));
24046 #endif
24047 break;
24049 case '@':
24051 ptrdiff_t count = inhibit_garbage_collection ();
24052 Lisp_Object curdir = BVAR (current_buffer, directory);
24053 Lisp_Object val = Qnil;
24055 if (STRINGP (curdir))
24056 val = call1 (intern ("file-remote-p"), curdir);
24058 unbind_to (count, Qnil);
24060 if (NILP (val))
24061 return "-";
24062 else
24063 return "@";
24066 case 'z':
24067 /* coding-system (not including end-of-line format) */
24068 case 'Z':
24069 /* coding-system (including end-of-line type) */
24071 bool eol_flag = (c == 'Z');
24072 char *p = decode_mode_spec_buf;
24074 if (! FRAME_WINDOW_P (f))
24076 /* No need to mention EOL here--the terminal never needs
24077 to do EOL conversion. */
24078 p = decode_mode_spec_coding (CODING_ID_NAME
24079 (FRAME_KEYBOARD_CODING (f)->id),
24080 p, false);
24081 p = decode_mode_spec_coding (CODING_ID_NAME
24082 (FRAME_TERMINAL_CODING (f)->id),
24083 p, false);
24085 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24086 p, eol_flag);
24088 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24089 #ifdef subprocesses
24090 obj = Fget_buffer_process (Fcurrent_buffer ());
24091 if (PROCESSP (obj))
24093 p = decode_mode_spec_coding
24094 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24095 p = decode_mode_spec_coding
24096 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24098 #endif /* subprocesses */
24099 #endif /* false */
24100 *p = 0;
24101 return decode_mode_spec_buf;
24105 if (STRINGP (obj))
24107 *string = obj;
24108 return SSDATA (obj);
24110 else
24111 return "";
24115 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24116 means count lines back from START_BYTE. But don't go beyond
24117 LIMIT_BYTE. Return the number of lines thus found (always
24118 nonnegative).
24120 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24121 either the position COUNT lines after/before START_BYTE, if we
24122 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24123 COUNT lines. */
24125 static ptrdiff_t
24126 display_count_lines (ptrdiff_t start_byte,
24127 ptrdiff_t limit_byte, ptrdiff_t count,
24128 ptrdiff_t *byte_pos_ptr)
24130 register unsigned char *cursor;
24131 unsigned char *base;
24133 register ptrdiff_t ceiling;
24134 register unsigned char *ceiling_addr;
24135 ptrdiff_t orig_count = count;
24137 /* If we are not in selective display mode,
24138 check only for newlines. */
24139 bool selective_display
24140 = (!NILP (BVAR (current_buffer, selective_display))
24141 && !INTEGERP (BVAR (current_buffer, selective_display)));
24143 if (count > 0)
24145 while (start_byte < limit_byte)
24147 ceiling = BUFFER_CEILING_OF (start_byte);
24148 ceiling = min (limit_byte - 1, ceiling);
24149 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24150 base = (cursor = BYTE_POS_ADDR (start_byte));
24154 if (selective_display)
24156 while (*cursor != '\n' && *cursor != 015
24157 && ++cursor != ceiling_addr)
24158 continue;
24159 if (cursor == ceiling_addr)
24160 break;
24162 else
24164 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24165 if (! cursor)
24166 break;
24169 cursor++;
24171 if (--count == 0)
24173 start_byte += cursor - base;
24174 *byte_pos_ptr = start_byte;
24175 return orig_count;
24178 while (cursor < ceiling_addr);
24180 start_byte += ceiling_addr - base;
24183 else
24185 while (start_byte > limit_byte)
24187 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24188 ceiling = max (limit_byte, ceiling);
24189 ceiling_addr = BYTE_POS_ADDR (ceiling);
24190 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24191 while (true)
24193 if (selective_display)
24195 while (--cursor >= ceiling_addr
24196 && *cursor != '\n' && *cursor != 015)
24197 continue;
24198 if (cursor < ceiling_addr)
24199 break;
24201 else
24203 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24204 if (! cursor)
24205 break;
24208 if (++count == 0)
24210 start_byte += cursor - base + 1;
24211 *byte_pos_ptr = start_byte;
24212 /* When scanning backwards, we should
24213 not count the newline posterior to which we stop. */
24214 return - orig_count - 1;
24217 start_byte += ceiling_addr - base;
24221 *byte_pos_ptr = limit_byte;
24223 if (count < 0)
24224 return - orig_count + count;
24225 return orig_count - count;
24231 /***********************************************************************
24232 Displaying strings
24233 ***********************************************************************/
24235 /* Display a NUL-terminated string, starting with index START.
24237 If STRING is non-null, display that C string. Otherwise, the Lisp
24238 string LISP_STRING is displayed. There's a case that STRING is
24239 non-null and LISP_STRING is not nil. It means STRING is a string
24240 data of LISP_STRING. In that case, we display LISP_STRING while
24241 ignoring its text properties.
24243 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24244 FACE_STRING. Display STRING or LISP_STRING with the face at
24245 FACE_STRING_POS in FACE_STRING:
24247 Display the string in the environment given by IT, but use the
24248 standard display table, temporarily.
24250 FIELD_WIDTH is the minimum number of output glyphs to produce.
24251 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24252 with spaces. If STRING has more characters, more than FIELD_WIDTH
24253 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24255 PRECISION is the maximum number of characters to output from
24256 STRING. PRECISION < 0 means don't truncate the string.
24258 This is roughly equivalent to printf format specifiers:
24260 FIELD_WIDTH PRECISION PRINTF
24261 ----------------------------------------
24262 -1 -1 %s
24263 -1 10 %.10s
24264 10 -1 %10s
24265 20 10 %20.10s
24267 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24268 display them, and < 0 means obey the current buffer's value of
24269 enable_multibyte_characters.
24271 Value is the number of columns displayed. */
24273 static int
24274 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24275 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24276 int field_width, int precision, int max_x, int multibyte)
24278 int hpos_at_start = it->hpos;
24279 int saved_face_id = it->face_id;
24280 struct glyph_row *row = it->glyph_row;
24281 ptrdiff_t it_charpos;
24283 /* Initialize the iterator IT for iteration over STRING beginning
24284 with index START. */
24285 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24286 precision, field_width, multibyte);
24287 if (string && STRINGP (lisp_string))
24288 /* LISP_STRING is the one returned by decode_mode_spec. We should
24289 ignore its text properties. */
24290 it->stop_charpos = it->end_charpos;
24292 /* If displaying STRING, set up the face of the iterator from
24293 FACE_STRING, if that's given. */
24294 if (STRINGP (face_string))
24296 ptrdiff_t endptr;
24297 struct face *face;
24299 it->face_id
24300 = face_at_string_position (it->w, face_string, face_string_pos,
24301 0, &endptr, it->base_face_id, false);
24302 face = FACE_FROM_ID (it->f, it->face_id);
24303 it->face_box_p = face->box != FACE_NO_BOX;
24306 /* Set max_x to the maximum allowed X position. Don't let it go
24307 beyond the right edge of the window. */
24308 if (max_x <= 0)
24309 max_x = it->last_visible_x;
24310 else
24311 max_x = min (max_x, it->last_visible_x);
24313 /* Skip over display elements that are not visible. because IT->w is
24314 hscrolled. */
24315 if (it->current_x < it->first_visible_x)
24316 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24317 MOVE_TO_POS | MOVE_TO_X);
24319 row->ascent = it->max_ascent;
24320 row->height = it->max_ascent + it->max_descent;
24321 row->phys_ascent = it->max_phys_ascent;
24322 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24323 row->extra_line_spacing = it->max_extra_line_spacing;
24325 if (STRINGP (it->string))
24326 it_charpos = IT_STRING_CHARPOS (*it);
24327 else
24328 it_charpos = IT_CHARPOS (*it);
24330 /* This condition is for the case that we are called with current_x
24331 past last_visible_x. */
24332 while (it->current_x < max_x)
24334 int x_before, x, n_glyphs_before, i, nglyphs;
24336 /* Get the next display element. */
24337 if (!get_next_display_element (it))
24338 break;
24340 /* Produce glyphs. */
24341 x_before = it->current_x;
24342 n_glyphs_before = row->used[TEXT_AREA];
24343 PRODUCE_GLYPHS (it);
24345 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24346 i = 0;
24347 x = x_before;
24348 while (i < nglyphs)
24350 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24352 if (it->line_wrap != TRUNCATE
24353 && x + glyph->pixel_width > max_x)
24355 /* End of continued line or max_x reached. */
24356 if (CHAR_GLYPH_PADDING_P (*glyph))
24358 /* A wide character is unbreakable. */
24359 if (row->reversed_p)
24360 unproduce_glyphs (it, row->used[TEXT_AREA]
24361 - n_glyphs_before);
24362 row->used[TEXT_AREA] = n_glyphs_before;
24363 it->current_x = x_before;
24365 else
24367 if (row->reversed_p)
24368 unproduce_glyphs (it, row->used[TEXT_AREA]
24369 - (n_glyphs_before + i));
24370 row->used[TEXT_AREA] = n_glyphs_before + i;
24371 it->current_x = x;
24373 break;
24375 else if (x + glyph->pixel_width >= it->first_visible_x)
24377 /* Glyph is at least partially visible. */
24378 ++it->hpos;
24379 if (x < it->first_visible_x)
24380 row->x = x - it->first_visible_x;
24382 else
24384 /* Glyph is off the left margin of the display area.
24385 Should not happen. */
24386 emacs_abort ();
24389 row->ascent = max (row->ascent, it->max_ascent);
24390 row->height = max (row->height, it->max_ascent + it->max_descent);
24391 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24392 row->phys_height = max (row->phys_height,
24393 it->max_phys_ascent + it->max_phys_descent);
24394 row->extra_line_spacing = max (row->extra_line_spacing,
24395 it->max_extra_line_spacing);
24396 x += glyph->pixel_width;
24397 ++i;
24400 /* Stop if max_x reached. */
24401 if (i < nglyphs)
24402 break;
24404 /* Stop at line ends. */
24405 if (ITERATOR_AT_END_OF_LINE_P (it))
24407 it->continuation_lines_width = 0;
24408 break;
24411 set_iterator_to_next (it, true);
24412 if (STRINGP (it->string))
24413 it_charpos = IT_STRING_CHARPOS (*it);
24414 else
24415 it_charpos = IT_CHARPOS (*it);
24417 /* Stop if truncating at the right edge. */
24418 if (it->line_wrap == TRUNCATE
24419 && it->current_x >= it->last_visible_x)
24421 /* Add truncation mark, but don't do it if the line is
24422 truncated at a padding space. */
24423 if (it_charpos < it->string_nchars)
24425 if (!FRAME_WINDOW_P (it->f))
24427 int ii, n;
24429 if (it->current_x > it->last_visible_x)
24431 if (!row->reversed_p)
24433 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24434 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24435 break;
24437 else
24439 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24440 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24441 break;
24442 unproduce_glyphs (it, ii + 1);
24443 ii = row->used[TEXT_AREA] - (ii + 1);
24445 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24447 row->used[TEXT_AREA] = ii;
24448 produce_special_glyphs (it, IT_TRUNCATION);
24451 produce_special_glyphs (it, IT_TRUNCATION);
24453 row->truncated_on_right_p = true;
24455 break;
24459 /* Maybe insert a truncation at the left. */
24460 if (it->first_visible_x
24461 && it_charpos > 0)
24463 if (!FRAME_WINDOW_P (it->f)
24464 || (row->reversed_p
24465 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24466 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24467 insert_left_trunc_glyphs (it);
24468 row->truncated_on_left_p = true;
24471 it->face_id = saved_face_id;
24473 /* Value is number of columns displayed. */
24474 return it->hpos - hpos_at_start;
24479 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24480 appears as an element of LIST or as the car of an element of LIST.
24481 If PROPVAL is a list, compare each element against LIST in that
24482 way, and return 1/2 if any element of PROPVAL is found in LIST.
24483 Otherwise return 0. This function cannot quit.
24484 The return value is 2 if the text is invisible but with an ellipsis
24485 and 1 if it's invisible and without an ellipsis. */
24488 invisible_prop (Lisp_Object propval, Lisp_Object list)
24490 Lisp_Object tail, proptail;
24492 for (tail = list; CONSP (tail); tail = XCDR (tail))
24494 register Lisp_Object tem;
24495 tem = XCAR (tail);
24496 if (EQ (propval, tem))
24497 return 1;
24498 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24499 return NILP (XCDR (tem)) ? 1 : 2;
24502 if (CONSP (propval))
24504 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24506 Lisp_Object propelt;
24507 propelt = XCAR (proptail);
24508 for (tail = list; CONSP (tail); tail = XCDR (tail))
24510 register Lisp_Object tem;
24511 tem = XCAR (tail);
24512 if (EQ (propelt, tem))
24513 return 1;
24514 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24515 return NILP (XCDR (tem)) ? 1 : 2;
24520 return 0;
24523 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24524 doc: /* Non-nil if the property makes the text invisible.
24525 POS-OR-PROP can be a marker or number, in which case it is taken to be
24526 a position in the current buffer and the value of the `invisible' property
24527 is checked; or it can be some other value, which is then presumed to be the
24528 value of the `invisible' property of the text of interest.
24529 The non-nil value returned can be t for truly invisible text or something
24530 else if the text is replaced by an ellipsis. */)
24531 (Lisp_Object pos_or_prop)
24533 Lisp_Object prop
24534 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24535 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24536 : pos_or_prop);
24537 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24538 return (invis == 0 ? Qnil
24539 : invis == 1 ? Qt
24540 : make_number (invis));
24543 /* Calculate a width or height in pixels from a specification using
24544 the following elements:
24546 SPEC ::=
24547 NUM - a (fractional) multiple of the default font width/height
24548 (NUM) - specifies exactly NUM pixels
24549 UNIT - a fixed number of pixels, see below.
24550 ELEMENT - size of a display element in pixels, see below.
24551 (NUM . SPEC) - equals NUM * SPEC
24552 (+ SPEC SPEC ...) - add pixel values
24553 (- SPEC SPEC ...) - subtract pixel values
24554 (- SPEC) - negate pixel value
24556 NUM ::=
24557 INT or FLOAT - a number constant
24558 SYMBOL - use symbol's (buffer local) variable binding.
24560 UNIT ::=
24561 in - pixels per inch *)
24562 mm - pixels per 1/1000 meter *)
24563 cm - pixels per 1/100 meter *)
24564 width - width of current font in pixels.
24565 height - height of current font in pixels.
24567 *) using the ratio(s) defined in display-pixels-per-inch.
24569 ELEMENT ::=
24571 left-fringe - left fringe width in pixels
24572 right-fringe - right fringe width in pixels
24574 left-margin - left margin width in pixels
24575 right-margin - right margin width in pixels
24577 scroll-bar - scroll-bar area width in pixels
24579 Examples:
24581 Pixels corresponding to 5 inches:
24582 (5 . in)
24584 Total width of non-text areas on left side of window (if scroll-bar is on left):
24585 '(space :width (+ left-fringe left-margin scroll-bar))
24587 Align to first text column (in header line):
24588 '(space :align-to 0)
24590 Align to middle of text area minus half the width of variable `my-image'
24591 containing a loaded image:
24592 '(space :align-to (0.5 . (- text my-image)))
24594 Width of left margin minus width of 1 character in the default font:
24595 '(space :width (- left-margin 1))
24597 Width of left margin minus width of 2 characters in the current font:
24598 '(space :width (- left-margin (2 . width)))
24600 Center 1 character over left-margin (in header line):
24601 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24603 Different ways to express width of left fringe plus left margin minus one pixel:
24604 '(space :width (- (+ left-fringe left-margin) (1)))
24605 '(space :width (+ left-fringe left-margin (- (1))))
24606 '(space :width (+ left-fringe left-margin (-1)))
24610 static bool
24611 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24612 struct font *font, bool width_p, int *align_to)
24614 double pixels;
24616 # define OK_PIXELS(val) (*res = (val), true)
24617 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24619 if (NILP (prop))
24620 return OK_PIXELS (0);
24622 eassert (FRAME_LIVE_P (it->f));
24624 if (SYMBOLP (prop))
24626 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24628 char *unit = SSDATA (SYMBOL_NAME (prop));
24630 if (unit[0] == 'i' && unit[1] == 'n')
24631 pixels = 1.0;
24632 else if (unit[0] == 'm' && unit[1] == 'm')
24633 pixels = 25.4;
24634 else if (unit[0] == 'c' && unit[1] == 'm')
24635 pixels = 2.54;
24636 else
24637 pixels = 0;
24638 if (pixels > 0)
24640 double ppi = (width_p ? FRAME_RES_X (it->f)
24641 : FRAME_RES_Y (it->f));
24643 if (ppi > 0)
24644 return OK_PIXELS (ppi / pixels);
24645 return false;
24649 #ifdef HAVE_WINDOW_SYSTEM
24650 if (EQ (prop, Qheight))
24651 return OK_PIXELS (font
24652 ? normal_char_height (font, -1)
24653 : FRAME_LINE_HEIGHT (it->f));
24654 if (EQ (prop, Qwidth))
24655 return OK_PIXELS (font
24656 ? FONT_WIDTH (font)
24657 : FRAME_COLUMN_WIDTH (it->f));
24658 #else
24659 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24660 return OK_PIXELS (1);
24661 #endif
24663 if (EQ (prop, Qtext))
24664 return OK_PIXELS (width_p
24665 ? window_box_width (it->w, TEXT_AREA)
24666 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24668 if (align_to && *align_to < 0)
24670 *res = 0;
24671 if (EQ (prop, Qleft))
24672 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24673 if (EQ (prop, Qright))
24674 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24675 if (EQ (prop, Qcenter))
24676 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24677 + window_box_width (it->w, TEXT_AREA) / 2);
24678 if (EQ (prop, Qleft_fringe))
24679 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24680 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24681 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24682 if (EQ (prop, Qright_fringe))
24683 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24684 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24685 : window_box_right_offset (it->w, TEXT_AREA));
24686 if (EQ (prop, Qleft_margin))
24687 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24688 if (EQ (prop, Qright_margin))
24689 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24690 if (EQ (prop, Qscroll_bar))
24691 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24693 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24694 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24695 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24696 : 0)));
24698 else
24700 if (EQ (prop, Qleft_fringe))
24701 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24702 if (EQ (prop, Qright_fringe))
24703 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24704 if (EQ (prop, Qleft_margin))
24705 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24706 if (EQ (prop, Qright_margin))
24707 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24708 if (EQ (prop, Qscroll_bar))
24709 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24712 prop = buffer_local_value (prop, it->w->contents);
24713 if (EQ (prop, Qunbound))
24714 prop = Qnil;
24717 if (NUMBERP (prop))
24719 int base_unit = (width_p
24720 ? FRAME_COLUMN_WIDTH (it->f)
24721 : FRAME_LINE_HEIGHT (it->f));
24722 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24725 if (CONSP (prop))
24727 Lisp_Object car = XCAR (prop);
24728 Lisp_Object cdr = XCDR (prop);
24730 if (SYMBOLP (car))
24732 #ifdef HAVE_WINDOW_SYSTEM
24733 if (FRAME_WINDOW_P (it->f)
24734 && valid_image_p (prop))
24736 ptrdiff_t id = lookup_image (it->f, prop);
24737 struct image *img = IMAGE_FROM_ID (it->f, id);
24739 return OK_PIXELS (width_p ? img->width : img->height);
24741 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24743 /* TODO: Don't return dummy size. */
24744 return OK_PIXELS (100);
24746 #endif
24747 if (EQ (car, Qplus) || EQ (car, Qminus))
24749 bool first = true;
24750 double px;
24752 pixels = 0;
24753 while (CONSP (cdr))
24755 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24756 font, width_p, align_to))
24757 return false;
24758 if (first)
24759 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24760 else
24761 pixels += px;
24762 cdr = XCDR (cdr);
24764 if (EQ (car, Qminus))
24765 pixels = -pixels;
24766 return OK_PIXELS (pixels);
24769 car = buffer_local_value (car, it->w->contents);
24770 if (EQ (car, Qunbound))
24771 car = Qnil;
24774 if (NUMBERP (car))
24776 double fact;
24777 pixels = XFLOATINT (car);
24778 if (NILP (cdr))
24779 return OK_PIXELS (pixels);
24780 if (calc_pixel_width_or_height (&fact, it, cdr,
24781 font, width_p, align_to))
24782 return OK_PIXELS (pixels * fact);
24783 return false;
24786 return false;
24789 return false;
24792 void
24793 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24795 #ifdef HAVE_WINDOW_SYSTEM
24796 normal_char_ascent_descent (font, -1, ascent, descent);
24797 #else
24798 *ascent = 1;
24799 *descent = 0;
24800 #endif
24804 /***********************************************************************
24805 Glyph Display
24806 ***********************************************************************/
24808 #ifdef HAVE_WINDOW_SYSTEM
24810 #ifdef GLYPH_DEBUG
24812 void
24813 dump_glyph_string (struct glyph_string *s)
24815 fprintf (stderr, "glyph string\n");
24816 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24817 s->x, s->y, s->width, s->height);
24818 fprintf (stderr, " ybase = %d\n", s->ybase);
24819 fprintf (stderr, " hl = %u\n", s->hl);
24820 fprintf (stderr, " left overhang = %d, right = %d\n",
24821 s->left_overhang, s->right_overhang);
24822 fprintf (stderr, " nchars = %d\n", s->nchars);
24823 fprintf (stderr, " extends to end of line = %d\n",
24824 s->extends_to_end_of_line_p);
24825 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24826 fprintf (stderr, " bg width = %d\n", s->background_width);
24829 #endif /* GLYPH_DEBUG */
24831 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24832 of XChar2b structures for S; it can't be allocated in
24833 init_glyph_string because it must be allocated via `alloca'. W
24834 is the window on which S is drawn. ROW and AREA are the glyph row
24835 and area within the row from which S is constructed. START is the
24836 index of the first glyph structure covered by S. HL is a
24837 face-override for drawing S. */
24839 #ifdef HAVE_NTGUI
24840 #define OPTIONAL_HDC(hdc) HDC hdc,
24841 #define DECLARE_HDC(hdc) HDC hdc;
24842 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24843 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24844 #endif
24846 #ifndef OPTIONAL_HDC
24847 #define OPTIONAL_HDC(hdc)
24848 #define DECLARE_HDC(hdc)
24849 #define ALLOCATE_HDC(hdc, f)
24850 #define RELEASE_HDC(hdc, f)
24851 #endif
24853 static void
24854 init_glyph_string (struct glyph_string *s,
24855 OPTIONAL_HDC (hdc)
24856 XChar2b *char2b, struct window *w, struct glyph_row *row,
24857 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24859 memset (s, 0, sizeof *s);
24860 s->w = w;
24861 s->f = XFRAME (w->frame);
24862 #ifdef HAVE_NTGUI
24863 s->hdc = hdc;
24864 #endif
24865 s->display = FRAME_X_DISPLAY (s->f);
24866 s->char2b = char2b;
24867 s->hl = hl;
24868 s->row = row;
24869 s->area = area;
24870 s->first_glyph = row->glyphs[area] + start;
24871 s->height = row->height;
24872 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24873 s->ybase = s->y + row->ascent;
24877 /* Append the list of glyph strings with head H and tail T to the list
24878 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24880 static void
24881 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24882 struct glyph_string *h, struct glyph_string *t)
24884 if (h)
24886 if (*head)
24887 (*tail)->next = h;
24888 else
24889 *head = h;
24890 h->prev = *tail;
24891 *tail = t;
24896 /* Prepend the list of glyph strings with head H and tail T to the
24897 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24898 result. */
24900 static void
24901 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24902 struct glyph_string *h, struct glyph_string *t)
24904 if (h)
24906 if (*head)
24907 (*head)->prev = t;
24908 else
24909 *tail = t;
24910 t->next = *head;
24911 *head = h;
24916 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24917 Set *HEAD and *TAIL to the resulting list. */
24919 static void
24920 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24921 struct glyph_string *s)
24923 s->next = s->prev = NULL;
24924 append_glyph_string_lists (head, tail, s, s);
24928 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24929 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24930 make sure that X resources for the face returned are allocated.
24931 Value is a pointer to a realized face that is ready for display if
24932 DISPLAY_P. */
24934 static struct face *
24935 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24936 XChar2b *char2b, bool display_p)
24938 struct face *face = FACE_FROM_ID (f, face_id);
24939 unsigned code = 0;
24941 if (face->font)
24943 code = face->font->driver->encode_char (face->font, c);
24945 if (code == FONT_INVALID_CODE)
24946 code = 0;
24948 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24950 /* Make sure X resources of the face are allocated. */
24951 #ifdef HAVE_X_WINDOWS
24952 if (display_p)
24953 #endif
24955 eassert (face != NULL);
24956 prepare_face_for_display (f, face);
24959 return face;
24963 /* Get face and two-byte form of character glyph GLYPH on frame F.
24964 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24965 a pointer to a realized face that is ready for display. */
24967 static struct face *
24968 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24969 XChar2b *char2b)
24971 struct face *face;
24972 unsigned code = 0;
24974 eassert (glyph->type == CHAR_GLYPH);
24975 face = FACE_FROM_ID (f, glyph->face_id);
24977 /* Make sure X resources of the face are allocated. */
24978 prepare_face_for_display (f, face);
24980 if (face->font)
24982 if (CHAR_BYTE8_P (glyph->u.ch))
24983 code = CHAR_TO_BYTE8 (glyph->u.ch);
24984 else
24985 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24987 if (code == FONT_INVALID_CODE)
24988 code = 0;
24991 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24992 return face;
24996 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24997 Return true iff FONT has a glyph for C. */
24999 static bool
25000 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25002 unsigned code;
25004 if (CHAR_BYTE8_P (c))
25005 code = CHAR_TO_BYTE8 (c);
25006 else
25007 code = font->driver->encode_char (font, c);
25009 if (code == FONT_INVALID_CODE)
25010 return false;
25011 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25012 return true;
25016 /* Fill glyph string S with composition components specified by S->cmp.
25018 BASE_FACE is the base face of the composition.
25019 S->cmp_from is the index of the first component for S.
25021 OVERLAPS non-zero means S should draw the foreground only, and use
25022 its physical height for clipping. See also draw_glyphs.
25024 Value is the index of a component not in S. */
25026 static int
25027 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25028 int overlaps)
25030 int i;
25031 /* For all glyphs of this composition, starting at the offset
25032 S->cmp_from, until we reach the end of the definition or encounter a
25033 glyph that requires the different face, add it to S. */
25034 struct face *face;
25036 eassert (s);
25038 s->for_overlaps = overlaps;
25039 s->face = NULL;
25040 s->font = NULL;
25041 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25043 int c = COMPOSITION_GLYPH (s->cmp, i);
25045 /* TAB in a composition means display glyphs with padding space
25046 on the left or right. */
25047 if (c != '\t')
25049 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25050 -1, Qnil);
25052 face = get_char_face_and_encoding (s->f, c, face_id,
25053 s->char2b + i, true);
25054 if (face)
25056 if (! s->face)
25058 s->face = face;
25059 s->font = s->face->font;
25061 else if (s->face != face)
25062 break;
25065 ++s->nchars;
25067 s->cmp_to = i;
25069 if (s->face == NULL)
25071 s->face = base_face->ascii_face;
25072 s->font = s->face->font;
25075 /* All glyph strings for the same composition has the same width,
25076 i.e. the width set for the first component of the composition. */
25077 s->width = s->first_glyph->pixel_width;
25079 /* If the specified font could not be loaded, use the frame's
25080 default font, but record the fact that we couldn't load it in
25081 the glyph string so that we can draw rectangles for the
25082 characters of the glyph string. */
25083 if (s->font == NULL)
25085 s->font_not_found_p = true;
25086 s->font = FRAME_FONT (s->f);
25089 /* Adjust base line for subscript/superscript text. */
25090 s->ybase += s->first_glyph->voffset;
25092 return s->cmp_to;
25095 static int
25096 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25097 int start, int end, int overlaps)
25099 struct glyph *glyph, *last;
25100 Lisp_Object lgstring;
25101 int i;
25103 s->for_overlaps = overlaps;
25104 glyph = s->row->glyphs[s->area] + start;
25105 last = s->row->glyphs[s->area] + end;
25106 s->cmp_id = glyph->u.cmp.id;
25107 s->cmp_from = glyph->slice.cmp.from;
25108 s->cmp_to = glyph->slice.cmp.to + 1;
25109 s->face = FACE_FROM_ID (s->f, face_id);
25110 lgstring = composition_gstring_from_id (s->cmp_id);
25111 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25112 glyph++;
25113 while (glyph < last
25114 && glyph->u.cmp.automatic
25115 && glyph->u.cmp.id == s->cmp_id
25116 && s->cmp_to == glyph->slice.cmp.from)
25117 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25119 for (i = s->cmp_from; i < s->cmp_to; i++)
25121 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25122 unsigned code = LGLYPH_CODE (lglyph);
25124 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25126 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25127 return glyph - s->row->glyphs[s->area];
25131 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25132 See the comment of fill_glyph_string for arguments.
25133 Value is the index of the first glyph not in S. */
25136 static int
25137 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25138 int start, int end, int overlaps)
25140 struct glyph *glyph, *last;
25141 int voffset;
25143 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25144 s->for_overlaps = overlaps;
25145 glyph = s->row->glyphs[s->area] + start;
25146 last = s->row->glyphs[s->area] + end;
25147 voffset = glyph->voffset;
25148 s->face = FACE_FROM_ID (s->f, face_id);
25149 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25150 s->nchars = 1;
25151 s->width = glyph->pixel_width;
25152 glyph++;
25153 while (glyph < last
25154 && glyph->type == GLYPHLESS_GLYPH
25155 && glyph->voffset == voffset
25156 && glyph->face_id == face_id)
25158 s->nchars++;
25159 s->width += glyph->pixel_width;
25160 glyph++;
25162 s->ybase += voffset;
25163 return glyph - s->row->glyphs[s->area];
25167 /* Fill glyph string S from a sequence of character glyphs.
25169 FACE_ID is the face id of the string. START is the index of the
25170 first glyph to consider, END is the index of the last + 1.
25171 OVERLAPS non-zero means S should draw the foreground only, and use
25172 its physical height for clipping. See also draw_glyphs.
25174 Value is the index of the first glyph not in S. */
25176 static int
25177 fill_glyph_string (struct glyph_string *s, int face_id,
25178 int start, int end, int overlaps)
25180 struct glyph *glyph, *last;
25181 int voffset;
25182 bool glyph_not_available_p;
25184 eassert (s->f == XFRAME (s->w->frame));
25185 eassert (s->nchars == 0);
25186 eassert (start >= 0 && end > start);
25188 s->for_overlaps = overlaps;
25189 glyph = s->row->glyphs[s->area] + start;
25190 last = s->row->glyphs[s->area] + end;
25191 voffset = glyph->voffset;
25192 s->padding_p = glyph->padding_p;
25193 glyph_not_available_p = glyph->glyph_not_available_p;
25195 while (glyph < last
25196 && glyph->type == CHAR_GLYPH
25197 && glyph->voffset == voffset
25198 /* Same face id implies same font, nowadays. */
25199 && glyph->face_id == face_id
25200 && glyph->glyph_not_available_p == glyph_not_available_p)
25202 s->face = get_glyph_face_and_encoding (s->f, glyph,
25203 s->char2b + s->nchars);
25204 ++s->nchars;
25205 eassert (s->nchars <= end - start);
25206 s->width += glyph->pixel_width;
25207 if (glyph++->padding_p != s->padding_p)
25208 break;
25211 s->font = s->face->font;
25213 /* If the specified font could not be loaded, use the frame's font,
25214 but record the fact that we couldn't load it in
25215 S->font_not_found_p so that we can draw rectangles for the
25216 characters of the glyph string. */
25217 if (s->font == NULL || glyph_not_available_p)
25219 s->font_not_found_p = true;
25220 s->font = FRAME_FONT (s->f);
25223 /* Adjust base line for subscript/superscript text. */
25224 s->ybase += voffset;
25226 eassert (s->face && s->face->gc);
25227 return glyph - s->row->glyphs[s->area];
25231 /* Fill glyph string S from image glyph S->first_glyph. */
25233 static void
25234 fill_image_glyph_string (struct glyph_string *s)
25236 eassert (s->first_glyph->type == IMAGE_GLYPH);
25237 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25238 eassert (s->img);
25239 s->slice = s->first_glyph->slice.img;
25240 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25241 s->font = s->face->font;
25242 s->width = s->first_glyph->pixel_width;
25244 /* Adjust base line for subscript/superscript text. */
25245 s->ybase += s->first_glyph->voffset;
25249 #ifdef HAVE_XWIDGETS
25250 static void
25251 fill_xwidget_glyph_string (struct glyph_string *s)
25253 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25254 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25255 s->font = s->face->font;
25256 s->width = s->first_glyph->pixel_width;
25257 s->ybase += s->first_glyph->voffset;
25258 s->xwidget = s->first_glyph->u.xwidget;
25260 #endif
25261 /* Fill glyph string S from a sequence of stretch glyphs.
25263 START is the index of the first glyph to consider,
25264 END is the index of the last + 1.
25266 Value is the index of the first glyph not in S. */
25268 static int
25269 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25271 struct glyph *glyph, *last;
25272 int voffset, face_id;
25274 eassert (s->first_glyph->type == STRETCH_GLYPH);
25276 glyph = s->row->glyphs[s->area] + start;
25277 last = s->row->glyphs[s->area] + end;
25278 face_id = glyph->face_id;
25279 s->face = FACE_FROM_ID (s->f, face_id);
25280 s->font = s->face->font;
25281 s->width = glyph->pixel_width;
25282 s->nchars = 1;
25283 voffset = glyph->voffset;
25285 for (++glyph;
25286 (glyph < last
25287 && glyph->type == STRETCH_GLYPH
25288 && glyph->voffset == voffset
25289 && glyph->face_id == face_id);
25290 ++glyph)
25291 s->width += glyph->pixel_width;
25293 /* Adjust base line for subscript/superscript text. */
25294 s->ybase += voffset;
25296 /* The case that face->gc == 0 is handled when drawing the glyph
25297 string by calling prepare_face_for_display. */
25298 eassert (s->face);
25299 return glyph - s->row->glyphs[s->area];
25302 static struct font_metrics *
25303 get_per_char_metric (struct font *font, XChar2b *char2b)
25305 static struct font_metrics metrics;
25306 unsigned code;
25308 if (! font)
25309 return NULL;
25310 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25311 if (code == FONT_INVALID_CODE)
25312 return NULL;
25313 font->driver->text_extents (font, &code, 1, &metrics);
25314 return &metrics;
25317 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25318 for FONT. Values are taken from font-global ones, except for fonts
25319 that claim preposterously large values, but whose glyphs actually
25320 have reasonable dimensions. C is the character to use for metrics
25321 if the font-global values are too large; if C is negative, the
25322 function selects a default character. */
25323 static void
25324 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25326 *ascent = FONT_BASE (font);
25327 *descent = FONT_DESCENT (font);
25329 if (FONT_TOO_HIGH (font))
25331 XChar2b char2b;
25333 /* Get metrics of C, defaulting to a reasonably sized ASCII
25334 character. */
25335 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25337 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25339 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25341 /* We add 1 pixel to character dimensions as heuristics
25342 that produces nicer display, e.g. when the face has
25343 the box attribute. */
25344 *ascent = pcm->ascent + 1;
25345 *descent = pcm->descent + 1;
25351 /* A subroutine that computes a reasonable "normal character height"
25352 for fonts that claim preposterously large vertical dimensions, but
25353 whose glyphs are actually reasonably sized. C is the character
25354 whose metrics to use for those fonts, or -1 for default
25355 character. */
25356 static int
25357 normal_char_height (struct font *font, int c)
25359 int ascent, descent;
25361 normal_char_ascent_descent (font, c, &ascent, &descent);
25363 return ascent + descent;
25366 /* EXPORT for RIF:
25367 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25368 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25369 assumed to be zero. */
25371 void
25372 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25374 *left = *right = 0;
25376 if (glyph->type == CHAR_GLYPH)
25378 XChar2b char2b;
25379 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25380 if (face->font)
25382 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25383 if (pcm)
25385 if (pcm->rbearing > pcm->width)
25386 *right = pcm->rbearing - pcm->width;
25387 if (pcm->lbearing < 0)
25388 *left = -pcm->lbearing;
25392 else if (glyph->type == COMPOSITE_GLYPH)
25394 if (! glyph->u.cmp.automatic)
25396 struct composition *cmp = composition_table[glyph->u.cmp.id];
25398 if (cmp->rbearing > cmp->pixel_width)
25399 *right = cmp->rbearing - cmp->pixel_width;
25400 if (cmp->lbearing < 0)
25401 *left = - cmp->lbearing;
25403 else
25405 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25406 struct font_metrics metrics;
25408 composition_gstring_width (gstring, glyph->slice.cmp.from,
25409 glyph->slice.cmp.to + 1, &metrics);
25410 if (metrics.rbearing > metrics.width)
25411 *right = metrics.rbearing - metrics.width;
25412 if (metrics.lbearing < 0)
25413 *left = - metrics.lbearing;
25419 /* Return the index of the first glyph preceding glyph string S that
25420 is overwritten by S because of S's left overhang. Value is -1
25421 if no glyphs are overwritten. */
25423 static int
25424 left_overwritten (struct glyph_string *s)
25426 int k;
25428 if (s->left_overhang)
25430 int x = 0, i;
25431 struct glyph *glyphs = s->row->glyphs[s->area];
25432 int first = s->first_glyph - glyphs;
25434 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25435 x -= glyphs[i].pixel_width;
25437 k = i + 1;
25439 else
25440 k = -1;
25442 return k;
25446 /* Return the index of the first glyph preceding glyph string S that
25447 is overwriting S because of its right overhang. Value is -1 if no
25448 glyph in front of S overwrites S. */
25450 static int
25451 left_overwriting (struct glyph_string *s)
25453 int i, k, x;
25454 struct glyph *glyphs = s->row->glyphs[s->area];
25455 int first = s->first_glyph - glyphs;
25457 k = -1;
25458 x = 0;
25459 for (i = first - 1; i >= 0; --i)
25461 int left, right;
25462 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25463 if (x + right > 0)
25464 k = i;
25465 x -= glyphs[i].pixel_width;
25468 return k;
25472 /* Return the index of the last glyph following glyph string S that is
25473 overwritten by S because of S's right overhang. Value is -1 if
25474 no such glyph is found. */
25476 static int
25477 right_overwritten (struct glyph_string *s)
25479 int k = -1;
25481 if (s->right_overhang)
25483 int x = 0, i;
25484 struct glyph *glyphs = s->row->glyphs[s->area];
25485 int first = (s->first_glyph - glyphs
25486 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25487 int end = s->row->used[s->area];
25489 for (i = first; i < end && s->right_overhang > x; ++i)
25490 x += glyphs[i].pixel_width;
25492 k = i;
25495 return k;
25499 /* Return the index of the last glyph following glyph string S that
25500 overwrites S because of its left overhang. Value is negative
25501 if no such glyph is found. */
25503 static int
25504 right_overwriting (struct glyph_string *s)
25506 int i, k, x;
25507 int end = s->row->used[s->area];
25508 struct glyph *glyphs = s->row->glyphs[s->area];
25509 int first = (s->first_glyph - glyphs
25510 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25512 k = -1;
25513 x = 0;
25514 for (i = first; i < end; ++i)
25516 int left, right;
25517 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25518 if (x - left < 0)
25519 k = i;
25520 x += glyphs[i].pixel_width;
25523 return k;
25527 /* Set background width of glyph string S. START is the index of the
25528 first glyph following S. LAST_X is the right-most x-position + 1
25529 in the drawing area. */
25531 static void
25532 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25534 /* If the face of this glyph string has to be drawn to the end of
25535 the drawing area, set S->extends_to_end_of_line_p. */
25537 if (start == s->row->used[s->area]
25538 && ((s->row->fill_line_p
25539 && (s->hl == DRAW_NORMAL_TEXT
25540 || s->hl == DRAW_IMAGE_RAISED
25541 || s->hl == DRAW_IMAGE_SUNKEN))
25542 || s->hl == DRAW_MOUSE_FACE))
25543 s->extends_to_end_of_line_p = true;
25545 /* If S extends its face to the end of the line, set its
25546 background_width to the distance to the right edge of the drawing
25547 area. */
25548 if (s->extends_to_end_of_line_p)
25549 s->background_width = last_x - s->x + 1;
25550 else
25551 s->background_width = s->width;
25555 /* Return glyph string that shares background with glyph string S and
25556 whose `background_width' member has been set. */
25558 static struct glyph_string *
25559 glyph_string_containing_background_width (struct glyph_string *s)
25561 if (s->cmp)
25562 while (s->cmp_from)
25563 s = s->prev;
25565 return s;
25569 /* Compute overhangs and x-positions for glyph string S and its
25570 predecessors, or successors. X is the starting x-position for S.
25571 BACKWARD_P means process predecessors. */
25573 static void
25574 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25576 if (backward_p)
25578 while (s)
25580 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25581 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25582 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25583 x -= s->width;
25584 s->x = x;
25585 s = s->prev;
25588 else
25590 while (s)
25592 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25593 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25594 s->x = x;
25595 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25596 x += s->width;
25597 s = s->next;
25604 /* The following macros are only called from draw_glyphs below.
25605 They reference the following parameters of that function directly:
25606 `w', `row', `area', and `overlap_p'
25607 as well as the following local variables:
25608 `s', `f', and `hdc' (in W32) */
25610 #ifdef HAVE_NTGUI
25611 /* On W32, silently add local `hdc' variable to argument list of
25612 init_glyph_string. */
25613 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25614 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25615 #else
25616 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25617 init_glyph_string (s, char2b, w, row, area, start, hl)
25618 #endif
25620 /* Add a glyph string for a stretch glyph to the list of strings
25621 between HEAD and TAIL. START is the index of the stretch glyph in
25622 row area AREA of glyph row ROW. END is the index of the last glyph
25623 in that glyph row area. X is the current output position assigned
25624 to the new glyph string constructed. HL overrides that face of the
25625 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25626 is the right-most x-position of the drawing area. */
25628 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25629 and below -- keep them on one line. */
25630 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25631 do \
25633 s = alloca (sizeof *s); \
25634 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25635 START = fill_stretch_glyph_string (s, START, END); \
25636 append_glyph_string (&HEAD, &TAIL, s); \
25637 s->x = (X); \
25639 while (false)
25642 /* Add a glyph string for an image glyph to the list of strings
25643 between HEAD and TAIL. START is the index of the image glyph in
25644 row area AREA of glyph row ROW. END is the index of the last glyph
25645 in that glyph row area. X is the current output position assigned
25646 to the new glyph string constructed. HL overrides that face of the
25647 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25648 is the right-most x-position of the drawing area. */
25650 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25651 do \
25653 s = alloca (sizeof *s); \
25654 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25655 fill_image_glyph_string (s); \
25656 append_glyph_string (&HEAD, &TAIL, s); \
25657 ++START; \
25658 s->x = (X); \
25660 while (false)
25662 #ifndef HAVE_XWIDGETS
25663 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25664 eassume (false)
25665 #else
25666 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25667 do \
25669 s = alloca (sizeof *s); \
25670 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25671 fill_xwidget_glyph_string (s); \
25672 append_glyph_string (&(HEAD), &(TAIL), s); \
25673 ++(START); \
25674 s->x = (X); \
25676 while (false)
25677 #endif
25679 /* Add a glyph string for a sequence of character glyphs to the list
25680 of strings between HEAD and TAIL. START is the index of the first
25681 glyph in row area AREA of glyph row ROW that is part of the new
25682 glyph string. END is the index of the last glyph in that glyph row
25683 area. X is the current output position assigned to the new glyph
25684 string constructed. HL overrides that face of the glyph; e.g. it
25685 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25686 right-most x-position of the drawing area. */
25688 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25689 do \
25691 int face_id; \
25692 XChar2b *char2b; \
25694 face_id = (row)->glyphs[area][START].face_id; \
25696 s = alloca (sizeof *s); \
25697 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25698 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25699 append_glyph_string (&HEAD, &TAIL, s); \
25700 s->x = (X); \
25701 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25703 while (false)
25706 /* Add a glyph string for a composite sequence to the list of strings
25707 between HEAD and TAIL. START is the index of the first glyph in
25708 row area AREA of glyph row ROW that is part of the new glyph
25709 string. END is the index of the last glyph in that glyph row area.
25710 X is the current output position assigned to the new glyph string
25711 constructed. HL overrides that face of the glyph; e.g. it is
25712 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25713 x-position of the drawing area. */
25715 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25716 do { \
25717 int face_id = (row)->glyphs[area][START].face_id; \
25718 struct face *base_face = FACE_FROM_ID (f, face_id); \
25719 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25720 struct composition *cmp = composition_table[cmp_id]; \
25721 XChar2b *char2b; \
25722 struct glyph_string *first_s = NULL; \
25723 int n; \
25725 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25727 /* Make glyph_strings for each glyph sequence that is drawable by \
25728 the same face, and append them to HEAD/TAIL. */ \
25729 for (n = 0; n < cmp->glyph_len;) \
25731 s = alloca (sizeof *s); \
25732 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25733 append_glyph_string (&(HEAD), &(TAIL), s); \
25734 s->cmp = cmp; \
25735 s->cmp_from = n; \
25736 s->x = (X); \
25737 if (n == 0) \
25738 first_s = s; \
25739 n = fill_composite_glyph_string (s, base_face, overlaps); \
25742 ++START; \
25743 s = first_s; \
25744 } while (false)
25747 /* Add a glyph string for a glyph-string sequence to the list of strings
25748 between HEAD and TAIL. */
25750 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25751 do { \
25752 int face_id; \
25753 XChar2b *char2b; \
25754 Lisp_Object gstring; \
25756 face_id = (row)->glyphs[area][START].face_id; \
25757 gstring = (composition_gstring_from_id \
25758 ((row)->glyphs[area][START].u.cmp.id)); \
25759 s = alloca (sizeof *s); \
25760 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25761 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25762 append_glyph_string (&(HEAD), &(TAIL), s); \
25763 s->x = (X); \
25764 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25765 } while (false)
25768 /* Add a glyph string for a sequence of glyphless character's glyphs
25769 to the list of strings between HEAD and TAIL. The meanings of
25770 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25772 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25773 do \
25775 int face_id; \
25777 face_id = (row)->glyphs[area][START].face_id; \
25779 s = alloca (sizeof *s); \
25780 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25781 append_glyph_string (&HEAD, &TAIL, s); \
25782 s->x = (X); \
25783 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25784 overlaps); \
25786 while (false)
25789 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25790 of AREA of glyph row ROW on window W between indices START and END.
25791 HL overrides the face for drawing glyph strings, e.g. it is
25792 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25793 x-positions of the drawing area.
25795 This is an ugly monster macro construct because we must use alloca
25796 to allocate glyph strings (because draw_glyphs can be called
25797 asynchronously). */
25799 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25800 do \
25802 HEAD = TAIL = NULL; \
25803 while (START < END) \
25805 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25806 switch (first_glyph->type) \
25808 case CHAR_GLYPH: \
25809 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25810 HL, X, LAST_X); \
25811 break; \
25813 case COMPOSITE_GLYPH: \
25814 if (first_glyph->u.cmp.automatic) \
25815 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25816 HL, X, LAST_X); \
25817 else \
25818 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25819 HL, X, LAST_X); \
25820 break; \
25822 case STRETCH_GLYPH: \
25823 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25824 HL, X, LAST_X); \
25825 break; \
25827 case IMAGE_GLYPH: \
25828 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25829 HL, X, LAST_X); \
25830 break;
25832 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25833 case XWIDGET_GLYPH: \
25834 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25835 HL, X, LAST_X); \
25836 break;
25838 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25839 case GLYPHLESS_GLYPH: \
25840 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25841 HL, X, LAST_X); \
25842 break; \
25844 default: \
25845 emacs_abort (); \
25848 if (s) \
25850 set_glyph_string_background_width (s, START, LAST_X); \
25851 (X) += s->width; \
25854 } while (false)
25857 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25858 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25859 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25860 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25863 /* Draw glyphs between START and END in AREA of ROW on window W,
25864 starting at x-position X. X is relative to AREA in W. HL is a
25865 face-override with the following meaning:
25867 DRAW_NORMAL_TEXT draw normally
25868 DRAW_CURSOR draw in cursor face
25869 DRAW_MOUSE_FACE draw in mouse face.
25870 DRAW_INVERSE_VIDEO draw in mode line face
25871 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25872 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25874 If OVERLAPS is non-zero, draw only the foreground of characters and
25875 clip to the physical height of ROW. Non-zero value also defines
25876 the overlapping part to be drawn:
25878 OVERLAPS_PRED overlap with preceding rows
25879 OVERLAPS_SUCC overlap with succeeding rows
25880 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25881 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25883 Value is the x-position reached, relative to AREA of W. */
25885 static int
25886 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25887 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25888 enum draw_glyphs_face hl, int overlaps)
25890 struct glyph_string *head, *tail;
25891 struct glyph_string *s;
25892 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25893 int i, j, x_reached, last_x, area_left = 0;
25894 struct frame *f = XFRAME (WINDOW_FRAME (w));
25895 DECLARE_HDC (hdc);
25897 ALLOCATE_HDC (hdc, f);
25899 /* Let's rather be paranoid than getting a SEGV. */
25900 end = min (end, row->used[area]);
25901 start = clip_to_bounds (0, start, end);
25903 /* Translate X to frame coordinates. Set last_x to the right
25904 end of the drawing area. */
25905 if (row->full_width_p)
25907 /* X is relative to the left edge of W, without scroll bars
25908 or fringes. */
25909 area_left = WINDOW_LEFT_EDGE_X (w);
25910 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25911 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25913 else
25915 area_left = window_box_left (w, area);
25916 last_x = area_left + window_box_width (w, area);
25918 x += area_left;
25920 /* Build a doubly-linked list of glyph_string structures between
25921 head and tail from what we have to draw. Note that the macro
25922 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25923 the reason we use a separate variable `i'. */
25924 i = start;
25925 USE_SAFE_ALLOCA;
25926 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25927 if (tail)
25929 s = glyph_string_containing_background_width (tail);
25930 x_reached = s->x + s->background_width;
25932 else
25933 x_reached = x;
25935 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25936 the row, redraw some glyphs in front or following the glyph
25937 strings built above. */
25938 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25940 struct glyph_string *h, *t;
25941 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25942 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25943 bool check_mouse_face = false;
25944 int dummy_x = 0;
25946 /* If mouse highlighting is on, we may need to draw adjacent
25947 glyphs using mouse-face highlighting. */
25948 if (area == TEXT_AREA && row->mouse_face_p
25949 && hlinfo->mouse_face_beg_row >= 0
25950 && hlinfo->mouse_face_end_row >= 0)
25952 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25954 if (row_vpos >= hlinfo->mouse_face_beg_row
25955 && row_vpos <= hlinfo->mouse_face_end_row)
25957 check_mouse_face = true;
25958 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25959 ? hlinfo->mouse_face_beg_col : 0;
25960 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25961 ? hlinfo->mouse_face_end_col
25962 : row->used[TEXT_AREA];
25966 /* Compute overhangs for all glyph strings. */
25967 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25968 for (s = head; s; s = s->next)
25969 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25971 /* Prepend glyph strings for glyphs in front of the first glyph
25972 string that are overwritten because of the first glyph
25973 string's left overhang. The background of all strings
25974 prepended must be drawn because the first glyph string
25975 draws over it. */
25976 i = left_overwritten (head);
25977 if (i >= 0)
25979 enum draw_glyphs_face overlap_hl;
25981 /* If this row contains mouse highlighting, attempt to draw
25982 the overlapped glyphs with the correct highlight. This
25983 code fails if the overlap encompasses more than one glyph
25984 and mouse-highlight spans only some of these glyphs.
25985 However, making it work perfectly involves a lot more
25986 code, and I don't know if the pathological case occurs in
25987 practice, so we'll stick to this for now. --- cyd */
25988 if (check_mouse_face
25989 && mouse_beg_col < start && mouse_end_col > i)
25990 overlap_hl = DRAW_MOUSE_FACE;
25991 else
25992 overlap_hl = DRAW_NORMAL_TEXT;
25994 if (hl != overlap_hl)
25995 clip_head = head;
25996 j = i;
25997 BUILD_GLYPH_STRINGS (j, start, h, t,
25998 overlap_hl, dummy_x, last_x);
25999 start = i;
26000 compute_overhangs_and_x (t, head->x, true);
26001 prepend_glyph_string_lists (&head, &tail, h, t);
26002 if (clip_head == NULL)
26003 clip_head = head;
26006 /* Prepend glyph strings for glyphs in front of the first glyph
26007 string that overwrite that glyph string because of their
26008 right overhang. For these strings, only the foreground must
26009 be drawn, because it draws over the glyph string at `head'.
26010 The background must not be drawn because this would overwrite
26011 right overhangs of preceding glyphs for which no glyph
26012 strings exist. */
26013 i = left_overwriting (head);
26014 if (i >= 0)
26016 enum draw_glyphs_face overlap_hl;
26018 if (check_mouse_face
26019 && mouse_beg_col < start && mouse_end_col > i)
26020 overlap_hl = DRAW_MOUSE_FACE;
26021 else
26022 overlap_hl = DRAW_NORMAL_TEXT;
26024 if (hl == overlap_hl || clip_head == NULL)
26025 clip_head = head;
26026 BUILD_GLYPH_STRINGS (i, start, h, t,
26027 overlap_hl, dummy_x, last_x);
26028 for (s = h; s; s = s->next)
26029 s->background_filled_p = true;
26030 compute_overhangs_and_x (t, head->x, true);
26031 prepend_glyph_string_lists (&head, &tail, h, t);
26034 /* Append glyphs strings for glyphs following the last glyph
26035 string tail that are overwritten by tail. The background of
26036 these strings has to be drawn because tail's foreground draws
26037 over it. */
26038 i = right_overwritten (tail);
26039 if (i >= 0)
26041 enum draw_glyphs_face overlap_hl;
26043 if (check_mouse_face
26044 && mouse_beg_col < i && mouse_end_col > end)
26045 overlap_hl = DRAW_MOUSE_FACE;
26046 else
26047 overlap_hl = DRAW_NORMAL_TEXT;
26049 if (hl != overlap_hl)
26050 clip_tail = tail;
26051 BUILD_GLYPH_STRINGS (end, i, h, t,
26052 overlap_hl, x, last_x);
26053 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26054 we don't have `end = i;' here. */
26055 compute_overhangs_and_x (h, tail->x + tail->width, false);
26056 append_glyph_string_lists (&head, &tail, h, t);
26057 if (clip_tail == NULL)
26058 clip_tail = tail;
26061 /* Append glyph strings for glyphs following the last glyph
26062 string tail that overwrite tail. The foreground of such
26063 glyphs has to be drawn because it writes into the background
26064 of tail. The background must not be drawn because it could
26065 paint over the foreground of following glyphs. */
26066 i = right_overwriting (tail);
26067 if (i >= 0)
26069 enum draw_glyphs_face overlap_hl;
26070 if (check_mouse_face
26071 && mouse_beg_col < i && mouse_end_col > end)
26072 overlap_hl = DRAW_MOUSE_FACE;
26073 else
26074 overlap_hl = DRAW_NORMAL_TEXT;
26076 if (hl == overlap_hl || clip_tail == NULL)
26077 clip_tail = tail;
26078 i++; /* We must include the Ith glyph. */
26079 BUILD_GLYPH_STRINGS (end, i, h, t,
26080 overlap_hl, x, last_x);
26081 for (s = h; s; s = s->next)
26082 s->background_filled_p = true;
26083 compute_overhangs_and_x (h, tail->x + tail->width, false);
26084 append_glyph_string_lists (&head, &tail, h, t);
26086 tail = glyph_string_containing_background_width (tail);
26087 if (clip_tail)
26088 clip_tail = glyph_string_containing_background_width (clip_tail);
26089 if (clip_head || clip_tail)
26090 for (s = head; s; s = s->next)
26092 s->clip_head = clip_head;
26093 s->clip_tail = clip_tail;
26097 /* Draw all strings. */
26098 for (s = head; s; s = s->next)
26099 FRAME_RIF (f)->draw_glyph_string (s);
26101 #ifndef HAVE_NS
26102 /* When focus a sole frame and move horizontally, this clears on_p
26103 causing a failure to erase prev cursor position. */
26104 if (area == TEXT_AREA
26105 && !row->full_width_p
26106 /* When drawing overlapping rows, only the glyph strings'
26107 foreground is drawn, which doesn't erase a cursor
26108 completely. */
26109 && !overlaps)
26111 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26112 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26113 : (tail ? tail->x + tail->background_width : x));
26114 x0 -= area_left;
26115 x1 -= area_left;
26117 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26118 row->y, MATRIX_ROW_BOTTOM_Y (row));
26120 #endif
26122 /* Value is the x-position up to which drawn, relative to AREA of W.
26123 This doesn't include parts drawn because of overhangs. */
26124 if (row->full_width_p)
26125 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26126 else
26127 x_reached -= area_left;
26129 RELEASE_HDC (hdc, f);
26131 SAFE_FREE ();
26132 return x_reached;
26135 /* Find the first glyph in the run of underlined glyphs preceding the
26136 beginning of glyph string S, and return its font (which could be
26137 NULL). This is needed because that font determines the underline
26138 position and thickness for the entire run of the underlined glyphs.
26139 This function is called from the draw_glyph_string method of GUI
26140 frame's redisplay interface (RIF) when it needs to draw in an
26141 underlined face. */
26142 struct font *
26143 font_for_underline_metrics (struct glyph_string *s)
26145 struct glyph *g0 = s->row->glyphs[s->area], *g;
26147 for (g = s->first_glyph - 1; g >= g0; g--)
26149 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26150 if (!(prev_face && prev_face->underline_p))
26151 break;
26154 /* If preceding glyphs are not underlined, use the font of S. */
26155 if (g == s->first_glyph - 1)
26156 return s->font;
26157 else
26159 /* Otherwise use the font of the last glyph we saw in the above
26160 loop whose face had the underline_p flag set. */
26161 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26165 /* Expand row matrix if too narrow. Don't expand if area
26166 is not present. */
26168 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26170 if (!it->f->fonts_changed \
26171 && (it->glyph_row->glyphs[area] \
26172 < it->glyph_row->glyphs[area + 1])) \
26174 it->w->ncols_scale_factor++; \
26175 it->f->fonts_changed = true; \
26179 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26180 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26182 static void
26183 append_glyph (struct it *it)
26185 struct glyph *glyph;
26186 enum glyph_row_area area = it->area;
26188 eassert (it->glyph_row);
26189 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26191 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26192 if (glyph < it->glyph_row->glyphs[area + 1])
26194 /* If the glyph row is reversed, we need to prepend the glyph
26195 rather than append it. */
26196 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26198 struct glyph *g;
26200 /* Make room for the additional glyph. */
26201 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26202 g[1] = *g;
26203 glyph = it->glyph_row->glyphs[area];
26205 glyph->charpos = CHARPOS (it->position);
26206 glyph->object = it->object;
26207 if (it->pixel_width > 0)
26209 eassert (it->pixel_width <= SHRT_MAX);
26210 glyph->pixel_width = it->pixel_width;
26211 glyph->padding_p = false;
26213 else
26215 /* Assure at least 1-pixel width. Otherwise, cursor can't
26216 be displayed correctly. */
26217 glyph->pixel_width = 1;
26218 glyph->padding_p = true;
26220 glyph->ascent = it->ascent;
26221 glyph->descent = it->descent;
26222 glyph->voffset = it->voffset;
26223 glyph->type = CHAR_GLYPH;
26224 glyph->avoid_cursor_p = it->avoid_cursor_p;
26225 glyph->multibyte_p = it->multibyte_p;
26226 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26228 /* In R2L rows, the left and the right box edges need to be
26229 drawn in reverse direction. */
26230 glyph->right_box_line_p = it->start_of_box_run_p;
26231 glyph->left_box_line_p = it->end_of_box_run_p;
26233 else
26235 glyph->left_box_line_p = it->start_of_box_run_p;
26236 glyph->right_box_line_p = it->end_of_box_run_p;
26238 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26239 || it->phys_descent > it->descent);
26240 glyph->glyph_not_available_p = it->glyph_not_available_p;
26241 glyph->face_id = it->face_id;
26242 glyph->u.ch = it->char_to_display;
26243 glyph->slice.img = null_glyph_slice;
26244 glyph->font_type = FONT_TYPE_UNKNOWN;
26245 if (it->bidi_p)
26247 glyph->resolved_level = it->bidi_it.resolved_level;
26248 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26249 glyph->bidi_type = it->bidi_it.type;
26251 else
26253 glyph->resolved_level = 0;
26254 glyph->bidi_type = UNKNOWN_BT;
26256 ++it->glyph_row->used[area];
26258 else
26259 IT_EXPAND_MATRIX_WIDTH (it, area);
26262 /* Store one glyph for the composition IT->cmp_it.id in
26263 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26264 non-null. */
26266 static void
26267 append_composite_glyph (struct it *it)
26269 struct glyph *glyph;
26270 enum glyph_row_area area = it->area;
26272 eassert (it->glyph_row);
26274 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26275 if (glyph < it->glyph_row->glyphs[area + 1])
26277 /* If the glyph row is reversed, we need to prepend the glyph
26278 rather than append it. */
26279 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26281 struct glyph *g;
26283 /* Make room for the new glyph. */
26284 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26285 g[1] = *g;
26286 glyph = it->glyph_row->glyphs[it->area];
26288 glyph->charpos = it->cmp_it.charpos;
26289 glyph->object = it->object;
26290 eassert (it->pixel_width <= SHRT_MAX);
26291 glyph->pixel_width = it->pixel_width;
26292 glyph->ascent = it->ascent;
26293 glyph->descent = it->descent;
26294 glyph->voffset = it->voffset;
26295 glyph->type = COMPOSITE_GLYPH;
26296 if (it->cmp_it.ch < 0)
26298 glyph->u.cmp.automatic = false;
26299 glyph->u.cmp.id = it->cmp_it.id;
26300 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26302 else
26304 glyph->u.cmp.automatic = true;
26305 glyph->u.cmp.id = it->cmp_it.id;
26306 glyph->slice.cmp.from = it->cmp_it.from;
26307 glyph->slice.cmp.to = it->cmp_it.to - 1;
26309 glyph->avoid_cursor_p = it->avoid_cursor_p;
26310 glyph->multibyte_p = it->multibyte_p;
26311 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26313 /* In R2L rows, the left and the right box edges need to be
26314 drawn in reverse direction. */
26315 glyph->right_box_line_p = it->start_of_box_run_p;
26316 glyph->left_box_line_p = it->end_of_box_run_p;
26318 else
26320 glyph->left_box_line_p = it->start_of_box_run_p;
26321 glyph->right_box_line_p = it->end_of_box_run_p;
26323 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26324 || it->phys_descent > it->descent);
26325 glyph->padding_p = false;
26326 glyph->glyph_not_available_p = false;
26327 glyph->face_id = it->face_id;
26328 glyph->font_type = FONT_TYPE_UNKNOWN;
26329 if (it->bidi_p)
26331 glyph->resolved_level = it->bidi_it.resolved_level;
26332 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26333 glyph->bidi_type = it->bidi_it.type;
26335 ++it->glyph_row->used[area];
26337 else
26338 IT_EXPAND_MATRIX_WIDTH (it, area);
26342 /* Change IT->ascent and IT->height according to the setting of
26343 IT->voffset. */
26345 static void
26346 take_vertical_position_into_account (struct it *it)
26348 if (it->voffset)
26350 if (it->voffset < 0)
26351 /* Increase the ascent so that we can display the text higher
26352 in the line. */
26353 it->ascent -= it->voffset;
26354 else
26355 /* Increase the descent so that we can display the text lower
26356 in the line. */
26357 it->descent += it->voffset;
26362 /* Produce glyphs/get display metrics for the image IT is loaded with.
26363 See the description of struct display_iterator in dispextern.h for
26364 an overview of struct display_iterator. */
26366 static void
26367 produce_image_glyph (struct it *it)
26369 struct image *img;
26370 struct face *face;
26371 int glyph_ascent, crop;
26372 struct glyph_slice slice;
26374 eassert (it->what == IT_IMAGE);
26376 face = FACE_FROM_ID (it->f, it->face_id);
26377 /* Make sure X resources of the face is loaded. */
26378 prepare_face_for_display (it->f, face);
26380 if (it->image_id < 0)
26382 /* Fringe bitmap. */
26383 it->ascent = it->phys_ascent = 0;
26384 it->descent = it->phys_descent = 0;
26385 it->pixel_width = 0;
26386 it->nglyphs = 0;
26387 return;
26390 img = IMAGE_FROM_ID (it->f, it->image_id);
26391 /* Make sure X resources of the image is loaded. */
26392 prepare_image_for_display (it->f, img);
26394 slice.x = slice.y = 0;
26395 slice.width = img->width;
26396 slice.height = img->height;
26398 if (INTEGERP (it->slice.x))
26399 slice.x = XINT (it->slice.x);
26400 else if (FLOATP (it->slice.x))
26401 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26403 if (INTEGERP (it->slice.y))
26404 slice.y = XINT (it->slice.y);
26405 else if (FLOATP (it->slice.y))
26406 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26408 if (INTEGERP (it->slice.width))
26409 slice.width = XINT (it->slice.width);
26410 else if (FLOATP (it->slice.width))
26411 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26413 if (INTEGERP (it->slice.height))
26414 slice.height = XINT (it->slice.height);
26415 else if (FLOATP (it->slice.height))
26416 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26418 if (slice.x >= img->width)
26419 slice.x = img->width;
26420 if (slice.y >= img->height)
26421 slice.y = img->height;
26422 if (slice.x + slice.width >= img->width)
26423 slice.width = img->width - slice.x;
26424 if (slice.y + slice.height > img->height)
26425 slice.height = img->height - slice.y;
26427 if (slice.width == 0 || slice.height == 0)
26428 return;
26430 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26432 it->descent = slice.height - glyph_ascent;
26433 if (slice.y == 0)
26434 it->descent += img->vmargin;
26435 if (slice.y + slice.height == img->height)
26436 it->descent += img->vmargin;
26437 it->phys_descent = it->descent;
26439 it->pixel_width = slice.width;
26440 if (slice.x == 0)
26441 it->pixel_width += img->hmargin;
26442 if (slice.x + slice.width == img->width)
26443 it->pixel_width += img->hmargin;
26445 /* It's quite possible for images to have an ascent greater than
26446 their height, so don't get confused in that case. */
26447 if (it->descent < 0)
26448 it->descent = 0;
26450 it->nglyphs = 1;
26452 if (face->box != FACE_NO_BOX)
26454 if (face->box_line_width > 0)
26456 if (slice.y == 0)
26457 it->ascent += face->box_line_width;
26458 if (slice.y + slice.height == img->height)
26459 it->descent += face->box_line_width;
26462 if (it->start_of_box_run_p && slice.x == 0)
26463 it->pixel_width += eabs (face->box_line_width);
26464 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26465 it->pixel_width += eabs (face->box_line_width);
26468 take_vertical_position_into_account (it);
26470 /* Automatically crop wide image glyphs at right edge so we can
26471 draw the cursor on same display row. */
26472 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26473 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26475 it->pixel_width -= crop;
26476 slice.width -= crop;
26479 if (it->glyph_row)
26481 struct glyph *glyph;
26482 enum glyph_row_area area = it->area;
26484 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26485 if (it->glyph_row->reversed_p)
26487 struct glyph *g;
26489 /* Make room for the new glyph. */
26490 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26491 g[1] = *g;
26492 glyph = it->glyph_row->glyphs[it->area];
26494 if (glyph < it->glyph_row->glyphs[area + 1])
26496 glyph->charpos = CHARPOS (it->position);
26497 glyph->object = it->object;
26498 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26499 glyph->ascent = glyph_ascent;
26500 glyph->descent = it->descent;
26501 glyph->voffset = it->voffset;
26502 glyph->type = IMAGE_GLYPH;
26503 glyph->avoid_cursor_p = it->avoid_cursor_p;
26504 glyph->multibyte_p = it->multibyte_p;
26505 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26507 /* In R2L rows, the left and the right box edges need to be
26508 drawn in reverse direction. */
26509 glyph->right_box_line_p = it->start_of_box_run_p;
26510 glyph->left_box_line_p = it->end_of_box_run_p;
26512 else
26514 glyph->left_box_line_p = it->start_of_box_run_p;
26515 glyph->right_box_line_p = it->end_of_box_run_p;
26517 glyph->overlaps_vertically_p = false;
26518 glyph->padding_p = false;
26519 glyph->glyph_not_available_p = false;
26520 glyph->face_id = it->face_id;
26521 glyph->u.img_id = img->id;
26522 glyph->slice.img = slice;
26523 glyph->font_type = FONT_TYPE_UNKNOWN;
26524 if (it->bidi_p)
26526 glyph->resolved_level = it->bidi_it.resolved_level;
26527 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26528 glyph->bidi_type = it->bidi_it.type;
26530 ++it->glyph_row->used[area];
26532 else
26533 IT_EXPAND_MATRIX_WIDTH (it, area);
26537 static void
26538 produce_xwidget_glyph (struct it *it)
26540 #ifdef HAVE_XWIDGETS
26541 struct xwidget *xw;
26542 int glyph_ascent, crop;
26543 eassert (it->what == IT_XWIDGET);
26545 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26546 /* Make sure X resources of the face is loaded. */
26547 prepare_face_for_display (it->f, face);
26549 xw = it->xwidget;
26550 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26551 it->descent = xw->height/2;
26552 it->phys_descent = it->descent;
26553 it->pixel_width = xw->width;
26554 /* It's quite possible for images to have an ascent greater than
26555 their height, so don't get confused in that case. */
26556 if (it->descent < 0)
26557 it->descent = 0;
26559 it->nglyphs = 1;
26561 if (face->box != FACE_NO_BOX)
26563 if (face->box_line_width > 0)
26565 it->ascent += face->box_line_width;
26566 it->descent += face->box_line_width;
26569 if (it->start_of_box_run_p)
26570 it->pixel_width += eabs (face->box_line_width);
26571 it->pixel_width += eabs (face->box_line_width);
26574 take_vertical_position_into_account (it);
26576 /* Automatically crop wide image glyphs at right edge so we can
26577 draw the cursor on same display row. */
26578 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26579 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26580 it->pixel_width -= crop;
26582 if (it->glyph_row)
26584 enum glyph_row_area area = it->area;
26585 struct glyph *glyph
26586 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26588 if (it->glyph_row->reversed_p)
26590 struct glyph *g;
26592 /* Make room for the new glyph. */
26593 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26594 g[1] = *g;
26595 glyph = it->glyph_row->glyphs[it->area];
26597 if (glyph < it->glyph_row->glyphs[area + 1])
26599 glyph->charpos = CHARPOS (it->position);
26600 glyph->object = it->object;
26601 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26602 glyph->ascent = glyph_ascent;
26603 glyph->descent = it->descent;
26604 glyph->voffset = it->voffset;
26605 glyph->type = XWIDGET_GLYPH;
26606 glyph->avoid_cursor_p = it->avoid_cursor_p;
26607 glyph->multibyte_p = it->multibyte_p;
26608 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26610 /* In R2L rows, the left and the right box edges need to be
26611 drawn in reverse direction. */
26612 glyph->right_box_line_p = it->start_of_box_run_p;
26613 glyph->left_box_line_p = it->end_of_box_run_p;
26615 else
26617 glyph->left_box_line_p = it->start_of_box_run_p;
26618 glyph->right_box_line_p = it->end_of_box_run_p;
26620 glyph->overlaps_vertically_p = 0;
26621 glyph->padding_p = 0;
26622 glyph->glyph_not_available_p = 0;
26623 glyph->face_id = it->face_id;
26624 glyph->u.xwidget = it->xwidget;
26625 glyph->font_type = FONT_TYPE_UNKNOWN;
26626 if (it->bidi_p)
26628 glyph->resolved_level = it->bidi_it.resolved_level;
26629 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26630 glyph->bidi_type = it->bidi_it.type;
26632 ++it->glyph_row->used[area];
26634 else
26635 IT_EXPAND_MATRIX_WIDTH (it, area);
26637 #endif
26640 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26641 of the glyph, WIDTH and HEIGHT are the width and height of the
26642 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26644 static void
26645 append_stretch_glyph (struct it *it, Lisp_Object object,
26646 int width, int height, int ascent)
26648 struct glyph *glyph;
26649 enum glyph_row_area area = it->area;
26651 eassert (ascent >= 0 && ascent <= height);
26653 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26654 if (glyph < it->glyph_row->glyphs[area + 1])
26656 /* If the glyph row is reversed, we need to prepend the glyph
26657 rather than append it. */
26658 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26660 struct glyph *g;
26662 /* Make room for the additional glyph. */
26663 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26664 g[1] = *g;
26665 glyph = it->glyph_row->glyphs[area];
26667 /* Decrease the width of the first glyph of the row that
26668 begins before first_visible_x (e.g., due to hscroll).
26669 This is so the overall width of the row becomes smaller
26670 by the scroll amount, and the stretch glyph appended by
26671 extend_face_to_end_of_line will be wider, to shift the
26672 row glyphs to the right. (In L2R rows, the corresponding
26673 left-shift effect is accomplished by setting row->x to a
26674 negative value, which won't work with R2L rows.)
26676 This must leave us with a positive value of WIDTH, since
26677 otherwise the call to move_it_in_display_line_to at the
26678 beginning of display_line would have got past the entire
26679 first glyph, and then it->current_x would have been
26680 greater or equal to it->first_visible_x. */
26681 if (it->current_x < it->first_visible_x)
26682 width -= it->first_visible_x - it->current_x;
26683 eassert (width > 0);
26685 glyph->charpos = CHARPOS (it->position);
26686 glyph->object = object;
26687 /* FIXME: It would be better to use TYPE_MAX here, but
26688 __typeof__ is not portable enough... */
26689 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26690 glyph->ascent = ascent;
26691 glyph->descent = height - ascent;
26692 glyph->voffset = it->voffset;
26693 glyph->type = STRETCH_GLYPH;
26694 glyph->avoid_cursor_p = it->avoid_cursor_p;
26695 glyph->multibyte_p = it->multibyte_p;
26696 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26698 /* In R2L rows, the left and the right box edges need to be
26699 drawn in reverse direction. */
26700 glyph->right_box_line_p = it->start_of_box_run_p;
26701 glyph->left_box_line_p = it->end_of_box_run_p;
26703 else
26705 glyph->left_box_line_p = it->start_of_box_run_p;
26706 glyph->right_box_line_p = it->end_of_box_run_p;
26708 glyph->overlaps_vertically_p = false;
26709 glyph->padding_p = false;
26710 glyph->glyph_not_available_p = false;
26711 glyph->face_id = it->face_id;
26712 glyph->u.stretch.ascent = ascent;
26713 glyph->u.stretch.height = height;
26714 glyph->slice.img = null_glyph_slice;
26715 glyph->font_type = FONT_TYPE_UNKNOWN;
26716 if (it->bidi_p)
26718 glyph->resolved_level = it->bidi_it.resolved_level;
26719 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26720 glyph->bidi_type = it->bidi_it.type;
26722 else
26724 glyph->resolved_level = 0;
26725 glyph->bidi_type = UNKNOWN_BT;
26727 ++it->glyph_row->used[area];
26729 else
26730 IT_EXPAND_MATRIX_WIDTH (it, area);
26733 #endif /* HAVE_WINDOW_SYSTEM */
26735 /* Produce a stretch glyph for iterator IT. IT->object is the value
26736 of the glyph property displayed. The value must be a list
26737 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26738 being recognized:
26740 1. `:width WIDTH' specifies that the space should be WIDTH *
26741 canonical char width wide. WIDTH may be an integer or floating
26742 point number.
26744 2. `:relative-width FACTOR' specifies that the width of the stretch
26745 should be computed from the width of the first character having the
26746 `glyph' property, and should be FACTOR times that width.
26748 3. `:align-to HPOS' specifies that the space should be wide enough
26749 to reach HPOS, a value in canonical character units.
26751 Exactly one of the above pairs must be present.
26753 4. `:height HEIGHT' specifies that the height of the stretch produced
26754 should be HEIGHT, measured in canonical character units.
26756 5. `:relative-height FACTOR' specifies that the height of the
26757 stretch should be FACTOR times the height of the characters having
26758 the glyph property.
26760 Either none or exactly one of 4 or 5 must be present.
26762 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26763 of the stretch should be used for the ascent of the stretch.
26764 ASCENT must be in the range 0 <= ASCENT <= 100. */
26766 void
26767 produce_stretch_glyph (struct it *it)
26769 /* (space :width WIDTH :height HEIGHT ...) */
26770 Lisp_Object prop, plist;
26771 int width = 0, height = 0, align_to = -1;
26772 bool zero_width_ok_p = false;
26773 double tem;
26774 struct font *font = NULL;
26776 #ifdef HAVE_WINDOW_SYSTEM
26777 int ascent = 0;
26778 bool zero_height_ok_p = false;
26780 if (FRAME_WINDOW_P (it->f))
26782 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26783 font = face->font ? face->font : FRAME_FONT (it->f);
26784 prepare_face_for_display (it->f, face);
26786 #endif
26788 /* List should start with `space'. */
26789 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26790 plist = XCDR (it->object);
26792 /* Compute the width of the stretch. */
26793 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26794 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26796 /* Absolute width `:width WIDTH' specified and valid. */
26797 zero_width_ok_p = true;
26798 width = (int)tem;
26800 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26802 /* Relative width `:relative-width FACTOR' specified and valid.
26803 Compute the width of the characters having the `glyph'
26804 property. */
26805 struct it it2;
26806 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26808 it2 = *it;
26809 if (it->multibyte_p)
26810 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26811 else
26813 it2.c = it2.char_to_display = *p, it2.len = 1;
26814 if (! ASCII_CHAR_P (it2.c))
26815 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26818 it2.glyph_row = NULL;
26819 it2.what = IT_CHARACTER;
26820 PRODUCE_GLYPHS (&it2);
26821 width = NUMVAL (prop) * it2.pixel_width;
26823 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26824 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26825 &align_to))
26827 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26828 align_to = (align_to < 0
26830 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26831 else if (align_to < 0)
26832 align_to = window_box_left_offset (it->w, TEXT_AREA);
26833 width = max (0, (int)tem + align_to - it->current_x);
26834 zero_width_ok_p = true;
26836 else
26837 /* Nothing specified -> width defaults to canonical char width. */
26838 width = FRAME_COLUMN_WIDTH (it->f);
26840 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26841 width = 1;
26843 #ifdef HAVE_WINDOW_SYSTEM
26844 /* Compute height. */
26845 if (FRAME_WINDOW_P (it->f))
26847 int default_height = normal_char_height (font, ' ');
26849 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26850 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26852 height = (int)tem;
26853 zero_height_ok_p = true;
26855 else if (prop = Fplist_get (plist, QCrelative_height),
26856 NUMVAL (prop) > 0)
26857 height = default_height * NUMVAL (prop);
26858 else
26859 height = default_height;
26861 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26862 height = 1;
26864 /* Compute percentage of height used for ascent. If
26865 `:ascent ASCENT' is present and valid, use that. Otherwise,
26866 derive the ascent from the font in use. */
26867 if (prop = Fplist_get (plist, QCascent),
26868 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26869 ascent = height * NUMVAL (prop) / 100.0;
26870 else if (!NILP (prop)
26871 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26872 ascent = min (max (0, (int)tem), height);
26873 else
26874 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26876 else
26877 #endif /* HAVE_WINDOW_SYSTEM */
26878 height = 1;
26880 if (width > 0 && it->line_wrap != TRUNCATE
26881 && it->current_x + width > it->last_visible_x)
26883 width = it->last_visible_x - it->current_x;
26884 #ifdef HAVE_WINDOW_SYSTEM
26885 /* Subtract one more pixel from the stretch width, but only on
26886 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26887 width -= FRAME_WINDOW_P (it->f);
26888 #endif
26891 if (width > 0 && height > 0 && it->glyph_row)
26893 Lisp_Object o_object = it->object;
26894 Lisp_Object object = it->stack[it->sp - 1].string;
26895 int n = width;
26897 if (!STRINGP (object))
26898 object = it->w->contents;
26899 #ifdef HAVE_WINDOW_SYSTEM
26900 if (FRAME_WINDOW_P (it->f))
26901 append_stretch_glyph (it, object, width, height, ascent);
26902 else
26903 #endif
26905 it->object = object;
26906 it->char_to_display = ' ';
26907 it->pixel_width = it->len = 1;
26908 while (n--)
26909 tty_append_glyph (it);
26910 it->object = o_object;
26914 it->pixel_width = width;
26915 #ifdef HAVE_WINDOW_SYSTEM
26916 if (FRAME_WINDOW_P (it->f))
26918 it->ascent = it->phys_ascent = ascent;
26919 it->descent = it->phys_descent = height - it->ascent;
26920 it->nglyphs = width > 0 && height > 0;
26921 take_vertical_position_into_account (it);
26923 else
26924 #endif
26925 it->nglyphs = width;
26928 /* Get information about special display element WHAT in an
26929 environment described by IT. WHAT is one of IT_TRUNCATION or
26930 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26931 non-null glyph_row member. This function ensures that fields like
26932 face_id, c, len of IT are left untouched. */
26934 static void
26935 produce_special_glyphs (struct it *it, enum display_element_type what)
26937 struct it temp_it;
26938 Lisp_Object gc;
26939 GLYPH glyph;
26941 temp_it = *it;
26942 temp_it.object = Qnil;
26943 memset (&temp_it.current, 0, sizeof temp_it.current);
26945 if (what == IT_CONTINUATION)
26947 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26948 if (it->bidi_it.paragraph_dir == R2L)
26949 SET_GLYPH_FROM_CHAR (glyph, '/');
26950 else
26951 SET_GLYPH_FROM_CHAR (glyph, '\\');
26952 if (it->dp
26953 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26955 /* FIXME: Should we mirror GC for R2L lines? */
26956 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26957 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26960 else if (what == IT_TRUNCATION)
26962 /* Truncation glyph. */
26963 SET_GLYPH_FROM_CHAR (glyph, '$');
26964 if (it->dp
26965 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26967 /* FIXME: Should we mirror GC for R2L lines? */
26968 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26969 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26972 else
26973 emacs_abort ();
26975 #ifdef HAVE_WINDOW_SYSTEM
26976 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26977 is turned off, we precede the truncation/continuation glyphs by a
26978 stretch glyph whose width is computed such that these special
26979 glyphs are aligned at the window margin, even when very different
26980 fonts are used in different glyph rows. */
26981 if (FRAME_WINDOW_P (temp_it.f)
26982 /* init_iterator calls this with it->glyph_row == NULL, and it
26983 wants only the pixel width of the truncation/continuation
26984 glyphs. */
26985 && temp_it.glyph_row
26986 /* insert_left_trunc_glyphs calls us at the beginning of the
26987 row, and it has its own calculation of the stretch glyph
26988 width. */
26989 && temp_it.glyph_row->used[TEXT_AREA] > 0
26990 && (temp_it.glyph_row->reversed_p
26991 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26992 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26994 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26996 if (stretch_width > 0)
26998 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26999 struct font *font =
27000 face->font ? face->font : FRAME_FONT (temp_it.f);
27001 int stretch_ascent =
27002 (((temp_it.ascent + temp_it.descent)
27003 * FONT_BASE (font)) / FONT_HEIGHT (font));
27005 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27006 temp_it.ascent + temp_it.descent,
27007 stretch_ascent);
27010 #endif
27012 temp_it.dp = NULL;
27013 temp_it.what = IT_CHARACTER;
27014 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27015 temp_it.face_id = GLYPH_FACE (glyph);
27016 temp_it.len = CHAR_BYTES (temp_it.c);
27018 PRODUCE_GLYPHS (&temp_it);
27019 it->pixel_width = temp_it.pixel_width;
27020 it->nglyphs = temp_it.nglyphs;
27023 #ifdef HAVE_WINDOW_SYSTEM
27025 /* Calculate line-height and line-spacing properties.
27026 An integer value specifies explicit pixel value.
27027 A float value specifies relative value to current face height.
27028 A cons (float . face-name) specifies relative value to
27029 height of specified face font.
27031 Returns height in pixels, or nil. */
27033 static Lisp_Object
27034 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27035 int boff, bool override)
27037 Lisp_Object face_name = Qnil;
27038 int ascent, descent, height;
27040 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27041 return val;
27043 if (CONSP (val))
27045 face_name = XCAR (val);
27046 val = XCDR (val);
27047 if (!NUMBERP (val))
27048 val = make_number (1);
27049 if (NILP (face_name))
27051 height = it->ascent + it->descent;
27052 goto scale;
27056 if (NILP (face_name))
27058 font = FRAME_FONT (it->f);
27059 boff = FRAME_BASELINE_OFFSET (it->f);
27061 else if (EQ (face_name, Qt))
27063 override = false;
27065 else
27067 int face_id;
27068 struct face *face;
27070 face_id = lookup_named_face (it->f, face_name, false);
27071 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27072 if (face == NULL || ((font = face->font) == NULL))
27073 return make_number (-1);
27074 boff = font->baseline_offset;
27075 if (font->vertical_centering)
27076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27079 normal_char_ascent_descent (font, -1, &ascent, &descent);
27081 if (override)
27083 it->override_ascent = ascent;
27084 it->override_descent = descent;
27085 it->override_boff = boff;
27088 height = ascent + descent;
27090 scale:
27091 if (FLOATP (val))
27092 height = (int)(XFLOAT_DATA (val) * height);
27093 else if (INTEGERP (val))
27094 height *= XINT (val);
27096 return make_number (height);
27100 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27101 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27102 and only if this is for a character for which no font was found.
27104 If the display method (it->glyphless_method) is
27105 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27106 length of the acronym or the hexadecimal string, UPPER_XOFF and
27107 UPPER_YOFF are pixel offsets for the upper part of the string,
27108 LOWER_XOFF and LOWER_YOFF are for the lower part.
27110 For the other display methods, LEN through LOWER_YOFF are zero. */
27112 static void
27113 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27114 short upper_xoff, short upper_yoff,
27115 short lower_xoff, short lower_yoff)
27117 struct glyph *glyph;
27118 enum glyph_row_area area = it->area;
27120 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27121 if (glyph < it->glyph_row->glyphs[area + 1])
27123 /* If the glyph row is reversed, we need to prepend the glyph
27124 rather than append it. */
27125 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27127 struct glyph *g;
27129 /* Make room for the additional glyph. */
27130 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27131 g[1] = *g;
27132 glyph = it->glyph_row->glyphs[area];
27134 glyph->charpos = CHARPOS (it->position);
27135 glyph->object = it->object;
27136 eassert (it->pixel_width <= SHRT_MAX);
27137 glyph->pixel_width = it->pixel_width;
27138 glyph->ascent = it->ascent;
27139 glyph->descent = it->descent;
27140 glyph->voffset = it->voffset;
27141 glyph->type = GLYPHLESS_GLYPH;
27142 glyph->u.glyphless.method = it->glyphless_method;
27143 glyph->u.glyphless.for_no_font = for_no_font;
27144 glyph->u.glyphless.len = len;
27145 glyph->u.glyphless.ch = it->c;
27146 glyph->slice.glyphless.upper_xoff = upper_xoff;
27147 glyph->slice.glyphless.upper_yoff = upper_yoff;
27148 glyph->slice.glyphless.lower_xoff = lower_xoff;
27149 glyph->slice.glyphless.lower_yoff = lower_yoff;
27150 glyph->avoid_cursor_p = it->avoid_cursor_p;
27151 glyph->multibyte_p = it->multibyte_p;
27152 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27154 /* In R2L rows, the left and the right box edges need to be
27155 drawn in reverse direction. */
27156 glyph->right_box_line_p = it->start_of_box_run_p;
27157 glyph->left_box_line_p = it->end_of_box_run_p;
27159 else
27161 glyph->left_box_line_p = it->start_of_box_run_p;
27162 glyph->right_box_line_p = it->end_of_box_run_p;
27164 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27165 || it->phys_descent > it->descent);
27166 glyph->padding_p = false;
27167 glyph->glyph_not_available_p = false;
27168 glyph->face_id = face_id;
27169 glyph->font_type = FONT_TYPE_UNKNOWN;
27170 if (it->bidi_p)
27172 glyph->resolved_level = it->bidi_it.resolved_level;
27173 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27174 glyph->bidi_type = it->bidi_it.type;
27176 ++it->glyph_row->used[area];
27178 else
27179 IT_EXPAND_MATRIX_WIDTH (it, area);
27183 /* Produce a glyph for a glyphless character for iterator IT.
27184 IT->glyphless_method specifies which method to use for displaying
27185 the character. See the description of enum
27186 glyphless_display_method in dispextern.h for the detail.
27188 FOR_NO_FONT is true if and only if this is for a character for
27189 which no font was found. ACRONYM, if non-nil, is an acronym string
27190 for the character. */
27192 static void
27193 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27195 int face_id;
27196 struct face *face;
27197 struct font *font;
27198 int base_width, base_height, width, height;
27199 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27200 int len;
27202 /* Get the metrics of the base font. We always refer to the current
27203 ASCII face. */
27204 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27205 font = face->font ? face->font : FRAME_FONT (it->f);
27206 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27207 it->ascent += font->baseline_offset;
27208 it->descent -= font->baseline_offset;
27209 base_height = it->ascent + it->descent;
27210 base_width = font->average_width;
27212 face_id = merge_glyphless_glyph_face (it);
27214 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27216 it->pixel_width = THIN_SPACE_WIDTH;
27217 len = 0;
27218 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27220 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27222 width = CHARACTER_WIDTH (it->c);
27223 if (width == 0)
27224 width = 1;
27225 else if (width > 4)
27226 width = 4;
27227 it->pixel_width = base_width * width;
27228 len = 0;
27229 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27231 else
27233 char buf[7];
27234 const char *str;
27235 unsigned int code[6];
27236 int upper_len;
27237 int ascent, descent;
27238 struct font_metrics metrics_upper, metrics_lower;
27240 face = FACE_FROM_ID (it->f, face_id);
27241 font = face->font ? face->font : FRAME_FONT (it->f);
27242 prepare_face_for_display (it->f, face);
27244 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27246 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27247 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27248 if (CONSP (acronym))
27249 acronym = XCAR (acronym);
27250 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27252 else
27254 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27255 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27256 str = buf;
27258 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27259 code[len] = font->driver->encode_char (font, str[len]);
27260 upper_len = (len + 1) / 2;
27261 font->driver->text_extents (font, code, upper_len,
27262 &metrics_upper);
27263 font->driver->text_extents (font, code + upper_len, len - upper_len,
27264 &metrics_lower);
27268 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27269 width = max (metrics_upper.width, metrics_lower.width) + 4;
27270 upper_xoff = upper_yoff = 2; /* the typical case */
27271 if (base_width >= width)
27273 /* Align the upper to the left, the lower to the right. */
27274 it->pixel_width = base_width;
27275 lower_xoff = base_width - 2 - metrics_lower.width;
27277 else
27279 /* Center the shorter one. */
27280 it->pixel_width = width;
27281 if (metrics_upper.width >= metrics_lower.width)
27282 lower_xoff = (width - metrics_lower.width) / 2;
27283 else
27285 /* FIXME: This code doesn't look right. It formerly was
27286 missing the "lower_xoff = 0;", which couldn't have
27287 been right since it left lower_xoff uninitialized. */
27288 lower_xoff = 0;
27289 upper_xoff = (width - metrics_upper.width) / 2;
27293 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27294 top, bottom, and between upper and lower strings. */
27295 height = (metrics_upper.ascent + metrics_upper.descent
27296 + metrics_lower.ascent + metrics_lower.descent) + 5;
27297 /* Center vertically.
27298 H:base_height, D:base_descent
27299 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27301 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27302 descent = D - H/2 + h/2;
27303 lower_yoff = descent - 2 - ld;
27304 upper_yoff = lower_yoff - la - 1 - ud; */
27305 ascent = - (it->descent - (base_height + height + 1) / 2);
27306 descent = it->descent - (base_height - height) / 2;
27307 lower_yoff = descent - 2 - metrics_lower.descent;
27308 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27309 - metrics_upper.descent);
27310 /* Don't make the height shorter than the base height. */
27311 if (height > base_height)
27313 it->ascent = ascent;
27314 it->descent = descent;
27318 it->phys_ascent = it->ascent;
27319 it->phys_descent = it->descent;
27320 if (it->glyph_row)
27321 append_glyphless_glyph (it, face_id, for_no_font, len,
27322 upper_xoff, upper_yoff,
27323 lower_xoff, lower_yoff);
27324 it->nglyphs = 1;
27325 take_vertical_position_into_account (it);
27329 /* RIF:
27330 Produce glyphs/get display metrics for the display element IT is
27331 loaded with. See the description of struct it in dispextern.h
27332 for an overview of struct it. */
27334 void
27335 x_produce_glyphs (struct it *it)
27337 int extra_line_spacing = it->extra_line_spacing;
27339 it->glyph_not_available_p = false;
27341 if (it->what == IT_CHARACTER)
27343 XChar2b char2b;
27344 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27345 struct font *font = face->font;
27346 struct font_metrics *pcm = NULL;
27347 int boff; /* Baseline offset. */
27349 if (font == NULL)
27351 /* When no suitable font is found, display this character by
27352 the method specified in the first extra slot of
27353 Vglyphless_char_display. */
27354 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27356 eassert (it->what == IT_GLYPHLESS);
27357 produce_glyphless_glyph (it, true,
27358 STRINGP (acronym) ? acronym : Qnil);
27359 goto done;
27362 boff = font->baseline_offset;
27363 if (font->vertical_centering)
27364 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27366 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27368 it->nglyphs = 1;
27370 if (it->override_ascent >= 0)
27372 it->ascent = it->override_ascent;
27373 it->descent = it->override_descent;
27374 boff = it->override_boff;
27376 else
27378 it->ascent = FONT_BASE (font) + boff;
27379 it->descent = FONT_DESCENT (font) - boff;
27382 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27384 pcm = get_per_char_metric (font, &char2b);
27385 if (pcm->width == 0
27386 && pcm->rbearing == 0 && pcm->lbearing == 0)
27387 pcm = NULL;
27390 if (pcm)
27392 it->phys_ascent = pcm->ascent + boff;
27393 it->phys_descent = pcm->descent - boff;
27394 it->pixel_width = pcm->width;
27395 /* Don't use font-global values for ascent and descent
27396 if they result in an exceedingly large line height. */
27397 if (it->override_ascent < 0)
27399 if (FONT_TOO_HIGH (font))
27401 it->ascent = it->phys_ascent;
27402 it->descent = it->phys_descent;
27403 /* These limitations are enforced by an
27404 assertion near the end of this function. */
27405 if (it->ascent < 0)
27406 it->ascent = 0;
27407 if (it->descent < 0)
27408 it->descent = 0;
27412 else
27414 it->glyph_not_available_p = true;
27415 it->phys_ascent = it->ascent;
27416 it->phys_descent = it->descent;
27417 it->pixel_width = font->space_width;
27420 if (it->constrain_row_ascent_descent_p)
27422 if (it->descent > it->max_descent)
27424 it->ascent += it->descent - it->max_descent;
27425 it->descent = it->max_descent;
27427 if (it->ascent > it->max_ascent)
27429 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27430 it->ascent = it->max_ascent;
27432 it->phys_ascent = min (it->phys_ascent, it->ascent);
27433 it->phys_descent = min (it->phys_descent, it->descent);
27434 extra_line_spacing = 0;
27437 /* If this is a space inside a region of text with
27438 `space-width' property, change its width. */
27439 bool stretched_p
27440 = it->char_to_display == ' ' && !NILP (it->space_width);
27441 if (stretched_p)
27442 it->pixel_width *= XFLOATINT (it->space_width);
27444 /* If face has a box, add the box thickness to the character
27445 height. If character has a box line to the left and/or
27446 right, add the box line width to the character's width. */
27447 if (face->box != FACE_NO_BOX)
27449 int thick = face->box_line_width;
27451 if (thick > 0)
27453 it->ascent += thick;
27454 it->descent += thick;
27456 else
27457 thick = -thick;
27459 if (it->start_of_box_run_p)
27460 it->pixel_width += thick;
27461 if (it->end_of_box_run_p)
27462 it->pixel_width += thick;
27465 /* If face has an overline, add the height of the overline
27466 (1 pixel) and a 1 pixel margin to the character height. */
27467 if (face->overline_p)
27468 it->ascent += overline_margin;
27470 if (it->constrain_row_ascent_descent_p)
27472 if (it->ascent > it->max_ascent)
27473 it->ascent = it->max_ascent;
27474 if (it->descent > it->max_descent)
27475 it->descent = it->max_descent;
27478 take_vertical_position_into_account (it);
27480 /* If we have to actually produce glyphs, do it. */
27481 if (it->glyph_row)
27483 if (stretched_p)
27485 /* Translate a space with a `space-width' property
27486 into a stretch glyph. */
27487 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27488 / FONT_HEIGHT (font));
27489 append_stretch_glyph (it, it->object, it->pixel_width,
27490 it->ascent + it->descent, ascent);
27492 else
27493 append_glyph (it);
27495 /* If characters with lbearing or rbearing are displayed
27496 in this line, record that fact in a flag of the
27497 glyph row. This is used to optimize X output code. */
27498 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27499 it->glyph_row->contains_overlapping_glyphs_p = true;
27501 if (! stretched_p && it->pixel_width == 0)
27502 /* We assure that all visible glyphs have at least 1-pixel
27503 width. */
27504 it->pixel_width = 1;
27506 else if (it->char_to_display == '\n')
27508 /* A newline has no width, but we need the height of the
27509 line. But if previous part of the line sets a height,
27510 don't increase that height. */
27512 Lisp_Object height;
27513 Lisp_Object total_height = Qnil;
27515 it->override_ascent = -1;
27516 it->pixel_width = 0;
27517 it->nglyphs = 0;
27519 height = get_it_property (it, Qline_height);
27520 /* Split (line-height total-height) list. */
27521 if (CONSP (height)
27522 && CONSP (XCDR (height))
27523 && NILP (XCDR (XCDR (height))))
27525 total_height = XCAR (XCDR (height));
27526 height = XCAR (height);
27528 height = calc_line_height_property (it, height, font, boff, true);
27530 if (it->override_ascent >= 0)
27532 it->ascent = it->override_ascent;
27533 it->descent = it->override_descent;
27534 boff = it->override_boff;
27536 else
27538 if (FONT_TOO_HIGH (font))
27540 it->ascent = font->pixel_size + boff - 1;
27541 it->descent = -boff + 1;
27542 if (it->descent < 0)
27543 it->descent = 0;
27545 else
27547 it->ascent = FONT_BASE (font) + boff;
27548 it->descent = FONT_DESCENT (font) - boff;
27552 if (EQ (height, Qt))
27554 if (it->descent > it->max_descent)
27556 it->ascent += it->descent - it->max_descent;
27557 it->descent = it->max_descent;
27559 if (it->ascent > it->max_ascent)
27561 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27562 it->ascent = it->max_ascent;
27564 it->phys_ascent = min (it->phys_ascent, it->ascent);
27565 it->phys_descent = min (it->phys_descent, it->descent);
27566 it->constrain_row_ascent_descent_p = true;
27567 extra_line_spacing = 0;
27569 else
27571 Lisp_Object spacing;
27573 it->phys_ascent = it->ascent;
27574 it->phys_descent = it->descent;
27576 if ((it->max_ascent > 0 || it->max_descent > 0)
27577 && face->box != FACE_NO_BOX
27578 && face->box_line_width > 0)
27580 it->ascent += face->box_line_width;
27581 it->descent += face->box_line_width;
27583 if (!NILP (height)
27584 && XINT (height) > it->ascent + it->descent)
27585 it->ascent = XINT (height) - it->descent;
27587 if (!NILP (total_height))
27588 spacing = calc_line_height_property (it, total_height, font,
27589 boff, false);
27590 else
27592 spacing = get_it_property (it, Qline_spacing);
27593 spacing = calc_line_height_property (it, spacing, font,
27594 boff, false);
27596 if (INTEGERP (spacing))
27598 extra_line_spacing = XINT (spacing);
27599 if (!NILP (total_height))
27600 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27604 else /* i.e. (it->char_to_display == '\t') */
27606 if (font->space_width > 0)
27608 int tab_width = it->tab_width * font->space_width;
27609 int x = it->current_x + it->continuation_lines_width;
27610 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27612 /* If the distance from the current position to the next tab
27613 stop is less than a space character width, use the
27614 tab stop after that. */
27615 if (next_tab_x - x < font->space_width)
27616 next_tab_x += tab_width;
27618 it->pixel_width = next_tab_x - x;
27619 it->nglyphs = 1;
27620 if (FONT_TOO_HIGH (font))
27622 if (get_char_glyph_code (' ', font, &char2b))
27624 pcm = get_per_char_metric (font, &char2b);
27625 if (pcm->width == 0
27626 && pcm->rbearing == 0 && pcm->lbearing == 0)
27627 pcm = NULL;
27630 if (pcm)
27632 it->ascent = pcm->ascent + boff;
27633 it->descent = pcm->descent - boff;
27635 else
27637 it->ascent = font->pixel_size + boff - 1;
27638 it->descent = -boff + 1;
27640 if (it->ascent < 0)
27641 it->ascent = 0;
27642 if (it->descent < 0)
27643 it->descent = 0;
27645 else
27647 it->ascent = FONT_BASE (font) + boff;
27648 it->descent = FONT_DESCENT (font) - boff;
27650 it->phys_ascent = it->ascent;
27651 it->phys_descent = it->descent;
27653 if (it->glyph_row)
27655 append_stretch_glyph (it, it->object, it->pixel_width,
27656 it->ascent + it->descent, it->ascent);
27659 else
27661 it->pixel_width = 0;
27662 it->nglyphs = 1;
27666 if (FONT_TOO_HIGH (font))
27668 int font_ascent, font_descent;
27670 /* For very large fonts, where we ignore the declared font
27671 dimensions, and go by per-character metrics instead,
27672 don't let the row ascent and descent values (and the row
27673 height computed from them) be smaller than the "normal"
27674 character metrics. This avoids unpleasant effects
27675 whereby lines on display would change their height
27676 depending on which characters are shown. */
27677 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27678 it->max_ascent = max (it->max_ascent, font_ascent);
27679 it->max_descent = max (it->max_descent, font_descent);
27682 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27684 /* A static composition.
27686 Note: A composition is represented as one glyph in the
27687 glyph matrix. There are no padding glyphs.
27689 Important note: pixel_width, ascent, and descent are the
27690 values of what is drawn by draw_glyphs (i.e. the values of
27691 the overall glyphs composed). */
27692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27693 int boff; /* baseline offset */
27694 struct composition *cmp = composition_table[it->cmp_it.id];
27695 int glyph_len = cmp->glyph_len;
27696 struct font *font = face->font;
27698 it->nglyphs = 1;
27700 /* If we have not yet calculated pixel size data of glyphs of
27701 the composition for the current face font, calculate them
27702 now. Theoretically, we have to check all fonts for the
27703 glyphs, but that requires much time and memory space. So,
27704 here we check only the font of the first glyph. This may
27705 lead to incorrect display, but it's very rare, and C-l
27706 (recenter-top-bottom) can correct the display anyway. */
27707 if (! cmp->font || cmp->font != font)
27709 /* Ascent and descent of the font of the first character
27710 of this composition (adjusted by baseline offset).
27711 Ascent and descent of overall glyphs should not be less
27712 than these, respectively. */
27713 int font_ascent, font_descent, font_height;
27714 /* Bounding box of the overall glyphs. */
27715 int leftmost, rightmost, lowest, highest;
27716 int lbearing, rbearing;
27717 int i, width, ascent, descent;
27718 int c;
27719 XChar2b char2b;
27720 struct font_metrics *pcm;
27721 ptrdiff_t pos;
27723 eassume (0 < glyph_len); /* See Bug#8512. */
27725 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27726 while (c == '\t' && 0 < --glyph_len);
27728 bool right_padded = glyph_len < cmp->glyph_len;
27729 for (i = 0; i < glyph_len; i++)
27731 c = COMPOSITION_GLYPH (cmp, i);
27732 if (c != '\t')
27733 break;
27734 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27736 bool left_padded = i > 0;
27738 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27739 : IT_CHARPOS (*it));
27740 /* If no suitable font is found, use the default font. */
27741 bool font_not_found_p = font == NULL;
27742 if (font_not_found_p)
27744 face = face->ascii_face;
27745 font = face->font;
27747 boff = font->baseline_offset;
27748 if (font->vertical_centering)
27749 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27750 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27751 font_ascent += boff;
27752 font_descent -= boff;
27753 font_height = font_ascent + font_descent;
27755 cmp->font = font;
27757 pcm = NULL;
27758 if (! font_not_found_p)
27760 get_char_face_and_encoding (it->f, c, it->face_id,
27761 &char2b, false);
27762 pcm = get_per_char_metric (font, &char2b);
27765 /* Initialize the bounding box. */
27766 if (pcm)
27768 width = cmp->glyph_len > 0 ? pcm->width : 0;
27769 ascent = pcm->ascent;
27770 descent = pcm->descent;
27771 lbearing = pcm->lbearing;
27772 rbearing = pcm->rbearing;
27774 else
27776 width = cmp->glyph_len > 0 ? font->space_width : 0;
27777 ascent = FONT_BASE (font);
27778 descent = FONT_DESCENT (font);
27779 lbearing = 0;
27780 rbearing = width;
27783 rightmost = width;
27784 leftmost = 0;
27785 lowest = - descent + boff;
27786 highest = ascent + boff;
27788 if (! font_not_found_p
27789 && font->default_ascent
27790 && CHAR_TABLE_P (Vuse_default_ascent)
27791 && !NILP (Faref (Vuse_default_ascent,
27792 make_number (it->char_to_display))))
27793 highest = font->default_ascent + boff;
27795 /* Draw the first glyph at the normal position. It may be
27796 shifted to right later if some other glyphs are drawn
27797 at the left. */
27798 cmp->offsets[i * 2] = 0;
27799 cmp->offsets[i * 2 + 1] = boff;
27800 cmp->lbearing = lbearing;
27801 cmp->rbearing = rbearing;
27803 /* Set cmp->offsets for the remaining glyphs. */
27804 for (i++; i < glyph_len; i++)
27806 int left, right, btm, top;
27807 int ch = COMPOSITION_GLYPH (cmp, i);
27808 int face_id;
27809 struct face *this_face;
27811 if (ch == '\t')
27812 ch = ' ';
27813 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27814 this_face = FACE_FROM_ID (it->f, face_id);
27815 font = this_face->font;
27817 if (font == NULL)
27818 pcm = NULL;
27819 else
27821 get_char_face_and_encoding (it->f, ch, face_id,
27822 &char2b, false);
27823 pcm = get_per_char_metric (font, &char2b);
27825 if (! pcm)
27826 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27827 else
27829 width = pcm->width;
27830 ascent = pcm->ascent;
27831 descent = pcm->descent;
27832 lbearing = pcm->lbearing;
27833 rbearing = pcm->rbearing;
27834 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27836 /* Relative composition with or without
27837 alternate chars. */
27838 left = (leftmost + rightmost - width) / 2;
27839 btm = - descent + boff;
27840 if (font->relative_compose
27841 && (! CHAR_TABLE_P (Vignore_relative_composition)
27842 || NILP (Faref (Vignore_relative_composition,
27843 make_number (ch)))))
27846 if (- descent >= font->relative_compose)
27847 /* One extra pixel between two glyphs. */
27848 btm = highest + 1;
27849 else if (ascent <= 0)
27850 /* One extra pixel between two glyphs. */
27851 btm = lowest - 1 - ascent - descent;
27854 else
27856 /* A composition rule is specified by an integer
27857 value that encodes global and new reference
27858 points (GREF and NREF). GREF and NREF are
27859 specified by numbers as below:
27861 0---1---2 -- ascent
27865 9--10--11 -- center
27867 ---3---4---5--- baseline
27869 6---7---8 -- descent
27871 int rule = COMPOSITION_RULE (cmp, i);
27872 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27874 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27875 grefx = gref % 3, nrefx = nref % 3;
27876 grefy = gref / 3, nrefy = nref / 3;
27877 if (xoff)
27878 xoff = font_height * (xoff - 128) / 256;
27879 if (yoff)
27880 yoff = font_height * (yoff - 128) / 256;
27882 left = (leftmost
27883 + grefx * (rightmost - leftmost) / 2
27884 - nrefx * width / 2
27885 + xoff);
27887 btm = ((grefy == 0 ? highest
27888 : grefy == 1 ? 0
27889 : grefy == 2 ? lowest
27890 : (highest + lowest) / 2)
27891 - (nrefy == 0 ? ascent + descent
27892 : nrefy == 1 ? descent - boff
27893 : nrefy == 2 ? 0
27894 : (ascent + descent) / 2)
27895 + yoff);
27898 cmp->offsets[i * 2] = left;
27899 cmp->offsets[i * 2 + 1] = btm + descent;
27901 /* Update the bounding box of the overall glyphs. */
27902 if (width > 0)
27904 right = left + width;
27905 if (left < leftmost)
27906 leftmost = left;
27907 if (right > rightmost)
27908 rightmost = right;
27910 top = btm + descent + ascent;
27911 if (top > highest)
27912 highest = top;
27913 if (btm < lowest)
27914 lowest = btm;
27916 if (cmp->lbearing > left + lbearing)
27917 cmp->lbearing = left + lbearing;
27918 if (cmp->rbearing < left + rbearing)
27919 cmp->rbearing = left + rbearing;
27923 /* If there are glyphs whose x-offsets are negative,
27924 shift all glyphs to the right and make all x-offsets
27925 non-negative. */
27926 if (leftmost < 0)
27928 for (i = 0; i < cmp->glyph_len; i++)
27929 cmp->offsets[i * 2] -= leftmost;
27930 rightmost -= leftmost;
27931 cmp->lbearing -= leftmost;
27932 cmp->rbearing -= leftmost;
27935 if (left_padded && cmp->lbearing < 0)
27937 for (i = 0; i < cmp->glyph_len; i++)
27938 cmp->offsets[i * 2] -= cmp->lbearing;
27939 rightmost -= cmp->lbearing;
27940 cmp->rbearing -= cmp->lbearing;
27941 cmp->lbearing = 0;
27943 if (right_padded && rightmost < cmp->rbearing)
27945 rightmost = cmp->rbearing;
27948 cmp->pixel_width = rightmost;
27949 cmp->ascent = highest;
27950 cmp->descent = - lowest;
27951 if (cmp->ascent < font_ascent)
27952 cmp->ascent = font_ascent;
27953 if (cmp->descent < font_descent)
27954 cmp->descent = font_descent;
27957 if (it->glyph_row
27958 && (cmp->lbearing < 0
27959 || cmp->rbearing > cmp->pixel_width))
27960 it->glyph_row->contains_overlapping_glyphs_p = true;
27962 it->pixel_width = cmp->pixel_width;
27963 it->ascent = it->phys_ascent = cmp->ascent;
27964 it->descent = it->phys_descent = cmp->descent;
27965 if (face->box != FACE_NO_BOX)
27967 int thick = face->box_line_width;
27969 if (thick > 0)
27971 it->ascent += thick;
27972 it->descent += thick;
27974 else
27975 thick = - thick;
27977 if (it->start_of_box_run_p)
27978 it->pixel_width += thick;
27979 if (it->end_of_box_run_p)
27980 it->pixel_width += thick;
27983 /* If face has an overline, add the height of the overline
27984 (1 pixel) and a 1 pixel margin to the character height. */
27985 if (face->overline_p)
27986 it->ascent += overline_margin;
27988 take_vertical_position_into_account (it);
27989 if (it->ascent < 0)
27990 it->ascent = 0;
27991 if (it->descent < 0)
27992 it->descent = 0;
27994 if (it->glyph_row && cmp->glyph_len > 0)
27995 append_composite_glyph (it);
27997 else if (it->what == IT_COMPOSITION)
27999 /* A dynamic (automatic) composition. */
28000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28001 Lisp_Object gstring;
28002 struct font_metrics metrics;
28004 it->nglyphs = 1;
28006 gstring = composition_gstring_from_id (it->cmp_it.id);
28007 it->pixel_width
28008 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28009 &metrics);
28010 if (it->glyph_row
28011 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28012 it->glyph_row->contains_overlapping_glyphs_p = true;
28013 it->ascent = it->phys_ascent = metrics.ascent;
28014 it->descent = it->phys_descent = metrics.descent;
28015 if (face->box != FACE_NO_BOX)
28017 int thick = face->box_line_width;
28019 if (thick > 0)
28021 it->ascent += thick;
28022 it->descent += thick;
28024 else
28025 thick = - thick;
28027 if (it->start_of_box_run_p)
28028 it->pixel_width += thick;
28029 if (it->end_of_box_run_p)
28030 it->pixel_width += thick;
28032 /* If face has an overline, add the height of the overline
28033 (1 pixel) and a 1 pixel margin to the character height. */
28034 if (face->overline_p)
28035 it->ascent += overline_margin;
28036 take_vertical_position_into_account (it);
28037 if (it->ascent < 0)
28038 it->ascent = 0;
28039 if (it->descent < 0)
28040 it->descent = 0;
28042 if (it->glyph_row)
28043 append_composite_glyph (it);
28045 else if (it->what == IT_GLYPHLESS)
28046 produce_glyphless_glyph (it, false, Qnil);
28047 else if (it->what == IT_IMAGE)
28048 produce_image_glyph (it);
28049 else if (it->what == IT_STRETCH)
28050 produce_stretch_glyph (it);
28051 else if (it->what == IT_XWIDGET)
28052 produce_xwidget_glyph (it);
28054 done:
28055 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28056 because this isn't true for images with `:ascent 100'. */
28057 eassert (it->ascent >= 0 && it->descent >= 0);
28058 if (it->area == TEXT_AREA)
28059 it->current_x += it->pixel_width;
28061 if (extra_line_spacing > 0)
28063 it->descent += extra_line_spacing;
28064 if (extra_line_spacing > it->max_extra_line_spacing)
28065 it->max_extra_line_spacing = extra_line_spacing;
28068 it->max_ascent = max (it->max_ascent, it->ascent);
28069 it->max_descent = max (it->max_descent, it->descent);
28070 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28071 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28074 /* EXPORT for RIF:
28075 Output LEN glyphs starting at START at the nominal cursor position.
28076 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28077 being updated, and UPDATED_AREA is the area of that row being updated. */
28079 void
28080 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28081 struct glyph *start, enum glyph_row_area updated_area, int len)
28083 int x, hpos, chpos = w->phys_cursor.hpos;
28085 eassert (updated_row);
28086 /* When the window is hscrolled, cursor hpos can legitimately be out
28087 of bounds, but we draw the cursor at the corresponding window
28088 margin in that case. */
28089 if (!updated_row->reversed_p && chpos < 0)
28090 chpos = 0;
28091 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28092 chpos = updated_row->used[TEXT_AREA] - 1;
28094 block_input ();
28096 /* Write glyphs. */
28098 hpos = start - updated_row->glyphs[updated_area];
28099 x = draw_glyphs (w, w->output_cursor.x,
28100 updated_row, updated_area,
28101 hpos, hpos + len,
28102 DRAW_NORMAL_TEXT, 0);
28104 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28105 if (updated_area == TEXT_AREA
28106 && w->phys_cursor_on_p
28107 && w->phys_cursor.vpos == w->output_cursor.vpos
28108 && chpos >= hpos
28109 && chpos < hpos + len)
28110 w->phys_cursor_on_p = false;
28112 unblock_input ();
28114 /* Advance the output cursor. */
28115 w->output_cursor.hpos += len;
28116 w->output_cursor.x = x;
28120 /* EXPORT for RIF:
28121 Insert LEN glyphs from START at the nominal cursor position. */
28123 void
28124 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28125 struct glyph *start, enum glyph_row_area updated_area, int len)
28127 struct frame *f;
28128 int line_height, shift_by_width, shifted_region_width;
28129 struct glyph_row *row;
28130 struct glyph *glyph;
28131 int frame_x, frame_y;
28132 ptrdiff_t hpos;
28134 eassert (updated_row);
28135 block_input ();
28136 f = XFRAME (WINDOW_FRAME (w));
28138 /* Get the height of the line we are in. */
28139 row = updated_row;
28140 line_height = row->height;
28142 /* Get the width of the glyphs to insert. */
28143 shift_by_width = 0;
28144 for (glyph = start; glyph < start + len; ++glyph)
28145 shift_by_width += glyph->pixel_width;
28147 /* Get the width of the region to shift right. */
28148 shifted_region_width = (window_box_width (w, updated_area)
28149 - w->output_cursor.x
28150 - shift_by_width);
28152 /* Shift right. */
28153 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28154 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28156 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28157 line_height, shift_by_width);
28159 /* Write the glyphs. */
28160 hpos = start - row->glyphs[updated_area];
28161 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28162 hpos, hpos + len,
28163 DRAW_NORMAL_TEXT, 0);
28165 /* Advance the output cursor. */
28166 w->output_cursor.hpos += len;
28167 w->output_cursor.x += shift_by_width;
28168 unblock_input ();
28172 /* EXPORT for RIF:
28173 Erase the current text line from the nominal cursor position
28174 (inclusive) to pixel column TO_X (exclusive). The idea is that
28175 everything from TO_X onward is already erased.
28177 TO_X is a pixel position relative to UPDATED_AREA of currently
28178 updated window W. TO_X == -1 means clear to the end of this area. */
28180 void
28181 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28182 enum glyph_row_area updated_area, int to_x)
28184 struct frame *f;
28185 int max_x, min_y, max_y;
28186 int from_x, from_y, to_y;
28188 eassert (updated_row);
28189 f = XFRAME (w->frame);
28191 if (updated_row->full_width_p)
28192 max_x = (WINDOW_PIXEL_WIDTH (w)
28193 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28194 else
28195 max_x = window_box_width (w, updated_area);
28196 max_y = window_text_bottom_y (w);
28198 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28199 of window. For TO_X > 0, truncate to end of drawing area. */
28200 if (to_x == 0)
28201 return;
28202 else if (to_x < 0)
28203 to_x = max_x;
28204 else
28205 to_x = min (to_x, max_x);
28207 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28209 /* Notice if the cursor will be cleared by this operation. */
28210 if (!updated_row->full_width_p)
28211 notice_overwritten_cursor (w, updated_area,
28212 w->output_cursor.x, -1,
28213 updated_row->y,
28214 MATRIX_ROW_BOTTOM_Y (updated_row));
28216 from_x = w->output_cursor.x;
28218 /* Translate to frame coordinates. */
28219 if (updated_row->full_width_p)
28221 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28222 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28224 else
28226 int area_left = window_box_left (w, updated_area);
28227 from_x += area_left;
28228 to_x += area_left;
28231 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28232 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28233 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28235 /* Prevent inadvertently clearing to end of the X window. */
28236 if (to_x > from_x && to_y > from_y)
28238 block_input ();
28239 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28240 to_x - from_x, to_y - from_y);
28241 unblock_input ();
28245 #endif /* HAVE_WINDOW_SYSTEM */
28249 /***********************************************************************
28250 Cursor types
28251 ***********************************************************************/
28253 /* Value is the internal representation of the specified cursor type
28254 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28255 of the bar cursor. */
28257 static enum text_cursor_kinds
28258 get_specified_cursor_type (Lisp_Object arg, int *width)
28260 enum text_cursor_kinds type;
28262 if (NILP (arg))
28263 return NO_CURSOR;
28265 if (EQ (arg, Qbox))
28266 return FILLED_BOX_CURSOR;
28268 if (EQ (arg, Qhollow))
28269 return HOLLOW_BOX_CURSOR;
28271 if (EQ (arg, Qbar))
28273 *width = 2;
28274 return BAR_CURSOR;
28277 if (CONSP (arg)
28278 && EQ (XCAR (arg), Qbar)
28279 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28281 *width = XINT (XCDR (arg));
28282 return BAR_CURSOR;
28285 if (EQ (arg, Qhbar))
28287 *width = 2;
28288 return HBAR_CURSOR;
28291 if (CONSP (arg)
28292 && EQ (XCAR (arg), Qhbar)
28293 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28295 *width = XINT (XCDR (arg));
28296 return HBAR_CURSOR;
28299 /* Treat anything unknown as "hollow box cursor".
28300 It was bad to signal an error; people have trouble fixing
28301 .Xdefaults with Emacs, when it has something bad in it. */
28302 type = HOLLOW_BOX_CURSOR;
28304 return type;
28307 /* Set the default cursor types for specified frame. */
28308 void
28309 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28311 int width = 1;
28312 Lisp_Object tem;
28314 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28315 FRAME_CURSOR_WIDTH (f) = width;
28317 /* By default, set up the blink-off state depending on the on-state. */
28319 tem = Fassoc (arg, Vblink_cursor_alist);
28320 if (!NILP (tem))
28322 FRAME_BLINK_OFF_CURSOR (f)
28323 = get_specified_cursor_type (XCDR (tem), &width);
28324 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28326 else
28327 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28329 /* Make sure the cursor gets redrawn. */
28330 f->cursor_type_changed = true;
28334 #ifdef HAVE_WINDOW_SYSTEM
28336 /* Return the cursor we want to be displayed in window W. Return
28337 width of bar/hbar cursor through WIDTH arg. Return with
28338 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28339 (i.e. if the `system caret' should track this cursor).
28341 In a mini-buffer window, we want the cursor only to appear if we
28342 are reading input from this window. For the selected window, we
28343 want the cursor type given by the frame parameter or buffer local
28344 setting of cursor-type. If explicitly marked off, draw no cursor.
28345 In all other cases, we want a hollow box cursor. */
28347 static enum text_cursor_kinds
28348 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28349 bool *active_cursor)
28351 struct frame *f = XFRAME (w->frame);
28352 struct buffer *b = XBUFFER (w->contents);
28353 int cursor_type = DEFAULT_CURSOR;
28354 Lisp_Object alt_cursor;
28355 bool non_selected = false;
28357 *active_cursor = true;
28359 /* Echo area */
28360 if (cursor_in_echo_area
28361 && FRAME_HAS_MINIBUF_P (f)
28362 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28364 if (w == XWINDOW (echo_area_window))
28366 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28368 *width = FRAME_CURSOR_WIDTH (f);
28369 return FRAME_DESIRED_CURSOR (f);
28371 else
28372 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28375 *active_cursor = false;
28376 non_selected = true;
28379 /* Detect a nonselected window or nonselected frame. */
28380 else if (w != XWINDOW (f->selected_window)
28381 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28383 *active_cursor = false;
28385 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28386 return NO_CURSOR;
28388 non_selected = true;
28391 /* Never display a cursor in a window in which cursor-type is nil. */
28392 if (NILP (BVAR (b, cursor_type)))
28393 return NO_CURSOR;
28395 /* Get the normal cursor type for this window. */
28396 if (EQ (BVAR (b, cursor_type), Qt))
28398 cursor_type = FRAME_DESIRED_CURSOR (f);
28399 *width = FRAME_CURSOR_WIDTH (f);
28401 else
28402 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28404 /* Use cursor-in-non-selected-windows instead
28405 for non-selected window or frame. */
28406 if (non_selected)
28408 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28409 if (!EQ (Qt, alt_cursor))
28410 return get_specified_cursor_type (alt_cursor, width);
28411 /* t means modify the normal cursor type. */
28412 if (cursor_type == FILLED_BOX_CURSOR)
28413 cursor_type = HOLLOW_BOX_CURSOR;
28414 else if (cursor_type == BAR_CURSOR && *width > 1)
28415 --*width;
28416 return cursor_type;
28419 /* Use normal cursor if not blinked off. */
28420 if (!w->cursor_off_p)
28422 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28423 return NO_CURSOR;
28424 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28426 if (cursor_type == FILLED_BOX_CURSOR)
28428 /* Using a block cursor on large images can be very annoying.
28429 So use a hollow cursor for "large" images.
28430 If image is not transparent (no mask), also use hollow cursor. */
28431 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28432 if (img != NULL && IMAGEP (img->spec))
28434 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28435 where N = size of default frame font size.
28436 This should cover most of the "tiny" icons people may use. */
28437 if (!img->mask
28438 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28439 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28440 cursor_type = HOLLOW_BOX_CURSOR;
28443 else if (cursor_type != NO_CURSOR)
28445 /* Display current only supports BOX and HOLLOW cursors for images.
28446 So for now, unconditionally use a HOLLOW cursor when cursor is
28447 not a solid box cursor. */
28448 cursor_type = HOLLOW_BOX_CURSOR;
28451 return cursor_type;
28454 /* Cursor is blinked off, so determine how to "toggle" it. */
28456 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28457 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28458 return get_specified_cursor_type (XCDR (alt_cursor), width);
28460 /* Then see if frame has specified a specific blink off cursor type. */
28461 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28463 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28464 return FRAME_BLINK_OFF_CURSOR (f);
28467 #if false
28468 /* Some people liked having a permanently visible blinking cursor,
28469 while others had very strong opinions against it. So it was
28470 decided to remove it. KFS 2003-09-03 */
28472 /* Finally perform built-in cursor blinking:
28473 filled box <-> hollow box
28474 wide [h]bar <-> narrow [h]bar
28475 narrow [h]bar <-> no cursor
28476 other type <-> no cursor */
28478 if (cursor_type == FILLED_BOX_CURSOR)
28479 return HOLLOW_BOX_CURSOR;
28481 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28483 *width = 1;
28484 return cursor_type;
28486 #endif
28488 return NO_CURSOR;
28492 /* Notice when the text cursor of window W has been completely
28493 overwritten by a drawing operation that outputs glyphs in AREA
28494 starting at X0 and ending at X1 in the line starting at Y0 and
28495 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28496 the rest of the line after X0 has been written. Y coordinates
28497 are window-relative. */
28499 static void
28500 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28501 int x0, int x1, int y0, int y1)
28503 int cx0, cx1, cy0, cy1;
28504 struct glyph_row *row;
28506 if (!w->phys_cursor_on_p)
28507 return;
28508 if (area != TEXT_AREA)
28509 return;
28511 if (w->phys_cursor.vpos < 0
28512 || w->phys_cursor.vpos >= w->current_matrix->nrows
28513 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28514 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28515 return;
28517 if (row->cursor_in_fringe_p)
28519 row->cursor_in_fringe_p = false;
28520 draw_fringe_bitmap (w, row, row->reversed_p);
28521 w->phys_cursor_on_p = false;
28522 return;
28525 cx0 = w->phys_cursor.x;
28526 cx1 = cx0 + w->phys_cursor_width;
28527 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28528 return;
28530 /* The cursor image will be completely removed from the
28531 screen if the output area intersects the cursor area in
28532 y-direction. When we draw in [y0 y1[, and some part of
28533 the cursor is at y < y0, that part must have been drawn
28534 before. When scrolling, the cursor is erased before
28535 actually scrolling, so we don't come here. When not
28536 scrolling, the rows above the old cursor row must have
28537 changed, and in this case these rows must have written
28538 over the cursor image.
28540 Likewise if part of the cursor is below y1, with the
28541 exception of the cursor being in the first blank row at
28542 the buffer and window end because update_text_area
28543 doesn't draw that row. (Except when it does, but
28544 that's handled in update_text_area.) */
28546 cy0 = w->phys_cursor.y;
28547 cy1 = cy0 + w->phys_cursor_height;
28548 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28549 return;
28551 w->phys_cursor_on_p = false;
28554 #endif /* HAVE_WINDOW_SYSTEM */
28557 /************************************************************************
28558 Mouse Face
28559 ************************************************************************/
28561 #ifdef HAVE_WINDOW_SYSTEM
28563 /* EXPORT for RIF:
28564 Fix the display of area AREA of overlapping row ROW in window W
28565 with respect to the overlapping part OVERLAPS. */
28567 void
28568 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28569 enum glyph_row_area area, int overlaps)
28571 int i, x;
28573 block_input ();
28575 x = 0;
28576 for (i = 0; i < row->used[area];)
28578 if (row->glyphs[area][i].overlaps_vertically_p)
28580 int start = i, start_x = x;
28584 x += row->glyphs[area][i].pixel_width;
28585 ++i;
28587 while (i < row->used[area]
28588 && row->glyphs[area][i].overlaps_vertically_p);
28590 draw_glyphs (w, start_x, row, area,
28591 start, i,
28592 DRAW_NORMAL_TEXT, overlaps);
28594 else
28596 x += row->glyphs[area][i].pixel_width;
28597 ++i;
28601 unblock_input ();
28605 /* EXPORT:
28606 Draw the cursor glyph of window W in glyph row ROW. See the
28607 comment of draw_glyphs for the meaning of HL. */
28609 void
28610 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28611 enum draw_glyphs_face hl)
28613 /* If cursor hpos is out of bounds, don't draw garbage. This can
28614 happen in mini-buffer windows when switching between echo area
28615 glyphs and mini-buffer. */
28616 if ((row->reversed_p
28617 ? (w->phys_cursor.hpos >= 0)
28618 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28620 bool on_p = w->phys_cursor_on_p;
28621 int x1;
28622 int hpos = w->phys_cursor.hpos;
28624 /* When the window is hscrolled, cursor hpos can legitimately be
28625 out of bounds, but we draw the cursor at the corresponding
28626 window margin in that case. */
28627 if (!row->reversed_p && hpos < 0)
28628 hpos = 0;
28629 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28630 hpos = row->used[TEXT_AREA] - 1;
28632 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28633 hl, 0);
28634 w->phys_cursor_on_p = on_p;
28636 if (hl == DRAW_CURSOR)
28637 w->phys_cursor_width = x1 - w->phys_cursor.x;
28638 /* When we erase the cursor, and ROW is overlapped by other
28639 rows, make sure that these overlapping parts of other rows
28640 are redrawn. */
28641 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28643 w->phys_cursor_width = x1 - w->phys_cursor.x;
28645 if (row > w->current_matrix->rows
28646 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28647 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28648 OVERLAPS_ERASED_CURSOR);
28650 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28651 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28652 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28653 OVERLAPS_ERASED_CURSOR);
28659 /* Erase the image of a cursor of window W from the screen. */
28661 void
28662 erase_phys_cursor (struct window *w)
28664 struct frame *f = XFRAME (w->frame);
28665 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28666 int hpos = w->phys_cursor.hpos;
28667 int vpos = w->phys_cursor.vpos;
28668 bool mouse_face_here_p = false;
28669 struct glyph_matrix *active_glyphs = w->current_matrix;
28670 struct glyph_row *cursor_row;
28671 struct glyph *cursor_glyph;
28672 enum draw_glyphs_face hl;
28674 /* No cursor displayed or row invalidated => nothing to do on the
28675 screen. */
28676 if (w->phys_cursor_type == NO_CURSOR)
28677 goto mark_cursor_off;
28679 /* VPOS >= active_glyphs->nrows means that window has been resized.
28680 Don't bother to erase the cursor. */
28681 if (vpos >= active_glyphs->nrows)
28682 goto mark_cursor_off;
28684 /* If row containing cursor is marked invalid, there is nothing we
28685 can do. */
28686 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28687 if (!cursor_row->enabled_p)
28688 goto mark_cursor_off;
28690 /* If line spacing is > 0, old cursor may only be partially visible in
28691 window after split-window. So adjust visible height. */
28692 cursor_row->visible_height = min (cursor_row->visible_height,
28693 window_text_bottom_y (w) - cursor_row->y);
28695 /* If row is completely invisible, don't attempt to delete a cursor which
28696 isn't there. This can happen if cursor is at top of a window, and
28697 we switch to a buffer with a header line in that window. */
28698 if (cursor_row->visible_height <= 0)
28699 goto mark_cursor_off;
28701 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28702 if (cursor_row->cursor_in_fringe_p)
28704 cursor_row->cursor_in_fringe_p = false;
28705 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28706 goto mark_cursor_off;
28709 /* This can happen when the new row is shorter than the old one.
28710 In this case, either draw_glyphs or clear_end_of_line
28711 should have cleared the cursor. Note that we wouldn't be
28712 able to erase the cursor in this case because we don't have a
28713 cursor glyph at hand. */
28714 if ((cursor_row->reversed_p
28715 ? (w->phys_cursor.hpos < 0)
28716 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28717 goto mark_cursor_off;
28719 /* When the window is hscrolled, cursor hpos can legitimately be out
28720 of bounds, but we draw the cursor at the corresponding window
28721 margin in that case. */
28722 if (!cursor_row->reversed_p && hpos < 0)
28723 hpos = 0;
28724 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28725 hpos = cursor_row->used[TEXT_AREA] - 1;
28727 /* If the cursor is in the mouse face area, redisplay that when
28728 we clear the cursor. */
28729 if (! NILP (hlinfo->mouse_face_window)
28730 && coords_in_mouse_face_p (w, hpos, vpos)
28731 /* Don't redraw the cursor's spot in mouse face if it is at the
28732 end of a line (on a newline). The cursor appears there, but
28733 mouse highlighting does not. */
28734 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28735 mouse_face_here_p = true;
28737 /* Maybe clear the display under the cursor. */
28738 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28740 int x, y;
28741 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28742 int width;
28744 cursor_glyph = get_phys_cursor_glyph (w);
28745 if (cursor_glyph == NULL)
28746 goto mark_cursor_off;
28748 width = cursor_glyph->pixel_width;
28749 x = w->phys_cursor.x;
28750 if (x < 0)
28752 width += x;
28753 x = 0;
28755 width = min (width, window_box_width (w, TEXT_AREA) - x);
28756 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28757 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28759 if (width > 0)
28760 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28763 /* Erase the cursor by redrawing the character underneath it. */
28764 if (mouse_face_here_p)
28765 hl = DRAW_MOUSE_FACE;
28766 else
28767 hl = DRAW_NORMAL_TEXT;
28768 draw_phys_cursor_glyph (w, cursor_row, hl);
28770 mark_cursor_off:
28771 w->phys_cursor_on_p = false;
28772 w->phys_cursor_type = NO_CURSOR;
28776 /* Display or clear cursor of window W. If !ON, clear the cursor.
28777 If ON, display the cursor; where to put the cursor is specified by
28778 HPOS, VPOS, X and Y. */
28780 void
28781 display_and_set_cursor (struct window *w, bool on,
28782 int hpos, int vpos, int x, int y)
28784 struct frame *f = XFRAME (w->frame);
28785 int new_cursor_type;
28786 int new_cursor_width;
28787 bool active_cursor;
28788 struct glyph_row *glyph_row;
28789 struct glyph *glyph;
28791 /* This is pointless on invisible frames, and dangerous on garbaged
28792 windows and frames; in the latter case, the frame or window may
28793 be in the midst of changing its size, and x and y may be off the
28794 window. */
28795 if (! FRAME_VISIBLE_P (f)
28796 || vpos >= w->current_matrix->nrows
28797 || hpos >= w->current_matrix->matrix_w)
28798 return;
28800 /* If cursor is off and we want it off, return quickly. */
28801 if (!on && !w->phys_cursor_on_p)
28802 return;
28804 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28805 /* If cursor row is not enabled, we don't really know where to
28806 display the cursor. */
28807 if (!glyph_row->enabled_p)
28809 w->phys_cursor_on_p = false;
28810 return;
28813 /* A frame might be marked garbaged even though its cursor position
28814 is correct, and will not change upon subsequent redisplay. This
28815 happens in some rare situations, like toggling the sort order in
28816 Dired windows. We've already established that VPOS is valid, so
28817 it shouldn't do any harm to record the cursor position, as we are
28818 going to return without acting on it anyway. Otherwise, expose
28819 events might come in and call update_window_cursor, which will
28820 blindly use outdated values in w->phys_cursor. */
28821 if (FRAME_GARBAGED_P (f))
28823 if (on)
28825 w->phys_cursor.x = x;
28826 w->phys_cursor.y = glyph_row->y;
28827 w->phys_cursor.hpos = hpos;
28828 w->phys_cursor.vpos = vpos;
28830 return;
28833 glyph = NULL;
28834 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28835 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28837 eassert (input_blocked_p ());
28839 /* Set new_cursor_type to the cursor we want to be displayed. */
28840 new_cursor_type = get_window_cursor_type (w, glyph,
28841 &new_cursor_width, &active_cursor);
28843 /* If cursor is currently being shown and we don't want it to be or
28844 it is in the wrong place, or the cursor type is not what we want,
28845 erase it. */
28846 if (w->phys_cursor_on_p
28847 && (!on
28848 || w->phys_cursor.x != x
28849 || w->phys_cursor.y != y
28850 /* HPOS can be negative in R2L rows whose
28851 exact_window_width_line_p flag is set (i.e. their newline
28852 would "overflow into the fringe"). */
28853 || hpos < 0
28854 || new_cursor_type != w->phys_cursor_type
28855 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28856 && new_cursor_width != w->phys_cursor_width)))
28857 erase_phys_cursor (w);
28859 /* Don't check phys_cursor_on_p here because that flag is only set
28860 to false in some cases where we know that the cursor has been
28861 completely erased, to avoid the extra work of erasing the cursor
28862 twice. In other words, phys_cursor_on_p can be true and the cursor
28863 still not be visible, or it has only been partly erased. */
28864 if (on)
28866 w->phys_cursor_ascent = glyph_row->ascent;
28867 w->phys_cursor_height = glyph_row->height;
28869 /* Set phys_cursor_.* before x_draw_.* is called because some
28870 of them may need the information. */
28871 w->phys_cursor.x = x;
28872 w->phys_cursor.y = glyph_row->y;
28873 w->phys_cursor.hpos = hpos;
28874 w->phys_cursor.vpos = vpos;
28877 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28878 new_cursor_type, new_cursor_width,
28879 on, active_cursor);
28883 /* Switch the display of W's cursor on or off, according to the value
28884 of ON. */
28886 static void
28887 update_window_cursor (struct window *w, bool on)
28889 /* Don't update cursor in windows whose frame is in the process
28890 of being deleted. */
28891 if (w->current_matrix)
28893 int hpos = w->phys_cursor.hpos;
28894 int vpos = w->phys_cursor.vpos;
28895 struct glyph_row *row;
28897 if (vpos >= w->current_matrix->nrows
28898 || hpos >= w->current_matrix->matrix_w)
28899 return;
28901 row = MATRIX_ROW (w->current_matrix, vpos);
28903 /* When the window is hscrolled, cursor hpos can legitimately be
28904 out of bounds, but we draw the cursor at the corresponding
28905 window margin in that case. */
28906 if (!row->reversed_p && hpos < 0)
28907 hpos = 0;
28908 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28909 hpos = row->used[TEXT_AREA] - 1;
28911 block_input ();
28912 display_and_set_cursor (w, on, hpos, vpos,
28913 w->phys_cursor.x, w->phys_cursor.y);
28914 unblock_input ();
28919 /* Call update_window_cursor with parameter ON_P on all leaf windows
28920 in the window tree rooted at W. */
28922 static void
28923 update_cursor_in_window_tree (struct window *w, bool on_p)
28925 while (w)
28927 if (WINDOWP (w->contents))
28928 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28929 else
28930 update_window_cursor (w, on_p);
28932 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28937 /* EXPORT:
28938 Display the cursor on window W, or clear it, according to ON_P.
28939 Don't change the cursor's position. */
28941 void
28942 x_update_cursor (struct frame *f, bool on_p)
28944 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28948 /* EXPORT:
28949 Clear the cursor of window W to background color, and mark the
28950 cursor as not shown. This is used when the text where the cursor
28951 is about to be rewritten. */
28953 void
28954 x_clear_cursor (struct window *w)
28956 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28957 update_window_cursor (w, false);
28960 #endif /* HAVE_WINDOW_SYSTEM */
28962 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28963 and MSDOS. */
28964 static void
28965 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28966 int start_hpos, int end_hpos,
28967 enum draw_glyphs_face draw)
28969 #ifdef HAVE_WINDOW_SYSTEM
28970 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28972 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28973 return;
28975 #endif
28976 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28977 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28978 #endif
28981 /* Display the active region described by mouse_face_* according to DRAW. */
28983 static void
28984 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28986 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28987 struct frame *f = XFRAME (WINDOW_FRAME (w));
28989 if (/* If window is in the process of being destroyed, don't bother
28990 to do anything. */
28991 w->current_matrix != NULL
28992 /* Don't update mouse highlight if hidden. */
28993 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28994 /* Recognize when we are called to operate on rows that don't exist
28995 anymore. This can happen when a window is split. */
28996 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28998 bool phys_cursor_on_p = w->phys_cursor_on_p;
28999 struct glyph_row *row, *first, *last;
29001 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29002 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29004 for (row = first; row <= last && row->enabled_p; ++row)
29006 int start_hpos, end_hpos, start_x;
29008 /* For all but the first row, the highlight starts at column 0. */
29009 if (row == first)
29011 /* R2L rows have BEG and END in reversed order, but the
29012 screen drawing geometry is always left to right. So
29013 we need to mirror the beginning and end of the
29014 highlighted area in R2L rows. */
29015 if (!row->reversed_p)
29017 start_hpos = hlinfo->mouse_face_beg_col;
29018 start_x = hlinfo->mouse_face_beg_x;
29020 else if (row == last)
29022 start_hpos = hlinfo->mouse_face_end_col;
29023 start_x = hlinfo->mouse_face_end_x;
29025 else
29027 start_hpos = 0;
29028 start_x = 0;
29031 else if (row->reversed_p && row == last)
29033 start_hpos = hlinfo->mouse_face_end_col;
29034 start_x = hlinfo->mouse_face_end_x;
29036 else
29038 start_hpos = 0;
29039 start_x = 0;
29042 if (row == last)
29044 if (!row->reversed_p)
29045 end_hpos = hlinfo->mouse_face_end_col;
29046 else if (row == first)
29047 end_hpos = hlinfo->mouse_face_beg_col;
29048 else
29050 end_hpos = row->used[TEXT_AREA];
29051 if (draw == DRAW_NORMAL_TEXT)
29052 row->fill_line_p = true; /* Clear to end of line. */
29055 else if (row->reversed_p && row == first)
29056 end_hpos = hlinfo->mouse_face_beg_col;
29057 else
29059 end_hpos = row->used[TEXT_AREA];
29060 if (draw == DRAW_NORMAL_TEXT)
29061 row->fill_line_p = true; /* Clear to end of line. */
29064 if (end_hpos > start_hpos)
29066 draw_row_with_mouse_face (w, start_x, row,
29067 start_hpos, end_hpos, draw);
29069 row->mouse_face_p
29070 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29074 /* When we've written over the cursor, arrange for it to
29075 be displayed again. */
29076 if (FRAME_WINDOW_P (f)
29077 && phys_cursor_on_p && !w->phys_cursor_on_p)
29079 #ifdef HAVE_WINDOW_SYSTEM
29080 int hpos = w->phys_cursor.hpos;
29082 /* When the window is hscrolled, cursor hpos can legitimately be
29083 out of bounds, but we draw the cursor at the corresponding
29084 window margin in that case. */
29085 if (!row->reversed_p && hpos < 0)
29086 hpos = 0;
29087 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29088 hpos = row->used[TEXT_AREA] - 1;
29090 block_input ();
29091 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29092 w->phys_cursor.x, w->phys_cursor.y);
29093 unblock_input ();
29094 #endif /* HAVE_WINDOW_SYSTEM */
29098 #ifdef HAVE_WINDOW_SYSTEM
29099 /* Change the mouse cursor. */
29100 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29102 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29103 if (draw == DRAW_NORMAL_TEXT
29104 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29105 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29106 else
29107 #endif
29108 if (draw == DRAW_MOUSE_FACE)
29109 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29110 else
29111 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29113 #endif /* HAVE_WINDOW_SYSTEM */
29116 /* EXPORT:
29117 Clear out the mouse-highlighted active region.
29118 Redraw it un-highlighted first. Value is true if mouse
29119 face was actually drawn unhighlighted. */
29121 bool
29122 clear_mouse_face (Mouse_HLInfo *hlinfo)
29124 bool cleared
29125 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29126 if (cleared)
29127 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29128 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29129 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29130 hlinfo->mouse_face_window = Qnil;
29131 hlinfo->mouse_face_overlay = Qnil;
29132 return cleared;
29135 /* Return true if the coordinates HPOS and VPOS on windows W are
29136 within the mouse face on that window. */
29137 static bool
29138 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29140 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29142 /* Quickly resolve the easy cases. */
29143 if (!(WINDOWP (hlinfo->mouse_face_window)
29144 && XWINDOW (hlinfo->mouse_face_window) == w))
29145 return false;
29146 if (vpos < hlinfo->mouse_face_beg_row
29147 || vpos > hlinfo->mouse_face_end_row)
29148 return false;
29149 if (vpos > hlinfo->mouse_face_beg_row
29150 && vpos < hlinfo->mouse_face_end_row)
29151 return true;
29153 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29155 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29157 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29158 return true;
29160 else if ((vpos == hlinfo->mouse_face_beg_row
29161 && hpos >= hlinfo->mouse_face_beg_col)
29162 || (vpos == hlinfo->mouse_face_end_row
29163 && hpos < hlinfo->mouse_face_end_col))
29164 return true;
29166 else
29168 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29170 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29171 return true;
29173 else if ((vpos == hlinfo->mouse_face_beg_row
29174 && hpos <= hlinfo->mouse_face_beg_col)
29175 || (vpos == hlinfo->mouse_face_end_row
29176 && hpos > hlinfo->mouse_face_end_col))
29177 return true;
29179 return false;
29183 /* EXPORT:
29184 True if physical cursor of window W is within mouse face. */
29186 bool
29187 cursor_in_mouse_face_p (struct window *w)
29189 int hpos = w->phys_cursor.hpos;
29190 int vpos = w->phys_cursor.vpos;
29191 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29193 /* When the window is hscrolled, cursor hpos can legitimately be out
29194 of bounds, but we draw the cursor at the corresponding window
29195 margin in that case. */
29196 if (!row->reversed_p && hpos < 0)
29197 hpos = 0;
29198 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29199 hpos = row->used[TEXT_AREA] - 1;
29201 return coords_in_mouse_face_p (w, hpos, vpos);
29206 /* Find the glyph rows START_ROW and END_ROW of window W that display
29207 characters between buffer positions START_CHARPOS and END_CHARPOS
29208 (excluding END_CHARPOS). DISP_STRING is a display string that
29209 covers these buffer positions. This is similar to
29210 row_containing_pos, but is more accurate when bidi reordering makes
29211 buffer positions change non-linearly with glyph rows. */
29212 static void
29213 rows_from_pos_range (struct window *w,
29214 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29215 Lisp_Object disp_string,
29216 struct glyph_row **start, struct glyph_row **end)
29218 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29219 int last_y = window_text_bottom_y (w);
29220 struct glyph_row *row;
29222 *start = NULL;
29223 *end = NULL;
29225 while (!first->enabled_p
29226 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29227 first++;
29229 /* Find the START row. */
29230 for (row = first;
29231 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29232 row++)
29234 /* A row can potentially be the START row if the range of the
29235 characters it displays intersects the range
29236 [START_CHARPOS..END_CHARPOS). */
29237 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29238 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29239 /* See the commentary in row_containing_pos, for the
29240 explanation of the complicated way to check whether
29241 some position is beyond the end of the characters
29242 displayed by a row. */
29243 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29244 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29245 && !row->ends_at_zv_p
29246 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29247 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29248 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29249 && !row->ends_at_zv_p
29250 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29252 /* Found a candidate row. Now make sure at least one of the
29253 glyphs it displays has a charpos from the range
29254 [START_CHARPOS..END_CHARPOS).
29256 This is not obvious because bidi reordering could make
29257 buffer positions of a row be 1,2,3,102,101,100, and if we
29258 want to highlight characters in [50..60), we don't want
29259 this row, even though [50..60) does intersect [1..103),
29260 the range of character positions given by the row's start
29261 and end positions. */
29262 struct glyph *g = row->glyphs[TEXT_AREA];
29263 struct glyph *e = g + row->used[TEXT_AREA];
29265 while (g < e)
29267 if (((BUFFERP (g->object) || NILP (g->object))
29268 && start_charpos <= g->charpos && g->charpos < end_charpos)
29269 /* A glyph that comes from DISP_STRING is by
29270 definition to be highlighted. */
29271 || EQ (g->object, disp_string))
29272 *start = row;
29273 g++;
29275 if (*start)
29276 break;
29280 /* Find the END row. */
29281 if (!*start
29282 /* If the last row is partially visible, start looking for END
29283 from that row, instead of starting from FIRST. */
29284 && !(row->enabled_p
29285 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29286 row = first;
29287 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29289 struct glyph_row *next = row + 1;
29290 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29292 if (!next->enabled_p
29293 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29294 /* The first row >= START whose range of displayed characters
29295 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29296 is the row END + 1. */
29297 || (start_charpos < next_start
29298 && end_charpos < next_start)
29299 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29300 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29301 && !next->ends_at_zv_p
29302 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29303 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29304 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29305 && !next->ends_at_zv_p
29306 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29308 *end = row;
29309 break;
29311 else
29313 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29314 but none of the characters it displays are in the range, it is
29315 also END + 1. */
29316 struct glyph *g = next->glyphs[TEXT_AREA];
29317 struct glyph *s = g;
29318 struct glyph *e = g + next->used[TEXT_AREA];
29320 while (g < e)
29322 if (((BUFFERP (g->object) || NILP (g->object))
29323 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29324 /* If the buffer position of the first glyph in
29325 the row is equal to END_CHARPOS, it means
29326 the last character to be highlighted is the
29327 newline of ROW, and we must consider NEXT as
29328 END, not END+1. */
29329 || (((!next->reversed_p && g == s)
29330 || (next->reversed_p && g == e - 1))
29331 && (g->charpos == end_charpos
29332 /* Special case for when NEXT is an
29333 empty line at ZV. */
29334 || (g->charpos == -1
29335 && !row->ends_at_zv_p
29336 && next_start == end_charpos)))))
29337 /* A glyph that comes from DISP_STRING is by
29338 definition to be highlighted. */
29339 || EQ (g->object, disp_string))
29340 break;
29341 g++;
29343 if (g == e)
29345 *end = row;
29346 break;
29348 /* The first row that ends at ZV must be the last to be
29349 highlighted. */
29350 else if (next->ends_at_zv_p)
29352 *end = next;
29353 break;
29359 /* This function sets the mouse_face_* elements of HLINFO, assuming
29360 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29361 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29362 for the overlay or run of text properties specifying the mouse
29363 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29364 before-string and after-string that must also be highlighted.
29365 DISP_STRING, if non-nil, is a display string that may cover some
29366 or all of the highlighted text. */
29368 static void
29369 mouse_face_from_buffer_pos (Lisp_Object window,
29370 Mouse_HLInfo *hlinfo,
29371 ptrdiff_t mouse_charpos,
29372 ptrdiff_t start_charpos,
29373 ptrdiff_t end_charpos,
29374 Lisp_Object before_string,
29375 Lisp_Object after_string,
29376 Lisp_Object disp_string)
29378 struct window *w = XWINDOW (window);
29379 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29380 struct glyph_row *r1, *r2;
29381 struct glyph *glyph, *end;
29382 ptrdiff_t ignore, pos;
29383 int x;
29385 eassert (NILP (disp_string) || STRINGP (disp_string));
29386 eassert (NILP (before_string) || STRINGP (before_string));
29387 eassert (NILP (after_string) || STRINGP (after_string));
29389 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29390 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29391 if (r1 == NULL)
29392 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29393 /* If the before-string or display-string contains newlines,
29394 rows_from_pos_range skips to its last row. Move back. */
29395 if (!NILP (before_string) || !NILP (disp_string))
29397 struct glyph_row *prev;
29398 while ((prev = r1 - 1, prev >= first)
29399 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29400 && prev->used[TEXT_AREA] > 0)
29402 struct glyph *beg = prev->glyphs[TEXT_AREA];
29403 glyph = beg + prev->used[TEXT_AREA];
29404 while (--glyph >= beg && NILP (glyph->object));
29405 if (glyph < beg
29406 || !(EQ (glyph->object, before_string)
29407 || EQ (glyph->object, disp_string)))
29408 break;
29409 r1 = prev;
29412 if (r2 == NULL)
29414 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29415 hlinfo->mouse_face_past_end = true;
29417 else if (!NILP (after_string))
29419 /* If the after-string has newlines, advance to its last row. */
29420 struct glyph_row *next;
29421 struct glyph_row *last
29422 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29424 for (next = r2 + 1;
29425 next <= last
29426 && next->used[TEXT_AREA] > 0
29427 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29428 ++next)
29429 r2 = next;
29431 /* The rest of the display engine assumes that mouse_face_beg_row is
29432 either above mouse_face_end_row or identical to it. But with
29433 bidi-reordered continued lines, the row for START_CHARPOS could
29434 be below the row for END_CHARPOS. If so, swap the rows and store
29435 them in correct order. */
29436 if (r1->y > r2->y)
29438 struct glyph_row *tem = r2;
29440 r2 = r1;
29441 r1 = tem;
29444 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29445 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29447 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29448 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29449 could be anywhere in the row and in any order. The strategy
29450 below is to find the leftmost and the rightmost glyph that
29451 belongs to either of these 3 strings, or whose position is
29452 between START_CHARPOS and END_CHARPOS, and highlight all the
29453 glyphs between those two. This may cover more than just the text
29454 between START_CHARPOS and END_CHARPOS if the range of characters
29455 strides the bidi level boundary, e.g. if the beginning is in R2L
29456 text while the end is in L2R text or vice versa. */
29457 if (!r1->reversed_p)
29459 /* This row is in a left to right paragraph. Scan it left to
29460 right. */
29461 glyph = r1->glyphs[TEXT_AREA];
29462 end = glyph + r1->used[TEXT_AREA];
29463 x = r1->x;
29465 /* Skip truncation glyphs at the start of the glyph row. */
29466 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29467 for (; glyph < end
29468 && NILP (glyph->object)
29469 && glyph->charpos < 0;
29470 ++glyph)
29471 x += glyph->pixel_width;
29473 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29474 or DISP_STRING, and the first glyph from buffer whose
29475 position is between START_CHARPOS and END_CHARPOS. */
29476 for (; glyph < end
29477 && !NILP (glyph->object)
29478 && !EQ (glyph->object, disp_string)
29479 && !(BUFFERP (glyph->object)
29480 && (glyph->charpos >= start_charpos
29481 && glyph->charpos < end_charpos));
29482 ++glyph)
29484 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29485 are present at buffer positions between START_CHARPOS and
29486 END_CHARPOS, or if they come from an overlay. */
29487 if (EQ (glyph->object, before_string))
29489 pos = string_buffer_position (before_string,
29490 start_charpos);
29491 /* If pos == 0, it means before_string came from an
29492 overlay, not from a buffer position. */
29493 if (!pos || (pos >= start_charpos && pos < end_charpos))
29494 break;
29496 else if (EQ (glyph->object, after_string))
29498 pos = string_buffer_position (after_string, end_charpos);
29499 if (!pos || (pos >= start_charpos && pos < end_charpos))
29500 break;
29502 x += glyph->pixel_width;
29504 hlinfo->mouse_face_beg_x = x;
29505 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29507 else
29509 /* This row is in a right to left paragraph. Scan it right to
29510 left. */
29511 struct glyph *g;
29513 end = r1->glyphs[TEXT_AREA] - 1;
29514 glyph = end + r1->used[TEXT_AREA];
29516 /* Skip truncation glyphs at the start of the glyph row. */
29517 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29518 for (; glyph > end
29519 && NILP (glyph->object)
29520 && glyph->charpos < 0;
29521 --glyph)
29524 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29525 or DISP_STRING, and the first glyph from buffer whose
29526 position is between START_CHARPOS and END_CHARPOS. */
29527 for (; glyph > end
29528 && !NILP (glyph->object)
29529 && !EQ (glyph->object, disp_string)
29530 && !(BUFFERP (glyph->object)
29531 && (glyph->charpos >= start_charpos
29532 && glyph->charpos < end_charpos));
29533 --glyph)
29535 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29536 are present at buffer positions between START_CHARPOS and
29537 END_CHARPOS, or if they come from an overlay. */
29538 if (EQ (glyph->object, before_string))
29540 pos = string_buffer_position (before_string, start_charpos);
29541 /* If pos == 0, it means before_string came from an
29542 overlay, not from a buffer position. */
29543 if (!pos || (pos >= start_charpos && pos < end_charpos))
29544 break;
29546 else if (EQ (glyph->object, after_string))
29548 pos = string_buffer_position (after_string, end_charpos);
29549 if (!pos || (pos >= start_charpos && pos < end_charpos))
29550 break;
29554 glyph++; /* first glyph to the right of the highlighted area */
29555 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29556 x += g->pixel_width;
29557 hlinfo->mouse_face_beg_x = x;
29558 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29561 /* If the highlight ends in a different row, compute GLYPH and END
29562 for the end row. Otherwise, reuse the values computed above for
29563 the row where the highlight begins. */
29564 if (r2 != r1)
29566 if (!r2->reversed_p)
29568 glyph = r2->glyphs[TEXT_AREA];
29569 end = glyph + r2->used[TEXT_AREA];
29570 x = r2->x;
29572 else
29574 end = r2->glyphs[TEXT_AREA] - 1;
29575 glyph = end + r2->used[TEXT_AREA];
29579 if (!r2->reversed_p)
29581 /* Skip truncation and continuation glyphs near the end of the
29582 row, and also blanks and stretch glyphs inserted by
29583 extend_face_to_end_of_line. */
29584 while (end > glyph
29585 && NILP ((end - 1)->object))
29586 --end;
29587 /* Scan the rest of the glyph row from the end, looking for the
29588 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29589 DISP_STRING, or whose position is between START_CHARPOS
29590 and END_CHARPOS */
29591 for (--end;
29592 end > glyph
29593 && !NILP (end->object)
29594 && !EQ (end->object, disp_string)
29595 && !(BUFFERP (end->object)
29596 && (end->charpos >= start_charpos
29597 && end->charpos < end_charpos));
29598 --end)
29600 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29601 are present at buffer positions between START_CHARPOS and
29602 END_CHARPOS, or if they come from an overlay. */
29603 if (EQ (end->object, before_string))
29605 pos = string_buffer_position (before_string, start_charpos);
29606 if (!pos || (pos >= start_charpos && pos < end_charpos))
29607 break;
29609 else if (EQ (end->object, after_string))
29611 pos = string_buffer_position (after_string, end_charpos);
29612 if (!pos || (pos >= start_charpos && pos < end_charpos))
29613 break;
29616 /* Find the X coordinate of the last glyph to be highlighted. */
29617 for (; glyph <= end; ++glyph)
29618 x += glyph->pixel_width;
29620 hlinfo->mouse_face_end_x = x;
29621 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29623 else
29625 /* Skip truncation and continuation glyphs near the end of the
29626 row, and also blanks and stretch glyphs inserted by
29627 extend_face_to_end_of_line. */
29628 x = r2->x;
29629 end++;
29630 while (end < glyph
29631 && NILP (end->object))
29633 x += end->pixel_width;
29634 ++end;
29636 /* Scan the rest of the glyph row from the end, looking for the
29637 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29638 DISP_STRING, or whose position is between START_CHARPOS
29639 and END_CHARPOS */
29640 for ( ;
29641 end < glyph
29642 && !NILP (end->object)
29643 && !EQ (end->object, disp_string)
29644 && !(BUFFERP (end->object)
29645 && (end->charpos >= start_charpos
29646 && end->charpos < end_charpos));
29647 ++end)
29649 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29650 are present at buffer positions between START_CHARPOS and
29651 END_CHARPOS, or if they come from an overlay. */
29652 if (EQ (end->object, before_string))
29654 pos = string_buffer_position (before_string, start_charpos);
29655 if (!pos || (pos >= start_charpos && pos < end_charpos))
29656 break;
29658 else if (EQ (end->object, after_string))
29660 pos = string_buffer_position (after_string, end_charpos);
29661 if (!pos || (pos >= start_charpos && pos < end_charpos))
29662 break;
29664 x += end->pixel_width;
29666 /* If we exited the above loop because we arrived at the last
29667 glyph of the row, and its buffer position is still not in
29668 range, it means the last character in range is the preceding
29669 newline. Bump the end column and x values to get past the
29670 last glyph. */
29671 if (end == glyph
29672 && BUFFERP (end->object)
29673 && (end->charpos < start_charpos
29674 || end->charpos >= end_charpos))
29676 x += end->pixel_width;
29677 ++end;
29679 hlinfo->mouse_face_end_x = x;
29680 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29683 hlinfo->mouse_face_window = window;
29684 hlinfo->mouse_face_face_id
29685 = face_at_buffer_position (w, mouse_charpos, &ignore,
29686 mouse_charpos + 1,
29687 !hlinfo->mouse_face_hidden, -1);
29688 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29691 /* The following function is not used anymore (replaced with
29692 mouse_face_from_string_pos), but I leave it here for the time
29693 being, in case someone would. */
29695 #if false /* not used */
29697 /* Find the position of the glyph for position POS in OBJECT in
29698 window W's current matrix, and return in *X, *Y the pixel
29699 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29701 RIGHT_P means return the position of the right edge of the glyph.
29702 !RIGHT_P means return the left edge position.
29704 If no glyph for POS exists in the matrix, return the position of
29705 the glyph with the next smaller position that is in the matrix, if
29706 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29707 exists in the matrix, return the position of the glyph with the
29708 next larger position in OBJECT.
29710 Value is true if a glyph was found. */
29712 static bool
29713 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29714 int *hpos, int *vpos, int *x, int *y, bool right_p)
29716 int yb = window_text_bottom_y (w);
29717 struct glyph_row *r;
29718 struct glyph *best_glyph = NULL;
29719 struct glyph_row *best_row = NULL;
29720 int best_x = 0;
29722 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29723 r->enabled_p && r->y < yb;
29724 ++r)
29726 struct glyph *g = r->glyphs[TEXT_AREA];
29727 struct glyph *e = g + r->used[TEXT_AREA];
29728 int gx;
29730 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29731 if (EQ (g->object, object))
29733 if (g->charpos == pos)
29735 best_glyph = g;
29736 best_x = gx;
29737 best_row = r;
29738 goto found;
29740 else if (best_glyph == NULL
29741 || ((eabs (g->charpos - pos)
29742 < eabs (best_glyph->charpos - pos))
29743 && (right_p
29744 ? g->charpos < pos
29745 : g->charpos > pos)))
29747 best_glyph = g;
29748 best_x = gx;
29749 best_row = r;
29754 found:
29756 if (best_glyph)
29758 *x = best_x;
29759 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29761 if (right_p)
29763 *x += best_glyph->pixel_width;
29764 ++*hpos;
29767 *y = best_row->y;
29768 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29771 return best_glyph != NULL;
29773 #endif /* not used */
29775 /* Find the positions of the first and the last glyphs in window W's
29776 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29777 (assumed to be a string), and return in HLINFO's mouse_face_*
29778 members the pixel and column/row coordinates of those glyphs. */
29780 static void
29781 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29782 Lisp_Object object,
29783 ptrdiff_t startpos, ptrdiff_t endpos)
29785 int yb = window_text_bottom_y (w);
29786 struct glyph_row *r;
29787 struct glyph *g, *e;
29788 int gx;
29789 bool found = false;
29791 /* Find the glyph row with at least one position in the range
29792 [STARTPOS..ENDPOS), and the first glyph in that row whose
29793 position belongs to that range. */
29794 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29795 r->enabled_p && r->y < yb;
29796 ++r)
29798 if (!r->reversed_p)
29800 g = r->glyphs[TEXT_AREA];
29801 e = g + r->used[TEXT_AREA];
29802 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29803 if (EQ (g->object, object)
29804 && startpos <= g->charpos && g->charpos < endpos)
29806 hlinfo->mouse_face_beg_row
29807 = MATRIX_ROW_VPOS (r, w->current_matrix);
29808 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29809 hlinfo->mouse_face_beg_x = gx;
29810 found = true;
29811 break;
29814 else
29816 struct glyph *g1;
29818 e = r->glyphs[TEXT_AREA];
29819 g = e + r->used[TEXT_AREA];
29820 for ( ; g > e; --g)
29821 if (EQ ((g-1)->object, object)
29822 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29824 hlinfo->mouse_face_beg_row
29825 = MATRIX_ROW_VPOS (r, w->current_matrix);
29826 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29827 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29828 gx += g1->pixel_width;
29829 hlinfo->mouse_face_beg_x = gx;
29830 found = true;
29831 break;
29834 if (found)
29835 break;
29838 if (!found)
29839 return;
29841 /* Starting with the next row, look for the first row which does NOT
29842 include any glyphs whose positions are in the range. */
29843 for (++r; r->enabled_p && r->y < yb; ++r)
29845 g = r->glyphs[TEXT_AREA];
29846 e = g + r->used[TEXT_AREA];
29847 found = false;
29848 for ( ; g < e; ++g)
29849 if (EQ (g->object, object)
29850 && startpos <= g->charpos && g->charpos < endpos)
29852 found = true;
29853 break;
29855 if (!found)
29856 break;
29859 /* The highlighted region ends on the previous row. */
29860 r--;
29862 /* Set the end row. */
29863 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29865 /* Compute and set the end column and the end column's horizontal
29866 pixel coordinate. */
29867 if (!r->reversed_p)
29869 g = r->glyphs[TEXT_AREA];
29870 e = g + r->used[TEXT_AREA];
29871 for ( ; e > g; --e)
29872 if (EQ ((e-1)->object, object)
29873 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29874 break;
29875 hlinfo->mouse_face_end_col = e - g;
29877 for (gx = r->x; g < e; ++g)
29878 gx += g->pixel_width;
29879 hlinfo->mouse_face_end_x = gx;
29881 else
29883 e = r->glyphs[TEXT_AREA];
29884 g = e + r->used[TEXT_AREA];
29885 for (gx = r->x ; e < g; ++e)
29887 if (EQ (e->object, object)
29888 && startpos <= e->charpos && e->charpos < endpos)
29889 break;
29890 gx += e->pixel_width;
29892 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29893 hlinfo->mouse_face_end_x = gx;
29897 #ifdef HAVE_WINDOW_SYSTEM
29899 /* See if position X, Y is within a hot-spot of an image. */
29901 static bool
29902 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29904 if (!CONSP (hot_spot))
29905 return false;
29907 if (EQ (XCAR (hot_spot), Qrect))
29909 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29910 Lisp_Object rect = XCDR (hot_spot);
29911 Lisp_Object tem;
29912 if (!CONSP (rect))
29913 return false;
29914 if (!CONSP (XCAR (rect)))
29915 return false;
29916 if (!CONSP (XCDR (rect)))
29917 return false;
29918 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29919 return false;
29920 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29921 return false;
29922 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29923 return false;
29924 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29925 return false;
29926 return true;
29928 else if (EQ (XCAR (hot_spot), Qcircle))
29930 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29931 Lisp_Object circ = XCDR (hot_spot);
29932 Lisp_Object lr, lx0, ly0;
29933 if (CONSP (circ)
29934 && CONSP (XCAR (circ))
29935 && (lr = XCDR (circ), NUMBERP (lr))
29936 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29937 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29939 double r = XFLOATINT (lr);
29940 double dx = XINT (lx0) - x;
29941 double dy = XINT (ly0) - y;
29942 return (dx * dx + dy * dy <= r * r);
29945 else if (EQ (XCAR (hot_spot), Qpoly))
29947 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29948 if (VECTORP (XCDR (hot_spot)))
29950 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29951 Lisp_Object *poly = v->contents;
29952 ptrdiff_t n = v->header.size;
29953 ptrdiff_t i;
29954 bool inside = false;
29955 Lisp_Object lx, ly;
29956 int x0, y0;
29958 /* Need an even number of coordinates, and at least 3 edges. */
29959 if (n < 6 || n & 1)
29960 return false;
29962 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29963 If count is odd, we are inside polygon. Pixels on edges
29964 may or may not be included depending on actual geometry of the
29965 polygon. */
29966 if ((lx = poly[n-2], !INTEGERP (lx))
29967 || (ly = poly[n-1], !INTEGERP (lx)))
29968 return false;
29969 x0 = XINT (lx), y0 = XINT (ly);
29970 for (i = 0; i < n; i += 2)
29972 int x1 = x0, y1 = y0;
29973 if ((lx = poly[i], !INTEGERP (lx))
29974 || (ly = poly[i+1], !INTEGERP (ly)))
29975 return false;
29976 x0 = XINT (lx), y0 = XINT (ly);
29978 /* Does this segment cross the X line? */
29979 if (x0 >= x)
29981 if (x1 >= x)
29982 continue;
29984 else if (x1 < x)
29985 continue;
29986 if (y > y0 && y > y1)
29987 continue;
29988 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29989 inside = !inside;
29991 return inside;
29994 return false;
29997 Lisp_Object
29998 find_hot_spot (Lisp_Object map, int x, int y)
30000 while (CONSP (map))
30002 if (CONSP (XCAR (map))
30003 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30004 return XCAR (map);
30005 map = XCDR (map);
30008 return Qnil;
30011 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30012 3, 3, 0,
30013 doc: /* Lookup in image map MAP coordinates X and Y.
30014 An image map is an alist where each element has the format (AREA ID PLIST).
30015 An AREA is specified as either a rectangle, a circle, or a polygon:
30016 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30017 pixel coordinates of the upper left and bottom right corners.
30018 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30019 and the radius of the circle; r may be a float or integer.
30020 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30021 vector describes one corner in the polygon.
30022 Returns the alist element for the first matching AREA in MAP. */)
30023 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30025 if (NILP (map))
30026 return Qnil;
30028 CHECK_NUMBER (x);
30029 CHECK_NUMBER (y);
30031 return find_hot_spot (map,
30032 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30033 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30035 #endif /* HAVE_WINDOW_SYSTEM */
30038 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30039 static void
30040 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30042 #ifdef HAVE_WINDOW_SYSTEM
30043 if (!FRAME_WINDOW_P (f))
30044 return;
30046 /* Do not change cursor shape while dragging mouse. */
30047 if (EQ (do_mouse_tracking, Qdragging))
30048 return;
30050 if (!NILP (pointer))
30052 if (EQ (pointer, Qarrow))
30053 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30054 else if (EQ (pointer, Qhand))
30055 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30056 else if (EQ (pointer, Qtext))
30057 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30058 else if (EQ (pointer, intern ("hdrag")))
30059 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30060 else if (EQ (pointer, intern ("nhdrag")))
30061 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30062 # ifdef HAVE_X_WINDOWS
30063 else if (EQ (pointer, intern ("vdrag")))
30064 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30065 # endif
30066 else if (EQ (pointer, intern ("hourglass")))
30067 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30068 else if (EQ (pointer, Qmodeline))
30069 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30070 else
30071 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30074 if (cursor != No_Cursor)
30075 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30076 #endif
30079 /* Take proper action when mouse has moved to the mode or header line
30080 or marginal area AREA of window W, x-position X and y-position Y.
30081 X is relative to the start of the text display area of W, so the
30082 width of bitmap areas and scroll bars must be subtracted to get a
30083 position relative to the start of the mode line. */
30085 static void
30086 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30087 enum window_part area)
30089 struct window *w = XWINDOW (window);
30090 struct frame *f = XFRAME (w->frame);
30091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30092 #ifdef HAVE_WINDOW_SYSTEM
30093 Display_Info *dpyinfo;
30094 #endif
30095 Cursor cursor = No_Cursor;
30096 Lisp_Object pointer = Qnil;
30097 int dx, dy, width, height;
30098 ptrdiff_t charpos;
30099 Lisp_Object string, object = Qnil;
30100 Lisp_Object pos UNINIT;
30101 Lisp_Object mouse_face;
30102 int original_x_pixel = x;
30103 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30104 struct glyph_row *row UNINIT;
30106 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30108 int x0;
30109 struct glyph *end;
30111 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30112 returns them in row/column units! */
30113 string = mode_line_string (w, area, &x, &y, &charpos,
30114 &object, &dx, &dy, &width, &height);
30116 row = (area == ON_MODE_LINE
30117 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30118 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30120 /* Find the glyph under the mouse pointer. */
30121 if (row->mode_line_p && row->enabled_p)
30123 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30124 end = glyph + row->used[TEXT_AREA];
30126 for (x0 = original_x_pixel;
30127 glyph < end && x0 >= glyph->pixel_width;
30128 ++glyph)
30129 x0 -= glyph->pixel_width;
30131 if (glyph >= end)
30132 glyph = NULL;
30135 else
30137 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30138 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30139 returns them in row/column units! */
30140 string = marginal_area_string (w, area, &x, &y, &charpos,
30141 &object, &dx, &dy, &width, &height);
30144 Lisp_Object help = Qnil;
30146 #ifdef HAVE_WINDOW_SYSTEM
30147 if (IMAGEP (object))
30149 Lisp_Object image_map, hotspot;
30150 if ((image_map = Fplist_get (XCDR (object), QCmap),
30151 !NILP (image_map))
30152 && (hotspot = find_hot_spot (image_map, dx, dy),
30153 CONSP (hotspot))
30154 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30156 Lisp_Object plist;
30158 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30159 If so, we could look for mouse-enter, mouse-leave
30160 properties in PLIST (and do something...). */
30161 hotspot = XCDR (hotspot);
30162 if (CONSP (hotspot)
30163 && (plist = XCAR (hotspot), CONSP (plist)))
30165 pointer = Fplist_get (plist, Qpointer);
30166 if (NILP (pointer))
30167 pointer = Qhand;
30168 help = Fplist_get (plist, Qhelp_echo);
30169 if (!NILP (help))
30171 help_echo_string = help;
30172 XSETWINDOW (help_echo_window, w);
30173 help_echo_object = w->contents;
30174 help_echo_pos = charpos;
30178 if (NILP (pointer))
30179 pointer = Fplist_get (XCDR (object), QCpointer);
30181 #endif /* HAVE_WINDOW_SYSTEM */
30183 if (STRINGP (string))
30184 pos = make_number (charpos);
30186 /* Set the help text and mouse pointer. If the mouse is on a part
30187 of the mode line without any text (e.g. past the right edge of
30188 the mode line text), use the default help text and pointer. */
30189 if (STRINGP (string) || area == ON_MODE_LINE)
30191 /* Arrange to display the help by setting the global variables
30192 help_echo_string, help_echo_object, and help_echo_pos. */
30193 if (NILP (help))
30195 if (STRINGP (string))
30196 help = Fget_text_property (pos, Qhelp_echo, string);
30198 if (!NILP (help))
30200 help_echo_string = help;
30201 XSETWINDOW (help_echo_window, w);
30202 help_echo_object = string;
30203 help_echo_pos = charpos;
30205 else if (area == ON_MODE_LINE)
30207 Lisp_Object default_help
30208 = buffer_local_value (Qmode_line_default_help_echo,
30209 w->contents);
30211 if (STRINGP (default_help))
30213 help_echo_string = default_help;
30214 XSETWINDOW (help_echo_window, w);
30215 help_echo_object = Qnil;
30216 help_echo_pos = -1;
30221 #ifdef HAVE_WINDOW_SYSTEM
30222 /* Change the mouse pointer according to what is under it. */
30223 if (FRAME_WINDOW_P (f))
30225 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30226 || minibuf_level
30227 || NILP (Vresize_mini_windows));
30229 dpyinfo = FRAME_DISPLAY_INFO (f);
30230 if (STRINGP (string))
30232 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30234 if (NILP (pointer))
30235 pointer = Fget_text_property (pos, Qpointer, string);
30237 /* Change the mouse pointer according to what is under X/Y. */
30238 if (NILP (pointer)
30239 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30241 Lisp_Object map;
30242 map = Fget_text_property (pos, Qlocal_map, string);
30243 if (!KEYMAPP (map))
30244 map = Fget_text_property (pos, Qkeymap, string);
30245 if (!KEYMAPP (map) && draggable)
30246 cursor = dpyinfo->vertical_scroll_bar_cursor;
30249 else if (draggable)
30250 /* Default mode-line pointer. */
30251 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30253 #endif
30256 /* Change the mouse face according to what is under X/Y. */
30257 bool mouse_face_shown = false;
30258 if (STRINGP (string))
30260 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30261 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30262 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30263 && glyph)
30265 Lisp_Object b, e;
30267 struct glyph * tmp_glyph;
30269 int gpos;
30270 int gseq_length;
30271 int total_pixel_width;
30272 ptrdiff_t begpos, endpos, ignore;
30274 int vpos, hpos;
30276 b = Fprevious_single_property_change (make_number (charpos + 1),
30277 Qmouse_face, string, Qnil);
30278 if (NILP (b))
30279 begpos = 0;
30280 else
30281 begpos = XINT (b);
30283 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30284 if (NILP (e))
30285 endpos = SCHARS (string);
30286 else
30287 endpos = XINT (e);
30289 /* Calculate the glyph position GPOS of GLYPH in the
30290 displayed string, relative to the beginning of the
30291 highlighted part of the string.
30293 Note: GPOS is different from CHARPOS. CHARPOS is the
30294 position of GLYPH in the internal string object. A mode
30295 line string format has structures which are converted to
30296 a flattened string by the Emacs Lisp interpreter. The
30297 internal string is an element of those structures. The
30298 displayed string is the flattened string. */
30299 tmp_glyph = row_start_glyph;
30300 while (tmp_glyph < glyph
30301 && (!(EQ (tmp_glyph->object, glyph->object)
30302 && begpos <= tmp_glyph->charpos
30303 && tmp_glyph->charpos < endpos)))
30304 tmp_glyph++;
30305 gpos = glyph - tmp_glyph;
30307 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30308 the highlighted part of the displayed string to which
30309 GLYPH belongs. Note: GSEQ_LENGTH is different from
30310 SCHARS (STRING), because the latter returns the length of
30311 the internal string. */
30312 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30313 tmp_glyph > glyph
30314 && (!(EQ (tmp_glyph->object, glyph->object)
30315 && begpos <= tmp_glyph->charpos
30316 && tmp_glyph->charpos < endpos));
30317 tmp_glyph--)
30319 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30321 /* Calculate the total pixel width of all the glyphs between
30322 the beginning of the highlighted area and GLYPH. */
30323 total_pixel_width = 0;
30324 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30325 total_pixel_width += tmp_glyph->pixel_width;
30327 /* Pre calculation of re-rendering position. Note: X is in
30328 column units here, after the call to mode_line_string or
30329 marginal_area_string. */
30330 hpos = x - gpos;
30331 vpos = (area == ON_MODE_LINE
30332 ? (w->current_matrix)->nrows - 1
30333 : 0);
30335 /* If GLYPH's position is included in the region that is
30336 already drawn in mouse face, we have nothing to do. */
30337 if ( EQ (window, hlinfo->mouse_face_window)
30338 && (!row->reversed_p
30339 ? (hlinfo->mouse_face_beg_col <= hpos
30340 && hpos < hlinfo->mouse_face_end_col)
30341 /* In R2L rows we swap BEG and END, see below. */
30342 : (hlinfo->mouse_face_end_col <= hpos
30343 && hpos < hlinfo->mouse_face_beg_col))
30344 && hlinfo->mouse_face_beg_row == vpos )
30345 return;
30347 if (clear_mouse_face (hlinfo))
30348 cursor = No_Cursor;
30350 if (!row->reversed_p)
30352 hlinfo->mouse_face_beg_col = hpos;
30353 hlinfo->mouse_face_beg_x = original_x_pixel
30354 - (total_pixel_width + dx);
30355 hlinfo->mouse_face_end_col = hpos + gseq_length;
30356 hlinfo->mouse_face_end_x = 0;
30358 else
30360 /* In R2L rows, show_mouse_face expects BEG and END
30361 coordinates to be swapped. */
30362 hlinfo->mouse_face_end_col = hpos;
30363 hlinfo->mouse_face_end_x = original_x_pixel
30364 - (total_pixel_width + dx);
30365 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30366 hlinfo->mouse_face_beg_x = 0;
30369 hlinfo->mouse_face_beg_row = vpos;
30370 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30371 hlinfo->mouse_face_past_end = false;
30372 hlinfo->mouse_face_window = window;
30374 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30375 charpos,
30376 0, &ignore,
30377 glyph->face_id,
30378 true);
30379 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30380 mouse_face_shown = true;
30382 if (NILP (pointer))
30383 pointer = Qhand;
30387 /* If mouse-face doesn't need to be shown, clear any existing
30388 mouse-face. */
30389 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30390 clear_mouse_face (hlinfo);
30392 define_frame_cursor1 (f, cursor, pointer);
30396 /* EXPORT:
30397 Take proper action when the mouse has moved to position X, Y on
30398 frame F with regards to highlighting portions of display that have
30399 mouse-face properties. Also de-highlight portions of display where
30400 the mouse was before, set the mouse pointer shape as appropriate
30401 for the mouse coordinates, and activate help echo (tooltips).
30402 X and Y can be negative or out of range. */
30404 void
30405 note_mouse_highlight (struct frame *f, int x, int y)
30407 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30408 enum window_part part = ON_NOTHING;
30409 Lisp_Object window;
30410 struct window *w;
30411 Cursor cursor = No_Cursor;
30412 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30413 struct buffer *b;
30415 /* When a menu is active, don't highlight because this looks odd. */
30416 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30417 if (popup_activated ())
30418 return;
30419 #endif
30421 if (!f->glyphs_initialized_p
30422 || f->pointer_invisible)
30423 return;
30425 hlinfo->mouse_face_mouse_x = x;
30426 hlinfo->mouse_face_mouse_y = y;
30427 hlinfo->mouse_face_mouse_frame = f;
30429 if (hlinfo->mouse_face_defer)
30430 return;
30432 /* Which window is that in? */
30433 window = window_from_coordinates (f, x, y, &part, true);
30435 /* If displaying active text in another window, clear that. */
30436 if (! EQ (window, hlinfo->mouse_face_window)
30437 /* Also clear if we move out of text area in same window. */
30438 || (!NILP (hlinfo->mouse_face_window)
30439 && !NILP (window)
30440 && part != ON_TEXT
30441 && part != ON_MODE_LINE
30442 && part != ON_HEADER_LINE))
30443 clear_mouse_face (hlinfo);
30445 /* Not on a window -> return. */
30446 if (!WINDOWP (window))
30447 return;
30449 /* Reset help_echo_string. It will get recomputed below. */
30450 help_echo_string = Qnil;
30452 /* Convert to window-relative pixel coordinates. */
30453 w = XWINDOW (window);
30454 frame_to_window_pixel_xy (w, &x, &y);
30456 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30457 /* Handle tool-bar window differently since it doesn't display a
30458 buffer. */
30459 if (EQ (window, f->tool_bar_window))
30461 note_tool_bar_highlight (f, x, y);
30462 return;
30464 #endif
30466 /* Mouse is on the mode, header line or margin? */
30467 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30468 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30470 note_mode_line_or_margin_highlight (window, x, y, part);
30472 #ifdef HAVE_WINDOW_SYSTEM
30473 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30475 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30476 /* Show non-text cursor (Bug#16647). */
30477 goto set_cursor;
30479 else
30480 #endif
30481 return;
30484 #ifdef HAVE_WINDOW_SYSTEM
30485 if (part == ON_VERTICAL_BORDER)
30487 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30488 help_echo_string = build_string ("drag-mouse-1: resize");
30490 else if (part == ON_RIGHT_DIVIDER)
30492 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30493 help_echo_string = build_string ("drag-mouse-1: resize");
30495 else if (part == ON_BOTTOM_DIVIDER)
30496 if (! WINDOW_BOTTOMMOST_P (w)
30497 || minibuf_level
30498 || NILP (Vresize_mini_windows))
30500 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30501 help_echo_string = build_string ("drag-mouse-1: resize");
30503 else
30504 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30505 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30506 || part == ON_VERTICAL_SCROLL_BAR
30507 || part == ON_HORIZONTAL_SCROLL_BAR)
30508 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30509 else
30510 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30511 #endif
30513 /* Are we in a window whose display is up to date?
30514 And verify the buffer's text has not changed. */
30515 b = XBUFFER (w->contents);
30516 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30518 int hpos, vpos, dx, dy, area = LAST_AREA;
30519 ptrdiff_t pos;
30520 struct glyph *glyph;
30521 Lisp_Object object;
30522 Lisp_Object mouse_face = Qnil, position;
30523 Lisp_Object *overlay_vec = NULL;
30524 ptrdiff_t i, noverlays;
30525 struct buffer *obuf;
30526 ptrdiff_t obegv, ozv;
30527 bool same_region;
30529 /* Find the glyph under X/Y. */
30530 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30532 #ifdef HAVE_WINDOW_SYSTEM
30533 /* Look for :pointer property on image. */
30534 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30536 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30537 if (img != NULL && IMAGEP (img->spec))
30539 Lisp_Object image_map, hotspot;
30540 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30541 !NILP (image_map))
30542 && (hotspot = find_hot_spot (image_map,
30543 glyph->slice.img.x + dx,
30544 glyph->slice.img.y + dy),
30545 CONSP (hotspot))
30546 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30548 Lisp_Object plist;
30550 /* Could check XCAR (hotspot) to see if we enter/leave
30551 this hot-spot.
30552 If so, we could look for mouse-enter, mouse-leave
30553 properties in PLIST (and do something...). */
30554 hotspot = XCDR (hotspot);
30555 if (CONSP (hotspot)
30556 && (plist = XCAR (hotspot), CONSP (plist)))
30558 pointer = Fplist_get (plist, Qpointer);
30559 if (NILP (pointer))
30560 pointer = Qhand;
30561 help_echo_string = Fplist_get (plist, Qhelp_echo);
30562 if (!NILP (help_echo_string))
30564 help_echo_window = window;
30565 help_echo_object = glyph->object;
30566 help_echo_pos = glyph->charpos;
30570 if (NILP (pointer))
30571 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30574 #endif /* HAVE_WINDOW_SYSTEM */
30576 /* Clear mouse face if X/Y not over text. */
30577 if (glyph == NULL
30578 || area != TEXT_AREA
30579 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30580 /* Glyph's OBJECT is nil for glyphs inserted by the
30581 display engine for its internal purposes, like truncation
30582 and continuation glyphs and blanks beyond the end of
30583 line's text on text terminals. If we are over such a
30584 glyph, we are not over any text. */
30585 || NILP (glyph->object)
30586 /* R2L rows have a stretch glyph at their front, which
30587 stands for no text, whereas L2R rows have no glyphs at
30588 all beyond the end of text. Treat such stretch glyphs
30589 like we do with NULL glyphs in L2R rows. */
30590 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30591 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30592 && glyph->type == STRETCH_GLYPH
30593 && glyph->avoid_cursor_p))
30595 if (clear_mouse_face (hlinfo))
30596 cursor = No_Cursor;
30597 if (FRAME_WINDOW_P (f) && NILP (pointer))
30599 #ifdef HAVE_WINDOW_SYSTEM
30600 if (area != TEXT_AREA)
30601 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30602 else
30603 pointer = Vvoid_text_area_pointer;
30604 #endif
30606 goto set_cursor;
30609 pos = glyph->charpos;
30610 object = glyph->object;
30611 if (!STRINGP (object) && !BUFFERP (object))
30612 goto set_cursor;
30614 /* If we get an out-of-range value, return now; avoid an error. */
30615 if (BUFFERP (object) && pos > BUF_Z (b))
30616 goto set_cursor;
30618 /* Make the window's buffer temporarily current for
30619 overlays_at and compute_char_face. */
30620 obuf = current_buffer;
30621 current_buffer = b;
30622 obegv = BEGV;
30623 ozv = ZV;
30624 BEGV = BEG;
30625 ZV = Z;
30627 /* Is this char mouse-active or does it have help-echo? */
30628 position = make_number (pos);
30630 USE_SAFE_ALLOCA;
30632 if (BUFFERP (object))
30634 /* Put all the overlays we want in a vector in overlay_vec. */
30635 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30636 /* Sort overlays into increasing priority order. */
30637 noverlays = sort_overlays (overlay_vec, noverlays, w);
30639 else
30640 noverlays = 0;
30642 if (NILP (Vmouse_highlight))
30644 clear_mouse_face (hlinfo);
30645 goto check_help_echo;
30648 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30650 if (same_region)
30651 cursor = No_Cursor;
30653 /* Check mouse-face highlighting. */
30654 if (! same_region
30655 /* If there exists an overlay with mouse-face overlapping
30656 the one we are currently highlighting, we have to
30657 check if we enter the overlapping overlay, and then
30658 highlight only that. */
30659 || (OVERLAYP (hlinfo->mouse_face_overlay)
30660 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30662 /* Find the highest priority overlay with a mouse-face. */
30663 Lisp_Object overlay = Qnil;
30664 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30666 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30667 if (!NILP (mouse_face))
30668 overlay = overlay_vec[i];
30671 /* If we're highlighting the same overlay as before, there's
30672 no need to do that again. */
30673 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30674 goto check_help_echo;
30676 /* Clear the display of the old active region, if any. */
30677 if (clear_mouse_face (hlinfo))
30678 cursor = No_Cursor;
30680 /* Record the overlay, if any, to be highlighted. */
30681 hlinfo->mouse_face_overlay = overlay;
30683 /* If no overlay applies, get a text property. */
30684 if (NILP (overlay))
30685 mouse_face = Fget_text_property (position, Qmouse_face, object);
30687 /* Next, compute the bounds of the mouse highlighting and
30688 display it. */
30689 if (!NILP (mouse_face) && STRINGP (object))
30691 /* The mouse-highlighting comes from a display string
30692 with a mouse-face. */
30693 Lisp_Object s, e;
30694 ptrdiff_t ignore;
30696 s = Fprevious_single_property_change
30697 (make_number (pos + 1), Qmouse_face, object, Qnil);
30698 e = Fnext_single_property_change
30699 (position, Qmouse_face, object, Qnil);
30700 if (NILP (s))
30701 s = make_number (0);
30702 if (NILP (e))
30703 e = make_number (SCHARS (object));
30704 mouse_face_from_string_pos (w, hlinfo, object,
30705 XINT (s), XINT (e));
30706 hlinfo->mouse_face_past_end = false;
30707 hlinfo->mouse_face_window = window;
30708 hlinfo->mouse_face_face_id
30709 = face_at_string_position (w, object, pos, 0, &ignore,
30710 glyph->face_id, true);
30711 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30712 cursor = No_Cursor;
30714 else
30716 /* The mouse-highlighting, if any, comes from an overlay
30717 or text property in the buffer. */
30718 Lisp_Object buffer UNINIT;
30719 Lisp_Object disp_string UNINIT;
30721 if (STRINGP (object))
30723 /* If we are on a display string with no mouse-face,
30724 check if the text under it has one. */
30725 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30726 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30727 pos = string_buffer_position (object, start);
30728 if (pos > 0)
30730 mouse_face = get_char_property_and_overlay
30731 (make_number (pos), Qmouse_face, w->contents, &overlay);
30732 buffer = w->contents;
30733 disp_string = object;
30736 else
30738 buffer = object;
30739 disp_string = Qnil;
30742 if (!NILP (mouse_face))
30744 Lisp_Object before, after;
30745 Lisp_Object before_string, after_string;
30746 /* To correctly find the limits of mouse highlight
30747 in a bidi-reordered buffer, we must not use the
30748 optimization of limiting the search in
30749 previous-single-property-change and
30750 next-single-property-change, because
30751 rows_from_pos_range needs the real start and end
30752 positions to DTRT in this case. That's because
30753 the first row visible in a window does not
30754 necessarily display the character whose position
30755 is the smallest. */
30756 Lisp_Object lim1
30757 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30758 ? Fmarker_position (w->start)
30759 : Qnil;
30760 Lisp_Object lim2
30761 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30762 ? make_number (BUF_Z (XBUFFER (buffer))
30763 - w->window_end_pos)
30764 : Qnil;
30766 if (NILP (overlay))
30768 /* Handle the text property case. */
30769 before = Fprevious_single_property_change
30770 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30771 after = Fnext_single_property_change
30772 (make_number (pos), Qmouse_face, buffer, lim2);
30773 before_string = after_string = Qnil;
30775 else
30777 /* Handle the overlay case. */
30778 before = Foverlay_start (overlay);
30779 after = Foverlay_end (overlay);
30780 before_string = Foverlay_get (overlay, Qbefore_string);
30781 after_string = Foverlay_get (overlay, Qafter_string);
30783 if (!STRINGP (before_string)) before_string = Qnil;
30784 if (!STRINGP (after_string)) after_string = Qnil;
30787 mouse_face_from_buffer_pos (window, hlinfo, pos,
30788 NILP (before)
30790 : XFASTINT (before),
30791 NILP (after)
30792 ? BUF_Z (XBUFFER (buffer))
30793 : XFASTINT (after),
30794 before_string, after_string,
30795 disp_string);
30796 cursor = No_Cursor;
30801 check_help_echo:
30803 /* Look for a `help-echo' property. */
30804 if (NILP (help_echo_string)) {
30805 Lisp_Object help, overlay;
30807 /* Check overlays first. */
30808 help = overlay = Qnil;
30809 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30811 overlay = overlay_vec[i];
30812 help = Foverlay_get (overlay, Qhelp_echo);
30815 if (!NILP (help))
30817 help_echo_string = help;
30818 help_echo_window = window;
30819 help_echo_object = overlay;
30820 help_echo_pos = pos;
30822 else
30824 Lisp_Object obj = glyph->object;
30825 ptrdiff_t charpos = glyph->charpos;
30827 /* Try text properties. */
30828 if (STRINGP (obj)
30829 && charpos >= 0
30830 && charpos < SCHARS (obj))
30832 help = Fget_text_property (make_number (charpos),
30833 Qhelp_echo, obj);
30834 if (NILP (help))
30836 /* If the string itself doesn't specify a help-echo,
30837 see if the buffer text ``under'' it does. */
30838 struct glyph_row *r
30839 = MATRIX_ROW (w->current_matrix, vpos);
30840 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30841 ptrdiff_t p = string_buffer_position (obj, start);
30842 if (p > 0)
30844 help = Fget_char_property (make_number (p),
30845 Qhelp_echo, w->contents);
30846 if (!NILP (help))
30848 charpos = p;
30849 obj = w->contents;
30854 else if (BUFFERP (obj)
30855 && charpos >= BEGV
30856 && charpos < ZV)
30857 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30858 obj);
30860 if (!NILP (help))
30862 help_echo_string = help;
30863 help_echo_window = window;
30864 help_echo_object = obj;
30865 help_echo_pos = charpos;
30870 #ifdef HAVE_WINDOW_SYSTEM
30871 /* Look for a `pointer' property. */
30872 if (FRAME_WINDOW_P (f) && NILP (pointer))
30874 /* Check overlays first. */
30875 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30876 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30878 if (NILP (pointer))
30880 Lisp_Object obj = glyph->object;
30881 ptrdiff_t charpos = glyph->charpos;
30883 /* Try text properties. */
30884 if (STRINGP (obj)
30885 && charpos >= 0
30886 && charpos < SCHARS (obj))
30888 pointer = Fget_text_property (make_number (charpos),
30889 Qpointer, obj);
30890 if (NILP (pointer))
30892 /* If the string itself doesn't specify a pointer,
30893 see if the buffer text ``under'' it does. */
30894 struct glyph_row *r
30895 = MATRIX_ROW (w->current_matrix, vpos);
30896 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30897 ptrdiff_t p = string_buffer_position (obj, start);
30898 if (p > 0)
30899 pointer = Fget_char_property (make_number (p),
30900 Qpointer, w->contents);
30903 else if (BUFFERP (obj)
30904 && charpos >= BEGV
30905 && charpos < ZV)
30906 pointer = Fget_text_property (make_number (charpos),
30907 Qpointer, obj);
30910 #endif /* HAVE_WINDOW_SYSTEM */
30912 BEGV = obegv;
30913 ZV = ozv;
30914 current_buffer = obuf;
30915 SAFE_FREE ();
30918 set_cursor:
30919 define_frame_cursor1 (f, cursor, pointer);
30923 /* EXPORT for RIF:
30924 Clear any mouse-face on window W. This function is part of the
30925 redisplay interface, and is called from try_window_id and similar
30926 functions to ensure the mouse-highlight is off. */
30928 void
30929 x_clear_window_mouse_face (struct window *w)
30931 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30932 Lisp_Object window;
30934 block_input ();
30935 XSETWINDOW (window, w);
30936 if (EQ (window, hlinfo->mouse_face_window))
30937 clear_mouse_face (hlinfo);
30938 unblock_input ();
30942 /* EXPORT:
30943 Just discard the mouse face information for frame F, if any.
30944 This is used when the size of F is changed. */
30946 void
30947 cancel_mouse_face (struct frame *f)
30949 Lisp_Object window;
30950 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30952 window = hlinfo->mouse_face_window;
30953 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30954 reset_mouse_highlight (hlinfo);
30959 /***********************************************************************
30960 Exposure Events
30961 ***********************************************************************/
30963 #ifdef HAVE_WINDOW_SYSTEM
30965 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30966 which intersects rectangle R. R is in window-relative coordinates. */
30968 static void
30969 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30970 enum glyph_row_area area)
30972 struct glyph *first = row->glyphs[area];
30973 struct glyph *end = row->glyphs[area] + row->used[area];
30974 struct glyph *last;
30975 int first_x, start_x, x;
30977 if (area == TEXT_AREA && row->fill_line_p)
30978 /* If row extends face to end of line write the whole line. */
30979 draw_glyphs (w, 0, row, area,
30980 0, row->used[area],
30981 DRAW_NORMAL_TEXT, 0);
30982 else
30984 /* Set START_X to the window-relative start position for drawing glyphs of
30985 AREA. The first glyph of the text area can be partially visible.
30986 The first glyphs of other areas cannot. */
30987 start_x = window_box_left_offset (w, area);
30988 x = start_x;
30989 if (area == TEXT_AREA)
30990 x += row->x;
30992 /* Find the first glyph that must be redrawn. */
30993 while (first < end
30994 && x + first->pixel_width < r->x)
30996 x += first->pixel_width;
30997 ++first;
31000 /* Find the last one. */
31001 last = first;
31002 first_x = x;
31003 /* Use a signed int intermediate value to avoid catastrophic
31004 failures due to comparison between signed and unsigned, when
31005 x is negative (can happen for wide images that are hscrolled). */
31006 int r_end = r->x + r->width;
31007 while (last < end && x < r_end)
31009 x += last->pixel_width;
31010 ++last;
31013 /* Repaint. */
31014 if (last > first)
31015 draw_glyphs (w, first_x - start_x, row, area,
31016 first - row->glyphs[area], last - row->glyphs[area],
31017 DRAW_NORMAL_TEXT, 0);
31022 /* Redraw the parts of the glyph row ROW on window W intersecting
31023 rectangle R. R is in window-relative coordinates. Value is
31024 true if mouse-face was overwritten. */
31026 static bool
31027 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31029 eassert (row->enabled_p);
31031 if (row->mode_line_p || w->pseudo_window_p)
31032 draw_glyphs (w, 0, row, TEXT_AREA,
31033 0, row->used[TEXT_AREA],
31034 DRAW_NORMAL_TEXT, 0);
31035 else
31037 if (row->used[LEFT_MARGIN_AREA])
31038 expose_area (w, row, r, LEFT_MARGIN_AREA);
31039 if (row->used[TEXT_AREA])
31040 expose_area (w, row, r, TEXT_AREA);
31041 if (row->used[RIGHT_MARGIN_AREA])
31042 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31043 draw_row_fringe_bitmaps (w, row);
31046 return row->mouse_face_p;
31050 /* Redraw those parts of glyphs rows during expose event handling that
31051 overlap other rows. Redrawing of an exposed line writes over parts
31052 of lines overlapping that exposed line; this function fixes that.
31054 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31055 row in W's current matrix that is exposed and overlaps other rows.
31056 LAST_OVERLAPPING_ROW is the last such row. */
31058 static void
31059 expose_overlaps (struct window *w,
31060 struct glyph_row *first_overlapping_row,
31061 struct glyph_row *last_overlapping_row,
31062 XRectangle *r)
31064 struct glyph_row *row;
31066 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31067 if (row->overlapping_p)
31069 eassert (row->enabled_p && !row->mode_line_p);
31071 row->clip = r;
31072 if (row->used[LEFT_MARGIN_AREA])
31073 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31075 if (row->used[TEXT_AREA])
31076 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31078 if (row->used[RIGHT_MARGIN_AREA])
31079 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31080 row->clip = NULL;
31085 /* Return true if W's cursor intersects rectangle R. */
31087 static bool
31088 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31090 XRectangle cr, result;
31091 struct glyph *cursor_glyph;
31092 struct glyph_row *row;
31094 if (w->phys_cursor.vpos >= 0
31095 && w->phys_cursor.vpos < w->current_matrix->nrows
31096 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31097 row->enabled_p)
31098 && row->cursor_in_fringe_p)
31100 /* Cursor is in the fringe. */
31101 cr.x = window_box_right_offset (w,
31102 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31103 ? RIGHT_MARGIN_AREA
31104 : TEXT_AREA));
31105 cr.y = row->y;
31106 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31107 cr.height = row->height;
31108 return x_intersect_rectangles (&cr, r, &result);
31111 cursor_glyph = get_phys_cursor_glyph (w);
31112 if (cursor_glyph)
31114 /* r is relative to W's box, but w->phys_cursor.x is relative
31115 to left edge of W's TEXT area. Adjust it. */
31116 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31117 cr.y = w->phys_cursor.y;
31118 cr.width = cursor_glyph->pixel_width;
31119 cr.height = w->phys_cursor_height;
31120 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31121 I assume the effect is the same -- and this is portable. */
31122 return x_intersect_rectangles (&cr, r, &result);
31124 /* If we don't understand the format, pretend we're not in the hot-spot. */
31125 return false;
31129 /* EXPORT:
31130 Draw a vertical window border to the right of window W if W doesn't
31131 have vertical scroll bars. */
31133 void
31134 x_draw_vertical_border (struct window *w)
31136 struct frame *f = XFRAME (WINDOW_FRAME (w));
31138 /* We could do better, if we knew what type of scroll-bar the adjacent
31139 windows (on either side) have... But we don't :-(
31140 However, I think this works ok. ++KFS 2003-04-25 */
31142 /* Redraw borders between horizontally adjacent windows. Don't
31143 do it for frames with vertical scroll bars because either the
31144 right scroll bar of a window, or the left scroll bar of its
31145 neighbor will suffice as a border. */
31146 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31147 return;
31149 /* Note: It is necessary to redraw both the left and the right
31150 borders, for when only this single window W is being
31151 redisplayed. */
31152 if (!WINDOW_RIGHTMOST_P (w)
31153 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31155 int x0, x1, y0, y1;
31157 window_box_edges (w, &x0, &y0, &x1, &y1);
31158 y1 -= 1;
31160 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31161 x1 -= 1;
31163 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31166 if (!WINDOW_LEFTMOST_P (w)
31167 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31169 int x0, x1, y0, y1;
31171 window_box_edges (w, &x0, &y0, &x1, &y1);
31172 y1 -= 1;
31174 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31175 x0 -= 1;
31177 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31182 /* Draw window dividers for window W. */
31184 void
31185 x_draw_right_divider (struct window *w)
31187 struct frame *f = WINDOW_XFRAME (w);
31189 if (w->mini || w->pseudo_window_p)
31190 return;
31191 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31193 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31194 int x1 = WINDOW_RIGHT_EDGE_X (w);
31195 int y0 = WINDOW_TOP_EDGE_Y (w);
31196 /* The bottom divider prevails. */
31197 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31199 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31203 static void
31204 x_draw_bottom_divider (struct window *w)
31206 struct frame *f = XFRAME (WINDOW_FRAME (w));
31208 if (w->mini || w->pseudo_window_p)
31209 return;
31210 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31212 int x0 = WINDOW_LEFT_EDGE_X (w);
31213 int x1 = WINDOW_RIGHT_EDGE_X (w);
31214 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31215 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31217 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31221 /* Redraw the part of window W intersection rectangle FR. Pixel
31222 coordinates in FR are frame-relative. Call this function with
31223 input blocked. Value is true if the exposure overwrites
31224 mouse-face. */
31226 static bool
31227 expose_window (struct window *w, XRectangle *fr)
31229 struct frame *f = XFRAME (w->frame);
31230 XRectangle wr, r;
31231 bool mouse_face_overwritten_p = false;
31233 /* If window is not yet fully initialized, do nothing. This can
31234 happen when toolkit scroll bars are used and a window is split.
31235 Reconfiguring the scroll bar will generate an expose for a newly
31236 created window. */
31237 if (w->current_matrix == NULL)
31238 return false;
31240 /* When we're currently updating the window, display and current
31241 matrix usually don't agree. Arrange for a thorough display
31242 later. */
31243 if (w->must_be_updated_p)
31245 SET_FRAME_GARBAGED (f);
31246 return false;
31249 /* Frame-relative pixel rectangle of W. */
31250 wr.x = WINDOW_LEFT_EDGE_X (w);
31251 wr.y = WINDOW_TOP_EDGE_Y (w);
31252 wr.width = WINDOW_PIXEL_WIDTH (w);
31253 wr.height = WINDOW_PIXEL_HEIGHT (w);
31255 if (x_intersect_rectangles (fr, &wr, &r))
31257 int yb = window_text_bottom_y (w);
31258 struct glyph_row *row;
31259 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31261 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31262 r.x, r.y, r.width, r.height));
31264 /* Convert to window coordinates. */
31265 r.x -= WINDOW_LEFT_EDGE_X (w);
31266 r.y -= WINDOW_TOP_EDGE_Y (w);
31268 /* Turn off the cursor. */
31269 bool cursor_cleared_p = (!w->pseudo_window_p
31270 && phys_cursor_in_rect_p (w, &r));
31271 if (cursor_cleared_p)
31272 x_clear_cursor (w);
31274 /* If the row containing the cursor extends face to end of line,
31275 then expose_area might overwrite the cursor outside the
31276 rectangle and thus notice_overwritten_cursor might clear
31277 w->phys_cursor_on_p. We remember the original value and
31278 check later if it is changed. */
31279 bool phys_cursor_on_p = w->phys_cursor_on_p;
31281 /* Use a signed int intermediate value to avoid catastrophic
31282 failures due to comparison between signed and unsigned, when
31283 y0 or y1 is negative (can happen for tall images). */
31284 int r_bottom = r.y + r.height;
31286 /* Update lines intersecting rectangle R. */
31287 first_overlapping_row = last_overlapping_row = NULL;
31288 for (row = w->current_matrix->rows;
31289 row->enabled_p;
31290 ++row)
31292 int y0 = row->y;
31293 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31295 if ((y0 >= r.y && y0 < r_bottom)
31296 || (y1 > r.y && y1 < r_bottom)
31297 || (r.y >= y0 && r.y < y1)
31298 || (r_bottom > y0 && r_bottom < y1))
31300 /* A header line may be overlapping, but there is no need
31301 to fix overlapping areas for them. KFS 2005-02-12 */
31302 if (row->overlapping_p && !row->mode_line_p)
31304 if (first_overlapping_row == NULL)
31305 first_overlapping_row = row;
31306 last_overlapping_row = row;
31309 row->clip = fr;
31310 if (expose_line (w, row, &r))
31311 mouse_face_overwritten_p = true;
31312 row->clip = NULL;
31314 else if (row->overlapping_p)
31316 /* We must redraw a row overlapping the exposed area. */
31317 if (y0 < r.y
31318 ? y0 + row->phys_height > r.y
31319 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31321 if (first_overlapping_row == NULL)
31322 first_overlapping_row = row;
31323 last_overlapping_row = row;
31327 if (y1 >= yb)
31328 break;
31331 /* Display the mode line if there is one. */
31332 if (WINDOW_WANTS_MODELINE_P (w)
31333 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31334 row->enabled_p)
31335 && row->y < r_bottom)
31337 if (expose_line (w, row, &r))
31338 mouse_face_overwritten_p = true;
31341 if (!w->pseudo_window_p)
31343 /* Fix the display of overlapping rows. */
31344 if (first_overlapping_row)
31345 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31346 fr);
31348 /* Draw border between windows. */
31349 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31350 x_draw_right_divider (w);
31351 else
31352 x_draw_vertical_border (w);
31354 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31355 x_draw_bottom_divider (w);
31357 /* Turn the cursor on again. */
31358 if (cursor_cleared_p
31359 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31360 update_window_cursor (w, true);
31364 return mouse_face_overwritten_p;
31369 /* Redraw (parts) of all windows in the window tree rooted at W that
31370 intersect R. R contains frame pixel coordinates. Value is
31371 true if the exposure overwrites mouse-face. */
31373 static bool
31374 expose_window_tree (struct window *w, XRectangle *r)
31376 struct frame *f = XFRAME (w->frame);
31377 bool mouse_face_overwritten_p = false;
31379 while (w && !FRAME_GARBAGED_P (f))
31381 mouse_face_overwritten_p
31382 |= (WINDOWP (w->contents)
31383 ? expose_window_tree (XWINDOW (w->contents), r)
31384 : expose_window (w, r));
31386 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31389 return mouse_face_overwritten_p;
31393 /* EXPORT:
31394 Redisplay an exposed area of frame F. X and Y are the upper-left
31395 corner of the exposed rectangle. W and H are width and height of
31396 the exposed area. All are pixel values. W or H zero means redraw
31397 the entire frame. */
31399 void
31400 expose_frame (struct frame *f, int x, int y, int w, int h)
31402 XRectangle r;
31403 bool mouse_face_overwritten_p = false;
31405 TRACE ((stderr, "expose_frame "));
31407 /* No need to redraw if frame will be redrawn soon. */
31408 if (FRAME_GARBAGED_P (f))
31410 TRACE ((stderr, " garbaged\n"));
31411 return;
31414 /* If basic faces haven't been realized yet, there is no point in
31415 trying to redraw anything. This can happen when we get an expose
31416 event while Emacs is starting, e.g. by moving another window. */
31417 if (FRAME_FACE_CACHE (f) == NULL
31418 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31420 TRACE ((stderr, " no faces\n"));
31421 return;
31424 if (w == 0 || h == 0)
31426 r.x = r.y = 0;
31427 r.width = FRAME_TEXT_WIDTH (f);
31428 r.height = FRAME_TEXT_HEIGHT (f);
31430 else
31432 r.x = x;
31433 r.y = y;
31434 r.width = w;
31435 r.height = h;
31438 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31439 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31441 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31442 if (WINDOWP (f->tool_bar_window))
31443 mouse_face_overwritten_p
31444 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31445 #endif
31447 #ifdef HAVE_X_WINDOWS
31448 #ifndef MSDOS
31449 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31450 if (WINDOWP (f->menu_bar_window))
31451 mouse_face_overwritten_p
31452 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31453 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31454 #endif
31455 #endif
31457 /* Some window managers support a focus-follows-mouse style with
31458 delayed raising of frames. Imagine a partially obscured frame,
31459 and moving the mouse into partially obscured mouse-face on that
31460 frame. The visible part of the mouse-face will be highlighted,
31461 then the WM raises the obscured frame. With at least one WM, KDE
31462 2.1, Emacs is not getting any event for the raising of the frame
31463 (even tried with SubstructureRedirectMask), only Expose events.
31464 These expose events will draw text normally, i.e. not
31465 highlighted. Which means we must redo the highlight here.
31466 Subsume it under ``we love X''. --gerd 2001-08-15 */
31467 /* Included in Windows version because Windows most likely does not
31468 do the right thing if any third party tool offers
31469 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31470 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31472 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31473 if (f == hlinfo->mouse_face_mouse_frame)
31475 int mouse_x = hlinfo->mouse_face_mouse_x;
31476 int mouse_y = hlinfo->mouse_face_mouse_y;
31477 clear_mouse_face (hlinfo);
31478 note_mouse_highlight (f, mouse_x, mouse_y);
31484 /* EXPORT:
31485 Determine the intersection of two rectangles R1 and R2. Return
31486 the intersection in *RESULT. Value is true if RESULT is not
31487 empty. */
31489 bool
31490 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31492 XRectangle *left, *right;
31493 XRectangle *upper, *lower;
31494 bool intersection_p = false;
31496 /* Rearrange so that R1 is the left-most rectangle. */
31497 if (r1->x < r2->x)
31498 left = r1, right = r2;
31499 else
31500 left = r2, right = r1;
31502 /* X0 of the intersection is right.x0, if this is inside R1,
31503 otherwise there is no intersection. */
31504 if (right->x <= left->x + left->width)
31506 result->x = right->x;
31508 /* The right end of the intersection is the minimum of
31509 the right ends of left and right. */
31510 result->width = (min (left->x + left->width, right->x + right->width)
31511 - result->x);
31513 /* Same game for Y. */
31514 if (r1->y < r2->y)
31515 upper = r1, lower = r2;
31516 else
31517 upper = r2, lower = r1;
31519 /* The upper end of the intersection is lower.y0, if this is inside
31520 of upper. Otherwise, there is no intersection. */
31521 if (lower->y <= upper->y + upper->height)
31523 result->y = lower->y;
31525 /* The lower end of the intersection is the minimum of the lower
31526 ends of upper and lower. */
31527 result->height = (min (lower->y + lower->height,
31528 upper->y + upper->height)
31529 - result->y);
31530 intersection_p = true;
31534 return intersection_p;
31537 #endif /* HAVE_WINDOW_SYSTEM */
31540 /***********************************************************************
31541 Initialization
31542 ***********************************************************************/
31544 void
31545 syms_of_xdisp (void)
31547 Vwith_echo_area_save_vector = Qnil;
31548 staticpro (&Vwith_echo_area_save_vector);
31550 Vmessage_stack = Qnil;
31551 staticpro (&Vmessage_stack);
31553 /* Non-nil means don't actually do any redisplay. */
31554 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31556 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31558 DEFVAR_BOOL("inhibit-message", inhibit_message,
31559 doc: /* Non-nil means calls to `message' are not displayed.
31560 They are still logged to the *Messages* buffer. */);
31561 inhibit_message = 0;
31563 message_dolog_marker1 = Fmake_marker ();
31564 staticpro (&message_dolog_marker1);
31565 message_dolog_marker2 = Fmake_marker ();
31566 staticpro (&message_dolog_marker2);
31567 message_dolog_marker3 = Fmake_marker ();
31568 staticpro (&message_dolog_marker3);
31570 defsubr (&Sset_buffer_redisplay);
31571 #ifdef GLYPH_DEBUG
31572 defsubr (&Sdump_frame_glyph_matrix);
31573 defsubr (&Sdump_glyph_matrix);
31574 defsubr (&Sdump_glyph_row);
31575 defsubr (&Sdump_tool_bar_row);
31576 defsubr (&Strace_redisplay);
31577 defsubr (&Strace_to_stderr);
31578 #endif
31579 #ifdef HAVE_WINDOW_SYSTEM
31580 defsubr (&Stool_bar_height);
31581 defsubr (&Slookup_image_map);
31582 #endif
31583 defsubr (&Sline_pixel_height);
31584 defsubr (&Sformat_mode_line);
31585 defsubr (&Sinvisible_p);
31586 defsubr (&Scurrent_bidi_paragraph_direction);
31587 defsubr (&Swindow_text_pixel_size);
31588 defsubr (&Smove_point_visually);
31589 defsubr (&Sbidi_find_overridden_directionality);
31591 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31592 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31593 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31594 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31595 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31596 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31597 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31598 DEFSYM (Qeval, "eval");
31599 DEFSYM (QCdata, ":data");
31601 /* Names of text properties relevant for redisplay. */
31602 DEFSYM (Qdisplay, "display");
31603 DEFSYM (Qspace_width, "space-width");
31604 DEFSYM (Qraise, "raise");
31605 DEFSYM (Qslice, "slice");
31606 DEFSYM (Qspace, "space");
31607 DEFSYM (Qmargin, "margin");
31608 DEFSYM (Qpointer, "pointer");
31609 DEFSYM (Qleft_margin, "left-margin");
31610 DEFSYM (Qright_margin, "right-margin");
31611 DEFSYM (Qcenter, "center");
31612 DEFSYM (Qline_height, "line-height");
31613 DEFSYM (QCalign_to, ":align-to");
31614 DEFSYM (QCrelative_width, ":relative-width");
31615 DEFSYM (QCrelative_height, ":relative-height");
31616 DEFSYM (QCeval, ":eval");
31617 DEFSYM (QCpropertize, ":propertize");
31618 DEFSYM (QCfile, ":file");
31619 DEFSYM (Qfontified, "fontified");
31620 DEFSYM (Qfontification_functions, "fontification-functions");
31622 /* Name of the face used to highlight trailing whitespace. */
31623 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31625 /* Name and number of the face used to highlight escape glyphs. */
31626 DEFSYM (Qescape_glyph, "escape-glyph");
31628 /* Name and number of the face used to highlight non-breaking
31629 spaces/hyphens. */
31630 DEFSYM (Qnobreak_space, "nobreak-space");
31631 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31633 /* The symbol 'image' which is the car of the lists used to represent
31634 images in Lisp. Also a tool bar style. */
31635 DEFSYM (Qimage, "image");
31637 /* Tool bar styles. */
31638 DEFSYM (Qtext, "text");
31639 DEFSYM (Qboth, "both");
31640 DEFSYM (Qboth_horiz, "both-horiz");
31641 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31643 /* The image map types. */
31644 DEFSYM (QCmap, ":map");
31645 DEFSYM (QCpointer, ":pointer");
31646 DEFSYM (Qrect, "rect");
31647 DEFSYM (Qcircle, "circle");
31648 DEFSYM (Qpoly, "poly");
31650 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31652 DEFSYM (Qgrow_only, "grow-only");
31653 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31654 DEFSYM (Qposition, "position");
31655 DEFSYM (Qbuffer_position, "buffer-position");
31656 DEFSYM (Qobject, "object");
31658 /* Cursor shapes. */
31659 DEFSYM (Qbar, "bar");
31660 DEFSYM (Qhbar, "hbar");
31661 DEFSYM (Qbox, "box");
31662 DEFSYM (Qhollow, "hollow");
31664 /* Pointer shapes. */
31665 DEFSYM (Qhand, "hand");
31666 DEFSYM (Qarrow, "arrow");
31667 /* also Qtext */
31669 DEFSYM (Qdragging, "dragging");
31671 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31673 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31674 staticpro (&list_of_error);
31676 /* Values of those variables at last redisplay are stored as
31677 properties on 'overlay-arrow-position' symbol. However, if
31678 Voverlay_arrow_position is a marker, last-arrow-position is its
31679 numerical position. */
31680 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31681 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31683 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31684 properties on a symbol in overlay-arrow-variable-list. */
31685 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31686 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31688 echo_buffer[0] = echo_buffer[1] = Qnil;
31689 staticpro (&echo_buffer[0]);
31690 staticpro (&echo_buffer[1]);
31692 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31693 staticpro (&echo_area_buffer[0]);
31694 staticpro (&echo_area_buffer[1]);
31696 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31697 staticpro (&Vmessages_buffer_name);
31699 mode_line_proptrans_alist = Qnil;
31700 staticpro (&mode_line_proptrans_alist);
31701 mode_line_string_list = Qnil;
31702 staticpro (&mode_line_string_list);
31703 mode_line_string_face = Qnil;
31704 staticpro (&mode_line_string_face);
31705 mode_line_string_face_prop = Qnil;
31706 staticpro (&mode_line_string_face_prop);
31707 Vmode_line_unwind_vector = Qnil;
31708 staticpro (&Vmode_line_unwind_vector);
31710 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31712 help_echo_string = Qnil;
31713 staticpro (&help_echo_string);
31714 help_echo_object = Qnil;
31715 staticpro (&help_echo_object);
31716 help_echo_window = Qnil;
31717 staticpro (&help_echo_window);
31718 previous_help_echo_string = Qnil;
31719 staticpro (&previous_help_echo_string);
31720 help_echo_pos = -1;
31722 DEFSYM (Qright_to_left, "right-to-left");
31723 DEFSYM (Qleft_to_right, "left-to-right");
31724 defsubr (&Sbidi_resolved_levels);
31726 #ifdef HAVE_WINDOW_SYSTEM
31727 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31728 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31729 For example, if a block cursor is over a tab, it will be drawn as
31730 wide as that tab on the display. */);
31731 x_stretch_cursor_p = 0;
31732 #endif
31734 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31735 doc: /* Non-nil means highlight trailing whitespace.
31736 The face used for trailing whitespace is `trailing-whitespace'. */);
31737 Vshow_trailing_whitespace = Qnil;
31739 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31740 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31741 If the value is t, Emacs highlights non-ASCII chars which have the
31742 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31743 or `nobreak-hyphen' face respectively.
31745 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31746 U+2011 (non-breaking hyphen) are affected.
31748 Any other non-nil value means to display these characters as a escape
31749 glyph followed by an ordinary space or hyphen.
31751 A value of nil means no special handling of these characters. */);
31752 Vnobreak_char_display = Qt;
31754 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31755 doc: /* The pointer shape to show in void text areas.
31756 A value of nil means to show the text pointer. Other options are
31757 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31758 `hourglass'. */);
31759 Vvoid_text_area_pointer = Qarrow;
31761 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31762 doc: /* Non-nil means don't actually do any redisplay.
31763 This is used for internal purposes. */);
31764 Vinhibit_redisplay = Qnil;
31766 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31767 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31768 Vglobal_mode_string = Qnil;
31770 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31771 doc: /* Marker for where to display an arrow on top of the buffer text.
31772 This must be the beginning of a line in order to work.
31773 See also `overlay-arrow-string'. */);
31774 Voverlay_arrow_position = Qnil;
31776 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31777 doc: /* String to display as an arrow in non-window frames.
31778 See also `overlay-arrow-position'. */);
31779 Voverlay_arrow_string = build_pure_c_string ("=>");
31781 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31782 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31783 The symbols on this list are examined during redisplay to determine
31784 where to display overlay arrows. */);
31785 Voverlay_arrow_variable_list
31786 = list1 (intern_c_string ("overlay-arrow-position"));
31788 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31789 doc: /* The number of lines to try scrolling a window by when point moves out.
31790 If that fails to bring point back on frame, point is centered instead.
31791 If this is zero, point is always centered after it moves off frame.
31792 If you want scrolling to always be a line at a time, you should set
31793 `scroll-conservatively' to a large value rather than set this to 1. */);
31795 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31796 doc: /* Scroll up to this many lines, to bring point back on screen.
31797 If point moves off-screen, redisplay will scroll by up to
31798 `scroll-conservatively' lines in order to bring point just barely
31799 onto the screen again. If that cannot be done, then redisplay
31800 recenters point as usual.
31802 If the value is greater than 100, redisplay will never recenter point,
31803 but will always scroll just enough text to bring point into view, even
31804 if you move far away.
31806 A value of zero means always recenter point if it moves off screen. */);
31807 scroll_conservatively = 0;
31809 DEFVAR_INT ("scroll-margin", scroll_margin,
31810 doc: /* Number of lines of margin at the top and bottom of a window.
31811 Recenter the window whenever point gets within this many lines
31812 of the top or bottom of the window. */);
31813 scroll_margin = 0;
31815 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
31816 doc: /* Maximum effective value of `scroll-margin'.
31817 Given as a fraction of the current window's lines. The value should
31818 be a floating point number between 0.0 and 0.5. The effective maximum
31819 is limited to (/ (1- window-lines) 2). Non-float values for this
31820 variable are ignored and the default 0.25 is used instead. */);
31821 Vmaximum_scroll_margin = make_float (0.25);
31823 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31824 doc: /* Pixels per inch value for non-window system displays.
31825 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31826 Vdisplay_pixels_per_inch = make_float (72.0);
31828 #ifdef GLYPH_DEBUG
31829 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31830 #endif
31832 DEFVAR_LISP ("truncate-partial-width-windows",
31833 Vtruncate_partial_width_windows,
31834 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31835 For an integer value, truncate lines in each window narrower than the
31836 full frame width, provided the total window width in column units is less
31837 than that integer; otherwise, respect the value of `truncate-lines'.
31838 The total width of the window is as returned by `window-total-width', it
31839 includes the fringes, the continuation and truncation glyphs, the
31840 display margins (if any), and the scroll bar
31842 For any other non-nil value, truncate lines in all windows that do
31843 not span the full frame width.
31845 A value of nil means to respect the value of `truncate-lines'.
31847 If `word-wrap' is enabled, you might want to reduce this. */);
31848 Vtruncate_partial_width_windows = make_number (50);
31850 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31851 doc: /* Maximum buffer size for which line number should be displayed.
31852 If the buffer is bigger than this, the line number does not appear
31853 in the mode line. A value of nil means no limit. */);
31854 Vline_number_display_limit = Qnil;
31856 DEFVAR_INT ("line-number-display-limit-width",
31857 line_number_display_limit_width,
31858 doc: /* Maximum line width (in characters) for line number display.
31859 If the average length of the lines near point is bigger than this, then the
31860 line number may be omitted from the mode line. */);
31861 line_number_display_limit_width = 200;
31863 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31864 doc: /* Non-nil means highlight region even in nonselected windows. */);
31865 highlight_nonselected_windows = false;
31867 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31868 doc: /* Non-nil if more than one frame is visible on this display.
31869 Minibuffer-only frames don't count, but iconified frames do.
31870 This variable is not guaranteed to be accurate except while processing
31871 `frame-title-format' and `icon-title-format'. */);
31873 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31874 doc: /* Template for displaying the title bar of visible frames.
31875 \(Assuming the window manager supports this feature.)
31877 This variable has the same structure as `mode-line-format', except that
31878 the %c, %C, and %l constructs are ignored. It is used only on frames for
31879 which no explicit name has been set (see `modify-frame-parameters'). */);
31881 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31882 doc: /* Template for displaying the title bar of an iconified frame.
31883 \(Assuming the window manager supports this feature.)
31884 This variable has the same structure as `mode-line-format' (which see),
31885 and is used only on frames for which no explicit name has been set
31886 \(see `modify-frame-parameters'). */);
31887 Vicon_title_format
31888 = Vframe_title_format
31889 = listn (CONSTYPE_PURE, 3,
31890 intern_c_string ("multiple-frames"),
31891 build_pure_c_string ("%b"),
31892 listn (CONSTYPE_PURE, 4,
31893 empty_unibyte_string,
31894 intern_c_string ("invocation-name"),
31895 build_pure_c_string ("@"),
31896 intern_c_string ("system-name")));
31898 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31899 doc: /* Maximum number of lines to keep in the message log buffer.
31900 If nil, disable message logging. If t, log messages but don't truncate
31901 the buffer when it becomes large. */);
31902 Vmessage_log_max = make_number (1000);
31904 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31905 doc: /* List of functions to call before redisplaying a window with scrolling.
31906 Each function is called with two arguments, the window and its new
31907 display-start position.
31908 These functions are called whenever the `window-start' marker is modified,
31909 either to point into another buffer (e.g. via `set-window-buffer') or another
31910 place in the same buffer.
31911 Note that the value of `window-end' is not valid when these functions are
31912 called.
31914 Warning: Do not use this feature to alter the way the window
31915 is scrolled. It is not designed for that, and such use probably won't
31916 work. */);
31917 Vwindow_scroll_functions = Qnil;
31919 DEFVAR_LISP ("window-text-change-functions",
31920 Vwindow_text_change_functions,
31921 doc: /* Functions to call in redisplay when text in the window might change. */);
31922 Vwindow_text_change_functions = Qnil;
31924 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31925 doc: /* Functions called when redisplay of a window reaches the end trigger.
31926 Each function is called with two arguments, the window and the end trigger value.
31927 See `set-window-redisplay-end-trigger'. */);
31928 Vredisplay_end_trigger_functions = Qnil;
31930 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31931 doc: /* Non-nil means autoselect window with mouse pointer.
31932 If nil, do not autoselect windows.
31933 A positive number means delay autoselection by that many seconds: a
31934 window is autoselected only after the mouse has remained in that
31935 window for the duration of the delay.
31936 A negative number has a similar effect, but causes windows to be
31937 autoselected only after the mouse has stopped moving. (Because of
31938 the way Emacs compares mouse events, you will occasionally wait twice
31939 that time before the window gets selected.)
31940 Any other value means to autoselect window instantaneously when the
31941 mouse pointer enters it.
31943 Autoselection selects the minibuffer only if it is active, and never
31944 unselects the minibuffer if it is active.
31946 When customizing this variable make sure that the actual value of
31947 `focus-follows-mouse' matches the behavior of your window manager. */);
31948 Vmouse_autoselect_window = Qnil;
31950 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31951 doc: /* Non-nil means automatically resize tool-bars.
31952 This dynamically changes the tool-bar's height to the minimum height
31953 that is needed to make all tool-bar items visible.
31954 If value is `grow-only', the tool-bar's height is only increased
31955 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31956 Vauto_resize_tool_bars = Qt;
31958 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31959 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31960 auto_raise_tool_bar_buttons_p = true;
31962 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31963 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31964 make_cursor_line_fully_visible_p = true;
31966 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31967 doc: /* Border below tool-bar in pixels.
31968 If an integer, use it as the height of the border.
31969 If it is one of `internal-border-width' or `border-width', use the
31970 value of the corresponding frame parameter.
31971 Otherwise, no border is added below the tool-bar. */);
31972 Vtool_bar_border = Qinternal_border_width;
31974 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31975 doc: /* Margin around tool-bar buttons in pixels.
31976 If an integer, use that for both horizontal and vertical margins.
31977 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31978 HORZ specifying the horizontal margin, and VERT specifying the
31979 vertical margin. */);
31980 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31982 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31983 doc: /* Relief thickness of tool-bar buttons. */);
31984 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31986 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31987 doc: /* Tool bar style to use.
31988 It can be one of
31989 image - show images only
31990 text - show text only
31991 both - show both, text below image
31992 both-horiz - show text to the right of the image
31993 text-image-horiz - show text to the left of the image
31994 any other - use system default or image if no system default.
31996 This variable only affects the GTK+ toolkit version of Emacs. */);
31997 Vtool_bar_style = Qnil;
31999 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32000 doc: /* Maximum number of characters a label can have to be shown.
32001 The tool bar style must also show labels for this to have any effect, see
32002 `tool-bar-style'. */);
32003 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32005 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32006 doc: /* List of functions to call to fontify regions of text.
32007 Each function is called with one argument POS. Functions must
32008 fontify a region starting at POS in the current buffer, and give
32009 fontified regions the property `fontified'. */);
32010 Vfontification_functions = Qnil;
32011 Fmake_variable_buffer_local (Qfontification_functions);
32013 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32014 unibyte_display_via_language_environment,
32015 doc: /* Non-nil means display unibyte text according to language environment.
32016 Specifically, this means that raw bytes in the range 160-255 decimal
32017 are displayed by converting them to the equivalent multibyte characters
32018 according to the current language environment. As a result, they are
32019 displayed according to the current fontset.
32021 Note that this variable affects only how these bytes are displayed,
32022 but does not change the fact they are interpreted as raw bytes. */);
32023 unibyte_display_via_language_environment = false;
32025 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32026 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32027 If a float, it specifies a fraction of the mini-window frame's height.
32028 If an integer, it specifies a number of lines. */);
32029 Vmax_mini_window_height = make_float (0.25);
32031 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32032 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32033 A value of nil means don't automatically resize mini-windows.
32034 A value of t means resize them to fit the text displayed in them.
32035 A value of `grow-only', the default, means let mini-windows grow only;
32036 they return to their normal size when the minibuffer is closed, or the
32037 echo area becomes empty. */);
32038 /* Contrary to the doc string, we initialize this to nil, so that
32039 loading loadup.el won't try to resize windows before loading
32040 window.el, where some functions we need to call for this live.
32041 We assign the 'grow-only' value right after loading window.el
32042 during loadup. */
32043 Vresize_mini_windows = Qnil;
32045 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32046 doc: /* Alist specifying how to blink the cursor off.
32047 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32048 `cursor-type' frame-parameter or variable equals ON-STATE,
32049 comparing using `equal', Emacs uses OFF-STATE to specify
32050 how to blink it off. ON-STATE and OFF-STATE are values for
32051 the `cursor-type' frame parameter.
32053 If a frame's ON-STATE has no entry in this list,
32054 the frame's other specifications determine how to blink the cursor off. */);
32055 Vblink_cursor_alist = Qnil;
32057 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32058 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32059 The value `current-line' means the line displaying point in each window
32060 is automatically scrolled horizontally to make point visible.
32061 Any other non-nil value means all the lines in a window are automatically
32062 scrolled horizontally to make point visible. */);
32063 automatic_hscrolling = Qt;
32064 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32065 DEFSYM (Qcurrent_line, "current-line");
32067 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32068 doc: /* How many columns away from the window edge point is allowed to get
32069 before automatic hscrolling will horizontally scroll the window. */);
32070 hscroll_margin = 5;
32072 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32073 doc: /* How many columns to scroll the window when point gets too close to the edge.
32074 When point is less than `hscroll-margin' columns from the window
32075 edge, automatic hscrolling will scroll the window by the amount of columns
32076 determined by this variable. If its value is a positive integer, scroll that
32077 many columns. If it's a positive floating-point number, it specifies the
32078 fraction of the window's width to scroll. If it's nil or zero, point will be
32079 centered horizontally after the scroll. Any other value, including negative
32080 numbers, are treated as if the value were zero.
32082 Automatic hscrolling always moves point outside the scroll margin, so if
32083 point was more than scroll step columns inside the margin, the window will
32084 scroll more than the value given by the scroll step.
32086 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32087 and `scroll-right' overrides this variable's effect. */);
32088 Vhscroll_step = make_number (0);
32090 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32091 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32092 Bind this around calls to `message' to let it take effect. */);
32093 message_truncate_lines = false;
32095 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32096 doc: /* Normal hook run to update the menu bar definitions.
32097 Redisplay runs this hook before it redisplays the menu bar.
32098 This is used to update menus such as Buffers, whose contents depend on
32099 various data. */);
32100 Vmenu_bar_update_hook = Qnil;
32102 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32103 doc: /* Frame for which we are updating a menu.
32104 The enable predicate for a menu binding should check this variable. */);
32105 Vmenu_updating_frame = Qnil;
32107 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32108 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32109 inhibit_menubar_update = false;
32111 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32112 doc: /* Prefix prepended to all continuation lines at display time.
32113 The value may be a string, an image, or a stretch-glyph; it is
32114 interpreted in the same way as the value of a `display' text property.
32116 This variable is overridden by any `wrap-prefix' text or overlay
32117 property.
32119 To add a prefix to non-continuation lines, use `line-prefix'. */);
32120 Vwrap_prefix = Qnil;
32121 DEFSYM (Qwrap_prefix, "wrap-prefix");
32122 Fmake_variable_buffer_local (Qwrap_prefix);
32124 DEFVAR_LISP ("line-prefix", Vline_prefix,
32125 doc: /* Prefix prepended to all non-continuation lines at display time.
32126 The value may be a string, an image, or a stretch-glyph; it is
32127 interpreted in the same way as the value of a `display' text property.
32129 This variable is overridden by any `line-prefix' text or overlay
32130 property.
32132 To add a prefix to continuation lines, use `wrap-prefix'. */);
32133 Vline_prefix = Qnil;
32134 DEFSYM (Qline_prefix, "line-prefix");
32135 Fmake_variable_buffer_local (Qline_prefix);
32137 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32138 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32139 inhibit_eval_during_redisplay = false;
32141 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32142 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32143 inhibit_free_realized_faces = false;
32145 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32146 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32147 Intended for use during debugging and for testing bidi display;
32148 see biditest.el in the test suite. */);
32149 inhibit_bidi_mirroring = false;
32151 #ifdef GLYPH_DEBUG
32152 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32153 doc: /* Inhibit try_window_id display optimization. */);
32154 inhibit_try_window_id = false;
32156 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32157 doc: /* Inhibit try_window_reusing display optimization. */);
32158 inhibit_try_window_reusing = false;
32160 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32161 doc: /* Inhibit try_cursor_movement display optimization. */);
32162 inhibit_try_cursor_movement = false;
32163 #endif /* GLYPH_DEBUG */
32165 DEFVAR_INT ("overline-margin", overline_margin,
32166 doc: /* Space between overline and text, in pixels.
32167 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32168 margin to the character height. */);
32169 overline_margin = 2;
32171 DEFVAR_INT ("underline-minimum-offset",
32172 underline_minimum_offset,
32173 doc: /* Minimum distance between baseline and underline.
32174 This can improve legibility of underlined text at small font sizes,
32175 particularly when using variable `x-use-underline-position-properties'
32176 with fonts that specify an UNDERLINE_POSITION relatively close to the
32177 baseline. The default value is 1. */);
32178 underline_minimum_offset = 1;
32180 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32181 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32182 This feature only works when on a window system that can change
32183 cursor shapes. */);
32184 display_hourglass_p = true;
32186 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32187 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32188 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32190 #ifdef HAVE_WINDOW_SYSTEM
32191 hourglass_atimer = NULL;
32192 hourglass_shown_p = false;
32193 #endif /* HAVE_WINDOW_SYSTEM */
32195 /* Name of the face used to display glyphless characters. */
32196 DEFSYM (Qglyphless_char, "glyphless-char");
32198 /* Method symbols for Vglyphless_char_display. */
32199 DEFSYM (Qhex_code, "hex-code");
32200 DEFSYM (Qempty_box, "empty-box");
32201 DEFSYM (Qthin_space, "thin-space");
32202 DEFSYM (Qzero_width, "zero-width");
32204 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32205 doc: /* Function run just before redisplay.
32206 It is called with one argument, which is the set of windows that are to
32207 be redisplayed. This set can be nil (meaning, only the selected window),
32208 or t (meaning all windows). */);
32209 Vpre_redisplay_function = intern ("ignore");
32211 /* Symbol for the purpose of Vglyphless_char_display. */
32212 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32213 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32215 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32216 doc: /* Char-table defining glyphless characters.
32217 Each element, if non-nil, should be one of the following:
32218 an ASCII acronym string: display this string in a box
32219 `hex-code': display the hexadecimal code of a character in a box
32220 `empty-box': display as an empty box
32221 `thin-space': display as 1-pixel width space
32222 `zero-width': don't display
32223 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32224 display method for graphical terminals and text terminals respectively.
32225 GRAPHICAL and TEXT should each have one of the values listed above.
32227 The char-table has one extra slot to control the display of a character for
32228 which no font is found. This slot only takes effect on graphical terminals.
32229 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32230 `thin-space'. The default is `empty-box'.
32232 If a character has a non-nil entry in an active display table, the
32233 display table takes effect; in this case, Emacs does not consult
32234 `glyphless-char-display' at all. */);
32235 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32236 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32237 Qempty_box);
32239 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32240 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32241 Vdebug_on_message = Qnil;
32243 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32244 doc: /* */);
32245 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32247 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32248 doc: /* */);
32249 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32251 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32252 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32253 /* Initialize to t, since we need to disable reordering until
32254 loadup.el successfully loads charprop.el. */
32255 redisplay__inhibit_bidi = true;
32257 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
32258 doc: /* Non-nil means display raw bytes in hexadecimal format.
32259 The default is to use octal format (\200) whereas hexadecimal (\x80)
32260 may be more familiar to users. */);
32261 display_raw_bytes_as_hex = false;
32266 /* Initialize this module when Emacs starts. */
32268 void
32269 init_xdisp (void)
32271 CHARPOS (this_line_start_pos) = 0;
32273 if (!noninteractive)
32275 struct window *m = XWINDOW (minibuf_window);
32276 Lisp_Object frame = m->frame;
32277 struct frame *f = XFRAME (frame);
32278 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32279 struct window *r = XWINDOW (root);
32280 int i;
32282 echo_area_window = minibuf_window;
32284 r->top_line = FRAME_TOP_MARGIN (f);
32285 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32286 r->total_cols = FRAME_COLS (f);
32287 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32288 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32289 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32291 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32292 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32293 m->total_cols = FRAME_COLS (f);
32294 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32295 m->total_lines = 1;
32296 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32298 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32299 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32300 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32302 /* The default ellipsis glyphs `...'. */
32303 for (i = 0; i < 3; ++i)
32304 default_invis_vector[i] = make_number ('.');
32308 /* Allocate the buffer for frame titles.
32309 Also used for `format-mode-line'. */
32310 int size = 100;
32311 mode_line_noprop_buf = xmalloc (size);
32312 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32313 mode_line_noprop_ptr = mode_line_noprop_buf;
32314 mode_line_target = MODE_LINE_DISPLAY;
32317 help_echo_showing_p = false;
32320 #ifdef HAVE_WINDOW_SYSTEM
32322 /* Platform-independent portion of hourglass implementation. */
32324 /* Timer function of hourglass_atimer. */
32326 static void
32327 show_hourglass (struct atimer *timer)
32329 /* The timer implementation will cancel this timer automatically
32330 after this function has run. Set hourglass_atimer to null
32331 so that we know the timer doesn't have to be canceled. */
32332 hourglass_atimer = NULL;
32334 if (!hourglass_shown_p)
32336 Lisp_Object tail, frame;
32338 block_input ();
32340 FOR_EACH_FRAME (tail, frame)
32342 struct frame *f = XFRAME (frame);
32344 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32345 && FRAME_RIF (f)->show_hourglass)
32346 FRAME_RIF (f)->show_hourglass (f);
32349 hourglass_shown_p = true;
32350 unblock_input ();
32354 /* Cancel a currently active hourglass timer, and start a new one. */
32356 void
32357 start_hourglass (void)
32359 struct timespec delay;
32361 cancel_hourglass ();
32363 if (INTEGERP (Vhourglass_delay)
32364 && XINT (Vhourglass_delay) > 0)
32365 delay = make_timespec (min (XINT (Vhourglass_delay),
32366 TYPE_MAXIMUM (time_t)),
32368 else if (FLOATP (Vhourglass_delay)
32369 && XFLOAT_DATA (Vhourglass_delay) > 0)
32370 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32371 else
32372 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32374 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32375 show_hourglass, NULL);
32378 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32379 shown. */
32381 void
32382 cancel_hourglass (void)
32384 if (hourglass_atimer)
32386 cancel_atimer (hourglass_atimer);
32387 hourglass_atimer = NULL;
32390 if (hourglass_shown_p)
32392 Lisp_Object tail, frame;
32394 block_input ();
32396 FOR_EACH_FRAME (tail, frame)
32398 struct frame *f = XFRAME (frame);
32400 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32401 && FRAME_RIF (f)->hide_hourglass)
32402 FRAME_RIF (f)->hide_hourglass (f);
32403 #ifdef HAVE_NTGUI
32404 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32405 else if (!FRAME_W32_P (f))
32406 w32_arrow_cursor ();
32407 #endif
32410 hourglass_shown_p = false;
32411 unblock_input ();
32415 #endif /* HAVE_WINDOW_SYSTEM */