Document and fix some bugs with side windows
[emacs.git] / src / xdisp.c
blobe8061663f866dde8e19fab4e2f70ef8a89f06f79
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 void
626 maybe_set_redisplay (Lisp_Object symbol)
628 if (HASH_TABLE_P (Vredisplay__variables)
629 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void redisplay_windows (Lisp_Object);
822 static void redisplay_window (Lisp_Object, bool);
823 static Lisp_Object redisplay_window_error (Lisp_Object);
824 static Lisp_Object redisplay_window_0 (Lisp_Object);
825 static Lisp_Object redisplay_window_1 (Lisp_Object);
826 static bool set_cursor_from_row (struct window *, struct glyph_row *,
827 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
828 int, int);
829 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
830 static bool update_menu_bar (struct frame *, bool, bool);
831 static bool try_window_reusing_current_matrix (struct window *);
832 static int try_window_id (struct window *);
833 static bool display_line (struct it *);
834 static int display_mode_lines (struct window *);
835 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
836 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
837 Lisp_Object, bool);
838 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
839 Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
843 ptrdiff_t *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static bool get_overlay_strings (struct it *, ptrdiff_t);
849 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, bool);
852 static void reseat_1 (struct it *, struct text_pos, bool);
853 static bool next_element_from_display_vector (struct it *);
854 static bool next_element_from_string (struct it *);
855 static bool next_element_from_c_string (struct it *);
856 static bool next_element_from_buffer (struct it *);
857 static bool next_element_from_composition (struct it *);
858 static bool next_element_from_image (struct it *);
859 static bool next_element_from_stretch (struct it *);
860 static bool next_element_from_xwidget (struct it *);
861 static void load_overlay_strings (struct it *, ptrdiff_t);
862 static bool get_next_display_element (struct it *);
863 static enum move_it_result
864 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
865 enum move_operation_enum);
866 static void get_visually_first_element (struct it *);
867 static void compute_stop_pos (struct it *);
868 static int face_before_or_after_it_pos (struct it *, bool);
869 static ptrdiff_t next_overlay_change (ptrdiff_t);
870 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
871 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
872 static int handle_single_display_spec (struct it *, Lisp_Object,
873 Lisp_Object, Lisp_Object,
874 struct text_pos *, ptrdiff_t, int, bool);
875 static int underlying_face_id (struct it *);
877 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
878 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
880 #ifdef HAVE_WINDOW_SYSTEM
882 static void update_tool_bar (struct frame *, bool);
883 static void x_draw_bottom_divider (struct window *w);
884 static void notice_overwritten_cursor (struct window *,
885 enum glyph_row_area,
886 int, int, int, int);
887 static int normal_char_height (struct font *, int);
888 static void normal_char_ascent_descent (struct font *, int, int *, int *);
890 static void append_stretch_glyph (struct it *, Lisp_Object,
891 int, int, int);
893 static Lisp_Object get_it_property (struct it *, Lisp_Object);
894 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
895 struct font *, int, bool);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return true if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1298 bool
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 bool visible_p = false;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. Also, someone might narrow-to-region and immediately
1322 call a scroll function. */
1323 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1324 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1326 /* If the top of the window is after CHARPOS, the latter is surely
1327 not visible. */
1328 if (charpos >= 0 && CHARPOS (top) > charpos)
1329 return visible_p;
1331 /* Compute exact mode line heights. */
1332 if (WINDOW_WANTS_MODELINE_P (w))
1333 w->mode_line_height
1334 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1335 BVAR (current_buffer, mode_line_format));
1337 if (WINDOW_WANTS_HEADER_LINE_P (w))
1338 w->header_line_height
1339 = display_mode_line (w, HEADER_LINE_FACE_ID,
1340 BVAR (current_buffer, header_line_format));
1342 start_display (&it, w, top);
1343 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1344 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1346 if (charpos >= 0
1347 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1348 && IT_CHARPOS (it) >= charpos)
1349 /* When scanning backwards under bidi iteration, move_it_to
1350 stops at or _before_ CHARPOS, because it stops at or to
1351 the _right_ of the character at CHARPOS. */
1352 || (it.bidi_p && it.bidi_it.scan_dir == -1
1353 && IT_CHARPOS (it) <= charpos)))
1355 /* We have reached CHARPOS, or passed it. How the call to
1356 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1357 or covered by a display property, move_it_to stops at the end
1358 of the invisible text, to the right of CHARPOS. (ii) If
1359 CHARPOS is in a display vector, move_it_to stops on its last
1360 glyph. */
1361 int top_x = it.current_x;
1362 int top_y = it.current_y;
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364 int bottom_y;
1365 struct it save_it;
1366 void *save_it_data = NULL;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 SAVE_IT (save_it, it, save_it_data);
1370 last_height = 0;
1371 bottom_y = line_bottom_y (&it);
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = true;
1376 if (bottom_y >= it.last_visible_y
1377 && it.bidi_p && it.bidi_it.scan_dir == -1
1378 && IT_CHARPOS (it) < charpos)
1380 /* When the last line of the window is scanned backwards
1381 under bidi iteration, we could be duped into thinking
1382 that we have passed CHARPOS, when in fact move_it_to
1383 simply stopped short of CHARPOS because it reached
1384 last_visible_y. To see if that's what happened, we call
1385 move_it_to again with a slightly larger vertical limit,
1386 and see if it actually moved vertically; if it did, we
1387 didn't really reach CHARPOS, which is beyond window end. */
1388 /* Why 10? because we don't know how many canonical lines
1389 will the height of the next line(s) be. So we guess. */
1390 int ten_more_lines = 10 * default_line_pixel_height (w);
1392 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1393 MOVE_TO_POS | MOVE_TO_Y);
1394 if (it.current_y > top_y)
1395 visible_p = false;
1398 RESTORE_IT (&it, &save_it, save_it_data);
1399 if (visible_p)
1401 if (it.method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2, it2_prev;
1410 /* The idea is to get to the previous buffer
1411 position, consume the character there, and use
1412 the pixel coordinates we get after that. But if
1413 the previous buffer position is also displayed
1414 from a display vector, we need to consume all of
1415 the glyphs from that display vector. */
1416 start_display (&it2, w, top);
1417 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1418 /* If we didn't get to CHARPOS - 1, there's some
1419 replacing display property at that position, and
1420 we stopped after it. That is exactly the place
1421 whose coordinates we want. */
1422 if (IT_CHARPOS (it2) != charpos - 1)
1423 it2_prev = it2;
1424 else
1426 /* Iterate until we get out of the display
1427 vector that displays the character at
1428 CHARPOS - 1. */
1429 do {
1430 get_next_display_element (&it2);
1431 PRODUCE_GLYPHS (&it2);
1432 it2_prev = it2;
1433 set_iterator_to_next (&it2, true);
1434 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1435 && IT_CHARPOS (it2) < charpos);
1437 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1438 || it2_prev.current_x > it2_prev.last_visible_x)
1439 top_x = it.glyph_row->x;
1440 else
1442 top_x = it2_prev.current_x;
1443 top_y = it2_prev.current_y;
1447 else if (IT_CHARPOS (it) != charpos)
1449 Lisp_Object cpos = make_number (charpos);
1450 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1451 Lisp_Object string = string_from_display_spec (spec);
1452 struct text_pos tpos;
1453 bool newline_in_string
1454 = (STRINGP (string)
1455 && memchr (SDATA (string), '\n', SBYTES (string)));
1457 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1458 bool replacing_spec_p
1459 = (!NILP (spec)
1460 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1461 charpos, FRAME_WINDOW_P (it.f)));
1462 /* The tricky code below is needed because there's a
1463 discrepancy between move_it_to and how we set cursor
1464 when PT is at the beginning of a portion of text
1465 covered by a display property or an overlay with a
1466 display property, or the display line ends in a
1467 newline from a display string. move_it_to will stop
1468 _after_ such display strings, whereas
1469 set_cursor_from_row conspires with cursor_row_p to
1470 place the cursor on the first glyph produced from the
1471 display string. */
1473 /* We have overshoot PT because it is covered by a
1474 display property that replaces the text it covers.
1475 If the string includes embedded newlines, we are also
1476 in the wrong display line. Backtrack to the correct
1477 line, where the display property begins. */
1478 if (replacing_spec_p)
1480 Lisp_Object startpos, endpos;
1481 EMACS_INT start, end;
1482 struct it it3;
1484 /* Find the first and the last buffer positions
1485 covered by the display string. */
1486 endpos =
1487 Fnext_single_char_property_change (cpos, Qdisplay,
1488 Qnil, Qnil);
1489 startpos =
1490 Fprevious_single_char_property_change (endpos, Qdisplay,
1491 Qnil, Qnil);
1492 start = XFASTINT (startpos);
1493 end = XFASTINT (endpos);
1494 /* Move to the last buffer position before the
1495 display property. */
1496 start_display (&it3, w, top);
1497 if (start > CHARPOS (top))
1498 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1499 /* Move forward one more line if the position before
1500 the display string is a newline or if it is the
1501 rightmost character on a line that is
1502 continued or word-wrapped. */
1503 if (it3.method == GET_FROM_BUFFER
1504 && (it3.c == '\n'
1505 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1506 move_it_by_lines (&it3, 1);
1507 else if (move_it_in_display_line_to (&it3, -1,
1508 it3.current_x
1509 + it3.pixel_width,
1510 MOVE_TO_X)
1511 == MOVE_LINE_CONTINUED)
1513 move_it_by_lines (&it3, 1);
1514 /* When we are under word-wrap, the #$@%!
1515 move_it_by_lines moves 2 lines, so we need to
1516 fix that up. */
1517 if (it3.line_wrap == WORD_WRAP)
1518 move_it_by_lines (&it3, -1);
1521 /* Record the vertical coordinate of the display
1522 line where we wound up. */
1523 top_y = it3.current_y;
1524 if (it3.bidi_p)
1526 /* When characters are reordered for display,
1527 the character displayed to the left of the
1528 display string could be _after_ the display
1529 property in the logical order. Use the
1530 smallest vertical position of these two. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1533 if (it3.current_y < top_y)
1534 top_y = it3.current_y;
1536 /* Move from the top of the window to the beginning
1537 of the display line where the display string
1538 begins. */
1539 start_display (&it3, w, top);
1540 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1541 /* If it3_moved stays false after the 'while' loop
1542 below, that means we already were at a newline
1543 before the loop (e.g., the display string begins
1544 with a newline), so we don't need to (and cannot)
1545 inspect the glyphs of it3.glyph_row, because
1546 PRODUCE_GLYPHS will not produce anything for a
1547 newline, and thus it3.glyph_row stays at its
1548 stale content it got at top of the window. */
1549 bool it3_moved = false;
1550 /* Finally, advance the iterator until we hit the
1551 first display element whose character position is
1552 CHARPOS, or until the first newline from the
1553 display string, which signals the end of the
1554 display line. */
1555 while (get_next_display_element (&it3))
1557 PRODUCE_GLYPHS (&it3);
1558 if (IT_CHARPOS (it3) == charpos
1559 || ITERATOR_AT_END_OF_LINE_P (&it3))
1560 break;
1561 it3_moved = true;
1562 set_iterator_to_next (&it3, false);
1564 top_x = it3.current_x - it3.pixel_width;
1565 /* Normally, we would exit the above loop because we
1566 found the display element whose character
1567 position is CHARPOS. For the contingency that we
1568 didn't, and stopped at the first newline from the
1569 display string, move back over the glyphs
1570 produced from the string, until we find the
1571 rightmost glyph not from the string. */
1572 if (it3_moved
1573 && newline_in_string
1574 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1576 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA];
1579 while (EQ ((g - 1)->object, string))
1581 --g;
1582 top_x -= g->pixel_width;
1584 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1585 + it3.glyph_row->used[TEXT_AREA]);
1590 *x = top_x;
1591 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1592 *rtop = max (0, window_top_y - top_y);
1593 *rbot = max (0, bottom_y - it.last_visible_y);
1594 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1595 - max (top_y, window_top_y)));
1596 *vpos = it.vpos;
1597 if (it.bidi_it.paragraph_dir == R2L)
1598 r2l = true;
1601 else
1603 /* Either we were asked to provide info about WINDOW_END, or
1604 CHARPOS is in the partially visible glyph row at end of
1605 window. */
1606 struct it it2;
1607 void *it2data = NULL;
1609 SAVE_IT (it2, it, it2data);
1610 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1611 move_it_by_lines (&it, 1);
1612 if (charpos < IT_CHARPOS (it)
1613 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1615 visible_p = true;
1616 RESTORE_IT (&it2, &it2, it2data);
1617 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1618 *x = it2.current_x;
1619 *y = it2.current_y + it2.max_ascent - it2.ascent;
1620 *rtop = max (0, -it2.current_y);
1621 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1622 - it.last_visible_y));
1623 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1624 it.last_visible_y)
1625 - max (it2.current_y,
1626 WINDOW_HEADER_LINE_HEIGHT (w))));
1627 *vpos = it2.vpos;
1628 if (it2.bidi_it.paragraph_dir == R2L)
1629 r2l = true;
1631 else
1632 bidi_unshelve_cache (it2data, true);
1634 bidi_unshelve_cache (itdata, false);
1636 if (old_buffer)
1637 set_buffer_internal_1 (old_buffer);
1639 if (visible_p)
1641 if (w->hscroll > 0)
1642 *x -=
1643 window_hscroll_limited (w, WINDOW_XFRAME (w))
1644 * WINDOW_FRAME_COLUMN_WIDTH (w);
1645 /* For lines in an R2L paragraph, we need to mirror the X pixel
1646 coordinate wrt the text area. For the reasons, see the
1647 commentary in buffer_posn_from_coords and the explanation of
1648 the geometry used by the move_it_* functions at the end of
1649 the large commentary near the beginning of this file. */
1650 if (r2l)
1651 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1654 #if false
1655 /* Debugging code. */
1656 if (visible_p)
1657 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1658 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1659 else
1660 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1661 #endif
1663 return visible_p;
1667 /* Return the next character from STR. Return in *LEN the length of
1668 the character. This is like STRING_CHAR_AND_LENGTH but never
1669 returns an invalid character. If we find one, we return a `?', but
1670 with the length of the invalid character. */
1672 static int
1673 string_char_and_length (const unsigned char *str, int *len)
1675 int c;
1677 c = STRING_CHAR_AND_LENGTH (str, *len);
1678 if (!CHAR_VALID_P (c))
1679 /* We may not change the length here because other places in Emacs
1680 don't use this function, i.e. they silently accept invalid
1681 characters. */
1682 c = '?';
1684 return c;
1689 /* Given a position POS containing a valid character and byte position
1690 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1692 static struct text_pos
1693 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1695 eassert (STRINGP (string) && nchars >= 0);
1697 if (STRING_MULTIBYTE (string))
1699 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1700 int len;
1702 while (nchars--)
1704 string_char_and_length (p, &len);
1705 p += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1710 else
1711 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1713 return pos;
1717 /* Value is the text position, i.e. character and byte position,
1718 for character position CHARPOS in STRING. */
1720 static struct text_pos
1721 string_pos (ptrdiff_t charpos, Lisp_Object string)
1723 struct text_pos pos;
1724 eassert (STRINGP (string));
1725 eassert (charpos >= 0);
1726 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1727 return pos;
1731 /* Value is a text position, i.e. character and byte position, for
1732 character position CHARPOS in C string S. MULTIBYTE_P
1733 means recognize multibyte characters. */
1735 static struct text_pos
1736 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1738 struct text_pos pos;
1740 eassert (s != NULL);
1741 eassert (charpos >= 0);
1743 if (multibyte_p)
1745 int len;
1747 SET_TEXT_POS (pos, 0, 0);
1748 while (charpos--)
1750 string_char_and_length ((const unsigned char *) s, &len);
1751 s += len;
1752 CHARPOS (pos) += 1;
1753 BYTEPOS (pos) += len;
1756 else
1757 SET_TEXT_POS (pos, charpos, charpos);
1759 return pos;
1763 /* Value is the number of characters in C string S. MULTIBYTE_P
1764 means recognize multibyte characters. */
1766 static ptrdiff_t
1767 number_of_chars (const char *s, bool multibyte_p)
1769 ptrdiff_t nchars;
1771 if (multibyte_p)
1773 ptrdiff_t rest = strlen (s);
1774 int len;
1775 const unsigned char *p = (const unsigned char *) s;
1777 for (nchars = 0; rest > 0; ++nchars)
1779 string_char_and_length (p, &len);
1780 rest -= len, p += len;
1783 else
1784 nchars = strlen (s);
1786 return nchars;
1790 /* Compute byte position NEWPOS->bytepos corresponding to
1791 NEWPOS->charpos. POS is a known position in string STRING.
1792 NEWPOS->charpos must be >= POS.charpos. */
1794 static void
1795 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1797 eassert (STRINGP (string));
1798 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1800 if (STRING_MULTIBYTE (string))
1801 *newpos = string_pos_nchars_ahead (pos, string,
1802 CHARPOS (*newpos) - CHARPOS (pos));
1803 else
1804 BYTEPOS (*newpos) = CHARPOS (*newpos);
1807 /* EXPORT:
1808 Return an estimation of the pixel height of mode or header lines on
1809 frame F. FACE_ID specifies what line's height to estimate. */
1812 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 if (FRAME_WINDOW_P (f))
1817 int height = FONT_HEIGHT (FRAME_FONT (f));
1819 /* This function is called so early when Emacs starts that the face
1820 cache and mode line face are not yet initialized. */
1821 if (FRAME_FACE_CACHE (f))
1823 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1824 if (face)
1826 if (face->font)
1827 height = normal_char_height (face->font, -1);
1828 if (face->box_line_width > 0)
1829 height += 2 * face->box_line_width;
1833 return height;
1835 #endif
1837 return 1;
1840 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1841 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1842 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1843 not force the value into range. */
1845 void
1846 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1847 NativeRectangle *bounds, bool noclip)
1850 #ifdef HAVE_WINDOW_SYSTEM
1851 if (FRAME_WINDOW_P (f))
1853 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1854 even for negative values. */
1855 if (pix_x < 0)
1856 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1857 if (pix_y < 0)
1858 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1860 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1861 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1863 if (bounds)
1864 STORE_NATIVE_RECT (*bounds,
1865 FRAME_COL_TO_PIXEL_X (f, pix_x),
1866 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1867 FRAME_COLUMN_WIDTH (f) - 1,
1868 FRAME_LINE_HEIGHT (f) - 1);
1870 /* PXW: Should we clip pixels before converting to columns/lines? */
1871 if (!noclip)
1873 if (pix_x < 0)
1874 pix_x = 0;
1875 else if (pix_x > FRAME_TOTAL_COLS (f))
1876 pix_x = FRAME_TOTAL_COLS (f);
1878 if (pix_y < 0)
1879 pix_y = 0;
1880 else if (pix_y > FRAME_TOTAL_LINES (f))
1881 pix_y = FRAME_TOTAL_LINES (f);
1884 #endif
1886 *x = pix_x;
1887 *y = pix_y;
1891 /* Find the glyph under window-relative coordinates X/Y in window W.
1892 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1893 strings. Return in *HPOS and *VPOS the row and column number of
1894 the glyph found. Return in *AREA the glyph area containing X.
1895 Value is a pointer to the glyph found or null if X/Y is not on
1896 text, or we can't tell because W's current matrix is not up to
1897 date. */
1899 static struct glyph *
1900 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1901 int *dx, int *dy, int *area)
1903 struct glyph *glyph, *end;
1904 struct glyph_row *row = NULL;
1905 int x0, i;
1907 /* Find row containing Y. Give up if some row is not enabled. */
1908 for (i = 0; i < w->current_matrix->nrows; ++i)
1910 row = MATRIX_ROW (w->current_matrix, i);
1911 if (!row->enabled_p)
1912 return NULL;
1913 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1914 break;
1917 *vpos = i;
1918 *hpos = 0;
1920 /* Give up if Y is not in the window. */
1921 if (i == w->current_matrix->nrows)
1922 return NULL;
1924 /* Get the glyph area containing X. */
1925 if (w->pseudo_window_p)
1927 *area = TEXT_AREA;
1928 x0 = 0;
1930 else
1932 if (x < window_box_left_offset (w, TEXT_AREA))
1934 *area = LEFT_MARGIN_AREA;
1935 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1937 else if (x < window_box_right_offset (w, TEXT_AREA))
1939 *area = TEXT_AREA;
1940 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1942 else
1944 *area = RIGHT_MARGIN_AREA;
1945 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1949 /* Find glyph containing X. */
1950 glyph = row->glyphs[*area];
1951 end = glyph + row->used[*area];
1952 x -= x0;
1953 while (glyph < end && x >= glyph->pixel_width)
1955 x -= glyph->pixel_width;
1956 ++glyph;
1959 if (glyph == end)
1960 return NULL;
1962 if (dx)
1964 *dx = x;
1965 *dy = y - (row->y + row->ascent - glyph->ascent);
1968 *hpos = glyph - row->glyphs[*area];
1969 return glyph;
1972 /* Convert frame-relative x/y to coordinates relative to window W.
1973 Takes pseudo-windows into account. */
1975 static void
1976 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1978 if (w->pseudo_window_p)
1980 /* A pseudo-window is always full-width, and starts at the
1981 left edge of the frame, plus a frame border. */
1982 struct frame *f = XFRAME (w->frame);
1983 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 else
1988 *x -= WINDOW_LEFT_EDGE_X (w);
1989 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1993 #ifdef HAVE_WINDOW_SYSTEM
1995 /* EXPORT:
1996 Return in RECTS[] at most N clipping rectangles for glyph string S.
1997 Return the number of stored rectangles. */
2000 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2002 XRectangle r;
2004 if (n <= 0)
2005 return 0;
2007 if (s->row->full_width_p)
2009 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2010 r.x = WINDOW_LEFT_EDGE_X (s->w);
2011 if (s->row->mode_line_p)
2012 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2013 else
2014 r.width = WINDOW_PIXEL_WIDTH (s->w);
2016 /* Unless displaying a mode or menu bar line, which are always
2017 fully visible, clip to the visible part of the row. */
2018 if (s->w->pseudo_window_p)
2019 r.height = s->row->visible_height;
2020 else
2021 r.height = s->height;
2023 else
2025 /* This is a text line that may be partially visible. */
2026 r.x = window_box_left (s->w, s->area);
2027 r.width = window_box_width (s->w, s->area);
2028 r.height = s->row->visible_height;
2031 if (s->clip_head)
2032 if (r.x < s->clip_head->x)
2034 if (r.width >= s->clip_head->x - r.x)
2035 r.width -= s->clip_head->x - r.x;
2036 else
2037 r.width = 0;
2038 r.x = s->clip_head->x;
2040 if (s->clip_tail)
2041 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2043 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2044 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2045 else
2046 r.width = 0;
2049 /* If S draws overlapping rows, it's sufficient to use the top and
2050 bottom of the window for clipping because this glyph string
2051 intentionally draws over other lines. */
2052 if (s->for_overlaps)
2054 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2055 r.height = window_text_bottom_y (s->w) - r.y;
2057 /* Alas, the above simple strategy does not work for the
2058 environments with anti-aliased text: if the same text is
2059 drawn onto the same place multiple times, it gets thicker.
2060 If the overlap we are processing is for the erased cursor, we
2061 take the intersection with the rectangle of the cursor. */
2062 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2064 XRectangle rc, r_save = r;
2066 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2067 rc.y = s->w->phys_cursor.y;
2068 rc.width = s->w->phys_cursor_width;
2069 rc.height = s->w->phys_cursor_height;
2071 x_intersect_rectangles (&r_save, &rc, &r);
2074 else
2076 /* Don't use S->y for clipping because it doesn't take partially
2077 visible lines into account. For example, it can be negative for
2078 partially visible lines at the top of a window. */
2079 if (!s->row->full_width_p
2080 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2081 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2082 else
2083 r.y = max (0, s->row->y);
2086 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2088 /* If drawing the cursor, don't let glyph draw outside its
2089 advertised boundaries. Cleartype does this under some circumstances. */
2090 if (s->hl == DRAW_CURSOR)
2092 struct glyph *glyph = s->first_glyph;
2093 int height, max_y;
2095 if (s->x > r.x)
2097 if (r.width >= s->x - r.x)
2098 r.width -= s->x - r.x;
2099 else /* R2L hscrolled row with cursor outside text area */
2100 r.width = 0;
2101 r.x = s->x;
2103 r.width = min (r.width, glyph->pixel_width);
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2112 r.y = max_y;
2113 r.height = height;
2115 else
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2128 if (s->row->clip)
2130 XRectangle r_save = r;
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2146 else
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2159 if (s->for_overlaps & OVERLAPS_PRED)
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2169 i++;
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2176 if (r.y + r.height > row_y + s->row->visible_height)
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2181 else
2182 rs[i].height = 0;
2184 i++;
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2202 get_glyph_string_clip_rects (s, nr, 1);
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0, ascent;
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width;
2224 x = w->phys_cursor.x;
2225 if (x < 0)
2227 wd += x;
2228 x = 0;
2231 if (glyph->type == STRETCH_GLYPH
2232 && !x_stretch_cursor_p)
2233 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2234 w->phys_cursor_width = wd;
2236 /* Don't let the hollow cursor glyph descend below the glyph row's
2237 ascent value, lest the hollow cursor looks funny. */
2238 y = w->phys_cursor.y;
2239 ascent = row->ascent;
2240 if (row->ascent < glyph->ascent)
2242 y -= glyph->ascent - row->ascent;
2243 ascent = glyph->ascent;
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249 h = max (h0, ascent + glyph->descent);
2250 h0 = min (h0, ascent + glyph->descent);
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2258 else
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2263 h += y - y0;
2264 y = y0;
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2274 * Remember which glyph the mouse is over.
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2290 if (window_resize_pixelwise)
2292 width = height = 1;
2293 goto virtual_glyph;
2295 else if (!f->glyphs_initialized_p
2296 || (window = window_from_coordinates (f, gx, gy, &part, false),
2297 NILP (window)))
2299 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2300 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2301 goto virtual_glyph;
2304 w = XWINDOW (window);
2305 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2306 height = WINDOW_FRAME_LINE_HEIGHT (w);
2308 x = window_relative_x_coord (w, part, gx);
2309 y = gy - WINDOW_TOP_EDGE_Y (w);
2311 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2312 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2314 if (w->pseudo_window_p)
2316 area = TEXT_AREA;
2317 part = ON_MODE_LINE; /* Don't adjust margin. */
2318 goto text_glyph;
2321 switch (part)
2323 case ON_LEFT_MARGIN:
2324 area = LEFT_MARGIN_AREA;
2325 goto text_glyph;
2327 case ON_RIGHT_MARGIN:
2328 area = RIGHT_MARGIN_AREA;
2329 goto text_glyph;
2331 case ON_HEADER_LINE:
2332 case ON_MODE_LINE:
2333 gr = (part == ON_HEADER_LINE
2334 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2335 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2336 gy = gr->y;
2337 area = TEXT_AREA;
2338 goto text_glyph_row_found;
2340 case ON_TEXT:
2341 area = TEXT_AREA;
2343 text_glyph:
2344 gr = 0; gy = 0;
2345 for (; r <= end_row && r->enabled_p; ++r)
2346 if (r->y + r->height > y)
2348 gr = r; gy = r->y;
2349 break;
2352 text_glyph_row_found:
2353 if (gr && gy <= y)
2355 struct glyph *g = gr->glyphs[area];
2356 struct glyph *end = g + gr->used[area];
2358 height = gr->height;
2359 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2360 if (gx + g->pixel_width > x)
2361 break;
2363 if (g < end)
2365 if (g->type == IMAGE_GLYPH)
2367 /* Don't remember when mouse is over image, as
2368 image may have hot-spots. */
2369 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2370 return;
2372 width = g->pixel_width;
2374 else
2376 /* Use nominal char spacing at end of line. */
2377 x -= gx;
2378 gx += (x / width) * width;
2381 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2383 gx += window_box_left_offset (w, area);
2384 /* Don't expand over the modeline to make sure the vertical
2385 drag cursor is shown early enough. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2390 else
2392 /* Use nominal line height at end of window. */
2393 gx = (x / width) * width;
2394 y -= gy;
2395 gy += (y / height) * height;
2396 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2397 /* See comment above. */
2398 height = min (height,
2399 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2401 break;
2403 case ON_LEFT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2406 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2407 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2408 goto row_glyph;
2410 case ON_RIGHT_FRINGE:
2411 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2412 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2413 : window_box_right_offset (w, TEXT_AREA));
2414 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2415 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2416 && !WINDOW_RIGHTMOST_P (w))
2417 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2418 /* Make sure the vertical border can get her own glyph to the
2419 right of the one we build here. */
2420 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2421 else
2422 width = WINDOW_PIXEL_WIDTH (w) - gx;
2423 else
2424 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2426 goto row_glyph;
2428 case ON_VERTICAL_BORDER:
2429 gx = WINDOW_PIXEL_WIDTH (w) - width;
2430 goto row_glyph;
2432 case ON_VERTICAL_SCROLL_BAR:
2433 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2435 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2436 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2437 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2438 : 0)));
2439 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2441 row_glyph:
2442 gr = 0, gy = 0;
2443 for (; r <= end_row && r->enabled_p; ++r)
2444 if (r->y + r->height > y)
2446 gr = r; gy = r->y;
2447 break;
2450 if (gr && gy <= y)
2451 height = gr->height;
2452 else
2454 /* Use nominal line height at end of window. */
2455 y -= gy;
2456 gy += (y / height) * height;
2458 break;
2460 case ON_RIGHT_DIVIDER:
2461 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2462 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2463 gy = 0;
2464 /* The bottom divider prevails. */
2465 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 case ON_BOTTOM_DIVIDER:
2469 gx = 0;
2470 width = WINDOW_PIXEL_WIDTH (w);
2471 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2472 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2473 goto add_edge;
2475 default:
2477 virtual_glyph:
2478 /* If there is no glyph under the mouse, then we divide the screen
2479 into a grid of the smallest glyph in the frame, and use that
2480 as our "glyph". */
2482 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2483 round down even for negative values. */
2484 if (gx < 0)
2485 gx -= width - 1;
2486 if (gy < 0)
2487 gy -= height - 1;
2489 gx = (gx / width) * width;
2490 gy = (gy / height) * height;
2492 goto store_rect;
2495 add_edge:
2496 gx += WINDOW_LEFT_EDGE_X (w);
2497 gy += WINDOW_TOP_EDGE_Y (w);
2499 store_rect:
2500 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2502 /* Visible feedback for debugging. */
2503 #if false && defined HAVE_X_WINDOWS
2504 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2505 f->output_data.x->normal_gc,
2506 gx, gy, width, height);
2507 #endif
2511 #endif /* HAVE_WINDOW_SYSTEM */
2513 static void
2514 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2516 eassert (w);
2517 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2518 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2519 w->window_end_vpos
2520 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2523 /***********************************************************************
2524 Lisp form evaluation
2525 ***********************************************************************/
2527 /* Error handler for safe_eval and safe_call. */
2529 static Lisp_Object
2530 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2532 add_to_log ("Error during redisplay: %S signaled %S",
2533 Flist (nargs, args), arg);
2534 return Qnil;
2537 /* Call function FUNC with the rest of NARGS - 1 arguments
2538 following. Return the result, or nil if something went
2539 wrong. Prevent redisplay during the evaluation. */
2541 static Lisp_Object
2542 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2544 Lisp_Object val;
2546 if (inhibit_eval_during_redisplay)
2547 val = Qnil;
2548 else
2550 ptrdiff_t i;
2551 ptrdiff_t count = SPECPDL_INDEX ();
2552 Lisp_Object *args;
2553 USE_SAFE_ALLOCA;
2554 SAFE_ALLOCA_LISP (args, nargs);
2556 args[0] = func;
2557 for (i = 1; i < nargs; i++)
2558 args[i] = va_arg (ap, Lisp_Object);
2560 specbind (Qinhibit_redisplay, Qt);
2561 if (inhibit_quit)
2562 specbind (Qinhibit_quit, Qt);
2563 /* Use Qt to ensure debugger does not run,
2564 so there is no possibility of wanting to redisplay. */
2565 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2566 safe_eval_handler);
2567 SAFE_FREE ();
2568 val = unbind_to (count, val);
2571 return val;
2574 Lisp_Object
2575 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2577 Lisp_Object retval;
2578 va_list ap;
2580 va_start (ap, func);
2581 retval = safe__call (false, nargs, func, ap);
2582 va_end (ap);
2583 return retval;
2586 /* Call function FN with one argument ARG.
2587 Return the result, or nil if something went wrong. */
2589 Lisp_Object
2590 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2592 return safe_call (2, fn, arg);
2595 static Lisp_Object
2596 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2598 Lisp_Object retval;
2599 va_list ap;
2601 va_start (ap, fn);
2602 retval = safe__call (inhibit_quit, 2, fn, ap);
2603 va_end (ap);
2604 return retval;
2607 Lisp_Object
2608 safe_eval (Lisp_Object sexpr)
2610 return safe__call1 (false, Qeval, sexpr);
2613 static Lisp_Object
2614 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2616 return safe__call1 (inhibit_quit, Qeval, sexpr);
2619 /* Call function FN with two arguments ARG1 and ARG2.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2625 return safe_call (3, fn, arg1, arg2);
2630 /***********************************************************************
2631 Debugging
2632 ***********************************************************************/
2634 /* Define CHECK_IT to perform sanity checks on iterators.
2635 This is for debugging. It is too slow to do unconditionally. */
2637 static void
2638 CHECK_IT (struct it *it)
2640 #if false
2641 if (it->method == GET_FROM_STRING)
2643 eassert (STRINGP (it->string));
2644 eassert (IT_STRING_CHARPOS (*it) >= 0);
2646 else
2648 eassert (IT_STRING_CHARPOS (*it) < 0);
2649 if (it->method == GET_FROM_BUFFER)
2651 /* Check that character and byte positions agree. */
2652 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2656 if (it->dpvec)
2657 eassert (it->current.dpvec_index >= 0);
2658 else
2659 eassert (it->current.dpvec_index < 0);
2660 #endif
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 CHECK_WINDOW_END (struct window *w)
2670 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2671 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2673 struct glyph_row *row;
2674 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2675 !row->enabled_p
2676 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2677 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2679 #endif
2682 /***********************************************************************
2683 Iterator initialization
2684 ***********************************************************************/
2686 /* Initialize IT for displaying current_buffer in window W, starting
2687 at character position CHARPOS. CHARPOS < 0 means that no buffer
2688 position is specified which is useful when the iterator is assigned
2689 a position later. BYTEPOS is the byte position corresponding to
2690 CHARPOS.
2692 If ROW is not null, calls to produce_glyphs with IT as parameter
2693 will produce glyphs in that row.
2695 BASE_FACE_ID is the id of a base face to use. It must be one of
2696 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2697 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2698 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2700 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2701 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2702 will be initialized to use the corresponding mode line glyph row of
2703 the desired matrix of W. */
2705 void
2706 init_iterator (struct it *it, struct window *w,
2707 ptrdiff_t charpos, ptrdiff_t bytepos,
2708 struct glyph_row *row, enum face_id base_face_id)
2710 enum face_id remapped_base_face_id = base_face_id;
2712 /* Some precondition checks. */
2713 eassert (w != NULL && it != NULL);
2714 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2715 && charpos <= ZV));
2717 /* If face attributes have been changed since the last redisplay,
2718 free realized faces now because they depend on face definitions
2719 that might have changed. Don't free faces while there might be
2720 desired matrices pending which reference these faces. */
2721 if (!inhibit_free_realized_faces)
2723 if (face_change)
2725 face_change = false;
2726 free_all_realized_faces (Qnil);
2728 else if (XFRAME (w->frame)->face_change)
2730 XFRAME (w->frame)->face_change = 0;
2731 free_all_realized_faces (w->frame);
2735 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2736 if (! NILP (Vface_remapping_alist))
2737 remapped_base_face_id
2738 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2740 /* Use one of the mode line rows of W's desired matrix if
2741 appropriate. */
2742 if (row == NULL)
2744 if (base_face_id == MODE_LINE_FACE_ID
2745 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2746 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2747 else if (base_face_id == HEADER_LINE_FACE_ID)
2748 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2751 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2752 Other parts of redisplay rely on that. */
2753 memclear (it, sizeof *it);
2754 it->current.overlay_string_index = -1;
2755 it->current.dpvec_index = -1;
2756 it->base_face_id = remapped_base_face_id;
2757 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2758 it->paragraph_embedding = L2R;
2759 it->bidi_it.w = w;
2761 /* The window in which we iterate over current_buffer: */
2762 XSETWINDOW (it->window, w);
2763 it->w = w;
2764 it->f = XFRAME (w->frame);
2766 it->cmp_it.id = -1;
2768 /* Extra space between lines (on window systems only). */
2769 if (base_face_id == DEFAULT_FACE_ID
2770 && FRAME_WINDOW_P (it->f))
2772 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2774 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2776 * FRAME_LINE_HEIGHT (it->f));
2777 else if (it->f->extra_line_spacing > 0)
2778 it->extra_line_spacing = it->f->extra_line_spacing;
2781 /* If realized faces have been removed, e.g. because of face
2782 attribute changes of named faces, recompute them. When running
2783 in batch mode, the face cache of the initial frame is null. If
2784 we happen to get called, make a dummy face cache. */
2785 if (FRAME_FACE_CACHE (it->f) == NULL)
2786 init_frame_faces (it->f);
2787 if (FRAME_FACE_CACHE (it->f)->used == 0)
2788 recompute_basic_faces (it->f);
2790 it->override_ascent = -1;
2792 /* Are control characters displayed as `^C'? */
2793 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2795 /* -1 means everything between a CR and the following line end
2796 is invisible. >0 means lines indented more than this value are
2797 invisible. */
2798 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2799 ? (clip_to_bounds
2800 (-1, XINT (BVAR (current_buffer, selective_display)),
2801 PTRDIFF_MAX))
2802 : (!NILP (BVAR (current_buffer, selective_display))
2803 ? -1 : 0));
2804 it->selective_display_ellipsis_p
2805 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2807 /* Display table to use. */
2808 it->dp = window_display_table (w);
2810 /* Are multibyte characters enabled in current_buffer? */
2811 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2813 /* Get the position at which the redisplay_end_trigger hook should
2814 be run, if it is to be run at all. */
2815 if (MARKERP (w->redisplay_end_trigger)
2816 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2817 it->redisplay_end_trigger_charpos
2818 = marker_position (w->redisplay_end_trigger);
2819 else if (INTEGERP (w->redisplay_end_trigger))
2820 it->redisplay_end_trigger_charpos
2821 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2822 PTRDIFF_MAX);
2824 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2826 /* Are lines in the display truncated? */
2827 if (TRUNCATE != 0)
2828 it->line_wrap = TRUNCATE;
2829 if (base_face_id == DEFAULT_FACE_ID
2830 && !it->w->hscroll
2831 && (WINDOW_FULL_WIDTH_P (it->w)
2832 || NILP (Vtruncate_partial_width_windows)
2833 || (INTEGERP (Vtruncate_partial_width_windows)
2834 /* PXW: Shall we do something about this? */
2835 && (XINT (Vtruncate_partial_width_windows)
2836 <= WINDOW_TOTAL_COLS (it->w))))
2837 && NILP (BVAR (current_buffer, truncate_lines)))
2838 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2839 ? WINDOW_WRAP : WORD_WRAP;
2841 /* Get dimensions of truncation and continuation glyphs. These are
2842 displayed as fringe bitmaps under X, but we need them for such
2843 frames when the fringes are turned off. But leave the dimensions
2844 zero for tooltip frames, as these glyphs look ugly there and also
2845 sabotage calculations of tooltip dimensions in x-show-tip. */
2846 #ifdef HAVE_WINDOW_SYSTEM
2847 if (!(FRAME_WINDOW_P (it->f)
2848 && FRAMEP (tip_frame)
2849 && it->f == XFRAME (tip_frame)))
2850 #endif
2852 if (it->line_wrap == TRUNCATE)
2854 /* We will need the truncation glyph. */
2855 eassert (it->glyph_row == NULL);
2856 produce_special_glyphs (it, IT_TRUNCATION);
2857 it->truncation_pixel_width = it->pixel_width;
2859 else
2861 /* We will need the continuation glyph. */
2862 eassert (it->glyph_row == NULL);
2863 produce_special_glyphs (it, IT_CONTINUATION);
2864 it->continuation_pixel_width = it->pixel_width;
2868 /* Reset these values to zero because the produce_special_glyphs
2869 above has changed them. */
2870 it->pixel_width = it->ascent = it->descent = 0;
2871 it->phys_ascent = it->phys_descent = 0;
2873 /* Set this after getting the dimensions of truncation and
2874 continuation glyphs, so that we don't produce glyphs when calling
2875 produce_special_glyphs, above. */
2876 it->glyph_row = row;
2877 it->area = TEXT_AREA;
2879 /* Get the dimensions of the display area. The display area
2880 consists of the visible window area plus a horizontally scrolled
2881 part to the left of the window. All x-values are relative to the
2882 start of this total display area. */
2883 if (base_face_id != DEFAULT_FACE_ID)
2885 /* Mode lines, menu bar in terminal frames. */
2886 it->first_visible_x = 0;
2887 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2889 else
2891 it->first_visible_x
2892 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2893 it->last_visible_x = (it->first_visible_x
2894 + window_box_width (w, TEXT_AREA));
2896 /* If we truncate lines, leave room for the truncation glyph(s) at
2897 the right margin. Otherwise, leave room for the continuation
2898 glyph(s). Done only if the window has no right fringe. */
2899 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2901 if (it->line_wrap == TRUNCATE)
2902 it->last_visible_x -= it->truncation_pixel_width;
2903 else
2904 it->last_visible_x -= it->continuation_pixel_width;
2907 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2908 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2911 /* Leave room for a border glyph. */
2912 if (!FRAME_WINDOW_P (it->f)
2913 && !WINDOW_RIGHTMOST_P (it->w))
2914 it->last_visible_x -= 1;
2916 it->last_visible_y = window_text_bottom_y (w);
2918 /* For mode lines and alike, arrange for the first glyph having a
2919 left box line if the face specifies a box. */
2920 if (base_face_id != DEFAULT_FACE_ID)
2922 struct face *face;
2924 it->face_id = remapped_base_face_id;
2926 /* If we have a boxed mode line, make the first character appear
2927 with a left box line. */
2928 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2929 if (face && face->box != FACE_NO_BOX)
2930 it->start_of_box_run_p = true;
2933 /* If a buffer position was specified, set the iterator there,
2934 getting overlays and face properties from that position. */
2935 if (charpos >= BUF_BEG (current_buffer))
2937 it->stop_charpos = charpos;
2938 it->end_charpos = ZV;
2939 eassert (charpos == BYTE_TO_CHAR (bytepos));
2940 IT_CHARPOS (*it) = charpos;
2941 IT_BYTEPOS (*it) = bytepos;
2943 /* We will rely on `reseat' to set this up properly, via
2944 handle_face_prop. */
2945 it->face_id = it->base_face_id;
2947 it->start = it->current;
2948 /* Do we need to reorder bidirectional text? Not if this is a
2949 unibyte buffer: by definition, none of the single-byte
2950 characters are strong R2L, so no reordering is needed. And
2951 bidi.c doesn't support unibyte buffers anyway. Also, don't
2952 reorder while we are loading loadup.el, since the tables of
2953 character properties needed for reordering are not yet
2954 available. */
2955 it->bidi_p =
2956 !redisplay__inhibit_bidi
2957 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2958 && it->multibyte_p;
2960 /* If we are to reorder bidirectional text, init the bidi
2961 iterator. */
2962 if (it->bidi_p)
2964 /* Since we don't know at this point whether there will be
2965 any R2L lines in the window, we reserve space for
2966 truncation/continuation glyphs even if only the left
2967 fringe is absent. */
2968 if (base_face_id == DEFAULT_FACE_ID
2969 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2970 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2972 if (it->line_wrap == TRUNCATE)
2973 it->last_visible_x -= it->truncation_pixel_width;
2974 else
2975 it->last_visible_x -= it->continuation_pixel_width;
2977 /* Note the paragraph direction that this buffer wants to
2978 use. */
2979 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2980 Qleft_to_right))
2981 it->paragraph_embedding = L2R;
2982 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2983 Qright_to_left))
2984 it->paragraph_embedding = R2L;
2985 else
2986 it->paragraph_embedding = NEUTRAL_DIR;
2987 bidi_unshelve_cache (NULL, false);
2988 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2989 &it->bidi_it);
2992 /* Compute faces etc. */
2993 reseat (it, it->current.pos, true);
2996 CHECK_IT (it);
3000 /* Initialize IT for the display of window W with window start POS. */
3002 void
3003 start_display (struct it *it, struct window *w, struct text_pos pos)
3005 struct glyph_row *row;
3006 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3008 row = w->desired_matrix->rows + first_vpos;
3009 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3010 it->first_vpos = first_vpos;
3012 /* Don't reseat to previous visible line start if current start
3013 position is in a string or image. */
3014 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3016 int first_y = it->current_y;
3018 /* If window start is not at a line start, skip forward to POS to
3019 get the correct continuation lines width. */
3020 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3021 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3022 if (!start_at_line_beg_p)
3024 int new_x;
3026 reseat_at_previous_visible_line_start (it);
3027 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3029 new_x = it->current_x + it->pixel_width;
3031 /* If lines are continued, this line may end in the middle
3032 of a multi-glyph character (e.g. a control character
3033 displayed as \003, or in the middle of an overlay
3034 string). In this case move_it_to above will not have
3035 taken us to the start of the continuation line but to the
3036 end of the continued line. */
3037 if (it->current_x > 0
3038 && it->line_wrap != TRUNCATE /* Lines are continued. */
3039 && (/* And glyph doesn't fit on the line. */
3040 new_x > it->last_visible_x
3041 /* Or it fits exactly and we're on a window
3042 system frame. */
3043 || (new_x == it->last_visible_x
3044 && FRAME_WINDOW_P (it->f)
3045 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3046 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3047 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3049 if ((it->current.dpvec_index >= 0
3050 || it->current.overlay_string_index >= 0)
3051 /* If we are on a newline from a display vector or
3052 overlay string, then we are already at the end of
3053 a screen line; no need to go to the next line in
3054 that case, as this line is not really continued.
3055 (If we do go to the next line, C-e will not DTRT.) */
3056 && it->c != '\n')
3058 set_iterator_to_next (it, true);
3059 move_it_in_display_line_to (it, -1, -1, 0);
3062 it->continuation_lines_width += it->current_x;
3064 /* If the character at POS is displayed via a display
3065 vector, move_it_to above stops at the final glyph of
3066 IT->dpvec. To make the caller redisplay that character
3067 again (a.k.a. start at POS), we need to reset the
3068 dpvec_index to the beginning of IT->dpvec. */
3069 else if (it->current.dpvec_index >= 0)
3070 it->current.dpvec_index = 0;
3072 /* We're starting a new display line, not affected by the
3073 height of the continued line, so clear the appropriate
3074 fields in the iterator structure. */
3075 it->max_ascent = it->max_descent = 0;
3076 it->max_phys_ascent = it->max_phys_descent = 0;
3078 it->current_y = first_y;
3079 it->vpos = 0;
3080 it->current_x = it->hpos = 0;
3086 /* Return true if POS is a position in ellipses displayed for invisible
3087 text. W is the window we display, for text property lookup. */
3089 static bool
3090 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3092 Lisp_Object prop, window;
3093 bool ellipses_p = false;
3094 ptrdiff_t charpos = CHARPOS (pos->pos);
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (pos->dpvec_index >= 0
3101 && pos->overlay_string_index < 0
3102 && CHARPOS (pos->string_pos) < 0
3103 && charpos > BEGV
3104 && (XSETWINDOW (window, w),
3105 prop = Fget_char_property (make_number (charpos),
3106 Qinvisible, window),
3107 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3109 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3110 window);
3111 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3114 return ellipses_p;
3118 /* Initialize IT for stepping through current_buffer in window W,
3119 starting at position POS that includes overlay string and display
3120 vector/ control character translation position information. Value
3121 is false if there are overlay strings with newlines at POS. */
3123 static bool
3124 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3126 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3127 int i;
3128 bool overlay_strings_with_newlines = false;
3130 /* If POS specifies a position in a display vector, this might
3131 be for an ellipsis displayed for invisible text. We won't
3132 get the iterator set up for delivering that ellipsis unless
3133 we make sure that it gets aware of the invisible text. */
3134 if (in_ellipses_for_invisible_text_p (pos, w))
3136 --charpos;
3137 bytepos = 0;
3140 /* Keep in mind: the call to reseat in init_iterator skips invisible
3141 text, so we might end up at a position different from POS. This
3142 is only a problem when POS is a row start after a newline and an
3143 overlay starts there with an after-string, and the overlay has an
3144 invisible property. Since we don't skip invisible text in
3145 display_line and elsewhere immediately after consuming the
3146 newline before the row start, such a POS will not be in a string,
3147 but the call to init_iterator below will move us to the
3148 after-string. */
3149 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3151 /* This only scans the current chunk -- it should scan all chunks.
3152 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3153 to 16 in 22.1 to make this a lesser problem. */
3154 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3156 const char *s = SSDATA (it->overlay_strings[i]);
3157 const char *e = s + SBYTES (it->overlay_strings[i]);
3159 while (s < e && *s != '\n')
3160 ++s;
3162 if (s < e)
3164 overlay_strings_with_newlines = true;
3165 break;
3169 /* If position is within an overlay string, set up IT to the right
3170 overlay string. */
3171 if (pos->overlay_string_index >= 0)
3173 int relative_index;
3175 /* If the first overlay string happens to have a `display'
3176 property for an image, the iterator will be set up for that
3177 image, and we have to undo that setup first before we can
3178 correct the overlay string index. */
3179 if (it->method == GET_FROM_IMAGE)
3180 pop_it (it);
3182 /* We already have the first chunk of overlay strings in
3183 IT->overlay_strings. Load more until the one for
3184 pos->overlay_string_index is in IT->overlay_strings. */
3185 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3187 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3188 it->current.overlay_string_index = 0;
3189 while (n--)
3191 load_overlay_strings (it, 0);
3192 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = pos->overlay_string_index;
3197 relative_index = (it->current.overlay_string_index
3198 % OVERLAY_STRING_CHUNK_SIZE);
3199 it->string = it->overlay_strings[relative_index];
3200 eassert (STRINGP (it->string));
3201 it->current.string_pos = pos->string_pos;
3202 it->method = GET_FROM_STRING;
3203 it->end_charpos = SCHARS (it->string);
3204 /* Set up the bidi iterator for this overlay string. */
3205 if (it->bidi_p)
3207 it->bidi_it.string.lstring = it->string;
3208 it->bidi_it.string.s = NULL;
3209 it->bidi_it.string.schars = SCHARS (it->string);
3210 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3211 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3212 it->bidi_it.string.unibyte = !it->multibyte_p;
3213 it->bidi_it.w = it->w;
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3217 /* Synchronize the state of the bidi iterator with
3218 pos->string_pos. For any string position other than
3219 zero, this will be done automagically when we resume
3220 iteration over the string and get_visually_first_element
3221 is called. But if string_pos is zero, and the string is
3222 to be reordered for display, we need to resync manually,
3223 since it could be that the iteration state recorded in
3224 pos ended at string_pos of 0 moving backwards in string. */
3225 if (CHARPOS (pos->string_pos) == 0)
3227 get_visually_first_element (it);
3228 if (IT_STRING_CHARPOS (*it) != 0)
3229 do {
3230 /* Paranoia. */
3231 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3232 bidi_move_to_visually_next (&it->bidi_it);
3233 } while (it->bidi_it.charpos != 0);
3235 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3236 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3240 if (CHARPOS (pos->string_pos) >= 0)
3242 /* Recorded position is not in an overlay string, but in another
3243 string. This can only be a string from a `display' property.
3244 IT should already be filled with that string. */
3245 it->current.string_pos = pos->string_pos;
3246 eassert (STRINGP (it->string));
3247 if (it->bidi_p)
3248 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3249 FRAME_WINDOW_P (it->f), &it->bidi_it);
3252 /* Restore position in display vector translations, control
3253 character translations or ellipses. */
3254 if (pos->dpvec_index >= 0)
3256 if (it->dpvec == NULL)
3257 get_next_display_element (it);
3258 eassert (it->dpvec && it->current.dpvec_index == 0);
3259 it->current.dpvec_index = pos->dpvec_index;
3262 CHECK_IT (it);
3263 return !overlay_strings_with_newlines;
3267 /* Initialize IT for stepping through current_buffer in window W
3268 starting at ROW->start. */
3270 static void
3271 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3273 init_from_display_pos (it, w, &row->start);
3274 it->start = row->start;
3275 it->continuation_lines_width = row->continuation_lines_width;
3276 CHECK_IT (it);
3280 /* Initialize IT for stepping through current_buffer in window W
3281 starting in the line following ROW, i.e. starting at ROW->end.
3282 Value is false if there are overlay strings with newlines at ROW's
3283 end position. */
3285 static bool
3286 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3288 bool success = false;
3290 if (init_from_display_pos (it, w, &row->end))
3292 if (row->continued_p)
3293 it->continuation_lines_width
3294 = row->continuation_lines_width + row->pixel_width;
3295 CHECK_IT (it);
3296 success = true;
3299 return success;
3305 /***********************************************************************
3306 Text properties
3307 ***********************************************************************/
3309 /* Called when IT reaches IT->stop_charpos. Handle text property and
3310 overlay changes. Set IT->stop_charpos to the next position where
3311 to stop. */
3313 static void
3314 handle_stop (struct it *it)
3316 enum prop_handled handled;
3317 bool handle_overlay_change_p;
3318 struct props *p;
3320 it->dpvec = NULL;
3321 it->current.dpvec_index = -1;
3322 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3323 it->ellipsis_p = false;
3325 /* Use face of preceding text for ellipsis (if invisible) */
3326 if (it->selective_display_ellipsis_p)
3327 it->saved_face_id = it->face_id;
3329 /* Here's the description of the semantics of, and the logic behind,
3330 the various HANDLED_* statuses:
3332 HANDLED_NORMALLY means the handler did its job, and the loop
3333 should proceed to calling the next handler in order.
3335 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3336 change in the properties and overlays at current position, so the
3337 loop should be restarted, to re-invoke the handlers that were
3338 already called. This happens when fontification-functions were
3339 called by handle_fontified_prop, and actually fontified
3340 something. Another case where HANDLED_RECOMPUTE_PROPS is
3341 returned is when we discover overlay strings that need to be
3342 displayed right away. The loop below will continue for as long
3343 as the status is HANDLED_RECOMPUTE_PROPS.
3345 HANDLED_RETURN means return immediately to the caller, to
3346 continue iteration without calling any further handlers. This is
3347 used when we need to act on some property right away, for example
3348 when we need to display the ellipsis or a replacing display
3349 property, such as display string or image.
3351 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3352 consumed, and the handler switched to the next overlay string.
3353 This signals the loop below to refrain from looking for more
3354 overlays before all the overlay strings of the current overlay
3355 are processed.
3357 Some of the handlers called by the loop push the iterator state
3358 onto the stack (see 'push_it'), and arrange for the iteration to
3359 continue with another object, such as an image, a display string,
3360 or an overlay string. In most such cases, it->stop_charpos is
3361 set to the first character of the string, so that when the
3362 iteration resumes, this function will immediately be called
3363 again, to examine the properties at the beginning of the string.
3365 When a display or overlay string is exhausted, the iterator state
3366 is popped (see 'pop_it'), and iteration continues with the
3367 previous object. Again, in many such cases this function is
3368 called again to find the next position where properties might
3369 change. */
3373 handled = HANDLED_NORMALLY;
3375 /* Call text property handlers. */
3376 for (p = it_props; p->handler; ++p)
3378 handled = p->handler (it);
3380 if (handled == HANDLED_RECOMPUTE_PROPS)
3381 break;
3382 else if (handled == HANDLED_RETURN)
3384 /* We still want to show before and after strings from
3385 overlays even if the actual buffer text is replaced. */
3386 if (!handle_overlay_change_p
3387 || it->sp > 1
3388 /* Don't call get_overlay_strings_1 if we already
3389 have overlay strings loaded, because doing so
3390 will load them again and push the iterator state
3391 onto the stack one more time, which is not
3392 expected by the rest of the code that processes
3393 overlay strings. */
3394 || (it->current.overlay_string_index < 0
3395 && !get_overlay_strings_1 (it, 0, false)))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->string_from_display_prop_p = false;
3413 it->from_disp_prop_p = false;
3414 handle_overlay_change_p = false;
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 break;
3419 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3420 handle_overlay_change_p = false;
3423 if (handled != HANDLED_RECOMPUTE_PROPS)
3425 /* Don't check for overlay strings below when set to deliver
3426 characters from a display vector. */
3427 if (it->method == GET_FROM_DISPLAY_VECTOR)
3428 handle_overlay_change_p = false;
3430 /* Handle overlay changes.
3431 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3432 if it finds overlays. */
3433 if (handle_overlay_change_p)
3434 handled = handle_overlay_change (it);
3437 if (it->ellipsis_p)
3439 setup_for_ellipsis (it, 0);
3440 break;
3443 while (handled == HANDLED_RECOMPUTE_PROPS);
3445 /* Determine where to stop next. */
3446 if (handled == HANDLED_NORMALLY)
3447 compute_stop_pos (it);
3451 /* Compute IT->stop_charpos from text property and overlay change
3452 information for IT's current position. */
3454 static void
3455 compute_stop_pos (struct it *it)
3457 register INTERVAL iv, next_iv;
3458 Lisp_Object object, limit, position;
3459 ptrdiff_t charpos, bytepos;
3461 if (STRINGP (it->string))
3463 /* Strings are usually short, so don't limit the search for
3464 properties. */
3465 it->stop_charpos = it->end_charpos;
3466 object = it->string;
3467 limit = Qnil;
3468 charpos = IT_STRING_CHARPOS (*it);
3469 bytepos = IT_STRING_BYTEPOS (*it);
3471 else
3473 ptrdiff_t pos;
3475 /* If end_charpos is out of range for some reason, such as a
3476 misbehaving display function, rationalize it (Bug#5984). */
3477 if (it->end_charpos > ZV)
3478 it->end_charpos = ZV;
3479 it->stop_charpos = it->end_charpos;
3481 /* If next overlay change is in front of the current stop pos
3482 (which is IT->end_charpos), stop there. Note: value of
3483 next_overlay_change is point-max if no overlay change
3484 follows. */
3485 charpos = IT_CHARPOS (*it);
3486 bytepos = IT_BYTEPOS (*it);
3487 pos = next_overlay_change (charpos);
3488 if (pos < it->stop_charpos)
3489 it->stop_charpos = pos;
3491 /* Set up variables for computing the stop position from text
3492 property changes. */
3493 XSETBUFFER (object, current_buffer);
3494 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3497 /* Get the interval containing IT's position. Value is a null
3498 interval if there isn't such an interval. */
3499 position = make_number (charpos);
3500 iv = validate_interval_range (object, &position, &position, false);
3501 if (iv)
3503 Lisp_Object values_here[LAST_PROP_IDX];
3504 struct props *p;
3506 /* Get properties here. */
3507 for (p = it_props; p->handler; ++p)
3508 values_here[p->idx] = textget (iv->plist,
3509 builtin_lisp_symbol (p->name));
3511 /* Look for an interval following iv that has different
3512 properties. */
3513 for (next_iv = next_interval (iv);
3514 (next_iv
3515 && (NILP (limit)
3516 || XFASTINT (limit) > next_iv->position));
3517 next_iv = next_interval (next_iv))
3519 for (p = it_props; p->handler; ++p)
3521 Lisp_Object new_value = textget (next_iv->plist,
3522 builtin_lisp_symbol (p->name));
3523 if (!EQ (values_here[p->idx], new_value))
3524 break;
3527 if (p->handler)
3528 break;
3531 if (next_iv)
3533 if (INTEGERP (limit)
3534 && next_iv->position >= XFASTINT (limit))
3535 /* No text property change up to limit. */
3536 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3537 else
3538 /* Text properties change in next_iv. */
3539 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3543 if (it->cmp_it.id < 0)
3545 ptrdiff_t stoppos = it->end_charpos;
3547 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3548 stoppos = -1;
3549 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3550 stoppos, it->string);
3553 eassert (STRINGP (it->string)
3554 || (it->stop_charpos >= BEGV
3555 && it->stop_charpos >= IT_CHARPOS (*it)));
3559 /* Return the position of the next overlay change after POS in
3560 current_buffer. Value is point-max if no overlay change
3561 follows. This is like `next-overlay-change' but doesn't use
3562 xmalloc. */
3564 static ptrdiff_t
3565 next_overlay_change (ptrdiff_t pos)
3567 ptrdiff_t i, noverlays;
3568 ptrdiff_t endpos;
3569 Lisp_Object *overlays;
3570 USE_SAFE_ALLOCA;
3572 /* Get all overlays at the given position. */
3573 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3575 /* If any of these overlays ends before endpos,
3576 use its ending point instead. */
3577 for (i = 0; i < noverlays; ++i)
3579 Lisp_Object oend;
3580 ptrdiff_t oendpos;
3582 oend = OVERLAY_END (overlays[i]);
3583 oendpos = OVERLAY_POSITION (oend);
3584 endpos = min (endpos, oendpos);
3587 SAFE_FREE ();
3588 return endpos;
3591 /* How many characters forward to search for a display property or
3592 display string. Searching too far forward makes the bidi display
3593 sluggish, especially in small windows. */
3594 #define MAX_DISP_SCAN 250
3596 /* Return the character position of a display string at or after
3597 position specified by POSITION. If no display string exists at or
3598 after POSITION, return ZV. A display string is either an overlay
3599 with `display' property whose value is a string, or a `display'
3600 text property whose value is a string. STRING is data about the
3601 string to iterate; if STRING->lstring is nil, we are iterating a
3602 buffer. FRAME_WINDOW_P is true when we are displaying a window
3603 on a GUI frame. DISP_PROP is set to zero if we searched
3604 MAX_DISP_SCAN characters forward without finding any display
3605 strings, non-zero otherwise. It is set to 2 if the display string
3606 uses any kind of `(space ...)' spec that will produce a stretch of
3607 white space in the text area. */
3608 ptrdiff_t
3609 compute_display_string_pos (struct text_pos *position,
3610 struct bidi_string_data *string,
3611 struct window *w,
3612 bool frame_window_p, int *disp_prop)
3614 /* OBJECT = nil means current buffer. */
3615 Lisp_Object object, object1;
3616 Lisp_Object pos, spec, limpos;
3617 bool string_p = string && (STRINGP (string->lstring) || string->s);
3618 ptrdiff_t eob = string_p ? string->schars : ZV;
3619 ptrdiff_t begb = string_p ? 0 : BEGV;
3620 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3621 ptrdiff_t lim =
3622 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3623 struct text_pos tpos;
3624 int rv = 0;
3626 if (string && STRINGP (string->lstring))
3627 object1 = object = string->lstring;
3628 else if (w && !string_p)
3630 XSETWINDOW (object, w);
3631 object1 = Qnil;
3633 else
3634 object1 = object = Qnil;
3636 *disp_prop = 1;
3638 if (charpos >= eob
3639 /* We don't support display properties whose values are strings
3640 that have display string properties. */
3641 || string->from_disp_str
3642 /* C strings cannot have display properties. */
3643 || (string->s && !STRINGP (object)))
3645 *disp_prop = 0;
3646 return eob;
3649 /* If the character at CHARPOS is where the display string begins,
3650 return CHARPOS. */
3651 pos = make_number (charpos);
3652 if (STRINGP (object))
3653 bufpos = string->bufpos;
3654 else
3655 bufpos = charpos;
3656 tpos = *position;
3657 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3658 && (charpos <= begb
3659 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3660 object),
3661 spec))
3662 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3663 frame_window_p)))
3665 if (rv == 2)
3666 *disp_prop = 2;
3667 return charpos;
3670 /* Look forward for the first character with a `display' property
3671 that will replace the underlying text when displayed. */
3672 limpos = make_number (lim);
3673 do {
3674 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3675 CHARPOS (tpos) = XFASTINT (pos);
3676 if (CHARPOS (tpos) >= lim)
3678 *disp_prop = 0;
3679 break;
3681 if (STRINGP (object))
3682 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3683 else
3684 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3685 spec = Fget_char_property (pos, Qdisplay, object);
3686 if (!STRINGP (object))
3687 bufpos = CHARPOS (tpos);
3688 } while (NILP (spec)
3689 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3690 bufpos, frame_window_p)));
3691 if (rv == 2)
3692 *disp_prop = 2;
3694 return CHARPOS (tpos);
3697 /* Return the character position of the end of the display string that
3698 started at CHARPOS. If there's no display string at CHARPOS,
3699 return -1. A display string is either an overlay with `display'
3700 property whose value is a string or a `display' text property whose
3701 value is a string. */
3702 ptrdiff_t
3703 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3705 /* OBJECT = nil means current buffer. */
3706 Lisp_Object object =
3707 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3708 Lisp_Object pos = make_number (charpos);
3709 ptrdiff_t eob =
3710 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3712 if (charpos >= eob || (string->s && !STRINGP (object)))
3713 return eob;
3715 /* It could happen that the display property or overlay was removed
3716 since we found it in compute_display_string_pos above. One way
3717 this can happen is if JIT font-lock was called (through
3718 handle_fontified_prop), and jit-lock-functions remove text
3719 properties or overlays from the portion of buffer that includes
3720 CHARPOS. Muse mode is known to do that, for example. In this
3721 case, we return -1 to the caller, to signal that no display
3722 string is actually present at CHARPOS. See bidi_fetch_char for
3723 how this is handled.
3725 An alternative would be to never look for display properties past
3726 it->stop_charpos. But neither compute_display_string_pos nor
3727 bidi_fetch_char that calls it know or care where the next
3728 stop_charpos is. */
3729 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3730 return -1;
3732 /* Look forward for the first character where the `display' property
3733 changes. */
3734 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3736 return XFASTINT (pos);
3741 /***********************************************************************
3742 Fontification
3743 ***********************************************************************/
3745 /* Handle changes in the `fontified' property of the current buffer by
3746 calling hook functions from Qfontification_functions to fontify
3747 regions of text. */
3749 static enum prop_handled
3750 handle_fontified_prop (struct it *it)
3752 Lisp_Object prop, pos;
3753 enum prop_handled handled = HANDLED_NORMALLY;
3755 if (!NILP (Vmemory_full))
3756 return handled;
3758 /* Get the value of the `fontified' property at IT's current buffer
3759 position. (The `fontified' property doesn't have a special
3760 meaning in strings.) If the value is nil, call functions from
3761 Qfontification_functions. */
3762 if (!STRINGP (it->string)
3763 && it->s == NULL
3764 && !NILP (Vfontification_functions)
3765 && !NILP (Vrun_hooks)
3766 && (pos = make_number (IT_CHARPOS (*it)),
3767 prop = Fget_char_property (pos, Qfontified, Qnil),
3768 /* Ignore the special cased nil value always present at EOB since
3769 no amount of fontifying will be able to change it. */
3770 NILP (prop) && IT_CHARPOS (*it) < Z))
3772 ptrdiff_t count = SPECPDL_INDEX ();
3773 Lisp_Object val;
3774 struct buffer *obuf = current_buffer;
3775 ptrdiff_t begv = BEGV, zv = ZV;
3776 bool old_clip_changed = current_buffer->clip_changed;
3778 val = Vfontification_functions;
3779 specbind (Qfontification_functions, Qnil);
3781 eassert (it->end_charpos == ZV);
3783 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3784 safe_call1 (val, pos);
3785 else
3787 Lisp_Object fns, fn;
3789 fns = Qnil;
3791 for (; CONSP (val); val = XCDR (val))
3793 fn = XCAR (val);
3795 if (EQ (fn, Qt))
3797 /* A value of t indicates this hook has a local
3798 binding; it means to run the global binding too.
3799 In a global value, t should not occur. If it
3800 does, we must ignore it to avoid an endless
3801 loop. */
3802 for (fns = Fdefault_value (Qfontification_functions);
3803 CONSP (fns);
3804 fns = XCDR (fns))
3806 fn = XCAR (fns);
3807 if (!EQ (fn, Qt))
3808 safe_call1 (fn, pos);
3811 else
3812 safe_call1 (fn, pos);
3816 unbind_to (count, Qnil);
3818 /* Fontification functions routinely call `save-restriction'.
3819 Normally, this tags clip_changed, which can confuse redisplay
3820 (see discussion in Bug#6671). Since we don't perform any
3821 special handling of fontification changes in the case where
3822 `save-restriction' isn't called, there's no point doing so in
3823 this case either. So, if the buffer's restrictions are
3824 actually left unchanged, reset clip_changed. */
3825 if (obuf == current_buffer)
3827 if (begv == BEGV && zv == ZV)
3828 current_buffer->clip_changed = old_clip_changed;
3830 /* There isn't much we can reasonably do to protect against
3831 misbehaving fontification, but here's a fig leaf. */
3832 else if (BUFFER_LIVE_P (obuf))
3833 set_buffer_internal_1 (obuf);
3835 /* The fontification code may have added/removed text.
3836 It could do even a lot worse, but let's at least protect against
3837 the most obvious case where only the text past `pos' gets changed',
3838 as is/was done in grep.el where some escapes sequences are turned
3839 into face properties (bug#7876). */
3840 it->end_charpos = ZV;
3842 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3843 something. This avoids an endless loop if they failed to
3844 fontify the text for which reason ever. */
3845 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3846 handled = HANDLED_RECOMPUTE_PROPS;
3849 return handled;
3854 /***********************************************************************
3855 Faces
3856 ***********************************************************************/
3858 /* Set up iterator IT from face properties at its current position.
3859 Called from handle_stop. */
3861 static enum prop_handled
3862 handle_face_prop (struct it *it)
3864 int new_face_id;
3865 ptrdiff_t next_stop;
3867 if (!STRINGP (it->string))
3869 new_face_id
3870 = face_at_buffer_position (it->w,
3871 IT_CHARPOS (*it),
3872 &next_stop,
3873 (IT_CHARPOS (*it)
3874 + TEXT_PROP_DISTANCE_LIMIT),
3875 false, it->base_face_id);
3877 /* Is this a start of a run of characters with box face?
3878 Caveat: this can be called for a freshly initialized
3879 iterator; face_id is -1 in this case. We know that the new
3880 face will not change until limit, i.e. if the new face has a
3881 box, all characters up to limit will have one. But, as
3882 usual, we don't know whether limit is really the end. */
3883 if (new_face_id != it->face_id)
3885 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3886 /* If it->face_id is -1, old_face below will be NULL, see
3887 the definition of FACE_FROM_ID_OR_NULL. This will happen
3888 if this is the initial call that gets the face. */
3889 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3891 /* If the value of face_id of the iterator is -1, we have to
3892 look in front of IT's position and see whether there is a
3893 face there that's different from new_face_id. */
3894 if (!old_face && IT_CHARPOS (*it) > BEG)
3896 int prev_face_id = face_before_it_pos (it);
3898 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3901 /* If the new face has a box, but the old face does not,
3902 this is the start of a run of characters with box face,
3903 i.e. this character has a shadow on the left side. */
3904 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3905 && (old_face == NULL || !old_face->box));
3906 it->face_box_p = new_face->box != FACE_NO_BOX;
3909 else
3911 int base_face_id;
3912 ptrdiff_t bufpos;
3913 int i;
3914 Lisp_Object from_overlay
3915 = (it->current.overlay_string_index >= 0
3916 ? it->string_overlays[it->current.overlay_string_index
3917 % OVERLAY_STRING_CHUNK_SIZE]
3918 : Qnil);
3920 /* See if we got to this string directly or indirectly from
3921 an overlay property. That includes the before-string or
3922 after-string of an overlay, strings in display properties
3923 provided by an overlay, their text properties, etc.
3925 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3926 if (! NILP (from_overlay))
3927 for (i = it->sp - 1; i >= 0; i--)
3929 if (it->stack[i].current.overlay_string_index >= 0)
3930 from_overlay
3931 = it->string_overlays[it->stack[i].current.overlay_string_index
3932 % OVERLAY_STRING_CHUNK_SIZE];
3933 else if (! NILP (it->stack[i].from_overlay))
3934 from_overlay = it->stack[i].from_overlay;
3936 if (!NILP (from_overlay))
3937 break;
3940 if (! NILP (from_overlay))
3942 bufpos = IT_CHARPOS (*it);
3943 /* For a string from an overlay, the base face depends
3944 only on text properties and ignores overlays. */
3945 base_face_id
3946 = face_for_overlay_string (it->w,
3947 IT_CHARPOS (*it),
3948 &next_stop,
3949 (IT_CHARPOS (*it)
3950 + TEXT_PROP_DISTANCE_LIMIT),
3951 false,
3952 from_overlay);
3954 else
3956 bufpos = 0;
3958 /* For strings from a `display' property, use the face at
3959 IT's current buffer position as the base face to merge
3960 with, so that overlay strings appear in the same face as
3961 surrounding text, unless they specify their own faces.
3962 For strings from wrap-prefix and line-prefix properties,
3963 use the default face, possibly remapped via
3964 Vface_remapping_alist. */
3965 /* Note that the fact that we use the face at _buffer_
3966 position means that a 'display' property on an overlay
3967 string will not inherit the face of that overlay string,
3968 but will instead revert to the face of buffer text
3969 covered by the overlay. This is visible, e.g., when the
3970 overlay specifies a box face, but neither the buffer nor
3971 the display string do. This sounds like a design bug,
3972 but Emacs always did that since v21.1, so changing that
3973 might be a big deal. */
3974 base_face_id = it->string_from_prefix_prop_p
3975 ? (!NILP (Vface_remapping_alist)
3976 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3977 : DEFAULT_FACE_ID)
3978 : underlying_face_id (it);
3981 new_face_id = face_at_string_position (it->w,
3982 it->string,
3983 IT_STRING_CHARPOS (*it),
3984 bufpos,
3985 &next_stop,
3986 base_face_id, false);
3988 /* Is this a start of a run of characters with box? Caveat:
3989 this can be called for a freshly allocated iterator; face_id
3990 is -1 is this case. We know that the new face will not
3991 change until the next check pos, i.e. if the new face has a
3992 box, all characters up to that position will have a
3993 box. But, as usual, we don't know whether that position
3994 is really the end. */
3995 if (new_face_id != it->face_id)
3997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3998 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4000 /* If new face has a box but old face hasn't, this is the
4001 start of a run of characters with box, i.e. it has a
4002 shadow on the left side. */
4003 it->start_of_box_run_p
4004 = new_face->box && (old_face == NULL || !old_face->box);
4005 it->face_box_p = new_face->box != FACE_NO_BOX;
4009 it->face_id = new_face_id;
4010 return HANDLED_NORMALLY;
4014 /* Return the ID of the face ``underlying'' IT's current position,
4015 which is in a string. If the iterator is associated with a
4016 buffer, return the face at IT's current buffer position.
4017 Otherwise, use the iterator's base_face_id. */
4019 static int
4020 underlying_face_id (struct it *it)
4022 int face_id = it->base_face_id, i;
4024 eassert (STRINGP (it->string));
4026 for (i = it->sp - 1; i >= 0; --i)
4027 if (NILP (it->stack[i].string))
4028 face_id = it->stack[i].face_id;
4030 return face_id;
4034 /* Compute the face one character before or after the current position
4035 of IT, in the visual order. BEFORE_P means get the face
4036 in front (to the left in L2R paragraphs, to the right in R2L
4037 paragraphs) of IT's screen position. Value is the ID of the face. */
4039 static int
4040 face_before_or_after_it_pos (struct it *it, bool before_p)
4042 int face_id, limit;
4043 ptrdiff_t next_check_charpos;
4044 struct it it_copy;
4045 void *it_copy_data = NULL;
4047 eassert (it->s == NULL);
4049 if (STRINGP (it->string))
4051 ptrdiff_t bufpos, charpos;
4052 int base_face_id;
4054 /* No face change past the end of the string (for the case
4055 we are padding with spaces). No face change before the
4056 string start. */
4057 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4058 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4059 return it->face_id;
4061 if (!it->bidi_p)
4063 /* Set charpos to the position before or after IT's current
4064 position, in the logical order, which in the non-bidi
4065 case is the same as the visual order. */
4066 if (before_p)
4067 charpos = IT_STRING_CHARPOS (*it) - 1;
4068 else if (it->what == IT_COMPOSITION)
4069 /* For composition, we must check the character after the
4070 composition. */
4071 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4072 else
4073 charpos = IT_STRING_CHARPOS (*it) + 1;
4075 else
4077 if (before_p)
4079 /* With bidi iteration, the character before the current
4080 in the visual order cannot be found by simple
4081 iteration, because "reverse" reordering is not
4082 supported. Instead, we need to start from the string
4083 beginning and go all the way to the current string
4084 position, remembering the previous position. */
4085 /* Ignore face changes before the first visible
4086 character on this display line. */
4087 if (it->current_x <= it->first_visible_x)
4088 return it->face_id;
4089 SAVE_IT (it_copy, *it, it_copy_data);
4090 IT_STRING_CHARPOS (it_copy) = 0;
4091 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4095 charpos = IT_STRING_CHARPOS (it_copy);
4096 if (charpos >= SCHARS (it->string))
4097 break;
4098 bidi_move_to_visually_next (&it_copy.bidi_it);
4100 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 int current_x;
4183 /* With bidi iteration, the character before the current
4184 in the visual order cannot be found by simple
4185 iteration, because "reverse" reordering is not
4186 supported. Instead, we need to use the move_it_*
4187 family of functions, and move to the previous
4188 character starting from the beginning of the visual
4189 line. */
4190 /* Ignore face changes before the first visible
4191 character on this display line. */
4192 if (it->current_x <= it->first_visible_x)
4193 return it->face_id;
4194 SAVE_IT (it_copy, *it, it_copy_data);
4195 /* Implementation note: Since move_it_in_display_line
4196 works in the iterator geometry, and thinks the first
4197 character is always the leftmost, even in R2L lines,
4198 we don't need to distinguish between the R2L and L2R
4199 cases here. */
4200 current_x = it_copy.current_x;
4201 move_it_vertically_backward (&it_copy, 0);
4202 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4203 pos = it_copy.current.pos;
4204 RESTORE_IT (it, it, it_copy_data);
4206 else
4208 /* Set charpos to the buffer position of the character
4209 that comes after IT's current position in the visual
4210 order. */
4211 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4213 it_copy = *it;
4214 while (n--)
4215 bidi_move_to_visually_next (&it_copy.bidi_it);
4217 SET_TEXT_POS (pos,
4218 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4221 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4223 /* Determine face for CHARSET_ASCII, or unibyte. */
4224 face_id = face_at_buffer_position (it->w,
4225 CHARPOS (pos),
4226 &next_check_charpos,
4227 limit, false, -1);
4229 /* Correct the face for charsets different from ASCII. Do it
4230 for the multibyte case only. The face returned above is
4231 suitable for unibyte text if current_buffer is unibyte. */
4232 if (it->multibyte_p)
4234 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4235 struct face *face = FACE_FROM_ID (it->f, face_id);
4236 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4240 return face_id;
4245 /***********************************************************************
4246 Invisible text
4247 ***********************************************************************/
4249 /* Set up iterator IT from invisible properties at its current
4250 position. Called from handle_stop. */
4252 static enum prop_handled
4253 handle_invisible_prop (struct it *it)
4255 enum prop_handled handled = HANDLED_NORMALLY;
4256 int invis;
4257 Lisp_Object prop;
4259 if (STRINGP (it->string))
4261 Lisp_Object end_charpos, limit;
4263 /* Get the value of the invisible text property at the
4264 current position. Value will be nil if there is no such
4265 property. */
4266 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4267 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4268 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4272 /* Record whether we have to display an ellipsis for the
4273 invisible text. */
4274 bool display_ellipsis_p = (invis == 2);
4275 ptrdiff_t len, endpos;
4277 handled = HANDLED_RECOMPUTE_PROPS;
4279 /* Get the position at which the next visible text can be
4280 found in IT->string, if any. */
4281 endpos = len = SCHARS (it->string);
4282 XSETINT (limit, len);
4285 end_charpos
4286 = Fnext_single_property_change (end_charpos, Qinvisible,
4287 it->string, limit);
4288 /* Since LIMIT is always an integer, so should be the
4289 value returned by Fnext_single_property_change. */
4290 eassert (INTEGERP (end_charpos));
4291 if (INTEGERP (end_charpos))
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis == 2)
4297 display_ellipsis_p = true;
4299 else /* Should never happen; but if it does, exit the loop. */
4300 endpos = len;
4302 while (invis != 0 && endpos < len);
4304 if (display_ellipsis_p)
4305 it->ellipsis_p = true;
4307 if (endpos < len)
4309 /* Text at END_CHARPOS is visible. Move IT there. */
4310 struct text_pos old;
4311 ptrdiff_t oldpos;
4313 old = it->current.string_pos;
4314 oldpos = CHARPOS (old);
4315 if (it->bidi_p)
4317 if (it->bidi_it.first_elt
4318 && it->bidi_it.charpos < SCHARS (it->string))
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, true);
4321 /* Bidi-iterate out of the invisible text. */
4324 bidi_move_to_visually_next (&it->bidi_it);
4326 while (oldpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < endpos);
4329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4330 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4331 if (IT_CHARPOS (*it) >= endpos)
4332 it->prev_stop = endpos;
4334 else
4336 IT_STRING_CHARPOS (*it) = endpos;
4337 compute_string_pos (&it->current.string_pos, old, it->string);
4340 else
4342 /* The rest of the string is invisible. If this is an
4343 overlay string, proceed with the next overlay string
4344 or whatever comes and return a character from there. */
4345 if (it->current.overlay_string_index >= 0
4346 && !display_ellipsis_p)
4348 next_overlay_string (it);
4349 /* Don't check for overlay strings when we just
4350 finished processing them. */
4351 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4353 else
4355 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4356 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4361 else
4363 ptrdiff_t newpos, next_stop, start_charpos, tem;
4364 Lisp_Object pos, overlay;
4366 /* First of all, is there invisible text at this position? */
4367 tem = start_charpos = IT_CHARPOS (*it);
4368 pos = make_number (tem);
4369 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4370 &overlay);
4371 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4373 /* If we are on invisible text, skip over it. */
4374 if (invis != 0 && start_charpos < it->end_charpos)
4376 /* Record whether we have to display an ellipsis for the
4377 invisible text. */
4378 bool display_ellipsis_p = invis == 2;
4380 handled = HANDLED_RECOMPUTE_PROPS;
4382 /* Loop skipping over invisible text. The loop is left at
4383 ZV or with IT on the first char being visible again. */
4386 /* Try to skip some invisible text. Return value is the
4387 position reached which can be equal to where we start
4388 if there is nothing invisible there. This skips both
4389 over invisible text properties and overlays with
4390 invisible property. */
4391 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4393 /* If we skipped nothing at all we weren't at invisible
4394 text in the first place. If everything to the end of
4395 the buffer was skipped, end the loop. */
4396 if (newpos == tem || newpos >= ZV)
4397 invis = 0;
4398 else
4400 /* We skipped some characters but not necessarily
4401 all there are. Check if we ended up on visible
4402 text. Fget_char_property returns the property of
4403 the char before the given position, i.e. if we
4404 get invis = 0, this means that the char at
4405 newpos is visible. */
4406 pos = make_number (newpos);
4407 prop = Fget_char_property (pos, Qinvisible, it->window);
4408 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4411 /* If we ended up on invisible text, proceed to
4412 skip starting with next_stop. */
4413 if (invis != 0)
4414 tem = next_stop;
4416 /* If there are adjacent invisible texts, don't lose the
4417 second one's ellipsis. */
4418 if (invis == 2)
4419 display_ellipsis_p = true;
4421 while (invis != 0);
4423 /* The position newpos is now either ZV or on visible text. */
4424 if (it->bidi_p)
4426 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4427 bool on_newline
4428 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4429 bool after_newline
4430 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4432 /* If the invisible text ends on a newline or on a
4433 character after a newline, we can avoid the costly,
4434 character by character, bidi iteration to NEWPOS, and
4435 instead simply reseat the iterator there. That's
4436 because all bidi reordering information is tossed at
4437 the newline. This is a big win for modes that hide
4438 complete lines, like Outline, Org, etc. */
4439 if (on_newline || after_newline)
4441 struct text_pos tpos;
4442 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4444 SET_TEXT_POS (tpos, newpos, bpos);
4445 reseat_1 (it, tpos, false);
4446 /* If we reseat on a newline/ZV, we need to prep the
4447 bidi iterator for advancing to the next character
4448 after the newline/EOB, keeping the current paragraph
4449 direction (so that PRODUCE_GLYPHS does TRT wrt
4450 prepending/appending glyphs to a glyph row). */
4451 if (on_newline)
4453 it->bidi_it.first_elt = false;
4454 it->bidi_it.paragraph_dir = pdir;
4455 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4456 it->bidi_it.nchars = 1;
4457 it->bidi_it.ch_len = 1;
4460 else /* Must use the slow method. */
4462 /* With bidi iteration, the region of invisible text
4463 could start and/or end in the middle of a
4464 non-base embedding level. Therefore, we need to
4465 skip invisible text using the bidi iterator,
4466 starting at IT's current position, until we find
4467 ourselves outside of the invisible text.
4468 Skipping invisible text _after_ bidi iteration
4469 avoids affecting the visual order of the
4470 displayed text when invisible properties are
4471 added or removed. */
4472 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4474 /* If we were `reseat'ed to a new paragraph,
4475 determine the paragraph base direction. We
4476 need to do it now because
4477 next_element_from_buffer may not have a
4478 chance to do it, if we are going to skip any
4479 text at the beginning, which resets the
4480 FIRST_ELT flag. */
4481 bidi_paragraph_init (it->paragraph_embedding,
4482 &it->bidi_it, true);
4486 bidi_move_to_visually_next (&it->bidi_it);
4488 while (it->stop_charpos <= it->bidi_it.charpos
4489 && it->bidi_it.charpos < newpos);
4490 IT_CHARPOS (*it) = it->bidi_it.charpos;
4491 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4492 /* If we overstepped NEWPOS, record its position in
4493 the iterator, so that we skip invisible text if
4494 later the bidi iteration lands us in the
4495 invisible region again. */
4496 if (IT_CHARPOS (*it) >= newpos)
4497 it->prev_stop = newpos;
4500 else
4502 IT_CHARPOS (*it) = newpos;
4503 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4506 if (display_ellipsis_p)
4508 /* Make sure that the glyphs of the ellipsis will get
4509 correct `charpos' values. If we would not update
4510 it->position here, the glyphs would belong to the
4511 last visible character _before_ the invisible
4512 text, which confuses `set_cursor_from_row'.
4514 We use the last invisible position instead of the
4515 first because this way the cursor is always drawn on
4516 the first "." of the ellipsis, whenever PT is inside
4517 the invisible text. Otherwise the cursor would be
4518 placed _after_ the ellipsis when the point is after the
4519 first invisible character. */
4520 if (!STRINGP (it->object))
4522 it->position.charpos = newpos - 1;
4523 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4527 /* If there are before-strings at the start of invisible
4528 text, and the text is invisible because of a text
4529 property, arrange to show before-strings because 20.x did
4530 it that way. (If the text is invisible because of an
4531 overlay property instead of a text property, this is
4532 already handled in the overlay code.) */
4533 if (NILP (overlay)
4534 && get_overlay_strings (it, it->stop_charpos))
4536 handled = HANDLED_RECOMPUTE_PROPS;
4537 if (it->sp > 0)
4539 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4540 /* The call to get_overlay_strings above recomputes
4541 it->stop_charpos, but it only considers changes
4542 in properties and overlays beyond iterator's
4543 current position. This causes us to miss changes
4544 that happen exactly where the invisible property
4545 ended. So we play it safe here and force the
4546 iterator to check for potential stop positions
4547 immediately after the invisible text. Note that
4548 if get_overlay_strings returns true, it
4549 normally also pushed the iterator stack, so we
4550 need to update the stop position in the slot
4551 below the current one. */
4552 it->stack[it->sp - 1].stop_charpos
4553 = CHARPOS (it->stack[it->sp - 1].current.pos);
4556 else if (display_ellipsis_p)
4558 it->ellipsis_p = true;
4559 /* Let the ellipsis display before
4560 considering any properties of the following char.
4561 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4562 handled = HANDLED_RETURN;
4567 return handled;
4571 /* Make iterator IT return `...' next.
4572 Replaces LEN characters from buffer. */
4574 static void
4575 setup_for_ellipsis (struct it *it, int len)
4577 /* Use the display table definition for `...'. Invalid glyphs
4578 will be handled by the method returning elements from dpvec. */
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4581 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4582 it->dpvec = v->contents;
4583 it->dpend = v->contents + v->header.size;
4585 else
4587 /* Default `...'. */
4588 it->dpvec = default_invis_vector;
4589 it->dpend = default_invis_vector + 3;
4592 it->dpvec_char_len = len;
4593 it->current.dpvec_index = 0;
4594 it->dpvec_face_id = -1;
4596 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4597 face as the preceding text. IT->saved_face_id was set in
4598 handle_stop to the face of the preceding character, and will be
4599 different from IT->face_id only if the invisible text skipped in
4600 handle_invisible_prop has some non-default face on its first
4601 character. We thus ignore the face of the invisible text when we
4602 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4603 if (it->saved_face_id >= 0)
4604 it->face_id = it->saved_face_id;
4606 /* If the ellipsis represents buffer text, it means we advanced in
4607 the buffer, so we should no longer ignore overlay strings. */
4608 if (it->method == GET_FROM_BUFFER)
4609 it->ignore_overlay_strings_at_pos_p = false;
4611 it->method = GET_FROM_DISPLAY_VECTOR;
4612 it->ellipsis_p = true;
4617 /***********************************************************************
4618 'display' property
4619 ***********************************************************************/
4621 /* Set up iterator IT from `display' property at its current position.
4622 Called from handle_stop.
4623 We return HANDLED_RETURN if some part of the display property
4624 overrides the display of the buffer text itself.
4625 Otherwise we return HANDLED_NORMALLY. */
4627 static enum prop_handled
4628 handle_display_prop (struct it *it)
4630 Lisp_Object propval, object, overlay;
4631 struct text_pos *position;
4632 ptrdiff_t bufpos;
4633 /* Nonzero if some property replaces the display of the text itself. */
4634 int display_replaced = 0;
4636 if (STRINGP (it->string))
4638 object = it->string;
4639 position = &it->current.string_pos;
4640 bufpos = CHARPOS (it->current.pos);
4642 else
4644 XSETWINDOW (object, it->w);
4645 position = &it->current.pos;
4646 bufpos = CHARPOS (*position);
4649 /* Reset those iterator values set from display property values. */
4650 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4651 it->space_width = Qnil;
4652 it->font_height = Qnil;
4653 it->voffset = 0;
4655 /* We don't support recursive `display' properties, i.e. string
4656 values that have a string `display' property, that have a string
4657 `display' property etc. */
4658 if (!it->string_from_display_prop_p)
4659 it->area = TEXT_AREA;
4661 propval = get_char_property_and_overlay (make_number (position->charpos),
4662 Qdisplay, object, &overlay);
4663 if (NILP (propval))
4664 return HANDLED_NORMALLY;
4665 /* Now OVERLAY is the overlay that gave us this property, or nil
4666 if it was a text property. */
4668 if (!STRINGP (it->string))
4669 object = it->w->contents;
4671 display_replaced = handle_display_spec (it, propval, object, overlay,
4672 position, bufpos,
4673 FRAME_WINDOW_P (it->f));
4674 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4677 /* Subroutine of handle_display_prop. Returns non-zero if the display
4678 specification in SPEC is a replacing specification, i.e. it would
4679 replace the text covered by `display' property with something else,
4680 such as an image or a display string. If SPEC includes any kind or
4681 `(space ...) specification, the value is 2; this is used by
4682 compute_display_string_pos, which see.
4684 See handle_single_display_spec for documentation of arguments.
4685 FRAME_WINDOW_P is true if the window being redisplayed is on a
4686 GUI frame; this argument is used only if IT is NULL, see below.
4688 IT can be NULL, if this is called by the bidi reordering code
4689 through compute_display_string_pos, which see. In that case, this
4690 function only examines SPEC, but does not otherwise "handle" it, in
4691 the sense that it doesn't set up members of IT from the display
4692 spec. */
4693 static int
4694 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4695 Lisp_Object overlay, struct text_pos *position,
4696 ptrdiff_t bufpos, bool frame_window_p)
4698 int replacing = 0;
4700 if (CONSP (spec)
4701 /* Simple specifications. */
4702 && !EQ (XCAR (spec), Qimage)
4703 #ifdef HAVE_XWIDGETS
4704 && !EQ (XCAR (spec), Qxwidget)
4705 #endif
4706 && !EQ (XCAR (spec), Qspace)
4707 && !EQ (XCAR (spec), Qwhen)
4708 && !EQ (XCAR (spec), Qslice)
4709 && !EQ (XCAR (spec), Qspace_width)
4710 && !EQ (XCAR (spec), Qheight)
4711 && !EQ (XCAR (spec), Qraise)
4712 /* Marginal area specifications. */
4713 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4714 && !EQ (XCAR (spec), Qleft_fringe)
4715 && !EQ (XCAR (spec), Qright_fringe)
4716 && !NILP (XCAR (spec)))
4718 for (; CONSP (spec); spec = XCDR (spec))
4720 int rv = handle_single_display_spec (it, XCAR (spec), object,
4721 overlay, position, bufpos,
4722 replacing, frame_window_p);
4723 if (rv != 0)
4725 replacing = rv;
4726 /* If some text in a string is replaced, `position' no
4727 longer points to the position of `object'. */
4728 if (!it || STRINGP (object))
4729 break;
4733 else if (VECTORP (spec))
4735 ptrdiff_t i;
4736 for (i = 0; i < ASIZE (spec); ++i)
4738 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4739 overlay, position, bufpos,
4740 replacing, frame_window_p);
4741 if (rv != 0)
4743 replacing = rv;
4744 /* If some text in a string is replaced, `position' no
4745 longer points to the position of `object'. */
4746 if (!it || STRINGP (object))
4747 break;
4751 else
4752 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4753 bufpos, 0, frame_window_p);
4754 return replacing;
4757 /* Value is the position of the end of the `display' property starting
4758 at START_POS in OBJECT. */
4760 static struct text_pos
4761 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4763 Lisp_Object end;
4764 struct text_pos end_pos;
4766 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4767 Qdisplay, object, Qnil);
4768 CHARPOS (end_pos) = XFASTINT (end);
4769 if (STRINGP (object))
4770 compute_string_pos (&end_pos, start_pos, it->string);
4771 else
4772 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4774 return end_pos;
4778 /* Set up IT from a single `display' property specification SPEC. OBJECT
4779 is the object in which the `display' property was found. *POSITION
4780 is the position in OBJECT at which the `display' property was found.
4781 BUFPOS is the buffer position of OBJECT (different from POSITION if
4782 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4783 previously saw a display specification which already replaced text
4784 display with something else, for example an image; we ignore such
4785 properties after the first one has been processed.
4787 OVERLAY is the overlay this `display' property came from,
4788 or nil if it was a text property.
4790 If SPEC is a `space' or `image' specification, and in some other
4791 cases too, set *POSITION to the position where the `display'
4792 property ends.
4794 If IT is NULL, only examine the property specification in SPEC, but
4795 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4796 is intended to be displayed in a window on a GUI frame.
4798 Value is non-zero if something was found which replaces the display
4799 of buffer or string text. */
4801 static int
4802 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4803 Lisp_Object overlay, struct text_pos *position,
4804 ptrdiff_t bufpos, int display_replaced,
4805 bool frame_window_p)
4807 Lisp_Object form;
4808 Lisp_Object location, value;
4809 struct text_pos start_pos = *position;
4811 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4812 If the result is non-nil, use VALUE instead of SPEC. */
4813 form = Qt;
4814 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4816 spec = XCDR (spec);
4817 if (!CONSP (spec))
4818 return 0;
4819 form = XCAR (spec);
4820 spec = XCDR (spec);
4823 if (!NILP (form) && !EQ (form, Qt))
4825 ptrdiff_t count = SPECPDL_INDEX ();
4827 /* Bind `object' to the object having the `display' property, a
4828 buffer or string. Bind `position' to the position in the
4829 object where the property was found, and `buffer-position'
4830 to the current position in the buffer. */
4832 if (NILP (object))
4833 XSETBUFFER (object, current_buffer);
4834 specbind (Qobject, object);
4835 specbind (Qposition, make_number (CHARPOS (*position)));
4836 specbind (Qbuffer_position, make_number (bufpos));
4837 form = safe_eval (form);
4838 unbind_to (count, Qnil);
4841 if (NILP (form))
4842 return 0;
4844 /* Handle `(height HEIGHT)' specifications. */
4845 if (CONSP (spec)
4846 && EQ (XCAR (spec), Qheight)
4847 && CONSP (XCDR (spec)))
4849 if (it)
4851 if (!FRAME_WINDOW_P (it->f))
4852 return 0;
4854 it->font_height = XCAR (XCDR (spec));
4855 if (!NILP (it->font_height))
4857 int new_height = -1;
4859 if (CONSP (it->font_height)
4860 && (EQ (XCAR (it->font_height), Qplus)
4861 || EQ (XCAR (it->font_height), Qminus))
4862 && CONSP (XCDR (it->font_height))
4863 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4865 /* `(+ N)' or `(- N)' where N is an integer. */
4866 int steps = XINT (XCAR (XCDR (it->font_height)));
4867 if (EQ (XCAR (it->font_height), Qplus))
4868 steps = - steps;
4869 it->face_id = smaller_face (it->f, it->face_id, steps);
4871 else if (FUNCTIONP (it->font_height))
4873 /* Call function with current height as argument.
4874 Value is the new height. */
4875 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4876 Lisp_Object height;
4877 height = safe_call1 (it->font_height,
4878 face->lface[LFACE_HEIGHT_INDEX]);
4879 if (NUMBERP (height))
4880 new_height = XFLOATINT (height);
4882 else if (NUMBERP (it->font_height))
4884 /* Value is a multiple of the canonical char height. */
4885 struct face *f;
4887 f = FACE_FROM_ID (it->f,
4888 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4889 new_height = (XFLOATINT (it->font_height)
4890 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4892 else
4894 /* Evaluate IT->font_height with `height' bound to the
4895 current specified height to get the new height. */
4896 ptrdiff_t count = SPECPDL_INDEX ();
4897 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4899 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4900 value = safe_eval (it->font_height);
4901 unbind_to (count, Qnil);
4903 if (NUMBERP (value))
4904 new_height = XFLOATINT (value);
4907 if (new_height > 0)
4908 it->face_id = face_with_height (it->f, it->face_id, new_height);
4912 return 0;
4915 /* Handle `(space-width WIDTH)'. */
4916 if (CONSP (spec)
4917 && EQ (XCAR (spec), Qspace_width)
4918 && CONSP (XCDR (spec)))
4920 if (it)
4922 if (!FRAME_WINDOW_P (it->f))
4923 return 0;
4925 value = XCAR (XCDR (spec));
4926 if (NUMBERP (value) && XFLOATINT (value) > 0)
4927 it->space_width = value;
4930 return 0;
4933 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4934 if (CONSP (spec)
4935 && EQ (XCAR (spec), Qslice))
4937 Lisp_Object tem;
4939 if (it)
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4944 if (tem = XCDR (spec), CONSP (tem))
4946 it->slice.x = XCAR (tem);
4947 if (tem = XCDR (tem), CONSP (tem))
4949 it->slice.y = XCAR (tem);
4950 if (tem = XCDR (tem), CONSP (tem))
4952 it->slice.width = XCAR (tem);
4953 if (tem = XCDR (tem), CONSP (tem))
4954 it->slice.height = XCAR (tem);
4960 return 0;
4963 /* Handle `(raise FACTOR)'. */
4964 if (CONSP (spec)
4965 && EQ (XCAR (spec), Qraise)
4966 && CONSP (XCDR (spec)))
4968 if (it)
4970 if (!FRAME_WINDOW_P (it->f))
4971 return 0;
4973 #ifdef HAVE_WINDOW_SYSTEM
4974 value = XCAR (XCDR (spec));
4975 if (NUMBERP (value))
4977 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4978 it->voffset = - (XFLOATINT (value)
4979 * (normal_char_height (face->font, -1)));
4981 #endif /* HAVE_WINDOW_SYSTEM */
4984 return 0;
4987 /* Don't handle the other kinds of display specifications
4988 inside a string that we got from a `display' property. */
4989 if (it && it->string_from_display_prop_p)
4990 return 0;
4992 /* Characters having this form of property are not displayed, so
4993 we have to find the end of the property. */
4994 if (it)
4996 start_pos = *position;
4997 *position = display_prop_end (it, object, start_pos);
4998 /* If the display property comes from an overlay, don't consider
4999 any potential stop_charpos values before the end of that
5000 overlay. Since display_prop_end will happily find another
5001 'display' property coming from some other overlay or text
5002 property on buffer positions before this overlay's end, we
5003 need to ignore them, or else we risk displaying this
5004 overlay's display string/image twice. */
5005 if (!NILP (overlay))
5007 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5009 if (ovendpos > CHARPOS (*position))
5010 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5013 value = Qnil;
5015 /* Stop the scan at that end position--we assume that all
5016 text properties change there. */
5017 if (it)
5018 it->stop_charpos = position->charpos;
5020 /* Handle `(left-fringe BITMAP [FACE])'
5021 and `(right-fringe BITMAP [FACE])'. */
5022 if (CONSP (spec)
5023 && (EQ (XCAR (spec), Qleft_fringe)
5024 || EQ (XCAR (spec), Qright_fringe))
5025 && CONSP (XCDR (spec)))
5027 if (it)
5029 if (!FRAME_WINDOW_P (it->f))
5030 /* If we return here, POSITION has been advanced
5031 across the text with this property. */
5033 /* Synchronize the bidi iterator with POSITION. This is
5034 needed because we are not going to push the iterator
5035 on behalf of this display property, so there will be
5036 no pop_it call to do this synchronization for us. */
5037 if (it->bidi_p)
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5043 return 1;
5046 else if (!frame_window_p)
5047 return 1;
5049 #ifdef HAVE_WINDOW_SYSTEM
5050 value = XCAR (XCDR (spec));
5051 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5052 if (! fringe_bitmap)
5053 /* If we return here, POSITION has been advanced
5054 across the text with this property. */
5056 if (it && it->bidi_p)
5058 it->position = *position;
5059 iterate_out_of_display_property (it);
5060 *position = it->position;
5062 return 1;
5065 if (it)
5067 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5069 if (CONSP (XCDR (XCDR (spec))))
5071 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5072 int face_id2 = lookup_derived_face (it->f, face_name,
5073 FRINGE_FACE_ID, false);
5074 if (face_id2 >= 0)
5075 face_id = face_id2;
5078 /* Save current settings of IT so that we can restore them
5079 when we are finished with the glyph property value. */
5080 push_it (it, position);
5082 it->area = TEXT_AREA;
5083 it->what = IT_IMAGE;
5084 it->image_id = -1; /* no image */
5085 it->position = start_pos;
5086 it->object = NILP (object) ? it->w->contents : object;
5087 it->method = GET_FROM_IMAGE;
5088 it->from_overlay = Qnil;
5089 it->face_id = face_id;
5090 it->from_disp_prop_p = true;
5092 /* Say that we haven't consumed the characters with
5093 `display' property yet. The call to pop_it in
5094 set_iterator_to_next will clean this up. */
5095 *position = start_pos;
5097 if (EQ (XCAR (spec), Qleft_fringe))
5099 it->left_user_fringe_bitmap = fringe_bitmap;
5100 it->left_user_fringe_face_id = face_id;
5102 else
5104 it->right_user_fringe_bitmap = fringe_bitmap;
5105 it->right_user_fringe_face_id = face_id;
5108 #endif /* HAVE_WINDOW_SYSTEM */
5109 return 1;
5112 /* Prepare to handle `((margin left-margin) ...)',
5113 `((margin right-margin) ...)' and `((margin nil) ...)'
5114 prefixes for display specifications. */
5115 location = Qunbound;
5116 if (CONSP (spec) && CONSP (XCAR (spec)))
5118 Lisp_Object tem;
5120 value = XCDR (spec);
5121 if (CONSP (value))
5122 value = XCAR (value);
5124 tem = XCAR (spec);
5125 if (EQ (XCAR (tem), Qmargin)
5126 && (tem = XCDR (tem),
5127 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5128 (NILP (tem)
5129 || EQ (tem, Qleft_margin)
5130 || EQ (tem, Qright_margin))))
5131 location = tem;
5134 if (EQ (location, Qunbound))
5136 location = Qnil;
5137 value = spec;
5140 /* After this point, VALUE is the property after any
5141 margin prefix has been stripped. It must be a string,
5142 an image specification, or `(space ...)'.
5144 LOCATION specifies where to display: `left-margin',
5145 `right-margin' or nil. */
5147 bool valid_p = (STRINGP (value)
5148 #ifdef HAVE_WINDOW_SYSTEM
5149 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5150 && valid_image_p (value))
5151 #endif /* not HAVE_WINDOW_SYSTEM */
5152 || (CONSP (value) && EQ (XCAR (value), Qspace))
5153 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5154 && valid_xwidget_spec_p (value)));
5156 if (valid_p && display_replaced == 0)
5158 int retval = 1;
5160 if (!it)
5162 /* Callers need to know whether the display spec is any kind
5163 of `(space ...)' spec that is about to affect text-area
5164 display. */
5165 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5166 retval = 2;
5167 return retval;
5170 /* Save current settings of IT so that we can restore them
5171 when we are finished with the glyph property value. */
5172 push_it (it, position);
5173 it->from_overlay = overlay;
5174 it->from_disp_prop_p = true;
5176 if (NILP (location))
5177 it->area = TEXT_AREA;
5178 else if (EQ (location, Qleft_margin))
5179 it->area = LEFT_MARGIN_AREA;
5180 else
5181 it->area = RIGHT_MARGIN_AREA;
5183 if (STRINGP (value))
5185 it->string = value;
5186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5187 it->current.overlay_string_index = -1;
5188 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5189 it->end_charpos = it->string_nchars = SCHARS (it->string);
5190 it->method = GET_FROM_STRING;
5191 it->stop_charpos = 0;
5192 it->prev_stop = 0;
5193 it->base_level_stop = 0;
5194 it->string_from_display_prop_p = true;
5195 /* Say that we haven't consumed the characters with
5196 `display' property yet. The call to pop_it in
5197 set_iterator_to_next will clean this up. */
5198 if (BUFFERP (object))
5199 *position = start_pos;
5201 /* Force paragraph direction to be that of the parent
5202 object. If the parent object's paragraph direction is
5203 not yet determined, default to L2R. */
5204 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5205 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5206 else
5207 it->paragraph_embedding = L2R;
5209 /* Set up the bidi iterator for this display string. */
5210 if (it->bidi_p)
5212 it->bidi_it.string.lstring = it->string;
5213 it->bidi_it.string.s = NULL;
5214 it->bidi_it.string.schars = it->end_charpos;
5215 it->bidi_it.string.bufpos = bufpos;
5216 it->bidi_it.string.from_disp_str = true;
5217 it->bidi_it.string.unibyte = !it->multibyte_p;
5218 it->bidi_it.w = it->w;
5219 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5222 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5224 it->method = GET_FROM_STRETCH;
5225 it->object = value;
5226 *position = it->position = start_pos;
5227 retval = 1 + (it->area == TEXT_AREA);
5229 else if (valid_xwidget_spec_p (value))
5231 it->what = IT_XWIDGET;
5232 it->method = GET_FROM_XWIDGET;
5233 it->position = start_pos;
5234 it->object = NILP (object) ? it->w->contents : object;
5235 *position = start_pos;
5236 it->xwidget = lookup_xwidget (value);
5238 #ifdef HAVE_WINDOW_SYSTEM
5239 else
5241 it->what = IT_IMAGE;
5242 it->image_id = lookup_image (it->f, value);
5243 it->position = start_pos;
5244 it->object = NILP (object) ? it->w->contents : object;
5245 it->method = GET_FROM_IMAGE;
5247 /* Say that we haven't consumed the characters with
5248 `display' property yet. The call to pop_it in
5249 set_iterator_to_next will clean this up. */
5250 *position = start_pos;
5252 #endif /* HAVE_WINDOW_SYSTEM */
5254 return retval;
5257 /* Invalid property or property not supported. Restore
5258 POSITION to what it was before. */
5259 *position = start_pos;
5260 return 0;
5263 /* Check if PROP is a display property value whose text should be
5264 treated as intangible. OVERLAY is the overlay from which PROP
5265 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5266 specify the buffer position covered by PROP. */
5268 bool
5269 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5270 ptrdiff_t charpos, ptrdiff_t bytepos)
5272 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5273 struct text_pos position;
5275 SET_TEXT_POS (position, charpos, bytepos);
5276 return (handle_display_spec (NULL, prop, Qnil, overlay,
5277 &position, charpos, frame_window_p)
5278 != 0);
5282 /* Return true if PROP is a display sub-property value containing STRING.
5284 Implementation note: this and the following function are really
5285 special cases of handle_display_spec and
5286 handle_single_display_spec, and should ideally use the same code.
5287 Until they do, these two pairs must be consistent and must be
5288 modified in sync. */
5290 static bool
5291 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5293 if (EQ (string, prop))
5294 return true;
5296 /* Skip over `when FORM'. */
5297 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5299 prop = XCDR (prop);
5300 if (!CONSP (prop))
5301 return false;
5302 /* Actually, the condition following `when' should be eval'ed,
5303 like handle_single_display_spec does, and we should return
5304 false if it evaluates to nil. However, this function is
5305 called only when the buffer was already displayed and some
5306 glyph in the glyph matrix was found to come from a display
5307 string. Therefore, the condition was already evaluated, and
5308 the result was non-nil, otherwise the display string wouldn't
5309 have been displayed and we would have never been called for
5310 this property. Thus, we can skip the evaluation and assume
5311 its result is non-nil. */
5312 prop = XCDR (prop);
5315 if (CONSP (prop))
5316 /* Skip over `margin LOCATION'. */
5317 if (EQ (XCAR (prop), Qmargin))
5319 prop = XCDR (prop);
5320 if (!CONSP (prop))
5321 return false;
5323 prop = XCDR (prop);
5324 if (!CONSP (prop))
5325 return false;
5328 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5332 /* Return true if STRING appears in the `display' property PROP. */
5334 static bool
5335 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5337 if (CONSP (prop)
5338 && !EQ (XCAR (prop), Qwhen)
5339 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5341 /* A list of sub-properties. */
5342 while (CONSP (prop))
5344 if (single_display_spec_string_p (XCAR (prop), string))
5345 return true;
5346 prop = XCDR (prop);
5349 else if (VECTORP (prop))
5351 /* A vector of sub-properties. */
5352 ptrdiff_t i;
5353 for (i = 0; i < ASIZE (prop); ++i)
5354 if (single_display_spec_string_p (AREF (prop, i), string))
5355 return true;
5357 else
5358 return single_display_spec_string_p (prop, string);
5360 return false;
5363 /* Look for STRING in overlays and text properties in the current
5364 buffer, between character positions FROM and TO (excluding TO).
5365 BACK_P means look back (in this case, TO is supposed to be
5366 less than FROM).
5367 Value is the first character position where STRING was found, or
5368 zero if it wasn't found before hitting TO.
5370 This function may only use code that doesn't eval because it is
5371 called asynchronously from note_mouse_highlight. */
5373 static ptrdiff_t
5374 string_buffer_position_lim (Lisp_Object string,
5375 ptrdiff_t from, ptrdiff_t to, bool back_p)
5377 Lisp_Object limit, prop, pos;
5378 bool found = false;
5380 pos = make_number (max (from, BEGV));
5382 if (!back_p) /* looking forward */
5384 limit = make_number (min (to, ZV));
5385 while (!found && !EQ (pos, limit))
5387 prop = Fget_char_property (pos, Qdisplay, Qnil);
5388 if (!NILP (prop) && display_prop_string_p (prop, string))
5389 found = true;
5390 else
5391 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5392 limit);
5395 else /* looking back */
5397 limit = make_number (max (to, BEGV));
5398 while (!found && !EQ (pos, limit))
5400 prop = Fget_char_property (pos, Qdisplay, Qnil);
5401 if (!NILP (prop) && display_prop_string_p (prop, string))
5402 found = true;
5403 else
5404 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5405 limit);
5409 return found ? XINT (pos) : 0;
5412 /* Determine which buffer position in current buffer STRING comes from.
5413 AROUND_CHARPOS is an approximate position where it could come from.
5414 Value is the buffer position or 0 if it couldn't be determined.
5416 This function is necessary because we don't record buffer positions
5417 in glyphs generated from strings (to keep struct glyph small).
5418 This function may only use code that doesn't eval because it is
5419 called asynchronously from note_mouse_highlight. */
5421 static ptrdiff_t
5422 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5424 const int MAX_DISTANCE = 1000;
5425 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5426 around_charpos + MAX_DISTANCE,
5427 false);
5429 if (!found)
5430 found = string_buffer_position_lim (string, around_charpos,
5431 around_charpos - MAX_DISTANCE, true);
5432 return found;
5437 /***********************************************************************
5438 `composition' property
5439 ***********************************************************************/
5441 /* Set up iterator IT from `composition' property at its current
5442 position. Called from handle_stop. */
5444 static enum prop_handled
5445 handle_composition_prop (struct it *it)
5447 Lisp_Object prop, string;
5448 ptrdiff_t pos, pos_byte, start, end;
5450 if (STRINGP (it->string))
5452 unsigned char *s;
5454 pos = IT_STRING_CHARPOS (*it);
5455 pos_byte = IT_STRING_BYTEPOS (*it);
5456 string = it->string;
5457 s = SDATA (string) + pos_byte;
5458 it->c = STRING_CHAR (s);
5460 else
5462 pos = IT_CHARPOS (*it);
5463 pos_byte = IT_BYTEPOS (*it);
5464 string = Qnil;
5465 it->c = FETCH_CHAR (pos_byte);
5468 /* If there's a valid composition and point is not inside of the
5469 composition (in the case that the composition is from the current
5470 buffer), draw a glyph composed from the composition components. */
5471 if (find_composition (pos, -1, &start, &end, &prop, string)
5472 && composition_valid_p (start, end, prop)
5473 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5475 if (start < pos)
5476 /* As we can't handle this situation (perhaps font-lock added
5477 a new composition), we just return here hoping that next
5478 redisplay will detect this composition much earlier. */
5479 return HANDLED_NORMALLY;
5480 if (start != pos)
5482 if (STRINGP (it->string))
5483 pos_byte = string_char_to_byte (it->string, start);
5484 else
5485 pos_byte = CHAR_TO_BYTE (start);
5487 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5488 prop, string);
5490 if (it->cmp_it.id >= 0)
5492 it->cmp_it.ch = -1;
5493 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5494 it->cmp_it.nglyphs = -1;
5498 return HANDLED_NORMALLY;
5503 /***********************************************************************
5504 Overlay strings
5505 ***********************************************************************/
5507 /* The following structure is used to record overlay strings for
5508 later sorting in load_overlay_strings. */
5510 struct overlay_entry
5512 Lisp_Object overlay;
5513 Lisp_Object string;
5514 EMACS_INT priority;
5515 bool after_string_p;
5519 /* Set up iterator IT from overlay strings at its current position.
5520 Called from handle_stop. */
5522 static enum prop_handled
5523 handle_overlay_change (struct it *it)
5525 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5526 return HANDLED_RECOMPUTE_PROPS;
5527 else
5528 return HANDLED_NORMALLY;
5532 /* Set up the next overlay string for delivery by IT, if there is an
5533 overlay string to deliver. Called by set_iterator_to_next when the
5534 end of the current overlay string is reached. If there are more
5535 overlay strings to display, IT->string and
5536 IT->current.overlay_string_index are set appropriately here.
5537 Otherwise IT->string is set to nil. */
5539 static void
5540 next_overlay_string (struct it *it)
5542 ++it->current.overlay_string_index;
5543 if (it->current.overlay_string_index == it->n_overlay_strings)
5545 /* No more overlay strings. Restore IT's settings to what
5546 they were before overlay strings were processed, and
5547 continue to deliver from current_buffer. */
5549 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5550 pop_it (it);
5551 eassert (it->sp > 0
5552 || (NILP (it->string)
5553 && it->method == GET_FROM_BUFFER
5554 && it->stop_charpos >= BEGV
5555 && it->stop_charpos <= it->end_charpos));
5556 it->current.overlay_string_index = -1;
5557 it->n_overlay_strings = 0;
5558 /* If there's an empty display string on the stack, pop the
5559 stack, to resync the bidi iterator with IT's position. Such
5560 empty strings are pushed onto the stack in
5561 get_overlay_strings_1. */
5562 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5563 pop_it (it);
5565 /* Since we've exhausted overlay strings at this buffer
5566 position, set the flag to ignore overlays until we move to
5567 another position. The flag is reset in
5568 next_element_from_buffer. */
5569 it->ignore_overlay_strings_at_pos_p = true;
5571 /* If we're at the end of the buffer, record that we have
5572 processed the overlay strings there already, so that
5573 next_element_from_buffer doesn't try it again. */
5574 if (NILP (it->string)
5575 && IT_CHARPOS (*it) >= it->end_charpos
5576 && it->overlay_strings_charpos >= it->end_charpos)
5577 it->overlay_strings_at_end_processed_p = true;
5578 /* Note: we reset overlay_strings_charpos only here, to make
5579 sure the just-processed overlays were indeed at EOB.
5580 Otherwise, overlays on text with invisible text property,
5581 which are processed with IT's position past the invisible
5582 text, might fool us into thinking the overlays at EOB were
5583 already processed (linum-mode can cause this, for
5584 example). */
5585 it->overlay_strings_charpos = -1;
5587 else
5589 /* There are more overlay strings to process. If
5590 IT->current.overlay_string_index has advanced to a position
5591 where we must load IT->overlay_strings with more strings, do
5592 it. We must load at the IT->overlay_strings_charpos where
5593 IT->n_overlay_strings was originally computed; when invisible
5594 text is present, this might not be IT_CHARPOS (Bug#7016). */
5595 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5597 if (it->current.overlay_string_index && i == 0)
5598 load_overlay_strings (it, it->overlay_strings_charpos);
5600 /* Initialize IT to deliver display elements from the overlay
5601 string. */
5602 it->string = it->overlay_strings[i];
5603 it->multibyte_p = STRING_MULTIBYTE (it->string);
5604 SET_TEXT_POS (it->current.string_pos, 0, 0);
5605 it->method = GET_FROM_STRING;
5606 it->stop_charpos = 0;
5607 it->end_charpos = SCHARS (it->string);
5608 if (it->cmp_it.stop_pos >= 0)
5609 it->cmp_it.stop_pos = 0;
5610 it->prev_stop = 0;
5611 it->base_level_stop = 0;
5613 /* Set up the bidi iterator for this overlay string. */
5614 if (it->bidi_p)
5616 it->bidi_it.string.lstring = it->string;
5617 it->bidi_it.string.s = NULL;
5618 it->bidi_it.string.schars = SCHARS (it->string);
5619 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5620 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5621 it->bidi_it.string.unibyte = !it->multibyte_p;
5622 it->bidi_it.w = it->w;
5623 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5627 CHECK_IT (it);
5631 /* Compare two overlay_entry structures E1 and E2. Used as a
5632 comparison function for qsort in load_overlay_strings. Overlay
5633 strings for the same position are sorted so that
5635 1. All after-strings come in front of before-strings, except
5636 when they come from the same overlay.
5638 2. Within after-strings, strings are sorted so that overlay strings
5639 from overlays with higher priorities come first.
5641 2. Within before-strings, strings are sorted so that overlay
5642 strings from overlays with higher priorities come last.
5644 Value is analogous to strcmp. */
5647 static int
5648 compare_overlay_entries (const void *e1, const void *e2)
5650 struct overlay_entry const *entry1 = e1;
5651 struct overlay_entry const *entry2 = e2;
5652 int result;
5654 if (entry1->after_string_p != entry2->after_string_p)
5656 /* Let after-strings appear in front of before-strings if
5657 they come from different overlays. */
5658 if (EQ (entry1->overlay, entry2->overlay))
5659 result = entry1->after_string_p ? 1 : -1;
5660 else
5661 result = entry1->after_string_p ? -1 : 1;
5663 else if (entry1->priority != entry2->priority)
5665 if (entry1->after_string_p)
5666 /* After-strings sorted in order of decreasing priority. */
5667 result = entry2->priority < entry1->priority ? -1 : 1;
5668 else
5669 /* Before-strings sorted in order of increasing priority. */
5670 result = entry1->priority < entry2->priority ? -1 : 1;
5672 else
5673 result = 0;
5675 return result;
5679 /* Load the vector IT->overlay_strings with overlay strings from IT's
5680 current buffer position, or from CHARPOS if that is > 0. Set
5681 IT->n_overlays to the total number of overlay strings found.
5683 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5684 a time. On entry into load_overlay_strings,
5685 IT->current.overlay_string_index gives the number of overlay
5686 strings that have already been loaded by previous calls to this
5687 function.
5689 IT->add_overlay_start contains an additional overlay start
5690 position to consider for taking overlay strings from, if non-zero.
5691 This position comes into play when the overlay has an `invisible'
5692 property, and both before and after-strings. When we've skipped to
5693 the end of the overlay, because of its `invisible' property, we
5694 nevertheless want its before-string to appear.
5695 IT->add_overlay_start will contain the overlay start position
5696 in this case.
5698 Overlay strings are sorted so that after-string strings come in
5699 front of before-string strings. Within before and after-strings,
5700 strings are sorted by overlay priority. See also function
5701 compare_overlay_entries. */
5703 static void
5704 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5706 Lisp_Object overlay, window, str, invisible;
5707 struct Lisp_Overlay *ov;
5708 ptrdiff_t start, end;
5709 ptrdiff_t n = 0, i, j;
5710 int invis;
5711 struct overlay_entry entriesbuf[20];
5712 ptrdiff_t size = ARRAYELTS (entriesbuf);
5713 struct overlay_entry *entries = entriesbuf;
5714 USE_SAFE_ALLOCA;
5716 if (charpos <= 0)
5717 charpos = IT_CHARPOS (*it);
5719 /* Append the overlay string STRING of overlay OVERLAY to vector
5720 `entries' which has size `size' and currently contains `n'
5721 elements. AFTER_P means STRING is an after-string of
5722 OVERLAY. */
5723 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5724 do \
5726 Lisp_Object priority; \
5728 if (n == size) \
5730 struct overlay_entry *old = entries; \
5731 SAFE_NALLOCA (entries, 2, size); \
5732 memcpy (entries, old, size * sizeof *entries); \
5733 size *= 2; \
5736 entries[n].string = (STRING); \
5737 entries[n].overlay = (OVERLAY); \
5738 priority = Foverlay_get ((OVERLAY), Qpriority); \
5739 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5740 entries[n].after_string_p = (AFTER_P); \
5741 ++n; \
5743 while (false)
5745 /* Process overlay before the overlay center. */
5746 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5748 XSETMISC (overlay, ov);
5749 eassert (OVERLAYP (overlay));
5750 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5751 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5753 if (end < charpos)
5754 break;
5756 /* Skip this overlay if it doesn't start or end at IT's current
5757 position. */
5758 if (end != charpos && start != charpos)
5759 continue;
5761 /* Skip this overlay if it doesn't apply to IT->w. */
5762 window = Foverlay_get (overlay, Qwindow);
5763 if (WINDOWP (window) && XWINDOW (window) != it->w)
5764 continue;
5766 /* If the text ``under'' the overlay is invisible, both before-
5767 and after-strings from this overlay are visible; start and
5768 end position are indistinguishable. */
5769 invisible = Foverlay_get (overlay, Qinvisible);
5770 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772 /* If overlay has a non-empty before-string, record it. */
5773 if ((start == charpos || (end == charpos && invis != 0))
5774 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5775 && SCHARS (str))
5776 RECORD_OVERLAY_STRING (overlay, str, false);
5778 /* If overlay has a non-empty after-string, record it. */
5779 if ((end == charpos || (start == charpos && invis != 0))
5780 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5781 && SCHARS (str))
5782 RECORD_OVERLAY_STRING (overlay, str, true);
5785 /* Process overlays after the overlay center. */
5786 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5788 XSETMISC (overlay, ov);
5789 eassert (OVERLAYP (overlay));
5790 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5791 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5793 if (start > charpos)
5794 break;
5796 /* Skip this overlay if it doesn't start or end at IT's current
5797 position. */
5798 if (end != charpos && start != charpos)
5799 continue;
5801 /* Skip this overlay if it doesn't apply to IT->w. */
5802 window = Foverlay_get (overlay, Qwindow);
5803 if (WINDOWP (window) && XWINDOW (window) != it->w)
5804 continue;
5806 /* If the text ``under'' the overlay is invisible, it has a zero
5807 dimension, and both before- and after-strings apply. */
5808 invisible = Foverlay_get (overlay, Qinvisible);
5809 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5811 /* If overlay has a non-empty before-string, record it. */
5812 if ((start == charpos || (end == charpos && invis != 0))
5813 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5814 && SCHARS (str))
5815 RECORD_OVERLAY_STRING (overlay, str, false);
5817 /* If overlay has a non-empty after-string, record it. */
5818 if ((end == charpos || (start == charpos && invis != 0))
5819 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5820 && SCHARS (str))
5821 RECORD_OVERLAY_STRING (overlay, str, true);
5824 #undef RECORD_OVERLAY_STRING
5826 /* Sort entries. */
5827 if (n > 1)
5828 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5830 /* Record number of overlay strings, and where we computed it. */
5831 it->n_overlay_strings = n;
5832 it->overlay_strings_charpos = charpos;
5834 /* IT->current.overlay_string_index is the number of overlay strings
5835 that have already been consumed by IT. Copy some of the
5836 remaining overlay strings to IT->overlay_strings. */
5837 i = 0;
5838 j = it->current.overlay_string_index;
5839 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5841 it->overlay_strings[i] = entries[j].string;
5842 it->string_overlays[i++] = entries[j++].overlay;
5845 CHECK_IT (it);
5846 SAFE_FREE ();
5850 /* Get the first chunk of overlay strings at IT's current buffer
5851 position, or at CHARPOS if that is > 0. Value is true if at
5852 least one overlay string was found. */
5854 static bool
5855 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5857 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5858 process. This fills IT->overlay_strings with strings, and sets
5859 IT->n_overlay_strings to the total number of strings to process.
5860 IT->pos.overlay_string_index has to be set temporarily to zero
5861 because load_overlay_strings needs this; it must be set to -1
5862 when no overlay strings are found because a zero value would
5863 indicate a position in the first overlay string. */
5864 it->current.overlay_string_index = 0;
5865 load_overlay_strings (it, charpos);
5867 /* If we found overlay strings, set up IT to deliver display
5868 elements from the first one. Otherwise set up IT to deliver
5869 from current_buffer. */
5870 if (it->n_overlay_strings)
5872 /* Make sure we know settings in current_buffer, so that we can
5873 restore meaningful values when we're done with the overlay
5874 strings. */
5875 if (compute_stop_p)
5876 compute_stop_pos (it);
5877 eassert (it->face_id >= 0);
5879 /* Save IT's settings. They are restored after all overlay
5880 strings have been processed. */
5881 eassert (!compute_stop_p || it->sp == 0);
5883 /* When called from handle_stop, there might be an empty display
5884 string loaded. In that case, don't bother saving it. But
5885 don't use this optimization with the bidi iterator, since we
5886 need the corresponding pop_it call to resync the bidi
5887 iterator's position with IT's position, after we are done
5888 with the overlay strings. (The corresponding call to pop_it
5889 in case of an empty display string is in
5890 next_overlay_string.) */
5891 if (!(!it->bidi_p
5892 && STRINGP (it->string) && !SCHARS (it->string)))
5893 push_it (it, NULL);
5895 /* Set up IT to deliver display elements from the first overlay
5896 string. */
5897 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5898 it->string = it->overlay_strings[0];
5899 it->from_overlay = Qnil;
5900 it->stop_charpos = 0;
5901 eassert (STRINGP (it->string));
5902 it->end_charpos = SCHARS (it->string);
5903 it->prev_stop = 0;
5904 it->base_level_stop = 0;
5905 it->multibyte_p = STRING_MULTIBYTE (it->string);
5906 it->method = GET_FROM_STRING;
5907 it->from_disp_prop_p = 0;
5909 /* Force paragraph direction to be that of the parent
5910 buffer. */
5911 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5912 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5913 else
5914 it->paragraph_embedding = L2R;
5916 /* Set up the bidi iterator for this overlay string. */
5917 if (it->bidi_p)
5919 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5921 it->bidi_it.string.lstring = it->string;
5922 it->bidi_it.string.s = NULL;
5923 it->bidi_it.string.schars = SCHARS (it->string);
5924 it->bidi_it.string.bufpos = pos;
5925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5926 it->bidi_it.string.unibyte = !it->multibyte_p;
5927 it->bidi_it.w = it->w;
5928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5930 return true;
5933 it->current.overlay_string_index = -1;
5934 return false;
5937 static bool
5938 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5940 it->string = Qnil;
5941 it->method = GET_FROM_BUFFER;
5943 get_overlay_strings_1 (it, charpos, true);
5945 CHECK_IT (it);
5947 /* Value is true if we found at least one overlay string. */
5948 return STRINGP (it->string);
5953 /***********************************************************************
5954 Saving and restoring state
5955 ***********************************************************************/
5957 /* Save current settings of IT on IT->stack. Called, for example,
5958 before setting up IT for an overlay string, to be able to restore
5959 IT's settings to what they were after the overlay string has been
5960 processed. If POSITION is non-NULL, it is the position to save on
5961 the stack instead of IT->position. */
5963 static void
5964 push_it (struct it *it, struct text_pos *position)
5966 struct iterator_stack_entry *p;
5968 eassert (it->sp < IT_STACK_SIZE);
5969 p = it->stack + it->sp;
5971 p->stop_charpos = it->stop_charpos;
5972 p->prev_stop = it->prev_stop;
5973 p->base_level_stop = it->base_level_stop;
5974 p->cmp_it = it->cmp_it;
5975 eassert (it->face_id >= 0);
5976 p->face_id = it->face_id;
5977 p->string = it->string;
5978 p->method = it->method;
5979 p->from_overlay = it->from_overlay;
5980 switch (p->method)
5982 case GET_FROM_IMAGE:
5983 p->u.image.object = it->object;
5984 p->u.image.image_id = it->image_id;
5985 p->u.image.slice = it->slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 p->u.stretch.object = it->object;
5989 break;
5990 case GET_FROM_XWIDGET:
5991 p->u.xwidget.object = it->object;
5992 break;
5993 case GET_FROM_BUFFER:
5994 case GET_FROM_DISPLAY_VECTOR:
5995 case GET_FROM_STRING:
5996 case GET_FROM_C_STRING:
5997 break;
5998 default:
5999 emacs_abort ();
6001 p->position = position ? *position : it->position;
6002 p->current = it->current;
6003 p->end_charpos = it->end_charpos;
6004 p->string_nchars = it->string_nchars;
6005 p->area = it->area;
6006 p->multibyte_p = it->multibyte_p;
6007 p->avoid_cursor_p = it->avoid_cursor_p;
6008 p->space_width = it->space_width;
6009 p->font_height = it->font_height;
6010 p->voffset = it->voffset;
6011 p->string_from_display_prop_p = it->string_from_display_prop_p;
6012 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6013 p->display_ellipsis_p = false;
6014 p->line_wrap = it->line_wrap;
6015 p->bidi_p = it->bidi_p;
6016 p->paragraph_embedding = it->paragraph_embedding;
6017 p->from_disp_prop_p = it->from_disp_prop_p;
6018 ++it->sp;
6020 /* Save the state of the bidi iterator as well. */
6021 if (it->bidi_p)
6022 bidi_push_it (&it->bidi_it);
6025 static void
6026 iterate_out_of_display_property (struct it *it)
6028 bool buffer_p = !STRINGP (it->string);
6029 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6030 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6032 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6034 /* Maybe initialize paragraph direction. If we are at the beginning
6035 of a new paragraph, next_element_from_buffer may not have a
6036 chance to do that. */
6037 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6038 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6039 /* prev_stop can be zero, so check against BEGV as well. */
6040 while (it->bidi_it.charpos >= bob
6041 && it->prev_stop <= it->bidi_it.charpos
6042 && it->bidi_it.charpos < CHARPOS (it->position)
6043 && it->bidi_it.charpos < eob)
6044 bidi_move_to_visually_next (&it->bidi_it);
6045 /* Record the stop_pos we just crossed, for when we cross it
6046 back, maybe. */
6047 if (it->bidi_it.charpos > CHARPOS (it->position))
6048 it->prev_stop = CHARPOS (it->position);
6049 /* If we ended up not where pop_it put us, resync IT's
6050 positional members with the bidi iterator. */
6051 if (it->bidi_it.charpos != CHARPOS (it->position))
6052 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6053 if (buffer_p)
6054 it->current.pos = it->position;
6055 else
6056 it->current.string_pos = it->position;
6059 /* Restore IT's settings from IT->stack. Called, for example, when no
6060 more overlay strings must be processed, and we return to delivering
6061 display elements from a buffer, or when the end of a string from a
6062 `display' property is reached and we return to delivering display
6063 elements from an overlay string, or from a buffer. */
6065 static void
6066 pop_it (struct it *it)
6068 struct iterator_stack_entry *p;
6069 bool from_display_prop = it->from_disp_prop_p;
6070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6072 eassert (it->sp > 0);
6073 --it->sp;
6074 p = it->stack + it->sp;
6075 it->stop_charpos = p->stop_charpos;
6076 it->prev_stop = p->prev_stop;
6077 it->base_level_stop = p->base_level_stop;
6078 it->cmp_it = p->cmp_it;
6079 it->face_id = p->face_id;
6080 it->current = p->current;
6081 it->position = p->position;
6082 it->string = p->string;
6083 it->from_overlay = p->from_overlay;
6084 if (NILP (it->string))
6085 SET_TEXT_POS (it->current.string_pos, -1, -1);
6086 it->method = p->method;
6087 switch (it->method)
6089 case GET_FROM_IMAGE:
6090 it->image_id = p->u.image.image_id;
6091 it->object = p->u.image.object;
6092 it->slice = p->u.image.slice;
6093 break;
6094 case GET_FROM_XWIDGET:
6095 it->object = p->u.xwidget.object;
6096 break;
6097 case GET_FROM_STRETCH:
6098 it->object = p->u.stretch.object;
6099 break;
6100 case GET_FROM_BUFFER:
6101 it->object = it->w->contents;
6102 break;
6103 case GET_FROM_STRING:
6105 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6107 /* Restore the face_box_p flag, since it could have been
6108 overwritten by the face of the object that we just finished
6109 displaying. */
6110 if (face)
6111 it->face_box_p = face->box != FACE_NO_BOX;
6112 it->object = it->string;
6114 break;
6115 case GET_FROM_DISPLAY_VECTOR:
6116 if (it->s)
6117 it->method = GET_FROM_C_STRING;
6118 else if (STRINGP (it->string))
6119 it->method = GET_FROM_STRING;
6120 else
6122 it->method = GET_FROM_BUFFER;
6123 it->object = it->w->contents;
6125 break;
6126 case GET_FROM_C_STRING:
6127 break;
6128 default:
6129 emacs_abort ();
6131 it->end_charpos = p->end_charpos;
6132 it->string_nchars = p->string_nchars;
6133 it->area = p->area;
6134 it->multibyte_p = p->multibyte_p;
6135 it->avoid_cursor_p = p->avoid_cursor_p;
6136 it->space_width = p->space_width;
6137 it->font_height = p->font_height;
6138 it->voffset = p->voffset;
6139 it->string_from_display_prop_p = p->string_from_display_prop_p;
6140 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6141 it->line_wrap = p->line_wrap;
6142 it->bidi_p = p->bidi_p;
6143 it->paragraph_embedding = p->paragraph_embedding;
6144 it->from_disp_prop_p = p->from_disp_prop_p;
6145 if (it->bidi_p)
6147 bidi_pop_it (&it->bidi_it);
6148 /* Bidi-iterate until we get out of the portion of text, if any,
6149 covered by a `display' text property or by an overlay with
6150 `display' property. (We cannot just jump there, because the
6151 internal coherency of the bidi iterator state can not be
6152 preserved across such jumps.) We also must determine the
6153 paragraph base direction if the overlay we just processed is
6154 at the beginning of a new paragraph. */
6155 if (from_display_prop
6156 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6157 iterate_out_of_display_property (it);
6159 eassert ((BUFFERP (it->object)
6160 && IT_CHARPOS (*it) == it->bidi_it.charpos
6161 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6162 || (STRINGP (it->object)
6163 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6164 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6165 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6167 /* If we move the iterator over text covered by a display property
6168 to a new buffer position, any info about previously seen overlays
6169 is no longer valid. */
6170 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6171 it->ignore_overlay_strings_at_pos_p = false;
6176 /***********************************************************************
6177 Moving over lines
6178 ***********************************************************************/
6180 /* Set IT's current position to the previous line start. */
6182 static void
6183 back_to_previous_line_start (struct it *it)
6185 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6187 DEC_BOTH (cp, bp);
6188 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6192 /* Move IT to the next line start.
6194 Value is true if a newline was found. Set *SKIPPED_P to true if
6195 we skipped over part of the text (as opposed to moving the iterator
6196 continuously over the text). Otherwise, don't change the value
6197 of *SKIPPED_P.
6199 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6200 iterator on the newline, if it was found.
6202 Newlines may come from buffer text, overlay strings, or strings
6203 displayed via the `display' property. That's the reason we can't
6204 simply use find_newline_no_quit.
6206 Note that this function may not skip over invisible text that is so
6207 because of text properties and immediately follows a newline. If
6208 it would, function reseat_at_next_visible_line_start, when called
6209 from set_iterator_to_next, would effectively make invisible
6210 characters following a newline part of the wrong glyph row, which
6211 leads to wrong cursor motion. */
6213 static bool
6214 forward_to_next_line_start (struct it *it, bool *skipped_p,
6215 struct bidi_it *bidi_it_prev)
6217 ptrdiff_t old_selective;
6218 bool newline_found_p = false;
6219 int n;
6220 const int MAX_NEWLINE_DISTANCE = 500;
6222 /* If already on a newline, just consume it to avoid unintended
6223 skipping over invisible text below. */
6224 if (it->what == IT_CHARACTER
6225 && it->c == '\n'
6226 && CHARPOS (it->position) == IT_CHARPOS (*it))
6228 if (it->bidi_p && bidi_it_prev)
6229 *bidi_it_prev = it->bidi_it;
6230 set_iterator_to_next (it, false);
6231 it->c = 0;
6232 return true;
6235 /* Don't handle selective display in the following. It's (a)
6236 unnecessary because it's done by the caller, and (b) leads to an
6237 infinite recursion because next_element_from_ellipsis indirectly
6238 calls this function. */
6239 old_selective = it->selective;
6240 it->selective = 0;
6242 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6243 from buffer text. */
6244 for (n = 0;
6245 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6246 n += !STRINGP (it->string))
6248 if (!get_next_display_element (it))
6249 return false;
6250 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6251 if (newline_found_p && it->bidi_p && bidi_it_prev)
6252 *bidi_it_prev = it->bidi_it;
6253 set_iterator_to_next (it, false);
6256 /* If we didn't find a newline near enough, see if we can use a
6257 short-cut. */
6258 if (!newline_found_p)
6260 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6261 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6262 1, &bytepos);
6263 Lisp_Object pos;
6265 eassert (!STRINGP (it->string));
6267 /* If there isn't any `display' property in sight, and no
6268 overlays, we can just use the position of the newline in
6269 buffer text. */
6270 if (it->stop_charpos >= limit
6271 || ((pos = Fnext_single_property_change (make_number (start),
6272 Qdisplay, Qnil,
6273 make_number (limit)),
6274 NILP (pos))
6275 && next_overlay_change (start) == ZV))
6277 if (!it->bidi_p)
6279 IT_CHARPOS (*it) = limit;
6280 IT_BYTEPOS (*it) = bytepos;
6282 else
6284 struct bidi_it bprev;
6286 /* Help bidi.c avoid expensive searches for display
6287 properties and overlays, by telling it that there are
6288 none up to `limit'. */
6289 if (it->bidi_it.disp_pos < limit)
6291 it->bidi_it.disp_pos = limit;
6292 it->bidi_it.disp_prop = 0;
6294 do {
6295 bprev = it->bidi_it;
6296 bidi_move_to_visually_next (&it->bidi_it);
6297 } while (it->bidi_it.charpos != limit);
6298 IT_CHARPOS (*it) = limit;
6299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 if (bidi_it_prev)
6301 *bidi_it_prev = bprev;
6303 *skipped_p = newline_found_p = true;
6305 else
6307 while (get_next_display_element (it)
6308 && !newline_found_p)
6310 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6311 if (newline_found_p && it->bidi_p && bidi_it_prev)
6312 *bidi_it_prev = it->bidi_it;
6313 set_iterator_to_next (it, false);
6318 it->selective = old_selective;
6319 return newline_found_p;
6323 /* Set IT's current position to the previous visible line start. Skip
6324 invisible text that is so either due to text properties or due to
6325 selective display. Caution: this does not change IT->current_x and
6326 IT->hpos. */
6328 static void
6329 back_to_previous_visible_line_start (struct it *it)
6331 while (IT_CHARPOS (*it) > BEGV)
6333 back_to_previous_line_start (it);
6335 if (IT_CHARPOS (*it) <= BEGV)
6336 break;
6338 /* If selective > 0, then lines indented more than its value are
6339 invisible. */
6340 if (it->selective > 0
6341 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6342 it->selective))
6343 continue;
6345 /* Check the newline before point for invisibility. */
6347 Lisp_Object prop;
6348 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6349 Qinvisible, it->window);
6350 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6351 continue;
6354 if (IT_CHARPOS (*it) <= BEGV)
6355 break;
6358 struct it it2;
6359 void *it2data = NULL;
6360 ptrdiff_t pos;
6361 ptrdiff_t beg, end;
6362 Lisp_Object val, overlay;
6364 SAVE_IT (it2, *it, it2data);
6366 /* If newline is part of a composition, continue from start of composition */
6367 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6368 && beg < IT_CHARPOS (*it))
6369 goto replaced;
6371 /* If newline is replaced by a display property, find start of overlay
6372 or interval and continue search from that point. */
6373 pos = --IT_CHARPOS (it2);
6374 --IT_BYTEPOS (it2);
6375 it2.sp = 0;
6376 bidi_unshelve_cache (NULL, false);
6377 it2.string_from_display_prop_p = false;
6378 it2.from_disp_prop_p = false;
6379 if (handle_display_prop (&it2) == HANDLED_RETURN
6380 && !NILP (val = get_char_property_and_overlay
6381 (make_number (pos), Qdisplay, Qnil, &overlay))
6382 && (OVERLAYP (overlay)
6383 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6384 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6386 RESTORE_IT (it, it, it2data);
6387 goto replaced;
6390 /* Newline is not replaced by anything -- so we are done. */
6391 RESTORE_IT (it, it, it2data);
6392 break;
6394 replaced:
6395 if (beg < BEGV)
6396 beg = BEGV;
6397 IT_CHARPOS (*it) = beg;
6398 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6402 it->continuation_lines_width = 0;
6404 eassert (IT_CHARPOS (*it) >= BEGV);
6405 eassert (IT_CHARPOS (*it) == BEGV
6406 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6407 CHECK_IT (it);
6411 /* Reseat iterator IT at the previous visible line start. Skip
6412 invisible text that is so either due to text properties or due to
6413 selective display. At the end, update IT's overlay information,
6414 face information etc. */
6416 void
6417 reseat_at_previous_visible_line_start (struct it *it)
6419 back_to_previous_visible_line_start (it);
6420 reseat (it, it->current.pos, true);
6421 CHECK_IT (it);
6425 /* Reseat iterator IT on the next visible line start in the current
6426 buffer. ON_NEWLINE_P means position IT on the newline
6427 preceding the line start. Skip over invisible text that is so
6428 because of selective display. Compute faces, overlays etc at the
6429 new position. Note that this function does not skip over text that
6430 is invisible because of text properties. */
6432 static void
6433 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6435 bool skipped_p = false;
6436 struct bidi_it bidi_it_prev;
6437 bool newline_found_p
6438 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6440 /* Skip over lines that are invisible because they are indented
6441 more than the value of IT->selective. */
6442 if (it->selective > 0)
6443 while (IT_CHARPOS (*it) < ZV
6444 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6445 it->selective))
6447 eassert (IT_BYTEPOS (*it) == BEGV
6448 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6449 newline_found_p =
6450 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6453 /* Position on the newline if that's what's requested. */
6454 if (on_newline_p && newline_found_p)
6456 if (STRINGP (it->string))
6458 if (IT_STRING_CHARPOS (*it) > 0)
6460 if (!it->bidi_p)
6462 --IT_STRING_CHARPOS (*it);
6463 --IT_STRING_BYTEPOS (*it);
6465 else
6467 /* We need to restore the bidi iterator to the state
6468 it had on the newline, and resync the IT's
6469 position with that. */
6470 it->bidi_it = bidi_it_prev;
6471 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6472 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6476 else if (IT_CHARPOS (*it) > BEGV)
6478 if (!it->bidi_p)
6480 --IT_CHARPOS (*it);
6481 --IT_BYTEPOS (*it);
6483 else
6485 /* We need to restore the bidi iterator to the state it
6486 had on the newline and resync IT with that. */
6487 it->bidi_it = bidi_it_prev;
6488 IT_CHARPOS (*it) = it->bidi_it.charpos;
6489 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6491 reseat (it, it->current.pos, false);
6494 else if (skipped_p)
6495 reseat (it, it->current.pos, false);
6497 CHECK_IT (it);
6502 /***********************************************************************
6503 Changing an iterator's position
6504 ***********************************************************************/
6506 /* Change IT's current position to POS in current_buffer.
6507 If FORCE_P, always check for text properties at the new position.
6508 Otherwise, text properties are only looked up if POS >=
6509 IT->check_charpos of a property. */
6511 static void
6512 reseat (struct it *it, struct text_pos pos, bool force_p)
6514 ptrdiff_t original_pos = IT_CHARPOS (*it);
6516 reseat_1 (it, pos, false);
6518 /* Determine where to check text properties. Avoid doing it
6519 where possible because text property lookup is very expensive. */
6520 if (force_p
6521 || CHARPOS (pos) > it->stop_charpos
6522 || CHARPOS (pos) < original_pos)
6524 if (it->bidi_p)
6526 /* For bidi iteration, we need to prime prev_stop and
6527 base_level_stop with our best estimations. */
6528 /* Implementation note: Of course, POS is not necessarily a
6529 stop position, so assigning prev_pos to it is a lie; we
6530 should have called compute_stop_backwards. However, if
6531 the current buffer does not include any R2L characters,
6532 that call would be a waste of cycles, because the
6533 iterator will never move back, and thus never cross this
6534 "fake" stop position. So we delay that backward search
6535 until the time we really need it, in next_element_from_buffer. */
6536 if (CHARPOS (pos) != it->prev_stop)
6537 it->prev_stop = CHARPOS (pos);
6538 if (CHARPOS (pos) < it->base_level_stop)
6539 it->base_level_stop = 0; /* meaning it's unknown */
6540 handle_stop (it);
6542 else
6544 handle_stop (it);
6545 it->prev_stop = it->base_level_stop = 0;
6550 CHECK_IT (it);
6554 /* Change IT's buffer position to POS. SET_STOP_P means set
6555 IT->stop_pos to POS, also. */
6557 static void
6558 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6560 /* Don't call this function when scanning a C string. */
6561 eassert (it->s == NULL);
6563 /* POS must be a reasonable value. */
6564 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6566 it->current.pos = it->position = pos;
6567 it->end_charpos = ZV;
6568 it->dpvec = NULL;
6569 it->current.dpvec_index = -1;
6570 it->current.overlay_string_index = -1;
6571 IT_STRING_CHARPOS (*it) = -1;
6572 IT_STRING_BYTEPOS (*it) = -1;
6573 it->string = Qnil;
6574 it->method = GET_FROM_BUFFER;
6575 it->object = it->w->contents;
6576 it->area = TEXT_AREA;
6577 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6578 it->sp = 0;
6579 it->string_from_display_prop_p = false;
6580 it->string_from_prefix_prop_p = false;
6582 it->from_disp_prop_p = false;
6583 it->face_before_selective_p = false;
6584 if (it->bidi_p)
6586 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6587 &it->bidi_it);
6588 bidi_unshelve_cache (NULL, false);
6589 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6590 it->bidi_it.string.s = NULL;
6591 it->bidi_it.string.lstring = Qnil;
6592 it->bidi_it.string.bufpos = 0;
6593 it->bidi_it.string.from_disp_str = false;
6594 it->bidi_it.string.unibyte = false;
6595 it->bidi_it.w = it->w;
6598 if (set_stop_p)
6600 it->stop_charpos = CHARPOS (pos);
6601 it->base_level_stop = CHARPOS (pos);
6603 /* This make the information stored in it->cmp_it invalidate. */
6604 it->cmp_it.id = -1;
6608 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6609 If S is non-null, it is a C string to iterate over. Otherwise,
6610 STRING gives a Lisp string to iterate over.
6612 If PRECISION > 0, don't return more then PRECISION number of
6613 characters from the string.
6615 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6616 characters have been returned. FIELD_WIDTH < 0 means an infinite
6617 field width.
6619 MULTIBYTE = 0 means disable processing of multibyte characters,
6620 MULTIBYTE > 0 means enable it,
6621 MULTIBYTE < 0 means use IT->multibyte_p.
6623 IT must be initialized via a prior call to init_iterator before
6624 calling this function. */
6626 static void
6627 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6628 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6629 int multibyte)
6631 /* No text property checks performed by default, but see below. */
6632 it->stop_charpos = -1;
6634 /* Set iterator position and end position. */
6635 memset (&it->current, 0, sizeof it->current);
6636 it->current.overlay_string_index = -1;
6637 it->current.dpvec_index = -1;
6638 eassert (charpos >= 0);
6640 /* If STRING is specified, use its multibyteness, otherwise use the
6641 setting of MULTIBYTE, if specified. */
6642 if (multibyte >= 0)
6643 it->multibyte_p = multibyte > 0;
6645 /* Bidirectional reordering of strings is controlled by the default
6646 value of bidi-display-reordering. Don't try to reorder while
6647 loading loadup.el, as the necessary character property tables are
6648 not yet available. */
6649 it->bidi_p =
6650 !redisplay__inhibit_bidi
6651 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6653 if (s == NULL)
6655 eassert (STRINGP (string));
6656 it->string = string;
6657 it->s = NULL;
6658 it->end_charpos = it->string_nchars = SCHARS (string);
6659 it->method = GET_FROM_STRING;
6660 it->current.string_pos = string_pos (charpos, string);
6662 if (it->bidi_p)
6664 it->bidi_it.string.lstring = string;
6665 it->bidi_it.string.s = NULL;
6666 it->bidi_it.string.schars = it->end_charpos;
6667 it->bidi_it.string.bufpos = 0;
6668 it->bidi_it.string.from_disp_str = false;
6669 it->bidi_it.string.unibyte = !it->multibyte_p;
6670 it->bidi_it.w = it->w;
6671 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6672 FRAME_WINDOW_P (it->f), &it->bidi_it);
6675 else
6677 it->s = (const unsigned char *) s;
6678 it->string = Qnil;
6680 /* Note that we use IT->current.pos, not it->current.string_pos,
6681 for displaying C strings. */
6682 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6683 if (it->multibyte_p)
6685 it->current.pos = c_string_pos (charpos, s, true);
6686 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6688 else
6690 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6691 it->end_charpos = it->string_nchars = strlen (s);
6694 if (it->bidi_p)
6696 it->bidi_it.string.lstring = Qnil;
6697 it->bidi_it.string.s = (const unsigned char *) s;
6698 it->bidi_it.string.schars = it->end_charpos;
6699 it->bidi_it.string.bufpos = 0;
6700 it->bidi_it.string.from_disp_str = false;
6701 it->bidi_it.string.unibyte = !it->multibyte_p;
6702 it->bidi_it.w = it->w;
6703 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6704 &it->bidi_it);
6706 it->method = GET_FROM_C_STRING;
6709 /* PRECISION > 0 means don't return more than PRECISION characters
6710 from the string. */
6711 if (precision > 0 && it->end_charpos - charpos > precision)
6713 it->end_charpos = it->string_nchars = charpos + precision;
6714 if (it->bidi_p)
6715 it->bidi_it.string.schars = it->end_charpos;
6718 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6719 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6720 FIELD_WIDTH < 0 means infinite field width. This is useful for
6721 padding with `-' at the end of a mode line. */
6722 if (field_width < 0)
6723 field_width = INFINITY;
6724 /* Implementation note: We deliberately don't enlarge
6725 it->bidi_it.string.schars here to fit it->end_charpos, because
6726 the bidi iterator cannot produce characters out of thin air. */
6727 if (field_width > it->end_charpos - charpos)
6728 it->end_charpos = charpos + field_width;
6730 /* Use the standard display table for displaying strings. */
6731 if (DISP_TABLE_P (Vstandard_display_table))
6732 it->dp = XCHAR_TABLE (Vstandard_display_table);
6734 it->stop_charpos = charpos;
6735 it->prev_stop = charpos;
6736 it->base_level_stop = 0;
6737 if (it->bidi_p)
6739 it->bidi_it.first_elt = true;
6740 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6741 it->bidi_it.disp_pos = -1;
6743 if (s == NULL && it->multibyte_p)
6745 ptrdiff_t endpos = SCHARS (it->string);
6746 if (endpos > it->end_charpos)
6747 endpos = it->end_charpos;
6748 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6749 it->string);
6751 CHECK_IT (it);
6756 /***********************************************************************
6757 Iteration
6758 ***********************************************************************/
6760 /* Map enum it_method value to corresponding next_element_from_* function. */
6762 typedef bool (*next_element_function) (struct it *);
6764 static next_element_function const get_next_element[NUM_IT_METHODS] =
6766 next_element_from_buffer,
6767 next_element_from_display_vector,
6768 next_element_from_string,
6769 next_element_from_c_string,
6770 next_element_from_image,
6771 next_element_from_stretch,
6772 next_element_from_xwidget,
6775 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6778 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6779 (possibly with the following characters). */
6781 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6782 ((IT)->cmp_it.id >= 0 \
6783 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6784 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6785 END_CHARPOS, (IT)->w, \
6786 FACE_FROM_ID_OR_NULL ((IT)->f, \
6787 (IT)->face_id), \
6788 (IT)->string)))
6791 /* Lookup the char-table Vglyphless_char_display for character C (-1
6792 if we want information for no-font case), and return the display
6793 method symbol. By side-effect, update it->what and
6794 it->glyphless_method. This function is called from
6795 get_next_display_element for each character element, and from
6796 x_produce_glyphs when no suitable font was found. */
6798 Lisp_Object
6799 lookup_glyphless_char_display (int c, struct it *it)
6801 Lisp_Object glyphless_method = Qnil;
6803 if (CHAR_TABLE_P (Vglyphless_char_display)
6804 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6806 if (c >= 0)
6808 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6809 if (CONSP (glyphless_method))
6810 glyphless_method = FRAME_WINDOW_P (it->f)
6811 ? XCAR (glyphless_method)
6812 : XCDR (glyphless_method);
6814 else
6815 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6818 retry:
6819 if (NILP (glyphless_method))
6821 if (c >= 0)
6822 /* The default is to display the character by a proper font. */
6823 return Qnil;
6824 /* The default for the no-font case is to display an empty box. */
6825 glyphless_method = Qempty_box;
6827 if (EQ (glyphless_method, Qzero_width))
6829 if (c >= 0)
6830 return glyphless_method;
6831 /* This method can't be used for the no-font case. */
6832 glyphless_method = Qempty_box;
6834 if (EQ (glyphless_method, Qthin_space))
6835 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6836 else if (EQ (glyphless_method, Qempty_box))
6837 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6838 else if (EQ (glyphless_method, Qhex_code))
6839 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6840 else if (STRINGP (glyphless_method))
6841 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6842 else
6844 /* Invalid value. We use the default method. */
6845 glyphless_method = Qnil;
6846 goto retry;
6848 it->what = IT_GLYPHLESS;
6849 return glyphless_method;
6852 /* Merge escape glyph face and cache the result. */
6854 static struct frame *last_escape_glyph_frame = NULL;
6855 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6856 static int last_escape_glyph_merged_face_id = 0;
6858 static int
6859 merge_escape_glyph_face (struct it *it)
6861 int face_id;
6863 if (it->f == last_escape_glyph_frame
6864 && it->face_id == last_escape_glyph_face_id)
6865 face_id = last_escape_glyph_merged_face_id;
6866 else
6868 /* Merge the `escape-glyph' face into the current face. */
6869 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6870 last_escape_glyph_frame = it->f;
6871 last_escape_glyph_face_id = it->face_id;
6872 last_escape_glyph_merged_face_id = face_id;
6874 return face_id;
6877 /* Likewise for glyphless glyph face. */
6879 static struct frame *last_glyphless_glyph_frame = NULL;
6880 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6881 static int last_glyphless_glyph_merged_face_id = 0;
6884 merge_glyphless_glyph_face (struct it *it)
6886 int face_id;
6888 if (it->f == last_glyphless_glyph_frame
6889 && it->face_id == last_glyphless_glyph_face_id)
6890 face_id = last_glyphless_glyph_merged_face_id;
6891 else
6893 /* Merge the `glyphless-char' face into the current face. */
6894 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6895 last_glyphless_glyph_frame = it->f;
6896 last_glyphless_glyph_face_id = it->face_id;
6897 last_glyphless_glyph_merged_face_id = face_id;
6899 return face_id;
6902 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6903 be called before redisplaying windows, and when the frame's face
6904 cache is freed. */
6905 void
6906 forget_escape_and_glyphless_faces (void)
6908 last_escape_glyph_frame = NULL;
6909 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6910 last_glyphless_glyph_frame = NULL;
6911 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6914 /* Load IT's display element fields with information about the next
6915 display element from the current position of IT. Value is false if
6916 end of buffer (or C string) is reached. */
6918 static bool
6919 get_next_display_element (struct it *it)
6921 /* True means that we found a display element. False means that
6922 we hit the end of what we iterate over. Performance note: the
6923 function pointer `method' used here turns out to be faster than
6924 using a sequence of if-statements. */
6925 bool success_p;
6927 get_next:
6928 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6930 if (it->what == IT_CHARACTER)
6932 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6933 and only if (a) the resolved directionality of that character
6934 is R..." */
6935 /* FIXME: Do we need an exception for characters from display
6936 tables? */
6937 if (it->bidi_p && it->bidi_it.type == STRONG_R
6938 && !inhibit_bidi_mirroring)
6939 it->c = bidi_mirror_char (it->c);
6940 /* Map via display table or translate control characters.
6941 IT->c, IT->len etc. have been set to the next character by
6942 the function call above. If we have a display table, and it
6943 contains an entry for IT->c, translate it. Don't do this if
6944 IT->c itself comes from a display table, otherwise we could
6945 end up in an infinite recursion. (An alternative could be to
6946 count the recursion depth of this function and signal an
6947 error when a certain maximum depth is reached.) Is it worth
6948 it? */
6949 if (success_p && it->dpvec == NULL)
6951 Lisp_Object dv;
6952 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6953 bool nonascii_space_p = false;
6954 bool nonascii_hyphen_p = false;
6955 int c = it->c; /* This is the character to display. */
6957 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6959 eassert (SINGLE_BYTE_CHAR_P (c));
6960 if (unibyte_display_via_language_environment)
6962 c = DECODE_CHAR (unibyte, c);
6963 if (c < 0)
6964 c = BYTE8_TO_CHAR (it->c);
6966 else
6967 c = BYTE8_TO_CHAR (it->c);
6970 if (it->dp
6971 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6972 VECTORP (dv)))
6974 struct Lisp_Vector *v = XVECTOR (dv);
6976 /* Return the first character from the display table
6977 entry, if not empty. If empty, don't display the
6978 current character. */
6979 if (v->header.size)
6981 it->dpvec_char_len = it->len;
6982 it->dpvec = v->contents;
6983 it->dpend = v->contents + v->header.size;
6984 it->current.dpvec_index = 0;
6985 it->dpvec_face_id = -1;
6986 it->saved_face_id = it->face_id;
6987 it->method = GET_FROM_DISPLAY_VECTOR;
6988 it->ellipsis_p = false;
6990 else
6992 set_iterator_to_next (it, false);
6994 goto get_next;
6997 if (! NILP (lookup_glyphless_char_display (c, it)))
6999 if (it->what == IT_GLYPHLESS)
7000 goto done;
7001 /* Don't display this character. */
7002 set_iterator_to_next (it, false);
7003 goto get_next;
7006 /* If `nobreak-char-display' is non-nil, we display
7007 non-ASCII spaces and hyphens specially. */
7008 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7010 if (c == NO_BREAK_SPACE)
7011 nonascii_space_p = true;
7012 else if (c == SOFT_HYPHEN || c == HYPHEN
7013 || c == NON_BREAKING_HYPHEN)
7014 nonascii_hyphen_p = true;
7017 /* Translate control characters into `\003' or `^C' form.
7018 Control characters coming from a display table entry are
7019 currently not translated because we use IT->dpvec to hold
7020 the translation. This could easily be changed but I
7021 don't believe that it is worth doing.
7023 The characters handled by `nobreak-char-display' must be
7024 translated too.
7026 Non-printable characters and raw-byte characters are also
7027 translated to octal form. */
7028 if (((c < ' ' || c == 127) /* ASCII control chars. */
7029 ? (it->area != TEXT_AREA
7030 /* In mode line, treat \n, \t like other crl chars. */
7031 || (c != '\t'
7032 && it->glyph_row
7033 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7034 || (c != '\n' && c != '\t'))
7035 : (nonascii_space_p
7036 || nonascii_hyphen_p
7037 || CHAR_BYTE8_P (c)
7038 || ! CHAR_PRINTABLE_P (c))))
7040 /* C is a control character, non-ASCII space/hyphen,
7041 raw-byte, or a non-printable character which must be
7042 displayed either as '\003' or as `^C' where the '\\'
7043 and '^' can be defined in the display table. Fill
7044 IT->ctl_chars with glyphs for what we have to
7045 display. Then, set IT->dpvec to these glyphs. */
7046 Lisp_Object gc;
7047 int ctl_len;
7048 int face_id;
7049 int lface_id = 0;
7050 int escape_glyph;
7052 /* Handle control characters with ^. */
7054 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7056 int g;
7058 g = '^'; /* default glyph for Control */
7059 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7060 if (it->dp
7061 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7063 g = GLYPH_CODE_CHAR (gc);
7064 lface_id = GLYPH_CODE_FACE (gc);
7067 face_id = (lface_id
7068 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7069 : merge_escape_glyph_face (it));
7071 XSETINT (it->ctl_chars[0], g);
7072 XSETINT (it->ctl_chars[1], c ^ 0100);
7073 ctl_len = 2;
7074 goto display_control;
7077 /* Handle non-ascii space in the mode where it only gets
7078 highlighting. */
7080 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7082 /* Merge `nobreak-space' into the current face. */
7083 face_id = merge_faces (it->f, Qnobreak_space, 0,
7084 it->face_id);
7085 XSETINT (it->ctl_chars[0], ' ');
7086 ctl_len = 1;
7087 goto display_control;
7090 /* Handle non-ascii hyphens in the mode where it only
7091 gets highlighting. */
7093 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7095 /* Merge `nobreak-space' into the current face. */
7096 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7097 it->face_id);
7098 XSETINT (it->ctl_chars[0], '-');
7099 ctl_len = 1;
7100 goto display_control;
7103 /* Handle sequences that start with the "escape glyph". */
7105 /* the default escape glyph is \. */
7106 escape_glyph = '\\';
7108 if (it->dp
7109 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7111 escape_glyph = GLYPH_CODE_CHAR (gc);
7112 lface_id = GLYPH_CODE_FACE (gc);
7115 face_id = (lface_id
7116 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7117 : merge_escape_glyph_face (it));
7119 /* Draw non-ASCII space/hyphen with escape glyph: */
7121 if (nonascii_space_p || nonascii_hyphen_p)
7123 XSETINT (it->ctl_chars[0], escape_glyph);
7124 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7125 ctl_len = 2;
7126 goto display_control;
7130 char str[10];
7131 int len, i;
7133 if (CHAR_BYTE8_P (c))
7134 /* Display \200 instead of \17777600. */
7135 c = CHAR_TO_BYTE8 (c);
7136 len = sprintf (str, "%03o", c + 0u);
7138 XSETINT (it->ctl_chars[0], escape_glyph);
7139 for (i = 0; i < len; i++)
7140 XSETINT (it->ctl_chars[i + 1], str[i]);
7141 ctl_len = len + 1;
7144 display_control:
7145 /* Set up IT->dpvec and return first character from it. */
7146 it->dpvec_char_len = it->len;
7147 it->dpvec = it->ctl_chars;
7148 it->dpend = it->dpvec + ctl_len;
7149 it->current.dpvec_index = 0;
7150 it->dpvec_face_id = face_id;
7151 it->saved_face_id = it->face_id;
7152 it->method = GET_FROM_DISPLAY_VECTOR;
7153 it->ellipsis_p = false;
7154 goto get_next;
7156 it->char_to_display = c;
7158 else if (success_p)
7160 it->char_to_display = it->c;
7164 #ifdef HAVE_WINDOW_SYSTEM
7165 /* Adjust face id for a multibyte character. There are no multibyte
7166 character in unibyte text. */
7167 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7168 && it->multibyte_p
7169 && success_p
7170 && FRAME_WINDOW_P (it->f))
7172 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7174 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7176 /* Automatic composition with glyph-string. */
7177 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7179 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7181 else
7183 ptrdiff_t pos = (it->s ? -1
7184 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7185 : IT_CHARPOS (*it));
7186 int c;
7188 if (it->what == IT_CHARACTER)
7189 c = it->char_to_display;
7190 else
7192 struct composition *cmp = composition_table[it->cmp_it.id];
7193 int i;
7195 c = ' ';
7196 for (i = 0; i < cmp->glyph_len; i++)
7197 /* TAB in a composition means display glyphs with
7198 padding space on the left or right. */
7199 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7200 break;
7202 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7205 #endif /* HAVE_WINDOW_SYSTEM */
7207 done:
7208 /* Is this character the last one of a run of characters with
7209 box? If yes, set IT->end_of_box_run_p to true. */
7210 if (it->face_box_p
7211 && it->s == NULL)
7213 if (it->method == GET_FROM_STRING && it->sp)
7215 int face_id = underlying_face_id (it);
7216 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7218 if (face)
7220 if (face->box == FACE_NO_BOX)
7222 /* If the box comes from face properties in a
7223 display string, check faces in that string. */
7224 int string_face_id = face_after_it_pos (it);
7225 it->end_of_box_run_p
7226 = (FACE_FROM_ID (it->f, string_face_id)->box
7227 == FACE_NO_BOX);
7229 /* Otherwise, the box comes from the underlying face.
7230 If this is the last string character displayed, check
7231 the next buffer location. */
7232 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7233 /* n_overlay_strings is unreliable unless
7234 overlay_string_index is non-negative. */
7235 && ((it->current.overlay_string_index >= 0
7236 && (it->current.overlay_string_index
7237 == it->n_overlay_strings - 1))
7238 /* A string from display property. */
7239 || it->from_disp_prop_p))
7241 ptrdiff_t ignore;
7242 int next_face_id;
7243 bool text_from_string = false;
7244 /* Normally, the next buffer location is stored in
7245 IT->current.pos... */
7246 struct text_pos pos = it->current.pos;
7248 /* ...but for a string from a display property, the
7249 next buffer position is stored in the 'position'
7250 member of the iteration stack slot below the
7251 current one, see handle_single_display_spec. By
7252 contrast, it->current.pos was not yet updated to
7253 point to that buffer position; that will happen
7254 in pop_it, after we finish displaying the current
7255 string. Note that we already checked above that
7256 it->sp is positive, so subtracting one from it is
7257 safe. */
7258 if (it->from_disp_prop_p)
7260 int stackp = it->sp - 1;
7262 /* Find the stack level with data from buffer. */
7263 while (stackp >= 0
7264 && STRINGP ((it->stack + stackp)->string))
7265 stackp--;
7266 if (stackp < 0)
7268 /* If no stack slot was found for iterating
7269 a buffer, we are displaying text from a
7270 string, most probably the mode line or
7271 the header line, and that string has a
7272 display string on some of its
7273 characters. */
7274 text_from_string = true;
7275 pos = it->stack[it->sp - 1].position;
7277 else
7278 pos = (it->stack + stackp)->position;
7280 else
7281 INC_TEXT_POS (pos, it->multibyte_p);
7283 if (text_from_string)
7285 Lisp_Object base_string = it->stack[it->sp - 1].string;
7287 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7288 it->end_of_box_run_p = true;
7289 else
7291 next_face_id
7292 = face_at_string_position (it->w, base_string,
7293 CHARPOS (pos), 0,
7294 &ignore, face_id, false);
7295 it->end_of_box_run_p
7296 = (FACE_FROM_ID (it->f, next_face_id)->box
7297 == FACE_NO_BOX);
7300 else if (CHARPOS (pos) >= ZV)
7301 it->end_of_box_run_p = true;
7302 else
7304 next_face_id =
7305 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7306 CHARPOS (pos)
7307 + TEXT_PROP_DISTANCE_LIMIT,
7308 false, -1);
7309 it->end_of_box_run_p
7310 = (FACE_FROM_ID (it->f, next_face_id)->box
7311 == FACE_NO_BOX);
7316 /* next_element_from_display_vector sets this flag according to
7317 faces of the display vector glyphs, see there. */
7318 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7320 int face_id = face_after_it_pos (it);
7321 it->end_of_box_run_p
7322 = (face_id != it->face_id
7323 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7326 /* If we reached the end of the object we've been iterating (e.g., a
7327 display string or an overlay string), and there's something on
7328 IT->stack, proceed with what's on the stack. It doesn't make
7329 sense to return false if there's unprocessed stuff on the stack,
7330 because otherwise that stuff will never be displayed. */
7331 if (!success_p && it->sp > 0)
7333 set_iterator_to_next (it, false);
7334 success_p = get_next_display_element (it);
7337 /* Value is false if end of buffer or string reached. */
7338 return success_p;
7342 /* Move IT to the next display element.
7344 RESEAT_P means if called on a newline in buffer text,
7345 skip to the next visible line start.
7347 Functions get_next_display_element and set_iterator_to_next are
7348 separate because I find this arrangement easier to handle than a
7349 get_next_display_element function that also increments IT's
7350 position. The way it is we can first look at an iterator's current
7351 display element, decide whether it fits on a line, and if it does,
7352 increment the iterator position. The other way around we probably
7353 would either need a flag indicating whether the iterator has to be
7354 incremented the next time, or we would have to implement a
7355 decrement position function which would not be easy to write. */
7357 void
7358 set_iterator_to_next (struct it *it, bool reseat_p)
7360 /* Reset flags indicating start and end of a sequence of characters
7361 with box. Reset them at the start of this function because
7362 moving the iterator to a new position might set them. */
7363 it->start_of_box_run_p = it->end_of_box_run_p = false;
7365 switch (it->method)
7367 case GET_FROM_BUFFER:
7368 /* The current display element of IT is a character from
7369 current_buffer. Advance in the buffer, and maybe skip over
7370 invisible lines that are so because of selective display. */
7371 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7372 reseat_at_next_visible_line_start (it, false);
7373 else if (it->cmp_it.id >= 0)
7375 /* We are currently getting glyphs from a composition. */
7376 if (! it->bidi_p)
7378 IT_CHARPOS (*it) += it->cmp_it.nchars;
7379 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7381 else
7383 int i;
7385 /* Update IT's char/byte positions to point to the first
7386 character of the next grapheme cluster, or to the
7387 character visually after the current composition. */
7388 for (i = 0; i < it->cmp_it.nchars; i++)
7389 bidi_move_to_visually_next (&it->bidi_it);
7390 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7391 IT_CHARPOS (*it) = it->bidi_it.charpos;
7394 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7395 && it->cmp_it.to < it->cmp_it.nglyphs)
7397 /* Composition created while scanning forward. Proceed
7398 to the next grapheme cluster. */
7399 it->cmp_it.from = it->cmp_it.to;
7401 else if ((it->bidi_p && it->cmp_it.reversed_p)
7402 && it->cmp_it.from > 0)
7404 /* Composition created while scanning backward. Proceed
7405 to the previous grapheme cluster. */
7406 it->cmp_it.to = it->cmp_it.from;
7408 else
7410 /* No more grapheme clusters in this composition.
7411 Find the next stop position. */
7412 ptrdiff_t stop = it->end_charpos;
7414 if (it->bidi_it.scan_dir < 0)
7415 /* Now we are scanning backward and don't know
7416 where to stop. */
7417 stop = -1;
7418 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7419 IT_BYTEPOS (*it), stop, Qnil);
7422 else
7424 eassert (it->len != 0);
7426 if (!it->bidi_p)
7428 IT_BYTEPOS (*it) += it->len;
7429 IT_CHARPOS (*it) += 1;
7431 else
7433 int prev_scan_dir = it->bidi_it.scan_dir;
7434 /* If this is a new paragraph, determine its base
7435 direction (a.k.a. its base embedding level). */
7436 if (it->bidi_it.new_paragraph)
7437 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7438 false);
7439 bidi_move_to_visually_next (&it->bidi_it);
7440 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7441 IT_CHARPOS (*it) = it->bidi_it.charpos;
7442 if (prev_scan_dir != it->bidi_it.scan_dir)
7444 /* As the scan direction was changed, we must
7445 re-compute the stop position for composition. */
7446 ptrdiff_t stop = it->end_charpos;
7447 if (it->bidi_it.scan_dir < 0)
7448 stop = -1;
7449 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7450 IT_BYTEPOS (*it), stop, Qnil);
7453 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7455 break;
7457 case GET_FROM_C_STRING:
7458 /* Current display element of IT is from a C string. */
7459 if (!it->bidi_p
7460 /* If the string position is beyond string's end, it means
7461 next_element_from_c_string is padding the string with
7462 blanks, in which case we bypass the bidi iterator,
7463 because it cannot deal with such virtual characters. */
7464 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7466 IT_BYTEPOS (*it) += it->len;
7467 IT_CHARPOS (*it) += 1;
7469 else
7471 bidi_move_to_visually_next (&it->bidi_it);
7472 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7473 IT_CHARPOS (*it) = it->bidi_it.charpos;
7475 break;
7477 case GET_FROM_DISPLAY_VECTOR:
7478 /* Current display element of IT is from a display table entry.
7479 Advance in the display table definition. Reset it to null if
7480 end reached, and continue with characters from buffers/
7481 strings. */
7482 ++it->current.dpvec_index;
7484 /* Restore face of the iterator to what they were before the
7485 display vector entry (these entries may contain faces). */
7486 it->face_id = it->saved_face_id;
7488 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7490 bool recheck_faces = it->ellipsis_p;
7492 if (it->s)
7493 it->method = GET_FROM_C_STRING;
7494 else if (STRINGP (it->string))
7495 it->method = GET_FROM_STRING;
7496 else
7498 it->method = GET_FROM_BUFFER;
7499 it->object = it->w->contents;
7502 it->dpvec = NULL;
7503 it->current.dpvec_index = -1;
7505 /* Skip over characters which were displayed via IT->dpvec. */
7506 if (it->dpvec_char_len < 0)
7507 reseat_at_next_visible_line_start (it, true);
7508 else if (it->dpvec_char_len > 0)
7510 it->len = it->dpvec_char_len;
7511 set_iterator_to_next (it, reseat_p);
7514 /* Maybe recheck faces after display vector. */
7515 if (recheck_faces)
7517 if (it->method == GET_FROM_STRING)
7518 it->stop_charpos = IT_STRING_CHARPOS (*it);
7519 else
7520 it->stop_charpos = IT_CHARPOS (*it);
7523 break;
7525 case GET_FROM_STRING:
7526 /* Current display element is a character from a Lisp string. */
7527 eassert (it->s == NULL && STRINGP (it->string));
7528 /* Don't advance past string end. These conditions are true
7529 when set_iterator_to_next is called at the end of
7530 get_next_display_element, in which case the Lisp string is
7531 already exhausted, and all we want is pop the iterator
7532 stack. */
7533 if (it->current.overlay_string_index >= 0)
7535 /* This is an overlay string, so there's no padding with
7536 spaces, and the number of characters in the string is
7537 where the string ends. */
7538 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7539 goto consider_string_end;
7541 else
7543 /* Not an overlay string. There could be padding, so test
7544 against it->end_charpos. */
7545 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7546 goto consider_string_end;
7548 if (it->cmp_it.id >= 0)
7550 /* We are delivering display elements from a composition.
7551 Update the string position past the grapheme cluster
7552 we've just processed. */
7553 if (! it->bidi_p)
7555 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7556 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7558 else
7560 int i;
7562 for (i = 0; i < it->cmp_it.nchars; i++)
7563 bidi_move_to_visually_next (&it->bidi_it);
7564 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7565 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7568 /* Did we exhaust all the grapheme clusters of this
7569 composition? */
7570 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7571 && (it->cmp_it.to < it->cmp_it.nglyphs))
7573 /* Not all the grapheme clusters were processed yet;
7574 advance to the next cluster. */
7575 it->cmp_it.from = it->cmp_it.to;
7577 else if ((it->bidi_p && it->cmp_it.reversed_p)
7578 && it->cmp_it.from > 0)
7580 /* Likewise: advance to the next cluster, but going in
7581 the reverse direction. */
7582 it->cmp_it.to = it->cmp_it.from;
7584 else
7586 /* This composition was fully processed; find the next
7587 candidate place for checking for composed
7588 characters. */
7589 /* Always limit string searches to the string length;
7590 any padding spaces are not part of the string, and
7591 there cannot be any compositions in that padding. */
7592 ptrdiff_t stop = SCHARS (it->string);
7594 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7595 stop = -1;
7596 else if (it->end_charpos < stop)
7598 /* Cf. PRECISION in reseat_to_string: we might be
7599 limited in how many of the string characters we
7600 need to deliver. */
7601 stop = it->end_charpos;
7603 composition_compute_stop_pos (&it->cmp_it,
7604 IT_STRING_CHARPOS (*it),
7605 IT_STRING_BYTEPOS (*it), stop,
7606 it->string);
7609 else
7611 if (!it->bidi_p
7612 /* If the string position is beyond string's end, it
7613 means next_element_from_string is padding the string
7614 with blanks, in which case we bypass the bidi
7615 iterator, because it cannot deal with such virtual
7616 characters. */
7617 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7619 IT_STRING_BYTEPOS (*it) += it->len;
7620 IT_STRING_CHARPOS (*it) += 1;
7622 else
7624 int prev_scan_dir = it->bidi_it.scan_dir;
7626 bidi_move_to_visually_next (&it->bidi_it);
7627 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7628 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7629 /* If the scan direction changes, we may need to update
7630 the place where to check for composed characters. */
7631 if (prev_scan_dir != it->bidi_it.scan_dir)
7633 ptrdiff_t stop = SCHARS (it->string);
7635 if (it->bidi_it.scan_dir < 0)
7636 stop = -1;
7637 else if (it->end_charpos < stop)
7638 stop = it->end_charpos;
7640 composition_compute_stop_pos (&it->cmp_it,
7641 IT_STRING_CHARPOS (*it),
7642 IT_STRING_BYTEPOS (*it), stop,
7643 it->string);
7648 consider_string_end:
7650 if (it->current.overlay_string_index >= 0)
7652 /* IT->string is an overlay string. Advance to the
7653 next, if there is one. */
7654 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7656 it->ellipsis_p = false;
7657 next_overlay_string (it);
7658 if (it->ellipsis_p)
7659 setup_for_ellipsis (it, 0);
7662 else
7664 /* IT->string is not an overlay string. If we reached
7665 its end, and there is something on IT->stack, proceed
7666 with what is on the stack. This can be either another
7667 string, this time an overlay string, or a buffer. */
7668 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7669 && it->sp > 0)
7671 pop_it (it);
7672 if (it->method == GET_FROM_STRING)
7673 goto consider_string_end;
7676 break;
7678 case GET_FROM_IMAGE:
7679 case GET_FROM_STRETCH:
7680 case GET_FROM_XWIDGET:
7682 /* The position etc with which we have to proceed are on
7683 the stack. The position may be at the end of a string,
7684 if the `display' property takes up the whole string. */
7685 eassert (it->sp > 0);
7686 pop_it (it);
7687 if (it->method == GET_FROM_STRING)
7688 goto consider_string_end;
7689 break;
7691 default:
7692 /* There are no other methods defined, so this should be a bug. */
7693 emacs_abort ();
7696 eassert (it->method != GET_FROM_STRING
7697 || (STRINGP (it->string)
7698 && IT_STRING_CHARPOS (*it) >= 0));
7701 /* Load IT's display element fields with information about the next
7702 display element which comes from a display table entry or from the
7703 result of translating a control character to one of the forms `^C'
7704 or `\003'.
7706 IT->dpvec holds the glyphs to return as characters.
7707 IT->saved_face_id holds the face id before the display vector--it
7708 is restored into IT->face_id in set_iterator_to_next. */
7710 static bool
7711 next_element_from_display_vector (struct it *it)
7713 Lisp_Object gc;
7714 int prev_face_id = it->face_id;
7715 int next_face_id;
7717 /* Precondition. */
7718 eassert (it->dpvec && it->current.dpvec_index >= 0);
7720 it->face_id = it->saved_face_id;
7722 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7723 That seemed totally bogus - so I changed it... */
7724 gc = it->dpvec[it->current.dpvec_index];
7726 if (GLYPH_CODE_P (gc))
7728 struct face *this_face, *prev_face, *next_face;
7730 it->c = GLYPH_CODE_CHAR (gc);
7731 it->len = CHAR_BYTES (it->c);
7733 /* The entry may contain a face id to use. Such a face id is
7734 the id of a Lisp face, not a realized face. A face id of
7735 zero means no face is specified. */
7736 if (it->dpvec_face_id >= 0)
7737 it->face_id = it->dpvec_face_id;
7738 else
7740 int lface_id = GLYPH_CODE_FACE (gc);
7741 if (lface_id > 0)
7742 it->face_id = merge_faces (it->f, Qt, lface_id,
7743 it->saved_face_id);
7746 /* Glyphs in the display vector could have the box face, so we
7747 need to set the related flags in the iterator, as
7748 appropriate. */
7749 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7750 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7752 /* Is this character the first character of a box-face run? */
7753 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7754 && (!prev_face
7755 || prev_face->box == FACE_NO_BOX));
7757 /* For the last character of the box-face run, we need to look
7758 either at the next glyph from the display vector, or at the
7759 face we saw before the display vector. */
7760 next_face_id = it->saved_face_id;
7761 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7763 if (it->dpvec_face_id >= 0)
7764 next_face_id = it->dpvec_face_id;
7765 else
7767 int lface_id =
7768 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7770 if (lface_id > 0)
7771 next_face_id = merge_faces (it->f, Qt, lface_id,
7772 it->saved_face_id);
7775 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7776 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7777 && (!next_face
7778 || next_face->box == FACE_NO_BOX));
7779 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7781 else
7782 /* Display table entry is invalid. Return a space. */
7783 it->c = ' ', it->len = 1;
7785 /* Don't change position and object of the iterator here. They are
7786 still the values of the character that had this display table
7787 entry or was translated, and that's what we want. */
7788 it->what = IT_CHARACTER;
7789 return true;
7792 /* Get the first element of string/buffer in the visual order, after
7793 being reseated to a new position in a string or a buffer. */
7794 static void
7795 get_visually_first_element (struct it *it)
7797 bool string_p = STRINGP (it->string) || it->s;
7798 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7799 ptrdiff_t bob = (string_p ? 0 : BEGV);
7801 if (STRINGP (it->string))
7803 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7804 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7806 else
7808 it->bidi_it.charpos = IT_CHARPOS (*it);
7809 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7812 if (it->bidi_it.charpos == eob)
7814 /* Nothing to do, but reset the FIRST_ELT flag, like
7815 bidi_paragraph_init does, because we are not going to
7816 call it. */
7817 it->bidi_it.first_elt = false;
7819 else if (it->bidi_it.charpos == bob
7820 || (!string_p
7821 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7822 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7824 /* If we are at the beginning of a line/string, we can produce
7825 the next element right away. */
7826 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7827 bidi_move_to_visually_next (&it->bidi_it);
7829 else
7831 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7833 /* We need to prime the bidi iterator starting at the line's or
7834 string's beginning, before we will be able to produce the
7835 next element. */
7836 if (string_p)
7837 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7838 else
7839 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7840 IT_BYTEPOS (*it), -1,
7841 &it->bidi_it.bytepos);
7842 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7845 /* Now return to buffer/string position where we were asked
7846 to get the next display element, and produce that. */
7847 bidi_move_to_visually_next (&it->bidi_it);
7849 while (it->bidi_it.bytepos != orig_bytepos
7850 && it->bidi_it.charpos < eob);
7853 /* Adjust IT's position information to where we ended up. */
7854 if (STRINGP (it->string))
7856 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7857 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7859 else
7861 IT_CHARPOS (*it) = it->bidi_it.charpos;
7862 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7865 if (STRINGP (it->string) || !it->s)
7867 ptrdiff_t stop, charpos, bytepos;
7869 if (STRINGP (it->string))
7871 eassert (!it->s);
7872 stop = SCHARS (it->string);
7873 if (stop > it->end_charpos)
7874 stop = it->end_charpos;
7875 charpos = IT_STRING_CHARPOS (*it);
7876 bytepos = IT_STRING_BYTEPOS (*it);
7878 else
7880 stop = it->end_charpos;
7881 charpos = IT_CHARPOS (*it);
7882 bytepos = IT_BYTEPOS (*it);
7884 if (it->bidi_it.scan_dir < 0)
7885 stop = -1;
7886 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7887 it->string);
7891 /* Load IT with the next display element from Lisp string IT->string.
7892 IT->current.string_pos is the current position within the string.
7893 If IT->current.overlay_string_index >= 0, the Lisp string is an
7894 overlay string. */
7896 static bool
7897 next_element_from_string (struct it *it)
7899 struct text_pos position;
7901 eassert (STRINGP (it->string));
7902 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7903 eassert (IT_STRING_CHARPOS (*it) >= 0);
7904 position = it->current.string_pos;
7906 /* With bidi reordering, the character to display might not be the
7907 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7908 that we were reseat()ed to a new string, whose paragraph
7909 direction is not known. */
7910 if (it->bidi_p && it->bidi_it.first_elt)
7912 get_visually_first_element (it);
7913 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7916 /* Time to check for invisible text? */
7917 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7919 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7921 if (!(!it->bidi_p
7922 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7923 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7925 /* With bidi non-linear iteration, we could find
7926 ourselves far beyond the last computed stop_charpos,
7927 with several other stop positions in between that we
7928 missed. Scan them all now, in buffer's logical
7929 order, until we find and handle the last stop_charpos
7930 that precedes our current position. */
7931 handle_stop_backwards (it, it->stop_charpos);
7932 return GET_NEXT_DISPLAY_ELEMENT (it);
7934 else
7936 if (it->bidi_p)
7938 /* Take note of the stop position we just moved
7939 across, for when we will move back across it. */
7940 it->prev_stop = it->stop_charpos;
7941 /* If we are at base paragraph embedding level, take
7942 note of the last stop position seen at this
7943 level. */
7944 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7945 it->base_level_stop = it->stop_charpos;
7947 handle_stop (it);
7949 /* Since a handler may have changed IT->method, we must
7950 recurse here. */
7951 return GET_NEXT_DISPLAY_ELEMENT (it);
7954 else if (it->bidi_p
7955 /* If we are before prev_stop, we may have overstepped
7956 on our way backwards a stop_pos, and if so, we need
7957 to handle that stop_pos. */
7958 && IT_STRING_CHARPOS (*it) < it->prev_stop
7959 /* We can sometimes back up for reasons that have nothing
7960 to do with bidi reordering. E.g., compositions. The
7961 code below is only needed when we are above the base
7962 embedding level, so test for that explicitly. */
7963 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7965 /* If we lost track of base_level_stop, we have no better
7966 place for handle_stop_backwards to start from than string
7967 beginning. This happens, e.g., when we were reseated to
7968 the previous screenful of text by vertical-motion. */
7969 if (it->base_level_stop <= 0
7970 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7971 it->base_level_stop = 0;
7972 handle_stop_backwards (it, it->base_level_stop);
7973 return GET_NEXT_DISPLAY_ELEMENT (it);
7977 if (it->current.overlay_string_index >= 0)
7979 /* Get the next character from an overlay string. In overlay
7980 strings, there is no field width or padding with spaces to
7981 do. */
7982 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7984 it->what = IT_EOB;
7985 return false;
7987 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7988 IT_STRING_BYTEPOS (*it),
7989 it->bidi_it.scan_dir < 0
7990 ? -1
7991 : SCHARS (it->string))
7992 && next_element_from_composition (it))
7994 return true;
7996 else if (STRING_MULTIBYTE (it->string))
7998 const unsigned char *s = (SDATA (it->string)
7999 + IT_STRING_BYTEPOS (*it));
8000 it->c = string_char_and_length (s, &it->len);
8002 else
8004 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8005 it->len = 1;
8008 else
8010 /* Get the next character from a Lisp string that is not an
8011 overlay string. Such strings come from the mode line, for
8012 example. We may have to pad with spaces, or truncate the
8013 string. See also next_element_from_c_string. */
8014 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8016 it->what = IT_EOB;
8017 return false;
8019 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8021 /* Pad with spaces. */
8022 it->c = ' ', it->len = 1;
8023 CHARPOS (position) = BYTEPOS (position) = -1;
8025 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8026 IT_STRING_BYTEPOS (*it),
8027 it->bidi_it.scan_dir < 0
8028 ? -1
8029 : it->string_nchars)
8030 && next_element_from_composition (it))
8032 return true;
8034 else if (STRING_MULTIBYTE (it->string))
8036 const unsigned char *s = (SDATA (it->string)
8037 + IT_STRING_BYTEPOS (*it));
8038 it->c = string_char_and_length (s, &it->len);
8040 else
8042 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8043 it->len = 1;
8047 /* Record what we have and where it came from. */
8048 it->what = IT_CHARACTER;
8049 it->object = it->string;
8050 it->position = position;
8051 return true;
8055 /* Load IT with next display element from C string IT->s.
8056 IT->string_nchars is the maximum number of characters to return
8057 from the string. IT->end_charpos may be greater than
8058 IT->string_nchars when this function is called, in which case we
8059 may have to return padding spaces. Value is false if end of string
8060 reached, including padding spaces. */
8062 static bool
8063 next_element_from_c_string (struct it *it)
8065 bool success_p = true;
8067 eassert (it->s);
8068 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8069 it->what = IT_CHARACTER;
8070 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8071 it->object = make_number (0);
8073 /* With bidi reordering, the character to display might not be the
8074 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8075 we were reseated to a new string, whose paragraph direction is
8076 not known. */
8077 if (it->bidi_p && it->bidi_it.first_elt)
8078 get_visually_first_element (it);
8080 /* IT's position can be greater than IT->string_nchars in case a
8081 field width or precision has been specified when the iterator was
8082 initialized. */
8083 if (IT_CHARPOS (*it) >= it->end_charpos)
8085 /* End of the game. */
8086 it->what = IT_EOB;
8087 success_p = false;
8089 else if (IT_CHARPOS (*it) >= it->string_nchars)
8091 /* Pad with spaces. */
8092 it->c = ' ', it->len = 1;
8093 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8095 else if (it->multibyte_p)
8096 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8097 else
8098 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8100 return success_p;
8104 /* Set up IT to return characters from an ellipsis, if appropriate.
8105 The definition of the ellipsis glyphs may come from a display table
8106 entry. This function fills IT with the first glyph from the
8107 ellipsis if an ellipsis is to be displayed. */
8109 static bool
8110 next_element_from_ellipsis (struct it *it)
8112 if (it->selective_display_ellipsis_p)
8113 setup_for_ellipsis (it, it->len);
8114 else
8116 /* The face at the current position may be different from the
8117 face we find after the invisible text. Remember what it
8118 was in IT->saved_face_id, and signal that it's there by
8119 setting face_before_selective_p. */
8120 it->saved_face_id = it->face_id;
8121 it->method = GET_FROM_BUFFER;
8122 it->object = it->w->contents;
8123 reseat_at_next_visible_line_start (it, true);
8124 it->face_before_selective_p = true;
8127 return GET_NEXT_DISPLAY_ELEMENT (it);
8131 /* Deliver an image display element. The iterator IT is already
8132 filled with image information (done in handle_display_prop). Value
8133 is always true. */
8136 static bool
8137 next_element_from_image (struct it *it)
8139 it->what = IT_IMAGE;
8140 return true;
8143 static bool
8144 next_element_from_xwidget (struct it *it)
8146 it->what = IT_XWIDGET;
8147 return true;
8151 /* Fill iterator IT with next display element from a stretch glyph
8152 property. IT->object is the value of the text property. Value is
8153 always true. */
8155 static bool
8156 next_element_from_stretch (struct it *it)
8158 it->what = IT_STRETCH;
8159 return true;
8162 /* Scan backwards from IT's current position until we find a stop
8163 position, or until BEGV. This is called when we find ourself
8164 before both the last known prev_stop and base_level_stop while
8165 reordering bidirectional text. */
8167 static void
8168 compute_stop_pos_backwards (struct it *it)
8170 const int SCAN_BACK_LIMIT = 1000;
8171 struct text_pos pos;
8172 struct display_pos save_current = it->current;
8173 struct text_pos save_position = it->position;
8174 ptrdiff_t charpos = IT_CHARPOS (*it);
8175 ptrdiff_t where_we_are = charpos;
8176 ptrdiff_t save_stop_pos = it->stop_charpos;
8177 ptrdiff_t save_end_pos = it->end_charpos;
8179 eassert (NILP (it->string) && !it->s);
8180 eassert (it->bidi_p);
8181 it->bidi_p = false;
8184 it->end_charpos = min (charpos + 1, ZV);
8185 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8186 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8187 reseat_1 (it, pos, false);
8188 compute_stop_pos (it);
8189 /* We must advance forward, right? */
8190 if (it->stop_charpos <= charpos)
8191 emacs_abort ();
8193 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8195 if (it->stop_charpos <= where_we_are)
8196 it->prev_stop = it->stop_charpos;
8197 else
8198 it->prev_stop = BEGV;
8199 it->bidi_p = true;
8200 it->current = save_current;
8201 it->position = save_position;
8202 it->stop_charpos = save_stop_pos;
8203 it->end_charpos = save_end_pos;
8206 /* Scan forward from CHARPOS in the current buffer/string, until we
8207 find a stop position > current IT's position. Then handle the stop
8208 position before that. This is called when we bump into a stop
8209 position while reordering bidirectional text. CHARPOS should be
8210 the last previously processed stop_pos (or BEGV/0, if none were
8211 processed yet) whose position is less that IT's current
8212 position. */
8214 static void
8215 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8217 bool bufp = !STRINGP (it->string);
8218 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8219 struct display_pos save_current = it->current;
8220 struct text_pos save_position = it->position;
8221 struct text_pos pos1;
8222 ptrdiff_t next_stop;
8224 /* Scan in strict logical order. */
8225 eassert (it->bidi_p);
8226 it->bidi_p = false;
8229 it->prev_stop = charpos;
8230 if (bufp)
8232 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8233 reseat_1 (it, pos1, false);
8235 else
8236 it->current.string_pos = string_pos (charpos, it->string);
8237 compute_stop_pos (it);
8238 /* We must advance forward, right? */
8239 if (it->stop_charpos <= it->prev_stop)
8240 emacs_abort ();
8241 charpos = it->stop_charpos;
8243 while (charpos <= where_we_are);
8245 it->bidi_p = true;
8246 it->current = save_current;
8247 it->position = save_position;
8248 next_stop = it->stop_charpos;
8249 it->stop_charpos = it->prev_stop;
8250 handle_stop (it);
8251 it->stop_charpos = next_stop;
8254 /* Load IT with the next display element from current_buffer. Value
8255 is false if end of buffer reached. IT->stop_charpos is the next
8256 position at which to stop and check for text properties or buffer
8257 end. */
8259 static bool
8260 next_element_from_buffer (struct it *it)
8262 bool success_p = true;
8264 eassert (IT_CHARPOS (*it) >= BEGV);
8265 eassert (NILP (it->string) && !it->s);
8266 eassert (!it->bidi_p
8267 || (EQ (it->bidi_it.string.lstring, Qnil)
8268 && it->bidi_it.string.s == NULL));
8270 /* With bidi reordering, the character to display might not be the
8271 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8272 we were reseat()ed to a new buffer position, which is potentially
8273 a different paragraph. */
8274 if (it->bidi_p && it->bidi_it.first_elt)
8276 get_visually_first_element (it);
8277 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8280 if (IT_CHARPOS (*it) >= it->stop_charpos)
8282 if (IT_CHARPOS (*it) >= it->end_charpos)
8284 bool overlay_strings_follow_p;
8286 /* End of the game, except when overlay strings follow that
8287 haven't been returned yet. */
8288 if (it->overlay_strings_at_end_processed_p)
8289 overlay_strings_follow_p = false;
8290 else
8292 it->overlay_strings_at_end_processed_p = true;
8293 overlay_strings_follow_p = get_overlay_strings (it, 0);
8296 if (overlay_strings_follow_p)
8297 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8298 else
8300 it->what = IT_EOB;
8301 it->position = it->current.pos;
8302 success_p = false;
8305 else if (!(!it->bidi_p
8306 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8307 || IT_CHARPOS (*it) == it->stop_charpos))
8309 /* With bidi non-linear iteration, we could find ourselves
8310 far beyond the last computed stop_charpos, with several
8311 other stop positions in between that we missed. Scan
8312 them all now, in buffer's logical order, until we find
8313 and handle the last stop_charpos that precedes our
8314 current position. */
8315 handle_stop_backwards (it, it->stop_charpos);
8316 it->ignore_overlay_strings_at_pos_p = false;
8317 return GET_NEXT_DISPLAY_ELEMENT (it);
8319 else
8321 if (it->bidi_p)
8323 /* Take note of the stop position we just moved across,
8324 for when we will move back across it. */
8325 it->prev_stop = it->stop_charpos;
8326 /* If we are at base paragraph embedding level, take
8327 note of the last stop position seen at this
8328 level. */
8329 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8330 it->base_level_stop = it->stop_charpos;
8332 handle_stop (it);
8333 it->ignore_overlay_strings_at_pos_p = false;
8334 return GET_NEXT_DISPLAY_ELEMENT (it);
8337 else if (it->bidi_p
8338 /* If we are before prev_stop, we may have overstepped on
8339 our way backwards a stop_pos, and if so, we need to
8340 handle that stop_pos. */
8341 && IT_CHARPOS (*it) < it->prev_stop
8342 /* We can sometimes back up for reasons that have nothing
8343 to do with bidi reordering. E.g., compositions. The
8344 code below is only needed when we are above the base
8345 embedding level, so test for that explicitly. */
8346 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8348 if (it->base_level_stop <= 0
8349 || IT_CHARPOS (*it) < it->base_level_stop)
8351 /* If we lost track of base_level_stop, we need to find
8352 prev_stop by looking backwards. This happens, e.g., when
8353 we were reseated to the previous screenful of text by
8354 vertical-motion. */
8355 it->base_level_stop = BEGV;
8356 compute_stop_pos_backwards (it);
8357 handle_stop_backwards (it, it->prev_stop);
8359 else
8360 handle_stop_backwards (it, it->base_level_stop);
8361 it->ignore_overlay_strings_at_pos_p = false;
8362 return GET_NEXT_DISPLAY_ELEMENT (it);
8364 else
8366 /* No face changes, overlays etc. in sight, so just return a
8367 character from current_buffer. */
8368 unsigned char *p;
8369 ptrdiff_t stop;
8371 /* We moved to the next buffer position, so any info about
8372 previously seen overlays is no longer valid. */
8373 it->ignore_overlay_strings_at_pos_p = false;
8375 /* Maybe run the redisplay end trigger hook. Performance note:
8376 This doesn't seem to cost measurable time. */
8377 if (it->redisplay_end_trigger_charpos
8378 && it->glyph_row
8379 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8380 run_redisplay_end_trigger_hook (it);
8382 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8383 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8384 stop)
8385 && next_element_from_composition (it))
8387 return true;
8390 /* Get the next character, maybe multibyte. */
8391 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8392 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8393 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8394 else
8395 it->c = *p, it->len = 1;
8397 /* Record what we have and where it came from. */
8398 it->what = IT_CHARACTER;
8399 it->object = it->w->contents;
8400 it->position = it->current.pos;
8402 /* Normally we return the character found above, except when we
8403 really want to return an ellipsis for selective display. */
8404 if (it->selective)
8406 if (it->c == '\n')
8408 /* A value of selective > 0 means hide lines indented more
8409 than that number of columns. */
8410 if (it->selective > 0
8411 && IT_CHARPOS (*it) + 1 < ZV
8412 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8413 IT_BYTEPOS (*it) + 1,
8414 it->selective))
8416 success_p = next_element_from_ellipsis (it);
8417 it->dpvec_char_len = -1;
8420 else if (it->c == '\r' && it->selective == -1)
8422 /* A value of selective == -1 means that everything from the
8423 CR to the end of the line is invisible, with maybe an
8424 ellipsis displayed for it. */
8425 success_p = next_element_from_ellipsis (it);
8426 it->dpvec_char_len = -1;
8431 /* Value is false if end of buffer reached. */
8432 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8433 return success_p;
8437 /* Run the redisplay end trigger hook for IT. */
8439 static void
8440 run_redisplay_end_trigger_hook (struct it *it)
8442 /* IT->glyph_row should be non-null, i.e. we should be actually
8443 displaying something, or otherwise we should not run the hook. */
8444 eassert (it->glyph_row);
8446 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8447 it->redisplay_end_trigger_charpos = 0;
8449 /* Since we are *trying* to run these functions, don't try to run
8450 them again, even if they get an error. */
8451 wset_redisplay_end_trigger (it->w, Qnil);
8452 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8453 make_number (charpos));
8455 /* Notice if it changed the face of the character we are on. */
8456 handle_face_prop (it);
8460 /* Deliver a composition display element. Unlike the other
8461 next_element_from_XXX, this function is not registered in the array
8462 get_next_element[]. It is called from next_element_from_buffer and
8463 next_element_from_string when necessary. */
8465 static bool
8466 next_element_from_composition (struct it *it)
8468 it->what = IT_COMPOSITION;
8469 it->len = it->cmp_it.nbytes;
8470 if (STRINGP (it->string))
8472 if (it->c < 0)
8474 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8475 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8476 return false;
8478 it->position = it->current.string_pos;
8479 it->object = it->string;
8480 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8481 IT_STRING_BYTEPOS (*it), it->string);
8483 else
8485 if (it->c < 0)
8487 IT_CHARPOS (*it) += it->cmp_it.nchars;
8488 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8489 if (it->bidi_p)
8491 if (it->bidi_it.new_paragraph)
8492 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8493 false);
8494 /* Resync the bidi iterator with IT's new position.
8495 FIXME: this doesn't support bidirectional text. */
8496 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8497 bidi_move_to_visually_next (&it->bidi_it);
8499 return false;
8501 it->position = it->current.pos;
8502 it->object = it->w->contents;
8503 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8504 IT_BYTEPOS (*it), Qnil);
8506 return true;
8511 /***********************************************************************
8512 Moving an iterator without producing glyphs
8513 ***********************************************************************/
8515 /* Check if iterator is at a position corresponding to a valid buffer
8516 position after some move_it_ call. */
8518 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8519 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8522 /* Move iterator IT to a specified buffer or X position within one
8523 line on the display without producing glyphs.
8525 OP should be a bit mask including some or all of these bits:
8526 MOVE_TO_X: Stop upon reaching x-position TO_X.
8527 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8528 Regardless of OP's value, stop upon reaching the end of the display line.
8530 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8531 This means, in particular, that TO_X includes window's horizontal
8532 scroll amount.
8534 The return value has several possible values that
8535 say what condition caused the scan to stop:
8537 MOVE_POS_MATCH_OR_ZV
8538 - when TO_POS or ZV was reached.
8540 MOVE_X_REACHED
8541 -when TO_X was reached before TO_POS or ZV were reached.
8543 MOVE_LINE_CONTINUED
8544 - when we reached the end of the display area and the line must
8545 be continued.
8547 MOVE_LINE_TRUNCATED
8548 - when we reached the end of the display area and the line is
8549 truncated.
8551 MOVE_NEWLINE_OR_CR
8552 - when we stopped at a line end, i.e. a newline or a CR and selective
8553 display is on. */
8555 static enum move_it_result
8556 move_it_in_display_line_to (struct it *it,
8557 ptrdiff_t to_charpos, int to_x,
8558 enum move_operation_enum op)
8560 enum move_it_result result = MOVE_UNDEFINED;
8561 struct glyph_row *saved_glyph_row;
8562 struct it wrap_it, atpos_it, atx_it, ppos_it;
8563 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8564 void *ppos_data = NULL;
8565 bool may_wrap = false;
8566 enum it_method prev_method = it->method;
8567 ptrdiff_t closest_pos UNINIT;
8568 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8569 bool saw_smaller_pos = prev_pos < to_charpos;
8571 /* Don't produce glyphs in produce_glyphs. */
8572 saved_glyph_row = it->glyph_row;
8573 it->glyph_row = NULL;
8575 /* Use wrap_it to save a copy of IT wherever a word wrap could
8576 occur. Use atpos_it to save a copy of IT at the desired buffer
8577 position, if found, so that we can scan ahead and check if the
8578 word later overshoots the window edge. Use atx_it similarly, for
8579 pixel positions. */
8580 wrap_it.sp = -1;
8581 atpos_it.sp = -1;
8582 atx_it.sp = -1;
8584 /* Use ppos_it under bidi reordering to save a copy of IT for the
8585 initial position. We restore that position in IT when we have
8586 scanned the entire display line without finding a match for
8587 TO_CHARPOS and all the character positions are greater than
8588 TO_CHARPOS. We then restart the scan from the initial position,
8589 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8590 the closest to TO_CHARPOS. */
8591 if (it->bidi_p)
8593 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8595 SAVE_IT (ppos_it, *it, ppos_data);
8596 closest_pos = IT_CHARPOS (*it);
8598 else
8599 closest_pos = ZV;
8602 #define BUFFER_POS_REACHED_P() \
8603 ((op & MOVE_TO_POS) != 0 \
8604 && BUFFERP (it->object) \
8605 && (IT_CHARPOS (*it) == to_charpos \
8606 || ((!it->bidi_p \
8607 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8608 && IT_CHARPOS (*it) > to_charpos) \
8609 || (it->what == IT_COMPOSITION \
8610 && ((IT_CHARPOS (*it) > to_charpos \
8611 && to_charpos >= it->cmp_it.charpos) \
8612 || (IT_CHARPOS (*it) < to_charpos \
8613 && to_charpos <= it->cmp_it.charpos)))) \
8614 && (it->method == GET_FROM_BUFFER \
8615 || (it->method == GET_FROM_DISPLAY_VECTOR \
8616 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8618 /* If there's a line-/wrap-prefix, handle it. */
8619 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8620 handle_line_prefix (it);
8622 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8623 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8625 while (true)
8627 int x, i, ascent = 0, descent = 0;
8629 /* Utility macro to reset an iterator with x, ascent, and descent. */
8630 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8631 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8632 (IT)->max_descent = descent)
8634 /* Stop if we move beyond TO_CHARPOS (after an image or a
8635 display string or stretch glyph). */
8636 if ((op & MOVE_TO_POS) != 0
8637 && BUFFERP (it->object)
8638 && it->method == GET_FROM_BUFFER
8639 && (((!it->bidi_p
8640 /* When the iterator is at base embedding level, we
8641 are guaranteed that characters are delivered for
8642 display in strictly increasing order of their
8643 buffer positions. */
8644 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8645 && IT_CHARPOS (*it) > to_charpos)
8646 || (it->bidi_p
8647 && (prev_method == GET_FROM_IMAGE
8648 || prev_method == GET_FROM_STRETCH
8649 || prev_method == GET_FROM_STRING)
8650 /* Passed TO_CHARPOS from left to right. */
8651 && ((prev_pos < to_charpos
8652 && IT_CHARPOS (*it) > to_charpos)
8653 /* Passed TO_CHARPOS from right to left. */
8654 || (prev_pos > to_charpos
8655 && IT_CHARPOS (*it) < to_charpos)))))
8657 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8659 result = MOVE_POS_MATCH_OR_ZV;
8660 break;
8662 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8663 /* If wrap_it is valid, the current position might be in a
8664 word that is wrapped. So, save the iterator in
8665 atpos_it and continue to see if wrapping happens. */
8666 SAVE_IT (atpos_it, *it, atpos_data);
8669 /* Stop when ZV reached.
8670 We used to stop here when TO_CHARPOS reached as well, but that is
8671 too soon if this glyph does not fit on this line. So we handle it
8672 explicitly below. */
8673 if (!get_next_display_element (it))
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 break;
8679 if (it->line_wrap == TRUNCATE)
8681 if (BUFFER_POS_REACHED_P ())
8683 result = MOVE_POS_MATCH_OR_ZV;
8684 break;
8687 else
8689 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8691 if (IT_DISPLAYING_WHITESPACE (it))
8692 may_wrap = true;
8693 else if (may_wrap)
8695 /* We have reached a glyph that follows one or more
8696 whitespace characters. If the position is
8697 already found, we are done. */
8698 if (atpos_it.sp >= 0)
8700 RESTORE_IT (it, &atpos_it, atpos_data);
8701 result = MOVE_POS_MATCH_OR_ZV;
8702 goto done;
8704 if (atx_it.sp >= 0)
8706 RESTORE_IT (it, &atx_it, atx_data);
8707 result = MOVE_X_REACHED;
8708 goto done;
8710 /* Otherwise, we can wrap here. */
8711 SAVE_IT (wrap_it, *it, wrap_data);
8712 may_wrap = false;
8717 /* Remember the line height for the current line, in case
8718 the next element doesn't fit on the line. */
8719 ascent = it->max_ascent;
8720 descent = it->max_descent;
8722 /* The call to produce_glyphs will get the metrics of the
8723 display element IT is loaded with. Record the x-position
8724 before this display element, in case it doesn't fit on the
8725 line. */
8726 x = it->current_x;
8728 PRODUCE_GLYPHS (it);
8730 if (it->area != TEXT_AREA)
8732 prev_method = it->method;
8733 if (it->method == GET_FROM_BUFFER)
8734 prev_pos = IT_CHARPOS (*it);
8735 set_iterator_to_next (it, true);
8736 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8737 SET_TEXT_POS (this_line_min_pos,
8738 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8739 if (it->bidi_p
8740 && (op & MOVE_TO_POS)
8741 && IT_CHARPOS (*it) > to_charpos
8742 && IT_CHARPOS (*it) < closest_pos)
8743 closest_pos = IT_CHARPOS (*it);
8744 continue;
8747 /* The number of glyphs we get back in IT->nglyphs will normally
8748 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8749 character on a terminal frame, or (iii) a line end. For the
8750 second case, IT->nglyphs - 1 padding glyphs will be present.
8751 (On X frames, there is only one glyph produced for a
8752 composite character.)
8754 The behavior implemented below means, for continuation lines,
8755 that as many spaces of a TAB as fit on the current line are
8756 displayed there. For terminal frames, as many glyphs of a
8757 multi-glyph character are displayed in the current line, too.
8758 This is what the old redisplay code did, and we keep it that
8759 way. Under X, the whole shape of a complex character must
8760 fit on the line or it will be completely displayed in the
8761 next line.
8763 Note that both for tabs and padding glyphs, all glyphs have
8764 the same width. */
8765 if (it->nglyphs)
8767 /* More than one glyph or glyph doesn't fit on line. All
8768 glyphs have the same width. */
8769 int single_glyph_width = it->pixel_width / it->nglyphs;
8770 int new_x;
8771 int x_before_this_char = x;
8772 int hpos_before_this_char = it->hpos;
8774 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8776 new_x = x + single_glyph_width;
8778 /* We want to leave anything reaching TO_X to the caller. */
8779 if ((op & MOVE_TO_X) && new_x > to_x)
8781 if (BUFFER_POS_REACHED_P ())
8783 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8784 goto buffer_pos_reached;
8785 if (atpos_it.sp < 0)
8787 SAVE_IT (atpos_it, *it, atpos_data);
8788 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8791 else
8793 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8795 it->current_x = x;
8796 result = MOVE_X_REACHED;
8797 break;
8799 if (atx_it.sp < 0)
8801 SAVE_IT (atx_it, *it, atx_data);
8802 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8807 if (/* Lines are continued. */
8808 it->line_wrap != TRUNCATE
8809 && (/* And glyph doesn't fit on the line. */
8810 new_x > it->last_visible_x
8811 /* Or it fits exactly and we're on a window
8812 system frame. */
8813 || (new_x == it->last_visible_x
8814 && FRAME_WINDOW_P (it->f)
8815 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8816 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8817 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8819 bool moved_forward = false;
8821 if (/* IT->hpos == 0 means the very first glyph
8822 doesn't fit on the line, e.g. a wide image. */
8823 it->hpos == 0
8824 || (new_x == it->last_visible_x
8825 && FRAME_WINDOW_P (it->f)))
8827 ++it->hpos;
8828 it->current_x = new_x;
8830 /* The character's last glyph just barely fits
8831 in this row. */
8832 if (i == it->nglyphs - 1)
8834 /* If this is the destination position,
8835 return a position *before* it in this row,
8836 now that we know it fits in this row. */
8837 if (BUFFER_POS_REACHED_P ())
8839 bool can_wrap = true;
8841 /* If we are at a whitespace character
8842 that barely fits on this screen line,
8843 but the next character is also
8844 whitespace, we cannot wrap here. */
8845 if (it->line_wrap == WORD_WRAP
8846 && wrap_it.sp >= 0
8847 && may_wrap
8848 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8850 struct it tem_it;
8851 void *tem_data = NULL;
8853 SAVE_IT (tem_it, *it, tem_data);
8854 set_iterator_to_next (it, true);
8855 if (get_next_display_element (it)
8856 && IT_DISPLAYING_WHITESPACE (it))
8857 can_wrap = false;
8858 RESTORE_IT (it, &tem_it, tem_data);
8860 if (it->line_wrap != WORD_WRAP
8861 || wrap_it.sp < 0
8862 /* If we've just found whitespace
8863 where we can wrap, effectively
8864 ignore the previous wrap point --
8865 it is no longer relevant, but we
8866 won't have an opportunity to
8867 update it, since we've reached
8868 the edge of this screen line. */
8869 || (may_wrap && can_wrap
8870 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8872 it->hpos = hpos_before_this_char;
8873 it->current_x = x_before_this_char;
8874 result = MOVE_POS_MATCH_OR_ZV;
8875 break;
8877 if (it->line_wrap == WORD_WRAP
8878 && atpos_it.sp < 0)
8880 SAVE_IT (atpos_it, *it, atpos_data);
8881 atpos_it.current_x = x_before_this_char;
8882 atpos_it.hpos = hpos_before_this_char;
8886 prev_method = it->method;
8887 if (it->method == GET_FROM_BUFFER)
8888 prev_pos = IT_CHARPOS (*it);
8889 set_iterator_to_next (it, true);
8890 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8891 SET_TEXT_POS (this_line_min_pos,
8892 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8893 /* On graphical terminals, newlines may
8894 "overflow" into the fringe if
8895 overflow-newline-into-fringe is non-nil.
8896 On text terminals, and on graphical
8897 terminals with no right margin, newlines
8898 may overflow into the last glyph on the
8899 display line.*/
8900 if (!FRAME_WINDOW_P (it->f)
8901 || ((it->bidi_p
8902 && it->bidi_it.paragraph_dir == R2L)
8903 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8904 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8905 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8907 if (!get_next_display_element (it))
8909 result = MOVE_POS_MATCH_OR_ZV;
8910 break;
8912 moved_forward = true;
8913 if (BUFFER_POS_REACHED_P ())
8915 if (ITERATOR_AT_END_OF_LINE_P (it))
8916 result = MOVE_POS_MATCH_OR_ZV;
8917 else
8918 result = MOVE_LINE_CONTINUED;
8919 break;
8921 if (ITERATOR_AT_END_OF_LINE_P (it)
8922 && (it->line_wrap != WORD_WRAP
8923 || wrap_it.sp < 0
8924 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8926 result = MOVE_NEWLINE_OR_CR;
8927 break;
8932 else
8933 IT_RESET_X_ASCENT_DESCENT (it);
8935 /* If the screen line ends with whitespace, and we
8936 are under word-wrap, don't use wrap_it: it is no
8937 longer relevant, but we won't have an opportunity
8938 to update it, since we are done with this screen
8939 line. */
8940 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8941 /* If the character after the one which set the
8942 may_wrap flag is also whitespace, we can't
8943 wrap here, since the screen line cannot be
8944 wrapped in the middle of whitespace.
8945 Therefore, wrap_it _is_ relevant in that
8946 case. */
8947 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8949 /* If we've found TO_X, go back there, as we now
8950 know the last word fits on this screen line. */
8951 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8952 && atx_it.sp >= 0)
8954 RESTORE_IT (it, &atx_it, atx_data);
8955 atpos_it.sp = -1;
8956 atx_it.sp = -1;
8957 result = MOVE_X_REACHED;
8958 break;
8961 else if (wrap_it.sp >= 0)
8963 RESTORE_IT (it, &wrap_it, wrap_data);
8964 atpos_it.sp = -1;
8965 atx_it.sp = -1;
8968 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8969 IT_CHARPOS (*it)));
8970 result = MOVE_LINE_CONTINUED;
8971 break;
8974 if (BUFFER_POS_REACHED_P ())
8976 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8977 goto buffer_pos_reached;
8978 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8980 SAVE_IT (atpos_it, *it, atpos_data);
8981 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8985 if (new_x > it->first_visible_x)
8987 /* Glyph is visible. Increment number of glyphs that
8988 would be displayed. */
8989 ++it->hpos;
8993 if (result != MOVE_UNDEFINED)
8994 break;
8996 else if (BUFFER_POS_REACHED_P ())
8998 buffer_pos_reached:
8999 IT_RESET_X_ASCENT_DESCENT (it);
9000 result = MOVE_POS_MATCH_OR_ZV;
9001 break;
9003 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9005 /* Stop when TO_X specified and reached. This check is
9006 necessary here because of lines consisting of a line end,
9007 only. The line end will not produce any glyphs and we
9008 would never get MOVE_X_REACHED. */
9009 eassert (it->nglyphs == 0);
9010 result = MOVE_X_REACHED;
9011 break;
9014 /* Is this a line end? If yes, we're done. */
9015 if (ITERATOR_AT_END_OF_LINE_P (it))
9017 /* If we are past TO_CHARPOS, but never saw any character
9018 positions smaller than TO_CHARPOS, return
9019 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9020 did. */
9021 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9023 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9025 if (closest_pos < ZV)
9027 RESTORE_IT (it, &ppos_it, ppos_data);
9028 /* Don't recurse if closest_pos is equal to
9029 to_charpos, since we have just tried that. */
9030 if (closest_pos != to_charpos)
9031 move_it_in_display_line_to (it, closest_pos, -1,
9032 MOVE_TO_POS);
9033 result = MOVE_POS_MATCH_OR_ZV;
9035 else
9036 goto buffer_pos_reached;
9038 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9039 && IT_CHARPOS (*it) > to_charpos)
9040 goto buffer_pos_reached;
9041 else
9042 result = MOVE_NEWLINE_OR_CR;
9044 else
9045 result = MOVE_NEWLINE_OR_CR;
9046 /* If we've processed the newline, make sure this flag is
9047 reset, as it must only be set when the newline itself is
9048 processed. */
9049 if (result == MOVE_NEWLINE_OR_CR)
9050 it->constrain_row_ascent_descent_p = false;
9051 break;
9054 prev_method = it->method;
9055 if (it->method == GET_FROM_BUFFER)
9056 prev_pos = IT_CHARPOS (*it);
9057 /* The current display element has been consumed. Advance
9058 to the next. */
9059 set_iterator_to_next (it, true);
9060 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9061 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9062 if (IT_CHARPOS (*it) < to_charpos)
9063 saw_smaller_pos = true;
9064 if (it->bidi_p
9065 && (op & MOVE_TO_POS)
9066 && IT_CHARPOS (*it) >= to_charpos
9067 && IT_CHARPOS (*it) < closest_pos)
9068 closest_pos = IT_CHARPOS (*it);
9070 /* Stop if lines are truncated and IT's current x-position is
9071 past the right edge of the window now. */
9072 if (it->line_wrap == TRUNCATE
9073 && it->current_x >= it->last_visible_x)
9075 if (!FRAME_WINDOW_P (it->f)
9076 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9077 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9078 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9079 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9081 bool at_eob_p = false;
9083 if ((at_eob_p = !get_next_display_element (it))
9084 || BUFFER_POS_REACHED_P ()
9085 /* If we are past TO_CHARPOS, but never saw any
9086 character positions smaller than TO_CHARPOS,
9087 return MOVE_POS_MATCH_OR_ZV, like the
9088 unidirectional display did. */
9089 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9090 && !saw_smaller_pos
9091 && IT_CHARPOS (*it) > to_charpos))
9093 if (it->bidi_p
9094 && !BUFFER_POS_REACHED_P ()
9095 && !at_eob_p && closest_pos < ZV)
9097 RESTORE_IT (it, &ppos_it, ppos_data);
9098 if (closest_pos != to_charpos)
9099 move_it_in_display_line_to (it, closest_pos, -1,
9100 MOVE_TO_POS);
9102 result = MOVE_POS_MATCH_OR_ZV;
9103 break;
9105 if (ITERATOR_AT_END_OF_LINE_P (it))
9107 result = MOVE_NEWLINE_OR_CR;
9108 break;
9111 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9112 && !saw_smaller_pos
9113 && IT_CHARPOS (*it) > to_charpos)
9115 if (closest_pos < ZV)
9117 RESTORE_IT (it, &ppos_it, ppos_data);
9118 if (closest_pos != to_charpos)
9119 move_it_in_display_line_to (it, closest_pos, -1,
9120 MOVE_TO_POS);
9122 result = MOVE_POS_MATCH_OR_ZV;
9123 break;
9125 result = MOVE_LINE_TRUNCATED;
9126 break;
9128 #undef IT_RESET_X_ASCENT_DESCENT
9131 #undef BUFFER_POS_REACHED_P
9133 /* If we scanned beyond TO_POS, restore the saved iterator either to
9134 the wrap point (if found), or to atpos/atx location. We decide which
9135 data to use to restore the saved iterator state by their X coordinates,
9136 since buffer positions might increase non-monotonically with screen
9137 coordinates due to bidi reordering. */
9138 if (result == MOVE_LINE_CONTINUED
9139 && it->line_wrap == WORD_WRAP
9140 && wrap_it.sp >= 0
9141 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9142 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9143 RESTORE_IT (it, &wrap_it, wrap_data);
9144 else if (atpos_it.sp >= 0)
9145 RESTORE_IT (it, &atpos_it, atpos_data);
9146 else if (atx_it.sp >= 0)
9147 RESTORE_IT (it, &atx_it, atx_data);
9149 done:
9151 if (atpos_data)
9152 bidi_unshelve_cache (atpos_data, true);
9153 if (atx_data)
9154 bidi_unshelve_cache (atx_data, true);
9155 if (wrap_data)
9156 bidi_unshelve_cache (wrap_data, true);
9157 if (ppos_data)
9158 bidi_unshelve_cache (ppos_data, true);
9160 /* Restore the iterator settings altered at the beginning of this
9161 function. */
9162 it->glyph_row = saved_glyph_row;
9163 return result;
9166 /* For external use. */
9167 void
9168 move_it_in_display_line (struct it *it,
9169 ptrdiff_t to_charpos, int to_x,
9170 enum move_operation_enum op)
9172 if (it->line_wrap == WORD_WRAP
9173 && (op & MOVE_TO_X))
9175 struct it save_it;
9176 void *save_data = NULL;
9177 int skip;
9179 SAVE_IT (save_it, *it, save_data);
9180 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9181 /* When word-wrap is on, TO_X may lie past the end
9182 of a wrapped line. Then it->current is the
9183 character on the next line, so backtrack to the
9184 space before the wrap point. */
9185 if (skip == MOVE_LINE_CONTINUED)
9187 int prev_x = max (it->current_x - 1, 0);
9188 RESTORE_IT (it, &save_it, save_data);
9189 move_it_in_display_line_to
9190 (it, -1, prev_x, MOVE_TO_X);
9192 else
9193 bidi_unshelve_cache (save_data, true);
9195 else
9196 move_it_in_display_line_to (it, to_charpos, to_x, op);
9200 /* Move IT forward until it satisfies one or more of the criteria in
9201 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9203 OP is a bit-mask that specifies where to stop, and in particular,
9204 which of those four position arguments makes a difference. See the
9205 description of enum move_operation_enum.
9207 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9208 screen line, this function will set IT to the next position that is
9209 displayed to the right of TO_CHARPOS on the screen.
9211 Return the maximum pixel length of any line scanned but never more
9212 than it.last_visible_x. */
9215 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9217 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9218 int line_height, line_start_x = 0, reached = 0;
9219 int max_current_x = 0;
9220 void *backup_data = NULL;
9222 for (;;)
9224 if (op & MOVE_TO_VPOS)
9226 /* If no TO_CHARPOS and no TO_X specified, stop at the
9227 start of the line TO_VPOS. */
9228 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9230 if (it->vpos == to_vpos)
9232 reached = 1;
9233 break;
9235 else
9236 skip = move_it_in_display_line_to (it, -1, -1, 0);
9238 else
9240 /* TO_VPOS >= 0 means stop at TO_X in the line at
9241 TO_VPOS, or at TO_POS, whichever comes first. */
9242 if (it->vpos == to_vpos)
9244 reached = 2;
9245 break;
9248 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9250 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9252 reached = 3;
9253 break;
9255 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9257 /* We have reached TO_X but not in the line we want. */
9258 skip = move_it_in_display_line_to (it, to_charpos,
9259 -1, MOVE_TO_POS);
9260 if (skip == MOVE_POS_MATCH_OR_ZV)
9262 reached = 4;
9263 break;
9268 else if (op & MOVE_TO_Y)
9270 struct it it_backup;
9272 if (it->line_wrap == WORD_WRAP)
9273 SAVE_IT (it_backup, *it, backup_data);
9275 /* TO_Y specified means stop at TO_X in the line containing
9276 TO_Y---or at TO_CHARPOS if this is reached first. The
9277 problem is that we can't really tell whether the line
9278 contains TO_Y before we have completely scanned it, and
9279 this may skip past TO_X. What we do is to first scan to
9280 TO_X.
9282 If TO_X is not specified, use a TO_X of zero. The reason
9283 is to make the outcome of this function more predictable.
9284 If we didn't use TO_X == 0, we would stop at the end of
9285 the line which is probably not what a caller would expect
9286 to happen. */
9287 skip = move_it_in_display_line_to
9288 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9289 (MOVE_TO_X | (op & MOVE_TO_POS)));
9291 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9292 if (skip == MOVE_POS_MATCH_OR_ZV)
9293 reached = 5;
9294 else if (skip == MOVE_X_REACHED)
9296 /* If TO_X was reached, we want to know whether TO_Y is
9297 in the line. We know this is the case if the already
9298 scanned glyphs make the line tall enough. Otherwise,
9299 we must check by scanning the rest of the line. */
9300 line_height = it->max_ascent + it->max_descent;
9301 if (to_y >= it->current_y
9302 && to_y < it->current_y + line_height)
9304 reached = 6;
9305 break;
9307 SAVE_IT (it_backup, *it, backup_data);
9308 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9309 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9310 op & MOVE_TO_POS);
9311 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9312 line_height = it->max_ascent + it->max_descent;
9313 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9315 if (to_y >= it->current_y
9316 && to_y < it->current_y + line_height)
9318 /* If TO_Y is in this line and TO_X was reached
9319 above, we scanned too far. We have to restore
9320 IT's settings to the ones before skipping. But
9321 keep the more accurate values of max_ascent and
9322 max_descent we've found while skipping the rest
9323 of the line, for the sake of callers, such as
9324 pos_visible_p, that need to know the line
9325 height. */
9326 int max_ascent = it->max_ascent;
9327 int max_descent = it->max_descent;
9329 RESTORE_IT (it, &it_backup, backup_data);
9330 it->max_ascent = max_ascent;
9331 it->max_descent = max_descent;
9332 reached = 6;
9334 else
9336 skip = skip2;
9337 if (skip == MOVE_POS_MATCH_OR_ZV)
9338 reached = 7;
9341 else
9343 /* Check whether TO_Y is in this line. */
9344 line_height = it->max_ascent + it->max_descent;
9345 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9347 if (to_y >= it->current_y
9348 && to_y < it->current_y + line_height)
9350 if (to_y > it->current_y)
9351 max_current_x = max (it->current_x, max_current_x);
9353 /* When word-wrap is on, TO_X may lie past the end
9354 of a wrapped line. Then it->current is the
9355 character on the next line, so backtrack to the
9356 space before the wrap point. */
9357 if (skip == MOVE_LINE_CONTINUED
9358 && it->line_wrap == WORD_WRAP)
9360 int prev_x = max (it->current_x - 1, 0);
9361 RESTORE_IT (it, &it_backup, backup_data);
9362 skip = move_it_in_display_line_to
9363 (it, -1, prev_x, MOVE_TO_X);
9366 reached = 6;
9370 if (reached)
9372 max_current_x = max (it->current_x, max_current_x);
9373 break;
9376 else if (BUFFERP (it->object)
9377 && (it->method == GET_FROM_BUFFER
9378 || it->method == GET_FROM_STRETCH)
9379 && IT_CHARPOS (*it) >= to_charpos
9380 /* Under bidi iteration, a call to set_iterator_to_next
9381 can scan far beyond to_charpos if the initial
9382 portion of the next line needs to be reordered. In
9383 that case, give move_it_in_display_line_to another
9384 chance below. */
9385 && !(it->bidi_p
9386 && it->bidi_it.scan_dir == -1))
9387 skip = MOVE_POS_MATCH_OR_ZV;
9388 else
9389 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9391 switch (skip)
9393 case MOVE_POS_MATCH_OR_ZV:
9394 max_current_x = max (it->current_x, max_current_x);
9395 reached = 8;
9396 goto out;
9398 case MOVE_NEWLINE_OR_CR:
9399 max_current_x = max (it->current_x, max_current_x);
9400 set_iterator_to_next (it, true);
9401 it->continuation_lines_width = 0;
9402 break;
9404 case MOVE_LINE_TRUNCATED:
9405 max_current_x = it->last_visible_x;
9406 it->continuation_lines_width = 0;
9407 reseat_at_next_visible_line_start (it, false);
9408 if ((op & MOVE_TO_POS) != 0
9409 && IT_CHARPOS (*it) > to_charpos)
9411 reached = 9;
9412 goto out;
9414 break;
9416 case MOVE_LINE_CONTINUED:
9417 max_current_x = it->last_visible_x;
9418 /* For continued lines ending in a tab, some of the glyphs
9419 associated with the tab are displayed on the current
9420 line. Since it->current_x does not include these glyphs,
9421 we use it->last_visible_x instead. */
9422 if (it->c == '\t')
9424 it->continuation_lines_width += it->last_visible_x;
9425 /* When moving by vpos, ensure that the iterator really
9426 advances to the next line (bug#847, bug#969). Fixme:
9427 do we need to do this in other circumstances? */
9428 if (it->current_x != it->last_visible_x
9429 && (op & MOVE_TO_VPOS)
9430 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9432 line_start_x = it->current_x + it->pixel_width
9433 - it->last_visible_x;
9434 if (FRAME_WINDOW_P (it->f))
9436 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9437 struct font *face_font = face->font;
9439 /* When display_line produces a continued line
9440 that ends in a TAB, it skips a tab stop that
9441 is closer than the font's space character
9442 width (see x_produce_glyphs where it produces
9443 the stretch glyph which represents a TAB).
9444 We need to reproduce the same logic here. */
9445 eassert (face_font);
9446 if (face_font)
9448 if (line_start_x < face_font->space_width)
9449 line_start_x
9450 += it->tab_width * face_font->space_width;
9453 set_iterator_to_next (it, false);
9456 else
9457 it->continuation_lines_width += it->current_x;
9458 break;
9460 default:
9461 emacs_abort ();
9464 /* Reset/increment for the next run. */
9465 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9466 it->current_x = line_start_x;
9467 line_start_x = 0;
9468 it->hpos = 0;
9469 it->current_y += it->max_ascent + it->max_descent;
9470 ++it->vpos;
9471 last_height = it->max_ascent + it->max_descent;
9472 it->max_ascent = it->max_descent = 0;
9475 out:
9477 /* On text terminals, we may stop at the end of a line in the middle
9478 of a multi-character glyph. If the glyph itself is continued,
9479 i.e. it is actually displayed on the next line, don't treat this
9480 stopping point as valid; move to the next line instead (unless
9481 that brings us offscreen). */
9482 if (!FRAME_WINDOW_P (it->f)
9483 && op & MOVE_TO_POS
9484 && IT_CHARPOS (*it) == to_charpos
9485 && it->what == IT_CHARACTER
9486 && it->nglyphs > 1
9487 && it->line_wrap == WINDOW_WRAP
9488 && it->current_x == it->last_visible_x - 1
9489 && it->c != '\n'
9490 && it->c != '\t'
9491 && it->w->window_end_valid
9492 && it->vpos < it->w->window_end_vpos)
9494 it->continuation_lines_width += it->current_x;
9495 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9496 it->current_y += it->max_ascent + it->max_descent;
9497 ++it->vpos;
9498 last_height = it->max_ascent + it->max_descent;
9501 if (backup_data)
9502 bidi_unshelve_cache (backup_data, true);
9504 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9506 return max_current_x;
9510 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9512 If DY > 0, move IT backward at least that many pixels. DY = 0
9513 means move IT backward to the preceding line start or BEGV. This
9514 function may move over more than DY pixels if IT->current_y - DY
9515 ends up in the middle of a line; in this case IT->current_y will be
9516 set to the top of the line moved to. */
9518 void
9519 move_it_vertically_backward (struct it *it, int dy)
9521 int nlines, h;
9522 struct it it2, it3;
9523 void *it2data = NULL, *it3data = NULL;
9524 ptrdiff_t start_pos;
9525 int nchars_per_row
9526 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9527 ptrdiff_t pos_limit;
9529 move_further_back:
9530 eassert (dy >= 0);
9532 start_pos = IT_CHARPOS (*it);
9534 /* Estimate how many newlines we must move back. */
9535 nlines = max (1, dy / default_line_pixel_height (it->w));
9536 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9537 pos_limit = BEGV;
9538 else
9539 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9541 /* Set the iterator's position that many lines back. But don't go
9542 back more than NLINES full screen lines -- this wins a day with
9543 buffers which have very long lines. */
9544 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9545 back_to_previous_visible_line_start (it);
9547 /* Reseat the iterator here. When moving backward, we don't want
9548 reseat to skip forward over invisible text, set up the iterator
9549 to deliver from overlay strings at the new position etc. So,
9550 use reseat_1 here. */
9551 reseat_1 (it, it->current.pos, true);
9553 /* We are now surely at a line start. */
9554 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9555 reordering is in effect. */
9556 it->continuation_lines_width = 0;
9558 /* Move forward and see what y-distance we moved. First move to the
9559 start of the next line so that we get its height. We need this
9560 height to be able to tell whether we reached the specified
9561 y-distance. */
9562 SAVE_IT (it2, *it, it2data);
9563 it2.max_ascent = it2.max_descent = 0;
9566 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9567 MOVE_TO_POS | MOVE_TO_VPOS);
9569 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9570 /* If we are in a display string which starts at START_POS,
9571 and that display string includes a newline, and we are
9572 right after that newline (i.e. at the beginning of a
9573 display line), exit the loop, because otherwise we will
9574 infloop, since move_it_to will see that it is already at
9575 START_POS and will not move. */
9576 || (it2.method == GET_FROM_STRING
9577 && IT_CHARPOS (it2) == start_pos
9578 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9579 eassert (IT_CHARPOS (*it) >= BEGV);
9580 SAVE_IT (it3, it2, it3data);
9582 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9583 eassert (IT_CHARPOS (*it) >= BEGV);
9584 /* H is the actual vertical distance from the position in *IT
9585 and the starting position. */
9586 h = it2.current_y - it->current_y;
9587 /* NLINES is the distance in number of lines. */
9588 nlines = it2.vpos - it->vpos;
9590 /* Correct IT's y and vpos position
9591 so that they are relative to the starting point. */
9592 it->vpos -= nlines;
9593 it->current_y -= h;
9595 if (dy == 0)
9597 /* DY == 0 means move to the start of the screen line. The
9598 value of nlines is > 0 if continuation lines were involved,
9599 or if the original IT position was at start of a line. */
9600 RESTORE_IT (it, it, it2data);
9601 if (nlines > 0)
9602 move_it_by_lines (it, nlines);
9603 /* The above code moves us to some position NLINES down,
9604 usually to its first glyph (leftmost in an L2R line), but
9605 that's not necessarily the start of the line, under bidi
9606 reordering. We want to get to the character position
9607 that is immediately after the newline of the previous
9608 line. */
9609 if (it->bidi_p
9610 && !it->continuation_lines_width
9611 && !STRINGP (it->string)
9612 && IT_CHARPOS (*it) > BEGV
9613 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9615 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9617 DEC_BOTH (cp, bp);
9618 cp = find_newline_no_quit (cp, bp, -1, NULL);
9619 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9621 bidi_unshelve_cache (it3data, true);
9623 else
9625 /* The y-position we try to reach, relative to *IT.
9626 Note that H has been subtracted in front of the if-statement. */
9627 int target_y = it->current_y + h - dy;
9628 int y0 = it3.current_y;
9629 int y1;
9630 int line_height;
9632 RESTORE_IT (&it3, &it3, it3data);
9633 y1 = line_bottom_y (&it3);
9634 line_height = y1 - y0;
9635 RESTORE_IT (it, it, it2data);
9636 /* If we did not reach target_y, try to move further backward if
9637 we can. If we moved too far backward, try to move forward. */
9638 if (target_y < it->current_y
9639 /* This is heuristic. In a window that's 3 lines high, with
9640 a line height of 13 pixels each, recentering with point
9641 on the bottom line will try to move -39/2 = 19 pixels
9642 backward. Try to avoid moving into the first line. */
9643 && (it->current_y - target_y
9644 > min (window_box_height (it->w), line_height * 2 / 3))
9645 && IT_CHARPOS (*it) > BEGV)
9647 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9648 target_y - it->current_y));
9649 dy = it->current_y - target_y;
9650 goto move_further_back;
9652 else if (target_y >= it->current_y + line_height
9653 && IT_CHARPOS (*it) < ZV)
9655 /* Should move forward by at least one line, maybe more.
9657 Note: Calling move_it_by_lines can be expensive on
9658 terminal frames, where compute_motion is used (via
9659 vmotion) to do the job, when there are very long lines
9660 and truncate-lines is nil. That's the reason for
9661 treating terminal frames specially here. */
9663 if (!FRAME_WINDOW_P (it->f))
9664 move_it_vertically (it, target_y - it->current_y);
9665 else
9669 move_it_by_lines (it, 1);
9671 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9678 /* Move IT by a specified amount of pixel lines DY. DY negative means
9679 move backwards. DY = 0 means move to start of screen line. At the
9680 end, IT will be on the start of a screen line. */
9682 void
9683 move_it_vertically (struct it *it, int dy)
9685 if (dy <= 0)
9686 move_it_vertically_backward (it, -dy);
9687 else
9689 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9690 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9691 MOVE_TO_POS | MOVE_TO_Y);
9692 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9694 /* If buffer ends in ZV without a newline, move to the start of
9695 the line to satisfy the post-condition. */
9696 if (IT_CHARPOS (*it) == ZV
9697 && ZV > BEGV
9698 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9699 move_it_by_lines (it, 0);
9704 /* Move iterator IT past the end of the text line it is in. */
9706 void
9707 move_it_past_eol (struct it *it)
9709 enum move_it_result rc;
9711 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9712 if (rc == MOVE_NEWLINE_OR_CR)
9713 set_iterator_to_next (it, false);
9717 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9718 negative means move up. DVPOS == 0 means move to the start of the
9719 screen line.
9721 Optimization idea: If we would know that IT->f doesn't use
9722 a face with proportional font, we could be faster for
9723 truncate-lines nil. */
9725 void
9726 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9729 /* The commented-out optimization uses vmotion on terminals. This
9730 gives bad results, because elements like it->what, on which
9731 callers such as pos_visible_p rely, aren't updated. */
9732 /* struct position pos;
9733 if (!FRAME_WINDOW_P (it->f))
9735 struct text_pos textpos;
9737 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9738 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9739 reseat (it, textpos, true);
9740 it->vpos += pos.vpos;
9741 it->current_y += pos.vpos;
9743 else */
9745 if (dvpos == 0)
9747 /* DVPOS == 0 means move to the start of the screen line. */
9748 move_it_vertically_backward (it, 0);
9749 /* Let next call to line_bottom_y calculate real line height. */
9750 last_height = 0;
9752 else if (dvpos > 0)
9754 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9755 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9757 /* Only move to the next buffer position if we ended up in a
9758 string from display property, not in an overlay string
9759 (before-string or after-string). That is because the
9760 latter don't conceal the underlying buffer position, so
9761 we can ask to move the iterator to the exact position we
9762 are interested in. Note that, even if we are already at
9763 IT_CHARPOS (*it), the call below is not a no-op, as it
9764 will detect that we are at the end of the string, pop the
9765 iterator, and compute it->current_x and it->hpos
9766 correctly. */
9767 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9768 -1, -1, -1, MOVE_TO_POS);
9771 else
9773 struct it it2;
9774 void *it2data = NULL;
9775 ptrdiff_t start_charpos, i;
9776 int nchars_per_row
9777 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9778 bool hit_pos_limit = false;
9779 ptrdiff_t pos_limit;
9781 /* Start at the beginning of the screen line containing IT's
9782 position. This may actually move vertically backwards,
9783 in case of overlays, so adjust dvpos accordingly. */
9784 dvpos += it->vpos;
9785 move_it_vertically_backward (it, 0);
9786 dvpos -= it->vpos;
9788 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9789 screen lines, and reseat the iterator there. */
9790 start_charpos = IT_CHARPOS (*it);
9791 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9792 pos_limit = BEGV;
9793 else
9794 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9796 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9797 back_to_previous_visible_line_start (it);
9798 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9799 hit_pos_limit = true;
9800 reseat (it, it->current.pos, true);
9802 /* Move further back if we end up in a string or an image. */
9803 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9805 /* First try to move to start of display line. */
9806 dvpos += it->vpos;
9807 move_it_vertically_backward (it, 0);
9808 dvpos -= it->vpos;
9809 if (IT_POS_VALID_AFTER_MOVE_P (it))
9810 break;
9811 /* If start of line is still in string or image,
9812 move further back. */
9813 back_to_previous_visible_line_start (it);
9814 reseat (it, it->current.pos, true);
9815 dvpos--;
9818 it->current_x = it->hpos = 0;
9820 /* Above call may have moved too far if continuation lines
9821 are involved. Scan forward and see if it did. */
9822 SAVE_IT (it2, *it, it2data);
9823 it2.vpos = it2.current_y = 0;
9824 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9825 it->vpos -= it2.vpos;
9826 it->current_y -= it2.current_y;
9827 it->current_x = it->hpos = 0;
9829 /* If we moved too far back, move IT some lines forward. */
9830 if (it2.vpos > -dvpos)
9832 int delta = it2.vpos + dvpos;
9834 RESTORE_IT (&it2, &it2, it2data);
9835 SAVE_IT (it2, *it, it2data);
9836 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9837 /* Move back again if we got too far ahead. */
9838 if (IT_CHARPOS (*it) >= start_charpos)
9839 RESTORE_IT (it, &it2, it2data);
9840 else
9841 bidi_unshelve_cache (it2data, true);
9843 else if (hit_pos_limit && pos_limit > BEGV
9844 && dvpos < 0 && it2.vpos < -dvpos)
9846 /* If we hit the limit, but still didn't make it far enough
9847 back, that means there's a display string with a newline
9848 covering a large chunk of text, and that caused
9849 back_to_previous_visible_line_start try to go too far.
9850 Punish those who commit such atrocities by going back
9851 until we've reached DVPOS, after lifting the limit, which
9852 could make it slow for very long lines. "If it hurts,
9853 don't do that!" */
9854 dvpos += it2.vpos;
9855 RESTORE_IT (it, it, it2data);
9856 for (i = -dvpos; i > 0; --i)
9858 back_to_previous_visible_line_start (it);
9859 it->vpos--;
9861 reseat_1 (it, it->current.pos, true);
9863 else
9864 RESTORE_IT (it, it, it2data);
9868 /* Return true if IT points into the middle of a display vector. */
9870 bool
9871 in_display_vector_p (struct it *it)
9873 return (it->method == GET_FROM_DISPLAY_VECTOR
9874 && it->current.dpvec_index > 0
9875 && it->dpvec + it->current.dpvec_index != it->dpend);
9878 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9879 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9880 WINDOW must be a live window and defaults to the selected one. The
9881 return value is a cons of the maximum pixel-width of any text line and
9882 the maximum pixel-height of all text lines.
9884 The optional argument FROM, if non-nil, specifies the first text
9885 position and defaults to the minimum accessible position of the buffer.
9886 If FROM is t, use the minimum accessible position that starts a
9887 non-empty line. TO, if non-nil, specifies the last text position and
9888 defaults to the maximum accessible position of the buffer. If TO is t,
9889 use the maximum accessible position that ends a non-empty line.
9891 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9892 width that can be returned. X-LIMIT nil or omitted, means to use the
9893 pixel-width of WINDOW's body; use this if you want to know how high
9894 WINDOW should be become in order to fit all of its buffer's text with
9895 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9896 if you intend to change WINDOW's width. In any case, text whose
9897 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9898 of long lines can take some time, it's always a good idea to make this
9899 argument as small as possible; in particular, if the buffer contains
9900 long lines that shall be truncated anyway.
9902 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9903 height (excluding the height of the mode- or header-line, if any) that
9904 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9905 ignored. Since calculating the text height of a large buffer can take
9906 some time, it makes sense to specify this argument if the size of the
9907 buffer is large or unknown.
9909 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9910 include the height of the mode- or header-line of WINDOW in the return
9911 value. If it is either the symbol `mode-line' or `header-line', include
9912 only the height of that line, if present, in the return value. If t,
9913 include the height of both, if present, in the return value. */)
9914 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9915 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9917 struct window *w = decode_live_window (window);
9918 Lisp_Object buffer = w->contents;
9919 struct buffer *b;
9920 struct it it;
9921 struct buffer *old_b = NULL;
9922 ptrdiff_t start, end, pos;
9923 struct text_pos startp;
9924 void *itdata = NULL;
9925 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9927 CHECK_BUFFER (buffer);
9928 b = XBUFFER (buffer);
9930 if (b != current_buffer)
9932 old_b = current_buffer;
9933 set_buffer_internal (b);
9936 if (NILP (from))
9937 start = BEGV;
9938 else if (EQ (from, Qt))
9940 start = pos = BEGV;
9941 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9942 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9943 start = pos;
9944 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9945 start = pos;
9947 else
9949 CHECK_NUMBER_COERCE_MARKER (from);
9950 start = min (max (XINT (from), BEGV), ZV);
9953 if (NILP (to))
9954 end = ZV;
9955 else if (EQ (to, Qt))
9957 end = pos = ZV;
9958 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9959 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9960 end = pos;
9961 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9962 end = pos;
9964 else
9966 CHECK_NUMBER_COERCE_MARKER (to);
9967 end = max (start, min (XINT (to), ZV));
9970 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
9971 max_x = XINT (x_limit);
9973 if (NILP (y_limit))
9974 max_y = INT_MAX;
9975 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
9976 max_y = XINT (y_limit);
9978 itdata = bidi_shelve_cache ();
9979 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9980 start_display (&it, w, startp);
9982 if (NILP (x_limit))
9983 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9984 else
9986 it.last_visible_x = max_x;
9987 /* Actually, we never want move_it_to stop at to_x. But to make
9988 sure that move_it_in_display_line_to always moves far enough,
9989 we set it to INT_MAX and specify MOVE_TO_X. */
9990 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9991 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9992 /* Don't return more than X-LIMIT. */
9993 if (x > max_x)
9994 x = max_x;
9997 /* Subtract height of header-line which was counted automatically by
9998 start_display. */
9999 y = it.current_y + it.max_ascent + it.max_descent
10000 - WINDOW_HEADER_LINE_HEIGHT (w);
10001 /* Don't return more than Y-LIMIT. */
10002 if (y > max_y)
10003 y = max_y;
10005 if (EQ (mode_and_header_line, Qheader_line)
10006 || EQ (mode_and_header_line, Qt))
10007 /* Re-add height of header-line as requested. */
10008 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10010 if (EQ (mode_and_header_line, Qmode_line)
10011 || EQ (mode_and_header_line, Qt))
10012 /* Add height of mode-line as requested. */
10013 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10015 bidi_unshelve_cache (itdata, false);
10017 if (old_b)
10018 set_buffer_internal (old_b);
10020 return Fcons (make_number (x), make_number (y));
10023 /***********************************************************************
10024 Messages
10025 ***********************************************************************/
10027 /* Return the number of arguments the format string FORMAT needs. */
10029 static ptrdiff_t
10030 format_nargs (char const *format)
10032 ptrdiff_t nargs = 0;
10033 for (char const *p = format; (p = strchr (p, '%')); p++)
10034 if (p[1] == '%')
10035 p++;
10036 else
10037 nargs++;
10038 return nargs;
10041 /* Add a message with format string FORMAT and formatted arguments
10042 to *Messages*. */
10044 void
10045 add_to_log (const char *format, ...)
10047 va_list ap;
10048 va_start (ap, format);
10049 vadd_to_log (format, ap);
10050 va_end (ap);
10053 void
10054 vadd_to_log (char const *format, va_list ap)
10056 ptrdiff_t form_nargs = format_nargs (format);
10057 ptrdiff_t nargs = 1 + form_nargs;
10058 Lisp_Object args[10];
10059 eassert (nargs <= ARRAYELTS (args));
10060 AUTO_STRING (args0, format);
10061 args[0] = args0;
10062 for (ptrdiff_t i = 1; i <= nargs; i++)
10063 args[i] = va_arg (ap, Lisp_Object);
10064 Lisp_Object msg = Qnil;
10065 msg = Fformat_message (nargs, args);
10067 ptrdiff_t len = SBYTES (msg) + 1;
10068 USE_SAFE_ALLOCA;
10069 char *buffer = SAFE_ALLOCA (len);
10070 memcpy (buffer, SDATA (msg), len);
10072 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10073 SAFE_FREE ();
10077 /* Output a newline in the *Messages* buffer if "needs" one. */
10079 void
10080 message_log_maybe_newline (void)
10082 if (message_log_need_newline)
10083 message_dolog ("", 0, true, false);
10087 /* Add a string M of length NBYTES to the message log, optionally
10088 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10089 true, means interpret the contents of M as multibyte. This
10090 function calls low-level routines in order to bypass text property
10091 hooks, etc. which might not be safe to run.
10093 This may GC (insert may run before/after change hooks),
10094 so the buffer M must NOT point to a Lisp string. */
10096 void
10097 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10099 const unsigned char *msg = (const unsigned char *) m;
10101 if (!NILP (Vmemory_full))
10102 return;
10104 if (!NILP (Vmessage_log_max))
10106 struct buffer *oldbuf;
10107 Lisp_Object oldpoint, oldbegv, oldzv;
10108 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10109 ptrdiff_t point_at_end = 0;
10110 ptrdiff_t zv_at_end = 0;
10111 Lisp_Object old_deactivate_mark;
10113 old_deactivate_mark = Vdeactivate_mark;
10114 oldbuf = current_buffer;
10116 /* Ensure the Messages buffer exists, and switch to it.
10117 If we created it, set the major-mode. */
10118 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10119 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10120 if (newbuffer
10121 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10122 call0 (intern ("messages-buffer-mode"));
10124 bset_undo_list (current_buffer, Qt);
10125 bset_cache_long_scans (current_buffer, Qnil);
10127 oldpoint = message_dolog_marker1;
10128 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10129 oldbegv = message_dolog_marker2;
10130 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10131 oldzv = message_dolog_marker3;
10132 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10134 if (PT == Z)
10135 point_at_end = 1;
10136 if (ZV == Z)
10137 zv_at_end = 1;
10139 BEGV = BEG;
10140 BEGV_BYTE = BEG_BYTE;
10141 ZV = Z;
10142 ZV_BYTE = Z_BYTE;
10143 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10145 /* Insert the string--maybe converting multibyte to single byte
10146 or vice versa, so that all the text fits the buffer. */
10147 if (multibyte
10148 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10150 ptrdiff_t i;
10151 int c, char_bytes;
10152 char work[1];
10154 /* Convert a multibyte string to single-byte
10155 for the *Message* buffer. */
10156 for (i = 0; i < nbytes; i += char_bytes)
10158 c = string_char_and_length (msg + i, &char_bytes);
10159 work[0] = CHAR_TO_BYTE8 (c);
10160 insert_1_both (work, 1, 1, true, false, false);
10163 else if (! multibyte
10164 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10166 ptrdiff_t i;
10167 int c, char_bytes;
10168 unsigned char str[MAX_MULTIBYTE_LENGTH];
10169 /* Convert a single-byte string to multibyte
10170 for the *Message* buffer. */
10171 for (i = 0; i < nbytes; i++)
10173 c = msg[i];
10174 MAKE_CHAR_MULTIBYTE (c);
10175 char_bytes = CHAR_STRING (c, str);
10176 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10179 else if (nbytes)
10180 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10181 true, false, false);
10183 if (nlflag)
10185 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10186 printmax_t dups;
10188 insert_1_both ("\n", 1, 1, true, false, false);
10190 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10191 this_bol = PT;
10192 this_bol_byte = PT_BYTE;
10194 /* See if this line duplicates the previous one.
10195 If so, combine duplicates. */
10196 if (this_bol > BEG)
10198 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10199 prev_bol = PT;
10200 prev_bol_byte = PT_BYTE;
10202 dups = message_log_check_duplicate (prev_bol_byte,
10203 this_bol_byte);
10204 if (dups)
10206 del_range_both (prev_bol, prev_bol_byte,
10207 this_bol, this_bol_byte, false);
10208 if (dups > 1)
10210 char dupstr[sizeof " [ times]"
10211 + INT_STRLEN_BOUND (printmax_t)];
10213 /* If you change this format, don't forget to also
10214 change message_log_check_duplicate. */
10215 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10216 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10217 insert_1_both (dupstr, duplen, duplen,
10218 true, false, true);
10223 /* If we have more than the desired maximum number of lines
10224 in the *Messages* buffer now, delete the oldest ones.
10225 This is safe because we don't have undo in this buffer. */
10227 if (NATNUMP (Vmessage_log_max))
10229 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10230 -XFASTINT (Vmessage_log_max) - 1, false);
10231 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10234 BEGV = marker_position (oldbegv);
10235 BEGV_BYTE = marker_byte_position (oldbegv);
10237 if (zv_at_end)
10239 ZV = Z;
10240 ZV_BYTE = Z_BYTE;
10242 else
10244 ZV = marker_position (oldzv);
10245 ZV_BYTE = marker_byte_position (oldzv);
10248 if (point_at_end)
10249 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10250 else
10251 /* We can't do Fgoto_char (oldpoint) because it will run some
10252 Lisp code. */
10253 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10254 marker_byte_position (oldpoint));
10256 unchain_marker (XMARKER (oldpoint));
10257 unchain_marker (XMARKER (oldbegv));
10258 unchain_marker (XMARKER (oldzv));
10260 /* We called insert_1_both above with its 5th argument (PREPARE)
10261 false, which prevents insert_1_both from calling
10262 prepare_to_modify_buffer, which in turns prevents us from
10263 incrementing windows_or_buffers_changed even if *Messages* is
10264 shown in some window. So we must manually set
10265 windows_or_buffers_changed here to make up for that. */
10266 windows_or_buffers_changed = old_windows_or_buffers_changed;
10267 bset_redisplay (current_buffer);
10269 set_buffer_internal (oldbuf);
10271 message_log_need_newline = !nlflag;
10272 Vdeactivate_mark = old_deactivate_mark;
10277 /* We are at the end of the buffer after just having inserted a newline.
10278 (Note: We depend on the fact we won't be crossing the gap.)
10279 Check to see if the most recent message looks a lot like the previous one.
10280 Return 0 if different, 1 if the new one should just replace it, or a
10281 value N > 1 if we should also append " [N times]". */
10283 static intmax_t
10284 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10286 ptrdiff_t i;
10287 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10288 bool seen_dots = false;
10289 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10290 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10292 for (i = 0; i < len; i++)
10294 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10295 seen_dots = true;
10296 if (p1[i] != p2[i])
10297 return seen_dots;
10299 p1 += len;
10300 if (*p1 == '\n')
10301 return 2;
10302 if (*p1++ == ' ' && *p1++ == '[')
10304 char *pend;
10305 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10306 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10307 return n + 1;
10309 return 0;
10313 /* Display an echo area message M with a specified length of NBYTES
10314 bytes. The string may include null characters. If M is not a
10315 string, clear out any existing message, and let the mini-buffer
10316 text show through.
10318 This function cancels echoing. */
10320 void
10321 message3 (Lisp_Object m)
10323 clear_message (true, true);
10324 cancel_echoing ();
10326 /* First flush out any partial line written with print. */
10327 message_log_maybe_newline ();
10328 if (STRINGP (m))
10330 ptrdiff_t nbytes = SBYTES (m);
10331 bool multibyte = STRING_MULTIBYTE (m);
10332 char *buffer;
10333 USE_SAFE_ALLOCA;
10334 SAFE_ALLOCA_STRING (buffer, m);
10335 message_dolog (buffer, nbytes, true, multibyte);
10336 SAFE_FREE ();
10338 if (! inhibit_message)
10339 message3_nolog (m);
10342 /* Log the message M to stderr. Log an empty line if M is not a string. */
10344 static void
10345 message_to_stderr (Lisp_Object m)
10347 if (noninteractive_need_newline)
10349 noninteractive_need_newline = false;
10350 fputc ('\n', stderr);
10352 if (STRINGP (m))
10354 Lisp_Object coding_system = Vlocale_coding_system;
10355 Lisp_Object s;
10357 if (!NILP (Vcoding_system_for_write))
10358 coding_system = Vcoding_system_for_write;
10359 if (!NILP (coding_system))
10360 s = code_convert_string_norecord (m, coding_system, true);
10361 else
10362 s = m;
10364 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10366 if (!cursor_in_echo_area)
10367 fputc ('\n', stderr);
10368 fflush (stderr);
10371 /* The non-logging version of message3.
10372 This does not cancel echoing, because it is used for echoing.
10373 Perhaps we need to make a separate function for echoing
10374 and make this cancel echoing. */
10376 void
10377 message3_nolog (Lisp_Object m)
10379 struct frame *sf = SELECTED_FRAME ();
10381 if (FRAME_INITIAL_P (sf))
10382 message_to_stderr (m);
10383 /* Error messages get reported properly by cmd_error, so this must be just an
10384 informative message; if the frame hasn't really been initialized yet, just
10385 toss it. */
10386 else if (INTERACTIVE && sf->glyphs_initialized_p)
10388 /* Get the frame containing the mini-buffer
10389 that the selected frame is using. */
10390 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10391 Lisp_Object frame = XWINDOW (mini_window)->frame;
10392 struct frame *f = XFRAME (frame);
10394 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10395 Fmake_frame_visible (frame);
10397 if (STRINGP (m) && SCHARS (m) > 0)
10399 set_message (m);
10400 if (minibuffer_auto_raise)
10401 Fraise_frame (frame);
10402 /* Assume we are not echoing.
10403 (If we are, echo_now will override this.) */
10404 echo_message_buffer = Qnil;
10406 else
10407 clear_message (true, true);
10409 do_pending_window_change (false);
10410 echo_area_display (true);
10411 do_pending_window_change (false);
10412 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10413 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10418 /* Display a null-terminated echo area message M. If M is 0, clear
10419 out any existing message, and let the mini-buffer text show through.
10421 The buffer M must continue to exist until after the echo area gets
10422 cleared or some other message gets displayed there. Do not pass
10423 text that is stored in a Lisp string. Do not pass text in a buffer
10424 that was alloca'd. */
10426 void
10427 message1 (const char *m)
10429 message3 (m ? build_unibyte_string (m) : Qnil);
10433 /* The non-logging counterpart of message1. */
10435 void
10436 message1_nolog (const char *m)
10438 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10441 /* Display a message M which contains a single %s
10442 which gets replaced with STRING. */
10444 void
10445 message_with_string (const char *m, Lisp_Object string, bool log)
10447 CHECK_STRING (string);
10449 bool need_message;
10450 if (noninteractive)
10451 need_message = !!m;
10452 else if (!INTERACTIVE)
10453 need_message = false;
10454 else
10456 /* The frame whose minibuffer we're going to display the message on.
10457 It may be larger than the selected frame, so we need
10458 to use its buffer, not the selected frame's buffer. */
10459 Lisp_Object mini_window;
10460 struct frame *f, *sf = SELECTED_FRAME ();
10462 /* Get the frame containing the minibuffer
10463 that the selected frame is using. */
10464 mini_window = FRAME_MINIBUF_WINDOW (sf);
10465 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10467 /* Error messages get reported properly by cmd_error, so this must be
10468 just an informative message; if the frame hasn't really been
10469 initialized yet, just toss it. */
10470 need_message = f->glyphs_initialized_p;
10473 if (need_message)
10475 AUTO_STRING (fmt, m);
10476 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10478 if (noninteractive)
10479 message_to_stderr (msg);
10480 else
10482 if (log)
10483 message3 (msg);
10484 else
10485 message3_nolog (msg);
10487 /* Print should start at the beginning of the message
10488 buffer next time. */
10489 message_buf_print = false;
10495 /* Dump an informative message to the minibuf. If M is 0, clear out
10496 any existing message, and let the mini-buffer text show through.
10498 The message must be safe ASCII and the format must not contain ` or
10499 '. If your message and format do not fit into this category,
10500 convert your arguments to Lisp objects and use Fmessage instead. */
10502 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10503 vmessage (const char *m, va_list ap)
10505 if (noninteractive)
10507 if (m)
10509 if (noninteractive_need_newline)
10510 putc ('\n', stderr);
10511 noninteractive_need_newline = false;
10512 vfprintf (stderr, m, ap);
10513 if (!cursor_in_echo_area)
10514 fprintf (stderr, "\n");
10515 fflush (stderr);
10518 else if (INTERACTIVE)
10520 /* The frame whose mini-buffer we're going to display the message
10521 on. It may be larger than the selected frame, so we need to
10522 use its buffer, not the selected frame's buffer. */
10523 Lisp_Object mini_window;
10524 struct frame *f, *sf = SELECTED_FRAME ();
10526 /* Get the frame containing the mini-buffer
10527 that the selected frame is using. */
10528 mini_window = FRAME_MINIBUF_WINDOW (sf);
10529 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10531 /* Error messages get reported properly by cmd_error, so this must be
10532 just an informative message; if the frame hasn't really been
10533 initialized yet, just toss it. */
10534 if (f->glyphs_initialized_p)
10536 if (m)
10538 ptrdiff_t len;
10539 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10540 USE_SAFE_ALLOCA;
10541 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10543 len = doprnt (message_buf, maxsize, m, 0, ap);
10545 message3 (make_string (message_buf, len));
10546 SAFE_FREE ();
10548 else
10549 message1 (0);
10551 /* Print should start at the beginning of the message
10552 buffer next time. */
10553 message_buf_print = false;
10558 void
10559 message (const char *m, ...)
10561 va_list ap;
10562 va_start (ap, m);
10563 vmessage (m, ap);
10564 va_end (ap);
10568 /* Display the current message in the current mini-buffer. This is
10569 only called from error handlers in process.c, and is not time
10570 critical. */
10572 void
10573 update_echo_area (void)
10575 if (!NILP (echo_area_buffer[0]))
10577 Lisp_Object string;
10578 string = Fcurrent_message ();
10579 message3 (string);
10584 /* Make sure echo area buffers in `echo_buffers' are live.
10585 If they aren't, make new ones. */
10587 static void
10588 ensure_echo_area_buffers (void)
10590 for (int i = 0; i < 2; i++)
10591 if (!BUFFERP (echo_buffer[i])
10592 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10594 Lisp_Object old_buffer = echo_buffer[i];
10595 static char const name_fmt[] = " *Echo Area %d*";
10596 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10597 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10598 echo_buffer[i] = Fget_buffer_create (lname);
10599 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10600 /* to force word wrap in echo area -
10601 it was decided to postpone this*/
10602 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10604 for (int j = 0; j < 2; j++)
10605 if (EQ (old_buffer, echo_area_buffer[j]))
10606 echo_area_buffer[j] = echo_buffer[i];
10611 /* Call FN with args A1..A2 with either the current or last displayed
10612 echo_area_buffer as current buffer.
10614 WHICH zero means use the current message buffer
10615 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10616 from echo_buffer[] and clear it.
10618 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10619 suitable buffer from echo_buffer[] and clear it.
10621 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10622 that the current message becomes the last displayed one, choose a
10623 suitable buffer for echo_area_buffer[0], and clear it.
10625 Value is what FN returns. */
10627 static bool
10628 with_echo_area_buffer (struct window *w, int which,
10629 bool (*fn) (ptrdiff_t, Lisp_Object),
10630 ptrdiff_t a1, Lisp_Object a2)
10632 Lisp_Object buffer;
10633 bool this_one, the_other, clear_buffer_p, rc;
10634 ptrdiff_t count = SPECPDL_INDEX ();
10636 /* If buffers aren't live, make new ones. */
10637 ensure_echo_area_buffers ();
10639 clear_buffer_p = false;
10641 if (which == 0)
10642 this_one = false, the_other = true;
10643 else if (which > 0)
10644 this_one = true, the_other = false;
10645 else
10647 this_one = false, the_other = true;
10648 clear_buffer_p = true;
10650 /* We need a fresh one in case the current echo buffer equals
10651 the one containing the last displayed echo area message. */
10652 if (!NILP (echo_area_buffer[this_one])
10653 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10654 echo_area_buffer[this_one] = Qnil;
10657 /* Choose a suitable buffer from echo_buffer[] if we don't
10658 have one. */
10659 if (NILP (echo_area_buffer[this_one]))
10661 echo_area_buffer[this_one]
10662 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10663 ? echo_buffer[the_other]
10664 : echo_buffer[this_one]);
10665 clear_buffer_p = true;
10668 buffer = echo_area_buffer[this_one];
10670 /* Don't get confused by reusing the buffer used for echoing
10671 for a different purpose. */
10672 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10673 cancel_echoing ();
10675 record_unwind_protect (unwind_with_echo_area_buffer,
10676 with_echo_area_buffer_unwind_data (w));
10678 /* Make the echo area buffer current. Note that for display
10679 purposes, it is not necessary that the displayed window's buffer
10680 == current_buffer, except for text property lookup. So, let's
10681 only set that buffer temporarily here without doing a full
10682 Fset_window_buffer. We must also change w->pointm, though,
10683 because otherwise an assertions in unshow_buffer fails, and Emacs
10684 aborts. */
10685 set_buffer_internal_1 (XBUFFER (buffer));
10686 if (w)
10688 wset_buffer (w, buffer);
10689 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10690 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10693 bset_undo_list (current_buffer, Qt);
10694 bset_read_only (current_buffer, Qnil);
10695 specbind (Qinhibit_read_only, Qt);
10696 specbind (Qinhibit_modification_hooks, Qt);
10698 if (clear_buffer_p && Z > BEG)
10699 del_range (BEG, Z);
10701 eassert (BEGV >= BEG);
10702 eassert (ZV <= Z && ZV >= BEGV);
10704 rc = fn (a1, a2);
10706 eassert (BEGV >= BEG);
10707 eassert (ZV <= Z && ZV >= BEGV);
10709 unbind_to (count, Qnil);
10710 return rc;
10714 /* Save state that should be preserved around the call to the function
10715 FN called in with_echo_area_buffer. */
10717 static Lisp_Object
10718 with_echo_area_buffer_unwind_data (struct window *w)
10720 int i = 0;
10721 Lisp_Object vector, tmp;
10723 /* Reduce consing by keeping one vector in
10724 Vwith_echo_area_save_vector. */
10725 vector = Vwith_echo_area_save_vector;
10726 Vwith_echo_area_save_vector = Qnil;
10728 if (NILP (vector))
10729 vector = Fmake_vector (make_number (11), Qnil);
10731 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10732 ASET (vector, i, Vdeactivate_mark); ++i;
10733 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10735 if (w)
10737 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10738 ASET (vector, i, w->contents); ++i;
10739 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10740 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10741 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10742 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10743 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10744 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10746 else
10748 int end = i + 8;
10749 for (; i < end; ++i)
10750 ASET (vector, i, Qnil);
10753 eassert (i == ASIZE (vector));
10754 return vector;
10758 /* Restore global state from VECTOR which was created by
10759 with_echo_area_buffer_unwind_data. */
10761 static void
10762 unwind_with_echo_area_buffer (Lisp_Object vector)
10764 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10765 Vdeactivate_mark = AREF (vector, 1);
10766 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10768 if (WINDOWP (AREF (vector, 3)))
10770 struct window *w;
10771 Lisp_Object buffer;
10773 w = XWINDOW (AREF (vector, 3));
10774 buffer = AREF (vector, 4);
10776 wset_buffer (w, buffer);
10777 set_marker_both (w->pointm, buffer,
10778 XFASTINT (AREF (vector, 5)),
10779 XFASTINT (AREF (vector, 6)));
10780 set_marker_both (w->old_pointm, buffer,
10781 XFASTINT (AREF (vector, 7)),
10782 XFASTINT (AREF (vector, 8)));
10783 set_marker_both (w->start, buffer,
10784 XFASTINT (AREF (vector, 9)),
10785 XFASTINT (AREF (vector, 10)));
10788 Vwith_echo_area_save_vector = vector;
10792 /* Set up the echo area for use by print functions. MULTIBYTE_P
10793 means we will print multibyte. */
10795 void
10796 setup_echo_area_for_printing (bool multibyte_p)
10798 /* If we can't find an echo area any more, exit. */
10799 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10800 Fkill_emacs (Qnil);
10802 ensure_echo_area_buffers ();
10804 if (!message_buf_print)
10806 /* A message has been output since the last time we printed.
10807 Choose a fresh echo area buffer. */
10808 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10809 echo_area_buffer[0] = echo_buffer[1];
10810 else
10811 echo_area_buffer[0] = echo_buffer[0];
10813 /* Switch to that buffer and clear it. */
10814 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10815 bset_truncate_lines (current_buffer, Qnil);
10817 if (Z > BEG)
10819 ptrdiff_t count = SPECPDL_INDEX ();
10820 specbind (Qinhibit_read_only, Qt);
10821 /* Note that undo recording is always disabled. */
10822 del_range (BEG, Z);
10823 unbind_to (count, Qnil);
10825 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10827 /* Set up the buffer for the multibyteness we need. */
10828 if (multibyte_p
10829 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10830 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10832 /* Raise the frame containing the echo area. */
10833 if (minibuffer_auto_raise)
10835 struct frame *sf = SELECTED_FRAME ();
10836 Lisp_Object mini_window;
10837 mini_window = FRAME_MINIBUF_WINDOW (sf);
10838 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10841 message_log_maybe_newline ();
10842 message_buf_print = true;
10844 else
10846 if (NILP (echo_area_buffer[0]))
10848 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10849 echo_area_buffer[0] = echo_buffer[1];
10850 else
10851 echo_area_buffer[0] = echo_buffer[0];
10854 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10856 /* Someone switched buffers between print requests. */
10857 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10858 bset_truncate_lines (current_buffer, Qnil);
10864 /* Display an echo area message in window W. Value is true if W's
10865 height is changed. If display_last_displayed_message_p,
10866 display the message that was last displayed, otherwise
10867 display the current message. */
10869 static bool
10870 display_echo_area (struct window *w)
10872 bool no_message_p, window_height_changed_p;
10874 /* Temporarily disable garbage collections while displaying the echo
10875 area. This is done because a GC can print a message itself.
10876 That message would modify the echo area buffer's contents while a
10877 redisplay of the buffer is going on, and seriously confuse
10878 redisplay. */
10879 ptrdiff_t count = inhibit_garbage_collection ();
10881 /* If there is no message, we must call display_echo_area_1
10882 nevertheless because it resizes the window. But we will have to
10883 reset the echo_area_buffer in question to nil at the end because
10884 with_echo_area_buffer will sets it to an empty buffer. */
10885 bool i = display_last_displayed_message_p;
10886 /* According to the C99, C11 and C++11 standards, the integral value
10887 of a "bool" is always 0 or 1, so this array access is safe here,
10888 if oddly typed. */
10889 no_message_p = NILP (echo_area_buffer[i]);
10891 window_height_changed_p
10892 = with_echo_area_buffer (w, display_last_displayed_message_p,
10893 display_echo_area_1,
10894 (intptr_t) w, Qnil);
10896 if (no_message_p)
10897 echo_area_buffer[i] = Qnil;
10899 unbind_to (count, Qnil);
10900 return window_height_changed_p;
10904 /* Helper for display_echo_area. Display the current buffer which
10905 contains the current echo area message in window W, a mini-window,
10906 a pointer to which is passed in A1. A2..A4 are currently not used.
10907 Change the height of W so that all of the message is displayed.
10908 Value is true if height of W was changed. */
10910 static bool
10911 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10913 intptr_t i1 = a1;
10914 struct window *w = (struct window *) i1;
10915 Lisp_Object window;
10916 struct text_pos start;
10918 /* We are about to enter redisplay without going through
10919 redisplay_internal, so we need to forget these faces by hand
10920 here. */
10921 forget_escape_and_glyphless_faces ();
10923 /* Do this before displaying, so that we have a large enough glyph
10924 matrix for the display. If we can't get enough space for the
10925 whole text, display the last N lines. That works by setting w->start. */
10926 bool window_height_changed_p = resize_mini_window (w, false);
10928 /* Use the starting position chosen by resize_mini_window. */
10929 SET_TEXT_POS_FROM_MARKER (start, w->start);
10931 /* Display. */
10932 clear_glyph_matrix (w->desired_matrix);
10933 XSETWINDOW (window, w);
10934 try_window (window, start, 0);
10936 return window_height_changed_p;
10940 /* Resize the echo area window to exactly the size needed for the
10941 currently displayed message, if there is one. If a mini-buffer
10942 is active, don't shrink it. */
10944 void
10945 resize_echo_area_exactly (void)
10947 if (BUFFERP (echo_area_buffer[0])
10948 && WINDOWP (echo_area_window))
10950 struct window *w = XWINDOW (echo_area_window);
10951 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10952 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10953 (intptr_t) w, resize_exactly);
10954 if (resized_p)
10956 windows_or_buffers_changed = 42;
10957 update_mode_lines = 30;
10958 redisplay_internal ();
10964 /* Callback function for with_echo_area_buffer, when used from
10965 resize_echo_area_exactly. A1 contains a pointer to the window to
10966 resize, EXACTLY non-nil means resize the mini-window exactly to the
10967 size of the text displayed. A3 and A4 are not used. Value is what
10968 resize_mini_window returns. */
10970 static bool
10971 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10973 intptr_t i1 = a1;
10974 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10978 /* Resize mini-window W to fit the size of its contents. EXACT_P
10979 means size the window exactly to the size needed. Otherwise, it's
10980 only enlarged until W's buffer is empty.
10982 Set W->start to the right place to begin display. If the whole
10983 contents fit, start at the beginning. Otherwise, start so as
10984 to make the end of the contents appear. This is particularly
10985 important for y-or-n-p, but seems desirable generally.
10987 Value is true if the window height has been changed. */
10989 bool
10990 resize_mini_window (struct window *w, bool exact_p)
10992 struct frame *f = XFRAME (w->frame);
10993 bool window_height_changed_p = false;
10995 eassert (MINI_WINDOW_P (w));
10997 /* By default, start display at the beginning. */
10998 set_marker_both (w->start, w->contents,
10999 BUF_BEGV (XBUFFER (w->contents)),
11000 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11002 /* Don't resize windows while redisplaying a window; it would
11003 confuse redisplay functions when the size of the window they are
11004 displaying changes from under them. Such a resizing can happen,
11005 for instance, when which-func prints a long message while
11006 we are running fontification-functions. We're running these
11007 functions with safe_call which binds inhibit-redisplay to t. */
11008 if (!NILP (Vinhibit_redisplay))
11009 return false;
11011 /* Nil means don't try to resize. */
11012 if (NILP (Vresize_mini_windows)
11013 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11014 return false;
11016 if (!FRAME_MINIBUF_ONLY_P (f))
11018 struct it it;
11019 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11020 + WINDOW_PIXEL_HEIGHT (w));
11021 int unit = FRAME_LINE_HEIGHT (f);
11022 int height, max_height;
11023 struct text_pos start;
11024 struct buffer *old_current_buffer = NULL;
11026 if (current_buffer != XBUFFER (w->contents))
11028 old_current_buffer = current_buffer;
11029 set_buffer_internal (XBUFFER (w->contents));
11032 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11034 /* Compute the max. number of lines specified by the user. */
11035 if (FLOATP (Vmax_mini_window_height))
11036 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
11037 else if (INTEGERP (Vmax_mini_window_height))
11038 max_height = XINT (Vmax_mini_window_height) * unit;
11039 else
11040 max_height = total_height / 4;
11042 /* Correct that max. height if it's bogus. */
11043 max_height = clip_to_bounds (unit, max_height, total_height);
11045 /* Find out the height of the text in the window. */
11046 if (it.line_wrap == TRUNCATE)
11047 height = unit;
11048 else
11050 last_height = 0;
11051 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11052 if (it.max_ascent == 0 && it.max_descent == 0)
11053 height = it.current_y + last_height;
11054 else
11055 height = it.current_y + it.max_ascent + it.max_descent;
11056 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11059 /* Compute a suitable window start. */
11060 if (height > max_height)
11062 height = (max_height / unit) * unit;
11063 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11064 move_it_vertically_backward (&it, height - unit);
11065 start = it.current.pos;
11067 else
11068 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11069 SET_MARKER_FROM_TEXT_POS (w->start, start);
11071 if (EQ (Vresize_mini_windows, Qgrow_only))
11073 /* Let it grow only, until we display an empty message, in which
11074 case the window shrinks again. */
11075 if (height > WINDOW_PIXEL_HEIGHT (w))
11077 int old_height = WINDOW_PIXEL_HEIGHT (w);
11079 FRAME_WINDOWS_FROZEN (f) = true;
11080 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11081 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11083 else if (height < WINDOW_PIXEL_HEIGHT (w)
11084 && (exact_p || BEGV == ZV))
11086 int old_height = WINDOW_PIXEL_HEIGHT (w);
11088 FRAME_WINDOWS_FROZEN (f) = false;
11089 shrink_mini_window (w, true);
11090 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11093 else
11095 /* Always resize to exact size needed. */
11096 if (height > WINDOW_PIXEL_HEIGHT (w))
11098 int old_height = WINDOW_PIXEL_HEIGHT (w);
11100 FRAME_WINDOWS_FROZEN (f) = true;
11101 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11102 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11104 else if (height < WINDOW_PIXEL_HEIGHT (w))
11106 int old_height = WINDOW_PIXEL_HEIGHT (w);
11108 FRAME_WINDOWS_FROZEN (f) = false;
11109 shrink_mini_window (w, true);
11111 if (height)
11113 FRAME_WINDOWS_FROZEN (f) = true;
11114 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11117 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11121 if (old_current_buffer)
11122 set_buffer_internal (old_current_buffer);
11125 return window_height_changed_p;
11129 /* Value is the current message, a string, or nil if there is no
11130 current message. */
11132 Lisp_Object
11133 current_message (void)
11135 Lisp_Object msg;
11137 if (!BUFFERP (echo_area_buffer[0]))
11138 msg = Qnil;
11139 else
11141 with_echo_area_buffer (0, 0, current_message_1,
11142 (intptr_t) &msg, Qnil);
11143 if (NILP (msg))
11144 echo_area_buffer[0] = Qnil;
11147 return msg;
11151 static bool
11152 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11154 intptr_t i1 = a1;
11155 Lisp_Object *msg = (Lisp_Object *) i1;
11157 if (Z > BEG)
11158 *msg = make_buffer_string (BEG, Z, true);
11159 else
11160 *msg = Qnil;
11161 return false;
11165 /* Push the current message on Vmessage_stack for later restoration
11166 by restore_message. Value is true if the current message isn't
11167 empty. This is a relatively infrequent operation, so it's not
11168 worth optimizing. */
11170 bool
11171 push_message (void)
11173 Lisp_Object msg = current_message ();
11174 Vmessage_stack = Fcons (msg, Vmessage_stack);
11175 return STRINGP (msg);
11179 /* Restore message display from the top of Vmessage_stack. */
11181 void
11182 restore_message (void)
11184 eassert (CONSP (Vmessage_stack));
11185 message3_nolog (XCAR (Vmessage_stack));
11189 /* Handler for unwind-protect calling pop_message. */
11191 void
11192 pop_message_unwind (void)
11194 /* Pop the top-most entry off Vmessage_stack. */
11195 eassert (CONSP (Vmessage_stack));
11196 Vmessage_stack = XCDR (Vmessage_stack);
11200 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11201 exits. If the stack is not empty, we have a missing pop_message
11202 somewhere. */
11204 void
11205 check_message_stack (void)
11207 if (!NILP (Vmessage_stack))
11208 emacs_abort ();
11212 /* Truncate to NCHARS what will be displayed in the echo area the next
11213 time we display it---but don't redisplay it now. */
11215 void
11216 truncate_echo_area (ptrdiff_t nchars)
11218 if (nchars == 0)
11219 echo_area_buffer[0] = Qnil;
11220 else if (!noninteractive
11221 && INTERACTIVE
11222 && !NILP (echo_area_buffer[0]))
11224 struct frame *sf = SELECTED_FRAME ();
11225 /* Error messages get reported properly by cmd_error, so this must be
11226 just an informative message; if the frame hasn't really been
11227 initialized yet, just toss it. */
11228 if (sf->glyphs_initialized_p)
11229 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11234 /* Helper function for truncate_echo_area. Truncate the current
11235 message to at most NCHARS characters. */
11237 static bool
11238 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11240 if (BEG + nchars < Z)
11241 del_range (BEG + nchars, Z);
11242 if (Z == BEG)
11243 echo_area_buffer[0] = Qnil;
11244 return false;
11247 /* Set the current message to STRING. */
11249 static void
11250 set_message (Lisp_Object string)
11252 eassert (STRINGP (string));
11254 message_enable_multibyte = STRING_MULTIBYTE (string);
11256 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11257 message_buf_print = false;
11258 help_echo_showing_p = false;
11260 if (STRINGP (Vdebug_on_message)
11261 && STRINGP (string)
11262 && fast_string_match (Vdebug_on_message, string) >= 0)
11263 call_debugger (list2 (Qerror, string));
11267 /* Helper function for set_message. First argument is ignored and second
11268 argument has the same meaning as for set_message.
11269 This function is called with the echo area buffer being current. */
11271 static bool
11272 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11274 eassert (STRINGP (string));
11276 /* Change multibyteness of the echo buffer appropriately. */
11277 if (message_enable_multibyte
11278 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11279 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11281 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11282 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11283 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11285 /* Insert new message at BEG. */
11286 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11288 /* This function takes care of single/multibyte conversion.
11289 We just have to ensure that the echo area buffer has the right
11290 setting of enable_multibyte_characters. */
11291 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11293 return false;
11297 /* Clear messages. CURRENT_P means clear the current message.
11298 LAST_DISPLAYED_P means clear the message last displayed. */
11300 void
11301 clear_message (bool current_p, bool last_displayed_p)
11303 if (current_p)
11305 echo_area_buffer[0] = Qnil;
11306 message_cleared_p = true;
11309 if (last_displayed_p)
11310 echo_area_buffer[1] = Qnil;
11312 message_buf_print = false;
11315 /* Clear garbaged frames.
11317 This function is used where the old redisplay called
11318 redraw_garbaged_frames which in turn called redraw_frame which in
11319 turn called clear_frame. The call to clear_frame was a source of
11320 flickering. I believe a clear_frame is not necessary. It should
11321 suffice in the new redisplay to invalidate all current matrices,
11322 and ensure a complete redisplay of all windows. */
11324 static void
11325 clear_garbaged_frames (void)
11327 if (frame_garbaged)
11329 Lisp_Object tail, frame;
11330 struct frame *sf = SELECTED_FRAME ();
11332 FOR_EACH_FRAME (tail, frame)
11334 struct frame *f = XFRAME (frame);
11336 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11338 if (f->resized_p
11339 /* It makes no sense to redraw a non-selected TTY
11340 frame, since that will actually clear the
11341 selected frame, and might leave the selected
11342 frame with corrupted display, if it happens not
11343 to be marked garbaged. */
11344 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11345 redraw_frame (f);
11346 else
11347 clear_current_matrices (f);
11348 fset_redisplay (f);
11349 f->garbaged = false;
11350 f->resized_p = false;
11354 frame_garbaged = false;
11359 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11360 selected_frame. */
11362 static void
11363 echo_area_display (bool update_frame_p)
11365 Lisp_Object mini_window;
11366 struct window *w;
11367 struct frame *f;
11368 bool window_height_changed_p = false;
11369 struct frame *sf = SELECTED_FRAME ();
11371 mini_window = FRAME_MINIBUF_WINDOW (sf);
11372 w = XWINDOW (mini_window);
11373 f = XFRAME (WINDOW_FRAME (w));
11375 /* Don't display if frame is invisible or not yet initialized. */
11376 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11377 return;
11379 #ifdef HAVE_WINDOW_SYSTEM
11380 /* When Emacs starts, selected_frame may be the initial terminal
11381 frame. If we let this through, a message would be displayed on
11382 the terminal. */
11383 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11384 return;
11385 #endif /* HAVE_WINDOW_SYSTEM */
11387 /* Redraw garbaged frames. */
11388 clear_garbaged_frames ();
11390 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11392 echo_area_window = mini_window;
11393 window_height_changed_p = display_echo_area (w);
11394 w->must_be_updated_p = true;
11396 /* Update the display, unless called from redisplay_internal.
11397 Also don't update the screen during redisplay itself. The
11398 update will happen at the end of redisplay, and an update
11399 here could cause confusion. */
11400 if (update_frame_p && !redisplaying_p)
11402 int n = 0;
11404 /* If the display update has been interrupted by pending
11405 input, update mode lines in the frame. Due to the
11406 pending input, it might have been that redisplay hasn't
11407 been called, so that mode lines above the echo area are
11408 garbaged. This looks odd, so we prevent it here. */
11409 if (!display_completed)
11410 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11412 if (window_height_changed_p
11413 /* Don't do this if Emacs is shutting down. Redisplay
11414 needs to run hooks. */
11415 && !NILP (Vrun_hooks))
11417 /* Must update other windows. Likewise as in other
11418 cases, don't let this update be interrupted by
11419 pending input. */
11420 ptrdiff_t count = SPECPDL_INDEX ();
11421 specbind (Qredisplay_dont_pause, Qt);
11422 fset_redisplay (f);
11423 redisplay_internal ();
11424 unbind_to (count, Qnil);
11426 else if (FRAME_WINDOW_P (f) && n == 0)
11428 /* Window configuration is the same as before.
11429 Can do with a display update of the echo area,
11430 unless we displayed some mode lines. */
11431 update_single_window (w);
11432 flush_frame (f);
11434 else
11435 update_frame (f, true, true);
11437 /* If cursor is in the echo area, make sure that the next
11438 redisplay displays the minibuffer, so that the cursor will
11439 be replaced with what the minibuffer wants. */
11440 if (cursor_in_echo_area)
11441 wset_redisplay (XWINDOW (mini_window));
11444 else if (!EQ (mini_window, selected_window))
11445 wset_redisplay (XWINDOW (mini_window));
11447 /* Last displayed message is now the current message. */
11448 echo_area_buffer[1] = echo_area_buffer[0];
11449 /* Inform read_char that we're not echoing. */
11450 echo_message_buffer = Qnil;
11452 /* Prevent redisplay optimization in redisplay_internal by resetting
11453 this_line_start_pos. This is done because the mini-buffer now
11454 displays the message instead of its buffer text. */
11455 if (EQ (mini_window, selected_window))
11456 CHARPOS (this_line_start_pos) = 0;
11458 if (window_height_changed_p)
11460 fset_redisplay (f);
11462 /* If window configuration was changed, frames may have been
11463 marked garbaged. Clear them or we will experience
11464 surprises wrt scrolling.
11465 FIXME: How/why/when? */
11466 clear_garbaged_frames ();
11470 /* True if W's buffer was changed but not saved. */
11472 static bool
11473 window_buffer_changed (struct window *w)
11475 struct buffer *b = XBUFFER (w->contents);
11477 eassert (BUFFER_LIVE_P (b));
11479 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11482 /* True if W has %c in its mode line and mode line should be updated. */
11484 static bool
11485 mode_line_update_needed (struct window *w)
11487 return (w->column_number_displayed != -1
11488 && !(PT == w->last_point && !window_outdated (w))
11489 && (w->column_number_displayed != current_column ()));
11492 /* True if window start of W is frozen and may not be changed during
11493 redisplay. */
11495 static bool
11496 window_frozen_p (struct window *w)
11498 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11500 Lisp_Object window;
11502 XSETWINDOW (window, w);
11503 if (MINI_WINDOW_P (w))
11504 return false;
11505 else if (EQ (window, selected_window))
11506 return false;
11507 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11508 && EQ (window, Vminibuf_scroll_window))
11509 /* This special window can't be frozen too. */
11510 return false;
11511 else
11512 return true;
11514 return false;
11517 /***********************************************************************
11518 Mode Lines and Frame Titles
11519 ***********************************************************************/
11521 /* A buffer for constructing non-propertized mode-line strings and
11522 frame titles in it; allocated from the heap in init_xdisp and
11523 resized as needed in store_mode_line_noprop_char. */
11525 static char *mode_line_noprop_buf;
11527 /* The buffer's end, and a current output position in it. */
11529 static char *mode_line_noprop_buf_end;
11530 static char *mode_line_noprop_ptr;
11532 #define MODE_LINE_NOPROP_LEN(start) \
11533 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11535 static enum {
11536 MODE_LINE_DISPLAY = 0,
11537 MODE_LINE_TITLE,
11538 MODE_LINE_NOPROP,
11539 MODE_LINE_STRING
11540 } mode_line_target;
11542 /* Alist that caches the results of :propertize.
11543 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11544 static Lisp_Object mode_line_proptrans_alist;
11546 /* List of strings making up the mode-line. */
11547 static Lisp_Object mode_line_string_list;
11549 /* Base face property when building propertized mode line string. */
11550 static Lisp_Object mode_line_string_face;
11551 static Lisp_Object mode_line_string_face_prop;
11554 /* Unwind data for mode line strings */
11556 static Lisp_Object Vmode_line_unwind_vector;
11558 static Lisp_Object
11559 format_mode_line_unwind_data (struct frame *target_frame,
11560 struct buffer *obuf,
11561 Lisp_Object owin,
11562 bool save_proptrans)
11564 Lisp_Object vector, tmp;
11566 /* Reduce consing by keeping one vector in
11567 Vwith_echo_area_save_vector. */
11568 vector = Vmode_line_unwind_vector;
11569 Vmode_line_unwind_vector = Qnil;
11571 if (NILP (vector))
11572 vector = Fmake_vector (make_number (10), Qnil);
11574 ASET (vector, 0, make_number (mode_line_target));
11575 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11576 ASET (vector, 2, mode_line_string_list);
11577 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11578 ASET (vector, 4, mode_line_string_face);
11579 ASET (vector, 5, mode_line_string_face_prop);
11581 if (obuf)
11582 XSETBUFFER (tmp, obuf);
11583 else
11584 tmp = Qnil;
11585 ASET (vector, 6, tmp);
11586 ASET (vector, 7, owin);
11587 if (target_frame)
11589 /* Similarly to `with-selected-window', if the operation selects
11590 a window on another frame, we must restore that frame's
11591 selected window, and (for a tty) the top-frame. */
11592 ASET (vector, 8, target_frame->selected_window);
11593 if (FRAME_TERMCAP_P (target_frame))
11594 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11597 return vector;
11600 static void
11601 unwind_format_mode_line (Lisp_Object vector)
11603 Lisp_Object old_window = AREF (vector, 7);
11604 Lisp_Object target_frame_window = AREF (vector, 8);
11605 Lisp_Object old_top_frame = AREF (vector, 9);
11607 mode_line_target = XINT (AREF (vector, 0));
11608 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11609 mode_line_string_list = AREF (vector, 2);
11610 if (! EQ (AREF (vector, 3), Qt))
11611 mode_line_proptrans_alist = AREF (vector, 3);
11612 mode_line_string_face = AREF (vector, 4);
11613 mode_line_string_face_prop = AREF (vector, 5);
11615 /* Select window before buffer, since it may change the buffer. */
11616 if (!NILP (old_window))
11618 /* If the operation that we are unwinding had selected a window
11619 on a different frame, reset its frame-selected-window. For a
11620 text terminal, reset its top-frame if necessary. */
11621 if (!NILP (target_frame_window))
11623 Lisp_Object frame
11624 = WINDOW_FRAME (XWINDOW (target_frame_window));
11626 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11627 Fselect_window (target_frame_window, Qt);
11629 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11630 Fselect_frame (old_top_frame, Qt);
11633 Fselect_window (old_window, Qt);
11636 if (!NILP (AREF (vector, 6)))
11638 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11639 ASET (vector, 6, Qnil);
11642 Vmode_line_unwind_vector = vector;
11646 /* Store a single character C for the frame title in mode_line_noprop_buf.
11647 Re-allocate mode_line_noprop_buf if necessary. */
11649 static void
11650 store_mode_line_noprop_char (char c)
11652 /* If output position has reached the end of the allocated buffer,
11653 increase the buffer's size. */
11654 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11656 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11657 ptrdiff_t size = len;
11658 mode_line_noprop_buf =
11659 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11660 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11661 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11664 *mode_line_noprop_ptr++ = c;
11668 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11669 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11670 characters that yield more columns than PRECISION; PRECISION <= 0
11671 means copy the whole string. Pad with spaces until FIELD_WIDTH
11672 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11673 pad. Called from display_mode_element when it is used to build a
11674 frame title. */
11676 static int
11677 store_mode_line_noprop (const char *string, int field_width, int precision)
11679 const unsigned char *str = (const unsigned char *) string;
11680 int n = 0;
11681 ptrdiff_t dummy, nbytes;
11683 /* Copy at most PRECISION chars from STR. */
11684 nbytes = strlen (string);
11685 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11686 while (nbytes--)
11687 store_mode_line_noprop_char (*str++);
11689 /* Fill up with spaces until FIELD_WIDTH reached. */
11690 while (field_width > 0
11691 && n < field_width)
11693 store_mode_line_noprop_char (' ');
11694 ++n;
11697 return n;
11700 /***********************************************************************
11701 Frame Titles
11702 ***********************************************************************/
11704 #ifdef HAVE_WINDOW_SYSTEM
11706 /* Set the title of FRAME, if it has changed. The title format is
11707 Vicon_title_format if FRAME is iconified, otherwise it is
11708 frame_title_format. */
11710 static void
11711 x_consider_frame_title (Lisp_Object frame)
11713 struct frame *f = XFRAME (frame);
11715 if ((FRAME_WINDOW_P (f)
11716 || FRAME_MINIBUF_ONLY_P (f)
11717 || f->explicit_name)
11718 && NILP (Fframe_parameter (frame, Qtooltip)))
11720 /* Do we have more than one visible frame on this X display? */
11721 Lisp_Object tail, other_frame, fmt;
11722 ptrdiff_t title_start;
11723 char *title;
11724 ptrdiff_t len;
11725 struct it it;
11726 ptrdiff_t count = SPECPDL_INDEX ();
11728 FOR_EACH_FRAME (tail, other_frame)
11730 struct frame *tf = XFRAME (other_frame);
11732 if (tf != f
11733 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11734 && !FRAME_MINIBUF_ONLY_P (tf)
11735 && !EQ (other_frame, tip_frame)
11736 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11737 break;
11740 /* Set global variable indicating that multiple frames exist. */
11741 multiple_frames = CONSP (tail);
11743 /* Switch to the buffer of selected window of the frame. Set up
11744 mode_line_target so that display_mode_element will output into
11745 mode_line_noprop_buf; then display the title. */
11746 record_unwind_protect (unwind_format_mode_line,
11747 format_mode_line_unwind_data
11748 (f, current_buffer, selected_window, false));
11749 /* select-frame calls resize_mini_window, which could resize the
11750 mini-window and by that undo the effect of this redisplay
11751 cycle wrt minibuffer and echo-area display. Binding
11752 inhibit-redisplay to t makes the call to resize_mini_window a
11753 no-op, thus avoiding the adverse side effects. */
11754 specbind (Qinhibit_redisplay, Qt);
11756 Fselect_window (f->selected_window, Qt);
11757 set_buffer_internal_1
11758 (XBUFFER (XWINDOW (f->selected_window)->contents));
11759 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11761 mode_line_target = MODE_LINE_TITLE;
11762 title_start = MODE_LINE_NOPROP_LEN (0);
11763 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11764 NULL, DEFAULT_FACE_ID);
11765 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11766 len = MODE_LINE_NOPROP_LEN (title_start);
11767 title = mode_line_noprop_buf + title_start;
11768 unbind_to (count, Qnil);
11770 /* Set the title only if it's changed. This avoids consing in
11771 the common case where it hasn't. (If it turns out that we've
11772 already wasted too much time by walking through the list with
11773 display_mode_element, then we might need to optimize at a
11774 higher level than this.) */
11775 if (! STRINGP (f->name)
11776 || SBYTES (f->name) != len
11777 || memcmp (title, SDATA (f->name), len) != 0)
11778 x_implicitly_set_name (f, make_string (title, len), Qnil);
11782 #endif /* not HAVE_WINDOW_SYSTEM */
11785 /***********************************************************************
11786 Menu Bars
11787 ***********************************************************************/
11789 /* True if we will not redisplay all visible windows. */
11790 #define REDISPLAY_SOME_P() \
11791 ((windows_or_buffers_changed == 0 \
11792 || windows_or_buffers_changed == REDISPLAY_SOME) \
11793 && (update_mode_lines == 0 \
11794 || update_mode_lines == REDISPLAY_SOME))
11796 /* Prepare for redisplay by updating menu-bar item lists when
11797 appropriate. This can call eval. */
11799 static void
11800 prepare_menu_bars (void)
11802 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11803 bool some_windows = REDISPLAY_SOME_P ();
11804 Lisp_Object tooltip_frame;
11806 #ifdef HAVE_WINDOW_SYSTEM
11807 tooltip_frame = tip_frame;
11808 #else
11809 tooltip_frame = Qnil;
11810 #endif
11812 if (FUNCTIONP (Vpre_redisplay_function))
11814 Lisp_Object windows = all_windows ? Qt : Qnil;
11815 if (all_windows && some_windows)
11817 Lisp_Object ws = window_list ();
11818 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11820 Lisp_Object this = XCAR (ws);
11821 struct window *w = XWINDOW (this);
11822 if (w->redisplay
11823 || XFRAME (w->frame)->redisplay
11824 || XBUFFER (w->contents)->text->redisplay)
11826 windows = Fcons (this, windows);
11830 safe__call1 (true, Vpre_redisplay_function, windows);
11833 /* Update all frame titles based on their buffer names, etc. We do
11834 this before the menu bars so that the buffer-menu will show the
11835 up-to-date frame titles. */
11836 #ifdef HAVE_WINDOW_SYSTEM
11837 if (all_windows)
11839 Lisp_Object tail, frame;
11841 FOR_EACH_FRAME (tail, frame)
11843 struct frame *f = XFRAME (frame);
11844 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11845 if (some_windows
11846 && !f->redisplay
11847 && !w->redisplay
11848 && !XBUFFER (w->contents)->text->redisplay)
11849 continue;
11851 if (!EQ (frame, tooltip_frame)
11852 && (FRAME_ICONIFIED_P (f)
11853 || FRAME_VISIBLE_P (f) == 1
11854 /* Exclude TTY frames that are obscured because they
11855 are not the top frame on their console. This is
11856 because x_consider_frame_title actually switches
11857 to the frame, which for TTY frames means it is
11858 marked as garbaged, and will be completely
11859 redrawn on the next redisplay cycle. This causes
11860 TTY frames to be completely redrawn, when there
11861 are more than one of them, even though nothing
11862 should be changed on display. */
11863 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11864 x_consider_frame_title (frame);
11867 #endif /* HAVE_WINDOW_SYSTEM */
11869 /* Update the menu bar item lists, if appropriate. This has to be
11870 done before any actual redisplay or generation of display lines. */
11872 if (all_windows)
11874 Lisp_Object tail, frame;
11875 ptrdiff_t count = SPECPDL_INDEX ();
11876 /* True means that update_menu_bar has run its hooks
11877 so any further calls to update_menu_bar shouldn't do so again. */
11878 bool menu_bar_hooks_run = false;
11880 record_unwind_save_match_data ();
11882 FOR_EACH_FRAME (tail, frame)
11884 struct frame *f = XFRAME (frame);
11885 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11887 /* Ignore tooltip frame. */
11888 if (EQ (frame, tooltip_frame))
11889 continue;
11891 if (some_windows
11892 && !f->redisplay
11893 && !w->redisplay
11894 && !XBUFFER (w->contents)->text->redisplay)
11895 continue;
11897 run_window_size_change_functions (frame);
11898 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11899 #ifdef HAVE_WINDOW_SYSTEM
11900 update_tool_bar (f, false);
11901 #endif
11904 unbind_to (count, Qnil);
11906 else
11908 struct frame *sf = SELECTED_FRAME ();
11909 update_menu_bar (sf, true, false);
11910 #ifdef HAVE_WINDOW_SYSTEM
11911 update_tool_bar (sf, true);
11912 #endif
11917 /* Update the menu bar item list for frame F. This has to be done
11918 before we start to fill in any display lines, because it can call
11919 eval.
11921 If SAVE_MATCH_DATA, we must save and restore it here.
11923 If HOOKS_RUN, a previous call to update_menu_bar
11924 already ran the menu bar hooks for this redisplay, so there
11925 is no need to run them again. The return value is the
11926 updated value of this flag, to pass to the next call. */
11928 static bool
11929 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11931 Lisp_Object window;
11932 struct window *w;
11934 /* If called recursively during a menu update, do nothing. This can
11935 happen when, for instance, an activate-menubar-hook causes a
11936 redisplay. */
11937 if (inhibit_menubar_update)
11938 return hooks_run;
11940 window = FRAME_SELECTED_WINDOW (f);
11941 w = XWINDOW (window);
11943 if (FRAME_WINDOW_P (f)
11945 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11946 || defined (HAVE_NS) || defined (USE_GTK)
11947 FRAME_EXTERNAL_MENU_BAR (f)
11948 #else
11949 FRAME_MENU_BAR_LINES (f) > 0
11950 #endif
11951 : FRAME_MENU_BAR_LINES (f) > 0)
11953 /* If the user has switched buffers or windows, we need to
11954 recompute to reflect the new bindings. But we'll
11955 recompute when update_mode_lines is set too; that means
11956 that people can use force-mode-line-update to request
11957 that the menu bar be recomputed. The adverse effect on
11958 the rest of the redisplay algorithm is about the same as
11959 windows_or_buffers_changed anyway. */
11960 if (windows_or_buffers_changed
11961 /* This used to test w->update_mode_line, but we believe
11962 there is no need to recompute the menu in that case. */
11963 || update_mode_lines
11964 || window_buffer_changed (w))
11966 struct buffer *prev = current_buffer;
11967 ptrdiff_t count = SPECPDL_INDEX ();
11969 specbind (Qinhibit_menubar_update, Qt);
11971 set_buffer_internal_1 (XBUFFER (w->contents));
11972 if (save_match_data)
11973 record_unwind_save_match_data ();
11974 if (NILP (Voverriding_local_map_menu_flag))
11976 specbind (Qoverriding_terminal_local_map, Qnil);
11977 specbind (Qoverriding_local_map, Qnil);
11980 if (!hooks_run)
11982 /* Run the Lucid hook. */
11983 safe_run_hooks (Qactivate_menubar_hook);
11985 /* If it has changed current-menubar from previous value,
11986 really recompute the menu-bar from the value. */
11987 if (! NILP (Vlucid_menu_bar_dirty_flag))
11988 call0 (Qrecompute_lucid_menubar);
11990 safe_run_hooks (Qmenu_bar_update_hook);
11992 hooks_run = true;
11995 XSETFRAME (Vmenu_updating_frame, f);
11996 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11998 /* Redisplay the menu bar in case we changed it. */
11999 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12000 || defined (HAVE_NS) || defined (USE_GTK)
12001 if (FRAME_WINDOW_P (f))
12003 #if defined (HAVE_NS)
12004 /* All frames on Mac OS share the same menubar. So only
12005 the selected frame should be allowed to set it. */
12006 if (f == SELECTED_FRAME ())
12007 #endif
12008 set_frame_menubar (f, false, false);
12010 else
12011 /* On a terminal screen, the menu bar is an ordinary screen
12012 line, and this makes it get updated. */
12013 w->update_mode_line = true;
12014 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12015 /* In the non-toolkit version, the menu bar is an ordinary screen
12016 line, and this makes it get updated. */
12017 w->update_mode_line = true;
12018 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12020 unbind_to (count, Qnil);
12021 set_buffer_internal_1 (prev);
12025 return hooks_run;
12028 /***********************************************************************
12029 Tool-bars
12030 ***********************************************************************/
12032 #ifdef HAVE_WINDOW_SYSTEM
12034 /* Select `frame' temporarily without running all the code in
12035 do_switch_frame.
12036 FIXME: Maybe do_switch_frame should be trimmed down similarly
12037 when `norecord' is set. */
12038 static void
12039 fast_set_selected_frame (Lisp_Object frame)
12041 if (!EQ (selected_frame, frame))
12043 selected_frame = frame;
12044 selected_window = XFRAME (frame)->selected_window;
12048 /* Update the tool-bar item list for frame F. This has to be done
12049 before we start to fill in any display lines. Called from
12050 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12051 and restore it here. */
12053 static void
12054 update_tool_bar (struct frame *f, bool save_match_data)
12056 #if defined (USE_GTK) || defined (HAVE_NS)
12057 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12058 #else
12059 bool do_update = (WINDOWP (f->tool_bar_window)
12060 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12061 #endif
12063 if (do_update)
12065 Lisp_Object window;
12066 struct window *w;
12068 window = FRAME_SELECTED_WINDOW (f);
12069 w = XWINDOW (window);
12071 /* If the user has switched buffers or windows, we need to
12072 recompute to reflect the new bindings. But we'll
12073 recompute when update_mode_lines is set too; that means
12074 that people can use force-mode-line-update to request
12075 that the menu bar be recomputed. The adverse effect on
12076 the rest of the redisplay algorithm is about the same as
12077 windows_or_buffers_changed anyway. */
12078 if (windows_or_buffers_changed
12079 || w->update_mode_line
12080 || update_mode_lines
12081 || window_buffer_changed (w))
12083 struct buffer *prev = current_buffer;
12084 ptrdiff_t count = SPECPDL_INDEX ();
12085 Lisp_Object frame, new_tool_bar;
12086 int new_n_tool_bar;
12088 /* Set current_buffer to the buffer of the selected
12089 window of the frame, so that we get the right local
12090 keymaps. */
12091 set_buffer_internal_1 (XBUFFER (w->contents));
12093 /* Save match data, if we must. */
12094 if (save_match_data)
12095 record_unwind_save_match_data ();
12097 /* Make sure that we don't accidentally use bogus keymaps. */
12098 if (NILP (Voverriding_local_map_menu_flag))
12100 specbind (Qoverriding_terminal_local_map, Qnil);
12101 specbind (Qoverriding_local_map, Qnil);
12104 /* We must temporarily set the selected frame to this frame
12105 before calling tool_bar_items, because the calculation of
12106 the tool-bar keymap uses the selected frame (see
12107 `tool-bar-make-keymap' in tool-bar.el). */
12108 eassert (EQ (selected_window,
12109 /* Since we only explicitly preserve selected_frame,
12110 check that selected_window would be redundant. */
12111 XFRAME (selected_frame)->selected_window));
12112 record_unwind_protect (fast_set_selected_frame, selected_frame);
12113 XSETFRAME (frame, f);
12114 fast_set_selected_frame (frame);
12116 /* Build desired tool-bar items from keymaps. */
12117 new_tool_bar
12118 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12119 &new_n_tool_bar);
12121 /* Redisplay the tool-bar if we changed it. */
12122 if (new_n_tool_bar != f->n_tool_bar_items
12123 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12125 /* Redisplay that happens asynchronously due to an expose event
12126 may access f->tool_bar_items. Make sure we update both
12127 variables within BLOCK_INPUT so no such event interrupts. */
12128 block_input ();
12129 fset_tool_bar_items (f, new_tool_bar);
12130 f->n_tool_bar_items = new_n_tool_bar;
12131 w->update_mode_line = true;
12132 unblock_input ();
12135 unbind_to (count, Qnil);
12136 set_buffer_internal_1 (prev);
12141 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12143 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12144 F's desired tool-bar contents. F->tool_bar_items must have
12145 been set up previously by calling prepare_menu_bars. */
12147 static void
12148 build_desired_tool_bar_string (struct frame *f)
12150 int i, size, size_needed;
12151 Lisp_Object image, plist;
12153 image = plist = Qnil;
12155 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12156 Otherwise, make a new string. */
12158 /* The size of the string we might be able to reuse. */
12159 size = (STRINGP (f->desired_tool_bar_string)
12160 ? SCHARS (f->desired_tool_bar_string)
12161 : 0);
12163 /* We need one space in the string for each image. */
12164 size_needed = f->n_tool_bar_items;
12166 /* Reuse f->desired_tool_bar_string, if possible. */
12167 if (size < size_needed || NILP (f->desired_tool_bar_string))
12168 fset_desired_tool_bar_string
12169 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12170 else
12172 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12173 Fremove_text_properties (make_number (0), make_number (size),
12174 props, f->desired_tool_bar_string);
12177 /* Put a `display' property on the string for the images to display,
12178 put a `menu_item' property on tool-bar items with a value that
12179 is the index of the item in F's tool-bar item vector. */
12180 for (i = 0; i < f->n_tool_bar_items; ++i)
12182 #define PROP(IDX) \
12183 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12185 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12186 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12187 int hmargin, vmargin, relief, idx, end;
12189 /* If image is a vector, choose the image according to the
12190 button state. */
12191 image = PROP (TOOL_BAR_ITEM_IMAGES);
12192 if (VECTORP (image))
12194 if (enabled_p)
12195 idx = (selected_p
12196 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12197 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12198 else
12199 idx = (selected_p
12200 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12201 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12203 eassert (ASIZE (image) >= idx);
12204 image = AREF (image, idx);
12206 else
12207 idx = -1;
12209 /* Ignore invalid image specifications. */
12210 if (!valid_image_p (image))
12211 continue;
12213 /* Display the tool-bar button pressed, or depressed. */
12214 plist = Fcopy_sequence (XCDR (image));
12216 /* Compute margin and relief to draw. */
12217 relief = (tool_bar_button_relief >= 0
12218 ? tool_bar_button_relief
12219 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12220 hmargin = vmargin = relief;
12222 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12223 INT_MAX - max (hmargin, vmargin)))
12225 hmargin += XFASTINT (Vtool_bar_button_margin);
12226 vmargin += XFASTINT (Vtool_bar_button_margin);
12228 else if (CONSP (Vtool_bar_button_margin))
12230 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12231 INT_MAX - hmargin))
12232 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12234 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12235 INT_MAX - vmargin))
12236 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12239 if (auto_raise_tool_bar_buttons_p)
12241 /* Add a `:relief' property to the image spec if the item is
12242 selected. */
12243 if (selected_p)
12245 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12246 hmargin -= relief;
12247 vmargin -= relief;
12250 else
12252 /* If image is selected, display it pressed, i.e. with a
12253 negative relief. If it's not selected, display it with a
12254 raised relief. */
12255 plist = Fplist_put (plist, QCrelief,
12256 (selected_p
12257 ? make_number (-relief)
12258 : make_number (relief)));
12259 hmargin -= relief;
12260 vmargin -= relief;
12263 /* Put a margin around the image. */
12264 if (hmargin || vmargin)
12266 if (hmargin == vmargin)
12267 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12268 else
12269 plist = Fplist_put (plist, QCmargin,
12270 Fcons (make_number (hmargin),
12271 make_number (vmargin)));
12274 /* If button is not enabled, and we don't have special images
12275 for the disabled state, make the image appear disabled by
12276 applying an appropriate algorithm to it. */
12277 if (!enabled_p && idx < 0)
12278 plist = Fplist_put (plist, QCconversion, Qdisabled);
12280 /* Put a `display' text property on the string for the image to
12281 display. Put a `menu-item' property on the string that gives
12282 the start of this item's properties in the tool-bar items
12283 vector. */
12284 image = Fcons (Qimage, plist);
12285 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12286 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12288 /* Let the last image hide all remaining spaces in the tool bar
12289 string. The string can be longer than needed when we reuse a
12290 previous string. */
12291 if (i + 1 == f->n_tool_bar_items)
12292 end = SCHARS (f->desired_tool_bar_string);
12293 else
12294 end = i + 1;
12295 Fadd_text_properties (make_number (i), make_number (end),
12296 props, f->desired_tool_bar_string);
12297 #undef PROP
12302 /* Display one line of the tool-bar of frame IT->f.
12304 HEIGHT specifies the desired height of the tool-bar line.
12305 If the actual height of the glyph row is less than HEIGHT, the
12306 row's height is increased to HEIGHT, and the icons are centered
12307 vertically in the new height.
12309 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12310 count a final empty row in case the tool-bar width exactly matches
12311 the window width.
12314 static void
12315 display_tool_bar_line (struct it *it, int height)
12317 struct glyph_row *row = it->glyph_row;
12318 int max_x = it->last_visible_x;
12319 struct glyph *last;
12321 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12322 clear_glyph_row (row);
12323 row->enabled_p = true;
12324 row->y = it->current_y;
12326 /* Note that this isn't made use of if the face hasn't a box,
12327 so there's no need to check the face here. */
12328 it->start_of_box_run_p = true;
12330 while (it->current_x < max_x)
12332 int x, n_glyphs_before, i, nglyphs;
12333 struct it it_before;
12335 /* Get the next display element. */
12336 if (!get_next_display_element (it))
12338 /* Don't count empty row if we are counting needed tool-bar lines. */
12339 if (height < 0 && !it->hpos)
12340 return;
12341 break;
12344 /* Produce glyphs. */
12345 n_glyphs_before = row->used[TEXT_AREA];
12346 it_before = *it;
12348 PRODUCE_GLYPHS (it);
12350 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12351 i = 0;
12352 x = it_before.current_x;
12353 while (i < nglyphs)
12355 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12357 if (x + glyph->pixel_width > max_x)
12359 /* Glyph doesn't fit on line. Backtrack. */
12360 row->used[TEXT_AREA] = n_glyphs_before;
12361 *it = it_before;
12362 /* If this is the only glyph on this line, it will never fit on the
12363 tool-bar, so skip it. But ensure there is at least one glyph,
12364 so we don't accidentally disable the tool-bar. */
12365 if (n_glyphs_before == 0
12366 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12367 break;
12368 goto out;
12371 ++it->hpos;
12372 x += glyph->pixel_width;
12373 ++i;
12376 /* Stop at line end. */
12377 if (ITERATOR_AT_END_OF_LINE_P (it))
12378 break;
12380 set_iterator_to_next (it, true);
12383 out:;
12385 row->displays_text_p = row->used[TEXT_AREA] != 0;
12387 /* Use default face for the border below the tool bar.
12389 FIXME: When auto-resize-tool-bars is grow-only, there is
12390 no additional border below the possibly empty tool-bar lines.
12391 So to make the extra empty lines look "normal", we have to
12392 use the tool-bar face for the border too. */
12393 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12394 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12395 it->face_id = DEFAULT_FACE_ID;
12397 extend_face_to_end_of_line (it);
12398 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12399 last->right_box_line_p = true;
12400 if (last == row->glyphs[TEXT_AREA])
12401 last->left_box_line_p = true;
12403 /* Make line the desired height and center it vertically. */
12404 if ((height -= it->max_ascent + it->max_descent) > 0)
12406 /* Don't add more than one line height. */
12407 height %= FRAME_LINE_HEIGHT (it->f);
12408 it->max_ascent += height / 2;
12409 it->max_descent += (height + 1) / 2;
12412 compute_line_metrics (it);
12414 /* If line is empty, make it occupy the rest of the tool-bar. */
12415 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12417 row->height = row->phys_height = it->last_visible_y - row->y;
12418 row->visible_height = row->height;
12419 row->ascent = row->phys_ascent = 0;
12420 row->extra_line_spacing = 0;
12423 row->full_width_p = true;
12424 row->continued_p = false;
12425 row->truncated_on_left_p = false;
12426 row->truncated_on_right_p = false;
12428 it->current_x = it->hpos = 0;
12429 it->current_y += row->height;
12430 ++it->vpos;
12431 ++it->glyph_row;
12435 /* Value is the number of pixels needed to make all tool-bar items of
12436 frame F visible. The actual number of glyph rows needed is
12437 returned in *N_ROWS if non-NULL. */
12438 static int
12439 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12441 struct window *w = XWINDOW (f->tool_bar_window);
12442 struct it it;
12443 /* tool_bar_height is called from redisplay_tool_bar after building
12444 the desired matrix, so use (unused) mode-line row as temporary row to
12445 avoid destroying the first tool-bar row. */
12446 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12448 /* Initialize an iterator for iteration over
12449 F->desired_tool_bar_string in the tool-bar window of frame F. */
12450 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12451 temp_row->reversed_p = false;
12452 it.first_visible_x = 0;
12453 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12454 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12455 it.paragraph_embedding = L2R;
12457 while (!ITERATOR_AT_END_P (&it))
12459 clear_glyph_row (temp_row);
12460 it.glyph_row = temp_row;
12461 display_tool_bar_line (&it, -1);
12463 clear_glyph_row (temp_row);
12465 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12466 if (n_rows)
12467 *n_rows = it.vpos > 0 ? it.vpos : -1;
12469 if (pixelwise)
12470 return it.current_y;
12471 else
12472 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12475 #endif /* !USE_GTK && !HAVE_NS */
12477 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12478 0, 2, 0,
12479 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12480 If FRAME is nil or omitted, use the selected frame. Optional argument
12481 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12482 (Lisp_Object frame, Lisp_Object pixelwise)
12484 int height = 0;
12486 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12487 struct frame *f = decode_any_frame (frame);
12489 if (WINDOWP (f->tool_bar_window)
12490 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12492 update_tool_bar (f, true);
12493 if (f->n_tool_bar_items)
12495 build_desired_tool_bar_string (f);
12496 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12499 #endif
12501 return make_number (height);
12505 /* Display the tool-bar of frame F. Value is true if tool-bar's
12506 height should be changed. */
12507 static bool
12508 redisplay_tool_bar (struct frame *f)
12510 f->tool_bar_redisplayed = true;
12511 #if defined (USE_GTK) || defined (HAVE_NS)
12513 if (FRAME_EXTERNAL_TOOL_BAR (f))
12514 update_frame_tool_bar (f);
12515 return false;
12517 #else /* !USE_GTK && !HAVE_NS */
12519 struct window *w;
12520 struct it it;
12521 struct glyph_row *row;
12523 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12524 do anything. This means you must start with tool-bar-lines
12525 non-zero to get the auto-sizing effect. Or in other words, you
12526 can turn off tool-bars by specifying tool-bar-lines zero. */
12527 if (!WINDOWP (f->tool_bar_window)
12528 || (w = XWINDOW (f->tool_bar_window),
12529 WINDOW_TOTAL_LINES (w) == 0))
12530 return false;
12532 /* Set up an iterator for the tool-bar window. */
12533 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12534 it.first_visible_x = 0;
12535 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12536 row = it.glyph_row;
12537 row->reversed_p = false;
12539 /* Build a string that represents the contents of the tool-bar. */
12540 build_desired_tool_bar_string (f);
12541 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12542 /* FIXME: This should be controlled by a user option. But it
12543 doesn't make sense to have an R2L tool bar if the menu bar cannot
12544 be drawn also R2L, and making the menu bar R2L is tricky due
12545 toolkit-specific code that implements it. If an R2L tool bar is
12546 ever supported, display_tool_bar_line should also be augmented to
12547 call unproduce_glyphs like display_line and display_string
12548 do. */
12549 it.paragraph_embedding = L2R;
12551 if (f->n_tool_bar_rows == 0)
12553 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12555 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12557 x_change_tool_bar_height (f, new_height);
12558 frame_default_tool_bar_height = new_height;
12559 /* Always do that now. */
12560 clear_glyph_matrix (w->desired_matrix);
12561 f->fonts_changed = true;
12562 return true;
12566 /* Display as many lines as needed to display all tool-bar items. */
12568 if (f->n_tool_bar_rows > 0)
12570 int border, rows, height, extra;
12572 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12573 border = XINT (Vtool_bar_border);
12574 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12575 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12576 else if (EQ (Vtool_bar_border, Qborder_width))
12577 border = f->border_width;
12578 else
12579 border = 0;
12580 if (border < 0)
12581 border = 0;
12583 rows = f->n_tool_bar_rows;
12584 height = max (1, (it.last_visible_y - border) / rows);
12585 extra = it.last_visible_y - border - height * rows;
12587 while (it.current_y < it.last_visible_y)
12589 int h = 0;
12590 if (extra > 0 && rows-- > 0)
12592 h = (extra + rows - 1) / rows;
12593 extra -= h;
12595 display_tool_bar_line (&it, height + h);
12598 else
12600 while (it.current_y < it.last_visible_y)
12601 display_tool_bar_line (&it, 0);
12604 /* It doesn't make much sense to try scrolling in the tool-bar
12605 window, so don't do it. */
12606 w->desired_matrix->no_scrolling_p = true;
12607 w->must_be_updated_p = true;
12609 if (!NILP (Vauto_resize_tool_bars))
12611 bool change_height_p = true;
12613 /* If we couldn't display everything, change the tool-bar's
12614 height if there is room for more. */
12615 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12616 change_height_p = true;
12618 /* We subtract 1 because display_tool_bar_line advances the
12619 glyph_row pointer before returning to its caller. We want to
12620 examine the last glyph row produced by
12621 display_tool_bar_line. */
12622 row = it.glyph_row - 1;
12624 /* If there are blank lines at the end, except for a partially
12625 visible blank line at the end that is smaller than
12626 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12627 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12628 && row->height >= FRAME_LINE_HEIGHT (f))
12629 change_height_p = true;
12631 /* If row displays tool-bar items, but is partially visible,
12632 change the tool-bar's height. */
12633 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12634 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12635 change_height_p = true;
12637 /* Resize windows as needed by changing the `tool-bar-lines'
12638 frame parameter. */
12639 if (change_height_p)
12641 int nrows;
12642 int new_height = tool_bar_height (f, &nrows, true);
12644 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12645 && !f->minimize_tool_bar_window_p)
12646 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12647 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12648 f->minimize_tool_bar_window_p = false;
12650 if (change_height_p)
12652 x_change_tool_bar_height (f, new_height);
12653 frame_default_tool_bar_height = new_height;
12654 clear_glyph_matrix (w->desired_matrix);
12655 f->n_tool_bar_rows = nrows;
12656 f->fonts_changed = true;
12658 return true;
12663 f->minimize_tool_bar_window_p = false;
12664 return false;
12666 #endif /* USE_GTK || HAVE_NS */
12669 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12671 /* Get information about the tool-bar item which is displayed in GLYPH
12672 on frame F. Return in *PROP_IDX the index where tool-bar item
12673 properties start in F->tool_bar_items. Value is false if
12674 GLYPH doesn't display a tool-bar item. */
12676 static bool
12677 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12679 Lisp_Object prop;
12680 int charpos;
12682 /* This function can be called asynchronously, which means we must
12683 exclude any possibility that Fget_text_property signals an
12684 error. */
12685 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12686 charpos = max (0, charpos);
12688 /* Get the text property `menu-item' at pos. The value of that
12689 property is the start index of this item's properties in
12690 F->tool_bar_items. */
12691 prop = Fget_text_property (make_number (charpos),
12692 Qmenu_item, f->current_tool_bar_string);
12693 if (! INTEGERP (prop))
12694 return false;
12695 *prop_idx = XINT (prop);
12696 return true;
12700 /* Get information about the tool-bar item at position X/Y on frame F.
12701 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12702 the current matrix of the tool-bar window of F, or NULL if not
12703 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12704 item in F->tool_bar_items. Value is
12706 -1 if X/Y is not on a tool-bar item
12707 0 if X/Y is on the same item that was highlighted before.
12708 1 otherwise. */
12710 static int
12711 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12712 int *hpos, int *vpos, int *prop_idx)
12714 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12715 struct window *w = XWINDOW (f->tool_bar_window);
12716 int area;
12718 /* Find the glyph under X/Y. */
12719 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12720 if (*glyph == NULL)
12721 return -1;
12723 /* Get the start of this tool-bar item's properties in
12724 f->tool_bar_items. */
12725 if (!tool_bar_item_info (f, *glyph, prop_idx))
12726 return -1;
12728 /* Is mouse on the highlighted item? */
12729 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12730 && *vpos >= hlinfo->mouse_face_beg_row
12731 && *vpos <= hlinfo->mouse_face_end_row
12732 && (*vpos > hlinfo->mouse_face_beg_row
12733 || *hpos >= hlinfo->mouse_face_beg_col)
12734 && (*vpos < hlinfo->mouse_face_end_row
12735 || *hpos < hlinfo->mouse_face_end_col
12736 || hlinfo->mouse_face_past_end))
12737 return 0;
12739 return 1;
12743 /* EXPORT:
12744 Handle mouse button event on the tool-bar of frame F, at
12745 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12746 false for button release. MODIFIERS is event modifiers for button
12747 release. */
12749 void
12750 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12751 int modifiers)
12753 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12754 struct window *w = XWINDOW (f->tool_bar_window);
12755 int hpos, vpos, prop_idx;
12756 struct glyph *glyph;
12757 Lisp_Object enabled_p;
12758 int ts;
12760 /* If not on the highlighted tool-bar item, and mouse-highlight is
12761 non-nil, return. This is so we generate the tool-bar button
12762 click only when the mouse button is released on the same item as
12763 where it was pressed. However, when mouse-highlight is disabled,
12764 generate the click when the button is released regardless of the
12765 highlight, since tool-bar items are not highlighted in that
12766 case. */
12767 frame_to_window_pixel_xy (w, &x, &y);
12768 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12769 if (ts == -1
12770 || (ts != 0 && !NILP (Vmouse_highlight)))
12771 return;
12773 /* When mouse-highlight is off, generate the click for the item
12774 where the button was pressed, disregarding where it was
12775 released. */
12776 if (NILP (Vmouse_highlight) && !down_p)
12777 prop_idx = f->last_tool_bar_item;
12779 /* If item is disabled, do nothing. */
12780 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12781 if (NILP (enabled_p))
12782 return;
12784 if (down_p)
12786 /* Show item in pressed state. */
12787 if (!NILP (Vmouse_highlight))
12788 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12789 f->last_tool_bar_item = prop_idx;
12791 else
12793 Lisp_Object key, frame;
12794 struct input_event event;
12795 EVENT_INIT (event);
12797 /* Show item in released state. */
12798 if (!NILP (Vmouse_highlight))
12799 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12801 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12803 XSETFRAME (frame, f);
12804 event.kind = TOOL_BAR_EVENT;
12805 event.frame_or_window = frame;
12806 event.arg = frame;
12807 kbd_buffer_store_event (&event);
12809 event.kind = TOOL_BAR_EVENT;
12810 event.frame_or_window = frame;
12811 event.arg = key;
12812 event.modifiers = modifiers;
12813 kbd_buffer_store_event (&event);
12814 f->last_tool_bar_item = -1;
12819 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12820 tool-bar window-relative coordinates X/Y. Called from
12821 note_mouse_highlight. */
12823 static void
12824 note_tool_bar_highlight (struct frame *f, int x, int y)
12826 Lisp_Object window = f->tool_bar_window;
12827 struct window *w = XWINDOW (window);
12828 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12829 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12830 int hpos, vpos;
12831 struct glyph *glyph;
12832 struct glyph_row *row;
12833 int i;
12834 Lisp_Object enabled_p;
12835 int prop_idx;
12836 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12837 bool mouse_down_p;
12838 int rc;
12840 /* Function note_mouse_highlight is called with negative X/Y
12841 values when mouse moves outside of the frame. */
12842 if (x <= 0 || y <= 0)
12844 clear_mouse_face (hlinfo);
12845 return;
12848 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12849 if (rc < 0)
12851 /* Not on tool-bar item. */
12852 clear_mouse_face (hlinfo);
12853 return;
12855 else if (rc == 0)
12856 /* On same tool-bar item as before. */
12857 goto set_help_echo;
12859 clear_mouse_face (hlinfo);
12861 /* Mouse is down, but on different tool-bar item? */
12862 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12863 && f == dpyinfo->last_mouse_frame);
12865 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12866 return;
12868 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12870 /* If tool-bar item is not enabled, don't highlight it. */
12871 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12872 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12874 /* Compute the x-position of the glyph. In front and past the
12875 image is a space. We include this in the highlighted area. */
12876 row = MATRIX_ROW (w->current_matrix, vpos);
12877 for (i = x = 0; i < hpos; ++i)
12878 x += row->glyphs[TEXT_AREA][i].pixel_width;
12880 /* Record this as the current active region. */
12881 hlinfo->mouse_face_beg_col = hpos;
12882 hlinfo->mouse_face_beg_row = vpos;
12883 hlinfo->mouse_face_beg_x = x;
12884 hlinfo->mouse_face_past_end = false;
12886 hlinfo->mouse_face_end_col = hpos + 1;
12887 hlinfo->mouse_face_end_row = vpos;
12888 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12889 hlinfo->mouse_face_window = window;
12890 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12892 /* Display it as active. */
12893 show_mouse_face (hlinfo, draw);
12896 set_help_echo:
12898 /* Set help_echo_string to a help string to display for this tool-bar item.
12899 XTread_socket does the rest. */
12900 help_echo_object = help_echo_window = Qnil;
12901 help_echo_pos = -1;
12902 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12903 if (NILP (help_echo_string))
12904 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12907 #endif /* !USE_GTK && !HAVE_NS */
12909 #endif /* HAVE_WINDOW_SYSTEM */
12913 /************************************************************************
12914 Horizontal scrolling
12915 ************************************************************************/
12917 /* For all leaf windows in the window tree rooted at WINDOW, set their
12918 hscroll value so that PT is (i) visible in the window, and (ii) so
12919 that it is not within a certain margin at the window's left and
12920 right border. Value is true if any window's hscroll has been
12921 changed. */
12923 static bool
12924 hscroll_window_tree (Lisp_Object window)
12926 bool hscrolled_p = false;
12927 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12928 int hscroll_step_abs = 0;
12929 double hscroll_step_rel = 0;
12931 if (hscroll_relative_p)
12933 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12934 if (hscroll_step_rel < 0)
12936 hscroll_relative_p = false;
12937 hscroll_step_abs = 0;
12940 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12942 hscroll_step_abs = XINT (Vhscroll_step);
12943 if (hscroll_step_abs < 0)
12944 hscroll_step_abs = 0;
12946 else
12947 hscroll_step_abs = 0;
12949 while (WINDOWP (window))
12951 struct window *w = XWINDOW (window);
12953 if (WINDOWP (w->contents))
12954 hscrolled_p |= hscroll_window_tree (w->contents);
12955 else if (w->cursor.vpos >= 0)
12957 int h_margin;
12958 int text_area_width;
12959 struct glyph_row *cursor_row;
12960 struct glyph_row *bottom_row;
12962 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12963 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12964 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12965 else
12966 cursor_row = bottom_row - 1;
12968 if (!cursor_row->enabled_p)
12970 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12971 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12972 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12973 else
12974 cursor_row = bottom_row - 1;
12976 bool row_r2l_p = cursor_row->reversed_p;
12978 text_area_width = window_box_width (w, TEXT_AREA);
12980 /* Scroll when cursor is inside this scroll margin. */
12981 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12983 /* If the position of this window's point has explicitly
12984 changed, no more suspend auto hscrolling. */
12985 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12986 w->suspend_auto_hscroll = false;
12988 /* Remember window point. */
12989 Fset_marker (w->old_pointm,
12990 ((w == XWINDOW (selected_window))
12991 ? make_number (BUF_PT (XBUFFER (w->contents)))
12992 : Fmarker_position (w->pointm)),
12993 w->contents);
12995 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12996 && !w->suspend_auto_hscroll
12997 /* In some pathological cases, like restoring a window
12998 configuration into a frame that is much smaller than
12999 the one from which the configuration was saved, we
13000 get glyph rows whose start and end have zero buffer
13001 positions, which we cannot handle below. Just skip
13002 such windows. */
13003 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13004 /* For left-to-right rows, hscroll when cursor is either
13005 (i) inside the right hscroll margin, or (ii) if it is
13006 inside the left margin and the window is already
13007 hscrolled. */
13008 && ((!row_r2l_p
13009 && ((w->hscroll && w->cursor.x <= h_margin)
13010 || (cursor_row->enabled_p
13011 && cursor_row->truncated_on_right_p
13012 && (w->cursor.x >= text_area_width - h_margin))))
13013 /* For right-to-left rows, the logic is similar,
13014 except that rules for scrolling to left and right
13015 are reversed. E.g., if cursor.x <= h_margin, we
13016 need to hscroll "to the right" unconditionally,
13017 and that will scroll the screen to the left so as
13018 to reveal the next portion of the row. */
13019 || (row_r2l_p
13020 && ((cursor_row->enabled_p
13021 /* FIXME: It is confusing to set the
13022 truncated_on_right_p flag when R2L rows
13023 are actually truncated on the left. */
13024 && cursor_row->truncated_on_right_p
13025 && w->cursor.x <= h_margin)
13026 || (w->hscroll
13027 && (w->cursor.x >= text_area_width - h_margin))))))
13029 struct it it;
13030 ptrdiff_t hscroll;
13031 struct buffer *saved_current_buffer;
13032 ptrdiff_t pt;
13033 int wanted_x;
13035 /* Find point in a display of infinite width. */
13036 saved_current_buffer = current_buffer;
13037 current_buffer = XBUFFER (w->contents);
13039 if (w == XWINDOW (selected_window))
13040 pt = PT;
13041 else
13042 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13044 /* Move iterator to pt starting at cursor_row->start in
13045 a line with infinite width. */
13046 init_to_row_start (&it, w, cursor_row);
13047 it.last_visible_x = INFINITY;
13048 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13049 current_buffer = saved_current_buffer;
13051 /* Position cursor in window. */
13052 if (!hscroll_relative_p && hscroll_step_abs == 0)
13053 hscroll = max (0, (it.current_x
13054 - (ITERATOR_AT_END_OF_LINE_P (&it)
13055 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13056 : (text_area_width / 2))))
13057 / FRAME_COLUMN_WIDTH (it.f);
13058 else if ((!row_r2l_p
13059 && w->cursor.x >= text_area_width - h_margin)
13060 || (row_r2l_p && w->cursor.x <= h_margin))
13062 if (hscroll_relative_p)
13063 wanted_x = text_area_width * (1 - hscroll_step_rel)
13064 - h_margin;
13065 else
13066 wanted_x = text_area_width
13067 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13068 - h_margin;
13069 hscroll
13070 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13072 else
13074 if (hscroll_relative_p)
13075 wanted_x = text_area_width * hscroll_step_rel
13076 + h_margin;
13077 else
13078 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13079 + h_margin;
13080 hscroll
13081 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13083 hscroll = max (hscroll, w->min_hscroll);
13085 /* Don't prevent redisplay optimizations if hscroll
13086 hasn't changed, as it will unnecessarily slow down
13087 redisplay. */
13088 if (w->hscroll != hscroll)
13090 struct buffer *b = XBUFFER (w->contents);
13091 b->prevent_redisplay_optimizations_p = true;
13092 w->hscroll = hscroll;
13093 hscrolled_p = true;
13098 window = w->next;
13101 /* Value is true if hscroll of any leaf window has been changed. */
13102 return hscrolled_p;
13106 /* Set hscroll so that cursor is visible and not inside horizontal
13107 scroll margins for all windows in the tree rooted at WINDOW. See
13108 also hscroll_window_tree above. Value is true if any window's
13109 hscroll has been changed. If it has, desired matrices on the frame
13110 of WINDOW are cleared. */
13112 static bool
13113 hscroll_windows (Lisp_Object window)
13115 bool hscrolled_p = hscroll_window_tree (window);
13116 if (hscrolled_p)
13117 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13118 return hscrolled_p;
13123 /************************************************************************
13124 Redisplay
13125 ************************************************************************/
13127 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13128 This is sometimes handy to have in a debugger session. */
13130 #ifdef GLYPH_DEBUG
13132 /* First and last unchanged row for try_window_id. */
13134 static int debug_first_unchanged_at_end_vpos;
13135 static int debug_last_unchanged_at_beg_vpos;
13137 /* Delta vpos and y. */
13139 static int debug_dvpos, debug_dy;
13141 /* Delta in characters and bytes for try_window_id. */
13143 static ptrdiff_t debug_delta, debug_delta_bytes;
13145 /* Values of window_end_pos and window_end_vpos at the end of
13146 try_window_id. */
13148 static ptrdiff_t debug_end_vpos;
13150 /* Append a string to W->desired_matrix->method. FMT is a printf
13151 format string. If trace_redisplay_p is true also printf the
13152 resulting string to stderr. */
13154 static void debug_method_add (struct window *, char const *, ...)
13155 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13157 static void
13158 debug_method_add (struct window *w, char const *fmt, ...)
13160 void *ptr = w;
13161 char *method = w->desired_matrix->method;
13162 int len = strlen (method);
13163 int size = sizeof w->desired_matrix->method;
13164 int remaining = size - len - 1;
13165 va_list ap;
13167 if (len && remaining)
13169 method[len] = '|';
13170 --remaining, ++len;
13173 va_start (ap, fmt);
13174 vsnprintf (method + len, remaining + 1, fmt, ap);
13175 va_end (ap);
13177 if (trace_redisplay_p)
13178 fprintf (stderr, "%p (%s): %s\n",
13179 ptr,
13180 ((BUFFERP (w->contents)
13181 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13182 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13183 : "no buffer"),
13184 method + len);
13187 #endif /* GLYPH_DEBUG */
13190 /* Value is true if all changes in window W, which displays
13191 current_buffer, are in the text between START and END. START is a
13192 buffer position, END is given as a distance from Z. Used in
13193 redisplay_internal for display optimization. */
13195 static bool
13196 text_outside_line_unchanged_p (struct window *w,
13197 ptrdiff_t start, ptrdiff_t end)
13199 bool unchanged_p = true;
13201 /* If text or overlays have changed, see where. */
13202 if (window_outdated (w))
13204 /* Gap in the line? */
13205 if (GPT < start || Z - GPT < end)
13206 unchanged_p = false;
13208 /* Changes start in front of the line, or end after it? */
13209 if (unchanged_p
13210 && (BEG_UNCHANGED < start - 1
13211 || END_UNCHANGED < end))
13212 unchanged_p = false;
13214 /* If selective display, can't optimize if changes start at the
13215 beginning of the line. */
13216 if (unchanged_p
13217 && INTEGERP (BVAR (current_buffer, selective_display))
13218 && XINT (BVAR (current_buffer, selective_display)) > 0
13219 && (BEG_UNCHANGED < start || GPT <= start))
13220 unchanged_p = false;
13222 /* If there are overlays at the start or end of the line, these
13223 may have overlay strings with newlines in them. A change at
13224 START, for instance, may actually concern the display of such
13225 overlay strings as well, and they are displayed on different
13226 lines. So, quickly rule out this case. (For the future, it
13227 might be desirable to implement something more telling than
13228 just BEG/END_UNCHANGED.) */
13229 if (unchanged_p)
13231 if (BEG + BEG_UNCHANGED == start
13232 && overlay_touches_p (start))
13233 unchanged_p = false;
13234 if (END_UNCHANGED == end
13235 && overlay_touches_p (Z - end))
13236 unchanged_p = false;
13239 /* Under bidi reordering, adding or deleting a character in the
13240 beginning of a paragraph, before the first strong directional
13241 character, can change the base direction of the paragraph (unless
13242 the buffer specifies a fixed paragraph direction), which will
13243 require redisplaying the whole paragraph. It might be worthwhile
13244 to find the paragraph limits and widen the range of redisplayed
13245 lines to that, but for now just give up this optimization. */
13246 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13247 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13248 unchanged_p = false;
13251 return unchanged_p;
13255 /* Do a frame update, taking possible shortcuts into account. This is
13256 the main external entry point for redisplay.
13258 If the last redisplay displayed an echo area message and that message
13259 is no longer requested, we clear the echo area or bring back the
13260 mini-buffer if that is in use. */
13262 void
13263 redisplay (void)
13265 redisplay_internal ();
13269 static Lisp_Object
13270 overlay_arrow_string_or_property (Lisp_Object var)
13272 Lisp_Object val;
13274 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13275 return val;
13277 return Voverlay_arrow_string;
13280 /* Return true if there are any overlay-arrows in current_buffer. */
13281 static bool
13282 overlay_arrow_in_current_buffer_p (void)
13284 Lisp_Object vlist;
13286 for (vlist = Voverlay_arrow_variable_list;
13287 CONSP (vlist);
13288 vlist = XCDR (vlist))
13290 Lisp_Object var = XCAR (vlist);
13291 Lisp_Object val;
13293 if (!SYMBOLP (var))
13294 continue;
13295 val = find_symbol_value (var);
13296 if (MARKERP (val)
13297 && current_buffer == XMARKER (val)->buffer)
13298 return true;
13300 return false;
13304 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13305 has changed. */
13307 static bool
13308 overlay_arrows_changed_p (void)
13310 Lisp_Object vlist;
13312 for (vlist = Voverlay_arrow_variable_list;
13313 CONSP (vlist);
13314 vlist = XCDR (vlist))
13316 Lisp_Object var = XCAR (vlist);
13317 Lisp_Object val, pstr;
13319 if (!SYMBOLP (var))
13320 continue;
13321 val = find_symbol_value (var);
13322 if (!MARKERP (val))
13323 continue;
13324 if (! EQ (COERCE_MARKER (val),
13325 Fget (var, Qlast_arrow_position))
13326 || ! (pstr = overlay_arrow_string_or_property (var),
13327 EQ (pstr, Fget (var, Qlast_arrow_string))))
13328 return true;
13330 return false;
13333 /* Mark overlay arrows to be updated on next redisplay. */
13335 static void
13336 update_overlay_arrows (int up_to_date)
13338 Lisp_Object vlist;
13340 for (vlist = Voverlay_arrow_variable_list;
13341 CONSP (vlist);
13342 vlist = XCDR (vlist))
13344 Lisp_Object var = XCAR (vlist);
13346 if (!SYMBOLP (var))
13347 continue;
13349 if (up_to_date > 0)
13351 Lisp_Object val = find_symbol_value (var);
13352 Fput (var, Qlast_arrow_position,
13353 COERCE_MARKER (val));
13354 Fput (var, Qlast_arrow_string,
13355 overlay_arrow_string_or_property (var));
13357 else if (up_to_date < 0
13358 || !NILP (Fget (var, Qlast_arrow_position)))
13360 Fput (var, Qlast_arrow_position, Qt);
13361 Fput (var, Qlast_arrow_string, Qt);
13367 /* Return overlay arrow string to display at row.
13368 Return integer (bitmap number) for arrow bitmap in left fringe.
13369 Return nil if no overlay arrow. */
13371 static Lisp_Object
13372 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13374 Lisp_Object vlist;
13376 for (vlist = Voverlay_arrow_variable_list;
13377 CONSP (vlist);
13378 vlist = XCDR (vlist))
13380 Lisp_Object var = XCAR (vlist);
13381 Lisp_Object val;
13383 if (!SYMBOLP (var))
13384 continue;
13386 val = find_symbol_value (var);
13388 if (MARKERP (val)
13389 && current_buffer == XMARKER (val)->buffer
13390 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13392 if (FRAME_WINDOW_P (it->f)
13393 /* FIXME: if ROW->reversed_p is set, this should test
13394 the right fringe, not the left one. */
13395 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13397 #ifdef HAVE_WINDOW_SYSTEM
13398 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13400 int fringe_bitmap = lookup_fringe_bitmap (val);
13401 if (fringe_bitmap != 0)
13402 return make_number (fringe_bitmap);
13404 #endif
13405 return make_number (-1); /* Use default arrow bitmap. */
13407 return overlay_arrow_string_or_property (var);
13411 return Qnil;
13414 /* Return true if point moved out of or into a composition. Otherwise
13415 return false. PREV_BUF and PREV_PT are the last point buffer and
13416 position. BUF and PT are the current point buffer and position. */
13418 static bool
13419 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13420 struct buffer *buf, ptrdiff_t pt)
13422 ptrdiff_t start, end;
13423 Lisp_Object prop;
13424 Lisp_Object buffer;
13426 XSETBUFFER (buffer, buf);
13427 /* Check a composition at the last point if point moved within the
13428 same buffer. */
13429 if (prev_buf == buf)
13431 if (prev_pt == pt)
13432 /* Point didn't move. */
13433 return false;
13435 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13436 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13437 && composition_valid_p (start, end, prop)
13438 && start < prev_pt && end > prev_pt)
13439 /* The last point was within the composition. Return true iff
13440 point moved out of the composition. */
13441 return (pt <= start || pt >= end);
13444 /* Check a composition at the current point. */
13445 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13446 && find_composition (pt, -1, &start, &end, &prop, buffer)
13447 && composition_valid_p (start, end, prop)
13448 && start < pt && end > pt);
13451 /* Reconsider the clip changes of buffer which is displayed in W. */
13453 static void
13454 reconsider_clip_changes (struct window *w)
13456 struct buffer *b = XBUFFER (w->contents);
13458 if (b->clip_changed
13459 && w->window_end_valid
13460 && w->current_matrix->buffer == b
13461 && w->current_matrix->zv == BUF_ZV (b)
13462 && w->current_matrix->begv == BUF_BEGV (b))
13463 b->clip_changed = false;
13465 /* If display wasn't paused, and W is not a tool bar window, see if
13466 point has been moved into or out of a composition. In that case,
13467 set b->clip_changed to force updating the screen. If
13468 b->clip_changed has already been set, skip this check. */
13469 if (!b->clip_changed && w->window_end_valid)
13471 ptrdiff_t pt = (w == XWINDOW (selected_window)
13472 ? PT : marker_position (w->pointm));
13474 if ((w->current_matrix->buffer != b || pt != w->last_point)
13475 && check_point_in_composition (w->current_matrix->buffer,
13476 w->last_point, b, pt))
13477 b->clip_changed = true;
13481 static void
13482 propagate_buffer_redisplay (void)
13483 { /* Resetting b->text->redisplay is problematic!
13484 We can't just reset it in the case that some window that displays
13485 it has not been redisplayed; and such a window can stay
13486 unredisplayed for a long time if it's currently invisible.
13487 But we do want to reset it at the end of redisplay otherwise
13488 its displayed windows will keep being redisplayed over and over
13489 again.
13490 So we copy all b->text->redisplay flags up to their windows here,
13491 such that mark_window_display_accurate can safely reset
13492 b->text->redisplay. */
13493 Lisp_Object ws = window_list ();
13494 for (; CONSP (ws); ws = XCDR (ws))
13496 struct window *thisw = XWINDOW (XCAR (ws));
13497 struct buffer *thisb = XBUFFER (thisw->contents);
13498 if (thisb->text->redisplay)
13499 thisw->redisplay = true;
13503 #define STOP_POLLING \
13504 do { if (! polling_stopped_here) stop_polling (); \
13505 polling_stopped_here = true; } while (false)
13507 #define RESUME_POLLING \
13508 do { if (polling_stopped_here) start_polling (); \
13509 polling_stopped_here = false; } while (false)
13512 /* Perhaps in the future avoid recentering windows if it
13513 is not necessary; currently that causes some problems. */
13515 static void
13516 redisplay_internal (void)
13518 struct window *w = XWINDOW (selected_window);
13519 struct window *sw;
13520 struct frame *fr;
13521 bool pending;
13522 bool must_finish = false, match_p;
13523 struct text_pos tlbufpos, tlendpos;
13524 int number_of_visible_frames;
13525 ptrdiff_t count;
13526 struct frame *sf;
13527 bool polling_stopped_here = false;
13528 Lisp_Object tail, frame;
13530 /* True means redisplay has to consider all windows on all
13531 frames. False, only selected_window is considered. */
13532 bool consider_all_windows_p;
13534 /* True means redisplay has to redisplay the miniwindow. */
13535 bool update_miniwindow_p = false;
13537 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13539 /* No redisplay if running in batch mode or frame is not yet fully
13540 initialized, or redisplay is explicitly turned off by setting
13541 Vinhibit_redisplay. */
13542 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13543 || !NILP (Vinhibit_redisplay))
13544 return;
13546 /* Don't examine these until after testing Vinhibit_redisplay.
13547 When Emacs is shutting down, perhaps because its connection to
13548 X has dropped, we should not look at them at all. */
13549 fr = XFRAME (w->frame);
13550 sf = SELECTED_FRAME ();
13552 if (!fr->glyphs_initialized_p)
13553 return;
13555 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13556 if (popup_activated ())
13557 return;
13558 #endif
13560 /* I don't think this happens but let's be paranoid. */
13561 if (redisplaying_p)
13562 return;
13564 /* Record a function that clears redisplaying_p
13565 when we leave this function. */
13566 count = SPECPDL_INDEX ();
13567 record_unwind_protect_void (unwind_redisplay);
13568 redisplaying_p = true;
13569 specbind (Qinhibit_free_realized_faces, Qnil);
13571 /* Record this function, so it appears on the profiler's backtraces. */
13572 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13574 FOR_EACH_FRAME (tail, frame)
13575 XFRAME (frame)->already_hscrolled_p = false;
13577 retry:
13578 /* Remember the currently selected window. */
13579 sw = w;
13581 pending = false;
13582 forget_escape_and_glyphless_faces ();
13584 inhibit_free_realized_faces = false;
13586 /* If face_change, init_iterator will free all realized faces, which
13587 includes the faces referenced from current matrices. So, we
13588 can't reuse current matrices in this case. */
13589 if (face_change)
13590 windows_or_buffers_changed = 47;
13592 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13593 && FRAME_TTY (sf)->previous_frame != sf)
13595 /* Since frames on a single ASCII terminal share the same
13596 display area, displaying a different frame means redisplay
13597 the whole thing. */
13598 SET_FRAME_GARBAGED (sf);
13599 #ifndef DOS_NT
13600 set_tty_color_mode (FRAME_TTY (sf), sf);
13601 #endif
13602 FRAME_TTY (sf)->previous_frame = sf;
13605 /* Set the visible flags for all frames. Do this before checking for
13606 resized or garbaged frames; they want to know if their frames are
13607 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13608 number_of_visible_frames = 0;
13610 FOR_EACH_FRAME (tail, frame)
13612 struct frame *f = XFRAME (frame);
13614 if (FRAME_VISIBLE_P (f))
13616 ++number_of_visible_frames;
13617 /* Adjust matrices for visible frames only. */
13618 if (f->fonts_changed)
13620 adjust_frame_glyphs (f);
13621 /* Disable all redisplay optimizations for this frame.
13622 This is because adjust_frame_glyphs resets the
13623 enabled_p flag for all glyph rows of all windows, so
13624 many optimizations will fail anyway, and some might
13625 fail to test that flag and do bogus things as
13626 result. */
13627 SET_FRAME_GARBAGED (f);
13628 f->fonts_changed = false;
13630 /* If cursor type has been changed on the frame
13631 other than selected, consider all frames. */
13632 if (f != sf && f->cursor_type_changed)
13633 fset_redisplay (f);
13635 clear_desired_matrices (f);
13638 /* Notice any pending interrupt request to change frame size. */
13639 do_pending_window_change (true);
13641 /* do_pending_window_change could change the selected_window due to
13642 frame resizing which makes the selected window too small. */
13643 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13644 sw = w;
13646 /* Clear frames marked as garbaged. */
13647 clear_garbaged_frames ();
13649 /* Build menubar and tool-bar items. */
13650 if (NILP (Vmemory_full))
13651 prepare_menu_bars ();
13653 reconsider_clip_changes (w);
13655 /* In most cases selected window displays current buffer. */
13656 match_p = XBUFFER (w->contents) == current_buffer;
13657 if (match_p)
13659 /* Detect case that we need to write or remove a star in the mode line. */
13660 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13661 w->update_mode_line = true;
13663 if (mode_line_update_needed (w))
13664 w->update_mode_line = true;
13666 /* If reconsider_clip_changes above decided that the narrowing
13667 in the current buffer changed, make sure all other windows
13668 showing that buffer will be redisplayed. */
13669 if (current_buffer->clip_changed)
13670 bset_update_mode_line (current_buffer);
13673 /* Normally the message* functions will have already displayed and
13674 updated the echo area, but the frame may have been trashed, or
13675 the update may have been preempted, so display the echo area
13676 again here. Checking message_cleared_p captures the case that
13677 the echo area should be cleared. */
13678 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13679 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13680 || (message_cleared_p
13681 && minibuf_level == 0
13682 /* If the mini-window is currently selected, this means the
13683 echo-area doesn't show through. */
13684 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13686 echo_area_display (false);
13688 /* If echo_area_display resizes the mini-window, the redisplay and
13689 window_sizes_changed flags of the selected frame are set, but
13690 it's too late for the hooks in window-size-change-functions,
13691 which have been examined already in prepare_menu_bars. So in
13692 that case we call the hooks here only for the selected frame. */
13693 if (sf->redisplay)
13695 ptrdiff_t count1 = SPECPDL_INDEX ();
13697 record_unwind_save_match_data ();
13698 run_window_size_change_functions (selected_frame);
13699 unbind_to (count1, Qnil);
13702 if (message_cleared_p)
13703 update_miniwindow_p = true;
13705 must_finish = true;
13707 /* If we don't display the current message, don't clear the
13708 message_cleared_p flag, because, if we did, we wouldn't clear
13709 the echo area in the next redisplay which doesn't preserve
13710 the echo area. */
13711 if (!display_last_displayed_message_p)
13712 message_cleared_p = false;
13714 else if (EQ (selected_window, minibuf_window)
13715 && (current_buffer->clip_changed || window_outdated (w))
13716 && resize_mini_window (w, false))
13718 if (sf->redisplay)
13720 ptrdiff_t count1 = SPECPDL_INDEX ();
13722 record_unwind_save_match_data ();
13723 run_window_size_change_functions (selected_frame);
13724 unbind_to (count1, Qnil);
13727 /* Resized active mini-window to fit the size of what it is
13728 showing if its contents might have changed. */
13729 must_finish = true;
13731 /* If window configuration was changed, frames may have been
13732 marked garbaged. Clear them or we will experience
13733 surprises wrt scrolling. */
13734 clear_garbaged_frames ();
13737 if (windows_or_buffers_changed && !update_mode_lines)
13738 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13739 only the windows's contents needs to be refreshed, or whether the
13740 mode-lines also need a refresh. */
13741 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13742 ? REDISPLAY_SOME : 32);
13744 /* If specs for an arrow have changed, do thorough redisplay
13745 to ensure we remove any arrow that should no longer exist. */
13746 if (overlay_arrows_changed_p ())
13747 /* Apparently, this is the only case where we update other windows,
13748 without updating other mode-lines. */
13749 windows_or_buffers_changed = 49;
13751 consider_all_windows_p = (update_mode_lines
13752 || windows_or_buffers_changed);
13754 #define AINC(a,i) \
13756 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13757 if (INTEGERP (entry)) \
13758 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13761 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13762 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13764 /* Optimize the case that only the line containing the cursor in the
13765 selected window has changed. Variables starting with this_ are
13766 set in display_line and record information about the line
13767 containing the cursor. */
13768 tlbufpos = this_line_start_pos;
13769 tlendpos = this_line_end_pos;
13770 if (!consider_all_windows_p
13771 && CHARPOS (tlbufpos) > 0
13772 && !w->update_mode_line
13773 && !current_buffer->clip_changed
13774 && !current_buffer->prevent_redisplay_optimizations_p
13775 && FRAME_VISIBLE_P (XFRAME (w->frame))
13776 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13777 && !XFRAME (w->frame)->cursor_type_changed
13778 && !XFRAME (w->frame)->face_change
13779 /* Make sure recorded data applies to current buffer, etc. */
13780 && this_line_buffer == current_buffer
13781 && match_p
13782 && !w->force_start
13783 && !w->optional_new_start
13784 /* Point must be on the line that we have info recorded about. */
13785 && PT >= CHARPOS (tlbufpos)
13786 && PT <= Z - CHARPOS (tlendpos)
13787 /* All text outside that line, including its final newline,
13788 must be unchanged. */
13789 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13790 CHARPOS (tlendpos)))
13792 if (CHARPOS (tlbufpos) > BEGV
13793 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13794 && (CHARPOS (tlbufpos) == ZV
13795 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13796 /* Former continuation line has disappeared by becoming empty. */
13797 goto cancel;
13798 else if (window_outdated (w) || MINI_WINDOW_P (w))
13800 /* We have to handle the case of continuation around a
13801 wide-column character (see the comment in indent.c around
13802 line 1340).
13804 For instance, in the following case:
13806 -------- Insert --------
13807 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13808 J_I_ ==> J_I_ `^^' are cursors.
13809 ^^ ^^
13810 -------- --------
13812 As we have to redraw the line above, we cannot use this
13813 optimization. */
13815 struct it it;
13816 int line_height_before = this_line_pixel_height;
13818 /* Note that start_display will handle the case that the
13819 line starting at tlbufpos is a continuation line. */
13820 start_display (&it, w, tlbufpos);
13822 /* Implementation note: It this still necessary? */
13823 if (it.current_x != this_line_start_x)
13824 goto cancel;
13826 TRACE ((stderr, "trying display optimization 1\n"));
13827 w->cursor.vpos = -1;
13828 overlay_arrow_seen = false;
13829 it.vpos = this_line_vpos;
13830 it.current_y = this_line_y;
13831 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13832 display_line (&it);
13834 /* If line contains point, is not continued,
13835 and ends at same distance from eob as before, we win. */
13836 if (w->cursor.vpos >= 0
13837 /* Line is not continued, otherwise this_line_start_pos
13838 would have been set to 0 in display_line. */
13839 && CHARPOS (this_line_start_pos)
13840 /* Line ends as before. */
13841 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13842 /* Line has same height as before. Otherwise other lines
13843 would have to be shifted up or down. */
13844 && this_line_pixel_height == line_height_before)
13846 /* If this is not the window's last line, we must adjust
13847 the charstarts of the lines below. */
13848 if (it.current_y < it.last_visible_y)
13850 struct glyph_row *row
13851 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13852 ptrdiff_t delta, delta_bytes;
13854 /* We used to distinguish between two cases here,
13855 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13856 when the line ends in a newline or the end of the
13857 buffer's accessible portion. But both cases did
13858 the same, so they were collapsed. */
13859 delta = (Z
13860 - CHARPOS (tlendpos)
13861 - MATRIX_ROW_START_CHARPOS (row));
13862 delta_bytes = (Z_BYTE
13863 - BYTEPOS (tlendpos)
13864 - MATRIX_ROW_START_BYTEPOS (row));
13866 increment_matrix_positions (w->current_matrix,
13867 this_line_vpos + 1,
13868 w->current_matrix->nrows,
13869 delta, delta_bytes);
13872 /* If this row displays text now but previously didn't,
13873 or vice versa, w->window_end_vpos may have to be
13874 adjusted. */
13875 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13877 if (w->window_end_vpos < this_line_vpos)
13878 w->window_end_vpos = this_line_vpos;
13880 else if (w->window_end_vpos == this_line_vpos
13881 && this_line_vpos > 0)
13882 w->window_end_vpos = this_line_vpos - 1;
13883 w->window_end_valid = false;
13885 /* Update hint: No need to try to scroll in update_window. */
13886 w->desired_matrix->no_scrolling_p = true;
13888 #ifdef GLYPH_DEBUG
13889 *w->desired_matrix->method = 0;
13890 debug_method_add (w, "optimization 1");
13891 #endif
13892 #ifdef HAVE_WINDOW_SYSTEM
13893 update_window_fringes (w, false);
13894 #endif
13895 goto update;
13897 else
13898 goto cancel;
13900 else if (/* Cursor position hasn't changed. */
13901 PT == w->last_point
13902 /* Make sure the cursor was last displayed
13903 in this window. Otherwise we have to reposition it. */
13905 /* PXW: Must be converted to pixels, probably. */
13906 && 0 <= w->cursor.vpos
13907 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13909 if (!must_finish)
13911 do_pending_window_change (true);
13912 /* If selected_window changed, redisplay again. */
13913 if (WINDOWP (selected_window)
13914 && (w = XWINDOW (selected_window)) != sw)
13915 goto retry;
13917 /* We used to always goto end_of_redisplay here, but this
13918 isn't enough if we have a blinking cursor. */
13919 if (w->cursor_off_p == w->last_cursor_off_p)
13920 goto end_of_redisplay;
13922 goto update;
13924 /* If highlighting the region, or if the cursor is in the echo area,
13925 then we can't just move the cursor. */
13926 else if (NILP (Vshow_trailing_whitespace)
13927 && !cursor_in_echo_area)
13929 struct it it;
13930 struct glyph_row *row;
13932 /* Skip from tlbufpos to PT and see where it is. Note that
13933 PT may be in invisible text. If so, we will end at the
13934 next visible position. */
13935 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13936 NULL, DEFAULT_FACE_ID);
13937 it.current_x = this_line_start_x;
13938 it.current_y = this_line_y;
13939 it.vpos = this_line_vpos;
13941 /* The call to move_it_to stops in front of PT, but
13942 moves over before-strings. */
13943 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13945 if (it.vpos == this_line_vpos
13946 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13947 row->enabled_p))
13949 eassert (this_line_vpos == it.vpos);
13950 eassert (this_line_y == it.current_y);
13951 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13952 if (cursor_row_fully_visible_p (w, false, true))
13954 #ifdef GLYPH_DEBUG
13955 *w->desired_matrix->method = 0;
13956 debug_method_add (w, "optimization 3");
13957 #endif
13958 goto update;
13960 else
13961 goto cancel;
13963 else
13964 goto cancel;
13967 cancel:
13968 /* Text changed drastically or point moved off of line. */
13969 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13972 CHARPOS (this_line_start_pos) = 0;
13973 ++clear_face_cache_count;
13974 #ifdef HAVE_WINDOW_SYSTEM
13975 ++clear_image_cache_count;
13976 #endif
13978 /* Build desired matrices, and update the display. If
13979 consider_all_windows_p, do it for all windows on all frames that
13980 require redisplay, as specified by their 'redisplay' flag.
13981 Otherwise do it for selected_window, only. */
13983 if (consider_all_windows_p)
13985 FOR_EACH_FRAME (tail, frame)
13986 XFRAME (frame)->updated_p = false;
13988 propagate_buffer_redisplay ();
13990 FOR_EACH_FRAME (tail, frame)
13992 struct frame *f = XFRAME (frame);
13994 /* We don't have to do anything for unselected terminal
13995 frames. */
13996 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13997 && !EQ (FRAME_TTY (f)->top_frame, frame))
13998 continue;
14000 retry_frame:
14001 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14003 bool gcscrollbars
14004 /* Only GC scrollbars when we redisplay the whole frame. */
14005 = f->redisplay || !REDISPLAY_SOME_P ();
14006 bool f_redisplay_flag = f->redisplay;
14007 /* Mark all the scroll bars to be removed; we'll redeem
14008 the ones we want when we redisplay their windows. */
14009 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14010 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14012 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14013 redisplay_windows (FRAME_ROOT_WINDOW (f));
14014 /* Remember that the invisible frames need to be redisplayed next
14015 time they're visible. */
14016 else if (!REDISPLAY_SOME_P ())
14017 f->redisplay = true;
14019 /* The X error handler may have deleted that frame. */
14020 if (!FRAME_LIVE_P (f))
14021 continue;
14023 /* Any scroll bars which redisplay_windows should have
14024 nuked should now go away. */
14025 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14026 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14028 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14030 /* If fonts changed on visible frame, display again. */
14031 if (f->fonts_changed)
14033 adjust_frame_glyphs (f);
14034 /* Disable all redisplay optimizations for this
14035 frame. For the reasons, see the comment near
14036 the previous call to adjust_frame_glyphs above. */
14037 SET_FRAME_GARBAGED (f);
14038 f->fonts_changed = false;
14039 goto retry_frame;
14042 /* See if we have to hscroll. */
14043 if (!f->already_hscrolled_p)
14045 f->already_hscrolled_p = true;
14046 if (hscroll_windows (f->root_window))
14047 goto retry_frame;
14050 /* If the frame's redisplay flag was not set before
14051 we went about redisplaying its windows, but it is
14052 set now, that means we employed some redisplay
14053 optimizations inside redisplay_windows, and
14054 bypassed producing some screen lines. But if
14055 f->redisplay is now set, it might mean the old
14056 faces are no longer valid (e.g., if redisplaying
14057 some window called some Lisp which defined a new
14058 face or redefined an existing face), so trying to
14059 use them in update_frame will segfault.
14060 Therefore, we must redisplay this frame. */
14061 if (!f_redisplay_flag && f->redisplay)
14062 goto retry_frame;
14064 /* Prevent various kinds of signals during display
14065 update. stdio is not robust about handling
14066 signals, which can cause an apparent I/O error. */
14067 if (interrupt_input)
14068 unrequest_sigio ();
14069 STOP_POLLING;
14071 pending |= update_frame (f, false, false);
14072 f->cursor_type_changed = false;
14073 f->updated_p = true;
14078 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14080 if (!pending)
14082 /* Do the mark_window_display_accurate after all windows have
14083 been redisplayed because this call resets flags in buffers
14084 which are needed for proper redisplay. */
14085 FOR_EACH_FRAME (tail, frame)
14087 struct frame *f = XFRAME (frame);
14088 if (f->updated_p)
14090 f->redisplay = false;
14091 mark_window_display_accurate (f->root_window, true);
14092 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14093 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14098 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14100 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14101 /* Use list_of_error, not Qerror, so that
14102 we catch only errors and don't run the debugger. */
14103 internal_condition_case_1 (redisplay_window_1, selected_window,
14104 list_of_error,
14105 redisplay_window_error);
14106 if (update_miniwindow_p)
14107 internal_condition_case_1 (redisplay_window_1,
14108 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14109 redisplay_window_error);
14111 /* Compare desired and current matrices, perform output. */
14113 update:
14114 /* If fonts changed, display again. Likewise if redisplay_window_1
14115 above caused some change (e.g., a change in faces) that requires
14116 considering the entire frame again. */
14117 if (sf->fonts_changed || sf->redisplay)
14119 if (sf->redisplay)
14121 /* Set this to force a more thorough redisplay.
14122 Otherwise, we might immediately loop back to the
14123 above "else-if" clause (since all the conditions that
14124 led here might still be true), and we will then
14125 infloop, because the selected-frame's redisplay flag
14126 is not (and cannot be) reset. */
14127 windows_or_buffers_changed = 50;
14129 goto retry;
14132 /* Prevent freeing of realized faces, since desired matrices are
14133 pending that reference the faces we computed and cached. */
14134 inhibit_free_realized_faces = true;
14136 /* Prevent various kinds of signals during display update.
14137 stdio is not robust about handling signals,
14138 which can cause an apparent I/O error. */
14139 if (interrupt_input)
14140 unrequest_sigio ();
14141 STOP_POLLING;
14143 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14145 if (hscroll_windows (selected_window))
14146 goto retry;
14148 XWINDOW (selected_window)->must_be_updated_p = true;
14149 pending = update_frame (sf, false, false);
14150 sf->cursor_type_changed = false;
14153 /* We may have called echo_area_display at the top of this
14154 function. If the echo area is on another frame, that may
14155 have put text on a frame other than the selected one, so the
14156 above call to update_frame would not have caught it. Catch
14157 it here. */
14158 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14159 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14161 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14163 XWINDOW (mini_window)->must_be_updated_p = true;
14164 pending |= update_frame (mini_frame, false, false);
14165 mini_frame->cursor_type_changed = false;
14166 if (!pending && hscroll_windows (mini_window))
14167 goto retry;
14171 /* If display was paused because of pending input, make sure we do a
14172 thorough update the next time. */
14173 if (pending)
14175 /* Prevent the optimization at the beginning of
14176 redisplay_internal that tries a single-line update of the
14177 line containing the cursor in the selected window. */
14178 CHARPOS (this_line_start_pos) = 0;
14180 /* Let the overlay arrow be updated the next time. */
14181 update_overlay_arrows (0);
14183 /* If we pause after scrolling, some rows in the current
14184 matrices of some windows are not valid. */
14185 if (!WINDOW_FULL_WIDTH_P (w)
14186 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14187 update_mode_lines = 36;
14189 else
14191 if (!consider_all_windows_p)
14193 /* This has already been done above if
14194 consider_all_windows_p is set. */
14195 if (XBUFFER (w->contents)->text->redisplay
14196 && buffer_window_count (XBUFFER (w->contents)) > 1)
14197 /* This can happen if b->text->redisplay was set during
14198 jit-lock. */
14199 propagate_buffer_redisplay ();
14200 mark_window_display_accurate_1 (w, true);
14202 /* Say overlay arrows are up to date. */
14203 update_overlay_arrows (1);
14205 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14206 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14209 update_mode_lines = 0;
14210 windows_or_buffers_changed = 0;
14213 /* Start SIGIO interrupts coming again. Having them off during the
14214 code above makes it less likely one will discard output, but not
14215 impossible, since there might be stuff in the system buffer here.
14216 But it is much hairier to try to do anything about that. */
14217 if (interrupt_input)
14218 request_sigio ();
14219 RESUME_POLLING;
14221 /* If a frame has become visible which was not before, redisplay
14222 again, so that we display it. Expose events for such a frame
14223 (which it gets when becoming visible) don't call the parts of
14224 redisplay constructing glyphs, so simply exposing a frame won't
14225 display anything in this case. So, we have to display these
14226 frames here explicitly. */
14227 if (!pending)
14229 int new_count = 0;
14231 FOR_EACH_FRAME (tail, frame)
14233 if (XFRAME (frame)->visible)
14234 new_count++;
14237 if (new_count != number_of_visible_frames)
14238 windows_or_buffers_changed = 52;
14241 /* Change frame size now if a change is pending. */
14242 do_pending_window_change (true);
14244 /* If we just did a pending size change, or have additional
14245 visible frames, or selected_window changed, redisplay again. */
14246 if ((windows_or_buffers_changed && !pending)
14247 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14248 goto retry;
14250 /* Clear the face and image caches.
14252 We used to do this only if consider_all_windows_p. But the cache
14253 needs to be cleared if a timer creates images in the current
14254 buffer (e.g. the test case in Bug#6230). */
14256 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14258 clear_face_cache (false);
14259 clear_face_cache_count = 0;
14262 #ifdef HAVE_WINDOW_SYSTEM
14263 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14265 clear_image_caches (Qnil);
14266 clear_image_cache_count = 0;
14268 #endif /* HAVE_WINDOW_SYSTEM */
14270 end_of_redisplay:
14271 #ifdef HAVE_NS
14272 ns_set_doc_edited ();
14273 #endif
14274 if (interrupt_input && interrupts_deferred)
14275 request_sigio ();
14277 unbind_to (count, Qnil);
14278 RESUME_POLLING;
14282 /* Redisplay, but leave alone any recent echo area message unless
14283 another message has been requested in its place.
14285 This is useful in situations where you need to redisplay but no
14286 user action has occurred, making it inappropriate for the message
14287 area to be cleared. See tracking_off and
14288 wait_reading_process_output for examples of these situations.
14290 FROM_WHERE is an integer saying from where this function was
14291 called. This is useful for debugging. */
14293 void
14294 redisplay_preserve_echo_area (int from_where)
14296 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14298 if (!NILP (echo_area_buffer[1]))
14300 /* We have a previously displayed message, but no current
14301 message. Redisplay the previous message. */
14302 display_last_displayed_message_p = true;
14303 redisplay_internal ();
14304 display_last_displayed_message_p = false;
14306 else
14307 redisplay_internal ();
14309 flush_frame (SELECTED_FRAME ());
14313 /* Function registered with record_unwind_protect in redisplay_internal. */
14315 static void
14316 unwind_redisplay (void)
14318 redisplaying_p = false;
14322 /* Mark the display of leaf window W as accurate or inaccurate.
14323 If ACCURATE_P, mark display of W as accurate.
14324 If !ACCURATE_P, arrange for W to be redisplayed the next
14325 time redisplay_internal is called. */
14327 static void
14328 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14330 struct buffer *b = XBUFFER (w->contents);
14332 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14333 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14334 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14336 if (accurate_p)
14338 b->clip_changed = false;
14339 b->prevent_redisplay_optimizations_p = false;
14340 eassert (buffer_window_count (b) > 0);
14341 /* Resetting b->text->redisplay is problematic!
14342 In order to make it safer to do it here, redisplay_internal must
14343 have copied all b->text->redisplay to their respective windows. */
14344 b->text->redisplay = false;
14346 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14347 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14348 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14349 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14351 w->current_matrix->buffer = b;
14352 w->current_matrix->begv = BUF_BEGV (b);
14353 w->current_matrix->zv = BUF_ZV (b);
14355 w->last_cursor_vpos = w->cursor.vpos;
14356 w->last_cursor_off_p = w->cursor_off_p;
14358 if (w == XWINDOW (selected_window))
14359 w->last_point = BUF_PT (b);
14360 else
14361 w->last_point = marker_position (w->pointm);
14363 w->window_end_valid = true;
14364 w->update_mode_line = false;
14367 w->redisplay = !accurate_p;
14371 /* Mark the display of windows in the window tree rooted at WINDOW as
14372 accurate or inaccurate. If ACCURATE_P, mark display of
14373 windows as accurate. If !ACCURATE_P, arrange for windows to
14374 be redisplayed the next time redisplay_internal is called. */
14376 void
14377 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14379 struct window *w;
14381 for (; !NILP (window); window = w->next)
14383 w = XWINDOW (window);
14384 if (WINDOWP (w->contents))
14385 mark_window_display_accurate (w->contents, accurate_p);
14386 else
14387 mark_window_display_accurate_1 (w, accurate_p);
14390 if (accurate_p)
14391 update_overlay_arrows (1);
14392 else
14393 /* Force a thorough redisplay the next time by setting
14394 last_arrow_position and last_arrow_string to t, which is
14395 unequal to any useful value of Voverlay_arrow_... */
14396 update_overlay_arrows (-1);
14400 /* Return value in display table DP (Lisp_Char_Table *) for character
14401 C. Since a display table doesn't have any parent, we don't have to
14402 follow parent. Do not call this function directly but use the
14403 macro DISP_CHAR_VECTOR. */
14405 Lisp_Object
14406 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14408 Lisp_Object val;
14410 if (ASCII_CHAR_P (c))
14412 val = dp->ascii;
14413 if (SUB_CHAR_TABLE_P (val))
14414 val = XSUB_CHAR_TABLE (val)->contents[c];
14416 else
14418 Lisp_Object table;
14420 XSETCHAR_TABLE (table, dp);
14421 val = char_table_ref (table, c);
14423 if (NILP (val))
14424 val = dp->defalt;
14425 return val;
14430 /***********************************************************************
14431 Window Redisplay
14432 ***********************************************************************/
14434 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14436 static void
14437 redisplay_windows (Lisp_Object window)
14439 while (!NILP (window))
14441 struct window *w = XWINDOW (window);
14443 if (WINDOWP (w->contents))
14444 redisplay_windows (w->contents);
14445 else if (BUFFERP (w->contents))
14447 displayed_buffer = XBUFFER (w->contents);
14448 /* Use list_of_error, not Qerror, so that
14449 we catch only errors and don't run the debugger. */
14450 internal_condition_case_1 (redisplay_window_0, window,
14451 list_of_error,
14452 redisplay_window_error);
14455 window = w->next;
14459 static Lisp_Object
14460 redisplay_window_error (Lisp_Object ignore)
14462 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14463 return Qnil;
14466 static Lisp_Object
14467 redisplay_window_0 (Lisp_Object window)
14469 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14470 redisplay_window (window, false);
14471 return Qnil;
14474 static Lisp_Object
14475 redisplay_window_1 (Lisp_Object window)
14477 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14478 redisplay_window (window, true);
14479 return Qnil;
14483 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14484 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14485 which positions recorded in ROW differ from current buffer
14486 positions.
14488 Return true iff cursor is on this row. */
14490 static bool
14491 set_cursor_from_row (struct window *w, struct glyph_row *row,
14492 struct glyph_matrix *matrix,
14493 ptrdiff_t delta, ptrdiff_t delta_bytes,
14494 int dy, int dvpos)
14496 struct glyph *glyph = row->glyphs[TEXT_AREA];
14497 struct glyph *end = glyph + row->used[TEXT_AREA];
14498 struct glyph *cursor = NULL;
14499 /* The last known character position in row. */
14500 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14501 int x = row->x;
14502 ptrdiff_t pt_old = PT - delta;
14503 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14504 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14505 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14506 /* A glyph beyond the edge of TEXT_AREA which we should never
14507 touch. */
14508 struct glyph *glyphs_end = end;
14509 /* True means we've found a match for cursor position, but that
14510 glyph has the avoid_cursor_p flag set. */
14511 bool match_with_avoid_cursor = false;
14512 /* True means we've seen at least one glyph that came from a
14513 display string. */
14514 bool string_seen = false;
14515 /* Largest and smallest buffer positions seen so far during scan of
14516 glyph row. */
14517 ptrdiff_t bpos_max = pos_before;
14518 ptrdiff_t bpos_min = pos_after;
14519 /* Last buffer position covered by an overlay string with an integer
14520 `cursor' property. */
14521 ptrdiff_t bpos_covered = 0;
14522 /* True means the display string on which to display the cursor
14523 comes from a text property, not from an overlay. */
14524 bool string_from_text_prop = false;
14526 /* Don't even try doing anything if called for a mode-line or
14527 header-line row, since the rest of the code isn't prepared to
14528 deal with such calamities. */
14529 eassert (!row->mode_line_p);
14530 if (row->mode_line_p)
14531 return false;
14533 /* Skip over glyphs not having an object at the start and the end of
14534 the row. These are special glyphs like truncation marks on
14535 terminal frames. */
14536 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14538 if (!row->reversed_p)
14540 while (glyph < end
14541 && NILP (glyph->object)
14542 && glyph->charpos < 0)
14544 x += glyph->pixel_width;
14545 ++glyph;
14547 while (end > glyph
14548 && NILP ((end - 1)->object)
14549 /* CHARPOS is zero for blanks and stretch glyphs
14550 inserted by extend_face_to_end_of_line. */
14551 && (end - 1)->charpos <= 0)
14552 --end;
14553 glyph_before = glyph - 1;
14554 glyph_after = end;
14556 else
14558 struct glyph *g;
14560 /* If the glyph row is reversed, we need to process it from back
14561 to front, so swap the edge pointers. */
14562 glyphs_end = end = glyph - 1;
14563 glyph += row->used[TEXT_AREA] - 1;
14565 while (glyph > end + 1
14566 && NILP (glyph->object)
14567 && glyph->charpos < 0)
14569 --glyph;
14570 x -= glyph->pixel_width;
14572 if (NILP (glyph->object) && glyph->charpos < 0)
14573 --glyph;
14574 /* By default, in reversed rows we put the cursor on the
14575 rightmost (first in the reading order) glyph. */
14576 for (g = end + 1; g < glyph; g++)
14577 x += g->pixel_width;
14578 while (end < glyph
14579 && NILP ((end + 1)->object)
14580 && (end + 1)->charpos <= 0)
14581 ++end;
14582 glyph_before = glyph + 1;
14583 glyph_after = end;
14586 else if (row->reversed_p)
14588 /* In R2L rows that don't display text, put the cursor on the
14589 rightmost glyph. Case in point: an empty last line that is
14590 part of an R2L paragraph. */
14591 cursor = end - 1;
14592 /* Avoid placing the cursor on the last glyph of the row, where
14593 on terminal frames we hold the vertical border between
14594 adjacent windows. */
14595 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14596 && !WINDOW_RIGHTMOST_P (w)
14597 && cursor == row->glyphs[LAST_AREA] - 1)
14598 cursor--;
14599 x = -1; /* will be computed below, at label compute_x */
14602 /* Step 1: Try to find the glyph whose character position
14603 corresponds to point. If that's not possible, find 2 glyphs
14604 whose character positions are the closest to point, one before
14605 point, the other after it. */
14606 if (!row->reversed_p)
14607 while (/* not marched to end of glyph row */
14608 glyph < end
14609 /* glyph was not inserted by redisplay for internal purposes */
14610 && !NILP (glyph->object))
14612 if (BUFFERP (glyph->object))
14614 ptrdiff_t dpos = glyph->charpos - pt_old;
14616 if (glyph->charpos > bpos_max)
14617 bpos_max = glyph->charpos;
14618 if (glyph->charpos < bpos_min)
14619 bpos_min = glyph->charpos;
14620 if (!glyph->avoid_cursor_p)
14622 /* If we hit point, we've found the glyph on which to
14623 display the cursor. */
14624 if (dpos == 0)
14626 match_with_avoid_cursor = false;
14627 break;
14629 /* See if we've found a better approximation to
14630 POS_BEFORE or to POS_AFTER. */
14631 if (0 > dpos && dpos > pos_before - pt_old)
14633 pos_before = glyph->charpos;
14634 glyph_before = glyph;
14636 else if (0 < dpos && dpos < pos_after - pt_old)
14638 pos_after = glyph->charpos;
14639 glyph_after = glyph;
14642 else if (dpos == 0)
14643 match_with_avoid_cursor = true;
14645 else if (STRINGP (glyph->object))
14647 Lisp_Object chprop;
14648 ptrdiff_t glyph_pos = glyph->charpos;
14650 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14651 glyph->object);
14652 if (!NILP (chprop))
14654 /* If the string came from a `display' text property,
14655 look up the buffer position of that property and
14656 use that position to update bpos_max, as if we
14657 actually saw such a position in one of the row's
14658 glyphs. This helps with supporting integer values
14659 of `cursor' property on the display string in
14660 situations where most or all of the row's buffer
14661 text is completely covered by display properties,
14662 so that no glyph with valid buffer positions is
14663 ever seen in the row. */
14664 ptrdiff_t prop_pos =
14665 string_buffer_position_lim (glyph->object, pos_before,
14666 pos_after, false);
14668 if (prop_pos >= pos_before)
14669 bpos_max = prop_pos;
14671 if (INTEGERP (chprop))
14673 bpos_covered = bpos_max + XINT (chprop);
14674 /* If the `cursor' property covers buffer positions up
14675 to and including point, we should display cursor on
14676 this glyph. Note that, if a `cursor' property on one
14677 of the string's characters has an integer value, we
14678 will break out of the loop below _before_ we get to
14679 the position match above. IOW, integer values of
14680 the `cursor' property override the "exact match for
14681 point" strategy of positioning the cursor. */
14682 /* Implementation note: bpos_max == pt_old when, e.g.,
14683 we are in an empty line, where bpos_max is set to
14684 MATRIX_ROW_START_CHARPOS, see above. */
14685 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14687 cursor = glyph;
14688 break;
14692 string_seen = true;
14694 x += glyph->pixel_width;
14695 ++glyph;
14697 else if (glyph > end) /* row is reversed */
14698 while (!NILP (glyph->object))
14700 if (BUFFERP (glyph->object))
14702 ptrdiff_t dpos = glyph->charpos - pt_old;
14704 if (glyph->charpos > bpos_max)
14705 bpos_max = glyph->charpos;
14706 if (glyph->charpos < bpos_min)
14707 bpos_min = glyph->charpos;
14708 if (!glyph->avoid_cursor_p)
14710 if (dpos == 0)
14712 match_with_avoid_cursor = false;
14713 break;
14715 if (0 > dpos && dpos > pos_before - pt_old)
14717 pos_before = glyph->charpos;
14718 glyph_before = glyph;
14720 else if (0 < dpos && dpos < pos_after - pt_old)
14722 pos_after = glyph->charpos;
14723 glyph_after = glyph;
14726 else if (dpos == 0)
14727 match_with_avoid_cursor = true;
14729 else if (STRINGP (glyph->object))
14731 Lisp_Object chprop;
14732 ptrdiff_t glyph_pos = glyph->charpos;
14734 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14735 glyph->object);
14736 if (!NILP (chprop))
14738 ptrdiff_t prop_pos =
14739 string_buffer_position_lim (glyph->object, pos_before,
14740 pos_after, false);
14742 if (prop_pos >= pos_before)
14743 bpos_max = prop_pos;
14745 if (INTEGERP (chprop))
14747 bpos_covered = bpos_max + XINT (chprop);
14748 /* If the `cursor' property covers buffer positions up
14749 to and including point, we should display cursor on
14750 this glyph. */
14751 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14753 cursor = glyph;
14754 break;
14757 string_seen = true;
14759 --glyph;
14760 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14762 x--; /* can't use any pixel_width */
14763 break;
14765 x -= glyph->pixel_width;
14768 /* Step 2: If we didn't find an exact match for point, we need to
14769 look for a proper place to put the cursor among glyphs between
14770 GLYPH_BEFORE and GLYPH_AFTER. */
14771 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14772 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14773 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14775 /* An empty line has a single glyph whose OBJECT is nil and
14776 whose CHARPOS is the position of a newline on that line.
14777 Note that on a TTY, there are more glyphs after that, which
14778 were produced by extend_face_to_end_of_line, but their
14779 CHARPOS is zero or negative. */
14780 bool empty_line_p =
14781 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14782 && NILP (glyph->object) && glyph->charpos > 0
14783 /* On a TTY, continued and truncated rows also have a glyph at
14784 their end whose OBJECT is nil and whose CHARPOS is
14785 positive (the continuation and truncation glyphs), but such
14786 rows are obviously not "empty". */
14787 && !(row->continued_p || row->truncated_on_right_p));
14789 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14791 ptrdiff_t ellipsis_pos;
14793 /* Scan back over the ellipsis glyphs. */
14794 if (!row->reversed_p)
14796 ellipsis_pos = (glyph - 1)->charpos;
14797 while (glyph > row->glyphs[TEXT_AREA]
14798 && (glyph - 1)->charpos == ellipsis_pos)
14799 glyph--, x -= glyph->pixel_width;
14800 /* That loop always goes one position too far, including
14801 the glyph before the ellipsis. So scan forward over
14802 that one. */
14803 x += glyph->pixel_width;
14804 glyph++;
14806 else /* row is reversed */
14808 ellipsis_pos = (glyph + 1)->charpos;
14809 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14810 && (glyph + 1)->charpos == ellipsis_pos)
14811 glyph++, x += glyph->pixel_width;
14812 x -= glyph->pixel_width;
14813 glyph--;
14816 else if (match_with_avoid_cursor)
14818 cursor = glyph_after;
14819 x = -1;
14821 else if (string_seen)
14823 int incr = row->reversed_p ? -1 : +1;
14825 /* Need to find the glyph that came out of a string which is
14826 present at point. That glyph is somewhere between
14827 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14828 positioned between POS_BEFORE and POS_AFTER in the
14829 buffer. */
14830 struct glyph *start, *stop;
14831 ptrdiff_t pos = pos_before;
14833 x = -1;
14835 /* If the row ends in a newline from a display string,
14836 reordering could have moved the glyphs belonging to the
14837 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14838 in this case we extend the search to the last glyph in
14839 the row that was not inserted by redisplay. */
14840 if (row->ends_in_newline_from_string_p)
14842 glyph_after = end;
14843 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14846 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14847 correspond to POS_BEFORE and POS_AFTER, respectively. We
14848 need START and STOP in the order that corresponds to the
14849 row's direction as given by its reversed_p flag. If the
14850 directionality of characters between POS_BEFORE and
14851 POS_AFTER is the opposite of the row's base direction,
14852 these characters will have been reordered for display,
14853 and we need to reverse START and STOP. */
14854 if (!row->reversed_p)
14856 start = min (glyph_before, glyph_after);
14857 stop = max (glyph_before, glyph_after);
14859 else
14861 start = max (glyph_before, glyph_after);
14862 stop = min (glyph_before, glyph_after);
14864 for (glyph = start + incr;
14865 row->reversed_p ? glyph > stop : glyph < stop; )
14868 /* Any glyphs that come from the buffer are here because
14869 of bidi reordering. Skip them, and only pay
14870 attention to glyphs that came from some string. */
14871 if (STRINGP (glyph->object))
14873 Lisp_Object str;
14874 ptrdiff_t tem;
14875 /* If the display property covers the newline, we
14876 need to search for it one position farther. */
14877 ptrdiff_t lim = pos_after
14878 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14880 string_from_text_prop = false;
14881 str = glyph->object;
14882 tem = string_buffer_position_lim (str, pos, lim, false);
14883 if (tem == 0 /* from overlay */
14884 || pos <= tem)
14886 /* If the string from which this glyph came is
14887 found in the buffer at point, or at position
14888 that is closer to point than pos_after, then
14889 we've found the glyph we've been looking for.
14890 If it comes from an overlay (tem == 0), and
14891 it has the `cursor' property on one of its
14892 glyphs, record that glyph as a candidate for
14893 displaying the cursor. (As in the
14894 unidirectional version, we will display the
14895 cursor on the last candidate we find.) */
14896 if (tem == 0
14897 || tem == pt_old
14898 || (tem - pt_old > 0 && tem < pos_after))
14900 /* The glyphs from this string could have
14901 been reordered. Find the one with the
14902 smallest string position. Or there could
14903 be a character in the string with the
14904 `cursor' property, which means display
14905 cursor on that character's glyph. */
14906 ptrdiff_t strpos = glyph->charpos;
14908 if (tem)
14910 cursor = glyph;
14911 string_from_text_prop = true;
14913 for ( ;
14914 (row->reversed_p ? glyph > stop : glyph < stop)
14915 && EQ (glyph->object, str);
14916 glyph += incr)
14918 Lisp_Object cprop;
14919 ptrdiff_t gpos = glyph->charpos;
14921 cprop = Fget_char_property (make_number (gpos),
14922 Qcursor,
14923 glyph->object);
14924 if (!NILP (cprop))
14926 cursor = glyph;
14927 break;
14929 if (tem && glyph->charpos < strpos)
14931 strpos = glyph->charpos;
14932 cursor = glyph;
14936 if (tem == pt_old
14937 || (tem - pt_old > 0 && tem < pos_after))
14938 goto compute_x;
14940 if (tem)
14941 pos = tem + 1; /* don't find previous instances */
14943 /* This string is not what we want; skip all of the
14944 glyphs that came from it. */
14945 while ((row->reversed_p ? glyph > stop : glyph < stop)
14946 && EQ (glyph->object, str))
14947 glyph += incr;
14949 else
14950 glyph += incr;
14953 /* If we reached the end of the line, and END was from a string,
14954 the cursor is not on this line. */
14955 if (cursor == NULL
14956 && (row->reversed_p ? glyph <= end : glyph >= end)
14957 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14958 && STRINGP (end->object)
14959 && row->continued_p)
14960 return false;
14962 /* A truncated row may not include PT among its character positions.
14963 Setting the cursor inside the scroll margin will trigger
14964 recalculation of hscroll in hscroll_window_tree. But if a
14965 display string covers point, defer to the string-handling
14966 code below to figure this out. */
14967 else if (row->truncated_on_left_p && pt_old < bpos_min)
14969 cursor = glyph_before;
14970 x = -1;
14972 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14973 /* Zero-width characters produce no glyphs. */
14974 || (!empty_line_p
14975 && (row->reversed_p
14976 ? glyph_after > glyphs_end
14977 : glyph_after < glyphs_end)))
14979 cursor = glyph_after;
14980 x = -1;
14984 compute_x:
14985 if (cursor != NULL)
14986 glyph = cursor;
14987 else if (glyph == glyphs_end
14988 && pos_before == pos_after
14989 && STRINGP ((row->reversed_p
14990 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14991 : row->glyphs[TEXT_AREA])->object))
14993 /* If all the glyphs of this row came from strings, put the
14994 cursor on the first glyph of the row. This avoids having the
14995 cursor outside of the text area in this very rare and hard
14996 use case. */
14997 glyph =
14998 row->reversed_p
14999 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15000 : row->glyphs[TEXT_AREA];
15002 if (x < 0)
15004 struct glyph *g;
15006 /* Need to compute x that corresponds to GLYPH. */
15007 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15009 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15010 emacs_abort ();
15011 x += g->pixel_width;
15015 /* ROW could be part of a continued line, which, under bidi
15016 reordering, might have other rows whose start and end charpos
15017 occlude point. Only set w->cursor if we found a better
15018 approximation to the cursor position than we have from previously
15019 examined candidate rows belonging to the same continued line. */
15020 if (/* We already have a candidate row. */
15021 w->cursor.vpos >= 0
15022 /* That candidate is not the row we are processing. */
15023 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15024 /* Make sure cursor.vpos specifies a row whose start and end
15025 charpos occlude point, and it is valid candidate for being a
15026 cursor-row. This is because some callers of this function
15027 leave cursor.vpos at the row where the cursor was displayed
15028 during the last redisplay cycle. */
15029 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15030 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15031 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15033 struct glyph *g1
15034 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15036 /* Don't consider glyphs that are outside TEXT_AREA. */
15037 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15038 return false;
15039 /* Keep the candidate whose buffer position is the closest to
15040 point or has the `cursor' property. */
15041 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15042 w->cursor.hpos >= 0
15043 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15044 && ((BUFFERP (g1->object)
15045 && (g1->charpos == pt_old /* An exact match always wins. */
15046 || (BUFFERP (glyph->object)
15047 && eabs (g1->charpos - pt_old)
15048 < eabs (glyph->charpos - pt_old))))
15049 /* Previous candidate is a glyph from a string that has
15050 a non-nil `cursor' property. */
15051 || (STRINGP (g1->object)
15052 && (!NILP (Fget_char_property (make_number (g1->charpos),
15053 Qcursor, g1->object))
15054 /* Previous candidate is from the same display
15055 string as this one, and the display string
15056 came from a text property. */
15057 || (EQ (g1->object, glyph->object)
15058 && string_from_text_prop)
15059 /* this candidate is from newline and its
15060 position is not an exact match */
15061 || (NILP (glyph->object)
15062 && glyph->charpos != pt_old)))))
15063 return false;
15064 /* If this candidate gives an exact match, use that. */
15065 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15066 /* If this candidate is a glyph created for the
15067 terminating newline of a line, and point is on that
15068 newline, it wins because it's an exact match. */
15069 || (!row->continued_p
15070 && NILP (glyph->object)
15071 && glyph->charpos == 0
15072 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15073 /* Otherwise, keep the candidate that comes from a row
15074 spanning less buffer positions. This may win when one or
15075 both candidate positions are on glyphs that came from
15076 display strings, for which we cannot compare buffer
15077 positions. */
15078 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15079 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15080 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15081 return false;
15083 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15084 w->cursor.x = x;
15085 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15086 w->cursor.y = row->y + dy;
15088 if (w == XWINDOW (selected_window))
15090 if (!row->continued_p
15091 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15092 && row->x == 0)
15094 this_line_buffer = XBUFFER (w->contents);
15096 CHARPOS (this_line_start_pos)
15097 = MATRIX_ROW_START_CHARPOS (row) + delta;
15098 BYTEPOS (this_line_start_pos)
15099 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15101 CHARPOS (this_line_end_pos)
15102 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15103 BYTEPOS (this_line_end_pos)
15104 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15106 this_line_y = w->cursor.y;
15107 this_line_pixel_height = row->height;
15108 this_line_vpos = w->cursor.vpos;
15109 this_line_start_x = row->x;
15111 else
15112 CHARPOS (this_line_start_pos) = 0;
15115 return true;
15119 /* Run window scroll functions, if any, for WINDOW with new window
15120 start STARTP. Sets the window start of WINDOW to that position.
15122 We assume that the window's buffer is really current. */
15124 static struct text_pos
15125 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15127 struct window *w = XWINDOW (window);
15128 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15130 eassert (current_buffer == XBUFFER (w->contents));
15132 if (!NILP (Vwindow_scroll_functions))
15134 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15135 make_number (CHARPOS (startp)));
15136 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15137 /* In case the hook functions switch buffers. */
15138 set_buffer_internal (XBUFFER (w->contents));
15141 return startp;
15145 /* Make sure the line containing the cursor is fully visible.
15146 A value of true means there is nothing to be done.
15147 (Either the line is fully visible, or it cannot be made so,
15148 or we cannot tell.)
15150 If FORCE_P, return false even if partial visible cursor row
15151 is higher than window.
15153 If CURRENT_MATRIX_P, use the information from the
15154 window's current glyph matrix; otherwise use the desired glyph
15155 matrix.
15157 A value of false means the caller should do scrolling
15158 as if point had gone off the screen. */
15160 static bool
15161 cursor_row_fully_visible_p (struct window *w, bool force_p,
15162 bool current_matrix_p)
15164 struct glyph_matrix *matrix;
15165 struct glyph_row *row;
15166 int window_height;
15168 if (!make_cursor_line_fully_visible_p)
15169 return true;
15171 /* It's not always possible to find the cursor, e.g, when a window
15172 is full of overlay strings. Don't do anything in that case. */
15173 if (w->cursor.vpos < 0)
15174 return true;
15176 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15177 row = MATRIX_ROW (matrix, w->cursor.vpos);
15179 /* If the cursor row is not partially visible, there's nothing to do. */
15180 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15181 return true;
15183 /* If the row the cursor is in is taller than the window's height,
15184 it's not clear what to do, so do nothing. */
15185 window_height = window_box_height (w);
15186 if (row->height >= window_height)
15188 if (!force_p || MINI_WINDOW_P (w)
15189 || w->vscroll || w->cursor.vpos == 0)
15190 return true;
15192 return false;
15196 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15197 means only WINDOW is redisplayed in redisplay_internal.
15198 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15199 in redisplay_window to bring a partially visible line into view in
15200 the case that only the cursor has moved.
15202 LAST_LINE_MISFIT should be true if we're scrolling because the
15203 last screen line's vertical height extends past the end of the screen.
15205 Value is
15207 1 if scrolling succeeded
15209 0 if scrolling didn't find point.
15211 -1 if new fonts have been loaded so that we must interrupt
15212 redisplay, adjust glyph matrices, and try again. */
15214 enum
15216 SCROLLING_SUCCESS,
15217 SCROLLING_FAILED,
15218 SCROLLING_NEED_LARGER_MATRICES
15221 /* If scroll-conservatively is more than this, never recenter.
15223 If you change this, don't forget to update the doc string of
15224 `scroll-conservatively' and the Emacs manual. */
15225 #define SCROLL_LIMIT 100
15227 static int
15228 try_scrolling (Lisp_Object window, bool just_this_one_p,
15229 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15230 bool temp_scroll_step, bool last_line_misfit)
15232 struct window *w = XWINDOW (window);
15233 struct frame *f = XFRAME (w->frame);
15234 struct text_pos pos, startp;
15235 struct it it;
15236 int this_scroll_margin, scroll_max, rc, height;
15237 int dy = 0, amount_to_scroll = 0;
15238 bool scroll_down_p = false;
15239 int extra_scroll_margin_lines = last_line_misfit;
15240 Lisp_Object aggressive;
15241 /* We will never try scrolling more than this number of lines. */
15242 int scroll_limit = SCROLL_LIMIT;
15243 int frame_line_height = default_line_pixel_height (w);
15244 int window_total_lines
15245 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15247 #ifdef GLYPH_DEBUG
15248 debug_method_add (w, "try_scrolling");
15249 #endif
15251 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15253 /* Compute scroll margin height in pixels. We scroll when point is
15254 within this distance from the top or bottom of the window. */
15255 if (scroll_margin > 0)
15256 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15257 * frame_line_height;
15258 else
15259 this_scroll_margin = 0;
15261 /* Force arg_scroll_conservatively to have a reasonable value, to
15262 avoid scrolling too far away with slow move_it_* functions. Note
15263 that the user can supply scroll-conservatively equal to
15264 `most-positive-fixnum', which can be larger than INT_MAX. */
15265 if (arg_scroll_conservatively > scroll_limit)
15267 arg_scroll_conservatively = scroll_limit + 1;
15268 scroll_max = scroll_limit * frame_line_height;
15270 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15271 /* Compute how much we should try to scroll maximally to bring
15272 point into view. */
15273 scroll_max = (max (scroll_step,
15274 max (arg_scroll_conservatively, temp_scroll_step))
15275 * frame_line_height);
15276 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15277 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15278 /* We're trying to scroll because of aggressive scrolling but no
15279 scroll_step is set. Choose an arbitrary one. */
15280 scroll_max = 10 * frame_line_height;
15281 else
15282 scroll_max = 0;
15284 too_near_end:
15286 /* Decide whether to scroll down. */
15287 if (PT > CHARPOS (startp))
15289 int scroll_margin_y;
15291 /* Compute the pixel ypos of the scroll margin, then move IT to
15292 either that ypos or PT, whichever comes first. */
15293 start_display (&it, w, startp);
15294 scroll_margin_y = it.last_visible_y - this_scroll_margin
15295 - frame_line_height * extra_scroll_margin_lines;
15296 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15297 (MOVE_TO_POS | MOVE_TO_Y));
15299 if (PT > CHARPOS (it.current.pos))
15301 int y0 = line_bottom_y (&it);
15302 /* Compute how many pixels below window bottom to stop searching
15303 for PT. This avoids costly search for PT that is far away if
15304 the user limited scrolling by a small number of lines, but
15305 always finds PT if scroll_conservatively is set to a large
15306 number, such as most-positive-fixnum. */
15307 int slack = max (scroll_max, 10 * frame_line_height);
15308 int y_to_move = it.last_visible_y + slack;
15310 /* Compute the distance from the scroll margin to PT or to
15311 the scroll limit, whichever comes first. This should
15312 include the height of the cursor line, to make that line
15313 fully visible. */
15314 move_it_to (&it, PT, -1, y_to_move,
15315 -1, MOVE_TO_POS | MOVE_TO_Y);
15316 dy = line_bottom_y (&it) - y0;
15318 if (dy > scroll_max)
15319 return SCROLLING_FAILED;
15321 if (dy > 0)
15322 scroll_down_p = true;
15324 else if (PT == IT_CHARPOS (it)
15325 && IT_CHARPOS (it) < ZV
15326 && it.method == GET_FROM_STRING
15327 && arg_scroll_conservatively > scroll_limit
15328 && it.current_x == 0)
15330 enum move_it_result skip;
15331 int y1 = it.current_y;
15332 int vpos;
15334 /* A before-string that includes newlines and is displayed
15335 on the last visible screen line could fail us under
15336 scroll-conservatively > 100, because we will be unable to
15337 position the cursor on that last visible line. Try to
15338 recover by finding the first screen line that has some
15339 glyphs coming from the buffer text. */
15340 do {
15341 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15342 if (skip != MOVE_NEWLINE_OR_CR
15343 || IT_CHARPOS (it) != PT
15344 || it.method == GET_FROM_BUFFER)
15345 break;
15346 vpos = it.vpos;
15347 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15348 } while (it.vpos > vpos);
15350 dy = it.current_y - y1;
15352 if (dy > scroll_max)
15353 return SCROLLING_FAILED;
15355 if (dy > 0)
15356 scroll_down_p = true;
15360 if (scroll_down_p)
15362 /* Point is in or below the bottom scroll margin, so move the
15363 window start down. If scrolling conservatively, move it just
15364 enough down to make point visible. If scroll_step is set,
15365 move it down by scroll_step. */
15366 if (arg_scroll_conservatively)
15367 amount_to_scroll
15368 = min (max (dy, frame_line_height),
15369 frame_line_height * arg_scroll_conservatively);
15370 else if (scroll_step || temp_scroll_step)
15371 amount_to_scroll = scroll_max;
15372 else
15374 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15375 height = WINDOW_BOX_TEXT_HEIGHT (w);
15376 if (NUMBERP (aggressive))
15378 double float_amount = XFLOATINT (aggressive) * height;
15379 int aggressive_scroll = float_amount;
15380 if (aggressive_scroll == 0 && float_amount > 0)
15381 aggressive_scroll = 1;
15382 /* Don't let point enter the scroll margin near top of
15383 the window. This could happen if the value of
15384 scroll_up_aggressively is too large and there are
15385 non-zero margins, because scroll_up_aggressively
15386 means put point that fraction of window height
15387 _from_the_bottom_margin_. */
15388 if (aggressive_scroll + 2 * this_scroll_margin > height)
15389 aggressive_scroll = height - 2 * this_scroll_margin;
15390 amount_to_scroll = dy + aggressive_scroll;
15394 if (amount_to_scroll <= 0)
15395 return SCROLLING_FAILED;
15397 start_display (&it, w, startp);
15398 if (arg_scroll_conservatively <= scroll_limit)
15399 move_it_vertically (&it, amount_to_scroll);
15400 else
15402 /* Extra precision for users who set scroll-conservatively
15403 to a large number: make sure the amount we scroll
15404 the window start is never less than amount_to_scroll,
15405 which was computed as distance from window bottom to
15406 point. This matters when lines at window top and lines
15407 below window bottom have different height. */
15408 struct it it1;
15409 void *it1data = NULL;
15410 /* We use a temporary it1 because line_bottom_y can modify
15411 its argument, if it moves one line down; see there. */
15412 int start_y;
15414 SAVE_IT (it1, it, it1data);
15415 start_y = line_bottom_y (&it1);
15416 do {
15417 RESTORE_IT (&it, &it, it1data);
15418 move_it_by_lines (&it, 1);
15419 SAVE_IT (it1, it, it1data);
15420 } while (IT_CHARPOS (it) < ZV
15421 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15422 bidi_unshelve_cache (it1data, true);
15425 /* If STARTP is unchanged, move it down another screen line. */
15426 if (IT_CHARPOS (it) == CHARPOS (startp))
15427 move_it_by_lines (&it, 1);
15428 startp = it.current.pos;
15430 else
15432 struct text_pos scroll_margin_pos = startp;
15433 int y_offset = 0;
15435 /* See if point is inside the scroll margin at the top of the
15436 window. */
15437 if (this_scroll_margin)
15439 int y_start;
15441 start_display (&it, w, startp);
15442 y_start = it.current_y;
15443 move_it_vertically (&it, this_scroll_margin);
15444 scroll_margin_pos = it.current.pos;
15445 /* If we didn't move enough before hitting ZV, request
15446 additional amount of scroll, to move point out of the
15447 scroll margin. */
15448 if (IT_CHARPOS (it) == ZV
15449 && it.current_y - y_start < this_scroll_margin)
15450 y_offset = this_scroll_margin - (it.current_y - y_start);
15453 if (PT < CHARPOS (scroll_margin_pos))
15455 /* Point is in the scroll margin at the top of the window or
15456 above what is displayed in the window. */
15457 int y0, y_to_move;
15459 /* Compute the vertical distance from PT to the scroll
15460 margin position. Move as far as scroll_max allows, or
15461 one screenful, or 10 screen lines, whichever is largest.
15462 Give up if distance is greater than scroll_max or if we
15463 didn't reach the scroll margin position. */
15464 SET_TEXT_POS (pos, PT, PT_BYTE);
15465 start_display (&it, w, pos);
15466 y0 = it.current_y;
15467 y_to_move = max (it.last_visible_y,
15468 max (scroll_max, 10 * frame_line_height));
15469 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15470 y_to_move, -1,
15471 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15472 dy = it.current_y - y0;
15473 if (dy > scroll_max
15474 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15475 return SCROLLING_FAILED;
15477 /* Additional scroll for when ZV was too close to point. */
15478 dy += y_offset;
15480 /* Compute new window start. */
15481 start_display (&it, w, startp);
15483 if (arg_scroll_conservatively)
15484 amount_to_scroll = max (dy, frame_line_height
15485 * max (scroll_step, temp_scroll_step));
15486 else if (scroll_step || temp_scroll_step)
15487 amount_to_scroll = scroll_max;
15488 else
15490 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15491 height = WINDOW_BOX_TEXT_HEIGHT (w);
15492 if (NUMBERP (aggressive))
15494 double float_amount = XFLOATINT (aggressive) * height;
15495 int aggressive_scroll = float_amount;
15496 if (aggressive_scroll == 0 && float_amount > 0)
15497 aggressive_scroll = 1;
15498 /* Don't let point enter the scroll margin near
15499 bottom of the window, if the value of
15500 scroll_down_aggressively happens to be too
15501 large. */
15502 if (aggressive_scroll + 2 * this_scroll_margin > height)
15503 aggressive_scroll = height - 2 * this_scroll_margin;
15504 amount_to_scroll = dy + aggressive_scroll;
15508 if (amount_to_scroll <= 0)
15509 return SCROLLING_FAILED;
15511 move_it_vertically_backward (&it, amount_to_scroll);
15512 startp = it.current.pos;
15516 /* Run window scroll functions. */
15517 startp = run_window_scroll_functions (window, startp);
15519 /* Display the window. Give up if new fonts are loaded, or if point
15520 doesn't appear. */
15521 if (!try_window (window, startp, 0))
15522 rc = SCROLLING_NEED_LARGER_MATRICES;
15523 else if (w->cursor.vpos < 0)
15525 clear_glyph_matrix (w->desired_matrix);
15526 rc = SCROLLING_FAILED;
15528 else
15530 /* Maybe forget recorded base line for line number display. */
15531 if (!just_this_one_p
15532 || current_buffer->clip_changed
15533 || BEG_UNCHANGED < CHARPOS (startp))
15534 w->base_line_number = 0;
15536 /* If cursor ends up on a partially visible line,
15537 treat that as being off the bottom of the screen. */
15538 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15539 false)
15540 /* It's possible that the cursor is on the first line of the
15541 buffer, which is partially obscured due to a vscroll
15542 (Bug#7537). In that case, avoid looping forever. */
15543 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15545 clear_glyph_matrix (w->desired_matrix);
15546 ++extra_scroll_margin_lines;
15547 goto too_near_end;
15549 rc = SCROLLING_SUCCESS;
15552 return rc;
15556 /* Compute a suitable window start for window W if display of W starts
15557 on a continuation line. Value is true if a new window start
15558 was computed.
15560 The new window start will be computed, based on W's width, starting
15561 from the start of the continued line. It is the start of the
15562 screen line with the minimum distance from the old start W->start,
15563 which is still before point (otherwise point will definitely not
15564 be visible in the window). */
15566 static bool
15567 compute_window_start_on_continuation_line (struct window *w)
15569 struct text_pos pos, start_pos, pos_before_pt;
15570 bool window_start_changed_p = false;
15572 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15574 /* If window start is on a continuation line... Window start may be
15575 < BEGV in case there's invisible text at the start of the
15576 buffer (M-x rmail, for example). */
15577 if (CHARPOS (start_pos) > BEGV
15578 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15580 struct it it;
15581 struct glyph_row *row;
15583 /* Handle the case that the window start is out of range. */
15584 if (CHARPOS (start_pos) < BEGV)
15585 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15586 else if (CHARPOS (start_pos) > ZV)
15587 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15589 /* Find the start of the continued line. This should be fast
15590 because find_newline is fast (newline cache). */
15591 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15592 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15593 row, DEFAULT_FACE_ID);
15594 reseat_at_previous_visible_line_start (&it);
15596 /* If the line start is "too far" away from the window start,
15597 say it takes too much time to compute a new window start.
15598 Also, give up if the line start is after point, as in that
15599 case point will not be visible with any window start we
15600 compute. */
15601 if (IT_CHARPOS (it) <= PT
15602 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15603 /* PXW: Do we need upper bounds here? */
15604 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15606 int min_distance, distance;
15608 /* Move forward by display lines to find the new window
15609 start. If window width was enlarged, the new start can
15610 be expected to be > the old start. If window width was
15611 decreased, the new window start will be < the old start.
15612 So, we're looking for the display line start with the
15613 minimum distance from the old window start. */
15614 pos_before_pt = pos = it.current.pos;
15615 min_distance = INFINITY;
15616 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15617 distance < min_distance)
15619 min_distance = distance;
15620 if (CHARPOS (pos) <= PT)
15621 pos_before_pt = pos;
15622 pos = it.current.pos;
15623 if (it.line_wrap == WORD_WRAP)
15625 /* Under WORD_WRAP, move_it_by_lines is likely to
15626 overshoot and stop not at the first, but the
15627 second character from the left margin. So in
15628 that case, we need a more tight control on the X
15629 coordinate of the iterator than move_it_by_lines
15630 promises in its contract. The method is to first
15631 go to the last (rightmost) visible character of a
15632 line, then move to the leftmost character on the
15633 next line in a separate call. */
15634 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15635 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15636 move_it_to (&it, ZV, 0,
15637 it.current_y + it.max_ascent + it.max_descent, -1,
15638 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15640 else
15641 move_it_by_lines (&it, 1);
15644 /* It makes very little sense to make the new window start
15645 after point, as point won't be visible. If that's what
15646 the loop above finds, fall back on the candidate before
15647 or at point that is closest to the old window start. */
15648 if (CHARPOS (pos) > PT)
15649 pos = pos_before_pt;
15651 /* Set the window start there. */
15652 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15653 window_start_changed_p = true;
15657 return window_start_changed_p;
15661 /* Try cursor movement in case text has not changed in window WINDOW,
15662 with window start STARTP. Value is
15664 CURSOR_MOVEMENT_SUCCESS if successful
15666 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15668 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15669 display. *SCROLL_STEP is set to true, under certain circumstances, if
15670 we want to scroll as if scroll-step were set to 1. See the code.
15672 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15673 which case we have to abort this redisplay, and adjust matrices
15674 first. */
15676 enum
15678 CURSOR_MOVEMENT_SUCCESS,
15679 CURSOR_MOVEMENT_CANNOT_BE_USED,
15680 CURSOR_MOVEMENT_MUST_SCROLL,
15681 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15684 static int
15685 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15686 bool *scroll_step)
15688 struct window *w = XWINDOW (window);
15689 struct frame *f = XFRAME (w->frame);
15690 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15692 #ifdef GLYPH_DEBUG
15693 if (inhibit_try_cursor_movement)
15694 return rc;
15695 #endif
15697 /* Previously, there was a check for Lisp integer in the
15698 if-statement below. Now, this field is converted to
15699 ptrdiff_t, thus zero means invalid position in a buffer. */
15700 eassert (w->last_point > 0);
15701 /* Likewise there was a check whether window_end_vpos is nil or larger
15702 than the window. Now window_end_vpos is int and so never nil, but
15703 let's leave eassert to check whether it fits in the window. */
15704 eassert (!w->window_end_valid
15705 || w->window_end_vpos < w->current_matrix->nrows);
15707 /* Handle case where text has not changed, only point, and it has
15708 not moved off the frame. */
15709 if (/* Point may be in this window. */
15710 PT >= CHARPOS (startp)
15711 /* Selective display hasn't changed. */
15712 && !current_buffer->clip_changed
15713 /* Function force-mode-line-update is used to force a thorough
15714 redisplay. It sets either windows_or_buffers_changed or
15715 update_mode_lines. So don't take a shortcut here for these
15716 cases. */
15717 && !update_mode_lines
15718 && !windows_or_buffers_changed
15719 && !f->cursor_type_changed
15720 && NILP (Vshow_trailing_whitespace)
15721 /* This code is not used for mini-buffer for the sake of the case
15722 of redisplaying to replace an echo area message; since in
15723 that case the mini-buffer contents per se are usually
15724 unchanged. This code is of no real use in the mini-buffer
15725 since the handling of this_line_start_pos, etc., in redisplay
15726 handles the same cases. */
15727 && !EQ (window, minibuf_window)
15728 && (FRAME_WINDOW_P (f)
15729 || !overlay_arrow_in_current_buffer_p ()))
15731 int this_scroll_margin, top_scroll_margin;
15732 struct glyph_row *row = NULL;
15733 int frame_line_height = default_line_pixel_height (w);
15734 int window_total_lines
15735 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15737 #ifdef GLYPH_DEBUG
15738 debug_method_add (w, "cursor movement");
15739 #endif
15741 /* Scroll if point within this distance from the top or bottom
15742 of the window. This is a pixel value. */
15743 if (scroll_margin > 0)
15745 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15746 this_scroll_margin *= frame_line_height;
15748 else
15749 this_scroll_margin = 0;
15751 top_scroll_margin = this_scroll_margin;
15752 if (WINDOW_WANTS_HEADER_LINE_P (w))
15753 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15755 /* Start with the row the cursor was displayed during the last
15756 not paused redisplay. Give up if that row is not valid. */
15757 if (w->last_cursor_vpos < 0
15758 || w->last_cursor_vpos >= w->current_matrix->nrows)
15759 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15760 else
15762 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15763 if (row->mode_line_p)
15764 ++row;
15765 if (!row->enabled_p)
15766 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15769 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15771 bool scroll_p = false, must_scroll = false;
15772 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15774 if (PT > w->last_point)
15776 /* Point has moved forward. */
15777 while (MATRIX_ROW_END_CHARPOS (row) < PT
15778 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15780 eassert (row->enabled_p);
15781 ++row;
15784 /* If the end position of a row equals the start
15785 position of the next row, and PT is at that position,
15786 we would rather display cursor in the next line. */
15787 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15788 && MATRIX_ROW_END_CHARPOS (row) == PT
15789 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15790 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15791 && !cursor_row_p (row))
15792 ++row;
15794 /* If within the scroll margin, scroll. Note that
15795 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15796 the next line would be drawn, and that
15797 this_scroll_margin can be zero. */
15798 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15799 || PT > MATRIX_ROW_END_CHARPOS (row)
15800 /* Line is completely visible last line in window
15801 and PT is to be set in the next line. */
15802 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15803 && PT == MATRIX_ROW_END_CHARPOS (row)
15804 && !row->ends_at_zv_p
15805 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15806 scroll_p = true;
15808 else if (PT < w->last_point)
15810 /* Cursor has to be moved backward. Note that PT >=
15811 CHARPOS (startp) because of the outer if-statement. */
15812 while (!row->mode_line_p
15813 && (MATRIX_ROW_START_CHARPOS (row) > PT
15814 || (MATRIX_ROW_START_CHARPOS (row) == PT
15815 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15816 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15817 row > w->current_matrix->rows
15818 && (row-1)->ends_in_newline_from_string_p))))
15819 && (row->y > top_scroll_margin
15820 || CHARPOS (startp) == BEGV))
15822 eassert (row->enabled_p);
15823 --row;
15826 /* Consider the following case: Window starts at BEGV,
15827 there is invisible, intangible text at BEGV, so that
15828 display starts at some point START > BEGV. It can
15829 happen that we are called with PT somewhere between
15830 BEGV and START. Try to handle that case. */
15831 if (row < w->current_matrix->rows
15832 || row->mode_line_p)
15834 row = w->current_matrix->rows;
15835 if (row->mode_line_p)
15836 ++row;
15839 /* Due to newlines in overlay strings, we may have to
15840 skip forward over overlay strings. */
15841 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15842 && MATRIX_ROW_END_CHARPOS (row) == PT
15843 && !cursor_row_p (row))
15844 ++row;
15846 /* If within the scroll margin, scroll. */
15847 if (row->y < top_scroll_margin
15848 && CHARPOS (startp) != BEGV)
15849 scroll_p = true;
15851 else
15853 /* Cursor did not move. So don't scroll even if cursor line
15854 is partially visible, as it was so before. */
15855 rc = CURSOR_MOVEMENT_SUCCESS;
15858 if (PT < MATRIX_ROW_START_CHARPOS (row)
15859 || PT > MATRIX_ROW_END_CHARPOS (row))
15861 /* if PT is not in the glyph row, give up. */
15862 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15863 must_scroll = true;
15865 else if (rc != CURSOR_MOVEMENT_SUCCESS
15866 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15868 struct glyph_row *row1;
15870 /* If rows are bidi-reordered and point moved, back up
15871 until we find a row that does not belong to a
15872 continuation line. This is because we must consider
15873 all rows of a continued line as candidates for the
15874 new cursor positioning, since row start and end
15875 positions change non-linearly with vertical position
15876 in such rows. */
15877 /* FIXME: Revisit this when glyph ``spilling'' in
15878 continuation lines' rows is implemented for
15879 bidi-reordered rows. */
15880 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15881 MATRIX_ROW_CONTINUATION_LINE_P (row);
15882 --row)
15884 /* If we hit the beginning of the displayed portion
15885 without finding the first row of a continued
15886 line, give up. */
15887 if (row <= row1)
15889 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15890 break;
15892 eassert (row->enabled_p);
15895 if (must_scroll)
15897 else if (rc != CURSOR_MOVEMENT_SUCCESS
15898 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15899 /* Make sure this isn't a header line by any chance, since
15900 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15901 && !row->mode_line_p
15902 && make_cursor_line_fully_visible_p)
15904 if (PT == MATRIX_ROW_END_CHARPOS (row)
15905 && !row->ends_at_zv_p
15906 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15907 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15908 else if (row->height > window_box_height (w))
15910 /* If we end up in a partially visible line, let's
15911 make it fully visible, except when it's taller
15912 than the window, in which case we can't do much
15913 about it. */
15914 *scroll_step = true;
15915 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15917 else
15919 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15920 if (!cursor_row_fully_visible_p (w, false, true))
15921 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15922 else
15923 rc = CURSOR_MOVEMENT_SUCCESS;
15926 else if (scroll_p)
15927 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15928 else if (rc != CURSOR_MOVEMENT_SUCCESS
15929 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15931 /* With bidi-reordered rows, there could be more than
15932 one candidate row whose start and end positions
15933 occlude point. We need to let set_cursor_from_row
15934 find the best candidate. */
15935 /* FIXME: Revisit this when glyph ``spilling'' in
15936 continuation lines' rows is implemented for
15937 bidi-reordered rows. */
15938 bool rv = false;
15942 bool at_zv_p = false, exact_match_p = false;
15944 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15945 && PT <= MATRIX_ROW_END_CHARPOS (row)
15946 && cursor_row_p (row))
15947 rv |= set_cursor_from_row (w, row, w->current_matrix,
15948 0, 0, 0, 0);
15949 /* As soon as we've found the exact match for point,
15950 or the first suitable row whose ends_at_zv_p flag
15951 is set, we are done. */
15952 if (rv)
15954 at_zv_p = MATRIX_ROW (w->current_matrix,
15955 w->cursor.vpos)->ends_at_zv_p;
15956 if (!at_zv_p
15957 && w->cursor.hpos >= 0
15958 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15959 w->cursor.vpos))
15961 struct glyph_row *candidate =
15962 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15963 struct glyph *g =
15964 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15965 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15967 exact_match_p =
15968 (BUFFERP (g->object) && g->charpos == PT)
15969 || (NILP (g->object)
15970 && (g->charpos == PT
15971 || (g->charpos == 0 && endpos - 1 == PT)));
15973 if (at_zv_p || exact_match_p)
15975 rc = CURSOR_MOVEMENT_SUCCESS;
15976 break;
15979 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15980 break;
15981 ++row;
15983 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15984 || row->continued_p)
15985 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15986 || (MATRIX_ROW_START_CHARPOS (row) == PT
15987 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15988 /* If we didn't find any candidate rows, or exited the
15989 loop before all the candidates were examined, signal
15990 to the caller that this method failed. */
15991 if (rc != CURSOR_MOVEMENT_SUCCESS
15992 && !(rv
15993 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15994 && !row->continued_p))
15995 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15996 else if (rv)
15997 rc = CURSOR_MOVEMENT_SUCCESS;
15999 else
16003 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16005 rc = CURSOR_MOVEMENT_SUCCESS;
16006 break;
16008 ++row;
16010 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16011 && MATRIX_ROW_START_CHARPOS (row) == PT
16012 && cursor_row_p (row));
16017 return rc;
16021 void
16022 set_vertical_scroll_bar (struct window *w)
16024 ptrdiff_t start, end, whole;
16026 /* Calculate the start and end positions for the current window.
16027 At some point, it would be nice to choose between scrollbars
16028 which reflect the whole buffer size, with special markers
16029 indicating narrowing, and scrollbars which reflect only the
16030 visible region.
16032 Note that mini-buffers sometimes aren't displaying any text. */
16033 if (!MINI_WINDOW_P (w)
16034 || (w == XWINDOW (minibuf_window)
16035 && NILP (echo_area_buffer[0])))
16037 struct buffer *buf = XBUFFER (w->contents);
16038 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16039 start = marker_position (w->start) - BUF_BEGV (buf);
16040 /* I don't think this is guaranteed to be right. For the
16041 moment, we'll pretend it is. */
16042 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16044 if (end < start)
16045 end = start;
16046 if (whole < (end - start))
16047 whole = end - start;
16049 else
16050 start = end = whole = 0;
16052 /* Indicate what this scroll bar ought to be displaying now. */
16053 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16054 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16055 (w, end - start, whole, start);
16059 void
16060 set_horizontal_scroll_bar (struct window *w)
16062 int start, end, whole, portion;
16064 if (!MINI_WINDOW_P (w)
16065 || (w == XWINDOW (minibuf_window)
16066 && NILP (echo_area_buffer[0])))
16068 struct buffer *b = XBUFFER (w->contents);
16069 struct buffer *old_buffer = NULL;
16070 struct it it;
16071 struct text_pos startp;
16073 if (b != current_buffer)
16075 old_buffer = current_buffer;
16076 set_buffer_internal (b);
16079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16080 start_display (&it, w, startp);
16081 it.last_visible_x = INT_MAX;
16082 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16083 MOVE_TO_X | MOVE_TO_Y);
16084 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16085 window_box_height (w), -1,
16086 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16088 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16089 end = start + window_box_width (w, TEXT_AREA);
16090 portion = end - start;
16091 /* After enlarging a horizontally scrolled window such that it
16092 gets at least as wide as the text it contains, make sure that
16093 the thumb doesn't fill the entire scroll bar so we can still
16094 drag it back to see the entire text. */
16095 whole = max (whole, end);
16097 if (it.bidi_p)
16099 Lisp_Object pdir;
16101 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16102 if (EQ (pdir, Qright_to_left))
16104 start = whole - end;
16105 end = start + portion;
16109 if (old_buffer)
16110 set_buffer_internal (old_buffer);
16112 else
16113 start = end = whole = portion = 0;
16115 w->hscroll_whole = whole;
16117 /* Indicate what this scroll bar ought to be displaying now. */
16118 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16119 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16120 (w, portion, whole, start);
16124 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16125 selected_window is redisplayed.
16127 We can return without actually redisplaying the window if fonts has been
16128 changed on window's frame. In that case, redisplay_internal will retry.
16130 As one of the important parts of redisplaying a window, we need to
16131 decide whether the previous window-start position (stored in the
16132 window's w->start marker position) is still valid, and if it isn't,
16133 recompute it. Some details about that:
16135 . The previous window-start could be in a continuation line, in
16136 which case we need to recompute it when the window width
16137 changes. See compute_window_start_on_continuation_line and its
16138 call below.
16140 . The text that changed since last redisplay could include the
16141 previous window-start position. In that case, we try to salvage
16142 what we can from the current glyph matrix by calling
16143 try_scrolling, which see.
16145 . Some Emacs command could force us to use a specific window-start
16146 position by setting the window's force_start flag, or gently
16147 propose doing that by setting the window's optional_new_start
16148 flag. In these cases, we try using the specified start point if
16149 that succeeds (i.e. the window desired matrix is successfully
16150 recomputed, and point location is within the window). In case
16151 of optional_new_start, we first check if the specified start
16152 position is feasible, i.e. if it will allow point to be
16153 displayed in the window. If using the specified start point
16154 fails, e.g., if new fonts are needed to be loaded, we abort the
16155 redisplay cycle and leave it up to the next cycle to figure out
16156 things.
16158 . Note that the window's force_start flag is sometimes set by
16159 redisplay itself, when it decides that the previous window start
16160 point is fine and should be kept. Search for "goto force_start"
16161 below to see the details. Like the values of window-start
16162 specified outside of redisplay, these internally-deduced values
16163 are tested for feasibility, and ignored if found to be
16164 unfeasible.
16166 . Note that the function try_window, used to completely redisplay
16167 a window, accepts the window's start point as its argument.
16168 This is used several times in the redisplay code to control
16169 where the window start will be, according to user options such
16170 as scroll-conservatively, and also to ensure the screen line
16171 showing point will be fully (as opposed to partially) visible on
16172 display. */
16174 static void
16175 redisplay_window (Lisp_Object window, bool just_this_one_p)
16177 struct window *w = XWINDOW (window);
16178 struct frame *f = XFRAME (w->frame);
16179 struct buffer *buffer = XBUFFER (w->contents);
16180 struct buffer *old = current_buffer;
16181 struct text_pos lpoint, opoint, startp;
16182 bool update_mode_line;
16183 int tem;
16184 struct it it;
16185 /* Record it now because it's overwritten. */
16186 bool current_matrix_up_to_date_p = false;
16187 bool used_current_matrix_p = false;
16188 /* This is less strict than current_matrix_up_to_date_p.
16189 It indicates that the buffer contents and narrowing are unchanged. */
16190 bool buffer_unchanged_p = false;
16191 bool temp_scroll_step = false;
16192 ptrdiff_t count = SPECPDL_INDEX ();
16193 int rc;
16194 int centering_position = -1;
16195 bool last_line_misfit = false;
16196 ptrdiff_t beg_unchanged, end_unchanged;
16197 int frame_line_height;
16198 bool use_desired_matrix;
16199 void *itdata = NULL;
16201 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16202 opoint = lpoint;
16204 #ifdef GLYPH_DEBUG
16205 *w->desired_matrix->method = 0;
16206 #endif
16208 if (!just_this_one_p
16209 && REDISPLAY_SOME_P ()
16210 && !w->redisplay
16211 && !w->update_mode_line
16212 && !f->face_change
16213 && !f->redisplay
16214 && !buffer->text->redisplay
16215 && BUF_PT (buffer) == w->last_point)
16216 return;
16218 /* Make sure that both W's markers are valid. */
16219 eassert (XMARKER (w->start)->buffer == buffer);
16220 eassert (XMARKER (w->pointm)->buffer == buffer);
16222 /* We come here again if we need to run window-text-change-functions
16223 below. */
16224 restart:
16225 reconsider_clip_changes (w);
16226 frame_line_height = default_line_pixel_height (w);
16228 /* Has the mode line to be updated? */
16229 update_mode_line = (w->update_mode_line
16230 || update_mode_lines
16231 || buffer->clip_changed
16232 || buffer->prevent_redisplay_optimizations_p);
16234 if (!just_this_one_p)
16235 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16236 cleverly elsewhere. */
16237 w->must_be_updated_p = true;
16239 if (MINI_WINDOW_P (w))
16241 if (w == XWINDOW (echo_area_window)
16242 && !NILP (echo_area_buffer[0]))
16244 if (update_mode_line)
16245 /* We may have to update a tty frame's menu bar or a
16246 tool-bar. Example `M-x C-h C-h C-g'. */
16247 goto finish_menu_bars;
16248 else
16249 /* We've already displayed the echo area glyphs in this window. */
16250 goto finish_scroll_bars;
16252 else if ((w != XWINDOW (minibuf_window)
16253 || minibuf_level == 0)
16254 /* When buffer is nonempty, redisplay window normally. */
16255 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16256 /* Quail displays non-mini buffers in minibuffer window.
16257 In that case, redisplay the window normally. */
16258 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16260 /* W is a mini-buffer window, but it's not active, so clear
16261 it. */
16262 int yb = window_text_bottom_y (w);
16263 struct glyph_row *row;
16264 int y;
16266 for (y = 0, row = w->desired_matrix->rows;
16267 y < yb;
16268 y += row->height, ++row)
16269 blank_row (w, row, y);
16270 goto finish_scroll_bars;
16273 clear_glyph_matrix (w->desired_matrix);
16276 /* Otherwise set up data on this window; select its buffer and point
16277 value. */
16278 /* Really select the buffer, for the sake of buffer-local
16279 variables. */
16280 set_buffer_internal_1 (XBUFFER (w->contents));
16282 current_matrix_up_to_date_p
16283 = (w->window_end_valid
16284 && !current_buffer->clip_changed
16285 && !current_buffer->prevent_redisplay_optimizations_p
16286 && !window_outdated (w));
16288 /* Run the window-text-change-functions
16289 if it is possible that the text on the screen has changed
16290 (either due to modification of the text, or any other reason). */
16291 if (!current_matrix_up_to_date_p
16292 && !NILP (Vwindow_text_change_functions))
16294 safe_run_hooks (Qwindow_text_change_functions);
16295 goto restart;
16298 beg_unchanged = BEG_UNCHANGED;
16299 end_unchanged = END_UNCHANGED;
16301 SET_TEXT_POS (opoint, PT, PT_BYTE);
16303 specbind (Qinhibit_point_motion_hooks, Qt);
16305 buffer_unchanged_p
16306 = (w->window_end_valid
16307 && !current_buffer->clip_changed
16308 && !window_outdated (w));
16310 /* When windows_or_buffers_changed is non-zero, we can't rely
16311 on the window end being valid, so set it to zero there. */
16312 if (windows_or_buffers_changed)
16314 /* If window starts on a continuation line, maybe adjust the
16315 window start in case the window's width changed. */
16316 if (XMARKER (w->start)->buffer == current_buffer)
16317 compute_window_start_on_continuation_line (w);
16319 w->window_end_valid = false;
16320 /* If so, we also can't rely on current matrix
16321 and should not fool try_cursor_movement below. */
16322 current_matrix_up_to_date_p = false;
16325 /* Some sanity checks. */
16326 CHECK_WINDOW_END (w);
16327 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16328 emacs_abort ();
16329 if (BYTEPOS (opoint) < CHARPOS (opoint))
16330 emacs_abort ();
16332 if (mode_line_update_needed (w))
16333 update_mode_line = true;
16335 /* Point refers normally to the selected window. For any other
16336 window, set up appropriate value. */
16337 if (!EQ (window, selected_window))
16339 ptrdiff_t new_pt = marker_position (w->pointm);
16340 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16342 if (new_pt < BEGV)
16344 new_pt = BEGV;
16345 new_pt_byte = BEGV_BYTE;
16346 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16348 else if (new_pt > (ZV - 1))
16350 new_pt = ZV;
16351 new_pt_byte = ZV_BYTE;
16352 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16355 /* We don't use SET_PT so that the point-motion hooks don't run. */
16356 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16359 /* If any of the character widths specified in the display table
16360 have changed, invalidate the width run cache. It's true that
16361 this may be a bit late to catch such changes, but the rest of
16362 redisplay goes (non-fatally) haywire when the display table is
16363 changed, so why should we worry about doing any better? */
16364 if (current_buffer->width_run_cache
16365 || (current_buffer->base_buffer
16366 && current_buffer->base_buffer->width_run_cache))
16368 struct Lisp_Char_Table *disptab = buffer_display_table ();
16370 if (! disptab_matches_widthtab
16371 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16373 struct buffer *buf = current_buffer;
16375 if (buf->base_buffer)
16376 buf = buf->base_buffer;
16377 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16378 recompute_width_table (current_buffer, disptab);
16382 /* If window-start is screwed up, choose a new one. */
16383 if (XMARKER (w->start)->buffer != current_buffer)
16384 goto recenter;
16386 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16388 /* If someone specified a new starting point but did not insist,
16389 check whether it can be used. */
16390 if ((w->optional_new_start || window_frozen_p (w))
16391 && CHARPOS (startp) >= BEGV
16392 && CHARPOS (startp) <= ZV)
16394 ptrdiff_t it_charpos;
16396 w->optional_new_start = false;
16397 start_display (&it, w, startp);
16398 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16399 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16400 /* Record IT's position now, since line_bottom_y might change
16401 that. */
16402 it_charpos = IT_CHARPOS (it);
16403 /* Make sure we set the force_start flag only if the cursor row
16404 will be fully visible. Otherwise, the code under force_start
16405 label below will try to move point back into view, which is
16406 not what the code which sets optional_new_start wants. */
16407 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16408 && !w->force_start)
16410 if (it_charpos == PT)
16411 w->force_start = true;
16412 /* IT may overshoot PT if text at PT is invisible. */
16413 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16414 w->force_start = true;
16415 #ifdef GLYPH_DEBUG
16416 if (w->force_start)
16418 if (window_frozen_p (w))
16419 debug_method_add (w, "set force_start from frozen window start");
16420 else
16421 debug_method_add (w, "set force_start from optional_new_start");
16423 #endif
16427 force_start:
16429 /* Handle case where place to start displaying has been specified,
16430 unless the specified location is outside the accessible range. */
16431 if (w->force_start)
16433 /* We set this later on if we have to adjust point. */
16434 int new_vpos = -1;
16436 w->force_start = false;
16437 w->vscroll = 0;
16438 w->window_end_valid = false;
16440 /* Forget any recorded base line for line number display. */
16441 if (!buffer_unchanged_p)
16442 w->base_line_number = 0;
16444 /* Redisplay the mode line. Select the buffer properly for that.
16445 Also, run the hook window-scroll-functions
16446 because we have scrolled. */
16447 /* Note, we do this after clearing force_start because
16448 if there's an error, it is better to forget about force_start
16449 than to get into an infinite loop calling the hook functions
16450 and having them get more errors. */
16451 if (!update_mode_line
16452 || ! NILP (Vwindow_scroll_functions))
16454 update_mode_line = true;
16455 w->update_mode_line = true;
16456 startp = run_window_scroll_functions (window, startp);
16459 if (CHARPOS (startp) < BEGV)
16460 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16461 else if (CHARPOS (startp) > ZV)
16462 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16464 /* Redisplay, then check if cursor has been set during the
16465 redisplay. Give up if new fonts were loaded. */
16466 /* We used to issue a CHECK_MARGINS argument to try_window here,
16467 but this causes scrolling to fail when point begins inside
16468 the scroll margin (bug#148) -- cyd */
16469 if (!try_window (window, startp, 0))
16471 w->force_start = true;
16472 clear_glyph_matrix (w->desired_matrix);
16473 goto need_larger_matrices;
16476 if (w->cursor.vpos < 0)
16478 /* If point does not appear, try to move point so it does
16479 appear. The desired matrix has been built above, so we
16480 can use it here. First see if point is in invisible
16481 text, and if so, move it to the first visible buffer
16482 position past that. */
16483 struct glyph_row *r = NULL;
16484 Lisp_Object invprop =
16485 get_char_property_and_overlay (make_number (PT), Qinvisible,
16486 Qnil, NULL);
16488 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16490 ptrdiff_t alt_pt;
16491 Lisp_Object invprop_end =
16492 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16493 Qnil, Qnil);
16495 if (NATNUMP (invprop_end))
16496 alt_pt = XFASTINT (invprop_end);
16497 else
16498 alt_pt = ZV;
16499 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16500 NULL, 0);
16502 if (r)
16503 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16504 else /* Give up and just move to the middle of the window. */
16505 new_vpos = window_box_height (w) / 2;
16508 if (!cursor_row_fully_visible_p (w, false, false))
16510 /* Point does appear, but on a line partly visible at end of window.
16511 Move it back to a fully-visible line. */
16512 new_vpos = window_box_height (w);
16513 /* But if window_box_height suggests a Y coordinate that is
16514 not less than we already have, that line will clearly not
16515 be fully visible, so give up and scroll the display.
16516 This can happen when the default face uses a font whose
16517 dimensions are different from the frame's default
16518 font. */
16519 if (new_vpos >= w->cursor.y)
16521 w->cursor.vpos = -1;
16522 clear_glyph_matrix (w->desired_matrix);
16523 goto try_to_scroll;
16526 else if (w->cursor.vpos >= 0)
16528 /* Some people insist on not letting point enter the scroll
16529 margin, even though this part handles windows that didn't
16530 scroll at all. */
16531 int window_total_lines
16532 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16533 int margin = min (scroll_margin, window_total_lines / 4);
16534 int pixel_margin = margin * frame_line_height;
16535 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16537 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16538 below, which finds the row to move point to, advances by
16539 the Y coordinate of the _next_ row, see the definition of
16540 MATRIX_ROW_BOTTOM_Y. */
16541 if (w->cursor.vpos < margin + header_line)
16543 w->cursor.vpos = -1;
16544 clear_glyph_matrix (w->desired_matrix);
16545 goto try_to_scroll;
16547 else
16549 int window_height = window_box_height (w);
16551 if (header_line)
16552 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16553 if (w->cursor.y >= window_height - pixel_margin)
16555 w->cursor.vpos = -1;
16556 clear_glyph_matrix (w->desired_matrix);
16557 goto try_to_scroll;
16562 /* If we need to move point for either of the above reasons,
16563 now actually do it. */
16564 if (new_vpos >= 0)
16566 struct glyph_row *row;
16568 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16569 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16570 ++row;
16572 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16573 MATRIX_ROW_START_BYTEPOS (row));
16575 if (w != XWINDOW (selected_window))
16576 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16577 else if (current_buffer == old)
16578 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16580 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16582 /* Re-run pre-redisplay-function so it can update the region
16583 according to the new position of point. */
16584 /* Other than the cursor, w's redisplay is done so we can set its
16585 redisplay to false. Also the buffer's redisplay can be set to
16586 false, since propagate_buffer_redisplay should have already
16587 propagated its info to `w' anyway. */
16588 w->redisplay = false;
16589 XBUFFER (w->contents)->text->redisplay = false;
16590 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16592 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16594 /* pre-redisplay-function made changes (e.g. move the region)
16595 that require another round of redisplay. */
16596 clear_glyph_matrix (w->desired_matrix);
16597 if (!try_window (window, startp, 0))
16598 goto need_larger_matrices;
16601 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16603 clear_glyph_matrix (w->desired_matrix);
16604 goto try_to_scroll;
16607 #ifdef GLYPH_DEBUG
16608 debug_method_add (w, "forced window start");
16609 #endif
16610 goto done;
16613 /* Handle case where text has not changed, only point, and it has
16614 not moved off the frame, and we are not retrying after hscroll.
16615 (current_matrix_up_to_date_p is true when retrying.) */
16616 if (current_matrix_up_to_date_p
16617 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16618 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16620 switch (rc)
16622 case CURSOR_MOVEMENT_SUCCESS:
16623 used_current_matrix_p = true;
16624 goto done;
16626 case CURSOR_MOVEMENT_MUST_SCROLL:
16627 goto try_to_scroll;
16629 default:
16630 emacs_abort ();
16633 /* If current starting point was originally the beginning of a line
16634 but no longer is, find a new starting point. */
16635 else if (w->start_at_line_beg
16636 && !(CHARPOS (startp) <= BEGV
16637 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16639 #ifdef GLYPH_DEBUG
16640 debug_method_add (w, "recenter 1");
16641 #endif
16642 goto recenter;
16645 /* Try scrolling with try_window_id. Value is > 0 if update has
16646 been done, it is -1 if we know that the same window start will
16647 not work. It is 0 if unsuccessful for some other reason. */
16648 else if ((tem = try_window_id (w)) != 0)
16650 #ifdef GLYPH_DEBUG
16651 debug_method_add (w, "try_window_id %d", tem);
16652 #endif
16654 if (f->fonts_changed)
16655 goto need_larger_matrices;
16656 if (tem > 0)
16657 goto done;
16659 /* Otherwise try_window_id has returned -1 which means that we
16660 don't want the alternative below this comment to execute. */
16662 else if (CHARPOS (startp) >= BEGV
16663 && CHARPOS (startp) <= ZV
16664 && PT >= CHARPOS (startp)
16665 && (CHARPOS (startp) < ZV
16666 /* Avoid starting at end of buffer. */
16667 || CHARPOS (startp) == BEGV
16668 || !window_outdated (w)))
16670 int d1, d2, d5, d6;
16671 int rtop, rbot;
16673 /* If first window line is a continuation line, and window start
16674 is inside the modified region, but the first change is before
16675 current window start, we must select a new window start.
16677 However, if this is the result of a down-mouse event (e.g. by
16678 extending the mouse-drag-overlay), we don't want to select a
16679 new window start, since that would change the position under
16680 the mouse, resulting in an unwanted mouse-movement rather
16681 than a simple mouse-click. */
16682 if (!w->start_at_line_beg
16683 && NILP (do_mouse_tracking)
16684 && CHARPOS (startp) > BEGV
16685 && CHARPOS (startp) > BEG + beg_unchanged
16686 && CHARPOS (startp) <= Z - end_unchanged
16687 /* Even if w->start_at_line_beg is nil, a new window may
16688 start at a line_beg, since that's how set_buffer_window
16689 sets it. So, we need to check the return value of
16690 compute_window_start_on_continuation_line. (See also
16691 bug#197). */
16692 && XMARKER (w->start)->buffer == current_buffer
16693 && compute_window_start_on_continuation_line (w)
16694 /* It doesn't make sense to force the window start like we
16695 do at label force_start if it is already known that point
16696 will not be fully visible in the resulting window, because
16697 doing so will move point from its correct position
16698 instead of scrolling the window to bring point into view.
16699 See bug#9324. */
16700 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16701 /* A very tall row could need more than the window height,
16702 in which case we accept that it is partially visible. */
16703 && (rtop != 0) == (rbot != 0))
16705 w->force_start = true;
16706 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16707 #ifdef GLYPH_DEBUG
16708 debug_method_add (w, "recomputed window start in continuation line");
16709 #endif
16710 goto force_start;
16713 #ifdef GLYPH_DEBUG
16714 debug_method_add (w, "same window start");
16715 #endif
16717 /* Try to redisplay starting at same place as before.
16718 If point has not moved off frame, accept the results. */
16719 if (!current_matrix_up_to_date_p
16720 /* Don't use try_window_reusing_current_matrix in this case
16721 because a window scroll function can have changed the
16722 buffer. */
16723 || !NILP (Vwindow_scroll_functions)
16724 || MINI_WINDOW_P (w)
16725 || !(used_current_matrix_p
16726 = try_window_reusing_current_matrix (w)))
16728 IF_DEBUG (debug_method_add (w, "1"));
16729 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16730 /* -1 means we need to scroll.
16731 0 means we need new matrices, but fonts_changed
16732 is set in that case, so we will detect it below. */
16733 goto try_to_scroll;
16736 if (f->fonts_changed)
16737 goto need_larger_matrices;
16739 if (w->cursor.vpos >= 0)
16741 if (!just_this_one_p
16742 || current_buffer->clip_changed
16743 || BEG_UNCHANGED < CHARPOS (startp))
16744 /* Forget any recorded base line for line number display. */
16745 w->base_line_number = 0;
16747 if (!cursor_row_fully_visible_p (w, true, false))
16749 clear_glyph_matrix (w->desired_matrix);
16750 last_line_misfit = true;
16752 /* Drop through and scroll. */
16753 else
16754 goto done;
16756 else
16757 clear_glyph_matrix (w->desired_matrix);
16760 try_to_scroll:
16762 /* Redisplay the mode line. Select the buffer properly for that. */
16763 if (!update_mode_line)
16765 update_mode_line = true;
16766 w->update_mode_line = true;
16769 /* Try to scroll by specified few lines. */
16770 if ((scroll_conservatively
16771 || emacs_scroll_step
16772 || temp_scroll_step
16773 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16774 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16775 && CHARPOS (startp) >= BEGV
16776 && CHARPOS (startp) <= ZV)
16778 /* The function returns -1 if new fonts were loaded, 1 if
16779 successful, 0 if not successful. */
16780 int ss = try_scrolling (window, just_this_one_p,
16781 scroll_conservatively,
16782 emacs_scroll_step,
16783 temp_scroll_step, last_line_misfit);
16784 switch (ss)
16786 case SCROLLING_SUCCESS:
16787 goto done;
16789 case SCROLLING_NEED_LARGER_MATRICES:
16790 goto need_larger_matrices;
16792 case SCROLLING_FAILED:
16793 break;
16795 default:
16796 emacs_abort ();
16800 /* Finally, just choose a place to start which positions point
16801 according to user preferences. */
16803 recenter:
16805 #ifdef GLYPH_DEBUG
16806 debug_method_add (w, "recenter");
16807 #endif
16809 /* Forget any previously recorded base line for line number display. */
16810 if (!buffer_unchanged_p)
16811 w->base_line_number = 0;
16813 /* Determine the window start relative to point. */
16814 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16815 it.current_y = it.last_visible_y;
16816 if (centering_position < 0)
16818 int window_total_lines
16819 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16820 int margin
16821 = scroll_margin > 0
16822 ? min (scroll_margin, window_total_lines / 4)
16823 : 0;
16824 ptrdiff_t margin_pos = CHARPOS (startp);
16825 Lisp_Object aggressive;
16826 bool scrolling_up;
16828 /* If there is a scroll margin at the top of the window, find
16829 its character position. */
16830 if (margin
16831 /* Cannot call start_display if startp is not in the
16832 accessible region of the buffer. This can happen when we
16833 have just switched to a different buffer and/or changed
16834 its restriction. In that case, startp is initialized to
16835 the character position 1 (BEGV) because we did not yet
16836 have chance to display the buffer even once. */
16837 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16839 struct it it1;
16840 void *it1data = NULL;
16842 SAVE_IT (it1, it, it1data);
16843 start_display (&it1, w, startp);
16844 move_it_vertically (&it1, margin * frame_line_height);
16845 margin_pos = IT_CHARPOS (it1);
16846 RESTORE_IT (&it, &it, it1data);
16848 scrolling_up = PT > margin_pos;
16849 aggressive =
16850 scrolling_up
16851 ? BVAR (current_buffer, scroll_up_aggressively)
16852 : BVAR (current_buffer, scroll_down_aggressively);
16854 if (!MINI_WINDOW_P (w)
16855 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16857 int pt_offset = 0;
16859 /* Setting scroll-conservatively overrides
16860 scroll-*-aggressively. */
16861 if (!scroll_conservatively && NUMBERP (aggressive))
16863 double float_amount = XFLOATINT (aggressive);
16865 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16866 if (pt_offset == 0 && float_amount > 0)
16867 pt_offset = 1;
16868 if (pt_offset && margin > 0)
16869 margin -= 1;
16871 /* Compute how much to move the window start backward from
16872 point so that point will be displayed where the user
16873 wants it. */
16874 if (scrolling_up)
16876 centering_position = it.last_visible_y;
16877 if (pt_offset)
16878 centering_position -= pt_offset;
16879 centering_position -=
16880 (frame_line_height * (1 + margin + last_line_misfit)
16881 + WINDOW_HEADER_LINE_HEIGHT (w));
16882 /* Don't let point enter the scroll margin near top of
16883 the window. */
16884 if (centering_position < margin * frame_line_height)
16885 centering_position = margin * frame_line_height;
16887 else
16888 centering_position = margin * frame_line_height + pt_offset;
16890 else
16891 /* Set the window start half the height of the window backward
16892 from point. */
16893 centering_position = window_box_height (w) / 2;
16895 move_it_vertically_backward (&it, centering_position);
16897 eassert (IT_CHARPOS (it) >= BEGV);
16899 /* The function move_it_vertically_backward may move over more
16900 than the specified y-distance. If it->w is small, e.g. a
16901 mini-buffer window, we may end up in front of the window's
16902 display area. Start displaying at the start of the line
16903 containing PT in this case. */
16904 if (it.current_y <= 0)
16906 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16907 move_it_vertically_backward (&it, 0);
16908 it.current_y = 0;
16911 it.current_x = it.hpos = 0;
16913 /* Set the window start position here explicitly, to avoid an
16914 infinite loop in case the functions in window-scroll-functions
16915 get errors. */
16916 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16918 /* Run scroll hooks. */
16919 startp = run_window_scroll_functions (window, it.current.pos);
16921 /* We invoke try_window and try_window_reusing_current_matrix below,
16922 and they manipulate the bidi cache. Save and restore the cache
16923 state of our iterator, so we could continue using it after that. */
16924 itdata = bidi_shelve_cache ();
16926 /* Redisplay the window. */
16927 use_desired_matrix = false;
16928 if (!current_matrix_up_to_date_p
16929 || windows_or_buffers_changed
16930 || f->cursor_type_changed
16931 /* Don't use try_window_reusing_current_matrix in this case
16932 because it can have changed the buffer. */
16933 || !NILP (Vwindow_scroll_functions)
16934 || !just_this_one_p
16935 || MINI_WINDOW_P (w)
16936 || !(used_current_matrix_p
16937 = try_window_reusing_current_matrix (w)))
16938 use_desired_matrix = (try_window (window, startp, 0) == 1);
16940 bidi_unshelve_cache (itdata, false);
16942 /* If new fonts have been loaded (due to fontsets), give up. We
16943 have to start a new redisplay since we need to re-adjust glyph
16944 matrices. */
16945 if (f->fonts_changed)
16946 goto need_larger_matrices;
16948 /* If cursor did not appear assume that the middle of the window is
16949 in the first line of the window. Do it again with the next line.
16950 (Imagine a window of height 100, displaying two lines of height
16951 60. Moving back 50 from it->last_visible_y will end in the first
16952 line.) */
16953 if (w->cursor.vpos < 0)
16955 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16957 clear_glyph_matrix (w->desired_matrix);
16958 move_it_by_lines (&it, 1);
16959 try_window (window, it.current.pos, 0);
16961 else if (PT < IT_CHARPOS (it))
16963 clear_glyph_matrix (w->desired_matrix);
16964 move_it_by_lines (&it, -1);
16965 try_window (window, it.current.pos, 0);
16967 else if (scroll_conservatively > SCROLL_LIMIT
16968 && (it.method == GET_FROM_STRING
16969 || overlay_touches_p (IT_CHARPOS (it)))
16970 && IT_CHARPOS (it) < ZV)
16972 /* If the window starts with a before-string that spans more
16973 than one screen line, using that position to display the
16974 window might fail to bring point into the view, because
16975 start_display will always start by displaying the string,
16976 whereas the code above determines where to set w->start
16977 by the buffer position of the place where it takes screen
16978 coordinates. Try to recover by finding the next screen
16979 line that displays buffer text. */
16980 ptrdiff_t pos0 = IT_CHARPOS (it);
16982 clear_glyph_matrix (w->desired_matrix);
16983 do {
16984 move_it_by_lines (&it, 1);
16985 } while (IT_CHARPOS (it) == pos0);
16986 try_window (window, it.current.pos, 0);
16988 else
16990 /* Not much we can do about it. */
16994 /* Consider the following case: Window starts at BEGV, there is
16995 invisible, intangible text at BEGV, so that display starts at
16996 some point START > BEGV. It can happen that we are called with
16997 PT somewhere between BEGV and START. Try to handle that case,
16998 and similar ones. */
16999 if (w->cursor.vpos < 0)
17001 /* Prefer the desired matrix to the current matrix, if possible,
17002 in the fallback calculations below. This is because using
17003 the current matrix might completely goof, e.g. if its first
17004 row is after point. */
17005 struct glyph_matrix *matrix =
17006 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17007 /* First, try locating the proper glyph row for PT. */
17008 struct glyph_row *row =
17009 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17011 /* Sometimes point is at the beginning of invisible text that is
17012 before the 1st character displayed in the row. In that case,
17013 row_containing_pos fails to find the row, because no glyphs
17014 with appropriate buffer positions are present in the row.
17015 Therefore, we next try to find the row which shows the 1st
17016 position after the invisible text. */
17017 if (!row)
17019 Lisp_Object val =
17020 get_char_property_and_overlay (make_number (PT), Qinvisible,
17021 Qnil, NULL);
17023 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17025 ptrdiff_t alt_pos;
17026 Lisp_Object invis_end =
17027 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17028 Qnil, Qnil);
17030 if (NATNUMP (invis_end))
17031 alt_pos = XFASTINT (invis_end);
17032 else
17033 alt_pos = ZV;
17034 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17037 /* Finally, fall back on the first row of the window after the
17038 header line (if any). This is slightly better than not
17039 displaying the cursor at all. */
17040 if (!row)
17042 row = matrix->rows;
17043 if (row->mode_line_p)
17044 ++row;
17046 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17049 if (!cursor_row_fully_visible_p (w, false, false))
17051 /* If vscroll is enabled, disable it and try again. */
17052 if (w->vscroll)
17054 w->vscroll = 0;
17055 clear_glyph_matrix (w->desired_matrix);
17056 goto recenter;
17059 /* Users who set scroll-conservatively to a large number want
17060 point just above/below the scroll margin. If we ended up
17061 with point's row partially visible, move the window start to
17062 make that row fully visible and out of the margin. */
17063 if (scroll_conservatively > SCROLL_LIMIT)
17065 int window_total_lines
17066 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17067 int margin =
17068 scroll_margin > 0
17069 ? min (scroll_margin, window_total_lines / 4)
17070 : 0;
17071 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17073 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17074 clear_glyph_matrix (w->desired_matrix);
17075 if (1 == try_window (window, it.current.pos,
17076 TRY_WINDOW_CHECK_MARGINS))
17077 goto done;
17080 /* If centering point failed to make the whole line visible,
17081 put point at the top instead. That has to make the whole line
17082 visible, if it can be done. */
17083 if (centering_position == 0)
17084 goto done;
17086 clear_glyph_matrix (w->desired_matrix);
17087 centering_position = 0;
17088 goto recenter;
17091 done:
17093 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17094 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17095 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17097 /* Display the mode line, if we must. */
17098 if ((update_mode_line
17099 /* If window not full width, must redo its mode line
17100 if (a) the window to its side is being redone and
17101 (b) we do a frame-based redisplay. This is a consequence
17102 of how inverted lines are drawn in frame-based redisplay. */
17103 || (!just_this_one_p
17104 && !FRAME_WINDOW_P (f)
17105 && !WINDOW_FULL_WIDTH_P (w))
17106 /* Line number to display. */
17107 || w->base_line_pos > 0
17108 /* Column number is displayed and different from the one displayed. */
17109 || (w->column_number_displayed != -1
17110 && (w->column_number_displayed != current_column ())))
17111 /* This means that the window has a mode line. */
17112 && (WINDOW_WANTS_MODELINE_P (w)
17113 || WINDOW_WANTS_HEADER_LINE_P (w)))
17116 display_mode_lines (w);
17118 /* If mode line height has changed, arrange for a thorough
17119 immediate redisplay using the correct mode line height. */
17120 if (WINDOW_WANTS_MODELINE_P (w)
17121 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17123 f->fonts_changed = true;
17124 w->mode_line_height = -1;
17125 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17126 = DESIRED_MODE_LINE_HEIGHT (w);
17129 /* If header line height has changed, arrange for a thorough
17130 immediate redisplay using the correct header line height. */
17131 if (WINDOW_WANTS_HEADER_LINE_P (w)
17132 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17134 f->fonts_changed = true;
17135 w->header_line_height = -1;
17136 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17137 = DESIRED_HEADER_LINE_HEIGHT (w);
17140 if (f->fonts_changed)
17141 goto need_larger_matrices;
17144 if (!line_number_displayed && w->base_line_pos != -1)
17146 w->base_line_pos = 0;
17147 w->base_line_number = 0;
17150 finish_menu_bars:
17152 /* When we reach a frame's selected window, redo the frame's menu
17153 bar and the frame's title. */
17154 if (update_mode_line
17155 && EQ (FRAME_SELECTED_WINDOW (f), window))
17157 bool redisplay_menu_p;
17159 if (FRAME_WINDOW_P (f))
17161 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17162 || defined (HAVE_NS) || defined (USE_GTK)
17163 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17164 #else
17165 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17166 #endif
17168 else
17169 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17171 if (redisplay_menu_p)
17172 display_menu_bar (w);
17174 #ifdef HAVE_WINDOW_SYSTEM
17175 if (FRAME_WINDOW_P (f))
17177 #if defined (USE_GTK) || defined (HAVE_NS)
17178 if (FRAME_EXTERNAL_TOOL_BAR (f))
17179 redisplay_tool_bar (f);
17180 #else
17181 if (WINDOWP (f->tool_bar_window)
17182 && (FRAME_TOOL_BAR_LINES (f) > 0
17183 || !NILP (Vauto_resize_tool_bars))
17184 && redisplay_tool_bar (f))
17185 ignore_mouse_drag_p = true;
17186 #endif
17188 x_consider_frame_title (w->frame);
17189 #endif
17192 #ifdef HAVE_WINDOW_SYSTEM
17193 if (FRAME_WINDOW_P (f)
17194 && update_window_fringes (w, (just_this_one_p
17195 || (!used_current_matrix_p && !overlay_arrow_seen)
17196 || w->pseudo_window_p)))
17198 update_begin (f);
17199 block_input ();
17200 if (draw_window_fringes (w, true))
17202 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17203 x_draw_right_divider (w);
17204 else
17205 x_draw_vertical_border (w);
17207 unblock_input ();
17208 update_end (f);
17211 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17212 x_draw_bottom_divider (w);
17213 #endif /* HAVE_WINDOW_SYSTEM */
17215 /* We go to this label, with fonts_changed set, if it is
17216 necessary to try again using larger glyph matrices.
17217 We have to redeem the scroll bar even in this case,
17218 because the loop in redisplay_internal expects that. */
17219 need_larger_matrices:
17221 finish_scroll_bars:
17223 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17225 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17226 /* Set the thumb's position and size. */
17227 set_vertical_scroll_bar (w);
17229 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17230 /* Set the thumb's position and size. */
17231 set_horizontal_scroll_bar (w);
17233 /* Note that we actually used the scroll bar attached to this
17234 window, so it shouldn't be deleted at the end of redisplay. */
17235 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17236 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17239 /* Restore current_buffer and value of point in it. The window
17240 update may have changed the buffer, so first make sure `opoint'
17241 is still valid (Bug#6177). */
17242 if (CHARPOS (opoint) < BEGV)
17243 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17244 else if (CHARPOS (opoint) > ZV)
17245 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17246 else
17247 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17249 set_buffer_internal_1 (old);
17250 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17251 shorter. This can be caused by log truncation in *Messages*. */
17252 if (CHARPOS (lpoint) <= ZV)
17253 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17255 unbind_to (count, Qnil);
17259 /* Build the complete desired matrix of WINDOW with a window start
17260 buffer position POS.
17262 Value is 1 if successful. It is zero if fonts were loaded during
17263 redisplay which makes re-adjusting glyph matrices necessary, and -1
17264 if point would appear in the scroll margins.
17265 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17266 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17267 set in FLAGS.) */
17270 try_window (Lisp_Object window, struct text_pos pos, int flags)
17272 struct window *w = XWINDOW (window);
17273 struct it it;
17274 struct glyph_row *last_text_row = NULL;
17275 struct frame *f = XFRAME (w->frame);
17276 int frame_line_height = default_line_pixel_height (w);
17278 /* Make POS the new window start. */
17279 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17281 /* Mark cursor position as unknown. No overlay arrow seen. */
17282 w->cursor.vpos = -1;
17283 overlay_arrow_seen = false;
17285 /* Initialize iterator and info to start at POS. */
17286 start_display (&it, w, pos);
17287 it.glyph_row->reversed_p = false;
17289 /* Display all lines of W. */
17290 while (it.current_y < it.last_visible_y)
17292 if (display_line (&it))
17293 last_text_row = it.glyph_row - 1;
17294 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17295 return 0;
17298 /* Don't let the cursor end in the scroll margins. */
17299 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17300 && !MINI_WINDOW_P (w))
17302 int this_scroll_margin;
17303 int window_total_lines
17304 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17306 if (scroll_margin > 0)
17308 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17309 this_scroll_margin *= frame_line_height;
17311 else
17312 this_scroll_margin = 0;
17314 if ((w->cursor.y >= 0 /* not vscrolled */
17315 && w->cursor.y < this_scroll_margin
17316 && CHARPOS (pos) > BEGV
17317 && IT_CHARPOS (it) < ZV)
17318 /* rms: considering make_cursor_line_fully_visible_p here
17319 seems to give wrong results. We don't want to recenter
17320 when the last line is partly visible, we want to allow
17321 that case to be handled in the usual way. */
17322 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17324 w->cursor.vpos = -1;
17325 clear_glyph_matrix (w->desired_matrix);
17326 return -1;
17330 /* If bottom moved off end of frame, change mode line percentage. */
17331 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17332 w->update_mode_line = true;
17334 /* Set window_end_pos to the offset of the last character displayed
17335 on the window from the end of current_buffer. Set
17336 window_end_vpos to its row number. */
17337 if (last_text_row)
17339 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17340 adjust_window_ends (w, last_text_row, false);
17341 eassert
17342 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17343 w->window_end_vpos)));
17345 else
17347 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17348 w->window_end_pos = Z - ZV;
17349 w->window_end_vpos = 0;
17352 /* But that is not valid info until redisplay finishes. */
17353 w->window_end_valid = false;
17354 return 1;
17359 /************************************************************************
17360 Window redisplay reusing current matrix when buffer has not changed
17361 ************************************************************************/
17363 /* Try redisplay of window W showing an unchanged buffer with a
17364 different window start than the last time it was displayed by
17365 reusing its current matrix. Value is true if successful.
17366 W->start is the new window start. */
17368 static bool
17369 try_window_reusing_current_matrix (struct window *w)
17371 struct frame *f = XFRAME (w->frame);
17372 struct glyph_row *bottom_row;
17373 struct it it;
17374 struct run run;
17375 struct text_pos start, new_start;
17376 int nrows_scrolled, i;
17377 struct glyph_row *last_text_row;
17378 struct glyph_row *last_reused_text_row;
17379 struct glyph_row *start_row;
17380 int start_vpos, min_y, max_y;
17382 #ifdef GLYPH_DEBUG
17383 if (inhibit_try_window_reusing)
17384 return false;
17385 #endif
17387 if (/* This function doesn't handle terminal frames. */
17388 !FRAME_WINDOW_P (f)
17389 /* Don't try to reuse the display if windows have been split
17390 or such. */
17391 || windows_or_buffers_changed
17392 || f->cursor_type_changed)
17393 return false;
17395 /* Can't do this if showing trailing whitespace. */
17396 if (!NILP (Vshow_trailing_whitespace))
17397 return false;
17399 /* If top-line visibility has changed, give up. */
17400 if (WINDOW_WANTS_HEADER_LINE_P (w)
17401 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17402 return false;
17404 /* Give up if old or new display is scrolled vertically. We could
17405 make this function handle this, but right now it doesn't. */
17406 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17407 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17408 return false;
17410 /* The variable new_start now holds the new window start. The old
17411 start `start' can be determined from the current matrix. */
17412 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17413 start = start_row->minpos;
17414 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17416 /* Clear the desired matrix for the display below. */
17417 clear_glyph_matrix (w->desired_matrix);
17419 if (CHARPOS (new_start) <= CHARPOS (start))
17421 /* Don't use this method if the display starts with an ellipsis
17422 displayed for invisible text. It's not easy to handle that case
17423 below, and it's certainly not worth the effort since this is
17424 not a frequent case. */
17425 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17426 return false;
17428 IF_DEBUG (debug_method_add (w, "twu1"));
17430 /* Display up to a row that can be reused. The variable
17431 last_text_row is set to the last row displayed that displays
17432 text. Note that it.vpos == 0 if or if not there is a
17433 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17434 start_display (&it, w, new_start);
17435 w->cursor.vpos = -1;
17436 last_text_row = last_reused_text_row = NULL;
17438 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17440 /* If we have reached into the characters in the START row,
17441 that means the line boundaries have changed. So we
17442 can't start copying with the row START. Maybe it will
17443 work to start copying with the following row. */
17444 while (IT_CHARPOS (it) > CHARPOS (start))
17446 /* Advance to the next row as the "start". */
17447 start_row++;
17448 start = start_row->minpos;
17449 /* If there are no more rows to try, or just one, give up. */
17450 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17451 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17452 || CHARPOS (start) == ZV)
17454 clear_glyph_matrix (w->desired_matrix);
17455 return false;
17458 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17460 /* If we have reached alignment, we can copy the rest of the
17461 rows. */
17462 if (IT_CHARPOS (it) == CHARPOS (start)
17463 /* Don't accept "alignment" inside a display vector,
17464 since start_row could have started in the middle of
17465 that same display vector (thus their character
17466 positions match), and we have no way of telling if
17467 that is the case. */
17468 && it.current.dpvec_index < 0)
17469 break;
17471 it.glyph_row->reversed_p = false;
17472 if (display_line (&it))
17473 last_text_row = it.glyph_row - 1;
17477 /* A value of current_y < last_visible_y means that we stopped
17478 at the previous window start, which in turn means that we
17479 have at least one reusable row. */
17480 if (it.current_y < it.last_visible_y)
17482 struct glyph_row *row;
17484 /* IT.vpos always starts from 0; it counts text lines. */
17485 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17487 /* Find PT if not already found in the lines displayed. */
17488 if (w->cursor.vpos < 0)
17490 int dy = it.current_y - start_row->y;
17492 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17493 row = row_containing_pos (w, PT, row, NULL, dy);
17494 if (row)
17495 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17496 dy, nrows_scrolled);
17497 else
17499 clear_glyph_matrix (w->desired_matrix);
17500 return false;
17504 /* Scroll the display. Do it before the current matrix is
17505 changed. The problem here is that update has not yet
17506 run, i.e. part of the current matrix is not up to date.
17507 scroll_run_hook will clear the cursor, and use the
17508 current matrix to get the height of the row the cursor is
17509 in. */
17510 run.current_y = start_row->y;
17511 run.desired_y = it.current_y;
17512 run.height = it.last_visible_y - it.current_y;
17514 if (run.height > 0 && run.current_y != run.desired_y)
17516 update_begin (f);
17517 FRAME_RIF (f)->update_window_begin_hook (w);
17518 FRAME_RIF (f)->clear_window_mouse_face (w);
17519 FRAME_RIF (f)->scroll_run_hook (w, &run);
17520 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17521 update_end (f);
17524 /* Shift current matrix down by nrows_scrolled lines. */
17525 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17526 rotate_matrix (w->current_matrix,
17527 start_vpos,
17528 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17529 nrows_scrolled);
17531 /* Disable lines that must be updated. */
17532 for (i = 0; i < nrows_scrolled; ++i)
17533 (start_row + i)->enabled_p = false;
17535 /* Re-compute Y positions. */
17536 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17537 max_y = it.last_visible_y;
17538 for (row = start_row + nrows_scrolled;
17539 row < bottom_row;
17540 ++row)
17542 row->y = it.current_y;
17543 row->visible_height = row->height;
17545 if (row->y < min_y)
17546 row->visible_height -= min_y - row->y;
17547 if (row->y + row->height > max_y)
17548 row->visible_height -= row->y + row->height - max_y;
17549 if (row->fringe_bitmap_periodic_p)
17550 row->redraw_fringe_bitmaps_p = true;
17552 it.current_y += row->height;
17554 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17555 last_reused_text_row = row;
17556 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17557 break;
17560 /* Disable lines in the current matrix which are now
17561 below the window. */
17562 for (++row; row < bottom_row; ++row)
17563 row->enabled_p = row->mode_line_p = false;
17566 /* Update window_end_pos etc.; last_reused_text_row is the last
17567 reused row from the current matrix containing text, if any.
17568 The value of last_text_row is the last displayed line
17569 containing text. */
17570 if (last_reused_text_row)
17571 adjust_window_ends (w, last_reused_text_row, true);
17572 else if (last_text_row)
17573 adjust_window_ends (w, last_text_row, false);
17574 else
17576 /* This window must be completely empty. */
17577 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17578 w->window_end_pos = Z - ZV;
17579 w->window_end_vpos = 0;
17581 w->window_end_valid = false;
17583 /* Update hint: don't try scrolling again in update_window. */
17584 w->desired_matrix->no_scrolling_p = true;
17586 #ifdef GLYPH_DEBUG
17587 debug_method_add (w, "try_window_reusing_current_matrix 1");
17588 #endif
17589 return true;
17591 else if (CHARPOS (new_start) > CHARPOS (start))
17593 struct glyph_row *pt_row, *row;
17594 struct glyph_row *first_reusable_row;
17595 struct glyph_row *first_row_to_display;
17596 int dy;
17597 int yb = window_text_bottom_y (w);
17599 /* Find the row starting at new_start, if there is one. Don't
17600 reuse a partially visible line at the end. */
17601 first_reusable_row = start_row;
17602 while (first_reusable_row->enabled_p
17603 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17604 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17605 < CHARPOS (new_start)))
17606 ++first_reusable_row;
17608 /* Give up if there is no row to reuse. */
17609 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17610 || !first_reusable_row->enabled_p
17611 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17612 != CHARPOS (new_start)))
17613 return false;
17615 /* We can reuse fully visible rows beginning with
17616 first_reusable_row to the end of the window. Set
17617 first_row_to_display to the first row that cannot be reused.
17618 Set pt_row to the row containing point, if there is any. */
17619 pt_row = NULL;
17620 for (first_row_to_display = first_reusable_row;
17621 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17622 ++first_row_to_display)
17624 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17625 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17626 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17627 && first_row_to_display->ends_at_zv_p
17628 && pt_row == NULL)))
17629 pt_row = first_row_to_display;
17632 /* Start displaying at the start of first_row_to_display. */
17633 eassert (first_row_to_display->y < yb);
17634 init_to_row_start (&it, w, first_row_to_display);
17636 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17637 - start_vpos);
17638 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17639 - nrows_scrolled);
17640 it.current_y = (first_row_to_display->y - first_reusable_row->y
17641 + WINDOW_HEADER_LINE_HEIGHT (w));
17643 /* Display lines beginning with first_row_to_display in the
17644 desired matrix. Set last_text_row to the last row displayed
17645 that displays text. */
17646 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17647 if (pt_row == NULL)
17648 w->cursor.vpos = -1;
17649 last_text_row = NULL;
17650 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17651 if (display_line (&it))
17652 last_text_row = it.glyph_row - 1;
17654 /* If point is in a reused row, adjust y and vpos of the cursor
17655 position. */
17656 if (pt_row)
17658 w->cursor.vpos -= nrows_scrolled;
17659 w->cursor.y -= first_reusable_row->y - start_row->y;
17662 /* Give up if point isn't in a row displayed or reused. (This
17663 also handles the case where w->cursor.vpos < nrows_scrolled
17664 after the calls to display_line, which can happen with scroll
17665 margins. See bug#1295.) */
17666 if (w->cursor.vpos < 0)
17668 clear_glyph_matrix (w->desired_matrix);
17669 return false;
17672 /* Scroll the display. */
17673 run.current_y = first_reusable_row->y;
17674 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17675 run.height = it.last_visible_y - run.current_y;
17676 dy = run.current_y - run.desired_y;
17678 if (run.height)
17680 update_begin (f);
17681 FRAME_RIF (f)->update_window_begin_hook (w);
17682 FRAME_RIF (f)->clear_window_mouse_face (w);
17683 FRAME_RIF (f)->scroll_run_hook (w, &run);
17684 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17685 update_end (f);
17688 /* Adjust Y positions of reused rows. */
17689 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17690 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17691 max_y = it.last_visible_y;
17692 for (row = first_reusable_row; row < first_row_to_display; ++row)
17694 row->y -= dy;
17695 row->visible_height = row->height;
17696 if (row->y < min_y)
17697 row->visible_height -= min_y - row->y;
17698 if (row->y + row->height > max_y)
17699 row->visible_height -= row->y + row->height - max_y;
17700 if (row->fringe_bitmap_periodic_p)
17701 row->redraw_fringe_bitmaps_p = true;
17704 /* Scroll the current matrix. */
17705 eassert (nrows_scrolled > 0);
17706 rotate_matrix (w->current_matrix,
17707 start_vpos,
17708 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17709 -nrows_scrolled);
17711 /* Disable rows not reused. */
17712 for (row -= nrows_scrolled; row < bottom_row; ++row)
17713 row->enabled_p = false;
17715 /* Point may have moved to a different line, so we cannot assume that
17716 the previous cursor position is valid; locate the correct row. */
17717 if (pt_row)
17719 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17720 row < bottom_row
17721 && PT >= MATRIX_ROW_END_CHARPOS (row)
17722 && !row->ends_at_zv_p;
17723 row++)
17725 w->cursor.vpos++;
17726 w->cursor.y = row->y;
17728 if (row < bottom_row)
17730 /* Can't simply scan the row for point with
17731 bidi-reordered glyph rows. Let set_cursor_from_row
17732 figure out where to put the cursor, and if it fails,
17733 give up. */
17734 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17736 if (!set_cursor_from_row (w, row, w->current_matrix,
17737 0, 0, 0, 0))
17739 clear_glyph_matrix (w->desired_matrix);
17740 return false;
17743 else
17745 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17746 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17748 for (; glyph < end
17749 && (!BUFFERP (glyph->object)
17750 || glyph->charpos < PT);
17751 glyph++)
17753 w->cursor.hpos++;
17754 w->cursor.x += glyph->pixel_width;
17760 /* Adjust window end. A null value of last_text_row means that
17761 the window end is in reused rows which in turn means that
17762 only its vpos can have changed. */
17763 if (last_text_row)
17764 adjust_window_ends (w, last_text_row, false);
17765 else
17766 w->window_end_vpos -= nrows_scrolled;
17768 w->window_end_valid = false;
17769 w->desired_matrix->no_scrolling_p = true;
17771 #ifdef GLYPH_DEBUG
17772 debug_method_add (w, "try_window_reusing_current_matrix 2");
17773 #endif
17774 return true;
17777 return false;
17782 /************************************************************************
17783 Window redisplay reusing current matrix when buffer has changed
17784 ************************************************************************/
17786 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17787 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17788 ptrdiff_t *, ptrdiff_t *);
17789 static struct glyph_row *
17790 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17791 struct glyph_row *);
17794 /* Return the last row in MATRIX displaying text. If row START is
17795 non-null, start searching with that row. IT gives the dimensions
17796 of the display. Value is null if matrix is empty; otherwise it is
17797 a pointer to the row found. */
17799 static struct glyph_row *
17800 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17801 struct glyph_row *start)
17803 struct glyph_row *row, *row_found;
17805 /* Set row_found to the last row in IT->w's current matrix
17806 displaying text. The loop looks funny but think of partially
17807 visible lines. */
17808 row_found = NULL;
17809 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17810 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17812 eassert (row->enabled_p);
17813 row_found = row;
17814 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17815 break;
17816 ++row;
17819 return row_found;
17823 /* Return the last row in the current matrix of W that is not affected
17824 by changes at the start of current_buffer that occurred since W's
17825 current matrix was built. Value is null if no such row exists.
17827 BEG_UNCHANGED us the number of characters unchanged at the start of
17828 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17829 first changed character in current_buffer. Characters at positions <
17830 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17831 when the current matrix was built. */
17833 static struct glyph_row *
17834 find_last_unchanged_at_beg_row (struct window *w)
17836 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17837 struct glyph_row *row;
17838 struct glyph_row *row_found = NULL;
17839 int yb = window_text_bottom_y (w);
17841 /* Find the last row displaying unchanged text. */
17842 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17843 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17844 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17845 ++row)
17847 if (/* If row ends before first_changed_pos, it is unchanged,
17848 except in some case. */
17849 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17850 /* When row ends in ZV and we write at ZV it is not
17851 unchanged. */
17852 && !row->ends_at_zv_p
17853 /* When first_changed_pos is the end of a continued line,
17854 row is not unchanged because it may be no longer
17855 continued. */
17856 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17857 && (row->continued_p
17858 || row->exact_window_width_line_p))
17859 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17860 needs to be recomputed, so don't consider this row as
17861 unchanged. This happens when the last line was
17862 bidi-reordered and was killed immediately before this
17863 redisplay cycle. In that case, ROW->end stores the
17864 buffer position of the first visual-order character of
17865 the killed text, which is now beyond ZV. */
17866 && CHARPOS (row->end.pos) <= ZV)
17867 row_found = row;
17869 /* Stop if last visible row. */
17870 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17871 break;
17874 return row_found;
17878 /* Find the first glyph row in the current matrix of W that is not
17879 affected by changes at the end of current_buffer since the
17880 time W's current matrix was built.
17882 Return in *DELTA the number of chars by which buffer positions in
17883 unchanged text at the end of current_buffer must be adjusted.
17885 Return in *DELTA_BYTES the corresponding number of bytes.
17887 Value is null if no such row exists, i.e. all rows are affected by
17888 changes. */
17890 static struct glyph_row *
17891 find_first_unchanged_at_end_row (struct window *w,
17892 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17894 struct glyph_row *row;
17895 struct glyph_row *row_found = NULL;
17897 *delta = *delta_bytes = 0;
17899 /* Display must not have been paused, otherwise the current matrix
17900 is not up to date. */
17901 eassert (w->window_end_valid);
17903 /* A value of window_end_pos >= END_UNCHANGED means that the window
17904 end is in the range of changed text. If so, there is no
17905 unchanged row at the end of W's current matrix. */
17906 if (w->window_end_pos >= END_UNCHANGED)
17907 return NULL;
17909 /* Set row to the last row in W's current matrix displaying text. */
17910 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17912 /* If matrix is entirely empty, no unchanged row exists. */
17913 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17915 /* The value of row is the last glyph row in the matrix having a
17916 meaningful buffer position in it. The end position of row
17917 corresponds to window_end_pos. This allows us to translate
17918 buffer positions in the current matrix to current buffer
17919 positions for characters not in changed text. */
17920 ptrdiff_t Z_old =
17921 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17922 ptrdiff_t Z_BYTE_old =
17923 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17924 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17925 struct glyph_row *first_text_row
17926 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17928 *delta = Z - Z_old;
17929 *delta_bytes = Z_BYTE - Z_BYTE_old;
17931 /* Set last_unchanged_pos to the buffer position of the last
17932 character in the buffer that has not been changed. Z is the
17933 index + 1 of the last character in current_buffer, i.e. by
17934 subtracting END_UNCHANGED we get the index of the last
17935 unchanged character, and we have to add BEG to get its buffer
17936 position. */
17937 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17938 last_unchanged_pos_old = last_unchanged_pos - *delta;
17940 /* Search backward from ROW for a row displaying a line that
17941 starts at a minimum position >= last_unchanged_pos_old. */
17942 for (; row > first_text_row; --row)
17944 /* This used to abort, but it can happen.
17945 It is ok to just stop the search instead here. KFS. */
17946 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17947 break;
17949 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17950 row_found = row;
17954 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17956 return row_found;
17960 /* Make sure that glyph rows in the current matrix of window W
17961 reference the same glyph memory as corresponding rows in the
17962 frame's frame matrix. This function is called after scrolling W's
17963 current matrix on a terminal frame in try_window_id and
17964 try_window_reusing_current_matrix. */
17966 static void
17967 sync_frame_with_window_matrix_rows (struct window *w)
17969 struct frame *f = XFRAME (w->frame);
17970 struct glyph_row *window_row, *window_row_end, *frame_row;
17972 /* Preconditions: W must be a leaf window and full-width. Its frame
17973 must have a frame matrix. */
17974 eassert (BUFFERP (w->contents));
17975 eassert (WINDOW_FULL_WIDTH_P (w));
17976 eassert (!FRAME_WINDOW_P (f));
17978 /* If W is a full-width window, glyph pointers in W's current matrix
17979 have, by definition, to be the same as glyph pointers in the
17980 corresponding frame matrix. Note that frame matrices have no
17981 marginal areas (see build_frame_matrix). */
17982 window_row = w->current_matrix->rows;
17983 window_row_end = window_row + w->current_matrix->nrows;
17984 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17985 while (window_row < window_row_end)
17987 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17988 struct glyph *end = window_row->glyphs[LAST_AREA];
17990 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17991 frame_row->glyphs[TEXT_AREA] = start;
17992 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17993 frame_row->glyphs[LAST_AREA] = end;
17995 /* Disable frame rows whose corresponding window rows have
17996 been disabled in try_window_id. */
17997 if (!window_row->enabled_p)
17998 frame_row->enabled_p = false;
18000 ++window_row, ++frame_row;
18005 /* Find the glyph row in window W containing CHARPOS. Consider all
18006 rows between START and END (not inclusive). END null means search
18007 all rows to the end of the display area of W. Value is the row
18008 containing CHARPOS or null. */
18010 struct glyph_row *
18011 row_containing_pos (struct window *w, ptrdiff_t charpos,
18012 struct glyph_row *start, struct glyph_row *end, int dy)
18014 struct glyph_row *row = start;
18015 struct glyph_row *best_row = NULL;
18016 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18017 int last_y;
18019 /* If we happen to start on a header-line, skip that. */
18020 if (row->mode_line_p)
18021 ++row;
18023 if ((end && row >= end) || !row->enabled_p)
18024 return NULL;
18026 last_y = window_text_bottom_y (w) - dy;
18028 while (true)
18030 /* Give up if we have gone too far. */
18031 if ((end && row >= end) || !row->enabled_p)
18032 return NULL;
18033 /* This formerly returned if they were equal.
18034 I think that both quantities are of a "last plus one" type;
18035 if so, when they are equal, the row is within the screen. -- rms. */
18036 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18037 return NULL;
18039 /* If it is in this row, return this row. */
18040 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18041 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18042 /* The end position of a row equals the start
18043 position of the next row. If CHARPOS is there, we
18044 would rather consider it displayed in the next
18045 line, except when this line ends in ZV. */
18046 && !row_for_charpos_p (row, charpos)))
18047 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18049 struct glyph *g;
18051 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18052 || (!best_row && !row->continued_p))
18053 return row;
18054 /* In bidi-reordered rows, there could be several rows whose
18055 edges surround CHARPOS, all of these rows belonging to
18056 the same continued line. We need to find the row which
18057 fits CHARPOS the best. */
18058 for (g = row->glyphs[TEXT_AREA];
18059 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18060 g++)
18062 if (!STRINGP (g->object))
18064 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18066 mindif = eabs (g->charpos - charpos);
18067 best_row = row;
18068 /* Exact match always wins. */
18069 if (mindif == 0)
18070 return best_row;
18075 else if (best_row && !row->continued_p)
18076 return best_row;
18077 ++row;
18082 /* Try to redisplay window W by reusing its existing display. W's
18083 current matrix must be up to date when this function is called,
18084 i.e., window_end_valid must be true.
18086 Value is
18088 >= 1 if successful, i.e. display has been updated
18089 specifically:
18090 1 means the changes were in front of a newline that precedes
18091 the window start, and the whole current matrix was reused
18092 2 means the changes were after the last position displayed
18093 in the window, and the whole current matrix was reused
18094 3 means portions of the current matrix were reused, while
18095 some of the screen lines were redrawn
18096 -1 if redisplay with same window start is known not to succeed
18097 0 if otherwise unsuccessful
18099 The following steps are performed:
18101 1. Find the last row in the current matrix of W that is not
18102 affected by changes at the start of current_buffer. If no such row
18103 is found, give up.
18105 2. Find the first row in W's current matrix that is not affected by
18106 changes at the end of current_buffer. Maybe there is no such row.
18108 3. Display lines beginning with the row + 1 found in step 1 to the
18109 row found in step 2 or, if step 2 didn't find a row, to the end of
18110 the window.
18112 4. If cursor is not known to appear on the window, give up.
18114 5. If display stopped at the row found in step 2, scroll the
18115 display and current matrix as needed.
18117 6. Maybe display some lines at the end of W, if we must. This can
18118 happen under various circumstances, like a partially visible line
18119 becoming fully visible, or because newly displayed lines are displayed
18120 in smaller font sizes.
18122 7. Update W's window end information. */
18124 static int
18125 try_window_id (struct window *w)
18127 struct frame *f = XFRAME (w->frame);
18128 struct glyph_matrix *current_matrix = w->current_matrix;
18129 struct glyph_matrix *desired_matrix = w->desired_matrix;
18130 struct glyph_row *last_unchanged_at_beg_row;
18131 struct glyph_row *first_unchanged_at_end_row;
18132 struct glyph_row *row;
18133 struct glyph_row *bottom_row;
18134 int bottom_vpos;
18135 struct it it;
18136 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18137 int dvpos, dy;
18138 struct text_pos start_pos;
18139 struct run run;
18140 int first_unchanged_at_end_vpos = 0;
18141 struct glyph_row *last_text_row, *last_text_row_at_end;
18142 struct text_pos start;
18143 ptrdiff_t first_changed_charpos, last_changed_charpos;
18145 #ifdef GLYPH_DEBUG
18146 if (inhibit_try_window_id)
18147 return 0;
18148 #endif
18150 /* This is handy for debugging. */
18151 #if false
18152 #define GIVE_UP(X) \
18153 do { \
18154 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18155 return 0; \
18156 } while (false)
18157 #else
18158 #define GIVE_UP(X) return 0
18159 #endif
18161 SET_TEXT_POS_FROM_MARKER (start, w->start);
18163 /* Don't use this for mini-windows because these can show
18164 messages and mini-buffers, and we don't handle that here. */
18165 if (MINI_WINDOW_P (w))
18166 GIVE_UP (1);
18168 /* This flag is used to prevent redisplay optimizations. */
18169 if (windows_or_buffers_changed || f->cursor_type_changed)
18170 GIVE_UP (2);
18172 /* This function's optimizations cannot be used if overlays have
18173 changed in the buffer displayed by the window, so give up if they
18174 have. */
18175 if (w->last_overlay_modified != OVERLAY_MODIFF)
18176 GIVE_UP (200);
18178 /* Verify that narrowing has not changed.
18179 Also verify that we were not told to prevent redisplay optimizations.
18180 It would be nice to further
18181 reduce the number of cases where this prevents try_window_id. */
18182 if (current_buffer->clip_changed
18183 || current_buffer->prevent_redisplay_optimizations_p)
18184 GIVE_UP (3);
18186 /* Window must either use window-based redisplay or be full width. */
18187 if (!FRAME_WINDOW_P (f)
18188 && (!FRAME_LINE_INS_DEL_OK (f)
18189 || !WINDOW_FULL_WIDTH_P (w)))
18190 GIVE_UP (4);
18192 /* Give up if point is known NOT to appear in W. */
18193 if (PT < CHARPOS (start))
18194 GIVE_UP (5);
18196 /* Another way to prevent redisplay optimizations. */
18197 if (w->last_modified == 0)
18198 GIVE_UP (6);
18200 /* Verify that window is not hscrolled. */
18201 if (w->hscroll != 0)
18202 GIVE_UP (7);
18204 /* Verify that display wasn't paused. */
18205 if (!w->window_end_valid)
18206 GIVE_UP (8);
18208 /* Likewise if highlighting trailing whitespace. */
18209 if (!NILP (Vshow_trailing_whitespace))
18210 GIVE_UP (11);
18212 /* Can't use this if overlay arrow position and/or string have
18213 changed. */
18214 if (overlay_arrows_changed_p ())
18215 GIVE_UP (12);
18217 /* When word-wrap is on, adding a space to the first word of a
18218 wrapped line can change the wrap position, altering the line
18219 above it. It might be worthwhile to handle this more
18220 intelligently, but for now just redisplay from scratch. */
18221 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18222 GIVE_UP (21);
18224 /* Under bidi reordering, adding or deleting a character in the
18225 beginning of a paragraph, before the first strong directional
18226 character, can change the base direction of the paragraph (unless
18227 the buffer specifies a fixed paragraph direction), which will
18228 require redisplaying the whole paragraph. It might be worthwhile
18229 to find the paragraph limits and widen the range of redisplayed
18230 lines to that, but for now just give up this optimization and
18231 redisplay from scratch. */
18232 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18233 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18234 GIVE_UP (22);
18236 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18237 to that variable require thorough redisplay. */
18238 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18239 GIVE_UP (23);
18241 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18242 only if buffer has really changed. The reason is that the gap is
18243 initially at Z for freshly visited files. The code below would
18244 set end_unchanged to 0 in that case. */
18245 if (MODIFF > SAVE_MODIFF
18246 /* This seems to happen sometimes after saving a buffer. */
18247 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18249 if (GPT - BEG < BEG_UNCHANGED)
18250 BEG_UNCHANGED = GPT - BEG;
18251 if (Z - GPT < END_UNCHANGED)
18252 END_UNCHANGED = Z - GPT;
18255 /* The position of the first and last character that has been changed. */
18256 first_changed_charpos = BEG + BEG_UNCHANGED;
18257 last_changed_charpos = Z - END_UNCHANGED;
18259 /* If window starts after a line end, and the last change is in
18260 front of that newline, then changes don't affect the display.
18261 This case happens with stealth-fontification. Note that although
18262 the display is unchanged, glyph positions in the matrix have to
18263 be adjusted, of course. */
18264 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18265 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18266 && ((last_changed_charpos < CHARPOS (start)
18267 && CHARPOS (start) == BEGV)
18268 || (last_changed_charpos < CHARPOS (start) - 1
18269 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18271 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18272 struct glyph_row *r0;
18274 /* Compute how many chars/bytes have been added to or removed
18275 from the buffer. */
18276 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18277 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18278 Z_delta = Z - Z_old;
18279 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18281 /* Give up if PT is not in the window. Note that it already has
18282 been checked at the start of try_window_id that PT is not in
18283 front of the window start. */
18284 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18285 GIVE_UP (13);
18287 /* If window start is unchanged, we can reuse the whole matrix
18288 as is, after adjusting glyph positions. No need to compute
18289 the window end again, since its offset from Z hasn't changed. */
18290 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18291 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18292 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18293 /* PT must not be in a partially visible line. */
18294 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18295 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18297 /* Adjust positions in the glyph matrix. */
18298 if (Z_delta || Z_delta_bytes)
18300 struct glyph_row *r1
18301 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18302 increment_matrix_positions (w->current_matrix,
18303 MATRIX_ROW_VPOS (r0, current_matrix),
18304 MATRIX_ROW_VPOS (r1, current_matrix),
18305 Z_delta, Z_delta_bytes);
18308 /* Set the cursor. */
18309 row = row_containing_pos (w, PT, r0, NULL, 0);
18310 if (row)
18311 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18312 return 1;
18316 /* Handle the case that changes are all below what is displayed in
18317 the window, and that PT is in the window. This shortcut cannot
18318 be taken if ZV is visible in the window, and text has been added
18319 there that is visible in the window. */
18320 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18321 /* ZV is not visible in the window, or there are no
18322 changes at ZV, actually. */
18323 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18324 || first_changed_charpos == last_changed_charpos))
18326 struct glyph_row *r0;
18328 /* Give up if PT is not in the window. Note that it already has
18329 been checked at the start of try_window_id that PT is not in
18330 front of the window start. */
18331 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18332 GIVE_UP (14);
18334 /* If window start is unchanged, we can reuse the whole matrix
18335 as is, without changing glyph positions since no text has
18336 been added/removed in front of the window end. */
18337 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18338 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18339 /* PT must not be in a partially visible line. */
18340 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18341 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18343 /* We have to compute the window end anew since text
18344 could have been added/removed after it. */
18345 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18346 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18348 /* Set the cursor. */
18349 row = row_containing_pos (w, PT, r0, NULL, 0);
18350 if (row)
18351 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18352 return 2;
18356 /* Give up if window start is in the changed area.
18358 The condition used to read
18360 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18362 but why that was tested escapes me at the moment. */
18363 if (CHARPOS (start) >= first_changed_charpos
18364 && CHARPOS (start) <= last_changed_charpos)
18365 GIVE_UP (15);
18367 /* Check that window start agrees with the start of the first glyph
18368 row in its current matrix. Check this after we know the window
18369 start is not in changed text, otherwise positions would not be
18370 comparable. */
18371 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18372 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18373 GIVE_UP (16);
18375 /* Give up if the window ends in strings. Overlay strings
18376 at the end are difficult to handle, so don't try. */
18377 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18378 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18379 GIVE_UP (20);
18381 /* Compute the position at which we have to start displaying new
18382 lines. Some of the lines at the top of the window might be
18383 reusable because they are not displaying changed text. Find the
18384 last row in W's current matrix not affected by changes at the
18385 start of current_buffer. Value is null if changes start in the
18386 first line of window. */
18387 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18388 if (last_unchanged_at_beg_row)
18390 /* Avoid starting to display in the middle of a character, a TAB
18391 for instance. This is easier than to set up the iterator
18392 exactly, and it's not a frequent case, so the additional
18393 effort wouldn't really pay off. */
18394 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18395 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18396 && last_unchanged_at_beg_row > w->current_matrix->rows)
18397 --last_unchanged_at_beg_row;
18399 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18400 GIVE_UP (17);
18402 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18403 GIVE_UP (18);
18404 start_pos = it.current.pos;
18406 /* Start displaying new lines in the desired matrix at the same
18407 vpos we would use in the current matrix, i.e. below
18408 last_unchanged_at_beg_row. */
18409 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18410 current_matrix);
18411 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18412 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18414 eassert (it.hpos == 0 && it.current_x == 0);
18416 else
18418 /* There are no reusable lines at the start of the window.
18419 Start displaying in the first text line. */
18420 start_display (&it, w, start);
18421 it.vpos = it.first_vpos;
18422 start_pos = it.current.pos;
18425 /* Find the first row that is not affected by changes at the end of
18426 the buffer. Value will be null if there is no unchanged row, in
18427 which case we must redisplay to the end of the window. delta
18428 will be set to the value by which buffer positions beginning with
18429 first_unchanged_at_end_row have to be adjusted due to text
18430 changes. */
18431 first_unchanged_at_end_row
18432 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18433 IF_DEBUG (debug_delta = delta);
18434 IF_DEBUG (debug_delta_bytes = delta_bytes);
18436 /* Set stop_pos to the buffer position up to which we will have to
18437 display new lines. If first_unchanged_at_end_row != NULL, this
18438 is the buffer position of the start of the line displayed in that
18439 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18440 that we don't stop at a buffer position. */
18441 stop_pos = 0;
18442 if (first_unchanged_at_end_row)
18444 eassert (last_unchanged_at_beg_row == NULL
18445 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18447 /* If this is a continuation line, move forward to the next one
18448 that isn't. Changes in lines above affect this line.
18449 Caution: this may move first_unchanged_at_end_row to a row
18450 not displaying text. */
18451 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18452 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18453 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18454 < it.last_visible_y))
18455 ++first_unchanged_at_end_row;
18457 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18458 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18459 >= it.last_visible_y))
18460 first_unchanged_at_end_row = NULL;
18461 else
18463 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18464 + delta);
18465 first_unchanged_at_end_vpos
18466 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18467 eassert (stop_pos >= Z - END_UNCHANGED);
18470 else if (last_unchanged_at_beg_row == NULL)
18471 GIVE_UP (19);
18474 #ifdef GLYPH_DEBUG
18476 /* Either there is no unchanged row at the end, or the one we have
18477 now displays text. This is a necessary condition for the window
18478 end pos calculation at the end of this function. */
18479 eassert (first_unchanged_at_end_row == NULL
18480 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18482 debug_last_unchanged_at_beg_vpos
18483 = (last_unchanged_at_beg_row
18484 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18485 : -1);
18486 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18488 #endif /* GLYPH_DEBUG */
18491 /* Display new lines. Set last_text_row to the last new line
18492 displayed which has text on it, i.e. might end up as being the
18493 line where the window_end_vpos is. */
18494 w->cursor.vpos = -1;
18495 last_text_row = NULL;
18496 overlay_arrow_seen = false;
18497 if (it.current_y < it.last_visible_y
18498 && !f->fonts_changed
18499 && (first_unchanged_at_end_row == NULL
18500 || IT_CHARPOS (it) < stop_pos))
18501 it.glyph_row->reversed_p = false;
18502 while (it.current_y < it.last_visible_y
18503 && !f->fonts_changed
18504 && (first_unchanged_at_end_row == NULL
18505 || IT_CHARPOS (it) < stop_pos))
18507 if (display_line (&it))
18508 last_text_row = it.glyph_row - 1;
18511 if (f->fonts_changed)
18512 return -1;
18514 /* The redisplay iterations in display_line above could have
18515 triggered font-lock, which could have done something that
18516 invalidates IT->w window's end-point information, on which we
18517 rely below. E.g., one package, which will remain unnamed, used
18518 to install a font-lock-fontify-region-function that called
18519 bury-buffer, whose side effect is to switch the buffer displayed
18520 by IT->w, and that predictably resets IT->w's window_end_valid
18521 flag, which we already tested at the entry to this function.
18522 Amply punish such packages/modes by giving up on this
18523 optimization in those cases. */
18524 if (!w->window_end_valid)
18526 clear_glyph_matrix (w->desired_matrix);
18527 return -1;
18530 /* Compute differences in buffer positions, y-positions etc. for
18531 lines reused at the bottom of the window. Compute what we can
18532 scroll. */
18533 if (first_unchanged_at_end_row
18534 /* No lines reused because we displayed everything up to the
18535 bottom of the window. */
18536 && it.current_y < it.last_visible_y)
18538 dvpos = (it.vpos
18539 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18540 current_matrix));
18541 dy = it.current_y - first_unchanged_at_end_row->y;
18542 run.current_y = first_unchanged_at_end_row->y;
18543 run.desired_y = run.current_y + dy;
18544 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18546 else
18548 delta = delta_bytes = dvpos = dy
18549 = run.current_y = run.desired_y = run.height = 0;
18550 first_unchanged_at_end_row = NULL;
18552 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18555 /* Find the cursor if not already found. We have to decide whether
18556 PT will appear on this window (it sometimes doesn't, but this is
18557 not a very frequent case.) This decision has to be made before
18558 the current matrix is altered. A value of cursor.vpos < 0 means
18559 that PT is either in one of the lines beginning at
18560 first_unchanged_at_end_row or below the window. Don't care for
18561 lines that might be displayed later at the window end; as
18562 mentioned, this is not a frequent case. */
18563 if (w->cursor.vpos < 0)
18565 /* Cursor in unchanged rows at the top? */
18566 if (PT < CHARPOS (start_pos)
18567 && last_unchanged_at_beg_row)
18569 row = row_containing_pos (w, PT,
18570 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18571 last_unchanged_at_beg_row + 1, 0);
18572 if (row)
18573 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18576 /* Start from first_unchanged_at_end_row looking for PT. */
18577 else if (first_unchanged_at_end_row)
18579 row = row_containing_pos (w, PT - delta,
18580 first_unchanged_at_end_row, NULL, 0);
18581 if (row)
18582 set_cursor_from_row (w, row, w->current_matrix, delta,
18583 delta_bytes, dy, dvpos);
18586 /* Give up if cursor was not found. */
18587 if (w->cursor.vpos < 0)
18589 clear_glyph_matrix (w->desired_matrix);
18590 return -1;
18594 /* Don't let the cursor end in the scroll margins. */
18596 int this_scroll_margin, cursor_height;
18597 int frame_line_height = default_line_pixel_height (w);
18598 int window_total_lines
18599 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18601 this_scroll_margin =
18602 max (0, min (scroll_margin, window_total_lines / 4));
18603 this_scroll_margin *= frame_line_height;
18604 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18606 if ((w->cursor.y < this_scroll_margin
18607 && CHARPOS (start) > BEGV)
18608 /* Old redisplay didn't take scroll margin into account at the bottom,
18609 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18610 || (w->cursor.y + (make_cursor_line_fully_visible_p
18611 ? cursor_height + this_scroll_margin
18612 : 1)) > it.last_visible_y)
18614 w->cursor.vpos = -1;
18615 clear_glyph_matrix (w->desired_matrix);
18616 return -1;
18620 /* Scroll the display. Do it before changing the current matrix so
18621 that xterm.c doesn't get confused about where the cursor glyph is
18622 found. */
18623 if (dy && run.height)
18625 update_begin (f);
18627 if (FRAME_WINDOW_P (f))
18629 FRAME_RIF (f)->update_window_begin_hook (w);
18630 FRAME_RIF (f)->clear_window_mouse_face (w);
18631 FRAME_RIF (f)->scroll_run_hook (w, &run);
18632 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18634 else
18636 /* Terminal frame. In this case, dvpos gives the number of
18637 lines to scroll by; dvpos < 0 means scroll up. */
18638 int from_vpos
18639 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18640 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18641 int end = (WINDOW_TOP_EDGE_LINE (w)
18642 + WINDOW_WANTS_HEADER_LINE_P (w)
18643 + window_internal_height (w));
18645 #if defined (HAVE_GPM) || defined (MSDOS)
18646 x_clear_window_mouse_face (w);
18647 #endif
18648 /* Perform the operation on the screen. */
18649 if (dvpos > 0)
18651 /* Scroll last_unchanged_at_beg_row to the end of the
18652 window down dvpos lines. */
18653 set_terminal_window (f, end);
18655 /* On dumb terminals delete dvpos lines at the end
18656 before inserting dvpos empty lines. */
18657 if (!FRAME_SCROLL_REGION_OK (f))
18658 ins_del_lines (f, end - dvpos, -dvpos);
18660 /* Insert dvpos empty lines in front of
18661 last_unchanged_at_beg_row. */
18662 ins_del_lines (f, from, dvpos);
18664 else if (dvpos < 0)
18666 /* Scroll up last_unchanged_at_beg_vpos to the end of
18667 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18668 set_terminal_window (f, end);
18670 /* Delete dvpos lines in front of
18671 last_unchanged_at_beg_vpos. ins_del_lines will set
18672 the cursor to the given vpos and emit |dvpos| delete
18673 line sequences. */
18674 ins_del_lines (f, from + dvpos, dvpos);
18676 /* On a dumb terminal insert dvpos empty lines at the
18677 end. */
18678 if (!FRAME_SCROLL_REGION_OK (f))
18679 ins_del_lines (f, end + dvpos, -dvpos);
18682 set_terminal_window (f, 0);
18685 update_end (f);
18688 /* Shift reused rows of the current matrix to the right position.
18689 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18690 text. */
18691 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18692 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18693 if (dvpos < 0)
18695 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18696 bottom_vpos, dvpos);
18697 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18698 bottom_vpos);
18700 else if (dvpos > 0)
18702 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18703 bottom_vpos, dvpos);
18704 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18705 first_unchanged_at_end_vpos + dvpos);
18708 /* For frame-based redisplay, make sure that current frame and window
18709 matrix are in sync with respect to glyph memory. */
18710 if (!FRAME_WINDOW_P (f))
18711 sync_frame_with_window_matrix_rows (w);
18713 /* Adjust buffer positions in reused rows. */
18714 if (delta || delta_bytes)
18715 increment_matrix_positions (current_matrix,
18716 first_unchanged_at_end_vpos + dvpos,
18717 bottom_vpos, delta, delta_bytes);
18719 /* Adjust Y positions. */
18720 if (dy)
18721 shift_glyph_matrix (w, current_matrix,
18722 first_unchanged_at_end_vpos + dvpos,
18723 bottom_vpos, dy);
18725 if (first_unchanged_at_end_row)
18727 first_unchanged_at_end_row += dvpos;
18728 if (first_unchanged_at_end_row->y >= it.last_visible_y
18729 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18730 first_unchanged_at_end_row = NULL;
18733 /* If scrolling up, there may be some lines to display at the end of
18734 the window. */
18735 last_text_row_at_end = NULL;
18736 if (dy < 0)
18738 /* Scrolling up can leave for example a partially visible line
18739 at the end of the window to be redisplayed. */
18740 /* Set last_row to the glyph row in the current matrix where the
18741 window end line is found. It has been moved up or down in
18742 the matrix by dvpos. */
18743 int last_vpos = w->window_end_vpos + dvpos;
18744 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18746 /* If last_row is the window end line, it should display text. */
18747 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18749 /* If window end line was partially visible before, begin
18750 displaying at that line. Otherwise begin displaying with the
18751 line following it. */
18752 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18754 init_to_row_start (&it, w, last_row);
18755 it.vpos = last_vpos;
18756 it.current_y = last_row->y;
18758 else
18760 init_to_row_end (&it, w, last_row);
18761 it.vpos = 1 + last_vpos;
18762 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18763 ++last_row;
18766 /* We may start in a continuation line. If so, we have to
18767 get the right continuation_lines_width and current_x. */
18768 it.continuation_lines_width = last_row->continuation_lines_width;
18769 it.hpos = it.current_x = 0;
18771 /* Display the rest of the lines at the window end. */
18772 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18773 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18775 /* Is it always sure that the display agrees with lines in
18776 the current matrix? I don't think so, so we mark rows
18777 displayed invalid in the current matrix by setting their
18778 enabled_p flag to false. */
18779 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18780 if (display_line (&it))
18781 last_text_row_at_end = it.glyph_row - 1;
18785 /* Update window_end_pos and window_end_vpos. */
18786 if (first_unchanged_at_end_row && !last_text_row_at_end)
18788 /* Window end line if one of the preserved rows from the current
18789 matrix. Set row to the last row displaying text in current
18790 matrix starting at first_unchanged_at_end_row, after
18791 scrolling. */
18792 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18793 row = find_last_row_displaying_text (w->current_matrix, &it,
18794 first_unchanged_at_end_row);
18795 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18796 adjust_window_ends (w, row, true);
18797 eassert (w->window_end_bytepos >= 0);
18798 IF_DEBUG (debug_method_add (w, "A"));
18800 else if (last_text_row_at_end)
18802 adjust_window_ends (w, last_text_row_at_end, false);
18803 eassert (w->window_end_bytepos >= 0);
18804 IF_DEBUG (debug_method_add (w, "B"));
18806 else if (last_text_row)
18808 /* We have displayed either to the end of the window or at the
18809 end of the window, i.e. the last row with text is to be found
18810 in the desired matrix. */
18811 adjust_window_ends (w, last_text_row, false);
18812 eassert (w->window_end_bytepos >= 0);
18814 else if (first_unchanged_at_end_row == NULL
18815 && last_text_row == NULL
18816 && last_text_row_at_end == NULL)
18818 /* Displayed to end of window, but no line containing text was
18819 displayed. Lines were deleted at the end of the window. */
18820 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18821 int vpos = w->window_end_vpos;
18822 struct glyph_row *current_row = current_matrix->rows + vpos;
18823 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18825 for (row = NULL; !row; --vpos, --current_row, --desired_row)
18827 eassert (first_vpos <= vpos);
18828 if (desired_row->enabled_p)
18830 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18831 row = desired_row;
18833 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18834 row = current_row;
18837 w->window_end_vpos = vpos + 1;
18838 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18839 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18840 eassert (w->window_end_bytepos >= 0);
18841 IF_DEBUG (debug_method_add (w, "C"));
18843 else
18844 emacs_abort ();
18846 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18847 debug_end_vpos = w->window_end_vpos));
18849 /* Record that display has not been completed. */
18850 w->window_end_valid = false;
18851 w->desired_matrix->no_scrolling_p = true;
18852 return 3;
18854 #undef GIVE_UP
18859 /***********************************************************************
18860 More debugging support
18861 ***********************************************************************/
18863 #ifdef GLYPH_DEBUG
18865 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18866 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18867 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18870 /* Dump the contents of glyph matrix MATRIX on stderr.
18872 GLYPHS 0 means don't show glyph contents.
18873 GLYPHS 1 means show glyphs in short form
18874 GLYPHS > 1 means show glyphs in long form. */
18876 void
18877 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18879 int i;
18880 for (i = 0; i < matrix->nrows; ++i)
18881 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18885 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18886 the glyph row and area where the glyph comes from. */
18888 void
18889 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18891 if (glyph->type == CHAR_GLYPH
18892 || glyph->type == GLYPHLESS_GLYPH)
18894 fprintf (stderr,
18895 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18896 glyph - row->glyphs[TEXT_AREA],
18897 (glyph->type == CHAR_GLYPH
18898 ? 'C'
18899 : 'G'),
18900 glyph->charpos,
18901 (BUFFERP (glyph->object)
18902 ? 'B'
18903 : (STRINGP (glyph->object)
18904 ? 'S'
18905 : (NILP (glyph->object)
18906 ? '0'
18907 : '-'))),
18908 glyph->pixel_width,
18909 glyph->u.ch,
18910 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18911 ? glyph->u.ch
18912 : '.'),
18913 glyph->face_id,
18914 glyph->left_box_line_p,
18915 glyph->right_box_line_p);
18917 else if (glyph->type == STRETCH_GLYPH)
18919 fprintf (stderr,
18920 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18921 glyph - row->glyphs[TEXT_AREA],
18922 'S',
18923 glyph->charpos,
18924 (BUFFERP (glyph->object)
18925 ? 'B'
18926 : (STRINGP (glyph->object)
18927 ? 'S'
18928 : (NILP (glyph->object)
18929 ? '0'
18930 : '-'))),
18931 glyph->pixel_width,
18933 ' ',
18934 glyph->face_id,
18935 glyph->left_box_line_p,
18936 glyph->right_box_line_p);
18938 else if (glyph->type == IMAGE_GLYPH)
18940 fprintf (stderr,
18941 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18942 glyph - row->glyphs[TEXT_AREA],
18943 'I',
18944 glyph->charpos,
18945 (BUFFERP (glyph->object)
18946 ? 'B'
18947 : (STRINGP (glyph->object)
18948 ? 'S'
18949 : (NILP (glyph->object)
18950 ? '0'
18951 : '-'))),
18952 glyph->pixel_width,
18953 glyph->u.img_id,
18954 '.',
18955 glyph->face_id,
18956 glyph->left_box_line_p,
18957 glyph->right_box_line_p);
18959 else if (glyph->type == COMPOSITE_GLYPH)
18961 fprintf (stderr,
18962 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18963 glyph - row->glyphs[TEXT_AREA],
18964 '+',
18965 glyph->charpos,
18966 (BUFFERP (glyph->object)
18967 ? 'B'
18968 : (STRINGP (glyph->object)
18969 ? 'S'
18970 : (NILP (glyph->object)
18971 ? '0'
18972 : '-'))),
18973 glyph->pixel_width,
18974 glyph->u.cmp.id);
18975 if (glyph->u.cmp.automatic)
18976 fprintf (stderr,
18977 "[%d-%d]",
18978 glyph->slice.cmp.from, glyph->slice.cmp.to);
18979 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18980 glyph->face_id,
18981 glyph->left_box_line_p,
18982 glyph->right_box_line_p);
18984 else if (glyph->type == XWIDGET_GLYPH)
18986 #ifndef HAVE_XWIDGETS
18987 eassume (false);
18988 #else
18989 fprintf (stderr,
18990 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18991 glyph - row->glyphs[TEXT_AREA],
18992 'X',
18993 glyph->charpos,
18994 (BUFFERP (glyph->object)
18995 ? 'B'
18996 : (STRINGP (glyph->object)
18997 ? 'S'
18998 : '-')),
18999 glyph->pixel_width,
19000 glyph->u.xwidget,
19001 '.',
19002 glyph->face_id,
19003 glyph->left_box_line_p,
19004 glyph->right_box_line_p);
19005 #endif
19010 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19011 GLYPHS 0 means don't show glyph contents.
19012 GLYPHS 1 means show glyphs in short form
19013 GLYPHS > 1 means show glyphs in long form. */
19015 void
19016 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19018 if (glyphs != 1)
19020 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19021 fprintf (stderr, "==============================================================================\n");
19023 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19024 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19025 vpos,
19026 MATRIX_ROW_START_CHARPOS (row),
19027 MATRIX_ROW_END_CHARPOS (row),
19028 row->used[TEXT_AREA],
19029 row->contains_overlapping_glyphs_p,
19030 row->enabled_p,
19031 row->truncated_on_left_p,
19032 row->truncated_on_right_p,
19033 row->continued_p,
19034 MATRIX_ROW_CONTINUATION_LINE_P (row),
19035 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19036 row->ends_at_zv_p,
19037 row->fill_line_p,
19038 row->ends_in_middle_of_char_p,
19039 row->starts_in_middle_of_char_p,
19040 row->mouse_face_p,
19041 row->x,
19042 row->y,
19043 row->pixel_width,
19044 row->height,
19045 row->visible_height,
19046 row->ascent,
19047 row->phys_ascent);
19048 /* The next 3 lines should align to "Start" in the header. */
19049 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19050 row->end.overlay_string_index,
19051 row->continuation_lines_width);
19052 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19053 CHARPOS (row->start.string_pos),
19054 CHARPOS (row->end.string_pos));
19055 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19056 row->end.dpvec_index);
19059 if (glyphs > 1)
19061 int area;
19063 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19065 struct glyph *glyph = row->glyphs[area];
19066 struct glyph *glyph_end = glyph + row->used[area];
19068 /* Glyph for a line end in text. */
19069 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19070 ++glyph_end;
19072 if (glyph < glyph_end)
19073 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19075 for (; glyph < glyph_end; ++glyph)
19076 dump_glyph (row, glyph, area);
19079 else if (glyphs == 1)
19081 int area;
19082 char s[SHRT_MAX + 4];
19084 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19086 int i;
19088 for (i = 0; i < row->used[area]; ++i)
19090 struct glyph *glyph = row->glyphs[area] + i;
19091 if (i == row->used[area] - 1
19092 && area == TEXT_AREA
19093 && NILP (glyph->object)
19094 && glyph->type == CHAR_GLYPH
19095 && glyph->u.ch == ' ')
19097 strcpy (&s[i], "[\\n]");
19098 i += 4;
19100 else if (glyph->type == CHAR_GLYPH
19101 && glyph->u.ch < 0x80
19102 && glyph->u.ch >= ' ')
19103 s[i] = glyph->u.ch;
19104 else
19105 s[i] = '.';
19108 s[i] = '\0';
19109 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19115 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19116 Sdump_glyph_matrix, 0, 1, "p",
19117 doc: /* Dump the current matrix of the selected window to stderr.
19118 Shows contents of glyph row structures. With non-nil
19119 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19120 glyphs in short form, otherwise show glyphs in long form.
19122 Interactively, no argument means show glyphs in short form;
19123 with numeric argument, its value is passed as the GLYPHS flag. */)
19124 (Lisp_Object glyphs)
19126 struct window *w = XWINDOW (selected_window);
19127 struct buffer *buffer = XBUFFER (w->contents);
19129 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19130 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19131 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19132 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19133 fprintf (stderr, "=============================================\n");
19134 dump_glyph_matrix (w->current_matrix,
19135 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19136 return Qnil;
19140 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19141 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19142 Only text-mode frames have frame glyph matrices. */)
19143 (void)
19145 struct frame *f = XFRAME (selected_frame);
19147 if (f->current_matrix)
19148 dump_glyph_matrix (f->current_matrix, 1);
19149 else
19150 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19151 return Qnil;
19155 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19156 doc: /* Dump glyph row ROW to stderr.
19157 GLYPH 0 means don't dump glyphs.
19158 GLYPH 1 means dump glyphs in short form.
19159 GLYPH > 1 or omitted means dump glyphs in long form. */)
19160 (Lisp_Object row, Lisp_Object glyphs)
19162 struct glyph_matrix *matrix;
19163 EMACS_INT vpos;
19165 CHECK_NUMBER (row);
19166 matrix = XWINDOW (selected_window)->current_matrix;
19167 vpos = XINT (row);
19168 if (vpos >= 0 && vpos < matrix->nrows)
19169 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19170 vpos,
19171 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19172 return Qnil;
19176 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19177 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19178 GLYPH 0 means don't dump glyphs.
19179 GLYPH 1 means dump glyphs in short form.
19180 GLYPH > 1 or omitted means dump glyphs in long form.
19182 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19183 do nothing. */)
19184 (Lisp_Object row, Lisp_Object glyphs)
19186 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19187 struct frame *sf = SELECTED_FRAME ();
19188 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19189 EMACS_INT vpos;
19191 CHECK_NUMBER (row);
19192 vpos = XINT (row);
19193 if (vpos >= 0 && vpos < m->nrows)
19194 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19195 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19196 #endif
19197 return Qnil;
19201 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19202 doc: /* Toggle tracing of redisplay.
19203 With ARG, turn tracing on if and only if ARG is positive. */)
19204 (Lisp_Object arg)
19206 if (NILP (arg))
19207 trace_redisplay_p = !trace_redisplay_p;
19208 else
19210 arg = Fprefix_numeric_value (arg);
19211 trace_redisplay_p = XINT (arg) > 0;
19214 return Qnil;
19218 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19219 doc: /* Like `format', but print result to stderr.
19220 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19221 (ptrdiff_t nargs, Lisp_Object *args)
19223 Lisp_Object s = Fformat (nargs, args);
19224 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19225 return Qnil;
19228 #endif /* GLYPH_DEBUG */
19232 /***********************************************************************
19233 Building Desired Matrix Rows
19234 ***********************************************************************/
19236 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19237 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19239 static struct glyph_row *
19240 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19242 struct frame *f = XFRAME (WINDOW_FRAME (w));
19243 struct buffer *buffer = XBUFFER (w->contents);
19244 struct buffer *old = current_buffer;
19245 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19246 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19247 const unsigned char *arrow_end = arrow_string + arrow_len;
19248 const unsigned char *p;
19249 struct it it;
19250 bool multibyte_p;
19251 int n_glyphs_before;
19253 set_buffer_temp (buffer);
19254 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19255 scratch_glyph_row.reversed_p = false;
19256 it.glyph_row->used[TEXT_AREA] = 0;
19257 SET_TEXT_POS (it.position, 0, 0);
19259 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19260 p = arrow_string;
19261 while (p < arrow_end)
19263 Lisp_Object face, ilisp;
19265 /* Get the next character. */
19266 if (multibyte_p)
19267 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19268 else
19270 it.c = it.char_to_display = *p, it.len = 1;
19271 if (! ASCII_CHAR_P (it.c))
19272 it.char_to_display = BYTE8_TO_CHAR (it.c);
19274 p += it.len;
19276 /* Get its face. */
19277 ilisp = make_number (p - arrow_string);
19278 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19279 it.face_id = compute_char_face (f, it.char_to_display, face);
19281 /* Compute its width, get its glyphs. */
19282 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19283 SET_TEXT_POS (it.position, -1, -1);
19284 PRODUCE_GLYPHS (&it);
19286 /* If this character doesn't fit any more in the line, we have
19287 to remove some glyphs. */
19288 if (it.current_x > it.last_visible_x)
19290 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19291 break;
19295 set_buffer_temp (old);
19296 return it.glyph_row;
19300 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19301 glyphs to insert is determined by produce_special_glyphs. */
19303 static void
19304 insert_left_trunc_glyphs (struct it *it)
19306 struct it truncate_it;
19307 struct glyph *from, *end, *to, *toend;
19309 eassert (!FRAME_WINDOW_P (it->f)
19310 || (!it->glyph_row->reversed_p
19311 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19312 || (it->glyph_row->reversed_p
19313 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19315 /* Get the truncation glyphs. */
19316 truncate_it = *it;
19317 truncate_it.current_x = 0;
19318 truncate_it.face_id = DEFAULT_FACE_ID;
19319 truncate_it.glyph_row = &scratch_glyph_row;
19320 truncate_it.area = TEXT_AREA;
19321 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19322 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19323 truncate_it.object = Qnil;
19324 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19326 /* Overwrite glyphs from IT with truncation glyphs. */
19327 if (!it->glyph_row->reversed_p)
19329 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19331 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19332 end = from + tused;
19333 to = it->glyph_row->glyphs[TEXT_AREA];
19334 toend = to + it->glyph_row->used[TEXT_AREA];
19335 if (FRAME_WINDOW_P (it->f))
19337 /* On GUI frames, when variable-size fonts are displayed,
19338 the truncation glyphs may need more pixels than the row's
19339 glyphs they overwrite. We overwrite more glyphs to free
19340 enough screen real estate, and enlarge the stretch glyph
19341 on the right (see display_line), if there is one, to
19342 preserve the screen position of the truncation glyphs on
19343 the right. */
19344 int w = 0;
19345 struct glyph *g = to;
19346 short used;
19348 /* The first glyph could be partially visible, in which case
19349 it->glyph_row->x will be negative. But we want the left
19350 truncation glyphs to be aligned at the left margin of the
19351 window, so we override the x coordinate at which the row
19352 will begin. */
19353 it->glyph_row->x = 0;
19354 while (g < toend && w < it->truncation_pixel_width)
19356 w += g->pixel_width;
19357 ++g;
19359 if (g - to - tused > 0)
19361 memmove (to + tused, g, (toend - g) * sizeof(*g));
19362 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19364 used = it->glyph_row->used[TEXT_AREA];
19365 if (it->glyph_row->truncated_on_right_p
19366 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19367 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19368 == STRETCH_GLYPH)
19370 int extra = w - it->truncation_pixel_width;
19372 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19376 while (from < end)
19377 *to++ = *from++;
19379 /* There may be padding glyphs left over. Overwrite them too. */
19380 if (!FRAME_WINDOW_P (it->f))
19382 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19384 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19385 while (from < end)
19386 *to++ = *from++;
19390 if (to > toend)
19391 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19393 else
19395 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19397 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19398 that back to front. */
19399 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19400 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19401 toend = it->glyph_row->glyphs[TEXT_AREA];
19402 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19403 if (FRAME_WINDOW_P (it->f))
19405 int w = 0;
19406 struct glyph *g = to;
19408 while (g >= toend && w < it->truncation_pixel_width)
19410 w += g->pixel_width;
19411 --g;
19413 if (to - g - tused > 0)
19414 to = g + tused;
19415 if (it->glyph_row->truncated_on_right_p
19416 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19417 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19419 int extra = w - it->truncation_pixel_width;
19421 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19425 while (from >= end && to >= toend)
19426 *to-- = *from--;
19427 if (!FRAME_WINDOW_P (it->f))
19429 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19431 from =
19432 truncate_it.glyph_row->glyphs[TEXT_AREA]
19433 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19434 while (from >= end && to >= toend)
19435 *to-- = *from--;
19438 if (from >= end)
19440 /* Need to free some room before prepending additional
19441 glyphs. */
19442 int move_by = from - end + 1;
19443 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19444 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19446 for ( ; g >= g0; g--)
19447 g[move_by] = *g;
19448 while (from >= end)
19449 *to-- = *from--;
19450 it->glyph_row->used[TEXT_AREA] += move_by;
19455 /* Compute the hash code for ROW. */
19456 unsigned
19457 row_hash (struct glyph_row *row)
19459 int area, k;
19460 unsigned hashval = 0;
19462 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19463 for (k = 0; k < row->used[area]; ++k)
19464 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19465 + row->glyphs[area][k].u.val
19466 + row->glyphs[area][k].face_id
19467 + row->glyphs[area][k].padding_p
19468 + (row->glyphs[area][k].type << 2));
19470 return hashval;
19473 /* Compute the pixel height and width of IT->glyph_row.
19475 Most of the time, ascent and height of a display line will be equal
19476 to the max_ascent and max_height values of the display iterator
19477 structure. This is not the case if
19479 1. We hit ZV without displaying anything. In this case, max_ascent
19480 and max_height will be zero.
19482 2. We have some glyphs that don't contribute to the line height.
19483 (The glyph row flag contributes_to_line_height_p is for future
19484 pixmap extensions).
19486 The first case is easily covered by using default values because in
19487 these cases, the line height does not really matter, except that it
19488 must not be zero. */
19490 static void
19491 compute_line_metrics (struct it *it)
19493 struct glyph_row *row = it->glyph_row;
19495 if (FRAME_WINDOW_P (it->f))
19497 int i, min_y, max_y;
19499 /* The line may consist of one space only, that was added to
19500 place the cursor on it. If so, the row's height hasn't been
19501 computed yet. */
19502 if (row->height == 0)
19504 if (it->max_ascent + it->max_descent == 0)
19505 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19506 row->ascent = it->max_ascent;
19507 row->height = it->max_ascent + it->max_descent;
19508 row->phys_ascent = it->max_phys_ascent;
19509 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19510 row->extra_line_spacing = it->max_extra_line_spacing;
19513 /* Compute the width of this line. */
19514 row->pixel_width = row->x;
19515 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19516 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19518 eassert (row->pixel_width >= 0);
19519 eassert (row->ascent >= 0 && row->height > 0);
19521 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19522 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19524 /* If first line's physical ascent is larger than its logical
19525 ascent, use the physical ascent, and make the row taller.
19526 This makes accented characters fully visible. */
19527 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19528 && row->phys_ascent > row->ascent)
19530 row->height += row->phys_ascent - row->ascent;
19531 row->ascent = row->phys_ascent;
19534 /* Compute how much of the line is visible. */
19535 row->visible_height = row->height;
19537 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19538 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19540 if (row->y < min_y)
19541 row->visible_height -= min_y - row->y;
19542 if (row->y + row->height > max_y)
19543 row->visible_height -= row->y + row->height - max_y;
19545 else
19547 row->pixel_width = row->used[TEXT_AREA];
19548 if (row->continued_p)
19549 row->pixel_width -= it->continuation_pixel_width;
19550 else if (row->truncated_on_right_p)
19551 row->pixel_width -= it->truncation_pixel_width;
19552 row->ascent = row->phys_ascent = 0;
19553 row->height = row->phys_height = row->visible_height = 1;
19554 row->extra_line_spacing = 0;
19557 /* Compute a hash code for this row. */
19558 row->hash = row_hash (row);
19560 it->max_ascent = it->max_descent = 0;
19561 it->max_phys_ascent = it->max_phys_descent = 0;
19565 /* Append one space to the glyph row of iterator IT if doing a
19566 window-based redisplay. The space has the same face as
19567 IT->face_id. Value is true if a space was added.
19569 This function is called to make sure that there is always one glyph
19570 at the end of a glyph row that the cursor can be set on under
19571 window-systems. (If there weren't such a glyph we would not know
19572 how wide and tall a box cursor should be displayed).
19574 At the same time this space let's a nicely handle clearing to the
19575 end of the line if the row ends in italic text. */
19577 static bool
19578 append_space_for_newline (struct it *it, bool default_face_p)
19580 if (FRAME_WINDOW_P (it->f))
19582 int n = it->glyph_row->used[TEXT_AREA];
19584 if (it->glyph_row->glyphs[TEXT_AREA] + n
19585 < it->glyph_row->glyphs[1 + TEXT_AREA])
19587 /* Save some values that must not be changed.
19588 Must save IT->c and IT->len because otherwise
19589 ITERATOR_AT_END_P wouldn't work anymore after
19590 append_space_for_newline has been called. */
19591 enum display_element_type saved_what = it->what;
19592 int saved_c = it->c, saved_len = it->len;
19593 int saved_char_to_display = it->char_to_display;
19594 int saved_x = it->current_x;
19595 int saved_face_id = it->face_id;
19596 bool saved_box_end = it->end_of_box_run_p;
19597 struct text_pos saved_pos;
19598 Lisp_Object saved_object;
19599 struct face *face;
19601 saved_object = it->object;
19602 saved_pos = it->position;
19604 it->what = IT_CHARACTER;
19605 memset (&it->position, 0, sizeof it->position);
19606 it->object = Qnil;
19607 it->c = it->char_to_display = ' ';
19608 it->len = 1;
19610 /* If the default face was remapped, be sure to use the
19611 remapped face for the appended newline. */
19612 if (default_face_p)
19613 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19614 else if (it->face_before_selective_p)
19615 it->face_id = it->saved_face_id;
19616 face = FACE_FROM_ID (it->f, it->face_id);
19617 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19618 /* In R2L rows, we will prepend a stretch glyph that will
19619 have the end_of_box_run_p flag set for it, so there's no
19620 need for the appended newline glyph to have that flag
19621 set. */
19622 if (it->glyph_row->reversed_p
19623 /* But if the appended newline glyph goes all the way to
19624 the end of the row, there will be no stretch glyph,
19625 so leave the box flag set. */
19626 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19627 it->end_of_box_run_p = false;
19629 PRODUCE_GLYPHS (it);
19631 #ifdef HAVE_WINDOW_SYSTEM
19632 /* Make sure this space glyph has the right ascent and
19633 descent values, or else cursor at end of line will look
19634 funny, and height of empty lines will be incorrect. */
19635 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19636 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19637 if (n == 0)
19639 Lisp_Object height, total_height;
19640 int extra_line_spacing = it->extra_line_spacing;
19641 int boff = font->baseline_offset;
19643 if (font->vertical_centering)
19644 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19646 it->object = saved_object; /* get_it_property needs this */
19647 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19648 /* Must do a subset of line height processing from
19649 x_produce_glyph for newline characters. */
19650 height = get_it_property (it, Qline_height);
19651 if (CONSP (height)
19652 && CONSP (XCDR (height))
19653 && NILP (XCDR (XCDR (height))))
19655 total_height = XCAR (XCDR (height));
19656 height = XCAR (height);
19658 else
19659 total_height = Qnil;
19660 height = calc_line_height_property (it, height, font, boff, true);
19662 if (it->override_ascent >= 0)
19664 it->ascent = it->override_ascent;
19665 it->descent = it->override_descent;
19666 boff = it->override_boff;
19668 if (EQ (height, Qt))
19669 extra_line_spacing = 0;
19670 else
19672 Lisp_Object spacing;
19674 it->phys_ascent = it->ascent;
19675 it->phys_descent = it->descent;
19676 if (!NILP (height)
19677 && XINT (height) > it->ascent + it->descent)
19678 it->ascent = XINT (height) - it->descent;
19680 if (!NILP (total_height))
19681 spacing = calc_line_height_property (it, total_height, font,
19682 boff, false);
19683 else
19685 spacing = get_it_property (it, Qline_spacing);
19686 spacing = calc_line_height_property (it, spacing, font,
19687 boff, false);
19689 if (INTEGERP (spacing))
19691 extra_line_spacing = XINT (spacing);
19692 if (!NILP (total_height))
19693 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19696 if (extra_line_spacing > 0)
19698 it->descent += extra_line_spacing;
19699 if (extra_line_spacing > it->max_extra_line_spacing)
19700 it->max_extra_line_spacing = extra_line_spacing;
19702 it->max_ascent = it->ascent;
19703 it->max_descent = it->descent;
19704 /* Make sure compute_line_metrics recomputes the row height. */
19705 it->glyph_row->height = 0;
19708 g->ascent = it->max_ascent;
19709 g->descent = it->max_descent;
19710 #endif
19712 it->override_ascent = -1;
19713 it->constrain_row_ascent_descent_p = false;
19714 it->current_x = saved_x;
19715 it->object = saved_object;
19716 it->position = saved_pos;
19717 it->what = saved_what;
19718 it->face_id = saved_face_id;
19719 it->len = saved_len;
19720 it->c = saved_c;
19721 it->char_to_display = saved_char_to_display;
19722 it->end_of_box_run_p = saved_box_end;
19723 return true;
19727 return false;
19731 /* Extend the face of the last glyph in the text area of IT->glyph_row
19732 to the end of the display line. Called from display_line. If the
19733 glyph row is empty, add a space glyph to it so that we know the
19734 face to draw. Set the glyph row flag fill_line_p. If the glyph
19735 row is R2L, prepend a stretch glyph to cover the empty space to the
19736 left of the leftmost glyph. */
19738 static void
19739 extend_face_to_end_of_line (struct it *it)
19741 struct face *face, *default_face;
19742 struct frame *f = it->f;
19744 /* If line is already filled, do nothing. Non window-system frames
19745 get a grace of one more ``pixel'' because their characters are
19746 1-``pixel'' wide, so they hit the equality too early. This grace
19747 is needed only for R2L rows that are not continued, to produce
19748 one extra blank where we could display the cursor. */
19749 if ((it->current_x >= it->last_visible_x
19750 + (!FRAME_WINDOW_P (f)
19751 && it->glyph_row->reversed_p
19752 && !it->glyph_row->continued_p))
19753 /* If the window has display margins, we will need to extend
19754 their face even if the text area is filled. */
19755 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19756 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19757 return;
19759 /* The default face, possibly remapped. */
19760 default_face = FACE_FROM_ID_OR_NULL (f,
19761 lookup_basic_face (f, DEFAULT_FACE_ID));
19763 /* Face extension extends the background and box of IT->face_id
19764 to the end of the line. If the background equals the background
19765 of the frame, we don't have to do anything. */
19766 face = FACE_FROM_ID (f, (it->face_before_selective_p
19767 ? it->saved_face_id
19768 : it->face_id));
19770 if (FRAME_WINDOW_P (f)
19771 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19772 && face->box == FACE_NO_BOX
19773 && face->background == FRAME_BACKGROUND_PIXEL (f)
19774 #ifdef HAVE_WINDOW_SYSTEM
19775 && !face->stipple
19776 #endif
19777 && !it->glyph_row->reversed_p)
19778 return;
19780 /* Set the glyph row flag indicating that the face of the last glyph
19781 in the text area has to be drawn to the end of the text area. */
19782 it->glyph_row->fill_line_p = true;
19784 /* If current character of IT is not ASCII, make sure we have the
19785 ASCII face. This will be automatically undone the next time
19786 get_next_display_element returns a multibyte character. Note
19787 that the character will always be single byte in unibyte
19788 text. */
19789 if (!ASCII_CHAR_P (it->c))
19791 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19794 if (FRAME_WINDOW_P (f))
19796 /* If the row is empty, add a space with the current face of IT,
19797 so that we know which face to draw. */
19798 if (it->glyph_row->used[TEXT_AREA] == 0)
19800 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19801 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19802 it->glyph_row->used[TEXT_AREA] = 1;
19804 /* Mode line and the header line don't have margins, and
19805 likewise the frame's tool-bar window, if there is any. */
19806 if (!(it->glyph_row->mode_line_p
19807 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19808 || (WINDOWP (f->tool_bar_window)
19809 && it->w == XWINDOW (f->tool_bar_window))
19810 #endif
19813 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19814 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19816 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19817 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19818 default_face->id;
19819 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19821 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19822 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19824 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19825 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19826 default_face->id;
19827 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19830 #ifdef HAVE_WINDOW_SYSTEM
19831 if (it->glyph_row->reversed_p)
19833 /* Prepend a stretch glyph to the row, such that the
19834 rightmost glyph will be drawn flushed all the way to the
19835 right margin of the window. The stretch glyph that will
19836 occupy the empty space, if any, to the left of the
19837 glyphs. */
19838 struct font *font = face->font ? face->font : FRAME_FONT (f);
19839 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19840 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19841 struct glyph *g;
19842 int row_width, stretch_ascent, stretch_width;
19843 struct text_pos saved_pos;
19844 int saved_face_id;
19845 bool saved_avoid_cursor, saved_box_start;
19847 for (row_width = 0, g = row_start; g < row_end; g++)
19848 row_width += g->pixel_width;
19850 /* FIXME: There are various minor display glitches in R2L
19851 rows when only one of the fringes is missing. The
19852 strange condition below produces the least bad effect. */
19853 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19854 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19855 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19856 stretch_width = window_box_width (it->w, TEXT_AREA);
19857 else
19858 stretch_width = it->last_visible_x - it->first_visible_x;
19859 stretch_width -= row_width;
19861 if (stretch_width > 0)
19863 stretch_ascent =
19864 (((it->ascent + it->descent)
19865 * FONT_BASE (font)) / FONT_HEIGHT (font));
19866 saved_pos = it->position;
19867 memset (&it->position, 0, sizeof it->position);
19868 saved_avoid_cursor = it->avoid_cursor_p;
19869 it->avoid_cursor_p = true;
19870 saved_face_id = it->face_id;
19871 saved_box_start = it->start_of_box_run_p;
19872 /* The last row's stretch glyph should get the default
19873 face, to avoid painting the rest of the window with
19874 the region face, if the region ends at ZV. */
19875 if (it->glyph_row->ends_at_zv_p)
19876 it->face_id = default_face->id;
19877 else
19878 it->face_id = face->id;
19879 it->start_of_box_run_p = false;
19880 append_stretch_glyph (it, Qnil, stretch_width,
19881 it->ascent + it->descent, stretch_ascent);
19882 it->position = saved_pos;
19883 it->avoid_cursor_p = saved_avoid_cursor;
19884 it->face_id = saved_face_id;
19885 it->start_of_box_run_p = saved_box_start;
19887 /* If stretch_width comes out negative, it means that the
19888 last glyph is only partially visible. In R2L rows, we
19889 want the leftmost glyph to be partially visible, so we
19890 need to give the row the corresponding left offset. */
19891 if (stretch_width < 0)
19892 it->glyph_row->x = stretch_width;
19894 #endif /* HAVE_WINDOW_SYSTEM */
19896 else
19898 /* Save some values that must not be changed. */
19899 int saved_x = it->current_x;
19900 struct text_pos saved_pos;
19901 Lisp_Object saved_object;
19902 enum display_element_type saved_what = it->what;
19903 int saved_face_id = it->face_id;
19905 saved_object = it->object;
19906 saved_pos = it->position;
19908 it->what = IT_CHARACTER;
19909 memset (&it->position, 0, sizeof it->position);
19910 it->object = Qnil;
19911 it->c = it->char_to_display = ' ';
19912 it->len = 1;
19914 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19915 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19916 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19917 && !it->glyph_row->mode_line_p
19918 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19920 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19921 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19923 for (it->current_x = 0; g < e; g++)
19924 it->current_x += g->pixel_width;
19926 it->area = LEFT_MARGIN_AREA;
19927 it->face_id = default_face->id;
19928 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19929 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19931 PRODUCE_GLYPHS (it);
19932 /* term.c:produce_glyphs advances it->current_x only for
19933 TEXT_AREA. */
19934 it->current_x += it->pixel_width;
19937 it->current_x = saved_x;
19938 it->area = TEXT_AREA;
19941 /* The last row's blank glyphs should get the default face, to
19942 avoid painting the rest of the window with the region face,
19943 if the region ends at ZV. */
19944 if (it->glyph_row->ends_at_zv_p)
19945 it->face_id = default_face->id;
19946 else
19947 it->face_id = face->id;
19948 PRODUCE_GLYPHS (it);
19950 while (it->current_x <= it->last_visible_x)
19951 PRODUCE_GLYPHS (it);
19953 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19954 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19955 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19956 && !it->glyph_row->mode_line_p
19957 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19959 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19960 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19962 for ( ; g < e; g++)
19963 it->current_x += g->pixel_width;
19965 it->area = RIGHT_MARGIN_AREA;
19966 it->face_id = default_face->id;
19967 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19968 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19970 PRODUCE_GLYPHS (it);
19971 it->current_x += it->pixel_width;
19974 it->area = TEXT_AREA;
19977 /* Don't count these blanks really. It would let us insert a left
19978 truncation glyph below and make us set the cursor on them, maybe. */
19979 it->current_x = saved_x;
19980 it->object = saved_object;
19981 it->position = saved_pos;
19982 it->what = saved_what;
19983 it->face_id = saved_face_id;
19988 /* Value is true if text starting at CHARPOS in current_buffer is
19989 trailing whitespace. */
19991 static bool
19992 trailing_whitespace_p (ptrdiff_t charpos)
19994 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19995 int c = 0;
19997 while (bytepos < ZV_BYTE
19998 && (c = FETCH_CHAR (bytepos),
19999 c == ' ' || c == '\t'))
20000 ++bytepos;
20002 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20004 if (bytepos != PT_BYTE)
20005 return true;
20007 return false;
20011 /* Highlight trailing whitespace, if any, in ROW. */
20013 static void
20014 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20016 int used = row->used[TEXT_AREA];
20018 if (used)
20020 struct glyph *start = row->glyphs[TEXT_AREA];
20021 struct glyph *glyph = start + used - 1;
20023 if (row->reversed_p)
20025 /* Right-to-left rows need to be processed in the opposite
20026 direction, so swap the edge pointers. */
20027 glyph = start;
20028 start = row->glyphs[TEXT_AREA] + used - 1;
20031 /* Skip over glyphs inserted to display the cursor at the
20032 end of a line, for extending the face of the last glyph
20033 to the end of the line on terminals, and for truncation
20034 and continuation glyphs. */
20035 if (!row->reversed_p)
20037 while (glyph >= start
20038 && glyph->type == CHAR_GLYPH
20039 && NILP (glyph->object))
20040 --glyph;
20042 else
20044 while (glyph <= start
20045 && glyph->type == CHAR_GLYPH
20046 && NILP (glyph->object))
20047 ++glyph;
20050 /* If last glyph is a space or stretch, and it's trailing
20051 whitespace, set the face of all trailing whitespace glyphs in
20052 IT->glyph_row to `trailing-whitespace'. */
20053 if ((row->reversed_p ? glyph <= start : glyph >= start)
20054 && BUFFERP (glyph->object)
20055 && (glyph->type == STRETCH_GLYPH
20056 || (glyph->type == CHAR_GLYPH
20057 && glyph->u.ch == ' '))
20058 && trailing_whitespace_p (glyph->charpos))
20060 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20061 if (face_id < 0)
20062 return;
20064 if (!row->reversed_p)
20066 while (glyph >= start
20067 && BUFFERP (glyph->object)
20068 && (glyph->type == STRETCH_GLYPH
20069 || (glyph->type == CHAR_GLYPH
20070 && glyph->u.ch == ' ')))
20071 (glyph--)->face_id = face_id;
20073 else
20075 while (glyph <= start
20076 && BUFFERP (glyph->object)
20077 && (glyph->type == STRETCH_GLYPH
20078 || (glyph->type == CHAR_GLYPH
20079 && glyph->u.ch == ' ')))
20080 (glyph++)->face_id = face_id;
20087 /* Value is true if glyph row ROW should be
20088 considered to hold the buffer position CHARPOS. */
20090 static bool
20091 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20093 bool result = true;
20095 if (charpos == CHARPOS (row->end.pos)
20096 || charpos == MATRIX_ROW_END_CHARPOS (row))
20098 /* Suppose the row ends on a string.
20099 Unless the row is continued, that means it ends on a newline
20100 in the string. If it's anything other than a display string
20101 (e.g., a before-string from an overlay), we don't want the
20102 cursor there. (This heuristic seems to give the optimal
20103 behavior for the various types of multi-line strings.)
20104 One exception: if the string has `cursor' property on one of
20105 its characters, we _do_ want the cursor there. */
20106 if (CHARPOS (row->end.string_pos) >= 0)
20108 if (row->continued_p)
20109 result = true;
20110 else
20112 /* Check for `display' property. */
20113 struct glyph *beg = row->glyphs[TEXT_AREA];
20114 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20115 struct glyph *glyph;
20117 result = false;
20118 for (glyph = end; glyph >= beg; --glyph)
20119 if (STRINGP (glyph->object))
20121 Lisp_Object prop
20122 = Fget_char_property (make_number (charpos),
20123 Qdisplay, Qnil);
20124 result =
20125 (!NILP (prop)
20126 && display_prop_string_p (prop, glyph->object));
20127 /* If there's a `cursor' property on one of the
20128 string's characters, this row is a cursor row,
20129 even though this is not a display string. */
20130 if (!result)
20132 Lisp_Object s = glyph->object;
20134 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20136 ptrdiff_t gpos = glyph->charpos;
20138 if (!NILP (Fget_char_property (make_number (gpos),
20139 Qcursor, s)))
20141 result = true;
20142 break;
20146 break;
20150 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20152 /* If the row ends in middle of a real character,
20153 and the line is continued, we want the cursor here.
20154 That's because CHARPOS (ROW->end.pos) would equal
20155 PT if PT is before the character. */
20156 if (!row->ends_in_ellipsis_p)
20157 result = row->continued_p;
20158 else
20159 /* If the row ends in an ellipsis, then
20160 CHARPOS (ROW->end.pos) will equal point after the
20161 invisible text. We want that position to be displayed
20162 after the ellipsis. */
20163 result = false;
20165 /* If the row ends at ZV, display the cursor at the end of that
20166 row instead of at the start of the row below. */
20167 else
20168 result = row->ends_at_zv_p;
20171 return result;
20174 /* Value is true if glyph row ROW should be
20175 used to hold the cursor. */
20177 static bool
20178 cursor_row_p (struct glyph_row *row)
20180 return row_for_charpos_p (row, PT);
20185 /* Push the property PROP so that it will be rendered at the current
20186 position in IT. Return true if PROP was successfully pushed, false
20187 otherwise. Called from handle_line_prefix to handle the
20188 `line-prefix' and `wrap-prefix' properties. */
20190 static bool
20191 push_prefix_prop (struct it *it, Lisp_Object prop)
20193 struct text_pos pos =
20194 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20196 eassert (it->method == GET_FROM_BUFFER
20197 || it->method == GET_FROM_DISPLAY_VECTOR
20198 || it->method == GET_FROM_STRING
20199 || it->method == GET_FROM_IMAGE);
20201 /* We need to save the current buffer/string position, so it will be
20202 restored by pop_it, because iterate_out_of_display_property
20203 depends on that being set correctly, but some situations leave
20204 it->position not yet set when this function is called. */
20205 push_it (it, &pos);
20207 if (STRINGP (prop))
20209 if (SCHARS (prop) == 0)
20211 pop_it (it);
20212 return false;
20215 it->string = prop;
20216 it->string_from_prefix_prop_p = true;
20217 it->multibyte_p = STRING_MULTIBYTE (it->string);
20218 it->current.overlay_string_index = -1;
20219 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20220 it->end_charpos = it->string_nchars = SCHARS (it->string);
20221 it->method = GET_FROM_STRING;
20222 it->stop_charpos = 0;
20223 it->prev_stop = 0;
20224 it->base_level_stop = 0;
20226 /* Force paragraph direction to be that of the parent
20227 buffer/string. */
20228 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20229 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20230 else
20231 it->paragraph_embedding = L2R;
20233 /* Set up the bidi iterator for this display string. */
20234 if (it->bidi_p)
20236 it->bidi_it.string.lstring = it->string;
20237 it->bidi_it.string.s = NULL;
20238 it->bidi_it.string.schars = it->end_charpos;
20239 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20240 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20241 it->bidi_it.string.unibyte = !it->multibyte_p;
20242 it->bidi_it.w = it->w;
20243 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20246 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20248 it->method = GET_FROM_STRETCH;
20249 it->object = prop;
20251 #ifdef HAVE_WINDOW_SYSTEM
20252 else if (IMAGEP (prop))
20254 it->what = IT_IMAGE;
20255 it->image_id = lookup_image (it->f, prop);
20256 it->method = GET_FROM_IMAGE;
20258 #endif /* HAVE_WINDOW_SYSTEM */
20259 else
20261 pop_it (it); /* bogus display property, give up */
20262 return false;
20265 return true;
20268 /* Return the character-property PROP at the current position in IT. */
20270 static Lisp_Object
20271 get_it_property (struct it *it, Lisp_Object prop)
20273 Lisp_Object position, object = it->object;
20275 if (STRINGP (object))
20276 position = make_number (IT_STRING_CHARPOS (*it));
20277 else if (BUFFERP (object))
20279 position = make_number (IT_CHARPOS (*it));
20280 object = it->window;
20282 else
20283 return Qnil;
20285 return Fget_char_property (position, prop, object);
20288 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20290 static void
20291 handle_line_prefix (struct it *it)
20293 Lisp_Object prefix;
20295 if (it->continuation_lines_width > 0)
20297 prefix = get_it_property (it, Qwrap_prefix);
20298 if (NILP (prefix))
20299 prefix = Vwrap_prefix;
20301 else
20303 prefix = get_it_property (it, Qline_prefix);
20304 if (NILP (prefix))
20305 prefix = Vline_prefix;
20307 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20309 /* If the prefix is wider than the window, and we try to wrap
20310 it, it would acquire its own wrap prefix, and so on till the
20311 iterator stack overflows. So, don't wrap the prefix. */
20312 it->line_wrap = TRUNCATE;
20313 it->avoid_cursor_p = true;
20319 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20320 only for R2L lines from display_line and display_string, when they
20321 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20322 the line/string needs to be continued on the next glyph row. */
20323 static void
20324 unproduce_glyphs (struct it *it, int n)
20326 struct glyph *glyph, *end;
20328 eassert (it->glyph_row);
20329 eassert (it->glyph_row->reversed_p);
20330 eassert (it->area == TEXT_AREA);
20331 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20333 if (n > it->glyph_row->used[TEXT_AREA])
20334 n = it->glyph_row->used[TEXT_AREA];
20335 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20336 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20337 for ( ; glyph < end; glyph++)
20338 glyph[-n] = *glyph;
20341 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20342 and ROW->maxpos. */
20343 static void
20344 find_row_edges (struct it *it, struct glyph_row *row,
20345 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20346 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20348 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20349 lines' rows is implemented for bidi-reordered rows. */
20351 /* ROW->minpos is the value of min_pos, the minimal buffer position
20352 we have in ROW, or ROW->start.pos if that is smaller. */
20353 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20354 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20355 else
20356 /* We didn't find buffer positions smaller than ROW->start, or
20357 didn't find _any_ valid buffer positions in any of the glyphs,
20358 so we must trust the iterator's computed positions. */
20359 row->minpos = row->start.pos;
20360 if (max_pos <= 0)
20362 max_pos = CHARPOS (it->current.pos);
20363 max_bpos = BYTEPOS (it->current.pos);
20366 /* Here are the various use-cases for ending the row, and the
20367 corresponding values for ROW->maxpos:
20369 Line ends in a newline from buffer eol_pos + 1
20370 Line is continued from buffer max_pos + 1
20371 Line is truncated on right it->current.pos
20372 Line ends in a newline from string max_pos + 1(*)
20373 (*) + 1 only when line ends in a forward scan
20374 Line is continued from string max_pos
20375 Line is continued from display vector max_pos
20376 Line is entirely from a string min_pos == max_pos
20377 Line is entirely from a display vector min_pos == max_pos
20378 Line that ends at ZV ZV
20380 If you discover other use-cases, please add them here as
20381 appropriate. */
20382 if (row->ends_at_zv_p)
20383 row->maxpos = it->current.pos;
20384 else if (row->used[TEXT_AREA])
20386 bool seen_this_string = false;
20387 struct glyph_row *r1 = row - 1;
20389 /* Did we see the same display string on the previous row? */
20390 if (STRINGP (it->object)
20391 /* this is not the first row */
20392 && row > it->w->desired_matrix->rows
20393 /* previous row is not the header line */
20394 && !r1->mode_line_p
20395 /* previous row also ends in a newline from a string */
20396 && r1->ends_in_newline_from_string_p)
20398 struct glyph *start, *end;
20400 /* Search for the last glyph of the previous row that came
20401 from buffer or string. Depending on whether the row is
20402 L2R or R2L, we need to process it front to back or the
20403 other way round. */
20404 if (!r1->reversed_p)
20406 start = r1->glyphs[TEXT_AREA];
20407 end = start + r1->used[TEXT_AREA];
20408 /* Glyphs inserted by redisplay have nil as their object. */
20409 while (end > start
20410 && NILP ((end - 1)->object)
20411 && (end - 1)->charpos <= 0)
20412 --end;
20413 if (end > start)
20415 if (EQ ((end - 1)->object, it->object))
20416 seen_this_string = true;
20418 else
20419 /* If all the glyphs of the previous row were inserted
20420 by redisplay, it means the previous row was
20421 produced from a single newline, which is only
20422 possible if that newline came from the same string
20423 as the one which produced this ROW. */
20424 seen_this_string = true;
20426 else
20428 end = r1->glyphs[TEXT_AREA] - 1;
20429 start = end + r1->used[TEXT_AREA];
20430 while (end < start
20431 && NILP ((end + 1)->object)
20432 && (end + 1)->charpos <= 0)
20433 ++end;
20434 if (end < start)
20436 if (EQ ((end + 1)->object, it->object))
20437 seen_this_string = true;
20439 else
20440 seen_this_string = true;
20443 /* Take note of each display string that covers a newline only
20444 once, the first time we see it. This is for when a display
20445 string includes more than one newline in it. */
20446 if (row->ends_in_newline_from_string_p && !seen_this_string)
20448 /* If we were scanning the buffer forward when we displayed
20449 the string, we want to account for at least one buffer
20450 position that belongs to this row (position covered by
20451 the display string), so that cursor positioning will
20452 consider this row as a candidate when point is at the end
20453 of the visual line represented by this row. This is not
20454 required when scanning back, because max_pos will already
20455 have a much larger value. */
20456 if (CHARPOS (row->end.pos) > max_pos)
20457 INC_BOTH (max_pos, max_bpos);
20458 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20460 else if (CHARPOS (it->eol_pos) > 0)
20461 SET_TEXT_POS (row->maxpos,
20462 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20463 else if (row->continued_p)
20465 /* If max_pos is different from IT's current position, it
20466 means IT->method does not belong to the display element
20467 at max_pos. However, it also means that the display
20468 element at max_pos was displayed in its entirety on this
20469 line, which is equivalent to saying that the next line
20470 starts at the next buffer position. */
20471 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20472 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20473 else
20475 INC_BOTH (max_pos, max_bpos);
20476 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20479 else if (row->truncated_on_right_p)
20480 /* display_line already called reseat_at_next_visible_line_start,
20481 which puts the iterator at the beginning of the next line, in
20482 the logical order. */
20483 row->maxpos = it->current.pos;
20484 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20485 /* A line that is entirely from a string/image/stretch... */
20486 row->maxpos = row->minpos;
20487 else
20488 emacs_abort ();
20490 else
20491 row->maxpos = it->current.pos;
20494 /* Construct the glyph row IT->glyph_row in the desired matrix of
20495 IT->w from text at the current position of IT. See dispextern.h
20496 for an overview of struct it. Value is true if
20497 IT->glyph_row displays text, as opposed to a line displaying ZV
20498 only. */
20500 static bool
20501 display_line (struct it *it)
20503 struct glyph_row *row = it->glyph_row;
20504 Lisp_Object overlay_arrow_string;
20505 struct it wrap_it;
20506 void *wrap_data = NULL;
20507 bool may_wrap = false;
20508 int wrap_x UNINIT;
20509 int wrap_row_used = -1;
20510 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20511 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20512 int wrap_row_extra_line_spacing UNINIT;
20513 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20514 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20515 int cvpos;
20516 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20517 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20518 bool pending_handle_line_prefix = false;
20520 /* We always start displaying at hpos zero even if hscrolled. */
20521 eassert (it->hpos == 0 && it->current_x == 0);
20523 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20524 >= it->w->desired_matrix->nrows)
20526 it->w->nrows_scale_factor++;
20527 it->f->fonts_changed = true;
20528 return false;
20531 /* Clear the result glyph row and enable it. */
20532 prepare_desired_row (it->w, row, false);
20534 row->y = it->current_y;
20535 row->start = it->start;
20536 row->continuation_lines_width = it->continuation_lines_width;
20537 row->displays_text_p = true;
20538 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20539 it->starts_in_middle_of_char_p = false;
20541 /* Arrange the overlays nicely for our purposes. Usually, we call
20542 display_line on only one line at a time, in which case this
20543 can't really hurt too much, or we call it on lines which appear
20544 one after another in the buffer, in which case all calls to
20545 recenter_overlay_lists but the first will be pretty cheap. */
20546 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20548 /* Move over display elements that are not visible because we are
20549 hscrolled. This may stop at an x-position < IT->first_visible_x
20550 if the first glyph is partially visible or if we hit a line end. */
20551 if (it->current_x < it->first_visible_x)
20553 enum move_it_result move_result;
20555 this_line_min_pos = row->start.pos;
20556 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20557 MOVE_TO_POS | MOVE_TO_X);
20558 /* If we are under a large hscroll, move_it_in_display_line_to
20559 could hit the end of the line without reaching
20560 it->first_visible_x. Pretend that we did reach it. This is
20561 especially important on a TTY, where we will call
20562 extend_face_to_end_of_line, which needs to know how many
20563 blank glyphs to produce. */
20564 if (it->current_x < it->first_visible_x
20565 && (move_result == MOVE_NEWLINE_OR_CR
20566 || move_result == MOVE_POS_MATCH_OR_ZV))
20567 it->current_x = it->first_visible_x;
20569 /* Record the smallest positions seen while we moved over
20570 display elements that are not visible. This is needed by
20571 redisplay_internal for optimizing the case where the cursor
20572 stays inside the same line. The rest of this function only
20573 considers positions that are actually displayed, so
20574 RECORD_MAX_MIN_POS will not otherwise record positions that
20575 are hscrolled to the left of the left edge of the window. */
20576 min_pos = CHARPOS (this_line_min_pos);
20577 min_bpos = BYTEPOS (this_line_min_pos);
20579 else if (it->area == TEXT_AREA)
20581 /* We only do this when not calling move_it_in_display_line_to
20582 above, because that function calls itself handle_line_prefix. */
20583 handle_line_prefix (it);
20585 else
20587 /* Line-prefix and wrap-prefix are always displayed in the text
20588 area. But if this is the first call to display_line after
20589 init_iterator, the iterator might have been set up to write
20590 into a marginal area, e.g. if the line begins with some
20591 display property that writes to the margins. So we need to
20592 wait with the call to handle_line_prefix until whatever
20593 writes to the margin has done its job. */
20594 pending_handle_line_prefix = true;
20597 /* Get the initial row height. This is either the height of the
20598 text hscrolled, if there is any, or zero. */
20599 row->ascent = it->max_ascent;
20600 row->height = it->max_ascent + it->max_descent;
20601 row->phys_ascent = it->max_phys_ascent;
20602 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20603 row->extra_line_spacing = it->max_extra_line_spacing;
20605 /* Utility macro to record max and min buffer positions seen until now. */
20606 #define RECORD_MAX_MIN_POS(IT) \
20607 do \
20609 bool composition_p \
20610 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20611 ptrdiff_t current_pos = \
20612 composition_p ? (IT)->cmp_it.charpos \
20613 : IT_CHARPOS (*(IT)); \
20614 ptrdiff_t current_bpos = \
20615 composition_p ? CHAR_TO_BYTE (current_pos) \
20616 : IT_BYTEPOS (*(IT)); \
20617 if (current_pos < min_pos) \
20619 min_pos = current_pos; \
20620 min_bpos = current_bpos; \
20622 if (IT_CHARPOS (*it) > max_pos) \
20624 max_pos = IT_CHARPOS (*it); \
20625 max_bpos = IT_BYTEPOS (*it); \
20628 while (false)
20630 /* Loop generating characters. The loop is left with IT on the next
20631 character to display. */
20632 while (true)
20634 int n_glyphs_before, hpos_before, x_before;
20635 int x, nglyphs;
20636 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20638 /* Retrieve the next thing to display. Value is false if end of
20639 buffer reached. */
20640 if (!get_next_display_element (it))
20642 /* Maybe add a space at the end of this line that is used to
20643 display the cursor there under X. Set the charpos of the
20644 first glyph of blank lines not corresponding to any text
20645 to -1. */
20646 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20647 row->exact_window_width_line_p = true;
20648 else if ((append_space_for_newline (it, true)
20649 && row->used[TEXT_AREA] == 1)
20650 || row->used[TEXT_AREA] == 0)
20652 row->glyphs[TEXT_AREA]->charpos = -1;
20653 row->displays_text_p = false;
20655 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20656 && (!MINI_WINDOW_P (it->w)
20657 || (minibuf_level && EQ (it->window, minibuf_window))))
20658 row->indicate_empty_line_p = true;
20661 it->continuation_lines_width = 0;
20662 row->ends_at_zv_p = true;
20663 /* A row that displays right-to-left text must always have
20664 its last face extended all the way to the end of line,
20665 even if this row ends in ZV, because we still write to
20666 the screen left to right. We also need to extend the
20667 last face if the default face is remapped to some
20668 different face, otherwise the functions that clear
20669 portions of the screen will clear with the default face's
20670 background color. */
20671 if (row->reversed_p
20672 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20673 extend_face_to_end_of_line (it);
20674 break;
20677 /* Now, get the metrics of what we want to display. This also
20678 generates glyphs in `row' (which is IT->glyph_row). */
20679 n_glyphs_before = row->used[TEXT_AREA];
20680 x = it->current_x;
20682 /* Remember the line height so far in case the next element doesn't
20683 fit on the line. */
20684 if (it->line_wrap != TRUNCATE)
20686 ascent = it->max_ascent;
20687 descent = it->max_descent;
20688 phys_ascent = it->max_phys_ascent;
20689 phys_descent = it->max_phys_descent;
20691 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20693 if (IT_DISPLAYING_WHITESPACE (it))
20694 may_wrap = true;
20695 else if (may_wrap)
20697 SAVE_IT (wrap_it, *it, wrap_data);
20698 wrap_x = x;
20699 wrap_row_used = row->used[TEXT_AREA];
20700 wrap_row_ascent = row->ascent;
20701 wrap_row_height = row->height;
20702 wrap_row_phys_ascent = row->phys_ascent;
20703 wrap_row_phys_height = row->phys_height;
20704 wrap_row_extra_line_spacing = row->extra_line_spacing;
20705 wrap_row_min_pos = min_pos;
20706 wrap_row_min_bpos = min_bpos;
20707 wrap_row_max_pos = max_pos;
20708 wrap_row_max_bpos = max_bpos;
20709 may_wrap = false;
20714 PRODUCE_GLYPHS (it);
20716 /* If this display element was in marginal areas, continue with
20717 the next one. */
20718 if (it->area != TEXT_AREA)
20720 row->ascent = max (row->ascent, it->max_ascent);
20721 row->height = max (row->height, it->max_ascent + it->max_descent);
20722 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20723 row->phys_height = max (row->phys_height,
20724 it->max_phys_ascent + it->max_phys_descent);
20725 row->extra_line_spacing = max (row->extra_line_spacing,
20726 it->max_extra_line_spacing);
20727 set_iterator_to_next (it, true);
20728 /* If we didn't handle the line/wrap prefix above, and the
20729 call to set_iterator_to_next just switched to TEXT_AREA,
20730 process the prefix now. */
20731 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20733 pending_handle_line_prefix = false;
20734 handle_line_prefix (it);
20736 continue;
20739 /* Does the display element fit on the line? If we truncate
20740 lines, we should draw past the right edge of the window. If
20741 we don't truncate, we want to stop so that we can display the
20742 continuation glyph before the right margin. If lines are
20743 continued, there are two possible strategies for characters
20744 resulting in more than 1 glyph (e.g. tabs): Display as many
20745 glyphs as possible in this line and leave the rest for the
20746 continuation line, or display the whole element in the next
20747 line. Original redisplay did the former, so we do it also. */
20748 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20749 hpos_before = it->hpos;
20750 x_before = x;
20752 if (/* Not a newline. */
20753 nglyphs > 0
20754 /* Glyphs produced fit entirely in the line. */
20755 && it->current_x < it->last_visible_x)
20757 it->hpos += nglyphs;
20758 row->ascent = max (row->ascent, it->max_ascent);
20759 row->height = max (row->height, it->max_ascent + it->max_descent);
20760 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20761 row->phys_height = max (row->phys_height,
20762 it->max_phys_ascent + it->max_phys_descent);
20763 row->extra_line_spacing = max (row->extra_line_spacing,
20764 it->max_extra_line_spacing);
20765 if (it->current_x - it->pixel_width < it->first_visible_x
20766 /* In R2L rows, we arrange in extend_face_to_end_of_line
20767 to add a right offset to the line, by a suitable
20768 change to the stretch glyph that is the leftmost
20769 glyph of the line. */
20770 && !row->reversed_p)
20771 row->x = x - it->first_visible_x;
20772 /* Record the maximum and minimum buffer positions seen so
20773 far in glyphs that will be displayed by this row. */
20774 if (it->bidi_p)
20775 RECORD_MAX_MIN_POS (it);
20777 else
20779 int i, new_x;
20780 struct glyph *glyph;
20782 for (i = 0; i < nglyphs; ++i, x = new_x)
20784 /* Identify the glyphs added by the last call to
20785 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20786 the previous glyphs. */
20787 if (!row->reversed_p)
20788 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20789 else
20790 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20791 new_x = x + glyph->pixel_width;
20793 if (/* Lines are continued. */
20794 it->line_wrap != TRUNCATE
20795 && (/* Glyph doesn't fit on the line. */
20796 new_x > it->last_visible_x
20797 /* Or it fits exactly on a window system frame. */
20798 || (new_x == it->last_visible_x
20799 && FRAME_WINDOW_P (it->f)
20800 && (row->reversed_p
20801 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20802 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20804 /* End of a continued line. */
20806 if (it->hpos == 0
20807 || (new_x == it->last_visible_x
20808 && FRAME_WINDOW_P (it->f)
20809 && (row->reversed_p
20810 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20811 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20813 /* Current glyph is the only one on the line or
20814 fits exactly on the line. We must continue
20815 the line because we can't draw the cursor
20816 after the glyph. */
20817 row->continued_p = true;
20818 it->current_x = new_x;
20819 it->continuation_lines_width += new_x;
20820 ++it->hpos;
20821 if (i == nglyphs - 1)
20823 /* If line-wrap is on, check if a previous
20824 wrap point was found. */
20825 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20826 && wrap_row_used > 0
20827 /* Even if there is a previous wrap
20828 point, continue the line here as
20829 usual, if (i) the previous character
20830 was a space or tab AND (ii) the
20831 current character is not. */
20832 && (!may_wrap
20833 || IT_DISPLAYING_WHITESPACE (it)))
20834 goto back_to_wrap;
20836 /* Record the maximum and minimum buffer
20837 positions seen so far in glyphs that will be
20838 displayed by this row. */
20839 if (it->bidi_p)
20840 RECORD_MAX_MIN_POS (it);
20841 set_iterator_to_next (it, true);
20842 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20844 if (!get_next_display_element (it))
20846 row->exact_window_width_line_p = true;
20847 it->continuation_lines_width = 0;
20848 row->continued_p = false;
20849 row->ends_at_zv_p = true;
20851 else if (ITERATOR_AT_END_OF_LINE_P (it))
20853 row->continued_p = false;
20854 row->exact_window_width_line_p = true;
20856 /* If line-wrap is on, check if a
20857 previous wrap point was found. */
20858 else if (wrap_row_used > 0
20859 /* Even if there is a previous wrap
20860 point, continue the line here as
20861 usual, if (i) the previous character
20862 was a space or tab AND (ii) the
20863 current character is not. */
20864 && (!may_wrap
20865 || IT_DISPLAYING_WHITESPACE (it)))
20866 goto back_to_wrap;
20870 else if (it->bidi_p)
20871 RECORD_MAX_MIN_POS (it);
20872 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20873 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20874 extend_face_to_end_of_line (it);
20876 else if (CHAR_GLYPH_PADDING_P (*glyph)
20877 && !FRAME_WINDOW_P (it->f))
20879 /* A padding glyph that doesn't fit on this line.
20880 This means the whole character doesn't fit
20881 on the line. */
20882 if (row->reversed_p)
20883 unproduce_glyphs (it, row->used[TEXT_AREA]
20884 - n_glyphs_before);
20885 row->used[TEXT_AREA] = n_glyphs_before;
20887 /* Fill the rest of the row with continuation
20888 glyphs like in 20.x. */
20889 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20890 < row->glyphs[1 + TEXT_AREA])
20891 produce_special_glyphs (it, IT_CONTINUATION);
20893 row->continued_p = true;
20894 it->current_x = x_before;
20895 it->continuation_lines_width += x_before;
20897 /* Restore the height to what it was before the
20898 element not fitting on the line. */
20899 it->max_ascent = ascent;
20900 it->max_descent = descent;
20901 it->max_phys_ascent = phys_ascent;
20902 it->max_phys_descent = phys_descent;
20903 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20904 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20905 extend_face_to_end_of_line (it);
20907 else if (wrap_row_used > 0)
20909 back_to_wrap:
20910 if (row->reversed_p)
20911 unproduce_glyphs (it,
20912 row->used[TEXT_AREA] - wrap_row_used);
20913 RESTORE_IT (it, &wrap_it, wrap_data);
20914 it->continuation_lines_width += wrap_x;
20915 row->used[TEXT_AREA] = wrap_row_used;
20916 row->ascent = wrap_row_ascent;
20917 row->height = wrap_row_height;
20918 row->phys_ascent = wrap_row_phys_ascent;
20919 row->phys_height = wrap_row_phys_height;
20920 row->extra_line_spacing = wrap_row_extra_line_spacing;
20921 min_pos = wrap_row_min_pos;
20922 min_bpos = wrap_row_min_bpos;
20923 max_pos = wrap_row_max_pos;
20924 max_bpos = wrap_row_max_bpos;
20925 row->continued_p = true;
20926 row->ends_at_zv_p = false;
20927 row->exact_window_width_line_p = false;
20928 it->continuation_lines_width += x;
20930 /* Make sure that a non-default face is extended
20931 up to the right margin of the window. */
20932 extend_face_to_end_of_line (it);
20934 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20936 /* A TAB that extends past the right edge of the
20937 window. This produces a single glyph on
20938 window system frames. We leave the glyph in
20939 this row and let it fill the row, but don't
20940 consume the TAB. */
20941 if ((row->reversed_p
20942 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20943 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20944 produce_special_glyphs (it, IT_CONTINUATION);
20945 it->continuation_lines_width += it->last_visible_x;
20946 row->ends_in_middle_of_char_p = true;
20947 row->continued_p = true;
20948 glyph->pixel_width = it->last_visible_x - x;
20949 it->starts_in_middle_of_char_p = true;
20950 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20951 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20952 extend_face_to_end_of_line (it);
20954 else
20956 /* Something other than a TAB that draws past
20957 the right edge of the window. Restore
20958 positions to values before the element. */
20959 if (row->reversed_p)
20960 unproduce_glyphs (it, row->used[TEXT_AREA]
20961 - (n_glyphs_before + i));
20962 row->used[TEXT_AREA] = n_glyphs_before + i;
20964 /* Display continuation glyphs. */
20965 it->current_x = x_before;
20966 it->continuation_lines_width += x;
20967 if (!FRAME_WINDOW_P (it->f)
20968 || (row->reversed_p
20969 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20970 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20971 produce_special_glyphs (it, IT_CONTINUATION);
20972 row->continued_p = true;
20974 extend_face_to_end_of_line (it);
20976 if (nglyphs > 1 && i > 0)
20978 row->ends_in_middle_of_char_p = true;
20979 it->starts_in_middle_of_char_p = true;
20982 /* Restore the height to what it was before the
20983 element not fitting on the line. */
20984 it->max_ascent = ascent;
20985 it->max_descent = descent;
20986 it->max_phys_ascent = phys_ascent;
20987 it->max_phys_descent = phys_descent;
20990 break;
20992 else if (new_x > it->first_visible_x)
20994 /* Increment number of glyphs actually displayed. */
20995 ++it->hpos;
20997 /* Record the maximum and minimum buffer positions
20998 seen so far in glyphs that will be displayed by
20999 this row. */
21000 if (it->bidi_p)
21001 RECORD_MAX_MIN_POS (it);
21003 if (x < it->first_visible_x && !row->reversed_p)
21004 /* Glyph is partially visible, i.e. row starts at
21005 negative X position. Don't do that in R2L
21006 rows, where we arrange to add a right offset to
21007 the line in extend_face_to_end_of_line, by a
21008 suitable change to the stretch glyph that is
21009 the leftmost glyph of the line. */
21010 row->x = x - it->first_visible_x;
21011 /* When the last glyph of an R2L row only fits
21012 partially on the line, we need to set row->x to a
21013 negative offset, so that the leftmost glyph is
21014 the one that is partially visible. But if we are
21015 going to produce the truncation glyph, this will
21016 be taken care of in produce_special_glyphs. */
21017 if (row->reversed_p
21018 && new_x > it->last_visible_x
21019 && !(it->line_wrap == TRUNCATE
21020 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21022 eassert (FRAME_WINDOW_P (it->f));
21023 row->x = it->last_visible_x - new_x;
21026 else
21028 /* Glyph is completely off the left margin of the
21029 window. This should not happen because of the
21030 move_it_in_display_line at the start of this
21031 function, unless the text display area of the
21032 window is empty. */
21033 eassert (it->first_visible_x <= it->last_visible_x);
21036 /* Even if this display element produced no glyphs at all,
21037 we want to record its position. */
21038 if (it->bidi_p && nglyphs == 0)
21039 RECORD_MAX_MIN_POS (it);
21041 row->ascent = max (row->ascent, it->max_ascent);
21042 row->height = max (row->height, it->max_ascent + it->max_descent);
21043 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21044 row->phys_height = max (row->phys_height,
21045 it->max_phys_ascent + it->max_phys_descent);
21046 row->extra_line_spacing = max (row->extra_line_spacing,
21047 it->max_extra_line_spacing);
21049 /* End of this display line if row is continued. */
21050 if (row->continued_p || row->ends_at_zv_p)
21051 break;
21054 at_end_of_line:
21055 /* Is this a line end? If yes, we're also done, after making
21056 sure that a non-default face is extended up to the right
21057 margin of the window. */
21058 if (ITERATOR_AT_END_OF_LINE_P (it))
21060 int used_before = row->used[TEXT_AREA];
21062 row->ends_in_newline_from_string_p = STRINGP (it->object);
21064 /* Add a space at the end of the line that is used to
21065 display the cursor there. */
21066 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21067 append_space_for_newline (it, false);
21069 /* Extend the face to the end of the line. */
21070 extend_face_to_end_of_line (it);
21072 /* Make sure we have the position. */
21073 if (used_before == 0)
21074 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21076 /* Record the position of the newline, for use in
21077 find_row_edges. */
21078 it->eol_pos = it->current.pos;
21080 /* Consume the line end. This skips over invisible lines. */
21081 set_iterator_to_next (it, true);
21082 it->continuation_lines_width = 0;
21083 break;
21086 /* Proceed with next display element. Note that this skips
21087 over lines invisible because of selective display. */
21088 set_iterator_to_next (it, true);
21090 /* If we truncate lines, we are done when the last displayed
21091 glyphs reach past the right margin of the window. */
21092 if (it->line_wrap == TRUNCATE
21093 && ((FRAME_WINDOW_P (it->f)
21094 /* Images are preprocessed in produce_image_glyph such
21095 that they are cropped at the right edge of the
21096 window, so an image glyph will always end exactly at
21097 last_visible_x, even if there's no right fringe. */
21098 && ((row->reversed_p
21099 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21100 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21101 || it->what == IT_IMAGE))
21102 ? (it->current_x >= it->last_visible_x)
21103 : (it->current_x > it->last_visible_x)))
21105 /* Maybe add truncation glyphs. */
21106 if (!FRAME_WINDOW_P (it->f)
21107 || (row->reversed_p
21108 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21109 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21111 int i, n;
21113 if (!row->reversed_p)
21115 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21116 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21117 break;
21119 else
21121 for (i = 0; i < row->used[TEXT_AREA]; i++)
21122 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21123 break;
21124 /* Remove any padding glyphs at the front of ROW, to
21125 make room for the truncation glyphs we will be
21126 adding below. The loop below always inserts at
21127 least one truncation glyph, so also remove the
21128 last glyph added to ROW. */
21129 unproduce_glyphs (it, i + 1);
21130 /* Adjust i for the loop below. */
21131 i = row->used[TEXT_AREA] - (i + 1);
21134 /* produce_special_glyphs overwrites the last glyph, so
21135 we don't want that if we want to keep that last
21136 glyph, which means it's an image. */
21137 if (it->current_x > it->last_visible_x)
21139 it->current_x = x_before;
21140 if (!FRAME_WINDOW_P (it->f))
21142 for (n = row->used[TEXT_AREA]; i < n; ++i)
21144 row->used[TEXT_AREA] = i;
21145 produce_special_glyphs (it, IT_TRUNCATION);
21148 else
21150 row->used[TEXT_AREA] = i;
21151 produce_special_glyphs (it, IT_TRUNCATION);
21153 it->hpos = hpos_before;
21156 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21158 /* Don't truncate if we can overflow newline into fringe. */
21159 if (!get_next_display_element (it))
21161 it->continuation_lines_width = 0;
21162 row->ends_at_zv_p = true;
21163 row->exact_window_width_line_p = true;
21164 break;
21166 if (ITERATOR_AT_END_OF_LINE_P (it))
21168 row->exact_window_width_line_p = true;
21169 goto at_end_of_line;
21171 it->current_x = x_before;
21172 it->hpos = hpos_before;
21175 row->truncated_on_right_p = true;
21176 it->continuation_lines_width = 0;
21177 reseat_at_next_visible_line_start (it, false);
21178 /* We insist below that IT's position be at ZV because in
21179 bidi-reordered lines the character at visible line start
21180 might not be the character that follows the newline in
21181 the logical order. */
21182 if (IT_BYTEPOS (*it) > BEG_BYTE)
21183 row->ends_at_zv_p =
21184 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21185 else
21186 row->ends_at_zv_p = false;
21187 break;
21191 if (wrap_data)
21192 bidi_unshelve_cache (wrap_data, true);
21194 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21195 at the left window margin. */
21196 if (it->first_visible_x
21197 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21199 if (!FRAME_WINDOW_P (it->f)
21200 || (((row->reversed_p
21201 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21202 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21203 /* Don't let insert_left_trunc_glyphs overwrite the
21204 first glyph of the row if it is an image. */
21205 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21206 insert_left_trunc_glyphs (it);
21207 row->truncated_on_left_p = true;
21210 /* Remember the position at which this line ends.
21212 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21213 cannot be before the call to find_row_edges below, since that is
21214 where these positions are determined. */
21215 row->end = it->current;
21216 if (!it->bidi_p)
21218 row->minpos = row->start.pos;
21219 row->maxpos = row->end.pos;
21221 else
21223 /* ROW->minpos and ROW->maxpos must be the smallest and
21224 `1 + the largest' buffer positions in ROW. But if ROW was
21225 bidi-reordered, these two positions can be anywhere in the
21226 row, so we must determine them now. */
21227 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21230 /* If the start of this line is the overlay arrow-position, then
21231 mark this glyph row as the one containing the overlay arrow.
21232 This is clearly a mess with variable size fonts. It would be
21233 better to let it be displayed like cursors under X. */
21234 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21235 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21236 !NILP (overlay_arrow_string)))
21238 /* Overlay arrow in window redisplay is a fringe bitmap. */
21239 if (STRINGP (overlay_arrow_string))
21241 struct glyph_row *arrow_row
21242 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21243 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21244 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21245 struct glyph *p = row->glyphs[TEXT_AREA];
21246 struct glyph *p2, *end;
21248 /* Copy the arrow glyphs. */
21249 while (glyph < arrow_end)
21250 *p++ = *glyph++;
21252 /* Throw away padding glyphs. */
21253 p2 = p;
21254 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21255 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21256 ++p2;
21257 if (p2 > p)
21259 while (p2 < end)
21260 *p++ = *p2++;
21261 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21264 else
21266 eassert (INTEGERP (overlay_arrow_string));
21267 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21269 overlay_arrow_seen = true;
21272 /* Highlight trailing whitespace. */
21273 if (!NILP (Vshow_trailing_whitespace))
21274 highlight_trailing_whitespace (it->f, it->glyph_row);
21276 /* Compute pixel dimensions of this line. */
21277 compute_line_metrics (it);
21279 /* Implementation note: No changes in the glyphs of ROW or in their
21280 faces can be done past this point, because compute_line_metrics
21281 computes ROW's hash value and stores it within the glyph_row
21282 structure. */
21284 /* Record whether this row ends inside an ellipsis. */
21285 row->ends_in_ellipsis_p
21286 = (it->method == GET_FROM_DISPLAY_VECTOR
21287 && it->ellipsis_p);
21289 /* Save fringe bitmaps in this row. */
21290 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21291 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21292 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21293 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21295 it->left_user_fringe_bitmap = 0;
21296 it->left_user_fringe_face_id = 0;
21297 it->right_user_fringe_bitmap = 0;
21298 it->right_user_fringe_face_id = 0;
21300 /* Maybe set the cursor. */
21301 cvpos = it->w->cursor.vpos;
21302 if ((cvpos < 0
21303 /* In bidi-reordered rows, keep checking for proper cursor
21304 position even if one has been found already, because buffer
21305 positions in such rows change non-linearly with ROW->VPOS,
21306 when a line is continued. One exception: when we are at ZV,
21307 display cursor on the first suitable glyph row, since all
21308 the empty rows after that also have their position set to ZV. */
21309 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21310 lines' rows is implemented for bidi-reordered rows. */
21311 || (it->bidi_p
21312 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21313 && PT >= MATRIX_ROW_START_CHARPOS (row)
21314 && PT <= MATRIX_ROW_END_CHARPOS (row)
21315 && cursor_row_p (row))
21316 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21318 /* Prepare for the next line. This line starts horizontally at (X
21319 HPOS) = (0 0). Vertical positions are incremented. As a
21320 convenience for the caller, IT->glyph_row is set to the next
21321 row to be used. */
21322 it->current_x = it->hpos = 0;
21323 it->current_y += row->height;
21324 SET_TEXT_POS (it->eol_pos, 0, 0);
21325 ++it->vpos;
21326 ++it->glyph_row;
21327 /* The next row should by default use the same value of the
21328 reversed_p flag as this one. set_iterator_to_next decides when
21329 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21330 the flag accordingly. */
21331 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21332 it->glyph_row->reversed_p = row->reversed_p;
21333 it->start = row->end;
21334 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21336 #undef RECORD_MAX_MIN_POS
21339 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21340 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21341 doc: /* Return paragraph direction at point in BUFFER.
21342 Value is either `left-to-right' or `right-to-left'.
21343 If BUFFER is omitted or nil, it defaults to the current buffer.
21345 Paragraph direction determines how the text in the paragraph is displayed.
21346 In left-to-right paragraphs, text begins at the left margin of the window
21347 and the reading direction is generally left to right. In right-to-left
21348 paragraphs, text begins at the right margin and is read from right to left.
21350 See also `bidi-paragraph-direction'. */)
21351 (Lisp_Object buffer)
21353 struct buffer *buf = current_buffer;
21354 struct buffer *old = buf;
21356 if (! NILP (buffer))
21358 CHECK_BUFFER (buffer);
21359 buf = XBUFFER (buffer);
21362 if (NILP (BVAR (buf, bidi_display_reordering))
21363 || NILP (BVAR (buf, enable_multibyte_characters))
21364 /* When we are loading loadup.el, the character property tables
21365 needed for bidi iteration are not yet available. */
21366 || redisplay__inhibit_bidi)
21367 return Qleft_to_right;
21368 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21369 return BVAR (buf, bidi_paragraph_direction);
21370 else
21372 /* Determine the direction from buffer text. We could try to
21373 use current_matrix if it is up to date, but this seems fast
21374 enough as it is. */
21375 struct bidi_it itb;
21376 ptrdiff_t pos = BUF_PT (buf);
21377 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21378 int c;
21379 void *itb_data = bidi_shelve_cache ();
21381 set_buffer_temp (buf);
21382 /* bidi_paragraph_init finds the base direction of the paragraph
21383 by searching forward from paragraph start. We need the base
21384 direction of the current or _previous_ paragraph, so we need
21385 to make sure we are within that paragraph. To that end, find
21386 the previous non-empty line. */
21387 if (pos >= ZV && pos > BEGV)
21388 DEC_BOTH (pos, bytepos);
21389 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21390 if (fast_looking_at (trailing_white_space,
21391 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21393 while ((c = FETCH_BYTE (bytepos)) == '\n'
21394 || c == ' ' || c == '\t' || c == '\f')
21396 if (bytepos <= BEGV_BYTE)
21397 break;
21398 bytepos--;
21399 pos--;
21401 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21402 bytepos--;
21404 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21405 itb.paragraph_dir = NEUTRAL_DIR;
21406 itb.string.s = NULL;
21407 itb.string.lstring = Qnil;
21408 itb.string.bufpos = 0;
21409 itb.string.from_disp_str = false;
21410 itb.string.unibyte = false;
21411 /* We have no window to use here for ignoring window-specific
21412 overlays. Using NULL for window pointer will cause
21413 compute_display_string_pos to use the current buffer. */
21414 itb.w = NULL;
21415 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21416 bidi_unshelve_cache (itb_data, false);
21417 set_buffer_temp (old);
21418 switch (itb.paragraph_dir)
21420 case L2R:
21421 return Qleft_to_right;
21422 break;
21423 case R2L:
21424 return Qright_to_left;
21425 break;
21426 default:
21427 emacs_abort ();
21432 DEFUN ("bidi-find-overridden-directionality",
21433 Fbidi_find_overridden_directionality,
21434 Sbidi_find_overridden_directionality, 2, 3, 0,
21435 doc: /* Return position between FROM and TO where directionality was overridden.
21437 This function returns the first character position in the specified
21438 region of OBJECT where there is a character whose `bidi-class' property
21439 is `L', but which was forced to display as `R' by a directional
21440 override, and likewise with characters whose `bidi-class' is `R'
21441 or `AL' that were forced to display as `L'.
21443 If no such character is found, the function returns nil.
21445 OBJECT is a Lisp string or buffer to search for overridden
21446 directionality, and defaults to the current buffer if nil or omitted.
21447 OBJECT can also be a window, in which case the function will search
21448 the buffer displayed in that window. Passing the window instead of
21449 a buffer is preferable when the buffer is displayed in some window,
21450 because this function will then be able to correctly account for
21451 window-specific overlays, which can affect the results.
21453 Strong directional characters `L', `R', and `AL' can have their
21454 intrinsic directionality overridden by directional override
21455 control characters RLO (u+202e) and LRO (u+202d). See the
21456 function `get-char-code-property' for a way to inquire about
21457 the `bidi-class' property of a character. */)
21458 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21460 struct buffer *buf = current_buffer;
21461 struct buffer *old = buf;
21462 struct window *w = NULL;
21463 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21464 struct bidi_it itb;
21465 ptrdiff_t from_pos, to_pos, from_bpos;
21466 void *itb_data;
21468 if (!NILP (object))
21470 if (BUFFERP (object))
21471 buf = XBUFFER (object);
21472 else if (WINDOWP (object))
21474 w = decode_live_window (object);
21475 buf = XBUFFER (w->contents);
21476 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21478 else
21479 CHECK_STRING (object);
21482 if (STRINGP (object))
21484 /* Characters in unibyte strings are always treated by bidi.c as
21485 strong LTR. */
21486 if (!STRING_MULTIBYTE (object)
21487 /* When we are loading loadup.el, the character property
21488 tables needed for bidi iteration are not yet
21489 available. */
21490 || redisplay__inhibit_bidi)
21491 return Qnil;
21493 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21494 if (from_pos >= SCHARS (object))
21495 return Qnil;
21497 /* Set up the bidi iterator. */
21498 itb_data = bidi_shelve_cache ();
21499 itb.paragraph_dir = NEUTRAL_DIR;
21500 itb.string.lstring = object;
21501 itb.string.s = NULL;
21502 itb.string.schars = SCHARS (object);
21503 itb.string.bufpos = 0;
21504 itb.string.from_disp_str = false;
21505 itb.string.unibyte = false;
21506 itb.w = w;
21507 bidi_init_it (0, 0, frame_window_p, &itb);
21509 else
21511 /* Nothing this fancy can happen in unibyte buffers, or in a
21512 buffer that disabled reordering, or if FROM is at EOB. */
21513 if (NILP (BVAR (buf, bidi_display_reordering))
21514 || NILP (BVAR (buf, enable_multibyte_characters))
21515 /* When we are loading loadup.el, the character property
21516 tables needed for bidi iteration are not yet
21517 available. */
21518 || redisplay__inhibit_bidi)
21519 return Qnil;
21521 set_buffer_temp (buf);
21522 validate_region (&from, &to);
21523 from_pos = XINT (from);
21524 to_pos = XINT (to);
21525 if (from_pos >= ZV)
21526 return Qnil;
21528 /* Set up the bidi iterator. */
21529 itb_data = bidi_shelve_cache ();
21530 from_bpos = CHAR_TO_BYTE (from_pos);
21531 if (from_pos == BEGV)
21533 itb.charpos = BEGV;
21534 itb.bytepos = BEGV_BYTE;
21536 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21538 itb.charpos = from_pos;
21539 itb.bytepos = from_bpos;
21541 else
21542 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21543 -1, &itb.bytepos);
21544 itb.paragraph_dir = NEUTRAL_DIR;
21545 itb.string.s = NULL;
21546 itb.string.lstring = Qnil;
21547 itb.string.bufpos = 0;
21548 itb.string.from_disp_str = false;
21549 itb.string.unibyte = false;
21550 itb.w = w;
21551 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21554 ptrdiff_t found;
21555 do {
21556 /* For the purposes of this function, the actual base direction of
21557 the paragraph doesn't matter, so just set it to L2R. */
21558 bidi_paragraph_init (L2R, &itb, false);
21559 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21561 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21563 bidi_unshelve_cache (itb_data, false);
21564 set_buffer_temp (old);
21566 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21569 DEFUN ("move-point-visually", Fmove_point_visually,
21570 Smove_point_visually, 1, 1, 0,
21571 doc: /* Move point in the visual order in the specified DIRECTION.
21572 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21573 left.
21575 Value is the new character position of point. */)
21576 (Lisp_Object direction)
21578 struct window *w = XWINDOW (selected_window);
21579 struct buffer *b = XBUFFER (w->contents);
21580 struct glyph_row *row;
21581 int dir;
21582 Lisp_Object paragraph_dir;
21584 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21585 (!(ROW)->continued_p \
21586 && NILP ((GLYPH)->object) \
21587 && (GLYPH)->type == CHAR_GLYPH \
21588 && (GLYPH)->u.ch == ' ' \
21589 && (GLYPH)->charpos >= 0 \
21590 && !(GLYPH)->avoid_cursor_p)
21592 CHECK_NUMBER (direction);
21593 dir = XINT (direction);
21594 if (dir > 0)
21595 dir = 1;
21596 else
21597 dir = -1;
21599 /* If current matrix is up-to-date, we can use the information
21600 recorded in the glyphs, at least as long as the goal is on the
21601 screen. */
21602 if (w->window_end_valid
21603 && !windows_or_buffers_changed
21604 && b
21605 && !b->clip_changed
21606 && !b->prevent_redisplay_optimizations_p
21607 && !window_outdated (w)
21608 /* We rely below on the cursor coordinates to be up to date, but
21609 we cannot trust them if some command moved point since the
21610 last complete redisplay. */
21611 && w->last_point == BUF_PT (b)
21612 && w->cursor.vpos >= 0
21613 && w->cursor.vpos < w->current_matrix->nrows
21614 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21616 struct glyph *g = row->glyphs[TEXT_AREA];
21617 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21618 struct glyph *gpt = g + w->cursor.hpos;
21620 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21622 if (BUFFERP (g->object) && g->charpos != PT)
21624 SET_PT (g->charpos);
21625 w->cursor.vpos = -1;
21626 return make_number (PT);
21628 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21630 ptrdiff_t new_pos;
21632 if (BUFFERP (gpt->object))
21634 new_pos = PT;
21635 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21636 new_pos += (row->reversed_p ? -dir : dir);
21637 else
21638 new_pos -= (row->reversed_p ? -dir : dir);
21640 else if (BUFFERP (g->object))
21641 new_pos = g->charpos;
21642 else
21643 break;
21644 SET_PT (new_pos);
21645 w->cursor.vpos = -1;
21646 return make_number (PT);
21648 else if (ROW_GLYPH_NEWLINE_P (row, g))
21650 /* Glyphs inserted at the end of a non-empty line for
21651 positioning the cursor have zero charpos, so we must
21652 deduce the value of point by other means. */
21653 if (g->charpos > 0)
21654 SET_PT (g->charpos);
21655 else if (row->ends_at_zv_p && PT != ZV)
21656 SET_PT (ZV);
21657 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21658 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21659 else
21660 break;
21661 w->cursor.vpos = -1;
21662 return make_number (PT);
21665 if (g == e || NILP (g->object))
21667 if (row->truncated_on_left_p || row->truncated_on_right_p)
21668 goto simulate_display;
21669 if (!row->reversed_p)
21670 row += dir;
21671 else
21672 row -= dir;
21673 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21674 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21675 goto simulate_display;
21677 if (dir > 0)
21679 if (row->reversed_p && !row->continued_p)
21681 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21682 w->cursor.vpos = -1;
21683 return make_number (PT);
21685 g = row->glyphs[TEXT_AREA];
21686 e = g + row->used[TEXT_AREA];
21687 for ( ; g < e; g++)
21689 if (BUFFERP (g->object)
21690 /* Empty lines have only one glyph, which stands
21691 for the newline, and whose charpos is the
21692 buffer position of the newline. */
21693 || ROW_GLYPH_NEWLINE_P (row, g)
21694 /* When the buffer ends in a newline, the line at
21695 EOB also has one glyph, but its charpos is -1. */
21696 || (row->ends_at_zv_p
21697 && !row->reversed_p
21698 && NILP (g->object)
21699 && g->type == CHAR_GLYPH
21700 && g->u.ch == ' '))
21702 if (g->charpos > 0)
21703 SET_PT (g->charpos);
21704 else if (!row->reversed_p
21705 && row->ends_at_zv_p
21706 && PT != ZV)
21707 SET_PT (ZV);
21708 else
21709 continue;
21710 w->cursor.vpos = -1;
21711 return make_number (PT);
21715 else
21717 if (!row->reversed_p && !row->continued_p)
21719 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21720 w->cursor.vpos = -1;
21721 return make_number (PT);
21723 e = row->glyphs[TEXT_AREA];
21724 g = e + row->used[TEXT_AREA] - 1;
21725 for ( ; g >= e; g--)
21727 if (BUFFERP (g->object)
21728 || (ROW_GLYPH_NEWLINE_P (row, g)
21729 && g->charpos > 0)
21730 /* Empty R2L lines on GUI frames have the buffer
21731 position of the newline stored in the stretch
21732 glyph. */
21733 || g->type == STRETCH_GLYPH
21734 || (row->ends_at_zv_p
21735 && row->reversed_p
21736 && NILP (g->object)
21737 && g->type == CHAR_GLYPH
21738 && g->u.ch == ' '))
21740 if (g->charpos > 0)
21741 SET_PT (g->charpos);
21742 else if (row->reversed_p
21743 && row->ends_at_zv_p
21744 && PT != ZV)
21745 SET_PT (ZV);
21746 else
21747 continue;
21748 w->cursor.vpos = -1;
21749 return make_number (PT);
21756 simulate_display:
21758 /* If we wind up here, we failed to move by using the glyphs, so we
21759 need to simulate display instead. */
21761 if (b)
21762 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21763 else
21764 paragraph_dir = Qleft_to_right;
21765 if (EQ (paragraph_dir, Qright_to_left))
21766 dir = -dir;
21767 if (PT <= BEGV && dir < 0)
21768 xsignal0 (Qbeginning_of_buffer);
21769 else if (PT >= ZV && dir > 0)
21770 xsignal0 (Qend_of_buffer);
21771 else
21773 struct text_pos pt;
21774 struct it it;
21775 int pt_x, target_x, pixel_width, pt_vpos;
21776 bool at_eol_p;
21777 bool overshoot_expected = false;
21778 bool target_is_eol_p = false;
21780 /* Setup the arena. */
21781 SET_TEXT_POS (pt, PT, PT_BYTE);
21782 start_display (&it, w, pt);
21783 /* When lines are truncated, we could be called with point
21784 outside of the windows edges, in which case move_it_*
21785 functions either prematurely stop at window's edge or jump to
21786 the next screen line, whereas we rely below on our ability to
21787 reach point, in order to start from its X coordinate. So we
21788 need to disregard the window's horizontal extent in that case. */
21789 if (it.line_wrap == TRUNCATE)
21790 it.last_visible_x = INFINITY;
21792 if (it.cmp_it.id < 0
21793 && it.method == GET_FROM_STRING
21794 && it.area == TEXT_AREA
21795 && it.string_from_display_prop_p
21796 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21797 overshoot_expected = true;
21799 /* Find the X coordinate of point. We start from the beginning
21800 of this or previous line to make sure we are before point in
21801 the logical order (since the move_it_* functions can only
21802 move forward). */
21803 reseat:
21804 reseat_at_previous_visible_line_start (&it);
21805 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21806 if (IT_CHARPOS (it) != PT)
21808 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21809 -1, -1, -1, MOVE_TO_POS);
21810 /* If we missed point because the character there is
21811 displayed out of a display vector that has more than one
21812 glyph, retry expecting overshoot. */
21813 if (it.method == GET_FROM_DISPLAY_VECTOR
21814 && it.current.dpvec_index > 0
21815 && !overshoot_expected)
21817 overshoot_expected = true;
21818 goto reseat;
21820 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21821 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21823 pt_x = it.current_x;
21824 pt_vpos = it.vpos;
21825 if (dir > 0 || overshoot_expected)
21827 struct glyph_row *row = it.glyph_row;
21829 /* When point is at beginning of line, we don't have
21830 information about the glyph there loaded into struct
21831 it. Calling get_next_display_element fixes that. */
21832 if (pt_x == 0)
21833 get_next_display_element (&it);
21834 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21835 it.glyph_row = NULL;
21836 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21837 it.glyph_row = row;
21838 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21839 it, lest it will become out of sync with it's buffer
21840 position. */
21841 it.current_x = pt_x;
21843 else
21844 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21845 pixel_width = it.pixel_width;
21846 if (overshoot_expected && at_eol_p)
21847 pixel_width = 0;
21848 else if (pixel_width <= 0)
21849 pixel_width = 1;
21851 /* If there's a display string (or something similar) at point,
21852 we are actually at the glyph to the left of point, so we need
21853 to correct the X coordinate. */
21854 if (overshoot_expected)
21856 if (it.bidi_p)
21857 pt_x += pixel_width * it.bidi_it.scan_dir;
21858 else
21859 pt_x += pixel_width;
21862 /* Compute target X coordinate, either to the left or to the
21863 right of point. On TTY frames, all characters have the same
21864 pixel width of 1, so we can use that. On GUI frames we don't
21865 have an easy way of getting at the pixel width of the
21866 character to the left of point, so we use a different method
21867 of getting to that place. */
21868 if (dir > 0)
21869 target_x = pt_x + pixel_width;
21870 else
21871 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21873 /* Target X coordinate could be one line above or below the line
21874 of point, in which case we need to adjust the target X
21875 coordinate. Also, if moving to the left, we need to begin at
21876 the left edge of the point's screen line. */
21877 if (dir < 0)
21879 if (pt_x > 0)
21881 start_display (&it, w, pt);
21882 if (it.line_wrap == TRUNCATE)
21883 it.last_visible_x = INFINITY;
21884 reseat_at_previous_visible_line_start (&it);
21885 it.current_x = it.current_y = it.hpos = 0;
21886 if (pt_vpos != 0)
21887 move_it_by_lines (&it, pt_vpos);
21889 else
21891 move_it_by_lines (&it, -1);
21892 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21893 target_is_eol_p = true;
21894 /* Under word-wrap, we don't know the x coordinate of
21895 the last character displayed on the previous line,
21896 which immediately precedes the wrap point. To find
21897 out its x coordinate, we try moving to the right
21898 margin of the window, which will stop at the wrap
21899 point, and then reset target_x to point at the
21900 character that precedes the wrap point. This is not
21901 needed on GUI frames, because (see below) there we
21902 move from the left margin one grapheme cluster at a
21903 time, and stop when we hit the wrap point. */
21904 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21906 void *it_data = NULL;
21907 struct it it2;
21909 SAVE_IT (it2, it, it_data);
21910 move_it_in_display_line_to (&it, ZV, target_x,
21911 MOVE_TO_POS | MOVE_TO_X);
21912 /* If we arrived at target_x, that _is_ the last
21913 character on the previous line. */
21914 if (it.current_x != target_x)
21915 target_x = it.current_x - 1;
21916 RESTORE_IT (&it, &it2, it_data);
21920 else
21922 if (at_eol_p
21923 || (target_x >= it.last_visible_x
21924 && it.line_wrap != TRUNCATE))
21926 if (pt_x > 0)
21927 move_it_by_lines (&it, 0);
21928 move_it_by_lines (&it, 1);
21929 target_x = 0;
21933 /* Move to the target X coordinate. */
21934 /* On GUI frames, as we don't know the X coordinate of the
21935 character to the left of point, moving point to the left
21936 requires walking, one grapheme cluster at a time, until we
21937 find ourself at a place immediately to the left of the
21938 character at point. */
21939 if (FRAME_WINDOW_P (it.f) && dir < 0)
21941 struct text_pos new_pos;
21942 enum move_it_result rc = MOVE_X_REACHED;
21944 if (it.current_x == 0)
21945 get_next_display_element (&it);
21946 if (it.what == IT_COMPOSITION)
21948 new_pos.charpos = it.cmp_it.charpos;
21949 new_pos.bytepos = -1;
21951 else
21952 new_pos = it.current.pos;
21954 while (it.current_x + it.pixel_width <= target_x
21955 && (rc == MOVE_X_REACHED
21956 /* Under word-wrap, move_it_in_display_line_to
21957 stops at correct coordinates, but sometimes
21958 returns MOVE_POS_MATCH_OR_ZV. */
21959 || (it.line_wrap == WORD_WRAP
21960 && rc == MOVE_POS_MATCH_OR_ZV)))
21962 int new_x = it.current_x + it.pixel_width;
21964 /* For composed characters, we want the position of the
21965 first character in the grapheme cluster (usually, the
21966 composition's base character), whereas it.current
21967 might give us the position of the _last_ one, e.g. if
21968 the composition is rendered in reverse due to bidi
21969 reordering. */
21970 if (it.what == IT_COMPOSITION)
21972 new_pos.charpos = it.cmp_it.charpos;
21973 new_pos.bytepos = -1;
21975 else
21976 new_pos = it.current.pos;
21977 if (new_x == it.current_x)
21978 new_x++;
21979 rc = move_it_in_display_line_to (&it, ZV, new_x,
21980 MOVE_TO_POS | MOVE_TO_X);
21981 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21982 break;
21984 /* The previous position we saw in the loop is the one we
21985 want. */
21986 if (new_pos.bytepos == -1)
21987 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21988 it.current.pos = new_pos;
21990 else if (it.current_x != target_x)
21991 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21993 /* If we ended up in a display string that covers point, move to
21994 buffer position to the right in the visual order. */
21995 if (dir > 0)
21997 while (IT_CHARPOS (it) == PT)
21999 set_iterator_to_next (&it, false);
22000 if (!get_next_display_element (&it))
22001 break;
22005 /* Move point to that position. */
22006 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22009 return make_number (PT);
22011 #undef ROW_GLYPH_NEWLINE_P
22014 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22015 Sbidi_resolved_levels, 0, 1, 0,
22016 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22018 The resolved levels are produced by the Emacs bidi reordering engine
22019 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22020 read the Unicode Standard Annex 9 (UAX#9) for background information
22021 about these levels.
22023 VPOS is the zero-based number of the current window's screen line
22024 for which to produce the resolved levels. If VPOS is nil or omitted,
22025 it defaults to the screen line of point. If the window displays a
22026 header line, VPOS of zero will report on the header line, and first
22027 line of text in the window will have VPOS of 1.
22029 Value is an array of resolved levels, indexed by glyph number.
22030 Glyphs are numbered from zero starting from the beginning of the
22031 screen line, i.e. the left edge of the window for left-to-right lines
22032 and from the right edge for right-to-left lines. The resolved levels
22033 are produced only for the window's text area; text in display margins
22034 is not included.
22036 If the selected window's display is not up-to-date, or if the specified
22037 screen line does not display text, this function returns nil. It is
22038 highly recommended to bind this function to some simple key, like F8,
22039 in order to avoid these problems.
22041 This function exists mainly for testing the correctness of the
22042 Emacs UBA implementation, in particular with the test suite. */)
22043 (Lisp_Object vpos)
22045 struct window *w = XWINDOW (selected_window);
22046 struct buffer *b = XBUFFER (w->contents);
22047 int nrow;
22048 struct glyph_row *row;
22050 if (NILP (vpos))
22052 int d1, d2, d3, d4, d5;
22054 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22056 else
22058 CHECK_NUMBER_COERCE_MARKER (vpos);
22059 nrow = XINT (vpos);
22062 /* We require up-to-date glyph matrix for this window. */
22063 if (w->window_end_valid
22064 && !windows_or_buffers_changed
22065 && b
22066 && !b->clip_changed
22067 && !b->prevent_redisplay_optimizations_p
22068 && !window_outdated (w)
22069 && nrow >= 0
22070 && nrow < w->current_matrix->nrows
22071 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22072 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22074 struct glyph *g, *e, *g1;
22075 int nglyphs, i;
22076 Lisp_Object levels;
22078 if (!row->reversed_p) /* Left-to-right glyph row. */
22080 g = g1 = row->glyphs[TEXT_AREA];
22081 e = g + row->used[TEXT_AREA];
22083 /* Skip over glyphs at the start of the row that was
22084 generated by redisplay for its own needs. */
22085 while (g < e
22086 && NILP (g->object)
22087 && g->charpos < 0)
22088 g++;
22089 g1 = g;
22091 /* Count the "interesting" glyphs in this row. */
22092 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22093 nglyphs++;
22095 /* Create and fill the array. */
22096 levels = make_uninit_vector (nglyphs);
22097 for (i = 0; g1 < g; i++, g1++)
22098 ASET (levels, i, make_number (g1->resolved_level));
22100 else /* Right-to-left glyph row. */
22102 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22103 e = row->glyphs[TEXT_AREA] - 1;
22104 while (g > e
22105 && NILP (g->object)
22106 && g->charpos < 0)
22107 g--;
22108 g1 = g;
22109 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22110 nglyphs++;
22111 levels = make_uninit_vector (nglyphs);
22112 for (i = 0; g1 > g; i++, g1--)
22113 ASET (levels, i, make_number (g1->resolved_level));
22115 return levels;
22117 else
22118 return Qnil;
22123 /***********************************************************************
22124 Menu Bar
22125 ***********************************************************************/
22127 /* Redisplay the menu bar in the frame for window W.
22129 The menu bar of X frames that don't have X toolkit support is
22130 displayed in a special window W->frame->menu_bar_window.
22132 The menu bar of terminal frames is treated specially as far as
22133 glyph matrices are concerned. Menu bar lines are not part of
22134 windows, so the update is done directly on the frame matrix rows
22135 for the menu bar. */
22137 static void
22138 display_menu_bar (struct window *w)
22140 struct frame *f = XFRAME (WINDOW_FRAME (w));
22141 struct it it;
22142 Lisp_Object items;
22143 int i;
22145 /* Don't do all this for graphical frames. */
22146 #ifdef HAVE_NTGUI
22147 if (FRAME_W32_P (f))
22148 return;
22149 #endif
22150 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22151 if (FRAME_X_P (f))
22152 return;
22153 #endif
22155 #ifdef HAVE_NS
22156 if (FRAME_NS_P (f))
22157 return;
22158 #endif /* HAVE_NS */
22160 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22161 eassert (!FRAME_WINDOW_P (f));
22162 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22163 it.first_visible_x = 0;
22164 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22165 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22166 if (FRAME_WINDOW_P (f))
22168 /* Menu bar lines are displayed in the desired matrix of the
22169 dummy window menu_bar_window. */
22170 struct window *menu_w;
22171 menu_w = XWINDOW (f->menu_bar_window);
22172 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22173 MENU_FACE_ID);
22174 it.first_visible_x = 0;
22175 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22177 else
22178 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22180 /* This is a TTY frame, i.e. character hpos/vpos are used as
22181 pixel x/y. */
22182 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22183 MENU_FACE_ID);
22184 it.first_visible_x = 0;
22185 it.last_visible_x = FRAME_COLS (f);
22188 /* FIXME: This should be controlled by a user option. See the
22189 comments in redisplay_tool_bar and display_mode_line about
22190 this. */
22191 it.paragraph_embedding = L2R;
22193 /* Clear all rows of the menu bar. */
22194 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22196 struct glyph_row *row = it.glyph_row + i;
22197 clear_glyph_row (row);
22198 row->enabled_p = true;
22199 row->full_width_p = true;
22200 row->reversed_p = false;
22203 /* Display all items of the menu bar. */
22204 items = FRAME_MENU_BAR_ITEMS (it.f);
22205 for (i = 0; i < ASIZE (items); i += 4)
22207 Lisp_Object string;
22209 /* Stop at nil string. */
22210 string = AREF (items, i + 1);
22211 if (NILP (string))
22212 break;
22214 /* Remember where item was displayed. */
22215 ASET (items, i + 3, make_number (it.hpos));
22217 /* Display the item, pad with one space. */
22218 if (it.current_x < it.last_visible_x)
22219 display_string (NULL, string, Qnil, 0, 0, &it,
22220 SCHARS (string) + 1, 0, 0, -1);
22223 /* Fill out the line with spaces. */
22224 if (it.current_x < it.last_visible_x)
22225 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22227 /* Compute the total height of the lines. */
22228 compute_line_metrics (&it);
22231 /* Deep copy of a glyph row, including the glyphs. */
22232 static void
22233 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22235 struct glyph *pointers[1 + LAST_AREA];
22236 int to_used = to->used[TEXT_AREA];
22238 /* Save glyph pointers of TO. */
22239 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22241 /* Do a structure assignment. */
22242 *to = *from;
22244 /* Restore original glyph pointers of TO. */
22245 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22247 /* Copy the glyphs. */
22248 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22249 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22251 /* If we filled only part of the TO row, fill the rest with
22252 space_glyph (which will display as empty space). */
22253 if (to_used > from->used[TEXT_AREA])
22254 fill_up_frame_row_with_spaces (to, to_used);
22257 /* Display one menu item on a TTY, by overwriting the glyphs in the
22258 frame F's desired glyph matrix with glyphs produced from the menu
22259 item text. Called from term.c to display TTY drop-down menus one
22260 item at a time.
22262 ITEM_TEXT is the menu item text as a C string.
22264 FACE_ID is the face ID to be used for this menu item. FACE_ID
22265 could specify one of 3 faces: a face for an enabled item, a face
22266 for a disabled item, or a face for a selected item.
22268 X and Y are coordinates of the first glyph in the frame's desired
22269 matrix to be overwritten by the menu item. Since this is a TTY, Y
22270 is the zero-based number of the glyph row and X is the zero-based
22271 glyph number in the row, starting from left, where to start
22272 displaying the item.
22274 SUBMENU means this menu item drops down a submenu, which
22275 should be indicated by displaying a proper visual cue after the
22276 item text. */
22278 void
22279 display_tty_menu_item (const char *item_text, int width, int face_id,
22280 int x, int y, bool submenu)
22282 struct it it;
22283 struct frame *f = SELECTED_FRAME ();
22284 struct window *w = XWINDOW (f->selected_window);
22285 struct glyph_row *row;
22286 size_t item_len = strlen (item_text);
22288 eassert (FRAME_TERMCAP_P (f));
22290 /* Don't write beyond the matrix's last row. This can happen for
22291 TTY screens that are not high enough to show the entire menu.
22292 (This is actually a bit of defensive programming, as
22293 tty_menu_display already limits the number of menu items to one
22294 less than the number of screen lines.) */
22295 if (y >= f->desired_matrix->nrows)
22296 return;
22298 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22299 it.first_visible_x = 0;
22300 it.last_visible_x = FRAME_COLS (f) - 1;
22301 row = it.glyph_row;
22302 /* Start with the row contents from the current matrix. */
22303 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22304 bool saved_width = row->full_width_p;
22305 row->full_width_p = true;
22306 bool saved_reversed = row->reversed_p;
22307 row->reversed_p = false;
22308 row->enabled_p = true;
22310 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22311 desired face. */
22312 eassert (x < f->desired_matrix->matrix_w);
22313 it.current_x = it.hpos = x;
22314 it.current_y = it.vpos = y;
22315 int saved_used = row->used[TEXT_AREA];
22316 bool saved_truncated = row->truncated_on_right_p;
22317 row->used[TEXT_AREA] = x;
22318 it.face_id = face_id;
22319 it.line_wrap = TRUNCATE;
22321 /* FIXME: This should be controlled by a user option. See the
22322 comments in redisplay_tool_bar and display_mode_line about this.
22323 Also, if paragraph_embedding could ever be R2L, changes will be
22324 needed to avoid shifting to the right the row characters in
22325 term.c:append_glyph. */
22326 it.paragraph_embedding = L2R;
22328 /* Pad with a space on the left. */
22329 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22330 width--;
22331 /* Display the menu item, pad with spaces to WIDTH. */
22332 if (submenu)
22334 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22335 item_len, 0, FRAME_COLS (f) - 1, -1);
22336 width -= item_len;
22337 /* Indicate with " >" that there's a submenu. */
22338 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22339 FRAME_COLS (f) - 1, -1);
22341 else
22342 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22343 width, 0, FRAME_COLS (f) - 1, -1);
22345 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22346 row->truncated_on_right_p = saved_truncated;
22347 row->hash = row_hash (row);
22348 row->full_width_p = saved_width;
22349 row->reversed_p = saved_reversed;
22352 /***********************************************************************
22353 Mode Line
22354 ***********************************************************************/
22356 /* Redisplay mode lines in the window tree whose root is WINDOW.
22357 If FORCE, redisplay mode lines unconditionally.
22358 Otherwise, redisplay only mode lines that are garbaged. Value is
22359 the number of windows whose mode lines were redisplayed. */
22361 static int
22362 redisplay_mode_lines (Lisp_Object window, bool force)
22364 int nwindows = 0;
22366 while (!NILP (window))
22368 struct window *w = XWINDOW (window);
22370 if (WINDOWP (w->contents))
22371 nwindows += redisplay_mode_lines (w->contents, force);
22372 else if (force
22373 || FRAME_GARBAGED_P (XFRAME (w->frame))
22374 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22376 struct text_pos lpoint;
22377 struct buffer *old = current_buffer;
22379 /* Set the window's buffer for the mode line display. */
22380 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22381 set_buffer_internal_1 (XBUFFER (w->contents));
22383 /* Point refers normally to the selected window. For any
22384 other window, set up appropriate value. */
22385 if (!EQ (window, selected_window))
22387 struct text_pos pt;
22389 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22390 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22393 /* Display mode lines. */
22394 clear_glyph_matrix (w->desired_matrix);
22395 if (display_mode_lines (w))
22396 ++nwindows;
22398 /* Restore old settings. */
22399 set_buffer_internal_1 (old);
22400 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22403 window = w->next;
22406 return nwindows;
22410 /* Display the mode and/or header line of window W. Value is the
22411 sum number of mode lines and header lines displayed. */
22413 static int
22414 display_mode_lines (struct window *w)
22416 Lisp_Object old_selected_window = selected_window;
22417 Lisp_Object old_selected_frame = selected_frame;
22418 Lisp_Object new_frame = w->frame;
22419 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22420 int n = 0;
22422 selected_frame = new_frame;
22423 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22424 or window's point, then we'd need select_window_1 here as well. */
22425 XSETWINDOW (selected_window, w);
22426 XFRAME (new_frame)->selected_window = selected_window;
22428 /* These will be set while the mode line specs are processed. */
22429 line_number_displayed = false;
22430 w->column_number_displayed = -1;
22432 if (WINDOW_WANTS_MODELINE_P (w))
22434 struct window *sel_w = XWINDOW (old_selected_window);
22436 /* Select mode line face based on the real selected window. */
22437 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22438 BVAR (current_buffer, mode_line_format));
22439 ++n;
22442 if (WINDOW_WANTS_HEADER_LINE_P (w))
22444 display_mode_line (w, HEADER_LINE_FACE_ID,
22445 BVAR (current_buffer, header_line_format));
22446 ++n;
22449 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22450 selected_frame = old_selected_frame;
22451 selected_window = old_selected_window;
22452 if (n > 0)
22453 w->must_be_updated_p = true;
22454 return n;
22458 /* Display mode or header line of window W. FACE_ID specifies which
22459 line to display; it is either MODE_LINE_FACE_ID or
22460 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22461 display. Value is the pixel height of the mode/header line
22462 displayed. */
22464 static int
22465 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22467 struct it it;
22468 struct face *face;
22469 ptrdiff_t count = SPECPDL_INDEX ();
22471 init_iterator (&it, w, -1, -1, NULL, face_id);
22472 /* Don't extend on a previously drawn mode-line.
22473 This may happen if called from pos_visible_p. */
22474 it.glyph_row->enabled_p = false;
22475 prepare_desired_row (w, it.glyph_row, true);
22477 it.glyph_row->mode_line_p = true;
22479 /* FIXME: This should be controlled by a user option. But
22480 supporting such an option is not trivial, since the mode line is
22481 made up of many separate strings. */
22482 it.paragraph_embedding = L2R;
22484 record_unwind_protect (unwind_format_mode_line,
22485 format_mode_line_unwind_data (NULL, NULL,
22486 Qnil, false));
22488 mode_line_target = MODE_LINE_DISPLAY;
22490 /* Temporarily make frame's keyboard the current kboard so that
22491 kboard-local variables in the mode_line_format will get the right
22492 values. */
22493 push_kboard (FRAME_KBOARD (it.f));
22494 record_unwind_save_match_data ();
22495 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22496 pop_kboard ();
22498 unbind_to (count, Qnil);
22500 /* Fill up with spaces. */
22501 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22503 compute_line_metrics (&it);
22504 it.glyph_row->full_width_p = true;
22505 it.glyph_row->continued_p = false;
22506 it.glyph_row->truncated_on_left_p = false;
22507 it.glyph_row->truncated_on_right_p = false;
22509 /* Make a 3D mode-line have a shadow at its right end. */
22510 face = FACE_FROM_ID (it.f, face_id);
22511 extend_face_to_end_of_line (&it);
22512 if (face->box != FACE_NO_BOX)
22514 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22515 + it.glyph_row->used[TEXT_AREA] - 1);
22516 last->right_box_line_p = true;
22519 return it.glyph_row->height;
22522 /* Move element ELT in LIST to the front of LIST.
22523 Return the updated list. */
22525 static Lisp_Object
22526 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22528 register Lisp_Object tail, prev;
22529 register Lisp_Object tem;
22531 tail = list;
22532 prev = Qnil;
22533 while (CONSP (tail))
22535 tem = XCAR (tail);
22537 if (EQ (elt, tem))
22539 /* Splice out the link TAIL. */
22540 if (NILP (prev))
22541 list = XCDR (tail);
22542 else
22543 Fsetcdr (prev, XCDR (tail));
22545 /* Now make it the first. */
22546 Fsetcdr (tail, list);
22547 return tail;
22549 else
22550 prev = tail;
22551 tail = XCDR (tail);
22552 QUIT;
22555 /* Not found--return unchanged LIST. */
22556 return list;
22559 /* Contribute ELT to the mode line for window IT->w. How it
22560 translates into text depends on its data type.
22562 IT describes the display environment in which we display, as usual.
22564 DEPTH is the depth in recursion. It is used to prevent
22565 infinite recursion here.
22567 FIELD_WIDTH is the number of characters the display of ELT should
22568 occupy in the mode line, and PRECISION is the maximum number of
22569 characters to display from ELT's representation. See
22570 display_string for details.
22572 Returns the hpos of the end of the text generated by ELT.
22574 PROPS is a property list to add to any string we encounter.
22576 If RISKY, remove (disregard) any properties in any string
22577 we encounter, and ignore :eval and :propertize.
22579 The global variable `mode_line_target' determines whether the
22580 output is passed to `store_mode_line_noprop',
22581 `store_mode_line_string', or `display_string'. */
22583 static int
22584 display_mode_element (struct it *it, int depth, int field_width, int precision,
22585 Lisp_Object elt, Lisp_Object props, bool risky)
22587 int n = 0, field, prec;
22588 bool literal = false;
22590 tail_recurse:
22591 if (depth > 100)
22592 elt = build_string ("*too-deep*");
22594 depth++;
22596 switch (XTYPE (elt))
22598 case Lisp_String:
22600 /* A string: output it and check for %-constructs within it. */
22601 unsigned char c;
22602 ptrdiff_t offset = 0;
22604 if (SCHARS (elt) > 0
22605 && (!NILP (props) || risky))
22607 Lisp_Object oprops, aelt;
22608 oprops = Ftext_properties_at (make_number (0), elt);
22610 /* If the starting string's properties are not what
22611 we want, translate the string. Also, if the string
22612 is risky, do that anyway. */
22614 if (NILP (Fequal (props, oprops)) || risky)
22616 /* If the starting string has properties,
22617 merge the specified ones onto the existing ones. */
22618 if (! NILP (oprops) && !risky)
22620 Lisp_Object tem;
22622 oprops = Fcopy_sequence (oprops);
22623 tem = props;
22624 while (CONSP (tem))
22626 oprops = Fplist_put (oprops, XCAR (tem),
22627 XCAR (XCDR (tem)));
22628 tem = XCDR (XCDR (tem));
22630 props = oprops;
22633 aelt = Fassoc (elt, mode_line_proptrans_alist);
22634 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22636 /* AELT is what we want. Move it to the front
22637 without consing. */
22638 elt = XCAR (aelt);
22639 mode_line_proptrans_alist
22640 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22642 else
22644 Lisp_Object tem;
22646 /* If AELT has the wrong props, it is useless.
22647 so get rid of it. */
22648 if (! NILP (aelt))
22649 mode_line_proptrans_alist
22650 = Fdelq (aelt, mode_line_proptrans_alist);
22652 elt = Fcopy_sequence (elt);
22653 Fset_text_properties (make_number (0), Flength (elt),
22654 props, elt);
22655 /* Add this item to mode_line_proptrans_alist. */
22656 mode_line_proptrans_alist
22657 = Fcons (Fcons (elt, props),
22658 mode_line_proptrans_alist);
22659 /* Truncate mode_line_proptrans_alist
22660 to at most 50 elements. */
22661 tem = Fnthcdr (make_number (50),
22662 mode_line_proptrans_alist);
22663 if (! NILP (tem))
22664 XSETCDR (tem, Qnil);
22669 offset = 0;
22671 if (literal)
22673 prec = precision - n;
22674 switch (mode_line_target)
22676 case MODE_LINE_NOPROP:
22677 case MODE_LINE_TITLE:
22678 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22679 break;
22680 case MODE_LINE_STRING:
22681 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22682 break;
22683 case MODE_LINE_DISPLAY:
22684 n += display_string (NULL, elt, Qnil, 0, 0, it,
22685 0, prec, 0, STRING_MULTIBYTE (elt));
22686 break;
22689 break;
22692 /* Handle the non-literal case. */
22694 while ((precision <= 0 || n < precision)
22695 && SREF (elt, offset) != 0
22696 && (mode_line_target != MODE_LINE_DISPLAY
22697 || it->current_x < it->last_visible_x))
22699 ptrdiff_t last_offset = offset;
22701 /* Advance to end of string or next format specifier. */
22702 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22705 if (offset - 1 != last_offset)
22707 ptrdiff_t nchars, nbytes;
22709 /* Output to end of string or up to '%'. Field width
22710 is length of string. Don't output more than
22711 PRECISION allows us. */
22712 offset--;
22714 prec = c_string_width (SDATA (elt) + last_offset,
22715 offset - last_offset, precision - n,
22716 &nchars, &nbytes);
22718 switch (mode_line_target)
22720 case MODE_LINE_NOPROP:
22721 case MODE_LINE_TITLE:
22722 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22723 break;
22724 case MODE_LINE_STRING:
22726 ptrdiff_t bytepos = last_offset;
22727 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22728 ptrdiff_t endpos = (precision <= 0
22729 ? string_byte_to_char (elt, offset)
22730 : charpos + nchars);
22731 Lisp_Object mode_string
22732 = Fsubstring (elt, make_number (charpos),
22733 make_number (endpos));
22734 n += store_mode_line_string (NULL, mode_string, false,
22735 0, 0, Qnil);
22737 break;
22738 case MODE_LINE_DISPLAY:
22740 ptrdiff_t bytepos = last_offset;
22741 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22743 if (precision <= 0)
22744 nchars = string_byte_to_char (elt, offset) - charpos;
22745 n += display_string (NULL, elt, Qnil, 0, charpos,
22746 it, 0, nchars, 0,
22747 STRING_MULTIBYTE (elt));
22749 break;
22752 else /* c == '%' */
22754 ptrdiff_t percent_position = offset;
22756 /* Get the specified minimum width. Zero means
22757 don't pad. */
22758 field = 0;
22759 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22760 field = field * 10 + c - '0';
22762 /* Don't pad beyond the total padding allowed. */
22763 if (field_width - n > 0 && field > field_width - n)
22764 field = field_width - n;
22766 /* Note that either PRECISION <= 0 or N < PRECISION. */
22767 prec = precision - n;
22769 if (c == 'M')
22770 n += display_mode_element (it, depth, field, prec,
22771 Vglobal_mode_string, props,
22772 risky);
22773 else if (c != 0)
22775 bool multibyte;
22776 ptrdiff_t bytepos, charpos;
22777 const char *spec;
22778 Lisp_Object string;
22780 bytepos = percent_position;
22781 charpos = (STRING_MULTIBYTE (elt)
22782 ? string_byte_to_char (elt, bytepos)
22783 : bytepos);
22784 spec = decode_mode_spec (it->w, c, field, &string);
22785 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22787 switch (mode_line_target)
22789 case MODE_LINE_NOPROP:
22790 case MODE_LINE_TITLE:
22791 n += store_mode_line_noprop (spec, field, prec);
22792 break;
22793 case MODE_LINE_STRING:
22795 Lisp_Object tem = build_string (spec);
22796 props = Ftext_properties_at (make_number (charpos), elt);
22797 /* Should only keep face property in props */
22798 n += store_mode_line_string (NULL, tem, false,
22799 field, prec, props);
22801 break;
22802 case MODE_LINE_DISPLAY:
22804 int nglyphs_before, nwritten;
22806 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22807 nwritten = display_string (spec, string, elt,
22808 charpos, 0, it,
22809 field, prec, 0,
22810 multibyte);
22812 /* Assign to the glyphs written above the
22813 string where the `%x' came from, position
22814 of the `%'. */
22815 if (nwritten > 0)
22817 struct glyph *glyph
22818 = (it->glyph_row->glyphs[TEXT_AREA]
22819 + nglyphs_before);
22820 int i;
22822 for (i = 0; i < nwritten; ++i)
22824 glyph[i].object = elt;
22825 glyph[i].charpos = charpos;
22828 n += nwritten;
22831 break;
22834 else /* c == 0 */
22835 break;
22839 break;
22841 case Lisp_Symbol:
22842 /* A symbol: process the value of the symbol recursively
22843 as if it appeared here directly. Avoid error if symbol void.
22844 Special case: if value of symbol is a string, output the string
22845 literally. */
22847 register Lisp_Object tem;
22849 /* If the variable is not marked as risky to set
22850 then its contents are risky to use. */
22851 if (NILP (Fget (elt, Qrisky_local_variable)))
22852 risky = true;
22854 tem = Fboundp (elt);
22855 if (!NILP (tem))
22857 tem = Fsymbol_value (elt);
22858 /* If value is a string, output that string literally:
22859 don't check for % within it. */
22860 if (STRINGP (tem))
22861 literal = true;
22863 if (!EQ (tem, elt))
22865 /* Give up right away for nil or t. */
22866 elt = tem;
22867 goto tail_recurse;
22871 break;
22873 case Lisp_Cons:
22875 register Lisp_Object car, tem;
22877 /* A cons cell: five distinct cases.
22878 If first element is :eval or :propertize, do something special.
22879 If first element is a string or a cons, process all the elements
22880 and effectively concatenate them.
22881 If first element is a negative number, truncate displaying cdr to
22882 at most that many characters. If positive, pad (with spaces)
22883 to at least that many characters.
22884 If first element is a symbol, process the cadr or caddr recursively
22885 according to whether the symbol's value is non-nil or nil. */
22886 car = XCAR (elt);
22887 if (EQ (car, QCeval))
22889 /* An element of the form (:eval FORM) means evaluate FORM
22890 and use the result as mode line elements. */
22892 if (risky)
22893 break;
22895 if (CONSP (XCDR (elt)))
22897 Lisp_Object spec;
22898 spec = safe__eval (true, XCAR (XCDR (elt)));
22899 n += display_mode_element (it, depth, field_width - n,
22900 precision - n, spec, props,
22901 risky);
22904 else if (EQ (car, QCpropertize))
22906 /* An element of the form (:propertize ELT PROPS...)
22907 means display ELT but applying properties PROPS. */
22909 if (risky)
22910 break;
22912 if (CONSP (XCDR (elt)))
22913 n += display_mode_element (it, depth, field_width - n,
22914 precision - n, XCAR (XCDR (elt)),
22915 XCDR (XCDR (elt)), risky);
22917 else if (SYMBOLP (car))
22919 tem = Fboundp (car);
22920 elt = XCDR (elt);
22921 if (!CONSP (elt))
22922 goto invalid;
22923 /* elt is now the cdr, and we know it is a cons cell.
22924 Use its car if CAR has a non-nil value. */
22925 if (!NILP (tem))
22927 tem = Fsymbol_value (car);
22928 if (!NILP (tem))
22930 elt = XCAR (elt);
22931 goto tail_recurse;
22934 /* Symbol's value is nil (or symbol is unbound)
22935 Get the cddr of the original list
22936 and if possible find the caddr and use that. */
22937 elt = XCDR (elt);
22938 if (NILP (elt))
22939 break;
22940 else if (!CONSP (elt))
22941 goto invalid;
22942 elt = XCAR (elt);
22943 goto tail_recurse;
22945 else if (INTEGERP (car))
22947 register int lim = XINT (car);
22948 elt = XCDR (elt);
22949 if (lim < 0)
22951 /* Negative int means reduce maximum width. */
22952 if (precision <= 0)
22953 precision = -lim;
22954 else
22955 precision = min (precision, -lim);
22957 else if (lim > 0)
22959 /* Padding specified. Don't let it be more than
22960 current maximum. */
22961 if (precision > 0)
22962 lim = min (precision, lim);
22964 /* If that's more padding than already wanted, queue it.
22965 But don't reduce padding already specified even if
22966 that is beyond the current truncation point. */
22967 field_width = max (lim, field_width);
22969 goto tail_recurse;
22971 else if (STRINGP (car) || CONSP (car))
22973 Lisp_Object halftail = elt;
22974 int len = 0;
22976 while (CONSP (elt)
22977 && (precision <= 0 || n < precision))
22979 n += display_mode_element (it, depth,
22980 /* Do padding only after the last
22981 element in the list. */
22982 (! CONSP (XCDR (elt))
22983 ? field_width - n
22984 : 0),
22985 precision - n, XCAR (elt),
22986 props, risky);
22987 elt = XCDR (elt);
22988 len++;
22989 if ((len & 1) == 0)
22990 halftail = XCDR (halftail);
22991 /* Check for cycle. */
22992 if (EQ (halftail, elt))
22993 break;
22997 break;
22999 default:
23000 invalid:
23001 elt = build_string ("*invalid*");
23002 goto tail_recurse;
23005 /* Pad to FIELD_WIDTH. */
23006 if (field_width > 0 && n < field_width)
23008 switch (mode_line_target)
23010 case MODE_LINE_NOPROP:
23011 case MODE_LINE_TITLE:
23012 n += store_mode_line_noprop ("", field_width - n, 0);
23013 break;
23014 case MODE_LINE_STRING:
23015 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23016 Qnil);
23017 break;
23018 case MODE_LINE_DISPLAY:
23019 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23020 0, 0, 0);
23021 break;
23025 return n;
23028 /* Store a mode-line string element in mode_line_string_list.
23030 If STRING is non-null, display that C string. Otherwise, the Lisp
23031 string LISP_STRING is displayed.
23033 FIELD_WIDTH is the minimum number of output glyphs to produce.
23034 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23035 with spaces. FIELD_WIDTH <= 0 means don't pad.
23037 PRECISION is the maximum number of characters to output from
23038 STRING. PRECISION <= 0 means don't truncate the string.
23040 If COPY_STRING, make a copy of LISP_STRING before adding
23041 properties to the string.
23043 PROPS are the properties to add to the string.
23044 The mode_line_string_face face property is always added to the string.
23047 static int
23048 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23049 bool copy_string,
23050 int field_width, int precision, Lisp_Object props)
23052 ptrdiff_t len;
23053 int n = 0;
23055 if (string != NULL)
23057 len = strlen (string);
23058 if (precision > 0 && len > precision)
23059 len = precision;
23060 lisp_string = make_string (string, len);
23061 if (NILP (props))
23062 props = mode_line_string_face_prop;
23063 else if (!NILP (mode_line_string_face))
23065 Lisp_Object face = Fplist_get (props, Qface);
23066 props = Fcopy_sequence (props);
23067 if (NILP (face))
23068 face = mode_line_string_face;
23069 else
23070 face = list2 (face, mode_line_string_face);
23071 props = Fplist_put (props, Qface, face);
23073 Fadd_text_properties (make_number (0), make_number (len),
23074 props, lisp_string);
23076 else
23078 len = XFASTINT (Flength (lisp_string));
23079 if (precision > 0 && len > precision)
23081 len = precision;
23082 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23083 precision = -1;
23085 if (!NILP (mode_line_string_face))
23087 Lisp_Object face;
23088 if (NILP (props))
23089 props = Ftext_properties_at (make_number (0), lisp_string);
23090 face = Fplist_get (props, Qface);
23091 if (NILP (face))
23092 face = mode_line_string_face;
23093 else
23094 face = list2 (face, mode_line_string_face);
23095 props = list2 (Qface, face);
23096 if (copy_string)
23097 lisp_string = Fcopy_sequence (lisp_string);
23099 if (!NILP (props))
23100 Fadd_text_properties (make_number (0), make_number (len),
23101 props, lisp_string);
23104 if (len > 0)
23106 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23107 n += len;
23110 if (field_width > len)
23112 field_width -= len;
23113 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23114 if (!NILP (props))
23115 Fadd_text_properties (make_number (0), make_number (field_width),
23116 props, lisp_string);
23117 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23118 n += field_width;
23121 return n;
23125 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23126 1, 4, 0,
23127 doc: /* Format a string out of a mode line format specification.
23128 First arg FORMAT specifies the mode line format (see `mode-line-format'
23129 for details) to use.
23131 By default, the format is evaluated for the currently selected window.
23133 Optional second arg FACE specifies the face property to put on all
23134 characters for which no face is specified. The value nil means the
23135 default face. The value t means whatever face the window's mode line
23136 currently uses (either `mode-line' or `mode-line-inactive',
23137 depending on whether the window is the selected window or not).
23138 An integer value means the value string has no text
23139 properties.
23141 Optional third and fourth args WINDOW and BUFFER specify the window
23142 and buffer to use as the context for the formatting (defaults
23143 are the selected window and the WINDOW's buffer). */)
23144 (Lisp_Object format, Lisp_Object face,
23145 Lisp_Object window, Lisp_Object buffer)
23147 struct it it;
23148 int len;
23149 struct window *w;
23150 struct buffer *old_buffer = NULL;
23151 int face_id;
23152 bool no_props = INTEGERP (face);
23153 ptrdiff_t count = SPECPDL_INDEX ();
23154 Lisp_Object str;
23155 int string_start = 0;
23157 w = decode_any_window (window);
23158 XSETWINDOW (window, w);
23160 if (NILP (buffer))
23161 buffer = w->contents;
23162 CHECK_BUFFER (buffer);
23164 /* Make formatting the modeline a non-op when noninteractive, otherwise
23165 there will be problems later caused by a partially initialized frame. */
23166 if (NILP (format) || noninteractive)
23167 return empty_unibyte_string;
23169 if (no_props)
23170 face = Qnil;
23172 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23173 : EQ (face, Qt) ? (EQ (window, selected_window)
23174 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23175 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23176 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23177 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23178 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23179 : DEFAULT_FACE_ID;
23181 old_buffer = current_buffer;
23183 /* Save things including mode_line_proptrans_alist,
23184 and set that to nil so that we don't alter the outer value. */
23185 record_unwind_protect (unwind_format_mode_line,
23186 format_mode_line_unwind_data
23187 (XFRAME (WINDOW_FRAME (w)),
23188 old_buffer, selected_window, true));
23189 mode_line_proptrans_alist = Qnil;
23191 Fselect_window (window, Qt);
23192 set_buffer_internal_1 (XBUFFER (buffer));
23194 init_iterator (&it, w, -1, -1, NULL, face_id);
23196 if (no_props)
23198 mode_line_target = MODE_LINE_NOPROP;
23199 mode_line_string_face_prop = Qnil;
23200 mode_line_string_list = Qnil;
23201 string_start = MODE_LINE_NOPROP_LEN (0);
23203 else
23205 mode_line_target = MODE_LINE_STRING;
23206 mode_line_string_list = Qnil;
23207 mode_line_string_face = face;
23208 mode_line_string_face_prop
23209 = NILP (face) ? Qnil : list2 (Qface, face);
23212 push_kboard (FRAME_KBOARD (it.f));
23213 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23214 pop_kboard ();
23216 if (no_props)
23218 len = MODE_LINE_NOPROP_LEN (string_start);
23219 str = make_string (mode_line_noprop_buf + string_start, len);
23221 else
23223 mode_line_string_list = Fnreverse (mode_line_string_list);
23224 str = Fmapconcat (Qidentity, mode_line_string_list,
23225 empty_unibyte_string);
23228 unbind_to (count, Qnil);
23229 return str;
23232 /* Write a null-terminated, right justified decimal representation of
23233 the positive integer D to BUF using a minimal field width WIDTH. */
23235 static void
23236 pint2str (register char *buf, register int width, register ptrdiff_t d)
23238 register char *p = buf;
23240 if (d <= 0)
23241 *p++ = '0';
23242 else
23244 while (d > 0)
23246 *p++ = d % 10 + '0';
23247 d /= 10;
23251 for (width -= (int) (p - buf); width > 0; --width)
23252 *p++ = ' ';
23253 *p-- = '\0';
23254 while (p > buf)
23256 d = *buf;
23257 *buf++ = *p;
23258 *p-- = d;
23262 /* Write a null-terminated, right justified decimal and "human
23263 readable" representation of the nonnegative integer D to BUF using
23264 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23266 static const char power_letter[] =
23268 0, /* no letter */
23269 'k', /* kilo */
23270 'M', /* mega */
23271 'G', /* giga */
23272 'T', /* tera */
23273 'P', /* peta */
23274 'E', /* exa */
23275 'Z', /* zetta */
23276 'Y' /* yotta */
23279 static void
23280 pint2hrstr (char *buf, int width, ptrdiff_t d)
23282 /* We aim to represent the nonnegative integer D as
23283 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23284 ptrdiff_t quotient = d;
23285 int remainder = 0;
23286 /* -1 means: do not use TENTHS. */
23287 int tenths = -1;
23288 int exponent = 0;
23290 /* Length of QUOTIENT.TENTHS as a string. */
23291 int length;
23293 char * psuffix;
23294 char * p;
23296 if (quotient >= 1000)
23298 /* Scale to the appropriate EXPONENT. */
23301 remainder = quotient % 1000;
23302 quotient /= 1000;
23303 exponent++;
23305 while (quotient >= 1000);
23307 /* Round to nearest and decide whether to use TENTHS or not. */
23308 if (quotient <= 9)
23310 tenths = remainder / 100;
23311 if (remainder % 100 >= 50)
23313 if (tenths < 9)
23314 tenths++;
23315 else
23317 quotient++;
23318 if (quotient == 10)
23319 tenths = -1;
23320 else
23321 tenths = 0;
23325 else
23326 if (remainder >= 500)
23328 if (quotient < 999)
23329 quotient++;
23330 else
23332 quotient = 1;
23333 exponent++;
23334 tenths = 0;
23339 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23340 if (tenths == -1 && quotient <= 99)
23341 if (quotient <= 9)
23342 length = 1;
23343 else
23344 length = 2;
23345 else
23346 length = 3;
23347 p = psuffix = buf + max (width, length);
23349 /* Print EXPONENT. */
23350 *psuffix++ = power_letter[exponent];
23351 *psuffix = '\0';
23353 /* Print TENTHS. */
23354 if (tenths >= 0)
23356 *--p = '0' + tenths;
23357 *--p = '.';
23360 /* Print QUOTIENT. */
23363 int digit = quotient % 10;
23364 *--p = '0' + digit;
23366 while ((quotient /= 10) != 0);
23368 /* Print leading spaces. */
23369 while (buf < p)
23370 *--p = ' ';
23373 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23374 If EOL_FLAG, set also a mnemonic character for end-of-line
23375 type of CODING_SYSTEM. Return updated pointer into BUF. */
23377 static unsigned char invalid_eol_type[] = "(*invalid*)";
23379 static char *
23380 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23382 Lisp_Object val;
23383 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23384 const unsigned char *eol_str;
23385 int eol_str_len;
23386 /* The EOL conversion we are using. */
23387 Lisp_Object eoltype;
23389 val = CODING_SYSTEM_SPEC (coding_system);
23390 eoltype = Qnil;
23392 if (!VECTORP (val)) /* Not yet decided. */
23394 *buf++ = multibyte ? '-' : ' ';
23395 if (eol_flag)
23396 eoltype = eol_mnemonic_undecided;
23397 /* Don't mention EOL conversion if it isn't decided. */
23399 else
23401 Lisp_Object attrs;
23402 Lisp_Object eolvalue;
23404 attrs = AREF (val, 0);
23405 eolvalue = AREF (val, 2);
23407 *buf++ = multibyte
23408 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23409 : ' ';
23411 if (eol_flag)
23413 /* The EOL conversion that is normal on this system. */
23415 if (NILP (eolvalue)) /* Not yet decided. */
23416 eoltype = eol_mnemonic_undecided;
23417 else if (VECTORP (eolvalue)) /* Not yet decided. */
23418 eoltype = eol_mnemonic_undecided;
23419 else /* eolvalue is Qunix, Qdos, or Qmac. */
23420 eoltype = (EQ (eolvalue, Qunix)
23421 ? eol_mnemonic_unix
23422 : EQ (eolvalue, Qdos)
23423 ? eol_mnemonic_dos : eol_mnemonic_mac);
23427 if (eol_flag)
23429 /* Mention the EOL conversion if it is not the usual one. */
23430 if (STRINGP (eoltype))
23432 eol_str = SDATA (eoltype);
23433 eol_str_len = SBYTES (eoltype);
23435 else if (CHARACTERP (eoltype))
23437 int c = XFASTINT (eoltype);
23438 return buf + CHAR_STRING (c, (unsigned char *) buf);
23440 else
23442 eol_str = invalid_eol_type;
23443 eol_str_len = sizeof (invalid_eol_type) - 1;
23445 memcpy (buf, eol_str, eol_str_len);
23446 buf += eol_str_len;
23449 return buf;
23452 /* Return the approximate percentage N is of D (rounding upward), or 99,
23453 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23455 static int
23456 percent99 (ptrdiff_t n, ptrdiff_t d)
23458 int percent = (d - 1 + 100.0 * n) / d;
23459 return min (percent, 99);
23462 /* Return a string for the output of a mode line %-spec for window W,
23463 generated by character C. FIELD_WIDTH > 0 means pad the string
23464 returned with spaces to that value. Return a Lisp string in
23465 *STRING if the resulting string is taken from that Lisp string.
23467 Note we operate on the current buffer for most purposes. */
23469 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23471 static const char *
23472 decode_mode_spec (struct window *w, register int c, int field_width,
23473 Lisp_Object *string)
23475 Lisp_Object obj;
23476 struct frame *f = XFRAME (WINDOW_FRAME (w));
23477 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23478 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23479 produce strings from numerical values, so limit preposterously
23480 large values of FIELD_WIDTH to avoid overrunning the buffer's
23481 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23482 bytes plus the terminating null. */
23483 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23484 struct buffer *b = current_buffer;
23486 obj = Qnil;
23487 *string = Qnil;
23489 switch (c)
23491 case '*':
23492 if (!NILP (BVAR (b, read_only)))
23493 return "%";
23494 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23495 return "*";
23496 return "-";
23498 case '+':
23499 /* This differs from %* only for a modified read-only buffer. */
23500 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23501 return "*";
23502 if (!NILP (BVAR (b, read_only)))
23503 return "%";
23504 return "-";
23506 case '&':
23507 /* This differs from %* in ignoring read-only-ness. */
23508 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23509 return "*";
23510 return "-";
23512 case '%':
23513 return "%";
23515 case '[':
23517 int i;
23518 char *p;
23520 if (command_loop_level > 5)
23521 return "[[[... ";
23522 p = decode_mode_spec_buf;
23523 for (i = 0; i < command_loop_level; i++)
23524 *p++ = '[';
23525 *p = 0;
23526 return decode_mode_spec_buf;
23529 case ']':
23531 int i;
23532 char *p;
23534 if (command_loop_level > 5)
23535 return " ...]]]";
23536 p = decode_mode_spec_buf;
23537 for (i = 0; i < command_loop_level; i++)
23538 *p++ = ']';
23539 *p = 0;
23540 return decode_mode_spec_buf;
23543 case '-':
23545 register int i;
23547 /* Let lots_of_dashes be a string of infinite length. */
23548 if (mode_line_target == MODE_LINE_NOPROP
23549 || mode_line_target == MODE_LINE_STRING)
23550 return "--";
23551 if (field_width <= 0
23552 || field_width > sizeof (lots_of_dashes))
23554 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23555 decode_mode_spec_buf[i] = '-';
23556 decode_mode_spec_buf[i] = '\0';
23557 return decode_mode_spec_buf;
23559 else
23560 return lots_of_dashes;
23563 case 'b':
23564 obj = BVAR (b, name);
23565 break;
23567 case 'c':
23568 /* %c and %l are ignored in `frame-title-format'.
23569 (In redisplay_internal, the frame title is drawn _before_ the
23570 windows are updated, so the stuff which depends on actual
23571 window contents (such as %l) may fail to render properly, or
23572 even crash emacs.) */
23573 if (mode_line_target == MODE_LINE_TITLE)
23574 return "";
23575 else
23577 ptrdiff_t col = current_column ();
23578 w->column_number_displayed = col;
23579 pint2str (decode_mode_spec_buf, width, col);
23580 return decode_mode_spec_buf;
23583 case 'e':
23584 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23586 if (NILP (Vmemory_full))
23587 return "";
23588 else
23589 return "!MEM FULL! ";
23591 #else
23592 return "";
23593 #endif
23595 case 'F':
23596 /* %F displays the frame name. */
23597 if (!NILP (f->title))
23598 return SSDATA (f->title);
23599 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23600 return SSDATA (f->name);
23601 return "Emacs";
23603 case 'f':
23604 obj = BVAR (b, filename);
23605 break;
23607 case 'i':
23609 ptrdiff_t size = ZV - BEGV;
23610 pint2str (decode_mode_spec_buf, width, size);
23611 return decode_mode_spec_buf;
23614 case 'I':
23616 ptrdiff_t size = ZV - BEGV;
23617 pint2hrstr (decode_mode_spec_buf, width, size);
23618 return decode_mode_spec_buf;
23621 case 'l':
23623 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23624 ptrdiff_t topline, nlines, height;
23625 ptrdiff_t junk;
23627 /* %c and %l are ignored in `frame-title-format'. */
23628 if (mode_line_target == MODE_LINE_TITLE)
23629 return "";
23631 startpos = marker_position (w->start);
23632 startpos_byte = marker_byte_position (w->start);
23633 height = WINDOW_TOTAL_LINES (w);
23635 /* If we decided that this buffer isn't suitable for line numbers,
23636 don't forget that too fast. */
23637 if (w->base_line_pos == -1)
23638 goto no_value;
23640 /* If the buffer is very big, don't waste time. */
23641 if (INTEGERP (Vline_number_display_limit)
23642 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23644 w->base_line_pos = 0;
23645 w->base_line_number = 0;
23646 goto no_value;
23649 if (w->base_line_number > 0
23650 && w->base_line_pos > 0
23651 && w->base_line_pos <= startpos)
23653 line = w->base_line_number;
23654 linepos = w->base_line_pos;
23655 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23657 else
23659 line = 1;
23660 linepos = BUF_BEGV (b);
23661 linepos_byte = BUF_BEGV_BYTE (b);
23664 /* Count lines from base line to window start position. */
23665 nlines = display_count_lines (linepos_byte,
23666 startpos_byte,
23667 startpos, &junk);
23669 topline = nlines + line;
23671 /* Determine a new base line, if the old one is too close
23672 or too far away, or if we did not have one.
23673 "Too close" means it's plausible a scroll-down would
23674 go back past it. */
23675 if (startpos == BUF_BEGV (b))
23677 w->base_line_number = topline;
23678 w->base_line_pos = BUF_BEGV (b);
23680 else if (nlines < height + 25 || nlines > height * 3 + 50
23681 || linepos == BUF_BEGV (b))
23683 ptrdiff_t limit = BUF_BEGV (b);
23684 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23685 ptrdiff_t position;
23686 ptrdiff_t distance =
23687 (height * 2 + 30) * line_number_display_limit_width;
23689 if (startpos - distance > limit)
23691 limit = startpos - distance;
23692 limit_byte = CHAR_TO_BYTE (limit);
23695 nlines = display_count_lines (startpos_byte,
23696 limit_byte,
23697 - (height * 2 + 30),
23698 &position);
23699 /* If we couldn't find the lines we wanted within
23700 line_number_display_limit_width chars per line,
23701 give up on line numbers for this window. */
23702 if (position == limit_byte && limit == startpos - distance)
23704 w->base_line_pos = -1;
23705 w->base_line_number = 0;
23706 goto no_value;
23709 w->base_line_number = topline - nlines;
23710 w->base_line_pos = BYTE_TO_CHAR (position);
23713 /* Now count lines from the start pos to point. */
23714 nlines = display_count_lines (startpos_byte,
23715 PT_BYTE, PT, &junk);
23717 /* Record that we did display the line number. */
23718 line_number_displayed = true;
23720 /* Make the string to show. */
23721 pint2str (decode_mode_spec_buf, width, topline + nlines);
23722 return decode_mode_spec_buf;
23723 no_value:
23725 char *p = decode_mode_spec_buf;
23726 int pad = width - 2;
23727 while (pad-- > 0)
23728 *p++ = ' ';
23729 *p++ = '?';
23730 *p++ = '?';
23731 *p = '\0';
23732 return decode_mode_spec_buf;
23735 break;
23737 case 'm':
23738 obj = BVAR (b, mode_name);
23739 break;
23741 case 'n':
23742 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23743 return " Narrow";
23744 break;
23746 case 'p':
23748 ptrdiff_t pos = marker_position (w->start);
23749 ptrdiff_t begv = BUF_BEGV (b);
23750 ptrdiff_t zv = BUF_ZV (b);
23752 if (w->window_end_pos <= BUF_Z (b) - zv)
23753 return pos <= begv ? "All" : "Bottom";
23754 else if (pos <= begv)
23755 return "Top";
23756 else
23758 sprintf (decode_mode_spec_buf, "%2d%%",
23759 percent99 (pos - begv, zv - begv));
23760 return decode_mode_spec_buf;
23764 /* Display percentage of size above the bottom of the screen. */
23765 case 'P':
23767 ptrdiff_t toppos = marker_position (w->start);
23768 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23769 ptrdiff_t begv = BUF_BEGV (b);
23770 ptrdiff_t zv = BUF_ZV (b);
23772 if (zv <= botpos)
23773 return toppos <= begv ? "All" : "Bottom";
23774 else
23776 sprintf (decode_mode_spec_buf,
23777 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
23778 percent99 (botpos - begv, zv - begv));
23779 return decode_mode_spec_buf;
23783 case 's':
23784 /* status of process */
23785 obj = Fget_buffer_process (Fcurrent_buffer ());
23786 if (NILP (obj))
23787 return "no process";
23788 #ifndef MSDOS
23789 obj = Fsymbol_name (Fprocess_status (obj));
23790 #endif
23791 break;
23793 case '@':
23795 ptrdiff_t count = inhibit_garbage_collection ();
23796 Lisp_Object curdir = BVAR (current_buffer, directory);
23797 Lisp_Object val = Qnil;
23799 if (STRINGP (curdir))
23800 val = call1 (intern ("file-remote-p"), curdir);
23802 unbind_to (count, Qnil);
23804 if (NILP (val))
23805 return "-";
23806 else
23807 return "@";
23810 case 'z':
23811 /* coding-system (not including end-of-line format) */
23812 case 'Z':
23813 /* coding-system (including end-of-line type) */
23815 bool eol_flag = (c == 'Z');
23816 char *p = decode_mode_spec_buf;
23818 if (! FRAME_WINDOW_P (f))
23820 /* No need to mention EOL here--the terminal never needs
23821 to do EOL conversion. */
23822 p = decode_mode_spec_coding (CODING_ID_NAME
23823 (FRAME_KEYBOARD_CODING (f)->id),
23824 p, false);
23825 p = decode_mode_spec_coding (CODING_ID_NAME
23826 (FRAME_TERMINAL_CODING (f)->id),
23827 p, false);
23829 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23830 p, eol_flag);
23832 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23833 #ifdef subprocesses
23834 obj = Fget_buffer_process (Fcurrent_buffer ());
23835 if (PROCESSP (obj))
23837 p = decode_mode_spec_coding
23838 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23839 p = decode_mode_spec_coding
23840 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23842 #endif /* subprocesses */
23843 #endif /* false */
23844 *p = 0;
23845 return decode_mode_spec_buf;
23849 if (STRINGP (obj))
23851 *string = obj;
23852 return SSDATA (obj);
23854 else
23855 return "";
23859 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23860 means count lines back from START_BYTE. But don't go beyond
23861 LIMIT_BYTE. Return the number of lines thus found (always
23862 nonnegative).
23864 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23865 either the position COUNT lines after/before START_BYTE, if we
23866 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23867 COUNT lines. */
23869 static ptrdiff_t
23870 display_count_lines (ptrdiff_t start_byte,
23871 ptrdiff_t limit_byte, ptrdiff_t count,
23872 ptrdiff_t *byte_pos_ptr)
23874 register unsigned char *cursor;
23875 unsigned char *base;
23877 register ptrdiff_t ceiling;
23878 register unsigned char *ceiling_addr;
23879 ptrdiff_t orig_count = count;
23881 /* If we are not in selective display mode,
23882 check only for newlines. */
23883 bool selective_display
23884 = (!NILP (BVAR (current_buffer, selective_display))
23885 && !INTEGERP (BVAR (current_buffer, selective_display)));
23887 if (count > 0)
23889 while (start_byte < limit_byte)
23891 ceiling = BUFFER_CEILING_OF (start_byte);
23892 ceiling = min (limit_byte - 1, ceiling);
23893 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23894 base = (cursor = BYTE_POS_ADDR (start_byte));
23898 if (selective_display)
23900 while (*cursor != '\n' && *cursor != 015
23901 && ++cursor != ceiling_addr)
23902 continue;
23903 if (cursor == ceiling_addr)
23904 break;
23906 else
23908 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23909 if (! cursor)
23910 break;
23913 cursor++;
23915 if (--count == 0)
23917 start_byte += cursor - base;
23918 *byte_pos_ptr = start_byte;
23919 return orig_count;
23922 while (cursor < ceiling_addr);
23924 start_byte += ceiling_addr - base;
23927 else
23929 while (start_byte > limit_byte)
23931 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23932 ceiling = max (limit_byte, ceiling);
23933 ceiling_addr = BYTE_POS_ADDR (ceiling);
23934 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23935 while (true)
23937 if (selective_display)
23939 while (--cursor >= ceiling_addr
23940 && *cursor != '\n' && *cursor != 015)
23941 continue;
23942 if (cursor < ceiling_addr)
23943 break;
23945 else
23947 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23948 if (! cursor)
23949 break;
23952 if (++count == 0)
23954 start_byte += cursor - base + 1;
23955 *byte_pos_ptr = start_byte;
23956 /* When scanning backwards, we should
23957 not count the newline posterior to which we stop. */
23958 return - orig_count - 1;
23961 start_byte += ceiling_addr - base;
23965 *byte_pos_ptr = limit_byte;
23967 if (count < 0)
23968 return - orig_count + count;
23969 return orig_count - count;
23975 /***********************************************************************
23976 Displaying strings
23977 ***********************************************************************/
23979 /* Display a NUL-terminated string, starting with index START.
23981 If STRING is non-null, display that C string. Otherwise, the Lisp
23982 string LISP_STRING is displayed. There's a case that STRING is
23983 non-null and LISP_STRING is not nil. It means STRING is a string
23984 data of LISP_STRING. In that case, we display LISP_STRING while
23985 ignoring its text properties.
23987 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23988 FACE_STRING. Display STRING or LISP_STRING with the face at
23989 FACE_STRING_POS in FACE_STRING:
23991 Display the string in the environment given by IT, but use the
23992 standard display table, temporarily.
23994 FIELD_WIDTH is the minimum number of output glyphs to produce.
23995 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23996 with spaces. If STRING has more characters, more than FIELD_WIDTH
23997 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23999 PRECISION is the maximum number of characters to output from
24000 STRING. PRECISION < 0 means don't truncate the string.
24002 This is roughly equivalent to printf format specifiers:
24004 FIELD_WIDTH PRECISION PRINTF
24005 ----------------------------------------
24006 -1 -1 %s
24007 -1 10 %.10s
24008 10 -1 %10s
24009 20 10 %20.10s
24011 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24012 display them, and < 0 means obey the current buffer's value of
24013 enable_multibyte_characters.
24015 Value is the number of columns displayed. */
24017 static int
24018 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24019 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24020 int field_width, int precision, int max_x, int multibyte)
24022 int hpos_at_start = it->hpos;
24023 int saved_face_id = it->face_id;
24024 struct glyph_row *row = it->glyph_row;
24025 ptrdiff_t it_charpos;
24027 /* Initialize the iterator IT for iteration over STRING beginning
24028 with index START. */
24029 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24030 precision, field_width, multibyte);
24031 if (string && STRINGP (lisp_string))
24032 /* LISP_STRING is the one returned by decode_mode_spec. We should
24033 ignore its text properties. */
24034 it->stop_charpos = it->end_charpos;
24036 /* If displaying STRING, set up the face of the iterator from
24037 FACE_STRING, if that's given. */
24038 if (STRINGP (face_string))
24040 ptrdiff_t endptr;
24041 struct face *face;
24043 it->face_id
24044 = face_at_string_position (it->w, face_string, face_string_pos,
24045 0, &endptr, it->base_face_id, false);
24046 face = FACE_FROM_ID (it->f, it->face_id);
24047 it->face_box_p = face->box != FACE_NO_BOX;
24050 /* Set max_x to the maximum allowed X position. Don't let it go
24051 beyond the right edge of the window. */
24052 if (max_x <= 0)
24053 max_x = it->last_visible_x;
24054 else
24055 max_x = min (max_x, it->last_visible_x);
24057 /* Skip over display elements that are not visible. because IT->w is
24058 hscrolled. */
24059 if (it->current_x < it->first_visible_x)
24060 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24061 MOVE_TO_POS | MOVE_TO_X);
24063 row->ascent = it->max_ascent;
24064 row->height = it->max_ascent + it->max_descent;
24065 row->phys_ascent = it->max_phys_ascent;
24066 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24067 row->extra_line_spacing = it->max_extra_line_spacing;
24069 if (STRINGP (it->string))
24070 it_charpos = IT_STRING_CHARPOS (*it);
24071 else
24072 it_charpos = IT_CHARPOS (*it);
24074 /* This condition is for the case that we are called with current_x
24075 past last_visible_x. */
24076 while (it->current_x < max_x)
24078 int x_before, x, n_glyphs_before, i, nglyphs;
24080 /* Get the next display element. */
24081 if (!get_next_display_element (it))
24082 break;
24084 /* Produce glyphs. */
24085 x_before = it->current_x;
24086 n_glyphs_before = row->used[TEXT_AREA];
24087 PRODUCE_GLYPHS (it);
24089 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24090 i = 0;
24091 x = x_before;
24092 while (i < nglyphs)
24094 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24096 if (it->line_wrap != TRUNCATE
24097 && x + glyph->pixel_width > max_x)
24099 /* End of continued line or max_x reached. */
24100 if (CHAR_GLYPH_PADDING_P (*glyph))
24102 /* A wide character is unbreakable. */
24103 if (row->reversed_p)
24104 unproduce_glyphs (it, row->used[TEXT_AREA]
24105 - n_glyphs_before);
24106 row->used[TEXT_AREA] = n_glyphs_before;
24107 it->current_x = x_before;
24109 else
24111 if (row->reversed_p)
24112 unproduce_glyphs (it, row->used[TEXT_AREA]
24113 - (n_glyphs_before + i));
24114 row->used[TEXT_AREA] = n_glyphs_before + i;
24115 it->current_x = x;
24117 break;
24119 else if (x + glyph->pixel_width >= it->first_visible_x)
24121 /* Glyph is at least partially visible. */
24122 ++it->hpos;
24123 if (x < it->first_visible_x)
24124 row->x = x - it->first_visible_x;
24126 else
24128 /* Glyph is off the left margin of the display area.
24129 Should not happen. */
24130 emacs_abort ();
24133 row->ascent = max (row->ascent, it->max_ascent);
24134 row->height = max (row->height, it->max_ascent + it->max_descent);
24135 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24136 row->phys_height = max (row->phys_height,
24137 it->max_phys_ascent + it->max_phys_descent);
24138 row->extra_line_spacing = max (row->extra_line_spacing,
24139 it->max_extra_line_spacing);
24140 x += glyph->pixel_width;
24141 ++i;
24144 /* Stop if max_x reached. */
24145 if (i < nglyphs)
24146 break;
24148 /* Stop at line ends. */
24149 if (ITERATOR_AT_END_OF_LINE_P (it))
24151 it->continuation_lines_width = 0;
24152 break;
24155 set_iterator_to_next (it, true);
24156 if (STRINGP (it->string))
24157 it_charpos = IT_STRING_CHARPOS (*it);
24158 else
24159 it_charpos = IT_CHARPOS (*it);
24161 /* Stop if truncating at the right edge. */
24162 if (it->line_wrap == TRUNCATE
24163 && it->current_x >= it->last_visible_x)
24165 /* Add truncation mark, but don't do it if the line is
24166 truncated at a padding space. */
24167 if (it_charpos < it->string_nchars)
24169 if (!FRAME_WINDOW_P (it->f))
24171 int ii, n;
24173 if (it->current_x > it->last_visible_x)
24175 if (!row->reversed_p)
24177 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24178 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24179 break;
24181 else
24183 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24184 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24185 break;
24186 unproduce_glyphs (it, ii + 1);
24187 ii = row->used[TEXT_AREA] - (ii + 1);
24189 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24191 row->used[TEXT_AREA] = ii;
24192 produce_special_glyphs (it, IT_TRUNCATION);
24195 produce_special_glyphs (it, IT_TRUNCATION);
24197 row->truncated_on_right_p = true;
24199 break;
24203 /* Maybe insert a truncation at the left. */
24204 if (it->first_visible_x
24205 && it_charpos > 0)
24207 if (!FRAME_WINDOW_P (it->f)
24208 || (row->reversed_p
24209 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24210 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24211 insert_left_trunc_glyphs (it);
24212 row->truncated_on_left_p = true;
24215 it->face_id = saved_face_id;
24217 /* Value is number of columns displayed. */
24218 return it->hpos - hpos_at_start;
24223 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24224 appears as an element of LIST or as the car of an element of LIST.
24225 If PROPVAL is a list, compare each element against LIST in that
24226 way, and return 1/2 if any element of PROPVAL is found in LIST.
24227 Otherwise return 0. This function cannot quit.
24228 The return value is 2 if the text is invisible but with an ellipsis
24229 and 1 if it's invisible and without an ellipsis. */
24232 invisible_prop (Lisp_Object propval, Lisp_Object list)
24234 Lisp_Object tail, proptail;
24236 for (tail = list; CONSP (tail); tail = XCDR (tail))
24238 register Lisp_Object tem;
24239 tem = XCAR (tail);
24240 if (EQ (propval, tem))
24241 return 1;
24242 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24243 return NILP (XCDR (tem)) ? 1 : 2;
24246 if (CONSP (propval))
24248 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24250 Lisp_Object propelt;
24251 propelt = XCAR (proptail);
24252 for (tail = list; CONSP (tail); tail = XCDR (tail))
24254 register Lisp_Object tem;
24255 tem = XCAR (tail);
24256 if (EQ (propelt, tem))
24257 return 1;
24258 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24259 return NILP (XCDR (tem)) ? 1 : 2;
24264 return 0;
24267 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24268 doc: /* Non-nil if the property makes the text invisible.
24269 POS-OR-PROP can be a marker or number, in which case it is taken to be
24270 a position in the current buffer and the value of the `invisible' property
24271 is checked; or it can be some other value, which is then presumed to be the
24272 value of the `invisible' property of the text of interest.
24273 The non-nil value returned can be t for truly invisible text or something
24274 else if the text is replaced by an ellipsis. */)
24275 (Lisp_Object pos_or_prop)
24277 Lisp_Object prop
24278 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24279 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24280 : pos_or_prop);
24281 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24282 return (invis == 0 ? Qnil
24283 : invis == 1 ? Qt
24284 : make_number (invis));
24287 /* Calculate a width or height in pixels from a specification using
24288 the following elements:
24290 SPEC ::=
24291 NUM - a (fractional) multiple of the default font width/height
24292 (NUM) - specifies exactly NUM pixels
24293 UNIT - a fixed number of pixels, see below.
24294 ELEMENT - size of a display element in pixels, see below.
24295 (NUM . SPEC) - equals NUM * SPEC
24296 (+ SPEC SPEC ...) - add pixel values
24297 (- SPEC SPEC ...) - subtract pixel values
24298 (- SPEC) - negate pixel value
24300 NUM ::=
24301 INT or FLOAT - a number constant
24302 SYMBOL - use symbol's (buffer local) variable binding.
24304 UNIT ::=
24305 in - pixels per inch *)
24306 mm - pixels per 1/1000 meter *)
24307 cm - pixels per 1/100 meter *)
24308 width - width of current font in pixels.
24309 height - height of current font in pixels.
24311 *) using the ratio(s) defined in display-pixels-per-inch.
24313 ELEMENT ::=
24315 left-fringe - left fringe width in pixels
24316 right-fringe - right fringe width in pixels
24318 left-margin - left margin width in pixels
24319 right-margin - right margin width in pixels
24321 scroll-bar - scroll-bar area width in pixels
24323 Examples:
24325 Pixels corresponding to 5 inches:
24326 (5 . in)
24328 Total width of non-text areas on left side of window (if scroll-bar is on left):
24329 '(space :width (+ left-fringe left-margin scroll-bar))
24331 Align to first text column (in header line):
24332 '(space :align-to 0)
24334 Align to middle of text area minus half the width of variable `my-image'
24335 containing a loaded image:
24336 '(space :align-to (0.5 . (- text my-image)))
24338 Width of left margin minus width of 1 character in the default font:
24339 '(space :width (- left-margin 1))
24341 Width of left margin minus width of 2 characters in the current font:
24342 '(space :width (- left-margin (2 . width)))
24344 Center 1 character over left-margin (in header line):
24345 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24347 Different ways to express width of left fringe plus left margin minus one pixel:
24348 '(space :width (- (+ left-fringe left-margin) (1)))
24349 '(space :width (+ left-fringe left-margin (- (1))))
24350 '(space :width (+ left-fringe left-margin (-1)))
24354 static bool
24355 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24356 struct font *font, bool width_p, int *align_to)
24358 double pixels;
24360 # define OK_PIXELS(val) (*res = (val), true)
24361 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24363 if (NILP (prop))
24364 return OK_PIXELS (0);
24366 eassert (FRAME_LIVE_P (it->f));
24368 if (SYMBOLP (prop))
24370 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24372 char *unit = SSDATA (SYMBOL_NAME (prop));
24374 if (unit[0] == 'i' && unit[1] == 'n')
24375 pixels = 1.0;
24376 else if (unit[0] == 'm' && unit[1] == 'm')
24377 pixels = 25.4;
24378 else if (unit[0] == 'c' && unit[1] == 'm')
24379 pixels = 2.54;
24380 else
24381 pixels = 0;
24382 if (pixels > 0)
24384 double ppi = (width_p ? FRAME_RES_X (it->f)
24385 : FRAME_RES_Y (it->f));
24387 if (ppi > 0)
24388 return OK_PIXELS (ppi / pixels);
24389 return false;
24393 #ifdef HAVE_WINDOW_SYSTEM
24394 if (EQ (prop, Qheight))
24395 return OK_PIXELS (font
24396 ? normal_char_height (font, -1)
24397 : FRAME_LINE_HEIGHT (it->f));
24398 if (EQ (prop, Qwidth))
24399 return OK_PIXELS (font
24400 ? FONT_WIDTH (font)
24401 : FRAME_COLUMN_WIDTH (it->f));
24402 #else
24403 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24404 return OK_PIXELS (1);
24405 #endif
24407 if (EQ (prop, Qtext))
24408 return OK_PIXELS (width_p
24409 ? window_box_width (it->w, TEXT_AREA)
24410 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24412 if (align_to && *align_to < 0)
24414 *res = 0;
24415 if (EQ (prop, Qleft))
24416 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24417 if (EQ (prop, Qright))
24418 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24419 if (EQ (prop, Qcenter))
24420 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24421 + window_box_width (it->w, TEXT_AREA) / 2);
24422 if (EQ (prop, Qleft_fringe))
24423 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24424 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24425 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24426 if (EQ (prop, Qright_fringe))
24427 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24428 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24429 : window_box_right_offset (it->w, TEXT_AREA));
24430 if (EQ (prop, Qleft_margin))
24431 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24432 if (EQ (prop, Qright_margin))
24433 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24434 if (EQ (prop, Qscroll_bar))
24435 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24437 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24438 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24439 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24440 : 0)));
24442 else
24444 if (EQ (prop, Qleft_fringe))
24445 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24446 if (EQ (prop, Qright_fringe))
24447 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24448 if (EQ (prop, Qleft_margin))
24449 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24450 if (EQ (prop, Qright_margin))
24451 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24452 if (EQ (prop, Qscroll_bar))
24453 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24456 prop = buffer_local_value (prop, it->w->contents);
24457 if (EQ (prop, Qunbound))
24458 prop = Qnil;
24461 if (NUMBERP (prop))
24463 int base_unit = (width_p
24464 ? FRAME_COLUMN_WIDTH (it->f)
24465 : FRAME_LINE_HEIGHT (it->f));
24466 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24469 if (CONSP (prop))
24471 Lisp_Object car = XCAR (prop);
24472 Lisp_Object cdr = XCDR (prop);
24474 if (SYMBOLP (car))
24476 #ifdef HAVE_WINDOW_SYSTEM
24477 if (FRAME_WINDOW_P (it->f)
24478 && valid_image_p (prop))
24480 ptrdiff_t id = lookup_image (it->f, prop);
24481 struct image *img = IMAGE_FROM_ID (it->f, id);
24483 return OK_PIXELS (width_p ? img->width : img->height);
24485 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24487 // TODO: Don't return dummy size.
24488 return OK_PIXELS (100);
24490 #endif
24491 if (EQ (car, Qplus) || EQ (car, Qminus))
24493 bool first = true;
24494 double px;
24496 pixels = 0;
24497 while (CONSP (cdr))
24499 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24500 font, width_p, align_to))
24501 return false;
24502 if (first)
24503 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24504 else
24505 pixels += px;
24506 cdr = XCDR (cdr);
24508 if (EQ (car, Qminus))
24509 pixels = -pixels;
24510 return OK_PIXELS (pixels);
24513 car = buffer_local_value (car, it->w->contents);
24514 if (EQ (car, Qunbound))
24515 car = Qnil;
24518 if (NUMBERP (car))
24520 double fact;
24521 pixels = XFLOATINT (car);
24522 if (NILP (cdr))
24523 return OK_PIXELS (pixels);
24524 if (calc_pixel_width_or_height (&fact, it, cdr,
24525 font, width_p, align_to))
24526 return OK_PIXELS (pixels * fact);
24527 return false;
24530 return false;
24533 return false;
24536 void
24537 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24539 #ifdef HAVE_WINDOW_SYSTEM
24540 normal_char_ascent_descent (font, -1, ascent, descent);
24541 #else
24542 *ascent = 1;
24543 *descent = 0;
24544 #endif
24548 /***********************************************************************
24549 Glyph Display
24550 ***********************************************************************/
24552 #ifdef HAVE_WINDOW_SYSTEM
24554 #ifdef GLYPH_DEBUG
24556 void
24557 dump_glyph_string (struct glyph_string *s)
24559 fprintf (stderr, "glyph string\n");
24560 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24561 s->x, s->y, s->width, s->height);
24562 fprintf (stderr, " ybase = %d\n", s->ybase);
24563 fprintf (stderr, " hl = %d\n", s->hl);
24564 fprintf (stderr, " left overhang = %d, right = %d\n",
24565 s->left_overhang, s->right_overhang);
24566 fprintf (stderr, " nchars = %d\n", s->nchars);
24567 fprintf (stderr, " extends to end of line = %d\n",
24568 s->extends_to_end_of_line_p);
24569 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24570 fprintf (stderr, " bg width = %d\n", s->background_width);
24573 #endif /* GLYPH_DEBUG */
24575 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24576 of XChar2b structures for S; it can't be allocated in
24577 init_glyph_string because it must be allocated via `alloca'. W
24578 is the window on which S is drawn. ROW and AREA are the glyph row
24579 and area within the row from which S is constructed. START is the
24580 index of the first glyph structure covered by S. HL is a
24581 face-override for drawing S. */
24583 #ifdef HAVE_NTGUI
24584 #define OPTIONAL_HDC(hdc) HDC hdc,
24585 #define DECLARE_HDC(hdc) HDC hdc;
24586 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24587 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24588 #endif
24590 #ifndef OPTIONAL_HDC
24591 #define OPTIONAL_HDC(hdc)
24592 #define DECLARE_HDC(hdc)
24593 #define ALLOCATE_HDC(hdc, f)
24594 #define RELEASE_HDC(hdc, f)
24595 #endif
24597 static void
24598 init_glyph_string (struct glyph_string *s,
24599 OPTIONAL_HDC (hdc)
24600 XChar2b *char2b, struct window *w, struct glyph_row *row,
24601 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24603 memset (s, 0, sizeof *s);
24604 s->w = w;
24605 s->f = XFRAME (w->frame);
24606 #ifdef HAVE_NTGUI
24607 s->hdc = hdc;
24608 #endif
24609 s->display = FRAME_X_DISPLAY (s->f);
24610 s->window = FRAME_X_WINDOW (s->f);
24611 s->char2b = char2b;
24612 s->hl = hl;
24613 s->row = row;
24614 s->area = area;
24615 s->first_glyph = row->glyphs[area] + start;
24616 s->height = row->height;
24617 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24618 s->ybase = s->y + row->ascent;
24622 /* Append the list of glyph strings with head H and tail T to the list
24623 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24625 static void
24626 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24627 struct glyph_string *h, struct glyph_string *t)
24629 if (h)
24631 if (*head)
24632 (*tail)->next = h;
24633 else
24634 *head = h;
24635 h->prev = *tail;
24636 *tail = t;
24641 /* Prepend the list of glyph strings with head H and tail T to the
24642 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24643 result. */
24645 static void
24646 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24647 struct glyph_string *h, struct glyph_string *t)
24649 if (h)
24651 if (*head)
24652 (*head)->prev = t;
24653 else
24654 *tail = t;
24655 t->next = *head;
24656 *head = h;
24661 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24662 Set *HEAD and *TAIL to the resulting list. */
24664 static void
24665 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24666 struct glyph_string *s)
24668 s->next = s->prev = NULL;
24669 append_glyph_string_lists (head, tail, s, s);
24673 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24674 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24675 make sure that X resources for the face returned are allocated.
24676 Value is a pointer to a realized face that is ready for display if
24677 DISPLAY_P. */
24679 static struct face *
24680 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24681 XChar2b *char2b, bool display_p)
24683 struct face *face = FACE_FROM_ID (f, face_id);
24684 unsigned code = 0;
24686 if (face->font)
24688 code = face->font->driver->encode_char (face->font, c);
24690 if (code == FONT_INVALID_CODE)
24691 code = 0;
24693 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24695 /* Make sure X resources of the face are allocated. */
24696 #ifdef HAVE_X_WINDOWS
24697 if (display_p)
24698 #endif
24700 eassert (face != NULL);
24701 prepare_face_for_display (f, face);
24704 return face;
24708 /* Get face and two-byte form of character glyph GLYPH on frame F.
24709 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24710 a pointer to a realized face that is ready for display. */
24712 static struct face *
24713 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24714 XChar2b *char2b)
24716 struct face *face;
24717 unsigned code = 0;
24719 eassert (glyph->type == CHAR_GLYPH);
24720 face = FACE_FROM_ID (f, glyph->face_id);
24722 /* Make sure X resources of the face are allocated. */
24723 prepare_face_for_display (f, face);
24725 if (face->font)
24727 if (CHAR_BYTE8_P (glyph->u.ch))
24728 code = CHAR_TO_BYTE8 (glyph->u.ch);
24729 else
24730 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24732 if (code == FONT_INVALID_CODE)
24733 code = 0;
24736 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24737 return face;
24741 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24742 Return true iff FONT has a glyph for C. */
24744 static bool
24745 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24747 unsigned code;
24749 if (CHAR_BYTE8_P (c))
24750 code = CHAR_TO_BYTE8 (c);
24751 else
24752 code = font->driver->encode_char (font, c);
24754 if (code == FONT_INVALID_CODE)
24755 return false;
24756 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24757 return true;
24761 /* Fill glyph string S with composition components specified by S->cmp.
24763 BASE_FACE is the base face of the composition.
24764 S->cmp_from is the index of the first component for S.
24766 OVERLAPS non-zero means S should draw the foreground only, and use
24767 its physical height for clipping. See also draw_glyphs.
24769 Value is the index of a component not in S. */
24771 static int
24772 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24773 int overlaps)
24775 int i;
24776 /* For all glyphs of this composition, starting at the offset
24777 S->cmp_from, until we reach the end of the definition or encounter a
24778 glyph that requires the different face, add it to S. */
24779 struct face *face;
24781 eassert (s);
24783 s->for_overlaps = overlaps;
24784 s->face = NULL;
24785 s->font = NULL;
24786 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24788 int c = COMPOSITION_GLYPH (s->cmp, i);
24790 /* TAB in a composition means display glyphs with padding space
24791 on the left or right. */
24792 if (c != '\t')
24794 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24795 -1, Qnil);
24797 face = get_char_face_and_encoding (s->f, c, face_id,
24798 s->char2b + i, true);
24799 if (face)
24801 if (! s->face)
24803 s->face = face;
24804 s->font = s->face->font;
24806 else if (s->face != face)
24807 break;
24810 ++s->nchars;
24812 s->cmp_to = i;
24814 if (s->face == NULL)
24816 s->face = base_face->ascii_face;
24817 s->font = s->face->font;
24820 /* All glyph strings for the same composition has the same width,
24821 i.e. the width set for the first component of the composition. */
24822 s->width = s->first_glyph->pixel_width;
24824 /* If the specified font could not be loaded, use the frame's
24825 default font, but record the fact that we couldn't load it in
24826 the glyph string so that we can draw rectangles for the
24827 characters of the glyph string. */
24828 if (s->font == NULL)
24830 s->font_not_found_p = true;
24831 s->font = FRAME_FONT (s->f);
24834 /* Adjust base line for subscript/superscript text. */
24835 s->ybase += s->first_glyph->voffset;
24837 return s->cmp_to;
24840 static int
24841 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24842 int start, int end, int overlaps)
24844 struct glyph *glyph, *last;
24845 Lisp_Object lgstring;
24846 int i;
24848 s->for_overlaps = overlaps;
24849 glyph = s->row->glyphs[s->area] + start;
24850 last = s->row->glyphs[s->area] + end;
24851 s->cmp_id = glyph->u.cmp.id;
24852 s->cmp_from = glyph->slice.cmp.from;
24853 s->cmp_to = glyph->slice.cmp.to + 1;
24854 s->face = FACE_FROM_ID (s->f, face_id);
24855 lgstring = composition_gstring_from_id (s->cmp_id);
24856 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24857 glyph++;
24858 while (glyph < last
24859 && glyph->u.cmp.automatic
24860 && glyph->u.cmp.id == s->cmp_id
24861 && s->cmp_to == glyph->slice.cmp.from)
24862 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24864 for (i = s->cmp_from; i < s->cmp_to; i++)
24866 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24867 unsigned code = LGLYPH_CODE (lglyph);
24869 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24871 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24872 return glyph - s->row->glyphs[s->area];
24876 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24877 See the comment of fill_glyph_string for arguments.
24878 Value is the index of the first glyph not in S. */
24881 static int
24882 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24883 int start, int end, int overlaps)
24885 struct glyph *glyph, *last;
24886 int voffset;
24888 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24889 s->for_overlaps = overlaps;
24890 glyph = s->row->glyphs[s->area] + start;
24891 last = s->row->glyphs[s->area] + end;
24892 voffset = glyph->voffset;
24893 s->face = FACE_FROM_ID (s->f, face_id);
24894 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24895 s->nchars = 1;
24896 s->width = glyph->pixel_width;
24897 glyph++;
24898 while (glyph < last
24899 && glyph->type == GLYPHLESS_GLYPH
24900 && glyph->voffset == voffset
24901 && glyph->face_id == face_id)
24903 s->nchars++;
24904 s->width += glyph->pixel_width;
24905 glyph++;
24907 s->ybase += voffset;
24908 return glyph - s->row->glyphs[s->area];
24912 /* Fill glyph string S from a sequence of character glyphs.
24914 FACE_ID is the face id of the string. START is the index of the
24915 first glyph to consider, END is the index of the last + 1.
24916 OVERLAPS non-zero means S should draw the foreground only, and use
24917 its physical height for clipping. See also draw_glyphs.
24919 Value is the index of the first glyph not in S. */
24921 static int
24922 fill_glyph_string (struct glyph_string *s, int face_id,
24923 int start, int end, int overlaps)
24925 struct glyph *glyph, *last;
24926 int voffset;
24927 bool glyph_not_available_p;
24929 eassert (s->f == XFRAME (s->w->frame));
24930 eassert (s->nchars == 0);
24931 eassert (start >= 0 && end > start);
24933 s->for_overlaps = overlaps;
24934 glyph = s->row->glyphs[s->area] + start;
24935 last = s->row->glyphs[s->area] + end;
24936 voffset = glyph->voffset;
24937 s->padding_p = glyph->padding_p;
24938 glyph_not_available_p = glyph->glyph_not_available_p;
24940 while (glyph < last
24941 && glyph->type == CHAR_GLYPH
24942 && glyph->voffset == voffset
24943 /* Same face id implies same font, nowadays. */
24944 && glyph->face_id == face_id
24945 && glyph->glyph_not_available_p == glyph_not_available_p)
24947 s->face = get_glyph_face_and_encoding (s->f, glyph,
24948 s->char2b + s->nchars);
24949 ++s->nchars;
24950 eassert (s->nchars <= end - start);
24951 s->width += glyph->pixel_width;
24952 if (glyph++->padding_p != s->padding_p)
24953 break;
24956 s->font = s->face->font;
24958 /* If the specified font could not be loaded, use the frame's font,
24959 but record the fact that we couldn't load it in
24960 S->font_not_found_p so that we can draw rectangles for the
24961 characters of the glyph string. */
24962 if (s->font == NULL || glyph_not_available_p)
24964 s->font_not_found_p = true;
24965 s->font = FRAME_FONT (s->f);
24968 /* Adjust base line for subscript/superscript text. */
24969 s->ybase += voffset;
24971 eassert (s->face && s->face->gc);
24972 return glyph - s->row->glyphs[s->area];
24976 /* Fill glyph string S from image glyph S->first_glyph. */
24978 static void
24979 fill_image_glyph_string (struct glyph_string *s)
24981 eassert (s->first_glyph->type == IMAGE_GLYPH);
24982 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24983 eassert (s->img);
24984 s->slice = s->first_glyph->slice.img;
24985 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24986 s->font = s->face->font;
24987 s->width = s->first_glyph->pixel_width;
24989 /* Adjust base line for subscript/superscript text. */
24990 s->ybase += s->first_glyph->voffset;
24994 #ifdef HAVE_XWIDGETS
24995 static void
24996 fill_xwidget_glyph_string (struct glyph_string *s)
24998 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24999 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25000 s->font = s->face->font;
25001 s->width = s->first_glyph->pixel_width;
25002 s->ybase += s->first_glyph->voffset;
25003 s->xwidget = s->first_glyph->u.xwidget;
25005 #endif
25006 /* Fill glyph string S from a sequence of stretch glyphs.
25008 START is the index of the first glyph to consider,
25009 END is the index of the last + 1.
25011 Value is the index of the first glyph not in S. */
25013 static int
25014 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25016 struct glyph *glyph, *last;
25017 int voffset, face_id;
25019 eassert (s->first_glyph->type == STRETCH_GLYPH);
25021 glyph = s->row->glyphs[s->area] + start;
25022 last = s->row->glyphs[s->area] + end;
25023 face_id = glyph->face_id;
25024 s->face = FACE_FROM_ID (s->f, face_id);
25025 s->font = s->face->font;
25026 s->width = glyph->pixel_width;
25027 s->nchars = 1;
25028 voffset = glyph->voffset;
25030 for (++glyph;
25031 (glyph < last
25032 && glyph->type == STRETCH_GLYPH
25033 && glyph->voffset == voffset
25034 && glyph->face_id == face_id);
25035 ++glyph)
25036 s->width += glyph->pixel_width;
25038 /* Adjust base line for subscript/superscript text. */
25039 s->ybase += voffset;
25041 /* The case that face->gc == 0 is handled when drawing the glyph
25042 string by calling prepare_face_for_display. */
25043 eassert (s->face);
25044 return glyph - s->row->glyphs[s->area];
25047 static struct font_metrics *
25048 get_per_char_metric (struct font *font, XChar2b *char2b)
25050 static struct font_metrics metrics;
25051 unsigned code;
25053 if (! font)
25054 return NULL;
25055 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25056 if (code == FONT_INVALID_CODE)
25057 return NULL;
25058 font->driver->text_extents (font, &code, 1, &metrics);
25059 return &metrics;
25062 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25063 for FONT. Values are taken from font-global ones, except for fonts
25064 that claim preposterously large values, but whose glyphs actually
25065 have reasonable dimensions. C is the character to use for metrics
25066 if the font-global values are too large; if C is negative, the
25067 function selects a default character. */
25068 static void
25069 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25071 *ascent = FONT_BASE (font);
25072 *descent = FONT_DESCENT (font);
25074 if (FONT_TOO_HIGH (font))
25076 XChar2b char2b;
25078 /* Get metrics of C, defaulting to a reasonably sized ASCII
25079 character. */
25080 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25082 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25084 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25086 /* We add 1 pixel to character dimensions as heuristics
25087 that produces nicer display, e.g. when the face has
25088 the box attribute. */
25089 *ascent = pcm->ascent + 1;
25090 *descent = pcm->descent + 1;
25096 /* A subroutine that computes a reasonable "normal character height"
25097 for fonts that claim preposterously large vertical dimensions, but
25098 whose glyphs are actually reasonably sized. C is the character
25099 whose metrics to use for those fonts, or -1 for default
25100 character. */
25101 static int
25102 normal_char_height (struct font *font, int c)
25104 int ascent, descent;
25106 normal_char_ascent_descent (font, c, &ascent, &descent);
25108 return ascent + descent;
25111 /* EXPORT for RIF:
25112 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25113 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25114 assumed to be zero. */
25116 void
25117 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25119 *left = *right = 0;
25121 if (glyph->type == CHAR_GLYPH)
25123 XChar2b char2b;
25124 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25125 if (face->font)
25127 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25128 if (pcm)
25130 if (pcm->rbearing > pcm->width)
25131 *right = pcm->rbearing - pcm->width;
25132 if (pcm->lbearing < 0)
25133 *left = -pcm->lbearing;
25137 else if (glyph->type == COMPOSITE_GLYPH)
25139 if (! glyph->u.cmp.automatic)
25141 struct composition *cmp = composition_table[glyph->u.cmp.id];
25143 if (cmp->rbearing > cmp->pixel_width)
25144 *right = cmp->rbearing - cmp->pixel_width;
25145 if (cmp->lbearing < 0)
25146 *left = - cmp->lbearing;
25148 else
25150 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25151 struct font_metrics metrics;
25153 composition_gstring_width (gstring, glyph->slice.cmp.from,
25154 glyph->slice.cmp.to + 1, &metrics);
25155 if (metrics.rbearing > metrics.width)
25156 *right = metrics.rbearing - metrics.width;
25157 if (metrics.lbearing < 0)
25158 *left = - metrics.lbearing;
25164 /* Return the index of the first glyph preceding glyph string S that
25165 is overwritten by S because of S's left overhang. Value is -1
25166 if no glyphs are overwritten. */
25168 static int
25169 left_overwritten (struct glyph_string *s)
25171 int k;
25173 if (s->left_overhang)
25175 int x = 0, i;
25176 struct glyph *glyphs = s->row->glyphs[s->area];
25177 int first = s->first_glyph - glyphs;
25179 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25180 x -= glyphs[i].pixel_width;
25182 k = i + 1;
25184 else
25185 k = -1;
25187 return k;
25191 /* Return the index of the first glyph preceding glyph string S that
25192 is overwriting S because of its right overhang. Value is -1 if no
25193 glyph in front of S overwrites S. */
25195 static int
25196 left_overwriting (struct glyph_string *s)
25198 int i, k, x;
25199 struct glyph *glyphs = s->row->glyphs[s->area];
25200 int first = s->first_glyph - glyphs;
25202 k = -1;
25203 x = 0;
25204 for (i = first - 1; i >= 0; --i)
25206 int left, right;
25207 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25208 if (x + right > 0)
25209 k = i;
25210 x -= glyphs[i].pixel_width;
25213 return k;
25217 /* Return the index of the last glyph following glyph string S that is
25218 overwritten by S because of S's right overhang. Value is -1 if
25219 no such glyph is found. */
25221 static int
25222 right_overwritten (struct glyph_string *s)
25224 int k = -1;
25226 if (s->right_overhang)
25228 int x = 0, i;
25229 struct glyph *glyphs = s->row->glyphs[s->area];
25230 int first = (s->first_glyph - glyphs
25231 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25232 int end = s->row->used[s->area];
25234 for (i = first; i < end && s->right_overhang > x; ++i)
25235 x += glyphs[i].pixel_width;
25237 k = i;
25240 return k;
25244 /* Return the index of the last glyph following glyph string S that
25245 overwrites S because of its left overhang. Value is negative
25246 if no such glyph is found. */
25248 static int
25249 right_overwriting (struct glyph_string *s)
25251 int i, k, x;
25252 int end = s->row->used[s->area];
25253 struct glyph *glyphs = s->row->glyphs[s->area];
25254 int first = (s->first_glyph - glyphs
25255 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25257 k = -1;
25258 x = 0;
25259 for (i = first; i < end; ++i)
25261 int left, right;
25262 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25263 if (x - left < 0)
25264 k = i;
25265 x += glyphs[i].pixel_width;
25268 return k;
25272 /* Set background width of glyph string S. START is the index of the
25273 first glyph following S. LAST_X is the right-most x-position + 1
25274 in the drawing area. */
25276 static void
25277 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25279 /* If the face of this glyph string has to be drawn to the end of
25280 the drawing area, set S->extends_to_end_of_line_p. */
25282 if (start == s->row->used[s->area]
25283 && ((s->row->fill_line_p
25284 && (s->hl == DRAW_NORMAL_TEXT
25285 || s->hl == DRAW_IMAGE_RAISED
25286 || s->hl == DRAW_IMAGE_SUNKEN))
25287 || s->hl == DRAW_MOUSE_FACE))
25288 s->extends_to_end_of_line_p = true;
25290 /* If S extends its face to the end of the line, set its
25291 background_width to the distance to the right edge of the drawing
25292 area. */
25293 if (s->extends_to_end_of_line_p)
25294 s->background_width = last_x - s->x + 1;
25295 else
25296 s->background_width = s->width;
25300 /* Compute overhangs and x-positions for glyph string S and its
25301 predecessors, or successors. X is the starting x-position for S.
25302 BACKWARD_P means process predecessors. */
25304 static void
25305 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25307 if (backward_p)
25309 while (s)
25311 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25312 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25313 x -= s->width;
25314 s->x = x;
25315 s = s->prev;
25318 else
25320 while (s)
25322 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25323 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25324 s->x = x;
25325 x += s->width;
25326 s = s->next;
25333 /* The following macros are only called from draw_glyphs below.
25334 They reference the following parameters of that function directly:
25335 `w', `row', `area', and `overlap_p'
25336 as well as the following local variables:
25337 `s', `f', and `hdc' (in W32) */
25339 #ifdef HAVE_NTGUI
25340 /* On W32, silently add local `hdc' variable to argument list of
25341 init_glyph_string. */
25342 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25343 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25344 #else
25345 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25346 init_glyph_string (s, char2b, w, row, area, start, hl)
25347 #endif
25349 /* Add a glyph string for a stretch glyph to the list of strings
25350 between HEAD and TAIL. START is the index of the stretch glyph in
25351 row area AREA of glyph row ROW. END is the index of the last glyph
25352 in that glyph row area. X is the current output position assigned
25353 to the new glyph string constructed. HL overrides that face of the
25354 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25355 is the right-most x-position of the drawing area. */
25357 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25358 and below -- keep them on one line. */
25359 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25360 do \
25362 s = alloca (sizeof *s); \
25363 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25364 START = fill_stretch_glyph_string (s, START, END); \
25365 append_glyph_string (&HEAD, &TAIL, s); \
25366 s->x = (X); \
25368 while (false)
25371 /* Add a glyph string for an image glyph to the list of strings
25372 between HEAD and TAIL. START is the index of the image glyph in
25373 row area AREA of glyph row ROW. END is the index of the last glyph
25374 in that glyph row area. X is the current output position assigned
25375 to the new glyph string constructed. HL overrides that face of the
25376 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25377 is the right-most x-position of the drawing area. */
25379 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25380 do \
25382 s = alloca (sizeof *s); \
25383 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25384 fill_image_glyph_string (s); \
25385 append_glyph_string (&HEAD, &TAIL, s); \
25386 ++START; \
25387 s->x = (X); \
25389 while (false)
25391 #ifndef HAVE_XWIDGETS
25392 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25393 eassume (false)
25394 #else
25395 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25396 do \
25398 s = alloca (sizeof *s); \
25399 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25400 fill_xwidget_glyph_string (s); \
25401 append_glyph_string (&(HEAD), &(TAIL), s); \
25402 ++(START); \
25403 s->x = (X); \
25405 while (false)
25406 #endif
25408 /* Add a glyph string for a sequence of character glyphs to the list
25409 of strings between HEAD and TAIL. START is the index of the first
25410 glyph in row area AREA of glyph row ROW that is part of the new
25411 glyph string. END is the index of the last glyph in that glyph row
25412 area. X is the current output position assigned to the new glyph
25413 string constructed. HL overrides that face of the glyph; e.g. it
25414 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25415 right-most x-position of the drawing area. */
25417 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25418 do \
25420 int face_id; \
25421 XChar2b *char2b; \
25423 face_id = (row)->glyphs[area][START].face_id; \
25425 s = alloca (sizeof *s); \
25426 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25427 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25428 append_glyph_string (&HEAD, &TAIL, s); \
25429 s->x = (X); \
25430 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25432 while (false)
25435 /* Add a glyph string for a composite sequence to the list of strings
25436 between HEAD and TAIL. START is the index of the first glyph in
25437 row area AREA of glyph row ROW that is part of the new glyph
25438 string. END is the index of the last glyph in that glyph row area.
25439 X is the current output position assigned to the new glyph string
25440 constructed. HL overrides that face of the glyph; e.g. it is
25441 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25442 x-position of the drawing area. */
25444 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25445 do { \
25446 int face_id = (row)->glyphs[area][START].face_id; \
25447 struct face *base_face = FACE_FROM_ID (f, face_id); \
25448 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25449 struct composition *cmp = composition_table[cmp_id]; \
25450 XChar2b *char2b; \
25451 struct glyph_string *first_s = NULL; \
25452 int n; \
25454 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25456 /* Make glyph_strings for each glyph sequence that is drawable by \
25457 the same face, and append them to HEAD/TAIL. */ \
25458 for (n = 0; n < cmp->glyph_len;) \
25460 s = alloca (sizeof *s); \
25461 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25462 append_glyph_string (&(HEAD), &(TAIL), s); \
25463 s->cmp = cmp; \
25464 s->cmp_from = n; \
25465 s->x = (X); \
25466 if (n == 0) \
25467 first_s = s; \
25468 n = fill_composite_glyph_string (s, base_face, overlaps); \
25471 ++START; \
25472 s = first_s; \
25473 } while (false)
25476 /* Add a glyph string for a glyph-string sequence to the list of strings
25477 between HEAD and TAIL. */
25479 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25480 do { \
25481 int face_id; \
25482 XChar2b *char2b; \
25483 Lisp_Object gstring; \
25485 face_id = (row)->glyphs[area][START].face_id; \
25486 gstring = (composition_gstring_from_id \
25487 ((row)->glyphs[area][START].u.cmp.id)); \
25488 s = alloca (sizeof *s); \
25489 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25490 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25491 append_glyph_string (&(HEAD), &(TAIL), s); \
25492 s->x = (X); \
25493 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25494 } while (false)
25497 /* Add a glyph string for a sequence of glyphless character's glyphs
25498 to the list of strings between HEAD and TAIL. The meanings of
25499 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25501 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25502 do \
25504 int face_id; \
25506 face_id = (row)->glyphs[area][START].face_id; \
25508 s = alloca (sizeof *s); \
25509 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25510 append_glyph_string (&HEAD, &TAIL, s); \
25511 s->x = (X); \
25512 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25513 overlaps); \
25515 while (false)
25518 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25519 of AREA of glyph row ROW on window W between indices START and END.
25520 HL overrides the face for drawing glyph strings, e.g. it is
25521 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25522 x-positions of the drawing area.
25524 This is an ugly monster macro construct because we must use alloca
25525 to allocate glyph strings (because draw_glyphs can be called
25526 asynchronously). */
25528 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25529 do \
25531 HEAD = TAIL = NULL; \
25532 while (START < END) \
25534 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25535 switch (first_glyph->type) \
25537 case CHAR_GLYPH: \
25538 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25539 HL, X, LAST_X); \
25540 break; \
25542 case COMPOSITE_GLYPH: \
25543 if (first_glyph->u.cmp.automatic) \
25544 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25545 HL, X, LAST_X); \
25546 else \
25547 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25548 HL, X, LAST_X); \
25549 break; \
25551 case STRETCH_GLYPH: \
25552 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25553 HL, X, LAST_X); \
25554 break; \
25556 case IMAGE_GLYPH: \
25557 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25558 HL, X, LAST_X); \
25559 break;
25561 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25562 case XWIDGET_GLYPH: \
25563 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25564 HL, X, LAST_X); \
25565 break;
25567 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25568 case GLYPHLESS_GLYPH: \
25569 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25570 HL, X, LAST_X); \
25571 break; \
25573 default: \
25574 emacs_abort (); \
25577 if (s) \
25579 set_glyph_string_background_width (s, START, LAST_X); \
25580 (X) += s->width; \
25583 } while (false)
25586 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25587 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25588 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25589 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25592 /* Draw glyphs between START and END in AREA of ROW on window W,
25593 starting at x-position X. X is relative to AREA in W. HL is a
25594 face-override with the following meaning:
25596 DRAW_NORMAL_TEXT draw normally
25597 DRAW_CURSOR draw in cursor face
25598 DRAW_MOUSE_FACE draw in mouse face.
25599 DRAW_INVERSE_VIDEO draw in mode line face
25600 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25601 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25603 If OVERLAPS is non-zero, draw only the foreground of characters and
25604 clip to the physical height of ROW. Non-zero value also defines
25605 the overlapping part to be drawn:
25607 OVERLAPS_PRED overlap with preceding rows
25608 OVERLAPS_SUCC overlap with succeeding rows
25609 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25610 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25612 Value is the x-position reached, relative to AREA of W. */
25614 static int
25615 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25616 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25617 enum draw_glyphs_face hl, int overlaps)
25619 struct glyph_string *head, *tail;
25620 struct glyph_string *s;
25621 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25622 int i, j, x_reached, last_x, area_left = 0;
25623 struct frame *f = XFRAME (WINDOW_FRAME (w));
25624 DECLARE_HDC (hdc);
25626 ALLOCATE_HDC (hdc, f);
25628 /* Let's rather be paranoid than getting a SEGV. */
25629 end = min (end, row->used[area]);
25630 start = clip_to_bounds (0, start, end);
25632 /* Translate X to frame coordinates. Set last_x to the right
25633 end of the drawing area. */
25634 if (row->full_width_p)
25636 /* X is relative to the left edge of W, without scroll bars
25637 or fringes. */
25638 area_left = WINDOW_LEFT_EDGE_X (w);
25639 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25640 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25642 else
25644 area_left = window_box_left (w, area);
25645 last_x = area_left + window_box_width (w, area);
25647 x += area_left;
25649 /* Build a doubly-linked list of glyph_string structures between
25650 head and tail from what we have to draw. Note that the macro
25651 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25652 the reason we use a separate variable `i'. */
25653 i = start;
25654 USE_SAFE_ALLOCA;
25655 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25656 if (tail)
25657 x_reached = tail->x + tail->background_width;
25658 else
25659 x_reached = x;
25661 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25662 the row, redraw some glyphs in front or following the glyph
25663 strings built above. */
25664 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25666 struct glyph_string *h, *t;
25667 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25668 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25669 bool check_mouse_face = false;
25670 int dummy_x = 0;
25672 /* If mouse highlighting is on, we may need to draw adjacent
25673 glyphs using mouse-face highlighting. */
25674 if (area == TEXT_AREA && row->mouse_face_p
25675 && hlinfo->mouse_face_beg_row >= 0
25676 && hlinfo->mouse_face_end_row >= 0)
25678 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25680 if (row_vpos >= hlinfo->mouse_face_beg_row
25681 && row_vpos <= hlinfo->mouse_face_end_row)
25683 check_mouse_face = true;
25684 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25685 ? hlinfo->mouse_face_beg_col : 0;
25686 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25687 ? hlinfo->mouse_face_end_col
25688 : row->used[TEXT_AREA];
25692 /* Compute overhangs for all glyph strings. */
25693 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25694 for (s = head; s; s = s->next)
25695 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25697 /* Prepend glyph strings for glyphs in front of the first glyph
25698 string that are overwritten because of the first glyph
25699 string's left overhang. The background of all strings
25700 prepended must be drawn because the first glyph string
25701 draws over it. */
25702 i = left_overwritten (head);
25703 if (i >= 0)
25705 enum draw_glyphs_face overlap_hl;
25707 /* If this row contains mouse highlighting, attempt to draw
25708 the overlapped glyphs with the correct highlight. This
25709 code fails if the overlap encompasses more than one glyph
25710 and mouse-highlight spans only some of these glyphs.
25711 However, making it work perfectly involves a lot more
25712 code, and I don't know if the pathological case occurs in
25713 practice, so we'll stick to this for now. --- cyd */
25714 if (check_mouse_face
25715 && mouse_beg_col < start && mouse_end_col > i)
25716 overlap_hl = DRAW_MOUSE_FACE;
25717 else
25718 overlap_hl = DRAW_NORMAL_TEXT;
25720 if (hl != overlap_hl)
25721 clip_head = head;
25722 j = i;
25723 BUILD_GLYPH_STRINGS (j, start, h, t,
25724 overlap_hl, dummy_x, last_x);
25725 start = i;
25726 compute_overhangs_and_x (t, head->x, true);
25727 prepend_glyph_string_lists (&head, &tail, h, t);
25728 if (clip_head == NULL)
25729 clip_head = head;
25732 /* Prepend glyph strings for glyphs in front of the first glyph
25733 string that overwrite that glyph string because of their
25734 right overhang. For these strings, only the foreground must
25735 be drawn, because it draws over the glyph string at `head'.
25736 The background must not be drawn because this would overwrite
25737 right overhangs of preceding glyphs for which no glyph
25738 strings exist. */
25739 i = left_overwriting (head);
25740 if (i >= 0)
25742 enum draw_glyphs_face overlap_hl;
25744 if (check_mouse_face
25745 && mouse_beg_col < start && mouse_end_col > i)
25746 overlap_hl = DRAW_MOUSE_FACE;
25747 else
25748 overlap_hl = DRAW_NORMAL_TEXT;
25750 if (hl == overlap_hl || clip_head == NULL)
25751 clip_head = head;
25752 BUILD_GLYPH_STRINGS (i, start, h, t,
25753 overlap_hl, dummy_x, last_x);
25754 for (s = h; s; s = s->next)
25755 s->background_filled_p = true;
25756 compute_overhangs_and_x (t, head->x, true);
25757 prepend_glyph_string_lists (&head, &tail, h, t);
25760 /* Append glyphs strings for glyphs following the last glyph
25761 string tail that are overwritten by tail. The background of
25762 these strings has to be drawn because tail's foreground draws
25763 over it. */
25764 i = right_overwritten (tail);
25765 if (i >= 0)
25767 enum draw_glyphs_face overlap_hl;
25769 if (check_mouse_face
25770 && mouse_beg_col < i && mouse_end_col > end)
25771 overlap_hl = DRAW_MOUSE_FACE;
25772 else
25773 overlap_hl = DRAW_NORMAL_TEXT;
25775 if (hl != overlap_hl)
25776 clip_tail = tail;
25777 BUILD_GLYPH_STRINGS (end, i, h, t,
25778 overlap_hl, x, last_x);
25779 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25780 we don't have `end = i;' here. */
25781 compute_overhangs_and_x (h, tail->x + tail->width, false);
25782 append_glyph_string_lists (&head, &tail, h, t);
25783 if (clip_tail == NULL)
25784 clip_tail = tail;
25787 /* Append glyph strings for glyphs following the last glyph
25788 string tail that overwrite tail. The foreground of such
25789 glyphs has to be drawn because it writes into the background
25790 of tail. The background must not be drawn because it could
25791 paint over the foreground of following glyphs. */
25792 i = right_overwriting (tail);
25793 if (i >= 0)
25795 enum draw_glyphs_face overlap_hl;
25796 if (check_mouse_face
25797 && mouse_beg_col < i && mouse_end_col > end)
25798 overlap_hl = DRAW_MOUSE_FACE;
25799 else
25800 overlap_hl = DRAW_NORMAL_TEXT;
25802 if (hl == overlap_hl || clip_tail == NULL)
25803 clip_tail = tail;
25804 i++; /* We must include the Ith glyph. */
25805 BUILD_GLYPH_STRINGS (end, i, h, t,
25806 overlap_hl, x, last_x);
25807 for (s = h; s; s = s->next)
25808 s->background_filled_p = true;
25809 compute_overhangs_and_x (h, tail->x + tail->width, false);
25810 append_glyph_string_lists (&head, &tail, h, t);
25812 if (clip_head || clip_tail)
25813 for (s = head; s; s = s->next)
25815 s->clip_head = clip_head;
25816 s->clip_tail = clip_tail;
25820 /* Draw all strings. */
25821 for (s = head; s; s = s->next)
25822 FRAME_RIF (f)->draw_glyph_string (s);
25824 #ifndef HAVE_NS
25825 /* When focus a sole frame and move horizontally, this clears on_p
25826 causing a failure to erase prev cursor position. */
25827 if (area == TEXT_AREA
25828 && !row->full_width_p
25829 /* When drawing overlapping rows, only the glyph strings'
25830 foreground is drawn, which doesn't erase a cursor
25831 completely. */
25832 && !overlaps)
25834 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25835 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25836 : (tail ? tail->x + tail->background_width : x));
25837 x0 -= area_left;
25838 x1 -= area_left;
25840 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25841 row->y, MATRIX_ROW_BOTTOM_Y (row));
25843 #endif
25845 /* Value is the x-position up to which drawn, relative to AREA of W.
25846 This doesn't include parts drawn because of overhangs. */
25847 if (row->full_width_p)
25848 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25849 else
25850 x_reached -= area_left;
25852 RELEASE_HDC (hdc, f);
25854 SAFE_FREE ();
25855 return x_reached;
25858 /* Expand row matrix if too narrow. Don't expand if area
25859 is not present. */
25861 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25863 if (!it->f->fonts_changed \
25864 && (it->glyph_row->glyphs[area] \
25865 < it->glyph_row->glyphs[area + 1])) \
25867 it->w->ncols_scale_factor++; \
25868 it->f->fonts_changed = true; \
25872 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25873 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25875 static void
25876 append_glyph (struct it *it)
25878 struct glyph *glyph;
25879 enum glyph_row_area area = it->area;
25881 eassert (it->glyph_row);
25882 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25884 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25885 if (glyph < it->glyph_row->glyphs[area + 1])
25887 /* If the glyph row is reversed, we need to prepend the glyph
25888 rather than append it. */
25889 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25891 struct glyph *g;
25893 /* Make room for the additional glyph. */
25894 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25895 g[1] = *g;
25896 glyph = it->glyph_row->glyphs[area];
25898 glyph->charpos = CHARPOS (it->position);
25899 glyph->object = it->object;
25900 if (it->pixel_width > 0)
25902 eassert (it->pixel_width <= SHRT_MAX);
25903 glyph->pixel_width = it->pixel_width;
25904 glyph->padding_p = false;
25906 else
25908 /* Assure at least 1-pixel width. Otherwise, cursor can't
25909 be displayed correctly. */
25910 glyph->pixel_width = 1;
25911 glyph->padding_p = true;
25913 glyph->ascent = it->ascent;
25914 glyph->descent = it->descent;
25915 glyph->voffset = it->voffset;
25916 glyph->type = CHAR_GLYPH;
25917 glyph->avoid_cursor_p = it->avoid_cursor_p;
25918 glyph->multibyte_p = it->multibyte_p;
25919 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25921 /* In R2L rows, the left and the right box edges need to be
25922 drawn in reverse direction. */
25923 glyph->right_box_line_p = it->start_of_box_run_p;
25924 glyph->left_box_line_p = it->end_of_box_run_p;
25926 else
25928 glyph->left_box_line_p = it->start_of_box_run_p;
25929 glyph->right_box_line_p = it->end_of_box_run_p;
25931 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25932 || it->phys_descent > it->descent);
25933 glyph->glyph_not_available_p = it->glyph_not_available_p;
25934 glyph->face_id = it->face_id;
25935 glyph->u.ch = it->char_to_display;
25936 glyph->slice.img = null_glyph_slice;
25937 glyph->font_type = FONT_TYPE_UNKNOWN;
25938 if (it->bidi_p)
25940 glyph->resolved_level = it->bidi_it.resolved_level;
25941 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25942 glyph->bidi_type = it->bidi_it.type;
25944 else
25946 glyph->resolved_level = 0;
25947 glyph->bidi_type = UNKNOWN_BT;
25949 ++it->glyph_row->used[area];
25951 else
25952 IT_EXPAND_MATRIX_WIDTH (it, area);
25955 /* Store one glyph for the composition IT->cmp_it.id in
25956 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25957 non-null. */
25959 static void
25960 append_composite_glyph (struct it *it)
25962 struct glyph *glyph;
25963 enum glyph_row_area area = it->area;
25965 eassert (it->glyph_row);
25967 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25968 if (glyph < it->glyph_row->glyphs[area + 1])
25970 /* If the glyph row is reversed, we need to prepend the glyph
25971 rather than append it. */
25972 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25974 struct glyph *g;
25976 /* Make room for the new glyph. */
25977 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25978 g[1] = *g;
25979 glyph = it->glyph_row->glyphs[it->area];
25981 glyph->charpos = it->cmp_it.charpos;
25982 glyph->object = it->object;
25983 eassert (it->pixel_width <= SHRT_MAX);
25984 glyph->pixel_width = it->pixel_width;
25985 glyph->ascent = it->ascent;
25986 glyph->descent = it->descent;
25987 glyph->voffset = it->voffset;
25988 glyph->type = COMPOSITE_GLYPH;
25989 if (it->cmp_it.ch < 0)
25991 glyph->u.cmp.automatic = false;
25992 glyph->u.cmp.id = it->cmp_it.id;
25993 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25995 else
25997 glyph->u.cmp.automatic = true;
25998 glyph->u.cmp.id = it->cmp_it.id;
25999 glyph->slice.cmp.from = it->cmp_it.from;
26000 glyph->slice.cmp.to = it->cmp_it.to - 1;
26002 glyph->avoid_cursor_p = it->avoid_cursor_p;
26003 glyph->multibyte_p = it->multibyte_p;
26004 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26006 /* In R2L rows, the left and the right box edges need to be
26007 drawn in reverse direction. */
26008 glyph->right_box_line_p = it->start_of_box_run_p;
26009 glyph->left_box_line_p = it->end_of_box_run_p;
26011 else
26013 glyph->left_box_line_p = it->start_of_box_run_p;
26014 glyph->right_box_line_p = it->end_of_box_run_p;
26016 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26017 || it->phys_descent > it->descent);
26018 glyph->padding_p = false;
26019 glyph->glyph_not_available_p = false;
26020 glyph->face_id = it->face_id;
26021 glyph->font_type = FONT_TYPE_UNKNOWN;
26022 if (it->bidi_p)
26024 glyph->resolved_level = it->bidi_it.resolved_level;
26025 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26026 glyph->bidi_type = it->bidi_it.type;
26028 ++it->glyph_row->used[area];
26030 else
26031 IT_EXPAND_MATRIX_WIDTH (it, area);
26035 /* Change IT->ascent and IT->height according to the setting of
26036 IT->voffset. */
26038 static void
26039 take_vertical_position_into_account (struct it *it)
26041 if (it->voffset)
26043 if (it->voffset < 0)
26044 /* Increase the ascent so that we can display the text higher
26045 in the line. */
26046 it->ascent -= it->voffset;
26047 else
26048 /* Increase the descent so that we can display the text lower
26049 in the line. */
26050 it->descent += it->voffset;
26055 /* Produce glyphs/get display metrics for the image IT is loaded with.
26056 See the description of struct display_iterator in dispextern.h for
26057 an overview of struct display_iterator. */
26059 static void
26060 produce_image_glyph (struct it *it)
26062 struct image *img;
26063 struct face *face;
26064 int glyph_ascent, crop;
26065 struct glyph_slice slice;
26067 eassert (it->what == IT_IMAGE);
26069 face = FACE_FROM_ID (it->f, it->face_id);
26070 /* Make sure X resources of the face is loaded. */
26071 prepare_face_for_display (it->f, face);
26073 if (it->image_id < 0)
26075 /* Fringe bitmap. */
26076 it->ascent = it->phys_ascent = 0;
26077 it->descent = it->phys_descent = 0;
26078 it->pixel_width = 0;
26079 it->nglyphs = 0;
26080 return;
26083 img = IMAGE_FROM_ID (it->f, it->image_id);
26084 /* Make sure X resources of the image is loaded. */
26085 prepare_image_for_display (it->f, img);
26087 slice.x = slice.y = 0;
26088 slice.width = img->width;
26089 slice.height = img->height;
26091 if (INTEGERP (it->slice.x))
26092 slice.x = XINT (it->slice.x);
26093 else if (FLOATP (it->slice.x))
26094 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26096 if (INTEGERP (it->slice.y))
26097 slice.y = XINT (it->slice.y);
26098 else if (FLOATP (it->slice.y))
26099 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26101 if (INTEGERP (it->slice.width))
26102 slice.width = XINT (it->slice.width);
26103 else if (FLOATP (it->slice.width))
26104 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26106 if (INTEGERP (it->slice.height))
26107 slice.height = XINT (it->slice.height);
26108 else if (FLOATP (it->slice.height))
26109 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26111 if (slice.x >= img->width)
26112 slice.x = img->width;
26113 if (slice.y >= img->height)
26114 slice.y = img->height;
26115 if (slice.x + slice.width >= img->width)
26116 slice.width = img->width - slice.x;
26117 if (slice.y + slice.height > img->height)
26118 slice.height = img->height - slice.y;
26120 if (slice.width == 0 || slice.height == 0)
26121 return;
26123 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26125 it->descent = slice.height - glyph_ascent;
26126 if (slice.y == 0)
26127 it->descent += img->vmargin;
26128 if (slice.y + slice.height == img->height)
26129 it->descent += img->vmargin;
26130 it->phys_descent = it->descent;
26132 it->pixel_width = slice.width;
26133 if (slice.x == 0)
26134 it->pixel_width += img->hmargin;
26135 if (slice.x + slice.width == img->width)
26136 it->pixel_width += img->hmargin;
26138 /* It's quite possible for images to have an ascent greater than
26139 their height, so don't get confused in that case. */
26140 if (it->descent < 0)
26141 it->descent = 0;
26143 it->nglyphs = 1;
26145 if (face->box != FACE_NO_BOX)
26147 if (face->box_line_width > 0)
26149 if (slice.y == 0)
26150 it->ascent += face->box_line_width;
26151 if (slice.y + slice.height == img->height)
26152 it->descent += face->box_line_width;
26155 if (it->start_of_box_run_p && slice.x == 0)
26156 it->pixel_width += eabs (face->box_line_width);
26157 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26158 it->pixel_width += eabs (face->box_line_width);
26161 take_vertical_position_into_account (it);
26163 /* Automatically crop wide image glyphs at right edge so we can
26164 draw the cursor on same display row. */
26165 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26166 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26168 it->pixel_width -= crop;
26169 slice.width -= crop;
26172 if (it->glyph_row)
26174 struct glyph *glyph;
26175 enum glyph_row_area area = it->area;
26177 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26178 if (it->glyph_row->reversed_p)
26180 struct glyph *g;
26182 /* Make room for the new glyph. */
26183 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26184 g[1] = *g;
26185 glyph = it->glyph_row->glyphs[it->area];
26187 if (glyph < it->glyph_row->glyphs[area + 1])
26189 glyph->charpos = CHARPOS (it->position);
26190 glyph->object = it->object;
26191 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26192 glyph->ascent = glyph_ascent;
26193 glyph->descent = it->descent;
26194 glyph->voffset = it->voffset;
26195 glyph->type = IMAGE_GLYPH;
26196 glyph->avoid_cursor_p = it->avoid_cursor_p;
26197 glyph->multibyte_p = it->multibyte_p;
26198 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26200 /* In R2L rows, the left and the right box edges need to be
26201 drawn in reverse direction. */
26202 glyph->right_box_line_p = it->start_of_box_run_p;
26203 glyph->left_box_line_p = it->end_of_box_run_p;
26205 else
26207 glyph->left_box_line_p = it->start_of_box_run_p;
26208 glyph->right_box_line_p = it->end_of_box_run_p;
26210 glyph->overlaps_vertically_p = false;
26211 glyph->padding_p = false;
26212 glyph->glyph_not_available_p = false;
26213 glyph->face_id = it->face_id;
26214 glyph->u.img_id = img->id;
26215 glyph->slice.img = slice;
26216 glyph->font_type = FONT_TYPE_UNKNOWN;
26217 if (it->bidi_p)
26219 glyph->resolved_level = it->bidi_it.resolved_level;
26220 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26221 glyph->bidi_type = it->bidi_it.type;
26223 ++it->glyph_row->used[area];
26225 else
26226 IT_EXPAND_MATRIX_WIDTH (it, area);
26230 static void
26231 produce_xwidget_glyph (struct it *it)
26233 #ifdef HAVE_XWIDGETS
26234 struct xwidget *xw;
26235 int glyph_ascent, crop;
26236 eassert (it->what == IT_XWIDGET);
26238 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26239 /* Make sure X resources of the face is loaded. */
26240 prepare_face_for_display (it->f, face);
26242 xw = it->xwidget;
26243 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26244 it->descent = xw->height/2;
26245 it->phys_descent = it->descent;
26246 it->pixel_width = xw->width;
26247 /* It's quite possible for images to have an ascent greater than
26248 their height, so don't get confused in that case. */
26249 if (it->descent < 0)
26250 it->descent = 0;
26252 it->nglyphs = 1;
26254 if (face->box != FACE_NO_BOX)
26256 if (face->box_line_width > 0)
26258 it->ascent += face->box_line_width;
26259 it->descent += face->box_line_width;
26262 if (it->start_of_box_run_p)
26263 it->pixel_width += eabs (face->box_line_width);
26264 it->pixel_width += eabs (face->box_line_width);
26267 take_vertical_position_into_account (it);
26269 /* Automatically crop wide image glyphs at right edge so we can
26270 draw the cursor on same display row. */
26271 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26272 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26273 it->pixel_width -= crop;
26275 if (it->glyph_row)
26277 enum glyph_row_area area = it->area;
26278 struct glyph *glyph
26279 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26281 if (it->glyph_row->reversed_p)
26283 struct glyph *g;
26285 /* Make room for the new glyph. */
26286 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26287 g[1] = *g;
26288 glyph = it->glyph_row->glyphs[it->area];
26290 if (glyph < it->glyph_row->glyphs[area + 1])
26292 glyph->charpos = CHARPOS (it->position);
26293 glyph->object = it->object;
26294 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26295 glyph->ascent = glyph_ascent;
26296 glyph->descent = it->descent;
26297 glyph->voffset = it->voffset;
26298 glyph->type = XWIDGET_GLYPH;
26299 glyph->avoid_cursor_p = it->avoid_cursor_p;
26300 glyph->multibyte_p = it->multibyte_p;
26301 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26303 /* In R2L rows, the left and the right box edges need to be
26304 drawn in reverse direction. */
26305 glyph->right_box_line_p = it->start_of_box_run_p;
26306 glyph->left_box_line_p = it->end_of_box_run_p;
26308 else
26310 glyph->left_box_line_p = it->start_of_box_run_p;
26311 glyph->right_box_line_p = it->end_of_box_run_p;
26313 glyph->overlaps_vertically_p = 0;
26314 glyph->padding_p = 0;
26315 glyph->glyph_not_available_p = 0;
26316 glyph->face_id = it->face_id;
26317 glyph->u.xwidget = it->xwidget;
26318 glyph->font_type = FONT_TYPE_UNKNOWN;
26319 if (it->bidi_p)
26321 glyph->resolved_level = it->bidi_it.resolved_level;
26322 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26323 glyph->bidi_type = it->bidi_it.type;
26325 ++it->glyph_row->used[area];
26327 else
26328 IT_EXPAND_MATRIX_WIDTH (it, area);
26330 #endif
26333 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26334 of the glyph, WIDTH and HEIGHT are the width and height of the
26335 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26337 static void
26338 append_stretch_glyph (struct it *it, Lisp_Object object,
26339 int width, int height, int ascent)
26341 struct glyph *glyph;
26342 enum glyph_row_area area = it->area;
26344 eassert (ascent >= 0 && ascent <= height);
26346 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26347 if (glyph < it->glyph_row->glyphs[area + 1])
26349 /* If the glyph row is reversed, we need to prepend the glyph
26350 rather than append it. */
26351 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26353 struct glyph *g;
26355 /* Make room for the additional glyph. */
26356 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26357 g[1] = *g;
26358 glyph = it->glyph_row->glyphs[area];
26360 /* Decrease the width of the first glyph of the row that
26361 begins before first_visible_x (e.g., due to hscroll).
26362 This is so the overall width of the row becomes smaller
26363 by the scroll amount, and the stretch glyph appended by
26364 extend_face_to_end_of_line will be wider, to shift the
26365 row glyphs to the right. (In L2R rows, the corresponding
26366 left-shift effect is accomplished by setting row->x to a
26367 negative value, which won't work with R2L rows.)
26369 This must leave us with a positive value of WIDTH, since
26370 otherwise the call to move_it_in_display_line_to at the
26371 beginning of display_line would have got past the entire
26372 first glyph, and then it->current_x would have been
26373 greater or equal to it->first_visible_x. */
26374 if (it->current_x < it->first_visible_x)
26375 width -= it->first_visible_x - it->current_x;
26376 eassert (width > 0);
26378 glyph->charpos = CHARPOS (it->position);
26379 glyph->object = object;
26380 /* FIXME: It would be better to use TYPE_MAX here, but
26381 __typeof__ is not portable enough... */
26382 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26383 glyph->ascent = ascent;
26384 glyph->descent = height - ascent;
26385 glyph->voffset = it->voffset;
26386 glyph->type = STRETCH_GLYPH;
26387 glyph->avoid_cursor_p = it->avoid_cursor_p;
26388 glyph->multibyte_p = it->multibyte_p;
26389 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26391 /* In R2L rows, the left and the right box edges need to be
26392 drawn in reverse direction. */
26393 glyph->right_box_line_p = it->start_of_box_run_p;
26394 glyph->left_box_line_p = it->end_of_box_run_p;
26396 else
26398 glyph->left_box_line_p = it->start_of_box_run_p;
26399 glyph->right_box_line_p = it->end_of_box_run_p;
26401 glyph->overlaps_vertically_p = false;
26402 glyph->padding_p = false;
26403 glyph->glyph_not_available_p = false;
26404 glyph->face_id = it->face_id;
26405 glyph->u.stretch.ascent = ascent;
26406 glyph->u.stretch.height = height;
26407 glyph->slice.img = null_glyph_slice;
26408 glyph->font_type = FONT_TYPE_UNKNOWN;
26409 if (it->bidi_p)
26411 glyph->resolved_level = it->bidi_it.resolved_level;
26412 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26413 glyph->bidi_type = it->bidi_it.type;
26415 else
26417 glyph->resolved_level = 0;
26418 glyph->bidi_type = UNKNOWN_BT;
26420 ++it->glyph_row->used[area];
26422 else
26423 IT_EXPAND_MATRIX_WIDTH (it, area);
26426 #endif /* HAVE_WINDOW_SYSTEM */
26428 /* Produce a stretch glyph for iterator IT. IT->object is the value
26429 of the glyph property displayed. The value must be a list
26430 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26431 being recognized:
26433 1. `:width WIDTH' specifies that the space should be WIDTH *
26434 canonical char width wide. WIDTH may be an integer or floating
26435 point number.
26437 2. `:relative-width FACTOR' specifies that the width of the stretch
26438 should be computed from the width of the first character having the
26439 `glyph' property, and should be FACTOR times that width.
26441 3. `:align-to HPOS' specifies that the space should be wide enough
26442 to reach HPOS, a value in canonical character units.
26444 Exactly one of the above pairs must be present.
26446 4. `:height HEIGHT' specifies that the height of the stretch produced
26447 should be HEIGHT, measured in canonical character units.
26449 5. `:relative-height FACTOR' specifies that the height of the
26450 stretch should be FACTOR times the height of the characters having
26451 the glyph property.
26453 Either none or exactly one of 4 or 5 must be present.
26455 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26456 of the stretch should be used for the ascent of the stretch.
26457 ASCENT must be in the range 0 <= ASCENT <= 100. */
26459 void
26460 produce_stretch_glyph (struct it *it)
26462 /* (space :width WIDTH :height HEIGHT ...) */
26463 Lisp_Object prop, plist;
26464 int width = 0, height = 0, align_to = -1;
26465 bool zero_width_ok_p = false;
26466 double tem;
26467 struct font *font = NULL;
26469 #ifdef HAVE_WINDOW_SYSTEM
26470 int ascent = 0;
26471 bool zero_height_ok_p = false;
26473 if (FRAME_WINDOW_P (it->f))
26475 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26476 font = face->font ? face->font : FRAME_FONT (it->f);
26477 prepare_face_for_display (it->f, face);
26479 #endif
26481 /* List should start with `space'. */
26482 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26483 plist = XCDR (it->object);
26485 /* Compute the width of the stretch. */
26486 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26487 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26489 /* Absolute width `:width WIDTH' specified and valid. */
26490 zero_width_ok_p = true;
26491 width = (int)tem;
26493 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26495 /* Relative width `:relative-width FACTOR' specified and valid.
26496 Compute the width of the characters having the `glyph'
26497 property. */
26498 struct it it2;
26499 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26501 it2 = *it;
26502 if (it->multibyte_p)
26503 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26504 else
26506 it2.c = it2.char_to_display = *p, it2.len = 1;
26507 if (! ASCII_CHAR_P (it2.c))
26508 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26511 it2.glyph_row = NULL;
26512 it2.what = IT_CHARACTER;
26513 PRODUCE_GLYPHS (&it2);
26514 width = NUMVAL (prop) * it2.pixel_width;
26516 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26517 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26518 &align_to))
26520 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26521 align_to = (align_to < 0
26523 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26524 else if (align_to < 0)
26525 align_to = window_box_left_offset (it->w, TEXT_AREA);
26526 width = max (0, (int)tem + align_to - it->current_x);
26527 zero_width_ok_p = true;
26529 else
26530 /* Nothing specified -> width defaults to canonical char width. */
26531 width = FRAME_COLUMN_WIDTH (it->f);
26533 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26534 width = 1;
26536 #ifdef HAVE_WINDOW_SYSTEM
26537 /* Compute height. */
26538 if (FRAME_WINDOW_P (it->f))
26540 int default_height = normal_char_height (font, ' ');
26542 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26543 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26545 height = (int)tem;
26546 zero_height_ok_p = true;
26548 else if (prop = Fplist_get (plist, QCrelative_height),
26549 NUMVAL (prop) > 0)
26550 height = default_height * NUMVAL (prop);
26551 else
26552 height = default_height;
26554 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26555 height = 1;
26557 /* Compute percentage of height used for ascent. If
26558 `:ascent ASCENT' is present and valid, use that. Otherwise,
26559 derive the ascent from the font in use. */
26560 if (prop = Fplist_get (plist, QCascent),
26561 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26562 ascent = height * NUMVAL (prop) / 100.0;
26563 else if (!NILP (prop)
26564 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26565 ascent = min (max (0, (int)tem), height);
26566 else
26567 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26569 else
26570 #endif /* HAVE_WINDOW_SYSTEM */
26571 height = 1;
26573 if (width > 0 && it->line_wrap != TRUNCATE
26574 && it->current_x + width > it->last_visible_x)
26576 width = it->last_visible_x - it->current_x;
26577 #ifdef HAVE_WINDOW_SYSTEM
26578 /* Subtract one more pixel from the stretch width, but only on
26579 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26580 width -= FRAME_WINDOW_P (it->f);
26581 #endif
26584 if (width > 0 && height > 0 && it->glyph_row)
26586 Lisp_Object o_object = it->object;
26587 Lisp_Object object = it->stack[it->sp - 1].string;
26588 int n = width;
26590 if (!STRINGP (object))
26591 object = it->w->contents;
26592 #ifdef HAVE_WINDOW_SYSTEM
26593 if (FRAME_WINDOW_P (it->f))
26594 append_stretch_glyph (it, object, width, height, ascent);
26595 else
26596 #endif
26598 it->object = object;
26599 it->char_to_display = ' ';
26600 it->pixel_width = it->len = 1;
26601 while (n--)
26602 tty_append_glyph (it);
26603 it->object = o_object;
26607 it->pixel_width = width;
26608 #ifdef HAVE_WINDOW_SYSTEM
26609 if (FRAME_WINDOW_P (it->f))
26611 it->ascent = it->phys_ascent = ascent;
26612 it->descent = it->phys_descent = height - it->ascent;
26613 it->nglyphs = width > 0 && height > 0;
26614 take_vertical_position_into_account (it);
26616 else
26617 #endif
26618 it->nglyphs = width;
26621 /* Get information about special display element WHAT in an
26622 environment described by IT. WHAT is one of IT_TRUNCATION or
26623 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26624 non-null glyph_row member. This function ensures that fields like
26625 face_id, c, len of IT are left untouched. */
26627 static void
26628 produce_special_glyphs (struct it *it, enum display_element_type what)
26630 struct it temp_it;
26631 Lisp_Object gc;
26632 GLYPH glyph;
26634 temp_it = *it;
26635 temp_it.object = Qnil;
26636 memset (&temp_it.current, 0, sizeof temp_it.current);
26638 if (what == IT_CONTINUATION)
26640 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26641 if (it->bidi_it.paragraph_dir == R2L)
26642 SET_GLYPH_FROM_CHAR (glyph, '/');
26643 else
26644 SET_GLYPH_FROM_CHAR (glyph, '\\');
26645 if (it->dp
26646 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26648 /* FIXME: Should we mirror GC for R2L lines? */
26649 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26650 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26653 else if (what == IT_TRUNCATION)
26655 /* Truncation glyph. */
26656 SET_GLYPH_FROM_CHAR (glyph, '$');
26657 if (it->dp
26658 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26660 /* FIXME: Should we mirror GC for R2L lines? */
26661 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26662 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26665 else
26666 emacs_abort ();
26668 #ifdef HAVE_WINDOW_SYSTEM
26669 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26670 is turned off, we precede the truncation/continuation glyphs by a
26671 stretch glyph whose width is computed such that these special
26672 glyphs are aligned at the window margin, even when very different
26673 fonts are used in different glyph rows. */
26674 if (FRAME_WINDOW_P (temp_it.f)
26675 /* init_iterator calls this with it->glyph_row == NULL, and it
26676 wants only the pixel width of the truncation/continuation
26677 glyphs. */
26678 && temp_it.glyph_row
26679 /* insert_left_trunc_glyphs calls us at the beginning of the
26680 row, and it has its own calculation of the stretch glyph
26681 width. */
26682 && temp_it.glyph_row->used[TEXT_AREA] > 0
26683 && (temp_it.glyph_row->reversed_p
26684 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26685 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26687 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26689 if (stretch_width > 0)
26691 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26692 struct font *font =
26693 face->font ? face->font : FRAME_FONT (temp_it.f);
26694 int stretch_ascent =
26695 (((temp_it.ascent + temp_it.descent)
26696 * FONT_BASE (font)) / FONT_HEIGHT (font));
26698 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26699 temp_it.ascent + temp_it.descent,
26700 stretch_ascent);
26703 #endif
26705 temp_it.dp = NULL;
26706 temp_it.what = IT_CHARACTER;
26707 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26708 temp_it.face_id = GLYPH_FACE (glyph);
26709 temp_it.len = CHAR_BYTES (temp_it.c);
26711 PRODUCE_GLYPHS (&temp_it);
26712 it->pixel_width = temp_it.pixel_width;
26713 it->nglyphs = temp_it.nglyphs;
26716 #ifdef HAVE_WINDOW_SYSTEM
26718 /* Calculate line-height and line-spacing properties.
26719 An integer value specifies explicit pixel value.
26720 A float value specifies relative value to current face height.
26721 A cons (float . face-name) specifies relative value to
26722 height of specified face font.
26724 Returns height in pixels, or nil. */
26726 static Lisp_Object
26727 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26728 int boff, bool override)
26730 Lisp_Object face_name = Qnil;
26731 int ascent, descent, height;
26733 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26734 return val;
26736 if (CONSP (val))
26738 face_name = XCAR (val);
26739 val = XCDR (val);
26740 if (!NUMBERP (val))
26741 val = make_number (1);
26742 if (NILP (face_name))
26744 height = it->ascent + it->descent;
26745 goto scale;
26749 if (NILP (face_name))
26751 font = FRAME_FONT (it->f);
26752 boff = FRAME_BASELINE_OFFSET (it->f);
26754 else if (EQ (face_name, Qt))
26756 override = false;
26758 else
26760 int face_id;
26761 struct face *face;
26763 face_id = lookup_named_face (it->f, face_name, false);
26764 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
26765 if (face == NULL || ((font = face->font) == NULL))
26766 return make_number (-1);
26767 boff = font->baseline_offset;
26768 if (font->vertical_centering)
26769 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26772 normal_char_ascent_descent (font, -1, &ascent, &descent);
26774 if (override)
26776 it->override_ascent = ascent;
26777 it->override_descent = descent;
26778 it->override_boff = boff;
26781 height = ascent + descent;
26783 scale:
26784 if (FLOATP (val))
26785 height = (int)(XFLOAT_DATA (val) * height);
26786 else if (INTEGERP (val))
26787 height *= XINT (val);
26789 return make_number (height);
26793 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26794 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26795 and only if this is for a character for which no font was found.
26797 If the display method (it->glyphless_method) is
26798 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26799 length of the acronym or the hexadecimal string, UPPER_XOFF and
26800 UPPER_YOFF are pixel offsets for the upper part of the string,
26801 LOWER_XOFF and LOWER_YOFF are for the lower part.
26803 For the other display methods, LEN through LOWER_YOFF are zero. */
26805 static void
26806 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26807 short upper_xoff, short upper_yoff,
26808 short lower_xoff, short lower_yoff)
26810 struct glyph *glyph;
26811 enum glyph_row_area area = it->area;
26813 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26814 if (glyph < it->glyph_row->glyphs[area + 1])
26816 /* If the glyph row is reversed, we need to prepend the glyph
26817 rather than append it. */
26818 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26820 struct glyph *g;
26822 /* Make room for the additional glyph. */
26823 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26824 g[1] = *g;
26825 glyph = it->glyph_row->glyphs[area];
26827 glyph->charpos = CHARPOS (it->position);
26828 glyph->object = it->object;
26829 eassert (it->pixel_width <= SHRT_MAX);
26830 glyph->pixel_width = it->pixel_width;
26831 glyph->ascent = it->ascent;
26832 glyph->descent = it->descent;
26833 glyph->voffset = it->voffset;
26834 glyph->type = GLYPHLESS_GLYPH;
26835 glyph->u.glyphless.method = it->glyphless_method;
26836 glyph->u.glyphless.for_no_font = for_no_font;
26837 glyph->u.glyphless.len = len;
26838 glyph->u.glyphless.ch = it->c;
26839 glyph->slice.glyphless.upper_xoff = upper_xoff;
26840 glyph->slice.glyphless.upper_yoff = upper_yoff;
26841 glyph->slice.glyphless.lower_xoff = lower_xoff;
26842 glyph->slice.glyphless.lower_yoff = lower_yoff;
26843 glyph->avoid_cursor_p = it->avoid_cursor_p;
26844 glyph->multibyte_p = it->multibyte_p;
26845 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26847 /* In R2L rows, the left and the right box edges need to be
26848 drawn in reverse direction. */
26849 glyph->right_box_line_p = it->start_of_box_run_p;
26850 glyph->left_box_line_p = it->end_of_box_run_p;
26852 else
26854 glyph->left_box_line_p = it->start_of_box_run_p;
26855 glyph->right_box_line_p = it->end_of_box_run_p;
26857 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26858 || it->phys_descent > it->descent);
26859 glyph->padding_p = false;
26860 glyph->glyph_not_available_p = false;
26861 glyph->face_id = face_id;
26862 glyph->font_type = FONT_TYPE_UNKNOWN;
26863 if (it->bidi_p)
26865 glyph->resolved_level = it->bidi_it.resolved_level;
26866 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26867 glyph->bidi_type = it->bidi_it.type;
26869 ++it->glyph_row->used[area];
26871 else
26872 IT_EXPAND_MATRIX_WIDTH (it, area);
26876 /* Produce a glyph for a glyphless character for iterator IT.
26877 IT->glyphless_method specifies which method to use for displaying
26878 the character. See the description of enum
26879 glyphless_display_method in dispextern.h for the detail.
26881 FOR_NO_FONT is true if and only if this is for a character for
26882 which no font was found. ACRONYM, if non-nil, is an acronym string
26883 for the character. */
26885 static void
26886 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26888 int face_id;
26889 struct face *face;
26890 struct font *font;
26891 int base_width, base_height, width, height;
26892 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26893 int len;
26895 /* Get the metrics of the base font. We always refer to the current
26896 ASCII face. */
26897 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26898 font = face->font ? face->font : FRAME_FONT (it->f);
26899 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26900 it->ascent += font->baseline_offset;
26901 it->descent -= font->baseline_offset;
26902 base_height = it->ascent + it->descent;
26903 base_width = font->average_width;
26905 face_id = merge_glyphless_glyph_face (it);
26907 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26909 it->pixel_width = THIN_SPACE_WIDTH;
26910 len = 0;
26911 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26913 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26915 width = CHARACTER_WIDTH (it->c);
26916 if (width == 0)
26917 width = 1;
26918 else if (width > 4)
26919 width = 4;
26920 it->pixel_width = base_width * width;
26921 len = 0;
26922 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26924 else
26926 char buf[7];
26927 const char *str;
26928 unsigned int code[6];
26929 int upper_len;
26930 int ascent, descent;
26931 struct font_metrics metrics_upper, metrics_lower;
26933 face = FACE_FROM_ID (it->f, face_id);
26934 font = face->font ? face->font : FRAME_FONT (it->f);
26935 prepare_face_for_display (it->f, face);
26937 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26939 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26940 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26941 if (CONSP (acronym))
26942 acronym = XCAR (acronym);
26943 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26945 else
26947 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26948 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26949 str = buf;
26951 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26952 code[len] = font->driver->encode_char (font, str[len]);
26953 upper_len = (len + 1) / 2;
26954 font->driver->text_extents (font, code, upper_len,
26955 &metrics_upper);
26956 font->driver->text_extents (font, code + upper_len, len - upper_len,
26957 &metrics_lower);
26961 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26962 width = max (metrics_upper.width, metrics_lower.width) + 4;
26963 upper_xoff = upper_yoff = 2; /* the typical case */
26964 if (base_width >= width)
26966 /* Align the upper to the left, the lower to the right. */
26967 it->pixel_width = base_width;
26968 lower_xoff = base_width - 2 - metrics_lower.width;
26970 else
26972 /* Center the shorter one. */
26973 it->pixel_width = width;
26974 if (metrics_upper.width >= metrics_lower.width)
26975 lower_xoff = (width - metrics_lower.width) / 2;
26976 else
26978 /* FIXME: This code doesn't look right. It formerly was
26979 missing the "lower_xoff = 0;", which couldn't have
26980 been right since it left lower_xoff uninitialized. */
26981 lower_xoff = 0;
26982 upper_xoff = (width - metrics_upper.width) / 2;
26986 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26987 top, bottom, and between upper and lower strings. */
26988 height = (metrics_upper.ascent + metrics_upper.descent
26989 + metrics_lower.ascent + metrics_lower.descent) + 5;
26990 /* Center vertically.
26991 H:base_height, D:base_descent
26992 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26994 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26995 descent = D - H/2 + h/2;
26996 lower_yoff = descent - 2 - ld;
26997 upper_yoff = lower_yoff - la - 1 - ud; */
26998 ascent = - (it->descent - (base_height + height + 1) / 2);
26999 descent = it->descent - (base_height - height) / 2;
27000 lower_yoff = descent - 2 - metrics_lower.descent;
27001 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27002 - metrics_upper.descent);
27003 /* Don't make the height shorter than the base height. */
27004 if (height > base_height)
27006 it->ascent = ascent;
27007 it->descent = descent;
27011 it->phys_ascent = it->ascent;
27012 it->phys_descent = it->descent;
27013 if (it->glyph_row)
27014 append_glyphless_glyph (it, face_id, for_no_font, len,
27015 upper_xoff, upper_yoff,
27016 lower_xoff, lower_yoff);
27017 it->nglyphs = 1;
27018 take_vertical_position_into_account (it);
27022 /* RIF:
27023 Produce glyphs/get display metrics for the display element IT is
27024 loaded with. See the description of struct it in dispextern.h
27025 for an overview of struct it. */
27027 void
27028 x_produce_glyphs (struct it *it)
27030 int extra_line_spacing = it->extra_line_spacing;
27032 it->glyph_not_available_p = false;
27034 if (it->what == IT_CHARACTER)
27036 XChar2b char2b;
27037 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27038 struct font *font = face->font;
27039 struct font_metrics *pcm = NULL;
27040 int boff; /* Baseline offset. */
27042 if (font == NULL)
27044 /* When no suitable font is found, display this character by
27045 the method specified in the first extra slot of
27046 Vglyphless_char_display. */
27047 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27049 eassert (it->what == IT_GLYPHLESS);
27050 produce_glyphless_glyph (it, true,
27051 STRINGP (acronym) ? acronym : Qnil);
27052 goto done;
27055 boff = font->baseline_offset;
27056 if (font->vertical_centering)
27057 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27059 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27061 it->nglyphs = 1;
27063 if (it->override_ascent >= 0)
27065 it->ascent = it->override_ascent;
27066 it->descent = it->override_descent;
27067 boff = it->override_boff;
27069 else
27071 it->ascent = FONT_BASE (font) + boff;
27072 it->descent = FONT_DESCENT (font) - boff;
27075 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27077 pcm = get_per_char_metric (font, &char2b);
27078 if (pcm->width == 0
27079 && pcm->rbearing == 0 && pcm->lbearing == 0)
27080 pcm = NULL;
27083 if (pcm)
27085 it->phys_ascent = pcm->ascent + boff;
27086 it->phys_descent = pcm->descent - boff;
27087 it->pixel_width = pcm->width;
27088 /* Don't use font-global values for ascent and descent
27089 if they result in an exceedingly large line height. */
27090 if (it->override_ascent < 0)
27092 if (FONT_TOO_HIGH (font))
27094 it->ascent = it->phys_ascent;
27095 it->descent = it->phys_descent;
27096 /* These limitations are enforced by an
27097 assertion near the end of this function. */
27098 if (it->ascent < 0)
27099 it->ascent = 0;
27100 if (it->descent < 0)
27101 it->descent = 0;
27105 else
27107 it->glyph_not_available_p = true;
27108 it->phys_ascent = it->ascent;
27109 it->phys_descent = it->descent;
27110 it->pixel_width = font->space_width;
27113 if (it->constrain_row_ascent_descent_p)
27115 if (it->descent > it->max_descent)
27117 it->ascent += it->descent - it->max_descent;
27118 it->descent = it->max_descent;
27120 if (it->ascent > it->max_ascent)
27122 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27123 it->ascent = it->max_ascent;
27125 it->phys_ascent = min (it->phys_ascent, it->ascent);
27126 it->phys_descent = min (it->phys_descent, it->descent);
27127 extra_line_spacing = 0;
27130 /* If this is a space inside a region of text with
27131 `space-width' property, change its width. */
27132 bool stretched_p
27133 = it->char_to_display == ' ' && !NILP (it->space_width);
27134 if (stretched_p)
27135 it->pixel_width *= XFLOATINT (it->space_width);
27137 /* If face has a box, add the box thickness to the character
27138 height. If character has a box line to the left and/or
27139 right, add the box line width to the character's width. */
27140 if (face->box != FACE_NO_BOX)
27142 int thick = face->box_line_width;
27144 if (thick > 0)
27146 it->ascent += thick;
27147 it->descent += thick;
27149 else
27150 thick = -thick;
27152 if (it->start_of_box_run_p)
27153 it->pixel_width += thick;
27154 if (it->end_of_box_run_p)
27155 it->pixel_width += thick;
27158 /* If face has an overline, add the height of the overline
27159 (1 pixel) and a 1 pixel margin to the character height. */
27160 if (face->overline_p)
27161 it->ascent += overline_margin;
27163 if (it->constrain_row_ascent_descent_p)
27165 if (it->ascent > it->max_ascent)
27166 it->ascent = it->max_ascent;
27167 if (it->descent > it->max_descent)
27168 it->descent = it->max_descent;
27171 take_vertical_position_into_account (it);
27173 /* If we have to actually produce glyphs, do it. */
27174 if (it->glyph_row)
27176 if (stretched_p)
27178 /* Translate a space with a `space-width' property
27179 into a stretch glyph. */
27180 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27181 / FONT_HEIGHT (font));
27182 append_stretch_glyph (it, it->object, it->pixel_width,
27183 it->ascent + it->descent, ascent);
27185 else
27186 append_glyph (it);
27188 /* If characters with lbearing or rbearing are displayed
27189 in this line, record that fact in a flag of the
27190 glyph row. This is used to optimize X output code. */
27191 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27192 it->glyph_row->contains_overlapping_glyphs_p = true;
27194 if (! stretched_p && it->pixel_width == 0)
27195 /* We assure that all visible glyphs have at least 1-pixel
27196 width. */
27197 it->pixel_width = 1;
27199 else if (it->char_to_display == '\n')
27201 /* A newline has no width, but we need the height of the
27202 line. But if previous part of the line sets a height,
27203 don't increase that height. */
27205 Lisp_Object height;
27206 Lisp_Object total_height = Qnil;
27208 it->override_ascent = -1;
27209 it->pixel_width = 0;
27210 it->nglyphs = 0;
27212 height = get_it_property (it, Qline_height);
27213 /* Split (line-height total-height) list. */
27214 if (CONSP (height)
27215 && CONSP (XCDR (height))
27216 && NILP (XCDR (XCDR (height))))
27218 total_height = XCAR (XCDR (height));
27219 height = XCAR (height);
27221 height = calc_line_height_property (it, height, font, boff, true);
27223 if (it->override_ascent >= 0)
27225 it->ascent = it->override_ascent;
27226 it->descent = it->override_descent;
27227 boff = it->override_boff;
27229 else
27231 if (FONT_TOO_HIGH (font))
27233 it->ascent = font->pixel_size + boff - 1;
27234 it->descent = -boff + 1;
27235 if (it->descent < 0)
27236 it->descent = 0;
27238 else
27240 it->ascent = FONT_BASE (font) + boff;
27241 it->descent = FONT_DESCENT (font) - boff;
27245 if (EQ (height, Qt))
27247 if (it->descent > it->max_descent)
27249 it->ascent += it->descent - it->max_descent;
27250 it->descent = it->max_descent;
27252 if (it->ascent > it->max_ascent)
27254 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27255 it->ascent = it->max_ascent;
27257 it->phys_ascent = min (it->phys_ascent, it->ascent);
27258 it->phys_descent = min (it->phys_descent, it->descent);
27259 it->constrain_row_ascent_descent_p = true;
27260 extra_line_spacing = 0;
27262 else
27264 Lisp_Object spacing;
27266 it->phys_ascent = it->ascent;
27267 it->phys_descent = it->descent;
27269 if ((it->max_ascent > 0 || it->max_descent > 0)
27270 && face->box != FACE_NO_BOX
27271 && face->box_line_width > 0)
27273 it->ascent += face->box_line_width;
27274 it->descent += face->box_line_width;
27276 if (!NILP (height)
27277 && XINT (height) > it->ascent + it->descent)
27278 it->ascent = XINT (height) - it->descent;
27280 if (!NILP (total_height))
27281 spacing = calc_line_height_property (it, total_height, font,
27282 boff, false);
27283 else
27285 spacing = get_it_property (it, Qline_spacing);
27286 spacing = calc_line_height_property (it, spacing, font,
27287 boff, false);
27289 if (INTEGERP (spacing))
27291 extra_line_spacing = XINT (spacing);
27292 if (!NILP (total_height))
27293 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27297 else /* i.e. (it->char_to_display == '\t') */
27299 if (font->space_width > 0)
27301 int tab_width = it->tab_width * font->space_width;
27302 int x = it->current_x + it->continuation_lines_width;
27303 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27305 /* If the distance from the current position to the next tab
27306 stop is less than a space character width, use the
27307 tab stop after that. */
27308 if (next_tab_x - x < font->space_width)
27309 next_tab_x += tab_width;
27311 it->pixel_width = next_tab_x - x;
27312 it->nglyphs = 1;
27313 if (FONT_TOO_HIGH (font))
27315 if (get_char_glyph_code (' ', font, &char2b))
27317 pcm = get_per_char_metric (font, &char2b);
27318 if (pcm->width == 0
27319 && pcm->rbearing == 0 && pcm->lbearing == 0)
27320 pcm = NULL;
27323 if (pcm)
27325 it->ascent = pcm->ascent + boff;
27326 it->descent = pcm->descent - boff;
27328 else
27330 it->ascent = font->pixel_size + boff - 1;
27331 it->descent = -boff + 1;
27333 if (it->ascent < 0)
27334 it->ascent = 0;
27335 if (it->descent < 0)
27336 it->descent = 0;
27338 else
27340 it->ascent = FONT_BASE (font) + boff;
27341 it->descent = FONT_DESCENT (font) - boff;
27343 it->phys_ascent = it->ascent;
27344 it->phys_descent = it->descent;
27346 if (it->glyph_row)
27348 append_stretch_glyph (it, it->object, it->pixel_width,
27349 it->ascent + it->descent, it->ascent);
27352 else
27354 it->pixel_width = 0;
27355 it->nglyphs = 1;
27359 if (FONT_TOO_HIGH (font))
27361 int font_ascent, font_descent;
27363 /* For very large fonts, where we ignore the declared font
27364 dimensions, and go by per-character metrics instead,
27365 don't let the row ascent and descent values (and the row
27366 height computed from them) be smaller than the "normal"
27367 character metrics. This avoids unpleasant effects
27368 whereby lines on display would change their height
27369 depending on which characters are shown. */
27370 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27371 it->max_ascent = max (it->max_ascent, font_ascent);
27372 it->max_descent = max (it->max_descent, font_descent);
27375 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27377 /* A static composition.
27379 Note: A composition is represented as one glyph in the
27380 glyph matrix. There are no padding glyphs.
27382 Important note: pixel_width, ascent, and descent are the
27383 values of what is drawn by draw_glyphs (i.e. the values of
27384 the overall glyphs composed). */
27385 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27386 int boff; /* baseline offset */
27387 struct composition *cmp = composition_table[it->cmp_it.id];
27388 int glyph_len = cmp->glyph_len;
27389 struct font *font = face->font;
27391 it->nglyphs = 1;
27393 /* If we have not yet calculated pixel size data of glyphs of
27394 the composition for the current face font, calculate them
27395 now. Theoretically, we have to check all fonts for the
27396 glyphs, but that requires much time and memory space. So,
27397 here we check only the font of the first glyph. This may
27398 lead to incorrect display, but it's very rare, and C-l
27399 (recenter-top-bottom) can correct the display anyway. */
27400 if (! cmp->font || cmp->font != font)
27402 /* Ascent and descent of the font of the first character
27403 of this composition (adjusted by baseline offset).
27404 Ascent and descent of overall glyphs should not be less
27405 than these, respectively. */
27406 int font_ascent, font_descent, font_height;
27407 /* Bounding box of the overall glyphs. */
27408 int leftmost, rightmost, lowest, highest;
27409 int lbearing, rbearing;
27410 int i, width, ascent, descent;
27411 int c;
27412 XChar2b char2b;
27413 struct font_metrics *pcm;
27414 ptrdiff_t pos;
27416 eassume (0 < glyph_len); /* See Bug#8512. */
27418 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27419 while (c == '\t' && 0 < --glyph_len);
27421 bool right_padded = glyph_len < cmp->glyph_len;
27422 for (i = 0; i < glyph_len; i++)
27424 c = COMPOSITION_GLYPH (cmp, i);
27425 if (c != '\t')
27426 break;
27427 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27429 bool left_padded = i > 0;
27431 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27432 : IT_CHARPOS (*it));
27433 /* If no suitable font is found, use the default font. */
27434 bool font_not_found_p = font == NULL;
27435 if (font_not_found_p)
27437 face = face->ascii_face;
27438 font = face->font;
27440 boff = font->baseline_offset;
27441 if (font->vertical_centering)
27442 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27443 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27444 font_ascent += boff;
27445 font_descent -= boff;
27446 font_height = font_ascent + font_descent;
27448 cmp->font = font;
27450 pcm = NULL;
27451 if (! font_not_found_p)
27453 get_char_face_and_encoding (it->f, c, it->face_id,
27454 &char2b, false);
27455 pcm = get_per_char_metric (font, &char2b);
27458 /* Initialize the bounding box. */
27459 if (pcm)
27461 width = cmp->glyph_len > 0 ? pcm->width : 0;
27462 ascent = pcm->ascent;
27463 descent = pcm->descent;
27464 lbearing = pcm->lbearing;
27465 rbearing = pcm->rbearing;
27467 else
27469 width = cmp->glyph_len > 0 ? font->space_width : 0;
27470 ascent = FONT_BASE (font);
27471 descent = FONT_DESCENT (font);
27472 lbearing = 0;
27473 rbearing = width;
27476 rightmost = width;
27477 leftmost = 0;
27478 lowest = - descent + boff;
27479 highest = ascent + boff;
27481 if (! font_not_found_p
27482 && font->default_ascent
27483 && CHAR_TABLE_P (Vuse_default_ascent)
27484 && !NILP (Faref (Vuse_default_ascent,
27485 make_number (it->char_to_display))))
27486 highest = font->default_ascent + boff;
27488 /* Draw the first glyph at the normal position. It may be
27489 shifted to right later if some other glyphs are drawn
27490 at the left. */
27491 cmp->offsets[i * 2] = 0;
27492 cmp->offsets[i * 2 + 1] = boff;
27493 cmp->lbearing = lbearing;
27494 cmp->rbearing = rbearing;
27496 /* Set cmp->offsets for the remaining glyphs. */
27497 for (i++; i < glyph_len; i++)
27499 int left, right, btm, top;
27500 int ch = COMPOSITION_GLYPH (cmp, i);
27501 int face_id;
27502 struct face *this_face;
27504 if (ch == '\t')
27505 ch = ' ';
27506 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27507 this_face = FACE_FROM_ID (it->f, face_id);
27508 font = this_face->font;
27510 if (font == NULL)
27511 pcm = NULL;
27512 else
27514 get_char_face_and_encoding (it->f, ch, face_id,
27515 &char2b, false);
27516 pcm = get_per_char_metric (font, &char2b);
27518 if (! pcm)
27519 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27520 else
27522 width = pcm->width;
27523 ascent = pcm->ascent;
27524 descent = pcm->descent;
27525 lbearing = pcm->lbearing;
27526 rbearing = pcm->rbearing;
27527 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27529 /* Relative composition with or without
27530 alternate chars. */
27531 left = (leftmost + rightmost - width) / 2;
27532 btm = - descent + boff;
27533 if (font->relative_compose
27534 && (! CHAR_TABLE_P (Vignore_relative_composition)
27535 || NILP (Faref (Vignore_relative_composition,
27536 make_number (ch)))))
27539 if (- descent >= font->relative_compose)
27540 /* One extra pixel between two glyphs. */
27541 btm = highest + 1;
27542 else if (ascent <= 0)
27543 /* One extra pixel between two glyphs. */
27544 btm = lowest - 1 - ascent - descent;
27547 else
27549 /* A composition rule is specified by an integer
27550 value that encodes global and new reference
27551 points (GREF and NREF). GREF and NREF are
27552 specified by numbers as below:
27554 0---1---2 -- ascent
27558 9--10--11 -- center
27560 ---3---4---5--- baseline
27562 6---7---8 -- descent
27564 int rule = COMPOSITION_RULE (cmp, i);
27565 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27567 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27568 grefx = gref % 3, nrefx = nref % 3;
27569 grefy = gref / 3, nrefy = nref / 3;
27570 if (xoff)
27571 xoff = font_height * (xoff - 128) / 256;
27572 if (yoff)
27573 yoff = font_height * (yoff - 128) / 256;
27575 left = (leftmost
27576 + grefx * (rightmost - leftmost) / 2
27577 - nrefx * width / 2
27578 + xoff);
27580 btm = ((grefy == 0 ? highest
27581 : grefy == 1 ? 0
27582 : grefy == 2 ? lowest
27583 : (highest + lowest) / 2)
27584 - (nrefy == 0 ? ascent + descent
27585 : nrefy == 1 ? descent - boff
27586 : nrefy == 2 ? 0
27587 : (ascent + descent) / 2)
27588 + yoff);
27591 cmp->offsets[i * 2] = left;
27592 cmp->offsets[i * 2 + 1] = btm + descent;
27594 /* Update the bounding box of the overall glyphs. */
27595 if (width > 0)
27597 right = left + width;
27598 if (left < leftmost)
27599 leftmost = left;
27600 if (right > rightmost)
27601 rightmost = right;
27603 top = btm + descent + ascent;
27604 if (top > highest)
27605 highest = top;
27606 if (btm < lowest)
27607 lowest = btm;
27609 if (cmp->lbearing > left + lbearing)
27610 cmp->lbearing = left + lbearing;
27611 if (cmp->rbearing < left + rbearing)
27612 cmp->rbearing = left + rbearing;
27616 /* If there are glyphs whose x-offsets are negative,
27617 shift all glyphs to the right and make all x-offsets
27618 non-negative. */
27619 if (leftmost < 0)
27621 for (i = 0; i < cmp->glyph_len; i++)
27622 cmp->offsets[i * 2] -= leftmost;
27623 rightmost -= leftmost;
27624 cmp->lbearing -= leftmost;
27625 cmp->rbearing -= leftmost;
27628 if (left_padded && cmp->lbearing < 0)
27630 for (i = 0; i < cmp->glyph_len; i++)
27631 cmp->offsets[i * 2] -= cmp->lbearing;
27632 rightmost -= cmp->lbearing;
27633 cmp->rbearing -= cmp->lbearing;
27634 cmp->lbearing = 0;
27636 if (right_padded && rightmost < cmp->rbearing)
27638 rightmost = cmp->rbearing;
27641 cmp->pixel_width = rightmost;
27642 cmp->ascent = highest;
27643 cmp->descent = - lowest;
27644 if (cmp->ascent < font_ascent)
27645 cmp->ascent = font_ascent;
27646 if (cmp->descent < font_descent)
27647 cmp->descent = font_descent;
27650 if (it->glyph_row
27651 && (cmp->lbearing < 0
27652 || cmp->rbearing > cmp->pixel_width))
27653 it->glyph_row->contains_overlapping_glyphs_p = true;
27655 it->pixel_width = cmp->pixel_width;
27656 it->ascent = it->phys_ascent = cmp->ascent;
27657 it->descent = it->phys_descent = cmp->descent;
27658 if (face->box != FACE_NO_BOX)
27660 int thick = face->box_line_width;
27662 if (thick > 0)
27664 it->ascent += thick;
27665 it->descent += thick;
27667 else
27668 thick = - thick;
27670 if (it->start_of_box_run_p)
27671 it->pixel_width += thick;
27672 if (it->end_of_box_run_p)
27673 it->pixel_width += thick;
27676 /* If face has an overline, add the height of the overline
27677 (1 pixel) and a 1 pixel margin to the character height. */
27678 if (face->overline_p)
27679 it->ascent += overline_margin;
27681 take_vertical_position_into_account (it);
27682 if (it->ascent < 0)
27683 it->ascent = 0;
27684 if (it->descent < 0)
27685 it->descent = 0;
27687 if (it->glyph_row && cmp->glyph_len > 0)
27688 append_composite_glyph (it);
27690 else if (it->what == IT_COMPOSITION)
27692 /* A dynamic (automatic) composition. */
27693 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27694 Lisp_Object gstring;
27695 struct font_metrics metrics;
27697 it->nglyphs = 1;
27699 gstring = composition_gstring_from_id (it->cmp_it.id);
27700 it->pixel_width
27701 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27702 &metrics);
27703 if (it->glyph_row
27704 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27705 it->glyph_row->contains_overlapping_glyphs_p = true;
27706 it->ascent = it->phys_ascent = metrics.ascent;
27707 it->descent = it->phys_descent = metrics.descent;
27708 if (face->box != FACE_NO_BOX)
27710 int thick = face->box_line_width;
27712 if (thick > 0)
27714 it->ascent += thick;
27715 it->descent += thick;
27717 else
27718 thick = - thick;
27720 if (it->start_of_box_run_p)
27721 it->pixel_width += thick;
27722 if (it->end_of_box_run_p)
27723 it->pixel_width += thick;
27725 /* If face has an overline, add the height of the overline
27726 (1 pixel) and a 1 pixel margin to the character height. */
27727 if (face->overline_p)
27728 it->ascent += overline_margin;
27729 take_vertical_position_into_account (it);
27730 if (it->ascent < 0)
27731 it->ascent = 0;
27732 if (it->descent < 0)
27733 it->descent = 0;
27735 if (it->glyph_row)
27736 append_composite_glyph (it);
27738 else if (it->what == IT_GLYPHLESS)
27739 produce_glyphless_glyph (it, false, Qnil);
27740 else if (it->what == IT_IMAGE)
27741 produce_image_glyph (it);
27742 else if (it->what == IT_STRETCH)
27743 produce_stretch_glyph (it);
27744 else if (it->what == IT_XWIDGET)
27745 produce_xwidget_glyph (it);
27747 done:
27748 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27749 because this isn't true for images with `:ascent 100'. */
27750 eassert (it->ascent >= 0 && it->descent >= 0);
27751 if (it->area == TEXT_AREA)
27752 it->current_x += it->pixel_width;
27754 if (extra_line_spacing > 0)
27756 it->descent += extra_line_spacing;
27757 if (extra_line_spacing > it->max_extra_line_spacing)
27758 it->max_extra_line_spacing = extra_line_spacing;
27761 it->max_ascent = max (it->max_ascent, it->ascent);
27762 it->max_descent = max (it->max_descent, it->descent);
27763 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27764 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27767 /* EXPORT for RIF:
27768 Output LEN glyphs starting at START at the nominal cursor position.
27769 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27770 being updated, and UPDATED_AREA is the area of that row being updated. */
27772 void
27773 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27774 struct glyph *start, enum glyph_row_area updated_area, int len)
27776 int x, hpos, chpos = w->phys_cursor.hpos;
27778 eassert (updated_row);
27779 /* When the window is hscrolled, cursor hpos can legitimately be out
27780 of bounds, but we draw the cursor at the corresponding window
27781 margin in that case. */
27782 if (!updated_row->reversed_p && chpos < 0)
27783 chpos = 0;
27784 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27785 chpos = updated_row->used[TEXT_AREA] - 1;
27787 block_input ();
27789 /* Write glyphs. */
27791 hpos = start - updated_row->glyphs[updated_area];
27792 x = draw_glyphs (w, w->output_cursor.x,
27793 updated_row, updated_area,
27794 hpos, hpos + len,
27795 DRAW_NORMAL_TEXT, 0);
27797 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27798 if (updated_area == TEXT_AREA
27799 && w->phys_cursor_on_p
27800 && w->phys_cursor.vpos == w->output_cursor.vpos
27801 && chpos >= hpos
27802 && chpos < hpos + len)
27803 w->phys_cursor_on_p = false;
27805 unblock_input ();
27807 /* Advance the output cursor. */
27808 w->output_cursor.hpos += len;
27809 w->output_cursor.x = x;
27813 /* EXPORT for RIF:
27814 Insert LEN glyphs from START at the nominal cursor position. */
27816 void
27817 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27818 struct glyph *start, enum glyph_row_area updated_area, int len)
27820 struct frame *f;
27821 int line_height, shift_by_width, shifted_region_width;
27822 struct glyph_row *row;
27823 struct glyph *glyph;
27824 int frame_x, frame_y;
27825 ptrdiff_t hpos;
27827 eassert (updated_row);
27828 block_input ();
27829 f = XFRAME (WINDOW_FRAME (w));
27831 /* Get the height of the line we are in. */
27832 row = updated_row;
27833 line_height = row->height;
27835 /* Get the width of the glyphs to insert. */
27836 shift_by_width = 0;
27837 for (glyph = start; glyph < start + len; ++glyph)
27838 shift_by_width += glyph->pixel_width;
27840 /* Get the width of the region to shift right. */
27841 shifted_region_width = (window_box_width (w, updated_area)
27842 - w->output_cursor.x
27843 - shift_by_width);
27845 /* Shift right. */
27846 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27847 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27849 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27850 line_height, shift_by_width);
27852 /* Write the glyphs. */
27853 hpos = start - row->glyphs[updated_area];
27854 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27855 hpos, hpos + len,
27856 DRAW_NORMAL_TEXT, 0);
27858 /* Advance the output cursor. */
27859 w->output_cursor.hpos += len;
27860 w->output_cursor.x += shift_by_width;
27861 unblock_input ();
27865 /* EXPORT for RIF:
27866 Erase the current text line from the nominal cursor position
27867 (inclusive) to pixel column TO_X (exclusive). The idea is that
27868 everything from TO_X onward is already erased.
27870 TO_X is a pixel position relative to UPDATED_AREA of currently
27871 updated window W. TO_X == -1 means clear to the end of this area. */
27873 void
27874 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27875 enum glyph_row_area updated_area, int to_x)
27877 struct frame *f;
27878 int max_x, min_y, max_y;
27879 int from_x, from_y, to_y;
27881 eassert (updated_row);
27882 f = XFRAME (w->frame);
27884 if (updated_row->full_width_p)
27885 max_x = (WINDOW_PIXEL_WIDTH (w)
27886 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27887 else
27888 max_x = window_box_width (w, updated_area);
27889 max_y = window_text_bottom_y (w);
27891 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27892 of window. For TO_X > 0, truncate to end of drawing area. */
27893 if (to_x == 0)
27894 return;
27895 else if (to_x < 0)
27896 to_x = max_x;
27897 else
27898 to_x = min (to_x, max_x);
27900 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27902 /* Notice if the cursor will be cleared by this operation. */
27903 if (!updated_row->full_width_p)
27904 notice_overwritten_cursor (w, updated_area,
27905 w->output_cursor.x, -1,
27906 updated_row->y,
27907 MATRIX_ROW_BOTTOM_Y (updated_row));
27909 from_x = w->output_cursor.x;
27911 /* Translate to frame coordinates. */
27912 if (updated_row->full_width_p)
27914 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27915 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27917 else
27919 int area_left = window_box_left (w, updated_area);
27920 from_x += area_left;
27921 to_x += area_left;
27924 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27925 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27926 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27928 /* Prevent inadvertently clearing to end of the X window. */
27929 if (to_x > from_x && to_y > from_y)
27931 block_input ();
27932 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27933 to_x - from_x, to_y - from_y);
27934 unblock_input ();
27938 #endif /* HAVE_WINDOW_SYSTEM */
27942 /***********************************************************************
27943 Cursor types
27944 ***********************************************************************/
27946 /* Value is the internal representation of the specified cursor type
27947 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27948 of the bar cursor. */
27950 static enum text_cursor_kinds
27951 get_specified_cursor_type (Lisp_Object arg, int *width)
27953 enum text_cursor_kinds type;
27955 if (NILP (arg))
27956 return NO_CURSOR;
27958 if (EQ (arg, Qbox))
27959 return FILLED_BOX_CURSOR;
27961 if (EQ (arg, Qhollow))
27962 return HOLLOW_BOX_CURSOR;
27964 if (EQ (arg, Qbar))
27966 *width = 2;
27967 return BAR_CURSOR;
27970 if (CONSP (arg)
27971 && EQ (XCAR (arg), Qbar)
27972 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27974 *width = XINT (XCDR (arg));
27975 return BAR_CURSOR;
27978 if (EQ (arg, Qhbar))
27980 *width = 2;
27981 return HBAR_CURSOR;
27984 if (CONSP (arg)
27985 && EQ (XCAR (arg), Qhbar)
27986 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27988 *width = XINT (XCDR (arg));
27989 return HBAR_CURSOR;
27992 /* Treat anything unknown as "hollow box cursor".
27993 It was bad to signal an error; people have trouble fixing
27994 .Xdefaults with Emacs, when it has something bad in it. */
27995 type = HOLLOW_BOX_CURSOR;
27997 return type;
28000 /* Set the default cursor types for specified frame. */
28001 void
28002 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28004 int width = 1;
28005 Lisp_Object tem;
28007 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28008 FRAME_CURSOR_WIDTH (f) = width;
28010 /* By default, set up the blink-off state depending on the on-state. */
28012 tem = Fassoc (arg, Vblink_cursor_alist);
28013 if (!NILP (tem))
28015 FRAME_BLINK_OFF_CURSOR (f)
28016 = get_specified_cursor_type (XCDR (tem), &width);
28017 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28019 else
28020 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28022 /* Make sure the cursor gets redrawn. */
28023 f->cursor_type_changed = true;
28027 #ifdef HAVE_WINDOW_SYSTEM
28029 /* Return the cursor we want to be displayed in window W. Return
28030 width of bar/hbar cursor through WIDTH arg. Return with
28031 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28032 (i.e. if the `system caret' should track this cursor).
28034 In a mini-buffer window, we want the cursor only to appear if we
28035 are reading input from this window. For the selected window, we
28036 want the cursor type given by the frame parameter or buffer local
28037 setting of cursor-type. If explicitly marked off, draw no cursor.
28038 In all other cases, we want a hollow box cursor. */
28040 static enum text_cursor_kinds
28041 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28042 bool *active_cursor)
28044 struct frame *f = XFRAME (w->frame);
28045 struct buffer *b = XBUFFER (w->contents);
28046 int cursor_type = DEFAULT_CURSOR;
28047 Lisp_Object alt_cursor;
28048 bool non_selected = false;
28050 *active_cursor = true;
28052 /* Echo area */
28053 if (cursor_in_echo_area
28054 && FRAME_HAS_MINIBUF_P (f)
28055 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28057 if (w == XWINDOW (echo_area_window))
28059 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28061 *width = FRAME_CURSOR_WIDTH (f);
28062 return FRAME_DESIRED_CURSOR (f);
28064 else
28065 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28068 *active_cursor = false;
28069 non_selected = true;
28072 /* Detect a nonselected window or nonselected frame. */
28073 else if (w != XWINDOW (f->selected_window)
28074 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28076 *active_cursor = false;
28078 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28079 return NO_CURSOR;
28081 non_selected = true;
28084 /* Never display a cursor in a window in which cursor-type is nil. */
28085 if (NILP (BVAR (b, cursor_type)))
28086 return NO_CURSOR;
28088 /* Get the normal cursor type for this window. */
28089 if (EQ (BVAR (b, cursor_type), Qt))
28091 cursor_type = FRAME_DESIRED_CURSOR (f);
28092 *width = FRAME_CURSOR_WIDTH (f);
28094 else
28095 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28097 /* Use cursor-in-non-selected-windows instead
28098 for non-selected window or frame. */
28099 if (non_selected)
28101 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28102 if (!EQ (Qt, alt_cursor))
28103 return get_specified_cursor_type (alt_cursor, width);
28104 /* t means modify the normal cursor type. */
28105 if (cursor_type == FILLED_BOX_CURSOR)
28106 cursor_type = HOLLOW_BOX_CURSOR;
28107 else if (cursor_type == BAR_CURSOR && *width > 1)
28108 --*width;
28109 return cursor_type;
28112 /* Use normal cursor if not blinked off. */
28113 if (!w->cursor_off_p)
28115 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28116 return NO_CURSOR;
28117 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28119 if (cursor_type == FILLED_BOX_CURSOR)
28121 /* Using a block cursor on large images can be very annoying.
28122 So use a hollow cursor for "large" images.
28123 If image is not transparent (no mask), also use hollow cursor. */
28124 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28125 if (img != NULL && IMAGEP (img->spec))
28127 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28128 where N = size of default frame font size.
28129 This should cover most of the "tiny" icons people may use. */
28130 if (!img->mask
28131 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28132 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28133 cursor_type = HOLLOW_BOX_CURSOR;
28136 else if (cursor_type != NO_CURSOR)
28138 /* Display current only supports BOX and HOLLOW cursors for images.
28139 So for now, unconditionally use a HOLLOW cursor when cursor is
28140 not a solid box cursor. */
28141 cursor_type = HOLLOW_BOX_CURSOR;
28144 return cursor_type;
28147 /* Cursor is blinked off, so determine how to "toggle" it. */
28149 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28150 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28151 return get_specified_cursor_type (XCDR (alt_cursor), width);
28153 /* Then see if frame has specified a specific blink off cursor type. */
28154 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28156 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28157 return FRAME_BLINK_OFF_CURSOR (f);
28160 #if false
28161 /* Some people liked having a permanently visible blinking cursor,
28162 while others had very strong opinions against it. So it was
28163 decided to remove it. KFS 2003-09-03 */
28165 /* Finally perform built-in cursor blinking:
28166 filled box <-> hollow box
28167 wide [h]bar <-> narrow [h]bar
28168 narrow [h]bar <-> no cursor
28169 other type <-> no cursor */
28171 if (cursor_type == FILLED_BOX_CURSOR)
28172 return HOLLOW_BOX_CURSOR;
28174 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28176 *width = 1;
28177 return cursor_type;
28179 #endif
28181 return NO_CURSOR;
28185 /* Notice when the text cursor of window W has been completely
28186 overwritten by a drawing operation that outputs glyphs in AREA
28187 starting at X0 and ending at X1 in the line starting at Y0 and
28188 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28189 the rest of the line after X0 has been written. Y coordinates
28190 are window-relative. */
28192 static void
28193 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28194 int x0, int x1, int y0, int y1)
28196 int cx0, cx1, cy0, cy1;
28197 struct glyph_row *row;
28199 if (!w->phys_cursor_on_p)
28200 return;
28201 if (area != TEXT_AREA)
28202 return;
28204 if (w->phys_cursor.vpos < 0
28205 || w->phys_cursor.vpos >= w->current_matrix->nrows
28206 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28207 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28208 return;
28210 if (row->cursor_in_fringe_p)
28212 row->cursor_in_fringe_p = false;
28213 draw_fringe_bitmap (w, row, row->reversed_p);
28214 w->phys_cursor_on_p = false;
28215 return;
28218 cx0 = w->phys_cursor.x;
28219 cx1 = cx0 + w->phys_cursor_width;
28220 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28221 return;
28223 /* The cursor image will be completely removed from the
28224 screen if the output area intersects the cursor area in
28225 y-direction. When we draw in [y0 y1[, and some part of
28226 the cursor is at y < y0, that part must have been drawn
28227 before. When scrolling, the cursor is erased before
28228 actually scrolling, so we don't come here. When not
28229 scrolling, the rows above the old cursor row must have
28230 changed, and in this case these rows must have written
28231 over the cursor image.
28233 Likewise if part of the cursor is below y1, with the
28234 exception of the cursor being in the first blank row at
28235 the buffer and window end because update_text_area
28236 doesn't draw that row. (Except when it does, but
28237 that's handled in update_text_area.) */
28239 cy0 = w->phys_cursor.y;
28240 cy1 = cy0 + w->phys_cursor_height;
28241 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28242 return;
28244 w->phys_cursor_on_p = false;
28247 #endif /* HAVE_WINDOW_SYSTEM */
28250 /************************************************************************
28251 Mouse Face
28252 ************************************************************************/
28254 #ifdef HAVE_WINDOW_SYSTEM
28256 /* EXPORT for RIF:
28257 Fix the display of area AREA of overlapping row ROW in window W
28258 with respect to the overlapping part OVERLAPS. */
28260 void
28261 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28262 enum glyph_row_area area, int overlaps)
28264 int i, x;
28266 block_input ();
28268 x = 0;
28269 for (i = 0; i < row->used[area];)
28271 if (row->glyphs[area][i].overlaps_vertically_p)
28273 int start = i, start_x = x;
28277 x += row->glyphs[area][i].pixel_width;
28278 ++i;
28280 while (i < row->used[area]
28281 && row->glyphs[area][i].overlaps_vertically_p);
28283 draw_glyphs (w, start_x, row, area,
28284 start, i,
28285 DRAW_NORMAL_TEXT, overlaps);
28287 else
28289 x += row->glyphs[area][i].pixel_width;
28290 ++i;
28294 unblock_input ();
28298 /* EXPORT:
28299 Draw the cursor glyph of window W in glyph row ROW. See the
28300 comment of draw_glyphs for the meaning of HL. */
28302 void
28303 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28304 enum draw_glyphs_face hl)
28306 /* If cursor hpos is out of bounds, don't draw garbage. This can
28307 happen in mini-buffer windows when switching between echo area
28308 glyphs and mini-buffer. */
28309 if ((row->reversed_p
28310 ? (w->phys_cursor.hpos >= 0)
28311 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28313 bool on_p = w->phys_cursor_on_p;
28314 int x1;
28315 int hpos = w->phys_cursor.hpos;
28317 /* When the window is hscrolled, cursor hpos can legitimately be
28318 out of bounds, but we draw the cursor at the corresponding
28319 window margin in that case. */
28320 if (!row->reversed_p && hpos < 0)
28321 hpos = 0;
28322 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28323 hpos = row->used[TEXT_AREA] - 1;
28325 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28326 hl, 0);
28327 w->phys_cursor_on_p = on_p;
28329 if (hl == DRAW_CURSOR)
28330 w->phys_cursor_width = x1 - w->phys_cursor.x;
28331 /* When we erase the cursor, and ROW is overlapped by other
28332 rows, make sure that these overlapping parts of other rows
28333 are redrawn. */
28334 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28336 w->phys_cursor_width = x1 - w->phys_cursor.x;
28338 if (row > w->current_matrix->rows
28339 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28340 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28341 OVERLAPS_ERASED_CURSOR);
28343 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28344 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28345 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28346 OVERLAPS_ERASED_CURSOR);
28352 /* Erase the image of a cursor of window W from the screen. */
28354 void
28355 erase_phys_cursor (struct window *w)
28357 struct frame *f = XFRAME (w->frame);
28358 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28359 int hpos = w->phys_cursor.hpos;
28360 int vpos = w->phys_cursor.vpos;
28361 bool mouse_face_here_p = false;
28362 struct glyph_matrix *active_glyphs = w->current_matrix;
28363 struct glyph_row *cursor_row;
28364 struct glyph *cursor_glyph;
28365 enum draw_glyphs_face hl;
28367 /* No cursor displayed or row invalidated => nothing to do on the
28368 screen. */
28369 if (w->phys_cursor_type == NO_CURSOR)
28370 goto mark_cursor_off;
28372 /* VPOS >= active_glyphs->nrows means that window has been resized.
28373 Don't bother to erase the cursor. */
28374 if (vpos >= active_glyphs->nrows)
28375 goto mark_cursor_off;
28377 /* If row containing cursor is marked invalid, there is nothing we
28378 can do. */
28379 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28380 if (!cursor_row->enabled_p)
28381 goto mark_cursor_off;
28383 /* If line spacing is > 0, old cursor may only be partially visible in
28384 window after split-window. So adjust visible height. */
28385 cursor_row->visible_height = min (cursor_row->visible_height,
28386 window_text_bottom_y (w) - cursor_row->y);
28388 /* If row is completely invisible, don't attempt to delete a cursor which
28389 isn't there. This can happen if cursor is at top of a window, and
28390 we switch to a buffer with a header line in that window. */
28391 if (cursor_row->visible_height <= 0)
28392 goto mark_cursor_off;
28394 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28395 if (cursor_row->cursor_in_fringe_p)
28397 cursor_row->cursor_in_fringe_p = false;
28398 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28399 goto mark_cursor_off;
28402 /* This can happen when the new row is shorter than the old one.
28403 In this case, either draw_glyphs or clear_end_of_line
28404 should have cleared the cursor. Note that we wouldn't be
28405 able to erase the cursor in this case because we don't have a
28406 cursor glyph at hand. */
28407 if ((cursor_row->reversed_p
28408 ? (w->phys_cursor.hpos < 0)
28409 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28410 goto mark_cursor_off;
28412 /* When the window is hscrolled, cursor hpos can legitimately be out
28413 of bounds, but we draw the cursor at the corresponding window
28414 margin in that case. */
28415 if (!cursor_row->reversed_p && hpos < 0)
28416 hpos = 0;
28417 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28418 hpos = cursor_row->used[TEXT_AREA] - 1;
28420 /* If the cursor is in the mouse face area, redisplay that when
28421 we clear the cursor. */
28422 if (! NILP (hlinfo->mouse_face_window)
28423 && coords_in_mouse_face_p (w, hpos, vpos)
28424 /* Don't redraw the cursor's spot in mouse face if it is at the
28425 end of a line (on a newline). The cursor appears there, but
28426 mouse highlighting does not. */
28427 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28428 mouse_face_here_p = true;
28430 /* Maybe clear the display under the cursor. */
28431 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28433 int x, y;
28434 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28435 int width;
28437 cursor_glyph = get_phys_cursor_glyph (w);
28438 if (cursor_glyph == NULL)
28439 goto mark_cursor_off;
28441 width = cursor_glyph->pixel_width;
28442 x = w->phys_cursor.x;
28443 if (x < 0)
28445 width += x;
28446 x = 0;
28448 width = min (width, window_box_width (w, TEXT_AREA) - x);
28449 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28450 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28452 if (width > 0)
28453 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28456 /* Erase the cursor by redrawing the character underneath it. */
28457 if (mouse_face_here_p)
28458 hl = DRAW_MOUSE_FACE;
28459 else
28460 hl = DRAW_NORMAL_TEXT;
28461 draw_phys_cursor_glyph (w, cursor_row, hl);
28463 mark_cursor_off:
28464 w->phys_cursor_on_p = false;
28465 w->phys_cursor_type = NO_CURSOR;
28469 /* Display or clear cursor of window W. If !ON, clear the cursor.
28470 If ON, display the cursor; where to put the cursor is specified by
28471 HPOS, VPOS, X and Y. */
28473 void
28474 display_and_set_cursor (struct window *w, bool on,
28475 int hpos, int vpos, int x, int y)
28477 struct frame *f = XFRAME (w->frame);
28478 int new_cursor_type;
28479 int new_cursor_width;
28480 bool active_cursor;
28481 struct glyph_row *glyph_row;
28482 struct glyph *glyph;
28484 /* This is pointless on invisible frames, and dangerous on garbaged
28485 windows and frames; in the latter case, the frame or window may
28486 be in the midst of changing its size, and x and y may be off the
28487 window. */
28488 if (! FRAME_VISIBLE_P (f)
28489 || FRAME_GARBAGED_P (f)
28490 || vpos >= w->current_matrix->nrows
28491 || hpos >= w->current_matrix->matrix_w)
28492 return;
28494 /* If cursor is off and we want it off, return quickly. */
28495 if (!on && !w->phys_cursor_on_p)
28496 return;
28498 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28499 /* If cursor row is not enabled, we don't really know where to
28500 display the cursor. */
28501 if (!glyph_row->enabled_p)
28503 w->phys_cursor_on_p = false;
28504 return;
28507 glyph = NULL;
28508 if (!glyph_row->exact_window_width_line_p
28509 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28510 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28512 eassert (input_blocked_p ());
28514 /* Set new_cursor_type to the cursor we want to be displayed. */
28515 new_cursor_type = get_window_cursor_type (w, glyph,
28516 &new_cursor_width, &active_cursor);
28518 /* If cursor is currently being shown and we don't want it to be or
28519 it is in the wrong place, or the cursor type is not what we want,
28520 erase it. */
28521 if (w->phys_cursor_on_p
28522 && (!on
28523 || w->phys_cursor.x != x
28524 || w->phys_cursor.y != y
28525 /* HPOS can be negative in R2L rows whose
28526 exact_window_width_line_p flag is set (i.e. their newline
28527 would "overflow into the fringe"). */
28528 || hpos < 0
28529 || new_cursor_type != w->phys_cursor_type
28530 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28531 && new_cursor_width != w->phys_cursor_width)))
28532 erase_phys_cursor (w);
28534 /* Don't check phys_cursor_on_p here because that flag is only set
28535 to false in some cases where we know that the cursor has been
28536 completely erased, to avoid the extra work of erasing the cursor
28537 twice. In other words, phys_cursor_on_p can be true and the cursor
28538 still not be visible, or it has only been partly erased. */
28539 if (on)
28541 w->phys_cursor_ascent = glyph_row->ascent;
28542 w->phys_cursor_height = glyph_row->height;
28544 /* Set phys_cursor_.* before x_draw_.* is called because some
28545 of them may need the information. */
28546 w->phys_cursor.x = x;
28547 w->phys_cursor.y = glyph_row->y;
28548 w->phys_cursor.hpos = hpos;
28549 w->phys_cursor.vpos = vpos;
28552 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28553 new_cursor_type, new_cursor_width,
28554 on, active_cursor);
28558 /* Switch the display of W's cursor on or off, according to the value
28559 of ON. */
28561 static void
28562 update_window_cursor (struct window *w, bool on)
28564 /* Don't update cursor in windows whose frame is in the process
28565 of being deleted. */
28566 if (w->current_matrix)
28568 int hpos = w->phys_cursor.hpos;
28569 int vpos = w->phys_cursor.vpos;
28570 struct glyph_row *row;
28572 if (vpos >= w->current_matrix->nrows
28573 || hpos >= w->current_matrix->matrix_w)
28574 return;
28576 row = MATRIX_ROW (w->current_matrix, vpos);
28578 /* When the window is hscrolled, cursor hpos can legitimately be
28579 out of bounds, but we draw the cursor at the corresponding
28580 window margin in that case. */
28581 if (!row->reversed_p && hpos < 0)
28582 hpos = 0;
28583 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28584 hpos = row->used[TEXT_AREA] - 1;
28586 block_input ();
28587 display_and_set_cursor (w, on, hpos, vpos,
28588 w->phys_cursor.x, w->phys_cursor.y);
28589 unblock_input ();
28594 /* Call update_window_cursor with parameter ON_P on all leaf windows
28595 in the window tree rooted at W. */
28597 static void
28598 update_cursor_in_window_tree (struct window *w, bool on_p)
28600 while (w)
28602 if (WINDOWP (w->contents))
28603 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28604 else
28605 update_window_cursor (w, on_p);
28607 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28612 /* EXPORT:
28613 Display the cursor on window W, or clear it, according to ON_P.
28614 Don't change the cursor's position. */
28616 void
28617 x_update_cursor (struct frame *f, bool on_p)
28619 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28623 /* EXPORT:
28624 Clear the cursor of window W to background color, and mark the
28625 cursor as not shown. This is used when the text where the cursor
28626 is about to be rewritten. */
28628 void
28629 x_clear_cursor (struct window *w)
28631 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28632 update_window_cursor (w, false);
28635 #endif /* HAVE_WINDOW_SYSTEM */
28637 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28638 and MSDOS. */
28639 static void
28640 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28641 int start_hpos, int end_hpos,
28642 enum draw_glyphs_face draw)
28644 #ifdef HAVE_WINDOW_SYSTEM
28645 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28647 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28648 return;
28650 #endif
28651 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28652 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28653 #endif
28656 /* Display the active region described by mouse_face_* according to DRAW. */
28658 static void
28659 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28661 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28662 struct frame *f = XFRAME (WINDOW_FRAME (w));
28664 if (/* If window is in the process of being destroyed, don't bother
28665 to do anything. */
28666 w->current_matrix != NULL
28667 /* Don't update mouse highlight if hidden. */
28668 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28669 /* Recognize when we are called to operate on rows that don't exist
28670 anymore. This can happen when a window is split. */
28671 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28673 bool phys_cursor_on_p = w->phys_cursor_on_p;
28674 struct glyph_row *row, *first, *last;
28676 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28677 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28679 for (row = first; row <= last && row->enabled_p; ++row)
28681 int start_hpos, end_hpos, start_x;
28683 /* For all but the first row, the highlight starts at column 0. */
28684 if (row == first)
28686 /* R2L rows have BEG and END in reversed order, but the
28687 screen drawing geometry is always left to right. So
28688 we need to mirror the beginning and end of the
28689 highlighted area in R2L rows. */
28690 if (!row->reversed_p)
28692 start_hpos = hlinfo->mouse_face_beg_col;
28693 start_x = hlinfo->mouse_face_beg_x;
28695 else if (row == last)
28697 start_hpos = hlinfo->mouse_face_end_col;
28698 start_x = hlinfo->mouse_face_end_x;
28700 else
28702 start_hpos = 0;
28703 start_x = 0;
28706 else if (row->reversed_p && row == last)
28708 start_hpos = hlinfo->mouse_face_end_col;
28709 start_x = hlinfo->mouse_face_end_x;
28711 else
28713 start_hpos = 0;
28714 start_x = 0;
28717 if (row == last)
28719 if (!row->reversed_p)
28720 end_hpos = hlinfo->mouse_face_end_col;
28721 else if (row == first)
28722 end_hpos = hlinfo->mouse_face_beg_col;
28723 else
28725 end_hpos = row->used[TEXT_AREA];
28726 if (draw == DRAW_NORMAL_TEXT)
28727 row->fill_line_p = true; /* Clear to end of line. */
28730 else if (row->reversed_p && row == first)
28731 end_hpos = hlinfo->mouse_face_beg_col;
28732 else
28734 end_hpos = row->used[TEXT_AREA];
28735 if (draw == DRAW_NORMAL_TEXT)
28736 row->fill_line_p = true; /* Clear to end of line. */
28739 if (end_hpos > start_hpos)
28741 draw_row_with_mouse_face (w, start_x, row,
28742 start_hpos, end_hpos, draw);
28744 row->mouse_face_p
28745 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28749 /* When we've written over the cursor, arrange for it to
28750 be displayed again. */
28751 if (FRAME_WINDOW_P (f)
28752 && phys_cursor_on_p && !w->phys_cursor_on_p)
28754 #ifdef HAVE_WINDOW_SYSTEM
28755 int hpos = w->phys_cursor.hpos;
28757 /* When the window is hscrolled, cursor hpos can legitimately be
28758 out of bounds, but we draw the cursor at the corresponding
28759 window margin in that case. */
28760 if (!row->reversed_p && hpos < 0)
28761 hpos = 0;
28762 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28763 hpos = row->used[TEXT_AREA] - 1;
28765 block_input ();
28766 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28767 w->phys_cursor.x, w->phys_cursor.y);
28768 unblock_input ();
28769 #endif /* HAVE_WINDOW_SYSTEM */
28773 #ifdef HAVE_WINDOW_SYSTEM
28774 /* Change the mouse cursor. */
28775 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28777 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28778 if (draw == DRAW_NORMAL_TEXT
28779 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28780 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28781 else
28782 #endif
28783 if (draw == DRAW_MOUSE_FACE)
28784 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28785 else
28786 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28788 #endif /* HAVE_WINDOW_SYSTEM */
28791 /* EXPORT:
28792 Clear out the mouse-highlighted active region.
28793 Redraw it un-highlighted first. Value is true if mouse
28794 face was actually drawn unhighlighted. */
28796 bool
28797 clear_mouse_face (Mouse_HLInfo *hlinfo)
28799 bool cleared
28800 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28801 if (cleared)
28802 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28803 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28804 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28805 hlinfo->mouse_face_window = Qnil;
28806 hlinfo->mouse_face_overlay = Qnil;
28807 return cleared;
28810 /* Return true if the coordinates HPOS and VPOS on windows W are
28811 within the mouse face on that window. */
28812 static bool
28813 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28815 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28817 /* Quickly resolve the easy cases. */
28818 if (!(WINDOWP (hlinfo->mouse_face_window)
28819 && XWINDOW (hlinfo->mouse_face_window) == w))
28820 return false;
28821 if (vpos < hlinfo->mouse_face_beg_row
28822 || vpos > hlinfo->mouse_face_end_row)
28823 return false;
28824 if (vpos > hlinfo->mouse_face_beg_row
28825 && vpos < hlinfo->mouse_face_end_row)
28826 return true;
28828 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28830 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28832 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28833 return true;
28835 else if ((vpos == hlinfo->mouse_face_beg_row
28836 && hpos >= hlinfo->mouse_face_beg_col)
28837 || (vpos == hlinfo->mouse_face_end_row
28838 && hpos < hlinfo->mouse_face_end_col))
28839 return true;
28841 else
28843 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28845 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28846 return true;
28848 else if ((vpos == hlinfo->mouse_face_beg_row
28849 && hpos <= hlinfo->mouse_face_beg_col)
28850 || (vpos == hlinfo->mouse_face_end_row
28851 && hpos > hlinfo->mouse_face_end_col))
28852 return true;
28854 return false;
28858 /* EXPORT:
28859 True if physical cursor of window W is within mouse face. */
28861 bool
28862 cursor_in_mouse_face_p (struct window *w)
28864 int hpos = w->phys_cursor.hpos;
28865 int vpos = w->phys_cursor.vpos;
28866 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28868 /* When the window is hscrolled, cursor hpos can legitimately be out
28869 of bounds, but we draw the cursor at the corresponding window
28870 margin in that case. */
28871 if (!row->reversed_p && hpos < 0)
28872 hpos = 0;
28873 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28874 hpos = row->used[TEXT_AREA] - 1;
28876 return coords_in_mouse_face_p (w, hpos, vpos);
28881 /* Find the glyph rows START_ROW and END_ROW of window W that display
28882 characters between buffer positions START_CHARPOS and END_CHARPOS
28883 (excluding END_CHARPOS). DISP_STRING is a display string that
28884 covers these buffer positions. This is similar to
28885 row_containing_pos, but is more accurate when bidi reordering makes
28886 buffer positions change non-linearly with glyph rows. */
28887 static void
28888 rows_from_pos_range (struct window *w,
28889 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28890 Lisp_Object disp_string,
28891 struct glyph_row **start, struct glyph_row **end)
28893 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28894 int last_y = window_text_bottom_y (w);
28895 struct glyph_row *row;
28897 *start = NULL;
28898 *end = NULL;
28900 while (!first->enabled_p
28901 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28902 first++;
28904 /* Find the START row. */
28905 for (row = first;
28906 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28907 row++)
28909 /* A row can potentially be the START row if the range of the
28910 characters it displays intersects the range
28911 [START_CHARPOS..END_CHARPOS). */
28912 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28913 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28914 /* See the commentary in row_containing_pos, for the
28915 explanation of the complicated way to check whether
28916 some position is beyond the end of the characters
28917 displayed by a row. */
28918 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28919 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28920 && !row->ends_at_zv_p
28921 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28922 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28923 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28924 && !row->ends_at_zv_p
28925 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28927 /* Found a candidate row. Now make sure at least one of the
28928 glyphs it displays has a charpos from the range
28929 [START_CHARPOS..END_CHARPOS).
28931 This is not obvious because bidi reordering could make
28932 buffer positions of a row be 1,2,3,102,101,100, and if we
28933 want to highlight characters in [50..60), we don't want
28934 this row, even though [50..60) does intersect [1..103),
28935 the range of character positions given by the row's start
28936 and end positions. */
28937 struct glyph *g = row->glyphs[TEXT_AREA];
28938 struct glyph *e = g + row->used[TEXT_AREA];
28940 while (g < e)
28942 if (((BUFFERP (g->object) || NILP (g->object))
28943 && start_charpos <= g->charpos && g->charpos < end_charpos)
28944 /* A glyph that comes from DISP_STRING is by
28945 definition to be highlighted. */
28946 || EQ (g->object, disp_string))
28947 *start = row;
28948 g++;
28950 if (*start)
28951 break;
28955 /* Find the END row. */
28956 if (!*start
28957 /* If the last row is partially visible, start looking for END
28958 from that row, instead of starting from FIRST. */
28959 && !(row->enabled_p
28960 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28961 row = first;
28962 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28964 struct glyph_row *next = row + 1;
28965 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28967 if (!next->enabled_p
28968 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28969 /* The first row >= START whose range of displayed characters
28970 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28971 is the row END + 1. */
28972 || (start_charpos < next_start
28973 && end_charpos < next_start)
28974 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28975 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28976 && !next->ends_at_zv_p
28977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28978 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28979 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28980 && !next->ends_at_zv_p
28981 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28983 *end = row;
28984 break;
28986 else
28988 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28989 but none of the characters it displays are in the range, it is
28990 also END + 1. */
28991 struct glyph *g = next->glyphs[TEXT_AREA];
28992 struct glyph *s = g;
28993 struct glyph *e = g + next->used[TEXT_AREA];
28995 while (g < e)
28997 if (((BUFFERP (g->object) || NILP (g->object))
28998 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28999 /* If the buffer position of the first glyph in
29000 the row is equal to END_CHARPOS, it means
29001 the last character to be highlighted is the
29002 newline of ROW, and we must consider NEXT as
29003 END, not END+1. */
29004 || (((!next->reversed_p && g == s)
29005 || (next->reversed_p && g == e - 1))
29006 && (g->charpos == end_charpos
29007 /* Special case for when NEXT is an
29008 empty line at ZV. */
29009 || (g->charpos == -1
29010 && !row->ends_at_zv_p
29011 && next_start == end_charpos)))))
29012 /* A glyph that comes from DISP_STRING is by
29013 definition to be highlighted. */
29014 || EQ (g->object, disp_string))
29015 break;
29016 g++;
29018 if (g == e)
29020 *end = row;
29021 break;
29023 /* The first row that ends at ZV must be the last to be
29024 highlighted. */
29025 else if (next->ends_at_zv_p)
29027 *end = next;
29028 break;
29034 /* This function sets the mouse_face_* elements of HLINFO, assuming
29035 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29036 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29037 for the overlay or run of text properties specifying the mouse
29038 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29039 before-string and after-string that must also be highlighted.
29040 DISP_STRING, if non-nil, is a display string that may cover some
29041 or all of the highlighted text. */
29043 static void
29044 mouse_face_from_buffer_pos (Lisp_Object window,
29045 Mouse_HLInfo *hlinfo,
29046 ptrdiff_t mouse_charpos,
29047 ptrdiff_t start_charpos,
29048 ptrdiff_t end_charpos,
29049 Lisp_Object before_string,
29050 Lisp_Object after_string,
29051 Lisp_Object disp_string)
29053 struct window *w = XWINDOW (window);
29054 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29055 struct glyph_row *r1, *r2;
29056 struct glyph *glyph, *end;
29057 ptrdiff_t ignore, pos;
29058 int x;
29060 eassert (NILP (disp_string) || STRINGP (disp_string));
29061 eassert (NILP (before_string) || STRINGP (before_string));
29062 eassert (NILP (after_string) || STRINGP (after_string));
29064 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29065 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29066 if (r1 == NULL)
29067 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29068 /* If the before-string or display-string contains newlines,
29069 rows_from_pos_range skips to its last row. Move back. */
29070 if (!NILP (before_string) || !NILP (disp_string))
29072 struct glyph_row *prev;
29073 while ((prev = r1 - 1, prev >= first)
29074 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29075 && prev->used[TEXT_AREA] > 0)
29077 struct glyph *beg = prev->glyphs[TEXT_AREA];
29078 glyph = beg + prev->used[TEXT_AREA];
29079 while (--glyph >= beg && NILP (glyph->object));
29080 if (glyph < beg
29081 || !(EQ (glyph->object, before_string)
29082 || EQ (glyph->object, disp_string)))
29083 break;
29084 r1 = prev;
29087 if (r2 == NULL)
29089 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29090 hlinfo->mouse_face_past_end = true;
29092 else if (!NILP (after_string))
29094 /* If the after-string has newlines, advance to its last row. */
29095 struct glyph_row *next;
29096 struct glyph_row *last
29097 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29099 for (next = r2 + 1;
29100 next <= last
29101 && next->used[TEXT_AREA] > 0
29102 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29103 ++next)
29104 r2 = next;
29106 /* The rest of the display engine assumes that mouse_face_beg_row is
29107 either above mouse_face_end_row or identical to it. But with
29108 bidi-reordered continued lines, the row for START_CHARPOS could
29109 be below the row for END_CHARPOS. If so, swap the rows and store
29110 them in correct order. */
29111 if (r1->y > r2->y)
29113 struct glyph_row *tem = r2;
29115 r2 = r1;
29116 r1 = tem;
29119 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29120 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29122 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29123 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29124 could be anywhere in the row and in any order. The strategy
29125 below is to find the leftmost and the rightmost glyph that
29126 belongs to either of these 3 strings, or whose position is
29127 between START_CHARPOS and END_CHARPOS, and highlight all the
29128 glyphs between those two. This may cover more than just the text
29129 between START_CHARPOS and END_CHARPOS if the range of characters
29130 strides the bidi level boundary, e.g. if the beginning is in R2L
29131 text while the end is in L2R text or vice versa. */
29132 if (!r1->reversed_p)
29134 /* This row is in a left to right paragraph. Scan it left to
29135 right. */
29136 glyph = r1->glyphs[TEXT_AREA];
29137 end = glyph + r1->used[TEXT_AREA];
29138 x = r1->x;
29140 /* Skip truncation glyphs at the start of the glyph row. */
29141 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29142 for (; glyph < end
29143 && NILP (glyph->object)
29144 && glyph->charpos < 0;
29145 ++glyph)
29146 x += glyph->pixel_width;
29148 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29149 or DISP_STRING, and the first glyph from buffer whose
29150 position is between START_CHARPOS and END_CHARPOS. */
29151 for (; glyph < end
29152 && !NILP (glyph->object)
29153 && !EQ (glyph->object, disp_string)
29154 && !(BUFFERP (glyph->object)
29155 && (glyph->charpos >= start_charpos
29156 && glyph->charpos < end_charpos));
29157 ++glyph)
29159 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29160 are present at buffer positions between START_CHARPOS and
29161 END_CHARPOS, or if they come from an overlay. */
29162 if (EQ (glyph->object, before_string))
29164 pos = string_buffer_position (before_string,
29165 start_charpos);
29166 /* If pos == 0, it means before_string came from an
29167 overlay, not from a buffer position. */
29168 if (!pos || (pos >= start_charpos && pos < end_charpos))
29169 break;
29171 else if (EQ (glyph->object, after_string))
29173 pos = string_buffer_position (after_string, end_charpos);
29174 if (!pos || (pos >= start_charpos && pos < end_charpos))
29175 break;
29177 x += glyph->pixel_width;
29179 hlinfo->mouse_face_beg_x = x;
29180 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29182 else
29184 /* This row is in a right to left paragraph. Scan it right to
29185 left. */
29186 struct glyph *g;
29188 end = r1->glyphs[TEXT_AREA] - 1;
29189 glyph = end + r1->used[TEXT_AREA];
29191 /* Skip truncation glyphs at the start of the glyph row. */
29192 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29193 for (; glyph > end
29194 && NILP (glyph->object)
29195 && glyph->charpos < 0;
29196 --glyph)
29199 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29200 or DISP_STRING, and the first glyph from buffer whose
29201 position is between START_CHARPOS and END_CHARPOS. */
29202 for (; glyph > end
29203 && !NILP (glyph->object)
29204 && !EQ (glyph->object, disp_string)
29205 && !(BUFFERP (glyph->object)
29206 && (glyph->charpos >= start_charpos
29207 && glyph->charpos < end_charpos));
29208 --glyph)
29210 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29211 are present at buffer positions between START_CHARPOS and
29212 END_CHARPOS, or if they come from an overlay. */
29213 if (EQ (glyph->object, before_string))
29215 pos = string_buffer_position (before_string, start_charpos);
29216 /* If pos == 0, it means before_string came from an
29217 overlay, not from a buffer position. */
29218 if (!pos || (pos >= start_charpos && pos < end_charpos))
29219 break;
29221 else if (EQ (glyph->object, after_string))
29223 pos = string_buffer_position (after_string, end_charpos);
29224 if (!pos || (pos >= start_charpos && pos < end_charpos))
29225 break;
29229 glyph++; /* first glyph to the right of the highlighted area */
29230 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29231 x += g->pixel_width;
29232 hlinfo->mouse_face_beg_x = x;
29233 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29236 /* If the highlight ends in a different row, compute GLYPH and END
29237 for the end row. Otherwise, reuse the values computed above for
29238 the row where the highlight begins. */
29239 if (r2 != r1)
29241 if (!r2->reversed_p)
29243 glyph = r2->glyphs[TEXT_AREA];
29244 end = glyph + r2->used[TEXT_AREA];
29245 x = r2->x;
29247 else
29249 end = r2->glyphs[TEXT_AREA] - 1;
29250 glyph = end + r2->used[TEXT_AREA];
29254 if (!r2->reversed_p)
29256 /* Skip truncation and continuation glyphs near the end of the
29257 row, and also blanks and stretch glyphs inserted by
29258 extend_face_to_end_of_line. */
29259 while (end > glyph
29260 && NILP ((end - 1)->object))
29261 --end;
29262 /* Scan the rest of the glyph row from the end, looking for the
29263 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29264 DISP_STRING, or whose position is between START_CHARPOS
29265 and END_CHARPOS */
29266 for (--end;
29267 end > glyph
29268 && !NILP (end->object)
29269 && !EQ (end->object, disp_string)
29270 && !(BUFFERP (end->object)
29271 && (end->charpos >= start_charpos
29272 && end->charpos < end_charpos));
29273 --end)
29275 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29276 are present at buffer positions between START_CHARPOS and
29277 END_CHARPOS, or if they come from an overlay. */
29278 if (EQ (end->object, before_string))
29280 pos = string_buffer_position (before_string, start_charpos);
29281 if (!pos || (pos >= start_charpos && pos < end_charpos))
29282 break;
29284 else if (EQ (end->object, after_string))
29286 pos = string_buffer_position (after_string, end_charpos);
29287 if (!pos || (pos >= start_charpos && pos < end_charpos))
29288 break;
29291 /* Find the X coordinate of the last glyph to be highlighted. */
29292 for (; glyph <= end; ++glyph)
29293 x += glyph->pixel_width;
29295 hlinfo->mouse_face_end_x = x;
29296 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29298 else
29300 /* Skip truncation and continuation glyphs near the end of the
29301 row, and also blanks and stretch glyphs inserted by
29302 extend_face_to_end_of_line. */
29303 x = r2->x;
29304 end++;
29305 while (end < glyph
29306 && NILP (end->object))
29308 x += end->pixel_width;
29309 ++end;
29311 /* Scan the rest of the glyph row from the end, looking for the
29312 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29313 DISP_STRING, or whose position is between START_CHARPOS
29314 and END_CHARPOS */
29315 for ( ;
29316 end < glyph
29317 && !NILP (end->object)
29318 && !EQ (end->object, disp_string)
29319 && !(BUFFERP (end->object)
29320 && (end->charpos >= start_charpos
29321 && end->charpos < end_charpos));
29322 ++end)
29324 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29325 are present at buffer positions between START_CHARPOS and
29326 END_CHARPOS, or if they come from an overlay. */
29327 if (EQ (end->object, before_string))
29329 pos = string_buffer_position (before_string, start_charpos);
29330 if (!pos || (pos >= start_charpos && pos < end_charpos))
29331 break;
29333 else if (EQ (end->object, after_string))
29335 pos = string_buffer_position (after_string, end_charpos);
29336 if (!pos || (pos >= start_charpos && pos < end_charpos))
29337 break;
29339 x += end->pixel_width;
29341 /* If we exited the above loop because we arrived at the last
29342 glyph of the row, and its buffer position is still not in
29343 range, it means the last character in range is the preceding
29344 newline. Bump the end column and x values to get past the
29345 last glyph. */
29346 if (end == glyph
29347 && BUFFERP (end->object)
29348 && (end->charpos < start_charpos
29349 || end->charpos >= end_charpos))
29351 x += end->pixel_width;
29352 ++end;
29354 hlinfo->mouse_face_end_x = x;
29355 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29358 hlinfo->mouse_face_window = window;
29359 hlinfo->mouse_face_face_id
29360 = face_at_buffer_position (w, mouse_charpos, &ignore,
29361 mouse_charpos + 1,
29362 !hlinfo->mouse_face_hidden, -1);
29363 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29366 /* The following function is not used anymore (replaced with
29367 mouse_face_from_string_pos), but I leave it here for the time
29368 being, in case someone would. */
29370 #if false /* not used */
29372 /* Find the position of the glyph for position POS in OBJECT in
29373 window W's current matrix, and return in *X, *Y the pixel
29374 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29376 RIGHT_P means return the position of the right edge of the glyph.
29377 !RIGHT_P means return the left edge position.
29379 If no glyph for POS exists in the matrix, return the position of
29380 the glyph with the next smaller position that is in the matrix, if
29381 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29382 exists in the matrix, return the position of the glyph with the
29383 next larger position in OBJECT.
29385 Value is true if a glyph was found. */
29387 static bool
29388 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29389 int *hpos, int *vpos, int *x, int *y, bool right_p)
29391 int yb = window_text_bottom_y (w);
29392 struct glyph_row *r;
29393 struct glyph *best_glyph = NULL;
29394 struct glyph_row *best_row = NULL;
29395 int best_x = 0;
29397 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29398 r->enabled_p && r->y < yb;
29399 ++r)
29401 struct glyph *g = r->glyphs[TEXT_AREA];
29402 struct glyph *e = g + r->used[TEXT_AREA];
29403 int gx;
29405 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29406 if (EQ (g->object, object))
29408 if (g->charpos == pos)
29410 best_glyph = g;
29411 best_x = gx;
29412 best_row = r;
29413 goto found;
29415 else if (best_glyph == NULL
29416 || ((eabs (g->charpos - pos)
29417 < eabs (best_glyph->charpos - pos))
29418 && (right_p
29419 ? g->charpos < pos
29420 : g->charpos > pos)))
29422 best_glyph = g;
29423 best_x = gx;
29424 best_row = r;
29429 found:
29431 if (best_glyph)
29433 *x = best_x;
29434 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29436 if (right_p)
29438 *x += best_glyph->pixel_width;
29439 ++*hpos;
29442 *y = best_row->y;
29443 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29446 return best_glyph != NULL;
29448 #endif /* not used */
29450 /* Find the positions of the first and the last glyphs in window W's
29451 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29452 (assumed to be a string), and return in HLINFO's mouse_face_*
29453 members the pixel and column/row coordinates of those glyphs. */
29455 static void
29456 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29457 Lisp_Object object,
29458 ptrdiff_t startpos, ptrdiff_t endpos)
29460 int yb = window_text_bottom_y (w);
29461 struct glyph_row *r;
29462 struct glyph *g, *e;
29463 int gx;
29464 bool found = false;
29466 /* Find the glyph row with at least one position in the range
29467 [STARTPOS..ENDPOS), and the first glyph in that row whose
29468 position belongs to that range. */
29469 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29470 r->enabled_p && r->y < yb;
29471 ++r)
29473 if (!r->reversed_p)
29475 g = r->glyphs[TEXT_AREA];
29476 e = g + r->used[TEXT_AREA];
29477 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29478 if (EQ (g->object, object)
29479 && startpos <= g->charpos && g->charpos < endpos)
29481 hlinfo->mouse_face_beg_row
29482 = MATRIX_ROW_VPOS (r, w->current_matrix);
29483 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29484 hlinfo->mouse_face_beg_x = gx;
29485 found = true;
29486 break;
29489 else
29491 struct glyph *g1;
29493 e = r->glyphs[TEXT_AREA];
29494 g = e + r->used[TEXT_AREA];
29495 for ( ; g > e; --g)
29496 if (EQ ((g-1)->object, object)
29497 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29499 hlinfo->mouse_face_beg_row
29500 = MATRIX_ROW_VPOS (r, w->current_matrix);
29501 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29502 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29503 gx += g1->pixel_width;
29504 hlinfo->mouse_face_beg_x = gx;
29505 found = true;
29506 break;
29509 if (found)
29510 break;
29513 if (!found)
29514 return;
29516 /* Starting with the next row, look for the first row which does NOT
29517 include any glyphs whose positions are in the range. */
29518 for (++r; r->enabled_p && r->y < yb; ++r)
29520 g = r->glyphs[TEXT_AREA];
29521 e = g + r->used[TEXT_AREA];
29522 found = false;
29523 for ( ; g < e; ++g)
29524 if (EQ (g->object, object)
29525 && startpos <= g->charpos && g->charpos < endpos)
29527 found = true;
29528 break;
29530 if (!found)
29531 break;
29534 /* The highlighted region ends on the previous row. */
29535 r--;
29537 /* Set the end row. */
29538 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29540 /* Compute and set the end column and the end column's horizontal
29541 pixel coordinate. */
29542 if (!r->reversed_p)
29544 g = r->glyphs[TEXT_AREA];
29545 e = g + r->used[TEXT_AREA];
29546 for ( ; e > g; --e)
29547 if (EQ ((e-1)->object, object)
29548 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29549 break;
29550 hlinfo->mouse_face_end_col = e - g;
29552 for (gx = r->x; g < e; ++g)
29553 gx += g->pixel_width;
29554 hlinfo->mouse_face_end_x = gx;
29556 else
29558 e = r->glyphs[TEXT_AREA];
29559 g = e + r->used[TEXT_AREA];
29560 for (gx = r->x ; e < g; ++e)
29562 if (EQ (e->object, object)
29563 && startpos <= e->charpos && e->charpos < endpos)
29564 break;
29565 gx += e->pixel_width;
29567 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29568 hlinfo->mouse_face_end_x = gx;
29572 #ifdef HAVE_WINDOW_SYSTEM
29574 /* See if position X, Y is within a hot-spot of an image. */
29576 static bool
29577 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29579 if (!CONSP (hot_spot))
29580 return false;
29582 if (EQ (XCAR (hot_spot), Qrect))
29584 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29585 Lisp_Object rect = XCDR (hot_spot);
29586 Lisp_Object tem;
29587 if (!CONSP (rect))
29588 return false;
29589 if (!CONSP (XCAR (rect)))
29590 return false;
29591 if (!CONSP (XCDR (rect)))
29592 return false;
29593 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29594 return false;
29595 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29596 return false;
29597 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29598 return false;
29599 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29600 return false;
29601 return true;
29603 else if (EQ (XCAR (hot_spot), Qcircle))
29605 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29606 Lisp_Object circ = XCDR (hot_spot);
29607 Lisp_Object lr, lx0, ly0;
29608 if (CONSP (circ)
29609 && CONSP (XCAR (circ))
29610 && (lr = XCDR (circ), NUMBERP (lr))
29611 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29612 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29614 double r = XFLOATINT (lr);
29615 double dx = XINT (lx0) - x;
29616 double dy = XINT (ly0) - y;
29617 return (dx * dx + dy * dy <= r * r);
29620 else if (EQ (XCAR (hot_spot), Qpoly))
29622 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29623 if (VECTORP (XCDR (hot_spot)))
29625 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29626 Lisp_Object *poly = v->contents;
29627 ptrdiff_t n = v->header.size;
29628 ptrdiff_t i;
29629 bool inside = false;
29630 Lisp_Object lx, ly;
29631 int x0, y0;
29633 /* Need an even number of coordinates, and at least 3 edges. */
29634 if (n < 6 || n & 1)
29635 return false;
29637 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29638 If count is odd, we are inside polygon. Pixels on edges
29639 may or may not be included depending on actual geometry of the
29640 polygon. */
29641 if ((lx = poly[n-2], !INTEGERP (lx))
29642 || (ly = poly[n-1], !INTEGERP (lx)))
29643 return false;
29644 x0 = XINT (lx), y0 = XINT (ly);
29645 for (i = 0; i < n; i += 2)
29647 int x1 = x0, y1 = y0;
29648 if ((lx = poly[i], !INTEGERP (lx))
29649 || (ly = poly[i+1], !INTEGERP (ly)))
29650 return false;
29651 x0 = XINT (lx), y0 = XINT (ly);
29653 /* Does this segment cross the X line? */
29654 if (x0 >= x)
29656 if (x1 >= x)
29657 continue;
29659 else if (x1 < x)
29660 continue;
29661 if (y > y0 && y > y1)
29662 continue;
29663 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29664 inside = !inside;
29666 return inside;
29669 return false;
29672 Lisp_Object
29673 find_hot_spot (Lisp_Object map, int x, int y)
29675 while (CONSP (map))
29677 if (CONSP (XCAR (map))
29678 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29679 return XCAR (map);
29680 map = XCDR (map);
29683 return Qnil;
29686 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29687 3, 3, 0,
29688 doc: /* Lookup in image map MAP coordinates X and Y.
29689 An image map is an alist where each element has the format (AREA ID PLIST).
29690 An AREA is specified as either a rectangle, a circle, or a polygon:
29691 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29692 pixel coordinates of the upper left and bottom right corners.
29693 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29694 and the radius of the circle; r may be a float or integer.
29695 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29696 vector describes one corner in the polygon.
29697 Returns the alist element for the first matching AREA in MAP. */)
29698 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29700 if (NILP (map))
29701 return Qnil;
29703 CHECK_NUMBER (x);
29704 CHECK_NUMBER (y);
29706 return find_hot_spot (map,
29707 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29708 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29710 #endif /* HAVE_WINDOW_SYSTEM */
29713 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29714 static void
29715 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29717 #ifdef HAVE_WINDOW_SYSTEM
29718 if (!FRAME_WINDOW_P (f))
29719 return;
29721 /* Do not change cursor shape while dragging mouse. */
29722 if (EQ (do_mouse_tracking, Qdragging))
29723 return;
29725 if (!NILP (pointer))
29727 if (EQ (pointer, Qarrow))
29728 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29729 else if (EQ (pointer, Qhand))
29730 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29731 else if (EQ (pointer, Qtext))
29732 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29733 else if (EQ (pointer, intern ("hdrag")))
29734 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29735 else if (EQ (pointer, intern ("nhdrag")))
29736 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29737 # ifdef HAVE_X_WINDOWS
29738 else if (EQ (pointer, intern ("vdrag")))
29739 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29740 # endif
29741 else if (EQ (pointer, intern ("hourglass")))
29742 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29743 else if (EQ (pointer, Qmodeline))
29744 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29745 else
29746 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29749 if (cursor != No_Cursor)
29750 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29751 #endif
29754 /* Take proper action when mouse has moved to the mode or header line
29755 or marginal area AREA of window W, x-position X and y-position Y.
29756 X is relative to the start of the text display area of W, so the
29757 width of bitmap areas and scroll bars must be subtracted to get a
29758 position relative to the start of the mode line. */
29760 static void
29761 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29762 enum window_part area)
29764 struct window *w = XWINDOW (window);
29765 struct frame *f = XFRAME (w->frame);
29766 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29767 #ifdef HAVE_WINDOW_SYSTEM
29768 Display_Info *dpyinfo;
29769 #endif
29770 Cursor cursor = No_Cursor;
29771 Lisp_Object pointer = Qnil;
29772 int dx, dy, width, height;
29773 ptrdiff_t charpos;
29774 Lisp_Object string, object = Qnil;
29775 Lisp_Object pos UNINIT;
29776 Lisp_Object mouse_face;
29777 int original_x_pixel = x;
29778 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29779 struct glyph_row *row UNINIT;
29781 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29783 int x0;
29784 struct glyph *end;
29786 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29787 returns them in row/column units! */
29788 string = mode_line_string (w, area, &x, &y, &charpos,
29789 &object, &dx, &dy, &width, &height);
29791 row = (area == ON_MODE_LINE
29792 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29793 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29795 /* Find the glyph under the mouse pointer. */
29796 if (row->mode_line_p && row->enabled_p)
29798 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29799 end = glyph + row->used[TEXT_AREA];
29801 for (x0 = original_x_pixel;
29802 glyph < end && x0 >= glyph->pixel_width;
29803 ++glyph)
29804 x0 -= glyph->pixel_width;
29806 if (glyph >= end)
29807 glyph = NULL;
29810 else
29812 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29813 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29814 returns them in row/column units! */
29815 string = marginal_area_string (w, area, &x, &y, &charpos,
29816 &object, &dx, &dy, &width, &height);
29819 Lisp_Object help = Qnil;
29821 #ifdef HAVE_WINDOW_SYSTEM
29822 if (IMAGEP (object))
29824 Lisp_Object image_map, hotspot;
29825 if ((image_map = Fplist_get (XCDR (object), QCmap),
29826 !NILP (image_map))
29827 && (hotspot = find_hot_spot (image_map, dx, dy),
29828 CONSP (hotspot))
29829 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29831 Lisp_Object plist;
29833 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29834 If so, we could look for mouse-enter, mouse-leave
29835 properties in PLIST (and do something...). */
29836 hotspot = XCDR (hotspot);
29837 if (CONSP (hotspot)
29838 && (plist = XCAR (hotspot), CONSP (plist)))
29840 pointer = Fplist_get (plist, Qpointer);
29841 if (NILP (pointer))
29842 pointer = Qhand;
29843 help = Fplist_get (plist, Qhelp_echo);
29844 if (!NILP (help))
29846 help_echo_string = help;
29847 XSETWINDOW (help_echo_window, w);
29848 help_echo_object = w->contents;
29849 help_echo_pos = charpos;
29853 if (NILP (pointer))
29854 pointer = Fplist_get (XCDR (object), QCpointer);
29856 #endif /* HAVE_WINDOW_SYSTEM */
29858 if (STRINGP (string))
29859 pos = make_number (charpos);
29861 /* Set the help text and mouse pointer. If the mouse is on a part
29862 of the mode line without any text (e.g. past the right edge of
29863 the mode line text), use the default help text and pointer. */
29864 if (STRINGP (string) || area == ON_MODE_LINE)
29866 /* Arrange to display the help by setting the global variables
29867 help_echo_string, help_echo_object, and help_echo_pos. */
29868 if (NILP (help))
29870 if (STRINGP (string))
29871 help = Fget_text_property (pos, Qhelp_echo, string);
29873 if (!NILP (help))
29875 help_echo_string = help;
29876 XSETWINDOW (help_echo_window, w);
29877 help_echo_object = string;
29878 help_echo_pos = charpos;
29880 else if (area == ON_MODE_LINE)
29882 Lisp_Object default_help
29883 = buffer_local_value (Qmode_line_default_help_echo,
29884 w->contents);
29886 if (STRINGP (default_help))
29888 help_echo_string = default_help;
29889 XSETWINDOW (help_echo_window, w);
29890 help_echo_object = Qnil;
29891 help_echo_pos = -1;
29896 #ifdef HAVE_WINDOW_SYSTEM
29897 /* Change the mouse pointer according to what is under it. */
29898 if (FRAME_WINDOW_P (f))
29900 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29901 || minibuf_level
29902 || NILP (Vresize_mini_windows));
29904 dpyinfo = FRAME_DISPLAY_INFO (f);
29905 if (STRINGP (string))
29907 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29909 if (NILP (pointer))
29910 pointer = Fget_text_property (pos, Qpointer, string);
29912 /* Change the mouse pointer according to what is under X/Y. */
29913 if (NILP (pointer)
29914 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29916 Lisp_Object map;
29917 map = Fget_text_property (pos, Qlocal_map, string);
29918 if (!KEYMAPP (map))
29919 map = Fget_text_property (pos, Qkeymap, string);
29920 if (!KEYMAPP (map) && draggable)
29921 cursor = dpyinfo->vertical_scroll_bar_cursor;
29924 else if (draggable)
29925 /* Default mode-line pointer. */
29926 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29928 #endif
29931 /* Change the mouse face according to what is under X/Y. */
29932 bool mouse_face_shown = false;
29933 if (STRINGP (string))
29935 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29936 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29937 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29938 && glyph)
29940 Lisp_Object b, e;
29942 struct glyph * tmp_glyph;
29944 int gpos;
29945 int gseq_length;
29946 int total_pixel_width;
29947 ptrdiff_t begpos, endpos, ignore;
29949 int vpos, hpos;
29951 b = Fprevious_single_property_change (make_number (charpos + 1),
29952 Qmouse_face, string, Qnil);
29953 if (NILP (b))
29954 begpos = 0;
29955 else
29956 begpos = XINT (b);
29958 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29959 if (NILP (e))
29960 endpos = SCHARS (string);
29961 else
29962 endpos = XINT (e);
29964 /* Calculate the glyph position GPOS of GLYPH in the
29965 displayed string, relative to the beginning of the
29966 highlighted part of the string.
29968 Note: GPOS is different from CHARPOS. CHARPOS is the
29969 position of GLYPH in the internal string object. A mode
29970 line string format has structures which are converted to
29971 a flattened string by the Emacs Lisp interpreter. The
29972 internal string is an element of those structures. The
29973 displayed string is the flattened string. */
29974 tmp_glyph = row_start_glyph;
29975 while (tmp_glyph < glyph
29976 && (!(EQ (tmp_glyph->object, glyph->object)
29977 && begpos <= tmp_glyph->charpos
29978 && tmp_glyph->charpos < endpos)))
29979 tmp_glyph++;
29980 gpos = glyph - tmp_glyph;
29982 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29983 the highlighted part of the displayed string to which
29984 GLYPH belongs. Note: GSEQ_LENGTH is different from
29985 SCHARS (STRING), because the latter returns the length of
29986 the internal string. */
29987 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29988 tmp_glyph > glyph
29989 && (!(EQ (tmp_glyph->object, glyph->object)
29990 && begpos <= tmp_glyph->charpos
29991 && tmp_glyph->charpos < endpos));
29992 tmp_glyph--)
29994 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29996 /* Calculate the total pixel width of all the glyphs between
29997 the beginning of the highlighted area and GLYPH. */
29998 total_pixel_width = 0;
29999 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30000 total_pixel_width += tmp_glyph->pixel_width;
30002 /* Pre calculation of re-rendering position. Note: X is in
30003 column units here, after the call to mode_line_string or
30004 marginal_area_string. */
30005 hpos = x - gpos;
30006 vpos = (area == ON_MODE_LINE
30007 ? (w->current_matrix)->nrows - 1
30008 : 0);
30010 /* If GLYPH's position is included in the region that is
30011 already drawn in mouse face, we have nothing to do. */
30012 if ( EQ (window, hlinfo->mouse_face_window)
30013 && (!row->reversed_p
30014 ? (hlinfo->mouse_face_beg_col <= hpos
30015 && hpos < hlinfo->mouse_face_end_col)
30016 /* In R2L rows we swap BEG and END, see below. */
30017 : (hlinfo->mouse_face_end_col <= hpos
30018 && hpos < hlinfo->mouse_face_beg_col))
30019 && hlinfo->mouse_face_beg_row == vpos )
30020 return;
30022 if (clear_mouse_face (hlinfo))
30023 cursor = No_Cursor;
30025 if (!row->reversed_p)
30027 hlinfo->mouse_face_beg_col = hpos;
30028 hlinfo->mouse_face_beg_x = original_x_pixel
30029 - (total_pixel_width + dx);
30030 hlinfo->mouse_face_end_col = hpos + gseq_length;
30031 hlinfo->mouse_face_end_x = 0;
30033 else
30035 /* In R2L rows, show_mouse_face expects BEG and END
30036 coordinates to be swapped. */
30037 hlinfo->mouse_face_end_col = hpos;
30038 hlinfo->mouse_face_end_x = original_x_pixel
30039 - (total_pixel_width + dx);
30040 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30041 hlinfo->mouse_face_beg_x = 0;
30044 hlinfo->mouse_face_beg_row = vpos;
30045 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30046 hlinfo->mouse_face_past_end = false;
30047 hlinfo->mouse_face_window = window;
30049 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30050 charpos,
30051 0, &ignore,
30052 glyph->face_id,
30053 true);
30054 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30055 mouse_face_shown = true;
30057 if (NILP (pointer))
30058 pointer = Qhand;
30062 /* If mouse-face doesn't need to be shown, clear any existing
30063 mouse-face. */
30064 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30065 clear_mouse_face (hlinfo);
30067 define_frame_cursor1 (f, cursor, pointer);
30071 /* EXPORT:
30072 Take proper action when the mouse has moved to position X, Y on
30073 frame F with regards to highlighting portions of display that have
30074 mouse-face properties. Also de-highlight portions of display where
30075 the mouse was before, set the mouse pointer shape as appropriate
30076 for the mouse coordinates, and activate help echo (tooltips).
30077 X and Y can be negative or out of range. */
30079 void
30080 note_mouse_highlight (struct frame *f, int x, int y)
30082 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30083 enum window_part part = ON_NOTHING;
30084 Lisp_Object window;
30085 struct window *w;
30086 Cursor cursor = No_Cursor;
30087 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30088 struct buffer *b;
30090 /* When a menu is active, don't highlight because this looks odd. */
30091 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30092 if (popup_activated ())
30093 return;
30094 #endif
30096 if (!f->glyphs_initialized_p
30097 || f->pointer_invisible)
30098 return;
30100 hlinfo->mouse_face_mouse_x = x;
30101 hlinfo->mouse_face_mouse_y = y;
30102 hlinfo->mouse_face_mouse_frame = f;
30104 if (hlinfo->mouse_face_defer)
30105 return;
30107 /* Which window is that in? */
30108 window = window_from_coordinates (f, x, y, &part, true);
30110 /* If displaying active text in another window, clear that. */
30111 if (! EQ (window, hlinfo->mouse_face_window)
30112 /* Also clear if we move out of text area in same window. */
30113 || (!NILP (hlinfo->mouse_face_window)
30114 && !NILP (window)
30115 && part != ON_TEXT
30116 && part != ON_MODE_LINE
30117 && part != ON_HEADER_LINE))
30118 clear_mouse_face (hlinfo);
30120 /* Not on a window -> return. */
30121 if (!WINDOWP (window))
30122 return;
30124 /* Reset help_echo_string. It will get recomputed below. */
30125 help_echo_string = Qnil;
30127 /* Convert to window-relative pixel coordinates. */
30128 w = XWINDOW (window);
30129 frame_to_window_pixel_xy (w, &x, &y);
30131 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30132 /* Handle tool-bar window differently since it doesn't display a
30133 buffer. */
30134 if (EQ (window, f->tool_bar_window))
30136 note_tool_bar_highlight (f, x, y);
30137 return;
30139 #endif
30141 /* Mouse is on the mode, header line or margin? */
30142 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30143 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30145 note_mode_line_or_margin_highlight (window, x, y, part);
30147 #ifdef HAVE_WINDOW_SYSTEM
30148 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30150 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30151 /* Show non-text cursor (Bug#16647). */
30152 goto set_cursor;
30154 else
30155 #endif
30156 return;
30159 #ifdef HAVE_WINDOW_SYSTEM
30160 if (part == ON_VERTICAL_BORDER)
30162 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30163 help_echo_string = build_string ("drag-mouse-1: resize");
30165 else if (part == ON_RIGHT_DIVIDER)
30167 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30168 help_echo_string = build_string ("drag-mouse-1: resize");
30170 else if (part == ON_BOTTOM_DIVIDER)
30171 if (! WINDOW_BOTTOMMOST_P (w)
30172 || minibuf_level
30173 || NILP (Vresize_mini_windows))
30175 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30176 help_echo_string = build_string ("drag-mouse-1: resize");
30178 else
30179 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30180 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30181 || part == ON_VERTICAL_SCROLL_BAR
30182 || part == ON_HORIZONTAL_SCROLL_BAR)
30183 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30184 else
30185 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30186 #endif
30188 /* Are we in a window whose display is up to date?
30189 And verify the buffer's text has not changed. */
30190 b = XBUFFER (w->contents);
30191 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30193 int hpos, vpos, dx, dy, area = LAST_AREA;
30194 ptrdiff_t pos;
30195 struct glyph *glyph;
30196 Lisp_Object object;
30197 Lisp_Object mouse_face = Qnil, position;
30198 Lisp_Object *overlay_vec = NULL;
30199 ptrdiff_t i, noverlays;
30200 struct buffer *obuf;
30201 ptrdiff_t obegv, ozv;
30202 bool same_region;
30204 /* Find the glyph under X/Y. */
30205 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30207 #ifdef HAVE_WINDOW_SYSTEM
30208 /* Look for :pointer property on image. */
30209 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30211 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30212 if (img != NULL && IMAGEP (img->spec))
30214 Lisp_Object image_map, hotspot;
30215 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30216 !NILP (image_map))
30217 && (hotspot = find_hot_spot (image_map,
30218 glyph->slice.img.x + dx,
30219 glyph->slice.img.y + dy),
30220 CONSP (hotspot))
30221 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30223 Lisp_Object plist;
30225 /* Could check XCAR (hotspot) to see if we enter/leave
30226 this hot-spot.
30227 If so, we could look for mouse-enter, mouse-leave
30228 properties in PLIST (and do something...). */
30229 hotspot = XCDR (hotspot);
30230 if (CONSP (hotspot)
30231 && (plist = XCAR (hotspot), CONSP (plist)))
30233 pointer = Fplist_get (plist, Qpointer);
30234 if (NILP (pointer))
30235 pointer = Qhand;
30236 help_echo_string = Fplist_get (plist, Qhelp_echo);
30237 if (!NILP (help_echo_string))
30239 help_echo_window = window;
30240 help_echo_object = glyph->object;
30241 help_echo_pos = glyph->charpos;
30245 if (NILP (pointer))
30246 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30249 #endif /* HAVE_WINDOW_SYSTEM */
30251 /* Clear mouse face if X/Y not over text. */
30252 if (glyph == NULL
30253 || area != TEXT_AREA
30254 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30255 /* Glyph's OBJECT is nil for glyphs inserted by the
30256 display engine for its internal purposes, like truncation
30257 and continuation glyphs and blanks beyond the end of
30258 line's text on text terminals. If we are over such a
30259 glyph, we are not over any text. */
30260 || NILP (glyph->object)
30261 /* R2L rows have a stretch glyph at their front, which
30262 stands for no text, whereas L2R rows have no glyphs at
30263 all beyond the end of text. Treat such stretch glyphs
30264 like we do with NULL glyphs in L2R rows. */
30265 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30266 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30267 && glyph->type == STRETCH_GLYPH
30268 && glyph->avoid_cursor_p))
30270 if (clear_mouse_face (hlinfo))
30271 cursor = No_Cursor;
30272 if (FRAME_WINDOW_P (f) && NILP (pointer))
30274 #ifdef HAVE_WINDOW_SYSTEM
30275 if (area != TEXT_AREA)
30276 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30277 else
30278 pointer = Vvoid_text_area_pointer;
30279 #endif
30281 goto set_cursor;
30284 pos = glyph->charpos;
30285 object = glyph->object;
30286 if (!STRINGP (object) && !BUFFERP (object))
30287 goto set_cursor;
30289 /* If we get an out-of-range value, return now; avoid an error. */
30290 if (BUFFERP (object) && pos > BUF_Z (b))
30291 goto set_cursor;
30293 /* Make the window's buffer temporarily current for
30294 overlays_at and compute_char_face. */
30295 obuf = current_buffer;
30296 current_buffer = b;
30297 obegv = BEGV;
30298 ozv = ZV;
30299 BEGV = BEG;
30300 ZV = Z;
30302 /* Is this char mouse-active or does it have help-echo? */
30303 position = make_number (pos);
30305 USE_SAFE_ALLOCA;
30307 if (BUFFERP (object))
30309 /* Put all the overlays we want in a vector in overlay_vec. */
30310 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30311 /* Sort overlays into increasing priority order. */
30312 noverlays = sort_overlays (overlay_vec, noverlays, w);
30314 else
30315 noverlays = 0;
30317 if (NILP (Vmouse_highlight))
30319 clear_mouse_face (hlinfo);
30320 goto check_help_echo;
30323 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30325 if (same_region)
30326 cursor = No_Cursor;
30328 /* Check mouse-face highlighting. */
30329 if (! same_region
30330 /* If there exists an overlay with mouse-face overlapping
30331 the one we are currently highlighting, we have to
30332 check if we enter the overlapping overlay, and then
30333 highlight only that. */
30334 || (OVERLAYP (hlinfo->mouse_face_overlay)
30335 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30337 /* Find the highest priority overlay with a mouse-face. */
30338 Lisp_Object overlay = Qnil;
30339 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30341 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30342 if (!NILP (mouse_face))
30343 overlay = overlay_vec[i];
30346 /* If we're highlighting the same overlay as before, there's
30347 no need to do that again. */
30348 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30349 goto check_help_echo;
30350 hlinfo->mouse_face_overlay = overlay;
30352 /* Clear the display of the old active region, if any. */
30353 if (clear_mouse_face (hlinfo))
30354 cursor = No_Cursor;
30356 /* If no overlay applies, get a text property. */
30357 if (NILP (overlay))
30358 mouse_face = Fget_text_property (position, Qmouse_face, object);
30360 /* Next, compute the bounds of the mouse highlighting and
30361 display it. */
30362 if (!NILP (mouse_face) && STRINGP (object))
30364 /* The mouse-highlighting comes from a display string
30365 with a mouse-face. */
30366 Lisp_Object s, e;
30367 ptrdiff_t ignore;
30369 s = Fprevious_single_property_change
30370 (make_number (pos + 1), Qmouse_face, object, Qnil);
30371 e = Fnext_single_property_change
30372 (position, Qmouse_face, object, Qnil);
30373 if (NILP (s))
30374 s = make_number (0);
30375 if (NILP (e))
30376 e = make_number (SCHARS (object));
30377 mouse_face_from_string_pos (w, hlinfo, object,
30378 XINT (s), XINT (e));
30379 hlinfo->mouse_face_past_end = false;
30380 hlinfo->mouse_face_window = window;
30381 hlinfo->mouse_face_face_id
30382 = face_at_string_position (w, object, pos, 0, &ignore,
30383 glyph->face_id, true);
30384 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30385 cursor = No_Cursor;
30387 else
30389 /* The mouse-highlighting, if any, comes from an overlay
30390 or text property in the buffer. */
30391 Lisp_Object buffer UNINIT;
30392 Lisp_Object disp_string UNINIT;
30394 if (STRINGP (object))
30396 /* If we are on a display string with no mouse-face,
30397 check if the text under it has one. */
30398 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30399 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30400 pos = string_buffer_position (object, start);
30401 if (pos > 0)
30403 mouse_face = get_char_property_and_overlay
30404 (make_number (pos), Qmouse_face, w->contents, &overlay);
30405 buffer = w->contents;
30406 disp_string = object;
30409 else
30411 buffer = object;
30412 disp_string = Qnil;
30415 if (!NILP (mouse_face))
30417 Lisp_Object before, after;
30418 Lisp_Object before_string, after_string;
30419 /* To correctly find the limits of mouse highlight
30420 in a bidi-reordered buffer, we must not use the
30421 optimization of limiting the search in
30422 previous-single-property-change and
30423 next-single-property-change, because
30424 rows_from_pos_range needs the real start and end
30425 positions to DTRT in this case. That's because
30426 the first row visible in a window does not
30427 necessarily display the character whose position
30428 is the smallest. */
30429 Lisp_Object lim1
30430 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30431 ? Fmarker_position (w->start)
30432 : Qnil;
30433 Lisp_Object lim2
30434 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30435 ? make_number (BUF_Z (XBUFFER (buffer))
30436 - w->window_end_pos)
30437 : Qnil;
30439 if (NILP (overlay))
30441 /* Handle the text property case. */
30442 before = Fprevious_single_property_change
30443 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30444 after = Fnext_single_property_change
30445 (make_number (pos), Qmouse_face, buffer, lim2);
30446 before_string = after_string = Qnil;
30448 else
30450 /* Handle the overlay case. */
30451 before = Foverlay_start (overlay);
30452 after = Foverlay_end (overlay);
30453 before_string = Foverlay_get (overlay, Qbefore_string);
30454 after_string = Foverlay_get (overlay, Qafter_string);
30456 if (!STRINGP (before_string)) before_string = Qnil;
30457 if (!STRINGP (after_string)) after_string = Qnil;
30460 mouse_face_from_buffer_pos (window, hlinfo, pos,
30461 NILP (before)
30463 : XFASTINT (before),
30464 NILP (after)
30465 ? BUF_Z (XBUFFER (buffer))
30466 : XFASTINT (after),
30467 before_string, after_string,
30468 disp_string);
30469 cursor = No_Cursor;
30474 check_help_echo:
30476 /* Look for a `help-echo' property. */
30477 if (NILP (help_echo_string)) {
30478 Lisp_Object help, overlay;
30480 /* Check overlays first. */
30481 help = overlay = Qnil;
30482 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30484 overlay = overlay_vec[i];
30485 help = Foverlay_get (overlay, Qhelp_echo);
30488 if (!NILP (help))
30490 help_echo_string = help;
30491 help_echo_window = window;
30492 help_echo_object = overlay;
30493 help_echo_pos = pos;
30495 else
30497 Lisp_Object obj = glyph->object;
30498 ptrdiff_t charpos = glyph->charpos;
30500 /* Try text properties. */
30501 if (STRINGP (obj)
30502 && charpos >= 0
30503 && charpos < SCHARS (obj))
30505 help = Fget_text_property (make_number (charpos),
30506 Qhelp_echo, obj);
30507 if (NILP (help))
30509 /* If the string itself doesn't specify a help-echo,
30510 see if the buffer text ``under'' it does. */
30511 struct glyph_row *r
30512 = MATRIX_ROW (w->current_matrix, vpos);
30513 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30514 ptrdiff_t p = string_buffer_position (obj, start);
30515 if (p > 0)
30517 help = Fget_char_property (make_number (p),
30518 Qhelp_echo, w->contents);
30519 if (!NILP (help))
30521 charpos = p;
30522 obj = w->contents;
30527 else if (BUFFERP (obj)
30528 && charpos >= BEGV
30529 && charpos < ZV)
30530 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30531 obj);
30533 if (!NILP (help))
30535 help_echo_string = help;
30536 help_echo_window = window;
30537 help_echo_object = obj;
30538 help_echo_pos = charpos;
30543 #ifdef HAVE_WINDOW_SYSTEM
30544 /* Look for a `pointer' property. */
30545 if (FRAME_WINDOW_P (f) && NILP (pointer))
30547 /* Check overlays first. */
30548 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30549 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30551 if (NILP (pointer))
30553 Lisp_Object obj = glyph->object;
30554 ptrdiff_t charpos = glyph->charpos;
30556 /* Try text properties. */
30557 if (STRINGP (obj)
30558 && charpos >= 0
30559 && charpos < SCHARS (obj))
30561 pointer = Fget_text_property (make_number (charpos),
30562 Qpointer, obj);
30563 if (NILP (pointer))
30565 /* If the string itself doesn't specify a pointer,
30566 see if the buffer text ``under'' it does. */
30567 struct glyph_row *r
30568 = MATRIX_ROW (w->current_matrix, vpos);
30569 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30570 ptrdiff_t p = string_buffer_position (obj, start);
30571 if (p > 0)
30572 pointer = Fget_char_property (make_number (p),
30573 Qpointer, w->contents);
30576 else if (BUFFERP (obj)
30577 && charpos >= BEGV
30578 && charpos < ZV)
30579 pointer = Fget_text_property (make_number (charpos),
30580 Qpointer, obj);
30583 #endif /* HAVE_WINDOW_SYSTEM */
30585 BEGV = obegv;
30586 ZV = ozv;
30587 current_buffer = obuf;
30588 SAFE_FREE ();
30591 set_cursor:
30592 define_frame_cursor1 (f, cursor, pointer);
30596 /* EXPORT for RIF:
30597 Clear any mouse-face on window W. This function is part of the
30598 redisplay interface, and is called from try_window_id and similar
30599 functions to ensure the mouse-highlight is off. */
30601 void
30602 x_clear_window_mouse_face (struct window *w)
30604 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30605 Lisp_Object window;
30607 block_input ();
30608 XSETWINDOW (window, w);
30609 if (EQ (window, hlinfo->mouse_face_window))
30610 clear_mouse_face (hlinfo);
30611 unblock_input ();
30615 /* EXPORT:
30616 Just discard the mouse face information for frame F, if any.
30617 This is used when the size of F is changed. */
30619 void
30620 cancel_mouse_face (struct frame *f)
30622 Lisp_Object window;
30623 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30625 window = hlinfo->mouse_face_window;
30626 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30627 reset_mouse_highlight (hlinfo);
30632 /***********************************************************************
30633 Exposure Events
30634 ***********************************************************************/
30636 #ifdef HAVE_WINDOW_SYSTEM
30638 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30639 which intersects rectangle R. R is in window-relative coordinates. */
30641 static void
30642 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30643 enum glyph_row_area area)
30645 struct glyph *first = row->glyphs[area];
30646 struct glyph *end = row->glyphs[area] + row->used[area];
30647 struct glyph *last;
30648 int first_x, start_x, x;
30650 if (area == TEXT_AREA && row->fill_line_p)
30651 /* If row extends face to end of line write the whole line. */
30652 draw_glyphs (w, 0, row, area,
30653 0, row->used[area],
30654 DRAW_NORMAL_TEXT, 0);
30655 else
30657 /* Set START_X to the window-relative start position for drawing glyphs of
30658 AREA. The first glyph of the text area can be partially visible.
30659 The first glyphs of other areas cannot. */
30660 start_x = window_box_left_offset (w, area);
30661 x = start_x;
30662 if (area == TEXT_AREA)
30663 x += row->x;
30665 /* Find the first glyph that must be redrawn. */
30666 while (first < end
30667 && x + first->pixel_width < r->x)
30669 x += first->pixel_width;
30670 ++first;
30673 /* Find the last one. */
30674 last = first;
30675 first_x = x;
30676 /* Use a signed int intermediate value to avoid catastrophic
30677 failures due to comparison between signed and unsigned, when
30678 x is negative (can happen for wide images that are hscrolled). */
30679 int r_end = r->x + r->width;
30680 while (last < end && x < r_end)
30682 x += last->pixel_width;
30683 ++last;
30686 /* Repaint. */
30687 if (last > first)
30688 draw_glyphs (w, first_x - start_x, row, area,
30689 first - row->glyphs[area], last - row->glyphs[area],
30690 DRAW_NORMAL_TEXT, 0);
30695 /* Redraw the parts of the glyph row ROW on window W intersecting
30696 rectangle R. R is in window-relative coordinates. Value is
30697 true if mouse-face was overwritten. */
30699 static bool
30700 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30702 eassert (row->enabled_p);
30704 if (row->mode_line_p || w->pseudo_window_p)
30705 draw_glyphs (w, 0, row, TEXT_AREA,
30706 0, row->used[TEXT_AREA],
30707 DRAW_NORMAL_TEXT, 0);
30708 else
30710 if (row->used[LEFT_MARGIN_AREA])
30711 expose_area (w, row, r, LEFT_MARGIN_AREA);
30712 if (row->used[TEXT_AREA])
30713 expose_area (w, row, r, TEXT_AREA);
30714 if (row->used[RIGHT_MARGIN_AREA])
30715 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30716 draw_row_fringe_bitmaps (w, row);
30719 return row->mouse_face_p;
30723 /* Redraw those parts of glyphs rows during expose event handling that
30724 overlap other rows. Redrawing of an exposed line writes over parts
30725 of lines overlapping that exposed line; this function fixes that.
30727 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30728 row in W's current matrix that is exposed and overlaps other rows.
30729 LAST_OVERLAPPING_ROW is the last such row. */
30731 static void
30732 expose_overlaps (struct window *w,
30733 struct glyph_row *first_overlapping_row,
30734 struct glyph_row *last_overlapping_row,
30735 XRectangle *r)
30737 struct glyph_row *row;
30739 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30740 if (row->overlapping_p)
30742 eassert (row->enabled_p && !row->mode_line_p);
30744 row->clip = r;
30745 if (row->used[LEFT_MARGIN_AREA])
30746 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30748 if (row->used[TEXT_AREA])
30749 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30751 if (row->used[RIGHT_MARGIN_AREA])
30752 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30753 row->clip = NULL;
30758 /* Return true if W's cursor intersects rectangle R. */
30760 static bool
30761 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30763 XRectangle cr, result;
30764 struct glyph *cursor_glyph;
30765 struct glyph_row *row;
30767 if (w->phys_cursor.vpos >= 0
30768 && w->phys_cursor.vpos < w->current_matrix->nrows
30769 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30770 row->enabled_p)
30771 && row->cursor_in_fringe_p)
30773 /* Cursor is in the fringe. */
30774 cr.x = window_box_right_offset (w,
30775 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30776 ? RIGHT_MARGIN_AREA
30777 : TEXT_AREA));
30778 cr.y = row->y;
30779 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30780 cr.height = row->height;
30781 return x_intersect_rectangles (&cr, r, &result);
30784 cursor_glyph = get_phys_cursor_glyph (w);
30785 if (cursor_glyph)
30787 /* r is relative to W's box, but w->phys_cursor.x is relative
30788 to left edge of W's TEXT area. Adjust it. */
30789 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30790 cr.y = w->phys_cursor.y;
30791 cr.width = cursor_glyph->pixel_width;
30792 cr.height = w->phys_cursor_height;
30793 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30794 I assume the effect is the same -- and this is portable. */
30795 return x_intersect_rectangles (&cr, r, &result);
30797 /* If we don't understand the format, pretend we're not in the hot-spot. */
30798 return false;
30802 /* EXPORT:
30803 Draw a vertical window border to the right of window W if W doesn't
30804 have vertical scroll bars. */
30806 void
30807 x_draw_vertical_border (struct window *w)
30809 struct frame *f = XFRAME (WINDOW_FRAME (w));
30811 /* We could do better, if we knew what type of scroll-bar the adjacent
30812 windows (on either side) have... But we don't :-(
30813 However, I think this works ok. ++KFS 2003-04-25 */
30815 /* Redraw borders between horizontally adjacent windows. Don't
30816 do it for frames with vertical scroll bars because either the
30817 right scroll bar of a window, or the left scroll bar of its
30818 neighbor will suffice as a border. */
30819 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30820 return;
30822 /* Note: It is necessary to redraw both the left and the right
30823 borders, for when only this single window W is being
30824 redisplayed. */
30825 if (!WINDOW_RIGHTMOST_P (w)
30826 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30828 int x0, x1, y0, y1;
30830 window_box_edges (w, &x0, &y0, &x1, &y1);
30831 y1 -= 1;
30833 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30834 x1 -= 1;
30836 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30839 if (!WINDOW_LEFTMOST_P (w)
30840 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30842 int x0, x1, y0, y1;
30844 window_box_edges (w, &x0, &y0, &x1, &y1);
30845 y1 -= 1;
30847 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30848 x0 -= 1;
30850 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30855 /* Draw window dividers for window W. */
30857 void
30858 x_draw_right_divider (struct window *w)
30860 struct frame *f = WINDOW_XFRAME (w);
30862 if (w->mini || w->pseudo_window_p)
30863 return;
30864 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30866 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30867 int x1 = WINDOW_RIGHT_EDGE_X (w);
30868 int y0 = WINDOW_TOP_EDGE_Y (w);
30869 /* The bottom divider prevails. */
30870 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30872 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30876 static void
30877 x_draw_bottom_divider (struct window *w)
30879 struct frame *f = XFRAME (WINDOW_FRAME (w));
30881 if (w->mini || w->pseudo_window_p)
30882 return;
30883 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30885 int x0 = WINDOW_LEFT_EDGE_X (w);
30886 int x1 = WINDOW_RIGHT_EDGE_X (w);
30887 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30888 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30890 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30894 /* Redraw the part of window W intersection rectangle FR. Pixel
30895 coordinates in FR are frame-relative. Call this function with
30896 input blocked. Value is true if the exposure overwrites
30897 mouse-face. */
30899 static bool
30900 expose_window (struct window *w, XRectangle *fr)
30902 struct frame *f = XFRAME (w->frame);
30903 XRectangle wr, r;
30904 bool mouse_face_overwritten_p = false;
30906 /* If window is not yet fully initialized, do nothing. This can
30907 happen when toolkit scroll bars are used and a window is split.
30908 Reconfiguring the scroll bar will generate an expose for a newly
30909 created window. */
30910 if (w->current_matrix == NULL)
30911 return false;
30913 /* When we're currently updating the window, display and current
30914 matrix usually don't agree. Arrange for a thorough display
30915 later. */
30916 if (w->must_be_updated_p)
30918 SET_FRAME_GARBAGED (f);
30919 return false;
30922 /* Frame-relative pixel rectangle of W. */
30923 wr.x = WINDOW_LEFT_EDGE_X (w);
30924 wr.y = WINDOW_TOP_EDGE_Y (w);
30925 wr.width = WINDOW_PIXEL_WIDTH (w);
30926 wr.height = WINDOW_PIXEL_HEIGHT (w);
30928 if (x_intersect_rectangles (fr, &wr, &r))
30930 int yb = window_text_bottom_y (w);
30931 struct glyph_row *row;
30932 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30934 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30935 r.x, r.y, r.width, r.height));
30937 /* Convert to window coordinates. */
30938 r.x -= WINDOW_LEFT_EDGE_X (w);
30939 r.y -= WINDOW_TOP_EDGE_Y (w);
30941 /* Turn off the cursor. */
30942 bool cursor_cleared_p = (!w->pseudo_window_p
30943 && phys_cursor_in_rect_p (w, &r));
30944 if (cursor_cleared_p)
30945 x_clear_cursor (w);
30947 /* If the row containing the cursor extends face to end of line,
30948 then expose_area might overwrite the cursor outside the
30949 rectangle and thus notice_overwritten_cursor might clear
30950 w->phys_cursor_on_p. We remember the original value and
30951 check later if it is changed. */
30952 bool phys_cursor_on_p = w->phys_cursor_on_p;
30954 /* Use a signed int intermediate value to avoid catastrophic
30955 failures due to comparison between signed and unsigned, when
30956 y0 or y1 is negative (can happen for tall images). */
30957 int r_bottom = r.y + r.height;
30959 /* Update lines intersecting rectangle R. */
30960 first_overlapping_row = last_overlapping_row = NULL;
30961 for (row = w->current_matrix->rows;
30962 row->enabled_p;
30963 ++row)
30965 int y0 = row->y;
30966 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30968 if ((y0 >= r.y && y0 < r_bottom)
30969 || (y1 > r.y && y1 < r_bottom)
30970 || (r.y >= y0 && r.y < y1)
30971 || (r_bottom > y0 && r_bottom < y1))
30973 /* A header line may be overlapping, but there is no need
30974 to fix overlapping areas for them. KFS 2005-02-12 */
30975 if (row->overlapping_p && !row->mode_line_p)
30977 if (first_overlapping_row == NULL)
30978 first_overlapping_row = row;
30979 last_overlapping_row = row;
30982 row->clip = fr;
30983 if (expose_line (w, row, &r))
30984 mouse_face_overwritten_p = true;
30985 row->clip = NULL;
30987 else if (row->overlapping_p)
30989 /* We must redraw a row overlapping the exposed area. */
30990 if (y0 < r.y
30991 ? y0 + row->phys_height > r.y
30992 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30994 if (first_overlapping_row == NULL)
30995 first_overlapping_row = row;
30996 last_overlapping_row = row;
31000 if (y1 >= yb)
31001 break;
31004 /* Display the mode line if there is one. */
31005 if (WINDOW_WANTS_MODELINE_P (w)
31006 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31007 row->enabled_p)
31008 && row->y < r_bottom)
31010 if (expose_line (w, row, &r))
31011 mouse_face_overwritten_p = true;
31014 if (!w->pseudo_window_p)
31016 /* Fix the display of overlapping rows. */
31017 if (first_overlapping_row)
31018 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31019 fr);
31021 /* Draw border between windows. */
31022 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31023 x_draw_right_divider (w);
31024 else
31025 x_draw_vertical_border (w);
31027 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31028 x_draw_bottom_divider (w);
31030 /* Turn the cursor on again. */
31031 if (cursor_cleared_p
31032 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31033 update_window_cursor (w, true);
31037 return mouse_face_overwritten_p;
31042 /* Redraw (parts) of all windows in the window tree rooted at W that
31043 intersect R. R contains frame pixel coordinates. Value is
31044 true if the exposure overwrites mouse-face. */
31046 static bool
31047 expose_window_tree (struct window *w, XRectangle *r)
31049 struct frame *f = XFRAME (w->frame);
31050 bool mouse_face_overwritten_p = false;
31052 while (w && !FRAME_GARBAGED_P (f))
31054 mouse_face_overwritten_p
31055 |= (WINDOWP (w->contents)
31056 ? expose_window_tree (XWINDOW (w->contents), r)
31057 : expose_window (w, r));
31059 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31062 return mouse_face_overwritten_p;
31066 /* EXPORT:
31067 Redisplay an exposed area of frame F. X and Y are the upper-left
31068 corner of the exposed rectangle. W and H are width and height of
31069 the exposed area. All are pixel values. W or H zero means redraw
31070 the entire frame. */
31072 void
31073 expose_frame (struct frame *f, int x, int y, int w, int h)
31075 XRectangle r;
31076 bool mouse_face_overwritten_p = false;
31078 TRACE ((stderr, "expose_frame "));
31080 /* No need to redraw if frame will be redrawn soon. */
31081 if (FRAME_GARBAGED_P (f))
31083 TRACE ((stderr, " garbaged\n"));
31084 return;
31087 /* If basic faces haven't been realized yet, there is no point in
31088 trying to redraw anything. This can happen when we get an expose
31089 event while Emacs is starting, e.g. by moving another window. */
31090 if (FRAME_FACE_CACHE (f) == NULL
31091 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31093 TRACE ((stderr, " no faces\n"));
31094 return;
31097 if (w == 0 || h == 0)
31099 r.x = r.y = 0;
31100 r.width = FRAME_TEXT_WIDTH (f);
31101 r.height = FRAME_TEXT_HEIGHT (f);
31103 else
31105 r.x = x;
31106 r.y = y;
31107 r.width = w;
31108 r.height = h;
31111 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31112 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31114 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31115 if (WINDOWP (f->tool_bar_window))
31116 mouse_face_overwritten_p
31117 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31118 #endif
31120 #ifdef HAVE_X_WINDOWS
31121 #ifndef MSDOS
31122 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31123 if (WINDOWP (f->menu_bar_window))
31124 mouse_face_overwritten_p
31125 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31126 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31127 #endif
31128 #endif
31130 /* Some window managers support a focus-follows-mouse style with
31131 delayed raising of frames. Imagine a partially obscured frame,
31132 and moving the mouse into partially obscured mouse-face on that
31133 frame. The visible part of the mouse-face will be highlighted,
31134 then the WM raises the obscured frame. With at least one WM, KDE
31135 2.1, Emacs is not getting any event for the raising of the frame
31136 (even tried with SubstructureRedirectMask), only Expose events.
31137 These expose events will draw text normally, i.e. not
31138 highlighted. Which means we must redo the highlight here.
31139 Subsume it under ``we love X''. --gerd 2001-08-15 */
31140 /* Included in Windows version because Windows most likely does not
31141 do the right thing if any third party tool offers
31142 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31143 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31145 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31146 if (f == hlinfo->mouse_face_mouse_frame)
31148 int mouse_x = hlinfo->mouse_face_mouse_x;
31149 int mouse_y = hlinfo->mouse_face_mouse_y;
31150 clear_mouse_face (hlinfo);
31151 note_mouse_highlight (f, mouse_x, mouse_y);
31157 /* EXPORT:
31158 Determine the intersection of two rectangles R1 and R2. Return
31159 the intersection in *RESULT. Value is true if RESULT is not
31160 empty. */
31162 bool
31163 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31165 XRectangle *left, *right;
31166 XRectangle *upper, *lower;
31167 bool intersection_p = false;
31169 /* Rearrange so that R1 is the left-most rectangle. */
31170 if (r1->x < r2->x)
31171 left = r1, right = r2;
31172 else
31173 left = r2, right = r1;
31175 /* X0 of the intersection is right.x0, if this is inside R1,
31176 otherwise there is no intersection. */
31177 if (right->x <= left->x + left->width)
31179 result->x = right->x;
31181 /* The right end of the intersection is the minimum of
31182 the right ends of left and right. */
31183 result->width = (min (left->x + left->width, right->x + right->width)
31184 - result->x);
31186 /* Same game for Y. */
31187 if (r1->y < r2->y)
31188 upper = r1, lower = r2;
31189 else
31190 upper = r2, lower = r1;
31192 /* The upper end of the intersection is lower.y0, if this is inside
31193 of upper. Otherwise, there is no intersection. */
31194 if (lower->y <= upper->y + upper->height)
31196 result->y = lower->y;
31198 /* The lower end of the intersection is the minimum of the lower
31199 ends of upper and lower. */
31200 result->height = (min (lower->y + lower->height,
31201 upper->y + upper->height)
31202 - result->y);
31203 intersection_p = true;
31207 return intersection_p;
31210 #endif /* HAVE_WINDOW_SYSTEM */
31213 /***********************************************************************
31214 Initialization
31215 ***********************************************************************/
31217 void
31218 syms_of_xdisp (void)
31220 Vwith_echo_area_save_vector = Qnil;
31221 staticpro (&Vwith_echo_area_save_vector);
31223 Vmessage_stack = Qnil;
31224 staticpro (&Vmessage_stack);
31226 /* Non-nil means don't actually do any redisplay. */
31227 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31229 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31231 DEFVAR_BOOL("inhibit-message", inhibit_message,
31232 doc: /* Non-nil means calls to `message' are not displayed.
31233 They are still logged to the *Messages* buffer. */);
31234 inhibit_message = 0;
31236 message_dolog_marker1 = Fmake_marker ();
31237 staticpro (&message_dolog_marker1);
31238 message_dolog_marker2 = Fmake_marker ();
31239 staticpro (&message_dolog_marker2);
31240 message_dolog_marker3 = Fmake_marker ();
31241 staticpro (&message_dolog_marker3);
31243 #ifdef GLYPH_DEBUG
31244 defsubr (&Sdump_frame_glyph_matrix);
31245 defsubr (&Sdump_glyph_matrix);
31246 defsubr (&Sdump_glyph_row);
31247 defsubr (&Sdump_tool_bar_row);
31248 defsubr (&Strace_redisplay);
31249 defsubr (&Strace_to_stderr);
31250 #endif
31251 #ifdef HAVE_WINDOW_SYSTEM
31252 defsubr (&Stool_bar_height);
31253 defsubr (&Slookup_image_map);
31254 #endif
31255 defsubr (&Sline_pixel_height);
31256 defsubr (&Sformat_mode_line);
31257 defsubr (&Sinvisible_p);
31258 defsubr (&Scurrent_bidi_paragraph_direction);
31259 defsubr (&Swindow_text_pixel_size);
31260 defsubr (&Smove_point_visually);
31261 defsubr (&Sbidi_find_overridden_directionality);
31263 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31264 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31265 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31266 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31267 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31268 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31269 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31270 DEFSYM (Qeval, "eval");
31271 DEFSYM (QCdata, ":data");
31273 /* Names of text properties relevant for redisplay. */
31274 DEFSYM (Qdisplay, "display");
31275 DEFSYM (Qspace_width, "space-width");
31276 DEFSYM (Qraise, "raise");
31277 DEFSYM (Qslice, "slice");
31278 DEFSYM (Qspace, "space");
31279 DEFSYM (Qmargin, "margin");
31280 DEFSYM (Qpointer, "pointer");
31281 DEFSYM (Qleft_margin, "left-margin");
31282 DEFSYM (Qright_margin, "right-margin");
31283 DEFSYM (Qcenter, "center");
31284 DEFSYM (Qline_height, "line-height");
31285 DEFSYM (QCalign_to, ":align-to");
31286 DEFSYM (QCrelative_width, ":relative-width");
31287 DEFSYM (QCrelative_height, ":relative-height");
31288 DEFSYM (QCeval, ":eval");
31289 DEFSYM (QCpropertize, ":propertize");
31290 DEFSYM (QCfile, ":file");
31291 DEFSYM (Qfontified, "fontified");
31292 DEFSYM (Qfontification_functions, "fontification-functions");
31294 /* Name of the face used to highlight trailing whitespace. */
31295 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31297 /* Name and number of the face used to highlight escape glyphs. */
31298 DEFSYM (Qescape_glyph, "escape-glyph");
31300 /* Name and number of the face used to highlight non-breaking
31301 spaces/hyphens. */
31302 DEFSYM (Qnobreak_space, "nobreak-space");
31303 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31305 /* The symbol 'image' which is the car of the lists used to represent
31306 images in Lisp. Also a tool bar style. */
31307 DEFSYM (Qimage, "image");
31309 /* Tool bar styles. */
31310 DEFSYM (Qtext, "text");
31311 DEFSYM (Qboth, "both");
31312 DEFSYM (Qboth_horiz, "both-horiz");
31313 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31315 /* The image map types. */
31316 DEFSYM (QCmap, ":map");
31317 DEFSYM (QCpointer, ":pointer");
31318 DEFSYM (Qrect, "rect");
31319 DEFSYM (Qcircle, "circle");
31320 DEFSYM (Qpoly, "poly");
31322 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31324 DEFSYM (Qgrow_only, "grow-only");
31325 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31326 DEFSYM (Qposition, "position");
31327 DEFSYM (Qbuffer_position, "buffer-position");
31328 DEFSYM (Qobject, "object");
31330 /* Cursor shapes. */
31331 DEFSYM (Qbar, "bar");
31332 DEFSYM (Qhbar, "hbar");
31333 DEFSYM (Qbox, "box");
31334 DEFSYM (Qhollow, "hollow");
31336 /* Pointer shapes. */
31337 DEFSYM (Qhand, "hand");
31338 DEFSYM (Qarrow, "arrow");
31339 /* also Qtext */
31341 DEFSYM (Qdragging, "dragging");
31343 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31345 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31346 staticpro (&list_of_error);
31348 /* Values of those variables at last redisplay are stored as
31349 properties on 'overlay-arrow-position' symbol. However, if
31350 Voverlay_arrow_position is a marker, last-arrow-position is its
31351 numerical position. */
31352 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31353 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31355 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31356 properties on a symbol in overlay-arrow-variable-list. */
31357 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31358 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31360 echo_buffer[0] = echo_buffer[1] = Qnil;
31361 staticpro (&echo_buffer[0]);
31362 staticpro (&echo_buffer[1]);
31364 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31365 staticpro (&echo_area_buffer[0]);
31366 staticpro (&echo_area_buffer[1]);
31368 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31369 staticpro (&Vmessages_buffer_name);
31371 mode_line_proptrans_alist = Qnil;
31372 staticpro (&mode_line_proptrans_alist);
31373 mode_line_string_list = Qnil;
31374 staticpro (&mode_line_string_list);
31375 mode_line_string_face = Qnil;
31376 staticpro (&mode_line_string_face);
31377 mode_line_string_face_prop = Qnil;
31378 staticpro (&mode_line_string_face_prop);
31379 Vmode_line_unwind_vector = Qnil;
31380 staticpro (&Vmode_line_unwind_vector);
31382 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31384 help_echo_string = Qnil;
31385 staticpro (&help_echo_string);
31386 help_echo_object = Qnil;
31387 staticpro (&help_echo_object);
31388 help_echo_window = Qnil;
31389 staticpro (&help_echo_window);
31390 previous_help_echo_string = Qnil;
31391 staticpro (&previous_help_echo_string);
31392 help_echo_pos = -1;
31394 DEFSYM (Qright_to_left, "right-to-left");
31395 DEFSYM (Qleft_to_right, "left-to-right");
31396 defsubr (&Sbidi_resolved_levels);
31398 #ifdef HAVE_WINDOW_SYSTEM
31399 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31400 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31401 For example, if a block cursor is over a tab, it will be drawn as
31402 wide as that tab on the display. */);
31403 x_stretch_cursor_p = 0;
31404 #endif
31406 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31407 doc: /* Non-nil means highlight trailing whitespace.
31408 The face used for trailing whitespace is `trailing-whitespace'. */);
31409 Vshow_trailing_whitespace = Qnil;
31411 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31412 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31413 If the value is t, Emacs highlights non-ASCII chars which have the
31414 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31415 or `nobreak-hyphen' face respectively.
31417 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31418 U+2011 (non-breaking hyphen) are affected.
31420 Any other non-nil value means to display these characters as a escape
31421 glyph followed by an ordinary space or hyphen.
31423 A value of nil means no special handling of these characters. */);
31424 Vnobreak_char_display = Qt;
31426 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31427 doc: /* The pointer shape to show in void text areas.
31428 A value of nil means to show the text pointer. Other options are
31429 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31430 `hourglass'. */);
31431 Vvoid_text_area_pointer = Qarrow;
31433 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31434 doc: /* Non-nil means don't actually do any redisplay.
31435 This is used for internal purposes. */);
31436 Vinhibit_redisplay = Qnil;
31438 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31439 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31440 Vglobal_mode_string = Qnil;
31442 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31443 doc: /* Marker for where to display an arrow on top of the buffer text.
31444 This must be the beginning of a line in order to work.
31445 See also `overlay-arrow-string'. */);
31446 Voverlay_arrow_position = Qnil;
31448 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31449 doc: /* String to display as an arrow in non-window frames.
31450 See also `overlay-arrow-position'. */);
31451 Voverlay_arrow_string = build_pure_c_string ("=>");
31453 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31454 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31455 The symbols on this list are examined during redisplay to determine
31456 where to display overlay arrows. */);
31457 Voverlay_arrow_variable_list
31458 = list1 (intern_c_string ("overlay-arrow-position"));
31460 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31461 doc: /* The number of lines to try scrolling a window by when point moves out.
31462 If that fails to bring point back on frame, point is centered instead.
31463 If this is zero, point is always centered after it moves off frame.
31464 If you want scrolling to always be a line at a time, you should set
31465 `scroll-conservatively' to a large value rather than set this to 1. */);
31467 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31468 doc: /* Scroll up to this many lines, to bring point back on screen.
31469 If point moves off-screen, redisplay will scroll by up to
31470 `scroll-conservatively' lines in order to bring point just barely
31471 onto the screen again. If that cannot be done, then redisplay
31472 recenters point as usual.
31474 If the value is greater than 100, redisplay will never recenter point,
31475 but will always scroll just enough text to bring point into view, even
31476 if you move far away.
31478 A value of zero means always recenter point if it moves off screen. */);
31479 scroll_conservatively = 0;
31481 DEFVAR_INT ("scroll-margin", scroll_margin,
31482 doc: /* Number of lines of margin at the top and bottom of a window.
31483 Recenter the window whenever point gets within this many lines
31484 of the top or bottom of the window. */);
31485 scroll_margin = 0;
31487 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31488 doc: /* Pixels per inch value for non-window system displays.
31489 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31490 Vdisplay_pixels_per_inch = make_float (72.0);
31492 #ifdef GLYPH_DEBUG
31493 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31494 #endif
31496 DEFVAR_LISP ("truncate-partial-width-windows",
31497 Vtruncate_partial_width_windows,
31498 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31499 For an integer value, truncate lines in each window narrower than the
31500 full frame width, provided the total window width in column units is less
31501 than that integer; otherwise, respect the value of `truncate-lines'.
31502 The total width of the window is as returned by `window-total-width', it
31503 includes the fringes, the continuation and truncation glyphs, the
31504 display margins (if any), and the scroll bar
31506 For any other non-nil value, truncate lines in all windows that do
31507 not span the full frame width.
31509 A value of nil means to respect the value of `truncate-lines'.
31511 If `word-wrap' is enabled, you might want to reduce this. */);
31512 Vtruncate_partial_width_windows = make_number (50);
31514 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31515 doc: /* Maximum buffer size for which line number should be displayed.
31516 If the buffer is bigger than this, the line number does not appear
31517 in the mode line. A value of nil means no limit. */);
31518 Vline_number_display_limit = Qnil;
31520 DEFVAR_INT ("line-number-display-limit-width",
31521 line_number_display_limit_width,
31522 doc: /* Maximum line width (in characters) for line number display.
31523 If the average length of the lines near point is bigger than this, then the
31524 line number may be omitted from the mode line. */);
31525 line_number_display_limit_width = 200;
31527 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31528 doc: /* Non-nil means highlight region even in nonselected windows. */);
31529 highlight_nonselected_windows = false;
31531 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31532 doc: /* Non-nil if more than one frame is visible on this display.
31533 Minibuffer-only frames don't count, but iconified frames do.
31534 This variable is not guaranteed to be accurate except while processing
31535 `frame-title-format' and `icon-title-format'. */);
31537 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31538 doc: /* Template for displaying the title bar of visible frames.
31539 \(Assuming the window manager supports this feature.)
31541 This variable has the same structure as `mode-line-format', except that
31542 the %c and %l constructs are ignored. It is used only on frames for
31543 which no explicit name has been set (see `modify-frame-parameters'). */);
31545 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31546 doc: /* Template for displaying the title bar of an iconified frame.
31547 \(Assuming the window manager supports this feature.)
31548 This variable has the same structure as `mode-line-format' (which see),
31549 and is used only on frames for which no explicit name has been set
31550 \(see `modify-frame-parameters'). */);
31551 Vicon_title_format
31552 = Vframe_title_format
31553 = listn (CONSTYPE_PURE, 3,
31554 intern_c_string ("multiple-frames"),
31555 build_pure_c_string ("%b"),
31556 listn (CONSTYPE_PURE, 4,
31557 empty_unibyte_string,
31558 intern_c_string ("invocation-name"),
31559 build_pure_c_string ("@"),
31560 intern_c_string ("system-name")));
31562 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31563 doc: /* Maximum number of lines to keep in the message log buffer.
31564 If nil, disable message logging. If t, log messages but don't truncate
31565 the buffer when it becomes large. */);
31566 Vmessage_log_max = make_number (1000);
31568 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31569 doc: /* List of functions to call before redisplaying a window with scrolling.
31570 Each function is called with two arguments, the window and its new
31571 display-start position.
31572 These functions are called whenever the `window-start' marker is modified,
31573 either to point into another buffer (e.g. via `set-window-buffer') or another
31574 place in the same buffer.
31575 Note that the value of `window-end' is not valid when these functions are
31576 called.
31578 Warning: Do not use this feature to alter the way the window
31579 is scrolled. It is not designed for that, and such use probably won't
31580 work. */);
31581 Vwindow_scroll_functions = Qnil;
31583 DEFVAR_LISP ("window-text-change-functions",
31584 Vwindow_text_change_functions,
31585 doc: /* Functions to call in redisplay when text in the window might change. */);
31586 Vwindow_text_change_functions = Qnil;
31588 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31589 doc: /* Functions called when redisplay of a window reaches the end trigger.
31590 Each function is called with two arguments, the window and the end trigger value.
31591 See `set-window-redisplay-end-trigger'. */);
31592 Vredisplay_end_trigger_functions = Qnil;
31594 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31595 doc: /* Non-nil means autoselect window with mouse pointer.
31596 If nil, do not autoselect windows.
31597 A positive number means delay autoselection by that many seconds: a
31598 window is autoselected only after the mouse has remained in that
31599 window for the duration of the delay.
31600 A negative number has a similar effect, but causes windows to be
31601 autoselected only after the mouse has stopped moving. (Because of
31602 the way Emacs compares mouse events, you will occasionally wait twice
31603 that time before the window gets selected.)
31604 Any other value means to autoselect window instantaneously when the
31605 mouse pointer enters it.
31607 Autoselection selects the minibuffer only if it is active, and never
31608 unselects the minibuffer if it is active.
31610 When customizing this variable make sure that the actual value of
31611 `focus-follows-mouse' matches the behavior of your window manager. */);
31612 Vmouse_autoselect_window = Qnil;
31614 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31615 doc: /* Non-nil means automatically resize tool-bars.
31616 This dynamically changes the tool-bar's height to the minimum height
31617 that is needed to make all tool-bar items visible.
31618 If value is `grow-only', the tool-bar's height is only increased
31619 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31620 Vauto_resize_tool_bars = Qt;
31622 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31623 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31624 auto_raise_tool_bar_buttons_p = true;
31626 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31627 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31628 make_cursor_line_fully_visible_p = true;
31630 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31631 doc: /* Border below tool-bar in pixels.
31632 If an integer, use it as the height of the border.
31633 If it is one of `internal-border-width' or `border-width', use the
31634 value of the corresponding frame parameter.
31635 Otherwise, no border is added below the tool-bar. */);
31636 Vtool_bar_border = Qinternal_border_width;
31638 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31639 doc: /* Margin around tool-bar buttons in pixels.
31640 If an integer, use that for both horizontal and vertical margins.
31641 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31642 HORZ specifying the horizontal margin, and VERT specifying the
31643 vertical margin. */);
31644 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31646 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31647 doc: /* Relief thickness of tool-bar buttons. */);
31648 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31650 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31651 doc: /* Tool bar style to use.
31652 It can be one of
31653 image - show images only
31654 text - show text only
31655 both - show both, text below image
31656 both-horiz - show text to the right of the image
31657 text-image-horiz - show text to the left of the image
31658 any other - use system default or image if no system default.
31660 This variable only affects the GTK+ toolkit version of Emacs. */);
31661 Vtool_bar_style = Qnil;
31663 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31664 doc: /* Maximum number of characters a label can have to be shown.
31665 The tool bar style must also show labels for this to have any effect, see
31666 `tool-bar-style'. */);
31667 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31669 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31670 doc: /* List of functions to call to fontify regions of text.
31671 Each function is called with one argument POS. Functions must
31672 fontify a region starting at POS in the current buffer, and give
31673 fontified regions the property `fontified'. */);
31674 Vfontification_functions = Qnil;
31675 Fmake_variable_buffer_local (Qfontification_functions);
31677 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31678 unibyte_display_via_language_environment,
31679 doc: /* Non-nil means display unibyte text according to language environment.
31680 Specifically, this means that raw bytes in the range 160-255 decimal
31681 are displayed by converting them to the equivalent multibyte characters
31682 according to the current language environment. As a result, they are
31683 displayed according to the current fontset.
31685 Note that this variable affects only how these bytes are displayed,
31686 but does not change the fact they are interpreted as raw bytes. */);
31687 unibyte_display_via_language_environment = false;
31689 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31690 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31691 If a float, it specifies a fraction of the mini-window frame's height.
31692 If an integer, it specifies a number of lines. */);
31693 Vmax_mini_window_height = make_float (0.25);
31695 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31696 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31697 A value of nil means don't automatically resize mini-windows.
31698 A value of t means resize them to fit the text displayed in them.
31699 A value of `grow-only', the default, means let mini-windows grow only;
31700 they return to their normal size when the minibuffer is closed, or the
31701 echo area becomes empty. */);
31702 /* Contrary to the doc string, we initialize this to nil, so that
31703 loading loadup.el won't try to resize windows before loading
31704 window.el, where some functions we need to call for this live.
31705 We assign the 'grow-only' value right after loading window.el
31706 during loadup. */
31707 Vresize_mini_windows = Qnil;
31709 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31710 doc: /* Alist specifying how to blink the cursor off.
31711 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31712 `cursor-type' frame-parameter or variable equals ON-STATE,
31713 comparing using `equal', Emacs uses OFF-STATE to specify
31714 how to blink it off. ON-STATE and OFF-STATE are values for
31715 the `cursor-type' frame parameter.
31717 If a frame's ON-STATE has no entry in this list,
31718 the frame's other specifications determine how to blink the cursor off. */);
31719 Vblink_cursor_alist = Qnil;
31721 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31722 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31723 If non-nil, windows are automatically scrolled horizontally to make
31724 point visible. */);
31725 automatic_hscrolling_p = true;
31726 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31728 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31729 doc: /* How many columns away from the window edge point is allowed to get
31730 before automatic hscrolling will horizontally scroll the window. */);
31731 hscroll_margin = 5;
31733 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31734 doc: /* How many columns to scroll the window when point gets too close to the edge.
31735 When point is less than `hscroll-margin' columns from the window
31736 edge, automatic hscrolling will scroll the window by the amount of columns
31737 determined by this variable. If its value is a positive integer, scroll that
31738 many columns. If it's a positive floating-point number, it specifies the
31739 fraction of the window's width to scroll. If it's nil or zero, point will be
31740 centered horizontally after the scroll. Any other value, including negative
31741 numbers, are treated as if the value were zero.
31743 Automatic hscrolling always moves point outside the scroll margin, so if
31744 point was more than scroll step columns inside the margin, the window will
31745 scroll more than the value given by the scroll step.
31747 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31748 and `scroll-right' overrides this variable's effect. */);
31749 Vhscroll_step = make_number (0);
31751 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31752 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31753 Bind this around calls to `message' to let it take effect. */);
31754 message_truncate_lines = false;
31756 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31757 doc: /* Normal hook run to update the menu bar definitions.
31758 Redisplay runs this hook before it redisplays the menu bar.
31759 This is used to update menus such as Buffers, whose contents depend on
31760 various data. */);
31761 Vmenu_bar_update_hook = Qnil;
31763 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31764 doc: /* Frame for which we are updating a menu.
31765 The enable predicate for a menu binding should check this variable. */);
31766 Vmenu_updating_frame = Qnil;
31768 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31769 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31770 inhibit_menubar_update = false;
31772 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31773 doc: /* Prefix prepended to all continuation lines at display time.
31774 The value may be a string, an image, or a stretch-glyph; it is
31775 interpreted in the same way as the value of a `display' text property.
31777 This variable is overridden by any `wrap-prefix' text or overlay
31778 property.
31780 To add a prefix to non-continuation lines, use `line-prefix'. */);
31781 Vwrap_prefix = Qnil;
31782 DEFSYM (Qwrap_prefix, "wrap-prefix");
31783 Fmake_variable_buffer_local (Qwrap_prefix);
31785 DEFVAR_LISP ("line-prefix", Vline_prefix,
31786 doc: /* Prefix prepended to all non-continuation lines at display time.
31787 The value may be a string, an image, or a stretch-glyph; it is
31788 interpreted in the same way as the value of a `display' text property.
31790 This variable is overridden by any `line-prefix' text or overlay
31791 property.
31793 To add a prefix to continuation lines, use `wrap-prefix'. */);
31794 Vline_prefix = Qnil;
31795 DEFSYM (Qline_prefix, "line-prefix");
31796 Fmake_variable_buffer_local (Qline_prefix);
31798 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31799 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31800 inhibit_eval_during_redisplay = false;
31802 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31803 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31804 inhibit_free_realized_faces = false;
31806 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31807 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31808 Intended for use during debugging and for testing bidi display;
31809 see biditest.el in the test suite. */);
31810 inhibit_bidi_mirroring = false;
31812 #ifdef GLYPH_DEBUG
31813 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31814 doc: /* Inhibit try_window_id display optimization. */);
31815 inhibit_try_window_id = false;
31817 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31818 doc: /* Inhibit try_window_reusing display optimization. */);
31819 inhibit_try_window_reusing = false;
31821 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31822 doc: /* Inhibit try_cursor_movement display optimization. */);
31823 inhibit_try_cursor_movement = false;
31824 #endif /* GLYPH_DEBUG */
31826 DEFVAR_INT ("overline-margin", overline_margin,
31827 doc: /* Space between overline and text, in pixels.
31828 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31829 margin to the character height. */);
31830 overline_margin = 2;
31832 DEFVAR_INT ("underline-minimum-offset",
31833 underline_minimum_offset,
31834 doc: /* Minimum distance between baseline and underline.
31835 This can improve legibility of underlined text at small font sizes,
31836 particularly when using variable `x-use-underline-position-properties'
31837 with fonts that specify an UNDERLINE_POSITION relatively close to the
31838 baseline. The default value is 1. */);
31839 underline_minimum_offset = 1;
31841 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31842 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31843 This feature only works when on a window system that can change
31844 cursor shapes. */);
31845 display_hourglass_p = true;
31847 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31848 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31849 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31851 #ifdef HAVE_WINDOW_SYSTEM
31852 hourglass_atimer = NULL;
31853 hourglass_shown_p = false;
31854 #endif /* HAVE_WINDOW_SYSTEM */
31856 /* Name of the face used to display glyphless characters. */
31857 DEFSYM (Qglyphless_char, "glyphless-char");
31859 /* Method symbols for Vglyphless_char_display. */
31860 DEFSYM (Qhex_code, "hex-code");
31861 DEFSYM (Qempty_box, "empty-box");
31862 DEFSYM (Qthin_space, "thin-space");
31863 DEFSYM (Qzero_width, "zero-width");
31865 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31866 doc: /* Function run just before redisplay.
31867 It is called with one argument, which is the set of windows that are to
31868 be redisplayed. This set can be nil (meaning, only the selected window),
31869 or t (meaning all windows). */);
31870 Vpre_redisplay_function = intern ("ignore");
31872 /* Symbol for the purpose of Vglyphless_char_display. */
31873 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31874 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31876 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31877 doc: /* Char-table defining glyphless characters.
31878 Each element, if non-nil, should be one of the following:
31879 an ASCII acronym string: display this string in a box
31880 `hex-code': display the hexadecimal code of a character in a box
31881 `empty-box': display as an empty box
31882 `thin-space': display as 1-pixel width space
31883 `zero-width': don't display
31884 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31885 display method for graphical terminals and text terminals respectively.
31886 GRAPHICAL and TEXT should each have one of the values listed above.
31888 The char-table has one extra slot to control the display of a character for
31889 which no font is found. This slot only takes effect on graphical terminals.
31890 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31891 `thin-space'. The default is `empty-box'.
31893 If a character has a non-nil entry in an active display table, the
31894 display table takes effect; in this case, Emacs does not consult
31895 `glyphless-char-display' at all. */);
31896 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31897 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31898 Qempty_box);
31900 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31901 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31902 Vdebug_on_message = Qnil;
31904 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31905 doc: /* */);
31906 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31908 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31909 doc: /* */);
31910 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31912 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31913 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31914 Vredisplay__variables = Qnil;
31916 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
31917 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
31918 /* Initialize to t, since we need to disable reordering until
31919 loadup.el successfully loads charprop.el. */
31920 redisplay__inhibit_bidi = true;
31924 /* Initialize this module when Emacs starts. */
31926 void
31927 init_xdisp (void)
31929 CHARPOS (this_line_start_pos) = 0;
31931 if (!noninteractive)
31933 struct window *m = XWINDOW (minibuf_window);
31934 Lisp_Object frame = m->frame;
31935 struct frame *f = XFRAME (frame);
31936 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31937 struct window *r = XWINDOW (root);
31938 int i;
31940 echo_area_window = minibuf_window;
31942 r->top_line = FRAME_TOP_MARGIN (f);
31943 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31944 r->total_cols = FRAME_COLS (f);
31945 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31946 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31947 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31949 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31950 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31951 m->total_cols = FRAME_COLS (f);
31952 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31953 m->total_lines = 1;
31954 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31956 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31957 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31958 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31960 /* The default ellipsis glyphs `...'. */
31961 for (i = 0; i < 3; ++i)
31962 default_invis_vector[i] = make_number ('.');
31966 /* Allocate the buffer for frame titles.
31967 Also used for `format-mode-line'. */
31968 int size = 100;
31969 mode_line_noprop_buf = xmalloc (size);
31970 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31971 mode_line_noprop_ptr = mode_line_noprop_buf;
31972 mode_line_target = MODE_LINE_DISPLAY;
31975 help_echo_showing_p = false;
31978 #ifdef HAVE_WINDOW_SYSTEM
31980 /* Platform-independent portion of hourglass implementation. */
31982 /* Timer function of hourglass_atimer. */
31984 static void
31985 show_hourglass (struct atimer *timer)
31987 /* The timer implementation will cancel this timer automatically
31988 after this function has run. Set hourglass_atimer to null
31989 so that we know the timer doesn't have to be canceled. */
31990 hourglass_atimer = NULL;
31992 if (!hourglass_shown_p)
31994 Lisp_Object tail, frame;
31996 block_input ();
31998 FOR_EACH_FRAME (tail, frame)
32000 struct frame *f = XFRAME (frame);
32002 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32003 && FRAME_RIF (f)->show_hourglass)
32004 FRAME_RIF (f)->show_hourglass (f);
32007 hourglass_shown_p = true;
32008 unblock_input ();
32012 /* Cancel a currently active hourglass timer, and start a new one. */
32014 void
32015 start_hourglass (void)
32017 struct timespec delay;
32019 cancel_hourglass ();
32021 if (INTEGERP (Vhourglass_delay)
32022 && XINT (Vhourglass_delay) > 0)
32023 delay = make_timespec (min (XINT (Vhourglass_delay),
32024 TYPE_MAXIMUM (time_t)),
32026 else if (FLOATP (Vhourglass_delay)
32027 && XFLOAT_DATA (Vhourglass_delay) > 0)
32028 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32029 else
32030 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32032 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32033 show_hourglass, NULL);
32036 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32037 shown. */
32039 void
32040 cancel_hourglass (void)
32042 if (hourglass_atimer)
32044 cancel_atimer (hourglass_atimer);
32045 hourglass_atimer = NULL;
32048 if (hourglass_shown_p)
32050 Lisp_Object tail, frame;
32052 block_input ();
32054 FOR_EACH_FRAME (tail, frame)
32056 struct frame *f = XFRAME (frame);
32058 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32059 && FRAME_RIF (f)->hide_hourglass)
32060 FRAME_RIF (f)->hide_hourglass (f);
32061 #ifdef HAVE_NTGUI
32062 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32063 else if (!FRAME_W32_P (f))
32064 w32_arrow_cursor ();
32065 #endif
32068 hourglass_shown_p = false;
32069 unblock_input ();
32073 #endif /* HAVE_WINDOW_SYSTEM */