Pre-4.6 GCC succeeds with unknown option
[emacs.git] / src / xdisp.c
bloba17f5a9c34f6f372615a79653710fbe232373545
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 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
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
325 #define INFINITY 10000000
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
330 #ifdef HAVE_WINDOW_SYSTEM
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
345 #endif /* HAVE_WINDOW_SYSTEM */
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363 /* True means print newline to stdout before next mini-buffer message. */
365 bool noninteractive_need_newline;
367 /* True means print newline to message log before next message. */
369 static bool message_log_need_newline;
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
383 static struct text_pos this_line_start_pos;
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
388 static struct text_pos this_line_end_pos;
390 /* The vertical positions and the height of this line. */
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
399 static int this_line_start_x;
401 /* The smallest character position seen by move_it_* functions as they
402 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
403 hscrolled lines, see display_line. */
405 static struct text_pos this_line_min_pos;
407 /* Buffer that this_line_.* variables are referring to. */
409 static struct buffer *this_line_buffer;
411 /* True if an overlay arrow has been displayed in this window. */
413 static bool overlay_arrow_seen;
415 /* Vector containing glyphs for an ellipsis `...'. */
417 static Lisp_Object default_invis_vector[3];
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
423 Lisp_Object echo_area_window;
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
430 static Lisp_Object Vmessage_stack;
432 /* True means multibyte characters were enabled when the echo area
433 message was specified. */
435 static bool message_enable_multibyte;
437 /* Nonzero if we should redraw the mode lines on the next redisplay.
438 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
439 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
440 (the number used is then only used to track down the cause for this
441 full-redisplay). */
443 int update_mode_lines;
445 /* Nonzero if window sizes or contents other than selected-window have changed
446 since last redisplay that finished.
447 If it has value REDISPLAY_SOME, then only redisplay the windows where
448 the `redisplay' bit has been set. Otherwise, redisplay all windows
449 (the number used is then only used to track down the cause for this
450 full-redisplay). */
452 int windows_or_buffers_changed;
454 /* True after display_mode_line if %l was used and it displayed a
455 line number. */
457 static bool line_number_displayed;
459 /* The name of the *Messages* buffer, a string. */
461 static Lisp_Object Vmessages_buffer_name;
463 /* Current, index 0, and last displayed echo area message. Either
464 buffers from echo_buffers, or nil to indicate no message. */
466 Lisp_Object echo_area_buffer[2];
468 /* The buffers referenced from echo_area_buffer. */
470 static Lisp_Object echo_buffer[2];
472 /* A vector saved used in with_area_buffer to reduce consing. */
474 static Lisp_Object Vwith_echo_area_save_vector;
476 /* True means display_echo_area should display the last echo area
477 message again. Set by redisplay_preserve_echo_area. */
479 static bool display_last_displayed_message_p;
481 /* True if echo area is being used by print; false if being used by
482 message. */
484 static bool message_buf_print;
486 /* Set to true in clear_message to make redisplay_internal aware
487 of an emptied echo area. */
489 static bool message_cleared_p;
491 /* A scratch glyph row with contents used for generating truncation
492 glyphs. Also used in direct_output_for_insert. */
494 #define MAX_SCRATCH_GLYPHS 100
495 static struct glyph_row scratch_glyph_row;
496 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
498 /* Ascent and height of the last line processed by move_it_to. */
500 static int last_height;
502 /* True if there's a help-echo in the echo area. */
504 bool help_echo_showing_p;
506 /* The maximum distance to look ahead for text properties. Values
507 that are too small let us call compute_char_face and similar
508 functions too often which is expensive. Values that are too large
509 let us call compute_char_face and alike too often because we
510 might not be interested in text properties that far away. */
512 #define TEXT_PROP_DISTANCE_LIMIT 100
514 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
515 iterator state and later restore it. This is needed because the
516 bidi iterator on bidi.c keeps a stacked cache of its states, which
517 is really a singleton. When we use scratch iterator objects to
518 move around the buffer, we can cause the bidi cache to be pushed or
519 popped, and therefore we need to restore the cache state when we
520 return to the original iterator. */
521 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
522 do { \
523 if (CACHE) \
524 bidi_unshelve_cache (CACHE, true); \
525 ITCOPY = ITORIG; \
526 CACHE = bidi_shelve_cache (); \
527 } while (false)
529 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
530 do { \
531 if (pITORIG != pITCOPY) \
532 *(pITORIG) = *(pITCOPY); \
533 bidi_unshelve_cache (CACHE, false); \
534 CACHE = NULL; \
535 } while (false)
537 /* Functions to mark elements as needing redisplay. */
538 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
540 void
541 redisplay_other_windows (void)
543 if (!windows_or_buffers_changed)
544 windows_or_buffers_changed = REDISPLAY_SOME;
547 void
548 wset_redisplay (struct window *w)
550 /* Beware: selected_window can be nil during early stages. */
551 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
552 redisplay_other_windows ();
553 w->redisplay = true;
556 void
557 fset_redisplay (struct frame *f)
559 redisplay_other_windows ();
560 f->redisplay = true;
563 void
564 bset_redisplay (struct buffer *b)
566 int count = buffer_window_count (b);
567 if (count > 0)
569 /* ... it's visible in other window than selected, */
570 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
571 redisplay_other_windows ();
572 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
573 so that if we later set windows_or_buffers_changed, this buffer will
574 not be omitted. */
575 b->text->redisplay = true;
579 void
580 bset_update_mode_line (struct buffer *b)
582 if (!update_mode_lines)
583 update_mode_lines = REDISPLAY_SOME;
584 b->text->redisplay = true;
587 #ifdef GLYPH_DEBUG
589 /* True means print traces of redisplay if compiled with
590 GLYPH_DEBUG defined. */
592 bool trace_redisplay_p;
594 #endif /* GLYPH_DEBUG */
596 #ifdef DEBUG_TRACE_MOVE
597 /* True means trace with TRACE_MOVE to stderr. */
598 static bool trace_move;
600 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
601 #else
602 #define TRACE_MOVE(x) (void) 0
603 #endif
605 /* Buffer being redisplayed -- for redisplay_window_error. */
607 static struct buffer *displayed_buffer;
609 /* Value returned from text property handlers (see below). */
611 enum prop_handled
613 HANDLED_NORMALLY,
614 HANDLED_RECOMPUTE_PROPS,
615 HANDLED_OVERLAY_STRING_CONSUMED,
616 HANDLED_RETURN
619 /* A description of text properties that redisplay is interested
620 in. */
622 struct props
624 /* The symbol index of the name of the property. */
625 short name;
627 /* A unique index for the property. */
628 enum prop_idx idx;
630 /* A handler function called to set up iterator IT from the property
631 at IT's current position. Value is used to steer handle_stop. */
632 enum prop_handled (*handler) (struct it *it);
635 static enum prop_handled handle_face_prop (struct it *);
636 static enum prop_handled handle_invisible_prop (struct it *);
637 static enum prop_handled handle_display_prop (struct it *);
638 static enum prop_handled handle_composition_prop (struct it *);
639 static enum prop_handled handle_overlay_change (struct it *);
640 static enum prop_handled handle_fontified_prop (struct it *);
642 /* Properties handled by iterators. */
644 static struct props it_props[] =
646 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
647 /* Handle `face' before `display' because some sub-properties of
648 `display' need to know the face. */
649 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
650 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
651 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
652 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
653 {0, 0, NULL}
656 /* Value is the position described by X. If X is a marker, value is
657 the marker_position of X. Otherwise, value is X. */
659 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
661 /* Enumeration returned by some move_it_.* functions internally. */
663 enum move_it_result
665 /* Not used. Undefined value. */
666 MOVE_UNDEFINED,
668 /* Move ended at the requested buffer position or ZV. */
669 MOVE_POS_MATCH_OR_ZV,
671 /* Move ended at the requested X pixel position. */
672 MOVE_X_REACHED,
674 /* Move within a line ended at the end of a line that must be
675 continued. */
676 MOVE_LINE_CONTINUED,
678 /* Move within a line ended at the end of a line that would
679 be displayed truncated. */
680 MOVE_LINE_TRUNCATED,
682 /* Move within a line ended at a line end. */
683 MOVE_NEWLINE_OR_CR
686 /* This counter is used to clear the face cache every once in a while
687 in redisplay_internal. It is incremented for each redisplay.
688 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
689 cleared. */
691 #define CLEAR_FACE_CACHE_COUNT 500
692 static int clear_face_cache_count;
694 /* Similarly for the image cache. */
696 #ifdef HAVE_WINDOW_SYSTEM
697 #define CLEAR_IMAGE_CACHE_COUNT 101
698 static int clear_image_cache_count;
700 /* Null glyph slice */
701 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
702 #endif
704 /* True while redisplay_internal is in progress. */
706 bool redisplaying_p;
708 /* If a string, XTread_socket generates an event to display that string.
709 (The display is done in read_char.) */
711 Lisp_Object help_echo_string;
712 Lisp_Object help_echo_window;
713 Lisp_Object help_echo_object;
714 ptrdiff_t help_echo_pos;
716 /* Temporary variable for XTread_socket. */
718 Lisp_Object previous_help_echo_string;
720 /* Platform-independent portion of hourglass implementation. */
722 #ifdef HAVE_WINDOW_SYSTEM
724 /* True means an hourglass cursor is currently shown. */
725 static bool hourglass_shown_p;
727 /* If non-null, an asynchronous timer that, when it expires, displays
728 an hourglass cursor on all frames. */
729 static struct atimer *hourglass_atimer;
731 #endif /* HAVE_WINDOW_SYSTEM */
733 /* Default number of seconds to wait before displaying an hourglass
734 cursor. */
735 #define DEFAULT_HOURGLASS_DELAY 1
737 #ifdef HAVE_WINDOW_SYSTEM
739 /* Default pixel width of `thin-space' display method. */
740 #define THIN_SPACE_WIDTH 1
742 #endif /* HAVE_WINDOW_SYSTEM */
744 /* Function prototypes. */
746 static void setup_for_ellipsis (struct it *, int);
747 static void set_iterator_to_next (struct it *, bool);
748 static void mark_window_display_accurate_1 (struct window *, bool);
749 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
750 static bool cursor_row_p (struct glyph_row *);
751 static int redisplay_mode_lines (Lisp_Object, bool);
753 static void handle_line_prefix (struct it *);
755 static void handle_stop_backwards (struct it *, ptrdiff_t);
756 static void unwind_with_echo_area_buffer (Lisp_Object);
757 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
758 static bool current_message_1 (ptrdiff_t, Lisp_Object);
759 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
760 static void set_message (Lisp_Object);
761 static bool set_message_1 (ptrdiff_t, Lisp_Object);
762 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
763 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
764 static void unwind_redisplay (void);
765 static void extend_face_to_end_of_line (struct it *);
766 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
767 static void push_it (struct it *, struct text_pos *);
768 static void iterate_out_of_display_property (struct it *);
769 static void pop_it (struct it *);
770 static void redisplay_internal (void);
771 static bool echo_area_display (bool);
772 static void redisplay_windows (Lisp_Object);
773 static void redisplay_window (Lisp_Object, bool);
774 static Lisp_Object redisplay_window_error (Lisp_Object);
775 static Lisp_Object redisplay_window_0 (Lisp_Object);
776 static Lisp_Object redisplay_window_1 (Lisp_Object);
777 static bool set_cursor_from_row (struct window *, struct glyph_row *,
778 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
779 int, int);
780 static bool update_menu_bar (struct frame *, bool, bool);
781 static bool try_window_reusing_current_matrix (struct window *);
782 static int try_window_id (struct window *);
783 static bool display_line (struct it *);
784 static int display_mode_lines (struct window *);
785 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
786 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
787 Lisp_Object, bool);
788 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
789 Lisp_Object);
790 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
791 static void display_menu_bar (struct window *);
792 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
793 ptrdiff_t *);
794 static int display_string (const char *, Lisp_Object, Lisp_Object,
795 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
796 static void compute_line_metrics (struct it *);
797 static void run_redisplay_end_trigger_hook (struct it *);
798 static bool get_overlay_strings (struct it *, ptrdiff_t);
799 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
800 static void next_overlay_string (struct it *);
801 static void reseat (struct it *, struct text_pos, bool);
802 static void reseat_1 (struct it *, struct text_pos, bool);
803 static bool next_element_from_display_vector (struct it *);
804 static bool next_element_from_string (struct it *);
805 static bool next_element_from_c_string (struct it *);
806 static bool next_element_from_buffer (struct it *);
807 static bool next_element_from_composition (struct it *);
808 static bool next_element_from_image (struct it *);
809 static bool next_element_from_stretch (struct it *);
810 static void load_overlay_strings (struct it *, ptrdiff_t);
811 static bool get_next_display_element (struct it *);
812 static enum move_it_result
813 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
814 enum move_operation_enum);
815 static void get_visually_first_element (struct it *);
816 static void compute_stop_pos (struct it *);
817 static int face_before_or_after_it_pos (struct it *, bool);
818 static ptrdiff_t next_overlay_change (ptrdiff_t);
819 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
820 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
821 static int handle_single_display_spec (struct it *, Lisp_Object,
822 Lisp_Object, Lisp_Object,
823 struct text_pos *, ptrdiff_t, int, bool);
824 static int underlying_face_id (struct it *);
826 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
827 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
829 #ifdef HAVE_WINDOW_SYSTEM
831 static void update_tool_bar (struct frame *, bool);
832 static void x_draw_bottom_divider (struct window *w);
833 static void notice_overwritten_cursor (struct window *,
834 enum glyph_row_area,
835 int, int, int, int);
836 static void append_stretch_glyph (struct it *, Lisp_Object,
837 int, int, int);
840 #endif /* HAVE_WINDOW_SYSTEM */
842 static void produce_special_glyphs (struct it *, enum display_element_type);
843 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
844 static bool coords_in_mouse_face_p (struct window *, int, int);
848 /***********************************************************************
849 Window display dimensions
850 ***********************************************************************/
852 /* Return the bottom boundary y-position for text lines in window W.
853 This is the first y position at which a line cannot start.
854 It is relative to the top of the window.
856 This is the height of W minus the height of a mode line, if any. */
859 window_text_bottom_y (struct window *w)
861 int height = WINDOW_PIXEL_HEIGHT (w);
863 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
865 if (WINDOW_WANTS_MODELINE_P (w))
866 height -= CURRENT_MODE_LINE_HEIGHT (w);
868 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
870 return height;
873 /* Return the pixel width of display area AREA of window W.
874 ANY_AREA means return the total width of W, not including
875 fringes to the left and right of the window. */
878 window_box_width (struct window *w, enum glyph_row_area area)
880 int width = w->pixel_width;
882 if (!w->pseudo_window_p)
884 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
885 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
887 if (area == TEXT_AREA)
888 width -= (WINDOW_MARGINS_WIDTH (w)
889 + WINDOW_FRINGES_WIDTH (w));
890 else if (area == LEFT_MARGIN_AREA)
891 width = WINDOW_LEFT_MARGIN_WIDTH (w);
892 else if (area == RIGHT_MARGIN_AREA)
893 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
896 /* With wide margins, fringes, etc. we might end up with a negative
897 width, correct that here. */
898 return max (0, width);
902 /* Return the pixel height of the display area of window W, not
903 including mode lines of W, if any. */
906 window_box_height (struct window *w)
908 struct frame *f = XFRAME (w->frame);
909 int height = WINDOW_PIXEL_HEIGHT (w);
911 eassert (height >= 0);
913 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
914 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
916 /* Note: the code below that determines the mode-line/header-line
917 height is essentially the same as that contained in the macro
918 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
919 the appropriate glyph row has its `mode_line_p' flag set,
920 and if it doesn't, uses estimate_mode_line_height instead. */
922 if (WINDOW_WANTS_MODELINE_P (w))
924 struct glyph_row *ml_row
925 = (w->current_matrix && w->current_matrix->rows
926 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
927 : 0);
928 if (ml_row && ml_row->mode_line_p)
929 height -= ml_row->height;
930 else
931 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
934 if (WINDOW_WANTS_HEADER_LINE_P (w))
936 struct glyph_row *hl_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
939 : 0);
940 if (hl_row && hl_row->mode_line_p)
941 height -= hl_row->height;
942 else
943 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
946 /* With a very small font and a mode-line that's taller than
947 default, we might end up with a negative height. */
948 return max (0, height);
951 /* Return the window-relative coordinate of the left edge of display
952 area AREA of window W. ANY_AREA means return the left edge of the
953 whole window, to the right of the left fringe of W. */
956 window_box_left_offset (struct window *w, enum glyph_row_area area)
958 int x;
960 if (w->pseudo_window_p)
961 return 0;
963 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
965 if (area == TEXT_AREA)
966 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
967 + window_box_width (w, LEFT_MARGIN_AREA));
968 else if (area == RIGHT_MARGIN_AREA)
969 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
970 + window_box_width (w, LEFT_MARGIN_AREA)
971 + window_box_width (w, TEXT_AREA)
972 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
974 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
975 else if (area == LEFT_MARGIN_AREA
976 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
977 x += WINDOW_LEFT_FRINGE_WIDTH (w);
979 /* Don't return more than the window's pixel width. */
980 return min (x, w->pixel_width);
984 /* Return the window-relative coordinate of the right edge of display
985 area AREA of window W. ANY_AREA means return the right edge of the
986 whole window, to the left of the right fringe of W. */
988 static int
989 window_box_right_offset (struct window *w, enum glyph_row_area area)
991 /* Don't return more than the window's pixel width. */
992 return min (window_box_left_offset (w, area) + window_box_width (w, area),
993 w->pixel_width);
996 /* Return the frame-relative coordinate of the left edge of display
997 area AREA of window W. ANY_AREA means return the left edge of the
998 whole window, to the right of the left fringe of W. */
1001 window_box_left (struct window *w, enum glyph_row_area area)
1003 struct frame *f = XFRAME (w->frame);
1004 int x;
1006 if (w->pseudo_window_p)
1007 return FRAME_INTERNAL_BORDER_WIDTH (f);
1009 x = (WINDOW_LEFT_EDGE_X (w)
1010 + window_box_left_offset (w, area));
1012 return x;
1016 /* Return the frame-relative coordinate of the right edge of display
1017 area AREA of window W. ANY_AREA means return the right edge of the
1018 whole window, to the left of the right fringe of W. */
1021 window_box_right (struct window *w, enum glyph_row_area area)
1023 return window_box_left (w, area) + window_box_width (w, area);
1026 /* Get the bounding box of the display area AREA of window W, without
1027 mode lines, in frame-relative coordinates. ANY_AREA means the
1028 whole window, not including the left and right fringes of
1029 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1030 coordinates of the upper-left corner of the box. Return in
1031 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1033 void
1034 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1035 int *box_y, int *box_width, int *box_height)
1037 if (box_width)
1038 *box_width = window_box_width (w, area);
1039 if (box_height)
1040 *box_height = window_box_height (w);
1041 if (box_x)
1042 *box_x = window_box_left (w, area);
1043 if (box_y)
1045 *box_y = WINDOW_TOP_EDGE_Y (w);
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1051 #ifdef HAVE_WINDOW_SYSTEM
1053 /* Get the bounding box of the display area AREA of window W, without
1054 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1055 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1056 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1057 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1058 box. */
1060 static void
1061 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1062 int *bottom_right_x, int *bottom_right_y)
1064 window_box (w, ANY_AREA, top_left_x, top_left_y,
1065 bottom_right_x, bottom_right_y);
1066 *bottom_right_x += *top_left_x;
1067 *bottom_right_y += *top_left_y;
1070 #endif /* HAVE_WINDOW_SYSTEM */
1072 /***********************************************************************
1073 Utilities
1074 ***********************************************************************/
1076 /* Return the bottom y-position of the line the iterator IT is in.
1077 This can modify IT's settings. */
1080 line_bottom_y (struct it *it)
1082 int line_height = it->max_ascent + it->max_descent;
1083 int line_top_y = it->current_y;
1085 if (line_height == 0)
1087 if (last_height)
1088 line_height = last_height;
1089 else if (IT_CHARPOS (*it) < ZV)
1091 move_it_by_lines (it, 1);
1092 line_height = (it->max_ascent || it->max_descent
1093 ? it->max_ascent + it->max_descent
1094 : last_height);
1096 else
1098 struct glyph_row *row = it->glyph_row;
1100 /* Use the default character height. */
1101 it->glyph_row = NULL;
1102 it->what = IT_CHARACTER;
1103 it->c = ' ';
1104 it->len = 1;
1105 PRODUCE_GLYPHS (it);
1106 line_height = it->ascent + it->descent;
1107 it->glyph_row = row;
1111 return line_top_y + line_height;
1114 DEFUN ("line-pixel-height", Fline_pixel_height,
1115 Sline_pixel_height, 0, 0, 0,
1116 doc: /* Return height in pixels of text line in the selected window.
1118 Value is the height in pixels of the line at point. */)
1119 (void)
1121 struct it it;
1122 struct text_pos pt;
1123 struct window *w = XWINDOW (selected_window);
1124 struct buffer *old_buffer = NULL;
1125 Lisp_Object result;
1127 if (XBUFFER (w->contents) != current_buffer)
1129 old_buffer = current_buffer;
1130 set_buffer_internal_1 (XBUFFER (w->contents));
1132 SET_TEXT_POS (pt, PT, PT_BYTE);
1133 start_display (&it, w, pt);
1134 it.vpos = it.current_y = 0;
1135 last_height = 0;
1136 result = make_number (line_bottom_y (&it));
1137 if (old_buffer)
1138 set_buffer_internal_1 (old_buffer);
1140 return result;
1143 /* Return the default pixel height of text lines in window W. The
1144 value is the canonical height of the W frame's default font, plus
1145 any extra space required by the line-spacing variable or frame
1146 parameter.
1148 Implementation note: this ignores any line-spacing text properties
1149 put on the newline characters. This is because those properties
1150 only affect the _screen_ line ending in the newline (i.e., in a
1151 continued line, only the last screen line will be affected), which
1152 means only a small number of lines in a buffer can ever use this
1153 feature. Since this function is used to compute the default pixel
1154 equivalent of text lines in a window, we can safely ignore those
1155 few lines. For the same reasons, we ignore the line-height
1156 properties. */
1158 default_line_pixel_height (struct window *w)
1160 struct frame *f = WINDOW_XFRAME (w);
1161 int height = FRAME_LINE_HEIGHT (f);
1163 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1165 struct buffer *b = XBUFFER (w->contents);
1166 Lisp_Object val = BVAR (b, extra_line_spacing);
1168 if (NILP (val))
1169 val = BVAR (&buffer_defaults, extra_line_spacing);
1170 if (!NILP (val))
1172 if (RANGED_INTEGERP (0, val, INT_MAX))
1173 height += XFASTINT (val);
1174 else if (FLOATP (val))
1176 int addon = XFLOAT_DATA (val) * height + 0.5;
1178 if (addon >= 0)
1179 height += addon;
1182 else
1183 height += f->extra_line_spacing;
1186 return height;
1189 /* Subroutine of pos_visible_p below. Extracts a display string, if
1190 any, from the display spec given as its argument. */
1191 static Lisp_Object
1192 string_from_display_spec (Lisp_Object spec)
1194 if (CONSP (spec))
1196 while (CONSP (spec))
1198 if (STRINGP (XCAR (spec)))
1199 return XCAR (spec);
1200 spec = XCDR (spec);
1203 else if (VECTORP (spec))
1205 ptrdiff_t i;
1207 for (i = 0; i < ASIZE (spec); i++)
1209 if (STRINGP (AREF (spec, i)))
1210 return AREF (spec, i);
1212 return Qnil;
1215 return spec;
1219 /* Limit insanely large values of W->hscroll on frame F to the largest
1220 value that will still prevent first_visible_x and last_visible_x of
1221 'struct it' from overflowing an int. */
1222 static int
1223 window_hscroll_limited (struct window *w, struct frame *f)
1225 ptrdiff_t window_hscroll = w->hscroll;
1226 int window_text_width = window_box_width (w, TEXT_AREA);
1227 int colwidth = FRAME_COLUMN_WIDTH (f);
1229 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1230 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1232 return window_hscroll;
1235 /* Return true if position CHARPOS is visible in window W.
1236 CHARPOS < 0 means return info about WINDOW_END position.
1237 If visible, set *X and *Y to pixel coordinates of top left corner.
1238 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1239 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1241 bool
1242 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1243 int *rtop, int *rbot, int *rowh, int *vpos)
1245 struct it it;
1246 void *itdata = bidi_shelve_cache ();
1247 struct text_pos top;
1248 bool visible_p = false;
1249 struct buffer *old_buffer = NULL;
1250 bool r2l = false;
1252 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1253 return visible_p;
1255 if (XBUFFER (w->contents) != current_buffer)
1257 old_buffer = current_buffer;
1258 set_buffer_internal_1 (XBUFFER (w->contents));
1261 SET_TEXT_POS_FROM_MARKER (top, w->start);
1262 /* Scrolling a minibuffer window via scroll bar when the echo area
1263 shows long text sometimes resets the minibuffer contents behind
1264 our backs. */
1265 if (CHARPOS (top) > ZV)
1266 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1268 /* Compute exact mode line heights. */
1269 if (WINDOW_WANTS_MODELINE_P (w))
1270 w->mode_line_height
1271 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1272 BVAR (current_buffer, mode_line_format));
1274 if (WINDOW_WANTS_HEADER_LINE_P (w))
1275 w->header_line_height
1276 = display_mode_line (w, HEADER_LINE_FACE_ID,
1277 BVAR (current_buffer, header_line_format));
1279 start_display (&it, w, top);
1280 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1281 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283 if (charpos >= 0
1284 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1285 && IT_CHARPOS (it) >= charpos)
1286 /* When scanning backwards under bidi iteration, move_it_to
1287 stops at or _before_ CHARPOS, because it stops at or to
1288 the _right_ of the character at CHARPOS. */
1289 || (it.bidi_p && it.bidi_it.scan_dir == -1
1290 && IT_CHARPOS (it) <= charpos)))
1292 /* We have reached CHARPOS, or passed it. How the call to
1293 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1294 or covered by a display property, move_it_to stops at the end
1295 of the invisible text, to the right of CHARPOS. (ii) If
1296 CHARPOS is in a display vector, move_it_to stops on its last
1297 glyph. */
1298 int top_x = it.current_x;
1299 int top_y = it.current_y;
1300 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1301 int bottom_y;
1302 struct it save_it;
1303 void *save_it_data = NULL;
1305 /* Calling line_bottom_y may change it.method, it.position, etc. */
1306 SAVE_IT (save_it, it, save_it_data);
1307 last_height = 0;
1308 bottom_y = line_bottom_y (&it);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = true;
1313 if (bottom_y >= it.last_visible_y
1314 && it.bidi_p && it.bidi_it.scan_dir == -1
1315 && IT_CHARPOS (it) < charpos)
1317 /* When the last line of the window is scanned backwards
1318 under bidi iteration, we could be duped into thinking
1319 that we have passed CHARPOS, when in fact move_it_to
1320 simply stopped short of CHARPOS because it reached
1321 last_visible_y. To see if that's what happened, we call
1322 move_it_to again with a slightly larger vertical limit,
1323 and see if it actually moved vertically; if it did, we
1324 didn't really reach CHARPOS, which is beyond window end. */
1325 /* Why 10? because we don't know how many canonical lines
1326 will the height of the next line(s) be. So we guess. */
1327 int ten_more_lines = 10 * default_line_pixel_height (w);
1329 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1330 MOVE_TO_POS | MOVE_TO_Y);
1331 if (it.current_y > top_y)
1332 visible_p = false;
1335 RESTORE_IT (&it, &save_it, save_it_data);
1336 if (visible_p)
1338 if (it.method == GET_FROM_DISPLAY_VECTOR)
1340 /* We stopped on the last glyph of a display vector.
1341 Try and recompute. Hack alert! */
1342 if (charpos < 2 || top.charpos >= charpos)
1343 top_x = it.glyph_row->x;
1344 else
1346 struct it it2, it2_prev;
1347 /* The idea is to get to the previous buffer
1348 position, consume the character there, and use
1349 the pixel coordinates we get after that. But if
1350 the previous buffer position is also displayed
1351 from a display vector, we need to consume all of
1352 the glyphs from that display vector. */
1353 start_display (&it2, w, top);
1354 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1355 /* If we didn't get to CHARPOS - 1, there's some
1356 replacing display property at that position, and
1357 we stopped after it. That is exactly the place
1358 whose coordinates we want. */
1359 if (IT_CHARPOS (it2) != charpos - 1)
1360 it2_prev = it2;
1361 else
1363 /* Iterate until we get out of the display
1364 vector that displays the character at
1365 CHARPOS - 1. */
1366 do {
1367 get_next_display_element (&it2);
1368 PRODUCE_GLYPHS (&it2);
1369 it2_prev = it2;
1370 set_iterator_to_next (&it2, true);
1371 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1372 && IT_CHARPOS (it2) < charpos);
1374 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1375 || it2_prev.current_x > it2_prev.last_visible_x)
1376 top_x = it.glyph_row->x;
1377 else
1379 top_x = it2_prev.current_x;
1380 top_y = it2_prev.current_y;
1384 else if (IT_CHARPOS (it) != charpos)
1386 Lisp_Object cpos = make_number (charpos);
1387 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1388 Lisp_Object string = string_from_display_spec (spec);
1389 struct text_pos tpos;
1390 bool newline_in_string
1391 = (STRINGP (string)
1392 && memchr (SDATA (string), '\n', SBYTES (string)));
1394 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1395 bool replacing_spec_p
1396 = (!NILP (spec)
1397 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1398 charpos, FRAME_WINDOW_P (it.f)));
1399 /* The tricky code below is needed because there's a
1400 discrepancy between move_it_to and how we set cursor
1401 when PT is at the beginning of a portion of text
1402 covered by a display property or an overlay with a
1403 display property, or the display line ends in a
1404 newline from a display string. move_it_to will stop
1405 _after_ such display strings, whereas
1406 set_cursor_from_row conspires with cursor_row_p to
1407 place the cursor on the first glyph produced from the
1408 display string. */
1410 /* We have overshoot PT because it is covered by a
1411 display property that replaces the text it covers.
1412 If the string includes embedded newlines, we are also
1413 in the wrong display line. Backtrack to the correct
1414 line, where the display property begins. */
1415 if (replacing_spec_p)
1417 Lisp_Object startpos, endpos;
1418 EMACS_INT start, end;
1419 struct it it3;
1421 /* Find the first and the last buffer positions
1422 covered by the display string. */
1423 endpos =
1424 Fnext_single_char_property_change (cpos, Qdisplay,
1425 Qnil, Qnil);
1426 startpos =
1427 Fprevious_single_char_property_change (endpos, Qdisplay,
1428 Qnil, Qnil);
1429 start = XFASTINT (startpos);
1430 end = XFASTINT (endpos);
1431 /* Move to the last buffer position before the
1432 display property. */
1433 start_display (&it3, w, top);
1434 if (start > CHARPOS (top))
1435 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1436 /* Move forward one more line if the position before
1437 the display string is a newline or if it is the
1438 rightmost character on a line that is
1439 continued or word-wrapped. */
1440 if (it3.method == GET_FROM_BUFFER
1441 && (it3.c == '\n'
1442 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1443 move_it_by_lines (&it3, 1);
1444 else if (move_it_in_display_line_to (&it3, -1,
1445 it3.current_x
1446 + it3.pixel_width,
1447 MOVE_TO_X)
1448 == MOVE_LINE_CONTINUED)
1450 move_it_by_lines (&it3, 1);
1451 /* When we are under word-wrap, the #$@%!
1452 move_it_by_lines moves 2 lines, so we need to
1453 fix that up. */
1454 if (it3.line_wrap == WORD_WRAP)
1455 move_it_by_lines (&it3, -1);
1458 /* Record the vertical coordinate of the display
1459 line where we wound up. */
1460 top_y = it3.current_y;
1461 if (it3.bidi_p)
1463 /* When characters are reordered for display,
1464 the character displayed to the left of the
1465 display string could be _after_ the display
1466 property in the logical order. Use the
1467 smallest vertical position of these two. */
1468 start_display (&it3, w, top);
1469 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1470 if (it3.current_y < top_y)
1471 top_y = it3.current_y;
1473 /* Move from the top of the window to the beginning
1474 of the display line where the display string
1475 begins. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1478 /* If it3_moved stays false after the 'while' loop
1479 below, that means we already were at a newline
1480 before the loop (e.g., the display string begins
1481 with a newline), so we don't need to (and cannot)
1482 inspect the glyphs of it3.glyph_row, because
1483 PRODUCE_GLYPHS will not produce anything for a
1484 newline, and thus it3.glyph_row stays at its
1485 stale content it got at top of the window. */
1486 bool it3_moved = false;
1487 /* Finally, advance the iterator until we hit the
1488 first display element whose character position is
1489 CHARPOS, or until the first newline from the
1490 display string, which signals the end of the
1491 display line. */
1492 while (get_next_display_element (&it3))
1494 PRODUCE_GLYPHS (&it3);
1495 if (IT_CHARPOS (it3) == charpos
1496 || ITERATOR_AT_END_OF_LINE_P (&it3))
1497 break;
1498 it3_moved = true;
1499 set_iterator_to_next (&it3, false);
1501 top_x = it3.current_x - it3.pixel_width;
1502 /* Normally, we would exit the above loop because we
1503 found the display element whose character
1504 position is CHARPOS. For the contingency that we
1505 didn't, and stopped at the first newline from the
1506 display string, move back over the glyphs
1507 produced from the string, until we find the
1508 rightmost glyph not from the string. */
1509 if (it3_moved
1510 && newline_in_string
1511 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1513 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1514 + it3.glyph_row->used[TEXT_AREA];
1516 while (EQ ((g - 1)->object, string))
1518 --g;
1519 top_x -= g->pixel_width;
1521 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1522 + it3.glyph_row->used[TEXT_AREA]);
1527 *x = top_x;
1528 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1529 *rtop = max (0, window_top_y - top_y);
1530 *rbot = max (0, bottom_y - it.last_visible_y);
1531 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1532 - max (top_y, window_top_y)));
1533 *vpos = it.vpos;
1534 if (it.bidi_it.paragraph_dir == R2L)
1535 r2l = true;
1538 else
1540 /* Either we were asked to provide info about WINDOW_END, or
1541 CHARPOS is in the partially visible glyph row at end of
1542 window. */
1543 struct it it2;
1544 void *it2data = NULL;
1546 SAVE_IT (it2, it, it2data);
1547 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1548 move_it_by_lines (&it, 1);
1549 if (charpos < IT_CHARPOS (it)
1550 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1552 visible_p = true;
1553 RESTORE_IT (&it2, &it2, it2data);
1554 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1555 *x = it2.current_x;
1556 *y = it2.current_y + it2.max_ascent - it2.ascent;
1557 *rtop = max (0, -it2.current_y);
1558 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1559 - it.last_visible_y));
1560 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1561 it.last_visible_y)
1562 - max (it2.current_y,
1563 WINDOW_HEADER_LINE_HEIGHT (w))));
1564 *vpos = it2.vpos;
1565 if (it2.bidi_it.paragraph_dir == R2L)
1566 r2l = true;
1568 else
1569 bidi_unshelve_cache (it2data, true);
1571 bidi_unshelve_cache (itdata, false);
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1576 if (visible_p)
1578 if (w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582 /* For lines in an R2L paragraph, we need to mirror the X pixel
1583 coordinate wrt the text area. For the reasons, see the
1584 commentary in buffer_posn_from_coords and the explanation of
1585 the geometry used by the move_it_* functions at the end of
1586 the large commentary near the beginning of this file. */
1587 if (r2l)
1588 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1591 #if false
1592 /* Debugging code. */
1593 if (visible_p)
1594 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1595 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1596 else
1597 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1598 #endif
1600 return visible_p;
1604 /* Return the next character from STR. Return in *LEN the length of
1605 the character. This is like STRING_CHAR_AND_LENGTH but never
1606 returns an invalid character. If we find one, we return a `?', but
1607 with the length of the invalid character. */
1609 static int
1610 string_char_and_length (const unsigned char *str, int *len)
1612 int c;
1614 c = STRING_CHAR_AND_LENGTH (str, *len);
1615 if (!CHAR_VALID_P (c))
1616 /* We may not change the length here because other places in Emacs
1617 don't use this function, i.e. they silently accept invalid
1618 characters. */
1619 c = '?';
1621 return c;
1626 /* Given a position POS containing a valid character and byte position
1627 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1629 static struct text_pos
1630 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1632 eassert (STRINGP (string) && nchars >= 0);
1634 if (STRING_MULTIBYTE (string))
1636 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1637 int len;
1639 while (nchars--)
1641 string_char_and_length (p, &len);
1642 p += len;
1643 CHARPOS (pos) += 1;
1644 BYTEPOS (pos) += len;
1647 else
1648 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1650 return pos;
1654 /* Value is the text position, i.e. character and byte position,
1655 for character position CHARPOS in STRING. */
1657 static struct text_pos
1658 string_pos (ptrdiff_t charpos, Lisp_Object string)
1660 struct text_pos pos;
1661 eassert (STRINGP (string));
1662 eassert (charpos >= 0);
1663 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1664 return pos;
1668 /* Value is a text position, i.e. character and byte position, for
1669 character position CHARPOS in C string S. MULTIBYTE_P
1670 means recognize multibyte characters. */
1672 static struct text_pos
1673 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1675 struct text_pos pos;
1677 eassert (s != NULL);
1678 eassert (charpos >= 0);
1680 if (multibyte_p)
1682 int len;
1684 SET_TEXT_POS (pos, 0, 0);
1685 while (charpos--)
1687 string_char_and_length ((const unsigned char *) s, &len);
1688 s += len;
1689 CHARPOS (pos) += 1;
1690 BYTEPOS (pos) += len;
1693 else
1694 SET_TEXT_POS (pos, charpos, charpos);
1696 return pos;
1700 /* Value is the number of characters in C string S. MULTIBYTE_P
1701 means recognize multibyte characters. */
1703 static ptrdiff_t
1704 number_of_chars (const char *s, bool multibyte_p)
1706 ptrdiff_t nchars;
1708 if (multibyte_p)
1710 ptrdiff_t rest = strlen (s);
1711 int len;
1712 const unsigned char *p = (const unsigned char *) s;
1714 for (nchars = 0; rest > 0; ++nchars)
1716 string_char_and_length (p, &len);
1717 rest -= len, p += len;
1720 else
1721 nchars = strlen (s);
1723 return nchars;
1727 /* Compute byte position NEWPOS->bytepos corresponding to
1728 NEWPOS->charpos. POS is a known position in string STRING.
1729 NEWPOS->charpos must be >= POS.charpos. */
1731 static void
1732 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1734 eassert (STRINGP (string));
1735 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1737 if (STRING_MULTIBYTE (string))
1738 *newpos = string_pos_nchars_ahead (pos, string,
1739 CHARPOS (*newpos) - CHARPOS (pos));
1740 else
1741 BYTEPOS (*newpos) = CHARPOS (*newpos);
1744 /* EXPORT:
1745 Return an estimation of the pixel height of mode or header lines on
1746 frame F. FACE_ID specifies what line's height to estimate. */
1749 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1751 #ifdef HAVE_WINDOW_SYSTEM
1752 if (FRAME_WINDOW_P (f))
1754 int height = FONT_HEIGHT (FRAME_FONT (f));
1756 /* This function is called so early when Emacs starts that the face
1757 cache and mode line face are not yet initialized. */
1758 if (FRAME_FACE_CACHE (f))
1760 struct face *face = FACE_FROM_ID (f, face_id);
1761 if (face)
1763 if (face->font)
1764 height = FONT_HEIGHT (face->font);
1765 if (face->box_line_width > 0)
1766 height += 2 * face->box_line_width;
1770 return height;
1772 #endif
1774 return 1;
1777 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1778 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1779 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1780 not force the value into range. */
1782 void
1783 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1784 NativeRectangle *bounds, bool noclip)
1787 #ifdef HAVE_WINDOW_SYSTEM
1788 if (FRAME_WINDOW_P (f))
1790 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1791 even for negative values. */
1792 if (pix_x < 0)
1793 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1794 if (pix_y < 0)
1795 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1797 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1798 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1800 if (bounds)
1801 STORE_NATIVE_RECT (*bounds,
1802 FRAME_COL_TO_PIXEL_X (f, pix_x),
1803 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1804 FRAME_COLUMN_WIDTH (f) - 1,
1805 FRAME_LINE_HEIGHT (f) - 1);
1807 /* PXW: Should we clip pixels before converting to columns/lines? */
1808 if (!noclip)
1810 if (pix_x < 0)
1811 pix_x = 0;
1812 else if (pix_x > FRAME_TOTAL_COLS (f))
1813 pix_x = FRAME_TOTAL_COLS (f);
1815 if (pix_y < 0)
1816 pix_y = 0;
1817 else if (pix_y > FRAME_TOTAL_LINES (f))
1818 pix_y = FRAME_TOTAL_LINES (f);
1821 #endif
1823 *x = pix_x;
1824 *y = pix_y;
1828 /* Find the glyph under window-relative coordinates X/Y in window W.
1829 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1830 strings. Return in *HPOS and *VPOS the row and column number of
1831 the glyph found. Return in *AREA the glyph area containing X.
1832 Value is a pointer to the glyph found or null if X/Y is not on
1833 text, or we can't tell because W's current matrix is not up to
1834 date. */
1836 static struct glyph *
1837 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1838 int *dx, int *dy, int *area)
1840 struct glyph *glyph, *end;
1841 struct glyph_row *row = NULL;
1842 int x0, i;
1844 /* Find row containing Y. Give up if some row is not enabled. */
1845 for (i = 0; i < w->current_matrix->nrows; ++i)
1847 row = MATRIX_ROW (w->current_matrix, i);
1848 if (!row->enabled_p)
1849 return NULL;
1850 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1851 break;
1854 *vpos = i;
1855 *hpos = 0;
1857 /* Give up if Y is not in the window. */
1858 if (i == w->current_matrix->nrows)
1859 return NULL;
1861 /* Get the glyph area containing X. */
1862 if (w->pseudo_window_p)
1864 *area = TEXT_AREA;
1865 x0 = 0;
1867 else
1869 if (x < window_box_left_offset (w, TEXT_AREA))
1871 *area = LEFT_MARGIN_AREA;
1872 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1874 else if (x < window_box_right_offset (w, TEXT_AREA))
1876 *area = TEXT_AREA;
1877 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1879 else
1881 *area = RIGHT_MARGIN_AREA;
1882 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1886 /* Find glyph containing X. */
1887 glyph = row->glyphs[*area];
1888 end = glyph + row->used[*area];
1889 x -= x0;
1890 while (glyph < end && x >= glyph->pixel_width)
1892 x -= glyph->pixel_width;
1893 ++glyph;
1896 if (glyph == end)
1897 return NULL;
1899 if (dx)
1901 *dx = x;
1902 *dy = y - (row->y + row->ascent - glyph->ascent);
1905 *hpos = glyph - row->glyphs[*area];
1906 return glyph;
1909 /* Convert frame-relative x/y to coordinates relative to window W.
1910 Takes pseudo-windows into account. */
1912 static void
1913 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1915 if (w->pseudo_window_p)
1917 /* A pseudo-window is always full-width, and starts at the
1918 left edge of the frame, plus a frame border. */
1919 struct frame *f = XFRAME (w->frame);
1920 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1921 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1923 else
1925 *x -= WINDOW_LEFT_EDGE_X (w);
1926 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1930 #ifdef HAVE_WINDOW_SYSTEM
1932 /* EXPORT:
1933 Return in RECTS[] at most N clipping rectangles for glyph string S.
1934 Return the number of stored rectangles. */
1937 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1939 XRectangle r;
1941 if (n <= 0)
1942 return 0;
1944 if (s->row->full_width_p)
1946 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1947 r.x = WINDOW_LEFT_EDGE_X (s->w);
1948 if (s->row->mode_line_p)
1949 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1950 else
1951 r.width = WINDOW_PIXEL_WIDTH (s->w);
1953 /* Unless displaying a mode or menu bar line, which are always
1954 fully visible, clip to the visible part of the row. */
1955 if (s->w->pseudo_window_p)
1956 r.height = s->row->visible_height;
1957 else
1958 r.height = s->height;
1960 else
1962 /* This is a text line that may be partially visible. */
1963 r.x = window_box_left (s->w, s->area);
1964 r.width = window_box_width (s->w, s->area);
1965 r.height = s->row->visible_height;
1968 if (s->clip_head)
1969 if (r.x < s->clip_head->x)
1971 if (r.width >= s->clip_head->x - r.x)
1972 r.width -= s->clip_head->x - r.x;
1973 else
1974 r.width = 0;
1975 r.x = s->clip_head->x;
1977 if (s->clip_tail)
1978 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1980 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1981 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1982 else
1983 r.width = 0;
1986 /* If S draws overlapping rows, it's sufficient to use the top and
1987 bottom of the window for clipping because this glyph string
1988 intentionally draws over other lines. */
1989 if (s->for_overlaps)
1991 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1992 r.height = window_text_bottom_y (s->w) - r.y;
1994 /* Alas, the above simple strategy does not work for the
1995 environments with anti-aliased text: if the same text is
1996 drawn onto the same place multiple times, it gets thicker.
1997 If the overlap we are processing is for the erased cursor, we
1998 take the intersection with the rectangle of the cursor. */
1999 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2001 XRectangle rc, r_save = r;
2003 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2004 rc.y = s->w->phys_cursor.y;
2005 rc.width = s->w->phys_cursor_width;
2006 rc.height = s->w->phys_cursor_height;
2008 x_intersect_rectangles (&r_save, &rc, &r);
2011 else
2013 /* Don't use S->y for clipping because it doesn't take partially
2014 visible lines into account. For example, it can be negative for
2015 partially visible lines at the top of a window. */
2016 if (!s->row->full_width_p
2017 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2018 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2019 else
2020 r.y = max (0, s->row->y);
2023 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2025 /* If drawing the cursor, don't let glyph draw outside its
2026 advertised boundaries. Cleartype does this under some circumstances. */
2027 if (s->hl == DRAW_CURSOR)
2029 struct glyph *glyph = s->first_glyph;
2030 int height, max_y;
2032 if (s->x > r.x)
2034 if (r.width >= s->x - r.x)
2035 r.width -= s->x - r.x;
2036 else /* R2L hscrolled row with cursor outside text area */
2037 r.width = 0;
2038 r.x = s->x;
2040 r.width = min (r.width, glyph->pixel_width);
2042 /* If r.y is below window bottom, ensure that we still see a cursor. */
2043 height = min (glyph->ascent + glyph->descent,
2044 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2045 max_y = window_text_bottom_y (s->w) - height;
2046 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2047 if (s->ybase - glyph->ascent > max_y)
2049 r.y = max_y;
2050 r.height = height;
2052 else
2054 /* Don't draw cursor glyph taller than our actual glyph. */
2055 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2056 if (height < r.height)
2058 max_y = r.y + r.height;
2059 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2060 r.height = min (max_y - r.y, height);
2065 if (s->row->clip)
2067 XRectangle r_save = r;
2069 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2070 r.width = 0;
2073 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2074 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2076 #ifdef CONVERT_FROM_XRECT
2077 CONVERT_FROM_XRECT (r, *rects);
2078 #else
2079 *rects = r;
2080 #endif
2081 return 1;
2083 else
2085 /* If we are processing overlapping and allowed to return
2086 multiple clipping rectangles, we exclude the row of the glyph
2087 string from the clipping rectangle. This is to avoid drawing
2088 the same text on the environment with anti-aliasing. */
2089 #ifdef CONVERT_FROM_XRECT
2090 XRectangle rs[2];
2091 #else
2092 XRectangle *rs = rects;
2093 #endif
2094 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2096 if (s->for_overlaps & OVERLAPS_PRED)
2098 rs[i] = r;
2099 if (r.y + r.height > row_y)
2101 if (r.y < row_y)
2102 rs[i].height = row_y - r.y;
2103 else
2104 rs[i].height = 0;
2106 i++;
2108 if (s->for_overlaps & OVERLAPS_SUCC)
2110 rs[i] = r;
2111 if (r.y < row_y + s->row->visible_height)
2113 if (r.y + r.height > row_y + s->row->visible_height)
2115 rs[i].y = row_y + s->row->visible_height;
2116 rs[i].height = r.y + r.height - rs[i].y;
2118 else
2119 rs[i].height = 0;
2121 i++;
2124 n = i;
2125 #ifdef CONVERT_FROM_XRECT
2126 for (i = 0; i < n; i++)
2127 CONVERT_FROM_XRECT (rs[i], rects[i]);
2128 #endif
2129 return n;
2133 /* EXPORT:
2134 Return in *NR the clipping rectangle for glyph string S. */
2136 void
2137 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2139 get_glyph_string_clip_rects (s, nr, 1);
2143 /* EXPORT:
2144 Return the position and height of the phys cursor in window W.
2145 Set w->phys_cursor_width to width of phys cursor.
2148 void
2149 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2150 struct glyph *glyph, int *xp, int *yp, int *heightp)
2152 struct frame *f = XFRAME (WINDOW_FRAME (w));
2153 int x, y, wd, h, h0, y0;
2155 /* Compute the width of the rectangle to draw. If on a stretch
2156 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2157 rectangle as wide as the glyph, but use a canonical character
2158 width instead. */
2159 wd = glyph->pixel_width;
2161 x = w->phys_cursor.x;
2162 if (x < 0)
2164 wd += x;
2165 x = 0;
2168 if (glyph->type == STRETCH_GLYPH
2169 && !x_stretch_cursor_p)
2170 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2171 w->phys_cursor_width = wd;
2173 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2175 /* If y is below window bottom, ensure that we still see a cursor. */
2176 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2178 h = max (h0, glyph->ascent + glyph->descent);
2179 h0 = min (h0, glyph->ascent + glyph->descent);
2181 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2182 if (y < y0)
2184 h = max (h - (y0 - y) + 1, h0);
2185 y = y0 - 1;
2187 else
2189 y0 = window_text_bottom_y (w) - h0;
2190 if (y > y0)
2192 h += y - y0;
2193 y = y0;
2197 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2198 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2199 *heightp = h;
2203 * Remember which glyph the mouse is over.
2206 void
2207 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2209 Lisp_Object window;
2210 struct window *w;
2211 struct glyph_row *r, *gr, *end_row;
2212 enum window_part part;
2213 enum glyph_row_area area;
2214 int x, y, width, height;
2216 /* Try to determine frame pixel position and size of the glyph under
2217 frame pixel coordinates X/Y on frame F. */
2219 if (window_resize_pixelwise)
2221 width = height = 1;
2222 goto virtual_glyph;
2224 else if (!f->glyphs_initialized_p
2225 || (window = window_from_coordinates (f, gx, gy, &part, false),
2226 NILP (window)))
2228 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2229 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2230 goto virtual_glyph;
2233 w = XWINDOW (window);
2234 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2235 height = WINDOW_FRAME_LINE_HEIGHT (w);
2237 x = window_relative_x_coord (w, part, gx);
2238 y = gy - WINDOW_TOP_EDGE_Y (w);
2240 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2241 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2243 if (w->pseudo_window_p)
2245 area = TEXT_AREA;
2246 part = ON_MODE_LINE; /* Don't adjust margin. */
2247 goto text_glyph;
2250 switch (part)
2252 case ON_LEFT_MARGIN:
2253 area = LEFT_MARGIN_AREA;
2254 goto text_glyph;
2256 case ON_RIGHT_MARGIN:
2257 area = RIGHT_MARGIN_AREA;
2258 goto text_glyph;
2260 case ON_HEADER_LINE:
2261 case ON_MODE_LINE:
2262 gr = (part == ON_HEADER_LINE
2263 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2264 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2265 gy = gr->y;
2266 area = TEXT_AREA;
2267 goto text_glyph_row_found;
2269 case ON_TEXT:
2270 area = TEXT_AREA;
2272 text_glyph:
2273 gr = 0; gy = 0;
2274 for (; r <= end_row && r->enabled_p; ++r)
2275 if (r->y + r->height > y)
2277 gr = r; gy = r->y;
2278 break;
2281 text_glyph_row_found:
2282 if (gr && gy <= y)
2284 struct glyph *g = gr->glyphs[area];
2285 struct glyph *end = g + gr->used[area];
2287 height = gr->height;
2288 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2289 if (gx + g->pixel_width > x)
2290 break;
2292 if (g < end)
2294 if (g->type == IMAGE_GLYPH)
2296 /* Don't remember when mouse is over image, as
2297 image may have hot-spots. */
2298 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2299 return;
2301 width = g->pixel_width;
2303 else
2305 /* Use nominal char spacing at end of line. */
2306 x -= gx;
2307 gx += (x / width) * width;
2310 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2312 gx += window_box_left_offset (w, area);
2313 /* Don't expand over the modeline to make sure the vertical
2314 drag cursor is shown early enough. */
2315 height = min (height,
2316 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2319 else
2321 /* Use nominal line height at end of window. */
2322 gx = (x / width) * width;
2323 y -= gy;
2324 gy += (y / height) * height;
2325 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2326 /* See comment above. */
2327 height = min (height,
2328 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2330 break;
2332 case ON_LEFT_FRINGE:
2333 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2334 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2335 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2336 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2337 goto row_glyph;
2339 case ON_RIGHT_FRINGE:
2340 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2341 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2342 : window_box_right_offset (w, TEXT_AREA));
2343 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2344 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2345 && !WINDOW_RIGHTMOST_P (w))
2346 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2347 /* Make sure the vertical border can get her own glyph to the
2348 right of the one we build here. */
2349 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2350 else
2351 width = WINDOW_PIXEL_WIDTH (w) - gx;
2352 else
2353 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2355 goto row_glyph;
2357 case ON_VERTICAL_BORDER:
2358 gx = WINDOW_PIXEL_WIDTH (w) - width;
2359 goto row_glyph;
2361 case ON_VERTICAL_SCROLL_BAR:
2362 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2364 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2365 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2366 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2367 : 0)));
2368 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2370 row_glyph:
2371 gr = 0, gy = 0;
2372 for (; r <= end_row && r->enabled_p; ++r)
2373 if (r->y + r->height > y)
2375 gr = r; gy = r->y;
2376 break;
2379 if (gr && gy <= y)
2380 height = gr->height;
2381 else
2383 /* Use nominal line height at end of window. */
2384 y -= gy;
2385 gy += (y / height) * height;
2387 break;
2389 case ON_RIGHT_DIVIDER:
2390 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2391 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2392 gy = 0;
2393 /* The bottom divider prevails. */
2394 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2395 goto add_edge;
2397 case ON_BOTTOM_DIVIDER:
2398 gx = 0;
2399 width = WINDOW_PIXEL_WIDTH (w);
2400 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2401 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2402 goto add_edge;
2404 default:
2406 virtual_glyph:
2407 /* If there is no glyph under the mouse, then we divide the screen
2408 into a grid of the smallest glyph in the frame, and use that
2409 as our "glyph". */
2411 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2412 round down even for negative values. */
2413 if (gx < 0)
2414 gx -= width - 1;
2415 if (gy < 0)
2416 gy -= height - 1;
2418 gx = (gx / width) * width;
2419 gy = (gy / height) * height;
2421 goto store_rect;
2424 add_edge:
2425 gx += WINDOW_LEFT_EDGE_X (w);
2426 gy += WINDOW_TOP_EDGE_Y (w);
2428 store_rect:
2429 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2431 /* Visible feedback for debugging. */
2432 #if false && defined HAVE_X_WINDOWS
2433 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2434 f->output_data.x->normal_gc,
2435 gx, gy, width, height);
2436 #endif
2440 #endif /* HAVE_WINDOW_SYSTEM */
2442 static void
2443 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2445 eassert (w);
2446 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2447 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2448 w->window_end_vpos
2449 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2452 /***********************************************************************
2453 Lisp form evaluation
2454 ***********************************************************************/
2456 /* Error handler for safe_eval and safe_call. */
2458 static Lisp_Object
2459 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2461 add_to_log ("Error during redisplay: %S signaled %S",
2462 Flist (nargs, args), arg);
2463 return Qnil;
2466 /* Call function FUNC with the rest of NARGS - 1 arguments
2467 following. Return the result, or nil if something went
2468 wrong. Prevent redisplay during the evaluation. */
2470 static Lisp_Object
2471 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2473 Lisp_Object val;
2475 if (inhibit_eval_during_redisplay)
2476 val = Qnil;
2477 else
2479 ptrdiff_t i;
2480 ptrdiff_t count = SPECPDL_INDEX ();
2481 Lisp_Object *args;
2482 USE_SAFE_ALLOCA;
2483 SAFE_ALLOCA_LISP (args, nargs);
2485 args[0] = func;
2486 for (i = 1; i < nargs; i++)
2487 args[i] = va_arg (ap, Lisp_Object);
2489 specbind (Qinhibit_redisplay, Qt);
2490 if (inhibit_quit)
2491 specbind (Qinhibit_quit, Qt);
2492 /* Use Qt to ensure debugger does not run,
2493 so there is no possibility of wanting to redisplay. */
2494 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2495 safe_eval_handler);
2496 SAFE_FREE ();
2497 val = unbind_to (count, val);
2500 return val;
2503 Lisp_Object
2504 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2506 Lisp_Object retval;
2507 va_list ap;
2509 va_start (ap, func);
2510 retval = safe__call (false, nargs, func, ap);
2511 va_end (ap);
2512 return retval;
2515 /* Call function FN with one argument ARG.
2516 Return the result, or nil if something went wrong. */
2518 Lisp_Object
2519 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2521 return safe_call (2, fn, arg);
2524 static Lisp_Object
2525 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2527 Lisp_Object retval;
2528 va_list ap;
2530 va_start (ap, fn);
2531 retval = safe__call (inhibit_quit, 2, fn, ap);
2532 va_end (ap);
2533 return retval;
2536 Lisp_Object
2537 safe_eval (Lisp_Object sexpr)
2539 return safe__call1 (false, Qeval, sexpr);
2542 static Lisp_Object
2543 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2545 return safe__call1 (inhibit_quit, Qeval, sexpr);
2548 /* Call function FN with two arguments ARG1 and ARG2.
2549 Return the result, or nil if something went wrong. */
2551 Lisp_Object
2552 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2554 return safe_call (3, fn, arg1, arg2);
2559 /***********************************************************************
2560 Debugging
2561 ***********************************************************************/
2563 /* Define CHECK_IT to perform sanity checks on iterators.
2564 This is for debugging. It is too slow to do unconditionally. */
2566 static void
2567 CHECK_IT (struct it *it)
2569 #if false
2570 if (it->method == GET_FROM_STRING)
2572 eassert (STRINGP (it->string));
2573 eassert (IT_STRING_CHARPOS (*it) >= 0);
2575 else
2577 eassert (IT_STRING_CHARPOS (*it) < 0);
2578 if (it->method == GET_FROM_BUFFER)
2580 /* Check that character and byte positions agree. */
2581 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2585 if (it->dpvec)
2586 eassert (it->current.dpvec_index >= 0);
2587 else
2588 eassert (it->current.dpvec_index < 0);
2589 #endif
2593 /* Check that the window end of window W is what we expect it
2594 to be---the last row in the current matrix displaying text. */
2596 static void
2597 CHECK_WINDOW_END (struct window *w)
2599 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2600 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2602 struct glyph_row *row;
2603 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2604 !row->enabled_p
2605 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2606 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2608 #endif
2611 /***********************************************************************
2612 Iterator initialization
2613 ***********************************************************************/
2615 /* Initialize IT for displaying current_buffer in window W, starting
2616 at character position CHARPOS. CHARPOS < 0 means that no buffer
2617 position is specified which is useful when the iterator is assigned
2618 a position later. BYTEPOS is the byte position corresponding to
2619 CHARPOS.
2621 If ROW is not null, calls to produce_glyphs with IT as parameter
2622 will produce glyphs in that row.
2624 BASE_FACE_ID is the id of a base face to use. It must be one of
2625 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2626 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2627 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2629 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2630 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2631 will be initialized to use the corresponding mode line glyph row of
2632 the desired matrix of W. */
2634 void
2635 init_iterator (struct it *it, struct window *w,
2636 ptrdiff_t charpos, ptrdiff_t bytepos,
2637 struct glyph_row *row, enum face_id base_face_id)
2639 enum face_id remapped_base_face_id = base_face_id;
2641 /* Some precondition checks. */
2642 eassert (w != NULL && it != NULL);
2643 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2644 && charpos <= ZV));
2646 /* If face attributes have been changed since the last redisplay,
2647 free realized faces now because they depend on face definitions
2648 that might have changed. Don't free faces while there might be
2649 desired matrices pending which reference these faces. */
2650 if (face_change && !inhibit_free_realized_faces)
2652 face_change = false;
2653 free_all_realized_faces (Qnil);
2656 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2657 if (! NILP (Vface_remapping_alist))
2658 remapped_base_face_id
2659 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2661 /* Use one of the mode line rows of W's desired matrix if
2662 appropriate. */
2663 if (row == NULL)
2665 if (base_face_id == MODE_LINE_FACE_ID
2666 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2667 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2668 else if (base_face_id == HEADER_LINE_FACE_ID)
2669 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2672 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2673 Other parts of redisplay rely on that. */
2674 memclear (it, sizeof *it);
2675 it->current.overlay_string_index = -1;
2676 it->current.dpvec_index = -1;
2677 it->base_face_id = remapped_base_face_id;
2678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2679 it->paragraph_embedding = L2R;
2680 it->bidi_it.w = w;
2682 /* The window in which we iterate over current_buffer: */
2683 XSETWINDOW (it->window, w);
2684 it->w = w;
2685 it->f = XFRAME (w->frame);
2687 it->cmp_it.id = -1;
2689 /* Extra space between lines (on window systems only). */
2690 if (base_face_id == DEFAULT_FACE_ID
2691 && FRAME_WINDOW_P (it->f))
2693 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2694 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2695 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2696 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2697 * FRAME_LINE_HEIGHT (it->f));
2698 else if (it->f->extra_line_spacing > 0)
2699 it->extra_line_spacing = it->f->extra_line_spacing;
2702 /* If realized faces have been removed, e.g. because of face
2703 attribute changes of named faces, recompute them. When running
2704 in batch mode, the face cache of the initial frame is null. If
2705 we happen to get called, make a dummy face cache. */
2706 if (FRAME_FACE_CACHE (it->f) == NULL)
2707 init_frame_faces (it->f);
2708 if (FRAME_FACE_CACHE (it->f)->used == 0)
2709 recompute_basic_faces (it->f);
2711 it->override_ascent = -1;
2713 /* Are control characters displayed as `^C'? */
2714 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2716 /* -1 means everything between a CR and the following line end
2717 is invisible. >0 means lines indented more than this value are
2718 invisible. */
2719 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2720 ? (clip_to_bounds
2721 (-1, XINT (BVAR (current_buffer, selective_display)),
2722 PTRDIFF_MAX))
2723 : (!NILP (BVAR (current_buffer, selective_display))
2724 ? -1 : 0));
2725 it->selective_display_ellipsis_p
2726 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2728 /* Display table to use. */
2729 it->dp = window_display_table (w);
2731 /* Are multibyte characters enabled in current_buffer? */
2732 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2734 /* Get the position at which the redisplay_end_trigger hook should
2735 be run, if it is to be run at all. */
2736 if (MARKERP (w->redisplay_end_trigger)
2737 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2738 it->redisplay_end_trigger_charpos
2739 = marker_position (w->redisplay_end_trigger);
2740 else if (INTEGERP (w->redisplay_end_trigger))
2741 it->redisplay_end_trigger_charpos
2742 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2743 PTRDIFF_MAX);
2745 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2747 /* Are lines in the display truncated? */
2748 if (TRUNCATE != 0)
2749 it->line_wrap = TRUNCATE;
2750 if (base_face_id == DEFAULT_FACE_ID
2751 && !it->w->hscroll
2752 && (WINDOW_FULL_WIDTH_P (it->w)
2753 || NILP (Vtruncate_partial_width_windows)
2754 || (INTEGERP (Vtruncate_partial_width_windows)
2755 /* PXW: Shall we do something about this? */
2756 && (XINT (Vtruncate_partial_width_windows)
2757 <= WINDOW_TOTAL_COLS (it->w))))
2758 && NILP (BVAR (current_buffer, truncate_lines)))
2759 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2760 ? WINDOW_WRAP : WORD_WRAP;
2762 /* Get dimensions of truncation and continuation glyphs. These are
2763 displayed as fringe bitmaps under X, but we need them for such
2764 frames when the fringes are turned off. But leave the dimensions
2765 zero for tooltip frames, as these glyphs look ugly there and also
2766 sabotage calculations of tooltip dimensions in x-show-tip. */
2767 #ifdef HAVE_WINDOW_SYSTEM
2768 if (!(FRAME_WINDOW_P (it->f)
2769 && FRAMEP (tip_frame)
2770 && it->f == XFRAME (tip_frame)))
2771 #endif
2773 if (it->line_wrap == TRUNCATE)
2775 /* We will need the truncation glyph. */
2776 eassert (it->glyph_row == NULL);
2777 produce_special_glyphs (it, IT_TRUNCATION);
2778 it->truncation_pixel_width = it->pixel_width;
2780 else
2782 /* We will need the continuation glyph. */
2783 eassert (it->glyph_row == NULL);
2784 produce_special_glyphs (it, IT_CONTINUATION);
2785 it->continuation_pixel_width = it->pixel_width;
2789 /* Reset these values to zero because the produce_special_glyphs
2790 above has changed them. */
2791 it->pixel_width = it->ascent = it->descent = 0;
2792 it->phys_ascent = it->phys_descent = 0;
2794 /* Set this after getting the dimensions of truncation and
2795 continuation glyphs, so that we don't produce glyphs when calling
2796 produce_special_glyphs, above. */
2797 it->glyph_row = row;
2798 it->area = TEXT_AREA;
2800 /* Get the dimensions of the display area. The display area
2801 consists of the visible window area plus a horizontally scrolled
2802 part to the left of the window. All x-values are relative to the
2803 start of this total display area. */
2804 if (base_face_id != DEFAULT_FACE_ID)
2806 /* Mode lines, menu bar in terminal frames. */
2807 it->first_visible_x = 0;
2808 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2810 else
2812 it->first_visible_x
2813 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2814 it->last_visible_x = (it->first_visible_x
2815 + window_box_width (w, TEXT_AREA));
2817 /* If we truncate lines, leave room for the truncation glyph(s) at
2818 the right margin. Otherwise, leave room for the continuation
2819 glyph(s). Done only if the window has no right fringe. */
2820 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2822 if (it->line_wrap == TRUNCATE)
2823 it->last_visible_x -= it->truncation_pixel_width;
2824 else
2825 it->last_visible_x -= it->continuation_pixel_width;
2828 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2829 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2832 /* Leave room for a border glyph. */
2833 if (!FRAME_WINDOW_P (it->f)
2834 && !WINDOW_RIGHTMOST_P (it->w))
2835 it->last_visible_x -= 1;
2837 it->last_visible_y = window_text_bottom_y (w);
2839 /* For mode lines and alike, arrange for the first glyph having a
2840 left box line if the face specifies a box. */
2841 if (base_face_id != DEFAULT_FACE_ID)
2843 struct face *face;
2845 it->face_id = remapped_base_face_id;
2847 /* If we have a boxed mode line, make the first character appear
2848 with a left box line. */
2849 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2850 if (face && face->box != FACE_NO_BOX)
2851 it->start_of_box_run_p = true;
2854 /* If a buffer position was specified, set the iterator there,
2855 getting overlays and face properties from that position. */
2856 if (charpos >= BUF_BEG (current_buffer))
2858 it->stop_charpos = charpos;
2859 it->end_charpos = ZV;
2860 eassert (charpos == BYTE_TO_CHAR (bytepos));
2861 IT_CHARPOS (*it) = charpos;
2862 IT_BYTEPOS (*it) = bytepos;
2864 /* We will rely on `reseat' to set this up properly, via
2865 handle_face_prop. */
2866 it->face_id = it->base_face_id;
2868 it->start = it->current;
2869 /* Do we need to reorder bidirectional text? Not if this is a
2870 unibyte buffer: by definition, none of the single-byte
2871 characters are strong R2L, so no reordering is needed. And
2872 bidi.c doesn't support unibyte buffers anyway. Also, don't
2873 reorder while we are loading loadup.el, since the tables of
2874 character properties needed for reordering are not yet
2875 available. */
2876 it->bidi_p =
2877 NILP (Vpurify_flag)
2878 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2879 && it->multibyte_p;
2881 /* If we are to reorder bidirectional text, init the bidi
2882 iterator. */
2883 if (it->bidi_p)
2885 /* Since we don't know at this point whether there will be
2886 any R2L lines in the window, we reserve space for
2887 truncation/continuation glyphs even if only the left
2888 fringe is absent. */
2889 if (base_face_id == DEFAULT_FACE_ID
2890 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2891 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2893 if (it->line_wrap == TRUNCATE)
2894 it->last_visible_x -= it->truncation_pixel_width;
2895 else
2896 it->last_visible_x -= it->continuation_pixel_width;
2898 /* Note the paragraph direction that this buffer wants to
2899 use. */
2900 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2901 Qleft_to_right))
2902 it->paragraph_embedding = L2R;
2903 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2904 Qright_to_left))
2905 it->paragraph_embedding = R2L;
2906 else
2907 it->paragraph_embedding = NEUTRAL_DIR;
2908 bidi_unshelve_cache (NULL, false);
2909 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2910 &it->bidi_it);
2913 /* Compute faces etc. */
2914 reseat (it, it->current.pos, true);
2917 CHECK_IT (it);
2921 /* Initialize IT for the display of window W with window start POS. */
2923 void
2924 start_display (struct it *it, struct window *w, struct text_pos pos)
2926 struct glyph_row *row;
2927 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2929 row = w->desired_matrix->rows + first_vpos;
2930 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2931 it->first_vpos = first_vpos;
2933 /* Don't reseat to previous visible line start if current start
2934 position is in a string or image. */
2935 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2937 int first_y = it->current_y;
2939 /* If window start is not at a line start, skip forward to POS to
2940 get the correct continuation lines width. */
2941 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
2942 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2943 if (!start_at_line_beg_p)
2945 int new_x;
2947 reseat_at_previous_visible_line_start (it);
2948 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2950 new_x = it->current_x + it->pixel_width;
2952 /* If lines are continued, this line may end in the middle
2953 of a multi-glyph character (e.g. a control character
2954 displayed as \003, or in the middle of an overlay
2955 string). In this case move_it_to above will not have
2956 taken us to the start of the continuation line but to the
2957 end of the continued line. */
2958 if (it->current_x > 0
2959 && it->line_wrap != TRUNCATE /* Lines are continued. */
2960 && (/* And glyph doesn't fit on the line. */
2961 new_x > it->last_visible_x
2962 /* Or it fits exactly and we're on a window
2963 system frame. */
2964 || (new_x == it->last_visible_x
2965 && FRAME_WINDOW_P (it->f)
2966 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2967 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2968 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2970 if ((it->current.dpvec_index >= 0
2971 || it->current.overlay_string_index >= 0)
2972 /* If we are on a newline from a display vector or
2973 overlay string, then we are already at the end of
2974 a screen line; no need to go to the next line in
2975 that case, as this line is not really continued.
2976 (If we do go to the next line, C-e will not DTRT.) */
2977 && it->c != '\n')
2979 set_iterator_to_next (it, true);
2980 move_it_in_display_line_to (it, -1, -1, 0);
2983 it->continuation_lines_width += it->current_x;
2985 /* If the character at POS is displayed via a display
2986 vector, move_it_to above stops at the final glyph of
2987 IT->dpvec. To make the caller redisplay that character
2988 again (a.k.a. start at POS), we need to reset the
2989 dpvec_index to the beginning of IT->dpvec. */
2990 else if (it->current.dpvec_index >= 0)
2991 it->current.dpvec_index = 0;
2993 /* We're starting a new display line, not affected by the
2994 height of the continued line, so clear the appropriate
2995 fields in the iterator structure. */
2996 it->max_ascent = it->max_descent = 0;
2997 it->max_phys_ascent = it->max_phys_descent = 0;
2999 it->current_y = first_y;
3000 it->vpos = 0;
3001 it->current_x = it->hpos = 0;
3007 /* Return true if POS is a position in ellipses displayed for invisible
3008 text. W is the window we display, for text property lookup. */
3010 static bool
3011 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3013 Lisp_Object prop, window;
3014 bool ellipses_p = false;
3015 ptrdiff_t charpos = CHARPOS (pos->pos);
3017 /* If POS specifies a position in a display vector, this might
3018 be for an ellipsis displayed for invisible text. We won't
3019 get the iterator set up for delivering that ellipsis unless
3020 we make sure that it gets aware of the invisible text. */
3021 if (pos->dpvec_index >= 0
3022 && pos->overlay_string_index < 0
3023 && CHARPOS (pos->string_pos) < 0
3024 && charpos > BEGV
3025 && (XSETWINDOW (window, w),
3026 prop = Fget_char_property (make_number (charpos),
3027 Qinvisible, window),
3028 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3030 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3031 window);
3032 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3035 return ellipses_p;
3039 /* Initialize IT for stepping through current_buffer in window W,
3040 starting at position POS that includes overlay string and display
3041 vector/ control character translation position information. Value
3042 is false if there are overlay strings with newlines at POS. */
3044 static bool
3045 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3047 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3048 int i;
3049 bool overlay_strings_with_newlines = false;
3051 /* If POS specifies a position in a display vector, this might
3052 be for an ellipsis displayed for invisible text. We won't
3053 get the iterator set up for delivering that ellipsis unless
3054 we make sure that it gets aware of the invisible text. */
3055 if (in_ellipses_for_invisible_text_p (pos, w))
3057 --charpos;
3058 bytepos = 0;
3061 /* Keep in mind: the call to reseat in init_iterator skips invisible
3062 text, so we might end up at a position different from POS. This
3063 is only a problem when POS is a row start after a newline and an
3064 overlay starts there with an after-string, and the overlay has an
3065 invisible property. Since we don't skip invisible text in
3066 display_line and elsewhere immediately after consuming the
3067 newline before the row start, such a POS will not be in a string,
3068 but the call to init_iterator below will move us to the
3069 after-string. */
3070 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3072 /* This only scans the current chunk -- it should scan all chunks.
3073 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3074 to 16 in 22.1 to make this a lesser problem. */
3075 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3077 const char *s = SSDATA (it->overlay_strings[i]);
3078 const char *e = s + SBYTES (it->overlay_strings[i]);
3080 while (s < e && *s != '\n')
3081 ++s;
3083 if (s < e)
3085 overlay_strings_with_newlines = true;
3086 break;
3090 /* If position is within an overlay string, set up IT to the right
3091 overlay string. */
3092 if (pos->overlay_string_index >= 0)
3094 int relative_index;
3096 /* If the first overlay string happens to have a `display'
3097 property for an image, the iterator will be set up for that
3098 image, and we have to undo that setup first before we can
3099 correct the overlay string index. */
3100 if (it->method == GET_FROM_IMAGE)
3101 pop_it (it);
3103 /* We already have the first chunk of overlay strings in
3104 IT->overlay_strings. Load more until the one for
3105 pos->overlay_string_index is in IT->overlay_strings. */
3106 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3108 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3109 it->current.overlay_string_index = 0;
3110 while (n--)
3112 load_overlay_strings (it, 0);
3113 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3117 it->current.overlay_string_index = pos->overlay_string_index;
3118 relative_index = (it->current.overlay_string_index
3119 % OVERLAY_STRING_CHUNK_SIZE);
3120 it->string = it->overlay_strings[relative_index];
3121 eassert (STRINGP (it->string));
3122 it->current.string_pos = pos->string_pos;
3123 it->method = GET_FROM_STRING;
3124 it->end_charpos = SCHARS (it->string);
3125 /* Set up the bidi iterator for this overlay string. */
3126 if (it->bidi_p)
3128 it->bidi_it.string.lstring = it->string;
3129 it->bidi_it.string.s = NULL;
3130 it->bidi_it.string.schars = SCHARS (it->string);
3131 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3132 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3133 it->bidi_it.string.unibyte = !it->multibyte_p;
3134 it->bidi_it.w = it->w;
3135 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3136 FRAME_WINDOW_P (it->f), &it->bidi_it);
3138 /* Synchronize the state of the bidi iterator with
3139 pos->string_pos. For any string position other than
3140 zero, this will be done automagically when we resume
3141 iteration over the string and get_visually_first_element
3142 is called. But if string_pos is zero, and the string is
3143 to be reordered for display, we need to resync manually,
3144 since it could be that the iteration state recorded in
3145 pos ended at string_pos of 0 moving backwards in string. */
3146 if (CHARPOS (pos->string_pos) == 0)
3148 get_visually_first_element (it);
3149 if (IT_STRING_CHARPOS (*it) != 0)
3150 do {
3151 /* Paranoia. */
3152 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3153 bidi_move_to_visually_next (&it->bidi_it);
3154 } while (it->bidi_it.charpos != 0);
3156 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3157 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3161 if (CHARPOS (pos->string_pos) >= 0)
3163 /* Recorded position is not in an overlay string, but in another
3164 string. This can only be a string from a `display' property.
3165 IT should already be filled with that string. */
3166 it->current.string_pos = pos->string_pos;
3167 eassert (STRINGP (it->string));
3168 if (it->bidi_p)
3169 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3170 FRAME_WINDOW_P (it->f), &it->bidi_it);
3173 /* Restore position in display vector translations, control
3174 character translations or ellipses. */
3175 if (pos->dpvec_index >= 0)
3177 if (it->dpvec == NULL)
3178 get_next_display_element (it);
3179 eassert (it->dpvec && it->current.dpvec_index == 0);
3180 it->current.dpvec_index = pos->dpvec_index;
3183 CHECK_IT (it);
3184 return !overlay_strings_with_newlines;
3188 /* Initialize IT for stepping through current_buffer in window W
3189 starting at ROW->start. */
3191 static void
3192 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3194 init_from_display_pos (it, w, &row->start);
3195 it->start = row->start;
3196 it->continuation_lines_width = row->continuation_lines_width;
3197 CHECK_IT (it);
3201 /* Initialize IT for stepping through current_buffer in window W
3202 starting in the line following ROW, i.e. starting at ROW->end.
3203 Value is false if there are overlay strings with newlines at ROW's
3204 end position. */
3206 static bool
3207 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3209 bool success = false;
3211 if (init_from_display_pos (it, w, &row->end))
3213 if (row->continued_p)
3214 it->continuation_lines_width
3215 = row->continuation_lines_width + row->pixel_width;
3216 CHECK_IT (it);
3217 success = true;
3220 return success;
3226 /***********************************************************************
3227 Text properties
3228 ***********************************************************************/
3230 /* Called when IT reaches IT->stop_charpos. Handle text property and
3231 overlay changes. Set IT->stop_charpos to the next position where
3232 to stop. */
3234 static void
3235 handle_stop (struct it *it)
3237 enum prop_handled handled;
3238 bool handle_overlay_change_p;
3239 struct props *p;
3241 it->dpvec = NULL;
3242 it->current.dpvec_index = -1;
3243 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3244 it->ellipsis_p = false;
3246 /* Use face of preceding text for ellipsis (if invisible) */
3247 if (it->selective_display_ellipsis_p)
3248 it->saved_face_id = it->face_id;
3250 /* Here's the description of the semantics of, and the logic behind,
3251 the various HANDLED_* statuses:
3253 HANDLED_NORMALLY means the handler did its job, and the loop
3254 should proceed to calling the next handler in order.
3256 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3257 change in the properties and overlays at current position, so the
3258 loop should be restarted, to re-invoke the handlers that were
3259 already called. This happens when fontification-functions were
3260 called by handle_fontified_prop, and actually fontified
3261 something. Another case where HANDLED_RECOMPUTE_PROPS is
3262 returned is when we discover overlay strings that need to be
3263 displayed right away. The loop below will continue for as long
3264 as the status is HANDLED_RECOMPUTE_PROPS.
3266 HANDLED_RETURN means return immediately to the caller, to
3267 continue iteration without calling any further handlers. This is
3268 used when we need to act on some property right away, for example
3269 when we need to display the ellipsis or a replacing display
3270 property, such as display string or image.
3272 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3273 consumed, and the handler switched to the next overlay string.
3274 This signals the loop below to refrain from looking for more
3275 overlays before all the overlay strings of the current overlay
3276 are processed.
3278 Some of the handlers called by the loop push the iterator state
3279 onto the stack (see 'push_it'), and arrange for the iteration to
3280 continue with another object, such as an image, a display string,
3281 or an overlay string. In most such cases, it->stop_charpos is
3282 set to the first character of the string, so that when the
3283 iteration resumes, this function will immediately be called
3284 again, to examine the properties at the beginning of the string.
3286 When a display or overlay string is exhausted, the iterator state
3287 is popped (see 'pop_it'), and iteration continues with the
3288 previous object. Again, in many such cases this function is
3289 called again to find the next position where properties might
3290 change. */
3294 handled = HANDLED_NORMALLY;
3296 /* Call text property handlers. */
3297 for (p = it_props; p->handler; ++p)
3299 handled = p->handler (it);
3301 if (handled == HANDLED_RECOMPUTE_PROPS)
3302 break;
3303 else if (handled == HANDLED_RETURN)
3305 /* We still want to show before and after strings from
3306 overlays even if the actual buffer text is replaced. */
3307 if (!handle_overlay_change_p
3308 || it->sp > 1
3309 /* Don't call get_overlay_strings_1 if we already
3310 have overlay strings loaded, because doing so
3311 will load them again and push the iterator state
3312 onto the stack one more time, which is not
3313 expected by the rest of the code that processes
3314 overlay strings. */
3315 || (it->current.overlay_string_index < 0
3316 && !get_overlay_strings_1 (it, 0, false)))
3318 if (it->ellipsis_p)
3319 setup_for_ellipsis (it, 0);
3320 /* When handling a display spec, we might load an
3321 empty string. In that case, discard it here. We
3322 used to discard it in handle_single_display_spec,
3323 but that causes get_overlay_strings_1, above, to
3324 ignore overlay strings that we must check. */
3325 if (STRINGP (it->string) && !SCHARS (it->string))
3326 pop_it (it);
3327 return;
3329 else if (STRINGP (it->string) && !SCHARS (it->string))
3330 pop_it (it);
3331 else
3333 it->string_from_display_prop_p = false;
3334 it->from_disp_prop_p = false;
3335 handle_overlay_change_p = false;
3337 handled = HANDLED_RECOMPUTE_PROPS;
3338 break;
3340 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3341 handle_overlay_change_p = false;
3344 if (handled != HANDLED_RECOMPUTE_PROPS)
3346 /* Don't check for overlay strings below when set to deliver
3347 characters from a display vector. */
3348 if (it->method == GET_FROM_DISPLAY_VECTOR)
3349 handle_overlay_change_p = false;
3351 /* Handle overlay changes.
3352 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3353 if it finds overlays. */
3354 if (handle_overlay_change_p)
3355 handled = handle_overlay_change (it);
3358 if (it->ellipsis_p)
3360 setup_for_ellipsis (it, 0);
3361 break;
3364 while (handled == HANDLED_RECOMPUTE_PROPS);
3366 /* Determine where to stop next. */
3367 if (handled == HANDLED_NORMALLY)
3368 compute_stop_pos (it);
3372 /* Compute IT->stop_charpos from text property and overlay change
3373 information for IT's current position. */
3375 static void
3376 compute_stop_pos (struct it *it)
3378 register INTERVAL iv, next_iv;
3379 Lisp_Object object, limit, position;
3380 ptrdiff_t charpos, bytepos;
3382 if (STRINGP (it->string))
3384 /* Strings are usually short, so don't limit the search for
3385 properties. */
3386 it->stop_charpos = it->end_charpos;
3387 object = it->string;
3388 limit = Qnil;
3389 charpos = IT_STRING_CHARPOS (*it);
3390 bytepos = IT_STRING_BYTEPOS (*it);
3392 else
3394 ptrdiff_t pos;
3396 /* If end_charpos is out of range for some reason, such as a
3397 misbehaving display function, rationalize it (Bug#5984). */
3398 if (it->end_charpos > ZV)
3399 it->end_charpos = ZV;
3400 it->stop_charpos = it->end_charpos;
3402 /* If next overlay change is in front of the current stop pos
3403 (which is IT->end_charpos), stop there. Note: value of
3404 next_overlay_change is point-max if no overlay change
3405 follows. */
3406 charpos = IT_CHARPOS (*it);
3407 bytepos = IT_BYTEPOS (*it);
3408 pos = next_overlay_change (charpos);
3409 if (pos < it->stop_charpos)
3410 it->stop_charpos = pos;
3412 /* Set up variables for computing the stop position from text
3413 property changes. */
3414 XSETBUFFER (object, current_buffer);
3415 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3418 /* Get the interval containing IT's position. Value is a null
3419 interval if there isn't such an interval. */
3420 position = make_number (charpos);
3421 iv = validate_interval_range (object, &position, &position, false);
3422 if (iv)
3424 Lisp_Object values_here[LAST_PROP_IDX];
3425 struct props *p;
3427 /* Get properties here. */
3428 for (p = it_props; p->handler; ++p)
3429 values_here[p->idx] = textget (iv->plist,
3430 builtin_lisp_symbol (p->name));
3432 /* Look for an interval following iv that has different
3433 properties. */
3434 for (next_iv = next_interval (iv);
3435 (next_iv
3436 && (NILP (limit)
3437 || XFASTINT (limit) > next_iv->position));
3438 next_iv = next_interval (next_iv))
3440 for (p = it_props; p->handler; ++p)
3442 Lisp_Object new_value = textget (next_iv->plist,
3443 builtin_lisp_symbol (p->name));
3444 if (!EQ (values_here[p->idx], new_value))
3445 break;
3448 if (p->handler)
3449 break;
3452 if (next_iv)
3454 if (INTEGERP (limit)
3455 && next_iv->position >= XFASTINT (limit))
3456 /* No text property change up to limit. */
3457 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3458 else
3459 /* Text properties change in next_iv. */
3460 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3464 if (it->cmp_it.id < 0)
3466 ptrdiff_t stoppos = it->end_charpos;
3468 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3469 stoppos = -1;
3470 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3471 stoppos, it->string);
3474 eassert (STRINGP (it->string)
3475 || (it->stop_charpos >= BEGV
3476 && it->stop_charpos >= IT_CHARPOS (*it)));
3480 /* Return the position of the next overlay change after POS in
3481 current_buffer. Value is point-max if no overlay change
3482 follows. This is like `next-overlay-change' but doesn't use
3483 xmalloc. */
3485 static ptrdiff_t
3486 next_overlay_change (ptrdiff_t pos)
3488 ptrdiff_t i, noverlays;
3489 ptrdiff_t endpos;
3490 Lisp_Object *overlays;
3491 USE_SAFE_ALLOCA;
3493 /* Get all overlays at the given position. */
3494 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3496 /* If any of these overlays ends before endpos,
3497 use its ending point instead. */
3498 for (i = 0; i < noverlays; ++i)
3500 Lisp_Object oend;
3501 ptrdiff_t oendpos;
3503 oend = OVERLAY_END (overlays[i]);
3504 oendpos = OVERLAY_POSITION (oend);
3505 endpos = min (endpos, oendpos);
3508 SAFE_FREE ();
3509 return endpos;
3512 /* How many characters forward to search for a display property or
3513 display string. Searching too far forward makes the bidi display
3514 sluggish, especially in small windows. */
3515 #define MAX_DISP_SCAN 250
3517 /* Return the character position of a display string at or after
3518 position specified by POSITION. If no display string exists at or
3519 after POSITION, return ZV. A display string is either an overlay
3520 with `display' property whose value is a string, or a `display'
3521 text property whose value is a string. STRING is data about the
3522 string to iterate; if STRING->lstring is nil, we are iterating a
3523 buffer. FRAME_WINDOW_P is true when we are displaying a window
3524 on a GUI frame. DISP_PROP is set to zero if we searched
3525 MAX_DISP_SCAN characters forward without finding any display
3526 strings, non-zero otherwise. It is set to 2 if the display string
3527 uses any kind of `(space ...)' spec that will produce a stretch of
3528 white space in the text area. */
3529 ptrdiff_t
3530 compute_display_string_pos (struct text_pos *position,
3531 struct bidi_string_data *string,
3532 struct window *w,
3533 bool frame_window_p, int *disp_prop)
3535 /* OBJECT = nil means current buffer. */
3536 Lisp_Object object, object1;
3537 Lisp_Object pos, spec, limpos;
3538 bool string_p = string && (STRINGP (string->lstring) || string->s);
3539 ptrdiff_t eob = string_p ? string->schars : ZV;
3540 ptrdiff_t begb = string_p ? 0 : BEGV;
3541 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3542 ptrdiff_t lim =
3543 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3544 struct text_pos tpos;
3545 int rv = 0;
3547 if (string && STRINGP (string->lstring))
3548 object1 = object = string->lstring;
3549 else if (w && !string_p)
3551 XSETWINDOW (object, w);
3552 object1 = Qnil;
3554 else
3555 object1 = object = Qnil;
3557 *disp_prop = 1;
3559 if (charpos >= eob
3560 /* We don't support display properties whose values are strings
3561 that have display string properties. */
3562 || string->from_disp_str
3563 /* C strings cannot have display properties. */
3564 || (string->s && !STRINGP (object)))
3566 *disp_prop = 0;
3567 return eob;
3570 /* If the character at CHARPOS is where the display string begins,
3571 return CHARPOS. */
3572 pos = make_number (charpos);
3573 if (STRINGP (object))
3574 bufpos = string->bufpos;
3575 else
3576 bufpos = charpos;
3577 tpos = *position;
3578 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3579 && (charpos <= begb
3580 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3581 object),
3582 spec))
3583 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3584 frame_window_p)))
3586 if (rv == 2)
3587 *disp_prop = 2;
3588 return charpos;
3591 /* Look forward for the first character with a `display' property
3592 that will replace the underlying text when displayed. */
3593 limpos = make_number (lim);
3594 do {
3595 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3596 CHARPOS (tpos) = XFASTINT (pos);
3597 if (CHARPOS (tpos) >= lim)
3599 *disp_prop = 0;
3600 break;
3602 if (STRINGP (object))
3603 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3604 else
3605 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3606 spec = Fget_char_property (pos, Qdisplay, object);
3607 if (!STRINGP (object))
3608 bufpos = CHARPOS (tpos);
3609 } while (NILP (spec)
3610 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3611 bufpos, frame_window_p)));
3612 if (rv == 2)
3613 *disp_prop = 2;
3615 return CHARPOS (tpos);
3618 /* Return the character position of the end of the display string that
3619 started at CHARPOS. If there's no display string at CHARPOS,
3620 return -1. A display string is either an overlay with `display'
3621 property whose value is a string or a `display' text property whose
3622 value is a string. */
3623 ptrdiff_t
3624 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3626 /* OBJECT = nil means current buffer. */
3627 Lisp_Object object =
3628 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3629 Lisp_Object pos = make_number (charpos);
3630 ptrdiff_t eob =
3631 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3633 if (charpos >= eob || (string->s && !STRINGP (object)))
3634 return eob;
3636 /* It could happen that the display property or overlay was removed
3637 since we found it in compute_display_string_pos above. One way
3638 this can happen is if JIT font-lock was called (through
3639 handle_fontified_prop), and jit-lock-functions remove text
3640 properties or overlays from the portion of buffer that includes
3641 CHARPOS. Muse mode is known to do that, for example. In this
3642 case, we return -1 to the caller, to signal that no display
3643 string is actually present at CHARPOS. See bidi_fetch_char for
3644 how this is handled.
3646 An alternative would be to never look for display properties past
3647 it->stop_charpos. But neither compute_display_string_pos nor
3648 bidi_fetch_char that calls it know or care where the next
3649 stop_charpos is. */
3650 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3651 return -1;
3653 /* Look forward for the first character where the `display' property
3654 changes. */
3655 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3657 return XFASTINT (pos);
3662 /***********************************************************************
3663 Fontification
3664 ***********************************************************************/
3666 /* Handle changes in the `fontified' property of the current buffer by
3667 calling hook functions from Qfontification_functions to fontify
3668 regions of text. */
3670 static enum prop_handled
3671 handle_fontified_prop (struct it *it)
3673 Lisp_Object prop, pos;
3674 enum prop_handled handled = HANDLED_NORMALLY;
3676 if (!NILP (Vmemory_full))
3677 return handled;
3679 /* Get the value of the `fontified' property at IT's current buffer
3680 position. (The `fontified' property doesn't have a special
3681 meaning in strings.) If the value is nil, call functions from
3682 Qfontification_functions. */
3683 if (!STRINGP (it->string)
3684 && it->s == NULL
3685 && !NILP (Vfontification_functions)
3686 && !NILP (Vrun_hooks)
3687 && (pos = make_number (IT_CHARPOS (*it)),
3688 prop = Fget_char_property (pos, Qfontified, Qnil),
3689 /* Ignore the special cased nil value always present at EOB since
3690 no amount of fontifying will be able to change it. */
3691 NILP (prop) && IT_CHARPOS (*it) < Z))
3693 ptrdiff_t count = SPECPDL_INDEX ();
3694 Lisp_Object val;
3695 struct buffer *obuf = current_buffer;
3696 ptrdiff_t begv = BEGV, zv = ZV;
3697 bool old_clip_changed = current_buffer->clip_changed;
3699 val = Vfontification_functions;
3700 specbind (Qfontification_functions, Qnil);
3702 eassert (it->end_charpos == ZV);
3704 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3705 safe_call1 (val, pos);
3706 else
3708 Lisp_Object fns, fn;
3709 struct gcpro gcpro1, gcpro2;
3711 fns = Qnil;
3712 GCPRO2 (val, fns);
3714 for (; CONSP (val); val = XCDR (val))
3716 fn = XCAR (val);
3718 if (EQ (fn, Qt))
3720 /* A value of t indicates this hook has a local
3721 binding; it means to run the global binding too.
3722 In a global value, t should not occur. If it
3723 does, we must ignore it to avoid an endless
3724 loop. */
3725 for (fns = Fdefault_value (Qfontification_functions);
3726 CONSP (fns);
3727 fns = XCDR (fns))
3729 fn = XCAR (fns);
3730 if (!EQ (fn, Qt))
3731 safe_call1 (fn, pos);
3734 else
3735 safe_call1 (fn, pos);
3738 UNGCPRO;
3741 unbind_to (count, Qnil);
3743 /* Fontification functions routinely call `save-restriction'.
3744 Normally, this tags clip_changed, which can confuse redisplay
3745 (see discussion in Bug#6671). Since we don't perform any
3746 special handling of fontification changes in the case where
3747 `save-restriction' isn't called, there's no point doing so in
3748 this case either. So, if the buffer's restrictions are
3749 actually left unchanged, reset clip_changed. */
3750 if (obuf == current_buffer)
3752 if (begv == BEGV && zv == ZV)
3753 current_buffer->clip_changed = old_clip_changed;
3755 /* There isn't much we can reasonably do to protect against
3756 misbehaving fontification, but here's a fig leaf. */
3757 else if (BUFFER_LIVE_P (obuf))
3758 set_buffer_internal_1 (obuf);
3760 /* The fontification code may have added/removed text.
3761 It could do even a lot worse, but let's at least protect against
3762 the most obvious case where only the text past `pos' gets changed',
3763 as is/was done in grep.el where some escapes sequences are turned
3764 into face properties (bug#7876). */
3765 it->end_charpos = ZV;
3767 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3768 something. This avoids an endless loop if they failed to
3769 fontify the text for which reason ever. */
3770 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3771 handled = HANDLED_RECOMPUTE_PROPS;
3774 return handled;
3779 /***********************************************************************
3780 Faces
3781 ***********************************************************************/
3783 /* Set up iterator IT from face properties at its current position.
3784 Called from handle_stop. */
3786 static enum prop_handled
3787 handle_face_prop (struct it *it)
3789 int new_face_id;
3790 ptrdiff_t next_stop;
3792 if (!STRINGP (it->string))
3794 new_face_id
3795 = face_at_buffer_position (it->w,
3796 IT_CHARPOS (*it),
3797 &next_stop,
3798 (IT_CHARPOS (*it)
3799 + TEXT_PROP_DISTANCE_LIMIT),
3800 false, it->base_face_id);
3802 /* Is this a start of a run of characters with box face?
3803 Caveat: this can be called for a freshly initialized
3804 iterator; face_id is -1 in this case. We know that the new
3805 face will not change until limit, i.e. if the new face has a
3806 box, all characters up to limit will have one. But, as
3807 usual, we don't know whether limit is really the end. */
3808 if (new_face_id != it->face_id)
3810 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3811 /* If it->face_id is -1, old_face below will be NULL, see
3812 the definition of FACE_FROM_ID. This will happen if this
3813 is the initial call that gets the face. */
3814 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3816 /* If the value of face_id of the iterator is -1, we have to
3817 look in front of IT's position and see whether there is a
3818 face there that's different from new_face_id. */
3819 if (!old_face && IT_CHARPOS (*it) > BEG)
3821 int prev_face_id = face_before_it_pos (it);
3823 old_face = FACE_FROM_ID (it->f, prev_face_id);
3826 /* If the new face has a box, but the old face does not,
3827 this is the start of a run of characters with box face,
3828 i.e. this character has a shadow on the left side. */
3829 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3830 && (old_face == NULL || !old_face->box));
3831 it->face_box_p = new_face->box != FACE_NO_BOX;
3834 else
3836 int base_face_id;
3837 ptrdiff_t bufpos;
3838 int i;
3839 Lisp_Object from_overlay
3840 = (it->current.overlay_string_index >= 0
3841 ? it->string_overlays[it->current.overlay_string_index
3842 % OVERLAY_STRING_CHUNK_SIZE]
3843 : Qnil);
3845 /* See if we got to this string directly or indirectly from
3846 an overlay property. That includes the before-string or
3847 after-string of an overlay, strings in display properties
3848 provided by an overlay, their text properties, etc.
3850 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3851 if (! NILP (from_overlay))
3852 for (i = it->sp - 1; i >= 0; i--)
3854 if (it->stack[i].current.overlay_string_index >= 0)
3855 from_overlay
3856 = it->string_overlays[it->stack[i].current.overlay_string_index
3857 % OVERLAY_STRING_CHUNK_SIZE];
3858 else if (! NILP (it->stack[i].from_overlay))
3859 from_overlay = it->stack[i].from_overlay;
3861 if (!NILP (from_overlay))
3862 break;
3865 if (! NILP (from_overlay))
3867 bufpos = IT_CHARPOS (*it);
3868 /* For a string from an overlay, the base face depends
3869 only on text properties and ignores overlays. */
3870 base_face_id
3871 = face_for_overlay_string (it->w,
3872 IT_CHARPOS (*it),
3873 &next_stop,
3874 (IT_CHARPOS (*it)
3875 + TEXT_PROP_DISTANCE_LIMIT),
3876 false,
3877 from_overlay);
3879 else
3881 bufpos = 0;
3883 /* For strings from a `display' property, use the face at
3884 IT's current buffer position as the base face to merge
3885 with, so that overlay strings appear in the same face as
3886 surrounding text, unless they specify their own faces.
3887 For strings from wrap-prefix and line-prefix properties,
3888 use the default face, possibly remapped via
3889 Vface_remapping_alist. */
3890 /* Note that the fact that we use the face at _buffer_
3891 position means that a 'display' property on an overlay
3892 string will not inherit the face of that overlay string,
3893 but will instead revert to the face of buffer text
3894 covered by the overlay. This is visible, e.g., when the
3895 overlay specifies a box face, but neither the buffer nor
3896 the display string do. This sounds like a design bug,
3897 but Emacs always did that since v21.1, so changing that
3898 might be a big deal. */
3899 base_face_id = it->string_from_prefix_prop_p
3900 ? (!NILP (Vface_remapping_alist)
3901 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3902 : DEFAULT_FACE_ID)
3903 : underlying_face_id (it);
3906 new_face_id = face_at_string_position (it->w,
3907 it->string,
3908 IT_STRING_CHARPOS (*it),
3909 bufpos,
3910 &next_stop,
3911 base_face_id, false);
3913 /* Is this a start of a run of characters with box? Caveat:
3914 this can be called for a freshly allocated iterator; face_id
3915 is -1 is this case. We know that the new face will not
3916 change until the next check pos, i.e. if the new face has a
3917 box, all characters up to that position will have a
3918 box. But, as usual, we don't know whether that position
3919 is really the end. */
3920 if (new_face_id != it->face_id)
3922 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3923 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3925 /* If new face has a box but old face hasn't, this is the
3926 start of a run of characters with box, i.e. it has a
3927 shadow on the left side. */
3928 it->start_of_box_run_p
3929 = new_face->box && (old_face == NULL || !old_face->box);
3930 it->face_box_p = new_face->box != FACE_NO_BOX;
3934 it->face_id = new_face_id;
3935 return HANDLED_NORMALLY;
3939 /* Return the ID of the face ``underlying'' IT's current position,
3940 which is in a string. If the iterator is associated with a
3941 buffer, return the face at IT's current buffer position.
3942 Otherwise, use the iterator's base_face_id. */
3944 static int
3945 underlying_face_id (struct it *it)
3947 int face_id = it->base_face_id, i;
3949 eassert (STRINGP (it->string));
3951 for (i = it->sp - 1; i >= 0; --i)
3952 if (NILP (it->stack[i].string))
3953 face_id = it->stack[i].face_id;
3955 return face_id;
3959 /* Compute the face one character before or after the current position
3960 of IT, in the visual order. BEFORE_P means get the face
3961 in front (to the left in L2R paragraphs, to the right in R2L
3962 paragraphs) of IT's screen position. Value is the ID of the face. */
3964 static int
3965 face_before_or_after_it_pos (struct it *it, bool before_p)
3967 int face_id, limit;
3968 ptrdiff_t next_check_charpos;
3969 struct it it_copy;
3970 void *it_copy_data = NULL;
3972 eassert (it->s == NULL);
3974 if (STRINGP (it->string))
3976 ptrdiff_t bufpos, charpos;
3977 int base_face_id;
3979 /* No face change past the end of the string (for the case
3980 we are padding with spaces). No face change before the
3981 string start. */
3982 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3983 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3984 return it->face_id;
3986 if (!it->bidi_p)
3988 /* Set charpos to the position before or after IT's current
3989 position, in the logical order, which in the non-bidi
3990 case is the same as the visual order. */
3991 if (before_p)
3992 charpos = IT_STRING_CHARPOS (*it) - 1;
3993 else if (it->what == IT_COMPOSITION)
3994 /* For composition, we must check the character after the
3995 composition. */
3996 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3997 else
3998 charpos = IT_STRING_CHARPOS (*it) + 1;
4000 else
4002 if (before_p)
4004 /* With bidi iteration, the character before the current
4005 in the visual order cannot be found by simple
4006 iteration, because "reverse" reordering is not
4007 supported. Instead, we need to use the move_it_*
4008 family of functions. */
4009 /* Ignore face changes before the first visible
4010 character on this display line. */
4011 if (it->current_x <= it->first_visible_x)
4012 return it->face_id;
4013 SAVE_IT (it_copy, *it, it_copy_data);
4014 /* Implementation note: Since move_it_in_display_line
4015 works in the iterator geometry, and thinks the first
4016 character is always the leftmost, even in R2L lines,
4017 we don't need to distinguish between the R2L and L2R
4018 cases here. */
4019 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4020 it_copy.current_x - 1, MOVE_TO_X);
4021 charpos = IT_STRING_CHARPOS (it_copy);
4022 RESTORE_IT (it, it, it_copy_data);
4024 else
4026 /* Set charpos to the string position of the character
4027 that comes after IT's current position in the visual
4028 order. */
4029 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4031 it_copy = *it;
4032 while (n--)
4033 bidi_move_to_visually_next (&it_copy.bidi_it);
4035 charpos = it_copy.bidi_it.charpos;
4038 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4040 if (it->current.overlay_string_index >= 0)
4041 bufpos = IT_CHARPOS (*it);
4042 else
4043 bufpos = 0;
4045 base_face_id = underlying_face_id (it);
4047 /* Get the face for ASCII, or unibyte. */
4048 face_id = face_at_string_position (it->w,
4049 it->string,
4050 charpos,
4051 bufpos,
4052 &next_check_charpos,
4053 base_face_id, false);
4055 /* Correct the face for charsets different from ASCII. Do it
4056 for the multibyte case only. The face returned above is
4057 suitable for unibyte text if IT->string is unibyte. */
4058 if (STRING_MULTIBYTE (it->string))
4060 struct text_pos pos1 = string_pos (charpos, it->string);
4061 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4062 int c, len;
4063 struct face *face = FACE_FROM_ID (it->f, face_id);
4065 c = string_char_and_length (p, &len);
4066 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4069 else
4071 struct text_pos pos;
4073 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4074 || (IT_CHARPOS (*it) <= BEGV && before_p))
4075 return it->face_id;
4077 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4078 pos = it->current.pos;
4080 if (!it->bidi_p)
4082 if (before_p)
4083 DEC_TEXT_POS (pos, it->multibyte_p);
4084 else
4086 if (it->what == IT_COMPOSITION)
4088 /* For composition, we must check the position after
4089 the composition. */
4090 pos.charpos += it->cmp_it.nchars;
4091 pos.bytepos += it->len;
4093 else
4094 INC_TEXT_POS (pos, it->multibyte_p);
4097 else
4099 if (before_p)
4101 /* With bidi iteration, the character before the current
4102 in the visual order cannot be found by simple
4103 iteration, because "reverse" reordering is not
4104 supported. Instead, we need to use the move_it_*
4105 family of functions. */
4106 /* Ignore face changes before the first visible
4107 character on this display line. */
4108 if (it->current_x <= it->first_visible_x)
4109 return it->face_id;
4110 SAVE_IT (it_copy, *it, it_copy_data);
4111 /* Implementation note: Since move_it_in_display_line
4112 works in the iterator geometry, and thinks the first
4113 character is always the leftmost, even in R2L lines,
4114 we don't need to distinguish between the R2L and L2R
4115 cases here. */
4116 move_it_in_display_line (&it_copy, ZV,
4117 it_copy.current_x - 1, MOVE_TO_X);
4118 pos = it_copy.current.pos;
4119 RESTORE_IT (it, it, it_copy_data);
4121 else
4123 /* Set charpos to the buffer position of the character
4124 that comes after IT's current position in the visual
4125 order. */
4126 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4128 it_copy = *it;
4129 while (n--)
4130 bidi_move_to_visually_next (&it_copy.bidi_it);
4132 SET_TEXT_POS (pos,
4133 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4136 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4138 /* Determine face for CHARSET_ASCII, or unibyte. */
4139 face_id = face_at_buffer_position (it->w,
4140 CHARPOS (pos),
4141 &next_check_charpos,
4142 limit, false, -1);
4144 /* Correct the face for charsets different from ASCII. Do it
4145 for the multibyte case only. The face returned above is
4146 suitable for unibyte text if current_buffer is unibyte. */
4147 if (it->multibyte_p)
4149 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4150 struct face *face = FACE_FROM_ID (it->f, face_id);
4151 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4155 return face_id;
4160 /***********************************************************************
4161 Invisible text
4162 ***********************************************************************/
4164 /* Set up iterator IT from invisible properties at its current
4165 position. Called from handle_stop. */
4167 static enum prop_handled
4168 handle_invisible_prop (struct it *it)
4170 enum prop_handled handled = HANDLED_NORMALLY;
4171 int invis;
4172 Lisp_Object prop;
4174 if (STRINGP (it->string))
4176 Lisp_Object end_charpos, limit, charpos;
4178 /* Get the value of the invisible text property at the
4179 current position. Value will be nil if there is no such
4180 property. */
4181 charpos = make_number (IT_STRING_CHARPOS (*it));
4182 prop = Fget_text_property (charpos, Qinvisible, it->string);
4183 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4185 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4187 /* Record whether we have to display an ellipsis for the
4188 invisible text. */
4189 bool display_ellipsis_p = (invis == 2);
4190 ptrdiff_t len, endpos;
4192 handled = HANDLED_RECOMPUTE_PROPS;
4194 /* Get the position at which the next visible text can be
4195 found in IT->string, if any. */
4196 endpos = len = SCHARS (it->string);
4197 XSETINT (limit, len);
4200 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4201 it->string, limit);
4202 if (INTEGERP (end_charpos))
4204 endpos = XFASTINT (end_charpos);
4205 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4206 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4207 if (invis == 2)
4208 display_ellipsis_p = true;
4211 while (invis != 0 && endpos < len);
4213 if (display_ellipsis_p)
4214 it->ellipsis_p = true;
4216 if (endpos < len)
4218 /* Text at END_CHARPOS is visible. Move IT there. */
4219 struct text_pos old;
4220 ptrdiff_t oldpos;
4222 old = it->current.string_pos;
4223 oldpos = CHARPOS (old);
4224 if (it->bidi_p)
4226 if (it->bidi_it.first_elt
4227 && it->bidi_it.charpos < SCHARS (it->string))
4228 bidi_paragraph_init (it->paragraph_embedding,
4229 &it->bidi_it, true);
4230 /* Bidi-iterate out of the invisible text. */
4233 bidi_move_to_visually_next (&it->bidi_it);
4235 while (oldpos <= it->bidi_it.charpos
4236 && it->bidi_it.charpos < endpos);
4238 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4239 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4240 if (IT_CHARPOS (*it) >= endpos)
4241 it->prev_stop = endpos;
4243 else
4245 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4246 compute_string_pos (&it->current.string_pos, old, it->string);
4249 else
4251 /* The rest of the string is invisible. If this is an
4252 overlay string, proceed with the next overlay string
4253 or whatever comes and return a character from there. */
4254 if (it->current.overlay_string_index >= 0
4255 && !display_ellipsis_p)
4257 next_overlay_string (it);
4258 /* Don't check for overlay strings when we just
4259 finished processing them. */
4260 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4262 else
4264 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4265 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4270 else
4272 ptrdiff_t newpos, next_stop, start_charpos, tem;
4273 Lisp_Object pos, overlay;
4275 /* First of all, is there invisible text at this position? */
4276 tem = start_charpos = IT_CHARPOS (*it);
4277 pos = make_number (tem);
4278 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4279 &overlay);
4280 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4282 /* If we are on invisible text, skip over it. */
4283 if (invis != 0 && start_charpos < it->end_charpos)
4285 /* Record whether we have to display an ellipsis for the
4286 invisible text. */
4287 bool display_ellipsis_p = invis == 2;
4289 handled = HANDLED_RECOMPUTE_PROPS;
4291 /* Loop skipping over invisible text. The loop is left at
4292 ZV or with IT on the first char being visible again. */
4295 /* Try to skip some invisible text. Return value is the
4296 position reached which can be equal to where we start
4297 if there is nothing invisible there. This skips both
4298 over invisible text properties and overlays with
4299 invisible property. */
4300 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4302 /* If we skipped nothing at all we weren't at invisible
4303 text in the first place. If everything to the end of
4304 the buffer was skipped, end the loop. */
4305 if (newpos == tem || newpos >= ZV)
4306 invis = 0;
4307 else
4309 /* We skipped some characters but not necessarily
4310 all there are. Check if we ended up on visible
4311 text. Fget_char_property returns the property of
4312 the char before the given position, i.e. if we
4313 get invis = 0, this means that the char at
4314 newpos is visible. */
4315 pos = make_number (newpos);
4316 prop = Fget_char_property (pos, Qinvisible, it->window);
4317 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4320 /* If we ended up on invisible text, proceed to
4321 skip starting with next_stop. */
4322 if (invis != 0)
4323 tem = next_stop;
4325 /* If there are adjacent invisible texts, don't lose the
4326 second one's ellipsis. */
4327 if (invis == 2)
4328 display_ellipsis_p = true;
4330 while (invis != 0);
4332 /* The position newpos is now either ZV or on visible text. */
4333 if (it->bidi_p)
4335 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4336 bool on_newline
4337 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4338 bool after_newline
4339 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4341 /* If the invisible text ends on a newline or on a
4342 character after a newline, we can avoid the costly,
4343 character by character, bidi iteration to NEWPOS, and
4344 instead simply reseat the iterator there. That's
4345 because all bidi reordering information is tossed at
4346 the newline. This is a big win for modes that hide
4347 complete lines, like Outline, Org, etc. */
4348 if (on_newline || after_newline)
4350 struct text_pos tpos;
4351 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4353 SET_TEXT_POS (tpos, newpos, bpos);
4354 reseat_1 (it, tpos, false);
4355 /* If we reseat on a newline/ZV, we need to prep the
4356 bidi iterator for advancing to the next character
4357 after the newline/EOB, keeping the current paragraph
4358 direction (so that PRODUCE_GLYPHS does TRT wrt
4359 prepending/appending glyphs to a glyph row). */
4360 if (on_newline)
4362 it->bidi_it.first_elt = false;
4363 it->bidi_it.paragraph_dir = pdir;
4364 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4365 it->bidi_it.nchars = 1;
4366 it->bidi_it.ch_len = 1;
4369 else /* Must use the slow method. */
4371 /* With bidi iteration, the region of invisible text
4372 could start and/or end in the middle of a
4373 non-base embedding level. Therefore, we need to
4374 skip invisible text using the bidi iterator,
4375 starting at IT's current position, until we find
4376 ourselves outside of the invisible text.
4377 Skipping invisible text _after_ bidi iteration
4378 avoids affecting the visual order of the
4379 displayed text when invisible properties are
4380 added or removed. */
4381 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4383 /* If we were `reseat'ed to a new paragraph,
4384 determine the paragraph base direction. We
4385 need to do it now because
4386 next_element_from_buffer may not have a
4387 chance to do it, if we are going to skip any
4388 text at the beginning, which resets the
4389 FIRST_ELT flag. */
4390 bidi_paragraph_init (it->paragraph_embedding,
4391 &it->bidi_it, true);
4395 bidi_move_to_visually_next (&it->bidi_it);
4397 while (it->stop_charpos <= it->bidi_it.charpos
4398 && it->bidi_it.charpos < newpos);
4399 IT_CHARPOS (*it) = it->bidi_it.charpos;
4400 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4401 /* If we overstepped NEWPOS, record its position in
4402 the iterator, so that we skip invisible text if
4403 later the bidi iteration lands us in the
4404 invisible region again. */
4405 if (IT_CHARPOS (*it) >= newpos)
4406 it->prev_stop = newpos;
4409 else
4411 IT_CHARPOS (*it) = newpos;
4412 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4415 if (display_ellipsis_p)
4417 /* Make sure that the glyphs of the ellipsis will get
4418 correct `charpos' values. If we would not update
4419 it->position here, the glyphs would belong to the
4420 last visible character _before_ the invisible
4421 text, which confuses `set_cursor_from_row'.
4423 We use the last invisible position instead of the
4424 first because this way the cursor is always drawn on
4425 the first "." of the ellipsis, whenever PT is inside
4426 the invisible text. Otherwise the cursor would be
4427 placed _after_ the ellipsis when the point is after the
4428 first invisible character. */
4429 if (!STRINGP (it->object))
4431 it->position.charpos = newpos - 1;
4432 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4436 /* If there are before-strings at the start of invisible
4437 text, and the text is invisible because of a text
4438 property, arrange to show before-strings because 20.x did
4439 it that way. (If the text is invisible because of an
4440 overlay property instead of a text property, this is
4441 already handled in the overlay code.) */
4442 if (NILP (overlay)
4443 && get_overlay_strings (it, it->stop_charpos))
4445 handled = HANDLED_RECOMPUTE_PROPS;
4446 if (it->sp > 0)
4448 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4449 /* The call to get_overlay_strings above recomputes
4450 it->stop_charpos, but it only considers changes
4451 in properties and overlays beyond iterator's
4452 current position. This causes us to miss changes
4453 that happen exactly where the invisible property
4454 ended. So we play it safe here and force the
4455 iterator to check for potential stop positions
4456 immediately after the invisible text. Note that
4457 if get_overlay_strings returns true, it
4458 normally also pushed the iterator stack, so we
4459 need to update the stop position in the slot
4460 below the current one. */
4461 it->stack[it->sp - 1].stop_charpos
4462 = CHARPOS (it->stack[it->sp - 1].current.pos);
4465 else if (display_ellipsis_p)
4467 it->ellipsis_p = true;
4468 /* Let the ellipsis display before
4469 considering any properties of the following char.
4470 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4471 handled = HANDLED_RETURN;
4476 return handled;
4480 /* Make iterator IT return `...' next.
4481 Replaces LEN characters from buffer. */
4483 static void
4484 setup_for_ellipsis (struct it *it, int len)
4486 /* Use the display table definition for `...'. Invalid glyphs
4487 will be handled by the method returning elements from dpvec. */
4488 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4490 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4491 it->dpvec = v->contents;
4492 it->dpend = v->contents + v->header.size;
4494 else
4496 /* Default `...'. */
4497 it->dpvec = default_invis_vector;
4498 it->dpend = default_invis_vector + 3;
4501 it->dpvec_char_len = len;
4502 it->current.dpvec_index = 0;
4503 it->dpvec_face_id = -1;
4505 /* Remember the current face id in case glyphs specify faces.
4506 IT's face is restored in set_iterator_to_next.
4507 saved_face_id was set to preceding char's face in handle_stop. */
4508 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4509 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4511 /* If the ellipsis represents buffer text, it means we advanced in
4512 the buffer, so we should no longer ignore overlay strings. */
4513 if (it->method == GET_FROM_BUFFER)
4514 it->ignore_overlay_strings_at_pos_p = false;
4516 it->method = GET_FROM_DISPLAY_VECTOR;
4517 it->ellipsis_p = true;
4522 /***********************************************************************
4523 'display' property
4524 ***********************************************************************/
4526 /* Set up iterator IT from `display' property at its current position.
4527 Called from handle_stop.
4528 We return HANDLED_RETURN if some part of the display property
4529 overrides the display of the buffer text itself.
4530 Otherwise we return HANDLED_NORMALLY. */
4532 static enum prop_handled
4533 handle_display_prop (struct it *it)
4535 Lisp_Object propval, object, overlay;
4536 struct text_pos *position;
4537 ptrdiff_t bufpos;
4538 /* Nonzero if some property replaces the display of the text itself. */
4539 int display_replaced = 0;
4541 if (STRINGP (it->string))
4543 object = it->string;
4544 position = &it->current.string_pos;
4545 bufpos = CHARPOS (it->current.pos);
4547 else
4549 XSETWINDOW (object, it->w);
4550 position = &it->current.pos;
4551 bufpos = CHARPOS (*position);
4554 /* Reset those iterator values set from display property values. */
4555 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4556 it->space_width = Qnil;
4557 it->font_height = Qnil;
4558 it->voffset = 0;
4560 /* We don't support recursive `display' properties, i.e. string
4561 values that have a string `display' property, that have a string
4562 `display' property etc. */
4563 if (!it->string_from_display_prop_p)
4564 it->area = TEXT_AREA;
4566 propval = get_char_property_and_overlay (make_number (position->charpos),
4567 Qdisplay, object, &overlay);
4568 if (NILP (propval))
4569 return HANDLED_NORMALLY;
4570 /* Now OVERLAY is the overlay that gave us this property, or nil
4571 if it was a text property. */
4573 if (!STRINGP (it->string))
4574 object = it->w->contents;
4576 display_replaced = handle_display_spec (it, propval, object, overlay,
4577 position, bufpos,
4578 FRAME_WINDOW_P (it->f));
4579 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4582 /* Subroutine of handle_display_prop. Returns non-zero if the display
4583 specification in SPEC is a replacing specification, i.e. it would
4584 replace the text covered by `display' property with something else,
4585 such as an image or a display string. If SPEC includes any kind or
4586 `(space ...) specification, the value is 2; this is used by
4587 compute_display_string_pos, which see.
4589 See handle_single_display_spec for documentation of arguments.
4590 FRAME_WINDOW_P is true if the window being redisplayed is on a
4591 GUI frame; this argument is used only if IT is NULL, see below.
4593 IT can be NULL, if this is called by the bidi reordering code
4594 through compute_display_string_pos, which see. In that case, this
4595 function only examines SPEC, but does not otherwise "handle" it, in
4596 the sense that it doesn't set up members of IT from the display
4597 spec. */
4598 static int
4599 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4600 Lisp_Object overlay, struct text_pos *position,
4601 ptrdiff_t bufpos, bool frame_window_p)
4603 int replacing = 0;
4605 if (CONSP (spec)
4606 /* Simple specifications. */
4607 && !EQ (XCAR (spec), Qimage)
4608 && !EQ (XCAR (spec), Qspace)
4609 && !EQ (XCAR (spec), Qwhen)
4610 && !EQ (XCAR (spec), Qslice)
4611 && !EQ (XCAR (spec), Qspace_width)
4612 && !EQ (XCAR (spec), Qheight)
4613 && !EQ (XCAR (spec), Qraise)
4614 /* Marginal area specifications. */
4615 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4616 && !EQ (XCAR (spec), Qleft_fringe)
4617 && !EQ (XCAR (spec), Qright_fringe)
4618 && !NILP (XCAR (spec)))
4620 for (; CONSP (spec); spec = XCDR (spec))
4622 int rv = handle_single_display_spec (it, XCAR (spec), object,
4623 overlay, position, bufpos,
4624 replacing, frame_window_p);
4625 if (rv != 0)
4627 replacing = rv;
4628 /* If some text in a string is replaced, `position' no
4629 longer points to the position of `object'. */
4630 if (!it || STRINGP (object))
4631 break;
4635 else if (VECTORP (spec))
4637 ptrdiff_t i;
4638 for (i = 0; i < ASIZE (spec); ++i)
4640 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4641 overlay, position, bufpos,
4642 replacing, frame_window_p);
4643 if (rv != 0)
4645 replacing = rv;
4646 /* If some text in a string is replaced, `position' no
4647 longer points to the position of `object'. */
4648 if (!it || STRINGP (object))
4649 break;
4653 else
4654 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4655 bufpos, 0, frame_window_p);
4656 return replacing;
4659 /* Value is the position of the end of the `display' property starting
4660 at START_POS in OBJECT. */
4662 static struct text_pos
4663 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4665 Lisp_Object end;
4666 struct text_pos end_pos;
4668 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4669 Qdisplay, object, Qnil);
4670 CHARPOS (end_pos) = XFASTINT (end);
4671 if (STRINGP (object))
4672 compute_string_pos (&end_pos, start_pos, it->string);
4673 else
4674 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4676 return end_pos;
4680 /* Set up IT from a single `display' property specification SPEC. OBJECT
4681 is the object in which the `display' property was found. *POSITION
4682 is the position in OBJECT at which the `display' property was found.
4683 BUFPOS is the buffer position of OBJECT (different from POSITION if
4684 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4685 previously saw a display specification which already replaced text
4686 display with something else, for example an image; we ignore such
4687 properties after the first one has been processed.
4689 OVERLAY is the overlay this `display' property came from,
4690 or nil if it was a text property.
4692 If SPEC is a `space' or `image' specification, and in some other
4693 cases too, set *POSITION to the position where the `display'
4694 property ends.
4696 If IT is NULL, only examine the property specification in SPEC, but
4697 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4698 is intended to be displayed in a window on a GUI frame.
4700 Value is non-zero if something was found which replaces the display
4701 of buffer or string text. */
4703 static int
4704 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4705 Lisp_Object overlay, struct text_pos *position,
4706 ptrdiff_t bufpos, int display_replaced,
4707 bool frame_window_p)
4709 Lisp_Object form;
4710 Lisp_Object location, value;
4711 struct text_pos start_pos = *position;
4713 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4714 If the result is non-nil, use VALUE instead of SPEC. */
4715 form = Qt;
4716 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4718 spec = XCDR (spec);
4719 if (!CONSP (spec))
4720 return 0;
4721 form = XCAR (spec);
4722 spec = XCDR (spec);
4725 if (!NILP (form) && !EQ (form, Qt))
4727 ptrdiff_t count = SPECPDL_INDEX ();
4728 struct gcpro gcpro1;
4730 /* Bind `object' to the object having the `display' property, a
4731 buffer or string. Bind `position' to the position in the
4732 object where the property was found, and `buffer-position'
4733 to the current position in the buffer. */
4735 if (NILP (object))
4736 XSETBUFFER (object, current_buffer);
4737 specbind (Qobject, object);
4738 specbind (Qposition, make_number (CHARPOS (*position)));
4739 specbind (Qbuffer_position, make_number (bufpos));
4740 GCPRO1 (form);
4741 form = safe_eval (form);
4742 UNGCPRO;
4743 unbind_to (count, Qnil);
4746 if (NILP (form))
4747 return 0;
4749 /* Handle `(height HEIGHT)' specifications. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qheight)
4752 && CONSP (XCDR (spec)))
4754 if (it)
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4759 it->font_height = XCAR (XCDR (spec));
4760 if (!NILP (it->font_height))
4762 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4763 int new_height = -1;
4765 if (CONSP (it->font_height)
4766 && (EQ (XCAR (it->font_height), Qplus)
4767 || EQ (XCAR (it->font_height), Qminus))
4768 && CONSP (XCDR (it->font_height))
4769 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4771 /* `(+ N)' or `(- N)' where N is an integer. */
4772 int steps = XINT (XCAR (XCDR (it->font_height)));
4773 if (EQ (XCAR (it->font_height), Qplus))
4774 steps = - steps;
4775 it->face_id = smaller_face (it->f, it->face_id, steps);
4777 else if (FUNCTIONP (it->font_height))
4779 /* Call function with current height as argument.
4780 Value is the new height. */
4781 Lisp_Object height;
4782 height = safe_call1 (it->font_height,
4783 face->lface[LFACE_HEIGHT_INDEX]);
4784 if (NUMBERP (height))
4785 new_height = XFLOATINT (height);
4787 else if (NUMBERP (it->font_height))
4789 /* Value is a multiple of the canonical char height. */
4790 struct face *f;
4792 f = FACE_FROM_ID (it->f,
4793 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4794 new_height = (XFLOATINT (it->font_height)
4795 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4797 else
4799 /* Evaluate IT->font_height with `height' bound to the
4800 current specified height to get the new height. */
4801 ptrdiff_t count = SPECPDL_INDEX ();
4803 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4804 value = safe_eval (it->font_height);
4805 unbind_to (count, Qnil);
4807 if (NUMBERP (value))
4808 new_height = XFLOATINT (value);
4811 if (new_height > 0)
4812 it->face_id = face_with_height (it->f, it->face_id, new_height);
4816 return 0;
4819 /* Handle `(space-width WIDTH)'. */
4820 if (CONSP (spec)
4821 && EQ (XCAR (spec), Qspace_width)
4822 && CONSP (XCDR (spec)))
4824 if (it)
4826 if (!FRAME_WINDOW_P (it->f))
4827 return 0;
4829 value = XCAR (XCDR (spec));
4830 if (NUMBERP (value) && XFLOATINT (value) > 0)
4831 it->space_width = value;
4834 return 0;
4837 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4838 if (CONSP (spec)
4839 && EQ (XCAR (spec), Qslice))
4841 Lisp_Object tem;
4843 if (it)
4845 if (!FRAME_WINDOW_P (it->f))
4846 return 0;
4848 if (tem = XCDR (spec), CONSP (tem))
4850 it->slice.x = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4853 it->slice.y = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4856 it->slice.width = XCAR (tem);
4857 if (tem = XCDR (tem), CONSP (tem))
4858 it->slice.height = XCAR (tem);
4864 return 0;
4867 /* Handle `(raise FACTOR)'. */
4868 if (CONSP (spec)
4869 && EQ (XCAR (spec), Qraise)
4870 && CONSP (XCDR (spec)))
4872 if (it)
4874 if (!FRAME_WINDOW_P (it->f))
4875 return 0;
4877 #ifdef HAVE_WINDOW_SYSTEM
4878 value = XCAR (XCDR (spec));
4879 if (NUMBERP (value))
4881 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4882 it->voffset = - (XFLOATINT (value)
4883 * (FONT_HEIGHT (face->font)));
4885 #endif /* HAVE_WINDOW_SYSTEM */
4888 return 0;
4891 /* Don't handle the other kinds of display specifications
4892 inside a string that we got from a `display' property. */
4893 if (it && it->string_from_display_prop_p)
4894 return 0;
4896 /* Characters having this form of property are not displayed, so
4897 we have to find the end of the property. */
4898 if (it)
4900 start_pos = *position;
4901 *position = display_prop_end (it, object, start_pos);
4903 value = Qnil;
4905 /* Stop the scan at that end position--we assume that all
4906 text properties change there. */
4907 if (it)
4908 it->stop_charpos = position->charpos;
4910 /* Handle `(left-fringe BITMAP [FACE])'
4911 and `(right-fringe BITMAP [FACE])'. */
4912 if (CONSP (spec)
4913 && (EQ (XCAR (spec), Qleft_fringe)
4914 || EQ (XCAR (spec), Qright_fringe))
4915 && CONSP (XCDR (spec)))
4917 int fringe_bitmap;
4919 if (it)
4921 if (!FRAME_WINDOW_P (it->f))
4922 /* If we return here, POSITION has been advanced
4923 across the text with this property. */
4925 /* Synchronize the bidi iterator with POSITION. This is
4926 needed because we are not going to push the iterator
4927 on behalf of this display property, so there will be
4928 no pop_it call to do this synchronization for us. */
4929 if (it->bidi_p)
4931 it->position = *position;
4932 iterate_out_of_display_property (it);
4933 *position = it->position;
4935 return 1;
4938 else if (!frame_window_p)
4939 return 1;
4941 #ifdef HAVE_WINDOW_SYSTEM
4942 value = XCAR (XCDR (spec));
4943 if (!SYMBOLP (value)
4944 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4948 if (it && it->bidi_p)
4950 it->position = *position;
4951 iterate_out_of_display_property (it);
4952 *position = it->position;
4954 return 1;
4957 if (it)
4959 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4961 if (CONSP (XCDR (XCDR (spec))))
4963 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4964 int face_id2 = lookup_derived_face (it->f, face_name,
4965 FRINGE_FACE_ID, false);
4966 if (face_id2 >= 0)
4967 face_id = face_id2;
4970 /* Save current settings of IT so that we can restore them
4971 when we are finished with the glyph property value. */
4972 push_it (it, position);
4974 it->area = TEXT_AREA;
4975 it->what = IT_IMAGE;
4976 it->image_id = -1; /* no image */
4977 it->position = start_pos;
4978 it->object = NILP (object) ? it->w->contents : object;
4979 it->method = GET_FROM_IMAGE;
4980 it->from_overlay = Qnil;
4981 it->face_id = face_id;
4982 it->from_disp_prop_p = true;
4984 /* Say that we haven't consumed the characters with
4985 `display' property yet. The call to pop_it in
4986 set_iterator_to_next will clean this up. */
4987 *position = start_pos;
4989 if (EQ (XCAR (spec), Qleft_fringe))
4991 it->left_user_fringe_bitmap = fringe_bitmap;
4992 it->left_user_fringe_face_id = face_id;
4994 else
4996 it->right_user_fringe_bitmap = fringe_bitmap;
4997 it->right_user_fringe_face_id = face_id;
5000 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 1;
5004 /* Prepare to handle `((margin left-margin) ...)',
5005 `((margin right-margin) ...)' and `((margin nil) ...)'
5006 prefixes for display specifications. */
5007 location = Qunbound;
5008 if (CONSP (spec) && CONSP (XCAR (spec)))
5010 Lisp_Object tem;
5012 value = XCDR (spec);
5013 if (CONSP (value))
5014 value = XCAR (value);
5016 tem = XCAR (spec);
5017 if (EQ (XCAR (tem), Qmargin)
5018 && (tem = XCDR (tem),
5019 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5020 (NILP (tem)
5021 || EQ (tem, Qleft_margin)
5022 || EQ (tem, Qright_margin))))
5023 location = tem;
5026 if (EQ (location, Qunbound))
5028 location = Qnil;
5029 value = spec;
5032 /* After this point, VALUE is the property after any
5033 margin prefix has been stripped. It must be a string,
5034 an image specification, or `(space ...)'.
5036 LOCATION specifies where to display: `left-margin',
5037 `right-margin' or nil. */
5039 bool valid_p = (STRINGP (value)
5040 #ifdef HAVE_WINDOW_SYSTEM
5041 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5042 && valid_image_p (value))
5043 #endif /* not HAVE_WINDOW_SYSTEM */
5044 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5046 if (valid_p && display_replaced == 0)
5048 int retval = 1;
5050 if (!it)
5052 /* Callers need to know whether the display spec is any kind
5053 of `(space ...)' spec that is about to affect text-area
5054 display. */
5055 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5056 retval = 2;
5057 return retval;
5060 /* Save current settings of IT so that we can restore them
5061 when we are finished with the glyph property value. */
5062 push_it (it, position);
5063 it->from_overlay = overlay;
5064 it->from_disp_prop_p = true;
5066 if (NILP (location))
5067 it->area = TEXT_AREA;
5068 else if (EQ (location, Qleft_margin))
5069 it->area = LEFT_MARGIN_AREA;
5070 else
5071 it->area = RIGHT_MARGIN_AREA;
5073 if (STRINGP (value))
5075 it->string = value;
5076 it->multibyte_p = STRING_MULTIBYTE (it->string);
5077 it->current.overlay_string_index = -1;
5078 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5079 it->end_charpos = it->string_nchars = SCHARS (it->string);
5080 it->method = GET_FROM_STRING;
5081 it->stop_charpos = 0;
5082 it->prev_stop = 0;
5083 it->base_level_stop = 0;
5084 it->string_from_display_prop_p = true;
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 if (BUFFERP (object))
5089 *position = start_pos;
5091 /* Force paragraph direction to be that of the parent
5092 object. If the parent object's paragraph direction is
5093 not yet determined, default to L2R. */
5094 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5095 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5096 else
5097 it->paragraph_embedding = L2R;
5099 /* Set up the bidi iterator for this display string. */
5100 if (it->bidi_p)
5102 it->bidi_it.string.lstring = it->string;
5103 it->bidi_it.string.s = NULL;
5104 it->bidi_it.string.schars = it->end_charpos;
5105 it->bidi_it.string.bufpos = bufpos;
5106 it->bidi_it.string.from_disp_str = true;
5107 it->bidi_it.string.unibyte = !it->multibyte_p;
5108 it->bidi_it.w = it->w;
5109 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5112 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5114 it->method = GET_FROM_STRETCH;
5115 it->object = value;
5116 *position = it->position = start_pos;
5117 retval = 1 + (it->area == TEXT_AREA);
5119 #ifdef HAVE_WINDOW_SYSTEM
5120 else
5122 it->what = IT_IMAGE;
5123 it->image_id = lookup_image (it->f, value);
5124 it->position = start_pos;
5125 it->object = NILP (object) ? it->w->contents : object;
5126 it->method = GET_FROM_IMAGE;
5128 /* Say that we haven't consumed the characters with
5129 `display' property yet. The call to pop_it in
5130 set_iterator_to_next will clean this up. */
5131 *position = start_pos;
5133 #endif /* HAVE_WINDOW_SYSTEM */
5135 return retval;
5138 /* Invalid property or property not supported. Restore
5139 POSITION to what it was before. */
5140 *position = start_pos;
5141 return 0;
5144 /* Check if PROP is a display property value whose text should be
5145 treated as intangible. OVERLAY is the overlay from which PROP
5146 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5147 specify the buffer position covered by PROP. */
5149 bool
5150 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5151 ptrdiff_t charpos, ptrdiff_t bytepos)
5153 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5154 struct text_pos position;
5156 SET_TEXT_POS (position, charpos, bytepos);
5157 return (handle_display_spec (NULL, prop, Qnil, overlay,
5158 &position, charpos, frame_window_p)
5159 != 0);
5163 /* Return true if PROP is a display sub-property value containing STRING.
5165 Implementation note: this and the following function are really
5166 special cases of handle_display_spec and
5167 handle_single_display_spec, and should ideally use the same code.
5168 Until they do, these two pairs must be consistent and must be
5169 modified in sync. */
5171 static bool
5172 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5174 if (EQ (string, prop))
5175 return true;
5177 /* Skip over `when FORM'. */
5178 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5180 prop = XCDR (prop);
5181 if (!CONSP (prop))
5182 return false;
5183 /* Actually, the condition following `when' should be eval'ed,
5184 like handle_single_display_spec does, and we should return
5185 false if it evaluates to nil. However, this function is
5186 called only when the buffer was already displayed and some
5187 glyph in the glyph matrix was found to come from a display
5188 string. Therefore, the condition was already evaluated, and
5189 the result was non-nil, otherwise the display string wouldn't
5190 have been displayed and we would have never been called for
5191 this property. Thus, we can skip the evaluation and assume
5192 its result is non-nil. */
5193 prop = XCDR (prop);
5196 if (CONSP (prop))
5197 /* Skip over `margin LOCATION'. */
5198 if (EQ (XCAR (prop), Qmargin))
5200 prop = XCDR (prop);
5201 if (!CONSP (prop))
5202 return false;
5204 prop = XCDR (prop);
5205 if (!CONSP (prop))
5206 return false;
5209 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5213 /* Return true if STRING appears in the `display' property PROP. */
5215 static bool
5216 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5218 if (CONSP (prop)
5219 && !EQ (XCAR (prop), Qwhen)
5220 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5222 /* A list of sub-properties. */
5223 while (CONSP (prop))
5225 if (single_display_spec_string_p (XCAR (prop), string))
5226 return true;
5227 prop = XCDR (prop);
5230 else if (VECTORP (prop))
5232 /* A vector of sub-properties. */
5233 ptrdiff_t i;
5234 for (i = 0; i < ASIZE (prop); ++i)
5235 if (single_display_spec_string_p (AREF (prop, i), string))
5236 return true;
5238 else
5239 return single_display_spec_string_p (prop, string);
5241 return false;
5244 /* Look for STRING in overlays and text properties in the current
5245 buffer, between character positions FROM and TO (excluding TO).
5246 BACK_P means look back (in this case, TO is supposed to be
5247 less than FROM).
5248 Value is the first character position where STRING was found, or
5249 zero if it wasn't found before hitting TO.
5251 This function may only use code that doesn't eval because it is
5252 called asynchronously from note_mouse_highlight. */
5254 static ptrdiff_t
5255 string_buffer_position_lim (Lisp_Object string,
5256 ptrdiff_t from, ptrdiff_t to, bool back_p)
5258 Lisp_Object limit, prop, pos;
5259 bool found = false;
5261 pos = make_number (max (from, BEGV));
5263 if (!back_p) /* looking forward */
5265 limit = make_number (min (to, ZV));
5266 while (!found && !EQ (pos, limit))
5268 prop = Fget_char_property (pos, Qdisplay, Qnil);
5269 if (!NILP (prop) && display_prop_string_p (prop, string))
5270 found = true;
5271 else
5272 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5273 limit);
5276 else /* looking back */
5278 limit = make_number (max (to, BEGV));
5279 while (!found && !EQ (pos, limit))
5281 prop = Fget_char_property (pos, Qdisplay, Qnil);
5282 if (!NILP (prop) && display_prop_string_p (prop, string))
5283 found = true;
5284 else
5285 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5286 limit);
5290 return found ? XINT (pos) : 0;
5293 /* Determine which buffer position in current buffer STRING comes from.
5294 AROUND_CHARPOS is an approximate position where it could come from.
5295 Value is the buffer position or 0 if it couldn't be determined.
5297 This function is necessary because we don't record buffer positions
5298 in glyphs generated from strings (to keep struct glyph small).
5299 This function may only use code that doesn't eval because it is
5300 called asynchronously from note_mouse_highlight. */
5302 static ptrdiff_t
5303 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5305 const int MAX_DISTANCE = 1000;
5306 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5307 around_charpos + MAX_DISTANCE,
5308 false);
5310 if (!found)
5311 found = string_buffer_position_lim (string, around_charpos,
5312 around_charpos - MAX_DISTANCE, true);
5313 return found;
5318 /***********************************************************************
5319 `composition' property
5320 ***********************************************************************/
5322 /* Set up iterator IT from `composition' property at its current
5323 position. Called from handle_stop. */
5325 static enum prop_handled
5326 handle_composition_prop (struct it *it)
5328 Lisp_Object prop, string;
5329 ptrdiff_t pos, pos_byte, start, end;
5331 if (STRINGP (it->string))
5333 unsigned char *s;
5335 pos = IT_STRING_CHARPOS (*it);
5336 pos_byte = IT_STRING_BYTEPOS (*it);
5337 string = it->string;
5338 s = SDATA (string) + pos_byte;
5339 it->c = STRING_CHAR (s);
5341 else
5343 pos = IT_CHARPOS (*it);
5344 pos_byte = IT_BYTEPOS (*it);
5345 string = Qnil;
5346 it->c = FETCH_CHAR (pos_byte);
5349 /* If there's a valid composition and point is not inside of the
5350 composition (in the case that the composition is from the current
5351 buffer), draw a glyph composed from the composition components. */
5352 if (find_composition (pos, -1, &start, &end, &prop, string)
5353 && composition_valid_p (start, end, prop)
5354 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5356 if (start < pos)
5357 /* As we can't handle this situation (perhaps font-lock added
5358 a new composition), we just return here hoping that next
5359 redisplay will detect this composition much earlier. */
5360 return HANDLED_NORMALLY;
5361 if (start != pos)
5363 if (STRINGP (it->string))
5364 pos_byte = string_char_to_byte (it->string, start);
5365 else
5366 pos_byte = CHAR_TO_BYTE (start);
5368 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5369 prop, string);
5371 if (it->cmp_it.id >= 0)
5373 it->cmp_it.ch = -1;
5374 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5375 it->cmp_it.nglyphs = -1;
5379 return HANDLED_NORMALLY;
5384 /***********************************************************************
5385 Overlay strings
5386 ***********************************************************************/
5388 /* The following structure is used to record overlay strings for
5389 later sorting in load_overlay_strings. */
5391 struct overlay_entry
5393 Lisp_Object overlay;
5394 Lisp_Object string;
5395 EMACS_INT priority;
5396 bool after_string_p;
5400 /* Set up iterator IT from overlay strings at its current position.
5401 Called from handle_stop. */
5403 static enum prop_handled
5404 handle_overlay_change (struct it *it)
5406 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5407 return HANDLED_RECOMPUTE_PROPS;
5408 else
5409 return HANDLED_NORMALLY;
5413 /* Set up the next overlay string for delivery by IT, if there is an
5414 overlay string to deliver. Called by set_iterator_to_next when the
5415 end of the current overlay string is reached. If there are more
5416 overlay strings to display, IT->string and
5417 IT->current.overlay_string_index are set appropriately here.
5418 Otherwise IT->string is set to nil. */
5420 static void
5421 next_overlay_string (struct it *it)
5423 ++it->current.overlay_string_index;
5424 if (it->current.overlay_string_index == it->n_overlay_strings)
5426 /* No more overlay strings. Restore IT's settings to what
5427 they were before overlay strings were processed, and
5428 continue to deliver from current_buffer. */
5430 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5431 pop_it (it);
5432 eassert (it->sp > 0
5433 || (NILP (it->string)
5434 && it->method == GET_FROM_BUFFER
5435 && it->stop_charpos >= BEGV
5436 && it->stop_charpos <= it->end_charpos));
5437 it->current.overlay_string_index = -1;
5438 it->n_overlay_strings = 0;
5439 /* If there's an empty display string on the stack, pop the
5440 stack, to resync the bidi iterator with IT's position. Such
5441 empty strings are pushed onto the stack in
5442 get_overlay_strings_1. */
5443 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5444 pop_it (it);
5446 /* Since we've exhausted overlay strings at this buffer
5447 position, set the flag to ignore overlays until we move to
5448 another position. The flag is reset in
5449 next_element_from_buffer. */
5450 it->ignore_overlay_strings_at_pos_p = true;
5452 /* If we're at the end of the buffer, record that we have
5453 processed the overlay strings there already, so that
5454 next_element_from_buffer doesn't try it again. */
5455 if (NILP (it->string)
5456 && IT_CHARPOS (*it) >= it->end_charpos
5457 && it->overlay_strings_charpos >= it->end_charpos)
5458 it->overlay_strings_at_end_processed_p = true;
5459 /* Note: we reset overlay_strings_charpos only here, to make
5460 sure the just-processed overlays were indeed at EOB.
5461 Otherwise, overlays on text with invisible text property,
5462 which are processed with IT's position past the invisible
5463 text, might fool us into thinking the overlays at EOB were
5464 already processed (linum-mode can cause this, for
5465 example). */
5466 it->overlay_strings_charpos = -1;
5468 else
5470 /* There are more overlay strings to process. If
5471 IT->current.overlay_string_index has advanced to a position
5472 where we must load IT->overlay_strings with more strings, do
5473 it. We must load at the IT->overlay_strings_charpos where
5474 IT->n_overlay_strings was originally computed; when invisible
5475 text is present, this might not be IT_CHARPOS (Bug#7016). */
5476 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5478 if (it->current.overlay_string_index && i == 0)
5479 load_overlay_strings (it, it->overlay_strings_charpos);
5481 /* Initialize IT to deliver display elements from the overlay
5482 string. */
5483 it->string = it->overlay_strings[i];
5484 it->multibyte_p = STRING_MULTIBYTE (it->string);
5485 SET_TEXT_POS (it->current.string_pos, 0, 0);
5486 it->method = GET_FROM_STRING;
5487 it->stop_charpos = 0;
5488 it->end_charpos = SCHARS (it->string);
5489 if (it->cmp_it.stop_pos >= 0)
5490 it->cmp_it.stop_pos = 0;
5491 it->prev_stop = 0;
5492 it->base_level_stop = 0;
5494 /* Set up the bidi iterator for this overlay string. */
5495 if (it->bidi_p)
5497 it->bidi_it.string.lstring = it->string;
5498 it->bidi_it.string.s = NULL;
5499 it->bidi_it.string.schars = SCHARS (it->string);
5500 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5501 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5502 it->bidi_it.string.unibyte = !it->multibyte_p;
5503 it->bidi_it.w = it->w;
5504 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5508 CHECK_IT (it);
5512 /* Compare two overlay_entry structures E1 and E2. Used as a
5513 comparison function for qsort in load_overlay_strings. Overlay
5514 strings for the same position are sorted so that
5516 1. All after-strings come in front of before-strings, except
5517 when they come from the same overlay.
5519 2. Within after-strings, strings are sorted so that overlay strings
5520 from overlays with higher priorities come first.
5522 2. Within before-strings, strings are sorted so that overlay
5523 strings from overlays with higher priorities come last.
5525 Value is analogous to strcmp. */
5528 static int
5529 compare_overlay_entries (const void *e1, const void *e2)
5531 struct overlay_entry const *entry1 = e1;
5532 struct overlay_entry const *entry2 = e2;
5533 int result;
5535 if (entry1->after_string_p != entry2->after_string_p)
5537 /* Let after-strings appear in front of before-strings if
5538 they come from different overlays. */
5539 if (EQ (entry1->overlay, entry2->overlay))
5540 result = entry1->after_string_p ? 1 : -1;
5541 else
5542 result = entry1->after_string_p ? -1 : 1;
5544 else if (entry1->priority != entry2->priority)
5546 if (entry1->after_string_p)
5547 /* After-strings sorted in order of decreasing priority. */
5548 result = entry2->priority < entry1->priority ? -1 : 1;
5549 else
5550 /* Before-strings sorted in order of increasing priority. */
5551 result = entry1->priority < entry2->priority ? -1 : 1;
5553 else
5554 result = 0;
5556 return result;
5560 /* Load the vector IT->overlay_strings with overlay strings from IT's
5561 current buffer position, or from CHARPOS if that is > 0. Set
5562 IT->n_overlays to the total number of overlay strings found.
5564 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5565 a time. On entry into load_overlay_strings,
5566 IT->current.overlay_string_index gives the number of overlay
5567 strings that have already been loaded by previous calls to this
5568 function.
5570 IT->add_overlay_start contains an additional overlay start
5571 position to consider for taking overlay strings from, if non-zero.
5572 This position comes into play when the overlay has an `invisible'
5573 property, and both before and after-strings. When we've skipped to
5574 the end of the overlay, because of its `invisible' property, we
5575 nevertheless want its before-string to appear.
5576 IT->add_overlay_start will contain the overlay start position
5577 in this case.
5579 Overlay strings are sorted so that after-string strings come in
5580 front of before-string strings. Within before and after-strings,
5581 strings are sorted by overlay priority. See also function
5582 compare_overlay_entries. */
5584 static void
5585 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5587 Lisp_Object overlay, window, str, invisible;
5588 struct Lisp_Overlay *ov;
5589 ptrdiff_t start, end;
5590 ptrdiff_t n = 0, i, j;
5591 int invis;
5592 struct overlay_entry entriesbuf[20];
5593 ptrdiff_t size = ARRAYELTS (entriesbuf);
5594 struct overlay_entry *entries = entriesbuf;
5595 USE_SAFE_ALLOCA;
5597 if (charpos <= 0)
5598 charpos = IT_CHARPOS (*it);
5600 /* Append the overlay string STRING of overlay OVERLAY to vector
5601 `entries' which has size `size' and currently contains `n'
5602 elements. AFTER_P means STRING is an after-string of
5603 OVERLAY. */
5604 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5605 do \
5607 Lisp_Object priority; \
5609 if (n == size) \
5611 struct overlay_entry *old = entries; \
5612 SAFE_NALLOCA (entries, 2, size); \
5613 memcpy (entries, old, size * sizeof *entries); \
5614 size *= 2; \
5617 entries[n].string = (STRING); \
5618 entries[n].overlay = (OVERLAY); \
5619 priority = Foverlay_get ((OVERLAY), Qpriority); \
5620 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5621 entries[n].after_string_p = (AFTER_P); \
5622 ++n; \
5624 while (false)
5626 /* Process overlay before the overlay center. */
5627 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5629 XSETMISC (overlay, ov);
5630 eassert (OVERLAYP (overlay));
5631 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5632 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5634 if (end < charpos)
5635 break;
5637 /* Skip this overlay if it doesn't start or end at IT's current
5638 position. */
5639 if (end != charpos && start != charpos)
5640 continue;
5642 /* Skip this overlay if it doesn't apply to IT->w. */
5643 window = Foverlay_get (overlay, Qwindow);
5644 if (WINDOWP (window) && XWINDOW (window) != it->w)
5645 continue;
5647 /* If the text ``under'' the overlay is invisible, both before-
5648 and after-strings from this overlay are visible; start and
5649 end position are indistinguishable. */
5650 invisible = Foverlay_get (overlay, Qinvisible);
5651 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5653 /* If overlay has a non-empty before-string, record it. */
5654 if ((start == charpos || (end == charpos && invis != 0))
5655 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5656 && SCHARS (str))
5657 RECORD_OVERLAY_STRING (overlay, str, false);
5659 /* If overlay has a non-empty after-string, record it. */
5660 if ((end == charpos || (start == charpos && invis != 0))
5661 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5662 && SCHARS (str))
5663 RECORD_OVERLAY_STRING (overlay, str, true);
5666 /* Process overlays after the overlay center. */
5667 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5669 XSETMISC (overlay, ov);
5670 eassert (OVERLAYP (overlay));
5671 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5672 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5674 if (start > charpos)
5675 break;
5677 /* Skip this overlay if it doesn't start or end at IT's current
5678 position. */
5679 if (end != charpos && start != charpos)
5680 continue;
5682 /* Skip this overlay if it doesn't apply to IT->w. */
5683 window = Foverlay_get (overlay, Qwindow);
5684 if (WINDOWP (window) && XWINDOW (window) != it->w)
5685 continue;
5687 /* If the text ``under'' the overlay is invisible, it has a zero
5688 dimension, and both before- and after-strings apply. */
5689 invisible = Foverlay_get (overlay, Qinvisible);
5690 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5692 /* If overlay has a non-empty before-string, record it. */
5693 if ((start == charpos || (end == charpos && invis != 0))
5694 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5695 && SCHARS (str))
5696 RECORD_OVERLAY_STRING (overlay, str, false);
5698 /* If overlay has a non-empty after-string, record it. */
5699 if ((end == charpos || (start == charpos && invis != 0))
5700 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5701 && SCHARS (str))
5702 RECORD_OVERLAY_STRING (overlay, str, true);
5705 #undef RECORD_OVERLAY_STRING
5707 /* Sort entries. */
5708 if (n > 1)
5709 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5711 /* Record number of overlay strings, and where we computed it. */
5712 it->n_overlay_strings = n;
5713 it->overlay_strings_charpos = charpos;
5715 /* IT->current.overlay_string_index is the number of overlay strings
5716 that have already been consumed by IT. Copy some of the
5717 remaining overlay strings to IT->overlay_strings. */
5718 i = 0;
5719 j = it->current.overlay_string_index;
5720 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5722 it->overlay_strings[i] = entries[j].string;
5723 it->string_overlays[i++] = entries[j++].overlay;
5726 CHECK_IT (it);
5727 SAFE_FREE ();
5731 /* Get the first chunk of overlay strings at IT's current buffer
5732 position, or at CHARPOS if that is > 0. Value is true if at
5733 least one overlay string was found. */
5735 static bool
5736 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5738 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5739 process. This fills IT->overlay_strings with strings, and sets
5740 IT->n_overlay_strings to the total number of strings to process.
5741 IT->pos.overlay_string_index has to be set temporarily to zero
5742 because load_overlay_strings needs this; it must be set to -1
5743 when no overlay strings are found because a zero value would
5744 indicate a position in the first overlay string. */
5745 it->current.overlay_string_index = 0;
5746 load_overlay_strings (it, charpos);
5748 /* If we found overlay strings, set up IT to deliver display
5749 elements from the first one. Otherwise set up IT to deliver
5750 from current_buffer. */
5751 if (it->n_overlay_strings)
5753 /* Make sure we know settings in current_buffer, so that we can
5754 restore meaningful values when we're done with the overlay
5755 strings. */
5756 if (compute_stop_p)
5757 compute_stop_pos (it);
5758 eassert (it->face_id >= 0);
5760 /* Save IT's settings. They are restored after all overlay
5761 strings have been processed. */
5762 eassert (!compute_stop_p || it->sp == 0);
5764 /* When called from handle_stop, there might be an empty display
5765 string loaded. In that case, don't bother saving it. But
5766 don't use this optimization with the bidi iterator, since we
5767 need the corresponding pop_it call to resync the bidi
5768 iterator's position with IT's position, after we are done
5769 with the overlay strings. (The corresponding call to pop_it
5770 in case of an empty display string is in
5771 next_overlay_string.) */
5772 if (!(!it->bidi_p
5773 && STRINGP (it->string) && !SCHARS (it->string)))
5774 push_it (it, NULL);
5776 /* Set up IT to deliver display elements from the first overlay
5777 string. */
5778 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5779 it->string = it->overlay_strings[0];
5780 it->from_overlay = Qnil;
5781 it->stop_charpos = 0;
5782 eassert (STRINGP (it->string));
5783 it->end_charpos = SCHARS (it->string);
5784 it->prev_stop = 0;
5785 it->base_level_stop = 0;
5786 it->multibyte_p = STRING_MULTIBYTE (it->string);
5787 it->method = GET_FROM_STRING;
5788 it->from_disp_prop_p = 0;
5790 /* Force paragraph direction to be that of the parent
5791 buffer. */
5792 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5793 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5794 else
5795 it->paragraph_embedding = L2R;
5797 /* Set up the bidi iterator for this overlay string. */
5798 if (it->bidi_p)
5800 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5802 it->bidi_it.string.lstring = it->string;
5803 it->bidi_it.string.s = NULL;
5804 it->bidi_it.string.schars = SCHARS (it->string);
5805 it->bidi_it.string.bufpos = pos;
5806 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5807 it->bidi_it.string.unibyte = !it->multibyte_p;
5808 it->bidi_it.w = it->w;
5809 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5811 return true;
5814 it->current.overlay_string_index = -1;
5815 return false;
5818 static bool
5819 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5821 it->string = Qnil;
5822 it->method = GET_FROM_BUFFER;
5824 get_overlay_strings_1 (it, charpos, true);
5826 CHECK_IT (it);
5828 /* Value is true if we found at least one overlay string. */
5829 return STRINGP (it->string);
5834 /***********************************************************************
5835 Saving and restoring state
5836 ***********************************************************************/
5838 /* Save current settings of IT on IT->stack. Called, for example,
5839 before setting up IT for an overlay string, to be able to restore
5840 IT's settings to what they were after the overlay string has been
5841 processed. If POSITION is non-NULL, it is the position to save on
5842 the stack instead of IT->position. */
5844 static void
5845 push_it (struct it *it, struct text_pos *position)
5847 struct iterator_stack_entry *p;
5849 eassert (it->sp < IT_STACK_SIZE);
5850 p = it->stack + it->sp;
5852 p->stop_charpos = it->stop_charpos;
5853 p->prev_stop = it->prev_stop;
5854 p->base_level_stop = it->base_level_stop;
5855 p->cmp_it = it->cmp_it;
5856 eassert (it->face_id >= 0);
5857 p->face_id = it->face_id;
5858 p->string = it->string;
5859 p->method = it->method;
5860 p->from_overlay = it->from_overlay;
5861 switch (p->method)
5863 case GET_FROM_IMAGE:
5864 p->u.image.object = it->object;
5865 p->u.image.image_id = it->image_id;
5866 p->u.image.slice = it->slice;
5867 break;
5868 case GET_FROM_STRETCH:
5869 p->u.stretch.object = it->object;
5870 break;
5872 p->position = position ? *position : it->position;
5873 p->current = it->current;
5874 p->end_charpos = it->end_charpos;
5875 p->string_nchars = it->string_nchars;
5876 p->area = it->area;
5877 p->multibyte_p = it->multibyte_p;
5878 p->avoid_cursor_p = it->avoid_cursor_p;
5879 p->space_width = it->space_width;
5880 p->font_height = it->font_height;
5881 p->voffset = it->voffset;
5882 p->string_from_display_prop_p = it->string_from_display_prop_p;
5883 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5884 p->display_ellipsis_p = false;
5885 p->line_wrap = it->line_wrap;
5886 p->bidi_p = it->bidi_p;
5887 p->paragraph_embedding = it->paragraph_embedding;
5888 p->from_disp_prop_p = it->from_disp_prop_p;
5889 ++it->sp;
5891 /* Save the state of the bidi iterator as well. */
5892 if (it->bidi_p)
5893 bidi_push_it (&it->bidi_it);
5896 static void
5897 iterate_out_of_display_property (struct it *it)
5899 bool buffer_p = !STRINGP (it->string);
5900 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5901 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5903 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5905 /* Maybe initialize paragraph direction. If we are at the beginning
5906 of a new paragraph, next_element_from_buffer may not have a
5907 chance to do that. */
5908 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5909 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5910 /* prev_stop can be zero, so check against BEGV as well. */
5911 while (it->bidi_it.charpos >= bob
5912 && it->prev_stop <= it->bidi_it.charpos
5913 && it->bidi_it.charpos < CHARPOS (it->position)
5914 && it->bidi_it.charpos < eob)
5915 bidi_move_to_visually_next (&it->bidi_it);
5916 /* Record the stop_pos we just crossed, for when we cross it
5917 back, maybe. */
5918 if (it->bidi_it.charpos > CHARPOS (it->position))
5919 it->prev_stop = CHARPOS (it->position);
5920 /* If we ended up not where pop_it put us, resync IT's
5921 positional members with the bidi iterator. */
5922 if (it->bidi_it.charpos != CHARPOS (it->position))
5923 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5924 if (buffer_p)
5925 it->current.pos = it->position;
5926 else
5927 it->current.string_pos = it->position;
5930 /* Restore IT's settings from IT->stack. Called, for example, when no
5931 more overlay strings must be processed, and we return to delivering
5932 display elements from a buffer, or when the end of a string from a
5933 `display' property is reached and we return to delivering display
5934 elements from an overlay string, or from a buffer. */
5936 static void
5937 pop_it (struct it *it)
5939 struct iterator_stack_entry *p;
5940 bool from_display_prop = it->from_disp_prop_p;
5942 eassert (it->sp > 0);
5943 --it->sp;
5944 p = it->stack + it->sp;
5945 it->stop_charpos = p->stop_charpos;
5946 it->prev_stop = p->prev_stop;
5947 it->base_level_stop = p->base_level_stop;
5948 it->cmp_it = p->cmp_it;
5949 it->face_id = p->face_id;
5950 it->current = p->current;
5951 it->position = p->position;
5952 it->string = p->string;
5953 it->from_overlay = p->from_overlay;
5954 if (NILP (it->string))
5955 SET_TEXT_POS (it->current.string_pos, -1, -1);
5956 it->method = p->method;
5957 switch (it->method)
5959 case GET_FROM_IMAGE:
5960 it->image_id = p->u.image.image_id;
5961 it->object = p->u.image.object;
5962 it->slice = p->u.image.slice;
5963 break;
5964 case GET_FROM_STRETCH:
5965 it->object = p->u.stretch.object;
5966 break;
5967 case GET_FROM_BUFFER:
5968 it->object = it->w->contents;
5969 break;
5970 case GET_FROM_STRING:
5972 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5974 /* Restore the face_box_p flag, since it could have been
5975 overwritten by the face of the object that we just finished
5976 displaying. */
5977 if (face)
5978 it->face_box_p = face->box != FACE_NO_BOX;
5979 it->object = it->string;
5981 break;
5982 case GET_FROM_DISPLAY_VECTOR:
5983 if (it->s)
5984 it->method = GET_FROM_C_STRING;
5985 else if (STRINGP (it->string))
5986 it->method = GET_FROM_STRING;
5987 else
5989 it->method = GET_FROM_BUFFER;
5990 it->object = it->w->contents;
5993 it->end_charpos = p->end_charpos;
5994 it->string_nchars = p->string_nchars;
5995 it->area = p->area;
5996 it->multibyte_p = p->multibyte_p;
5997 it->avoid_cursor_p = p->avoid_cursor_p;
5998 it->space_width = p->space_width;
5999 it->font_height = p->font_height;
6000 it->voffset = p->voffset;
6001 it->string_from_display_prop_p = p->string_from_display_prop_p;
6002 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6003 it->line_wrap = p->line_wrap;
6004 it->bidi_p = p->bidi_p;
6005 it->paragraph_embedding = p->paragraph_embedding;
6006 it->from_disp_prop_p = p->from_disp_prop_p;
6007 if (it->bidi_p)
6009 bidi_pop_it (&it->bidi_it);
6010 /* Bidi-iterate until we get out of the portion of text, if any,
6011 covered by a `display' text property or by an overlay with
6012 `display' property. (We cannot just jump there, because the
6013 internal coherency of the bidi iterator state can not be
6014 preserved across such jumps.) We also must determine the
6015 paragraph base direction if the overlay we just processed is
6016 at the beginning of a new paragraph. */
6017 if (from_display_prop
6018 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6019 iterate_out_of_display_property (it);
6021 eassert ((BUFFERP (it->object)
6022 && IT_CHARPOS (*it) == it->bidi_it.charpos
6023 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6024 || (STRINGP (it->object)
6025 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6026 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6027 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6033 /***********************************************************************
6034 Moving over lines
6035 ***********************************************************************/
6037 /* Set IT's current position to the previous line start. */
6039 static void
6040 back_to_previous_line_start (struct it *it)
6042 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6044 DEC_BOTH (cp, bp);
6045 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6049 /* Move IT to the next line start.
6051 Value is true if a newline was found. Set *SKIPPED_P to true if
6052 we skipped over part of the text (as opposed to moving the iterator
6053 continuously over the text). Otherwise, don't change the value
6054 of *SKIPPED_P.
6056 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6057 iterator on the newline, if it was found.
6059 Newlines may come from buffer text, overlay strings, or strings
6060 displayed via the `display' property. That's the reason we can't
6061 simply use find_newline_no_quit.
6063 Note that this function may not skip over invisible text that is so
6064 because of text properties and immediately follows a newline. If
6065 it would, function reseat_at_next_visible_line_start, when called
6066 from set_iterator_to_next, would effectively make invisible
6067 characters following a newline part of the wrong glyph row, which
6068 leads to wrong cursor motion. */
6070 static bool
6071 forward_to_next_line_start (struct it *it, bool *skipped_p,
6072 struct bidi_it *bidi_it_prev)
6074 ptrdiff_t old_selective;
6075 bool newline_found_p = false;
6076 int n;
6077 const int MAX_NEWLINE_DISTANCE = 500;
6079 /* If already on a newline, just consume it to avoid unintended
6080 skipping over invisible text below. */
6081 if (it->what == IT_CHARACTER
6082 && it->c == '\n'
6083 && CHARPOS (it->position) == IT_CHARPOS (*it))
6085 if (it->bidi_p && bidi_it_prev)
6086 *bidi_it_prev = it->bidi_it;
6087 set_iterator_to_next (it, false);
6088 it->c = 0;
6089 return true;
6092 /* Don't handle selective display in the following. It's (a)
6093 unnecessary because it's done by the caller, and (b) leads to an
6094 infinite recursion because next_element_from_ellipsis indirectly
6095 calls this function. */
6096 old_selective = it->selective;
6097 it->selective = 0;
6099 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6100 from buffer text. */
6101 for (n = 0;
6102 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6103 n += !STRINGP (it->string))
6105 if (!get_next_display_element (it))
6106 return false;
6107 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6108 if (newline_found_p && it->bidi_p && bidi_it_prev)
6109 *bidi_it_prev = it->bidi_it;
6110 set_iterator_to_next (it, false);
6113 /* If we didn't find a newline near enough, see if we can use a
6114 short-cut. */
6115 if (!newline_found_p)
6117 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6118 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6119 1, &bytepos);
6120 Lisp_Object pos;
6122 eassert (!STRINGP (it->string));
6124 /* If there isn't any `display' property in sight, and no
6125 overlays, we can just use the position of the newline in
6126 buffer text. */
6127 if (it->stop_charpos >= limit
6128 || ((pos = Fnext_single_property_change (make_number (start),
6129 Qdisplay, Qnil,
6130 make_number (limit)),
6131 NILP (pos))
6132 && next_overlay_change (start) == ZV))
6134 if (!it->bidi_p)
6136 IT_CHARPOS (*it) = limit;
6137 IT_BYTEPOS (*it) = bytepos;
6139 else
6141 struct bidi_it bprev;
6143 /* Help bidi.c avoid expensive searches for display
6144 properties and overlays, by telling it that there are
6145 none up to `limit'. */
6146 if (it->bidi_it.disp_pos < limit)
6148 it->bidi_it.disp_pos = limit;
6149 it->bidi_it.disp_prop = 0;
6151 do {
6152 bprev = it->bidi_it;
6153 bidi_move_to_visually_next (&it->bidi_it);
6154 } while (it->bidi_it.charpos != limit);
6155 IT_CHARPOS (*it) = limit;
6156 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6157 if (bidi_it_prev)
6158 *bidi_it_prev = bprev;
6160 *skipped_p = newline_found_p = true;
6162 else
6164 while (get_next_display_element (it)
6165 && !newline_found_p)
6167 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6168 if (newline_found_p && it->bidi_p && bidi_it_prev)
6169 *bidi_it_prev = it->bidi_it;
6170 set_iterator_to_next (it, false);
6175 it->selective = old_selective;
6176 return newline_found_p;
6180 /* Set IT's current position to the previous visible line start. Skip
6181 invisible text that is so either due to text properties or due to
6182 selective display. Caution: this does not change IT->current_x and
6183 IT->hpos. */
6185 static void
6186 back_to_previous_visible_line_start (struct it *it)
6188 while (IT_CHARPOS (*it) > BEGV)
6190 back_to_previous_line_start (it);
6192 if (IT_CHARPOS (*it) <= BEGV)
6193 break;
6195 /* If selective > 0, then lines indented more than its value are
6196 invisible. */
6197 if (it->selective > 0
6198 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6199 it->selective))
6200 continue;
6202 /* Check the newline before point for invisibility. */
6204 Lisp_Object prop;
6205 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6206 Qinvisible, it->window);
6207 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6208 continue;
6211 if (IT_CHARPOS (*it) <= BEGV)
6212 break;
6215 struct it it2;
6216 void *it2data = NULL;
6217 ptrdiff_t pos;
6218 ptrdiff_t beg, end;
6219 Lisp_Object val, overlay;
6221 SAVE_IT (it2, *it, it2data);
6223 /* If newline is part of a composition, continue from start of composition */
6224 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6225 && beg < IT_CHARPOS (*it))
6226 goto replaced;
6228 /* If newline is replaced by a display property, find start of overlay
6229 or interval and continue search from that point. */
6230 pos = --IT_CHARPOS (it2);
6231 --IT_BYTEPOS (it2);
6232 it2.sp = 0;
6233 bidi_unshelve_cache (NULL, false);
6234 it2.string_from_display_prop_p = false;
6235 it2.from_disp_prop_p = false;
6236 if (handle_display_prop (&it2) == HANDLED_RETURN
6237 && !NILP (val = get_char_property_and_overlay
6238 (make_number (pos), Qdisplay, Qnil, &overlay))
6239 && (OVERLAYP (overlay)
6240 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6241 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6243 RESTORE_IT (it, it, it2data);
6244 goto replaced;
6247 /* Newline is not replaced by anything -- so we are done. */
6248 RESTORE_IT (it, it, it2data);
6249 break;
6251 replaced:
6252 if (beg < BEGV)
6253 beg = BEGV;
6254 IT_CHARPOS (*it) = beg;
6255 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6259 it->continuation_lines_width = 0;
6261 eassert (IT_CHARPOS (*it) >= BEGV);
6262 eassert (IT_CHARPOS (*it) == BEGV
6263 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6264 CHECK_IT (it);
6268 /* Reseat iterator IT at the previous visible line start. Skip
6269 invisible text that is so either due to text properties or due to
6270 selective display. At the end, update IT's overlay information,
6271 face information etc. */
6273 void
6274 reseat_at_previous_visible_line_start (struct it *it)
6276 back_to_previous_visible_line_start (it);
6277 reseat (it, it->current.pos, true);
6278 CHECK_IT (it);
6282 /* Reseat iterator IT on the next visible line start in the current
6283 buffer. ON_NEWLINE_P means position IT on the newline
6284 preceding the line start. Skip over invisible text that is so
6285 because of selective display. Compute faces, overlays etc at the
6286 new position. Note that this function does not skip over text that
6287 is invisible because of text properties. */
6289 static void
6290 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6292 bool skipped_p = false;
6293 struct bidi_it bidi_it_prev;
6294 bool newline_found_p
6295 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6297 /* Skip over lines that are invisible because they are indented
6298 more than the value of IT->selective. */
6299 if (it->selective > 0)
6300 while (IT_CHARPOS (*it) < ZV
6301 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6302 it->selective))
6304 eassert (IT_BYTEPOS (*it) == BEGV
6305 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6306 newline_found_p =
6307 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6310 /* Position on the newline if that's what's requested. */
6311 if (on_newline_p && newline_found_p)
6313 if (STRINGP (it->string))
6315 if (IT_STRING_CHARPOS (*it) > 0)
6317 if (!it->bidi_p)
6319 --IT_STRING_CHARPOS (*it);
6320 --IT_STRING_BYTEPOS (*it);
6322 else
6324 /* We need to restore the bidi iterator to the state
6325 it had on the newline, and resync the IT's
6326 position with that. */
6327 it->bidi_it = bidi_it_prev;
6328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6333 else if (IT_CHARPOS (*it) > BEGV)
6335 if (!it->bidi_p)
6337 --IT_CHARPOS (*it);
6338 --IT_BYTEPOS (*it);
6340 else
6342 /* We need to restore the bidi iterator to the state it
6343 had on the newline and resync IT with that. */
6344 it->bidi_it = bidi_it_prev;
6345 IT_CHARPOS (*it) = it->bidi_it.charpos;
6346 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6348 reseat (it, it->current.pos, false);
6351 else if (skipped_p)
6352 reseat (it, it->current.pos, false);
6354 CHECK_IT (it);
6359 /***********************************************************************
6360 Changing an iterator's position
6361 ***********************************************************************/
6363 /* Change IT's current position to POS in current_buffer.
6364 If FORCE_P, always check for text properties at the new position.
6365 Otherwise, text properties are only looked up if POS >=
6366 IT->check_charpos of a property. */
6368 static void
6369 reseat (struct it *it, struct text_pos pos, bool force_p)
6371 ptrdiff_t original_pos = IT_CHARPOS (*it);
6373 reseat_1 (it, pos, false);
6375 /* Determine where to check text properties. Avoid doing it
6376 where possible because text property lookup is very expensive. */
6377 if (force_p
6378 || CHARPOS (pos) > it->stop_charpos
6379 || CHARPOS (pos) < original_pos)
6381 if (it->bidi_p)
6383 /* For bidi iteration, we need to prime prev_stop and
6384 base_level_stop with our best estimations. */
6385 /* Implementation note: Of course, POS is not necessarily a
6386 stop position, so assigning prev_pos to it is a lie; we
6387 should have called compute_stop_backwards. However, if
6388 the current buffer does not include any R2L characters,
6389 that call would be a waste of cycles, because the
6390 iterator will never move back, and thus never cross this
6391 "fake" stop position. So we delay that backward search
6392 until the time we really need it, in next_element_from_buffer. */
6393 if (CHARPOS (pos) != it->prev_stop)
6394 it->prev_stop = CHARPOS (pos);
6395 if (CHARPOS (pos) < it->base_level_stop)
6396 it->base_level_stop = 0; /* meaning it's unknown */
6397 handle_stop (it);
6399 else
6401 handle_stop (it);
6402 it->prev_stop = it->base_level_stop = 0;
6407 CHECK_IT (it);
6411 /* Change IT's buffer position to POS. SET_STOP_P means set
6412 IT->stop_pos to POS, also. */
6414 static void
6415 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6417 /* Don't call this function when scanning a C string. */
6418 eassert (it->s == NULL);
6420 /* POS must be a reasonable value. */
6421 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6423 it->current.pos = it->position = pos;
6424 it->end_charpos = ZV;
6425 it->dpvec = NULL;
6426 it->current.dpvec_index = -1;
6427 it->current.overlay_string_index = -1;
6428 IT_STRING_CHARPOS (*it) = -1;
6429 IT_STRING_BYTEPOS (*it) = -1;
6430 it->string = Qnil;
6431 it->method = GET_FROM_BUFFER;
6432 it->object = it->w->contents;
6433 it->area = TEXT_AREA;
6434 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6435 it->sp = 0;
6436 it->string_from_display_prop_p = false;
6437 it->string_from_prefix_prop_p = false;
6439 it->from_disp_prop_p = false;
6440 it->face_before_selective_p = false;
6441 if (it->bidi_p)
6443 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6444 &it->bidi_it);
6445 bidi_unshelve_cache (NULL, false);
6446 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6447 it->bidi_it.string.s = NULL;
6448 it->bidi_it.string.lstring = Qnil;
6449 it->bidi_it.string.bufpos = 0;
6450 it->bidi_it.string.from_disp_str = false;
6451 it->bidi_it.string.unibyte = false;
6452 it->bidi_it.w = it->w;
6455 if (set_stop_p)
6457 it->stop_charpos = CHARPOS (pos);
6458 it->base_level_stop = CHARPOS (pos);
6460 /* This make the information stored in it->cmp_it invalidate. */
6461 it->cmp_it.id = -1;
6465 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6466 If S is non-null, it is a C string to iterate over. Otherwise,
6467 STRING gives a Lisp string to iterate over.
6469 If PRECISION > 0, don't return more then PRECISION number of
6470 characters from the string.
6472 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6473 characters have been returned. FIELD_WIDTH < 0 means an infinite
6474 field width.
6476 MULTIBYTE = 0 means disable processing of multibyte characters,
6477 MULTIBYTE > 0 means enable it,
6478 MULTIBYTE < 0 means use IT->multibyte_p.
6480 IT must be initialized via a prior call to init_iterator before
6481 calling this function. */
6483 static void
6484 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6485 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6486 int multibyte)
6488 /* No text property checks performed by default, but see below. */
6489 it->stop_charpos = -1;
6491 /* Set iterator position and end position. */
6492 memset (&it->current, 0, sizeof it->current);
6493 it->current.overlay_string_index = -1;
6494 it->current.dpvec_index = -1;
6495 eassert (charpos >= 0);
6497 /* If STRING is specified, use its multibyteness, otherwise use the
6498 setting of MULTIBYTE, if specified. */
6499 if (multibyte >= 0)
6500 it->multibyte_p = multibyte > 0;
6502 /* Bidirectional reordering of strings is controlled by the default
6503 value of bidi-display-reordering. Don't try to reorder while
6504 loading loadup.el, as the necessary character property tables are
6505 not yet available. */
6506 it->bidi_p =
6507 NILP (Vpurify_flag)
6508 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6510 if (s == NULL)
6512 eassert (STRINGP (string));
6513 it->string = string;
6514 it->s = NULL;
6515 it->end_charpos = it->string_nchars = SCHARS (string);
6516 it->method = GET_FROM_STRING;
6517 it->current.string_pos = string_pos (charpos, string);
6519 if (it->bidi_p)
6521 it->bidi_it.string.lstring = string;
6522 it->bidi_it.string.s = NULL;
6523 it->bidi_it.string.schars = it->end_charpos;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = false;
6526 it->bidi_it.string.unibyte = !it->multibyte_p;
6527 it->bidi_it.w = it->w;
6528 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6529 FRAME_WINDOW_P (it->f), &it->bidi_it);
6532 else
6534 it->s = (const unsigned char *) s;
6535 it->string = Qnil;
6537 /* Note that we use IT->current.pos, not it->current.string_pos,
6538 for displaying C strings. */
6539 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6540 if (it->multibyte_p)
6542 it->current.pos = c_string_pos (charpos, s, true);
6543 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6545 else
6547 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6548 it->end_charpos = it->string_nchars = strlen (s);
6551 if (it->bidi_p)
6553 it->bidi_it.string.lstring = Qnil;
6554 it->bidi_it.string.s = (const unsigned char *) s;
6555 it->bidi_it.string.schars = it->end_charpos;
6556 it->bidi_it.string.bufpos = 0;
6557 it->bidi_it.string.from_disp_str = false;
6558 it->bidi_it.string.unibyte = !it->multibyte_p;
6559 it->bidi_it.w = it->w;
6560 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6561 &it->bidi_it);
6563 it->method = GET_FROM_C_STRING;
6566 /* PRECISION > 0 means don't return more than PRECISION characters
6567 from the string. */
6568 if (precision > 0 && it->end_charpos - charpos > precision)
6570 it->end_charpos = it->string_nchars = charpos + precision;
6571 if (it->bidi_p)
6572 it->bidi_it.string.schars = it->end_charpos;
6575 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6576 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6577 FIELD_WIDTH < 0 means infinite field width. This is useful for
6578 padding with `-' at the end of a mode line. */
6579 if (field_width < 0)
6580 field_width = INFINITY;
6581 /* Implementation note: We deliberately don't enlarge
6582 it->bidi_it.string.schars here to fit it->end_charpos, because
6583 the bidi iterator cannot produce characters out of thin air. */
6584 if (field_width > it->end_charpos - charpos)
6585 it->end_charpos = charpos + field_width;
6587 /* Use the standard display table for displaying strings. */
6588 if (DISP_TABLE_P (Vstandard_display_table))
6589 it->dp = XCHAR_TABLE (Vstandard_display_table);
6591 it->stop_charpos = charpos;
6592 it->prev_stop = charpos;
6593 it->base_level_stop = 0;
6594 if (it->bidi_p)
6596 it->bidi_it.first_elt = true;
6597 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6598 it->bidi_it.disp_pos = -1;
6600 if (s == NULL && it->multibyte_p)
6602 ptrdiff_t endpos = SCHARS (it->string);
6603 if (endpos > it->end_charpos)
6604 endpos = it->end_charpos;
6605 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6606 it->string);
6608 CHECK_IT (it);
6613 /***********************************************************************
6614 Iteration
6615 ***********************************************************************/
6617 /* Map enum it_method value to corresponding next_element_from_* function. */
6619 typedef bool (*next_element_function) (struct it *);
6621 static next_element_function const get_next_element[NUM_IT_METHODS] =
6623 next_element_from_buffer,
6624 next_element_from_display_vector,
6625 next_element_from_string,
6626 next_element_from_c_string,
6627 next_element_from_image,
6628 next_element_from_stretch
6631 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6634 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6635 (possibly with the following characters). */
6637 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6638 ((IT)->cmp_it.id >= 0 \
6639 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6640 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6641 END_CHARPOS, (IT)->w, \
6642 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6643 (IT)->string)))
6646 /* Lookup the char-table Vglyphless_char_display for character C (-1
6647 if we want information for no-font case), and return the display
6648 method symbol. By side-effect, update it->what and
6649 it->glyphless_method. This function is called from
6650 get_next_display_element for each character element, and from
6651 x_produce_glyphs when no suitable font was found. */
6653 Lisp_Object
6654 lookup_glyphless_char_display (int c, struct it *it)
6656 Lisp_Object glyphless_method = Qnil;
6658 if (CHAR_TABLE_P (Vglyphless_char_display)
6659 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6661 if (c >= 0)
6663 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6664 if (CONSP (glyphless_method))
6665 glyphless_method = FRAME_WINDOW_P (it->f)
6666 ? XCAR (glyphless_method)
6667 : XCDR (glyphless_method);
6669 else
6670 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6673 retry:
6674 if (NILP (glyphless_method))
6676 if (c >= 0)
6677 /* The default is to display the character by a proper font. */
6678 return Qnil;
6679 /* The default for the no-font case is to display an empty box. */
6680 glyphless_method = Qempty_box;
6682 if (EQ (glyphless_method, Qzero_width))
6684 if (c >= 0)
6685 return glyphless_method;
6686 /* This method can't be used for the no-font case. */
6687 glyphless_method = Qempty_box;
6689 if (EQ (glyphless_method, Qthin_space))
6690 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6691 else if (EQ (glyphless_method, Qempty_box))
6692 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6693 else if (EQ (glyphless_method, Qhex_code))
6694 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6695 else if (STRINGP (glyphless_method))
6696 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6697 else
6699 /* Invalid value. We use the default method. */
6700 glyphless_method = Qnil;
6701 goto retry;
6703 it->what = IT_GLYPHLESS;
6704 return glyphless_method;
6707 /* Merge escape glyph face and cache the result. */
6709 static struct frame *last_escape_glyph_frame = NULL;
6710 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6711 static int last_escape_glyph_merged_face_id = 0;
6713 static int
6714 merge_escape_glyph_face (struct it *it)
6716 int face_id;
6718 if (it->f == last_escape_glyph_frame
6719 && it->face_id == last_escape_glyph_face_id)
6720 face_id = last_escape_glyph_merged_face_id;
6721 else
6723 /* Merge the `escape-glyph' face into the current face. */
6724 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6725 last_escape_glyph_frame = it->f;
6726 last_escape_glyph_face_id = it->face_id;
6727 last_escape_glyph_merged_face_id = face_id;
6729 return face_id;
6732 /* Likewise for glyphless glyph face. */
6734 static struct frame *last_glyphless_glyph_frame = NULL;
6735 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6736 static int last_glyphless_glyph_merged_face_id = 0;
6739 merge_glyphless_glyph_face (struct it *it)
6741 int face_id;
6743 if (it->f == last_glyphless_glyph_frame
6744 && it->face_id == last_glyphless_glyph_face_id)
6745 face_id = last_glyphless_glyph_merged_face_id;
6746 else
6748 /* Merge the `glyphless-char' face into the current face. */
6749 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6750 last_glyphless_glyph_frame = it->f;
6751 last_glyphless_glyph_face_id = it->face_id;
6752 last_glyphless_glyph_merged_face_id = face_id;
6754 return face_id;
6757 /* Load IT's display element fields with information about the next
6758 display element from the current position of IT. Value is false if
6759 end of buffer (or C string) is reached. */
6761 static bool
6762 get_next_display_element (struct it *it)
6764 /* True means that we found a display element. False means that
6765 we hit the end of what we iterate over. Performance note: the
6766 function pointer `method' used here turns out to be faster than
6767 using a sequence of if-statements. */
6768 bool success_p;
6770 get_next:
6771 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6773 if (it->what == IT_CHARACTER)
6775 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6776 and only if (a) the resolved directionality of that character
6777 is R..." */
6778 /* FIXME: Do we need an exception for characters from display
6779 tables? */
6780 if (it->bidi_p && it->bidi_it.type == STRONG_R
6781 && !inhibit_bidi_mirroring)
6782 it->c = bidi_mirror_char (it->c);
6783 /* Map via display table or translate control characters.
6784 IT->c, IT->len etc. have been set to the next character by
6785 the function call above. If we have a display table, and it
6786 contains an entry for IT->c, translate it. Don't do this if
6787 IT->c itself comes from a display table, otherwise we could
6788 end up in an infinite recursion. (An alternative could be to
6789 count the recursion depth of this function and signal an
6790 error when a certain maximum depth is reached.) Is it worth
6791 it? */
6792 if (success_p && it->dpvec == NULL)
6794 Lisp_Object dv;
6795 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6796 bool nonascii_space_p = false;
6797 bool nonascii_hyphen_p = false;
6798 int c = it->c; /* This is the character to display. */
6800 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6802 eassert (SINGLE_BYTE_CHAR_P (c));
6803 if (unibyte_display_via_language_environment)
6805 c = DECODE_CHAR (unibyte, c);
6806 if (c < 0)
6807 c = BYTE8_TO_CHAR (it->c);
6809 else
6810 c = BYTE8_TO_CHAR (it->c);
6813 if (it->dp
6814 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6815 VECTORP (dv)))
6817 struct Lisp_Vector *v = XVECTOR (dv);
6819 /* Return the first character from the display table
6820 entry, if not empty. If empty, don't display the
6821 current character. */
6822 if (v->header.size)
6824 it->dpvec_char_len = it->len;
6825 it->dpvec = v->contents;
6826 it->dpend = v->contents + v->header.size;
6827 it->current.dpvec_index = 0;
6828 it->dpvec_face_id = -1;
6829 it->saved_face_id = it->face_id;
6830 it->method = GET_FROM_DISPLAY_VECTOR;
6831 it->ellipsis_p = false;
6833 else
6835 set_iterator_to_next (it, false);
6837 goto get_next;
6840 if (! NILP (lookup_glyphless_char_display (c, it)))
6842 if (it->what == IT_GLYPHLESS)
6843 goto done;
6844 /* Don't display this character. */
6845 set_iterator_to_next (it, false);
6846 goto get_next;
6849 /* If `nobreak-char-display' is non-nil, we display
6850 non-ASCII spaces and hyphens specially. */
6851 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6853 if (c == 0xA0)
6854 nonascii_space_p = true;
6855 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6856 nonascii_hyphen_p = true;
6859 /* Translate control characters into `\003' or `^C' form.
6860 Control characters coming from a display table entry are
6861 currently not translated because we use IT->dpvec to hold
6862 the translation. This could easily be changed but I
6863 don't believe that it is worth doing.
6865 The characters handled by `nobreak-char-display' must be
6866 translated too.
6868 Non-printable characters and raw-byte characters are also
6869 translated to octal form. */
6870 if (((c < ' ' || c == 127) /* ASCII control chars. */
6871 ? (it->area != TEXT_AREA
6872 /* In mode line, treat \n, \t like other crl chars. */
6873 || (c != '\t'
6874 && it->glyph_row
6875 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6876 || (c != '\n' && c != '\t'))
6877 : (nonascii_space_p
6878 || nonascii_hyphen_p
6879 || CHAR_BYTE8_P (c)
6880 || ! CHAR_PRINTABLE_P (c))))
6882 /* C is a control character, non-ASCII space/hyphen,
6883 raw-byte, or a non-printable character which must be
6884 displayed either as '\003' or as `^C' where the '\\'
6885 and '^' can be defined in the display table. Fill
6886 IT->ctl_chars with glyphs for what we have to
6887 display. Then, set IT->dpvec to these glyphs. */
6888 Lisp_Object gc;
6889 int ctl_len;
6890 int face_id;
6891 int lface_id = 0;
6892 int escape_glyph;
6894 /* Handle control characters with ^. */
6896 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6898 int g;
6900 g = '^'; /* default glyph for Control */
6901 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6902 if (it->dp
6903 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6905 g = GLYPH_CODE_CHAR (gc);
6906 lface_id = GLYPH_CODE_FACE (gc);
6909 face_id = (lface_id
6910 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6911 : merge_escape_glyph_face (it));
6913 XSETINT (it->ctl_chars[0], g);
6914 XSETINT (it->ctl_chars[1], c ^ 0100);
6915 ctl_len = 2;
6916 goto display_control;
6919 /* Handle non-ascii space in the mode where it only gets
6920 highlighting. */
6922 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6924 /* Merge `nobreak-space' into the current face. */
6925 face_id = merge_faces (it->f, Qnobreak_space, 0,
6926 it->face_id);
6927 XSETINT (it->ctl_chars[0], ' ');
6928 ctl_len = 1;
6929 goto display_control;
6932 /* Handle sequences that start with the "escape glyph". */
6934 /* the default escape glyph is \. */
6935 escape_glyph = '\\';
6937 if (it->dp
6938 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6940 escape_glyph = GLYPH_CODE_CHAR (gc);
6941 lface_id = GLYPH_CODE_FACE (gc);
6944 face_id = (lface_id
6945 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6946 : merge_escape_glyph_face (it));
6948 /* Draw non-ASCII hyphen with just highlighting: */
6950 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6952 XSETINT (it->ctl_chars[0], '-');
6953 ctl_len = 1;
6954 goto display_control;
6957 /* Draw non-ASCII space/hyphen with escape glyph: */
6959 if (nonascii_space_p || nonascii_hyphen_p)
6961 XSETINT (it->ctl_chars[0], escape_glyph);
6962 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6963 ctl_len = 2;
6964 goto display_control;
6968 char str[10];
6969 int len, i;
6971 if (CHAR_BYTE8_P (c))
6972 /* Display \200 instead of \17777600. */
6973 c = CHAR_TO_BYTE8 (c);
6974 len = sprintf (str, "%03o", c);
6976 XSETINT (it->ctl_chars[0], escape_glyph);
6977 for (i = 0; i < len; i++)
6978 XSETINT (it->ctl_chars[i + 1], str[i]);
6979 ctl_len = len + 1;
6982 display_control:
6983 /* Set up IT->dpvec and return first character from it. */
6984 it->dpvec_char_len = it->len;
6985 it->dpvec = it->ctl_chars;
6986 it->dpend = it->dpvec + ctl_len;
6987 it->current.dpvec_index = 0;
6988 it->dpvec_face_id = face_id;
6989 it->saved_face_id = it->face_id;
6990 it->method = GET_FROM_DISPLAY_VECTOR;
6991 it->ellipsis_p = false;
6992 goto get_next;
6994 it->char_to_display = c;
6996 else if (success_p)
6998 it->char_to_display = it->c;
7002 #ifdef HAVE_WINDOW_SYSTEM
7003 /* Adjust face id for a multibyte character. There are no multibyte
7004 character in unibyte text. */
7005 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7006 && it->multibyte_p
7007 && success_p
7008 && FRAME_WINDOW_P (it->f))
7010 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7012 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7014 /* Automatic composition with glyph-string. */
7015 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7017 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7019 else
7021 ptrdiff_t pos = (it->s ? -1
7022 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7023 : IT_CHARPOS (*it));
7024 int c;
7026 if (it->what == IT_CHARACTER)
7027 c = it->char_to_display;
7028 else
7030 struct composition *cmp = composition_table[it->cmp_it.id];
7031 int i;
7033 c = ' ';
7034 for (i = 0; i < cmp->glyph_len; i++)
7035 /* TAB in a composition means display glyphs with
7036 padding space on the left or right. */
7037 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7038 break;
7040 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7043 #endif /* HAVE_WINDOW_SYSTEM */
7045 done:
7046 /* Is this character the last one of a run of characters with
7047 box? If yes, set IT->end_of_box_run_p to true. */
7048 if (it->face_box_p
7049 && it->s == NULL)
7051 if (it->method == GET_FROM_STRING && it->sp)
7053 int face_id = underlying_face_id (it);
7054 struct face *face = FACE_FROM_ID (it->f, face_id);
7056 if (face)
7058 if (face->box == FACE_NO_BOX)
7060 /* If the box comes from face properties in a
7061 display string, check faces in that string. */
7062 int string_face_id = face_after_it_pos (it);
7063 it->end_of_box_run_p
7064 = (FACE_FROM_ID (it->f, string_face_id)->box
7065 == FACE_NO_BOX);
7067 /* Otherwise, the box comes from the underlying face.
7068 If this is the last string character displayed, check
7069 the next buffer location. */
7070 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7071 /* n_overlay_strings is unreliable unless
7072 overlay_string_index is non-negative. */
7073 && ((it->current.overlay_string_index >= 0
7074 && (it->current.overlay_string_index
7075 == it->n_overlay_strings - 1))
7076 /* A string from display property. */
7077 || it->from_disp_prop_p))
7079 ptrdiff_t ignore;
7080 int next_face_id;
7081 struct text_pos pos = it->current.pos;
7083 /* For a string from a display property, the next
7084 buffer position is stored in the 'position'
7085 member of the iteration stack slot below the
7086 current one, see handle_single_display_spec. By
7087 contrast, it->current.pos was is not yet updated
7088 to point to that buffer position; that will
7089 happen in pop_it, after we finish displaying the
7090 current string. Note that we already checked
7091 above that it->sp is positive, so subtracting one
7092 from it is safe. */
7093 if (it->from_disp_prop_p)
7094 pos = (it->stack + it->sp - 1)->position;
7095 else
7096 INC_TEXT_POS (pos, it->multibyte_p);
7098 if (CHARPOS (pos) >= ZV)
7099 it->end_of_box_run_p = true;
7100 else
7102 next_face_id = face_at_buffer_position
7103 (it->w, CHARPOS (pos), &ignore,
7104 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7105 it->end_of_box_run_p
7106 = (FACE_FROM_ID (it->f, next_face_id)->box
7107 == FACE_NO_BOX);
7112 /* next_element_from_display_vector sets this flag according to
7113 faces of the display vector glyphs, see there. */
7114 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7116 int face_id = face_after_it_pos (it);
7117 it->end_of_box_run_p
7118 = (face_id != it->face_id
7119 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7122 /* If we reached the end of the object we've been iterating (e.g., a
7123 display string or an overlay string), and there's something on
7124 IT->stack, proceed with what's on the stack. It doesn't make
7125 sense to return false if there's unprocessed stuff on the stack,
7126 because otherwise that stuff will never be displayed. */
7127 if (!success_p && it->sp > 0)
7129 set_iterator_to_next (it, false);
7130 success_p = get_next_display_element (it);
7133 /* Value is false if end of buffer or string reached. */
7134 return success_p;
7138 /* Move IT to the next display element.
7140 RESEAT_P means if called on a newline in buffer text,
7141 skip to the next visible line start.
7143 Functions get_next_display_element and set_iterator_to_next are
7144 separate because I find this arrangement easier to handle than a
7145 get_next_display_element function that also increments IT's
7146 position. The way it is we can first look at an iterator's current
7147 display element, decide whether it fits on a line, and if it does,
7148 increment the iterator position. The other way around we probably
7149 would either need a flag indicating whether the iterator has to be
7150 incremented the next time, or we would have to implement a
7151 decrement position function which would not be easy to write. */
7153 void
7154 set_iterator_to_next (struct it *it, bool reseat_p)
7156 /* Reset flags indicating start and end of a sequence of characters
7157 with box. Reset them at the start of this function because
7158 moving the iterator to a new position might set them. */
7159 it->start_of_box_run_p = it->end_of_box_run_p = false;
7161 switch (it->method)
7163 case GET_FROM_BUFFER:
7164 /* The current display element of IT is a character from
7165 current_buffer. Advance in the buffer, and maybe skip over
7166 invisible lines that are so because of selective display. */
7167 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7168 reseat_at_next_visible_line_start (it, false);
7169 else if (it->cmp_it.id >= 0)
7171 /* We are currently getting glyphs from a composition. */
7172 if (! it->bidi_p)
7174 IT_CHARPOS (*it) += it->cmp_it.nchars;
7175 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7177 else
7179 int i;
7181 /* Update IT's char/byte positions to point to the first
7182 character of the next grapheme cluster, or to the
7183 character visually after the current composition. */
7184 for (i = 0; i < it->cmp_it.nchars; i++)
7185 bidi_move_to_visually_next (&it->bidi_it);
7186 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7187 IT_CHARPOS (*it) = it->bidi_it.charpos;
7190 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7191 && it->cmp_it.to < it->cmp_it.nglyphs)
7193 /* Composition created while scanning forward. Proceed
7194 to the next grapheme cluster. */
7195 it->cmp_it.from = it->cmp_it.to;
7197 else if ((it->bidi_p && it->cmp_it.reversed_p)
7198 && it->cmp_it.from > 0)
7200 /* Composition created while scanning backward. Proceed
7201 to the previous grapheme cluster. */
7202 it->cmp_it.to = it->cmp_it.from;
7204 else
7206 /* No more grapheme clusters in this composition.
7207 Find the next stop position. */
7208 ptrdiff_t stop = it->end_charpos;
7210 if (it->bidi_it.scan_dir < 0)
7211 /* Now we are scanning backward and don't know
7212 where to stop. */
7213 stop = -1;
7214 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7215 IT_BYTEPOS (*it), stop, Qnil);
7218 else
7220 eassert (it->len != 0);
7222 if (!it->bidi_p)
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7227 else
7229 int prev_scan_dir = it->bidi_it.scan_dir;
7230 /* If this is a new paragraph, determine its base
7231 direction (a.k.a. its base embedding level). */
7232 if (it->bidi_it.new_paragraph)
7233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7234 false);
7235 bidi_move_to_visually_next (&it->bidi_it);
7236 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7237 IT_CHARPOS (*it) = it->bidi_it.charpos;
7238 if (prev_scan_dir != it->bidi_it.scan_dir)
7240 /* As the scan direction was changed, we must
7241 re-compute the stop position for composition. */
7242 ptrdiff_t stop = it->end_charpos;
7243 if (it->bidi_it.scan_dir < 0)
7244 stop = -1;
7245 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7246 IT_BYTEPOS (*it), stop, Qnil);
7249 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7251 break;
7253 case GET_FROM_C_STRING:
7254 /* Current display element of IT is from a C string. */
7255 if (!it->bidi_p
7256 /* If the string position is beyond string's end, it means
7257 next_element_from_c_string is padding the string with
7258 blanks, in which case we bypass the bidi iterator,
7259 because it cannot deal with such virtual characters. */
7260 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7262 IT_BYTEPOS (*it) += it->len;
7263 IT_CHARPOS (*it) += 1;
7265 else
7267 bidi_move_to_visually_next (&it->bidi_it);
7268 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7269 IT_CHARPOS (*it) = it->bidi_it.charpos;
7271 break;
7273 case GET_FROM_DISPLAY_VECTOR:
7274 /* Current display element of IT is from a display table entry.
7275 Advance in the display table definition. Reset it to null if
7276 end reached, and continue with characters from buffers/
7277 strings. */
7278 ++it->current.dpvec_index;
7280 /* Restore face of the iterator to what they were before the
7281 display vector entry (these entries may contain faces). */
7282 it->face_id = it->saved_face_id;
7284 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7286 bool recheck_faces = it->ellipsis_p;
7288 if (it->s)
7289 it->method = GET_FROM_C_STRING;
7290 else if (STRINGP (it->string))
7291 it->method = GET_FROM_STRING;
7292 else
7294 it->method = GET_FROM_BUFFER;
7295 it->object = it->w->contents;
7298 it->dpvec = NULL;
7299 it->current.dpvec_index = -1;
7301 /* Skip over characters which were displayed via IT->dpvec. */
7302 if (it->dpvec_char_len < 0)
7303 reseat_at_next_visible_line_start (it, true);
7304 else if (it->dpvec_char_len > 0)
7306 it->len = it->dpvec_char_len;
7307 set_iterator_to_next (it, reseat_p);
7310 /* Maybe recheck faces after display vector. */
7311 if (recheck_faces)
7313 if (it->method == GET_FROM_STRING)
7314 it->stop_charpos = IT_STRING_CHARPOS (*it);
7315 else
7316 it->stop_charpos = IT_CHARPOS (*it);
7319 break;
7321 case GET_FROM_STRING:
7322 /* Current display element is a character from a Lisp string. */
7323 eassert (it->s == NULL && STRINGP (it->string));
7324 /* Don't advance past string end. These conditions are true
7325 when set_iterator_to_next is called at the end of
7326 get_next_display_element, in which case the Lisp string is
7327 already exhausted, and all we want is pop the iterator
7328 stack. */
7329 if (it->current.overlay_string_index >= 0)
7331 /* This is an overlay string, so there's no padding with
7332 spaces, and the number of characters in the string is
7333 where the string ends. */
7334 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7335 goto consider_string_end;
7337 else
7339 /* Not an overlay string. There could be padding, so test
7340 against it->end_charpos. */
7341 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7342 goto consider_string_end;
7344 if (it->cmp_it.id >= 0)
7346 /* We are delivering display elements from a composition.
7347 Update the string position past the grapheme cluster
7348 we've just processed. */
7349 if (! it->bidi_p)
7351 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7352 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7354 else
7356 int i;
7358 for (i = 0; i < it->cmp_it.nchars; i++)
7359 bidi_move_to_visually_next (&it->bidi_it);
7360 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7361 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7364 /* Did we exhaust all the grapheme clusters of this
7365 composition? */
7366 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7367 && (it->cmp_it.to < it->cmp_it.nglyphs))
7369 /* Not all the grapheme clusters were processed yet;
7370 advance to the next cluster. */
7371 it->cmp_it.from = it->cmp_it.to;
7373 else if ((it->bidi_p && it->cmp_it.reversed_p)
7374 && it->cmp_it.from > 0)
7376 /* Likewise: advance to the next cluster, but going in
7377 the reverse direction. */
7378 it->cmp_it.to = it->cmp_it.from;
7380 else
7382 /* This composition was fully processed; find the next
7383 candidate place for checking for composed
7384 characters. */
7385 /* Always limit string searches to the string length;
7386 any padding spaces are not part of the string, and
7387 there cannot be any compositions in that padding. */
7388 ptrdiff_t stop = SCHARS (it->string);
7390 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7391 stop = -1;
7392 else if (it->end_charpos < stop)
7394 /* Cf. PRECISION in reseat_to_string: we might be
7395 limited in how many of the string characters we
7396 need to deliver. */
7397 stop = it->end_charpos;
7399 composition_compute_stop_pos (&it->cmp_it,
7400 IT_STRING_CHARPOS (*it),
7401 IT_STRING_BYTEPOS (*it), stop,
7402 it->string);
7405 else
7407 if (!it->bidi_p
7408 /* If the string position is beyond string's end, it
7409 means next_element_from_string is padding the string
7410 with blanks, in which case we bypass the bidi
7411 iterator, because it cannot deal with such virtual
7412 characters. */
7413 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7415 IT_STRING_BYTEPOS (*it) += it->len;
7416 IT_STRING_CHARPOS (*it) += 1;
7418 else
7420 int prev_scan_dir = it->bidi_it.scan_dir;
7422 bidi_move_to_visually_next (&it->bidi_it);
7423 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7424 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7425 /* If the scan direction changes, we may need to update
7426 the place where to check for composed characters. */
7427 if (prev_scan_dir != it->bidi_it.scan_dir)
7429 ptrdiff_t stop = SCHARS (it->string);
7431 if (it->bidi_it.scan_dir < 0)
7432 stop = -1;
7433 else if (it->end_charpos < stop)
7434 stop = it->end_charpos;
7436 composition_compute_stop_pos (&it->cmp_it,
7437 IT_STRING_CHARPOS (*it),
7438 IT_STRING_BYTEPOS (*it), stop,
7439 it->string);
7444 consider_string_end:
7446 if (it->current.overlay_string_index >= 0)
7448 /* IT->string is an overlay string. Advance to the
7449 next, if there is one. */
7450 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7452 it->ellipsis_p = false;
7453 next_overlay_string (it);
7454 if (it->ellipsis_p)
7455 setup_for_ellipsis (it, 0);
7458 else
7460 /* IT->string is not an overlay string. If we reached
7461 its end, and there is something on IT->stack, proceed
7462 with what is on the stack. This can be either another
7463 string, this time an overlay string, or a buffer. */
7464 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7465 && it->sp > 0)
7467 pop_it (it);
7468 if (it->method == GET_FROM_STRING)
7469 goto consider_string_end;
7472 break;
7474 case GET_FROM_IMAGE:
7475 case GET_FROM_STRETCH:
7476 /* The position etc with which we have to proceed are on
7477 the stack. The position may be at the end of a string,
7478 if the `display' property takes up the whole string. */
7479 eassert (it->sp > 0);
7480 pop_it (it);
7481 if (it->method == GET_FROM_STRING)
7482 goto consider_string_end;
7483 break;
7485 default:
7486 /* There are no other methods defined, so this should be a bug. */
7487 emacs_abort ();
7490 eassert (it->method != GET_FROM_STRING
7491 || (STRINGP (it->string)
7492 && IT_STRING_CHARPOS (*it) >= 0));
7495 /* Load IT's display element fields with information about the next
7496 display element which comes from a display table entry or from the
7497 result of translating a control character to one of the forms `^C'
7498 or `\003'.
7500 IT->dpvec holds the glyphs to return as characters.
7501 IT->saved_face_id holds the face id before the display vector--it
7502 is restored into IT->face_id in set_iterator_to_next. */
7504 static bool
7505 next_element_from_display_vector (struct it *it)
7507 Lisp_Object gc;
7508 int prev_face_id = it->face_id;
7509 int next_face_id;
7511 /* Precondition. */
7512 eassert (it->dpvec && it->current.dpvec_index >= 0);
7514 it->face_id = it->saved_face_id;
7516 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7517 That seemed totally bogus - so I changed it... */
7518 gc = it->dpvec[it->current.dpvec_index];
7520 if (GLYPH_CODE_P (gc))
7522 struct face *this_face, *prev_face, *next_face;
7524 it->c = GLYPH_CODE_CHAR (gc);
7525 it->len = CHAR_BYTES (it->c);
7527 /* The entry may contain a face id to use. Such a face id is
7528 the id of a Lisp face, not a realized face. A face id of
7529 zero means no face is specified. */
7530 if (it->dpvec_face_id >= 0)
7531 it->face_id = it->dpvec_face_id;
7532 else
7534 int lface_id = GLYPH_CODE_FACE (gc);
7535 if (lface_id > 0)
7536 it->face_id = merge_faces (it->f, Qt, lface_id,
7537 it->saved_face_id);
7540 /* Glyphs in the display vector could have the box face, so we
7541 need to set the related flags in the iterator, as
7542 appropriate. */
7543 this_face = FACE_FROM_ID (it->f, it->face_id);
7544 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7546 /* Is this character the first character of a box-face run? */
7547 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7548 && (!prev_face
7549 || prev_face->box == FACE_NO_BOX));
7551 /* For the last character of the box-face run, we need to look
7552 either at the next glyph from the display vector, or at the
7553 face we saw before the display vector. */
7554 next_face_id = it->saved_face_id;
7555 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7557 if (it->dpvec_face_id >= 0)
7558 next_face_id = it->dpvec_face_id;
7559 else
7561 int lface_id =
7562 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7564 if (lface_id > 0)
7565 next_face_id = merge_faces (it->f, Qt, lface_id,
7566 it->saved_face_id);
7569 next_face = FACE_FROM_ID (it->f, next_face_id);
7570 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7571 && (!next_face
7572 || next_face->box == FACE_NO_BOX));
7573 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7575 else
7576 /* Display table entry is invalid. Return a space. */
7577 it->c = ' ', it->len = 1;
7579 /* Don't change position and object of the iterator here. They are
7580 still the values of the character that had this display table
7581 entry or was translated, and that's what we want. */
7582 it->what = IT_CHARACTER;
7583 return true;
7586 /* Get the first element of string/buffer in the visual order, after
7587 being reseated to a new position in a string or a buffer. */
7588 static void
7589 get_visually_first_element (struct it *it)
7591 bool string_p = STRINGP (it->string) || it->s;
7592 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7593 ptrdiff_t bob = (string_p ? 0 : BEGV);
7595 if (STRINGP (it->string))
7597 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7598 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7600 else
7602 it->bidi_it.charpos = IT_CHARPOS (*it);
7603 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7606 if (it->bidi_it.charpos == eob)
7608 /* Nothing to do, but reset the FIRST_ELT flag, like
7609 bidi_paragraph_init does, because we are not going to
7610 call it. */
7611 it->bidi_it.first_elt = false;
7613 else if (it->bidi_it.charpos == bob
7614 || (!string_p
7615 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7616 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7618 /* If we are at the beginning of a line/string, we can produce
7619 the next element right away. */
7620 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7621 bidi_move_to_visually_next (&it->bidi_it);
7623 else
7625 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7627 /* We need to prime the bidi iterator starting at the line's or
7628 string's beginning, before we will be able to produce the
7629 next element. */
7630 if (string_p)
7631 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7632 else
7633 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7634 IT_BYTEPOS (*it), -1,
7635 &it->bidi_it.bytepos);
7636 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7639 /* Now return to buffer/string position where we were asked
7640 to get the next display element, and produce that. */
7641 bidi_move_to_visually_next (&it->bidi_it);
7643 while (it->bidi_it.bytepos != orig_bytepos
7644 && it->bidi_it.charpos < eob);
7647 /* Adjust IT's position information to where we ended up. */
7648 if (STRINGP (it->string))
7650 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7651 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7653 else
7655 IT_CHARPOS (*it) = it->bidi_it.charpos;
7656 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7659 if (STRINGP (it->string) || !it->s)
7661 ptrdiff_t stop, charpos, bytepos;
7663 if (STRINGP (it->string))
7665 eassert (!it->s);
7666 stop = SCHARS (it->string);
7667 if (stop > it->end_charpos)
7668 stop = it->end_charpos;
7669 charpos = IT_STRING_CHARPOS (*it);
7670 bytepos = IT_STRING_BYTEPOS (*it);
7672 else
7674 stop = it->end_charpos;
7675 charpos = IT_CHARPOS (*it);
7676 bytepos = IT_BYTEPOS (*it);
7678 if (it->bidi_it.scan_dir < 0)
7679 stop = -1;
7680 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7681 it->string);
7685 /* Load IT with the next display element from Lisp string IT->string.
7686 IT->current.string_pos is the current position within the string.
7687 If IT->current.overlay_string_index >= 0, the Lisp string is an
7688 overlay string. */
7690 static bool
7691 next_element_from_string (struct it *it)
7693 struct text_pos position;
7695 eassert (STRINGP (it->string));
7696 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7697 eassert (IT_STRING_CHARPOS (*it) >= 0);
7698 position = it->current.string_pos;
7700 /* With bidi reordering, the character to display might not be the
7701 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7702 that we were reseat()ed to a new string, whose paragraph
7703 direction is not known. */
7704 if (it->bidi_p && it->bidi_it.first_elt)
7706 get_visually_first_element (it);
7707 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7710 /* Time to check for invisible text? */
7711 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7713 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7715 if (!(!it->bidi_p
7716 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7717 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7719 /* With bidi non-linear iteration, we could find
7720 ourselves far beyond the last computed stop_charpos,
7721 with several other stop positions in between that we
7722 missed. Scan them all now, in buffer's logical
7723 order, until we find and handle the last stop_charpos
7724 that precedes our current position. */
7725 handle_stop_backwards (it, it->stop_charpos);
7726 return GET_NEXT_DISPLAY_ELEMENT (it);
7728 else
7730 if (it->bidi_p)
7732 /* Take note of the stop position we just moved
7733 across, for when we will move back across it. */
7734 it->prev_stop = it->stop_charpos;
7735 /* If we are at base paragraph embedding level, take
7736 note of the last stop position seen at this
7737 level. */
7738 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7739 it->base_level_stop = it->stop_charpos;
7741 handle_stop (it);
7743 /* Since a handler may have changed IT->method, we must
7744 recurse here. */
7745 return GET_NEXT_DISPLAY_ELEMENT (it);
7748 else if (it->bidi_p
7749 /* If we are before prev_stop, we may have overstepped
7750 on our way backwards a stop_pos, and if so, we need
7751 to handle that stop_pos. */
7752 && IT_STRING_CHARPOS (*it) < it->prev_stop
7753 /* We can sometimes back up for reasons that have nothing
7754 to do with bidi reordering. E.g., compositions. The
7755 code below is only needed when we are above the base
7756 embedding level, so test for that explicitly. */
7757 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7759 /* If we lost track of base_level_stop, we have no better
7760 place for handle_stop_backwards to start from than string
7761 beginning. This happens, e.g., when we were reseated to
7762 the previous screenful of text by vertical-motion. */
7763 if (it->base_level_stop <= 0
7764 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7765 it->base_level_stop = 0;
7766 handle_stop_backwards (it, it->base_level_stop);
7767 return GET_NEXT_DISPLAY_ELEMENT (it);
7771 if (it->current.overlay_string_index >= 0)
7773 /* Get the next character from an overlay string. In overlay
7774 strings, there is no field width or padding with spaces to
7775 do. */
7776 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7778 it->what = IT_EOB;
7779 return false;
7781 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7782 IT_STRING_BYTEPOS (*it),
7783 it->bidi_it.scan_dir < 0
7784 ? -1
7785 : SCHARS (it->string))
7786 && next_element_from_composition (it))
7788 return true;
7790 else if (STRING_MULTIBYTE (it->string))
7792 const unsigned char *s = (SDATA (it->string)
7793 + IT_STRING_BYTEPOS (*it));
7794 it->c = string_char_and_length (s, &it->len);
7796 else
7798 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7799 it->len = 1;
7802 else
7804 /* Get the next character from a Lisp string that is not an
7805 overlay string. Such strings come from the mode line, for
7806 example. We may have to pad with spaces, or truncate the
7807 string. See also next_element_from_c_string. */
7808 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7810 it->what = IT_EOB;
7811 return false;
7813 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7815 /* Pad with spaces. */
7816 it->c = ' ', it->len = 1;
7817 CHARPOS (position) = BYTEPOS (position) = -1;
7819 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7820 IT_STRING_BYTEPOS (*it),
7821 it->bidi_it.scan_dir < 0
7822 ? -1
7823 : it->string_nchars)
7824 && next_element_from_composition (it))
7826 return true;
7828 else if (STRING_MULTIBYTE (it->string))
7830 const unsigned char *s = (SDATA (it->string)
7831 + IT_STRING_BYTEPOS (*it));
7832 it->c = string_char_and_length (s, &it->len);
7834 else
7836 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7837 it->len = 1;
7841 /* Record what we have and where it came from. */
7842 it->what = IT_CHARACTER;
7843 it->object = it->string;
7844 it->position = position;
7845 return true;
7849 /* Load IT with next display element from C string IT->s.
7850 IT->string_nchars is the maximum number of characters to return
7851 from the string. IT->end_charpos may be greater than
7852 IT->string_nchars when this function is called, in which case we
7853 may have to return padding spaces. Value is false if end of string
7854 reached, including padding spaces. */
7856 static bool
7857 next_element_from_c_string (struct it *it)
7859 bool success_p = true;
7861 eassert (it->s);
7862 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7863 it->what = IT_CHARACTER;
7864 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7865 it->object = make_number (0);
7867 /* With bidi reordering, the character to display might not be the
7868 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7869 we were reseated to a new string, whose paragraph direction is
7870 not known. */
7871 if (it->bidi_p && it->bidi_it.first_elt)
7872 get_visually_first_element (it);
7874 /* IT's position can be greater than IT->string_nchars in case a
7875 field width or precision has been specified when the iterator was
7876 initialized. */
7877 if (IT_CHARPOS (*it) >= it->end_charpos)
7879 /* End of the game. */
7880 it->what = IT_EOB;
7881 success_p = false;
7883 else if (IT_CHARPOS (*it) >= it->string_nchars)
7885 /* Pad with spaces. */
7886 it->c = ' ', it->len = 1;
7887 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7889 else if (it->multibyte_p)
7890 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7891 else
7892 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7894 return success_p;
7898 /* Set up IT to return characters from an ellipsis, if appropriate.
7899 The definition of the ellipsis glyphs may come from a display table
7900 entry. This function fills IT with the first glyph from the
7901 ellipsis if an ellipsis is to be displayed. */
7903 static bool
7904 next_element_from_ellipsis (struct it *it)
7906 if (it->selective_display_ellipsis_p)
7907 setup_for_ellipsis (it, it->len);
7908 else
7910 /* The face at the current position may be different from the
7911 face we find after the invisible text. Remember what it
7912 was in IT->saved_face_id, and signal that it's there by
7913 setting face_before_selective_p. */
7914 it->saved_face_id = it->face_id;
7915 it->method = GET_FROM_BUFFER;
7916 it->object = it->w->contents;
7917 reseat_at_next_visible_line_start (it, true);
7918 it->face_before_selective_p = true;
7921 return GET_NEXT_DISPLAY_ELEMENT (it);
7925 /* Deliver an image display element. The iterator IT is already
7926 filled with image information (done in handle_display_prop). Value
7927 is always true. */
7930 static bool
7931 next_element_from_image (struct it *it)
7933 it->what = IT_IMAGE;
7934 return true;
7938 /* Fill iterator IT with next display element from a stretch glyph
7939 property. IT->object is the value of the text property. Value is
7940 always true. */
7942 static bool
7943 next_element_from_stretch (struct it *it)
7945 it->what = IT_STRETCH;
7946 return true;
7949 /* Scan backwards from IT's current position until we find a stop
7950 position, or until BEGV. This is called when we find ourself
7951 before both the last known prev_stop and base_level_stop while
7952 reordering bidirectional text. */
7954 static void
7955 compute_stop_pos_backwards (struct it *it)
7957 const int SCAN_BACK_LIMIT = 1000;
7958 struct text_pos pos;
7959 struct display_pos save_current = it->current;
7960 struct text_pos save_position = it->position;
7961 ptrdiff_t charpos = IT_CHARPOS (*it);
7962 ptrdiff_t where_we_are = charpos;
7963 ptrdiff_t save_stop_pos = it->stop_charpos;
7964 ptrdiff_t save_end_pos = it->end_charpos;
7966 eassert (NILP (it->string) && !it->s);
7967 eassert (it->bidi_p);
7968 it->bidi_p = false;
7971 it->end_charpos = min (charpos + 1, ZV);
7972 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7973 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7974 reseat_1 (it, pos, false);
7975 compute_stop_pos (it);
7976 /* We must advance forward, right? */
7977 if (it->stop_charpos <= charpos)
7978 emacs_abort ();
7980 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7982 if (it->stop_charpos <= where_we_are)
7983 it->prev_stop = it->stop_charpos;
7984 else
7985 it->prev_stop = BEGV;
7986 it->bidi_p = true;
7987 it->current = save_current;
7988 it->position = save_position;
7989 it->stop_charpos = save_stop_pos;
7990 it->end_charpos = save_end_pos;
7993 /* Scan forward from CHARPOS in the current buffer/string, until we
7994 find a stop position > current IT's position. Then handle the stop
7995 position before that. This is called when we bump into a stop
7996 position while reordering bidirectional text. CHARPOS should be
7997 the last previously processed stop_pos (or BEGV/0, if none were
7998 processed yet) whose position is less that IT's current
7999 position. */
8001 static void
8002 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8004 bool bufp = !STRINGP (it->string);
8005 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8006 struct display_pos save_current = it->current;
8007 struct text_pos save_position = it->position;
8008 struct text_pos pos1;
8009 ptrdiff_t next_stop;
8011 /* Scan in strict logical order. */
8012 eassert (it->bidi_p);
8013 it->bidi_p = false;
8016 it->prev_stop = charpos;
8017 if (bufp)
8019 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8020 reseat_1 (it, pos1, false);
8022 else
8023 it->current.string_pos = string_pos (charpos, it->string);
8024 compute_stop_pos (it);
8025 /* We must advance forward, right? */
8026 if (it->stop_charpos <= it->prev_stop)
8027 emacs_abort ();
8028 charpos = it->stop_charpos;
8030 while (charpos <= where_we_are);
8032 it->bidi_p = true;
8033 it->current = save_current;
8034 it->position = save_position;
8035 next_stop = it->stop_charpos;
8036 it->stop_charpos = it->prev_stop;
8037 handle_stop (it);
8038 it->stop_charpos = next_stop;
8041 /* Load IT with the next display element from current_buffer. Value
8042 is false if end of buffer reached. IT->stop_charpos is the next
8043 position at which to stop and check for text properties or buffer
8044 end. */
8046 static bool
8047 next_element_from_buffer (struct it *it)
8049 bool success_p = true;
8051 eassert (IT_CHARPOS (*it) >= BEGV);
8052 eassert (NILP (it->string) && !it->s);
8053 eassert (!it->bidi_p
8054 || (EQ (it->bidi_it.string.lstring, Qnil)
8055 && it->bidi_it.string.s == NULL));
8057 /* With bidi reordering, the character to display might not be the
8058 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8059 we were reseat()ed to a new buffer position, which is potentially
8060 a different paragraph. */
8061 if (it->bidi_p && it->bidi_it.first_elt)
8063 get_visually_first_element (it);
8064 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8067 if (IT_CHARPOS (*it) >= it->stop_charpos)
8069 if (IT_CHARPOS (*it) >= it->end_charpos)
8071 bool overlay_strings_follow_p;
8073 /* End of the game, except when overlay strings follow that
8074 haven't been returned yet. */
8075 if (it->overlay_strings_at_end_processed_p)
8076 overlay_strings_follow_p = false;
8077 else
8079 it->overlay_strings_at_end_processed_p = true;
8080 overlay_strings_follow_p = get_overlay_strings (it, 0);
8083 if (overlay_strings_follow_p)
8084 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8085 else
8087 it->what = IT_EOB;
8088 it->position = it->current.pos;
8089 success_p = false;
8092 else if (!(!it->bidi_p
8093 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8094 || IT_CHARPOS (*it) == it->stop_charpos))
8096 /* With bidi non-linear iteration, we could find ourselves
8097 far beyond the last computed stop_charpos, with several
8098 other stop positions in between that we missed. Scan
8099 them all now, in buffer's logical order, until we find
8100 and handle the last stop_charpos that precedes our
8101 current position. */
8102 handle_stop_backwards (it, it->stop_charpos);
8103 it->ignore_overlay_strings_at_pos_p = false;
8104 return GET_NEXT_DISPLAY_ELEMENT (it);
8106 else
8108 if (it->bidi_p)
8110 /* Take note of the stop position we just moved across,
8111 for when we will move back across it. */
8112 it->prev_stop = it->stop_charpos;
8113 /* If we are at base paragraph embedding level, take
8114 note of the last stop position seen at this
8115 level. */
8116 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8117 it->base_level_stop = it->stop_charpos;
8119 handle_stop (it);
8120 it->ignore_overlay_strings_at_pos_p = false;
8121 return GET_NEXT_DISPLAY_ELEMENT (it);
8124 else if (it->bidi_p
8125 /* If we are before prev_stop, we may have overstepped on
8126 our way backwards a stop_pos, and if so, we need to
8127 handle that stop_pos. */
8128 && IT_CHARPOS (*it) < it->prev_stop
8129 /* We can sometimes back up for reasons that have nothing
8130 to do with bidi reordering. E.g., compositions. The
8131 code below is only needed when we are above the base
8132 embedding level, so test for that explicitly. */
8133 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8135 if (it->base_level_stop <= 0
8136 || IT_CHARPOS (*it) < it->base_level_stop)
8138 /* If we lost track of base_level_stop, we need to find
8139 prev_stop by looking backwards. This happens, e.g., when
8140 we were reseated to the previous screenful of text by
8141 vertical-motion. */
8142 it->base_level_stop = BEGV;
8143 compute_stop_pos_backwards (it);
8144 handle_stop_backwards (it, it->prev_stop);
8146 else
8147 handle_stop_backwards (it, it->base_level_stop);
8148 it->ignore_overlay_strings_at_pos_p = false;
8149 return GET_NEXT_DISPLAY_ELEMENT (it);
8151 else
8153 /* No face changes, overlays etc. in sight, so just return a
8154 character from current_buffer. */
8155 unsigned char *p;
8156 ptrdiff_t stop;
8158 /* We moved to the next buffer position, so any info about
8159 previously seen overlays is no longer valid. */
8160 it->ignore_overlay_strings_at_pos_p = false;
8162 /* Maybe run the redisplay end trigger hook. Performance note:
8163 This doesn't seem to cost measurable time. */
8164 if (it->redisplay_end_trigger_charpos
8165 && it->glyph_row
8166 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8167 run_redisplay_end_trigger_hook (it);
8169 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8170 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8171 stop)
8172 && next_element_from_composition (it))
8174 return true;
8177 /* Get the next character, maybe multibyte. */
8178 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8179 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8180 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8181 else
8182 it->c = *p, it->len = 1;
8184 /* Record what we have and where it came from. */
8185 it->what = IT_CHARACTER;
8186 it->object = it->w->contents;
8187 it->position = it->current.pos;
8189 /* Normally we return the character found above, except when we
8190 really want to return an ellipsis for selective display. */
8191 if (it->selective)
8193 if (it->c == '\n')
8195 /* A value of selective > 0 means hide lines indented more
8196 than that number of columns. */
8197 if (it->selective > 0
8198 && IT_CHARPOS (*it) + 1 < ZV
8199 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8200 IT_BYTEPOS (*it) + 1,
8201 it->selective))
8203 success_p = next_element_from_ellipsis (it);
8204 it->dpvec_char_len = -1;
8207 else if (it->c == '\r' && it->selective == -1)
8209 /* A value of selective == -1 means that everything from the
8210 CR to the end of the line is invisible, with maybe an
8211 ellipsis displayed for it. */
8212 success_p = next_element_from_ellipsis (it);
8213 it->dpvec_char_len = -1;
8218 /* Value is false if end of buffer reached. */
8219 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8220 return success_p;
8224 /* Run the redisplay end trigger hook for IT. */
8226 static void
8227 run_redisplay_end_trigger_hook (struct it *it)
8229 /* IT->glyph_row should be non-null, i.e. we should be actually
8230 displaying something, or otherwise we should not run the hook. */
8231 eassert (it->glyph_row);
8233 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8234 it->redisplay_end_trigger_charpos = 0;
8236 /* Since we are *trying* to run these functions, don't try to run
8237 them again, even if they get an error. */
8238 wset_redisplay_end_trigger (it->w, Qnil);
8239 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8240 make_number (charpos));
8242 /* Notice if it changed the face of the character we are on. */
8243 handle_face_prop (it);
8247 /* Deliver a composition display element. Unlike the other
8248 next_element_from_XXX, this function is not registered in the array
8249 get_next_element[]. It is called from next_element_from_buffer and
8250 next_element_from_string when necessary. */
8252 static bool
8253 next_element_from_composition (struct it *it)
8255 it->what = IT_COMPOSITION;
8256 it->len = it->cmp_it.nbytes;
8257 if (STRINGP (it->string))
8259 if (it->c < 0)
8261 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8262 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8263 return false;
8265 it->position = it->current.string_pos;
8266 it->object = it->string;
8267 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8268 IT_STRING_BYTEPOS (*it), it->string);
8270 else
8272 if (it->c < 0)
8274 IT_CHARPOS (*it) += it->cmp_it.nchars;
8275 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8276 if (it->bidi_p)
8278 if (it->bidi_it.new_paragraph)
8279 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8280 false);
8281 /* Resync the bidi iterator with IT's new position.
8282 FIXME: this doesn't support bidirectional text. */
8283 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8284 bidi_move_to_visually_next (&it->bidi_it);
8286 return false;
8288 it->position = it->current.pos;
8289 it->object = it->w->contents;
8290 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8291 IT_BYTEPOS (*it), Qnil);
8293 return true;
8298 /***********************************************************************
8299 Moving an iterator without producing glyphs
8300 ***********************************************************************/
8302 /* Check if iterator is at a position corresponding to a valid buffer
8303 position after some move_it_ call. */
8305 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8306 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8309 /* Move iterator IT to a specified buffer or X position within one
8310 line on the display without producing glyphs.
8312 OP should be a bit mask including some or all of these bits:
8313 MOVE_TO_X: Stop upon reaching x-position TO_X.
8314 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8315 Regardless of OP's value, stop upon reaching the end of the display line.
8317 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8318 This means, in particular, that TO_X includes window's horizontal
8319 scroll amount.
8321 The return value has several possible values that
8322 say what condition caused the scan to stop:
8324 MOVE_POS_MATCH_OR_ZV
8325 - when TO_POS or ZV was reached.
8327 MOVE_X_REACHED
8328 -when TO_X was reached before TO_POS or ZV were reached.
8330 MOVE_LINE_CONTINUED
8331 - when we reached the end of the display area and the line must
8332 be continued.
8334 MOVE_LINE_TRUNCATED
8335 - when we reached the end of the display area and the line is
8336 truncated.
8338 MOVE_NEWLINE_OR_CR
8339 - when we stopped at a line end, i.e. a newline or a CR and selective
8340 display is on. */
8342 static enum move_it_result
8343 move_it_in_display_line_to (struct it *it,
8344 ptrdiff_t to_charpos, int to_x,
8345 enum move_operation_enum op)
8347 enum move_it_result result = MOVE_UNDEFINED;
8348 struct glyph_row *saved_glyph_row;
8349 struct it wrap_it, atpos_it, atx_it, ppos_it;
8350 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8351 void *ppos_data = NULL;
8352 bool may_wrap = false;
8353 enum it_method prev_method = it->method;
8354 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8355 bool saw_smaller_pos = prev_pos < to_charpos;
8357 /* Don't produce glyphs in produce_glyphs. */
8358 saved_glyph_row = it->glyph_row;
8359 it->glyph_row = NULL;
8361 /* Use wrap_it to save a copy of IT wherever a word wrap could
8362 occur. Use atpos_it to save a copy of IT at the desired buffer
8363 position, if found, so that we can scan ahead and check if the
8364 word later overshoots the window edge. Use atx_it similarly, for
8365 pixel positions. */
8366 wrap_it.sp = -1;
8367 atpos_it.sp = -1;
8368 atx_it.sp = -1;
8370 /* Use ppos_it under bidi reordering to save a copy of IT for the
8371 initial position. We restore that position in IT when we have
8372 scanned the entire display line without finding a match for
8373 TO_CHARPOS and all the character positions are greater than
8374 TO_CHARPOS. We then restart the scan from the initial position,
8375 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8376 the closest to TO_CHARPOS. */
8377 if (it->bidi_p)
8379 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8381 SAVE_IT (ppos_it, *it, ppos_data);
8382 closest_pos = IT_CHARPOS (*it);
8384 else
8385 closest_pos = ZV;
8388 #define BUFFER_POS_REACHED_P() \
8389 ((op & MOVE_TO_POS) != 0 \
8390 && BUFFERP (it->object) \
8391 && (IT_CHARPOS (*it) == to_charpos \
8392 || ((!it->bidi_p \
8393 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8394 && IT_CHARPOS (*it) > to_charpos) \
8395 || (it->what == IT_COMPOSITION \
8396 && ((IT_CHARPOS (*it) > to_charpos \
8397 && to_charpos >= it->cmp_it.charpos) \
8398 || (IT_CHARPOS (*it) < to_charpos \
8399 && to_charpos <= it->cmp_it.charpos)))) \
8400 && (it->method == GET_FROM_BUFFER \
8401 || (it->method == GET_FROM_DISPLAY_VECTOR \
8402 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8404 /* If there's a line-/wrap-prefix, handle it. */
8405 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8406 && it->current_y < it->last_visible_y)
8407 handle_line_prefix (it);
8409 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8410 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8412 while (true)
8414 int x, i, ascent = 0, descent = 0;
8416 /* Utility macro to reset an iterator with x, ascent, and descent. */
8417 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8418 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8419 (IT)->max_descent = descent)
8421 /* Stop if we move beyond TO_CHARPOS (after an image or a
8422 display string or stretch glyph). */
8423 if ((op & MOVE_TO_POS) != 0
8424 && BUFFERP (it->object)
8425 && it->method == GET_FROM_BUFFER
8426 && (((!it->bidi_p
8427 /* When the iterator is at base embedding level, we
8428 are guaranteed that characters are delivered for
8429 display in strictly increasing order of their
8430 buffer positions. */
8431 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8432 && IT_CHARPOS (*it) > to_charpos)
8433 || (it->bidi_p
8434 && (prev_method == GET_FROM_IMAGE
8435 || prev_method == GET_FROM_STRETCH
8436 || prev_method == GET_FROM_STRING)
8437 /* Passed TO_CHARPOS from left to right. */
8438 && ((prev_pos < to_charpos
8439 && IT_CHARPOS (*it) > to_charpos)
8440 /* Passed TO_CHARPOS from right to left. */
8441 || (prev_pos > to_charpos
8442 && IT_CHARPOS (*it) < to_charpos)))))
8444 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8446 result = MOVE_POS_MATCH_OR_ZV;
8447 break;
8449 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8450 /* If wrap_it is valid, the current position might be in a
8451 word that is wrapped. So, save the iterator in
8452 atpos_it and continue to see if wrapping happens. */
8453 SAVE_IT (atpos_it, *it, atpos_data);
8456 /* Stop when ZV reached.
8457 We used to stop here when TO_CHARPOS reached as well, but that is
8458 too soon if this glyph does not fit on this line. So we handle it
8459 explicitly below. */
8460 if (!get_next_display_element (it))
8462 result = MOVE_POS_MATCH_OR_ZV;
8463 break;
8466 if (it->line_wrap == TRUNCATE)
8468 if (BUFFER_POS_REACHED_P ())
8470 result = MOVE_POS_MATCH_OR_ZV;
8471 break;
8474 else
8476 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8478 if (IT_DISPLAYING_WHITESPACE (it))
8479 may_wrap = true;
8480 else if (may_wrap)
8482 /* We have reached a glyph that follows one or more
8483 whitespace characters. If the position is
8484 already found, we are done. */
8485 if (atpos_it.sp >= 0)
8487 RESTORE_IT (it, &atpos_it, atpos_data);
8488 result = MOVE_POS_MATCH_OR_ZV;
8489 goto done;
8491 if (atx_it.sp >= 0)
8493 RESTORE_IT (it, &atx_it, atx_data);
8494 result = MOVE_X_REACHED;
8495 goto done;
8497 /* Otherwise, we can wrap here. */
8498 SAVE_IT (wrap_it, *it, wrap_data);
8499 may_wrap = false;
8504 /* Remember the line height for the current line, in case
8505 the next element doesn't fit on the line. */
8506 ascent = it->max_ascent;
8507 descent = it->max_descent;
8509 /* The call to produce_glyphs will get the metrics of the
8510 display element IT is loaded with. Record the x-position
8511 before this display element, in case it doesn't fit on the
8512 line. */
8513 x = it->current_x;
8515 PRODUCE_GLYPHS (it);
8517 if (it->area != TEXT_AREA)
8519 prev_method = it->method;
8520 if (it->method == GET_FROM_BUFFER)
8521 prev_pos = IT_CHARPOS (*it);
8522 set_iterator_to_next (it, true);
8523 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8524 SET_TEXT_POS (this_line_min_pos,
8525 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8526 if (it->bidi_p
8527 && (op & MOVE_TO_POS)
8528 && IT_CHARPOS (*it) > to_charpos
8529 && IT_CHARPOS (*it) < closest_pos)
8530 closest_pos = IT_CHARPOS (*it);
8531 continue;
8534 /* The number of glyphs we get back in IT->nglyphs will normally
8535 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8536 character on a terminal frame, or (iii) a line end. For the
8537 second case, IT->nglyphs - 1 padding glyphs will be present.
8538 (On X frames, there is only one glyph produced for a
8539 composite character.)
8541 The behavior implemented below means, for continuation lines,
8542 that as many spaces of a TAB as fit on the current line are
8543 displayed there. For terminal frames, as many glyphs of a
8544 multi-glyph character are displayed in the current line, too.
8545 This is what the old redisplay code did, and we keep it that
8546 way. Under X, the whole shape of a complex character must
8547 fit on the line or it will be completely displayed in the
8548 next line.
8550 Note that both for tabs and padding glyphs, all glyphs have
8551 the same width. */
8552 if (it->nglyphs)
8554 /* More than one glyph or glyph doesn't fit on line. All
8555 glyphs have the same width. */
8556 int single_glyph_width = it->pixel_width / it->nglyphs;
8557 int new_x;
8558 int x_before_this_char = x;
8559 int hpos_before_this_char = it->hpos;
8561 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8563 new_x = x + single_glyph_width;
8565 /* We want to leave anything reaching TO_X to the caller. */
8566 if ((op & MOVE_TO_X) && new_x > to_x)
8568 if (BUFFER_POS_REACHED_P ())
8570 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8571 goto buffer_pos_reached;
8572 if (atpos_it.sp < 0)
8574 SAVE_IT (atpos_it, *it, atpos_data);
8575 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8578 else
8580 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8582 it->current_x = x;
8583 result = MOVE_X_REACHED;
8584 break;
8586 if (atx_it.sp < 0)
8588 SAVE_IT (atx_it, *it, atx_data);
8589 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8594 if (/* Lines are continued. */
8595 it->line_wrap != TRUNCATE
8596 && (/* And glyph doesn't fit on the line. */
8597 new_x > it->last_visible_x
8598 /* Or it fits exactly and we're on a window
8599 system frame. */
8600 || (new_x == it->last_visible_x
8601 && FRAME_WINDOW_P (it->f)
8602 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8603 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8604 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8606 if (/* IT->hpos == 0 means the very first glyph
8607 doesn't fit on the line, e.g. a wide image. */
8608 it->hpos == 0
8609 || (new_x == it->last_visible_x
8610 && FRAME_WINDOW_P (it->f)))
8612 ++it->hpos;
8613 it->current_x = new_x;
8615 /* The character's last glyph just barely fits
8616 in this row. */
8617 if (i == it->nglyphs - 1)
8619 /* If this is the destination position,
8620 return a position *before* it in this row,
8621 now that we know it fits in this row. */
8622 if (BUFFER_POS_REACHED_P ())
8624 if (it->line_wrap != WORD_WRAP
8625 || wrap_it.sp < 0
8626 /* If we've just found whitespace to
8627 wrap, effectively ignore the
8628 previous wrap point -- it is no
8629 longer relevant, but we won't
8630 have an opportunity to update it,
8631 since we've reached the edge of
8632 this screen line. */
8633 || (may_wrap
8634 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8636 it->hpos = hpos_before_this_char;
8637 it->current_x = x_before_this_char;
8638 result = MOVE_POS_MATCH_OR_ZV;
8639 break;
8641 if (it->line_wrap == WORD_WRAP
8642 && atpos_it.sp < 0)
8644 SAVE_IT (atpos_it, *it, atpos_data);
8645 atpos_it.current_x = x_before_this_char;
8646 atpos_it.hpos = hpos_before_this_char;
8650 prev_method = it->method;
8651 if (it->method == GET_FROM_BUFFER)
8652 prev_pos = IT_CHARPOS (*it);
8653 set_iterator_to_next (it, true);
8654 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8655 SET_TEXT_POS (this_line_min_pos,
8656 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8657 /* On graphical terminals, newlines may
8658 "overflow" into the fringe if
8659 overflow-newline-into-fringe is non-nil.
8660 On text terminals, and on graphical
8661 terminals with no right margin, newlines
8662 may overflow into the last glyph on the
8663 display line.*/
8664 if (!FRAME_WINDOW_P (it->f)
8665 || ((it->bidi_p
8666 && it->bidi_it.paragraph_dir == R2L)
8667 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8668 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8669 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8671 if (!get_next_display_element (it))
8673 result = MOVE_POS_MATCH_OR_ZV;
8674 break;
8676 if (BUFFER_POS_REACHED_P ())
8678 if (ITERATOR_AT_END_OF_LINE_P (it))
8679 result = MOVE_POS_MATCH_OR_ZV;
8680 else
8681 result = MOVE_LINE_CONTINUED;
8682 break;
8684 if (ITERATOR_AT_END_OF_LINE_P (it)
8685 && (it->line_wrap != WORD_WRAP
8686 || wrap_it.sp < 0
8687 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8689 result = MOVE_NEWLINE_OR_CR;
8690 break;
8695 else
8696 IT_RESET_X_ASCENT_DESCENT (it);
8698 /* If the screen line ends with whitespace, and we
8699 are under word-wrap, don't use wrap_it: it is no
8700 longer relevant, but we won't have an opportunity
8701 to update it, since we are done with this screen
8702 line. */
8703 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8705 /* If we've found TO_X, go back there, as we now
8706 know the last word fits on this screen line. */
8707 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8708 && atx_it.sp >= 0)
8710 RESTORE_IT (it, &atx_it, atx_data);
8711 atpos_it.sp = -1;
8712 atx_it.sp = -1;
8713 result = MOVE_X_REACHED;
8714 break;
8717 else if (wrap_it.sp >= 0)
8719 RESTORE_IT (it, &wrap_it, wrap_data);
8720 atpos_it.sp = -1;
8721 atx_it.sp = -1;
8724 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8725 IT_CHARPOS (*it)));
8726 result = MOVE_LINE_CONTINUED;
8727 break;
8730 if (BUFFER_POS_REACHED_P ())
8732 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8733 goto buffer_pos_reached;
8734 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8736 SAVE_IT (atpos_it, *it, atpos_data);
8737 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8741 if (new_x > it->first_visible_x)
8743 /* Glyph is visible. Increment number of glyphs that
8744 would be displayed. */
8745 ++it->hpos;
8749 if (result != MOVE_UNDEFINED)
8750 break;
8752 else if (BUFFER_POS_REACHED_P ())
8754 buffer_pos_reached:
8755 IT_RESET_X_ASCENT_DESCENT (it);
8756 result = MOVE_POS_MATCH_OR_ZV;
8757 break;
8759 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8761 /* Stop when TO_X specified and reached. This check is
8762 necessary here because of lines consisting of a line end,
8763 only. The line end will not produce any glyphs and we
8764 would never get MOVE_X_REACHED. */
8765 eassert (it->nglyphs == 0);
8766 result = MOVE_X_REACHED;
8767 break;
8770 /* Is this a line end? If yes, we're done. */
8771 if (ITERATOR_AT_END_OF_LINE_P (it))
8773 /* If we are past TO_CHARPOS, but never saw any character
8774 positions smaller than TO_CHARPOS, return
8775 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8776 did. */
8777 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8779 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8781 if (closest_pos < ZV)
8783 RESTORE_IT (it, &ppos_it, ppos_data);
8784 /* Don't recurse if closest_pos is equal to
8785 to_charpos, since we have just tried that. */
8786 if (closest_pos != to_charpos)
8787 move_it_in_display_line_to (it, closest_pos, -1,
8788 MOVE_TO_POS);
8789 result = MOVE_POS_MATCH_OR_ZV;
8791 else
8792 goto buffer_pos_reached;
8794 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8795 && IT_CHARPOS (*it) > to_charpos)
8796 goto buffer_pos_reached;
8797 else
8798 result = MOVE_NEWLINE_OR_CR;
8800 else
8801 result = MOVE_NEWLINE_OR_CR;
8802 break;
8805 prev_method = it->method;
8806 if (it->method == GET_FROM_BUFFER)
8807 prev_pos = IT_CHARPOS (*it);
8808 /* The current display element has been consumed. Advance
8809 to the next. */
8810 set_iterator_to_next (it, true);
8811 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8812 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8813 if (IT_CHARPOS (*it) < to_charpos)
8814 saw_smaller_pos = true;
8815 if (it->bidi_p
8816 && (op & MOVE_TO_POS)
8817 && IT_CHARPOS (*it) >= to_charpos
8818 && IT_CHARPOS (*it) < closest_pos)
8819 closest_pos = IT_CHARPOS (*it);
8821 /* Stop if lines are truncated and IT's current x-position is
8822 past the right edge of the window now. */
8823 if (it->line_wrap == TRUNCATE
8824 && it->current_x >= it->last_visible_x)
8826 if (!FRAME_WINDOW_P (it->f)
8827 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8828 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8829 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8830 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8832 bool at_eob_p = false;
8834 if ((at_eob_p = !get_next_display_element (it))
8835 || BUFFER_POS_REACHED_P ()
8836 /* If we are past TO_CHARPOS, but never saw any
8837 character positions smaller than TO_CHARPOS,
8838 return MOVE_POS_MATCH_OR_ZV, like the
8839 unidirectional display did. */
8840 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8841 && !saw_smaller_pos
8842 && IT_CHARPOS (*it) > to_charpos))
8844 if (it->bidi_p
8845 && !BUFFER_POS_REACHED_P ()
8846 && !at_eob_p && closest_pos < ZV)
8848 RESTORE_IT (it, &ppos_it, ppos_data);
8849 if (closest_pos != to_charpos)
8850 move_it_in_display_line_to (it, closest_pos, -1,
8851 MOVE_TO_POS);
8853 result = MOVE_POS_MATCH_OR_ZV;
8854 break;
8856 if (ITERATOR_AT_END_OF_LINE_P (it))
8858 result = MOVE_NEWLINE_OR_CR;
8859 break;
8862 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8863 && !saw_smaller_pos
8864 && IT_CHARPOS (*it) > to_charpos)
8866 if (closest_pos < ZV)
8868 RESTORE_IT (it, &ppos_it, ppos_data);
8869 if (closest_pos != to_charpos)
8870 move_it_in_display_line_to (it, closest_pos, -1,
8871 MOVE_TO_POS);
8873 result = MOVE_POS_MATCH_OR_ZV;
8874 break;
8876 result = MOVE_LINE_TRUNCATED;
8877 break;
8879 #undef IT_RESET_X_ASCENT_DESCENT
8882 #undef BUFFER_POS_REACHED_P
8884 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8885 restore the saved iterator. */
8886 if (atpos_it.sp >= 0)
8887 RESTORE_IT (it, &atpos_it, atpos_data);
8888 else if (atx_it.sp >= 0)
8889 RESTORE_IT (it, &atx_it, atx_data);
8891 done:
8893 if (atpos_data)
8894 bidi_unshelve_cache (atpos_data, true);
8895 if (atx_data)
8896 bidi_unshelve_cache (atx_data, true);
8897 if (wrap_data)
8898 bidi_unshelve_cache (wrap_data, true);
8899 if (ppos_data)
8900 bidi_unshelve_cache (ppos_data, true);
8902 /* Restore the iterator settings altered at the beginning of this
8903 function. */
8904 it->glyph_row = saved_glyph_row;
8905 return result;
8908 /* For external use. */
8909 void
8910 move_it_in_display_line (struct it *it,
8911 ptrdiff_t to_charpos, int to_x,
8912 enum move_operation_enum op)
8914 if (it->line_wrap == WORD_WRAP
8915 && (op & MOVE_TO_X))
8917 struct it save_it;
8918 void *save_data = NULL;
8919 int skip;
8921 SAVE_IT (save_it, *it, save_data);
8922 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8923 /* When word-wrap is on, TO_X may lie past the end
8924 of a wrapped line. Then it->current is the
8925 character on the next line, so backtrack to the
8926 space before the wrap point. */
8927 if (skip == MOVE_LINE_CONTINUED)
8929 int prev_x = max (it->current_x - 1, 0);
8930 RESTORE_IT (it, &save_it, save_data);
8931 move_it_in_display_line_to
8932 (it, -1, prev_x, MOVE_TO_X);
8934 else
8935 bidi_unshelve_cache (save_data, true);
8937 else
8938 move_it_in_display_line_to (it, to_charpos, to_x, op);
8942 /* Move IT forward until it satisfies one or more of the criteria in
8943 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8945 OP is a bit-mask that specifies where to stop, and in particular,
8946 which of those four position arguments makes a difference. See the
8947 description of enum move_operation_enum.
8949 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8950 screen line, this function will set IT to the next position that is
8951 displayed to the right of TO_CHARPOS on the screen.
8953 Return the maximum pixel length of any line scanned but never more
8954 than it.last_visible_x. */
8957 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8959 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8960 int line_height, line_start_x = 0, reached = 0;
8961 int max_current_x = 0;
8962 void *backup_data = NULL;
8964 for (;;)
8966 if (op & MOVE_TO_VPOS)
8968 /* If no TO_CHARPOS and no TO_X specified, stop at the
8969 start of the line TO_VPOS. */
8970 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8972 if (it->vpos == to_vpos)
8974 reached = 1;
8975 break;
8977 else
8978 skip = move_it_in_display_line_to (it, -1, -1, 0);
8980 else
8982 /* TO_VPOS >= 0 means stop at TO_X in the line at
8983 TO_VPOS, or at TO_POS, whichever comes first. */
8984 if (it->vpos == to_vpos)
8986 reached = 2;
8987 break;
8990 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8992 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8994 reached = 3;
8995 break;
8997 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8999 /* We have reached TO_X but not in the line we want. */
9000 skip = move_it_in_display_line_to (it, to_charpos,
9001 -1, MOVE_TO_POS);
9002 if (skip == MOVE_POS_MATCH_OR_ZV)
9004 reached = 4;
9005 break;
9010 else if (op & MOVE_TO_Y)
9012 struct it it_backup;
9014 if (it->line_wrap == WORD_WRAP)
9015 SAVE_IT (it_backup, *it, backup_data);
9017 /* TO_Y specified means stop at TO_X in the line containing
9018 TO_Y---or at TO_CHARPOS if this is reached first. The
9019 problem is that we can't really tell whether the line
9020 contains TO_Y before we have completely scanned it, and
9021 this may skip past TO_X. What we do is to first scan to
9022 TO_X.
9024 If TO_X is not specified, use a TO_X of zero. The reason
9025 is to make the outcome of this function more predictable.
9026 If we didn't use TO_X == 0, we would stop at the end of
9027 the line which is probably not what a caller would expect
9028 to happen. */
9029 skip = move_it_in_display_line_to
9030 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9031 (MOVE_TO_X | (op & MOVE_TO_POS)));
9033 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9034 if (skip == MOVE_POS_MATCH_OR_ZV)
9035 reached = 5;
9036 else if (skip == MOVE_X_REACHED)
9038 /* If TO_X was reached, we want to know whether TO_Y is
9039 in the line. We know this is the case if the already
9040 scanned glyphs make the line tall enough. Otherwise,
9041 we must check by scanning the rest of the line. */
9042 line_height = it->max_ascent + it->max_descent;
9043 if (to_y >= it->current_y
9044 && to_y < it->current_y + line_height)
9046 reached = 6;
9047 break;
9049 SAVE_IT (it_backup, *it, backup_data);
9050 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9051 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9052 op & MOVE_TO_POS);
9053 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9054 line_height = it->max_ascent + it->max_descent;
9055 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9057 if (to_y >= it->current_y
9058 && to_y < it->current_y + line_height)
9060 /* If TO_Y is in this line and TO_X was reached
9061 above, we scanned too far. We have to restore
9062 IT's settings to the ones before skipping. But
9063 keep the more accurate values of max_ascent and
9064 max_descent we've found while skipping the rest
9065 of the line, for the sake of callers, such as
9066 pos_visible_p, that need to know the line
9067 height. */
9068 int max_ascent = it->max_ascent;
9069 int max_descent = it->max_descent;
9071 RESTORE_IT (it, &it_backup, backup_data);
9072 it->max_ascent = max_ascent;
9073 it->max_descent = max_descent;
9074 reached = 6;
9076 else
9078 skip = skip2;
9079 if (skip == MOVE_POS_MATCH_OR_ZV)
9080 reached = 7;
9083 else
9085 /* Check whether TO_Y is in this line. */
9086 line_height = it->max_ascent + it->max_descent;
9087 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9089 if (to_y >= it->current_y
9090 && to_y < it->current_y + line_height)
9092 if (to_y > it->current_y)
9093 max_current_x = max (it->current_x, max_current_x);
9095 /* When word-wrap is on, TO_X may lie past the end
9096 of a wrapped line. Then it->current is the
9097 character on the next line, so backtrack to the
9098 space before the wrap point. */
9099 if (skip == MOVE_LINE_CONTINUED
9100 && it->line_wrap == WORD_WRAP)
9102 int prev_x = max (it->current_x - 1, 0);
9103 RESTORE_IT (it, &it_backup, backup_data);
9104 skip = move_it_in_display_line_to
9105 (it, -1, prev_x, MOVE_TO_X);
9108 reached = 6;
9112 if (reached)
9114 max_current_x = max (it->current_x, max_current_x);
9115 break;
9118 else if (BUFFERP (it->object)
9119 && (it->method == GET_FROM_BUFFER
9120 || it->method == GET_FROM_STRETCH)
9121 && IT_CHARPOS (*it) >= to_charpos
9122 /* Under bidi iteration, a call to set_iterator_to_next
9123 can scan far beyond to_charpos if the initial
9124 portion of the next line needs to be reordered. In
9125 that case, give move_it_in_display_line_to another
9126 chance below. */
9127 && !(it->bidi_p
9128 && it->bidi_it.scan_dir == -1))
9129 skip = MOVE_POS_MATCH_OR_ZV;
9130 else
9131 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9133 switch (skip)
9135 case MOVE_POS_MATCH_OR_ZV:
9136 max_current_x = max (it->current_x, max_current_x);
9137 reached = 8;
9138 goto out;
9140 case MOVE_NEWLINE_OR_CR:
9141 max_current_x = max (it->current_x, max_current_x);
9142 set_iterator_to_next (it, true);
9143 it->continuation_lines_width = 0;
9144 break;
9146 case MOVE_LINE_TRUNCATED:
9147 max_current_x = it->last_visible_x;
9148 it->continuation_lines_width = 0;
9149 reseat_at_next_visible_line_start (it, false);
9150 if ((op & MOVE_TO_POS) != 0
9151 && IT_CHARPOS (*it) > to_charpos)
9153 reached = 9;
9154 goto out;
9156 break;
9158 case MOVE_LINE_CONTINUED:
9159 max_current_x = it->last_visible_x;
9160 /* For continued lines ending in a tab, some of the glyphs
9161 associated with the tab are displayed on the current
9162 line. Since it->current_x does not include these glyphs,
9163 we use it->last_visible_x instead. */
9164 if (it->c == '\t')
9166 it->continuation_lines_width += it->last_visible_x;
9167 /* When moving by vpos, ensure that the iterator really
9168 advances to the next line (bug#847, bug#969). Fixme:
9169 do we need to do this in other circumstances? */
9170 if (it->current_x != it->last_visible_x
9171 && (op & MOVE_TO_VPOS)
9172 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9174 line_start_x = it->current_x + it->pixel_width
9175 - it->last_visible_x;
9176 if (FRAME_WINDOW_P (it->f))
9178 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9179 struct font *face_font = face->font;
9181 /* When display_line produces a continued line
9182 that ends in a TAB, it skips a tab stop that
9183 is closer than the font's space character
9184 width (see x_produce_glyphs where it produces
9185 the stretch glyph which represents a TAB).
9186 We need to reproduce the same logic here. */
9187 eassert (face_font);
9188 if (face_font)
9190 if (line_start_x < face_font->space_width)
9191 line_start_x
9192 += it->tab_width * face_font->space_width;
9195 set_iterator_to_next (it, false);
9198 else
9199 it->continuation_lines_width += it->current_x;
9200 break;
9202 default:
9203 emacs_abort ();
9206 /* Reset/increment for the next run. */
9207 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9208 it->current_x = line_start_x;
9209 line_start_x = 0;
9210 it->hpos = 0;
9211 it->current_y += it->max_ascent + it->max_descent;
9212 ++it->vpos;
9213 last_height = it->max_ascent + it->max_descent;
9214 it->max_ascent = it->max_descent = 0;
9217 out:
9219 /* On text terminals, we may stop at the end of a line in the middle
9220 of a multi-character glyph. If the glyph itself is continued,
9221 i.e. it is actually displayed on the next line, don't treat this
9222 stopping point as valid; move to the next line instead (unless
9223 that brings us offscreen). */
9224 if (!FRAME_WINDOW_P (it->f)
9225 && op & MOVE_TO_POS
9226 && IT_CHARPOS (*it) == to_charpos
9227 && it->what == IT_CHARACTER
9228 && it->nglyphs > 1
9229 && it->line_wrap == WINDOW_WRAP
9230 && it->current_x == it->last_visible_x - 1
9231 && it->c != '\n'
9232 && it->c != '\t'
9233 && it->w->window_end_valid
9234 && it->vpos < it->w->window_end_vpos)
9236 it->continuation_lines_width += it->current_x;
9237 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9238 it->current_y += it->max_ascent + it->max_descent;
9239 ++it->vpos;
9240 last_height = it->max_ascent + it->max_descent;
9243 if (backup_data)
9244 bidi_unshelve_cache (backup_data, true);
9246 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9248 return max_current_x;
9252 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9254 If DY > 0, move IT backward at least that many pixels. DY = 0
9255 means move IT backward to the preceding line start or BEGV. This
9256 function may move over more than DY pixels if IT->current_y - DY
9257 ends up in the middle of a line; in this case IT->current_y will be
9258 set to the top of the line moved to. */
9260 void
9261 move_it_vertically_backward (struct it *it, int dy)
9263 int nlines, h;
9264 struct it it2, it3;
9265 void *it2data = NULL, *it3data = NULL;
9266 ptrdiff_t start_pos;
9267 int nchars_per_row
9268 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9269 ptrdiff_t pos_limit;
9271 move_further_back:
9272 eassert (dy >= 0);
9274 start_pos = IT_CHARPOS (*it);
9276 /* Estimate how many newlines we must move back. */
9277 nlines = max (1, dy / default_line_pixel_height (it->w));
9278 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9279 pos_limit = BEGV;
9280 else
9281 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9283 /* Set the iterator's position that many lines back. But don't go
9284 back more than NLINES full screen lines -- this wins a day with
9285 buffers which have very long lines. */
9286 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9287 back_to_previous_visible_line_start (it);
9289 /* Reseat the iterator here. When moving backward, we don't want
9290 reseat to skip forward over invisible text, set up the iterator
9291 to deliver from overlay strings at the new position etc. So,
9292 use reseat_1 here. */
9293 reseat_1 (it, it->current.pos, true);
9295 /* We are now surely at a line start. */
9296 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9297 reordering is in effect. */
9298 it->continuation_lines_width = 0;
9300 /* Move forward and see what y-distance we moved. First move to the
9301 start of the next line so that we get its height. We need this
9302 height to be able to tell whether we reached the specified
9303 y-distance. */
9304 SAVE_IT (it2, *it, it2data);
9305 it2.max_ascent = it2.max_descent = 0;
9308 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9309 MOVE_TO_POS | MOVE_TO_VPOS);
9311 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9312 /* If we are in a display string which starts at START_POS,
9313 and that display string includes a newline, and we are
9314 right after that newline (i.e. at the beginning of a
9315 display line), exit the loop, because otherwise we will
9316 infloop, since move_it_to will see that it is already at
9317 START_POS and will not move. */
9318 || (it2.method == GET_FROM_STRING
9319 && IT_CHARPOS (it2) == start_pos
9320 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9321 eassert (IT_CHARPOS (*it) >= BEGV);
9322 SAVE_IT (it3, it2, it3data);
9324 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9325 eassert (IT_CHARPOS (*it) >= BEGV);
9326 /* H is the actual vertical distance from the position in *IT
9327 and the starting position. */
9328 h = it2.current_y - it->current_y;
9329 /* NLINES is the distance in number of lines. */
9330 nlines = it2.vpos - it->vpos;
9332 /* Correct IT's y and vpos position
9333 so that they are relative to the starting point. */
9334 it->vpos -= nlines;
9335 it->current_y -= h;
9337 if (dy == 0)
9339 /* DY == 0 means move to the start of the screen line. The
9340 value of nlines is > 0 if continuation lines were involved,
9341 or if the original IT position was at start of a line. */
9342 RESTORE_IT (it, it, it2data);
9343 if (nlines > 0)
9344 move_it_by_lines (it, nlines);
9345 /* The above code moves us to some position NLINES down,
9346 usually to its first glyph (leftmost in an L2R line), but
9347 that's not necessarily the start of the line, under bidi
9348 reordering. We want to get to the character position
9349 that is immediately after the newline of the previous
9350 line. */
9351 if (it->bidi_p
9352 && !it->continuation_lines_width
9353 && !STRINGP (it->string)
9354 && IT_CHARPOS (*it) > BEGV
9355 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9357 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9359 DEC_BOTH (cp, bp);
9360 cp = find_newline_no_quit (cp, bp, -1, NULL);
9361 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9363 bidi_unshelve_cache (it3data, true);
9365 else
9367 /* The y-position we try to reach, relative to *IT.
9368 Note that H has been subtracted in front of the if-statement. */
9369 int target_y = it->current_y + h - dy;
9370 int y0 = it3.current_y;
9371 int y1;
9372 int line_height;
9374 RESTORE_IT (&it3, &it3, it3data);
9375 y1 = line_bottom_y (&it3);
9376 line_height = y1 - y0;
9377 RESTORE_IT (it, it, it2data);
9378 /* If we did not reach target_y, try to move further backward if
9379 we can. If we moved too far backward, try to move forward. */
9380 if (target_y < it->current_y
9381 /* This is heuristic. In a window that's 3 lines high, with
9382 a line height of 13 pixels each, recentering with point
9383 on the bottom line will try to move -39/2 = 19 pixels
9384 backward. Try to avoid moving into the first line. */
9385 && (it->current_y - target_y
9386 > min (window_box_height (it->w), line_height * 2 / 3))
9387 && IT_CHARPOS (*it) > BEGV)
9389 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9390 target_y - it->current_y));
9391 dy = it->current_y - target_y;
9392 goto move_further_back;
9394 else if (target_y >= it->current_y + line_height
9395 && IT_CHARPOS (*it) < ZV)
9397 /* Should move forward by at least one line, maybe more.
9399 Note: Calling move_it_by_lines can be expensive on
9400 terminal frames, where compute_motion is used (via
9401 vmotion) to do the job, when there are very long lines
9402 and truncate-lines is nil. That's the reason for
9403 treating terminal frames specially here. */
9405 if (!FRAME_WINDOW_P (it->f))
9406 move_it_vertically (it, target_y - (it->current_y + line_height));
9407 else
9411 move_it_by_lines (it, 1);
9413 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9420 /* Move IT by a specified amount of pixel lines DY. DY negative means
9421 move backwards. DY = 0 means move to start of screen line. At the
9422 end, IT will be on the start of a screen line. */
9424 void
9425 move_it_vertically (struct it *it, int dy)
9427 if (dy <= 0)
9428 move_it_vertically_backward (it, -dy);
9429 else
9431 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9432 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9433 MOVE_TO_POS | MOVE_TO_Y);
9434 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9436 /* If buffer ends in ZV without a newline, move to the start of
9437 the line to satisfy the post-condition. */
9438 if (IT_CHARPOS (*it) == ZV
9439 && ZV > BEGV
9440 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9441 move_it_by_lines (it, 0);
9446 /* Move iterator IT past the end of the text line it is in. */
9448 void
9449 move_it_past_eol (struct it *it)
9451 enum move_it_result rc;
9453 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9454 if (rc == MOVE_NEWLINE_OR_CR)
9455 set_iterator_to_next (it, false);
9459 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9460 negative means move up. DVPOS == 0 means move to the start of the
9461 screen line.
9463 Optimization idea: If we would know that IT->f doesn't use
9464 a face with proportional font, we could be faster for
9465 truncate-lines nil. */
9467 void
9468 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9471 /* The commented-out optimization uses vmotion on terminals. This
9472 gives bad results, because elements like it->what, on which
9473 callers such as pos_visible_p rely, aren't updated. */
9474 /* struct position pos;
9475 if (!FRAME_WINDOW_P (it->f))
9477 struct text_pos textpos;
9479 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9480 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9481 reseat (it, textpos, true);
9482 it->vpos += pos.vpos;
9483 it->current_y += pos.vpos;
9485 else */
9487 if (dvpos == 0)
9489 /* DVPOS == 0 means move to the start of the screen line. */
9490 move_it_vertically_backward (it, 0);
9491 /* Let next call to line_bottom_y calculate real line height. */
9492 last_height = 0;
9494 else if (dvpos > 0)
9496 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9497 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9499 /* Only move to the next buffer position if we ended up in a
9500 string from display property, not in an overlay string
9501 (before-string or after-string). That is because the
9502 latter don't conceal the underlying buffer position, so
9503 we can ask to move the iterator to the exact position we
9504 are interested in. Note that, even if we are already at
9505 IT_CHARPOS (*it), the call below is not a no-op, as it
9506 will detect that we are at the end of the string, pop the
9507 iterator, and compute it->current_x and it->hpos
9508 correctly. */
9509 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9510 -1, -1, -1, MOVE_TO_POS);
9513 else
9515 struct it it2;
9516 void *it2data = NULL;
9517 ptrdiff_t start_charpos, i;
9518 int nchars_per_row
9519 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9520 bool hit_pos_limit = false;
9521 ptrdiff_t pos_limit;
9523 /* Start at the beginning of the screen line containing IT's
9524 position. This may actually move vertically backwards,
9525 in case of overlays, so adjust dvpos accordingly. */
9526 dvpos += it->vpos;
9527 move_it_vertically_backward (it, 0);
9528 dvpos -= it->vpos;
9530 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9531 screen lines, and reseat the iterator there. */
9532 start_charpos = IT_CHARPOS (*it);
9533 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9534 pos_limit = BEGV;
9535 else
9536 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9538 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9539 back_to_previous_visible_line_start (it);
9540 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9541 hit_pos_limit = true;
9542 reseat (it, it->current.pos, true);
9544 /* Move further back if we end up in a string or an image. */
9545 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9547 /* First try to move to start of display line. */
9548 dvpos += it->vpos;
9549 move_it_vertically_backward (it, 0);
9550 dvpos -= it->vpos;
9551 if (IT_POS_VALID_AFTER_MOVE_P (it))
9552 break;
9553 /* If start of line is still in string or image,
9554 move further back. */
9555 back_to_previous_visible_line_start (it);
9556 reseat (it, it->current.pos, true);
9557 dvpos--;
9560 it->current_x = it->hpos = 0;
9562 /* Above call may have moved too far if continuation lines
9563 are involved. Scan forward and see if it did. */
9564 SAVE_IT (it2, *it, it2data);
9565 it2.vpos = it2.current_y = 0;
9566 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9567 it->vpos -= it2.vpos;
9568 it->current_y -= it2.current_y;
9569 it->current_x = it->hpos = 0;
9571 /* If we moved too far back, move IT some lines forward. */
9572 if (it2.vpos > -dvpos)
9574 int delta = it2.vpos + dvpos;
9576 RESTORE_IT (&it2, &it2, it2data);
9577 SAVE_IT (it2, *it, it2data);
9578 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9579 /* Move back again if we got too far ahead. */
9580 if (IT_CHARPOS (*it) >= start_charpos)
9581 RESTORE_IT (it, &it2, it2data);
9582 else
9583 bidi_unshelve_cache (it2data, true);
9585 else if (hit_pos_limit && pos_limit > BEGV
9586 && dvpos < 0 && it2.vpos < -dvpos)
9588 /* If we hit the limit, but still didn't make it far enough
9589 back, that means there's a display string with a newline
9590 covering a large chunk of text, and that caused
9591 back_to_previous_visible_line_start try to go too far.
9592 Punish those who commit such atrocities by going back
9593 until we've reached DVPOS, after lifting the limit, which
9594 could make it slow for very long lines. "If it hurts,
9595 don't do that!" */
9596 dvpos += it2.vpos;
9597 RESTORE_IT (it, it, it2data);
9598 for (i = -dvpos; i > 0; --i)
9600 back_to_previous_visible_line_start (it);
9601 it->vpos--;
9603 reseat_1 (it, it->current.pos, true);
9605 else
9606 RESTORE_IT (it, it, it2data);
9610 /* Return true if IT points into the middle of a display vector. */
9612 bool
9613 in_display_vector_p (struct it *it)
9615 return (it->method == GET_FROM_DISPLAY_VECTOR
9616 && it->current.dpvec_index > 0
9617 && it->dpvec + it->current.dpvec_index != it->dpend);
9620 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9621 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9622 WINDOW must be a live window and defaults to the selected one. The
9623 return value is a cons of the maximum pixel-width of any text line and
9624 the maximum pixel-height of all text lines.
9626 The optional argument FROM, if non-nil, specifies the first text
9627 position and defaults to the minimum accessible position of the buffer.
9628 If FROM is t, use the minimum accessible position that is not a newline
9629 character. TO, if non-nil, specifies the last text position and
9630 defaults to the maximum accessible position of the buffer. If TO is t,
9631 use the maximum accessible position that is not a newline character.
9633 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9634 width that can be returned. X-LIMIT nil or omitted, means to use the
9635 pixel-width of WINDOW's body; use this if you do not intend to change
9636 the width of WINDOW. Use the maximum width WINDOW may assume if you
9637 intend to change WINDOW's width. In any case, text whose x-coordinate
9638 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9639 can take some time, it's always a good idea to make this argument as
9640 small as possible; in particular, if the buffer contains long lines that
9641 shall be truncated anyway.
9643 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9644 height that can be returned. Text lines whose y-coordinate is beyond
9645 Y-LIMIT are ignored. Since calculating the text height of a large
9646 buffer can take some time, it makes sense to specify this argument if
9647 the size of the buffer is unknown.
9649 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9650 include the height of the mode- or header-line of WINDOW in the return
9651 value. If it is either the symbol `mode-line' or `header-line', include
9652 only the height of that line, if present, in the return value. If t,
9653 include the height of both, if present, in the return value. */)
9654 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9655 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9657 struct window *w = decode_live_window (window);
9658 Lisp_Object buffer = w->contents;
9659 struct buffer *b;
9660 struct it it;
9661 struct buffer *old_b = NULL;
9662 ptrdiff_t start, end, pos;
9663 struct text_pos startp;
9664 void *itdata = NULL;
9665 int c, max_y = -1, x = 0, y = 0;
9667 CHECK_BUFFER (buffer);
9668 b = XBUFFER (buffer);
9670 if (b != current_buffer)
9672 old_b = current_buffer;
9673 set_buffer_internal (b);
9676 if (NILP (from))
9677 start = BEGV;
9678 else if (EQ (from, Qt))
9680 start = pos = BEGV;
9681 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9682 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9683 start = pos;
9684 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9685 start = pos;
9687 else
9689 CHECK_NUMBER_COERCE_MARKER (from);
9690 start = min (max (XINT (from), BEGV), ZV);
9693 if (NILP (to))
9694 end = ZV;
9695 else if (EQ (to, Qt))
9697 end = pos = ZV;
9698 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9699 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9700 end = pos;
9701 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9702 end = pos;
9704 else
9706 CHECK_NUMBER_COERCE_MARKER (to);
9707 end = max (start, min (XINT (to), ZV));
9710 if (!NILP (y_limit))
9712 CHECK_NUMBER (y_limit);
9713 max_y = min (XINT (y_limit), INT_MAX);
9716 itdata = bidi_shelve_cache ();
9717 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9718 start_display (&it, w, startp);
9720 if (NILP (x_limit))
9721 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9722 else
9724 CHECK_NUMBER (x_limit);
9725 it.last_visible_x = min (XINT (x_limit), INFINITY);
9726 /* Actually, we never want move_it_to stop at to_x. But to make
9727 sure that move_it_in_display_line_to always moves far enough,
9728 we set it to INT_MAX and specify MOVE_TO_X. */
9729 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9730 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9733 y = it.current_y + it.max_ascent + it.max_descent;
9735 if (!EQ (mode_and_header_line, Qheader_line)
9736 && !EQ (mode_and_header_line, Qt))
9737 /* Do not count the header-line which was counted automatically by
9738 start_display. */
9739 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9741 if (EQ (mode_and_header_line, Qmode_line)
9742 || EQ (mode_and_header_line, Qt))
9743 /* Do count the mode-line which is not included automatically by
9744 start_display. */
9745 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9747 bidi_unshelve_cache (itdata, false);
9749 if (old_b)
9750 set_buffer_internal (old_b);
9752 return Fcons (make_number (x), make_number (y));
9755 /***********************************************************************
9756 Messages
9757 ***********************************************************************/
9760 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9761 to *Messages*. */
9763 void
9764 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9766 Lisp_Object msg, fmt;
9767 char *buffer;
9768 ptrdiff_t len;
9769 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9770 USE_SAFE_ALLOCA;
9772 fmt = msg = Qnil;
9773 GCPRO4 (fmt, msg, arg1, arg2);
9775 fmt = build_string (format);
9776 msg = CALLN (Fformat, fmt, arg1, arg2);
9778 len = SBYTES (msg) + 1;
9779 buffer = SAFE_ALLOCA (len);
9780 memcpy (buffer, SDATA (msg), len);
9782 message_dolog (buffer, len - 1, true, false);
9783 SAFE_FREE ();
9785 UNGCPRO;
9789 /* Output a newline in the *Messages* buffer if "needs" one. */
9791 void
9792 message_log_maybe_newline (void)
9794 if (message_log_need_newline)
9795 message_dolog ("", 0, true, false);
9799 /* Add a string M of length NBYTES to the message log, optionally
9800 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9801 true, means interpret the contents of M as multibyte. This
9802 function calls low-level routines in order to bypass text property
9803 hooks, etc. which might not be safe to run.
9805 This may GC (insert may run before/after change hooks),
9806 so the buffer M must NOT point to a Lisp string. */
9808 void
9809 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9811 const unsigned char *msg = (const unsigned char *) m;
9813 if (!NILP (Vmemory_full))
9814 return;
9816 if (!NILP (Vmessage_log_max))
9818 struct buffer *oldbuf;
9819 Lisp_Object oldpoint, oldbegv, oldzv;
9820 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9821 ptrdiff_t point_at_end = 0;
9822 ptrdiff_t zv_at_end = 0;
9823 Lisp_Object old_deactivate_mark;
9824 struct gcpro gcpro1;
9826 old_deactivate_mark = Vdeactivate_mark;
9827 oldbuf = current_buffer;
9829 /* Ensure the Messages buffer exists, and switch to it.
9830 If we created it, set the major-mode. */
9831 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9832 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9833 if (newbuffer
9834 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9835 call0 (intern ("messages-buffer-mode"));
9837 bset_undo_list (current_buffer, Qt);
9838 bset_cache_long_scans (current_buffer, Qnil);
9840 oldpoint = message_dolog_marker1;
9841 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9842 oldbegv = message_dolog_marker2;
9843 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9844 oldzv = message_dolog_marker3;
9845 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9846 GCPRO1 (old_deactivate_mark);
9848 if (PT == Z)
9849 point_at_end = 1;
9850 if (ZV == Z)
9851 zv_at_end = 1;
9853 BEGV = BEG;
9854 BEGV_BYTE = BEG_BYTE;
9855 ZV = Z;
9856 ZV_BYTE = Z_BYTE;
9857 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9859 /* Insert the string--maybe converting multibyte to single byte
9860 or vice versa, so that all the text fits the buffer. */
9861 if (multibyte
9862 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9864 ptrdiff_t i;
9865 int c, char_bytes;
9866 char work[1];
9868 /* Convert a multibyte string to single-byte
9869 for the *Message* buffer. */
9870 for (i = 0; i < nbytes; i += char_bytes)
9872 c = string_char_and_length (msg + i, &char_bytes);
9873 work[0] = CHAR_TO_BYTE8 (c);
9874 insert_1_both (work, 1, 1, true, false, false);
9877 else if (! multibyte
9878 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9880 ptrdiff_t i;
9881 int c, char_bytes;
9882 unsigned char str[MAX_MULTIBYTE_LENGTH];
9883 /* Convert a single-byte string to multibyte
9884 for the *Message* buffer. */
9885 for (i = 0; i < nbytes; i++)
9887 c = msg[i];
9888 MAKE_CHAR_MULTIBYTE (c);
9889 char_bytes = CHAR_STRING (c, str);
9890 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
9893 else if (nbytes)
9894 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
9895 true, false, false);
9897 if (nlflag)
9899 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9900 printmax_t dups;
9902 insert_1_both ("\n", 1, 1, true, false, false);
9904 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
9905 this_bol = PT;
9906 this_bol_byte = PT_BYTE;
9908 /* See if this line duplicates the previous one.
9909 If so, combine duplicates. */
9910 if (this_bol > BEG)
9912 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
9913 prev_bol = PT;
9914 prev_bol_byte = PT_BYTE;
9916 dups = message_log_check_duplicate (prev_bol_byte,
9917 this_bol_byte);
9918 if (dups)
9920 del_range_both (prev_bol, prev_bol_byte,
9921 this_bol, this_bol_byte, false);
9922 if (dups > 1)
9924 char dupstr[sizeof " [ times]"
9925 + INT_STRLEN_BOUND (printmax_t)];
9927 /* If you change this format, don't forget to also
9928 change message_log_check_duplicate. */
9929 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9930 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9931 insert_1_both (dupstr, duplen, duplen,
9932 true, false, true);
9937 /* If we have more than the desired maximum number of lines
9938 in the *Messages* buffer now, delete the oldest ones.
9939 This is safe because we don't have undo in this buffer. */
9941 if (NATNUMP (Vmessage_log_max))
9943 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9944 -XFASTINT (Vmessage_log_max) - 1, false);
9945 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
9948 BEGV = marker_position (oldbegv);
9949 BEGV_BYTE = marker_byte_position (oldbegv);
9951 if (zv_at_end)
9953 ZV = Z;
9954 ZV_BYTE = Z_BYTE;
9956 else
9958 ZV = marker_position (oldzv);
9959 ZV_BYTE = marker_byte_position (oldzv);
9962 if (point_at_end)
9963 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9964 else
9965 /* We can't do Fgoto_char (oldpoint) because it will run some
9966 Lisp code. */
9967 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9968 marker_byte_position (oldpoint));
9970 UNGCPRO;
9971 unchain_marker (XMARKER (oldpoint));
9972 unchain_marker (XMARKER (oldbegv));
9973 unchain_marker (XMARKER (oldzv));
9975 /* We called insert_1_both above with its 5th argument (PREPARE)
9976 false, which prevents insert_1_both from calling
9977 prepare_to_modify_buffer, which in turns prevents us from
9978 incrementing windows_or_buffers_changed even if *Messages* is
9979 shown in some window. So we must manually set
9980 windows_or_buffers_changed here to make up for that. */
9981 windows_or_buffers_changed = old_windows_or_buffers_changed;
9982 bset_redisplay (current_buffer);
9984 set_buffer_internal (oldbuf);
9986 message_log_need_newline = !nlflag;
9987 Vdeactivate_mark = old_deactivate_mark;
9992 /* We are at the end of the buffer after just having inserted a newline.
9993 (Note: We depend on the fact we won't be crossing the gap.)
9994 Check to see if the most recent message looks a lot like the previous one.
9995 Return 0 if different, 1 if the new one should just replace it, or a
9996 value N > 1 if we should also append " [N times]". */
9998 static intmax_t
9999 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10001 ptrdiff_t i;
10002 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10003 bool seen_dots = false;
10004 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10005 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10007 for (i = 0; i < len; i++)
10009 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10010 seen_dots = true;
10011 if (p1[i] != p2[i])
10012 return seen_dots;
10014 p1 += len;
10015 if (*p1 == '\n')
10016 return 2;
10017 if (*p1++ == ' ' && *p1++ == '[')
10019 char *pend;
10020 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10021 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10022 return n + 1;
10024 return 0;
10028 /* Display an echo area message M with a specified length of NBYTES
10029 bytes. The string may include null characters. If M is not a
10030 string, clear out any existing message, and let the mini-buffer
10031 text show through.
10033 This function cancels echoing. */
10035 void
10036 message3 (Lisp_Object m)
10038 struct gcpro gcpro1;
10040 GCPRO1 (m);
10041 clear_message (true, true);
10042 cancel_echoing ();
10044 /* First flush out any partial line written with print. */
10045 message_log_maybe_newline ();
10046 if (STRINGP (m))
10048 ptrdiff_t nbytes = SBYTES (m);
10049 bool multibyte = STRING_MULTIBYTE (m);
10050 char *buffer;
10051 USE_SAFE_ALLOCA;
10052 SAFE_ALLOCA_STRING (buffer, m);
10053 message_dolog (buffer, nbytes, true, multibyte);
10054 SAFE_FREE ();
10056 message3_nolog (m);
10058 UNGCPRO;
10062 /* The non-logging version of message3.
10063 This does not cancel echoing, because it is used for echoing.
10064 Perhaps we need to make a separate function for echoing
10065 and make this cancel echoing. */
10067 void
10068 message3_nolog (Lisp_Object m)
10070 struct frame *sf = SELECTED_FRAME ();
10072 if (FRAME_INITIAL_P (sf))
10074 if (noninteractive_need_newline)
10075 putc ('\n', stderr);
10076 noninteractive_need_newline = false;
10077 if (STRINGP (m))
10079 Lisp_Object s = ENCODE_SYSTEM (m);
10081 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10083 if (!cursor_in_echo_area)
10084 fprintf (stderr, "\n");
10085 fflush (stderr);
10087 /* Error messages get reported properly by cmd_error, so this must be just an
10088 informative message; if the frame hasn't really been initialized yet, just
10089 toss it. */
10090 else if (INTERACTIVE && sf->glyphs_initialized_p)
10092 /* Get the frame containing the mini-buffer
10093 that the selected frame is using. */
10094 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10095 Lisp_Object frame = XWINDOW (mini_window)->frame;
10096 struct frame *f = XFRAME (frame);
10098 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10099 Fmake_frame_visible (frame);
10101 if (STRINGP (m) && SCHARS (m) > 0)
10103 set_message (m);
10104 if (minibuffer_auto_raise)
10105 Fraise_frame (frame);
10106 /* Assume we are not echoing.
10107 (If we are, echo_now will override this.) */
10108 echo_message_buffer = Qnil;
10110 else
10111 clear_message (true, true);
10113 do_pending_window_change (false);
10114 echo_area_display (true);
10115 do_pending_window_change (false);
10116 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10117 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10122 /* Display a null-terminated echo area message M. If M is 0, clear
10123 out any existing message, and let the mini-buffer text show through.
10125 The buffer M must continue to exist until after the echo area gets
10126 cleared or some other message gets displayed there. Do not pass
10127 text that is stored in a Lisp string. Do not pass text in a buffer
10128 that was alloca'd. */
10130 void
10131 message1 (const char *m)
10133 message3 (m ? build_unibyte_string (m) : Qnil);
10137 /* The non-logging counterpart of message1. */
10139 void
10140 message1_nolog (const char *m)
10142 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10145 /* Display a message M which contains a single %s
10146 which gets replaced with STRING. */
10148 void
10149 message_with_string (const char *m, Lisp_Object string, bool log)
10151 CHECK_STRING (string);
10153 if (noninteractive)
10155 if (m)
10157 /* ENCODE_SYSTEM below can GC and/or relocate the
10158 Lisp data, so make sure we don't use it here. */
10159 eassert (relocatable_string_data_p (m) != 1);
10161 if (noninteractive_need_newline)
10162 putc ('\n', stderr);
10163 noninteractive_need_newline = false;
10164 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10165 if (!cursor_in_echo_area)
10166 fprintf (stderr, "\n");
10167 fflush (stderr);
10170 else if (INTERACTIVE)
10172 /* The frame whose minibuffer we're going to display the message on.
10173 It may be larger than the selected frame, so we need
10174 to use its buffer, not the selected frame's buffer. */
10175 Lisp_Object mini_window;
10176 struct frame *f, *sf = SELECTED_FRAME ();
10178 /* Get the frame containing the minibuffer
10179 that the selected frame is using. */
10180 mini_window = FRAME_MINIBUF_WINDOW (sf);
10181 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10183 /* Error messages get reported properly by cmd_error, so this must be
10184 just an informative message; if the frame hasn't really been
10185 initialized yet, just toss it. */
10186 if (f->glyphs_initialized_p)
10188 struct gcpro gcpro1, gcpro2;
10190 Lisp_Object fmt = build_string (m);
10191 Lisp_Object msg = string;
10192 GCPRO2 (fmt, msg);
10194 msg = CALLN (Fformat, fmt, msg);
10196 if (log)
10197 message3 (msg);
10198 else
10199 message3_nolog (msg);
10201 UNGCPRO;
10203 /* Print should start at the beginning of the message
10204 buffer next time. */
10205 message_buf_print = false;
10211 /* Dump an informative message to the minibuf. If M is 0, clear out
10212 any existing message, and let the mini-buffer text show through. */
10214 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10215 vmessage (const char *m, va_list ap)
10217 if (noninteractive)
10219 if (m)
10221 if (noninteractive_need_newline)
10222 putc ('\n', stderr);
10223 noninteractive_need_newline = false;
10224 vfprintf (stderr, m, ap);
10225 if (!cursor_in_echo_area)
10226 fprintf (stderr, "\n");
10227 fflush (stderr);
10230 else if (INTERACTIVE)
10232 /* The frame whose mini-buffer we're going to display the message
10233 on. It may be larger than the selected frame, so we need to
10234 use its buffer, not the selected frame's buffer. */
10235 Lisp_Object mini_window;
10236 struct frame *f, *sf = SELECTED_FRAME ();
10238 /* Get the frame containing the mini-buffer
10239 that the selected frame is using. */
10240 mini_window = FRAME_MINIBUF_WINDOW (sf);
10241 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10243 /* Error messages get reported properly by cmd_error, so this must be
10244 just an informative message; if the frame hasn't really been
10245 initialized yet, just toss it. */
10246 if (f->glyphs_initialized_p)
10248 if (m)
10250 ptrdiff_t len;
10251 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10252 USE_SAFE_ALLOCA;
10253 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10255 len = doprnt (message_buf, maxsize, m, 0, ap);
10257 message3 (make_string (message_buf, len));
10258 SAFE_FREE ();
10260 else
10261 message1 (0);
10263 /* Print should start at the beginning of the message
10264 buffer next time. */
10265 message_buf_print = false;
10270 void
10271 message (const char *m, ...)
10273 va_list ap;
10274 va_start (ap, m);
10275 vmessage (m, ap);
10276 va_end (ap);
10280 #if false
10281 /* The non-logging version of message. */
10283 void
10284 message_nolog (const char *m, ...)
10286 Lisp_Object old_log_max;
10287 va_list ap;
10288 va_start (ap, m);
10289 old_log_max = Vmessage_log_max;
10290 Vmessage_log_max = Qnil;
10291 vmessage (m, ap);
10292 Vmessage_log_max = old_log_max;
10293 va_end (ap);
10295 #endif
10298 /* Display the current message in the current mini-buffer. This is
10299 only called from error handlers in process.c, and is not time
10300 critical. */
10302 void
10303 update_echo_area (void)
10305 if (!NILP (echo_area_buffer[0]))
10307 Lisp_Object string;
10308 string = Fcurrent_message ();
10309 message3 (string);
10314 /* Make sure echo area buffers in `echo_buffers' are live.
10315 If they aren't, make new ones. */
10317 static void
10318 ensure_echo_area_buffers (void)
10320 int i;
10322 for (i = 0; i < 2; ++i)
10323 if (!BUFFERP (echo_buffer[i])
10324 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10326 char name[30];
10327 Lisp_Object old_buffer;
10328 int j;
10330 old_buffer = echo_buffer[i];
10331 echo_buffer[i] = Fget_buffer_create
10332 (make_formatted_string (name, " *Echo Area %d*", i));
10333 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10334 /* to force word wrap in echo area -
10335 it was decided to postpone this*/
10336 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10338 for (j = 0; j < 2; ++j)
10339 if (EQ (old_buffer, echo_area_buffer[j]))
10340 echo_area_buffer[j] = echo_buffer[i];
10345 /* Call FN with args A1..A2 with either the current or last displayed
10346 echo_area_buffer as current buffer.
10348 WHICH zero means use the current message buffer
10349 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10350 from echo_buffer[] and clear it.
10352 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10353 suitable buffer from echo_buffer[] and clear it.
10355 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10356 that the current message becomes the last displayed one, make
10357 choose a suitable buffer for echo_area_buffer[0], and clear it.
10359 Value is what FN returns. */
10361 static bool
10362 with_echo_area_buffer (struct window *w, int which,
10363 bool (*fn) (ptrdiff_t, Lisp_Object),
10364 ptrdiff_t a1, Lisp_Object a2)
10366 Lisp_Object buffer;
10367 bool this_one, the_other, clear_buffer_p, rc;
10368 ptrdiff_t count = SPECPDL_INDEX ();
10370 /* If buffers aren't live, make new ones. */
10371 ensure_echo_area_buffers ();
10373 clear_buffer_p = false;
10375 if (which == 0)
10376 this_one = false, the_other = true;
10377 else if (which > 0)
10378 this_one = true, the_other = false;
10379 else
10381 this_one = false, the_other = true;
10382 clear_buffer_p = true;
10384 /* We need a fresh one in case the current echo buffer equals
10385 the one containing the last displayed echo area message. */
10386 if (!NILP (echo_area_buffer[this_one])
10387 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10388 echo_area_buffer[this_one] = Qnil;
10391 /* Choose a suitable buffer from echo_buffer[] is we don't
10392 have one. */
10393 if (NILP (echo_area_buffer[this_one]))
10395 echo_area_buffer[this_one]
10396 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10397 ? echo_buffer[the_other]
10398 : echo_buffer[this_one]);
10399 clear_buffer_p = true;
10402 buffer = echo_area_buffer[this_one];
10404 /* Don't get confused by reusing the buffer used for echoing
10405 for a different purpose. */
10406 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10407 cancel_echoing ();
10409 record_unwind_protect (unwind_with_echo_area_buffer,
10410 with_echo_area_buffer_unwind_data (w));
10412 /* Make the echo area buffer current. Note that for display
10413 purposes, it is not necessary that the displayed window's buffer
10414 == current_buffer, except for text property lookup. So, let's
10415 only set that buffer temporarily here without doing a full
10416 Fset_window_buffer. We must also change w->pointm, though,
10417 because otherwise an assertions in unshow_buffer fails, and Emacs
10418 aborts. */
10419 set_buffer_internal_1 (XBUFFER (buffer));
10420 if (w)
10422 wset_buffer (w, buffer);
10423 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10424 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10427 bset_undo_list (current_buffer, Qt);
10428 bset_read_only (current_buffer, Qnil);
10429 specbind (Qinhibit_read_only, Qt);
10430 specbind (Qinhibit_modification_hooks, Qt);
10432 if (clear_buffer_p && Z > BEG)
10433 del_range (BEG, Z);
10435 eassert (BEGV >= BEG);
10436 eassert (ZV <= Z && ZV >= BEGV);
10438 rc = fn (a1, a2);
10440 eassert (BEGV >= BEG);
10441 eassert (ZV <= Z && ZV >= BEGV);
10443 unbind_to (count, Qnil);
10444 return rc;
10448 /* Save state that should be preserved around the call to the function
10449 FN called in with_echo_area_buffer. */
10451 static Lisp_Object
10452 with_echo_area_buffer_unwind_data (struct window *w)
10454 int i = 0;
10455 Lisp_Object vector, tmp;
10457 /* Reduce consing by keeping one vector in
10458 Vwith_echo_area_save_vector. */
10459 vector = Vwith_echo_area_save_vector;
10460 Vwith_echo_area_save_vector = Qnil;
10462 if (NILP (vector))
10463 vector = Fmake_vector (make_number (11), Qnil);
10465 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10466 ASET (vector, i, Vdeactivate_mark); ++i;
10467 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10469 if (w)
10471 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10472 ASET (vector, i, w->contents); ++i;
10473 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10474 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10475 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10476 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10477 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10478 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10480 else
10482 int end = i + 8;
10483 for (; i < end; ++i)
10484 ASET (vector, i, Qnil);
10487 eassert (i == ASIZE (vector));
10488 return vector;
10492 /* Restore global state from VECTOR which was created by
10493 with_echo_area_buffer_unwind_data. */
10495 static void
10496 unwind_with_echo_area_buffer (Lisp_Object vector)
10498 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10499 Vdeactivate_mark = AREF (vector, 1);
10500 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10502 if (WINDOWP (AREF (vector, 3)))
10504 struct window *w;
10505 Lisp_Object buffer;
10507 w = XWINDOW (AREF (vector, 3));
10508 buffer = AREF (vector, 4);
10510 wset_buffer (w, buffer);
10511 set_marker_both (w->pointm, buffer,
10512 XFASTINT (AREF (vector, 5)),
10513 XFASTINT (AREF (vector, 6)));
10514 set_marker_both (w->old_pointm, buffer,
10515 XFASTINT (AREF (vector, 7)),
10516 XFASTINT (AREF (vector, 8)));
10517 set_marker_both (w->start, buffer,
10518 XFASTINT (AREF (vector, 9)),
10519 XFASTINT (AREF (vector, 10)));
10522 Vwith_echo_area_save_vector = vector;
10526 /* Set up the echo area for use by print functions. MULTIBYTE_P
10527 means we will print multibyte. */
10529 void
10530 setup_echo_area_for_printing (bool multibyte_p)
10532 /* If we can't find an echo area any more, exit. */
10533 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10534 Fkill_emacs (Qnil);
10536 ensure_echo_area_buffers ();
10538 if (!message_buf_print)
10540 /* A message has been output since the last time we printed.
10541 Choose a fresh echo area buffer. */
10542 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10543 echo_area_buffer[0] = echo_buffer[1];
10544 else
10545 echo_area_buffer[0] = echo_buffer[0];
10547 /* Switch to that buffer and clear it. */
10548 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10549 bset_truncate_lines (current_buffer, Qnil);
10551 if (Z > BEG)
10553 ptrdiff_t count = SPECPDL_INDEX ();
10554 specbind (Qinhibit_read_only, Qt);
10555 /* Note that undo recording is always disabled. */
10556 del_range (BEG, Z);
10557 unbind_to (count, Qnil);
10559 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10561 /* Set up the buffer for the multibyteness we need. */
10562 if (multibyte_p
10563 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10564 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10566 /* Raise the frame containing the echo area. */
10567 if (minibuffer_auto_raise)
10569 struct frame *sf = SELECTED_FRAME ();
10570 Lisp_Object mini_window;
10571 mini_window = FRAME_MINIBUF_WINDOW (sf);
10572 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10575 message_log_maybe_newline ();
10576 message_buf_print = true;
10578 else
10580 if (NILP (echo_area_buffer[0]))
10582 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10583 echo_area_buffer[0] = echo_buffer[1];
10584 else
10585 echo_area_buffer[0] = echo_buffer[0];
10588 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10590 /* Someone switched buffers between print requests. */
10591 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10592 bset_truncate_lines (current_buffer, Qnil);
10598 /* Display an echo area message in window W. Value is true if W's
10599 height is changed. If display_last_displayed_message_p,
10600 display the message that was last displayed, otherwise
10601 display the current message. */
10603 static bool
10604 display_echo_area (struct window *w)
10606 bool no_message_p, window_height_changed_p;
10608 /* Temporarily disable garbage collections while displaying the echo
10609 area. This is done because a GC can print a message itself.
10610 That message would modify the echo area buffer's contents while a
10611 redisplay of the buffer is going on, and seriously confuse
10612 redisplay. */
10613 ptrdiff_t count = inhibit_garbage_collection ();
10615 /* If there is no message, we must call display_echo_area_1
10616 nevertheless because it resizes the window. But we will have to
10617 reset the echo_area_buffer in question to nil at the end because
10618 with_echo_area_buffer will sets it to an empty buffer. */
10619 bool i = display_last_displayed_message_p;
10620 no_message_p = NILP (echo_area_buffer[i]);
10622 window_height_changed_p
10623 = with_echo_area_buffer (w, display_last_displayed_message_p,
10624 display_echo_area_1,
10625 (intptr_t) w, Qnil);
10627 if (no_message_p)
10628 echo_area_buffer[i] = Qnil;
10630 unbind_to (count, Qnil);
10631 return window_height_changed_p;
10635 /* Helper for display_echo_area. Display the current buffer which
10636 contains the current echo area message in window W, a mini-window,
10637 a pointer to which is passed in A1. A2..A4 are currently not used.
10638 Change the height of W so that all of the message is displayed.
10639 Value is true if height of W was changed. */
10641 static bool
10642 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10644 intptr_t i1 = a1;
10645 struct window *w = (struct window *) i1;
10646 Lisp_Object window;
10647 struct text_pos start;
10649 /* Do this before displaying, so that we have a large enough glyph
10650 matrix for the display. If we can't get enough space for the
10651 whole text, display the last N lines. That works by setting w->start. */
10652 bool window_height_changed_p = resize_mini_window (w, false);
10654 /* Use the starting position chosen by resize_mini_window. */
10655 SET_TEXT_POS_FROM_MARKER (start, w->start);
10657 /* Display. */
10658 clear_glyph_matrix (w->desired_matrix);
10659 XSETWINDOW (window, w);
10660 try_window (window, start, 0);
10662 return window_height_changed_p;
10666 /* Resize the echo area window to exactly the size needed for the
10667 currently displayed message, if there is one. If a mini-buffer
10668 is active, don't shrink it. */
10670 void
10671 resize_echo_area_exactly (void)
10673 if (BUFFERP (echo_area_buffer[0])
10674 && WINDOWP (echo_area_window))
10676 struct window *w = XWINDOW (echo_area_window);
10677 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10678 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10679 (intptr_t) w, resize_exactly);
10680 if (resized_p)
10682 windows_or_buffers_changed = 42;
10683 update_mode_lines = 30;
10684 redisplay_internal ();
10690 /* Callback function for with_echo_area_buffer, when used from
10691 resize_echo_area_exactly. A1 contains a pointer to the window to
10692 resize, EXACTLY non-nil means resize the mini-window exactly to the
10693 size of the text displayed. A3 and A4 are not used. Value is what
10694 resize_mini_window returns. */
10696 static bool
10697 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10699 intptr_t i1 = a1;
10700 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10704 /* Resize mini-window W to fit the size of its contents. EXACT_P
10705 means size the window exactly to the size needed. Otherwise, it's
10706 only enlarged until W's buffer is empty.
10708 Set W->start to the right place to begin display. If the whole
10709 contents fit, start at the beginning. Otherwise, start so as
10710 to make the end of the contents appear. This is particularly
10711 important for y-or-n-p, but seems desirable generally.
10713 Value is true if the window height has been changed. */
10715 bool
10716 resize_mini_window (struct window *w, bool exact_p)
10718 struct frame *f = XFRAME (w->frame);
10719 bool window_height_changed_p = false;
10721 eassert (MINI_WINDOW_P (w));
10723 /* By default, start display at the beginning. */
10724 set_marker_both (w->start, w->contents,
10725 BUF_BEGV (XBUFFER (w->contents)),
10726 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10728 /* Don't resize windows while redisplaying a window; it would
10729 confuse redisplay functions when the size of the window they are
10730 displaying changes from under them. Such a resizing can happen,
10731 for instance, when which-func prints a long message while
10732 we are running fontification-functions. We're running these
10733 functions with safe_call which binds inhibit-redisplay to t. */
10734 if (!NILP (Vinhibit_redisplay))
10735 return false;
10737 /* Nil means don't try to resize. */
10738 if (NILP (Vresize_mini_windows)
10739 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10740 return false;
10742 if (!FRAME_MINIBUF_ONLY_P (f))
10744 struct it it;
10745 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10746 + WINDOW_PIXEL_HEIGHT (w));
10747 int unit = FRAME_LINE_HEIGHT (f);
10748 int height, max_height;
10749 struct text_pos start;
10750 struct buffer *old_current_buffer = NULL;
10752 if (current_buffer != XBUFFER (w->contents))
10754 old_current_buffer = current_buffer;
10755 set_buffer_internal (XBUFFER (w->contents));
10758 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10760 /* Compute the max. number of lines specified by the user. */
10761 if (FLOATP (Vmax_mini_window_height))
10762 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10763 else if (INTEGERP (Vmax_mini_window_height))
10764 max_height = XINT (Vmax_mini_window_height) * unit;
10765 else
10766 max_height = total_height / 4;
10768 /* Correct that max. height if it's bogus. */
10769 max_height = clip_to_bounds (unit, max_height, total_height);
10771 /* Find out the height of the text in the window. */
10772 if (it.line_wrap == TRUNCATE)
10773 height = unit;
10774 else
10776 last_height = 0;
10777 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10778 if (it.max_ascent == 0 && it.max_descent == 0)
10779 height = it.current_y + last_height;
10780 else
10781 height = it.current_y + it.max_ascent + it.max_descent;
10782 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10785 /* Compute a suitable window start. */
10786 if (height > max_height)
10788 height = (max_height / unit) * unit;
10789 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10790 move_it_vertically_backward (&it, height - unit);
10791 start = it.current.pos;
10793 else
10794 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10795 SET_MARKER_FROM_TEXT_POS (w->start, start);
10797 if (EQ (Vresize_mini_windows, Qgrow_only))
10799 /* Let it grow only, until we display an empty message, in which
10800 case the window shrinks again. */
10801 if (height > WINDOW_PIXEL_HEIGHT (w))
10803 int old_height = WINDOW_PIXEL_HEIGHT (w);
10805 FRAME_WINDOWS_FROZEN (f) = true;
10806 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10807 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10809 else if (height < WINDOW_PIXEL_HEIGHT (w)
10810 && (exact_p || BEGV == ZV))
10812 int old_height = WINDOW_PIXEL_HEIGHT (w);
10814 FRAME_WINDOWS_FROZEN (f) = false;
10815 shrink_mini_window (w, true);
10816 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10819 else
10821 /* Always resize to exact size needed. */
10822 if (height > WINDOW_PIXEL_HEIGHT (w))
10824 int old_height = WINDOW_PIXEL_HEIGHT (w);
10826 FRAME_WINDOWS_FROZEN (f) = true;
10827 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10828 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10830 else if (height < WINDOW_PIXEL_HEIGHT (w))
10832 int old_height = WINDOW_PIXEL_HEIGHT (w);
10834 FRAME_WINDOWS_FROZEN (f) = false;
10835 shrink_mini_window (w, true);
10837 if (height)
10839 FRAME_WINDOWS_FROZEN (f) = true;
10840 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10843 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10847 if (old_current_buffer)
10848 set_buffer_internal (old_current_buffer);
10851 return window_height_changed_p;
10855 /* Value is the current message, a string, or nil if there is no
10856 current message. */
10858 Lisp_Object
10859 current_message (void)
10861 Lisp_Object msg;
10863 if (!BUFFERP (echo_area_buffer[0]))
10864 msg = Qnil;
10865 else
10867 with_echo_area_buffer (0, 0, current_message_1,
10868 (intptr_t) &msg, Qnil);
10869 if (NILP (msg))
10870 echo_area_buffer[0] = Qnil;
10873 return msg;
10877 static bool
10878 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10880 intptr_t i1 = a1;
10881 Lisp_Object *msg = (Lisp_Object *) i1;
10883 if (Z > BEG)
10884 *msg = make_buffer_string (BEG, Z, true);
10885 else
10886 *msg = Qnil;
10887 return false;
10891 /* Push the current message on Vmessage_stack for later restoration
10892 by restore_message. Value is true if the current message isn't
10893 empty. This is a relatively infrequent operation, so it's not
10894 worth optimizing. */
10896 bool
10897 push_message (void)
10899 Lisp_Object msg = current_message ();
10900 Vmessage_stack = Fcons (msg, Vmessage_stack);
10901 return STRINGP (msg);
10905 /* Restore message display from the top of Vmessage_stack. */
10907 void
10908 restore_message (void)
10910 eassert (CONSP (Vmessage_stack));
10911 message3_nolog (XCAR (Vmessage_stack));
10915 /* Handler for unwind-protect calling pop_message. */
10917 void
10918 pop_message_unwind (void)
10920 /* Pop the top-most entry off Vmessage_stack. */
10921 eassert (CONSP (Vmessage_stack));
10922 Vmessage_stack = XCDR (Vmessage_stack);
10926 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10927 exits. If the stack is not empty, we have a missing pop_message
10928 somewhere. */
10930 void
10931 check_message_stack (void)
10933 if (!NILP (Vmessage_stack))
10934 emacs_abort ();
10938 /* Truncate to NCHARS what will be displayed in the echo area the next
10939 time we display it---but don't redisplay it now. */
10941 void
10942 truncate_echo_area (ptrdiff_t nchars)
10944 if (nchars == 0)
10945 echo_area_buffer[0] = Qnil;
10946 else if (!noninteractive
10947 && INTERACTIVE
10948 && !NILP (echo_area_buffer[0]))
10950 struct frame *sf = SELECTED_FRAME ();
10951 /* Error messages get reported properly by cmd_error, so this must be
10952 just an informative message; if the frame hasn't really been
10953 initialized yet, just toss it. */
10954 if (sf->glyphs_initialized_p)
10955 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10960 /* Helper function for truncate_echo_area. Truncate the current
10961 message to at most NCHARS characters. */
10963 static bool
10964 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10966 if (BEG + nchars < Z)
10967 del_range (BEG + nchars, Z);
10968 if (Z == BEG)
10969 echo_area_buffer[0] = Qnil;
10970 return false;
10973 /* Set the current message to STRING. */
10975 static void
10976 set_message (Lisp_Object string)
10978 eassert (STRINGP (string));
10980 message_enable_multibyte = STRING_MULTIBYTE (string);
10982 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10983 message_buf_print = false;
10984 help_echo_showing_p = false;
10986 if (STRINGP (Vdebug_on_message)
10987 && STRINGP (string)
10988 && fast_string_match (Vdebug_on_message, string) >= 0)
10989 call_debugger (list2 (Qerror, string));
10993 /* Helper function for set_message. First argument is ignored and second
10994 argument has the same meaning as for set_message.
10995 This function is called with the echo area buffer being current. */
10997 static bool
10998 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11000 eassert (STRINGP (string));
11002 /* Change multibyteness of the echo buffer appropriately. */
11003 if (message_enable_multibyte
11004 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11005 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11007 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11008 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11009 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11011 /* Insert new message at BEG. */
11012 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11014 /* This function takes care of single/multibyte conversion.
11015 We just have to ensure that the echo area buffer has the right
11016 setting of enable_multibyte_characters. */
11017 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11019 return false;
11023 /* Clear messages. CURRENT_P means clear the current message.
11024 LAST_DISPLAYED_P means clear the message last displayed. */
11026 void
11027 clear_message (bool current_p, bool last_displayed_p)
11029 if (current_p)
11031 echo_area_buffer[0] = Qnil;
11032 message_cleared_p = true;
11035 if (last_displayed_p)
11036 echo_area_buffer[1] = Qnil;
11038 message_buf_print = false;
11041 /* Clear garbaged frames.
11043 This function is used where the old redisplay called
11044 redraw_garbaged_frames which in turn called redraw_frame which in
11045 turn called clear_frame. The call to clear_frame was a source of
11046 flickering. I believe a clear_frame is not necessary. It should
11047 suffice in the new redisplay to invalidate all current matrices,
11048 and ensure a complete redisplay of all windows. */
11050 static void
11051 clear_garbaged_frames (void)
11053 if (frame_garbaged)
11055 Lisp_Object tail, frame;
11057 FOR_EACH_FRAME (tail, frame)
11059 struct frame *f = XFRAME (frame);
11061 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11063 if (f->resized_p)
11064 redraw_frame (f);
11065 else
11066 clear_current_matrices (f);
11067 fset_redisplay (f);
11068 f->garbaged = false;
11069 f->resized_p = false;
11073 frame_garbaged = false;
11078 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P,
11079 update selected_frame. Value is true if the mini-windows height
11080 has been changed. */
11082 static bool
11083 echo_area_display (bool update_frame_p)
11085 Lisp_Object mini_window;
11086 struct window *w;
11087 struct frame *f;
11088 bool window_height_changed_p = false;
11089 struct frame *sf = SELECTED_FRAME ();
11091 mini_window = FRAME_MINIBUF_WINDOW (sf);
11092 w = XWINDOW (mini_window);
11093 f = XFRAME (WINDOW_FRAME (w));
11095 /* Don't display if frame is invisible or not yet initialized. */
11096 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11097 return false;
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 /* When Emacs starts, selected_frame may be the initial terminal
11101 frame. If we let this through, a message would be displayed on
11102 the terminal. */
11103 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11104 return false;
11105 #endif /* HAVE_WINDOW_SYSTEM */
11107 /* Redraw garbaged frames. */
11108 clear_garbaged_frames ();
11110 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11112 echo_area_window = mini_window;
11113 window_height_changed_p = display_echo_area (w);
11114 w->must_be_updated_p = true;
11116 /* Update the display, unless called from redisplay_internal.
11117 Also don't update the screen during redisplay itself. The
11118 update will happen at the end of redisplay, and an update
11119 here could cause confusion. */
11120 if (update_frame_p && !redisplaying_p)
11122 int n = 0;
11124 /* If the display update has been interrupted by pending
11125 input, update mode lines in the frame. Due to the
11126 pending input, it might have been that redisplay hasn't
11127 been called, so that mode lines above the echo area are
11128 garbaged. This looks odd, so we prevent it here. */
11129 if (!display_completed)
11130 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11132 if (window_height_changed_p
11133 /* Don't do this if Emacs is shutting down. Redisplay
11134 needs to run hooks. */
11135 && !NILP (Vrun_hooks))
11137 /* Must update other windows. Likewise as in other
11138 cases, don't let this update be interrupted by
11139 pending input. */
11140 ptrdiff_t count = SPECPDL_INDEX ();
11141 specbind (Qredisplay_dont_pause, Qt);
11142 windows_or_buffers_changed = 44;
11143 redisplay_internal ();
11144 unbind_to (count, Qnil);
11146 else if (FRAME_WINDOW_P (f) && n == 0)
11148 /* Window configuration is the same as before.
11149 Can do with a display update of the echo area,
11150 unless we displayed some mode lines. */
11151 update_single_window (w);
11152 flush_frame (f);
11154 else
11155 update_frame (f, true, true);
11157 /* If cursor is in the echo area, make sure that the next
11158 redisplay displays the minibuffer, so that the cursor will
11159 be replaced with what the minibuffer wants. */
11160 if (cursor_in_echo_area)
11161 wset_redisplay (XWINDOW (mini_window));
11164 else if (!EQ (mini_window, selected_window))
11165 wset_redisplay (XWINDOW (mini_window));
11167 /* Last displayed message is now the current message. */
11168 echo_area_buffer[1] = echo_area_buffer[0];
11169 /* Inform read_char that we're not echoing. */
11170 echo_message_buffer = Qnil;
11172 /* Prevent redisplay optimization in redisplay_internal by resetting
11173 this_line_start_pos. This is done because the mini-buffer now
11174 displays the message instead of its buffer text. */
11175 if (EQ (mini_window, selected_window))
11176 CHARPOS (this_line_start_pos) = 0;
11178 return window_height_changed_p;
11181 /* True if W's buffer was changed but not saved. */
11183 static bool
11184 window_buffer_changed (struct window *w)
11186 struct buffer *b = XBUFFER (w->contents);
11188 eassert (BUFFER_LIVE_P (b));
11190 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11193 /* True if W has %c in its mode line and mode line should be updated. */
11195 static bool
11196 mode_line_update_needed (struct window *w)
11198 return (w->column_number_displayed != -1
11199 && !(PT == w->last_point && !window_outdated (w))
11200 && (w->column_number_displayed != current_column ()));
11203 /* True if window start of W is frozen and may not be changed during
11204 redisplay. */
11206 static bool
11207 window_frozen_p (struct window *w)
11209 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11211 Lisp_Object window;
11213 XSETWINDOW (window, w);
11214 if (MINI_WINDOW_P (w))
11215 return false;
11216 else if (EQ (window, selected_window))
11217 return false;
11218 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11219 && EQ (window, Vminibuf_scroll_window))
11220 /* This special window can't be frozen too. */
11221 return false;
11222 else
11223 return true;
11225 return false;
11228 /***********************************************************************
11229 Mode Lines and Frame Titles
11230 ***********************************************************************/
11232 /* A buffer for constructing non-propertized mode-line strings and
11233 frame titles in it; allocated from the heap in init_xdisp and
11234 resized as needed in store_mode_line_noprop_char. */
11236 static char *mode_line_noprop_buf;
11238 /* The buffer's end, and a current output position in it. */
11240 static char *mode_line_noprop_buf_end;
11241 static char *mode_line_noprop_ptr;
11243 #define MODE_LINE_NOPROP_LEN(start) \
11244 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11246 static enum {
11247 MODE_LINE_DISPLAY = 0,
11248 MODE_LINE_TITLE,
11249 MODE_LINE_NOPROP,
11250 MODE_LINE_STRING
11251 } mode_line_target;
11253 /* Alist that caches the results of :propertize.
11254 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11255 static Lisp_Object mode_line_proptrans_alist;
11257 /* List of strings making up the mode-line. */
11258 static Lisp_Object mode_line_string_list;
11260 /* Base face property when building propertized mode line string. */
11261 static Lisp_Object mode_line_string_face;
11262 static Lisp_Object mode_line_string_face_prop;
11265 /* Unwind data for mode line strings */
11267 static Lisp_Object Vmode_line_unwind_vector;
11269 static Lisp_Object
11270 format_mode_line_unwind_data (struct frame *target_frame,
11271 struct buffer *obuf,
11272 Lisp_Object owin,
11273 bool save_proptrans)
11275 Lisp_Object vector, tmp;
11277 /* Reduce consing by keeping one vector in
11278 Vwith_echo_area_save_vector. */
11279 vector = Vmode_line_unwind_vector;
11280 Vmode_line_unwind_vector = Qnil;
11282 if (NILP (vector))
11283 vector = Fmake_vector (make_number (10), Qnil);
11285 ASET (vector, 0, make_number (mode_line_target));
11286 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11287 ASET (vector, 2, mode_line_string_list);
11288 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11289 ASET (vector, 4, mode_line_string_face);
11290 ASET (vector, 5, mode_line_string_face_prop);
11292 if (obuf)
11293 XSETBUFFER (tmp, obuf);
11294 else
11295 tmp = Qnil;
11296 ASET (vector, 6, tmp);
11297 ASET (vector, 7, owin);
11298 if (target_frame)
11300 /* Similarly to `with-selected-window', if the operation selects
11301 a window on another frame, we must restore that frame's
11302 selected window, and (for a tty) the top-frame. */
11303 ASET (vector, 8, target_frame->selected_window);
11304 if (FRAME_TERMCAP_P (target_frame))
11305 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11308 return vector;
11311 static void
11312 unwind_format_mode_line (Lisp_Object vector)
11314 Lisp_Object old_window = AREF (vector, 7);
11315 Lisp_Object target_frame_window = AREF (vector, 8);
11316 Lisp_Object old_top_frame = AREF (vector, 9);
11318 mode_line_target = XINT (AREF (vector, 0));
11319 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11320 mode_line_string_list = AREF (vector, 2);
11321 if (! EQ (AREF (vector, 3), Qt))
11322 mode_line_proptrans_alist = AREF (vector, 3);
11323 mode_line_string_face = AREF (vector, 4);
11324 mode_line_string_face_prop = AREF (vector, 5);
11326 /* Select window before buffer, since it may change the buffer. */
11327 if (!NILP (old_window))
11329 /* If the operation that we are unwinding had selected a window
11330 on a different frame, reset its frame-selected-window. For a
11331 text terminal, reset its top-frame if necessary. */
11332 if (!NILP (target_frame_window))
11334 Lisp_Object frame
11335 = WINDOW_FRAME (XWINDOW (target_frame_window));
11337 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11338 Fselect_window (target_frame_window, Qt);
11340 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11341 Fselect_frame (old_top_frame, Qt);
11344 Fselect_window (old_window, Qt);
11347 if (!NILP (AREF (vector, 6)))
11349 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11350 ASET (vector, 6, Qnil);
11353 Vmode_line_unwind_vector = vector;
11357 /* Store a single character C for the frame title in mode_line_noprop_buf.
11358 Re-allocate mode_line_noprop_buf if necessary. */
11360 static void
11361 store_mode_line_noprop_char (char c)
11363 /* If output position has reached the end of the allocated buffer,
11364 increase the buffer's size. */
11365 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11367 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11368 ptrdiff_t size = len;
11369 mode_line_noprop_buf =
11370 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11371 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11372 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11375 *mode_line_noprop_ptr++ = c;
11379 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11380 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11381 characters that yield more columns than PRECISION; PRECISION <= 0
11382 means copy the whole string. Pad with spaces until FIELD_WIDTH
11383 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11384 pad. Called from display_mode_element when it is used to build a
11385 frame title. */
11387 static int
11388 store_mode_line_noprop (const char *string, int field_width, int precision)
11390 const unsigned char *str = (const unsigned char *) string;
11391 int n = 0;
11392 ptrdiff_t dummy, nbytes;
11394 /* Copy at most PRECISION chars from STR. */
11395 nbytes = strlen (string);
11396 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11397 while (nbytes--)
11398 store_mode_line_noprop_char (*str++);
11400 /* Fill up with spaces until FIELD_WIDTH reached. */
11401 while (field_width > 0
11402 && n < field_width)
11404 store_mode_line_noprop_char (' ');
11405 ++n;
11408 return n;
11411 /***********************************************************************
11412 Frame Titles
11413 ***********************************************************************/
11415 #ifdef HAVE_WINDOW_SYSTEM
11417 /* Set the title of FRAME, if it has changed. The title format is
11418 Vicon_title_format if FRAME is iconified, otherwise it is
11419 frame_title_format. */
11421 static void
11422 x_consider_frame_title (Lisp_Object frame)
11424 struct frame *f = XFRAME (frame);
11426 if (FRAME_WINDOW_P (f)
11427 || FRAME_MINIBUF_ONLY_P (f)
11428 || f->explicit_name)
11430 /* Do we have more than one visible frame on this X display? */
11431 Lisp_Object tail, other_frame, fmt;
11432 ptrdiff_t title_start;
11433 char *title;
11434 ptrdiff_t len;
11435 struct it it;
11436 ptrdiff_t count = SPECPDL_INDEX ();
11438 FOR_EACH_FRAME (tail, other_frame)
11440 struct frame *tf = XFRAME (other_frame);
11442 if (tf != f
11443 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11444 && !FRAME_MINIBUF_ONLY_P (tf)
11445 && !EQ (other_frame, tip_frame)
11446 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11447 break;
11450 /* Set global variable indicating that multiple frames exist. */
11451 multiple_frames = CONSP (tail);
11453 /* Switch to the buffer of selected window of the frame. Set up
11454 mode_line_target so that display_mode_element will output into
11455 mode_line_noprop_buf; then display the title. */
11456 record_unwind_protect (unwind_format_mode_line,
11457 format_mode_line_unwind_data
11458 (f, current_buffer, selected_window, false));
11460 Fselect_window (f->selected_window, Qt);
11461 set_buffer_internal_1
11462 (XBUFFER (XWINDOW (f->selected_window)->contents));
11463 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11465 mode_line_target = MODE_LINE_TITLE;
11466 title_start = MODE_LINE_NOPROP_LEN (0);
11467 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11468 NULL, DEFAULT_FACE_ID);
11469 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11470 len = MODE_LINE_NOPROP_LEN (title_start);
11471 title = mode_line_noprop_buf + title_start;
11472 unbind_to (count, Qnil);
11474 /* Set the title only if it's changed. This avoids consing in
11475 the common case where it hasn't. (If it turns out that we've
11476 already wasted too much time by walking through the list with
11477 display_mode_element, then we might need to optimize at a
11478 higher level than this.) */
11479 if (! STRINGP (f->name)
11480 || SBYTES (f->name) != len
11481 || memcmp (title, SDATA (f->name), len) != 0)
11482 x_implicitly_set_name (f, make_string (title, len), Qnil);
11486 #endif /* not HAVE_WINDOW_SYSTEM */
11489 /***********************************************************************
11490 Menu Bars
11491 ***********************************************************************/
11493 /* True if we will not redisplay all visible windows. */
11494 #define REDISPLAY_SOME_P() \
11495 ((windows_or_buffers_changed == 0 \
11496 || windows_or_buffers_changed == REDISPLAY_SOME) \
11497 && (update_mode_lines == 0 \
11498 || update_mode_lines == REDISPLAY_SOME))
11500 /* Prepare for redisplay by updating menu-bar item lists when
11501 appropriate. This can call eval. */
11503 static void
11504 prepare_menu_bars (void)
11506 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11507 bool some_windows = REDISPLAY_SOME_P ();
11508 struct gcpro gcpro1, gcpro2;
11509 Lisp_Object tooltip_frame;
11511 #ifdef HAVE_WINDOW_SYSTEM
11512 tooltip_frame = tip_frame;
11513 #else
11514 tooltip_frame = Qnil;
11515 #endif
11517 if (FUNCTIONP (Vpre_redisplay_function))
11519 Lisp_Object windows = all_windows ? Qt : Qnil;
11520 if (all_windows && some_windows)
11522 Lisp_Object ws = window_list ();
11523 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11525 Lisp_Object this = XCAR (ws);
11526 struct window *w = XWINDOW (this);
11527 if (w->redisplay
11528 || XFRAME (w->frame)->redisplay
11529 || XBUFFER (w->contents)->text->redisplay)
11531 windows = Fcons (this, windows);
11535 safe__call1 (true, Vpre_redisplay_function, windows);
11538 /* Update all frame titles based on their buffer names, etc. We do
11539 this before the menu bars so that the buffer-menu will show the
11540 up-to-date frame titles. */
11541 #ifdef HAVE_WINDOW_SYSTEM
11542 if (all_windows)
11544 Lisp_Object tail, frame;
11546 FOR_EACH_FRAME (tail, frame)
11548 struct frame *f = XFRAME (frame);
11549 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11550 if (some_windows
11551 && !f->redisplay
11552 && !w->redisplay
11553 && !XBUFFER (w->contents)->text->redisplay)
11554 continue;
11556 if (!EQ (frame, tooltip_frame)
11557 && (FRAME_ICONIFIED_P (f)
11558 || FRAME_VISIBLE_P (f) == 1
11559 /* Exclude TTY frames that are obscured because they
11560 are not the top frame on their console. This is
11561 because x_consider_frame_title actually switches
11562 to the frame, which for TTY frames means it is
11563 marked as garbaged, and will be completely
11564 redrawn on the next redisplay cycle. This causes
11565 TTY frames to be completely redrawn, when there
11566 are more than one of them, even though nothing
11567 should be changed on display. */
11568 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11569 x_consider_frame_title (frame);
11572 #endif /* HAVE_WINDOW_SYSTEM */
11574 /* Update the menu bar item lists, if appropriate. This has to be
11575 done before any actual redisplay or generation of display lines. */
11577 if (all_windows)
11579 Lisp_Object tail, frame;
11580 ptrdiff_t count = SPECPDL_INDEX ();
11581 /* True means that update_menu_bar has run its hooks
11582 so any further calls to update_menu_bar shouldn't do so again. */
11583 bool menu_bar_hooks_run = false;
11585 record_unwind_save_match_data ();
11587 FOR_EACH_FRAME (tail, frame)
11589 struct frame *f = XFRAME (frame);
11590 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11592 /* Ignore tooltip frame. */
11593 if (EQ (frame, tooltip_frame))
11594 continue;
11596 if (some_windows
11597 && !f->redisplay
11598 && !w->redisplay
11599 && !XBUFFER (w->contents)->text->redisplay)
11600 continue;
11602 /* If a window on this frame changed size, report that to
11603 the user and clear the size-change flag. */
11604 if (FRAME_WINDOW_SIZES_CHANGED (f))
11606 Lisp_Object functions;
11608 /* Clear flag first in case we get an error below. */
11609 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11610 functions = Vwindow_size_change_functions;
11611 GCPRO2 (tail, functions);
11613 while (CONSP (functions))
11615 if (!EQ (XCAR (functions), Qt))
11616 call1 (XCAR (functions), frame);
11617 functions = XCDR (functions);
11619 UNGCPRO;
11622 GCPRO1 (tail);
11623 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11624 #ifdef HAVE_WINDOW_SYSTEM
11625 update_tool_bar (f, false);
11626 #endif
11627 UNGCPRO;
11630 unbind_to (count, Qnil);
11632 else
11634 struct frame *sf = SELECTED_FRAME ();
11635 update_menu_bar (sf, true, false);
11636 #ifdef HAVE_WINDOW_SYSTEM
11637 update_tool_bar (sf, true);
11638 #endif
11643 /* Update the menu bar item list for frame F. This has to be done
11644 before we start to fill in any display lines, because it can call
11645 eval.
11647 If SAVE_MATCH_DATA, we must save and restore it here.
11649 If HOOKS_RUN, a previous call to update_menu_bar
11650 already ran the menu bar hooks for this redisplay, so there
11651 is no need to run them again. The return value is the
11652 updated value of this flag, to pass to the next call. */
11654 static bool
11655 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11657 Lisp_Object window;
11658 struct window *w;
11660 /* If called recursively during a menu update, do nothing. This can
11661 happen when, for instance, an activate-menubar-hook causes a
11662 redisplay. */
11663 if (inhibit_menubar_update)
11664 return hooks_run;
11666 window = FRAME_SELECTED_WINDOW (f);
11667 w = XWINDOW (window);
11669 if (FRAME_WINDOW_P (f)
11671 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11672 || defined (HAVE_NS) || defined (USE_GTK)
11673 FRAME_EXTERNAL_MENU_BAR (f)
11674 #else
11675 FRAME_MENU_BAR_LINES (f) > 0
11676 #endif
11677 : FRAME_MENU_BAR_LINES (f) > 0)
11679 /* If the user has switched buffers or windows, we need to
11680 recompute to reflect the new bindings. But we'll
11681 recompute when update_mode_lines is set too; that means
11682 that people can use force-mode-line-update to request
11683 that the menu bar be recomputed. The adverse effect on
11684 the rest of the redisplay algorithm is about the same as
11685 windows_or_buffers_changed anyway. */
11686 if (windows_or_buffers_changed
11687 /* This used to test w->update_mode_line, but we believe
11688 there is no need to recompute the menu in that case. */
11689 || update_mode_lines
11690 || window_buffer_changed (w))
11692 struct buffer *prev = current_buffer;
11693 ptrdiff_t count = SPECPDL_INDEX ();
11695 specbind (Qinhibit_menubar_update, Qt);
11697 set_buffer_internal_1 (XBUFFER (w->contents));
11698 if (save_match_data)
11699 record_unwind_save_match_data ();
11700 if (NILP (Voverriding_local_map_menu_flag))
11702 specbind (Qoverriding_terminal_local_map, Qnil);
11703 specbind (Qoverriding_local_map, Qnil);
11706 if (!hooks_run)
11708 /* Run the Lucid hook. */
11709 safe_run_hooks (Qactivate_menubar_hook);
11711 /* If it has changed current-menubar from previous value,
11712 really recompute the menu-bar from the value. */
11713 if (! NILP (Vlucid_menu_bar_dirty_flag))
11714 call0 (Qrecompute_lucid_menubar);
11716 safe_run_hooks (Qmenu_bar_update_hook);
11718 hooks_run = true;
11721 XSETFRAME (Vmenu_updating_frame, f);
11722 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11724 /* Redisplay the menu bar in case we changed it. */
11725 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11726 || defined (HAVE_NS) || defined (USE_GTK)
11727 if (FRAME_WINDOW_P (f))
11729 #if defined (HAVE_NS)
11730 /* All frames on Mac OS share the same menubar. So only
11731 the selected frame should be allowed to set it. */
11732 if (f == SELECTED_FRAME ())
11733 #endif
11734 set_frame_menubar (f, false, false);
11736 else
11737 /* On a terminal screen, the menu bar is an ordinary screen
11738 line, and this makes it get updated. */
11739 w->update_mode_line = true;
11740 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11741 /* In the non-toolkit version, the menu bar is an ordinary screen
11742 line, and this makes it get updated. */
11743 w->update_mode_line = true;
11744 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11746 unbind_to (count, Qnil);
11747 set_buffer_internal_1 (prev);
11751 return hooks_run;
11754 /***********************************************************************
11755 Tool-bars
11756 ***********************************************************************/
11758 #ifdef HAVE_WINDOW_SYSTEM
11760 /* Select `frame' temporarily without running all the code in
11761 do_switch_frame.
11762 FIXME: Maybe do_switch_frame should be trimmed down similarly
11763 when `norecord' is set. */
11764 static void
11765 fast_set_selected_frame (Lisp_Object frame)
11767 if (!EQ (selected_frame, frame))
11769 selected_frame = frame;
11770 selected_window = XFRAME (frame)->selected_window;
11774 /* Update the tool-bar item list for frame F. This has to be done
11775 before we start to fill in any display lines. Called from
11776 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11777 and restore it here. */
11779 static void
11780 update_tool_bar (struct frame *f, bool save_match_data)
11782 #if defined (USE_GTK) || defined (HAVE_NS)
11783 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11784 #else
11785 bool do_update = (WINDOWP (f->tool_bar_window)
11786 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11787 #endif
11789 if (do_update)
11791 Lisp_Object window;
11792 struct window *w;
11794 window = FRAME_SELECTED_WINDOW (f);
11795 w = XWINDOW (window);
11797 /* If the user has switched buffers or windows, we need to
11798 recompute to reflect the new bindings. But we'll
11799 recompute when update_mode_lines is set too; that means
11800 that people can use force-mode-line-update to request
11801 that the menu bar be recomputed. The adverse effect on
11802 the rest of the redisplay algorithm is about the same as
11803 windows_or_buffers_changed anyway. */
11804 if (windows_or_buffers_changed
11805 || w->update_mode_line
11806 || update_mode_lines
11807 || window_buffer_changed (w))
11809 struct buffer *prev = current_buffer;
11810 ptrdiff_t count = SPECPDL_INDEX ();
11811 Lisp_Object frame, new_tool_bar;
11812 int new_n_tool_bar;
11813 struct gcpro gcpro1;
11815 /* Set current_buffer to the buffer of the selected
11816 window of the frame, so that we get the right local
11817 keymaps. */
11818 set_buffer_internal_1 (XBUFFER (w->contents));
11820 /* Save match data, if we must. */
11821 if (save_match_data)
11822 record_unwind_save_match_data ();
11824 /* Make sure that we don't accidentally use bogus keymaps. */
11825 if (NILP (Voverriding_local_map_menu_flag))
11827 specbind (Qoverriding_terminal_local_map, Qnil);
11828 specbind (Qoverriding_local_map, Qnil);
11831 GCPRO1 (new_tool_bar);
11833 /* We must temporarily set the selected frame to this frame
11834 before calling tool_bar_items, because the calculation of
11835 the tool-bar keymap uses the selected frame (see
11836 `tool-bar-make-keymap' in tool-bar.el). */
11837 eassert (EQ (selected_window,
11838 /* Since we only explicitly preserve selected_frame,
11839 check that selected_window would be redundant. */
11840 XFRAME (selected_frame)->selected_window));
11841 record_unwind_protect (fast_set_selected_frame, selected_frame);
11842 XSETFRAME (frame, f);
11843 fast_set_selected_frame (frame);
11845 /* Build desired tool-bar items from keymaps. */
11846 new_tool_bar
11847 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11848 &new_n_tool_bar);
11850 /* Redisplay the tool-bar if we changed it. */
11851 if (new_n_tool_bar != f->n_tool_bar_items
11852 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11854 /* Redisplay that happens asynchronously due to an expose event
11855 may access f->tool_bar_items. Make sure we update both
11856 variables within BLOCK_INPUT so no such event interrupts. */
11857 block_input ();
11858 fset_tool_bar_items (f, new_tool_bar);
11859 f->n_tool_bar_items = new_n_tool_bar;
11860 w->update_mode_line = true;
11861 unblock_input ();
11864 UNGCPRO;
11866 unbind_to (count, Qnil);
11867 set_buffer_internal_1 (prev);
11872 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11874 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11875 F's desired tool-bar contents. F->tool_bar_items must have
11876 been set up previously by calling prepare_menu_bars. */
11878 static void
11879 build_desired_tool_bar_string (struct frame *f)
11881 int i, size, size_needed;
11882 struct gcpro gcpro1, gcpro2;
11883 Lisp_Object image, plist;
11885 image = plist = Qnil;
11886 GCPRO2 (image, plist);
11888 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11889 Otherwise, make a new string. */
11891 /* The size of the string we might be able to reuse. */
11892 size = (STRINGP (f->desired_tool_bar_string)
11893 ? SCHARS (f->desired_tool_bar_string)
11894 : 0);
11896 /* We need one space in the string for each image. */
11897 size_needed = f->n_tool_bar_items;
11899 /* Reuse f->desired_tool_bar_string, if possible. */
11900 if (size < size_needed || NILP (f->desired_tool_bar_string))
11901 fset_desired_tool_bar_string
11902 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11903 else
11905 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11906 struct gcpro gcpro1;
11907 GCPRO1 (props);
11908 Fremove_text_properties (make_number (0), make_number (size),
11909 props, f->desired_tool_bar_string);
11910 UNGCPRO;
11913 /* Put a `display' property on the string for the images to display,
11914 put a `menu_item' property on tool-bar items with a value that
11915 is the index of the item in F's tool-bar item vector. */
11916 for (i = 0; i < f->n_tool_bar_items; ++i)
11918 #define PROP(IDX) \
11919 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11921 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11922 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11923 int hmargin, vmargin, relief, idx, end;
11925 /* If image is a vector, choose the image according to the
11926 button state. */
11927 image = PROP (TOOL_BAR_ITEM_IMAGES);
11928 if (VECTORP (image))
11930 if (enabled_p)
11931 idx = (selected_p
11932 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11933 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11934 else
11935 idx = (selected_p
11936 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11937 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11939 eassert (ASIZE (image) >= idx);
11940 image = AREF (image, idx);
11942 else
11943 idx = -1;
11945 /* Ignore invalid image specifications. */
11946 if (!valid_image_p (image))
11947 continue;
11949 /* Display the tool-bar button pressed, or depressed. */
11950 plist = Fcopy_sequence (XCDR (image));
11952 /* Compute margin and relief to draw. */
11953 relief = (tool_bar_button_relief >= 0
11954 ? tool_bar_button_relief
11955 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11956 hmargin = vmargin = relief;
11958 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11959 INT_MAX - max (hmargin, vmargin)))
11961 hmargin += XFASTINT (Vtool_bar_button_margin);
11962 vmargin += XFASTINT (Vtool_bar_button_margin);
11964 else if (CONSP (Vtool_bar_button_margin))
11966 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11967 INT_MAX - hmargin))
11968 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11970 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11971 INT_MAX - vmargin))
11972 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11975 if (auto_raise_tool_bar_buttons_p)
11977 /* Add a `:relief' property to the image spec if the item is
11978 selected. */
11979 if (selected_p)
11981 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11982 hmargin -= relief;
11983 vmargin -= relief;
11986 else
11988 /* If image is selected, display it pressed, i.e. with a
11989 negative relief. If it's not selected, display it with a
11990 raised relief. */
11991 plist = Fplist_put (plist, QCrelief,
11992 (selected_p
11993 ? make_number (-relief)
11994 : make_number (relief)));
11995 hmargin -= relief;
11996 vmargin -= relief;
11999 /* Put a margin around the image. */
12000 if (hmargin || vmargin)
12002 if (hmargin == vmargin)
12003 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12004 else
12005 plist = Fplist_put (plist, QCmargin,
12006 Fcons (make_number (hmargin),
12007 make_number (vmargin)));
12010 /* If button is not enabled, and we don't have special images
12011 for the disabled state, make the image appear disabled by
12012 applying an appropriate algorithm to it. */
12013 if (!enabled_p && idx < 0)
12014 plist = Fplist_put (plist, QCconversion, Qdisabled);
12016 /* Put a `display' text property on the string for the image to
12017 display. Put a `menu-item' property on the string that gives
12018 the start of this item's properties in the tool-bar items
12019 vector. */
12020 image = Fcons (Qimage, plist);
12021 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12022 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12023 struct gcpro gcpro1;
12024 GCPRO1 (props);
12026 /* Let the last image hide all remaining spaces in the tool bar
12027 string. The string can be longer than needed when we reuse a
12028 previous string. */
12029 if (i + 1 == f->n_tool_bar_items)
12030 end = SCHARS (f->desired_tool_bar_string);
12031 else
12032 end = i + 1;
12033 Fadd_text_properties (make_number (i), make_number (end),
12034 props, f->desired_tool_bar_string);
12035 UNGCPRO;
12036 #undef PROP
12039 UNGCPRO;
12043 /* Display one line of the tool-bar of frame IT->f.
12045 HEIGHT specifies the desired height of the tool-bar line.
12046 If the actual height of the glyph row is less than HEIGHT, the
12047 row's height is increased to HEIGHT, and the icons are centered
12048 vertically in the new height.
12050 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12051 count a final empty row in case the tool-bar width exactly matches
12052 the window width.
12055 static void
12056 display_tool_bar_line (struct it *it, int height)
12058 struct glyph_row *row = it->glyph_row;
12059 int max_x = it->last_visible_x;
12060 struct glyph *last;
12062 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12063 clear_glyph_row (row);
12064 row->enabled_p = true;
12065 row->y = it->current_y;
12067 /* Note that this isn't made use of if the face hasn't a box,
12068 so there's no need to check the face here. */
12069 it->start_of_box_run_p = true;
12071 while (it->current_x < max_x)
12073 int x, n_glyphs_before, i, nglyphs;
12074 struct it it_before;
12076 /* Get the next display element. */
12077 if (!get_next_display_element (it))
12079 /* Don't count empty row if we are counting needed tool-bar lines. */
12080 if (height < 0 && !it->hpos)
12081 return;
12082 break;
12085 /* Produce glyphs. */
12086 n_glyphs_before = row->used[TEXT_AREA];
12087 it_before = *it;
12089 PRODUCE_GLYPHS (it);
12091 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12092 i = 0;
12093 x = it_before.current_x;
12094 while (i < nglyphs)
12096 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12098 if (x + glyph->pixel_width > max_x)
12100 /* Glyph doesn't fit on line. Backtrack. */
12101 row->used[TEXT_AREA] = n_glyphs_before;
12102 *it = it_before;
12103 /* If this is the only glyph on this line, it will never fit on the
12104 tool-bar, so skip it. But ensure there is at least one glyph,
12105 so we don't accidentally disable the tool-bar. */
12106 if (n_glyphs_before == 0
12107 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12108 break;
12109 goto out;
12112 ++it->hpos;
12113 x += glyph->pixel_width;
12114 ++i;
12117 /* Stop at line end. */
12118 if (ITERATOR_AT_END_OF_LINE_P (it))
12119 break;
12121 set_iterator_to_next (it, true);
12124 out:;
12126 row->displays_text_p = row->used[TEXT_AREA] != 0;
12128 /* Use default face for the border below the tool bar.
12130 FIXME: When auto-resize-tool-bars is grow-only, there is
12131 no additional border below the possibly empty tool-bar lines.
12132 So to make the extra empty lines look "normal", we have to
12133 use the tool-bar face for the border too. */
12134 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12135 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12136 it->face_id = DEFAULT_FACE_ID;
12138 extend_face_to_end_of_line (it);
12139 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12140 last->right_box_line_p = true;
12141 if (last == row->glyphs[TEXT_AREA])
12142 last->left_box_line_p = true;
12144 /* Make line the desired height and center it vertically. */
12145 if ((height -= it->max_ascent + it->max_descent) > 0)
12147 /* Don't add more than one line height. */
12148 height %= FRAME_LINE_HEIGHT (it->f);
12149 it->max_ascent += height / 2;
12150 it->max_descent += (height + 1) / 2;
12153 compute_line_metrics (it);
12155 /* If line is empty, make it occupy the rest of the tool-bar. */
12156 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12158 row->height = row->phys_height = it->last_visible_y - row->y;
12159 row->visible_height = row->height;
12160 row->ascent = row->phys_ascent = 0;
12161 row->extra_line_spacing = 0;
12164 row->full_width_p = true;
12165 row->continued_p = false;
12166 row->truncated_on_left_p = false;
12167 row->truncated_on_right_p = false;
12169 it->current_x = it->hpos = 0;
12170 it->current_y += row->height;
12171 ++it->vpos;
12172 ++it->glyph_row;
12176 /* Value is the number of pixels needed to make all tool-bar items of
12177 frame F visible. The actual number of glyph rows needed is
12178 returned in *N_ROWS if non-NULL. */
12179 static int
12180 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12182 struct window *w = XWINDOW (f->tool_bar_window);
12183 struct it it;
12184 /* tool_bar_height is called from redisplay_tool_bar after building
12185 the desired matrix, so use (unused) mode-line row as temporary row to
12186 avoid destroying the first tool-bar row. */
12187 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12189 /* Initialize an iterator for iteration over
12190 F->desired_tool_bar_string in the tool-bar window of frame F. */
12191 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12192 temp_row->reversed_p = false;
12193 it.first_visible_x = 0;
12194 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12195 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12196 it.paragraph_embedding = L2R;
12198 while (!ITERATOR_AT_END_P (&it))
12200 clear_glyph_row (temp_row);
12201 it.glyph_row = temp_row;
12202 display_tool_bar_line (&it, -1);
12204 clear_glyph_row (temp_row);
12206 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12207 if (n_rows)
12208 *n_rows = it.vpos > 0 ? it.vpos : -1;
12210 if (pixelwise)
12211 return it.current_y;
12212 else
12213 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12216 #endif /* !USE_GTK && !HAVE_NS */
12218 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12219 0, 2, 0,
12220 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12221 If FRAME is nil or omitted, use the selected frame. Optional argument
12222 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12223 (Lisp_Object frame, Lisp_Object pixelwise)
12225 int height = 0;
12227 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12228 struct frame *f = decode_any_frame (frame);
12230 if (WINDOWP (f->tool_bar_window)
12231 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12233 update_tool_bar (f, true);
12234 if (f->n_tool_bar_items)
12236 build_desired_tool_bar_string (f);
12237 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12240 #endif
12242 return make_number (height);
12246 /* Display the tool-bar of frame F. Value is true if tool-bar's
12247 height should be changed. */
12248 static bool
12249 redisplay_tool_bar (struct frame *f)
12251 #if defined (USE_GTK) || defined (HAVE_NS)
12253 if (FRAME_EXTERNAL_TOOL_BAR (f))
12254 update_frame_tool_bar (f);
12255 return false;
12257 #else /* !USE_GTK && !HAVE_NS */
12259 struct window *w;
12260 struct it it;
12261 struct glyph_row *row;
12263 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12264 do anything. This means you must start with tool-bar-lines
12265 non-zero to get the auto-sizing effect. Or in other words, you
12266 can turn off tool-bars by specifying tool-bar-lines zero. */
12267 if (!WINDOWP (f->tool_bar_window)
12268 || (w = XWINDOW (f->tool_bar_window),
12269 WINDOW_TOTAL_LINES (w) == 0))
12270 return false;
12272 /* Set up an iterator for the tool-bar window. */
12273 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12274 it.first_visible_x = 0;
12275 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12276 row = it.glyph_row;
12277 row->reversed_p = false;
12279 /* Build a string that represents the contents of the tool-bar. */
12280 build_desired_tool_bar_string (f);
12281 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12282 /* FIXME: This should be controlled by a user option. But it
12283 doesn't make sense to have an R2L tool bar if the menu bar cannot
12284 be drawn also R2L, and making the menu bar R2L is tricky due
12285 toolkit-specific code that implements it. If an R2L tool bar is
12286 ever supported, display_tool_bar_line should also be augmented to
12287 call unproduce_glyphs like display_line and display_string
12288 do. */
12289 it.paragraph_embedding = L2R;
12291 if (f->n_tool_bar_rows == 0)
12293 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12295 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12297 x_change_tool_bar_height (f, new_height);
12298 frame_default_tool_bar_height = new_height;
12299 /* Always do that now. */
12300 clear_glyph_matrix (w->desired_matrix);
12301 f->fonts_changed = true;
12302 return true;
12306 /* Display as many lines as needed to display all tool-bar items. */
12308 if (f->n_tool_bar_rows > 0)
12310 int border, rows, height, extra;
12312 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12313 border = XINT (Vtool_bar_border);
12314 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12315 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12316 else if (EQ (Vtool_bar_border, Qborder_width))
12317 border = f->border_width;
12318 else
12319 border = 0;
12320 if (border < 0)
12321 border = 0;
12323 rows = f->n_tool_bar_rows;
12324 height = max (1, (it.last_visible_y - border) / rows);
12325 extra = it.last_visible_y - border - height * rows;
12327 while (it.current_y < it.last_visible_y)
12329 int h = 0;
12330 if (extra > 0 && rows-- > 0)
12332 h = (extra + rows - 1) / rows;
12333 extra -= h;
12335 display_tool_bar_line (&it, height + h);
12338 else
12340 while (it.current_y < it.last_visible_y)
12341 display_tool_bar_line (&it, 0);
12344 /* It doesn't make much sense to try scrolling in the tool-bar
12345 window, so don't do it. */
12346 w->desired_matrix->no_scrolling_p = true;
12347 w->must_be_updated_p = true;
12349 if (!NILP (Vauto_resize_tool_bars))
12351 bool change_height_p = true;
12353 /* If we couldn't display everything, change the tool-bar's
12354 height if there is room for more. */
12355 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12356 change_height_p = true;
12358 /* We subtract 1 because display_tool_bar_line advances the
12359 glyph_row pointer before returning to its caller. We want to
12360 examine the last glyph row produced by
12361 display_tool_bar_line. */
12362 row = it.glyph_row - 1;
12364 /* If there are blank lines at the end, except for a partially
12365 visible blank line at the end that is smaller than
12366 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12367 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12368 && row->height >= FRAME_LINE_HEIGHT (f))
12369 change_height_p = true;
12371 /* If row displays tool-bar items, but is partially visible,
12372 change the tool-bar's height. */
12373 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12374 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12375 change_height_p = true;
12377 /* Resize windows as needed by changing the `tool-bar-lines'
12378 frame parameter. */
12379 if (change_height_p)
12381 int nrows;
12382 int new_height = tool_bar_height (f, &nrows, true);
12384 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12385 && !f->minimize_tool_bar_window_p)
12386 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12387 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12388 f->minimize_tool_bar_window_p = false;
12390 if (change_height_p)
12392 x_change_tool_bar_height (f, new_height);
12393 frame_default_tool_bar_height = new_height;
12394 clear_glyph_matrix (w->desired_matrix);
12395 f->n_tool_bar_rows = nrows;
12396 f->fonts_changed = true;
12398 return true;
12403 f->minimize_tool_bar_window_p = false;
12404 return false;
12406 #endif /* USE_GTK || HAVE_NS */
12409 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12411 /* Get information about the tool-bar item which is displayed in GLYPH
12412 on frame F. Return in *PROP_IDX the index where tool-bar item
12413 properties start in F->tool_bar_items. Value is false if
12414 GLYPH doesn't display a tool-bar item. */
12416 static bool
12417 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12419 Lisp_Object prop;
12420 int charpos;
12422 /* This function can be called asynchronously, which means we must
12423 exclude any possibility that Fget_text_property signals an
12424 error. */
12425 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12426 charpos = max (0, charpos);
12428 /* Get the text property `menu-item' at pos. The value of that
12429 property is the start index of this item's properties in
12430 F->tool_bar_items. */
12431 prop = Fget_text_property (make_number (charpos),
12432 Qmenu_item, f->current_tool_bar_string);
12433 if (! INTEGERP (prop))
12434 return false;
12435 *prop_idx = XINT (prop);
12436 return true;
12440 /* Get information about the tool-bar item at position X/Y on frame F.
12441 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12442 the current matrix of the tool-bar window of F, or NULL if not
12443 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12444 item in F->tool_bar_items. Value is
12446 -1 if X/Y is not on a tool-bar item
12447 0 if X/Y is on the same item that was highlighted before.
12448 1 otherwise. */
12450 static int
12451 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12452 int *hpos, int *vpos, int *prop_idx)
12454 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12455 struct window *w = XWINDOW (f->tool_bar_window);
12456 int area;
12458 /* Find the glyph under X/Y. */
12459 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12460 if (*glyph == NULL)
12461 return -1;
12463 /* Get the start of this tool-bar item's properties in
12464 f->tool_bar_items. */
12465 if (!tool_bar_item_info (f, *glyph, prop_idx))
12466 return -1;
12468 /* Is mouse on the highlighted item? */
12469 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12470 && *vpos >= hlinfo->mouse_face_beg_row
12471 && *vpos <= hlinfo->mouse_face_end_row
12472 && (*vpos > hlinfo->mouse_face_beg_row
12473 || *hpos >= hlinfo->mouse_face_beg_col)
12474 && (*vpos < hlinfo->mouse_face_end_row
12475 || *hpos < hlinfo->mouse_face_end_col
12476 || hlinfo->mouse_face_past_end))
12477 return 0;
12479 return 1;
12483 /* EXPORT:
12484 Handle mouse button event on the tool-bar of frame F, at
12485 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12486 false for button release. MODIFIERS is event modifiers for button
12487 release. */
12489 void
12490 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12491 int modifiers)
12493 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12494 struct window *w = XWINDOW (f->tool_bar_window);
12495 int hpos, vpos, prop_idx;
12496 struct glyph *glyph;
12497 Lisp_Object enabled_p;
12498 int ts;
12500 /* If not on the highlighted tool-bar item, and mouse-highlight is
12501 non-nil, return. This is so we generate the tool-bar button
12502 click only when the mouse button is released on the same item as
12503 where it was pressed. However, when mouse-highlight is disabled,
12504 generate the click when the button is released regardless of the
12505 highlight, since tool-bar items are not highlighted in that
12506 case. */
12507 frame_to_window_pixel_xy (w, &x, &y);
12508 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12509 if (ts == -1
12510 || (ts != 0 && !NILP (Vmouse_highlight)))
12511 return;
12513 /* When mouse-highlight is off, generate the click for the item
12514 where the button was pressed, disregarding where it was
12515 released. */
12516 if (NILP (Vmouse_highlight) && !down_p)
12517 prop_idx = f->last_tool_bar_item;
12519 /* If item is disabled, do nothing. */
12520 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12521 if (NILP (enabled_p))
12522 return;
12524 if (down_p)
12526 /* Show item in pressed state. */
12527 if (!NILP (Vmouse_highlight))
12528 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12529 f->last_tool_bar_item = prop_idx;
12531 else
12533 Lisp_Object key, frame;
12534 struct input_event event;
12535 EVENT_INIT (event);
12537 /* Show item in released state. */
12538 if (!NILP (Vmouse_highlight))
12539 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12541 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12543 XSETFRAME (frame, f);
12544 event.kind = TOOL_BAR_EVENT;
12545 event.frame_or_window = frame;
12546 event.arg = frame;
12547 kbd_buffer_store_event (&event);
12549 event.kind = TOOL_BAR_EVENT;
12550 event.frame_or_window = frame;
12551 event.arg = key;
12552 event.modifiers = modifiers;
12553 kbd_buffer_store_event (&event);
12554 f->last_tool_bar_item = -1;
12559 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12560 tool-bar window-relative coordinates X/Y. Called from
12561 note_mouse_highlight. */
12563 static void
12564 note_tool_bar_highlight (struct frame *f, int x, int y)
12566 Lisp_Object window = f->tool_bar_window;
12567 struct window *w = XWINDOW (window);
12568 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12569 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12570 int hpos, vpos;
12571 struct glyph *glyph;
12572 struct glyph_row *row;
12573 int i;
12574 Lisp_Object enabled_p;
12575 int prop_idx;
12576 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12577 bool mouse_down_p;
12578 int rc;
12580 /* Function note_mouse_highlight is called with negative X/Y
12581 values when mouse moves outside of the frame. */
12582 if (x <= 0 || y <= 0)
12584 clear_mouse_face (hlinfo);
12585 return;
12588 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12589 if (rc < 0)
12591 /* Not on tool-bar item. */
12592 clear_mouse_face (hlinfo);
12593 return;
12595 else if (rc == 0)
12596 /* On same tool-bar item as before. */
12597 goto set_help_echo;
12599 clear_mouse_face (hlinfo);
12601 /* Mouse is down, but on different tool-bar item? */
12602 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12603 && f == dpyinfo->last_mouse_frame);
12605 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12606 return;
12608 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12610 /* If tool-bar item is not enabled, don't highlight it. */
12611 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12612 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12614 /* Compute the x-position of the glyph. In front and past the
12615 image is a space. We include this in the highlighted area. */
12616 row = MATRIX_ROW (w->current_matrix, vpos);
12617 for (i = x = 0; i < hpos; ++i)
12618 x += row->glyphs[TEXT_AREA][i].pixel_width;
12620 /* Record this as the current active region. */
12621 hlinfo->mouse_face_beg_col = hpos;
12622 hlinfo->mouse_face_beg_row = vpos;
12623 hlinfo->mouse_face_beg_x = x;
12624 hlinfo->mouse_face_past_end = false;
12626 hlinfo->mouse_face_end_col = hpos + 1;
12627 hlinfo->mouse_face_end_row = vpos;
12628 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12629 hlinfo->mouse_face_window = window;
12630 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12632 /* Display it as active. */
12633 show_mouse_face (hlinfo, draw);
12636 set_help_echo:
12638 /* Set help_echo_string to a help string to display for this tool-bar item.
12639 XTread_socket does the rest. */
12640 help_echo_object = help_echo_window = Qnil;
12641 help_echo_pos = -1;
12642 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12643 if (NILP (help_echo_string))
12644 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12647 #endif /* !USE_GTK && !HAVE_NS */
12649 #endif /* HAVE_WINDOW_SYSTEM */
12653 /************************************************************************
12654 Horizontal scrolling
12655 ************************************************************************/
12657 /* For all leaf windows in the window tree rooted at WINDOW, set their
12658 hscroll value so that PT is (i) visible in the window, and (ii) so
12659 that it is not within a certain margin at the window's left and
12660 right border. Value is true if any window's hscroll has been
12661 changed. */
12663 static bool
12664 hscroll_window_tree (Lisp_Object window)
12666 bool hscrolled_p = false;
12667 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12668 int hscroll_step_abs = 0;
12669 double hscroll_step_rel = 0;
12671 if (hscroll_relative_p)
12673 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12674 if (hscroll_step_rel < 0)
12676 hscroll_relative_p = false;
12677 hscroll_step_abs = 0;
12680 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12682 hscroll_step_abs = XINT (Vhscroll_step);
12683 if (hscroll_step_abs < 0)
12684 hscroll_step_abs = 0;
12686 else
12687 hscroll_step_abs = 0;
12689 while (WINDOWP (window))
12691 struct window *w = XWINDOW (window);
12693 if (WINDOWP (w->contents))
12694 hscrolled_p |= hscroll_window_tree (w->contents);
12695 else if (w->cursor.vpos >= 0)
12697 int h_margin;
12698 int text_area_width;
12699 struct glyph_row *cursor_row;
12700 struct glyph_row *bottom_row;
12702 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12703 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12704 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12705 else
12706 cursor_row = bottom_row - 1;
12708 if (!cursor_row->enabled_p)
12710 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12711 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12712 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12713 else
12714 cursor_row = bottom_row - 1;
12716 bool row_r2l_p = cursor_row->reversed_p;
12718 text_area_width = window_box_width (w, TEXT_AREA);
12720 /* Scroll when cursor is inside this scroll margin. */
12721 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12723 /* If the position of this window's point has explicitly
12724 changed, no more suspend auto hscrolling. */
12725 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12726 w->suspend_auto_hscroll = false;
12728 /* Remember window point. */
12729 Fset_marker (w->old_pointm,
12730 ((w == XWINDOW (selected_window))
12731 ? make_number (BUF_PT (XBUFFER (w->contents)))
12732 : Fmarker_position (w->pointm)),
12733 w->contents);
12735 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12736 && !w->suspend_auto_hscroll
12737 /* In some pathological cases, like restoring a window
12738 configuration into a frame that is much smaller than
12739 the one from which the configuration was saved, we
12740 get glyph rows whose start and end have zero buffer
12741 positions, which we cannot handle below. Just skip
12742 such windows. */
12743 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12744 /* For left-to-right rows, hscroll when cursor is either
12745 (i) inside the right hscroll margin, or (ii) if it is
12746 inside the left margin and the window is already
12747 hscrolled. */
12748 && ((!row_r2l_p
12749 && ((w->hscroll && w->cursor.x <= h_margin)
12750 || (cursor_row->enabled_p
12751 && cursor_row->truncated_on_right_p
12752 && (w->cursor.x >= text_area_width - h_margin))))
12753 /* For right-to-left rows, the logic is similar,
12754 except that rules for scrolling to left and right
12755 are reversed. E.g., if cursor.x <= h_margin, we
12756 need to hscroll "to the right" unconditionally,
12757 and that will scroll the screen to the left so as
12758 to reveal the next portion of the row. */
12759 || (row_r2l_p
12760 && ((cursor_row->enabled_p
12761 /* FIXME: It is confusing to set the
12762 truncated_on_right_p flag when R2L rows
12763 are actually truncated on the left. */
12764 && cursor_row->truncated_on_right_p
12765 && w->cursor.x <= h_margin)
12766 || (w->hscroll
12767 && (w->cursor.x >= text_area_width - h_margin))))))
12769 struct it it;
12770 ptrdiff_t hscroll;
12771 struct buffer *saved_current_buffer;
12772 ptrdiff_t pt;
12773 int wanted_x;
12775 /* Find point in a display of infinite width. */
12776 saved_current_buffer = current_buffer;
12777 current_buffer = XBUFFER (w->contents);
12779 if (w == XWINDOW (selected_window))
12780 pt = PT;
12781 else
12782 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12784 /* Move iterator to pt starting at cursor_row->start in
12785 a line with infinite width. */
12786 init_to_row_start (&it, w, cursor_row);
12787 it.last_visible_x = INFINITY;
12788 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12789 current_buffer = saved_current_buffer;
12791 /* Position cursor in window. */
12792 if (!hscroll_relative_p && hscroll_step_abs == 0)
12793 hscroll = max (0, (it.current_x
12794 - (ITERATOR_AT_END_OF_LINE_P (&it)
12795 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12796 : (text_area_width / 2))))
12797 / FRAME_COLUMN_WIDTH (it.f);
12798 else if ((!row_r2l_p
12799 && w->cursor.x >= text_area_width - h_margin)
12800 || (row_r2l_p && w->cursor.x <= h_margin))
12802 if (hscroll_relative_p)
12803 wanted_x = text_area_width * (1 - hscroll_step_rel)
12804 - h_margin;
12805 else
12806 wanted_x = text_area_width
12807 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12808 - h_margin;
12809 hscroll
12810 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12812 else
12814 if (hscroll_relative_p)
12815 wanted_x = text_area_width * hscroll_step_rel
12816 + h_margin;
12817 else
12818 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12819 + h_margin;
12820 hscroll
12821 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12823 hscroll = max (hscroll, w->min_hscroll);
12825 /* Don't prevent redisplay optimizations if hscroll
12826 hasn't changed, as it will unnecessarily slow down
12827 redisplay. */
12828 if (w->hscroll != hscroll)
12830 struct buffer *b = XBUFFER (w->contents);
12831 b->prevent_redisplay_optimizations_p = true;
12832 w->hscroll = hscroll;
12833 hscrolled_p = true;
12838 window = w->next;
12841 /* Value is true if hscroll of any leaf window has been changed. */
12842 return hscrolled_p;
12846 /* Set hscroll so that cursor is visible and not inside horizontal
12847 scroll margins for all windows in the tree rooted at WINDOW. See
12848 also hscroll_window_tree above. Value is true if any window's
12849 hscroll has been changed. If it has, desired matrices on the frame
12850 of WINDOW are cleared. */
12852 static bool
12853 hscroll_windows (Lisp_Object window)
12855 bool hscrolled_p = hscroll_window_tree (window);
12856 if (hscrolled_p)
12857 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12858 return hscrolled_p;
12863 /************************************************************************
12864 Redisplay
12865 ************************************************************************/
12867 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12868 This is sometimes handy to have in a debugger session. */
12870 #ifdef GLYPH_DEBUG
12872 /* First and last unchanged row for try_window_id. */
12874 static int debug_first_unchanged_at_end_vpos;
12875 static int debug_last_unchanged_at_beg_vpos;
12877 /* Delta vpos and y. */
12879 static int debug_dvpos, debug_dy;
12881 /* Delta in characters and bytes for try_window_id. */
12883 static ptrdiff_t debug_delta, debug_delta_bytes;
12885 /* Values of window_end_pos and window_end_vpos at the end of
12886 try_window_id. */
12888 static ptrdiff_t debug_end_vpos;
12890 /* Append a string to W->desired_matrix->method. FMT is a printf
12891 format string. If trace_redisplay_p is true also printf the
12892 resulting string to stderr. */
12894 static void debug_method_add (struct window *, char const *, ...)
12895 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12897 static void
12898 debug_method_add (struct window *w, char const *fmt, ...)
12900 void *ptr = w;
12901 char *method = w->desired_matrix->method;
12902 int len = strlen (method);
12903 int size = sizeof w->desired_matrix->method;
12904 int remaining = size - len - 1;
12905 va_list ap;
12907 if (len && remaining)
12909 method[len] = '|';
12910 --remaining, ++len;
12913 va_start (ap, fmt);
12914 vsnprintf (method + len, remaining + 1, fmt, ap);
12915 va_end (ap);
12917 if (trace_redisplay_p)
12918 fprintf (stderr, "%p (%s): %s\n",
12919 ptr,
12920 ((BUFFERP (w->contents)
12921 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12922 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12923 : "no buffer"),
12924 method + len);
12927 #endif /* GLYPH_DEBUG */
12930 /* Value is true if all changes in window W, which displays
12931 current_buffer, are in the text between START and END. START is a
12932 buffer position, END is given as a distance from Z. Used in
12933 redisplay_internal for display optimization. */
12935 static bool
12936 text_outside_line_unchanged_p (struct window *w,
12937 ptrdiff_t start, ptrdiff_t end)
12939 bool unchanged_p = true;
12941 /* If text or overlays have changed, see where. */
12942 if (window_outdated (w))
12944 /* Gap in the line? */
12945 if (GPT < start || Z - GPT < end)
12946 unchanged_p = false;
12948 /* Changes start in front of the line, or end after it? */
12949 if (unchanged_p
12950 && (BEG_UNCHANGED < start - 1
12951 || END_UNCHANGED < end))
12952 unchanged_p = false;
12954 /* If selective display, can't optimize if changes start at the
12955 beginning of the line. */
12956 if (unchanged_p
12957 && INTEGERP (BVAR (current_buffer, selective_display))
12958 && XINT (BVAR (current_buffer, selective_display)) > 0
12959 && (BEG_UNCHANGED < start || GPT <= start))
12960 unchanged_p = false;
12962 /* If there are overlays at the start or end of the line, these
12963 may have overlay strings with newlines in them. A change at
12964 START, for instance, may actually concern the display of such
12965 overlay strings as well, and they are displayed on different
12966 lines. So, quickly rule out this case. (For the future, it
12967 might be desirable to implement something more telling than
12968 just BEG/END_UNCHANGED.) */
12969 if (unchanged_p)
12971 if (BEG + BEG_UNCHANGED == start
12972 && overlay_touches_p (start))
12973 unchanged_p = false;
12974 if (END_UNCHANGED == end
12975 && overlay_touches_p (Z - end))
12976 unchanged_p = false;
12979 /* Under bidi reordering, adding or deleting a character in the
12980 beginning of a paragraph, before the first strong directional
12981 character, can change the base direction of the paragraph (unless
12982 the buffer specifies a fixed paragraph direction), which will
12983 require to redisplay the whole paragraph. It might be worthwhile
12984 to find the paragraph limits and widen the range of redisplayed
12985 lines to that, but for now just give up this optimization. */
12986 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12987 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12988 unchanged_p = false;
12991 return unchanged_p;
12995 /* Do a frame update, taking possible shortcuts into account. This is
12996 the main external entry point for redisplay.
12998 If the last redisplay displayed an echo area message and that message
12999 is no longer requested, we clear the echo area or bring back the
13000 mini-buffer if that is in use. */
13002 void
13003 redisplay (void)
13005 redisplay_internal ();
13009 static Lisp_Object
13010 overlay_arrow_string_or_property (Lisp_Object var)
13012 Lisp_Object val;
13014 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13015 return val;
13017 return Voverlay_arrow_string;
13020 /* Return true if there are any overlay-arrows in current_buffer. */
13021 static bool
13022 overlay_arrow_in_current_buffer_p (void)
13024 Lisp_Object vlist;
13026 for (vlist = Voverlay_arrow_variable_list;
13027 CONSP (vlist);
13028 vlist = XCDR (vlist))
13030 Lisp_Object var = XCAR (vlist);
13031 Lisp_Object val;
13033 if (!SYMBOLP (var))
13034 continue;
13035 val = find_symbol_value (var);
13036 if (MARKERP (val)
13037 && current_buffer == XMARKER (val)->buffer)
13038 return true;
13040 return false;
13044 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13045 has changed. */
13047 static bool
13048 overlay_arrows_changed_p (void)
13050 Lisp_Object vlist;
13052 for (vlist = Voverlay_arrow_variable_list;
13053 CONSP (vlist);
13054 vlist = XCDR (vlist))
13056 Lisp_Object var = XCAR (vlist);
13057 Lisp_Object val, pstr;
13059 if (!SYMBOLP (var))
13060 continue;
13061 val = find_symbol_value (var);
13062 if (!MARKERP (val))
13063 continue;
13064 if (! EQ (COERCE_MARKER (val),
13065 Fget (var, Qlast_arrow_position))
13066 || ! (pstr = overlay_arrow_string_or_property (var),
13067 EQ (pstr, Fget (var, Qlast_arrow_string))))
13068 return true;
13070 return false;
13073 /* Mark overlay arrows to be updated on next redisplay. */
13075 static void
13076 update_overlay_arrows (int up_to_date)
13078 Lisp_Object vlist;
13080 for (vlist = Voverlay_arrow_variable_list;
13081 CONSP (vlist);
13082 vlist = XCDR (vlist))
13084 Lisp_Object var = XCAR (vlist);
13086 if (!SYMBOLP (var))
13087 continue;
13089 if (up_to_date > 0)
13091 Lisp_Object val = find_symbol_value (var);
13092 Fput (var, Qlast_arrow_position,
13093 COERCE_MARKER (val));
13094 Fput (var, Qlast_arrow_string,
13095 overlay_arrow_string_or_property (var));
13097 else if (up_to_date < 0
13098 || !NILP (Fget (var, Qlast_arrow_position)))
13100 Fput (var, Qlast_arrow_position, Qt);
13101 Fput (var, Qlast_arrow_string, Qt);
13107 /* Return overlay arrow string to display at row.
13108 Return integer (bitmap number) for arrow bitmap in left fringe.
13109 Return nil if no overlay arrow. */
13111 static Lisp_Object
13112 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13114 Lisp_Object vlist;
13116 for (vlist = Voverlay_arrow_variable_list;
13117 CONSP (vlist);
13118 vlist = XCDR (vlist))
13120 Lisp_Object var = XCAR (vlist);
13121 Lisp_Object val;
13123 if (!SYMBOLP (var))
13124 continue;
13126 val = find_symbol_value (var);
13128 if (MARKERP (val)
13129 && current_buffer == XMARKER (val)->buffer
13130 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13132 if (FRAME_WINDOW_P (it->f)
13133 /* FIXME: if ROW->reversed_p is set, this should test
13134 the right fringe, not the left one. */
13135 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13137 #ifdef HAVE_WINDOW_SYSTEM
13138 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13140 int fringe_bitmap = lookup_fringe_bitmap (val);
13141 if (fringe_bitmap != 0)
13142 return make_number (fringe_bitmap);
13144 #endif
13145 return make_number (-1); /* Use default arrow bitmap. */
13147 return overlay_arrow_string_or_property (var);
13151 return Qnil;
13154 /* Return true if point moved out of or into a composition. Otherwise
13155 return false. PREV_BUF and PREV_PT are the last point buffer and
13156 position. BUF and PT are the current point buffer and position. */
13158 static bool
13159 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13160 struct buffer *buf, ptrdiff_t pt)
13162 ptrdiff_t start, end;
13163 Lisp_Object prop;
13164 Lisp_Object buffer;
13166 XSETBUFFER (buffer, buf);
13167 /* Check a composition at the last point if point moved within the
13168 same buffer. */
13169 if (prev_buf == buf)
13171 if (prev_pt == pt)
13172 /* Point didn't move. */
13173 return false;
13175 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13176 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13177 && composition_valid_p (start, end, prop)
13178 && start < prev_pt && end > prev_pt)
13179 /* The last point was within the composition. Return true iff
13180 point moved out of the composition. */
13181 return (pt <= start || pt >= end);
13184 /* Check a composition at the current point. */
13185 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13186 && find_composition (pt, -1, &start, &end, &prop, buffer)
13187 && composition_valid_p (start, end, prop)
13188 && start < pt && end > pt);
13191 /* Reconsider the clip changes of buffer which is displayed in W. */
13193 static void
13194 reconsider_clip_changes (struct window *w)
13196 struct buffer *b = XBUFFER (w->contents);
13198 if (b->clip_changed
13199 && w->window_end_valid
13200 && w->current_matrix->buffer == b
13201 && w->current_matrix->zv == BUF_ZV (b)
13202 && w->current_matrix->begv == BUF_BEGV (b))
13203 b->clip_changed = false;
13205 /* If display wasn't paused, and W is not a tool bar window, see if
13206 point has been moved into or out of a composition. In that case,
13207 set b->clip_changed to force updating the screen. If
13208 b->clip_changed has already been set, skip this check. */
13209 if (!b->clip_changed && w->window_end_valid)
13211 ptrdiff_t pt = (w == XWINDOW (selected_window)
13212 ? PT : marker_position (w->pointm));
13214 if ((w->current_matrix->buffer != b || pt != w->last_point)
13215 && check_point_in_composition (w->current_matrix->buffer,
13216 w->last_point, b, pt))
13217 b->clip_changed = true;
13221 static void
13222 propagate_buffer_redisplay (void)
13223 { /* Resetting b->text->redisplay is problematic!
13224 We can't just reset it in the case that some window that displays
13225 it has not been redisplayed; and such a window can stay
13226 unredisplayed for a long time if it's currently invisible.
13227 But we do want to reset it at the end of redisplay otherwise
13228 its displayed windows will keep being redisplayed over and over
13229 again.
13230 So we copy all b->text->redisplay flags up to their windows here,
13231 such that mark_window_display_accurate can safely reset
13232 b->text->redisplay. */
13233 Lisp_Object ws = window_list ();
13234 for (; CONSP (ws); ws = XCDR (ws))
13236 struct window *thisw = XWINDOW (XCAR (ws));
13237 struct buffer *thisb = XBUFFER (thisw->contents);
13238 if (thisb->text->redisplay)
13239 thisw->redisplay = true;
13243 #define STOP_POLLING \
13244 do { if (! polling_stopped_here) stop_polling (); \
13245 polling_stopped_here = true; } while (false)
13247 #define RESUME_POLLING \
13248 do { if (polling_stopped_here) start_polling (); \
13249 polling_stopped_here = false; } while (false)
13252 /* Perhaps in the future avoid recentering windows if it
13253 is not necessary; currently that causes some problems. */
13255 static void
13256 redisplay_internal (void)
13258 struct window *w = XWINDOW (selected_window);
13259 struct window *sw;
13260 struct frame *fr;
13261 bool pending;
13262 bool must_finish = false, match_p;
13263 struct text_pos tlbufpos, tlendpos;
13264 int number_of_visible_frames;
13265 ptrdiff_t count;
13266 struct frame *sf;
13267 bool polling_stopped_here = false;
13268 Lisp_Object tail, frame;
13270 /* True means redisplay has to consider all windows on all
13271 frames. False, only selected_window is considered. */
13272 bool consider_all_windows_p;
13274 /* True means redisplay has to redisplay the miniwindow. */
13275 bool update_miniwindow_p = false;
13277 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13279 /* No redisplay if running in batch mode or frame is not yet fully
13280 initialized, or redisplay is explicitly turned off by setting
13281 Vinhibit_redisplay. */
13282 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13283 || !NILP (Vinhibit_redisplay))
13284 return;
13286 /* Don't examine these until after testing Vinhibit_redisplay.
13287 When Emacs is shutting down, perhaps because its connection to
13288 X has dropped, we should not look at them at all. */
13289 fr = XFRAME (w->frame);
13290 sf = SELECTED_FRAME ();
13292 if (!fr->glyphs_initialized_p)
13293 return;
13295 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13296 if (popup_activated ())
13297 return;
13298 #endif
13300 /* I don't think this happens but let's be paranoid. */
13301 if (redisplaying_p)
13302 return;
13304 /* Record a function that clears redisplaying_p
13305 when we leave this function. */
13306 count = SPECPDL_INDEX ();
13307 record_unwind_protect_void (unwind_redisplay);
13308 redisplaying_p = true;
13309 specbind (Qinhibit_free_realized_faces, Qnil);
13311 /* Record this function, so it appears on the profiler's backtraces. */
13312 record_in_backtrace (Qredisplay_internal, 0, 0);
13314 FOR_EACH_FRAME (tail, frame)
13315 XFRAME (frame)->already_hscrolled_p = false;
13317 retry:
13318 /* Remember the currently selected window. */
13319 sw = w;
13321 pending = false;
13322 last_escape_glyph_frame = NULL;
13323 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13324 last_glyphless_glyph_frame = NULL;
13325 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13327 /* If face_change, init_iterator will free all realized faces, which
13328 includes the faces referenced from current matrices. So, we
13329 can't reuse current matrices in this case. */
13330 if (face_change)
13331 windows_or_buffers_changed = 47;
13333 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13334 && FRAME_TTY (sf)->previous_frame != sf)
13336 /* Since frames on a single ASCII terminal share the same
13337 display area, displaying a different frame means redisplay
13338 the whole thing. */
13339 SET_FRAME_GARBAGED (sf);
13340 #ifndef DOS_NT
13341 set_tty_color_mode (FRAME_TTY (sf), sf);
13342 #endif
13343 FRAME_TTY (sf)->previous_frame = sf;
13346 /* Set the visible flags for all frames. Do this before checking for
13347 resized or garbaged frames; they want to know if their frames are
13348 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13349 number_of_visible_frames = 0;
13351 FOR_EACH_FRAME (tail, frame)
13353 struct frame *f = XFRAME (frame);
13355 if (FRAME_VISIBLE_P (f))
13357 ++number_of_visible_frames;
13358 /* Adjust matrices for visible frames only. */
13359 if (f->fonts_changed)
13361 adjust_frame_glyphs (f);
13362 f->fonts_changed = false;
13364 /* If cursor type has been changed on the frame
13365 other than selected, consider all frames. */
13366 if (f != sf && f->cursor_type_changed)
13367 update_mode_lines = 31;
13369 clear_desired_matrices (f);
13372 /* Notice any pending interrupt request to change frame size. */
13373 do_pending_window_change (true);
13375 /* do_pending_window_change could change the selected_window due to
13376 frame resizing which makes the selected window too small. */
13377 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13378 sw = w;
13380 /* Clear frames marked as garbaged. */
13381 clear_garbaged_frames ();
13383 /* Build menubar and tool-bar items. */
13384 if (NILP (Vmemory_full))
13385 prepare_menu_bars ();
13387 reconsider_clip_changes (w);
13389 /* In most cases selected window displays current buffer. */
13390 match_p = XBUFFER (w->contents) == current_buffer;
13391 if (match_p)
13393 /* Detect case that we need to write or remove a star in the mode line. */
13394 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13395 w->update_mode_line = true;
13397 if (mode_line_update_needed (w))
13398 w->update_mode_line = true;
13400 /* If reconsider_clip_changes above decided that the narrowing
13401 in the current buffer changed, make sure all other windows
13402 showing that buffer will be redisplayed. */
13403 if (current_buffer->clip_changed)
13404 bset_update_mode_line (current_buffer);
13407 /* Normally the message* functions will have already displayed and
13408 updated the echo area, but the frame may have been trashed, or
13409 the update may have been preempted, so display the echo area
13410 again here. Checking message_cleared_p captures the case that
13411 the echo area should be cleared. */
13412 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13413 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13414 || (message_cleared_p
13415 && minibuf_level == 0
13416 /* If the mini-window is currently selected, this means the
13417 echo-area doesn't show through. */
13418 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13420 bool window_height_changed_p = echo_area_display (false);
13422 if (message_cleared_p)
13423 update_miniwindow_p = true;
13425 must_finish = true;
13427 /* If we don't display the current message, don't clear the
13428 message_cleared_p flag, because, if we did, we wouldn't clear
13429 the echo area in the next redisplay which doesn't preserve
13430 the echo area. */
13431 if (!display_last_displayed_message_p)
13432 message_cleared_p = false;
13434 if (window_height_changed_p)
13436 windows_or_buffers_changed = 50;
13438 /* If window configuration was changed, frames may have been
13439 marked garbaged. Clear them or we will experience
13440 surprises wrt scrolling. */
13441 clear_garbaged_frames ();
13444 else if (EQ (selected_window, minibuf_window)
13445 && (current_buffer->clip_changed || window_outdated (w))
13446 && resize_mini_window (w, false))
13448 /* Resized active mini-window to fit the size of what it is
13449 showing if its contents might have changed. */
13450 must_finish = true;
13452 /* If window configuration was changed, frames may have been
13453 marked garbaged. Clear them or we will experience
13454 surprises wrt scrolling. */
13455 clear_garbaged_frames ();
13458 if (windows_or_buffers_changed && !update_mode_lines)
13459 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13460 only the windows's contents needs to be refreshed, or whether the
13461 mode-lines also need a refresh. */
13462 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13463 ? REDISPLAY_SOME : 32);
13465 /* If specs for an arrow have changed, do thorough redisplay
13466 to ensure we remove any arrow that should no longer exist. */
13467 if (overlay_arrows_changed_p ())
13468 /* Apparently, this is the only case where we update other windows,
13469 without updating other mode-lines. */
13470 windows_or_buffers_changed = 49;
13472 consider_all_windows_p = (update_mode_lines
13473 || windows_or_buffers_changed);
13475 #define AINC(a,i) \
13476 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13477 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13479 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13480 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13482 /* Optimize the case that only the line containing the cursor in the
13483 selected window has changed. Variables starting with this_ are
13484 set in display_line and record information about the line
13485 containing the cursor. */
13486 tlbufpos = this_line_start_pos;
13487 tlendpos = this_line_end_pos;
13488 if (!consider_all_windows_p
13489 && CHARPOS (tlbufpos) > 0
13490 && !w->update_mode_line
13491 && !current_buffer->clip_changed
13492 && !current_buffer->prevent_redisplay_optimizations_p
13493 && FRAME_VISIBLE_P (XFRAME (w->frame))
13494 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13495 && !XFRAME (w->frame)->cursor_type_changed
13496 /* Make sure recorded data applies to current buffer, etc. */
13497 && this_line_buffer == current_buffer
13498 && match_p
13499 && !w->force_start
13500 && !w->optional_new_start
13501 /* Point must be on the line that we have info recorded about. */
13502 && PT >= CHARPOS (tlbufpos)
13503 && PT <= Z - CHARPOS (tlendpos)
13504 /* All text outside that line, including its final newline,
13505 must be unchanged. */
13506 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13507 CHARPOS (tlendpos)))
13509 if (CHARPOS (tlbufpos) > BEGV
13510 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13511 && (CHARPOS (tlbufpos) == ZV
13512 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13513 /* Former continuation line has disappeared by becoming empty. */
13514 goto cancel;
13515 else if (window_outdated (w) || MINI_WINDOW_P (w))
13517 /* We have to handle the case of continuation around a
13518 wide-column character (see the comment in indent.c around
13519 line 1340).
13521 For instance, in the following case:
13523 -------- Insert --------
13524 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13525 J_I_ ==> J_I_ `^^' are cursors.
13526 ^^ ^^
13527 -------- --------
13529 As we have to redraw the line above, we cannot use this
13530 optimization. */
13532 struct it it;
13533 int line_height_before = this_line_pixel_height;
13535 /* Note that start_display will handle the case that the
13536 line starting at tlbufpos is a continuation line. */
13537 start_display (&it, w, tlbufpos);
13539 /* Implementation note: It this still necessary? */
13540 if (it.current_x != this_line_start_x)
13541 goto cancel;
13543 TRACE ((stderr, "trying display optimization 1\n"));
13544 w->cursor.vpos = -1;
13545 overlay_arrow_seen = false;
13546 it.vpos = this_line_vpos;
13547 it.current_y = this_line_y;
13548 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13549 display_line (&it);
13551 /* If line contains point, is not continued,
13552 and ends at same distance from eob as before, we win. */
13553 if (w->cursor.vpos >= 0
13554 /* Line is not continued, otherwise this_line_start_pos
13555 would have been set to 0 in display_line. */
13556 && CHARPOS (this_line_start_pos)
13557 /* Line ends as before. */
13558 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13559 /* Line has same height as before. Otherwise other lines
13560 would have to be shifted up or down. */
13561 && this_line_pixel_height == line_height_before)
13563 /* If this is not the window's last line, we must adjust
13564 the charstarts of the lines below. */
13565 if (it.current_y < it.last_visible_y)
13567 struct glyph_row *row
13568 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13569 ptrdiff_t delta, delta_bytes;
13571 /* We used to distinguish between two cases here,
13572 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13573 when the line ends in a newline or the end of the
13574 buffer's accessible portion. But both cases did
13575 the same, so they were collapsed. */
13576 delta = (Z
13577 - CHARPOS (tlendpos)
13578 - MATRIX_ROW_START_CHARPOS (row));
13579 delta_bytes = (Z_BYTE
13580 - BYTEPOS (tlendpos)
13581 - MATRIX_ROW_START_BYTEPOS (row));
13583 increment_matrix_positions (w->current_matrix,
13584 this_line_vpos + 1,
13585 w->current_matrix->nrows,
13586 delta, delta_bytes);
13589 /* If this row displays text now but previously didn't,
13590 or vice versa, w->window_end_vpos may have to be
13591 adjusted. */
13592 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13594 if (w->window_end_vpos < this_line_vpos)
13595 w->window_end_vpos = this_line_vpos;
13597 else if (w->window_end_vpos == this_line_vpos
13598 && this_line_vpos > 0)
13599 w->window_end_vpos = this_line_vpos - 1;
13600 w->window_end_valid = false;
13602 /* Update hint: No need to try to scroll in update_window. */
13603 w->desired_matrix->no_scrolling_p = true;
13605 #ifdef GLYPH_DEBUG
13606 *w->desired_matrix->method = 0;
13607 debug_method_add (w, "optimization 1");
13608 #endif
13609 #ifdef HAVE_WINDOW_SYSTEM
13610 update_window_fringes (w, false);
13611 #endif
13612 goto update;
13614 else
13615 goto cancel;
13617 else if (/* Cursor position hasn't changed. */
13618 PT == w->last_point
13619 /* Make sure the cursor was last displayed
13620 in this window. Otherwise we have to reposition it. */
13622 /* PXW: Must be converted to pixels, probably. */
13623 && 0 <= w->cursor.vpos
13624 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13626 if (!must_finish)
13628 do_pending_window_change (true);
13629 /* If selected_window changed, redisplay again. */
13630 if (WINDOWP (selected_window)
13631 && (w = XWINDOW (selected_window)) != sw)
13632 goto retry;
13634 /* We used to always goto end_of_redisplay here, but this
13635 isn't enough if we have a blinking cursor. */
13636 if (w->cursor_off_p == w->last_cursor_off_p)
13637 goto end_of_redisplay;
13639 goto update;
13641 /* If highlighting the region, or if the cursor is in the echo area,
13642 then we can't just move the cursor. */
13643 else if (NILP (Vshow_trailing_whitespace)
13644 && !cursor_in_echo_area)
13646 struct it it;
13647 struct glyph_row *row;
13649 /* Skip from tlbufpos to PT and see where it is. Note that
13650 PT may be in invisible text. If so, we will end at the
13651 next visible position. */
13652 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13653 NULL, DEFAULT_FACE_ID);
13654 it.current_x = this_line_start_x;
13655 it.current_y = this_line_y;
13656 it.vpos = this_line_vpos;
13658 /* The call to move_it_to stops in front of PT, but
13659 moves over before-strings. */
13660 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13662 if (it.vpos == this_line_vpos
13663 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13664 row->enabled_p))
13666 eassert (this_line_vpos == it.vpos);
13667 eassert (this_line_y == it.current_y);
13668 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13669 #ifdef GLYPH_DEBUG
13670 *w->desired_matrix->method = 0;
13671 debug_method_add (w, "optimization 3");
13672 #endif
13673 goto update;
13675 else
13676 goto cancel;
13679 cancel:
13680 /* Text changed drastically or point moved off of line. */
13681 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13684 CHARPOS (this_line_start_pos) = 0;
13685 ++clear_face_cache_count;
13686 #ifdef HAVE_WINDOW_SYSTEM
13687 ++clear_image_cache_count;
13688 #endif
13690 /* Build desired matrices, and update the display. If
13691 consider_all_windows_p, do it for all windows on all frames.
13692 Otherwise do it for selected_window, only. */
13694 if (consider_all_windows_p)
13696 FOR_EACH_FRAME (tail, frame)
13697 XFRAME (frame)->updated_p = false;
13699 propagate_buffer_redisplay ();
13701 FOR_EACH_FRAME (tail, frame)
13703 struct frame *f = XFRAME (frame);
13705 /* We don't have to do anything for unselected terminal
13706 frames. */
13707 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13708 && !EQ (FRAME_TTY (f)->top_frame, frame))
13709 continue;
13711 retry_frame:
13713 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13714 /* Redisplay internal tool bar if this is the first time so we
13715 can adjust the frame height right now, if necessary. */
13716 if (!f->tool_bar_redisplayed_once)
13718 if (redisplay_tool_bar (f))
13719 adjust_frame_glyphs (f);
13720 f->tool_bar_redisplayed_once = true;
13722 #endif
13724 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13726 bool gcscrollbars
13727 /* Only GC scrollbars when we redisplay the whole frame. */
13728 = f->redisplay || !REDISPLAY_SOME_P ();
13729 /* Mark all the scroll bars to be removed; we'll redeem
13730 the ones we want when we redisplay their windows. */
13731 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13732 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13734 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13735 redisplay_windows (FRAME_ROOT_WINDOW (f));
13736 /* Remember that the invisible frames need to be redisplayed next
13737 time they're visible. */
13738 else if (!REDISPLAY_SOME_P ())
13739 f->redisplay = true;
13741 /* The X error handler may have deleted that frame. */
13742 if (!FRAME_LIVE_P (f))
13743 continue;
13745 /* Any scroll bars which redisplay_windows should have
13746 nuked should now go away. */
13747 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13748 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13750 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13752 /* If fonts changed on visible frame, display again. */
13753 if (f->fonts_changed)
13755 adjust_frame_glyphs (f);
13756 f->fonts_changed = false;
13757 goto retry_frame;
13760 /* See if we have to hscroll. */
13761 if (!f->already_hscrolled_p)
13763 f->already_hscrolled_p = true;
13764 if (hscroll_windows (f->root_window))
13765 goto retry_frame;
13768 /* Prevent various kinds of signals during display
13769 update. stdio is not robust about handling
13770 signals, which can cause an apparent I/O error. */
13771 if (interrupt_input)
13772 unrequest_sigio ();
13773 STOP_POLLING;
13775 pending |= update_frame (f, false, false);
13776 f->cursor_type_changed = false;
13777 f->updated_p = true;
13782 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13784 if (!pending)
13786 /* Do the mark_window_display_accurate after all windows have
13787 been redisplayed because this call resets flags in buffers
13788 which are needed for proper redisplay. */
13789 FOR_EACH_FRAME (tail, frame)
13791 struct frame *f = XFRAME (frame);
13792 if (f->updated_p)
13794 f->redisplay = false;
13795 mark_window_display_accurate (f->root_window, true);
13796 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13797 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13802 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13804 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13805 struct frame *mini_frame;
13807 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13808 /* Use list_of_error, not Qerror, so that
13809 we catch only errors and don't run the debugger. */
13810 internal_condition_case_1 (redisplay_window_1, selected_window,
13811 list_of_error,
13812 redisplay_window_error);
13813 if (update_miniwindow_p)
13814 internal_condition_case_1 (redisplay_window_1, mini_window,
13815 list_of_error,
13816 redisplay_window_error);
13818 /* Compare desired and current matrices, perform output. */
13820 update:
13821 /* If fonts changed, display again. */
13822 if (sf->fonts_changed)
13823 goto retry;
13825 /* Prevent various kinds of signals during display update.
13826 stdio is not robust about handling signals,
13827 which can cause an apparent I/O error. */
13828 if (interrupt_input)
13829 unrequest_sigio ();
13830 STOP_POLLING;
13832 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13834 if (hscroll_windows (selected_window))
13835 goto retry;
13837 XWINDOW (selected_window)->must_be_updated_p = true;
13838 pending = update_frame (sf, false, false);
13839 sf->cursor_type_changed = false;
13842 /* We may have called echo_area_display at the top of this
13843 function. If the echo area is on another frame, that may
13844 have put text on a frame other than the selected one, so the
13845 above call to update_frame would not have caught it. Catch
13846 it here. */
13847 mini_window = FRAME_MINIBUF_WINDOW (sf);
13848 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13850 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13852 XWINDOW (mini_window)->must_be_updated_p = true;
13853 pending |= update_frame (mini_frame, false, false);
13854 mini_frame->cursor_type_changed = false;
13855 if (!pending && hscroll_windows (mini_window))
13856 goto retry;
13860 /* If display was paused because of pending input, make sure we do a
13861 thorough update the next time. */
13862 if (pending)
13864 /* Prevent the optimization at the beginning of
13865 redisplay_internal that tries a single-line update of the
13866 line containing the cursor in the selected window. */
13867 CHARPOS (this_line_start_pos) = 0;
13869 /* Let the overlay arrow be updated the next time. */
13870 update_overlay_arrows (0);
13872 /* If we pause after scrolling, some rows in the current
13873 matrices of some windows are not valid. */
13874 if (!WINDOW_FULL_WIDTH_P (w)
13875 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13876 update_mode_lines = 36;
13878 else
13880 if (!consider_all_windows_p)
13882 /* This has already been done above if
13883 consider_all_windows_p is set. */
13884 if (XBUFFER (w->contents)->text->redisplay
13885 && buffer_window_count (XBUFFER (w->contents)) > 1)
13886 /* This can happen if b->text->redisplay was set during
13887 jit-lock. */
13888 propagate_buffer_redisplay ();
13889 mark_window_display_accurate_1 (w, true);
13891 /* Say overlay arrows are up to date. */
13892 update_overlay_arrows (1);
13894 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13895 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13898 update_mode_lines = 0;
13899 windows_or_buffers_changed = 0;
13902 /* Start SIGIO interrupts coming again. Having them off during the
13903 code above makes it less likely one will discard output, but not
13904 impossible, since there might be stuff in the system buffer here.
13905 But it is much hairier to try to do anything about that. */
13906 if (interrupt_input)
13907 request_sigio ();
13908 RESUME_POLLING;
13910 /* If a frame has become visible which was not before, redisplay
13911 again, so that we display it. Expose events for such a frame
13912 (which it gets when becoming visible) don't call the parts of
13913 redisplay constructing glyphs, so simply exposing a frame won't
13914 display anything in this case. So, we have to display these
13915 frames here explicitly. */
13916 if (!pending)
13918 int new_count = 0;
13920 FOR_EACH_FRAME (tail, frame)
13922 if (XFRAME (frame)->visible)
13923 new_count++;
13926 if (new_count != number_of_visible_frames)
13927 windows_or_buffers_changed = 52;
13930 /* Change frame size now if a change is pending. */
13931 do_pending_window_change (true);
13933 /* If we just did a pending size change, or have additional
13934 visible frames, or selected_window changed, redisplay again. */
13935 if ((windows_or_buffers_changed && !pending)
13936 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13937 goto retry;
13939 /* Clear the face and image caches.
13941 We used to do this only if consider_all_windows_p. But the cache
13942 needs to be cleared if a timer creates images in the current
13943 buffer (e.g. the test case in Bug#6230). */
13945 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13947 clear_face_cache (false);
13948 clear_face_cache_count = 0;
13951 #ifdef HAVE_WINDOW_SYSTEM
13952 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13954 clear_image_caches (Qnil);
13955 clear_image_cache_count = 0;
13957 #endif /* HAVE_WINDOW_SYSTEM */
13959 end_of_redisplay:
13960 #ifdef HAVE_NS
13961 ns_set_doc_edited ();
13962 #endif
13963 if (interrupt_input && interrupts_deferred)
13964 request_sigio ();
13966 unbind_to (count, Qnil);
13967 RESUME_POLLING;
13971 /* Redisplay, but leave alone any recent echo area message unless
13972 another message has been requested in its place.
13974 This is useful in situations where you need to redisplay but no
13975 user action has occurred, making it inappropriate for the message
13976 area to be cleared. See tracking_off and
13977 wait_reading_process_output for examples of these situations.
13979 FROM_WHERE is an integer saying from where this function was
13980 called. This is useful for debugging. */
13982 void
13983 redisplay_preserve_echo_area (int from_where)
13985 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13987 if (!NILP (echo_area_buffer[1]))
13989 /* We have a previously displayed message, but no current
13990 message. Redisplay the previous message. */
13991 display_last_displayed_message_p = true;
13992 redisplay_internal ();
13993 display_last_displayed_message_p = false;
13995 else
13996 redisplay_internal ();
13998 flush_frame (SELECTED_FRAME ());
14002 /* Function registered with record_unwind_protect in redisplay_internal. */
14004 static void
14005 unwind_redisplay (void)
14007 redisplaying_p = false;
14011 /* Mark the display of leaf window W as accurate or inaccurate.
14012 If ACCURATE_P, mark display of W as accurate.
14013 If !ACCURATE_P, arrange for W to be redisplayed the next
14014 time redisplay_internal is called. */
14016 static void
14017 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14019 struct buffer *b = XBUFFER (w->contents);
14021 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14022 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14023 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14025 if (accurate_p)
14027 b->clip_changed = false;
14028 b->prevent_redisplay_optimizations_p = false;
14029 eassert (buffer_window_count (b) > 0);
14030 /* Resetting b->text->redisplay is problematic!
14031 In order to make it safer to do it here, redisplay_internal must
14032 have copied all b->text->redisplay to their respective windows. */
14033 b->text->redisplay = false;
14035 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14036 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14037 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14038 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14040 w->current_matrix->buffer = b;
14041 w->current_matrix->begv = BUF_BEGV (b);
14042 w->current_matrix->zv = BUF_ZV (b);
14044 w->last_cursor_vpos = w->cursor.vpos;
14045 w->last_cursor_off_p = w->cursor_off_p;
14047 if (w == XWINDOW (selected_window))
14048 w->last_point = BUF_PT (b);
14049 else
14050 w->last_point = marker_position (w->pointm);
14052 w->window_end_valid = true;
14053 w->update_mode_line = false;
14056 w->redisplay = !accurate_p;
14060 /* Mark the display of windows in the window tree rooted at WINDOW as
14061 accurate or inaccurate. If ACCURATE_P, mark display of
14062 windows as accurate. If !ACCURATE_P, arrange for windows to
14063 be redisplayed the next time redisplay_internal is called. */
14065 void
14066 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14068 struct window *w;
14070 for (; !NILP (window); window = w->next)
14072 w = XWINDOW (window);
14073 if (WINDOWP (w->contents))
14074 mark_window_display_accurate (w->contents, accurate_p);
14075 else
14076 mark_window_display_accurate_1 (w, accurate_p);
14079 if (accurate_p)
14080 update_overlay_arrows (1);
14081 else
14082 /* Force a thorough redisplay the next time by setting
14083 last_arrow_position and last_arrow_string to t, which is
14084 unequal to any useful value of Voverlay_arrow_... */
14085 update_overlay_arrows (-1);
14089 /* Return value in display table DP (Lisp_Char_Table *) for character
14090 C. Since a display table doesn't have any parent, we don't have to
14091 follow parent. Do not call this function directly but use the
14092 macro DISP_CHAR_VECTOR. */
14094 Lisp_Object
14095 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14097 Lisp_Object val;
14099 if (ASCII_CHAR_P (c))
14101 val = dp->ascii;
14102 if (SUB_CHAR_TABLE_P (val))
14103 val = XSUB_CHAR_TABLE (val)->contents[c];
14105 else
14107 Lisp_Object table;
14109 XSETCHAR_TABLE (table, dp);
14110 val = char_table_ref (table, c);
14112 if (NILP (val))
14113 val = dp->defalt;
14114 return val;
14119 /***********************************************************************
14120 Window Redisplay
14121 ***********************************************************************/
14123 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14125 static void
14126 redisplay_windows (Lisp_Object window)
14128 while (!NILP (window))
14130 struct window *w = XWINDOW (window);
14132 if (WINDOWP (w->contents))
14133 redisplay_windows (w->contents);
14134 else if (BUFFERP (w->contents))
14136 displayed_buffer = XBUFFER (w->contents);
14137 /* Use list_of_error, not Qerror, so that
14138 we catch only errors and don't run the debugger. */
14139 internal_condition_case_1 (redisplay_window_0, window,
14140 list_of_error,
14141 redisplay_window_error);
14144 window = w->next;
14148 static Lisp_Object
14149 redisplay_window_error (Lisp_Object ignore)
14151 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14152 return Qnil;
14155 static Lisp_Object
14156 redisplay_window_0 (Lisp_Object window)
14158 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14159 redisplay_window (window, false);
14160 return Qnil;
14163 static Lisp_Object
14164 redisplay_window_1 (Lisp_Object window)
14166 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14167 redisplay_window (window, true);
14168 return Qnil;
14172 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14173 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14174 which positions recorded in ROW differ from current buffer
14175 positions.
14177 Return true iff cursor is on this row. */
14179 static bool
14180 set_cursor_from_row (struct window *w, struct glyph_row *row,
14181 struct glyph_matrix *matrix,
14182 ptrdiff_t delta, ptrdiff_t delta_bytes,
14183 int dy, int dvpos)
14185 struct glyph *glyph = row->glyphs[TEXT_AREA];
14186 struct glyph *end = glyph + row->used[TEXT_AREA];
14187 struct glyph *cursor = NULL;
14188 /* The last known character position in row. */
14189 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14190 int x = row->x;
14191 ptrdiff_t pt_old = PT - delta;
14192 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14193 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14194 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14195 /* A glyph beyond the edge of TEXT_AREA which we should never
14196 touch. */
14197 struct glyph *glyphs_end = end;
14198 /* True means we've found a match for cursor position, but that
14199 glyph has the avoid_cursor_p flag set. */
14200 bool match_with_avoid_cursor = false;
14201 /* True means we've seen at least one glyph that came from a
14202 display string. */
14203 bool string_seen = false;
14204 /* Largest and smallest buffer positions seen so far during scan of
14205 glyph row. */
14206 ptrdiff_t bpos_max = pos_before;
14207 ptrdiff_t bpos_min = pos_after;
14208 /* Last buffer position covered by an overlay string with an integer
14209 `cursor' property. */
14210 ptrdiff_t bpos_covered = 0;
14211 /* True means the display string on which to display the cursor
14212 comes from a text property, not from an overlay. */
14213 bool string_from_text_prop = false;
14215 /* Don't even try doing anything if called for a mode-line or
14216 header-line row, since the rest of the code isn't prepared to
14217 deal with such calamities. */
14218 eassert (!row->mode_line_p);
14219 if (row->mode_line_p)
14220 return false;
14222 /* Skip over glyphs not having an object at the start and the end of
14223 the row. These are special glyphs like truncation marks on
14224 terminal frames. */
14225 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14227 if (!row->reversed_p)
14229 while (glyph < end
14230 && NILP (glyph->object)
14231 && glyph->charpos < 0)
14233 x += glyph->pixel_width;
14234 ++glyph;
14236 while (end > glyph
14237 && NILP ((end - 1)->object)
14238 /* CHARPOS is zero for blanks and stretch glyphs
14239 inserted by extend_face_to_end_of_line. */
14240 && (end - 1)->charpos <= 0)
14241 --end;
14242 glyph_before = glyph - 1;
14243 glyph_after = end;
14245 else
14247 struct glyph *g;
14249 /* If the glyph row is reversed, we need to process it from back
14250 to front, so swap the edge pointers. */
14251 glyphs_end = end = glyph - 1;
14252 glyph += row->used[TEXT_AREA] - 1;
14254 while (glyph > end + 1
14255 && NILP (glyph->object)
14256 && glyph->charpos < 0)
14258 --glyph;
14259 x -= glyph->pixel_width;
14261 if (NILP (glyph->object) && glyph->charpos < 0)
14262 --glyph;
14263 /* By default, in reversed rows we put the cursor on the
14264 rightmost (first in the reading order) glyph. */
14265 for (g = end + 1; g < glyph; g++)
14266 x += g->pixel_width;
14267 while (end < glyph
14268 && NILP ((end + 1)->object)
14269 && (end + 1)->charpos <= 0)
14270 ++end;
14271 glyph_before = glyph + 1;
14272 glyph_after = end;
14275 else if (row->reversed_p)
14277 /* In R2L rows that don't display text, put the cursor on the
14278 rightmost glyph. Case in point: an empty last line that is
14279 part of an R2L paragraph. */
14280 cursor = end - 1;
14281 /* Avoid placing the cursor on the last glyph of the row, where
14282 on terminal frames we hold the vertical border between
14283 adjacent windows. */
14284 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14285 && !WINDOW_RIGHTMOST_P (w)
14286 && cursor == row->glyphs[LAST_AREA] - 1)
14287 cursor--;
14288 x = -1; /* will be computed below, at label compute_x */
14291 /* Step 1: Try to find the glyph whose character position
14292 corresponds to point. If that's not possible, find 2 glyphs
14293 whose character positions are the closest to point, one before
14294 point, the other after it. */
14295 if (!row->reversed_p)
14296 while (/* not marched to end of glyph row */
14297 glyph < end
14298 /* glyph was not inserted by redisplay for internal purposes */
14299 && !NILP (glyph->object))
14301 if (BUFFERP (glyph->object))
14303 ptrdiff_t dpos = glyph->charpos - pt_old;
14305 if (glyph->charpos > bpos_max)
14306 bpos_max = glyph->charpos;
14307 if (glyph->charpos < bpos_min)
14308 bpos_min = glyph->charpos;
14309 if (!glyph->avoid_cursor_p)
14311 /* If we hit point, we've found the glyph on which to
14312 display the cursor. */
14313 if (dpos == 0)
14315 match_with_avoid_cursor = false;
14316 break;
14318 /* See if we've found a better approximation to
14319 POS_BEFORE or to POS_AFTER. */
14320 if (0 > dpos && dpos > pos_before - pt_old)
14322 pos_before = glyph->charpos;
14323 glyph_before = glyph;
14325 else if (0 < dpos && dpos < pos_after - pt_old)
14327 pos_after = glyph->charpos;
14328 glyph_after = glyph;
14331 else if (dpos == 0)
14332 match_with_avoid_cursor = true;
14334 else if (STRINGP (glyph->object))
14336 Lisp_Object chprop;
14337 ptrdiff_t glyph_pos = glyph->charpos;
14339 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14340 glyph->object);
14341 if (!NILP (chprop))
14343 /* If the string came from a `display' text property,
14344 look up the buffer position of that property and
14345 use that position to update bpos_max, as if we
14346 actually saw such a position in one of the row's
14347 glyphs. This helps with supporting integer values
14348 of `cursor' property on the display string in
14349 situations where most or all of the row's buffer
14350 text is completely covered by display properties,
14351 so that no glyph with valid buffer positions is
14352 ever seen in the row. */
14353 ptrdiff_t prop_pos =
14354 string_buffer_position_lim (glyph->object, pos_before,
14355 pos_after, false);
14357 if (prop_pos >= pos_before)
14358 bpos_max = prop_pos;
14360 if (INTEGERP (chprop))
14362 bpos_covered = bpos_max + XINT (chprop);
14363 /* If the `cursor' property covers buffer positions up
14364 to and including point, we should display cursor on
14365 this glyph. Note that, if a `cursor' property on one
14366 of the string's characters has an integer value, we
14367 will break out of the loop below _before_ we get to
14368 the position match above. IOW, integer values of
14369 the `cursor' property override the "exact match for
14370 point" strategy of positioning the cursor. */
14371 /* Implementation note: bpos_max == pt_old when, e.g.,
14372 we are in an empty line, where bpos_max is set to
14373 MATRIX_ROW_START_CHARPOS, see above. */
14374 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14376 cursor = glyph;
14377 break;
14381 string_seen = true;
14383 x += glyph->pixel_width;
14384 ++glyph;
14386 else if (glyph > end) /* row is reversed */
14387 while (!NILP (glyph->object))
14389 if (BUFFERP (glyph->object))
14391 ptrdiff_t dpos = glyph->charpos - pt_old;
14393 if (glyph->charpos > bpos_max)
14394 bpos_max = glyph->charpos;
14395 if (glyph->charpos < bpos_min)
14396 bpos_min = glyph->charpos;
14397 if (!glyph->avoid_cursor_p)
14399 if (dpos == 0)
14401 match_with_avoid_cursor = false;
14402 break;
14404 if (0 > dpos && dpos > pos_before - pt_old)
14406 pos_before = glyph->charpos;
14407 glyph_before = glyph;
14409 else if (0 < dpos && dpos < pos_after - pt_old)
14411 pos_after = glyph->charpos;
14412 glyph_after = glyph;
14415 else if (dpos == 0)
14416 match_with_avoid_cursor = true;
14418 else if (STRINGP (glyph->object))
14420 Lisp_Object chprop;
14421 ptrdiff_t glyph_pos = glyph->charpos;
14423 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14424 glyph->object);
14425 if (!NILP (chprop))
14427 ptrdiff_t prop_pos =
14428 string_buffer_position_lim (glyph->object, pos_before,
14429 pos_after, false);
14431 if (prop_pos >= pos_before)
14432 bpos_max = prop_pos;
14434 if (INTEGERP (chprop))
14436 bpos_covered = bpos_max + XINT (chprop);
14437 /* If the `cursor' property covers buffer positions up
14438 to and including point, we should display cursor on
14439 this glyph. */
14440 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14442 cursor = glyph;
14443 break;
14446 string_seen = true;
14448 --glyph;
14449 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14451 x--; /* can't use any pixel_width */
14452 break;
14454 x -= glyph->pixel_width;
14457 /* Step 2: If we didn't find an exact match for point, we need to
14458 look for a proper place to put the cursor among glyphs between
14459 GLYPH_BEFORE and GLYPH_AFTER. */
14460 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14461 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14462 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14464 /* An empty line has a single glyph whose OBJECT is nil and
14465 whose CHARPOS is the position of a newline on that line.
14466 Note that on a TTY, there are more glyphs after that, which
14467 were produced by extend_face_to_end_of_line, but their
14468 CHARPOS is zero or negative. */
14469 bool empty_line_p =
14470 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14471 && NILP (glyph->object) && glyph->charpos > 0
14472 /* On a TTY, continued and truncated rows also have a glyph at
14473 their end whose OBJECT is nil and whose CHARPOS is
14474 positive (the continuation and truncation glyphs), but such
14475 rows are obviously not "empty". */
14476 && !(row->continued_p || row->truncated_on_right_p));
14478 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14480 ptrdiff_t ellipsis_pos;
14482 /* Scan back over the ellipsis glyphs. */
14483 if (!row->reversed_p)
14485 ellipsis_pos = (glyph - 1)->charpos;
14486 while (glyph > row->glyphs[TEXT_AREA]
14487 && (glyph - 1)->charpos == ellipsis_pos)
14488 glyph--, x -= glyph->pixel_width;
14489 /* That loop always goes one position too far, including
14490 the glyph before the ellipsis. So scan forward over
14491 that one. */
14492 x += glyph->pixel_width;
14493 glyph++;
14495 else /* row is reversed */
14497 ellipsis_pos = (glyph + 1)->charpos;
14498 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14499 && (glyph + 1)->charpos == ellipsis_pos)
14500 glyph++, x += glyph->pixel_width;
14501 x -= glyph->pixel_width;
14502 glyph--;
14505 else if (match_with_avoid_cursor)
14507 cursor = glyph_after;
14508 x = -1;
14510 else if (string_seen)
14512 int incr = row->reversed_p ? -1 : +1;
14514 /* Need to find the glyph that came out of a string which is
14515 present at point. That glyph is somewhere between
14516 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14517 positioned between POS_BEFORE and POS_AFTER in the
14518 buffer. */
14519 struct glyph *start, *stop;
14520 ptrdiff_t pos = pos_before;
14522 x = -1;
14524 /* If the row ends in a newline from a display string,
14525 reordering could have moved the glyphs belonging to the
14526 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14527 in this case we extend the search to the last glyph in
14528 the row that was not inserted by redisplay. */
14529 if (row->ends_in_newline_from_string_p)
14531 glyph_after = end;
14532 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14535 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14536 correspond to POS_BEFORE and POS_AFTER, respectively. We
14537 need START and STOP in the order that corresponds to the
14538 row's direction as given by its reversed_p flag. If the
14539 directionality of characters between POS_BEFORE and
14540 POS_AFTER is the opposite of the row's base direction,
14541 these characters will have been reordered for display,
14542 and we need to reverse START and STOP. */
14543 if (!row->reversed_p)
14545 start = min (glyph_before, glyph_after);
14546 stop = max (glyph_before, glyph_after);
14548 else
14550 start = max (glyph_before, glyph_after);
14551 stop = min (glyph_before, glyph_after);
14553 for (glyph = start + incr;
14554 row->reversed_p ? glyph > stop : glyph < stop; )
14557 /* Any glyphs that come from the buffer are here because
14558 of bidi reordering. Skip them, and only pay
14559 attention to glyphs that came from some string. */
14560 if (STRINGP (glyph->object))
14562 Lisp_Object str;
14563 ptrdiff_t tem;
14564 /* If the display property covers the newline, we
14565 need to search for it one position farther. */
14566 ptrdiff_t lim = pos_after
14567 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14569 string_from_text_prop = false;
14570 str = glyph->object;
14571 tem = string_buffer_position_lim (str, pos, lim, false);
14572 if (tem == 0 /* from overlay */
14573 || pos <= tem)
14575 /* If the string from which this glyph came is
14576 found in the buffer at point, or at position
14577 that is closer to point than pos_after, then
14578 we've found the glyph we've been looking for.
14579 If it comes from an overlay (tem == 0), and
14580 it has the `cursor' property on one of its
14581 glyphs, record that glyph as a candidate for
14582 displaying the cursor. (As in the
14583 unidirectional version, we will display the
14584 cursor on the last candidate we find.) */
14585 if (tem == 0
14586 || tem == pt_old
14587 || (tem - pt_old > 0 && tem < pos_after))
14589 /* The glyphs from this string could have
14590 been reordered. Find the one with the
14591 smallest string position. Or there could
14592 be a character in the string with the
14593 `cursor' property, which means display
14594 cursor on that character's glyph. */
14595 ptrdiff_t strpos = glyph->charpos;
14597 if (tem)
14599 cursor = glyph;
14600 string_from_text_prop = true;
14602 for ( ;
14603 (row->reversed_p ? glyph > stop : glyph < stop)
14604 && EQ (glyph->object, str);
14605 glyph += incr)
14607 Lisp_Object cprop;
14608 ptrdiff_t gpos = glyph->charpos;
14610 cprop = Fget_char_property (make_number (gpos),
14611 Qcursor,
14612 glyph->object);
14613 if (!NILP (cprop))
14615 cursor = glyph;
14616 break;
14618 if (tem && glyph->charpos < strpos)
14620 strpos = glyph->charpos;
14621 cursor = glyph;
14625 if (tem == pt_old
14626 || (tem - pt_old > 0 && tem < pos_after))
14627 goto compute_x;
14629 if (tem)
14630 pos = tem + 1; /* don't find previous instances */
14632 /* This string is not what we want; skip all of the
14633 glyphs that came from it. */
14634 while ((row->reversed_p ? glyph > stop : glyph < stop)
14635 && EQ (glyph->object, str))
14636 glyph += incr;
14638 else
14639 glyph += incr;
14642 /* If we reached the end of the line, and END was from a string,
14643 the cursor is not on this line. */
14644 if (cursor == NULL
14645 && (row->reversed_p ? glyph <= end : glyph >= end)
14646 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14647 && STRINGP (end->object)
14648 && row->continued_p)
14649 return false;
14651 /* A truncated row may not include PT among its character positions.
14652 Setting the cursor inside the scroll margin will trigger
14653 recalculation of hscroll in hscroll_window_tree. But if a
14654 display string covers point, defer to the string-handling
14655 code below to figure this out. */
14656 else if (row->truncated_on_left_p && pt_old < bpos_min)
14658 cursor = glyph_before;
14659 x = -1;
14661 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14662 /* Zero-width characters produce no glyphs. */
14663 || (!empty_line_p
14664 && (row->reversed_p
14665 ? glyph_after > glyphs_end
14666 : glyph_after < glyphs_end)))
14668 cursor = glyph_after;
14669 x = -1;
14673 compute_x:
14674 if (cursor != NULL)
14675 glyph = cursor;
14676 else if (glyph == glyphs_end
14677 && pos_before == pos_after
14678 && STRINGP ((row->reversed_p
14679 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14680 : row->glyphs[TEXT_AREA])->object))
14682 /* If all the glyphs of this row came from strings, put the
14683 cursor on the first glyph of the row. This avoids having the
14684 cursor outside of the text area in this very rare and hard
14685 use case. */
14686 glyph =
14687 row->reversed_p
14688 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14689 : row->glyphs[TEXT_AREA];
14691 if (x < 0)
14693 struct glyph *g;
14695 /* Need to compute x that corresponds to GLYPH. */
14696 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14698 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14699 emacs_abort ();
14700 x += g->pixel_width;
14704 /* ROW could be part of a continued line, which, under bidi
14705 reordering, might have other rows whose start and end charpos
14706 occlude point. Only set w->cursor if we found a better
14707 approximation to the cursor position than we have from previously
14708 examined candidate rows belonging to the same continued line. */
14709 if (/* We already have a candidate row. */
14710 w->cursor.vpos >= 0
14711 /* That candidate is not the row we are processing. */
14712 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14713 /* Make sure cursor.vpos specifies a row whose start and end
14714 charpos occlude point, and it is valid candidate for being a
14715 cursor-row. This is because some callers of this function
14716 leave cursor.vpos at the row where the cursor was displayed
14717 during the last redisplay cycle. */
14718 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14719 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14720 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14722 struct glyph *g1
14723 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14725 /* Don't consider glyphs that are outside TEXT_AREA. */
14726 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14727 return false;
14728 /* Keep the candidate whose buffer position is the closest to
14729 point or has the `cursor' property. */
14730 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14731 w->cursor.hpos >= 0
14732 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14733 && ((BUFFERP (g1->object)
14734 && (g1->charpos == pt_old /* An exact match always wins. */
14735 || (BUFFERP (glyph->object)
14736 && eabs (g1->charpos - pt_old)
14737 < eabs (glyph->charpos - pt_old))))
14738 /* Previous candidate is a glyph from a string that has
14739 a non-nil `cursor' property. */
14740 || (STRINGP (g1->object)
14741 && (!NILP (Fget_char_property (make_number (g1->charpos),
14742 Qcursor, g1->object))
14743 /* Previous candidate is from the same display
14744 string as this one, and the display string
14745 came from a text property. */
14746 || (EQ (g1->object, glyph->object)
14747 && string_from_text_prop)
14748 /* this candidate is from newline and its
14749 position is not an exact match */
14750 || (NILP (glyph->object)
14751 && glyph->charpos != pt_old)))))
14752 return false;
14753 /* If this candidate gives an exact match, use that. */
14754 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14755 /* If this candidate is a glyph created for the
14756 terminating newline of a line, and point is on that
14757 newline, it wins because it's an exact match. */
14758 || (!row->continued_p
14759 && NILP (glyph->object)
14760 && glyph->charpos == 0
14761 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14762 /* Otherwise, keep the candidate that comes from a row
14763 spanning less buffer positions. This may win when one or
14764 both candidate positions are on glyphs that came from
14765 display strings, for which we cannot compare buffer
14766 positions. */
14767 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14768 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14769 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14770 return false;
14772 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14773 w->cursor.x = x;
14774 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14775 w->cursor.y = row->y + dy;
14777 if (w == XWINDOW (selected_window))
14779 if (!row->continued_p
14780 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14781 && row->x == 0)
14783 this_line_buffer = XBUFFER (w->contents);
14785 CHARPOS (this_line_start_pos)
14786 = MATRIX_ROW_START_CHARPOS (row) + delta;
14787 BYTEPOS (this_line_start_pos)
14788 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14790 CHARPOS (this_line_end_pos)
14791 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14792 BYTEPOS (this_line_end_pos)
14793 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14795 this_line_y = w->cursor.y;
14796 this_line_pixel_height = row->height;
14797 this_line_vpos = w->cursor.vpos;
14798 this_line_start_x = row->x;
14800 else
14801 CHARPOS (this_line_start_pos) = 0;
14804 return true;
14808 /* Run window scroll functions, if any, for WINDOW with new window
14809 start STARTP. Sets the window start of WINDOW to that position.
14811 We assume that the window's buffer is really current. */
14813 static struct text_pos
14814 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14816 struct window *w = XWINDOW (window);
14817 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14819 eassert (current_buffer == XBUFFER (w->contents));
14821 if (!NILP (Vwindow_scroll_functions))
14823 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14824 make_number (CHARPOS (startp)));
14825 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14826 /* In case the hook functions switch buffers. */
14827 set_buffer_internal (XBUFFER (w->contents));
14830 return startp;
14834 /* Make sure the line containing the cursor is fully visible.
14835 A value of true means there is nothing to be done.
14836 (Either the line is fully visible, or it cannot be made so,
14837 or we cannot tell.)
14839 If FORCE_P, return false even if partial visible cursor row
14840 is higher than window.
14842 If CURRENT_MATRIX_P, use the information from the
14843 window's current glyph matrix; otherwise use the desired glyph
14844 matrix.
14846 A value of false means the caller should do scrolling
14847 as if point had gone off the screen. */
14849 static bool
14850 cursor_row_fully_visible_p (struct window *w, bool force_p,
14851 bool current_matrix_p)
14853 struct glyph_matrix *matrix;
14854 struct glyph_row *row;
14855 int window_height;
14857 if (!make_cursor_line_fully_visible_p)
14858 return true;
14860 /* It's not always possible to find the cursor, e.g, when a window
14861 is full of overlay strings. Don't do anything in that case. */
14862 if (w->cursor.vpos < 0)
14863 return true;
14865 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14866 row = MATRIX_ROW (matrix, w->cursor.vpos);
14868 /* If the cursor row is not partially visible, there's nothing to do. */
14869 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14870 return true;
14872 /* If the row the cursor is in is taller than the window's height,
14873 it's not clear what to do, so do nothing. */
14874 window_height = window_box_height (w);
14875 if (row->height >= window_height)
14877 if (!force_p || MINI_WINDOW_P (w)
14878 || w->vscroll || w->cursor.vpos == 0)
14879 return true;
14881 return false;
14885 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14886 means only WINDOW is redisplayed in redisplay_internal.
14887 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14888 in redisplay_window to bring a partially visible line into view in
14889 the case that only the cursor has moved.
14891 LAST_LINE_MISFIT should be true if we're scrolling because the
14892 last screen line's vertical height extends past the end of the screen.
14894 Value is
14896 1 if scrolling succeeded
14898 0 if scrolling didn't find point.
14900 -1 if new fonts have been loaded so that we must interrupt
14901 redisplay, adjust glyph matrices, and try again. */
14903 enum
14905 SCROLLING_SUCCESS,
14906 SCROLLING_FAILED,
14907 SCROLLING_NEED_LARGER_MATRICES
14910 /* If scroll-conservatively is more than this, never recenter.
14912 If you change this, don't forget to update the doc string of
14913 `scroll-conservatively' and the Emacs manual. */
14914 #define SCROLL_LIMIT 100
14916 static int
14917 try_scrolling (Lisp_Object window, bool just_this_one_p,
14918 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14919 bool temp_scroll_step, bool last_line_misfit)
14921 struct window *w = XWINDOW (window);
14922 struct frame *f = XFRAME (w->frame);
14923 struct text_pos pos, startp;
14924 struct it it;
14925 int this_scroll_margin, scroll_max, rc, height;
14926 int dy = 0, amount_to_scroll = 0;
14927 bool scroll_down_p = false;
14928 int extra_scroll_margin_lines = last_line_misfit;
14929 Lisp_Object aggressive;
14930 /* We will never try scrolling more than this number of lines. */
14931 int scroll_limit = SCROLL_LIMIT;
14932 int frame_line_height = default_line_pixel_height (w);
14933 int window_total_lines
14934 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14936 #ifdef GLYPH_DEBUG
14937 debug_method_add (w, "try_scrolling");
14938 #endif
14940 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14942 /* Compute scroll margin height in pixels. We scroll when point is
14943 within this distance from the top or bottom of the window. */
14944 if (scroll_margin > 0)
14945 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14946 * frame_line_height;
14947 else
14948 this_scroll_margin = 0;
14950 /* Force arg_scroll_conservatively to have a reasonable value, to
14951 avoid scrolling too far away with slow move_it_* functions. Note
14952 that the user can supply scroll-conservatively equal to
14953 `most-positive-fixnum', which can be larger than INT_MAX. */
14954 if (arg_scroll_conservatively > scroll_limit)
14956 arg_scroll_conservatively = scroll_limit + 1;
14957 scroll_max = scroll_limit * frame_line_height;
14959 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14960 /* Compute how much we should try to scroll maximally to bring
14961 point into view. */
14962 scroll_max = (max (scroll_step,
14963 max (arg_scroll_conservatively, temp_scroll_step))
14964 * frame_line_height);
14965 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14966 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14967 /* We're trying to scroll because of aggressive scrolling but no
14968 scroll_step is set. Choose an arbitrary one. */
14969 scroll_max = 10 * frame_line_height;
14970 else
14971 scroll_max = 0;
14973 too_near_end:
14975 /* Decide whether to scroll down. */
14976 if (PT > CHARPOS (startp))
14978 int scroll_margin_y;
14980 /* Compute the pixel ypos of the scroll margin, then move IT to
14981 either that ypos or PT, whichever comes first. */
14982 start_display (&it, w, startp);
14983 scroll_margin_y = it.last_visible_y - this_scroll_margin
14984 - frame_line_height * extra_scroll_margin_lines;
14985 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14986 (MOVE_TO_POS | MOVE_TO_Y));
14988 if (PT > CHARPOS (it.current.pos))
14990 int y0 = line_bottom_y (&it);
14991 /* Compute how many pixels below window bottom to stop searching
14992 for PT. This avoids costly search for PT that is far away if
14993 the user limited scrolling by a small number of lines, but
14994 always finds PT if scroll_conservatively is set to a large
14995 number, such as most-positive-fixnum. */
14996 int slack = max (scroll_max, 10 * frame_line_height);
14997 int y_to_move = it.last_visible_y + slack;
14999 /* Compute the distance from the scroll margin to PT or to
15000 the scroll limit, whichever comes first. This should
15001 include the height of the cursor line, to make that line
15002 fully visible. */
15003 move_it_to (&it, PT, -1, y_to_move,
15004 -1, MOVE_TO_POS | MOVE_TO_Y);
15005 dy = line_bottom_y (&it) - y0;
15007 if (dy > scroll_max)
15008 return SCROLLING_FAILED;
15010 if (dy > 0)
15011 scroll_down_p = true;
15015 if (scroll_down_p)
15017 /* Point is in or below the bottom scroll margin, so move the
15018 window start down. If scrolling conservatively, move it just
15019 enough down to make point visible. If scroll_step is set,
15020 move it down by scroll_step. */
15021 if (arg_scroll_conservatively)
15022 amount_to_scroll
15023 = min (max (dy, frame_line_height),
15024 frame_line_height * arg_scroll_conservatively);
15025 else if (scroll_step || temp_scroll_step)
15026 amount_to_scroll = scroll_max;
15027 else
15029 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15030 height = WINDOW_BOX_TEXT_HEIGHT (w);
15031 if (NUMBERP (aggressive))
15033 double float_amount = XFLOATINT (aggressive) * height;
15034 int aggressive_scroll = float_amount;
15035 if (aggressive_scroll == 0 && float_amount > 0)
15036 aggressive_scroll = 1;
15037 /* Don't let point enter the scroll margin near top of
15038 the window. This could happen if the value of
15039 scroll_up_aggressively is too large and there are
15040 non-zero margins, because scroll_up_aggressively
15041 means put point that fraction of window height
15042 _from_the_bottom_margin_. */
15043 if (aggressive_scroll + 2 * this_scroll_margin > height)
15044 aggressive_scroll = height - 2 * this_scroll_margin;
15045 amount_to_scroll = dy + aggressive_scroll;
15049 if (amount_to_scroll <= 0)
15050 return SCROLLING_FAILED;
15052 start_display (&it, w, startp);
15053 if (arg_scroll_conservatively <= scroll_limit)
15054 move_it_vertically (&it, amount_to_scroll);
15055 else
15057 /* Extra precision for users who set scroll-conservatively
15058 to a large number: make sure the amount we scroll
15059 the window start is never less than amount_to_scroll,
15060 which was computed as distance from window bottom to
15061 point. This matters when lines at window top and lines
15062 below window bottom have different height. */
15063 struct it it1;
15064 void *it1data = NULL;
15065 /* We use a temporary it1 because line_bottom_y can modify
15066 its argument, if it moves one line down; see there. */
15067 int start_y;
15069 SAVE_IT (it1, it, it1data);
15070 start_y = line_bottom_y (&it1);
15071 do {
15072 RESTORE_IT (&it, &it, it1data);
15073 move_it_by_lines (&it, 1);
15074 SAVE_IT (it1, it, it1data);
15075 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15078 /* If STARTP is unchanged, move it down another screen line. */
15079 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15080 move_it_by_lines (&it, 1);
15081 startp = it.current.pos;
15083 else
15085 struct text_pos scroll_margin_pos = startp;
15086 int y_offset = 0;
15088 /* See if point is inside the scroll margin at the top of the
15089 window. */
15090 if (this_scroll_margin)
15092 int y_start;
15094 start_display (&it, w, startp);
15095 y_start = it.current_y;
15096 move_it_vertically (&it, this_scroll_margin);
15097 scroll_margin_pos = it.current.pos;
15098 /* If we didn't move enough before hitting ZV, request
15099 additional amount of scroll, to move point out of the
15100 scroll margin. */
15101 if (IT_CHARPOS (it) == ZV
15102 && it.current_y - y_start < this_scroll_margin)
15103 y_offset = this_scroll_margin - (it.current_y - y_start);
15106 if (PT < CHARPOS (scroll_margin_pos))
15108 /* Point is in the scroll margin at the top of the window or
15109 above what is displayed in the window. */
15110 int y0, y_to_move;
15112 /* Compute the vertical distance from PT to the scroll
15113 margin position. Move as far as scroll_max allows, or
15114 one screenful, or 10 screen lines, whichever is largest.
15115 Give up if distance is greater than scroll_max or if we
15116 didn't reach the scroll margin position. */
15117 SET_TEXT_POS (pos, PT, PT_BYTE);
15118 start_display (&it, w, pos);
15119 y0 = it.current_y;
15120 y_to_move = max (it.last_visible_y,
15121 max (scroll_max, 10 * frame_line_height));
15122 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15123 y_to_move, -1,
15124 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15125 dy = it.current_y - y0;
15126 if (dy > scroll_max
15127 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15128 return SCROLLING_FAILED;
15130 /* Additional scroll for when ZV was too close to point. */
15131 dy += y_offset;
15133 /* Compute new window start. */
15134 start_display (&it, w, startp);
15136 if (arg_scroll_conservatively)
15137 amount_to_scroll = max (dy, frame_line_height
15138 * max (scroll_step, temp_scroll_step));
15139 else if (scroll_step || temp_scroll_step)
15140 amount_to_scroll = scroll_max;
15141 else
15143 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15144 height = WINDOW_BOX_TEXT_HEIGHT (w);
15145 if (NUMBERP (aggressive))
15147 double float_amount = XFLOATINT (aggressive) * height;
15148 int aggressive_scroll = float_amount;
15149 if (aggressive_scroll == 0 && float_amount > 0)
15150 aggressive_scroll = 1;
15151 /* Don't let point enter the scroll margin near
15152 bottom of the window, if the value of
15153 scroll_down_aggressively happens to be too
15154 large. */
15155 if (aggressive_scroll + 2 * this_scroll_margin > height)
15156 aggressive_scroll = height - 2 * this_scroll_margin;
15157 amount_to_scroll = dy + aggressive_scroll;
15161 if (amount_to_scroll <= 0)
15162 return SCROLLING_FAILED;
15164 move_it_vertically_backward (&it, amount_to_scroll);
15165 startp = it.current.pos;
15169 /* Run window scroll functions. */
15170 startp = run_window_scroll_functions (window, startp);
15172 /* Display the window. Give up if new fonts are loaded, or if point
15173 doesn't appear. */
15174 if (!try_window (window, startp, 0))
15175 rc = SCROLLING_NEED_LARGER_MATRICES;
15176 else if (w->cursor.vpos < 0)
15178 clear_glyph_matrix (w->desired_matrix);
15179 rc = SCROLLING_FAILED;
15181 else
15183 /* Maybe forget recorded base line for line number display. */
15184 if (!just_this_one_p
15185 || current_buffer->clip_changed
15186 || BEG_UNCHANGED < CHARPOS (startp))
15187 w->base_line_number = 0;
15189 /* If cursor ends up on a partially visible line,
15190 treat that as being off the bottom of the screen. */
15191 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15192 false)
15193 /* It's possible that the cursor is on the first line of the
15194 buffer, which is partially obscured due to a vscroll
15195 (Bug#7537). In that case, avoid looping forever. */
15196 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15198 clear_glyph_matrix (w->desired_matrix);
15199 ++extra_scroll_margin_lines;
15200 goto too_near_end;
15202 rc = SCROLLING_SUCCESS;
15205 return rc;
15209 /* Compute a suitable window start for window W if display of W starts
15210 on a continuation line. Value is true if a new window start
15211 was computed.
15213 The new window start will be computed, based on W's width, starting
15214 from the start of the continued line. It is the start of the
15215 screen line with the minimum distance from the old start W->start. */
15217 static bool
15218 compute_window_start_on_continuation_line (struct window *w)
15220 struct text_pos pos, start_pos;
15221 bool window_start_changed_p = false;
15223 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15225 /* If window start is on a continuation line... Window start may be
15226 < BEGV in case there's invisible text at the start of the
15227 buffer (M-x rmail, for example). */
15228 if (CHARPOS (start_pos) > BEGV
15229 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15231 struct it it;
15232 struct glyph_row *row;
15234 /* Handle the case that the window start is out of range. */
15235 if (CHARPOS (start_pos) < BEGV)
15236 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15237 else if (CHARPOS (start_pos) > ZV)
15238 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15240 /* Find the start of the continued line. This should be fast
15241 because find_newline is fast (newline cache). */
15242 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15243 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15244 row, DEFAULT_FACE_ID);
15245 reseat_at_previous_visible_line_start (&it);
15247 /* If the line start is "too far" away from the window start,
15248 say it takes too much time to compute a new window start. */
15249 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15250 /* PXW: Do we need upper bounds here? */
15251 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15253 int min_distance, distance;
15255 /* Move forward by display lines to find the new window
15256 start. If window width was enlarged, the new start can
15257 be expected to be > the old start. If window width was
15258 decreased, the new window start will be < the old start.
15259 So, we're looking for the display line start with the
15260 minimum distance from the old window start. */
15261 pos = it.current.pos;
15262 min_distance = INFINITY;
15263 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15264 distance < min_distance)
15266 min_distance = distance;
15267 pos = it.current.pos;
15268 if (it.line_wrap == WORD_WRAP)
15270 /* Under WORD_WRAP, move_it_by_lines is likely to
15271 overshoot and stop not at the first, but the
15272 second character from the left margin. So in
15273 that case, we need a more tight control on the X
15274 coordinate of the iterator than move_it_by_lines
15275 promises in its contract. The method is to first
15276 go to the last (rightmost) visible character of a
15277 line, then move to the leftmost character on the
15278 next line in a separate call. */
15279 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15280 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15281 move_it_to (&it, ZV, 0,
15282 it.current_y + it.max_ascent + it.max_descent, -1,
15283 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15285 else
15286 move_it_by_lines (&it, 1);
15289 /* Set the window start there. */
15290 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15291 window_start_changed_p = true;
15295 return window_start_changed_p;
15299 /* Try cursor movement in case text has not changed in window WINDOW,
15300 with window start STARTP. Value is
15302 CURSOR_MOVEMENT_SUCCESS if successful
15304 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15306 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15307 display. *SCROLL_STEP is set to true, under certain circumstances, if
15308 we want to scroll as if scroll-step were set to 1. See the code.
15310 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15311 which case we have to abort this redisplay, and adjust matrices
15312 first. */
15314 enum
15316 CURSOR_MOVEMENT_SUCCESS,
15317 CURSOR_MOVEMENT_CANNOT_BE_USED,
15318 CURSOR_MOVEMENT_MUST_SCROLL,
15319 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15322 static int
15323 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15324 bool *scroll_step)
15326 struct window *w = XWINDOW (window);
15327 struct frame *f = XFRAME (w->frame);
15328 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15330 #ifdef GLYPH_DEBUG
15331 if (inhibit_try_cursor_movement)
15332 return rc;
15333 #endif
15335 /* Previously, there was a check for Lisp integer in the
15336 if-statement below. Now, this field is converted to
15337 ptrdiff_t, thus zero means invalid position in a buffer. */
15338 eassert (w->last_point > 0);
15339 /* Likewise there was a check whether window_end_vpos is nil or larger
15340 than the window. Now window_end_vpos is int and so never nil, but
15341 let's leave eassert to check whether it fits in the window. */
15342 eassert (!w->window_end_valid
15343 || w->window_end_vpos < w->current_matrix->nrows);
15345 /* Handle case where text has not changed, only point, and it has
15346 not moved off the frame. */
15347 if (/* Point may be in this window. */
15348 PT >= CHARPOS (startp)
15349 /* Selective display hasn't changed. */
15350 && !current_buffer->clip_changed
15351 /* Function force-mode-line-update is used to force a thorough
15352 redisplay. It sets either windows_or_buffers_changed or
15353 update_mode_lines. So don't take a shortcut here for these
15354 cases. */
15355 && !update_mode_lines
15356 && !windows_or_buffers_changed
15357 && !f->cursor_type_changed
15358 && NILP (Vshow_trailing_whitespace)
15359 /* This code is not used for mini-buffer for the sake of the case
15360 of redisplaying to replace an echo area message; since in
15361 that case the mini-buffer contents per se are usually
15362 unchanged. This code is of no real use in the mini-buffer
15363 since the handling of this_line_start_pos, etc., in redisplay
15364 handles the same cases. */
15365 && !EQ (window, minibuf_window)
15366 && (FRAME_WINDOW_P (f)
15367 || !overlay_arrow_in_current_buffer_p ()))
15369 int this_scroll_margin, top_scroll_margin;
15370 struct glyph_row *row = NULL;
15371 int frame_line_height = default_line_pixel_height (w);
15372 int window_total_lines
15373 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15375 #ifdef GLYPH_DEBUG
15376 debug_method_add (w, "cursor movement");
15377 #endif
15379 /* Scroll if point within this distance from the top or bottom
15380 of the window. This is a pixel value. */
15381 if (scroll_margin > 0)
15383 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15384 this_scroll_margin *= frame_line_height;
15386 else
15387 this_scroll_margin = 0;
15389 top_scroll_margin = this_scroll_margin;
15390 if (WINDOW_WANTS_HEADER_LINE_P (w))
15391 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15393 /* Start with the row the cursor was displayed during the last
15394 not paused redisplay. Give up if that row is not valid. */
15395 if (w->last_cursor_vpos < 0
15396 || w->last_cursor_vpos >= w->current_matrix->nrows)
15397 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15398 else
15400 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15401 if (row->mode_line_p)
15402 ++row;
15403 if (!row->enabled_p)
15404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15407 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15409 bool scroll_p = false, must_scroll = false;
15410 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15412 if (PT > w->last_point)
15414 /* Point has moved forward. */
15415 while (MATRIX_ROW_END_CHARPOS (row) < PT
15416 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15418 eassert (row->enabled_p);
15419 ++row;
15422 /* If the end position of a row equals the start
15423 position of the next row, and PT is at that position,
15424 we would rather display cursor in the next line. */
15425 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15426 && MATRIX_ROW_END_CHARPOS (row) == PT
15427 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15428 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15429 && !cursor_row_p (row))
15430 ++row;
15432 /* If within the scroll margin, scroll. Note that
15433 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15434 the next line would be drawn, and that
15435 this_scroll_margin can be zero. */
15436 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15437 || PT > MATRIX_ROW_END_CHARPOS (row)
15438 /* Line is completely visible last line in window
15439 and PT is to be set in the next line. */
15440 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15441 && PT == MATRIX_ROW_END_CHARPOS (row)
15442 && !row->ends_at_zv_p
15443 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15444 scroll_p = true;
15446 else if (PT < w->last_point)
15448 /* Cursor has to be moved backward. Note that PT >=
15449 CHARPOS (startp) because of the outer if-statement. */
15450 while (!row->mode_line_p
15451 && (MATRIX_ROW_START_CHARPOS (row) > PT
15452 || (MATRIX_ROW_START_CHARPOS (row) == PT
15453 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15454 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15455 row > w->current_matrix->rows
15456 && (row-1)->ends_in_newline_from_string_p))))
15457 && (row->y > top_scroll_margin
15458 || CHARPOS (startp) == BEGV))
15460 eassert (row->enabled_p);
15461 --row;
15464 /* Consider the following case: Window starts at BEGV,
15465 there is invisible, intangible text at BEGV, so that
15466 display starts at some point START > BEGV. It can
15467 happen that we are called with PT somewhere between
15468 BEGV and START. Try to handle that case. */
15469 if (row < w->current_matrix->rows
15470 || row->mode_line_p)
15472 row = w->current_matrix->rows;
15473 if (row->mode_line_p)
15474 ++row;
15477 /* Due to newlines in overlay strings, we may have to
15478 skip forward over overlay strings. */
15479 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15480 && MATRIX_ROW_END_CHARPOS (row) == PT
15481 && !cursor_row_p (row))
15482 ++row;
15484 /* If within the scroll margin, scroll. */
15485 if (row->y < top_scroll_margin
15486 && CHARPOS (startp) != BEGV)
15487 scroll_p = true;
15489 else
15491 /* Cursor did not move. So don't scroll even if cursor line
15492 is partially visible, as it was so before. */
15493 rc = CURSOR_MOVEMENT_SUCCESS;
15496 if (PT < MATRIX_ROW_START_CHARPOS (row)
15497 || PT > MATRIX_ROW_END_CHARPOS (row))
15499 /* if PT is not in the glyph row, give up. */
15500 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15501 must_scroll = true;
15503 else if (rc != CURSOR_MOVEMENT_SUCCESS
15504 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15506 struct glyph_row *row1;
15508 /* If rows are bidi-reordered and point moved, back up
15509 until we find a row that does not belong to a
15510 continuation line. This is because we must consider
15511 all rows of a continued line as candidates for the
15512 new cursor positioning, since row start and end
15513 positions change non-linearly with vertical position
15514 in such rows. */
15515 /* FIXME: Revisit this when glyph ``spilling'' in
15516 continuation lines' rows is implemented for
15517 bidi-reordered rows. */
15518 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15519 MATRIX_ROW_CONTINUATION_LINE_P (row);
15520 --row)
15522 /* If we hit the beginning of the displayed portion
15523 without finding the first row of a continued
15524 line, give up. */
15525 if (row <= row1)
15527 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15528 break;
15530 eassert (row->enabled_p);
15533 if (must_scroll)
15535 else if (rc != CURSOR_MOVEMENT_SUCCESS
15536 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15537 /* Make sure this isn't a header line by any chance, since
15538 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15539 && !row->mode_line_p
15540 && make_cursor_line_fully_visible_p)
15542 if (PT == MATRIX_ROW_END_CHARPOS (row)
15543 && !row->ends_at_zv_p
15544 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15545 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15546 else if (row->height > window_box_height (w))
15548 /* If we end up in a partially visible line, let's
15549 make it fully visible, except when it's taller
15550 than the window, in which case we can't do much
15551 about it. */
15552 *scroll_step = true;
15553 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15555 else
15557 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15558 if (!cursor_row_fully_visible_p (w, false, true))
15559 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15560 else
15561 rc = CURSOR_MOVEMENT_SUCCESS;
15564 else if (scroll_p)
15565 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15566 else if (rc != CURSOR_MOVEMENT_SUCCESS
15567 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15569 /* With bidi-reordered rows, there could be more than
15570 one candidate row whose start and end positions
15571 occlude point. We need to let set_cursor_from_row
15572 find the best candidate. */
15573 /* FIXME: Revisit this when glyph ``spilling'' in
15574 continuation lines' rows is implemented for
15575 bidi-reordered rows. */
15576 bool rv = false;
15580 bool at_zv_p = false, exact_match_p = false;
15582 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15583 && PT <= MATRIX_ROW_END_CHARPOS (row)
15584 && cursor_row_p (row))
15585 rv |= set_cursor_from_row (w, row, w->current_matrix,
15586 0, 0, 0, 0);
15587 /* As soon as we've found the exact match for point,
15588 or the first suitable row whose ends_at_zv_p flag
15589 is set, we are done. */
15590 if (rv)
15592 at_zv_p = MATRIX_ROW (w->current_matrix,
15593 w->cursor.vpos)->ends_at_zv_p;
15594 if (!at_zv_p
15595 && w->cursor.hpos >= 0
15596 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15597 w->cursor.vpos))
15599 struct glyph_row *candidate =
15600 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15601 struct glyph *g =
15602 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15603 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15605 exact_match_p =
15606 (BUFFERP (g->object) && g->charpos == PT)
15607 || (NILP (g->object)
15608 && (g->charpos == PT
15609 || (g->charpos == 0 && endpos - 1 == PT)));
15611 if (at_zv_p || exact_match_p)
15613 rc = CURSOR_MOVEMENT_SUCCESS;
15614 break;
15617 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15618 break;
15619 ++row;
15621 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15622 || row->continued_p)
15623 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15624 || (MATRIX_ROW_START_CHARPOS (row) == PT
15625 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15626 /* If we didn't find any candidate rows, or exited the
15627 loop before all the candidates were examined, signal
15628 to the caller that this method failed. */
15629 if (rc != CURSOR_MOVEMENT_SUCCESS
15630 && !(rv
15631 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15632 && !row->continued_p))
15633 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15634 else if (rv)
15635 rc = CURSOR_MOVEMENT_SUCCESS;
15637 else
15641 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15643 rc = CURSOR_MOVEMENT_SUCCESS;
15644 break;
15646 ++row;
15648 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15649 && MATRIX_ROW_START_CHARPOS (row) == PT
15650 && cursor_row_p (row));
15655 return rc;
15659 void
15660 set_vertical_scroll_bar (struct window *w)
15662 ptrdiff_t start, end, whole;
15664 /* Calculate the start and end positions for the current window.
15665 At some point, it would be nice to choose between scrollbars
15666 which reflect the whole buffer size, with special markers
15667 indicating narrowing, and scrollbars which reflect only the
15668 visible region.
15670 Note that mini-buffers sometimes aren't displaying any text. */
15671 if (!MINI_WINDOW_P (w)
15672 || (w == XWINDOW (minibuf_window)
15673 && NILP (echo_area_buffer[0])))
15675 struct buffer *buf = XBUFFER (w->contents);
15676 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15677 start = marker_position (w->start) - BUF_BEGV (buf);
15678 /* I don't think this is guaranteed to be right. For the
15679 moment, we'll pretend it is. */
15680 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15682 if (end < start)
15683 end = start;
15684 if (whole < (end - start))
15685 whole = end - start;
15687 else
15688 start = end = whole = 0;
15690 /* Indicate what this scroll bar ought to be displaying now. */
15691 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15692 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15693 (w, end - start, whole, start);
15697 void
15698 set_horizontal_scroll_bar (struct window *w)
15700 int start, end, whole, portion;
15702 if (!MINI_WINDOW_P (w)
15703 || (w == XWINDOW (minibuf_window)
15704 && NILP (echo_area_buffer[0])))
15706 struct buffer *b = XBUFFER (w->contents);
15707 struct buffer *old_buffer = NULL;
15708 struct it it;
15709 struct text_pos startp;
15711 if (b != current_buffer)
15713 old_buffer = current_buffer;
15714 set_buffer_internal (b);
15717 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15718 start_display (&it, w, startp);
15719 it.last_visible_x = INT_MAX;
15720 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15721 MOVE_TO_X | MOVE_TO_Y);
15722 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15723 window_box_height (w), -1,
15724 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15726 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15727 end = start + window_box_width (w, TEXT_AREA);
15728 portion = end - start;
15729 /* After enlarging a horizontally scrolled window such that it
15730 gets at least as wide as the text it contains, make sure that
15731 the thumb doesn't fill the entire scroll bar so we can still
15732 drag it back to see the entire text. */
15733 whole = max (whole, end);
15735 if (it.bidi_p)
15737 Lisp_Object pdir;
15739 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15740 if (EQ (pdir, Qright_to_left))
15742 start = whole - end;
15743 end = start + portion;
15747 if (old_buffer)
15748 set_buffer_internal (old_buffer);
15750 else
15751 start = end = whole = portion = 0;
15753 w->hscroll_whole = whole;
15755 /* Indicate what this scroll bar ought to be displaying now. */
15756 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15757 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15758 (w, portion, whole, start);
15762 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15763 selected_window is redisplayed.
15765 We can return without actually redisplaying the window if fonts has been
15766 changed on window's frame. In that case, redisplay_internal will retry.
15768 As one of the important parts of redisplaying a window, we need to
15769 decide whether the previous window-start position (stored in the
15770 window's w->start marker position) is still valid, and if it isn't,
15771 recompute it. Some details about that:
15773 . The previous window-start could be in a continuation line, in
15774 which case we need to recompute it when the window width
15775 changes. See compute_window_start_on_continuation_line and its
15776 call below.
15778 . The text that changed since last redisplay could include the
15779 previous window-start position. In that case, we try to salvage
15780 what we can from the current glyph matrix by calling
15781 try_scrolling, which see.
15783 . Some Emacs command could force us to use a specific window-start
15784 position by setting the window's force_start flag, or gently
15785 propose doing that by setting the window's optional_new_start
15786 flag. In these cases, we try using the specified start point if
15787 that succeeds (i.e. the window desired matrix is successfully
15788 recomputed, and point location is within the window). In case
15789 of optional_new_start, we first check if the specified start
15790 position is feasible, i.e. if it will allow point to be
15791 displayed in the window. If using the specified start point
15792 fails, e.g., if new fonts are needed to be loaded, we abort the
15793 redisplay cycle and leave it up to the next cycle to figure out
15794 things.
15796 . Note that the window's force_start flag is sometimes set by
15797 redisplay itself, when it decides that the previous window start
15798 point is fine and should be kept. Search for "goto force_start"
15799 below to see the details. Like the values of window-start
15800 specified outside of redisplay, these internally-deduced values
15801 are tested for feasibility, and ignored if found to be
15802 unfeasible.
15804 . Note that the function try_window, used to completely redisplay
15805 a window, accepts the window's start point as its argument.
15806 This is used several times in the redisplay code to control
15807 where the window start will be, according to user options such
15808 as scroll-conservatively, and also to ensure the screen line
15809 showing point will be fully (as opposed to partially) visible on
15810 display. */
15812 static void
15813 redisplay_window (Lisp_Object window, bool just_this_one_p)
15815 struct window *w = XWINDOW (window);
15816 struct frame *f = XFRAME (w->frame);
15817 struct buffer *buffer = XBUFFER (w->contents);
15818 struct buffer *old = current_buffer;
15819 struct text_pos lpoint, opoint, startp;
15820 bool update_mode_line;
15821 int tem;
15822 struct it it;
15823 /* Record it now because it's overwritten. */
15824 bool current_matrix_up_to_date_p = false;
15825 bool used_current_matrix_p = false;
15826 /* This is less strict than current_matrix_up_to_date_p.
15827 It indicates that the buffer contents and narrowing are unchanged. */
15828 bool buffer_unchanged_p = false;
15829 bool temp_scroll_step = false;
15830 ptrdiff_t count = SPECPDL_INDEX ();
15831 int rc;
15832 int centering_position = -1;
15833 bool last_line_misfit = false;
15834 ptrdiff_t beg_unchanged, end_unchanged;
15835 int frame_line_height;
15837 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15838 opoint = lpoint;
15840 #ifdef GLYPH_DEBUG
15841 *w->desired_matrix->method = 0;
15842 #endif
15844 if (!just_this_one_p
15845 && REDISPLAY_SOME_P ()
15846 && !w->redisplay
15847 && !w->update_mode_line
15848 && !f->redisplay
15849 && !buffer->text->redisplay
15850 && BUF_PT (buffer) == w->last_point)
15851 return;
15853 /* Make sure that both W's markers are valid. */
15854 eassert (XMARKER (w->start)->buffer == buffer);
15855 eassert (XMARKER (w->pointm)->buffer == buffer);
15857 /* We come here again if we need to run window-text-change-functions
15858 below. */
15859 restart:
15860 reconsider_clip_changes (w);
15861 frame_line_height = default_line_pixel_height (w);
15863 /* Has the mode line to be updated? */
15864 update_mode_line = (w->update_mode_line
15865 || update_mode_lines
15866 || buffer->clip_changed
15867 || buffer->prevent_redisplay_optimizations_p);
15869 if (!just_this_one_p)
15870 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15871 cleverly elsewhere. */
15872 w->must_be_updated_p = true;
15874 if (MINI_WINDOW_P (w))
15876 if (w == XWINDOW (echo_area_window)
15877 && !NILP (echo_area_buffer[0]))
15879 if (update_mode_line)
15880 /* We may have to update a tty frame's menu bar or a
15881 tool-bar. Example `M-x C-h C-h C-g'. */
15882 goto finish_menu_bars;
15883 else
15884 /* We've already displayed the echo area glyphs in this window. */
15885 goto finish_scroll_bars;
15887 else if ((w != XWINDOW (minibuf_window)
15888 || minibuf_level == 0)
15889 /* When buffer is nonempty, redisplay window normally. */
15890 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15891 /* Quail displays non-mini buffers in minibuffer window.
15892 In that case, redisplay the window normally. */
15893 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15895 /* W is a mini-buffer window, but it's not active, so clear
15896 it. */
15897 int yb = window_text_bottom_y (w);
15898 struct glyph_row *row;
15899 int y;
15901 for (y = 0, row = w->desired_matrix->rows;
15902 y < yb;
15903 y += row->height, ++row)
15904 blank_row (w, row, y);
15905 goto finish_scroll_bars;
15908 clear_glyph_matrix (w->desired_matrix);
15911 /* Otherwise set up data on this window; select its buffer and point
15912 value. */
15913 /* Really select the buffer, for the sake of buffer-local
15914 variables. */
15915 set_buffer_internal_1 (XBUFFER (w->contents));
15917 current_matrix_up_to_date_p
15918 = (w->window_end_valid
15919 && !current_buffer->clip_changed
15920 && !current_buffer->prevent_redisplay_optimizations_p
15921 && !window_outdated (w));
15923 /* Run the window-text-change-functions
15924 if it is possible that the text on the screen has changed
15925 (either due to modification of the text, or any other reason). */
15926 if (!current_matrix_up_to_date_p
15927 && !NILP (Vwindow_text_change_functions))
15929 safe_run_hooks (Qwindow_text_change_functions);
15930 goto restart;
15933 beg_unchanged = BEG_UNCHANGED;
15934 end_unchanged = END_UNCHANGED;
15936 SET_TEXT_POS (opoint, PT, PT_BYTE);
15938 specbind (Qinhibit_point_motion_hooks, Qt);
15940 buffer_unchanged_p
15941 = (w->window_end_valid
15942 && !current_buffer->clip_changed
15943 && !window_outdated (w));
15945 /* When windows_or_buffers_changed is non-zero, we can't rely
15946 on the window end being valid, so set it to zero there. */
15947 if (windows_or_buffers_changed)
15949 /* If window starts on a continuation line, maybe adjust the
15950 window start in case the window's width changed. */
15951 if (XMARKER (w->start)->buffer == current_buffer)
15952 compute_window_start_on_continuation_line (w);
15954 w->window_end_valid = false;
15955 /* If so, we also can't rely on current matrix
15956 and should not fool try_cursor_movement below. */
15957 current_matrix_up_to_date_p = false;
15960 /* Some sanity checks. */
15961 CHECK_WINDOW_END (w);
15962 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15963 emacs_abort ();
15964 if (BYTEPOS (opoint) < CHARPOS (opoint))
15965 emacs_abort ();
15967 if (mode_line_update_needed (w))
15968 update_mode_line = true;
15970 /* Point refers normally to the selected window. For any other
15971 window, set up appropriate value. */
15972 if (!EQ (window, selected_window))
15974 ptrdiff_t new_pt = marker_position (w->pointm);
15975 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15977 if (new_pt < BEGV)
15979 new_pt = BEGV;
15980 new_pt_byte = BEGV_BYTE;
15981 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15983 else if (new_pt > (ZV - 1))
15985 new_pt = ZV;
15986 new_pt_byte = ZV_BYTE;
15987 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15990 /* We don't use SET_PT so that the point-motion hooks don't run. */
15991 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15994 /* If any of the character widths specified in the display table
15995 have changed, invalidate the width run cache. It's true that
15996 this may be a bit late to catch such changes, but the rest of
15997 redisplay goes (non-fatally) haywire when the display table is
15998 changed, so why should we worry about doing any better? */
15999 if (current_buffer->width_run_cache
16000 || (current_buffer->base_buffer
16001 && current_buffer->base_buffer->width_run_cache))
16003 struct Lisp_Char_Table *disptab = buffer_display_table ();
16005 if (! disptab_matches_widthtab
16006 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16008 struct buffer *buf = current_buffer;
16010 if (buf->base_buffer)
16011 buf = buf->base_buffer;
16012 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16013 recompute_width_table (current_buffer, disptab);
16017 /* If window-start is screwed up, choose a new one. */
16018 if (XMARKER (w->start)->buffer != current_buffer)
16019 goto recenter;
16021 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16023 /* If someone specified a new starting point but did not insist,
16024 check whether it can be used. */
16025 if ((w->optional_new_start || window_frozen_p (w))
16026 && CHARPOS (startp) >= BEGV
16027 && CHARPOS (startp) <= ZV)
16029 ptrdiff_t it_charpos;
16031 w->optional_new_start = false;
16032 start_display (&it, w, startp);
16033 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16034 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16035 /* Record IT's position now, since line_bottom_y might change
16036 that. */
16037 it_charpos = IT_CHARPOS (it);
16038 /* Make sure we set the force_start flag only if the cursor row
16039 will be fully visible. Otherwise, the code under force_start
16040 label below will try to move point back into view, which is
16041 not what the code which sets optional_new_start wants. */
16042 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16043 && !w->force_start)
16045 if (it_charpos == PT)
16046 w->force_start = true;
16047 /* IT may overshoot PT if text at PT is invisible. */
16048 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16049 w->force_start = true;
16050 #ifdef GLYPH_DEBUG
16051 if (w->force_start)
16053 if (window_frozen_p (w))
16054 debug_method_add (w, "set force_start from frozen window start");
16055 else
16056 debug_method_add (w, "set force_start from optional_new_start");
16058 #endif
16062 force_start:
16064 /* Handle case where place to start displaying has been specified,
16065 unless the specified location is outside the accessible range. */
16066 if (w->force_start)
16068 /* We set this later on if we have to adjust point. */
16069 int new_vpos = -1;
16071 w->force_start = false;
16072 w->vscroll = 0;
16073 w->window_end_valid = false;
16075 /* Forget any recorded base line for line number display. */
16076 if (!buffer_unchanged_p)
16077 w->base_line_number = 0;
16079 /* Redisplay the mode line. Select the buffer properly for that.
16080 Also, run the hook window-scroll-functions
16081 because we have scrolled. */
16082 /* Note, we do this after clearing force_start because
16083 if there's an error, it is better to forget about force_start
16084 than to get into an infinite loop calling the hook functions
16085 and having them get more errors. */
16086 if (!update_mode_line
16087 || ! NILP (Vwindow_scroll_functions))
16089 update_mode_line = true;
16090 w->update_mode_line = true;
16091 startp = run_window_scroll_functions (window, startp);
16094 if (CHARPOS (startp) < BEGV)
16095 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16096 else if (CHARPOS (startp) > ZV)
16097 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16099 /* Redisplay, then check if cursor has been set during the
16100 redisplay. Give up if new fonts were loaded. */
16101 /* We used to issue a CHECK_MARGINS argument to try_window here,
16102 but this causes scrolling to fail when point begins inside
16103 the scroll margin (bug#148) -- cyd */
16104 if (!try_window (window, startp, 0))
16106 w->force_start = true;
16107 clear_glyph_matrix (w->desired_matrix);
16108 goto need_larger_matrices;
16111 if (w->cursor.vpos < 0)
16113 /* If point does not appear, try to move point so it does
16114 appear. The desired matrix has been built above, so we
16115 can use it here. */
16116 new_vpos = window_box_height (w) / 2;
16119 if (!cursor_row_fully_visible_p (w, false, false))
16121 /* Point does appear, but on a line partly visible at end of window.
16122 Move it back to a fully-visible line. */
16123 new_vpos = window_box_height (w);
16124 /* But if window_box_height suggests a Y coordinate that is
16125 not less than we already have, that line will clearly not
16126 be fully visible, so give up and scroll the display.
16127 This can happen when the default face uses a font whose
16128 dimensions are different from the frame's default
16129 font. */
16130 if (new_vpos >= w->cursor.y)
16132 w->cursor.vpos = -1;
16133 clear_glyph_matrix (w->desired_matrix);
16134 goto try_to_scroll;
16137 else if (w->cursor.vpos >= 0)
16139 /* Some people insist on not letting point enter the scroll
16140 margin, even though this part handles windows that didn't
16141 scroll at all. */
16142 int window_total_lines
16143 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16144 int margin = min (scroll_margin, window_total_lines / 4);
16145 int pixel_margin = margin * frame_line_height;
16146 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16148 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16149 below, which finds the row to move point to, advances by
16150 the Y coordinate of the _next_ row, see the definition of
16151 MATRIX_ROW_BOTTOM_Y. */
16152 if (w->cursor.vpos < margin + header_line)
16154 w->cursor.vpos = -1;
16155 clear_glyph_matrix (w->desired_matrix);
16156 goto try_to_scroll;
16158 else
16160 int window_height = window_box_height (w);
16162 if (header_line)
16163 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16164 if (w->cursor.y >= window_height - pixel_margin)
16166 w->cursor.vpos = -1;
16167 clear_glyph_matrix (w->desired_matrix);
16168 goto try_to_scroll;
16173 /* If we need to move point for either of the above reasons,
16174 now actually do it. */
16175 if (new_vpos >= 0)
16177 struct glyph_row *row;
16179 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16180 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16181 ++row;
16183 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16184 MATRIX_ROW_START_BYTEPOS (row));
16186 if (w != XWINDOW (selected_window))
16187 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16188 else if (current_buffer == old)
16189 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16191 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16193 /* Re-run pre-redisplay-function so it can update the region
16194 according to the new position of point. */
16195 /* Other than the cursor, w's redisplay is done so we can set its
16196 redisplay to false. Also the buffer's redisplay can be set to
16197 false, since propagate_buffer_redisplay should have already
16198 propagated its info to `w' anyway. */
16199 w->redisplay = false;
16200 XBUFFER (w->contents)->text->redisplay = false;
16201 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16203 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16205 /* pre-redisplay-function made changes (e.g. move the region)
16206 that require another round of redisplay. */
16207 clear_glyph_matrix (w->desired_matrix);
16208 if (!try_window (window, startp, 0))
16209 goto need_larger_matrices;
16212 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16214 clear_glyph_matrix (w->desired_matrix);
16215 goto try_to_scroll;
16218 #ifdef GLYPH_DEBUG
16219 debug_method_add (w, "forced window start");
16220 #endif
16221 goto done;
16224 /* Handle case where text has not changed, only point, and it has
16225 not moved off the frame, and we are not retrying after hscroll.
16226 (current_matrix_up_to_date_p is true when retrying.) */
16227 if (current_matrix_up_to_date_p
16228 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16229 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16231 switch (rc)
16233 case CURSOR_MOVEMENT_SUCCESS:
16234 used_current_matrix_p = true;
16235 goto done;
16237 case CURSOR_MOVEMENT_MUST_SCROLL:
16238 goto try_to_scroll;
16240 default:
16241 emacs_abort ();
16244 /* If current starting point was originally the beginning of a line
16245 but no longer is, find a new starting point. */
16246 else if (w->start_at_line_beg
16247 && !(CHARPOS (startp) <= BEGV
16248 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16250 #ifdef GLYPH_DEBUG
16251 debug_method_add (w, "recenter 1");
16252 #endif
16253 goto recenter;
16256 /* Try scrolling with try_window_id. Value is > 0 if update has
16257 been done, it is -1 if we know that the same window start will
16258 not work. It is 0 if unsuccessful for some other reason. */
16259 else if ((tem = try_window_id (w)) != 0)
16261 #ifdef GLYPH_DEBUG
16262 debug_method_add (w, "try_window_id %d", tem);
16263 #endif
16265 if (f->fonts_changed)
16266 goto need_larger_matrices;
16267 if (tem > 0)
16268 goto done;
16270 /* Otherwise try_window_id has returned -1 which means that we
16271 don't want the alternative below this comment to execute. */
16273 else if (CHARPOS (startp) >= BEGV
16274 && CHARPOS (startp) <= ZV
16275 && PT >= CHARPOS (startp)
16276 && (CHARPOS (startp) < ZV
16277 /* Avoid starting at end of buffer. */
16278 || CHARPOS (startp) == BEGV
16279 || !window_outdated (w)))
16281 int d1, d2, d5, d6;
16282 int rtop, rbot;
16284 /* If first window line is a continuation line, and window start
16285 is inside the modified region, but the first change is before
16286 current window start, we must select a new window start.
16288 However, if this is the result of a down-mouse event (e.g. by
16289 extending the mouse-drag-overlay), we don't want to select a
16290 new window start, since that would change the position under
16291 the mouse, resulting in an unwanted mouse-movement rather
16292 than a simple mouse-click. */
16293 if (!w->start_at_line_beg
16294 && NILP (do_mouse_tracking)
16295 && CHARPOS (startp) > BEGV
16296 && CHARPOS (startp) > BEG + beg_unchanged
16297 && CHARPOS (startp) <= Z - end_unchanged
16298 /* Even if w->start_at_line_beg is nil, a new window may
16299 start at a line_beg, since that's how set_buffer_window
16300 sets it. So, we need to check the return value of
16301 compute_window_start_on_continuation_line. (See also
16302 bug#197). */
16303 && XMARKER (w->start)->buffer == current_buffer
16304 && compute_window_start_on_continuation_line (w)
16305 /* It doesn't make sense to force the window start like we
16306 do at label force_start if it is already known that point
16307 will not be fully visible in the resulting window, because
16308 doing so will move point from its correct position
16309 instead of scrolling the window to bring point into view.
16310 See bug#9324. */
16311 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16312 /* A very tall row could need more than the window height,
16313 in which case we accept that it is partially visible. */
16314 && (rtop != 0) == (rbot != 0))
16316 w->force_start = true;
16317 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16318 #ifdef GLYPH_DEBUG
16319 debug_method_add (w, "recomputed window start in continuation line");
16320 #endif
16321 goto force_start;
16324 #ifdef GLYPH_DEBUG
16325 debug_method_add (w, "same window start");
16326 #endif
16328 /* Try to redisplay starting at same place as before.
16329 If point has not moved off frame, accept the results. */
16330 if (!current_matrix_up_to_date_p
16331 /* Don't use try_window_reusing_current_matrix in this case
16332 because a window scroll function can have changed the
16333 buffer. */
16334 || !NILP (Vwindow_scroll_functions)
16335 || MINI_WINDOW_P (w)
16336 || !(used_current_matrix_p
16337 = try_window_reusing_current_matrix (w)))
16339 IF_DEBUG (debug_method_add (w, "1"));
16340 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16341 /* -1 means we need to scroll.
16342 0 means we need new matrices, but fonts_changed
16343 is set in that case, so we will detect it below. */
16344 goto try_to_scroll;
16347 if (f->fonts_changed)
16348 goto need_larger_matrices;
16350 if (w->cursor.vpos >= 0)
16352 if (!just_this_one_p
16353 || current_buffer->clip_changed
16354 || BEG_UNCHANGED < CHARPOS (startp))
16355 /* Forget any recorded base line for line number display. */
16356 w->base_line_number = 0;
16358 if (!cursor_row_fully_visible_p (w, true, false))
16360 clear_glyph_matrix (w->desired_matrix);
16361 last_line_misfit = true;
16363 /* Drop through and scroll. */
16364 else
16365 goto done;
16367 else
16368 clear_glyph_matrix (w->desired_matrix);
16371 try_to_scroll:
16373 /* Redisplay the mode line. Select the buffer properly for that. */
16374 if (!update_mode_line)
16376 update_mode_line = true;
16377 w->update_mode_line = true;
16380 /* Try to scroll by specified few lines. */
16381 if ((scroll_conservatively
16382 || emacs_scroll_step
16383 || temp_scroll_step
16384 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16385 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16386 && CHARPOS (startp) >= BEGV
16387 && CHARPOS (startp) <= ZV)
16389 /* The function returns -1 if new fonts were loaded, 1 if
16390 successful, 0 if not successful. */
16391 int ss = try_scrolling (window, just_this_one_p,
16392 scroll_conservatively,
16393 emacs_scroll_step,
16394 temp_scroll_step, last_line_misfit);
16395 switch (ss)
16397 case SCROLLING_SUCCESS:
16398 goto done;
16400 case SCROLLING_NEED_LARGER_MATRICES:
16401 goto need_larger_matrices;
16403 case SCROLLING_FAILED:
16404 break;
16406 default:
16407 emacs_abort ();
16411 /* Finally, just choose a place to start which positions point
16412 according to user preferences. */
16414 recenter:
16416 #ifdef GLYPH_DEBUG
16417 debug_method_add (w, "recenter");
16418 #endif
16420 /* Forget any previously recorded base line for line number display. */
16421 if (!buffer_unchanged_p)
16422 w->base_line_number = 0;
16424 /* Determine the window start relative to point. */
16425 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16426 it.current_y = it.last_visible_y;
16427 if (centering_position < 0)
16429 int window_total_lines
16430 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16431 int margin
16432 = scroll_margin > 0
16433 ? min (scroll_margin, window_total_lines / 4)
16434 : 0;
16435 ptrdiff_t margin_pos = CHARPOS (startp);
16436 Lisp_Object aggressive;
16437 bool scrolling_up;
16439 /* If there is a scroll margin at the top of the window, find
16440 its character position. */
16441 if (margin
16442 /* Cannot call start_display if startp is not in the
16443 accessible region of the buffer. This can happen when we
16444 have just switched to a different buffer and/or changed
16445 its restriction. In that case, startp is initialized to
16446 the character position 1 (BEGV) because we did not yet
16447 have chance to display the buffer even once. */
16448 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16450 struct it it1;
16451 void *it1data = NULL;
16453 SAVE_IT (it1, it, it1data);
16454 start_display (&it1, w, startp);
16455 move_it_vertically (&it1, margin * frame_line_height);
16456 margin_pos = IT_CHARPOS (it1);
16457 RESTORE_IT (&it, &it, it1data);
16459 scrolling_up = PT > margin_pos;
16460 aggressive =
16461 scrolling_up
16462 ? BVAR (current_buffer, scroll_up_aggressively)
16463 : BVAR (current_buffer, scroll_down_aggressively);
16465 if (!MINI_WINDOW_P (w)
16466 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16468 int pt_offset = 0;
16470 /* Setting scroll-conservatively overrides
16471 scroll-*-aggressively. */
16472 if (!scroll_conservatively && NUMBERP (aggressive))
16474 double float_amount = XFLOATINT (aggressive);
16476 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16477 if (pt_offset == 0 && float_amount > 0)
16478 pt_offset = 1;
16479 if (pt_offset && margin > 0)
16480 margin -= 1;
16482 /* Compute how much to move the window start backward from
16483 point so that point will be displayed where the user
16484 wants it. */
16485 if (scrolling_up)
16487 centering_position = it.last_visible_y;
16488 if (pt_offset)
16489 centering_position -= pt_offset;
16490 centering_position -=
16491 (frame_line_height * (1 + margin + last_line_misfit)
16492 + WINDOW_HEADER_LINE_HEIGHT (w));
16493 /* Don't let point enter the scroll margin near top of
16494 the window. */
16495 if (centering_position < margin * frame_line_height)
16496 centering_position = margin * frame_line_height;
16498 else
16499 centering_position = margin * frame_line_height + pt_offset;
16501 else
16502 /* Set the window start half the height of the window backward
16503 from point. */
16504 centering_position = window_box_height (w) / 2;
16506 move_it_vertically_backward (&it, centering_position);
16508 eassert (IT_CHARPOS (it) >= BEGV);
16510 /* The function move_it_vertically_backward may move over more
16511 than the specified y-distance. If it->w is small, e.g. a
16512 mini-buffer window, we may end up in front of the window's
16513 display area. Start displaying at the start of the line
16514 containing PT in this case. */
16515 if (it.current_y <= 0)
16517 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16518 move_it_vertically_backward (&it, 0);
16519 it.current_y = 0;
16522 it.current_x = it.hpos = 0;
16524 /* Set the window start position here explicitly, to avoid an
16525 infinite loop in case the functions in window-scroll-functions
16526 get errors. */
16527 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16529 /* Run scroll hooks. */
16530 startp = run_window_scroll_functions (window, it.current.pos);
16532 /* Redisplay the window. */
16533 if (!current_matrix_up_to_date_p
16534 || windows_or_buffers_changed
16535 || f->cursor_type_changed
16536 /* Don't use try_window_reusing_current_matrix in this case
16537 because it can have changed the buffer. */
16538 || !NILP (Vwindow_scroll_functions)
16539 || !just_this_one_p
16540 || MINI_WINDOW_P (w)
16541 || !(used_current_matrix_p
16542 = try_window_reusing_current_matrix (w)))
16543 try_window (window, startp, 0);
16545 /* If new fonts have been loaded (due to fontsets), give up. We
16546 have to start a new redisplay since we need to re-adjust glyph
16547 matrices. */
16548 if (f->fonts_changed)
16549 goto need_larger_matrices;
16551 /* If cursor did not appear assume that the middle of the window is
16552 in the first line of the window. Do it again with the next line.
16553 (Imagine a window of height 100, displaying two lines of height
16554 60. Moving back 50 from it->last_visible_y will end in the first
16555 line.) */
16556 if (w->cursor.vpos < 0)
16558 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16560 clear_glyph_matrix (w->desired_matrix);
16561 move_it_by_lines (&it, 1);
16562 try_window (window, it.current.pos, 0);
16564 else if (PT < IT_CHARPOS (it))
16566 clear_glyph_matrix (w->desired_matrix);
16567 move_it_by_lines (&it, -1);
16568 try_window (window, it.current.pos, 0);
16570 else
16572 /* Not much we can do about it. */
16576 /* Consider the following case: Window starts at BEGV, there is
16577 invisible, intangible text at BEGV, so that display starts at
16578 some point START > BEGV. It can happen that we are called with
16579 PT somewhere between BEGV and START. Try to handle that case,
16580 and similar ones. */
16581 if (w->cursor.vpos < 0)
16583 /* First, try locating the proper glyph row for PT. */
16584 struct glyph_row *row =
16585 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16587 /* Sometimes point is at the beginning of invisible text that is
16588 before the 1st character displayed in the row. In that case,
16589 row_containing_pos fails to find the row, because no glyphs
16590 with appropriate buffer positions are present in the row.
16591 Therefore, we next try to find the row which shows the 1st
16592 position after the invisible text. */
16593 if (!row)
16595 Lisp_Object val =
16596 get_char_property_and_overlay (make_number (PT), Qinvisible,
16597 Qnil, NULL);
16599 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16601 ptrdiff_t alt_pos;
16602 Lisp_Object invis_end =
16603 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16604 Qnil, Qnil);
16606 if (NATNUMP (invis_end))
16607 alt_pos = XFASTINT (invis_end);
16608 else
16609 alt_pos = ZV;
16610 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16611 NULL, 0);
16614 /* Finally, fall back on the first row of the window after the
16615 header line (if any). This is slightly better than not
16616 displaying the cursor at all. */
16617 if (!row)
16619 row = w->current_matrix->rows;
16620 if (row->mode_line_p)
16621 ++row;
16623 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16626 if (!cursor_row_fully_visible_p (w, false, false))
16628 /* If vscroll is enabled, disable it and try again. */
16629 if (w->vscroll)
16631 w->vscroll = 0;
16632 clear_glyph_matrix (w->desired_matrix);
16633 goto recenter;
16636 /* Users who set scroll-conservatively to a large number want
16637 point just above/below the scroll margin. If we ended up
16638 with point's row partially visible, move the window start to
16639 make that row fully visible and out of the margin. */
16640 if (scroll_conservatively > SCROLL_LIMIT)
16642 int window_total_lines
16643 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16644 int margin =
16645 scroll_margin > 0
16646 ? min (scroll_margin, window_total_lines / 4)
16647 : 0;
16648 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16650 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16651 clear_glyph_matrix (w->desired_matrix);
16652 if (1 == try_window (window, it.current.pos,
16653 TRY_WINDOW_CHECK_MARGINS))
16654 goto done;
16657 /* If centering point failed to make the whole line visible,
16658 put point at the top instead. That has to make the whole line
16659 visible, if it can be done. */
16660 if (centering_position == 0)
16661 goto done;
16663 clear_glyph_matrix (w->desired_matrix);
16664 centering_position = 0;
16665 goto recenter;
16668 done:
16670 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16671 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16672 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16674 /* Display the mode line, if we must. */
16675 if ((update_mode_line
16676 /* If window not full width, must redo its mode line
16677 if (a) the window to its side is being redone and
16678 (b) we do a frame-based redisplay. This is a consequence
16679 of how inverted lines are drawn in frame-based redisplay. */
16680 || (!just_this_one_p
16681 && !FRAME_WINDOW_P (f)
16682 && !WINDOW_FULL_WIDTH_P (w))
16683 /* Line number to display. */
16684 || w->base_line_pos > 0
16685 /* Column number is displayed and different from the one displayed. */
16686 || (w->column_number_displayed != -1
16687 && (w->column_number_displayed != current_column ())))
16688 /* This means that the window has a mode line. */
16689 && (WINDOW_WANTS_MODELINE_P (w)
16690 || WINDOW_WANTS_HEADER_LINE_P (w)))
16693 display_mode_lines (w);
16695 /* If mode line height has changed, arrange for a thorough
16696 immediate redisplay using the correct mode line height. */
16697 if (WINDOW_WANTS_MODELINE_P (w)
16698 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16700 f->fonts_changed = true;
16701 w->mode_line_height = -1;
16702 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16703 = DESIRED_MODE_LINE_HEIGHT (w);
16706 /* If header line height has changed, arrange for a thorough
16707 immediate redisplay using the correct header line height. */
16708 if (WINDOW_WANTS_HEADER_LINE_P (w)
16709 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16711 f->fonts_changed = true;
16712 w->header_line_height = -1;
16713 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16714 = DESIRED_HEADER_LINE_HEIGHT (w);
16717 if (f->fonts_changed)
16718 goto need_larger_matrices;
16721 if (!line_number_displayed && w->base_line_pos != -1)
16723 w->base_line_pos = 0;
16724 w->base_line_number = 0;
16727 finish_menu_bars:
16729 /* When we reach a frame's selected window, redo the frame's menu bar. */
16730 if (update_mode_line
16731 && EQ (FRAME_SELECTED_WINDOW (f), window))
16733 bool redisplay_menu_p;
16735 if (FRAME_WINDOW_P (f))
16737 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16738 || defined (HAVE_NS) || defined (USE_GTK)
16739 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16740 #else
16741 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16742 #endif
16744 else
16745 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16747 if (redisplay_menu_p)
16748 display_menu_bar (w);
16750 #ifdef HAVE_WINDOW_SYSTEM
16751 if (FRAME_WINDOW_P (f))
16753 #if defined (USE_GTK) || defined (HAVE_NS)
16754 if (FRAME_EXTERNAL_TOOL_BAR (f))
16755 redisplay_tool_bar (f);
16756 #else
16757 if (WINDOWP (f->tool_bar_window)
16758 && (FRAME_TOOL_BAR_LINES (f) > 0
16759 || !NILP (Vauto_resize_tool_bars))
16760 && redisplay_tool_bar (f))
16761 ignore_mouse_drag_p = true;
16762 #endif
16764 #endif
16767 #ifdef HAVE_WINDOW_SYSTEM
16768 if (FRAME_WINDOW_P (f)
16769 && update_window_fringes (w, (just_this_one_p
16770 || (!used_current_matrix_p && !overlay_arrow_seen)
16771 || w->pseudo_window_p)))
16773 update_begin (f);
16774 block_input ();
16775 if (draw_window_fringes (w, true))
16777 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16778 x_draw_right_divider (w);
16779 else
16780 x_draw_vertical_border (w);
16782 unblock_input ();
16783 update_end (f);
16786 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16787 x_draw_bottom_divider (w);
16788 #endif /* HAVE_WINDOW_SYSTEM */
16790 /* We go to this label, with fonts_changed set, if it is
16791 necessary to try again using larger glyph matrices.
16792 We have to redeem the scroll bar even in this case,
16793 because the loop in redisplay_internal expects that. */
16794 need_larger_matrices:
16796 finish_scroll_bars:
16798 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16800 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16801 /* Set the thumb's position and size. */
16802 set_vertical_scroll_bar (w);
16804 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16805 /* Set the thumb's position and size. */
16806 set_horizontal_scroll_bar (w);
16808 /* Note that we actually used the scroll bar attached to this
16809 window, so it shouldn't be deleted at the end of redisplay. */
16810 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16811 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16814 /* Restore current_buffer and value of point in it. The window
16815 update may have changed the buffer, so first make sure `opoint'
16816 is still valid (Bug#6177). */
16817 if (CHARPOS (opoint) < BEGV)
16818 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16819 else if (CHARPOS (opoint) > ZV)
16820 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16821 else
16822 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16824 set_buffer_internal_1 (old);
16825 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16826 shorter. This can be caused by log truncation in *Messages*. */
16827 if (CHARPOS (lpoint) <= ZV)
16828 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16830 unbind_to (count, Qnil);
16834 /* Build the complete desired matrix of WINDOW with a window start
16835 buffer position POS.
16837 Value is 1 if successful. It is zero if fonts were loaded during
16838 redisplay which makes re-adjusting glyph matrices necessary, and -1
16839 if point would appear in the scroll margins.
16840 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16841 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16842 set in FLAGS.) */
16845 try_window (Lisp_Object window, struct text_pos pos, int flags)
16847 struct window *w = XWINDOW (window);
16848 struct it it;
16849 struct glyph_row *last_text_row = NULL;
16850 struct frame *f = XFRAME (w->frame);
16851 int frame_line_height = default_line_pixel_height (w);
16853 /* Make POS the new window start. */
16854 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16856 /* Mark cursor position as unknown. No overlay arrow seen. */
16857 w->cursor.vpos = -1;
16858 overlay_arrow_seen = false;
16860 /* Initialize iterator and info to start at POS. */
16861 start_display (&it, w, pos);
16862 it.glyph_row->reversed_p = false;
16864 /* Display all lines of W. */
16865 while (it.current_y < it.last_visible_y)
16867 if (display_line (&it))
16868 last_text_row = it.glyph_row - 1;
16869 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16870 return 0;
16873 /* Don't let the cursor end in the scroll margins. */
16874 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16875 && !MINI_WINDOW_P (w))
16877 int this_scroll_margin;
16878 int window_total_lines
16879 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16881 if (scroll_margin > 0)
16883 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16884 this_scroll_margin *= frame_line_height;
16886 else
16887 this_scroll_margin = 0;
16889 if ((w->cursor.y >= 0 /* not vscrolled */
16890 && w->cursor.y < this_scroll_margin
16891 && CHARPOS (pos) > BEGV
16892 && IT_CHARPOS (it) < ZV)
16893 /* rms: considering make_cursor_line_fully_visible_p here
16894 seems to give wrong results. We don't want to recenter
16895 when the last line is partly visible, we want to allow
16896 that case to be handled in the usual way. */
16897 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16899 w->cursor.vpos = -1;
16900 clear_glyph_matrix (w->desired_matrix);
16901 return -1;
16905 /* If bottom moved off end of frame, change mode line percentage. */
16906 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16907 w->update_mode_line = true;
16909 /* Set window_end_pos to the offset of the last character displayed
16910 on the window from the end of current_buffer. Set
16911 window_end_vpos to its row number. */
16912 if (last_text_row)
16914 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16915 adjust_window_ends (w, last_text_row, false);
16916 eassert
16917 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16918 w->window_end_vpos)));
16920 else
16922 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16923 w->window_end_pos = Z - ZV;
16924 w->window_end_vpos = 0;
16927 /* But that is not valid info until redisplay finishes. */
16928 w->window_end_valid = false;
16929 return 1;
16934 /************************************************************************
16935 Window redisplay reusing current matrix when buffer has not changed
16936 ************************************************************************/
16938 /* Try redisplay of window W showing an unchanged buffer with a
16939 different window start than the last time it was displayed by
16940 reusing its current matrix. Value is true if successful.
16941 W->start is the new window start. */
16943 static bool
16944 try_window_reusing_current_matrix (struct window *w)
16946 struct frame *f = XFRAME (w->frame);
16947 struct glyph_row *bottom_row;
16948 struct it it;
16949 struct run run;
16950 struct text_pos start, new_start;
16951 int nrows_scrolled, i;
16952 struct glyph_row *last_text_row;
16953 struct glyph_row *last_reused_text_row;
16954 struct glyph_row *start_row;
16955 int start_vpos, min_y, max_y;
16957 #ifdef GLYPH_DEBUG
16958 if (inhibit_try_window_reusing)
16959 return false;
16960 #endif
16962 if (/* This function doesn't handle terminal frames. */
16963 !FRAME_WINDOW_P (f)
16964 /* Don't try to reuse the display if windows have been split
16965 or such. */
16966 || windows_or_buffers_changed
16967 || f->cursor_type_changed)
16968 return false;
16970 /* Can't do this if showing trailing whitespace. */
16971 if (!NILP (Vshow_trailing_whitespace))
16972 return false;
16974 /* If top-line visibility has changed, give up. */
16975 if (WINDOW_WANTS_HEADER_LINE_P (w)
16976 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16977 return false;
16979 /* Give up if old or new display is scrolled vertically. We could
16980 make this function handle this, but right now it doesn't. */
16981 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16982 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16983 return false;
16985 /* The variable new_start now holds the new window start. The old
16986 start `start' can be determined from the current matrix. */
16987 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16988 start = start_row->minpos;
16989 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16991 /* Clear the desired matrix for the display below. */
16992 clear_glyph_matrix (w->desired_matrix);
16994 if (CHARPOS (new_start) <= CHARPOS (start))
16996 /* Don't use this method if the display starts with an ellipsis
16997 displayed for invisible text. It's not easy to handle that case
16998 below, and it's certainly not worth the effort since this is
16999 not a frequent case. */
17000 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17001 return false;
17003 IF_DEBUG (debug_method_add (w, "twu1"));
17005 /* Display up to a row that can be reused. The variable
17006 last_text_row is set to the last row displayed that displays
17007 text. Note that it.vpos == 0 if or if not there is a
17008 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17009 start_display (&it, w, new_start);
17010 w->cursor.vpos = -1;
17011 last_text_row = last_reused_text_row = NULL;
17013 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17015 /* If we have reached into the characters in the START row,
17016 that means the line boundaries have changed. So we
17017 can't start copying with the row START. Maybe it will
17018 work to start copying with the following row. */
17019 while (IT_CHARPOS (it) > CHARPOS (start))
17021 /* Advance to the next row as the "start". */
17022 start_row++;
17023 start = start_row->minpos;
17024 /* If there are no more rows to try, or just one, give up. */
17025 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17026 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17027 || CHARPOS (start) == ZV)
17029 clear_glyph_matrix (w->desired_matrix);
17030 return false;
17033 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17035 /* If we have reached alignment, we can copy the rest of the
17036 rows. */
17037 if (IT_CHARPOS (it) == CHARPOS (start)
17038 /* Don't accept "alignment" inside a display vector,
17039 since start_row could have started in the middle of
17040 that same display vector (thus their character
17041 positions match), and we have no way of telling if
17042 that is the case. */
17043 && it.current.dpvec_index < 0)
17044 break;
17046 it.glyph_row->reversed_p = false;
17047 if (display_line (&it))
17048 last_text_row = it.glyph_row - 1;
17052 /* A value of current_y < last_visible_y means that we stopped
17053 at the previous window start, which in turn means that we
17054 have at least one reusable row. */
17055 if (it.current_y < it.last_visible_y)
17057 struct glyph_row *row;
17059 /* IT.vpos always starts from 0; it counts text lines. */
17060 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17062 /* Find PT if not already found in the lines displayed. */
17063 if (w->cursor.vpos < 0)
17065 int dy = it.current_y - start_row->y;
17067 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17068 row = row_containing_pos (w, PT, row, NULL, dy);
17069 if (row)
17070 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17071 dy, nrows_scrolled);
17072 else
17074 clear_glyph_matrix (w->desired_matrix);
17075 return false;
17079 /* Scroll the display. Do it before the current matrix is
17080 changed. The problem here is that update has not yet
17081 run, i.e. part of the current matrix is not up to date.
17082 scroll_run_hook will clear the cursor, and use the
17083 current matrix to get the height of the row the cursor is
17084 in. */
17085 run.current_y = start_row->y;
17086 run.desired_y = it.current_y;
17087 run.height = it.last_visible_y - it.current_y;
17089 if (run.height > 0 && run.current_y != run.desired_y)
17091 update_begin (f);
17092 FRAME_RIF (f)->update_window_begin_hook (w);
17093 FRAME_RIF (f)->clear_window_mouse_face (w);
17094 FRAME_RIF (f)->scroll_run_hook (w, &run);
17095 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17096 update_end (f);
17099 /* Shift current matrix down by nrows_scrolled lines. */
17100 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17101 rotate_matrix (w->current_matrix,
17102 start_vpos,
17103 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17104 nrows_scrolled);
17106 /* Disable lines that must be updated. */
17107 for (i = 0; i < nrows_scrolled; ++i)
17108 (start_row + i)->enabled_p = false;
17110 /* Re-compute Y positions. */
17111 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17112 max_y = it.last_visible_y;
17113 for (row = start_row + nrows_scrolled;
17114 row < bottom_row;
17115 ++row)
17117 row->y = it.current_y;
17118 row->visible_height = row->height;
17120 if (row->y < min_y)
17121 row->visible_height -= min_y - row->y;
17122 if (row->y + row->height > max_y)
17123 row->visible_height -= row->y + row->height - max_y;
17124 if (row->fringe_bitmap_periodic_p)
17125 row->redraw_fringe_bitmaps_p = true;
17127 it.current_y += row->height;
17129 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17130 last_reused_text_row = row;
17131 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17132 break;
17135 /* Disable lines in the current matrix which are now
17136 below the window. */
17137 for (++row; row < bottom_row; ++row)
17138 row->enabled_p = row->mode_line_p = false;
17141 /* Update window_end_pos etc.; last_reused_text_row is the last
17142 reused row from the current matrix containing text, if any.
17143 The value of last_text_row is the last displayed line
17144 containing text. */
17145 if (last_reused_text_row)
17146 adjust_window_ends (w, last_reused_text_row, true);
17147 else if (last_text_row)
17148 adjust_window_ends (w, last_text_row, false);
17149 else
17151 /* This window must be completely empty. */
17152 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17153 w->window_end_pos = Z - ZV;
17154 w->window_end_vpos = 0;
17156 w->window_end_valid = false;
17158 /* Update hint: don't try scrolling again in update_window. */
17159 w->desired_matrix->no_scrolling_p = true;
17161 #ifdef GLYPH_DEBUG
17162 debug_method_add (w, "try_window_reusing_current_matrix 1");
17163 #endif
17164 return true;
17166 else if (CHARPOS (new_start) > CHARPOS (start))
17168 struct glyph_row *pt_row, *row;
17169 struct glyph_row *first_reusable_row;
17170 struct glyph_row *first_row_to_display;
17171 int dy;
17172 int yb = window_text_bottom_y (w);
17174 /* Find the row starting at new_start, if there is one. Don't
17175 reuse a partially visible line at the end. */
17176 first_reusable_row = start_row;
17177 while (first_reusable_row->enabled_p
17178 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17179 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17180 < CHARPOS (new_start)))
17181 ++first_reusable_row;
17183 /* Give up if there is no row to reuse. */
17184 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17185 || !first_reusable_row->enabled_p
17186 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17187 != CHARPOS (new_start)))
17188 return false;
17190 /* We can reuse fully visible rows beginning with
17191 first_reusable_row to the end of the window. Set
17192 first_row_to_display to the first row that cannot be reused.
17193 Set pt_row to the row containing point, if there is any. */
17194 pt_row = NULL;
17195 for (first_row_to_display = first_reusable_row;
17196 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17197 ++first_row_to_display)
17199 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17200 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17201 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17202 && first_row_to_display->ends_at_zv_p
17203 && pt_row == NULL)))
17204 pt_row = first_row_to_display;
17207 /* Start displaying at the start of first_row_to_display. */
17208 eassert (first_row_to_display->y < yb);
17209 init_to_row_start (&it, w, first_row_to_display);
17211 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17212 - start_vpos);
17213 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17214 - nrows_scrolled);
17215 it.current_y = (first_row_to_display->y - first_reusable_row->y
17216 + WINDOW_HEADER_LINE_HEIGHT (w));
17218 /* Display lines beginning with first_row_to_display in the
17219 desired matrix. Set last_text_row to the last row displayed
17220 that displays text. */
17221 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17222 if (pt_row == NULL)
17223 w->cursor.vpos = -1;
17224 last_text_row = NULL;
17225 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17226 if (display_line (&it))
17227 last_text_row = it.glyph_row - 1;
17229 /* If point is in a reused row, adjust y and vpos of the cursor
17230 position. */
17231 if (pt_row)
17233 w->cursor.vpos -= nrows_scrolled;
17234 w->cursor.y -= first_reusable_row->y - start_row->y;
17237 /* Give up if point isn't in a row displayed or reused. (This
17238 also handles the case where w->cursor.vpos < nrows_scrolled
17239 after the calls to display_line, which can happen with scroll
17240 margins. See bug#1295.) */
17241 if (w->cursor.vpos < 0)
17243 clear_glyph_matrix (w->desired_matrix);
17244 return false;
17247 /* Scroll the display. */
17248 run.current_y = first_reusable_row->y;
17249 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17250 run.height = it.last_visible_y - run.current_y;
17251 dy = run.current_y - run.desired_y;
17253 if (run.height)
17255 update_begin (f);
17256 FRAME_RIF (f)->update_window_begin_hook (w);
17257 FRAME_RIF (f)->clear_window_mouse_face (w);
17258 FRAME_RIF (f)->scroll_run_hook (w, &run);
17259 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17260 update_end (f);
17263 /* Adjust Y positions of reused rows. */
17264 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17265 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17266 max_y = it.last_visible_y;
17267 for (row = first_reusable_row; row < first_row_to_display; ++row)
17269 row->y -= dy;
17270 row->visible_height = row->height;
17271 if (row->y < min_y)
17272 row->visible_height -= min_y - row->y;
17273 if (row->y + row->height > max_y)
17274 row->visible_height -= row->y + row->height - max_y;
17275 if (row->fringe_bitmap_periodic_p)
17276 row->redraw_fringe_bitmaps_p = true;
17279 /* Scroll the current matrix. */
17280 eassert (nrows_scrolled > 0);
17281 rotate_matrix (w->current_matrix,
17282 start_vpos,
17283 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17284 -nrows_scrolled);
17286 /* Disable rows not reused. */
17287 for (row -= nrows_scrolled; row < bottom_row; ++row)
17288 row->enabled_p = false;
17290 /* Point may have moved to a different line, so we cannot assume that
17291 the previous cursor position is valid; locate the correct row. */
17292 if (pt_row)
17294 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17295 row < bottom_row
17296 && PT >= MATRIX_ROW_END_CHARPOS (row)
17297 && !row->ends_at_zv_p;
17298 row++)
17300 w->cursor.vpos++;
17301 w->cursor.y = row->y;
17303 if (row < bottom_row)
17305 /* Can't simply scan the row for point with
17306 bidi-reordered glyph rows. Let set_cursor_from_row
17307 figure out where to put the cursor, and if it fails,
17308 give up. */
17309 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17311 if (!set_cursor_from_row (w, row, w->current_matrix,
17312 0, 0, 0, 0))
17314 clear_glyph_matrix (w->desired_matrix);
17315 return false;
17318 else
17320 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17321 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17323 for (; glyph < end
17324 && (!BUFFERP (glyph->object)
17325 || glyph->charpos < PT);
17326 glyph++)
17328 w->cursor.hpos++;
17329 w->cursor.x += glyph->pixel_width;
17335 /* Adjust window end. A null value of last_text_row means that
17336 the window end is in reused rows which in turn means that
17337 only its vpos can have changed. */
17338 if (last_text_row)
17339 adjust_window_ends (w, last_text_row, false);
17340 else
17341 w->window_end_vpos -= nrows_scrolled;
17343 w->window_end_valid = false;
17344 w->desired_matrix->no_scrolling_p = true;
17346 #ifdef GLYPH_DEBUG
17347 debug_method_add (w, "try_window_reusing_current_matrix 2");
17348 #endif
17349 return true;
17352 return false;
17357 /************************************************************************
17358 Window redisplay reusing current matrix when buffer has changed
17359 ************************************************************************/
17361 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17362 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17363 ptrdiff_t *, ptrdiff_t *);
17364 static struct glyph_row *
17365 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17366 struct glyph_row *);
17369 /* Return the last row in MATRIX displaying text. If row START is
17370 non-null, start searching with that row. IT gives the dimensions
17371 of the display. Value is null if matrix is empty; otherwise it is
17372 a pointer to the row found. */
17374 static struct glyph_row *
17375 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17376 struct glyph_row *start)
17378 struct glyph_row *row, *row_found;
17380 /* Set row_found to the last row in IT->w's current matrix
17381 displaying text. The loop looks funny but think of partially
17382 visible lines. */
17383 row_found = NULL;
17384 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17385 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17387 eassert (row->enabled_p);
17388 row_found = row;
17389 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17390 break;
17391 ++row;
17394 return row_found;
17398 /* Return the last row in the current matrix of W that is not affected
17399 by changes at the start of current_buffer that occurred since W's
17400 current matrix was built. Value is null if no such row exists.
17402 BEG_UNCHANGED us the number of characters unchanged at the start of
17403 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17404 first changed character in current_buffer. Characters at positions <
17405 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17406 when the current matrix was built. */
17408 static struct glyph_row *
17409 find_last_unchanged_at_beg_row (struct window *w)
17411 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17412 struct glyph_row *row;
17413 struct glyph_row *row_found = NULL;
17414 int yb = window_text_bottom_y (w);
17416 /* Find the last row displaying unchanged text. */
17417 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17418 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17419 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17420 ++row)
17422 if (/* If row ends before first_changed_pos, it is unchanged,
17423 except in some case. */
17424 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17425 /* When row ends in ZV and we write at ZV it is not
17426 unchanged. */
17427 && !row->ends_at_zv_p
17428 /* When first_changed_pos is the end of a continued line,
17429 row is not unchanged because it may be no longer
17430 continued. */
17431 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17432 && (row->continued_p
17433 || row->exact_window_width_line_p))
17434 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17435 needs to be recomputed, so don't consider this row as
17436 unchanged. This happens when the last line was
17437 bidi-reordered and was killed immediately before this
17438 redisplay cycle. In that case, ROW->end stores the
17439 buffer position of the first visual-order character of
17440 the killed text, which is now beyond ZV. */
17441 && CHARPOS (row->end.pos) <= ZV)
17442 row_found = row;
17444 /* Stop if last visible row. */
17445 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17446 break;
17449 return row_found;
17453 /* Find the first glyph row in the current matrix of W that is not
17454 affected by changes at the end of current_buffer since the
17455 time W's current matrix was built.
17457 Return in *DELTA the number of chars by which buffer positions in
17458 unchanged text at the end of current_buffer must be adjusted.
17460 Return in *DELTA_BYTES the corresponding number of bytes.
17462 Value is null if no such row exists, i.e. all rows are affected by
17463 changes. */
17465 static struct glyph_row *
17466 find_first_unchanged_at_end_row (struct window *w,
17467 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17469 struct glyph_row *row;
17470 struct glyph_row *row_found = NULL;
17472 *delta = *delta_bytes = 0;
17474 /* Display must not have been paused, otherwise the current matrix
17475 is not up to date. */
17476 eassert (w->window_end_valid);
17478 /* A value of window_end_pos >= END_UNCHANGED means that the window
17479 end is in the range of changed text. If so, there is no
17480 unchanged row at the end of W's current matrix. */
17481 if (w->window_end_pos >= END_UNCHANGED)
17482 return NULL;
17484 /* Set row to the last row in W's current matrix displaying text. */
17485 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17487 /* If matrix is entirely empty, no unchanged row exists. */
17488 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17490 /* The value of row is the last glyph row in the matrix having a
17491 meaningful buffer position in it. The end position of row
17492 corresponds to window_end_pos. This allows us to translate
17493 buffer positions in the current matrix to current buffer
17494 positions for characters not in changed text. */
17495 ptrdiff_t Z_old =
17496 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17497 ptrdiff_t Z_BYTE_old =
17498 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17499 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17500 struct glyph_row *first_text_row
17501 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17503 *delta = Z - Z_old;
17504 *delta_bytes = Z_BYTE - Z_BYTE_old;
17506 /* Set last_unchanged_pos to the buffer position of the last
17507 character in the buffer that has not been changed. Z is the
17508 index + 1 of the last character in current_buffer, i.e. by
17509 subtracting END_UNCHANGED we get the index of the last
17510 unchanged character, and we have to add BEG to get its buffer
17511 position. */
17512 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17513 last_unchanged_pos_old = last_unchanged_pos - *delta;
17515 /* Search backward from ROW for a row displaying a line that
17516 starts at a minimum position >= last_unchanged_pos_old. */
17517 for (; row > first_text_row; --row)
17519 /* This used to abort, but it can happen.
17520 It is ok to just stop the search instead here. KFS. */
17521 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17522 break;
17524 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17525 row_found = row;
17529 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17531 return row_found;
17535 /* Make sure that glyph rows in the current matrix of window W
17536 reference the same glyph memory as corresponding rows in the
17537 frame's frame matrix. This function is called after scrolling W's
17538 current matrix on a terminal frame in try_window_id and
17539 try_window_reusing_current_matrix. */
17541 static void
17542 sync_frame_with_window_matrix_rows (struct window *w)
17544 struct frame *f = XFRAME (w->frame);
17545 struct glyph_row *window_row, *window_row_end, *frame_row;
17547 /* Preconditions: W must be a leaf window and full-width. Its frame
17548 must have a frame matrix. */
17549 eassert (BUFFERP (w->contents));
17550 eassert (WINDOW_FULL_WIDTH_P (w));
17551 eassert (!FRAME_WINDOW_P (f));
17553 /* If W is a full-width window, glyph pointers in W's current matrix
17554 have, by definition, to be the same as glyph pointers in the
17555 corresponding frame matrix. Note that frame matrices have no
17556 marginal areas (see build_frame_matrix). */
17557 window_row = w->current_matrix->rows;
17558 window_row_end = window_row + w->current_matrix->nrows;
17559 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17560 while (window_row < window_row_end)
17562 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17563 struct glyph *end = window_row->glyphs[LAST_AREA];
17565 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17566 frame_row->glyphs[TEXT_AREA] = start;
17567 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17568 frame_row->glyphs[LAST_AREA] = end;
17570 /* Disable frame rows whose corresponding window rows have
17571 been disabled in try_window_id. */
17572 if (!window_row->enabled_p)
17573 frame_row->enabled_p = false;
17575 ++window_row, ++frame_row;
17580 /* Find the glyph row in window W containing CHARPOS. Consider all
17581 rows between START and END (not inclusive). END null means search
17582 all rows to the end of the display area of W. Value is the row
17583 containing CHARPOS or null. */
17585 struct glyph_row *
17586 row_containing_pos (struct window *w, ptrdiff_t charpos,
17587 struct glyph_row *start, struct glyph_row *end, int dy)
17589 struct glyph_row *row = start;
17590 struct glyph_row *best_row = NULL;
17591 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17592 int last_y;
17594 /* If we happen to start on a header-line, skip that. */
17595 if (row->mode_line_p)
17596 ++row;
17598 if ((end && row >= end) || !row->enabled_p)
17599 return NULL;
17601 last_y = window_text_bottom_y (w) - dy;
17603 while (true)
17605 /* Give up if we have gone too far. */
17606 if (end && row >= end)
17607 return NULL;
17608 /* This formerly returned if they were equal.
17609 I think that both quantities are of a "last plus one" type;
17610 if so, when they are equal, the row is within the screen. -- rms. */
17611 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17612 return NULL;
17614 /* If it is in this row, return this row. */
17615 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17616 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17617 /* The end position of a row equals the start
17618 position of the next row. If CHARPOS is there, we
17619 would rather consider it displayed in the next
17620 line, except when this line ends in ZV. */
17621 && !row_for_charpos_p (row, charpos)))
17622 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17624 struct glyph *g;
17626 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17627 || (!best_row && !row->continued_p))
17628 return row;
17629 /* In bidi-reordered rows, there could be several rows whose
17630 edges surround CHARPOS, all of these rows belonging to
17631 the same continued line. We need to find the row which
17632 fits CHARPOS the best. */
17633 for (g = row->glyphs[TEXT_AREA];
17634 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17635 g++)
17637 if (!STRINGP (g->object))
17639 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17641 mindif = eabs (g->charpos - charpos);
17642 best_row = row;
17643 /* Exact match always wins. */
17644 if (mindif == 0)
17645 return best_row;
17650 else if (best_row && !row->continued_p)
17651 return best_row;
17652 ++row;
17657 /* Try to redisplay window W by reusing its existing display. W's
17658 current matrix must be up to date when this function is called,
17659 i.e., window_end_valid must be true.
17661 Value is
17663 >= 1 if successful, i.e. display has been updated
17664 specifically:
17665 1 means the changes were in front of a newline that precedes
17666 the window start, and the whole current matrix was reused
17667 2 means the changes were after the last position displayed
17668 in the window, and the whole current matrix was reused
17669 3 means portions of the current matrix were reused, while
17670 some of the screen lines were redrawn
17671 -1 if redisplay with same window start is known not to succeed
17672 0 if otherwise unsuccessful
17674 The following steps are performed:
17676 1. Find the last row in the current matrix of W that is not
17677 affected by changes at the start of current_buffer. If no such row
17678 is found, give up.
17680 2. Find the first row in W's current matrix that is not affected by
17681 changes at the end of current_buffer. Maybe there is no such row.
17683 3. Display lines beginning with the row + 1 found in step 1 to the
17684 row found in step 2 or, if step 2 didn't find a row, to the end of
17685 the window.
17687 4. If cursor is not known to appear on the window, give up.
17689 5. If display stopped at the row found in step 2, scroll the
17690 display and current matrix as needed.
17692 6. Maybe display some lines at the end of W, if we must. This can
17693 happen under various circumstances, like a partially visible line
17694 becoming fully visible, or because newly displayed lines are displayed
17695 in smaller font sizes.
17697 7. Update W's window end information. */
17699 static int
17700 try_window_id (struct window *w)
17702 struct frame *f = XFRAME (w->frame);
17703 struct glyph_matrix *current_matrix = w->current_matrix;
17704 struct glyph_matrix *desired_matrix = w->desired_matrix;
17705 struct glyph_row *last_unchanged_at_beg_row;
17706 struct glyph_row *first_unchanged_at_end_row;
17707 struct glyph_row *row;
17708 struct glyph_row *bottom_row;
17709 int bottom_vpos;
17710 struct it it;
17711 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17712 int dvpos, dy;
17713 struct text_pos start_pos;
17714 struct run run;
17715 int first_unchanged_at_end_vpos = 0;
17716 struct glyph_row *last_text_row, *last_text_row_at_end;
17717 struct text_pos start;
17718 ptrdiff_t first_changed_charpos, last_changed_charpos;
17720 #ifdef GLYPH_DEBUG
17721 if (inhibit_try_window_id)
17722 return 0;
17723 #endif
17725 /* This is handy for debugging. */
17726 #if false
17727 #define GIVE_UP(X) \
17728 do { \
17729 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17730 return 0; \
17731 } while (false)
17732 #else
17733 #define GIVE_UP(X) return 0
17734 #endif
17736 SET_TEXT_POS_FROM_MARKER (start, w->start);
17738 /* Don't use this for mini-windows because these can show
17739 messages and mini-buffers, and we don't handle that here. */
17740 if (MINI_WINDOW_P (w))
17741 GIVE_UP (1);
17743 /* This flag is used to prevent redisplay optimizations. */
17744 if (windows_or_buffers_changed || f->cursor_type_changed)
17745 GIVE_UP (2);
17747 /* This function's optimizations cannot be used if overlays have
17748 changed in the buffer displayed by the window, so give up if they
17749 have. */
17750 if (w->last_overlay_modified != OVERLAY_MODIFF)
17751 GIVE_UP (21);
17753 /* Verify that narrowing has not changed.
17754 Also verify that we were not told to prevent redisplay optimizations.
17755 It would be nice to further
17756 reduce the number of cases where this prevents try_window_id. */
17757 if (current_buffer->clip_changed
17758 || current_buffer->prevent_redisplay_optimizations_p)
17759 GIVE_UP (3);
17761 /* Window must either use window-based redisplay or be full width. */
17762 if (!FRAME_WINDOW_P (f)
17763 && (!FRAME_LINE_INS_DEL_OK (f)
17764 || !WINDOW_FULL_WIDTH_P (w)))
17765 GIVE_UP (4);
17767 /* Give up if point is known NOT to appear in W. */
17768 if (PT < CHARPOS (start))
17769 GIVE_UP (5);
17771 /* Another way to prevent redisplay optimizations. */
17772 if (w->last_modified == 0)
17773 GIVE_UP (6);
17775 /* Verify that window is not hscrolled. */
17776 if (w->hscroll != 0)
17777 GIVE_UP (7);
17779 /* Verify that display wasn't paused. */
17780 if (!w->window_end_valid)
17781 GIVE_UP (8);
17783 /* Likewise if highlighting trailing whitespace. */
17784 if (!NILP (Vshow_trailing_whitespace))
17785 GIVE_UP (11);
17787 /* Can't use this if overlay arrow position and/or string have
17788 changed. */
17789 if (overlay_arrows_changed_p ())
17790 GIVE_UP (12);
17792 /* When word-wrap is on, adding a space to the first word of a
17793 wrapped line can change the wrap position, altering the line
17794 above it. It might be worthwhile to handle this more
17795 intelligently, but for now just redisplay from scratch. */
17796 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17797 GIVE_UP (21);
17799 /* Under bidi reordering, adding or deleting a character in the
17800 beginning of a paragraph, before the first strong directional
17801 character, can change the base direction of the paragraph (unless
17802 the buffer specifies a fixed paragraph direction), which will
17803 require to redisplay the whole paragraph. It might be worthwhile
17804 to find the paragraph limits and widen the range of redisplayed
17805 lines to that, but for now just give up this optimization and
17806 redisplay from scratch. */
17807 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17808 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17809 GIVE_UP (22);
17811 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17812 only if buffer has really changed. The reason is that the gap is
17813 initially at Z for freshly visited files. The code below would
17814 set end_unchanged to 0 in that case. */
17815 if (MODIFF > SAVE_MODIFF
17816 /* This seems to happen sometimes after saving a buffer. */
17817 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17819 if (GPT - BEG < BEG_UNCHANGED)
17820 BEG_UNCHANGED = GPT - BEG;
17821 if (Z - GPT < END_UNCHANGED)
17822 END_UNCHANGED = Z - GPT;
17825 /* The position of the first and last character that has been changed. */
17826 first_changed_charpos = BEG + BEG_UNCHANGED;
17827 last_changed_charpos = Z - END_UNCHANGED;
17829 /* If window starts after a line end, and the last change is in
17830 front of that newline, then changes don't affect the display.
17831 This case happens with stealth-fontification. Note that although
17832 the display is unchanged, glyph positions in the matrix have to
17833 be adjusted, of course. */
17834 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17835 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17836 && ((last_changed_charpos < CHARPOS (start)
17837 && CHARPOS (start) == BEGV)
17838 || (last_changed_charpos < CHARPOS (start) - 1
17839 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17841 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17842 struct glyph_row *r0;
17844 /* Compute how many chars/bytes have been added to or removed
17845 from the buffer. */
17846 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17847 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17848 Z_delta = Z - Z_old;
17849 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17851 /* Give up if PT is not in the window. Note that it already has
17852 been checked at the start of try_window_id that PT is not in
17853 front of the window start. */
17854 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17855 GIVE_UP (13);
17857 /* If window start is unchanged, we can reuse the whole matrix
17858 as is, after adjusting glyph positions. No need to compute
17859 the window end again, since its offset from Z hasn't changed. */
17860 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17861 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17862 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17863 /* PT must not be in a partially visible line. */
17864 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17865 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17867 /* Adjust positions in the glyph matrix. */
17868 if (Z_delta || Z_delta_bytes)
17870 struct glyph_row *r1
17871 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17872 increment_matrix_positions (w->current_matrix,
17873 MATRIX_ROW_VPOS (r0, current_matrix),
17874 MATRIX_ROW_VPOS (r1, current_matrix),
17875 Z_delta, Z_delta_bytes);
17878 /* Set the cursor. */
17879 row = row_containing_pos (w, PT, r0, NULL, 0);
17880 if (row)
17881 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17882 return 1;
17886 /* Handle the case that changes are all below what is displayed in
17887 the window, and that PT is in the window. This shortcut cannot
17888 be taken if ZV is visible in the window, and text has been added
17889 there that is visible in the window. */
17890 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17891 /* ZV is not visible in the window, or there are no
17892 changes at ZV, actually. */
17893 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17894 || first_changed_charpos == last_changed_charpos))
17896 struct glyph_row *r0;
17898 /* Give up if PT is not in the window. Note that it already has
17899 been checked at the start of try_window_id that PT is not in
17900 front of the window start. */
17901 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17902 GIVE_UP (14);
17904 /* If window start is unchanged, we can reuse the whole matrix
17905 as is, without changing glyph positions since no text has
17906 been added/removed in front of the window end. */
17907 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17908 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17909 /* PT must not be in a partially visible line. */
17910 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17911 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17913 /* We have to compute the window end anew since text
17914 could have been added/removed after it. */
17915 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17916 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17918 /* Set the cursor. */
17919 row = row_containing_pos (w, PT, r0, NULL, 0);
17920 if (row)
17921 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17922 return 2;
17926 /* Give up if window start is in the changed area.
17928 The condition used to read
17930 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17932 but why that was tested escapes me at the moment. */
17933 if (CHARPOS (start) >= first_changed_charpos
17934 && CHARPOS (start) <= last_changed_charpos)
17935 GIVE_UP (15);
17937 /* Check that window start agrees with the start of the first glyph
17938 row in its current matrix. Check this after we know the window
17939 start is not in changed text, otherwise positions would not be
17940 comparable. */
17941 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17942 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17943 GIVE_UP (16);
17945 /* Give up if the window ends in strings. Overlay strings
17946 at the end are difficult to handle, so don't try. */
17947 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17948 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17949 GIVE_UP (20);
17951 /* Compute the position at which we have to start displaying new
17952 lines. Some of the lines at the top of the window might be
17953 reusable because they are not displaying changed text. Find the
17954 last row in W's current matrix not affected by changes at the
17955 start of current_buffer. Value is null if changes start in the
17956 first line of window. */
17957 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17958 if (last_unchanged_at_beg_row)
17960 /* Avoid starting to display in the middle of a character, a TAB
17961 for instance. This is easier than to set up the iterator
17962 exactly, and it's not a frequent case, so the additional
17963 effort wouldn't really pay off. */
17964 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17965 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17966 && last_unchanged_at_beg_row > w->current_matrix->rows)
17967 --last_unchanged_at_beg_row;
17969 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17970 GIVE_UP (17);
17972 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
17973 GIVE_UP (18);
17974 start_pos = it.current.pos;
17976 /* Start displaying new lines in the desired matrix at the same
17977 vpos we would use in the current matrix, i.e. below
17978 last_unchanged_at_beg_row. */
17979 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17980 current_matrix);
17981 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17982 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17984 eassert (it.hpos == 0 && it.current_x == 0);
17986 else
17988 /* There are no reusable lines at the start of the window.
17989 Start displaying in the first text line. */
17990 start_display (&it, w, start);
17991 it.vpos = it.first_vpos;
17992 start_pos = it.current.pos;
17995 /* Find the first row that is not affected by changes at the end of
17996 the buffer. Value will be null if there is no unchanged row, in
17997 which case we must redisplay to the end of the window. delta
17998 will be set to the value by which buffer positions beginning with
17999 first_unchanged_at_end_row have to be adjusted due to text
18000 changes. */
18001 first_unchanged_at_end_row
18002 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18003 IF_DEBUG (debug_delta = delta);
18004 IF_DEBUG (debug_delta_bytes = delta_bytes);
18006 /* Set stop_pos to the buffer position up to which we will have to
18007 display new lines. If first_unchanged_at_end_row != NULL, this
18008 is the buffer position of the start of the line displayed in that
18009 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18010 that we don't stop at a buffer position. */
18011 stop_pos = 0;
18012 if (first_unchanged_at_end_row)
18014 eassert (last_unchanged_at_beg_row == NULL
18015 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18017 /* If this is a continuation line, move forward to the next one
18018 that isn't. Changes in lines above affect this line.
18019 Caution: this may move first_unchanged_at_end_row to a row
18020 not displaying text. */
18021 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18022 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18023 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18024 < it.last_visible_y))
18025 ++first_unchanged_at_end_row;
18027 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18028 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18029 >= it.last_visible_y))
18030 first_unchanged_at_end_row = NULL;
18031 else
18033 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18034 + delta);
18035 first_unchanged_at_end_vpos
18036 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18037 eassert (stop_pos >= Z - END_UNCHANGED);
18040 else if (last_unchanged_at_beg_row == NULL)
18041 GIVE_UP (19);
18044 #ifdef GLYPH_DEBUG
18046 /* Either there is no unchanged row at the end, or the one we have
18047 now displays text. This is a necessary condition for the window
18048 end pos calculation at the end of this function. */
18049 eassert (first_unchanged_at_end_row == NULL
18050 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18052 debug_last_unchanged_at_beg_vpos
18053 = (last_unchanged_at_beg_row
18054 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18055 : -1);
18056 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18058 #endif /* GLYPH_DEBUG */
18061 /* Display new lines. Set last_text_row to the last new line
18062 displayed which has text on it, i.e. might end up as being the
18063 line where the window_end_vpos is. */
18064 w->cursor.vpos = -1;
18065 last_text_row = NULL;
18066 overlay_arrow_seen = false;
18067 if (it.current_y < it.last_visible_y
18068 && !f->fonts_changed
18069 && (first_unchanged_at_end_row == NULL
18070 || IT_CHARPOS (it) < stop_pos))
18071 it.glyph_row->reversed_p = false;
18072 while (it.current_y < it.last_visible_y
18073 && !f->fonts_changed
18074 && (first_unchanged_at_end_row == NULL
18075 || IT_CHARPOS (it) < stop_pos))
18077 if (display_line (&it))
18078 last_text_row = it.glyph_row - 1;
18081 if (f->fonts_changed)
18082 return -1;
18084 /* The redisplay iterations in display_line above could have
18085 triggered font-lock, which could have done something that
18086 invalidates IT->w window's end-point information, on which we
18087 rely below. E.g., one package, which will remain unnamed, used
18088 to install a font-lock-fontify-region-function that called
18089 bury-buffer, whose side effect is to switch the buffer displayed
18090 by IT->w, and that predictably resets IT->w's window_end_valid
18091 flag, which we already tested at the entry to this function.
18092 Amply punish such packages/modes by giving up on this
18093 optimization in those cases. */
18094 if (!w->window_end_valid)
18096 clear_glyph_matrix (w->desired_matrix);
18097 return -1;
18100 /* Compute differences in buffer positions, y-positions etc. for
18101 lines reused at the bottom of the window. Compute what we can
18102 scroll. */
18103 if (first_unchanged_at_end_row
18104 /* No lines reused because we displayed everything up to the
18105 bottom of the window. */
18106 && it.current_y < it.last_visible_y)
18108 dvpos = (it.vpos
18109 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18110 current_matrix));
18111 dy = it.current_y - first_unchanged_at_end_row->y;
18112 run.current_y = first_unchanged_at_end_row->y;
18113 run.desired_y = run.current_y + dy;
18114 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18116 else
18118 delta = delta_bytes = dvpos = dy
18119 = run.current_y = run.desired_y = run.height = 0;
18120 first_unchanged_at_end_row = NULL;
18122 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18125 /* Find the cursor if not already found. We have to decide whether
18126 PT will appear on this window (it sometimes doesn't, but this is
18127 not a very frequent case.) This decision has to be made before
18128 the current matrix is altered. A value of cursor.vpos < 0 means
18129 that PT is either in one of the lines beginning at
18130 first_unchanged_at_end_row or below the window. Don't care for
18131 lines that might be displayed later at the window end; as
18132 mentioned, this is not a frequent case. */
18133 if (w->cursor.vpos < 0)
18135 /* Cursor in unchanged rows at the top? */
18136 if (PT < CHARPOS (start_pos)
18137 && last_unchanged_at_beg_row)
18139 row = row_containing_pos (w, PT,
18140 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18141 last_unchanged_at_beg_row + 1, 0);
18142 if (row)
18143 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18146 /* Start from first_unchanged_at_end_row looking for PT. */
18147 else if (first_unchanged_at_end_row)
18149 row = row_containing_pos (w, PT - delta,
18150 first_unchanged_at_end_row, NULL, 0);
18151 if (row)
18152 set_cursor_from_row (w, row, w->current_matrix, delta,
18153 delta_bytes, dy, dvpos);
18156 /* Give up if cursor was not found. */
18157 if (w->cursor.vpos < 0)
18159 clear_glyph_matrix (w->desired_matrix);
18160 return -1;
18164 /* Don't let the cursor end in the scroll margins. */
18166 int this_scroll_margin, cursor_height;
18167 int frame_line_height = default_line_pixel_height (w);
18168 int window_total_lines
18169 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18171 this_scroll_margin =
18172 max (0, min (scroll_margin, window_total_lines / 4));
18173 this_scroll_margin *= frame_line_height;
18174 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18176 if ((w->cursor.y < this_scroll_margin
18177 && CHARPOS (start) > BEGV)
18178 /* Old redisplay didn't take scroll margin into account at the bottom,
18179 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18180 || (w->cursor.y + (make_cursor_line_fully_visible_p
18181 ? cursor_height + this_scroll_margin
18182 : 1)) > it.last_visible_y)
18184 w->cursor.vpos = -1;
18185 clear_glyph_matrix (w->desired_matrix);
18186 return -1;
18190 /* Scroll the display. Do it before changing the current matrix so
18191 that xterm.c doesn't get confused about where the cursor glyph is
18192 found. */
18193 if (dy && run.height)
18195 update_begin (f);
18197 if (FRAME_WINDOW_P (f))
18199 FRAME_RIF (f)->update_window_begin_hook (w);
18200 FRAME_RIF (f)->clear_window_mouse_face (w);
18201 FRAME_RIF (f)->scroll_run_hook (w, &run);
18202 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18204 else
18206 /* Terminal frame. In this case, dvpos gives the number of
18207 lines to scroll by; dvpos < 0 means scroll up. */
18208 int from_vpos
18209 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18210 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18211 int end = (WINDOW_TOP_EDGE_LINE (w)
18212 + WINDOW_WANTS_HEADER_LINE_P (w)
18213 + window_internal_height (w));
18215 #if defined (HAVE_GPM) || defined (MSDOS)
18216 x_clear_window_mouse_face (w);
18217 #endif
18218 /* Perform the operation on the screen. */
18219 if (dvpos > 0)
18221 /* Scroll last_unchanged_at_beg_row to the end of the
18222 window down dvpos lines. */
18223 set_terminal_window (f, end);
18225 /* On dumb terminals delete dvpos lines at the end
18226 before inserting dvpos empty lines. */
18227 if (!FRAME_SCROLL_REGION_OK (f))
18228 ins_del_lines (f, end - dvpos, -dvpos);
18230 /* Insert dvpos empty lines in front of
18231 last_unchanged_at_beg_row. */
18232 ins_del_lines (f, from, dvpos);
18234 else if (dvpos < 0)
18236 /* Scroll up last_unchanged_at_beg_vpos to the end of
18237 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18238 set_terminal_window (f, end);
18240 /* Delete dvpos lines in front of
18241 last_unchanged_at_beg_vpos. ins_del_lines will set
18242 the cursor to the given vpos and emit |dvpos| delete
18243 line sequences. */
18244 ins_del_lines (f, from + dvpos, dvpos);
18246 /* On a dumb terminal insert dvpos empty lines at the
18247 end. */
18248 if (!FRAME_SCROLL_REGION_OK (f))
18249 ins_del_lines (f, end + dvpos, -dvpos);
18252 set_terminal_window (f, 0);
18255 update_end (f);
18258 /* Shift reused rows of the current matrix to the right position.
18259 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18260 text. */
18261 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18262 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18263 if (dvpos < 0)
18265 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18266 bottom_vpos, dvpos);
18267 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18268 bottom_vpos);
18270 else if (dvpos > 0)
18272 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18273 bottom_vpos, dvpos);
18274 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18275 first_unchanged_at_end_vpos + dvpos);
18278 /* For frame-based redisplay, make sure that current frame and window
18279 matrix are in sync with respect to glyph memory. */
18280 if (!FRAME_WINDOW_P (f))
18281 sync_frame_with_window_matrix_rows (w);
18283 /* Adjust buffer positions in reused rows. */
18284 if (delta || delta_bytes)
18285 increment_matrix_positions (current_matrix,
18286 first_unchanged_at_end_vpos + dvpos,
18287 bottom_vpos, delta, delta_bytes);
18289 /* Adjust Y positions. */
18290 if (dy)
18291 shift_glyph_matrix (w, current_matrix,
18292 first_unchanged_at_end_vpos + dvpos,
18293 bottom_vpos, dy);
18295 if (first_unchanged_at_end_row)
18297 first_unchanged_at_end_row += dvpos;
18298 if (first_unchanged_at_end_row->y >= it.last_visible_y
18299 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18300 first_unchanged_at_end_row = NULL;
18303 /* If scrolling up, there may be some lines to display at the end of
18304 the window. */
18305 last_text_row_at_end = NULL;
18306 if (dy < 0)
18308 /* Scrolling up can leave for example a partially visible line
18309 at the end of the window to be redisplayed. */
18310 /* Set last_row to the glyph row in the current matrix where the
18311 window end line is found. It has been moved up or down in
18312 the matrix by dvpos. */
18313 int last_vpos = w->window_end_vpos + dvpos;
18314 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18316 /* If last_row is the window end line, it should display text. */
18317 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18319 /* If window end line was partially visible before, begin
18320 displaying at that line. Otherwise begin displaying with the
18321 line following it. */
18322 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18324 init_to_row_start (&it, w, last_row);
18325 it.vpos = last_vpos;
18326 it.current_y = last_row->y;
18328 else
18330 init_to_row_end (&it, w, last_row);
18331 it.vpos = 1 + last_vpos;
18332 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18333 ++last_row;
18336 /* We may start in a continuation line. If so, we have to
18337 get the right continuation_lines_width and current_x. */
18338 it.continuation_lines_width = last_row->continuation_lines_width;
18339 it.hpos = it.current_x = 0;
18341 /* Display the rest of the lines at the window end. */
18342 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18343 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18345 /* Is it always sure that the display agrees with lines in
18346 the current matrix? I don't think so, so we mark rows
18347 displayed invalid in the current matrix by setting their
18348 enabled_p flag to false. */
18349 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18350 if (display_line (&it))
18351 last_text_row_at_end = it.glyph_row - 1;
18355 /* Update window_end_pos and window_end_vpos. */
18356 if (first_unchanged_at_end_row && !last_text_row_at_end)
18358 /* Window end line if one of the preserved rows from the current
18359 matrix. Set row to the last row displaying text in current
18360 matrix starting at first_unchanged_at_end_row, after
18361 scrolling. */
18362 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18363 row = find_last_row_displaying_text (w->current_matrix, &it,
18364 first_unchanged_at_end_row);
18365 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18366 adjust_window_ends (w, row, true);
18367 eassert (w->window_end_bytepos >= 0);
18368 IF_DEBUG (debug_method_add (w, "A"));
18370 else if (last_text_row_at_end)
18372 adjust_window_ends (w, last_text_row_at_end, false);
18373 eassert (w->window_end_bytepos >= 0);
18374 IF_DEBUG (debug_method_add (w, "B"));
18376 else if (last_text_row)
18378 /* We have displayed either to the end of the window or at the
18379 end of the window, i.e. the last row with text is to be found
18380 in the desired matrix. */
18381 adjust_window_ends (w, last_text_row, false);
18382 eassert (w->window_end_bytepos >= 0);
18384 else if (first_unchanged_at_end_row == NULL
18385 && last_text_row == NULL
18386 && last_text_row_at_end == NULL)
18388 /* Displayed to end of window, but no line containing text was
18389 displayed. Lines were deleted at the end of the window. */
18390 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18391 int vpos = w->window_end_vpos;
18392 struct glyph_row *current_row = current_matrix->rows + vpos;
18393 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18395 for (row = NULL;
18396 row == NULL && vpos >= first_vpos;
18397 --vpos, --current_row, --desired_row)
18399 if (desired_row->enabled_p)
18401 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18402 row = desired_row;
18404 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18405 row = current_row;
18408 eassert (row != NULL);
18409 w->window_end_vpos = vpos + 1;
18410 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18411 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18412 eassert (w->window_end_bytepos >= 0);
18413 IF_DEBUG (debug_method_add (w, "C"));
18415 else
18416 emacs_abort ();
18418 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18419 debug_end_vpos = w->window_end_vpos));
18421 /* Record that display has not been completed. */
18422 w->window_end_valid = false;
18423 w->desired_matrix->no_scrolling_p = true;
18424 return 3;
18426 #undef GIVE_UP
18431 /***********************************************************************
18432 More debugging support
18433 ***********************************************************************/
18435 #ifdef GLYPH_DEBUG
18437 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18438 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18439 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18442 /* Dump the contents of glyph matrix MATRIX on stderr.
18444 GLYPHS 0 means don't show glyph contents.
18445 GLYPHS 1 means show glyphs in short form
18446 GLYPHS > 1 means show glyphs in long form. */
18448 void
18449 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18451 int i;
18452 for (i = 0; i < matrix->nrows; ++i)
18453 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18457 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18458 the glyph row and area where the glyph comes from. */
18460 void
18461 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18463 if (glyph->type == CHAR_GLYPH
18464 || glyph->type == GLYPHLESS_GLYPH)
18466 fprintf (stderr,
18467 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18468 glyph - row->glyphs[TEXT_AREA],
18469 (glyph->type == CHAR_GLYPH
18470 ? 'C'
18471 : 'G'),
18472 glyph->charpos,
18473 (BUFFERP (glyph->object)
18474 ? 'B'
18475 : (STRINGP (glyph->object)
18476 ? 'S'
18477 : (NILP (glyph->object)
18478 ? '0'
18479 : '-'))),
18480 glyph->pixel_width,
18481 glyph->u.ch,
18482 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18483 ? glyph->u.ch
18484 : '.'),
18485 glyph->face_id,
18486 glyph->left_box_line_p,
18487 glyph->right_box_line_p);
18489 else if (glyph->type == STRETCH_GLYPH)
18491 fprintf (stderr,
18492 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18493 glyph - row->glyphs[TEXT_AREA],
18494 'S',
18495 glyph->charpos,
18496 (BUFFERP (glyph->object)
18497 ? 'B'
18498 : (STRINGP (glyph->object)
18499 ? 'S'
18500 : (NILP (glyph->object)
18501 ? '0'
18502 : '-'))),
18503 glyph->pixel_width,
18505 ' ',
18506 glyph->face_id,
18507 glyph->left_box_line_p,
18508 glyph->right_box_line_p);
18510 else if (glyph->type == IMAGE_GLYPH)
18512 fprintf (stderr,
18513 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18514 glyph - row->glyphs[TEXT_AREA],
18515 'I',
18516 glyph->charpos,
18517 (BUFFERP (glyph->object)
18518 ? 'B'
18519 : (STRINGP (glyph->object)
18520 ? 'S'
18521 : (NILP (glyph->object)
18522 ? '0'
18523 : '-'))),
18524 glyph->pixel_width,
18525 glyph->u.img_id,
18526 '.',
18527 glyph->face_id,
18528 glyph->left_box_line_p,
18529 glyph->right_box_line_p);
18531 else if (glyph->type == COMPOSITE_GLYPH)
18533 fprintf (stderr,
18534 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18535 glyph - row->glyphs[TEXT_AREA],
18536 '+',
18537 glyph->charpos,
18538 (BUFFERP (glyph->object)
18539 ? 'B'
18540 : (STRINGP (glyph->object)
18541 ? 'S'
18542 : (NILP (glyph->object)
18543 ? '0'
18544 : '-'))),
18545 glyph->pixel_width,
18546 glyph->u.cmp.id);
18547 if (glyph->u.cmp.automatic)
18548 fprintf (stderr,
18549 "[%d-%d]",
18550 glyph->slice.cmp.from, glyph->slice.cmp.to);
18551 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18552 glyph->face_id,
18553 glyph->left_box_line_p,
18554 glyph->right_box_line_p);
18559 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18560 GLYPHS 0 means don't show glyph contents.
18561 GLYPHS 1 means show glyphs in short form
18562 GLYPHS > 1 means show glyphs in long form. */
18564 void
18565 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18567 if (glyphs != 1)
18569 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18570 fprintf (stderr, "==============================================================================\n");
18572 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18573 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18574 vpos,
18575 MATRIX_ROW_START_CHARPOS (row),
18576 MATRIX_ROW_END_CHARPOS (row),
18577 row->used[TEXT_AREA],
18578 row->contains_overlapping_glyphs_p,
18579 row->enabled_p,
18580 row->truncated_on_left_p,
18581 row->truncated_on_right_p,
18582 row->continued_p,
18583 MATRIX_ROW_CONTINUATION_LINE_P (row),
18584 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18585 row->ends_at_zv_p,
18586 row->fill_line_p,
18587 row->ends_in_middle_of_char_p,
18588 row->starts_in_middle_of_char_p,
18589 row->mouse_face_p,
18590 row->x,
18591 row->y,
18592 row->pixel_width,
18593 row->height,
18594 row->visible_height,
18595 row->ascent,
18596 row->phys_ascent);
18597 /* The next 3 lines should align to "Start" in the header. */
18598 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18599 row->end.overlay_string_index,
18600 row->continuation_lines_width);
18601 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18602 CHARPOS (row->start.string_pos),
18603 CHARPOS (row->end.string_pos));
18604 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18605 row->end.dpvec_index);
18608 if (glyphs > 1)
18610 int area;
18612 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18614 struct glyph *glyph = row->glyphs[area];
18615 struct glyph *glyph_end = glyph + row->used[area];
18617 /* Glyph for a line end in text. */
18618 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18619 ++glyph_end;
18621 if (glyph < glyph_end)
18622 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18624 for (; glyph < glyph_end; ++glyph)
18625 dump_glyph (row, glyph, area);
18628 else if (glyphs == 1)
18630 int area;
18631 char s[SHRT_MAX + 4];
18633 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18635 int i;
18637 for (i = 0; i < row->used[area]; ++i)
18639 struct glyph *glyph = row->glyphs[area] + i;
18640 if (i == row->used[area] - 1
18641 && area == TEXT_AREA
18642 && NILP (glyph->object)
18643 && glyph->type == CHAR_GLYPH
18644 && glyph->u.ch == ' ')
18646 strcpy (&s[i], "[\\n]");
18647 i += 4;
18649 else if (glyph->type == CHAR_GLYPH
18650 && glyph->u.ch < 0x80
18651 && glyph->u.ch >= ' ')
18652 s[i] = glyph->u.ch;
18653 else
18654 s[i] = '.';
18657 s[i] = '\0';
18658 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18664 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18665 Sdump_glyph_matrix, 0, 1, "p",
18666 doc: /* Dump the current matrix of the selected window to stderr.
18667 Shows contents of glyph row structures. With non-nil
18668 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18669 glyphs in short form, otherwise show glyphs in long form.
18671 Interactively, no argument means show glyphs in short form;
18672 with numeric argument, its value is passed as the GLYPHS flag. */)
18673 (Lisp_Object glyphs)
18675 struct window *w = XWINDOW (selected_window);
18676 struct buffer *buffer = XBUFFER (w->contents);
18678 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18679 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18680 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18681 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18682 fprintf (stderr, "=============================================\n");
18683 dump_glyph_matrix (w->current_matrix,
18684 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18685 return Qnil;
18689 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18690 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18691 Only text-mode frames have frame glyph matrices. */)
18692 (void)
18694 struct frame *f = XFRAME (selected_frame);
18696 if (f->current_matrix)
18697 dump_glyph_matrix (f->current_matrix, 1);
18698 else
18699 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18700 return Qnil;
18704 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18705 doc: /* Dump glyph row ROW to stderr.
18706 GLYPH 0 means don't dump glyphs.
18707 GLYPH 1 means dump glyphs in short form.
18708 GLYPH > 1 or omitted means dump glyphs in long form. */)
18709 (Lisp_Object row, Lisp_Object glyphs)
18711 struct glyph_matrix *matrix;
18712 EMACS_INT vpos;
18714 CHECK_NUMBER (row);
18715 matrix = XWINDOW (selected_window)->current_matrix;
18716 vpos = XINT (row);
18717 if (vpos >= 0 && vpos < matrix->nrows)
18718 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18719 vpos,
18720 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18721 return Qnil;
18725 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18726 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18727 GLYPH 0 means don't dump glyphs.
18728 GLYPH 1 means dump glyphs in short form.
18729 GLYPH > 1 or omitted means dump glyphs in long form.
18731 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18732 do nothing. */)
18733 (Lisp_Object row, Lisp_Object glyphs)
18735 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18736 struct frame *sf = SELECTED_FRAME ();
18737 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18738 EMACS_INT vpos;
18740 CHECK_NUMBER (row);
18741 vpos = XINT (row);
18742 if (vpos >= 0 && vpos < m->nrows)
18743 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18744 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18745 #endif
18746 return Qnil;
18750 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18751 doc: /* Toggle tracing of redisplay.
18752 With ARG, turn tracing on if and only if ARG is positive. */)
18753 (Lisp_Object arg)
18755 if (NILP (arg))
18756 trace_redisplay_p = !trace_redisplay_p;
18757 else
18759 arg = Fprefix_numeric_value (arg);
18760 trace_redisplay_p = XINT (arg) > 0;
18763 return Qnil;
18767 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18768 doc: /* Like `format', but print result to stderr.
18769 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18770 (ptrdiff_t nargs, Lisp_Object *args)
18772 Lisp_Object s = Fformat (nargs, args);
18773 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18774 return Qnil;
18777 #endif /* GLYPH_DEBUG */
18781 /***********************************************************************
18782 Building Desired Matrix Rows
18783 ***********************************************************************/
18785 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18786 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18788 static struct glyph_row *
18789 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18791 struct frame *f = XFRAME (WINDOW_FRAME (w));
18792 struct buffer *buffer = XBUFFER (w->contents);
18793 struct buffer *old = current_buffer;
18794 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18795 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18796 const unsigned char *arrow_end = arrow_string + arrow_len;
18797 const unsigned char *p;
18798 struct it it;
18799 bool multibyte_p;
18800 int n_glyphs_before;
18802 set_buffer_temp (buffer);
18803 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18804 scratch_glyph_row.reversed_p = false;
18805 it.glyph_row->used[TEXT_AREA] = 0;
18806 SET_TEXT_POS (it.position, 0, 0);
18808 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18809 p = arrow_string;
18810 while (p < arrow_end)
18812 Lisp_Object face, ilisp;
18814 /* Get the next character. */
18815 if (multibyte_p)
18816 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18817 else
18819 it.c = it.char_to_display = *p, it.len = 1;
18820 if (! ASCII_CHAR_P (it.c))
18821 it.char_to_display = BYTE8_TO_CHAR (it.c);
18823 p += it.len;
18825 /* Get its face. */
18826 ilisp = make_number (p - arrow_string);
18827 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18828 it.face_id = compute_char_face (f, it.char_to_display, face);
18830 /* Compute its width, get its glyphs. */
18831 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18832 SET_TEXT_POS (it.position, -1, -1);
18833 PRODUCE_GLYPHS (&it);
18835 /* If this character doesn't fit any more in the line, we have
18836 to remove some glyphs. */
18837 if (it.current_x > it.last_visible_x)
18839 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18840 break;
18844 set_buffer_temp (old);
18845 return it.glyph_row;
18849 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18850 glyphs to insert is determined by produce_special_glyphs. */
18852 static void
18853 insert_left_trunc_glyphs (struct it *it)
18855 struct it truncate_it;
18856 struct glyph *from, *end, *to, *toend;
18858 eassert (!FRAME_WINDOW_P (it->f)
18859 || (!it->glyph_row->reversed_p
18860 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18861 || (it->glyph_row->reversed_p
18862 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18864 /* Get the truncation glyphs. */
18865 truncate_it = *it;
18866 truncate_it.current_x = 0;
18867 truncate_it.face_id = DEFAULT_FACE_ID;
18868 truncate_it.glyph_row = &scratch_glyph_row;
18869 truncate_it.area = TEXT_AREA;
18870 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18871 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18872 truncate_it.object = Qnil;
18873 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18875 /* Overwrite glyphs from IT with truncation glyphs. */
18876 if (!it->glyph_row->reversed_p)
18878 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18880 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18881 end = from + tused;
18882 to = it->glyph_row->glyphs[TEXT_AREA];
18883 toend = to + it->glyph_row->used[TEXT_AREA];
18884 if (FRAME_WINDOW_P (it->f))
18886 /* On GUI frames, when variable-size fonts are displayed,
18887 the truncation glyphs may need more pixels than the row's
18888 glyphs they overwrite. We overwrite more glyphs to free
18889 enough screen real estate, and enlarge the stretch glyph
18890 on the right (see display_line), if there is one, to
18891 preserve the screen position of the truncation glyphs on
18892 the right. */
18893 int w = 0;
18894 struct glyph *g = to;
18895 short used;
18897 /* The first glyph could be partially visible, in which case
18898 it->glyph_row->x will be negative. But we want the left
18899 truncation glyphs to be aligned at the left margin of the
18900 window, so we override the x coordinate at which the row
18901 will begin. */
18902 it->glyph_row->x = 0;
18903 while (g < toend && w < it->truncation_pixel_width)
18905 w += g->pixel_width;
18906 ++g;
18908 if (g - to - tused > 0)
18910 memmove (to + tused, g, (toend - g) * sizeof(*g));
18911 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18913 used = it->glyph_row->used[TEXT_AREA];
18914 if (it->glyph_row->truncated_on_right_p
18915 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18916 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18917 == STRETCH_GLYPH)
18919 int extra = w - it->truncation_pixel_width;
18921 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18925 while (from < end)
18926 *to++ = *from++;
18928 /* There may be padding glyphs left over. Overwrite them too. */
18929 if (!FRAME_WINDOW_P (it->f))
18931 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18933 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18934 while (from < end)
18935 *to++ = *from++;
18939 if (to > toend)
18940 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18942 else
18944 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18946 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18947 that back to front. */
18948 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18949 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18950 toend = it->glyph_row->glyphs[TEXT_AREA];
18951 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18952 if (FRAME_WINDOW_P (it->f))
18954 int w = 0;
18955 struct glyph *g = to;
18957 while (g >= toend && w < it->truncation_pixel_width)
18959 w += g->pixel_width;
18960 --g;
18962 if (to - g - tused > 0)
18963 to = g + tused;
18964 if (it->glyph_row->truncated_on_right_p
18965 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18966 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18968 int extra = w - it->truncation_pixel_width;
18970 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18974 while (from >= end && to >= toend)
18975 *to-- = *from--;
18976 if (!FRAME_WINDOW_P (it->f))
18978 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18980 from =
18981 truncate_it.glyph_row->glyphs[TEXT_AREA]
18982 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18983 while (from >= end && to >= toend)
18984 *to-- = *from--;
18987 if (from >= end)
18989 /* Need to free some room before prepending additional
18990 glyphs. */
18991 int move_by = from - end + 1;
18992 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18993 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18995 for ( ; g >= g0; g--)
18996 g[move_by] = *g;
18997 while (from >= end)
18998 *to-- = *from--;
18999 it->glyph_row->used[TEXT_AREA] += move_by;
19004 /* Compute the hash code for ROW. */
19005 unsigned
19006 row_hash (struct glyph_row *row)
19008 int area, k;
19009 unsigned hashval = 0;
19011 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19012 for (k = 0; k < row->used[area]; ++k)
19013 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19014 + row->glyphs[area][k].u.val
19015 + row->glyphs[area][k].face_id
19016 + row->glyphs[area][k].padding_p
19017 + (row->glyphs[area][k].type << 2));
19019 return hashval;
19022 /* Compute the pixel height and width of IT->glyph_row.
19024 Most of the time, ascent and height of a display line will be equal
19025 to the max_ascent and max_height values of the display iterator
19026 structure. This is not the case if
19028 1. We hit ZV without displaying anything. In this case, max_ascent
19029 and max_height will be zero.
19031 2. We have some glyphs that don't contribute to the line height.
19032 (The glyph row flag contributes_to_line_height_p is for future
19033 pixmap extensions).
19035 The first case is easily covered by using default values because in
19036 these cases, the line height does not really matter, except that it
19037 must not be zero. */
19039 static void
19040 compute_line_metrics (struct it *it)
19042 struct glyph_row *row = it->glyph_row;
19044 if (FRAME_WINDOW_P (it->f))
19046 int i, min_y, max_y;
19048 /* The line may consist of one space only, that was added to
19049 place the cursor on it. If so, the row's height hasn't been
19050 computed yet. */
19051 if (row->height == 0)
19053 if (it->max_ascent + it->max_descent == 0)
19054 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19055 row->ascent = it->max_ascent;
19056 row->height = it->max_ascent + it->max_descent;
19057 row->phys_ascent = it->max_phys_ascent;
19058 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19059 row->extra_line_spacing = it->max_extra_line_spacing;
19062 /* Compute the width of this line. */
19063 row->pixel_width = row->x;
19064 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19065 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19067 eassert (row->pixel_width >= 0);
19068 eassert (row->ascent >= 0 && row->height > 0);
19070 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19071 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19073 /* If first line's physical ascent is larger than its logical
19074 ascent, use the physical ascent, and make the row taller.
19075 This makes accented characters fully visible. */
19076 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19077 && row->phys_ascent > row->ascent)
19079 row->height += row->phys_ascent - row->ascent;
19080 row->ascent = row->phys_ascent;
19083 /* Compute how much of the line is visible. */
19084 row->visible_height = row->height;
19086 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19087 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19089 if (row->y < min_y)
19090 row->visible_height -= min_y - row->y;
19091 if (row->y + row->height > max_y)
19092 row->visible_height -= row->y + row->height - max_y;
19094 else
19096 row->pixel_width = row->used[TEXT_AREA];
19097 if (row->continued_p)
19098 row->pixel_width -= it->continuation_pixel_width;
19099 else if (row->truncated_on_right_p)
19100 row->pixel_width -= it->truncation_pixel_width;
19101 row->ascent = row->phys_ascent = 0;
19102 row->height = row->phys_height = row->visible_height = 1;
19103 row->extra_line_spacing = 0;
19106 /* Compute a hash code for this row. */
19107 row->hash = row_hash (row);
19109 it->max_ascent = it->max_descent = 0;
19110 it->max_phys_ascent = it->max_phys_descent = 0;
19114 /* Append one space to the glyph row of iterator IT if doing a
19115 window-based redisplay. The space has the same face as
19116 IT->face_id. Value is true if a space was added.
19118 This function is called to make sure that there is always one glyph
19119 at the end of a glyph row that the cursor can be set on under
19120 window-systems. (If there weren't such a glyph we would not know
19121 how wide and tall a box cursor should be displayed).
19123 At the same time this space let's a nicely handle clearing to the
19124 end of the line if the row ends in italic text. */
19126 static bool
19127 append_space_for_newline (struct it *it, bool default_face_p)
19129 if (FRAME_WINDOW_P (it->f))
19131 int n = it->glyph_row->used[TEXT_AREA];
19133 if (it->glyph_row->glyphs[TEXT_AREA] + n
19134 < it->glyph_row->glyphs[1 + TEXT_AREA])
19136 /* Save some values that must not be changed.
19137 Must save IT->c and IT->len because otherwise
19138 ITERATOR_AT_END_P wouldn't work anymore after
19139 append_space_for_newline has been called. */
19140 enum display_element_type saved_what = it->what;
19141 int saved_c = it->c, saved_len = it->len;
19142 int saved_char_to_display = it->char_to_display;
19143 int saved_x = it->current_x;
19144 int saved_face_id = it->face_id;
19145 bool saved_box_end = it->end_of_box_run_p;
19146 struct text_pos saved_pos;
19147 Lisp_Object saved_object;
19148 struct face *face;
19150 saved_object = it->object;
19151 saved_pos = it->position;
19153 it->what = IT_CHARACTER;
19154 memset (&it->position, 0, sizeof it->position);
19155 it->object = Qnil;
19156 it->c = it->char_to_display = ' ';
19157 it->len = 1;
19159 /* If the default face was remapped, be sure to use the
19160 remapped face for the appended newline. */
19161 if (default_face_p)
19162 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19163 else if (it->face_before_selective_p)
19164 it->face_id = it->saved_face_id;
19165 face = FACE_FROM_ID (it->f, it->face_id);
19166 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19167 /* In R2L rows, we will prepend a stretch glyph that will
19168 have the end_of_box_run_p flag set for it, so there's no
19169 need for the appended newline glyph to have that flag
19170 set. */
19171 if (it->glyph_row->reversed_p
19172 /* But if the appended newline glyph goes all the way to
19173 the end of the row, there will be no stretch glyph,
19174 so leave the box flag set. */
19175 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19176 it->end_of_box_run_p = false;
19178 PRODUCE_GLYPHS (it);
19180 it->override_ascent = -1;
19181 it->constrain_row_ascent_descent_p = false;
19182 it->current_x = saved_x;
19183 it->object = saved_object;
19184 it->position = saved_pos;
19185 it->what = saved_what;
19186 it->face_id = saved_face_id;
19187 it->len = saved_len;
19188 it->c = saved_c;
19189 it->char_to_display = saved_char_to_display;
19190 it->end_of_box_run_p = saved_box_end;
19191 return true;
19195 return false;
19199 /* Extend the face of the last glyph in the text area of IT->glyph_row
19200 to the end of the display line. Called from display_line. If the
19201 glyph row is empty, add a space glyph to it so that we know the
19202 face to draw. Set the glyph row flag fill_line_p. If the glyph
19203 row is R2L, prepend a stretch glyph to cover the empty space to the
19204 left of the leftmost glyph. */
19206 static void
19207 extend_face_to_end_of_line (struct it *it)
19209 struct face *face, *default_face;
19210 struct frame *f = it->f;
19212 /* If line is already filled, do nothing. Non window-system frames
19213 get a grace of one more ``pixel'' because their characters are
19214 1-``pixel'' wide, so they hit the equality too early. This grace
19215 is needed only for R2L rows that are not continued, to produce
19216 one extra blank where we could display the cursor. */
19217 if ((it->current_x >= it->last_visible_x
19218 + (!FRAME_WINDOW_P (f)
19219 && it->glyph_row->reversed_p
19220 && !it->glyph_row->continued_p))
19221 /* If the window has display margins, we will need to extend
19222 their face even if the text area is filled. */
19223 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19224 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19225 return;
19227 /* The default face, possibly remapped. */
19228 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19230 /* Face extension extends the background and box of IT->face_id
19231 to the end of the line. If the background equals the background
19232 of the frame, we don't have to do anything. */
19233 if (it->face_before_selective_p)
19234 face = FACE_FROM_ID (f, it->saved_face_id);
19235 else
19236 face = FACE_FROM_ID (f, it->face_id);
19238 if (FRAME_WINDOW_P (f)
19239 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19240 && face->box == FACE_NO_BOX
19241 && face->background == FRAME_BACKGROUND_PIXEL (f)
19242 #ifdef HAVE_WINDOW_SYSTEM
19243 && !face->stipple
19244 #endif
19245 && !it->glyph_row->reversed_p)
19246 return;
19248 /* Set the glyph row flag indicating that the face of the last glyph
19249 in the text area has to be drawn to the end of the text area. */
19250 it->glyph_row->fill_line_p = true;
19252 /* If current character of IT is not ASCII, make sure we have the
19253 ASCII face. This will be automatically undone the next time
19254 get_next_display_element returns a multibyte character. Note
19255 that the character will always be single byte in unibyte
19256 text. */
19257 if (!ASCII_CHAR_P (it->c))
19259 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19262 if (FRAME_WINDOW_P (f))
19264 /* If the row is empty, add a space with the current face of IT,
19265 so that we know which face to draw. */
19266 if (it->glyph_row->used[TEXT_AREA] == 0)
19268 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19269 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19270 it->glyph_row->used[TEXT_AREA] = 1;
19272 /* Mode line and the header line don't have margins, and
19273 likewise the frame's tool-bar window, if there is any. */
19274 if (!(it->glyph_row->mode_line_p
19275 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19276 || (WINDOWP (f->tool_bar_window)
19277 && it->w == XWINDOW (f->tool_bar_window))
19278 #endif
19281 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19282 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19284 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19285 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19286 default_face->id;
19287 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19289 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19290 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19292 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19293 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19294 default_face->id;
19295 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19298 #ifdef HAVE_WINDOW_SYSTEM
19299 if (it->glyph_row->reversed_p)
19301 /* Prepend a stretch glyph to the row, such that the
19302 rightmost glyph will be drawn flushed all the way to the
19303 right margin of the window. The stretch glyph that will
19304 occupy the empty space, if any, to the left of the
19305 glyphs. */
19306 struct font *font = face->font ? face->font : FRAME_FONT (f);
19307 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19308 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19309 struct glyph *g;
19310 int row_width, stretch_ascent, stretch_width;
19311 struct text_pos saved_pos;
19312 int saved_face_id;
19313 bool saved_avoid_cursor, saved_box_start;
19315 for (row_width = 0, g = row_start; g < row_end; g++)
19316 row_width += g->pixel_width;
19318 /* FIXME: There are various minor display glitches in R2L
19319 rows when only one of the fringes is missing. The
19320 strange condition below produces the least bad effect. */
19321 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19322 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19323 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19324 stretch_width = window_box_width (it->w, TEXT_AREA);
19325 else
19326 stretch_width = it->last_visible_x - it->first_visible_x;
19327 stretch_width -= row_width;
19329 if (stretch_width > 0)
19331 stretch_ascent =
19332 (((it->ascent + it->descent)
19333 * FONT_BASE (font)) / FONT_HEIGHT (font));
19334 saved_pos = it->position;
19335 memset (&it->position, 0, sizeof it->position);
19336 saved_avoid_cursor = it->avoid_cursor_p;
19337 it->avoid_cursor_p = true;
19338 saved_face_id = it->face_id;
19339 saved_box_start = it->start_of_box_run_p;
19340 /* The last row's stretch glyph should get the default
19341 face, to avoid painting the rest of the window with
19342 the region face, if the region ends at ZV. */
19343 if (it->glyph_row->ends_at_zv_p)
19344 it->face_id = default_face->id;
19345 else
19346 it->face_id = face->id;
19347 it->start_of_box_run_p = false;
19348 append_stretch_glyph (it, Qnil, stretch_width,
19349 it->ascent + it->descent, stretch_ascent);
19350 it->position = saved_pos;
19351 it->avoid_cursor_p = saved_avoid_cursor;
19352 it->face_id = saved_face_id;
19353 it->start_of_box_run_p = saved_box_start;
19355 /* If stretch_width comes out negative, it means that the
19356 last glyph is only partially visible. In R2L rows, we
19357 want the leftmost glyph to be partially visible, so we
19358 need to give the row the corresponding left offset. */
19359 if (stretch_width < 0)
19360 it->glyph_row->x = stretch_width;
19362 #endif /* HAVE_WINDOW_SYSTEM */
19364 else
19366 /* Save some values that must not be changed. */
19367 int saved_x = it->current_x;
19368 struct text_pos saved_pos;
19369 Lisp_Object saved_object;
19370 enum display_element_type saved_what = it->what;
19371 int saved_face_id = it->face_id;
19373 saved_object = it->object;
19374 saved_pos = it->position;
19376 it->what = IT_CHARACTER;
19377 memset (&it->position, 0, sizeof it->position);
19378 it->object = Qnil;
19379 it->c = it->char_to_display = ' ';
19380 it->len = 1;
19382 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19383 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19384 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19385 && !it->glyph_row->mode_line_p
19386 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19388 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19389 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19391 for (it->current_x = 0; g < e; g++)
19392 it->current_x += g->pixel_width;
19394 it->area = LEFT_MARGIN_AREA;
19395 it->face_id = default_face->id;
19396 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19397 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19399 PRODUCE_GLYPHS (it);
19400 /* term.c:produce_glyphs advances it->current_x only for
19401 TEXT_AREA. */
19402 it->current_x += it->pixel_width;
19405 it->current_x = saved_x;
19406 it->area = TEXT_AREA;
19409 /* The last row's blank glyphs should get the default face, to
19410 avoid painting the rest of the window with the region face,
19411 if the region ends at ZV. */
19412 if (it->glyph_row->ends_at_zv_p)
19413 it->face_id = default_face->id;
19414 else
19415 it->face_id = face->id;
19416 PRODUCE_GLYPHS (it);
19418 while (it->current_x <= it->last_visible_x)
19419 PRODUCE_GLYPHS (it);
19421 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19422 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19423 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19424 && !it->glyph_row->mode_line_p
19425 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19427 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19428 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19430 for ( ; g < e; g++)
19431 it->current_x += g->pixel_width;
19433 it->area = RIGHT_MARGIN_AREA;
19434 it->face_id = default_face->id;
19435 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19436 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19438 PRODUCE_GLYPHS (it);
19439 it->current_x += it->pixel_width;
19442 it->area = TEXT_AREA;
19445 /* Don't count these blanks really. It would let us insert a left
19446 truncation glyph below and make us set the cursor on them, maybe. */
19447 it->current_x = saved_x;
19448 it->object = saved_object;
19449 it->position = saved_pos;
19450 it->what = saved_what;
19451 it->face_id = saved_face_id;
19456 /* Value is true if text starting at CHARPOS in current_buffer is
19457 trailing whitespace. */
19459 static bool
19460 trailing_whitespace_p (ptrdiff_t charpos)
19462 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19463 int c = 0;
19465 while (bytepos < ZV_BYTE
19466 && (c = FETCH_CHAR (bytepos),
19467 c == ' ' || c == '\t'))
19468 ++bytepos;
19470 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19472 if (bytepos != PT_BYTE)
19473 return true;
19475 return false;
19479 /* Highlight trailing whitespace, if any, in ROW. */
19481 static void
19482 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19484 int used = row->used[TEXT_AREA];
19486 if (used)
19488 struct glyph *start = row->glyphs[TEXT_AREA];
19489 struct glyph *glyph = start + used - 1;
19491 if (row->reversed_p)
19493 /* Right-to-left rows need to be processed in the opposite
19494 direction, so swap the edge pointers. */
19495 glyph = start;
19496 start = row->glyphs[TEXT_AREA] + used - 1;
19499 /* Skip over glyphs inserted to display the cursor at the
19500 end of a line, for extending the face of the last glyph
19501 to the end of the line on terminals, and for truncation
19502 and continuation glyphs. */
19503 if (!row->reversed_p)
19505 while (glyph >= start
19506 && glyph->type == CHAR_GLYPH
19507 && NILP (glyph->object))
19508 --glyph;
19510 else
19512 while (glyph <= start
19513 && glyph->type == CHAR_GLYPH
19514 && NILP (glyph->object))
19515 ++glyph;
19518 /* If last glyph is a space or stretch, and it's trailing
19519 whitespace, set the face of all trailing whitespace glyphs in
19520 IT->glyph_row to `trailing-whitespace'. */
19521 if ((row->reversed_p ? glyph <= start : glyph >= start)
19522 && BUFFERP (glyph->object)
19523 && (glyph->type == STRETCH_GLYPH
19524 || (glyph->type == CHAR_GLYPH
19525 && glyph->u.ch == ' '))
19526 && trailing_whitespace_p (glyph->charpos))
19528 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19529 if (face_id < 0)
19530 return;
19532 if (!row->reversed_p)
19534 while (glyph >= start
19535 && BUFFERP (glyph->object)
19536 && (glyph->type == STRETCH_GLYPH
19537 || (glyph->type == CHAR_GLYPH
19538 && glyph->u.ch == ' ')))
19539 (glyph--)->face_id = face_id;
19541 else
19543 while (glyph <= start
19544 && BUFFERP (glyph->object)
19545 && (glyph->type == STRETCH_GLYPH
19546 || (glyph->type == CHAR_GLYPH
19547 && glyph->u.ch == ' ')))
19548 (glyph++)->face_id = face_id;
19555 /* Value is true if glyph row ROW should be
19556 considered to hold the buffer position CHARPOS. */
19558 static bool
19559 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19561 bool result = true;
19563 if (charpos == CHARPOS (row->end.pos)
19564 || charpos == MATRIX_ROW_END_CHARPOS (row))
19566 /* Suppose the row ends on a string.
19567 Unless the row is continued, that means it ends on a newline
19568 in the string. If it's anything other than a display string
19569 (e.g., a before-string from an overlay), we don't want the
19570 cursor there. (This heuristic seems to give the optimal
19571 behavior for the various types of multi-line strings.)
19572 One exception: if the string has `cursor' property on one of
19573 its characters, we _do_ want the cursor there. */
19574 if (CHARPOS (row->end.string_pos) >= 0)
19576 if (row->continued_p)
19577 result = true;
19578 else
19580 /* Check for `display' property. */
19581 struct glyph *beg = row->glyphs[TEXT_AREA];
19582 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19583 struct glyph *glyph;
19585 result = false;
19586 for (glyph = end; glyph >= beg; --glyph)
19587 if (STRINGP (glyph->object))
19589 Lisp_Object prop
19590 = Fget_char_property (make_number (charpos),
19591 Qdisplay, Qnil);
19592 result =
19593 (!NILP (prop)
19594 && display_prop_string_p (prop, glyph->object));
19595 /* If there's a `cursor' property on one of the
19596 string's characters, this row is a cursor row,
19597 even though this is not a display string. */
19598 if (!result)
19600 Lisp_Object s = glyph->object;
19602 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19604 ptrdiff_t gpos = glyph->charpos;
19606 if (!NILP (Fget_char_property (make_number (gpos),
19607 Qcursor, s)))
19609 result = true;
19610 break;
19614 break;
19618 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19620 /* If the row ends in middle of a real character,
19621 and the line is continued, we want the cursor here.
19622 That's because CHARPOS (ROW->end.pos) would equal
19623 PT if PT is before the character. */
19624 if (!row->ends_in_ellipsis_p)
19625 result = row->continued_p;
19626 else
19627 /* If the row ends in an ellipsis, then
19628 CHARPOS (ROW->end.pos) will equal point after the
19629 invisible text. We want that position to be displayed
19630 after the ellipsis. */
19631 result = false;
19633 /* If the row ends at ZV, display the cursor at the end of that
19634 row instead of at the start of the row below. */
19635 else
19636 result = row->ends_at_zv_p;
19639 return result;
19642 /* Value is true if glyph row ROW should be
19643 used to hold the cursor. */
19645 static bool
19646 cursor_row_p (struct glyph_row *row)
19648 return row_for_charpos_p (row, PT);
19653 /* Push the property PROP so that it will be rendered at the current
19654 position in IT. Return true if PROP was successfully pushed, false
19655 otherwise. Called from handle_line_prefix to handle the
19656 `line-prefix' and `wrap-prefix' properties. */
19658 static bool
19659 push_prefix_prop (struct it *it, Lisp_Object prop)
19661 struct text_pos pos =
19662 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19664 eassert (it->method == GET_FROM_BUFFER
19665 || it->method == GET_FROM_DISPLAY_VECTOR
19666 || it->method == GET_FROM_STRING);
19668 /* We need to save the current buffer/string position, so it will be
19669 restored by pop_it, because iterate_out_of_display_property
19670 depends on that being set correctly, but some situations leave
19671 it->position not yet set when this function is called. */
19672 push_it (it, &pos);
19674 if (STRINGP (prop))
19676 if (SCHARS (prop) == 0)
19678 pop_it (it);
19679 return false;
19682 it->string = prop;
19683 it->string_from_prefix_prop_p = true;
19684 it->multibyte_p = STRING_MULTIBYTE (it->string);
19685 it->current.overlay_string_index = -1;
19686 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19687 it->end_charpos = it->string_nchars = SCHARS (it->string);
19688 it->method = GET_FROM_STRING;
19689 it->stop_charpos = 0;
19690 it->prev_stop = 0;
19691 it->base_level_stop = 0;
19693 /* Force paragraph direction to be that of the parent
19694 buffer/string. */
19695 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19696 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19697 else
19698 it->paragraph_embedding = L2R;
19700 /* Set up the bidi iterator for this display string. */
19701 if (it->bidi_p)
19703 it->bidi_it.string.lstring = it->string;
19704 it->bidi_it.string.s = NULL;
19705 it->bidi_it.string.schars = it->end_charpos;
19706 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19707 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19708 it->bidi_it.string.unibyte = !it->multibyte_p;
19709 it->bidi_it.w = it->w;
19710 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19713 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19715 it->method = GET_FROM_STRETCH;
19716 it->object = prop;
19718 #ifdef HAVE_WINDOW_SYSTEM
19719 else if (IMAGEP (prop))
19721 it->what = IT_IMAGE;
19722 it->image_id = lookup_image (it->f, prop);
19723 it->method = GET_FROM_IMAGE;
19725 #endif /* HAVE_WINDOW_SYSTEM */
19726 else
19728 pop_it (it); /* bogus display property, give up */
19729 return false;
19732 return true;
19735 /* Return the character-property PROP at the current position in IT. */
19737 static Lisp_Object
19738 get_it_property (struct it *it, Lisp_Object prop)
19740 Lisp_Object position, object = it->object;
19742 if (STRINGP (object))
19743 position = make_number (IT_STRING_CHARPOS (*it));
19744 else if (BUFFERP (object))
19746 position = make_number (IT_CHARPOS (*it));
19747 object = it->window;
19749 else
19750 return Qnil;
19752 return Fget_char_property (position, prop, object);
19755 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19757 static void
19758 handle_line_prefix (struct it *it)
19760 Lisp_Object prefix;
19762 if (it->continuation_lines_width > 0)
19764 prefix = get_it_property (it, Qwrap_prefix);
19765 if (NILP (prefix))
19766 prefix = Vwrap_prefix;
19768 else
19770 prefix = get_it_property (it, Qline_prefix);
19771 if (NILP (prefix))
19772 prefix = Vline_prefix;
19774 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19776 /* If the prefix is wider than the window, and we try to wrap
19777 it, it would acquire its own wrap prefix, and so on till the
19778 iterator stack overflows. So, don't wrap the prefix. */
19779 it->line_wrap = TRUNCATE;
19780 it->avoid_cursor_p = true;
19786 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19787 only for R2L lines from display_line and display_string, when they
19788 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19789 the line/string needs to be continued on the next glyph row. */
19790 static void
19791 unproduce_glyphs (struct it *it, int n)
19793 struct glyph *glyph, *end;
19795 eassert (it->glyph_row);
19796 eassert (it->glyph_row->reversed_p);
19797 eassert (it->area == TEXT_AREA);
19798 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19800 if (n > it->glyph_row->used[TEXT_AREA])
19801 n = it->glyph_row->used[TEXT_AREA];
19802 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19803 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19804 for ( ; glyph < end; glyph++)
19805 glyph[-n] = *glyph;
19808 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19809 and ROW->maxpos. */
19810 static void
19811 find_row_edges (struct it *it, struct glyph_row *row,
19812 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19813 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19815 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19816 lines' rows is implemented for bidi-reordered rows. */
19818 /* ROW->minpos is the value of min_pos, the minimal buffer position
19819 we have in ROW, or ROW->start.pos if that is smaller. */
19820 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19821 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19822 else
19823 /* We didn't find buffer positions smaller than ROW->start, or
19824 didn't find _any_ valid buffer positions in any of the glyphs,
19825 so we must trust the iterator's computed positions. */
19826 row->minpos = row->start.pos;
19827 if (max_pos <= 0)
19829 max_pos = CHARPOS (it->current.pos);
19830 max_bpos = BYTEPOS (it->current.pos);
19833 /* Here are the various use-cases for ending the row, and the
19834 corresponding values for ROW->maxpos:
19836 Line ends in a newline from buffer eol_pos + 1
19837 Line is continued from buffer max_pos + 1
19838 Line is truncated on right it->current.pos
19839 Line ends in a newline from string max_pos + 1(*)
19840 (*) + 1 only when line ends in a forward scan
19841 Line is continued from string max_pos
19842 Line is continued from display vector max_pos
19843 Line is entirely from a string min_pos == max_pos
19844 Line is entirely from a display vector min_pos == max_pos
19845 Line that ends at ZV ZV
19847 If you discover other use-cases, please add them here as
19848 appropriate. */
19849 if (row->ends_at_zv_p)
19850 row->maxpos = it->current.pos;
19851 else if (row->used[TEXT_AREA])
19853 bool seen_this_string = false;
19854 struct glyph_row *r1 = row - 1;
19856 /* Did we see the same display string on the previous row? */
19857 if (STRINGP (it->object)
19858 /* this is not the first row */
19859 && row > it->w->desired_matrix->rows
19860 /* previous row is not the header line */
19861 && !r1->mode_line_p
19862 /* previous row also ends in a newline from a string */
19863 && r1->ends_in_newline_from_string_p)
19865 struct glyph *start, *end;
19867 /* Search for the last glyph of the previous row that came
19868 from buffer or string. Depending on whether the row is
19869 L2R or R2L, we need to process it front to back or the
19870 other way round. */
19871 if (!r1->reversed_p)
19873 start = r1->glyphs[TEXT_AREA];
19874 end = start + r1->used[TEXT_AREA];
19875 /* Glyphs inserted by redisplay have nil as their object. */
19876 while (end > start
19877 && NILP ((end - 1)->object)
19878 && (end - 1)->charpos <= 0)
19879 --end;
19880 if (end > start)
19882 if (EQ ((end - 1)->object, it->object))
19883 seen_this_string = true;
19885 else
19886 /* If all the glyphs of the previous row were inserted
19887 by redisplay, it means the previous row was
19888 produced from a single newline, which is only
19889 possible if that newline came from the same string
19890 as the one which produced this ROW. */
19891 seen_this_string = true;
19893 else
19895 end = r1->glyphs[TEXT_AREA] - 1;
19896 start = end + r1->used[TEXT_AREA];
19897 while (end < start
19898 && NILP ((end + 1)->object)
19899 && (end + 1)->charpos <= 0)
19900 ++end;
19901 if (end < start)
19903 if (EQ ((end + 1)->object, it->object))
19904 seen_this_string = true;
19906 else
19907 seen_this_string = true;
19910 /* Take note of each display string that covers a newline only
19911 once, the first time we see it. This is for when a display
19912 string includes more than one newline in it. */
19913 if (row->ends_in_newline_from_string_p && !seen_this_string)
19915 /* If we were scanning the buffer forward when we displayed
19916 the string, we want to account for at least one buffer
19917 position that belongs to this row (position covered by
19918 the display string), so that cursor positioning will
19919 consider this row as a candidate when point is at the end
19920 of the visual line represented by this row. This is not
19921 required when scanning back, because max_pos will already
19922 have a much larger value. */
19923 if (CHARPOS (row->end.pos) > max_pos)
19924 INC_BOTH (max_pos, max_bpos);
19925 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19927 else if (CHARPOS (it->eol_pos) > 0)
19928 SET_TEXT_POS (row->maxpos,
19929 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19930 else if (row->continued_p)
19932 /* If max_pos is different from IT's current position, it
19933 means IT->method does not belong to the display element
19934 at max_pos. However, it also means that the display
19935 element at max_pos was displayed in its entirety on this
19936 line, which is equivalent to saying that the next line
19937 starts at the next buffer position. */
19938 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19939 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19940 else
19942 INC_BOTH (max_pos, max_bpos);
19943 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19946 else if (row->truncated_on_right_p)
19947 /* display_line already called reseat_at_next_visible_line_start,
19948 which puts the iterator at the beginning of the next line, in
19949 the logical order. */
19950 row->maxpos = it->current.pos;
19951 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19952 /* A line that is entirely from a string/image/stretch... */
19953 row->maxpos = row->minpos;
19954 else
19955 emacs_abort ();
19957 else
19958 row->maxpos = it->current.pos;
19961 /* Construct the glyph row IT->glyph_row in the desired matrix of
19962 IT->w from text at the current position of IT. See dispextern.h
19963 for an overview of struct it. Value is true if
19964 IT->glyph_row displays text, as opposed to a line displaying ZV
19965 only. */
19967 static bool
19968 display_line (struct it *it)
19970 struct glyph_row *row = it->glyph_row;
19971 Lisp_Object overlay_arrow_string;
19972 struct it wrap_it;
19973 void *wrap_data = NULL;
19974 bool may_wrap = false;
19975 int wrap_x IF_LINT (= 0);
19976 int wrap_row_used = -1;
19977 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19978 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19979 int wrap_row_extra_line_spacing IF_LINT (= 0);
19980 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19981 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19982 int cvpos;
19983 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19984 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19985 bool pending_handle_line_prefix = false;
19987 /* We always start displaying at hpos zero even if hscrolled. */
19988 eassert (it->hpos == 0 && it->current_x == 0);
19990 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19991 >= it->w->desired_matrix->nrows)
19993 it->w->nrows_scale_factor++;
19994 it->f->fonts_changed = true;
19995 return false;
19998 /* Clear the result glyph row and enable it. */
19999 prepare_desired_row (it->w, row, false);
20001 row->y = it->current_y;
20002 row->start = it->start;
20003 row->continuation_lines_width = it->continuation_lines_width;
20004 row->displays_text_p = true;
20005 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20006 it->starts_in_middle_of_char_p = false;
20008 /* Arrange the overlays nicely for our purposes. Usually, we call
20009 display_line on only one line at a time, in which case this
20010 can't really hurt too much, or we call it on lines which appear
20011 one after another in the buffer, in which case all calls to
20012 recenter_overlay_lists but the first will be pretty cheap. */
20013 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20015 /* Move over display elements that are not visible because we are
20016 hscrolled. This may stop at an x-position < IT->first_visible_x
20017 if the first glyph is partially visible or if we hit a line end. */
20018 if (it->current_x < it->first_visible_x)
20020 enum move_it_result move_result;
20022 this_line_min_pos = row->start.pos;
20023 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20024 MOVE_TO_POS | MOVE_TO_X);
20025 /* If we are under a large hscroll, move_it_in_display_line_to
20026 could hit the end of the line without reaching
20027 it->first_visible_x. Pretend that we did reach it. This is
20028 especially important on a TTY, where we will call
20029 extend_face_to_end_of_line, which needs to know how many
20030 blank glyphs to produce. */
20031 if (it->current_x < it->first_visible_x
20032 && (move_result == MOVE_NEWLINE_OR_CR
20033 || move_result == MOVE_POS_MATCH_OR_ZV))
20034 it->current_x = it->first_visible_x;
20036 /* Record the smallest positions seen while we moved over
20037 display elements that are not visible. This is needed by
20038 redisplay_internal for optimizing the case where the cursor
20039 stays inside the same line. The rest of this function only
20040 considers positions that are actually displayed, so
20041 RECORD_MAX_MIN_POS will not otherwise record positions that
20042 are hscrolled to the left of the left edge of the window. */
20043 min_pos = CHARPOS (this_line_min_pos);
20044 min_bpos = BYTEPOS (this_line_min_pos);
20046 else if (it->area == TEXT_AREA)
20048 /* We only do this when not calling move_it_in_display_line_to
20049 above, because that function calls itself handle_line_prefix. */
20050 handle_line_prefix (it);
20052 else
20054 /* Line-prefix and wrap-prefix are always displayed in the text
20055 area. But if this is the first call to display_line after
20056 init_iterator, the iterator might have been set up to write
20057 into a marginal area, e.g. if the line begins with some
20058 display property that writes to the margins. So we need to
20059 wait with the call to handle_line_prefix until whatever
20060 writes to the margin has done its job. */
20061 pending_handle_line_prefix = true;
20064 /* Get the initial row height. This is either the height of the
20065 text hscrolled, if there is any, or zero. */
20066 row->ascent = it->max_ascent;
20067 row->height = it->max_ascent + it->max_descent;
20068 row->phys_ascent = it->max_phys_ascent;
20069 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20070 row->extra_line_spacing = it->max_extra_line_spacing;
20072 /* Utility macro to record max and min buffer positions seen until now. */
20073 #define RECORD_MAX_MIN_POS(IT) \
20074 do \
20076 bool composition_p \
20077 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20078 ptrdiff_t current_pos = \
20079 composition_p ? (IT)->cmp_it.charpos \
20080 : IT_CHARPOS (*(IT)); \
20081 ptrdiff_t current_bpos = \
20082 composition_p ? CHAR_TO_BYTE (current_pos) \
20083 : IT_BYTEPOS (*(IT)); \
20084 if (current_pos < min_pos) \
20086 min_pos = current_pos; \
20087 min_bpos = current_bpos; \
20089 if (IT_CHARPOS (*it) > max_pos) \
20091 max_pos = IT_CHARPOS (*it); \
20092 max_bpos = IT_BYTEPOS (*it); \
20095 while (false)
20097 /* Loop generating characters. The loop is left with IT on the next
20098 character to display. */
20099 while (true)
20101 int n_glyphs_before, hpos_before, x_before;
20102 int x, nglyphs;
20103 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20105 /* Retrieve the next thing to display. Value is false if end of
20106 buffer reached. */
20107 if (!get_next_display_element (it))
20109 /* Maybe add a space at the end of this line that is used to
20110 display the cursor there under X. Set the charpos of the
20111 first glyph of blank lines not corresponding to any text
20112 to -1. */
20113 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20114 row->exact_window_width_line_p = true;
20115 else if ((append_space_for_newline (it, true)
20116 && row->used[TEXT_AREA] == 1)
20117 || row->used[TEXT_AREA] == 0)
20119 row->glyphs[TEXT_AREA]->charpos = -1;
20120 row->displays_text_p = false;
20122 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20123 && (!MINI_WINDOW_P (it->w)
20124 || (minibuf_level && EQ (it->window, minibuf_window))))
20125 row->indicate_empty_line_p = true;
20128 it->continuation_lines_width = 0;
20129 row->ends_at_zv_p = true;
20130 /* A row that displays right-to-left text must always have
20131 its last face extended all the way to the end of line,
20132 even if this row ends in ZV, because we still write to
20133 the screen left to right. We also need to extend the
20134 last face if the default face is remapped to some
20135 different face, otherwise the functions that clear
20136 portions of the screen will clear with the default face's
20137 background color. */
20138 if (row->reversed_p
20139 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20140 extend_face_to_end_of_line (it);
20141 break;
20144 /* Now, get the metrics of what we want to display. This also
20145 generates glyphs in `row' (which is IT->glyph_row). */
20146 n_glyphs_before = row->used[TEXT_AREA];
20147 x = it->current_x;
20149 /* Remember the line height so far in case the next element doesn't
20150 fit on the line. */
20151 if (it->line_wrap != TRUNCATE)
20153 ascent = it->max_ascent;
20154 descent = it->max_descent;
20155 phys_ascent = it->max_phys_ascent;
20156 phys_descent = it->max_phys_descent;
20158 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20160 if (IT_DISPLAYING_WHITESPACE (it))
20161 may_wrap = true;
20162 else if (may_wrap)
20164 SAVE_IT (wrap_it, *it, wrap_data);
20165 wrap_x = x;
20166 wrap_row_used = row->used[TEXT_AREA];
20167 wrap_row_ascent = row->ascent;
20168 wrap_row_height = row->height;
20169 wrap_row_phys_ascent = row->phys_ascent;
20170 wrap_row_phys_height = row->phys_height;
20171 wrap_row_extra_line_spacing = row->extra_line_spacing;
20172 wrap_row_min_pos = min_pos;
20173 wrap_row_min_bpos = min_bpos;
20174 wrap_row_max_pos = max_pos;
20175 wrap_row_max_bpos = max_bpos;
20176 may_wrap = false;
20181 PRODUCE_GLYPHS (it);
20183 /* If this display element was in marginal areas, continue with
20184 the next one. */
20185 if (it->area != TEXT_AREA)
20187 row->ascent = max (row->ascent, it->max_ascent);
20188 row->height = max (row->height, it->max_ascent + it->max_descent);
20189 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20190 row->phys_height = max (row->phys_height,
20191 it->max_phys_ascent + it->max_phys_descent);
20192 row->extra_line_spacing = max (row->extra_line_spacing,
20193 it->max_extra_line_spacing);
20194 set_iterator_to_next (it, true);
20195 /* If we didn't handle the line/wrap prefix above, and the
20196 call to set_iterator_to_next just switched to TEXT_AREA,
20197 process the prefix now. */
20198 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20200 pending_handle_line_prefix = false;
20201 handle_line_prefix (it);
20203 continue;
20206 /* Does the display element fit on the line? If we truncate
20207 lines, we should draw past the right edge of the window. If
20208 we don't truncate, we want to stop so that we can display the
20209 continuation glyph before the right margin. If lines are
20210 continued, there are two possible strategies for characters
20211 resulting in more than 1 glyph (e.g. tabs): Display as many
20212 glyphs as possible in this line and leave the rest for the
20213 continuation line, or display the whole element in the next
20214 line. Original redisplay did the former, so we do it also. */
20215 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20216 hpos_before = it->hpos;
20217 x_before = x;
20219 if (/* Not a newline. */
20220 nglyphs > 0
20221 /* Glyphs produced fit entirely in the line. */
20222 && it->current_x < it->last_visible_x)
20224 it->hpos += nglyphs;
20225 row->ascent = max (row->ascent, it->max_ascent);
20226 row->height = max (row->height, it->max_ascent + it->max_descent);
20227 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20228 row->phys_height = max (row->phys_height,
20229 it->max_phys_ascent + it->max_phys_descent);
20230 row->extra_line_spacing = max (row->extra_line_spacing,
20231 it->max_extra_line_spacing);
20232 if (it->current_x - it->pixel_width < it->first_visible_x
20233 /* In R2L rows, we arrange in extend_face_to_end_of_line
20234 to add a right offset to the line, by a suitable
20235 change to the stretch glyph that is the leftmost
20236 glyph of the line. */
20237 && !row->reversed_p)
20238 row->x = x - it->first_visible_x;
20239 /* Record the maximum and minimum buffer positions seen so
20240 far in glyphs that will be displayed by this row. */
20241 if (it->bidi_p)
20242 RECORD_MAX_MIN_POS (it);
20244 else
20246 int i, new_x;
20247 struct glyph *glyph;
20249 for (i = 0; i < nglyphs; ++i, x = new_x)
20251 /* Identify the glyphs added by the last call to
20252 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20253 the previous glyphs. */
20254 if (!row->reversed_p)
20255 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20256 else
20257 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20258 new_x = x + glyph->pixel_width;
20260 if (/* Lines are continued. */
20261 it->line_wrap != TRUNCATE
20262 && (/* Glyph doesn't fit on the line. */
20263 new_x > it->last_visible_x
20264 /* Or it fits exactly on a window system frame. */
20265 || (new_x == it->last_visible_x
20266 && FRAME_WINDOW_P (it->f)
20267 && (row->reversed_p
20268 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20269 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20271 /* End of a continued line. */
20273 if (it->hpos == 0
20274 || (new_x == it->last_visible_x
20275 && FRAME_WINDOW_P (it->f)
20276 && (row->reversed_p
20277 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20278 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20280 /* Current glyph is the only one on the line or
20281 fits exactly on the line. We must continue
20282 the line because we can't draw the cursor
20283 after the glyph. */
20284 row->continued_p = true;
20285 it->current_x = new_x;
20286 it->continuation_lines_width += new_x;
20287 ++it->hpos;
20288 if (i == nglyphs - 1)
20290 /* If line-wrap is on, check if a previous
20291 wrap point was found. */
20292 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20293 && wrap_row_used > 0
20294 /* Even if there is a previous wrap
20295 point, continue the line here as
20296 usual, if (i) the previous character
20297 was a space or tab AND (ii) the
20298 current character is not. */
20299 && (!may_wrap
20300 || IT_DISPLAYING_WHITESPACE (it)))
20301 goto back_to_wrap;
20303 /* Record the maximum and minimum buffer
20304 positions seen so far in glyphs that will be
20305 displayed by this row. */
20306 if (it->bidi_p)
20307 RECORD_MAX_MIN_POS (it);
20308 set_iterator_to_next (it, true);
20309 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20311 if (!get_next_display_element (it))
20313 row->exact_window_width_line_p = true;
20314 it->continuation_lines_width = 0;
20315 row->continued_p = false;
20316 row->ends_at_zv_p = true;
20318 else if (ITERATOR_AT_END_OF_LINE_P (it))
20320 row->continued_p = false;
20321 row->exact_window_width_line_p = true;
20323 /* If line-wrap is on, check if a
20324 previous wrap point was found. */
20325 else if (wrap_row_used > 0
20326 /* Even if there is a previous wrap
20327 point, continue the line here as
20328 usual, if (i) the previous character
20329 was a space or tab AND (ii) the
20330 current character is not. */
20331 && (!may_wrap
20332 || IT_DISPLAYING_WHITESPACE (it)))
20333 goto back_to_wrap;
20337 else if (it->bidi_p)
20338 RECORD_MAX_MIN_POS (it);
20339 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20340 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20341 extend_face_to_end_of_line (it);
20343 else if (CHAR_GLYPH_PADDING_P (*glyph)
20344 && !FRAME_WINDOW_P (it->f))
20346 /* A padding glyph that doesn't fit on this line.
20347 This means the whole character doesn't fit
20348 on the line. */
20349 if (row->reversed_p)
20350 unproduce_glyphs (it, row->used[TEXT_AREA]
20351 - n_glyphs_before);
20352 row->used[TEXT_AREA] = n_glyphs_before;
20354 /* Fill the rest of the row with continuation
20355 glyphs like in 20.x. */
20356 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20357 < row->glyphs[1 + TEXT_AREA])
20358 produce_special_glyphs (it, IT_CONTINUATION);
20360 row->continued_p = true;
20361 it->current_x = x_before;
20362 it->continuation_lines_width += x_before;
20364 /* Restore the height to what it was before the
20365 element not fitting on the line. */
20366 it->max_ascent = ascent;
20367 it->max_descent = descent;
20368 it->max_phys_ascent = phys_ascent;
20369 it->max_phys_descent = phys_descent;
20370 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20371 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20372 extend_face_to_end_of_line (it);
20374 else if (wrap_row_used > 0)
20376 back_to_wrap:
20377 if (row->reversed_p)
20378 unproduce_glyphs (it,
20379 row->used[TEXT_AREA] - wrap_row_used);
20380 RESTORE_IT (it, &wrap_it, wrap_data);
20381 it->continuation_lines_width += wrap_x;
20382 row->used[TEXT_AREA] = wrap_row_used;
20383 row->ascent = wrap_row_ascent;
20384 row->height = wrap_row_height;
20385 row->phys_ascent = wrap_row_phys_ascent;
20386 row->phys_height = wrap_row_phys_height;
20387 row->extra_line_spacing = wrap_row_extra_line_spacing;
20388 min_pos = wrap_row_min_pos;
20389 min_bpos = wrap_row_min_bpos;
20390 max_pos = wrap_row_max_pos;
20391 max_bpos = wrap_row_max_bpos;
20392 row->continued_p = true;
20393 row->ends_at_zv_p = false;
20394 row->exact_window_width_line_p = false;
20395 it->continuation_lines_width += x;
20397 /* Make sure that a non-default face is extended
20398 up to the right margin of the window. */
20399 extend_face_to_end_of_line (it);
20401 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20403 /* A TAB that extends past the right edge of the
20404 window. This produces a single glyph on
20405 window system frames. We leave the glyph in
20406 this row and let it fill the row, but don't
20407 consume the TAB. */
20408 if ((row->reversed_p
20409 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20410 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20411 produce_special_glyphs (it, IT_CONTINUATION);
20412 it->continuation_lines_width += it->last_visible_x;
20413 row->ends_in_middle_of_char_p = true;
20414 row->continued_p = true;
20415 glyph->pixel_width = it->last_visible_x - x;
20416 it->starts_in_middle_of_char_p = true;
20417 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20418 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20419 extend_face_to_end_of_line (it);
20421 else
20423 /* Something other than a TAB that draws past
20424 the right edge of the window. Restore
20425 positions to values before the element. */
20426 if (row->reversed_p)
20427 unproduce_glyphs (it, row->used[TEXT_AREA]
20428 - (n_glyphs_before + i));
20429 row->used[TEXT_AREA] = n_glyphs_before + i;
20431 /* Display continuation glyphs. */
20432 it->current_x = x_before;
20433 it->continuation_lines_width += x;
20434 if (!FRAME_WINDOW_P (it->f)
20435 || (row->reversed_p
20436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20438 produce_special_glyphs (it, IT_CONTINUATION);
20439 row->continued_p = true;
20441 extend_face_to_end_of_line (it);
20443 if (nglyphs > 1 && i > 0)
20445 row->ends_in_middle_of_char_p = true;
20446 it->starts_in_middle_of_char_p = true;
20449 /* Restore the height to what it was before the
20450 element not fitting on the line. */
20451 it->max_ascent = ascent;
20452 it->max_descent = descent;
20453 it->max_phys_ascent = phys_ascent;
20454 it->max_phys_descent = phys_descent;
20457 break;
20459 else if (new_x > it->first_visible_x)
20461 /* Increment number of glyphs actually displayed. */
20462 ++it->hpos;
20464 /* Record the maximum and minimum buffer positions
20465 seen so far in glyphs that will be displayed by
20466 this row. */
20467 if (it->bidi_p)
20468 RECORD_MAX_MIN_POS (it);
20470 if (x < it->first_visible_x && !row->reversed_p)
20471 /* Glyph is partially visible, i.e. row starts at
20472 negative X position. Don't do that in R2L
20473 rows, where we arrange to add a right offset to
20474 the line in extend_face_to_end_of_line, by a
20475 suitable change to the stretch glyph that is
20476 the leftmost glyph of the line. */
20477 row->x = x - it->first_visible_x;
20478 /* When the last glyph of an R2L row only fits
20479 partially on the line, we need to set row->x to a
20480 negative offset, so that the leftmost glyph is
20481 the one that is partially visible. But if we are
20482 going to produce the truncation glyph, this will
20483 be taken care of in produce_special_glyphs. */
20484 if (row->reversed_p
20485 && new_x > it->last_visible_x
20486 && !(it->line_wrap == TRUNCATE
20487 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20489 eassert (FRAME_WINDOW_P (it->f));
20490 row->x = it->last_visible_x - new_x;
20493 else
20495 /* Glyph is completely off the left margin of the
20496 window. This should not happen because of the
20497 move_it_in_display_line at the start of this
20498 function, unless the text display area of the
20499 window is empty. */
20500 eassert (it->first_visible_x <= it->last_visible_x);
20503 /* Even if this display element produced no glyphs at all,
20504 we want to record its position. */
20505 if (it->bidi_p && nglyphs == 0)
20506 RECORD_MAX_MIN_POS (it);
20508 row->ascent = max (row->ascent, it->max_ascent);
20509 row->height = max (row->height, it->max_ascent + it->max_descent);
20510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20511 row->phys_height = max (row->phys_height,
20512 it->max_phys_ascent + it->max_phys_descent);
20513 row->extra_line_spacing = max (row->extra_line_spacing,
20514 it->max_extra_line_spacing);
20516 /* End of this display line if row is continued. */
20517 if (row->continued_p || row->ends_at_zv_p)
20518 break;
20521 at_end_of_line:
20522 /* Is this a line end? If yes, we're also done, after making
20523 sure that a non-default face is extended up to the right
20524 margin of the window. */
20525 if (ITERATOR_AT_END_OF_LINE_P (it))
20527 int used_before = row->used[TEXT_AREA];
20529 row->ends_in_newline_from_string_p = STRINGP (it->object);
20531 /* Add a space at the end of the line that is used to
20532 display the cursor there. */
20533 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20534 append_space_for_newline (it, false);
20536 /* Extend the face to the end of the line. */
20537 extend_face_to_end_of_line (it);
20539 /* Make sure we have the position. */
20540 if (used_before == 0)
20541 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20543 /* Record the position of the newline, for use in
20544 find_row_edges. */
20545 it->eol_pos = it->current.pos;
20547 /* Consume the line end. This skips over invisible lines. */
20548 set_iterator_to_next (it, true);
20549 it->continuation_lines_width = 0;
20550 break;
20553 /* Proceed with next display element. Note that this skips
20554 over lines invisible because of selective display. */
20555 set_iterator_to_next (it, true);
20557 /* If we truncate lines, we are done when the last displayed
20558 glyphs reach past the right margin of the window. */
20559 if (it->line_wrap == TRUNCATE
20560 && ((FRAME_WINDOW_P (it->f)
20561 /* Images are preprocessed in produce_image_glyph such
20562 that they are cropped at the right edge of the
20563 window, so an image glyph will always end exactly at
20564 last_visible_x, even if there's no right fringe. */
20565 && ((row->reversed_p
20566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20568 || it->what == IT_IMAGE))
20569 ? (it->current_x >= it->last_visible_x)
20570 : (it->current_x > it->last_visible_x)))
20572 /* Maybe add truncation glyphs. */
20573 if (!FRAME_WINDOW_P (it->f)
20574 || (row->reversed_p
20575 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20576 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20578 int i, n;
20580 if (!row->reversed_p)
20582 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20583 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20584 break;
20586 else
20588 for (i = 0; i < row->used[TEXT_AREA]; i++)
20589 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20590 break;
20591 /* Remove any padding glyphs at the front of ROW, to
20592 make room for the truncation glyphs we will be
20593 adding below. The loop below always inserts at
20594 least one truncation glyph, so also remove the
20595 last glyph added to ROW. */
20596 unproduce_glyphs (it, i + 1);
20597 /* Adjust i for the loop below. */
20598 i = row->used[TEXT_AREA] - (i + 1);
20601 /* produce_special_glyphs overwrites the last glyph, so
20602 we don't want that if we want to keep that last
20603 glyph, which means it's an image. */
20604 if (it->current_x > it->last_visible_x)
20606 it->current_x = x_before;
20607 if (!FRAME_WINDOW_P (it->f))
20609 for (n = row->used[TEXT_AREA]; i < n; ++i)
20611 row->used[TEXT_AREA] = i;
20612 produce_special_glyphs (it, IT_TRUNCATION);
20615 else
20617 row->used[TEXT_AREA] = i;
20618 produce_special_glyphs (it, IT_TRUNCATION);
20620 it->hpos = hpos_before;
20623 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20625 /* Don't truncate if we can overflow newline into fringe. */
20626 if (!get_next_display_element (it))
20628 it->continuation_lines_width = 0;
20629 row->ends_at_zv_p = true;
20630 row->exact_window_width_line_p = true;
20631 break;
20633 if (ITERATOR_AT_END_OF_LINE_P (it))
20635 row->exact_window_width_line_p = true;
20636 goto at_end_of_line;
20638 it->current_x = x_before;
20639 it->hpos = hpos_before;
20642 row->truncated_on_right_p = true;
20643 it->continuation_lines_width = 0;
20644 reseat_at_next_visible_line_start (it, false);
20645 /* We insist below that IT's position be at ZV because in
20646 bidi-reordered lines the character at visible line start
20647 might not be the character that follows the newline in
20648 the logical order. */
20649 if (IT_BYTEPOS (*it) > BEG_BYTE)
20650 row->ends_at_zv_p =
20651 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20652 else
20653 row->ends_at_zv_p = false;
20654 break;
20658 if (wrap_data)
20659 bidi_unshelve_cache (wrap_data, true);
20661 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20662 at the left window margin. */
20663 if (it->first_visible_x
20664 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20666 if (!FRAME_WINDOW_P (it->f)
20667 || (((row->reversed_p
20668 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20669 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20670 /* Don't let insert_left_trunc_glyphs overwrite the
20671 first glyph of the row if it is an image. */
20672 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20673 insert_left_trunc_glyphs (it);
20674 row->truncated_on_left_p = true;
20677 /* Remember the position at which this line ends.
20679 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20680 cannot be before the call to find_row_edges below, since that is
20681 where these positions are determined. */
20682 row->end = it->current;
20683 if (!it->bidi_p)
20685 row->minpos = row->start.pos;
20686 row->maxpos = row->end.pos;
20688 else
20690 /* ROW->minpos and ROW->maxpos must be the smallest and
20691 `1 + the largest' buffer positions in ROW. But if ROW was
20692 bidi-reordered, these two positions can be anywhere in the
20693 row, so we must determine them now. */
20694 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20697 /* If the start of this line is the overlay arrow-position, then
20698 mark this glyph row as the one containing the overlay arrow.
20699 This is clearly a mess with variable size fonts. It would be
20700 better to let it be displayed like cursors under X. */
20701 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20702 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20703 !NILP (overlay_arrow_string)))
20705 /* Overlay arrow in window redisplay is a fringe bitmap. */
20706 if (STRINGP (overlay_arrow_string))
20708 struct glyph_row *arrow_row
20709 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20710 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20711 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20712 struct glyph *p = row->glyphs[TEXT_AREA];
20713 struct glyph *p2, *end;
20715 /* Copy the arrow glyphs. */
20716 while (glyph < arrow_end)
20717 *p++ = *glyph++;
20719 /* Throw away padding glyphs. */
20720 p2 = p;
20721 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20722 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20723 ++p2;
20724 if (p2 > p)
20726 while (p2 < end)
20727 *p++ = *p2++;
20728 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20731 else
20733 eassert (INTEGERP (overlay_arrow_string));
20734 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20736 overlay_arrow_seen = true;
20739 /* Highlight trailing whitespace. */
20740 if (!NILP (Vshow_trailing_whitespace))
20741 highlight_trailing_whitespace (it->f, it->glyph_row);
20743 /* Compute pixel dimensions of this line. */
20744 compute_line_metrics (it);
20746 /* Implementation note: No changes in the glyphs of ROW or in their
20747 faces can be done past this point, because compute_line_metrics
20748 computes ROW's hash value and stores it within the glyph_row
20749 structure. */
20751 /* Record whether this row ends inside an ellipsis. */
20752 row->ends_in_ellipsis_p
20753 = (it->method == GET_FROM_DISPLAY_VECTOR
20754 && it->ellipsis_p);
20756 /* Save fringe bitmaps in this row. */
20757 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20758 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20759 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20760 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20762 it->left_user_fringe_bitmap = 0;
20763 it->left_user_fringe_face_id = 0;
20764 it->right_user_fringe_bitmap = 0;
20765 it->right_user_fringe_face_id = 0;
20767 /* Maybe set the cursor. */
20768 cvpos = it->w->cursor.vpos;
20769 if ((cvpos < 0
20770 /* In bidi-reordered rows, keep checking for proper cursor
20771 position even if one has been found already, because buffer
20772 positions in such rows change non-linearly with ROW->VPOS,
20773 when a line is continued. One exception: when we are at ZV,
20774 display cursor on the first suitable glyph row, since all
20775 the empty rows after that also have their position set to ZV. */
20776 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20777 lines' rows is implemented for bidi-reordered rows. */
20778 || (it->bidi_p
20779 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20780 && PT >= MATRIX_ROW_START_CHARPOS (row)
20781 && PT <= MATRIX_ROW_END_CHARPOS (row)
20782 && cursor_row_p (row))
20783 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20785 /* Prepare for the next line. This line starts horizontally at (X
20786 HPOS) = (0 0). Vertical positions are incremented. As a
20787 convenience for the caller, IT->glyph_row is set to the next
20788 row to be used. */
20789 it->current_x = it->hpos = 0;
20790 it->current_y += row->height;
20791 SET_TEXT_POS (it->eol_pos, 0, 0);
20792 ++it->vpos;
20793 ++it->glyph_row;
20794 /* The next row should by default use the same value of the
20795 reversed_p flag as this one. set_iterator_to_next decides when
20796 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20797 the flag accordingly. */
20798 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20799 it->glyph_row->reversed_p = row->reversed_p;
20800 it->start = row->end;
20801 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20803 #undef RECORD_MAX_MIN_POS
20806 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20807 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20808 doc: /* Return paragraph direction at point in BUFFER.
20809 Value is either `left-to-right' or `right-to-left'.
20810 If BUFFER is omitted or nil, it defaults to the current buffer.
20812 Paragraph direction determines how the text in the paragraph is displayed.
20813 In left-to-right paragraphs, text begins at the left margin of the window
20814 and the reading direction is generally left to right. In right-to-left
20815 paragraphs, text begins at the right margin and is read from right to left.
20817 See also `bidi-paragraph-direction'. */)
20818 (Lisp_Object buffer)
20820 struct buffer *buf = current_buffer;
20821 struct buffer *old = buf;
20823 if (! NILP (buffer))
20825 CHECK_BUFFER (buffer);
20826 buf = XBUFFER (buffer);
20829 if (NILP (BVAR (buf, bidi_display_reordering))
20830 || NILP (BVAR (buf, enable_multibyte_characters))
20831 /* When we are loading loadup.el, the character property tables
20832 needed for bidi iteration are not yet available. */
20833 || !NILP (Vpurify_flag))
20834 return Qleft_to_right;
20835 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20836 return BVAR (buf, bidi_paragraph_direction);
20837 else
20839 /* Determine the direction from buffer text. We could try to
20840 use current_matrix if it is up to date, but this seems fast
20841 enough as it is. */
20842 struct bidi_it itb;
20843 ptrdiff_t pos = BUF_PT (buf);
20844 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20845 int c;
20846 void *itb_data = bidi_shelve_cache ();
20848 set_buffer_temp (buf);
20849 /* bidi_paragraph_init finds the base direction of the paragraph
20850 by searching forward from paragraph start. We need the base
20851 direction of the current or _previous_ paragraph, so we need
20852 to make sure we are within that paragraph. To that end, find
20853 the previous non-empty line. */
20854 if (pos >= ZV && pos > BEGV)
20855 DEC_BOTH (pos, bytepos);
20856 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20857 if (fast_looking_at (trailing_white_space,
20858 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20860 while ((c = FETCH_BYTE (bytepos)) == '\n'
20861 || c == ' ' || c == '\t' || c == '\f')
20863 if (bytepos <= BEGV_BYTE)
20864 break;
20865 bytepos--;
20866 pos--;
20868 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20869 bytepos--;
20871 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20872 itb.paragraph_dir = NEUTRAL_DIR;
20873 itb.string.s = NULL;
20874 itb.string.lstring = Qnil;
20875 itb.string.bufpos = 0;
20876 itb.string.from_disp_str = false;
20877 itb.string.unibyte = false;
20878 /* We have no window to use here for ignoring window-specific
20879 overlays. Using NULL for window pointer will cause
20880 compute_display_string_pos to use the current buffer. */
20881 itb.w = NULL;
20882 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
20883 bidi_unshelve_cache (itb_data, false);
20884 set_buffer_temp (old);
20885 switch (itb.paragraph_dir)
20887 case L2R:
20888 return Qleft_to_right;
20889 break;
20890 case R2L:
20891 return Qright_to_left;
20892 break;
20893 default:
20894 emacs_abort ();
20899 DEFUN ("bidi-find-overridden-directionality",
20900 Fbidi_find_overridden_directionality,
20901 Sbidi_find_overridden_directionality, 2, 3, 0,
20902 doc: /* Return position between FROM and TO where directionality was overridden.
20904 This function returns the first character position in the specified
20905 region of OBJECT where there is a character whose `bidi-class' property
20906 is `L', but which was forced to display as `R' by a directional
20907 override, and likewise with characters whose `bidi-class' is `R'
20908 or `AL' that were forced to display as `L'.
20910 If no such character is found, the function returns nil.
20912 OBJECT is a Lisp string or buffer to search for overridden
20913 directionality, and defaults to the current buffer if nil or omitted.
20914 OBJECT can also be a window, in which case the function will search
20915 the buffer displayed in that window. Passing the window instead of
20916 a buffer is preferable when the buffer is displayed in some window,
20917 because this function will then be able to correctly account for
20918 window-specific overlays, which can affect the results.
20920 Strong directional characters `L', `R', and `AL' can have their
20921 intrinsic directionality overridden by directional override
20922 control characters RLO \(u+202e) and LRO \(u+202d). See the
20923 function `get-char-code-property' for a way to inquire about
20924 the `bidi-class' property of a character. */)
20925 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20927 struct buffer *buf = current_buffer;
20928 struct buffer *old = buf;
20929 struct window *w = NULL;
20930 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20931 struct bidi_it itb;
20932 ptrdiff_t from_pos, to_pos, from_bpos;
20933 void *itb_data;
20935 if (!NILP (object))
20937 if (BUFFERP (object))
20938 buf = XBUFFER (object);
20939 else if (WINDOWP (object))
20941 w = decode_live_window (object);
20942 buf = XBUFFER (w->contents);
20943 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20945 else
20946 CHECK_STRING (object);
20949 if (STRINGP (object))
20951 /* Characters in unibyte strings are always treated by bidi.c as
20952 strong LTR. */
20953 if (!STRING_MULTIBYTE (object)
20954 /* When we are loading loadup.el, the character property
20955 tables needed for bidi iteration are not yet
20956 available. */
20957 || !NILP (Vpurify_flag))
20958 return Qnil;
20960 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
20961 if (from_pos >= SCHARS (object))
20962 return Qnil;
20964 /* Set up the bidi iterator. */
20965 itb_data = bidi_shelve_cache ();
20966 itb.paragraph_dir = NEUTRAL_DIR;
20967 itb.string.lstring = object;
20968 itb.string.s = NULL;
20969 itb.string.schars = SCHARS (object);
20970 itb.string.bufpos = 0;
20971 itb.string.from_disp_str = false;
20972 itb.string.unibyte = false;
20973 itb.w = w;
20974 bidi_init_it (0, 0, frame_window_p, &itb);
20976 else
20978 /* Nothing this fancy can happen in unibyte buffers, or in a
20979 buffer that disabled reordering, or if FROM is at EOB. */
20980 if (NILP (BVAR (buf, bidi_display_reordering))
20981 || NILP (BVAR (buf, enable_multibyte_characters))
20982 /* When we are loading loadup.el, the character property
20983 tables needed for bidi iteration are not yet
20984 available. */
20985 || !NILP (Vpurify_flag))
20986 return Qnil;
20988 set_buffer_temp (buf);
20989 validate_region (&from, &to);
20990 from_pos = XINT (from);
20991 to_pos = XINT (to);
20992 if (from_pos >= ZV)
20993 return Qnil;
20995 /* Set up the bidi iterator. */
20996 itb_data = bidi_shelve_cache ();
20997 from_bpos = CHAR_TO_BYTE (from_pos);
20998 if (from_pos == BEGV)
21000 itb.charpos = BEGV;
21001 itb.bytepos = BEGV_BYTE;
21003 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21005 itb.charpos = from_pos;
21006 itb.bytepos = from_bpos;
21008 else
21009 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21010 -1, &itb.bytepos);
21011 itb.paragraph_dir = NEUTRAL_DIR;
21012 itb.string.s = NULL;
21013 itb.string.lstring = Qnil;
21014 itb.string.bufpos = 0;
21015 itb.string.from_disp_str = false;
21016 itb.string.unibyte = false;
21017 itb.w = w;
21018 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21021 ptrdiff_t found;
21022 do {
21023 /* For the purposes of this function, the actual base direction of
21024 the paragraph doesn't matter, so just set it to L2R. */
21025 bidi_paragraph_init (L2R, &itb, false);
21026 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21028 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21030 bidi_unshelve_cache (itb_data, false);
21031 set_buffer_temp (old);
21033 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21036 DEFUN ("move-point-visually", Fmove_point_visually,
21037 Smove_point_visually, 1, 1, 0,
21038 doc: /* Move point in the visual order in the specified DIRECTION.
21039 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21040 left.
21042 Value is the new character position of point. */)
21043 (Lisp_Object direction)
21045 struct window *w = XWINDOW (selected_window);
21046 struct buffer *b = XBUFFER (w->contents);
21047 struct glyph_row *row;
21048 int dir;
21049 Lisp_Object paragraph_dir;
21051 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21052 (!(ROW)->continued_p \
21053 && NILP ((GLYPH)->object) \
21054 && (GLYPH)->type == CHAR_GLYPH \
21055 && (GLYPH)->u.ch == ' ' \
21056 && (GLYPH)->charpos >= 0 \
21057 && !(GLYPH)->avoid_cursor_p)
21059 CHECK_NUMBER (direction);
21060 dir = XINT (direction);
21061 if (dir > 0)
21062 dir = 1;
21063 else
21064 dir = -1;
21066 /* If current matrix is up-to-date, we can use the information
21067 recorded in the glyphs, at least as long as the goal is on the
21068 screen. */
21069 if (w->window_end_valid
21070 && !windows_or_buffers_changed
21071 && b
21072 && !b->clip_changed
21073 && !b->prevent_redisplay_optimizations_p
21074 && !window_outdated (w)
21075 /* We rely below on the cursor coordinates to be up to date, but
21076 we cannot trust them if some command moved point since the
21077 last complete redisplay. */
21078 && w->last_point == BUF_PT (b)
21079 && w->cursor.vpos >= 0
21080 && w->cursor.vpos < w->current_matrix->nrows
21081 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21083 struct glyph *g = row->glyphs[TEXT_AREA];
21084 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21085 struct glyph *gpt = g + w->cursor.hpos;
21087 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21089 if (BUFFERP (g->object) && g->charpos != PT)
21091 SET_PT (g->charpos);
21092 w->cursor.vpos = -1;
21093 return make_number (PT);
21095 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21097 ptrdiff_t new_pos;
21099 if (BUFFERP (gpt->object))
21101 new_pos = PT;
21102 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21103 new_pos += (row->reversed_p ? -dir : dir);
21104 else
21105 new_pos -= (row->reversed_p ? -dir : dir);
21107 else if (BUFFERP (g->object))
21108 new_pos = g->charpos;
21109 else
21110 break;
21111 SET_PT (new_pos);
21112 w->cursor.vpos = -1;
21113 return make_number (PT);
21115 else if (ROW_GLYPH_NEWLINE_P (row, g))
21117 /* Glyphs inserted at the end of a non-empty line for
21118 positioning the cursor have zero charpos, so we must
21119 deduce the value of point by other means. */
21120 if (g->charpos > 0)
21121 SET_PT (g->charpos);
21122 else if (row->ends_at_zv_p && PT != ZV)
21123 SET_PT (ZV);
21124 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21125 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21126 else
21127 break;
21128 w->cursor.vpos = -1;
21129 return make_number (PT);
21132 if (g == e || NILP (g->object))
21134 if (row->truncated_on_left_p || row->truncated_on_right_p)
21135 goto simulate_display;
21136 if (!row->reversed_p)
21137 row += dir;
21138 else
21139 row -= dir;
21140 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21141 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21142 goto simulate_display;
21144 if (dir > 0)
21146 if (row->reversed_p && !row->continued_p)
21148 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21149 w->cursor.vpos = -1;
21150 return make_number (PT);
21152 g = row->glyphs[TEXT_AREA];
21153 e = g + row->used[TEXT_AREA];
21154 for ( ; g < e; g++)
21156 if (BUFFERP (g->object)
21157 /* Empty lines have only one glyph, which stands
21158 for the newline, and whose charpos is the
21159 buffer position of the newline. */
21160 || ROW_GLYPH_NEWLINE_P (row, g)
21161 /* When the buffer ends in a newline, the line at
21162 EOB also has one glyph, but its charpos is -1. */
21163 || (row->ends_at_zv_p
21164 && !row->reversed_p
21165 && NILP (g->object)
21166 && g->type == CHAR_GLYPH
21167 && g->u.ch == ' '))
21169 if (g->charpos > 0)
21170 SET_PT (g->charpos);
21171 else if (!row->reversed_p
21172 && row->ends_at_zv_p
21173 && PT != ZV)
21174 SET_PT (ZV);
21175 else
21176 continue;
21177 w->cursor.vpos = -1;
21178 return make_number (PT);
21182 else
21184 if (!row->reversed_p && !row->continued_p)
21186 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21187 w->cursor.vpos = -1;
21188 return make_number (PT);
21190 e = row->glyphs[TEXT_AREA];
21191 g = e + row->used[TEXT_AREA] - 1;
21192 for ( ; g >= e; g--)
21194 if (BUFFERP (g->object)
21195 || (ROW_GLYPH_NEWLINE_P (row, g)
21196 && g->charpos > 0)
21197 /* Empty R2L lines on GUI frames have the buffer
21198 position of the newline stored in the stretch
21199 glyph. */
21200 || g->type == STRETCH_GLYPH
21201 || (row->ends_at_zv_p
21202 && row->reversed_p
21203 && NILP (g->object)
21204 && g->type == CHAR_GLYPH
21205 && g->u.ch == ' '))
21207 if (g->charpos > 0)
21208 SET_PT (g->charpos);
21209 else if (row->reversed_p
21210 && row->ends_at_zv_p
21211 && PT != ZV)
21212 SET_PT (ZV);
21213 else
21214 continue;
21215 w->cursor.vpos = -1;
21216 return make_number (PT);
21223 simulate_display:
21225 /* If we wind up here, we failed to move by using the glyphs, so we
21226 need to simulate display instead. */
21228 if (b)
21229 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21230 else
21231 paragraph_dir = Qleft_to_right;
21232 if (EQ (paragraph_dir, Qright_to_left))
21233 dir = -dir;
21234 if (PT <= BEGV && dir < 0)
21235 xsignal0 (Qbeginning_of_buffer);
21236 else if (PT >= ZV && dir > 0)
21237 xsignal0 (Qend_of_buffer);
21238 else
21240 struct text_pos pt;
21241 struct it it;
21242 int pt_x, target_x, pixel_width, pt_vpos;
21243 bool at_eol_p;
21244 bool overshoot_expected = false;
21245 bool target_is_eol_p = false;
21247 /* Setup the arena. */
21248 SET_TEXT_POS (pt, PT, PT_BYTE);
21249 start_display (&it, w, pt);
21251 if (it.cmp_it.id < 0
21252 && it.method == GET_FROM_STRING
21253 && it.area == TEXT_AREA
21254 && it.string_from_display_prop_p
21255 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21256 overshoot_expected = true;
21258 /* Find the X coordinate of point. We start from the beginning
21259 of this or previous line to make sure we are before point in
21260 the logical order (since the move_it_* functions can only
21261 move forward). */
21262 reseat:
21263 reseat_at_previous_visible_line_start (&it);
21264 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21265 if (IT_CHARPOS (it) != PT)
21267 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21268 -1, -1, -1, MOVE_TO_POS);
21269 /* If we missed point because the character there is
21270 displayed out of a display vector that has more than one
21271 glyph, retry expecting overshoot. */
21272 if (it.method == GET_FROM_DISPLAY_VECTOR
21273 && it.current.dpvec_index > 0
21274 && !overshoot_expected)
21276 overshoot_expected = true;
21277 goto reseat;
21279 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21280 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21282 pt_x = it.current_x;
21283 pt_vpos = it.vpos;
21284 if (dir > 0 || overshoot_expected)
21286 struct glyph_row *row = it.glyph_row;
21288 /* When point is at beginning of line, we don't have
21289 information about the glyph there loaded into struct
21290 it. Calling get_next_display_element fixes that. */
21291 if (pt_x == 0)
21292 get_next_display_element (&it);
21293 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21294 it.glyph_row = NULL;
21295 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21296 it.glyph_row = row;
21297 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21298 it, lest it will become out of sync with it's buffer
21299 position. */
21300 it.current_x = pt_x;
21302 else
21303 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21304 pixel_width = it.pixel_width;
21305 if (overshoot_expected && at_eol_p)
21306 pixel_width = 0;
21307 else if (pixel_width <= 0)
21308 pixel_width = 1;
21310 /* If there's a display string (or something similar) at point,
21311 we are actually at the glyph to the left of point, so we need
21312 to correct the X coordinate. */
21313 if (overshoot_expected)
21315 if (it.bidi_p)
21316 pt_x += pixel_width * it.bidi_it.scan_dir;
21317 else
21318 pt_x += pixel_width;
21321 /* Compute target X coordinate, either to the left or to the
21322 right of point. On TTY frames, all characters have the same
21323 pixel width of 1, so we can use that. On GUI frames we don't
21324 have an easy way of getting at the pixel width of the
21325 character to the left of point, so we use a different method
21326 of getting to that place. */
21327 if (dir > 0)
21328 target_x = pt_x + pixel_width;
21329 else
21330 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21332 /* Target X coordinate could be one line above or below the line
21333 of point, in which case we need to adjust the target X
21334 coordinate. Also, if moving to the left, we need to begin at
21335 the left edge of the point's screen line. */
21336 if (dir < 0)
21338 if (pt_x > 0)
21340 start_display (&it, w, pt);
21341 reseat_at_previous_visible_line_start (&it);
21342 it.current_x = it.current_y = it.hpos = 0;
21343 if (pt_vpos != 0)
21344 move_it_by_lines (&it, pt_vpos);
21346 else
21348 move_it_by_lines (&it, -1);
21349 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21350 target_is_eol_p = true;
21351 /* Under word-wrap, we don't know the x coordinate of
21352 the last character displayed on the previous line,
21353 which immediately precedes the wrap point. To find
21354 out its x coordinate, we try moving to the right
21355 margin of the window, which will stop at the wrap
21356 point, and then reset target_x to point at the
21357 character that precedes the wrap point. This is not
21358 needed on GUI frames, because (see below) there we
21359 move from the left margin one grapheme cluster at a
21360 time, and stop when we hit the wrap point. */
21361 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21363 void *it_data = NULL;
21364 struct it it2;
21366 SAVE_IT (it2, it, it_data);
21367 move_it_in_display_line_to (&it, ZV, target_x,
21368 MOVE_TO_POS | MOVE_TO_X);
21369 /* If we arrived at target_x, that _is_ the last
21370 character on the previous line. */
21371 if (it.current_x != target_x)
21372 target_x = it.current_x - 1;
21373 RESTORE_IT (&it, &it2, it_data);
21377 else
21379 if (at_eol_p
21380 || (target_x >= it.last_visible_x
21381 && it.line_wrap != TRUNCATE))
21383 if (pt_x > 0)
21384 move_it_by_lines (&it, 0);
21385 move_it_by_lines (&it, 1);
21386 target_x = 0;
21390 /* Move to the target X coordinate. */
21391 #ifdef HAVE_WINDOW_SYSTEM
21392 /* On GUI frames, as we don't know the X coordinate of the
21393 character to the left of point, moving point to the left
21394 requires walking, one grapheme cluster at a time, until we
21395 find ourself at a place immediately to the left of the
21396 character at point. */
21397 if (FRAME_WINDOW_P (it.f) && dir < 0)
21399 struct text_pos new_pos;
21400 enum move_it_result rc = MOVE_X_REACHED;
21402 if (it.current_x == 0)
21403 get_next_display_element (&it);
21404 if (it.what == IT_COMPOSITION)
21406 new_pos.charpos = it.cmp_it.charpos;
21407 new_pos.bytepos = -1;
21409 else
21410 new_pos = it.current.pos;
21412 while (it.current_x + it.pixel_width <= target_x
21413 && (rc == MOVE_X_REACHED
21414 /* Under word-wrap, move_it_in_display_line_to
21415 stops at correct coordinates, but sometimes
21416 returns MOVE_POS_MATCH_OR_ZV. */
21417 || (it.line_wrap == WORD_WRAP
21418 && rc == MOVE_POS_MATCH_OR_ZV)))
21420 int new_x = it.current_x + it.pixel_width;
21422 /* For composed characters, we want the position of the
21423 first character in the grapheme cluster (usually, the
21424 composition's base character), whereas it.current
21425 might give us the position of the _last_ one, e.g. if
21426 the composition is rendered in reverse due to bidi
21427 reordering. */
21428 if (it.what == IT_COMPOSITION)
21430 new_pos.charpos = it.cmp_it.charpos;
21431 new_pos.bytepos = -1;
21433 else
21434 new_pos = it.current.pos;
21435 if (new_x == it.current_x)
21436 new_x++;
21437 rc = move_it_in_display_line_to (&it, ZV, new_x,
21438 MOVE_TO_POS | MOVE_TO_X);
21439 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21440 break;
21442 /* The previous position we saw in the loop is the one we
21443 want. */
21444 if (new_pos.bytepos == -1)
21445 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21446 it.current.pos = new_pos;
21448 else
21449 #endif
21450 if (it.current_x != target_x)
21451 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21453 /* When lines are truncated, the above loop will stop at the
21454 window edge. But we want to get to the end of line, even if
21455 it is beyond the window edge; automatic hscroll will then
21456 scroll the window to show point as appropriate. */
21457 if (target_is_eol_p && it.line_wrap == TRUNCATE
21458 && get_next_display_element (&it))
21460 struct text_pos new_pos = it.current.pos;
21462 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21464 set_iterator_to_next (&it, false);
21465 if (it.method == GET_FROM_BUFFER)
21466 new_pos = it.current.pos;
21467 if (!get_next_display_element (&it))
21468 break;
21471 it.current.pos = new_pos;
21474 /* If we ended up in a display string that covers point, move to
21475 buffer position to the right in the visual order. */
21476 if (dir > 0)
21478 while (IT_CHARPOS (it) == PT)
21480 set_iterator_to_next (&it, false);
21481 if (!get_next_display_element (&it))
21482 break;
21486 /* Move point to that position. */
21487 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21490 return make_number (PT);
21492 #undef ROW_GLYPH_NEWLINE_P
21495 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21496 Sbidi_resolved_levels, 0, 1, 0,
21497 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21499 The resolved levels are produced by the Emacs bidi reordering engine
21500 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21501 read the Unicode Standard Annex 9 (UAX#9) for background information
21502 about these levels.
21504 VPOS is the zero-based number of the current window's screen line
21505 for which to produce the resolved levels. If VPOS is nil or omitted,
21506 it defaults to the screen line of point. If the window displays a
21507 header line, VPOS of zero will report on the header line, and first
21508 line of text in the window will have VPOS of 1.
21510 Value is an array of resolved levels, indexed by glyph number.
21511 Glyphs are numbered from zero starting from the beginning of the
21512 screen line, i.e. the left edge of the window for left-to-right lines
21513 and from the right edge for right-to-left lines. The resolved levels
21514 are produced only for the window's text area; text in display margins
21515 is not included.
21517 If the selected window's display is not up-to-date, or if the specified
21518 screen line does not display text, this function returns nil. It is
21519 highly recommended to bind this function to some simple key, like F8,
21520 in order to avoid these problems.
21522 This function exists mainly for testing the correctness of the
21523 Emacs UBA implementation, in particular with the test suite. */)
21524 (Lisp_Object vpos)
21526 struct window *w = XWINDOW (selected_window);
21527 struct buffer *b = XBUFFER (w->contents);
21528 int nrow;
21529 struct glyph_row *row;
21531 if (NILP (vpos))
21533 int d1, d2, d3, d4, d5;
21535 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21537 else
21539 CHECK_NUMBER_COERCE_MARKER (vpos);
21540 nrow = XINT (vpos);
21543 /* We require up-to-date glyph matrix for this window. */
21544 if (w->window_end_valid
21545 && !windows_or_buffers_changed
21546 && b
21547 && !b->clip_changed
21548 && !b->prevent_redisplay_optimizations_p
21549 && !window_outdated (w)
21550 && nrow >= 0
21551 && nrow < w->current_matrix->nrows
21552 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21553 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21555 struct glyph *g, *e, *g1;
21556 int nglyphs, i;
21557 Lisp_Object levels;
21559 if (!row->reversed_p) /* Left-to-right glyph row. */
21561 g = g1 = row->glyphs[TEXT_AREA];
21562 e = g + row->used[TEXT_AREA];
21564 /* Skip over glyphs at the start of the row that was
21565 generated by redisplay for its own needs. */
21566 while (g < e
21567 && NILP (g->object)
21568 && g->charpos < 0)
21569 g++;
21570 g1 = g;
21572 /* Count the "interesting" glyphs in this row. */
21573 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21574 nglyphs++;
21576 /* Create and fill the array. */
21577 levels = make_uninit_vector (nglyphs);
21578 for (i = 0; g1 < g; i++, g1++)
21579 ASET (levels, i, make_number (g1->resolved_level));
21581 else /* Right-to-left glyph row. */
21583 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21584 e = row->glyphs[TEXT_AREA] - 1;
21585 while (g > e
21586 && NILP (g->object)
21587 && g->charpos < 0)
21588 g--;
21589 g1 = g;
21590 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21591 nglyphs++;
21592 levels = make_uninit_vector (nglyphs);
21593 for (i = 0; g1 > g; i++, g1--)
21594 ASET (levels, i, make_number (g1->resolved_level));
21596 return levels;
21598 else
21599 return Qnil;
21604 /***********************************************************************
21605 Menu Bar
21606 ***********************************************************************/
21608 /* Redisplay the menu bar in the frame for window W.
21610 The menu bar of X frames that don't have X toolkit support is
21611 displayed in a special window W->frame->menu_bar_window.
21613 The menu bar of terminal frames is treated specially as far as
21614 glyph matrices are concerned. Menu bar lines are not part of
21615 windows, so the update is done directly on the frame matrix rows
21616 for the menu bar. */
21618 static void
21619 display_menu_bar (struct window *w)
21621 struct frame *f = XFRAME (WINDOW_FRAME (w));
21622 struct it it;
21623 Lisp_Object items;
21624 int i;
21626 /* Don't do all this for graphical frames. */
21627 #ifdef HAVE_NTGUI
21628 if (FRAME_W32_P (f))
21629 return;
21630 #endif
21631 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21632 if (FRAME_X_P (f))
21633 return;
21634 #endif
21636 #ifdef HAVE_NS
21637 if (FRAME_NS_P (f))
21638 return;
21639 #endif /* HAVE_NS */
21641 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21642 eassert (!FRAME_WINDOW_P (f));
21643 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21644 it.first_visible_x = 0;
21645 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21646 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21647 if (FRAME_WINDOW_P (f))
21649 /* Menu bar lines are displayed in the desired matrix of the
21650 dummy window menu_bar_window. */
21651 struct window *menu_w;
21652 menu_w = XWINDOW (f->menu_bar_window);
21653 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21654 MENU_FACE_ID);
21655 it.first_visible_x = 0;
21656 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21658 else
21659 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21661 /* This is a TTY frame, i.e. character hpos/vpos are used as
21662 pixel x/y. */
21663 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21664 MENU_FACE_ID);
21665 it.first_visible_x = 0;
21666 it.last_visible_x = FRAME_COLS (f);
21669 /* FIXME: This should be controlled by a user option. See the
21670 comments in redisplay_tool_bar and display_mode_line about
21671 this. */
21672 it.paragraph_embedding = L2R;
21674 /* Clear all rows of the menu bar. */
21675 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21677 struct glyph_row *row = it.glyph_row + i;
21678 clear_glyph_row (row);
21679 row->enabled_p = true;
21680 row->full_width_p = true;
21681 row->reversed_p = false;
21684 /* Display all items of the menu bar. */
21685 items = FRAME_MENU_BAR_ITEMS (it.f);
21686 for (i = 0; i < ASIZE (items); i += 4)
21688 Lisp_Object string;
21690 /* Stop at nil string. */
21691 string = AREF (items, i + 1);
21692 if (NILP (string))
21693 break;
21695 /* Remember where item was displayed. */
21696 ASET (items, i + 3, make_number (it.hpos));
21698 /* Display the item, pad with one space. */
21699 if (it.current_x < it.last_visible_x)
21700 display_string (NULL, string, Qnil, 0, 0, &it,
21701 SCHARS (string) + 1, 0, 0, -1);
21704 /* Fill out the line with spaces. */
21705 if (it.current_x < it.last_visible_x)
21706 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21708 /* Compute the total height of the lines. */
21709 compute_line_metrics (&it);
21712 /* Deep copy of a glyph row, including the glyphs. */
21713 static void
21714 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21716 struct glyph *pointers[1 + LAST_AREA];
21717 int to_used = to->used[TEXT_AREA];
21719 /* Save glyph pointers of TO. */
21720 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21722 /* Do a structure assignment. */
21723 *to = *from;
21725 /* Restore original glyph pointers of TO. */
21726 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21728 /* Copy the glyphs. */
21729 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21730 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21732 /* If we filled only part of the TO row, fill the rest with
21733 space_glyph (which will display as empty space). */
21734 if (to_used > from->used[TEXT_AREA])
21735 fill_up_frame_row_with_spaces (to, to_used);
21738 /* Display one menu item on a TTY, by overwriting the glyphs in the
21739 frame F's desired glyph matrix with glyphs produced from the menu
21740 item text. Called from term.c to display TTY drop-down menus one
21741 item at a time.
21743 ITEM_TEXT is the menu item text as a C string.
21745 FACE_ID is the face ID to be used for this menu item. FACE_ID
21746 could specify one of 3 faces: a face for an enabled item, a face
21747 for a disabled item, or a face for a selected item.
21749 X and Y are coordinates of the first glyph in the frame's desired
21750 matrix to be overwritten by the menu item. Since this is a TTY, Y
21751 is the zero-based number of the glyph row and X is the zero-based
21752 glyph number in the row, starting from left, where to start
21753 displaying the item.
21755 SUBMENU means this menu item drops down a submenu, which
21756 should be indicated by displaying a proper visual cue after the
21757 item text. */
21759 void
21760 display_tty_menu_item (const char *item_text, int width, int face_id,
21761 int x, int y, bool submenu)
21763 struct it it;
21764 struct frame *f = SELECTED_FRAME ();
21765 struct window *w = XWINDOW (f->selected_window);
21766 struct glyph_row *row;
21767 size_t item_len = strlen (item_text);
21769 eassert (FRAME_TERMCAP_P (f));
21771 /* Don't write beyond the matrix's last row. This can happen for
21772 TTY screens that are not high enough to show the entire menu.
21773 (This is actually a bit of defensive programming, as
21774 tty_menu_display already limits the number of menu items to one
21775 less than the number of screen lines.) */
21776 if (y >= f->desired_matrix->nrows)
21777 return;
21779 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21780 it.first_visible_x = 0;
21781 it.last_visible_x = FRAME_COLS (f) - 1;
21782 row = it.glyph_row;
21783 /* Start with the row contents from the current matrix. */
21784 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21785 bool saved_width = row->full_width_p;
21786 row->full_width_p = true;
21787 bool saved_reversed = row->reversed_p;
21788 row->reversed_p = false;
21789 row->enabled_p = true;
21791 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21792 desired face. */
21793 eassert (x < f->desired_matrix->matrix_w);
21794 it.current_x = it.hpos = x;
21795 it.current_y = it.vpos = y;
21796 int saved_used = row->used[TEXT_AREA];
21797 bool saved_truncated = row->truncated_on_right_p;
21798 row->used[TEXT_AREA] = x;
21799 it.face_id = face_id;
21800 it.line_wrap = TRUNCATE;
21802 /* FIXME: This should be controlled by a user option. See the
21803 comments in redisplay_tool_bar and display_mode_line about this.
21804 Also, if paragraph_embedding could ever be R2L, changes will be
21805 needed to avoid shifting to the right the row characters in
21806 term.c:append_glyph. */
21807 it.paragraph_embedding = L2R;
21809 /* Pad with a space on the left. */
21810 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21811 width--;
21812 /* Display the menu item, pad with spaces to WIDTH. */
21813 if (submenu)
21815 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21816 item_len, 0, FRAME_COLS (f) - 1, -1);
21817 width -= item_len;
21818 /* Indicate with " >" that there's a submenu. */
21819 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21820 FRAME_COLS (f) - 1, -1);
21822 else
21823 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21824 width, 0, FRAME_COLS (f) - 1, -1);
21826 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21827 row->truncated_on_right_p = saved_truncated;
21828 row->hash = row_hash (row);
21829 row->full_width_p = saved_width;
21830 row->reversed_p = saved_reversed;
21833 /***********************************************************************
21834 Mode Line
21835 ***********************************************************************/
21837 /* Redisplay mode lines in the window tree whose root is WINDOW.
21838 If FORCE, redisplay mode lines unconditionally.
21839 Otherwise, redisplay only mode lines that are garbaged. Value is
21840 the number of windows whose mode lines were redisplayed. */
21842 static int
21843 redisplay_mode_lines (Lisp_Object window, bool force)
21845 int nwindows = 0;
21847 while (!NILP (window))
21849 struct window *w = XWINDOW (window);
21851 if (WINDOWP (w->contents))
21852 nwindows += redisplay_mode_lines (w->contents, force);
21853 else if (force
21854 || FRAME_GARBAGED_P (XFRAME (w->frame))
21855 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21857 struct text_pos lpoint;
21858 struct buffer *old = current_buffer;
21860 /* Set the window's buffer for the mode line display. */
21861 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21862 set_buffer_internal_1 (XBUFFER (w->contents));
21864 /* Point refers normally to the selected window. For any
21865 other window, set up appropriate value. */
21866 if (!EQ (window, selected_window))
21868 struct text_pos pt;
21870 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21871 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21874 /* Display mode lines. */
21875 clear_glyph_matrix (w->desired_matrix);
21876 if (display_mode_lines (w))
21877 ++nwindows;
21879 /* Restore old settings. */
21880 set_buffer_internal_1 (old);
21881 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21884 window = w->next;
21887 return nwindows;
21891 /* Display the mode and/or header line of window W. Value is the
21892 sum number of mode lines and header lines displayed. */
21894 static int
21895 display_mode_lines (struct window *w)
21897 Lisp_Object old_selected_window = selected_window;
21898 Lisp_Object old_selected_frame = selected_frame;
21899 Lisp_Object new_frame = w->frame;
21900 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21901 int n = 0;
21903 selected_frame = new_frame;
21904 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21905 or window's point, then we'd need select_window_1 here as well. */
21906 XSETWINDOW (selected_window, w);
21907 XFRAME (new_frame)->selected_window = selected_window;
21909 /* These will be set while the mode line specs are processed. */
21910 line_number_displayed = false;
21911 w->column_number_displayed = -1;
21913 if (WINDOW_WANTS_MODELINE_P (w))
21915 struct window *sel_w = XWINDOW (old_selected_window);
21917 /* Select mode line face based on the real selected window. */
21918 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21919 BVAR (current_buffer, mode_line_format));
21920 ++n;
21923 if (WINDOW_WANTS_HEADER_LINE_P (w))
21925 display_mode_line (w, HEADER_LINE_FACE_ID,
21926 BVAR (current_buffer, header_line_format));
21927 ++n;
21930 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21931 selected_frame = old_selected_frame;
21932 selected_window = old_selected_window;
21933 if (n > 0)
21934 w->must_be_updated_p = true;
21935 return n;
21939 /* Display mode or header line of window W. FACE_ID specifies which
21940 line to display; it is either MODE_LINE_FACE_ID or
21941 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21942 display. Value is the pixel height of the mode/header line
21943 displayed. */
21945 static int
21946 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21948 struct it it;
21949 struct face *face;
21950 ptrdiff_t count = SPECPDL_INDEX ();
21952 init_iterator (&it, w, -1, -1, NULL, face_id);
21953 /* Don't extend on a previously drawn mode-line.
21954 This may happen if called from pos_visible_p. */
21955 it.glyph_row->enabled_p = false;
21956 prepare_desired_row (w, it.glyph_row, true);
21958 it.glyph_row->mode_line_p = true;
21960 /* FIXME: This should be controlled by a user option. But
21961 supporting such an option is not trivial, since the mode line is
21962 made up of many separate strings. */
21963 it.paragraph_embedding = L2R;
21965 record_unwind_protect (unwind_format_mode_line,
21966 format_mode_line_unwind_data (NULL, NULL,
21967 Qnil, false));
21969 mode_line_target = MODE_LINE_DISPLAY;
21971 /* Temporarily make frame's keyboard the current kboard so that
21972 kboard-local variables in the mode_line_format will get the right
21973 values. */
21974 push_kboard (FRAME_KBOARD (it.f));
21975 record_unwind_save_match_data ();
21976 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
21977 pop_kboard ();
21979 unbind_to (count, Qnil);
21981 /* Fill up with spaces. */
21982 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21984 compute_line_metrics (&it);
21985 it.glyph_row->full_width_p = true;
21986 it.glyph_row->continued_p = false;
21987 it.glyph_row->truncated_on_left_p = false;
21988 it.glyph_row->truncated_on_right_p = false;
21990 /* Make a 3D mode-line have a shadow at its right end. */
21991 face = FACE_FROM_ID (it.f, face_id);
21992 extend_face_to_end_of_line (&it);
21993 if (face->box != FACE_NO_BOX)
21995 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21996 + it.glyph_row->used[TEXT_AREA] - 1);
21997 last->right_box_line_p = true;
22000 return it.glyph_row->height;
22003 /* Move element ELT in LIST to the front of LIST.
22004 Return the updated list. */
22006 static Lisp_Object
22007 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22009 register Lisp_Object tail, prev;
22010 register Lisp_Object tem;
22012 tail = list;
22013 prev = Qnil;
22014 while (CONSP (tail))
22016 tem = XCAR (tail);
22018 if (EQ (elt, tem))
22020 /* Splice out the link TAIL. */
22021 if (NILP (prev))
22022 list = XCDR (tail);
22023 else
22024 Fsetcdr (prev, XCDR (tail));
22026 /* Now make it the first. */
22027 Fsetcdr (tail, list);
22028 return tail;
22030 else
22031 prev = tail;
22032 tail = XCDR (tail);
22033 QUIT;
22036 /* Not found--return unchanged LIST. */
22037 return list;
22040 /* Contribute ELT to the mode line for window IT->w. How it
22041 translates into text depends on its data type.
22043 IT describes the display environment in which we display, as usual.
22045 DEPTH is the depth in recursion. It is used to prevent
22046 infinite recursion here.
22048 FIELD_WIDTH is the number of characters the display of ELT should
22049 occupy in the mode line, and PRECISION is the maximum number of
22050 characters to display from ELT's representation. See
22051 display_string for details.
22053 Returns the hpos of the end of the text generated by ELT.
22055 PROPS is a property list to add to any string we encounter.
22057 If RISKY, remove (disregard) any properties in any string
22058 we encounter, and ignore :eval and :propertize.
22060 The global variable `mode_line_target' determines whether the
22061 output is passed to `store_mode_line_noprop',
22062 `store_mode_line_string', or `display_string'. */
22064 static int
22065 display_mode_element (struct it *it, int depth, int field_width, int precision,
22066 Lisp_Object elt, Lisp_Object props, bool risky)
22068 int n = 0, field, prec;
22069 bool literal = false;
22071 tail_recurse:
22072 if (depth > 100)
22073 elt = build_string ("*too-deep*");
22075 depth++;
22077 switch (XTYPE (elt))
22079 case Lisp_String:
22081 /* A string: output it and check for %-constructs within it. */
22082 unsigned char c;
22083 ptrdiff_t offset = 0;
22085 if (SCHARS (elt) > 0
22086 && (!NILP (props) || risky))
22088 Lisp_Object oprops, aelt;
22089 oprops = Ftext_properties_at (make_number (0), elt);
22091 /* If the starting string's properties are not what
22092 we want, translate the string. Also, if the string
22093 is risky, do that anyway. */
22095 if (NILP (Fequal (props, oprops)) || risky)
22097 /* If the starting string has properties,
22098 merge the specified ones onto the existing ones. */
22099 if (! NILP (oprops) && !risky)
22101 Lisp_Object tem;
22103 oprops = Fcopy_sequence (oprops);
22104 tem = props;
22105 while (CONSP (tem))
22107 oprops = Fplist_put (oprops, XCAR (tem),
22108 XCAR (XCDR (tem)));
22109 tem = XCDR (XCDR (tem));
22111 props = oprops;
22114 aelt = Fassoc (elt, mode_line_proptrans_alist);
22115 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22117 /* AELT is what we want. Move it to the front
22118 without consing. */
22119 elt = XCAR (aelt);
22120 mode_line_proptrans_alist
22121 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22123 else
22125 Lisp_Object tem;
22127 /* If AELT has the wrong props, it is useless.
22128 so get rid of it. */
22129 if (! NILP (aelt))
22130 mode_line_proptrans_alist
22131 = Fdelq (aelt, mode_line_proptrans_alist);
22133 elt = Fcopy_sequence (elt);
22134 Fset_text_properties (make_number (0), Flength (elt),
22135 props, elt);
22136 /* Add this item to mode_line_proptrans_alist. */
22137 mode_line_proptrans_alist
22138 = Fcons (Fcons (elt, props),
22139 mode_line_proptrans_alist);
22140 /* Truncate mode_line_proptrans_alist
22141 to at most 50 elements. */
22142 tem = Fnthcdr (make_number (50),
22143 mode_line_proptrans_alist);
22144 if (! NILP (tem))
22145 XSETCDR (tem, Qnil);
22150 offset = 0;
22152 if (literal)
22154 prec = precision - n;
22155 switch (mode_line_target)
22157 case MODE_LINE_NOPROP:
22158 case MODE_LINE_TITLE:
22159 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22160 break;
22161 case MODE_LINE_STRING:
22162 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22163 break;
22164 case MODE_LINE_DISPLAY:
22165 n += display_string (NULL, elt, Qnil, 0, 0, it,
22166 0, prec, 0, STRING_MULTIBYTE (elt));
22167 break;
22170 break;
22173 /* Handle the non-literal case. */
22175 while ((precision <= 0 || n < precision)
22176 && SREF (elt, offset) != 0
22177 && (mode_line_target != MODE_LINE_DISPLAY
22178 || it->current_x < it->last_visible_x))
22180 ptrdiff_t last_offset = offset;
22182 /* Advance to end of string or next format specifier. */
22183 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22186 if (offset - 1 != last_offset)
22188 ptrdiff_t nchars, nbytes;
22190 /* Output to end of string or up to '%'. Field width
22191 is length of string. Don't output more than
22192 PRECISION allows us. */
22193 offset--;
22195 prec = c_string_width (SDATA (elt) + last_offset,
22196 offset - last_offset, precision - n,
22197 &nchars, &nbytes);
22199 switch (mode_line_target)
22201 case MODE_LINE_NOPROP:
22202 case MODE_LINE_TITLE:
22203 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22204 break;
22205 case MODE_LINE_STRING:
22207 ptrdiff_t bytepos = last_offset;
22208 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22209 ptrdiff_t endpos = (precision <= 0
22210 ? string_byte_to_char (elt, offset)
22211 : charpos + nchars);
22212 Lisp_Object mode_string
22213 = Fsubstring (elt, make_number (charpos),
22214 make_number (endpos));
22215 n += store_mode_line_string (NULL, mode_string, false,
22216 0, 0, Qnil);
22218 break;
22219 case MODE_LINE_DISPLAY:
22221 ptrdiff_t bytepos = last_offset;
22222 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22224 if (precision <= 0)
22225 nchars = string_byte_to_char (elt, offset) - charpos;
22226 n += display_string (NULL, elt, Qnil, 0, charpos,
22227 it, 0, nchars, 0,
22228 STRING_MULTIBYTE (elt));
22230 break;
22233 else /* c == '%' */
22235 ptrdiff_t percent_position = offset;
22237 /* Get the specified minimum width. Zero means
22238 don't pad. */
22239 field = 0;
22240 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22241 field = field * 10 + c - '0';
22243 /* Don't pad beyond the total padding allowed. */
22244 if (field_width - n > 0 && field > field_width - n)
22245 field = field_width - n;
22247 /* Note that either PRECISION <= 0 or N < PRECISION. */
22248 prec = precision - n;
22250 if (c == 'M')
22251 n += display_mode_element (it, depth, field, prec,
22252 Vglobal_mode_string, props,
22253 risky);
22254 else if (c != 0)
22256 bool multibyte;
22257 ptrdiff_t bytepos, charpos;
22258 const char *spec;
22259 Lisp_Object string;
22261 bytepos = percent_position;
22262 charpos = (STRING_MULTIBYTE (elt)
22263 ? string_byte_to_char (elt, bytepos)
22264 : bytepos);
22265 spec = decode_mode_spec (it->w, c, field, &string);
22266 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22268 switch (mode_line_target)
22270 case MODE_LINE_NOPROP:
22271 case MODE_LINE_TITLE:
22272 n += store_mode_line_noprop (spec, field, prec);
22273 break;
22274 case MODE_LINE_STRING:
22276 Lisp_Object tem = build_string (spec);
22277 props = Ftext_properties_at (make_number (charpos), elt);
22278 /* Should only keep face property in props */
22279 n += store_mode_line_string (NULL, tem, false,
22280 field, prec, props);
22282 break;
22283 case MODE_LINE_DISPLAY:
22285 int nglyphs_before, nwritten;
22287 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22288 nwritten = display_string (spec, string, elt,
22289 charpos, 0, it,
22290 field, prec, 0,
22291 multibyte);
22293 /* Assign to the glyphs written above the
22294 string where the `%x' came from, position
22295 of the `%'. */
22296 if (nwritten > 0)
22298 struct glyph *glyph
22299 = (it->glyph_row->glyphs[TEXT_AREA]
22300 + nglyphs_before);
22301 int i;
22303 for (i = 0; i < nwritten; ++i)
22305 glyph[i].object = elt;
22306 glyph[i].charpos = charpos;
22309 n += nwritten;
22312 break;
22315 else /* c == 0 */
22316 break;
22320 break;
22322 case Lisp_Symbol:
22323 /* A symbol: process the value of the symbol recursively
22324 as if it appeared here directly. Avoid error if symbol void.
22325 Special case: if value of symbol is a string, output the string
22326 literally. */
22328 register Lisp_Object tem;
22330 /* If the variable is not marked as risky to set
22331 then its contents are risky to use. */
22332 if (NILP (Fget (elt, Qrisky_local_variable)))
22333 risky = true;
22335 tem = Fboundp (elt);
22336 if (!NILP (tem))
22338 tem = Fsymbol_value (elt);
22339 /* If value is a string, output that string literally:
22340 don't check for % within it. */
22341 if (STRINGP (tem))
22342 literal = true;
22344 if (!EQ (tem, elt))
22346 /* Give up right away for nil or t. */
22347 elt = tem;
22348 goto tail_recurse;
22352 break;
22354 case Lisp_Cons:
22356 register Lisp_Object car, tem;
22358 /* A cons cell: five distinct cases.
22359 If first element is :eval or :propertize, do something special.
22360 If first element is a string or a cons, process all the elements
22361 and effectively concatenate them.
22362 If first element is a negative number, truncate displaying cdr to
22363 at most that many characters. If positive, pad (with spaces)
22364 to at least that many characters.
22365 If first element is a symbol, process the cadr or caddr recursively
22366 according to whether the symbol's value is non-nil or nil. */
22367 car = XCAR (elt);
22368 if (EQ (car, QCeval))
22370 /* An element of the form (:eval FORM) means evaluate FORM
22371 and use the result as mode line elements. */
22373 if (risky)
22374 break;
22376 if (CONSP (XCDR (elt)))
22378 Lisp_Object spec;
22379 spec = safe__eval (true, XCAR (XCDR (elt)));
22380 n += display_mode_element (it, depth, field_width - n,
22381 precision - n, spec, props,
22382 risky);
22385 else if (EQ (car, QCpropertize))
22387 /* An element of the form (:propertize ELT PROPS...)
22388 means display ELT but applying properties PROPS. */
22390 if (risky)
22391 break;
22393 if (CONSP (XCDR (elt)))
22394 n += display_mode_element (it, depth, field_width - n,
22395 precision - n, XCAR (XCDR (elt)),
22396 XCDR (XCDR (elt)), risky);
22398 else if (SYMBOLP (car))
22400 tem = Fboundp (car);
22401 elt = XCDR (elt);
22402 if (!CONSP (elt))
22403 goto invalid;
22404 /* elt is now the cdr, and we know it is a cons cell.
22405 Use its car if CAR has a non-nil value. */
22406 if (!NILP (tem))
22408 tem = Fsymbol_value (car);
22409 if (!NILP (tem))
22411 elt = XCAR (elt);
22412 goto tail_recurse;
22415 /* Symbol's value is nil (or symbol is unbound)
22416 Get the cddr of the original list
22417 and if possible find the caddr and use that. */
22418 elt = XCDR (elt);
22419 if (NILP (elt))
22420 break;
22421 else if (!CONSP (elt))
22422 goto invalid;
22423 elt = XCAR (elt);
22424 goto tail_recurse;
22426 else if (INTEGERP (car))
22428 register int lim = XINT (car);
22429 elt = XCDR (elt);
22430 if (lim < 0)
22432 /* Negative int means reduce maximum width. */
22433 if (precision <= 0)
22434 precision = -lim;
22435 else
22436 precision = min (precision, -lim);
22438 else if (lim > 0)
22440 /* Padding specified. Don't let it be more than
22441 current maximum. */
22442 if (precision > 0)
22443 lim = min (precision, lim);
22445 /* If that's more padding than already wanted, queue it.
22446 But don't reduce padding already specified even if
22447 that is beyond the current truncation point. */
22448 field_width = max (lim, field_width);
22450 goto tail_recurse;
22452 else if (STRINGP (car) || CONSP (car))
22454 Lisp_Object halftail = elt;
22455 int len = 0;
22457 while (CONSP (elt)
22458 && (precision <= 0 || n < precision))
22460 n += display_mode_element (it, depth,
22461 /* Do padding only after the last
22462 element in the list. */
22463 (! CONSP (XCDR (elt))
22464 ? field_width - n
22465 : 0),
22466 precision - n, XCAR (elt),
22467 props, risky);
22468 elt = XCDR (elt);
22469 len++;
22470 if ((len & 1) == 0)
22471 halftail = XCDR (halftail);
22472 /* Check for cycle. */
22473 if (EQ (halftail, elt))
22474 break;
22478 break;
22480 default:
22481 invalid:
22482 elt = build_string ("*invalid*");
22483 goto tail_recurse;
22486 /* Pad to FIELD_WIDTH. */
22487 if (field_width > 0 && n < field_width)
22489 switch (mode_line_target)
22491 case MODE_LINE_NOPROP:
22492 case MODE_LINE_TITLE:
22493 n += store_mode_line_noprop ("", field_width - n, 0);
22494 break;
22495 case MODE_LINE_STRING:
22496 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22497 Qnil);
22498 break;
22499 case MODE_LINE_DISPLAY:
22500 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22501 0, 0, 0);
22502 break;
22506 return n;
22509 /* Store a mode-line string element in mode_line_string_list.
22511 If STRING is non-null, display that C string. Otherwise, the Lisp
22512 string LISP_STRING is displayed.
22514 FIELD_WIDTH is the minimum number of output glyphs to produce.
22515 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22516 with spaces. FIELD_WIDTH <= 0 means don't pad.
22518 PRECISION is the maximum number of characters to output from
22519 STRING. PRECISION <= 0 means don't truncate the string.
22521 If COPY_STRING, make a copy of LISP_STRING before adding
22522 properties to the string.
22524 PROPS are the properties to add to the string.
22525 The mode_line_string_face face property is always added to the string.
22528 static int
22529 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22530 bool copy_string,
22531 int field_width, int precision, Lisp_Object props)
22533 ptrdiff_t len;
22534 int n = 0;
22536 if (string != NULL)
22538 len = strlen (string);
22539 if (precision > 0 && len > precision)
22540 len = precision;
22541 lisp_string = make_string (string, len);
22542 if (NILP (props))
22543 props = mode_line_string_face_prop;
22544 else if (!NILP (mode_line_string_face))
22546 Lisp_Object face = Fplist_get (props, Qface);
22547 props = Fcopy_sequence (props);
22548 if (NILP (face))
22549 face = mode_line_string_face;
22550 else
22551 face = list2 (face, mode_line_string_face);
22552 props = Fplist_put (props, Qface, face);
22554 Fadd_text_properties (make_number (0), make_number (len),
22555 props, lisp_string);
22557 else
22559 len = XFASTINT (Flength (lisp_string));
22560 if (precision > 0 && len > precision)
22562 len = precision;
22563 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22564 precision = -1;
22566 if (!NILP (mode_line_string_face))
22568 Lisp_Object face;
22569 if (NILP (props))
22570 props = Ftext_properties_at (make_number (0), lisp_string);
22571 face = Fplist_get (props, Qface);
22572 if (NILP (face))
22573 face = mode_line_string_face;
22574 else
22575 face = list2 (face, mode_line_string_face);
22576 props = list2 (Qface, face);
22577 if (copy_string)
22578 lisp_string = Fcopy_sequence (lisp_string);
22580 if (!NILP (props))
22581 Fadd_text_properties (make_number (0), make_number (len),
22582 props, lisp_string);
22585 if (len > 0)
22587 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22588 n += len;
22591 if (field_width > len)
22593 field_width -= len;
22594 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22595 if (!NILP (props))
22596 Fadd_text_properties (make_number (0), make_number (field_width),
22597 props, lisp_string);
22598 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22599 n += field_width;
22602 return n;
22606 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22607 1, 4, 0,
22608 doc: /* Format a string out of a mode line format specification.
22609 First arg FORMAT specifies the mode line format (see `mode-line-format'
22610 for details) to use.
22612 By default, the format is evaluated for the currently selected window.
22614 Optional second arg FACE specifies the face property to put on all
22615 characters for which no face is specified. The value nil means the
22616 default face. The value t means whatever face the window's mode line
22617 currently uses (either `mode-line' or `mode-line-inactive',
22618 depending on whether the window is the selected window or not).
22619 An integer value means the value string has no text
22620 properties.
22622 Optional third and fourth args WINDOW and BUFFER specify the window
22623 and buffer to use as the context for the formatting (defaults
22624 are the selected window and the WINDOW's buffer). */)
22625 (Lisp_Object format, Lisp_Object face,
22626 Lisp_Object window, Lisp_Object buffer)
22628 struct it it;
22629 int len;
22630 struct window *w;
22631 struct buffer *old_buffer = NULL;
22632 int face_id;
22633 bool no_props = INTEGERP (face);
22634 ptrdiff_t count = SPECPDL_INDEX ();
22635 Lisp_Object str;
22636 int string_start = 0;
22638 w = decode_any_window (window);
22639 XSETWINDOW (window, w);
22641 if (NILP (buffer))
22642 buffer = w->contents;
22643 CHECK_BUFFER (buffer);
22645 /* Make formatting the modeline a non-op when noninteractive, otherwise
22646 there will be problems later caused by a partially initialized frame. */
22647 if (NILP (format) || noninteractive)
22648 return empty_unibyte_string;
22650 if (no_props)
22651 face = Qnil;
22653 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22654 : EQ (face, Qt) ? (EQ (window, selected_window)
22655 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22656 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22657 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22658 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22659 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22660 : DEFAULT_FACE_ID;
22662 old_buffer = current_buffer;
22664 /* Save things including mode_line_proptrans_alist,
22665 and set that to nil so that we don't alter the outer value. */
22666 record_unwind_protect (unwind_format_mode_line,
22667 format_mode_line_unwind_data
22668 (XFRAME (WINDOW_FRAME (w)),
22669 old_buffer, selected_window, true));
22670 mode_line_proptrans_alist = Qnil;
22672 Fselect_window (window, Qt);
22673 set_buffer_internal_1 (XBUFFER (buffer));
22675 init_iterator (&it, w, -1, -1, NULL, face_id);
22677 if (no_props)
22679 mode_line_target = MODE_LINE_NOPROP;
22680 mode_line_string_face_prop = Qnil;
22681 mode_line_string_list = Qnil;
22682 string_start = MODE_LINE_NOPROP_LEN (0);
22684 else
22686 mode_line_target = MODE_LINE_STRING;
22687 mode_line_string_list = Qnil;
22688 mode_line_string_face = face;
22689 mode_line_string_face_prop
22690 = NILP (face) ? Qnil : list2 (Qface, face);
22693 push_kboard (FRAME_KBOARD (it.f));
22694 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22695 pop_kboard ();
22697 if (no_props)
22699 len = MODE_LINE_NOPROP_LEN (string_start);
22700 str = make_string (mode_line_noprop_buf + string_start, len);
22702 else
22704 mode_line_string_list = Fnreverse (mode_line_string_list);
22705 str = Fmapconcat (Qidentity, mode_line_string_list,
22706 empty_unibyte_string);
22709 unbind_to (count, Qnil);
22710 return str;
22713 /* Write a null-terminated, right justified decimal representation of
22714 the positive integer D to BUF using a minimal field width WIDTH. */
22716 static void
22717 pint2str (register char *buf, register int width, register ptrdiff_t d)
22719 register char *p = buf;
22721 if (d <= 0)
22722 *p++ = '0';
22723 else
22725 while (d > 0)
22727 *p++ = d % 10 + '0';
22728 d /= 10;
22732 for (width -= (int) (p - buf); width > 0; --width)
22733 *p++ = ' ';
22734 *p-- = '\0';
22735 while (p > buf)
22737 d = *buf;
22738 *buf++ = *p;
22739 *p-- = d;
22743 /* Write a null-terminated, right justified decimal and "human
22744 readable" representation of the nonnegative integer D to BUF using
22745 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22747 static const char power_letter[] =
22749 0, /* no letter */
22750 'k', /* kilo */
22751 'M', /* mega */
22752 'G', /* giga */
22753 'T', /* tera */
22754 'P', /* peta */
22755 'E', /* exa */
22756 'Z', /* zetta */
22757 'Y' /* yotta */
22760 static void
22761 pint2hrstr (char *buf, int width, ptrdiff_t d)
22763 /* We aim to represent the nonnegative integer D as
22764 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22765 ptrdiff_t quotient = d;
22766 int remainder = 0;
22767 /* -1 means: do not use TENTHS. */
22768 int tenths = -1;
22769 int exponent = 0;
22771 /* Length of QUOTIENT.TENTHS as a string. */
22772 int length;
22774 char * psuffix;
22775 char * p;
22777 if (quotient >= 1000)
22779 /* Scale to the appropriate EXPONENT. */
22782 remainder = quotient % 1000;
22783 quotient /= 1000;
22784 exponent++;
22786 while (quotient >= 1000);
22788 /* Round to nearest and decide whether to use TENTHS or not. */
22789 if (quotient <= 9)
22791 tenths = remainder / 100;
22792 if (remainder % 100 >= 50)
22794 if (tenths < 9)
22795 tenths++;
22796 else
22798 quotient++;
22799 if (quotient == 10)
22800 tenths = -1;
22801 else
22802 tenths = 0;
22806 else
22807 if (remainder >= 500)
22809 if (quotient < 999)
22810 quotient++;
22811 else
22813 quotient = 1;
22814 exponent++;
22815 tenths = 0;
22820 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22821 if (tenths == -1 && quotient <= 99)
22822 if (quotient <= 9)
22823 length = 1;
22824 else
22825 length = 2;
22826 else
22827 length = 3;
22828 p = psuffix = buf + max (width, length);
22830 /* Print EXPONENT. */
22831 *psuffix++ = power_letter[exponent];
22832 *psuffix = '\0';
22834 /* Print TENTHS. */
22835 if (tenths >= 0)
22837 *--p = '0' + tenths;
22838 *--p = '.';
22841 /* Print QUOTIENT. */
22844 int digit = quotient % 10;
22845 *--p = '0' + digit;
22847 while ((quotient /= 10) != 0);
22849 /* Print leading spaces. */
22850 while (buf < p)
22851 *--p = ' ';
22854 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22855 If EOL_FLAG, set also a mnemonic character for end-of-line
22856 type of CODING_SYSTEM. Return updated pointer into BUF. */
22858 static unsigned char invalid_eol_type[] = "(*invalid*)";
22860 static char *
22861 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
22863 Lisp_Object val;
22864 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22865 const unsigned char *eol_str;
22866 int eol_str_len;
22867 /* The EOL conversion we are using. */
22868 Lisp_Object eoltype;
22870 val = CODING_SYSTEM_SPEC (coding_system);
22871 eoltype = Qnil;
22873 if (!VECTORP (val)) /* Not yet decided. */
22875 *buf++ = multibyte ? '-' : ' ';
22876 if (eol_flag)
22877 eoltype = eol_mnemonic_undecided;
22878 /* Don't mention EOL conversion if it isn't decided. */
22880 else
22882 Lisp_Object attrs;
22883 Lisp_Object eolvalue;
22885 attrs = AREF (val, 0);
22886 eolvalue = AREF (val, 2);
22888 *buf++ = multibyte
22889 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22890 : ' ';
22892 if (eol_flag)
22894 /* The EOL conversion that is normal on this system. */
22896 if (NILP (eolvalue)) /* Not yet decided. */
22897 eoltype = eol_mnemonic_undecided;
22898 else if (VECTORP (eolvalue)) /* Not yet decided. */
22899 eoltype = eol_mnemonic_undecided;
22900 else /* eolvalue is Qunix, Qdos, or Qmac. */
22901 eoltype = (EQ (eolvalue, Qunix)
22902 ? eol_mnemonic_unix
22903 : EQ (eolvalue, Qdos)
22904 ? eol_mnemonic_dos : eol_mnemonic_mac);
22908 if (eol_flag)
22910 /* Mention the EOL conversion if it is not the usual one. */
22911 if (STRINGP (eoltype))
22913 eol_str = SDATA (eoltype);
22914 eol_str_len = SBYTES (eoltype);
22916 else if (CHARACTERP (eoltype))
22918 int c = XFASTINT (eoltype);
22919 return buf + CHAR_STRING (c, (unsigned char *) buf);
22921 else
22923 eol_str = invalid_eol_type;
22924 eol_str_len = sizeof (invalid_eol_type) - 1;
22926 memcpy (buf, eol_str, eol_str_len);
22927 buf += eol_str_len;
22930 return buf;
22933 /* Return a string for the output of a mode line %-spec for window W,
22934 generated by character C. FIELD_WIDTH > 0 means pad the string
22935 returned with spaces to that value. Return a Lisp string in
22936 *STRING if the resulting string is taken from that Lisp string.
22938 Note we operate on the current buffer for most purposes. */
22940 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22942 static const char *
22943 decode_mode_spec (struct window *w, register int c, int field_width,
22944 Lisp_Object *string)
22946 Lisp_Object obj;
22947 struct frame *f = XFRAME (WINDOW_FRAME (w));
22948 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22949 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22950 produce strings from numerical values, so limit preposterously
22951 large values of FIELD_WIDTH to avoid overrunning the buffer's
22952 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22953 bytes plus the terminating null. */
22954 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22955 struct buffer *b = current_buffer;
22957 obj = Qnil;
22958 *string = Qnil;
22960 switch (c)
22962 case '*':
22963 if (!NILP (BVAR (b, read_only)))
22964 return "%";
22965 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22966 return "*";
22967 return "-";
22969 case '+':
22970 /* This differs from %* only for a modified read-only buffer. */
22971 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22972 return "*";
22973 if (!NILP (BVAR (b, read_only)))
22974 return "%";
22975 return "-";
22977 case '&':
22978 /* This differs from %* in ignoring read-only-ness. */
22979 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22980 return "*";
22981 return "-";
22983 case '%':
22984 return "%";
22986 case '[':
22988 int i;
22989 char *p;
22991 if (command_loop_level > 5)
22992 return "[[[... ";
22993 p = decode_mode_spec_buf;
22994 for (i = 0; i < command_loop_level; i++)
22995 *p++ = '[';
22996 *p = 0;
22997 return decode_mode_spec_buf;
23000 case ']':
23002 int i;
23003 char *p;
23005 if (command_loop_level > 5)
23006 return " ...]]]";
23007 p = decode_mode_spec_buf;
23008 for (i = 0; i < command_loop_level; i++)
23009 *p++ = ']';
23010 *p = 0;
23011 return decode_mode_spec_buf;
23014 case '-':
23016 register int i;
23018 /* Let lots_of_dashes be a string of infinite length. */
23019 if (mode_line_target == MODE_LINE_NOPROP
23020 || mode_line_target == MODE_LINE_STRING)
23021 return "--";
23022 if (field_width <= 0
23023 || field_width > sizeof (lots_of_dashes))
23025 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23026 decode_mode_spec_buf[i] = '-';
23027 decode_mode_spec_buf[i] = '\0';
23028 return decode_mode_spec_buf;
23030 else
23031 return lots_of_dashes;
23034 case 'b':
23035 obj = BVAR (b, name);
23036 break;
23038 case 'c':
23039 /* %c and %l are ignored in `frame-title-format'.
23040 (In redisplay_internal, the frame title is drawn _before_ the
23041 windows are updated, so the stuff which depends on actual
23042 window contents (such as %l) may fail to render properly, or
23043 even crash emacs.) */
23044 if (mode_line_target == MODE_LINE_TITLE)
23045 return "";
23046 else
23048 ptrdiff_t col = current_column ();
23049 w->column_number_displayed = col;
23050 pint2str (decode_mode_spec_buf, width, col);
23051 return decode_mode_spec_buf;
23054 case 'e':
23055 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23057 if (NILP (Vmemory_full))
23058 return "";
23059 else
23060 return "!MEM FULL! ";
23062 #else
23063 return "";
23064 #endif
23066 case 'F':
23067 /* %F displays the frame name. */
23068 if (!NILP (f->title))
23069 return SSDATA (f->title);
23070 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23071 return SSDATA (f->name);
23072 return "Emacs";
23074 case 'f':
23075 obj = BVAR (b, filename);
23076 break;
23078 case 'i':
23080 ptrdiff_t size = ZV - BEGV;
23081 pint2str (decode_mode_spec_buf, width, size);
23082 return decode_mode_spec_buf;
23085 case 'I':
23087 ptrdiff_t size = ZV - BEGV;
23088 pint2hrstr (decode_mode_spec_buf, width, size);
23089 return decode_mode_spec_buf;
23092 case 'l':
23094 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23095 ptrdiff_t topline, nlines, height;
23096 ptrdiff_t junk;
23098 /* %c and %l are ignored in `frame-title-format'. */
23099 if (mode_line_target == MODE_LINE_TITLE)
23100 return "";
23102 startpos = marker_position (w->start);
23103 startpos_byte = marker_byte_position (w->start);
23104 height = WINDOW_TOTAL_LINES (w);
23106 /* If we decided that this buffer isn't suitable for line numbers,
23107 don't forget that too fast. */
23108 if (w->base_line_pos == -1)
23109 goto no_value;
23111 /* If the buffer is very big, don't waste time. */
23112 if (INTEGERP (Vline_number_display_limit)
23113 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23115 w->base_line_pos = 0;
23116 w->base_line_number = 0;
23117 goto no_value;
23120 if (w->base_line_number > 0
23121 && w->base_line_pos > 0
23122 && w->base_line_pos <= startpos)
23124 line = w->base_line_number;
23125 linepos = w->base_line_pos;
23126 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23128 else
23130 line = 1;
23131 linepos = BUF_BEGV (b);
23132 linepos_byte = BUF_BEGV_BYTE (b);
23135 /* Count lines from base line to window start position. */
23136 nlines = display_count_lines (linepos_byte,
23137 startpos_byte,
23138 startpos, &junk);
23140 topline = nlines + line;
23142 /* Determine a new base line, if the old one is too close
23143 or too far away, or if we did not have one.
23144 "Too close" means it's plausible a scroll-down would
23145 go back past it. */
23146 if (startpos == BUF_BEGV (b))
23148 w->base_line_number = topline;
23149 w->base_line_pos = BUF_BEGV (b);
23151 else if (nlines < height + 25 || nlines > height * 3 + 50
23152 || linepos == BUF_BEGV (b))
23154 ptrdiff_t limit = BUF_BEGV (b);
23155 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23156 ptrdiff_t position;
23157 ptrdiff_t distance =
23158 (height * 2 + 30) * line_number_display_limit_width;
23160 if (startpos - distance > limit)
23162 limit = startpos - distance;
23163 limit_byte = CHAR_TO_BYTE (limit);
23166 nlines = display_count_lines (startpos_byte,
23167 limit_byte,
23168 - (height * 2 + 30),
23169 &position);
23170 /* If we couldn't find the lines we wanted within
23171 line_number_display_limit_width chars per line,
23172 give up on line numbers for this window. */
23173 if (position == limit_byte && limit == startpos - distance)
23175 w->base_line_pos = -1;
23176 w->base_line_number = 0;
23177 goto no_value;
23180 w->base_line_number = topline - nlines;
23181 w->base_line_pos = BYTE_TO_CHAR (position);
23184 /* Now count lines from the start pos to point. */
23185 nlines = display_count_lines (startpos_byte,
23186 PT_BYTE, PT, &junk);
23188 /* Record that we did display the line number. */
23189 line_number_displayed = true;
23191 /* Make the string to show. */
23192 pint2str (decode_mode_spec_buf, width, topline + nlines);
23193 return decode_mode_spec_buf;
23194 no_value:
23196 char *p = decode_mode_spec_buf;
23197 int pad = width - 2;
23198 while (pad-- > 0)
23199 *p++ = ' ';
23200 *p++ = '?';
23201 *p++ = '?';
23202 *p = '\0';
23203 return decode_mode_spec_buf;
23206 break;
23208 case 'm':
23209 obj = BVAR (b, mode_name);
23210 break;
23212 case 'n':
23213 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23214 return " Narrow";
23215 break;
23217 case 'p':
23219 ptrdiff_t pos = marker_position (w->start);
23220 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23222 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23224 if (pos <= BUF_BEGV (b))
23225 return "All";
23226 else
23227 return "Bottom";
23229 else if (pos <= BUF_BEGV (b))
23230 return "Top";
23231 else
23233 if (total > 1000000)
23234 /* Do it differently for a large value, to avoid overflow. */
23235 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23236 else
23237 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23238 /* We can't normally display a 3-digit number,
23239 so get us a 2-digit number that is close. */
23240 if (total == 100)
23241 total = 99;
23242 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23243 return decode_mode_spec_buf;
23247 /* Display percentage of size above the bottom of the screen. */
23248 case 'P':
23250 ptrdiff_t toppos = marker_position (w->start);
23251 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23252 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23254 if (botpos >= BUF_ZV (b))
23256 if (toppos <= BUF_BEGV (b))
23257 return "All";
23258 else
23259 return "Bottom";
23261 else
23263 if (total > 1000000)
23264 /* Do it differently for a large value, to avoid overflow. */
23265 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23266 else
23267 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23268 /* We can't normally display a 3-digit number,
23269 so get us a 2-digit number that is close. */
23270 if (total == 100)
23271 total = 99;
23272 if (toppos <= BUF_BEGV (b))
23273 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23274 else
23275 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23276 return decode_mode_spec_buf;
23280 case 's':
23281 /* status of process */
23282 obj = Fget_buffer_process (Fcurrent_buffer ());
23283 if (NILP (obj))
23284 return "no process";
23285 #ifndef MSDOS
23286 obj = Fsymbol_name (Fprocess_status (obj));
23287 #endif
23288 break;
23290 case '@':
23292 ptrdiff_t count = inhibit_garbage_collection ();
23293 Lisp_Object curdir = BVAR (current_buffer, directory);
23294 Lisp_Object val = Qnil;
23296 if (STRINGP (curdir))
23297 val = call1 (intern ("file-remote-p"), curdir);
23299 unbind_to (count, Qnil);
23301 if (NILP (val))
23302 return "-";
23303 else
23304 return "@";
23307 case 'z':
23308 /* coding-system (not including end-of-line format) */
23309 case 'Z':
23310 /* coding-system (including end-of-line type) */
23312 bool eol_flag = (c == 'Z');
23313 char *p = decode_mode_spec_buf;
23315 if (! FRAME_WINDOW_P (f))
23317 /* No need to mention EOL here--the terminal never needs
23318 to do EOL conversion. */
23319 p = decode_mode_spec_coding (CODING_ID_NAME
23320 (FRAME_KEYBOARD_CODING (f)->id),
23321 p, false);
23322 p = decode_mode_spec_coding (CODING_ID_NAME
23323 (FRAME_TERMINAL_CODING (f)->id),
23324 p, false);
23326 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23327 p, eol_flag);
23329 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23330 #ifdef subprocesses
23331 obj = Fget_buffer_process (Fcurrent_buffer ());
23332 if (PROCESSP (obj))
23334 p = decode_mode_spec_coding
23335 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23336 p = decode_mode_spec_coding
23337 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23339 #endif /* subprocesses */
23340 #endif /* false */
23341 *p = 0;
23342 return decode_mode_spec_buf;
23346 if (STRINGP (obj))
23348 *string = obj;
23349 return SSDATA (obj);
23351 else
23352 return "";
23356 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23357 means count lines back from START_BYTE. But don't go beyond
23358 LIMIT_BYTE. Return the number of lines thus found (always
23359 nonnegative).
23361 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23362 either the position COUNT lines after/before START_BYTE, if we
23363 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23364 COUNT lines. */
23366 static ptrdiff_t
23367 display_count_lines (ptrdiff_t start_byte,
23368 ptrdiff_t limit_byte, ptrdiff_t count,
23369 ptrdiff_t *byte_pos_ptr)
23371 register unsigned char *cursor;
23372 unsigned char *base;
23374 register ptrdiff_t ceiling;
23375 register unsigned char *ceiling_addr;
23376 ptrdiff_t orig_count = count;
23378 /* If we are not in selective display mode,
23379 check only for newlines. */
23380 bool selective_display
23381 = (!NILP (BVAR (current_buffer, selective_display))
23382 && !INTEGERP (BVAR (current_buffer, selective_display)));
23384 if (count > 0)
23386 while (start_byte < limit_byte)
23388 ceiling = BUFFER_CEILING_OF (start_byte);
23389 ceiling = min (limit_byte - 1, ceiling);
23390 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23391 base = (cursor = BYTE_POS_ADDR (start_byte));
23395 if (selective_display)
23397 while (*cursor != '\n' && *cursor != 015
23398 && ++cursor != ceiling_addr)
23399 continue;
23400 if (cursor == ceiling_addr)
23401 break;
23403 else
23405 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23406 if (! cursor)
23407 break;
23410 cursor++;
23412 if (--count == 0)
23414 start_byte += cursor - base;
23415 *byte_pos_ptr = start_byte;
23416 return orig_count;
23419 while (cursor < ceiling_addr);
23421 start_byte += ceiling_addr - base;
23424 else
23426 while (start_byte > limit_byte)
23428 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23429 ceiling = max (limit_byte, ceiling);
23430 ceiling_addr = BYTE_POS_ADDR (ceiling);
23431 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23432 while (true)
23434 if (selective_display)
23436 while (--cursor >= ceiling_addr
23437 && *cursor != '\n' && *cursor != 015)
23438 continue;
23439 if (cursor < ceiling_addr)
23440 break;
23442 else
23444 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23445 if (! cursor)
23446 break;
23449 if (++count == 0)
23451 start_byte += cursor - base + 1;
23452 *byte_pos_ptr = start_byte;
23453 /* When scanning backwards, we should
23454 not count the newline posterior to which we stop. */
23455 return - orig_count - 1;
23458 start_byte += ceiling_addr - base;
23462 *byte_pos_ptr = limit_byte;
23464 if (count < 0)
23465 return - orig_count + count;
23466 return orig_count - count;
23472 /***********************************************************************
23473 Displaying strings
23474 ***********************************************************************/
23476 /* Display a NUL-terminated string, starting with index START.
23478 If STRING is non-null, display that C string. Otherwise, the Lisp
23479 string LISP_STRING is displayed. There's a case that STRING is
23480 non-null and LISP_STRING is not nil. It means STRING is a string
23481 data of LISP_STRING. In that case, we display LISP_STRING while
23482 ignoring its text properties.
23484 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23485 FACE_STRING. Display STRING or LISP_STRING with the face at
23486 FACE_STRING_POS in FACE_STRING:
23488 Display the string in the environment given by IT, but use the
23489 standard display table, temporarily.
23491 FIELD_WIDTH is the minimum number of output glyphs to produce.
23492 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23493 with spaces. If STRING has more characters, more than FIELD_WIDTH
23494 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23496 PRECISION is the maximum number of characters to output from
23497 STRING. PRECISION < 0 means don't truncate the string.
23499 This is roughly equivalent to printf format specifiers:
23501 FIELD_WIDTH PRECISION PRINTF
23502 ----------------------------------------
23503 -1 -1 %s
23504 -1 10 %.10s
23505 10 -1 %10s
23506 20 10 %20.10s
23508 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23509 display them, and < 0 means obey the current buffer's value of
23510 enable_multibyte_characters.
23512 Value is the number of columns displayed. */
23514 static int
23515 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23516 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23517 int field_width, int precision, int max_x, int multibyte)
23519 int hpos_at_start = it->hpos;
23520 int saved_face_id = it->face_id;
23521 struct glyph_row *row = it->glyph_row;
23522 ptrdiff_t it_charpos;
23524 /* Initialize the iterator IT for iteration over STRING beginning
23525 with index START. */
23526 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23527 precision, field_width, multibyte);
23528 if (string && STRINGP (lisp_string))
23529 /* LISP_STRING is the one returned by decode_mode_spec. We should
23530 ignore its text properties. */
23531 it->stop_charpos = it->end_charpos;
23533 /* If displaying STRING, set up the face of the iterator from
23534 FACE_STRING, if that's given. */
23535 if (STRINGP (face_string))
23537 ptrdiff_t endptr;
23538 struct face *face;
23540 it->face_id
23541 = face_at_string_position (it->w, face_string, face_string_pos,
23542 0, &endptr, it->base_face_id, false);
23543 face = FACE_FROM_ID (it->f, it->face_id);
23544 it->face_box_p = face->box != FACE_NO_BOX;
23547 /* Set max_x to the maximum allowed X position. Don't let it go
23548 beyond the right edge of the window. */
23549 if (max_x <= 0)
23550 max_x = it->last_visible_x;
23551 else
23552 max_x = min (max_x, it->last_visible_x);
23554 /* Skip over display elements that are not visible. because IT->w is
23555 hscrolled. */
23556 if (it->current_x < it->first_visible_x)
23557 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23558 MOVE_TO_POS | MOVE_TO_X);
23560 row->ascent = it->max_ascent;
23561 row->height = it->max_ascent + it->max_descent;
23562 row->phys_ascent = it->max_phys_ascent;
23563 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23564 row->extra_line_spacing = it->max_extra_line_spacing;
23566 if (STRINGP (it->string))
23567 it_charpos = IT_STRING_CHARPOS (*it);
23568 else
23569 it_charpos = IT_CHARPOS (*it);
23571 /* This condition is for the case that we are called with current_x
23572 past last_visible_x. */
23573 while (it->current_x < max_x)
23575 int x_before, x, n_glyphs_before, i, nglyphs;
23577 /* Get the next display element. */
23578 if (!get_next_display_element (it))
23579 break;
23581 /* Produce glyphs. */
23582 x_before = it->current_x;
23583 n_glyphs_before = row->used[TEXT_AREA];
23584 PRODUCE_GLYPHS (it);
23586 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23587 i = 0;
23588 x = x_before;
23589 while (i < nglyphs)
23591 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23593 if (it->line_wrap != TRUNCATE
23594 && x + glyph->pixel_width > max_x)
23596 /* End of continued line or max_x reached. */
23597 if (CHAR_GLYPH_PADDING_P (*glyph))
23599 /* A wide character is unbreakable. */
23600 if (row->reversed_p)
23601 unproduce_glyphs (it, row->used[TEXT_AREA]
23602 - n_glyphs_before);
23603 row->used[TEXT_AREA] = n_glyphs_before;
23604 it->current_x = x_before;
23606 else
23608 if (row->reversed_p)
23609 unproduce_glyphs (it, row->used[TEXT_AREA]
23610 - (n_glyphs_before + i));
23611 row->used[TEXT_AREA] = n_glyphs_before + i;
23612 it->current_x = x;
23614 break;
23616 else if (x + glyph->pixel_width >= it->first_visible_x)
23618 /* Glyph is at least partially visible. */
23619 ++it->hpos;
23620 if (x < it->first_visible_x)
23621 row->x = x - it->first_visible_x;
23623 else
23625 /* Glyph is off the left margin of the display area.
23626 Should not happen. */
23627 emacs_abort ();
23630 row->ascent = max (row->ascent, it->max_ascent);
23631 row->height = max (row->height, it->max_ascent + it->max_descent);
23632 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23633 row->phys_height = max (row->phys_height,
23634 it->max_phys_ascent + it->max_phys_descent);
23635 row->extra_line_spacing = max (row->extra_line_spacing,
23636 it->max_extra_line_spacing);
23637 x += glyph->pixel_width;
23638 ++i;
23641 /* Stop if max_x reached. */
23642 if (i < nglyphs)
23643 break;
23645 /* Stop at line ends. */
23646 if (ITERATOR_AT_END_OF_LINE_P (it))
23648 it->continuation_lines_width = 0;
23649 break;
23652 set_iterator_to_next (it, true);
23653 if (STRINGP (it->string))
23654 it_charpos = IT_STRING_CHARPOS (*it);
23655 else
23656 it_charpos = IT_CHARPOS (*it);
23658 /* Stop if truncating at the right edge. */
23659 if (it->line_wrap == TRUNCATE
23660 && it->current_x >= it->last_visible_x)
23662 /* Add truncation mark, but don't do it if the line is
23663 truncated at a padding space. */
23664 if (it_charpos < it->string_nchars)
23666 if (!FRAME_WINDOW_P (it->f))
23668 int ii, n;
23670 if (it->current_x > it->last_visible_x)
23672 if (!row->reversed_p)
23674 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23675 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23676 break;
23678 else
23680 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23681 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23682 break;
23683 unproduce_glyphs (it, ii + 1);
23684 ii = row->used[TEXT_AREA] - (ii + 1);
23686 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23688 row->used[TEXT_AREA] = ii;
23689 produce_special_glyphs (it, IT_TRUNCATION);
23692 produce_special_glyphs (it, IT_TRUNCATION);
23694 row->truncated_on_right_p = true;
23696 break;
23700 /* Maybe insert a truncation at the left. */
23701 if (it->first_visible_x
23702 && it_charpos > 0)
23704 if (!FRAME_WINDOW_P (it->f)
23705 || (row->reversed_p
23706 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23707 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23708 insert_left_trunc_glyphs (it);
23709 row->truncated_on_left_p = true;
23712 it->face_id = saved_face_id;
23714 /* Value is number of columns displayed. */
23715 return it->hpos - hpos_at_start;
23720 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23721 appears as an element of LIST or as the car of an element of LIST.
23722 If PROPVAL is a list, compare each element against LIST in that
23723 way, and return 1/2 if any element of PROPVAL is found in LIST.
23724 Otherwise return 0. This function cannot quit.
23725 The return value is 2 if the text is invisible but with an ellipsis
23726 and 1 if it's invisible and without an ellipsis. */
23729 invisible_prop (Lisp_Object propval, Lisp_Object list)
23731 Lisp_Object tail, proptail;
23733 for (tail = list; CONSP (tail); tail = XCDR (tail))
23735 register Lisp_Object tem;
23736 tem = XCAR (tail);
23737 if (EQ (propval, tem))
23738 return 1;
23739 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23740 return NILP (XCDR (tem)) ? 1 : 2;
23743 if (CONSP (propval))
23745 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23747 Lisp_Object propelt;
23748 propelt = XCAR (proptail);
23749 for (tail = list; CONSP (tail); tail = XCDR (tail))
23751 register Lisp_Object tem;
23752 tem = XCAR (tail);
23753 if (EQ (propelt, tem))
23754 return 1;
23755 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23756 return NILP (XCDR (tem)) ? 1 : 2;
23761 return 0;
23764 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23765 doc: /* Non-nil if the property makes the text invisible.
23766 POS-OR-PROP can be a marker or number, in which case it is taken to be
23767 a position in the current buffer and the value of the `invisible' property
23768 is checked; or it can be some other value, which is then presumed to be the
23769 value of the `invisible' property of the text of interest.
23770 The non-nil value returned can be t for truly invisible text or something
23771 else if the text is replaced by an ellipsis. */)
23772 (Lisp_Object pos_or_prop)
23774 Lisp_Object prop
23775 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23776 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23777 : pos_or_prop);
23778 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23779 return (invis == 0 ? Qnil
23780 : invis == 1 ? Qt
23781 : make_number (invis));
23784 /* Calculate a width or height in pixels from a specification using
23785 the following elements:
23787 SPEC ::=
23788 NUM - a (fractional) multiple of the default font width/height
23789 (NUM) - specifies exactly NUM pixels
23790 UNIT - a fixed number of pixels, see below.
23791 ELEMENT - size of a display element in pixels, see below.
23792 (NUM . SPEC) - equals NUM * SPEC
23793 (+ SPEC SPEC ...) - add pixel values
23794 (- SPEC SPEC ...) - subtract pixel values
23795 (- SPEC) - negate pixel value
23797 NUM ::=
23798 INT or FLOAT - a number constant
23799 SYMBOL - use symbol's (buffer local) variable binding.
23801 UNIT ::=
23802 in - pixels per inch *)
23803 mm - pixels per 1/1000 meter *)
23804 cm - pixels per 1/100 meter *)
23805 width - width of current font in pixels.
23806 height - height of current font in pixels.
23808 *) using the ratio(s) defined in display-pixels-per-inch.
23810 ELEMENT ::=
23812 left-fringe - left fringe width in pixels
23813 right-fringe - right fringe width in pixels
23815 left-margin - left margin width in pixels
23816 right-margin - right margin width in pixels
23818 scroll-bar - scroll-bar area width in pixels
23820 Examples:
23822 Pixels corresponding to 5 inches:
23823 (5 . in)
23825 Total width of non-text areas on left side of window (if scroll-bar is on left):
23826 '(space :width (+ left-fringe left-margin scroll-bar))
23828 Align to first text column (in header line):
23829 '(space :align-to 0)
23831 Align to middle of text area minus half the width of variable `my-image'
23832 containing a loaded image:
23833 '(space :align-to (0.5 . (- text my-image)))
23835 Width of left margin minus width of 1 character in the default font:
23836 '(space :width (- left-margin 1))
23838 Width of left margin minus width of 2 characters in the current font:
23839 '(space :width (- left-margin (2 . width)))
23841 Center 1 character over left-margin (in header line):
23842 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23844 Different ways to express width of left fringe plus left margin minus one pixel:
23845 '(space :width (- (+ left-fringe left-margin) (1)))
23846 '(space :width (+ left-fringe left-margin (- (1))))
23847 '(space :width (+ left-fringe left-margin (-1)))
23851 static bool
23852 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23853 struct font *font, bool width_p, int *align_to)
23855 double pixels;
23857 # define OK_PIXELS(val) (*res = (val), true)
23858 # define OK_ALIGN_TO(val) (*align_to = (val), true)
23860 if (NILP (prop))
23861 return OK_PIXELS (0);
23863 eassert (FRAME_LIVE_P (it->f));
23865 if (SYMBOLP (prop))
23867 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23869 char *unit = SSDATA (SYMBOL_NAME (prop));
23871 if (unit[0] == 'i' && unit[1] == 'n')
23872 pixels = 1.0;
23873 else if (unit[0] == 'm' && unit[1] == 'm')
23874 pixels = 25.4;
23875 else if (unit[0] == 'c' && unit[1] == 'm')
23876 pixels = 2.54;
23877 else
23878 pixels = 0;
23879 if (pixels > 0)
23881 double ppi = (width_p ? FRAME_RES_X (it->f)
23882 : FRAME_RES_Y (it->f));
23884 if (ppi > 0)
23885 return OK_PIXELS (ppi / pixels);
23886 return false;
23890 #ifdef HAVE_WINDOW_SYSTEM
23891 if (EQ (prop, Qheight))
23892 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23893 if (EQ (prop, Qwidth))
23894 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23895 #else
23896 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23897 return OK_PIXELS (1);
23898 #endif
23900 if (EQ (prop, Qtext))
23901 return OK_PIXELS (width_p
23902 ? window_box_width (it->w, TEXT_AREA)
23903 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23905 if (align_to && *align_to < 0)
23907 *res = 0;
23908 if (EQ (prop, Qleft))
23909 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23910 if (EQ (prop, Qright))
23911 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23912 if (EQ (prop, Qcenter))
23913 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23914 + window_box_width (it->w, TEXT_AREA) / 2);
23915 if (EQ (prop, Qleft_fringe))
23916 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23917 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23918 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23919 if (EQ (prop, Qright_fringe))
23920 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23921 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23922 : window_box_right_offset (it->w, TEXT_AREA));
23923 if (EQ (prop, Qleft_margin))
23924 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23925 if (EQ (prop, Qright_margin))
23926 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23927 if (EQ (prop, Qscroll_bar))
23928 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23930 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23931 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23932 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23933 : 0)));
23935 else
23937 if (EQ (prop, Qleft_fringe))
23938 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23939 if (EQ (prop, Qright_fringe))
23940 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23941 if (EQ (prop, Qleft_margin))
23942 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23943 if (EQ (prop, Qright_margin))
23944 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23945 if (EQ (prop, Qscroll_bar))
23946 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23949 prop = buffer_local_value (prop, it->w->contents);
23950 if (EQ (prop, Qunbound))
23951 prop = Qnil;
23954 if (INTEGERP (prop) || FLOATP (prop))
23956 int base_unit = (width_p
23957 ? FRAME_COLUMN_WIDTH (it->f)
23958 : FRAME_LINE_HEIGHT (it->f));
23959 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23962 if (CONSP (prop))
23964 Lisp_Object car = XCAR (prop);
23965 Lisp_Object cdr = XCDR (prop);
23967 if (SYMBOLP (car))
23969 #ifdef HAVE_WINDOW_SYSTEM
23970 if (FRAME_WINDOW_P (it->f)
23971 && valid_image_p (prop))
23973 ptrdiff_t id = lookup_image (it->f, prop);
23974 struct image *img = IMAGE_FROM_ID (it->f, id);
23976 return OK_PIXELS (width_p ? img->width : img->height);
23978 #endif
23979 if (EQ (car, Qplus) || EQ (car, Qminus))
23981 bool first = true;
23982 double px;
23984 pixels = 0;
23985 while (CONSP (cdr))
23987 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23988 font, width_p, align_to))
23989 return false;
23990 if (first)
23991 pixels = (EQ (car, Qplus) ? px : -px), first = false;
23992 else
23993 pixels += px;
23994 cdr = XCDR (cdr);
23996 if (EQ (car, Qminus))
23997 pixels = -pixels;
23998 return OK_PIXELS (pixels);
24001 car = buffer_local_value (car, it->w->contents);
24002 if (EQ (car, Qunbound))
24003 car = Qnil;
24006 if (INTEGERP (car) || FLOATP (car))
24008 double fact;
24009 pixels = XFLOATINT (car);
24010 if (NILP (cdr))
24011 return OK_PIXELS (pixels);
24012 if (calc_pixel_width_or_height (&fact, it, cdr,
24013 font, width_p, align_to))
24014 return OK_PIXELS (pixels * fact);
24015 return false;
24018 return false;
24021 return false;
24025 /***********************************************************************
24026 Glyph Display
24027 ***********************************************************************/
24029 #ifdef HAVE_WINDOW_SYSTEM
24031 #ifdef GLYPH_DEBUG
24033 void
24034 dump_glyph_string (struct glyph_string *s)
24036 fprintf (stderr, "glyph string\n");
24037 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24038 s->x, s->y, s->width, s->height);
24039 fprintf (stderr, " ybase = %d\n", s->ybase);
24040 fprintf (stderr, " hl = %d\n", s->hl);
24041 fprintf (stderr, " left overhang = %d, right = %d\n",
24042 s->left_overhang, s->right_overhang);
24043 fprintf (stderr, " nchars = %d\n", s->nchars);
24044 fprintf (stderr, " extends to end of line = %d\n",
24045 s->extends_to_end_of_line_p);
24046 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24047 fprintf (stderr, " bg width = %d\n", s->background_width);
24050 #endif /* GLYPH_DEBUG */
24052 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24053 of XChar2b structures for S; it can't be allocated in
24054 init_glyph_string because it must be allocated via `alloca'. W
24055 is the window on which S is drawn. ROW and AREA are the glyph row
24056 and area within the row from which S is constructed. START is the
24057 index of the first glyph structure covered by S. HL is a
24058 face-override for drawing S. */
24060 #ifdef HAVE_NTGUI
24061 #define OPTIONAL_HDC(hdc) HDC hdc,
24062 #define DECLARE_HDC(hdc) HDC hdc;
24063 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24064 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24065 #endif
24067 #ifndef OPTIONAL_HDC
24068 #define OPTIONAL_HDC(hdc)
24069 #define DECLARE_HDC(hdc)
24070 #define ALLOCATE_HDC(hdc, f)
24071 #define RELEASE_HDC(hdc, f)
24072 #endif
24074 static void
24075 init_glyph_string (struct glyph_string *s,
24076 OPTIONAL_HDC (hdc)
24077 XChar2b *char2b, struct window *w, struct glyph_row *row,
24078 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24080 memset (s, 0, sizeof *s);
24081 s->w = w;
24082 s->f = XFRAME (w->frame);
24083 #ifdef HAVE_NTGUI
24084 s->hdc = hdc;
24085 #endif
24086 s->display = FRAME_X_DISPLAY (s->f);
24087 s->window = FRAME_X_WINDOW (s->f);
24088 s->char2b = char2b;
24089 s->hl = hl;
24090 s->row = row;
24091 s->area = area;
24092 s->first_glyph = row->glyphs[area] + start;
24093 s->height = row->height;
24094 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24095 s->ybase = s->y + row->ascent;
24099 /* Append the list of glyph strings with head H and tail T to the list
24100 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24102 static void
24103 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24104 struct glyph_string *h, struct glyph_string *t)
24106 if (h)
24108 if (*head)
24109 (*tail)->next = h;
24110 else
24111 *head = h;
24112 h->prev = *tail;
24113 *tail = t;
24118 /* Prepend the list of glyph strings with head H and tail T to the
24119 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24120 result. */
24122 static void
24123 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24124 struct glyph_string *h, struct glyph_string *t)
24126 if (h)
24128 if (*head)
24129 (*head)->prev = t;
24130 else
24131 *tail = t;
24132 t->next = *head;
24133 *head = h;
24138 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24139 Set *HEAD and *TAIL to the resulting list. */
24141 static void
24142 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24143 struct glyph_string *s)
24145 s->next = s->prev = NULL;
24146 append_glyph_string_lists (head, tail, s, s);
24150 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24151 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24152 make sure that X resources for the face returned are allocated.
24153 Value is a pointer to a realized face that is ready for display if
24154 DISPLAY_P. */
24156 static struct face *
24157 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24158 XChar2b *char2b, bool display_p)
24160 struct face *face = FACE_FROM_ID (f, face_id);
24161 unsigned code = 0;
24163 if (face->font)
24165 code = face->font->driver->encode_char (face->font, c);
24167 if (code == FONT_INVALID_CODE)
24168 code = 0;
24170 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24172 /* Make sure X resources of the face are allocated. */
24173 #ifdef HAVE_X_WINDOWS
24174 if (display_p)
24175 #endif
24177 eassert (face != NULL);
24178 prepare_face_for_display (f, face);
24181 return face;
24185 /* Get face and two-byte form of character glyph GLYPH on frame F.
24186 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24187 a pointer to a realized face that is ready for display. */
24189 static struct face *
24190 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24191 XChar2b *char2b)
24193 struct face *face;
24194 unsigned code = 0;
24196 eassert (glyph->type == CHAR_GLYPH);
24197 face = FACE_FROM_ID (f, glyph->face_id);
24199 /* Make sure X resources of the face are allocated. */
24200 eassert (face != NULL);
24201 prepare_face_for_display (f, face);
24203 if (face->font)
24205 if (CHAR_BYTE8_P (glyph->u.ch))
24206 code = CHAR_TO_BYTE8 (glyph->u.ch);
24207 else
24208 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24210 if (code == FONT_INVALID_CODE)
24211 code = 0;
24214 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24215 return face;
24219 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24220 Return true iff FONT has a glyph for C. */
24222 static bool
24223 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24225 unsigned code;
24227 if (CHAR_BYTE8_P (c))
24228 code = CHAR_TO_BYTE8 (c);
24229 else
24230 code = font->driver->encode_char (font, c);
24232 if (code == FONT_INVALID_CODE)
24233 return false;
24234 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24235 return true;
24239 /* Fill glyph string S with composition components specified by S->cmp.
24241 BASE_FACE is the base face of the composition.
24242 S->cmp_from is the index of the first component for S.
24244 OVERLAPS non-zero means S should draw the foreground only, and use
24245 its physical height for clipping. See also draw_glyphs.
24247 Value is the index of a component not in S. */
24249 static int
24250 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24251 int overlaps)
24253 int i;
24254 /* For all glyphs of this composition, starting at the offset
24255 S->cmp_from, until we reach the end of the definition or encounter a
24256 glyph that requires the different face, add it to S. */
24257 struct face *face;
24259 eassert (s);
24261 s->for_overlaps = overlaps;
24262 s->face = NULL;
24263 s->font = NULL;
24264 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24266 int c = COMPOSITION_GLYPH (s->cmp, i);
24268 /* TAB in a composition means display glyphs with padding space
24269 on the left or right. */
24270 if (c != '\t')
24272 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24273 -1, Qnil);
24275 face = get_char_face_and_encoding (s->f, c, face_id,
24276 s->char2b + i, true);
24277 if (face)
24279 if (! s->face)
24281 s->face = face;
24282 s->font = s->face->font;
24284 else if (s->face != face)
24285 break;
24288 ++s->nchars;
24290 s->cmp_to = i;
24292 if (s->face == NULL)
24294 s->face = base_face->ascii_face;
24295 s->font = s->face->font;
24298 /* All glyph strings for the same composition has the same width,
24299 i.e. the width set for the first component of the composition. */
24300 s->width = s->first_glyph->pixel_width;
24302 /* If the specified font could not be loaded, use the frame's
24303 default font, but record the fact that we couldn't load it in
24304 the glyph string so that we can draw rectangles for the
24305 characters of the glyph string. */
24306 if (s->font == NULL)
24308 s->font_not_found_p = true;
24309 s->font = FRAME_FONT (s->f);
24312 /* Adjust base line for subscript/superscript text. */
24313 s->ybase += s->first_glyph->voffset;
24315 return s->cmp_to;
24318 static int
24319 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24320 int start, int end, int overlaps)
24322 struct glyph *glyph, *last;
24323 Lisp_Object lgstring;
24324 int i;
24326 s->for_overlaps = overlaps;
24327 glyph = s->row->glyphs[s->area] + start;
24328 last = s->row->glyphs[s->area] + end;
24329 s->cmp_id = glyph->u.cmp.id;
24330 s->cmp_from = glyph->slice.cmp.from;
24331 s->cmp_to = glyph->slice.cmp.to + 1;
24332 s->face = FACE_FROM_ID (s->f, face_id);
24333 lgstring = composition_gstring_from_id (s->cmp_id);
24334 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24335 glyph++;
24336 while (glyph < last
24337 && glyph->u.cmp.automatic
24338 && glyph->u.cmp.id == s->cmp_id
24339 && s->cmp_to == glyph->slice.cmp.from)
24340 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24342 for (i = s->cmp_from; i < s->cmp_to; i++)
24344 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24345 unsigned code = LGLYPH_CODE (lglyph);
24347 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24349 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24350 return glyph - s->row->glyphs[s->area];
24354 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24355 See the comment of fill_glyph_string for arguments.
24356 Value is the index of the first glyph not in S. */
24359 static int
24360 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24361 int start, int end, int overlaps)
24363 struct glyph *glyph, *last;
24364 int voffset;
24366 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24367 s->for_overlaps = overlaps;
24368 glyph = s->row->glyphs[s->area] + start;
24369 last = s->row->glyphs[s->area] + end;
24370 voffset = glyph->voffset;
24371 s->face = FACE_FROM_ID (s->f, face_id);
24372 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24373 s->nchars = 1;
24374 s->width = glyph->pixel_width;
24375 glyph++;
24376 while (glyph < last
24377 && glyph->type == GLYPHLESS_GLYPH
24378 && glyph->voffset == voffset
24379 && glyph->face_id == face_id)
24381 s->nchars++;
24382 s->width += glyph->pixel_width;
24383 glyph++;
24385 s->ybase += voffset;
24386 return glyph - s->row->glyphs[s->area];
24390 /* Fill glyph string S from a sequence of character glyphs.
24392 FACE_ID is the face id of the string. START is the index of the
24393 first glyph to consider, END is the index of the last + 1.
24394 OVERLAPS non-zero means S should draw the foreground only, and use
24395 its physical height for clipping. See also draw_glyphs.
24397 Value is the index of the first glyph not in S. */
24399 static int
24400 fill_glyph_string (struct glyph_string *s, int face_id,
24401 int start, int end, int overlaps)
24403 struct glyph *glyph, *last;
24404 int voffset;
24405 bool glyph_not_available_p;
24407 eassert (s->f == XFRAME (s->w->frame));
24408 eassert (s->nchars == 0);
24409 eassert (start >= 0 && end > start);
24411 s->for_overlaps = overlaps;
24412 glyph = s->row->glyphs[s->area] + start;
24413 last = s->row->glyphs[s->area] + end;
24414 voffset = glyph->voffset;
24415 s->padding_p = glyph->padding_p;
24416 glyph_not_available_p = glyph->glyph_not_available_p;
24418 while (glyph < last
24419 && glyph->type == CHAR_GLYPH
24420 && glyph->voffset == voffset
24421 /* Same face id implies same font, nowadays. */
24422 && glyph->face_id == face_id
24423 && glyph->glyph_not_available_p == glyph_not_available_p)
24425 s->face = get_glyph_face_and_encoding (s->f, glyph,
24426 s->char2b + s->nchars);
24427 ++s->nchars;
24428 eassert (s->nchars <= end - start);
24429 s->width += glyph->pixel_width;
24430 if (glyph++->padding_p != s->padding_p)
24431 break;
24434 s->font = s->face->font;
24436 /* If the specified font could not be loaded, use the frame's font,
24437 but record the fact that we couldn't load it in
24438 S->font_not_found_p so that we can draw rectangles for the
24439 characters of the glyph string. */
24440 if (s->font == NULL || glyph_not_available_p)
24442 s->font_not_found_p = true;
24443 s->font = FRAME_FONT (s->f);
24446 /* Adjust base line for subscript/superscript text. */
24447 s->ybase += voffset;
24449 eassert (s->face && s->face->gc);
24450 return glyph - s->row->glyphs[s->area];
24454 /* Fill glyph string S from image glyph S->first_glyph. */
24456 static void
24457 fill_image_glyph_string (struct glyph_string *s)
24459 eassert (s->first_glyph->type == IMAGE_GLYPH);
24460 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24461 eassert (s->img);
24462 s->slice = s->first_glyph->slice.img;
24463 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24464 s->font = s->face->font;
24465 s->width = s->first_glyph->pixel_width;
24467 /* Adjust base line for subscript/superscript text. */
24468 s->ybase += s->first_glyph->voffset;
24472 /* Fill glyph string S from a sequence of stretch glyphs.
24474 START is the index of the first glyph to consider,
24475 END is the index of the last + 1.
24477 Value is the index of the first glyph not in S. */
24479 static int
24480 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24482 struct glyph *glyph, *last;
24483 int voffset, face_id;
24485 eassert (s->first_glyph->type == STRETCH_GLYPH);
24487 glyph = s->row->glyphs[s->area] + start;
24488 last = s->row->glyphs[s->area] + end;
24489 face_id = glyph->face_id;
24490 s->face = FACE_FROM_ID (s->f, face_id);
24491 s->font = s->face->font;
24492 s->width = glyph->pixel_width;
24493 s->nchars = 1;
24494 voffset = glyph->voffset;
24496 for (++glyph;
24497 (glyph < last
24498 && glyph->type == STRETCH_GLYPH
24499 && glyph->voffset == voffset
24500 && glyph->face_id == face_id);
24501 ++glyph)
24502 s->width += glyph->pixel_width;
24504 /* Adjust base line for subscript/superscript text. */
24505 s->ybase += voffset;
24507 /* The case that face->gc == 0 is handled when drawing the glyph
24508 string by calling prepare_face_for_display. */
24509 eassert (s->face);
24510 return glyph - s->row->glyphs[s->area];
24513 static struct font_metrics *
24514 get_per_char_metric (struct font *font, XChar2b *char2b)
24516 static struct font_metrics metrics;
24517 unsigned code;
24519 if (! font)
24520 return NULL;
24521 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24522 if (code == FONT_INVALID_CODE)
24523 return NULL;
24524 font->driver->text_extents (font, &code, 1, &metrics);
24525 return &metrics;
24528 /* EXPORT for RIF:
24529 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24530 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24531 assumed to be zero. */
24533 void
24534 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24536 *left = *right = 0;
24538 if (glyph->type == CHAR_GLYPH)
24540 XChar2b char2b;
24541 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24542 if (face->font)
24544 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24545 if (pcm)
24547 if (pcm->rbearing > pcm->width)
24548 *right = pcm->rbearing - pcm->width;
24549 if (pcm->lbearing < 0)
24550 *left = -pcm->lbearing;
24554 else if (glyph->type == COMPOSITE_GLYPH)
24556 if (! glyph->u.cmp.automatic)
24558 struct composition *cmp = composition_table[glyph->u.cmp.id];
24560 if (cmp->rbearing > cmp->pixel_width)
24561 *right = cmp->rbearing - cmp->pixel_width;
24562 if (cmp->lbearing < 0)
24563 *left = - cmp->lbearing;
24565 else
24567 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24568 struct font_metrics metrics;
24570 composition_gstring_width (gstring, glyph->slice.cmp.from,
24571 glyph->slice.cmp.to + 1, &metrics);
24572 if (metrics.rbearing > metrics.width)
24573 *right = metrics.rbearing - metrics.width;
24574 if (metrics.lbearing < 0)
24575 *left = - metrics.lbearing;
24581 /* Return the index of the first glyph preceding glyph string S that
24582 is overwritten by S because of S's left overhang. Value is -1
24583 if no glyphs are overwritten. */
24585 static int
24586 left_overwritten (struct glyph_string *s)
24588 int k;
24590 if (s->left_overhang)
24592 int x = 0, i;
24593 struct glyph *glyphs = s->row->glyphs[s->area];
24594 int first = s->first_glyph - glyphs;
24596 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24597 x -= glyphs[i].pixel_width;
24599 k = i + 1;
24601 else
24602 k = -1;
24604 return k;
24608 /* Return the index of the first glyph preceding glyph string S that
24609 is overwriting S because of its right overhang. Value is -1 if no
24610 glyph in front of S overwrites S. */
24612 static int
24613 left_overwriting (struct glyph_string *s)
24615 int i, k, x;
24616 struct glyph *glyphs = s->row->glyphs[s->area];
24617 int first = s->first_glyph - glyphs;
24619 k = -1;
24620 x = 0;
24621 for (i = first - 1; i >= 0; --i)
24623 int left, right;
24624 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24625 if (x + right > 0)
24626 k = i;
24627 x -= glyphs[i].pixel_width;
24630 return k;
24634 /* Return the index of the last glyph following glyph string S that is
24635 overwritten by S because of S's right overhang. Value is -1 if
24636 no such glyph is found. */
24638 static int
24639 right_overwritten (struct glyph_string *s)
24641 int k = -1;
24643 if (s->right_overhang)
24645 int x = 0, i;
24646 struct glyph *glyphs = s->row->glyphs[s->area];
24647 int first = (s->first_glyph - glyphs
24648 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24649 int end = s->row->used[s->area];
24651 for (i = first; i < end && s->right_overhang > x; ++i)
24652 x += glyphs[i].pixel_width;
24654 k = i;
24657 return k;
24661 /* Return the index of the last glyph following glyph string S that
24662 overwrites S because of its left overhang. Value is negative
24663 if no such glyph is found. */
24665 static int
24666 right_overwriting (struct glyph_string *s)
24668 int i, k, x;
24669 int end = s->row->used[s->area];
24670 struct glyph *glyphs = s->row->glyphs[s->area];
24671 int first = (s->first_glyph - glyphs
24672 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24674 k = -1;
24675 x = 0;
24676 for (i = first; i < end; ++i)
24678 int left, right;
24679 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24680 if (x - left < 0)
24681 k = i;
24682 x += glyphs[i].pixel_width;
24685 return k;
24689 /* Set background width of glyph string S. START is the index of the
24690 first glyph following S. LAST_X is the right-most x-position + 1
24691 in the drawing area. */
24693 static void
24694 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24696 /* If the face of this glyph string has to be drawn to the end of
24697 the drawing area, set S->extends_to_end_of_line_p. */
24699 if (start == s->row->used[s->area]
24700 && ((s->row->fill_line_p
24701 && (s->hl == DRAW_NORMAL_TEXT
24702 || s->hl == DRAW_IMAGE_RAISED
24703 || s->hl == DRAW_IMAGE_SUNKEN))
24704 || s->hl == DRAW_MOUSE_FACE))
24705 s->extends_to_end_of_line_p = true;
24707 /* If S extends its face to the end of the line, set its
24708 background_width to the distance to the right edge of the drawing
24709 area. */
24710 if (s->extends_to_end_of_line_p)
24711 s->background_width = last_x - s->x + 1;
24712 else
24713 s->background_width = s->width;
24717 /* Compute overhangs and x-positions for glyph string S and its
24718 predecessors, or successors. X is the starting x-position for S.
24719 BACKWARD_P means process predecessors. */
24721 static void
24722 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24724 if (backward_p)
24726 while (s)
24728 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24729 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24730 x -= s->width;
24731 s->x = x;
24732 s = s->prev;
24735 else
24737 while (s)
24739 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24740 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24741 s->x = x;
24742 x += s->width;
24743 s = s->next;
24750 /* The following macros are only called from draw_glyphs below.
24751 They reference the following parameters of that function directly:
24752 `w', `row', `area', and `overlap_p'
24753 as well as the following local variables:
24754 `s', `f', and `hdc' (in W32) */
24756 #ifdef HAVE_NTGUI
24757 /* On W32, silently add local `hdc' variable to argument list of
24758 init_glyph_string. */
24759 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24760 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24761 #else
24762 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24763 init_glyph_string (s, char2b, w, row, area, start, hl)
24764 #endif
24766 /* Add a glyph string for a stretch glyph to the list of strings
24767 between HEAD and TAIL. START is the index of the stretch glyph in
24768 row area AREA of glyph row ROW. END is the index of the last glyph
24769 in that glyph row area. X is the current output position assigned
24770 to the new glyph string constructed. HL overrides that face of the
24771 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24772 is the right-most x-position of the drawing area. */
24774 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24775 and below -- keep them on one line. */
24776 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24777 do \
24779 s = alloca (sizeof *s); \
24780 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24781 START = fill_stretch_glyph_string (s, START, END); \
24782 append_glyph_string (&HEAD, &TAIL, s); \
24783 s->x = (X); \
24785 while (false)
24788 /* Add a glyph string for an image glyph to the list of strings
24789 between HEAD and TAIL. START is the index of the image glyph in
24790 row area AREA of glyph row ROW. END is the index of the last glyph
24791 in that glyph row area. X is the current output position assigned
24792 to the new glyph string constructed. HL overrides that face of the
24793 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24794 is the right-most x-position of the drawing area. */
24796 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24797 do \
24799 s = alloca (sizeof *s); \
24800 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24801 fill_image_glyph_string (s); \
24802 append_glyph_string (&HEAD, &TAIL, s); \
24803 ++START; \
24804 s->x = (X); \
24806 while (false)
24809 /* Add a glyph string for a sequence of character glyphs to the list
24810 of strings between HEAD and TAIL. START is the index of the first
24811 glyph in row area AREA of glyph row ROW that is part of the new
24812 glyph string. END is the index of the last glyph in that glyph row
24813 area. X is the current output position assigned to the new glyph
24814 string constructed. HL overrides that face of the glyph; e.g. it
24815 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24816 right-most x-position of the drawing area. */
24818 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24819 do \
24821 int face_id; \
24822 XChar2b *char2b; \
24824 face_id = (row)->glyphs[area][START].face_id; \
24826 s = alloca (sizeof *s); \
24827 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24828 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24829 append_glyph_string (&HEAD, &TAIL, s); \
24830 s->x = (X); \
24831 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24833 while (false)
24836 /* Add a glyph string for a composite sequence to the list of strings
24837 between HEAD and TAIL. START is the index of the first glyph in
24838 row area AREA of glyph row ROW that is part of the new glyph
24839 string. END is the index of the last glyph in that glyph row area.
24840 X is the current output position assigned to the new glyph string
24841 constructed. HL overrides that face of the glyph; e.g. it is
24842 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24843 x-position of the drawing area. */
24845 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24846 do { \
24847 int face_id = (row)->glyphs[area][START].face_id; \
24848 struct face *base_face = FACE_FROM_ID (f, face_id); \
24849 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24850 struct composition *cmp = composition_table[cmp_id]; \
24851 XChar2b *char2b; \
24852 struct glyph_string *first_s = NULL; \
24853 int n; \
24855 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24857 /* Make glyph_strings for each glyph sequence that is drawable by \
24858 the same face, and append them to HEAD/TAIL. */ \
24859 for (n = 0; n < cmp->glyph_len;) \
24861 s = alloca (sizeof *s); \
24862 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24863 append_glyph_string (&(HEAD), &(TAIL), s); \
24864 s->cmp = cmp; \
24865 s->cmp_from = n; \
24866 s->x = (X); \
24867 if (n == 0) \
24868 first_s = s; \
24869 n = fill_composite_glyph_string (s, base_face, overlaps); \
24872 ++START; \
24873 s = first_s; \
24874 } while (false)
24877 /* Add a glyph string for a glyph-string sequence to the list of strings
24878 between HEAD and TAIL. */
24880 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24881 do { \
24882 int face_id; \
24883 XChar2b *char2b; \
24884 Lisp_Object gstring; \
24886 face_id = (row)->glyphs[area][START].face_id; \
24887 gstring = (composition_gstring_from_id \
24888 ((row)->glyphs[area][START].u.cmp.id)); \
24889 s = alloca (sizeof *s); \
24890 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24891 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24892 append_glyph_string (&(HEAD), &(TAIL), s); \
24893 s->x = (X); \
24894 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24895 } while (false)
24898 /* Add a glyph string for a sequence of glyphless character's glyphs
24899 to the list of strings between HEAD and TAIL. The meanings of
24900 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24902 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24903 do \
24905 int face_id; \
24907 face_id = (row)->glyphs[area][START].face_id; \
24909 s = alloca (sizeof *s); \
24910 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24911 append_glyph_string (&HEAD, &TAIL, s); \
24912 s->x = (X); \
24913 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24914 overlaps); \
24916 while (false)
24919 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24920 of AREA of glyph row ROW on window W between indices START and END.
24921 HL overrides the face for drawing glyph strings, e.g. it is
24922 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24923 x-positions of the drawing area.
24925 This is an ugly monster macro construct because we must use alloca
24926 to allocate glyph strings (because draw_glyphs can be called
24927 asynchronously). */
24929 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24930 do \
24932 HEAD = TAIL = NULL; \
24933 while (START < END) \
24935 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24936 switch (first_glyph->type) \
24938 case CHAR_GLYPH: \
24939 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24940 HL, X, LAST_X); \
24941 break; \
24943 case COMPOSITE_GLYPH: \
24944 if (first_glyph->u.cmp.automatic) \
24945 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24946 HL, X, LAST_X); \
24947 else \
24948 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24949 HL, X, LAST_X); \
24950 break; \
24952 case STRETCH_GLYPH: \
24953 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24954 HL, X, LAST_X); \
24955 break; \
24957 case IMAGE_GLYPH: \
24958 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24959 HL, X, LAST_X); \
24960 break; \
24962 case GLYPHLESS_GLYPH: \
24963 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24964 HL, X, LAST_X); \
24965 break; \
24967 default: \
24968 emacs_abort (); \
24971 if (s) \
24973 set_glyph_string_background_width (s, START, LAST_X); \
24974 (X) += s->width; \
24977 } while (false)
24980 /* Draw glyphs between START and END in AREA of ROW on window W,
24981 starting at x-position X. X is relative to AREA in W. HL is a
24982 face-override with the following meaning:
24984 DRAW_NORMAL_TEXT draw normally
24985 DRAW_CURSOR draw in cursor face
24986 DRAW_MOUSE_FACE draw in mouse face.
24987 DRAW_INVERSE_VIDEO draw in mode line face
24988 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24989 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24991 If OVERLAPS is non-zero, draw only the foreground of characters and
24992 clip to the physical height of ROW. Non-zero value also defines
24993 the overlapping part to be drawn:
24995 OVERLAPS_PRED overlap with preceding rows
24996 OVERLAPS_SUCC overlap with succeeding rows
24997 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24998 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25000 Value is the x-position reached, relative to AREA of W. */
25002 static int
25003 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25004 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25005 enum draw_glyphs_face hl, int overlaps)
25007 struct glyph_string *head, *tail;
25008 struct glyph_string *s;
25009 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25010 int i, j, x_reached, last_x, area_left = 0;
25011 struct frame *f = XFRAME (WINDOW_FRAME (w));
25012 DECLARE_HDC (hdc);
25014 ALLOCATE_HDC (hdc, f);
25016 /* Let's rather be paranoid than getting a SEGV. */
25017 end = min (end, row->used[area]);
25018 start = clip_to_bounds (0, start, end);
25020 /* Translate X to frame coordinates. Set last_x to the right
25021 end of the drawing area. */
25022 if (row->full_width_p)
25024 /* X is relative to the left edge of W, without scroll bars
25025 or fringes. */
25026 area_left = WINDOW_LEFT_EDGE_X (w);
25027 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25028 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25030 else
25032 area_left = window_box_left (w, area);
25033 last_x = area_left + window_box_width (w, area);
25035 x += area_left;
25037 /* Build a doubly-linked list of glyph_string structures between
25038 head and tail from what we have to draw. Note that the macro
25039 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25040 the reason we use a separate variable `i'. */
25041 i = start;
25042 USE_SAFE_ALLOCA;
25043 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25044 if (tail)
25045 x_reached = tail->x + tail->background_width;
25046 else
25047 x_reached = x;
25049 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25050 the row, redraw some glyphs in front or following the glyph
25051 strings built above. */
25052 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25054 struct glyph_string *h, *t;
25055 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25056 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25057 bool check_mouse_face = false;
25058 int dummy_x = 0;
25060 /* If mouse highlighting is on, we may need to draw adjacent
25061 glyphs using mouse-face highlighting. */
25062 if (area == TEXT_AREA && row->mouse_face_p
25063 && hlinfo->mouse_face_beg_row >= 0
25064 && hlinfo->mouse_face_end_row >= 0)
25066 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25068 if (row_vpos >= hlinfo->mouse_face_beg_row
25069 && row_vpos <= hlinfo->mouse_face_end_row)
25071 check_mouse_face = true;
25072 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25073 ? hlinfo->mouse_face_beg_col : 0;
25074 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25075 ? hlinfo->mouse_face_end_col
25076 : row->used[TEXT_AREA];
25080 /* Compute overhangs for all glyph strings. */
25081 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25082 for (s = head; s; s = s->next)
25083 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25085 /* Prepend glyph strings for glyphs in front of the first glyph
25086 string that are overwritten because of the first glyph
25087 string's left overhang. The background of all strings
25088 prepended must be drawn because the first glyph string
25089 draws over it. */
25090 i = left_overwritten (head);
25091 if (i >= 0)
25093 enum draw_glyphs_face overlap_hl;
25095 /* If this row contains mouse highlighting, attempt to draw
25096 the overlapped glyphs with the correct highlight. This
25097 code fails if the overlap encompasses more than one glyph
25098 and mouse-highlight spans only some of these glyphs.
25099 However, making it work perfectly involves a lot more
25100 code, and I don't know if the pathological case occurs in
25101 practice, so we'll stick to this for now. --- cyd */
25102 if (check_mouse_face
25103 && mouse_beg_col < start && mouse_end_col > i)
25104 overlap_hl = DRAW_MOUSE_FACE;
25105 else
25106 overlap_hl = DRAW_NORMAL_TEXT;
25108 if (hl != overlap_hl)
25109 clip_head = head;
25110 j = i;
25111 BUILD_GLYPH_STRINGS (j, start, h, t,
25112 overlap_hl, dummy_x, last_x);
25113 start = i;
25114 compute_overhangs_and_x (t, head->x, true);
25115 prepend_glyph_string_lists (&head, &tail, h, t);
25116 if (clip_head == NULL)
25117 clip_head = head;
25120 /* Prepend glyph strings for glyphs in front of the first glyph
25121 string that overwrite that glyph string because of their
25122 right overhang. For these strings, only the foreground must
25123 be drawn, because it draws over the glyph string at `head'.
25124 The background must not be drawn because this would overwrite
25125 right overhangs of preceding glyphs for which no glyph
25126 strings exist. */
25127 i = left_overwriting (head);
25128 if (i >= 0)
25130 enum draw_glyphs_face overlap_hl;
25132 if (check_mouse_face
25133 && mouse_beg_col < start && mouse_end_col > i)
25134 overlap_hl = DRAW_MOUSE_FACE;
25135 else
25136 overlap_hl = DRAW_NORMAL_TEXT;
25138 if (hl == overlap_hl || clip_head == NULL)
25139 clip_head = head;
25140 BUILD_GLYPH_STRINGS (i, start, h, t,
25141 overlap_hl, dummy_x, last_x);
25142 for (s = h; s; s = s->next)
25143 s->background_filled_p = true;
25144 compute_overhangs_and_x (t, head->x, true);
25145 prepend_glyph_string_lists (&head, &tail, h, t);
25148 /* Append glyphs strings for glyphs following the last glyph
25149 string tail that are overwritten by tail. The background of
25150 these strings has to be drawn because tail's foreground draws
25151 over it. */
25152 i = right_overwritten (tail);
25153 if (i >= 0)
25155 enum draw_glyphs_face overlap_hl;
25157 if (check_mouse_face
25158 && mouse_beg_col < i && mouse_end_col > end)
25159 overlap_hl = DRAW_MOUSE_FACE;
25160 else
25161 overlap_hl = DRAW_NORMAL_TEXT;
25163 if (hl != overlap_hl)
25164 clip_tail = tail;
25165 BUILD_GLYPH_STRINGS (end, i, h, t,
25166 overlap_hl, x, last_x);
25167 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25168 we don't have `end = i;' here. */
25169 compute_overhangs_and_x (h, tail->x + tail->width, false);
25170 append_glyph_string_lists (&head, &tail, h, t);
25171 if (clip_tail == NULL)
25172 clip_tail = tail;
25175 /* Append glyph strings for glyphs following the last glyph
25176 string tail that overwrite tail. The foreground of such
25177 glyphs has to be drawn because it writes into the background
25178 of tail. The background must not be drawn because it could
25179 paint over the foreground of following glyphs. */
25180 i = right_overwriting (tail);
25181 if (i >= 0)
25183 enum draw_glyphs_face overlap_hl;
25184 if (check_mouse_face
25185 && mouse_beg_col < i && mouse_end_col > end)
25186 overlap_hl = DRAW_MOUSE_FACE;
25187 else
25188 overlap_hl = DRAW_NORMAL_TEXT;
25190 if (hl == overlap_hl || clip_tail == NULL)
25191 clip_tail = tail;
25192 i++; /* We must include the Ith glyph. */
25193 BUILD_GLYPH_STRINGS (end, i, h, t,
25194 overlap_hl, x, last_x);
25195 for (s = h; s; s = s->next)
25196 s->background_filled_p = true;
25197 compute_overhangs_and_x (h, tail->x + tail->width, false);
25198 append_glyph_string_lists (&head, &tail, h, t);
25200 if (clip_head || clip_tail)
25201 for (s = head; s; s = s->next)
25203 s->clip_head = clip_head;
25204 s->clip_tail = clip_tail;
25208 /* Draw all strings. */
25209 for (s = head; s; s = s->next)
25210 FRAME_RIF (f)->draw_glyph_string (s);
25212 #ifndef HAVE_NS
25213 /* When focus a sole frame and move horizontally, this clears on_p
25214 causing a failure to erase prev cursor position. */
25215 if (area == TEXT_AREA
25216 && !row->full_width_p
25217 /* When drawing overlapping rows, only the glyph strings'
25218 foreground is drawn, which doesn't erase a cursor
25219 completely. */
25220 && !overlaps)
25222 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25223 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25224 : (tail ? tail->x + tail->background_width : x));
25225 x0 -= area_left;
25226 x1 -= area_left;
25228 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25229 row->y, MATRIX_ROW_BOTTOM_Y (row));
25231 #endif
25233 /* Value is the x-position up to which drawn, relative to AREA of W.
25234 This doesn't include parts drawn because of overhangs. */
25235 if (row->full_width_p)
25236 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25237 else
25238 x_reached -= area_left;
25240 RELEASE_HDC (hdc, f);
25242 SAFE_FREE ();
25243 return x_reached;
25246 /* Expand row matrix if too narrow. Don't expand if area
25247 is not present. */
25249 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25251 if (!it->f->fonts_changed \
25252 && (it->glyph_row->glyphs[area] \
25253 < it->glyph_row->glyphs[area + 1])) \
25255 it->w->ncols_scale_factor++; \
25256 it->f->fonts_changed = true; \
25260 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25261 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25263 static void
25264 append_glyph (struct it *it)
25266 struct glyph *glyph;
25267 enum glyph_row_area area = it->area;
25269 eassert (it->glyph_row);
25270 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25272 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25273 if (glyph < it->glyph_row->glyphs[area + 1])
25275 /* If the glyph row is reversed, we need to prepend the glyph
25276 rather than append it. */
25277 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25279 struct glyph *g;
25281 /* Make room for the additional glyph. */
25282 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25283 g[1] = *g;
25284 glyph = it->glyph_row->glyphs[area];
25286 glyph->charpos = CHARPOS (it->position);
25287 glyph->object = it->object;
25288 if (it->pixel_width > 0)
25290 glyph->pixel_width = it->pixel_width;
25291 glyph->padding_p = false;
25293 else
25295 /* Assure at least 1-pixel width. Otherwise, cursor can't
25296 be displayed correctly. */
25297 glyph->pixel_width = 1;
25298 glyph->padding_p = true;
25300 glyph->ascent = it->ascent;
25301 glyph->descent = it->descent;
25302 glyph->voffset = it->voffset;
25303 glyph->type = CHAR_GLYPH;
25304 glyph->avoid_cursor_p = it->avoid_cursor_p;
25305 glyph->multibyte_p = it->multibyte_p;
25306 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25308 /* In R2L rows, the left and the right box edges need to be
25309 drawn in reverse direction. */
25310 glyph->right_box_line_p = it->start_of_box_run_p;
25311 glyph->left_box_line_p = it->end_of_box_run_p;
25313 else
25315 glyph->left_box_line_p = it->start_of_box_run_p;
25316 glyph->right_box_line_p = it->end_of_box_run_p;
25318 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25319 || it->phys_descent > it->descent);
25320 glyph->glyph_not_available_p = it->glyph_not_available_p;
25321 glyph->face_id = it->face_id;
25322 glyph->u.ch = it->char_to_display;
25323 glyph->slice.img = null_glyph_slice;
25324 glyph->font_type = FONT_TYPE_UNKNOWN;
25325 if (it->bidi_p)
25327 glyph->resolved_level = it->bidi_it.resolved_level;
25328 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25329 glyph->bidi_type = it->bidi_it.type;
25331 else
25333 glyph->resolved_level = 0;
25334 glyph->bidi_type = UNKNOWN_BT;
25336 ++it->glyph_row->used[area];
25338 else
25339 IT_EXPAND_MATRIX_WIDTH (it, area);
25342 /* Store one glyph for the composition IT->cmp_it.id in
25343 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25344 non-null. */
25346 static void
25347 append_composite_glyph (struct it *it)
25349 struct glyph *glyph;
25350 enum glyph_row_area area = it->area;
25352 eassert (it->glyph_row);
25354 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25355 if (glyph < it->glyph_row->glyphs[area + 1])
25357 /* If the glyph row is reversed, we need to prepend the glyph
25358 rather than append it. */
25359 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25361 struct glyph *g;
25363 /* Make room for the new glyph. */
25364 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25365 g[1] = *g;
25366 glyph = it->glyph_row->glyphs[it->area];
25368 glyph->charpos = it->cmp_it.charpos;
25369 glyph->object = it->object;
25370 glyph->pixel_width = it->pixel_width;
25371 glyph->ascent = it->ascent;
25372 glyph->descent = it->descent;
25373 glyph->voffset = it->voffset;
25374 glyph->type = COMPOSITE_GLYPH;
25375 if (it->cmp_it.ch < 0)
25377 glyph->u.cmp.automatic = false;
25378 glyph->u.cmp.id = it->cmp_it.id;
25379 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25381 else
25383 glyph->u.cmp.automatic = true;
25384 glyph->u.cmp.id = it->cmp_it.id;
25385 glyph->slice.cmp.from = it->cmp_it.from;
25386 glyph->slice.cmp.to = it->cmp_it.to - 1;
25388 glyph->avoid_cursor_p = it->avoid_cursor_p;
25389 glyph->multibyte_p = it->multibyte_p;
25390 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25392 /* In R2L rows, the left and the right box edges need to be
25393 drawn in reverse direction. */
25394 glyph->right_box_line_p = it->start_of_box_run_p;
25395 glyph->left_box_line_p = it->end_of_box_run_p;
25397 else
25399 glyph->left_box_line_p = it->start_of_box_run_p;
25400 glyph->right_box_line_p = it->end_of_box_run_p;
25402 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25403 || it->phys_descent > it->descent);
25404 glyph->padding_p = false;
25405 glyph->glyph_not_available_p = false;
25406 glyph->face_id = it->face_id;
25407 glyph->font_type = FONT_TYPE_UNKNOWN;
25408 if (it->bidi_p)
25410 glyph->resolved_level = it->bidi_it.resolved_level;
25411 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25412 glyph->bidi_type = it->bidi_it.type;
25414 ++it->glyph_row->used[area];
25416 else
25417 IT_EXPAND_MATRIX_WIDTH (it, area);
25421 /* Change IT->ascent and IT->height according to the setting of
25422 IT->voffset. */
25424 static void
25425 take_vertical_position_into_account (struct it *it)
25427 if (it->voffset)
25429 if (it->voffset < 0)
25430 /* Increase the ascent so that we can display the text higher
25431 in the line. */
25432 it->ascent -= it->voffset;
25433 else
25434 /* Increase the descent so that we can display the text lower
25435 in the line. */
25436 it->descent += it->voffset;
25441 /* Produce glyphs/get display metrics for the image IT is loaded with.
25442 See the description of struct display_iterator in dispextern.h for
25443 an overview of struct display_iterator. */
25445 static void
25446 produce_image_glyph (struct it *it)
25448 struct image *img;
25449 struct face *face;
25450 int glyph_ascent, crop;
25451 struct glyph_slice slice;
25453 eassert (it->what == IT_IMAGE);
25455 face = FACE_FROM_ID (it->f, it->face_id);
25456 eassert (face);
25457 /* Make sure X resources of the face is loaded. */
25458 prepare_face_for_display (it->f, face);
25460 if (it->image_id < 0)
25462 /* Fringe bitmap. */
25463 it->ascent = it->phys_ascent = 0;
25464 it->descent = it->phys_descent = 0;
25465 it->pixel_width = 0;
25466 it->nglyphs = 0;
25467 return;
25470 img = IMAGE_FROM_ID (it->f, it->image_id);
25471 eassert (img);
25472 /* Make sure X resources of the image is loaded. */
25473 prepare_image_for_display (it->f, img);
25475 slice.x = slice.y = 0;
25476 slice.width = img->width;
25477 slice.height = img->height;
25479 if (INTEGERP (it->slice.x))
25480 slice.x = XINT (it->slice.x);
25481 else if (FLOATP (it->slice.x))
25482 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25484 if (INTEGERP (it->slice.y))
25485 slice.y = XINT (it->slice.y);
25486 else if (FLOATP (it->slice.y))
25487 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25489 if (INTEGERP (it->slice.width))
25490 slice.width = XINT (it->slice.width);
25491 else if (FLOATP (it->slice.width))
25492 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25494 if (INTEGERP (it->slice.height))
25495 slice.height = XINT (it->slice.height);
25496 else if (FLOATP (it->slice.height))
25497 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25499 if (slice.x >= img->width)
25500 slice.x = img->width;
25501 if (slice.y >= img->height)
25502 slice.y = img->height;
25503 if (slice.x + slice.width >= img->width)
25504 slice.width = img->width - slice.x;
25505 if (slice.y + slice.height > img->height)
25506 slice.height = img->height - slice.y;
25508 if (slice.width == 0 || slice.height == 0)
25509 return;
25511 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25513 it->descent = slice.height - glyph_ascent;
25514 if (slice.y == 0)
25515 it->descent += img->vmargin;
25516 if (slice.y + slice.height == img->height)
25517 it->descent += img->vmargin;
25518 it->phys_descent = it->descent;
25520 it->pixel_width = slice.width;
25521 if (slice.x == 0)
25522 it->pixel_width += img->hmargin;
25523 if (slice.x + slice.width == img->width)
25524 it->pixel_width += img->hmargin;
25526 /* It's quite possible for images to have an ascent greater than
25527 their height, so don't get confused in that case. */
25528 if (it->descent < 0)
25529 it->descent = 0;
25531 it->nglyphs = 1;
25533 if (face->box != FACE_NO_BOX)
25535 if (face->box_line_width > 0)
25537 if (slice.y == 0)
25538 it->ascent += face->box_line_width;
25539 if (slice.y + slice.height == img->height)
25540 it->descent += face->box_line_width;
25543 if (it->start_of_box_run_p && slice.x == 0)
25544 it->pixel_width += eabs (face->box_line_width);
25545 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25546 it->pixel_width += eabs (face->box_line_width);
25549 take_vertical_position_into_account (it);
25551 /* Automatically crop wide image glyphs at right edge so we can
25552 draw the cursor on same display row. */
25553 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25554 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25556 it->pixel_width -= crop;
25557 slice.width -= crop;
25560 if (it->glyph_row)
25562 struct glyph *glyph;
25563 enum glyph_row_area area = it->area;
25565 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25566 if (it->glyph_row->reversed_p)
25568 struct glyph *g;
25570 /* Make room for the new glyph. */
25571 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25572 g[1] = *g;
25573 glyph = it->glyph_row->glyphs[it->area];
25575 if (glyph < it->glyph_row->glyphs[area + 1])
25577 glyph->charpos = CHARPOS (it->position);
25578 glyph->object = it->object;
25579 glyph->pixel_width = it->pixel_width;
25580 glyph->ascent = glyph_ascent;
25581 glyph->descent = it->descent;
25582 glyph->voffset = it->voffset;
25583 glyph->type = IMAGE_GLYPH;
25584 glyph->avoid_cursor_p = it->avoid_cursor_p;
25585 glyph->multibyte_p = it->multibyte_p;
25586 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25588 /* In R2L rows, the left and the right box edges need to be
25589 drawn in reverse direction. */
25590 glyph->right_box_line_p = it->start_of_box_run_p;
25591 glyph->left_box_line_p = it->end_of_box_run_p;
25593 else
25595 glyph->left_box_line_p = it->start_of_box_run_p;
25596 glyph->right_box_line_p = it->end_of_box_run_p;
25598 glyph->overlaps_vertically_p = false;
25599 glyph->padding_p = false;
25600 glyph->glyph_not_available_p = false;
25601 glyph->face_id = it->face_id;
25602 glyph->u.img_id = img->id;
25603 glyph->slice.img = slice;
25604 glyph->font_type = FONT_TYPE_UNKNOWN;
25605 if (it->bidi_p)
25607 glyph->resolved_level = it->bidi_it.resolved_level;
25608 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25609 glyph->bidi_type = it->bidi_it.type;
25611 ++it->glyph_row->used[area];
25613 else
25614 IT_EXPAND_MATRIX_WIDTH (it, area);
25619 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25620 of the glyph, WIDTH and HEIGHT are the width and height of the
25621 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25623 static void
25624 append_stretch_glyph (struct it *it, Lisp_Object object,
25625 int width, int height, int ascent)
25627 struct glyph *glyph;
25628 enum glyph_row_area area = it->area;
25630 eassert (ascent >= 0 && ascent <= height);
25632 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25633 if (glyph < it->glyph_row->glyphs[area + 1])
25635 /* If the glyph row is reversed, we need to prepend the glyph
25636 rather than append it. */
25637 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25639 struct glyph *g;
25641 /* Make room for the additional glyph. */
25642 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25643 g[1] = *g;
25644 glyph = it->glyph_row->glyphs[area];
25646 /* Decrease the width of the first glyph of the row that
25647 begins before first_visible_x (e.g., due to hscroll).
25648 This is so the overall width of the row becomes smaller
25649 by the scroll amount, and the stretch glyph appended by
25650 extend_face_to_end_of_line will be wider, to shift the
25651 row glyphs to the right. (In L2R rows, the corresponding
25652 left-shift effect is accomplished by setting row->x to a
25653 negative value, which won't work with R2L rows.)
25655 This must leave us with a positive value of WIDTH, since
25656 otherwise the call to move_it_in_display_line_to at the
25657 beginning of display_line would have got past the entire
25658 first glyph, and then it->current_x would have been
25659 greater or equal to it->first_visible_x. */
25660 if (it->current_x < it->first_visible_x)
25661 width -= it->first_visible_x - it->current_x;
25662 eassert (width > 0);
25664 glyph->charpos = CHARPOS (it->position);
25665 glyph->object = object;
25666 glyph->pixel_width = width;
25667 glyph->ascent = ascent;
25668 glyph->descent = height - ascent;
25669 glyph->voffset = it->voffset;
25670 glyph->type = STRETCH_GLYPH;
25671 glyph->avoid_cursor_p = it->avoid_cursor_p;
25672 glyph->multibyte_p = it->multibyte_p;
25673 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25675 /* In R2L rows, the left and the right box edges need to be
25676 drawn in reverse direction. */
25677 glyph->right_box_line_p = it->start_of_box_run_p;
25678 glyph->left_box_line_p = it->end_of_box_run_p;
25680 else
25682 glyph->left_box_line_p = it->start_of_box_run_p;
25683 glyph->right_box_line_p = it->end_of_box_run_p;
25685 glyph->overlaps_vertically_p = false;
25686 glyph->padding_p = false;
25687 glyph->glyph_not_available_p = false;
25688 glyph->face_id = it->face_id;
25689 glyph->u.stretch.ascent = ascent;
25690 glyph->u.stretch.height = height;
25691 glyph->slice.img = null_glyph_slice;
25692 glyph->font_type = FONT_TYPE_UNKNOWN;
25693 if (it->bidi_p)
25695 glyph->resolved_level = it->bidi_it.resolved_level;
25696 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25697 glyph->bidi_type = it->bidi_it.type;
25699 else
25701 glyph->resolved_level = 0;
25702 glyph->bidi_type = UNKNOWN_BT;
25704 ++it->glyph_row->used[area];
25706 else
25707 IT_EXPAND_MATRIX_WIDTH (it, area);
25710 #endif /* HAVE_WINDOW_SYSTEM */
25712 /* Produce a stretch glyph for iterator IT. IT->object is the value
25713 of the glyph property displayed. The value must be a list
25714 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25715 being recognized:
25717 1. `:width WIDTH' specifies that the space should be WIDTH *
25718 canonical char width wide. WIDTH may be an integer or floating
25719 point number.
25721 2. `:relative-width FACTOR' specifies that the width of the stretch
25722 should be computed from the width of the first character having the
25723 `glyph' property, and should be FACTOR times that width.
25725 3. `:align-to HPOS' specifies that the space should be wide enough
25726 to reach HPOS, a value in canonical character units.
25728 Exactly one of the above pairs must be present.
25730 4. `:height HEIGHT' specifies that the height of the stretch produced
25731 should be HEIGHT, measured in canonical character units.
25733 5. `:relative-height FACTOR' specifies that the height of the
25734 stretch should be FACTOR times the height of the characters having
25735 the glyph property.
25737 Either none or exactly one of 4 or 5 must be present.
25739 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25740 of the stretch should be used for the ascent of the stretch.
25741 ASCENT must be in the range 0 <= ASCENT <= 100. */
25743 void
25744 produce_stretch_glyph (struct it *it)
25746 /* (space :width WIDTH :height HEIGHT ...) */
25747 Lisp_Object prop, plist;
25748 int width = 0, height = 0, align_to = -1;
25749 bool zero_width_ok_p = false;
25750 double tem;
25751 struct font *font = NULL;
25753 #ifdef HAVE_WINDOW_SYSTEM
25754 int ascent = 0;
25755 bool zero_height_ok_p = false;
25757 if (FRAME_WINDOW_P (it->f))
25759 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25760 font = face->font ? face->font : FRAME_FONT (it->f);
25761 prepare_face_for_display (it->f, face);
25763 #endif
25765 /* List should start with `space'. */
25766 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25767 plist = XCDR (it->object);
25769 /* Compute the width of the stretch. */
25770 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25771 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
25773 /* Absolute width `:width WIDTH' specified and valid. */
25774 zero_width_ok_p = true;
25775 width = (int)tem;
25777 #ifdef HAVE_WINDOW_SYSTEM
25778 else if (FRAME_WINDOW_P (it->f)
25779 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25781 /* Relative width `:relative-width FACTOR' specified and valid.
25782 Compute the width of the characters having the `glyph'
25783 property. */
25784 struct it it2;
25785 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25787 it2 = *it;
25788 if (it->multibyte_p)
25789 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25790 else
25792 it2.c = it2.char_to_display = *p, it2.len = 1;
25793 if (! ASCII_CHAR_P (it2.c))
25794 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25797 it2.glyph_row = NULL;
25798 it2.what = IT_CHARACTER;
25799 x_produce_glyphs (&it2);
25800 width = NUMVAL (prop) * it2.pixel_width;
25802 #endif /* HAVE_WINDOW_SYSTEM */
25803 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25804 && calc_pixel_width_or_height (&tem, it, prop, font, true,
25805 &align_to))
25807 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25808 align_to = (align_to < 0
25810 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25811 else if (align_to < 0)
25812 align_to = window_box_left_offset (it->w, TEXT_AREA);
25813 width = max (0, (int)tem + align_to - it->current_x);
25814 zero_width_ok_p = true;
25816 else
25817 /* Nothing specified -> width defaults to canonical char width. */
25818 width = FRAME_COLUMN_WIDTH (it->f);
25820 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25821 width = 1;
25823 #ifdef HAVE_WINDOW_SYSTEM
25824 /* Compute height. */
25825 if (FRAME_WINDOW_P (it->f))
25827 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25828 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25830 height = (int)tem;
25831 zero_height_ok_p = true;
25833 else if (prop = Fplist_get (plist, QCrelative_height),
25834 NUMVAL (prop) > 0)
25835 height = FONT_HEIGHT (font) * NUMVAL (prop);
25836 else
25837 height = FONT_HEIGHT (font);
25839 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25840 height = 1;
25842 /* Compute percentage of height used for ascent. If
25843 `:ascent ASCENT' is present and valid, use that. Otherwise,
25844 derive the ascent from the font in use. */
25845 if (prop = Fplist_get (plist, QCascent),
25846 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25847 ascent = height * NUMVAL (prop) / 100.0;
25848 else if (!NILP (prop)
25849 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25850 ascent = min (max (0, (int)tem), height);
25851 else
25852 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25854 else
25855 #endif /* HAVE_WINDOW_SYSTEM */
25856 height = 1;
25858 if (width > 0 && it->line_wrap != TRUNCATE
25859 && it->current_x + width > it->last_visible_x)
25861 width = it->last_visible_x - it->current_x;
25862 #ifdef HAVE_WINDOW_SYSTEM
25863 /* Subtract one more pixel from the stretch width, but only on
25864 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25865 width -= FRAME_WINDOW_P (it->f);
25866 #endif
25869 if (width > 0 && height > 0 && it->glyph_row)
25871 Lisp_Object o_object = it->object;
25872 Lisp_Object object = it->stack[it->sp - 1].string;
25873 int n = width;
25875 if (!STRINGP (object))
25876 object = it->w->contents;
25877 #ifdef HAVE_WINDOW_SYSTEM
25878 if (FRAME_WINDOW_P (it->f))
25879 append_stretch_glyph (it, object, width, height, ascent);
25880 else
25881 #endif
25883 it->object = object;
25884 it->char_to_display = ' ';
25885 it->pixel_width = it->len = 1;
25886 while (n--)
25887 tty_append_glyph (it);
25888 it->object = o_object;
25892 it->pixel_width = width;
25893 #ifdef HAVE_WINDOW_SYSTEM
25894 if (FRAME_WINDOW_P (it->f))
25896 it->ascent = it->phys_ascent = ascent;
25897 it->descent = it->phys_descent = height - it->ascent;
25898 it->nglyphs = width > 0 && height > 0;
25899 take_vertical_position_into_account (it);
25901 else
25902 #endif
25903 it->nglyphs = width;
25906 /* Get information about special display element WHAT in an
25907 environment described by IT. WHAT is one of IT_TRUNCATION or
25908 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25909 non-null glyph_row member. This function ensures that fields like
25910 face_id, c, len of IT are left untouched. */
25912 static void
25913 produce_special_glyphs (struct it *it, enum display_element_type what)
25915 struct it temp_it;
25916 Lisp_Object gc;
25917 GLYPH glyph;
25919 temp_it = *it;
25920 temp_it.object = Qnil;
25921 memset (&temp_it.current, 0, sizeof temp_it.current);
25923 if (what == IT_CONTINUATION)
25925 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25926 if (it->bidi_it.paragraph_dir == R2L)
25927 SET_GLYPH_FROM_CHAR (glyph, '/');
25928 else
25929 SET_GLYPH_FROM_CHAR (glyph, '\\');
25930 if (it->dp
25931 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25933 /* FIXME: Should we mirror GC for R2L lines? */
25934 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25935 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25938 else if (what == IT_TRUNCATION)
25940 /* Truncation glyph. */
25941 SET_GLYPH_FROM_CHAR (glyph, '$');
25942 if (it->dp
25943 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25945 /* FIXME: Should we mirror GC for R2L lines? */
25946 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25947 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25950 else
25951 emacs_abort ();
25953 #ifdef HAVE_WINDOW_SYSTEM
25954 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25955 is turned off, we precede the truncation/continuation glyphs by a
25956 stretch glyph whose width is computed such that these special
25957 glyphs are aligned at the window margin, even when very different
25958 fonts are used in different glyph rows. */
25959 if (FRAME_WINDOW_P (temp_it.f)
25960 /* init_iterator calls this with it->glyph_row == NULL, and it
25961 wants only the pixel width of the truncation/continuation
25962 glyphs. */
25963 && temp_it.glyph_row
25964 /* insert_left_trunc_glyphs calls us at the beginning of the
25965 row, and it has its own calculation of the stretch glyph
25966 width. */
25967 && temp_it.glyph_row->used[TEXT_AREA] > 0
25968 && (temp_it.glyph_row->reversed_p
25969 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25970 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25972 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25974 if (stretch_width > 0)
25976 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25977 struct font *font =
25978 face->font ? face->font : FRAME_FONT (temp_it.f);
25979 int stretch_ascent =
25980 (((temp_it.ascent + temp_it.descent)
25981 * FONT_BASE (font)) / FONT_HEIGHT (font));
25983 append_stretch_glyph (&temp_it, Qnil, stretch_width,
25984 temp_it.ascent + temp_it.descent,
25985 stretch_ascent);
25988 #endif
25990 temp_it.dp = NULL;
25991 temp_it.what = IT_CHARACTER;
25992 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25993 temp_it.face_id = GLYPH_FACE (glyph);
25994 temp_it.len = CHAR_BYTES (temp_it.c);
25996 PRODUCE_GLYPHS (&temp_it);
25997 it->pixel_width = temp_it.pixel_width;
25998 it->nglyphs = temp_it.nglyphs;
26001 #ifdef HAVE_WINDOW_SYSTEM
26003 /* Calculate line-height and line-spacing properties.
26004 An integer value specifies explicit pixel value.
26005 A float value specifies relative value to current face height.
26006 A cons (float . face-name) specifies relative value to
26007 height of specified face font.
26009 Returns height in pixels, or nil. */
26011 static Lisp_Object
26012 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26013 int boff, bool override)
26015 Lisp_Object face_name = Qnil;
26016 int ascent, descent, height;
26018 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26019 return val;
26021 if (CONSP (val))
26023 face_name = XCAR (val);
26024 val = XCDR (val);
26025 if (!NUMBERP (val))
26026 val = make_number (1);
26027 if (NILP (face_name))
26029 height = it->ascent + it->descent;
26030 goto scale;
26034 if (NILP (face_name))
26036 font = FRAME_FONT (it->f);
26037 boff = FRAME_BASELINE_OFFSET (it->f);
26039 else if (EQ (face_name, Qt))
26041 override = false;
26043 else
26045 int face_id;
26046 struct face *face;
26048 face_id = lookup_named_face (it->f, face_name, false);
26049 if (face_id < 0)
26050 return make_number (-1);
26052 face = FACE_FROM_ID (it->f, face_id);
26053 font = face->font;
26054 if (font == NULL)
26055 return make_number (-1);
26056 boff = font->baseline_offset;
26057 if (font->vertical_centering)
26058 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26061 ascent = FONT_BASE (font) + boff;
26062 descent = FONT_DESCENT (font) - boff;
26064 if (override)
26066 it->override_ascent = ascent;
26067 it->override_descent = descent;
26068 it->override_boff = boff;
26071 height = ascent + descent;
26073 scale:
26074 if (FLOATP (val))
26075 height = (int)(XFLOAT_DATA (val) * height);
26076 else if (INTEGERP (val))
26077 height *= XINT (val);
26079 return make_number (height);
26083 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26084 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26085 and only if this is for a character for which no font was found.
26087 If the display method (it->glyphless_method) is
26088 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26089 length of the acronym or the hexadecimal string, UPPER_XOFF and
26090 UPPER_YOFF are pixel offsets for the upper part of the string,
26091 LOWER_XOFF and LOWER_YOFF are for the lower part.
26093 For the other display methods, LEN through LOWER_YOFF are zero. */
26095 static void
26096 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26097 short upper_xoff, short upper_yoff,
26098 short lower_xoff, short lower_yoff)
26100 struct glyph *glyph;
26101 enum glyph_row_area area = it->area;
26103 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26104 if (glyph < it->glyph_row->glyphs[area + 1])
26106 /* If the glyph row is reversed, we need to prepend the glyph
26107 rather than append it. */
26108 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26110 struct glyph *g;
26112 /* Make room for the additional glyph. */
26113 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26114 g[1] = *g;
26115 glyph = it->glyph_row->glyphs[area];
26117 glyph->charpos = CHARPOS (it->position);
26118 glyph->object = it->object;
26119 glyph->pixel_width = it->pixel_width;
26120 glyph->ascent = it->ascent;
26121 glyph->descent = it->descent;
26122 glyph->voffset = it->voffset;
26123 glyph->type = GLYPHLESS_GLYPH;
26124 glyph->u.glyphless.method = it->glyphless_method;
26125 glyph->u.glyphless.for_no_font = for_no_font;
26126 glyph->u.glyphless.len = len;
26127 glyph->u.glyphless.ch = it->c;
26128 glyph->slice.glyphless.upper_xoff = upper_xoff;
26129 glyph->slice.glyphless.upper_yoff = upper_yoff;
26130 glyph->slice.glyphless.lower_xoff = lower_xoff;
26131 glyph->slice.glyphless.lower_yoff = lower_yoff;
26132 glyph->avoid_cursor_p = it->avoid_cursor_p;
26133 glyph->multibyte_p = it->multibyte_p;
26134 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26136 /* In R2L rows, the left and the right box edges need to be
26137 drawn in reverse direction. */
26138 glyph->right_box_line_p = it->start_of_box_run_p;
26139 glyph->left_box_line_p = it->end_of_box_run_p;
26141 else
26143 glyph->left_box_line_p = it->start_of_box_run_p;
26144 glyph->right_box_line_p = it->end_of_box_run_p;
26146 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26147 || it->phys_descent > it->descent);
26148 glyph->padding_p = false;
26149 glyph->glyph_not_available_p = false;
26150 glyph->face_id = face_id;
26151 glyph->font_type = FONT_TYPE_UNKNOWN;
26152 if (it->bidi_p)
26154 glyph->resolved_level = it->bidi_it.resolved_level;
26155 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26156 glyph->bidi_type = it->bidi_it.type;
26158 ++it->glyph_row->used[area];
26160 else
26161 IT_EXPAND_MATRIX_WIDTH (it, area);
26165 /* Produce a glyph for a glyphless character for iterator IT.
26166 IT->glyphless_method specifies which method to use for displaying
26167 the character. See the description of enum
26168 glyphless_display_method in dispextern.h for the detail.
26170 FOR_NO_FONT is true if and only if this is for a character for
26171 which no font was found. ACRONYM, if non-nil, is an acronym string
26172 for the character. */
26174 static void
26175 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26177 int face_id;
26178 struct face *face;
26179 struct font *font;
26180 int base_width, base_height, width, height;
26181 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26182 int len;
26184 /* Get the metrics of the base font. We always refer to the current
26185 ASCII face. */
26186 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26187 font = face->font ? face->font : FRAME_FONT (it->f);
26188 it->ascent = FONT_BASE (font) + font->baseline_offset;
26189 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26190 base_height = it->ascent + it->descent;
26191 base_width = font->average_width;
26193 face_id = merge_glyphless_glyph_face (it);
26195 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26197 it->pixel_width = THIN_SPACE_WIDTH;
26198 len = 0;
26199 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26201 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26203 width = CHAR_WIDTH (it->c);
26204 if (width == 0)
26205 width = 1;
26206 else if (width > 4)
26207 width = 4;
26208 it->pixel_width = base_width * width;
26209 len = 0;
26210 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26212 else
26214 char buf[7];
26215 const char *str;
26216 unsigned int code[6];
26217 int upper_len;
26218 int ascent, descent;
26219 struct font_metrics metrics_upper, metrics_lower;
26221 face = FACE_FROM_ID (it->f, face_id);
26222 font = face->font ? face->font : FRAME_FONT (it->f);
26223 prepare_face_for_display (it->f, face);
26225 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26227 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26228 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26229 if (CONSP (acronym))
26230 acronym = XCAR (acronym);
26231 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26233 else
26235 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26236 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26237 str = buf;
26239 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26240 code[len] = font->driver->encode_char (font, str[len]);
26241 upper_len = (len + 1) / 2;
26242 font->driver->text_extents (font, code, upper_len,
26243 &metrics_upper);
26244 font->driver->text_extents (font, code + upper_len, len - upper_len,
26245 &metrics_lower);
26249 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26250 width = max (metrics_upper.width, metrics_lower.width) + 4;
26251 upper_xoff = upper_yoff = 2; /* the typical case */
26252 if (base_width >= width)
26254 /* Align the upper to the left, the lower to the right. */
26255 it->pixel_width = base_width;
26256 lower_xoff = base_width - 2 - metrics_lower.width;
26258 else
26260 /* Center the shorter one. */
26261 it->pixel_width = width;
26262 if (metrics_upper.width >= metrics_lower.width)
26263 lower_xoff = (width - metrics_lower.width) / 2;
26264 else
26266 /* FIXME: This code doesn't look right. It formerly was
26267 missing the "lower_xoff = 0;", which couldn't have
26268 been right since it left lower_xoff uninitialized. */
26269 lower_xoff = 0;
26270 upper_xoff = (width - metrics_upper.width) / 2;
26274 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26275 top, bottom, and between upper and lower strings. */
26276 height = (metrics_upper.ascent + metrics_upper.descent
26277 + metrics_lower.ascent + metrics_lower.descent) + 5;
26278 /* Center vertically.
26279 H:base_height, D:base_descent
26280 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26282 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26283 descent = D - H/2 + h/2;
26284 lower_yoff = descent - 2 - ld;
26285 upper_yoff = lower_yoff - la - 1 - ud; */
26286 ascent = - (it->descent - (base_height + height + 1) / 2);
26287 descent = it->descent - (base_height - height) / 2;
26288 lower_yoff = descent - 2 - metrics_lower.descent;
26289 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26290 - metrics_upper.descent);
26291 /* Don't make the height shorter than the base height. */
26292 if (height > base_height)
26294 it->ascent = ascent;
26295 it->descent = descent;
26299 it->phys_ascent = it->ascent;
26300 it->phys_descent = it->descent;
26301 if (it->glyph_row)
26302 append_glyphless_glyph (it, face_id, for_no_font, len,
26303 upper_xoff, upper_yoff,
26304 lower_xoff, lower_yoff);
26305 it->nglyphs = 1;
26306 take_vertical_position_into_account (it);
26310 /* RIF:
26311 Produce glyphs/get display metrics for the display element IT is
26312 loaded with. See the description of struct it in dispextern.h
26313 for an overview of struct it. */
26315 void
26316 x_produce_glyphs (struct it *it)
26318 int extra_line_spacing = it->extra_line_spacing;
26320 it->glyph_not_available_p = false;
26322 if (it->what == IT_CHARACTER)
26324 XChar2b char2b;
26325 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26326 struct font *font = face->font;
26327 struct font_metrics *pcm = NULL;
26328 int boff; /* Baseline offset. */
26330 if (font == NULL)
26332 /* When no suitable font is found, display this character by
26333 the method specified in the first extra slot of
26334 Vglyphless_char_display. */
26335 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26337 eassert (it->what == IT_GLYPHLESS);
26338 produce_glyphless_glyph (it, true,
26339 STRINGP (acronym) ? acronym : Qnil);
26340 goto done;
26343 boff = font->baseline_offset;
26344 if (font->vertical_centering)
26345 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26347 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26349 it->nglyphs = 1;
26351 if (it->override_ascent >= 0)
26353 it->ascent = it->override_ascent;
26354 it->descent = it->override_descent;
26355 boff = it->override_boff;
26357 else
26359 it->ascent = FONT_BASE (font) + boff;
26360 it->descent = FONT_DESCENT (font) - boff;
26363 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26365 pcm = get_per_char_metric (font, &char2b);
26366 if (pcm->width == 0
26367 && pcm->rbearing == 0 && pcm->lbearing == 0)
26368 pcm = NULL;
26371 if (pcm)
26373 it->phys_ascent = pcm->ascent + boff;
26374 it->phys_descent = pcm->descent - boff;
26375 it->pixel_width = pcm->width;
26377 else
26379 it->glyph_not_available_p = true;
26380 it->phys_ascent = it->ascent;
26381 it->phys_descent = it->descent;
26382 it->pixel_width = font->space_width;
26385 if (it->constrain_row_ascent_descent_p)
26387 if (it->descent > it->max_descent)
26389 it->ascent += it->descent - it->max_descent;
26390 it->descent = it->max_descent;
26392 if (it->ascent > it->max_ascent)
26394 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26395 it->ascent = it->max_ascent;
26397 it->phys_ascent = min (it->phys_ascent, it->ascent);
26398 it->phys_descent = min (it->phys_descent, it->descent);
26399 extra_line_spacing = 0;
26402 /* If this is a space inside a region of text with
26403 `space-width' property, change its width. */
26404 bool stretched_p
26405 = it->char_to_display == ' ' && !NILP (it->space_width);
26406 if (stretched_p)
26407 it->pixel_width *= XFLOATINT (it->space_width);
26409 /* If face has a box, add the box thickness to the character
26410 height. If character has a box line to the left and/or
26411 right, add the box line width to the character's width. */
26412 if (face->box != FACE_NO_BOX)
26414 int thick = face->box_line_width;
26416 if (thick > 0)
26418 it->ascent += thick;
26419 it->descent += thick;
26421 else
26422 thick = -thick;
26424 if (it->start_of_box_run_p)
26425 it->pixel_width += thick;
26426 if (it->end_of_box_run_p)
26427 it->pixel_width += thick;
26430 /* If face has an overline, add the height of the overline
26431 (1 pixel) and a 1 pixel margin to the character height. */
26432 if (face->overline_p)
26433 it->ascent += overline_margin;
26435 if (it->constrain_row_ascent_descent_p)
26437 if (it->ascent > it->max_ascent)
26438 it->ascent = it->max_ascent;
26439 if (it->descent > it->max_descent)
26440 it->descent = it->max_descent;
26443 take_vertical_position_into_account (it);
26445 /* If we have to actually produce glyphs, do it. */
26446 if (it->glyph_row)
26448 if (stretched_p)
26450 /* Translate a space with a `space-width' property
26451 into a stretch glyph. */
26452 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26453 / FONT_HEIGHT (font));
26454 append_stretch_glyph (it, it->object, it->pixel_width,
26455 it->ascent + it->descent, ascent);
26457 else
26458 append_glyph (it);
26460 /* If characters with lbearing or rbearing are displayed
26461 in this line, record that fact in a flag of the
26462 glyph row. This is used to optimize X output code. */
26463 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26464 it->glyph_row->contains_overlapping_glyphs_p = true;
26466 if (! stretched_p && it->pixel_width == 0)
26467 /* We assure that all visible glyphs have at least 1-pixel
26468 width. */
26469 it->pixel_width = 1;
26471 else if (it->char_to_display == '\n')
26473 /* A newline has no width, but we need the height of the
26474 line. But if previous part of the line sets a height,
26475 don't increase that height. */
26477 Lisp_Object height;
26478 Lisp_Object total_height = Qnil;
26480 it->override_ascent = -1;
26481 it->pixel_width = 0;
26482 it->nglyphs = 0;
26484 height = get_it_property (it, Qline_height);
26485 /* Split (line-height total-height) list. */
26486 if (CONSP (height)
26487 && CONSP (XCDR (height))
26488 && NILP (XCDR (XCDR (height))))
26490 total_height = XCAR (XCDR (height));
26491 height = XCAR (height);
26493 height = calc_line_height_property (it, height, font, boff, true);
26495 if (it->override_ascent >= 0)
26497 it->ascent = it->override_ascent;
26498 it->descent = it->override_descent;
26499 boff = it->override_boff;
26501 else
26503 it->ascent = FONT_BASE (font) + boff;
26504 it->descent = FONT_DESCENT (font) - boff;
26507 if (EQ (height, Qt))
26509 if (it->descent > it->max_descent)
26511 it->ascent += it->descent - it->max_descent;
26512 it->descent = it->max_descent;
26514 if (it->ascent > it->max_ascent)
26516 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26517 it->ascent = it->max_ascent;
26519 it->phys_ascent = min (it->phys_ascent, it->ascent);
26520 it->phys_descent = min (it->phys_descent, it->descent);
26521 it->constrain_row_ascent_descent_p = true;
26522 extra_line_spacing = 0;
26524 else
26526 Lisp_Object spacing;
26528 it->phys_ascent = it->ascent;
26529 it->phys_descent = it->descent;
26531 if ((it->max_ascent > 0 || it->max_descent > 0)
26532 && face->box != FACE_NO_BOX
26533 && face->box_line_width > 0)
26535 it->ascent += face->box_line_width;
26536 it->descent += face->box_line_width;
26538 if (!NILP (height)
26539 && XINT (height) > it->ascent + it->descent)
26540 it->ascent = XINT (height) - it->descent;
26542 if (!NILP (total_height))
26543 spacing = calc_line_height_property (it, total_height, font,
26544 boff, false);
26545 else
26547 spacing = get_it_property (it, Qline_spacing);
26548 spacing = calc_line_height_property (it, spacing, font,
26549 boff, false);
26551 if (INTEGERP (spacing))
26553 extra_line_spacing = XINT (spacing);
26554 if (!NILP (total_height))
26555 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26559 else /* i.e. (it->char_to_display == '\t') */
26561 if (font->space_width > 0)
26563 int tab_width = it->tab_width * font->space_width;
26564 int x = it->current_x + it->continuation_lines_width;
26565 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26567 /* If the distance from the current position to the next tab
26568 stop is less than a space character width, use the
26569 tab stop after that. */
26570 if (next_tab_x - x < font->space_width)
26571 next_tab_x += tab_width;
26573 it->pixel_width = next_tab_x - x;
26574 it->nglyphs = 1;
26575 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26576 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26578 if (it->glyph_row)
26580 append_stretch_glyph (it, it->object, it->pixel_width,
26581 it->ascent + it->descent, it->ascent);
26584 else
26586 it->pixel_width = 0;
26587 it->nglyphs = 1;
26591 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26593 /* A static composition.
26595 Note: A composition is represented as one glyph in the
26596 glyph matrix. There are no padding glyphs.
26598 Important note: pixel_width, ascent, and descent are the
26599 values of what is drawn by draw_glyphs (i.e. the values of
26600 the overall glyphs composed). */
26601 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26602 int boff; /* baseline offset */
26603 struct composition *cmp = composition_table[it->cmp_it.id];
26604 int glyph_len = cmp->glyph_len;
26605 struct font *font = face->font;
26607 it->nglyphs = 1;
26609 /* If we have not yet calculated pixel size data of glyphs of
26610 the composition for the current face font, calculate them
26611 now. Theoretically, we have to check all fonts for the
26612 glyphs, but that requires much time and memory space. So,
26613 here we check only the font of the first glyph. This may
26614 lead to incorrect display, but it's very rare, and C-l
26615 (recenter-top-bottom) can correct the display anyway. */
26616 if (! cmp->font || cmp->font != font)
26618 /* Ascent and descent of the font of the first character
26619 of this composition (adjusted by baseline offset).
26620 Ascent and descent of overall glyphs should not be less
26621 than these, respectively. */
26622 int font_ascent, font_descent, font_height;
26623 /* Bounding box of the overall glyphs. */
26624 int leftmost, rightmost, lowest, highest;
26625 int lbearing, rbearing;
26626 int i, width, ascent, descent;
26627 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26628 XChar2b char2b;
26629 struct font_metrics *pcm;
26630 ptrdiff_t pos;
26632 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26633 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26634 break;
26635 bool right_padded = glyph_len < cmp->glyph_len;
26636 for (i = 0; i < glyph_len; i++)
26638 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26639 break;
26640 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26642 bool left_padded = i > 0;
26644 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26645 : IT_CHARPOS (*it));
26646 /* If no suitable font is found, use the default font. */
26647 bool font_not_found_p = font == NULL;
26648 if (font_not_found_p)
26650 face = face->ascii_face;
26651 font = face->font;
26653 boff = font->baseline_offset;
26654 if (font->vertical_centering)
26655 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26656 font_ascent = FONT_BASE (font) + boff;
26657 font_descent = FONT_DESCENT (font) - boff;
26658 font_height = FONT_HEIGHT (font);
26660 cmp->font = font;
26662 pcm = NULL;
26663 if (! font_not_found_p)
26665 get_char_face_and_encoding (it->f, c, it->face_id,
26666 &char2b, false);
26667 pcm = get_per_char_metric (font, &char2b);
26670 /* Initialize the bounding box. */
26671 if (pcm)
26673 width = cmp->glyph_len > 0 ? pcm->width : 0;
26674 ascent = pcm->ascent;
26675 descent = pcm->descent;
26676 lbearing = pcm->lbearing;
26677 rbearing = pcm->rbearing;
26679 else
26681 width = cmp->glyph_len > 0 ? font->space_width : 0;
26682 ascent = FONT_BASE (font);
26683 descent = FONT_DESCENT (font);
26684 lbearing = 0;
26685 rbearing = width;
26688 rightmost = width;
26689 leftmost = 0;
26690 lowest = - descent + boff;
26691 highest = ascent + boff;
26693 if (! font_not_found_p
26694 && font->default_ascent
26695 && CHAR_TABLE_P (Vuse_default_ascent)
26696 && !NILP (Faref (Vuse_default_ascent,
26697 make_number (it->char_to_display))))
26698 highest = font->default_ascent + boff;
26700 /* Draw the first glyph at the normal position. It may be
26701 shifted to right later if some other glyphs are drawn
26702 at the left. */
26703 cmp->offsets[i * 2] = 0;
26704 cmp->offsets[i * 2 + 1] = boff;
26705 cmp->lbearing = lbearing;
26706 cmp->rbearing = rbearing;
26708 /* Set cmp->offsets for the remaining glyphs. */
26709 for (i++; i < glyph_len; i++)
26711 int left, right, btm, top;
26712 int ch = COMPOSITION_GLYPH (cmp, i);
26713 int face_id;
26714 struct face *this_face;
26716 if (ch == '\t')
26717 ch = ' ';
26718 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26719 this_face = FACE_FROM_ID (it->f, face_id);
26720 font = this_face->font;
26722 if (font == NULL)
26723 pcm = NULL;
26724 else
26726 get_char_face_and_encoding (it->f, ch, face_id,
26727 &char2b, false);
26728 pcm = get_per_char_metric (font, &char2b);
26730 if (! pcm)
26731 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26732 else
26734 width = pcm->width;
26735 ascent = pcm->ascent;
26736 descent = pcm->descent;
26737 lbearing = pcm->lbearing;
26738 rbearing = pcm->rbearing;
26739 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26741 /* Relative composition with or without
26742 alternate chars. */
26743 left = (leftmost + rightmost - width) / 2;
26744 btm = - descent + boff;
26745 if (font->relative_compose
26746 && (! CHAR_TABLE_P (Vignore_relative_composition)
26747 || NILP (Faref (Vignore_relative_composition,
26748 make_number (ch)))))
26751 if (- descent >= font->relative_compose)
26752 /* One extra pixel between two glyphs. */
26753 btm = highest + 1;
26754 else if (ascent <= 0)
26755 /* One extra pixel between two glyphs. */
26756 btm = lowest - 1 - ascent - descent;
26759 else
26761 /* A composition rule is specified by an integer
26762 value that encodes global and new reference
26763 points (GREF and NREF). GREF and NREF are
26764 specified by numbers as below:
26766 0---1---2 -- ascent
26770 9--10--11 -- center
26772 ---3---4---5--- baseline
26774 6---7---8 -- descent
26776 int rule = COMPOSITION_RULE (cmp, i);
26777 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26779 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26780 grefx = gref % 3, nrefx = nref % 3;
26781 grefy = gref / 3, nrefy = nref / 3;
26782 if (xoff)
26783 xoff = font_height * (xoff - 128) / 256;
26784 if (yoff)
26785 yoff = font_height * (yoff - 128) / 256;
26787 left = (leftmost
26788 + grefx * (rightmost - leftmost) / 2
26789 - nrefx * width / 2
26790 + xoff);
26792 btm = ((grefy == 0 ? highest
26793 : grefy == 1 ? 0
26794 : grefy == 2 ? lowest
26795 : (highest + lowest) / 2)
26796 - (nrefy == 0 ? ascent + descent
26797 : nrefy == 1 ? descent - boff
26798 : nrefy == 2 ? 0
26799 : (ascent + descent) / 2)
26800 + yoff);
26803 cmp->offsets[i * 2] = left;
26804 cmp->offsets[i * 2 + 1] = btm + descent;
26806 /* Update the bounding box of the overall glyphs. */
26807 if (width > 0)
26809 right = left + width;
26810 if (left < leftmost)
26811 leftmost = left;
26812 if (right > rightmost)
26813 rightmost = right;
26815 top = btm + descent + ascent;
26816 if (top > highest)
26817 highest = top;
26818 if (btm < lowest)
26819 lowest = btm;
26821 if (cmp->lbearing > left + lbearing)
26822 cmp->lbearing = left + lbearing;
26823 if (cmp->rbearing < left + rbearing)
26824 cmp->rbearing = left + rbearing;
26828 /* If there are glyphs whose x-offsets are negative,
26829 shift all glyphs to the right and make all x-offsets
26830 non-negative. */
26831 if (leftmost < 0)
26833 for (i = 0; i < cmp->glyph_len; i++)
26834 cmp->offsets[i * 2] -= leftmost;
26835 rightmost -= leftmost;
26836 cmp->lbearing -= leftmost;
26837 cmp->rbearing -= leftmost;
26840 if (left_padded && cmp->lbearing < 0)
26842 for (i = 0; i < cmp->glyph_len; i++)
26843 cmp->offsets[i * 2] -= cmp->lbearing;
26844 rightmost -= cmp->lbearing;
26845 cmp->rbearing -= cmp->lbearing;
26846 cmp->lbearing = 0;
26848 if (right_padded && rightmost < cmp->rbearing)
26850 rightmost = cmp->rbearing;
26853 cmp->pixel_width = rightmost;
26854 cmp->ascent = highest;
26855 cmp->descent = - lowest;
26856 if (cmp->ascent < font_ascent)
26857 cmp->ascent = font_ascent;
26858 if (cmp->descent < font_descent)
26859 cmp->descent = font_descent;
26862 if (it->glyph_row
26863 && (cmp->lbearing < 0
26864 || cmp->rbearing > cmp->pixel_width))
26865 it->glyph_row->contains_overlapping_glyphs_p = true;
26867 it->pixel_width = cmp->pixel_width;
26868 it->ascent = it->phys_ascent = cmp->ascent;
26869 it->descent = it->phys_descent = cmp->descent;
26870 if (face->box != FACE_NO_BOX)
26872 int thick = face->box_line_width;
26874 if (thick > 0)
26876 it->ascent += thick;
26877 it->descent += thick;
26879 else
26880 thick = - thick;
26882 if (it->start_of_box_run_p)
26883 it->pixel_width += thick;
26884 if (it->end_of_box_run_p)
26885 it->pixel_width += thick;
26888 /* If face has an overline, add the height of the overline
26889 (1 pixel) and a 1 pixel margin to the character height. */
26890 if (face->overline_p)
26891 it->ascent += overline_margin;
26893 take_vertical_position_into_account (it);
26894 if (it->ascent < 0)
26895 it->ascent = 0;
26896 if (it->descent < 0)
26897 it->descent = 0;
26899 if (it->glyph_row && cmp->glyph_len > 0)
26900 append_composite_glyph (it);
26902 else if (it->what == IT_COMPOSITION)
26904 /* A dynamic (automatic) composition. */
26905 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26906 Lisp_Object gstring;
26907 struct font_metrics metrics;
26909 it->nglyphs = 1;
26911 gstring = composition_gstring_from_id (it->cmp_it.id);
26912 it->pixel_width
26913 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26914 &metrics);
26915 if (it->glyph_row
26916 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26917 it->glyph_row->contains_overlapping_glyphs_p = true;
26918 it->ascent = it->phys_ascent = metrics.ascent;
26919 it->descent = it->phys_descent = metrics.descent;
26920 if (face->box != FACE_NO_BOX)
26922 int thick = face->box_line_width;
26924 if (thick > 0)
26926 it->ascent += thick;
26927 it->descent += thick;
26929 else
26930 thick = - thick;
26932 if (it->start_of_box_run_p)
26933 it->pixel_width += thick;
26934 if (it->end_of_box_run_p)
26935 it->pixel_width += thick;
26937 /* If face has an overline, add the height of the overline
26938 (1 pixel) and a 1 pixel margin to the character height. */
26939 if (face->overline_p)
26940 it->ascent += overline_margin;
26941 take_vertical_position_into_account (it);
26942 if (it->ascent < 0)
26943 it->ascent = 0;
26944 if (it->descent < 0)
26945 it->descent = 0;
26947 if (it->glyph_row)
26948 append_composite_glyph (it);
26950 else if (it->what == IT_GLYPHLESS)
26951 produce_glyphless_glyph (it, false, Qnil);
26952 else if (it->what == IT_IMAGE)
26953 produce_image_glyph (it);
26954 else if (it->what == IT_STRETCH)
26955 produce_stretch_glyph (it);
26957 done:
26958 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26959 because this isn't true for images with `:ascent 100'. */
26960 eassert (it->ascent >= 0 && it->descent >= 0);
26961 if (it->area == TEXT_AREA)
26962 it->current_x += it->pixel_width;
26964 if (extra_line_spacing > 0)
26966 it->descent += extra_line_spacing;
26967 if (extra_line_spacing > it->max_extra_line_spacing)
26968 it->max_extra_line_spacing = extra_line_spacing;
26971 it->max_ascent = max (it->max_ascent, it->ascent);
26972 it->max_descent = max (it->max_descent, it->descent);
26973 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26974 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26977 /* EXPORT for RIF:
26978 Output LEN glyphs starting at START at the nominal cursor position.
26979 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26980 being updated, and UPDATED_AREA is the area of that row being updated. */
26982 void
26983 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26984 struct glyph *start, enum glyph_row_area updated_area, int len)
26986 int x, hpos, chpos = w->phys_cursor.hpos;
26988 eassert (updated_row);
26989 /* When the window is hscrolled, cursor hpos can legitimately be out
26990 of bounds, but we draw the cursor at the corresponding window
26991 margin in that case. */
26992 if (!updated_row->reversed_p && chpos < 0)
26993 chpos = 0;
26994 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26995 chpos = updated_row->used[TEXT_AREA] - 1;
26997 block_input ();
26999 /* Write glyphs. */
27001 hpos = start - updated_row->glyphs[updated_area];
27002 x = draw_glyphs (w, w->output_cursor.x,
27003 updated_row, updated_area,
27004 hpos, hpos + len,
27005 DRAW_NORMAL_TEXT, 0);
27007 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27008 if (updated_area == TEXT_AREA
27009 && w->phys_cursor_on_p
27010 && w->phys_cursor.vpos == w->output_cursor.vpos
27011 && chpos >= hpos
27012 && chpos < hpos + len)
27013 w->phys_cursor_on_p = false;
27015 unblock_input ();
27017 /* Advance the output cursor. */
27018 w->output_cursor.hpos += len;
27019 w->output_cursor.x = x;
27023 /* EXPORT for RIF:
27024 Insert LEN glyphs from START at the nominal cursor position. */
27026 void
27027 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27028 struct glyph *start, enum glyph_row_area updated_area, int len)
27030 struct frame *f;
27031 int line_height, shift_by_width, shifted_region_width;
27032 struct glyph_row *row;
27033 struct glyph *glyph;
27034 int frame_x, frame_y;
27035 ptrdiff_t hpos;
27037 eassert (updated_row);
27038 block_input ();
27039 f = XFRAME (WINDOW_FRAME (w));
27041 /* Get the height of the line we are in. */
27042 row = updated_row;
27043 line_height = row->height;
27045 /* Get the width of the glyphs to insert. */
27046 shift_by_width = 0;
27047 for (glyph = start; glyph < start + len; ++glyph)
27048 shift_by_width += glyph->pixel_width;
27050 /* Get the width of the region to shift right. */
27051 shifted_region_width = (window_box_width (w, updated_area)
27052 - w->output_cursor.x
27053 - shift_by_width);
27055 /* Shift right. */
27056 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27057 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27059 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27060 line_height, shift_by_width);
27062 /* Write the glyphs. */
27063 hpos = start - row->glyphs[updated_area];
27064 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27065 hpos, hpos + len,
27066 DRAW_NORMAL_TEXT, 0);
27068 /* Advance the output cursor. */
27069 w->output_cursor.hpos += len;
27070 w->output_cursor.x += shift_by_width;
27071 unblock_input ();
27075 /* EXPORT for RIF:
27076 Erase the current text line from the nominal cursor position
27077 (inclusive) to pixel column TO_X (exclusive). The idea is that
27078 everything from TO_X onward is already erased.
27080 TO_X is a pixel position relative to UPDATED_AREA of currently
27081 updated window W. TO_X == -1 means clear to the end of this area. */
27083 void
27084 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27085 enum glyph_row_area updated_area, int to_x)
27087 struct frame *f;
27088 int max_x, min_y, max_y;
27089 int from_x, from_y, to_y;
27091 eassert (updated_row);
27092 f = XFRAME (w->frame);
27094 if (updated_row->full_width_p)
27095 max_x = (WINDOW_PIXEL_WIDTH (w)
27096 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27097 else
27098 max_x = window_box_width (w, updated_area);
27099 max_y = window_text_bottom_y (w);
27101 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27102 of window. For TO_X > 0, truncate to end of drawing area. */
27103 if (to_x == 0)
27104 return;
27105 else if (to_x < 0)
27106 to_x = max_x;
27107 else
27108 to_x = min (to_x, max_x);
27110 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27112 /* Notice if the cursor will be cleared by this operation. */
27113 if (!updated_row->full_width_p)
27114 notice_overwritten_cursor (w, updated_area,
27115 w->output_cursor.x, -1,
27116 updated_row->y,
27117 MATRIX_ROW_BOTTOM_Y (updated_row));
27119 from_x = w->output_cursor.x;
27121 /* Translate to frame coordinates. */
27122 if (updated_row->full_width_p)
27124 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27125 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27127 else
27129 int area_left = window_box_left (w, updated_area);
27130 from_x += area_left;
27131 to_x += area_left;
27134 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27135 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27136 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27138 /* Prevent inadvertently clearing to end of the X window. */
27139 if (to_x > from_x && to_y > from_y)
27141 block_input ();
27142 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27143 to_x - from_x, to_y - from_y);
27144 unblock_input ();
27148 #endif /* HAVE_WINDOW_SYSTEM */
27152 /***********************************************************************
27153 Cursor types
27154 ***********************************************************************/
27156 /* Value is the internal representation of the specified cursor type
27157 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27158 of the bar cursor. */
27160 static enum text_cursor_kinds
27161 get_specified_cursor_type (Lisp_Object arg, int *width)
27163 enum text_cursor_kinds type;
27165 if (NILP (arg))
27166 return NO_CURSOR;
27168 if (EQ (arg, Qbox))
27169 return FILLED_BOX_CURSOR;
27171 if (EQ (arg, Qhollow))
27172 return HOLLOW_BOX_CURSOR;
27174 if (EQ (arg, Qbar))
27176 *width = 2;
27177 return BAR_CURSOR;
27180 if (CONSP (arg)
27181 && EQ (XCAR (arg), Qbar)
27182 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27184 *width = XINT (XCDR (arg));
27185 return BAR_CURSOR;
27188 if (EQ (arg, Qhbar))
27190 *width = 2;
27191 return HBAR_CURSOR;
27194 if (CONSP (arg)
27195 && EQ (XCAR (arg), Qhbar)
27196 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27198 *width = XINT (XCDR (arg));
27199 return HBAR_CURSOR;
27202 /* Treat anything unknown as "hollow box cursor".
27203 It was bad to signal an error; people have trouble fixing
27204 .Xdefaults with Emacs, when it has something bad in it. */
27205 type = HOLLOW_BOX_CURSOR;
27207 return type;
27210 /* Set the default cursor types for specified frame. */
27211 void
27212 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27214 int width = 1;
27215 Lisp_Object tem;
27217 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27218 FRAME_CURSOR_WIDTH (f) = width;
27220 /* By default, set up the blink-off state depending on the on-state. */
27222 tem = Fassoc (arg, Vblink_cursor_alist);
27223 if (!NILP (tem))
27225 FRAME_BLINK_OFF_CURSOR (f)
27226 = get_specified_cursor_type (XCDR (tem), &width);
27227 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27229 else
27230 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27232 /* Make sure the cursor gets redrawn. */
27233 f->cursor_type_changed = true;
27237 #ifdef HAVE_WINDOW_SYSTEM
27239 /* Return the cursor we want to be displayed in window W. Return
27240 width of bar/hbar cursor through WIDTH arg. Return with
27241 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27242 (i.e. if the `system caret' should track this cursor).
27244 In a mini-buffer window, we want the cursor only to appear if we
27245 are reading input from this window. For the selected window, we
27246 want the cursor type given by the frame parameter or buffer local
27247 setting of cursor-type. If explicitly marked off, draw no cursor.
27248 In all other cases, we want a hollow box cursor. */
27250 static enum text_cursor_kinds
27251 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27252 bool *active_cursor)
27254 struct frame *f = XFRAME (w->frame);
27255 struct buffer *b = XBUFFER (w->contents);
27256 int cursor_type = DEFAULT_CURSOR;
27257 Lisp_Object alt_cursor;
27258 bool non_selected = false;
27260 *active_cursor = true;
27262 /* Echo area */
27263 if (cursor_in_echo_area
27264 && FRAME_HAS_MINIBUF_P (f)
27265 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27267 if (w == XWINDOW (echo_area_window))
27269 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27271 *width = FRAME_CURSOR_WIDTH (f);
27272 return FRAME_DESIRED_CURSOR (f);
27274 else
27275 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27278 *active_cursor = false;
27279 non_selected = true;
27282 /* Detect a nonselected window or nonselected frame. */
27283 else if (w != XWINDOW (f->selected_window)
27284 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27286 *active_cursor = false;
27288 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27289 return NO_CURSOR;
27291 non_selected = true;
27294 /* Never display a cursor in a window in which cursor-type is nil. */
27295 if (NILP (BVAR (b, cursor_type)))
27296 return NO_CURSOR;
27298 /* Get the normal cursor type for this window. */
27299 if (EQ (BVAR (b, cursor_type), Qt))
27301 cursor_type = FRAME_DESIRED_CURSOR (f);
27302 *width = FRAME_CURSOR_WIDTH (f);
27304 else
27305 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27307 /* Use cursor-in-non-selected-windows instead
27308 for non-selected window or frame. */
27309 if (non_selected)
27311 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27312 if (!EQ (Qt, alt_cursor))
27313 return get_specified_cursor_type (alt_cursor, width);
27314 /* t means modify the normal cursor type. */
27315 if (cursor_type == FILLED_BOX_CURSOR)
27316 cursor_type = HOLLOW_BOX_CURSOR;
27317 else if (cursor_type == BAR_CURSOR && *width > 1)
27318 --*width;
27319 return cursor_type;
27322 /* Use normal cursor if not blinked off. */
27323 if (!w->cursor_off_p)
27325 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27327 if (cursor_type == FILLED_BOX_CURSOR)
27329 /* Using a block cursor on large images can be very annoying.
27330 So use a hollow cursor for "large" images.
27331 If image is not transparent (no mask), also use hollow cursor. */
27332 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27333 if (img != NULL && IMAGEP (img->spec))
27335 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27336 where N = size of default frame font size.
27337 This should cover most of the "tiny" icons people may use. */
27338 if (!img->mask
27339 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27340 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27341 cursor_type = HOLLOW_BOX_CURSOR;
27344 else if (cursor_type != NO_CURSOR)
27346 /* Display current only supports BOX and HOLLOW cursors for images.
27347 So for now, unconditionally use a HOLLOW cursor when cursor is
27348 not a solid box cursor. */
27349 cursor_type = HOLLOW_BOX_CURSOR;
27352 return cursor_type;
27355 /* Cursor is blinked off, so determine how to "toggle" it. */
27357 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27358 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27359 return get_specified_cursor_type (XCDR (alt_cursor), width);
27361 /* Then see if frame has specified a specific blink off cursor type. */
27362 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27364 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27365 return FRAME_BLINK_OFF_CURSOR (f);
27368 #if false
27369 /* Some people liked having a permanently visible blinking cursor,
27370 while others had very strong opinions against it. So it was
27371 decided to remove it. KFS 2003-09-03 */
27373 /* Finally perform built-in cursor blinking:
27374 filled box <-> hollow box
27375 wide [h]bar <-> narrow [h]bar
27376 narrow [h]bar <-> no cursor
27377 other type <-> no cursor */
27379 if (cursor_type == FILLED_BOX_CURSOR)
27380 return HOLLOW_BOX_CURSOR;
27382 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27384 *width = 1;
27385 return cursor_type;
27387 #endif
27389 return NO_CURSOR;
27393 /* Notice when the text cursor of window W has been completely
27394 overwritten by a drawing operation that outputs glyphs in AREA
27395 starting at X0 and ending at X1 in the line starting at Y0 and
27396 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27397 the rest of the line after X0 has been written. Y coordinates
27398 are window-relative. */
27400 static void
27401 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27402 int x0, int x1, int y0, int y1)
27404 int cx0, cx1, cy0, cy1;
27405 struct glyph_row *row;
27407 if (!w->phys_cursor_on_p)
27408 return;
27409 if (area != TEXT_AREA)
27410 return;
27412 if (w->phys_cursor.vpos < 0
27413 || w->phys_cursor.vpos >= w->current_matrix->nrows
27414 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27415 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27416 return;
27418 if (row->cursor_in_fringe_p)
27420 row->cursor_in_fringe_p = false;
27421 draw_fringe_bitmap (w, row, row->reversed_p);
27422 w->phys_cursor_on_p = false;
27423 return;
27426 cx0 = w->phys_cursor.x;
27427 cx1 = cx0 + w->phys_cursor_width;
27428 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27429 return;
27431 /* The cursor image will be completely removed from the
27432 screen if the output area intersects the cursor area in
27433 y-direction. When we draw in [y0 y1[, and some part of
27434 the cursor is at y < y0, that part must have been drawn
27435 before. When scrolling, the cursor is erased before
27436 actually scrolling, so we don't come here. When not
27437 scrolling, the rows above the old cursor row must have
27438 changed, and in this case these rows must have written
27439 over the cursor image.
27441 Likewise if part of the cursor is below y1, with the
27442 exception of the cursor being in the first blank row at
27443 the buffer and window end because update_text_area
27444 doesn't draw that row. (Except when it does, but
27445 that's handled in update_text_area.) */
27447 cy0 = w->phys_cursor.y;
27448 cy1 = cy0 + w->phys_cursor_height;
27449 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27450 return;
27452 w->phys_cursor_on_p = false;
27455 #endif /* HAVE_WINDOW_SYSTEM */
27458 /************************************************************************
27459 Mouse Face
27460 ************************************************************************/
27462 #ifdef HAVE_WINDOW_SYSTEM
27464 /* EXPORT for RIF:
27465 Fix the display of area AREA of overlapping row ROW in window W
27466 with respect to the overlapping part OVERLAPS. */
27468 void
27469 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27470 enum glyph_row_area area, int overlaps)
27472 int i, x;
27474 block_input ();
27476 x = 0;
27477 for (i = 0; i < row->used[area];)
27479 if (row->glyphs[area][i].overlaps_vertically_p)
27481 int start = i, start_x = x;
27485 x += row->glyphs[area][i].pixel_width;
27486 ++i;
27488 while (i < row->used[area]
27489 && row->glyphs[area][i].overlaps_vertically_p);
27491 draw_glyphs (w, start_x, row, area,
27492 start, i,
27493 DRAW_NORMAL_TEXT, overlaps);
27495 else
27497 x += row->glyphs[area][i].pixel_width;
27498 ++i;
27502 unblock_input ();
27506 /* EXPORT:
27507 Draw the cursor glyph of window W in glyph row ROW. See the
27508 comment of draw_glyphs for the meaning of HL. */
27510 void
27511 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27512 enum draw_glyphs_face hl)
27514 /* If cursor hpos is out of bounds, don't draw garbage. This can
27515 happen in mini-buffer windows when switching between echo area
27516 glyphs and mini-buffer. */
27517 if ((row->reversed_p
27518 ? (w->phys_cursor.hpos >= 0)
27519 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27521 bool on_p = w->phys_cursor_on_p;
27522 int x1;
27523 int hpos = w->phys_cursor.hpos;
27525 /* When the window is hscrolled, cursor hpos can legitimately be
27526 out of bounds, but we draw the cursor at the corresponding
27527 window margin in that case. */
27528 if (!row->reversed_p && hpos < 0)
27529 hpos = 0;
27530 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27531 hpos = row->used[TEXT_AREA] - 1;
27533 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27534 hl, 0);
27535 w->phys_cursor_on_p = on_p;
27537 if (hl == DRAW_CURSOR)
27538 w->phys_cursor_width = x1 - w->phys_cursor.x;
27539 /* When we erase the cursor, and ROW is overlapped by other
27540 rows, make sure that these overlapping parts of other rows
27541 are redrawn. */
27542 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27544 w->phys_cursor_width = x1 - w->phys_cursor.x;
27546 if (row > w->current_matrix->rows
27547 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27548 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27549 OVERLAPS_ERASED_CURSOR);
27551 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27552 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27553 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27554 OVERLAPS_ERASED_CURSOR);
27560 /* Erase the image of a cursor of window W from the screen. */
27562 void
27563 erase_phys_cursor (struct window *w)
27565 struct frame *f = XFRAME (w->frame);
27566 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27567 int hpos = w->phys_cursor.hpos;
27568 int vpos = w->phys_cursor.vpos;
27569 bool mouse_face_here_p = false;
27570 struct glyph_matrix *active_glyphs = w->current_matrix;
27571 struct glyph_row *cursor_row;
27572 struct glyph *cursor_glyph;
27573 enum draw_glyphs_face hl;
27575 /* No cursor displayed or row invalidated => nothing to do on the
27576 screen. */
27577 if (w->phys_cursor_type == NO_CURSOR)
27578 goto mark_cursor_off;
27580 /* VPOS >= active_glyphs->nrows means that window has been resized.
27581 Don't bother to erase the cursor. */
27582 if (vpos >= active_glyphs->nrows)
27583 goto mark_cursor_off;
27585 /* If row containing cursor is marked invalid, there is nothing we
27586 can do. */
27587 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27588 if (!cursor_row->enabled_p)
27589 goto mark_cursor_off;
27591 /* If line spacing is > 0, old cursor may only be partially visible in
27592 window after split-window. So adjust visible height. */
27593 cursor_row->visible_height = min (cursor_row->visible_height,
27594 window_text_bottom_y (w) - cursor_row->y);
27596 /* If row is completely invisible, don't attempt to delete a cursor which
27597 isn't there. This can happen if cursor is at top of a window, and
27598 we switch to a buffer with a header line in that window. */
27599 if (cursor_row->visible_height <= 0)
27600 goto mark_cursor_off;
27602 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27603 if (cursor_row->cursor_in_fringe_p)
27605 cursor_row->cursor_in_fringe_p = false;
27606 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27607 goto mark_cursor_off;
27610 /* This can happen when the new row is shorter than the old one.
27611 In this case, either draw_glyphs or clear_end_of_line
27612 should have cleared the cursor. Note that we wouldn't be
27613 able to erase the cursor in this case because we don't have a
27614 cursor glyph at hand. */
27615 if ((cursor_row->reversed_p
27616 ? (w->phys_cursor.hpos < 0)
27617 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27618 goto mark_cursor_off;
27620 /* When the window is hscrolled, cursor hpos can legitimately be out
27621 of bounds, but we draw the cursor at the corresponding window
27622 margin in that case. */
27623 if (!cursor_row->reversed_p && hpos < 0)
27624 hpos = 0;
27625 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27626 hpos = cursor_row->used[TEXT_AREA] - 1;
27628 /* If the cursor is in the mouse face area, redisplay that when
27629 we clear the cursor. */
27630 if (! NILP (hlinfo->mouse_face_window)
27631 && coords_in_mouse_face_p (w, hpos, vpos)
27632 /* Don't redraw the cursor's spot in mouse face if it is at the
27633 end of a line (on a newline). The cursor appears there, but
27634 mouse highlighting does not. */
27635 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27636 mouse_face_here_p = true;
27638 /* Maybe clear the display under the cursor. */
27639 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27641 int x, y;
27642 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27643 int width;
27645 cursor_glyph = get_phys_cursor_glyph (w);
27646 if (cursor_glyph == NULL)
27647 goto mark_cursor_off;
27649 width = cursor_glyph->pixel_width;
27650 x = w->phys_cursor.x;
27651 if (x < 0)
27653 width += x;
27654 x = 0;
27656 width = min (width, window_box_width (w, TEXT_AREA) - x);
27657 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27658 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27660 if (width > 0)
27661 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27664 /* Erase the cursor by redrawing the character underneath it. */
27665 if (mouse_face_here_p)
27666 hl = DRAW_MOUSE_FACE;
27667 else
27668 hl = DRAW_NORMAL_TEXT;
27669 draw_phys_cursor_glyph (w, cursor_row, hl);
27671 mark_cursor_off:
27672 w->phys_cursor_on_p = false;
27673 w->phys_cursor_type = NO_CURSOR;
27677 /* Display or clear cursor of window W. If !ON, clear the cursor.
27678 If ON, display the cursor; where to put the cursor is specified by
27679 HPOS, VPOS, X and Y. */
27681 void
27682 display_and_set_cursor (struct window *w, bool on,
27683 int hpos, int vpos, int x, int y)
27685 struct frame *f = XFRAME (w->frame);
27686 int new_cursor_type;
27687 int new_cursor_width;
27688 bool active_cursor;
27689 struct glyph_row *glyph_row;
27690 struct glyph *glyph;
27692 /* This is pointless on invisible frames, and dangerous on garbaged
27693 windows and frames; in the latter case, the frame or window may
27694 be in the midst of changing its size, and x and y may be off the
27695 window. */
27696 if (! FRAME_VISIBLE_P (f)
27697 || FRAME_GARBAGED_P (f)
27698 || vpos >= w->current_matrix->nrows
27699 || hpos >= w->current_matrix->matrix_w)
27700 return;
27702 /* If cursor is off and we want it off, return quickly. */
27703 if (!on && !w->phys_cursor_on_p)
27704 return;
27706 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27707 /* If cursor row is not enabled, we don't really know where to
27708 display the cursor. */
27709 if (!glyph_row->enabled_p)
27711 w->phys_cursor_on_p = false;
27712 return;
27715 glyph = NULL;
27716 if (!glyph_row->exact_window_width_line_p
27717 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27718 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27720 eassert (input_blocked_p ());
27722 /* Set new_cursor_type to the cursor we want to be displayed. */
27723 new_cursor_type = get_window_cursor_type (w, glyph,
27724 &new_cursor_width, &active_cursor);
27726 /* If cursor is currently being shown and we don't want it to be or
27727 it is in the wrong place, or the cursor type is not what we want,
27728 erase it. */
27729 if (w->phys_cursor_on_p
27730 && (!on
27731 || w->phys_cursor.x != x
27732 || w->phys_cursor.y != y
27733 /* HPOS can be negative in R2L rows whose
27734 exact_window_width_line_p flag is set (i.e. their newline
27735 would "overflow into the fringe"). */
27736 || hpos < 0
27737 || new_cursor_type != w->phys_cursor_type
27738 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27739 && new_cursor_width != w->phys_cursor_width)))
27740 erase_phys_cursor (w);
27742 /* Don't check phys_cursor_on_p here because that flag is only set
27743 to false in some cases where we know that the cursor has been
27744 completely erased, to avoid the extra work of erasing the cursor
27745 twice. In other words, phys_cursor_on_p can be true and the cursor
27746 still not be visible, or it has only been partly erased. */
27747 if (on)
27749 w->phys_cursor_ascent = glyph_row->ascent;
27750 w->phys_cursor_height = glyph_row->height;
27752 /* Set phys_cursor_.* before x_draw_.* is called because some
27753 of them may need the information. */
27754 w->phys_cursor.x = x;
27755 w->phys_cursor.y = glyph_row->y;
27756 w->phys_cursor.hpos = hpos;
27757 w->phys_cursor.vpos = vpos;
27760 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27761 new_cursor_type, new_cursor_width,
27762 on, active_cursor);
27766 /* Switch the display of W's cursor on or off, according to the value
27767 of ON. */
27769 static void
27770 update_window_cursor (struct window *w, bool on)
27772 /* Don't update cursor in windows whose frame is in the process
27773 of being deleted. */
27774 if (w->current_matrix)
27776 int hpos = w->phys_cursor.hpos;
27777 int vpos = w->phys_cursor.vpos;
27778 struct glyph_row *row;
27780 if (vpos >= w->current_matrix->nrows
27781 || hpos >= w->current_matrix->matrix_w)
27782 return;
27784 row = MATRIX_ROW (w->current_matrix, vpos);
27786 /* When the window is hscrolled, cursor hpos can legitimately be
27787 out of bounds, but we draw the cursor at the corresponding
27788 window margin in that case. */
27789 if (!row->reversed_p && hpos < 0)
27790 hpos = 0;
27791 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27792 hpos = row->used[TEXT_AREA] - 1;
27794 block_input ();
27795 display_and_set_cursor (w, on, hpos, vpos,
27796 w->phys_cursor.x, w->phys_cursor.y);
27797 unblock_input ();
27802 /* Call update_window_cursor with parameter ON_P on all leaf windows
27803 in the window tree rooted at W. */
27805 static void
27806 update_cursor_in_window_tree (struct window *w, bool on_p)
27808 while (w)
27810 if (WINDOWP (w->contents))
27811 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27812 else
27813 update_window_cursor (w, on_p);
27815 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27820 /* EXPORT:
27821 Display the cursor on window W, or clear it, according to ON_P.
27822 Don't change the cursor's position. */
27824 void
27825 x_update_cursor (struct frame *f, bool on_p)
27827 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27831 /* EXPORT:
27832 Clear the cursor of window W to background color, and mark the
27833 cursor as not shown. This is used when the text where the cursor
27834 is about to be rewritten. */
27836 void
27837 x_clear_cursor (struct window *w)
27839 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27840 update_window_cursor (w, false);
27843 #endif /* HAVE_WINDOW_SYSTEM */
27845 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27846 and MSDOS. */
27847 static void
27848 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27849 int start_hpos, int end_hpos,
27850 enum draw_glyphs_face draw)
27852 #ifdef HAVE_WINDOW_SYSTEM
27853 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27855 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27856 return;
27858 #endif
27859 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27860 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27861 #endif
27864 /* Display the active region described by mouse_face_* according to DRAW. */
27866 static void
27867 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27869 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27870 struct frame *f = XFRAME (WINDOW_FRAME (w));
27872 if (/* If window is in the process of being destroyed, don't bother
27873 to do anything. */
27874 w->current_matrix != NULL
27875 /* Don't update mouse highlight if hidden. */
27876 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27877 /* Recognize when we are called to operate on rows that don't exist
27878 anymore. This can happen when a window is split. */
27879 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27881 bool phys_cursor_on_p = w->phys_cursor_on_p;
27882 struct glyph_row *row, *first, *last;
27884 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27885 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27887 for (row = first; row <= last && row->enabled_p; ++row)
27889 int start_hpos, end_hpos, start_x;
27891 /* For all but the first row, the highlight starts at column 0. */
27892 if (row == first)
27894 /* R2L rows have BEG and END in reversed order, but the
27895 screen drawing geometry is always left to right. So
27896 we need to mirror the beginning and end of the
27897 highlighted area in R2L rows. */
27898 if (!row->reversed_p)
27900 start_hpos = hlinfo->mouse_face_beg_col;
27901 start_x = hlinfo->mouse_face_beg_x;
27903 else if (row == last)
27905 start_hpos = hlinfo->mouse_face_end_col;
27906 start_x = hlinfo->mouse_face_end_x;
27908 else
27910 start_hpos = 0;
27911 start_x = 0;
27914 else if (row->reversed_p && row == last)
27916 start_hpos = hlinfo->mouse_face_end_col;
27917 start_x = hlinfo->mouse_face_end_x;
27919 else
27921 start_hpos = 0;
27922 start_x = 0;
27925 if (row == last)
27927 if (!row->reversed_p)
27928 end_hpos = hlinfo->mouse_face_end_col;
27929 else if (row == first)
27930 end_hpos = hlinfo->mouse_face_beg_col;
27931 else
27933 end_hpos = row->used[TEXT_AREA];
27934 if (draw == DRAW_NORMAL_TEXT)
27935 row->fill_line_p = true; /* Clear to end of line. */
27938 else if (row->reversed_p && row == first)
27939 end_hpos = hlinfo->mouse_face_beg_col;
27940 else
27942 end_hpos = row->used[TEXT_AREA];
27943 if (draw == DRAW_NORMAL_TEXT)
27944 row->fill_line_p = true; /* Clear to end of line. */
27947 if (end_hpos > start_hpos)
27949 draw_row_with_mouse_face (w, start_x, row,
27950 start_hpos, end_hpos, draw);
27952 row->mouse_face_p
27953 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27957 #ifdef HAVE_WINDOW_SYSTEM
27958 /* When we've written over the cursor, arrange for it to
27959 be displayed again. */
27960 if (FRAME_WINDOW_P (f)
27961 && phys_cursor_on_p && !w->phys_cursor_on_p)
27963 int hpos = w->phys_cursor.hpos;
27965 /* When the window is hscrolled, cursor hpos can legitimately be
27966 out of bounds, but we draw the cursor at the corresponding
27967 window margin in that case. */
27968 if (!row->reversed_p && hpos < 0)
27969 hpos = 0;
27970 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27971 hpos = row->used[TEXT_AREA] - 1;
27973 block_input ();
27974 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
27975 w->phys_cursor.x, w->phys_cursor.y);
27976 unblock_input ();
27978 #endif /* HAVE_WINDOW_SYSTEM */
27981 #ifdef HAVE_WINDOW_SYSTEM
27982 /* Change the mouse cursor. */
27983 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
27985 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27986 if (draw == DRAW_NORMAL_TEXT
27987 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27988 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27989 else
27990 #endif
27991 if (draw == DRAW_MOUSE_FACE)
27992 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27993 else
27994 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27996 #endif /* HAVE_WINDOW_SYSTEM */
27999 /* EXPORT:
28000 Clear out the mouse-highlighted active region.
28001 Redraw it un-highlighted first. Value is true if mouse
28002 face was actually drawn unhighlighted. */
28004 bool
28005 clear_mouse_face (Mouse_HLInfo *hlinfo)
28007 bool cleared
28008 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28009 if (cleared)
28010 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28011 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28012 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28013 hlinfo->mouse_face_window = Qnil;
28014 hlinfo->mouse_face_overlay = Qnil;
28015 return cleared;
28018 /* Return true if the coordinates HPOS and VPOS on windows W are
28019 within the mouse face on that window. */
28020 static bool
28021 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28023 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28025 /* Quickly resolve the easy cases. */
28026 if (!(WINDOWP (hlinfo->mouse_face_window)
28027 && XWINDOW (hlinfo->mouse_face_window) == w))
28028 return false;
28029 if (vpos < hlinfo->mouse_face_beg_row
28030 || vpos > hlinfo->mouse_face_end_row)
28031 return false;
28032 if (vpos > hlinfo->mouse_face_beg_row
28033 && vpos < hlinfo->mouse_face_end_row)
28034 return true;
28036 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28038 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28040 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28041 return true;
28043 else if ((vpos == hlinfo->mouse_face_beg_row
28044 && hpos >= hlinfo->mouse_face_beg_col)
28045 || (vpos == hlinfo->mouse_face_end_row
28046 && hpos < hlinfo->mouse_face_end_col))
28047 return true;
28049 else
28051 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28053 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28054 return true;
28056 else if ((vpos == hlinfo->mouse_face_beg_row
28057 && hpos <= hlinfo->mouse_face_beg_col)
28058 || (vpos == hlinfo->mouse_face_end_row
28059 && hpos > hlinfo->mouse_face_end_col))
28060 return true;
28062 return false;
28066 /* EXPORT:
28067 True if physical cursor of window W is within mouse face. */
28069 bool
28070 cursor_in_mouse_face_p (struct window *w)
28072 int hpos = w->phys_cursor.hpos;
28073 int vpos = w->phys_cursor.vpos;
28074 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28076 /* When the window is hscrolled, cursor hpos can legitimately be out
28077 of bounds, but we draw the cursor at the corresponding window
28078 margin in that case. */
28079 if (!row->reversed_p && hpos < 0)
28080 hpos = 0;
28081 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28082 hpos = row->used[TEXT_AREA] - 1;
28084 return coords_in_mouse_face_p (w, hpos, vpos);
28089 /* Find the glyph rows START_ROW and END_ROW of window W that display
28090 characters between buffer positions START_CHARPOS and END_CHARPOS
28091 (excluding END_CHARPOS). DISP_STRING is a display string that
28092 covers these buffer positions. This is similar to
28093 row_containing_pos, but is more accurate when bidi reordering makes
28094 buffer positions change non-linearly with glyph rows. */
28095 static void
28096 rows_from_pos_range (struct window *w,
28097 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28098 Lisp_Object disp_string,
28099 struct glyph_row **start, struct glyph_row **end)
28101 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28102 int last_y = window_text_bottom_y (w);
28103 struct glyph_row *row;
28105 *start = NULL;
28106 *end = NULL;
28108 while (!first->enabled_p
28109 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28110 first++;
28112 /* Find the START row. */
28113 for (row = first;
28114 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28115 row++)
28117 /* A row can potentially be the START row if the range of the
28118 characters it displays intersects the range
28119 [START_CHARPOS..END_CHARPOS). */
28120 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28121 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28122 /* See the commentary in row_containing_pos, for the
28123 explanation of the complicated way to check whether
28124 some position is beyond the end of the characters
28125 displayed by a row. */
28126 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28127 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28128 && !row->ends_at_zv_p
28129 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28130 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28131 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28132 && !row->ends_at_zv_p
28133 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28135 /* Found a candidate row. Now make sure at least one of the
28136 glyphs it displays has a charpos from the range
28137 [START_CHARPOS..END_CHARPOS).
28139 This is not obvious because bidi reordering could make
28140 buffer positions of a row be 1,2,3,102,101,100, and if we
28141 want to highlight characters in [50..60), we don't want
28142 this row, even though [50..60) does intersect [1..103),
28143 the range of character positions given by the row's start
28144 and end positions. */
28145 struct glyph *g = row->glyphs[TEXT_AREA];
28146 struct glyph *e = g + row->used[TEXT_AREA];
28148 while (g < e)
28150 if (((BUFFERP (g->object) || NILP (g->object))
28151 && start_charpos <= g->charpos && g->charpos < end_charpos)
28152 /* A glyph that comes from DISP_STRING is by
28153 definition to be highlighted. */
28154 || EQ (g->object, disp_string))
28155 *start = row;
28156 g++;
28158 if (*start)
28159 break;
28163 /* Find the END row. */
28164 if (!*start
28165 /* If the last row is partially visible, start looking for END
28166 from that row, instead of starting from FIRST. */
28167 && !(row->enabled_p
28168 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28169 row = first;
28170 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28172 struct glyph_row *next = row + 1;
28173 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28175 if (!next->enabled_p
28176 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28177 /* The first row >= START whose range of displayed characters
28178 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28179 is the row END + 1. */
28180 || (start_charpos < next_start
28181 && end_charpos < next_start)
28182 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28183 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28184 && !next->ends_at_zv_p
28185 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28186 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28187 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28188 && !next->ends_at_zv_p
28189 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28191 *end = row;
28192 break;
28194 else
28196 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28197 but none of the characters it displays are in the range, it is
28198 also END + 1. */
28199 struct glyph *g = next->glyphs[TEXT_AREA];
28200 struct glyph *s = g;
28201 struct glyph *e = g + next->used[TEXT_AREA];
28203 while (g < e)
28205 if (((BUFFERP (g->object) || NILP (g->object))
28206 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28207 /* If the buffer position of the first glyph in
28208 the row is equal to END_CHARPOS, it means
28209 the last character to be highlighted is the
28210 newline of ROW, and we must consider NEXT as
28211 END, not END+1. */
28212 || (((!next->reversed_p && g == s)
28213 || (next->reversed_p && g == e - 1))
28214 && (g->charpos == end_charpos
28215 /* Special case for when NEXT is an
28216 empty line at ZV. */
28217 || (g->charpos == -1
28218 && !row->ends_at_zv_p
28219 && next_start == end_charpos)))))
28220 /* A glyph that comes from DISP_STRING is by
28221 definition to be highlighted. */
28222 || EQ (g->object, disp_string))
28223 break;
28224 g++;
28226 if (g == e)
28228 *end = row;
28229 break;
28231 /* The first row that ends at ZV must be the last to be
28232 highlighted. */
28233 else if (next->ends_at_zv_p)
28235 *end = next;
28236 break;
28242 /* This function sets the mouse_face_* elements of HLINFO, assuming
28243 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28244 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28245 for the overlay or run of text properties specifying the mouse
28246 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28247 before-string and after-string that must also be highlighted.
28248 DISP_STRING, if non-nil, is a display string that may cover some
28249 or all of the highlighted text. */
28251 static void
28252 mouse_face_from_buffer_pos (Lisp_Object window,
28253 Mouse_HLInfo *hlinfo,
28254 ptrdiff_t mouse_charpos,
28255 ptrdiff_t start_charpos,
28256 ptrdiff_t end_charpos,
28257 Lisp_Object before_string,
28258 Lisp_Object after_string,
28259 Lisp_Object disp_string)
28261 struct window *w = XWINDOW (window);
28262 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28263 struct glyph_row *r1, *r2;
28264 struct glyph *glyph, *end;
28265 ptrdiff_t ignore, pos;
28266 int x;
28268 eassert (NILP (disp_string) || STRINGP (disp_string));
28269 eassert (NILP (before_string) || STRINGP (before_string));
28270 eassert (NILP (after_string) || STRINGP (after_string));
28272 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28273 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28274 if (r1 == NULL)
28275 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28276 /* If the before-string or display-string contains newlines,
28277 rows_from_pos_range skips to its last row. Move back. */
28278 if (!NILP (before_string) || !NILP (disp_string))
28280 struct glyph_row *prev;
28281 while ((prev = r1 - 1, prev >= first)
28282 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28283 && prev->used[TEXT_AREA] > 0)
28285 struct glyph *beg = prev->glyphs[TEXT_AREA];
28286 glyph = beg + prev->used[TEXT_AREA];
28287 while (--glyph >= beg && NILP (glyph->object));
28288 if (glyph < beg
28289 || !(EQ (glyph->object, before_string)
28290 || EQ (glyph->object, disp_string)))
28291 break;
28292 r1 = prev;
28295 if (r2 == NULL)
28297 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28298 hlinfo->mouse_face_past_end = true;
28300 else if (!NILP (after_string))
28302 /* If the after-string has newlines, advance to its last row. */
28303 struct glyph_row *next;
28304 struct glyph_row *last
28305 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28307 for (next = r2 + 1;
28308 next <= last
28309 && next->used[TEXT_AREA] > 0
28310 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28311 ++next)
28312 r2 = next;
28314 /* The rest of the display engine assumes that mouse_face_beg_row is
28315 either above mouse_face_end_row or identical to it. But with
28316 bidi-reordered continued lines, the row for START_CHARPOS could
28317 be below the row for END_CHARPOS. If so, swap the rows and store
28318 them in correct order. */
28319 if (r1->y > r2->y)
28321 struct glyph_row *tem = r2;
28323 r2 = r1;
28324 r1 = tem;
28327 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28328 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28330 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28331 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28332 could be anywhere in the row and in any order. The strategy
28333 below is to find the leftmost and the rightmost glyph that
28334 belongs to either of these 3 strings, or whose position is
28335 between START_CHARPOS and END_CHARPOS, and highlight all the
28336 glyphs between those two. This may cover more than just the text
28337 between START_CHARPOS and END_CHARPOS if the range of characters
28338 strides the bidi level boundary, e.g. if the beginning is in R2L
28339 text while the end is in L2R text or vice versa. */
28340 if (!r1->reversed_p)
28342 /* This row is in a left to right paragraph. Scan it left to
28343 right. */
28344 glyph = r1->glyphs[TEXT_AREA];
28345 end = glyph + r1->used[TEXT_AREA];
28346 x = r1->x;
28348 /* Skip truncation glyphs at the start of the glyph row. */
28349 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28350 for (; glyph < end
28351 && NILP (glyph->object)
28352 && glyph->charpos < 0;
28353 ++glyph)
28354 x += glyph->pixel_width;
28356 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28357 or DISP_STRING, and the first glyph from buffer whose
28358 position is between START_CHARPOS and END_CHARPOS. */
28359 for (; glyph < end
28360 && !NILP (glyph->object)
28361 && !EQ (glyph->object, disp_string)
28362 && !(BUFFERP (glyph->object)
28363 && (glyph->charpos >= start_charpos
28364 && glyph->charpos < end_charpos));
28365 ++glyph)
28367 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28368 are present at buffer positions between START_CHARPOS and
28369 END_CHARPOS, or if they come from an overlay. */
28370 if (EQ (glyph->object, before_string))
28372 pos = string_buffer_position (before_string,
28373 start_charpos);
28374 /* If pos == 0, it means before_string came from an
28375 overlay, not from a buffer position. */
28376 if (!pos || (pos >= start_charpos && pos < end_charpos))
28377 break;
28379 else if (EQ (glyph->object, after_string))
28381 pos = string_buffer_position (after_string, end_charpos);
28382 if (!pos || (pos >= start_charpos && pos < end_charpos))
28383 break;
28385 x += glyph->pixel_width;
28387 hlinfo->mouse_face_beg_x = x;
28388 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28390 else
28392 /* This row is in a right to left paragraph. Scan it right to
28393 left. */
28394 struct glyph *g;
28396 end = r1->glyphs[TEXT_AREA] - 1;
28397 glyph = end + r1->used[TEXT_AREA];
28399 /* Skip truncation glyphs at the start of the glyph row. */
28400 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28401 for (; glyph > end
28402 && NILP (glyph->object)
28403 && glyph->charpos < 0;
28404 --glyph)
28407 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28408 or DISP_STRING, and the first glyph from buffer whose
28409 position is between START_CHARPOS and END_CHARPOS. */
28410 for (; glyph > end
28411 && !NILP (glyph->object)
28412 && !EQ (glyph->object, disp_string)
28413 && !(BUFFERP (glyph->object)
28414 && (glyph->charpos >= start_charpos
28415 && glyph->charpos < end_charpos));
28416 --glyph)
28418 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28419 are present at buffer positions between START_CHARPOS and
28420 END_CHARPOS, or if they come from an overlay. */
28421 if (EQ (glyph->object, before_string))
28423 pos = string_buffer_position (before_string, start_charpos);
28424 /* If pos == 0, it means before_string came from an
28425 overlay, not from a buffer position. */
28426 if (!pos || (pos >= start_charpos && pos < end_charpos))
28427 break;
28429 else if (EQ (glyph->object, after_string))
28431 pos = string_buffer_position (after_string, end_charpos);
28432 if (!pos || (pos >= start_charpos && pos < end_charpos))
28433 break;
28437 glyph++; /* first glyph to the right of the highlighted area */
28438 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28439 x += g->pixel_width;
28440 hlinfo->mouse_face_beg_x = x;
28441 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28444 /* If the highlight ends in a different row, compute GLYPH and END
28445 for the end row. Otherwise, reuse the values computed above for
28446 the row where the highlight begins. */
28447 if (r2 != r1)
28449 if (!r2->reversed_p)
28451 glyph = r2->glyphs[TEXT_AREA];
28452 end = glyph + r2->used[TEXT_AREA];
28453 x = r2->x;
28455 else
28457 end = r2->glyphs[TEXT_AREA] - 1;
28458 glyph = end + r2->used[TEXT_AREA];
28462 if (!r2->reversed_p)
28464 /* Skip truncation and continuation glyphs near the end of the
28465 row, and also blanks and stretch glyphs inserted by
28466 extend_face_to_end_of_line. */
28467 while (end > glyph
28468 && NILP ((end - 1)->object))
28469 --end;
28470 /* Scan the rest of the glyph row from the end, looking for the
28471 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28472 DISP_STRING, or whose position is between START_CHARPOS
28473 and END_CHARPOS */
28474 for (--end;
28475 end > glyph
28476 && !NILP (end->object)
28477 && !EQ (end->object, disp_string)
28478 && !(BUFFERP (end->object)
28479 && (end->charpos >= start_charpos
28480 && end->charpos < end_charpos));
28481 --end)
28483 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28484 are present at buffer positions between START_CHARPOS and
28485 END_CHARPOS, or if they come from an overlay. */
28486 if (EQ (end->object, before_string))
28488 pos = string_buffer_position (before_string, start_charpos);
28489 if (!pos || (pos >= start_charpos && pos < end_charpos))
28490 break;
28492 else if (EQ (end->object, after_string))
28494 pos = string_buffer_position (after_string, end_charpos);
28495 if (!pos || (pos >= start_charpos && pos < end_charpos))
28496 break;
28499 /* Find the X coordinate of the last glyph to be highlighted. */
28500 for (; glyph <= end; ++glyph)
28501 x += glyph->pixel_width;
28503 hlinfo->mouse_face_end_x = x;
28504 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28506 else
28508 /* Skip truncation and continuation glyphs near the end of the
28509 row, and also blanks and stretch glyphs inserted by
28510 extend_face_to_end_of_line. */
28511 x = r2->x;
28512 end++;
28513 while (end < glyph
28514 && NILP (end->object))
28516 x += end->pixel_width;
28517 ++end;
28519 /* Scan the rest of the glyph row from the end, looking for the
28520 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28521 DISP_STRING, or whose position is between START_CHARPOS
28522 and END_CHARPOS */
28523 for ( ;
28524 end < glyph
28525 && !NILP (end->object)
28526 && !EQ (end->object, disp_string)
28527 && !(BUFFERP (end->object)
28528 && (end->charpos >= start_charpos
28529 && end->charpos < end_charpos));
28530 ++end)
28532 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28533 are present at buffer positions between START_CHARPOS and
28534 END_CHARPOS, or if they come from an overlay. */
28535 if (EQ (end->object, before_string))
28537 pos = string_buffer_position (before_string, start_charpos);
28538 if (!pos || (pos >= start_charpos && pos < end_charpos))
28539 break;
28541 else if (EQ (end->object, after_string))
28543 pos = string_buffer_position (after_string, end_charpos);
28544 if (!pos || (pos >= start_charpos && pos < end_charpos))
28545 break;
28547 x += end->pixel_width;
28549 /* If we exited the above loop because we arrived at the last
28550 glyph of the row, and its buffer position is still not in
28551 range, it means the last character in range is the preceding
28552 newline. Bump the end column and x values to get past the
28553 last glyph. */
28554 if (end == glyph
28555 && BUFFERP (end->object)
28556 && (end->charpos < start_charpos
28557 || end->charpos >= end_charpos))
28559 x += end->pixel_width;
28560 ++end;
28562 hlinfo->mouse_face_end_x = x;
28563 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28566 hlinfo->mouse_face_window = window;
28567 hlinfo->mouse_face_face_id
28568 = face_at_buffer_position (w, mouse_charpos, &ignore,
28569 mouse_charpos + 1,
28570 !hlinfo->mouse_face_hidden, -1);
28571 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28574 /* The following function is not used anymore (replaced with
28575 mouse_face_from_string_pos), but I leave it here for the time
28576 being, in case someone would. */
28578 #if false /* not used */
28580 /* Find the position of the glyph for position POS in OBJECT in
28581 window W's current matrix, and return in *X, *Y the pixel
28582 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28584 RIGHT_P means return the position of the right edge of the glyph.
28585 !RIGHT_P means return the left edge position.
28587 If no glyph for POS exists in the matrix, return the position of
28588 the glyph with the next smaller position that is in the matrix, if
28589 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28590 exists in the matrix, return the position of the glyph with the
28591 next larger position in OBJECT.
28593 Value is true if a glyph was found. */
28595 static bool
28596 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28597 int *hpos, int *vpos, int *x, int *y, bool right_p)
28599 int yb = window_text_bottom_y (w);
28600 struct glyph_row *r;
28601 struct glyph *best_glyph = NULL;
28602 struct glyph_row *best_row = NULL;
28603 int best_x = 0;
28605 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28606 r->enabled_p && r->y < yb;
28607 ++r)
28609 struct glyph *g = r->glyphs[TEXT_AREA];
28610 struct glyph *e = g + r->used[TEXT_AREA];
28611 int gx;
28613 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28614 if (EQ (g->object, object))
28616 if (g->charpos == pos)
28618 best_glyph = g;
28619 best_x = gx;
28620 best_row = r;
28621 goto found;
28623 else if (best_glyph == NULL
28624 || ((eabs (g->charpos - pos)
28625 < eabs (best_glyph->charpos - pos))
28626 && (right_p
28627 ? g->charpos < pos
28628 : g->charpos > pos)))
28630 best_glyph = g;
28631 best_x = gx;
28632 best_row = r;
28637 found:
28639 if (best_glyph)
28641 *x = best_x;
28642 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28644 if (right_p)
28646 *x += best_glyph->pixel_width;
28647 ++*hpos;
28650 *y = best_row->y;
28651 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28654 return best_glyph != NULL;
28656 #endif /* not used */
28658 /* Find the positions of the first and the last glyphs in window W's
28659 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28660 (assumed to be a string), and return in HLINFO's mouse_face_*
28661 members the pixel and column/row coordinates of those glyphs. */
28663 static void
28664 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28665 Lisp_Object object,
28666 ptrdiff_t startpos, ptrdiff_t endpos)
28668 int yb = window_text_bottom_y (w);
28669 struct glyph_row *r;
28670 struct glyph *g, *e;
28671 int gx;
28672 bool found = false;
28674 /* Find the glyph row with at least one position in the range
28675 [STARTPOS..ENDPOS), and the first glyph in that row whose
28676 position belongs to that range. */
28677 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28678 r->enabled_p && r->y < yb;
28679 ++r)
28681 if (!r->reversed_p)
28683 g = r->glyphs[TEXT_AREA];
28684 e = g + r->used[TEXT_AREA];
28685 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28686 if (EQ (g->object, object)
28687 && startpos <= g->charpos && g->charpos < endpos)
28689 hlinfo->mouse_face_beg_row
28690 = MATRIX_ROW_VPOS (r, w->current_matrix);
28691 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28692 hlinfo->mouse_face_beg_x = gx;
28693 found = true;
28694 break;
28697 else
28699 struct glyph *g1;
28701 e = r->glyphs[TEXT_AREA];
28702 g = e + r->used[TEXT_AREA];
28703 for ( ; g > e; --g)
28704 if (EQ ((g-1)->object, object)
28705 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28707 hlinfo->mouse_face_beg_row
28708 = MATRIX_ROW_VPOS (r, w->current_matrix);
28709 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28710 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28711 gx += g1->pixel_width;
28712 hlinfo->mouse_face_beg_x = gx;
28713 found = true;
28714 break;
28717 if (found)
28718 break;
28721 if (!found)
28722 return;
28724 /* Starting with the next row, look for the first row which does NOT
28725 include any glyphs whose positions are in the range. */
28726 for (++r; r->enabled_p && r->y < yb; ++r)
28728 g = r->glyphs[TEXT_AREA];
28729 e = g + r->used[TEXT_AREA];
28730 found = false;
28731 for ( ; g < e; ++g)
28732 if (EQ (g->object, object)
28733 && startpos <= g->charpos && g->charpos < endpos)
28735 found = true;
28736 break;
28738 if (!found)
28739 break;
28742 /* The highlighted region ends on the previous row. */
28743 r--;
28745 /* Set the end row. */
28746 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28748 /* Compute and set the end column and the end column's horizontal
28749 pixel coordinate. */
28750 if (!r->reversed_p)
28752 g = r->glyphs[TEXT_AREA];
28753 e = g + r->used[TEXT_AREA];
28754 for ( ; e > g; --e)
28755 if (EQ ((e-1)->object, object)
28756 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28757 break;
28758 hlinfo->mouse_face_end_col = e - g;
28760 for (gx = r->x; g < e; ++g)
28761 gx += g->pixel_width;
28762 hlinfo->mouse_face_end_x = gx;
28764 else
28766 e = r->glyphs[TEXT_AREA];
28767 g = e + r->used[TEXT_AREA];
28768 for (gx = r->x ; e < g; ++e)
28770 if (EQ (e->object, object)
28771 && startpos <= e->charpos && e->charpos < endpos)
28772 break;
28773 gx += e->pixel_width;
28775 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28776 hlinfo->mouse_face_end_x = gx;
28780 #ifdef HAVE_WINDOW_SYSTEM
28782 /* See if position X, Y is within a hot-spot of an image. */
28784 static bool
28785 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28787 if (!CONSP (hot_spot))
28788 return false;
28790 if (EQ (XCAR (hot_spot), Qrect))
28792 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28793 Lisp_Object rect = XCDR (hot_spot);
28794 Lisp_Object tem;
28795 if (!CONSP (rect))
28796 return false;
28797 if (!CONSP (XCAR (rect)))
28798 return false;
28799 if (!CONSP (XCDR (rect)))
28800 return false;
28801 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28802 return false;
28803 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28804 return false;
28805 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28806 return false;
28807 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28808 return false;
28809 return true;
28811 else if (EQ (XCAR (hot_spot), Qcircle))
28813 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28814 Lisp_Object circ = XCDR (hot_spot);
28815 Lisp_Object lr, lx0, ly0;
28816 if (CONSP (circ)
28817 && CONSP (XCAR (circ))
28818 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28819 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28820 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28822 double r = XFLOATINT (lr);
28823 double dx = XINT (lx0) - x;
28824 double dy = XINT (ly0) - y;
28825 return (dx * dx + dy * dy <= r * r);
28828 else if (EQ (XCAR (hot_spot), Qpoly))
28830 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28831 if (VECTORP (XCDR (hot_spot)))
28833 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28834 Lisp_Object *poly = v->contents;
28835 ptrdiff_t n = v->header.size;
28836 ptrdiff_t i;
28837 bool inside = false;
28838 Lisp_Object lx, ly;
28839 int x0, y0;
28841 /* Need an even number of coordinates, and at least 3 edges. */
28842 if (n < 6 || n & 1)
28843 return false;
28845 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28846 If count is odd, we are inside polygon. Pixels on edges
28847 may or may not be included depending on actual geometry of the
28848 polygon. */
28849 if ((lx = poly[n-2], !INTEGERP (lx))
28850 || (ly = poly[n-1], !INTEGERP (lx)))
28851 return false;
28852 x0 = XINT (lx), y0 = XINT (ly);
28853 for (i = 0; i < n; i += 2)
28855 int x1 = x0, y1 = y0;
28856 if ((lx = poly[i], !INTEGERP (lx))
28857 || (ly = poly[i+1], !INTEGERP (ly)))
28858 return false;
28859 x0 = XINT (lx), y0 = XINT (ly);
28861 /* Does this segment cross the X line? */
28862 if (x0 >= x)
28864 if (x1 >= x)
28865 continue;
28867 else if (x1 < x)
28868 continue;
28869 if (y > y0 && y > y1)
28870 continue;
28871 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28872 inside = !inside;
28874 return inside;
28877 return false;
28880 Lisp_Object
28881 find_hot_spot (Lisp_Object map, int x, int y)
28883 while (CONSP (map))
28885 if (CONSP (XCAR (map))
28886 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28887 return XCAR (map);
28888 map = XCDR (map);
28891 return Qnil;
28894 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28895 3, 3, 0,
28896 doc: /* Lookup in image map MAP coordinates X and Y.
28897 An image map is an alist where each element has the format (AREA ID PLIST).
28898 An AREA is specified as either a rectangle, a circle, or a polygon:
28899 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28900 pixel coordinates of the upper left and bottom right corners.
28901 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28902 and the radius of the circle; r may be a float or integer.
28903 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28904 vector describes one corner in the polygon.
28905 Returns the alist element for the first matching AREA in MAP. */)
28906 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28908 if (NILP (map))
28909 return Qnil;
28911 CHECK_NUMBER (x);
28912 CHECK_NUMBER (y);
28914 return find_hot_spot (map,
28915 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28916 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28920 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28921 static void
28922 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28924 /* Do not change cursor shape while dragging mouse. */
28925 if (!NILP (do_mouse_tracking))
28926 return;
28928 if (!NILP (pointer))
28930 if (EQ (pointer, Qarrow))
28931 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28932 else if (EQ (pointer, Qhand))
28933 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28934 else if (EQ (pointer, Qtext))
28935 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28936 else if (EQ (pointer, intern ("hdrag")))
28937 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28938 else if (EQ (pointer, intern ("nhdrag")))
28939 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28940 #ifdef HAVE_X_WINDOWS
28941 else if (EQ (pointer, intern ("vdrag")))
28942 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28943 #endif
28944 else if (EQ (pointer, intern ("hourglass")))
28945 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28946 else if (EQ (pointer, Qmodeline))
28947 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28948 else
28949 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28952 if (cursor != No_Cursor)
28953 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28956 #endif /* HAVE_WINDOW_SYSTEM */
28958 /* Take proper action when mouse has moved to the mode or header line
28959 or marginal area AREA of window W, x-position X and y-position Y.
28960 X is relative to the start of the text display area of W, so the
28961 width of bitmap areas and scroll bars must be subtracted to get a
28962 position relative to the start of the mode line. */
28964 static void
28965 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28966 enum window_part area)
28968 struct window *w = XWINDOW (window);
28969 struct frame *f = XFRAME (w->frame);
28970 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28971 #ifdef HAVE_WINDOW_SYSTEM
28972 Display_Info *dpyinfo;
28973 #endif
28974 Cursor cursor = No_Cursor;
28975 Lisp_Object pointer = Qnil;
28976 int dx, dy, width, height;
28977 ptrdiff_t charpos;
28978 Lisp_Object string, object = Qnil;
28979 Lisp_Object pos IF_LINT (= Qnil), help;
28981 Lisp_Object mouse_face;
28982 int original_x_pixel = x;
28983 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28984 struct glyph_row *row IF_LINT (= 0);
28986 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28988 int x0;
28989 struct glyph *end;
28991 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28992 returns them in row/column units! */
28993 string = mode_line_string (w, area, &x, &y, &charpos,
28994 &object, &dx, &dy, &width, &height);
28996 row = (area == ON_MODE_LINE
28997 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28998 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29000 /* Find the glyph under the mouse pointer. */
29001 if (row->mode_line_p && row->enabled_p)
29003 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29004 end = glyph + row->used[TEXT_AREA];
29006 for (x0 = original_x_pixel;
29007 glyph < end && x0 >= glyph->pixel_width;
29008 ++glyph)
29009 x0 -= glyph->pixel_width;
29011 if (glyph >= end)
29012 glyph = NULL;
29015 else
29017 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29018 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29019 returns them in row/column units! */
29020 string = marginal_area_string (w, area, &x, &y, &charpos,
29021 &object, &dx, &dy, &width, &height);
29024 help = Qnil;
29026 #ifdef HAVE_WINDOW_SYSTEM
29027 if (IMAGEP (object))
29029 Lisp_Object image_map, hotspot;
29030 if ((image_map = Fplist_get (XCDR (object), QCmap),
29031 !NILP (image_map))
29032 && (hotspot = find_hot_spot (image_map, dx, dy),
29033 CONSP (hotspot))
29034 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29036 Lisp_Object plist;
29038 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29039 If so, we could look for mouse-enter, mouse-leave
29040 properties in PLIST (and do something...). */
29041 hotspot = XCDR (hotspot);
29042 if (CONSP (hotspot)
29043 && (plist = XCAR (hotspot), CONSP (plist)))
29045 pointer = Fplist_get (plist, Qpointer);
29046 if (NILP (pointer))
29047 pointer = Qhand;
29048 help = Fplist_get (plist, Qhelp_echo);
29049 if (!NILP (help))
29051 help_echo_string = help;
29052 XSETWINDOW (help_echo_window, w);
29053 help_echo_object = w->contents;
29054 help_echo_pos = charpos;
29058 if (NILP (pointer))
29059 pointer = Fplist_get (XCDR (object), QCpointer);
29061 #endif /* HAVE_WINDOW_SYSTEM */
29063 if (STRINGP (string))
29064 pos = make_number (charpos);
29066 /* Set the help text and mouse pointer. If the mouse is on a part
29067 of the mode line without any text (e.g. past the right edge of
29068 the mode line text), use the default help text and pointer. */
29069 if (STRINGP (string) || area == ON_MODE_LINE)
29071 /* Arrange to display the help by setting the global variables
29072 help_echo_string, help_echo_object, and help_echo_pos. */
29073 if (NILP (help))
29075 if (STRINGP (string))
29076 help = Fget_text_property (pos, Qhelp_echo, string);
29078 if (!NILP (help))
29080 help_echo_string = help;
29081 XSETWINDOW (help_echo_window, w);
29082 help_echo_object = string;
29083 help_echo_pos = charpos;
29085 else if (area == ON_MODE_LINE)
29087 Lisp_Object default_help
29088 = buffer_local_value (Qmode_line_default_help_echo,
29089 w->contents);
29091 if (STRINGP (default_help))
29093 help_echo_string = default_help;
29094 XSETWINDOW (help_echo_window, w);
29095 help_echo_object = Qnil;
29096 help_echo_pos = -1;
29101 #ifdef HAVE_WINDOW_SYSTEM
29102 /* Change the mouse pointer according to what is under it. */
29103 if (FRAME_WINDOW_P (f))
29105 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29106 || minibuf_level
29107 || NILP (Vresize_mini_windows));
29109 dpyinfo = FRAME_DISPLAY_INFO (f);
29110 if (STRINGP (string))
29112 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29114 if (NILP (pointer))
29115 pointer = Fget_text_property (pos, Qpointer, string);
29117 /* Change the mouse pointer according to what is under X/Y. */
29118 if (NILP (pointer)
29119 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29121 Lisp_Object map;
29122 map = Fget_text_property (pos, Qlocal_map, string);
29123 if (!KEYMAPP (map))
29124 map = Fget_text_property (pos, Qkeymap, string);
29125 if (!KEYMAPP (map) && draggable)
29126 cursor = dpyinfo->vertical_scroll_bar_cursor;
29129 else if (draggable)
29130 /* Default mode-line pointer. */
29131 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29133 #endif
29136 /* Change the mouse face according to what is under X/Y. */
29137 if (STRINGP (string))
29139 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29140 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29141 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29142 && glyph)
29144 Lisp_Object b, e;
29146 struct glyph * tmp_glyph;
29148 int gpos;
29149 int gseq_length;
29150 int total_pixel_width;
29151 ptrdiff_t begpos, endpos, ignore;
29153 int vpos, hpos;
29155 b = Fprevious_single_property_change (make_number (charpos + 1),
29156 Qmouse_face, string, Qnil);
29157 if (NILP (b))
29158 begpos = 0;
29159 else
29160 begpos = XINT (b);
29162 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29163 if (NILP (e))
29164 endpos = SCHARS (string);
29165 else
29166 endpos = XINT (e);
29168 /* Calculate the glyph position GPOS of GLYPH in the
29169 displayed string, relative to the beginning of the
29170 highlighted part of the string.
29172 Note: GPOS is different from CHARPOS. CHARPOS is the
29173 position of GLYPH in the internal string object. A mode
29174 line string format has structures which are converted to
29175 a flattened string by the Emacs Lisp interpreter. The
29176 internal string is an element of those structures. The
29177 displayed string is the flattened string. */
29178 tmp_glyph = row_start_glyph;
29179 while (tmp_glyph < glyph
29180 && (!(EQ (tmp_glyph->object, glyph->object)
29181 && begpos <= tmp_glyph->charpos
29182 && tmp_glyph->charpos < endpos)))
29183 tmp_glyph++;
29184 gpos = glyph - tmp_glyph;
29186 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29187 the highlighted part of the displayed string to which
29188 GLYPH belongs. Note: GSEQ_LENGTH is different from
29189 SCHARS (STRING), because the latter returns the length of
29190 the internal string. */
29191 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29192 tmp_glyph > glyph
29193 && (!(EQ (tmp_glyph->object, glyph->object)
29194 && begpos <= tmp_glyph->charpos
29195 && tmp_glyph->charpos < endpos));
29196 tmp_glyph--)
29198 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29200 /* Calculate the total pixel width of all the glyphs between
29201 the beginning of the highlighted area and GLYPH. */
29202 total_pixel_width = 0;
29203 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29204 total_pixel_width += tmp_glyph->pixel_width;
29206 /* Pre calculation of re-rendering position. Note: X is in
29207 column units here, after the call to mode_line_string or
29208 marginal_area_string. */
29209 hpos = x - gpos;
29210 vpos = (area == ON_MODE_LINE
29211 ? (w->current_matrix)->nrows - 1
29212 : 0);
29214 /* If GLYPH's position is included in the region that is
29215 already drawn in mouse face, we have nothing to do. */
29216 if ( EQ (window, hlinfo->mouse_face_window)
29217 && (!row->reversed_p
29218 ? (hlinfo->mouse_face_beg_col <= hpos
29219 && hpos < hlinfo->mouse_face_end_col)
29220 /* In R2L rows we swap BEG and END, see below. */
29221 : (hlinfo->mouse_face_end_col <= hpos
29222 && hpos < hlinfo->mouse_face_beg_col))
29223 && hlinfo->mouse_face_beg_row == vpos )
29224 return;
29226 if (clear_mouse_face (hlinfo))
29227 cursor = No_Cursor;
29229 if (!row->reversed_p)
29231 hlinfo->mouse_face_beg_col = hpos;
29232 hlinfo->mouse_face_beg_x = original_x_pixel
29233 - (total_pixel_width + dx);
29234 hlinfo->mouse_face_end_col = hpos + gseq_length;
29235 hlinfo->mouse_face_end_x = 0;
29237 else
29239 /* In R2L rows, show_mouse_face expects BEG and END
29240 coordinates to be swapped. */
29241 hlinfo->mouse_face_end_col = hpos;
29242 hlinfo->mouse_face_end_x = original_x_pixel
29243 - (total_pixel_width + dx);
29244 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29245 hlinfo->mouse_face_beg_x = 0;
29248 hlinfo->mouse_face_beg_row = vpos;
29249 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29250 hlinfo->mouse_face_past_end = false;
29251 hlinfo->mouse_face_window = window;
29253 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29254 charpos,
29255 0, &ignore,
29256 glyph->face_id,
29257 true);
29258 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29260 if (NILP (pointer))
29261 pointer = Qhand;
29263 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29264 clear_mouse_face (hlinfo);
29266 #ifdef HAVE_WINDOW_SYSTEM
29267 if (FRAME_WINDOW_P (f))
29268 define_frame_cursor1 (f, cursor, pointer);
29269 #endif
29273 /* EXPORT:
29274 Take proper action when the mouse has moved to position X, Y on
29275 frame F with regards to highlighting portions of display that have
29276 mouse-face properties. Also de-highlight portions of display where
29277 the mouse was before, set the mouse pointer shape as appropriate
29278 for the mouse coordinates, and activate help echo (tooltips).
29279 X and Y can be negative or out of range. */
29281 void
29282 note_mouse_highlight (struct frame *f, int x, int y)
29284 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29285 enum window_part part = ON_NOTHING;
29286 Lisp_Object window;
29287 struct window *w;
29288 Cursor cursor = No_Cursor;
29289 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29290 struct buffer *b;
29292 /* When a menu is active, don't highlight because this looks odd. */
29293 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29294 if (popup_activated ())
29295 return;
29296 #endif
29298 if (!f->glyphs_initialized_p
29299 || f->pointer_invisible)
29300 return;
29302 hlinfo->mouse_face_mouse_x = x;
29303 hlinfo->mouse_face_mouse_y = y;
29304 hlinfo->mouse_face_mouse_frame = f;
29306 if (hlinfo->mouse_face_defer)
29307 return;
29309 /* Which window is that in? */
29310 window = window_from_coordinates (f, x, y, &part, true);
29312 /* If displaying active text in another window, clear that. */
29313 if (! EQ (window, hlinfo->mouse_face_window)
29314 /* Also clear if we move out of text area in same window. */
29315 || (!NILP (hlinfo->mouse_face_window)
29316 && !NILP (window)
29317 && part != ON_TEXT
29318 && part != ON_MODE_LINE
29319 && part != ON_HEADER_LINE))
29320 clear_mouse_face (hlinfo);
29322 /* Not on a window -> return. */
29323 if (!WINDOWP (window))
29324 return;
29326 /* Reset help_echo_string. It will get recomputed below. */
29327 help_echo_string = Qnil;
29329 /* Convert to window-relative pixel coordinates. */
29330 w = XWINDOW (window);
29331 frame_to_window_pixel_xy (w, &x, &y);
29333 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29334 /* Handle tool-bar window differently since it doesn't display a
29335 buffer. */
29336 if (EQ (window, f->tool_bar_window))
29338 note_tool_bar_highlight (f, x, y);
29339 return;
29341 #endif
29343 /* Mouse is on the mode, header line or margin? */
29344 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29345 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29347 note_mode_line_or_margin_highlight (window, x, y, part);
29349 #ifdef HAVE_WINDOW_SYSTEM
29350 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29352 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29353 /* Show non-text cursor (Bug#16647). */
29354 goto set_cursor;
29356 else
29357 #endif
29358 return;
29361 #ifdef HAVE_WINDOW_SYSTEM
29362 if (part == ON_VERTICAL_BORDER)
29364 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29365 help_echo_string = build_string ("drag-mouse-1: resize");
29367 else if (part == ON_RIGHT_DIVIDER)
29369 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29370 help_echo_string = build_string ("drag-mouse-1: resize");
29372 else if (part == ON_BOTTOM_DIVIDER)
29373 if (! WINDOW_BOTTOMMOST_P (w)
29374 || minibuf_level
29375 || NILP (Vresize_mini_windows))
29377 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29378 help_echo_string = build_string ("drag-mouse-1: resize");
29380 else
29381 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29382 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29383 || part == ON_VERTICAL_SCROLL_BAR
29384 || part == ON_HORIZONTAL_SCROLL_BAR)
29385 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29386 else
29387 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29388 #endif
29390 /* Are we in a window whose display is up to date?
29391 And verify the buffer's text has not changed. */
29392 b = XBUFFER (w->contents);
29393 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29395 int hpos, vpos, dx, dy, area = LAST_AREA;
29396 ptrdiff_t pos;
29397 struct glyph *glyph;
29398 Lisp_Object object;
29399 Lisp_Object mouse_face = Qnil, position;
29400 Lisp_Object *overlay_vec = NULL;
29401 ptrdiff_t i, noverlays;
29402 struct buffer *obuf;
29403 ptrdiff_t obegv, ozv;
29404 bool same_region;
29406 /* Find the glyph under X/Y. */
29407 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29409 #ifdef HAVE_WINDOW_SYSTEM
29410 /* Look for :pointer property on image. */
29411 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29413 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29414 if (img != NULL && IMAGEP (img->spec))
29416 Lisp_Object image_map, hotspot;
29417 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29418 !NILP (image_map))
29419 && (hotspot = find_hot_spot (image_map,
29420 glyph->slice.img.x + dx,
29421 glyph->slice.img.y + dy),
29422 CONSP (hotspot))
29423 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29425 Lisp_Object plist;
29427 /* Could check XCAR (hotspot) to see if we enter/leave
29428 this hot-spot.
29429 If so, we could look for mouse-enter, mouse-leave
29430 properties in PLIST (and do something...). */
29431 hotspot = XCDR (hotspot);
29432 if (CONSP (hotspot)
29433 && (plist = XCAR (hotspot), CONSP (plist)))
29435 pointer = Fplist_get (plist, Qpointer);
29436 if (NILP (pointer))
29437 pointer = Qhand;
29438 help_echo_string = Fplist_get (plist, Qhelp_echo);
29439 if (!NILP (help_echo_string))
29441 help_echo_window = window;
29442 help_echo_object = glyph->object;
29443 help_echo_pos = glyph->charpos;
29447 if (NILP (pointer))
29448 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29451 #endif /* HAVE_WINDOW_SYSTEM */
29453 /* Clear mouse face if X/Y not over text. */
29454 if (glyph == NULL
29455 || area != TEXT_AREA
29456 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29457 /* Glyph's OBJECT is nil for glyphs inserted by the
29458 display engine for its internal purposes, like truncation
29459 and continuation glyphs and blanks beyond the end of
29460 line's text on text terminals. If we are over such a
29461 glyph, we are not over any text. */
29462 || NILP (glyph->object)
29463 /* R2L rows have a stretch glyph at their front, which
29464 stands for no text, whereas L2R rows have no glyphs at
29465 all beyond the end of text. Treat such stretch glyphs
29466 like we do with NULL glyphs in L2R rows. */
29467 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29468 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29469 && glyph->type == STRETCH_GLYPH
29470 && glyph->avoid_cursor_p))
29472 if (clear_mouse_face (hlinfo))
29473 cursor = No_Cursor;
29474 #ifdef HAVE_WINDOW_SYSTEM
29475 if (FRAME_WINDOW_P (f) && NILP (pointer))
29477 if (area != TEXT_AREA)
29478 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29479 else
29480 pointer = Vvoid_text_area_pointer;
29482 #endif
29483 goto set_cursor;
29486 pos = glyph->charpos;
29487 object = glyph->object;
29488 if (!STRINGP (object) && !BUFFERP (object))
29489 goto set_cursor;
29491 /* If we get an out-of-range value, return now; avoid an error. */
29492 if (BUFFERP (object) && pos > BUF_Z (b))
29493 goto set_cursor;
29495 /* Make the window's buffer temporarily current for
29496 overlays_at and compute_char_face. */
29497 obuf = current_buffer;
29498 current_buffer = b;
29499 obegv = BEGV;
29500 ozv = ZV;
29501 BEGV = BEG;
29502 ZV = Z;
29504 /* Is this char mouse-active or does it have help-echo? */
29505 position = make_number (pos);
29507 USE_SAFE_ALLOCA;
29509 if (BUFFERP (object))
29511 /* Put all the overlays we want in a vector in overlay_vec. */
29512 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29513 /* Sort overlays into increasing priority order. */
29514 noverlays = sort_overlays (overlay_vec, noverlays, w);
29516 else
29517 noverlays = 0;
29519 if (NILP (Vmouse_highlight))
29521 clear_mouse_face (hlinfo);
29522 goto check_help_echo;
29525 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29527 if (same_region)
29528 cursor = No_Cursor;
29530 /* Check mouse-face highlighting. */
29531 if (! same_region
29532 /* If there exists an overlay with mouse-face overlapping
29533 the one we are currently highlighting, we have to
29534 check if we enter the overlapping overlay, and then
29535 highlight only that. */
29536 || (OVERLAYP (hlinfo->mouse_face_overlay)
29537 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29539 /* Find the highest priority overlay with a mouse-face. */
29540 Lisp_Object overlay = Qnil;
29541 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29543 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29544 if (!NILP (mouse_face))
29545 overlay = overlay_vec[i];
29548 /* If we're highlighting the same overlay as before, there's
29549 no need to do that again. */
29550 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29551 goto check_help_echo;
29552 hlinfo->mouse_face_overlay = overlay;
29554 /* Clear the display of the old active region, if any. */
29555 if (clear_mouse_face (hlinfo))
29556 cursor = No_Cursor;
29558 /* If no overlay applies, get a text property. */
29559 if (NILP (overlay))
29560 mouse_face = Fget_text_property (position, Qmouse_face, object);
29562 /* Next, compute the bounds of the mouse highlighting and
29563 display it. */
29564 if (!NILP (mouse_face) && STRINGP (object))
29566 /* The mouse-highlighting comes from a display string
29567 with a mouse-face. */
29568 Lisp_Object s, e;
29569 ptrdiff_t ignore;
29571 s = Fprevious_single_property_change
29572 (make_number (pos + 1), Qmouse_face, object, Qnil);
29573 e = Fnext_single_property_change
29574 (position, Qmouse_face, object, Qnil);
29575 if (NILP (s))
29576 s = make_number (0);
29577 if (NILP (e))
29578 e = make_number (SCHARS (object));
29579 mouse_face_from_string_pos (w, hlinfo, object,
29580 XINT (s), XINT (e));
29581 hlinfo->mouse_face_past_end = false;
29582 hlinfo->mouse_face_window = window;
29583 hlinfo->mouse_face_face_id
29584 = face_at_string_position (w, object, pos, 0, &ignore,
29585 glyph->face_id, true);
29586 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29587 cursor = No_Cursor;
29589 else
29591 /* The mouse-highlighting, if any, comes from an overlay
29592 or text property in the buffer. */
29593 Lisp_Object buffer IF_LINT (= Qnil);
29594 Lisp_Object disp_string IF_LINT (= Qnil);
29596 if (STRINGP (object))
29598 /* If we are on a display string with no mouse-face,
29599 check if the text under it has one. */
29600 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29601 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29602 pos = string_buffer_position (object, start);
29603 if (pos > 0)
29605 mouse_face = get_char_property_and_overlay
29606 (make_number (pos), Qmouse_face, w->contents, &overlay);
29607 buffer = w->contents;
29608 disp_string = object;
29611 else
29613 buffer = object;
29614 disp_string = Qnil;
29617 if (!NILP (mouse_face))
29619 Lisp_Object before, after;
29620 Lisp_Object before_string, after_string;
29621 /* To correctly find the limits of mouse highlight
29622 in a bidi-reordered buffer, we must not use the
29623 optimization of limiting the search in
29624 previous-single-property-change and
29625 next-single-property-change, because
29626 rows_from_pos_range needs the real start and end
29627 positions to DTRT in this case. That's because
29628 the first row visible in a window does not
29629 necessarily display the character whose position
29630 is the smallest. */
29631 Lisp_Object lim1
29632 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29633 ? Fmarker_position (w->start)
29634 : Qnil;
29635 Lisp_Object lim2
29636 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29637 ? make_number (BUF_Z (XBUFFER (buffer))
29638 - w->window_end_pos)
29639 : Qnil;
29641 if (NILP (overlay))
29643 /* Handle the text property case. */
29644 before = Fprevious_single_property_change
29645 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29646 after = Fnext_single_property_change
29647 (make_number (pos), Qmouse_face, buffer, lim2);
29648 before_string = after_string = Qnil;
29650 else
29652 /* Handle the overlay case. */
29653 before = Foverlay_start (overlay);
29654 after = Foverlay_end (overlay);
29655 before_string = Foverlay_get (overlay, Qbefore_string);
29656 after_string = Foverlay_get (overlay, Qafter_string);
29658 if (!STRINGP (before_string)) before_string = Qnil;
29659 if (!STRINGP (after_string)) after_string = Qnil;
29662 mouse_face_from_buffer_pos (window, hlinfo, pos,
29663 NILP (before)
29665 : XFASTINT (before),
29666 NILP (after)
29667 ? BUF_Z (XBUFFER (buffer))
29668 : XFASTINT (after),
29669 before_string, after_string,
29670 disp_string);
29671 cursor = No_Cursor;
29676 check_help_echo:
29678 /* Look for a `help-echo' property. */
29679 if (NILP (help_echo_string)) {
29680 Lisp_Object help, overlay;
29682 /* Check overlays first. */
29683 help = overlay = Qnil;
29684 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29686 overlay = overlay_vec[i];
29687 help = Foverlay_get (overlay, Qhelp_echo);
29690 if (!NILP (help))
29692 help_echo_string = help;
29693 help_echo_window = window;
29694 help_echo_object = overlay;
29695 help_echo_pos = pos;
29697 else
29699 Lisp_Object obj = glyph->object;
29700 ptrdiff_t charpos = glyph->charpos;
29702 /* Try text properties. */
29703 if (STRINGP (obj)
29704 && charpos >= 0
29705 && charpos < SCHARS (obj))
29707 help = Fget_text_property (make_number (charpos),
29708 Qhelp_echo, obj);
29709 if (NILP (help))
29711 /* If the string itself doesn't specify a help-echo,
29712 see if the buffer text ``under'' it does. */
29713 struct glyph_row *r
29714 = MATRIX_ROW (w->current_matrix, vpos);
29715 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29716 ptrdiff_t p = string_buffer_position (obj, start);
29717 if (p > 0)
29719 help = Fget_char_property (make_number (p),
29720 Qhelp_echo, w->contents);
29721 if (!NILP (help))
29723 charpos = p;
29724 obj = w->contents;
29729 else if (BUFFERP (obj)
29730 && charpos >= BEGV
29731 && charpos < ZV)
29732 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29733 obj);
29735 if (!NILP (help))
29737 help_echo_string = help;
29738 help_echo_window = window;
29739 help_echo_object = obj;
29740 help_echo_pos = charpos;
29745 #ifdef HAVE_WINDOW_SYSTEM
29746 /* Look for a `pointer' property. */
29747 if (FRAME_WINDOW_P (f) && NILP (pointer))
29749 /* Check overlays first. */
29750 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29751 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29753 if (NILP (pointer))
29755 Lisp_Object obj = glyph->object;
29756 ptrdiff_t charpos = glyph->charpos;
29758 /* Try text properties. */
29759 if (STRINGP (obj)
29760 && charpos >= 0
29761 && charpos < SCHARS (obj))
29763 pointer = Fget_text_property (make_number (charpos),
29764 Qpointer, obj);
29765 if (NILP (pointer))
29767 /* If the string itself doesn't specify a pointer,
29768 see if the buffer text ``under'' it does. */
29769 struct glyph_row *r
29770 = MATRIX_ROW (w->current_matrix, vpos);
29771 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29772 ptrdiff_t p = string_buffer_position (obj, start);
29773 if (p > 0)
29774 pointer = Fget_char_property (make_number (p),
29775 Qpointer, w->contents);
29778 else if (BUFFERP (obj)
29779 && charpos >= BEGV
29780 && charpos < ZV)
29781 pointer = Fget_text_property (make_number (charpos),
29782 Qpointer, obj);
29785 #endif /* HAVE_WINDOW_SYSTEM */
29787 BEGV = obegv;
29788 ZV = ozv;
29789 current_buffer = obuf;
29790 SAFE_FREE ();
29793 set_cursor:
29795 #ifdef HAVE_WINDOW_SYSTEM
29796 if (FRAME_WINDOW_P (f))
29797 define_frame_cursor1 (f, cursor, pointer);
29798 #else
29799 /* This is here to prevent a compiler error, about "label at end of
29800 compound statement". */
29801 return;
29802 #endif
29806 /* EXPORT for RIF:
29807 Clear any mouse-face on window W. This function is part of the
29808 redisplay interface, and is called from try_window_id and similar
29809 functions to ensure the mouse-highlight is off. */
29811 void
29812 x_clear_window_mouse_face (struct window *w)
29814 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29815 Lisp_Object window;
29817 block_input ();
29818 XSETWINDOW (window, w);
29819 if (EQ (window, hlinfo->mouse_face_window))
29820 clear_mouse_face (hlinfo);
29821 unblock_input ();
29825 /* EXPORT:
29826 Just discard the mouse face information for frame F, if any.
29827 This is used when the size of F is changed. */
29829 void
29830 cancel_mouse_face (struct frame *f)
29832 Lisp_Object window;
29833 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29835 window = hlinfo->mouse_face_window;
29836 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29837 reset_mouse_highlight (hlinfo);
29842 /***********************************************************************
29843 Exposure Events
29844 ***********************************************************************/
29846 #ifdef HAVE_WINDOW_SYSTEM
29848 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29849 which intersects rectangle R. R is in window-relative coordinates. */
29851 static void
29852 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29853 enum glyph_row_area area)
29855 struct glyph *first = row->glyphs[area];
29856 struct glyph *end = row->glyphs[area] + row->used[area];
29857 struct glyph *last;
29858 int first_x, start_x, x;
29860 if (area == TEXT_AREA && row->fill_line_p)
29861 /* If row extends face to end of line write the whole line. */
29862 draw_glyphs (w, 0, row, area,
29863 0, row->used[area],
29864 DRAW_NORMAL_TEXT, 0);
29865 else
29867 /* Set START_X to the window-relative start position for drawing glyphs of
29868 AREA. The first glyph of the text area can be partially visible.
29869 The first glyphs of other areas cannot. */
29870 start_x = window_box_left_offset (w, area);
29871 x = start_x;
29872 if (area == TEXT_AREA)
29873 x += row->x;
29875 /* Find the first glyph that must be redrawn. */
29876 while (first < end
29877 && x + first->pixel_width < r->x)
29879 x += first->pixel_width;
29880 ++first;
29883 /* Find the last one. */
29884 last = first;
29885 first_x = x;
29886 while (last < end
29887 && x < r->x + r->width)
29889 x += last->pixel_width;
29890 ++last;
29893 /* Repaint. */
29894 if (last > first)
29895 draw_glyphs (w, first_x - start_x, row, area,
29896 first - row->glyphs[area], last - row->glyphs[area],
29897 DRAW_NORMAL_TEXT, 0);
29902 /* Redraw the parts of the glyph row ROW on window W intersecting
29903 rectangle R. R is in window-relative coordinates. Value is
29904 true if mouse-face was overwritten. */
29906 static bool
29907 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29909 eassert (row->enabled_p);
29911 if (row->mode_line_p || w->pseudo_window_p)
29912 draw_glyphs (w, 0, row, TEXT_AREA,
29913 0, row->used[TEXT_AREA],
29914 DRAW_NORMAL_TEXT, 0);
29915 else
29917 if (row->used[LEFT_MARGIN_AREA])
29918 expose_area (w, row, r, LEFT_MARGIN_AREA);
29919 if (row->used[TEXT_AREA])
29920 expose_area (w, row, r, TEXT_AREA);
29921 if (row->used[RIGHT_MARGIN_AREA])
29922 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29923 draw_row_fringe_bitmaps (w, row);
29926 return row->mouse_face_p;
29930 /* Redraw those parts of glyphs rows during expose event handling that
29931 overlap other rows. Redrawing of an exposed line writes over parts
29932 of lines overlapping that exposed line; this function fixes that.
29934 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29935 row in W's current matrix that is exposed and overlaps other rows.
29936 LAST_OVERLAPPING_ROW is the last such row. */
29938 static void
29939 expose_overlaps (struct window *w,
29940 struct glyph_row *first_overlapping_row,
29941 struct glyph_row *last_overlapping_row,
29942 XRectangle *r)
29944 struct glyph_row *row;
29946 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29947 if (row->overlapping_p)
29949 eassert (row->enabled_p && !row->mode_line_p);
29951 row->clip = r;
29952 if (row->used[LEFT_MARGIN_AREA])
29953 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29955 if (row->used[TEXT_AREA])
29956 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29958 if (row->used[RIGHT_MARGIN_AREA])
29959 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29960 row->clip = NULL;
29965 /* Return true if W's cursor intersects rectangle R. */
29967 static bool
29968 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29970 XRectangle cr, result;
29971 struct glyph *cursor_glyph;
29972 struct glyph_row *row;
29974 if (w->phys_cursor.vpos >= 0
29975 && w->phys_cursor.vpos < w->current_matrix->nrows
29976 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29977 row->enabled_p)
29978 && row->cursor_in_fringe_p)
29980 /* Cursor is in the fringe. */
29981 cr.x = window_box_right_offset (w,
29982 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29983 ? RIGHT_MARGIN_AREA
29984 : TEXT_AREA));
29985 cr.y = row->y;
29986 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29987 cr.height = row->height;
29988 return x_intersect_rectangles (&cr, r, &result);
29991 cursor_glyph = get_phys_cursor_glyph (w);
29992 if (cursor_glyph)
29994 /* r is relative to W's box, but w->phys_cursor.x is relative
29995 to left edge of W's TEXT area. Adjust it. */
29996 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29997 cr.y = w->phys_cursor.y;
29998 cr.width = cursor_glyph->pixel_width;
29999 cr.height = w->phys_cursor_height;
30000 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30001 I assume the effect is the same -- and this is portable. */
30002 return x_intersect_rectangles (&cr, r, &result);
30004 /* If we don't understand the format, pretend we're not in the hot-spot. */
30005 return false;
30009 /* EXPORT:
30010 Draw a vertical window border to the right of window W if W doesn't
30011 have vertical scroll bars. */
30013 void
30014 x_draw_vertical_border (struct window *w)
30016 struct frame *f = XFRAME (WINDOW_FRAME (w));
30018 /* We could do better, if we knew what type of scroll-bar the adjacent
30019 windows (on either side) have... But we don't :-(
30020 However, I think this works ok. ++KFS 2003-04-25 */
30022 /* Redraw borders between horizontally adjacent windows. Don't
30023 do it for frames with vertical scroll bars because either the
30024 right scroll bar of a window, or the left scroll bar of its
30025 neighbor will suffice as a border. */
30026 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30027 return;
30029 /* Note: It is necessary to redraw both the left and the right
30030 borders, for when only this single window W is being
30031 redisplayed. */
30032 if (!WINDOW_RIGHTMOST_P (w)
30033 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30035 int x0, x1, y0, y1;
30037 window_box_edges (w, &x0, &y0, &x1, &y1);
30038 y1 -= 1;
30040 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30041 x1 -= 1;
30043 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30046 if (!WINDOW_LEFTMOST_P (w)
30047 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30049 int x0, x1, y0, y1;
30051 window_box_edges (w, &x0, &y0, &x1, &y1);
30052 y1 -= 1;
30054 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30055 x0 -= 1;
30057 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30062 /* Draw window dividers for window W. */
30064 void
30065 x_draw_right_divider (struct window *w)
30067 struct frame *f = WINDOW_XFRAME (w);
30069 if (w->mini || w->pseudo_window_p)
30070 return;
30071 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30073 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30074 int x1 = WINDOW_RIGHT_EDGE_X (w);
30075 int y0 = WINDOW_TOP_EDGE_Y (w);
30076 /* The bottom divider prevails. */
30077 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30079 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30083 static void
30084 x_draw_bottom_divider (struct window *w)
30086 struct frame *f = XFRAME (WINDOW_FRAME (w));
30088 if (w->mini || w->pseudo_window_p)
30089 return;
30090 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30092 int x0 = WINDOW_LEFT_EDGE_X (w);
30093 int x1 = WINDOW_RIGHT_EDGE_X (w);
30094 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30095 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30097 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30101 /* Redraw the part of window W intersection rectangle FR. Pixel
30102 coordinates in FR are frame-relative. Call this function with
30103 input blocked. Value is true if the exposure overwrites
30104 mouse-face. */
30106 static bool
30107 expose_window (struct window *w, XRectangle *fr)
30109 struct frame *f = XFRAME (w->frame);
30110 XRectangle wr, r;
30111 bool mouse_face_overwritten_p = false;
30113 /* If window is not yet fully initialized, do nothing. This can
30114 happen when toolkit scroll bars are used and a window is split.
30115 Reconfiguring the scroll bar will generate an expose for a newly
30116 created window. */
30117 if (w->current_matrix == NULL)
30118 return false;
30120 /* When we're currently updating the window, display and current
30121 matrix usually don't agree. Arrange for a thorough display
30122 later. */
30123 if (w->must_be_updated_p)
30125 SET_FRAME_GARBAGED (f);
30126 return false;
30129 /* Frame-relative pixel rectangle of W. */
30130 wr.x = WINDOW_LEFT_EDGE_X (w);
30131 wr.y = WINDOW_TOP_EDGE_Y (w);
30132 wr.width = WINDOW_PIXEL_WIDTH (w);
30133 wr.height = WINDOW_PIXEL_HEIGHT (w);
30135 if (x_intersect_rectangles (fr, &wr, &r))
30137 int yb = window_text_bottom_y (w);
30138 struct glyph_row *row;
30139 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30141 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30142 r.x, r.y, r.width, r.height));
30144 /* Convert to window coordinates. */
30145 r.x -= WINDOW_LEFT_EDGE_X (w);
30146 r.y -= WINDOW_TOP_EDGE_Y (w);
30148 /* Turn off the cursor. */
30149 bool cursor_cleared_p = (!w->pseudo_window_p
30150 && phys_cursor_in_rect_p (w, &r));
30151 if (cursor_cleared_p)
30152 x_clear_cursor (w);
30154 /* If the row containing the cursor extends face to end of line,
30155 then expose_area might overwrite the cursor outside the
30156 rectangle and thus notice_overwritten_cursor might clear
30157 w->phys_cursor_on_p. We remember the original value and
30158 check later if it is changed. */
30159 bool phys_cursor_on_p = w->phys_cursor_on_p;
30161 /* Update lines intersecting rectangle R. */
30162 first_overlapping_row = last_overlapping_row = NULL;
30163 for (row = w->current_matrix->rows;
30164 row->enabled_p;
30165 ++row)
30167 int y0 = row->y;
30168 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30170 if ((y0 >= r.y && y0 < r.y + r.height)
30171 || (y1 > r.y && y1 < r.y + r.height)
30172 || (r.y >= y0 && r.y < y1)
30173 || (r.y + r.height > y0 && r.y + r.height < y1))
30175 /* A header line may be overlapping, but there is no need
30176 to fix overlapping areas for them. KFS 2005-02-12 */
30177 if (row->overlapping_p && !row->mode_line_p)
30179 if (first_overlapping_row == NULL)
30180 first_overlapping_row = row;
30181 last_overlapping_row = row;
30184 row->clip = fr;
30185 if (expose_line (w, row, &r))
30186 mouse_face_overwritten_p = true;
30187 row->clip = NULL;
30189 else if (row->overlapping_p)
30191 /* We must redraw a row overlapping the exposed area. */
30192 if (y0 < r.y
30193 ? y0 + row->phys_height > r.y
30194 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30196 if (first_overlapping_row == NULL)
30197 first_overlapping_row = row;
30198 last_overlapping_row = row;
30202 if (y1 >= yb)
30203 break;
30206 /* Display the mode line if there is one. */
30207 if (WINDOW_WANTS_MODELINE_P (w)
30208 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30209 row->enabled_p)
30210 && row->y < r.y + r.height)
30212 if (expose_line (w, row, &r))
30213 mouse_face_overwritten_p = true;
30216 if (!w->pseudo_window_p)
30218 /* Fix the display of overlapping rows. */
30219 if (first_overlapping_row)
30220 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30221 fr);
30223 /* Draw border between windows. */
30224 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30225 x_draw_right_divider (w);
30226 else
30227 x_draw_vertical_border (w);
30229 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30230 x_draw_bottom_divider (w);
30232 /* Turn the cursor on again. */
30233 if (cursor_cleared_p
30234 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30235 update_window_cursor (w, true);
30239 return mouse_face_overwritten_p;
30244 /* Redraw (parts) of all windows in the window tree rooted at W that
30245 intersect R. R contains frame pixel coordinates. Value is
30246 true if the exposure overwrites mouse-face. */
30248 static bool
30249 expose_window_tree (struct window *w, XRectangle *r)
30251 struct frame *f = XFRAME (w->frame);
30252 bool mouse_face_overwritten_p = false;
30254 while (w && !FRAME_GARBAGED_P (f))
30256 mouse_face_overwritten_p
30257 |= (WINDOWP (w->contents)
30258 ? expose_window_tree (XWINDOW (w->contents), r)
30259 : expose_window (w, r));
30261 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30264 return mouse_face_overwritten_p;
30268 /* EXPORT:
30269 Redisplay an exposed area of frame F. X and Y are the upper-left
30270 corner of the exposed rectangle. W and H are width and height of
30271 the exposed area. All are pixel values. W or H zero means redraw
30272 the entire frame. */
30274 void
30275 expose_frame (struct frame *f, int x, int y, int w, int h)
30277 XRectangle r;
30278 bool mouse_face_overwritten_p = false;
30280 TRACE ((stderr, "expose_frame "));
30282 /* No need to redraw if frame will be redrawn soon. */
30283 if (FRAME_GARBAGED_P (f))
30285 TRACE ((stderr, " garbaged\n"));
30286 return;
30289 /* If basic faces haven't been realized yet, there is no point in
30290 trying to redraw anything. This can happen when we get an expose
30291 event while Emacs is starting, e.g. by moving another window. */
30292 if (FRAME_FACE_CACHE (f) == NULL
30293 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30295 TRACE ((stderr, " no faces\n"));
30296 return;
30299 if (w == 0 || h == 0)
30301 r.x = r.y = 0;
30302 r.width = FRAME_TEXT_WIDTH (f);
30303 r.height = FRAME_TEXT_HEIGHT (f);
30305 else
30307 r.x = x;
30308 r.y = y;
30309 r.width = w;
30310 r.height = h;
30313 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30314 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30316 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30317 if (WINDOWP (f->tool_bar_window))
30318 mouse_face_overwritten_p
30319 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30320 #endif
30322 #ifdef HAVE_X_WINDOWS
30323 #ifndef MSDOS
30324 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30325 if (WINDOWP (f->menu_bar_window))
30326 mouse_face_overwritten_p
30327 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30328 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30329 #endif
30330 #endif
30332 /* Some window managers support a focus-follows-mouse style with
30333 delayed raising of frames. Imagine a partially obscured frame,
30334 and moving the mouse into partially obscured mouse-face on that
30335 frame. The visible part of the mouse-face will be highlighted,
30336 then the WM raises the obscured frame. With at least one WM, KDE
30337 2.1, Emacs is not getting any event for the raising of the frame
30338 (even tried with SubstructureRedirectMask), only Expose events.
30339 These expose events will draw text normally, i.e. not
30340 highlighted. Which means we must redo the highlight here.
30341 Subsume it under ``we love X''. --gerd 2001-08-15 */
30342 /* Included in Windows version because Windows most likely does not
30343 do the right thing if any third party tool offers
30344 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30345 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30347 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30348 if (f == hlinfo->mouse_face_mouse_frame)
30350 int mouse_x = hlinfo->mouse_face_mouse_x;
30351 int mouse_y = hlinfo->mouse_face_mouse_y;
30352 clear_mouse_face (hlinfo);
30353 note_mouse_highlight (f, mouse_x, mouse_y);
30359 /* EXPORT:
30360 Determine the intersection of two rectangles R1 and R2. Return
30361 the intersection in *RESULT. Value is true if RESULT is not
30362 empty. */
30364 bool
30365 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30367 XRectangle *left, *right;
30368 XRectangle *upper, *lower;
30369 bool intersection_p = false;
30371 /* Rearrange so that R1 is the left-most rectangle. */
30372 if (r1->x < r2->x)
30373 left = r1, right = r2;
30374 else
30375 left = r2, right = r1;
30377 /* X0 of the intersection is right.x0, if this is inside R1,
30378 otherwise there is no intersection. */
30379 if (right->x <= left->x + left->width)
30381 result->x = right->x;
30383 /* The right end of the intersection is the minimum of
30384 the right ends of left and right. */
30385 result->width = (min (left->x + left->width, right->x + right->width)
30386 - result->x);
30388 /* Same game for Y. */
30389 if (r1->y < r2->y)
30390 upper = r1, lower = r2;
30391 else
30392 upper = r2, lower = r1;
30394 /* The upper end of the intersection is lower.y0, if this is inside
30395 of upper. Otherwise, there is no intersection. */
30396 if (lower->y <= upper->y + upper->height)
30398 result->y = lower->y;
30400 /* The lower end of the intersection is the minimum of the lower
30401 ends of upper and lower. */
30402 result->height = (min (lower->y + lower->height,
30403 upper->y + upper->height)
30404 - result->y);
30405 intersection_p = true;
30409 return intersection_p;
30412 #endif /* HAVE_WINDOW_SYSTEM */
30415 /***********************************************************************
30416 Initialization
30417 ***********************************************************************/
30419 void
30420 syms_of_xdisp (void)
30422 Vwith_echo_area_save_vector = Qnil;
30423 staticpro (&Vwith_echo_area_save_vector);
30425 Vmessage_stack = Qnil;
30426 staticpro (&Vmessage_stack);
30428 /* Non-nil means don't actually do any redisplay. */
30429 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30431 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30433 message_dolog_marker1 = Fmake_marker ();
30434 staticpro (&message_dolog_marker1);
30435 message_dolog_marker2 = Fmake_marker ();
30436 staticpro (&message_dolog_marker2);
30437 message_dolog_marker3 = Fmake_marker ();
30438 staticpro (&message_dolog_marker3);
30440 #ifdef GLYPH_DEBUG
30441 defsubr (&Sdump_frame_glyph_matrix);
30442 defsubr (&Sdump_glyph_matrix);
30443 defsubr (&Sdump_glyph_row);
30444 defsubr (&Sdump_tool_bar_row);
30445 defsubr (&Strace_redisplay);
30446 defsubr (&Strace_to_stderr);
30447 #endif
30448 #ifdef HAVE_WINDOW_SYSTEM
30449 defsubr (&Stool_bar_height);
30450 defsubr (&Slookup_image_map);
30451 #endif
30452 defsubr (&Sline_pixel_height);
30453 defsubr (&Sformat_mode_line);
30454 defsubr (&Sinvisible_p);
30455 defsubr (&Scurrent_bidi_paragraph_direction);
30456 defsubr (&Swindow_text_pixel_size);
30457 defsubr (&Smove_point_visually);
30458 defsubr (&Sbidi_find_overridden_directionality);
30460 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30461 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30462 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30463 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30464 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30465 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30466 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30467 DEFSYM (Qeval, "eval");
30468 DEFSYM (QCdata, ":data");
30470 /* Names of text properties relevant for redisplay. */
30471 DEFSYM (Qdisplay, "display");
30472 DEFSYM (Qspace_width, "space-width");
30473 DEFSYM (Qraise, "raise");
30474 DEFSYM (Qslice, "slice");
30475 DEFSYM (Qspace, "space");
30476 DEFSYM (Qmargin, "margin");
30477 DEFSYM (Qpointer, "pointer");
30478 DEFSYM (Qleft_margin, "left-margin");
30479 DEFSYM (Qright_margin, "right-margin");
30480 DEFSYM (Qcenter, "center");
30481 DEFSYM (Qline_height, "line-height");
30482 DEFSYM (QCalign_to, ":align-to");
30483 DEFSYM (QCrelative_width, ":relative-width");
30484 DEFSYM (QCrelative_height, ":relative-height");
30485 DEFSYM (QCeval, ":eval");
30486 DEFSYM (QCpropertize, ":propertize");
30487 DEFSYM (QCfile, ":file");
30488 DEFSYM (Qfontified, "fontified");
30489 DEFSYM (Qfontification_functions, "fontification-functions");
30491 /* Name of the face used to highlight trailing whitespace. */
30492 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30494 /* Name and number of the face used to highlight escape glyphs. */
30495 DEFSYM (Qescape_glyph, "escape-glyph");
30497 /* Name and number of the face used to highlight non-breaking spaces. */
30498 DEFSYM (Qnobreak_space, "nobreak-space");
30500 /* The symbol 'image' which is the car of the lists used to represent
30501 images in Lisp. Also a tool bar style. */
30502 DEFSYM (Qimage, "image");
30504 /* Tool bar styles. */
30505 DEFSYM (Qtext, "text");
30506 DEFSYM (Qboth, "both");
30507 DEFSYM (Qboth_horiz, "both-horiz");
30508 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30510 /* The image map types. */
30511 DEFSYM (QCmap, ":map");
30512 DEFSYM (QCpointer, ":pointer");
30513 DEFSYM (Qrect, "rect");
30514 DEFSYM (Qcircle, "circle");
30515 DEFSYM (Qpoly, "poly");
30517 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30518 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30519 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30521 DEFSYM (Qgrow_only, "grow-only");
30522 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30523 DEFSYM (Qposition, "position");
30524 DEFSYM (Qbuffer_position, "buffer-position");
30525 DEFSYM (Qobject, "object");
30527 /* Cursor shapes. */
30528 DEFSYM (Qbar, "bar");
30529 DEFSYM (Qhbar, "hbar");
30530 DEFSYM (Qbox, "box");
30531 DEFSYM (Qhollow, "hollow");
30533 /* Pointer shapes. */
30534 DEFSYM (Qhand, "hand");
30535 DEFSYM (Qarrow, "arrow");
30536 /* also Qtext */
30538 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30540 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30541 staticpro (&list_of_error);
30543 /* Values of those variables at last redisplay are stored as
30544 properties on 'overlay-arrow-position' symbol. However, if
30545 Voverlay_arrow_position is a marker, last-arrow-position is its
30546 numerical position. */
30547 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30548 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30550 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30551 properties on a symbol in overlay-arrow-variable-list. */
30552 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30553 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30555 echo_buffer[0] = echo_buffer[1] = Qnil;
30556 staticpro (&echo_buffer[0]);
30557 staticpro (&echo_buffer[1]);
30559 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30560 staticpro (&echo_area_buffer[0]);
30561 staticpro (&echo_area_buffer[1]);
30563 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30564 staticpro (&Vmessages_buffer_name);
30566 mode_line_proptrans_alist = Qnil;
30567 staticpro (&mode_line_proptrans_alist);
30568 mode_line_string_list = Qnil;
30569 staticpro (&mode_line_string_list);
30570 mode_line_string_face = Qnil;
30571 staticpro (&mode_line_string_face);
30572 mode_line_string_face_prop = Qnil;
30573 staticpro (&mode_line_string_face_prop);
30574 Vmode_line_unwind_vector = Qnil;
30575 staticpro (&Vmode_line_unwind_vector);
30577 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30579 help_echo_string = Qnil;
30580 staticpro (&help_echo_string);
30581 help_echo_object = Qnil;
30582 staticpro (&help_echo_object);
30583 help_echo_window = Qnil;
30584 staticpro (&help_echo_window);
30585 previous_help_echo_string = Qnil;
30586 staticpro (&previous_help_echo_string);
30587 help_echo_pos = -1;
30589 DEFSYM (Qright_to_left, "right-to-left");
30590 DEFSYM (Qleft_to_right, "left-to-right");
30591 defsubr (&Sbidi_resolved_levels);
30593 #ifdef HAVE_WINDOW_SYSTEM
30594 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30595 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30596 For example, if a block cursor is over a tab, it will be drawn as
30597 wide as that tab on the display. */);
30598 x_stretch_cursor_p = 0;
30599 #endif
30601 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30602 doc: /* Non-nil means highlight trailing whitespace.
30603 The face used for trailing whitespace is `trailing-whitespace'. */);
30604 Vshow_trailing_whitespace = Qnil;
30606 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30607 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30608 If the value is t, Emacs highlights non-ASCII chars which have the
30609 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30610 or `escape-glyph' face respectively.
30612 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30613 U+2011 (non-breaking hyphen) are affected.
30615 Any other non-nil value means to display these characters as a escape
30616 glyph followed by an ordinary space or hyphen.
30618 A value of nil means no special handling of these characters. */);
30619 Vnobreak_char_display = Qt;
30621 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30622 doc: /* The pointer shape to show in void text areas.
30623 A value of nil means to show the text pointer. Other options are
30624 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30625 `hourglass'. */);
30626 Vvoid_text_area_pointer = Qarrow;
30628 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30629 doc: /* Non-nil means don't actually do any redisplay.
30630 This is used for internal purposes. */);
30631 Vinhibit_redisplay = Qnil;
30633 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30634 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30635 Vglobal_mode_string = Qnil;
30637 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30638 doc: /* Marker for where to display an arrow on top of the buffer text.
30639 This must be the beginning of a line in order to work.
30640 See also `overlay-arrow-string'. */);
30641 Voverlay_arrow_position = Qnil;
30643 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30644 doc: /* String to display as an arrow in non-window frames.
30645 See also `overlay-arrow-position'. */);
30646 Voverlay_arrow_string = build_pure_c_string ("=>");
30648 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30649 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30650 The symbols on this list are examined during redisplay to determine
30651 where to display overlay arrows. */);
30652 Voverlay_arrow_variable_list
30653 = list1 (intern_c_string ("overlay-arrow-position"));
30655 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30656 doc: /* The number of lines to try scrolling a window by when point moves out.
30657 If that fails to bring point back on frame, point is centered instead.
30658 If this is zero, point is always centered after it moves off frame.
30659 If you want scrolling to always be a line at a time, you should set
30660 `scroll-conservatively' to a large value rather than set this to 1. */);
30662 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30663 doc: /* Scroll up to this many lines, to bring point back on screen.
30664 If point moves off-screen, redisplay will scroll by up to
30665 `scroll-conservatively' lines in order to bring point just barely
30666 onto the screen again. If that cannot be done, then redisplay
30667 recenters point as usual.
30669 If the value is greater than 100, redisplay will never recenter point,
30670 but will always scroll just enough text to bring point into view, even
30671 if you move far away.
30673 A value of zero means always recenter point if it moves off screen. */);
30674 scroll_conservatively = 0;
30676 DEFVAR_INT ("scroll-margin", scroll_margin,
30677 doc: /* Number of lines of margin at the top and bottom of a window.
30678 Recenter the window whenever point gets within this many lines
30679 of the top or bottom of the window. */);
30680 scroll_margin = 0;
30682 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30683 doc: /* Pixels per inch value for non-window system displays.
30684 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30685 Vdisplay_pixels_per_inch = make_float (72.0);
30687 #ifdef GLYPH_DEBUG
30688 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30689 #endif
30691 DEFVAR_LISP ("truncate-partial-width-windows",
30692 Vtruncate_partial_width_windows,
30693 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30694 For an integer value, truncate lines in each window narrower than the
30695 full frame width, provided the window width is less than that integer;
30696 otherwise, respect the value of `truncate-lines'.
30698 For any other non-nil value, truncate lines in all windows that do
30699 not span the full frame width.
30701 A value of nil means to respect the value of `truncate-lines'.
30703 If `word-wrap' is enabled, you might want to reduce this. */);
30704 Vtruncate_partial_width_windows = make_number (50);
30706 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30707 doc: /* Maximum buffer size for which line number should be displayed.
30708 If the buffer is bigger than this, the line number does not appear
30709 in the mode line. A value of nil means no limit. */);
30710 Vline_number_display_limit = Qnil;
30712 DEFVAR_INT ("line-number-display-limit-width",
30713 line_number_display_limit_width,
30714 doc: /* Maximum line width (in characters) for line number display.
30715 If the average length of the lines near point is bigger than this, then the
30716 line number may be omitted from the mode line. */);
30717 line_number_display_limit_width = 200;
30719 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30720 doc: /* Non-nil means highlight region even in nonselected windows. */);
30721 highlight_nonselected_windows = false;
30723 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30724 doc: /* Non-nil if more than one frame is visible on this display.
30725 Minibuffer-only frames don't count, but iconified frames do.
30726 This variable is not guaranteed to be accurate except while processing
30727 `frame-title-format' and `icon-title-format'. */);
30729 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30730 doc: /* Template for displaying the title bar of visible frames.
30731 \(Assuming the window manager supports this feature.)
30733 This variable has the same structure as `mode-line-format', except that
30734 the %c and %l constructs are ignored. It is used only on frames for
30735 which no explicit name has been set \(see `modify-frame-parameters'). */);
30737 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30738 doc: /* Template for displaying the title bar of an iconified frame.
30739 \(Assuming the window manager supports this feature.)
30740 This variable has the same structure as `mode-line-format' (which see),
30741 and is used only on frames for which no explicit name has been set
30742 \(see `modify-frame-parameters'). */);
30743 Vicon_title_format
30744 = Vframe_title_format
30745 = listn (CONSTYPE_PURE, 3,
30746 intern_c_string ("multiple-frames"),
30747 build_pure_c_string ("%b"),
30748 listn (CONSTYPE_PURE, 4,
30749 empty_unibyte_string,
30750 intern_c_string ("invocation-name"),
30751 build_pure_c_string ("@"),
30752 intern_c_string ("system-name")));
30754 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30755 doc: /* Maximum number of lines to keep in the message log buffer.
30756 If nil, disable message logging. If t, log messages but don't truncate
30757 the buffer when it becomes large. */);
30758 Vmessage_log_max = make_number (1000);
30760 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30761 doc: /* Functions called before redisplay, if window sizes have changed.
30762 The value should be a list of functions that take one argument.
30763 Just before redisplay, for each frame, if any of its windows have changed
30764 size since the last redisplay, or have been split or deleted,
30765 all the functions in the list are called, with the frame as argument. */);
30766 Vwindow_size_change_functions = Qnil;
30768 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30769 doc: /* List of functions to call before redisplaying a window with scrolling.
30770 Each function is called with two arguments, the window and its new
30771 display-start position.
30772 These functions are called whenever the `window-start' marker is modified,
30773 either to point into another buffer (e.g. via `set-window-buffer') or another
30774 place in the same buffer.
30775 Note that the value of `window-end' is not valid when these functions are
30776 called.
30778 Warning: Do not use this feature to alter the way the window
30779 is scrolled. It is not designed for that, and such use probably won't
30780 work. */);
30781 Vwindow_scroll_functions = Qnil;
30783 DEFVAR_LISP ("window-text-change-functions",
30784 Vwindow_text_change_functions,
30785 doc: /* Functions to call in redisplay when text in the window might change. */);
30786 Vwindow_text_change_functions = Qnil;
30788 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30789 doc: /* Functions called when redisplay of a window reaches the end trigger.
30790 Each function is called with two arguments, the window and the end trigger value.
30791 See `set-window-redisplay-end-trigger'. */);
30792 Vredisplay_end_trigger_functions = Qnil;
30794 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30795 doc: /* Non-nil means autoselect window with mouse pointer.
30796 If nil, do not autoselect windows.
30797 A positive number means delay autoselection by that many seconds: a
30798 window is autoselected only after the mouse has remained in that
30799 window for the duration of the delay.
30800 A negative number has a similar effect, but causes windows to be
30801 autoselected only after the mouse has stopped moving. \(Because of
30802 the way Emacs compares mouse events, you will occasionally wait twice
30803 that time before the window gets selected.\)
30804 Any other value means to autoselect window instantaneously when the
30805 mouse pointer enters it.
30807 Autoselection selects the minibuffer only if it is active, and never
30808 unselects the minibuffer if it is active.
30810 When customizing this variable make sure that the actual value of
30811 `focus-follows-mouse' matches the behavior of your window manager. */);
30812 Vmouse_autoselect_window = Qnil;
30814 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30815 doc: /* Non-nil means automatically resize tool-bars.
30816 This dynamically changes the tool-bar's height to the minimum height
30817 that is needed to make all tool-bar items visible.
30818 If value is `grow-only', the tool-bar's height is only increased
30819 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30820 Vauto_resize_tool_bars = Qt;
30822 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30823 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30824 auto_raise_tool_bar_buttons_p = true;
30826 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30827 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30828 make_cursor_line_fully_visible_p = true;
30830 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30831 doc: /* Border below tool-bar in pixels.
30832 If an integer, use it as the height of the border.
30833 If it is one of `internal-border-width' or `border-width', use the
30834 value of the corresponding frame parameter.
30835 Otherwise, no border is added below the tool-bar. */);
30836 Vtool_bar_border = Qinternal_border_width;
30838 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30839 doc: /* Margin around tool-bar buttons in pixels.
30840 If an integer, use that for both horizontal and vertical margins.
30841 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30842 HORZ specifying the horizontal margin, and VERT specifying the
30843 vertical margin. */);
30844 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30846 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30847 doc: /* Relief thickness of tool-bar buttons. */);
30848 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30850 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30851 doc: /* Tool bar style to use.
30852 It can be one of
30853 image - show images only
30854 text - show text only
30855 both - show both, text below image
30856 both-horiz - show text to the right of the image
30857 text-image-horiz - show text to the left of the image
30858 any other - use system default or image if no system default.
30860 This variable only affects the GTK+ toolkit version of Emacs. */);
30861 Vtool_bar_style = Qnil;
30863 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30864 doc: /* Maximum number of characters a label can have to be shown.
30865 The tool bar style must also show labels for this to have any effect, see
30866 `tool-bar-style'. */);
30867 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30869 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30870 doc: /* List of functions to call to fontify regions of text.
30871 Each function is called with one argument POS. Functions must
30872 fontify a region starting at POS in the current buffer, and give
30873 fontified regions the property `fontified'. */);
30874 Vfontification_functions = Qnil;
30875 Fmake_variable_buffer_local (Qfontification_functions);
30877 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30878 unibyte_display_via_language_environment,
30879 doc: /* Non-nil means display unibyte text according to language environment.
30880 Specifically, this means that raw bytes in the range 160-255 decimal
30881 are displayed by converting them to the equivalent multibyte characters
30882 according to the current language environment. As a result, they are
30883 displayed according to the current fontset.
30885 Note that this variable affects only how these bytes are displayed,
30886 but does not change the fact they are interpreted as raw bytes. */);
30887 unibyte_display_via_language_environment = false;
30889 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30890 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30891 If a float, it specifies a fraction of the mini-window frame's height.
30892 If an integer, it specifies a number of lines. */);
30893 Vmax_mini_window_height = make_float (0.25);
30895 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30896 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30897 A value of nil means don't automatically resize mini-windows.
30898 A value of t means resize them to fit the text displayed in them.
30899 A value of `grow-only', the default, means let mini-windows grow only;
30900 they return to their normal size when the minibuffer is closed, or the
30901 echo area becomes empty. */);
30902 Vresize_mini_windows = Qgrow_only;
30904 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30905 doc: /* Alist specifying how to blink the cursor off.
30906 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30907 `cursor-type' frame-parameter or variable equals ON-STATE,
30908 comparing using `equal', Emacs uses OFF-STATE to specify
30909 how to blink it off. ON-STATE and OFF-STATE are values for
30910 the `cursor-type' frame parameter.
30912 If a frame's ON-STATE has no entry in this list,
30913 the frame's other specifications determine how to blink the cursor off. */);
30914 Vblink_cursor_alist = Qnil;
30916 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30917 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30918 If non-nil, windows are automatically scrolled horizontally to make
30919 point visible. */);
30920 automatic_hscrolling_p = true;
30921 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30923 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30924 doc: /* How many columns away from the window edge point is allowed to get
30925 before automatic hscrolling will horizontally scroll the window. */);
30926 hscroll_margin = 5;
30928 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30929 doc: /* How many columns to scroll the window when point gets too close to the edge.
30930 When point is less than `hscroll-margin' columns from the window
30931 edge, automatic hscrolling will scroll the window by the amount of columns
30932 determined by this variable. If its value is a positive integer, scroll that
30933 many columns. If it's a positive floating-point number, it specifies the
30934 fraction of the window's width to scroll. If it's nil or zero, point will be
30935 centered horizontally after the scroll. Any other value, including negative
30936 numbers, are treated as if the value were zero.
30938 Automatic hscrolling always moves point outside the scroll margin, so if
30939 point was more than scroll step columns inside the margin, the window will
30940 scroll more than the value given by the scroll step.
30942 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30943 and `scroll-right' overrides this variable's effect. */);
30944 Vhscroll_step = make_number (0);
30946 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30947 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30948 Bind this around calls to `message' to let it take effect. */);
30949 message_truncate_lines = false;
30951 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30952 doc: /* Normal hook run to update the menu bar definitions.
30953 Redisplay runs this hook before it redisplays the menu bar.
30954 This is used to update menus such as Buffers, whose contents depend on
30955 various data. */);
30956 Vmenu_bar_update_hook = Qnil;
30958 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30959 doc: /* Frame for which we are updating a menu.
30960 The enable predicate for a menu binding should check this variable. */);
30961 Vmenu_updating_frame = Qnil;
30963 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30964 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30965 inhibit_menubar_update = false;
30967 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30968 doc: /* Prefix prepended to all continuation lines at display time.
30969 The value may be a string, an image, or a stretch-glyph; it is
30970 interpreted in the same way as the value of a `display' text property.
30972 This variable is overridden by any `wrap-prefix' text or overlay
30973 property.
30975 To add a prefix to non-continuation lines, use `line-prefix'. */);
30976 Vwrap_prefix = Qnil;
30977 DEFSYM (Qwrap_prefix, "wrap-prefix");
30978 Fmake_variable_buffer_local (Qwrap_prefix);
30980 DEFVAR_LISP ("line-prefix", Vline_prefix,
30981 doc: /* Prefix prepended to all non-continuation lines at display time.
30982 The value may be a string, an image, or a stretch-glyph; it is
30983 interpreted in the same way as the value of a `display' text property.
30985 This variable is overridden by any `line-prefix' text or overlay
30986 property.
30988 To add a prefix to continuation lines, use `wrap-prefix'. */);
30989 Vline_prefix = Qnil;
30990 DEFSYM (Qline_prefix, "line-prefix");
30991 Fmake_variable_buffer_local (Qline_prefix);
30993 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30994 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30995 inhibit_eval_during_redisplay = false;
30997 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30998 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30999 inhibit_free_realized_faces = false;
31001 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31002 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31003 Intended for use during debugging and for testing bidi display;
31004 see biditest.el in the test suite. */);
31005 inhibit_bidi_mirroring = false;
31007 #ifdef GLYPH_DEBUG
31008 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31009 doc: /* Inhibit try_window_id display optimization. */);
31010 inhibit_try_window_id = false;
31012 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31013 doc: /* Inhibit try_window_reusing display optimization. */);
31014 inhibit_try_window_reusing = false;
31016 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31017 doc: /* Inhibit try_cursor_movement display optimization. */);
31018 inhibit_try_cursor_movement = false;
31019 #endif /* GLYPH_DEBUG */
31021 DEFVAR_INT ("overline-margin", overline_margin,
31022 doc: /* Space between overline and text, in pixels.
31023 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31024 margin to the character height. */);
31025 overline_margin = 2;
31027 DEFVAR_INT ("underline-minimum-offset",
31028 underline_minimum_offset,
31029 doc: /* Minimum distance between baseline and underline.
31030 This can improve legibility of underlined text at small font sizes,
31031 particularly when using variable `x-use-underline-position-properties'
31032 with fonts that specify an UNDERLINE_POSITION relatively close to the
31033 baseline. The default value is 1. */);
31034 underline_minimum_offset = 1;
31036 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31037 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31038 This feature only works when on a window system that can change
31039 cursor shapes. */);
31040 display_hourglass_p = true;
31042 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31043 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31044 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31046 #ifdef HAVE_WINDOW_SYSTEM
31047 hourglass_atimer = NULL;
31048 hourglass_shown_p = false;
31049 #endif /* HAVE_WINDOW_SYSTEM */
31051 /* Name of the face used to display glyphless characters. */
31052 DEFSYM (Qglyphless_char, "glyphless-char");
31054 /* Method symbols for Vglyphless_char_display. */
31055 DEFSYM (Qhex_code, "hex-code");
31056 DEFSYM (Qempty_box, "empty-box");
31057 DEFSYM (Qthin_space, "thin-space");
31058 DEFSYM (Qzero_width, "zero-width");
31060 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31061 doc: /* Function run just before redisplay.
31062 It is called with one argument, which is the set of windows that are to
31063 be redisplayed. This set can be nil (meaning, only the selected window),
31064 or t (meaning all windows). */);
31065 Vpre_redisplay_function = intern ("ignore");
31067 /* Symbol for the purpose of Vglyphless_char_display. */
31068 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31069 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31071 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31072 doc: /* Char-table defining glyphless characters.
31073 Each element, if non-nil, should be one of the following:
31074 an ASCII acronym string: display this string in a box
31075 `hex-code': display the hexadecimal code of a character in a box
31076 `empty-box': display as an empty box
31077 `thin-space': display as 1-pixel width space
31078 `zero-width': don't display
31079 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31080 display method for graphical terminals and text terminals respectively.
31081 GRAPHICAL and TEXT should each have one of the values listed above.
31083 The char-table has one extra slot to control the display of a character for
31084 which no font is found. This slot only takes effect on graphical terminals.
31085 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31086 `thin-space'. The default is `empty-box'.
31088 If a character has a non-nil entry in an active display table, the
31089 display table takes effect; in this case, Emacs does not consult
31090 `glyphless-char-display' at all. */);
31091 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31092 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31093 Qempty_box);
31095 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31096 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31097 Vdebug_on_message = Qnil;
31099 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31100 doc: /* */);
31101 Vredisplay__all_windows_cause
31102 = Fmake_vector (make_number (100), make_number (0));
31104 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31105 doc: /* */);
31106 Vredisplay__mode_lines_cause
31107 = Fmake_vector (make_number (100), make_number (0));
31111 /* Initialize this module when Emacs starts. */
31113 void
31114 init_xdisp (void)
31116 CHARPOS (this_line_start_pos) = 0;
31118 if (!noninteractive)
31120 struct window *m = XWINDOW (minibuf_window);
31121 Lisp_Object frame = m->frame;
31122 struct frame *f = XFRAME (frame);
31123 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31124 struct window *r = XWINDOW (root);
31125 int i;
31127 echo_area_window = minibuf_window;
31129 r->top_line = FRAME_TOP_MARGIN (f);
31130 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31131 r->total_cols = FRAME_COLS (f);
31132 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31133 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31134 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31136 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31137 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31138 m->total_cols = FRAME_COLS (f);
31139 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31140 m->total_lines = 1;
31141 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31143 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31144 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31145 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31147 /* The default ellipsis glyphs `...'. */
31148 for (i = 0; i < 3; ++i)
31149 default_invis_vector[i] = make_number ('.');
31153 /* Allocate the buffer for frame titles.
31154 Also used for `format-mode-line'. */
31155 int size = 100;
31156 mode_line_noprop_buf = xmalloc (size);
31157 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31158 mode_line_noprop_ptr = mode_line_noprop_buf;
31159 mode_line_target = MODE_LINE_DISPLAY;
31162 help_echo_showing_p = false;
31165 #ifdef HAVE_WINDOW_SYSTEM
31167 /* Platform-independent portion of hourglass implementation. */
31169 /* Timer function of hourglass_atimer. */
31171 static void
31172 show_hourglass (struct atimer *timer)
31174 /* The timer implementation will cancel this timer automatically
31175 after this function has run. Set hourglass_atimer to null
31176 so that we know the timer doesn't have to be canceled. */
31177 hourglass_atimer = NULL;
31179 if (!hourglass_shown_p)
31181 Lisp_Object tail, frame;
31183 block_input ();
31185 FOR_EACH_FRAME (tail, frame)
31187 struct frame *f = XFRAME (frame);
31189 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31190 && FRAME_RIF (f)->show_hourglass)
31191 FRAME_RIF (f)->show_hourglass (f);
31194 hourglass_shown_p = true;
31195 unblock_input ();
31199 /* Cancel a currently active hourglass timer, and start a new one. */
31201 void
31202 start_hourglass (void)
31204 struct timespec delay;
31206 cancel_hourglass ();
31208 if (INTEGERP (Vhourglass_delay)
31209 && XINT (Vhourglass_delay) > 0)
31210 delay = make_timespec (min (XINT (Vhourglass_delay),
31211 TYPE_MAXIMUM (time_t)),
31213 else if (FLOATP (Vhourglass_delay)
31214 && XFLOAT_DATA (Vhourglass_delay) > 0)
31215 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31216 else
31217 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31219 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31220 show_hourglass, NULL);
31223 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31224 shown. */
31226 void
31227 cancel_hourglass (void)
31229 if (hourglass_atimer)
31231 cancel_atimer (hourglass_atimer);
31232 hourglass_atimer = NULL;
31235 if (hourglass_shown_p)
31237 Lisp_Object tail, frame;
31239 block_input ();
31241 FOR_EACH_FRAME (tail, frame)
31243 struct frame *f = XFRAME (frame);
31245 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31246 && FRAME_RIF (f)->hide_hourglass)
31247 FRAME_RIF (f)->hide_hourglass (f);
31248 #ifdef HAVE_NTGUI
31249 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31250 else if (!FRAME_W32_P (f))
31251 w32_arrow_cursor ();
31252 #endif
31255 hourglass_shown_p = false;
31256 unblock_input ();
31260 #endif /* HAVE_WINDOW_SYSTEM */